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