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