return mp;
return NULL;
}
+
+struct map_ent *map_by_name(struct map_ent **map, char *name)
+{
+ struct map_ent *mp;
+ if (!*map)
+ map_read(map);
+
+ for (mp = *map ; mp ; mp = mp->next) {
+ if (strncmp(mp->path, "/dev/md/", 8) != 0)
+ continue;
+ if (strcmp(mp->path+8, name) == 0)
+ return mp;
+ }
+ return NULL;
+}
extern int map_update(struct map_ent **mpp, int devnum, char *metadata,
int uuid[4], char *path);
extern struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4]);
-struct map_ent *map_by_devnum(struct map_ent **map, int devnum);
+extern struct map_ent *map_by_devnum(struct map_ent **map, int devnum);
+extern struct map_ent *map_by_name(struct map_ent **map, char *name);
extern void map_read(struct map_ent **melp);
extern int map_write(struct map_ent *mel);
extern void map_delete(struct map_ent **mapp, int devnum);
* reasonable length and remove '/'
*/
char *cp;
+ struct map_ent *map = NULL;
+ int conflict = 1;
+ int unum = 0;
+ int cnlen;
strncpy(cname, name, 200);
cname[200] = 0;
while ((cp = strchr(cname, '/')) != NULL)
*cp = '-';
- if (trustworthy == METADATA)
- /* always add device number to metadata */
- sprintf(cname+strlen(cname), "%d", num);
- else if (trustworthy == FOREIGN &&
- strchr(cname, ':') == NULL)
- /* add _%d to FOREIGN array that don't have
- * a 'host:' prefix
- */
- sprintf(cname+strlen(cname), "_%d", num<0?(-1-num):num);
+ if (trustworthy == LOCAL ||
+ (trustworthy == FOREIGN && strchr(cname, ':') != NULL)) {
+ /* Only need suffix if there is a conflict */
+ if (map_by_name(&map, cname) == NULL)
+ conflict = 0;
+ }
+ cnlen = strlen(cname);
+ while (conflict) {
+ if (trustworthy == METADATA)
+ sprintf(cname+cnlen, "%d", unum);
+ else
+ /* add _%d to FOREIGN array that don't
+ * a 'host:' prefix
+ */
+ sprintf(cname+cnlen, "_%d", unum);
+ unum++;
+ if (map_by_name(&map, cname) == NULL)
+ conflict = 0;
+ }
}
if (cname[0] == 0)
strcpy(chosen, devname);