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