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