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