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