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