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