]>
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) { | |
82b27616 | 110 | fputs(Name ": internal error - Assemble called with no device 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 | } | |
82b27616 | 121 | for (; device_list; device_list=device_list->next) { |
64c4757e NB |
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 | } | |
64c4757e NB |
139 | } |
140 | if (found) | |
141 | return 0; | |
82b27616 | 142 | fprintf(stderr,Name ": Did not successfully Assemble any devices\n"); |
64c4757e NB |
143 | return 1; |
144 | } | |
145 | ||
146 | /* | |
147 | * Ok, we have an mddev, check it out | |
148 | */ | |
149 | vers = md_get_version(mdfd); | |
150 | if (vers <= 0) { | |
682c7051 | 151 | fprintf(stderr, Name ": %s appears not to be an md device.\n"); |
64c4757e NB |
152 | return 1; |
153 | } | |
682c7051 NB |
154 | if (vers < 9000) { |
155 | fprintf(stderr, Name ": Assemble requires driver version 0.90.0 or later.\n" | |
64c4757e NB |
156 | " Upgrade your kernel or try --Build\n"); |
157 | return 1; | |
158 | } | |
682c7051 | 159 | if (get_linux_version() < 2004000) |
64c4757e NB |
160 | old_linux = 1; |
161 | ||
162 | if (ioctl(mdfd, GET_ARRAY_INFO, &array)>=0) { | |
682c7051 | 163 | fprintf(stderr, Name ": device %s already active - cannot assemble it\n", |
64c4757e NB |
164 | mddev); |
165 | return 1; | |
166 | } | |
167 | ioctl(mdfd, STOP_ARRAY, NULL); /* just incase it was started but has no content */ | |
168 | ||
169 | /* | |
170 | * We have a valid mddev, check out uuid | |
171 | */ | |
172 | if (!uuidset && scan) { | |
173 | /* device must be listed with uuid in conf file */ | |
174 | mddev_uuid_t device_list; | |
175 | device_list = conf_get_uuids(conffile); | |
176 | while (device_list && | |
177 | strcmp(device_list->devname, mddev) != 0) | |
178 | device_list = device_list->next; | |
179 | ||
180 | if (!device_list) { | |
682c7051 | 181 | fprintf(stderr, Name ": --scan set and no uuid found for %s in config file.\n", |
64c4757e NB |
182 | mddev); |
183 | return 1; | |
184 | } | |
185 | /* the uuid is safe until next call to conf_get_uuids */ | |
186 | uuid = device_list->uuid; | |
187 | uuidset = 1; | |
188 | } | |
189 | ||
190 | /* Now to start looking at devices. | |
191 | * If no devices were given, but a uuid is available and | |
192 | * --scan was set, then we should scan all devices listed in the | |
193 | * config file | |
194 | * | |
195 | */ | |
196 | if (subdevs==0 && scan && uuidset) | |
197 | devlist = conf_get_devs(conffile); | |
198 | ||
199 | if (subdevs == 0 && devlist == NULL) { | |
682c7051 | 200 | fprintf(stderr, Name ": no devices given for %s\n", mddev); |
64c4757e NB |
201 | return 1; |
202 | } | |
203 | /* now for each device */ | |
204 | first_super.md_magic = 0; | |
205 | for (i=0; i<MD_SB_DISKS; i++) | |
206 | best[i] = -1; | |
207 | ||
82b27616 NB |
208 | if (verbose) |
209 | fprintf(stderr, Name ": looking for devices for %s\n", | |
210 | mddev); | |
211 | ||
64c4757e NB |
212 | while (subdevs || devlist) { |
213 | char *devname; | |
214 | int this_uuid[4]; | |
215 | int dfd; | |
216 | struct stat stb; | |
217 | int inargv; | |
218 | if (subdevs) { | |
219 | devname = *subdev++; | |
220 | subdevs--; | |
221 | inargv=1; | |
222 | } else { | |
223 | devname = devlist->devname; | |
224 | devlist = devlist->next; | |
225 | inargv=0; | |
226 | } | |
227 | ||
228 | dfd = open(devname, O_RDONLY, 0); | |
229 | if (dfd < 0) { | |
230 | if (inargv || verbose) | |
682c7051 | 231 | fprintf(stderr, Name ": cannot open device %s: %s\n", |
64c4757e NB |
232 | devname, strerror(errno)); |
233 | continue; | |
234 | } | |
235 | if (fstat(dfd, &stb)< 0) { | |
236 | /* Impossible! */ | |
682c7051 | 237 | fprintf(stderr, Name ": fstat failed for %s: %s\n", |
64c4757e NB |
238 | devname, strerror(errno)); |
239 | close(dfd); | |
240 | continue; | |
241 | } | |
242 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
682c7051 | 243 | fprintf(stderr, Name ": %d is not a block device.\n", |
64c4757e NB |
244 | devname); |
245 | close(dfd); | |
246 | continue; | |
247 | } | |
248 | if (load_super(dfd, &super)) { | |
249 | if (inargv || verbose) | |
682c7051 | 250 | fprintf( stderr, Name ": no RAID superblock on %s\n", |
64c4757e NB |
251 | devname); |
252 | close(dfd); | |
253 | continue; | |
254 | } | |
255 | close(dfd); | |
82b27616 NB |
256 | uuid_from_super(this_uuid, &super); |
257 | if (uuidset && !same_uuid(this_uuid, uuid)) { | |
258 | if (inargv || verbose) | |
259 | fprintf(stderr, Name ": %s has wrong uuid.\n", | |
260 | devname); | |
261 | continue; | |
262 | } | |
64c4757e NB |
263 | if (compare_super(&first_super, &super)) { |
264 | if (inargv || verbose) | |
682c7051 | 265 | fprintf(stderr, Name ": superblock on %s doesn't match\n", |
64c4757e NB |
266 | devname); |
267 | continue; | |
268 | } | |
269 | if (uuidset) { | |
270 | uuid_from_super(this_uuid, &first_super); | |
271 | if (!same_uuid(this_uuid, uuid)) { | |
272 | if (inargv || verbose) | |
682c7051 | 273 | fprintf(stderr, Name ": %s has wrong uuid.\n", |
64c4757e NB |
274 | devname); |
275 | continue; | |
276 | } | |
277 | } else { | |
278 | uuid_from_super(uuid, &first_super); | |
279 | uuidset = 1; | |
280 | } | |
281 | ||
282 | /* Ok, this one is at least worth considering */ | |
283 | if (devcnt >= MD_SB_DISKS) { | |
682c7051 | 284 | fprintf(stderr, Name ": ouch - too many devices appear to be in this array. Ignoring %s\n", |
64c4757e NB |
285 | devname); |
286 | continue; | |
287 | } | |
288 | devices[devcnt].devname = devname; | |
289 | devices[devcnt].major = MAJOR(stb.st_rdev); | |
290 | devices[devcnt].minor = MINOR(stb.st_rdev); | |
291 | devices[devcnt].events = md_event(&super); | |
292 | devices[devcnt].utime = super.utime; | |
293 | devices[devcnt].uptodate = 0; | |
294 | if (most_recent < devcnt) { | |
295 | if (devices[devcnt].events | |
296 | > devices[most_recent].events) | |
297 | most_recent = devcnt; | |
298 | } | |
299 | i = super.this_disk.raid_disk; | |
300 | if (best[i] == -1 | |
301 | || devices[best[i]].events < devices[devcnt].events) { | |
302 | best[i] = devcnt; | |
303 | } | |
304 | devcnt++; | |
305 | } | |
306 | ||
307 | if (devcnt == 0) { | |
682c7051 | 308 | fprintf(stderr, Name ": no devices found for %s\n", |
64c4757e NB |
309 | mddev); |
310 | return 1; | |
311 | } | |
312 | /* now we have some devices that might be suitable. | |
313 | * I wonder how many | |
314 | */ | |
315 | okcnt = 0; | |
316 | for (i=0; i< first_super.raid_disks;i++) { | |
317 | int j = best[i]; | |
318 | if (j < 0) continue; | |
319 | if (devices[j].events+1 >= | |
320 | devices[most_recent].events) { | |
321 | devices[j].uptodate = 1; | |
322 | okcnt++; | |
323 | } | |
324 | } | |
325 | while (force && !enough(first_super.level, first_super.raid_disks, okcnt)) { | |
326 | /* Choose the newest best drive which is | |
327 | * not up-to-date, update the superblock | |
328 | * and add it. | |
329 | */ | |
682c7051 | 330 | fprintf(stderr,"NotImplementedYet\n"); |
64c4757e NB |
331 | /* FIXME */ |
332 | exit(2); | |
333 | } | |
334 | /* Almost ready to actually *do* something */ | |
335 | if (!old_linux) { | |
336 | if (ioctl(mdfd, SET_ARRAY_INFO, NULL) != 0) { | |
682c7051 | 337 | fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n", |
64c4757e NB |
338 | mddev, strerror(errno)); |
339 | return 1; | |
340 | } | |
341 | /* First, add the raid disks */ | |
342 | for (i=0; i<first_super.raid_disks; i++) { | |
343 | int j = best[i]; | |
0db17fcb | 344 | if (j >= 0 && devices[j].uptodate) { |
64c4757e NB |
345 | mdu_disk_info_t disk; |
346 | memset(&disk, 0, sizeof(disk)); | |
347 | disk.major = devices[j].major; | |
348 | disk.minor = devices[j].minor; | |
349 | if (ioctl(mdfd, ADD_NEW_DISK, &disk)!=0) { | |
682c7051 | 350 | fprintf(stderr, Name ": failed to add %s to %s: %s\n", |
64c4757e NB |
351 | devices[j].devname, |
352 | mddev, | |
353 | strerror(errno)); | |
64c4757e | 354 | okcnt--; |
82b27616 | 355 | } |
64c4757e | 356 | } else if (verbose) |
682c7051 | 357 | fprintf(stderr, Name ": no uptodate device for slot %d of %s\n", |
64c4757e NB |
358 | i, mddev); |
359 | } | |
360 | if (runstop == 1 || | |
361 | (runstop == 0 && | |
362 | enough(first_super.level, first_super.raid_disks, okcnt))) { | |
82b27616 NB |
363 | if (ioctl(mdfd, RUN_ARRAY, NULL)==0) { |
364 | fprintf(stderr, Name ": %s has been started with %d drives\n", | |
365 | mddev, okcnt); | |
64c4757e | 366 | return 0; |
82b27616 | 367 | } |
682c7051 | 368 | fprintf(stderr, Name ": failed to RUN_ARRAY %s: %s\n", |
64c4757e NB |
369 | mddev, strerror(errno)); |
370 | return 1; | |
371 | } | |
82b27616 NB |
372 | if (runstop == -1) { |
373 | fprintf(stderr, Name ": %s assembled from %d drives, but not started.\n", | |
374 | mddev, okcnt); | |
64c4757e | 375 | return 0; |
82b27616 NB |
376 | } |
377 | fprintf(stderr, Name ": %s assembled from %d drives - not enough to start it.\n", | |
378 | mddev, okcnt); | |
64c4757e | 379 | return 1; |
82b27616 NB |
380 | } else { |
381 | /* It maybe just a case of calling START_ARRAY, but it may not.. | |
382 | * We need to pick a working device, read it's super block, and make | |
383 | * sure all the device numbers and the minor number are right. | |
384 | * Then we might need to re-write the super block. | |
385 | * THEN we call START_ARRAY | |
386 | * If the md_minor is wrong, wejust give up for now. The alternate is to | |
387 | * re-write ALL super blocks. | |
388 | */ | |
389 | int chosen_drive = -1; | |
390 | int change = 0; | |
391 | int dev; | |
392 | for (i=0; i<first_super.nr_disks; i++) { | |
393 | if (!devices[i].uptodate) | |
394 | continue; | |
395 | if (chosen_drive == -1) { | |
396 | int fd; | |
397 | chosen_drive = i; | |
398 | if (open(devices[i].devname, O_RDONLY)>= 0) { | |
399 | fprintf(stderr, Name ": Cannot open %s: %s\n", | |
400 | devices[i].devname, strerror(errno)); | |
401 | return 1; | |
402 | } | |
403 | if (load_super(fd, &super)) { | |
404 | close(fd); | |
405 | fprintf(stderr, Name ": RAID superblock has disappeared from %s\n", | |
406 | devices[i].devname); | |
407 | return 1; | |
408 | } | |
409 | close(fd); | |
410 | } | |
411 | if (devices[i].major != super.disks[i].major || | |
412 | devices[i].minor != super.disks[i].minor) { | |
413 | change = 1; | |
414 | super.disks[i].major = devices[i].major; | |
415 | super.disks[i].minor = devices[i].minor; | |
416 | } | |
417 | } | |
418 | if (change) { | |
419 | int fd; | |
420 | super.sb_csum = calc_sb_csum(super); | |
421 | fd = open(devices[chosen_drive].devname, O_RDWR); | |
422 | if (fd < 0) { | |
423 | fprintf(stderr, Name ": Could open %s for write - cannot Assemble array.\n", | |
424 | devices[chosen_drive].devname); | |
425 | return 1; | |
426 | } | |
427 | if (store_super(fd, &super)) { | |
428 | close(fd); | |
429 | fprintf(stderr, Name ": Could not re-write superblock on %s\n", | |
430 | devices[chosen_drive].devname); | |
431 | return 1; | |
432 | } | |
433 | close(fd); | |
434 | } | |
435 | dev = MKDEV(devices[chosen_drive].major, | |
436 | devices[chosen_drive].minor); | |
437 | if (ioctl(mdfd, START_ARRAY, dev)) { | |
438 | fprintf(stderr, Name ": Cannot start array: %s\n", | |
439 | strerror(errno)); | |
440 | } | |
441 | ||
64c4757e NB |
442 | } |
443 | } |