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