]>
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; | |
8382f19b NB |
86 | struct createinfo *ci = conf_get_create_info(); |
87 | ||
8382f19b NB |
88 | |
89 | /* 1/ Check if devices is permitted by mdadm.conf */ | |
90 | ||
91 | if (!conf_test_dev(devname)) { | |
92 | if (verbose >= 0) | |
93 | fprintf(stderr, Name | |
94 | ": %s not permitted by mdadm.conf.\n", | |
95 | devname); | |
96 | return 1; | |
97 | } | |
98 | ||
99 | /* 2/ Find metadata, reject if none appropriate (check | |
100 | * version/name from args) */ | |
101 | ||
102 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
103 | if (dfd < 0) { | |
104 | if (verbose >= 0) | |
105 | fprintf(stderr, Name ": cannot open %s: %s.\n", | |
106 | devname, strerror(errno)); | |
107 | return 1; | |
108 | } | |
109 | if (fstat(dfd, &stb) < 0) { | |
110 | if (verbose >= 0) | |
111 | fprintf(stderr, Name ": fstat failed for %s: %s.\n", | |
112 | devname, strerror(errno)); | |
113 | close(dfd); | |
114 | return 1; | |
115 | } | |
116 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
117 | if (verbose >= 0) | |
118 | fprintf(stderr, Name ": %s is not a block device.\n", | |
119 | devname); | |
120 | close(dfd); | |
121 | return 1; | |
122 | } | |
123 | ||
124 | if (st == NULL && (st = guess_super(dfd)) == NULL) { | |
125 | if (verbose >= 0) | |
126 | fprintf(stderr, Name | |
127 | ": no recognisable superblock on %s.\n", | |
128 | devname); | |
129 | close(dfd); | |
130 | return 1; | |
131 | } | |
3da92f27 | 132 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
133 | if (verbose >= 0) |
134 | fprintf(stderr, Name ": no RAID superblock on %s.\n", | |
135 | devname); | |
136 | close(dfd); | |
137 | return 1; | |
138 | } | |
3da92f27 | 139 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
140 | close (dfd); |
141 | ||
142 | /* 3/ Check if there is a match in mdadm.conf */ | |
143 | ||
144 | array_list = conf_get_ident(NULL); | |
145 | match = NULL; | |
146 | for (; array_list; array_list = array_list->next) { | |
147 | if (array_list->uuid_set && | |
148 | same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid) | |
149 | == 0) { | |
150 | if (verbose >= 2) | |
151 | fprintf(stderr, Name | |
152 | ": UUID differs from %s.\n", | |
153 | array_list->devname); | |
154 | continue; | |
155 | } | |
156 | if (array_list->name[0] && | |
157 | strcasecmp(array_list->name, info.name) != 0) { | |
158 | if (verbose >= 2) | |
159 | fprintf(stderr, Name | |
160 | ": Name differs from %s.\n", | |
161 | array_list->devname); | |
162 | continue; | |
163 | } | |
164 | if (array_list->devices && | |
165 | !match_oneof(array_list->devices, devname)) { | |
166 | if (verbose >= 2) | |
167 | fprintf(stderr, Name | |
168 | ": Not a listed device for %s.\n", | |
169 | array_list->devname); | |
170 | continue; | |
171 | } | |
172 | if (array_list->super_minor != UnSet && | |
173 | array_list->super_minor != info.array.md_minor) { | |
174 | if (verbose >= 2) | |
175 | fprintf(stderr, Name | |
176 | ": Different super-minor to %s.\n", | |
177 | array_list->devname); | |
178 | continue; | |
179 | } | |
180 | if (!array_list->uuid_set && | |
181 | !array_list->name[0] && | |
182 | !array_list->devices && | |
183 | array_list->super_minor == UnSet) { | |
184 | if (verbose >= 2) | |
185 | fprintf(stderr, Name | |
186 | ": %s doesn't have any identifying information.\n", | |
187 | array_list->devname); | |
188 | continue; | |
189 | } | |
190 | /* FIXME, should I check raid_disks and level too?? */ | |
191 | ||
192 | if (match) { | |
193 | if (verbose >= 0) | |
194 | fprintf(stderr, Name | |
195 | ": we match both %s and %s - cannot decide which to use.\n", | |
196 | match->devname, array_list->devname); | |
197 | return 2; | |
198 | } | |
199 | match = array_list; | |
200 | } | |
201 | ||
202 | /* 3a/ if not, check for homehost match. If no match, reject. */ | |
203 | if (!match) { | |
204 | if (homehost == NULL || | |
3da92f27 | 205 | st->ss->match_home(st, homehost) == 0) { |
8382f19b NB |
206 | if (verbose >= 0) |
207 | fprintf(stderr, Name | |
208 | ": not found in mdadm.conf and not identified by homehost.\n"); | |
209 | return 2; | |
210 | } | |
211 | } | |
212 | /* 4/ Determine device number. */ | |
2b4ca8f0 N |
213 | /* - If in mdadm.conf with std name, get number from name. */ |
214 | /* - UUID in /var/run/mdadm.map get number from mapping */ | |
8382f19b NB |
215 | /* - If name is suggestive, use that. unless in use with */ |
216 | /* different uuid. */ | |
217 | /* - Choose a free, high number. */ | |
218 | /* - Use a partitioned device unless strong suggestion not to. */ | |
219 | /* e.g. auto=md */ | |
4ef2f11e N |
220 | |
221 | /* There are three possible sources for 'autof': command line, | |
222 | * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf. | |
223 | * They have precedence in that order. | |
224 | */ | |
225 | if (autof == 0 && match) | |
226 | autof = match->autof; | |
227 | if (autof == 0) | |
228 | autof = ci->autof; | |
229 | ||
2b4ca8f0 N |
230 | if (match && (rv = is_standard(match->devname, &devnum))) { |
231 | devnum = (rv > 0) ? (-1-devnum) : devnum; | |
232 | } else if ((mp = map_by_uuid(&map, info.uuid)) != NULL) | |
8382f19b NB |
233 | devnum = mp->devnum; |
234 | else { | |
235 | /* Have to guess a bit. */ | |
236 | int use_partitions = 1; | |
237 | char *np, *ep; | |
238 | if ((autof&7) == 3 || (autof&7) == 5) | |
239 | use_partitions = 0; | |
240 | np = strchr(info.name, ':'); | |
241 | if (np) | |
242 | np++; | |
243 | else | |
244 | np = info.name; | |
245 | devnum = strtoul(np, &ep, 10); | |
246 | if (ep > np && *ep == 0) { | |
247 | /* This is a number. Let check that it is unused. */ | |
248 | if (mddev_busy(use_partitions ? (-1-devnum) : devnum)) | |
249 | devnum = -1; | |
250 | } else | |
251 | devnum = -1; | |
252 | ||
253 | if (devnum < 0) { | |
254 | /* Haven't found anything yet, choose something free */ | |
63152c1b NB |
255 | devnum = find_free_devnum(use_partitions); |
256 | ||
257 | if (devnum == NoMdDev) { | |
8382f19b NB |
258 | fprintf(stderr, Name |
259 | ": No spare md devices!!\n"); | |
260 | return 2; | |
261 | } | |
63152c1b NB |
262 | } else |
263 | devnum = use_partitions ? (-1-devnum) : devnum; | |
8382f19b NB |
264 | } |
265 | mdfd = open_mddev_devnum(match ? match->devname : NULL, | |
266 | devnum, | |
267 | info.name, | |
9a02c62a | 268 | chosen_name, autof >> 3); |
8382f19b NB |
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]; | |
7e0f6979 | 283 | struct mdinfo *sra; |
8382f19b NB |
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); | |
06c7f68e | 310 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { |
8382f19b NB |
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; | |
7e0f6979 | 332 | struct mdinfo *sra; |
3da92f27 | 333 | struct supertype *st2; |
8382f19b NB |
334 | sra = sysfs_read(mdfd, devnum, (GET_VERSION | GET_DEVS | |
335 | GET_STATE)); | |
2cdb6489 | 336 | |
7e0f6979 NB |
337 | if (sra->array.major_version != st->ss->major || |
338 | sra->array.minor_version != st->minor_version) { | |
8382f19b NB |
339 | if (verbose >= 0) |
340 | fprintf(stderr, Name | |
341 | ": %s has different metadata to chosen array %s %d.%d %d.%d.\n", | |
342 | devname, chosen_name, | |
7e0f6979 NB |
343 | sra->array.major_version, |
344 | sra->array.minor_version, | |
8382f19b NB |
345 | st->ss->major, st->minor_version); |
346 | close(mdfd); | |
347 | return 1; | |
348 | } | |
06c7f68e NB |
349 | sprintf(dn, "%d:%d", sra->devs->disk.major, |
350 | sra->devs->disk.minor); | |
8382f19b | 351 | dfd2 = dev_open(dn, O_RDONLY); |
3da92f27 NB |
352 | st2 = dup_super(st); |
353 | if (st2->ss->load_super(st2, dfd2, NULL)) { | |
8382f19b NB |
354 | fprintf(stderr, Name |
355 | ": Strange error loading metadata for %s.\n", | |
356 | chosen_name); | |
357 | close(mdfd); | |
358 | close(dfd2); | |
359 | return 2; | |
360 | } | |
361 | close(dfd2); | |
3da92f27 NB |
362 | st2->ss->getinfo_super(st2, &info2); |
363 | st2->ss->free_super(st2); | |
8382f19b NB |
364 | if (info.array.level != info2.array.level || |
365 | memcmp(info.uuid, info2.uuid, 16) != 0 || | |
366 | info.array.raid_disks != info2.array.raid_disks) { | |
367 | fprintf(stderr, Name | |
368 | ": unexpected difference between %s and %s.\n", | |
369 | chosen_name, devname); | |
370 | close(mdfd); | |
371 | return 2; | |
372 | } | |
373 | memset(&disk, 0, sizeof(disk)); | |
374 | disk.major = major(stb.st_rdev); | |
375 | disk.minor = minor(stb.st_rdev); | |
376 | err = ioctl(mdfd, ADD_NEW_DISK, &disk); | |
377 | if (err < 0 && errno == EBUSY) { | |
378 | /* could be another device present with the same | |
379 | * disk.number. Find and reject any such | |
380 | */ | |
381 | find_reject(mdfd, st, sra, info.disk.number, | |
382 | info.events, verbose, chosen_name); | |
383 | err = ioctl(mdfd, ADD_NEW_DISK, &disk); | |
384 | } | |
385 | if (err < 0) { | |
386 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
387 | devname, chosen_name, strerror(errno)); | |
388 | close(mdfd); | |
389 | return 2; | |
390 | } | |
391 | } | |
392 | /* 6/ Make sure /var/run/mdadm.map contains this array. */ | |
393 | map_update(&map, devnum, | |
394 | info.array.major_version, | |
395 | info.array.minor_version, | |
396 | info.uuid, chosen_name); | |
397 | ||
398 | /* 7/ Is there enough devices to possibly start the array? */ | |
399 | /* 7a/ if not, finish with success. */ | |
f8409e54 | 400 | avail = NULL; |
8382f19b NB |
401 | active_disks = count_active(st, mdfd, &avail, &info); |
402 | if (enough(info.array.level, info.array.raid_disks, | |
403 | info.array.layout, info.array.state & 1, | |
404 | avail, active_disks) == 0) { | |
405 | free(avail); | |
406 | if (verbose >= 0) | |
407 | fprintf(stderr, Name | |
408 | ": %s attached to %s, not enough to start (%d).\n", | |
409 | devname, chosen_name, active_disks); | |
410 | close(mdfd); | |
411 | return 0; | |
412 | } | |
413 | free(avail); | |
414 | ||
415 | /* 7b/ if yes, */ | |
416 | /* - if number of OK devices match expected, or -R and there */ | |
417 | /* are enough, */ | |
418 | /* + add any bitmap file */ | |
419 | /* + start the array (auto-readonly). */ | |
420 | { | |
421 | mdu_array_info_t ainf; | |
422 | ||
423 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
424 | if (verbose >= 0) | |
425 | fprintf(stderr, Name | |
426 | ": %s attached to %s which is already active.\n", | |
427 | devname, chosen_name); | |
428 | close (mdfd); | |
429 | return 0; | |
430 | } | |
431 | } | |
432 | if (runstop > 0 || active_disks >= info.array.working_disks) { | |
7e0f6979 | 433 | struct mdinfo *sra; |
8382f19b NB |
434 | /* Let's try to start it */ |
435 | if (match && match->bitmap_file) { | |
436 | int bmfd = open(match->bitmap_file, O_RDWR); | |
437 | if (bmfd < 0) { | |
438 | fprintf(stderr, Name | |
439 | ": Could not open bitmap file %s.\n", | |
440 | match->bitmap_file); | |
441 | close(mdfd); | |
442 | return 1; | |
443 | } | |
444 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
445 | close(bmfd); | |
446 | fprintf(stderr, Name | |
447 | ": Failed to set bitmapfile for %s.\n", | |
448 | chosen_name); | |
449 | close(mdfd); | |
450 | return 1; | |
451 | } | |
452 | close(bmfd); | |
453 | } | |
454 | sra = sysfs_read(mdfd, devnum, 0); | |
455 | if (sra == NULL || active_disks >= info.array.working_disks) | |
456 | rv = ioctl(mdfd, RUN_ARRAY, NULL); | |
457 | else | |
458 | rv = sysfs_set_str(sra, NULL, | |
459 | "array_state", "read-auto"); | |
460 | if (rv == 0) { | |
461 | if (verbose >= 0) | |
462 | fprintf(stderr, Name | |
463 | ": %s attached to %s, which has been started.\n", | |
464 | devname, chosen_name); | |
465 | rv = 0; | |
466 | } else { | |
467 | fprintf(stderr, Name | |
468 | ": %s attached to %s, but failed to start: %s.\n", | |
469 | devname, chosen_name, strerror(errno)); | |
470 | rv = 1; | |
471 | } | |
472 | } else { | |
473 | if (verbose >= 0) | |
474 | fprintf(stderr, Name | |
475 | ": %s attached to %s, not enough to start safely.\n", | |
476 | devname, chosen_name); | |
477 | rv = 0; | |
478 | } | |
479 | close(mdfd); | |
480 | return rv; | |
481 | } | |
482 | ||
7e0f6979 | 483 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
484 | int number, __u64 events, int verbose, |
485 | char *array_name) | |
486 | { | |
3da92f27 | 487 | /* Find a device attached to this array with a disk.number of number |
8382f19b NB |
488 | * and events less than the passed events, and remove the device. |
489 | */ | |
06c7f68e | 490 | struct mdinfo *d; |
8382f19b NB |
491 | mdu_array_info_t ra; |
492 | ||
493 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
494 | return; /* not safe to remove from active arrays | |
495 | * without thinking more */ | |
496 | ||
497 | for (d = sra->devs; d ; d = d->next) { | |
498 | char dn[10]; | |
499 | int dfd; | |
8382f19b | 500 | struct mdinfo info; |
06c7f68e | 501 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
502 | dfd = dev_open(dn, O_RDONLY); |
503 | if (dfd < 0) | |
504 | continue; | |
3da92f27 | 505 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
506 | close(dfd); |
507 | continue; | |
508 | } | |
3da92f27 NB |
509 | st->ss->getinfo_super(st, &info); |
510 | st->ss->free_super(st); | |
8382f19b NB |
511 | close(dfd); |
512 | ||
513 | if (info.disk.number != number || | |
514 | info.events >= events) | |
515 | continue; | |
516 | ||
06c7f68e | 517 | if (d->disk.raid_disk > -1) |
8382f19b NB |
518 | sysfs_set_str(sra, d, "slot", "none"); |
519 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
520 | if (verbose >= 0) | |
521 | fprintf(stderr, Name | |
522 | ": removing old device %s from %s\n", | |
06c7f68e | 523 | d->sys_name+4, array_name); |
8382f19b NB |
524 | } |
525 | } | |
526 | ||
527 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
528 | struct mdinfo *bestinfo) | |
529 | { | |
530 | /* count how many devices in sra think they are active */ | |
06c7f68e | 531 | struct mdinfo *d; |
8382f19b NB |
532 | int cnt = 0, cnt1 = 0; |
533 | __u64 max_events = 0; | |
7e0f6979 | 534 | struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); |
8382f19b NB |
535 | char *avail = NULL; |
536 | ||
537 | for (d = sra->devs ; d ; d = d->next) { | |
538 | char dn[30]; | |
539 | int dfd; | |
8382f19b NB |
540 | int ok; |
541 | struct mdinfo info; | |
542 | ||
06c7f68e | 543 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
544 | dfd = dev_open(dn, O_RDONLY); |
545 | if (dfd < 0) | |
546 | continue; | |
3da92f27 | 547 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
548 | close(dfd); |
549 | if (ok != 0) | |
550 | continue; | |
3da92f27 | 551 | st->ss->getinfo_super(st, &info); |
43aaf431 DL |
552 | if (!avail) { |
553 | avail = malloc(info.array.raid_disks); | |
554 | if (!avail) { | |
555 | fprintf(stderr, Name ": out of memory.\n"); | |
556 | exit(1); | |
557 | } | |
558 | memset(avail, 0, info.array.raid_disks); | |
559 | *availp = avail; | |
560 | } | |
561 | ||
8382f19b NB |
562 | if (info.disk.state & (1<<MD_DISK_SYNC)) |
563 | { | |
8382f19b NB |
564 | if (cnt == 0) { |
565 | cnt++; | |
566 | max_events = info.events; | |
567 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 568 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
569 | } else if (info.events == max_events) { |
570 | cnt++; | |
571 | avail[info.disk.raid_disk] = 2; | |
572 | } else if (info.events == max_events-1) { | |
573 | cnt1++; | |
574 | avail[info.disk.raid_disk] = 1; | |
575 | } else if (info.events < max_events - 1) | |
576 | ; | |
577 | else if (info.events == max_events+1) { | |
578 | int i; | |
579 | cnt1 = cnt; | |
580 | cnt = 1; | |
581 | max_events = info.events; | |
582 | for (i=0; i<info.array.raid_disks; i++) | |
583 | if (avail[i]) | |
584 | avail[i]--; | |
585 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 586 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
587 | } else { /* info.events much bigger */ |
588 | cnt = 1; cnt1 = 0; | |
589 | memset(avail, 0, info.disk.raid_disk); | |
590 | max_events = info.events; | |
3da92f27 | 591 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
592 | } |
593 | } | |
3da92f27 | 594 | st->ss->free_super(st); |
8382f19b NB |
595 | } |
596 | return cnt + cnt1; | |
597 | } | |
598 | ||
599 | void RebuildMap(void) | |
600 | { | |
601 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
602 | struct mdstat_ent *md; | |
603 | struct map_ent *map = NULL; | |
604 | int mdp = get_mdp_major(); | |
605 | ||
606 | for (md = mdstat ; md ; md = md->next) { | |
7e0f6979 | 607 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS); |
06c7f68e | 608 | struct mdinfo *sd; |
8382f19b NB |
609 | |
610 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
611 | char dn[30]; | |
612 | int dfd; | |
613 | int ok; | |
614 | struct supertype *st; | |
615 | char *path; | |
8382f19b NB |
616 | struct mdinfo info; |
617 | ||
06c7f68e | 618 | sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor); |
8382f19b NB |
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 | |
3da92f27 | 626 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
627 | close(dfd); |
628 | if (ok != 0) | |
629 | continue; | |
3da92f27 | 630 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
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"); | |
3da92f27 | 638 | st->ss->free_super(st); |
8382f19b NB |
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; | |
7e0f6979 | 666 | struct mdinfo *sra; |
9a02c62a NB |
667 | int mdfd = open_mddev_devnum(me->path, me->devnum, |
668 | NULL, path, 0); | |
8382f19b NB |
669 | if (mdfd < 0) |
670 | continue; | |
671 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
672 | errno != ENODEV) { | |
673 | close(mdfd); | |
674 | continue; | |
675 | } | |
676 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
677 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
678 | if (strcmp(mddev->devname, me->path) == 0) | |
679 | break; | |
680 | if (mddev && mddev->bitmap_file) { | |
681 | /* | |
682 | * Note: early kernels will wrongly fail this, so it | |
683 | * is a hint only | |
684 | */ | |
685 | int added = -1; | |
686 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
687 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
688 | if (bmfd >= 0) { | |
689 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
690 | bmfd); | |
691 | close(bmfd); | |
692 | } | |
693 | } | |
694 | if (verbose >= 0) { | |
695 | if (added == 0) | |
696 | fprintf(stderr, Name | |
697 | ": Added bitmap %s to %s\n", | |
698 | mddev->bitmap_file, me->path); | |
699 | else if (errno != EEXIST) | |
700 | fprintf(stderr, Name | |
701 | ": Failed to add bitmap to %s: %s\n", | |
702 | me->path, strerror(errno)); | |
703 | } | |
704 | } | |
705 | sra = sysfs_read(mdfd, 0, 0); | |
706 | if (sra) { | |
707 | if (sysfs_set_str(sra, NULL, | |
708 | "array_state", "read-auto") == 0) { | |
709 | if (verbose >= 0) | |
710 | fprintf(stderr, Name | |
711 | ": started array %s\n", | |
712 | me->path); | |
713 | } else { | |
714 | fprintf(stderr, Name | |
715 | ": failed to start array %s: %s\n", | |
716 | me->path, strerror(errno)); | |
717 | rv = 1; | |
718 | } | |
719 | } | |
720 | } | |
721 | return rv; | |
722 | } |