Centralise code for copying uuid
[thirdparty/mdadm.git] / Manage.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
4f589ad0 4 * Copyright (C) 2001-2006 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
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
9a9dab36 30#include "mdadm.h"
682c7051
NB
31#include "md_u.h"
32#include "md_p.h"
64c4757e 33
82b27616
NB
34#define REGISTER_DEV _IO (MD_MAJOR, 1)
35#define START_MD _IO (MD_MAJOR, 2)
36#define STOP_MD _IO (MD_MAJOR, 3)
37
64c4757e
NB
38int Manage_ro(char *devname, int fd, int readonly)
39{
682c7051
NB
40 /* switch to readonly or rw
41 *
42 * requires >= 0.90.0
43 * first check that array is runing
44 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
45 *
46 */
47 mdu_array_info_t array;
48
49 if (md_get_version(fd) < 9000) {
50 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
51 return 1;
52 }
53 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
54 fprintf(stderr, Name ": %s does not appear to be active.\n",
55 devname);
56 return 1;
57 }
58
59 if (readonly>0) {
60 if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
61 fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
62 devname, strerror(errno));
63 return 1;
64 }
65 } else if (readonly < 0) {
66 if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
82b27616 67 fprintf(stderr, Name ": failed to set writable for %s: %s\n",
682c7051
NB
68 devname, strerror(errno));
69 return 1;
70 }
71 }
72 return 0;
64c4757e
NB
73}
74
435d4ebb
LB
75#ifndef MDASSEMBLE
76
91f068bf 77int Manage_runstop(char *devname, int fd, int runstop, int quiet)
64c4757e 78{
682c7051
NB
79 /* Run or stop the array. array must already be configured
80 * required >= 0.90.0
81 */
682c7051 82 mdu_param_t param; /* unused */
82b27616
NB
83
84 if (runstop == -1 && md_get_version(fd) < 9000) {
85 if (ioctl(fd, STOP_MD, 0)) {
91f068bf
NB
86 if (!quiet) fprintf(stderr, Name ": stopping device %s failed: %s\n",
87 devname, strerror(errno));
82b27616
NB
88 return 1;
89 }
90 }
682c7051
NB
91
92 if (md_get_version(fd) < 9000) {
93 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
94 return 1;
95 }
82b27616 96 /*
682c7051
NB
97 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
98 fprintf(stderr, Name ": %s does not appear to be active.\n",
99 devname);
100 return 1;
101 }
82b27616 102 */
682c7051
NB
103 if (runstop>0) {
104 if (ioctl(fd, RUN_ARRAY, &param)) {
105 fprintf(stderr, Name ": failed to run array %s: %s\n",
106 devname, strerror(errno));
107 return 1;
108 }
109 } else if (runstop < 0){
110 if (ioctl(fd, STOP_ARRAY, NULL)) {
ab56093f 111 if (quiet==0)
91f068bf
NB
112 fprintf(stderr, Name ": fail to stop array %s: %s\n",
113 devname, strerror(errno));
682c7051
NB
114 return 1;
115 }
ab56093f
NB
116 if (quiet <= 0)
117 fprintf(stderr, Name ": stopped %s\n", devname);
682c7051
NB
118 }
119 return 0;
64c4757e
NB
120}
121
dd0781e5
NB
122int Manage_resize(char *devname, int fd, long long size, int raid_disks)
123{
124 mdu_array_info_t info;
125 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
126 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
127 devname, strerror(errno));
128 return 1;
129 }
130 if (size >= 0)
131 info.size = size;
132 if (raid_disks > 0)
133 info.raid_disks = raid_disks;
134 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
135 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
136 devname, strerror(errno));
137 return 1;
138 }
139 return 0;
140}
141
b5e64645
NB
142int Manage_reconfig(char *devname, int fd, int layout)
143{
144 mdu_array_info_t info;
145 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
146 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
147 devname, strerror(errno));
148 return 1;
149 }
150 info.layout = layout;
151 printf("layout set to %d\n", info.layout);
152 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
153 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
154 devname, strerror(errno));
155 return 1;
156 }
157 return 0;
158}
dd0781e5 159
64c4757e 160int Manage_subdevs(char *devname, int fd,
dab6685f 161 mddev_dev_t devlist, int verbose)
cd29a5c8 162{
682c7051
NB
163 /* do something to each dev.
164 * devmode can be
165 * 'a' - add the device
166 * try HOT_ADD_DISK
167 * If that fails EINVAL, try ADD_NEW_DISK
168 * 'r' - remove the device HOT_REMOVE_DISK
169 * 'f' - set the device faulty SET_DISK_FAULTY
170 */
171 mdu_array_info_t array;
172 mdu_disk_info_t disc;
cd29a5c8 173 mddev_dev_t dv;
682c7051 174 struct stat stb;
c913b90e 175 int j;
0fbf459d 176 int tfd;
892debc8
NB
177 struct supertype *st;
178 void *dsuper = NULL;
fe80f49b
NB
179 void *osuper = NULL; /* original super */
180 int duuid[4];
181 int ouuid[4];
682c7051
NB
182
183 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
184 fprintf(stderr, Name ": cannot get array info for %s\n",
185 devname);
186 return 1;
187 }
cd29a5c8 188 for (dv = devlist ; dv; dv=dv->next) {
4a39c6f2 189 unsigned long long ldsize;
4a39c6f2 190
cd29a5c8 191 if (stat(dv->devname, &stb)) {
682c7051 192 fprintf(stderr, Name ": cannot find %s: %s\n",
cd29a5c8 193 dv->devname, strerror(errno));
682c7051
NB
194 return 1;
195 }
196 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
197 fprintf(stderr, Name ": %s is not a block device.\n",
cd29a5c8 198 dv->devname);
682c7051
NB
199 return 1;
200 }
cd29a5c8 201 switch(dv->disposition){
682c7051 202 default:
c913b90e
NB
203 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
204 dv->devname, dv->disposition);
682c7051
NB
205 return 1;
206 case 'a':
4a39c6f2 207 /* add the device */
fe80f49b
NB
208 st = super_by_version(array.major_version,
209 array.minor_version);
210 if (!st) {
211 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
212 array.major_version, array.minor_version);
213 return 1;
214 }
215
f277ce36 216 /* Make sure it isn't in use (in 2.6 or later) */
0fbf459d
NB
217 tfd = open(dv->devname, O_RDONLY|O_EXCL);
218 if (tfd < 0) {
d7eaf49f
NB
219 fprintf(stderr, Name ": Cannot open %s: %s\n",
220 dv->devname, strerror(errno));
221 return 1;
222 }
fe80f49b
NB
223 if (array.not_persistent==0)
224 st->ss->load_super(st, tfd, &osuper, NULL);
225 /* will use osuper later */
beae1dfe 226 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
4a39c6f2
NB
227 close(tfd);
228 return 1;
4a39c6f2 229 }
0fbf459d 230 close(tfd);
fe80f49b 231
60d9a174
NB
232 if (array.major_version == 0 &&
233 md_get_version(fd)%100 < 2) {
892debc8
NB
234 if (ioctl(fd, HOT_ADD_DISK,
235 (unsigned long)stb.st_rdev)==0) {
dab6685f
NB
236 if (verbose >= 0)
237 fprintf(stderr, Name ": hot added %s\n",
238 dv->devname);
892debc8
NB
239 continue;
240 }
241
cd29a5c8 242 fprintf(stderr, Name ": hot add failed for %s: %s\n",
892debc8 243 dv->devname, strerror(errno));
cd29a5c8
NB
244 return 1;
245 }
892debc8 246
fe80f49b
NB
247 if (array.not_persistent == 0) {
248
4a39c6f2
NB
249 /* Make sure device is large enough */
250 if (st->ss->avail_size(st, ldsize/512) <
251 array.size) {
252 fprintf(stderr, Name ": %s not large enough to join array\n",
253 dv->devname);
254 return 1;
255 }
256
fe80f49b
NB
257 /* need to find a sample superblock to copy, and
258 * a spare slot to use
259 */
260 for (j=0; j<st->max_devs; j++) {
261 char *dev;
262 int dfd;
263 disc.number = j;
264 if (ioctl(fd, GET_DISK_INFO, &disc))
265 continue;
266 if (disc.major==0 && disc.minor==0)
267 continue;
268 if ((disc.state & 4)==0) continue; /* sync */
269 /* Looks like a good device to try */
16c6fa80 270 dev = map_dev(disc.major, disc.minor, 1);
fe80f49b 271 if (!dev) continue;
16c6fa80 272 dfd = dev_open(dev, O_RDONLY);
fe80f49b
NB
273 if (dfd < 0) continue;
274 if (st->ss->load_super(st, dfd, &dsuper, NULL)) {
275 close(dfd);
276 continue;
277 }
0430ed48 278 remove_partitions(dfd);
892debc8 279 close(dfd);
fe80f49b
NB
280 break;
281 }
282 if (!dsuper) {
283 fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
284 return 1;
285 }
286 /* Possibly this device was recently part of the array
287 * and was temporarily removed, and is now being re-added.
288 * If so, we can simply re-add it.
289 */
290 st->ss->uuid_from_super(duuid, dsuper);
7eae7080
NB
291
292 /* re-add doesn't work for version-1 superblocks
293 * before 2.6.18 :-(
294 */
295 if (array.major_version == 1 &&
296 get_linux_version() <= 2006018)
297 ;
298 else if (osuper) {
fe80f49b
NB
299 st->ss->uuid_from_super(ouuid, osuper);
300 if (memcmp(duuid, ouuid, sizeof(ouuid))==0) {
301 /* look close enough for now. Kernel
302 * will worry about where a bitmap
303 * based reconstruct is possible
304 */
305 struct mdinfo mdi;
31317663 306 st->ss->getinfo_super(&mdi, osuper);
fe80f49b
NB
307 disc.major = major(stb.st_rdev);
308 disc.minor = minor(stb.st_rdev);
309 disc.number = mdi.disk.number;
310 disc.raid_disk = mdi.disk.raid_disk;
311 disc.state = mdi.disk.state;
312 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
313 if (verbose >= 0)
314 fprintf(stderr, Name ": re-added %s\n", dv->devname);
308e1801 315 continue;
fe80f49b
NB
316 }
317 /* fall back on normal-add */
318 }
892debc8 319 }
4a39c6f2
NB
320 } else {
321 /* non-persistent. Must ensure that new drive
322 * is at least array.size big.
323 */
324 if (ldsize/512 < array.size) {
325 fprintf(stderr, Name ": %s not large enough to join array\n",
326 dv->devname);
327 return 1;
328 }
892debc8 329 }
7eae7080
NB
330 /* in 2.6.17 and earlier, version-1 superblocks won't
331 * use the number we write, but will choose a free number.
332 * we must choose the same free number, which requires
333 * starting at 'raid_disks' and counting up
334 */
335 for (j = array.raid_disks; j< st->max_devs; j++) {
aa88f531 336 disc.number = j;
682c7051
NB
337 if (ioctl(fd, GET_DISK_INFO, &disc))
338 break;
339 if (disc.major==0 && disc.minor==0)
340 break;
341 if (disc.state & 8) /* removed */
342 break;
343 }
0df46c2a
NB
344 disc.major = major(stb.st_rdev);
345 disc.minor = minor(stb.st_rdev);
892debc8
NB
346 disc.number =j;
347 disc.state = 0;
fe80f49b
NB
348 if (array.not_persistent==0) {
349 if (dv->writemostly)
350 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
351 st->ss->add_to_super(dsuper, &disc);
1bf4e2d9 352 if (st->ss->write_init_super(st, dsuper, &disc, dv->devname))
fe80f49b
NB
353 return 1;
354 } else if (dv->re_add) {
355 /* this had better be raid1.
356 * As we are "--re-add"ing we must find a spare slot
357 * to fill.
358 */
359 char *used = malloc(array.raid_disks);
360 memset(used, 0, array.raid_disks);
361 for (j=0; j< st->max_devs; j++) {
362 mdu_disk_info_t disc2;
363 disc2.number = j;
364 if (ioctl(fd, GET_DISK_INFO, &disc2))
365 continue;
366 if (disc2.major==0 && disc2.minor==0)
367 continue;
368 if (disc2.state & 8) /* removed */
369 continue;
370 if (disc2.raid_disk < 0)
371 continue;
372 if (disc2.raid_disk > array.raid_disks)
373 continue;
374 used[disc2.raid_disk] = 1;
375 }
376 for (j=0 ; j<array.raid_disks; j++)
377 if (!used[j]) {
378 disc.raid_disk = j;
379 disc.state |= (1<<MD_DISK_SYNC);
380 break;
381 }
382 }
d8def61c
NB
383 if (dv->writemostly)
384 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
682c7051 385 if (ioctl(fd,ADD_NEW_DISK, &disc)) {
f277ce36
NB
386 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
387 dv->devname, j, strerror(errno));
682c7051
NB
388 return 1;
389 }
dab6685f
NB
390 if (verbose >= 0)
391 fprintf(stderr, Name ": added %s\n", dv->devname);
682c7051
NB
392 break;
393
394 case 'r':
395 /* hot remove */
82b27616 396 /* FIXME check that it is a current member */
cd29a5c8 397 if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
682c7051 398 fprintf(stderr, Name ": hot remove failed for %s: %s\n",
cd29a5c8 399 dv->devname, strerror(errno));
682c7051
NB
400 return 1;
401 }
dab6685f
NB
402 if (verbose >= 0)
403 fprintf(stderr, Name ": hot removed %s\n", dv->devname);
682c7051
NB
404 break;
405
406 case 'f': /* set faulty */
407 /* FIXME check current member */
cd29a5c8 408 if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
b83d95f3 409 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
cd29a5c8 410 dv->devname, strerror(errno));
682c7051
NB
411 return 1;
412 }
dab6685f
NB
413 if (verbose >= 0)
414 fprintf(stderr, Name ": set %s faulty in %s\n",
415 dv->devname, devname);
682c7051
NB
416 break;
417 }
418 }
419 return 0;
420
64c4757e 421}
435d4ebb 422#endif