]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Incremental.c - support --incremental. Part of: | |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
4 | * | |
5 | * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de> | |
6 | * | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | * Author: Neil Brown | |
23 | * Email: <neilb@suse.de> | |
24 | * Paper: Neil Brown | |
25 | * Novell Inc | |
26 | * GPO Box Q1283 | |
27 | * QVB Post Office, NSW 1230 | |
28 | * Australia | |
29 | */ | |
30 | ||
31 | #include "mdadm.h" | |
32 | #include <dirent.h> | |
33 | #include <ctype.h> | |
34 | ||
35 | static int count_active(struct supertype *st, struct mdinfo *sra, | |
36 | int mdfd, char **availp, | |
37 | struct mdinfo *info); | |
38 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, | |
39 | int number, __u64 events, int verbose, | |
40 | char *array_name); | |
41 | static int try_spare(char *devname, int *dfdp, struct dev_policy *pol, | |
42 | struct map_ent *target, | |
43 | struct supertype *st, int verbose); | |
44 | ||
45 | static int Incremental_container(struct supertype *st, char *devname, | |
46 | char *homehost, | |
47 | int verbose, int runstop, int autof); | |
48 | ||
49 | static struct mddev_ident *search_mdstat(struct supertype *st, | |
50 | struct mdinfo *info, | |
51 | char *devname, | |
52 | int verbose, int *rvp); | |
53 | ||
54 | int Incremental(char *devname, int verbose, int runstop, | |
55 | struct supertype *st, char *homehost, int require_homehost, | |
56 | int autof) | |
57 | { | |
58 | /* Add this device to an array, creating the array if necessary | |
59 | * and starting the array if sensible or - if runstop>0 - if possible. | |
60 | * | |
61 | * This has several steps: | |
62 | * | |
63 | * 1/ Check if device is permitted by mdadm.conf, reject if not. | |
64 | * 2/ Find metadata, reject if none appropriate (check | |
65 | * version/name from args) | |
66 | * 3/ Check if there is a match in mdadm.conf | |
67 | * 3a/ if not, check for homehost match. If no match, assemble as | |
68 | * a 'foreign' array. | |
69 | * 4/ Determine device number. | |
70 | * - If in mdadm.conf with std name, use that | |
71 | * - UUID in /var/run/mdadm.map use that | |
72 | * - If name is suggestive, use that. unless in use with different uuid. | |
73 | * - Choose a free, high number. | |
74 | * - Use a partitioned device unless strong suggestion not to. | |
75 | * e.g. auto=md | |
76 | * Don't choose partitioned for containers. | |
77 | * 5/ Find out if array already exists | |
78 | * 5a/ if it does not | |
79 | * - choose a name, from mdadm.conf or 'name' field in array. | |
80 | * - create the array | |
81 | * - add the device | |
82 | * 5b/ if it does | |
83 | * - check one drive in array to make sure metadata is a reasonably | |
84 | * close match. Reject if not (e.g. different type) | |
85 | * - add the device | |
86 | * 6/ Make sure /var/run/mdadm.map contains this array. | |
87 | * 7/ Is there enough devices to possibly start the array? | |
88 | * For a container, this means running Incremental_container. | |
89 | * 7a/ if not, finish with success. | |
90 | * 7b/ if yes, | |
91 | * - read all metadata and arrange devices like -A does | |
92 | * - if number of OK devices match expected, or -R and there are enough, | |
93 | * start the array (auto-readonly). | |
94 | */ | |
95 | struct stat stb; | |
96 | struct mdinfo info, dinfo; | |
97 | struct mdinfo *sra = NULL, *d; | |
98 | struct mddev_ident *match; | |
99 | char chosen_name[1024]; | |
100 | int rv = 1; | |
101 | struct map_ent *mp, *map = NULL; | |
102 | int dfd = -1, mdfd = -1; | |
103 | char *avail = NULL; | |
104 | int active_disks; | |
105 | int trustworthy; | |
106 | char *name_to_use; | |
107 | mdu_array_info_t ainf; | |
108 | struct dev_policy *policy = NULL; | |
109 | struct map_ent target_array; | |
110 | int have_target; | |
111 | ||
112 | struct createinfo *ci = conf_get_create_info(); | |
113 | ||
114 | if (stat(devname, &stb) < 0) { | |
115 | if (verbose >= 0) | |
116 | fprintf(stderr, Name ": stat failed for %s: %s.\n", | |
117 | devname, strerror(errno)); | |
118 | return rv; | |
119 | } | |
120 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
121 | if (verbose >= 0) | |
122 | fprintf(stderr, Name ": %s is not a block device.\n", | |
123 | devname); | |
124 | return rv; | |
125 | } | |
126 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
127 | if (dfd < 0) { | |
128 | if (verbose >= 0) | |
129 | fprintf(stderr, Name ": cannot open %s: %s.\n", | |
130 | devname, strerror(errno)); | |
131 | return rv; | |
132 | } | |
133 | /* If the device is a container, we do something very different */ | |
134 | if (must_be_container(dfd)) { | |
135 | if (!st) | |
136 | st = super_by_fd(dfd, NULL); | |
137 | if (st && st->ss->load_container) | |
138 | rv = st->ss->load_container(st, dfd, NULL); | |
139 | ||
140 | close(dfd); | |
141 | if (!rv && st->ss->container_content) | |
142 | return Incremental_container(st, devname, homehost, | |
143 | verbose, runstop, autof); | |
144 | ||
145 | fprintf(stderr, Name ": %s is not part of an md array.\n", | |
146 | devname); | |
147 | return rv; | |
148 | } | |
149 | ||
150 | /* 1/ Check if device is permitted by mdadm.conf */ | |
151 | ||
152 | if (!conf_test_dev(devname)) { | |
153 | if (verbose >= 0) | |
154 | fprintf(stderr, Name | |
155 | ": %s not permitted by mdadm.conf.\n", | |
156 | devname); | |
157 | goto out; | |
158 | } | |
159 | ||
160 | /* 2/ Find metadata, reject if none appropriate (check | |
161 | * version/name from args) */ | |
162 | ||
163 | if (fstat(dfd, &stb) < 0) { | |
164 | if (verbose >= 0) | |
165 | fprintf(stderr, Name ": fstat failed for %s: %s.\n", | |
166 | devname, strerror(errno)); | |
167 | goto out; | |
168 | } | |
169 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
170 | if (verbose >= 0) | |
171 | fprintf(stderr, Name ": %s is not a block device.\n", | |
172 | devname); | |
173 | goto out; | |
174 | } | |
175 | ||
176 | dinfo.disk.major = major(stb.st_rdev); | |
177 | dinfo.disk.minor = minor(stb.st_rdev); | |
178 | ||
179 | policy = disk_policy(&dinfo); | |
180 | have_target = policy_check_path(&dinfo, &target_array); | |
181 | ||
182 | if (st == NULL && (st = guess_super(dfd)) == NULL) { | |
183 | if (verbose >= 0) | |
184 | fprintf(stderr, Name | |
185 | ": no recognisable superblock on %s.\n", | |
186 | devname); | |
187 | rv = try_spare(devname, &dfd, policy, | |
188 | have_target ? &target_array : NULL, | |
189 | st, verbose); | |
190 | goto out; | |
191 | } | |
192 | if (st->ss->compare_super == NULL || | |
193 | st->ss->load_super(st, dfd, NULL)) { | |
194 | if (verbose >= 0) | |
195 | fprintf(stderr, Name ": no RAID superblock on %s.\n", | |
196 | devname); | |
197 | rv = try_spare(devname, &dfd, policy, | |
198 | have_target ? &target_array : NULL, | |
199 | st, verbose); | |
200 | free(st); | |
201 | goto out; | |
202 | } | |
203 | close (dfd); dfd = -1; | |
204 | ||
205 | memset(&info, 0, sizeof(info)); | |
206 | st->ss->getinfo_super(st, &info, NULL); | |
207 | ||
208 | /* 3/ Check if there is a match in mdadm.conf */ | |
209 | match = search_mdstat(st, &info, devname, verbose, &rv); | |
210 | if (!match && rv == 2) | |
211 | goto out; | |
212 | ||
213 | if (match && match->devname | |
214 | && strcasecmp(match->devname, "<ignore>") == 0) { | |
215 | if (verbose >= 0) | |
216 | fprintf(stderr, Name ": array containing %s is explicitly" | |
217 | " ignored by mdadm.conf\n", | |
218 | devname); | |
219 | goto out; | |
220 | } | |
221 | ||
222 | /* 3a/ if not, check for homehost match. If no match, continue | |
223 | * but don't trust the 'name' in the array. Thus a 'random' minor | |
224 | * number will be assigned, and the device name will be based | |
225 | * on that. */ | |
226 | if (match) | |
227 | trustworthy = LOCAL; | |
228 | else if (st->ss->match_home(st, homehost) == 1) | |
229 | trustworthy = LOCAL; | |
230 | else if (st->ss->match_home(st, "any") == 1) | |
231 | trustworthy = LOCAL_ANY; | |
232 | else | |
233 | trustworthy = FOREIGN; | |
234 | ||
235 | ||
236 | if (!match && !conf_test_metadata(st->ss->name, policy, | |
237 | (trustworthy == LOCAL))) { | |
238 | if (verbose >= 1) | |
239 | fprintf(stderr, Name | |
240 | ": %s has metadata type %s for which " | |
241 | "auto-assembly is disabled\n", | |
242 | devname, st->ss->name); | |
243 | goto out; | |
244 | } | |
245 | if (trustworthy == LOCAL_ANY) | |
246 | trustworthy = LOCAL; | |
247 | ||
248 | /* There are three possible sources for 'autof': command line, | |
249 | * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf. | |
250 | * ARRAY takes precedence, then command line, then | |
251 | * CREATE. | |
252 | */ | |
253 | if (match && match->autof) | |
254 | autof = match->autof; | |
255 | if (autof == 0) | |
256 | autof = ci->autof; | |
257 | ||
258 | name_to_use = info.name; | |
259 | if (name_to_use[0] == 0 && | |
260 | info.array.level == LEVEL_CONTAINER && | |
261 | trustworthy == LOCAL) { | |
262 | name_to_use = info.text_version; | |
263 | trustworthy = METADATA; | |
264 | } | |
265 | if (name_to_use[0] && trustworthy != LOCAL && | |
266 | ! require_homehost && | |
267 | conf_name_is_free(name_to_use)) | |
268 | trustworthy = LOCAL; | |
269 | ||
270 | /* strip "hostname:" prefix from name if we have decided | |
271 | * to treat it as LOCAL | |
272 | */ | |
273 | if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL) | |
274 | name_to_use = strchr(name_to_use, ':')+1; | |
275 | ||
276 | /* 4/ Check if array exists. | |
277 | */ | |
278 | if (map_lock(&map)) | |
279 | fprintf(stderr, Name ": failed to get exclusive lock on " | |
280 | "mapfile\n"); | |
281 | mp = map_by_uuid(&map, info.uuid); | |
282 | if (mp) | |
283 | mdfd = open_dev(mp->devnum); | |
284 | else | |
285 | mdfd = -1; | |
286 | ||
287 | if (mdfd < 0) { | |
288 | ||
289 | /* Couldn't find an existing array, maybe make a new one */ | |
290 | mdfd = create_mddev(match ? match->devname : NULL, | |
291 | name_to_use, autof, trustworthy, chosen_name); | |
292 | ||
293 | if (mdfd < 0) | |
294 | goto out; | |
295 | ||
296 | sysfs_init(&info, mdfd, 0); | |
297 | ||
298 | if (set_array_info(mdfd, st, &info) != 0) { | |
299 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", | |
300 | chosen_name, strerror(errno)); | |
301 | rv = 2; | |
302 | goto out; | |
303 | } | |
304 | ||
305 | dinfo = info; | |
306 | dinfo.disk.major = major(stb.st_rdev); | |
307 | dinfo.disk.minor = minor(stb.st_rdev); | |
308 | if (add_disk(mdfd, st, &info, &dinfo) != 0) { | |
309 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
310 | devname, chosen_name, strerror(errno)); | |
311 | ioctl(mdfd, STOP_ARRAY, 0); | |
312 | rv = 2; | |
313 | goto out; | |
314 | } | |
315 | sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE | | |
316 | GET_OFFSET | GET_SIZE)); | |
317 | ||
318 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { | |
319 | /* It really should be 'none' - must be old buggy | |
320 | * kernel, and mdadm -I may not be able to complete. | |
321 | * So reject it. | |
322 | */ | |
323 | ioctl(mdfd, STOP_ARRAY, NULL); | |
324 | fprintf(stderr, Name | |
325 | ": You have an old buggy kernel which cannot support\n" | |
326 | " --incremental reliably. Aborting.\n"); | |
327 | sysfs_free(sra); | |
328 | rv = 2; | |
329 | goto out; | |
330 | } | |
331 | info.array.working_disks = 1; | |
332 | /* 6/ Make sure /var/run/mdadm.map contains this array. */ | |
333 | map_update(&map, fd2devnum(mdfd), | |
334 | info.text_version, | |
335 | info.uuid, chosen_name); | |
336 | } else { | |
337 | /* 5b/ if it does */ | |
338 | /* - check one drive in array to make sure metadata is a reasonably */ | |
339 | /* close match. Reject if not (e.g. different type) */ | |
340 | /* - add the device */ | |
341 | char dn[20]; | |
342 | int dfd2; | |
343 | int err; | |
344 | struct supertype *st2; | |
345 | struct mdinfo info2, *d; | |
346 | ||
347 | sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE | | |
348 | GET_OFFSET | GET_SIZE)); | |
349 | ||
350 | if (mp->path) | |
351 | strcpy(chosen_name, mp->path); | |
352 | else | |
353 | strcpy(chosen_name, devnum2devname(mp->devnum)); | |
354 | ||
355 | /* It is generally not OK to add non-spare drives to a | |
356 | * running array as they are probably missing because | |
357 | * they failed. However if runstop is 1, then the | |
358 | * array was possibly started early and our best bet is | |
359 | * to add this anyway. | |
360 | * Also if action policy is re-add or better we allow | |
361 | * re-add | |
362 | */ | |
363 | if ((info.disk.state & (1<<MD_DISK_SYNC)) != 0 | |
364 | && ! policy_action_allows(policy, st->ss->name, | |
365 | act_re_add) | |
366 | && runstop < 1) { | |
367 | int active = 0; | |
368 | ||
369 | if (st->ss->external) { | |
370 | char *devname = devnum2devname(fd2devnum(mdfd)); | |
371 | ||
372 | active = devname && is_container_active(devname); | |
373 | free(devname); | |
374 | } else if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) | |
375 | active = 1; | |
376 | if (active) { | |
377 | fprintf(stderr, Name | |
378 | ": not adding %s to active array (without --run) %s\n", | |
379 | devname, chosen_name); | |
380 | rv = 2; | |
381 | goto out; | |
382 | } | |
383 | } | |
384 | if (!sra) { | |
385 | rv = 2; | |
386 | goto out; | |
387 | } | |
388 | if (sra->devs) { | |
389 | sprintf(dn, "%d:%d", sra->devs->disk.major, | |
390 | sra->devs->disk.minor); | |
391 | dfd2 = dev_open(dn, O_RDONLY); | |
392 | st2 = dup_super(st); | |
393 | if (st2->ss->load_super(st2, dfd2, NULL) || | |
394 | st->ss->compare_super(st, st2) != 0) { | |
395 | fprintf(stderr, Name | |
396 | ": metadata mismatch between %s and " | |
397 | "chosen array %s\n", | |
398 | devname, chosen_name); | |
399 | close(dfd2); | |
400 | rv = 2; | |
401 | goto out; | |
402 | } | |
403 | close(dfd2); | |
404 | memset(&info2, 0, sizeof(info2)); | |
405 | st2->ss->getinfo_super(st2, &info2, NULL); | |
406 | st2->ss->free_super(st2); | |
407 | if (info.array.level != info2.array.level || | |
408 | memcmp(info.uuid, info2.uuid, 16) != 0 || | |
409 | info.array.raid_disks != info2.array.raid_disks) { | |
410 | fprintf(stderr, Name | |
411 | ": unexpected difference between %s and %s.\n", | |
412 | chosen_name, devname); | |
413 | rv = 2; | |
414 | goto out; | |
415 | } | |
416 | } | |
417 | info2.disk.major = major(stb.st_rdev); | |
418 | info2.disk.minor = minor(stb.st_rdev); | |
419 | /* add disk needs to know about containers */ | |
420 | if (st->ss->external) | |
421 | sra->array.level = LEVEL_CONTAINER; | |
422 | err = add_disk(mdfd, st, sra, &info2); | |
423 | if (err < 0 && errno == EBUSY) { | |
424 | /* could be another device present with the same | |
425 | * disk.number. Find and reject any such | |
426 | */ | |
427 | find_reject(mdfd, st, sra, info.disk.number, | |
428 | info.events, verbose, chosen_name); | |
429 | err = add_disk(mdfd, st, sra, &info2); | |
430 | } | |
431 | if (err < 0) { | |
432 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
433 | devname, chosen_name, strerror(errno)); | |
434 | rv = 2; | |
435 | goto out; | |
436 | } | |
437 | info.array.working_disks = 0; | |
438 | for (d = sra->devs; d; d=d->next) | |
439 | info.array.working_disks ++; | |
440 | ||
441 | } | |
442 | ||
443 | /* 7/ Is there enough devices to possibly start the array? */ | |
444 | /* 7a/ if not, finish with success. */ | |
445 | if (info.array.level == LEVEL_CONTAINER) { | |
446 | char *devname = NULL; | |
447 | /* Try to assemble within the container */ | |
448 | map_unlock(&map); | |
449 | sysfs_uevent(&info, "change"); | |
450 | if (verbose >= 0) | |
451 | fprintf(stderr, Name | |
452 | ": container %s now has %d devices\n", | |
453 | chosen_name, info.array.working_disks); | |
454 | wait_for(chosen_name, mdfd); | |
455 | if (st->ss->external) | |
456 | devname = devnum2devname(fd2devnum(mdfd)); | |
457 | close(mdfd); | |
458 | sysfs_free(sra); | |
459 | rv = Incremental(chosen_name, verbose, runstop, | |
460 | NULL, homehost, require_homehost, autof); | |
461 | if (rv == 1) | |
462 | /* Don't fail the whole -I if a subarray didn't | |
463 | * have enough devices to start yet | |
464 | */ | |
465 | rv = 0; | |
466 | /* after spare is added, ping monitor for external metadata | |
467 | * so that it can eg. try to rebuild degraded array */ | |
468 | if (st->ss->external) { | |
469 | ping_monitor(devname); | |
470 | free(devname); | |
471 | } | |
472 | return rv; | |
473 | } | |
474 | ||
475 | /* We have added something to the array, so need to re-read the | |
476 | * state. Eventually this state should be kept up-to-date as | |
477 | * things change. | |
478 | */ | |
479 | sysfs_free(sra); | |
480 | sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE | | |
481 | GET_OFFSET | GET_SIZE)); | |
482 | active_disks = count_active(st, sra, mdfd, &avail, &info); | |
483 | if (enough(info.array.level, info.array.raid_disks, | |
484 | info.array.layout, info.array.state & 1, | |
485 | avail, active_disks) == 0) { | |
486 | if (verbose >= 0) | |
487 | fprintf(stderr, Name | |
488 | ": %s attached to %s, not enough to start (%d).\n", | |
489 | devname, chosen_name, active_disks); | |
490 | map_unlock(&map); | |
491 | rv = 0; | |
492 | goto out; | |
493 | } | |
494 | ||
495 | /* 7b/ if yes, */ | |
496 | /* - if number of OK devices match expected, or -R and there */ | |
497 | /* are enough, */ | |
498 | /* + add any bitmap file */ | |
499 | /* + start the array (auto-readonly). */ | |
500 | ||
501 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
502 | if (verbose >= 0) | |
503 | fprintf(stderr, Name | |
504 | ": %s attached to %s which is already active.\n", | |
505 | devname, chosen_name); | |
506 | map_unlock(&map); | |
507 | rv = 0; | |
508 | goto out; | |
509 | } | |
510 | ||
511 | map_unlock(&map); | |
512 | if (runstop > 0 || active_disks >= info.array.working_disks) { | |
513 | struct mdinfo *dsk; | |
514 | /* Let's try to start it */ | |
515 | if (match && match->bitmap_file) { | |
516 | int bmfd = open(match->bitmap_file, O_RDWR); | |
517 | if (bmfd < 0) { | |
518 | fprintf(stderr, Name | |
519 | ": Could not open bitmap file %s.\n", | |
520 | match->bitmap_file); | |
521 | goto out; | |
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 | goto out; | |
529 | } | |
530 | close(bmfd); | |
531 | } | |
532 | /* Need to remove from the array any devices which | |
533 | * 'count_active' discerned were too old or inappropriate | |
534 | */ | |
535 | for (d = sra ? sra->devs : NULL ; d ; d = d->next) | |
536 | if (d->disk.state & (1<<MD_DISK_REMOVED)) | |
537 | remove_disk(mdfd, st, sra, d); | |
538 | ||
539 | if ((sra == NULL || active_disks >= info.array.working_disks) | |
540 | && trustworthy != FOREIGN) | |
541 | rv = ioctl(mdfd, RUN_ARRAY, NULL); | |
542 | else | |
543 | rv = sysfs_set_str(sra, NULL, | |
544 | "array_state", "read-auto"); | |
545 | if (rv == 0) { | |
546 | if (verbose >= 0) | |
547 | fprintf(stderr, Name | |
548 | ": %s attached to %s, which has been started.\n", | |
549 | devname, chosen_name); | |
550 | rv = 0; | |
551 | wait_for(chosen_name, mdfd); | |
552 | /* We just started the array, so some devices | |
553 | * might have been evicted from the array | |
554 | * because their event counts were too old. | |
555 | * If the action=re-add policy is in-force for | |
556 | * those devices we should re-add them now. | |
557 | */ | |
558 | for (dsk = sra->devs; dsk ; dsk = dsk->next) { | |
559 | if (disk_action_allows(dsk, st->ss->name, act_re_add) && | |
560 | add_disk(mdfd, st, sra, dsk) == 0) | |
561 | fprintf(stderr, Name | |
562 | ": %s re-added to %s\n", | |
563 | dsk->sys_name, chosen_name); | |
564 | } | |
565 | } else { | |
566 | fprintf(stderr, Name | |
567 | ": %s attached to %s, but failed to start: %s.\n", | |
568 | devname, chosen_name, strerror(errno)); | |
569 | rv = 1; | |
570 | } | |
571 | } else { | |
572 | if (verbose >= 0) | |
573 | fprintf(stderr, Name | |
574 | ": %s attached to %s, not enough to start safely.\n", | |
575 | devname, chosen_name); | |
576 | rv = 0; | |
577 | } | |
578 | out: | |
579 | free(avail); | |
580 | if (dfd >= 0) | |
581 | close(dfd); | |
582 | if (mdfd >= 0) | |
583 | close(mdfd); | |
584 | if (policy) | |
585 | dev_policy_free(policy); | |
586 | if (sra) | |
587 | sysfs_free(sra); | |
588 | return rv; | |
589 | } | |
590 | ||
591 | static struct mddev_ident *search_mdstat(struct supertype *st, | |
592 | struct mdinfo *info, | |
593 | char *devname, | |
594 | int verbose, int *rvp) | |
595 | { | |
596 | struct mddev_ident *array_list, *match; | |
597 | array_list = conf_get_ident(NULL); | |
598 | match = NULL; | |
599 | for (; array_list; array_list = array_list->next) { | |
600 | if (array_list->uuid_set && | |
601 | same_uuid(array_list->uuid, info->uuid, st->ss->swapuuid) | |
602 | == 0) { | |
603 | if (verbose >= 2 && array_list->devname) | |
604 | fprintf(stderr, Name | |
605 | ": UUID differs from %s.\n", | |
606 | array_list->devname); | |
607 | continue; | |
608 | } | |
609 | if (array_list->name[0] && | |
610 | strcasecmp(array_list->name, info->name) != 0) { | |
611 | if (verbose >= 2 && array_list->devname) | |
612 | fprintf(stderr, Name | |
613 | ": Name differs from %s.\n", | |
614 | array_list->devname); | |
615 | continue; | |
616 | } | |
617 | if (array_list->devices && | |
618 | !match_oneof(array_list->devices, devname)) { | |
619 | if (verbose >= 2 && array_list->devname) | |
620 | fprintf(stderr, Name | |
621 | ": Not a listed device for %s.\n", | |
622 | array_list->devname); | |
623 | continue; | |
624 | } | |
625 | if (array_list->super_minor != UnSet && | |
626 | array_list->super_minor != info->array.md_minor) { | |
627 | if (verbose >= 2 && array_list->devname) | |
628 | fprintf(stderr, Name | |
629 | ": Different super-minor to %s.\n", | |
630 | array_list->devname); | |
631 | continue; | |
632 | } | |
633 | if (!array_list->uuid_set && | |
634 | !array_list->name[0] && | |
635 | !array_list->devices && | |
636 | array_list->super_minor == UnSet) { | |
637 | if (verbose >= 2 && array_list->devname) | |
638 | fprintf(stderr, Name | |
639 | ": %s doesn't have any identifying information.\n", | |
640 | array_list->devname); | |
641 | continue; | |
642 | } | |
643 | /* FIXME, should I check raid_disks and level too?? */ | |
644 | ||
645 | if (match) { | |
646 | if (verbose >= 0) { | |
647 | if (match->devname && array_list->devname) | |
648 | fprintf(stderr, Name | |
649 | ": we match both %s and %s - cannot decide which to use.\n", | |
650 | match->devname, array_list->devname); | |
651 | else | |
652 | fprintf(stderr, Name | |
653 | ": multiple lines in mdadm.conf match\n"); | |
654 | } | |
655 | *rvp = 2; | |
656 | match = NULL; | |
657 | break; | |
658 | } | |
659 | match = array_list; | |
660 | } | |
661 | return match; | |
662 | } | |
663 | ||
664 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, | |
665 | int number, __u64 events, int verbose, | |
666 | char *array_name) | |
667 | { | |
668 | /* Find a device attached to this array with a disk.number of number | |
669 | * and events less than the passed events, and remove the device. | |
670 | */ | |
671 | struct mdinfo *d; | |
672 | mdu_array_info_t ra; | |
673 | ||
674 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
675 | return; /* not safe to remove from active arrays | |
676 | * without thinking more */ | |
677 | ||
678 | for (d = sra->devs; d ; d = d->next) { | |
679 | char dn[10]; | |
680 | int dfd; | |
681 | struct mdinfo info; | |
682 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); | |
683 | dfd = dev_open(dn, O_RDONLY); | |
684 | if (dfd < 0) | |
685 | continue; | |
686 | if (st->ss->load_super(st, dfd, NULL)) { | |
687 | close(dfd); | |
688 | continue; | |
689 | } | |
690 | st->ss->getinfo_super(st, &info, NULL); | |
691 | st->ss->free_super(st); | |
692 | close(dfd); | |
693 | ||
694 | if (info.disk.number != number || | |
695 | info.events >= events) | |
696 | continue; | |
697 | ||
698 | if (d->disk.raid_disk > -1) | |
699 | sysfs_set_str(sra, d, "slot", "none"); | |
700 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
701 | if (verbose >= 0) | |
702 | fprintf(stderr, Name | |
703 | ": removing old device %s from %s\n", | |
704 | d->sys_name+4, array_name); | |
705 | } | |
706 | } | |
707 | ||
708 | static int count_active(struct supertype *st, struct mdinfo *sra, | |
709 | int mdfd, char **availp, | |
710 | struct mdinfo *bestinfo) | |
711 | { | |
712 | /* count how many devices in sra think they are active */ | |
713 | struct mdinfo *d; | |
714 | int cnt = 0; | |
715 | __u64 max_events = 0; | |
716 | char *avail = NULL; | |
717 | int *best; | |
718 | char *devmap = NULL; | |
719 | int numdevs = 0; | |
720 | int devnum; | |
721 | int b, i; | |
722 | int raid_disks = 0; | |
723 | ||
724 | if (!sra) | |
725 | return 0; | |
726 | ||
727 | for (d = sra->devs ; d ; d = d->next) | |
728 | numdevs++; | |
729 | for (d = sra->devs, devnum=0 ; d ; d = d->next, devnum++) { | |
730 | char dn[30]; | |
731 | int dfd; | |
732 | int ok; | |
733 | struct mdinfo info; | |
734 | ||
735 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); | |
736 | dfd = dev_open(dn, O_RDONLY); | |
737 | if (dfd < 0) | |
738 | continue; | |
739 | ok = st->ss->load_super(st, dfd, NULL); | |
740 | close(dfd); | |
741 | if (ok != 0) | |
742 | continue; | |
743 | info.array.raid_disks = raid_disks; | |
744 | st->ss->getinfo_super(st, &info, devmap + raid_disks * devnum); | |
745 | if (!avail) { | |
746 | raid_disks = info.array.raid_disks; | |
747 | avail = calloc(raid_disks, 1); | |
748 | if (!avail) { | |
749 | fprintf(stderr, Name ": out of memory.\n"); | |
750 | exit(1); | |
751 | } | |
752 | *availp = avail; | |
753 | ||
754 | best = calloc(raid_disks, sizeof(int)); | |
755 | devmap = calloc(raid_disks * numdevs, 1); | |
756 | ||
757 | st->ss->getinfo_super(st, &info, devmap); | |
758 | } | |
759 | ||
760 | if (info.disk.state & (1<<MD_DISK_SYNC)) | |
761 | { | |
762 | if (cnt == 0) { | |
763 | cnt++; | |
764 | max_events = info.events; | |
765 | avail[info.disk.raid_disk] = 2; | |
766 | best[info.disk.raid_disk] = devnum; | |
767 | st->ss->getinfo_super(st, bestinfo, NULL); | |
768 | } else if (info.events == max_events) { | |
769 | avail[info.disk.raid_disk] = 2; | |
770 | best[info.disk.raid_disk] = devnum; | |
771 | } else if (info.events == max_events-1) { | |
772 | if (avail[info.disk.raid_disk] == 0) { | |
773 | avail[info.disk.raid_disk] = 1; | |
774 | best[info.disk.raid_disk] = devnum; | |
775 | } | |
776 | } else if (info.events < max_events - 1) | |
777 | ; | |
778 | else if (info.events == max_events+1) { | |
779 | int i; | |
780 | max_events = info.events; | |
781 | for (i=0; i < raid_disks; i++) | |
782 | if (avail[i]) | |
783 | avail[i]--; | |
784 | avail[info.disk.raid_disk] = 2; | |
785 | best[info.disk.raid_disk] = devnum; | |
786 | st->ss->getinfo_super(st, bestinfo, NULL); | |
787 | } else { /* info.events much bigger */ | |
788 | memset(avail, 0, info.disk.raid_disk); | |
789 | max_events = info.events; | |
790 | avail[info.disk.raid_disk] = 2; | |
791 | st->ss->getinfo_super(st, bestinfo, NULL); | |
792 | } | |
793 | } | |
794 | st->ss->free_super(st); | |
795 | } | |
796 | if (!avail) | |
797 | return 0; | |
798 | /* We need to reject any device that thinks the best device is | |
799 | * failed or missing */ | |
800 | for (b = 0; b < raid_disks; b++) | |
801 | if (avail[b] == 2) | |
802 | break; | |
803 | cnt = 0; | |
804 | for (i = 0 ; i < raid_disks ; i++) { | |
805 | if (i != b && avail[i]) | |
806 | if (devmap[raid_disks * best[i] + b] == 0) { | |
807 | /* This device thinks 'b' is failed - | |
808 | * don't use it */ | |
809 | devnum = best[i]; | |
810 | for (d=sra->devs ; devnum; d = d->next) | |
811 | devnum--; | |
812 | d->disk.state |= (1 << MD_DISK_REMOVED); | |
813 | avail[i] = 0; | |
814 | } | |
815 | if (avail[i]) | |
816 | cnt++; | |
817 | } | |
818 | free(best); | |
819 | free(devmap); | |
820 | return cnt; | |
821 | } | |
822 | ||
823 | /* test if container has degraded member(s) */ | |
824 | static int container_members_max_degradation(struct map_ent *map, struct map_ent *me) | |
825 | { | |
826 | mdu_array_info_t array; | |
827 | int afd; | |
828 | int max_degraded = 0; | |
829 | ||
830 | for(; map; map = map->next) { | |
831 | if (!is_subarray(map->metadata) || | |
832 | devname2devnum(map->metadata+1) != me->devnum) | |
833 | continue; | |
834 | afd = open_dev(map->devnum); | |
835 | if (afd < 0) | |
836 | continue; | |
837 | /* most accurate information regarding array degradation */ | |
838 | if (ioctl(afd, GET_ARRAY_INFO, &array) >= 0) { | |
839 | int degraded = array.raid_disks - array.active_disks - | |
840 | array.spare_disks; | |
841 | if (degraded > max_degraded) | |
842 | max_degraded = degraded; | |
843 | } | |
844 | close(afd); | |
845 | } | |
846 | return (max_degraded); | |
847 | } | |
848 | ||
849 | static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol, | |
850 | struct map_ent *target, int bare, | |
851 | struct supertype *st, int verbose) | |
852 | { | |
853 | /* This device doesn't have any md metadata | |
854 | * The device policy allows 'spare' and if !bare, it allows spare-same-slot. | |
855 | * If 'st' is not set, then we only know that some metadata allows this, | |
856 | * others possibly don't. | |
857 | * So look for a container or array to attach the device to. | |
858 | * Prefer 'target' if that is set and the array is found. | |
859 | * | |
860 | * If st is set, then only arrays of that type are considered | |
861 | * Return 0 on success, or some exit code on failure, probably 1. | |
862 | */ | |
863 | int rv = 1; | |
864 | struct stat stb; | |
865 | struct map_ent *mp, *map = NULL; | |
866 | struct mdinfo *chosen = NULL; | |
867 | int dfd = *dfdp; | |
868 | ||
869 | if (fstat(dfd, &stb) != 0) | |
870 | return 1; | |
871 | ||
872 | /* | |
873 | * Now we need to find a suitable array to add this to. | |
874 | * We only accept arrays that: | |
875 | * - match 'st' | |
876 | * - are in the same domains as the device | |
877 | * - are of an size for which the device will be useful | |
878 | * and we choose the one that is the most degraded | |
879 | */ | |
880 | ||
881 | if (map_lock(&map)) { | |
882 | fprintf(stderr, Name ": failed to get exclusive lock on " | |
883 | "mapfile\n"); | |
884 | return 1; | |
885 | } | |
886 | for (mp = map ; mp ; mp = mp->next) { | |
887 | struct supertype *st2; | |
888 | struct domainlist *dl = NULL; | |
889 | struct mdinfo *sra; | |
890 | unsigned long long devsize; | |
891 | unsigned long long component_size; | |
892 | ||
893 | if (is_subarray(mp->metadata)) | |
894 | continue; | |
895 | if (st) { | |
896 | st2 = st->ss->match_metadata_desc(mp->metadata); | |
897 | if (!st2 || | |
898 | (st->minor_version >= 0 && | |
899 | st->minor_version != st2->minor_version)) { | |
900 | if (verbose > 1) | |
901 | fprintf(stderr, Name ": not adding %s to %s as metadata type doesn't match\n", | |
902 | devname, mp->path); | |
903 | free(st2); | |
904 | continue; | |
905 | } | |
906 | free(st2); | |
907 | } | |
908 | sra = sysfs_read(-1, mp->devnum, | |
909 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE| | |
910 | GET_DEGRADED|GET_COMPONENT|GET_VERSION); | |
911 | if (!sra) { | |
912 | /* Probably a container - no degraded info */ | |
913 | sra = sysfs_read(-1, mp->devnum, | |
914 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE| | |
915 | GET_COMPONENT|GET_VERSION); | |
916 | if (sra) | |
917 | sra->array.failed_disks = -1; | |
918 | } | |
919 | if (!sra) | |
920 | continue; | |
921 | if (st == NULL) { | |
922 | int i; | |
923 | st2 = NULL; | |
924 | for(i=0; !st2 && superlist[i]; i++) | |
925 | st2 = superlist[i]->match_metadata_desc( | |
926 | sra->text_version); | |
927 | if (!st2) { | |
928 | if (verbose > 1) | |
929 | fprintf(stderr, Name ": not adding %s to %s" | |
930 | " as metadata not recognised.\n", | |
931 | devname, mp->path); | |
932 | goto next; | |
933 | } | |
934 | /* Need to double check the 'act_spare' permissions applies | |
935 | * to this metadata. | |
936 | */ | |
937 | if (!policy_action_allows(pol, st2->ss->name, act_spare)) | |
938 | goto next; | |
939 | if (!bare && !policy_action_allows(pol, st2->ss->name, | |
940 | act_spare_same_slot)) | |
941 | goto next; | |
942 | } else | |
943 | st2 = st; | |
944 | /* update number of failed disks for mostly degraded | |
945 | * container member */ | |
946 | if (sra->array.failed_disks == -1) | |
947 | sra->array.failed_disks = container_members_max_degradation(map, mp); | |
948 | ||
949 | get_dev_size(dfd, NULL, &devsize); | |
950 | if (sra->component_size == 0) { | |
951 | /* true for containers, here we must read superblock | |
952 | * to obtain minimum spare size */ | |
953 | struct supertype *st3 = dup_super(st2); | |
954 | int mdfd = open_dev(mp->devnum); | |
955 | if (!mdfd) | |
956 | goto next; | |
957 | if (st3->ss->load_container && | |
958 | !st3->ss->load_container(st3, mdfd, mp->path)) { | |
959 | component_size = st3->ss->min_acceptable_spare_size(st3); | |
960 | st3->ss->free_super(st3); | |
961 | } | |
962 | free(st3); | |
963 | close(mdfd); | |
964 | } | |
965 | if ((sra->component_size > 0 && | |
966 | st2->ss->avail_size(st2, devsize) < sra->component_size) | |
967 | || | |
968 | (sra->component_size == 0 && devsize < component_size)) { | |
969 | if (verbose > 1) | |
970 | fprintf(stderr, Name ": not adding %s to %s as it is too small\n", | |
971 | devname, mp->path); | |
972 | goto next; | |
973 | } | |
974 | /* test against target. | |
975 | * If 'target' is set and 'bare' is false, we only accept | |
976 | * arrays/containers that match 'target'. | |
977 | * If 'target' is set and 'bare' is true, we prefer the | |
978 | * array which matches 'target'. | |
979 | * target is considered only if we deal with degraded array | |
980 | */ | |
981 | if (target) { | |
982 | if (strcmp(target->metadata, mp->metadata) == 0 && | |
983 | memcmp(target->uuid, mp->uuid, | |
984 | sizeof(target->uuid)) == 0 && | |
985 | sra->array.failed_disks > 0) { | |
986 | /* This is our target!! */ | |
987 | if (chosen) | |
988 | sysfs_free(chosen); | |
989 | chosen = sra; | |
990 | sra = NULL; | |
991 | /* skip to end so we don't check any more */ | |
992 | while (mp->next) | |
993 | mp = mp->next; | |
994 | goto next; | |
995 | } | |
996 | /* not our target */ | |
997 | if (!bare) | |
998 | goto next; | |
999 | } | |
1000 | ||
1001 | dl = domain_from_array(sra, st2->ss->name); | |
1002 | if (!domain_test(dl, pol, st2->ss->name)) { | |
1003 | /* domain test fails */ | |
1004 | if (verbose > 1) | |
1005 | fprintf(stderr, Name ": not adding %s to %s as" | |
1006 | " it is not in a compatible domain\n", | |
1007 | devname, mp->path); | |
1008 | ||
1009 | goto next; | |
1010 | } | |
1011 | /* all tests passed, OK to add to this array */ | |
1012 | if (!chosen) { | |
1013 | chosen = sra; | |
1014 | sra = NULL; | |
1015 | } else if (chosen->array.failed_disks < sra->array.failed_disks) { | |
1016 | sysfs_free(chosen); | |
1017 | chosen = sra; | |
1018 | sra = NULL; | |
1019 | } | |
1020 | next: | |
1021 | if (sra) | |
1022 | sysfs_free(sra); | |
1023 | if (st != st2) | |
1024 | free(st2); | |
1025 | if (dl) | |
1026 | domain_free(dl); | |
1027 | } | |
1028 | if (chosen) { | |
1029 | /* add current device to chosen array as a spare */ | |
1030 | int mdfd = open_dev(devname2devnum(chosen->sys_name)); | |
1031 | if (mdfd >= 0) { | |
1032 | struct mddev_dev devlist; | |
1033 | char devname[20]; | |
1034 | devlist.next = NULL; | |
1035 | devlist.used = 0; | |
1036 | devlist.re_add = 0; | |
1037 | devlist.writemostly = 0; | |
1038 | devlist.devname = devname; | |
1039 | sprintf(devname, "%d:%d", major(stb.st_rdev), | |
1040 | minor(stb.st_rdev)); | |
1041 | devlist.disposition = 'a'; | |
1042 | close(dfd); | |
1043 | *dfdp = -1; | |
1044 | rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist, | |
1045 | -1, 0, NULL); | |
1046 | close(mdfd); | |
1047 | } | |
1048 | if (verbose > 0) { | |
1049 | if (rv == 0) | |
1050 | fprintf(stderr, Name ": added %s as spare for %s\n", | |
1051 | devname, chosen->sys_name); | |
1052 | else | |
1053 | fprintf(stderr, Name ": failed to add %s as spare for %s\n", | |
1054 | devname, chosen->sys_name); | |
1055 | } | |
1056 | sysfs_free(chosen); | |
1057 | } | |
1058 | return rv; | |
1059 | } | |
1060 | ||
1061 | static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol, | |
1062 | struct supertype *st, int verbose) | |
1063 | { | |
1064 | /* we know that at least one partition virtual-metadata is | |
1065 | * allowed to incorporate spares like this device. We need to | |
1066 | * find a suitable device to copy partition information from. | |
1067 | * | |
1068 | * Getting a list of all disk (not partition) devices is | |
1069 | * slightly non-trivial. We could look at /sys/block, but | |
1070 | * that is theoretically due to be removed. Maybe best to use | |
1071 | * /dev/disk/by-path/?* and ignore names ending '-partNN' as | |
1072 | * we depend on this directory of 'path' info. But that fails | |
1073 | * to find loop devices and probably others. Maybe don't | |
1074 | * worry about that, they aren't the real target. | |
1075 | * | |
1076 | * So: check things in /dev/disk/by-path to see if they are in | |
1077 | * a compatible domain, then load the partition table and see | |
1078 | * if it is OK for the new device, and choose the largest | |
1079 | * partition table that fits. | |
1080 | */ | |
1081 | DIR *dir; | |
1082 | struct dirent *de; | |
1083 | char *chosen = NULL; | |
1084 | unsigned long long chosen_size; | |
1085 | struct supertype *chosen_st = NULL; | |
1086 | int fd; | |
1087 | ||
1088 | dir = opendir("/dev/disk/by-path"); | |
1089 | if (!dir) | |
1090 | return 1; | |
1091 | while ((de = readdir(dir)) != NULL) { | |
1092 | char *ep; | |
1093 | struct dev_policy *pol2 = NULL; | |
1094 | struct domainlist *domlist = NULL; | |
1095 | int fd = -1; | |
1096 | struct mdinfo info; | |
1097 | struct supertype *st2 = NULL; | |
1098 | char *devname = NULL; | |
1099 | unsigned long long devsectors; | |
1100 | ||
1101 | if (de->d_ino == 0 || | |
1102 | de->d_name[0] == '.' || | |
1103 | (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN)) | |
1104 | goto next; | |
1105 | ||
1106 | ep = de->d_name + strlen(de->d_name); | |
1107 | while (ep > de->d_name && | |
1108 | isdigit(ep[-1])) | |
1109 | ep--; | |
1110 | if (ep > de->d_name + 5 && | |
1111 | strncmp(ep-5, "-part", 5) == 0) | |
1112 | /* This is a partition - skip it */ | |
1113 | goto next; | |
1114 | ||
1115 | pol2 = path_policy(de->d_name, type_disk); | |
1116 | ||
1117 | domain_merge(&domlist, pol2, st ? st->ss->name : NULL); | |
1118 | if (domain_test(domlist, pol, st ? st->ss->name : NULL) == 0) | |
1119 | /* new device is incompatible with this device. */ | |
1120 | goto next; | |
1121 | ||
1122 | domain_free(domlist); | |
1123 | domlist = NULL; | |
1124 | ||
1125 | asprintf(&devname, "/dev/disk/by-path/%s", de->d_name); | |
1126 | fd = open(devname, O_RDONLY); | |
1127 | if (fd < 0) | |
1128 | goto next; | |
1129 | if (get_dev_size(fd, devname, &devsectors) == 0) | |
1130 | goto next; | |
1131 | devsectors >>= 9; | |
1132 | ||
1133 | if (st) | |
1134 | st2 = dup_super(st); | |
1135 | else | |
1136 | st2 = guess_super_type(fd, guess_partitions); | |
1137 | if (st2 == NULL || | |
1138 | st2->ss->load_super(st2, fd, NULL) < 0) | |
1139 | goto next; | |
1140 | ||
1141 | if (!st) { | |
1142 | /* Check domain policy again, this time referring to metadata */ | |
1143 | domain_merge(&domlist, pol2, st2->ss->name); | |
1144 | if (domain_test(domlist, pol, st2->ss->name) == 0) | |
1145 | /* Incompatible devices for this metadata type */ | |
1146 | goto next; | |
1147 | if (!policy_action_allows(pol, st2->ss->name, act_spare)) | |
1148 | /* Some partition types allow sparing, but not | |
1149 | * this one. | |
1150 | */ | |
1151 | goto next; | |
1152 | } | |
1153 | ||
1154 | st2->ss->getinfo_super(st2, &info, NULL); | |
1155 | if (info.component_size > devsectors) | |
1156 | /* This partitioning doesn't fit in the device */ | |
1157 | goto next; | |
1158 | ||
1159 | /* This is an acceptable device to copy partition | |
1160 | * metadata from. We could just stop here, but I | |
1161 | * think I want to keep looking incase a larger | |
1162 | * metadata which makes better use of the device can | |
1163 | * be found. | |
1164 | */ | |
1165 | if (chosen == NULL || | |
1166 | chosen_size < info.component_size) { | |
1167 | chosen_size = info.component_size; | |
1168 | free(chosen); | |
1169 | chosen = devname; | |
1170 | devname = NULL; | |
1171 | if (chosen_st) { | |
1172 | chosen_st->ss->free_super(chosen_st); | |
1173 | free(chosen_st); | |
1174 | } | |
1175 | chosen_st = st2; | |
1176 | st2 = NULL; | |
1177 | } | |
1178 | ||
1179 | next: | |
1180 | free(devname); | |
1181 | domain_free(domlist); | |
1182 | dev_policy_free(pol2); | |
1183 | if (st2) | |
1184 | st2->ss->free_super(st2); | |
1185 | free(st2); | |
1186 | ||
1187 | if (fd >= 0) | |
1188 | close(fd); | |
1189 | } | |
1190 | ||
1191 | if (!chosen) | |
1192 | return 1; | |
1193 | ||
1194 | /* 'chosen' is the best device we can find. Let's write its | |
1195 | * metadata to devname dfd is read-only so don't use that | |
1196 | */ | |
1197 | fd = open(devname, O_RDWR); | |
1198 | if (fd >= 0) { | |
1199 | chosen_st->ss->store_super(chosen_st, fd); | |
1200 | close(fd); | |
1201 | } | |
1202 | free(chosen); | |
1203 | chosen_st->ss->free_super(chosen_st); | |
1204 | free(chosen_st); | |
1205 | return 0; | |
1206 | } | |
1207 | ||
1208 | static int is_bare(int dfd) | |
1209 | { | |
1210 | unsigned long long size = 0; | |
1211 | char bufpad[4096 + 4096]; | |
1212 | char *buf = (char*)(((long)bufpad + 4096) & ~4095); | |
1213 | ||
1214 | if (lseek(dfd, 0, SEEK_SET) != 0 || | |
1215 | read(dfd, buf, 4096) != 4096) | |
1216 | return 0; | |
1217 | ||
1218 | if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff') | |
1219 | return 0; | |
1220 | if (memcmp(buf, buf+1, 4095) != 0) | |
1221 | return 0; | |
1222 | ||
1223 | /* OK, first 4K appear blank, try the end. */ | |
1224 | get_dev_size(dfd, NULL, &size); | |
1225 | if (lseek(dfd, size-4096, SEEK_SET) < 0 || | |
1226 | read(dfd, buf, 4096) != 4096) | |
1227 | return 0; | |
1228 | ||
1229 | if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff') | |
1230 | return 0; | |
1231 | if (memcmp(buf, buf+1, 4095) != 0) | |
1232 | return 0; | |
1233 | ||
1234 | return 1; | |
1235 | } | |
1236 | ||
1237 | /* adding a spare to a regular array is quite different from adding one to | |
1238 | * a set-of-partitions virtual array. | |
1239 | * This function determines which is worth trying and tries as appropriate. | |
1240 | * Arrays are given priority over partitions. | |
1241 | */ | |
1242 | static int try_spare(char *devname, int *dfdp, struct dev_policy *pol, | |
1243 | struct map_ent *target, | |
1244 | struct supertype *st, int verbose) | |
1245 | { | |
1246 | int i; | |
1247 | int rv; | |
1248 | int arrays_ok = 0; | |
1249 | int partitions_ok = 0; | |
1250 | int dfd = *dfdp; | |
1251 | int bare; | |
1252 | ||
1253 | /* Can only add a spare if device has at least one domain */ | |
1254 | if (pol_find(pol, pol_domain) == NULL) | |
1255 | return 1; | |
1256 | /* And only if some action allows spares */ | |
1257 | if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare)) | |
1258 | return 1; | |
1259 | ||
1260 | /* Now check if the device is bare. | |
1261 | * bare devices can always be added as a spare | |
1262 | * non-bare devices can only be added if spare-same-slot is permitted, | |
1263 | * and this device is replacing a previous device - in which case 'target' | |
1264 | * will be set. | |
1265 | */ | |
1266 | if (!is_bare(dfd)) { | |
1267 | /* Must have a target and allow same_slot */ | |
1268 | /* Later - may allow force_spare without target */ | |
1269 | if (!target || | |
1270 | !policy_action_allows(pol, st?st->ss->name:NULL, | |
1271 | act_spare_same_slot)) { | |
1272 | if (verbose > 1) | |
1273 | fprintf(stderr, Name ": %s is not bare, so not " | |
1274 | "considering as a spare\n", | |
1275 | devname); | |
1276 | return 1; | |
1277 | } | |
1278 | bare = 0; | |
1279 | } else | |
1280 | bare = 1; | |
1281 | ||
1282 | /* It might be OK to add this device to an array - need to see | |
1283 | * what arrays might be candidates. | |
1284 | */ | |
1285 | if (st) { | |
1286 | /* just try try 'array' or 'partition' based on this metadata */ | |
1287 | if (st->ss->add_to_super) | |
1288 | return array_try_spare(devname, dfdp, pol, target, bare, | |
1289 | st, verbose); | |
1290 | else | |
1291 | return partition_try_spare(devname, dfdp, pol, | |
1292 | st, verbose); | |
1293 | } | |
1294 | /* No metadata was specified or found so options are open. | |
1295 | * Check for whether any array metadata, or any partition metadata | |
1296 | * might allow adding the spare. This check is just help to avoid | |
1297 | * a more costly scan of all arrays when we can be sure that will | |
1298 | * fail. | |
1299 | */ | |
1300 | for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) { | |
1301 | if (superlist[i]->add_to_super && !arrays_ok && | |
1302 | policy_action_allows(pol, superlist[i]->name, act_spare)) | |
1303 | arrays_ok = 1; | |
1304 | if (superlist[i]->add_to_super == NULL && !partitions_ok && | |
1305 | policy_action_allows(pol, superlist[i]->name, act_spare)) | |
1306 | partitions_ok = 1; | |
1307 | } | |
1308 | rv = 1; | |
1309 | if (arrays_ok) | |
1310 | rv = array_try_spare(devname, dfdp, pol, target, bare, | |
1311 | st, verbose); | |
1312 | if (rv != 0 && partitions_ok) | |
1313 | rv = partition_try_spare(devname, dfdp, pol, st, verbose); | |
1314 | return rv; | |
1315 | } | |
1316 | ||
1317 | int IncrementalScan(int verbose) | |
1318 | { | |
1319 | /* look at every device listed in the 'map' file. | |
1320 | * If one is found that is not running then: | |
1321 | * look in mdadm.conf for bitmap file. | |
1322 | * if one exists, but array has none, add it. | |
1323 | * try to start array in auto-readonly mode | |
1324 | */ | |
1325 | struct map_ent *mapl = NULL; | |
1326 | struct map_ent *me; | |
1327 | struct mddev_ident *devs, *mddev; | |
1328 | int rv = 0; | |
1329 | ||
1330 | map_read(&mapl); | |
1331 | devs = conf_get_ident(NULL); | |
1332 | ||
1333 | for (me = mapl ; me ; me = me->next) { | |
1334 | mdu_array_info_t array; | |
1335 | mdu_bitmap_file_t bmf; | |
1336 | struct mdinfo *sra; | |
1337 | int mdfd = open_dev(me->devnum); | |
1338 | ||
1339 | if (mdfd < 0) | |
1340 | continue; | |
1341 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
1342 | errno != ENODEV) { | |
1343 | close(mdfd); | |
1344 | continue; | |
1345 | } | |
1346 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
1347 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
1348 | if (mddev->devname && me->path | |
1349 | && devname_matches(mddev->devname, me->path)) | |
1350 | break; | |
1351 | if (mddev && mddev->bitmap_file) { | |
1352 | /* | |
1353 | * Note: early kernels will wrongly fail this, so it | |
1354 | * is a hint only | |
1355 | */ | |
1356 | int added = -1; | |
1357 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
1358 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
1359 | if (bmfd >= 0) { | |
1360 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
1361 | bmfd); | |
1362 | close(bmfd); | |
1363 | } | |
1364 | } | |
1365 | if (verbose >= 0) { | |
1366 | if (added == 0) | |
1367 | fprintf(stderr, Name | |
1368 | ": Added bitmap %s to %s\n", | |
1369 | mddev->bitmap_file, me->path); | |
1370 | else if (errno != EEXIST) | |
1371 | fprintf(stderr, Name | |
1372 | ": Failed to add bitmap to %s: %s\n", | |
1373 | me->path, strerror(errno)); | |
1374 | } | |
1375 | } | |
1376 | sra = sysfs_read(mdfd, 0, 0); | |
1377 | if (sra) { | |
1378 | if (sysfs_set_str(sra, NULL, | |
1379 | "array_state", "read-auto") == 0) { | |
1380 | if (verbose >= 0) | |
1381 | fprintf(stderr, Name | |
1382 | ": started array %s\n", | |
1383 | me->path ?: devnum2devname(me->devnum)); | |
1384 | } else { | |
1385 | fprintf(stderr, Name | |
1386 | ": failed to start array %s: %s\n", | |
1387 | me->path ?: devnum2devname(me->devnum), | |
1388 | strerror(errno)); | |
1389 | rv = 1; | |
1390 | } | |
1391 | } | |
1392 | } | |
1393 | return rv; | |
1394 | } | |
1395 | ||
1396 | static char *container2devname(char *devname) | |
1397 | { | |
1398 | char *mdname = NULL; | |
1399 | ||
1400 | if (devname[0] == '/') { | |
1401 | int fd = open(devname, O_RDONLY); | |
1402 | if (fd >= 0) { | |
1403 | mdname = devnum2devname(fd2devnum(fd)); | |
1404 | close(fd); | |
1405 | } | |
1406 | } else { | |
1407 | int uuid[4]; | |
1408 | struct map_ent *mp, *map = NULL; | |
1409 | ||
1410 | if (!parse_uuid(devname, uuid)) | |
1411 | return mdname; | |
1412 | mp = map_by_uuid(&map, uuid); | |
1413 | if (mp) | |
1414 | mdname = devnum2devname(mp->devnum); | |
1415 | map_free(map); | |
1416 | } | |
1417 | ||
1418 | return mdname; | |
1419 | } | |
1420 | ||
1421 | static int Incremental_container(struct supertype *st, char *devname, | |
1422 | char *homehost, int verbose, | |
1423 | int runstop, int autof) | |
1424 | { | |
1425 | /* Collect the contents of this container and for each | |
1426 | * array, choose a device name and assemble the array. | |
1427 | */ | |
1428 | ||
1429 | struct mdinfo *list; | |
1430 | struct mdinfo *ra; | |
1431 | struct map_ent *map = NULL; | |
1432 | struct mdinfo info; | |
1433 | int trustworthy; | |
1434 | struct mddev_ident *match; | |
1435 | int rv = 0; | |
1436 | ||
1437 | memset(&info, 0, sizeof(info)); | |
1438 | st->ss->getinfo_super(st, &info, NULL); | |
1439 | ||
1440 | if ((runstop > 0 && info.container_enough >= 0) || | |
1441 | info.container_enough > 0) | |
1442 | /* pass */; | |
1443 | else { | |
1444 | if (verbose) | |
1445 | fprintf(stderr, Name ": not enough devices to start the container\n"); | |
1446 | return 0; | |
1447 | } | |
1448 | ||
1449 | match = search_mdstat(st, &info, devname, verbose, &rv); | |
1450 | if (match == NULL && rv == 2) | |
1451 | return rv; | |
1452 | ||
1453 | /* Need to compute 'trustworthy' */ | |
1454 | if (match) | |
1455 | trustworthy = LOCAL; | |
1456 | else if (st->ss->match_home(st, homehost) == 1) | |
1457 | trustworthy = LOCAL; | |
1458 | else if (st->ss->match_home(st, "any") == 1) | |
1459 | trustworthy = LOCAL; | |
1460 | else | |
1461 | trustworthy = FOREIGN; | |
1462 | ||
1463 | list = st->ss->container_content(st, NULL); | |
1464 | if (map_lock(&map)) | |
1465 | fprintf(stderr, Name ": failed to get exclusive lock on " | |
1466 | "mapfile\n"); | |
1467 | /* do not assemble arrays that might have bad blocks */ | |
1468 | if (list->array.state & (1<<MD_SB_BBM_ERRORS)) { | |
1469 | fprintf(stderr, Name ": BBM log found in metadata. " | |
1470 | "Cannot activate array(s).\n"); | |
1471 | list = NULL; | |
1472 | } | |
1473 | ||
1474 | for (ra = list ; ra ; ra = ra->next) { | |
1475 | int mdfd; | |
1476 | char chosen_name[1024]; | |
1477 | struct map_ent *mp; | |
1478 | struct mddev_ident *match = NULL; | |
1479 | ||
1480 | mp = map_by_uuid(&map, ra->uuid); | |
1481 | ||
1482 | if (mp) { | |
1483 | mdfd = open_dev(mp->devnum); | |
1484 | if (mp->path) | |
1485 | strcpy(chosen_name, mp->path); | |
1486 | else | |
1487 | strcpy(chosen_name, devnum2devname(mp->devnum)); | |
1488 | } else { | |
1489 | ||
1490 | /* Check in mdadm.conf for container == devname and | |
1491 | * member == ra->text_version after second slash. | |
1492 | */ | |
1493 | char *sub = strchr(ra->text_version+1, '/'); | |
1494 | struct mddev_ident *array_list; | |
1495 | if (sub) { | |
1496 | sub++; | |
1497 | array_list = conf_get_ident(NULL); | |
1498 | } else | |
1499 | array_list = NULL; | |
1500 | for(; array_list ; array_list = array_list->next) { | |
1501 | char *dn; | |
1502 | if (array_list->member == NULL || | |
1503 | array_list->container == NULL) | |
1504 | continue; | |
1505 | if (strcmp(array_list->member, sub) != 0) | |
1506 | continue; | |
1507 | if (array_list->uuid_set && | |
1508 | !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid)) | |
1509 | continue; | |
1510 | dn = container2devname(array_list->container); | |
1511 | if (dn == NULL) | |
1512 | continue; | |
1513 | if (strncmp(dn, ra->text_version+1, | |
1514 | strlen(dn)) != 0 || | |
1515 | ra->text_version[strlen(dn)+1] != '/') { | |
1516 | free(dn); | |
1517 | continue; | |
1518 | } | |
1519 | free(dn); | |
1520 | /* we have a match */ | |
1521 | match = array_list; | |
1522 | if (verbose>0) | |
1523 | fprintf(stderr, Name ": match found for member %s\n", | |
1524 | array_list->member); | |
1525 | break; | |
1526 | } | |
1527 | ||
1528 | if (match && match->devname && | |
1529 | strcasecmp(match->devname, "<ignore>") == 0) { | |
1530 | if (verbose > 0) | |
1531 | fprintf(stderr, Name ": array %s/%s is " | |
1532 | "explicitly ignored by mdadm.conf\n", | |
1533 | match->container, match->member); | |
1534 | return 2; | |
1535 | } | |
1536 | if (match) | |
1537 | trustworthy = LOCAL; | |
1538 | ||
1539 | mdfd = create_mddev(match ? match->devname : NULL, | |
1540 | ra->name, | |
1541 | autof, | |
1542 | trustworthy, | |
1543 | chosen_name); | |
1544 | } | |
1545 | ||
1546 | if (mdfd < 0) { | |
1547 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
1548 | chosen_name, strerror(errno)); | |
1549 | return 2; | |
1550 | } | |
1551 | ||
1552 | assemble_container_content(st, mdfd, ra, runstop, | |
1553 | chosen_name, verbose); | |
1554 | } | |
1555 | map_unlock(&map); | |
1556 | return 0; | |
1557 | } | |
1558 | ||
1559 | /* | |
1560 | * IncrementalRemove - Attempt to see if the passed in device belongs to any | |
1561 | * raid arrays, and if so first fail (if needed) and then remove the device. | |
1562 | * | |
1563 | * @devname - The device we want to remove | |
1564 | * @id_path - name as found in /dev/disk/by-path for this device | |
1565 | * | |
1566 | * Note: the device name must be a kernel name like "sda", so | |
1567 | * that we can find it in /proc/mdstat | |
1568 | */ | |
1569 | int IncrementalRemove(char *devname, char *id_path, int verbose) | |
1570 | { | |
1571 | int mdfd; | |
1572 | int rv; | |
1573 | struct mdstat_ent *ent; | |
1574 | struct mddev_dev devlist; | |
1575 | ||
1576 | if (!id_path) | |
1577 | dprintf(Name ": incremental removal without --path <id_path> " | |
1578 | "lacks the possibility to re-add new device in this " | |
1579 | "port\n"); | |
1580 | ||
1581 | if (strchr(devname, '/')) { | |
1582 | fprintf(stderr, Name ": incremental removal requires a " | |
1583 | "kernel device name, not a file: %s\n", devname); | |
1584 | return 1; | |
1585 | } | |
1586 | ent = mdstat_by_component(devname); | |
1587 | if (!ent) { | |
1588 | fprintf(stderr, Name ": %s does not appear to be a component " | |
1589 | "of any array\n", devname); | |
1590 | return 1; | |
1591 | } | |
1592 | mdfd = open_dev(ent->devnum); | |
1593 | if (mdfd < 0) { | |
1594 | fprintf(stderr, Name ": Cannot open array %s!!\n", ent->dev); | |
1595 | free_mdstat(ent); | |
1596 | return 1; | |
1597 | } | |
1598 | ||
1599 | if (id_path) { | |
1600 | struct map_ent *map = NULL, *me; | |
1601 | me = map_by_devnum(&map, ent->devnum); | |
1602 | if (me) | |
1603 | policy_save_path(id_path, me); | |
1604 | map_free(map); | |
1605 | } | |
1606 | ||
1607 | memset(&devlist, 0, sizeof(devlist)); | |
1608 | devlist.devname = devname; | |
1609 | devlist.disposition = 'f'; | |
1610 | /* for a container, we must fail each member array */ | |
1611 | if (ent->metadata_version && | |
1612 | strncmp(ent->metadata_version, "external:", 9) == 0) { | |
1613 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
1614 | struct mdstat_ent *memb; | |
1615 | for (memb = mdstat ; memb ; memb = memb->next) | |
1616 | if (is_container_member(memb, ent->dev)) { | |
1617 | int subfd = open_dev(memb->devnum); | |
1618 | if (subfd >= 0) { | |
1619 | Manage_subdevs(memb->dev, subfd, | |
1620 | &devlist, verbose, 0, | |
1621 | NULL); | |
1622 | close(subfd); | |
1623 | } | |
1624 | } | |
1625 | free_mdstat(mdstat); | |
1626 | } else | |
1627 | Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL); | |
1628 | devlist.disposition = 'r'; | |
1629 | rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0, NULL); | |
1630 | close(mdfd); | |
1631 | free_mdstat(ent); | |
1632 | return rv; | |
1633 | } |