Check all member devices in enough_fd
[thirdparty/mdadm.git] / Manage.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
e736b623 4 * Copyright (C) 2001-2009 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
aba69144 47
682c7051
NB
48 if (md_get_version(fd) < 9000) {
49 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
50 return 1;
51 }
0e600426 52#ifndef MDASSEMBLE
e9dd1598
N
53 /* If this is an externally-manage array, we need to modify the
54 * metadata_version so that mdmon doesn't undo our change.
55 */
56 mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
57 if (mdi &&
58 mdi->array.major_version == -1 &&
e9dd1598
N
59 is_subarray(mdi->text_version)) {
60 char vers[64];
61 strcpy(vers, "external:");
62 strcat(vers, mdi->text_version);
63 if (readonly > 0) {
64 int rv;
65 /* We set readonly ourselves. */
66 vers[9] = '-';
67 sysfs_set_str(mdi, NULL, "metadata_version", vers);
68
69 close(fd);
70 rv = sysfs_set_str(mdi, NULL, "array_state", "readonly");
71
72 if (rv < 0) {
73 fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
74 devname, strerror(errno));
75
76 vers[9] = mdi->text_version[0];
77 sysfs_set_str(mdi, NULL, "metadata_version", vers);
78 return 1;
79 }
80 } else {
81 char *cp;
82 /* We cannot set read/write - must signal mdmon */
83 vers[9] = '/';
84 sysfs_set_str(mdi, NULL, "metadata_version", vers);
85
86 cp = strchr(vers+10, '/');
87 if (*cp)
88 *cp = 0;
89 ping_monitor(vers+10);
9ea5a252
DW
90 if (mdi->array.level <= 0)
91 sysfs_set_str(mdi, NULL, "array_state", "active");
e9dd1598
N
92 }
93 return 0;
94 }
0e600426 95#endif
682c7051
NB
96 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
97 fprintf(stderr, Name ": %s does not appear to be active.\n",
98 devname);
99 return 1;
100 }
aba69144 101
682c7051
NB
102 if (readonly>0) {
103 if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
104 fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
105 devname, strerror(errno));
106 return 1;
107 }
108 } else if (readonly < 0) {
109 if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
82b27616 110 fprintf(stderr, Name ": failed to set writable for %s: %s\n",
682c7051
NB
111 devname, strerror(errno));
112 return 1;
113 }
114 }
aba69144 115 return 0;
64c4757e
NB
116}
117
435d4ebb
LB
118#ifndef MDASSEMBLE
119
4ccad7b1
N
120static void remove_devices(int devnum, char *path)
121{
b1702f48
N
122 /*
123 * Remove names at 'path' - possibly with
124 * partition suffixes - which link to the 'standard'
125 * name for devnum. These were probably created
126 * by mdadm when the array was assembled.
4ccad7b1
N
127 */
128 char base[40];
129 char *path2;
130 char link[1024];
131 int n;
132 int part;
133 char *be;
134 char *pe;
135
b1702f48
N
136 if (!path)
137 return;
138
4ccad7b1
N
139 if (devnum >= 0)
140 sprintf(base, "/dev/md%d", devnum);
141 else
142 sprintf(base, "/dev/md_d%d", -1-devnum);
143 be = base + strlen(base);
b1702f48
N
144
145 path2 = malloc(strlen(path)+20);
146 strcpy(path2, path);
147 pe = path2 + strlen(path2);
4ccad7b1
N
148
149 for (part = 0; part < 16; part++) {
150 if (part) {
151 sprintf(be, "p%d", part);
b1702f48
N
152
153 if (isdigit(pe[-1]))
154 sprintf(pe, "p%d", part);
155 else
156 sprintf(pe, "%d", part);
4ccad7b1 157 }
b1702f48
N
158 n = readlink(path2, link, sizeof(link));
159 if (n && (int)strlen(base) == n &&
160 strncmp(link, base, n) == 0)
161 unlink(path2);
4ccad7b1 162 }
0eb26465 163 free(path2);
4ccad7b1
N
164}
165
166
91f068bf 167int Manage_runstop(char *devname, int fd, int runstop, int quiet)
64c4757e 168{
682c7051
NB
169 /* Run or stop the array. array must already be configured
170 * required >= 0.90.0
4b9d39fc
N
171 * Only print failure messages if quiet == 0;
172 * quiet > 0 means really be quiet
173 * quiet < 0 means we will try again if it fails.
682c7051 174 */
682c7051 175 mdu_param_t param; /* unused */
82b27616
NB
176
177 if (runstop == -1 && md_get_version(fd) < 9000) {
178 if (ioctl(fd, STOP_MD, 0)) {
4b9d39fc
N
179 if (quiet == 0) fprintf(stderr,
180 Name ": stopping device %s "
181 "failed: %s\n",
182 devname, strerror(errno));
82b27616
NB
183 return 1;
184 }
185 }
aba69144 186
682c7051
NB
187 if (md_get_version(fd) < 9000) {
188 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
189 return 1;
190 }
82b27616 191 /*
682c7051
NB
192 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
193 fprintf(stderr, Name ": %s does not appear to be active.\n",
194 devname);
195 return 1;
196 }
82b27616 197 */
682c7051
NB
198 if (runstop>0) {
199 if (ioctl(fd, RUN_ARRAY, &param)) {
200 fprintf(stderr, Name ": failed to run array %s: %s\n",
201 devname, strerror(errno));
202 return 1;
203 }
8382f19b
NB
204 if (quiet <= 0)
205 fprintf(stderr, Name ": started %s\n", devname);
682c7051 206 } else if (runstop < 0){
8382f19b
NB
207 struct map_ent *map = NULL;
208 struct stat stb;
daf7a3ce 209 struct mdinfo *mdi;
4ccad7b1 210 int devnum;
eb0af526
N
211 int err;
212 int count;
daf7a3ce
NB
213 /* If this is an mdmon managed array, just write 'inactive'
214 * to the array state and let mdmon clear up.
215 */
4ccad7b1 216 devnum = fd2devnum(fd);
eb0af526
N
217 /* Get EXCL access first. If this fails, then attempting
218 * to stop is probably a bad idea.
219 */
220 close(fd);
221 fd = open(devname, O_RDONLY|O_EXCL);
222 if (fd < 0 || fd2devnum(fd) != devnum) {
223 if (fd >= 0)
224 close(fd);
225 fprintf(stderr,
226 Name ": Cannot get exclusive access to %s:"
1ae42d9d
KW
227 "Perhaps a running "
228 "process, mounted filesystem "
229 "or active volume group?\n",
eb0af526
N
230 devname);
231 return 1;
232 }
daf7a3ce
NB
233 mdi = sysfs_read(fd, -1, GET_LEVEL|GET_VERSION);
234 if (mdi &&
235 mdi->array.level > 0 &&
3c558363 236 is_subarray(mdi->text_version)) {
1ae42d9d 237 int err;
daf7a3ce
NB
238 /* This is mdmon managed. */
239 close(fd);
1ae42d9d
KW
240
241 count = 25;
242 while (count &&
243 (err = sysfs_set_str(mdi, NULL,
244 "array_state",
245 "inactive")) < 0
246 && errno == EBUSY) {
247 usleep(200000);
248 count--;
249 }
250 if (err && !quiet) {
251 fprintf(stderr, Name
252 ": failed to stop array %s: %s\n",
253 devname, strerror(errno));
daf7a3ce
NB
254 return 1;
255 }
256
257 /* Give monitor a chance to act */
c94709e8 258 ping_monitor(mdi->text_version);
daf7a3ce 259
eb0af526
N
260 fd = open_dev_excl(devnum);
261 if (fd < 0) {
262 fprintf(stderr, Name
263 ": failed to completely stop %s"
264 ": Device is busy\n",
265 devname);
266 return 1;
267 }
ada6c239
N
268 } else if (mdi &&
269 mdi->array.major_version == -1 &&
270 mdi->array.minor_version == -2 &&
3c558363 271 !is_subarray(mdi->text_version)) {
430ea469 272 struct mdstat_ent *mds, *m;
ada6c239
N
273 /* container, possibly mdmon-managed.
274 * Make sure mdmon isn't opening it, which
275 * would interfere with the 'stop'
276 */
277 ping_monitor(mdi->sys_name);
430ea469
N
278
279 /* now check that there are no existing arrays
280 * which are members of this array
281 */
282 mds = mdstat_read(0, 0);
283 for (m=mds; m; m=m->next)
284 if (m->metadata_version &&
285 strncmp(m->metadata_version, "external:", 9)==0 &&
286 is_subarray(m->metadata_version+9) &&
287 devname2devnum(m->metadata_version+10) == devnum) {
288 if (!quiet)
289 fprintf(stderr, Name
290 ": Cannot stop container %s: "
291 "member %s still active\n",
292 devname, m->dev);
293 free_mdstat(mds);
294 if (mdi)
295 sysfs_free(mdi);
296 return 1;
297 }
daf7a3ce 298 }
daf7a3ce 299
eb0af526
N
300 /* As we have an O_EXCL open, any use of the device
301 * which blocks STOP_ARRAY is probably a transient use,
302 * so it is reasonable to retry for a while - 5 seconds.
303 */
fb0d4b9c 304 count = 25; err = 0;
eb0af526
N
305 while (count && fd >= 0
306 && (err = ioctl(fd, STOP_ARRAY, NULL)) < 0
307 && errno == EBUSY) {
308 usleep(200000);
309 count --;
310 }
311 if (fd >= 0 && err) {
37ea3936 312 if (quiet == 0) {
daf7a3ce 313 fprintf(stderr, Name
4b9d39fc 314 ": failed to stop array %s: %s\n",
91f068bf 315 devname, strerror(errno));
d927f3c4
NB
316 if (errno == EBUSY)
317 fprintf(stderr, "Perhaps a running "
318 "process, mounted filesystem "
319 "or active volume group?\n");
320 }
97590376
N
321 if (mdi)
322 sysfs_free(mdi);
682c7051
NB
323 return 1;
324 }
97590376 325 /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array
d49410d3
N
326 * was stopped, so We'll do it here just to be sure. Drop any
327 * partitions as well...
97590376 328 */
d49410d3
N
329 if (fd >= 0)
330 ioctl(fd, BLKRRPART, 0);
97590376
N
331 if (mdi)
332 sysfs_uevent(mdi, "change");
daf7a3ce 333
4ccad7b1
N
334
335 if (devnum != NoMdDev &&
336 (stat("/dev/.udev", &stb) != 0 ||
337 check_env("MDADM_NO_UDEV"))) {
338 struct map_ent *mp = map_by_devnum(&map, devnum);
339 remove_devices(devnum, mp ? mp->path : NULL);
340 }
341
342
ab56093f
NB
343 if (quiet <= 0)
344 fprintf(stderr, Name ": stopped %s\n", devname);
4eb26970
DW
345 map_lock(&map);
346 map_remove(&map, devnum);
347 map_unlock(&map);
682c7051
NB
348 }
349 return 0;
64c4757e
NB
350}
351
dd0781e5
NB
352int Manage_resize(char *devname, int fd, long long size, int raid_disks)
353{
354 mdu_array_info_t info;
355 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
356 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
357 devname, strerror(errno));
358 return 1;
359 }
360 if (size >= 0)
361 info.size = size;
362 if (raid_disks > 0)
363 info.raid_disks = raid_disks;
364 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
365 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
366 devname, strerror(errno));
367 return 1;
368 }
369 return 0;
b5e64645 370}
dd0781e5 371
64c4757e 372int Manage_subdevs(char *devname, int fd,
833bb0f8
N
373 struct mddev_dev *devlist, int verbose, int test,
374 char *update)
cd29a5c8 375{
682c7051
NB
376 /* do something to each dev.
377 * devmode can be
378 * 'a' - add the device
379 * try HOT_ADD_DISK
380 * If that fails EINVAL, try ADD_NEW_DISK
381 * 'r' - remove the device HOT_REMOVE_DISK
b80da661
NB
382 * device can be 'faulty' or 'detached' in which case all
383 * matching devices are removed.
682c7051 384 * 'f' - set the device faulty SET_DISK_FAULTY
b80da661
NB
385 * device can be 'detached' in which case any device that
386 * is inaccessible will be marked faulty.
98d27e39
N
387 * For 'f' and 'r', the device can also be a kernel-internal
388 * name such as 'sdb'.
682c7051 389 */
a655e550 390 struct mddev_dev *add_devlist = NULL;
682c7051
NB
391 mdu_array_info_t array;
392 mdu_disk_info_t disc;
7a3be72f 393 unsigned long long array_size;
a655e550 394 struct mddev_dev *dv, *next = NULL;
682c7051 395 struct stat stb;
b80da661 396 int j, jnext = 0;
cfad27a9 397 int tfd = -1;
3da92f27 398 struct supertype *st, *tst;
4725bc31 399 char *subarray = NULL;
fe80f49b
NB
400 int duuid[4];
401 int ouuid[4];
f94d52f4 402 int lfd = -1;
98d27e39 403 int sysfd = -1;
7d2e6486 404 int count = 0; /* number of actions taken */
682c7051
NB
405
406 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
407 fprintf(stderr, Name ": cannot get array info for %s\n",
408 devname);
409 return 1;
410 }
3da92f27 411
7a3be72f
NB
412 /* array.size is only 32 bit and may be truncated.
413 * So read from sysfs if possible, and record number of sectors
414 */
415
416 array_size = get_component_size(fd);
417 if (array_size <= 0)
418 array_size = array.size * 2;
419
4725bc31 420 tst = super_by_fd(fd, &subarray);
3da92f27
NB
421 if (!tst) {
422 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
423 array.major_version, array.minor_version);
424 return 1;
425 }
426
b3b4e8a7 427 stb.st_rdev = 0;
b80da661 428 for (dv = devlist, j=0 ; dv; dv = next, j = jnext) {
4a39c6f2 429 unsigned long long ldsize;
b80da661
NB
430 char dvname[20];
431 char *dnprintable = dv->devname;
a4e13010 432 char *add_dev = dv->devname;
f94d52f4 433 int err;
8453e704 434 int re_add_failed = 0;
4a39c6f2 435
b80da661
NB
436 next = dv->next;
437 jnext = 0;
438
439 if (strcmp(dv->devname, "failed")==0 ||
440 strcmp(dv->devname, "faulty")==0) {
9e6d9291 441 int remaining_disks = array.nr_disks;
b80da661
NB
442 if (dv->disposition != 'r') {
443 fprintf(stderr, Name ": %s only meaningful "
444 "with -r, not -%c\n",
445 dv->devname, dv->disposition);
446 return 1;
447 }
9e6d9291 448 for (; j < 1024 && remaining_disks > 0; j++) {
f21e18ca 449 unsigned dev;
b80da661
NB
450 disc.number = j;
451 if (ioctl(fd, GET_DISK_INFO, &disc))
452 continue;
453 if (disc.major == 0 && disc.minor == 0)
454 continue;
9e6d9291 455 remaining_disks --;
b80da661
NB
456 if ((disc.state & 1) == 0) /* faulty */
457 continue;
b3b4e8a7
N
458 dev = makedev(disc.major, disc.minor);
459 if (stb.st_rdev == dev)
460 /* already did that one */
461 continue;
462 stb.st_rdev = dev;
b80da661 463 next = dv;
b3b4e8a7
N
464 /* same slot again next time - things might
465 * have reshuffled */
466 jnext = j;
b80da661
NB
467 sprintf(dvname,"%d:%d", disc.major, disc.minor);
468 dnprintable = dvname;
469 break;
470 }
5b660791 471 if (next != dv)
b80da661
NB
472 continue;
473 } else if (strcmp(dv->devname, "detached") == 0) {
9e6d9291 474 int remaining_disks = array.nr_disks;
b80da661
NB
475 if (dv->disposition != 'r' && dv->disposition != 'f') {
476 fprintf(stderr, Name ": %s only meaningful "
477 "with -r of -f, not -%c\n",
478 dv->devname, dv->disposition);
479 return 1;
480 }
9e6d9291 481 for (; j < 1024 && remaining_disks > 0; j++) {
b80da661 482 int sfd;
f21e18ca 483 unsigned dev;
b80da661
NB
484 disc.number = j;
485 if (ioctl(fd, GET_DISK_INFO, &disc))
486 continue;
487 if (disc.major == 0 && disc.minor == 0)
488 continue;
9e6d9291 489 remaining_disks --;
b80da661
NB
490 sprintf(dvname,"%d:%d", disc.major, disc.minor);
491 sfd = dev_open(dvname, O_RDONLY);
492 if (sfd >= 0) {
493 close(sfd);
494 continue;
495 }
496 if (dv->disposition == 'f' &&
497 (disc.state & 1) == 1) /* already faulty */
498 continue;
499 if (errno != ENXIO)
500 continue;
b3b4e8a7
N
501 dev = makedev(disc.major, disc.minor);
502 if (stb.st_rdev == dev)
503 /* already did that one */
504 continue;
505 stb.st_rdev = dev;
b80da661 506 next = dv;
b3b4e8a7
N
507 /* same slot again next time - things might
508 * have reshuffled */
509 jnext = j;
b80da661
NB
510 dnprintable = dvname;
511 break;
512 }
5b660791 513 if (next != dv)
b80da661 514 continue;
a4e13010
N
515 } else if (strcmp(dv->devname, "missing") == 0) {
516 if (dv->disposition != 'a' || dv->re_add == 0) {
517 fprintf(stderr, Name ": 'missing' only meaningful "
518 "with --re-add\n");
519 return 1;
520 }
521 if (add_devlist == NULL)
522 add_devlist = conf_get_devs();
523 if (add_devlist == NULL) {
524 fprintf(stderr, Name ": no devices to scan for missing members.");
525 continue;
526 }
527 add_dev = add_devlist->devname;
528 add_devlist = add_devlist->next;
529 if (add_devlist != NULL)
530 next = dv;
531 if (stat(add_dev, &stb) < 0)
532 continue;
98d27e39 533 } else if (strchr(dv->devname, '/') == NULL &&
93601c43 534 strchr(dv->devname, ':') == NULL &&
98d27e39
N
535 strlen(dv->devname) < 50) {
536 /* Assume this is a kernel-internal name like 'sda1' */
537 int found = 0;
538 char dname[55];
539 if (dv->disposition != 'r' && dv->disposition != 'f') {
540 fprintf(stderr, Name ": %s only meaningful "
cfad27a9 541 "with -r or -f, not -%c\n",
98d27e39
N
542 dv->devname, dv->disposition);
543 return 1;
544 }
545
546 sprintf(dname, "dev-%s", dv->devname);
547 sysfd = sysfs_open(fd2devnum(fd), dname, "block/dev");
548 if (sysfd >= 0) {
549 char dn[20];
550 int mj,mn;
551 if (sysfs_fd_get_str(sysfd, dn, 20) > 0 &&
552 sscanf(dn, "%d:%d", &mj,&mn) == 2) {
553 stb.st_rdev = makedev(mj,mn);
554 found = 1;
555 }
556 close(sysfd);
557 sysfd = -1;
558 }
559 if (!found) {
560 sysfd = sysfs_open(fd2devnum(fd), dname, "state");
561 if (sysfd < 0) {
562 fprintf(stderr, Name ": %s does not appear "
563 "to be a component of %s\n",
564 dv->devname, devname);
565 return 1;
566 }
567 }
b80da661
NB
568 } else {
569 j = 0;
570
c7b47447 571 tfd = dev_open(dv->devname, O_RDONLY);
5a9de8db
N
572 if (tfd < 0 && dv->disposition == 'r' &&
573 lstat(dv->devname, &stb) == 0)
574 /* Be happy, the lstat worked, that is
575 * enough for --remove
576 */
577 ;
578 else {
579 if (tfd < 0 || fstat(tfd, &stb) != 0) {
580 fprintf(stderr, Name ": cannot find %s: %s\n",
581 dv->devname, strerror(errno));
582 if (tfd >= 0)
583 close(tfd);
584 return 1;
585 }
586 close(tfd);
cfad27a9 587 tfd = -1;
b80da661
NB
588 }
589 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
590 fprintf(stderr, Name ": %s is not a "
591 "block device.\n",
592 dv->devname);
593 return 1;
594 }
682c7051 595 }
cd29a5c8 596 switch(dv->disposition){
682c7051 597 default:
c913b90e
NB
598 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
599 dv->devname, dv->disposition);
682c7051
NB
600 return 1;
601 case 'a':
4a39c6f2 602 /* add the device */
4725bc31 603 if (subarray) {
f7dd881f
DW
604 fprintf(stderr, Name ": Cannot add disks to a"
605 " \'member\' array, perform this"
606 " operation on the parent container\n");
607 return 1;
f94d52f4 608 }
f277ce36 609 /* Make sure it isn't in use (in 2.6 or later) */
a4e13010
N
610 tfd = dev_open(add_dev, O_RDONLY|O_EXCL|O_DIRECT);
611 if (tfd < 0 && add_dev != dv->devname)
612 continue;
0fbf459d 613 if (tfd < 0) {
d7eaf49f
NB
614 fprintf(stderr, Name ": Cannot open %s: %s\n",
615 dv->devname, strerror(errno));
616 return 1;
617 }
3da92f27 618
1686dc25 619 st = dup_super(tst);
3da92f27 620
fe80f49b 621 if (array.not_persistent==0)
3da92f27
NB
622 st->ss->load_super(st, tfd, NULL);
623
a4e13010
N
624 if (add_dev == dv->devname) {
625 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
626 close(tfd);
627 return 1;
628 }
629 } else if (!get_dev_size(tfd, NULL, &ldsize)) {
4a39c6f2 630 close(tfd);
cfad27a9 631 tfd = -1;
a4e13010 632 continue;
4a39c6f2 633 }
f94d52f4
NB
634
635 if (!tst->ss->external &&
636 array.major_version == 0 &&
60d9a174 637 md_get_version(fd)%100 < 2) {
a4e13010 638 close(tfd);
cfad27a9 639 tfd = -1;
892debc8
NB
640 if (ioctl(fd, HOT_ADD_DISK,
641 (unsigned long)stb.st_rdev)==0) {
dab6685f
NB
642 if (verbose >= 0)
643 fprintf(stderr, Name ": hot added %s\n",
a4e13010 644 add_dev);
892debc8
NB
645 continue;
646 }
647
cd29a5c8 648 fprintf(stderr, Name ": hot add failed for %s: %s\n",
a4e13010 649 add_dev, strerror(errno));
cd29a5c8
NB
650 return 1;
651 }
892debc8 652
43dad3d6 653 if (array.not_persistent == 0 || tst->ss->external) {
4a39c6f2 654
fe80f49b 655 /* need to find a sample superblock to copy, and
43dad3d6
DW
656 * a spare slot to use.
657 * For 'external' array (well, container based),
658 * We can just load the metadata for the array.
fe80f49b 659 */
a4e13010
N
660 if (tst->sb)
661 /* already loaded */;
662 else if (tst->ss->external) {
0fb69d1d 663 tst->ss->load_container(tst, fd, NULL);
43dad3d6 664 } else for (j = 0; j < tst->max_devs; j++) {
fe80f49b
NB
665 char *dev;
666 int dfd;
667 disc.number = j;
668 if (ioctl(fd, GET_DISK_INFO, &disc))
669 continue;
670 if (disc.major==0 && disc.minor==0)
671 continue;
672 if ((disc.state & 4)==0) continue; /* sync */
673 /* Looks like a good device to try */
16c6fa80 674 dev = map_dev(disc.major, disc.minor, 1);
fe80f49b 675 if (!dev) continue;
16c6fa80 676 dfd = dev_open(dev, O_RDONLY);
fe80f49b 677 if (dfd < 0) continue;
3da92f27
NB
678 if (tst->ss->load_super(tst, dfd,
679 NULL)) {
fe80f49b
NB
680 close(dfd);
681 continue;
682 }
892debc8 683 close(dfd);
fe80f49b
NB
684 break;
685 }
43dad3d6 686 /* FIXME this is a bad test to be using */
3da92f27 687 if (!tst->sb) {
a4e13010 688 close(tfd);
0081eb00 689 fprintf(stderr, Name ": cannot load array metadata from %s\n", devname);
fe80f49b
NB
690 return 1;
691 }
2a528478
N
692
693 /* Make sure device is large enough */
694 if (tst->ss->avail_size(tst, ldsize/512) <
695 array_size) {
a4e13010 696 close(tfd);
cfad27a9 697 tfd = -1;
a4e13010
N
698 if (add_dev != dv->devname)
699 continue;
2a528478
N
700 fprintf(stderr, Name ": %s not large enough to join array\n",
701 dv->devname);
702 return 1;
703 }
704
fe80f49b
NB
705 /* Possibly this device was recently part of the array
706 * and was temporarily removed, and is now being re-added.
707 * If so, we can simply re-add it.
708 */
3da92f27 709 tst->ss->uuid_from_super(tst, duuid);
7eae7080 710
873eec46 711 if (st->sb) {
8453e704 712 struct mdinfo mdi;
a5d85af7 713 st->ss->getinfo_super(st, &mdi, NULL);
3da92f27 714 st->ss->uuid_from_super(st, ouuid);
8453e704
N
715 if ((mdi.disk.state & (1<<MD_DISK_ACTIVE)) &&
716 !(mdi.disk.state & (1<<MD_DISK_FAULTY)) &&
717 memcmp(duuid, ouuid, sizeof(ouuid))==0) {
718 /* look like it is worth a try. Need to
719 * make sure kernel will accept it though.
fe80f49b 720 */
873eec46
N
721 /* re-add doesn't work for version-1 superblocks
722 * before 2.6.18 :-(
723 */
724 if (array.major_version == 1 &&
725 get_linux_version() <= 2006018)
726 goto skip_re_add;
8453e704
N
727 disc.number = mdi.disk.number;
728 if (ioctl(fd, GET_DISK_INFO, &disc) != 0
729 || disc.major != 0 || disc.minor != 0
730 || !enough_fd(fd))
731 goto skip_re_add;
fe80f49b
NB
732 disc.major = major(stb.st_rdev);
733 disc.minor = minor(stb.st_rdev);
734 disc.number = mdi.disk.number;
735 disc.raid_disk = mdi.disk.raid_disk;
736 disc.state = mdi.disk.state;
b3d31955 737 if (dv->writemostly == 1)
4d20d744 738 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
b3d31955
N
739 if (dv->writemostly == 2)
740 disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
a4e13010
N
741 remove_partitions(tfd);
742 close(tfd);
743 tfd = -1;
833bb0f8
N
744 if (update) {
745 int rv = -1;
746 tfd = dev_open(dv->devname, O_RDWR);
747
748 if (tfd >= 0)
749 rv = st->ss->update_super(
750 st, NULL, update,
751 devname, verbose, 0, NULL);
752 if (rv == 0)
c0f8269d 753 rv = st->ss->store_super(st, tfd);
833bb0f8
N
754 close(tfd);
755 tfd = -1;
756 if (rv != 0) {
757 fprintf(stderr, Name ": failed to update"
758 " superblock during re-add\n");
759 return 1;
760 }
761 }
5c64fcb5
N
762 /* don't even try if disk is marked as faulty */
763 errno = 0;
8453e704 764 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
fe80f49b 765 if (verbose >= 0)
a4e13010 766 fprintf(stderr, Name ": re-added %s\n", add_dev);
7d2e6486 767 count++;
308e1801 768 continue;
fe80f49b 769 }
c9f39c1b
N
770 if (errno == ENOMEM || errno == EROFS) {
771 fprintf(stderr, Name ": add new device failed for %s: %s\n",
a4e13010
N
772 add_dev, strerror(errno));
773 if (add_dev != dv->devname)
774 continue;
c9f39c1b
N
775 return 1;
776 }
8453e704
N
777 skip_re_add:
778 re_add_failed = 1;
fe80f49b 779 }
1cc7f4fe 780 st->ss->free_super(st);
892debc8 781 }
a4e13010
N
782 if (add_dev != dv->devname) {
783 if (verbose > 0)
784 fprintf(stderr, Name
785 ": --re-add for %s to %s is not possible\n",
786 add_dev, devname);
cfad27a9 787 if (tfd >= 0) {
a4e13010 788 close(tfd);
cfad27a9
DL
789 tfd = -1;
790 }
a4e13010
N
791 continue;
792 }
751fd6c0 793 if (dv->re_add) {
a4e13010
N
794 if (tfd >= 0)
795 close(tfd);
751fd6c0
N
796 fprintf(stderr, Name
797 ": --re-add for %s to %s is not possible\n",
798 dv->devname, devname);
799 return 1;
800 }
8453e704
N
801 if (re_add_failed) {
802 fprintf(stderr, Name ": %s reports being an active member for %s, but a --re-add fails.\n",
803 dv->devname, devname);
804 fprintf(stderr, Name ": not performing --add as that would convert %s in to a spare.\n",
805 dv->devname);
806 fprintf(stderr, Name ": To make this a spare, use \"mdadm --zero-superblock %s\" first.\n",
807 dv->devname);
808 if (tfd >= 0)
809 close(tfd);
810 return 1;
811 }
4a39c6f2
NB
812 } else {
813 /* non-persistent. Must ensure that new drive
814 * is at least array.size big.
815 */
7a3be72f 816 if (ldsize/512 < array_size) {
4a39c6f2
NB
817 fprintf(stderr, Name ": %s not large enough to join array\n",
818 dv->devname);
cfad27a9
DL
819 if (tfd >= 0)
820 close(tfd);
4a39c6f2
NB
821 return 1;
822 }
892debc8 823 }
a4e13010
N
824 /* committed to really trying this device now*/
825 if (tfd >= 0) {
826 remove_partitions(tfd);
827 close(tfd);
cfad27a9 828 tfd = -1;
a4e13010 829 }
7eae7080
NB
830 /* in 2.6.17 and earlier, version-1 superblocks won't
831 * use the number we write, but will choose a free number.
832 * we must choose the same free number, which requires
833 * starting at 'raid_disks' and counting up
834 */
3da92f27 835 for (j = array.raid_disks; j< tst->max_devs; j++) {
aa88f531 836 disc.number = j;
682c7051
NB
837 if (ioctl(fd, GET_DISK_INFO, &disc))
838 break;
839 if (disc.major==0 && disc.minor==0)
840 break;
841 if (disc.state & 8) /* removed */
842 break;
843 }
0df46c2a
NB
844 disc.major = major(stb.st_rdev);
845 disc.minor = minor(stb.st_rdev);
892debc8
NB
846 disc.number =j;
847 disc.state = 0;
02c39ab1 848 if (array.not_persistent==0) {
111d01fc 849 int dfd;
b3d31955 850 if (dv->writemostly == 1)
fe80f49b 851 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
3b435195 852 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
f20c3968
DW
853 if (tst->ss->add_to_super(tst, &disc, dfd,
854 dv->devname)) {
f20c3968
DW
855 close(dfd);
856 return 1;
857 }
1cc7f4fe
N
858 if (tst->ss->write_init_super(tst)) {
859 close(dfd);
fe80f49b 860 return 1;
1cc7f4fe 861 }
fe80f49b
NB
862 } else if (dv->re_add) {
863 /* this had better be raid1.
864 * As we are "--re-add"ing we must find a spare slot
865 * to fill.
866 */
867 char *used = malloc(array.raid_disks);
868 memset(used, 0, array.raid_disks);
3da92f27 869 for (j=0; j< tst->max_devs; j++) {
fe80f49b
NB
870 mdu_disk_info_t disc2;
871 disc2.number = j;
872 if (ioctl(fd, GET_DISK_INFO, &disc2))
873 continue;
874 if (disc2.major==0 && disc2.minor==0)
875 continue;
876 if (disc2.state & 8) /* removed */
877 continue;
878 if (disc2.raid_disk < 0)
879 continue;
880 if (disc2.raid_disk > array.raid_disks)
881 continue;
882 used[disc2.raid_disk] = 1;
883 }
884 for (j=0 ; j<array.raid_disks; j++)
885 if (!used[j]) {
886 disc.raid_disk = j;
887 disc.state |= (1<<MD_DISK_SYNC);
888 break;
889 }
0eb26465 890 free(used);
fe80f49b 891 }
b3d31955 892 if (dv->writemostly == 1)
d8def61c 893 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
43dad3d6 894 if (tst->ss->external) {
73cb8d43
AK
895 /* add a disk
896 * to an external metadata container */
43dad3d6
DW
897 struct mdinfo new_mdi;
898 struct mdinfo *sra;
899 int container_fd;
900 int devnum = fd2devnum(fd);
02c39ab1 901 int dfd;
43dad3d6
DW
902
903 container_fd = open_dev_excl(devnum);
904 if (container_fd < 0) {
905 fprintf(stderr, Name ": add failed for %s:"
906 " could not get exclusive access to container\n",
907 dv->devname);
562e70e4 908 tst->ss->free_super(tst);
43dad3d6
DW
909 return 1;
910 }
911
02c39ab1 912 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
d6221e66
N
913 if (mdmon_running(tst->container_dev))
914 tst->update_tail = &tst->updates;
02c39ab1
N
915 if (tst->ss->add_to_super(tst, &disc, dfd,
916 dv->devname)) {
917 close(dfd);
918 close(container_fd);
919 return 1;
920 }
c0f8269d
AK
921 if (tst->update_tail)
922 flush_metadata_updates(tst);
d6221e66 923 else
c0f8269d 924 tst->ss->sync_metadata(tst);
02c39ab1 925
43dad3d6
DW
926 sra = sysfs_read(container_fd, -1, 0);
927 if (!sra) {
928 fprintf(stderr, Name ": add failed for %s: sysfs_read failed\n",
929 dv->devname);
930 close(container_fd);
562e70e4 931 tst->ss->free_super(tst);
43dad3d6
DW
932 return 1;
933 }
934 sra->array.level = LEVEL_CONTAINER;
935 /* Need to set data_offset and component_size */
a5d85af7 936 tst->ss->getinfo_super(tst, &new_mdi, NULL);
43dad3d6
DW
937 new_mdi.disk.major = disc.major;
938 new_mdi.disk.minor = disc.minor;
d23534e4 939 new_mdi.recovery_start = 0;
1cc7f4fe
N
940 /* Make sure fds are closed as they are O_EXCL which
941 * would block add_disk */
942 tst->ss->free_super(tst);
462906cd 943 if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
43dad3d6
DW
944 fprintf(stderr, Name ": add new device to external metadata"
945 " failed for %s\n", dv->devname);
946 close(container_fd);
73cb8d43 947 sysfs_free(sra);
43dad3d6
DW
948 return 1;
949 }
983fff45 950 ping_monitor_by_id(devnum);
43dad3d6
DW
951 sysfs_free(sra);
952 close(container_fd);
562e70e4
N
953 } else {
954 tst->ss->free_super(tst);
955 if (ioctl(fd, ADD_NEW_DISK, &disc)) {
956 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
957 dv->devname, j, strerror(errno));
958 return 1;
959 }
682c7051 960 }
dab6685f
NB
961 if (verbose >= 0)
962 fprintf(stderr, Name ": added %s\n", dv->devname);
682c7051
NB
963 break;
964
965 case 'r':
966 /* hot remove */
4725bc31 967 if (subarray) {
f7dd881f
DW
968 fprintf(stderr, Name ": Cannot remove disks from a"
969 " \'member\' array, perform this"
970 " operation on the parent container\n");
98d27e39
N
971 if (sysfd >= 0)
972 close(sysfd);
f7dd881f 973 return 1;
f94d52f4
NB
974 }
975 if (tst->ss->external) {
976 /* To remove a device from a container, we must
977 * check that it isn't in use in an array.
978 * This involves looking in the 'holders'
979 * directory - there must be just one entry,
980 * the container.
981 * To ensure that it doesn't get used as a
982 * hold spare while we are checking, we
983 * get an O_EXCL open on the container
984 */
985 int dnum = fd2devnum(fd);
986 lfd = open_dev_excl(dnum);
987 if (lfd < 0) {
988 fprintf(stderr, Name
989 ": Cannot get exclusive access "
990 " to container - odd\n");
98d27e39
N
991 if (sysfd >= 0)
992 close(sysfd);
f94d52f4
NB
993 return 1;
994 }
85f9b5f7
DW
995 /* in the detached case it is not possible to
996 * check if we are the unique holder, so just
997 * rely on the 'detached' checks
998 */
999 if (strcmp(dv->devname, "detached") == 0 ||
98d27e39 1000 sysfd >= 0 ||
85f9b5f7
DW
1001 sysfs_unique_holder(dnum, stb.st_rdev))
1002 /* pass */;
1003 else {
f94d52f4
NB
1004 fprintf(stderr, Name
1005 ": %s is %s, cannot remove.\n",
1006 dnprintable,
1007 errno == EEXIST ? "still in use":
1008 "not a member");
1009 close(lfd);
1010 return 1;
1011 }
1012 }
82b27616 1013 /* FIXME check that it is a current member */
98d27e39
N
1014 if (sysfd >= 0) {
1015 /* device has been removed and we don't know
1016 * the major:minor number
1017 */
1018 int n = write(sysfd, "remove", 6);
1019 if (n != 6)
f94d52f4 1020 err = -1;
98d27e39
N
1021 else
1022 err = 0;
1023 close(sysfd);
1024 sysfd = -1;
1025 } else {
1026 err = ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev);
1027 if (err && errno == ENODEV) {
1028 /* Old kernels rejected this if no personality
1029 * registered */
1030 struct mdinfo *sra = sysfs_read(fd, 0, GET_DEVS);
1031 struct mdinfo *dv = NULL;
1032 if (sra)
1033 dv = sra->devs;
1034 for ( ; dv ; dv=dv->next)
f21e18ca
N
1035 if (dv->disk.major == (int)major(stb.st_rdev) &&
1036 dv->disk.minor == (int)minor(stb.st_rdev))
98d27e39
N
1037 break;
1038 if (dv)
1039 err = sysfs_set_str(sra, dv,
1040 "state", "remove");
1041 else
1042 err = -1;
1043 if (sra)
1044 sysfs_free(sra);
1045 }
f94d52f4
NB
1046 }
1047 if (err) {
b80da661
NB
1048 fprintf(stderr, Name ": hot remove failed "
1049 "for %s: %s\n", dnprintable,
1050 strerror(errno));
f94d52f4
NB
1051 if (lfd >= 0)
1052 close(lfd);
682c7051
NB
1053 return 1;
1054 }
313a4a82
DW
1055 if (tst->ss->external) {
1056 /*
1057 * Before dropping our exclusive open we make an
1058 * attempt at preventing mdmon from seeing an
1059 * 'add' event before reconciling this 'remove'
1060 * event.
1061 */
1062 char *name = devnum2devname(fd2devnum(fd));
1063
1064 if (!name) {
1065 fprintf(stderr, Name ": unable to get container name\n");
1066 return 1;
1067 }
1068
1069 ping_manager(name);
1070 free(name);
1071 }
98d27e39
N
1072 if (lfd >= 0)
1073 close(lfd);
7d2e6486 1074 count++;
dab6685f 1075 if (verbose >= 0)
29ba4804
N
1076 fprintf(stderr, Name ": hot removed %s from %s\n",
1077 dnprintable, devname);
682c7051
NB
1078 break;
1079
1080 case 'f': /* set faulty */
1081 /* FIXME check current member */
98d27e39
N
1082 if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
1083 (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
1084 (unsigned long) stb.st_rdev))) {
b83d95f3 1085 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
b80da661 1086 dnprintable, strerror(errno));
98d27e39
N
1087 if (sysfd >= 0)
1088 close(sysfd);
682c7051
NB
1089 return 1;
1090 }
98d27e39
N
1091 if (sysfd >= 0)
1092 close(sysfd);
1093 sysfd = -1;
7d2e6486 1094 count++;
dab6685f
NB
1095 if (verbose >= 0)
1096 fprintf(stderr, Name ": set %s faulty in %s\n",
b80da661 1097 dnprintable, devname);
682c7051
NB
1098 break;
1099 }
1100 }
7d2e6486
N
1101 if (test && count == 0)
1102 return 2;
682c7051 1103 return 0;
64c4757e 1104}
1f48664b
NB
1105
1106int autodetect(void)
1107{
1108 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1109 int rv = 1;
1110 int fd = dev_open("9:0", O_RDONLY);
1111 if (fd >= 0) {
1112 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
1113 rv = 0;
1114 close(fd);
1115 }
1116 return rv;
1117}
aa534678 1118
fa56eddb 1119int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int quiet)
aa534678
DW
1120{
1121 struct supertype supertype, *st = &supertype;
1122 int fd, rv = 2;
1123
1124 memset(st, 0, sizeof(*st));
aa534678 1125
feab51f8 1126 fd = open_subarray(dev, subarray, st, quiet);
aa534678
DW
1127 if (fd < 0)
1128 return 2;
1129
1130 if (!st->ss->update_subarray) {
1131 if (!quiet)
1132 fprintf(stderr,
1133 Name ": Operation not supported for %s metadata\n",
1134 st->ss->name);
1135 goto free_super;
1136 }
1137
1138 if (mdmon_running(st->devnum))
1139 st->update_tail = &st->updates;
1140
a951a4f7 1141 rv = st->ss->update_subarray(st, subarray, update, ident);
aa534678
DW
1142
1143 if (rv) {
1144 if (!quiet)
1145 fprintf(stderr, Name ": Failed to update %s of subarray-%s in %s\n",
1146 update, subarray, dev);
1147 } else if (st->update_tail)
1148 flush_metadata_updates(st);
1149 else
1150 st->ss->sync_metadata(st);
1151
1152 if (rv == 0 && strcmp(update, "name") == 0 && !quiet)
1153 fprintf(stderr,
1154 Name ": Updated subarray-%s name from %s, UUIDs may have changed\n",
1155 subarray, dev);
1156
1157 free_super:
1158 st->ss->free_super(st);
1159 close(fd);
1160
1161 return rv;
1162}
d52bb542
AC
1163
1164/* Move spare from one array to another
1165 * If adding to destination array fails
1166 * add back to original array
1167 * Returns 1 on success, 0 on failure */
1168int move_spare(char *from_devname, char *to_devname, dev_t devid)
1169{
1170 struct mddev_dev devlist;
1171 char devname[20];
1172
1173 /* try to remove and add */
1174 int fd1 = open(to_devname, O_RDONLY);
1175 int fd2 = open(from_devname, O_RDONLY);
1176
1177 if (fd1 < 0 || fd2 < 0) {
1178 if (fd1>=0) close(fd1);
1179 if (fd2>=0) close(fd2);
1180 return 0;
1181 }
1182
1183 devlist.next = NULL;
1184 devlist.used = 0;
1185 devlist.re_add = 0;
1186 devlist.writemostly = 0;
1187 devlist.devname = devname;
1188 sprintf(devname, "%d:%d", major(devid), minor(devid));
1189
1190 devlist.disposition = 'r';
1191 if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL) == 0) {
1192 devlist.disposition = 'a';
1193 if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL) == 0) {
1194 /* make sure manager is aware of changes */
1195 ping_manager(to_devname);
1196 ping_manager(from_devname);
1197 close(fd1);
1198 close(fd2);
1199 return 1;
1200 }
1201 else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL);
1202 }
1203 close(fd1);
1204 close(fd2);
1205 return 0;
1206}
435d4ebb 1207#endif