]>
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 | 378 | close(mdfd); |
97590376 | 379 | sysfs_uevent(&info, "change"); |
c5afc314 N |
380 | if (verbose >= 0) |
381 | fprintf(stderr, Name | |
382 | ": container %s now has %d devices\n", | |
383 | chosen_name, info.array.working_disks); | |
352452c3 N |
384 | return Incremental(chosen_name, verbose, runstop, |
385 | NULL, homehost, autof); | |
386 | } | |
f8409e54 | 387 | avail = NULL; |
8382f19b NB |
388 | active_disks = count_active(st, mdfd, &avail, &info); |
389 | if (enough(info.array.level, info.array.raid_disks, | |
390 | info.array.layout, info.array.state & 1, | |
391 | avail, active_disks) == 0) { | |
392 | free(avail); | |
393 | if (verbose >= 0) | |
394 | fprintf(stderr, Name | |
395 | ": %s attached to %s, not enough to start (%d).\n", | |
396 | devname, chosen_name, active_disks); | |
397 | close(mdfd); | |
398 | return 0; | |
399 | } | |
400 | free(avail); | |
401 | ||
402 | /* 7b/ if yes, */ | |
403 | /* - if number of OK devices match expected, or -R and there */ | |
404 | /* are enough, */ | |
405 | /* + add any bitmap file */ | |
406 | /* + start the array (auto-readonly). */ | |
407 | { | |
408 | mdu_array_info_t ainf; | |
409 | ||
410 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
411 | if (verbose >= 0) | |
412 | fprintf(stderr, Name | |
413 | ": %s attached to %s which is already active.\n", | |
414 | devname, chosen_name); | |
415 | close (mdfd); | |
416 | return 0; | |
417 | } | |
418 | } | |
419 | if (runstop > 0 || active_disks >= info.array.working_disks) { | |
7e0f6979 | 420 | struct mdinfo *sra; |
8382f19b NB |
421 | /* Let's try to start it */ |
422 | if (match && match->bitmap_file) { | |
423 | int bmfd = open(match->bitmap_file, O_RDWR); | |
424 | if (bmfd < 0) { | |
425 | fprintf(stderr, Name | |
426 | ": Could not open bitmap file %s.\n", | |
427 | match->bitmap_file); | |
428 | close(mdfd); | |
429 | return 1; | |
430 | } | |
431 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
432 | close(bmfd); | |
433 | fprintf(stderr, Name | |
434 | ": Failed to set bitmapfile for %s.\n", | |
435 | chosen_name); | |
436 | close(mdfd); | |
437 | return 1; | |
438 | } | |
439 | close(bmfd); | |
440 | } | |
441 | sra = sysfs_read(mdfd, devnum, 0); | |
7b403fef | 442 | if ((sra == NULL || active_disks >= info.array.working_disks) |
215bb3f7 | 443 | && trustworthy != FOREIGN) |
8382f19b NB |
444 | rv = ioctl(mdfd, RUN_ARRAY, NULL); |
445 | else | |
446 | rv = sysfs_set_str(sra, NULL, | |
447 | "array_state", "read-auto"); | |
448 | if (rv == 0) { | |
449 | if (verbose >= 0) | |
450 | fprintf(stderr, Name | |
451 | ": %s attached to %s, which has been started.\n", | |
452 | devname, chosen_name); | |
453 | rv = 0; | |
454 | } else { | |
455 | fprintf(stderr, Name | |
456 | ": %s attached to %s, but failed to start: %s.\n", | |
457 | devname, chosen_name, strerror(errno)); | |
458 | rv = 1; | |
459 | } | |
460 | } else { | |
461 | if (verbose >= 0) | |
462 | fprintf(stderr, Name | |
463 | ": %s attached to %s, not enough to start safely.\n", | |
464 | devname, chosen_name); | |
465 | rv = 0; | |
466 | } | |
467 | close(mdfd); | |
468 | return rv; | |
469 | } | |
470 | ||
7e0f6979 | 471 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
472 | int number, __u64 events, int verbose, |
473 | char *array_name) | |
474 | { | |
3da92f27 | 475 | /* Find a device attached to this array with a disk.number of number |
8382f19b NB |
476 | * and events less than the passed events, and remove the device. |
477 | */ | |
06c7f68e | 478 | struct mdinfo *d; |
8382f19b NB |
479 | mdu_array_info_t ra; |
480 | ||
481 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
482 | return; /* not safe to remove from active arrays | |
483 | * without thinking more */ | |
484 | ||
485 | for (d = sra->devs; d ; d = d->next) { | |
486 | char dn[10]; | |
487 | int dfd; | |
8382f19b | 488 | struct mdinfo info; |
06c7f68e | 489 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
490 | dfd = dev_open(dn, O_RDONLY); |
491 | if (dfd < 0) | |
492 | continue; | |
3da92f27 | 493 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
494 | close(dfd); |
495 | continue; | |
496 | } | |
3da92f27 NB |
497 | st->ss->getinfo_super(st, &info); |
498 | st->ss->free_super(st); | |
8382f19b NB |
499 | close(dfd); |
500 | ||
501 | if (info.disk.number != number || | |
502 | info.events >= events) | |
503 | continue; | |
504 | ||
06c7f68e | 505 | if (d->disk.raid_disk > -1) |
8382f19b NB |
506 | sysfs_set_str(sra, d, "slot", "none"); |
507 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
508 | if (verbose >= 0) | |
509 | fprintf(stderr, Name | |
510 | ": removing old device %s from %s\n", | |
06c7f68e | 511 | d->sys_name+4, array_name); |
8382f19b NB |
512 | } |
513 | } | |
514 | ||
515 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
516 | struct mdinfo *bestinfo) | |
517 | { | |
518 | /* count how many devices in sra think they are active */ | |
06c7f68e | 519 | struct mdinfo *d; |
8382f19b NB |
520 | int cnt = 0, cnt1 = 0; |
521 | __u64 max_events = 0; | |
7e0f6979 | 522 | struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); |
8382f19b NB |
523 | char *avail = NULL; |
524 | ||
525 | for (d = sra->devs ; d ; d = d->next) { | |
526 | char dn[30]; | |
527 | int dfd; | |
8382f19b NB |
528 | int ok; |
529 | struct mdinfo info; | |
530 | ||
06c7f68e | 531 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
532 | dfd = dev_open(dn, O_RDONLY); |
533 | if (dfd < 0) | |
534 | continue; | |
3da92f27 | 535 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
536 | close(dfd); |
537 | if (ok != 0) | |
538 | continue; | |
3da92f27 | 539 | st->ss->getinfo_super(st, &info); |
43aaf431 DL |
540 | if (!avail) { |
541 | avail = malloc(info.array.raid_disks); | |
542 | if (!avail) { | |
543 | fprintf(stderr, Name ": out of memory.\n"); | |
544 | exit(1); | |
545 | } | |
546 | memset(avail, 0, info.array.raid_disks); | |
547 | *availp = avail; | |
548 | } | |
549 | ||
8382f19b NB |
550 | if (info.disk.state & (1<<MD_DISK_SYNC)) |
551 | { | |
8382f19b NB |
552 | if (cnt == 0) { |
553 | cnt++; | |
554 | max_events = info.events; | |
555 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 556 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
557 | } else if (info.events == max_events) { |
558 | cnt++; | |
559 | avail[info.disk.raid_disk] = 2; | |
560 | } else if (info.events == max_events-1) { | |
561 | cnt1++; | |
562 | avail[info.disk.raid_disk] = 1; | |
563 | } else if (info.events < max_events - 1) | |
564 | ; | |
565 | else if (info.events == max_events+1) { | |
566 | int i; | |
567 | cnt1 = cnt; | |
568 | cnt = 1; | |
569 | max_events = info.events; | |
570 | for (i=0; i<info.array.raid_disks; i++) | |
571 | if (avail[i]) | |
572 | avail[i]--; | |
573 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 574 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
575 | } else { /* info.events much bigger */ |
576 | cnt = 1; cnt1 = 0; | |
577 | memset(avail, 0, info.disk.raid_disk); | |
578 | max_events = info.events; | |
3da92f27 | 579 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
580 | } |
581 | } | |
3da92f27 | 582 | st->ss->free_super(st); |
8382f19b NB |
583 | } |
584 | return cnt + cnt1; | |
585 | } | |
586 | ||
587 | void RebuildMap(void) | |
588 | { | |
589 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
590 | struct mdstat_ent *md; | |
591 | struct map_ent *map = NULL; | |
592 | int mdp = get_mdp_major(); | |
593 | ||
594 | for (md = mdstat ; md ; md = md->next) { | |
7e0f6979 | 595 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS); |
06c7f68e | 596 | struct mdinfo *sd; |
8382f19b NB |
597 | |
598 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
599 | char dn[30]; | |
600 | int dfd; | |
601 | int ok; | |
602 | struct supertype *st; | |
603 | char *path; | |
8382f19b NB |
604 | struct mdinfo info; |
605 | ||
06c7f68e | 606 | sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor); |
8382f19b NB |
607 | dfd = dev_open(dn, O_RDONLY); |
608 | if (dfd < 0) | |
609 | continue; | |
610 | st = guess_super(dfd); | |
611 | if ( st == NULL) | |
612 | ok = -1; | |
613 | else | |
3da92f27 | 614 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
615 | close(dfd); |
616 | if (ok != 0) | |
617 | continue; | |
3da92f27 | 618 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
619 | if (md->devnum > 0) |
620 | path = map_dev(MD_MAJOR, md->devnum, 0); | |
621 | else | |
622 | path = map_dev(mdp, (-1-md->devnum)<< 6, 0); | |
1522c538 NB |
623 | map_add(&map, md->devnum, |
624 | info.text_version, | |
8382f19b | 625 | info.uuid, path ? : "/unknown"); |
3da92f27 | 626 | st->ss->free_super(st); |
8382f19b NB |
627 | break; |
628 | } | |
97590376 | 629 | sysfs_free(sra); |
8382f19b NB |
630 | } |
631 | map_write(map); | |
632 | map_free(map); | |
97590376 N |
633 | for (md = mdstat ; md ; md = md->next) { |
634 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_VERSION); | |
635 | sysfs_uevent(sra, "change"); | |
636 | sysfs_free(sra); | |
637 | } | |
8382f19b NB |
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) { | |
8382f19b NB |
657 | mdu_array_info_t array; |
658 | mdu_bitmap_file_t bmf; | |
7e0f6979 | 659 | struct mdinfo *sra; |
215bb3f7 N |
660 | int mdfd = open_mddev(me->path, 0); |
661 | ||
8382f19b NB |
662 | if (mdfd < 0) |
663 | continue; | |
664 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
665 | errno != ENODEV) { | |
666 | close(mdfd); | |
667 | continue; | |
668 | } | |
669 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
670 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
fe056d1f N |
671 | if (mddev->devname |
672 | && strcmp(mddev->devname, me->path) == 0) | |
8382f19b NB |
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 | } | |
598f0d58 | 717 | |
1771a6e2 N |
718 | static char *container2devname(char *devname) |
719 | { | |
720 | char *mdname = NULL; | |
721 | ||
722 | if (devname[0] == '/') { | |
723 | int fd = open(devname, O_RDONLY); | |
724 | if (fd >= 0) { | |
725 | mdname = devnum2devname(fd2devnum(fd)); | |
726 | close(fd); | |
727 | } | |
728 | } else { | |
729 | int uuid[4]; | |
730 | struct map_ent *mp, *map = NULL; | |
731 | ||
732 | if (!parse_uuid(devname, uuid)) | |
733 | return mdname; | |
734 | mp = map_by_uuid(&map, uuid); | |
735 | if (mp) | |
736 | mdname = devnum2devname(mp->devnum); | |
737 | map_free(map); | |
738 | } | |
739 | ||
740 | return mdname; | |
741 | } | |
742 | ||
598f0d58 | 743 | int Incremental_container(struct supertype *st, char *devname, int verbose, |
215bb3f7 | 744 | int runstop, int autof, int trustworthy) |
598f0d58 NB |
745 | { |
746 | /* Collect the contents of this container and for each | |
747 | * array, choose a device name and assemble the array. | |
748 | */ | |
749 | ||
750 | struct mdinfo *list = st->ss->container_content(st); | |
751 | struct mdinfo *ra; | |
752 | ||
753 | for (ra = list ; ra ; ra = ra->next) { | |
04c0634e | 754 | struct mdinfo *dev, *sra; |
598f0d58 NB |
755 | int mdfd; |
756 | char chosen_name[1024]; | |
c5afc314 N |
757 | int working = 0, preexist = 0; |
758 | struct map_ent *mp, *map = NULL; | |
dbb44303 | 759 | struct mddev_ident_s *match = NULL; |
598f0d58 | 760 | |
c5afc314 | 761 | mp = map_by_uuid(&map, ra->uuid); |
d7288ddc | 762 | |
dbb44303 N |
763 | if (!mp) { |
764 | ||
765 | /* Check in mdadm.conf for devices == devname and | |
766 | * member == ra->text_version after second slash. | |
767 | */ | |
768 | char *sub = strchr(ra->text_version+1, '/'); | |
769 | struct mddev_ident_s *array_list; | |
770 | if (sub) { | |
771 | sub++; | |
772 | array_list = conf_get_ident(NULL); | |
773 | } else | |
774 | array_list = NULL; | |
775 | for(; array_list ; array_list = array_list->next) { | |
dbb44303 N |
776 | char *dn; |
777 | if (array_list->member == NULL || | |
778 | array_list->container == NULL) | |
779 | continue; | |
780 | if (strcmp(array_list->member, sub) != 0) | |
781 | continue; | |
71d60c48 DW |
782 | if (array_list->uuid_set && |
783 | !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid)) | |
784 | continue; | |
1771a6e2 N |
785 | dn = container2devname(array_list->container); |
786 | if (dn == NULL) | |
dbb44303 | 787 | continue; |
dbb44303 N |
788 | if (strncmp(dn, ra->text_version+1, |
789 | strlen(dn)) != 0 || | |
790 | ra->text_version[strlen(dn)+1] != '/') { | |
791 | free(dn); | |
792 | continue; | |
793 | } | |
794 | free(dn); | |
795 | /* we have a match */ | |
796 | match = array_list; | |
71d60c48 DW |
797 | if (verbose>0) |
798 | fprintf(stderr, Name ": match found for member %s\n", | |
799 | array_list->member); | |
dbb44303 N |
800 | break; |
801 | } | |
802 | } | |
803 | ||
215bb3f7 N |
804 | mdfd = create_mddev(match ? match->devname : NULL, |
805 | ra->name, | |
806 | autof, | |
807 | trustworthy, | |
808 | chosen_name); | |
598f0d58 NB |
809 | |
810 | if (mdfd < 0) { | |
811 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
812 | chosen_name, strerror(errno)); | |
813 | return 2; | |
814 | } | |
815 | ||
04c0634e | 816 | |
f35f2525 | 817 | sysfs_init(ra, mdfd, 0); |
04c0634e N |
818 | |
819 | sra = sysfs_read(mdfd, 0, GET_VERSION); | |
820 | if (sra == NULL || strcmp(sra->text_version, ra->text_version) != 0) | |
821 | if (sysfs_set_array(ra, md_get_version(mdfd)) != 0) | |
822 | return 1; | |
823 | if (sra) | |
824 | sysfs_free(sra); | |
825 | ||
2318b9f0 | 826 | for (dev = ra->devs; dev; dev = dev->next) |
f35f2525 | 827 | if (sysfs_add_disk(ra, dev) == 0) |
598f0d58 | 828 | working++; |
c5afc314 N |
829 | else if (errno == EEXIST) |
830 | preexist++; | |
831 | if (working == 0) | |
832 | /* Nothing new, don't try to start */ ; | |
833 | else if (runstop > 0 || | |
834 | (working + preexist) >= ra->array.working_disks) { | |
598f0d58 NB |
835 | switch(ra->array.level) { |
836 | case LEVEL_LINEAR: | |
837 | case LEVEL_MULTIPATH: | |
838 | case 0: | |
f35f2525 | 839 | sysfs_set_str(ra, NULL, "array_state", |
598f0d58 NB |
840 | "active"); |
841 | break; | |
842 | default: | |
f35f2525 | 843 | sysfs_set_str(ra, NULL, "array_state", |
598f0d58 | 844 | "readonly"); |
75aa18b5 | 845 | /* start mdmon if needed. */ |
8850ee3e N |
846 | if (!mdmon_running(st->container_dev)) |
847 | start_mdmon(st->container_dev); | |
848 | ping_monitor(devnum2devname(st->container_dev)); | |
598f0d58 NB |
849 | break; |
850 | } | |
f35f2525 | 851 | sysfs_set_safemode(ra, ra->safe_mode_delay); |
c5afc314 N |
852 | if (verbose >= 0) { |
853 | fprintf(stderr, Name | |
d7288ddc | 854 | ": Started %s with %d devices", |
c5afc314 N |
855 | chosen_name, working + preexist); |
856 | if (preexist) | |
857 | fprintf(stderr, " (%d new)", working); | |
858 | fprintf(stderr, "\n"); | |
859 | } | |
75aa18b5 | 860 | /* FIXME should have an O_EXCL and wait for read-auto */ |
598f0d58 NB |
861 | } else |
862 | if (verbose >= 0) | |
c5afc314 | 863 | fprintf(stderr, Name |
d7288ddc | 864 | ": %s assembled with %d devices but " |
c5afc314 N |
865 | "not started\n", |
866 | chosen_name, working); | |
215bb3f7 | 867 | map_update(&map, fd2devnum(mdfd), |
c5afc314 N |
868 | ra->text_version, |
869 | ra->uuid, chosen_name); | |
215bb3f7 | 870 | close(mdfd); |
598f0d58 NB |
871 | } |
872 | return 0; | |
873 | } |