]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Incremental.c
Create: support --readonly flag.
[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"
61018da0
N
32#include <dirent.h>
33#include <ctype.h>
8382f19b 34
2bf740b3
N
35static int count_active(struct supertype *st, struct mdinfo *sra,
36 int mdfd, char **availp,
8382f19b 37 struct mdinfo *info);
7e0f6979 38static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
8382f19b
NB
39 int number, __u64 events, int verbose,
40 char *array_name);
f08605b3 41static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
d2db3045 42 struct map_ent *target,
f08605b3 43 struct supertype *st, int verbose);
8382f19b 44
7d91c3f5 45static int Incremental_container(struct supertype *st, char *devname,
47c74f3f 46 char *homehost,
0ea8f5b1
N
47 int verbose, int runstop,
48 int readonly, int autof,
b76b30e0 49 int freeze_reshape);
7d91c3f5 50
8382f19b 51int Incremental(char *devname, int verbose, int runstop,
0ac91628 52 struct supertype *st, char *homehost, int require_homehost,
b76b30e0 53 int autof, int freeze_reshape)
8382f19b
NB
54{
55 /* Add this device to an array, creating the array if necessary
598f0d58 56 * and starting the array if sensible or - if runstop>0 - if possible.
8382f19b
NB
57 *
58 * This has several steps:
59 *
60 * 1/ Check if device is permitted by mdadm.conf, reject if not.
61 * 2/ Find metadata, reject if none appropriate (check
62 * version/name from args)
63 * 3/ Check if there is a match in mdadm.conf
215bb3f7
N
64 * 3a/ if not, check for homehost match. If no match, assemble as
65 * a 'foreign' array.
8382f19b
NB
66 * 4/ Determine device number.
67 * - If in mdadm.conf with std name, use that
68 * - UUID in /var/run/mdadm.map use that
69 * - If name is suggestive, use that. unless in use with different uuid.
70 * - Choose a free, high number.
71 * - Use a partitioned device unless strong suggestion not to.
72 * e.g. auto=md
352452c3 73 * Don't choose partitioned for containers.
8382f19b
NB
74 * 5/ Find out if array already exists
75 * 5a/ if it does not
76 * - choose a name, from mdadm.conf or 'name' field in array.
77 * - create the array
78 * - add the device
79 * 5b/ if it does
80 * - check one drive in array to make sure metadata is a reasonably
81 * close match. Reject if not (e.g. different type)
82 * - add the device
83 * 6/ Make sure /var/run/mdadm.map contains this array.
84 * 7/ Is there enough devices to possibly start the array?
352452c3 85 * For a container, this means running Incremental_container.
8382f19b
NB
86 * 7a/ if not, finish with success.
87 * 7b/ if yes,
88 * - read all metadata and arrange devices like -A does
89 * - if number of OK devices match expected, or -R and there are enough,
90 * start the array (auto-readonly).
91 */
92 struct stat stb;
7e83544b 93 struct mdinfo info, dinfo;
de6ae750 94 struct mdinfo *sra = NULL, *d;
fa56eddb 95 struct mddev_ident *match;
8382f19b 96 char chosen_name[1024];
15d4a7e4 97 int rv = 1;
8382f19b 98 struct map_ent *mp, *map = NULL;
15d4a7e4 99 int dfd = -1, mdfd = -1;
de6ae750 100 char *avail = NULL;
8382f19b 101 int active_disks;
47c74f3f 102 int trustworthy;
d7288ddc 103 char *name_to_use;
ad5bc697 104 mdu_array_info_t ainf;
7e83544b 105 struct dev_policy *policy = NULL;
d2db3045
N
106 struct map_ent target_array;
107 int have_target;
8382f19b
NB
108
109 struct createinfo *ci = conf_get_create_info();
110
47c74f3f
N
111 if (stat(devname, &stb) < 0) {
112 if (verbose >= 0)
e7b84f9d 113 pr_err("stat failed for %s: %s.\n",
47c74f3f
N
114 devname, strerror(errno));
115 return rv;
116 }
117 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
118 if (verbose >= 0)
e7b84f9d 119 pr_err("%s is not a block device.\n",
47c74f3f
N
120 devname);
121 return rv;
122 }
123 dfd = dev_open(devname, O_RDONLY|O_EXCL);
124 if (dfd < 0) {
125 if (verbose >= 0)
e7b84f9d 126 pr_err("cannot open %s: %s.\n",
47c74f3f
N
127 devname, strerror(errno));
128 return rv;
129 }
130 /* If the device is a container, we do something very different */
3a371610 131 if (must_be_container(dfd)) {
47c74f3f
N
132 if (!st)
133 st = super_by_fd(dfd, NULL);
a92b2112 134 if (st && st->ss->load_container)
47c74f3f
N
135 rv = st->ss->load_container(st, dfd, NULL);
136
137 close(dfd);
5fc8cff3
JS
138 if (!rv && st->ss->container_content) {
139 if (map_lock(&map))
e7b84f9d
N
140 pr_err("failed to get "
141 "exclusive lock on mapfile\n");
5fc8cff3 142 rv = Incremental_container(st, devname, homehost,
0ea8f5b1
N
143 verbose, runstop,
144 0, autof,
5fc8cff3
JS
145 freeze_reshape);
146 map_unlock(&map);
147 return rv;
148 }
47c74f3f 149
e7b84f9d 150 pr_err("%s is not part of an md array.\n",
47c74f3f
N
151 devname);
152 return rv;
153 }
8382f19b 154
352452c3 155 /* 1/ Check if device is permitted by mdadm.conf */
8382f19b
NB
156
157 if (!conf_test_dev(devname)) {
158 if (verbose >= 0)
e7b84f9d
N
159 pr_err("%s not permitted by mdadm.conf.\n",
160 devname);
15d4a7e4 161 goto out;
8382f19b
NB
162 }
163
164 /* 2/ Find metadata, reject if none appropriate (check
165 * version/name from args) */
166
8382f19b
NB
167 if (fstat(dfd, &stb) < 0) {
168 if (verbose >= 0)
e7b84f9d 169 pr_err("fstat failed for %s: %s.\n",
8382f19b 170 devname, strerror(errno));
15d4a7e4 171 goto out;
8382f19b
NB
172 }
173 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
174 if (verbose >= 0)
e7b84f9d 175 pr_err("%s is not a block device.\n",
8382f19b 176 devname);
15d4a7e4 177 goto out;
8382f19b
NB
178 }
179
7e83544b
N
180 dinfo.disk.major = major(stb.st_rdev);
181 dinfo.disk.minor = minor(stb.st_rdev);
182
183 policy = disk_policy(&dinfo);
d2db3045 184 have_target = policy_check_path(&dinfo, &target_array);
7e83544b 185
8382f19b
NB
186 if (st == NULL && (st = guess_super(dfd)) == NULL) {
187 if (verbose >= 0)
e7b84f9d
N
188 pr_err("no recognisable superblock on %s.\n",
189 devname);
d2db3045
N
190 rv = try_spare(devname, &dfd, policy,
191 have_target ? &target_array : NULL,
192 st, verbose);
15d4a7e4 193 goto out;
8382f19b 194 }
0f22b998
N
195 if (st->ss->compare_super == NULL ||
196 st->ss->load_super(st, dfd, NULL)) {
8382f19b 197 if (verbose >= 0)
e7b84f9d 198 pr_err("no RAID superblock on %s.\n",
8382f19b 199 devname);
d2db3045
N
200 rv = try_spare(devname, &dfd, policy,
201 have_target ? &target_array : NULL,
202 st, verbose);
0f22b998 203 free(st);
15d4a7e4 204 goto out;
8382f19b 205 }
15d4a7e4 206 close (dfd); dfd = -1;
8382f19b 207
a5d85af7 208 st->ss->getinfo_super(st, &info, NULL);
8382f19b 209
3a97f210 210 /* 3/ Check if there is a match in mdadm.conf */
2244d1a9 211 match = conf_match(st, &info, devname, verbose, &rv);
3a97f210
N
212 if (!match && rv == 2)
213 goto out;
8382f19b 214
112cace6
N
215 if (match && match->devname
216 && strcasecmp(match->devname, "<ignore>") == 0) {
217 if (verbose >= 0)
e7b84f9d 218 pr_err("array containing %s is explicitly"
112cace6
N
219 " ignored by mdadm.conf\n",
220 devname);
15d4a7e4 221 goto out;
112cace6
N
222 }
223
7b403fef
N
224 /* 3a/ if not, check for homehost match. If no match, continue
225 * but don't trust the 'name' in the array. Thus a 'random' minor
226 * number will be assigned, and the device name will be based
227 * on that. */
215bb3f7
N
228 if (match)
229 trustworthy = LOCAL;
d1d3482b
N
230 else if (st->ss->match_home(st, homehost) == 1)
231 trustworthy = LOCAL;
232 else if (st->ss->match_home(st, "any") == 1)
233 trustworthy = LOCAL_ANY;
215bb3f7 234 else
d1d3482b
N
235 trustworthy = FOREIGN;
236
237
4e8d9f0a 238 if (!match && !conf_test_metadata(st->ss->name, policy,
d1d3482b
N
239 (trustworthy == LOCAL))) {
240 if (verbose >= 1)
e7b84f9d
N
241 pr_err("%s has metadata type %s for which "
242 "auto-assembly is disabled\n",
243 devname, st->ss->name);
15d4a7e4 244 goto out;
d1d3482b
N
245 }
246 if (trustworthy == LOCAL_ANY)
ecb02e31 247 trustworthy = LOCAL;
e12e1d1d 248
4ef2f11e
N
249 /* There are three possible sources for 'autof': command line,
250 * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf.
350ac35d
N
251 * ARRAY takes precedence, then command line, then
252 * CREATE.
4ef2f11e 253 */
350ac35d 254 if (match && match->autof)
4ef2f11e
N
255 autof = match->autof;
256 if (autof == 0)
257 autof = ci->autof;
258
7cdc0872 259 name_to_use = info.name;
0ac91628 260 if (name_to_use[0] == 0 &&
0c4304ca 261 info.array.level == LEVEL_CONTAINER) {
ecb02e31
N
262 name_to_use = info.text_version;
263 trustworthy = METADATA;
264 }
0ac91628
N
265 if (name_to_use[0] && trustworthy != LOCAL &&
266 ! require_homehost &&
267 conf_name_is_free(name_to_use))
268 trustworthy = LOCAL;
ecb02e31 269
7cdc0872
N
270 /* strip "hostname:" prefix from name if we have decided
271 * to treat it as LOCAL
272 */
273 if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL)
274 name_to_use = strchr(name_to_use, ':')+1;
275
ad5bc697 276 /* 4/ Check if array exists.
215bb3f7 277 */
93c861ee 278 if (map_lock(&map))
e7b84f9d 279 pr_err("failed to get exclusive lock on "
93c861ee 280 "mapfile\n");
215bb3f7 281 mp = map_by_uuid(&map, info.uuid);
339c2d6c
N
282 if (mp)
283 mdfd = open_dev(mp->devnum);
284 else
215bb3f7 285 mdfd = -1;
d7288ddc 286
8382f19b 287 if (mdfd < 0) {
8382f19b 288
215bb3f7 289 /* Couldn't find an existing array, maybe make a new one */
ffb3e494 290 mdfd = create_mddev(match ? match->devname : NULL,
e12e1d1d 291 name_to_use, autof, trustworthy, chosen_name);
215bb3f7
N
292
293 if (mdfd < 0)
25824e2d 294 goto out_unlock;
215bb3f7
N
295
296 sysfs_init(&info, mdfd, 0);
297
f35f2525 298 if (set_array_info(mdfd, st, &info) != 0) {
e7b84f9d 299 pr_err("failed to set array info for %s: %s\n",
8382f19b 300 chosen_name, strerror(errno));
15d4a7e4 301 rv = 2;
25824e2d 302 goto out_unlock;
8382f19b 303 }
7801ac20 304
c5afc314
N
305 dinfo = info;
306 dinfo.disk.major = major(stb.st_rdev);
307 dinfo.disk.minor = minor(stb.st_rdev);
308 if (add_disk(mdfd, st, &info, &dinfo) != 0) {
e7b84f9d 309 pr_err("failed to add %s to %s: %s.\n",
8382f19b
NB
310 devname, chosen_name, strerror(errno));
311 ioctl(mdfd, STOP_ARRAY, 0);
15d4a7e4 312 rv = 2;
25824e2d 313 goto out_unlock;
8382f19b 314 }
2bf740b3
N
315 sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
316 GET_OFFSET | GET_SIZE));
317
06c7f68e 318 if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) {
8382f19b
NB
319 /* It really should be 'none' - must be old buggy
320 * kernel, and mdadm -I may not be able to complete.
321 * So reject it.
322 */
323 ioctl(mdfd, STOP_ARRAY, NULL);
e7b84f9d
N
324 pr_err("You have an old buggy kernel which cannot support\n"
325 " --incremental reliably. Aborting.\n");
15d4a7e4 326 rv = 2;
25824e2d 327 goto out_unlock;
8382f19b 328 }
c5afc314 329 info.array.working_disks = 1;
ad5bc697
N
330 /* 6/ Make sure /var/run/mdadm.map contains this array. */
331 map_update(&map, fd2devnum(mdfd),
332 info.text_version,
333 info.uuid, chosen_name);
8382f19b
NB
334 } else {
335 /* 5b/ if it does */
336 /* - check one drive in array to make sure metadata is a reasonably */
337 /* close match. Reject if not (e.g. different type) */
338 /* - add the device */
339 char dn[20];
340 int dfd2;
8382f19b 341 int err;
3da92f27 342 struct supertype *st2;
c5afc314 343 struct mdinfo info2, *d;
215bb3f7 344
2bf740b3
N
345 sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
346 GET_OFFSET | GET_SIZE));
347
339c2d6c
N
348 if (mp->path)
349 strcpy(chosen_name, mp->path);
350 else
351 strcpy(chosen_name, devnum2devname(mp->devnum));
215bb3f7 352
ef83fe7c
N
353 /* It is generally not OK to add non-spare drives to a
354 * running array as they are probably missing because
355 * they failed. However if runstop is 1, then the
7e83544b
N
356 * array was possibly started early and our best bet is
357 * to add this anyway.
358 * Also if action policy is re-add or better we allow
e5e5d7ce
N
359 * re-add.
360 * This doesn't apply to containers as the 'non-spare'
361 * flag has a different meaning. The test has to happen
362 * at the device level there
3a6ec29a 363 */
e5e5d7ce
N
364 if (!st->ss->external
365 && (info.disk.state & (1<<MD_DISK_SYNC)) != 0
7e83544b
N
366 && ! policy_action_allows(policy, st->ss->name,
367 act_re_add)
ef83fe7c 368 && runstop < 1) {
e5e5d7ce 369 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
e7b84f9d
N
370 pr_err("not adding %s to active array (without --run) %s\n",
371 devname, chosen_name);
15d4a7e4 372 rv = 2;
25824e2d 373 goto out_unlock;
3a6ec29a
N
374 }
375 }
15d4a7e4
N
376 if (!sra) {
377 rv = 2;
25824e2d 378 goto out_unlock;
15d4a7e4 379 }
7c548327
N
380 if (sra->devs) {
381 sprintf(dn, "%d:%d", sra->devs->disk.major,
382 sra->devs->disk.minor);
383 dfd2 = dev_open(dn, O_RDONLY);
fb745c4b 384 if (dfd2 < 0) {
e7b84f9d 385 pr_err("unable to open %s\n", devname);
fb745c4b
JS
386 rv = 2;
387 goto out_unlock;
388 }
7c548327
N
389 st2 = dup_super(st);
390 if (st2->ss->load_super(st2, dfd2, NULL) ||
391 st->ss->compare_super(st, st2) != 0) {
e7b84f9d
N
392 pr_err("metadata mismatch between %s and "
393 "chosen array %s\n",
394 devname, chosen_name);
7c548327 395 close(dfd2);
15d4a7e4 396 rv = 2;
25824e2d 397 goto out_unlock;
7c548327 398 }
8382f19b 399 close(dfd2);
a5d85af7 400 st2->ss->getinfo_super(st2, &info2, NULL);
7c548327
N
401 st2->ss->free_super(st2);
402 if (info.array.level != info2.array.level ||
403 memcmp(info.uuid, info2.uuid, 16) != 0 ||
404 info.array.raid_disks != info2.array.raid_disks) {
e7b84f9d
N
405 pr_err("unexpected difference between %s and %s.\n",
406 chosen_name, devname);
15d4a7e4 407 rv = 2;
25824e2d 408 goto out_unlock;
7c548327 409 }
8382f19b 410 }
69fe207e
N
411 info.disk.major = major(stb.st_rdev);
412 info.disk.minor = minor(stb.st_rdev);
c5afc314
N
413 /* add disk needs to know about containers */
414 if (st->ss->external)
415 sra->array.level = LEVEL_CONTAINER;
69fe207e 416 err = add_disk(mdfd, st, sra, &info);
8382f19b
NB
417 if (err < 0 && errno == EBUSY) {
418 /* could be another device present with the same
419 * disk.number. Find and reject any such
420 */
421 find_reject(mdfd, st, sra, info.disk.number,
422 info.events, verbose, chosen_name);
69fe207e 423 err = add_disk(mdfd, st, sra, &info);
8382f19b
NB
424 }
425 if (err < 0) {
e7b84f9d 426 pr_err("failed to add %s to %s: %s.\n",
8382f19b 427 devname, chosen_name, strerror(errno));
15d4a7e4 428 rv = 2;
25824e2d 429 goto out_unlock;
8382f19b 430 }
c5afc314
N
431 info.array.working_disks = 0;
432 for (d = sra->devs; d; d=d->next)
433 info.array.working_disks ++;
434
8382f19b 435 }
8382f19b
NB
436
437 /* 7/ Is there enough devices to possibly start the array? */
438 /* 7a/ if not, finish with success. */
352452c3 439 if (info.array.level == LEVEL_CONTAINER) {
7b0bbd0f 440 int devnum = devnum; /* defined and used iff ->external */
352452c3 441 /* Try to assemble within the container */
a408d66c 442 sysfs_uevent(sra, "change");
c5afc314 443 if (verbose >= 0)
e7b84f9d
N
444 pr_err("container %s now has %d device%s\n",
445 chosen_name, info.array.working_disks,
446 info.array.working_disks==1?"":"s");
a7c6e3fb 447 wait_for(chosen_name, mdfd);
ed690d36 448 if (st->ss->external)
983fff45 449 devnum = fd2devnum(mdfd);
5fc8cff3
JS
450 if (st->ss->load_container)
451 rv = st->ss->load_container(st, mdfd, NULL);
a7c6e3fb 452 close(mdfd);
2bf740b3 453 sysfs_free(sra);
5fc8cff3
JS
454 if (!rv)
455 rv = Incremental_container(st, chosen_name, homehost,
0ea8f5b1 456 verbose, runstop, 0, autof,
5fc8cff3
JS
457 freeze_reshape);
458 map_unlock(&map);
03b7f6c6
N
459 if (rv == 1)
460 /* Don't fail the whole -I if a subarray didn't
461 * have enough devices to start yet
462 */
463 rv = 0;
ed690d36
PC
464 /* after spare is added, ping monitor for external metadata
465 * so that it can eg. try to rebuild degraded array */
983fff45
AK
466 if (st->ss->external)
467 ping_monitor_by_id(devnum);
03b7f6c6 468 return rv;
352452c3 469 }
de6ae750 470
2bf740b3
N
471 /* We have added something to the array, so need to re-read the
472 * state. Eventually this state should be kept up-to-date as
473 * things change.
474 */
475 sysfs_free(sra);
476 sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE |
477 GET_OFFSET | GET_SIZE));
478 active_disks = count_active(st, sra, mdfd, &avail, &info);
8382f19b
NB
479 if (enough(info.array.level, info.array.raid_disks,
480 info.array.layout, info.array.state & 1,
de5a472e 481 avail) == 0) {
8382f19b 482 if (verbose >= 0)
e7b84f9d
N
483 pr_err("%s attached to %s, not enough to start (%d).\n",
484 devname, chosen_name, active_disks);
15d4a7e4 485 rv = 0;
25824e2d 486 goto out_unlock;
8382f19b 487 }
8382f19b
NB
488
489 /* 7b/ if yes, */
490 /* - if number of OK devices match expected, or -R and there */
491 /* are enough, */
492 /* + add any bitmap file */
493 /* + start the array (auto-readonly). */
8382f19b
NB
494
495 if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) {
496 if (verbose >= 0)
e7b84f9d
N
497 pr_err("%s attached to %s which is already active.\n",
498 devname, chosen_name);
15d4a7e4 499 rv = 0;
25824e2d 500 goto out_unlock;
8382f19b 501 }
ad5bc697
N
502
503 map_unlock(&map);
8382f19b 504 if (runstop > 0 || active_disks >= info.array.working_disks) {
2bf740b3 505 struct mdinfo *dsk;
8382f19b
NB
506 /* Let's try to start it */
507 if (match && match->bitmap_file) {
508 int bmfd = open(match->bitmap_file, O_RDWR);
509 if (bmfd < 0) {
e7b84f9d
N
510 pr_err("Could not open bitmap file %s.\n",
511 match->bitmap_file);
15d4a7e4 512 goto out;
8382f19b
NB
513 }
514 if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) {
515 close(bmfd);
e7b84f9d
N
516 pr_err("Failed to set bitmapfile for %s.\n",
517 chosen_name);
15d4a7e4 518 goto out;
8382f19b
NB
519 }
520 close(bmfd);
521 }
de6ae750
N
522 /* Need to remove from the array any devices which
523 * 'count_active' discerned were too old or inappropriate
524 */
525 for (d = sra ? sra->devs : NULL ; d ; d = d->next)
526 if (d->disk.state & (1<<MD_DISK_REMOVED))
527 remove_disk(mdfd, st, sra, d);
2bf740b3 528
7b403fef 529 if ((sra == NULL || active_disks >= info.array.working_disks)
215bb3f7 530 && trustworthy != FOREIGN)
8382f19b
NB
531 rv = ioctl(mdfd, RUN_ARRAY, NULL);
532 else
533 rv = sysfs_set_str(sra, NULL,
534 "array_state", "read-auto");
535 if (rv == 0) {
536 if (verbose >= 0)
e7b84f9d
N
537 pr_err("%s attached to %s, which has been started.\n",
538 devname, chosen_name);
8382f19b 539 rv = 0;
a7c6e3fb 540 wait_for(chosen_name, mdfd);
7e83544b
N
541 /* We just started the array, so some devices
542 * might have been evicted from the array
543 * because their event counts were too old.
544 * If the action=re-add policy is in-force for
545 * those devices we should re-add them now.
546 */
547 for (dsk = sra->devs; dsk ; dsk = dsk->next) {
548 if (disk_action_allows(dsk, st->ss->name, act_re_add) &&
549 add_disk(mdfd, st, sra, dsk) == 0)
e7b84f9d
N
550 pr_err("%s re-added to %s\n",
551 dsk->sys_name, chosen_name);
7e83544b 552 }
8382f19b 553 } else {
e7b84f9d
N
554 pr_err("%s attached to %s, but failed to start: %s.\n",
555 devname, chosen_name, strerror(errno));
8382f19b
NB
556 rv = 1;
557 }
558 } else {
559 if (verbose >= 0)
e7b84f9d
N
560 pr_err("%s attached to %s, not enough to start safely.\n",
561 devname, chosen_name);
8382f19b
NB
562 rv = 0;
563 }
15d4a7e4 564out:
de6ae750 565 free(avail);
15d4a7e4
N
566 if (dfd >= 0)
567 close(dfd);
568 if (mdfd >= 0)
569 close(mdfd);
7e83544b
N
570 if (policy)
571 dev_policy_free(policy);
2bf740b3
N
572 if (sra)
573 sysfs_free(sra);
8382f19b 574 return rv;
25824e2d
JS
575out_unlock:
576 map_unlock(&map);
577 goto out;
8382f19b
NB
578}
579
7e0f6979 580static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra,
8382f19b
NB
581 int number, __u64 events, int verbose,
582 char *array_name)
583{
3da92f27 584 /* Find a device attached to this array with a disk.number of number
8382f19b
NB
585 * and events less than the passed events, and remove the device.
586 */
06c7f68e 587 struct mdinfo *d;
8382f19b
NB
588 mdu_array_info_t ra;
589
590 if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0)
591 return; /* not safe to remove from active arrays
592 * without thinking more */
593
594 for (d = sra->devs; d ; d = d->next) {
595 char dn[10];
596 int dfd;
8382f19b 597 struct mdinfo info;
06c7f68e 598 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
599 dfd = dev_open(dn, O_RDONLY);
600 if (dfd < 0)
601 continue;
3da92f27 602 if (st->ss->load_super(st, dfd, NULL)) {
8382f19b
NB
603 close(dfd);
604 continue;
605 }
a5d85af7 606 st->ss->getinfo_super(st, &info, NULL);
3da92f27 607 st->ss->free_super(st);
8382f19b
NB
608 close(dfd);
609
610 if (info.disk.number != number ||
611 info.events >= events)
612 continue;
613
06c7f68e 614 if (d->disk.raid_disk > -1)
8382f19b
NB
615 sysfs_set_str(sra, d, "slot", "none");
616 if (sysfs_set_str(sra, d, "state", "remove") == 0)
617 if (verbose >= 0)
e7b84f9d
N
618 pr_err("removing old device %s from %s\n",
619 d->sys_name+4, array_name);
8382f19b
NB
620 }
621}
622
2bf740b3
N
623static int count_active(struct supertype *st, struct mdinfo *sra,
624 int mdfd, char **availp,
8382f19b
NB
625 struct mdinfo *bestinfo)
626{
627 /* count how many devices in sra think they are active */
06c7f68e 628 struct mdinfo *d;
de6ae750 629 int cnt = 0;
8382f19b 630 __u64 max_events = 0;
8382f19b 631 char *avail = NULL;
e4c72d1d 632 int *best = NULL;
de6ae750
N
633 char *devmap = NULL;
634 int numdevs = 0;
635 int devnum;
636 int b, i;
637 int raid_disks = 0;
8382f19b 638
b526e52d
DW
639 if (!sra)
640 return 0;
641
de6ae750
N
642 for (d = sra->devs ; d ; d = d->next)
643 numdevs++;
644 for (d = sra->devs, devnum=0 ; d ; d = d->next, devnum++) {
8382f19b
NB
645 char dn[30];
646 int dfd;
8382f19b
NB
647 int ok;
648 struct mdinfo info;
649
06c7f68e 650 sprintf(dn, "%d:%d", d->disk.major, d->disk.minor);
8382f19b
NB
651 dfd = dev_open(dn, O_RDONLY);
652 if (dfd < 0)
653 continue;
3da92f27 654 ok = st->ss->load_super(st, dfd, NULL);
8382f19b
NB
655 close(dfd);
656 if (ok != 0)
657 continue;
de6ae750
N
658 info.array.raid_disks = raid_disks;
659 st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum);
43aaf431 660 if (!avail) {
de6ae750 661 raid_disks = info.array.raid_disks;
503975b9 662 avail = xcalloc(raid_disks, 1);
43aaf431 663 *availp = avail;
de6ae750 664
503975b9
N
665 best = xcalloc(raid_disks, sizeof(int));
666 devmap = xcalloc(raid_disks, numdevs);
de6ae750
N
667
668 st->ss->getinfo_super(st, &info, devmap);
43aaf431
DL
669 }
670
8382f19b
NB
671 if (info.disk.state & (1<<MD_DISK_SYNC))
672 {
8382f19b
NB
673 if (cnt == 0) {
674 cnt++;
675 max_events = info.events;
676 avail[info.disk.raid_disk] = 2;
de6ae750 677 best[info.disk.raid_disk] = devnum;
a5d85af7 678 st->ss->getinfo_super(st, bestinfo, NULL);
8382f19b 679 } else if (info.events == max_events) {
8382f19b 680 avail[info.disk.raid_disk] = 2;
de6ae750 681 best[info.disk.raid_disk] = devnum;
8382f19b 682 } else if (info.events == max_events-1) {
de6ae750
N
683 if (avail[info.disk.raid_disk] == 0) {
684 avail[info.disk.raid_disk] = 1;
685 best[info.disk.raid_disk] = devnum;
686 }
8382f19b
NB
687 } else if (info.events < max_events - 1)
688 ;
689 else if (info.events == max_events+1) {
690 int i;
8382f19b 691 max_events = info.events;
de6ae750 692 for (i=0; i < raid_disks; i++)
8382f19b
NB
693 if (avail[i])
694 avail[i]--;
695 avail[info.disk.raid_disk] = 2;
de6ae750 696 best[info.disk.raid_disk] = devnum;
a5d85af7 697 st->ss->getinfo_super(st, bestinfo, NULL);
8382f19b 698 } else { /* info.events much bigger */
47c7a4be 699 memset(avail, 0, raid_disks);
8382f19b 700 max_events = info.events;
1d1a9f87 701 avail[info.disk.raid_disk] = 2;
47c7a4be 702 best[info.disk.raid_disk] = devnum;
a5d85af7 703 st->ss->getinfo_super(st, bestinfo, NULL);
8382f19b
NB
704 }
705 }
3da92f27 706 st->ss->free_super(st);
8382f19b 707 }
de6ae750
N
708 if (!avail)
709 return 0;
710 /* We need to reject any device that thinks the best device is
711 * failed or missing */
712 for (b = 0; b < raid_disks; b++)
713 if (avail[b] == 2)
714 break;
715 cnt = 0;
716 for (i = 0 ; i < raid_disks ; i++) {
717 if (i != b && avail[i])
718 if (devmap[raid_disks * best[i] + b] == 0) {
719 /* This device thinks 'b' is failed -
720 * don't use it */
721 devnum = best[i];
722 for (d=sra->devs ; devnum; d = d->next)
723 devnum--;
724 d->disk.state |= (1 << MD_DISK_REMOVED);
725 avail[i] = 0;
726 }
727 if (avail[i])
728 cnt++;
729 }
730 free(best);
731 free(devmap);
732 return cnt;
8382f19b
NB
733}
734
5be68a07
PC
735/* test if container has degraded member(s) */
736static int container_members_max_degradation(struct map_ent *map, struct map_ent *me)
737{
738 mdu_array_info_t array;
739 int afd;
740 int max_degraded = 0;
741
742 for(; map; map = map->next) {
743 if (!is_subarray(map->metadata) ||
744 devname2devnum(map->metadata+1) != me->devnum)
745 continue;
746 afd = open_dev(map->devnum);
747 if (afd < 0)
748 continue;
749 /* most accurate information regarding array degradation */
750 if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) {
751 int degraded = array.raid_disks - array.active_disks -
752 array.spare_disks;
753 if (degraded > max_degraded)
754 max_degraded = degraded;
755 }
756 close(afd);
757 }
758 return (max_degraded);
759}
760
56e8be85 761static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
d2db3045 762 struct map_ent *target, int bare,
56e8be85 763 struct supertype *st, int verbose)
f08605b3
N
764{
765 /* This device doesn't have any md metadata
d2db3045
N
766 * The device policy allows 'spare' and if !bare, it allows spare-same-slot.
767 * If 'st' is not set, then we only know that some metadata allows this,
768 * others possibly don't.
769 * So look for a container or array to attach the device to.
770 * Prefer 'target' if that is set and the array is found.
771 *
f08605b3
N
772 * If st is set, then only arrays of that type are considered
773 * Return 0 on success, or some exit code on failure, probably 1.
774 */
8659d089 775 int rv = 1;
f08605b3
N
776 struct stat stb;
777 struct map_ent *mp, *map = NULL;
778 struct mdinfo *chosen = NULL;
779 int dfd = *dfdp;
780
f08605b3
N
781 if (fstat(dfd, &stb) != 0)
782 return 1;
f08605b3 783
56e8be85 784 /*
f08605b3
N
785 * Now we need to find a suitable array to add this to.
786 * We only accept arrays that:
787 * - match 'st'
788 * - are in the same domains as the device
789 * - are of an size for which the device will be useful
790 * and we choose the one that is the most degraded
791 */
792
793 if (map_lock(&map)) {
e7b84f9d 794 pr_err("failed to get exclusive lock on "
f08605b3
N
795 "mapfile\n");
796 return 1;
797 }
798 for (mp = map ; mp ; mp = mp->next) {
799 struct supertype *st2;
800 struct domainlist *dl = NULL;
801 struct mdinfo *sra;
802 unsigned long long devsize;
71204a50 803 unsigned long long component_size = 0;
f08605b3
N
804
805 if (is_subarray(mp->metadata))
806 continue;
807 if (st) {
808 st2 = st->ss->match_metadata_desc(mp->metadata);
809 if (!st2 ||
810 (st->minor_version >= 0 &&
811 st->minor_version != st2->minor_version)) {
812 if (verbose > 1)
e7b84f9d 813 pr_err("not adding %s to %s as metadata type doesn't match\n",
f08605b3
N
814 devname, mp->path);
815 free(st2);
816 continue;
817 }
818 free(st2);
819 }
820 sra = sysfs_read(-1, mp->devnum,
821 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
822 GET_DEGRADED|GET_COMPONENT|GET_VERSION);
823 if (!sra) {
824 /* Probably a container - no degraded info */
825 sra = sysfs_read(-1, mp->devnum,
826 GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE|
827 GET_COMPONENT|GET_VERSION);
828 if (sra)
5be68a07 829 sra->array.failed_disks = -1;
f08605b3
N
830 }
831 if (!sra)
832 continue;
833 if (st == NULL) {
834 int i;
835 st2 = NULL;
836 for(i=0; !st2 && superlist[i]; i++)
837 st2 = superlist[i]->match_metadata_desc(
838 sra->text_version);
6e57f80a
N
839 if (!st2) {
840 if (verbose > 1)
e7b84f9d 841 pr_err("not adding %s to %s"
6e57f80a
N
842 " as metadata not recognised.\n",
843 devname, mp->path);
844 goto next;
845 }
625b2507
N
846 /* Need to double check the 'act_spare' permissions applies
847 * to this metadata.
848 */
849 if (!policy_action_allows(pol, st2->ss->name, act_spare))
850 goto next;
d2db3045
N
851 if (!bare && !policy_action_allows(pol, st2->ss->name,
852 act_spare_same_slot))
853 goto next;
f08605b3
N
854 } else
855 st2 = st;
5be68a07
PC
856 /* update number of failed disks for mostly degraded
857 * container member */
858 if (sra->array.failed_disks == -1)
859 sra->array.failed_disks = container_members_max_degradation(map, mp);
860
f08605b3 861 get_dev_size(dfd, NULL, &devsize);
48865704
PC
862 if (sra->component_size == 0) {
863 /* true for containers, here we must read superblock
864 * to obtain minimum spare size */
865 struct supertype *st3 = dup_super(st2);
866 int mdfd = open_dev(mp->devnum);
f56128b9 867 if (mdfd < 0) {
1fdeb8a0 868 free(st3);
48865704 869 goto next;
1fdeb8a0 870 }
48865704
PC
871 if (st3->ss->load_container &&
872 !st3->ss->load_container(st3, mdfd, mp->path)) {
873 component_size = st3->ss->min_acceptable_spare_size(st3);
874 st3->ss->free_super(st3);
875 }
876 free(st3);
877 close(mdfd);
878 }
879 if ((sra->component_size > 0 &&
880 st2->ss->avail_size(st2, devsize) < sra->component_size)
881 ||
882 (sra->component_size == 0 && devsize < component_size)) {
f08605b3 883 if (verbose > 1)
e7b84f9d 884 pr_err("not adding %s to %s as it is too small\n",
f08605b3
N
885 devname, mp->path);
886 goto next;
887 }
d2db3045
N
888 /* test against target.
889 * If 'target' is set and 'bare' is false, we only accept
890 * arrays/containers that match 'target'.
891 * If 'target' is set and 'bare' is true, we prefer the
892 * array which matches 'target'.
b4924220 893 * target is considered only if we deal with degraded array
d2db3045 894 */
fee6a49e
PC
895 if (target && policy_action_allows(pol, st2->ss->name,
896 act_spare_same_slot)) {
d2db3045
N
897 if (strcmp(target->metadata, mp->metadata) == 0 &&
898 memcmp(target->uuid, mp->uuid,
b4924220
PC
899 sizeof(target->uuid)) == 0 &&
900 sra->array.failed_disks > 0) {
d2db3045
N
901 /* This is our target!! */
902 if (chosen)
903 sysfs_free(chosen);
904 chosen = sra;
905 sra = NULL;
906 /* skip to end so we don't check any more */
907 while (mp->next)
908 mp = mp->next;
909 goto next;
910 }
911 /* not our target */
912 if (!bare)
913 goto next;
914 }
915
b6b3f0f7 916 dl = domain_from_array(sra, st2->ss->name);
e5508b36 917 if (domain_test(dl, pol, st2->ss->name) != 1) {
b6b3f0f7
PC
918 /* domain test fails */
919 if (verbose > 1)
e7b84f9d 920 pr_err("not adding %s to %s as"
b6b3f0f7
PC
921 " it is not in a compatible domain\n",
922 devname, mp->path);
923
924 goto next;
925 }
f08605b3
N
926 /* all tests passed, OK to add to this array */
927 if (!chosen) {
928 chosen = sra;
929 sra = NULL;
930 } else if (chosen->array.failed_disks < sra->array.failed_disks) {
931 sysfs_free(chosen);
932 chosen = sra;
933 sra = NULL;
934 }
935 next:
936 if (sra)
937 sysfs_free(sra);
938 if (st != st2)
939 free(st2);
940 if (dl)
941 domain_free(dl);
942 }
943 if (chosen) {
944 /* add current device to chosen array as a spare */
945 int mdfd = open_dev(devname2devnum(chosen->sys_name));
946 if (mdfd >= 0) {
a655e550 947 struct mddev_dev devlist;
f08605b3
N
948 char devname[20];
949 devlist.next = NULL;
950 devlist.used = 0;
f08605b3
N
951 devlist.writemostly = 0;
952 devlist.devname = devname;
953 sprintf(devname, "%d:%d", major(stb.st_rdev),
954 minor(stb.st_rdev));
955 devlist.disposition = 'a';
956 close(dfd);
957 *dfdp = -1;
958 rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist,
11b391ec 959 -1, 0, NULL, 0);
f08605b3
N
960 close(mdfd);
961 }
962 if (verbose > 0) {
963 if (rv == 0)
e7b84f9d 964 pr_err("added %s as spare for %s\n",
f08605b3
N
965 devname, chosen->sys_name);
966 else
e7b84f9d 967 pr_err("failed to add %s as spare for %s\n",
f08605b3
N
968 devname, chosen->sys_name);
969 }
970 sysfs_free(chosen);
971 }
015da8f5 972 map_unlock(&map);
8659d089 973 return rv;
f08605b3
N
974}
975
56e8be85
N
976static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol,
977 struct supertype *st, int verbose)
978{
61018da0
N
979 /* we know that at least one partition virtual-metadata is
980 * allowed to incorporate spares like this device. We need to
981 * find a suitable device to copy partition information from.
982 *
983 * Getting a list of all disk (not partition) devices is
984 * slightly non-trivial. We could look at /sys/block, but
985 * that is theoretically due to be removed. Maybe best to use
986 * /dev/disk/by-path/?* and ignore names ending '-partNN' as
987 * we depend on this directory of 'path' info. But that fails
988 * to find loop devices and probably others. Maybe don't
989 * worry about that, they aren't the real target.
990 *
991 * So: check things in /dev/disk/by-path to see if they are in
992 * a compatible domain, then load the partition table and see
993 * if it is OK for the new device, and choose the largest
994 * partition table that fits.
995 */
996 DIR *dir;
997 struct dirent *de;
998 char *chosen = NULL;
71204a50 999 unsigned long long chosen_size = 0;
61018da0
N
1000 struct supertype *chosen_st = NULL;
1001 int fd;
1002
1003 dir = opendir("/dev/disk/by-path");
1004 if (!dir)
1005 return 1;
1006 while ((de = readdir(dir)) != NULL) {
1007 char *ep;
1008 struct dev_policy *pol2 = NULL;
1009 struct domainlist *domlist = NULL;
1010 int fd = -1;
1011 struct mdinfo info;
1012 struct supertype *st2 = NULL;
1013 char *devname = NULL;
1014 unsigned long long devsectors;
1015
1016 if (de->d_ino == 0 ||
1017 de->d_name[0] == '.' ||
1018 (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN))
1019 goto next;
1020
1021 ep = de->d_name + strlen(de->d_name);
1022 while (ep > de->d_name &&
1023 isdigit(ep[-1]))
1024 ep--;
1025 if (ep > de->d_name + 5 &&
1026 strncmp(ep-5, "-part", 5) == 0)
1027 /* This is a partition - skip it */
1028 goto next;
1029
1030 pol2 = path_policy(de->d_name, type_disk);
1031
1032 domain_merge(&domlist, pol2, st ? st->ss->name : NULL);
e5508b36 1033 if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1)
61018da0
N
1034 /* new device is incompatible with this device. */
1035 goto next;
1036
1037 domain_free(domlist);
1038 domlist = NULL;
1039
71204a50
N
1040 if (asprintf(&devname, "/dev/disk/by-path/%s", de->d_name) != 1) {
1041 devname = NULL;
1042 goto next;
1043 }
61018da0
N
1044 fd = open(devname, O_RDONLY);
1045 if (fd < 0)
1046 goto next;
1047 if (get_dev_size(fd, devname, &devsectors) == 0)
1048 goto next;
1049 devsectors >>= 9;
1050
1051 if (st)
1052 st2 = dup_super(st);
1053 else
1054 st2 = guess_super_type(fd, guess_partitions);
1055 if (st2 == NULL ||
1056 st2->ss->load_super(st2, fd, NULL) < 0)
1057 goto next;
1058
1059 if (!st) {
1060 /* Check domain policy again, this time referring to metadata */
1061 domain_merge(&domlist, pol2, st2->ss->name);
e5508b36 1062 if (domain_test(domlist, pol, st2->ss->name) != 1)
61018da0
N
1063 /* Incompatible devices for this metadata type */
1064 goto next;
625b2507
N
1065 if (!policy_action_allows(pol, st2->ss->name, act_spare))
1066 /* Some partition types allow sparing, but not
1067 * this one.
1068 */
1069 goto next;
61018da0
N
1070 }
1071
a5d85af7 1072 st2->ss->getinfo_super(st2, &info, NULL);
61018da0
N
1073 if (info.component_size > devsectors)
1074 /* This partitioning doesn't fit in the device */
1075 goto next;
1076
1077 /* This is an acceptable device to copy partition
1078 * metadata from. We could just stop here, but I
1079 * think I want to keep looking incase a larger
1080 * metadata which makes better use of the device can
1081 * be found.
1082 */
1083 if (chosen == NULL ||
1084 chosen_size < info.component_size) {
1085 chosen_size = info.component_size;
1086 free(chosen);
1087 chosen = devname;
1088 devname = NULL;
1089 if (chosen_st) {
1090 chosen_st->ss->free_super(chosen_st);
1091 free(chosen_st);
1092 }
1093 chosen_st = st2;
1094 st2 = NULL;
1095 }
1096
1097 next:
1098 free(devname);
1099 domain_free(domlist);
1100 dev_policy_free(pol2);
1101 if (st2)
1102 st2->ss->free_super(st2);
1103 free(st2);
1104
1105 if (fd >= 0)
1106 close(fd);
1107 }
1108
be5c60e3
JS
1109 closedir(dir);
1110
61018da0
N
1111 if (!chosen)
1112 return 1;
1113
1114 /* 'chosen' is the best device we can find. Let's write its
1115 * metadata to devname dfd is read-only so don't use that
1116 */
1117 fd = open(devname, O_RDWR);
1118 if (fd >= 0) {
1119 chosen_st->ss->store_super(chosen_st, fd);
1120 close(fd);
1121 }
1122 free(chosen);
1123 chosen_st->ss->free_super(chosen_st);
1124 free(chosen_st);
1125 return 0;
56e8be85
N
1126}
1127
52e965c2
N
1128static int is_bare(int dfd)
1129{
1130 unsigned long long size = 0;
1131 char bufpad[4096 + 4096];
1132 char *buf = (char*)(((long)bufpad + 4096) & ~4095);
1133
1134 if (lseek(dfd, 0, SEEK_SET) != 0 ||
1135 read(dfd, buf, 4096) != 4096)
1136 return 0;
1137
1138 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1139 return 0;
1140 if (memcmp(buf, buf+1, 4095) != 0)
1141 return 0;
1142
1143 /* OK, first 4K appear blank, try the end. */
1144 get_dev_size(dfd, NULL, &size);
1145 if (lseek(dfd, size-4096, SEEK_SET) < 0 ||
1146 read(dfd, buf, 4096) != 4096)
1147 return 0;
1148
1149 if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff')
1150 return 0;
1151 if (memcmp(buf, buf+1, 4095) != 0)
1152 return 0;
1153
1154 return 1;
1155}
56e8be85
N
1156
1157/* adding a spare to a regular array is quite different from adding one to
1158 * a set-of-partitions virtual array.
1159 * This function determines which is worth trying and tries as appropriate.
1160 * Arrays are given priority over partitions.
1161 */
1162static int try_spare(char *devname, int *dfdp, struct dev_policy *pol,
d2db3045 1163 struct map_ent *target,
56e8be85
N
1164 struct supertype *st, int verbose)
1165{
1166 int i;
1167 int rv;
1168 int arrays_ok = 0;
1169 int partitions_ok = 0;
56e8be85 1170 int dfd = *dfdp;
d2db3045 1171 int bare;
56e8be85 1172
d2db3045 1173 /* Can only add a spare if device has at least one domain */
56e8be85
N
1174 if (pol_find(pol, pol_domain) == NULL)
1175 return 1;
1176 /* And only if some action allows spares */
1177 if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare))
1178 return 1;
1179
d2db3045
N
1180 /* Now check if the device is bare.
1181 * bare devices can always be added as a spare
1182 * non-bare devices can only be added if spare-same-slot is permitted,
1183 * and this device is replacing a previous device - in which case 'target'
1184 * will be set.
56e8be85 1185 */
52e965c2 1186 if (!is_bare(dfd)) {
d2db3045
N
1187 /* Must have a target and allow same_slot */
1188 /* Later - may allow force_spare without target */
1189 if (!target ||
1190 !policy_action_allows(pol, st?st->ss->name:NULL,
1191 act_spare_same_slot)) {
1192 if (verbose > 1)
e7b84f9d 1193 pr_err("%s is not bare, so not "
d2db3045
N
1194 "considering as a spare\n",
1195 devname);
1196 return 1;
1197 }
1198 bare = 0;
1199 } else
1200 bare = 1;
56e8be85 1201
d2db3045
N
1202 /* It might be OK to add this device to an array - need to see
1203 * what arrays might be candidates.
56e8be85
N
1204 */
1205 if (st) {
1206 /* just try try 'array' or 'partition' based on this metadata */
1207 if (st->ss->add_to_super)
d2db3045 1208 return array_try_spare(devname, dfdp, pol, target, bare,
56e8be85
N
1209 st, verbose);
1210 else
1211 return partition_try_spare(devname, dfdp, pol,
1212 st, verbose);
1213 }
d2db3045
N
1214 /* No metadata was specified or found so options are open.
1215 * Check for whether any array metadata, or any partition metadata
1216 * might allow adding the spare. This check is just help to avoid
1217 * a more costly scan of all arrays when we can be sure that will
1218 * fail.
1219 */
56e8be85
N
1220 for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) {
1221 if (superlist[i]->add_to_super && !arrays_ok &&
1222 policy_action_allows(pol, superlist[i]->name, act_spare))
1223 arrays_ok = 1;
1224 if (superlist[i]->add_to_super == NULL && !partitions_ok &&
1225 policy_action_allows(pol, superlist[i]->name, act_spare))
1226 partitions_ok = 1;
1227 }
aaccda44 1228 rv = 1;
56e8be85 1229 if (arrays_ok)
d2db3045
N
1230 rv = array_try_spare(devname, dfdp, pol, target, bare,
1231 st, verbose);
aaccda44 1232 if (rv != 0 && partitions_ok)
56e8be85
N
1233 rv = partition_try_spare(devname, dfdp, pol, st, verbose);
1234 return rv;
1235}
1236
8382f19b
NB
1237int IncrementalScan(int verbose)
1238{
1239 /* look at every device listed in the 'map' file.
1240 * If one is found that is not running then:
1241 * look in mdadm.conf for bitmap file.
1242 * if one exists, but array has none, add it.
1243 * try to start array in auto-readonly mode
1244 */
1245 struct map_ent *mapl = NULL;
1246 struct map_ent *me;
fa56eddb 1247 struct mddev_ident *devs, *mddev;
8382f19b
NB
1248 int rv = 0;
1249
1250 map_read(&mapl);
1251 devs = conf_get_ident(NULL);
1252
1253 for (me = mapl ; me ; me = me->next) {
8382f19b
NB
1254 mdu_array_info_t array;
1255 mdu_bitmap_file_t bmf;
7e0f6979 1256 struct mdinfo *sra;
339c2d6c 1257 int mdfd = open_dev(me->devnum);
215bb3f7 1258
8382f19b
NB
1259 if (mdfd < 0)
1260 continue;
1261 if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 ||
1262 errno != ENODEV) {
1263 close(mdfd);
1264 continue;
1265 }
1266 /* Ok, we can try this one. Maybe it needs a bitmap */
1267 for (mddev = devs ; mddev ; mddev = mddev->next)
339c2d6c 1268 if (mddev->devname && me->path
2400e6eb 1269 && devname_matches(mddev->devname, me->path))
8382f19b
NB
1270 break;
1271 if (mddev && mddev->bitmap_file) {
1272 /*
1273 * Note: early kernels will wrongly fail this, so it
1274 * is a hint only
1275 */
1276 int added = -1;
1277 if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) {
1278 int bmfd = open(mddev->bitmap_file, O_RDWR);
1279 if (bmfd >= 0) {
1280 added = ioctl(mdfd, SET_BITMAP_FILE,
1281 bmfd);
1282 close(bmfd);
1283 }
1284 }
1285 if (verbose >= 0) {
1286 if (added == 0)
e7b84f9d
N
1287 pr_err("Added bitmap %s to %s\n",
1288 mddev->bitmap_file, me->path);
8382f19b 1289 else if (errno != EEXIST)
e7b84f9d
N
1290 pr_err("Failed to add bitmap to %s: %s\n",
1291 me->path, strerror(errno));
8382f19b
NB
1292 }
1293 }
1294 sra = sysfs_read(mdfd, 0, 0);
1295 if (sra) {
1296 if (sysfs_set_str(sra, NULL,
1297 "array_state", "read-auto") == 0) {
1298 if (verbose >= 0)
e7b84f9d
N
1299 pr_err("started array %s\n",
1300 me->path ?: devnum2devname(me->devnum));
8382f19b 1301 } else {
e7b84f9d
N
1302 pr_err("failed to start array %s: %s\n",
1303 me->path ?: devnum2devname(me->devnum),
1304 strerror(errno));
8382f19b
NB
1305 rv = 1;
1306 }
b1efa6c2 1307 sysfs_free(sra);
8382f19b
NB
1308 }
1309 }
1310 return rv;
1311}
598f0d58 1312
1771a6e2
N
1313static char *container2devname(char *devname)
1314{
1315 char *mdname = NULL;
1316
1317 if (devname[0] == '/') {
1318 int fd = open(devname, O_RDONLY);
1319 if (fd >= 0) {
1320 mdname = devnum2devname(fd2devnum(fd));
1321 close(fd);
1322 }
1323 } else {
1324 int uuid[4];
1325 struct map_ent *mp, *map = NULL;
1326
1327 if (!parse_uuid(devname, uuid))
1328 return mdname;
1329 mp = map_by_uuid(&map, uuid);
1330 if (mp)
1331 mdname = devnum2devname(mp->devnum);
1332 map_free(map);
1333 }
1334
1335 return mdname;
1336}
1337
47c74f3f
N
1338static int Incremental_container(struct supertype *st, char *devname,
1339 char *homehost, int verbose,
0ea8f5b1
N
1340 int runstop, int readonly,
1341 int autof, int freeze_reshape)
598f0d58
NB
1342{
1343 /* Collect the contents of this container and for each
1344 * array, choose a device name and assemble the array.
1345 */
1346
47c74f3f 1347 struct mdinfo *list;
598f0d58 1348 struct mdinfo *ra;
ad5bc697 1349 struct map_ent *map = NULL;
47c74f3f
N
1350 struct mdinfo info;
1351 int trustworthy;
fa56eddb 1352 struct mddev_ident *match;
47c74f3f 1353 int rv = 0;
037e12d6
AC
1354 struct domainlist *domains;
1355 struct map_ent *smp;
1356 int suuid[4];
1357 int sfd;
81219e70
LM
1358 int ra_blocked = 0;
1359 int ra_all = 0;
47c74f3f 1360
47c74f3f
N
1361 st->ss->getinfo_super(st, &info, NULL);
1362
1363 if ((runstop > 0 && info.container_enough >= 0) ||
1364 info.container_enough > 0)
1365 /* pass */;
1366 else {
1367 if (verbose)
e7b84f9d 1368 pr_err("not enough devices to start the container\n");
47c74f3f
N
1369 return 0;
1370 }
1371
2244d1a9 1372 match = conf_match(st, &info, devname, verbose, &rv);
47c74f3f
N
1373 if (match == NULL && rv == 2)
1374 return rv;
1375
1376 /* Need to compute 'trustworthy' */
1377 if (match)
1378 trustworthy = LOCAL;
1379 else if (st->ss->match_home(st, homehost) == 1)
1380 trustworthy = LOCAL;
1381 else if (st->ss->match_home(st, "any") == 1)
1382 trustworthy = LOCAL;
1383 else
1384 trustworthy = FOREIGN;
ad5bc697 1385
47c74f3f 1386 list = st->ss->container_content(st, NULL);
81219e70
LM
1387 /* when nothing to activate - quit */
1388 if (list == NULL)
1389 return 0;
598f0d58 1390 for (ra = list ; ra ; ra = ra->next) {
598f0d58
NB
1391 int mdfd;
1392 char chosen_name[1024];
ad5bc697 1393 struct map_ent *mp;
fa56eddb 1394 struct mddev_ident *match = NULL;
598f0d58 1395
81219e70
LM
1396 ra_all++;
1397 /* do not activate arrays blocked by metadata handler */
1398 if (ra->array.state & (1 << MD_SB_BLOCK_VOLUME)) {
e7b84f9d 1399 pr_err("Cannot activate array %s in %s.\n",
81219e70
LM
1400 ra->text_version, devname);
1401 ra_blocked++;
1402 continue;
1403 }
c5afc314 1404 mp = map_by_uuid(&map, ra->uuid);
d7288ddc 1405
30926600
N
1406 if (mp) {
1407 mdfd = open_dev(mp->devnum);
339c2d6c
N
1408 if (mp->path)
1409 strcpy(chosen_name, mp->path);
1410 else
1411 strcpy(chosen_name, devnum2devname(mp->devnum));
30926600 1412 } else {
dbb44303 1413
112cace6 1414 /* Check in mdadm.conf for container == devname and
dbb44303
N
1415 * member == ra->text_version after second slash.
1416 */
1417 char *sub = strchr(ra->text_version+1, '/');
fa56eddb 1418 struct mddev_ident *array_list;
dbb44303
N
1419 if (sub) {
1420 sub++;
1421 array_list = conf_get_ident(NULL);
1422 } else
1423 array_list = NULL;
1424 for(; array_list ; array_list = array_list->next) {
dbb44303
N
1425 char *dn;
1426 if (array_list->member == NULL ||
1427 array_list->container == NULL)
1428 continue;
1429 if (strcmp(array_list->member, sub) != 0)
1430 continue;
71d60c48
DW
1431 if (array_list->uuid_set &&
1432 !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid))
1433 continue;
1771a6e2
N
1434 dn = container2devname(array_list->container);
1435 if (dn == NULL)
dbb44303 1436 continue;
dbb44303
N
1437 if (strncmp(dn, ra->text_version+1,
1438 strlen(dn)) != 0 ||
1439 ra->text_version[strlen(dn)+1] != '/') {
1440 free(dn);
1441 continue;
1442 }
1443 free(dn);
1444 /* we have a match */
1445 match = array_list;
71d60c48 1446 if (verbose>0)
e7b84f9d 1447 pr_err("match found for member %s\n",
71d60c48 1448 array_list->member);
dbb44303
N
1449 break;
1450 }
dbb44303 1451
112cace6
N
1452 if (match && match->devname &&
1453 strcasecmp(match->devname, "<ignore>") == 0) {
1454 if (verbose > 0)
e7b84f9d
N
1455 pr_err("array %s/%s is "
1456 "explicitly ignored by mdadm.conf\n",
1457 match->container, match->member);
112cace6
N
1458 return 2;
1459 }
05833051
N
1460 if (match)
1461 trustworthy = LOCAL;
112cace6 1462
30926600
N
1463 mdfd = create_mddev(match ? match->devname : NULL,
1464 ra->name,
1465 autof,
1466 trustworthy,
1467 chosen_name);
1468 }
598f0d58
NB
1469
1470 if (mdfd < 0) {
e7b84f9d 1471 pr_err("failed to open %s: %s.\n",
598f0d58
NB
1472 chosen_name, strerror(errno));
1473 return 2;
1474 }
1475
0ea8f5b1 1476 assemble_container_content(st, mdfd, ra, runstop, readonly,
b76b30e0
AK
1477 chosen_name, verbose, NULL,
1478 freeze_reshape);
588bebfc 1479 close(mdfd);
598f0d58 1480 }
037e12d6 1481
81219e70
LM
1482 /* don't move spares to container with volume being activated
1483 when all volumes are blocked */
ad098cdd 1484 if (ra_all == ra_blocked)
81219e70 1485 return 0;
81219e70 1486
037e12d6
AC
1487 /* Now move all suitable spares from spare container */
1488 domains = domain_from_array(list, st->ss->name);
1489 memcpy(suuid, uuid_zero, sizeof(int[4]));
1490 if (domains &&
1491 (smp = map_by_uuid(&map, suuid)) != NULL &&
1492 (sfd = open(smp->path, O_RDONLY)) >= 0) {
1493 /* spare container found */
1494 struct supertype *sst =
1495 super_imsm.match_metadata_desc("imsm");
1496 struct mdinfo *sinfo;
1497 unsigned long long min_size = 0;
1498 if (st->ss->min_acceptable_spare_size)
1499 min_size = st->ss->min_acceptable_spare_size(st);
1500 if (!sst->ss->load_container(sst, sfd, NULL)) {
1501 close(sfd);
1502 sinfo = container_choose_spares(sst, min_size,
1503 domains, NULL,
1504 st->ss->name, 0);
1505 sst->ss->free_super(sst);
1506 if (sinfo){
1507 int count = 0;
1508 struct mdinfo *disks = sinfo->devs;
1509 while (disks) {
1510 /* move spare from spare
1511 * container to currently
1512 * assembled one
1513 */
1514 if (move_spare(
1515 smp->path,
1516 devname,
1517 makedev(disks->disk.major,
1518 disks->disk.minor)))
1519 count++;
1520 disks = disks->next;
1521 }
1522 if (count)
e7b84f9d
N
1523 pr_err("Added %d spare%s to %s\n",
1524 count, count>1?"s":"", devname);
037e12d6
AC
1525 }
1526 sysfs_free(sinfo);
1527 } else
1528 close(sfd);
1529 }
1530 domain_free(domains);
598f0d58
NB
1531 return 0;
1532}
29ba4804
N
1533
1534/*
1535 * IncrementalRemove - Attempt to see if the passed in device belongs to any
1536 * raid arrays, and if so first fail (if needed) and then remove the device.
1537 *
1538 * @devname - The device we want to remove
950bc344 1539 * @id_path - name as found in /dev/disk/by-path for this device
29ba4804
N
1540 *
1541 * Note: the device name must be a kernel name like "sda", so
1542 * that we can find it in /proc/mdstat
1543 */
950bc344 1544int IncrementalRemove(char *devname, char *id_path, int verbose)
29ba4804
N
1545{
1546 int mdfd;
aae3cdc3 1547 int rv;
29ba4804 1548 struct mdstat_ent *ent;
a655e550 1549 struct mddev_dev devlist;
29ba4804 1550
950bc344
PC
1551 if (!id_path)
1552 dprintf(Name ": incremental removal without --path <id_path> "
1553 "lacks the possibility to re-add new device in this "
1554 "port\n");
1555
29ba4804 1556 if (strchr(devname, '/')) {
e7b84f9d 1557 pr_err("incremental removal requires a "
29ba4804
N
1558 "kernel device name, not a file: %s\n", devname);
1559 return 1;
1560 }
1561 ent = mdstat_by_component(devname);
1562 if (!ent) {
e7b84f9d 1563 pr_err("%s does not appear to be a component "
29ba4804
N
1564 "of any array\n", devname);
1565 return 1;
1566 }
1567 mdfd = open_dev(ent->devnum);
1568 if (mdfd < 0) {
e7b84f9d 1569 pr_err("Cannot open array %s!!\n", ent->dev);
08387a04 1570 free_mdstat(ent);
29ba4804
N
1571 return 1;
1572 }
403410eb
PC
1573
1574 if (id_path) {
1575 struct map_ent *map = NULL, *me;
1576 me = map_by_devnum(&map, ent->devnum);
1577 if (me)
1578 policy_save_path(id_path, me);
1579 map_free(map);
1580 }
1581
29ba4804
N
1582 memset(&devlist, 0, sizeof(devlist));
1583 devlist.devname = devname;
1584 devlist.disposition = 'f';
08387a04
N
1585 /* for a container, we must fail each member array */
1586 if (ent->metadata_version &&
1587 strncmp(ent->metadata_version, "external:", 9) == 0) {
1588 struct mdstat_ent *mdstat = mdstat_read(0, 0);
1589 struct mdstat_ent *memb;
1590 for (memb = mdstat ; memb ; memb = memb->next)
1591 if (is_container_member(memb, ent->dev)) {
1592 int subfd = open_dev(memb->devnum);
1593 if (subfd >= 0) {
1594 Manage_subdevs(memb->dev, subfd,
833bb0f8 1595 &devlist, verbose, 0,
11b391ec 1596 NULL, 0);
08387a04
N
1597 close(subfd);
1598 }
1599 }
1600 free_mdstat(mdstat);
1601 } else
11b391ec 1602 Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL, 0);
29ba4804 1603 devlist.disposition = 'r';
11b391ec 1604 rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL, 0);
aae3cdc3 1605 close(mdfd);
08387a04 1606 free_mdstat(ent);
aae3cdc3 1607 return rv;
29ba4804 1608}