]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mapfile - keep track of uuid <-> array mapping. Part of: | |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
4 | * | |
5 | * Copyright (C) 2006-2010 Neil Brown <neilb@suse.de> | |
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 | /* The mapfile is used to track arrays being created in --incremental | |
32 | * mode. It particularly allows lookup from UUID to array device, but | |
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: | |
36 | * Device id - mdX or mdpX where X is a number. | |
37 | * metadata - 0.90 1.0 1.1 1.2 ddf ... | |
38 | * UUID - uuid of the array | |
39 | * path - path where device created: /dev/md/home | |
40 | * | |
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. | |
44 | */ | |
45 | #include "mdadm.h" | |
46 | #include <sys/file.h> | |
47 | #include <ctype.h> | |
48 | ||
49 | ||
50 | #define MAP_READ 0 | |
51 | #define MAP_NEW 1 | |
52 | #define MAP_LOCK 2 | |
53 | #define MAP_DIRNAME 3 | |
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 | |
60 | }; | |
61 | ||
62 | int mapmode[3] = { O_RDONLY, O_RDWR|O_CREAT, O_RDWR|O_CREAT|O_TRUNC }; | |
63 | char *mapsmode[3] = { "r", "w", "w"}; | |
64 | ||
65 | FILE *open_map(int modenum) | |
66 | { | |
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]); | |
76 | return NULL; | |
77 | } | |
78 | ||
79 | int map_write(struct map_ent *mel) | |
80 | { | |
81 | FILE *f; | |
82 | int err; | |
83 | ||
84 | f = open_map(MAP_NEW); | |
85 | ||
86 | if (!f) | |
87 | return 0; | |
88 | for (; mel; mel = mel->next) { | |
89 | if (mel->bad) | |
90 | continue; | |
91 | fprintf(f, "%s ", mel->devnm); | |
92 | fprintf(f, "%s ", mel->metadata); | |
93 | fprintf(f, "%08x:%08x:%08x:%08x ", mel->uuid[0], | |
94 | mel->uuid[1], mel->uuid[2], mel->uuid[3]); | |
95 | fprintf(f, "%s\n", mel->path?:""); | |
96 | } | |
97 | fflush(f); | |
98 | err = ferror(f); | |
99 | fclose(f); | |
100 | if (err) { | |
101 | unlink(mapname[1]); | |
102 | return 0; | |
103 | } | |
104 | return rename(mapname[1], | |
105 | mapname[0]) == 0; | |
106 | } | |
107 | ||
108 | ||
109 | static FILE *lf = NULL; | |
110 | int map_lock(struct map_ent **melp) | |
111 | { | |
112 | while (lf == NULL) { | |
113 | struct stat buf; | |
114 | lf = open_map(MAP_LOCK); | |
115 | if (lf == NULL) | |
116 | return -1; | |
117 | if (flock(fileno(lf), LOCK_EX) != 0) { | |
118 | fclose(lf); | |
119 | lf = NULL; | |
120 | return -1; | |
121 | } | |
122 | if (fstat(fileno(lf), &buf) != 0 || | |
123 | buf.st_nlink == 0) { | |
124 | /* The owner of the lock unlinked it, | |
125 | * so we have a lock on a stale file, | |
126 | * try again | |
127 | */ | |
128 | fclose(lf); | |
129 | lf = NULL; | |
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 | { | |
140 | if (lf) { | |
141 | /* must unlink before closing the file, | |
142 | * as only the owner of the lock may | |
143 | * unlink the file | |
144 | */ | |
145 | unlink(mapname[2]); | |
146 | fclose(lf); | |
147 | } | |
148 | lf = NULL; | |
149 | } | |
150 | ||
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 | ||
163 | void map_add(struct map_ent **melp, | |
164 | char * devnm, char *metadata, int uuid[4], char *path) | |
165 | { | |
166 | struct map_ent *me = xmalloc(sizeof(*me)); | |
167 | ||
168 | strcpy(me->devnm, devnm); | |
169 | strcpy(me->metadata, metadata); | |
170 | memcpy(me->uuid, uuid, 16); | |
171 | me->path = path ? xstrdup(path) : NULL; | |
172 | me->next = *melp; | |
173 | me->bad = 0; | |
174 | *melp = me; | |
175 | } | |
176 | ||
177 | void map_read(struct map_ent **melp) | |
178 | { | |
179 | FILE *f; | |
180 | char buf[8192]; | |
181 | char path[200]; | |
182 | int uuid[4]; | |
183 | char devnm[32]; | |
184 | char metadata[30]; | |
185 | ||
186 | *melp = NULL; | |
187 | ||
188 | f = open_map(MAP_READ); | |
189 | if (!f) { | |
190 | RebuildMap(); | |
191 | f = open_map(MAP_READ); | |
192 | } | |
193 | if (!f) | |
194 | return; | |
195 | ||
196 | while (fgets(buf, sizeof(buf), f)) { | |
197 | path[0] = 0; | |
198 | if (sscanf(buf, " %s %s %x:%x:%x:%x %200s", | |
199 | devnm, metadata, uuid, uuid+1, | |
200 | uuid+2, uuid+3, path) >= 7) { | |
201 | map_add(melp, devnm, metadata, uuid, path); | |
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 | ||
217 | int map_update(struct map_ent **mpp, char *devnm, char *metadata, | |
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) | |
229 | if (strcmp(mp->devnm, devnm) == 0) { | |
230 | strcpy(mp->metadata, metadata); | |
231 | memcpy(mp->uuid, uuid, 16); | |
232 | free(mp->path); | |
233 | mp->path = path ? xstrdup(path) : NULL; | |
234 | mp->bad = 0; | |
235 | break; | |
236 | } | |
237 | if (!mp) | |
238 | map_add(&map, devnm, metadata, uuid, path); | |
239 | if (mpp) | |
240 | *mpp = NULL; | |
241 | rv = map_write(map); | |
242 | map_free(map); | |
243 | return rv; | |
244 | } | |
245 | ||
246 | void map_delete(struct map_ent **mapp, char *devnm) | |
247 | { | |
248 | struct map_ent *mp; | |
249 | ||
250 | if (*mapp == NULL) | |
251 | map_read(mapp); | |
252 | ||
253 | for (mp = *mapp; mp; mp = *mapp) { | |
254 | if (strcmp(mp->devnm, devnm) == 0) { | |
255 | *mapp = mp->next; | |
256 | free(mp->path); | |
257 | free(mp); | |
258 | } else | |
259 | mapp = & mp->next; | |
260 | } | |
261 | } | |
262 | ||
263 | void map_remove(struct map_ent **mapp, char *devnm) | |
264 | { | |
265 | if (devnm[0] == 0) | |
266 | return; | |
267 | ||
268 | map_delete(mapp, devnm); | |
269 | map_write(*mapp); | |
270 | map_free(*mapp); | |
271 | } | |
272 | ||
273 | struct map_ent *map_by_uuid(struct map_ent **map, int uuid[4]) | |
274 | { | |
275 | struct map_ent *mp; | |
276 | if (!*map) | |
277 | map_read(map); | |
278 | ||
279 | for (mp = *map ; mp ; mp = mp->next) { | |
280 | if (memcmp(uuid, mp->uuid, 16) != 0) | |
281 | continue; | |
282 | if (!mddev_busy(mp->devnm)) { | |
283 | mp->bad = 1; | |
284 | continue; | |
285 | } | |
286 | return mp; | |
287 | } | |
288 | return NULL; | |
289 | } | |
290 | ||
291 | struct map_ent *map_by_devnm(struct map_ent **map, char *devnm) | |
292 | { | |
293 | struct map_ent *mp; | |
294 | if (!*map) | |
295 | map_read(map); | |
296 | ||
297 | for (mp = *map ; mp ; mp = mp->next) { | |
298 | if (strcmp(mp->devnm, devnm) != 0) | |
299 | continue; | |
300 | if (!mddev_busy(mp->devnm)) { | |
301 | mp->bad = 1; | |
302 | continue; | |
303 | } | |
304 | return mp; | |
305 | } | |
306 | return NULL; | |
307 | } | |
308 | ||
309 | struct map_ent *map_by_name(struct map_ent **map, char *name) | |
310 | { | |
311 | struct map_ent *mp; | |
312 | if (!*map) | |
313 | map_read(map); | |
314 | ||
315 | for (mp = *map ; mp ; mp = mp->next) { | |
316 | if (!mp->path) | |
317 | continue; | |
318 | if (strncmp(mp->path, "/dev/md/", 8) != 0) | |
319 | continue; | |
320 | if (strcmp(mp->path+8, name) != 0) | |
321 | continue; | |
322 | if (!mddev_busy(mp->devnm)) { | |
323 | mp->bad = 1; | |
324 | continue; | |
325 | } | |
326 | return mp; | |
327 | } | |
328 | return NULL; | |
329 | } | |
330 | ||
331 | /* sets the proper subarray and container_dev according to the metadata | |
332 | * version super_by_fd does this automatically, this routine is meant as | |
333 | * a supplement for guess_super() | |
334 | */ | |
335 | static char *get_member_info(struct mdstat_ent *ent) | |
336 | { | |
337 | ||
338 | if (ent->metadata_version == NULL || | |
339 | strncmp(ent->metadata_version, "external:", 9) != 0) | |
340 | return NULL; | |
341 | ||
342 | if (is_subarray(&ent->metadata_version[9])) { | |
343 | char *subarray; | |
344 | ||
345 | subarray = strrchr(ent->metadata_version, '/'); | |
346 | return subarray + 1; | |
347 | } | |
348 | return NULL; | |
349 | } | |
350 | ||
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 require_homehost; | |
357 | char sys_hostname[256]; | |
358 | char *homehost = conf_get_homehost(&require_homehost); | |
359 | ||
360 | if (homehost == NULL || strcmp(homehost, "<system>")==0) { | |
361 | if (gethostname(sys_hostname, sizeof(sys_hostname)) == 0) { | |
362 | sys_hostname[sizeof(sys_hostname)-1] = 0; | |
363 | homehost = sys_hostname; | |
364 | } | |
365 | } | |
366 | ||
367 | for (md = mdstat ; md ; md = md->next) { | |
368 | struct mdinfo *sra = sysfs_read(-1, md->devnm, GET_DEVS); | |
369 | struct mdinfo *sd; | |
370 | ||
371 | if (!sra) | |
372 | continue; | |
373 | ||
374 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
375 | char namebuf[100]; | |
376 | char dn[30]; | |
377 | int dfd; | |
378 | int ok; | |
379 | int devid; | |
380 | struct supertype *st; | |
381 | char *subarray = NULL; | |
382 | char *path; | |
383 | struct mdinfo *info; | |
384 | ||
385 | sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor); | |
386 | dfd = dev_open(dn, O_RDONLY); | |
387 | if (dfd < 0) | |
388 | continue; | |
389 | st = guess_super(dfd); | |
390 | if ( st == NULL) | |
391 | ok = -1; | |
392 | else { | |
393 | subarray = get_member_info(md); | |
394 | ok = st->ss->load_super(st, dfd, NULL); | |
395 | } | |
396 | close(dfd); | |
397 | if (ok != 0) | |
398 | continue; | |
399 | if (subarray) | |
400 | info = st->ss->container_content(st, subarray); | |
401 | else { | |
402 | info = xmalloc(sizeof(*info)); | |
403 | st->ss->getinfo_super(st, info, NULL); | |
404 | } | |
405 | if (!info) | |
406 | continue; | |
407 | ||
408 | devid = devnm2devid(md->devnm); | |
409 | path = map_dev(major(devid), minor(devid), 0); | |
410 | if (path == NULL || | |
411 | strncmp(path, "/dev/md/", 8) != 0) { | |
412 | /* We would really like a name that provides | |
413 | * an MD_DEVNAME for udev. | |
414 | * The name needs to be unique both in /dev/md/ | |
415 | * and in this mapfile. | |
416 | * It needs to match what -I or -As would come | |
417 | * up with. | |
418 | * That means: | |
419 | * Check if array is in mdadm.conf | |
420 | * - if so use that. | |
421 | * determine trustworthy from homehost etc | |
422 | * find a unique name based on metadata name. | |
423 | * | |
424 | */ | |
425 | struct mddev_ident *match = conf_match(st, info, | |
426 | NULL, 0, | |
427 | NULL); | |
428 | struct stat stb; | |
429 | if (match && match->devname && match->devname[0] == '/') { | |
430 | path = match->devname; | |
431 | if (path[0] != '/') { | |
432 | strcpy(namebuf, "/dev/md/"); | |
433 | strcat(namebuf, path); | |
434 | path = namebuf; | |
435 | } | |
436 | } else { | |
437 | int unum = 0; | |
438 | char *sep = "_"; | |
439 | const char *name; | |
440 | int conflict = 1; | |
441 | if ((homehost == NULL || | |
442 | st->ss->match_home(st, homehost) != 1) && | |
443 | st->ss->match_home(st, "any") != 1 && | |
444 | (require_homehost | |
445 | || ! conf_name_is_free(info->name))) | |
446 | /* require a numeric suffix */ | |
447 | unum = 0; | |
448 | else | |
449 | /* allow name to be used as-is if no conflict */ | |
450 | unum = -1; | |
451 | name = info->name; | |
452 | if (!*name) { | |
453 | name = st->ss->name; | |
454 | if (!isdigit(name[strlen(name)-1]) && | |
455 | unum == -1) { | |
456 | unum = 0; | |
457 | sep = ""; | |
458 | } | |
459 | } | |
460 | if (strchr(name, ':')) | |
461 | /* probably a uniquifying | |
462 | * hostname prefix. Allow | |
463 | * without a suffix | |
464 | */ | |
465 | unum = -1; | |
466 | ||
467 | while (conflict) { | |
468 | if (unum >= 0) | |
469 | sprintf(namebuf, "/dev/md/%s%s%d", | |
470 | name, sep, unum); | |
471 | else | |
472 | sprintf(namebuf, "/dev/md/%s", | |
473 | name); | |
474 | unum++; | |
475 | if (lstat(namebuf, &stb) != 0 && | |
476 | (map == NULL || | |
477 | !map_by_name(&map, namebuf+8))) | |
478 | conflict = 0; | |
479 | } | |
480 | path = namebuf; | |
481 | } | |
482 | } | |
483 | map_add(&map, md->devnm, | |
484 | info->text_version, | |
485 | info->uuid, path); | |
486 | st->ss->free_super(st); | |
487 | free(info); | |
488 | break; | |
489 | } | |
490 | sysfs_free(sra); | |
491 | } | |
492 | /* Only trigger a change if we wrote a new map file */ | |
493 | if (map_write(map)) | |
494 | for (md = mdstat ; md ; md = md->next) { | |
495 | struct mdinfo *sra = sysfs_read(-1, md->devnm, | |
496 | GET_VERSION); | |
497 | if (sra) | |
498 | sysfs_uevent(sra, "change"); | |
499 | sysfs_free(sra); | |
500 | } | |
501 | map_free(map); | |
502 | free_mdstat(mdstat); | |
503 | } |