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