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