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