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