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