]>
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 | ||
8382f19b NB |
531 | if (enough(info.array.level, info.array.raid_disks, |
532 | info.array.layout, info.array.state & 1, | |
de5a472e | 533 | avail) == 0) { |
9ca39acb N |
534 | if (c->export) { |
535 | printf("MD_STARTED=no\n"); | |
536 | } else if (c->verbose >= 0) | |
e7b84f9d N |
537 | pr_err("%s attached to %s, not enough to start (%d).\n", |
538 | devname, chosen_name, active_disks); | |
15d4a7e4 | 539 | rv = 0; |
25824e2d | 540 | goto out_unlock; |
8382f19b | 541 | } |
8382f19b NB |
542 | |
543 | /* 7b/ if yes, */ | |
544 | /* - if number of OK devices match expected, or -R and there */ | |
545 | /* are enough, */ | |
546 | /* + add any bitmap file */ | |
547 | /* + start the array (auto-readonly). */ | |
8382f19b NB |
548 | |
549 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
9ca39acb N |
550 | if (c->export) { |
551 | printf("MD_STARTED=already\n"); | |
552 | } else if (c->verbose >= 0) | |
e7b84f9d N |
553 | pr_err("%s attached to %s which is already active.\n", |
554 | devname, chosen_name); | |
15d4a7e4 | 555 | rv = 0; |
25824e2d | 556 | goto out_unlock; |
8382f19b | 557 | } |
ad5bc697 N |
558 | |
559 | map_unlock(&map); | |
5c6ad211 | 560 | if (c->runstop > 0 || (!journal_device_missing && active_disks >= info.array.working_disks)) { |
2bf740b3 | 561 | struct mdinfo *dsk; |
8382f19b | 562 | /* Let's try to start it */ |
5e88ab2e | 563 | |
5c6ad211 SL |
564 | if (journal_device_missing) |
565 | pr_err("Trying to run with missing journal device\n"); | |
5e88ab2e | 566 | if (info.reshape_active && !(info.reshape_active & RESHAPE_NO_BACKUP)) { |
ed503f89 N |
567 | pr_err("%s: This array is being reshaped and cannot be started\n", |
568 | chosen_name); | |
569 | cont_err("by --incremental. Please use --assemble\n"); | |
5e88ab2e N |
570 | goto out; |
571 | } | |
8382f19b NB |
572 | if (match && match->bitmap_file) { |
573 | int bmfd = open(match->bitmap_file, O_RDWR); | |
574 | if (bmfd < 0) { | |
e7b84f9d N |
575 | pr_err("Could not open bitmap file %s.\n", |
576 | match->bitmap_file); | |
15d4a7e4 | 577 | goto out; |
8382f19b NB |
578 | } |
579 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
580 | close(bmfd); | |
e7b84f9d N |
581 | pr_err("Failed to set bitmapfile for %s.\n", |
582 | chosen_name); | |
15d4a7e4 | 583 | goto out; |
8382f19b NB |
584 | } |
585 | close(bmfd); | |
586 | } | |
de6ae750 N |
587 | /* Need to remove from the array any devices which |
588 | * 'count_active' discerned were too old or inappropriate | |
589 | */ | |
590 | for (d = sra ? sra->devs : NULL ; d ; d = d->next) | |
591 | if (d->disk.state & (1<<MD_DISK_REMOVED)) | |
592 | remove_disk(mdfd, st, sra, d); | |
2bf740b3 | 593 | |
7b403fef | 594 | if ((sra == NULL || active_disks >= info.array.working_disks) |
215bb3f7 | 595 | && trustworthy != FOREIGN) |
8382f19b NB |
596 | rv = ioctl(mdfd, RUN_ARRAY, NULL); |
597 | else | |
598 | rv = sysfs_set_str(sra, NULL, | |
599 | "array_state", "read-auto"); | |
8832342d N |
600 | /* Array might be O_EXCL which will interfere with |
601 | * fsck and mount. So re-open without O_EXCL. | |
602 | */ | |
603 | reopen_mddev(mdfd); | |
8382f19b | 604 | if (rv == 0) { |
8832342d N |
605 | if (c->export) { |
606 | printf("MD_STARTED=yes\n"); | |
607 | } else if (c->verbose >= 0) | |
e7b84f9d N |
608 | pr_err("%s attached to %s, which has been started.\n", |
609 | devname, chosen_name); | |
8382f19b | 610 | rv = 0; |
a7c6e3fb | 611 | wait_for(chosen_name, mdfd); |
7e83544b N |
612 | /* We just started the array, so some devices |
613 | * might have been evicted from the array | |
614 | * because their event counts were too old. | |
615 | * If the action=re-add policy is in-force for | |
616 | * those devices we should re-add them now. | |
617 | */ | |
618 | for (dsk = sra->devs; dsk ; dsk = dsk->next) { | |
619 | if (disk_action_allows(dsk, st->ss->name, act_re_add) && | |
620 | add_disk(mdfd, st, sra, dsk) == 0) | |
e7b84f9d N |
621 | pr_err("%s re-added to %s\n", |
622 | dsk->sys_name, chosen_name); | |
7e83544b | 623 | } |
8382f19b | 624 | } else { |
e7b84f9d N |
625 | pr_err("%s attached to %s, but failed to start: %s.\n", |
626 | devname, chosen_name, strerror(errno)); | |
8382f19b NB |
627 | rv = 1; |
628 | } | |
629 | } else { | |
9ca39acb N |
630 | if (c->export) { |
631 | printf("MD_STARTED=unsafe\n"); | |
5c6ad211 SL |
632 | } else if (journal_device_missing) { |
633 | pr_err("Journal device is missing, not safe to start yet.\n"); | |
9ca39acb | 634 | } else if (c->verbose >= 0) |
e7b84f9d N |
635 | pr_err("%s attached to %s, not enough to start safely.\n", |
636 | devname, chosen_name); | |
8382f19b NB |
637 | rv = 0; |
638 | } | |
15d4a7e4 | 639 | out: |
de6ae750 | 640 | free(avail); |
15d4a7e4 N |
641 | if (dfd >= 0) |
642 | close(dfd); | |
643 | if (mdfd >= 0) | |
644 | close(mdfd); | |
7e83544b N |
645 | if (policy) |
646 | dev_policy_free(policy); | |
fe112c9e | 647 | sysfs_free(sra); |
8382f19b | 648 | return rv; |
25824e2d JS |
649 | out_unlock: |
650 | map_unlock(&map); | |
651 | goto out; | |
8382f19b NB |
652 | } |
653 | ||
7e0f6979 | 654 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
655 | int number, __u64 events, int verbose, |
656 | char *array_name) | |
657 | { | |
3da92f27 | 658 | /* Find a device attached to this array with a disk.number of number |
8382f19b NB |
659 | * and events less than the passed events, and remove the device. |
660 | */ | |
06c7f68e | 661 | struct mdinfo *d; |
8382f19b NB |
662 | mdu_array_info_t ra; |
663 | ||
664 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
665 | return; /* not safe to remove from active arrays | |
666 | * without thinking more */ | |
667 | ||
668 | for (d = sra->devs; d ; d = d->next) { | |
a90ed30e | 669 | char dn[24]; // 2*11 bytes for ints (including sign) + colon + null byte |
8382f19b | 670 | int dfd; |
8382f19b | 671 | struct mdinfo info; |
06c7f68e | 672 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
673 | dfd = dev_open(dn, O_RDONLY); |
674 | if (dfd < 0) | |
675 | continue; | |
3da92f27 | 676 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
677 | close(dfd); |
678 | continue; | |
679 | } | |
a5d85af7 | 680 | st->ss->getinfo_super(st, &info, NULL); |
3da92f27 | 681 | st->ss->free_super(st); |
8382f19b NB |
682 | close(dfd); |
683 | ||
684 | if (info.disk.number != number || | |
685 | info.events >= events) | |
686 | continue; | |
687 | ||
06c7f68e | 688 | if (d->disk.raid_disk > -1) |
8382f19b NB |
689 | sysfs_set_str(sra, d, "slot", "none"); |
690 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
691 | if (verbose >= 0) | |
e7b84f9d N |
692 | pr_err("removing old device %s from %s\n", |
693 | d->sys_name+4, array_name); | |
8382f19b NB |
694 | } |
695 | } | |
696 | ||
2bf740b3 N |
697 | static int count_active(struct supertype *st, struct mdinfo *sra, |
698 | int mdfd, char **availp, | |
051f3265 | 699 | struct mdinfo *bestinfo) |
8382f19b NB |
700 | { |
701 | /* count how many devices in sra think they are active */ | |
06c7f68e | 702 | struct mdinfo *d; |
de6ae750 | 703 | int cnt = 0; |
72e7fb13 | 704 | int replcnt = 0; |
8382f19b | 705 | __u64 max_events = 0; |
8382f19b | 706 | char *avail = NULL; |
e4c72d1d | 707 | int *best = NULL; |
de6ae750 N |
708 | char *devmap = NULL; |
709 | int numdevs = 0; | |
710 | int devnum; | |
711 | int b, i; | |
712 | int raid_disks = 0; | |
8382f19b | 713 | |
b526e52d DW |
714 | if (!sra) |
715 | return 0; | |
716 | ||
de6ae750 N |
717 | for (d = sra->devs ; d ; d = d->next) |
718 | numdevs++; | |
ca3b6696 | 719 | for (d = sra->devs, devnum = 0 ; d ; d = d->next, devnum++) { |
8382f19b NB |
720 | char dn[30]; |
721 | int dfd; | |
8382f19b NB |
722 | int ok; |
723 | struct mdinfo info; | |
724 | ||
06c7f68e | 725 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
726 | dfd = dev_open(dn, O_RDONLY); |
727 | if (dfd < 0) | |
728 | continue; | |
3da92f27 | 729 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
730 | close(dfd); |
731 | if (ok != 0) | |
732 | continue; | |
5c6ad211 | 733 | |
de6ae750 N |
734 | info.array.raid_disks = raid_disks; |
735 | st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum); | |
5c6ad211 | 736 | if (info.disk.raid_disk == MD_DISK_ROLE_JOURNAL) |
051f3265 | 737 | bestinfo->journal_clean = 1; |
43aaf431 | 738 | if (!avail) { |
de6ae750 | 739 | raid_disks = info.array.raid_disks; |
503975b9 | 740 | avail = xcalloc(raid_disks, 1); |
43aaf431 | 741 | *availp = avail; |
de6ae750 | 742 | |
503975b9 N |
743 | best = xcalloc(raid_disks, sizeof(int)); |
744 | devmap = xcalloc(raid_disks, numdevs); | |
de6ae750 N |
745 | |
746 | st->ss->getinfo_super(st, &info, devmap); | |
43aaf431 DL |
747 | } |
748 | ||
8382f19b NB |
749 | if (info.disk.state & (1<<MD_DISK_SYNC)) |
750 | { | |
8382f19b NB |
751 | if (cnt == 0) { |
752 | cnt++; | |
753 | max_events = info.events; | |
754 | avail[info.disk.raid_disk] = 2; | |
de6ae750 | 755 | best[info.disk.raid_disk] = devnum; |
a5d85af7 | 756 | st->ss->getinfo_super(st, bestinfo, NULL); |
8382f19b | 757 | } else if (info.events == max_events) { |
8382f19b | 758 | avail[info.disk.raid_disk] = 2; |
de6ae750 | 759 | best[info.disk.raid_disk] = devnum; |
8382f19b | 760 | } else if (info.events == max_events-1) { |
de6ae750 N |
761 | if (avail[info.disk.raid_disk] == 0) { |
762 | avail[info.disk.raid_disk] = 1; | |
763 | best[info.disk.raid_disk] = devnum; | |
764 | } | |
8382f19b NB |
765 | } else if (info.events < max_events - 1) |
766 | ; | |
767 | else if (info.events == max_events+1) { | |
768 | int i; | |
8382f19b | 769 | max_events = info.events; |
ca3b6696 | 770 | for (i = 0; i < raid_disks; i++) |
8382f19b NB |
771 | if (avail[i]) |
772 | avail[i]--; | |
773 | avail[info.disk.raid_disk] = 2; | |
de6ae750 | 774 | best[info.disk.raid_disk] = devnum; |
a5d85af7 | 775 | st->ss->getinfo_super(st, bestinfo, NULL); |
8382f19b | 776 | } else { /* info.events much bigger */ |
47c7a4be | 777 | memset(avail, 0, raid_disks); |
8382f19b | 778 | max_events = info.events; |
1d1a9f87 | 779 | avail[info.disk.raid_disk] = 2; |
47c7a4be | 780 | best[info.disk.raid_disk] = devnum; |
a5d85af7 | 781 | st->ss->getinfo_super(st, bestinfo, NULL); |
8382f19b | 782 | } |
72e7fb13 N |
783 | } else if (info.disk.state & (1<<MD_DISK_REPLACEMENT)) |
784 | replcnt++; | |
3da92f27 | 785 | st->ss->free_super(st); |
8382f19b | 786 | } |
5c6ad211 | 787 | |
de6ae750 N |
788 | if (!avail) |
789 | return 0; | |
790 | /* We need to reject any device that thinks the best device is | |
791 | * failed or missing */ | |
792 | for (b = 0; b < raid_disks; b++) | |
793 | if (avail[b] == 2) | |
794 | break; | |
795 | cnt = 0; | |
796 | for (i = 0 ; i < raid_disks ; i++) { | |
797 | if (i != b && avail[i]) | |
798 | if (devmap[raid_disks * best[i] + b] == 0) { | |
799 | /* This device thinks 'b' is failed - | |
800 | * don't use it */ | |
801 | devnum = best[i]; | |
802 | for (d=sra->devs ; devnum; d = d->next) | |
803 | devnum--; | |
804 | d->disk.state |= (1 << MD_DISK_REMOVED); | |
805 | avail[i] = 0; | |
806 | } | |
807 | if (avail[i]) | |
808 | cnt++; | |
809 | } | |
f80057ae N |
810 | /* Also need to reject any spare device with an event count that |
811 | * is too high | |
812 | */ | |
813 | for (d = sra->devs; d; d = d->next) { | |
814 | if (!(d->disk.state & (1<<MD_DISK_SYNC)) && | |
815 | d->events > max_events) | |
816 | d->disk.state |= (1 << MD_DISK_REMOVED); | |
817 | } | |
de6ae750 N |
818 | free(best); |
819 | free(devmap); | |
72e7fb13 | 820 | return cnt + replcnt; |
8382f19b NB |
821 | } |
822 | ||
5be68a07 PC |
823 | /* test if container has degraded member(s) */ |
824 | static int container_members_max_degradation(struct map_ent *map, struct map_ent *me) | |
825 | { | |
826 | mdu_array_info_t array; | |
827 | int afd; | |
828 | int max_degraded = 0; | |
829 | ||
830 | for(; map; map = map->next) { | |
4dd2df09 | 831 | if (!metadata_container_matches(map->metadata, me->devnm)) |
5be68a07 | 832 | continue; |
4dd2df09 | 833 | afd = open_dev(map->devnm); |
5be68a07 PC |
834 | if (afd < 0) |
835 | continue; | |
836 | /* most accurate information regarding array degradation */ | |
837 | if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) { | |
838 | int degraded = array.raid_disks - array.active_disks - | |
839 | array.spare_disks; | |
840 | if (degraded > max_degraded) | |
841 | max_degraded = degraded; | |
842 | } | |
843 | close(afd); | |
844 | } | |
845 | return (max_degraded); | |
846 | } | |
847 | ||
56e8be85 | 848 | static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol, |
d2db3045 | 849 | struct map_ent *target, int bare, |
56e8be85 | 850 | struct supertype *st, int verbose) |
f08605b3 N |
851 | { |
852 | /* This device doesn't have any md metadata | |
d2db3045 N |
853 | * The device policy allows 'spare' and if !bare, it allows spare-same-slot. |
854 | * If 'st' is not set, then we only know that some metadata allows this, | |
855 | * others possibly don't. | |
856 | * So look for a container or array to attach the device to. | |
857 | * Prefer 'target' if that is set and the array is found. | |
858 | * | |
f08605b3 N |
859 | * If st is set, then only arrays of that type are considered |
860 | * Return 0 on success, or some exit code on failure, probably 1. | |
861 | */ | |
8659d089 | 862 | int rv = 1; |
f08605b3 N |
863 | struct stat stb; |
864 | struct map_ent *mp, *map = NULL; | |
865 | struct mdinfo *chosen = NULL; | |
866 | int dfd = *dfdp; | |
867 | ||
f08605b3 N |
868 | if (fstat(dfd, &stb) != 0) |
869 | return 1; | |
f08605b3 | 870 | |
56e8be85 | 871 | /* |
f08605b3 N |
872 | * Now we need to find a suitable array to add this to. |
873 | * We only accept arrays that: | |
874 | * - match 'st' | |
875 | * - are in the same domains as the device | |
876 | * - are of an size for which the device will be useful | |
877 | * and we choose the one that is the most degraded | |
878 | */ | |
879 | ||
880 | if (map_lock(&map)) { | |
7a862a02 | 881 | pr_err("failed to get exclusive lock on mapfile\n"); |
f08605b3 N |
882 | return 1; |
883 | } | |
884 | for (mp = map ; mp ; mp = mp->next) { | |
885 | struct supertype *st2; | |
886 | struct domainlist *dl = NULL; | |
887 | struct mdinfo *sra; | |
888 | unsigned long long devsize; | |
71204a50 | 889 | unsigned long long component_size = 0; |
f08605b3 N |
890 | |
891 | if (is_subarray(mp->metadata)) | |
892 | continue; | |
893 | if (st) { | |
894 | st2 = st->ss->match_metadata_desc(mp->metadata); | |
895 | if (!st2 || | |
896 | (st->minor_version >= 0 && | |
897 | st->minor_version != st2->minor_version)) { | |
898 | if (verbose > 1) | |
e7b84f9d | 899 | pr_err("not adding %s to %s as metadata type doesn't match\n", |
f08605b3 N |
900 | devname, mp->path); |
901 | free(st2); | |
902 | continue; | |
903 | } | |
904 | free(st2); | |
905 | } | |
4dd2df09 | 906 | sra = sysfs_read(-1, mp->devnm, |
f08605b3 N |
907 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE| |
908 | GET_DEGRADED|GET_COMPONENT|GET_VERSION); | |
909 | if (!sra) { | |
910 | /* Probably a container - no degraded info */ | |
4dd2df09 | 911 | sra = sysfs_read(-1, mp->devnm, |
f08605b3 N |
912 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE| |
913 | GET_COMPONENT|GET_VERSION); | |
914 | if (sra) | |
5be68a07 | 915 | sra->array.failed_disks = -1; |
f08605b3 N |
916 | } |
917 | if (!sra) | |
918 | continue; | |
919 | if (st == NULL) { | |
920 | int i; | |
921 | st2 = NULL; | |
ca3b6696 | 922 | for(i = 0; !st2 && superlist[i]; i++) |
f08605b3 N |
923 | st2 = superlist[i]->match_metadata_desc( |
924 | sra->text_version); | |
6e57f80a N |
925 | if (!st2) { |
926 | if (verbose > 1) | |
7a862a02 | 927 | pr_err("not adding %s to %s as metadata not recognised.\n", |
6e57f80a N |
928 | devname, mp->path); |
929 | goto next; | |
930 | } | |
625b2507 N |
931 | /* Need to double check the 'act_spare' permissions applies |
932 | * to this metadata. | |
933 | */ | |
934 | if (!policy_action_allows(pol, st2->ss->name, act_spare)) | |
935 | goto next; | |
d2db3045 N |
936 | if (!bare && !policy_action_allows(pol, st2->ss->name, |
937 | act_spare_same_slot)) | |
938 | goto next; | |
f08605b3 N |
939 | } else |
940 | st2 = st; | |
5be68a07 PC |
941 | /* update number of failed disks for mostly degraded |
942 | * container member */ | |
943 | if (sra->array.failed_disks == -1) | |
944 | sra->array.failed_disks = container_members_max_degradation(map, mp); | |
945 | ||
f08605b3 | 946 | get_dev_size(dfd, NULL, &devsize); |
48865704 PC |
947 | if (sra->component_size == 0) { |
948 | /* true for containers, here we must read superblock | |
949 | * to obtain minimum spare size */ | |
950 | struct supertype *st3 = dup_super(st2); | |
4dd2df09 | 951 | int mdfd = open_dev(mp->devnm); |
f56128b9 | 952 | if (mdfd < 0) { |
1fdeb8a0 | 953 | free(st3); |
48865704 | 954 | goto next; |
1fdeb8a0 | 955 | } |
48865704 PC |
956 | if (st3->ss->load_container && |
957 | !st3->ss->load_container(st3, mdfd, mp->path)) { | |
958 | component_size = st3->ss->min_acceptable_spare_size(st3); | |
959 | st3->ss->free_super(st3); | |
960 | } | |
961 | free(st3); | |
962 | close(mdfd); | |
963 | } | |
964 | if ((sra->component_size > 0 && | |
387fcd59 N |
965 | st2->ss->avail_size(st2, devsize, |
966 | sra->devs | |
967 | ? sra->devs->data_offset | |
968 | : INVALID_SECTORS) | |
969 | < sra->component_size) | |
48865704 PC |
970 | || |
971 | (sra->component_size == 0 && devsize < component_size)) { | |
f08605b3 | 972 | if (verbose > 1) |
e7b84f9d | 973 | pr_err("not adding %s to %s as it is too small\n", |
f08605b3 N |
974 | devname, mp->path); |
975 | goto next; | |
976 | } | |
d2db3045 N |
977 | /* test against target. |
978 | * If 'target' is set and 'bare' is false, we only accept | |
979 | * arrays/containers that match 'target'. | |
980 | * If 'target' is set and 'bare' is true, we prefer the | |
981 | * array which matches 'target'. | |
b4924220 | 982 | * target is considered only if we deal with degraded array |
d2db3045 | 983 | */ |
fee6a49e PC |
984 | if (target && policy_action_allows(pol, st2->ss->name, |
985 | act_spare_same_slot)) { | |
d2db3045 N |
986 | if (strcmp(target->metadata, mp->metadata) == 0 && |
987 | memcmp(target->uuid, mp->uuid, | |
b4924220 PC |
988 | sizeof(target->uuid)) == 0 && |
989 | sra->array.failed_disks > 0) { | |
d2db3045 | 990 | /* This is our target!! */ |
fe112c9e | 991 | sysfs_free(chosen); |
d2db3045 N |
992 | chosen = sra; |
993 | sra = NULL; | |
994 | /* skip to end so we don't check any more */ | |
995 | while (mp->next) | |
996 | mp = mp->next; | |
997 | goto next; | |
998 | } | |
999 | /* not our target */ | |
1000 | if (!bare) | |
1001 | goto next; | |
1002 | } | |
1003 | ||
b6b3f0f7 | 1004 | dl = domain_from_array(sra, st2->ss->name); |
e5508b36 | 1005 | if (domain_test(dl, pol, st2->ss->name) != 1) { |
b6b3f0f7 PC |
1006 | /* domain test fails */ |
1007 | if (verbose > 1) | |
7a862a02 | 1008 | pr_err("not adding %s to %s as it is not in a compatible domain\n", |
b6b3f0f7 PC |
1009 | devname, mp->path); |
1010 | ||
1011 | goto next; | |
1012 | } | |
f08605b3 N |
1013 | /* all tests passed, OK to add to this array */ |
1014 | if (!chosen) { | |
1015 | chosen = sra; | |
1016 | sra = NULL; | |
1017 | } else if (chosen->array.failed_disks < sra->array.failed_disks) { | |
1018 | sysfs_free(chosen); | |
1019 | chosen = sra; | |
1020 | sra = NULL; | |
1021 | } | |
1022 | next: | |
fe112c9e | 1023 | sysfs_free(sra); |
f08605b3 N |
1024 | if (st != st2) |
1025 | free(st2); | |
1026 | if (dl) | |
1027 | domain_free(dl); | |
1028 | } | |
1029 | if (chosen) { | |
1030 | /* add current device to chosen array as a spare */ | |
4dd2df09 | 1031 | int mdfd = open_dev(chosen->sys_name); |
f08605b3 | 1032 | if (mdfd >= 0) { |
a655e550 | 1033 | struct mddev_dev devlist; |
fa9aca49 | 1034 | char chosen_devname[24]; // 2*11 for int (including signs) + colon + null |
f08605b3 N |
1035 | devlist.next = NULL; |
1036 | devlist.used = 0; | |
f08605b3 | 1037 | devlist.writemostly = 0; |
fa9aca49 BS |
1038 | devlist.devname = chosen_devname; |
1039 | sprintf(chosen_devname, "%d:%d", major(stb.st_rdev), | |
f08605b3 N |
1040 | minor(stb.st_rdev)); |
1041 | devlist.disposition = 'a'; | |
1042 | close(dfd); | |
1043 | *dfdp = -1; | |
1044 | rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist, | |
11b391ec | 1045 | -1, 0, NULL, 0); |
f08605b3 N |
1046 | close(mdfd); |
1047 | } | |
1048 | if (verbose > 0) { | |
1049 | if (rv == 0) | |
e7b84f9d | 1050 | pr_err("added %s as spare for %s\n", |
f08605b3 N |
1051 | devname, chosen->sys_name); |
1052 | else | |
e7b84f9d | 1053 | pr_err("failed to add %s as spare for %s\n", |
f08605b3 N |
1054 | devname, chosen->sys_name); |
1055 | } | |
1056 | sysfs_free(chosen); | |
1057 | } | |
015da8f5 | 1058 | map_unlock(&map); |
8659d089 | 1059 | return rv; |
f08605b3 N |
1060 | } |
1061 | ||
56e8be85 N |
1062 | static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol, |
1063 | struct supertype *st, int verbose) | |
1064 | { | |
61018da0 N |
1065 | /* we know that at least one partition virtual-metadata is |
1066 | * allowed to incorporate spares like this device. We need to | |
1067 | * find a suitable device to copy partition information from. | |
1068 | * | |
1069 | * Getting a list of all disk (not partition) devices is | |
1070 | * slightly non-trivial. We could look at /sys/block, but | |
1071 | * that is theoretically due to be removed. Maybe best to use | |
1072 | * /dev/disk/by-path/?* and ignore names ending '-partNN' as | |
1073 | * we depend on this directory of 'path' info. But that fails | |
1074 | * to find loop devices and probably others. Maybe don't | |
1075 | * worry about that, they aren't the real target. | |
1076 | * | |
1077 | * So: check things in /dev/disk/by-path to see if they are in | |
1078 | * a compatible domain, then load the partition table and see | |
1079 | * if it is OK for the new device, and choose the largest | |
1080 | * partition table that fits. | |
1081 | */ | |
1082 | DIR *dir; | |
1083 | struct dirent *de; | |
1084 | char *chosen = NULL; | |
71204a50 | 1085 | unsigned long long chosen_size = 0; |
61018da0 N |
1086 | struct supertype *chosen_st = NULL; |
1087 | int fd; | |
1088 | ||
1089 | dir = opendir("/dev/disk/by-path"); | |
1090 | if (!dir) | |
1091 | return 1; | |
1092 | while ((de = readdir(dir)) != NULL) { | |
1093 | char *ep; | |
1094 | struct dev_policy *pol2 = NULL; | |
1095 | struct domainlist *domlist = NULL; | |
1096 | int fd = -1; | |
1097 | struct mdinfo info; | |
1098 | struct supertype *st2 = NULL; | |
1099 | char *devname = NULL; | |
1100 | unsigned long long devsectors; | |
1101 | ||
1102 | if (de->d_ino == 0 || | |
1103 | de->d_name[0] == '.' || | |
1104 | (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN)) | |
1105 | goto next; | |
1106 | ||
1107 | ep = de->d_name + strlen(de->d_name); | |
1108 | while (ep > de->d_name && | |
1109 | isdigit(ep[-1])) | |
1110 | ep--; | |
1111 | if (ep > de->d_name + 5 && | |
1112 | strncmp(ep-5, "-part", 5) == 0) | |
1113 | /* This is a partition - skip it */ | |
1114 | goto next; | |
1115 | ||
1116 | pol2 = path_policy(de->d_name, type_disk); | |
1117 | ||
1118 | domain_merge(&domlist, pol2, st ? st->ss->name : NULL); | |
e5508b36 | 1119 | if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1) |
61018da0 N |
1120 | /* new device is incompatible with this device. */ |
1121 | goto next; | |
1122 | ||
1123 | domain_free(domlist); | |
1124 | domlist = NULL; | |
1125 | ||
71204a50 N |
1126 | if (asprintf(&devname, "/dev/disk/by-path/%s", de->d_name) != 1) { |
1127 | devname = NULL; | |
1128 | goto next; | |
1129 | } | |
61018da0 N |
1130 | fd = open(devname, O_RDONLY); |
1131 | if (fd < 0) | |
1132 | goto next; | |
1133 | if (get_dev_size(fd, devname, &devsectors) == 0) | |
1134 | goto next; | |
1135 | devsectors >>= 9; | |
1136 | ||
1137 | if (st) | |
1138 | st2 = dup_super(st); | |
1139 | else | |
1140 | st2 = guess_super_type(fd, guess_partitions); | |
1141 | if (st2 == NULL || | |
1142 | st2->ss->load_super(st2, fd, NULL) < 0) | |
1143 | goto next; | |
9f2e55a4 | 1144 | st2->ignore_hw_compat = 0; |
61018da0 N |
1145 | |
1146 | if (!st) { | |
1147 | /* Check domain policy again, this time referring to metadata */ | |
1148 | domain_merge(&domlist, pol2, st2->ss->name); | |
e5508b36 | 1149 | if (domain_test(domlist, pol, st2->ss->name) != 1) |
61018da0 N |
1150 | /* Incompatible devices for this metadata type */ |
1151 | goto next; | |
625b2507 N |
1152 | if (!policy_action_allows(pol, st2->ss->name, act_spare)) |
1153 | /* Some partition types allow sparing, but not | |
1154 | * this one. | |
1155 | */ | |
1156 | goto next; | |
61018da0 N |
1157 | } |
1158 | ||
a5d85af7 | 1159 | st2->ss->getinfo_super(st2, &info, NULL); |
61018da0 N |
1160 | if (info.component_size > devsectors) |
1161 | /* This partitioning doesn't fit in the device */ | |
1162 | goto next; | |
1163 | ||
1164 | /* This is an acceptable device to copy partition | |
1165 | * metadata from. We could just stop here, but I | |
1166 | * think I want to keep looking incase a larger | |
1167 | * metadata which makes better use of the device can | |
1168 | * be found. | |
1169 | */ | |
1170 | if (chosen == NULL || | |
1171 | chosen_size < info.component_size) { | |
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 NB |
1343 | mdu_array_info_t array; |
1344 | mdu_bitmap_file_t bmf; | |
7e0f6979 | 1345 | struct mdinfo *sra; |
d3786cdc N |
1346 | int mdfd; |
1347 | ||
1348 | if (devnm && strcmp(devnm, me->devnm) != 0) | |
1349 | continue; | |
d5a40416 N |
1350 | if (devnm && me->metadata[0] == '/') { |
1351 | char *sl; | |
1352 | /* member array, need to work on container */ | |
1353 | strncpy(container, me->metadata+1, 32); | |
1354 | container[31] = 0; | |
1355 | sl = strchr(container, '/'); | |
1356 | if (sl) | |
1357 | *sl = 0; | |
1358 | only = devnm; | |
1359 | devnm = container; | |
1360 | goto restart; | |
1361 | } | |
d3786cdc | 1362 | mdfd = open_dev(me->devnm); |
215bb3f7 | 1363 | |
8382f19b NB |
1364 | if (mdfd < 0) |
1365 | continue; | |
d5a40416 N |
1366 | if (!isdigit(me->metadata[0])) { |
1367 | /* must be a container */ | |
1368 | struct supertype *st = super_by_fd(mdfd, NULL); | |
1369 | int ret = 0; | |
1370 | struct map_ent *map = NULL; | |
9f2e55a4 | 1371 | |
d5a40416 N |
1372 | if (st && st->ss->load_container) |
1373 | ret = st->ss->load_container(st, mdfd, NULL); | |
1374 | close(mdfd); | |
5d94384e | 1375 | if (!ret && st && st->ss->container_content) { |
d5a40416 N |
1376 | if (map_lock(&map)) |
1377 | pr_err("failed to get exclusive lock on mapfile\n"); | |
1378 | ret = Incremental_container(st, me->path, c, only); | |
1379 | map_unlock(&map); | |
1380 | } | |
1381 | if (ret) | |
1382 | rv = 1; | |
1383 | continue; | |
1384 | } | |
8382f19b NB |
1385 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || |
1386 | errno != ENODEV) { | |
1387 | close(mdfd); | |
1388 | continue; | |
1389 | } | |
1390 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
1391 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
339c2d6c | 1392 | if (mddev->devname && me->path |
2400e6eb | 1393 | && devname_matches(mddev->devname, me->path)) |
8382f19b NB |
1394 | break; |
1395 | if (mddev && mddev->bitmap_file) { | |
1396 | /* | |
1397 | * Note: early kernels will wrongly fail this, so it | |
1398 | * is a hint only | |
1399 | */ | |
1400 | int added = -1; | |
1401 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
1402 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
1403 | if (bmfd >= 0) { | |
1404 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
1405 | bmfd); | |
1406 | close(bmfd); | |
1407 | } | |
1408 | } | |
d5a40416 | 1409 | if (c->verbose >= 0) { |
8382f19b | 1410 | if (added == 0) |
e7b84f9d N |
1411 | pr_err("Added bitmap %s to %s\n", |
1412 | mddev->bitmap_file, me->path); | |
8382f19b | 1413 | else if (errno != EEXIST) |
e7b84f9d N |
1414 | pr_err("Failed to add bitmap to %s: %s\n", |
1415 | me->path, strerror(errno)); | |
8382f19b NB |
1416 | } |
1417 | } | |
5e88ab2e N |
1418 | /* FIXME check for reshape_active and consider not |
1419 | * starting array. | |
1420 | */ | |
4dd2df09 | 1421 | sra = sysfs_read(mdfd, NULL, 0); |
8382f19b NB |
1422 | if (sra) { |
1423 | if (sysfs_set_str(sra, NULL, | |
1424 | "array_state", "read-auto") == 0) { | |
d5a40416 | 1425 | if (c->verbose >= 0) |
e7b84f9d | 1426 | pr_err("started array %s\n", |
4dd2df09 | 1427 | me->path ?: me->devnm); |
8382f19b | 1428 | } else { |
e7b84f9d | 1429 | pr_err("failed to start array %s: %s\n", |
4dd2df09 | 1430 | me->path ?: me->devnm, |
e7b84f9d | 1431 | strerror(errno)); |
8382f19b NB |
1432 | rv = 1; |
1433 | } | |
b1efa6c2 | 1434 | sysfs_free(sra); |
8382f19b NB |
1435 | } |
1436 | } | |
1437 | return rv; | |
1438 | } | |
598f0d58 | 1439 | |
1771a6e2 N |
1440 | static char *container2devname(char *devname) |
1441 | { | |
1442 | char *mdname = NULL; | |
1443 | ||
1444 | if (devname[0] == '/') { | |
1445 | int fd = open(devname, O_RDONLY); | |
1446 | if (fd >= 0) { | |
4dd2df09 | 1447 | mdname = xstrdup(fd2devnm(fd)); |
1771a6e2 N |
1448 | close(fd); |
1449 | } | |
1450 | } else { | |
1451 | int uuid[4]; | |
1452 | struct map_ent *mp, *map = NULL; | |
ca3b6696 | 1453 | |
1771a6e2 N |
1454 | if (!parse_uuid(devname, uuid)) |
1455 | return mdname; | |
1456 | mp = map_by_uuid(&map, uuid); | |
1457 | if (mp) | |
4dd2df09 | 1458 | mdname = xstrdup(mp->devnm); |
1771a6e2 N |
1459 | map_free(map); |
1460 | } | |
1461 | ||
1462 | return mdname; | |
1463 | } | |
1464 | ||
47c74f3f | 1465 | static int Incremental_container(struct supertype *st, char *devname, |
d5a40416 | 1466 | struct context *c, char *only) |
598f0d58 NB |
1467 | { |
1468 | /* Collect the contents of this container and for each | |
1469 | * array, choose a device name and assemble the array. | |
1470 | */ | |
1471 | ||
47c74f3f | 1472 | struct mdinfo *list; |
598f0d58 | 1473 | struct mdinfo *ra; |
ad5bc697 | 1474 | struct map_ent *map = NULL; |
47c74f3f N |
1475 | struct mdinfo info; |
1476 | int trustworthy; | |
fa56eddb | 1477 | struct mddev_ident *match; |
47c74f3f | 1478 | int rv = 0; |
037e12d6 AC |
1479 | struct domainlist *domains; |
1480 | struct map_ent *smp; | |
1481 | int suuid[4]; | |
1482 | int sfd; | |
81219e70 LM |
1483 | int ra_blocked = 0; |
1484 | int ra_all = 0; | |
9ca39acb | 1485 | int result = 0; |
47c74f3f | 1486 | |
47c74f3f N |
1487 | st->ss->getinfo_super(st, &info, NULL); |
1488 | ||
11b6d91d | 1489 | if ((c->runstop > 0 && info.container_enough >= 0) || |
47c74f3f N |
1490 | info.container_enough > 0) |
1491 | /* pass */; | |
1492 | else { | |
9ca39acb N |
1493 | if (c->export) { |
1494 | printf("MD_STARTED=no\n"); | |
1495 | } else if (c->verbose) | |
e7b84f9d | 1496 | pr_err("not enough devices to start the container\n"); |
47c74f3f N |
1497 | return 0; |
1498 | } | |
1499 | ||
11b6d91d | 1500 | match = conf_match(st, &info, devname, c->verbose, &rv); |
47c74f3f N |
1501 | if (match == NULL && rv == 2) |
1502 | return rv; | |
1503 | ||
1504 | /* Need to compute 'trustworthy' */ | |
1505 | if (match) | |
1506 | trustworthy = LOCAL; | |
11b6d91d | 1507 | else if (st->ss->match_home(st, c->homehost) == 1) |
47c74f3f N |
1508 | trustworthy = LOCAL; |
1509 | else if (st->ss->match_home(st, "any") == 1) | |
1510 | trustworthy = LOCAL; | |
1511 | else | |
1512 | trustworthy = FOREIGN; | |
ad5bc697 | 1513 | |
47c74f3f | 1514 | list = st->ss->container_content(st, NULL); |
81219e70 | 1515 | /* when nothing to activate - quit */ |
9ca39acb N |
1516 | if (list == NULL) { |
1517 | if (c->export) { | |
1518 | printf("MD_STARTED=nothing\n"); | |
1519 | } | |
81219e70 | 1520 | return 0; |
9ca39acb | 1521 | } |
598f0d58 | 1522 | for (ra = list ; ra ; ra = ra->next) { |
598f0d58 NB |
1523 | int mdfd; |
1524 | char chosen_name[1024]; | |
ad5bc697 | 1525 | struct map_ent *mp; |
fa56eddb | 1526 | struct mddev_ident *match = NULL; |
598f0d58 | 1527 | |
81219e70 LM |
1528 | ra_all++; |
1529 | /* do not activate arrays blocked by metadata handler */ | |
1530 | if (ra->array.state & (1 << MD_SB_BLOCK_VOLUME)) { | |
e7b84f9d | 1531 | pr_err("Cannot activate array %s in %s.\n", |
81219e70 LM |
1532 | ra->text_version, devname); |
1533 | ra_blocked++; | |
1534 | continue; | |
1535 | } | |
c5afc314 | 1536 | mp = map_by_uuid(&map, ra->uuid); |
d7288ddc | 1537 | |
30926600 | 1538 | if (mp) { |
4dd2df09 | 1539 | mdfd = open_dev(mp->devnm); |
339c2d6c N |
1540 | if (mp->path) |
1541 | strcpy(chosen_name, mp->path); | |
1542 | else | |
4dd2df09 | 1543 | strcpy(chosen_name, mp->devnm); |
d5a40416 | 1544 | } else if (!only) { |
dbb44303 | 1545 | |
112cace6 | 1546 | /* Check in mdadm.conf for container == devname and |
dbb44303 N |
1547 | * member == ra->text_version after second slash. |
1548 | */ | |
1549 | char *sub = strchr(ra->text_version+1, '/'); | |
fa56eddb | 1550 | struct mddev_ident *array_list; |
dbb44303 N |
1551 | if (sub) { |
1552 | sub++; | |
1553 | array_list = conf_get_ident(NULL); | |
1554 | } else | |
1555 | array_list = NULL; | |
1556 | for(; array_list ; array_list = array_list->next) { | |
dbb44303 N |
1557 | char *dn; |
1558 | if (array_list->member == NULL || | |
1559 | array_list->container == NULL) | |
1560 | continue; | |
1561 | if (strcmp(array_list->member, sub) != 0) | |
1562 | continue; | |
71d60c48 DW |
1563 | if (array_list->uuid_set && |
1564 | !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid)) | |
1565 | continue; | |
1771a6e2 N |
1566 | dn = container2devname(array_list->container); |
1567 | if (dn == NULL) | |
dbb44303 | 1568 | continue; |
dbb44303 N |
1569 | if (strncmp(dn, ra->text_version+1, |
1570 | strlen(dn)) != 0 || | |
1571 | ra->text_version[strlen(dn)+1] != '/') { | |
1572 | free(dn); | |
1573 | continue; | |
1574 | } | |
1575 | free(dn); | |
1576 | /* we have a match */ | |
1577 | match = array_list; | |
11b6d91d | 1578 | if (c->verbose>0) |
e7b84f9d | 1579 | pr_err("match found for member %s\n", |
71d60c48 | 1580 | array_list->member); |
dbb44303 N |
1581 | break; |
1582 | } | |
dbb44303 | 1583 | |
112cace6 N |
1584 | if (match && match->devname && |
1585 | strcasecmp(match->devname, "<ignore>") == 0) { | |
11b6d91d | 1586 | if (c->verbose > 0) |
7a862a02 | 1587 | pr_err("array %s/%s is explicitly ignored by mdadm.conf\n", |
e7b84f9d | 1588 | match->container, match->member); |
eb8b9516 | 1589 | continue; |
112cace6 | 1590 | } |
05833051 N |
1591 | if (match) |
1592 | trustworthy = LOCAL; | |
112cace6 | 1593 | |
30926600 N |
1594 | mdfd = create_mddev(match ? match->devname : NULL, |
1595 | ra->name, | |
11b6d91d | 1596 | c->autof, |
30926600 N |
1597 | trustworthy, |
1598 | chosen_name); | |
1599 | } | |
d5a40416 N |
1600 | if (only && (!mp || strcmp(mp->devnm, only) != 0)) |
1601 | continue; | |
598f0d58 NB |
1602 | |
1603 | if (mdfd < 0) { | |
e7b84f9d | 1604 | pr_err("failed to open %s: %s.\n", |
598f0d58 NB |
1605 | chosen_name, strerror(errno)); |
1606 | return 2; | |
1607 | } | |
1608 | ||
11b6d91d | 1609 | assemble_container_content(st, mdfd, ra, c, |
9ca39acb | 1610 | chosen_name, &result); |
588bebfc | 1611 | close(mdfd); |
598f0d58 | 1612 | } |
9ca39acb N |
1613 | if (c->export && result) { |
1614 | char sep = '='; | |
1615 | printf("MD_STARTED"); | |
1616 | if (result & INCR_NO) { | |
1617 | printf("%cno", sep); | |
1618 | sep = ','; | |
1619 | } | |
1620 | if (result & INCR_UNSAFE) { | |
1621 | printf("%cunsafe", sep); | |
1622 | sep = ','; | |
1623 | } | |
1624 | if (result & INCR_ALREADY) { | |
1625 | printf("%calready", sep); | |
1626 | sep = ','; | |
1627 | } | |
1628 | if (result & INCR_YES) { | |
1629 | printf("%cyes", sep); | |
1630 | sep = ','; | |
1631 | } | |
1632 | printf("\n"); | |
1633 | } | |
037e12d6 | 1634 | |
81219e70 LM |
1635 | /* don't move spares to container with volume being activated |
1636 | when all volumes are blocked */ | |
ad098cdd | 1637 | if (ra_all == ra_blocked) |
81219e70 | 1638 | return 0; |
81219e70 | 1639 | |
037e12d6 AC |
1640 | /* Now move all suitable spares from spare container */ |
1641 | domains = domain_from_array(list, st->ss->name); | |
1642 | memcpy(suuid, uuid_zero, sizeof(int[4])); | |
1643 | if (domains && | |
1644 | (smp = map_by_uuid(&map, suuid)) != NULL && | |
1645 | (sfd = open(smp->path, O_RDONLY)) >= 0) { | |
1646 | /* spare container found */ | |
1647 | struct supertype *sst = | |
1648 | super_imsm.match_metadata_desc("imsm"); | |
1649 | struct mdinfo *sinfo; | |
1650 | unsigned long long min_size = 0; | |
1651 | if (st->ss->min_acceptable_spare_size) | |
1652 | min_size = st->ss->min_acceptable_spare_size(st); | |
1653 | if (!sst->ss->load_container(sst, sfd, NULL)) { | |
1654 | close(sfd); | |
1655 | sinfo = container_choose_spares(sst, min_size, | |
1656 | domains, NULL, | |
1657 | st->ss->name, 0); | |
1658 | sst->ss->free_super(sst); | |
1659 | if (sinfo){ | |
1660 | int count = 0; | |
1661 | struct mdinfo *disks = sinfo->devs; | |
1662 | while (disks) { | |
1663 | /* move spare from spare | |
1664 | * container to currently | |
1665 | * assembled one | |
1666 | */ | |
1667 | if (move_spare( | |
1668 | smp->path, | |
1669 | devname, | |
1670 | makedev(disks->disk.major, | |
1671 | disks->disk.minor))) | |
1672 | count++; | |
1673 | disks = disks->next; | |
1674 | } | |
1675 | if (count) | |
e7b84f9d N |
1676 | pr_err("Added %d spare%s to %s\n", |
1677 | count, count>1?"s":"", devname); | |
037e12d6 AC |
1678 | } |
1679 | sysfs_free(sinfo); | |
1680 | } else | |
1681 | close(sfd); | |
1682 | } | |
1683 | domain_free(domains); | |
598f0d58 NB |
1684 | return 0; |
1685 | } | |
29ba4804 | 1686 | |
6b63c1a4 N |
1687 | static void run_udisks(char *arg1, char *arg2) |
1688 | { | |
1689 | int pid = fork(); | |
1690 | int status; | |
1691 | if (pid == 0) { | |
1692 | execl("/usr/bin/udisks", "udisks", arg1, arg2, NULL); | |
1693 | execl("/bin/udisks", "udisks", arg1, arg2, NULL); | |
1694 | exit(1); | |
1695 | } | |
1696 | while (pid > 0 && wait(&status) != pid) | |
1697 | ; | |
1698 | } | |
1699 | ||
29ba4804 N |
1700 | /* |
1701 | * IncrementalRemove - Attempt to see if the passed in device belongs to any | |
1702 | * raid arrays, and if so first fail (if needed) and then remove the device. | |
1703 | * | |
1704 | * @devname - The device we want to remove | |
950bc344 | 1705 | * @id_path - name as found in /dev/disk/by-path for this device |
29ba4804 N |
1706 | * |
1707 | * Note: the device name must be a kernel name like "sda", so | |
1708 | * that we can find it in /proc/mdstat | |
1709 | */ | |
950bc344 | 1710 | int IncrementalRemove(char *devname, char *id_path, int verbose) |
29ba4804 N |
1711 | { |
1712 | int mdfd; | |
8af530b0 | 1713 | int rv = 0; |
29ba4804 | 1714 | struct mdstat_ent *ent; |
a655e550 | 1715 | struct mddev_dev devlist; |
8af530b0 N |
1716 | struct mdinfo mdi; |
1717 | char buf[32]; | |
29ba4804 | 1718 | |
950bc344 | 1719 | if (!id_path) |
7a862a02 | 1720 | dprintf("incremental removal without --path <id_path> lacks the possibility to re-add new device in this port\n"); |
950bc344 | 1721 | |
29ba4804 | 1722 | if (strchr(devname, '/')) { |
7a862a02 | 1723 | pr_err("incremental removal requires a kernel device name, not a file: %s\n", devname); |
29ba4804 N |
1724 | return 1; |
1725 | } | |
1726 | ent = mdstat_by_component(devname); | |
1727 | if (!ent) { | |
041b815f | 1728 | if (verbose >= 0) |
7a862a02 | 1729 | pr_err("%s does not appear to be a component of any array\n", devname); |
29ba4804 N |
1730 | return 1; |
1731 | } | |
8af530b0 | 1732 | sysfs_init(&mdi, -1, ent->devnm); |
4d149ab5 PB |
1733 | mdfd = open_dev_excl(ent->devnm); |
1734 | if (mdfd > 0) { | |
1735 | close(mdfd); | |
1736 | if (sysfs_get_str(&mdi, NULL, "array_state", | |
1737 | buf, sizeof(buf)) > 0) { | |
1738 | if (strncmp(buf, "active", 6) == 0 || | |
1739 | strncmp(buf, "clean", 5) == 0) | |
1740 | sysfs_set_str(&mdi, NULL, | |
1741 | "array_state", "read-auto"); | |
1742 | } | |
8af530b0 | 1743 | } |
4dd2df09 | 1744 | mdfd = open_dev(ent->devnm); |
29ba4804 | 1745 | if (mdfd < 0) { |
041b815f | 1746 | if (verbose >= 0) |
9581efb1 | 1747 | pr_err("Cannot open array %s!!\n", ent->devnm); |
08387a04 | 1748 | free_mdstat(ent); |
29ba4804 N |
1749 | return 1; |
1750 | } | |
403410eb PC |
1751 | |
1752 | if (id_path) { | |
1753 | struct map_ent *map = NULL, *me; | |
4dd2df09 | 1754 | me = map_by_devnm(&map, ent->devnm); |
403410eb PC |
1755 | if (me) |
1756 | policy_save_path(id_path, me); | |
1757 | map_free(map); | |
1758 | } | |
1759 | ||
29ba4804 N |
1760 | memset(&devlist, 0, sizeof(devlist)); |
1761 | devlist.devname = devname; | |
1762 | devlist.disposition = 'f'; | |
08387a04 N |
1763 | /* for a container, we must fail each member array */ |
1764 | if (ent->metadata_version && | |
1765 | strncmp(ent->metadata_version, "external:", 9) == 0) { | |
1766 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
1767 | struct mdstat_ent *memb; | |
1768 | for (memb = mdstat ; memb ; memb = memb->next) | |
9581efb1 | 1769 | if (is_container_member(memb, ent->devnm)) { |
4dd2df09 | 1770 | int subfd = open_dev(memb->devnm); |
08387a04 | 1771 | if (subfd >= 0) { |
8af530b0 | 1772 | rv |= Manage_subdevs( |
9581efb1 | 1773 | memb->devnm, subfd, |
8af530b0 N |
1774 | &devlist, verbose, 0, |
1775 | NULL, 0); | |
08387a04 N |
1776 | close(subfd); |
1777 | } | |
1778 | } | |
1779 | free_mdstat(mdstat); | |
1780 | } else | |
9581efb1 | 1781 | rv |= Manage_subdevs(ent->devnm, mdfd, &devlist, |
8af530b0 N |
1782 | verbose, 0, NULL, 0); |
1783 | if (rv & 2) { | |
6b63c1a4 N |
1784 | /* Failed due to EBUSY, try to stop the array. |
1785 | * Give udisks a chance to unmount it first. | |
8af530b0 | 1786 | */ |
6b63c1a4 N |
1787 | int devid = devnm2devid(ent->devnm); |
1788 | run_udisks("--unmount", map_dev(major(devid),minor(devid), 0)); | |
9581efb1 | 1789 | rv = Manage_stop(ent->devnm, mdfd, verbose, 1); |
8af530b0 N |
1790 | if (rv) |
1791 | /* At least we can try to trigger a 'remove' */ | |
1792 | sysfs_uevent(&mdi, "remove"); | |
6b63c1a4 N |
1793 | if (verbose) { |
1794 | if (rv) | |
1795 | pr_err("Fail to stop %s too.\n", ent->devnm); | |
1796 | } | |
8af530b0 N |
1797 | } else { |
1798 | devlist.disposition = 'r'; | |
9581efb1 | 1799 | rv = Manage_subdevs(ent->devnm, mdfd, &devlist, |
8af530b0 N |
1800 | verbose, 0, NULL, 0); |
1801 | } | |
aae3cdc3 | 1802 | close(mdfd); |
08387a04 | 1803 | free_mdstat(ent); |
aae3cdc3 | 1804 | return rv; |
29ba4804 | 1805 | } |