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