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