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