2 * mapfile - keep track of uuid <-> array mapping. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-2010 Neil Brown <neilb@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Email: <neilb@suse.de>
27 * QVB Post Office, NSW 1230
31 /* The mapfile is used to track arrays being created in --incremental
32 * mode. It particularly allows lookup from UUID to array device, but
33 * also allows the array device name to be easily found.
35 * The map file is line based with space separated fields. The fields are:
36 * Device id - mdX or mdpX where X is a number.
37 * metadata - 0.90 1.0 1.1 1.2 ddf ...
38 * UUID - uuid of the array
39 * path - path where device created: /dev/md/home
41 * The best place for the mapfile is /run/mdadm/map. Distros and users
42 * which have not switched to /run yet can choose a different location
43 * at compile time via MAP_DIR and MAP_FILE.
56 MAP_DIR "/" MAP_FILE ".new",
57 MAP_DIR "/" MAP_FILE ".lock",
61 int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT|O_TRUNC };
62 char *mapsmode[3] = { "r", "w", "w"};
64 FILE *open_map(int modenum)
67 if ((mapmode[modenum] & O_CREAT))
68 /* Attempt to create directory, don't worry about
71 (void)mkdir(mapname[MAP_DIRNAME], 0755);
72 fd = open(mapname[modenum], mapmode[modenum], 0600);
74 return fdopen(fd, mapsmode[modenum]);
78 int map_write(struct map_ent *mel)
83 f = open_map(MAP_NEW);
87 for (; mel; mel = mel->next) {
90 fprintf(f, "%s ", mel->devnm);
91 fprintf(f, "%s ", mel->metadata);
92 fprintf(f, "%08x:%08x:%08x:%08x ", mel->uuid[0],
93 mel->uuid[1], mel->uuid[2], mel->uuid[3]);
94 fprintf(f, "%s\n", mel->path?:"");
103 return rename(mapname[1],
107 static FILE *lf = NULL;
108 int map_lock(struct map_ent **melp)
112 lf = open_map(MAP_LOCK);
115 if (flock(fileno(lf), LOCK_EX) != 0) {
120 if (fstat(fileno(lf), &buf) != 0 ||
122 /* The owner of the lock unlinked it,
123 * so we have a lock on a stale file,
136 void map_unlock(struct map_ent **melp)
139 /* must unlink before closing the file,
140 * as only the owner of the lock may
151 /* We are forking, so must close the lock file.
152 * Don't risk flushing anything though.
161 void map_add(struct map_ent **melp,
162 char * devnm, char *metadata, int uuid[4], char *path)
164 struct map_ent *me = xmalloc(sizeof(*me));
166 strcpy(me->devnm, devnm);
167 strcpy(me->metadata, metadata);
168 memcpy(me->uuid, uuid, 16);
169 me->path = path ? xstrdup(path) : NULL;
175 void map_read(struct map_ent **melp)
186 f = open_map(MAP_READ);
189 f = open_map(MAP_READ);
194 while (fgets(buf, sizeof(buf), f)) {
196 if (sscanf(buf, " %s %s %x:%x:%x:%x %200s",
197 devnm, metadata, uuid, uuid+1,
198 uuid+2, uuid+3, path) >= 7) {
199 map_add(melp, devnm, metadata, uuid, path);
205 void map_free(struct map_ent *map)
208 struct map_ent *mp = map;
215 int map_update(struct map_ent **mpp, char *devnm, char *metadata,
216 int *uuid, char *path)
218 struct map_ent *map, *mp;
226 for (mp = map ; mp ; mp=mp->next)
227 if (strcmp(mp->devnm, devnm) == 0) {
228 strcpy(mp->metadata, metadata);
229 memcpy(mp->uuid, uuid, 16);
231 mp->path = path ? xstrdup(path) : NULL;
236 map_add(&map, devnm, metadata, uuid, path);
244 void map_delete(struct map_ent **mapp, char *devnm)
251 for (mp = *mapp; mp; mp = *mapp) {
252 if (strcmp(mp->devnm, devnm) == 0) {
261 void map_remove(struct map_ent **mapp, char *devnm)
266 map_delete(mapp, devnm);
271 struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
277 for (mp = *map ; mp ; mp = mp->next) {
278 if (memcmp(uuid, mp->uuid, 16) != 0)
280 if (!mddev_busy(mp->devnm)) {
289 struct map_ent *map_by_devnm(struct map_ent **map, char *devnm)
295 for (mp = *map ; mp ; mp = mp->next) {
296 if (strcmp(mp->devnm, devnm) != 0)
298 if (!mddev_busy(mp->devnm)) {
307 struct map_ent *map_by_name(struct map_ent **map, char *name)
313 for (mp = *map ; mp ; mp = mp->next) {
316 if (strncmp(mp->path, "/dev/md/", 8) != 0)
318 if (strcmp(mp->path+8, name) != 0)
320 if (!mddev_busy(mp->devnm)) {
329 /* sets the proper subarray and container_dev according to the metadata
330 * version super_by_fd does this automatically, this routine is meant as
331 * a supplement for guess_super()
333 static char *get_member_info(struct mdstat_ent *ent)
336 if (ent->metadata_version == NULL ||
337 strncmp(ent->metadata_version, "external:", 9) != 0)
340 if (is_subarray(&ent->metadata_version[9])) {
343 subarray = strrchr(ent->metadata_version, '/');
349 void RebuildMap(void)
351 struct mdstat_ent *mdstat = mdstat_read(0, 0);
352 struct mdstat_ent *md;
353 struct map_ent *map = NULL;
354 int require_homehost;
355 char sys_hostname[256];
356 char *homehost = conf_get_homehost(&require_homehost);
358 if (homehost == NULL || strcmp(homehost, "<system>")==0) {
359 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
360 sys_hostname[sizeof(sys_hostname)-1] = 0;
361 homehost = sys_hostname;
365 for (md = mdstat ; md ; md = md->next) {
366 struct mdinfo *sra = sysfs_read(-1, md->devnm, GET_DEVS);
372 for (sd = sra->devs ; sd ; sd = sd->next) {
378 struct supertype *st;
379 char *subarray = NULL;
383 sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
384 dfd = dev_open(dn, O_RDONLY);
387 st = guess_super(dfd);
391 subarray = get_member_info(md);
392 ok = st->ss->load_super(st, dfd, NULL);
398 info = st->ss->container_content(st, subarray);
400 info = xmalloc(sizeof(*info));
401 st->ss->getinfo_super(st, info, NULL);
406 devid = devnm2devid(md->devnm);
407 path = map_dev(major(devid), minor(devid), 0);
409 strncmp(path, "/dev/md/", 8) != 0) {
410 /* We would really like a name that provides
411 * an MD_DEVNAME for udev.
412 * The name needs to be unique both in /dev/md/
413 * and in this mapfile.
414 * It needs to match what -I or -As would come
417 * Check if array is in mdadm.conf
419 * determine trustworthy from homehost etc
420 * find a unique name based on metadata name.
423 struct mddev_ident *match = conf_match(st, info,
427 if (match && match->devname && match->devname[0] == '/') {
428 path = match->devname;
429 if (path[0] != '/') {
430 strcpy(namebuf, "/dev/md/");
431 strcat(namebuf, path);
439 if ((homehost == NULL ||
440 st->ss->match_home(st, homehost) != 1) &&
441 st->ss->match_home(st, "any") != 1 &&
443 || ! conf_name_is_free(info->name)))
444 /* require a numeric suffix */
447 /* allow name to be used as-is if no conflict */
452 if (!isdigit(name[strlen(name)-1]) &&
458 if (strchr(name, ':')) {
459 /* Probably a uniquifying
460 * hostname prefix. Allow
461 * without a suffix, and strip
462 * hostname if it is us.
464 if (homehost && unum == -1 &&
465 strncmp(name, homehost,
466 strlen(homehost)) == 0 &&
467 name[strlen(homehost)] == ':')
468 name += strlen(homehost)+1;
474 sprintf(namebuf, "/dev/md/%s%s%d",
477 sprintf(namebuf, "/dev/md/%s",
480 if (lstat(namebuf, &stb) != 0 &&
482 !map_by_name(&map, namebuf+8)))
488 map_add(&map, md->devnm,
491 st->ss->free_super(st);
497 /* Only trigger a change if we wrote a new map file */
499 for (md = mdstat ; md ; md = md->next) {
500 struct mdinfo *sra = sysfs_read(-1, md->devnm,
503 sysfs_uevent(sra, "change");