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