]>
Commit | Line | Data |
---|---|---|
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" | |
31 | #include "md_p.h" | |
32 | #include "md_u.h" | |
33 | ||
34 | int Assemble(char *mddev, int mdfd, | |
35 | int uuid[4], int uuidset, | |
36 | char *conffile, int scan, | |
37 | int subdevs, char **subdev, | |
38 | int readonly, int runstop, | |
39 | int verbose, int force) | |
40 | { | |
41 | /* | |
42 | * The task of Assemble is to submit a | |
43 | * SET_ARRAY_INFO ioctl with no arg - to prepare | |
44 | * the array - and then submit a number of | |
45 | * ADD_NEW_DISK ioctls to add disks into | |
46 | * the array. Finally RUN_ARRAY might | |
47 | * be submitted to start the array. | |
48 | * | |
49 | * Much of the work of Assemble is in finding and/or | |
50 | * checking the disks to make sure they look right. | |
51 | * | |
52 | * If mddev is not set, then scan must be and we | |
53 | * read through the config file for dev+uuid mapping | |
54 | * We recurse, setting mddev, for each device that | |
55 | * - isn't running | |
56 | * - has a valid uuid (or any uuid if !uuidset | |
57 | * | |
58 | * If mddev is set, we try to determine state of md. | |
59 | * check version - must be at least 0.90.0 | |
60 | * check kernel version. must be at least 2.4. | |
61 | * If not, we can possibly fall back on START_ARRAY | |
62 | * Try to GET_ARRAY_INFO. | |
63 | * If possible, give up | |
64 | * If not, try to STOP_ARRAY just to make sure | |
65 | * | |
66 | * If !uuidset and scan, look in conf-file for uuid | |
67 | * If not found, give up | |
68 | * If !subdevs and scan and uuidset, get list of devs from conf-file | |
69 | * | |
70 | * For each device: | |
71 | * Check superblock - discard if bad | |
72 | * Check uuid (set if we don't have one) - discard if no match | |
73 | * Check superblock similarity if we have a superbloc - discard if different | |
74 | * Record events, devicenum, utime | |
75 | * This should give us a list of devices for the array | |
76 | * We should collect the most recent event and utime numbers | |
77 | * | |
78 | * Count disks with recent enough event count | |
79 | * While force && !enough disks | |
80 | * Choose newest rejected disks, update event count | |
81 | * mark clean and rewrite superblock | |
82 | * If recent kernel: | |
83 | * SET_ARRAY_INFO | |
84 | * foreach device with recent events : ADD_NEW_DISK | |
85 | * if runstop == 1 || "enough" disks and runstop==0 -> RUN_ARRAY | |
86 | * If old kernel: | |
87 | * Check the device numbers in superblock are right | |
88 | * update superblock if any changes | |
89 | * START_ARRAY | |
90 | * | |
91 | */ | |
92 | int old_linux = 0; | |
93 | int vers; | |
94 | mdu_array_info_t array; | |
95 | mddev_dev_t devlist = NULL; | |
96 | mdp_super_t first_super, super; | |
97 | struct { | |
98 | char *devname; | |
99 | int major, minor; | |
100 | long long events; | |
101 | time_t utime; | |
102 | int uptodate; | |
103 | } devices[MD_SB_DISKS]; | |
104 | int best[MD_SB_DISKS]; /* indexed by raid_disk */ | |
105 | int devcnt = 0, okcnt; | |
106 | int i; | |
107 | int most_recent = 0; | |
108 | ||
109 | if (!mddev && !scan) { | |
682c7051 | 110 | fputs(Name ": internal error - Assemble called with no devie or scan\n", stderr); |
64c4757e NB |
111 | return 1; |
112 | } | |
113 | if (!mddev) { | |
114 | mddev_uuid_t device_list; | |
115 | int found = 0; | |
116 | device_list = conf_get_uuids(conffile); | |
117 | if (!device_list) { | |
682c7051 | 118 | fprintf(stderr, Name ": No devices found in config file\n"); |
64c4757e NB |
119 | return 1; |
120 | } | |
121 | while (device_list) { | |
122 | if (!uuidset || same_uuid(device_list->uuid,uuid)) { | |
123 | mdfd = open(device_list->devname, O_RDONLY, 0); | |
124 | if (mdfd < 0) { | |
125 | fprintf(stderr, | |
682c7051 | 126 | Name ": error opening %s: %s\n", |
64c4757e NB |
127 | device_list->devname, |
128 | strerror(errno)); | |
129 | continue; | |
130 | } | |
131 | if (Assemble(device_list->devname, mdfd, | |
132 | device_list->uuid, 1, | |
133 | conffile, 1, | |
134 | subdevs, subdev, | |
135 | readonly, runstop, verbose, force)==0) | |
136 | found++; | |
137 | close(mdfd); | |
138 | } | |
139 | device_list = device_list->next; | |
140 | } | |
141 | if (found) | |
142 | return 0; | |
682c7051 | 143 | fprintf(stderr,Name ": Did not successful Assemble any devices\n"); |
64c4757e NB |
144 | return 1; |
145 | } | |
146 | ||
147 | /* | |
148 | * Ok, we have an mddev, check it out | |
149 | */ | |
150 | vers = md_get_version(mdfd); | |
151 | if (vers <= 0) { | |
682c7051 | 152 | fprintf(stderr, Name ": %s appears not to be an md device.\n"); |
64c4757e NB |
153 | return 1; |
154 | } | |
682c7051 NB |
155 | if (vers < 9000) { |
156 | fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n" | |
64c4757e NB |
157 | " Upgrade your kernel or try --Build\n"); |
158 | return 1; | |
159 | } | |
682c7051 | 160 | if (get_linux_version() < 2004000) |
64c4757e NB |
161 | old_linux = 1; |
162 | ||
163 | if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0) { | |
682c7051 | 164 | fprintf(stderr, Name ": device %s already active - cannot assemble it\n", |
64c4757e NB |
165 | mddev); |
166 | return 1; | |
167 | } | |
168 | ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */ | |
169 | ||
170 | /* | |
171 | * We have a valid mddev, check out uuid | |
172 | */ | |
173 | if (!uuidset && scan) { | |
174 | /* device must be listed with uuid in conf file */ | |
175 | mddev_uuid_t device_list; | |
176 | device_list = conf_get_uuids(conffile); | |
177 | while (device_list && | |
178 | strcmp(device_list->devname, mddev) != 0) | |
179 | device_list = device_list->next; | |
180 | ||
181 | if (!device_list) { | |
682c7051 | 182 | fprintf(stderr, Name ": --scan set and no uuid found for %s in config file.\n", |
64c4757e NB |
183 | mddev); |
184 | return 1; | |
185 | } | |
186 | /* the uuid is safe until next call to conf_get_uuids */ | |
187 | uuid = device_list->uuid; | |
188 | uuidset = 1; | |
189 | } | |
190 | ||
191 | /* Now to start looking at devices. | |
192 | * If no devices were given, but a uuid is available and | |
193 | * --scan was set, then we should scan all devices listed in the | |
194 | * config file | |
195 | * | |
196 | */ | |
197 | if (subdevs==0 && scan && uuidset) | |
198 | devlist = conf_get_devs(conffile); | |
199 | ||
200 | if (subdevs == 0 && devlist == NULL) { | |
682c7051 | 201 | fprintf(stderr, Name ": no devices given for %s\n", mddev); |
64c4757e NB |
202 | return 1; |
203 | } | |
204 | /* now for each device */ | |
205 | first_super.md_magic = 0; | |
206 | for (i=0; i<MD_SB_DISKS; i++) | |
207 | best[i] = -1; | |
208 | ||
209 | while (subdevs || devlist) { | |
210 | char *devname; | |
211 | int this_uuid[4]; | |
212 | int dfd; | |
213 | struct stat stb; | |
214 | int inargv; | |
215 | if (subdevs) { | |
216 | devname = *subdev++; | |
217 | subdevs--; | |
218 | inargv=1; | |
219 | } else { | |
220 | devname = devlist->devname; | |
221 | devlist = devlist->next; | |
222 | inargv=0; | |
223 | } | |
224 | ||
225 | dfd = open(devname, O_RDONLY, 0); | |
226 | if (dfd < 0) { | |
227 | if (inargv || verbose) | |
682c7051 | 228 | fprintf(stderr, Name ": cannot open device %s: %s\n", |
64c4757e NB |
229 | devname, strerror(errno)); |
230 | continue; | |
231 | } | |
232 | if (fstat(dfd, &stb)< 0) { | |
233 | /* Impossible! */ | |
682c7051 | 234 | fprintf(stderr, Name ": fstat failed for %s: %s\n", |
64c4757e NB |
235 | devname, strerror(errno)); |
236 | close(dfd); | |
237 | continue; | |
238 | } | |
239 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
682c7051 | 240 | fprintf(stderr, Name ": %d is not a block device.\n", |
64c4757e NB |
241 | devname); |
242 | close(dfd); | |
243 | continue; | |
244 | } | |
245 | if (load_super(dfd, &super)) { | |
246 | if (inargv || verbose) | |
682c7051 | 247 | fprintf( stderr, Name ": no RAID superblock on %s\n", |
64c4757e NB |
248 | devname); |
249 | close(dfd); | |
250 | continue; | |
251 | } | |
252 | close(dfd); | |
253 | if (compare_super(&first_super, &super)) { | |
254 | if (inargv || verbose) | |
682c7051 | 255 | fprintf(stderr, Name ": superblock on %s doesn't match\n", |
64c4757e NB |
256 | devname); |
257 | continue; | |
258 | } | |
259 | if (uuidset) { | |
260 | uuid_from_super(this_uuid, &first_super); | |
261 | if (!same_uuid(this_uuid, uuid)) { | |
262 | if (inargv || verbose) | |
682c7051 | 263 | fprintf(stderr, Name ": %s has wrong uuid.\n", |
64c4757e NB |
264 | devname); |
265 | continue; | |
266 | } | |
267 | } else { | |
268 | uuid_from_super(uuid, &first_super); | |
269 | uuidset = 1; | |
270 | } | |
271 | ||
272 | /* Ok, this one is at least worth considering */ | |
273 | if (devcnt >= MD_SB_DISKS) { | |
682c7051 | 274 | fprintf(stderr, Name ": ouch - too many devices appear to be in this array. Ignoring %s\n", |
64c4757e NB |
275 | devname); |
276 | continue; | |
277 | } | |
278 | devices[devcnt].devname = devname; | |
279 | devices[devcnt].major = MAJOR(stb.st_rdev); | |
280 | devices[devcnt].minor = MINOR(stb.st_rdev); | |
281 | devices[devcnt].events = md_event(&super); | |
282 | devices[devcnt].utime = super.utime; | |
283 | devices[devcnt].uptodate = 0; | |
284 | if (most_recent < devcnt) { | |
285 | if (devices[devcnt].events | |
286 | > devices[most_recent].events) | |
287 | most_recent = devcnt; | |
288 | } | |
289 | i = super.this_disk.raid_disk; | |
290 | if (best[i] == -1 | |
291 | || devices[best[i]].events < devices[devcnt].events) { | |
292 | best[i] = devcnt; | |
293 | } | |
294 | devcnt++; | |
295 | } | |
296 | ||
297 | if (devcnt == 0) { | |
682c7051 | 298 | fprintf(stderr, Name ": no devices found for %s\n", |
64c4757e NB |
299 | mddev); |
300 | return 1; | |
301 | } | |
302 | /* now we have some devices that might be suitable. | |
303 | * I wonder how many | |
304 | */ | |
305 | okcnt = 0; | |
306 | for (i=0; i< first_super.raid_disks;i++) { | |
307 | int j = best[i]; | |
308 | if (j < 0) continue; | |
309 | if (devices[j].events+1 >= | |
310 | devices[most_recent].events) { | |
311 | devices[j].uptodate = 1; | |
312 | okcnt++; | |
313 | } | |
314 | } | |
315 | while (force && !enough(first_super.level, first_super.raid_disks, okcnt)) { | |
316 | /* Choose the newest best drive which is | |
317 | * not up-to-date, update the superblock | |
318 | * and add it. | |
319 | */ | |
682c7051 | 320 | fprintf(stderr,"NotImplementedYet\n"); |
64c4757e NB |
321 | /* FIXME */ |
322 | exit(2); | |
323 | } | |
324 | /* Almost ready to actually *do* something */ | |
325 | if (!old_linux) { | |
326 | if (ioctl(mdfd, SET_ARRAY_INFO, NULL) != 0) { | |
682c7051 | 327 | fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n", |
64c4757e NB |
328 | mddev, strerror(errno)); |
329 | return 1; | |
330 | } | |
331 | /* First, add the raid disks */ | |
332 | for (i=0; i<first_super.raid_disks; i++) { | |
333 | int j = best[i]; | |
334 | if (devices[j].uptodate) { | |
335 | mdu_disk_info_t disk; | |
336 | memset(&disk, 0, sizeof(disk)); | |
337 | disk.major = devices[j].major; | |
338 | disk.minor = devices[j].minor; | |
339 | if (ioctl(mdfd, ADD_NEW_DISK, &disk)!=0) { | |
682c7051 | 340 | fprintf(stderr, Name ": failed to add %s to %s: %s\n", |
64c4757e NB |
341 | devices[j].devname, |
342 | mddev, | |
343 | strerror(errno)); | |
344 | } else | |
345 | okcnt--; | |
346 | } else if (verbose) | |
682c7051 | 347 | fprintf(stderr, Name ": no uptodate device for slot %d of %s\n", |
64c4757e NB |
348 | i, mddev); |
349 | } | |
350 | if (runstop == 1 || | |
351 | (runstop == 0 && | |
352 | enough(first_super.level, first_super.raid_disks, okcnt))) { | |
353 | if (ioctl(mdfd, RUN_ARRAY, NULL)==0) | |
354 | return 0; | |
682c7051 | 355 | fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n", |
64c4757e NB |
356 | mddev, strerror(errno)); |
357 | return 1; | |
358 | } | |
359 | if (runstop == -1) | |
360 | return 0; | |
361 | else return 1; | |
362 | } else { | |
363 | /* FIXME */ | |
364 | return 1; | |
365 | } | |
366 | } |