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