]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
Skip clustered devices in incremental
[thirdparty/mdadm.git] / Manage.c
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
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, 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
193 int 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);
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 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 }
686 skip_re_add:
687 return 0;
688 }
689
690 int 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 int raid_slot)
695 {
696 unsigned long long ldsize;
697 struct supertype *dev_st = NULL;
698 int j;
699 mdu_disk_info_t disc;
700
701 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
702 if (dv->disposition == 'M')
703 return 0;
704 else
705 return -1;
706 }
707
708 if (tst->ss == &super0 && ldsize > 4ULL*1024*1024*1024*1024) {
709 /* More than 4TB is wasted on v0.90 */
710 if (!force) {
711 pr_err("%s is larger than %s can effectively use.\n"
712 " Add --force is you really want to add this device.\n",
713 dv->devname, devname);
714 return -1;
715 }
716 pr_err("%s is larger than %s can effectively use.\n"
717 " Adding anyway as --force was given.\n",
718 dv->devname, devname);
719 }
720 if (!tst->ss->external &&
721 array->major_version == 0 &&
722 md_get_version(fd)%100 < 2) {
723 if (ioctl(fd, HOT_ADD_DISK, rdev)==0) {
724 if (verbose >= 0)
725 pr_err("hot added %s\n",
726 dv->devname);
727 return 1;
728 }
729
730 pr_err("hot add failed for %s: %s\n",
731 dv->devname, strerror(errno));
732 return -1;
733 }
734
735 if (array->not_persistent == 0 || tst->ss->external) {
736
737 /* need to find a sample superblock to copy, and
738 * a spare slot to use.
739 * For 'external' array (well, container based),
740 * We can just load the metadata for the array->
741 */
742 int array_failed;
743 if (tst->sb)
744 /* already loaded */;
745 else if (tst->ss->external) {
746 tst->ss->load_container(tst, fd, NULL);
747 } else for (j = 0; j < tst->max_devs; j++) {
748 char *dev;
749 int dfd;
750 disc.number = j;
751 if (ioctl(fd, GET_DISK_INFO, &disc))
752 continue;
753 if (disc.major==0 && disc.minor==0)
754 continue;
755 if ((disc.state & 4)==0) /* sync */
756 continue;
757 /* Looks like a good device to try */
758 dev = map_dev(disc.major, disc.minor, 1);
759 if (!dev)
760 continue;
761 dfd = dev_open(dev, O_RDONLY);
762 if (dfd < 0)
763 continue;
764 if (tst->ss->load_super(tst, dfd,
765 NULL)) {
766 close(dfd);
767 continue;
768 }
769 close(dfd);
770 break;
771 }
772 /* FIXME this is a bad test to be using */
773 if (!tst->sb && (dv->disposition != 'a'
774 && dv->disposition != 'S')) {
775 /* we are re-adding a device to a
776 * completely dead array - have to depend
777 * on kernel to check
778 */
779 } else if (!tst->sb) {
780 pr_err("cannot load array metadata from %s\n", devname);
781 return -1;
782 }
783
784 /* Make sure device is large enough */
785 if (tst->sb &&
786 tst->ss->avail_size(tst, ldsize/512, INVALID_SECTORS) <
787 array_size) {
788 if (dv->disposition == 'M')
789 return 0;
790 pr_err("%s not large enough to join array\n",
791 dv->devname);
792 return -1;
793 }
794
795 /* Possibly this device was recently part of
796 * the array and was temporarily removed, and
797 * is now being re-added. If so, we can
798 * simply re-add it.
799 */
800
801 if (array->not_persistent==0) {
802 dev_st = dup_super(tst);
803 dev_st->ss->load_super(dev_st, tfd, NULL);
804 }
805 if (dev_st && dev_st->sb && dv->disposition != 'S') {
806 int rv = attempt_re_add(fd, tfd, dv,
807 dev_st, tst,
808 rdev,
809 update, devname,
810 verbose,
811 array);
812 dev_st->ss->free_super(dev_st);
813 if (rv)
814 return rv;
815 }
816 if (dv->disposition == 'M') {
817 if (verbose > 0)
818 pr_err("--re-add for %s to %s is not possible\n",
819 dv->devname, devname);
820 return 0;
821 }
822 if (dv->disposition == 'A') {
823 pr_err("--re-add for %s to %s is not possible\n",
824 dv->devname, devname);
825 return -1;
826 }
827 if (array->active_disks < array->raid_disks) {
828 char *avail = xcalloc(array->raid_disks, 1);
829 int d;
830 int found = 0;
831
832 for (d = 0; d < MAX_DISKS && found < array->nr_disks; d++) {
833 disc.number = d;
834 if (ioctl(fd, GET_DISK_INFO, &disc))
835 continue;
836 if (disc.major == 0 && disc.minor == 0)
837 continue;
838 found++;
839 if (!(disc.state & (1<<MD_DISK_SYNC)))
840 continue;
841 avail[disc.raid_disk] = 1;
842 }
843 array_failed = !enough(array->level, array->raid_disks,
844 array->layout, 1, avail);
845 free(avail);
846 } else
847 array_failed = 0;
848 if (array_failed) {
849 pr_err("%s has failed so using --add cannot work and might destroy\n",
850 devname);
851 pr_err("data on %s. You should stop the array and re-assemble it.\n",
852 dv->devname);
853 return -1;
854 }
855 } else {
856 /* non-persistent. Must ensure that new drive
857 * is at least array->size big.
858 */
859 if (ldsize/512 < array_size) {
860 pr_err("%s not large enough to join array\n",
861 dv->devname);
862 return -1;
863 }
864 }
865 /* committed to really trying this device now*/
866 remove_partitions(tfd);
867
868 /* in 2.6.17 and earlier, version-1 superblocks won't
869 * use the number we write, but will choose a free number.
870 * we must choose the same free number, which requires
871 * starting at 'raid_disks' and counting up
872 */
873 for (j = array->raid_disks; j < tst->max_devs; j++) {
874 disc.number = j;
875 if (ioctl(fd, GET_DISK_INFO, &disc))
876 break;
877 if (disc.major==0 && disc.minor==0)
878 break;
879 if (disc.state & 8) /* removed */
880 break;
881 }
882 disc.major = major(rdev);
883 disc.minor = minor(rdev);
884 if (raid_slot < 0)
885 disc.number = j;
886 else
887 disc.number = raid_slot;
888 disc.state = 0;
889 if (array->not_persistent==0) {
890 int dfd;
891 if (dv->writemostly == 1)
892 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
893 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
894 if (tst->ss->add_to_super(tst, &disc, dfd,
895 dv->devname, INVALID_SECTORS))
896 return -1;
897 if (tst->ss->write_init_super(tst))
898 return -1;
899 } else if (dv->disposition == 'A') {
900 /* this had better be raid1.
901 * As we are "--re-add"ing we must find a spare slot
902 * to fill.
903 */
904 char *used = xcalloc(array->raid_disks, 1);
905 for (j = 0; j < tst->max_devs; j++) {
906 mdu_disk_info_t disc2;
907 disc2.number = j;
908 if (ioctl(fd, GET_DISK_INFO, &disc2))
909 continue;
910 if (disc2.major==0 && disc2.minor==0)
911 continue;
912 if (disc2.state & 8) /* removed */
913 continue;
914 if (disc2.raid_disk < 0)
915 continue;
916 if (disc2.raid_disk > array->raid_disks)
917 continue;
918 used[disc2.raid_disk] = 1;
919 }
920 for (j = 0 ; j < array->raid_disks; j++)
921 if (!used[j]) {
922 disc.raid_disk = j;
923 disc.state |= (1<<MD_DISK_SYNC);
924 break;
925 }
926 free(used);
927 }
928
929 if (array->state & (1 << MD_SB_CLUSTERED)) {
930 if (dv->disposition == 'c')
931 disc.state |= (1 << MD_DISK_CANDIDATE);
932 else
933 disc.state |= (1 << MD_DISK_CLUSTER_ADD);
934 }
935
936 if (dv->writemostly == 1)
937 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
938 if (tst->ss->external) {
939 /* add a disk
940 * to an external metadata container */
941 struct mdinfo new_mdi;
942 struct mdinfo *sra;
943 int container_fd;
944 char devnm[32];
945 int dfd;
946
947 strcpy(devnm, fd2devnm(fd));
948
949 container_fd = open_dev_excl(devnm);
950 if (container_fd < 0) {
951 pr_err("add failed for %s: could not get exclusive access to container\n",
952 dv->devname);
953 tst->ss->free_super(tst);
954 return -1;
955 }
956
957 Kill(dv->devname, NULL, 0, -1, 0);
958 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
959 if (mdmon_running(tst->container_devnm))
960 tst->update_tail = &tst->updates;
961 if (tst->ss->add_to_super(tst, &disc, dfd,
962 dv->devname, INVALID_SECTORS)) {
963 close(dfd);
964 close(container_fd);
965 return -1;
966 }
967 if (tst->update_tail)
968 flush_metadata_updates(tst);
969 else
970 tst->ss->sync_metadata(tst);
971
972 sra = sysfs_read(container_fd, NULL, 0);
973 if (!sra) {
974 pr_err("add failed for %s: sysfs_read failed\n",
975 dv->devname);
976 close(container_fd);
977 tst->ss->free_super(tst);
978 return -1;
979 }
980 sra->array.level = LEVEL_CONTAINER;
981 /* Need to set data_offset and component_size */
982 tst->ss->getinfo_super(tst, &new_mdi, NULL);
983 new_mdi.disk.major = disc.major;
984 new_mdi.disk.minor = disc.minor;
985 new_mdi.recovery_start = 0;
986 /* Make sure fds are closed as they are O_EXCL which
987 * would block add_disk */
988 tst->ss->free_super(tst);
989 if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
990 pr_err("add new device to external metadata failed for %s\n", dv->devname);
991 close(container_fd);
992 sysfs_free(sra);
993 return -1;
994 }
995 ping_monitor(devnm);
996 sysfs_free(sra);
997 close(container_fd);
998 } else {
999 tst->ss->free_super(tst);
1000 if (ioctl(fd, ADD_NEW_DISK, &disc)) {
1001 pr_err("add new device failed for %s as %d: %s\n",
1002 dv->devname, j, strerror(errno));
1003 return -1;
1004 }
1005 }
1006 if (verbose >= 0)
1007 pr_err("added %s\n", dv->devname);
1008 return 1;
1009 }
1010
1011 int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
1012 int sysfd, unsigned long rdev, int verbose, char *devname)
1013 {
1014 int lfd = -1;
1015 int err;
1016
1017 if (tst->ss->external) {
1018 /* To remove a device from a container, we must
1019 * check that it isn't in use in an array.
1020 * This involves looking in the 'holders'
1021 * directory - there must be just one entry,
1022 * the container.
1023 * To ensure that it doesn't get used as a
1024 * hot spare while we are checking, we
1025 * get an O_EXCL open on the container
1026 */
1027 int ret;
1028 char devnm[32];
1029 strcpy(devnm, fd2devnm(fd));
1030 lfd = open_dev_excl(devnm);
1031 if (lfd < 0) {
1032 pr_err("Cannot get exclusive access to container - odd\n");
1033 return -1;
1034 }
1035 /* We may not be able to check on holders in
1036 * sysfs, either because we don't have the dev num
1037 * (rdev == 0) or because the device has been detached
1038 * and the 'holders' directory no longer exists
1039 * (ret == -1). In that case, assume it is OK to
1040 * remove.
1041 */
1042 if (rdev == 0)
1043 ret = -1;
1044 else
1045 ret = sysfs_unique_holder(devnm, rdev);
1046 if (ret == 0) {
1047 pr_err("%s is not a member, cannot remove.\n",
1048 dv->devname);
1049 close(lfd);
1050 return -1;
1051 }
1052 if (ret >= 2) {
1053 pr_err("%s is still in use, cannot remove.\n",
1054 dv->devname);
1055 close(lfd);
1056 return -1;
1057 }
1058 }
1059 /* FIXME check that it is a current member */
1060 if (sysfd >= 0) {
1061 /* device has been removed and we don't know
1062 * the major:minor number
1063 */
1064 int n = write(sysfd, "remove", 6);
1065 if (n != 6)
1066 err = -1;
1067 else
1068 err = 0;
1069 } else {
1070 err = ioctl(fd, HOT_REMOVE_DISK, rdev);
1071 if (err && errno == ENODEV) {
1072 /* Old kernels rejected this if no personality
1073 * is registered */
1074 struct mdinfo *sra = sysfs_read(fd, NULL, GET_DEVS);
1075 struct mdinfo *dv = NULL;
1076 if (sra)
1077 dv = sra->devs;
1078 for ( ; dv ; dv=dv->next)
1079 if (dv->disk.major == (int)major(rdev) &&
1080 dv->disk.minor == (int)minor(rdev))
1081 break;
1082 if (dv)
1083 err = sysfs_set_str(sra, dv,
1084 "state", "remove");
1085 else
1086 err = -1;
1087 if (sra)
1088 sysfs_free(sra);
1089 }
1090 }
1091 if (err) {
1092 pr_err("hot remove failed for %s: %s\n", dv->devname,
1093 strerror(errno));
1094 if (lfd >= 0)
1095 close(lfd);
1096 return -1;
1097 }
1098 if (tst->ss->external) {
1099 /*
1100 * Before dropping our exclusive open we make an
1101 * attempt at preventing mdmon from seeing an
1102 * 'add' event before reconciling this 'remove'
1103 * event.
1104 */
1105 char *devnm = fd2devnm(fd);
1106
1107 if (!devnm) {
1108 pr_err("unable to get container name\n");
1109 return -1;
1110 }
1111
1112 ping_manager(devnm);
1113 }
1114 if (lfd >= 0)
1115 close(lfd);
1116 if (verbose >= 0)
1117 pr_err("hot removed %s from %s\n",
1118 dv->devname, devname);
1119 return 1;
1120 }
1121
1122 int Manage_replace(struct supertype *tst, int fd, struct mddev_dev *dv,
1123 unsigned long rdev, int verbose, char *devname)
1124 {
1125 struct mdinfo *mdi, *di;
1126 if (tst->ss->external) {
1127 pr_err("--replace only supported for native metadata (0.90 or 1.x)\n");
1128 return -1;
1129 }
1130 /* Need to find the device in sysfs and add 'want_replacement' to the
1131 * status.
1132 */
1133 mdi = sysfs_read(fd, NULL, GET_DEVS);
1134 if (!mdi || !mdi->devs) {
1135 pr_err("Cannot find status of %s to enable replacement - strange\n",
1136 devname);
1137 return -1;
1138 }
1139 for (di = mdi->devs; di; di = di->next)
1140 if (di->disk.major == (int)major(rdev) &&
1141 di->disk.minor == (int)minor(rdev))
1142 break;
1143 if (di) {
1144 int rv;
1145 if (di->disk.raid_disk < 0) {
1146 pr_err("%s is not active and so cannot be replaced.\n",
1147 dv->devname);
1148 sysfs_free(mdi);
1149 return -1;
1150 }
1151 rv = sysfs_set_str(mdi, di,
1152 "state", "want_replacement");
1153 if (rv) {
1154 sysfs_free(mdi);
1155 pr_err("Failed to request replacement for %s\n",
1156 dv->devname);
1157 return -1;
1158 }
1159 if (verbose >= 0)
1160 pr_err("Marked %s (device %d in %s) for replacement\n",
1161 dv->devname, di->disk.raid_disk, devname);
1162 /* If there is a matching 'with', we need to tell it which
1163 * raid disk
1164 */
1165 while (dv && dv->disposition != 'W')
1166 dv = dv->next;
1167 if (dv) {
1168 dv->disposition = 'w';
1169 dv->used = di->disk.raid_disk;
1170 }
1171 return 1;
1172 }
1173 sysfs_free(mdi);
1174 pr_err("%s not found in %s so cannot --replace it\n",
1175 dv->devname, devname);
1176 return -1;
1177 }
1178
1179 int Manage_with(struct supertype *tst, int fd, struct mddev_dev *dv,
1180 unsigned long rdev, int verbose, char *devname)
1181 {
1182 struct mdinfo *mdi, *di;
1183 /* try to set 'slot' for 'rdev' in 'fd' to 'dv->used' */
1184 mdi = sysfs_read(fd, NULL, GET_DEVS|GET_STATE);
1185 if (!mdi || !mdi->devs) {
1186 pr_err("Cannot find status of %s to enable replacement - strange\n",
1187 devname);
1188 return -1;
1189 }
1190 for (di = mdi->devs; di; di = di->next)
1191 if (di->disk.major == (int)major(rdev) &&
1192 di->disk.minor == (int)minor(rdev))
1193 break;
1194 if (di) {
1195 int rv;
1196 if (di->disk.state & (1<<MD_DISK_FAULTY)) {
1197 pr_err("%s is faulty and cannot be a replacement\n",
1198 dv->devname);
1199 sysfs_free(mdi);
1200 return -1;
1201 }
1202 if (di->disk.raid_disk >= 0) {
1203 pr_err("%s is active and cannot be a replacement\n",
1204 dv->devname);
1205 sysfs_free(mdi);
1206 return -1;
1207 }
1208 rv = sysfs_set_num(mdi, di,
1209 "slot", dv->used);
1210 if (rv) {
1211 sysfs_free(mdi);
1212 pr_err("Failed to set %s as preferred replacement.\n",
1213 dv->devname);
1214 return -1;
1215 }
1216 if (verbose >= 0)
1217 pr_err("Marked %s in %s as replacement for device %d\n",
1218 dv->devname, devname, dv->used);
1219 return 1;
1220 }
1221 sysfs_free(mdi);
1222 pr_err("%s not found in %s so cannot make it preferred replacement\n",
1223 dv->devname, devname);
1224 return -1;
1225 }
1226
1227 int Manage_subdevs(char *devname, int fd,
1228 struct mddev_dev *devlist, int verbose, int test,
1229 char *update, int force)
1230 {
1231 /* Do something to each dev.
1232 * devmode can be
1233 * 'a' - add the device
1234 * try HOT_ADD_DISK
1235 * If that fails EINVAL, try ADD_NEW_DISK
1236 * 'S' - add the device as a spare - don't try re-add
1237 * 'A' - re-add the device
1238 * 'r' - remove the device: HOT_REMOVE_DISK
1239 * device can be 'faulty' or 'detached' in which case all
1240 * matching devices are removed.
1241 * 'f' - set the device faulty SET_DISK_FAULTY
1242 * device can be 'detached' in which case any device that
1243 * is inaccessible will be marked faulty.
1244 * 'R' - mark this device as wanting replacement.
1245 * 'W' - this device is added if necessary and activated as
1246 * a replacement for a previous 'R' device.
1247 * -----
1248 * 'w' - 'W' will be changed to 'w' when it is paired with
1249 * a 'R' device. If a 'W' is found while walking the list
1250 * it must be unpaired, and is an error.
1251 * 'M' - this is created by a 'missing' target. It is a slight
1252 * variant on 'A'
1253 * 'F' - Another variant of 'A', where the device was faulty
1254 * so must be removed from the array first.
1255 * 'c' - confirm the device as found (for clustered environments)
1256 *
1257 * For 'f' and 'r', the device can also be a kernel-internal
1258 * name such as 'sdb'.
1259 */
1260 mdu_array_info_t array;
1261 unsigned long long array_size;
1262 struct mddev_dev *dv;
1263 int tfd = -1;
1264 struct supertype *tst;
1265 char *subarray = NULL;
1266 int sysfd = -1;
1267 int count = 0; /* number of actions taken */
1268 struct mdinfo info;
1269 int frozen = 0;
1270 int busy = 0;
1271 int raid_slot = -1;
1272
1273 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
1274 pr_err("Cannot get array info for %s\n",
1275 devname);
1276 goto abort;
1277 }
1278 sysfs_init(&info, fd, NULL);
1279
1280 /* array.size is only 32 bits and may be truncated.
1281 * So read from sysfs if possible, and record number of sectors
1282 */
1283
1284 array_size = get_component_size(fd);
1285 if (array_size <= 0)
1286 array_size = array.size * 2;
1287
1288 tst = super_by_fd(fd, &subarray);
1289 if (!tst) {
1290 pr_err("unsupport array - version %d.%d\n",
1291 array.major_version, array.minor_version);
1292 goto abort;
1293 }
1294
1295 for (dv = devlist; dv; dv = dv->next) {
1296 unsigned long rdev = 0; /* device to add/remove etc */
1297 int rv;
1298 int mj,mn;
1299
1300 raid_slot = -1;
1301 if (dv->disposition == 'c') {
1302 rv = parse_cluster_confirm_arg(dv->devname,
1303 &dv->devname,
1304 &raid_slot);
1305 if (!rv) {
1306 pr_err("Could not get the devname of cluster\n");
1307 goto abort;
1308 }
1309 }
1310
1311 if (strcmp(dv->devname, "failed") == 0 ||
1312 strcmp(dv->devname, "faulty") == 0) {
1313 if (dv->disposition != 'A'
1314 && dv->disposition != 'r') {
1315 pr_err("%s only meaningful with -r or --re-add, not -%c\n",
1316 dv->devname, dv->disposition);
1317 goto abort;
1318 }
1319 add_faulty(dv, fd, (dv->disposition == 'A'
1320 ? 'F' : 'r'));
1321 continue;
1322 }
1323 if (strcmp(dv->devname, "detached") == 0) {
1324 if (dv->disposition != 'r' && dv->disposition != 'f') {
1325 pr_err("%s only meaningful with -r of -f, not -%c\n",
1326 dv->devname, dv->disposition);
1327 goto abort;
1328 }
1329 add_detached(dv, fd, dv->disposition);
1330 continue;
1331 }
1332
1333 if (strcmp(dv->devname, "missing") == 0) {
1334 struct mddev_dev *add_devlist = NULL;
1335 struct mddev_dev **dp;
1336 if (dv->disposition == 'c') {
1337 rv = ioctl(fd, CLUSTERED_DISK_NACK, NULL);
1338 break;
1339 }
1340
1341 if (dv->disposition != 'A') {
1342 pr_err("'missing' only meaningful with --re-add\n");
1343 goto abort;
1344 }
1345 add_devlist = conf_get_devs();
1346 if (add_devlist == NULL) {
1347 pr_err("no devices to scan for missing members.");
1348 continue;
1349 }
1350 for (dp = &add_devlist; *dp; dp = & (*dp)->next)
1351 /* 'M' (for 'missing') is like 'A' without errors */
1352 (*dp)->disposition = 'M';
1353 *dp = dv->next;
1354 dv->next = add_devlist;
1355 continue;
1356 }
1357
1358 if (strncmp(dv->devname, "set-", 4) == 0 &&
1359 strlen(dv->devname) == 5) {
1360 int copies;
1361
1362 if (dv->disposition != 'r' &&
1363 dv->disposition != 'f') {
1364 pr_err("'%s' only meaningful with -r or -f\n",
1365 dv->devname);
1366 goto abort;
1367 }
1368 if (array.level != 10) {
1369 pr_err("'%s' only meaningful with RAID10 arrays\n",
1370 dv->devname);
1371 goto abort;
1372 }
1373 copies = ((array.layout & 0xff) *
1374 ((array.layout >> 8) & 0xff));
1375 if (array.raid_disks % copies != 0 ||
1376 dv->devname[4] < 'A' ||
1377 dv->devname[4] >= 'A' + copies ||
1378 copies > 26) {
1379 pr_err("'%s' not meaningful with this array\n",
1380 dv->devname);
1381 goto abort;
1382 }
1383 add_set(dv, fd, dv->devname[4]);
1384 continue;
1385 }
1386
1387 if (strchr(dv->devname, '/') == NULL &&
1388 strchr(dv->devname, ':') == NULL &&
1389 strlen(dv->devname) < 50) {
1390 /* Assume this is a kernel-internal name like 'sda1' */
1391 int found = 0;
1392 char dname[55];
1393 if (dv->disposition != 'r' && dv->disposition != 'f') {
1394 pr_err("%s only meaningful with -r or -f, not -%c\n",
1395 dv->devname, dv->disposition);
1396 goto abort;
1397 }
1398
1399 sprintf(dname, "dev-%s", dv->devname);
1400 sysfd = sysfs_open(fd2devnm(fd), dname, "block/dev");
1401 if (sysfd >= 0) {
1402 char dn[20];
1403 if (sysfs_fd_get_str(sysfd, dn, 20) > 0 &&
1404 sscanf(dn, "%d:%d", &mj,&mn) == 2) {
1405 rdev = makedev(mj,mn);
1406 found = 1;
1407 }
1408 close(sysfd);
1409 sysfd = -1;
1410 }
1411 if (!found) {
1412 sysfd = sysfs_open(fd2devnm(fd), dname, "state");
1413 if (sysfd < 0) {
1414 pr_err("%s does not appear to be a component of %s\n",
1415 dv->devname, devname);
1416 goto abort;
1417 }
1418 }
1419 } else if ((dv->disposition == 'r' || dv->disposition == 'f')
1420 && get_maj_min(dv->devname, &mj, &mn)) {
1421 /* for 'fail' and 'remove', the device might
1422 * not exist.
1423 */
1424 rdev = makedev(mj, mn);
1425 } else {
1426 struct stat stb;
1427 tfd = dev_open(dv->devname, O_RDONLY);
1428 if (tfd >= 0)
1429 fstat(tfd, &stb);
1430 else {
1431 int open_err = errno;
1432 if (stat(dv->devname, &stb) != 0) {
1433 pr_err("Cannot find %s: %s\n",
1434 dv->devname, strerror(errno));
1435 goto abort;
1436 }
1437 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
1438 if (dv->disposition == 'M')
1439 /* non-fatal. Also improbable */
1440 continue;
1441 pr_err("%s is not a block device.\n",
1442 dv->devname);
1443 goto abort;
1444 }
1445 if (dv->disposition == 'r')
1446 /* Be happy, the stat worked, that is
1447 * enough for --remove
1448 */
1449 ;
1450 else {
1451 if (dv->disposition == 'M')
1452 /* non-fatal */
1453 continue;
1454 pr_err("Cannot open %s: %s\n",
1455 dv->devname, strerror(open_err));
1456 goto abort;
1457 }
1458 }
1459 rdev = stb.st_rdev;
1460 }
1461 switch(dv->disposition){
1462 default:
1463 pr_err("internal error - devmode[%s]=%d\n",
1464 dv->devname, dv->disposition);
1465 goto abort;
1466 case 'a':
1467 case 'S': /* --add-spare */
1468 case 'A':
1469 case 'M': /* --re-add missing */
1470 case 'F': /* --re-add faulty */
1471 case 'c': /* --cluster-confirm */
1472 /* add the device */
1473 if (subarray) {
1474 pr_err("Cannot add disks to a \'member\' array, perform this operation on the parent container\n");
1475 goto abort;
1476 }
1477 if (dv->disposition == 'F')
1478 /* Need to remove first */
1479 ioctl(fd, HOT_REMOVE_DISK, rdev);
1480 /* Make sure it isn't in use (in 2.6 or later) */
1481 tfd = dev_open(dv->devname, O_RDONLY|O_EXCL);
1482 if (tfd >= 0) {
1483 /* We know no-one else is using it. We'll
1484 * need non-exclusive access to add it, so
1485 * do that now.
1486 */
1487 close(tfd);
1488 tfd = dev_open(dv->devname, O_RDONLY);
1489 }
1490 if (tfd < 0) {
1491 if (dv->disposition == 'M')
1492 continue;
1493 pr_err("Cannot open %s: %s\n",
1494 dv->devname, strerror(errno));
1495 goto abort;
1496 }
1497 if (!frozen) {
1498 if (sysfs_freeze_array(&info) == 1)
1499 frozen = 1;
1500 else
1501 frozen = -1;
1502 }
1503 rv = Manage_add(fd, tfd, dv, tst, &array,
1504 force, verbose, devname, update,
1505 rdev, array_size, raid_slot);
1506 close(tfd);
1507 tfd = -1;
1508 if (rv < 0)
1509 goto abort;
1510 if (rv > 0)
1511 count++;
1512 break;
1513
1514 case 'r':
1515 /* hot remove */
1516 if (subarray) {
1517 pr_err("Cannot remove disks from a \'member\' array, perform this operation on the parent container\n");
1518 rv = -1;
1519 } else
1520 rv = Manage_remove(tst, fd, dv, sysfd,
1521 rdev, verbose,
1522 devname);
1523 if (sysfd >= 0)
1524 close(sysfd);
1525 sysfd = -1;
1526 if (rv < 0)
1527 goto abort;
1528 if (rv > 0)
1529 count++;
1530 break;
1531
1532 case 'f': /* set faulty */
1533 /* FIXME check current member */
1534 if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
1535 (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
1536 rdev))) {
1537 if (errno == EBUSY)
1538 busy = 1;
1539 pr_err("set device faulty failed for %s: %s\n",
1540 dv->devname, strerror(errno));
1541 if (sysfd >= 0)
1542 close(sysfd);
1543 goto abort;
1544 }
1545 if (sysfd >= 0)
1546 close(sysfd);
1547 sysfd = -1;
1548 count++;
1549 if (verbose >= 0)
1550 pr_err("set %s faulty in %s\n",
1551 dv->devname, devname);
1552 break;
1553 case 'R': /* Mark as replaceable */
1554 if (subarray) {
1555 pr_err("Cannot replace disks in a \'member\' array, perform this operation on the parent container\n");
1556 rv = -1;
1557 } else {
1558 if (!frozen) {
1559 if (sysfs_freeze_array(&info) == 1)
1560 frozen = 1;
1561 else
1562 frozen = -1;
1563 }
1564 rv = Manage_replace(tst, fd, dv,
1565 rdev, verbose,
1566 devname);
1567 }
1568 if (rv < 0)
1569 goto abort;
1570 if (rv > 0)
1571 count++;
1572 break;
1573 case 'W': /* --with device that doesn't match */
1574 pr_err("No matching --replace device for --with %s\n",
1575 dv->devname);
1576 goto abort;
1577 case 'w': /* --with device which was matched */
1578 rv = Manage_with(tst, fd, dv,
1579 rdev, verbose, devname);
1580 if (rv < 0)
1581 goto abort;
1582 break;
1583 }
1584 }
1585 if (frozen > 0)
1586 sysfs_set_str(&info, NULL, "sync_action","idle");
1587 if (test && count == 0)
1588 return 2;
1589 return 0;
1590
1591 abort:
1592 if (frozen > 0)
1593 sysfs_set_str(&info, NULL, "sync_action","idle");
1594 return !test && busy ? 2 : 1;
1595 }
1596
1597 int autodetect(void)
1598 {
1599 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1600 int rv = 1;
1601 int fd = dev_open("9:0", O_RDONLY);
1602 if (fd >= 0) {
1603 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
1604 rv = 0;
1605 close(fd);
1606 }
1607 return rv;
1608 }
1609
1610 int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int verbose)
1611 {
1612 struct supertype supertype, *st = &supertype;
1613 int fd, rv = 2;
1614
1615 memset(st, 0, sizeof(*st));
1616
1617 fd = open_subarray(dev, subarray, st, verbose < 0);
1618 if (fd < 0)
1619 return 2;
1620
1621 if (!st->ss->update_subarray) {
1622 if (verbose >= 0)
1623 pr_err("Operation not supported for %s metadata\n",
1624 st->ss->name);
1625 goto free_super;
1626 }
1627
1628 if (mdmon_running(st->devnm))
1629 st->update_tail = &st->updates;
1630
1631 rv = st->ss->update_subarray(st, subarray, update, ident);
1632
1633 if (rv) {
1634 if (verbose >= 0)
1635 pr_err("Failed to update %s of subarray-%s in %s\n",
1636 update, subarray, dev);
1637 } else if (st->update_tail)
1638 flush_metadata_updates(st);
1639 else
1640 st->ss->sync_metadata(st);
1641
1642 if (rv == 0 && strcmp(update, "name") == 0 && verbose >= 0)
1643 pr_err("Updated subarray-%s name from %s, UUIDs may have changed\n",
1644 subarray, dev);
1645
1646 free_super:
1647 st->ss->free_super(st);
1648 close(fd);
1649
1650 return rv;
1651 }
1652
1653 /* Move spare from one array to another If adding to destination array fails
1654 * add back to original array.
1655 * Returns 1 on success, 0 on failure */
1656 int move_spare(char *from_devname, char *to_devname, dev_t devid)
1657 {
1658 struct mddev_dev devlist;
1659 char devname[20];
1660
1661 /* try to remove and add */
1662 int fd1 = open(to_devname, O_RDONLY);
1663 int fd2 = open(from_devname, O_RDONLY);
1664
1665 if (fd1 < 0 || fd2 < 0) {
1666 if (fd1>=0) close(fd1);
1667 if (fd2>=0) close(fd2);
1668 return 0;
1669 }
1670
1671 devlist.next = NULL;
1672 devlist.used = 0;
1673 devlist.writemostly = 0;
1674 devlist.devname = devname;
1675 sprintf(devname, "%d:%d", major(devid), minor(devid));
1676
1677 devlist.disposition = 'r';
1678 if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0) == 0) {
1679 devlist.disposition = 'a';
1680 if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL, 0) == 0) {
1681 /* make sure manager is aware of changes */
1682 ping_manager(to_devname);
1683 ping_manager(from_devname);
1684 close(fd1);
1685 close(fd2);
1686 return 1;
1687 }
1688 else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0);
1689 }
1690 close(fd1);
1691 close(fd2);
1692 return 0;
1693 }
1694 #endif