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