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