]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Manage.c
mdctl-v0.3
[thirdparty/mdadm.git] / Manage.c
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"
31 #include "md_u.h"
32 #include "md_p.h"
33
34 int Manage_ro(char *devname, int fd, int readonly)
35 {
36 /* switch to readonly or rw
37 *
38 * requires >= 0.90.0
39 * first check that array is runing
40 * use RESTART_ARRAY_RW or STOP_ARRAY_RO
41 *
42 */
43 mdu_array_info_t array;
44
45 if (md_get_version(fd) < 9000) {
46 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
47 return 1;
48 }
49 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
50 fprintf(stderr, Name ": %s does not appear to be active.\n",
51 devname);
52 return 1;
53 }
54
55 if (readonly>0) {
56 if (ioctl(fd, STOP_ARRAY_RO, NULL)) {
57 fprintf(stderr, Name ": failed to set readonly for %s: %s\n",
58 devname, strerror(errno));
59 return 1;
60 }
61 } else if (readonly < 0) {
62 if (ioctl(fd, RESTART_ARRAY_RW, NULL)) {
63 fprintf(stderr, Name ": fail to re writable for %s: %s\n",
64 devname, strerror(errno));
65 return 1;
66 }
67 }
68 return 0;
69 }
70
71 int Manage_runstop(char *devname, int fd, int runstop)
72 {
73 /* Run or stop the array. array must already be configured
74 * required >= 0.90.0
75 */
76 mdu_array_info_t array;
77 mdu_param_t param; /* unused */
78
79 if (md_get_version(fd) < 9000) {
80 fprintf(stderr, Name ": need md driver version 0.90.0 or later\n");
81 return 1;
82 }
83 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
84 fprintf(stderr, Name ": %s does not appear to be active.\n",
85 devname);
86 return 1;
87 }
88
89 if (runstop>0) {
90 if (ioctl(fd, RUN_ARRAY, &param)) {
91 fprintf(stderr, Name ": failed to run array %s: %s\n",
92 devname, strerror(errno));
93 return 1;
94 }
95 } else if (runstop < 0){
96 if (ioctl(fd, STOP_ARRAY, NULL)) {
97 fprintf(stderr, Name ": fail to re writable for %s: %s\n",
98 devname, strerror(errno));
99 return 1;
100 }
101 }
102 return 0;
103 }
104
105 int Manage_subdevs(char *devname, int fd,
106 int devcnt, char *devnames[], int devmodes[])
107 {
108 /* do something to each dev.
109 * devmode can be
110 * 'a' - add the device
111 * try HOT_ADD_DISK
112 * If that fails EINVAL, try ADD_NEW_DISK
113 * 'r' - remove the device HOT_REMOVE_DISK
114 * 'f' - set the device faulty SET_DISK_FAULTY
115 */
116 mdu_array_info_t array;
117 mdu_disk_info_t disc;
118 struct stat stb;
119 int i,j;
120
121 if (ioctl(fd, GET_ARRAY_INFO, &array)) {
122 fprintf(stderr, Name ": cannot get array info for %s\n",
123 devname);
124 return 1;
125 }
126 for (i=0 ; i<devcnt; i++) {
127 if (stat(devnames[i], &stb)) {
128 fprintf(stderr, Name ": cannot find %s: %s\n",
129 devnames[i], strerror(errno));
130 return 1;
131 }
132 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
133 fprintf(stderr, Name ": %s is not a block device.\n",
134 devnames[i]);
135 return 1;
136 }
137 switch(devmodes[i]){
138 default:
139 fprintf(stderr, Name ": internal error - devmode[%d]=%d\n",
140 i, devmodes[i]);
141 return 1;
142 case 'a':
143 /* add the device - hot or cold */
144 if (ioctl(fd, HOT_ADD_DISK, stb.st_rdev)==0) {
145 fprintf(stderr, Name ": hot added %s\n",
146 devnames[i]);
147 continue;
148 }
149 /* try ADD_NEW_DISK.
150 * we might be creating, we might be assembling,
151 * it is hard to tell.
152 * set up number/raid_disk/state just
153 * in case
154 */
155 for (j=0; j<array.nr_disks; j++) {
156 if (ioctl(fd, GET_DISK_INFO, &disc))
157 break;
158 if (disc.major==0 && disc.minor==0)
159 break;
160 if (disc.state & 8) /* removed */
161 break;
162 }
163 disc.number =j;
164 disc.raid_disk = j;
165 disc.state = 0;
166 disc.major = MAJOR(stb.st_rdev);
167 disc.minor = MINOR(stb.st_rdev);
168 if (ioctl(fd,ADD_NEW_DISK, &disc)) {
169 fprintf(stderr, Name ": add new disk failed for %s: %s\n",
170 devnames[i], strerror(errno));
171 return 1;
172 }
173 fprintf(stderr, Name ": added %s\n", devnames[i]);
174 break;
175
176 case 'r':
177 /* hot remove */
178 /* FIXME check that is is a current member */
179 if (ioctl(fd, HOT_REMOVE_DISK, stb.st_rdev)) {
180 fprintf(stderr, Name ": hot remove failed for %s: %s\n",
181 devnames[i], strerror(errno));
182 return 1;
183 }
184 fprintf(stderr, Name ": hot removed %s\n", devnames[i]);
185 break;
186
187 case 'f': /* set faulty */
188 /* FIXME check current member */
189 if (ioctl(fd, SET_DISK_FAULTY, stb.st_rdev)) {
190 fprintf(stderr, Name ": set disk faulty failed for %s: %s\n",
191 devnames[i], strerror(errno));
192 return 1;
193 }
194 fprintf(stderr, Name ": set %s faulty in %s\n",
195 devnames[i], devname);
196 break;
197 }
198 }
199 return 0;
200
201 }