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