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