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