]>
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); | |
7e0f6979 | 35 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
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; | |
8382f19b NB |
77 | struct mdinfo info, info2; |
78 | struct mddev_ident_s *array_list, *match; | |
79 | char chosen_name[1024]; | |
80 | int rv; | |
81 | int devnum; | |
82 | struct map_ent *mp, *map = NULL; | |
83 | int dfd, mdfd; | |
84 | char *avail; | |
85 | int active_disks; | |
86 | ||
87 | ||
88 | struct createinfo *ci = conf_get_create_info(); | |
89 | ||
90 | if (autof == 0) | |
91 | autof = ci->autof; | |
92 | ||
93 | /* 1/ Check if devices is permitted by mdadm.conf */ | |
94 | ||
95 | if (!conf_test_dev(devname)) { | |
96 | if (verbose >= 0) | |
97 | fprintf(stderr, Name | |
98 | ": %s not permitted by mdadm.conf.\n", | |
99 | devname); | |
100 | return 1; | |
101 | } | |
102 | ||
103 | /* 2/ Find metadata, reject if none appropriate (check | |
104 | * version/name from args) */ | |
105 | ||
106 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
107 | if (dfd < 0) { | |
108 | if (verbose >= 0) | |
109 | fprintf(stderr, Name ": cannot open %s: %s.\n", | |
110 | devname, strerror(errno)); | |
111 | return 1; | |
112 | } | |
113 | if (fstat(dfd, &stb) < 0) { | |
114 | if (verbose >= 0) | |
115 | fprintf(stderr, Name ": fstat failed for %s: %s.\n", | |
116 | devname, strerror(errno)); | |
117 | close(dfd); | |
118 | return 1; | |
119 | } | |
120 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
121 | if (verbose >= 0) | |
122 | fprintf(stderr, Name ": %s is not a block device.\n", | |
123 | devname); | |
124 | close(dfd); | |
125 | return 1; | |
126 | } | |
127 | ||
128 | if (st == NULL && (st = guess_super(dfd)) == NULL) { | |
129 | if (verbose >= 0) | |
130 | fprintf(stderr, Name | |
131 | ": no recognisable superblock on %s.\n", | |
132 | devname); | |
133 | close(dfd); | |
134 | return 1; | |
135 | } | |
3da92f27 | 136 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
137 | if (verbose >= 0) |
138 | fprintf(stderr, Name ": no RAID superblock on %s.\n", | |
139 | devname); | |
140 | close(dfd); | |
141 | return 1; | |
142 | } | |
3da92f27 | 143 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
144 | close (dfd); |
145 | ||
146 | /* 3/ Check if there is a match in mdadm.conf */ | |
147 | ||
148 | array_list = conf_get_ident(NULL); | |
149 | match = NULL; | |
150 | for (; array_list; array_list = array_list->next) { | |
151 | if (array_list->uuid_set && | |
152 | same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid) | |
153 | == 0) { | |
154 | if (verbose >= 2) | |
155 | fprintf(stderr, Name | |
156 | ": UUID differs from %s.\n", | |
157 | array_list->devname); | |
158 | continue; | |
159 | } | |
160 | if (array_list->name[0] && | |
161 | strcasecmp(array_list->name, info.name) != 0) { | |
162 | if (verbose >= 2) | |
163 | fprintf(stderr, Name | |
164 | ": Name differs from %s.\n", | |
165 | array_list->devname); | |
166 | continue; | |
167 | } | |
168 | if (array_list->devices && | |
169 | !match_oneof(array_list->devices, devname)) { | |
170 | if (verbose >= 2) | |
171 | fprintf(stderr, Name | |
172 | ": Not a listed device for %s.\n", | |
173 | array_list->devname); | |
174 | continue; | |
175 | } | |
176 | if (array_list->super_minor != UnSet && | |
177 | array_list->super_minor != info.array.md_minor) { | |
178 | if (verbose >= 2) | |
179 | fprintf(stderr, Name | |
180 | ": Different super-minor to %s.\n", | |
181 | array_list->devname); | |
182 | continue; | |
183 | } | |
184 | if (!array_list->uuid_set && | |
185 | !array_list->name[0] && | |
186 | !array_list->devices && | |
187 | array_list->super_minor == UnSet) { | |
188 | if (verbose >= 2) | |
189 | fprintf(stderr, Name | |
190 | ": %s doesn't have any identifying information.\n", | |
191 | array_list->devname); | |
192 | continue; | |
193 | } | |
194 | /* FIXME, should I check raid_disks and level too?? */ | |
195 | ||
196 | if (match) { | |
197 | if (verbose >= 0) | |
198 | fprintf(stderr, Name | |
199 | ": we match both %s and %s - cannot decide which to use.\n", | |
200 | match->devname, array_list->devname); | |
201 | return 2; | |
202 | } | |
203 | match = array_list; | |
204 | } | |
205 | ||
206 | /* 3a/ if not, check for homehost match. If no match, reject. */ | |
207 | if (!match) { | |
208 | if (homehost == NULL || | |
3da92f27 | 209 | st->ss->match_home(st, homehost) == 0) { |
8382f19b NB |
210 | if (verbose >= 0) |
211 | fprintf(stderr, Name | |
212 | ": not found in mdadm.conf and not identified by homehost.\n"); | |
213 | return 2; | |
214 | } | |
215 | } | |
216 | /* 4/ Determine device number. */ | |
217 | /* - If in mdadm.conf with std name, use that */ | |
218 | /* - UUID in /var/run/mdadm.map use that */ | |
219 | /* - If name is suggestive, use that. unless in use with */ | |
220 | /* different uuid. */ | |
221 | /* - Choose a free, high number. */ | |
222 | /* - Use a partitioned device unless strong suggestion not to. */ | |
223 | /* e.g. auto=md */ | |
224 | if (match && is_standard(match->devname, &devnum)) | |
225 | /* We have devnum now */; | |
226 | else if ((mp = map_by_uuid(&map, info.uuid)) != NULL) | |
227 | devnum = mp->devnum; | |
228 | else { | |
229 | /* Have to guess a bit. */ | |
230 | int use_partitions = 1; | |
231 | char *np, *ep; | |
232 | if ((autof&7) == 3 || (autof&7) == 5) | |
233 | use_partitions = 0; | |
234 | np = strchr(info.name, ':'); | |
235 | if (np) | |
236 | np++; | |
237 | else | |
238 | np = info.name; | |
239 | devnum = strtoul(np, &ep, 10); | |
240 | if (ep > np && *ep == 0) { | |
241 | /* This is a number. Let check that it is unused. */ | |
242 | if (mddev_busy(use_partitions ? (-1-devnum) : devnum)) | |
243 | devnum = -1; | |
244 | } else | |
245 | devnum = -1; | |
246 | ||
247 | if (devnum < 0) { | |
248 | /* Haven't found anything yet, choose something free */ | |
63152c1b NB |
249 | devnum = find_free_devnum(use_partitions); |
250 | ||
251 | if (devnum == NoMdDev) { | |
8382f19b NB |
252 | fprintf(stderr, Name |
253 | ": No spare md devices!!\n"); | |
254 | return 2; | |
255 | } | |
63152c1b NB |
256 | } else |
257 | devnum = use_partitions ? (-1-devnum) : devnum; | |
8382f19b NB |
258 | } |
259 | mdfd = open_mddev_devnum(match ? match->devname : NULL, | |
260 | devnum, | |
261 | info.name, | |
9a02c62a | 262 | chosen_name, autof >> 3); |
8382f19b NB |
263 | if (mdfd < 0) { |
264 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
265 | chosen_name, strerror(errno)); | |
266 | return 2; | |
267 | } | |
268 | /* 5/ Find out if array already exists */ | |
269 | if (! mddev_busy(devnum)) { | |
270 | /* 5a/ if it does not */ | |
271 | /* - choose a name, from mdadm.conf or 'name' field in array. */ | |
272 | /* - create the array */ | |
273 | /* - add the device */ | |
274 | mdu_array_info_t ainf; | |
275 | mdu_disk_info_t disk; | |
276 | char md[20]; | |
7e0f6979 | 277 | struct mdinfo *sra; |
8382f19b NB |
278 | |
279 | memset(&ainf, 0, sizeof(ainf)); | |
280 | ainf.major_version = st->ss->major; | |
281 | ainf.minor_version = st->minor_version; | |
282 | if (ioctl(mdfd, SET_ARRAY_INFO, &ainf) != 0) { | |
283 | fprintf(stderr, Name | |
284 | ": SET_ARRAY_INFO failed for %s: %s\b", | |
285 | chosen_name, strerror(errno)); | |
286 | close(mdfd); | |
287 | return 2; | |
288 | } | |
289 | sprintf(md, "%d.%d\n", st->ss->major, st->minor_version); | |
290 | sra = sysfs_read(mdfd, devnum, GET_VERSION); | |
291 | sysfs_set_str(sra, NULL, "metadata_version", md); | |
292 | memset(&disk, 0, sizeof(disk)); | |
293 | disk.major = major(stb.st_rdev); | |
294 | disk.minor = minor(stb.st_rdev); | |
295 | sysfs_free(sra); | |
296 | if (ioctl(mdfd, ADD_NEW_DISK, &disk) != 0) { | |
297 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
298 | devname, chosen_name, strerror(errno)); | |
299 | ioctl(mdfd, STOP_ARRAY, 0); | |
300 | close(mdfd); | |
301 | return 2; | |
302 | } | |
303 | sra = sysfs_read(mdfd, devnum, GET_DEVS); | |
06c7f68e | 304 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { |
8382f19b NB |
305 | /* It really should be 'none' - must be old buggy |
306 | * kernel, and mdadm -I may not be able to complete. | |
307 | * So reject it. | |
308 | */ | |
309 | ioctl(mdfd, STOP_ARRAY, NULL); | |
310 | fprintf(stderr, Name | |
311 | ": You have an old buggy kernel which cannot support\n" | |
312 | " --incremental reliably. Aborting.\n"); | |
313 | close(mdfd); | |
314 | sysfs_free(sra); | |
315 | return 2; | |
316 | } | |
317 | } else { | |
318 | /* 5b/ if it does */ | |
319 | /* - check one drive in array to make sure metadata is a reasonably */ | |
320 | /* close match. Reject if not (e.g. different type) */ | |
321 | /* - add the device */ | |
322 | char dn[20]; | |
323 | int dfd2; | |
324 | mdu_disk_info_t disk; | |
325 | int err; | |
7e0f6979 | 326 | struct mdinfo *sra; |
3da92f27 | 327 | struct supertype *st2; |
8382f19b NB |
328 | sra = sysfs_read(mdfd, devnum, (GET_VERSION | GET_DEVS | |
329 | GET_STATE)); | |
2cdb6489 | 330 | |
7e0f6979 NB |
331 | if (sra->array.major_version != st->ss->major || |
332 | sra->array.minor_version != st->minor_version) { | |
8382f19b NB |
333 | if (verbose >= 0) |
334 | fprintf(stderr, Name | |
335 | ": %s has different metadata to chosen array %s %d.%d %d.%d.\n", | |
336 | devname, chosen_name, | |
7e0f6979 NB |
337 | sra->array.major_version, |
338 | sra->array.minor_version, | |
8382f19b NB |
339 | st->ss->major, st->minor_version); |
340 | close(mdfd); | |
341 | return 1; | |
342 | } | |
06c7f68e NB |
343 | sprintf(dn, "%d:%d", sra->devs->disk.major, |
344 | sra->devs->disk.minor); | |
8382f19b | 345 | dfd2 = dev_open(dn, O_RDONLY); |
3da92f27 NB |
346 | st2 = dup_super(st); |
347 | if (st2->ss->load_super(st2, dfd2, NULL)) { | |
8382f19b NB |
348 | fprintf(stderr, Name |
349 | ": Strange error loading metadata for %s.\n", | |
350 | chosen_name); | |
351 | close(mdfd); | |
352 | close(dfd2); | |
353 | return 2; | |
354 | } | |
355 | close(dfd2); | |
3da92f27 NB |
356 | st2->ss->getinfo_super(st2, &info2); |
357 | st2->ss->free_super(st2); | |
8382f19b NB |
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. */ | |
f8409e54 | 394 | avail = NULL; |
8382f19b NB |
395 | active_disks = count_active(st, mdfd, &avail, &info); |
396 | if (enough(info.array.level, info.array.raid_disks, | |
397 | info.array.layout, info.array.state & 1, | |
398 | avail, active_disks) == 0) { | |
399 | free(avail); | |
400 | if (verbose >= 0) | |
401 | fprintf(stderr, Name | |
402 | ": %s attached to %s, not enough to start (%d).\n", | |
403 | devname, chosen_name, active_disks); | |
404 | close(mdfd); | |
405 | return 0; | |
406 | } | |
407 | free(avail); | |
408 | ||
409 | /* 7b/ if yes, */ | |
410 | /* - if number of OK devices match expected, or -R and there */ | |
411 | /* are enough, */ | |
412 | /* + add any bitmap file */ | |
413 | /* + start the array (auto-readonly). */ | |
414 | { | |
415 | mdu_array_info_t ainf; | |
416 | ||
417 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
418 | if (verbose >= 0) | |
419 | fprintf(stderr, Name | |
420 | ": %s attached to %s which is already active.\n", | |
421 | devname, chosen_name); | |
422 | close (mdfd); | |
423 | return 0; | |
424 | } | |
425 | } | |
426 | if (runstop > 0 || active_disks >= info.array.working_disks) { | |
7e0f6979 | 427 | struct mdinfo *sra; |
8382f19b NB |
428 | /* Let's try to start it */ |
429 | if (match && match->bitmap_file) { | |
430 | int bmfd = open(match->bitmap_file, O_RDWR); | |
431 | if (bmfd < 0) { | |
432 | fprintf(stderr, Name | |
433 | ": Could not open bitmap file %s.\n", | |
434 | match->bitmap_file); | |
435 | close(mdfd); | |
436 | return 1; | |
437 | } | |
438 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
439 | close(bmfd); | |
440 | fprintf(stderr, Name | |
441 | ": Failed to set bitmapfile for %s.\n", | |
442 | chosen_name); | |
443 | close(mdfd); | |
444 | return 1; | |
445 | } | |
446 | close(bmfd); | |
447 | } | |
448 | sra = sysfs_read(mdfd, devnum, 0); | |
449 | if (sra == NULL || active_disks >= info.array.working_disks) | |
450 | rv = ioctl(mdfd, RUN_ARRAY, NULL); | |
451 | else | |
452 | rv = sysfs_set_str(sra, NULL, | |
453 | "array_state", "read-auto"); | |
454 | if (rv == 0) { | |
455 | if (verbose >= 0) | |
456 | fprintf(stderr, Name | |
457 | ": %s attached to %s, which has been started.\n", | |
458 | devname, chosen_name); | |
459 | rv = 0; | |
460 | } else { | |
461 | fprintf(stderr, Name | |
462 | ": %s attached to %s, but failed to start: %s.\n", | |
463 | devname, chosen_name, strerror(errno)); | |
464 | rv = 1; | |
465 | } | |
466 | } else { | |
467 | if (verbose >= 0) | |
468 | fprintf(stderr, Name | |
469 | ": %s attached to %s, not enough to start safely.\n", | |
470 | devname, chosen_name); | |
471 | rv = 0; | |
472 | } | |
473 | close(mdfd); | |
474 | return rv; | |
475 | } | |
476 | ||
7e0f6979 | 477 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
478 | int number, __u64 events, int verbose, |
479 | char *array_name) | |
480 | { | |
3da92f27 | 481 | /* Find a device attached to this array with a disk.number of number |
8382f19b NB |
482 | * and events less than the passed events, and remove the device. |
483 | */ | |
06c7f68e | 484 | struct mdinfo *d; |
8382f19b NB |
485 | mdu_array_info_t ra; |
486 | ||
487 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
488 | return; /* not safe to remove from active arrays | |
489 | * without thinking more */ | |
490 | ||
491 | for (d = sra->devs; d ; d = d->next) { | |
492 | char dn[10]; | |
493 | int dfd; | |
8382f19b | 494 | struct mdinfo info; |
06c7f68e | 495 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
496 | dfd = dev_open(dn, O_RDONLY); |
497 | if (dfd < 0) | |
498 | continue; | |
3da92f27 | 499 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
500 | close(dfd); |
501 | continue; | |
502 | } | |
3da92f27 NB |
503 | st->ss->getinfo_super(st, &info); |
504 | st->ss->free_super(st); | |
8382f19b NB |
505 | close(dfd); |
506 | ||
507 | if (info.disk.number != number || | |
508 | info.events >= events) | |
509 | continue; | |
510 | ||
06c7f68e | 511 | if (d->disk.raid_disk > -1) |
8382f19b NB |
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", | |
06c7f68e | 517 | d->sys_name+4, array_name); |
8382f19b NB |
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 */ | |
06c7f68e | 525 | struct mdinfo *d; |
8382f19b NB |
526 | int cnt = 0, cnt1 = 0; |
527 | __u64 max_events = 0; | |
7e0f6979 | 528 | struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); |
8382f19b NB |
529 | char *avail = NULL; |
530 | ||
531 | for (d = sra->devs ; d ; d = d->next) { | |
532 | char dn[30]; | |
533 | int dfd; | |
8382f19b NB |
534 | int ok; |
535 | struct mdinfo info; | |
536 | ||
06c7f68e | 537 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
538 | dfd = dev_open(dn, O_RDONLY); |
539 | if (dfd < 0) | |
540 | continue; | |
3da92f27 | 541 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
542 | close(dfd); |
543 | if (ok != 0) | |
544 | continue; | |
3da92f27 | 545 | st->ss->getinfo_super(st, &info); |
43aaf431 DL |
546 | if (!avail) { |
547 | avail = malloc(info.array.raid_disks); | |
548 | if (!avail) { | |
549 | fprintf(stderr, Name ": out of memory.\n"); | |
550 | exit(1); | |
551 | } | |
552 | memset(avail, 0, info.array.raid_disks); | |
553 | *availp = avail; | |
554 | } | |
555 | ||
8382f19b NB |
556 | if (info.disk.state & (1<<MD_DISK_SYNC)) |
557 | { | |
8382f19b NB |
558 | if (cnt == 0) { |
559 | cnt++; | |
560 | max_events = info.events; | |
561 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 562 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
563 | } else if (info.events == max_events) { |
564 | cnt++; | |
565 | avail[info.disk.raid_disk] = 2; | |
566 | } else if (info.events == max_events-1) { | |
567 | cnt1++; | |
568 | avail[info.disk.raid_disk] = 1; | |
569 | } else if (info.events < max_events - 1) | |
570 | ; | |
571 | else if (info.events == max_events+1) { | |
572 | int i; | |
573 | cnt1 = cnt; | |
574 | cnt = 1; | |
575 | max_events = info.events; | |
576 | for (i=0; i<info.array.raid_disks; i++) | |
577 | if (avail[i]) | |
578 | avail[i]--; | |
579 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 580 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
581 | } else { /* info.events much bigger */ |
582 | cnt = 1; cnt1 = 0; | |
583 | memset(avail, 0, info.disk.raid_disk); | |
584 | max_events = info.events; | |
3da92f27 | 585 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
586 | } |
587 | } | |
3da92f27 | 588 | st->ss->free_super(st); |
8382f19b NB |
589 | } |
590 | return cnt + cnt1; | |
591 | } | |
592 | ||
593 | void RebuildMap(void) | |
594 | { | |
595 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
596 | struct mdstat_ent *md; | |
597 | struct map_ent *map = NULL; | |
598 | int mdp = get_mdp_major(); | |
599 | ||
600 | for (md = mdstat ; md ; md = md->next) { | |
7e0f6979 | 601 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS); |
06c7f68e | 602 | struct mdinfo *sd; |
8382f19b NB |
603 | |
604 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
605 | char dn[30]; | |
606 | int dfd; | |
607 | int ok; | |
608 | struct supertype *st; | |
609 | char *path; | |
8382f19b NB |
610 | struct mdinfo info; |
611 | ||
06c7f68e | 612 | sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor); |
8382f19b NB |
613 | dfd = dev_open(dn, O_RDONLY); |
614 | if (dfd < 0) | |
615 | continue; | |
616 | st = guess_super(dfd); | |
617 | if ( st == NULL) | |
618 | ok = -1; | |
619 | else | |
3da92f27 | 620 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
621 | close(dfd); |
622 | if (ok != 0) | |
623 | continue; | |
3da92f27 | 624 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
625 | if (md->devnum > 0) |
626 | path = map_dev(MD_MAJOR, md->devnum, 0); | |
627 | else | |
628 | path = map_dev(mdp, (-1-md->devnum)<< 6, 0); | |
629 | map_add(&map, md->devnum, st->ss->major, | |
630 | st->minor_version, | |
631 | info.uuid, path ? : "/unknown"); | |
3da92f27 | 632 | st->ss->free_super(st); |
8382f19b NB |
633 | break; |
634 | } | |
635 | } | |
636 | map_write(map); | |
637 | map_free(map); | |
638 | } | |
639 | ||
640 | int IncrementalScan(int verbose) | |
641 | { | |
642 | /* look at every device listed in the 'map' file. | |
643 | * If one is found that is not running then: | |
644 | * look in mdadm.conf for bitmap file. | |
645 | * if one exists, but array has none, add it. | |
646 | * try to start array in auto-readonly mode | |
647 | */ | |
648 | struct map_ent *mapl = NULL; | |
649 | struct map_ent *me; | |
650 | mddev_ident_t devs, mddev; | |
651 | int rv = 0; | |
652 | ||
653 | map_read(&mapl); | |
654 | devs = conf_get_ident(NULL); | |
655 | ||
656 | for (me = mapl ; me ; me = me->next) { | |
657 | char path[1024]; | |
658 | mdu_array_info_t array; | |
659 | mdu_bitmap_file_t bmf; | |
7e0f6979 | 660 | struct mdinfo *sra; |
9a02c62a NB |
661 | int mdfd = open_mddev_devnum(me->path, me->devnum, |
662 | NULL, path, 0); | |
8382f19b NB |
663 | if (mdfd < 0) |
664 | continue; | |
665 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
666 | errno != ENODEV) { | |
667 | close(mdfd); | |
668 | continue; | |
669 | } | |
670 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
671 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
672 | if (strcmp(mddev->devname, me->path) == 0) | |
673 | break; | |
674 | if (mddev && mddev->bitmap_file) { | |
675 | /* | |
676 | * Note: early kernels will wrongly fail this, so it | |
677 | * is a hint only | |
678 | */ | |
679 | int added = -1; | |
680 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
681 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
682 | if (bmfd >= 0) { | |
683 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
684 | bmfd); | |
685 | close(bmfd); | |
686 | } | |
687 | } | |
688 | if (verbose >= 0) { | |
689 | if (added == 0) | |
690 | fprintf(stderr, Name | |
691 | ": Added bitmap %s to %s\n", | |
692 | mddev->bitmap_file, me->path); | |
693 | else if (errno != EEXIST) | |
694 | fprintf(stderr, Name | |
695 | ": Failed to add bitmap to %s: %s\n", | |
696 | me->path, strerror(errno)); | |
697 | } | |
698 | } | |
699 | sra = sysfs_read(mdfd, 0, 0); | |
700 | if (sra) { | |
701 | if (sysfs_set_str(sra, NULL, | |
702 | "array_state", "read-auto") == 0) { | |
703 | if (verbose >= 0) | |
704 | fprintf(stderr, Name | |
705 | ": started array %s\n", | |
706 | me->path); | |
707 | } else { | |
708 | fprintf(stderr, Name | |
709 | ": failed to start array %s: %s\n", | |
710 | me->path, strerror(errno)); | |
711 | rv = 1; | |
712 | } | |
713 | } | |
714 | } | |
715 | return rv; | |
716 | } |