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