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