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