2 * mapfile - manage /var/run/mdadm.map. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-2009 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 /* /var/run/mdadm.map is used to track arrays being created in --incremental
32 * more. 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 preferred location for the map file is /var/run/mdadm.map.
42 * However /var/run may not exist or be writable in early boot. And if
43 * no-one has created /var/run/mdadm, we still want to survive.
44 * So possible locations are:
45 * /var/run/mdadm/map /var/run/mdadm.map /dev/.mdadm.map
46 * the last, because udev requires a writable /dev very early.
47 * We read from the first one that exists and write to the first
52 #define mapnames(base) { #base, #base ".new", #base ".lock"}
53 char *mapname[3][3] = {
54 mapnames(/var/run/mdadm/map),
55 mapnames(/var/run/mdadm.map),
56 mapnames(/dev/.mdadm.map)
59 int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT | O_TRUNC };
60 char *mapsmode[3] = { "r", "w", "w"};
62 FILE *open_map(int modenum, int *choice)
65 for (i = 0 ; i < 3 ; i++) {
66 int fd = open(mapname[i][modenum], mapmode[modenum], 0600);
69 return fdopen(fd, mapsmode[modenum]);
75 int map_write(struct map_ent *mel)
81 f = open_map(1, &which);
85 for (; mel; mel = mel->next) {
89 fprintf(f, "mdp%d ", -1-mel->devnum);
91 fprintf(f, "md%d ", mel->devnum);
92 fprintf(f, "%s ", mel->metadata);
93 fprintf(f, "%08x:%08x:%08x:%08x ", mel->uuid[0],
94 mel->uuid[1], mel->uuid[2], mel->uuid[3]);
95 fprintf(f, "%s\n", mel->path?:"");
101 unlink(mapname[which][1]);
104 return rename(mapname[which][1],
105 mapname[which][0]) == 0;
109 static FILE *lf = NULL;
110 static int lwhich = 0;
111 int map_lock(struct map_ent **melp)
114 lf = open_map(2, &lwhich);
117 if (lockf(fileno(lf), F_LOCK, 0) != 0) {
129 void map_unlock(struct map_ent **melp)
133 unlink(mapname[lwhich][2]);
137 void map_add(struct map_ent **melp,
138 int devnum, char *metadata, int uuid[4], char *path)
140 struct map_ent *me = malloc(sizeof(*me));
143 strcpy(me->metadata, metadata);
144 memcpy(me->uuid, uuid, 16);
145 me->path = path ? strdup(path) : NULL;
151 void map_read(struct map_ent **melp)
163 f = open_map(0, &which);
166 f = open_map(0, &which);
171 while (fgets(buf, sizeof(buf), f)) {
173 if (sscanf(buf, " %3[mdp]%d %s %x:%x:%x:%x %200s",
174 nam, &devnum, metadata, uuid, uuid+1,
175 uuid+2, uuid+3, path) >= 7) {
176 if (strncmp(nam, "md", 2) != 0)
179 devnum = -1 - devnum;
180 map_add(melp, devnum, metadata, uuid, path);
186 void map_free(struct map_ent *map)
189 struct map_ent *mp = map;
196 int map_update(struct map_ent **mpp, int devnum, char *metadata,
197 int *uuid, char *path)
199 struct map_ent *map, *mp;
207 for (mp = map ; mp ; mp=mp->next)
208 if (mp->devnum == devnum) {
209 strcpy(mp->metadata, metadata);
210 memcpy(mp->uuid, uuid, 16);
212 mp->path = path ? strdup(path) : NULL;
216 map_add(&map, devnum, metadata, uuid, path);
224 void map_delete(struct map_ent **mapp, int devnum)
231 for (mp = *mapp; mp; mp = *mapp) {
232 if (mp->devnum == devnum) {
241 struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
247 for (mp = *map ; mp ; mp = mp->next) {
248 if (memcmp(uuid, mp->uuid, 16) != 0)
250 if (!mddev_busy(mp->devnum)) {
259 struct map_ent *map_by_devnum(struct map_ent **map, int devnum)
265 for (mp = *map ; mp ; mp = mp->next) {
266 if (mp->devnum != devnum)
268 if (!mddev_busy(mp->devnum)) {
277 struct map_ent *map_by_name(struct map_ent **map, char *name)
283 for (mp = *map ; mp ; mp = mp->next) {
286 if (strncmp(mp->path, "/dev/md/", 8) != 0)
288 if (strcmp(mp->path+8, name) != 0)
290 if (!mddev_busy(mp->devnum)) {
299 void RebuildMap(void)
301 struct mdstat_ent *mdstat = mdstat_read(0, 0);
302 struct mdstat_ent *md;
303 struct map_ent *map = NULL;
304 int mdp = get_mdp_major();
306 for (md = mdstat ; md ; md = md->next) {
307 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS|SKIP_GONE_DEVS);
313 for (sd = sra->devs ; sd ; sd = sd->next) {
317 struct supertype *st;
321 sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
322 dfd = dev_open(dn, O_RDONLY);
325 st = guess_super(dfd);
329 ok = st->ss->load_super(st, dfd, NULL);
333 st->ss->getinfo_super(st, &info);
335 path = map_dev(MD_MAJOR, md->devnum, 0);
337 path = map_dev(mdp, (-1-md->devnum)<< 6, 0);
338 map_add(&map, md->devnum,
341 st->ss->free_super(st);
348 for (md = mdstat ; md ; md = md->next) {
349 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_VERSION);
350 sysfs_uevent(sra, "change");