mdctl-0.5
[thirdparty/mdadm.git] / Manage.c
CommitLineData
64c4757e
NB
1/*
2 * mdctl - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001 Neil Brown <neilb@cse.unsw.edu.au>
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
30#include "mdctl.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 */
80 mdu_array_info_t array;
81 mdu_param_t param; /* unused */
82b27616
NB
82
83 if (runstop == -1 && md_get_version(fd) < 9000) {
84 if (ioctl(fd, STOP_MD, 0)) {
85 fprintf(stderr, Name ": stopping device %s failed: %s\n",
86 devname, strerror(errno));
87 return 1;
88 }
89 }
682c7051
NB
90
91 if (md_get_version(fd) < 9000) {
92 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
93 return 1;
94 }
82b27616 95 /*
682c7051
NB
96 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
97 fprintf(stderr, Name ": %s does not appear to be active.\n",
98 devname);
99 return 1;
100 }
82b27616 101 */
682c7051
NB
102 if (runstop>0) {
103 if (ioctl(fd, RUN_ARRAY, &param)) {
104 fprintf(stderr, Name ": failed to run array %s: %s\n",
105 devname, strerror(errno));
106 return 1;
107 }
108 } else if (runstop < 0){
109 if (ioctl(fd, STOP_ARRAY, NULL)) {
52826846 110 fprintf(stderr, Name ": fail to stop array %s: %s\n",
682c7051
NB
111 devname, strerror(errno));
112 return 1;
113 }
114 }
115 return 0;
64c4757e
NB
116}
117
118int Manage_subdevs(char *devname, int fd,
119 int devcnt, char *devnames[], int devmodes[])
682c7051
NB
120 {
121 /* do something to each dev.
122 * devmode can be
123 * 'a' - add the device
124 * try HOT_ADD_DISK
125 * If that fails EINVAL, try ADD_NEW_DISK
126 * 'r' - remove the device HOT_REMOVE_DISK
127 * 'f' - set the device faulty SET_DISK_FAULTY
128 */
129 mdu_array_info_t array;
130 mdu_disk_info_t disc;
131 struct stat stb;
132 int i,j;
133
134 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
135 fprintf(stderr, Name ": cannot get array info for %s\n",
136 devname);
137 return 1;
138 }
139 for (i=0 ; i<devcnt; i++) {
140 if (stat(devnames[i], &stb)) {
141 fprintf(stderr, Name ": cannot find %s: %s\n",
142 devnames[i], strerror(errno));
143 return 1;
144 }
145 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
146 fprintf(stderr, Name ": %s is not a block device.\n",
147 devnames[i]);
148 return 1;
149 }
150 switch(devmodes[i]){
151 default:
152 fprintf(stderr, Name ": internal error - devmode[%d]=%d\n",
153 i, devmodes[i]);
154 return 1;
155 case 'a':
156 /* add the device - hot or cold */
157 if (ioctl(fd, HOT_ADD_DISK, stb.st_rdev)==0) {
158 fprintf(stderr, Name ": hot added %s\n",
159 devnames[i]);
160 continue;
161 }
162 /* try ADD_NEW_DISK.
163 * we might be creating, we might be assembling,
164 * it is hard to tell.
165 * set up number/raid_disk/state just
166 * in case
167 */
168 for (j=0; j<array.nr_disks; j++) {
169 if (ioctl(fd, GET_DISK_INFO, &disc))
170 break;
171 if (disc.major==0 && disc.minor==0)
172 break;
173 if (disc.state & 8) /* removed */
174 break;
175 }
176 disc.number =j;
177 disc.raid_disk = j;
178 disc.state = 0;
179 disc.major = MAJOR(stb.st_rdev);
180 disc.minor = MINOR(stb.st_rdev);
181 if (ioctl(fd,ADD_NEW_DISK, &disc)) {
182 fprintf(stderr, Name ": add new disk failed for %s: %s\n",
183 devnames[i], strerror(errno));
184 return 1;
185 }
186 fprintf(stderr, Name ": added %s\n", devnames[i]);
187 break;
188
189 case 'r':
190 /* hot remove */
82b27616 191 /* FIXME check that it is a current member */
682c7051
NB
192 if (ioctl(fd, HOT_REMOVE_DISK, stb.st_rdev)) {
193 fprintf(stderr, Name ": hot remove failed for %s: %s\n",
194 devnames[i], strerror(errno));
195 return 1;
196 }
197 fprintf(stderr, Name ": hot removed %s\n", devnames[i]);
198 break;
199
200 case 'f': /* set faulty */
201 /* FIXME check current member */
202 if (ioctl(fd, SET_DISK_FAULTY, stb.st_rdev)) {
203 fprintf(stderr, Name ": set disk faulty failed for %s: %s\n",
204 devnames[i], strerror(errno));
205 return 1;
206 }
207 fprintf(stderr, Name ": set %s faulty in %s\n",
208 devnames[i], devname);
209 break;
210 }
211 }
212 return 0;
213
64c4757e 214}