]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Incremental.c - support --incremental. Part of: | |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
4 | * | |
5 | * Copyright (C) 2006 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 | #include "mdadm.h" | |
32 | ||
33 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
34 | struct mdinfo *info); | |
35 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, | |
36 | int number, __u64 events, int verbose, | |
37 | char *array_name); | |
38 | ||
39 | int Incremental(char *devname, int verbose, int runstop, | |
40 | struct supertype *st, char *homehost, int autof) | |
41 | { | |
42 | /* Add this device to an array, creating the array if necessary | |
43 | * and starting the array if sensible or - if runstop>0 - if possible. | |
44 | * | |
45 | * This has several steps: | |
46 | * | |
47 | * 1/ Check if device is permitted by mdadm.conf, reject if not. | |
48 | * 2/ Find metadata, reject if none appropriate (check | |
49 | * version/name from args) | |
50 | * 3/ Check if there is a match in mdadm.conf | |
51 | * 3a/ if not, check for homehost match. If no match, assemble as | |
52 | * a 'foreign' array. | |
53 | * 4/ Determine device number. | |
54 | * - If in mdadm.conf with std name, use that | |
55 | * - UUID in /var/run/mdadm.map use that | |
56 | * - If name is suggestive, use that. unless in use with different uuid. | |
57 | * - Choose a free, high number. | |
58 | * - Use a partitioned device unless strong suggestion not to. | |
59 | * e.g. auto=md | |
60 | * Don't choose partitioned for containers. | |
61 | * 5/ Find out if array already exists | |
62 | * 5a/ if it does not | |
63 | * - choose a name, from mdadm.conf or 'name' field in array. | |
64 | * - create the array | |
65 | * - add the device | |
66 | * 5b/ if it does | |
67 | * - check one drive in array to make sure metadata is a reasonably | |
68 | * close match. Reject if not (e.g. different type) | |
69 | * - add the device | |
70 | * 6/ Make sure /var/run/mdadm.map contains this array. | |
71 | * 7/ Is there enough devices to possibly start the array? | |
72 | * For a container, this means running Incremental_container. | |
73 | * 7a/ if not, finish with success. | |
74 | * 7b/ if yes, | |
75 | * - read all metadata and arrange devices like -A does | |
76 | * - if number of OK devices match expected, or -R and there are enough, | |
77 | * start the array (auto-readonly). | |
78 | */ | |
79 | struct stat stb; | |
80 | struct mdinfo info; | |
81 | struct mddev_ident_s *array_list, *match; | |
82 | char chosen_name[1024]; | |
83 | int rv; | |
84 | struct map_ent *mp, *map = NULL; | |
85 | int dfd, mdfd; | |
86 | char *avail; | |
87 | int active_disks; | |
88 | int trustworthy = FOREIGN; | |
89 | char *name_to_use; | |
90 | mdu_array_info_t ainf; | |
91 | ||
92 | struct createinfo *ci = conf_get_create_info(); | |
93 | ||
94 | ||
95 | /* 1/ Check if device is permitted by mdadm.conf */ | |
96 | ||
97 | if (!conf_test_dev(devname)) { | |
98 | if (verbose >= 0) | |
99 | fprintf(stderr, Name | |
100 | ": %s not permitted by mdadm.conf.\n", | |
101 | devname); | |
102 | return 1; | |
103 | } | |
104 | ||
105 | /* 2/ Find metadata, reject if none appropriate (check | |
106 | * version/name from args) */ | |
107 | ||
108 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
109 | if (dfd < 0) { | |
110 | if (verbose >= 0) | |
111 | fprintf(stderr, Name ": cannot open %s: %s.\n", | |
112 | devname, strerror(errno)); | |
113 | return 1; | |
114 | } | |
115 | if (fstat(dfd, &stb) < 0) { | |
116 | if (verbose >= 0) | |
117 | fprintf(stderr, Name ": fstat failed for %s: %s.\n", | |
118 | devname, strerror(errno)); | |
119 | close(dfd); | |
120 | return 1; | |
121 | } | |
122 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
123 | if (verbose >= 0) | |
124 | fprintf(stderr, Name ": %s is not a block device.\n", | |
125 | devname); | |
126 | close(dfd); | |
127 | return 1; | |
128 | } | |
129 | ||
130 | if (st == NULL && (st = guess_super(dfd)) == NULL) { | |
131 | if (verbose >= 0) | |
132 | fprintf(stderr, Name | |
133 | ": no recognisable superblock on %s.\n", | |
134 | devname); | |
135 | close(dfd); | |
136 | return 1; | |
137 | } | |
138 | if (st->ss->load_super(st, dfd, NULL)) { | |
139 | if (verbose >= 0) | |
140 | fprintf(stderr, Name ": no RAID superblock on %s.\n", | |
141 | devname); | |
142 | close(dfd); | |
143 | return 1; | |
144 | } | |
145 | close (dfd); | |
146 | ||
147 | memset(&info, 0, sizeof(info)); | |
148 | st->ss->getinfo_super(st, &info); | |
149 | /* 3/ Check if there is a match in mdadm.conf */ | |
150 | ||
151 | array_list = conf_get_ident(NULL); | |
152 | match = NULL; | |
153 | for (; array_list; array_list = array_list->next) { | |
154 | if (array_list->uuid_set && | |
155 | same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid) | |
156 | == 0) { | |
157 | if (verbose >= 2 && array_list->devname) | |
158 | fprintf(stderr, Name | |
159 | ": UUID differs from %s.\n", | |
160 | array_list->devname); | |
161 | continue; | |
162 | } | |
163 | if (array_list->name[0] && | |
164 | strcasecmp(array_list->name, info.name) != 0) { | |
165 | if (verbose >= 2 && array_list->devname) | |
166 | fprintf(stderr, Name | |
167 | ": Name differs from %s.\n", | |
168 | array_list->devname); | |
169 | continue; | |
170 | } | |
171 | if (array_list->devices && | |
172 | !match_oneof(array_list->devices, devname)) { | |
173 | if (verbose >= 2 && array_list->devname) | |
174 | fprintf(stderr, Name | |
175 | ": Not a listed device for %s.\n", | |
176 | array_list->devname); | |
177 | continue; | |
178 | } | |
179 | if (array_list->super_minor != UnSet && | |
180 | array_list->super_minor != info.array.md_minor) { | |
181 | if (verbose >= 2 && array_list->devname) | |
182 | fprintf(stderr, Name | |
183 | ": Different super-minor to %s.\n", | |
184 | array_list->devname); | |
185 | continue; | |
186 | } | |
187 | if (!array_list->uuid_set && | |
188 | !array_list->name[0] && | |
189 | !array_list->devices && | |
190 | array_list->super_minor == UnSet) { | |
191 | if (verbose >= 2 && array_list->devname) | |
192 | fprintf(stderr, Name | |
193 | ": %s doesn't have any identifying information.\n", | |
194 | array_list->devname); | |
195 | continue; | |
196 | } | |
197 | /* FIXME, should I check raid_disks and level too?? */ | |
198 | ||
199 | if (match) { | |
200 | if (verbose >= 0) { | |
201 | if (match->devname && array_list->devname) | |
202 | fprintf(stderr, Name | |
203 | ": we match both %s and %s - cannot decide which to use.\n", | |
204 | match->devname, array_list->devname); | |
205 | else | |
206 | fprintf(stderr, Name | |
207 | ": multiple lines in mdadm.conf match\n"); | |
208 | } | |
209 | return 2; | |
210 | } | |
211 | match = array_list; | |
212 | } | |
213 | ||
214 | /* 3a/ if not, check for homehost match. If no match, continue | |
215 | * but don't trust the 'name' in the array. Thus a 'random' minor | |
216 | * number will be assigned, and the device name will be based | |
217 | * on that. */ | |
218 | if (match) | |
219 | trustworthy = LOCAL; | |
220 | else if (homehost == NULL || | |
221 | st->ss->match_home(st, homehost) != 1) | |
222 | trustworthy = FOREIGN; | |
223 | name_to_use = strchr(info.name, ':'); | |
224 | if (name_to_use) | |
225 | name_to_use++; | |
226 | else | |
227 | name_to_use = info.name; | |
228 | ||
229 | /* There are three possible sources for 'autof': command line, | |
230 | * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf. | |
231 | * ARRAY takes precedence, then command line, then | |
232 | * CREATE. | |
233 | */ | |
234 | if (match && match->autof) | |
235 | autof = match->autof; | |
236 | if (autof == 0) | |
237 | autof = ci->autof; | |
238 | ||
239 | if (st->ss->container_content && st->loaded_container) { | |
240 | /* This is a pre-built container array, so we do something | |
241 | * rather different. | |
242 | */ | |
243 | return Incremental_container(st, devname, verbose, runstop, | |
244 | autof, trustworthy); | |
245 | } | |
246 | /* 4/ Check if array exists. | |
247 | */ | |
248 | map_lock(&map); | |
249 | mp = map_by_uuid(&map, info.uuid); | |
250 | if (mp) | |
251 | mdfd = open_mddev(mp->path, 0); | |
252 | else | |
253 | mdfd = -1; | |
254 | ||
255 | if (mdfd < 0) { | |
256 | struct mdinfo *sra; | |
257 | struct mdinfo dinfo; | |
258 | ||
259 | /* Couldn't find an existing array, maybe make a new one */ | |
260 | mdfd = create_mddev(mp ? mp->path : match ? match->devname : NULL, | |
261 | info.name, autof, trustworthy, chosen_name); | |
262 | ||
263 | ||
264 | if (mdfd < 0) | |
265 | return 1; | |
266 | ||
267 | sysfs_init(&info, mdfd, 0); | |
268 | ||
269 | if (set_array_info(mdfd, st, &info) != 0) { | |
270 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", | |
271 | chosen_name, strerror(errno)); | |
272 | close(mdfd); | |
273 | return 2; | |
274 | } | |
275 | ||
276 | dinfo = info; | |
277 | dinfo.disk.major = major(stb.st_rdev); | |
278 | dinfo.disk.minor = minor(stb.st_rdev); | |
279 | if (add_disk(mdfd, st, &info, &dinfo) != 0) { | |
280 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
281 | devname, chosen_name, strerror(errno)); | |
282 | ioctl(mdfd, STOP_ARRAY, 0); | |
283 | close(mdfd); | |
284 | return 2; | |
285 | } | |
286 | sra = sysfs_read(mdfd, fd2devnum(mdfd), GET_DEVS); | |
287 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { | |
288 | /* It really should be 'none' - must be old buggy | |
289 | * kernel, and mdadm -I may not be able to complete. | |
290 | * So reject it. | |
291 | */ | |
292 | ioctl(mdfd, STOP_ARRAY, NULL); | |
293 | fprintf(stderr, Name | |
294 | ": You have an old buggy kernel which cannot support\n" | |
295 | " --incremental reliably. Aborting.\n"); | |
296 | close(mdfd); | |
297 | sysfs_free(sra); | |
298 | return 2; | |
299 | } | |
300 | info.array.working_disks = 1; | |
301 | sysfs_free(sra); | |
302 | /* 6/ Make sure /var/run/mdadm.map contains this array. */ | |
303 | map_update(&map, fd2devnum(mdfd), | |
304 | info.text_version, | |
305 | info.uuid, chosen_name); | |
306 | } else { | |
307 | /* 5b/ if it does */ | |
308 | /* - check one drive in array to make sure metadata is a reasonably */ | |
309 | /* close match. Reject if not (e.g. different type) */ | |
310 | /* - add the device */ | |
311 | char dn[20]; | |
312 | int dfd2; | |
313 | int err; | |
314 | struct mdinfo *sra; | |
315 | struct supertype *st2; | |
316 | struct mdinfo info2, *d; | |
317 | ||
318 | strcpy(chosen_name, mp->path); | |
319 | ||
320 | sra = sysfs_read(mdfd, fd2devnum(mdfd), (GET_DEVS | GET_STATE)); | |
321 | ||
322 | sprintf(dn, "%d:%d", sra->devs->disk.major, | |
323 | sra->devs->disk.minor); | |
324 | dfd2 = dev_open(dn, O_RDONLY); | |
325 | st2 = dup_super(st); | |
326 | if (st2->ss->load_super(st2, dfd2, NULL) || | |
327 | st->ss->compare_super(st, st2) != 0) { | |
328 | fprintf(stderr, Name | |
329 | ": metadata mismatch between %s and " | |
330 | "chosen array %s\n", | |
331 | devname, chosen_name); | |
332 | close(mdfd); | |
333 | close(dfd2); | |
334 | return 2; | |
335 | } | |
336 | close(dfd2); | |
337 | memset(&info2, 0, sizeof(info2)); | |
338 | st2->ss->getinfo_super(st2, &info2); | |
339 | st2->ss->free_super(st2); | |
340 | if (info.array.level != info2.array.level || | |
341 | memcmp(info.uuid, info2.uuid, 16) != 0 || | |
342 | info.array.raid_disks != info2.array.raid_disks) { | |
343 | fprintf(stderr, Name | |
344 | ": unexpected difference between %s and %s.\n", | |
345 | chosen_name, devname); | |
346 | close(mdfd); | |
347 | return 2; | |
348 | } | |
349 | info2.disk.major = major(stb.st_rdev); | |
350 | info2.disk.minor = minor(stb.st_rdev); | |
351 | /* add disk needs to know about containers */ | |
352 | if (st->ss->external) | |
353 | sra->array.level = LEVEL_CONTAINER; | |
354 | err = add_disk(mdfd, st2, sra, &info2); | |
355 | if (err < 0 && errno == EBUSY) { | |
356 | /* could be another device present with the same | |
357 | * disk.number. Find and reject any such | |
358 | */ | |
359 | find_reject(mdfd, st, sra, info.disk.number, | |
360 | info.events, verbose, chosen_name); | |
361 | err = add_disk(mdfd, st2, sra, &info2); | |
362 | } | |
363 | if (err < 0) { | |
364 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
365 | devname, chosen_name, strerror(errno)); | |
366 | close(mdfd); | |
367 | return 2; | |
368 | } | |
369 | info.array.working_disks = 0; | |
370 | for (d = sra->devs; d; d=d->next) | |
371 | info.array.working_disks ++; | |
372 | ||
373 | } | |
374 | ||
375 | /* 7/ Is there enough devices to possibly start the array? */ | |
376 | /* 7a/ if not, finish with success. */ | |
377 | if (info.array.level == LEVEL_CONTAINER) { | |
378 | /* Try to assemble within the container */ | |
379 | close(mdfd); | |
380 | map_unlock(&map); | |
381 | sysfs_uevent(&info, "change"); | |
382 | if (verbose >= 0) | |
383 | fprintf(stderr, Name | |
384 | ": container %s now has %d devices\n", | |
385 | chosen_name, info.array.working_disks); | |
386 | return Incremental(chosen_name, verbose, runstop, | |
387 | NULL, homehost, autof); | |
388 | } | |
389 | avail = NULL; | |
390 | active_disks = count_active(st, mdfd, &avail, &info); | |
391 | if (enough(info.array.level, info.array.raid_disks, | |
392 | info.array.layout, info.array.state & 1, | |
393 | avail, active_disks) == 0) { | |
394 | free(avail); | |
395 | if (verbose >= 0) | |
396 | fprintf(stderr, Name | |
397 | ": %s attached to %s, not enough to start (%d).\n", | |
398 | devname, chosen_name, active_disks); | |
399 | map_unlock(&map); | |
400 | close(mdfd); | |
401 | return 0; | |
402 | } | |
403 | free(avail); | |
404 | ||
405 | /* 7b/ if yes, */ | |
406 | /* - if number of OK devices match expected, or -R and there */ | |
407 | /* are enough, */ | |
408 | /* + add any bitmap file */ | |
409 | /* + start the array (auto-readonly). */ | |
410 | ||
411 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
412 | if (verbose >= 0) | |
413 | fprintf(stderr, Name | |
414 | ": %s attached to %s which is already active.\n", | |
415 | devname, chosen_name); | |
416 | close(mdfd); | |
417 | map_unlock(&map); | |
418 | return 0; | |
419 | } | |
420 | ||
421 | map_unlock(&map); | |
422 | if (runstop > 0 || active_disks >= info.array.working_disks) { | |
423 | struct mdinfo *sra; | |
424 | /* Let's try to start it */ | |
425 | if (match && match->bitmap_file) { | |
426 | int bmfd = open(match->bitmap_file, O_RDWR); | |
427 | if (bmfd < 0) { | |
428 | fprintf(stderr, Name | |
429 | ": Could not open bitmap file %s.\n", | |
430 | match->bitmap_file); | |
431 | close(mdfd); | |
432 | return 1; | |
433 | } | |
434 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
435 | close(bmfd); | |
436 | fprintf(stderr, Name | |
437 | ": Failed to set bitmapfile for %s.\n", | |
438 | chosen_name); | |
439 | close(mdfd); | |
440 | return 1; | |
441 | } | |
442 | close(bmfd); | |
443 | } | |
444 | sra = sysfs_read(mdfd, fd2devnum(mdfd), 0); | |
445 | if ((sra == NULL || active_disks >= info.array.working_disks) | |
446 | && trustworthy != FOREIGN) | |
447 | rv = ioctl(mdfd, RUN_ARRAY, NULL); | |
448 | else | |
449 | rv = sysfs_set_str(sra, NULL, | |
450 | "array_state", "read-auto"); | |
451 | if (rv == 0) { | |
452 | if (verbose >= 0) | |
453 | fprintf(stderr, Name | |
454 | ": %s attached to %s, which has been started.\n", | |
455 | devname, chosen_name); | |
456 | rv = 0; | |
457 | } else { | |
458 | fprintf(stderr, Name | |
459 | ": %s attached to %s, but failed to start: %s.\n", | |
460 | devname, chosen_name, strerror(errno)); | |
461 | rv = 1; | |
462 | } | |
463 | } else { | |
464 | if (verbose >= 0) | |
465 | fprintf(stderr, Name | |
466 | ": %s attached to %s, not enough to start safely.\n", | |
467 | devname, chosen_name); | |
468 | rv = 0; | |
469 | } | |
470 | close(mdfd); | |
471 | return rv; | |
472 | } | |
473 | ||
474 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, | |
475 | int number, __u64 events, int verbose, | |
476 | char *array_name) | |
477 | { | |
478 | /* Find a device attached to this array with a disk.number of number | |
479 | * and events less than the passed events, and remove the device. | |
480 | */ | |
481 | struct mdinfo *d; | |
482 | mdu_array_info_t ra; | |
483 | ||
484 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
485 | return; /* not safe to remove from active arrays | |
486 | * without thinking more */ | |
487 | ||
488 | for (d = sra->devs; d ; d = d->next) { | |
489 | char dn[10]; | |
490 | int dfd; | |
491 | struct mdinfo info; | |
492 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); | |
493 | dfd = dev_open(dn, O_RDONLY); | |
494 | if (dfd < 0) | |
495 | continue; | |
496 | if (st->ss->load_super(st, dfd, NULL)) { | |
497 | close(dfd); | |
498 | continue; | |
499 | } | |
500 | st->ss->getinfo_super(st, &info); | |
501 | st->ss->free_super(st); | |
502 | close(dfd); | |
503 | ||
504 | if (info.disk.number != number || | |
505 | info.events >= events) | |
506 | continue; | |
507 | ||
508 | if (d->disk.raid_disk > -1) | |
509 | sysfs_set_str(sra, d, "slot", "none"); | |
510 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
511 | if (verbose >= 0) | |
512 | fprintf(stderr, Name | |
513 | ": removing old device %s from %s\n", | |
514 | d->sys_name+4, array_name); | |
515 | } | |
516 | } | |
517 | ||
518 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
519 | struct mdinfo *bestinfo) | |
520 | { | |
521 | /* count how many devices in sra think they are active */ | |
522 | struct mdinfo *d; | |
523 | int cnt = 0, cnt1 = 0; | |
524 | __u64 max_events = 0; | |
525 | struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); | |
526 | char *avail = NULL; | |
527 | ||
528 | for (d = sra->devs ; d ; d = d->next) { | |
529 | char dn[30]; | |
530 | int dfd; | |
531 | int ok; | |
532 | struct mdinfo info; | |
533 | ||
534 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); | |
535 | dfd = dev_open(dn, O_RDONLY); | |
536 | if (dfd < 0) | |
537 | continue; | |
538 | ok = st->ss->load_super(st, dfd, NULL); | |
539 | close(dfd); | |
540 | if (ok != 0) | |
541 | continue; | |
542 | st->ss->getinfo_super(st, &info); | |
543 | if (!avail) { | |
544 | avail = malloc(info.array.raid_disks); | |
545 | if (!avail) { | |
546 | fprintf(stderr, Name ": out of memory.\n"); | |
547 | exit(1); | |
548 | } | |
549 | memset(avail, 0, info.array.raid_disks); | |
550 | *availp = avail; | |
551 | } | |
552 | ||
553 | if (info.disk.state & (1<<MD_DISK_SYNC)) | |
554 | { | |
555 | if (cnt == 0) { | |
556 | cnt++; | |
557 | max_events = info.events; | |
558 | avail[info.disk.raid_disk] = 2; | |
559 | st->ss->getinfo_super(st, bestinfo); | |
560 | } else if (info.events == max_events) { | |
561 | cnt++; | |
562 | avail[info.disk.raid_disk] = 2; | |
563 | } else if (info.events == max_events-1) { | |
564 | cnt1++; | |
565 | avail[info.disk.raid_disk] = 1; | |
566 | } else if (info.events < max_events - 1) | |
567 | ; | |
568 | else if (info.events == max_events+1) { | |
569 | int i; | |
570 | cnt1 = cnt; | |
571 | cnt = 1; | |
572 | max_events = info.events; | |
573 | for (i=0; i<info.array.raid_disks; i++) | |
574 | if (avail[i]) | |
575 | avail[i]--; | |
576 | avail[info.disk.raid_disk] = 2; | |
577 | st->ss->getinfo_super(st, bestinfo); | |
578 | } else { /* info.events much bigger */ | |
579 | cnt = 1; cnt1 = 0; | |
580 | memset(avail, 0, info.disk.raid_disk); | |
581 | max_events = info.events; | |
582 | st->ss->getinfo_super(st, bestinfo); | |
583 | } | |
584 | } | |
585 | st->ss->free_super(st); | |
586 | } | |
587 | return cnt + cnt1; | |
588 | } | |
589 | ||
590 | void RebuildMap(void) | |
591 | { | |
592 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
593 | struct mdstat_ent *md; | |
594 | struct map_ent *map = NULL; | |
595 | int mdp = get_mdp_major(); | |
596 | ||
597 | for (md = mdstat ; md ; md = md->next) { | |
598 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS); | |
599 | struct mdinfo *sd; | |
600 | ||
601 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
602 | char dn[30]; | |
603 | int dfd; | |
604 | int ok; | |
605 | struct supertype *st; | |
606 | char *path; | |
607 | struct mdinfo info; | |
608 | ||
609 | sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor); | |
610 | dfd = dev_open(dn, O_RDONLY); | |
611 | if (dfd < 0) | |
612 | continue; | |
613 | st = guess_super(dfd); | |
614 | if ( st == NULL) | |
615 | ok = -1; | |
616 | else | |
617 | ok = st->ss->load_super(st, dfd, NULL); | |
618 | close(dfd); | |
619 | if (ok != 0) | |
620 | continue; | |
621 | st->ss->getinfo_super(st, &info); | |
622 | if (md->devnum > 0) | |
623 | path = map_dev(MD_MAJOR, md->devnum, 0); | |
624 | else | |
625 | path = map_dev(mdp, (-1-md->devnum)<< 6, 0); | |
626 | map_add(&map, md->devnum, | |
627 | info.text_version, | |
628 | info.uuid, path ? : "/unknown"); | |
629 | st->ss->free_super(st); | |
630 | break; | |
631 | } | |
632 | sysfs_free(sra); | |
633 | } | |
634 | map_write(map); | |
635 | map_free(map); | |
636 | for (md = mdstat ; md ; md = md->next) { | |
637 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_VERSION); | |
638 | sysfs_uevent(sra, "change"); | |
639 | sysfs_free(sra); | |
640 | } | |
641 | } | |
642 | ||
643 | int IncrementalScan(int verbose) | |
644 | { | |
645 | /* look at every device listed in the 'map' file. | |
646 | * If one is found that is not running then: | |
647 | * look in mdadm.conf for bitmap file. | |
648 | * if one exists, but array has none, add it. | |
649 | * try to start array in auto-readonly mode | |
650 | */ | |
651 | struct map_ent *mapl = NULL; | |
652 | struct map_ent *me; | |
653 | mddev_ident_t devs, mddev; | |
654 | int rv = 0; | |
655 | ||
656 | map_read(&mapl); | |
657 | devs = conf_get_ident(NULL); | |
658 | ||
659 | for (me = mapl ; me ; me = me->next) { | |
660 | mdu_array_info_t array; | |
661 | mdu_bitmap_file_t bmf; | |
662 | struct mdinfo *sra; | |
663 | int mdfd = open_mddev(me->path, 0); | |
664 | ||
665 | if (mdfd < 0) | |
666 | continue; | |
667 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
668 | errno != ENODEV) { | |
669 | close(mdfd); | |
670 | continue; | |
671 | } | |
672 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
673 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
674 | if (mddev->devname | |
675 | && strcmp(mddev->devname, me->path) == 0) | |
676 | break; | |
677 | if (mddev && mddev->bitmap_file) { | |
678 | /* | |
679 | * Note: early kernels will wrongly fail this, so it | |
680 | * is a hint only | |
681 | */ | |
682 | int added = -1; | |
683 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
684 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
685 | if (bmfd >= 0) { | |
686 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
687 | bmfd); | |
688 | close(bmfd); | |
689 | } | |
690 | } | |
691 | if (verbose >= 0) { | |
692 | if (added == 0) | |
693 | fprintf(stderr, Name | |
694 | ": Added bitmap %s to %s\n", | |
695 | mddev->bitmap_file, me->path); | |
696 | else if (errno != EEXIST) | |
697 | fprintf(stderr, Name | |
698 | ": Failed to add bitmap to %s: %s\n", | |
699 | me->path, strerror(errno)); | |
700 | } | |
701 | } | |
702 | sra = sysfs_read(mdfd, 0, 0); | |
703 | if (sra) { | |
704 | if (sysfs_set_str(sra, NULL, | |
705 | "array_state", "read-auto") == 0) { | |
706 | if (verbose >= 0) | |
707 | fprintf(stderr, Name | |
708 | ": started array %s\n", | |
709 | me->path); | |
710 | } else { | |
711 | fprintf(stderr, Name | |
712 | ": failed to start array %s: %s\n", | |
713 | me->path, strerror(errno)); | |
714 | rv = 1; | |
715 | } | |
716 | } | |
717 | } | |
718 | return rv; | |
719 | } | |
720 | ||
721 | static char *container2devname(char *devname) | |
722 | { | |
723 | char *mdname = NULL; | |
724 | ||
725 | if (devname[0] == '/') { | |
726 | int fd = open(devname, O_RDONLY); | |
727 | if (fd >= 0) { | |
728 | mdname = devnum2devname(fd2devnum(fd)); | |
729 | close(fd); | |
730 | } | |
731 | } else { | |
732 | int uuid[4]; | |
733 | struct map_ent *mp, *map = NULL; | |
734 | ||
735 | if (!parse_uuid(devname, uuid)) | |
736 | return mdname; | |
737 | mp = map_by_uuid(&map, uuid); | |
738 | if (mp) | |
739 | mdname = devnum2devname(mp->devnum); | |
740 | map_free(map); | |
741 | } | |
742 | ||
743 | return mdname; | |
744 | } | |
745 | ||
746 | int Incremental_container(struct supertype *st, char *devname, int verbose, | |
747 | int runstop, int autof, int trustworthy) | |
748 | { | |
749 | /* Collect the contents of this container and for each | |
750 | * array, choose a device name and assemble the array. | |
751 | */ | |
752 | ||
753 | struct mdinfo *list = st->ss->container_content(st); | |
754 | struct mdinfo *ra; | |
755 | struct map_ent *map = NULL; | |
756 | ||
757 | map_lock(&map); | |
758 | ||
759 | for (ra = list ; ra ; ra = ra->next) { | |
760 | struct mdinfo *dev, *sra; | |
761 | int mdfd; | |
762 | char chosen_name[1024]; | |
763 | int working = 0, preexist = 0; | |
764 | struct map_ent *mp; | |
765 | struct mddev_ident_s *match = NULL; | |
766 | ||
767 | mp = map_by_uuid(&map, ra->uuid); | |
768 | ||
769 | if (!mp) { | |
770 | ||
771 | /* Check in mdadm.conf for devices == devname and | |
772 | * member == ra->text_version after second slash. | |
773 | */ | |
774 | char *sub = strchr(ra->text_version+1, '/'); | |
775 | struct mddev_ident_s *array_list; | |
776 | if (sub) { | |
777 | sub++; | |
778 | array_list = conf_get_ident(NULL); | |
779 | } else | |
780 | array_list = NULL; | |
781 | for(; array_list ; array_list = array_list->next) { | |
782 | char *dn; | |
783 | if (array_list->member == NULL || | |
784 | array_list->container == NULL) | |
785 | continue; | |
786 | if (strcmp(array_list->member, sub) != 0) | |
787 | continue; | |
788 | if (array_list->uuid_set && | |
789 | !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid)) | |
790 | continue; | |
791 | dn = container2devname(array_list->container); | |
792 | if (dn == NULL) | |
793 | continue; | |
794 | if (strncmp(dn, ra->text_version+1, | |
795 | strlen(dn)) != 0 || | |
796 | ra->text_version[strlen(dn)+1] != '/') { | |
797 | free(dn); | |
798 | continue; | |
799 | } | |
800 | free(dn); | |
801 | /* we have a match */ | |
802 | match = array_list; | |
803 | if (verbose>0) | |
804 | fprintf(stderr, Name ": match found for member %s\n", | |
805 | array_list->member); | |
806 | break; | |
807 | } | |
808 | } | |
809 | ||
810 | mdfd = create_mddev(match ? match->devname : NULL, | |
811 | ra->name, | |
812 | autof, | |
813 | trustworthy, | |
814 | chosen_name); | |
815 | ||
816 | if (mdfd < 0) { | |
817 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
818 | chosen_name, strerror(errno)); | |
819 | return 2; | |
820 | } | |
821 | ||
822 | ||
823 | sysfs_init(ra, mdfd, 0); | |
824 | ||
825 | sra = sysfs_read(mdfd, 0, GET_VERSION); | |
826 | if (sra == NULL || strcmp(sra->text_version, ra->text_version) != 0) | |
827 | if (sysfs_set_array(ra, md_get_version(mdfd)) != 0) | |
828 | return 1; | |
829 | if (sra) | |
830 | sysfs_free(sra); | |
831 | ||
832 | for (dev = ra->devs; dev; dev = dev->next) | |
833 | if (sysfs_add_disk(ra, dev) == 0) | |
834 | working++; | |
835 | else if (errno == EEXIST) | |
836 | preexist++; | |
837 | if (working == 0) | |
838 | /* Nothing new, don't try to start */ ; | |
839 | else if (runstop > 0 || | |
840 | (working + preexist) >= ra->array.working_disks) { | |
841 | switch(ra->array.level) { | |
842 | case LEVEL_LINEAR: | |
843 | case LEVEL_MULTIPATH: | |
844 | case 0: | |
845 | sysfs_set_str(ra, NULL, "array_state", | |
846 | "active"); | |
847 | break; | |
848 | default: | |
849 | sysfs_set_str(ra, NULL, "array_state", | |
850 | "readonly"); | |
851 | /* start mdmon if needed. */ | |
852 | if (!mdmon_running(st->container_dev)) | |
853 | start_mdmon(st->container_dev); | |
854 | ping_monitor(devnum2devname(st->container_dev)); | |
855 | break; | |
856 | } | |
857 | sysfs_set_safemode(ra, ra->safe_mode_delay); | |
858 | if (verbose >= 0) { | |
859 | fprintf(stderr, Name | |
860 | ": Started %s with %d devices", | |
861 | chosen_name, working + preexist); | |
862 | if (preexist) | |
863 | fprintf(stderr, " (%d new)", working); | |
864 | fprintf(stderr, "\n"); | |
865 | } | |
866 | /* FIXME should have an O_EXCL and wait for read-auto */ | |
867 | } else | |
868 | if (verbose >= 0) | |
869 | fprintf(stderr, Name | |
870 | ": %s assembled with %d devices but " | |
871 | "not started\n", | |
872 | chosen_name, working); | |
873 | map_update(&map, fd2devnum(mdfd), | |
874 | ra->text_version, | |
875 | ra->uuid, chosen_name); | |
876 | close(mdfd); | |
877 | } | |
878 | map_unlock(&map); | |
879 | return 0; | |
880 | } |