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