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