]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
Coverity: Resource leak: close fd before return
[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 __u64 max_journal_events = 0;
688 char *avail = NULL;
689 int *best = NULL;
690 char *devmap = NULL;
691 int numdevs = 0;
692 int devnum;
693 int b, i;
694 int raid_disks = 0;
695
696 if (!sra)
697 return 0;
698
699 for (d = sra->devs ; d ; d = d->next)
700 numdevs++;
701 for (d = sra->devs, devnum = 0 ; d ; d = d->next, devnum++) {
702 char dn[30];
703 int dfd;
704 int ok;
705 struct mdinfo info;
706
707 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
708 dfd = dev_open(dn, O_RDONLY);
709 if (dfd < 0)
710 continue;
711 ok = st->ss->load_super(st, dfd, NULL);
712 close(dfd);
713 if (ok != 0)
714 continue;
715
716 info.array.raid_disks = raid_disks;
717 st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum);
718 if (info.disk.raid_disk == MD_DISK_ROLE_JOURNAL &&
719 info.events > max_journal_events)
720 max_journal_events = info.events;
721 if (!avail) {
722 raid_disks = info.array.raid_disks;
723 avail = xcalloc(raid_disks, 1);
724 *availp = avail;
725
726 best = xcalloc(raid_disks, sizeof(int));
727 devmap = xcalloc(raid_disks, numdevs);
728
729 st->ss->getinfo_super(st, &info, devmap);
730 }
731
732 if (info.disk.state & (1<<MD_DISK_SYNC))
733 {
734 if (cnt == 0) {
735 cnt++;
736 max_events = info.events;
737 avail[info.disk.raid_disk] = 2;
738 best[info.disk.raid_disk] = devnum;
739 st->ss->getinfo_super(st, bestinfo, NULL);
740 } else if (info.events == max_events) {
741 avail[info.disk.raid_disk] = 2;
742 best[info.disk.raid_disk] = devnum;
743 } else if (info.events == max_events-1) {
744 if (avail[info.disk.raid_disk] == 0) {
745 avail[info.disk.raid_disk] = 1;
746 best[info.disk.raid_disk] = devnum;
747 }
748 } else if (info.events < max_events - 1)
749 ;
750 else if (info.events == max_events+1) {
751 int i;
752 max_events = info.events;
753 for (i = 0; i < raid_disks; i++)
754 if (avail[i])
755 avail[i]--;
756 avail[info.disk.raid_disk] = 2;
757 best[info.disk.raid_disk] = devnum;
758 st->ss->getinfo_super(st, bestinfo, NULL);
759 } else { /* info.events much bigger */
760 memset(avail, 0, raid_disks);
761 max_events = info.events;
762 avail[info.disk.raid_disk] = 2;
763 best[info.disk.raid_disk] = devnum;
764 st->ss->getinfo_super(st, bestinfo, NULL);
765 }
766 } else if (info.disk.state & (1<<MD_DISK_REPLACEMENT))
767 replcnt++;
768 st->ss->free_super(st);
769 }
770 if (max_journal_events >= max_events - 1)
771 bestinfo->journal_clean = 1;
772
773 if (!avail)
774 return 0;
775 /* We need to reject any device that thinks the best device is
776 * failed or missing */
777 for (b = 0; b < raid_disks; b++)
778 if (avail[b] == 2)
779 break;
780 cnt = 0;
781 for (i = 0 ; i < raid_disks ; i++) {
782 if (i != b && avail[i])
783 if (devmap[raid_disks * best[i] + b] == 0) {
784 /* This device thinks 'b' is failed -
785 * don't use it */
786 devnum = best[i];
787 for (d=sra->devs ; devnum; d = d->next)
788 devnum--;
789 d->disk.state |= (1 << MD_DISK_REMOVED);
790 avail[i] = 0;
791 }
792 if (avail[i])
793 cnt++;
794 }
795 /* Also need to reject any spare device with an event count that
796 * is too high
797 */
798 for (d = sra->devs; d; d = d->next) {
799 if (!(d->disk.state & (1<<MD_DISK_SYNC)) &&
800 d->events > max_events)
801 d->disk.state |= (1 << MD_DISK_REMOVED);
802 }
803 free(best);
804 free(devmap);
805 return cnt + replcnt;
806 }
807
808 /* test if container has degraded member(s) */
809 static int
810 container_members_max_degradation(struct map_ent *map, struct map_ent *me)
811 {
812 struct mdinfo *sra;
813 int degraded, max_degraded = 0;
814
815 for(; map; map = map->next) {
816 if (!metadata_container_matches(map->metadata, me->devnm))
817 continue;
818 /* most accurate information regarding array degradation */
819 sra = sysfs_read(-1, map->devnm,
820 GET_DISKS | GET_DEVS | GET_STATE);
821 if (!sra)
822 continue;
823 degraded = sra->array.raid_disks - sra->array.active_disks -
824 sra->array.spare_disks;
825 if (degraded > max_degraded)
826 max_degraded = degraded;
827 sysfs_free(sra);
828 }
829
830 return max_degraded;
831 }
832
833 static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
834 struct map_ent *target, int bare,
835 struct supertype *st, int verbose)
836 {
837 /* This device doesn't have any md metadata
838 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
839 * If 'st' is not set, then we only know that some metadata allows this,
840 * others possibly don't.
841 * So look for a container or array to attach the device to.
842 * Prefer 'target' if that is set and the array is found.
843 *
844 * If st is set, then only arrays of that type are considered
845 * Return 0 on success, or some exit code on failure, probably 1.
846 */
847 int rv = 1;
848 dev_t rdev;
849 struct map_ent *mp, *map = NULL;
850 struct mdinfo *chosen = NULL;
851 int dfd = *dfdp;
852
853 if (!fstat_is_blkdev(dfd, devname, &rdev))
854 return 1;
855
856 /*
857 * Now we need to find a suitable array to add this to.
858 * We only accept arrays that:
859 * - match 'st'
860 * - are in the same domains as the device
861 * - are of an size for which the device will be useful
862 * and we choose the one that is the most degraded
863 */
864
865 if (map_lock(&map)) {
866 pr_err("failed to get exclusive lock on mapfile\n");
867 return 1;
868 }
869 for (mp = map ; mp ; mp = mp->next) {
870 struct supertype *st2;
871 struct domainlist *dl = NULL;
872 struct mdinfo *sra;
873 unsigned long long devsize, freesize = 0;
874 struct spare_criteria sc = {0, 0};
875
876 if (is_subarray(mp->metadata))
877 continue;
878 if (st) {
879 st2 = st->ss->match_metadata_desc(mp->metadata);
880 if (!st2 ||
881 (st->minor_version >= 0 &&
882 st->minor_version != st2->minor_version)) {
883 if (verbose > 1)
884 pr_err("not adding %s to %s as metadata type doesn't match\n",
885 devname, mp->path);
886 free(st2);
887 continue;
888 }
889 free(st2);
890 }
891 sra = sysfs_read(-1, mp->devnm,
892 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
893 GET_COMPONENT|GET_VERSION);
894 if (sra)
895 sra->array.failed_disks = -1;
896 else
897 continue;
898 if (st == NULL) {
899 int i;
900 st2 = NULL;
901 for(i = 0; !st2 && superlist[i]; i++)
902 st2 = superlist[i]->match_metadata_desc(
903 sra->text_version);
904 if (!st2) {
905 if (verbose > 1)
906 pr_err("not adding %s to %s as metadata not recognised.\n",
907 devname, mp->path);
908 goto next;
909 }
910 /* Need to double check the 'act_spare' permissions applies
911 * to this metadata.
912 */
913 if (!policy_action_allows(pol, st2->ss->name, act_spare))
914 goto next;
915 if (!bare && !policy_action_allows(pol, st2->ss->name,
916 act_spare_same_slot))
917 goto next;
918 } else
919 st2 = st;
920 /* update number of failed disks for mostly degraded
921 * container member */
922 if (sra->array.failed_disks == -1)
923 sra->array.failed_disks = container_members_max_degradation(map, mp);
924
925 get_dev_size(dfd, NULL, &devsize);
926 if (sra->component_size == 0) {
927 /* true for containers, here we must read superblock
928 * to obtain minimum spare size */
929 struct supertype *st3 = dup_super(st2);
930 int mdfd = open_dev(mp->devnm);
931 if (mdfd < 0) {
932 free(st3);
933 goto next;
934 }
935 if (st3->ss->load_container &&
936 !st3->ss->load_container(st3, mdfd, mp->path)) {
937 if (st3->ss->get_spare_criteria)
938 st3->ss->get_spare_criteria(st3, &sc);
939 st3->ss->free_super(st3);
940 }
941 free(st3);
942 close(mdfd);
943 }
944 if ((sra->component_size > 0 &&
945 st2->ss->validate_geometry(st2, sra->array.level, sra->array.layout,
946 sra->array.raid_disks, &sra->array.chunk_size,
947 sra->component_size,
948 sra->devs ? sra->devs->data_offset : INVALID_SECTORS,
949 devname, &freesize, sra->consistency_policy,
950 0) &&
951 freesize < sra->component_size) ||
952 (sra->component_size == 0 && devsize < sc.min_size)) {
953 if (verbose > 1)
954 pr_err("not adding %s to %s as it is too small\n",
955 devname, mp->path);
956 goto next;
957 }
958 /* test against target.
959 * If 'target' is set and 'bare' is false, we only accept
960 * arrays/containers that match 'target'.
961 * If 'target' is set and 'bare' is true, we prefer the
962 * array which matches 'target'.
963 * target is considered only if we deal with degraded array
964 */
965 if (target && policy_action_allows(pol, st2->ss->name,
966 act_spare_same_slot)) {
967 if (strcmp(target->metadata, mp->metadata) == 0 &&
968 memcmp(target->uuid, mp->uuid,
969 sizeof(target->uuid)) == 0 &&
970 sra->array.failed_disks > 0) {
971 /* This is our target!! */
972 sysfs_free(chosen);
973 chosen = sra;
974 sra = NULL;
975 /* skip to end so we don't check any more */
976 while (mp->next)
977 mp = mp->next;
978 goto next;
979 }
980 /* not our target */
981 if (!bare)
982 goto next;
983 }
984
985 dl = domain_from_array(sra, st2->ss->name);
986 if (domain_test(dl, pol, st2->ss->name) != 1) {
987 /* domain test fails */
988 if (verbose > 1)
989 pr_err("not adding %s to %s as it is not in a compatible domain\n",
990 devname, mp->path);
991
992 goto next;
993 }
994 /* all tests passed, OK to add to this array */
995 if (!chosen) {
996 chosen = sra;
997 sra = NULL;
998 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
999 sysfs_free(chosen);
1000 chosen = sra;
1001 sra = NULL;
1002 }
1003 next:
1004 sysfs_free(sra);
1005 if (st != st2)
1006 free(st2);
1007 if (dl)
1008 domain_free(dl);
1009 }
1010 if (chosen) {
1011 /* add current device to chosen array as a spare */
1012 int mdfd = open_dev(chosen->sys_name);
1013 if (mdfd >= 0) {
1014 struct mddev_dev devlist;
1015 char chosen_devname[24]; // 2*11 for int (including signs) + colon + null
1016 devlist.next = NULL;
1017 devlist.used = 0;
1018 devlist.writemostly = FlagDefault;
1019 devlist.failfast = FlagDefault;
1020 devlist.devname = chosen_devname;
1021 sprintf(chosen_devname, "%d:%d", major(rdev),
1022 minor(rdev));
1023 devlist.disposition = 'a';
1024 close(dfd);
1025 *dfdp = -1;
1026 rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist,
1027 -1, 0, NULL, 0);
1028 close(mdfd);
1029 }
1030 if (verbose > 0) {
1031 if (rv == 0)
1032 pr_err("added %s as spare for %s\n",
1033 devname, chosen->sys_name);
1034 else
1035 pr_err("failed to add %s as spare for %s\n",
1036 devname, chosen->sys_name);
1037 }
1038 sysfs_free(chosen);
1039 }
1040 map_unlock(&map);
1041 return rv;
1042 }
1043
1044 static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1045 struct supertype *st, int verbose)
1046 {
1047 /* we know that at least one partition virtual-metadata is
1048 * allowed to incorporate spares like this device. We need to
1049 * find a suitable device to copy partition information from.
1050 *
1051 * Getting a list of all disk (not partition) devices is
1052 * slightly non-trivial. We could look at /sys/block, but
1053 * that is theoretically due to be removed. Maybe best to use
1054 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1055 * we depend on this directory of 'path' info. But that fails
1056 * to find loop devices and probably others. Maybe don't
1057 * worry about that, they aren't the real target.
1058 *
1059 * So: check things in /dev/disk/by-path to see if they are in
1060 * a compatible domain, then load the partition table and see
1061 * if it is OK for the new device, and choose the largest
1062 * partition table that fits.
1063 */
1064 DIR *dir;
1065 struct dirent *de;
1066 char *chosen = NULL;
1067 unsigned long long chosen_size = 0;
1068 struct supertype *chosen_st = NULL;
1069 int fd;
1070
1071 dir = opendir("/dev/disk/by-path");
1072 if (!dir)
1073 return 1;
1074 while ((de = readdir(dir)) != NULL) {
1075 char *ep;
1076 struct dev_policy *pol2 = NULL;
1077 struct domainlist *domlist = NULL;
1078 int fd = -1;
1079 struct mdinfo info;
1080 struct supertype *st2 = NULL;
1081 char *devname = NULL;
1082 unsigned long long devsectors;
1083
1084 if (de->d_ino == 0 || de->d_name[0] == '.' ||
1085 (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
1086 goto next;
1087
1088 ep = de->d_name + strlen(de->d_name);
1089 while (ep > de->d_name &&
1090 isdigit(ep[-1]))
1091 ep--;
1092 if (ep > de->d_name + 5 &&
1093 strncmp(ep-5, "-part", 5) == 0)
1094 /* This is a partition - skip it */
1095 goto next;
1096
1097 pol2 = path_policy(de->d_name, type_disk);
1098
1099 domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
1100 if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
1101 /* new device is incompatible with this device. */
1102 goto next;
1103
1104 domain_free(domlist);
1105 domlist = NULL;
1106
1107 if (asprintf(&devname, "/dev/disk/by-path/%s", de->d_name) != 1) {
1108 devname = NULL;
1109 goto next;
1110 }
1111 fd = open(devname, O_RDONLY);
1112 if (fd < 0)
1113 goto next;
1114 if (get_dev_size(fd, devname, &devsectors) == 0)
1115 goto next;
1116 devsectors >>= 9;
1117
1118 if (st)
1119 st2 = dup_super(st);
1120 else
1121 st2 = guess_super_type(fd, guess_partitions);
1122 if (st2 == NULL || st2->ss->load_super(st2, fd, NULL) < 0)
1123 goto next;
1124 st2->ignore_hw_compat = 0;
1125
1126 if (!st) {
1127 /* Check domain policy again, this time referring to metadata */
1128 domain_merge(&domlist, pol2, st2->ss->name);
1129 if (domain_test(domlist, pol, st2->ss->name) != 1)
1130 /* Incompatible devices for this metadata type */
1131 goto next;
1132 if (!policy_action_allows(pol, st2->ss->name, act_spare))
1133 /* Some partition types allow sparing, but not
1134 * this one.
1135 */
1136 goto next;
1137 }
1138
1139 st2->ss->getinfo_super(st2, &info, NULL);
1140 if (info.component_size > devsectors)
1141 /* This partitioning doesn't fit in the device */
1142 goto next;
1143
1144 /* This is an acceptable device to copy partition
1145 * metadata from. We could just stop here, but I
1146 * think I want to keep looking incase a larger
1147 * metadata which makes better use of the device can
1148 * be found.
1149 */
1150 if (chosen == NULL || chosen_size < info.component_size) {
1151 chosen_size = info.component_size;
1152 free(chosen);
1153 chosen = devname;
1154 devname = NULL;
1155 if (chosen_st) {
1156 chosen_st->ss->free_super(chosen_st);
1157 free(chosen_st);
1158 }
1159 chosen_st = st2;
1160 st2 = NULL;
1161 }
1162
1163 next:
1164 free(devname);
1165 domain_free(domlist);
1166 dev_policy_free(pol2);
1167 if (st2)
1168 st2->ss->free_super(st2);
1169 free(st2);
1170
1171 if (fd >= 0)
1172 close(fd);
1173 }
1174
1175 closedir(dir);
1176
1177 if (!chosen)
1178 return 1;
1179
1180 /* 'chosen' is the best device we can find. Let's write its
1181 * metadata to devname dfd is read-only so don't use that
1182 */
1183 fd = open(devname, O_RDWR);
1184 if (fd >= 0) {
1185 chosen_st->ss->store_super(chosen_st, fd);
1186 close(fd);
1187 }
1188 free(chosen);
1189 chosen_st->ss->free_super(chosen_st);
1190 free(chosen_st);
1191 return 0;
1192 }
1193
1194 static int is_bare(int dfd)
1195 {
1196 unsigned long long size = 0;
1197 char bufpad[4096 + 4096];
1198 char *buf = (char*)(((long)bufpad + 4096) & ~4095);
1199
1200 if (lseek(dfd, 0, SEEK_SET) != 0 ||
1201 read(dfd, buf, 4096) != 4096)
1202 return 0;
1203
1204 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1205 return 0;
1206 if (memcmp(buf, buf+1, 4095) != 0)
1207 return 0;
1208
1209 /* OK, first 4K appear blank, try the end. */
1210 get_dev_size(dfd, NULL, &size);
1211 if (lseek(dfd, size-4096, SEEK_SET) < 0 ||
1212 read(dfd, buf, 4096) != 4096)
1213 return 0;
1214
1215 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1216 return 0;
1217 if (memcmp(buf, buf+1, 4095) != 0)
1218 return 0;
1219
1220 return 1;
1221 }
1222
1223 /* adding a spare to a regular array is quite different from adding one to
1224 * a set-of-partitions virtual array.
1225 * This function determines which is worth trying and tries as appropriate.
1226 * Arrays are given priority over partitions.
1227 */
1228 static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1229 struct map_ent *target,
1230 struct supertype *st, int verbose)
1231 {
1232 int i;
1233 int rv;
1234 int arrays_ok = 0;
1235 int partitions_ok = 0;
1236 int dfd = *dfdp;
1237 int bare;
1238
1239 /* Can only add a spare if device has at least one domain */
1240 if (pol_find(pol, pol_domain) == NULL)
1241 return 1;
1242 /* And only if some action allows spares */
1243 if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare))
1244 return 1;
1245
1246 /* Now check if the device is bare.
1247 * bare devices can always be added as a spare
1248 * non-bare devices can only be added if spare-same-slot is permitted,
1249 * and this device is replacing a previous device - in which case 'target'
1250 * will be set.
1251 */
1252 if (!is_bare(dfd)) {
1253 /* Must have a target and allow same_slot */
1254 /* Later - may allow force_spare without target */
1255 if (!target ||
1256 !policy_action_allows(pol, st?st->ss->name:NULL,
1257 act_spare_same_slot)) {
1258 if (verbose > 1)
1259 pr_err("%s is not bare, so not considering as a spare\n",
1260 devname);
1261 return 1;
1262 }
1263 bare = 0;
1264 } else
1265 bare = 1;
1266
1267 /* It might be OK to add this device to an array - need to see
1268 * what arrays might be candidates.
1269 */
1270 if (st) {
1271 /* just try to add 'array' or 'partition' based on this metadata */
1272 if (st->ss->add_to_super)
1273 return array_try_spare(devname, dfdp, pol, target, bare,
1274 st, verbose);
1275 else
1276 return partition_try_spare(devname, dfdp, pol,
1277 st, verbose);
1278 }
1279 /* No metadata was specified or found so options are open.
1280 * Check for whether any array metadata, or any partition metadata
1281 * might allow adding the spare. This check is just help to avoid
1282 * a more costly scan of all arrays when we can be sure that will
1283 * fail.
1284 */
1285 for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) {
1286 if (superlist[i]->add_to_super && !arrays_ok &&
1287 policy_action_allows(pol, superlist[i]->name, act_spare))
1288 arrays_ok = 1;
1289 if (superlist[i]->add_to_super == NULL && !partitions_ok &&
1290 policy_action_allows(pol, superlist[i]->name, act_spare))
1291 partitions_ok = 1;
1292 }
1293 rv = 1;
1294 if (arrays_ok)
1295 rv = array_try_spare(devname, dfdp, pol, target, bare,
1296 st, verbose);
1297 if (rv != 0 && partitions_ok)
1298 rv = partition_try_spare(devname, dfdp, pol, st, verbose);
1299 return rv;
1300 }
1301
1302 int IncrementalScan(struct context *c, char *devnm)
1303 {
1304 /* look at every device listed in the 'map' file.
1305 * If one is found that is not running then:
1306 * look in mdadm.conf for bitmap file.
1307 * if one exists, but array has none, add it.
1308 * try to start array in auto-readonly mode
1309 */
1310 struct map_ent *mapl = NULL;
1311 struct map_ent *me;
1312 struct mddev_ident *devs, *mddev;
1313 int rv = 0;
1314 char container[32];
1315 char *only = NULL;
1316
1317 map_read(&mapl);
1318 devs = conf_get_ident(NULL);
1319
1320 restart:
1321 for (me = mapl ; me ; me = me->next) {
1322 struct mdinfo *sra;
1323 int mdfd;
1324
1325 if (devnm && strcmp(devnm, me->devnm) != 0)
1326 continue;
1327 if (me->metadata[0] == '/') {
1328 char *sl;
1329
1330 if (!devnm)
1331 continue;
1332
1333 /* member array, need to work on container */
1334 strncpy(container, me->metadata+1, 32);
1335 container[31] = 0;
1336 sl = strchr(container, '/');
1337 if (sl)
1338 *sl = 0;
1339 only = devnm;
1340 devnm = container;
1341 goto restart;
1342 }
1343 mdfd = open_dev(me->devnm);
1344
1345 if (mdfd < 0)
1346 continue;
1347 if (!isdigit(me->metadata[0])) {
1348 /* must be a container */
1349 struct supertype *st = super_by_fd(mdfd, NULL);
1350 int ret = 0;
1351 struct map_ent *map = NULL;
1352
1353 if (st && st->ss->load_container)
1354 ret = st->ss->load_container(st, mdfd, NULL);
1355 close(mdfd);
1356 if (!ret && st && st->ss->container_content) {
1357 if (map_lock(&map))
1358 pr_err("failed to get exclusive lock on mapfile\n");
1359 ret = Incremental_container(st, me->path, c, only);
1360 map_unlock(&map);
1361 }
1362 if (ret)
1363 rv = 1;
1364 continue;
1365 }
1366 if (md_array_active(mdfd)) {
1367 close(mdfd);
1368 continue;
1369 }
1370 /* Ok, we can try this one. Maybe it needs a bitmap */
1371 for (mddev = devs ; mddev ; mddev = mddev->next)
1372 if (mddev->devname && me->path &&
1373 devname_matches(mddev->devname, me->path))
1374 break;
1375 if (mddev && mddev->bitmap_file) {
1376 /*
1377 * Note: early kernels will wrongly fail this, so it
1378 * is a hint only
1379 */
1380 int added = -1;
1381 int bmfd;
1382
1383 bmfd = open(mddev->bitmap_file, O_RDWR);
1384 if (bmfd >= 0) {
1385 added = ioctl(mdfd, SET_BITMAP_FILE, bmfd);
1386 close(bmfd);
1387 }
1388 if (c->verbose >= 0) {
1389 if (added == 0)
1390 pr_err("Added bitmap %s to %s\n",
1391 mddev->bitmap_file, me->path);
1392 else if (errno != EEXIST)
1393 pr_err("Failed to add bitmap to %s: %s\n",
1394 me->path, strerror(errno));
1395 }
1396 }
1397 /* FIXME check for reshape_active and consider not
1398 * starting array.
1399 */
1400 sra = sysfs_read(mdfd, NULL, 0);
1401 if (sra) {
1402 if (sysfs_set_str(sra, NULL,
1403 "array_state", "read-auto") == 0) {
1404 if (c->verbose >= 0)
1405 pr_err("started array %s\n",
1406 me->path ?: me->devnm);
1407 } else {
1408 pr_err("failed to start array %s: %s\n",
1409 me->path ?: me->devnm,
1410 strerror(errno));
1411 rv = 1;
1412 }
1413 sysfs_free(sra);
1414 }
1415 }
1416 map_free(mapl);
1417 return rv;
1418 }
1419
1420 static char *container2devname(char *devname)
1421 {
1422 char *mdname = NULL;
1423
1424 if (devname[0] == '/') {
1425 int fd = open(devname, O_RDONLY);
1426 if (fd >= 0) {
1427 mdname = xstrdup(fd2devnm(fd));
1428 close(fd);
1429 }
1430 } else {
1431 int uuid[4];
1432 struct map_ent *mp, *map = NULL;
1433
1434 if (!parse_uuid(devname, uuid))
1435 return mdname;
1436 mp = map_by_uuid(&map, uuid);
1437 if (mp)
1438 mdname = xstrdup(mp->devnm);
1439 map_free(map);
1440 }
1441
1442 return mdname;
1443 }
1444
1445 static int Incremental_container(struct supertype *st, char *devname,
1446 struct context *c, char *only)
1447 {
1448 /* Collect the contents of this container and for each
1449 * array, choose a device name and assemble the array.
1450 */
1451
1452 struct mdinfo *list;
1453 struct mdinfo *ra;
1454 struct map_ent *map = NULL;
1455 struct mdinfo info;
1456 int trustworthy;
1457 struct mddev_ident *match;
1458 int rv = 0;
1459 struct domainlist *domains;
1460 struct map_ent *smp;
1461 int suuid[4];
1462 int sfd;
1463 int ra_blocked = 0;
1464 int ra_all = 0;
1465 int result = 0;
1466
1467 st->ss->getinfo_super(st, &info, NULL);
1468
1469 if ((c->runstop > 0 && info.container_enough >= 0) ||
1470 info.container_enough > 0)
1471 /* pass */;
1472 else {
1473 if (c->export) {
1474 printf("MD_STARTED=no\n");
1475 } else if (c->verbose)
1476 pr_err("not enough devices to start the container\n");
1477 return 0;
1478 }
1479
1480 match = conf_match(st, &info, devname, c->verbose, &rv);
1481 if (match == NULL && rv == 2)
1482 return rv;
1483
1484 /* Need to compute 'trustworthy' */
1485 if (match)
1486 trustworthy = LOCAL;
1487 else if (st->ss->match_home(st, c->homehost) == 1)
1488 trustworthy = LOCAL;
1489 else if (st->ss->match_home(st, "any") == 1)
1490 trustworthy = LOCAL;
1491 else
1492 trustworthy = FOREIGN;
1493
1494 list = st->ss->container_content(st, NULL);
1495 /* when nothing to activate - quit */
1496 if (list == NULL) {
1497 if (c->export) {
1498 printf("MD_STARTED=nothing\n");
1499 }
1500 return 0;
1501 }
1502 for (ra = list ; ra ; ra = ra->next) {
1503 int mdfd;
1504 char chosen_name[1024];
1505 struct map_ent *mp;
1506 struct mddev_ident *match = NULL;
1507
1508 ra_all++;
1509 /* do not activate arrays blocked by metadata handler */
1510 if (ra->array.state & (1 << MD_SB_BLOCK_VOLUME)) {
1511 pr_err("Cannot activate array %s in %s.\n",
1512 ra->text_version, devname);
1513 ra_blocked++;
1514 continue;
1515 }
1516 mp = map_by_uuid(&map, ra->uuid);
1517
1518 if (mp) {
1519 mdfd = open_dev(mp->devnm);
1520 if (mp->path)
1521 strcpy(chosen_name, mp->path);
1522 else
1523 strcpy(chosen_name, mp->devnm);
1524 } else if (!only) {
1525
1526 /* Check in mdadm.conf for container == devname and
1527 * member == ra->text_version after second slash.
1528 */
1529 char *sub = strchr(ra->text_version+1, '/');
1530 struct mddev_ident *array_list;
1531 if (sub) {
1532 sub++;
1533 array_list = conf_get_ident(NULL);
1534 } else
1535 array_list = NULL;
1536 for(; array_list ; array_list = array_list->next) {
1537 char *dn;
1538 if (array_list->member == NULL ||
1539 array_list->container == NULL)
1540 continue;
1541 if (strcmp(array_list->member, sub) != 0)
1542 continue;
1543 if (array_list->uuid_set &&
1544 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1545 continue;
1546 dn = container2devname(array_list->container);
1547 if (dn == NULL)
1548 continue;
1549 if (strncmp(dn, ra->text_version+1,
1550 strlen(dn)) != 0 ||
1551 ra->text_version[strlen(dn)+1] != '/') {
1552 free(dn);
1553 continue;
1554 }
1555 free(dn);
1556 /* we have a match */
1557 match = array_list;
1558 if (c->verbose>0)
1559 pr_err("match found for member %s\n",
1560 array_list->member);
1561 break;
1562 }
1563
1564 if (match && match->devname &&
1565 strcasecmp(match->devname, "<ignore>") == 0) {
1566 if (c->verbose > 0)
1567 pr_err("array %s/%s is explicitly ignored by mdadm.conf\n",
1568 match->container, match->member);
1569 continue;
1570 }
1571 if (match)
1572 trustworthy = LOCAL;
1573
1574 mdfd = create_mddev(match ? match->devname : NULL,
1575 ra->name,
1576 c->autof,
1577 trustworthy,
1578 chosen_name, 0);
1579 }
1580 if (only && (!mp || strcmp(mp->devnm, only) != 0))
1581 continue;
1582
1583 if (mdfd < 0) {
1584 pr_err("failed to open %s: %s.\n",
1585 chosen_name, strerror(errno));
1586 return 2;
1587 }
1588
1589 assemble_container_content(st, mdfd, ra, c,
1590 chosen_name, &result);
1591 map_free(map);
1592 map = NULL;
1593 close(mdfd);
1594 }
1595 if (c->export && result) {
1596 char sep = '=';
1597 printf("MD_STARTED");
1598 if (result & INCR_NO) {
1599 printf("%cno", sep);
1600 sep = ',';
1601 }
1602 if (result & INCR_UNSAFE) {
1603 printf("%cunsafe", sep);
1604 sep = ',';
1605 }
1606 if (result & INCR_ALREADY) {
1607 printf("%calready", sep);
1608 sep = ',';
1609 }
1610 if (result & INCR_YES) {
1611 printf("%cyes", sep);
1612 sep = ',';
1613 }
1614 printf("\n");
1615 }
1616
1617 /* don't move spares to container with volume being activated
1618 when all volumes are blocked */
1619 if (ra_all == ra_blocked)
1620 return 0;
1621
1622 /* Now move all suitable spares from spare container */
1623 domains = domain_from_array(list, st->ss->name);
1624 memcpy(suuid, uuid_zero, sizeof(int[4]));
1625 if (domains &&
1626 (smp = map_by_uuid(&map, suuid)) != NULL &&
1627 (sfd = open(smp->path, O_RDONLY)) >= 0) {
1628 /* spare container found */
1629 struct supertype *sst =
1630 super_imsm.match_metadata_desc("imsm");
1631 struct mdinfo *sinfo;
1632
1633 if (!sst->ss->load_container(sst, sfd, NULL)) {
1634 struct spare_criteria sc = {0, 0};
1635
1636 if (st->ss->get_spare_criteria)
1637 st->ss->get_spare_criteria(st, &sc);
1638
1639 close(sfd);
1640 sinfo = container_choose_spares(sst, &sc,
1641 domains, NULL,
1642 st->ss->name, 0);
1643 sst->ss->free_super(sst);
1644 if (sinfo){
1645 int count = 0;
1646 struct mdinfo *disks = sinfo->devs;
1647 while (disks) {
1648 /* move spare from spare
1649 * container to currently
1650 * assembled one
1651 */
1652 if (move_spare(
1653 smp->path,
1654 devname,
1655 makedev(disks->disk.major,
1656 disks->disk.minor)))
1657 count++;
1658 disks = disks->next;
1659 }
1660 if (count)
1661 pr_err("Added %d spare%s to %s\n",
1662 count, count>1?"s":"", devname);
1663 }
1664 sysfs_free(sinfo);
1665 } else
1666 close(sfd);
1667 }
1668 domain_free(domains);
1669 map_free(map);
1670 return 0;
1671 }
1672
1673 static void run_udisks(char *arg1, char *arg2)
1674 {
1675 int pid = fork();
1676 int status;
1677 if (pid == 0) {
1678 execl("/usr/bin/udisks", "udisks", arg1, arg2, NULL);
1679 execl("/bin/udisks", "udisks", arg1, arg2, NULL);
1680 exit(1);
1681 }
1682 while (pid > 0 && wait(&status) != pid)
1683 ;
1684 }
1685
1686 /*
1687 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1688 * raid arrays, and if so first fail (if needed) and then remove the device.
1689 *
1690 * @devname - The device we want to remove
1691 * @id_path - name as found in /dev/disk/by-path for this device
1692 *
1693 * Note: the device name must be a kernel name like "sda", so
1694 * that we can find it in /proc/mdstat
1695 */
1696 int IncrementalRemove(char *devname, char *id_path, int verbose)
1697 {
1698 int mdfd;
1699 int rv = 0;
1700 struct mdstat_ent *ent;
1701 struct mddev_dev devlist;
1702 struct mdinfo mdi;
1703 char buf[32];
1704
1705 if (!id_path)
1706 dprintf("incremental removal without --path <id_path> lacks the possibility to re-add new device in this port\n");
1707
1708 if (strchr(devname, '/')) {
1709 pr_err("incremental removal requires a kernel device name, not a file: %s\n", devname);
1710 return 1;
1711 }
1712 ent = mdstat_by_component(devname);
1713 if (!ent) {
1714 if (verbose >= 0)
1715 pr_err("%s does not appear to be a component of any array\n", devname);
1716 return 1;
1717 }
1718 if (sysfs_init(&mdi, -1, ent->devnm)) {
1719 pr_err("unable to initialize sysfs for: %s\n", devname);
1720 return 1;
1721 }
1722 mdfd = open_dev_excl(ent->devnm);
1723 if (mdfd > 0) {
1724 close(mdfd);
1725 if (sysfs_get_str(&mdi, NULL, "array_state",
1726 buf, sizeof(buf)) > 0) {
1727 if (strncmp(buf, "active", 6) == 0 ||
1728 strncmp(buf, "clean", 5) == 0)
1729 sysfs_set_str(&mdi, NULL,
1730 "array_state", "read-auto");
1731 }
1732 }
1733 mdfd = open_dev(ent->devnm);
1734 if (mdfd < 0) {
1735 if (verbose >= 0)
1736 pr_err("Cannot open array %s!!\n", ent->devnm);
1737 free_mdstat(ent);
1738 return 1;
1739 }
1740
1741 if (id_path) {
1742 struct map_ent *map = NULL, *me;
1743 me = map_by_devnm(&map, ent->devnm);
1744 if (me)
1745 policy_save_path(id_path, me);
1746 map_free(map);
1747 }
1748
1749 memset(&devlist, 0, sizeof(devlist));
1750 devlist.devname = devname;
1751 devlist.disposition = 'f';
1752 /* for a container, we must fail each member array */
1753 if (ent->metadata_version &&
1754 strncmp(ent->metadata_version, "external:", 9) == 0) {
1755 struct mdstat_ent *mdstat = mdstat_read(0, 0);
1756 struct mdstat_ent *memb;
1757 for (memb = mdstat ; memb ; memb = memb->next)
1758 if (is_container_member(memb, ent->devnm)) {
1759 int subfd = open_dev(memb->devnm);
1760 if (subfd >= 0) {
1761 rv |= Manage_subdevs(
1762 memb->devnm, subfd,
1763 &devlist, verbose, 0,
1764 NULL, 0);
1765 close(subfd);
1766 }
1767 }
1768 free_mdstat(mdstat);
1769 } else
1770 rv |= Manage_subdevs(ent->devnm, mdfd, &devlist,
1771 verbose, 0, NULL, 0);
1772 if (rv & 2) {
1773 /* Failed due to EBUSY, try to stop the array.
1774 * Give udisks a chance to unmount it first.
1775 */
1776 int devid = devnm2devid(ent->devnm);
1777 run_udisks("--unmount", map_dev(major(devid),minor(devid), 0));
1778 rv = Manage_stop(ent->devnm, mdfd, verbose, 1);
1779 if (rv)
1780 /* At least we can try to trigger a 'remove' */
1781 sysfs_uevent(&mdi, "remove");
1782 if (verbose) {
1783 if (rv)
1784 pr_err("Fail to stop %s too.\n", ent->devnm);
1785 }
1786 } else {
1787 devlist.disposition = 'r';
1788 rv = Manage_subdevs(ent->devnm, mdfd, &devlist,
1789 verbose, 0, NULL, 0);
1790 }
1791 close(mdfd);
1792 free_mdstat(ent);
1793 return rv;
1794 }