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