Use O_EXCL when opening component devices to be assembled into an array
[thirdparty/mdadm.git] / Manage.c
1 /*
2  * mdadm - manage Linux "md" devices aka RAID arrays.
3  *
4  * Copyright (C) 2001-2002 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 "mdadm.h"
31 #include "md_u.h"
32 #include "md_p.h"
33
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
38 int Manage_ro(char *devname, int fd, int readonly)
39 {
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)) {
67                         fprintf(stderr, Name ": failed to set writable for %s: %s\n",
68                                 devname, strerror(errno));
69                         return 1;
70                 }
71         }
72         return 0;                       
73 }
74
75 int Manage_runstop(char *devname, int fd, int runstop)
76 {
77         /* Run or stop the array. array must already be configured
78          * required >= 0.90.0
79          */
80         mdu_param_t param; /* unused */
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         }
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         }
94         /*
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         }
100         */
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)) {
109                         fprintf(stderr, Name ": fail to stop array %s: %s\n",
110                                 devname, strerror(errno));
111                         return 1;
112                 }
113         }
114         return 0;
115 }
116
117 int 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 int Manage_reconfig(char *devname, int fd, int layout)
138 {
139         mdu_array_info_t info;
140         if (ioctl(fd, GET_ARRAY_INFO, &info) != 0) {
141                 fprintf(stderr, Name ": Cannot get array information for %s: %s\n",
142                         devname, strerror(errno));
143                 return 1;
144         }
145         info.layout = layout;
146         printf("layout set to %d\n", info.layout);
147         if (ioctl(fd, SET_ARRAY_INFO, &info) != 0) {
148                 fprintf(stderr, Name ": Cannot set layout for %s: %s\n",
149                         devname, strerror(errno));
150                 return 1;
151         }
152         return 0;
153 }
154
155 int Manage_subdevs(char *devname, int fd,
156                    mddev_dev_t devlist)
157 {
158         /* do something to each dev.
159          * devmode can be
160          *  'a' - add the device
161          *         try HOT_ADD_DISK
162          *         If that fails EINVAL, try ADD_NEW_DISK
163          *  'r' - remove the device HOT_REMOVE_DISK
164          *  'f' - set the device faulty SET_DISK_FAULTY
165          */
166         mdu_array_info_t array;
167         mdu_disk_info_t disc;
168         mddev_dev_t dv;
169         struct stat stb;
170         int j;
171         int save_errno;
172         static char buf[4096];
173
174         if (ioctl(fd, GET_ARRAY_INFO, &array)) {
175                 fprintf(stderr, Name ": cannot get array info for %s\n",
176                         devname);
177                 return 1;
178         }
179         for (dv = devlist ; dv; dv=dv->next) {
180                 if (stat(dv->devname, &stb)) {
181                         fprintf(stderr, Name ": cannot find %s: %s\n",
182                                 dv->devname, strerror(errno));
183                         return 1;
184                 }
185                 if ((stb.st_mode & S_IFMT) != S_IFBLK) {
186                         fprintf(stderr, Name ": %s is not a block device.\n",
187                                 dv->devname);
188                         return 1;
189                 }
190                 switch(dv->disposition){
191                 default:
192                         fprintf(stderr, Name ": internal error - devmode[%s]=%d\n",
193                                 dv->devname, dv->disposition);
194                         return 1;
195                 case 'a':
196                         /* add the device - hot or cold */
197                         /* Make sure it isn' in use (in 2.6 or later) */
198                         fd = open(dv->devname, O_RDONLY|O_EXCL);
199                         if (fd < 0) {
200                                 fprintf(stderr, Name ": Cannot open %s: %s\n",
201                                         dv->devname, strerror(errno));
202                                 return 1;
203                         }
204                         close(fd);
205                         if (ioctl(fd, HOT_ADD_DISK, (unsigned long)stb.st_rdev)==0) {
206                                 fprintf(stderr, Name ": hot added %s\n",
207                                         dv->devname);
208                                 continue;
209                         }
210                         save_errno = errno;
211                         if (read(fd, buf, sizeof(buf)) > 0) {
212                                 /* array is active, so don't try to add.
213                                  * i.e. something is wrong 
214                                  */
215                                 fprintf(stderr, Name ": hot add failed for %s: %s\n",
216                                         dv->devname, strerror(save_errno));
217                                 return 1;
218                         }
219                         /* try ADD_NEW_DISK.
220                          * we might be creating, we might be assembling,
221                          * it is hard to tell.
222                          * set up number/raid_disk/state just
223                          * in case
224                          */
225                         for (j=0; j<array.nr_disks; j++) {
226                                 disc.number = j;
227                                 if (ioctl(fd, GET_DISK_INFO, &disc))
228                                         break;
229                                 if (disc.major==0 && disc.minor==0)
230                                         break;
231                                 if (disc.state & 8) /* removed */
232                                         break;
233                         }
234                         disc.number =j;
235                         disc.raid_disk = j;
236                         disc.state = 0;
237                         disc.major = major(stb.st_rdev);
238                         disc.minor = minor(stb.st_rdev);
239                         if (ioctl(fd,ADD_NEW_DISK, &disc)) {
240                                 fprintf(stderr, Name ": add new device failed for %s: %s\n",
241                                         dv->devname, strerror(errno));
242                                 return 1;
243                         }
244                         fprintf(stderr, Name ": added %s\n", dv->devname);
245                         break;
246
247                 case 'r':
248                         /* hot remove */
249                         /* FIXME check that it is a current member */
250                         if (ioctl(fd, HOT_REMOVE_DISK, (unsigned long)stb.st_rdev)) {
251                                 fprintf(stderr, Name ": hot remove failed for %s: %s\n",
252                                         dv->devname, strerror(errno));
253                                 return 1;
254                         }
255                         fprintf(stderr, Name ": hot removed %s\n", dv->devname);
256                         break;
257
258                 case 'f': /* set faulty */
259                         /* FIXME check current member */
260                         if (ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev)) {
261                                 fprintf(stderr, Name ": set device faulty failed for %s:  %s\n",
262                                         dv->devname, strerror(errno));
263                                 return 1;
264                         }
265                         fprintf(stderr, Name ": set %s faulty in %s\n",
266                                 dv->devname, devname);
267                         break;
268                 }
269         }
270         return 0;
271         
272 }