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