]>
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; | |
8382f19b NB |
84 | struct map_ent *mp, *map = NULL; |
85 | int dfd, mdfd; | |
86 | char *avail; | |
87 | int active_disks; | |
215bb3f7 | 88 | int trustworthy = FOREIGN; |
d7288ddc | 89 | char *name_to_use; |
ad5bc697 | 90 | mdu_array_info_t ainf; |
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 | } |
ad5bc697 | 246 | /* 4/ Check if array exists. |
215bb3f7 | 247 | */ |
ad5bc697 | 248 | map_lock(&map); |
215bb3f7 | 249 | mp = map_by_uuid(&map, info.uuid); |
ffb3e494 | 250 | if (mp) { |
215bb3f7 | 251 | mdfd = open_mddev(mp->path, 0); |
ffb3e494 N |
252 | if (mdfd < 0 && mddev_busy(mp->devnum)) { |
253 | /* maybe udev hasn't created it yet. */ | |
254 | char buf[50]; | |
255 | sprintf(buf, "%d:%d", dev2major(mp->devnum), | |
256 | dev2minor(mp->devnum)); | |
257 | mdfd = dev_open(buf, O_RDWR); | |
258 | } | |
259 | } else | |
215bb3f7 | 260 | mdfd = -1; |
d7288ddc | 261 | |
8382f19b | 262 | if (mdfd < 0) { |
7e0f6979 | 263 | struct mdinfo *sra; |
c5afc314 | 264 | struct mdinfo dinfo; |
8382f19b | 265 | |
215bb3f7 | 266 | /* Couldn't find an existing array, maybe make a new one */ |
ffb3e494 | 267 | mdfd = create_mddev(match ? match->devname : NULL, |
215bb3f7 N |
268 | info.name, autof, trustworthy, chosen_name); |
269 | ||
270 | ||
271 | if (mdfd < 0) | |
272 | return 1; | |
273 | ||
274 | sysfs_init(&info, mdfd, 0); | |
275 | ||
f35f2525 N |
276 | if (set_array_info(mdfd, st, &info) != 0) { |
277 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", | |
8382f19b NB |
278 | chosen_name, strerror(errno)); |
279 | close(mdfd); | |
280 | return 2; | |
281 | } | |
7801ac20 | 282 | |
c5afc314 N |
283 | dinfo = info; |
284 | dinfo.disk.major = major(stb.st_rdev); | |
285 | dinfo.disk.minor = minor(stb.st_rdev); | |
286 | if (add_disk(mdfd, st, &info, &dinfo) != 0) { | |
8382f19b NB |
287 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", |
288 | devname, chosen_name, strerror(errno)); | |
289 | ioctl(mdfd, STOP_ARRAY, 0); | |
290 | close(mdfd); | |
291 | return 2; | |
292 | } | |
197e3eb6 | 293 | sra = sysfs_read(mdfd, fd2devnum(mdfd), GET_DEVS); |
06c7f68e | 294 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { |
8382f19b NB |
295 | /* It really should be 'none' - must be old buggy |
296 | * kernel, and mdadm -I may not be able to complete. | |
297 | * So reject it. | |
298 | */ | |
299 | ioctl(mdfd, STOP_ARRAY, NULL); | |
300 | fprintf(stderr, Name | |
301 | ": You have an old buggy kernel which cannot support\n" | |
302 | " --incremental reliably. Aborting.\n"); | |
303 | close(mdfd); | |
304 | sysfs_free(sra); | |
305 | return 2; | |
306 | } | |
c5afc314 | 307 | info.array.working_disks = 1; |
f35f2525 | 308 | sysfs_free(sra); |
ad5bc697 N |
309 | /* 6/ Make sure /var/run/mdadm.map contains this array. */ |
310 | map_update(&map, fd2devnum(mdfd), | |
311 | info.text_version, | |
312 | info.uuid, chosen_name); | |
8382f19b NB |
313 | } else { |
314 | /* 5b/ if it does */ | |
315 | /* - check one drive in array to make sure metadata is a reasonably */ | |
316 | /* close match. Reject if not (e.g. different type) */ | |
317 | /* - add the device */ | |
318 | char dn[20]; | |
319 | int dfd2; | |
8382f19b | 320 | int err; |
7e0f6979 | 321 | struct mdinfo *sra; |
3da92f27 | 322 | struct supertype *st2; |
c5afc314 | 323 | struct mdinfo info2, *d; |
215bb3f7 N |
324 | |
325 | strcpy(chosen_name, mp->path); | |
326 | ||
197e3eb6 | 327 | sra = sysfs_read(mdfd, fd2devnum(mdfd), (GET_DEVS | GET_STATE)); |
2cdb6489 | 328 | |
06c7f68e NB |
329 | sprintf(dn, "%d:%d", sra->devs->disk.major, |
330 | sra->devs->disk.minor); | |
8382f19b | 331 | dfd2 = dev_open(dn, O_RDONLY); |
3da92f27 | 332 | st2 = dup_super(st); |
b8ac1967 NB |
333 | if (st2->ss->load_super(st2, dfd2, NULL) || |
334 | st->ss->compare_super(st, st2) != 0) { | |
8382f19b | 335 | fprintf(stderr, Name |
b8ac1967 NB |
336 | ": metadata mismatch between %s and " |
337 | "chosen array %s\n", | |
338 | devname, chosen_name); | |
8382f19b NB |
339 | close(mdfd); |
340 | close(dfd2); | |
341 | return 2; | |
342 | } | |
343 | close(dfd2); | |
f35f2525 | 344 | memset(&info2, 0, sizeof(info2)); |
3da92f27 NB |
345 | st2->ss->getinfo_super(st2, &info2); |
346 | st2->ss->free_super(st2); | |
8382f19b NB |
347 | if (info.array.level != info2.array.level || |
348 | memcmp(info.uuid, info2.uuid, 16) != 0 || | |
349 | info.array.raid_disks != info2.array.raid_disks) { | |
350 | fprintf(stderr, Name | |
351 | ": unexpected difference between %s and %s.\n", | |
352 | chosen_name, devname); | |
353 | close(mdfd); | |
354 | return 2; | |
355 | } | |
7801ac20 N |
356 | info2.disk.major = major(stb.st_rdev); |
357 | info2.disk.minor = minor(stb.st_rdev); | |
c5afc314 N |
358 | /* add disk needs to know about containers */ |
359 | if (st->ss->external) | |
360 | sra->array.level = LEVEL_CONTAINER; | |
7801ac20 | 361 | err = add_disk(mdfd, st2, sra, &info2); |
8382f19b NB |
362 | if (err < 0 && errno == EBUSY) { |
363 | /* could be another device present with the same | |
364 | * disk.number. Find and reject any such | |
365 | */ | |
366 | find_reject(mdfd, st, sra, info.disk.number, | |
367 | info.events, verbose, chosen_name); | |
7801ac20 | 368 | err = add_disk(mdfd, st2, sra, &info2); |
8382f19b NB |
369 | } |
370 | if (err < 0) { | |
371 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
372 | devname, chosen_name, strerror(errno)); | |
373 | close(mdfd); | |
374 | return 2; | |
375 | } | |
c5afc314 N |
376 | info.array.working_disks = 0; |
377 | for (d = sra->devs; d; d=d->next) | |
378 | info.array.working_disks ++; | |
379 | ||
8382f19b | 380 | } |
8382f19b NB |
381 | |
382 | /* 7/ Is there enough devices to possibly start the array? */ | |
383 | /* 7a/ if not, finish with success. */ | |
352452c3 N |
384 | if (info.array.level == LEVEL_CONTAINER) { |
385 | /* Try to assemble within the container */ | |
c5afc314 | 386 | close(mdfd); |
ad5bc697 | 387 | map_unlock(&map); |
97590376 | 388 | sysfs_uevent(&info, "change"); |
c5afc314 N |
389 | if (verbose >= 0) |
390 | fprintf(stderr, Name | |
391 | ": container %s now has %d devices\n", | |
392 | chosen_name, info.array.working_disks); | |
a714580e | 393 | wait_for(chosen_name); |
352452c3 N |
394 | return Incremental(chosen_name, verbose, runstop, |
395 | NULL, homehost, autof); | |
396 | } | |
f8409e54 | 397 | avail = NULL; |
8382f19b NB |
398 | active_disks = count_active(st, mdfd, &avail, &info); |
399 | if (enough(info.array.level, info.array.raid_disks, | |
400 | info.array.layout, info.array.state & 1, | |
401 | avail, active_disks) == 0) { | |
402 | free(avail); | |
403 | if (verbose >= 0) | |
404 | fprintf(stderr, Name | |
405 | ": %s attached to %s, not enough to start (%d).\n", | |
406 | devname, chosen_name, active_disks); | |
ad5bc697 | 407 | map_unlock(&map); |
8382f19b NB |
408 | close(mdfd); |
409 | return 0; | |
410 | } | |
411 | free(avail); | |
412 | ||
413 | /* 7b/ if yes, */ | |
414 | /* - if number of OK devices match expected, or -R and there */ | |
415 | /* are enough, */ | |
416 | /* + add any bitmap file */ | |
417 | /* + start the array (auto-readonly). */ | |
8382f19b NB |
418 | |
419 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
420 | if (verbose >= 0) | |
421 | fprintf(stderr, Name | |
422 | ": %s attached to %s which is already active.\n", | |
423 | devname, chosen_name); | |
ad5bc697 N |
424 | close(mdfd); |
425 | map_unlock(&map); | |
8382f19b NB |
426 | return 0; |
427 | } | |
ad5bc697 N |
428 | |
429 | map_unlock(&map); | |
8382f19b | 430 | if (runstop > 0 || active_disks >= info.array.working_disks) { |
7e0f6979 | 431 | struct mdinfo *sra; |
8382f19b NB |
432 | /* Let's try to start it */ |
433 | if (match && match->bitmap_file) { | |
434 | int bmfd = open(match->bitmap_file, O_RDWR); | |
435 | if (bmfd < 0) { | |
436 | fprintf(stderr, Name | |
437 | ": Could not open bitmap file %s.\n", | |
438 | match->bitmap_file); | |
439 | close(mdfd); | |
440 | return 1; | |
441 | } | |
442 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
443 | close(bmfd); | |
444 | fprintf(stderr, Name | |
445 | ": Failed to set bitmapfile for %s.\n", | |
446 | chosen_name); | |
447 | close(mdfd); | |
448 | return 1; | |
449 | } | |
450 | close(bmfd); | |
451 | } | |
197e3eb6 | 452 | sra = sysfs_read(mdfd, fd2devnum(mdfd), 0); |
7b403fef | 453 | if ((sra == NULL || active_disks >= info.array.working_disks) |
215bb3f7 | 454 | && trustworthy != FOREIGN) |
8382f19b NB |
455 | rv = ioctl(mdfd, RUN_ARRAY, NULL); |
456 | else | |
457 | rv = sysfs_set_str(sra, NULL, | |
458 | "array_state", "read-auto"); | |
459 | if (rv == 0) { | |
460 | if (verbose >= 0) | |
461 | fprintf(stderr, Name | |
462 | ": %s attached to %s, which has been started.\n", | |
463 | devname, chosen_name); | |
464 | rv = 0; | |
a714580e | 465 | wait_for(chosen_name); |
8382f19b NB |
466 | } else { |
467 | fprintf(stderr, Name | |
468 | ": %s attached to %s, but failed to start: %s.\n", | |
469 | devname, chosen_name, strerror(errno)); | |
470 | rv = 1; | |
471 | } | |
472 | } else { | |
473 | if (verbose >= 0) | |
474 | fprintf(stderr, Name | |
475 | ": %s attached to %s, not enough to start safely.\n", | |
476 | devname, chosen_name); | |
477 | rv = 0; | |
478 | } | |
479 | close(mdfd); | |
480 | return rv; | |
481 | } | |
482 | ||
7e0f6979 | 483 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
484 | int number, __u64 events, int verbose, |
485 | char *array_name) | |
486 | { | |
3da92f27 | 487 | /* Find a device attached to this array with a disk.number of number |
8382f19b NB |
488 | * and events less than the passed events, and remove the device. |
489 | */ | |
06c7f68e | 490 | struct mdinfo *d; |
8382f19b NB |
491 | mdu_array_info_t ra; |
492 | ||
493 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
494 | return; /* not safe to remove from active arrays | |
495 | * without thinking more */ | |
496 | ||
497 | for (d = sra->devs; d ; d = d->next) { | |
498 | char dn[10]; | |
499 | int dfd; | |
8382f19b | 500 | struct mdinfo info; |
06c7f68e | 501 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
502 | dfd = dev_open(dn, O_RDONLY); |
503 | if (dfd < 0) | |
504 | continue; | |
3da92f27 | 505 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
506 | close(dfd); |
507 | continue; | |
508 | } | |
3da92f27 NB |
509 | st->ss->getinfo_super(st, &info); |
510 | st->ss->free_super(st); | |
8382f19b NB |
511 | close(dfd); |
512 | ||
513 | if (info.disk.number != number || | |
514 | info.events >= events) | |
515 | continue; | |
516 | ||
06c7f68e | 517 | if (d->disk.raid_disk > -1) |
8382f19b NB |
518 | sysfs_set_str(sra, d, "slot", "none"); |
519 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
520 | if (verbose >= 0) | |
521 | fprintf(stderr, Name | |
522 | ": removing old device %s from %s\n", | |
06c7f68e | 523 | d->sys_name+4, array_name); |
8382f19b NB |
524 | } |
525 | } | |
526 | ||
527 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
528 | struct mdinfo *bestinfo) | |
529 | { | |
530 | /* count how many devices in sra think they are active */ | |
06c7f68e | 531 | struct mdinfo *d; |
8382f19b NB |
532 | int cnt = 0, cnt1 = 0; |
533 | __u64 max_events = 0; | |
7e0f6979 | 534 | struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); |
8382f19b NB |
535 | char *avail = NULL; |
536 | ||
537 | for (d = sra->devs ; d ; d = d->next) { | |
538 | char dn[30]; | |
539 | int dfd; | |
8382f19b NB |
540 | int ok; |
541 | struct mdinfo info; | |
542 | ||
06c7f68e | 543 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
544 | dfd = dev_open(dn, O_RDONLY); |
545 | if (dfd < 0) | |
546 | continue; | |
3da92f27 | 547 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
548 | close(dfd); |
549 | if (ok != 0) | |
550 | continue; | |
3da92f27 | 551 | st->ss->getinfo_super(st, &info); |
43aaf431 DL |
552 | if (!avail) { |
553 | avail = malloc(info.array.raid_disks); | |
554 | if (!avail) { | |
555 | fprintf(stderr, Name ": out of memory.\n"); | |
556 | exit(1); | |
557 | } | |
558 | memset(avail, 0, info.array.raid_disks); | |
559 | *availp = avail; | |
560 | } | |
561 | ||
8382f19b NB |
562 | if (info.disk.state & (1<<MD_DISK_SYNC)) |
563 | { | |
8382f19b NB |
564 | if (cnt == 0) { |
565 | cnt++; | |
566 | max_events = info.events; | |
567 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 568 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
569 | } else if (info.events == max_events) { |
570 | cnt++; | |
571 | avail[info.disk.raid_disk] = 2; | |
572 | } else if (info.events == max_events-1) { | |
573 | cnt1++; | |
574 | avail[info.disk.raid_disk] = 1; | |
575 | } else if (info.events < max_events - 1) | |
576 | ; | |
577 | else if (info.events == max_events+1) { | |
578 | int i; | |
579 | cnt1 = cnt; | |
580 | cnt = 1; | |
581 | max_events = info.events; | |
582 | for (i=0; i<info.array.raid_disks; i++) | |
583 | if (avail[i]) | |
584 | avail[i]--; | |
585 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 586 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
587 | } else { /* info.events much bigger */ |
588 | cnt = 1; cnt1 = 0; | |
589 | memset(avail, 0, info.disk.raid_disk); | |
590 | max_events = info.events; | |
3da92f27 | 591 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
592 | } |
593 | } | |
3da92f27 | 594 | st->ss->free_super(st); |
8382f19b NB |
595 | } |
596 | return cnt + cnt1; | |
597 | } | |
598 | ||
599 | void RebuildMap(void) | |
600 | { | |
601 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
602 | struct mdstat_ent *md; | |
603 | struct map_ent *map = NULL; | |
604 | int mdp = get_mdp_major(); | |
605 | ||
606 | for (md = mdstat ; md ; md = md->next) { | |
7e0f6979 | 607 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS); |
06c7f68e | 608 | struct mdinfo *sd; |
8382f19b NB |
609 | |
610 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
611 | char dn[30]; | |
612 | int dfd; | |
613 | int ok; | |
614 | struct supertype *st; | |
615 | char *path; | |
8382f19b NB |
616 | struct mdinfo info; |
617 | ||
06c7f68e | 618 | sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor); |
8382f19b NB |
619 | dfd = dev_open(dn, O_RDONLY); |
620 | if (dfd < 0) | |
621 | continue; | |
622 | st = guess_super(dfd); | |
623 | if ( st == NULL) | |
624 | ok = -1; | |
625 | else | |
3da92f27 | 626 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
627 | close(dfd); |
628 | if (ok != 0) | |
629 | continue; | |
3da92f27 | 630 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
631 | if (md->devnum > 0) |
632 | path = map_dev(MD_MAJOR, md->devnum, 0); | |
633 | else | |
634 | path = map_dev(mdp, (-1-md->devnum)<< 6, 0); | |
1522c538 NB |
635 | map_add(&map, md->devnum, |
636 | info.text_version, | |
8382f19b | 637 | info.uuid, path ? : "/unknown"); |
3da92f27 | 638 | st->ss->free_super(st); |
8382f19b NB |
639 | break; |
640 | } | |
97590376 | 641 | sysfs_free(sra); |
8382f19b NB |
642 | } |
643 | map_write(map); | |
644 | map_free(map); | |
97590376 N |
645 | for (md = mdstat ; md ; md = md->next) { |
646 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_VERSION); | |
647 | sysfs_uevent(sra, "change"); | |
648 | sysfs_free(sra); | |
649 | } | |
8382f19b NB |
650 | } |
651 | ||
652 | int IncrementalScan(int verbose) | |
653 | { | |
654 | /* look at every device listed in the 'map' file. | |
655 | * If one is found that is not running then: | |
656 | * look in mdadm.conf for bitmap file. | |
657 | * if one exists, but array has none, add it. | |
658 | * try to start array in auto-readonly mode | |
659 | */ | |
660 | struct map_ent *mapl = NULL; | |
661 | struct map_ent *me; | |
662 | mddev_ident_t devs, mddev; | |
663 | int rv = 0; | |
664 | ||
665 | map_read(&mapl); | |
666 | devs = conf_get_ident(NULL); | |
667 | ||
668 | for (me = mapl ; me ; me = me->next) { | |
8382f19b NB |
669 | mdu_array_info_t array; |
670 | mdu_bitmap_file_t bmf; | |
7e0f6979 | 671 | struct mdinfo *sra; |
215bb3f7 N |
672 | int mdfd = open_mddev(me->path, 0); |
673 | ||
8382f19b NB |
674 | if (mdfd < 0) |
675 | continue; | |
676 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
677 | errno != ENODEV) { | |
678 | close(mdfd); | |
679 | continue; | |
680 | } | |
681 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
682 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
fe056d1f N |
683 | if (mddev->devname |
684 | && strcmp(mddev->devname, me->path) == 0) | |
8382f19b NB |
685 | break; |
686 | if (mddev && mddev->bitmap_file) { | |
687 | /* | |
688 | * Note: early kernels will wrongly fail this, so it | |
689 | * is a hint only | |
690 | */ | |
691 | int added = -1; | |
692 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
693 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
694 | if (bmfd >= 0) { | |
695 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
696 | bmfd); | |
697 | close(bmfd); | |
698 | } | |
699 | } | |
700 | if (verbose >= 0) { | |
701 | if (added == 0) | |
702 | fprintf(stderr, Name | |
703 | ": Added bitmap %s to %s\n", | |
704 | mddev->bitmap_file, me->path); | |
705 | else if (errno != EEXIST) | |
706 | fprintf(stderr, Name | |
707 | ": Failed to add bitmap to %s: %s\n", | |
708 | me->path, strerror(errno)); | |
709 | } | |
710 | } | |
711 | sra = sysfs_read(mdfd, 0, 0); | |
712 | if (sra) { | |
713 | if (sysfs_set_str(sra, NULL, | |
714 | "array_state", "read-auto") == 0) { | |
715 | if (verbose >= 0) | |
716 | fprintf(stderr, Name | |
717 | ": started array %s\n", | |
718 | me->path); | |
719 | } else { | |
720 | fprintf(stderr, Name | |
721 | ": failed to start array %s: %s\n", | |
722 | me->path, strerror(errno)); | |
723 | rv = 1; | |
724 | } | |
725 | } | |
726 | } | |
727 | return rv; | |
728 | } | |
598f0d58 | 729 | |
1771a6e2 N |
730 | static char *container2devname(char *devname) |
731 | { | |
732 | char *mdname = NULL; | |
733 | ||
734 | if (devname[0] == '/') { | |
735 | int fd = open(devname, O_RDONLY); | |
736 | if (fd >= 0) { | |
737 | mdname = devnum2devname(fd2devnum(fd)); | |
738 | close(fd); | |
739 | } | |
740 | } else { | |
741 | int uuid[4]; | |
742 | struct map_ent *mp, *map = NULL; | |
743 | ||
744 | if (!parse_uuid(devname, uuid)) | |
745 | return mdname; | |
746 | mp = map_by_uuid(&map, uuid); | |
747 | if (mp) | |
748 | mdname = devnum2devname(mp->devnum); | |
749 | map_free(map); | |
750 | } | |
751 | ||
752 | return mdname; | |
753 | } | |
754 | ||
598f0d58 | 755 | int Incremental_container(struct supertype *st, char *devname, int verbose, |
215bb3f7 | 756 | int runstop, int autof, int trustworthy) |
598f0d58 NB |
757 | { |
758 | /* Collect the contents of this container and for each | |
759 | * array, choose a device name and assemble the array. | |
760 | */ | |
761 | ||
762 | struct mdinfo *list = st->ss->container_content(st); | |
763 | struct mdinfo *ra; | |
ad5bc697 N |
764 | struct map_ent *map = NULL; |
765 | ||
766 | map_lock(&map); | |
598f0d58 NB |
767 | |
768 | for (ra = list ; ra ; ra = ra->next) { | |
598f0d58 NB |
769 | int mdfd; |
770 | char chosen_name[1024]; | |
ad5bc697 | 771 | struct map_ent *mp; |
dbb44303 | 772 | struct mddev_ident_s *match = NULL; |
6234c63c | 773 | int err; |
598f0d58 | 774 | |
c5afc314 | 775 | mp = map_by_uuid(&map, ra->uuid); |
d7288ddc | 776 | |
dbb44303 N |
777 | if (!mp) { |
778 | ||
779 | /* Check in mdadm.conf for devices == devname and | |
780 | * member == ra->text_version after second slash. | |
781 | */ | |
782 | char *sub = strchr(ra->text_version+1, '/'); | |
783 | struct mddev_ident_s *array_list; | |
784 | if (sub) { | |
785 | sub++; | |
786 | array_list = conf_get_ident(NULL); | |
787 | } else | |
788 | array_list = NULL; | |
789 | for(; array_list ; array_list = array_list->next) { | |
dbb44303 N |
790 | char *dn; |
791 | if (array_list->member == NULL || | |
792 | array_list->container == NULL) | |
793 | continue; | |
794 | if (strcmp(array_list->member, sub) != 0) | |
795 | continue; | |
71d60c48 DW |
796 | if (array_list->uuid_set && |
797 | !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid)) | |
798 | continue; | |
1771a6e2 N |
799 | dn = container2devname(array_list->container); |
800 | if (dn == NULL) | |
dbb44303 | 801 | continue; |
dbb44303 N |
802 | if (strncmp(dn, ra->text_version+1, |
803 | strlen(dn)) != 0 || | |
804 | ra->text_version[strlen(dn)+1] != '/') { | |
805 | free(dn); | |
806 | continue; | |
807 | } | |
808 | free(dn); | |
809 | /* we have a match */ | |
810 | match = array_list; | |
71d60c48 DW |
811 | if (verbose>0) |
812 | fprintf(stderr, Name ": match found for member %s\n", | |
813 | array_list->member); | |
dbb44303 N |
814 | break; |
815 | } | |
816 | } | |
817 | ||
215bb3f7 N |
818 | mdfd = create_mddev(match ? match->devname : NULL, |
819 | ra->name, | |
820 | autof, | |
821 | trustworthy, | |
822 | chosen_name); | |
598f0d58 NB |
823 | |
824 | if (mdfd < 0) { | |
825 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
826 | chosen_name, strerror(errno)); | |
827 | return 2; | |
828 | } | |
829 | ||
6234c63c DW |
830 | err = assemble_container_content(st, mdfd, ra, runstop, |
831 | chosen_name, verbose); | |
6234c63c DW |
832 | if (err) |
833 | return err; | |
598f0d58 | 834 | } |
ad5bc697 | 835 | map_unlock(&map); |
598f0d58 NB |
836 | return 0; |
837 | } |