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