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