]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
imsm: Execute size change for external metatdata
[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 re_add_failed = 0;
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 < 1024 && 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 < 1024 && 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 skip_re_add:
855 re_add_failed = 1;
856 }
857 st->ss->free_super(st);
858 }
859 if (add_dev != dv->devname) {
860 if (verbose > 0)
861 fprintf(stderr, Name
862 ": --re-add for %s to %s is not possible\n",
863 add_dev, devname);
864 if (tfd >= 0) {
865 close(tfd);
866 tfd = -1;
867 }
868 continue;
869 }
870 if (dv->re_add) {
871 if (tfd >= 0)
872 close(tfd);
873 fprintf(stderr, Name
874 ": --re-add for %s to %s is not possible\n",
875 dv->devname, devname);
876 goto abort;
877 }
878 if (re_add_failed) {
879 fprintf(stderr, Name ": %s reports being an active member for %s, but a --re-add fails.\n",
880 dv->devname, devname);
881 fprintf(stderr, Name ": not performing --add as that would convert %s in to a spare.\n",
882 dv->devname);
883 fprintf(stderr, Name ": To make this a spare, use \"mdadm --zero-superblock %s\" first.\n",
884 dv->devname);
885 if (tfd >= 0)
886 close(tfd);
887 goto abort;
888 }
889 } else {
890 /* non-persistent. Must ensure that new drive
891 * is at least array.size big.
892 */
893 if (ldsize/512 < array_size) {
894 fprintf(stderr, Name ": %s not large enough to join array\n",
895 dv->devname);
896 if (tfd >= 0)
897 close(tfd);
898 goto abort;
899 }
900 }
901 /* committed to really trying this device now*/
902 if (tfd >= 0) {
903 remove_partitions(tfd);
904 close(tfd);
905 tfd = -1;
906 }
907 /* in 2.6.17 and earlier, version-1 superblocks won't
908 * use the number we write, but will choose a free number.
909 * we must choose the same free number, which requires
910 * starting at 'raid_disks' and counting up
911 */
912 for (j = array.raid_disks; j< tst->max_devs; j++) {
913 disc.number = j;
914 if (ioctl(fd, GET_DISK_INFO, &disc))
915 break;
916 if (disc.major==0 && disc.minor==0)
917 break;
918 if (disc.state & 8) /* removed */
919 break;
920 }
921 disc.major = major(stb.st_rdev);
922 disc.minor = minor(stb.st_rdev);
923 disc.number =j;
924 disc.state = 0;
925 if (array.not_persistent==0) {
926 int dfd;
927 if (dv->writemostly == 1)
928 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
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)) {
932 close(dfd);
933 goto abort;
934 }
935 if (tst->ss->write_init_super(tst)) {
936 close(dfd);
937 goto abort;
938 }
939 } else if (dv->re_add) {
940 /* this had better be raid1.
941 * As we are "--re-add"ing we must find a spare slot
942 * to fill.
943 */
944 char *used = malloc(array.raid_disks);
945 memset(used, 0, array.raid_disks);
946 for (j=0; j< tst->max_devs; j++) {
947 mdu_disk_info_t disc2;
948 disc2.number = j;
949 if (ioctl(fd, GET_DISK_INFO, &disc2))
950 continue;
951 if (disc2.major==0 && disc2.minor==0)
952 continue;
953 if (disc2.state & 8) /* removed */
954 continue;
955 if (disc2.raid_disk < 0)
956 continue;
957 if (disc2.raid_disk > array.raid_disks)
958 continue;
959 used[disc2.raid_disk] = 1;
960 }
961 for (j=0 ; j<array.raid_disks; j++)
962 if (!used[j]) {
963 disc.raid_disk = j;
964 disc.state |= (1<<MD_DISK_SYNC);
965 break;
966 }
967 free(used);
968 }
969 if (dv->writemostly == 1)
970 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
971 if (tst->ss->external) {
972 /* add a disk
973 * to an external metadata container */
974 struct mdinfo new_mdi;
975 struct mdinfo *sra;
976 int container_fd;
977 int devnum = fd2devnum(fd);
978 int dfd;
979
980 container_fd = open_dev_excl(devnum);
981 if (container_fd < 0) {
982 fprintf(stderr, Name ": add failed for %s:"
983 " could not get exclusive access to container\n",
984 dv->devname);
985 tst->ss->free_super(tst);
986 goto abort;
987 }
988
989 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
990 if (mdmon_running(tst->container_dev))
991 tst->update_tail = &tst->updates;
992 if (tst->ss->add_to_super(tst, &disc, dfd,
993 dv->devname)) {
994 close(dfd);
995 close(container_fd);
996 goto abort;
997 }
998 if (tst->update_tail)
999 flush_metadata_updates(tst);
1000 else
1001 tst->ss->sync_metadata(tst);
1002
1003 sra = sysfs_read(container_fd, -1, 0);
1004 if (!sra) {
1005 fprintf(stderr, Name ": add failed for %s: sysfs_read failed\n",
1006 dv->devname);
1007 close(container_fd);
1008 tst->ss->free_super(tst);
1009 goto abort;
1010 }
1011 sra->array.level = LEVEL_CONTAINER;
1012 /* Need to set data_offset and component_size */
1013 tst->ss->getinfo_super(tst, &new_mdi, NULL);
1014 new_mdi.disk.major = disc.major;
1015 new_mdi.disk.minor = disc.minor;
1016 new_mdi.recovery_start = 0;
1017 /* Make sure fds are closed as they are O_EXCL which
1018 * would block add_disk */
1019 tst->ss->free_super(tst);
1020 if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
1021 fprintf(stderr, Name ": add new device to external metadata"
1022 " failed for %s\n", dv->devname);
1023 close(container_fd);
1024 sysfs_free(sra);
1025 goto abort;
1026 }
1027 ping_monitor_by_id(devnum);
1028 sysfs_free(sra);
1029 close(container_fd);
1030 } else {
1031 tst->ss->free_super(tst);
1032 if (ioctl(fd, ADD_NEW_DISK, &disc)) {
1033 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
1034 dv->devname, j, strerror(errno));
1035 goto abort;
1036 }
1037 }
1038 if (verbose >= 0)
1039 fprintf(stderr, Name ": added %s\n", dv->devname);
1040 break;
1041
1042 case 'r':
1043 /* hot remove */
1044 if (subarray) {
1045 fprintf(stderr, Name ": Cannot remove disks from a"
1046 " \'member\' array, perform this"
1047 " operation on the parent container\n");
1048 if (sysfd >= 0)
1049 close(sysfd);
1050 goto abort;
1051 }
1052 if (tst->ss->external) {
1053 /* To remove a device from a container, we must
1054 * check that it isn't in use in an array.
1055 * This involves looking in the 'holders'
1056 * directory - there must be just one entry,
1057 * the container.
1058 * To ensure that it doesn't get used as a
1059 * hold spare while we are checking, we
1060 * get an O_EXCL open on the container
1061 */
1062 int dnum = fd2devnum(fd);
1063 lfd = open_dev_excl(dnum);
1064 if (lfd < 0) {
1065 fprintf(stderr, Name
1066 ": Cannot get exclusive access "
1067 " to container - odd\n");
1068 if (sysfd >= 0)
1069 close(sysfd);
1070 goto abort;
1071 }
1072 /* in the detached case it is not possible to
1073 * check if we are the unique holder, so just
1074 * rely on the 'detached' checks
1075 */
1076 if (strcmp(dv->devname, "detached") == 0 ||
1077 sysfd >= 0 ||
1078 sysfs_unique_holder(dnum, stb.st_rdev))
1079 /* pass */;
1080 else {
1081 fprintf(stderr, Name
1082 ": %s is %s, cannot remove.\n",
1083 dnprintable,
1084 errno == EEXIST ? "still in use":
1085 "not a member");
1086 close(lfd);
1087 goto abort;
1088 }
1089 }
1090 /* FIXME check that it is a current member */
1091 if (sysfd >= 0) {
1092 /* device has been removed and we don't know
1093 * the major:minor number
1094 */
1095 int n = write(sysfd, "remove", 6);
1096 if (n != 6)
1097 err = -1;
1098 else
1099 err = 0;
1100 close(sysfd);
1101 sysfd = -1;
1102 } else {
1103 err = ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev);
1104 if (err && errno == ENODEV) {
1105 /* Old kernels rejected this if no personality
1106 * registered */
1107 struct mdinfo *sra = sysfs_read(fd, 0, GET_DEVS);
1108 struct mdinfo *dv = NULL;
1109 if (sra)
1110 dv = sra->devs;
1111 for ( ; dv ; dv=dv->next)
1112 if (dv->disk.major == (int)major(stb.st_rdev) &&
1113 dv->disk.minor == (int)minor(stb.st_rdev))
1114 break;
1115 if (dv)
1116 err = sysfs_set_str(sra, dv,
1117 "state", "remove");
1118 else
1119 err = -1;
1120 if (sra)
1121 sysfs_free(sra);
1122 }
1123 }
1124 if (err) {
1125 fprintf(stderr, Name ": hot remove failed "
1126 "for %s: %s\n", dnprintable,
1127 strerror(errno));
1128 if (lfd >= 0)
1129 close(lfd);
1130 goto abort;
1131 }
1132 if (tst->ss->external) {
1133 /*
1134 * Before dropping our exclusive open we make an
1135 * attempt at preventing mdmon from seeing an
1136 * 'add' event before reconciling this 'remove'
1137 * event.
1138 */
1139 char *name = devnum2devname(fd2devnum(fd));
1140
1141 if (!name) {
1142 fprintf(stderr, Name ": unable to get container name\n");
1143 goto abort;
1144 }
1145
1146 ping_manager(name);
1147 free(name);
1148 }
1149 if (lfd >= 0)
1150 close(lfd);
1151 count++;
1152 if (verbose >= 0)
1153 fprintf(stderr, Name ": hot removed %s from %s\n",
1154 dnprintable, devname);
1155 break;
1156
1157 case 'f': /* set faulty */
1158 /* FIXME check current member */
1159 if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
1160 (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
1161 (unsigned long) stb.st_rdev))) {
1162 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
1163 dnprintable, strerror(errno));
1164 if (sysfd >= 0)
1165 close(sysfd);
1166 goto abort;
1167 }
1168 if (sysfd >= 0)
1169 close(sysfd);
1170 sysfd = -1;
1171 count++;
1172 if (verbose >= 0)
1173 fprintf(stderr, Name ": set %s faulty in %s\n",
1174 dnprintable, devname);
1175 break;
1176 }
1177 }
1178 if (frozen > 0)
1179 sysfs_set_str(&info, NULL, "sync_action","idle");
1180 if (test && count == 0)
1181 return 2;
1182 return 0;
1183
1184 abort:
1185 if (frozen > 0)
1186 sysfs_set_str(&info, NULL, "sync_action","idle");
1187 return 1;
1188 }
1189
1190 int autodetect(void)
1191 {
1192 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1193 int rv = 1;
1194 int fd = dev_open("9:0", O_RDONLY);
1195 if (fd >= 0) {
1196 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
1197 rv = 0;
1198 close(fd);
1199 }
1200 return rv;
1201 }
1202
1203 int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int quiet)
1204 {
1205 struct supertype supertype, *st = &supertype;
1206 int fd, rv = 2;
1207
1208 memset(st, 0, sizeof(*st));
1209
1210 fd = open_subarray(dev, subarray, st, quiet);
1211 if (fd < 0)
1212 return 2;
1213
1214 if (!st->ss->update_subarray) {
1215 if (!quiet)
1216 fprintf(stderr,
1217 Name ": Operation not supported for %s metadata\n",
1218 st->ss->name);
1219 goto free_super;
1220 }
1221
1222 if (mdmon_running(st->devnum))
1223 st->update_tail = &st->updates;
1224
1225 rv = st->ss->update_subarray(st, subarray, update, ident);
1226
1227 if (rv) {
1228 if (!quiet)
1229 fprintf(stderr, Name ": Failed to update %s of subarray-%s in %s\n",
1230 update, subarray, dev);
1231 } else if (st->update_tail)
1232 flush_metadata_updates(st);
1233 else
1234 st->ss->sync_metadata(st);
1235
1236 if (rv == 0 && strcmp(update, "name") == 0 && !quiet)
1237 fprintf(stderr,
1238 Name ": Updated subarray-%s name from %s, UUIDs may have changed\n",
1239 subarray, dev);
1240
1241 free_super:
1242 st->ss->free_super(st);
1243 close(fd);
1244
1245 return rv;
1246 }
1247
1248 /* Move spare from one array to another
1249 * If adding to destination array fails
1250 * add back to original array
1251 * Returns 1 on success, 0 on failure */
1252 int move_spare(char *from_devname, char *to_devname, dev_t devid)
1253 {
1254 struct mddev_dev devlist;
1255 char devname[20];
1256
1257 /* try to remove and add */
1258 int fd1 = open(to_devname, O_RDONLY);
1259 int fd2 = open(from_devname, O_RDONLY);
1260
1261 if (fd1 < 0 || fd2 < 0) {
1262 if (fd1>=0) close(fd1);
1263 if (fd2>=0) close(fd2);
1264 return 0;
1265 }
1266
1267 devlist.next = NULL;
1268 devlist.used = 0;
1269 devlist.re_add = 0;
1270 devlist.writemostly = 0;
1271 devlist.devname = devname;
1272 sprintf(devname, "%d:%d", major(devid), minor(devid));
1273
1274 devlist.disposition = 'r';
1275 if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0) == 0) {
1276 devlist.disposition = 'a';
1277 if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL, 0) == 0) {
1278 /* make sure manager is aware of changes */
1279 ping_manager(to_devname);
1280 ping_manager(from_devname);
1281 close(fd1);
1282 close(fd2);
1283 return 1;
1284 }
1285 else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0);
1286 }
1287 close(fd1);
1288 close(fd2);
1289 return 0;
1290 }
1291 #endif