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