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