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