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