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