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