]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Manage.c
Monitor: --oneshot must imply --no-sharing
[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 373 struct mddev_dev *devlist, int verbose, int test,
11b391ec 374 char *update, int force)
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 634
11b391ec
N
635 if (tst->ss->validate_geometry(
636 tst, array.level, array.layout,
637 array.raid_disks, NULL,
638 ldsize >> 9, NULL, NULL, 0) == 0) {
639 if (!force) {
640 fprintf(stderr, Name
641 ": %s is larger than %s can "
642 "effectively use.\n"
643 " Add --force is you "
644 "really wan to add this device.\n",
645 add_dev, devname);
646 close(tfd);
647 return 1;
648 }
649 fprintf(stderr, Name
650 ": %s is larger than %s can "
651 "effectively use.\n"
652 " Adding anyway as --force "
653 "was given.\n",
654 add_dev, devname);
655 }
f94d52f4
NB
656 if (!tst->ss->external &&
657 array.major_version == 0 &&
60d9a174 658 md_get_version(fd)%100 < 2) {
a4e13010 659 close(tfd);
cfad27a9 660 tfd = -1;
892debc8
NB
661 if (ioctl(fd, HOT_ADD_DISK,
662 (unsigned long)stb.st_rdev)==0) {
dab6685f
NB
663 if (verbose >= 0)
664 fprintf(stderr, Name ": hot added %s\n",
a4e13010 665 add_dev);
892debc8
NB
666 continue;
667 }
668
cd29a5c8 669 fprintf(stderr, Name ": hot add failed for %s: %s\n",
a4e13010 670 add_dev, strerror(errno));
cd29a5c8
NB
671 return 1;
672 }
892debc8 673
43dad3d6 674 if (array.not_persistent == 0 || tst->ss->external) {
4a39c6f2 675
fe80f49b 676 /* need to find a sample superblock to copy, and
43dad3d6
DW
677 * a spare slot to use.
678 * For 'external' array (well, container based),
679 * We can just load the metadata for the array.
fe80f49b 680 */
a4e13010
N
681 if (tst->sb)
682 /* already loaded */;
683 else if (tst->ss->external) {
0fb69d1d 684 tst->ss->load_container(tst, fd, NULL);
43dad3d6 685 } else for (j = 0; j < tst->max_devs; j++) {
fe80f49b
NB
686 char *dev;
687 int dfd;
688 disc.number = j;
689 if (ioctl(fd, GET_DISK_INFO, &disc))
690 continue;
691 if (disc.major==0 && disc.minor==0)
692 continue;
693 if ((disc.state & 4)==0) continue; /* sync */
694 /* Looks like a good device to try */
16c6fa80 695 dev = map_dev(disc.major, disc.minor, 1);
fe80f49b 696 if (!dev) continue;
16c6fa80 697 dfd = dev_open(dev, O_RDONLY);
fe80f49b 698 if (dfd < 0) continue;
3da92f27
NB
699 if (tst->ss->load_super(tst, dfd,
700 NULL)) {
fe80f49b
NB
701 close(dfd);
702 continue;
703 }
892debc8 704 close(dfd);
fe80f49b
NB
705 break;
706 }
43dad3d6 707 /* FIXME this is a bad test to be using */
3da92f27 708 if (!tst->sb) {
a4e13010 709 close(tfd);
0081eb00 710 fprintf(stderr, Name ": cannot load array metadata from %s\n", devname);
fe80f49b
NB
711 return 1;
712 }
2a528478
N
713
714 /* Make sure device is large enough */
715 if (tst->ss->avail_size(tst, ldsize/512) <
716 array_size) {
a4e13010 717 close(tfd);
cfad27a9 718 tfd = -1;
a4e13010
N
719 if (add_dev != dv->devname)
720 continue;
2a528478
N
721 fprintf(stderr, Name ": %s not large enough to join array\n",
722 dv->devname);
723 return 1;
724 }
725
fe80f49b
NB
726 /* Possibly this device was recently part of the array
727 * and was temporarily removed, and is now being re-added.
728 * If so, we can simply re-add it.
729 */
3da92f27 730 tst->ss->uuid_from_super(tst, duuid);
7eae7080 731
873eec46 732 if (st->sb) {
8453e704 733 struct mdinfo mdi;
a5d85af7 734 st->ss->getinfo_super(st, &mdi, NULL);
3da92f27 735 st->ss->uuid_from_super(st, ouuid);
8453e704
N
736 if ((mdi.disk.state & (1<<MD_DISK_ACTIVE)) &&
737 !(mdi.disk.state & (1<<MD_DISK_FAULTY)) &&
738 memcmp(duuid, ouuid, sizeof(ouuid))==0) {
739 /* look like it is worth a try. Need to
740 * make sure kernel will accept it though.
fe80f49b 741 */
873eec46
N
742 /* re-add doesn't work for version-1 superblocks
743 * before 2.6.18 :-(
744 */
745 if (array.major_version == 1 &&
746 get_linux_version() <= 2006018)
747 goto skip_re_add;
8453e704
N
748 disc.number = mdi.disk.number;
749 if (ioctl(fd, GET_DISK_INFO, &disc) != 0
750 || disc.major != 0 || disc.minor != 0
751 || !enough_fd(fd))
752 goto skip_re_add;
fe80f49b
NB
753 disc.major = major(stb.st_rdev);
754 disc.minor = minor(stb.st_rdev);
755 disc.number = mdi.disk.number;
756 disc.raid_disk = mdi.disk.raid_disk;
757 disc.state = mdi.disk.state;
b3d31955 758 if (dv->writemostly == 1)
4d20d744 759 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
b3d31955
N
760 if (dv->writemostly == 2)
761 disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
a4e13010
N
762 remove_partitions(tfd);
763 close(tfd);
764 tfd = -1;
833bb0f8
N
765 if (update) {
766 int rv = -1;
767 tfd = dev_open(dv->devname, O_RDWR);
768
769 if (tfd >= 0)
770 rv = st->ss->update_super(
771 st, NULL, update,
772 devname, verbose, 0, NULL);
773 if (rv == 0)
c0f8269d 774 rv = st->ss->store_super(st, tfd);
833bb0f8
N
775 close(tfd);
776 tfd = -1;
777 if (rv != 0) {
778 fprintf(stderr, Name ": failed to update"
779 " superblock during re-add\n");
780 return 1;
781 }
782 }
5c64fcb5
N
783 /* don't even try if disk is marked as faulty */
784 errno = 0;
8453e704 785 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
fe80f49b 786 if (verbose >= 0)
a4e13010 787 fprintf(stderr, Name ": re-added %s\n", add_dev);
7d2e6486 788 count++;
308e1801 789 continue;
fe80f49b 790 }
c9f39c1b
N
791 if (errno == ENOMEM || errno == EROFS) {
792 fprintf(stderr, Name ": add new device failed for %s: %s\n",
a4e13010
N
793 add_dev, strerror(errno));
794 if (add_dev != dv->devname)
795 continue;
c9f39c1b
N
796 return 1;
797 }
8453e704
N
798 skip_re_add:
799 re_add_failed = 1;
fe80f49b 800 }
1cc7f4fe 801 st->ss->free_super(st);
892debc8 802 }
a4e13010
N
803 if (add_dev != dv->devname) {
804 if (verbose > 0)
805 fprintf(stderr, Name
806 ": --re-add for %s to %s is not possible\n",
807 add_dev, devname);
cfad27a9 808 if (tfd >= 0) {
a4e13010 809 close(tfd);
cfad27a9
DL
810 tfd = -1;
811 }
a4e13010
N
812 continue;
813 }
751fd6c0 814 if (dv->re_add) {
a4e13010
N
815 if (tfd >= 0)
816 close(tfd);
751fd6c0
N
817 fprintf(stderr, Name
818 ": --re-add for %s to %s is not possible\n",
819 dv->devname, devname);
820 return 1;
821 }
8453e704
N
822 if (re_add_failed) {
823 fprintf(stderr, Name ": %s reports being an active member for %s, but a --re-add fails.\n",
824 dv->devname, devname);
825 fprintf(stderr, Name ": not performing --add as that would convert %s in to a spare.\n",
826 dv->devname);
827 fprintf(stderr, Name ": To make this a spare, use \"mdadm --zero-superblock %s\" first.\n",
828 dv->devname);
829 if (tfd >= 0)
830 close(tfd);
831 return 1;
832 }
4a39c6f2
NB
833 } else {
834 /* non-persistent. Must ensure that new drive
835 * is at least array.size big.
836 */
7a3be72f 837 if (ldsize/512 < array_size) {
4a39c6f2
NB
838 fprintf(stderr, Name ": %s not large enough to join array\n",
839 dv->devname);
cfad27a9
DL
840 if (tfd >= 0)
841 close(tfd);
4a39c6f2
NB
842 return 1;
843 }
892debc8 844 }
a4e13010
N
845 /* committed to really trying this device now*/
846 if (tfd >= 0) {
847 remove_partitions(tfd);
848 close(tfd);
cfad27a9 849 tfd = -1;
a4e13010 850 }
7eae7080
NB
851 /* in 2.6.17 and earlier, version-1 superblocks won't
852 * use the number we write, but will choose a free number.
853 * we must choose the same free number, which requires
854 * starting at 'raid_disks' and counting up
855 */
3da92f27 856 for (j = array.raid_disks; j< tst->max_devs; j++) {
aa88f531 857 disc.number = j;
682c7051
NB
858 if (ioctl(fd, GET_DISK_INFO, &disc))
859 break;
860 if (disc.major==0 && disc.minor==0)
861 break;
862 if (disc.state & 8) /* removed */
863 break;
864 }
0df46c2a
NB
865 disc.major = major(stb.st_rdev);
866 disc.minor = minor(stb.st_rdev);
892debc8
NB
867 disc.number =j;
868 disc.state = 0;
02c39ab1 869 if (array.not_persistent==0) {
111d01fc 870 int dfd;
b3d31955 871 if (dv->writemostly == 1)
fe80f49b 872 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
3b435195 873 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
f20c3968
DW
874 if (tst->ss->add_to_super(tst, &disc, dfd,
875 dv->devname)) {
f20c3968
DW
876 close(dfd);
877 return 1;
878 }
1cc7f4fe
N
879 if (tst->ss->write_init_super(tst)) {
880 close(dfd);
fe80f49b 881 return 1;
1cc7f4fe 882 }
fe80f49b
NB
883 } else if (dv->re_add) {
884 /* this had better be raid1.
885 * As we are "--re-add"ing we must find a spare slot
886 * to fill.
887 */
888 char *used = malloc(array.raid_disks);
889 memset(used, 0, array.raid_disks);
3da92f27 890 for (j=0; j< tst->max_devs; j++) {
fe80f49b
NB
891 mdu_disk_info_t disc2;
892 disc2.number = j;
893 if (ioctl(fd, GET_DISK_INFO, &disc2))
894 continue;
895 if (disc2.major==0 && disc2.minor==0)
896 continue;
897 if (disc2.state & 8) /* removed */
898 continue;
899 if (disc2.raid_disk < 0)
900 continue;
901 if (disc2.raid_disk > array.raid_disks)
902 continue;
903 used[disc2.raid_disk] = 1;
904 }
905 for (j=0 ; j<array.raid_disks; j++)
906 if (!used[j]) {
907 disc.raid_disk = j;
908 disc.state |= (1<<MD_DISK_SYNC);
909 break;
910 }
0eb26465 911 free(used);
fe80f49b 912 }
b3d31955 913 if (dv->writemostly == 1)
d8def61c 914 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
43dad3d6 915 if (tst->ss->external) {
73cb8d43
AK
916 /* add a disk
917 * to an external metadata container */
43dad3d6
DW
918 struct mdinfo new_mdi;
919 struct mdinfo *sra;
920 int container_fd;
921 int devnum = fd2devnum(fd);
02c39ab1 922 int dfd;
43dad3d6
DW
923
924 container_fd = open_dev_excl(devnum);
925 if (container_fd < 0) {
926 fprintf(stderr, Name ": add failed for %s:"
927 " could not get exclusive access to container\n",
928 dv->devname);
562e70e4 929 tst->ss->free_super(tst);
43dad3d6
DW
930 return 1;
931 }
932
02c39ab1 933 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
d6221e66
N
934 if (mdmon_running(tst->container_dev))
935 tst->update_tail = &tst->updates;
02c39ab1
N
936 if (tst->ss->add_to_super(tst, &disc, dfd,
937 dv->devname)) {
938 close(dfd);
939 close(container_fd);
940 return 1;
941 }
c0f8269d
AK
942 if (tst->update_tail)
943 flush_metadata_updates(tst);
d6221e66 944 else
c0f8269d 945 tst->ss->sync_metadata(tst);
02c39ab1 946
43dad3d6
DW
947 sra = sysfs_read(container_fd, -1, 0);
948 if (!sra) {
949 fprintf(stderr, Name ": add failed for %s: sysfs_read failed\n",
950 dv->devname);
951 close(container_fd);
562e70e4 952 tst->ss->free_super(tst);
43dad3d6
DW
953 return 1;
954 }
955 sra->array.level = LEVEL_CONTAINER;
956 /* Need to set data_offset and component_size */
a5d85af7 957 tst->ss->getinfo_super(tst, &new_mdi, NULL);
43dad3d6
DW
958 new_mdi.disk.major = disc.major;
959 new_mdi.disk.minor = disc.minor;
d23534e4 960 new_mdi.recovery_start = 0;
1cc7f4fe
N
961 /* Make sure fds are closed as they are O_EXCL which
962 * would block add_disk */
963 tst->ss->free_super(tst);
462906cd 964 if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
43dad3d6
DW
965 fprintf(stderr, Name ": add new device to external metadata"
966 " failed for %s\n", dv->devname);
967 close(container_fd);
73cb8d43 968 sysfs_free(sra);
43dad3d6
DW
969 return 1;
970 }
983fff45 971 ping_monitor_by_id(devnum);
43dad3d6
DW
972 sysfs_free(sra);
973 close(container_fd);
562e70e4
N
974 } else {
975 tst->ss->free_super(tst);
976 if (ioctl(fd, ADD_NEW_DISK, &disc)) {
977 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
978 dv->devname, j, strerror(errno));
979 return 1;
980 }
682c7051 981 }
dab6685f
NB
982 if (verbose >= 0)
983 fprintf(stderr, Name ": added %s\n", dv->devname);
682c7051
NB
984 break;
985
986 case 'r':
987 /* hot remove */
4725bc31 988 if (subarray) {
f7dd881f
DW
989 fprintf(stderr, Name ": Cannot remove disks from a"
990 " \'member\' array, perform this"
991 " operation on the parent container\n");
98d27e39
N
992 if (sysfd >= 0)
993 close(sysfd);
f7dd881f 994 return 1;
f94d52f4
NB
995 }
996 if (tst->ss->external) {
997 /* To remove a device from a container, we must
998 * check that it isn't in use in an array.
999 * This involves looking in the 'holders'
1000 * directory - there must be just one entry,
1001 * the container.
1002 * To ensure that it doesn't get used as a
1003 * hold spare while we are checking, we
1004 * get an O_EXCL open on the container
1005 */
1006 int dnum = fd2devnum(fd);
1007 lfd = open_dev_excl(dnum);
1008 if (lfd < 0) {
1009 fprintf(stderr, Name
1010 ": Cannot get exclusive access "
1011 " to container - odd\n");
98d27e39
N
1012 if (sysfd >= 0)
1013 close(sysfd);
f94d52f4
NB
1014 return 1;
1015 }
85f9b5f7
DW
1016 /* in the detached case it is not possible to
1017 * check if we are the unique holder, so just
1018 * rely on the 'detached' checks
1019 */
1020 if (strcmp(dv->devname, "detached") == 0 ||
98d27e39 1021 sysfd >= 0 ||
85f9b5f7
DW
1022 sysfs_unique_holder(dnum, stb.st_rdev))
1023 /* pass */;
1024 else {
f94d52f4
NB
1025 fprintf(stderr, Name
1026 ": %s is %s, cannot remove.\n",
1027 dnprintable,
1028 errno == EEXIST ? "still in use":
1029 "not a member");
1030 close(lfd);
1031 return 1;
1032 }
1033 }
82b27616 1034 /* FIXME check that it is a current member */
98d27e39
N
1035 if (sysfd >= 0) {
1036 /* device has been removed and we don't know
1037 * the major:minor number
1038 */
1039 int n = write(sysfd, "remove", 6);
1040 if (n != 6)
f94d52f4 1041 err = -1;
98d27e39
N
1042 else
1043 err = 0;
1044 close(sysfd);
1045 sysfd = -1;
1046 } else {
1047 err = ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev);
1048 if (err && errno == ENODEV) {
1049 /* Old kernels rejected this if no personality
1050 * registered */
1051 struct mdinfo *sra = sysfs_read(fd, 0, GET_DEVS);
1052 struct mdinfo *dv = NULL;
1053 if (sra)
1054 dv = sra->devs;
1055 for ( ; dv ; dv=dv->next)
f21e18ca
N
1056 if (dv->disk.major == (int)major(stb.st_rdev) &&
1057 dv->disk.minor == (int)minor(stb.st_rdev))
98d27e39
N
1058 break;
1059 if (dv)
1060 err = sysfs_set_str(sra, dv,
1061 "state", "remove");
1062 else
1063 err = -1;
1064 if (sra)
1065 sysfs_free(sra);
1066 }
f94d52f4
NB
1067 }
1068 if (err) {
b80da661
NB
1069 fprintf(stderr, Name ": hot remove failed "
1070 "for %s: %s\n", dnprintable,
1071 strerror(errno));
f94d52f4
NB
1072 if (lfd >= 0)
1073 close(lfd);
682c7051
NB
1074 return 1;
1075 }
313a4a82
DW
1076 if (tst->ss->external) {
1077 /*
1078 * Before dropping our exclusive open we make an
1079 * attempt at preventing mdmon from seeing an
1080 * 'add' event before reconciling this 'remove'
1081 * event.
1082 */
1083 char *name = devnum2devname(fd2devnum(fd));
1084
1085 if (!name) {
1086 fprintf(stderr, Name ": unable to get container name\n");
1087 return 1;
1088 }
1089
1090 ping_manager(name);
1091 free(name);
1092 }
98d27e39
N
1093 if (lfd >= 0)
1094 close(lfd);
7d2e6486 1095 count++;
dab6685f 1096 if (verbose >= 0)
29ba4804
N
1097 fprintf(stderr, Name ": hot removed %s from %s\n",
1098 dnprintable, devname);
682c7051
NB
1099 break;
1100
1101 case 'f': /* set faulty */
1102 /* FIXME check current member */
98d27e39
N
1103 if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
1104 (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
1105 (unsigned long) stb.st_rdev))) {
b83d95f3 1106 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
b80da661 1107 dnprintable, strerror(errno));
98d27e39
N
1108 if (sysfd >= 0)
1109 close(sysfd);
682c7051
NB
1110 return 1;
1111 }
98d27e39
N
1112 if (sysfd >= 0)
1113 close(sysfd);
1114 sysfd = -1;
7d2e6486 1115 count++;
dab6685f
NB
1116 if (verbose >= 0)
1117 fprintf(stderr, Name ": set %s faulty in %s\n",
b80da661 1118 dnprintable, devname);
682c7051
NB
1119 break;
1120 }
1121 }
7d2e6486
N
1122 if (test && count == 0)
1123 return 2;
682c7051 1124 return 0;
64c4757e 1125}
1f48664b
NB
1126
1127int autodetect(void)
1128{
1129 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1130 int rv = 1;
1131 int fd = dev_open("9:0", O_RDONLY);
1132 if (fd >= 0) {
1133 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
1134 rv = 0;
1135 close(fd);
1136 }
1137 return rv;
1138}
aa534678 1139
fa56eddb 1140int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int quiet)
aa534678
DW
1141{
1142 struct supertype supertype, *st = &supertype;
1143 int fd, rv = 2;
1144
1145 memset(st, 0, sizeof(*st));
aa534678 1146
feab51f8 1147 fd = open_subarray(dev, subarray, st, quiet);
aa534678
DW
1148 if (fd < 0)
1149 return 2;
1150
1151 if (!st->ss->update_subarray) {
1152 if (!quiet)
1153 fprintf(stderr,
1154 Name ": Operation not supported for %s metadata\n",
1155 st->ss->name);
1156 goto free_super;
1157 }
1158
1159 if (mdmon_running(st->devnum))
1160 st->update_tail = &st->updates;
1161
a951a4f7 1162 rv = st->ss->update_subarray(st, subarray, update, ident);
aa534678
DW
1163
1164 if (rv) {
1165 if (!quiet)
1166 fprintf(stderr, Name ": Failed to update %s of subarray-%s in %s\n",
1167 update, subarray, dev);
1168 } else if (st->update_tail)
1169 flush_metadata_updates(st);
1170 else
1171 st->ss->sync_metadata(st);
1172
1173 if (rv == 0 && strcmp(update, "name") == 0 && !quiet)
1174 fprintf(stderr,
1175 Name ": Updated subarray-%s name from %s, UUIDs may have changed\n",
1176 subarray, dev);
1177
1178 free_super:
1179 st->ss->free_super(st);
1180 close(fd);
1181
1182 return rv;
1183}
d52bb542
AC
1184
1185/* Move spare from one array to another
1186 * If adding to destination array fails
1187 * add back to original array
1188 * Returns 1 on success, 0 on failure */
1189int move_spare(char *from_devname, char *to_devname, dev_t devid)
1190{
1191 struct mddev_dev devlist;
1192 char devname[20];
1193
1194 /* try to remove and add */
1195 int fd1 = open(to_devname, O_RDONLY);
1196 int fd2 = open(from_devname, O_RDONLY);
1197
1198 if (fd1 < 0 || fd2 < 0) {
1199 if (fd1>=0) close(fd1);
1200 if (fd2>=0) close(fd2);
1201 return 0;
1202 }
1203
1204 devlist.next = NULL;
1205 devlist.used = 0;
1206 devlist.re_add = 0;
1207 devlist.writemostly = 0;
1208 devlist.devname = devname;
1209 sprintf(devname, "%d:%d", major(devid), minor(devid));
1210
1211 devlist.disposition = 'r';
11b391ec 1212 if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0) == 0) {
d52bb542 1213 devlist.disposition = 'a';
11b391ec 1214 if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL, 0) == 0) {
d52bb542
AC
1215 /* make sure manager is aware of changes */
1216 ping_manager(to_devname);
1217 ping_manager(from_devname);
1218 close(fd1);
1219 close(fd2);
1220 return 1;
1221 }
11b391ec 1222 else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0);
d52bb542
AC
1223 }
1224 close(fd1);
1225 close(fd2);
1226 return 0;
1227}
435d4ebb 1228#endif