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