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