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