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