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