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