]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Manage.c
Fix typos in mdadm.8.in
[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 */
786 if (!tst->sb && dv->disposition != 'a') {
787 /* we are re-adding a device to a
788 * completely dead array - have to depend
789 * on kernel to check
790 */
791 } else if (!tst->sb) {
792 pr_err("cannot load array metadata from %s\n", devname);
793 return -1;
794 }
795
796 /* Make sure device is large enough */
387fcd59 797 if (tst->ss->avail_size(tst, ldsize/512, INVALID_SECTORS) <
38aeaf3a
N
798 array_size) {
799 if (dv->disposition == 'M')
800 return 0;
801 pr_err("%s not large enough to join array\n",
802 dv->devname);
803 return -1;
804 }
805
806 /* Possibly this device was recently part of
807 * the array and was temporarily removed, and
808 * is now being re-added. If so, we can
809 * simply re-add it.
810 */
811
812 if (array->not_persistent==0) {
813 dev_st = dup_super(tst);
814 dev_st->ss->load_super(dev_st, tfd, NULL);
815 }
816 if (dev_st && dev_st->sb) {
817 int rv = attempt_re_add(fd, tfd, dv,
818 dev_st, tst,
819 rdev,
820 update, devname,
821 verbose,
822 array);
823 dev_st->ss->free_super(dev_st);
824 if (rv)
825 return rv;
826 }
827 if (dv->disposition == 'M') {
828 if (verbose > 0)
829 pr_err("--re-add for %s to %s is not possible\n",
830 dv->devname, devname);
831 return 0;
832 }
833 if (dv->disposition == 'A') {
834 pr_err("--re-add for %s to %s is not possible\n",
835 dv->devname, devname);
836 return -1;
837 }
838 if (array->active_disks < array->raid_disks) {
839 char *avail = xcalloc(array->raid_disks, 1);
840 int d;
841 int found = 0;
842
843 for (d = 0; d < MAX_DISKS && found < array->active_disks; d++) {
844 disc.number = d;
845 if (ioctl(fd, GET_DISK_INFO, &disc))
846 continue;
847 if (disc.major == 0 && disc.minor == 0)
848 continue;
849 if (!(disc.state & (1<<MD_DISK_SYNC)))
850 continue;
851 avail[disc.raid_disk] = 1;
852 found++;
853 }
854 array_failed = !enough(array->level, array->raid_disks,
855 array->layout, 1, avail);
6157951f 856 free(avail);
38aeaf3a
N
857 } else
858 array_failed = 0;
859 if (array_failed) {
860 pr_err("%s has failed so using --add cannot work and might destroy\n",
861 devname);
862 pr_err("data on %s. You should stop the array and re-assemble it.\n",
863 dv->devname);
864 return -1;
865 }
866 } else {
867 /* non-persistent. Must ensure that new drive
868 * is at least array->size big.
869 */
870 if (ldsize/512 < array_size) {
871 pr_err("%s not large enough to join array\n",
872 dv->devname);
873 return -1;
874 }
875 }
876 /* committed to really trying this device now*/
877 remove_partitions(tfd);
878
879 /* in 2.6.17 and earlier, version-1 superblocks won't
880 * use the number we write, but will choose a free number.
881 * we must choose the same free number, which requires
882 * starting at 'raid_disks' and counting up
883 */
884 for (j = array->raid_disks; j < tst->max_devs; j++) {
885 disc.number = j;
886 if (ioctl(fd, GET_DISK_INFO, &disc))
887 break;
888 if (disc.major==0 && disc.minor==0)
889 break;
890 if (disc.state & 8) /* removed */
891 break;
892 }
893 disc.major = major(rdev);
894 disc.minor = minor(rdev);
895 disc.number =j;
896 disc.state = 0;
897 if (array->not_persistent==0) {
898 int dfd;
899 if (dv->writemostly == 1)
900 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
901 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
902 if (tst->ss->add_to_super(tst, &disc, dfd,
72ca9bcf 903 dv->devname, INVALID_SECTORS))
38aeaf3a
N
904 return -1;
905 if (tst->ss->write_init_super(tst))
906 return -1;
907 } else if (dv->disposition == 'A') {
908 /* this had better be raid1.
909 * As we are "--re-add"ing we must find a spare slot
910 * to fill.
911 */
912 char *used = xcalloc(array->raid_disks, 1);
913 for (j = 0; j < tst->max_devs; j++) {
914 mdu_disk_info_t disc2;
915 disc2.number = j;
916 if (ioctl(fd, GET_DISK_INFO, &disc2))
917 continue;
918 if (disc2.major==0 && disc2.minor==0)
919 continue;
920 if (disc2.state & 8) /* removed */
921 continue;
922 if (disc2.raid_disk < 0)
923 continue;
924 if (disc2.raid_disk > array->raid_disks)
925 continue;
926 used[disc2.raid_disk] = 1;
927 }
928 for (j = 0 ; j < array->raid_disks; j++)
929 if (!used[j]) {
930 disc.raid_disk = j;
931 disc.state |= (1<<MD_DISK_SYNC);
932 break;
933 }
934 free(used);
935 }
936 if (dv->writemostly == 1)
937 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
938 if (tst->ss->external) {
939 /* add a disk
940 * to an external metadata container */
941 struct mdinfo new_mdi;
942 struct mdinfo *sra;
943 int container_fd;
4dd2df09 944 char devnm[32];
38aeaf3a
N
945 int dfd;
946
4dd2df09
N
947 strcpy(devnm, fd2devnm(fd));
948
949 container_fd = open_dev_excl(devnm);
38aeaf3a
N
950 if (container_fd < 0) {
951 pr_err("add failed for %s:"
952 " could not get exclusive access to container\n",
953 dv->devname);
954 tst->ss->free_super(tst);
955 return -1;
956 }
957
9cf9a1de 958 Kill(dv->devname, NULL, 0, -1, 0);
38aeaf3a 959 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
4dd2df09 960 if (mdmon_running(tst->container_devnm))
38aeaf3a
N
961 tst->update_tail = &tst->updates;
962 if (tst->ss->add_to_super(tst, &disc, dfd,
72ca9bcf 963 dv->devname, INVALID_SECTORS)) {
38aeaf3a
N
964 close(dfd);
965 close(container_fd);
966 return -1;
967 }
968 if (tst->update_tail)
969 flush_metadata_updates(tst);
970 else
971 tst->ss->sync_metadata(tst);
972
4dd2df09 973 sra = sysfs_read(container_fd, NULL, 0);
38aeaf3a
N
974 if (!sra) {
975 pr_err("add failed for %s: sysfs_read failed\n",
976 dv->devname);
977 close(container_fd);
978 tst->ss->free_super(tst);
979 return -1;
980 }
981 sra->array.level = LEVEL_CONTAINER;
982 /* Need to set data_offset and component_size */
983 tst->ss->getinfo_super(tst, &new_mdi, NULL);
984 new_mdi.disk.major = disc.major;
985 new_mdi.disk.minor = disc.minor;
986 new_mdi.recovery_start = 0;
987 /* Make sure fds are closed as they are O_EXCL which
988 * would block add_disk */
989 tst->ss->free_super(tst);
990 if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
991 pr_err("add new device to external metadata"
992 " failed for %s\n", dv->devname);
993 close(container_fd);
994 sysfs_free(sra);
995 return -1;
996 }
4dd2df09 997 ping_monitor(devnm);
38aeaf3a
N
998 sysfs_free(sra);
999 close(container_fd);
1000 } else {
1001 tst->ss->free_super(tst);
1002 if (ioctl(fd, ADD_NEW_DISK, &disc)) {
1003 pr_err("add new device failed for %s as %d: %s\n",
1004 dv->devname, j, strerror(errno));
1005 return -1;
1006 }
1007 }
1008 if (verbose >= 0)
1009 pr_err("added %s\n", dv->devname);
1010 return 1;
1011}
1012
d070235d
N
1013int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
1014 int sysfd, unsigned long rdev, int verbose, char *devname)
1015{
1016 int lfd = -1;
1017 int err;
1018
1019 if (tst->ss->external) {
1020 /* To remove a device from a container, we must
1021 * check that it isn't in use in an array.
1022 * This involves looking in the 'holders'
1023 * directory - there must be just one entry,
1024 * the container.
1025 * To ensure that it doesn't get used as a
1026 * hot spare while we are checking, we
1027 * get an O_EXCL open on the container
1028 */
aab15415 1029 int ret;
4dd2df09
N
1030 char devnm[32];
1031 strcpy(devnm, fd2devnm(fd));
1032 lfd = open_dev_excl(devnm);
d070235d
N
1033 if (lfd < 0) {
1034 pr_err("Cannot get exclusive access "
1035 " to container - odd\n");
1036 return -1;
1037 }
aab15415
N
1038 /* We may not be able to check on holders in
1039 * sysfs, either because we don't have the dev num
1040 * (rdev == 0) or because the device has been detached
1041 * and the 'holders' directory no longer exists
1042 * (ret == -1). In that case, assume it is OK to
1043 * remove.
d070235d 1044 */
aab15415
N
1045 if (rdev == 0)
1046 ret = -1;
1047 else
4dd2df09 1048 ret = sysfs_unique_holder(devnm, rdev);
aab15415
N
1049 if (ret == 0) {
1050 pr_err("%s is not a member, cannot remove.\n",
1051 dv->devname);
1052 close(lfd);
1053 return -1;
1054 }
1055 if (ret >= 2) {
1056 pr_err("%s is still in use, cannot remove.\n",
1057 dv->devname);
d070235d
N
1058 close(lfd);
1059 return -1;
1060 }
1061 }
1062 /* FIXME check that it is a current member */
1063 if (sysfd >= 0) {
1064 /* device has been removed and we don't know
1065 * the major:minor number
1066 */
1067 int n = write(sysfd, "remove", 6);
1068 if (n != 6)
1069 err = -1;
1070 else
1071 err = 0;
1072 } else {
1073 err = ioctl(fd, HOT_REMOVE_DISK, rdev);
1074 if (err && errno == ENODEV) {
1075 /* Old kernels rejected this if no personality
1076 * is registered */
4dd2df09 1077 struct mdinfo *sra = sysfs_read(fd, NULL, GET_DEVS);
d070235d
N
1078 struct mdinfo *dv = NULL;
1079 if (sra)
1080 dv = sra->devs;
1081 for ( ; dv ; dv=dv->next)
1082 if (dv->disk.major == (int)major(rdev) &&
1083 dv->disk.minor == (int)minor(rdev))
1084 break;
1085 if (dv)
1086 err = sysfs_set_str(sra, dv,
1087 "state", "remove");
1088 else
1089 err = -1;
1090 if (sra)
1091 sysfs_free(sra);
1092 }
1093 }
1094 if (err) {
1095 pr_err("hot remove failed "
1096 "for %s: %s\n", dv->devname,
1097 strerror(errno));
1098 if (lfd >= 0)
1099 close(lfd);
1100 return -1;
1101 }
1102 if (tst->ss->external) {
1103 /*
1104 * Before dropping our exclusive open we make an
1105 * attempt at preventing mdmon from seeing an
1106 * 'add' event before reconciling this 'remove'
1107 * event.
1108 */
4dd2df09 1109 char *devnm = fd2devnm(fd);
d070235d 1110
4dd2df09 1111 if (!devnm) {
d070235d
N
1112 pr_err("unable to get container name\n");
1113 return -1;
1114 }
1115
4dd2df09 1116 ping_manager(devnm);
d070235d
N
1117 }
1118 if (lfd >= 0)
1119 close(lfd);
1120 if (verbose >= 0)
1121 pr_err("hot removed %s from %s\n",
1122 dv->devname, devname);
1123 return 1;
1124}
1125
70c55e36
N
1126int Manage_replace(struct supertype *tst, int fd, struct mddev_dev *dv,
1127 unsigned long rdev, int verbose, char *devname)
1128{
1129 struct mdinfo *mdi, *di;
1130 if (tst->ss->external) {
1131 pr_err("--replace only supported for native metadata (0.90 or 1.x)\n");
1132 return -1;
1133 }
1134 /* Need to find the device in sysfs and add 'want_replacement' to the
1135 * status.
1136 */
4dd2df09 1137 mdi = sysfs_read(fd, NULL, GET_DEVS);
70c55e36
N
1138 if (!mdi || !mdi->devs) {
1139 pr_err("Cannot find status of %s to enable replacement - strange\n",
1140 devname);
1141 return -1;
1142 }
1143 for (di = mdi->devs; di; di = di->next)
1144 if (di->disk.major == (int)major(rdev) &&
1145 di->disk.minor == (int)minor(rdev))
1146 break;
1147 if (di) {
1148 int rv;
1149 if (di->disk.raid_disk < 0) {
1150 pr_err("%s is not active and so cannot be replaced.\n",
1151 dv->devname);
1152 sysfs_free(mdi);
1153 return -1;
1154 }
1155 rv = sysfs_set_str(mdi, di,
1156 "state", "want_replacement");
1157 if (rv) {
1158 sysfs_free(mdi);
1159 pr_err("Failed to request replacement for %s\n",
1160 dv->devname);
1161 return -1;
1162 }
1163 if (verbose >= 0)
1164 pr_err("Marked %s (device %d in %s) for replacement\n",
1165 dv->devname, di->disk.raid_disk, devname);
1166 /* If there is a matching 'with', we need to tell it which
1167 * raid disk
1168 */
1169 while (dv && dv->disposition != 'W')
1170 dv = dv->next;
1171 if (dv) {
1172 dv->disposition = 'w';
1173 dv->used = di->disk.raid_disk;
1174 }
1175 return 1;
1176 }
1177 sysfs_free(mdi);
1178 pr_err("%s not found in %s so cannot --replace it\n",
1179 dv->devname, devname);
1180 return -1;
1181}
1182
1183int Manage_with(struct supertype *tst, int fd, struct mddev_dev *dv,
1184 unsigned long rdev, int verbose, char *devname)
1185{
1186 struct mdinfo *mdi, *di;
1187 /* try to set 'slot' for 'rdev' in 'fd' to 'dv->used' */
4dd2df09 1188 mdi = sysfs_read(fd, NULL, GET_DEVS|GET_STATE);
70c55e36
N
1189 if (!mdi || !mdi->devs) {
1190 pr_err("Cannot find status of %s to enable replacement - strange\n",
1191 devname);
1192 return -1;
1193 }
1194 for (di = mdi->devs; di; di = di->next)
1195 if (di->disk.major == (int)major(rdev) &&
1196 di->disk.minor == (int)minor(rdev))
1197 break;
1198 if (di) {
1199 int rv;
1200 if (di->disk.state & (1<<MD_DISK_FAULTY)) {
1201 pr_err("%s is faulty and cannot be a replacement\n",
1202 dv->devname);
1203 sysfs_free(mdi);
1204 return -1;
1205 }
1206 if (di->disk.raid_disk >= 0) {
1207 pr_err("%s is active and cannot be a replacement\n",
1208 dv->devname);
1209 sysfs_free(mdi);
1210 return -1;
1211 }
1212 rv = sysfs_set_num(mdi, di,
1213 "slot", dv->used);
1214 if (rv) {
1215 sysfs_free(mdi);
51425978 1216 pr_err("Failed to set %s as preferred replacement.\n",
70c55e36
N
1217 dv->devname);
1218 return -1;
1219 }
1220 if (verbose >= 0)
1221 pr_err("Marked %s in %s as replacement for device %d\n",
1222 dv->devname, devname, dv->used);
1223 return 1;
1224 }
1225 sysfs_free(mdi);
1226 pr_err("%s not found in %s so cannot make it preferred replacement\n",
1227 dv->devname, devname);
1228 return -1;
1229}
1230
64c4757e 1231int Manage_subdevs(char *devname, int fd,
833bb0f8 1232 struct mddev_dev *devlist, int verbose, int test,
11b391ec 1233 char *update, int force)
cd29a5c8 1234{
7bd04da9 1235 /* Do something to each dev.
682c7051
NB
1236 * devmode can be
1237 * 'a' - add the device
1238 * try HOT_ADD_DISK
1239 * If that fails EINVAL, try ADD_NEW_DISK
7bd04da9
N
1240 * 'A' - re-add the device
1241 * 'r' - remove the device: HOT_REMOVE_DISK
b80da661
NB
1242 * device can be 'faulty' or 'detached' in which case all
1243 * matching devices are removed.
682c7051 1244 * 'f' - set the device faulty SET_DISK_FAULTY
b80da661
NB
1245 * device can be 'detached' in which case any device that
1246 * is inaccessible will be marked faulty.
70c55e36
N
1247 * 'R' - mark this device as wanting replacement.
1248 * 'W' - this device is added if necessary and activated as
1249 * a replacement for a previous 'R' device.
1250 * -----
1251 * 'w' - 'W' will be changed to 'w' when it is paired with
1252 * a 'R' device. If a 'W' is found while walking the list
1253 * it must be unpaired, and is an error.
1254 * 'M' - this is created by a 'missing' target. It is a slight
1255 * variant on 'A'
262e3b7f
N
1256 * 'F' - Another variant of 'A', where the device was faulty
1257 * so must be removed from the array first.
70c55e36 1258 *
98d27e39
N
1259 * For 'f' and 'r', the device can also be a kernel-internal
1260 * name such as 'sdb'.
682c7051
NB
1261 */
1262 mdu_array_info_t array;
7a3be72f 1263 unsigned long long array_size;
1d997643 1264 struct mddev_dev *dv;
682c7051 1265 struct stat stb;
cfad27a9 1266 int tfd = -1;
38aeaf3a 1267 struct supertype *tst;
4725bc31 1268 char *subarray = NULL;
98d27e39 1269 int sysfd = -1;
7d2e6486 1270 int count = 0; /* number of actions taken */
9f584691
N
1271 struct mdinfo info;
1272 int frozen = 0;
8af530b0 1273 int busy = 0;
682c7051
NB
1274
1275 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
7bd04da9 1276 pr_err("Cannot get array info for %s\n",
682c7051 1277 devname);
bcbb3112 1278 goto abort;
682c7051 1279 }
4dd2df09 1280 sysfs_init(&info, fd, NULL);
3da92f27 1281
7bd04da9 1282 /* array.size is only 32 bits and may be truncated.
7a3be72f
NB
1283 * So read from sysfs if possible, and record number of sectors
1284 */
1285
1286 array_size = get_component_size(fd);
1287 if (array_size <= 0)
1288 array_size = array.size * 2;
1289
4725bc31 1290 tst = super_by_fd(fd, &subarray);
3da92f27 1291 if (!tst) {
e7b84f9d 1292 pr_err("unsupport array - version %d.%d\n",
3da92f27 1293 array.major_version, array.minor_version);
bcbb3112 1294 goto abort;
3da92f27
NB
1295 }
1296
b3b4e8a7 1297 stb.st_rdev = 0;
38aeaf3a 1298 for (dv = devlist; dv; dv = dv->next) {
38aeaf3a 1299 int rv;
4a39c6f2 1300
1d997643
N
1301 if (strcmp(dv->devname, "failed") == 0 ||
1302 strcmp(dv->devname, "faulty") == 0) {
262e3b7f
N
1303 if (dv->disposition != 'A'
1304 && dv->disposition != 'r') {
e7b84f9d 1305 pr_err("%s only meaningful "
262e3b7f 1306 "with -r or --re-add, not -%c\n",
b80da661 1307 dv->devname, dv->disposition);
bcbb3112 1308 goto abort;
b80da661 1309 }
262e3b7f
N
1310 add_faulty(dv, fd, (dv->disposition == 'A'
1311 ? 'F' : 'r'));
1d997643
N
1312 continue;
1313 }
1314 if (strcmp(dv->devname, "detached") == 0) {
b80da661 1315 if (dv->disposition != 'r' && dv->disposition != 'f') {
e7b84f9d 1316 pr_err("%s only meaningful "
b80da661
NB
1317 "with -r of -f, not -%c\n",
1318 dv->devname, dv->disposition);
bcbb3112 1319 goto abort;
b80da661 1320 }
1d997643
N
1321 add_detached(dv, fd, dv->disposition);
1322 continue;
1323 }
1324
1325 if (strcmp(dv->devname, "missing") == 0) {
1326 struct mddev_dev *add_devlist = NULL;
1327 struct mddev_dev **dp;
c8e1a230 1328 if (dv->disposition != 'A') {
e7b84f9d 1329 pr_err("'missing' only meaningful "
aab15415 1330 "with --re-add\n");
bcbb3112 1331 goto abort;
a4e13010 1332 }
1d997643 1333 add_devlist = conf_get_devs();
a4e13010 1334 if (add_devlist == NULL) {
e7b84f9d 1335 pr_err("no devices to scan for missing members.");
a4e13010
N
1336 continue;
1337 }
1d997643 1338 for (dp = &add_devlist; *dp; dp = & (*dp)->next)
7bd04da9 1339 /* 'M' (for 'missing') is like 'A' without errors */
1d997643
N
1340 (*dp)->disposition = 'M';
1341 *dp = dv->next;
1342 dv->next = add_devlist;
1343 continue;
1344 }
1345
64a78416
N
1346 if (strncmp(dv->devname, "set-", 4) == 0 &&
1347 strlen(dv->devname) == 5) {
1348 int copies;
1349
1350 if (dv->disposition != 'r' &&
1351 dv->disposition != 'f') {
1352 pr_err("'%s' only meaningful with -r or -f\n",
1353 dv->devname);
1354 goto abort;
1355 }
1356 if (array.level != 10) {
1357 pr_err("'%s' only meaningful with RAID10 arrays\n",
1358 dv->devname);
1359 goto abort;
1360 }
1361 copies = ((array.layout & 0xff) *
1362 ((array.layout >> 8) & 0xff));
1363 if (array.raid_disks % copies != 0 ||
1364 dv->devname[4] < 'A' ||
1365 dv->devname[4] >= 'A' + copies ||
1366 copies > 26) {
1367 pr_err("'%s' not meaningful with this array\n",
1368 dv->devname);
1369 goto abort;
1370 }
1371 add_set(dv, fd, dv->devname[4]);
1372 continue;
1373 }
1374
1d997643 1375 if (strchr(dv->devname, '/') == NULL &&
aab15415
N
1376 strchr(dv->devname, ':') == NULL &&
1377 strlen(dv->devname) < 50) {
98d27e39
N
1378 /* Assume this is a kernel-internal name like 'sda1' */
1379 int found = 0;
1380 char dname[55];
1381 if (dv->disposition != 'r' && dv->disposition != 'f') {
e7b84f9d 1382 pr_err("%s only meaningful "
cfad27a9 1383 "with -r or -f, not -%c\n",
98d27e39 1384 dv->devname, dv->disposition);
bcbb3112 1385 goto abort;
98d27e39
N
1386 }
1387
1388 sprintf(dname, "dev-%s", dv->devname);
4dd2df09 1389 sysfd = sysfs_open(fd2devnm(fd), dname, "block/dev");
98d27e39
N
1390 if (sysfd >= 0) {
1391 char dn[20];
1392 int mj,mn;
1393 if (sysfs_fd_get_str(sysfd, dn, 20) > 0 &&
1394 sscanf(dn, "%d:%d", &mj,&mn) == 2) {
1395 stb.st_rdev = makedev(mj,mn);
1396 found = 1;
1397 }
1398 close(sysfd);
1399 sysfd = -1;
1400 }
1401 if (!found) {
4dd2df09 1402 sysfd = sysfs_open(fd2devnm(fd), dname, "state");
98d27e39 1403 if (sysfd < 0) {
e7b84f9d 1404 pr_err("%s does not appear "
98d27e39
N
1405 "to be a component of %s\n",
1406 dv->devname, devname);
bcbb3112 1407 goto abort;
98d27e39
N
1408 }
1409 }
b80da661 1410 } else {
c7b47447 1411 tfd = dev_open(dv->devname, O_RDONLY);
5fe7f5f7
N
1412 if (tfd >= 0)
1413 fstat(tfd, &stb);
5a9de8db 1414 else {
5fe7f5f7
N
1415 int open_err = errno;
1416 if (stat(dv->devname, &stb) != 0) {
1417 pr_err("Cannot find %s: %s\n",
1418 dv->devname, strerror(errno));
1419 goto abort;
1420 }
1421 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
1422 if (dv->disposition == 'M')
1423 /* non-fatal. Also improbable */
1424 continue;
1425 pr_err("%s is not a block device.\n",
1426 dv->devname);
1427 goto abort;
1428 }
1429 if (dv->disposition == 'r')
1430 /* Be happy, the stat worked, that is
1431 * enough for --remove
1432 */
1433 ;
1434 else {
1d997643
N
1435 if (dv->disposition == 'M')
1436 /* non-fatal */
1437 continue;
839f27a3 1438 pr_err("Cannot open %s: %s\n",
5fe7f5f7 1439 dv->devname, strerror(open_err));
bcbb3112 1440 goto abort;
5a9de8db 1441 }
b80da661 1442 }
682c7051 1443 }
cd29a5c8 1444 switch(dv->disposition){
682c7051 1445 default:
e7b84f9d 1446 pr_err("internal error - devmode[%s]=%d\n",
c913b90e 1447 dv->devname, dv->disposition);
bcbb3112 1448 goto abort;
682c7051 1449 case 'a':
c8e1a230 1450 case 'A':
262e3b7f
N
1451 case 'M': /* --re-add missing */
1452 case 'F': /* --re-add faulty */
4a39c6f2 1453 /* add the device */
4725bc31 1454 if (subarray) {
e7b84f9d 1455 pr_err("Cannot add disks to a"
f7dd881f
DW
1456 " \'member\' array, perform this"
1457 " operation on the parent container\n");
bcbb3112 1458 goto abort;
f94d52f4 1459 }
262e3b7f
N
1460 if (dv->disposition == 'F')
1461 /* Need to remove first */
1462 ioctl(fd, HOT_REMOVE_DISK,
1463 (unsigned long)stb.st_rdev);
f277ce36 1464 /* Make sure it isn't in use (in 2.6 or later) */
1d997643 1465 tfd = dev_open(dv->devname, O_RDONLY|O_EXCL);
38aeaf3a
N
1466 if (tfd >= 0) {
1467 /* We know no-one else is using it. We'll
1468 * need non-exclusive access to add it, so
1469 * do that now.
1470 */
1471 close(tfd);
1472 tfd = dev_open(dv->devname, O_RDONLY);
1011e834 1473 }
0fbf459d 1474 if (tfd < 0) {
1d997643
N
1475 if (dv->disposition == 'M')
1476 continue;
e7b84f9d 1477 pr_err("Cannot open %s: %s\n",
d7eaf49f 1478 dv->devname, strerror(errno));
bcbb3112 1479 goto abort;
d7eaf49f 1480 }
9f584691
N
1481 if (!frozen) {
1482 if (sysfs_freeze_array(&info) == 1)
1483 frozen = 1;
1484 else
1485 frozen = -1;
1486 }
38aeaf3a
N
1487 rv = Manage_add(fd, tfd, dv, tst, &array,
1488 force, verbose, devname, update,
1489 stb.st_rdev, array_size);
1490 close(tfd);
1491 tfd = -1;
1492 if (rv < 0)
bcbb3112 1493 goto abort;
38aeaf3a
N
1494 if (rv > 0)
1495 count++;
682c7051
NB
1496 break;
1497
1498 case 'r':
1499 /* hot remove */
4725bc31 1500 if (subarray) {
e7b84f9d 1501 pr_err("Cannot remove disks from a"
f7dd881f
DW
1502 " \'member\' array, perform this"
1503 " operation on the parent container\n");
d070235d
N
1504 rv = -1;
1505 } else
1506 rv = Manage_remove(tst, fd, dv, sysfd,
1507 stb.st_rdev, verbose,
1508 devname);
1509 if (sysfd >= 0)
98d27e39 1510 close(sysfd);
d070235d
N
1511 sysfd = -1;
1512 if (rv < 0)
bcbb3112 1513 goto abort;
d070235d
N
1514 if (rv > 0)
1515 count++;
682c7051
NB
1516 break;
1517
1518 case 'f': /* set faulty */
1519 /* FIXME check current member */
98d27e39
N
1520 if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
1521 (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
1522 (unsigned long) stb.st_rdev))) {
8af530b0
N
1523 if (errno == EBUSY)
1524 busy = 1;
e7b84f9d 1525 pr_err("set device faulty failed for %s: %s\n",
1d997643 1526 dv->devname, strerror(errno));
98d27e39
N
1527 if (sysfd >= 0)
1528 close(sysfd);
bcbb3112 1529 goto abort;
682c7051 1530 }
98d27e39
N
1531 if (sysfd >= 0)
1532 close(sysfd);
1533 sysfd = -1;
7d2e6486 1534 count++;
dab6685f 1535 if (verbose >= 0)
e7b84f9d 1536 pr_err("set %s faulty in %s\n",
1d997643 1537 dv->devname, devname);
682c7051 1538 break;
70c55e36
N
1539 case 'R': /* Mark as replaceable */
1540 if (subarray) {
1541 pr_err("Cannot replace disks in a"
1542 " \'member\' array, perform this"
1543 " operation on the parent container\n");
1544 rv = -1;
1545 } else {
1546 if (!frozen) {
1547 if (sysfs_freeze_array(&info) == 1)
1548 frozen = 1;
1549 else
1550 frozen = -1;
1551 }
1552 rv = Manage_replace(tst, fd, dv,
1553 stb.st_rdev, verbose,
1554 devname);
1555 }
1556 if (rv < 0)
1557 goto abort;
1558 if (rv > 0)
1559 count++;
1560 break;
1561 case 'W': /* --with device that doesn't match */
1562 pr_err("No matching --replace device for --with %s\n",
1563 dv->devname);
1564 goto abort;
1565 case 'w': /* --with device which was matched */
1566 rv = Manage_with(tst, fd, dv,
1567 stb.st_rdev, verbose, devname);
1568 if (rv < 0)
1569 goto abort;
1570 break;
682c7051
NB
1571 }
1572 }
9f584691
N
1573 if (frozen > 0)
1574 sysfs_set_str(&info, NULL, "sync_action","idle");
7d2e6486
N
1575 if (test && count == 0)
1576 return 2;
682c7051 1577 return 0;
bcbb3112
N
1578
1579abort:
9f584691
N
1580 if (frozen > 0)
1581 sysfs_set_str(&info, NULL, "sync_action","idle");
8af530b0 1582 return !test && busy ? 2 : 1;
64c4757e 1583}
1f48664b
NB
1584
1585int autodetect(void)
1586{
1587 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1588 int rv = 1;
1589 int fd = dev_open("9:0", O_RDONLY);
1590 if (fd >= 0) {
1591 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
1592 rv = 0;
1593 close(fd);
1594 }
1595 return rv;
1596}
aa534678 1597
ba728be7 1598int Update_subarray(char *dev, char *subarray, char *update, struct mddev_ident *ident, int verbose)
aa534678
DW
1599{
1600 struct supertype supertype, *st = &supertype;
1601 int fd, rv = 2;
1602
1603 memset(st, 0, sizeof(*st));
aa534678 1604
ba728be7 1605 fd = open_subarray(dev, subarray, st, verbose < 0);
aa534678
DW
1606 if (fd < 0)
1607 return 2;
1608
1609 if (!st->ss->update_subarray) {
ba728be7 1610 if (verbose >= 0)
e7b84f9d
N
1611 pr_err("Operation not supported for %s metadata\n",
1612 st->ss->name);
aa534678
DW
1613 goto free_super;
1614 }
1615
4dd2df09 1616 if (mdmon_running(st->devnm))
aa534678
DW
1617 st->update_tail = &st->updates;
1618
a951a4f7 1619 rv = st->ss->update_subarray(st, subarray, update, ident);
aa534678
DW
1620
1621 if (rv) {
ba728be7 1622 if (verbose >= 0)
e7b84f9d 1623 pr_err("Failed to update %s of subarray-%s in %s\n",
aa534678
DW
1624 update, subarray, dev);
1625 } else if (st->update_tail)
1626 flush_metadata_updates(st);
1627 else
1628 st->ss->sync_metadata(st);
1629
ba728be7 1630 if (rv == 0 && strcmp(update, "name") == 0 && verbose >= 0)
e7b84f9d
N
1631 pr_err("Updated subarray-%s name from %s, UUIDs may have changed\n",
1632 subarray, dev);
aa534678
DW
1633
1634 free_super:
1635 st->ss->free_super(st);
1636 close(fd);
1637
1638 return rv;
1639}
d52bb542 1640
7bd04da9
N
1641/* Move spare from one array to another If adding to destination array fails
1642 * add back to original array.
d52bb542
AC
1643 * Returns 1 on success, 0 on failure */
1644int move_spare(char *from_devname, char *to_devname, dev_t devid)
1645{
1646 struct mddev_dev devlist;
1647 char devname[20];
1648
1649 /* try to remove and add */
1650 int fd1 = open(to_devname, O_RDONLY);
1651 int fd2 = open(from_devname, O_RDONLY);
1652
1653 if (fd1 < 0 || fd2 < 0) {
1654 if (fd1>=0) close(fd1);
1655 if (fd2>=0) close(fd2);
1656 return 0;
1657 }
1658
1659 devlist.next = NULL;
1660 devlist.used = 0;
d52bb542
AC
1661 devlist.writemostly = 0;
1662 devlist.devname = devname;
1663 sprintf(devname, "%d:%d", major(devid), minor(devid));
1664
1665 devlist.disposition = 'r';
11b391ec 1666 if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0) == 0) {
d52bb542 1667 devlist.disposition = 'a';
11b391ec 1668 if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0, NULL, 0) == 0) {
d52bb542
AC
1669 /* make sure manager is aware of changes */
1670 ping_manager(to_devname);
1671 ping_manager(from_devname);
1672 close(fd1);
1673 close(fd2);
1674 return 1;
1675 }
11b391ec 1676 else Manage_subdevs(from_devname, fd2, &devlist, -1, 0, NULL, 0);
d52bb542
AC
1677 }
1678 close(fd1);
1679 close(fd2);
1680 return 0;
1681}
435d4ebb 1682#endif