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