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