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