]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Incremental.c
Assemble: 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, 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 /* Skip the clustered ones. This should be started by
235 * clustering resource agents
236 */
237 if (info.array.state & (1 << MD_SB_CLUSTERED))
238 goto out;
239
240 /* 3a/ if not, check for homehost match. If no match, continue
241 * but don't trust the 'name' in the array. Thus a 'random' minor
242 * number will be assigned, and the device name will be based
243 * on that. */
244 if (match)
245 trustworthy = LOCAL;
246 else if (st->ss->match_home(st, c->homehost) == 1)
247 trustworthy = LOCAL;
248 else if (st->ss->match_home(st, "any") == 1)
249 trustworthy = LOCAL_ANY;
250 else
251 trustworthy = FOREIGN;
252
253 if (!match && !conf_test_metadata(st->ss->name, policy,
254 (trustworthy == LOCAL))) {
255 if (c->verbose >= 1)
256 pr_err("%s has metadata type %s for which auto-assembly is disabled\n",
257 devname, st->ss->name);
258 goto out;
259 }
260 if (trustworthy == LOCAL_ANY)
261 trustworthy = LOCAL;
262
263 /* There are three possible sources for 'autof': command line,
264 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
265 * ARRAY takes precedence, then command line, then
266 * CREATE.
267 */
268 if (match && match->autof)
269 c->autof = match->autof;
270 if (c->autof == 0)
271 c->autof = ci->autof;
272
273 name_to_use = info.name;
274 if (name_to_use[0] == 0 &&
275 info.array.level == LEVEL_CONTAINER) {
276 name_to_use = info.text_version;
277 trustworthy = METADATA;
278 }
279 if (name_to_use[0] && trustworthy != LOCAL &&
280 ! c->require_homehost &&
281 conf_name_is_free(name_to_use))
282 trustworthy = LOCAL;
283
284 /* strip "hostname:" prefix from name if we have decided
285 * to treat it as LOCAL
286 */
287 if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL)
288 name_to_use = strchr(name_to_use, ':')+1;
289
290 /* 4/ Check if array exists.
291 */
292 if (map_lock(&map))
293 pr_err("failed to get exclusive lock on 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 --incremental reliably. Aborting.\n");
356 rv = 2;
357 goto out_unlock;
358 }
359 info.array.working_disks = 1;
360 /* 6/ Make sure /var/run/mdadm.map contains this array. */
361 map_update(&map, fd2devnm(mdfd),
362 info.text_version,
363 info.uuid, chosen_name);
364 } else {
365 /* 5b/ if it does */
366 /* - check one drive in array to make sure metadata is a reasonably */
367 /* close match. Reject if not (e.g. different type) */
368 /* - add the device */
369 char dn[20];
370 int dfd2;
371 int err;
372 struct supertype *st2;
373 struct mdinfo info2, *d;
374
375 sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
376 GET_OFFSET | GET_SIZE));
377
378 if (mp->path)
379 strcpy(chosen_name, mp->path);
380 else
381 strcpy(chosen_name, mp->devnm);
382
383 /* It is generally not OK to add non-spare drives to a
384 * running array as they are probably missing because
385 * they failed. However if runstop is 1, then the
386 * array was possibly started early and our best bet is
387 * to add this anyway.
388 * Also if action policy is re-add or better we allow
389 * re-add.
390 * This doesn't apply to containers as the 'non-spare'
391 * flag has a different meaning. The test has to happen
392 * at the device level there
393 */
394 if (!st->ss->external
395 && (info.disk.state & (1<<MD_DISK_SYNC)) != 0
396 && ! policy_action_allows(policy, st->ss->name,
397 act_re_add)
398 && c->runstop < 1) {
399 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
400 pr_err("not adding %s to active array (without --run) %s\n",
401 devname, chosen_name);
402 rv = 2;
403 goto out_unlock;
404 }
405 }
406 if (!sra) {
407 rv = 2;
408 goto out_unlock;
409 }
410 if (sra->devs) {
411 sprintf(dn, "%d:%d", sra->devs->disk.major,
412 sra->devs->disk.minor);
413 dfd2 = dev_open(dn, O_RDONLY);
414 if (dfd2 < 0) {
415 pr_err("unable to open %s\n", devname);
416 rv = 2;
417 goto out_unlock;
418 }
419 st2 = dup_super(st);
420 if (st2->ss->load_super(st2, dfd2, NULL) ||
421 st->ss->compare_super(st, st2) != 0) {
422 pr_err("metadata mismatch between %s and chosen array %s\n",
423 devname, chosen_name);
424 close(dfd2);
425 rv = 2;
426 goto out_unlock;
427 }
428 close(dfd2);
429 st2->ss->getinfo_super(st2, &info2, NULL);
430 st2->ss->free_super(st2);
431 if (info.array.level != info2.array.level ||
432 memcmp(info.uuid, info2.uuid, 16) != 0 ||
433 info.array.raid_disks != info2.array.raid_disks) {
434 pr_err("unexpected difference between %s and %s.\n",
435 chosen_name, devname);
436 rv = 2;
437 goto out_unlock;
438 }
439 }
440 info.disk.major = major(stb.st_rdev);
441 info.disk.minor = minor(stb.st_rdev);
442 /* add disk needs to know about containers */
443 if (st->ss->external)
444 sra->array.level = LEVEL_CONTAINER;
445 err = add_disk(mdfd, st, sra, &info);
446 if (err < 0 && errno == EBUSY) {
447 /* could be another device present with the same
448 * disk.number. Find and reject any such
449 */
450 find_reject(mdfd, st, sra, info.disk.number,
451 info.events, c->verbose, chosen_name);
452 err = add_disk(mdfd, st, sra, &info);
453 }
454 if (err < 0 && errno == EINVAL &&
455 info.disk.state & (1<<MD_DISK_SYNC)) {
456 /* Maybe it needs to be added as a spare */
457 if (policy_action_allows(policy, st->ss->name,
458 act_force_spare)) {
459 info.disk.state &= ~(1<<MD_DISK_SYNC);
460 err = add_disk(mdfd, st, sra, &info);
461 } else
462 if (c->verbose >= 0)
463 pr_err("can only add %s to %s as a spare, and force-spare is not set.\n",
464 devname, chosen_name);
465 }
466 if (err < 0) {
467 pr_err("failed to add %s to existing array %s: %s.\n",
468 devname, chosen_name, strerror(errno));
469 rv = 2;
470 goto out_unlock;
471 }
472 info.array.working_disks = 0;
473 for (d = sra->devs; d; d=d->next)
474 info.array.working_disks ++;
475
476 }
477 if (strncmp(chosen_name, "/dev/md/", 8) == 0)
478 md_devname = chosen_name+8;
479 else
480 md_devname = chosen_name;
481 if (c->export) {
482 printf("MD_DEVICE=%s\n", fd2devnm(mdfd));
483 printf("MD_DEVNAME=%s\n", md_devname);
484 printf("MD_FOREIGN=%s\n", trustworthy == FOREIGN ? "yes" : "no");
485 }
486
487 /* 7/ Is there enough devices to possibly start the array? */
488 /* 7a/ if not, finish with success. */
489 if (info.array.level == LEVEL_CONTAINER) {
490 char devnm[32];
491 /* Try to assemble within the container */
492 sysfs_uevent(sra, "change");
493 if (!c->export && c->verbose >= 0)
494 pr_err("container %s now has %d device%s\n",
495 chosen_name, info.array.working_disks,
496 info.array.working_disks == 1?"":"s");
497 wait_for(chosen_name, mdfd);
498 if (st->ss->external)
499 strcpy(devnm, fd2devnm(mdfd));
500 if (st->ss->load_container)
501 rv = st->ss->load_container(st, mdfd, NULL);
502 close(mdfd);
503 sysfs_free(sra);
504 if (!rv)
505 rv = Incremental_container(st, chosen_name, c, NULL);
506 map_unlock(&map);
507 /* after spare is added, ping monitor for external metadata
508 * so that it can eg. try to rebuild degraded array */
509 if (st->ss->external)
510 ping_monitor(devnm);
511 return rv;
512 }
513
514 /* We have added something to the array, so need to re-read the
515 * state. Eventually this state should be kept up-to-date as
516 * things change.
517 */
518 sysfs_free(sra);
519 sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE |
520 GET_OFFSET | GET_SIZE));
521 active_disks = count_active(st, sra, mdfd, &avail, &info);
522 if (enough(info.array.level, info.array.raid_disks,
523 info.array.layout, info.array.state & 1,
524 avail) == 0) {
525 if (c->export) {
526 printf("MD_STARTED=no\n");
527 } else if (c->verbose >= 0)
528 pr_err("%s attached to %s, not enough to start (%d).\n",
529 devname, chosen_name, active_disks);
530 rv = 0;
531 goto out_unlock;
532 }
533
534 /* 7b/ if yes, */
535 /* - if number of OK devices match expected, or -R and there */
536 /* are enough, */
537 /* + add any bitmap file */
538 /* + start the array (auto-readonly). */
539
540 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
541 if (c->export) {
542 printf("MD_STARTED=already\n");
543 } else if (c->verbose >= 0)
544 pr_err("%s attached to %s which is already active.\n",
545 devname, chosen_name);
546 rv = 0;
547 goto out_unlock;
548 }
549
550 map_unlock(&map);
551 if (c->runstop > 0 || active_disks >= info.array.working_disks) {
552 struct mdinfo *dsk;
553 /* Let's try to start it */
554
555 if (info.reshape_active && !(info.reshape_active & RESHAPE_NO_BACKUP)) {
556 pr_err("%s: This array is being reshaped and cannot be started\n",
557 chosen_name);
558 cont_err("by --incremental. Please use --assemble\n");
559 goto out;
560 }
561 if (match && match->bitmap_file) {
562 int bmfd = open(match->bitmap_file, O_RDWR);
563 if (bmfd < 0) {
564 pr_err("Could not open bitmap file %s.\n",
565 match->bitmap_file);
566 goto out;
567 }
568 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
569 close(bmfd);
570 pr_err("Failed to set bitmapfile for %s.\n",
571 chosen_name);
572 goto out;
573 }
574 close(bmfd);
575 }
576 /* Need to remove from the array any devices which
577 * 'count_active' discerned were too old or inappropriate
578 */
579 for (d = sra ? sra->devs : NULL ; d ; d = d->next)
580 if (d->disk.state & (1<<MD_DISK_REMOVED))
581 remove_disk(mdfd, st, sra, d);
582
583 if ((sra == NULL || active_disks >= info.array.working_disks)
584 && trustworthy != FOREIGN)
585 rv = ioctl(mdfd, RUN_ARRAY, NULL);
586 else
587 rv = sysfs_set_str(sra, NULL,
588 "array_state", "read-auto");
589 /* Array might be O_EXCL which will interfere with
590 * fsck and mount. So re-open without O_EXCL.
591 */
592 reopen_mddev(mdfd);
593 if (rv == 0) {
594 if (c->export) {
595 printf("MD_STARTED=yes\n");
596 } else if (c->verbose >= 0)
597 pr_err("%s attached to %s, which has been started.\n",
598 devname, chosen_name);
599 rv = 0;
600 wait_for(chosen_name, mdfd);
601 /* We just started the array, so some devices
602 * might have been evicted from the array
603 * because their event counts were too old.
604 * If the action=re-add policy is in-force for
605 * those devices we should re-add them now.
606 */
607 for (dsk = sra->devs; dsk ; dsk = dsk->next) {
608 if (disk_action_allows(dsk, st->ss->name, act_re_add) &&
609 add_disk(mdfd, st, sra, dsk) == 0)
610 pr_err("%s re-added to %s\n",
611 dsk->sys_name, chosen_name);
612 }
613 } else {
614 pr_err("%s attached to %s, but failed to start: %s.\n",
615 devname, chosen_name, strerror(errno));
616 rv = 1;
617 }
618 } else {
619 if (c->export) {
620 printf("MD_STARTED=unsafe\n");
621 } else if (c->verbose >= 0)
622 pr_err("%s attached to %s, not enough to start safely.\n",
623 devname, chosen_name);
624 rv = 0;
625 }
626 out:
627 free(avail);
628 if (dfd >= 0)
629 close(dfd);
630 if (mdfd >= 0)
631 close(mdfd);
632 if (policy)
633 dev_policy_free(policy);
634 if (sra)
635 sysfs_free(sra);
636 return rv;
637 out_unlock:
638 map_unlock(&map);
639 goto out;
640 }
641
642 static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
643 int number, __u64 events, int verbose,
644 char *array_name)
645 {
646 /* Find a device attached to this array with a disk.number of number
647 * and events less than the passed events, and remove the device.
648 */
649 struct mdinfo *d;
650 mdu_array_info_t ra;
651
652 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
653 return; /* not safe to remove from active arrays
654 * without thinking more */
655
656 for (d = sra->devs; d ; d = d->next) {
657 char dn[10];
658 int dfd;
659 struct mdinfo info;
660 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
661 dfd = dev_open(dn, O_RDONLY);
662 if (dfd < 0)
663 continue;
664 if (st->ss->load_super(st, dfd, NULL)) {
665 close(dfd);
666 continue;
667 }
668 st->ss->getinfo_super(st, &info, NULL);
669 st->ss->free_super(st);
670 close(dfd);
671
672 if (info.disk.number != number ||
673 info.events >= events)
674 continue;
675
676 if (d->disk.raid_disk > -1)
677 sysfs_set_str(sra, d, "slot", "none");
678 if (sysfs_set_str(sra, d, "state", "remove") == 0)
679 if (verbose >= 0)
680 pr_err("removing old device %s from %s\n",
681 d->sys_name+4, array_name);
682 }
683 }
684
685 static int count_active(struct supertype *st, struct mdinfo *sra,
686 int mdfd, char **availp,
687 struct mdinfo *bestinfo)
688 {
689 /* count how many devices in sra think they are active */
690 struct mdinfo *d;
691 int cnt = 0;
692 int replcnt = 0;
693 __u64 max_events = 0;
694 char *avail = NULL;
695 int *best = NULL;
696 char *devmap = NULL;
697 int numdevs = 0;
698 int devnum;
699 int b, i;
700 int raid_disks = 0;
701
702 if (!sra)
703 return 0;
704
705 for (d = sra->devs ; d ; d = d->next)
706 numdevs++;
707 for (d = sra->devs, devnum = 0 ; d ; d = d->next, devnum++) {
708 char dn[30];
709 int dfd;
710 int ok;
711 struct mdinfo info;
712
713 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
714 dfd = dev_open(dn, O_RDONLY);
715 if (dfd < 0)
716 continue;
717 ok = st->ss->load_super(st, dfd, NULL);
718 close(dfd);
719 if (ok != 0)
720 continue;
721 info.array.raid_disks = raid_disks;
722 st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum);
723 if (!avail) {
724 raid_disks = info.array.raid_disks;
725 avail = xcalloc(raid_disks, 1);
726 *availp = avail;
727
728 best = xcalloc(raid_disks, sizeof(int));
729 devmap = xcalloc(raid_disks, numdevs);
730
731 st->ss->getinfo_super(st, &info, devmap);
732 }
733
734 if (info.disk.state & (1<<MD_DISK_SYNC))
735 {
736 if (cnt == 0) {
737 cnt++;
738 max_events = info.events;
739 avail[info.disk.raid_disk] = 2;
740 best[info.disk.raid_disk] = devnum;
741 st->ss->getinfo_super(st, bestinfo, NULL);
742 } else if (info.events == max_events) {
743 avail[info.disk.raid_disk] = 2;
744 best[info.disk.raid_disk] = devnum;
745 } else if (info.events == max_events-1) {
746 if (avail[info.disk.raid_disk] == 0) {
747 avail[info.disk.raid_disk] = 1;
748 best[info.disk.raid_disk] = devnum;
749 }
750 } else if (info.events < max_events - 1)
751 ;
752 else if (info.events == max_events+1) {
753 int i;
754 max_events = info.events;
755 for (i = 0; i < raid_disks; i++)
756 if (avail[i])
757 avail[i]--;
758 avail[info.disk.raid_disk] = 2;
759 best[info.disk.raid_disk] = devnum;
760 st->ss->getinfo_super(st, bestinfo, NULL);
761 } else { /* info.events much bigger */
762 memset(avail, 0, raid_disks);
763 max_events = info.events;
764 avail[info.disk.raid_disk] = 2;
765 best[info.disk.raid_disk] = devnum;
766 st->ss->getinfo_super(st, bestinfo, NULL);
767 }
768 } else if (info.disk.state & (1<<MD_DISK_REPLACEMENT))
769 replcnt++;
770 st->ss->free_super(st);
771 }
772 if (!avail)
773 return 0;
774 /* We need to reject any device that thinks the best device is
775 * failed or missing */
776 for (b = 0; b < raid_disks; b++)
777 if (avail[b] == 2)
778 break;
779 cnt = 0;
780 for (i = 0 ; i < raid_disks ; i++) {
781 if (i != b && avail[i])
782 if (devmap[raid_disks * best[i] + b] == 0) {
783 /* This device thinks 'b' is failed -
784 * don't use it */
785 devnum = best[i];
786 for (d=sra->devs ; devnum; d = d->next)
787 devnum--;
788 d->disk.state |= (1 << MD_DISK_REMOVED);
789 avail[i] = 0;
790 }
791 if (avail[i])
792 cnt++;
793 }
794 /* Also need to reject any spare device with an event count that
795 * is too high
796 */
797 for (d = sra->devs; d; d = d->next) {
798 if (!(d->disk.state & (1<<MD_DISK_SYNC)) &&
799 d->events > max_events)
800 d->disk.state |= (1 << MD_DISK_REMOVED);
801 }
802 free(best);
803 free(devmap);
804 return cnt + replcnt;
805 }
806
807 /* test if container has degraded member(s) */
808 static int container_members_max_degradation(struct map_ent *map, struct map_ent *me)
809 {
810 mdu_array_info_t array;
811 int afd;
812 int max_degraded = 0;
813
814 for(; map; map = map->next) {
815 if (!metadata_container_matches(map->metadata, me->devnm))
816 continue;
817 afd = open_dev(map->devnm);
818 if (afd < 0)
819 continue;
820 /* most accurate information regarding array degradation */
821 if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) {
822 int degraded = array.raid_disks - array.active_disks -
823 array.spare_disks;
824 if (degraded > max_degraded)
825 max_degraded = degraded;
826 }
827 close(afd);
828 }
829 return (max_degraded);
830 }
831
832 static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
833 struct map_ent *target, int bare,
834 struct supertype *st, int verbose)
835 {
836 /* This device doesn't have any md metadata
837 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
838 * If 'st' is not set, then we only know that some metadata allows this,
839 * others possibly don't.
840 * So look for a container or array to attach the device to.
841 * Prefer 'target' if that is set and the array is found.
842 *
843 * If st is set, then only arrays of that type are considered
844 * Return 0 on success, or some exit code on failure, probably 1.
845 */
846 int rv = 1;
847 struct stat stb;
848 struct map_ent *mp, *map = NULL;
849 struct mdinfo *chosen = NULL;
850 int dfd = *dfdp;
851
852 if (fstat(dfd, &stb) != 0)
853 return 1;
854
855 /*
856 * Now we need to find a suitable array to add this to.
857 * We only accept arrays that:
858 * - match 'st'
859 * - are in the same domains as the device
860 * - are of an size for which the device will be useful
861 * and we choose the one that is the most degraded
862 */
863
864 if (map_lock(&map)) {
865 pr_err("failed to get exclusive lock on mapfile\n");
866 return 1;
867 }
868 for (mp = map ; mp ; mp = mp->next) {
869 struct supertype *st2;
870 struct domainlist *dl = NULL;
871 struct mdinfo *sra;
872 unsigned long long devsize;
873 unsigned long long component_size = 0;
874
875 if (is_subarray(mp->metadata))
876 continue;
877 if (st) {
878 st2 = st->ss->match_metadata_desc(mp->metadata);
879 if (!st2 ||
880 (st->minor_version >= 0 &&
881 st->minor_version != st2->minor_version)) {
882 if (verbose > 1)
883 pr_err("not adding %s to %s as metadata type doesn't match\n",
884 devname, mp->path);
885 free(st2);
886 continue;
887 }
888 free(st2);
889 }
890 sra = sysfs_read(-1, mp->devnm,
891 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
892 GET_DEGRADED|GET_COMPONENT|GET_VERSION);
893 if (!sra) {
894 /* Probably a container - no degraded info */
895 sra = sysfs_read(-1, mp->devnm,
896 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
897 GET_COMPONENT|GET_VERSION);
898 if (sra)
899 sra->array.failed_disks = -1;
900 }
901 if (!sra)
902 continue;
903 if (st == NULL) {
904 int i;
905 st2 = NULL;
906 for(i = 0; !st2 && superlist[i]; i++)
907 st2 = superlist[i]->match_metadata_desc(
908 sra->text_version);
909 if (!st2) {
910 if (verbose > 1)
911 pr_err("not adding %s to %s 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 it is not in a compatible domain\n",
994 devname, mp->path);
995
996 goto next;
997 }
998 /* all tests passed, OK to add to this array */
999 if (!chosen) {
1000 chosen = sra;
1001 sra = NULL;
1002 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
1003 sysfs_free(chosen);
1004 chosen = sra;
1005 sra = NULL;
1006 }
1007 next:
1008 if (sra)
1009 sysfs_free(sra);
1010 if (st != st2)
1011 free(st2);
1012 if (dl)
1013 domain_free(dl);
1014 }
1015 if (chosen) {
1016 /* add current device to chosen array as a spare */
1017 int mdfd = open_dev(chosen->sys_name);
1018 if (mdfd >= 0) {
1019 struct mddev_dev devlist;
1020 char devname[20];
1021 devlist.next = NULL;
1022 devlist.used = 0;
1023 devlist.writemostly = 0;
1024 devlist.devname = devname;
1025 sprintf(devname, "%d:%d", major(stb.st_rdev),
1026 minor(stb.st_rdev));
1027 devlist.disposition = 'a';
1028 close(dfd);
1029 *dfdp = -1;
1030 rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist,
1031 -1, 0, NULL, 0);
1032 close(mdfd);
1033 }
1034 if (verbose > 0) {
1035 if (rv == 0)
1036 pr_err("added %s as spare for %s\n",
1037 devname, chosen->sys_name);
1038 else
1039 pr_err("failed to add %s as spare for %s\n",
1040 devname, chosen->sys_name);
1041 }
1042 sysfs_free(chosen);
1043 }
1044 map_unlock(&map);
1045 return rv;
1046 }
1047
1048 static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
1049 struct supertype *st, int verbose)
1050 {
1051 /* we know that at least one partition virtual-metadata is
1052 * allowed to incorporate spares like this device. We need to
1053 * find a suitable device to copy partition information from.
1054 *
1055 * Getting a list of all disk (not partition) devices is
1056 * slightly non-trivial. We could look at /sys/block, but
1057 * that is theoretically due to be removed. Maybe best to use
1058 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
1059 * we depend on this directory of 'path' info. But that fails
1060 * to find loop devices and probably others. Maybe don't
1061 * worry about that, they aren't the real target.
1062 *
1063 * So: check things in /dev/disk/by-path to see if they are in
1064 * a compatible domain, then load the partition table and see
1065 * if it is OK for the new device, and choose the largest
1066 * partition table that fits.
1067 */
1068 DIR *dir;
1069 struct dirent *de;
1070 char *chosen = NULL;
1071 unsigned long long chosen_size = 0;
1072 struct supertype *chosen_st = NULL;
1073 int fd;
1074
1075 dir = opendir("/dev/disk/by-path");
1076 if (!dir)
1077 return 1;
1078 while ((de = readdir(dir)) != NULL) {
1079 char *ep;
1080 struct dev_policy *pol2 = NULL;
1081 struct domainlist *domlist = NULL;
1082 int fd = -1;
1083 struct mdinfo info;
1084 struct supertype *st2 = NULL;
1085 char *devname = NULL;
1086 unsigned long long devsectors;
1087
1088 if (de->d_ino == 0 ||
1089 de->d_name[0] == '.' ||
1090 (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
1091 goto next;
1092
1093 ep = de->d_name + strlen(de->d_name);
1094 while (ep > de->d_name &&
1095 isdigit(ep[-1]))
1096 ep--;
1097 if (ep > de->d_name + 5 &&
1098 strncmp(ep-5, "-part", 5) == 0)
1099 /* This is a partition - skip it */
1100 goto next;
1101
1102 pol2 = path_policy(de->d_name, type_disk);
1103
1104 domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
1105 if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
1106 /* new device is incompatible with this device. */
1107 goto next;
1108
1109 domain_free(domlist);
1110 domlist = NULL;
1111
1112 if (asprintf(&devname, "/dev/disk/by-path/%s", de->d_name) != 1) {
1113 devname = NULL;
1114 goto next;
1115 }
1116 fd = open(devname, O_RDONLY);
1117 if (fd < 0)
1118 goto next;
1119 if (get_dev_size(fd, devname, &devsectors) == 0)
1120 goto next;
1121 devsectors >>= 9;
1122
1123 if (st)
1124 st2 = dup_super(st);
1125 else
1126 st2 = guess_super_type(fd, guess_partitions);
1127 if (st2 == NULL ||
1128 st2->ss->load_super(st2, fd, NULL) < 0)
1129 goto next;
1130 st2->ignore_hw_compat = 0;
1131
1132 if (!st) {
1133 /* Check domain policy again, this time referring to metadata */
1134 domain_merge(&domlist, pol2, st2->ss->name);
1135 if (domain_test(domlist, pol, st2->ss->name) != 1)
1136 /* Incompatible devices for this metadata type */
1137 goto next;
1138 if (!policy_action_allows(pol, st2->ss->name, act_spare))
1139 /* Some partition types allow sparing, but not
1140 * this one.
1141 */
1142 goto next;
1143 }
1144
1145 st2->ss->getinfo_super(st2, &info, NULL);
1146 if (info.component_size > devsectors)
1147 /* This partitioning doesn't fit in the device */
1148 goto next;
1149
1150 /* This is an acceptable device to copy partition
1151 * metadata from. We could just stop here, but I
1152 * think I want to keep looking incase a larger
1153 * metadata which makes better use of the device can
1154 * be found.
1155 */
1156 if (chosen == NULL ||
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 considering as a spare\n",
1267 devname);
1268 return 1;
1269 }
1270 bare = 0;
1271 } else
1272 bare = 1;
1273
1274 /* It might be OK to add this device to an array - need to see
1275 * what arrays might be candidates.
1276 */
1277 if (st) {
1278 /* just try try 'array' or 'partition' based on this metadata */
1279 if (st->ss->add_to_super)
1280 return array_try_spare(devname, dfdp, pol, target, bare,
1281 st, verbose);
1282 else
1283 return partition_try_spare(devname, dfdp, pol,
1284 st, verbose);
1285 }
1286 /* No metadata was specified or found so options are open.
1287 * Check for whether any array metadata, or any partition metadata
1288 * might allow adding the spare. This check is just help to avoid
1289 * a more costly scan of all arrays when we can be sure that will
1290 * fail.
1291 */
1292 for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) {
1293 if (superlist[i]->add_to_super && !arrays_ok &&
1294 policy_action_allows(pol, superlist[i]->name, act_spare))
1295 arrays_ok = 1;
1296 if (superlist[i]->add_to_super == NULL && !partitions_ok &&
1297 policy_action_allows(pol, superlist[i]->name, act_spare))
1298 partitions_ok = 1;
1299 }
1300 rv = 1;
1301 if (arrays_ok)
1302 rv = array_try_spare(devname, dfdp, pol, target, bare,
1303 st, verbose);
1304 if (rv != 0 && partitions_ok)
1305 rv = partition_try_spare(devname, dfdp, pol, st, verbose);
1306 return rv;
1307 }
1308
1309 int IncrementalScan(struct context *c, char *devnm)
1310 {
1311 /* look at every device listed in the 'map' file.
1312 * If one is found that is not running then:
1313 * look in mdadm.conf for bitmap file.
1314 * if one exists, but array has none, add it.
1315 * try to start array in auto-readonly mode
1316 */
1317 struct map_ent *mapl = NULL;
1318 struct map_ent *me;
1319 struct mddev_ident *devs, *mddev;
1320 int rv = 0;
1321 char container[32];
1322 char *only = NULL;
1323
1324 map_read(&mapl);
1325 devs = conf_get_ident(NULL);
1326
1327 restart:
1328 for (me = mapl ; me ; me = me->next) {
1329 mdu_array_info_t array;
1330 mdu_bitmap_file_t bmf;
1331 struct mdinfo *sra;
1332 int mdfd;
1333
1334 if (devnm && strcmp(devnm, me->devnm) != 0)
1335 continue;
1336 if (devnm && me->metadata[0] == '/') {
1337 char *sl;
1338 /* member array, need to work on container */
1339 strncpy(container, me->metadata+1, 32);
1340 container[31] = 0;
1341 sl = strchr(container, '/');
1342 if (sl)
1343 *sl = 0;
1344 only = devnm;
1345 devnm = container;
1346 goto restart;
1347 }
1348 mdfd = open_dev(me->devnm);
1349
1350 if (mdfd < 0)
1351 continue;
1352 if (!isdigit(me->metadata[0])) {
1353 /* must be a container */
1354 struct supertype *st = super_by_fd(mdfd, NULL);
1355 int ret = 0;
1356 struct map_ent *map = NULL;
1357
1358 if (st && st->ss->load_container)
1359 ret = st->ss->load_container(st, mdfd, NULL);
1360 close(mdfd);
1361 if (!ret && st && st->ss->container_content) {
1362 if (map_lock(&map))
1363 pr_err("failed to get exclusive lock on mapfile\n");
1364 ret = Incremental_container(st, me->path, c, only);
1365 map_unlock(&map);
1366 }
1367 if (ret)
1368 rv = 1;
1369 continue;
1370 }
1371 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
1372 errno != ENODEV) {
1373 close(mdfd);
1374 continue;
1375 }
1376 /* Ok, we can try this one. Maybe it needs a bitmap */
1377 for (mddev = devs ; mddev ; mddev = mddev->next)
1378 if (mddev->devname && me->path
1379 && devname_matches(mddev->devname, me->path))
1380 break;
1381 if (mddev && mddev->bitmap_file) {
1382 /*
1383 * Note: early kernels will wrongly fail this, so it
1384 * is a hint only
1385 */
1386 int added = -1;
1387 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
1388 int bmfd = open(mddev->bitmap_file, O_RDWR);
1389 if (bmfd >= 0) {
1390 added = ioctl(mdfd, SET_BITMAP_FILE,
1391 bmfd);
1392 close(bmfd);
1393 }
1394 }
1395 if (c->verbose >= 0) {
1396 if (added == 0)
1397 pr_err("Added bitmap %s to %s\n",
1398 mddev->bitmap_file, me->path);
1399 else if (errno != EEXIST)
1400 pr_err("Failed to add bitmap to %s: %s\n",
1401 me->path, strerror(errno));
1402 }
1403 }
1404 /* FIXME check for reshape_active and consider not
1405 * starting array.
1406 */
1407 sra = sysfs_read(mdfd, NULL, 0);
1408 if (sra) {
1409 if (sysfs_set_str(sra, NULL,
1410 "array_state", "read-auto") == 0) {
1411 if (c->verbose >= 0)
1412 pr_err("started array %s\n",
1413 me->path ?: me->devnm);
1414 } else {
1415 pr_err("failed to start array %s: %s\n",
1416 me->path ?: me->devnm,
1417 strerror(errno));
1418 rv = 1;
1419 }
1420 sysfs_free(sra);
1421 }
1422 }
1423 return rv;
1424 }
1425
1426 static char *container2devname(char *devname)
1427 {
1428 char *mdname = NULL;
1429
1430 if (devname[0] == '/') {
1431 int fd = open(devname, O_RDONLY);
1432 if (fd >= 0) {
1433 mdname = xstrdup(fd2devnm(fd));
1434 close(fd);
1435 }
1436 } else {
1437 int uuid[4];
1438 struct map_ent *mp, *map = NULL;
1439
1440 if (!parse_uuid(devname, uuid))
1441 return mdname;
1442 mp = map_by_uuid(&map, uuid);
1443 if (mp)
1444 mdname = xstrdup(mp->devnm);
1445 map_free(map);
1446 }
1447
1448 return mdname;
1449 }
1450
1451 static int Incremental_container(struct supertype *st, char *devname,
1452 struct context *c, char *only)
1453 {
1454 /* Collect the contents of this container and for each
1455 * array, choose a device name and assemble the array.
1456 */
1457
1458 struct mdinfo *list;
1459 struct mdinfo *ra;
1460 struct map_ent *map = NULL;
1461 struct mdinfo info;
1462 int trustworthy;
1463 struct mddev_ident *match;
1464 int rv = 0;
1465 struct domainlist *domains;
1466 struct map_ent *smp;
1467 int suuid[4];
1468 int sfd;
1469 int ra_blocked = 0;
1470 int ra_all = 0;
1471 int result = 0;
1472
1473 st->ss->getinfo_super(st, &info, NULL);
1474
1475 if ((c->runstop > 0 && info.container_enough >= 0) ||
1476 info.container_enough > 0)
1477 /* pass */;
1478 else {
1479 if (c->export) {
1480 printf("MD_STARTED=no\n");
1481 } else if (c->verbose)
1482 pr_err("not enough devices to start the container\n");
1483 return 0;
1484 }
1485
1486 match = conf_match(st, &info, devname, c->verbose, &rv);
1487 if (match == NULL && rv == 2)
1488 return rv;
1489
1490 /* Need to compute 'trustworthy' */
1491 if (match)
1492 trustworthy = LOCAL;
1493 else if (st->ss->match_home(st, c->homehost) == 1)
1494 trustworthy = LOCAL;
1495 else if (st->ss->match_home(st, "any") == 1)
1496 trustworthy = LOCAL;
1497 else
1498 trustworthy = FOREIGN;
1499
1500 list = st->ss->container_content(st, NULL);
1501 /* when nothing to activate - quit */
1502 if (list == NULL) {
1503 if (c->export) {
1504 printf("MD_STARTED=nothing\n");
1505 }
1506 return 0;
1507 }
1508 for (ra = list ; ra ; ra = ra->next) {
1509 int mdfd;
1510 char chosen_name[1024];
1511 struct map_ent *mp;
1512 struct mddev_ident *match = NULL;
1513
1514 ra_all++;
1515 /* do not activate arrays blocked by metadata handler */
1516 if (ra->array.state & (1 << MD_SB_BLOCK_VOLUME)) {
1517 pr_err("Cannot activate array %s in %s.\n",
1518 ra->text_version, devname);
1519 ra_blocked++;
1520 continue;
1521 }
1522 mp = map_by_uuid(&map, ra->uuid);
1523
1524 if (mp) {
1525 mdfd = open_dev(mp->devnm);
1526 if (mp->path)
1527 strcpy(chosen_name, mp->path);
1528 else
1529 strcpy(chosen_name, mp->devnm);
1530 } else if (!only) {
1531
1532 /* Check in mdadm.conf for container == devname and
1533 * member == ra->text_version after second slash.
1534 */
1535 char *sub = strchr(ra->text_version+1, '/');
1536 struct mddev_ident *array_list;
1537 if (sub) {
1538 sub++;
1539 array_list = conf_get_ident(NULL);
1540 } else
1541 array_list = NULL;
1542 for(; array_list ; array_list = array_list->next) {
1543 char *dn;
1544 if (array_list->member == NULL ||
1545 array_list->container == NULL)
1546 continue;
1547 if (strcmp(array_list->member, sub) != 0)
1548 continue;
1549 if (array_list->uuid_set &&
1550 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1551 continue;
1552 dn = container2devname(array_list->container);
1553 if (dn == NULL)
1554 continue;
1555 if (strncmp(dn, ra->text_version+1,
1556 strlen(dn)) != 0 ||
1557 ra->text_version[strlen(dn)+1] != '/') {
1558 free(dn);
1559 continue;
1560 }
1561 free(dn);
1562 /* we have a match */
1563 match = array_list;
1564 if (c->verbose>0)
1565 pr_err("match found for member %s\n",
1566 array_list->member);
1567 break;
1568 }
1569
1570 if (match && match->devname &&
1571 strcasecmp(match->devname, "<ignore>") == 0) {
1572 if (c->verbose > 0)
1573 pr_err("array %s/%s is explicitly ignored by mdadm.conf\n",
1574 match->container, match->member);
1575 continue;
1576 }
1577 if (match)
1578 trustworthy = LOCAL;
1579
1580 mdfd = create_mddev(match ? match->devname : NULL,
1581 ra->name,
1582 c->autof,
1583 trustworthy,
1584 chosen_name);
1585 }
1586 if (only && (!mp || strcmp(mp->devnm, only) != 0))
1587 continue;
1588
1589 if (mdfd < 0) {
1590 pr_err("failed to open %s: %s.\n",
1591 chosen_name, strerror(errno));
1592 return 2;
1593 }
1594
1595 assemble_container_content(st, mdfd, ra, c,
1596 chosen_name, &result);
1597 close(mdfd);
1598 }
1599 if (c->export && result) {
1600 char sep = '=';
1601 printf("MD_STARTED");
1602 if (result & INCR_NO) {
1603 printf("%cno", sep);
1604 sep = ',';
1605 }
1606 if (result & INCR_UNSAFE) {
1607 printf("%cunsafe", sep);
1608 sep = ',';
1609 }
1610 if (result & INCR_ALREADY) {
1611 printf("%calready", sep);
1612 sep = ',';
1613 }
1614 if (result & INCR_YES) {
1615 printf("%cyes", sep);
1616 sep = ',';
1617 }
1618 printf("\n");
1619 }
1620
1621 /* don't move spares to container with volume being activated
1622 when all volumes are blocked */
1623 if (ra_all == ra_blocked)
1624 return 0;
1625
1626 /* Now move all suitable spares from spare container */
1627 domains = domain_from_array(list, st->ss->name);
1628 memcpy(suuid, uuid_zero, sizeof(int[4]));
1629 if (domains &&
1630 (smp = map_by_uuid(&map, suuid)) != NULL &&
1631 (sfd = open(smp->path, O_RDONLY)) >= 0) {
1632 /* spare container found */
1633 struct supertype *sst =
1634 super_imsm.match_metadata_desc("imsm");
1635 struct mdinfo *sinfo;
1636 unsigned long long min_size = 0;
1637 if (st->ss->min_acceptable_spare_size)
1638 min_size = st->ss->min_acceptable_spare_size(st);
1639 if (!sst->ss->load_container(sst, sfd, NULL)) {
1640 close(sfd);
1641 sinfo = container_choose_spares(sst, min_size,
1642 domains, NULL,
1643 st->ss->name, 0);
1644 sst->ss->free_super(sst);
1645 if (sinfo){
1646 int count = 0;
1647 struct mdinfo *disks = sinfo->devs;
1648 while (disks) {
1649 /* move spare from spare
1650 * container to currently
1651 * assembled one
1652 */
1653 if (move_spare(
1654 smp->path,
1655 devname,
1656 makedev(disks->disk.major,
1657 disks->disk.minor)))
1658 count++;
1659 disks = disks->next;
1660 }
1661 if (count)
1662 pr_err("Added %d spare%s to %s\n",
1663 count, count>1?"s":"", devname);
1664 }
1665 sysfs_free(sinfo);
1666 } else
1667 close(sfd);
1668 }
1669 domain_free(domains);
1670 return 0;
1671 }
1672
1673 static void run_udisks(char *arg1, char *arg2)
1674 {
1675 int pid = fork();
1676 int status;
1677 if (pid == 0) {
1678 execl("/usr/bin/udisks", "udisks", arg1, arg2, NULL);
1679 execl("/bin/udisks", "udisks", arg1, arg2, NULL);
1680 exit(1);
1681 }
1682 while (pid > 0 && wait(&status) != pid)
1683 ;
1684 }
1685
1686 /*
1687 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1688 * raid arrays, and if so first fail (if needed) and then remove the device.
1689 *
1690 * @devname - The device we want to remove
1691 * @id_path - name as found in /dev/disk/by-path for this device
1692 *
1693 * Note: the device name must be a kernel name like "sda", so
1694 * that we can find it in /proc/mdstat
1695 */
1696 int IncrementalRemove(char *devname, char *id_path, int verbose)
1697 {
1698 int mdfd;
1699 int rv = 0;
1700 struct mdstat_ent *ent;
1701 struct mddev_dev devlist;
1702 struct mdinfo mdi;
1703 char buf[32];
1704
1705 if (!id_path)
1706 dprintf("incremental removal without --path <id_path> lacks the possibility to re-add new device in this port\n");
1707
1708 if (strchr(devname, '/')) {
1709 pr_err("incremental removal requires a kernel device name, not a file: %s\n", devname);
1710 return 1;
1711 }
1712 ent = mdstat_by_component(devname);
1713 if (!ent) {
1714 if (verbose >= 0)
1715 pr_err("%s does not appear to be a component of any array\n", devname);
1716 return 1;
1717 }
1718 sysfs_init(&mdi, -1, ent->devnm);
1719 mdfd = open_dev_excl(ent->devnm);
1720 if (mdfd > 0) {
1721 close(mdfd);
1722 if (sysfs_get_str(&mdi, NULL, "array_state",
1723 buf, sizeof(buf)) > 0) {
1724 if (strncmp(buf, "active", 6) == 0 ||
1725 strncmp(buf, "clean", 5) == 0)
1726 sysfs_set_str(&mdi, NULL,
1727 "array_state", "read-auto");
1728 }
1729 }
1730 mdfd = open_dev(ent->devnm);
1731 if (mdfd < 0) {
1732 if (verbose >= 0)
1733 pr_err("Cannot open array %s!!\n", ent->devnm);
1734 free_mdstat(ent);
1735 return 1;
1736 }
1737
1738 if (id_path) {
1739 struct map_ent *map = NULL, *me;
1740 me = map_by_devnm(&map, ent->devnm);
1741 if (me)
1742 policy_save_path(id_path, me);
1743 map_free(map);
1744 }
1745
1746 memset(&devlist, 0, sizeof(devlist));
1747 devlist.devname = devname;
1748 devlist.disposition = 'f';
1749 /* for a container, we must fail each member array */
1750 if (ent->metadata_version &&
1751 strncmp(ent->metadata_version, "external:", 9) == 0) {
1752 struct mdstat_ent *mdstat = mdstat_read(0, 0);
1753 struct mdstat_ent *memb;
1754 for (memb = mdstat ; memb ; memb = memb->next)
1755 if (is_container_member(memb, ent->devnm)) {
1756 int subfd = open_dev(memb->devnm);
1757 if (subfd >= 0) {
1758 rv |= Manage_subdevs(
1759 memb->devnm, subfd,
1760 &devlist, verbose, 0,
1761 NULL, 0);
1762 close(subfd);
1763 }
1764 }
1765 free_mdstat(mdstat);
1766 } else
1767 rv |= Manage_subdevs(ent->devnm, mdfd, &devlist,
1768 verbose, 0, NULL, 0);
1769 if (rv & 2) {
1770 /* Failed due to EBUSY, try to stop the array.
1771 * Give udisks a chance to unmount it first.
1772 */
1773 int devid = devnm2devid(ent->devnm);
1774 run_udisks("--unmount", map_dev(major(devid),minor(devid), 0));
1775 rv = Manage_stop(ent->devnm, mdfd, verbose, 1);
1776 if (rv)
1777 /* At least we can try to trigger a 'remove' */
1778 sysfs_uevent(&mdi, "remove");
1779 if (verbose) {
1780 if (rv)
1781 pr_err("Fail to stop %s too.\n", ent->devnm);
1782 }
1783 } else {
1784 devlist.disposition = 'r';
1785 rv = Manage_subdevs(ent->devnm, mdfd, &devlist,
1786 verbose, 0, NULL, 0);
1787 }
1788 close(mdfd);
1789 free_mdstat(ent);
1790 return rv;
1791 }