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