]> git.neil.brown.name Git - mdadm.git/blob - mapfile.c
Bugfix: mapfile locking is broken/racy
[mdadm.git] / mapfile.c
1 /*
2  * mapfile - manage /var/run/mdadm.map. Part of:
3  * mdadm - manage Linux "md" devices aka RAID arrays.
4  *
5  * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de>
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  *    Author: Neil Brown
23  *    Email: <neilb@suse.de>
24  *    Paper: Neil Brown
25  *           Novell Inc
26  *           GPO Box Q1283
27  *           QVB Post Office, NSW 1230
28  *           Australia
29  */
30
31 /* /var/run/mdadm.map 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.
34  *
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
40  *
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  /lib/initrw/madam/map
46  * The last can easily be change at compile to e.g. somewhere in /dev.
47  * We read from the first one that exists and write to the first
48  * one that we can.
49  */
50 #include        "mdadm.h"
51 #include        <sys/file.h>
52 #include        <ctype.h>
53
54 #define mapnames(base) { base, base ".new", base ".lock"}
55 char *mapname[2][3] = {
56         mapnames(MAP_DIR "/" MAP_FILE),
57         mapnames("/var/run/mdadm.map")
58 };
59 char *mapdir[2] = { MAP_DIR, NULL };
60
61 int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT|O_TRUNC };
62 char *mapsmode[3] = { "r", "w", "w"};
63
64 FILE *open_map(int modenum, int *choice)
65 {
66         int i;
67
68         for (i = 0 ; i < 2 ; i++) {
69                 int fd;
70                 if ((mapmode[modenum] & O_CREAT) && mapdir[i])
71                         /* Attempt to create directory, don't worry about
72                          * failure.
73                          */
74                         mkdir(mapdir[i], 0755);
75                 fd = open(mapname[i][modenum], mapmode[modenum], 0600);
76                 if (fd >= 0) {
77                         *choice = i;
78                         return fdopen(fd, mapsmode[modenum]);
79                 }
80         }
81         return NULL;
82 }
83
84 int map_write(struct map_ent *mel)
85 {
86         FILE *f;
87         int err;
88         int which;
89
90         f = open_map(1, &which);
91
92         if (!f)
93                 return 0;
94         for (; mel; mel = mel->next) {
95                 if (mel->bad)
96                         continue;
97                 if (mel->devnum < 0)
98                         fprintf(f, "mdp%d ", -1-mel->devnum);
99                 else
100                         fprintf(f, "md%d ", mel->devnum);
101                 fprintf(f, "%s ", mel->metadata);
102                 fprintf(f, "%08x:%08x:%08x:%08x ", mel->uuid[0],
103                         mel->uuid[1], mel->uuid[2], mel->uuid[3]);
104                 fprintf(f, "%s\n", mel->path?:"");
105         }
106         fflush(f);
107         err = ferror(f);
108         fclose(f);
109         if (err) {
110                 unlink(mapname[which][1]);
111                 return 0;
112         }
113         return rename(mapname[which][1],
114                       mapname[which][0]) == 0;
115 }
116
117
118 static FILE *lf = NULL;
119 static int lwhich = 0;
120 int map_lock(struct map_ent **melp)
121 {
122         while (lf == NULL) {
123                 struct stat buf;
124                 lf = open_map(2, &lwhich);
125                 if (lf == NULL)
126                         return -1;
127                 if (flock(fileno(lf), LOCK_EX) != 0) {
128                         fclose(lf);
129                         lf = NULL;
130                         return -1;
131                 }
132                 if (fstat(fileno(lf), &buf) != 0 ||
133                     buf.st_nlink == 0) {
134                         /* The owner of the lock unlinked it,
135                          * so we have a lock on a stale file,
136                          * try again
137                          */
138                         fclose(lf);
139                         lf = NULL;
140                 }
141         }
142         if (*melp)
143                 map_free(*melp);
144         map_read(melp);
145         return 0;
146 }
147
148 void map_unlock(struct map_ent **melp)
149 {
150         if (lf) {
151                 /* must unlink before closing the file,
152                  * as only the owner of the lock may
153                  * unlink the file
154                  */
155                 unlink(mapname[lwhich][2]);
156                 fclose(lf);
157         }
158         lf = NULL;
159 }
160
161 void map_add(struct map_ent **melp,
162             int devnum, char *metadata, int uuid[4], char *path)
163 {
164         struct map_ent *me = malloc(sizeof(*me));
165
166         me->devnum = devnum;
167         strcpy(me->metadata, metadata);
168         memcpy(me->uuid, uuid, 16);
169         me->path = path ? strdup(path) : NULL;
170         me->next = *melp;
171         me->bad = 0;
172         *melp = me;
173 }
174
175 void map_read(struct map_ent **melp)
176 {
177         FILE *f;
178         char buf[8192];
179         char path[200];
180         int devnum, uuid[4];
181         char metadata[30];
182         char nam[4];
183         int which;
184
185         *melp = NULL;
186
187         f = open_map(0, &which);
188         if (!f) {
189                 RebuildMap();
190                 f = open_map(0, &which);
191         }
192         if (!f)
193                 return;
194
195         while (fgets(buf, sizeof(buf), f)) {
196                 path[0] = 0;
197                 if (sscanf(buf, " %3[mdp]%d %s %x:%x:%x:%x %200s",
198                            nam, &devnum, metadata, uuid, uuid+1,
199                            uuid+2, uuid+3, path) >= 7) {
200                         if (strncmp(nam, "md", 2) != 0)
201                                 continue;
202                         if (nam[2] == 'p')
203                                 devnum = -1 - devnum;
204                         map_add(melp, devnum, metadata, uuid, path);
205                 }
206         }
207         fclose(f);
208 }
209
210 void map_free(struct map_ent *map)
211 {
212         while (map) {
213                 struct map_ent *mp = map;
214                 map = mp->next;
215                 free(mp->path);
216                 free(mp);
217         }
218 }
219
220 int map_update(struct map_ent **mpp, int devnum, char *metadata,
221                int *uuid, char *path)
222 {
223         struct map_ent *map, *mp;
224         int rv;
225
226         if (mpp && *mpp)
227                 map = *mpp;
228         else
229                 map_read(&map);
230
231         for (mp = map ; mp ; mp=mp->next)
232                 if (mp->devnum == devnum) {
233                         strcpy(mp->metadata, metadata);
234                         memcpy(mp->uuid, uuid, 16);
235                         free(mp->path);
236                         mp->path = path ? strdup(path) : NULL;
237                         break;
238                 }
239         if (!mp)
240                 map_add(&map, devnum, metadata, uuid, path);
241         if (mpp)
242                 *mpp = NULL;
243         rv = map_write(map);
244         map_free(map);
245         return rv;
246 }
247
248 void map_delete(struct map_ent **mapp, int devnum)
249 {
250         struct map_ent *mp;
251
252         if (*mapp == NULL)
253                 map_read(mapp);
254
255         for (mp = *mapp; mp; mp = *mapp) {
256                 if (mp->devnum == devnum) {
257                         *mapp = mp->next;
258                         free(mp->path);
259                         free(mp);
260                 } else
261                         mapp = & mp->next;
262         }
263 }
264
265 void map_remove(struct map_ent **mapp, int devnum)
266 {
267         if (devnum == NoMdDev)
268                 return;
269
270         map_delete(mapp, devnum);
271         map_write(*mapp);
272         map_free(*mapp);
273 }
274
275 struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4])
276 {
277         struct map_ent *mp;
278         if (!*map)
279                 map_read(map);
280
281         for (mp = *map ; mp ; mp = mp->next) {
282                 if (memcmp(uuid, mp->uuid, 16) != 0)
283                         continue;
284                 if (!mddev_busy(mp->devnum)) {
285                         mp->bad = 1;
286                         continue;
287                 }
288                 return mp;
289         }
290         return NULL;
291 }
292
293 struct map_ent *map_by_devnum(struct map_ent **map, int devnum)
294 {
295         struct map_ent *mp;
296         if (!*map)
297                 map_read(map);
298
299         for (mp = *map ; mp ; mp = mp->next) {
300                 if (mp->devnum != devnum)
301                         continue;
302                 if (!mddev_busy(mp->devnum)) {
303                         mp->bad = 1;
304                         continue;
305                 }
306                 return mp;
307         }
308         return NULL;
309 }
310
311 struct map_ent *map_by_name(struct map_ent **map, char *name)
312 {
313         struct map_ent *mp;
314         if (!*map)
315                 map_read(map);
316
317         for (mp = *map ; mp ; mp = mp->next) {
318                 if (!mp->path)
319                         continue;
320                 if (strncmp(mp->path, "/dev/md/", 8) != 0)
321                         continue;
322                 if (strcmp(mp->path+8, name) != 0)
323                         continue;
324                 if (!mddev_busy(mp->devnum)) {
325                         mp->bad = 1;
326                         continue;
327                 }
328                 return mp;
329         }
330         return NULL;
331 }
332
333 /* sets the proper subarray and container_dev according to the metadata
334  * version super_by_fd does this automatically, this routine is meant as
335  * a supplement for guess_super()
336  */
337 static void set_member_info(struct supertype *st, struct mdstat_ent *ent)
338 {
339
340         st->subarray[0] = '\0';
341
342         if (ent->metadata_version == NULL ||
343             strncmp(ent->metadata_version, "external:", 9) != 0)
344                 return;
345
346         if (is_subarray(&ent->metadata_version[9])) {
347                 char version[strlen(ent->metadata_version)+1];
348                 char *subarray;
349                 char *name = &version[10];
350
351                 strcpy(version, ent->metadata_version);
352                 subarray = strrchr(version, '/');
353                 name = &version[10];
354
355                 if (!subarray)
356                         return;
357                 *subarray++ = '\0';
358
359                 st->container_dev = devname2devnum(name);
360                 strncpy(st->subarray, subarray, sizeof(st->subarray));
361         }
362 }
363
364 void RebuildMap(void)
365 {
366         struct mdstat_ent *mdstat = mdstat_read(0, 0);
367         struct mdstat_ent *md;
368         struct map_ent *map = NULL;
369         int mdp = get_mdp_major();
370         int require_homehost;
371         char sys_hostname[256];
372         char *homehost = conf_get_homehost(&require_homehost);
373
374         if (homehost == NULL || strcmp(homehost, "<system>")==0) {
375                 if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) {
376                         sys_hostname[sizeof(sys_hostname)-1] = 0;
377                         homehost = sys_hostname;
378                 }
379         }
380
381         for (md = mdstat ; md ; md = md->next) {
382                 struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS);
383                 struct mdinfo *sd;
384
385                 if (!sra)
386                         continue;
387
388                 for (sd = sra->devs ; sd ; sd = sd->next) {
389                         char namebuf[100];
390                         char dn[30];
391                         int dfd;
392                         int ok;
393                         struct supertype *st;
394                         char *path;
395                         struct mdinfo info;
396
397                         sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor);
398                         dfd = dev_open(dn, O_RDONLY);
399                         if (dfd < 0)
400                                 continue;
401                         st = guess_super(dfd);
402                         if ( st == NULL)
403                                 ok = -1;
404                         else {
405                                 set_member_info(st, md);
406                                 ok = st->ss->load_super(st, dfd, NULL);
407                         }
408                         close(dfd);
409                         if (ok != 0)
410                                 continue;
411                         st->ss->getinfo_super(st, &info);
412                         if (md->devnum >= 0)
413                                 path = map_dev(MD_MAJOR, md->devnum, 0);
414                         else
415                                 path = map_dev(mdp, (-1-md->devnum)<< 6, 0);
416                         if (path == NULL ||
417                             strncmp(path, "/dev/md/", 8) != 0) {
418                                 /* We would really like a name that provides
419                                  * an MD_DEVNAME for udev.
420                                  * The name needs to be unique both in /dev/md/
421                                  * and in this mapfile.
422                                  * It needs to match watch -I or -As would come
423                                  * up with.
424                                  * That means:
425                                  *   Check if array is in mdadm.conf 
426                                  *        - if so use that.
427                                  *   determine trustworthy from homehost etc
428                                  *   find a unique name based on metadata name.
429                                  *   
430                                  */
431                                 struct mddev_ident_s *match = conf_match(&info, st);
432                                 struct stat stb;
433                                 if (match && match->devname && match->devname[0] == '/') {
434                                         path = match->devname;
435                                         if (path[0] != '/') {
436                                                 strcpy(namebuf, "/dev/md/");
437                                                 strcat(namebuf, path);
438                                                 path = namebuf;
439                                         }
440                                 } else {
441                                         int unum = 0;
442                                         char *sep = "_";
443                                         const char *name;
444                                         int conflict = 1;
445                                         if ((homehost == NULL ||
446                                              st->ss->match_home(st, homehost) != 1) &&
447                                             st->ss->match_home(st, "any") != 1 &&
448                                             (require_homehost
449                                              || ! conf_name_is_free(info.name)))
450                                                 /* require a numeric suffix */
451                                                 unum = 0;
452                                         else
453                                                 /* allow name to be used as-is if no conflict */
454                                                 unum = -1;
455                                         name = info.name;
456                                         if (!*name) {
457                                                 name = st->ss->name;
458                                                 if (!isdigit(name[strlen(name)-1]) &&
459                                                     unum == -1) {
460                                                         unum = 0;
461                                                         sep = "";
462                                                 }
463                                         }
464                                         if (strchr(name, ':'))
465                                                 /* probably a uniquifying
466                                                  * hostname prefix.  Allow
467                                                  * without a suffix
468                                                  */
469                                                 unum = -1;
470
471                                         while (conflict) {
472                                                 if (unum >= 0)
473                                                         sprintf(namebuf, "/dev/md/%s%s%d",
474                                                                 name, sep, unum);
475                                                 else
476                                                         sprintf(namebuf, "/dev/md/%s",
477                                                                 name);
478                                                 unum++;
479                                                 if (lstat(namebuf, &stb) != 0 &&
480                                                     (map == NULL ||
481                                                      !map_by_name(&map, namebuf+8)))
482                                                         conflict = 0;
483                                         }
484                                         path = namebuf;
485                                 }
486                         }
487                         map_add(&map, md->devnum,
488                                 info.text_version,
489                                 info.uuid, path);
490                         st->ss->free_super(st);
491                         break;
492                 }
493                 sysfs_free(sra);
494         }
495         /* Only trigger a change if we wrote a new map file */
496         if (map_write(map))
497                 for (md = mdstat ; md ; md = md->next) {
498                         struct mdinfo *sra = sysfs_read(-1, md->devnum,
499                                                         GET_VERSION);
500                         if (sra)
501                                 sysfs_uevent(sra, "change");
502                         sysfs_free(sra);
503                 }
504         map_free(map);
505         free_mdstat(mdstat);
506 }