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