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