]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Manage.c
Fix assembling RAID volume by using incremental
[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
NB
29
30int Manage_ro(char *devname, int fd, int readonly)
31{
682c7051
NB
32 /* switch to readonly or rw
33 *
34 * requires >= 0.90.0
35 * first check that array is runing
36 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
37 *
38 */
e9dd1598 39 struct mdinfo *mdi;
b73e45ae 40 int rv = 0;
aba69144 41
7bd04da9 42 /* If this is an externally-managed array, we need to modify the
e9dd1598
N
43 * metadata_version so that mdmon doesn't undo our change.
44 */
4dd2df09 45 mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION);
e9dd1598
N
46 if (mdi &&
47 mdi->array.major_version == -1 &&
e9dd1598
N
48 is_subarray(mdi->text_version)) {
49 char vers[64];
50 strcpy(vers, "external:");
51 strcat(vers, mdi->text_version);
52 if (readonly > 0) {
53 int rv;
54 /* We set readonly ourselves. */
55 vers[9] = '-';
56 sysfs_set_str(mdi, NULL, "metadata_version", vers);
57
58 close(fd);
59 rv = sysfs_set_str(mdi, NULL, "array_state", "readonly");
60
61 if (rv < 0) {
e7b84f9d 62 pr_err("failed to set readonly for %s: %s\n",
e9dd1598
N
63 devname, strerror(errno));
64
65 vers[9] = mdi->text_version[0];
66 sysfs_set_str(mdi, NULL, "metadata_version", vers);
b73e45ae
JS
67 rv = 1;
68 goto out;
e9dd1598
N
69 }
70 } else {
71 char *cp;
72 /* We cannot set read/write - must signal mdmon */
73 vers[9] = '/';
74 sysfs_set_str(mdi, NULL, "metadata_version", vers);
75
76 cp = strchr(vers+10, '/');
1471b8b1 77 if (cp)
e9dd1598
N
78 *cp = 0;
79 ping_monitor(vers+10);
9ea5a252
DW
80 if (mdi->array.level <= 0)
81 sysfs_set_str(mdi, NULL, "array_state", "active");
e9dd1598 82 }
b73e45ae 83 goto out;
e9dd1598 84 }
32141c17 85
80223cb4
JS
86 if (!md_array_active(fd)) {
87 pr_err("%s does not appear to be active.\n", devname);
b73e45ae
JS
88 rv = 1;
89 goto out;
682c7051 90 }
aba69144 91
7bd04da9 92 if (readonly > 0) {
682c7051 93 if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
e7b84f9d 94 pr_err("failed to set readonly for %s: %s\n",
682c7051 95 devname, strerror(errno));
b73e45ae
JS
96 rv = 1;
97 goto out;
682c7051
NB
98 }
99 } else if (readonly < 0) {
100 if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
e7b84f9d 101 pr_err("failed to set writable for %s: %s\n",
682c7051 102 devname, strerror(errno));
b73e45ae
JS
103 rv = 1;
104 goto out;
682c7051
NB
105 }
106 }
b73e45ae 107out:
6e8d27e7 108 sysfs_free(mdi);
b73e45ae 109 return rv;
64c4757e
NB
110}
111
4dd2df09 112static void remove_devices(char *devnm, char *path)
4ccad7b1 113{
7bd04da9 114 /*
b1702f48
N
115 * Remove names at 'path' - possibly with
116 * partition suffixes - which link to the 'standard'
4dd2df09 117 * name for devnm. These were probably created
b1702f48 118 * by mdadm when the array was assembled.
4ccad7b1
N
119 */
120 char base[40];
121 char *path2;
122 char link[1024];
123 int n;
124 int part;
125 char *be;
126 char *pe;
127
b1702f48
N
128 if (!path)
129 return;
130
4dd2df09 131 sprintf(base, "/dev/%s", devnm);
4ccad7b1 132 be = base + strlen(base);
b1702f48 133
503975b9 134 path2 = xmalloc(strlen(path)+20);
b1702f48
N
135 strcpy(path2, path);
136 pe = path2 + strlen(path2);
7bd04da9 137
4ccad7b1
N
138 for (part = 0; part < 16; part++) {
139 if (part) {
140 sprintf(be, "p%d", part);
b1702f48
N
141
142 if (isdigit(pe[-1]))
143 sprintf(pe, "p%d", part);
144 else
145 sprintf(pe, "%d", part);
4ccad7b1 146 }
b1702f48 147 n = readlink(path2, link, sizeof(link));
d9ca03e9 148 if (n > 0 && (int)strlen(base) == n &&
b1702f48
N
149 strncmp(link, base, n) == 0)
150 unlink(path2);
4ccad7b1 151 }
0eb26465 152 free(path2);
4ccad7b1 153}
4ccad7b1 154
d5a40416 155int Manage_run(char *devname, int fd, struct context *c)
64c4757e 156{
fe7e0e64
N
157 /* Run the array. Array must already be configured
158 * Requires >= 0.90.0
682c7051 159 */
d3786cdc 160 char nm[32], *nmp;
82b27616 161
d3786cdc
N
162 nmp = fd2devnm(fd);
163 if (!nmp) {
164 pr_err("Cannot find %s in sysfs!!\n", devname);
fe7e0e64
N
165 return 1;
166 }
d3786cdc 167 strcpy(nm, nmp);
d5a40416 168 return IncrementalScan(c, nm);
fe7e0e64
N
169}
170
171int Manage_stop(char *devname, int fd, int verbose, int will_retry)
172{
173 /* Stop the array. Array must already be configured
174 * 'will_retry' means that error messages are not wanted.
175 */
176 int rv = 0;
177 struct map_ent *map = NULL;
178 struct mdinfo *mdi;
179 char devnm[32];
180 char container[32];
181 int err;
182 int count;
2eba8496
N
183 char buf[32];
184 unsigned long long rd1, rd2;
fe7e0e64 185
ba728be7
N
186 if (will_retry && verbose == 0)
187 verbose = -1;
188
fe7e0e64
N
189 strcpy(devnm, fd2devnm(fd));
190 /* Get EXCL access first. If this fails, then attempting
191 * to stop is probably a bad idea.
192 */
2eba8496 193 mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_COMPONENT|GET_VERSION);
fe7e0e64
N
194 if (mdi && is_subarray(mdi->text_version)) {
195 char *sl;
196 strncpy(container, mdi->text_version+1, sizeof(container));
197 container[sizeof(container)-1] = 0;
198 sl = strchr(container, '/');
199 if (sl)
200 *sl = 0;
201 } else
202 container[0] = 0;
203 close(fd);
204 count = 5;
52b6ccad 205 while (((fd = ((devname[0] == '/')
fe7e0e64 206 ?open(devname, O_RDONLY|O_EXCL)
fc54fe7a
JS
207 :open_dev_flags(devnm, O_RDONLY|O_EXCL))) < 0 ||
208 strcmp(fd2devnm(fd), devnm) != 0) && container[0] &&
209 mdmon_running(container) && count) {
932be627
N
210 /* Can't open, so something might be wrong. However it
211 * is a container, so we might be racing with mdmon, so
212 * retry for a bit.
213 */
fe7e0e64
N
214 if (fd >= 0)
215 close(fd);
216 flush_mdmon(container);
217 count--;
682c7051 218 }
fe7e0e64
N
219 if (fd < 0 || strcmp(fd2devnm(fd), devnm) != 0) {
220 if (fd >= 0)
221 close(fd);
ba728be7 222 if (verbose >= 0)
7a862a02 223 pr_err("Cannot get exclusive access to %s:Perhaps a running process, mounted filesystem or active volume group?\n",
fe7e0e64 224 devname);
f6feb3fb 225 sysfs_free(mdi);
fe7e0e64
N
226 return 1;
227 }
932be627
N
228 /* If this is an mdmon managed array, just write 'inactive'
229 * to the array state and let mdmon clear up.
230 */
fe7e0e64
N
231 if (mdi &&
232 mdi->array.level > 0 &&
233 is_subarray(mdi->text_version)) {
eb0af526 234 int err;
fe7e0e64 235 /* This is mdmon managed. */
eb0af526 236 close(fd);
daf7a3ce 237
932be627 238 /* As we had an O_EXCL open, any use of the device
eb0af526
N
239 * which blocks STOP_ARRAY is probably a transient use,
240 * so it is reasonable to retry for a while - 5 seconds.
241 */
fe7e0e64
N
242 count = 25;
243 while (count &&
244 (err = sysfs_set_str(mdi, NULL,
245 "array_state",
fc54fe7a
JS
246 "inactive")) < 0 &&
247 errno == EBUSY) {
239b3cc0 248 sleep_for(0, MSEC_TO_NSEC(200), true);
fe7e0e64 249 count--;
eb0af526 250 }
fe7e0e64
N
251 if (err) {
252 if (verbose >= 0)
e7b84f9d
N
253 pr_err("failed to stop array %s: %s\n",
254 devname, strerror(errno));
bccd8153
JS
255 rv = 1;
256 goto out;
682c7051 257 }
fe7e0e64
N
258
259 /* Give monitor a chance to act */
260 ping_monitor(mdi->text_version);
261
262 fd = open_dev_excl(devnm);
263 if (fd < 0) {
264 if (verbose >= 0)
7a862a02 265 pr_err("failed to completely stop %s: Device is busy\n",
fe7e0e64
N
266 devname);
267 rv = 1;
268 goto out;
269 }
270 } else if (mdi &&
271 mdi->array.major_version == -1 &&
272 mdi->array.minor_version == -2 &&
273 !is_subarray(mdi->text_version)) {
274 struct mdstat_ent *mds, *m;
275 /* container, possibly mdmon-managed.
276 * Make sure mdmon isn't opening it, which
277 * would interfere with the 'stop'
97590376 278 */
fe7e0e64 279 ping_monitor(mdi->sys_name);
daf7a3ce 280
fe7e0e64
N
281 /* now check that there are no existing arrays
282 * which are members of this array
283 */
284 mds = mdstat_read(0, 0);
285 for (m = mds; m; m = m->next)
286 if (m->metadata_version &&
287 strncmp(m->metadata_version, "external:", 9)==0 &&
288 metadata_container_matches(m->metadata_version+9,
289 devnm)) {
290 if (verbose >= 0)
7a862a02 291 pr_err("Cannot stop container %s: member %s still active\n",
9581efb1 292 devname, m->devnm);
fe7e0e64
N
293 free_mdstat(mds);
294 rv = 1;
295 goto out;
296 }
297 }
298
2eba8496
N
299 /* If the array is undergoing a reshape which changes the number
300 * of devices, then it would be nice to stop it at a point where
301 * it has completed a full number of stripes in both old and
302 * new layouts as this will allow the reshape to be reverted.
303 * So if 'sync_action' is "reshape" and 'raid_disks' shows two
304 * different numbers, then
305 * - freeze reshape
306 * - set sync_max to next multiple of both data_disks and
307 * chunk sizes (or next but one)
308 * - unfreeze reshape
309 * - wait on 'sync_completed' for that point to be reached.
310 */
70f1ff42 311 if (mdi && is_level456(mdi->array.level) &&
2eba8496
N
312 sysfs_attribute_available(mdi, NULL, "sync_action") &&
313 sysfs_attribute_available(mdi, NULL, "reshape_direction") &&
314 sysfs_get_str(mdi, NULL, "sync_action", buf, 20) > 0 &&
315 strcmp(buf, "reshape\n") == 0 &&
e3e0d0a8 316 sysfs_get_two(mdi, NULL, "raid_disks", &rd1, &rd2) == 2) {
2eba8496
N
317 unsigned long long position, curr;
318 unsigned long long chunk1, chunk2;
319 unsigned long long rddiv, chunkdiv;
320 unsigned long long sectors;
5509dc44
N
321 unsigned long long sync_max, old_sync_max;
322 unsigned long long completed;
2eba8496
N
323 int backwards = 0;
324 int delay;
325 int scfd;
326
e3e0d0a8
N
327 delay = 40;
328 while (rd1 > rd2 && delay > 0 &&
329 sysfs_get_ll(mdi, NULL, "sync_max", &old_sync_max) == 0) {
330 /* must be in the critical section - wait a bit */
331 delay -= 1;
239b3cc0 332 sleep_for(0, MSEC_TO_NSEC(100), true);
e3e0d0a8
N
333 }
334
335 if (sysfs_set_str(mdi, NULL, "sync_action", "frozen") != 0)
336 goto done;
337 /* Array is frozen */
338
2eba8496
N
339 rd1 -= mdi->array.level == 6 ? 2 : 1;
340 rd2 -= mdi->array.level == 6 ? 2 : 1;
341 sysfs_get_str(mdi, NULL, "reshape_direction", buf, sizeof(buf));
342 if (strncmp(buf, "back", 4) == 0)
343 backwards = 1;
e3e0d0a8
N
344 if (sysfs_get_ll(mdi, NULL, "reshape_position", &position) != 0) {
345 /* reshape must have finished now */
346 sysfs_set_str(mdi, NULL, "sync_action", "idle");
347 goto done;
348 }
2eba8496
N
349 sysfs_get_two(mdi, NULL, "chunk_size", &chunk1, &chunk2);
350 chunk1 /= 512;
351 chunk2 /= 512;
352 rddiv = GCD(rd1, rd2);
353 chunkdiv = GCD(chunk1, chunk2);
354 sectors = (chunk1/chunkdiv) * chunk2 * (rd1/rddiv) * rd2;
355
356 if (backwards) {
357 /* Need to subtract 'reshape_position' from
358 * array size to get equivalent of sync_max.
359 * Size calculation based on raid5_size in kernel.
360 */
361 unsigned long long size = mdi->component_size;
362 size &= ~(chunk1-1);
363 size &= ~(chunk2-1);
364 /* rd1 must be smaller */
932be627
N
365 /* Reshape may have progressed further backwards than
366 * recorded, so target even further back (hence "-1")
367 */
5509dc44 368 position = (position / sectors - 1) * sectors;
932be627
N
369 /* rd1 is always the conversion factor between 'sync'
370 * position and 'reshape' position.
371 * We read 1 "new" stripe worth of data from where-ever,
372 * and when write out that full stripe.
373 */
5509dc44 374 sync_max = size - position/rd1;
2eba8496 375 } else {
932be627
N
376 /* Reshape will very likely be beyond position, and it may
377 * be too late to stop at '+1', so aim for '+2'
378 */
5509dc44
N
379 position = (position / sectors + 2) * sectors;
380 sync_max = position/rd1;
2eba8496 381 }
5509dc44
N
382 if (sysfs_get_ll(mdi, NULL, "sync_max", &old_sync_max) < 0)
383 old_sync_max = mdi->component_size;
384 /* Must not advance sync_max as that could confuse
385 * the reshape monitor */
386 if (sync_max < old_sync_max)
387 sysfs_set_num(mdi, NULL, "sync_max", sync_max);
2eba8496
N
388 sysfs_set_str(mdi, NULL, "sync_action", "idle");
389
390 /* That should have set things going again. Now we
3afaff93 391 * wait a little while (3 second max) for sync_completed
2eba8496 392 * to reach the target.
3afaff93
N
393 * The reshape process can block for 500msec if
394 * the sync speed limit is hit, so we need to wait
395 * a lot longer than that. 1 second is usually
396 * enough. 3 is safe.
2eba8496 397 */
3afaff93 398 delay = 3000;
2eba8496 399 scfd = sysfs_open(mdi->sys_name, NULL, "sync_completed");
3afaff93 400 while (scfd >= 0 && delay > 0 && old_sync_max > 0) {
30ddba7d 401 unsigned long long max_completed;
5509dc44 402 sysfs_get_ll(mdi, NULL, "reshape_position", &curr);
2eba8496 403 sysfs_fd_get_str(scfd, buf, sizeof(buf));
3afaff93
N
404 if (strncmp(buf, "none", 4) == 0) {
405 /* Either reshape has aborted, or hasn't
406 * quite started yet. Wait a bit and
407 * check 'sync_action' to see.
408 */
239b3cc0 409 sleep_for(0, MSEC_TO_NSEC(10), true);
3afaff93
N
410 sysfs_get_str(mdi, NULL, "sync_action", buf, sizeof(buf));
411 if (strncmp(buf, "reshape", 7) != 0)
412 break;
413 }
5509dc44 414
30ddba7d
N
415 if (sysfs_fd_get_two(scfd, &completed,
416 &max_completed) == 2 &&
417 /* 'completed' sometimes reads as max-uulong */
418 completed < max_completed &&
5509dc44
N
419 (completed > sync_max ||
420 (completed == sync_max && curr != position))) {
421 while (completed > sync_max) {
422 sync_max += sectors / rd1;
423 if (backwards)
424 position -= sectors;
425 else
426 position += sectors;
427 }
428 if (sync_max < old_sync_max)
429 sysfs_set_num(mdi, NULL, "sync_max", sync_max);
430 }
431
2eba8496
N
432 if (!backwards && curr >= position)
433 break;
434 if (backwards && curr <= position)
435 break;
436 sysfs_wait(scfd, &delay);
437 }
438 if (scfd >= 0)
439 close(scfd);
440
441 }
e3e0d0a8 442done:
2eba8496 443
fe7e0e64
N
444 /* As we have an O_EXCL open, any use of the device
445 * which blocks STOP_ARRAY is probably a transient use,
446 * so it is reasonable to retry for a while - 5 seconds.
447 */
448 count = 25; err = 0;
fc54fe7a
JS
449 while (count && fd >= 0 &&
450 (err = ioctl(fd, STOP_ARRAY, NULL)) < 0 && errno == EBUSY) {
239b3cc0 451 sleep_for(0, MSEC_TO_NSEC(200), true);
fe7e0e64
N
452 count --;
453 }
454 if (fd >= 0 && err) {
455 if (verbose >= 0) {
456 pr_err("failed to stop array %s: %s\n",
457 devname, strerror(errno));
458 if (errno == EBUSY)
7a862a02 459 cont_err("Perhaps a running process, mounted filesystem or active volume group?\n");
4ccad7b1 460 }
fe7e0e64
N
461 rv = 1;
462 goto out;
463 }
229e66cb 464
fe7e0e64
N
465 if (devnm[0] && use_udev()) {
466 struct map_ent *mp = map_by_devnm(&map, devnm);
467 remove_devices(devnm, mp ? mp->path : NULL);
682c7051 468 }
fe7e0e64
N
469
470 if (verbose >= 0)
471 pr_err("stopped %s\n", devname);
472 map_lock(&map);
473 map_remove(&map, devnm);
474 map_unlock(&map);
475out:
6e8d27e7 476 sysfs_free(mdi);
fe7e0e64 477
bccd8153 478 return rv;
64c4757e
NB
479}
480
5e73b024
N
481static struct mddev_dev *add_one(struct mddev_dev *dv, char *name, char disp)
482{
483 struct mddev_dev *new;
484 new = xmalloc(sizeof(*new));
485 memset(new, 0, sizeof(*new));
486 new->devname = xstrdup(name);
487 new->disposition = disp;
488 new->next = dv->next;
489 dv->next = new;
490 return new;
491}
492
1d997643
N
493static void add_faulty(struct mddev_dev *dv, int fd, char disp)
494{
495 mdu_array_info_t array;
496 mdu_disk_info_t disk;
497 int remaining_disks;
498 int i;
499
9cd39f01 500 if (md_get_array_info(fd, &array) != 0)
1d997643
N
501 return;
502
503 remaining_disks = array.nr_disks;
504 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
1d997643
N
505 char buf[40];
506 disk.number = i;
d97572f5 507 if (md_get_disk_info(fd, &disk) != 0)
1d997643
N
508 continue;
509 if (disk.major == 0 && disk.minor == 0)
510 continue;
511 remaining_disks--;
512 if ((disk.state & 1) == 0) /* not faulty */
513 continue;
514 sprintf(buf, "%d:%d", disk.major, disk.minor);
5e73b024 515 dv = add_one(dv, buf, disp);
1d997643
N
516 }
517}
518
519static void add_detached(struct mddev_dev *dv, int fd, char disp)
520{
521 mdu_array_info_t array;
522 mdu_disk_info_t disk;
523 int remaining_disks;
524 int i;
525
9cd39f01 526 if (md_get_array_info(fd, &array) != 0)
1d997643
N
527 return;
528
529 remaining_disks = array.nr_disks;
530 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
1d997643
N
531 char buf[40];
532 int sfd;
533 disk.number = i;
d97572f5 534 if (md_get_disk_info(fd, &disk) != 0)
1d997643
N
535 continue;
536 if (disk.major == 0 && disk.minor == 0)
537 continue;
538 remaining_disks--;
539 if (disp == 'f' && (disk.state & 1) != 0) /* already faulty */
540 continue;
541 sprintf(buf, "%d:%d", disk.major, disk.minor);
542 sfd = dev_open(buf, O_RDONLY);
543 if (sfd >= 0) {
544 /* Not detached */
545 close(sfd);
546 continue;
547 }
548 if (errno != ENXIO)
549 /* Probably not detached */
550 continue;
5e73b024 551 dv = add_one(dv, buf, disp);
1d997643
N
552 }
553}
554
64a78416
N
555static void add_set(struct mddev_dev *dv, int fd, char set_char)
556{
557 mdu_array_info_t array;
558 mdu_disk_info_t disk;
559 int remaining_disks;
560 int copies, set;
561 int i;
562
9cd39f01 563 if (md_get_array_info(fd, &array) != 0)
64a78416
N
564 return;
565 if (array.level != 10)
566 return;
567 copies = ((array.layout & 0xff) *
568 ((array.layout >> 8) & 0xff));
569 if (array.raid_disks % copies)
570 return;
571
572 remaining_disks = array.nr_disks;
573 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
574 char buf[40];
575 disk.number = i;
d97572f5 576 if (md_get_disk_info(fd, &disk) != 0)
64a78416
N
577 continue;
578 if (disk.major == 0 && disk.minor == 0)
579 continue;
580 remaining_disks--;
581 set = disk.raid_disk % copies;
582 if (set_char != set + 'A')
583 continue;
584 sprintf(buf, "%d:%d", disk.major, disk.minor);
585 dv = add_one(dv, buf, dv->disposition);
586 }
587}
588
abe94694
N
589int attempt_re_add(int fd, int tfd, struct mddev_dev *dv,
590 struct supertype *dev_st, struct supertype *tst,
f2e8393b
MK
591 unsigned long rdev, enum update_opt update,
592 char *devname, int verbose, mdu_array_info_t *array)
abe94694
N
593{
594 struct mdinfo mdi;
595 int duuid[4];
596 int ouuid[4];
597
598 dev_st->ss->getinfo_super(dev_st, &mdi, NULL);
599 dev_st->ss->uuid_from_super(dev_st, ouuid);
600 if (tst->sb)
601 tst->ss->uuid_from_super(tst, duuid);
602 else
603 /* Assume uuid matches: kernel will check */
604 memcpy(duuid, ouuid, sizeof(ouuid));
605 if ((mdi.disk.state & (1<<MD_DISK_ACTIVE)) &&
606 !(mdi.disk.state & (1<<MD_DISK_FAULTY)) &&
607 memcmp(duuid, ouuid, sizeof(ouuid))==0) {
608 /* Looks like it is worth a
609 * try. Need to make sure
610 * kernel will accept it
611 * though.
612 */
613 mdu_disk_info_t disc;
abe94694 614 disc.number = mdi.disk.number;
d97572f5
JS
615 if (md_get_disk_info(fd, &disc) != 0 ||
616 disc.major != 0 || disc.minor != 0)
abe94694
N
617 goto skip_re_add;
618 disc.major = major(rdev);
619 disc.minor = minor(rdev);
620 disc.number = mdi.disk.number;
621 disc.raid_disk = mdi.disk.raid_disk;
622 disc.state = mdi.disk.state;
bff96f73
GJ
623 if (array->state & (1 << MD_SB_CLUSTERED)) {
624 /* extra flags are needed when adding to a cluster as
625 * there are two cases to distinguish
626 */
627 if (dv->disposition == 'c')
628 disc.state |= (1 << MD_DISK_CANDIDATE);
629 else
630 disc.state |= (1 << MD_DISK_CLUSTER_ADD);
631 }
e22fe3ae 632 if (dv->writemostly == FlagSet)
abe94694 633 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
e22fe3ae 634 if (dv->writemostly == FlagClear)
abe94694 635 disc.state &= ~(1 << MD_DISK_WRITEMOSTLY);
e22fe3ae 636 if (dv->failfast == FlagSet)
71574efb 637 disc.state |= 1 << MD_DISK_FAILFAST;
e22fe3ae 638 if (dv->failfast == FlagClear)
71574efb 639 disc.state &= ~(1 << MD_DISK_FAILFAST);
abe94694 640 remove_partitions(tfd);
d7be7d87
JS
641 if (update || dv->writemostly != FlagDefault ||
642 dv->failfast != FlagDefault) {
abe94694
N
643 int rv = -1;
644 tfd = dev_open(dv->devname, O_RDWR);
645 if (tfd < 0) {
7a862a02 646 pr_err("failed to open %s for superblock update during re-add\n", dv->devname);
abe94694
N
647 return -1;
648 }
649
e22fe3ae 650 if (dv->writemostly == FlagSet)
abe94694 651 rv = dev_st->ss->update_super(
03312b52 652 dev_st, NULL, UOPT_SPEC_WRITEMOSTLY,
abe94694 653 devname, verbose, 0, NULL);
e22fe3ae 654 if (dv->writemostly == FlagClear)
abe94694 655 rv = dev_st->ss->update_super(
03312b52 656 dev_st, NULL, UOPT_SPEC_READWRITE,
abe94694 657 devname, verbose, 0, NULL);
e22fe3ae 658 if (dv->failfast == FlagSet)
71574efb 659 rv = dev_st->ss->update_super(
03312b52 660 dev_st, NULL, UOPT_SPEC_FAILFAST,
71574efb 661 devname, verbose, 0, NULL);
e22fe3ae 662 if (dv->failfast == FlagClear)
71574efb 663 rv = dev_st->ss->update_super(
03312b52 664 dev_st, NULL, UOPT_SPEC_NOFAILFAST,
71574efb 665 devname, verbose, 0, NULL);
abe94694
N
666 if (update)
667 rv = dev_st->ss->update_super(
f2e8393b 668 dev_st, NULL, update,
abe94694
N
669 devname, verbose, 0, NULL);
670 if (rv == 0)
671 rv = dev_st->ss->store_super(dev_st, tfd);
672 close(tfd);
673 if (rv != 0) {
7a862a02 674 pr_err("failed to update superblock during re-add\n");
abe94694
N
675 return -1;
676 }
677 }
678 /* don't even try if disk is marked as faulty */
679 errno = 0;
680 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
681 if (verbose >= 0)
682 pr_err("re-added %s\n", dv->devname);
683 return 1;
684 }
685 if (errno == ENOMEM || errno == EROFS) {
686 pr_err("add new device failed for %s: %s\n",
687 dv->devname, strerror(errno));
688 if (dv->disposition == 'M')
689 return 0;
690 return -1;
691 }
692 }
693skip_re_add:
694 return 0;
695}
696
38aeaf3a
N
697int Manage_add(int fd, int tfd, struct mddev_dev *dv,
698 struct supertype *tst, mdu_array_info_t *array,
699 int force, int verbose, char *devname,
f2e8393b
MK
700 enum update_opt update, unsigned long rdev,
701 unsigned long long array_size, int raid_slot)
38aeaf3a
N
702{
703 unsigned long long ldsize;
fbd3e15c 704 struct supertype *dev_st;
38aeaf3a
N
705 int j;
706 mdu_disk_info_t disc;
ed2c2cb3 707 struct map_ent *map = NULL;
38aeaf3a
N
708
709 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
710 if (dv->disposition == 'M')
711 return 0;
712 else
713 return -1;
714 }
715
632dc30c 716 if (tst->ss == &super0 && ldsize > 4ULL*1024*1024*1024*1024) {
7ccc4cc4 717 /* More than 4TB is wasted on v0.90 */
38aeaf3a 718 if (!force) {
7a862a02
N
719 pr_err("%s is larger than %s can effectively use.\n"
720 " Add --force is you really want to add this device.\n",
38aeaf3a
N
721 dv->devname, devname);
722 return -1;
723 }
7a862a02
N
724 pr_err("%s is larger than %s can effectively use.\n"
725 " Adding anyway as --force was given.\n",
38aeaf3a
N
726 dv->devname, devname);
727 }
38aeaf3a
N
728
729 if (array->not_persistent == 0 || tst->ss->external) {
730
731 /* need to find a sample superblock to copy, and
732 * a spare slot to use.
733 * For 'external' array (well, container based),
734 * We can just load the metadata for the array->
735 */
736 int array_failed;
737 if (tst->sb)
738 /* already loaded */;
739 else if (tst->ss->external) {
740 tst->ss->load_container(tst, fd, NULL);
741 } else for (j = 0; j < tst->max_devs; j++) {
742 char *dev;
743 int dfd;
744 disc.number = j;
d97572f5 745 if (md_get_disk_info(fd, &disc))
38aeaf3a
N
746 continue;
747 if (disc.major==0 && disc.minor==0)
748 continue;
749 if ((disc.state & 4)==0) /* sync */
750 continue;
751 /* Looks like a good device to try */
752 dev = map_dev(disc.major, disc.minor, 1);
753 if (!dev)
754 continue;
755 dfd = dev_open(dev, O_RDONLY);
756 if (dfd < 0)
757 continue;
758 if (tst->ss->load_super(tst, dfd,
759 NULL)) {
760 close(dfd);
761 continue;
762 }
763 close(dfd);
764 break;
765 }
766 /* FIXME this is a bad test to be using */
fc54fe7a
JS
767 if (!tst->sb && (dv->disposition != 'a' &&
768 dv->disposition != 'S')) {
38aeaf3a
N
769 /* we are re-adding a device to a
770 * completely dead array - have to depend
771 * on kernel to check
772 */
773 } else if (!tst->sb) {
774 pr_err("cannot load array metadata from %s\n", devname);
775 return -1;
776 }
777
778 /* Make sure device is large enough */
01290056
SL
779 if (dv->disposition != 'j' && /* skip size check for Journal */
780 tst->sb &&
2609f339 781 tst->ss->avail_size(tst, ldsize/512, INVALID_SECTORS) <
38aeaf3a
N
782 array_size) {
783 if (dv->disposition == 'M')
784 return 0;
785 pr_err("%s not large enough to join array\n",
786 dv->devname);
787 return -1;
788 }
789
790 /* Possibly this device was recently part of
791 * the array and was temporarily removed, and
792 * is now being re-added. If so, we can
793 * simply re-add it.
794 */
795
fbd3e15c 796 if (array->not_persistent == 0) {
38aeaf3a
N
797 dev_st = dup_super(tst);
798 dev_st->ss->load_super(dev_st, tfd, NULL);
fbd3e15c
JS
799 if (dev_st->sb && dv->disposition != 'S') {
800 int rv;
801
802 rv = attempt_re_add(fd, tfd, dv, dev_st, tst,
803 rdev, update, devname,
804 verbose, array);
805 dev_st->ss->free_super(dev_st);
f6feb3fb
GM
806 if (rv) {
807 free(dev_st);
fbd3e15c 808 return rv;
f6feb3fb
GM
809 }
810 }
811 if (dev_st) {
812 dev_st->ss->free_super(dev_st);
813 free(dev_st);
fbd3e15c 814 }
38aeaf3a
N
815 }
816 if (dv->disposition == 'M') {
817 if (verbose > 0)
818 pr_err("--re-add for %s to %s is not possible\n",
819 dv->devname, devname);
820 return 0;
821 }
822 if (dv->disposition == 'A') {
823 pr_err("--re-add for %s to %s is not possible\n",
824 dv->devname, devname);
825 return -1;
826 }
827 if (array->active_disks < array->raid_disks) {
828 char *avail = xcalloc(array->raid_disks, 1);
829 int d;
830 int found = 0;
831
d180d2aa 832 for (d = 0; d < MAX_DISKS && found < array->nr_disks; d++) {
38aeaf3a 833 disc.number = d;
d97572f5 834 if (md_get_disk_info(fd, &disc))
38aeaf3a
N
835 continue;
836 if (disc.major == 0 && disc.minor == 0)
837 continue;
838 if (!(disc.state & (1<<MD_DISK_SYNC)))
839 continue;
840 avail[disc.raid_disk] = 1;
d31d0f52 841 found++;
38aeaf3a
N
842 }
843 array_failed = !enough(array->level, array->raid_disks,
844 array->layout, 1, avail);
6157951f 845 free(avail);
38aeaf3a
N
846 } else
847 array_failed = 0;
848 if (array_failed) {
849 pr_err("%s has failed so using --add cannot work and might destroy\n",
850 devname);
851 pr_err("data on %s. You should stop the array and re-assemble it.\n",
852 dv->devname);
853 return -1;
854 }
855 } else {
856 /* non-persistent. Must ensure that new drive
857 * is at least array->size big.
858 */
859 if (ldsize/512 < array_size) {
860 pr_err("%s not large enough to join array\n",
861 dv->devname);
862 return -1;
863 }
864 }
865 /* committed to really trying this device now*/
866 remove_partitions(tfd);
867
868 /* in 2.6.17 and earlier, version-1 superblocks won't
869 * use the number we write, but will choose a free number.
870 * we must choose the same free number, which requires
871 * starting at 'raid_disks' and counting up
872 */
873 for (j = array->raid_disks; j < tst->max_devs; j++) {
874 disc.number = j;
d97572f5 875 if (md_get_disk_info(fd, &disc))
38aeaf3a
N
876 break;
877 if (disc.major==0 && disc.minor==0)
878 break;
879 if (disc.state & 8) /* removed */
880 break;
881 }
882 disc.major = major(rdev);
883 disc.minor = minor(rdev);
4de90913
GJ
884 if (raid_slot < 0)
885 disc.number = j;
886 else
887 disc.number = raid_slot;
38aeaf3a 888 disc.state = 0;
01290056
SL
889
890 /* only add journal to array that supports journaling */
891 if (dv->disposition == 'j') {
01290056
SL
892 struct mdinfo *mdp;
893
894 mdp = sysfs_read(fd, NULL, GET_ARRAY_STATE);
2a1990c0
JS
895 if (!mdp) {
896 pr_err("%s unable to read array state.\n", devname);
897 return -1;
898 }
01290056 899
5e4ca8bb 900 if (mdp->array_state != ARRAY_READONLY) {
d209181d 901 sysfs_free(mdp);
01290056
SL
902 pr_err("%s is not readonly, cannot add journal.\n", devname);
903 return -1;
904 }
905
d209181d
JS
906 sysfs_free(mdp);
907
38c2e05b 908 disc.raid_disk = 0;
01290056
SL
909 }
910
ed2c2cb3
LXK
911 if (map_lock(&map))
912 pr_err("failed to get exclusive lock on mapfile when add disk\n");
913
38aeaf3a
N
914 if (array->not_persistent==0) {
915 int dfd;
01290056
SL
916 if (dv->disposition == 'j')
917 disc.state |= (1 << MD_DISK_JOURNAL) | (1 << MD_DISK_SYNC);
e22fe3ae 918 if (dv->writemostly == FlagSet)
38aeaf3a 919 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
e22fe3ae 920 if (dv->failfast == FlagSet)
71574efb 921 disc.state |= 1 << MD_DISK_FAILFAST;
38aeaf3a
N
922 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
923 if (tst->ss->add_to_super(tst, &disc, dfd,
72ca9bcf 924 dv->devname, INVALID_SECTORS))
ed2c2cb3 925 goto unlock;
38aeaf3a 926 if (tst->ss->write_init_super(tst))
ed2c2cb3 927 goto unlock;
38aeaf3a
N
928 } else if (dv->disposition == 'A') {
929 /* this had better be raid1.
930 * As we are "--re-add"ing we must find a spare slot
931 * to fill.
932 */
933 char *used = xcalloc(array->raid_disks, 1);
934 for (j = 0; j < tst->max_devs; j++) {
935 mdu_disk_info_t disc2;
936 disc2.number = j;
d97572f5 937 if (md_get_disk_info(fd, &disc2))
38aeaf3a
N
938 continue;
939 if (disc2.major==0 && disc2.minor==0)
940 continue;
941 if (disc2.state & 8) /* removed */
942 continue;
943 if (disc2.raid_disk < 0)
944 continue;
945 if (disc2.raid_disk > array->raid_disks)
946 continue;
947 used[disc2.raid_disk] = 1;
948 }
949 for (j = 0 ; j < array->raid_disks; j++)
950 if (!used[j]) {
951 disc.raid_disk = j;
952 disc.state |= (1<<MD_DISK_SYNC);
953 break;
954 }
955 free(used);
956 }
4de90913
GJ
957
958 if (array->state & (1 << MD_SB_CLUSTERED)) {
959 if (dv->disposition == 'c')
960 disc.state |= (1 << MD_DISK_CANDIDATE);
961 else
962 disc.state |= (1 << MD_DISK_CLUSTER_ADD);
963 }
964
e22fe3ae 965 if (dv->writemostly == FlagSet)
38aeaf3a 966 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
e22fe3ae 967 if (dv->failfast == FlagSet)
71574efb 968 disc.state |= (1 << MD_DISK_FAILFAST);
38aeaf3a
N
969 if (tst->ss->external) {
970 /* add a disk
971 * to an external metadata container */
972 struct mdinfo new_mdi;
973 struct mdinfo *sra;
974 int container_fd;
4dd2df09 975 char devnm[32];
38aeaf3a
N
976 int dfd;
977
4dd2df09
N
978 strcpy(devnm, fd2devnm(fd));
979
980 container_fd = open_dev_excl(devnm);
38aeaf3a 981 if (container_fd < 0) {
7a862a02 982 pr_err("add failed for %s: could not get exclusive access to container\n",
38aeaf3a
N
983 dv->devname);
984 tst->ss->free_super(tst);
ed2c2cb3 985 goto unlock;
38aeaf3a
N
986 }
987
1f5d54a0
MT
988 /* Check if metadata handler is able to accept the drive */
989 if (!tst->ss->validate_geometry(tst, LEVEL_CONTAINER, 0, 1, NULL,
990 0, 0, dv->devname, NULL, 0, 1)) {
991 close(container_fd);
ed2c2cb3 992 goto unlock;
1f5d54a0
MT
993 }
994
9cf9a1de 995 Kill(dv->devname, NULL, 0, -1, 0);
38aeaf3a 996 dfd = dev_open(dv->devname, O_RDWR | O_EXCL|O_DIRECT);
38aeaf3a 997 if (tst->ss->add_to_super(tst, &disc, dfd,
72ca9bcf 998 dv->devname, INVALID_SECTORS)) {
38aeaf3a
N
999 close(dfd);
1000 close(container_fd);
ed2c2cb3 1001 goto unlock;
38aeaf3a 1002 }
12724c01 1003 if (!mdmon_running(tst->container_devnm))
38aeaf3a
N
1004 tst->ss->sync_metadata(tst);
1005
4dd2df09 1006 sra = sysfs_read(container_fd, NULL, 0);
38aeaf3a
N
1007 if (!sra) {
1008 pr_err("add failed for %s: sysfs_read failed\n",
1009 dv->devname);
1010 close(container_fd);
1011 tst->ss->free_super(tst);
ed2c2cb3 1012 goto unlock;
38aeaf3a
N
1013 }
1014 sra->array.level = LEVEL_CONTAINER;
1015 /* Need to set data_offset and component_size */
1016 tst->ss->getinfo_super(tst, &new_mdi, NULL);
1017 new_mdi.disk.major = disc.major;
1018 new_mdi.disk.minor = disc.minor;
1019 new_mdi.recovery_start = 0;
1020 /* Make sure fds are closed as they are O_EXCL which
1021 * would block add_disk */
1022 tst->ss->free_super(tst);
1023 if (sysfs_add_disk(sra, &new_mdi, 0) != 0) {
7a862a02 1024 pr_err("add new device to external metadata failed for %s\n", dv->devname);
38aeaf3a
N
1025 close(container_fd);
1026 sysfs_free(sra);
ed2c2cb3 1027 goto unlock;
38aeaf3a 1028 }
4dd2df09 1029 ping_monitor(devnm);
38aeaf3a
N
1030 sysfs_free(sra);
1031 close(container_fd);
1032 } else {
1033 tst->ss->free_super(tst);
1034 if (ioctl(fd, ADD_NEW_DISK, &disc)) {
01290056
SL
1035 if (dv->disposition == 'j')
1036 pr_err("Failed to hot add %s as journal, "
1037 "please try restart %s.\n", dv->devname, devname);
1038 else
1039 pr_err("add new device failed for %s as %d: %s\n",
1040 dv->devname, j, strerror(errno));
ed2c2cb3 1041 goto unlock;
38aeaf3a 1042 }
01290056
SL
1043 if (dv->disposition == 'j') {
1044 pr_err("Journal added successfully, making %s read-write\n", devname);
1045 if (Manage_ro(devname, fd, -1))
1046 pr_err("Failed to make %s read-write\n", devname);
1047 }
1048
38aeaf3a
N
1049 }
1050 if (verbose >= 0)
1051 pr_err("added %s\n", dv->devname);
ed2c2cb3 1052 map_unlock(&map);
38aeaf3a 1053 return 1;
ed2c2cb3
LXK
1054unlock:
1055 map_unlock(&map);
1056 return -1;
38aeaf3a
N
1057}
1058
d070235d 1059int Manage_remove(struct supertype *tst, int fd, struct mddev_dev *dv,
1ab9ed2a 1060 int sysfd, unsigned long rdev, int force, int verbose, char *devname)
d070235d
N
1061{
1062 int lfd = -1;
1063 int err;
1064
1065 if (tst->ss->external) {
1066 /* To remove a device from a container, we must
1067 * check that it isn't in use in an array.
1068 * This involves looking in the 'holders'
1069 * directory - there must be just one entry,
1070 * the container.
1071 * To ensure that it doesn't get used as a
1072 * hot spare while we are checking, we
1073 * get an O_EXCL open on the container
1074 */
aab15415 1075 int ret;
4dd2df09
N
1076 char devnm[32];
1077 strcpy(devnm, fd2devnm(fd));
1078 lfd = open_dev_excl(devnm);
d070235d 1079 if (lfd < 0) {
7a862a02 1080 pr_err("Cannot get exclusive access to container - odd\n");
d070235d
N
1081 return -1;
1082 }
aab15415
N
1083 /* We may not be able to check on holders in
1084 * sysfs, either because we don't have the dev num
1085 * (rdev == 0) or because the device has been detached
1086 * and the 'holders' directory no longer exists
1087 * (ret == -1). In that case, assume it is OK to
1088 * remove.
d070235d 1089 */
aab15415
N
1090 if (rdev == 0)
1091 ret = -1;
c922221e
TM
1092 else {
1093 /*
1094 * The drive has already been set to 'faulty', however
1095 * monitor might not have had time to process it and the
1096 * drive might still have an entry in the 'holders'
1097 * directory. Try a few times to avoid a false error
1098 */
1099 int count = 20;
1100
1101 do {
1102 ret = sysfs_unique_holder(devnm, rdev);
1103 if (ret < 2)
1104 break;
239b3cc0 1105 sleep_for(0, MSEC_TO_NSEC(100), true);
c922221e
TM
1106 } while (--count > 0);
1107
1108 if (ret == 0) {
1109 pr_err("%s is not a member, cannot remove.\n",
1110 dv->devname);
1111 close(lfd);
1112 return -1;
1113 }
1114 if (ret >= 2) {
1115 pr_err("%s is still in use, cannot remove.\n",
1116 dv->devname);
1117 close(lfd);
1118 return -1;
1119 }
d070235d
N
1120 }
1121 }
1122 /* FIXME check that it is a current member */
1123 if (sysfd >= 0) {
1124 /* device has been removed and we don't know
1125 * the major:minor number
1126 */
1ab9ed2a 1127 err = sys_hot_remove_disk(sysfd, force);
d070235d 1128 } else {
1ab9ed2a 1129 err = hot_remove_disk(fd, rdev, force);
d070235d
N
1130 if (err && errno == ENODEV) {
1131 /* Old kernels rejected this if no personality
1132 * is registered */
4dd2df09 1133 struct mdinfo *sra = sysfs_read(fd, NULL, GET_DEVS);
d070235d
N
1134 struct mdinfo *dv = NULL;
1135 if (sra)
1136 dv = sra->devs;
1137 for ( ; dv ; dv=dv->next)
1138 if (dv->disk.major == (int)major(rdev) &&
1139 dv->disk.minor == (int)minor(rdev))
1140 break;
1141 if (dv)
1142 err = sysfs_set_str(sra, dv,
1143 "state", "remove");
1144 else
1145 err = -1;
6e8d27e7 1146 sysfs_free(sra);
d070235d
N
1147 }
1148 }
1149 if (err) {
7a862a02 1150 pr_err("hot remove failed for %s: %s\n", dv->devname,
d070235d
N
1151 strerror(errno));
1152 if (lfd >= 0)
1153 close(lfd);
1154 return -1;
1155 }
1156 if (tst->ss->external) {
1157 /*
1158 * Before dropping our exclusive open we make an
1159 * attempt at preventing mdmon from seeing an
1160 * 'add' event before reconciling this 'remove'
1161 * event.
1162 */
4dd2df09 1163 char *devnm = fd2devnm(fd);
d070235d 1164
4dd2df09 1165 if (!devnm) {
d070235d
N
1166 pr_err("unable to get container name\n");
1167 return -1;
1168 }
1169
4dd2df09 1170 ping_manager(devnm);
d070235d
N
1171 }
1172 if (lfd >= 0)
1173 close(lfd);
1174 if (verbose >= 0)
1175 pr_err("hot removed %s from %s\n",
1176 dv->devname, devname);
1177 return 1;
1178}
1179
70c55e36
N
1180int Manage_replace(struct supertype *tst, int fd, struct mddev_dev *dv,
1181 unsigned long rdev, int verbose, char *devname)
1182{
1183 struct mdinfo *mdi, *di;
1184 if (tst->ss->external) {
1185 pr_err("--replace only supported for native metadata (0.90 or 1.x)\n");
1186 return -1;
1187 }
1188 /* Need to find the device in sysfs and add 'want_replacement' to the
1189 * status.
1190 */
4dd2df09 1191 mdi = sysfs_read(fd, NULL, GET_DEVS);
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.raid_disk < 0) {
1204 pr_err("%s is not active and so cannot be replaced.\n",
1205 dv->devname);
1206 sysfs_free(mdi);
1207 return -1;
1208 }
1209 rv = sysfs_set_str(mdi, di,
1210 "state", "want_replacement");
1211 if (rv) {
1212 sysfs_free(mdi);
1213 pr_err("Failed to request replacement for %s\n",
1214 dv->devname);
1215 return -1;
1216 }
1217 if (verbose >= 0)
1218 pr_err("Marked %s (device %d in %s) for replacement\n",
1219 dv->devname, di->disk.raid_disk, devname);
1220 /* If there is a matching 'with', we need to tell it which
1221 * raid disk
1222 */
1223 while (dv && dv->disposition != 'W')
1224 dv = dv->next;
1225 if (dv) {
1226 dv->disposition = 'w';
1227 dv->used = di->disk.raid_disk;
1228 }
1229 return 1;
1230 }
1231 sysfs_free(mdi);
1232 pr_err("%s not found in %s so cannot --replace it\n",
1233 dv->devname, devname);
1234 return -1;
1235}
1236
1237int Manage_with(struct supertype *tst, int fd, struct mddev_dev *dv,
1238 unsigned long rdev, int verbose, char *devname)
1239{
1240 struct mdinfo *mdi, *di;
1241 /* try to set 'slot' for 'rdev' in 'fd' to 'dv->used' */
4dd2df09 1242 mdi = sysfs_read(fd, NULL, GET_DEVS|GET_STATE);
70c55e36
N
1243 if (!mdi || !mdi->devs) {
1244 pr_err("Cannot find status of %s to enable replacement - strange\n",
1245 devname);
1246 return -1;
1247 }
1248 for (di = mdi->devs; di; di = di->next)
1249 if (di->disk.major == (int)major(rdev) &&
1250 di->disk.minor == (int)minor(rdev))
1251 break;
1252 if (di) {
1253 int rv;
1254 if (di->disk.state & (1<<MD_DISK_FAULTY)) {
1255 pr_err("%s is faulty and cannot be a replacement\n",
1256 dv->devname);
1257 sysfs_free(mdi);
1258 return -1;
1259 }
1260 if (di->disk.raid_disk >= 0) {
1261 pr_err("%s is active and cannot be a replacement\n",
1262 dv->devname);
1263 sysfs_free(mdi);
1264 return -1;
1265 }
1266 rv = sysfs_set_num(mdi, di,
1267 "slot", dv->used);
1268 if (rv) {
1269 sysfs_free(mdi);
51425978 1270 pr_err("Failed to set %s as preferred replacement.\n",
70c55e36
N
1271 dv->devname);
1272 return -1;
1273 }
1274 if (verbose >= 0)
1275 pr_err("Marked %s in %s as replacement for device %d\n",
1276 dv->devname, devname, dv->used);
1277 return 1;
1278 }
1279 sysfs_free(mdi);
1280 pr_err("%s not found in %s so cannot make it preferred replacement\n",
1281 dv->devname, devname);
1282 return -1;
1283}
1284
fc6fd406
MK
1285/**
1286 * is_remove_safe() - Check if remove is safe.
1287 * @array: Array info.
1288 * @fd: Array file descriptor.
1289 * @devname: Name of device to remove.
1290 * @verbose: Verbose.
1291 *
1292 * The function determines if array will be operational
1293 * after removing &devname.
1294 *
1295 * Return: True if array will be operational, false otherwise.
1296 */
1297bool is_remove_safe(mdu_array_info_t *array, const int fd, char *devname, const int verbose)
1298{
1299 dev_t devid = devnm2devid(devname + 5);
1300 struct mdinfo *mdi = sysfs_read(fd, NULL, GET_DEVS | GET_DISKS | GET_STATE);
1301
1302 if (!mdi) {
1303 if (verbose)
1304 pr_err("Failed to read sysfs attributes for %s\n", devname);
1305 return false;
1306 }
1307
1308 char *avail = xcalloc(array->raid_disks, sizeof(char));
1309
1310 for (mdi = mdi->devs; mdi; mdi = mdi->next) {
1311 if (mdi->disk.raid_disk < 0)
1312 continue;
1313 if (!(mdi->disk.state & (1 << MD_DISK_SYNC)))
1314 continue;
1315 if (makedev(mdi->disk.major, mdi->disk.minor) == devid)
1316 continue;
1317 avail[mdi->disk.raid_disk] = 1;
1318 }
1319 sysfs_free(mdi);
1320
1321 bool is_enough = enough(array->level, array->raid_disks,
b3e7b7eb 1322 array->layout, 1, avail);
fc6fd406
MK
1323
1324 free(avail);
1325 return is_enough;
1326}
1327
36a70782
KT
1328/**
1329 * Manage_subdevs() - Execute operation depending on devmode.
1330 *
1331 * @devname: name of the device.
1332 * @fd: file descriptor.
1333 * @devlist: list of sub-devices to manage.
1334 * @verbose: verbose level.
1335 * @test: test flag.
1336 * @update: type of update.
1337 * @force: force flag.
1338 *
1339 * This function executes operation defined by devmode
1340 * for each dev from devlist.
1341 * Devmode can be:
1342 * 'a' - add the device
1343 * 'S' - add the device as a spare - don't try re-add
1344 * 'j' - add the device as a journal device
1345 * 'A' - re-add the device
1346 * 'r' - remove the device: HOT_REMOVE_DISK
1347 * device can be 'faulty' or 'detached' in which case all
1348 * matching devices are removed.
1349 * 'f' - set the device faulty SET_DISK_FAULTY
1350 * device can be 'detached' in which case any device that
1351 * is inaccessible will be marked faulty.
1352 * 'I' - remove device by using incremental fail
1353 * which is executed when device is removed surprisingly.
1354 * 'R' - mark this device as wanting replacement.
1355 * 'W' - this device is added if necessary and activated as
1356 * a replacement for a previous 'R' device.
1357 * -----
1358 * 'w' - 'W' will be changed to 'w' when it is paired with
1359 * a 'R' device. If a 'W' is found while walking the list
1360 * it must be unpaired, and is an error.
1361 * 'M' - this is created by a 'missing' target. It is a slight
1362 * variant on 'A'
1363 * 'F' - Another variant of 'A', where the device was faulty
1364 * so must be removed from the array first.
1365 * 'c' - confirm the device as found (for clustered environments)
1366 *
1367 * For 'f' and 'r', the device can also be a kernel-internal
1368 * name such as 'sdb'.
1369 *
1370 * Return: 0 on success, otherwise 1 or 2.
1371 */
64c4757e 1372int Manage_subdevs(char *devname, int fd,
833bb0f8 1373 struct mddev_dev *devlist, int verbose, int test,
f2e8393b 1374 enum update_opt update, int force)
cd29a5c8 1375{
682c7051 1376 mdu_array_info_t array;
7a3be72f 1377 unsigned long long array_size;
1d997643 1378 struct mddev_dev *dv;
cfad27a9 1379 int tfd = -1;
f6feb3fb 1380 struct supertype *tst = NULL;
4725bc31 1381 char *subarray = NULL;
98d27e39 1382 int sysfd = -1;
7d2e6486 1383 int count = 0; /* number of actions taken */
9f584691 1384 struct mdinfo info;
9465f170 1385 struct mdinfo devinfo;
9f584691 1386 int frozen = 0;
8af530b0 1387 int busy = 0;
4de90913 1388 int raid_slot = -1;
682c7051 1389
dae13137
JS
1390 if (sysfs_init(&info, fd, NULL)) {
1391 pr_err("sysfs not availabile for %s\n", devname);
1392 goto abort;
1393 }
1394
9cd39f01
JS
1395 if (md_get_array_info(fd, &array)) {
1396 pr_err("Cannot get array info for %s\n", devname);
bcbb3112 1397 goto abort;
682c7051 1398 }
7bd04da9 1399 /* array.size is only 32 bits and may be truncated.
7a3be72f
NB
1400 * So read from sysfs if possible, and record number of sectors
1401 */
1402
1403 array_size = get_component_size(fd);
1404 if (array_size <= 0)
1405 array_size = array.size * 2;
1406
4725bc31 1407 tst = super_by_fd(fd, &subarray);
3da92f27 1408 if (!tst) {
e7b84f9d 1409 pr_err("unsupport array - version %d.%d\n",
3da92f27 1410 array.major_version, array.minor_version);
bcbb3112 1411 goto abort;
3da92f27
NB
1412 }
1413
38aeaf3a 1414 for (dv = devlist; dv; dv = dv->next) {
ffaf1a7e 1415 dev_t rdev = 0; /* device to add/remove etc */
38aeaf3a 1416 int rv;
b47024f1 1417 int mj,mn;
4a39c6f2 1418
4de90913
GJ
1419 raid_slot = -1;
1420 if (dv->disposition == 'c') {
1421 rv = parse_cluster_confirm_arg(dv->devname,
1422 &dv->devname,
1423 &raid_slot);
d7a49369 1424 if (rv) {
4de90913
GJ
1425 pr_err("Could not get the devname of cluster\n");
1426 goto abort;
1427 }
1428 }
1429
1d997643
N
1430 if (strcmp(dv->devname, "failed") == 0 ||
1431 strcmp(dv->devname, "faulty") == 0) {
fc54fe7a 1432 if (dv->disposition != 'A' && dv->disposition != 'r') {
7a862a02 1433 pr_err("%s only meaningful with -r or --re-add, not -%c\n",
b80da661 1434 dv->devname, dv->disposition);
bcbb3112 1435 goto abort;
b80da661 1436 }
262e3b7f
N
1437 add_faulty(dv, fd, (dv->disposition == 'A'
1438 ? 'F' : 'r'));
1d997643
N
1439 continue;
1440 }
1441 if (strcmp(dv->devname, "detached") == 0) {
b80da661 1442 if (dv->disposition != 'r' && dv->disposition != 'f') {
7a862a02 1443 pr_err("%s only meaningful with -r of -f, not -%c\n",
b80da661 1444 dv->devname, dv->disposition);
bcbb3112 1445 goto abort;
b80da661 1446 }
1d997643
N
1447 add_detached(dv, fd, dv->disposition);
1448 continue;
1449 }
1450
1451 if (strcmp(dv->devname, "missing") == 0) {
e9ddbb2b 1452 struct mddev_dev *add_devlist;
1d997643 1453 struct mddev_dev **dp;
4de90913
GJ
1454 if (dv->disposition == 'c') {
1455 rv = ioctl(fd, CLUSTERED_DISK_NACK, NULL);
1456 break;
1457 }
1458
c8e1a230 1459 if (dv->disposition != 'A') {
7a862a02 1460 pr_err("'missing' only meaningful with --re-add\n");
bcbb3112 1461 goto abort;
a4e13010 1462 }
1d997643 1463 add_devlist = conf_get_devs();
a4e13010 1464 if (add_devlist == NULL) {
8e5b52cd 1465 pr_err("no devices to scan for missing members.\n");
a4e13010
N
1466 continue;
1467 }
1d997643 1468 for (dp = &add_devlist; *dp; dp = & (*dp)->next)
7bd04da9 1469 /* 'M' (for 'missing') is like 'A' without errors */
1d997643
N
1470 (*dp)->disposition = 'M';
1471 *dp = dv->next;
1472 dv->next = add_devlist;
1473 continue;
1474 }
1475
64a78416
N
1476 if (strncmp(dv->devname, "set-", 4) == 0 &&
1477 strlen(dv->devname) == 5) {
1478 int copies;
1479
1480 if (dv->disposition != 'r' &&
1481 dv->disposition != 'f') {
1482 pr_err("'%s' only meaningful with -r or -f\n",
1483 dv->devname);
1484 goto abort;
1485 }
1486 if (array.level != 10) {
1487 pr_err("'%s' only meaningful with RAID10 arrays\n",
1488 dv->devname);
1489 goto abort;
1490 }
1491 copies = ((array.layout & 0xff) *
1492 ((array.layout >> 8) & 0xff));
1493 if (array.raid_disks % copies != 0 ||
1494 dv->devname[4] < 'A' ||
1495 dv->devname[4] >= 'A' + copies ||
1496 copies > 26) {
1497 pr_err("'%s' not meaningful with this array\n",
1498 dv->devname);
1499 goto abort;
1500 }
1501 add_set(dv, fd, dv->devname[4]);
1502 continue;
1503 }
1504
1d997643 1505 if (strchr(dv->devname, '/') == NULL &&
aab15415
N
1506 strchr(dv->devname, ':') == NULL &&
1507 strlen(dv->devname) < 50) {
98d27e39
N
1508 /* Assume this is a kernel-internal name like 'sda1' */
1509 int found = 0;
1510 char dname[55];
461fae7e
KT
1511 if (dv->disposition != 'r' && dv->disposition != 'f' &&
1512 dv->disposition != 'I') {
1513 pr_err("%s only meaningful with -r, -f or -I, not -%c\n",
98d27e39 1514 dv->devname, dv->disposition);
bcbb3112 1515 goto abort;
98d27e39
N
1516 }
1517
1518 sprintf(dname, "dev-%s", dv->devname);
4dd2df09 1519 sysfd = sysfs_open(fd2devnm(fd), dname, "block/dev");
98d27e39
N
1520 if (sysfd >= 0) {
1521 char dn[20];
98d27e39
N
1522 if (sysfs_fd_get_str(sysfd, dn, 20) > 0 &&
1523 sscanf(dn, "%d:%d", &mj,&mn) == 2) {
5dffd09d 1524 rdev = makedev(mj,mn);
98d27e39
N
1525 found = 1;
1526 }
1527 close(sysfd);
1528 sysfd = -1;
1529 }
1530 if (!found) {
4dd2df09 1531 sysfd = sysfs_open(fd2devnm(fd), dname, "state");
98d27e39 1532 if (sysfd < 0) {
7a862a02 1533 pr_err("%s does not appear to be a component of %s\n",
98d27e39 1534 dv->devname, devname);
bcbb3112 1535 goto abort;
98d27e39
N
1536 }
1537 }
fc54fe7a
JS
1538 } else if ((dv->disposition == 'r' ||
1539 dv->disposition == 'f') &&
1540 get_maj_min(dv->devname, &mj, &mn)) {
b47024f1
N
1541 /* for 'fail' and 'remove', the device might
1542 * not exist.
1543 */
1544 rdev = makedev(mj, mn);
b80da661 1545 } else {
c7b47447 1546 tfd = dev_open(dv->devname, O_RDONLY);
cc5083d1 1547 if (tfd >= 0) {
0a6bff09 1548 fstat_is_blkdev(tfd, dv->devname, &rdev);
cc5083d1
JS
1549 close(tfd);
1550 } else {
5fe7f5f7 1551 int open_err = errno;
9e04ac1c 1552 if (!stat_is_blkdev(dv->devname, &rdev)) {
5fe7f5f7
N
1553 if (dv->disposition == 'M')
1554 /* non-fatal. Also improbable */
1555 continue;
5fe7f5f7
N
1556 goto abort;
1557 }
1558 if (dv->disposition == 'r')
1559 /* Be happy, the stat worked, that is
1560 * enough for --remove
1561 */
1562 ;
1563 else {
1d997643
N
1564 if (dv->disposition == 'M')
1565 /* non-fatal */
1566 continue;
839f27a3 1567 pr_err("Cannot open %s: %s\n",
5fe7f5f7 1568 dv->devname, strerror(open_err));
bcbb3112 1569 goto abort;
5a9de8db 1570 }
b80da661 1571 }
682c7051 1572 }
cd29a5c8 1573 switch(dv->disposition){
682c7051 1574 default:
e7b84f9d 1575 pr_err("internal error - devmode[%s]=%d\n",
c913b90e 1576 dv->devname, dv->disposition);
bcbb3112 1577 goto abort;
682c7051 1578 case 'a':
f33a71f1 1579 case 'S': /* --add-spare */
01290056 1580 case 'j': /* --add-journal */
c8e1a230 1581 case 'A':
262e3b7f
N
1582 case 'M': /* --re-add missing */
1583 case 'F': /* --re-add faulty */
4de90913 1584 case 'c': /* --cluster-confirm */
4a39c6f2 1585 /* add the device */
4725bc31 1586 if (subarray) {
7a862a02 1587 pr_err("Cannot add disks to a \'member\' array, perform this operation on the parent container\n");
bcbb3112 1588 goto abort;
f94d52f4 1589 }
9465f170
GJ
1590
1591 /* Let's first try to write re-add to sysfs */
1592 if (rdev != 0 &&
1593 (dv->disposition == 'A' || dv->disposition == 'F')) {
1594 sysfs_init_dev(&devinfo, rdev);
1595 if (sysfs_set_str(&info, &devinfo, "state", "re-add") == 0) {
1596 pr_err("re-add %s to %s succeed\n",
1597 dv->devname, info.sys_name);
1598 break;
1599 }
1600 }
1601
262e3b7f
N
1602 if (dv->disposition == 'F')
1603 /* Need to remove first */
1ab9ed2a 1604 hot_remove_disk(fd, rdev, force);
f277ce36 1605 /* Make sure it isn't in use (in 2.6 or later) */
1d997643 1606 tfd = dev_open(dv->devname, O_RDONLY|O_EXCL);
38aeaf3a
N
1607 if (tfd >= 0) {
1608 /* We know no-one else is using it. We'll
1609 * need non-exclusive access to add it, so
1610 * do that now.
1611 */
1612 close(tfd);
1613 tfd = dev_open(dv->devname, O_RDONLY);
1011e834 1614 }
0fbf459d 1615 if (tfd < 0) {
1d997643
N
1616 if (dv->disposition == 'M')
1617 continue;
e7b84f9d 1618 pr_err("Cannot open %s: %s\n",
d7eaf49f 1619 dv->devname, strerror(errno));
bcbb3112 1620 goto abort;
d7eaf49f 1621 }
9f584691
N
1622 if (!frozen) {
1623 if (sysfs_freeze_array(&info) == 1)
1624 frozen = 1;
1625 else
1626 frozen = -1;
1627 }
38aeaf3a
N
1628 rv = Manage_add(fd, tfd, dv, tst, &array,
1629 force, verbose, devname, update,
4de90913 1630 rdev, array_size, raid_slot);
38aeaf3a
N
1631 close(tfd);
1632 tfd = -1;
1633 if (rv < 0)
bcbb3112 1634 goto abort;
38aeaf3a
N
1635 if (rv > 0)
1636 count++;
682c7051
NB
1637 break;
1638
1639 case 'r':
1640 /* hot remove */
4725bc31 1641 if (subarray) {
7a862a02 1642 pr_err("Cannot remove disks from a \'member\' array, perform this operation on the parent container\n");
d070235d
N
1643 rv = -1;
1644 } else
1645 rv = Manage_remove(tst, fd, dv, sysfd,
1ab9ed2a 1646 rdev, verbose, force,
d070235d
N
1647 devname);
1648 if (sysfd >= 0)
98d27e39 1649 close(sysfd);
d070235d
N
1650 sysfd = -1;
1651 if (rv < 0)
bcbb3112 1652 goto abort;
d070235d
N
1653 if (rv > 0)
1654 count++;
682c7051
NB
1655 break;
1656
1657 case 'f': /* set faulty */
fc6fd406
MK
1658 if (!is_remove_safe(&array, fd, dv->devname, verbose)) {
1659 pr_err("Cannot remove %s from %s, array will be failed.\n",
1660 dv->devname, devname);
1661 if (sysfd >= 0)
1662 close(sysfd);
1663 goto abort;
1664 }
461fae7e 1665 case 'I': /* incremental fail */
98d27e39
N
1666 if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) ||
1667 (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY,
5dffd09d 1668 rdev))) {
8af530b0
N
1669 if (errno == EBUSY)
1670 busy = 1;
e7b84f9d 1671 pr_err("set device faulty failed for %s: %s\n",
1d997643 1672 dv->devname, strerror(errno));
98d27e39
N
1673 if (sysfd >= 0)
1674 close(sysfd);
bcbb3112 1675 goto abort;
682c7051 1676 }
98d27e39
N
1677 if (sysfd >= 0)
1678 close(sysfd);
1679 sysfd = -1;
7d2e6486 1680 count++;
dab6685f 1681 if (verbose >= 0)
e7b84f9d 1682 pr_err("set %s faulty in %s\n",
1d997643 1683 dv->devname, devname);
682c7051 1684 break;
70c55e36
N
1685 case 'R': /* Mark as replaceable */
1686 if (subarray) {
7a862a02 1687 pr_err("Cannot replace disks in a \'member\' array, perform this operation on the parent container\n");
70c55e36
N
1688 rv = -1;
1689 } else {
1690 if (!frozen) {
1691 if (sysfs_freeze_array(&info) == 1)
1692 frozen = 1;
1693 else
1694 frozen = -1;
1695 }
1696 rv = Manage_replace(tst, fd, dv,
5dffd09d 1697 rdev, verbose,
70c55e36
N
1698 devname);
1699 }
1700 if (rv < 0)
1701 goto abort;
1702 if (rv > 0)
1703 count++;
1704 break;
1705 case 'W': /* --with device that doesn't match */
1706 pr_err("No matching --replace device for --with %s\n",
1707 dv->devname);
1708 goto abort;
1709 case 'w': /* --with device which was matched */
1710 rv = Manage_with(tst, fd, dv,
5dffd09d 1711 rdev, verbose, devname);
70c55e36
N
1712 if (rv < 0)
1713 goto abort;
1714 break;
682c7051
NB
1715 }
1716 }
f6feb3fb 1717 free(tst);
9f584691
N
1718 if (frozen > 0)
1719 sysfs_set_str(&info, NULL, "sync_action","idle");
7d2e6486
N
1720 if (test && count == 0)
1721 return 2;
682c7051 1722 return 0;
bcbb3112
N
1723
1724abort:
f6feb3fb 1725 free(tst);
9f584691
N
1726 if (frozen > 0)
1727 sysfs_set_str(&info, NULL, "sync_action","idle");
8af530b0 1728 return !test && busy ? 2 : 1;
64c4757e 1729}
1f48664b
NB
1730
1731int autodetect(void)
1732{
1733 /* Open any md device, and issue the RAID_AUTORUN ioctl */
1734 int rv = 1;
1735 int fd = dev_open("9:0", O_RDONLY);
1736 if (fd >= 0) {
1737 if (ioctl(fd, RAID_AUTORUN, 0) == 0)
1738 rv = 0;
1739 close(fd);
1740 }
1741 return rv;
1742}
aa534678 1743
f2e8393b
MK
1744int Update_subarray(char *dev, char *subarray, enum update_opt update,
1745 struct mddev_ident *ident, int verbose)
aa534678
DW
1746{
1747 struct supertype supertype, *st = &supertype;
1748 int fd, rv = 2;
70f1ff42 1749 struct mdinfo *info = NULL;
f2e8393b 1750 char *update_verb = map_num(update_options, update);
aa534678
DW
1751
1752 memset(st, 0, sizeof(*st));
aa534678 1753
ba728be7 1754 fd = open_subarray(dev, subarray, st, verbose < 0);
aa534678
DW
1755 if (fd < 0)
1756 return 2;
1757
1758 if (!st->ss->update_subarray) {
ba728be7 1759 if (verbose >= 0)
e7b84f9d
N
1760 pr_err("Operation not supported for %s metadata\n",
1761 st->ss->name);
aa534678
DW
1762 goto free_super;
1763 }
1764
db10eab6
MK
1765 if (is_subarray_active(subarray, st->devnm)) {
1766 if (verbose >= 0)
1767 pr_err("Subarray %s in %s is active, cannot update %s\n",
f2e8393b 1768 subarray, dev, update_verb);
db10eab6
MK
1769 goto free_super;
1770 }
1771
4dd2df09 1772 if (mdmon_running(st->devnm))
aa534678
DW
1773 st->update_tail = &st->updates;
1774
70f1ff42
LF
1775 info = st->ss->container_content(st, subarray);
1776
f2e8393b 1777 if (update == UOPT_PPL && !is_level456(info->array.level)) {
70f1ff42
LF
1778 pr_err("RWH policy ppl is supported only for raid4, raid5 and raid6.\n");
1779 goto free_super;
1780 }
1781
f2e8393b 1782 rv = st->ss->update_subarray(st, subarray, update, ident);
aa534678
DW
1783
1784 if (rv) {
ba728be7 1785 if (verbose >= 0)
e7b84f9d 1786 pr_err("Failed to update %s of subarray-%s in %s\n",
f2e8393b 1787 update_verb, subarray, dev);
aa534678
DW
1788 } else if (st->update_tail)
1789 flush_metadata_updates(st);
1790 else
1791 st->ss->sync_metadata(st);
1792
f2e8393b 1793 if (rv == 0 && update == UOPT_NAME && verbose >= 0)
e7b84f9d
N
1794 pr_err("Updated subarray-%s name from %s, UUIDs may have changed\n",
1795 subarray, dev);
aa534678 1796
70f1ff42
LF
1797free_super:
1798 if (info)
1799 free(info);
aa534678
DW
1800 st->ss->free_super(st);
1801 close(fd);
1802
1803 return rv;
1804}
d52bb542 1805
7bd04da9
N
1806/* Move spare from one array to another If adding to destination array fails
1807 * add back to original array.
d52bb542
AC
1808 * Returns 1 on success, 0 on failure */
1809int move_spare(char *from_devname, char *to_devname, dev_t devid)
1810{
1811 struct mddev_dev devlist;
1812 char devname[20];
1813
1814 /* try to remove and add */
1815 int fd1 = open(to_devname, O_RDONLY);
1816 int fd2 = open(from_devname, O_RDONLY);
1817
1818 if (fd1 < 0 || fd2 < 0) {
9cf361f8
JS
1819 if (fd1 >= 0)
1820 close(fd1);
1821 if (fd2 >= 0)
1822 close(fd2);
d52bb542
AC
1823 return 0;
1824 }
1825
1826 devlist.next = NULL;
1827 devlist.used = 0;
e22fe3ae
N
1828 devlist.writemostly = FlagDefault;
1829 devlist.failfast = FlagDefault;
d52bb542
AC
1830 devlist.devname = devname;
1831 sprintf(devname, "%d:%d", major(devid), minor(devid));
1832
1833 devlist.disposition = 'r';
f2e8393b 1834 if (Manage_subdevs(from_devname, fd2, &devlist, -1, 0, UOPT_UNDEFINED, 0) == 0) {
d52bb542 1835 devlist.disposition = 'a';
9cf361f8 1836 if (Manage_subdevs(to_devname, fd1, &devlist, -1, 0,
f2e8393b 1837 UOPT_UNDEFINED, 0) == 0) {
d52bb542
AC
1838 /* make sure manager is aware of changes */
1839 ping_manager(to_devname);
1840 ping_manager(from_devname);
1841 close(fd1);
1842 close(fd2);
1843 return 1;
1844 }
9cf361f8
JS
1845 else
1846 Manage_subdevs(from_devname, fd2, &devlist,
f2e8393b 1847 -1, 0, UOPT_UNDEFINED, 0);
d52bb542
AC
1848 }
1849 close(fd1);
1850 close(fd2);
1851 return 0;
1852}