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