]>
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 | |
598f0d58 | 43 | * and starting the array if sensible or - if runstop>0 - if possible. |
8382f19b NB |
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 | |
352452c3 | 59 | * Don't choose partitioned for containers. |
8382f19b NB |
60 | * 5/ Find out if array already exists |
61 | * 5a/ if it does not | |
62 | * - choose a name, from mdadm.conf or 'name' field in array. | |
63 | * - create the array | |
64 | * - add the device | |
65 | * 5b/ if it does | |
66 | * - check one drive in array to make sure metadata is a reasonably | |
67 | * close match. Reject if not (e.g. different type) | |
68 | * - add the device | |
69 | * 6/ Make sure /var/run/mdadm.map contains this array. | |
70 | * 7/ Is there enough devices to possibly start the array? | |
352452c3 | 71 | * For a container, this means running Incremental_container. |
8382f19b NB |
72 | * 7a/ if not, finish with success. |
73 | * 7b/ if yes, | |
74 | * - read all metadata and arrange devices like -A does | |
75 | * - if number of OK devices match expected, or -R and there are enough, | |
76 | * start the array (auto-readonly). | |
77 | */ | |
78 | struct stat stb; | |
f35f2525 | 79 | struct mdinfo info; |
8382f19b NB |
80 | struct mddev_ident_s *array_list, *match; |
81 | char chosen_name[1024]; | |
82 | int rv; | |
83 | int devnum; | |
84 | struct map_ent *mp, *map = NULL; | |
85 | int dfd, mdfd; | |
86 | char *avail; | |
87 | int active_disks; | |
88 | ||
89 | ||
90 | struct createinfo *ci = conf_get_create_info(); | |
91 | ||
92 | if (autof == 0) | |
93 | autof = ci->autof; | |
94 | ||
352452c3 | 95 | /* 1/ Check if device is permitted by mdadm.conf */ |
8382f19b NB |
96 | |
97 | if (!conf_test_dev(devname)) { | |
98 | if (verbose >= 0) | |
99 | fprintf(stderr, Name | |
100 | ": %s not permitted by mdadm.conf.\n", | |
101 | devname); | |
102 | return 1; | |
103 | } | |
104 | ||
105 | /* 2/ Find metadata, reject if none appropriate (check | |
106 | * version/name from args) */ | |
107 | ||
108 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
109 | if (dfd < 0) { | |
110 | if (verbose >= 0) | |
111 | fprintf(stderr, Name ": cannot open %s: %s.\n", | |
112 | devname, strerror(errno)); | |
113 | return 1; | |
114 | } | |
115 | if (fstat(dfd, &stb) < 0) { | |
116 | if (verbose >= 0) | |
117 | fprintf(stderr, Name ": fstat failed for %s: %s.\n", | |
118 | devname, strerror(errno)); | |
119 | close(dfd); | |
120 | return 1; | |
121 | } | |
122 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
123 | if (verbose >= 0) | |
124 | fprintf(stderr, Name ": %s is not a block device.\n", | |
125 | devname); | |
126 | close(dfd); | |
127 | return 1; | |
128 | } | |
129 | ||
130 | if (st == NULL && (st = guess_super(dfd)) == NULL) { | |
131 | if (verbose >= 0) | |
132 | fprintf(stderr, Name | |
133 | ": no recognisable superblock on %s.\n", | |
134 | devname); | |
135 | close(dfd); | |
136 | return 1; | |
137 | } | |
3da92f27 | 138 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
139 | if (verbose >= 0) |
140 | fprintf(stderr, Name ": no RAID superblock on %s.\n", | |
141 | devname); | |
142 | close(dfd); | |
143 | return 1; | |
144 | } | |
8382f19b NB |
145 | close (dfd); |
146 | ||
352452c3 | 147 | if (st->ss->container_content && st->loaded_container) { |
598f0d58 NB |
148 | /* This is a pre-built container array, so we do something |
149 | * rather different. | |
150 | */ | |
151 | return Incremental_container(st, devname, verbose, runstop, | |
152 | autof); | |
153 | } | |
154 | ||
f35f2525 | 155 | memset(&info, 0, sizeof(info)); |
598f0d58 | 156 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
157 | /* 3/ Check if there is a match in mdadm.conf */ |
158 | ||
159 | array_list = conf_get_ident(NULL); | |
160 | match = NULL; | |
161 | for (; array_list; array_list = array_list->next) { | |
162 | if (array_list->uuid_set && | |
163 | same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid) | |
164 | == 0) { | |
165 | if (verbose >= 2) | |
166 | fprintf(stderr, Name | |
167 | ": UUID differs from %s.\n", | |
168 | array_list->devname); | |
169 | continue; | |
170 | } | |
171 | if (array_list->name[0] && | |
172 | strcasecmp(array_list->name, info.name) != 0) { | |
173 | if (verbose >= 2) | |
174 | fprintf(stderr, Name | |
175 | ": Name differs from %s.\n", | |
176 | array_list->devname); | |
177 | continue; | |
178 | } | |
179 | if (array_list->devices && | |
180 | !match_oneof(array_list->devices, devname)) { | |
181 | if (verbose >= 2) | |
182 | fprintf(stderr, Name | |
183 | ": Not a listed device for %s.\n", | |
184 | array_list->devname); | |
185 | continue; | |
186 | } | |
187 | if (array_list->super_minor != UnSet && | |
188 | array_list->super_minor != info.array.md_minor) { | |
189 | if (verbose >= 2) | |
190 | fprintf(stderr, Name | |
191 | ": Different super-minor to %s.\n", | |
192 | array_list->devname); | |
193 | continue; | |
194 | } | |
195 | if (!array_list->uuid_set && | |
196 | !array_list->name[0] && | |
197 | !array_list->devices && | |
198 | array_list->super_minor == UnSet) { | |
199 | if (verbose >= 2) | |
200 | fprintf(stderr, Name | |
201 | ": %s doesn't have any identifying information.\n", | |
202 | array_list->devname); | |
203 | continue; | |
204 | } | |
205 | /* FIXME, should I check raid_disks and level too?? */ | |
206 | ||
207 | if (match) { | |
208 | if (verbose >= 0) | |
209 | fprintf(stderr, Name | |
210 | ": we match both %s and %s - cannot decide which to use.\n", | |
211 | match->devname, array_list->devname); | |
212 | return 2; | |
213 | } | |
214 | match = array_list; | |
215 | } | |
216 | ||
217 | /* 3a/ if not, check for homehost match. If no match, reject. */ | |
218 | if (!match) { | |
219 | if (homehost == NULL || | |
3da92f27 | 220 | st->ss->match_home(st, homehost) == 0) { |
8382f19b NB |
221 | if (verbose >= 0) |
222 | fprintf(stderr, Name | |
223 | ": not found in mdadm.conf and not identified by homehost.\n"); | |
224 | return 2; | |
225 | } | |
226 | } | |
227 | /* 4/ Determine device number. */ | |
228 | /* - If in mdadm.conf with std name, use that */ | |
229 | /* - UUID in /var/run/mdadm.map use that */ | |
230 | /* - If name is suggestive, use that. unless in use with */ | |
231 | /* different uuid. */ | |
232 | /* - Choose a free, high number. */ | |
233 | /* - Use a partitioned device unless strong suggestion not to. */ | |
234 | /* e.g. auto=md */ | |
235 | if (match && is_standard(match->devname, &devnum)) | |
236 | /* We have devnum now */; | |
237 | else if ((mp = map_by_uuid(&map, info.uuid)) != NULL) | |
238 | devnum = mp->devnum; | |
239 | else { | |
240 | /* Have to guess a bit. */ | |
241 | int use_partitions = 1; | |
242 | char *np, *ep; | |
25dbe93a | 243 | char *nm, nbuf[1024]; |
8382f19b NB |
244 | if ((autof&7) == 3 || (autof&7) == 5) |
245 | use_partitions = 0; | |
352452c3 N |
246 | if (st->ss->external) |
247 | use_partitions = 0; | |
8382f19b NB |
248 | np = strchr(info.name, ':'); |
249 | if (np) | |
250 | np++; | |
251 | else | |
252 | np = info.name; | |
253 | devnum = strtoul(np, &ep, 10); | |
254 | if (ep > np && *ep == 0) { | |
255 | /* This is a number. Let check that it is unused. */ | |
256 | if (mddev_busy(use_partitions ? (-1-devnum) : devnum)) | |
257 | devnum = -1; | |
258 | } else | |
259 | devnum = -1; | |
260 | ||
25dbe93a NB |
261 | if (match) |
262 | nm = match->devname; | |
263 | else { | |
264 | sprintf(nbuf, "/dev/md/%s", np); | |
265 | nm = nbuf; | |
266 | } | |
267 | if (stat(nm, &stb) == 0 && | |
268 | S_ISBLK(stb.st_mode) && | |
269 | major(stb.st_rdev) == (use_partitions ? | |
270 | get_mdp_major() : MD_MAJOR)) { | |
271 | if (use_partitions) | |
272 | devnum = minor(stb.st_rdev) >> MdpMinorShift; | |
273 | else | |
274 | devnum = minor(stb.st_rdev); | |
275 | if (mddev_busy(use_partitions ? (-1-devnum) : devnum)) | |
276 | devnum = -1; | |
277 | } | |
278 | ||
8382f19b NB |
279 | if (devnum < 0) { |
280 | /* Haven't found anything yet, choose something free */ | |
63152c1b NB |
281 | devnum = find_free_devnum(use_partitions); |
282 | ||
283 | if (devnum == NoMdDev) { | |
8382f19b NB |
284 | fprintf(stderr, Name |
285 | ": No spare md devices!!\n"); | |
286 | return 2; | |
287 | } | |
63152c1b NB |
288 | } else |
289 | devnum = use_partitions ? (-1-devnum) : devnum; | |
8382f19b NB |
290 | } |
291 | mdfd = open_mddev_devnum(match ? match->devname : NULL, | |
292 | devnum, | |
293 | info.name, | |
9a02c62a | 294 | chosen_name, autof >> 3); |
8382f19b NB |
295 | if (mdfd < 0) { |
296 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
297 | chosen_name, strerror(errno)); | |
298 | return 2; | |
299 | } | |
f35f2525 N |
300 | sysfs_init(&info, mdfd, 0); |
301 | ||
8382f19b NB |
302 | /* 5/ Find out if array already exists */ |
303 | if (! mddev_busy(devnum)) { | |
304 | /* 5a/ if it does not */ | |
305 | /* - choose a name, from mdadm.conf or 'name' field in array. */ | |
306 | /* - create the array */ | |
307 | /* - add the device */ | |
7e0f6979 | 308 | struct mdinfo *sra; |
8382f19b | 309 | |
f35f2525 N |
310 | if (set_array_info(mdfd, st, &info) != 0) { |
311 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", | |
8382f19b NB |
312 | chosen_name, strerror(errno)); |
313 | close(mdfd); | |
314 | return 2; | |
315 | } | |
7801ac20 | 316 | |
7801ac20 N |
317 | info.disk.major = major(stb.st_rdev); |
318 | info.disk.minor = minor(stb.st_rdev); | |
f35f2525 | 319 | if (add_disk(mdfd, st, &info, &info) != 0) { |
8382f19b NB |
320 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", |
321 | devname, chosen_name, strerror(errno)); | |
322 | ioctl(mdfd, STOP_ARRAY, 0); | |
323 | close(mdfd); | |
324 | return 2; | |
325 | } | |
326 | sra = sysfs_read(mdfd, devnum, GET_DEVS); | |
06c7f68e | 327 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { |
8382f19b NB |
328 | /* It really should be 'none' - must be old buggy |
329 | * kernel, and mdadm -I may not be able to complete. | |
330 | * So reject it. | |
331 | */ | |
332 | ioctl(mdfd, STOP_ARRAY, NULL); | |
333 | fprintf(stderr, Name | |
334 | ": You have an old buggy kernel which cannot support\n" | |
335 | " --incremental reliably. Aborting.\n"); | |
336 | close(mdfd); | |
337 | sysfs_free(sra); | |
338 | return 2; | |
339 | } | |
f35f2525 | 340 | sysfs_free(sra); |
8382f19b NB |
341 | } else { |
342 | /* 5b/ if it does */ | |
343 | /* - check one drive in array to make sure metadata is a reasonably */ | |
344 | /* close match. Reject if not (e.g. different type) */ | |
345 | /* - add the device */ | |
346 | char dn[20]; | |
347 | int dfd2; | |
8382f19b | 348 | int err; |
7e0f6979 | 349 | struct mdinfo *sra; |
3da92f27 | 350 | struct supertype *st2; |
f35f2525 | 351 | struct mdinfo info2; |
b8ac1967 | 352 | sra = sysfs_read(mdfd, devnum, (GET_DEVS | GET_STATE)); |
2cdb6489 | 353 | |
06c7f68e NB |
354 | sprintf(dn, "%d:%d", sra->devs->disk.major, |
355 | sra->devs->disk.minor); | |
8382f19b | 356 | dfd2 = dev_open(dn, O_RDONLY); |
3da92f27 | 357 | st2 = dup_super(st); |
b8ac1967 NB |
358 | if (st2->ss->load_super(st2, dfd2, NULL) || |
359 | st->ss->compare_super(st, st2) != 0) { | |
8382f19b | 360 | fprintf(stderr, Name |
b8ac1967 NB |
361 | ": metadata mismatch between %s and " |
362 | "chosen array %s\n", | |
363 | devname, chosen_name); | |
8382f19b NB |
364 | close(mdfd); |
365 | close(dfd2); | |
366 | return 2; | |
367 | } | |
368 | close(dfd2); | |
f35f2525 | 369 | memset(&info2, 0, sizeof(info2)); |
3da92f27 NB |
370 | st2->ss->getinfo_super(st2, &info2); |
371 | st2->ss->free_super(st2); | |
8382f19b NB |
372 | if (info.array.level != info2.array.level || |
373 | memcmp(info.uuid, info2.uuid, 16) != 0 || | |
374 | info.array.raid_disks != info2.array.raid_disks) { | |
375 | fprintf(stderr, Name | |
376 | ": unexpected difference between %s and %s.\n", | |
377 | chosen_name, devname); | |
378 | close(mdfd); | |
379 | return 2; | |
380 | } | |
7801ac20 N |
381 | info2.disk.major = major(stb.st_rdev); |
382 | info2.disk.minor = minor(stb.st_rdev); | |
383 | err = add_disk(mdfd, st2, sra, &info2); | |
8382f19b NB |
384 | if (err < 0 && errno == EBUSY) { |
385 | /* could be another device present with the same | |
386 | * disk.number. Find and reject any such | |
387 | */ | |
388 | find_reject(mdfd, st, sra, info.disk.number, | |
389 | info.events, verbose, chosen_name); | |
7801ac20 | 390 | err = add_disk(mdfd, st2, sra, &info2); |
8382f19b NB |
391 | } |
392 | if (err < 0) { | |
393 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
394 | devname, chosen_name, strerror(errno)); | |
395 | close(mdfd); | |
396 | return 2; | |
397 | } | |
398 | } | |
399 | /* 6/ Make sure /var/run/mdadm.map contains this array. */ | |
400 | map_update(&map, devnum, | |
1522c538 | 401 | info.text_version, |
8382f19b NB |
402 | info.uuid, chosen_name); |
403 | ||
404 | /* 7/ Is there enough devices to possibly start the array? */ | |
405 | /* 7a/ if not, finish with success. */ | |
352452c3 N |
406 | if (info.array.level == LEVEL_CONTAINER) { |
407 | /* Try to assemble within the container */ | |
408 | return Incremental(chosen_name, verbose, runstop, | |
409 | NULL, homehost, autof); | |
410 | } | |
f8409e54 | 411 | avail = NULL; |
8382f19b NB |
412 | active_disks = count_active(st, mdfd, &avail, &info); |
413 | if (enough(info.array.level, info.array.raid_disks, | |
414 | info.array.layout, info.array.state & 1, | |
415 | avail, active_disks) == 0) { | |
416 | free(avail); | |
417 | if (verbose >= 0) | |
418 | fprintf(stderr, Name | |
419 | ": %s attached to %s, not enough to start (%d).\n", | |
420 | devname, chosen_name, active_disks); | |
421 | close(mdfd); | |
422 | return 0; | |
423 | } | |
424 | free(avail); | |
425 | ||
426 | /* 7b/ if yes, */ | |
427 | /* - if number of OK devices match expected, or -R and there */ | |
428 | /* are enough, */ | |
429 | /* + add any bitmap file */ | |
430 | /* + start the array (auto-readonly). */ | |
431 | { | |
432 | mdu_array_info_t ainf; | |
433 | ||
434 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
435 | if (verbose >= 0) | |
436 | fprintf(stderr, Name | |
437 | ": %s attached to %s which is already active.\n", | |
438 | devname, chosen_name); | |
439 | close (mdfd); | |
440 | return 0; | |
441 | } | |
442 | } | |
443 | if (runstop > 0 || active_disks >= info.array.working_disks) { | |
7e0f6979 | 444 | struct mdinfo *sra; |
8382f19b NB |
445 | /* Let's try to start it */ |
446 | if (match && match->bitmap_file) { | |
447 | int bmfd = open(match->bitmap_file, O_RDWR); | |
448 | if (bmfd < 0) { | |
449 | fprintf(stderr, Name | |
450 | ": Could not open bitmap file %s.\n", | |
451 | match->bitmap_file); | |
452 | close(mdfd); | |
453 | return 1; | |
454 | } | |
455 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
456 | close(bmfd); | |
457 | fprintf(stderr, Name | |
458 | ": Failed to set bitmapfile for %s.\n", | |
459 | chosen_name); | |
460 | close(mdfd); | |
461 | return 1; | |
462 | } | |
463 | close(bmfd); | |
464 | } | |
465 | sra = sysfs_read(mdfd, devnum, 0); | |
466 | if (sra == NULL || active_disks >= info.array.working_disks) | |
467 | rv = ioctl(mdfd, RUN_ARRAY, NULL); | |
468 | else | |
469 | rv = sysfs_set_str(sra, NULL, | |
470 | "array_state", "read-auto"); | |
471 | if (rv == 0) { | |
472 | if (verbose >= 0) | |
473 | fprintf(stderr, Name | |
474 | ": %s attached to %s, which has been started.\n", | |
475 | devname, chosen_name); | |
476 | rv = 0; | |
477 | } else { | |
478 | fprintf(stderr, Name | |
479 | ": %s attached to %s, but failed to start: %s.\n", | |
480 | devname, chosen_name, strerror(errno)); | |
481 | rv = 1; | |
482 | } | |
483 | } else { | |
484 | if (verbose >= 0) | |
485 | fprintf(stderr, Name | |
486 | ": %s attached to %s, not enough to start safely.\n", | |
487 | devname, chosen_name); | |
488 | rv = 0; | |
489 | } | |
490 | close(mdfd); | |
491 | return rv; | |
492 | } | |
493 | ||
7e0f6979 | 494 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
495 | int number, __u64 events, int verbose, |
496 | char *array_name) | |
497 | { | |
3da92f27 | 498 | /* Find a device attached to this array with a disk.number of number |
8382f19b NB |
499 | * and events less than the passed events, and remove the device. |
500 | */ | |
06c7f68e | 501 | struct mdinfo *d; |
8382f19b NB |
502 | mdu_array_info_t ra; |
503 | ||
504 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
505 | return; /* not safe to remove from active arrays | |
506 | * without thinking more */ | |
507 | ||
508 | for (d = sra->devs; d ; d = d->next) { | |
509 | char dn[10]; | |
510 | int dfd; | |
8382f19b | 511 | struct mdinfo info; |
06c7f68e | 512 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
513 | dfd = dev_open(dn, O_RDONLY); |
514 | if (dfd < 0) | |
515 | continue; | |
3da92f27 | 516 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
517 | close(dfd); |
518 | continue; | |
519 | } | |
3da92f27 NB |
520 | st->ss->getinfo_super(st, &info); |
521 | st->ss->free_super(st); | |
8382f19b NB |
522 | close(dfd); |
523 | ||
524 | if (info.disk.number != number || | |
525 | info.events >= events) | |
526 | continue; | |
527 | ||
06c7f68e | 528 | if (d->disk.raid_disk > -1) |
8382f19b NB |
529 | sysfs_set_str(sra, d, "slot", "none"); |
530 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
531 | if (verbose >= 0) | |
532 | fprintf(stderr, Name | |
533 | ": removing old device %s from %s\n", | |
06c7f68e | 534 | d->sys_name+4, array_name); |
8382f19b NB |
535 | } |
536 | } | |
537 | ||
538 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
539 | struct mdinfo *bestinfo) | |
540 | { | |
541 | /* count how many devices in sra think they are active */ | |
06c7f68e | 542 | struct mdinfo *d; |
8382f19b NB |
543 | int cnt = 0, cnt1 = 0; |
544 | __u64 max_events = 0; | |
7e0f6979 | 545 | struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); |
8382f19b NB |
546 | char *avail = NULL; |
547 | ||
548 | for (d = sra->devs ; d ; d = d->next) { | |
549 | char dn[30]; | |
550 | int dfd; | |
8382f19b NB |
551 | int ok; |
552 | struct mdinfo info; | |
553 | ||
06c7f68e | 554 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
555 | dfd = dev_open(dn, O_RDONLY); |
556 | if (dfd < 0) | |
557 | continue; | |
3da92f27 | 558 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
559 | close(dfd); |
560 | if (ok != 0) | |
561 | continue; | |
3da92f27 | 562 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
563 | if (info.disk.state & (1<<MD_DISK_SYNC)) |
564 | { | |
565 | if (avail == NULL) { | |
566 | avail = malloc(info.array.raid_disks); | |
567 | memset(avail, 0, info.array.raid_disks); | |
568 | } | |
569 | if (cnt == 0) { | |
570 | cnt++; | |
571 | max_events = info.events; | |
572 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 573 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
574 | } else if (info.events == max_events) { |
575 | cnt++; | |
576 | avail[info.disk.raid_disk] = 2; | |
577 | } else if (info.events == max_events-1) { | |
578 | cnt1++; | |
579 | avail[info.disk.raid_disk] = 1; | |
580 | } else if (info.events < max_events - 1) | |
581 | ; | |
582 | else if (info.events == max_events+1) { | |
583 | int i; | |
584 | cnt1 = cnt; | |
585 | cnt = 1; | |
586 | max_events = info.events; | |
587 | for (i=0; i<info.array.raid_disks; i++) | |
588 | if (avail[i]) | |
589 | avail[i]--; | |
590 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 591 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
592 | } else { /* info.events much bigger */ |
593 | cnt = 1; cnt1 = 0; | |
594 | memset(avail, 0, info.disk.raid_disk); | |
595 | max_events = info.events; | |
3da92f27 | 596 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
597 | } |
598 | } | |
3da92f27 | 599 | st->ss->free_super(st); |
8382f19b NB |
600 | } |
601 | return cnt + cnt1; | |
602 | } | |
603 | ||
604 | void RebuildMap(void) | |
605 | { | |
606 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
607 | struct mdstat_ent *md; | |
608 | struct map_ent *map = NULL; | |
609 | int mdp = get_mdp_major(); | |
610 | ||
611 | for (md = mdstat ; md ; md = md->next) { | |
7e0f6979 | 612 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS); |
06c7f68e | 613 | struct mdinfo *sd; |
8382f19b NB |
614 | |
615 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
616 | char dn[30]; | |
617 | int dfd; | |
618 | int ok; | |
619 | struct supertype *st; | |
620 | char *path; | |
8382f19b NB |
621 | struct mdinfo info; |
622 | ||
06c7f68e | 623 | sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor); |
8382f19b NB |
624 | dfd = dev_open(dn, O_RDONLY); |
625 | if (dfd < 0) | |
626 | continue; | |
627 | st = guess_super(dfd); | |
628 | if ( st == NULL) | |
629 | ok = -1; | |
630 | else | |
3da92f27 | 631 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
632 | close(dfd); |
633 | if (ok != 0) | |
634 | continue; | |
3da92f27 | 635 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
636 | if (md->devnum > 0) |
637 | path = map_dev(MD_MAJOR, md->devnum, 0); | |
638 | else | |
639 | path = map_dev(mdp, (-1-md->devnum)<< 6, 0); | |
1522c538 NB |
640 | map_add(&map, md->devnum, |
641 | info.text_version, | |
8382f19b | 642 | info.uuid, path ? : "/unknown"); |
3da92f27 | 643 | st->ss->free_super(st); |
8382f19b NB |
644 | break; |
645 | } | |
646 | } | |
647 | map_write(map); | |
648 | map_free(map); | |
649 | } | |
650 | ||
651 | int IncrementalScan(int verbose) | |
652 | { | |
653 | /* look at every device listed in the 'map' file. | |
654 | * If one is found that is not running then: | |
655 | * look in mdadm.conf for bitmap file. | |
656 | * if one exists, but array has none, add it. | |
657 | * try to start array in auto-readonly mode | |
658 | */ | |
659 | struct map_ent *mapl = NULL; | |
660 | struct map_ent *me; | |
661 | mddev_ident_t devs, mddev; | |
662 | int rv = 0; | |
663 | ||
664 | map_read(&mapl); | |
665 | devs = conf_get_ident(NULL); | |
666 | ||
667 | for (me = mapl ; me ; me = me->next) { | |
668 | char path[1024]; | |
669 | mdu_array_info_t array; | |
670 | mdu_bitmap_file_t bmf; | |
7e0f6979 | 671 | struct mdinfo *sra; |
9a02c62a NB |
672 | int mdfd = open_mddev_devnum(me->path, me->devnum, |
673 | NULL, path, 0); | |
8382f19b NB |
674 | if (mdfd < 0) |
675 | continue; | |
676 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
677 | errno != ENODEV) { | |
678 | close(mdfd); | |
679 | continue; | |
680 | } | |
681 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
682 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
683 | if (strcmp(mddev->devname, me->path) == 0) | |
684 | break; | |
685 | if (mddev && mddev->bitmap_file) { | |
686 | /* | |
687 | * Note: early kernels will wrongly fail this, so it | |
688 | * is a hint only | |
689 | */ | |
690 | int added = -1; | |
691 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
692 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
693 | if (bmfd >= 0) { | |
694 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
695 | bmfd); | |
696 | close(bmfd); | |
697 | } | |
698 | } | |
699 | if (verbose >= 0) { | |
700 | if (added == 0) | |
701 | fprintf(stderr, Name | |
702 | ": Added bitmap %s to %s\n", | |
703 | mddev->bitmap_file, me->path); | |
704 | else if (errno != EEXIST) | |
705 | fprintf(stderr, Name | |
706 | ": Failed to add bitmap to %s: %s\n", | |
707 | me->path, strerror(errno)); | |
708 | } | |
709 | } | |
710 | sra = sysfs_read(mdfd, 0, 0); | |
711 | if (sra) { | |
712 | if (sysfs_set_str(sra, NULL, | |
713 | "array_state", "read-auto") == 0) { | |
714 | if (verbose >= 0) | |
715 | fprintf(stderr, Name | |
716 | ": started array %s\n", | |
717 | me->path); | |
718 | } else { | |
719 | fprintf(stderr, Name | |
720 | ": failed to start array %s: %s\n", | |
721 | me->path, strerror(errno)); | |
722 | rv = 1; | |
723 | } | |
724 | } | |
725 | } | |
726 | return rv; | |
727 | } | |
598f0d58 | 728 | |
5f2aace8 DW |
729 | static char *container2devname(char *devname) |
730 | { | |
731 | int fd = open(devname, O_RDONLY); | |
732 | char *mdname = NULL; | |
733 | ||
d7161f3b | 734 | if (fd >= 0) { |
5f2aace8 DW |
735 | mdname = devnum2devname(fd2devnum(fd)); |
736 | close(fd); | |
737 | } | |
738 | ||
739 | return mdname; | |
740 | } | |
741 | ||
598f0d58 NB |
742 | int Incremental_container(struct supertype *st, char *devname, int verbose, |
743 | int runstop, int autof) | |
744 | { | |
745 | /* Collect the contents of this container and for each | |
746 | * array, choose a device name and assemble the array. | |
747 | */ | |
748 | ||
749 | struct mdinfo *list = st->ss->container_content(st); | |
750 | struct mdinfo *ra; | |
5f2aace8 DW |
751 | char *mdname = container2devname(devname); |
752 | ||
753 | if (!mdname) { | |
754 | fprintf(stderr, Name": failed to determine device name\n"); | |
755 | return 2; | |
756 | } | |
598f0d58 NB |
757 | |
758 | for (ra = list ; ra ; ra = ra->next) { | |
598f0d58 NB |
759 | struct mdinfo *dev; |
760 | int devnum = -1; | |
761 | int mdfd; | |
762 | char chosen_name[1024]; | |
763 | int usepart = 1; | |
764 | char *n; | |
765 | int working = 0; | |
766 | ||
767 | if ((autof&7) == 3 || (autof&7) == 5) | |
768 | usepart = 0; | |
769 | ||
770 | n = ra->name; | |
771 | if (*n == 'd') | |
772 | n++; | |
773 | if (*n) { | |
774 | devnum = strtoul(n, &n, 10); | |
775 | if (devnum >= 0 && (*n == 0 || *n == ' ')) { | |
776 | /* Use this devnum */ | |
777 | usepart = (ra->name[0] == 'd'); | |
778 | if (mddev_busy(usepart ? (-1-devnum) : devnum)) | |
779 | devnum = -1; | |
780 | } else | |
781 | devnum = -1; | |
782 | } | |
783 | ||
25dbe93a NB |
784 | if (devnum < 0) { |
785 | char *nm = ra->name; | |
786 | char nbuf[1024]; | |
787 | struct stat stb; | |
788 | if (strchr(nm, ':')) | |
789 | nm = strchr(nm, ':')+1; | |
790 | sprintf(nbuf, "/dev/md/%s", nm); | |
791 | ||
792 | if (stat(nbuf, &stb) == 0 && | |
793 | S_ISBLK(stb.st_mode) && | |
794 | major(stb.st_rdev) == (usepart ? | |
795 | get_mdp_major() : MD_MAJOR)){ | |
796 | if (usepart) | |
797 | devnum = minor(stb.st_rdev) | |
798 | >> MdpMinorShift; | |
799 | else | |
800 | devnum = minor(stb.st_rdev); | |
801 | if (mddev_busy(usepart ? (-1-devnum) : devnum)) | |
802 | devnum = -1; | |
803 | } | |
804 | } | |
805 | ||
598f0d58 NB |
806 | if (devnum >= 0) |
807 | devnum = usepart ? (-1-devnum) : devnum; | |
808 | else | |
809 | devnum = find_free_devnum(usepart); | |
810 | mdfd = open_mddev_devnum(NULL, devnum, ra->name, | |
811 | chosen_name, autof>>3); | |
812 | ||
813 | if (mdfd < 0) { | |
814 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
815 | chosen_name, strerror(errno)); | |
816 | return 2; | |
817 | } | |
818 | ||
f35f2525 N |
819 | sysfs_init(ra, mdfd, 0); |
820 | sysfs_set_array(ra, md_get_version(mdfd)); | |
2318b9f0 | 821 | for (dev = ra->devs; dev; dev = dev->next) |
f35f2525 | 822 | if (sysfs_add_disk(ra, dev) == 0) |
598f0d58 | 823 | working++; |
2318b9f0 | 824 | |
598f0d58 NB |
825 | if (runstop > 0 || working >= ra->array.working_disks) { |
826 | switch(ra->array.level) { | |
827 | case LEVEL_LINEAR: | |
828 | case LEVEL_MULTIPATH: | |
829 | case 0: | |
f35f2525 | 830 | sysfs_set_str(ra, NULL, "array_state", |
598f0d58 NB |
831 | "active"); |
832 | break; | |
833 | default: | |
f35f2525 | 834 | sysfs_set_str(ra, NULL, "array_state", |
598f0d58 | 835 | "readonly"); |
75aa18b5 | 836 | /* start mdmon if needed. */ |
8850ee3e N |
837 | if (!mdmon_running(st->container_dev)) |
838 | start_mdmon(st->container_dev); | |
839 | ping_monitor(devnum2devname(st->container_dev)); | |
598f0d58 NB |
840 | break; |
841 | } | |
f35f2525 | 842 | sysfs_set_safemode(ra, ra->safe_mode_delay); |
598f0d58 NB |
843 | if (verbose >= 0) |
844 | printf("Started %s with %d devices\n", | |
845 | chosen_name, working); | |
75aa18b5 | 846 | /* FIXME should have an O_EXCL and wait for read-auto */ |
598f0d58 NB |
847 | } else |
848 | if (verbose >= 0) | |
849 | printf("%s assembled with %d devices but " | |
850 | "not started\n", | |
851 | chosen_name, working); | |
852 | close(mdfd); | |
853 | } | |
854 | return 0; | |
855 | } |