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