Fix --detail output for version 1 superblocks.
[thirdparty/mdadm.git] / Manage.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
cd29a5c8 4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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
91f068bf 75int Manage_runstop(char *devname, int fd, int runstop, int quiet)
64c4757e 76{
682c7051
NB
77 /* Run or stop the array. array must already be configured
78 * required >= 0.90.0
79 */
682c7051 80 mdu_param_t param; /* unused */
82b27616
NB
81
82 if (runstop == -1 && md_get_version(fd) < 9000) {
83 if (ioctl(fd, STOP_MD, 0)) {
91f068bf
NB
84 if (!quiet) fprintf(stderr, Name ": stopping device %s failed: %s\n",
85 devname, strerror(errno));
82b27616
NB
86 return 1;
87 }
88 }
682c7051
NB
89
90 if (md_get_version(fd) < 9000) {
91 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
92 return 1;
93 }
82b27616 94 /*
682c7051
NB
95 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
96 fprintf(stderr, Name ": %s does not appear to be active.\n",
97 devname);
98 return 1;
99 }
82b27616 100 */
682c7051
NB
101 if (runstop>0) {
102 if (ioctl(fd, RUN_ARRAY, &param)) {
103 fprintf(stderr, Name ": failed to run array %s: %s\n",
104 devname, strerror(errno));
105 return 1;
106 }
107 } else if (runstop < 0){
108 if (ioctl(fd, STOP_ARRAY, NULL)) {
91f068bf
NB
109 if (!quiet)
110 fprintf(stderr, Name ": fail to stop array %s: %s\n",
111 devname, strerror(errno));
682c7051
NB
112 return 1;
113 }
114 }
115 return 0;
64c4757e
NB
116}
117
dd0781e5
NB
118int Manage_resize(char *devname, int fd, long long size, int raid_disks)
119{
120 mdu_array_info_t info;
121 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
122 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
123 devname, strerror(errno));
124 return 1;
125 }
126 if (size >= 0)
127 info.size = size;
128 if (raid_disks > 0)
129 info.raid_disks = raid_disks;
130 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
131 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
132 devname, strerror(errno));
133 return 1;
134 }
135 return 0;
136}
137
b5e64645
NB
138int Manage_reconfig(char *devname, int fd, int layout)
139{
140 mdu_array_info_t info;
141 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
142 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
143 devname, strerror(errno));
144 return 1;
145 }
146 info.layout = layout;
147 printf("layout set to %d\n", info.layout);
148 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
149 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
150 devname, strerror(errno));
151 return 1;
152 }
153 return 0;
154}
dd0781e5 155
64c4757e 156int Manage_subdevs(char *devname, int fd,
dab6685f 157 mddev_dev_t devlist, int verbose)
cd29a5c8 158{
682c7051
NB
159 /* do something to each dev.
160 * devmode can be
161 * 'a' - add the device
162 * try HOT_ADD_DISK
163 * If that fails EINVAL, try ADD_NEW_DISK
164 * 'r' - remove the device HOT_REMOVE_DISK
165 * 'f' - set the device faulty SET_DISK_FAULTY
166 */
167 mdu_array_info_t array;
168 mdu_disk_info_t disc;
cd29a5c8 169 mddev_dev_t dv;
682c7051 170 struct stat stb;
c913b90e 171 int j;
0fbf459d 172 int tfd;
892debc8
NB
173 struct supertype *st;
174 void *dsuper = NULL;
682c7051
NB
175
176 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
177 fprintf(stderr, Name ": cannot get array info for %s\n",
178 devname);
179 return 1;
180 }
cd29a5c8
NB
181 for (dv = devlist ; dv; dv=dv->next) {
182 if (stat(dv->devname, &stb)) {
682c7051 183 fprintf(stderr, Name ": cannot find %s: %s\n",
cd29a5c8 184 dv->devname, strerror(errno));
682c7051
NB
185 return 1;
186 }
187 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
188 fprintf(stderr, Name ": %s is not a block device.\n",
cd29a5c8 189 dv->devname);
682c7051
NB
190 return 1;
191 }
cd29a5c8 192 switch(dv->disposition){
682c7051 193 default:
c913b90e
NB
194 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
195 dv->devname, dv->disposition);
682c7051
NB
196 return 1;
197 case 'a':
198 /* add the device - hot or cold */
f277ce36 199 /* Make sure it isn't in use (in 2.6 or later) */
0fbf459d
NB
200 tfd = open(dv->devname, O_RDONLY|O_EXCL);
201 if (tfd < 0) {
d7eaf49f
NB
202 fprintf(stderr, Name ": Cannot open %s: %s\n",
203 dv->devname, strerror(errno));
204 return 1;
205 }
0fbf459d 206 close(tfd);
60d9a174
NB
207 if (array.major_version == 0 &&
208 md_get_version(fd)%100 < 2) {
892debc8
NB
209 if (ioctl(fd, HOT_ADD_DISK,
210 (unsigned long)stb.st_rdev)==0) {
dab6685f
NB
211 if (verbose >= 0)
212 fprintf(stderr, Name ": hot added %s\n",
213 dv->devname);
892debc8
NB
214 continue;
215 }
216
cd29a5c8 217 fprintf(stderr, Name ": hot add failed for %s: %s\n",
892debc8 218 dv->devname, strerror(errno));
cd29a5c8
NB
219 return 1;
220 }
892debc8
NB
221
222 /* need to find a sample superblock to copy, and
223 * a spare slot to use
682c7051 224 */
892debc8
NB
225 st = super_by_version(array.major_version,
226 array.minor_version);
227 if (!st) {
228 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
229 array.major_version, array.minor_version);
230 return 1;
231 }
f277ce36 232 for (j=0; j<st->max_devs; j++) {
892debc8
NB
233 char *dev;
234 int dfd;
235 disc.number = j;
236 if (ioctl(fd, GET_DISK_INFO, &disc))
237 continue;
238 if (disc.major==0 && disc.minor==0)
239 continue;
240 if ((disc.state & 4)==0) continue; /* sync */
241 /* Looks like a good device to try */
242 dev = map_dev(disc.major, disc.minor);
243 if (!dev) continue;
244 dfd = open(dev, O_RDONLY);
245 if (dfd < 0) continue;
246 if (st->ss->load_super(st, dfd, &dsuper, NULL)) {
247 close(dfd);
248 continue;
249 }
250 close(dfd);
251 break;
252 }
253 if (!dsuper) {
254 fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
255 return 1;
256 }
f277ce36 257 for (j=0; j< st->max_devs; j++) {
aa88f531 258 disc.number = j;
682c7051
NB
259 if (ioctl(fd, GET_DISK_INFO, &disc))
260 break;
261 if (disc.major==0 && disc.minor==0)
262 break;
263 if (disc.state & 8) /* removed */
264 break;
265 }
0df46c2a
NB
266 disc.major = major(stb.st_rdev);
267 disc.minor = minor(stb.st_rdev);
892debc8
NB
268 disc.number =j;
269 disc.state = 0;
dfd4d8ee
NB
270 if (dv->writemostly)
271 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
f277ce36 272 st->ss->add_to_super(dsuper, &disc);
892debc8
NB
273 if (st->ss->write_init_super(st, dsuper, &disc, dv->devname))
274 return 1;
682c7051 275 if (ioctl(fd,ADD_NEW_DISK, &disc)) {
f277ce36
NB
276 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
277 dv->devname, j, strerror(errno));
682c7051
NB
278 return 1;
279 }
dab6685f
NB
280 if (verbose >= 0)
281 fprintf(stderr, Name ": added %s\n", dv->devname);
682c7051
NB
282 break;
283
284 case 'r':
285 /* hot remove */
82b27616 286 /* FIXME check that it is a current member */
cd29a5c8 287 if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
682c7051 288 fprintf(stderr, Name ": hot remove failed for %s: %s\n",
cd29a5c8 289 dv->devname, strerror(errno));
682c7051
NB
290 return 1;
291 }
dab6685f
NB
292 if (verbose >= 0)
293 fprintf(stderr, Name ": hot removed %s\n", dv->devname);
682c7051
NB
294 break;
295
296 case 'f': /* set faulty */
297 /* FIXME check current member */
cd29a5c8 298 if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
b83d95f3 299 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
cd29a5c8 300 dv->devname, strerror(errno));
682c7051
NB
301 return 1;
302 }
dab6685f
NB
303 if (verbose >= 0)
304 fprintf(stderr, Name ": set %s faulty in %s\n",
305 dv->devname, devname);
682c7051
NB
306 break;
307 }
308 }
309 return 0;
310
64c4757e 311}