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