]>
Commit | Line | Data |
---|---|---|
8382f19b | 1 | /* |
df65ac70 | 2 | * mapfile - manage /var/run/mdadm/map. Part of: |
8382f19b NB |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. |
4 | * | |
cf3a3d78 | 5 | * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de> |
8382f19b NB |
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 | ||
df65ac70 | 31 | /* /var/run/mdadm/map is used to track arrays being created in --incremental |
435b90e7 | 32 | * mode. It particularly allows lookup from UUID to array device, but |
8382f19b NB |
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: | |
1522c538 NB |
36 | * Device id - mdX or mdpX where X is a number. |
37 | * metadata - 0.90 1.0 1.1 1.2 ddf ... | |
8382f19b NB |
38 | * UUID - uuid of the array |
39 | * path - path where device created: /dev/md/home | |
40 | * | |
df65ac70 | 41 | * The preferred location for the map file is /var/run/mdadm/map. |
cf3a3d78 N |
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: | |
df65ac70 | 45 | * /var/run/mdadm/map /dev/.mdadm/map(changable at compile time). |
cf3a3d78 N |
46 | * We read from the first one that exists and write to the first |
47 | * one that we can. | |
8382f19b | 48 | */ |
360b4636 | 49 | #include "mdadm.h" |
fc7e81e5 | 50 | #include <sys/file.h> |
360b4636 | 51 | #include <ctype.h> |
8382f19b | 52 | |
df65ac70 N |
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), | |
cf3a3d78 | 67 | }; |
8382f19b | 68 | |
753cf905 | 69 | int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT|O_TRUNC }; |
cf3a3d78 | 70 | char *mapsmode[3] = { "r", "w", "w"}; |
8382f19b | 71 | |
cf3a3d78 N |
72 | FILE *open_map(int modenum, int *choice) |
73 | { | |
74 | int i; | |
435b90e7 | 75 | |
df65ac70 | 76 | for (i = 0 ; i <= MAP_DIRS ; i++) { |
435b90e7 | 77 | int fd; |
df65ac70 | 78 | if ((mapmode[modenum] & O_CREAT)) |
435b90e7 DL |
79 | /* Attempt to create directory, don't worry about |
80 | * failure. | |
81 | */ | |
df65ac70 | 82 | (void)mkdir(mapname[i][MAP_DIRNAME], 0755); |
435b90e7 | 83 | fd = open(mapname[i][modenum], mapmode[modenum], 0600); |
cf3a3d78 N |
84 | if (fd >= 0) { |
85 | *choice = i; | |
86 | return fdopen(fd, mapsmode[modenum]); | |
87 | } | |
88 | } | |
89 | return NULL; | |
90 | } | |
8382f19b NB |
91 | |
92 | int map_write(struct map_ent *mel) | |
93 | { | |
94 | FILE *f; | |
95 | int err; | |
cf3a3d78 N |
96 | int which; |
97 | ||
df65ac70 | 98 | f = open_map(MAP_NEW, &which); |
8382f19b | 99 | |
8382f19b NB |
100 | if (!f) |
101 | return 0; | |
195254b8 N |
102 | for (; mel; mel = mel->next) { |
103 | if (mel->bad) | |
104 | continue; | |
8382f19b NB |
105 | if (mel->devnum < 0) |
106 | fprintf(f, "mdp%d ", -1-mel->devnum); | |
107 | else | |
108 | fprintf(f, "md%d ", mel->devnum); | |
1522c538 | 109 | fprintf(f, "%s ", mel->metadata); |
8382f19b NB |
110 | fprintf(f, "%08x:%08x:%08x:%08x ", mel->uuid[0], |
111 | mel->uuid[1], mel->uuid[2], mel->uuid[3]); | |
8615dcff | 112 | fprintf(f, "%s\n", mel->path?:""); |
8382f19b NB |
113 | } |
114 | fflush(f); | |
115 | err = ferror(f); | |
116 | fclose(f); | |
117 | if (err) { | |
cf3a3d78 | 118 | unlink(mapname[which][1]); |
8382f19b NB |
119 | return 0; |
120 | } | |
cf3a3d78 N |
121 | return rename(mapname[which][1], |
122 | mapname[which][0]) == 0; | |
8382f19b NB |
123 | } |
124 | ||
ad5bc697 | 125 | |
cf3a3d78 N |
126 | static FILE *lf = NULL; |
127 | static int lwhich = 0; | |
ad5bc697 N |
128 | int map_lock(struct map_ent **melp) |
129 | { | |
08415c46 N |
130 | while (lf == NULL) { |
131 | struct stat buf; | |
df65ac70 | 132 | lf = open_map(MAP_LOCK, &lwhich); |
cf3a3d78 | 133 | if (lf == NULL) |
ad5bc697 | 134 | return -1; |
fc7e81e5 | 135 | if (flock(fileno(lf), LOCK_EX) != 0) { |
cf3a3d78 N |
136 | fclose(lf); |
137 | lf = NULL; | |
ad5bc697 N |
138 | return -1; |
139 | } | |
08415c46 N |
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 | } | |
ad5bc697 N |
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 | { | |
fc7e81e5 | 158 | if (lf) { |
08415c46 N |
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]); | |
cf3a3d78 | 164 | fclose(lf); |
fc7e81e5 | 165 | } |
cf3a3d78 | 166 | lf = NULL; |
ad5bc697 N |
167 | } |
168 | ||
8382f19b | 169 | void map_add(struct map_ent **melp, |
1522c538 | 170 | int devnum, char *metadata, int uuid[4], char *path) |
8382f19b NB |
171 | { |
172 | struct map_ent *me = malloc(sizeof(*me)); | |
173 | ||
174 | me->devnum = devnum; | |
1522c538 | 175 | strcpy(me->metadata, metadata); |
8382f19b | 176 | memcpy(me->uuid, uuid, 16); |
8615dcff | 177 | me->path = path ? strdup(path) : NULL; |
8382f19b | 178 | me->next = *melp; |
195254b8 | 179 | me->bad = 0; |
8382f19b NB |
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]; | |
1522c538 NB |
188 | int devnum, uuid[4]; |
189 | char metadata[30]; | |
8382f19b | 190 | char nam[4]; |
cf3a3d78 | 191 | int which; |
8382f19b NB |
192 | |
193 | *melp = NULL; | |
194 | ||
df65ac70 | 195 | f = open_map(MAP_READ, &which); |
3a56f223 N |
196 | if (!f) { |
197 | RebuildMap(); | |
df65ac70 | 198 | f = open_map(MAP_READ, &which); |
3a56f223 | 199 | } |
8382f19b NB |
200 | if (!f) |
201 | return; | |
202 | ||
203 | while (fgets(buf, sizeof(buf), f)) { | |
8615dcff | 204 | path[0] = 0; |
c5afc314 | 205 | if (sscanf(buf, " %3[mdp]%d %s %x:%x:%x:%x %200s", |
1522c538 | 206 | nam, &devnum, metadata, uuid, uuid+1, |
8615dcff | 207 | uuid+2, uuid+3, path) >= 7) { |
c5afc314 N |
208 | if (strncmp(nam, "md", 2) != 0) |
209 | continue; | |
210 | if (nam[2] == 'p') | |
8382f19b | 211 | devnum = -1 - devnum; |
1522c538 | 212 | map_add(melp, devnum, metadata, uuid, path); |
8382f19b NB |
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 | ||
1522c538 | 228 | int map_update(struct map_ent **mpp, int devnum, char *metadata, |
8382f19b NB |
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) { | |
1522c538 | 241 | strcpy(mp->metadata, metadata); |
8382f19b NB |
242 | memcpy(mp->uuid, uuid, 16); |
243 | free(mp->path); | |
8615dcff | 244 | mp->path = path ? strdup(path) : NULL; |
8382f19b NB |
245 | break; |
246 | } | |
247 | if (!mp) | |
1522c538 | 248 | map_add(&map, devnum, metadata, uuid, path); |
a04d5763 N |
249 | if (mpp) |
250 | *mpp = NULL; | |
8382f19b NB |
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 | ||
4eb26970 DW |
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 | ||
8382f19b NB |
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 | ||
195254b8 N |
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 | } | |
8382f19b | 298 | return NULL; |
4ccad7b1 | 299 | } |
8382f19b | 300 | |
4ccad7b1 N |
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 | ||
195254b8 N |
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 | } | |
4ccad7b1 | 316 | return NULL; |
8382f19b | 317 | } |
f2e55ecc N |
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) { | |
8615dcff N |
326 | if (!mp->path) |
327 | continue; | |
f2e55ecc N |
328 | if (strncmp(mp->path, "/dev/md/", 8) != 0) |
329 | continue; | |
195254b8 N |
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; | |
f2e55ecc N |
337 | } |
338 | return NULL; | |
339 | } | |
3a56f223 | 340 | |
f98d41dd DW |
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 | { | |
f98d41dd DW |
347 | |
348 | st->subarray[0] = '\0'; | |
349 | ||
2b9aa337 N |
350 | if (ent->metadata_version == NULL || |
351 | strncmp(ent->metadata_version, "external:", 9) != 0) | |
f98d41dd DW |
352 | return; |
353 | ||
2b9aa337 N |
354 | if (is_subarray(&ent->metadata_version[9])) { |
355 | char version[strlen(ent->metadata_version)+1]; | |
356 | char *subarray; | |
f98d41dd DW |
357 | char *name = &version[10]; |
358 | ||
2b9aa337 N |
359 | strcpy(version, ent->metadata_version); |
360 | subarray = strrchr(version, '/'); | |
361 | name = &version[10]; | |
362 | ||
f98d41dd DW |
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 | ||
3a56f223 N |
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(); | |
360b4636 N |
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 | } | |
3a56f223 N |
388 | |
389 | for (md = mdstat ; md ; md = md->next) { | |
b526e52d | 390 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS); |
3a56f223 N |
391 | struct mdinfo *sd; |
392 | ||
506ffd1e DW |
393 | if (!sra) |
394 | continue; | |
395 | ||
3a56f223 | 396 | for (sd = sra->devs ; sd ; sd = sd->next) { |
360b4636 | 397 | char namebuf[100]; |
3a56f223 N |
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; | |
f98d41dd DW |
412 | else { |
413 | set_member_info(st, md); | |
3a56f223 | 414 | ok = st->ss->load_super(st, dfd, NULL); |
f98d41dd | 415 | } |
3a56f223 N |
416 | close(dfd); |
417 | if (ok != 0) | |
418 | continue; | |
419 | st->ss->getinfo_super(st, &info); | |
60f8cb9b | 420 | if (md->devnum >= 0) |
3a56f223 N |
421 | path = map_dev(MD_MAJOR, md->devnum, 0); |
422 | else | |
423 | path = map_dev(mdp, (-1-md->devnum)<< 6, 0); | |
360b4636 N |
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 | } | |
8a659c33 N |
495 | map_add(&map, md->devnum, |
496 | info.text_version, | |
8615dcff | 497 | info.uuid, path); |
3a56f223 N |
498 | st->ss->free_super(st); |
499 | break; | |
500 | } | |
8a659c33 | 501 | sysfs_free(sra); |
3a56f223 | 502 | } |
7bf59f5c DL |
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); | |
b526e52d DW |
508 | if (sra) |
509 | sysfs_uevent(sra, "change"); | |
7bf59f5c DL |
510 | sysfs_free(sra); |
511 | } | |
3a56f223 | 512 | map_free(map); |
78fbcc10 | 513 | free_mdstat(mdstat); |
3a56f223 | 514 | } |