]>
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 | } |
898bd1ec GJ |
146 | if (*melp) |
147 | map_free(*melp); | |
cf3a3d78 | 148 | lf = NULL; |
ad5bc697 N |
149 | } |
150 | ||
cc700db3 LD |
151 | void map_fork(void) |
152 | { | |
153 | /* We are forking, so must close the lock file. | |
154 | * Don't risk flushing anything though. | |
155 | */ | |
156 | if (lf) { | |
157 | close(fileno(lf)); | |
158 | fclose(lf); | |
159 | lf = NULL; | |
160 | } | |
161 | } | |
162 | ||
8382f19b | 163 | void map_add(struct map_ent **melp, |
4dd2df09 | 164 | char * devnm, char *metadata, int uuid[4], char *path) |
8382f19b | 165 | { |
503975b9 | 166 | struct map_ent *me = xmalloc(sizeof(*me)); |
8382f19b | 167 | |
4dd2df09 | 168 | strcpy(me->devnm, devnm); |
1522c538 | 169 | strcpy(me->metadata, metadata); |
8382f19b | 170 | memcpy(me->uuid, uuid, 16); |
503975b9 | 171 | me->path = path ? xstrdup(path) : NULL; |
8382f19b | 172 | me->next = *melp; |
195254b8 | 173 | me->bad = 0; |
8382f19b NB |
174 | *melp = me; |
175 | } | |
176 | ||
177 | void map_read(struct map_ent **melp) | |
178 | { | |
179 | FILE *f; | |
180 | char buf[8192]; | |
1158f25e | 181 | char path[201]; |
4dd2df09 N |
182 | int uuid[4]; |
183 | char devnm[32]; | |
1522c538 | 184 | char metadata[30]; |
8382f19b NB |
185 | |
186 | *melp = NULL; | |
187 | ||
9e751dc7 | 188 | f = open_map(MAP_READ); |
3a56f223 N |
189 | if (!f) { |
190 | RebuildMap(); | |
9e751dc7 | 191 | f = open_map(MAP_READ); |
3a56f223 | 192 | } |
8382f19b NB |
193 | if (!f) |
194 | return; | |
195 | ||
196 | while (fgets(buf, sizeof(buf), f)) { | |
8615dcff | 197 | path[0] = 0; |
4dd2df09 N |
198 | if (sscanf(buf, " %s %s %x:%x:%x:%x %200s", |
199 | devnm, metadata, uuid, uuid+1, | |
8615dcff | 200 | uuid+2, uuid+3, path) >= 7) { |
4dd2df09 | 201 | map_add(melp, devnm, metadata, uuid, path); |
8382f19b NB |
202 | } |
203 | } | |
204 | fclose(f); | |
205 | } | |
206 | ||
207 | void map_free(struct map_ent *map) | |
208 | { | |
209 | while (map) { | |
210 | struct map_ent *mp = map; | |
211 | map = mp->next; | |
212 | free(mp->path); | |
213 | free(mp); | |
214 | } | |
215 | } | |
216 | ||
4dd2df09 | 217 | int map_update(struct map_ent **mpp, char *devnm, char *metadata, |
8382f19b NB |
218 | int *uuid, char *path) |
219 | { | |
220 | struct map_ent *map, *mp; | |
221 | int rv; | |
222 | ||
223 | if (mpp && *mpp) | |
224 | map = *mpp; | |
225 | else | |
226 | map_read(&map); | |
227 | ||
228 | for (mp = map ; mp ; mp=mp->next) | |
4dd2df09 | 229 | if (strcmp(mp->devnm, devnm) == 0) { |
1522c538 | 230 | strcpy(mp->metadata, metadata); |
8382f19b NB |
231 | memcpy(mp->uuid, uuid, 16); |
232 | free(mp->path); | |
503975b9 | 233 | mp->path = path ? xstrdup(path) : NULL; |
52f07f57 | 234 | mp->bad = 0; |
8382f19b NB |
235 | break; |
236 | } | |
237 | if (!mp) | |
4dd2df09 | 238 | map_add(&map, devnm, metadata, uuid, path); |
a04d5763 N |
239 | if (mpp) |
240 | *mpp = NULL; | |
8382f19b NB |
241 | rv = map_write(map); |
242 | map_free(map); | |
243 | return rv; | |
244 | } | |
245 | ||
4dd2df09 | 246 | void map_delete(struct map_ent **mapp, char *devnm) |
8382f19b NB |
247 | { |
248 | struct map_ent *mp; | |
249 | ||
250 | if (*mapp == NULL) | |
251 | map_read(mapp); | |
252 | ||
253 | for (mp = *mapp; mp; mp = *mapp) { | |
4dd2df09 | 254 | if (strcmp(mp->devnm, devnm) == 0) { |
8382f19b NB |
255 | *mapp = mp->next; |
256 | free(mp->path); | |
257 | free(mp); | |
258 | } else | |
259 | mapp = & mp->next; | |
260 | } | |
261 | } | |
262 | ||
4dd2df09 | 263 | void map_remove(struct map_ent **mapp, char *devnm) |
4eb26970 | 264 | { |
4dd2df09 | 265 | if (devnm[0] == 0) |
4eb26970 DW |
266 | return; |
267 | ||
4dd2df09 | 268 | map_delete(mapp, devnm); |
4eb26970 DW |
269 | map_write(*mapp); |
270 | map_free(*mapp); | |
531d7991 | 271 | *mapp = NULL; |
4eb26970 DW |
272 | } |
273 | ||
8382f19b NB |
274 | struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4]) |
275 | { | |
276 | struct map_ent *mp; | |
277 | if (!*map) | |
278 | map_read(map); | |
279 | ||
195254b8 N |
280 | for (mp = *map ; mp ; mp = mp->next) { |
281 | if (memcmp(uuid, mp->uuid, 16) != 0) | |
282 | continue; | |
4dd2df09 | 283 | if (!mddev_busy(mp->devnm)) { |
195254b8 N |
284 | mp->bad = 1; |
285 | continue; | |
286 | } | |
287 | return mp; | |
288 | } | |
8382f19b | 289 | return NULL; |
4ccad7b1 | 290 | } |
8382f19b | 291 | |
4dd2df09 | 292 | struct map_ent *map_by_devnm(struct map_ent **map, char *devnm) |
4ccad7b1 N |
293 | { |
294 | struct map_ent *mp; | |
295 | if (!*map) | |
296 | map_read(map); | |
297 | ||
195254b8 | 298 | for (mp = *map ; mp ; mp = mp->next) { |
4dd2df09 | 299 | if (strcmp(mp->devnm, devnm) != 0) |
195254b8 | 300 | continue; |
4dd2df09 | 301 | if (!mddev_busy(mp->devnm)) { |
195254b8 N |
302 | mp->bad = 1; |
303 | continue; | |
304 | } | |
305 | return mp; | |
306 | } | |
4ccad7b1 | 307 | return NULL; |
8382f19b | 308 | } |
f2e55ecc N |
309 | |
310 | struct map_ent *map_by_name(struct map_ent **map, char *name) | |
311 | { | |
312 | struct map_ent *mp; | |
313 | if (!*map) | |
314 | map_read(map); | |
315 | ||
316 | for (mp = *map ; mp ; mp = mp->next) { | |
8615dcff N |
317 | if (!mp->path) |
318 | continue; | |
f2e55ecc N |
319 | if (strncmp(mp->path, "/dev/md/", 8) != 0) |
320 | continue; | |
195254b8 N |
321 | if (strcmp(mp->path+8, name) != 0) |
322 | continue; | |
4dd2df09 | 323 | if (!mddev_busy(mp->devnm)) { |
195254b8 N |
324 | mp->bad = 1; |
325 | continue; | |
326 | } | |
327 | return mp; | |
f2e55ecc N |
328 | } |
329 | return NULL; | |
330 | } | |
3a56f223 | 331 | |
f98d41dd DW |
332 | /* sets the proper subarray and container_dev according to the metadata |
333 | * version super_by_fd does this automatically, this routine is meant as | |
334 | * a supplement for guess_super() | |
335 | */ | |
ca145a1e | 336 | static char *get_member_info(struct mdstat_ent *ent) |
f98d41dd | 337 | { |
f98d41dd | 338 | |
2b9aa337 N |
339 | if (ent->metadata_version == NULL || |
340 | strncmp(ent->metadata_version, "external:", 9) != 0) | |
ca145a1e | 341 | return NULL; |
f98d41dd | 342 | |
2b9aa337 | 343 | if (is_subarray(&ent->metadata_version[9])) { |
2b9aa337 | 344 | char *subarray; |
2b9aa337 | 345 | |
ca145a1e N |
346 | subarray = strrchr(ent->metadata_version, '/'); |
347 | return subarray + 1; | |
f98d41dd | 348 | } |
ca145a1e | 349 | return NULL; |
f98d41dd DW |
350 | } |
351 | ||
3a56f223 N |
352 | void RebuildMap(void) |
353 | { | |
354 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
355 | struct mdstat_ent *md; | |
356 | struct map_ent *map = NULL; | |
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) { | |
4dd2df09 | 369 | struct mdinfo *sra = sysfs_read(-1, md->devnm, 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; | |
13db17bd | 380 | dev_t devid; |
3a56f223 | 381 | struct supertype *st; |
71204a50 | 382 | char *subarray = NULL; |
3a56f223 | 383 | char *path; |
a1f976a0 | 384 | struct mdinfo *info; |
3a56f223 N |
385 | |
386 | sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor); | |
387 | dfd = dev_open(dn, O_RDONLY); | |
388 | if (dfd < 0) | |
389 | continue; | |
390 | st = guess_super(dfd); | |
391 | if ( st == NULL) | |
392 | ok = -1; | |
f98d41dd | 393 | else { |
a1f976a0 | 394 | subarray = get_member_info(md); |
3a56f223 | 395 | ok = st->ss->load_super(st, dfd, NULL); |
f98d41dd | 396 | } |
3a56f223 N |
397 | close(dfd); |
398 | if (ok != 0) | |
399 | continue; | |
a74e5731 N |
400 | if (subarray) |
401 | info = st->ss->container_content(st, subarray); | |
402 | else { | |
403 | info = xmalloc(sizeof(*info)); | |
404 | st->ss->getinfo_super(st, info, NULL); | |
405 | } | |
ccced50f N |
406 | if (!info) |
407 | continue; | |
a1f976a0 | 408 | |
4dd2df09 N |
409 | devid = devnm2devid(md->devnm); |
410 | path = map_dev(major(devid), minor(devid), 0); | |
360b4636 N |
411 | if (path == NULL || |
412 | strncmp(path, "/dev/md/", 8) != 0) { | |
413 | /* We would really like a name that provides | |
414 | * an MD_DEVNAME for udev. | |
415 | * The name needs to be unique both in /dev/md/ | |
416 | * and in this mapfile. | |
7103b9b8 | 417 | * It needs to match what -I or -As would come |
360b4636 N |
418 | * up with. |
419 | * That means: | |
1011e834 | 420 | * Check if array is in mdadm.conf |
360b4636 N |
421 | * - if so use that. |
422 | * determine trustworthy from homehost etc | |
423 | * find a unique name based on metadata name. | |
1011e834 | 424 | * |
360b4636 | 425 | */ |
2244d1a9 N |
426 | struct mddev_ident *match = conf_match(st, info, |
427 | NULL, 0, | |
428 | NULL); | |
360b4636 N |
429 | struct stat stb; |
430 | if (match && match->devname && match->devname[0] == '/') { | |
431 | path = match->devname; | |
432 | if (path[0] != '/') { | |
433 | strcpy(namebuf, "/dev/md/"); | |
434 | strcat(namebuf, path); | |
435 | path = namebuf; | |
436 | } | |
437 | } else { | |
438 | int unum = 0; | |
439 | char *sep = "_"; | |
440 | const char *name; | |
441 | int conflict = 1; | |
442 | if ((homehost == NULL || | |
443 | st->ss->match_home(st, homehost) != 1) && | |
444 | st->ss->match_home(st, "any") != 1 && | |
d7be7d87 JS |
445 | (require_homehost || |
446 | !conf_name_is_free(info->name))) | |
360b4636 N |
447 | /* require a numeric suffix */ |
448 | unum = 0; | |
449 | else | |
450 | /* allow name to be used as-is if no conflict */ | |
451 | unum = -1; | |
a1f976a0 | 452 | name = info->name; |
360b4636 N |
453 | if (!*name) { |
454 | name = st->ss->name; | |
455 | if (!isdigit(name[strlen(name)-1]) && | |
456 | unum == -1) { | |
457 | unum = 0; | |
458 | sep = ""; | |
459 | } | |
460 | } | |
628cdf19 N |
461 | if (strchr(name, ':')) { |
462 | /* Probably a uniquifying | |
360b4636 | 463 | * hostname prefix. Allow |
628cdf19 N |
464 | * without a suffix, and strip |
465 | * hostname if it is us. | |
360b4636 | 466 | */ |
628cdf19 N |
467 | if (homehost && unum == -1 && |
468 | strncmp(name, homehost, | |
469 | strlen(homehost)) == 0 && | |
470 | name[strlen(homehost)] == ':') | |
471 | name += strlen(homehost)+1; | |
360b4636 | 472 | unum = -1; |
628cdf19 | 473 | } |
360b4636 N |
474 | |
475 | while (conflict) { | |
476 | if (unum >= 0) | |
477 | sprintf(namebuf, "/dev/md/%s%s%d", | |
478 | name, sep, unum); | |
479 | else | |
480 | sprintf(namebuf, "/dev/md/%s", | |
481 | name); | |
482 | unum++; | |
483 | if (lstat(namebuf, &stb) != 0 && | |
484 | (map == NULL || | |
485 | !map_by_name(&map, namebuf+8))) | |
486 | conflict = 0; | |
487 | } | |
488 | path = namebuf; | |
489 | } | |
490 | } | |
4dd2df09 | 491 | map_add(&map, md->devnm, |
a1f976a0 N |
492 | info->text_version, |
493 | info->uuid, path); | |
3a56f223 | 494 | st->ss->free_super(st); |
a1f976a0 | 495 | free(info); |
3a56f223 N |
496 | break; |
497 | } | |
8a659c33 | 498 | sysfs_free(sra); |
3a56f223 | 499 | } |
7bf59f5c DL |
500 | /* Only trigger a change if we wrote a new map file */ |
501 | if (map_write(map)) | |
502 | for (md = mdstat ; md ; md = md->next) { | |
4dd2df09 | 503 | struct mdinfo *sra = sysfs_read(-1, md->devnm, |
7bf59f5c | 504 | GET_VERSION); |
b526e52d DW |
505 | if (sra) |
506 | sysfs_uevent(sra, "change"); | |
7bf59f5c DL |
507 | sysfs_free(sra); |
508 | } | |
3a56f223 | 509 | map_free(map); |
78fbcc10 | 510 | free_mdstat(mdstat); |
3a56f223 | 511 | } |