]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
IncrementalScan: Use md_array_active() instead of md_get_array_info()
[thirdparty/mdadm.git] / Incremental.c
1 /*
2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
4 *
5 * Copyright (C) 2006-2013 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 #include <sys/wait.h>
33 #include <dirent.h>
34 #include <ctype.h>
35
36 static int count_active(struct supertype *st, struct mdinfo *sra,
37 int mdfd, char **availp,
38 struct mdinfo *info);
39 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
40 int number, __u64 events, int verbose,
41 char *array_name);
42 static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
43 struct map_ent *target,
44 struct supertype *st, int verbose);
45
46 static int Incremental_container(struct supertype *st, char *devname,
47 struct context *c, char *only);
48
49 int Incremental(struct mddev_dev *devlist, struct context *c,
50 struct supertype *st)
51 {
52 /* Add this device to an array, creating the array if necessary
53 * and starting the array if sensible or - if runstop>0 - if possible.
54 *
55 * This has several steps:
56 *
57 * 1/ Check if device is permitted by mdadm.conf, reject if not.
58 * 2/ Find metadata, reject if none appropriate (check
59 * version/name from args)
60 * 3/ Check if there is a match in mdadm.conf
61 * 3a/ if not, check for homehost match. If no match, assemble as
62 * a 'foreign' array.
63 * 4/ Determine device number.
64 * - If in mdadm.conf with std name, use that
65 * - UUID in /var/run/mdadm.map use that
66 * - If name is suggestive, use that. unless in use with different uuid.
67 * - Choose a free, high number.
68 * - Use a partitioned device unless strong suggestion not to.
69 * e.g. auto=md
70 * Don't choose partitioned for containers.
71 * 5/ Find out if array already exists
72 * 5a/ if it does not
73 * - choose a name, from mdadm.conf or 'name' field in array.
74 * - create the array
75 * - add the device
76 * 5b/ if it does
77 * - check one drive in array to make sure metadata is a reasonably
78 * close match. Reject if not (e.g. different type)
79 * - add the device
80 * 6/ Make sure /var/run/mdadm.map contains this array.
81 * 7/ Is there enough devices to possibly start the array?
82 * For a container, this means running Incremental_container.
83 * 7a/ if not, finish with success.
84 * 7b/ if yes,
85 * - read all metadata and arrange devices like -A does
86 * - if number of OK devices match expected, or -R and there are enough,
87 * start the array (auto-readonly).
88 */
89 dev_t rdev, rdev2;
90 struct mdinfo info, dinfo;
91 struct mdinfo *sra = NULL, *d;
92 struct mddev_ident *match;
93 char chosen_name[1024];
94 char *md_devname;
95 int rv = 1;
96 struct map_ent *mp, *map = NULL;
97 int dfd = -1, mdfd = -1;
98 char *avail = NULL;
99 int active_disks;
100 int trustworthy;
101 char *name_to_use;
102 struct dev_policy *policy = NULL;
103 struct map_ent target_array;
104 int have_target;
105 char *devname = devlist->devname;
106 int journal_device_missing = 0;
107
108 struct createinfo *ci = conf_get_create_info();
109
110 if (!stat_is_blkdev(devname, &rdev))
111 return rv;
112 dfd = dev_open(devname, O_RDONLY);
113 if (dfd < 0) {
114 if (c->verbose >= 0)
115 pr_err("cannot open %s: %s.\n",
116 devname, strerror(errno));
117 return rv;
118 }
119 /* If the device is a container, we do something very different */
120 if (must_be_container(dfd)) {
121 if (!st)
122 st = super_by_fd(dfd, NULL);
123 if (st && st->ss->load_container)
124 rv = st->ss->load_container(st, dfd, NULL);
125
126 close(dfd);
127 if (!rv && st->ss->container_content) {
128 if (map_lock(&map))
129 pr_err("failed to get exclusive lock on mapfile\n");
130 if (c->export)
131 printf("MD_DEVNAME=%s\n", devname);
132 rv = Incremental_container(st, devname, c, NULL);
133 map_unlock(&map);
134 return rv;
135 }
136
137 pr_err("%s is not part of an md array.\n",
138 devname);
139 return rv;
140 }
141
142 /* 1/ Check if device is permitted by mdadm.conf */
143
144 for (;devlist; devlist = devlist->next)
145 if (conf_test_dev(devlist->devname))
146 break;
147 if (!devlist) {
148 devlist = conf_get_devs();
149 for (;devlist; devlist = devlist->next) {
150 if (stat_is_blkdev(devlist->devname, &rdev2) &&
151 rdev2 == rdev)
152 break;
153 }
154 }
155 if (!devlist) {
156 if (c->verbose >= 0)
157 pr_err("%s not permitted by mdadm.conf.\n",
158 devname);
159 goto out;
160 }
161
162 /* 2/ Find metadata, reject if none appropriate (check
163 * version/name from args) */
164
165 if (!fstat_is_blkdev(dfd, devname, &rdev))
166 goto out;
167
168 dinfo.disk.major = major(rdev);
169 dinfo.disk.minor = minor(rdev);
170
171 policy = disk_policy(&dinfo);
172 have_target = policy_check_path(&dinfo, &target_array);
173
174 if (st == NULL && (st = guess_super_type(dfd, guess_array)) == NULL) {
175 if (c->verbose >= 0)
176 pr_err("no recognisable superblock on %s.\n",
177 devname);
178 rv = try_spare(devname, &dfd, policy,
179 have_target ? &target_array : NULL,
180 NULL, c->verbose);
181 goto out;
182 }
183 st->ignore_hw_compat = 0;
184
185 if (st->ss->compare_super == NULL ||
186 st->ss->load_super(st, dfd, c->verbose >= 0 ? devname : NULL)) {
187 if (c->verbose >= 0)
188 pr_err("no RAID superblock on %s.\n",
189 devname);
190 rv = try_spare(devname, &dfd, policy,
191 have_target ? &target_array : NULL,
192 st, c->verbose);
193 free(st);
194 goto out;
195 }
196 close (dfd); dfd = -1;
197
198 st->ss->getinfo_super(st, &info, NULL);
199
200 /* 3/ Check if there is a match in mdadm.conf */
201 match = conf_match(st, &info, devname, c->verbose, &rv);
202 if (!match && rv == 2)
203 goto out;
204
205 if (match && match->devname &&
206 strcasecmp(match->devname, "<ignore>") == 0) {
207 if (c->verbose >= 0)
208 pr_err("array containing %s is explicitly ignored by mdadm.conf\n",
209 devname);
210 goto out;
211 }
212
213 /* 3a/ if not, check for homehost match. If no match, continue
214 * but don't trust the 'name' in the array. Thus a 'random' minor
215 * number will be assigned, and the device name will be based
216 * on that. */
217 if (match)
218 trustworthy = LOCAL;
219 else if (st->ss->match_home(st, c->homehost) == 1)
220 trustworthy = LOCAL;
221 else if (st->ss->match_home(st, "any") == 1)
222 trustworthy = LOCAL_ANY;
223 else
224 trustworthy = FOREIGN;
225
226 if (!match && !conf_test_metadata(st->ss->name, policy,
227 (trustworthy == LOCAL))) {
228 if (c->verbose >= 1)
229 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
230 devname, st->ss->name);
231 goto out;
232 }
233 if (trustworthy == LOCAL_ANY)
234 trustworthy = LOCAL;
235
236 /* There are three possible sources for 'autof': command line,
237 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
238 * ARRAY takes precedence, then command line, then
239 * CREATE.
240 */
241 if (match && match->autof)
242 c->autof = match->autof;
243 if (c->autof == 0)
244 c->autof = ci->autof;
245
246 name_to_use = info.name;
247 if (name_to_use[0] == 0 && info.array.level == LEVEL_CONTAINER) {
248 name_to_use = info.text_version;
249 trustworthy = METADATA;
250 }
251 if (name_to_use[0] && trustworthy != LOCAL &&
252 ! c->require_homehost &&
253 conf_name_is_free(name_to_use))
254 trustworthy = LOCAL;
255
256 /* strip "hostname:" prefix from name if we have decided
257 * to treat it as LOCAL
258 */
259 if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL)
260 name_to_use = strchr(name_to_use, ':')+1;
261
262 /* 4/ Check if array exists.
263 */
264 if (map_lock(&map))
265 pr_err("failed to get exclusive lock on mapfile\n");
266 /* Now check we can get O_EXCL. If not, probably "mdadm -A" has
267 * taken over
268 */
269 dfd = dev_open(devname, O_RDONLY|O_EXCL);
270 if (dfd < 0) {
271 if (c->verbose >= 0)
272 pr_err("cannot reopen %s: %s.\n",
273 devname, strerror(errno));
274 goto out_unlock;
275 }
276 /* Cannot hold it open while we add the device to the array,
277 * so we must release the O_EXCL and depend on the map_lock()
278 * So now is the best time to remove any partitions.
279 */
280 remove_partitions(dfd);
281 close(dfd);
282 dfd = -1;
283
284 mp = map_by_uuid(&map, info.uuid);
285 if (mp)
286 mdfd = open_dev(mp->devnm);
287 else
288 mdfd = -1;
289
290 if (mdfd < 0) {
291
292 /* Skip the clustered ones. This should be started by
293 * clustering resource agents
294 */
295 if (info.array.state & (1 << MD_SB_CLUSTERED))
296 goto out;
297
298 /* Couldn't find an existing array, maybe make a new one */
299 mdfd = create_mddev(match ? match->devname : NULL,
300 name_to_use, c->autof, trustworthy, chosen_name, 0);
301
302 if (mdfd < 0)
303 goto out_unlock;
304
305 if (sysfs_init(&info, mdfd, NULL)) {
306 pr_err("unable to initialize sysfs for %s\n",
307 chosen_name);
308 rv = 2;
309 goto out_unlock;
310 }
311
312 if (set_array_info(mdfd, st, &info) != 0) {
313 pr_err("failed to set array info for %s: %s\n",
314 chosen_name, strerror(errno));
315 rv = 2;
316 goto out_unlock;
317 }
318
319 dinfo = info;
320 dinfo.disk.major = major(rdev);
321 dinfo.disk.minor = minor(rdev);
322 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
323 pr_err("failed to add %s to new array %s: %s.\n",
324 devname, chosen_name, strerror(errno));
325 ioctl(mdfd, STOP_ARRAY, 0);
326 rv = 2;
327 goto out_unlock;
328 }
329 sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
330 GET_OFFSET | GET_SIZE));
331
332 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
333 /* It really should be 'none' - must be old buggy
334 * kernel, and mdadm -I may not be able to complete.
335 * So reject it.
336 */
337 ioctl(mdfd, STOP_ARRAY, NULL);
338 pr_err("You have an old buggy kernel which cannot support\n --incremental reliably. Aborting.\n");
339 rv = 2;
340 goto out_unlock;
341 }
342 info.array.working_disks = 1;
343 /* 6/ Make sure /var/run/mdadm.map contains this array. */
344 map_update(&map, fd2devnm(mdfd),
345 info.text_version,
346 info.uuid, chosen_name);
347 } else {
348 /* 5b/ if it does */
349 /* - check one drive in array to make sure metadata is a reasonably */
350 /* close match. Reject if not (e.g. different type) */
351 /* - add the device */
352 char dn[20];
353 int dfd2;
354 int err;
355 struct supertype *st2;
356 struct mdinfo info2, *d;
357
358 sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
359 GET_OFFSET | GET_SIZE));
360
361 if (mp->path)
362 strcpy(chosen_name, mp->path);
363 else
364 strcpy(chosen_name, mp->devnm);
365
366 /* It is generally not OK to add non-spare drives to a
367 * running array as they are probably missing because
368 * they failed. However if runstop is 1, then the
369 * array was possibly started early and our best bet is
370 * to add this anyway.
371 * Also if action policy is re-add or better we allow
372 * re-add.
373 * This doesn't apply to containers as the 'non-spare'
374 * flag has a different meaning. The test has to happen
375 * at the device level there
376 */
377 if (!st->ss->external &&
378 (info.disk.state & (1 << MD_DISK_SYNC)) != 0 &&
379 !policy_action_allows(policy, st->ss->name, act_re_add) &&
380 c->runstop < 1) {
381 if (md_array_active(mdfd)) {
382 pr_err("not adding %s to active array (without --run) %s\n",
383 devname, chosen_name);
384 rv = 2;
385 goto out_unlock;
386 }
387 }
388 if (!sra) {
389 rv = 2;
390 goto out_unlock;
391 }
392 if (sra->devs) {
393 sprintf(dn, "%d:%d", sra->devs->disk.major,
394 sra->devs->disk.minor);
395 dfd2 = dev_open(dn, O_RDONLY);
396 if (dfd2 < 0) {
397 pr_err("unable to open %s\n", devname);
398 rv = 2;
399 goto out_unlock;
400 }
401 st2 = dup_super(st);
402 if (st2->ss->load_super(st2, dfd2, NULL) ||
403 st->ss->compare_super(st, st2) != 0) {
404 pr_err("metadata mismatch between %s and chosen array %s\n",
405 devname, chosen_name);
406 close(dfd2);
407 rv = 2;
408 goto out_unlock;
409 }
410 close(dfd2);
411 st2->ss->getinfo_super(st2, &info2, NULL);
412 st2->ss->free_super(st2);
413 if (info.array.level != info2.array.level ||
414 memcmp(info.uuid, info2.uuid, 16) != 0 ||
415 info.array.raid_disks != info2.array.raid_disks) {
416 pr_err("unexpected difference between %s and %s.\n",
417 chosen_name, devname);
418 rv = 2;
419 goto out_unlock;
420 }
421 }
422 info.disk.major = major(rdev);
423 info.disk.minor = minor(rdev);
424 /* add disk needs to know about containers */
425 if (st->ss->external)
426 sra->array.level = LEVEL_CONTAINER;
427
428 if (info.array.state & (1 << MD_SB_CLUSTERED))
429 info.disk.state |= (1 << MD_DISK_CLUSTER_ADD);
430
431 err = add_disk(mdfd, st, sra, &info);
432 if (err < 0 && errno == EBUSY) {
433 /* could be another device present with the same
434 * disk.number. Find and reject any such
435 */
436 find_reject(mdfd, st, sra, info.disk.number,
437 info.events, c->verbose, chosen_name);
438 err = add_disk(mdfd, st, sra, &info);
439 }
440 if (err < 0 && errno == EINVAL &&
441 info.disk.state & (1<<MD_DISK_SYNC)) {
442 /* Maybe it needs to be added as a spare */
443 if (policy_action_allows(policy, st->ss->name,
444 act_force_spare)) {
445 info.disk.state &= ~(1<<MD_DISK_SYNC);
446 err = add_disk(mdfd, st, sra, &info);
447 } else
448 if (c->verbose >= 0)
449 pr_err("can only add %s to %s as a spare, and force-spare is not set.\n",
450 devname, chosen_name);
451 }
452 if (err < 0) {
453 pr_err("failed to add %s to existing array %s: %s.\n",
454 devname, chosen_name, strerror(errno));
455 rv = 2;
456 goto out_unlock;
457 }
458 info.array.working_disks = 0;
459 for (d = sra->devs; d; d=d->next)
460 info.array.working_disks ++;
461
462 }
463 if (strncmp(chosen_name, "/dev/md/", 8) == 0)
464 md_devname = chosen_name+8;
465 else
466 md_devname = chosen_name;
467 if (c->export) {
468 printf("MD_DEVICE=%s\n", fd2devnm(mdfd));
469 printf("MD_DEVNAME=%s\n", md_devname);
470 printf("MD_FOREIGN=%s\n", trustworthy == FOREIGN ? "yes" : "no");
471 }
472
473 /* 7/ Is there enough devices to possibly start the array? */
474 /* 7a/ if not, finish with success. */
475 if (info.array.level == LEVEL_CONTAINER) {
476 char devnm[32];
477 /* Try to assemble within the container */
478 sysfs_uevent(sra, "change");
479 if (!c->export && c->verbose >= 0)
480 pr_err("container %s now has %d device%s\n",
481 chosen_name, info.array.working_disks,
482 info.array.working_disks == 1?"":"s");
483 wait_for(chosen_name, mdfd);
484 if (st->ss->external)
485 strcpy(devnm, fd2devnm(mdfd));
486 if (st->ss->load_container)
487 rv = st->ss->load_container(st, mdfd, NULL);
488 close(mdfd);
489 sysfs_free(sra);
490 if (!rv)
491 rv = Incremental_container(st, chosen_name, c, NULL);
492 map_unlock(&map);
493 /* after spare is added, ping monitor for external metadata
494 * so that it can eg. try to rebuild degraded array */
495 if (st->ss->external)
496 ping_monitor(devnm);
497 return rv;
498 }
499
500 /* We have added something to the array, so need to re-read the
501 * state. Eventually this state should be kept up-to-date as
502 * things change.
503 */
504 sysfs_free(sra);
505 sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
506 GET_OFFSET | GET_SIZE));
507 active_disks = count_active(st, sra, mdfd, &avail, &info);
508
509 journal_device_missing = (info.journal_device_required) && (info.journal_clean == 0);
510
511 if (info.consistency_policy == CONSISTENCY_POLICY_PPL)
512 info.array.state |= 1;
513
514 if (enough(info.array.level, info.array.raid_disks,
515 info.array.layout, info.array.state & 1, avail) == 0) {
516 if (c->export) {
517 printf("MD_STARTED=no\n");
518 } else if (c->verbose >= 0)
519 pr_err("%s attached to %s, not enough to start (%d).\n",
520 devname, chosen_name, active_disks);
521 rv = 0;
522 goto out_unlock;
523 }
524
525 /* 7b/ if yes, */
526 /* - if number of OK devices match expected, or -R and there */
527 /* are enough, */
528 /* + add any bitmap file */
529 /* + start the array (auto-readonly). */
530
531 if (md_array_active(mdfd)) {
532 if (c->export) {
533 printf("MD_STARTED=already\n");
534 } else if (c->verbose >= 0)
535 pr_err("%s attached to %s which is already active.\n",
536 devname, chosen_name);
537 rv = 0;
538 goto out_unlock;
539 }
540
541 map_unlock(&map);
542 if (c->runstop > 0 || (!journal_device_missing && active_disks >= info.array.working_disks)) {
543 struct mdinfo *dsk;
544 /* Let's try to start it */
545
546 if (journal_device_missing)
547 pr_err("Trying to run with missing journal device\n");
548 if (info.reshape_active && !(info.reshape_active & RESHAPE_NO_BACKUP)) {
549 pr_err("%s: This array is being reshaped and cannot be started\n",
550 chosen_name);
551 cont_err("by --incremental. Please use --assemble\n");
552 goto out;
553 }
554 if (match && match->bitmap_file) {
555 int bmfd = open(match->bitmap_file, O_RDWR);
556 if (bmfd < 0) {
557 pr_err("Could not open bitmap file %s.\n",
558 match->bitmap_file);
559 goto out;
560 }
561 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
562 close(bmfd);
563 pr_err("Failed to set bitmapfile for %s.\n",
564 chosen_name);
565 goto out;
566 }
567 close(bmfd);
568 }
569 /* Need to remove from the array any devices which
570 * 'count_active' discerned were too old or inappropriate
571 */
572 for (d = sra ? sra->devs : NULL ; d ; d = d->next)
573 if (d->disk.state & (1<<MD_DISK_REMOVED))
574 remove_disk(mdfd, st, sra, d);
575
576 if ((sra == NULL || active_disks >= info.array.working_disks) &&
577 trustworthy != FOREIGN)
578 rv = ioctl(mdfd, RUN_ARRAY, NULL);
579 else
580 rv = sysfs_set_str(sra, NULL,
581 "array_state", "read-auto");
582 /* Array might be O_EXCL which will interfere with
583 * fsck and mount. So re-open without O_EXCL.
584 */
585 reopen_mddev(mdfd);
586 if (rv == 0) {
587 if (c->export) {
588 printf("MD_STARTED=yes\n");
589 } else if (c->verbose >= 0)
590 pr_err("%s attached to %s, which has been started.\n",
591 devname, chosen_name);
592 rv = 0;
593 wait_for(chosen_name, mdfd);
594 /* We just started the array, so some devices
595 * might have been evicted from the array
596 * because their event counts were too old.
597 * If the action=re-add policy is in-force for
598 * those devices we should re-add them now.
599 */
600 for (dsk = sra->devs; dsk ; dsk = dsk->next) {
601 if (disk_action_allows(dsk, st->ss->name,
602 act_re_add) &&
603 add_disk(mdfd, st, sra, dsk) == 0)
604 pr_err("%s re-added to %s\n",
605 dsk->sys_name, chosen_name);
606 }
607 } else {
608 pr_err("%s attached to %s, but failed to start: %s.\n",
609 devname, chosen_name, strerror(errno));
610 rv = 1;
611 }
612 } else {
613 if (c->export) {
614 printf("MD_STARTED=unsafe\n");
615 } else if (journal_device_missing) {
616 pr_err("Journal device is missing, not safe to start yet.\n");
617 } else if (c->verbose >= 0)
618 pr_err("%s attached to %s, not enough to start safely.\n",
619 devname, chosen_name);
620 rv = 0;
621 }
622 out:
623 free(avail);
624 if (dfd >= 0)
625 close(dfd);
626 if (mdfd >= 0)
627 close(mdfd);
628 if (policy)
629 dev_policy_free(policy);
630 sysfs_free(sra);
631 return rv;
632 out_unlock:
633 map_unlock(&map);
634 goto out;
635 }
636
637 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
638 int number, __u64 events, int verbose,
639 char *array_name)
640 {
641 /* Find a device attached to this array with a disk.number of number
642 * and events less than the passed events, and remove the device.
643 */
644 struct mdinfo *d;
645
646 if (md_array_active(mdfd))
647 return; /* not safe to remove from active arrays
648 * without thinking more */
649
650 for (d = sra->devs; d ; d = d->next) {
651 char dn[24]; // 2*11 bytes for ints (including sign) + colon + null byte
652 int dfd;
653 struct mdinfo info;
654 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
655 dfd = dev_open(dn, O_RDONLY);
656 if (dfd < 0)
657 continue;
658 if (st->ss->load_super(st, dfd, NULL)) {
659 close(dfd);
660 continue;
661 }
662 st->ss->getinfo_super(st, &info, NULL);
663 st->ss->free_super(st);
664 close(dfd);
665
666 if (info.disk.number != number || info.events >= events)
667 continue;
668
669 if (d->disk.raid_disk > -1)
670 sysfs_set_str(sra, d, "slot", "none");
671 if (sysfs_set_str(sra, d, "state", "remove") == 0)
672 if (verbose >= 0)
673 pr_err("removing old device %s from %s\n",
674 d->sys_name+4, array_name);
675 }
676 }
677
678 static int count_active(struct supertype *st, struct mdinfo *sra,
679 int mdfd, char **availp,
680 struct mdinfo *bestinfo)
681 {
682 /* count how many devices in sra think they are active */
683 struct mdinfo *d;
684 int cnt = 0;
685 int replcnt = 0;
686 __u64 max_events = 0;
687 char *avail = NULL;
688 int *best = NULL;
689 char *devmap = NULL;
690 int numdevs = 0;
691 int devnum;
692 int b, i;
693 int raid_disks = 0;
694
695 if (!sra)
696 return 0;
697
698 for (d = sra->devs ; d ; d = d->next)
699 numdevs++;
700 for (d = sra->devs, devnum = 0 ; d ; d = d->next, devnum++) {
701 char dn[30];
702 int dfd;
703 int ok;
704 struct mdinfo info;
705
706 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
707 dfd = dev_open(dn, O_RDONLY);
708 if (dfd < 0)
709 continue;
710 ok = st->ss->load_super(st, dfd, NULL);
711 close(dfd);
712 if (ok != 0)
713 continue;
714
715 info.array.raid_disks = raid_disks;
716 st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum);
717 if (info.disk.raid_disk == MD_DISK_ROLE_JOURNAL)
718 bestinfo->journal_clean = 1;
719 if (!avail) {
720 raid_disks = info.array.raid_disks;
721 avail = xcalloc(raid_disks, 1);
722 *availp = avail;
723
724 best = xcalloc(raid_disks, sizeof(int));
725 devmap = xcalloc(raid_disks, numdevs);
726
727 st->ss->getinfo_super(st, &info, devmap);
728 }
729
730 if (info.disk.state & (1<<MD_DISK_SYNC))
731 {
732 if (cnt == 0) {
733 cnt++;
734 max_events = info.events;
735 avail[info.disk.raid_disk] = 2;
736 best[info.disk.raid_disk] = devnum;
737 st->ss->getinfo_super(st, bestinfo, NULL);
738 } else if (info.events == max_events) {
739 avail[info.disk.raid_disk] = 2;
740 best[info.disk.raid_disk] = devnum;
741 } else if (info.events == max_events-1) {
742 if (avail[info.disk.raid_disk] == 0) {
743 avail[info.disk.raid_disk] = 1;
744 best[info.disk.raid_disk] = devnum;
745 }
746 } else if (info.events < max_events - 1)
747 ;
748 else if (info.events == max_events+1) {
749 int i;
750 max_events = info.events;
751 for (i = 0; i < raid_disks; i++)
752 if (avail[i])
753 avail[i]--;
754 avail[info.disk.raid_disk] = 2;
755 best[info.disk.raid_disk] = devnum;
756 st->ss->getinfo_super(st, bestinfo, NULL);
757 } else { /* info.events much bigger */
758 memset(avail, 0, raid_disks);
759 max_events = info.events;
760 avail[info.disk.raid_disk] = 2;
761 best[info.disk.raid_disk] = devnum;
762 st->ss->getinfo_super(st, bestinfo, NULL);
763 }
764 } else if (info.disk.state & (1<<MD_DISK_REPLACEMENT))
765 replcnt++;
766 st->ss->free_super(st);
767 }
768
769 if (!avail)
770 return 0;
771 /* We need to reject any device that thinks the best device is
772 * failed or missing */
773 for (b = 0; b < raid_disks; b++)
774 if (avail[b] == 2)
775 break;
776 cnt = 0;
777 for (i = 0 ; i < raid_disks ; i++) {
778 if (i != b && avail[i])
779 if (devmap[raid_disks * best[i] + b] == 0) {
780 /* This device thinks 'b' is failed -
781 * don't use it */
782 devnum = best[i];
783 for (d=sra->devs ; devnum; d = d->next)
784 devnum--;
785 d->disk.state |= (1 << MD_DISK_REMOVED);
786 avail[i] = 0;
787 }
788 if (avail[i])
789 cnt++;
790 }
791 /* Also need to reject any spare device with an event count that
792 * is too high
793 */
794 for (d = sra->devs; d; d = d->next) {
795 if (!(d->disk.state & (1<<MD_DISK_SYNC)) &&
796 d->events > max_events)
797 d->disk.state |= (1 << MD_DISK_REMOVED);
798 }
799 free(best);
800 free(devmap);
801 return cnt + replcnt;
802 }
803
804 /* test if container has degraded member(s) */
805 static int
806 container_members_max_degradation(struct map_ent *map, struct map_ent *me)
807 {
808 struct mdinfo *sra;
809 int degraded, max_degraded = 0;
810
811 for(; map; map = map->next) {
812 if (!metadata_container_matches(map->metadata, me->devnm))
813 continue;
814 /* most accurate information regarding array degradation */
815 sra = sysfs_read(-1, map->devnm,
816 GET_DISKS | GET_DEVS | GET_STATE);
817 if (!sra)
818 continue;
819 degraded = sra->array.raid_disks - sra->array.active_disks -
820 sra->array.spare_disks;
821 if (degraded > max_degraded)
822 max_degraded = degraded;
823 sysfs_free(sra);
824 }
825
826 return max_degraded;
827 }
828
829 static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
830 struct map_ent *target, int bare,
831 struct supertype *st, int verbose)
832 {
833 /* This device doesn't have any md metadata
834 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
835 * If 'st' is not set, then we only know that some metadata allows this,
836 * others possibly don't.
837 * So look for a container or array to attach the device to.
838 * Prefer 'target' if that is set and the array is found.
839 *
840 * If st is set, then only arrays of that type are considered
841 * Return 0 on success, or some exit code on failure, probably 1.
842 */
843 int rv = 1;
844 dev_t rdev;
845 struct map_ent *mp, *map = NULL;
846 struct mdinfo *chosen = NULL;
847 int dfd = *dfdp;
848
849 if (!fstat_is_blkdev(dfd, devname, &rdev))
850 return 1;
851
852 /*
853 * Now we need to find a suitable array to add this to.
854 * We only accept arrays that:
855 * - match 'st'
856 * - are in the same domains as the device
857 * - are of an size for which the device will be useful
858 * and we choose the one that is the most degraded
859 */
860
861 if (map_lock(&map)) {
862 pr_err("failed to get exclusive lock on mapfile\n");
863 return 1;
864 }
865 for (mp = map ; mp ; mp = mp->next) {
866 struct supertype *st2;
867 struct domainlist *dl = NULL;
868 struct mdinfo *sra;
869 unsigned long long devsize;
870 unsigned long long component_size = 0;
871
872 if (is_subarray(mp->metadata))
873 continue;
874 if (st) {
875 st2 = st->ss->match_metadata_desc(mp->metadata);
876 if (!st2 ||
877 (st->minor_version >= 0 &&
878 st->minor_version != st2->minor_version)) {
879 if (verbose > 1)
880 pr_err("not adding %s to %s as metadata type doesn't match\n",
881 devname, mp->path);
882 free(st2);
883 continue;
884 }
885 free(st2);
886 }
887 sra = sysfs_read(-1, mp->devnm,
888 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
889 GET_DEGRADED|GET_COMPONENT|GET_VERSION);
890 if (!sra) {
891 /* Probably a container - no degraded info */
892 sra = sysfs_read(-1, mp->devnm,
893 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
894 GET_COMPONENT|GET_VERSION);
895 if (sra)
896 sra->array.failed_disks = -1;
897 }
898 if (!sra)
899 continue;
900 if (st == NULL) {
901 int i;
902 st2 = NULL;
903 for(i = 0; !st2 && superlist[i]; i++)
904 st2 = superlist[i]->match_metadata_desc(
905 sra->text_version);
906 if (!st2) {
907 if (verbose > 1)
908 pr_err("not adding %s to %s as metadata not recognised.\n",
909 devname, mp->path);
910 goto next;
911 }
912 /* Need to double check the 'act_spare' permissions applies
913 * to this metadata.
914 */
915 if (!policy_action_allows(pol, st2->ss->name, act_spare))
916 goto next;
917 if (!bare && !policy_action_allows(pol, st2->ss->name,
918 act_spare_same_slot))
919 goto next;
920 } else
921 st2 = st;
922 /* update number of failed disks for mostly degraded
923 * container member */
924 if (sra->array.failed_disks == -1)
925 sra->array.failed_disks = container_members_max_degradation(map, mp);
926
927 get_dev_size(dfd, NULL, &devsize);
928 if (sra->component_size == 0) {
929 /* true for containers, here we must read superblock
930 * to obtain minimum spare size */
931 struct supertype *st3 = dup_super(st2);
932 int mdfd = open_dev(mp->devnm);
933 if (mdfd < 0) {
934 free(st3);
935 goto next;
936 }
937 if (st3->ss->load_container &&
938 !st3->ss->load_container(st3, mdfd, mp->path)) {
939 component_size = st3->ss->min_acceptable_spare_size(st3);
940 st3->ss->free_super(st3);
941 }
942 free(st3);
943 close(mdfd);
944 }
945 if ((sra->component_size > 0 &&
946 st2->ss->avail_size(st2, devsize,
947 sra->devs ? sra->devs->data_offset :
948 INVALID_SECTORS) <
949 sra->component_size) ||
950 (sra->component_size == 0 && devsize < component_size)) {
951 if (verbose > 1)
952 pr_err("not adding %s to %s as it is too small\n",
953 devname, mp->path);
954 goto next;
955 }
956 /* test against target.
957 * If 'target' is set and 'bare' is false, we only accept
958 * arrays/containers that match 'target'.
959 * If 'target' is set and 'bare' is true, we prefer the
960 * array which matches 'target'.
961 * target is considered only if we deal with degraded array
962 */
963 if (target && policy_action_allows(pol, st2->ss->name,
964 act_spare_same_slot)) {
965 if (strcmp(target->metadata, mp->metadata) == 0 &&
966 memcmp(target->uuid, mp->uuid,
967 sizeof(target->uuid)) == 0 &&
968 sra->array.failed_disks > 0) {
969 /* This is our target!! */
970 sysfs_free(chosen);
971 chosen = sra;
972 sra = NULL;
973 /* skip to end so we don't check any more */
974 while (mp->next)
975 mp = mp->next;
976 goto next;
977 }
978 /* not our target */
979 if (!bare)
980 goto next;
981 }
982
983 dl = domain_from_array(sra, st2->ss->name);
984 if (domain_test(dl, pol, st2->ss->name) != 1) {
985 /* domain test fails */
986 if (verbose > 1)
987 pr_err("not adding %s to %s as it is not in a compatible domain\n",
988 devname, mp->path);
989
990 goto next;
991 }
992 /* all tests passed, OK to add to this array */
993 if (!chosen) {
994 chosen = sra;
995 sra = NULL;
996 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
997 sysfs_free(chosen);
998 chosen = sra;
999 sra = NULL;
1000 }
1001 next:
1002 sysfs_free(sra);
1003 if (st != st2)
1004 free(st2);
1005 if (dl)
1006 domain_free(dl);
1007 }
1008 if (chosen) {
1009 /* add current device to chosen array as a spare */
1010 int mdfd = open_dev(chosen->sys_name);
1011 if (mdfd >= 0) {
1012 struct mddev_dev devlist;
1013 char chosen_devname[24]; // 2*11 for int (including signs) + colon + null
1014 devlist.next = NULL;
1015 devlist.used = 0;
1016 devlist.writemostly = FlagDefault;
1017 devlist.failfast = FlagDefault;
1018 devlist.devname = chosen_devname;
1019 sprintf(chosen_devname, "%d:%d", major(rdev),
1020 minor(rdev));
1021 devlist.disposition = 'a';
1022 close(dfd);
1023 *dfdp = -1;
1024 rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist,
1025 -1, 0, NULL, 0);
1026 close(mdfd);
1027 }
1028 if (verbose > 0) {
1029 if (rv == 0)
1030 pr_err("added %s as spare for %s\n",
1031 devname, chosen->sys_name);
1032 else
1033 pr_err("failed to add %s as spare for %s\n",
1034 devname, chosen->sys_name);
1035 }
1036 sysfs_free(chosen);
1037 }
1038 map_unlock(&map);
1039 return rv;
1040 }
1041
1042 static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1043 struct supertype *st, int verbose)
1044 {
1045 /* we know that at least one partition virtual-metadata is
1046 * allowed to incorporate spares like this device. We need to
1047 * find a suitable device to copy partition information from.
1048 *
1049 * Getting a list of all disk (not partition) devices is
1050 * slightly non-trivial. We could look at /sys/block, but
1051 * that is theoretically due to be removed. Maybe best to use
1052 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1053 * we depend on this directory of 'path' info. But that fails
1054 * to find loop devices and probably others. Maybe don't
1055 * worry about that, they aren't the real target.
1056 *
1057 * So: check things in /dev/disk/by-path to see if they are in
1058 * a compatible domain, then load the partition table and see
1059 * if it is OK for the new device, and choose the largest
1060 * partition table that fits.
1061 */
1062 DIR *dir;
1063 struct dirent *de;
1064 char *chosen = NULL;
1065 unsigned long long chosen_size = 0;
1066 struct supertype *chosen_st = NULL;
1067 int fd;
1068
1069 dir = opendir("/dev/disk/by-path");
1070 if (!dir)
1071 return 1;
1072 while ((de = readdir(dir)) != NULL) {
1073 char *ep;
1074 struct dev_policy *pol2 = NULL;
1075 struct domainlist *domlist = NULL;
1076 int fd = -1;
1077 struct mdinfo info;
1078 struct supertype *st2 = NULL;
1079 char *devname = NULL;
1080 unsigned long long devsectors;
1081
1082 if (de->d_ino == 0 || de->d_name[0] == '.' ||
1083 (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
1084 goto next;
1085
1086 ep = de->d_name + strlen(de->d_name);
1087 while (ep > de->d_name &&
1088 isdigit(ep[-1]))
1089 ep--;
1090 if (ep > de->d_name + 5 &&
1091 strncmp(ep-5, "-part", 5) == 0)
1092 /* This is a partition - skip it */
1093 goto next;
1094
1095 pol2 = path_policy(de->d_name, type_disk);
1096
1097 domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
1098 if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
1099 /* new device is incompatible with this device. */
1100 goto next;
1101
1102 domain_free(domlist);
1103 domlist = NULL;
1104
1105 if (asprintf(&devname, "/dev/disk/by-path/%s", de->d_name) != 1) {
1106 devname = NULL;
1107 goto next;
1108 }
1109 fd = open(devname, O_RDONLY);
1110 if (fd < 0)
1111 goto next;
1112 if (get_dev_size(fd, devname, &devsectors) == 0)
1113 goto next;
1114 devsectors >>= 9;
1115
1116 if (st)
1117 st2 = dup_super(st);
1118 else
1119 st2 = guess_super_type(fd, guess_partitions);
1120 if (st2 == NULL || st2->ss->load_super(st2, fd, NULL) < 0)
1121 goto next;
1122 st2->ignore_hw_compat = 0;
1123
1124 if (!st) {
1125 /* Check domain policy again, this time referring to metadata */
1126 domain_merge(&domlist, pol2, st2->ss->name);
1127 if (domain_test(domlist, pol, st2->ss->name) != 1)
1128 /* Incompatible devices for this metadata type */
1129 goto next;
1130 if (!policy_action_allows(pol, st2->ss->name, act_spare))
1131 /* Some partition types allow sparing, but not
1132 * this one.
1133 */
1134 goto next;
1135 }
1136
1137 st2->ss->getinfo_super(st2, &info, NULL);
1138 if (info.component_size > devsectors)
1139 /* This partitioning doesn't fit in the device */
1140 goto next;
1141
1142 /* This is an acceptable device to copy partition
1143 * metadata from. We could just stop here, but I
1144 * think I want to keep looking incase a larger
1145 * metadata which makes better use of the device can
1146 * be found.
1147 */
1148 if (chosen == NULL || chosen_size < info.component_size) {
1149 chosen_size = info.component_size;
1150 free(chosen);
1151 chosen = devname;
1152 devname = NULL;
1153 if (chosen_st) {
1154 chosen_st->ss->free_super(chosen_st);
1155 free(chosen_st);
1156 }
1157 chosen_st = st2;
1158 st2 = NULL;
1159 }
1160
1161 next:
1162 free(devname);
1163 domain_free(domlist);
1164 dev_policy_free(pol2);
1165 if (st2)
1166 st2->ss->free_super(st2);
1167 free(st2);
1168
1169 if (fd >= 0)
1170 close(fd);
1171 }
1172
1173 closedir(dir);
1174
1175 if (!chosen)
1176 return 1;
1177
1178 /* 'chosen' is the best device we can find. Let's write its
1179 * metadata to devname dfd is read-only so don't use that
1180 */
1181 fd = open(devname, O_RDWR);
1182 if (fd >= 0) {
1183 chosen_st->ss->store_super(chosen_st, fd);
1184 close(fd);
1185 }
1186 free(chosen);
1187 chosen_st->ss->free_super(chosen_st);
1188 free(chosen_st);
1189 return 0;
1190 }
1191
1192 static int is_bare(int dfd)
1193 {
1194 unsigned long long size = 0;
1195 char bufpad[4096 + 4096];
1196 char *buf = (char*)(((long)bufpad + 4096) & ~4095);
1197
1198 if (lseek(dfd, 0, SEEK_SET) != 0 ||
1199 read(dfd, buf, 4096) != 4096)
1200 return 0;
1201
1202 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1203 return 0;
1204 if (memcmp(buf, buf+1, 4095) != 0)
1205 return 0;
1206
1207 /* OK, first 4K appear blank, try the end. */
1208 get_dev_size(dfd, NULL, &size);
1209 if (lseek(dfd, size-4096, SEEK_SET) < 0 ||
1210 read(dfd, buf, 4096) != 4096)
1211 return 0;
1212
1213 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1214 return 0;
1215 if (memcmp(buf, buf+1, 4095) != 0)
1216 return 0;
1217
1218 return 1;
1219 }
1220
1221 /* adding a spare to a regular array is quite different from adding one to
1222 * a set-of-partitions virtual array.
1223 * This function determines which is worth trying and tries as appropriate.
1224 * Arrays are given priority over partitions.
1225 */
1226 static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1227 struct map_ent *target,
1228 struct supertype *st, int verbose)
1229 {
1230 int i;
1231 int rv;
1232 int arrays_ok = 0;
1233 int partitions_ok = 0;
1234 int dfd = *dfdp;
1235 int bare;
1236
1237 /* Can only add a spare if device has at least one domain */
1238 if (pol_find(pol, pol_domain) == NULL)
1239 return 1;
1240 /* And only if some action allows spares */
1241 if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare))
1242 return 1;
1243
1244 /* Now check if the device is bare.
1245 * bare devices can always be added as a spare
1246 * non-bare devices can only be added if spare-same-slot is permitted,
1247 * and this device is replacing a previous device - in which case 'target'
1248 * will be set.
1249 */
1250 if (!is_bare(dfd)) {
1251 /* Must have a target and allow same_slot */
1252 /* Later - may allow force_spare without target */
1253 if (!target ||
1254 !policy_action_allows(pol, st?st->ss->name:NULL,
1255 act_spare_same_slot)) {
1256 if (verbose > 1)
1257 pr_err("%s is not bare, so not considering as a spare\n",
1258 devname);
1259 return 1;
1260 }
1261 bare = 0;
1262 } else
1263 bare = 1;
1264
1265 /* It might be OK to add this device to an array - need to see
1266 * what arrays might be candidates.
1267 */
1268 if (st) {
1269 /* just try try 'array' or 'partition' based on this metadata */
1270 if (st->ss->add_to_super)
1271 return array_try_spare(devname, dfdp, pol, target, bare,
1272 st, verbose);
1273 else
1274 return partition_try_spare(devname, dfdp, pol,
1275 st, verbose);
1276 }
1277 /* No metadata was specified or found so options are open.
1278 * Check for whether any array metadata, or any partition metadata
1279 * might allow adding the spare. This check is just help to avoid
1280 * a more costly scan of all arrays when we can be sure that will
1281 * fail.
1282 */
1283 for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) {
1284 if (superlist[i]->add_to_super && !arrays_ok &&
1285 policy_action_allows(pol, superlist[i]->name, act_spare))
1286 arrays_ok = 1;
1287 if (superlist[i]->add_to_super == NULL && !partitions_ok &&
1288 policy_action_allows(pol, superlist[i]->name, act_spare))
1289 partitions_ok = 1;
1290 }
1291 rv = 1;
1292 if (arrays_ok)
1293 rv = array_try_spare(devname, dfdp, pol, target, bare,
1294 st, verbose);
1295 if (rv != 0 && partitions_ok)
1296 rv = partition_try_spare(devname, dfdp, pol, st, verbose);
1297 return rv;
1298 }
1299
1300 int IncrementalScan(struct context *c, char *devnm)
1301 {
1302 /* look at every device listed in the 'map' file.
1303 * If one is found that is not running then:
1304 * look in mdadm.conf for bitmap file.
1305 * if one exists, but array has none, add it.
1306 * try to start array in auto-readonly mode
1307 */
1308 struct map_ent *mapl = NULL;
1309 struct map_ent *me;
1310 struct mddev_ident *devs, *mddev;
1311 int rv = 0;
1312 char container[32];
1313 char *only = NULL;
1314
1315 map_read(&mapl);
1316 devs = conf_get_ident(NULL);
1317
1318 restart:
1319 for (me = mapl ; me ; me = me->next) {
1320 struct mdinfo *sra;
1321 int mdfd;
1322
1323 if (devnm && strcmp(devnm, me->devnm) != 0)
1324 continue;
1325 if (me->metadata[0] == '/') {
1326 char *sl;
1327
1328 if (!devnm)
1329 continue;
1330
1331 /* member array, need to work on container */
1332 strncpy(container, me->metadata+1, 32);
1333 container[31] = 0;
1334 sl = strchr(container, '/');
1335 if (sl)
1336 *sl = 0;
1337 only = devnm;
1338 devnm = container;
1339 goto restart;
1340 }
1341 mdfd = open_dev(me->devnm);
1342
1343 if (mdfd < 0)
1344 continue;
1345 if (!isdigit(me->metadata[0])) {
1346 /* must be a container */
1347 struct supertype *st = super_by_fd(mdfd, NULL);
1348 int ret = 0;
1349 struct map_ent *map = NULL;
1350
1351 if (st && st->ss->load_container)
1352 ret = st->ss->load_container(st, mdfd, NULL);
1353 close(mdfd);
1354 if (!ret && st && st->ss->container_content) {
1355 if (map_lock(&map))
1356 pr_err("failed to get exclusive lock on mapfile\n");
1357 ret = Incremental_container(st, me->path, c, only);
1358 map_unlock(&map);
1359 }
1360 if (ret)
1361 rv = 1;
1362 continue;
1363 }
1364 if (md_array_active(mdfd)) {
1365 close(mdfd);
1366 continue;
1367 }
1368 /* Ok, we can try this one. Maybe it needs a bitmap */
1369 for (mddev = devs ; mddev ; mddev = mddev->next)
1370 if (mddev->devname && me->path &&
1371 devname_matches(mddev->devname, me->path))
1372 break;
1373 if (mddev && mddev->bitmap_file) {
1374 /*
1375 * Note: early kernels will wrongly fail this, so it
1376 * is a hint only
1377 */
1378 int added = -1;
1379 int bmfd;
1380
1381 bmfd = open(mddev->bitmap_file, O_RDWR);
1382 if (bmfd >= 0) {
1383 added = ioctl(mdfd, SET_BITMAP_FILE, bmfd);
1384 close(bmfd);
1385 }
1386 if (c->verbose >= 0) {
1387 if (added == 0)
1388 pr_err("Added bitmap %s to %s\n",
1389 mddev->bitmap_file, me->path);
1390 else if (errno != EEXIST)
1391 pr_err("Failed to add bitmap to %s: %s\n",
1392 me->path, strerror(errno));
1393 }
1394 }
1395 /* FIXME check for reshape_active and consider not
1396 * starting array.
1397 */
1398 sra = sysfs_read(mdfd, NULL, 0);
1399 if (sra) {
1400 if (sysfs_set_str(sra, NULL,
1401 "array_state", "read-auto") == 0) {
1402 if (c->verbose >= 0)
1403 pr_err("started array %s\n",
1404 me->path ?: me->devnm);
1405 } else {
1406 pr_err("failed to start array %s: %s\n",
1407 me->path ?: me->devnm,
1408 strerror(errno));
1409 rv = 1;
1410 }
1411 sysfs_free(sra);
1412 }
1413 }
1414 return rv;
1415 }
1416
1417 static char *container2devname(char *devname)
1418 {
1419 char *mdname = NULL;
1420
1421 if (devname[0] == '/') {
1422 int fd = open(devname, O_RDONLY);
1423 if (fd >= 0) {
1424 mdname = xstrdup(fd2devnm(fd));
1425 close(fd);
1426 }
1427 } else {
1428 int uuid[4];
1429 struct map_ent *mp, *map = NULL;
1430
1431 if (!parse_uuid(devname, uuid))
1432 return mdname;
1433 mp = map_by_uuid(&map, uuid);
1434 if (mp)
1435 mdname = xstrdup(mp->devnm);
1436 map_free(map);
1437 }
1438
1439 return mdname;
1440 }
1441
1442 static int Incremental_container(struct supertype *st, char *devname,
1443 struct context *c, char *only)
1444 {
1445 /* Collect the contents of this container and for each
1446 * array, choose a device name and assemble the array.
1447 */
1448
1449 struct mdinfo *list;
1450 struct mdinfo *ra;
1451 struct map_ent *map = NULL;
1452 struct mdinfo info;
1453 int trustworthy;
1454 struct mddev_ident *match;
1455 int rv = 0;
1456 struct domainlist *domains;
1457 struct map_ent *smp;
1458 int suuid[4];
1459 int sfd;
1460 int ra_blocked = 0;
1461 int ra_all = 0;
1462 int result = 0;
1463
1464 st->ss->getinfo_super(st, &info, NULL);
1465
1466 if ((c->runstop > 0 && info.container_enough >= 0) ||
1467 info.container_enough > 0)
1468 /* pass */;
1469 else {
1470 if (c->export) {
1471 printf("MD_STARTED=no\n");
1472 } else if (c->verbose)
1473 pr_err("not enough devices to start the container\n");
1474 return 0;
1475 }
1476
1477 match = conf_match(st, &info, devname, c->verbose, &rv);
1478 if (match == NULL && rv == 2)
1479 return rv;
1480
1481 /* Need to compute 'trustworthy' */
1482 if (match)
1483 trustworthy = LOCAL;
1484 else if (st->ss->match_home(st, c->homehost) == 1)
1485 trustworthy = LOCAL;
1486 else if (st->ss->match_home(st, "any") == 1)
1487 trustworthy = LOCAL;
1488 else
1489 trustworthy = FOREIGN;
1490
1491 list = st->ss->container_content(st, NULL);
1492 /* when nothing to activate - quit */
1493 if (list == NULL) {
1494 if (c->export) {
1495 printf("MD_STARTED=nothing\n");
1496 }
1497 return 0;
1498 }
1499 for (ra = list ; ra ; ra = ra->next) {
1500 int mdfd;
1501 char chosen_name[1024];
1502 struct map_ent *mp;
1503 struct mddev_ident *match = NULL;
1504
1505 ra_all++;
1506 /* do not activate arrays blocked by metadata handler */
1507 if (ra->array.state & (1 << MD_SB_BLOCK_VOLUME)) {
1508 pr_err("Cannot activate array %s in %s.\n",
1509 ra->text_version, devname);
1510 ra_blocked++;
1511 continue;
1512 }
1513 mp = map_by_uuid(&map, ra->uuid);
1514
1515 if (mp) {
1516 mdfd = open_dev(mp->devnm);
1517 if (mp->path)
1518 strcpy(chosen_name, mp->path);
1519 else
1520 strcpy(chosen_name, mp->devnm);
1521 } else if (!only) {
1522
1523 /* Check in mdadm.conf for container == devname and
1524 * member == ra->text_version after second slash.
1525 */
1526 char *sub = strchr(ra->text_version+1, '/');
1527 struct mddev_ident *array_list;
1528 if (sub) {
1529 sub++;
1530 array_list = conf_get_ident(NULL);
1531 } else
1532 array_list = NULL;
1533 for(; array_list ; array_list = array_list->next) {
1534 char *dn;
1535 if (array_list->member == NULL ||
1536 array_list->container == NULL)
1537 continue;
1538 if (strcmp(array_list->member, sub) != 0)
1539 continue;
1540 if (array_list->uuid_set &&
1541 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1542 continue;
1543 dn = container2devname(array_list->container);
1544 if (dn == NULL)
1545 continue;
1546 if (strncmp(dn, ra->text_version+1,
1547 strlen(dn)) != 0 ||
1548 ra->text_version[strlen(dn)+1] != '/') {
1549 free(dn);
1550 continue;
1551 }
1552 free(dn);
1553 /* we have a match */
1554 match = array_list;
1555 if (c->verbose>0)
1556 pr_err("match found for member %s\n",
1557 array_list->member);
1558 break;
1559 }
1560
1561 if (match && match->devname &&
1562 strcasecmp(match->devname, "<ignore>") == 0) {
1563 if (c->verbose > 0)
1564 pr_err("array %s/%s is explicitly ignored by mdadm.conf\n",
1565 match->container, match->member);
1566 continue;
1567 }
1568 if (match)
1569 trustworthy = LOCAL;
1570
1571 mdfd = create_mddev(match ? match->devname : NULL,
1572 ra->name,
1573 c->autof,
1574 trustworthy,
1575 chosen_name, 0);
1576 }
1577 if (only && (!mp || strcmp(mp->devnm, only) != 0))
1578 continue;
1579
1580 if (mdfd < 0) {
1581 pr_err("failed to open %s: %s.\n",
1582 chosen_name, strerror(errno));
1583 return 2;
1584 }
1585
1586 assemble_container_content(st, mdfd, ra, c,
1587 chosen_name, &result);
1588 close(mdfd);
1589 }
1590 if (c->export && result) {
1591 char sep = '=';
1592 printf("MD_STARTED");
1593 if (result & INCR_NO) {
1594 printf("%cno", sep);
1595 sep = ',';
1596 }
1597 if (result & INCR_UNSAFE) {
1598 printf("%cunsafe", sep);
1599 sep = ',';
1600 }
1601 if (result & INCR_ALREADY) {
1602 printf("%calready", sep);
1603 sep = ',';
1604 }
1605 if (result & INCR_YES) {
1606 printf("%cyes", sep);
1607 sep = ',';
1608 }
1609 printf("\n");
1610 }
1611
1612 /* don't move spares to container with volume being activated
1613 when all volumes are blocked */
1614 if (ra_all == ra_blocked)
1615 return 0;
1616
1617 /* Now move all suitable spares from spare container */
1618 domains = domain_from_array(list, st->ss->name);
1619 memcpy(suuid, uuid_zero, sizeof(int[4]));
1620 if (domains &&
1621 (smp = map_by_uuid(&map, suuid)) != NULL &&
1622 (sfd = open(smp->path, O_RDONLY)) >= 0) {
1623 /* spare container found */
1624 struct supertype *sst =
1625 super_imsm.match_metadata_desc("imsm");
1626 struct mdinfo *sinfo;
1627 unsigned long long min_size = 0;
1628 if (st->ss->min_acceptable_spare_size)
1629 min_size = st->ss->min_acceptable_spare_size(st);
1630 if (!sst->ss->load_container(sst, sfd, NULL)) {
1631 close(sfd);
1632 sinfo = container_choose_spares(sst, min_size,
1633 domains, NULL,
1634 st->ss->name, 0);
1635 sst->ss->free_super(sst);
1636 if (sinfo){
1637 int count = 0;
1638 struct mdinfo *disks = sinfo->devs;
1639 while (disks) {
1640 /* move spare from spare
1641 * container to currently
1642 * assembled one
1643 */
1644 if (move_spare(
1645 smp->path,
1646 devname,
1647 makedev(disks->disk.major,
1648 disks->disk.minor)))
1649 count++;
1650 disks = disks->next;
1651 }
1652 if (count)
1653 pr_err("Added %d spare%s to %s\n",
1654 count, count>1?"s":"", devname);
1655 }
1656 sysfs_free(sinfo);
1657 } else
1658 close(sfd);
1659 }
1660 domain_free(domains);
1661 return 0;
1662 }
1663
1664 static void run_udisks(char *arg1, char *arg2)
1665 {
1666 int pid = fork();
1667 int status;
1668 if (pid == 0) {
1669 execl("/usr/bin/udisks", "udisks", arg1, arg2, NULL);
1670 execl("/bin/udisks", "udisks", arg1, arg2, NULL);
1671 exit(1);
1672 }
1673 while (pid > 0 && wait(&status) != pid)
1674 ;
1675 }
1676
1677 /*
1678 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1679 * raid arrays, and if so first fail (if needed) and then remove the device.
1680 *
1681 * @devname - The device we want to remove
1682 * @id_path - name as found in /dev/disk/by-path for this device
1683 *
1684 * Note: the device name must be a kernel name like "sda", so
1685 * that we can find it in /proc/mdstat
1686 */
1687 int IncrementalRemove(char *devname, char *id_path, int verbose)
1688 {
1689 int mdfd;
1690 int rv = 0;
1691 struct mdstat_ent *ent;
1692 struct mddev_dev devlist;
1693 struct mdinfo mdi;
1694 char buf[32];
1695
1696 if (!id_path)
1697 dprintf("incremental removal without --path <id_path> lacks the possibility to re-add new device in this port\n");
1698
1699 if (strchr(devname, '/')) {
1700 pr_err("incremental removal requires a kernel device name, not a file: %s\n", devname);
1701 return 1;
1702 }
1703 ent = mdstat_by_component(devname);
1704 if (!ent) {
1705 if (verbose >= 0)
1706 pr_err("%s does not appear to be a component of any array\n", devname);
1707 return 1;
1708 }
1709 if (sysfs_init(&mdi, -1, ent->devnm)) {
1710 pr_err("unable to initialize sysfs for: %s\n", devname);
1711 return 1;
1712 }
1713 mdfd = open_dev_excl(ent->devnm);
1714 if (mdfd > 0) {
1715 close(mdfd);
1716 if (sysfs_get_str(&mdi, NULL, "array_state",
1717 buf, sizeof(buf)) > 0) {
1718 if (strncmp(buf, "active", 6) == 0 ||
1719 strncmp(buf, "clean", 5) == 0)
1720 sysfs_set_str(&mdi, NULL,
1721 "array_state", "read-auto");
1722 }
1723 }
1724 mdfd = open_dev(ent->devnm);
1725 if (mdfd < 0) {
1726 if (verbose >= 0)
1727 pr_err("Cannot open array %s!!\n", ent->devnm);
1728 free_mdstat(ent);
1729 return 1;
1730 }
1731
1732 if (id_path) {
1733 struct map_ent *map = NULL, *me;
1734 me = map_by_devnm(&map, ent->devnm);
1735 if (me)
1736 policy_save_path(id_path, me);
1737 map_free(map);
1738 }
1739
1740 memset(&devlist, 0, sizeof(devlist));
1741 devlist.devname = devname;
1742 devlist.disposition = 'f';
1743 /* for a container, we must fail each member array */
1744 if (ent->metadata_version &&
1745 strncmp(ent->metadata_version, "external:", 9) == 0) {
1746 struct mdstat_ent *mdstat = mdstat_read(0, 0);
1747 struct mdstat_ent *memb;
1748 for (memb = mdstat ; memb ; memb = memb->next)
1749 if (is_container_member(memb, ent->devnm)) {
1750 int subfd = open_dev(memb->devnm);
1751 if (subfd >= 0) {
1752 rv |= Manage_subdevs(
1753 memb->devnm, subfd,
1754 &devlist, verbose, 0,
1755 NULL, 0);
1756 close(subfd);
1757 }
1758 }
1759 free_mdstat(mdstat);
1760 } else
1761 rv |= Manage_subdevs(ent->devnm, mdfd, &devlist,
1762 verbose, 0, NULL, 0);
1763 if (rv & 2) {
1764 /* Failed due to EBUSY, try to stop the array.
1765 * Give udisks a chance to unmount it first.
1766 */
1767 int devid = devnm2devid(ent->devnm);
1768 run_udisks("--unmount", map_dev(major(devid),minor(devid), 0));
1769 rv = Manage_stop(ent->devnm, mdfd, verbose, 1);
1770 if (rv)
1771 /* At least we can try to trigger a 'remove' */
1772 sysfs_uevent(&mdi, "remove");
1773 if (verbose) {
1774 if (rv)
1775 pr_err("Fail to stop %s too.\n", ent->devnm);
1776 }
1777 } else {
1778 devlist.disposition = 'r';
1779 rv = Manage_subdevs(ent->devnm, mdfd, &devlist,
1780 verbose, 0, NULL, 0);
1781 }
1782 close(mdfd);
1783 free_mdstat(ent);
1784 return rv;
1785 }