Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / mdadm / Manage.c
blob91934822cf83c923dabeafa5d9559911d184f3ef
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
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.
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.
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
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
30 #include "mdadm.h"
31 #include "md_u.h"
32 #include "md_p.h"
34 #define REGISTER_DEV _IO (MD_MAJOR, 1)
35 #define START_MD _IO (MD_MAJOR, 2)
36 #define STOP_MD _IO (MD_MAJOR, 3)
38 int Manage_ro(char *devname, int fd, int readonly)
40 /* switch to readonly or rw
42 * requires >= 0.90.0
43 * first check that array is runing
44 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
47 mdu_array_info_t array;
49 if (md_get_version(fd) < 9000) {
50 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
51 return 1;
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;
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;
65 } else if (readonly < 0) {
66 if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
67 fprintf(stderr, Name ": failed to set writable for %s: %s\n",
68 devname, strerror(errno));
69 return 1;
72 return 0;
75 #ifndef MDASSEMBLE
77 int Manage_runstop(char *devname, int fd, int runstop, int quiet)
79 /* Run or stop the array. array must already be configured
80 * required >= 0.90.0
82 mdu_param_t param; /* unused */
84 if (runstop == -1 && md_get_version(fd) < 9000) {
85 if (ioctl(fd, STOP_MD, 0)) {
86 if (!quiet) fprintf(stderr, Name ": stopping device %s failed: %s\n",
87 devname, strerror(errno));
88 return 1;
92 if (md_get_version(fd) < 9000) {
93 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
94 return 1;
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;
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;
109 if (quiet <= 0)
110 fprintf(stderr, Name ": started %s\n", devname);
111 } else if (runstop < 0){
112 struct map_ent *map = NULL;
113 struct stat stb;
114 if (ioctl(fd, STOP_ARRAY, NULL)) {
115 if (quiet==0)
116 fprintf(stderr, Name ": fail to stop array %s: %s\n",
117 devname, strerror(errno));
118 return 1;
120 if (quiet <= 0)
121 fprintf(stderr, Name ": stopped %s\n", devname);
122 if (fstat(fd, &stb) == 0) {
123 int devnum;
124 if (major(stb.st_rdev) == MD_MAJOR)
125 devnum = minor(stb.st_rdev);
126 else
127 devnum = -1-(minor(stb.st_rdev)>>6);
128 map_delete(&map, devnum);
129 map_write(map);
130 map_free(map);
133 return 0;
136 int Manage_resize(char *devname, int fd, long long size, int raid_disks)
138 mdu_array_info_t info;
139 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
140 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
141 devname, strerror(errno));
142 return 1;
144 if (size >= 0)
145 info.size = size;
146 if (raid_disks > 0)
147 info.raid_disks = raid_disks;
148 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
149 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
150 devname, strerror(errno));
151 return 1;
153 return 0;
156 int Manage_reconfig(char *devname, int fd, int layout)
158 mdu_array_info_t info;
159 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
160 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
161 devname, strerror(errno));
162 return 1;
164 info.layout = layout;
165 printf("layout set to %d\n", info.layout);
166 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
167 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
168 devname, strerror(errno));
169 return 1;
171 return 0;
174 int Manage_subdevs(char *devname, int fd,
175 mddev_dev_t devlist, int verbose)
177 /* do something to each dev.
178 * devmode can be
179 * 'a' - add the device
180 * try HOT_ADD_DISK
181 * If that fails EINVAL, try ADD_NEW_DISK
182 * 'r' - remove the device HOT_REMOVE_DISK
183 * 'f' - set the device faulty SET_DISK_FAULTY
185 mdu_array_info_t array;
186 mdu_disk_info_t disc;
187 mddev_dev_t dv;
188 struct stat stb;
189 int j;
190 int tfd;
191 struct supertype *st;
192 void *dsuper = NULL;
193 void *osuper = NULL; /* original super */
194 int duuid[4];
195 int ouuid[4];
197 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
198 fprintf(stderr, Name ": cannot get array info for %s\n",
199 devname);
200 return 1;
202 for (dv = devlist ; dv; dv=dv->next) {
203 unsigned long long ldsize;
205 if (stat(dv->devname, &stb)) {
206 fprintf(stderr, Name ": cannot find %s: %s\n",
207 dv->devname, strerror(errno));
208 return 1;
210 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
211 fprintf(stderr, Name ": %s is not a block device.\n",
212 dv->devname);
213 return 1;
215 switch(dv->disposition){
216 default:
217 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
218 dv->devname, dv->disposition);
219 return 1;
220 case 'a':
221 /* add the device */
222 st = super_by_version(array.major_version,
223 array.minor_version);
224 if (!st) {
225 fprintf(stderr, Name ": unsupport array - version %d.%d\n",
226 array.major_version, array.minor_version);
227 return 1;
230 /* Make sure it isn't in use (in 2.6 or later) */
231 tfd = open(dv->devname, O_RDONLY|O_EXCL);
232 if (tfd < 0) {
233 fprintf(stderr, Name ": Cannot open %s: %s\n",
234 dv->devname, strerror(errno));
235 return 1;
237 if (array.not_persistent==0)
238 st->ss->load_super(st, tfd, &osuper, NULL);
239 /* will use osuper later */
240 if (!get_dev_size(tfd, dv->devname, &ldsize)) {
241 close(tfd);
242 return 1;
244 close(tfd);
246 if (array.major_version == 0 &&
247 md_get_version(fd)%100 < 2) {
248 if (ioctl(fd, HOT_ADD_DISK,
249 (unsigned long)stb.st_rdev)==0) {
250 if (verbose >= 0)
251 fprintf(stderr, Name ": hot added %s\n",
252 dv->devname);
253 continue;
256 fprintf(stderr, Name ": hot add failed for %s: %s\n",
257 dv->devname, strerror(errno));
258 return 1;
261 if (array.not_persistent == 0) {
263 /* Make sure device is large enough */
264 if (st->ss->avail_size(st, ldsize/512) <
265 array.size) {
266 fprintf(stderr, Name ": %s not large enough to join array\n",
267 dv->devname);
268 return 1;
271 /* need to find a sample superblock to copy, and
272 * a spare slot to use
274 for (j=0; j<st->max_devs; j++) {
275 char *dev;
276 int dfd;
277 disc.number = j;
278 if (ioctl(fd, GET_DISK_INFO, &disc))
279 continue;
280 if (disc.major==0 && disc.minor==0)
281 continue;
282 if ((disc.state & 4)==0) continue; /* sync */
283 /* Looks like a good device to try */
284 dev = map_dev(disc.major, disc.minor, 1);
285 if (!dev) continue;
286 dfd = dev_open(dev, O_RDONLY);
287 if (dfd < 0) continue;
288 if (st->ss->load_super(st, dfd, &dsuper, NULL)) {
289 close(dfd);
290 continue;
292 remove_partitions(dfd);
293 close(dfd);
294 break;
296 if (!dsuper) {
297 fprintf(stderr, Name ": cannot find valid superblock in this array - HELP\n");
298 return 1;
300 /* Possibly this device was recently part of the array
301 * and was temporarily removed, and is now being re-added.
302 * If so, we can simply re-add it.
304 st->ss->uuid_from_super(duuid, dsuper);
306 /* re-add doesn't work for version-1 superblocks
307 * before 2.6.18 :-(
309 if (array.major_version == 1 &&
310 get_linux_version() <= 2006018)
312 else if (osuper) {
313 st->ss->uuid_from_super(ouuid, osuper);
314 if (memcmp(duuid, ouuid, sizeof(ouuid))==0) {
315 /* look close enough for now. Kernel
316 * will worry about where a bitmap
317 * based reconstruct is possible
319 struct mdinfo mdi;
320 st->ss->getinfo_super(&mdi, osuper);
321 disc.major = major(stb.st_rdev);
322 disc.minor = minor(stb.st_rdev);
323 disc.number = mdi.disk.number;
324 disc.raid_disk = mdi.disk.raid_disk;
325 disc.state = mdi.disk.state;
326 if (ioctl(fd, ADD_NEW_DISK, &disc) == 0) {
327 if (verbose >= 0)
328 fprintf(stderr, Name ": re-added %s\n", dv->devname);
329 continue;
331 /* fall back on normal-add */
334 } else {
335 /* non-persistent. Must ensure that new drive
336 * is at least array.size big.
338 if (ldsize/512 < array.size) {
339 fprintf(stderr, Name ": %s not large enough to join array\n",
340 dv->devname);
341 return 1;
344 /* in 2.6.17 and earlier, version-1 superblocks won't
345 * use the number we write, but will choose a free number.
346 * we must choose the same free number, which requires
347 * starting at 'raid_disks' and counting up
349 for (j = array.raid_disks; j< st->max_devs; j++) {
350 disc.number = j;
351 if (ioctl(fd, GET_DISK_INFO, &disc))
352 break;
353 if (disc.major==0 && disc.minor==0)
354 break;
355 if (disc.state & 8) /* removed */
356 break;
358 disc.major = major(stb.st_rdev);
359 disc.minor = minor(stb.st_rdev);
360 disc.number =j;
361 disc.state = 0;
362 if (array.not_persistent==0) {
363 if (dv->writemostly)
364 disc.state |= 1 << MD_DISK_WRITEMOSTLY;
365 st->ss->add_to_super(dsuper, &disc);
366 if (st->ss->write_init_super(st, dsuper, &disc, dv->devname))
367 return 1;
368 } else if (dv->re_add) {
369 /* this had better be raid1.
370 * As we are "--re-add"ing we must find a spare slot
371 * to fill.
373 char *used = malloc(array.raid_disks);
374 memset(used, 0, array.raid_disks);
375 for (j=0; j< st->max_devs; j++) {
376 mdu_disk_info_t disc2;
377 disc2.number = j;
378 if (ioctl(fd, GET_DISK_INFO, &disc2))
379 continue;
380 if (disc2.major==0 && disc2.minor==0)
381 continue;
382 if (disc2.state & 8) /* removed */
383 continue;
384 if (disc2.raid_disk < 0)
385 continue;
386 if (disc2.raid_disk > array.raid_disks)
387 continue;
388 used[disc2.raid_disk] = 1;
390 for (j=0 ; j<array.raid_disks; j++)
391 if (!used[j]) {
392 disc.raid_disk = j;
393 disc.state |= (1<<MD_DISK_SYNC);
394 break;
397 if (dv->writemostly)
398 disc.state |= (1 << MD_DISK_WRITEMOSTLY);
399 if (ioctl(fd,ADD_NEW_DISK, &disc)) {
400 fprintf(stderr, Name ": add new device failed for %s as %d: %s\n",
401 dv->devname, j, strerror(errno));
402 return 1;
404 if (verbose >= 0)
405 fprintf(stderr, Name ": added %s\n", dv->devname);
406 break;
408 case 'r':
409 /* hot remove */
410 /* FIXME check that it is a current member */
411 if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
412 fprintf(stderr, Name ": hot remove failed for %s: %s\n",
413 dv->devname, strerror(errno));
414 return 1;
416 if (verbose >= 0)
417 fprintf(stderr, Name ": hot removed %s\n", dv->devname);
418 break;
420 case 'f': /* set faulty */
421 /* FIXME check current member */
422 if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
423 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
424 dv->devname, strerror(errno));
425 return 1;
427 if (verbose >= 0)
428 fprintf(stderr, Name ": set %s faulty in %s\n",
429 dv->devname, devname);
430 break;
433 return 0;
436 #endif