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