]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Incremental.c - support --incremental. Part of: | |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
4 | * | |
5 | * Copyright (C) 2006-2013 Neil Brown <neilb@suse.de> | |
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" | |
32 | #include "xmalloc.h" | |
33 | #include "udev.h" | |
34 | ||
35 | #include <sys/wait.h> | |
36 | #include <dirent.h> | |
37 | #include <ctype.h> | |
38 | ||
39 | static int count_active(struct supertype *st, struct mdinfo *sra, | |
40 | int mdfd, char **availp, | |
41 | struct mdinfo *info); | |
42 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, | |
43 | int number, __u64 events, int verbose, | |
44 | char *array_name); | |
45 | static int try_spare(char *devname, int *dfdp, struct dev_policy *pol, | |
46 | struct map_ent *target, | |
47 | struct supertype *st, int verbose); | |
48 | ||
49 | static int Incremental_container(struct supertype *st, char *devname, | |
50 | struct context *c, char *only); | |
51 | ||
52 | int Incremental(struct mddev_dev *devlist, struct context *c, | |
53 | struct supertype *st) | |
54 | { | |
55 | /* Add this device to an array, creating the array if necessary | |
56 | * and starting the array if sensible or - if runstop>0 - if possible. | |
57 | * | |
58 | * This has several steps: | |
59 | * | |
60 | * 1/ Check if device is permitted by mdadm.conf, reject if not. | |
61 | * 2/ Find metadata, reject if none appropriate (check | |
62 | * version/name from args) | |
63 | * 3/ Check if there is a match in mdadm.conf | |
64 | * 3a/ if not, check for homehost match. If no match, assemble as | |
65 | * a 'foreign' array. | |
66 | * 4/ Determine device number. | |
67 | * - If in mdadm.conf with std name, use that | |
68 | * - UUID in /var/run/mdadm.map use that | |
69 | * - If name is suggestive, use that. unless in use with different uuid. | |
70 | * - Choose a free, high number. | |
71 | * - Use a partitioned device unless strong suggestion not to. | |
72 | * e.g. auto=md | |
73 | * Don't choose partitioned for containers. | |
74 | * 5/ Find out if array already exists | |
75 | * 5a/ if it does not | |
76 | * - choose a name, from mdadm.conf or 'name' field in array. | |
77 | * - create the array | |
78 | * - add the device | |
79 | * 5b/ if it does | |
80 | * - check one drive in array to make sure metadata is a reasonably | |
81 | * close match. Reject if not (e.g. different type) | |
82 | * - add the device | |
83 | * 6/ Make sure /var/run/mdadm.map contains this array. | |
84 | * 7/ Is there enough devices to possibly start the array? | |
85 | * For a container, this means running Incremental_container. | |
86 | * 7a/ if not, finish with success. | |
87 | * 7b/ if yes, | |
88 | * - read all metadata and arrange devices like -A does | |
89 | * - if number of OK devices match expected, or -R and there are enough, | |
90 | * start the array (auto-readonly). | |
91 | */ | |
92 | dev_t rdev, rdev2; | |
93 | struct mdinfo info, dinfo; | |
94 | struct mdinfo *sra = NULL, *d; | |
95 | struct mddev_ident *match; | |
96 | char chosen_name[1024]; | |
97 | char *md_devname; | |
98 | int rv = 1; | |
99 | struct map_ent *mp, *map = NULL; | |
100 | int dfd = -1, mdfd = -1; | |
101 | char *avail = NULL; | |
102 | int active_disks; | |
103 | int trustworthy; | |
104 | char *name_to_use; | |
105 | struct dev_policy *policy = NULL; | |
106 | struct map_ent target_array; | |
107 | int have_target; | |
108 | char *devname = devlist->devname; | |
109 | int journal_device_missing = 0; | |
110 | ||
111 | if (!stat_is_blkdev(devname, &rdev)) | |
112 | return rv; | |
113 | dfd = dev_open(devname, O_RDONLY); | |
114 | if (dfd < 0) { | |
115 | if (c->verbose >= 0) | |
116 | pr_err("cannot open %s: %s.\n", | |
117 | devname, strerror(errno)); | |
118 | return rv; | |
119 | } | |
120 | /* If the device is a container, we do something very different */ | |
121 | if (must_be_container(dfd)) { | |
122 | if (!st) | |
123 | st = super_by_fd(dfd, NULL); | |
124 | if (st && st->ss->load_container) | |
125 | rv = st->ss->load_container(st, dfd, NULL); | |
126 | ||
127 | close(dfd); | |
128 | if (!rv && st->ss->container_content) { | |
129 | if (map_lock(&map)) | |
130 | pr_err("failed to get exclusive lock on mapfile\n"); | |
131 | if (c->export) | |
132 | printf("MD_DEVNAME=%s\n", devname); | |
133 | rv = Incremental_container(st, devname, c, NULL); | |
134 | map_unlock(&map); | |
135 | return rv; | |
136 | } | |
137 | ||
138 | pr_err("%s is not part of an md array.\n", | |
139 | devname); | |
140 | return rv; | |
141 | } | |
142 | ||
143 | /* 1/ Check if device is permitted by mdadm.conf */ | |
144 | ||
145 | for (;devlist; devlist = devlist->next) | |
146 | if (conf_test_dev(devlist->devname)) | |
147 | break; | |
148 | if (!devlist) { | |
149 | devlist = conf_get_devs(); | |
150 | for (;devlist; devlist = devlist->next) { | |
151 | if (stat_is_blkdev(devlist->devname, &rdev2) && | |
152 | rdev2 == rdev) | |
153 | break; | |
154 | } | |
155 | } | |
156 | if (!devlist) { | |
157 | if (c->verbose >= 0) | |
158 | pr_err("%s not permitted by mdadm.conf.\n", | |
159 | devname); | |
160 | goto out; | |
161 | } | |
162 | ||
163 | /* 2/ Find metadata, reject if none appropriate (check | |
164 | * version/name from args) */ | |
165 | ||
166 | if (!fstat_is_blkdev(dfd, devname, &rdev)) | |
167 | goto out; | |
168 | ||
169 | dinfo.disk.major = major(rdev); | |
170 | dinfo.disk.minor = minor(rdev); | |
171 | ||
172 | policy = disk_policy(&dinfo); | |
173 | have_target = policy_check_path(&dinfo, &target_array); | |
174 | ||
175 | if (st == NULL && (st = guess_super_type(dfd, guess_array)) == NULL) { | |
176 | if (c->verbose >= 0) | |
177 | pr_err("no recognisable superblock on %s.\n", | |
178 | devname); | |
179 | rv = try_spare(devname, &dfd, policy, | |
180 | have_target ? &target_array : NULL, | |
181 | NULL, c->verbose); | |
182 | goto out; | |
183 | } | |
184 | st->ignore_hw_compat = 0; | |
185 | ||
186 | if (st->ss->compare_super == NULL || | |
187 | st->ss->load_super(st, dfd, c->verbose >= 0 ? devname : NULL)) { | |
188 | if (c->verbose >= 0) | |
189 | pr_err("no RAID superblock on %s.\n", | |
190 | devname); | |
191 | rv = try_spare(devname, &dfd, policy, | |
192 | have_target ? &target_array : NULL, | |
193 | st, c->verbose); | |
194 | free(st); | |
195 | goto out; | |
196 | } | |
197 | close (dfd); dfd = -1; | |
198 | ||
199 | st->ss->getinfo_super(st, &info, NULL); | |
200 | ||
201 | /* 3/ Check if there is a match in mdadm.conf */ | |
202 | match = conf_match(st, &info, devname, c->verbose, &rv); | |
203 | if (!match && rv == 2) | |
204 | goto out; | |
205 | ||
206 | if (match && match->devname && is_devname_ignore(match->devname) == true) { | |
207 | if (c->verbose >= 0) | |
208 | pr_err("array containing %s is explicitly ignored by mdadm.conf\n", | |
209 | devname); | |
210 | goto out; | |
211 | } | |
212 | ||
213 | /* 3a/ if not, check for homehost match. If no match, continue | |
214 | * but don't trust the 'name' in the array. Thus a 'random' minor | |
215 | * number will be assigned, and the device name will be based | |
216 | * on that. */ | |
217 | if (match) | |
218 | trustworthy = LOCAL; | |
219 | else if (st->ss->match_home(st, c->homehost) == 1) | |
220 | trustworthy = LOCAL; | |
221 | else if (st->ss->match_home(st, "any") == 1) | |
222 | trustworthy = LOCAL_ANY; | |
223 | else | |
224 | trustworthy = FOREIGN; | |
225 | ||
226 | if (!match && !conf_test_metadata(st->ss->name, policy, | |
227 | (trustworthy == LOCAL))) { | |
228 | if (c->verbose >= 1) | |
229 | pr_err("%s has metadata type %s for which auto-assembly is disabled\n", | |
230 | devname, st->ss->name); | |
231 | goto out; | |
232 | } | |
233 | if (trustworthy == LOCAL_ANY) | |
234 | trustworthy = LOCAL; | |
235 | ||
236 | name_to_use = info.name; | |
237 | if (name_to_use[0] == 0 && is_container(info.array.level)) { | |
238 | name_to_use = info.text_version; | |
239 | trustworthy = METADATA; | |
240 | } | |
241 | if (name_to_use[0] && trustworthy != LOCAL && | |
242 | ! c->require_homehost && | |
243 | conf_name_is_free(name_to_use)) | |
244 | trustworthy = LOCAL; | |
245 | ||
246 | /* strip "hostname:" prefix from name if we have decided | |
247 | * to treat it as LOCAL | |
248 | */ | |
249 | if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL) | |
250 | name_to_use = strchr(name_to_use, ':')+1; | |
251 | ||
252 | /* 4/ Check if array exists. | |
253 | */ | |
254 | if (map_lock(&map)) | |
255 | pr_err("failed to get exclusive lock on mapfile\n"); | |
256 | /* Now check we can get O_EXCL. If not, probably "mdadm -A" has | |
257 | * taken over | |
258 | */ | |
259 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
260 | if (dfd < 0) { | |
261 | if (c->verbose >= 0) | |
262 | pr_err("cannot reopen %s: %s.\n", | |
263 | devname, strerror(errno)); | |
264 | goto out_unlock; | |
265 | } | |
266 | /* Cannot hold it open while we add the device to the array, | |
267 | * so we must release the O_EXCL and depend on the map_lock() | |
268 | * So now is the best time to remove any partitions. | |
269 | */ | |
270 | remove_partitions(dfd); | |
271 | close(dfd); | |
272 | dfd = -1; | |
273 | ||
274 | mp = map_by_uuid(&map, info.uuid); | |
275 | if (mp) | |
276 | mdfd = open_dev(mp->devnm); | |
277 | else | |
278 | mdfd = -1; | |
279 | ||
280 | if (mdfd < 0) { | |
281 | ||
282 | /* Skip the clustered ones. This should be started by | |
283 | * clustering resource agents | |
284 | */ | |
285 | if (info.array.state & (1 << MD_SB_CLUSTERED)) | |
286 | goto out_unlock; | |
287 | ||
288 | /* Couldn't find an existing array, maybe make a new one */ | |
289 | mdfd = create_mddev(match ? match->devname : NULL, name_to_use, trustworthy, | |
290 | chosen_name, 1); | |
291 | ||
292 | if (mdfd < 0) | |
293 | goto out_unlock; | |
294 | ||
295 | if (sysfs_init(&info, mdfd, NULL)) { | |
296 | pr_err("unable to initialize sysfs for %s\n", | |
297 | chosen_name); | |
298 | rv = 2; | |
299 | goto out_unlock; | |
300 | } | |
301 | ||
302 | if (set_array_info(mdfd, st, &info) != 0) { | |
303 | pr_err("failed to set array info for %s: %s\n", | |
304 | chosen_name, strerror(errno)); | |
305 | rv = 2; | |
306 | goto out_unlock; | |
307 | } | |
308 | ||
309 | dinfo = info; | |
310 | dinfo.disk.major = major(rdev); | |
311 | dinfo.disk.minor = minor(rdev); | |
312 | if (add_disk(mdfd, st, &info, &dinfo) != 0) { | |
313 | pr_err("failed to add %s to new array %s: %s.\n", | |
314 | devname, chosen_name, strerror(errno)); | |
315 | ioctl(mdfd, STOP_ARRAY, 0); | |
316 | rv = 2; | |
317 | goto out_unlock; | |
318 | } | |
319 | sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE | | |
320 | GET_OFFSET | GET_SIZE)); | |
321 | ||
322 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { | |
323 | /* It really should be 'none' - must be old buggy | |
324 | * kernel, and mdadm -I may not be able to complete. | |
325 | * So reject it. | |
326 | */ | |
327 | ioctl(mdfd, STOP_ARRAY, NULL); | |
328 | pr_err("You have an old buggy kernel which cannot support\n --incremental reliably. Aborting.\n"); | |
329 | rv = 2; | |
330 | goto out_unlock; | |
331 | } | |
332 | info.array.working_disks = 1; | |
333 | /* 6/ Make sure /var/run/mdadm.map contains this array. */ | |
334 | map_update(&map, fd2devnm(mdfd), | |
335 | info.text_version, | |
336 | info.uuid, chosen_name); | |
337 | } else { | |
338 | /* 5b/ if it does */ | |
339 | /* - check one drive in array to make sure metadata is a reasonably */ | |
340 | /* close match. Reject if not (e.g. different type) */ | |
341 | /* - add the device */ | |
342 | char dn[20]; | |
343 | int dfd2; | |
344 | int err; | |
345 | struct supertype *st2; | |
346 | struct mdinfo info2, *d; | |
347 | ||
348 | sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE | | |
349 | GET_OFFSET | GET_SIZE)); | |
350 | ||
351 | if (mp->path) | |
352 | strcpy(chosen_name, mp->path); | |
353 | else | |
354 | strcpy(chosen_name, mp->devnm); | |
355 | ||
356 | /* It is generally not OK to add non-spare drives to a | |
357 | * running array as they are probably missing because | |
358 | * they failed. However if runstop is 1, then the | |
359 | * array was possibly started early and our best bet is | |
360 | * to add this anyway. | |
361 | * Also if action policy is re-add or better we allow | |
362 | * re-add. | |
363 | * This doesn't apply to containers as the 'non-spare' | |
364 | * flag has a different meaning. The test has to happen | |
365 | * at the device level there | |
366 | */ | |
367 | if (!st->ss->external && | |
368 | (info.disk.state & (1 << MD_DISK_SYNC)) != 0 && | |
369 | !policy_action_allows(policy, st->ss->name, act_re_add) && | |
370 | c->runstop < 1) { | |
371 | if (md_array_active(mdfd)) { | |
372 | pr_err("not adding %s to active array (without --run) %s\n", | |
373 | devname, chosen_name); | |
374 | rv = 2; | |
375 | goto out_unlock; | |
376 | } | |
377 | } | |
378 | if (!sra) { | |
379 | rv = 2; | |
380 | goto out_unlock; | |
381 | } | |
382 | if (sra->devs) { | |
383 | sprintf(dn, "%d:%d", sra->devs->disk.major, | |
384 | sra->devs->disk.minor); | |
385 | dfd2 = dev_open(dn, O_RDONLY); | |
386 | if (dfd2 < 0) { | |
387 | pr_err("unable to open %s\n", devname); | |
388 | rv = 2; | |
389 | goto out_unlock; | |
390 | } | |
391 | st2 = dup_super(st); | |
392 | if (st2->ss->load_super(st2, dfd2, NULL) || | |
393 | st->ss->compare_super(st, st2, 1) != 0) { | |
394 | pr_err("metadata mismatch between %s and chosen array %s\n", | |
395 | devname, chosen_name); | |
396 | close(dfd2); | |
397 | rv = 2; | |
398 | goto out_unlock; | |
399 | } | |
400 | close(dfd2); | |
401 | st2->ss->getinfo_super(st2, &info2, NULL); | |
402 | st2->ss->free_super(st2); | |
403 | if (info.array.level != info2.array.level || | |
404 | memcmp(info.uuid, info2.uuid, 16) != 0 || | |
405 | info.array.raid_disks != info2.array.raid_disks) { | |
406 | pr_err("unexpected difference between %s and %s.\n", | |
407 | chosen_name, devname); | |
408 | rv = 2; | |
409 | goto out_unlock; | |
410 | } | |
411 | } | |
412 | info.disk.major = major(rdev); | |
413 | info.disk.minor = minor(rdev); | |
414 | /* add disk needs to know about containers */ | |
415 | if (st->ss->external) | |
416 | sra->array.level = LEVEL_CONTAINER; | |
417 | ||
418 | if (info.array.state & (1 << MD_SB_CLUSTERED)) | |
419 | info.disk.state |= (1 << MD_DISK_CLUSTER_ADD); | |
420 | ||
421 | err = add_disk(mdfd, st, sra, &info); | |
422 | if (err < 0 && errno == EBUSY) { | |
423 | /* could be another device present with the same | |
424 | * disk.number. Find and reject any such | |
425 | */ | |
426 | find_reject(mdfd, st, sra, info.disk.number, | |
427 | info.events, c->verbose, chosen_name); | |
428 | err = add_disk(mdfd, st, sra, &info); | |
429 | } | |
430 | if (err < 0 && errno == EINVAL && | |
431 | info.disk.state & (1<<MD_DISK_SYNC)) { | |
432 | /* Maybe it needs to be added as a spare */ | |
433 | if (policy_action_allows(policy, st->ss->name, | |
434 | act_force_spare)) { | |
435 | info.disk.state &= ~(1<<MD_DISK_SYNC); | |
436 | err = add_disk(mdfd, st, sra, &info); | |
437 | } else | |
438 | if (c->verbose >= 0) | |
439 | pr_err("can only add %s to %s as a spare, and force-spare is not set.\n", | |
440 | devname, chosen_name); | |
441 | } | |
442 | if (err < 0) { | |
443 | pr_err("failed to add %s to existing array %s: %s.\n", | |
444 | devname, chosen_name, strerror(errno)); | |
445 | rv = 2; | |
446 | goto out_unlock; | |
447 | } | |
448 | info.array.working_disks = 0; | |
449 | for (d = sra->devs; d; d=d->next) | |
450 | info.array.working_disks ++; | |
451 | } | |
452 | if (strncmp(chosen_name, DEV_MD_DIR, DEV_MD_DIR_LEN) == 0) | |
453 | md_devname = chosen_name + DEV_MD_DIR_LEN; | |
454 | else | |
455 | md_devname = chosen_name; | |
456 | if (c->export) { | |
457 | printf("MD_DEVICE=%s\n", fd2devnm(mdfd)); | |
458 | printf("MD_DEVNAME=%s\n", md_devname); | |
459 | printf("MD_FOREIGN=%s\n", trustworthy == FOREIGN ? "yes" : "no"); | |
460 | } | |
461 | ||
462 | /* 7/ Is there enough devices to possibly start the array? */ | |
463 | /* 7a/ if not, finish with success. */ | |
464 | if (is_container(info.array.level)) { | |
465 | char devnm[32]; | |
466 | /* Try to assemble within the container */ | |
467 | if (!c->export && c->verbose >= 0) | |
468 | pr_err("container %s now has %d device%s\n", | |
469 | chosen_name, info.array.working_disks, | |
470 | info.array.working_disks == 1?"":"s"); | |
471 | sysfs_rules_apply(chosen_name, &info); | |
472 | wait_for(chosen_name, mdfd); | |
473 | if (st->ss->external) | |
474 | strcpy(devnm, fd2devnm(mdfd)); | |
475 | if (st->ss->load_container) | |
476 | rv = st->ss->load_container(st, mdfd, NULL); | |
477 | close(mdfd); | |
478 | udev_unblock(); | |
479 | sysfs_uevent(sra, "change"); | |
480 | sysfs_free(sra); | |
481 | if (!rv) | |
482 | rv = Incremental_container(st, chosen_name, c, NULL); | |
483 | map_unlock(&map); | |
484 | /* after spare is added, ping monitor for external metadata | |
485 | * so that it can eg. try to rebuild degraded array */ | |
486 | if (st->ss->external) | |
487 | ping_monitor(devnm); | |
488 | udev_unblock(); | |
489 | return rv; | |
490 | } | |
491 | ||
492 | /* We have added something to the array, so need to re-read the | |
493 | * state. Eventually this state should be kept up-to-date as | |
494 | * things change. | |
495 | */ | |
496 | sysfs_free(sra); | |
497 | sra = sysfs_read(mdfd, NULL, (GET_DEVS | GET_STATE | | |
498 | GET_OFFSET | GET_SIZE)); | |
499 | active_disks = count_active(st, sra, mdfd, &avail, &info); | |
500 | ||
501 | if (!avail) | |
502 | goto out_unlock; | |
503 | ||
504 | journal_device_missing = (info.journal_device_required) && (info.journal_clean == 0); | |
505 | ||
506 | if (info.consistency_policy == CONSISTENCY_POLICY_PPL) | |
507 | info.array.state |= 1; | |
508 | ||
509 | if (enough(info.array.level, info.array.raid_disks, | |
510 | info.array.layout, info.array.state & 1, avail) == 0) { | |
511 | if (c->export) { | |
512 | printf("MD_STARTED=no\n"); | |
513 | } else if (c->verbose >= 0) | |
514 | pr_err("%s attached to %s, not enough to start (%d).\n", | |
515 | devname, chosen_name, active_disks); | |
516 | rv = 0; | |
517 | goto out_unlock; | |
518 | } | |
519 | ||
520 | /* 7b/ if yes, */ | |
521 | /* - if number of OK devices match expected, or -R and there */ | |
522 | /* are enough, */ | |
523 | /* + add any bitmap file */ | |
524 | /* + start the array (auto-readonly). */ | |
525 | ||
526 | if (md_array_active(mdfd)) { | |
527 | if (c->export) { | |
528 | printf("MD_STARTED=already\n"); | |
529 | } else if (c->verbose >= 0) | |
530 | pr_err("%s attached to %s which is already active.\n", | |
531 | devname, chosen_name); | |
532 | rv = 0; | |
533 | goto out_unlock; | |
534 | } | |
535 | ||
536 | map_unlock(&map); | |
537 | if (c->runstop > 0 || (!journal_device_missing && active_disks >= info.array.working_disks)) { | |
538 | struct mdinfo *dsk; | |
539 | /* Let's try to start it */ | |
540 | ||
541 | if (journal_device_missing) | |
542 | pr_err("Trying to run with missing journal device\n"); | |
543 | if (info.reshape_active && !(info.reshape_active & RESHAPE_NO_BACKUP)) { | |
544 | pr_err("%s: This array is being reshaped and cannot be started\n", | |
545 | chosen_name); | |
546 | cont_err("by --incremental. Please use --assemble\n"); | |
547 | goto out; | |
548 | } | |
549 | ||
550 | /* Need to remove from the array any devices which | |
551 | * 'count_active' discerned were too old or inappropriate | |
552 | */ | |
553 | for (d = sra ? sra->devs : NULL ; d ; d = d->next) | |
554 | if (d->disk.state & (1<<MD_DISK_REMOVED)) | |
555 | remove_disk(mdfd, st, sra, d); | |
556 | ||
557 | if ((sra == NULL || active_disks >= info.array.working_disks) && | |
558 | trustworthy != FOREIGN) | |
559 | rv = ioctl(mdfd, RUN_ARRAY, NULL); | |
560 | else | |
561 | rv = sysfs_set_str(sra, NULL, | |
562 | "array_state", "read-auto"); | |
563 | /* Array might be O_EXCL which will interfere with | |
564 | * fsck and mount. So re-open without O_EXCL. | |
565 | */ | |
566 | reopen_mddev(mdfd); | |
567 | if (rv == 0) { | |
568 | if (c->export) { | |
569 | printf("MD_STARTED=yes\n"); | |
570 | } else if (c->verbose >= 0) | |
571 | pr_err("%s attached to %s, which has been started.\n", | |
572 | devname, chosen_name); | |
573 | rv = 0; | |
574 | wait_for(chosen_name, mdfd); | |
575 | /* We just started the array, so some devices | |
576 | * might have been evicted from the array | |
577 | * because their event counts were too old. | |
578 | * If the action=re-add policy is in-force for | |
579 | * those devices we should re-add them now. | |
580 | */ | |
581 | for (dsk = sra->devs; dsk ; dsk = dsk->next) { | |
582 | if (disk_action_allows(dsk, st->ss->name, | |
583 | act_re_add) && | |
584 | add_disk(mdfd, st, sra, dsk) == 0) | |
585 | pr_err("%s re-added to %s\n", | |
586 | dsk->sys_name, chosen_name); | |
587 | } | |
588 | } else { | |
589 | pr_err("%s attached to %s, but failed to start: %s.\n", | |
590 | devname, chosen_name, strerror(errno)); | |
591 | rv = 1; | |
592 | } | |
593 | } else { | |
594 | if (c->export) { | |
595 | printf("MD_STARTED=unsafe\n"); | |
596 | } else if (journal_device_missing) { | |
597 | pr_err("Journal device is missing, not safe to start yet.\n"); | |
598 | } else if (c->verbose >= 0) | |
599 | pr_err("%s attached to %s, not enough to start safely.\n", | |
600 | devname, chosen_name); | |
601 | rv = 0; | |
602 | } | |
603 | out: | |
604 | free(avail); | |
605 | if (dfd >= 0) | |
606 | close(dfd); | |
607 | if (mdfd >= 0) | |
608 | close(mdfd); | |
609 | if (policy) | |
610 | dev_policy_free(policy); | |
611 | udev_unblock(); | |
612 | if (sra) { | |
613 | sysfs_uevent(sra, "change"); | |
614 | sysfs_free(sra); | |
615 | } | |
616 | return rv; | |
617 | out_unlock: | |
618 | map_unlock(&map); | |
619 | goto out; | |
620 | } | |
621 | ||
622 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, | |
623 | int number, __u64 events, int verbose, | |
624 | char *array_name) | |
625 | { | |
626 | /* Find a device attached to this array with a disk.number of number | |
627 | * and events less than the passed events, and remove the device. | |
628 | */ | |
629 | struct mdinfo *d; | |
630 | ||
631 | if (md_array_active(mdfd)) | |
632 | return; /* not safe to remove from active arrays | |
633 | * without thinking more */ | |
634 | ||
635 | for (d = sra->devs; d ; d = d->next) { | |
636 | char dn[24]; // 2*11 bytes for ints (including sign) + colon + null byte | |
637 | int dfd; | |
638 | struct mdinfo info; | |
639 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); | |
640 | dfd = dev_open(dn, O_RDONLY); | |
641 | if (dfd < 0) | |
642 | continue; | |
643 | if (st->ss->load_super(st, dfd, NULL)) { | |
644 | close(dfd); | |
645 | continue; | |
646 | } | |
647 | st->ss->getinfo_super(st, &info, NULL); | |
648 | st->ss->free_super(st); | |
649 | close(dfd); | |
650 | ||
651 | if (info.disk.number != number || info.events >= events) | |
652 | continue; | |
653 | ||
654 | if (d->disk.raid_disk > -1) | |
655 | sysfs_set_str(sra, d, "slot", STR_COMMON_NONE); | |
656 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
657 | if (verbose >= 0) | |
658 | pr_err("removing old device %s from %s\n", | |
659 | d->sys_name+4, array_name); | |
660 | } | |
661 | } | |
662 | ||
663 | static int count_active(struct supertype *st, struct mdinfo *sra, | |
664 | int mdfd, char **availp, | |
665 | struct mdinfo *bestinfo) | |
666 | { | |
667 | /* count how many devices in sra think they are active */ | |
668 | struct mdinfo *d; | |
669 | int cnt = 0; | |
670 | int replcnt = 0; | |
671 | __u64 max_events = 0; | |
672 | __u64 max_journal_events = 0; | |
673 | char *avail = NULL; | |
674 | int *best = NULL; | |
675 | char *devmap = NULL; | |
676 | int numdevs = 0; | |
677 | int devnum; | |
678 | int b, i; | |
679 | int raid_disks = 0; | |
680 | ||
681 | if (!sra) | |
682 | return 0; | |
683 | ||
684 | for (d = sra->devs ; d ; d = d->next) | |
685 | numdevs++; | |
686 | for (d = sra->devs, devnum = 0 ; d ; d = d->next, devnum++) { | |
687 | char dn[30]; | |
688 | int dfd; | |
689 | int ok; | |
690 | struct mdinfo info; | |
691 | ||
692 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); | |
693 | dfd = dev_open(dn, O_RDONLY); | |
694 | if (dfd < 0) | |
695 | continue; | |
696 | ok = st->ss->load_super(st, dfd, NULL); | |
697 | close(dfd); | |
698 | if (ok != 0) | |
699 | continue; | |
700 | ||
701 | info.array.raid_disks = raid_disks; | |
702 | st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum); | |
703 | if (info.disk.raid_disk == MD_DISK_ROLE_JOURNAL && | |
704 | info.events > max_journal_events) | |
705 | max_journal_events = info.events; | |
706 | if (!avail) { | |
707 | raid_disks = info.array.raid_disks; | |
708 | avail = xcalloc(raid_disks, 1); | |
709 | *availp = avail; | |
710 | ||
711 | best = xcalloc(raid_disks, sizeof(int)); | |
712 | devmap = xcalloc(raid_disks, numdevs); | |
713 | ||
714 | st->ss->getinfo_super(st, &info, devmap); | |
715 | } | |
716 | ||
717 | if (info.disk.state & (1<<MD_DISK_SYNC)) | |
718 | { | |
719 | if (cnt == 0) { | |
720 | cnt++; | |
721 | max_events = info.events; | |
722 | avail[info.disk.raid_disk] = 2; | |
723 | best[info.disk.raid_disk] = devnum; | |
724 | st->ss->getinfo_super(st, bestinfo, NULL); | |
725 | } else if (info.events == max_events) { | |
726 | avail[info.disk.raid_disk] = 2; | |
727 | best[info.disk.raid_disk] = devnum; | |
728 | } else if (info.events == max_events-1) { | |
729 | if (avail[info.disk.raid_disk] == 0) { | |
730 | avail[info.disk.raid_disk] = 1; | |
731 | best[info.disk.raid_disk] = devnum; | |
732 | } | |
733 | } else if (info.events < max_events - 1) | |
734 | ; | |
735 | else if (info.events == max_events+1) { | |
736 | int i; | |
737 | max_events = info.events; | |
738 | for (i = 0; i < raid_disks; i++) | |
739 | if (avail[i]) | |
740 | avail[i]--; | |
741 | avail[info.disk.raid_disk] = 2; | |
742 | best[info.disk.raid_disk] = devnum; | |
743 | st->ss->getinfo_super(st, bestinfo, NULL); | |
744 | } else { /* info.events much bigger */ | |
745 | memset(avail, 0, raid_disks); | |
746 | max_events = info.events; | |
747 | avail[info.disk.raid_disk] = 2; | |
748 | best[info.disk.raid_disk] = devnum; | |
749 | st->ss->getinfo_super(st, bestinfo, NULL); | |
750 | } | |
751 | } else if (info.disk.state & (1<<MD_DISK_REPLACEMENT)) | |
752 | replcnt++; | |
753 | st->ss->free_super(st); | |
754 | } | |
755 | if (max_events > 0 && max_journal_events >= max_events - 1) | |
756 | bestinfo->journal_clean = 1; | |
757 | ||
758 | if (!avail) | |
759 | return 0; | |
760 | /* We need to reject any device that thinks the best device is | |
761 | * failed or missing */ | |
762 | for (b = 0; b < raid_disks; b++) | |
763 | if (avail[b] == 2) | |
764 | break; | |
765 | cnt = 0; | |
766 | for (i = 0 ; i < raid_disks ; i++) { | |
767 | if (i != b && avail[i]) | |
768 | if (devmap[raid_disks * best[i] + b] == 0) { | |
769 | /* This device thinks 'b' is failed - | |
770 | * don't use it */ | |
771 | devnum = best[i]; | |
772 | for (d=sra->devs ; devnum; d = d->next) | |
773 | devnum--; | |
774 | d->disk.state |= (1 << MD_DISK_REMOVED); | |
775 | avail[i] = 0; | |
776 | } | |
777 | if (avail[i]) | |
778 | cnt++; | |
779 | } | |
780 | /* Also need to reject any spare device with an event count that | |
781 | * is too high | |
782 | */ | |
783 | for (d = sra->devs; d; d = d->next) { | |
784 | if (!(d->disk.state & (1<<MD_DISK_SYNC)) && | |
785 | d->events > max_events) | |
786 | d->disk.state |= (1 << MD_DISK_REMOVED); | |
787 | } | |
788 | free(best); | |
789 | free(devmap); | |
790 | return cnt + replcnt; | |
791 | } | |
792 | ||
793 | /* test if container has degraded member(s) */ | |
794 | static int | |
795 | container_members_max_degradation(struct map_ent *map, struct map_ent *me) | |
796 | { | |
797 | struct mdinfo *sra; | |
798 | int degraded, max_degraded = 0; | |
799 | ||
800 | for(; map; map = map->next) { | |
801 | if (!metadata_container_matches(map->metadata, me->devnm)) | |
802 | continue; | |
803 | /* most accurate information regarding array degradation */ | |
804 | sra = sysfs_read(-1, map->devnm, | |
805 | GET_DISKS | GET_DEVS | GET_STATE); | |
806 | if (!sra) | |
807 | continue; | |
808 | degraded = sra->array.raid_disks - sra->array.active_disks - | |
809 | sra->array.spare_disks; | |
810 | if (degraded > max_degraded) | |
811 | max_degraded = degraded; | |
812 | sysfs_free(sra); | |
813 | } | |
814 | ||
815 | return max_degraded; | |
816 | } | |
817 | ||
818 | /** | |
819 | * incremental_external_test_spare_criteria() - helper to test spare criteria. | |
820 | * @st: supertype, must be not NULL, it is duplicated here. | |
821 | * @container_devnm: devnm of the container. | |
822 | * @disk_fd: file descriptor of device to tested. | |
823 | * @verbose: verbose flag. | |
824 | * | |
825 | * The function is used on new drive verification path to check if it can be added to external | |
826 | * container. To test spare criteria, metadata must be loaded. It duplicates super to not mess in | |
827 | * original one. | |
828 | * Function is executed if superblock supports get_spare_criteria(), otherwise success is returned. | |
829 | */ | |
830 | mdadm_status_t incremental_external_test_spare_criteria(struct supertype *st, char *container_devnm, | |
831 | int disk_fd, int verbose) | |
832 | { | |
833 | mdadm_status_t rv = MDADM_STATUS_ERROR; | |
834 | char container_devname[PATH_MAX]; | |
835 | struct spare_criteria sc = {0}; | |
836 | struct supertype *dup; | |
837 | ||
838 | if (!st->ss->get_spare_criteria) | |
839 | return MDADM_STATUS_SUCCESS; | |
840 | ||
841 | dup = dup_super(st); | |
842 | snprintf(container_devname, PATH_MAX, "/dev/%s", container_devnm); | |
843 | ||
844 | if (dup->ss->get_spare_criteria(dup, container_devname, &sc) != 0) { | |
845 | if (verbose > 1) | |
846 | pr_err("Failed to get spare criteria for %s\n", container_devname); | |
847 | goto out; | |
848 | } | |
849 | ||
850 | if (!disk_fd_matches_criteria(dup, disk_fd, &sc)) { | |
851 | if (verbose > 1) | |
852 | pr_err("Disk does not match spare criteria for %s\n", container_devname); | |
853 | goto out; | |
854 | } | |
855 | ||
856 | rv = MDADM_STATUS_SUCCESS; | |
857 | ||
858 | out: | |
859 | dev_policy_free(sc.pols); | |
860 | dup->ss->free_super(dup); | |
861 | free(dup); | |
862 | ||
863 | return rv; | |
864 | } | |
865 | ||
866 | static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol, | |
867 | struct map_ent *target, int bare, | |
868 | struct supertype *st, int verbose) | |
869 | { | |
870 | /* This device doesn't have any md metadata | |
871 | * The device policy allows 'spare' and if !bare, it allows spare-same-slot. | |
872 | * If 'st' is not set, then we only know that some metadata allows this, | |
873 | * others possibly don't. | |
874 | * So look for a container or array to attach the device to. | |
875 | * Prefer 'target' if that is set and the array is found. | |
876 | * | |
877 | * If st is set, then only arrays of that type are considered | |
878 | * Return 0 on success, or some exit code on failure, probably 1. | |
879 | */ | |
880 | int rv = 1; | |
881 | dev_t rdev; | |
882 | struct map_ent *mp, *map = NULL; | |
883 | struct mdinfo *chosen = NULL; | |
884 | int dfd = *dfdp; | |
885 | ||
886 | if (!fstat_is_blkdev(dfd, devname, &rdev)) | |
887 | return 1; | |
888 | ||
889 | /* | |
890 | * Now we need to find a suitable array to add this to. | |
891 | * We only accept arrays that: | |
892 | * - match 'st' | |
893 | * - are in the same domains as the device | |
894 | * - are of an size for which the device will be useful | |
895 | * and we choose the one that is the most degraded | |
896 | */ | |
897 | ||
898 | if (map_lock(&map)) { | |
899 | pr_err("failed to get exclusive lock on mapfile\n"); | |
900 | return 1; | |
901 | } | |
902 | for (mp = map ; mp ; mp = mp->next) { | |
903 | struct supertype *st2; | |
904 | struct domainlist *dl = NULL; | |
905 | struct mdinfo *sra; | |
906 | unsigned long long freesize = 0; | |
907 | ||
908 | if (is_subarray(mp->metadata)) | |
909 | continue; | |
910 | if (st) { | |
911 | st2 = st->ss->match_metadata_desc(mp->metadata); | |
912 | if (!st2 || | |
913 | (st->minor_version >= 0 && | |
914 | st->minor_version != st2->minor_version)) { | |
915 | if (verbose > 1) | |
916 | pr_err("not adding %s to %s as metadata type doesn't match\n", | |
917 | devname, mp->path); | |
918 | free(st2); | |
919 | continue; | |
920 | } | |
921 | free(st2); | |
922 | } | |
923 | sra = sysfs_read(-1, mp->devnm, | |
924 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE| | |
925 | GET_COMPONENT|GET_VERSION); | |
926 | if (sra) | |
927 | sra->array.failed_disks = -1; | |
928 | else | |
929 | continue; | |
930 | if (st == NULL) { | |
931 | int i; | |
932 | st2 = NULL; | |
933 | for(i = 0; !st2 && superlist[i]; i++) | |
934 | st2 = superlist[i]->match_metadata_desc( | |
935 | sra->text_version); | |
936 | if (!st2) { | |
937 | if (verbose > 1) | |
938 | pr_err("not adding %s to %s as metadata not recognised.\n", | |
939 | devname, mp->path); | |
940 | goto next; | |
941 | } | |
942 | /* Need to double check the 'act_spare' permissions applies | |
943 | * to this metadata. | |
944 | */ | |
945 | if (!policy_action_allows(pol, st2->ss->name, act_spare)) | |
946 | goto next; | |
947 | if (!bare && !policy_action_allows(pol, st2->ss->name, | |
948 | act_spare_same_slot)) | |
949 | goto next; | |
950 | } else | |
951 | st2 = st; | |
952 | /* update number of failed disks for mostly degraded | |
953 | * container member */ | |
954 | if (sra->array.failed_disks == -1) | |
955 | sra->array.failed_disks = container_members_max_degradation(map, mp); | |
956 | ||
957 | if (sra->component_size == 0) { | |
958 | /* true for containers */ | |
959 | if (incremental_external_test_spare_criteria(st2, mp->devnm, dfd, verbose)) | |
960 | goto next; | |
961 | } | |
962 | ||
963 | if (sra->component_size > 0 && | |
964 | st2->ss->validate_geometry(st2, sra->array.level, sra->array.layout, | |
965 | sra->array.raid_disks, &sra->array.chunk_size, | |
966 | sra->component_size, | |
967 | sra->devs ? sra->devs->data_offset : INVALID_SECTORS, | |
968 | devname, &freesize, sra->consistency_policy, | |
969 | 0) && freesize < sra->component_size) { | |
970 | if (verbose > 1) | |
971 | pr_err("not adding %s to %s as it is too small\n", | |
972 | devname, mp->path); | |
973 | goto next; | |
974 | } | |
975 | /* test against target. | |
976 | * If 'target' is set and 'bare' is false, we only accept | |
977 | * arrays/containers that match 'target'. | |
978 | * If 'target' is set and 'bare' is true, we prefer the | |
979 | * array which matches 'target'. | |
980 | * target is considered only if we deal with degraded array | |
981 | */ | |
982 | if (target && policy_action_allows(pol, st2->ss->name, | |
983 | act_spare_same_slot)) { | |
984 | if (strcmp(target->metadata, mp->metadata) == 0 && | |
985 | memcmp(target->uuid, mp->uuid, | |
986 | sizeof(target->uuid)) == 0 && | |
987 | sra->array.failed_disks > 0) { | |
988 | /* This is our target!! */ | |
989 | sysfs_free(chosen); | |
990 | chosen = sra; | |
991 | sra = NULL; | |
992 | /* skip to end so we don't check any more */ | |
993 | while (mp->next) | |
994 | mp = mp->next; | |
995 | goto next; | |
996 | } | |
997 | /* not our target */ | |
998 | if (!bare) | |
999 | goto next; | |
1000 | } | |
1001 | ||
1002 | dl = domain_from_array(sra, st2->ss->name); | |
1003 | if (domain_test(dl, pol, st2->ss->name) != 1) { | |
1004 | /* domain test fails */ | |
1005 | if (verbose > 1) | |
1006 | pr_err("not adding %s to %s as it is not in a compatible domain\n", | |
1007 | devname, mp->path); | |
1008 | ||
1009 | goto next; | |
1010 | } | |
1011 | /* all tests passed, OK to add to this array */ | |
1012 | if (!chosen) { | |
1013 | chosen = sra; | |
1014 | sra = NULL; | |
1015 | } else if (chosen->array.failed_disks < sra->array.failed_disks) { | |
1016 | sysfs_free(chosen); | |
1017 | chosen = sra; | |
1018 | sra = NULL; | |
1019 | } | |
1020 | next: | |
1021 | sysfs_free(sra); | |
1022 | if (st != st2) | |
1023 | free(st2); | |
1024 | if (dl) | |
1025 | domain_free(dl); | |
1026 | } | |
1027 | if (chosen) { | |
1028 | /* add current device to chosen array as a spare */ | |
1029 | int mdfd = open_dev(chosen->sys_name); | |
1030 | if (mdfd >= 0) { | |
1031 | struct mddev_dev devlist; | |
1032 | char chosen_devname[24]; // 2*11 for int (including signs) + colon + null | |
1033 | devlist.next = NULL; | |
1034 | devlist.used = 0; | |
1035 | devlist.writemostly = FlagDefault; | |
1036 | devlist.failfast = FlagDefault; | |
1037 | devlist.devname = chosen_devname; | |
1038 | sprintf(chosen_devname, "%d:%d", major(rdev), | |
1039 | minor(rdev)); | |
1040 | devlist.disposition = 'a'; | |
1041 | close(dfd); | |
1042 | *dfdp = -1; | |
1043 | rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist, | |
1044 | -1, 0, UOPT_UNDEFINED, 0); | |
1045 | close(mdfd); | |
1046 | } | |
1047 | if (verbose > 0) { | |
1048 | if (rv == 0) | |
1049 | pr_err("added %s as spare for %s\n", | |
1050 | devname, chosen->sys_name); | |
1051 | else | |
1052 | pr_err("failed to add %s as spare for %s\n", | |
1053 | devname, chosen->sys_name); | |
1054 | } | |
1055 | sysfs_free(chosen); | |
1056 | } | |
1057 | map_unlock(&map); | |
1058 | return rv; | |
1059 | } | |
1060 | ||
1061 | static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol, | |
1062 | struct supertype *st, int verbose) | |
1063 | { | |
1064 | /* we know that at least one partition virtual-metadata is | |
1065 | * allowed to incorporate spares like this device. We need to | |
1066 | * find a suitable device to copy partition information from. | |
1067 | * | |
1068 | * Getting a list of all disk (not partition) devices is | |
1069 | * slightly non-trivial. We could look at /sys/block, but | |
1070 | * that is theoretically due to be removed. Maybe best to use | |
1071 | * /dev/disk/by-path/?* and ignore names ending '-partNN' as | |
1072 | * we depend on this directory of 'path' info. But that fails | |
1073 | * to find loop devices and probably others. Maybe don't | |
1074 | * worry about that, they aren't the real target. | |
1075 | * | |
1076 | * So: check things in /dev/disk/by-path to see if they are in | |
1077 | * a compatible domain, then load the partition table and see | |
1078 | * if it is OK for the new device, and choose the largest | |
1079 | * partition table that fits. | |
1080 | */ | |
1081 | DIR *dir; | |
1082 | struct dirent *de; | |
1083 | char *chosen = NULL; | |
1084 | unsigned long long chosen_size = 0; | |
1085 | struct supertype *chosen_st = NULL; | |
1086 | int fd; | |
1087 | ||
1088 | dir = opendir("/dev/disk/by-path"); | |
1089 | if (!dir) | |
1090 | return 1; | |
1091 | while ((de = readdir(dir)) != NULL) { | |
1092 | char *ep; | |
1093 | struct dev_policy *pol2 = NULL; | |
1094 | struct domainlist *domlist = NULL; | |
1095 | int fd = -1; | |
1096 | struct mdinfo info; | |
1097 | struct supertype *st2 = NULL; | |
1098 | char *dev_path_name = NULL; | |
1099 | unsigned long long devsectors; | |
1100 | char *pathlist[2]; | |
1101 | ||
1102 | if (de->d_ino == 0 || de->d_name[0] == '.' || | |
1103 | (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN)) | |
1104 | goto next; | |
1105 | ||
1106 | ep = de->d_name + strlen(de->d_name); | |
1107 | while (ep > de->d_name && | |
1108 | isdigit(ep[-1])) | |
1109 | ep--; | |
1110 | if (ep > de->d_name + 5 && | |
1111 | strncmp(ep-5, "-part", 5) == 0) | |
1112 | /* This is a partition - skip it */ | |
1113 | goto next; | |
1114 | ||
1115 | pathlist[0] = de->d_name; | |
1116 | pathlist[1] = NULL; | |
1117 | pol2 = path_policy(pathlist, type_disk); | |
1118 | ||
1119 | domain_merge(&domlist, pol2, st ? st->ss->name : NULL); | |
1120 | if (domain_test(domlist, pol, st ? st->ss->name : NULL) != 1) | |
1121 | /* new device is incompatible with this device. */ | |
1122 | goto next; | |
1123 | ||
1124 | domain_free(domlist); | |
1125 | domlist = NULL; | |
1126 | ||
1127 | if (asprintf(&dev_path_name, "/dev/disk/by-path/%s", de->d_name) != 1) { | |
1128 | dev_path_name = NULL; | |
1129 | goto next; | |
1130 | } | |
1131 | fd = open(dev_path_name, O_RDONLY); | |
1132 | if (fd < 0) | |
1133 | goto next; | |
1134 | if (get_dev_size(fd, dev_path_name, &devsectors) == 0) | |
1135 | goto next; | |
1136 | devsectors >>= 9; | |
1137 | ||
1138 | if (st) | |
1139 | st2 = dup_super(st); | |
1140 | else | |
1141 | st2 = guess_super_type(fd, guess_partitions); | |
1142 | if (st2 == NULL || st2->ss->load_super(st2, fd, NULL) < 0) | |
1143 | goto next; | |
1144 | st2->ignore_hw_compat = 0; | |
1145 | ||
1146 | if (!st) { | |
1147 | /* Check domain policy again, this time referring to metadata */ | |
1148 | domain_merge(&domlist, pol2, st2->ss->name); | |
1149 | if (domain_test(domlist, pol, st2->ss->name) != 1) | |
1150 | /* Incompatible devices for this metadata type */ | |
1151 | goto next; | |
1152 | if (!policy_action_allows(pol, st2->ss->name, act_spare)) | |
1153 | /* Some partition types allow sparing, but not | |
1154 | * this one. | |
1155 | */ | |
1156 | goto next; | |
1157 | } | |
1158 | ||
1159 | st2->ss->getinfo_super(st2, &info, NULL); | |
1160 | if (info.component_size > devsectors) | |
1161 | /* This partitioning doesn't fit in the device */ | |
1162 | goto next; | |
1163 | ||
1164 | /* This is an acceptable device to copy partition | |
1165 | * metadata from. We could just stop here, but I | |
1166 | * think I want to keep looking incase a larger | |
1167 | * metadata which makes better use of the device can | |
1168 | * be found. | |
1169 | */ | |
1170 | if (chosen == NULL || chosen_size < info.component_size) { | |
1171 | chosen_size = info.component_size; | |
1172 | free(chosen); | |
1173 | chosen = dev_path_name; | |
1174 | dev_path_name = NULL; | |
1175 | if (chosen_st) { | |
1176 | chosen_st->ss->free_super(chosen_st); | |
1177 | free(chosen_st); | |
1178 | } | |
1179 | chosen_st = st2; | |
1180 | st2 = NULL; | |
1181 | } | |
1182 | ||
1183 | next: | |
1184 | free(dev_path_name); | |
1185 | domain_free(domlist); | |
1186 | dev_policy_free(pol2); | |
1187 | if (st2) | |
1188 | st2->ss->free_super(st2); | |
1189 | free(st2); | |
1190 | ||
1191 | if (fd >= 0) | |
1192 | close(fd); | |
1193 | } | |
1194 | ||
1195 | closedir(dir); | |
1196 | ||
1197 | if (!chosen) | |
1198 | return 1; | |
1199 | ||
1200 | /* 'chosen' is the best device we can find. Let's write its | |
1201 | * metadata to devname dfd is read-only so don't use that | |
1202 | */ | |
1203 | fd = open(devname, O_RDWR); | |
1204 | if (fd >= 0) { | |
1205 | chosen_st->ss->store_super(chosen_st, fd); | |
1206 | close(fd); | |
1207 | } | |
1208 | free(chosen); | |
1209 | chosen_st->ss->free_super(chosen_st); | |
1210 | free(chosen_st); | |
1211 | return 0; | |
1212 | } | |
1213 | ||
1214 | static int is_bare(int dfd) | |
1215 | { | |
1216 | unsigned long long size = 0; | |
1217 | char bufpad[4096 + 4096]; | |
1218 | char *buf = (char*)(((long)bufpad + 4096) & ~4095); | |
1219 | ||
1220 | if (lseek(dfd, 0, SEEK_SET) != 0 || | |
1221 | read(dfd, buf, 4096) != 4096) | |
1222 | return 0; | |
1223 | ||
1224 | if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff') | |
1225 | return 0; | |
1226 | if (memcmp(buf, buf+1, 4095) != 0) | |
1227 | return 0; | |
1228 | ||
1229 | /* OK, first 4K appear blank, try the end. */ | |
1230 | get_dev_size(dfd, NULL, &size); | |
1231 | if ((size >= 4096 && lseek(dfd, size-4096, SEEK_SET) < 0) || | |
1232 | read(dfd, buf, 4096) != 4096) | |
1233 | return 0; | |
1234 | ||
1235 | if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff') | |
1236 | return 0; | |
1237 | if (memcmp(buf, buf+1, 4095) != 0) | |
1238 | return 0; | |
1239 | ||
1240 | return 1; | |
1241 | } | |
1242 | ||
1243 | /* adding a spare to a regular array is quite different from adding one to | |
1244 | * a set-of-partitions virtual array. | |
1245 | * This function determines which is worth trying and tries as appropriate. | |
1246 | * Arrays are given priority over partitions. | |
1247 | */ | |
1248 | static int try_spare(char *devname, int *dfdp, struct dev_policy *pol, | |
1249 | struct map_ent *target, | |
1250 | struct supertype *st, int verbose) | |
1251 | { | |
1252 | int i; | |
1253 | int rv; | |
1254 | int arrays_ok = 0; | |
1255 | int partitions_ok = 0; | |
1256 | int dfd = *dfdp; | |
1257 | int bare; | |
1258 | ||
1259 | /* Can only add a spare if device has at least one domain */ | |
1260 | if (pol_find(pol, pol_domain) == NULL) | |
1261 | return 1; | |
1262 | /* And only if some action allows spares */ | |
1263 | if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare)) | |
1264 | return 1; | |
1265 | ||
1266 | /* Now check if the device is bare. | |
1267 | * bare devices can always be added as a spare | |
1268 | * non-bare devices can only be added if spare-same-slot is permitted, | |
1269 | * and this device is replacing a previous device - in which case 'target' | |
1270 | * will be set. | |
1271 | */ | |
1272 | if (!is_bare(dfd)) { | |
1273 | /* Must have a target and allow same_slot */ | |
1274 | /* Later - may allow force_spare without target */ | |
1275 | if (!target || | |
1276 | !policy_action_allows(pol, st?st->ss->name:NULL, | |
1277 | act_spare_same_slot)) { | |
1278 | if (verbose > 1) | |
1279 | pr_err("%s is not bare, so not considering as a spare\n", | |
1280 | devname); | |
1281 | return 1; | |
1282 | } | |
1283 | bare = 0; | |
1284 | } else | |
1285 | bare = 1; | |
1286 | ||
1287 | /* It might be OK to add this device to an array - need to see | |
1288 | * what arrays might be candidates. | |
1289 | */ | |
1290 | if (st) { | |
1291 | /* just try to add 'array' or 'partition' based on this metadata */ | |
1292 | if (st->ss->add_to_super) | |
1293 | return array_try_spare(devname, dfdp, pol, target, bare, | |
1294 | st, verbose); | |
1295 | else | |
1296 | return partition_try_spare(devname, dfdp, pol, | |
1297 | st, verbose); | |
1298 | } | |
1299 | /* No metadata was specified or found so options are open. | |
1300 | * Check for whether any array metadata, or any partition metadata | |
1301 | * might allow adding the spare. This check is just help to avoid | |
1302 | * a more costly scan of all arrays when we can be sure that will | |
1303 | * fail. | |
1304 | */ | |
1305 | for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) { | |
1306 | if (superlist[i]->add_to_super && !arrays_ok && | |
1307 | policy_action_allows(pol, superlist[i]->name, act_spare)) | |
1308 | arrays_ok = 1; | |
1309 | if (superlist[i]->add_to_super == NULL && !partitions_ok && | |
1310 | policy_action_allows(pol, superlist[i]->name, act_spare)) | |
1311 | partitions_ok = 1; | |
1312 | } | |
1313 | rv = 1; | |
1314 | if (arrays_ok) | |
1315 | rv = array_try_spare(devname, dfdp, pol, target, bare, | |
1316 | st, verbose); | |
1317 | if (rv != 0 && partitions_ok) | |
1318 | rv = partition_try_spare(devname, dfdp, pol, st, verbose); | |
1319 | return rv; | |
1320 | } | |
1321 | ||
1322 | int IncrementalScan(struct context *c, char *devnm) | |
1323 | { | |
1324 | /* look at every device listed in the 'map' file. | |
1325 | * If one is found that is not running then: | |
1326 | * look in mdadm.conf for bitmap file. | |
1327 | * if one exists, but array has none, add it. | |
1328 | * try to start array in auto-readonly mode | |
1329 | */ | |
1330 | struct map_ent *mapl = NULL; | |
1331 | struct map_ent *me; | |
1332 | struct mddev_ident *devs, *mddev; | |
1333 | int rv = 0; | |
1334 | char container[32]; | |
1335 | char *only = NULL; | |
1336 | ||
1337 | map_read(&mapl); | |
1338 | devs = conf_get_ident(NULL); | |
1339 | ||
1340 | restart: | |
1341 | for (me = mapl ; me ; me = me->next) { | |
1342 | struct mdinfo *sra; | |
1343 | int mdfd; | |
1344 | ||
1345 | if (devnm && strcmp(devnm, me->devnm) != 0) | |
1346 | continue; | |
1347 | if (me->metadata[0] == '/') { | |
1348 | char *sl; | |
1349 | ||
1350 | if (!devnm) | |
1351 | continue; | |
1352 | ||
1353 | /* member array, need to work on container */ | |
1354 | strncpy(container, me->metadata+1, 32); | |
1355 | container[31] = 0; | |
1356 | sl = strchr(container, '/'); | |
1357 | if (sl) | |
1358 | *sl = 0; | |
1359 | only = devnm; | |
1360 | devnm = container; | |
1361 | goto restart; | |
1362 | } | |
1363 | mdfd = open_dev(me->devnm); | |
1364 | ||
1365 | if (!is_fd_valid(mdfd)) | |
1366 | continue; | |
1367 | if (!isdigit(me->metadata[0])) { | |
1368 | /* must be a container */ | |
1369 | struct supertype *st = super_by_fd(mdfd, NULL); | |
1370 | int ret = 0; | |
1371 | struct map_ent *map = NULL; | |
1372 | ||
1373 | if (st && st->ss->load_container) | |
1374 | ret = st->ss->load_container(st, mdfd, NULL); | |
1375 | close_fd(&mdfd); | |
1376 | if (!ret && st && st->ss->container_content) { | |
1377 | if (map_lock(&map)) | |
1378 | pr_err("failed to get exclusive lock on mapfile\n"); | |
1379 | ret = Incremental_container(st, me->path, c, only); | |
1380 | map_unlock(&map); | |
1381 | } | |
1382 | if (ret) | |
1383 | rv = 1; | |
1384 | continue; | |
1385 | } | |
1386 | if (md_array_active(mdfd)) { | |
1387 | close_fd(&mdfd); | |
1388 | continue; | |
1389 | } | |
1390 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
1391 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
1392 | if (mddev->devname && me->path && | |
1393 | devname_matches(mddev->devname, me->path)) | |
1394 | break; | |
1395 | ||
1396 | /* FIXME check for reshape_active and consider not | |
1397 | * starting array. | |
1398 | */ | |
1399 | sra = sysfs_read(mdfd, NULL, 0); | |
1400 | if (sra) { | |
1401 | if (sysfs_set_str(sra, NULL, | |
1402 | "array_state", "read-auto") == 0) { | |
1403 | if (c->verbose >= 0) | |
1404 | pr_err("started array %s\n", | |
1405 | me->path ?: me->devnm); | |
1406 | } else { | |
1407 | pr_err("failed to start array %s: %s\n", | |
1408 | me->path ?: me->devnm, | |
1409 | strerror(errno)); | |
1410 | rv = 1; | |
1411 | } | |
1412 | sysfs_free(sra); | |
1413 | } | |
1414 | close_fd(&mdfd); | |
1415 | } | |
1416 | map_free(mapl); | |
1417 | return rv; | |
1418 | } | |
1419 | ||
1420 | static char *container2devname(char *devname) | |
1421 | { | |
1422 | char *mdname = NULL; | |
1423 | ||
1424 | if (devname[0] == '/') { | |
1425 | int fd = open(devname, O_RDONLY); | |
1426 | if (fd >= 0) { | |
1427 | mdname = xstrdup(fd2devnm(fd)); | |
1428 | close(fd); | |
1429 | } | |
1430 | } else { | |
1431 | int uuid[4]; | |
1432 | struct map_ent *mp, *map = NULL; | |
1433 | ||
1434 | if (!parse_uuid(devname, uuid)) | |
1435 | return mdname; | |
1436 | mp = map_by_uuid(&map, uuid); | |
1437 | if (mp) | |
1438 | mdname = xstrdup(mp->devnm); | |
1439 | map_free(map); | |
1440 | } | |
1441 | ||
1442 | return mdname; | |
1443 | } | |
1444 | ||
1445 | static int Incremental_container(struct supertype *st, char *devname, | |
1446 | struct context *c, char *only) | |
1447 | { | |
1448 | /* Collect the contents of this container and for each | |
1449 | * array, choose a device name and assemble the array. | |
1450 | */ | |
1451 | ||
1452 | struct mdinfo *list; | |
1453 | struct mdinfo *ra; | |
1454 | struct map_ent *map = NULL; | |
1455 | struct mdinfo info; | |
1456 | int trustworthy; | |
1457 | struct mddev_ident *match; | |
1458 | int rv = 0; | |
1459 | int result = 0; | |
1460 | ||
1461 | st->ss->getinfo_super(st, &info, NULL); | |
1462 | ||
1463 | if (info.container_enough < 0 || (info.container_enough == 0 && c->runstop < 1)) { | |
1464 | if (c->export) | |
1465 | printf("MD_STARTED=no\n"); | |
1466 | else if (c->verbose) | |
1467 | pr_err("Not enough devices to start the container.\n"); | |
1468 | ||
1469 | return 0; | |
1470 | } | |
1471 | ||
1472 | match = conf_match(st, &info, devname, c->verbose, &rv); | |
1473 | if (match == NULL && rv == 2) | |
1474 | return rv; | |
1475 | ||
1476 | /* Need to compute 'trustworthy' */ | |
1477 | if (match) | |
1478 | trustworthy = LOCAL; | |
1479 | else if (st->ss->match_home(st, c->homehost) == 1) | |
1480 | trustworthy = LOCAL; | |
1481 | else if (st->ss->match_home(st, "any") == 1) | |
1482 | trustworthy = LOCAL; | |
1483 | else | |
1484 | trustworthy = FOREIGN; | |
1485 | ||
1486 | list = st->ss->container_content(st, NULL); | |
1487 | /* when nothing to activate - quit */ | |
1488 | if (list == NULL) { | |
1489 | if (c->export) { | |
1490 | printf("MD_STARTED=nothing\n"); | |
1491 | } | |
1492 | return 0; | |
1493 | } | |
1494 | for (ra = list ; ra ; ra = ra->next) { | |
1495 | int mdfd = -1; | |
1496 | char chosen_name[1024]; | |
1497 | struct map_ent *mp; | |
1498 | struct mddev_ident *match = NULL; | |
1499 | ||
1500 | /* do not activate arrays blocked by metadata handler */ | |
1501 | if (ra->array.state & (1 << MD_SB_BLOCK_VOLUME)) { | |
1502 | pr_err("Cannot activate array %s in %s.\n", | |
1503 | ra->text_version, devname); | |
1504 | continue; | |
1505 | } | |
1506 | mp = map_by_uuid(&map, ra->uuid); | |
1507 | ||
1508 | if (mp) { | |
1509 | mdfd = open_dev(mp->devnm); | |
1510 | if (!is_fd_valid(mdfd)) { | |
1511 | pr_err("failed to open %s: %s.\n", | |
1512 | mp->devnm, strerror(errno)); | |
1513 | rv = 2; | |
1514 | goto release; | |
1515 | } | |
1516 | if (mp->path) | |
1517 | strcpy(chosen_name, mp->path); | |
1518 | else | |
1519 | strcpy(chosen_name, mp->devnm); | |
1520 | } else if (!only) { | |
1521 | ||
1522 | /* Check in mdadm.conf for container == devname and | |
1523 | * member == ra->text_version after second slash. | |
1524 | */ | |
1525 | char *sub = strchr(ra->text_version+1, '/'); | |
1526 | struct mddev_ident *array_list; | |
1527 | if (sub) { | |
1528 | sub++; | |
1529 | array_list = conf_get_ident(NULL); | |
1530 | } else | |
1531 | array_list = NULL; | |
1532 | for(; array_list ; array_list = array_list->next) { | |
1533 | char *dn; | |
1534 | if (array_list->member == NULL || | |
1535 | array_list->container == NULL) | |
1536 | continue; | |
1537 | if (strcmp(array_list->member, sub) != 0) | |
1538 | continue; | |
1539 | if (array_list->uuid_set && | |
1540 | !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid)) | |
1541 | continue; | |
1542 | dn = container2devname(array_list->container); | |
1543 | if (dn == NULL) | |
1544 | continue; | |
1545 | if (strncmp(dn, ra->text_version+1, | |
1546 | strlen(dn)) != 0 || | |
1547 | ra->text_version[strlen(dn)+1] != '/') { | |
1548 | free(dn); | |
1549 | continue; | |
1550 | } | |
1551 | free(dn); | |
1552 | /* we have a match */ | |
1553 | match = array_list; | |
1554 | if (c->verbose>0) | |
1555 | pr_err("match found for member %s\n", | |
1556 | array_list->member); | |
1557 | break; | |
1558 | } | |
1559 | ||
1560 | if (match && match->devname && is_devname_ignore(match->devname) == true) { | |
1561 | if (c->verbose > 0) | |
1562 | pr_err("array %s/%s is explicitly ignored by mdadm.conf\n", | |
1563 | match->container, match->member); | |
1564 | continue; | |
1565 | } | |
1566 | if (match) | |
1567 | trustworthy = LOCAL; | |
1568 | ||
1569 | mdfd = create_mddev(match ? match->devname : NULL, ra->name, trustworthy, | |
1570 | chosen_name, 1); | |
1571 | ||
1572 | if (!is_fd_valid(mdfd)) { | |
1573 | pr_err("create_mddev failed with chosen name %s: %s.\n", | |
1574 | chosen_name, strerror(errno)); | |
1575 | rv = 2; | |
1576 | goto release; | |
1577 | } | |
1578 | } | |
1579 | ||
1580 | if (only && (!mp || strcmp(mp->devnm, only) != 0)) { | |
1581 | close_fd(&mdfd); | |
1582 | continue; | |
1583 | } | |
1584 | ||
1585 | assemble_container_content(st, mdfd, ra, c, | |
1586 | chosen_name, &result); | |
1587 | map_free(map); | |
1588 | map = NULL; | |
1589 | close_fd(&mdfd); | |
1590 | udev_unblock(); | |
1591 | sysfs_uevent(&info, "change"); | |
1592 | } | |
1593 | if (c->export && result) { | |
1594 | char sep = '='; | |
1595 | printf("MD_STARTED"); | |
1596 | if (result & INCR_NO) { | |
1597 | printf("%cno", sep); | |
1598 | sep = ','; | |
1599 | } | |
1600 | if (result & INCR_UNSAFE) { | |
1601 | printf("%cunsafe", sep); | |
1602 | sep = ','; | |
1603 | } | |
1604 | if (result & INCR_ALREADY) { | |
1605 | printf("%calready", sep); | |
1606 | sep = ','; | |
1607 | } | |
1608 | if (result & INCR_YES) { | |
1609 | printf("%cyes", sep); | |
1610 | sep = ','; | |
1611 | } | |
1612 | printf("\n"); | |
1613 | } | |
1614 | ||
1615 | release: | |
1616 | map_free(map); | |
1617 | sysfs_free(list); | |
1618 | udev_unblock(); | |
1619 | sysfs_uevent(&info, "change"); | |
1620 | return rv; | |
1621 | } | |
1622 | ||
1623 | /** | |
1624 | * is_devnode_path() - check if the devname passed might be devnode path. | |
1625 | * @devnode: the path to check. | |
1626 | * | |
1627 | * Devnode must be located directly in /dev directory. It is not checking existence of the file | |
1628 | * because the device might no longer exist during removal from raid array. | |
1629 | */ | |
1630 | static bool is_devnode_path(char *devnode) | |
1631 | { | |
1632 | char *devnm = strrchr(devnode, '/'); | |
1633 | ||
1634 | if (!devnm || *(devnm + 1) == 0) | |
1635 | return false; | |
1636 | ||
1637 | if (strncmp(devnode, DEV_DIR, DEV_DIR_LEN) == 0 && devnode + DEV_DIR_LEN - 1 == devnm) | |
1638 | return true; | |
1639 | ||
1640 | return false; | |
1641 | } | |
1642 | ||
1643 | /** | |
1644 | * Incremental_remove_external() - Remove the device from external container. | |
1645 | * @device_devnm: block device to remove. | |
1646 | * @container_devnm: the parent container | |
1647 | * @mdstat: mdstat file content. | |
1648 | * @verbose: verbose flag. | |
1649 | * | |
1650 | * Fail member device in each subarray and remove member device from external container. | |
1651 | * The resposibility of removing member disks from external subararys belongs to mdmon. | |
1652 | */ | |
1653 | static mdadm_status_t Incremental_remove_external(char *device_devnm, char *container_devnm, | |
1654 | struct mdstat_ent *mdstat, int verbose) | |
1655 | { | |
1656 | mdadm_status_t rv = MDADM_STATUS_SUCCESS; | |
1657 | struct mdstat_ent *memb; | |
1658 | ||
1659 | for (memb = mdstat ; memb ; memb = memb->next) { | |
1660 | mdadm_status_t ret = MDADM_STATUS_SUCCESS; | |
1661 | int state_fd; | |
1662 | ||
1663 | if (!is_container_member(memb, container_devnm)) | |
1664 | continue; | |
1665 | ||
1666 | /* | |
1667 | * Checking mdstat is pointles because it might be outdated, try open descriptor | |
1668 | * instead. If it fails, we are fine with that, device is already gone. | |
1669 | */ | |
1670 | state_fd = sysfs_open_memb_attr(memb->devnm, device_devnm, "state", O_RDWR); | |
1671 | if (!is_fd_valid(state_fd)) | |
1672 | continue; | |
1673 | ||
1674 | ret = sysfs_set_memb_state_fd(state_fd, MEMB_STATE_FAULTY, NULL); | |
1675 | if (ret && verbose >= 0) | |
1676 | pr_err("Cannot fail member device %s in external subarray %s.\n", | |
1677 | device_devnm, memb->devnm); | |
1678 | ||
1679 | close_fd(&state_fd); | |
1680 | ||
1681 | /* | |
1682 | * Don't remove member device from container if it failed to remove it | |
1683 | * from any member array. | |
1684 | */ | |
1685 | rv |= ret; | |
1686 | } | |
1687 | ||
1688 | if (rv == MDADM_STATUS_SUCCESS) | |
1689 | rv = sysfs_set_memb_state(container_devnm, device_devnm, MEMB_STATE_REMOVE); | |
1690 | ||
1691 | if (rv && verbose >= 0) | |
1692 | pr_err("Cannot remove member device %s from container %s.\n", device_devnm, | |
1693 | container_devnm); | |
1694 | ||
1695 | return rv; | |
1696 | } | |
1697 | ||
1698 | /** | |
1699 | * Incremental_remove() - Remove the device from all raid arrays. | |
1700 | * @devname: the device we want to remove, it could be kernel device name or devnode. | |
1701 | * @id_path: optional, /dev/disk/by-path path to save for bare scenarios support. | |
1702 | * @verbose: verbose flag. | |
1703 | * | |
1704 | * First, fail the device (if needed) and then remove the device. This code is critical for system | |
1705 | * funtionality and that is why it is keept as simple as possible. We do not load devices using | |
1706 | * sysfs_read() because any unerelated failure may lead us to abort. We also do not call | |
1707 | * Manage_Subdevs(). | |
1708 | */ | |
1709 | int Incremental_remove(char *devname, char *id_path, int verbose) | |
1710 | { | |
1711 | mdadm_status_t rv = MDADM_STATUS_SUCCESS; | |
1712 | char *devnm = basename(devname); | |
1713 | char buf[SYSFS_MAX_BUF_SIZE]; | |
1714 | struct mdstat_ent *mdstat; | |
1715 | struct mdstat_ent *ent; | |
1716 | struct mdinfo mdi; | |
1717 | int mdfd = -1; | |
1718 | ||
1719 | if (strcmp(devnm, devname) != 0) | |
1720 | if (!is_devnode_path(devname)) { | |
1721 | pr_err("Cannot remove \"%s\", devnode path or kernel device name is allowed.\n", | |
1722 | devname); | |
1723 | return 1; | |
1724 | } | |
1725 | ||
1726 | mdstat = mdstat_read(0, 0); | |
1727 | if (!mdstat) { | |
1728 | pr_err("Cannot read /proc/mdstat file, aborting\n"); | |
1729 | return 1; | |
1730 | } | |
1731 | ||
1732 | ent = mdstat_find_by_member_name(mdstat, devnm); | |
1733 | if (!ent) { | |
1734 | if (verbose >= 0) | |
1735 | pr_vrb("%s does not appear to be a component of any array\n", devnm); | |
1736 | goto out; | |
1737 | } | |
1738 | ||
1739 | if (sysfs_init(&mdi, -1, ent->devnm)) { | |
1740 | pr_err("unable to initialize sysfs for: %s\n", devnm); | |
1741 | goto out; | |
1742 | } | |
1743 | ||
1744 | mdfd = open_dev_excl(ent->devnm); | |
1745 | if (is_fd_valid(mdfd)) { | |
1746 | char *array_state_file = "array_state"; | |
1747 | ||
1748 | /** | |
1749 | * This is a workaround for the old issue. | |
1750 | * Incremental_remove() triggered from udev rule when disk is removed from OS | |
1751 | * tries to set array in auto-read-only mode. This can interrupt rebuild | |
1752 | * process which is started automatically, e.g. if array is mounted and | |
1753 | * spare disk is available (I/O errors limit might be achieved faster than disk is | |
1754 | * removed by mdadm). Prevent Incremental_remove() from setting array | |
1755 | * into "auto-read-only", by requiring exclusive open to succeed. | |
1756 | */ | |
1757 | close_fd(&mdfd); | |
1758 | ||
1759 | if (sysfs_get_str(&mdi, NULL, array_state_file, buf, sizeof(buf)) > 0) { | |
1760 | char *str_read_auto = map_num_s(sysfs_array_states, ARRAY_READ_AUTO); | |
1761 | char *str_active = map_num_s(sysfs_array_states, ARRAY_ACTIVE); | |
1762 | char *str_clean = map_num_s(sysfs_array_states, ARRAY_CLEAN); | |
1763 | ||
1764 | if (strncmp(buf, str_active, strlen(str_active)) == 0 || | |
1765 | strncmp(buf, str_clean, strlen(str_clean)) == 0) | |
1766 | sysfs_set_str(&mdi, NULL, array_state_file, str_read_auto); | |
1767 | } | |
1768 | } | |
1769 | ||
1770 | mdfd = open_dev(ent->devnm); | |
1771 | if (mdfd < 0) { | |
1772 | if (verbose >= 0) | |
1773 | pr_err("Cannot open array %s!!\n", ent->devnm); | |
1774 | goto out; | |
1775 | } | |
1776 | ||
1777 | if (id_path) { | |
1778 | struct map_ent *map = NULL, *me; | |
1779 | ||
1780 | me = map_by_devnm(&map, ent->devnm); | |
1781 | if (me) | |
1782 | policy_save_path(id_path, me); | |
1783 | map_free(map); | |
1784 | } | |
1785 | ||
1786 | if (is_mdstat_ent_external(ent)) { | |
1787 | rv = Incremental_remove_external(devnm, ent->devnm, mdstat, verbose); | |
1788 | goto out; | |
1789 | } | |
1790 | ||
1791 | /* Native arrays are handled separatelly to provide more detailed error handling */ | |
1792 | rv = sysfs_set_memb_state(ent->devnm, devnm, MEMB_STATE_FAULTY); | |
1793 | if (rv && verbose >= 0) | |
1794 | pr_err("Cannot fail member device %s in array %s.\n", devnm, ent->devnm); | |
1795 | ||
1796 | if (rv == MDADM_STATUS_SUCCESS) | |
1797 | rv = sysfs_set_memb_state(ent->devnm, devnm, MEMB_STATE_REMOVE); | |
1798 | ||
1799 | if (rv && verbose >= 0) | |
1800 | pr_err("Cannot remove member device %s from %s.\n", devnm, ent->devnm); | |
1801 | ||
1802 | out: | |
1803 | close_fd(&mdfd); | |
1804 | free_mdstat(mdstat); | |
1805 | return rv; | |
1806 | } |