]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
Release 3.2.6 - stability release
[thirdparty/mdadm.git] / Manage.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 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 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
51 return 1;
52 }
53 #ifndef MDASSEMBLE
54 /* If this is an externally-manage array, we need to modify the
55 * metadata_version so that mdmon doesn't undo our change.
56 */
57 mdi = sysfs_read(fd, -1, 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 fprintf(stderr, Name ": 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 fprintf(stderr, Name ": %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 fprintf(stderr, Name ": 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 fprintf(stderr, Name ": 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(int devnum, char *path)
131 {
132 /*
133 * Remove names at 'path' - possibly with
134 * partition suffixes - which link to the 'standard'
135 * name for devnum. 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 if (devnum >= 0)
150 sprintf(base, "/dev/md%d", devnum);
151 else
152 sprintf(base, "/dev/md_d%d", -1-devnum);
153 be = base + strlen(base);
154
155 path2 = malloc(strlen(path)+20);
156 strcpy(path2, path);
157 pe = path2 + strlen(path2);
158
159 for (part = 0; part < 16; part++) {
160 if (part) {
161 sprintf(be, "p%d", part);
162
163 if (isdigit(pe[-1]))
164 sprintf(pe, "p%d", part);
165 else
166 sprintf(pe, "%d", part);
167 }
168 n = readlink(path2, link, sizeof(link));
169 if (n > 0 && (int)strlen(base) == n &&
170 strncmp(link, base, n) == 0)
171 unlink(path2);
172 }
173 free(path2);
174 }
175
176
177 int Manage_runstop(char *devname, int fd, int runstop, int quiet)
178 {
179 /* Run or stop the array. array must already be configured
180 * required >= 0.90.0
181 * Only print failure messages if quiet == 0;
182 * quiet > 0 means really be quiet
183 * quiet < 0 means we will try again if it fails.
184 */
185 mdu_param_t param; /* unused */
186 int rv = 0;
187
188 if (runstop == -1 && md_get_version(fd) < 9000) {
189 if (ioctl(fd, STOP_MD, 0)) {
190 if (quiet == 0) fprintf(stderr,
191 Name ": stopping device %s "
192 "failed: %s\n",
193 devname, strerror(errno));
194 return 1;
195 }
196 }
197
198 if (md_get_version(fd) < 9000) {
199 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
200 return 1;
201 }
202 /*
203 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
204 fprintf(stderr, Name ": %s does not appear to be active.\n",
205 devname);
206 return 1;
207 }
208 */
209 if (runstop>0) {
210 if (ioctl(fd, RUN_ARRAY, &param)) {
211 fprintf(stderr, Name ": failed to run array %s: %s\n",
212 devname, strerror(errno));
213 return 1;
214 }
215 if (quiet <= 0)
216 fprintf(stderr, Name ": started %s\n", devname);
217 } else if (runstop < 0){
218 struct map_ent *map = NULL;
219 struct stat stb;
220 struct mdinfo *mdi;
221 int devnum;
222 int err;
223 int count;
224 /* If this is an mdmon managed array, just write 'inactive'
225 * to the array state and let mdmon clear up.
226 */
227 devnum = fd2devnum(fd);
228 /* Get EXCL access first. If this fails, then attempting
229 * to stop is probably a bad idea.
230 */
231 close(fd);
232 fd = open(devname, O_RDONLY|O_EXCL);
233 if (fd < 0 || fd2devnum(fd) != devnum) {
234 if (fd >= 0)
235 close(fd);
236 fprintf(stderr,
237 Name ": Cannot get exclusive access to %s:"
238 "Perhaps a running "
239 "process, mounted filesystem "
240 "or active volume group?\n",
241 devname);
242 return 1;
243 }
244 mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
245 if (mdi &&
246 mdi->array.level > 0 &&
247 is_subarray(mdi->text_version)) {
248 int err;
249 /* This is mdmon managed. */
250 close(fd);
251
252 count = 25;
253 while (count &&
254 (err = sysfs_set_str(mdi, NULL,
255 "array_state",
256 "inactive")) < 0
257 && errno == EBUSY) {
258 usleep(200000);
259 count--;
260 }
261 if (err && !quiet) {
262 fprintf(stderr, Name
263 ": failed to stop array %s: %s\n",
264 devname, strerror(errno));
265 rv = 1;
266 goto out;
267 }
268
269 /* Give monitor a chance to act */
270 ping_monitor(mdi->text_version);
271
272 fd = open_dev_excl(devnum);
273 if (fd < 0) {
274 fprintf(stderr, Name
275 ": failed to completely stop %s"
276 ": Device is busy\n",
277 devname);
278 rv = 1;
279 goto out;
280 }
281 } else if (mdi &&
282 mdi->array.major_version == -1 &&
283 mdi->array.minor_version == -2 &&
284 !is_subarray(mdi->text_version)) {
285 struct mdstat_ent *mds, *m;
286 /* container, possibly mdmon-managed.
287 * Make sure mdmon isn't opening it, which
288 * would interfere with the 'stop'
289 */
290 ping_monitor(mdi->sys_name);
291
292 /* now check that there are no existing arrays
293 * which are members of this array
294 */
295 mds = mdstat_read(0, 0);
296 for (m=mds; m; m=m->next)
297 if (m->metadata_version &&
298 strncmp(m->metadata_version, "external:", 9)==0 &&
299 is_subarray(m->metadata_version+9) &&
300 devname2devnum(m->metadata_version+10) == devnum) {
301 if (!quiet)
302 fprintf(stderr, Name
303 ": Cannot stop container %s: "
304 "member %s still active\n",
305 devname, m->dev);
306 free_mdstat(mds);
307 rv = 1;
308 goto out;
309 }
310 }
311
312 /* As we have an O_EXCL open, any use of the device
313 * which blocks STOP_ARRAY is probably a transient use,
314 * so it is reasonable to retry for a while - 5 seconds.
315 */
316 count = 25; err = 0;
317 while (count && fd >= 0
318 && (err = ioctl(fd, STOP_ARRAY, NULL)) < 0
319 && errno == EBUSY) {
320 usleep(200000);
321 count --;
322 }
323 if (fd >= 0 && err) {
324 if (quiet == 0) {
325 fprintf(stderr, Name
326 ": failed to stop array %s: %s\n",
327 devname, strerror(errno));
328 if (errno == EBUSY)
329 fprintf(stderr, "Perhaps a running "
330 "process, mounted filesystem "
331 "or active volume group?\n");
332 }
333 rv = 1;
334 goto out;
335 }
336 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
337 * was stopped, so We'll do it here just to be sure. Drop any
338 * partitions as well...
339 */
340 if (fd >= 0)
341 ioctl(fd, BLKRRPART, 0);
342 if (mdi)
343 sysfs_uevent(mdi, "change");
344
345
346 if (devnum != NoMdDev &&
347 (stat("/dev/.udev", &stb) != 0 ||
348 check_env("MDADM_NO_UDEV"))) {
349 struct map_ent *mp = map_by_devnum(&map, devnum);
350 remove_devices(devnum, mp ? mp->path : NULL);
351 }
352
353
354 if (quiet <= 0)
355 fprintf(stderr, Name ": stopped %s\n", devname);
356 map_lock(&map);
357 map_remove(&map, devnum);
358 map_unlock(&map);
359 out:
360 if (mdi)
361 sysfs_free(mdi);
362 }
363 return rv;
364 }
365
366 int Manage_resize(char *devname, int fd, long long size, int raid_disks)
367 {
368 mdu_array_info_t info;
369 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
370 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
371 devname, strerror(errno));
372 return 1;
373 }
374 if (size >= 0)
375 info.size = size;
376 if (raid_disks > 0)
377 info.raid_disks = raid_disks;
378 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
379 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
380 devname, strerror(errno));
381 return 1;
382 }
383 return 0;
384 }
385
386 int Manage_subdevs(char *devname, int fd,
387 struct mddev_dev *devlist, int verbose, int test,
388 char *update, int force)
389 {
390 /* do something to each dev.
391 * devmode can be
392 * 'a' - add the device
393 * try HOT_ADD_DISK
394 * If that fails EINVAL, try ADD_NEW_DISK
395 * 'r' - remove the device HOT_REMOVE_DISK
396 * device can be 'faulty' or 'detached' in which case all
397 * matching devices are removed.
398 * 'f' - set the device faulty SET_DISK_FAULTY
399 * device can be 'detached' in which case any device that
400 * is inaccessible will be marked faulty.
401 * For 'f' and 'r', the device can also be a kernel-internal
402 * name such as 'sdb'.
403 */
404 struct mddev_dev *add_devlist = NULL;
405 mdu_array_info_t array;
406 mdu_disk_info_t disc;
407 unsigned long long array_size;
408 struct mddev_dev *dv, *next = NULL;
409 struct stat stb;
410 int j, jnext = 0;
411 int tfd = -1;
412 struct supertype *st, *tst;
413 char *subarray = NULL;
414 int duuid[4];
415 int ouuid[4];
416 int lfd = -1;
417 int sysfd = -1;
418 int count = 0; /* number of actions taken */
419 struct mdinfo info;
420 int frozen = 0;
421
422 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
423 fprintf(stderr, Name ": cannot get array info for %s\n",
424 devname);
425 goto abort;
426 }
427 sysfs_init(&info, fd, 0);
428
429 /* array.size is only 32 bit and may be truncated.
430 * So read from sysfs if possible, and record number of sectors
431 */
432
433 array_size = get_component_size(fd);
434 if (array_size <= 0)
435 array_size = array.size * 2;
436
437 tst = super_by_fd(fd, &subarray);
438 if (!tst) {
439 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
440 array.major_version, array.minor_version);
441 goto abort;
442 }
443
444 stb.st_rdev = 0;
445 for (dv = devlist, j=0 ; dv; dv = next, j = jnext) {
446 unsigned long long ldsize;
447 char dvname[20];
448 char *dnprintable = dv->devname;
449 char *add_dev = dv->devname;
450 int err;
451 int array_failed;
452
453 next = dv->next;
454 jnext = 0;
455
456 if (strcmp(dv->devname, "failed")==0 ||
457 strcmp(dv->devname, "faulty")==0) {
458 int remaining_disks = array.nr_disks;
459 if (dv->disposition != 'r') {
460 fprintf(stderr, Name ": %s only meaningful "
461 "with -r, not -%c\n",
462 dv->devname, dv->disposition);
463 goto abort;
464 }
465 for (; j < MAX_DISKS && remaining_disks > 0; j++) {
466 unsigned dev;
467 disc.number = j;
468 if (ioctl(fd, GET_DISK_INFO, &disc))
469 continue;
470 if (disc.major == 0 && disc.minor == 0)
471 continue;
472 remaining_disks --;
473 if ((disc.state & 1) == 0) /* faulty */
474 continue;
475 dev = makedev(disc.major, disc.minor);
476 if (stb.st_rdev == dev)
477 /* already did that one */
478 continue;
479 stb.st_rdev = dev;
480 next = dv;
481 /* same slot again next time - things might
482 * have reshuffled */
483 jnext = j;
484 sprintf(dvname,"%d:%d", disc.major, disc.minor);
485 dnprintable = dvname;
486 break;
487 }
488 if (next != dv)
489 continue;
490 } else if (strcmp(dv->devname, "detached") == 0) {
491 int remaining_disks = array.nr_disks;
492 if (dv->disposition != 'r' && dv->disposition != 'f') {
493 fprintf(stderr, Name ": %s only meaningful "
494 "with -r of -f, not -%c\n",
495 dv->devname, dv->disposition);
496 goto abort;
497 }
498 for (; j < MAX_DISKS && remaining_disks > 0; j++) {
499 int sfd;
500 unsigned dev;
501 disc.number = j;
502 if (ioctl(fd, GET_DISK_INFO, &disc))
503 continue;
504 if (disc.major == 0 && disc.minor == 0)
505 continue;
506 remaining_disks --;
507 sprintf(dvname,"%d:%d", disc.major, disc.minor);
508 sfd = dev_open(dvname, O_RDONLY);
509 if (sfd >= 0) {
510 close(sfd);
511 continue;
512 }
513 if (dv->disposition == 'f' &&
514 (disc.state & 1) == 1) /* already faulty */
515 continue;
516 if (errno != ENXIO)
517 continue;
518 dev = makedev(disc.major, disc.minor);
519 if (stb.st_rdev == dev)
520 /* already did that one */
521 continue;
522 stb.st_rdev = dev;
523 next = dv;
524 /* same slot again next time - things might
525 * have reshuffled */
526 jnext = j;
527 dnprintable = dvname;
528 break;
529 }
530 if (next != dv)
531 continue;
532 } else if (strcmp(dv->devname, "missing") == 0) {
533 if (dv->disposition != 'a' || dv->re_add == 0) {
534 fprintf(stderr, Name ": 'missing' only meaningful "
535 "with --re-add\n");
536 goto abort;
537 }
538 if (add_devlist == NULL)
539 add_devlist = conf_get_devs();
540 if (add_devlist == NULL) {
541 fprintf(stderr, Name ": no devices to scan for missing members.");
542 continue;
543 }
544 add_dev = add_devlist->devname;
545 add_devlist = add_devlist->next;
546 if (add_devlist != NULL)
547 next = dv;
548 if (stat(add_dev, &stb) < 0)
549 continue;
550 } else if (strchr(dv->devname, '/') == NULL &&
551 strchr(dv->devname, ':') == NULL &&
552 strlen(dv->devname) < 50) {
553 /* Assume this is a kernel-internal name like 'sda1' */
554 int found = 0;
555 char dname[55];
556 if (dv->disposition != 'r' && dv->disposition != 'f') {
557 fprintf(stderr, Name ": %s only meaningful "
558 "with -r or -f, not -%c\n",
559 dv->devname, dv->disposition);
560 goto abort;
561 }
562
563 sprintf(dname, "dev-%s", dv->devname);
564 sysfd = sysfs_open(fd2devnum(fd), dname, "block/dev");
565 if (sysfd >= 0) {
566 char dn[20];
567 int mj,mn;
568 if (sysfs_fd_get_str(sysfd, dn, 20) > 0 &&
569 sscanf(dn, "%d:%d", &mj,&mn) == 2) {
570 stb.st_rdev = makedev(mj,mn);
571 found = 1;
572 }
573 close(sysfd);
574 sysfd = -1;
575 }
576 if (!found) {
577 sysfd = sysfs_open(fd2devnum(fd), dname, "state");
578 if (sysfd < 0) {
579 fprintf(stderr, Name ": %s does not appear "
580 "to be a component of %s\n",
581 dv->devname, devname);
582 goto abort;
583 }
584 }
585 } else {
586 j = 0;
587
588 tfd = dev_open(dv->devname, O_RDONLY);
589 if (tfd < 0 && dv->disposition == 'r' &&
590 lstat(dv->devname, &stb) == 0)
591 /* Be happy, the lstat worked, that is
592 * enough for --remove
593 */
594 ;
595 else {
596 if (tfd < 0 || fstat(tfd, &stb) != 0) {
597 fprintf(stderr, Name ": cannot find %s: %s\n",
598 dv->devname, strerror(errno));
599 if (tfd >= 0)
600 close(tfd);
601 goto abort;
602 }
603 close(tfd);
604 tfd = -1;
605 }
606 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
607 fprintf(stderr, Name ": %s is not a "
608 "block device.\n",
609 dv->devname);
610 goto abort;
611 }
612 }
613 switch(dv->disposition){
614 default:
615 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
616 dv->devname, dv->disposition);
617 goto abort;
618 case 'a':
619 /* add the device */
620 if (subarray) {
621 fprintf(stderr, Name ": Cannot add disks to a"
622 " \'member\' array, perform this"
623 " operation on the parent container\n");
624 goto abort;
625 }
626 /* Make sure it isn't in use (in 2.6 or later) */
627 tfd = dev_open(add_dev, O_RDONLY|O_EXCL|O_DIRECT);
628 if (tfd < 0 && add_dev != dv->devname)
629 continue;
630 if (tfd < 0) {
631 fprintf(stderr, Name ": Cannot open %s: %s\n",
632 dv->devname, strerror(errno));
633 goto abort;
634 }
635 if (!frozen) {
636 if (sysfs_freeze_array(&info) == 1)
637 frozen = 1;
638 else
639 frozen = -1;
640 }
641
642 st = dup_super(tst);
643
644 if (array.not_persistent==0)
645 st->ss->load_super(st, tfd, NULL);
646
647 if (add_dev == dv->devname) {
648 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
649 st->ss->free_super(st);
650 close(tfd);
651 goto abort;
652 }
653 } else if (!get_dev_size(tfd, NULL, &ldsize)) {
654 st->ss->free_super(st);
655 close(tfd);
656 tfd = -1;
657 continue;
658 }
659
660 if (tst->ss->validate_geometry(
661 tst, array.level, array.layout,
662 array.raid_disks, NULL,
663 ldsize >> 9, NULL, NULL, 0) == 0) {
664 if (!force) {
665 fprintf(stderr, Name
666 ": %s is larger than %s can "
667 "effectively use.\n"
668 " Add --force is you "
669 "really want to add this device.\n",
670 add_dev, devname);
671 st->ss->free_super(st);
672 close(tfd);
673 goto abort;
674 }
675 fprintf(stderr, Name
676 ": %s is larger than %s can "
677 "effectively use.\n"
678 " Adding anyway as --force "
679 "was given.\n",
680 add_dev, devname);
681 }
682 if (!tst->ss->external &&
683 array.major_version == 0 &&
684 md_get_version(fd)%100 < 2) {
685 close(tfd);
686 st->ss->free_super(st);
687 tfd = -1;
688 if (ioctl(fd, HOT_ADD_DISK,
689 (unsigned long)stb.st_rdev)==0) {
690 if (verbose >= 0)
691 fprintf(stderr, Name ": hot added %s\n",
692 add_dev);
693 continue;
694 }
695
696 fprintf(stderr, Name ": hot add failed for %s: %s\n",
697 add_dev, strerror(errno));
698 goto abort;
699 }
700
701 if (array.not_persistent == 0 || tst->ss->external) {
702
703 /* need to find a sample superblock to copy, and
704 * a spare slot to use.
705 * For 'external' array (well, container based),
706 * We can just load the metadata for the array.
707 */
708 if (tst->sb)
709 /* already loaded */;
710 else if (tst->ss->external) {
711 tst->ss->load_container(tst, fd, NULL);
712 } else for (j = 0; j < tst->max_devs; j++) {
713 char *dev;
714 int dfd;
715 disc.number = j;
716 if (ioctl(fd, GET_DISK_INFO, &disc))
717 continue;
718 if (disc.major==0 && disc.minor==0)
719 continue;
720 if ((disc.state & 4)==0) continue; /* sync */
721 /* Looks like a good device to try */
722 dev = map_dev(disc.major, disc.minor, 1);
723 if (!dev) continue;
724 dfd = dev_open(dev, O_RDONLY);
725 if (dfd < 0) continue;
726 if (tst->ss->load_super(tst, dfd,
727 NULL)) {
728 close(dfd);
729 continue;
730 }
731 close(dfd);
732 break;
733 }
734 /* FIXME this is a bad test to be using */
735 if (!tst->sb &&
736 dv->re_add) {
737 /* we are re-adding a device to a
738 * completely dead array - have to depend
739 * on kernel to check
740 */
741 } else if (!tst->sb) {
742 close(tfd);
743 st->ss->free_super(st);
744 fprintf(stderr, Name ": cannot load array metadata from %s\n", devname);
745 goto abort;
746 }
747
748 /* Make sure device is large enough */
749 if (tst->ss->avail_size(tst, ldsize/512) <
750 array_size) {
751 close(tfd);
752 tfd = -1;
753 st->ss->free_super(st);
754 if (add_dev != dv->devname)
755 continue;
756 fprintf(stderr, Name ": %s not large enough to join array\n",
757 dv->devname);
758 goto abort;
759 }
760
761 /* Possibly this device was recently part of the array
762 * and was temporarily removed, and is now being re-added.
763 * If so, we can simply re-add it.
764 */
765
766 if (st->sb) {
767 struct mdinfo mdi;
768 st->ss->getinfo_super(st, &mdi, NULL);
769 st->ss->uuid_from_super(st, ouuid);
770 if (tst->sb)
771 tst->ss->uuid_from_super(tst, duuid);
772 else
773 /* Assume uuid matches: kernel will check */
774 memcpy(duuid, ouuid, sizeof(ouuid));
775 if ((mdi.disk.state & (1<<MD_DISK_ACTIVE)) &&
776 !(mdi.disk.state & (1<<MD_DISK_FAULTY)) &&
777 memcmp(duuid, ouuid, sizeof(ouuid))==0) {
778 /* look like it is worth a try. Need to
779 * make sure kernel will accept it though.
780 */
781 /* re-add doesn't work for version-1 superblocks
782 * before 2.6.18 :-(
783 */
784 if (array.major_version == 1 &&
785 get_linux_version() <= 2006018)
786 goto skip_re_add;
787 disc.number = mdi.disk.number;
788 if (ioctl(fd, GET_DISK_INFO, &disc) != 0
789 || disc.major != 0 || disc.minor != 0
790 )
791 goto skip_re_add;
792 disc.major = major(stb.st_rdev);
793 disc.minor = minor(stb.st_rdev);
794 disc.number = mdi.disk.number;
795 disc.raid_disk = mdi.disk.raid_disk;
796 disc.state = mdi.disk.state;
797 if (dv->writemostly == 1)
798 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
799 if (dv->writemostly == 2)
800 disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
801 remove_partitions(tfd);
802 close(tfd);
803 tfd = -1;
804 if (update || dv->writemostly > 0) {
805 int rv = -1;
806 tfd = dev_open(dv->devname, O_RDWR);
807 if (tfd < 0) {
808 fprintf(stderr, Name ": failed to open %s for"
809 " superblock update during re-add\n", dv->devname);
810 st->ss->free_super(st);
811 goto abort;
812 }
813
814 if (dv->writemostly == 1)
815 rv = st->ss->update_super(
816 st, NULL, "writemostly",
817 devname, verbose, 0, NULL);
818 if (dv->writemostly == 2)
819 rv = st->ss->update_super(
820 st, NULL, "readwrite",
821 devname, verbose, 0, NULL);
822 if (update)
823 rv = st->ss->update_super(
824 st, NULL, update,
825 devname, verbose, 0, NULL);
826 if (rv == 0)
827 rv = st->ss->store_super(st, tfd);
828 close(tfd);
829 tfd = -1;
830 if (rv != 0) {
831 fprintf(stderr, Name ": failed to update"
832 " superblock during re-add\n");
833 st->ss->free_super(st);
834 goto abort;
835 }
836 }
837 /* don't even try if disk is marked as faulty */
838 errno = 0;
839 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
840 if (verbose >= 0)
841 fprintf(stderr, Name ": re-added %s\n", add_dev);
842 count++;
843 st->ss->free_super(st);
844 continue;
845 }
846 if (errno == ENOMEM || errno == EROFS) {
847 fprintf(stderr, Name ": add new device failed for %s: %s\n",
848 add_dev, strerror(errno));
849 st->ss->free_super(st);
850 if (add_dev != dv->devname)
851 continue;
852 goto abort;
853 }
854 }
855 skip_re_add:
856 st->ss->free_super(st);
857 }
858 if (add_dev != dv->devname) {
859 if (verbose > 0)
860 fprintf(stderr, Name
861 ": --re-add for %s to %s is not possible\n",
862 add_dev, devname);
863 if (tfd >= 0) {
864 close(tfd);
865 tfd = -1;
866 }
867 continue;
868 }
869 if (dv->re_add) {
870 if (tfd >= 0)
871 close(tfd);
872 fprintf(stderr, Name
873 ": --re-add for %s to %s is not possible\n",
874 dv->devname, devname);
875 goto abort;
876 }
877 if (array.active_disks < array.raid_disks) {
878 char *avail = calloc(array.raid_disks, 1);
879 int d;
880 int found = 0;
881
882 for (d = 0; d < MAX_DISKS && found < array.active_disks; d++) {
883 disc.number = d;
884 if (ioctl(fd, GET_DISK_INFO, &disc))
885 continue;
886 if (disc.major == 0 && disc.minor == 0)
887 continue;
888 if (!(disc.state & (1<<MD_DISK_SYNC)))
889 continue;
890 avail[disc.raid_disk] = 1;
891 found++;
892 }
893 array_failed = !enough(array.level, array.raid_disks,
894 array.layout, 1, avail);
895 } else
896 array_failed = 0;
897 if (array_failed) {
898 fprintf(stderr, Name ": %s has failed so using --add cannot work and might destroy\n",
899 devname);
900 fprintf(stderr, Name ": data on %s. You should stop the array and re-assemble it.\n",
901 dv->devname);
902 if (tfd >= 0)
903 close(tfd);
904 goto abort;
905 }
906 } else {
907 /* non-persistent. Must ensure that new drive
908 * is at least array.size big.
909 */
910 if (ldsize/512 < array_size) {
911 fprintf(stderr, Name ": %s not large enough to join array\n",
912 dv->devname);
913 if (tfd >= 0)
914 close(tfd);
915 goto abort;
916 }
917 }
918 /* committed to really trying this device now*/
919 if (tfd >= 0) {
920 remove_partitions(tfd);
921 close(tfd);
922 tfd = -1;
923 }
924 /* in 2.6.17 and earlier, version-1 superblocks won't
925 * use the number we write, but will choose a free number.
926 * we must choose the same free number, which requires
927 * starting at 'raid_disks' and counting up
928 */
929 for (j = array.raid_disks; j< tst->max_devs; j++) {
930 disc.number = j;
931 if (ioctl(fd, GET_DISK_INFO, &disc))
932 break;
933 if (disc.major==0 && disc.minor==0)
934 break;
935 if (disc.state & 8) /* removed */
936 break;
937 }
938 disc.major = major(stb.st_rdev);
939 disc.minor = minor(stb.st_rdev);
940 disc.number =j;
941 disc.state = 0;
942 if (array.not_persistent==0) {
943 int dfd;
944 if (dv->writemostly == 1)
945 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
946 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
947 if (tst->ss->add_to_super(tst, &disc, dfd,
948 dv->devname)) {
949 close(dfd);
950 goto abort;
951 }
952 if (tst->ss->write_init_super(tst)) {
953 close(dfd);
954 goto abort;
955 }
956 } else if (dv->re_add) {
957 /* this had better be raid1.
958 * As we are "--re-add"ing we must find a spare slot
959 * to fill.
960 */
961 char *used = malloc(array.raid_disks);
962 memset(used, 0, array.raid_disks);
963 for (j=0; j< tst->max_devs; j++) {
964 mdu_disk_info_t disc2;
965 disc2.number = j;
966 if (ioctl(fd, GET_DISK_INFO, &disc2))
967 continue;
968 if (disc2.major==0 && disc2.minor==0)
969 continue;
970 if (disc2.state & 8) /* removed */
971 continue;
972 if (disc2.raid_disk < 0)
973 continue;
974 if (disc2.raid_disk > array.raid_disks)
975 continue;
976 used[disc2.raid_disk] = 1;
977 }
978 for (j=0 ; j<array.raid_disks; j++)
979 if (!used[j]) {
980 disc.raid_disk = j;
981 disc.state |= (1<<MD_DISK_SYNC);
982 break;
983 }
984 free(used);
985 }
986 if (dv->writemostly == 1)
987 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
988 if (tst->ss->external) {
989 /* add a disk
990 * to an external metadata container */
991 struct mdinfo new_mdi;
992 struct mdinfo *sra;
993 int container_fd;
994 int devnum = fd2devnum(fd);
995 int dfd;
996
997 container_fd = open_dev_excl(devnum);
998 if (container_fd < 0) {
999 fprintf(stderr, Name ": add failed for %s:"
1000 " could not get exclusive access to container\n",
1001 dv->devname);
1002 tst->ss->free_super(tst);
1003 goto abort;
1004 }
1005
1006 Kill(dv->devname, NULL, 0, -1, 0);
1007 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
1008 if (mdmon_running(tst->container_dev))
1009 tst->update_tail = &tst->updates;
1010 if (tst->ss->add_to_super(tst, &disc, dfd,
1011 dv->devname)) {
1012 close(dfd);
1013 close(container_fd);
1014 goto abort;
1015 }
1016 if (tst->update_tail)
1017 flush_metadata_updates(tst);
1018 else
1019 tst->ss->sync_metadata(tst);
1020
1021 sra = sysfs_read(container_fd, -1, 0);
1022 if (!sra) {
1023 fprintf(stderr, Name ": add failed for %s: sysfs_read failed\n",
1024 dv->devname);
1025 close(container_fd);
1026 tst->ss->free_super(tst);
1027 goto abort;
1028 }
1029 sra->array.level = LEVEL_CONTAINER;
1030 /* Need to set data_offset and component_size */
1031 tst->ss->getinfo_super(tst, &new_mdi, NULL);
1032 new_mdi.disk.major = disc.major;
1033 new_mdi.disk.minor = disc.minor;
1034 new_mdi.recovery_start = 0;
1035 /* Make sure fds are closed as they are O_EXCL which
1036 * would block add_disk */
1037 tst->ss->free_super(tst);
1038 if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
1039 fprintf(stderr, Name ": add new device to external metadata"
1040 " failed for %s\n", dv->devname);
1041 close(container_fd);
1042 sysfs_free(sra);
1043 goto abort;
1044 }
1045 ping_monitor_by_id(devnum);
1046 sysfs_free(sra);
1047 close(container_fd);
1048 } else {
1049 tst->ss->free_super(tst);
1050 if (ioctl(fd, ADD_NEW_DISK, &disc)) {
1051 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
1052 dv->devname, j, strerror(errno));
1053 goto abort;
1054 }
1055 }
1056 if (verbose >= 0)
1057 fprintf(stderr, Name ": added %s\n", dv->devname);
1058 break;
1059
1060 case 'r':
1061 /* hot remove */
1062 if (subarray) {
1063 fprintf(stderr, Name ": Cannot remove disks from a"
1064 " \'member\' array, perform this"
1065 " operation on the parent container\n");
1066 if (sysfd >= 0)
1067 close(sysfd);
1068 goto abort;
1069 }
1070 if (tst->ss->external) {
1071 /* To remove a device from a container, we must
1072 * check that it isn't in use in an array.
1073 * This involves looking in the 'holders'
1074 * directory - there must be just one entry,
1075 * the container.
1076 * To ensure that it doesn't get used as a
1077 * hold spare while we are checking, we
1078 * get an O_EXCL open on the container
1079 */
1080 int dnum = fd2devnum(fd);
1081 lfd = open_dev_excl(dnum);
1082 if (lfd < 0) {
1083 fprintf(stderr, Name
1084 ": Cannot get exclusive access "
1085 " to container - odd\n");
1086 if (sysfd >= 0)
1087 close(sysfd);
1088 goto abort;
1089 }
1090 /* in the detached case it is not possible to
1091 * check if we are the unique holder, so just
1092 * rely on the 'detached' checks
1093 */
1094 if (strcmp(dv->devname, "detached") == 0 ||
1095 sysfd >= 0 ||
1096 sysfs_unique_holder(dnum, stb.st_rdev))
1097 /* pass */;
1098 else {
1099 fprintf(stderr, Name
1100 ": %s is %s, cannot remove.\n",
1101 dnprintable,
1102 errno == EEXIST ? "still in use":
1103 "not a member");
1104 close(lfd);
1105 goto abort;
1106 }
1107 }
1108 /* FIXME check that it is a current member */
1109 if (sysfd >= 0) {
1110 /* device has been removed and we don't know
1111 * the major:minor number
1112 */
1113 int n = write(sysfd, "remove", 6);
1114 if (n != 6)
1115 err = -1;
1116 else
1117 err = 0;
1118 close(sysfd);
1119 sysfd = -1;
1120 } else {
1121 err = ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev);
1122 if (err && errno == ENODEV) {
1123 /* Old kernels rejected this if no personality
1124 * registered */
1125 struct mdinfo *sra = sysfs_read(fd, 0, GET_DEVS);
1126 struct mdinfo *dv = NULL;
1127 if (sra)
1128 dv = sra->devs;
1129 for ( ; dv ; dv=dv->next)
1130 if (dv->disk.major == (int)major(stb.st_rdev) &&
1131 dv->disk.minor == (int)minor(stb.st_rdev))
1132 break;
1133 if (dv)
1134 err = sysfs_set_str(sra, dv,
1135 "state", "remove");
1136 else
1137 err = -1;
1138 if (sra)
1139 sysfs_free(sra);
1140 }
1141 }
1142 if (err) {
1143 fprintf(stderr, Name ": hot remove failed "
1144 "for %s: %s\n", dnprintable,
1145 strerror(errno));
1146 if (lfd >= 0)
1147 close(lfd);
1148 goto abort;
1149 }
1150 if (tst->ss->external) {
1151 /*
1152 * Before dropping our exclusive open we make an
1153 * attempt at preventing mdmon from seeing an
1154 * 'add' event before reconciling this 'remove'
1155 * event.
1156 */
1157 char *name = devnum2devname(fd2devnum(fd));
1158
1159 if (!name) {
1160 fprintf(stderr, Name ": unable to get container name\n");
1161 goto abort;
1162 }
1163
1164 ping_manager(name);
1165 free(name);
1166 }
1167 if (lfd >= 0)
1168 close(lfd);
1169 count++;
1170 if (verbose >= 0)
1171 fprintf(stderr, Name ": hot removed %s from %s\n",
1172 dnprintable, devname);
1173 break;
1174
1175 case 'f': /* set faulty */
1176 /* FIXME check current member */
1177 if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
1178 (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
1179 (unsigned long) stb.st_rdev))) {
1180 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
1181 dnprintable, strerror(errno));
1182 if (sysfd >= 0)
1183 close(sysfd);
1184 goto abort;
1185 }
1186 if (sysfd >= 0)
1187 close(sysfd);
1188 sysfd = -1;
1189 count++;
1190 if (verbose >= 0)
1191 fprintf(stderr, Name ": set %s faulty in %s\n",
1192 dnprintable, devname);
1193 break;
1194 }
1195 }
1196 if (frozen > 0)
1197 sysfs_set_str(&info, NULL, "sync_action","idle");
1198 if (test && count == 0)
1199 return 2;
1200 return 0;
1201
1202 abort:
1203 if (frozen > 0)
1204 sysfs_set_str(&info, NULL, "sync_action","idle");
1205 return 1;
1206 }
1207
1208 int autodetect(void)
1209 {
1210 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1211 int rv = 1;
1212 int fd = dev_open("9:0", O_RDONLY);
1213 if (fd >= 0) {
1214 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
1215 rv = 0;
1216 close(fd);
1217 }
1218 return rv;
1219 }
1220
1221 int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int quiet)
1222 {
1223 struct supertype supertype, *st = &supertype;
1224 int fd, rv = 2;
1225
1226 memset(st, 0, sizeof(*st));
1227
1228 fd = open_subarray(dev, subarray, st, quiet);
1229 if (fd < 0)
1230 return 2;
1231
1232 if (!st->ss->update_subarray) {
1233 if (!quiet)
1234 fprintf(stderr,
1235 Name ": Operation not supported for %s metadata\n",
1236 st->ss->name);
1237 goto free_super;
1238 }
1239
1240 if (mdmon_running(st->devnum))
1241 st->update_tail = &st->updates;
1242
1243 rv = st->ss->update_subarray(st, subarray, update, ident);
1244
1245 if (rv) {
1246 if (!quiet)
1247 fprintf(stderr, Name ": Failed to update %s of subarray-%s in %s\n",
1248 update, subarray, dev);
1249 } else if (st->update_tail)
1250 flush_metadata_updates(st);
1251 else
1252 st->ss->sync_metadata(st);
1253
1254 if (rv == 0 && strcmp(update, "name") == 0 && !quiet)
1255 fprintf(stderr,
1256 Name ": Updated subarray-%s name from %s, UUIDs may have changed\n",
1257 subarray, dev);
1258
1259 free_super:
1260 st->ss->free_super(st);
1261 close(fd);
1262
1263 return rv;
1264 }
1265
1266 /* Move spare from one array to another
1267 * If adding to destination array fails
1268 * add back to original array
1269 * Returns 1 on success, 0 on failure */
1270 int move_spare(char *from_devname, char *to_devname, dev_t devid)
1271 {
1272 struct mddev_dev devlist;
1273 char devname[20];
1274
1275 /* try to remove and add */
1276 int fd1 = open(to_devname, O_RDONLY);
1277 int fd2 = open(from_devname, O_RDONLY);
1278
1279 if (fd1 < 0 || fd2 < 0) {
1280 if (fd1>=0) close(fd1);
1281 if (fd2>=0) close(fd2);
1282 return 0;
1283 }
1284
1285 devlist.next = NULL;
1286 devlist.used = 0;
1287 devlist.re_add = 0;
1288 devlist.writemostly = 0;
1289 devlist.devname = devname;
1290 sprintf(devname, "%d:%d", major(devid), minor(devid));
1291
1292 devlist.disposition = 'r';
1293 if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0) == 0) {
1294 devlist.disposition = 'a';
1295 if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL, 0) == 0) {
1296 /* make sure manager is aware of changes */
1297 ping_manager(to_devname);
1298 ping_manager(from_devname);
1299 close(fd1);
1300 close(fd2);
1301 return 1;
1302 }
1303 else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0);
1304 }
1305 close(fd1);
1306 close(fd2);
1307 return 0;
1308 }
1309 #endif