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