]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Incremental.c
Use action policy to keep recently-disconnected devices in the array.
[thirdparty/mdadm.git] / Incremental.c
CommitLineData
8382f19b
NB
1/*
2 * Incremental.c - support --incremental. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
4 *
e736b623 5 * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de>
8382f19b
NB
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
33static int count_active(struct supertype *st, int mdfd, char **availp,
34 struct mdinfo *info);
7e0f6979 35static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
8382f19b
NB
36 int number, __u64 events, int verbose,
37 char *array_name);
38
39int Incremental(char *devname, int verbose, int runstop,
0ac91628
N
40 struct supertype *st, char *homehost, int require_homehost,
41 int autof)
8382f19b
NB
42{
43 /* Add this device to an array, creating the array if necessary
598f0d58 44 * and starting the array if sensible or - if runstop>0 - if possible.
8382f19b
NB
45 *
46 * This has several steps:
47 *
48 * 1/ Check if device is permitted by mdadm.conf, reject if not.
49 * 2/ Find metadata, reject if none appropriate (check
50 * version/name from args)
51 * 3/ Check if there is a match in mdadm.conf
215bb3f7
N
52 * 3a/ if not, check for homehost match. If no match, assemble as
53 * a 'foreign' array.
8382f19b
NB
54 * 4/ Determine device number.
55 * - If in mdadm.conf with std name, use that
56 * - UUID in /var/run/mdadm.map use that
57 * - If name is suggestive, use that. unless in use with different uuid.
58 * - Choose a free, high number.
59 * - Use a partitioned device unless strong suggestion not to.
60 * e.g. auto=md
352452c3 61 * Don't choose partitioned for containers.
8382f19b
NB
62 * 5/ Find out if array already exists
63 * 5a/ if it does not
64 * - choose a name, from mdadm.conf or 'name' field in array.
65 * - create the array
66 * - add the device
67 * 5b/ if it does
68 * - check one drive in array to make sure metadata is a reasonably
69 * close match. Reject if not (e.g. different type)
70 * - add the device
71 * 6/ Make sure /var/run/mdadm.map contains this array.
72 * 7/ Is there enough devices to possibly start the array?
352452c3 73 * For a container, this means running Incremental_container.
8382f19b
NB
74 * 7a/ if not, finish with success.
75 * 7b/ if yes,
76 * - read all metadata and arrange devices like -A does
77 * - if number of OK devices match expected, or -R and there are enough,
78 * start the array (auto-readonly).
79 */
80 struct stat stb;
7e83544b 81 struct mdinfo info, dinfo;
8382f19b
NB
82 struct mddev_ident_s *array_list, *match;
83 char chosen_name[1024];
15d4a7e4 84 int rv = 1;
8382f19b 85 struct map_ent *mp, *map = NULL;
15d4a7e4 86 int dfd = -1, mdfd = -1;
8382f19b
NB
87 char *avail;
88 int active_disks;
215bb3f7 89 int trustworthy = FOREIGN;
d7288ddc 90 char *name_to_use;
ad5bc697 91 mdu_array_info_t ainf;
7e83544b 92 struct dev_policy *policy = NULL;
8382f19b
NB
93
94 struct createinfo *ci = conf_get_create_info();
95
8382f19b 96
352452c3 97 /* 1/ Check if device is permitted by mdadm.conf */
8382f19b
NB
98
99 if (!conf_test_dev(devname)) {
100 if (verbose >= 0)
101 fprintf(stderr, Name
102 ": %s not permitted by mdadm.conf.\n",
103 devname);
15d4a7e4 104 goto out;
8382f19b
NB
105 }
106
107 /* 2/ Find metadata, reject if none appropriate (check
108 * version/name from args) */
109
110 dfd = dev_open(devname, O_RDONLY|O_EXCL);
111 if (dfd < 0) {
112 if (verbose >= 0)
113 fprintf(stderr, Name ": cannot open %s: %s.\n",
114 devname, strerror(errno));
15d4a7e4 115 goto out;
8382f19b
NB
116 }
117 if (fstat(dfd, &stb) < 0) {
118 if (verbose >= 0)
119 fprintf(stderr, Name ": fstat failed for %s: %s.\n",
120 devname, strerror(errno));
15d4a7e4 121 goto out;
8382f19b
NB
122 }
123 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
124 if (verbose >= 0)
125 fprintf(stderr, Name ": %s is not a block device.\n",
126 devname);
15d4a7e4 127 goto out;
8382f19b
NB
128 }
129
7e83544b
N
130 dinfo.disk.major = major(stb.st_rdev);
131 dinfo.disk.minor = minor(stb.st_rdev);
132
133 policy = disk_policy(&dinfo);
134
8382f19b
NB
135 if (st == NULL && (st = guess_super(dfd)) == NULL) {
136 if (verbose >= 0)
137 fprintf(stderr, Name
138 ": no recognisable superblock on %s.\n",
139 devname);
15d4a7e4 140 goto out;
8382f19b 141 }
3da92f27 142 if (st->ss->load_super(st, dfd, NULL)) {
8382f19b
NB
143 if (verbose >= 0)
144 fprintf(stderr, Name ": no RAID superblock on %s.\n",
145 devname);
15d4a7e4 146 goto out;
8382f19b 147 }
15d4a7e4 148 close (dfd); dfd = -1;
8382f19b 149
f35f2525 150 memset(&info, 0, sizeof(info));
598f0d58 151 st->ss->getinfo_super(st, &info);
8382f19b
NB
152 /* 3/ Check if there is a match in mdadm.conf */
153
154 array_list = conf_get_ident(NULL);
155 match = NULL;
156 for (; array_list; array_list = array_list->next) {
157 if (array_list->uuid_set &&
158 same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid)
159 == 0) {
fe056d1f 160 if (verbose >= 2 && array_list->devname)
8382f19b
NB
161 fprintf(stderr, Name
162 ": UUID differs from %s.\n",
163 array_list->devname);
164 continue;
165 }
166 if (array_list->name[0] &&
167 strcasecmp(array_list->name, info.name) != 0) {
fe056d1f 168 if (verbose >= 2 && array_list->devname)
8382f19b
NB
169 fprintf(stderr, Name
170 ": Name differs from %s.\n",
171 array_list->devname);
172 continue;
173 }
174 if (array_list->devices &&
175 !match_oneof(array_list->devices, devname)) {
fe056d1f 176 if (verbose >= 2 && array_list->devname)
8382f19b
NB
177 fprintf(stderr, Name
178 ": Not a listed device for %s.\n",
179 array_list->devname);
180 continue;
181 }
182 if (array_list->super_minor != UnSet &&
183 array_list->super_minor != info.array.md_minor) {
fe056d1f 184 if (verbose >= 2 && array_list->devname)
8382f19b
NB
185 fprintf(stderr, Name
186 ": Different super-minor to %s.\n",
187 array_list->devname);
188 continue;
189 }
190 if (!array_list->uuid_set &&
191 !array_list->name[0] &&
192 !array_list->devices &&
193 array_list->super_minor == UnSet) {
fe056d1f 194 if (verbose >= 2 && array_list->devname)
8382f19b
NB
195 fprintf(stderr, Name
196 ": %s doesn't have any identifying information.\n",
197 array_list->devname);
198 continue;
199 }
200 /* FIXME, should I check raid_disks and level too?? */
201
202 if (match) {
fe056d1f
N
203 if (verbose >= 0) {
204 if (match->devname && array_list->devname)
205 fprintf(stderr, Name
8382f19b 206 ": we match both %s and %s - cannot decide which to use.\n",
fe056d1f
N
207 match->devname, array_list->devname);
208 else
209 fprintf(stderr, Name
210 ": multiple lines in mdadm.conf match\n");
211 }
15d4a7e4
N
212 rv = 2;
213 goto out;
8382f19b
NB
214 }
215 match = array_list;
216 }
217
112cace6
N
218 if (match && match->devname
219 && strcasecmp(match->devname, "<ignore>") == 0) {
220 if (verbose >= 0)
221 fprintf(stderr, Name ": array containing %s is explicitly"
222 " ignored by mdadm.conf\n",
223 devname);
15d4a7e4 224 goto out;
112cace6
N
225 }
226
7b403fef
N
227 /* 3a/ if not, check for homehost match. If no match, continue
228 * but don't trust the 'name' in the array. Thus a 'random' minor
229 * number will be assigned, and the device name will be based
230 * on that. */
215bb3f7
N
231 if (match)
232 trustworthy = LOCAL;
d1d3482b
N
233 else if (st->ss->match_home(st, homehost) == 1)
234 trustworthy = LOCAL;
235 else if (st->ss->match_home(st, "any") == 1)
236 trustworthy = LOCAL_ANY;
215bb3f7 237 else
d1d3482b
N
238 trustworthy = FOREIGN;
239
240
241 if (!match && !conf_test_metadata(st->ss->name,
242 (trustworthy == LOCAL))) {
243 if (verbose >= 1)
244 fprintf(stderr, Name
245 ": %s has metadata type %s for which "
246 "auto-assembly is disabled\n",
247 devname, st->ss->name);
15d4a7e4 248 goto out;
d1d3482b
N
249 }
250 if (trustworthy == LOCAL_ANY)
ecb02e31 251 trustworthy = LOCAL;
e12e1d1d 252
4ef2f11e
N
253 /* There are three possible sources for 'autof': command line,
254 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
350ac35d
N
255 * ARRAY takes precedence, then command line, then
256 * CREATE.
4ef2f11e 257 */
350ac35d 258 if (match && match->autof)
4ef2f11e
N
259 autof = match->autof;
260 if (autof == 0)
261 autof = ci->autof;
262
215bb3f7 263 if (st->ss->container_content && st->loaded_container) {
97b4d0e9
DW
264 if ((runstop > 0 && info.container_enough >= 0) ||
265 info.container_enough > 0)
266 /* pass */;
267 else {
268 if (verbose)
269 fprintf(stderr, Name ": not enough devices to start the container\n");
15d4a7e4
N
270 rv = 0;
271 goto out;
97b4d0e9
DW
272 }
273
215bb3f7
N
274 /* This is a pre-built container array, so we do something
275 * rather different.
276 */
15d4a7e4 277 rv = Incremental_container(st, devname, verbose, runstop,
215bb3f7 278 autof, trustworthy);
15d4a7e4 279 goto out;
8382f19b 280 }
ecb02e31 281
7cdc0872 282 name_to_use = info.name;
0ac91628 283 if (name_to_use[0] == 0 &&
ecb02e31
N
284 info.array.level == LEVEL_CONTAINER &&
285 trustworthy == LOCAL) {
286 name_to_use = info.text_version;
287 trustworthy = METADATA;
288 }
0ac91628
N
289 if (name_to_use[0] && trustworthy != LOCAL &&
290 ! require_homehost &&
291 conf_name_is_free(name_to_use))
292 trustworthy = LOCAL;
ecb02e31 293
7cdc0872
N
294 /* strip "hostname:" prefix from name if we have decided
295 * to treat it as LOCAL
296 */
297 if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL)
298 name_to_use = strchr(name_to_use, ':')+1;
299
ad5bc697 300 /* 4/ Check if array exists.
215bb3f7 301 */
93c861ee
DL
302 if (map_lock(&map))
303 fprintf(stderr, Name ": failed to get exclusive lock on "
304 "mapfile\n");
215bb3f7 305 mp = map_by_uuid(&map, info.uuid);
339c2d6c
N
306 if (mp)
307 mdfd = open_dev(mp->devnum);
308 else
215bb3f7 309 mdfd = -1;
d7288ddc 310
8382f19b 311 if (mdfd < 0) {
7e0f6979 312 struct mdinfo *sra;
8382f19b 313
215bb3f7 314 /* Couldn't find an existing array, maybe make a new one */
ffb3e494 315 mdfd = create_mddev(match ? match->devname : NULL,
e12e1d1d 316 name_to_use, autof, trustworthy, chosen_name);
215bb3f7
N
317
318 if (mdfd < 0)
15d4a7e4 319 goto out;
215bb3f7
N
320
321 sysfs_init(&info, mdfd, 0);
322
f35f2525
N
323 if (set_array_info(mdfd, st, &info) != 0) {
324 fprintf(stderr, Name ": failed to set array info for %s: %s\n",
8382f19b 325 chosen_name, strerror(errno));
15d4a7e4
N
326 rv = 2;
327 goto out;
8382f19b 328 }
7801ac20 329
c5afc314
N
330 dinfo = info;
331 dinfo.disk.major = major(stb.st_rdev);
332 dinfo.disk.minor = minor(stb.st_rdev);
333 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
8382f19b
NB
334 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
335 devname, chosen_name, strerror(errno));
336 ioctl(mdfd, STOP_ARRAY, 0);
15d4a7e4
N
337 rv = 2;
338 goto out;
8382f19b 339 }
197e3eb6 340 sra = sysfs_read(mdfd, fd2devnum(mdfd), GET_DEVS);
06c7f68e 341 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
8382f19b
NB
342 /* It really should be 'none' - must be old buggy
343 * kernel, and mdadm -I may not be able to complete.
344 * So reject it.
345 */
346 ioctl(mdfd, STOP_ARRAY, NULL);
347 fprintf(stderr, Name
348 ": You have an old buggy kernel which cannot support\n"
349 " --incremental reliably. Aborting.\n");
8382f19b 350 sysfs_free(sra);
15d4a7e4
N
351 rv = 2;
352 goto out;
8382f19b 353 }
c5afc314 354 info.array.working_disks = 1;
f35f2525 355 sysfs_free(sra);
ad5bc697
N
356 /* 6/ Make sure /var/run/mdadm.map contains this array. */
357 map_update(&map, fd2devnum(mdfd),
358 info.text_version,
359 info.uuid, chosen_name);
8382f19b
NB
360 } else {
361 /* 5b/ if it does */
362 /* - check one drive in array to make sure metadata is a reasonably */
363 /* close match. Reject if not (e.g. different type) */
364 /* - add the device */
365 char dn[20];
366 int dfd2;
8382f19b 367 int err;
7e0f6979 368 struct mdinfo *sra;
3da92f27 369 struct supertype *st2;
c5afc314 370 struct mdinfo info2, *d;
215bb3f7 371
339c2d6c
N
372 if (mp->path)
373 strcpy(chosen_name, mp->path);
374 else
375 strcpy(chosen_name, devnum2devname(mp->devnum));
215bb3f7 376
ef83fe7c
N
377 /* It is generally not OK to add non-spare drives to a
378 * running array as they are probably missing because
379 * they failed. However if runstop is 1, then the
7e83544b
N
380 * array was possibly started early and our best bet is
381 * to add this anyway.
382 * Also if action policy is re-add or better we allow
383 * re-add
3a6ec29a 384 */
ef83fe7c 385 if ((info.disk.state & (1<<MD_DISK_SYNC)) != 0
7e83544b
N
386 && ! policy_action_allows(policy, st->ss->name,
387 act_re_add)
ef83fe7c 388 && runstop < 1) {
1dccfff9
DW
389 int active = 0;
390
391 if (st->ss->external) {
392 char *devname = devnum2devname(fd2devnum(mdfd));
393
394 active = devname && is_container_active(devname);
395 free(devname);
396 } else if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0)
397 active = 1;
398 if (active) {
3a6ec29a
N
399 fprintf(stderr, Name
400 ": not adding %s to active array (without --run) %s\n",
401 devname, chosen_name);
15d4a7e4
N
402 rv = 2;
403 goto out;
3a6ec29a
N
404 }
405 }
197e3eb6 406 sra = sysfs_read(mdfd, fd2devnum(mdfd), (GET_DEVS | GET_STATE));
15d4a7e4
N
407 if (!sra) {
408 rv = 2;
409 goto out;
410 }
7c548327
N
411 if (sra->devs) {
412 sprintf(dn, "%d:%d", sra->devs->disk.major,
413 sra->devs->disk.minor);
414 dfd2 = dev_open(dn, O_RDONLY);
415 st2 = dup_super(st);
416 if (st2->ss->load_super(st2, dfd2, NULL) ||
417 st->ss->compare_super(st, st2) != 0) {
418 fprintf(stderr, Name
419 ": metadata mismatch between %s and "
420 "chosen array %s\n",
421 devname, chosen_name);
7c548327 422 close(dfd2);
15d4a7e4
N
423 rv = 2;
424 goto out;
7c548327 425 }
8382f19b 426 close(dfd2);
7c548327
N
427 memset(&info2, 0, sizeof(info2));
428 st2->ss->getinfo_super(st2, &info2);
429 st2->ss->free_super(st2);
430 if (info.array.level != info2.array.level ||
431 memcmp(info.uuid, info2.uuid, 16) != 0 ||
432 info.array.raid_disks != info2.array.raid_disks) {
433 fprintf(stderr, Name
434 ": unexpected difference between %s and %s.\n",
435 chosen_name, devname);
15d4a7e4
N
436 rv = 2;
437 goto out;
7c548327 438 }
8382f19b 439 }
7801ac20
N
440 info2.disk.major = major(stb.st_rdev);
441 info2.disk.minor = minor(stb.st_rdev);
c5afc314
N
442 /* add disk needs to know about containers */
443 if (st->ss->external)
444 sra->array.level = LEVEL_CONTAINER;
ac7de9d9 445 err = add_disk(mdfd, st, sra, &info2);
8382f19b
NB
446 if (err < 0 && errno == EBUSY) {
447 /* could be another device present with the same
448 * disk.number. Find and reject any such
449 */
450 find_reject(mdfd, st, sra, info.disk.number,
451 info.events, verbose, chosen_name);
ac7de9d9 452 err = add_disk(mdfd, st, sra, &info2);
8382f19b
NB
453 }
454 if (err < 0) {
455 fprintf(stderr, Name ": failed to add %s to %s: %s.\n",
456 devname, chosen_name, strerror(errno));
15d4a7e4
N
457 rv = 2;
458 goto out;
8382f19b 459 }
c5afc314
N
460 info.array.working_disks = 0;
461 for (d = sra->devs; d; d=d->next)
462 info.array.working_disks ++;
463
8382f19b 464 }
8382f19b
NB
465
466 /* 7/ Is there enough devices to possibly start the array? */
467 /* 7a/ if not, finish with success. */
352452c3
N
468 if (info.array.level == LEVEL_CONTAINER) {
469 /* Try to assemble within the container */
ad5bc697 470 map_unlock(&map);
97590376 471 sysfs_uevent(&info, "change");
c5afc314
N
472 if (verbose >= 0)
473 fprintf(stderr, Name
474 ": container %s now has %d devices\n",
475 chosen_name, info.array.working_disks);
a7c6e3fb
N
476 wait_for(chosen_name, mdfd);
477 close(mdfd);
03b7f6c6 478 rv = Incremental(chosen_name, verbose, runstop,
0ac91628 479 NULL, homehost, require_homehost, autof);
03b7f6c6
N
480 if (rv == 1)
481 /* Don't fail the whole -I if a subarray didn't
482 * have enough devices to start yet
483 */
484 rv = 0;
485 return rv;
352452c3 486 }
f8409e54 487 avail = NULL;
8382f19b
NB
488 active_disks = count_active(st, mdfd, &avail, &info);
489 if (enough(info.array.level, info.array.raid_disks,
490 info.array.layout, info.array.state & 1,
3288b419 491 avail, active_disks) == 0) {
8382f19b
NB
492 free(avail);
493 if (verbose >= 0)
494 fprintf(stderr, Name
495 ": %s attached to %s, not enough to start (%d).\n",
496 devname, chosen_name, active_disks);
ad5bc697 497 map_unlock(&map);
15d4a7e4
N
498 rv = 0;
499 goto out;
8382f19b
NB
500 }
501 free(avail);
502
503 /* 7b/ if yes, */
504 /* - if number of OK devices match expected, or -R and there */
505 /* are enough, */
506 /* + add any bitmap file */
507 /* + start the array (auto-readonly). */
8382f19b
NB
508
509 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
510 if (verbose >= 0)
511 fprintf(stderr, Name
512 ": %s attached to %s which is already active.\n",
513 devname, chosen_name);
ad5bc697 514 map_unlock(&map);
15d4a7e4
N
515 rv = 0;
516 goto out;
8382f19b 517 }
ad5bc697
N
518
519 map_unlock(&map);
8382f19b 520 if (runstop > 0 || active_disks >= info.array.working_disks) {
7e83544b 521 struct mdinfo *sra, *dsk;
8382f19b
NB
522 /* Let's try to start it */
523 if (match && match->bitmap_file) {
524 int bmfd = open(match->bitmap_file, O_RDWR);
525 if (bmfd < 0) {
526 fprintf(stderr, Name
527 ": Could not open bitmap file %s.\n",
528 match->bitmap_file);
15d4a7e4 529 goto out;
8382f19b
NB
530 }
531 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
532 close(bmfd);
533 fprintf(stderr, Name
534 ": Failed to set bitmapfile for %s.\n",
535 chosen_name);
15d4a7e4 536 goto out;
8382f19b
NB
537 }
538 close(bmfd);
539 }
7e83544b
N
540 /* GET_* needed so add_disk works below */
541 sra = sysfs_read(mdfd, fd2devnum(mdfd),
542 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
7b403fef 543 if ((sra == NULL || active_disks >= info.array.working_disks)
215bb3f7 544 && trustworthy != FOREIGN)
8382f19b
NB
545 rv = ioctl(mdfd, RUN_ARRAY, NULL);
546 else
547 rv = sysfs_set_str(sra, NULL,
548 "array_state", "read-auto");
549 if (rv == 0) {
550 if (verbose >= 0)
551 fprintf(stderr, Name
7e83544b 552 ": %s attached to %s, which has been started.\n",
8382f19b
NB
553 devname, chosen_name);
554 rv = 0;
a7c6e3fb 555 wait_for(chosen_name, mdfd);
7e83544b
N
556 /* We just started the array, so some devices
557 * might have been evicted from the array
558 * because their event counts were too old.
559 * If the action=re-add policy is in-force for
560 * those devices we should re-add them now.
561 */
562 for (dsk = sra->devs; dsk ; dsk = dsk->next) {
563 if (disk_action_allows(dsk, st->ss->name, act_re_add) &&
564 add_disk(mdfd, st, sra, dsk) == 0)
565 fprintf(stderr, Name
566 ": %s re-added to %s\n",
567 dsk->sys_name, chosen_name);
568 }
8382f19b
NB
569 } else {
570 fprintf(stderr, Name
571 ": %s attached to %s, but failed to start: %s.\n",
572 devname, chosen_name, strerror(errno));
573 rv = 1;
574 }
575 } else {
576 if (verbose >= 0)
577 fprintf(stderr, Name
578 ": %s attached to %s, not enough to start safely.\n",
579 devname, chosen_name);
580 rv = 0;
581 }
15d4a7e4
N
582out:
583 if (dfd >= 0)
584 close(dfd);
585 if (mdfd >= 0)
586 close(mdfd);
7e83544b
N
587 if (policy)
588 dev_policy_free(policy);
8382f19b
NB
589 return rv;
590}
591
7e0f6979 592static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
8382f19b
NB
593 int number, __u64 events, int verbose,
594 char *array_name)
595{
3da92f27 596 /* Find a device attached to this array with a disk.number of number
8382f19b
NB
597 * and events less than the passed events, and remove the device.
598 */
06c7f68e 599 struct mdinfo *d;
8382f19b
NB
600 mdu_array_info_t ra;
601
602 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
603 return; /* not safe to remove from active arrays
604 * without thinking more */
605
606 for (d = sra->devs; d ; d = d->next) {
607 char dn[10];
608 int dfd;
8382f19b 609 struct mdinfo info;
06c7f68e 610 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
611 dfd = dev_open(dn, O_RDONLY);
612 if (dfd < 0)
613 continue;
3da92f27 614 if (st->ss->load_super(st, dfd, NULL)) {
8382f19b
NB
615 close(dfd);
616 continue;
617 }
3da92f27
NB
618 st->ss->getinfo_super(st, &info);
619 st->ss->free_super(st);
8382f19b
NB
620 close(dfd);
621
622 if (info.disk.number != number ||
623 info.events >= events)
624 continue;
625
06c7f68e 626 if (d->disk.raid_disk > -1)
8382f19b
NB
627 sysfs_set_str(sra, d, "slot", "none");
628 if (sysfs_set_str(sra, d, "state", "remove") == 0)
629 if (verbose >= 0)
630 fprintf(stderr, Name
631 ": removing old device %s from %s\n",
06c7f68e 632 d->sys_name+4, array_name);
8382f19b
NB
633 }
634}
635
636static int count_active(struct supertype *st, int mdfd, char **availp,
637 struct mdinfo *bestinfo)
638{
639 /* count how many devices in sra think they are active */
06c7f68e 640 struct mdinfo *d;
8382f19b
NB
641 int cnt = 0, cnt1 = 0;
642 __u64 max_events = 0;
7e0f6979 643 struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE);
8382f19b
NB
644 char *avail = NULL;
645
b526e52d
DW
646 if (!sra)
647 return 0;
648
8382f19b
NB
649 for (d = sra->devs ; d ; d = d->next) {
650 char dn[30];
651 int dfd;
8382f19b
NB
652 int ok;
653 struct mdinfo info;
654
06c7f68e 655 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
656 dfd = dev_open(dn, O_RDONLY);
657 if (dfd < 0)
658 continue;
3da92f27 659 ok = st->ss->load_super(st, dfd, NULL);
8382f19b
NB
660 close(dfd);
661 if (ok != 0)
662 continue;
3da92f27 663 st->ss->getinfo_super(st, &info);
43aaf431
DL
664 if (!avail) {
665 avail = malloc(info.array.raid_disks);
666 if (!avail) {
667 fprintf(stderr, Name ": out of memory.\n");
668 exit(1);
669 }
670 memset(avail, 0, info.array.raid_disks);
671 *availp = avail;
672 }
673
8382f19b
NB
674 if (info.disk.state & (1<<MD_DISK_SYNC))
675 {
8382f19b
NB
676 if (cnt == 0) {
677 cnt++;
678 max_events = info.events;
679 avail[info.disk.raid_disk] = 2;
3da92f27 680 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
681 } else if (info.events == max_events) {
682 cnt++;
683 avail[info.disk.raid_disk] = 2;
684 } else if (info.events == max_events-1) {
685 cnt1++;
686 avail[info.disk.raid_disk] = 1;
687 } else if (info.events < max_events - 1)
688 ;
689 else if (info.events == max_events+1) {
690 int i;
691 cnt1 = cnt;
692 cnt = 1;
693 max_events = info.events;
694 for (i=0; i<info.array.raid_disks; i++)
695 if (avail[i])
696 avail[i]--;
697 avail[info.disk.raid_disk] = 2;
3da92f27 698 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
699 } else { /* info.events much bigger */
700 cnt = 1; cnt1 = 0;
701 memset(avail, 0, info.disk.raid_disk);
702 max_events = info.events;
3da92f27 703 st->ss->getinfo_super(st, bestinfo);
8382f19b
NB
704 }
705 }
3da92f27 706 st->ss->free_super(st);
8382f19b
NB
707 }
708 return cnt + cnt1;
709}
710
8382f19b
NB
711int IncrementalScan(int verbose)
712{
713 /* look at every device listed in the 'map' file.
714 * If one is found that is not running then:
715 * look in mdadm.conf for bitmap file.
716 * if one exists, but array has none, add it.
717 * try to start array in auto-readonly mode
718 */
719 struct map_ent *mapl = NULL;
720 struct map_ent *me;
721 mddev_ident_t devs, mddev;
722 int rv = 0;
723
724 map_read(&mapl);
725 devs = conf_get_ident(NULL);
726
727 for (me = mapl ; me ; me = me->next) {
8382f19b
NB
728 mdu_array_info_t array;
729 mdu_bitmap_file_t bmf;
7e0f6979 730 struct mdinfo *sra;
339c2d6c 731 int mdfd = open_dev(me->devnum);
215bb3f7 732
8382f19b
NB
733 if (mdfd < 0)
734 continue;
735 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
736 errno != ENODEV) {
737 close(mdfd);
738 continue;
739 }
740 /* Ok, we can try this one. Maybe it needs a bitmap */
741 for (mddev = devs ; mddev ; mddev = mddev->next)
339c2d6c 742 if (mddev->devname && me->path
2400e6eb 743 && devname_matches(mddev->devname, me->path))
8382f19b
NB
744 break;
745 if (mddev && mddev->bitmap_file) {
746 /*
747 * Note: early kernels will wrongly fail this, so it
748 * is a hint only
749 */
750 int added = -1;
751 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
752 int bmfd = open(mddev->bitmap_file, O_RDWR);
753 if (bmfd >= 0) {
754 added = ioctl(mdfd, SET_BITMAP_FILE,
755 bmfd);
756 close(bmfd);
757 }
758 }
759 if (verbose >= 0) {
760 if (added == 0)
761 fprintf(stderr, Name
762 ": Added bitmap %s to %s\n",
763 mddev->bitmap_file, me->path);
764 else if (errno != EEXIST)
765 fprintf(stderr, Name
766 ": Failed to add bitmap to %s: %s\n",
767 me->path, strerror(errno));
768 }
769 }
770 sra = sysfs_read(mdfd, 0, 0);
771 if (sra) {
772 if (sysfs_set_str(sra, NULL,
773 "array_state", "read-auto") == 0) {
774 if (verbose >= 0)
775 fprintf(stderr, Name
776 ": started array %s\n",
339c2d6c 777 me->path ?: devnum2devname(me->devnum));
8382f19b
NB
778 } else {
779 fprintf(stderr, Name
780 ": failed to start array %s: %s\n",
339c2d6c
N
781 me->path ?: devnum2devname(me->devnum),
782 strerror(errno));
8382f19b
NB
783 rv = 1;
784 }
785 }
786 }
787 return rv;
788}
598f0d58 789
1771a6e2
N
790static char *container2devname(char *devname)
791{
792 char *mdname = NULL;
793
794 if (devname[0] == '/') {
795 int fd = open(devname, O_RDONLY);
796 if (fd >= 0) {
797 mdname = devnum2devname(fd2devnum(fd));
798 close(fd);
799 }
800 } else {
801 int uuid[4];
802 struct map_ent *mp, *map = NULL;
803
804 if (!parse_uuid(devname, uuid))
805 return mdname;
806 mp = map_by_uuid(&map, uuid);
807 if (mp)
808 mdname = devnum2devname(mp->devnum);
809 map_free(map);
810 }
811
812 return mdname;
813}
814
598f0d58 815int Incremental_container(struct supertype *st, char *devname, int verbose,
215bb3f7 816 int runstop, int autof, int trustworthy)
598f0d58
NB
817{
818 /* Collect the contents of this container and for each
819 * array, choose a device name and assemble the array.
820 */
821
822 struct mdinfo *list = st->ss->container_content(st);
823 struct mdinfo *ra;
ad5bc697
N
824 struct map_ent *map = NULL;
825
93c861ee
DL
826 if (map_lock(&map))
827 fprintf(stderr, Name ": failed to get exclusive lock on "
828 "mapfile\n");
598f0d58
NB
829
830 for (ra = list ; ra ; ra = ra->next) {
598f0d58
NB
831 int mdfd;
832 char chosen_name[1024];
ad5bc697 833 struct map_ent *mp;
dbb44303 834 struct mddev_ident_s *match = NULL;
598f0d58 835
c5afc314 836 mp = map_by_uuid(&map, ra->uuid);
d7288ddc 837
30926600
N
838 if (mp) {
839 mdfd = open_dev(mp->devnum);
339c2d6c
N
840 if (mp->path)
841 strcpy(chosen_name, mp->path);
842 else
843 strcpy(chosen_name, devnum2devname(mp->devnum));
30926600 844 } else {
dbb44303 845
112cace6 846 /* Check in mdadm.conf for container == devname and
dbb44303
N
847 * member == ra->text_version after second slash.
848 */
849 char *sub = strchr(ra->text_version+1, '/');
850 struct mddev_ident_s *array_list;
851 if (sub) {
852 sub++;
853 array_list = conf_get_ident(NULL);
854 } else
855 array_list = NULL;
856 for(; array_list ; array_list = array_list->next) {
dbb44303
N
857 char *dn;
858 if (array_list->member == NULL ||
859 array_list->container == NULL)
860 continue;
861 if (strcmp(array_list->member, sub) != 0)
862 continue;
71d60c48
DW
863 if (array_list->uuid_set &&
864 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
865 continue;
1771a6e2
N
866 dn = container2devname(array_list->container);
867 if (dn == NULL)
dbb44303 868 continue;
dbb44303
N
869 if (strncmp(dn, ra->text_version+1,
870 strlen(dn)) != 0 ||
871 ra->text_version[strlen(dn)+1] != '/') {
872 free(dn);
873 continue;
874 }
875 free(dn);
876 /* we have a match */
877 match = array_list;
71d60c48
DW
878 if (verbose>0)
879 fprintf(stderr, Name ": match found for member %s\n",
880 array_list->member);
dbb44303
N
881 break;
882 }
dbb44303 883
112cace6
N
884 if (match && match->devname &&
885 strcasecmp(match->devname, "<ignore>") == 0) {
886 if (verbose > 0)
887 fprintf(stderr, Name ": array %s/%s is "
888 "explicitly ignored by mdadm.conf\n",
889 match->container, match->member);
890 return 2;
891 }
05833051
N
892 if (match)
893 trustworthy = LOCAL;
112cace6 894
30926600
N
895 mdfd = create_mddev(match ? match->devname : NULL,
896 ra->name,
897 autof,
898 trustworthy,
899 chosen_name);
900 }
598f0d58
NB
901
902 if (mdfd < 0) {
903 fprintf(stderr, Name ": failed to open %s: %s.\n",
904 chosen_name, strerror(errno));
905 return 2;
906 }
907
03b7f6c6
N
908 assemble_container_content(st, mdfd, ra, runstop,
909 chosen_name, verbose);
598f0d58 910 }
ad5bc697 911 map_unlock(&map);
598f0d58
NB
912 return 0;
913}
29ba4804
N
914
915/*
916 * IncrementalRemove - Attempt to see if the passed in device belongs to any
917 * raid arrays, and if so first fail (if needed) and then remove the device.
918 *
919 * @devname - The device we want to remove
920 *
921 * Note: the device name must be a kernel name like "sda", so
922 * that we can find it in /proc/mdstat
923 */
924int IncrementalRemove(char *devname, int verbose)
925{
926 int mdfd;
aae3cdc3 927 int rv;
29ba4804
N
928 struct mdstat_ent *ent;
929 struct mddev_dev_s devlist;
930
931 if (strchr(devname, '/')) {
932 fprintf(stderr, Name ": incremental removal requires a "
933 "kernel device name, not a file: %s\n", devname);
934 return 1;
935 }
936 ent = mdstat_by_component(devname);
937 if (!ent) {
938 fprintf(stderr, Name ": %s does not appear to be a component "
939 "of any array\n", devname);
940 return 1;
941 }
942 mdfd = open_dev(ent->devnum);
943 if (mdfd < 0) {
944 fprintf(stderr, Name ": Cannot open array %s!!\n", ent->dev);
945 return 1;
946 }
947 memset(&devlist, 0, sizeof(devlist));
948 devlist.devname = devname;
949 devlist.disposition = 'f';
7d2e6486 950 Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0);
29ba4804 951 devlist.disposition = 'r';
aae3cdc3
PC
952 rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0);
953 close(mdfd);
954 return rv;
29ba4804 955}