]>
Commit | Line | Data |
---|---|---|
8382f19b NB |
1 | /* |
2 | * Incremental.c - support --incremental. Part of: | |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
4 | * | |
5 | * Copyright (C) 2006 Neil Brown <neilb@suse.de> | |
6 | * | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | * Author: Neil Brown | |
23 | * Email: <neilb@suse.de> | |
24 | * Paper: Neil Brown | |
25 | * Novell Inc | |
26 | * GPO Box Q1283 | |
27 | * QVB Post Office, NSW 1230 | |
28 | * Australia | |
29 | */ | |
30 | ||
31 | #include "mdadm.h" | |
32 | ||
33 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
34 | struct mdinfo *info); | |
35 | static void find_reject(int mdfd, struct supertype *st, struct sysarray *sra, | |
36 | int number, __u64 events, int verbose, | |
37 | char *array_name); | |
38 | ||
39 | int Incremental(char *devname, int verbose, int runstop, | |
40 | struct supertype *st, char *homehost, int autof) | |
41 | { | |
42 | /* Add this device to an array, creating the array if necessary | |
43 | * and starting the array if sensibe or - if runstop>0 - if possible. | |
44 | * | |
45 | * This has several steps: | |
46 | * | |
47 | * 1/ Check if device is permitted by mdadm.conf, reject if not. | |
48 | * 2/ Find metadata, reject if none appropriate (check | |
49 | * version/name from args) | |
50 | * 3/ Check if there is a match in mdadm.conf | |
51 | * 3a/ if not, check for homehost match. If no match, reject. | |
52 | * 4/ Determine device number. | |
53 | * - If in mdadm.conf with std name, use that | |
54 | * - UUID in /var/run/mdadm.map use that | |
55 | * - If name is suggestive, use that. unless in use with different uuid. | |
56 | * - Choose a free, high number. | |
57 | * - Use a partitioned device unless strong suggestion not to. | |
58 | * e.g. auto=md | |
59 | * 5/ Find out if array already exists | |
60 | * 5a/ if it does not | |
61 | * - choose a name, from mdadm.conf or 'name' field in array. | |
62 | * - create the array | |
63 | * - add the device | |
64 | * 5b/ if it does | |
65 | * - check one drive in array to make sure metadata is a reasonably | |
66 | * close match. Reject if not (e.g. different type) | |
67 | * - add the device | |
68 | * 6/ Make sure /var/run/mdadm.map contains this array. | |
69 | * 7/ Is there enough devices to possibly start the array? | |
70 | * 7a/ if not, finish with success. | |
71 | * 7b/ if yes, | |
72 | * - read all metadata and arrange devices like -A does | |
73 | * - if number of OK devices match expected, or -R and there are enough, | |
74 | * start the array (auto-readonly). | |
75 | */ | |
76 | struct stat stb; | |
77 | void *super, *super2; | |
78 | struct mdinfo info, info2; | |
79 | struct mddev_ident_s *array_list, *match; | |
80 | char chosen_name[1024]; | |
81 | int rv; | |
82 | int devnum; | |
83 | struct map_ent *mp, *map = NULL; | |
84 | int dfd, mdfd; | |
85 | char *avail; | |
86 | int active_disks; | |
87 | ||
88 | ||
89 | struct createinfo *ci = conf_get_create_info(); | |
90 | ||
91 | if (autof == 0) | |
92 | autof = ci->autof; | |
93 | ||
94 | /* 1/ Check if devices is permitted by mdadm.conf */ | |
95 | ||
96 | if (!conf_test_dev(devname)) { | |
97 | if (verbose >= 0) | |
98 | fprintf(stderr, Name | |
99 | ": %s not permitted by mdadm.conf.\n", | |
100 | devname); | |
101 | return 1; | |
102 | } | |
103 | ||
104 | /* 2/ Find metadata, reject if none appropriate (check | |
105 | * version/name from args) */ | |
106 | ||
107 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
108 | if (dfd < 0) { | |
109 | if (verbose >= 0) | |
110 | fprintf(stderr, Name ": cannot open %s: %s.\n", | |
111 | devname, strerror(errno)); | |
112 | return 1; | |
113 | } | |
114 | if (fstat(dfd, &stb) < 0) { | |
115 | if (verbose >= 0) | |
116 | fprintf(stderr, Name ": fstat failed for %s: %s.\n", | |
117 | devname, strerror(errno)); | |
118 | close(dfd); | |
119 | return 1; | |
120 | } | |
121 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
122 | if (verbose >= 0) | |
123 | fprintf(stderr, Name ": %s is not a block device.\n", | |
124 | devname); | |
125 | close(dfd); | |
126 | return 1; | |
127 | } | |
128 | ||
129 | if (st == NULL && (st = guess_super(dfd)) == NULL) { | |
130 | if (verbose >= 0) | |
131 | fprintf(stderr, Name | |
132 | ": no recognisable superblock on %s.\n", | |
133 | devname); | |
134 | close(dfd); | |
135 | return 1; | |
136 | } | |
137 | if (st->ss->load_super(st, dfd, &super, NULL)) { | |
138 | if (verbose >= 0) | |
139 | fprintf(stderr, Name ": no RAID superblock on %s.\n", | |
140 | devname); | |
141 | close(dfd); | |
142 | return 1; | |
143 | } | |
144 | st->ss->getinfo_super(&info, super); | |
145 | close (dfd); | |
146 | ||
147 | /* 3/ Check if there is a match in mdadm.conf */ | |
148 | ||
149 | array_list = conf_get_ident(NULL); | |
150 | match = NULL; | |
151 | for (; array_list; array_list = array_list->next) { | |
152 | if (array_list->uuid_set && | |
153 | same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid) | |
154 | == 0) { | |
155 | if (verbose >= 2) | |
156 | fprintf(stderr, Name | |
157 | ": UUID differs from %s.\n", | |
158 | array_list->devname); | |
159 | continue; | |
160 | } | |
161 | if (array_list->name[0] && | |
162 | strcasecmp(array_list->name, info.name) != 0) { | |
163 | if (verbose >= 2) | |
164 | fprintf(stderr, Name | |
165 | ": Name differs from %s.\n", | |
166 | array_list->devname); | |
167 | continue; | |
168 | } | |
169 | if (array_list->devices && | |
170 | !match_oneof(array_list->devices, devname)) { | |
171 | if (verbose >= 2) | |
172 | fprintf(stderr, Name | |
173 | ": Not a listed device for %s.\n", | |
174 | array_list->devname); | |
175 | continue; | |
176 | } | |
177 | if (array_list->super_minor != UnSet && | |
178 | array_list->super_minor != info.array.md_minor) { | |
179 | if (verbose >= 2) | |
180 | fprintf(stderr, Name | |
181 | ": Different super-minor to %s.\n", | |
182 | array_list->devname); | |
183 | continue; | |
184 | } | |
185 | if (!array_list->uuid_set && | |
186 | !array_list->name[0] && | |
187 | !array_list->devices && | |
188 | array_list->super_minor == UnSet) { | |
189 | if (verbose >= 2) | |
190 | fprintf(stderr, Name | |
191 | ": %s doesn't have any identifying information.\n", | |
192 | array_list->devname); | |
193 | continue; | |
194 | } | |
195 | /* FIXME, should I check raid_disks and level too?? */ | |
196 | ||
197 | if (match) { | |
198 | if (verbose >= 0) | |
199 | fprintf(stderr, Name | |
200 | ": we match both %s and %s - cannot decide which to use.\n", | |
201 | match->devname, array_list->devname); | |
202 | return 2; | |
203 | } | |
204 | match = array_list; | |
205 | } | |
206 | ||
207 | /* 3a/ if not, check for homehost match. If no match, reject. */ | |
208 | if (!match) { | |
209 | if (homehost == NULL || | |
210 | st->ss->match_home(super, homehost) == 0) { | |
211 | if (verbose >= 0) | |
212 | fprintf(stderr, Name | |
213 | ": not found in mdadm.conf and not identified by homehost.\n"); | |
214 | return 2; | |
215 | } | |
216 | } | |
217 | /* 4/ Determine device number. */ | |
218 | /* - If in mdadm.conf with std name, use that */ | |
219 | /* - UUID in /var/run/mdadm.map use that */ | |
220 | /* - If name is suggestive, use that. unless in use with */ | |
221 | /* different uuid. */ | |
222 | /* - Choose a free, high number. */ | |
223 | /* - Use a partitioned device unless strong suggestion not to. */ | |
224 | /* e.g. auto=md */ | |
225 | if (match && is_standard(match->devname, &devnum)) | |
226 | /* We have devnum now */; | |
227 | else if ((mp = map_by_uuid(&map, info.uuid)) != NULL) | |
228 | devnum = mp->devnum; | |
229 | else { | |
230 | /* Have to guess a bit. */ | |
231 | int use_partitions = 1; | |
232 | char *np, *ep; | |
233 | if ((autof&7) == 3 || (autof&7) == 5) | |
234 | use_partitions = 0; | |
235 | np = strchr(info.name, ':'); | |
236 | if (np) | |
237 | np++; | |
238 | else | |
239 | np = info.name; | |
240 | devnum = strtoul(np, &ep, 10); | |
241 | if (ep > np && *ep == 0) { | |
242 | /* This is a number. Let check that it is unused. */ | |
243 | if (mddev_busy(use_partitions ? (-1-devnum) : devnum)) | |
244 | devnum = -1; | |
245 | } else | |
246 | devnum = -1; | |
247 | ||
248 | if (devnum < 0) { | |
249 | /* Haven't found anything yet, choose something free */ | |
250 | /* There is similar code in mdopen.c - should unify */ | |
251 | for (devnum = 127 ; devnum != 128 ; | |
252 | devnum = devnum ? devnum-1 : (1<<22)-1) { | |
253 | if (mddev_busy(use_partitions ? | |
254 | (-1-devnum) : devnum)) | |
255 | break; | |
256 | } | |
257 | if (devnum == 128) { | |
258 | fprintf(stderr, Name | |
259 | ": No spare md devices!!\n"); | |
260 | return 2; | |
261 | } | |
262 | } | |
263 | devnum = use_partitions ? (-1-devnum) : devnum; | |
264 | } | |
265 | mdfd = open_mddev_devnum(match ? match->devname : NULL, | |
266 | devnum, | |
267 | info.name, | |
268 | chosen_name); | |
269 | if (mdfd < 0) { | |
270 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
271 | chosen_name, strerror(errno)); | |
272 | return 2; | |
273 | } | |
274 | /* 5/ Find out if array already exists */ | |
275 | if (! mddev_busy(devnum)) { | |
276 | /* 5a/ if it does not */ | |
277 | /* - choose a name, from mdadm.conf or 'name' field in array. */ | |
278 | /* - create the array */ | |
279 | /* - add the device */ | |
280 | mdu_array_info_t ainf; | |
281 | mdu_disk_info_t disk; | |
282 | char md[20]; | |
283 | struct sysarray *sra; | |
284 | ||
285 | memset(&ainf, 0, sizeof(ainf)); | |
286 | ainf.major_version = st->ss->major; | |
287 | ainf.minor_version = st->minor_version; | |
288 | if (ioctl(mdfd, SET_ARRAY_INFO, &ainf) != 0) { | |
289 | fprintf(stderr, Name | |
290 | ": SET_ARRAY_INFO failed for %s: %s\b", | |
291 | chosen_name, strerror(errno)); | |
292 | close(mdfd); | |
293 | return 2; | |
294 | } | |
295 | sprintf(md, "%d.%d\n", st->ss->major, st->minor_version); | |
296 | sra = sysfs_read(mdfd, devnum, GET_VERSION); | |
297 | sysfs_set_str(sra, NULL, "metadata_version", md); | |
298 | memset(&disk, 0, sizeof(disk)); | |
299 | disk.major = major(stb.st_rdev); | |
300 | disk.minor = minor(stb.st_rdev); | |
301 | sysfs_free(sra); | |
302 | if (ioctl(mdfd, ADD_NEW_DISK, &disk) != 0) { | |
303 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
304 | devname, chosen_name, strerror(errno)); | |
305 | ioctl(mdfd, STOP_ARRAY, 0); | |
306 | close(mdfd); | |
307 | return 2; | |
308 | } | |
309 | sra = sysfs_read(mdfd, devnum, GET_DEVS); | |
310 | if (!sra || !sra->devs || sra->devs->role >= 0) { | |
311 | /* It really should be 'none' - must be old buggy | |
312 | * kernel, and mdadm -I may not be able to complete. | |
313 | * So reject it. | |
314 | */ | |
315 | ioctl(mdfd, STOP_ARRAY, NULL); | |
316 | fprintf(stderr, Name | |
317 | ": You have an old buggy kernel which cannot support\n" | |
318 | " --incremental reliably. Aborting.\n"); | |
319 | close(mdfd); | |
320 | sysfs_free(sra); | |
321 | return 2; | |
322 | } | |
323 | } else { | |
324 | /* 5b/ if it does */ | |
325 | /* - check one drive in array to make sure metadata is a reasonably */ | |
326 | /* close match. Reject if not (e.g. different type) */ | |
327 | /* - add the device */ | |
328 | char dn[20]; | |
329 | int dfd2; | |
330 | mdu_disk_info_t disk; | |
331 | int err; | |
332 | struct sysarray *sra; | |
333 | sra = sysfs_read(mdfd, devnum, (GET_VERSION | GET_DEVS | | |
334 | GET_STATE)); | |
335 | if (sra->major_version != st->ss->major || | |
336 | sra->minor_version != st->minor_version) { | |
337 | if (verbose >= 0) | |
338 | fprintf(stderr, Name | |
339 | ": %s has different metadata to chosen array %s %d.%d %d.%d.\n", | |
340 | devname, chosen_name, | |
341 | sra->major_version, sra->minor_version, | |
342 | st->ss->major, st->minor_version); | |
343 | close(mdfd); | |
344 | return 1; | |
345 | } | |
346 | sprintf(dn, "%d:%d", sra->devs->major, sra->devs->minor); | |
347 | dfd2 = dev_open(dn, O_RDONLY); | |
348 | if (st->ss->load_super(st, dfd2,&super2, NULL)) { | |
349 | fprintf(stderr, Name | |
350 | ": Strange error loading metadata for %s.\n", | |
351 | chosen_name); | |
352 | close(mdfd); | |
353 | close(dfd2); | |
354 | return 2; | |
355 | } | |
356 | close(dfd2); | |
357 | st->ss->getinfo_super(&info2, super2); | |
358 | if (info.array.level != info2.array.level || | |
359 | memcmp(info.uuid, info2.uuid, 16) != 0 || | |
360 | info.array.raid_disks != info2.array.raid_disks) { | |
361 | fprintf(stderr, Name | |
362 | ": unexpected difference between %s and %s.\n", | |
363 | chosen_name, devname); | |
364 | close(mdfd); | |
365 | return 2; | |
366 | } | |
367 | memset(&disk, 0, sizeof(disk)); | |
368 | disk.major = major(stb.st_rdev); | |
369 | disk.minor = minor(stb.st_rdev); | |
370 | err = ioctl(mdfd, ADD_NEW_DISK, &disk); | |
371 | if (err < 0 && errno == EBUSY) { | |
372 | /* could be another device present with the same | |
373 | * disk.number. Find and reject any such | |
374 | */ | |
375 | find_reject(mdfd, st, sra, info.disk.number, | |
376 | info.events, verbose, chosen_name); | |
377 | err = ioctl(mdfd, ADD_NEW_DISK, &disk); | |
378 | } | |
379 | if (err < 0) { | |
380 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
381 | devname, chosen_name, strerror(errno)); | |
382 | close(mdfd); | |
383 | return 2; | |
384 | } | |
385 | } | |
386 | /* 6/ Make sure /var/run/mdadm.map contains this array. */ | |
387 | map_update(&map, devnum, | |
388 | info.array.major_version, | |
389 | info.array.minor_version, | |
390 | info.uuid, chosen_name); | |
391 | ||
392 | /* 7/ Is there enough devices to possibly start the array? */ | |
393 | /* 7a/ if not, finish with success. */ | |
394 | active_disks = count_active(st, mdfd, &avail, &info); | |
395 | if (enough(info.array.level, info.array.raid_disks, | |
396 | info.array.layout, info.array.state & 1, | |
397 | avail, active_disks) == 0) { | |
398 | free(avail); | |
399 | if (verbose >= 0) | |
400 | fprintf(stderr, Name | |
401 | ": %s attached to %s, not enough to start (%d).\n", | |
402 | devname, chosen_name, active_disks); | |
403 | close(mdfd); | |
404 | return 0; | |
405 | } | |
406 | free(avail); | |
407 | ||
408 | /* 7b/ if yes, */ | |
409 | /* - if number of OK devices match expected, or -R and there */ | |
410 | /* are enough, */ | |
411 | /* + add any bitmap file */ | |
412 | /* + start the array (auto-readonly). */ | |
413 | { | |
414 | mdu_array_info_t ainf; | |
415 | ||
416 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
417 | if (verbose >= 0) | |
418 | fprintf(stderr, Name | |
419 | ": %s attached to %s which is already active.\n", | |
420 | devname, chosen_name); | |
421 | close (mdfd); | |
422 | return 0; | |
423 | } | |
424 | } | |
425 | if (runstop > 0 || active_disks >= info.array.working_disks) { | |
426 | struct sysarray *sra; | |
427 | /* Let's try to start it */ | |
428 | if (match && match->bitmap_file) { | |
429 | int bmfd = open(match->bitmap_file, O_RDWR); | |
430 | if (bmfd < 0) { | |
431 | fprintf(stderr, Name | |
432 | ": Could not open bitmap file %s.\n", | |
433 | match->bitmap_file); | |
434 | close(mdfd); | |
435 | return 1; | |
436 | } | |
437 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
438 | close(bmfd); | |
439 | fprintf(stderr, Name | |
440 | ": Failed to set bitmapfile for %s.\n", | |
441 | chosen_name); | |
442 | close(mdfd); | |
443 | return 1; | |
444 | } | |
445 | close(bmfd); | |
446 | } | |
447 | sra = sysfs_read(mdfd, devnum, 0); | |
448 | if (sra == NULL || active_disks >= info.array.working_disks) | |
449 | rv = ioctl(mdfd, RUN_ARRAY, NULL); | |
450 | else | |
451 | rv = sysfs_set_str(sra, NULL, | |
452 | "array_state", "read-auto"); | |
453 | if (rv == 0) { | |
454 | if (verbose >= 0) | |
455 | fprintf(stderr, Name | |
456 | ": %s attached to %s, which has been started.\n", | |
457 | devname, chosen_name); | |
458 | rv = 0; | |
459 | } else { | |
460 | fprintf(stderr, Name | |
461 | ": %s attached to %s, but failed to start: %s.\n", | |
462 | devname, chosen_name, strerror(errno)); | |
463 | rv = 1; | |
464 | } | |
465 | } else { | |
466 | if (verbose >= 0) | |
467 | fprintf(stderr, Name | |
468 | ": %s attached to %s, not enough to start safely.\n", | |
469 | devname, chosen_name); | |
470 | rv = 0; | |
471 | } | |
472 | close(mdfd); | |
473 | return rv; | |
474 | } | |
475 | ||
476 | static void find_reject(int mdfd, struct supertype *st, struct sysarray *sra, | |
477 | int number, __u64 events, int verbose, | |
478 | char *array_name) | |
479 | { | |
480 | /* Find an device attached to this array with a disk.number of number | |
481 | * and events less than the passed events, and remove the device. | |
482 | */ | |
483 | struct sysdev *d; | |
484 | mdu_array_info_t ra; | |
485 | ||
486 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
487 | return; /* not safe to remove from active arrays | |
488 | * without thinking more */ | |
489 | ||
490 | for (d = sra->devs; d ; d = d->next) { | |
491 | char dn[10]; | |
492 | int dfd; | |
493 | void *super; | |
494 | struct mdinfo info; | |
495 | sprintf(dn, "%d:%d", d->major, d->minor); | |
496 | dfd = dev_open(dn, O_RDONLY); | |
497 | if (dfd < 0) | |
498 | continue; | |
499 | if (st->ss->load_super(st, dfd, &super, NULL)) { | |
500 | close(dfd); | |
501 | continue; | |
502 | } | |
503 | st->ss->getinfo_super(&info, super); | |
504 | free(super); | |
505 | close(dfd); | |
506 | ||
507 | if (info.disk.number != number || | |
508 | info.events >= events) | |
509 | continue; | |
510 | ||
511 | if (d->role > -1) | |
512 | sysfs_set_str(sra, d, "slot", "none"); | |
513 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
514 | if (verbose >= 0) | |
515 | fprintf(stderr, Name | |
516 | ": removing old device %s from %s\n", | |
517 | d->name+4, array_name); | |
518 | } | |
519 | } | |
520 | ||
521 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
522 | struct mdinfo *bestinfo) | |
523 | { | |
524 | /* count how many devices in sra think they are active */ | |
525 | struct sysdev *d; | |
526 | int cnt = 0, cnt1 = 0; | |
527 | __u64 max_events = 0; | |
528 | void *best_super = NULL; | |
529 | struct sysarray *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); | |
530 | char *avail = NULL; | |
531 | ||
532 | for (d = sra->devs ; d ; d = d->next) { | |
533 | char dn[30]; | |
534 | int dfd; | |
535 | void *super; | |
536 | int ok; | |
537 | struct mdinfo info; | |
538 | ||
539 | sprintf(dn, "%d:%d", d->major, d->minor); | |
540 | dfd = dev_open(dn, O_RDONLY); | |
541 | if (dfd < 0) | |
542 | continue; | |
543 | ok = st->ss->load_super(st, dfd, &super, NULL); | |
544 | close(dfd); | |
545 | if (ok != 0) | |
546 | continue; | |
547 | st->ss->getinfo_super(&info, super); | |
548 | if (info.disk.state & (1<<MD_DISK_SYNC)) | |
549 | { | |
550 | if (avail == NULL) { | |
551 | avail = malloc(info.array.raid_disks); | |
552 | memset(avail, 0, info.array.raid_disks); | |
553 | } | |
554 | if (cnt == 0) { | |
555 | cnt++; | |
556 | max_events = info.events; | |
557 | avail[info.disk.raid_disk] = 2; | |
558 | best_super = super; super = NULL; | |
559 | } else if (info.events == max_events) { | |
560 | cnt++; | |
561 | avail[info.disk.raid_disk] = 2; | |
562 | } else if (info.events == max_events-1) { | |
563 | cnt1++; | |
564 | avail[info.disk.raid_disk] = 1; | |
565 | } else if (info.events < max_events - 1) | |
566 | ; | |
567 | else if (info.events == max_events+1) { | |
568 | int i; | |
569 | cnt1 = cnt; | |
570 | cnt = 1; | |
571 | max_events = info.events; | |
572 | for (i=0; i<info.array.raid_disks; i++) | |
573 | if (avail[i]) | |
574 | avail[i]--; | |
575 | avail[info.disk.raid_disk] = 2; | |
576 | free(best_super); | |
577 | best_super = super; | |
578 | super = NULL; | |
579 | } else { /* info.events much bigger */ | |
580 | cnt = 1; cnt1 = 0; | |
581 | memset(avail, 0, info.disk.raid_disk); | |
582 | max_events = info.events; | |
583 | free(best_super); | |
584 | best_super = super; | |
585 | super = NULL; | |
586 | } | |
587 | } | |
588 | if (super) | |
589 | free(super); | |
590 | } | |
591 | if (best_super) { | |
592 | st->ss->getinfo_super(bestinfo,best_super); | |
593 | free(best_super); | |
594 | } | |
595 | return cnt + cnt1; | |
596 | } | |
597 | ||
598 | void RebuildMap(void) | |
599 | { | |
600 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
601 | struct mdstat_ent *md; | |
602 | struct map_ent *map = NULL; | |
603 | int mdp = get_mdp_major(); | |
604 | ||
605 | for (md = mdstat ; md ; md = md->next) { | |
606 | struct sysarray *sra = sysfs_read(-1, md->devnum, GET_DEVS); | |
607 | struct sysdev *sd; | |
608 | ||
609 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
610 | char dn[30]; | |
611 | int dfd; | |
612 | int ok; | |
613 | struct supertype *st; | |
614 | char *path; | |
615 | void *super; | |
616 | struct mdinfo info; | |
617 | ||
618 | sprintf(dn, "%d:%d", sd->major, sd->minor); | |
619 | dfd = dev_open(dn, O_RDONLY); | |
620 | if (dfd < 0) | |
621 | continue; | |
622 | st = guess_super(dfd); | |
623 | if ( st == NULL) | |
624 | ok = -1; | |
625 | else | |
626 | ok = st->ss->load_super(st, dfd, &super, NULL); | |
627 | close(dfd); | |
628 | if (ok != 0) | |
629 | continue; | |
630 | st->ss->getinfo_super(&info, super); | |
631 | if (md->devnum > 0) | |
632 | path = map_dev(MD_MAJOR, md->devnum, 0); | |
633 | else | |
634 | path = map_dev(mdp, (-1-md->devnum)<< 6, 0); | |
635 | map_add(&map, md->devnum, st->ss->major, | |
636 | st->minor_version, | |
637 | info.uuid, path ? : "/unknown"); | |
638 | free(super); | |
639 | break; | |
640 | } | |
641 | } | |
642 | map_write(map); | |
643 | map_free(map); | |
644 | } | |
645 | ||
646 | int IncrementalScan(int verbose) | |
647 | { | |
648 | /* look at every device listed in the 'map' file. | |
649 | * If one is found that is not running then: | |
650 | * look in mdadm.conf for bitmap file. | |
651 | * if one exists, but array has none, add it. | |
652 | * try to start array in auto-readonly mode | |
653 | */ | |
654 | struct map_ent *mapl = NULL; | |
655 | struct map_ent *me; | |
656 | mddev_ident_t devs, mddev; | |
657 | int rv = 0; | |
658 | ||
659 | map_read(&mapl); | |
660 | devs = conf_get_ident(NULL); | |
661 | ||
662 | for (me = mapl ; me ; me = me->next) { | |
663 | char path[1024]; | |
664 | mdu_array_info_t array; | |
665 | mdu_bitmap_file_t bmf; | |
666 | struct sysarray *sra; | |
667 | int mdfd = open_mddev_devnum(me->path, me->devnum, NULL, path); | |
668 | if (mdfd < 0) | |
669 | continue; | |
670 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
671 | errno != ENODEV) { | |
672 | close(mdfd); | |
673 | continue; | |
674 | } | |
675 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
676 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
677 | if (strcmp(mddev->devname, me->path) == 0) | |
678 | break; | |
679 | if (mddev && mddev->bitmap_file) { | |
680 | /* | |
681 | * Note: early kernels will wrongly fail this, so it | |
682 | * is a hint only | |
683 | */ | |
684 | int added = -1; | |
685 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
686 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
687 | if (bmfd >= 0) { | |
688 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
689 | bmfd); | |
690 | close(bmfd); | |
691 | } | |
692 | } | |
693 | if (verbose >= 0) { | |
694 | if (added == 0) | |
695 | fprintf(stderr, Name | |
696 | ": Added bitmap %s to %s\n", | |
697 | mddev->bitmap_file, me->path); | |
698 | else if (errno != EEXIST) | |
699 | fprintf(stderr, Name | |
700 | ": Failed to add bitmap to %s: %s\n", | |
701 | me->path, strerror(errno)); | |
702 | } | |
703 | } | |
704 | sra = sysfs_read(mdfd, 0, 0); | |
705 | if (sra) { | |
706 | if (sysfs_set_str(sra, NULL, | |
707 | "array_state", "read-auto") == 0) { | |
708 | if (verbose >= 0) | |
709 | fprintf(stderr, Name | |
710 | ": started array %s\n", | |
711 | me->path); | |
712 | } else { | |
713 | fprintf(stderr, Name | |
714 | ": failed to start array %s: %s\n", | |
715 | me->path, strerror(errno)); | |
716 | rv = 1; | |
717 | } | |
718 | } | |
719 | } | |
720 | return rv; | |
721 | } |