]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Manage.c
Create: default to bitmap=internal for large arrays.
[thirdparty/mdadm.git] / Manage.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
7bd04da9 4 * Copyright (C) 2001-2012 Neil Brown <neilb@suse.de>
64c4757e
NB
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
e736b623 22 * Email: <neilb@suse.de>
64c4757e
NB
23 */
24
9a9dab36 25#include "mdadm.h"
682c7051
NB
26#include "md_u.h"
27#include "md_p.h"
4ccad7b1 28#include <ctype.h>
64c4757e 29
82b27616
NB
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
64c4757e
NB
34int Manage_ro(char *devname, int fd, int readonly)
35{
682c7051
NB
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;
0e600426 44#ifndef MDASSEMBLE
e9dd1598 45 struct mdinfo *mdi;
0e600426 46#endif
b73e45ae 47 int rv = 0;
aba69144 48
682c7051 49 if (md_get_version(fd) < 9000) {
e7b84f9d 50 pr_err("need md driver version 0.90.0 or later\n");
682c7051
NB
51 return 1;
52 }
0e600426 53#ifndef MDASSEMBLE
7bd04da9 54 /* If this is an externally-managed array, we need to modify the
e9dd1598
N
55 * metadata_version so that mdmon doesn't undo our change.
56 */
4dd2df09 57 mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION);
e9dd1598
N
58 if (mdi &&
59 mdi->array.major_version == -1 &&
e9dd1598
N
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) {
e7b84f9d 74 pr_err("failed to set readonly for %s: %s\n",
e9dd1598
N
75 devname, strerror(errno));
76
77 vers[9] = mdi->text_version[0];
78 sysfs_set_str(mdi, NULL, "metadata_version", vers);
b73e45ae
JS
79 rv = 1;
80 goto out;
e9dd1598
N
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, '/');
1471b8b1 89 if (cp)
e9dd1598
N
90 *cp = 0;
91 ping_monitor(vers+10);
9ea5a252
DW
92 if (mdi->array.level <= 0)
93 sysfs_set_str(mdi, NULL, "array_state", "active");
e9dd1598 94 }
b73e45ae 95 goto out;
e9dd1598 96 }
0e600426 97#endif
682c7051 98 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
e7b84f9d 99 pr_err("%s does not appear to be active.\n",
682c7051 100 devname);
b73e45ae
JS
101 rv = 1;
102 goto out;
682c7051 103 }
aba69144 104
7bd04da9 105 if (readonly > 0) {
682c7051 106 if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
e7b84f9d 107 pr_err("failed to set readonly for %s: %s\n",
682c7051 108 devname, strerror(errno));
b73e45ae
JS
109 rv = 1;
110 goto out;
682c7051
NB
111 }
112 } else if (readonly < 0) {
113 if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
e7b84f9d 114 pr_err("failed to set writable for %s: %s\n",
682c7051 115 devname, strerror(errno));
b73e45ae
JS
116 rv = 1;
117 goto out;
682c7051
NB
118 }
119 }
b73e45ae
JS
120out:
121#ifndef MDASSEMBLE
122 if (mdi)
123 sysfs_free(mdi);
124#endif
125 return rv;
64c4757e
NB
126}
127
435d4ebb
LB
128#ifndef MDASSEMBLE
129
4dd2df09 130static void remove_devices(char *devnm, char *path)
4ccad7b1 131{
7bd04da9 132 /*
b1702f48
N
133 * Remove names at 'path' - possibly with
134 * partition suffixes - which link to the 'standard'
4dd2df09 135 * name for devnm. These were probably created
b1702f48 136 * by mdadm when the array was assembled.
4ccad7b1
N
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
b1702f48
N
146 if (!path)
147 return;
148
4dd2df09 149 sprintf(base, "/dev/%s", devnm);
4ccad7b1 150 be = base + strlen(base);
b1702f48 151
503975b9 152 path2 = xmalloc(strlen(path)+20);
b1702f48
N
153 strcpy(path2, path);
154 pe = path2 + strlen(path2);
7bd04da9 155
4ccad7b1
N
156 for (part = 0; part < 16; part++) {
157 if (part) {
158 sprintf(be, "p%d", part);
b1702f48
N
159
160 if (isdigit(pe[-1]))
161 sprintf(pe, "p%d", part);
162 else
163 sprintf(pe, "%d", part);
4ccad7b1 164 }
b1702f48 165 n = readlink(path2, link, sizeof(link));
d9ca03e9 166 if (n > 0 && (int)strlen(base) == n &&
b1702f48
N
167 strncmp(link, base, n) == 0)
168 unlink(path2);
4ccad7b1 169 }
0eb26465 170 free(path2);
4ccad7b1 171}
4ccad7b1 172
ba728be7
N
173int Manage_runstop(char *devname, int fd, int runstop,
174 int verbose, int will_retry)
64c4757e 175{
7bd04da9 176 /* Run or stop the array. Array must already be configured
ba728be7
N
177 * 'Run' requires >= 0.90.0
178 * 'will_retry' is only relevant for 'stop', and means
179 * that error messages are not wanted.
682c7051 180 */
682c7051 181 mdu_param_t param; /* unused */
bccd8153 182 int rv = 0;
82b27616 183
ba728be7
N
184 if (will_retry && verbose == 0)
185 verbose = -1;
186
82b27616 187 if (runstop == -1 && md_get_version(fd) < 9000) {
7bd04da9
N
188 if (ioctl(fd, STOP_MD, 0) == 0)
189 return 0;
190 pr_err("stopping device %s "
191 "failed: %s\n",
192 devname, strerror(errno));
193 return 1;
82b27616 194 }
aba69144 195
682c7051 196 if (md_get_version(fd) < 9000) {
e7b84f9d 197 pr_err("need md driver version 0.90.0 or later\n");
682c7051
NB
198 return 1;
199 }
ba728be7 200
7bd04da9 201 if (runstop > 0) {
682c7051 202 if (ioctl(fd, RUN_ARRAY, &param)) {
ba728be7
N
203 if (verbose >= 0)
204 pr_err("failed to run array %s: %s\n",
205 devname, strerror(errno));
682c7051
NB
206 return 1;
207 }
ba728be7 208 if (verbose >= 0)
e7b84f9d 209 pr_err("started %s\n", devname);
682c7051 210 } else if (runstop < 0){
8382f19b
NB
211 struct map_ent *map = NULL;
212 struct stat stb;
daf7a3ce 213 struct mdinfo *mdi;
4dd2df09 214 char devnm[32];
eb0af526
N
215 int err;
216 int count;
daf7a3ce
NB
217 /* If this is an mdmon managed array, just write 'inactive'
218 * to the array state and let mdmon clear up.
219 */
4dd2df09 220 strcpy(devnm, fd2devnm(fd));
eb0af526
N
221 /* Get EXCL access first. If this fails, then attempting
222 * to stop is probably a bad idea.
223 */
224 close(fd);
8af530b0
N
225 if (devnm[0] == '/')
226 fd = open(devname, O_RDONLY|O_EXCL);
227 else
228 fd = open_dev_flags(devnm, O_RDONLY|O_EXCL);
4dd2df09 229 if (fd < 0 || strcmp(fd2devnm(fd), devnm) != 0) {
eb0af526
N
230 if (fd >= 0)
231 close(fd);
ba728be7
N
232 if (verbose >= 0)
233 pr_err("Cannot get exclusive access to %s:"
234 "Perhaps a running "
235 "process, mounted filesystem "
236 "or active volume group?\n",
237 devname);
eb0af526
N
238 return 1;
239 }
4dd2df09 240 mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION);
daf7a3ce
NB
241 if (mdi &&
242 mdi->array.level > 0 &&
3c558363 243 is_subarray(mdi->text_version)) {
1ae42d9d 244 int err;
daf7a3ce
NB
245 /* This is mdmon managed. */
246 close(fd);
1ae42d9d 247
7bd04da9
N
248 /* As we have an O_EXCL open, any use of the device
249 * which blocks STOP_ARRAY is probably a transient use,
250 * so it is reasonable to retry for a while - 5 seconds.
251 */
1ae42d9d
KW
252 count = 25;
253 while (count &&
254 (err = sysfs_set_str(mdi, NULL,
255 "array_state",
256 "inactive")) < 0
257 && errno == EBUSY) {
258 usleep(200000);
259 count--;
260 }
ba728be7
N
261 if (err) {
262 if (verbose >= 0)
263 pr_err("failed to stop array %s: %s\n",
264 devname, strerror(errno));
bccd8153
JS
265 rv = 1;
266 goto out;
daf7a3ce
NB
267 }
268
269 /* Give monitor a chance to act */
c94709e8 270 ping_monitor(mdi->text_version);
daf7a3ce 271
4dd2df09 272 fd = open_dev_excl(devnm);
eb0af526 273 if (fd < 0) {
ba728be7
N
274 if (verbose >= 0)
275 pr_err("failed to completely stop %s"
276 ": Device is busy\n",
277 devname);
bccd8153
JS
278 rv = 1;
279 goto out;
eb0af526 280 }
ada6c239
N
281 } else if (mdi &&
282 mdi->array.major_version == -1 &&
283 mdi->array.minor_version == -2 &&
3c558363 284 !is_subarray(mdi->text_version)) {
430ea469 285 struct mdstat_ent *mds, *m;
ada6c239
N
286 /* container, possibly mdmon-managed.
287 * Make sure mdmon isn't opening it, which
288 * would interfere with the 'stop'
289 */
290 ping_monitor(mdi->sys_name);
430ea469
N
291
292 /* now check that there are no existing arrays
293 * which are members of this array
294 */
295 mds = mdstat_read(0, 0);
7bd04da9 296 for (m = mds; m; m = m->next)
430ea469
N
297 if (m->metadata_version &&
298 strncmp(m->metadata_version, "external:", 9)==0 &&
4dd2df09
N
299 metadata_container_matches(m->metadata_version+9,
300 devnm)) {
ba728be7 301 if (verbose >= 0)
e7b84f9d
N
302 pr_err("Cannot stop container %s: "
303 "member %s still active\n",
304 devname, m->dev);
430ea469 305 free_mdstat(mds);
bccd8153
JS
306 rv = 1;
307 goto out;
430ea469 308 }
daf7a3ce 309 }
daf7a3ce 310
eb0af526
N
311 /* As we have an O_EXCL open, any use of the device
312 * which blocks STOP_ARRAY is probably a transient use,
313 * so it is reasonable to retry for a while - 5 seconds.
314 */
fb0d4b9c 315 count = 25; err = 0;
eb0af526
N
316 while (count && fd >= 0
317 && (err = ioctl(fd, STOP_ARRAY, NULL)) < 0
318 && errno == EBUSY) {
319 usleep(200000);
320 count --;
321 }
322 if (fd >= 0 && err) {
ba728be7 323 if (verbose >= 0) {
e7b84f9d
N
324 pr_err("failed to stop array %s: %s\n",
325 devname, strerror(errno));
d927f3c4
NB
326 if (errno == EBUSY)
327 fprintf(stderr, "Perhaps a running "
328 "process, mounted filesystem "
329 "or active volume group?\n");
330 }
bccd8153
JS
331 rv = 1;
332 goto out;
682c7051 333 }
97590376 334 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
d49410d3
N
335 * was stopped, so We'll do it here just to be sure. Drop any
336 * partitions as well...
97590376 337 */
d49410d3
N
338 if (fd >= 0)
339 ioctl(fd, BLKRRPART, 0);
97590376
N
340 if (mdi)
341 sysfs_uevent(mdi, "change");
daf7a3ce 342
4dd2df09 343 if (devnm[0] &&
4ccad7b1
N
344 (stat("/dev/.udev", &stb) != 0 ||
345 check_env("MDADM_NO_UDEV"))) {
4dd2df09
N
346 struct map_ent *mp = map_by_devnm(&map, devnm);
347 remove_devices(devnm, mp ? mp->path : NULL);
4ccad7b1
N
348 }
349
ba728be7 350 if (verbose >= 0)
e7b84f9d 351 pr_err("stopped %s\n", devname);
4eb26970 352 map_lock(&map);
4dd2df09 353 map_remove(&map, devnm);
4eb26970 354 map_unlock(&map);
bccd8153
JS
355 out:
356 if (mdi)
357 sysfs_free(mdi);
682c7051 358 }
bccd8153 359 return rv;
64c4757e
NB
360}
361
1d997643
N
362static void add_faulty(struct mddev_dev *dv, int fd, char disp)
363{
364 mdu_array_info_t array;
365 mdu_disk_info_t disk;
366 int remaining_disks;
367 int i;
368
369 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0)
370 return;
371
372 remaining_disks = array.nr_disks;
373 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
374 struct mddev_dev *new;
375 char buf[40];
376 disk.number = i;
377 if (ioctl(fd, GET_DISK_INFO, &disk) != 0)
378 continue;
379 if (disk.major == 0 && disk.minor == 0)
380 continue;
381 remaining_disks--;
382 if ((disk.state & 1) == 0) /* not faulty */
383 continue;
384 sprintf(buf, "%d:%d", disk.major, disk.minor);
385 new = xmalloc(sizeof(*new));
386 new->devname = xstrdup(buf);
387 new->disposition = disp;
388 new->next = dv->next;
389 dv->next = new;
390 dv = new;
391 }
392}
393
394static void add_detached(struct mddev_dev *dv, int fd, char disp)
395{
396 mdu_array_info_t array;
397 mdu_disk_info_t disk;
398 int remaining_disks;
399 int i;
400
401 if (ioctl(fd, GET_ARRAY_INFO, &array) != 0)
402 return;
403
404 remaining_disks = array.nr_disks;
405 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
406 struct mddev_dev *new;
407 char buf[40];
408 int sfd;
409 disk.number = i;
410 if (ioctl(fd, GET_DISK_INFO, &disk) != 0)
411 continue;
412 if (disk.major == 0 && disk.minor == 0)
413 continue;
414 remaining_disks--;
415 if (disp == 'f' && (disk.state & 1) != 0) /* already faulty */
416 continue;
417 sprintf(buf, "%d:%d", disk.major, disk.minor);
418 sfd = dev_open(buf, O_RDONLY);
419 if (sfd >= 0) {
420 /* Not detached */
421 close(sfd);
422 continue;
423 }
424 if (errno != ENXIO)
425 /* Probably not detached */
426 continue;
427 new = xmalloc(sizeof(*new));
428 new->devname = xstrdup(buf);
429 new->disposition = disp;
430 new->next = dv->next;
431 dv->next = new;
432 dv = new;
433 }
434}
435
abe94694
N
436int attempt_re_add(int fd, int tfd, struct mddev_dev *dv,
437 struct supertype *dev_st, struct supertype *tst,
438 unsigned long rdev,
439 char *update, char *devname, int verbose,
440 mdu_array_info_t *array)
441{
442 struct mdinfo mdi;
443 int duuid[4];
444 int ouuid[4];
445
446 dev_st->ss->getinfo_super(dev_st, &mdi, NULL);
447 dev_st->ss->uuid_from_super(dev_st, ouuid);
448 if (tst->sb)
449 tst->ss->uuid_from_super(tst, duuid);
450 else
451 /* Assume uuid matches: kernel will check */
452 memcpy(duuid, ouuid, sizeof(ouuid));
453 if ((mdi.disk.state & (1<<MD_DISK_ACTIVE)) &&
454 !(mdi.disk.state & (1<<MD_DISK_FAULTY)) &&
455 memcmp(duuid, ouuid, sizeof(ouuid))==0) {
456 /* Looks like it is worth a
457 * try. Need to make sure
458 * kernel will accept it
459 * though.
460 */
461 mdu_disk_info_t disc;
462 /* re-add doesn't work for version-1 superblocks
463 * before 2.6.18 :-(
464 */
465 if (array->major_version == 1 &&
466 get_linux_version() <= 2006018)
467 goto skip_re_add;
468 disc.number = mdi.disk.number;
469 if (ioctl(fd, GET_DISK_INFO, &disc) != 0
470 || disc.major != 0 || disc.minor != 0
471 )
472 goto skip_re_add;
473 disc.major = major(rdev);
474 disc.minor = minor(rdev);
475 disc.number = mdi.disk.number;
476 disc.raid_disk = mdi.disk.raid_disk;
477 disc.state = mdi.disk.state;
478 if (dv->writemostly == 1)
479 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
480 if (dv->writemostly == 2)
481 disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
482 remove_partitions(tfd);
483 if (update || dv->writemostly > 0) {
484 int rv = -1;
485 tfd = dev_open(dv->devname, O_RDWR);
486 if (tfd < 0) {
487 pr_err("failed to open %s for"
488 " superblock update during re-add\n", dv->devname);
489 return -1;
490 }
491
492 if (dv->writemostly == 1)
493 rv = dev_st->ss->update_super(
494 dev_st, NULL, "writemostly",
495 devname, verbose, 0, NULL);
496 if (dv->writemostly == 2)
497 rv = dev_st->ss->update_super(
498 dev_st, NULL, "readwrite",
499 devname, verbose, 0, NULL);
500 if (update)
501 rv = dev_st->ss->update_super(
502 dev_st, NULL, update,
503 devname, verbose, 0, NULL);
504 if (rv == 0)
505 rv = dev_st->ss->store_super(dev_st, tfd);
506 close(tfd);
507 if (rv != 0) {
508 pr_err("failed to update"
509 " superblock during re-add\n");
510 return -1;
511 }
512 }
513 /* don't even try if disk is marked as faulty */
514 errno = 0;
515 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
516 if (verbose >= 0)
517 pr_err("re-added %s\n", dv->devname);
518 return 1;
519 }
520 if (errno == ENOMEM || errno == EROFS) {
521 pr_err("add new device failed for %s: %s\n",
522 dv->devname, strerror(errno));
523 if (dv->disposition == 'M')
524 return 0;
525 return -1;
526 }
527 }
528skip_re_add:
529 return 0;
530}
531
38aeaf3a
N
532int Manage_add(int fd, int tfd, struct mddev_dev *dv,
533 struct supertype *tst, mdu_array_info_t *array,
534 int force, int verbose, char *devname,
535 char *update, unsigned long rdev, unsigned long long array_size)
536{
537 unsigned long long ldsize;
6d43efb5 538 struct supertype *dev_st = NULL;
38aeaf3a
N
539 int j;
540 mdu_disk_info_t disc;
541
542 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
543 if (dv->disposition == 'M')
544 return 0;
545 else
546 return -1;
547 }
548
549 if (tst->ss->validate_geometry(
550 tst, array->level, array->layout,
551 array->raid_disks, NULL,
af4348dd 552 ldsize >> 9, INVALID_SECTORS, NULL, NULL, 0) == 0) {
38aeaf3a
N
553 if (!force) {
554 pr_err("%s is larger than %s can "
555 "effectively use.\n"
556 " Add --force is you "
557 "really want to add this device.\n",
558 dv->devname, devname);
559 return -1;
560 }
561 pr_err("%s is larger than %s can "
562 "effectively use.\n"
563 " Adding anyway as --force "
564 "was given.\n",
565 dv->devname, devname);
566 }
567 if (!tst->ss->external &&
568 array->major_version == 0 &&
569 md_get_version(fd)%100 < 2) {
570 if (ioctl(fd, HOT_ADD_DISK, rdev)==0) {
571 if (verbose >= 0)
572 pr_err("hot added %s\n",
573 dv->devname);
574 return 1;
575 }
576
577 pr_err("hot add failed for %s: %s\n",
578 dv->devname, strerror(errno));
579 return -1;
580 }
581
582 if (array->not_persistent == 0 || tst->ss->external) {
583
584 /* need to find a sample superblock to copy, and
585 * a spare slot to use.
586 * For 'external' array (well, container based),
587 * We can just load the metadata for the array->
588 */
589 int array_failed;
590 if (tst->sb)
591 /* already loaded */;
592 else if (tst->ss->external) {
593 tst->ss->load_container(tst, fd, NULL);
594 } else for (j = 0; j < tst->max_devs; j++) {
595 char *dev;
596 int dfd;
597 disc.number = j;
598 if (ioctl(fd, GET_DISK_INFO, &disc))
599 continue;
600 if (disc.major==0 && disc.minor==0)
601 continue;
602 if ((disc.state & 4)==0) /* sync */
603 continue;
604 /* Looks like a good device to try */
605 dev = map_dev(disc.major, disc.minor, 1);
606 if (!dev)
607 continue;
608 dfd = dev_open(dev, O_RDONLY);
609 if (dfd < 0)
610 continue;
611 if (tst->ss->load_super(tst, dfd,
612 NULL)) {
613 close(dfd);
614 continue;
615 }
616 close(dfd);
617 break;
618 }
619 /* FIXME this is a bad test to be using */
620 if (!tst->sb && dv->disposition != 'a') {
621 /* we are re-adding a device to a
622 * completely dead array - have to depend
623 * on kernel to check
624 */
625 } else if (!tst->sb) {
626 pr_err("cannot load array metadata from %s\n", devname);
627 return -1;
628 }
629
630 /* Make sure device is large enough */
387fcd59 631 if (tst->ss->avail_size(tst, ldsize/512, INVALID_SECTORS) <
38aeaf3a
N
632 array_size) {
633 if (dv->disposition == 'M')
634 return 0;
635 pr_err("%s not large enough to join array\n",
636 dv->devname);
637 return -1;
638 }
639
640 /* Possibly this device was recently part of
641 * the array and was temporarily removed, and
642 * is now being re-added. If so, we can
643 * simply re-add it.
644 */
645
646 if (array->not_persistent==0) {
647 dev_st = dup_super(tst);
648 dev_st->ss->load_super(dev_st, tfd, NULL);
649 }
650 if (dev_st && dev_st->sb) {
651 int rv = attempt_re_add(fd, tfd, dv,
652 dev_st, tst,
653 rdev,
654 update, devname,
655 verbose,
656 array);
657 dev_st->ss->free_super(dev_st);
658 if (rv)
659 return rv;
660 }
661 if (dv->disposition == 'M') {
662 if (verbose > 0)
663 pr_err("--re-add for %s to %s is not possible\n",
664 dv->devname, devname);
665 return 0;
666 }
667 if (dv->disposition == 'A') {
668 pr_err("--re-add for %s to %s is not possible\n",
669 dv->devname, devname);
670 return -1;
671 }
672 if (array->active_disks < array->raid_disks) {
673 char *avail = xcalloc(array->raid_disks, 1);
674 int d;
675 int found = 0;
676
677 for (d = 0; d < MAX_DISKS && found < array->active_disks; d++) {
678 disc.number = d;
679 if (ioctl(fd, GET_DISK_INFO, &disc))
680 continue;
681 if (disc.major == 0 && disc.minor == 0)
682 continue;
683 if (!(disc.state & (1<<MD_DISK_SYNC)))
684 continue;
685 avail[disc.raid_disk] = 1;
686 found++;
687 }
688 array_failed = !enough(array->level, array->raid_disks,
689 array->layout, 1, avail);
690 } else
691 array_failed = 0;
692 if (array_failed) {
693 pr_err("%s has failed so using --add cannot work and might destroy\n",
694 devname);
695 pr_err("data on %s. You should stop the array and re-assemble it.\n",
696 dv->devname);
697 return -1;
698 }
699 } else {
700 /* non-persistent. Must ensure that new drive
701 * is at least array->size big.
702 */
703 if (ldsize/512 < array_size) {
704 pr_err("%s not large enough to join array\n",
705 dv->devname);
706 return -1;
707 }
708 }
709 /* committed to really trying this device now*/
710 remove_partitions(tfd);
711
712 /* in 2.6.17 and earlier, version-1 superblocks won't
713 * use the number we write, but will choose a free number.
714 * we must choose the same free number, which requires
715 * starting at 'raid_disks' and counting up
716 */
717 for (j = array->raid_disks; j < tst->max_devs; j++) {
718 disc.number = j;
719 if (ioctl(fd, GET_DISK_INFO, &disc))
720 break;
721 if (disc.major==0 && disc.minor==0)
722 break;
723 if (disc.state & 8) /* removed */
724 break;
725 }
726 disc.major = major(rdev);
727 disc.minor = minor(rdev);
728 disc.number =j;
729 disc.state = 0;
730 if (array->not_persistent==0) {
731 int dfd;
732 if (dv->writemostly == 1)
733 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
734 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
735 if (tst->ss->add_to_super(tst, &disc, dfd,
72ca9bcf 736 dv->devname, INVALID_SECTORS))
38aeaf3a
N
737 return -1;
738 if (tst->ss->write_init_super(tst))
739 return -1;
740 } else if (dv->disposition == 'A') {
741 /* this had better be raid1.
742 * As we are "--re-add"ing we must find a spare slot
743 * to fill.
744 */
745 char *used = xcalloc(array->raid_disks, 1);
746 for (j = 0; j < tst->max_devs; j++) {
747 mdu_disk_info_t disc2;
748 disc2.number = j;
749 if (ioctl(fd, GET_DISK_INFO, &disc2))
750 continue;
751 if (disc2.major==0 && disc2.minor==0)
752 continue;
753 if (disc2.state & 8) /* removed */
754 continue;
755 if (disc2.raid_disk < 0)
756 continue;
757 if (disc2.raid_disk > array->raid_disks)
758 continue;
759 used[disc2.raid_disk] = 1;
760 }
761 for (j = 0 ; j < array->raid_disks; j++)
762 if (!used[j]) {
763 disc.raid_disk = j;
764 disc.state |= (1<<MD_DISK_SYNC);
765 break;
766 }
767 free(used);
768 }
769 if (dv->writemostly == 1)
770 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
771 if (tst->ss->external) {
772 /* add a disk
773 * to an external metadata container */
774 struct mdinfo new_mdi;
775 struct mdinfo *sra;
776 int container_fd;
4dd2df09 777 char devnm[32];
38aeaf3a
N
778 int dfd;
779
4dd2df09
N
780 strcpy(devnm, fd2devnm(fd));
781
782 container_fd = open_dev_excl(devnm);
38aeaf3a
N
783 if (container_fd < 0) {
784 pr_err("add failed for %s:"
785 " could not get exclusive access to container\n",
786 dv->devname);
787 tst->ss->free_super(tst);
788 return -1;
789 }
790
9cf9a1de 791 Kill(dv->devname, NULL, 0, -1, 0);
38aeaf3a 792 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
4dd2df09 793 if (mdmon_running(tst->container_devnm))
38aeaf3a
N
794 tst->update_tail = &tst->updates;
795 if (tst->ss->add_to_super(tst, &disc, dfd,
72ca9bcf 796 dv->devname, INVALID_SECTORS)) {
38aeaf3a
N
797 close(dfd);
798 close(container_fd);
799 return -1;
800 }
801 if (tst->update_tail)
802 flush_metadata_updates(tst);
803 else
804 tst->ss->sync_metadata(tst);
805
4dd2df09 806 sra = sysfs_read(container_fd, NULL, 0);
38aeaf3a
N
807 if (!sra) {
808 pr_err("add failed for %s: sysfs_read failed\n",
809 dv->devname);
810 close(container_fd);
811 tst->ss->free_super(tst);
812 return -1;
813 }
814 sra->array.level = LEVEL_CONTAINER;
815 /* Need to set data_offset and component_size */
816 tst->ss->getinfo_super(tst, &new_mdi, NULL);
817 new_mdi.disk.major = disc.major;
818 new_mdi.disk.minor = disc.minor;
819 new_mdi.recovery_start = 0;
820 /* Make sure fds are closed as they are O_EXCL which
821 * would block add_disk */
822 tst->ss->free_super(tst);
823 if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
824 pr_err("add new device to external metadata"
825 " failed for %s\n", dv->devname);
826 close(container_fd);
827 sysfs_free(sra);
828 return -1;
829 }
4dd2df09 830 ping_monitor(devnm);
38aeaf3a
N
831 sysfs_free(sra);
832 close(container_fd);
833 } else {
834 tst->ss->free_super(tst);
835 if (ioctl(fd, ADD_NEW_DISK, &disc)) {
836 pr_err("add new device failed for %s as %d: %s\n",
837 dv->devname, j, strerror(errno));
838 return -1;
839 }
840 }
841 if (verbose >= 0)
842 pr_err("added %s\n", dv->devname);
843 return 1;
844}
845
d070235d
N
846int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
847 int sysfd, unsigned long rdev, int verbose, char *devname)
848{
849 int lfd = -1;
850 int err;
851
852 if (tst->ss->external) {
853 /* To remove a device from a container, we must
854 * check that it isn't in use in an array.
855 * This involves looking in the 'holders'
856 * directory - there must be just one entry,
857 * the container.
858 * To ensure that it doesn't get used as a
859 * hot spare while we are checking, we
860 * get an O_EXCL open on the container
861 */
aab15415 862 int ret;
4dd2df09
N
863 char devnm[32];
864 strcpy(devnm, fd2devnm(fd));
865 lfd = open_dev_excl(devnm);
d070235d
N
866 if (lfd < 0) {
867 pr_err("Cannot get exclusive access "
868 " to container - odd\n");
869 return -1;
870 }
aab15415
N
871 /* We may not be able to check on holders in
872 * sysfs, either because we don't have the dev num
873 * (rdev == 0) or because the device has been detached
874 * and the 'holders' directory no longer exists
875 * (ret == -1). In that case, assume it is OK to
876 * remove.
d070235d 877 */
aab15415
N
878 if (rdev == 0)
879 ret = -1;
880 else
4dd2df09 881 ret = sysfs_unique_holder(devnm, rdev);
aab15415
N
882 if (ret == 0) {
883 pr_err("%s is not a member, cannot remove.\n",
884 dv->devname);
885 close(lfd);
886 return -1;
887 }
888 if (ret >= 2) {
889 pr_err("%s is still in use, cannot remove.\n",
890 dv->devname);
d070235d
N
891 close(lfd);
892 return -1;
893 }
894 }
895 /* FIXME check that it is a current member */
896 if (sysfd >= 0) {
897 /* device has been removed and we don't know
898 * the major:minor number
899 */
900 int n = write(sysfd, "remove", 6);
901 if (n != 6)
902 err = -1;
903 else
904 err = 0;
905 } else {
906 err = ioctl(fd, HOT_REMOVE_DISK, rdev);
907 if (err && errno == ENODEV) {
908 /* Old kernels rejected this if no personality
909 * is registered */
4dd2df09 910 struct mdinfo *sra = sysfs_read(fd, NULL, GET_DEVS);
d070235d
N
911 struct mdinfo *dv = NULL;
912 if (sra)
913 dv = sra->devs;
914 for ( ; dv ; dv=dv->next)
915 if (dv->disk.major == (int)major(rdev) &&
916 dv->disk.minor == (int)minor(rdev))
917 break;
918 if (dv)
919 err = sysfs_set_str(sra, dv,
920 "state", "remove");
921 else
922 err = -1;
923 if (sra)
924 sysfs_free(sra);
925 }
926 }
927 if (err) {
928 pr_err("hot remove failed "
929 "for %s: %s\n", dv->devname,
930 strerror(errno));
931 if (lfd >= 0)
932 close(lfd);
933 return -1;
934 }
935 if (tst->ss->external) {
936 /*
937 * Before dropping our exclusive open we make an
938 * attempt at preventing mdmon from seeing an
939 * 'add' event before reconciling this 'remove'
940 * event.
941 */
4dd2df09 942 char *devnm = fd2devnm(fd);
d070235d 943
4dd2df09 944 if (!devnm) {
d070235d
N
945 pr_err("unable to get container name\n");
946 return -1;
947 }
948
4dd2df09 949 ping_manager(devnm);
d070235d
N
950 }
951 if (lfd >= 0)
952 close(lfd);
953 if (verbose >= 0)
954 pr_err("hot removed %s from %s\n",
955 dv->devname, devname);
956 return 1;
957}
958
70c55e36
N
959int Manage_replace(struct supertype *tst, int fd, struct mddev_dev *dv,
960 unsigned long rdev, int verbose, char *devname)
961{
962 struct mdinfo *mdi, *di;
963 if (tst->ss->external) {
964 pr_err("--replace only supported for native metadata (0.90 or 1.x)\n");
965 return -1;
966 }
967 /* Need to find the device in sysfs and add 'want_replacement' to the
968 * status.
969 */
4dd2df09 970 mdi = sysfs_read(fd, NULL, GET_DEVS);
70c55e36
N
971 if (!mdi || !mdi->devs) {
972 pr_err("Cannot find status of %s to enable replacement - strange\n",
973 devname);
974 return -1;
975 }
976 for (di = mdi->devs; di; di = di->next)
977 if (di->disk.major == (int)major(rdev) &&
978 di->disk.minor == (int)minor(rdev))
979 break;
980 if (di) {
981 int rv;
982 if (di->disk.raid_disk < 0) {
983 pr_err("%s is not active and so cannot be replaced.\n",
984 dv->devname);
985 sysfs_free(mdi);
986 return -1;
987 }
988 rv = sysfs_set_str(mdi, di,
989 "state", "want_replacement");
990 if (rv) {
991 sysfs_free(mdi);
992 pr_err("Failed to request replacement for %s\n",
993 dv->devname);
994 return -1;
995 }
996 if (verbose >= 0)
997 pr_err("Marked %s (device %d in %s) for replacement\n",
998 dv->devname, di->disk.raid_disk, devname);
999 /* If there is a matching 'with', we need to tell it which
1000 * raid disk
1001 */
1002 while (dv && dv->disposition != 'W')
1003 dv = dv->next;
1004 if (dv) {
1005 dv->disposition = 'w';
1006 dv->used = di->disk.raid_disk;
1007 }
1008 return 1;
1009 }
1010 sysfs_free(mdi);
1011 pr_err("%s not found in %s so cannot --replace it\n",
1012 dv->devname, devname);
1013 return -1;
1014}
1015
1016int Manage_with(struct supertype *tst, int fd, struct mddev_dev *dv,
1017 unsigned long rdev, int verbose, char *devname)
1018{
1019 struct mdinfo *mdi, *di;
1020 /* try to set 'slot' for 'rdev' in 'fd' to 'dv->used' */
4dd2df09 1021 mdi = sysfs_read(fd, NULL, GET_DEVS|GET_STATE);
70c55e36
N
1022 if (!mdi || !mdi->devs) {
1023 pr_err("Cannot find status of %s to enable replacement - strange\n",
1024 devname);
1025 return -1;
1026 }
1027 for (di = mdi->devs; di; di = di->next)
1028 if (di->disk.major == (int)major(rdev) &&
1029 di->disk.minor == (int)minor(rdev))
1030 break;
1031 if (di) {
1032 int rv;
1033 if (di->disk.state & (1<<MD_DISK_FAULTY)) {
1034 pr_err("%s is faulty and cannot be a replacement\n",
1035 dv->devname);
1036 sysfs_free(mdi);
1037 return -1;
1038 }
1039 if (di->disk.raid_disk >= 0) {
1040 pr_err("%s is active and cannot be a replacement\n",
1041 dv->devname);
1042 sysfs_free(mdi);
1043 return -1;
1044 }
1045 rv = sysfs_set_num(mdi, di,
1046 "slot", dv->used);
1047 if (rv) {
1048 sysfs_free(mdi);
1049 pr_err("Failed to %s as preferred replacement.\n",
1050 dv->devname);
1051 return -1;
1052 }
1053 if (verbose >= 0)
1054 pr_err("Marked %s in %s as replacement for device %d\n",
1055 dv->devname, devname, dv->used);
1056 return 1;
1057 }
1058 sysfs_free(mdi);
1059 pr_err("%s not found in %s so cannot make it preferred replacement\n",
1060 dv->devname, devname);
1061 return -1;
1062}
1063
64c4757e 1064int Manage_subdevs(char *devname, int fd,
833bb0f8 1065 struct mddev_dev *devlist, int verbose, int test,
11b391ec 1066 char *update, int force)
cd29a5c8 1067{
7bd04da9 1068 /* Do something to each dev.
682c7051
NB
1069 * devmode can be
1070 * 'a' - add the device
1071 * try HOT_ADD_DISK
1072 * If that fails EINVAL, try ADD_NEW_DISK
7bd04da9
N
1073 * 'A' - re-add the device
1074 * 'r' - remove the device: HOT_REMOVE_DISK
b80da661
NB
1075 * device can be 'faulty' or 'detached' in which case all
1076 * matching devices are removed.
682c7051 1077 * 'f' - set the device faulty SET_DISK_FAULTY
b80da661
NB
1078 * device can be 'detached' in which case any device that
1079 * is inaccessible will be marked faulty.
70c55e36
N
1080 * 'R' - mark this device as wanting replacement.
1081 * 'W' - this device is added if necessary and activated as
1082 * a replacement for a previous 'R' device.
1083 * -----
1084 * 'w' - 'W' will be changed to 'w' when it is paired with
1085 * a 'R' device. If a 'W' is found while walking the list
1086 * it must be unpaired, and is an error.
1087 * 'M' - this is created by a 'missing' target. It is a slight
1088 * variant on 'A'
262e3b7f
N
1089 * 'F' - Another variant of 'A', where the device was faulty
1090 * so must be removed from the array first.
70c55e36 1091 *
98d27e39
N
1092 * For 'f' and 'r', the device can also be a kernel-internal
1093 * name such as 'sdb'.
682c7051
NB
1094 */
1095 mdu_array_info_t array;
7a3be72f 1096 unsigned long long array_size;
1d997643 1097 struct mddev_dev *dv;
682c7051 1098 struct stat stb;
cfad27a9 1099 int tfd = -1;
38aeaf3a 1100 struct supertype *tst;
4725bc31 1101 char *subarray = NULL;
98d27e39 1102 int sysfd = -1;
7d2e6486 1103 int count = 0; /* number of actions taken */
9f584691
N
1104 struct mdinfo info;
1105 int frozen = 0;
8af530b0 1106 int busy = 0;
682c7051
NB
1107
1108 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
7bd04da9 1109 pr_err("Cannot get array info for %s\n",
682c7051 1110 devname);
bcbb3112 1111 goto abort;
682c7051 1112 }
4dd2df09 1113 sysfs_init(&info, fd, NULL);
3da92f27 1114
7bd04da9 1115 /* array.size is only 32 bits and may be truncated.
7a3be72f
NB
1116 * So read from sysfs if possible, and record number of sectors
1117 */
1118
1119 array_size = get_component_size(fd);
1120 if (array_size <= 0)
1121 array_size = array.size * 2;
1122
4725bc31 1123 tst = super_by_fd(fd, &subarray);
3da92f27 1124 if (!tst) {
e7b84f9d 1125 pr_err("unsupport array - version %d.%d\n",
3da92f27 1126 array.major_version, array.minor_version);
bcbb3112 1127 goto abort;
3da92f27
NB
1128 }
1129
b3b4e8a7 1130 stb.st_rdev = 0;
38aeaf3a 1131 for (dv = devlist; dv; dv = dv->next) {
38aeaf3a 1132 int rv;
4a39c6f2 1133
1d997643
N
1134 if (strcmp(dv->devname, "failed") == 0 ||
1135 strcmp(dv->devname, "faulty") == 0) {
262e3b7f
N
1136 if (dv->disposition != 'A'
1137 && dv->disposition != 'r') {
e7b84f9d 1138 pr_err("%s only meaningful "
262e3b7f 1139 "with -r or --re-add, not -%c\n",
b80da661 1140 dv->devname, dv->disposition);
bcbb3112 1141 goto abort;
b80da661 1142 }
262e3b7f
N
1143 add_faulty(dv, fd, (dv->disposition == 'A'
1144 ? 'F' : 'r'));
1d997643
N
1145 continue;
1146 }
1147 if (strcmp(dv->devname, "detached") == 0) {
b80da661 1148 if (dv->disposition != 'r' && dv->disposition != 'f') {
e7b84f9d 1149 pr_err("%s only meaningful "
b80da661
NB
1150 "with -r of -f, not -%c\n",
1151 dv->devname, dv->disposition);
bcbb3112 1152 goto abort;
b80da661 1153 }
1d997643
N
1154 add_detached(dv, fd, dv->disposition);
1155 continue;
1156 }
1157
1158 if (strcmp(dv->devname, "missing") == 0) {
1159 struct mddev_dev *add_devlist = NULL;
1160 struct mddev_dev **dp;
c8e1a230 1161 if (dv->disposition != 'A') {
e7b84f9d 1162 pr_err("'missing' only meaningful "
aab15415 1163 "with --re-add\n");
bcbb3112 1164 goto abort;
a4e13010 1165 }
1d997643 1166 add_devlist = conf_get_devs();
a4e13010 1167 if (add_devlist == NULL) {
e7b84f9d 1168 pr_err("no devices to scan for missing members.");
a4e13010
N
1169 continue;
1170 }
1d997643 1171 for (dp = &add_devlist; *dp; dp = & (*dp)->next)
7bd04da9 1172 /* 'M' (for 'missing') is like 'A' without errors */
1d997643
N
1173 (*dp)->disposition = 'M';
1174 *dp = dv->next;
1175 dv->next = add_devlist;
1176 continue;
1177 }
1178
1179 if (strchr(dv->devname, '/') == NULL &&
aab15415
N
1180 strchr(dv->devname, ':') == NULL &&
1181 strlen(dv->devname) < 50) {
98d27e39
N
1182 /* Assume this is a kernel-internal name like 'sda1' */
1183 int found = 0;
1184 char dname[55];
1185 if (dv->disposition != 'r' && dv->disposition != 'f') {
e7b84f9d 1186 pr_err("%s only meaningful "
cfad27a9 1187 "with -r or -f, not -%c\n",
98d27e39 1188 dv->devname, dv->disposition);
bcbb3112 1189 goto abort;
98d27e39
N
1190 }
1191
1192 sprintf(dname, "dev-%s", dv->devname);
4dd2df09 1193 sysfd = sysfs_open(fd2devnm(fd), dname, "block/dev");
98d27e39
N
1194 if (sysfd >= 0) {
1195 char dn[20];
1196 int mj,mn;
1197 if (sysfs_fd_get_str(sysfd, dn, 20) > 0 &&
1198 sscanf(dn, "%d:%d", &mj,&mn) == 2) {
1199 stb.st_rdev = makedev(mj,mn);
1200 found = 1;
1201 }
1202 close(sysfd);
1203 sysfd = -1;
1204 }
1205 if (!found) {
4dd2df09 1206 sysfd = sysfs_open(fd2devnm(fd), dname, "state");
98d27e39 1207 if (sysfd < 0) {
e7b84f9d 1208 pr_err("%s does not appear "
98d27e39
N
1209 "to be a component of %s\n",
1210 dv->devname, devname);
bcbb3112 1211 goto abort;
98d27e39
N
1212 }
1213 }
b80da661 1214 } else {
c7b47447 1215 tfd = dev_open(dv->devname, O_RDONLY);
5fe7f5f7
N
1216 if (tfd >= 0)
1217 fstat(tfd, &stb);
5a9de8db 1218 else {
5fe7f5f7
N
1219 int open_err = errno;
1220 if (stat(dv->devname, &stb) != 0) {
1221 pr_err("Cannot find %s: %s\n",
1222 dv->devname, strerror(errno));
1223 goto abort;
1224 }
1225 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
1226 if (dv->disposition == 'M')
1227 /* non-fatal. Also improbable */
1228 continue;
1229 pr_err("%s is not a block device.\n",
1230 dv->devname);
1231 goto abort;
1232 }
1233 if (dv->disposition == 'r')
1234 /* Be happy, the stat worked, that is
1235 * enough for --remove
1236 */
1237 ;
1238 else {
1d997643
N
1239 if (dv->disposition == 'M')
1240 /* non-fatal */
1241 continue;
839f27a3 1242 pr_err("Cannot open %s: %s\n",
5fe7f5f7 1243 dv->devname, strerror(open_err));
bcbb3112 1244 goto abort;
5a9de8db 1245 }
b80da661 1246 }
682c7051 1247 }
cd29a5c8 1248 switch(dv->disposition){
682c7051 1249 default:
e7b84f9d 1250 pr_err("internal error - devmode[%s]=%d\n",
c913b90e 1251 dv->devname, dv->disposition);
bcbb3112 1252 goto abort;
682c7051 1253 case 'a':
c8e1a230 1254 case 'A':
262e3b7f
N
1255 case 'M': /* --re-add missing */
1256 case 'F': /* --re-add faulty */
4a39c6f2 1257 /* add the device */
4725bc31 1258 if (subarray) {
e7b84f9d 1259 pr_err("Cannot add disks to a"
f7dd881f
DW
1260 " \'member\' array, perform this"
1261 " operation on the parent container\n");
bcbb3112 1262 goto abort;
f94d52f4 1263 }
262e3b7f
N
1264 if (dv->disposition == 'F')
1265 /* Need to remove first */
1266 ioctl(fd, HOT_REMOVE_DISK,
1267 (unsigned long)stb.st_rdev);
f277ce36 1268 /* Make sure it isn't in use (in 2.6 or later) */
1d997643 1269 tfd = dev_open(dv->devname, O_RDONLY|O_EXCL);
38aeaf3a
N
1270 if (tfd >= 0) {
1271 /* We know no-one else is using it. We'll
1272 * need non-exclusive access to add it, so
1273 * do that now.
1274 */
1275 close(tfd);
1276 tfd = dev_open(dv->devname, O_RDONLY);
1277 }
0fbf459d 1278 if (tfd < 0) {
1d997643
N
1279 if (dv->disposition == 'M')
1280 continue;
e7b84f9d 1281 pr_err("Cannot open %s: %s\n",
d7eaf49f 1282 dv->devname, strerror(errno));
bcbb3112 1283 goto abort;
d7eaf49f 1284 }
9f584691
N
1285 if (!frozen) {
1286 if (sysfs_freeze_array(&info) == 1)
1287 frozen = 1;
1288 else
1289 frozen = -1;
1290 }
38aeaf3a
N
1291 rv = Manage_add(fd, tfd, dv, tst, &array,
1292 force, verbose, devname, update,
1293 stb.st_rdev, array_size);
1294 close(tfd);
1295 tfd = -1;
1296 if (rv < 0)
bcbb3112 1297 goto abort;
38aeaf3a
N
1298 if (rv > 0)
1299 count++;
682c7051
NB
1300 break;
1301
1302 case 'r':
1303 /* hot remove */
4725bc31 1304 if (subarray) {
e7b84f9d 1305 pr_err("Cannot remove disks from a"
f7dd881f
DW
1306 " \'member\' array, perform this"
1307 " operation on the parent container\n");
d070235d
N
1308 rv = -1;
1309 } else
1310 rv = Manage_remove(tst, fd, dv, sysfd,
1311 stb.st_rdev, verbose,
1312 devname);
1313 if (sysfd >= 0)
98d27e39 1314 close(sysfd);
d070235d
N
1315 sysfd = -1;
1316 if (rv < 0)
bcbb3112 1317 goto abort;
d070235d
N
1318 if (rv > 0)
1319 count++;
682c7051
NB
1320 break;
1321
1322 case 'f': /* set faulty */
1323 /* FIXME check current member */
98d27e39
N
1324 if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
1325 (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
1326 (unsigned long) stb.st_rdev))) {
8af530b0
N
1327 if (errno == EBUSY)
1328 busy = 1;
e7b84f9d 1329 pr_err("set device faulty failed for %s: %s\n",
1d997643 1330 dv->devname, strerror(errno));
98d27e39
N
1331 if (sysfd >= 0)
1332 close(sysfd);
bcbb3112 1333 goto abort;
682c7051 1334 }
98d27e39
N
1335 if (sysfd >= 0)
1336 close(sysfd);
1337 sysfd = -1;
7d2e6486 1338 count++;
dab6685f 1339 if (verbose >= 0)
e7b84f9d 1340 pr_err("set %s faulty in %s\n",
1d997643 1341 dv->devname, devname);
682c7051 1342 break;
70c55e36
N
1343 case 'R': /* Mark as replaceable */
1344 if (subarray) {
1345 pr_err("Cannot replace disks in a"
1346 " \'member\' array, perform this"
1347 " operation on the parent container\n");
1348 rv = -1;
1349 } else {
1350 if (!frozen) {
1351 if (sysfs_freeze_array(&info) == 1)
1352 frozen = 1;
1353 else
1354 frozen = -1;
1355 }
1356 rv = Manage_replace(tst, fd, dv,
1357 stb.st_rdev, verbose,
1358 devname);
1359 }
1360 if (rv < 0)
1361 goto abort;
1362 if (rv > 0)
1363 count++;
1364 break;
1365 case 'W': /* --with device that doesn't match */
1366 pr_err("No matching --replace device for --with %s\n",
1367 dv->devname);
1368 goto abort;
1369 case 'w': /* --with device which was matched */
1370 rv = Manage_with(tst, fd, dv,
1371 stb.st_rdev, verbose, devname);
1372 if (rv < 0)
1373 goto abort;
1374 break;
682c7051
NB
1375 }
1376 }
9f584691
N
1377 if (frozen > 0)
1378 sysfs_set_str(&info, NULL, "sync_action","idle");
7d2e6486
N
1379 if (test && count == 0)
1380 return 2;
682c7051 1381 return 0;
bcbb3112
N
1382
1383abort:
9f584691
N
1384 if (frozen > 0)
1385 sysfs_set_str(&info, NULL, "sync_action","idle");
8af530b0 1386 return !test && busy ? 2 : 1;
64c4757e 1387}
1f48664b
NB
1388
1389int autodetect(void)
1390{
1391 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1392 int rv = 1;
1393 int fd = dev_open("9:0", O_RDONLY);
1394 if (fd >= 0) {
1395 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
1396 rv = 0;
1397 close(fd);
1398 }
1399 return rv;
1400}
aa534678 1401
ba728be7 1402int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int verbose)
aa534678
DW
1403{
1404 struct supertype supertype, *st = &supertype;
1405 int fd, rv = 2;
1406
1407 memset(st, 0, sizeof(*st));
aa534678 1408
ba728be7 1409 fd = open_subarray(dev, subarray, st, verbose < 0);
aa534678
DW
1410 if (fd < 0)
1411 return 2;
1412
1413 if (!st->ss->update_subarray) {
ba728be7 1414 if (verbose >= 0)
e7b84f9d
N
1415 pr_err("Operation not supported for %s metadata\n",
1416 st->ss->name);
aa534678
DW
1417 goto free_super;
1418 }
1419
4dd2df09 1420 if (mdmon_running(st->devnm))
aa534678
DW
1421 st->update_tail = &st->updates;
1422
a951a4f7 1423 rv = st->ss->update_subarray(st, subarray, update, ident);
aa534678
DW
1424
1425 if (rv) {
ba728be7 1426 if (verbose >= 0)
e7b84f9d 1427 pr_err("Failed to update %s of subarray-%s in %s\n",
aa534678
DW
1428 update, subarray, dev);
1429 } else if (st->update_tail)
1430 flush_metadata_updates(st);
1431 else
1432 st->ss->sync_metadata(st);
1433
ba728be7 1434 if (rv == 0 && strcmp(update, "name") == 0 && verbose >= 0)
e7b84f9d
N
1435 pr_err("Updated subarray-%s name from %s, UUIDs may have changed\n",
1436 subarray, dev);
aa534678
DW
1437
1438 free_super:
1439 st->ss->free_super(st);
1440 close(fd);
1441
1442 return rv;
1443}
d52bb542 1444
7bd04da9
N
1445/* Move spare from one array to another If adding to destination array fails
1446 * add back to original array.
d52bb542
AC
1447 * Returns 1 on success, 0 on failure */
1448int move_spare(char *from_devname, char *to_devname, dev_t devid)
1449{
1450 struct mddev_dev devlist;
1451 char devname[20];
1452
1453 /* try to remove and add */
1454 int fd1 = open(to_devname, O_RDONLY);
1455 int fd2 = open(from_devname, O_RDONLY);
1456
1457 if (fd1 < 0 || fd2 < 0) {
1458 if (fd1>=0) close(fd1);
1459 if (fd2>=0) close(fd2);
1460 return 0;
1461 }
1462
1463 devlist.next = NULL;
1464 devlist.used = 0;
d52bb542
AC
1465 devlist.writemostly = 0;
1466 devlist.devname = devname;
1467 sprintf(devname, "%d:%d", major(devid), minor(devid));
1468
1469 devlist.disposition = 'r';
11b391ec 1470 if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0) == 0) {
d52bb542 1471 devlist.disposition = 'a';
11b391ec 1472 if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL, 0) == 0) {
d52bb542
AC
1473 /* make sure manager is aware of changes */
1474 ping_manager(to_devname);
1475 ping_manager(from_devname);
1476 close(fd1);
1477 close(fd2);
1478 return 1;
1479 }
11b391ec 1480 else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0);
d52bb542
AC
1481 }
1482 close(fd1);
1483 close(fd2);
1484 return 0;
1485}
435d4ebb 1486#endif