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