]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
Manage: Block unsafe member failing
[thirdparty/mdadm.git] / Manage.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include "md_u.h"
27 #include "md_p.h"
28 #include <ctype.h>
29
30 int Manage_ro(char *devname, int fd, int readonly)
31 {
32 /* switch to readonly or rw
33 *
34 * requires >= 0.90.0
35 * first check that array is runing
36 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
37 *
38 */
39 struct mdinfo *mdi;
40 int rv = 0;
41
42 /* If this is an externally-managed array, we need to modify the
43 * metadata_version so that mdmon doesn't undo our change.
44 */
45 mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION);
46 if (mdi &&
47 mdi->array.major_version == -1 &&
48 is_subarray(mdi->text_version)) {
49 char vers[64];
50 strcpy(vers, "external:");
51 strcat(vers, mdi->text_version);
52 if (readonly > 0) {
53 int rv;
54 /* We set readonly ourselves. */
55 vers[9] = '-';
56 sysfs_set_str(mdi, NULL, "metadata_version", vers);
57
58 close(fd);
59 rv = sysfs_set_str(mdi, NULL, "array_state", "readonly");
60
61 if (rv < 0) {
62 pr_err("failed to set readonly for %s: %s\n",
63 devname, strerror(errno));
64
65 vers[9] = mdi->text_version[0];
66 sysfs_set_str(mdi, NULL, "metadata_version", vers);
67 rv = 1;
68 goto out;
69 }
70 } else {
71 char *cp;
72 /* We cannot set read/write - must signal mdmon */
73 vers[9] = '/';
74 sysfs_set_str(mdi, NULL, "metadata_version", vers);
75
76 cp = strchr(vers+10, '/');
77 if (cp)
78 *cp = 0;
79 ping_monitor(vers+10);
80 if (mdi->array.level <= 0)
81 sysfs_set_str(mdi, NULL, "array_state", "active");
82 }
83 goto out;
84 }
85
86 if (!md_array_active(fd)) {
87 pr_err("%s does not appear to be active.\n", devname);
88 rv = 1;
89 goto out;
90 }
91
92 if (readonly > 0) {
93 if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
94 pr_err("failed to set readonly for %s: %s\n",
95 devname, strerror(errno));
96 rv = 1;
97 goto out;
98 }
99 } else if (readonly < 0) {
100 if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
101 pr_err("failed to set writable for %s: %s\n",
102 devname, strerror(errno));
103 rv = 1;
104 goto out;
105 }
106 }
107 out:
108 sysfs_free(mdi);
109 return rv;
110 }
111
112 static void remove_devices(char *devnm, char *path)
113 {
114 /*
115 * Remove names at 'path' - possibly with
116 * partition suffixes - which link to the 'standard'
117 * name for devnm. These were probably created
118 * by mdadm when the array was assembled.
119 */
120 char base[40];
121 char *path2;
122 char link[1024];
123 int n;
124 int part;
125 char *be;
126 char *pe;
127
128 if (!path)
129 return;
130
131 sprintf(base, "/dev/%s", devnm);
132 be = base + strlen(base);
133
134 path2 = xmalloc(strlen(path)+20);
135 strcpy(path2, path);
136 pe = path2 + strlen(path2);
137
138 for (part = 0; part < 16; part++) {
139 if (part) {
140 sprintf(be, "p%d", part);
141
142 if (isdigit(pe[-1]))
143 sprintf(pe, "p%d", part);
144 else
145 sprintf(pe, "%d", part);
146 }
147 n = readlink(path2, link, sizeof(link));
148 if (n > 0 && (int)strlen(base) == n &&
149 strncmp(link, base, n) == 0)
150 unlink(path2);
151 }
152 free(path2);
153 }
154
155 int Manage_run(char *devname, int fd, struct context *c)
156 {
157 /* Run the array. Array must already be configured
158 * Requires >= 0.90.0
159 */
160 char nm[32], *nmp;
161
162 nmp = fd2devnm(fd);
163 if (!nmp) {
164 pr_err("Cannot find %s in sysfs!!\n", devname);
165 return 1;
166 }
167 strcpy(nm, nmp);
168 return IncrementalScan(c, nm);
169 }
170
171 int Manage_stop(char *devname, int fd, int verbose, int will_retry)
172 {
173 /* Stop the array. Array must already be configured
174 * 'will_retry' means that error messages are not wanted.
175 */
176 int rv = 0;
177 struct map_ent *map = NULL;
178 struct mdinfo *mdi;
179 char devnm[32];
180 char container[32];
181 int err;
182 int count;
183 char buf[32];
184 unsigned long long rd1, rd2;
185
186 if (will_retry && verbose == 0)
187 verbose = -1;
188
189 strcpy(devnm, fd2devnm(fd));
190 /* Get EXCL access first. If this fails, then attempting
191 * to stop is probably a bad idea.
192 */
193 mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_COMPONENT|GET_VERSION);
194 if (mdi && is_subarray(mdi->text_version)) {
195 char *sl;
196 strncpy(container, mdi->text_version+1, sizeof(container));
197 container[sizeof(container)-1] = 0;
198 sl = strchr(container, '/');
199 if (sl)
200 *sl = 0;
201 } else
202 container[0] = 0;
203 close(fd);
204 count = 5;
205 while (((fd = ((devname[0] == '/')
206 ?open(devname, O_RDONLY|O_EXCL)
207 :open_dev_flags(devnm, O_RDONLY|O_EXCL))) < 0 ||
208 strcmp(fd2devnm(fd), devnm) != 0) && container[0] &&
209 mdmon_running(container) && count) {
210 /* Can't open, so something might be wrong. However it
211 * is a container, so we might be racing with mdmon, so
212 * retry for a bit.
213 */
214 if (fd >= 0)
215 close(fd);
216 flush_mdmon(container);
217 count--;
218 }
219 if (fd < 0 || strcmp(fd2devnm(fd), devnm) != 0) {
220 if (fd >= 0)
221 close(fd);
222 if (verbose >= 0)
223 pr_err("Cannot get exclusive access to %s:Perhaps a running process, mounted filesystem or active volume group?\n",
224 devname);
225 return 1;
226 }
227 /* If this is an mdmon managed array, just write 'inactive'
228 * to the array state and let mdmon clear up.
229 */
230 if (mdi &&
231 mdi->array.level > 0 &&
232 is_subarray(mdi->text_version)) {
233 int err;
234 /* This is mdmon managed. */
235 close(fd);
236
237 /* As we had an O_EXCL open, any use of the device
238 * which blocks STOP_ARRAY is probably a transient use,
239 * so it is reasonable to retry for a while - 5 seconds.
240 */
241 count = 25;
242 while (count &&
243 (err = sysfs_set_str(mdi, NULL,
244 "array_state",
245 "inactive")) < 0 &&
246 errno == EBUSY) {
247 sleep_for(0, MSEC_TO_NSEC(200), true);
248 count--;
249 }
250 if (err) {
251 if (verbose >= 0)
252 pr_err("failed to stop array %s: %s\n",
253 devname, strerror(errno));
254 rv = 1;
255 goto out;
256 }
257
258 /* Give monitor a chance to act */
259 ping_monitor(mdi->text_version);
260
261 fd = open_dev_excl(devnm);
262 if (fd < 0) {
263 if (verbose >= 0)
264 pr_err("failed to completely stop %s: Device is busy\n",
265 devname);
266 rv = 1;
267 goto out;
268 }
269 } else if (mdi &&
270 mdi->array.major_version == -1 &&
271 mdi->array.minor_version == -2 &&
272 !is_subarray(mdi->text_version)) {
273 struct mdstat_ent *mds, *m;
274 /* container, possibly mdmon-managed.
275 * Make sure mdmon isn't opening it, which
276 * would interfere with the 'stop'
277 */
278 ping_monitor(mdi->sys_name);
279
280 /* now check that there are no existing arrays
281 * which are members of this array
282 */
283 mds = mdstat_read(0, 0);
284 for (m = mds; m; m = m->next)
285 if (m->metadata_version &&
286 strncmp(m->metadata_version, "external:", 9)==0 &&
287 metadata_container_matches(m->metadata_version+9,
288 devnm)) {
289 if (verbose >= 0)
290 pr_err("Cannot stop container %s: member %s still active\n",
291 devname, m->devnm);
292 free_mdstat(mds);
293 rv = 1;
294 goto out;
295 }
296 }
297
298 /* If the array is undergoing a reshape which changes the number
299 * of devices, then it would be nice to stop it at a point where
300 * it has completed a full number of stripes in both old and
301 * new layouts as this will allow the reshape to be reverted.
302 * So if 'sync_action' is "reshape" and 'raid_disks' shows two
303 * different numbers, then
304 * - freeze reshape
305 * - set sync_max to next multiple of both data_disks and
306 * chunk sizes (or next but one)
307 * - unfreeze reshape
308 * - wait on 'sync_completed' for that point to be reached.
309 */
310 if (mdi && is_level456(mdi->array.level) &&
311 sysfs_attribute_available(mdi, NULL, "sync_action") &&
312 sysfs_attribute_available(mdi, NULL, "reshape_direction") &&
313 sysfs_get_str(mdi, NULL, "sync_action", buf, 20) > 0 &&
314 strcmp(buf, "reshape\n") == 0 &&
315 sysfs_get_two(mdi, NULL, "raid_disks", &rd1, &rd2) == 2) {
316 unsigned long long position, curr;
317 unsigned long long chunk1, chunk2;
318 unsigned long long rddiv, chunkdiv;
319 unsigned long long sectors;
320 unsigned long long sync_max, old_sync_max;
321 unsigned long long completed;
322 int backwards = 0;
323 int delay;
324 int scfd;
325
326 delay = 40;
327 while (rd1 > rd2 && delay > 0 &&
328 sysfs_get_ll(mdi, NULL, "sync_max", &old_sync_max) == 0) {
329 /* must be in the critical section - wait a bit */
330 delay -= 1;
331 sleep_for(0, MSEC_TO_NSEC(100), true);
332 }
333
334 if (sysfs_set_str(mdi, NULL, "sync_action", "frozen") != 0)
335 goto done;
336 /* Array is frozen */
337
338 rd1 -= mdi->array.level == 6 ? 2 : 1;
339 rd2 -= mdi->array.level == 6 ? 2 : 1;
340 sysfs_get_str(mdi, NULL, "reshape_direction", buf, sizeof(buf));
341 if (strncmp(buf, "back", 4) == 0)
342 backwards = 1;
343 if (sysfs_get_ll(mdi, NULL, "reshape_position", &position) != 0) {
344 /* reshape must have finished now */
345 sysfs_set_str(mdi, NULL, "sync_action", "idle");
346 goto done;
347 }
348 sysfs_get_two(mdi, NULL, "chunk_size", &chunk1, &chunk2);
349 chunk1 /= 512;
350 chunk2 /= 512;
351 rddiv = GCD(rd1, rd2);
352 chunkdiv = GCD(chunk1, chunk2);
353 sectors = (chunk1/chunkdiv) * chunk2 * (rd1/rddiv) * rd2;
354
355 if (backwards) {
356 /* Need to subtract 'reshape_position' from
357 * array size to get equivalent of sync_max.
358 * Size calculation based on raid5_size in kernel.
359 */
360 unsigned long long size = mdi->component_size;
361 size &= ~(chunk1-1);
362 size &= ~(chunk2-1);
363 /* rd1 must be smaller */
364 /* Reshape may have progressed further backwards than
365 * recorded, so target even further back (hence "-1")
366 */
367 position = (position / sectors - 1) * sectors;
368 /* rd1 is always the conversion factor between 'sync'
369 * position and 'reshape' position.
370 * We read 1 "new" stripe worth of data from where-ever,
371 * and when write out that full stripe.
372 */
373 sync_max = size - position/rd1;
374 } else {
375 /* Reshape will very likely be beyond position, and it may
376 * be too late to stop at '+1', so aim for '+2'
377 */
378 position = (position / sectors + 2) * sectors;
379 sync_max = position/rd1;
380 }
381 if (sysfs_get_ll(mdi, NULL, "sync_max", &old_sync_max) < 0)
382 old_sync_max = mdi->component_size;
383 /* Must not advance sync_max as that could confuse
384 * the reshape monitor */
385 if (sync_max < old_sync_max)
386 sysfs_set_num(mdi, NULL, "sync_max", sync_max);
387 sysfs_set_str(mdi, NULL, "sync_action", "idle");
388
389 /* That should have set things going again. Now we
390 * wait a little while (3 second max) for sync_completed
391 * to reach the target.
392 * The reshape process can block for 500msec if
393 * the sync speed limit is hit, so we need to wait
394 * a lot longer than that. 1 second is usually
395 * enough. 3 is safe.
396 */
397 delay = 3000;
398 scfd = sysfs_open(mdi->sys_name, NULL, "sync_completed");
399 while (scfd >= 0 && delay > 0 && old_sync_max > 0) {
400 unsigned long long max_completed;
401 sysfs_get_ll(mdi, NULL, "reshape_position", &curr);
402 sysfs_fd_get_str(scfd, buf, sizeof(buf));
403 if (strncmp(buf, "none", 4) == 0) {
404 /* Either reshape has aborted, or hasn't
405 * quite started yet. Wait a bit and
406 * check 'sync_action' to see.
407 */
408 sleep_for(0, MSEC_TO_NSEC(10), true);
409 sysfs_get_str(mdi, NULL, "sync_action", buf, sizeof(buf));
410 if (strncmp(buf, "reshape", 7) != 0)
411 break;
412 }
413
414 if (sysfs_fd_get_two(scfd, &completed,
415 &max_completed) == 2 &&
416 /* 'completed' sometimes reads as max-uulong */
417 completed < max_completed &&
418 (completed > sync_max ||
419 (completed == sync_max && curr != position))) {
420 while (completed > sync_max) {
421 sync_max += sectors / rd1;
422 if (backwards)
423 position -= sectors;
424 else
425 position += sectors;
426 }
427 if (sync_max < old_sync_max)
428 sysfs_set_num(mdi, NULL, "sync_max", sync_max);
429 }
430
431 if (!backwards && curr >= position)
432 break;
433 if (backwards && curr <= position)
434 break;
435 sysfs_wait(scfd, &delay);
436 }
437 if (scfd >= 0)
438 close(scfd);
439
440 }
441 done:
442
443 /* As we have an O_EXCL open, any use of the device
444 * which blocks STOP_ARRAY is probably a transient use,
445 * so it is reasonable to retry for a while - 5 seconds.
446 */
447 count = 25; err = 0;
448 while (count && fd >= 0 &&
449 (err = ioctl(fd, STOP_ARRAY, NULL)) < 0 && errno == EBUSY) {
450 sleep_for(0, MSEC_TO_NSEC(200), true);
451 count --;
452 }
453 if (fd >= 0 && err) {
454 if (verbose >= 0) {
455 pr_err("failed to stop array %s: %s\n",
456 devname, strerror(errno));
457 if (errno == EBUSY)
458 cont_err("Perhaps a running process, mounted filesystem or active volume group?\n");
459 }
460 rv = 1;
461 goto out;
462 }
463
464 if (get_linux_version() < 2006028) {
465 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
466 * was stopped, so We'll do it here just to be sure. Drop any
467 * partitions as well...
468 */
469 if (fd >= 0)
470 ioctl(fd, BLKRRPART, 0);
471 if (mdi)
472 sysfs_uevent(mdi, "change");
473 }
474
475 if (devnm[0] && use_udev()) {
476 struct map_ent *mp = map_by_devnm(&map, devnm);
477 remove_devices(devnm, mp ? mp->path : NULL);
478 }
479
480 if (verbose >= 0)
481 pr_err("stopped %s\n", devname);
482 map_lock(&map);
483 map_remove(&map, devnm);
484 map_unlock(&map);
485 out:
486 sysfs_free(mdi);
487
488 return rv;
489 }
490
491 static struct mddev_dev *add_one(struct mddev_dev *dv, char *name, char disp)
492 {
493 struct mddev_dev *new;
494 new = xmalloc(sizeof(*new));
495 memset(new, 0, sizeof(*new));
496 new->devname = xstrdup(name);
497 new->disposition = disp;
498 new->next = dv->next;
499 dv->next = new;
500 return new;
501 }
502
503 static void add_faulty(struct mddev_dev *dv, int fd, char disp)
504 {
505 mdu_array_info_t array;
506 mdu_disk_info_t disk;
507 int remaining_disks;
508 int i;
509
510 if (md_get_array_info(fd, &array) != 0)
511 return;
512
513 remaining_disks = array.nr_disks;
514 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
515 char buf[40];
516 disk.number = i;
517 if (md_get_disk_info(fd, &disk) != 0)
518 continue;
519 if (disk.major == 0 && disk.minor == 0)
520 continue;
521 remaining_disks--;
522 if ((disk.state & 1) == 0) /* not faulty */
523 continue;
524 sprintf(buf, "%d:%d", disk.major, disk.minor);
525 dv = add_one(dv, buf, disp);
526 }
527 }
528
529 static void add_detached(struct mddev_dev *dv, int fd, char disp)
530 {
531 mdu_array_info_t array;
532 mdu_disk_info_t disk;
533 int remaining_disks;
534 int i;
535
536 if (md_get_array_info(fd, &array) != 0)
537 return;
538
539 remaining_disks = array.nr_disks;
540 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
541 char buf[40];
542 int sfd;
543 disk.number = i;
544 if (md_get_disk_info(fd, &disk) != 0)
545 continue;
546 if (disk.major == 0 && disk.minor == 0)
547 continue;
548 remaining_disks--;
549 if (disp == 'f' && (disk.state & 1) != 0) /* already faulty */
550 continue;
551 sprintf(buf, "%d:%d", disk.major, disk.minor);
552 sfd = dev_open(buf, O_RDONLY);
553 if (sfd >= 0) {
554 /* Not detached */
555 close(sfd);
556 continue;
557 }
558 if (errno != ENXIO)
559 /* Probably not detached */
560 continue;
561 dv = add_one(dv, buf, disp);
562 }
563 }
564
565 static void add_set(struct mddev_dev *dv, int fd, char set_char)
566 {
567 mdu_array_info_t array;
568 mdu_disk_info_t disk;
569 int remaining_disks;
570 int copies, set;
571 int i;
572
573 if (md_get_array_info(fd, &array) != 0)
574 return;
575 if (array.level != 10)
576 return;
577 copies = ((array.layout & 0xff) *
578 ((array.layout >> 8) & 0xff));
579 if (array.raid_disks % copies)
580 return;
581
582 remaining_disks = array.nr_disks;
583 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
584 char buf[40];
585 disk.number = i;
586 if (md_get_disk_info(fd, &disk) != 0)
587 continue;
588 if (disk.major == 0 && disk.minor == 0)
589 continue;
590 remaining_disks--;
591 set = disk.raid_disk % copies;
592 if (set_char != set + 'A')
593 continue;
594 sprintf(buf, "%d:%d", disk.major, disk.minor);
595 dv = add_one(dv, buf, dv->disposition);
596 }
597 }
598
599 int attempt_re_add(int fd, int tfd, struct mddev_dev *dv,
600 struct supertype *dev_st, struct supertype *tst,
601 unsigned long rdev,
602 char *update, char *devname, int verbose,
603 mdu_array_info_t *array)
604 {
605 struct mdinfo mdi;
606 int duuid[4];
607 int ouuid[4];
608
609 dev_st->ss->getinfo_super(dev_st, &mdi, NULL);
610 dev_st->ss->uuid_from_super(dev_st, ouuid);
611 if (tst->sb)
612 tst->ss->uuid_from_super(tst, duuid);
613 else
614 /* Assume uuid matches: kernel will check */
615 memcpy(duuid, ouuid, sizeof(ouuid));
616 if ((mdi.disk.state & (1<<MD_DISK_ACTIVE)) &&
617 !(mdi.disk.state & (1<<MD_DISK_FAULTY)) &&
618 memcmp(duuid, ouuid, sizeof(ouuid))==0) {
619 /* Looks like it is worth a
620 * try. Need to make sure
621 * kernel will accept it
622 * though.
623 */
624 mdu_disk_info_t disc;
625 /* re-add doesn't work for version-1 superblocks
626 * before 2.6.18 :-(
627 */
628 if (array->major_version == 1 &&
629 get_linux_version() <= 2006018)
630 goto skip_re_add;
631 disc.number = mdi.disk.number;
632 if (md_get_disk_info(fd, &disc) != 0 ||
633 disc.major != 0 || disc.minor != 0)
634 goto skip_re_add;
635 disc.major = major(rdev);
636 disc.minor = minor(rdev);
637 disc.number = mdi.disk.number;
638 disc.raid_disk = mdi.disk.raid_disk;
639 disc.state = mdi.disk.state;
640 if (array->state & (1 << MD_SB_CLUSTERED)) {
641 /* extra flags are needed when adding to a cluster as
642 * there are two cases to distinguish
643 */
644 if (dv->disposition == 'c')
645 disc.state |= (1 << MD_DISK_CANDIDATE);
646 else
647 disc.state |= (1 << MD_DISK_CLUSTER_ADD);
648 }
649 if (dv->writemostly == FlagSet)
650 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
651 if (dv->writemostly == FlagClear)
652 disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
653 if (dv->failfast == FlagSet)
654 disc.state |= 1 << MD_DISK_FAILFAST;
655 if (dv->failfast == FlagClear)
656 disc.state &= ~(1 << MD_DISK_FAILFAST);
657 remove_partitions(tfd);
658 if (update || dv->writemostly != FlagDefault ||
659 dv->failfast != FlagDefault) {
660 int rv = -1;
661 tfd = dev_open(dv->devname, O_RDWR);
662 if (tfd < 0) {
663 pr_err("failed to open %s for superblock update during re-add\n", dv->devname);
664 return -1;
665 }
666
667 if (dv->writemostly == FlagSet)
668 rv = dev_st->ss->update_super(
669 dev_st, NULL, "writemostly",
670 devname, verbose, 0, NULL);
671 if (dv->writemostly == FlagClear)
672 rv = dev_st->ss->update_super(
673 dev_st, NULL, "readwrite",
674 devname, verbose, 0, NULL);
675 if (dv->failfast == FlagSet)
676 rv = dev_st->ss->update_super(
677 dev_st, NULL, "failfast",
678 devname, verbose, 0, NULL);
679 if (dv->failfast == FlagClear)
680 rv = dev_st->ss->update_super(
681 dev_st, NULL, "nofailfast",
682 devname, verbose, 0, NULL);
683 if (update)
684 rv = dev_st->ss->update_super(
685 dev_st, NULL, update,
686 devname, verbose, 0, NULL);
687 if (rv == 0)
688 rv = dev_st->ss->store_super(dev_st, tfd);
689 close(tfd);
690 if (rv != 0) {
691 pr_err("failed to update superblock during re-add\n");
692 return -1;
693 }
694 }
695 /* don't even try if disk is marked as faulty */
696 errno = 0;
697 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
698 if (verbose >= 0)
699 pr_err("re-added %s\n", dv->devname);
700 return 1;
701 }
702 if (errno == ENOMEM || errno == EROFS) {
703 pr_err("add new device failed for %s: %s\n",
704 dv->devname, strerror(errno));
705 if (dv->disposition == 'M')
706 return 0;
707 return -1;
708 }
709 }
710 skip_re_add:
711 return 0;
712 }
713
714 int Manage_add(int fd, int tfd, struct mddev_dev *dv,
715 struct supertype *tst, mdu_array_info_t *array,
716 int force, int verbose, char *devname,
717 char *update, unsigned long rdev, unsigned long long array_size,
718 int raid_slot)
719 {
720 unsigned long long ldsize;
721 struct supertype *dev_st;
722 int j;
723 mdu_disk_info_t disc;
724
725 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
726 if (dv->disposition == 'M')
727 return 0;
728 else
729 return -1;
730 }
731
732 if (tst->ss == &super0 && ldsize > 4ULL*1024*1024*1024*1024) {
733 /* More than 4TB is wasted on v0.90 */
734 if (!force) {
735 pr_err("%s is larger than %s can effectively use.\n"
736 " Add --force is you really want to add this device.\n",
737 dv->devname, devname);
738 return -1;
739 }
740 pr_err("%s is larger than %s can effectively use.\n"
741 " Adding anyway as --force was given.\n",
742 dv->devname, devname);
743 }
744
745 if (array->not_persistent == 0 || tst->ss->external) {
746
747 /* need to find a sample superblock to copy, and
748 * a spare slot to use.
749 * For 'external' array (well, container based),
750 * We can just load the metadata for the array->
751 */
752 int array_failed;
753 if (tst->sb)
754 /* already loaded */;
755 else if (tst->ss->external) {
756 tst->ss->load_container(tst, fd, NULL);
757 } else for (j = 0; j < tst->max_devs; j++) {
758 char *dev;
759 int dfd;
760 disc.number = j;
761 if (md_get_disk_info(fd, &disc))
762 continue;
763 if (disc.major==0 && disc.minor==0)
764 continue;
765 if ((disc.state & 4)==0) /* sync */
766 continue;
767 /* Looks like a good device to try */
768 dev = map_dev(disc.major, disc.minor, 1);
769 if (!dev)
770 continue;
771 dfd = dev_open(dev, O_RDONLY);
772 if (dfd < 0)
773 continue;
774 if (tst->ss->load_super(tst, dfd,
775 NULL)) {
776 close(dfd);
777 continue;
778 }
779 close(dfd);
780 break;
781 }
782 /* FIXME this is a bad test to be using */
783 if (!tst->sb && (dv->disposition != 'a' &&
784 dv->disposition != 'S')) {
785 /* we are re-adding a device to a
786 * completely dead array - have to depend
787 * on kernel to check
788 */
789 } else if (!tst->sb) {
790 pr_err("cannot load array metadata from %s\n", devname);
791 return -1;
792 }
793
794 /* Make sure device is large enough */
795 if (dv->disposition != 'j' && /* skip size check for Journal */
796 tst->sb &&
797 tst->ss->avail_size(tst, ldsize/512, INVALID_SECTORS) <
798 array_size) {
799 if (dv->disposition == 'M')
800 return 0;
801 pr_err("%s not large enough to join array\n",
802 dv->devname);
803 return -1;
804 }
805
806 /* Possibly this device was recently part of
807 * the array and was temporarily removed, and
808 * is now being re-added. If so, we can
809 * simply re-add it.
810 */
811
812 if (array->not_persistent == 0) {
813 dev_st = dup_super(tst);
814 dev_st->ss->load_super(dev_st, tfd, NULL);
815 if (dev_st->sb && dv->disposition != 'S') {
816 int rv;
817
818 rv = attempt_re_add(fd, tfd, dv, dev_st, tst,
819 rdev, update, devname,
820 verbose, array);
821 dev_st->ss->free_super(dev_st);
822 if (rv)
823 return rv;
824 }
825 }
826 if (dv->disposition == 'M') {
827 if (verbose > 0)
828 pr_err("--re-add for %s to %s is not possible\n",
829 dv->devname, devname);
830 return 0;
831 }
832 if (dv->disposition == 'A') {
833 pr_err("--re-add for %s to %s is not possible\n",
834 dv->devname, devname);
835 return -1;
836 }
837 if (array->active_disks < array->raid_disks) {
838 char *avail = xcalloc(array->raid_disks, 1);
839 int d;
840 int found = 0;
841
842 for (d = 0; d < MAX_DISKS && found < array->nr_disks; d++) {
843 disc.number = d;
844 if (md_get_disk_info(fd, &disc))
845 continue;
846 if (disc.major == 0 && disc.minor == 0)
847 continue;
848 if (!(disc.state & (1<<MD_DISK_SYNC)))
849 continue;
850 avail[disc.raid_disk] = 1;
851 found++;
852 }
853 array_failed = !enough(array->level, array->raid_disks,
854 array->layout, 1, avail);
855 free(avail);
856 } else
857 array_failed = 0;
858 if (array_failed) {
859 pr_err("%s has failed so using --add cannot work and might destroy\n",
860 devname);
861 pr_err("data on %s. You should stop the array and re-assemble it.\n",
862 dv->devname);
863 return -1;
864 }
865 } else {
866 /* non-persistent. Must ensure that new drive
867 * is at least array->size big.
868 */
869 if (ldsize/512 < array_size) {
870 pr_err("%s not large enough to join array\n",
871 dv->devname);
872 return -1;
873 }
874 }
875 /* committed to really trying this device now*/
876 remove_partitions(tfd);
877
878 /* in 2.6.17 and earlier, version-1 superblocks won't
879 * use the number we write, but will choose a free number.
880 * we must choose the same free number, which requires
881 * starting at 'raid_disks' and counting up
882 */
883 for (j = array->raid_disks; j < tst->max_devs; j++) {
884 disc.number = j;
885 if (md_get_disk_info(fd, &disc))
886 break;
887 if (disc.major==0 && disc.minor==0)
888 break;
889 if (disc.state & 8) /* removed */
890 break;
891 }
892 disc.major = major(rdev);
893 disc.minor = minor(rdev);
894 if (raid_slot < 0)
895 disc.number = j;
896 else
897 disc.number = raid_slot;
898 disc.state = 0;
899
900 /* only add journal to array that supports journaling */
901 if (dv->disposition == 'j') {
902 struct mdinfo *mdp;
903
904 mdp = sysfs_read(fd, NULL, GET_ARRAY_STATE);
905 if (!mdp) {
906 pr_err("%s unable to read array state.\n", devname);
907 return -1;
908 }
909
910 if (mdp->array_state != ARRAY_READONLY) {
911 sysfs_free(mdp);
912 pr_err("%s is not readonly, cannot add journal.\n", devname);
913 return -1;
914 }
915
916 sysfs_free(mdp);
917
918 disc.raid_disk = 0;
919 }
920
921 if (array->not_persistent==0) {
922 int dfd;
923 if (dv->disposition == 'j')
924 disc.state |= (1 << MD_DISK_JOURNAL) | (1 << MD_DISK_SYNC);
925 if (dv->writemostly == FlagSet)
926 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
927 if (dv->failfast == FlagSet)
928 disc.state |= 1 << MD_DISK_FAILFAST;
929 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
930 if (tst->ss->add_to_super(tst, &disc, dfd,
931 dv->devname, INVALID_SECTORS))
932 return -1;
933 if (tst->ss->write_init_super(tst))
934 return -1;
935 } else if (dv->disposition == 'A') {
936 /* this had better be raid1.
937 * As we are "--re-add"ing we must find a spare slot
938 * to fill.
939 */
940 char *used = xcalloc(array->raid_disks, 1);
941 for (j = 0; j < tst->max_devs; j++) {
942 mdu_disk_info_t disc2;
943 disc2.number = j;
944 if (md_get_disk_info(fd, &disc2))
945 continue;
946 if (disc2.major==0 && disc2.minor==0)
947 continue;
948 if (disc2.state & 8) /* removed */
949 continue;
950 if (disc2.raid_disk < 0)
951 continue;
952 if (disc2.raid_disk > array->raid_disks)
953 continue;
954 used[disc2.raid_disk] = 1;
955 }
956 for (j = 0 ; j < array->raid_disks; j++)
957 if (!used[j]) {
958 disc.raid_disk = j;
959 disc.state |= (1<<MD_DISK_SYNC);
960 break;
961 }
962 free(used);
963 }
964
965 if (array->state & (1 << MD_SB_CLUSTERED)) {
966 if (dv->disposition == 'c')
967 disc.state |= (1 << MD_DISK_CANDIDATE);
968 else
969 disc.state |= (1 << MD_DISK_CLUSTER_ADD);
970 }
971
972 if (dv->writemostly == FlagSet)
973 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
974 if (dv->failfast == FlagSet)
975 disc.state |= (1 << MD_DISK_FAILFAST);
976 if (tst->ss->external) {
977 /* add a disk
978 * to an external metadata container */
979 struct mdinfo new_mdi;
980 struct mdinfo *sra;
981 int container_fd;
982 char devnm[32];
983 int dfd;
984
985 strcpy(devnm, fd2devnm(fd));
986
987 container_fd = open_dev_excl(devnm);
988 if (container_fd < 0) {
989 pr_err("add failed for %s: could not get exclusive access to container\n",
990 dv->devname);
991 tst->ss->free_super(tst);
992 return -1;
993 }
994
995 /* Check if metadata handler is able to accept the drive */
996 if (!tst->ss->validate_geometry(tst, LEVEL_CONTAINER, 0, 1, NULL,
997 0, 0, dv->devname, NULL, 0, 1)) {
998 close(container_fd);
999 return -1;
1000 }
1001
1002 Kill(dv->devname, NULL, 0, -1, 0);
1003 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
1004 if (tst->ss->add_to_super(tst, &disc, dfd,
1005 dv->devname, INVALID_SECTORS)) {
1006 close(dfd);
1007 close(container_fd);
1008 return -1;
1009 }
1010 if (!mdmon_running(tst->container_devnm))
1011 tst->ss->sync_metadata(tst);
1012
1013 sra = sysfs_read(container_fd, NULL, 0);
1014 if (!sra) {
1015 pr_err("add failed for %s: sysfs_read failed\n",
1016 dv->devname);
1017 close(container_fd);
1018 tst->ss->free_super(tst);
1019 return -1;
1020 }
1021 sra->array.level = LEVEL_CONTAINER;
1022 /* Need to set data_offset and component_size */
1023 tst->ss->getinfo_super(tst, &new_mdi, NULL);
1024 new_mdi.disk.major = disc.major;
1025 new_mdi.disk.minor = disc.minor;
1026 new_mdi.recovery_start = 0;
1027 /* Make sure fds are closed as they are O_EXCL which
1028 * would block add_disk */
1029 tst->ss->free_super(tst);
1030 if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
1031 pr_err("add new device to external metadata failed for %s\n", dv->devname);
1032 close(container_fd);
1033 sysfs_free(sra);
1034 return -1;
1035 }
1036 ping_monitor(devnm);
1037 sysfs_free(sra);
1038 close(container_fd);
1039 } else {
1040 tst->ss->free_super(tst);
1041 if (ioctl(fd, ADD_NEW_DISK, &disc)) {
1042 if (dv->disposition == 'j')
1043 pr_err("Failed to hot add %s as journal, "
1044 "please try restart %s.\n", dv->devname, devname);
1045 else
1046 pr_err("add new device failed for %s as %d: %s\n",
1047 dv->devname, j, strerror(errno));
1048 return -1;
1049 }
1050 if (dv->disposition == 'j') {
1051 pr_err("Journal added successfully, making %s read-write\n", devname);
1052 if (Manage_ro(devname, fd, -1))
1053 pr_err("Failed to make %s read-write\n", devname);
1054 }
1055
1056 }
1057 if (verbose >= 0)
1058 pr_err("added %s\n", dv->devname);
1059 return 1;
1060 }
1061
1062 int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
1063 int sysfd, unsigned long rdev, int force, int verbose, char *devname)
1064 {
1065 int lfd = -1;
1066 int err;
1067
1068 if (tst->ss->external) {
1069 /* To remove a device from a container, we must
1070 * check that it isn't in use in an array.
1071 * This involves looking in the 'holders'
1072 * directory - there must be just one entry,
1073 * the container.
1074 * To ensure that it doesn't get used as a
1075 * hot spare while we are checking, we
1076 * get an O_EXCL open on the container
1077 */
1078 int ret;
1079 char devnm[32];
1080 strcpy(devnm, fd2devnm(fd));
1081 lfd = open_dev_excl(devnm);
1082 if (lfd < 0) {
1083 pr_err("Cannot get exclusive access to container - odd\n");
1084 return -1;
1085 }
1086 /* We may not be able to check on holders in
1087 * sysfs, either because we don't have the dev num
1088 * (rdev == 0) or because the device has been detached
1089 * and the 'holders' directory no longer exists
1090 * (ret == -1). In that case, assume it is OK to
1091 * remove.
1092 */
1093 if (rdev == 0)
1094 ret = -1;
1095 else {
1096 /*
1097 * The drive has already been set to 'faulty', however
1098 * monitor might not have had time to process it and the
1099 * drive might still have an entry in the 'holders'
1100 * directory. Try a few times to avoid a false error
1101 */
1102 int count = 20;
1103
1104 do {
1105 ret = sysfs_unique_holder(devnm, rdev);
1106 if (ret < 2)
1107 break;
1108 sleep_for(0, MSEC_TO_NSEC(100), true);
1109 } while (--count > 0);
1110
1111 if (ret == 0) {
1112 pr_err("%s is not a member, cannot remove.\n",
1113 dv->devname);
1114 close(lfd);
1115 return -1;
1116 }
1117 if (ret >= 2) {
1118 pr_err("%s is still in use, cannot remove.\n",
1119 dv->devname);
1120 close(lfd);
1121 return -1;
1122 }
1123 }
1124 }
1125 /* FIXME check that it is a current member */
1126 if (sysfd >= 0) {
1127 /* device has been removed and we don't know
1128 * the major:minor number
1129 */
1130 err = sys_hot_remove_disk(sysfd, force);
1131 } else {
1132 err = hot_remove_disk(fd, rdev, force);
1133 if (err && errno == ENODEV) {
1134 /* Old kernels rejected this if no personality
1135 * is registered */
1136 struct mdinfo *sra = sysfs_read(fd, NULL, GET_DEVS);
1137 struct mdinfo *dv = NULL;
1138 if (sra)
1139 dv = sra->devs;
1140 for ( ; dv ; dv=dv->next)
1141 if (dv->disk.major == (int)major(rdev) &&
1142 dv->disk.minor == (int)minor(rdev))
1143 break;
1144 if (dv)
1145 err = sysfs_set_str(sra, dv,
1146 "state", "remove");
1147 else
1148 err = -1;
1149 sysfs_free(sra);
1150 }
1151 }
1152 if (err) {
1153 pr_err("hot remove failed for %s: %s\n", dv->devname,
1154 strerror(errno));
1155 if (lfd >= 0)
1156 close(lfd);
1157 return -1;
1158 }
1159 if (tst->ss->external) {
1160 /*
1161 * Before dropping our exclusive open we make an
1162 * attempt at preventing mdmon from seeing an
1163 * 'add' event before reconciling this 'remove'
1164 * event.
1165 */
1166 char *devnm = fd2devnm(fd);
1167
1168 if (!devnm) {
1169 pr_err("unable to get container name\n");
1170 return -1;
1171 }
1172
1173 ping_manager(devnm);
1174 }
1175 if (lfd >= 0)
1176 close(lfd);
1177 if (verbose >= 0)
1178 pr_err("hot removed %s from %s\n",
1179 dv->devname, devname);
1180 return 1;
1181 }
1182
1183 int Manage_replace(struct supertype *tst, int fd, struct mddev_dev *dv,
1184 unsigned long rdev, int verbose, char *devname)
1185 {
1186 struct mdinfo *mdi, *di;
1187 if (tst->ss->external) {
1188 pr_err("--replace only supported for native metadata (0.90 or 1.x)\n");
1189 return -1;
1190 }
1191 /* Need to find the device in sysfs and add 'want_replacement' to the
1192 * status.
1193 */
1194 mdi = sysfs_read(fd, NULL, GET_DEVS);
1195 if (!mdi || !mdi->devs) {
1196 pr_err("Cannot find status of %s to enable replacement - strange\n",
1197 devname);
1198 return -1;
1199 }
1200 for (di = mdi->devs; di; di = di->next)
1201 if (di->disk.major == (int)major(rdev) &&
1202 di->disk.minor == (int)minor(rdev))
1203 break;
1204 if (di) {
1205 int rv;
1206 if (di->disk.raid_disk < 0) {
1207 pr_err("%s is not active and so cannot be replaced.\n",
1208 dv->devname);
1209 sysfs_free(mdi);
1210 return -1;
1211 }
1212 rv = sysfs_set_str(mdi, di,
1213 "state", "want_replacement");
1214 if (rv) {
1215 sysfs_free(mdi);
1216 pr_err("Failed to request replacement for %s\n",
1217 dv->devname);
1218 return -1;
1219 }
1220 if (verbose >= 0)
1221 pr_err("Marked %s (device %d in %s) for replacement\n",
1222 dv->devname, di->disk.raid_disk, devname);
1223 /* If there is a matching 'with', we need to tell it which
1224 * raid disk
1225 */
1226 while (dv && dv->disposition != 'W')
1227 dv = dv->next;
1228 if (dv) {
1229 dv->disposition = 'w';
1230 dv->used = di->disk.raid_disk;
1231 }
1232 return 1;
1233 }
1234 sysfs_free(mdi);
1235 pr_err("%s not found in %s so cannot --replace it\n",
1236 dv->devname, devname);
1237 return -1;
1238 }
1239
1240 int Manage_with(struct supertype *tst, int fd, struct mddev_dev *dv,
1241 unsigned long rdev, int verbose, char *devname)
1242 {
1243 struct mdinfo *mdi, *di;
1244 /* try to set 'slot' for 'rdev' in 'fd' to 'dv->used' */
1245 mdi = sysfs_read(fd, NULL, GET_DEVS|GET_STATE);
1246 if (!mdi || !mdi->devs) {
1247 pr_err("Cannot find status of %s to enable replacement - strange\n",
1248 devname);
1249 return -1;
1250 }
1251 for (di = mdi->devs; di; di = di->next)
1252 if (di->disk.major == (int)major(rdev) &&
1253 di->disk.minor == (int)minor(rdev))
1254 break;
1255 if (di) {
1256 int rv;
1257 if (di->disk.state & (1<<MD_DISK_FAULTY)) {
1258 pr_err("%s is faulty and cannot be a replacement\n",
1259 dv->devname);
1260 sysfs_free(mdi);
1261 return -1;
1262 }
1263 if (di->disk.raid_disk >= 0) {
1264 pr_err("%s is active and cannot be a replacement\n",
1265 dv->devname);
1266 sysfs_free(mdi);
1267 return -1;
1268 }
1269 rv = sysfs_set_num(mdi, di,
1270 "slot", dv->used);
1271 if (rv) {
1272 sysfs_free(mdi);
1273 pr_err("Failed to set %s as preferred replacement.\n",
1274 dv->devname);
1275 return -1;
1276 }
1277 if (verbose >= 0)
1278 pr_err("Marked %s in %s as replacement for device %d\n",
1279 dv->devname, devname, dv->used);
1280 return 1;
1281 }
1282 sysfs_free(mdi);
1283 pr_err("%s not found in %s so cannot make it preferred replacement\n",
1284 dv->devname, devname);
1285 return -1;
1286 }
1287
1288 /**
1289 * is_remove_safe() - Check if remove is safe.
1290 * @array: Array info.
1291 * @fd: Array file descriptor.
1292 * @devname: Name of device to remove.
1293 * @verbose: Verbose.
1294 *
1295 * The function determines if array will be operational
1296 * after removing &devname.
1297 *
1298 * Return: True if array will be operational, false otherwise.
1299 */
1300 bool is_remove_safe(mdu_array_info_t *array, const int fd, char *devname, const int verbose)
1301 {
1302 dev_t devid = devnm2devid(devname + 5);
1303 struct mdinfo *mdi = sysfs_read(fd, NULL, GET_DEVS | GET_DISKS | GET_STATE);
1304
1305 if (!mdi) {
1306 if (verbose)
1307 pr_err("Failed to read sysfs attributes for %s\n", devname);
1308 return false;
1309 }
1310
1311 char *avail = xcalloc(array->raid_disks, sizeof(char));
1312
1313 for (mdi = mdi->devs; mdi; mdi = mdi->next) {
1314 if (mdi->disk.raid_disk < 0)
1315 continue;
1316 if (!(mdi->disk.state & (1 << MD_DISK_SYNC)))
1317 continue;
1318 if (makedev(mdi->disk.major, mdi->disk.minor) == devid)
1319 continue;
1320 avail[mdi->disk.raid_disk] = 1;
1321 }
1322 sysfs_free(mdi);
1323
1324 bool is_enough = enough(array->level, array->raid_disks,
1325 array->layout, (array->state & 1),
1326 avail);
1327
1328 free(avail);
1329 return is_enough;
1330 }
1331
1332 int Manage_subdevs(char *devname, int fd,
1333 struct mddev_dev *devlist, int verbose, int test,
1334 char *update, int force)
1335 {
1336 /* Do something to each dev.
1337 * devmode can be
1338 * 'a' - add the device
1339 * 'S' - add the device as a spare - don't try re-add
1340 * 'j' - add the device as a journal device
1341 * 'A' - re-add the device
1342 * 'r' - remove the device: HOT_REMOVE_DISK
1343 * device can be 'faulty' or 'detached' in which case all
1344 * matching devices are removed.
1345 * 'f' - set the device faulty SET_DISK_FAULTY
1346 * device can be 'detached' in which case any device that
1347 * is inaccessible will be marked faulty.
1348 * 'R' - mark this device as wanting replacement.
1349 * 'W' - this device is added if necessary and activated as
1350 * a replacement for a previous 'R' device.
1351 * -----
1352 * 'w' - 'W' will be changed to 'w' when it is paired with
1353 * a 'R' device. If a 'W' is found while walking the list
1354 * it must be unpaired, and is an error.
1355 * 'M' - this is created by a 'missing' target. It is a slight
1356 * variant on 'A'
1357 * 'F' - Another variant of 'A', where the device was faulty
1358 * so must be removed from the array first.
1359 * 'c' - confirm the device as found (for clustered environments)
1360 *
1361 * For 'f' and 'r', the device can also be a kernel-internal
1362 * name such as 'sdb'.
1363 */
1364 mdu_array_info_t array;
1365 unsigned long long array_size;
1366 struct mddev_dev *dv;
1367 int tfd = -1;
1368 struct supertype *tst;
1369 char *subarray = NULL;
1370 int sysfd = -1;
1371 int count = 0; /* number of actions taken */
1372 struct mdinfo info;
1373 struct mdinfo devinfo;
1374 int frozen = 0;
1375 int busy = 0;
1376 int raid_slot = -1;
1377
1378 if (sysfs_init(&info, fd, NULL)) {
1379 pr_err("sysfs not availabile for %s\n", devname);
1380 goto abort;
1381 }
1382
1383 if (md_get_array_info(fd, &array)) {
1384 pr_err("Cannot get array info for %s\n", devname);
1385 goto abort;
1386 }
1387 /* array.size is only 32 bits and may be truncated.
1388 * So read from sysfs if possible, and record number of sectors
1389 */
1390
1391 array_size = get_component_size(fd);
1392 if (array_size <= 0)
1393 array_size = array.size * 2;
1394
1395 tst = super_by_fd(fd, &subarray);
1396 if (!tst) {
1397 pr_err("unsupport array - version %d.%d\n",
1398 array.major_version, array.minor_version);
1399 goto abort;
1400 }
1401
1402 for (dv = devlist; dv; dv = dv->next) {
1403 dev_t rdev = 0; /* device to add/remove etc */
1404 int rv;
1405 int mj,mn;
1406
1407 raid_slot = -1;
1408 if (dv->disposition == 'c') {
1409 rv = parse_cluster_confirm_arg(dv->devname,
1410 &dv->devname,
1411 &raid_slot);
1412 if (rv) {
1413 pr_err("Could not get the devname of cluster\n");
1414 goto abort;
1415 }
1416 }
1417
1418 if (strcmp(dv->devname, "failed") == 0 ||
1419 strcmp(dv->devname, "faulty") == 0) {
1420 if (dv->disposition != 'A' && dv->disposition != 'r') {
1421 pr_err("%s only meaningful with -r or --re-add, not -%c\n",
1422 dv->devname, dv->disposition);
1423 goto abort;
1424 }
1425 add_faulty(dv, fd, (dv->disposition == 'A'
1426 ? 'F' : 'r'));
1427 continue;
1428 }
1429 if (strcmp(dv->devname, "detached") == 0) {
1430 if (dv->disposition != 'r' && dv->disposition != 'f') {
1431 pr_err("%s only meaningful with -r of -f, not -%c\n",
1432 dv->devname, dv->disposition);
1433 goto abort;
1434 }
1435 add_detached(dv, fd, dv->disposition);
1436 continue;
1437 }
1438
1439 if (strcmp(dv->devname, "missing") == 0) {
1440 struct mddev_dev *add_devlist;
1441 struct mddev_dev **dp;
1442 if (dv->disposition == 'c') {
1443 rv = ioctl(fd, CLUSTERED_DISK_NACK, NULL);
1444 break;
1445 }
1446
1447 if (dv->disposition != 'A') {
1448 pr_err("'missing' only meaningful with --re-add\n");
1449 goto abort;
1450 }
1451 add_devlist = conf_get_devs();
1452 if (add_devlist == NULL) {
1453 pr_err("no devices to scan for missing members.\n");
1454 continue;
1455 }
1456 for (dp = &add_devlist; *dp; dp = & (*dp)->next)
1457 /* 'M' (for 'missing') is like 'A' without errors */
1458 (*dp)->disposition = 'M';
1459 *dp = dv->next;
1460 dv->next = add_devlist;
1461 continue;
1462 }
1463
1464 if (strncmp(dv->devname, "set-", 4) == 0 &&
1465 strlen(dv->devname) == 5) {
1466 int copies;
1467
1468 if (dv->disposition != 'r' &&
1469 dv->disposition != 'f') {
1470 pr_err("'%s' only meaningful with -r or -f\n",
1471 dv->devname);
1472 goto abort;
1473 }
1474 if (array.level != 10) {
1475 pr_err("'%s' only meaningful with RAID10 arrays\n",
1476 dv->devname);
1477 goto abort;
1478 }
1479 copies = ((array.layout & 0xff) *
1480 ((array.layout >> 8) & 0xff));
1481 if (array.raid_disks % copies != 0 ||
1482 dv->devname[4] < 'A' ||
1483 dv->devname[4] >= 'A' + copies ||
1484 copies > 26) {
1485 pr_err("'%s' not meaningful with this array\n",
1486 dv->devname);
1487 goto abort;
1488 }
1489 add_set(dv, fd, dv->devname[4]);
1490 continue;
1491 }
1492
1493 if (strchr(dv->devname, '/') == NULL &&
1494 strchr(dv->devname, ':') == NULL &&
1495 strlen(dv->devname) < 50) {
1496 /* Assume this is a kernel-internal name like 'sda1' */
1497 int found = 0;
1498 char dname[55];
1499 if (dv->disposition != 'r' && dv->disposition != 'f') {
1500 pr_err("%s only meaningful with -r or -f, not -%c\n",
1501 dv->devname, dv->disposition);
1502 goto abort;
1503 }
1504
1505 sprintf(dname, "dev-%s", dv->devname);
1506 sysfd = sysfs_open(fd2devnm(fd), dname, "block/dev");
1507 if (sysfd >= 0) {
1508 char dn[20];
1509 if (sysfs_fd_get_str(sysfd, dn, 20) > 0 &&
1510 sscanf(dn, "%d:%d", &mj,&mn) == 2) {
1511 rdev = makedev(mj,mn);
1512 found = 1;
1513 }
1514 close(sysfd);
1515 sysfd = -1;
1516 }
1517 if (!found) {
1518 sysfd = sysfs_open(fd2devnm(fd), dname, "state");
1519 if (sysfd < 0) {
1520 pr_err("%s does not appear to be a component of %s\n",
1521 dv->devname, devname);
1522 goto abort;
1523 }
1524 }
1525 } else if ((dv->disposition == 'r' ||
1526 dv->disposition == 'f') &&
1527 get_maj_min(dv->devname, &mj, &mn)) {
1528 /* for 'fail' and 'remove', the device might
1529 * not exist.
1530 */
1531 rdev = makedev(mj, mn);
1532 } else {
1533 tfd = dev_open(dv->devname, O_RDONLY);
1534 if (tfd >= 0) {
1535 fstat_is_blkdev(tfd, dv->devname, &rdev);
1536 close(tfd);
1537 } else {
1538 int open_err = errno;
1539 if (!stat_is_blkdev(dv->devname, &rdev)) {
1540 if (dv->disposition == 'M')
1541 /* non-fatal. Also improbable */
1542 continue;
1543 goto abort;
1544 }
1545 if (dv->disposition == 'r')
1546 /* Be happy, the stat worked, that is
1547 * enough for --remove
1548 */
1549 ;
1550 else {
1551 if (dv->disposition == 'M')
1552 /* non-fatal */
1553 continue;
1554 pr_err("Cannot open %s: %s\n",
1555 dv->devname, strerror(open_err));
1556 goto abort;
1557 }
1558 }
1559 }
1560 switch(dv->disposition){
1561 default:
1562 pr_err("internal error - devmode[%s]=%d\n",
1563 dv->devname, dv->disposition);
1564 goto abort;
1565 case 'a':
1566 case 'S': /* --add-spare */
1567 case 'j': /* --add-journal */
1568 case 'A':
1569 case 'M': /* --re-add missing */
1570 case 'F': /* --re-add faulty */
1571 case 'c': /* --cluster-confirm */
1572 /* add the device */
1573 if (subarray) {
1574 pr_err("Cannot add disks to a \'member\' array, perform this operation on the parent container\n");
1575 goto abort;
1576 }
1577
1578 /* Let's first try to write re-add to sysfs */
1579 if (rdev != 0 &&
1580 (dv->disposition == 'A' || dv->disposition == 'F')) {
1581 sysfs_init_dev(&devinfo, rdev);
1582 if (sysfs_set_str(&info, &devinfo, "state", "re-add") == 0) {
1583 pr_err("re-add %s to %s succeed\n",
1584 dv->devname, info.sys_name);
1585 break;
1586 }
1587 }
1588
1589 if (dv->disposition == 'F')
1590 /* Need to remove first */
1591 hot_remove_disk(fd, rdev, force);
1592 /* Make sure it isn't in use (in 2.6 or later) */
1593 tfd = dev_open(dv->devname, O_RDONLY|O_EXCL);
1594 if (tfd >= 0) {
1595 /* We know no-one else is using it. We'll
1596 * need non-exclusive access to add it, so
1597 * do that now.
1598 */
1599 close(tfd);
1600 tfd = dev_open(dv->devname, O_RDONLY);
1601 }
1602 if (tfd < 0) {
1603 if (dv->disposition == 'M')
1604 continue;
1605 pr_err("Cannot open %s: %s\n",
1606 dv->devname, strerror(errno));
1607 goto abort;
1608 }
1609 if (!frozen) {
1610 if (sysfs_freeze_array(&info) == 1)
1611 frozen = 1;
1612 else
1613 frozen = -1;
1614 }
1615 rv = Manage_add(fd, tfd, dv, tst, &array,
1616 force, verbose, devname, update,
1617 rdev, array_size, raid_slot);
1618 close(tfd);
1619 tfd = -1;
1620 if (rv < 0)
1621 goto abort;
1622 if (rv > 0)
1623 count++;
1624 break;
1625
1626 case 'r':
1627 /* hot remove */
1628 if (subarray) {
1629 pr_err("Cannot remove disks from a \'member\' array, perform this operation on the parent container\n");
1630 rv = -1;
1631 } else
1632 rv = Manage_remove(tst, fd, dv, sysfd,
1633 rdev, verbose, force,
1634 devname);
1635 if (sysfd >= 0)
1636 close(sysfd);
1637 sysfd = -1;
1638 if (rv < 0)
1639 goto abort;
1640 if (rv > 0)
1641 count++;
1642 break;
1643
1644 case 'f': /* set faulty */
1645 if (!is_remove_safe(&array, fd, dv->devname, verbose)) {
1646 pr_err("Cannot remove %s from %s, array will be failed.\n",
1647 dv->devname, devname);
1648 if (sysfd >= 0)
1649 close(sysfd);
1650 goto abort;
1651 }
1652
1653 if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
1654 (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
1655 rdev))) {
1656 if (errno == EBUSY)
1657 busy = 1;
1658 pr_err("set device faulty failed for %s: %s\n",
1659 dv->devname, strerror(errno));
1660 if (sysfd >= 0)
1661 close(sysfd);
1662 goto abort;
1663 }
1664 if (sysfd >= 0)
1665 close(sysfd);
1666 sysfd = -1;
1667 count++;
1668 if (verbose >= 0)
1669 pr_err("set %s faulty in %s\n",
1670 dv->devname, devname);
1671 break;
1672 case 'R': /* Mark as replaceable */
1673 if (subarray) {
1674 pr_err("Cannot replace disks in a \'member\' array, perform this operation on the parent container\n");
1675 rv = -1;
1676 } else {
1677 if (!frozen) {
1678 if (sysfs_freeze_array(&info) == 1)
1679 frozen = 1;
1680 else
1681 frozen = -1;
1682 }
1683 rv = Manage_replace(tst, fd, dv,
1684 rdev, verbose,
1685 devname);
1686 }
1687 if (rv < 0)
1688 goto abort;
1689 if (rv > 0)
1690 count++;
1691 break;
1692 case 'W': /* --with device that doesn't match */
1693 pr_err("No matching --replace device for --with %s\n",
1694 dv->devname);
1695 goto abort;
1696 case 'w': /* --with device which was matched */
1697 rv = Manage_with(tst, fd, dv,
1698 rdev, verbose, devname);
1699 if (rv < 0)
1700 goto abort;
1701 break;
1702 }
1703 }
1704 if (frozen > 0)
1705 sysfs_set_str(&info, NULL, "sync_action","idle");
1706 if (test && count == 0)
1707 return 2;
1708 return 0;
1709
1710 abort:
1711 if (frozen > 0)
1712 sysfs_set_str(&info, NULL, "sync_action","idle");
1713 return !test && busy ? 2 : 1;
1714 }
1715
1716 int autodetect(void)
1717 {
1718 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1719 int rv = 1;
1720 int fd = dev_open("9:0", O_RDONLY);
1721 if (fd >= 0) {
1722 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
1723 rv = 0;
1724 close(fd);
1725 }
1726 return rv;
1727 }
1728
1729 int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int verbose)
1730 {
1731 struct supertype supertype, *st = &supertype;
1732 int fd, rv = 2;
1733 struct mdinfo *info = NULL;
1734
1735 memset(st, 0, sizeof(*st));
1736
1737 fd = open_subarray(dev, subarray, st, verbose < 0);
1738 if (fd < 0)
1739 return 2;
1740
1741 if (!st->ss->update_subarray) {
1742 if (verbose >= 0)
1743 pr_err("Operation not supported for %s metadata\n",
1744 st->ss->name);
1745 goto free_super;
1746 }
1747
1748 if (mdmon_running(st->devnm))
1749 st->update_tail = &st->updates;
1750
1751 info = st->ss->container_content(st, subarray);
1752
1753 if (strncmp(update, "ppl", 3) == 0 && !is_level456(info->array.level)) {
1754 pr_err("RWH policy ppl is supported only for raid4, raid5 and raid6.\n");
1755 goto free_super;
1756 }
1757
1758 rv = st->ss->update_subarray(st, subarray, update, ident);
1759
1760 if (rv) {
1761 if (verbose >= 0)
1762 pr_err("Failed to update %s of subarray-%s in %s\n",
1763 update, subarray, dev);
1764 } else if (st->update_tail)
1765 flush_metadata_updates(st);
1766 else
1767 st->ss->sync_metadata(st);
1768
1769 if (rv == 0 && strcmp(update, "name") == 0 && verbose >= 0)
1770 pr_err("Updated subarray-%s name from %s, UUIDs may have changed\n",
1771 subarray, dev);
1772
1773 free_super:
1774 if (info)
1775 free(info);
1776 st->ss->free_super(st);
1777 close(fd);
1778
1779 return rv;
1780 }
1781
1782 /* Move spare from one array to another If adding to destination array fails
1783 * add back to original array.
1784 * Returns 1 on success, 0 on failure */
1785 int move_spare(char *from_devname, char *to_devname, dev_t devid)
1786 {
1787 struct mddev_dev devlist;
1788 char devname[20];
1789
1790 /* try to remove and add */
1791 int fd1 = open(to_devname, O_RDONLY);
1792 int fd2 = open(from_devname, O_RDONLY);
1793
1794 if (fd1 < 0 || fd2 < 0) {
1795 if (fd1 >= 0)
1796 close(fd1);
1797 if (fd2 >= 0)
1798 close(fd2);
1799 return 0;
1800 }
1801
1802 devlist.next = NULL;
1803 devlist.used = 0;
1804 devlist.writemostly = FlagDefault;
1805 devlist.failfast = FlagDefault;
1806 devlist.devname = devname;
1807 sprintf(devname, "%d:%d", major(devid), minor(devid));
1808
1809 devlist.disposition = 'r';
1810 if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0) == 0) {
1811 devlist.disposition = 'a';
1812 if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0,
1813 NULL, 0) == 0) {
1814 /* make sure manager is aware of changes */
1815 ping_manager(to_devname);
1816 ping_manager(from_devname);
1817 close(fd1);
1818 close(fd2);
1819 return 1;
1820 }
1821 else
1822 Manage_subdevs(from_devname, fd2, &devlist,
1823 -1, 0, NULL, 0);
1824 }
1825 close(fd1);
1826 close(fd2);
1827 return 0;
1828 }