]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Manage.c
mdadm-1.7.0
[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
75int Manage_runstop(char *devname, int fd, int runstop)
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)) {
84 fprintf(stderr, Name ": stopping device %s failed: %s\n",
85 devname, strerror(errno));
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)) {
52826846 109 fprintf(stderr, Name ": fail to stop array %s: %s\n",
682c7051
NB
110 devname, strerror(errno));
111 return 1;
112 }
113 }
114 return 0;
64c4757e
NB
115}
116
dd0781e5
NB
117int Manage_resize(char *devname, int fd, long long size, int raid_disks)
118{
119 mdu_array_info_t info;
120 if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
121 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
122 devname, strerror(errno));
123 return 1;
124 }
125 if (size >= 0)
126 info.size = size;
127 if (raid_disks > 0)
128 info.raid_disks = raid_disks;
129 if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
130 fprintf(stderr, Name ": Cannot set device size/shape for %s: %s\n",
131 devname, strerror(errno));
132 return 1;
133 }
134 return 0;
135}
136
137
64c4757e 138int Manage_subdevs(char *devname, int fd,
cd29a5c8
NB
139 mddev_dev_t devlist)
140{
682c7051
NB
141 /* do something to each dev.
142 * devmode can be
143 * 'a' - add the device
144 * try HOT_ADD_DISK
145 * If that fails EINVAL, try ADD_NEW_DISK
146 * 'r' - remove the device HOT_REMOVE_DISK
147 * 'f' - set the device faulty SET_DISK_FAULTY
148 */
149 mdu_array_info_t array;
150 mdu_disk_info_t disc;
cd29a5c8 151 mddev_dev_t dv;
682c7051 152 struct stat stb;
c913b90e 153 int j;
cd29a5c8 154 int save_errno;
e0d19036 155 static char buf[4096];
682c7051
NB
156
157 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
158 fprintf(stderr, Name ": cannot get array info for %s\n",
159 devname);
160 return 1;
161 }
cd29a5c8
NB
162 for (dv = devlist ; dv; dv=dv->next) {
163 if (stat(dv->devname, &stb)) {
682c7051 164 fprintf(stderr, Name ": cannot find %s: %s\n",
cd29a5c8 165 dv->devname, strerror(errno));
682c7051
NB
166 return 1;
167 }
168 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
169 fprintf(stderr, Name ": %s is not a block device.\n",
cd29a5c8 170 dv->devname);
682c7051
NB
171 return 1;
172 }
cd29a5c8 173 switch(dv->disposition){
682c7051 174 default:
c913b90e
NB
175 fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
176 dv->devname, dv->disposition);
682c7051
NB
177 return 1;
178 case 'a':
179 /* add the device - hot or cold */
cd29a5c8 180 if (ioctl(fd, HOT_ADD_DISK, (unsigned long)stb.st_rdev)==0) {
682c7051 181 fprintf(stderr, Name ": hot added %s\n",
cd29a5c8 182 dv->devname);
682c7051
NB
183 continue;
184 }
cd29a5c8
NB
185 save_errno = errno;
186 if (read(fd, buf, sizeof(buf)) > 0) {
187 /* array is active, so don't try to add.
188 * i.e. something is wrong
189 */
190 fprintf(stderr, Name ": hot add failed for %s: %s\n",
191 dv->devname, strerror(save_errno));
192 return 1;
193 }
682c7051
NB
194 /* try ADD_NEW_DISK.
195 * we might be creating, we might be assembling,
196 * it is hard to tell.
197 * set up number/raid_disk/state just
198 * in case
199 */
200 for (j=0; j<array.nr_disks; j++) {
aa88f531 201 disc.number = j;
682c7051
NB
202 if (ioctl(fd, GET_DISK_INFO, &disc))
203 break;
204 if (disc.major==0 && disc.minor==0)
205 break;
206 if (disc.state & 8) /* removed */
207 break;
208 }
209 disc.number =j;
210 disc.raid_disk = j;
211 disc.state = 0;
212 disc.major = MAJOR(stb.st_rdev);
213 disc.minor = MINOR(stb.st_rdev);
214 if (ioctl(fd,ADD_NEW_DISK, &disc)) {
b83d95f3 215 fprintf(stderr, Name ": add new device failed for %s: %s\n",
cd29a5c8 216 dv->devname, strerror(errno));
682c7051
NB
217 return 1;
218 }
cd29a5c8 219 fprintf(stderr, Name ": added %s\n", dv->devname);
682c7051
NB
220 break;
221
222 case 'r':
223 /* hot remove */
82b27616 224 /* FIXME check that it is a current member */
cd29a5c8 225 if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
682c7051 226 fprintf(stderr, Name ": hot remove failed for %s: %s\n",
cd29a5c8 227 dv->devname, strerror(errno));
682c7051
NB
228 return 1;
229 }
cd29a5c8 230 fprintf(stderr, Name ": hot removed %s\n", dv->devname);
682c7051
NB
231 break;
232
233 case 'f': /* set faulty */
234 /* FIXME check current member */
cd29a5c8 235 if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
b83d95f3 236 fprintf(stderr, Name ": set device faulty failed for %s: %s\n",
cd29a5c8 237 dv->devname, strerror(errno));
682c7051
NB
238 return 1;
239 }
240 fprintf(stderr, Name ": set %s faulty in %s\n",
cd29a5c8 241 dv->devname, devname);
682c7051
NB
242 break;
243 }
244 }
245 return 0;
246
64c4757e 247}