]>
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" | |
61018da0 N |
32 | #include <dirent.h> |
33 | #include <ctype.h> | |
8382f19b | 34 | |
2bf740b3 N |
35 | static int count_active(struct supertype *st, struct mdinfo *sra, |
36 | int mdfd, char **availp, | |
8382f19b | 37 | struct mdinfo *info); |
7e0f6979 | 38 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
39 | int number, __u64 events, int verbose, |
40 | char *array_name); | |
f08605b3 | 41 | static int try_spare(char *devname, int *dfdp, struct dev_policy *pol, |
d2db3045 | 42 | struct map_ent *target, |
f08605b3 | 43 | struct supertype *st, int verbose); |
8382f19b | 44 | |
7d91c3f5 | 45 | static int Incremental_container(struct supertype *st, char *devname, |
47c74f3f N |
46 | char *homehost, |
47 | int verbose, int runstop, int autof); | |
7d91c3f5 | 48 | |
fa56eddb | 49 | static struct mddev_ident *search_mdstat(struct supertype *st, |
3a97f210 N |
50 | struct mdinfo *info, |
51 | char *devname, | |
52 | int verbose, int *rvp); | |
53 | ||
8382f19b | 54 | int Incremental(char *devname, int verbose, int runstop, |
0ac91628 N |
55 | struct supertype *st, char *homehost, int require_homehost, |
56 | int autof) | |
8382f19b NB |
57 | { |
58 | /* Add this device to an array, creating the array if necessary | |
598f0d58 | 59 | * and starting the array if sensible or - if runstop>0 - if possible. |
8382f19b NB |
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 | |
215bb3f7 N |
67 | * 3a/ if not, check for homehost match. If no match, assemble as |
68 | * a 'foreign' array. | |
8382f19b NB |
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 | |
352452c3 | 76 | * Don't choose partitioned for containers. |
8382f19b NB |
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? | |
352452c3 | 88 | * For a container, this means running Incremental_container. |
8382f19b NB |
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; | |
7e83544b | 96 | struct mdinfo info, dinfo; |
2bf740b3 | 97 | struct mdinfo *sra = NULL; |
fa56eddb | 98 | struct mddev_ident *match; |
8382f19b | 99 | char chosen_name[1024]; |
15d4a7e4 | 100 | int rv = 1; |
8382f19b | 101 | struct map_ent *mp, *map = NULL; |
15d4a7e4 | 102 | int dfd = -1, mdfd = -1; |
8382f19b NB |
103 | char *avail; |
104 | int active_disks; | |
47c74f3f | 105 | int trustworthy; |
d7288ddc | 106 | char *name_to_use; |
ad5bc697 | 107 | mdu_array_info_t ainf; |
7e83544b | 108 | struct dev_policy *policy = NULL; |
d2db3045 N |
109 | struct map_ent target_array; |
110 | int have_target; | |
8382f19b NB |
111 | |
112 | struct createinfo *ci = conf_get_create_info(); | |
113 | ||
47c74f3f N |
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 */ | |
3a371610 | 134 | if (must_be_container(dfd)) { |
47c74f3f N |
135 | if (!st) |
136 | st = super_by_fd(dfd, NULL); | |
137 | if (st) | |
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 | } | |
8382f19b | 149 | |
352452c3 | 150 | /* 1/ Check if device is permitted by mdadm.conf */ |
8382f19b NB |
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); | |
15d4a7e4 | 157 | goto out; |
8382f19b NB |
158 | } |
159 | ||
160 | /* 2/ Find metadata, reject if none appropriate (check | |
161 | * version/name from args) */ | |
162 | ||
8382f19b NB |
163 | if (fstat(dfd, &stb) < 0) { |
164 | if (verbose >= 0) | |
165 | fprintf(stderr, Name ": fstat failed for %s: %s.\n", | |
166 | devname, strerror(errno)); | |
15d4a7e4 | 167 | goto out; |
8382f19b NB |
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); | |
15d4a7e4 | 173 | goto out; |
8382f19b NB |
174 | } |
175 | ||
7e83544b N |
176 | dinfo.disk.major = major(stb.st_rdev); |
177 | dinfo.disk.minor = minor(stb.st_rdev); | |
178 | ||
179 | policy = disk_policy(&dinfo); | |
d2db3045 | 180 | have_target = policy_check_path(&dinfo, &target_array); |
7e83544b | 181 | |
8382f19b NB |
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); | |
d2db3045 N |
187 | rv = try_spare(devname, &dfd, policy, |
188 | have_target ? &target_array : NULL, | |
189 | st, verbose); | |
15d4a7e4 | 190 | goto out; |
8382f19b | 191 | } |
0f22b998 N |
192 | if (st->ss->compare_super == NULL || |
193 | st->ss->load_super(st, dfd, NULL)) { | |
8382f19b NB |
194 | if (verbose >= 0) |
195 | fprintf(stderr, Name ": no RAID superblock on %s.\n", | |
196 | devname); | |
d2db3045 N |
197 | rv = try_spare(devname, &dfd, policy, |
198 | have_target ? &target_array : NULL, | |
199 | st, verbose); | |
0f22b998 | 200 | free(st); |
15d4a7e4 | 201 | goto out; |
8382f19b | 202 | } |
15d4a7e4 | 203 | close (dfd); dfd = -1; |
8382f19b | 204 | |
f35f2525 | 205 | memset(&info, 0, sizeof(info)); |
a5d85af7 | 206 | st->ss->getinfo_super(st, &info, NULL); |
8382f19b | 207 | |
3a97f210 N |
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; | |
8382f19b | 212 | |
112cace6 N |
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); | |
15d4a7e4 | 219 | goto out; |
112cace6 N |
220 | } |
221 | ||
7b403fef N |
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. */ | |
215bb3f7 N |
226 | if (match) |
227 | trustworthy = LOCAL; | |
d1d3482b N |
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; | |
215bb3f7 | 232 | else |
d1d3482b N |
233 | trustworthy = FOREIGN; |
234 | ||
235 | ||
4e8d9f0a | 236 | if (!match && !conf_test_metadata(st->ss->name, policy, |
d1d3482b N |
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); | |
15d4a7e4 | 243 | goto out; |
d1d3482b N |
244 | } |
245 | if (trustworthy == LOCAL_ANY) | |
ecb02e31 | 246 | trustworthy = LOCAL; |
e12e1d1d | 247 | |
4ef2f11e N |
248 | /* There are three possible sources for 'autof': command line, |
249 | * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf. | |
350ac35d N |
250 | * ARRAY takes precedence, then command line, then |
251 | * CREATE. | |
4ef2f11e | 252 | */ |
350ac35d | 253 | if (match && match->autof) |
4ef2f11e N |
254 | autof = match->autof; |
255 | if (autof == 0) | |
256 | autof = ci->autof; | |
257 | ||
7cdc0872 | 258 | name_to_use = info.name; |
0ac91628 | 259 | if (name_to_use[0] == 0 && |
ecb02e31 N |
260 | info.array.level == LEVEL_CONTAINER && |
261 | trustworthy == LOCAL) { | |
262 | name_to_use = info.text_version; | |
263 | trustworthy = METADATA; | |
264 | } | |
0ac91628 N |
265 | if (name_to_use[0] && trustworthy != LOCAL && |
266 | ! require_homehost && | |
267 | conf_name_is_free(name_to_use)) | |
268 | trustworthy = LOCAL; | |
ecb02e31 | 269 | |
7cdc0872 N |
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 | ||
ad5bc697 | 276 | /* 4/ Check if array exists. |
215bb3f7 | 277 | */ |
93c861ee DL |
278 | if (map_lock(&map)) |
279 | fprintf(stderr, Name ": failed to get exclusive lock on " | |
280 | "mapfile\n"); | |
215bb3f7 | 281 | mp = map_by_uuid(&map, info.uuid); |
339c2d6c N |
282 | if (mp) |
283 | mdfd = open_dev(mp->devnum); | |
284 | else | |
215bb3f7 | 285 | mdfd = -1; |
d7288ddc | 286 | |
8382f19b | 287 | if (mdfd < 0) { |
8382f19b | 288 | |
215bb3f7 | 289 | /* Couldn't find an existing array, maybe make a new one */ |
ffb3e494 | 290 | mdfd = create_mddev(match ? match->devname : NULL, |
e12e1d1d | 291 | name_to_use, autof, trustworthy, chosen_name); |
215bb3f7 N |
292 | |
293 | if (mdfd < 0) | |
15d4a7e4 | 294 | goto out; |
215bb3f7 N |
295 | |
296 | sysfs_init(&info, mdfd, 0); | |
297 | ||
f35f2525 N |
298 | if (set_array_info(mdfd, st, &info) != 0) { |
299 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", | |
8382f19b | 300 | chosen_name, strerror(errno)); |
15d4a7e4 N |
301 | rv = 2; |
302 | goto out; | |
8382f19b | 303 | } |
7801ac20 | 304 | |
c5afc314 N |
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) { | |
8382f19b NB |
309 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", |
310 | devname, chosen_name, strerror(errno)); | |
311 | ioctl(mdfd, STOP_ARRAY, 0); | |
15d4a7e4 N |
312 | rv = 2; |
313 | goto out; | |
8382f19b | 314 | } |
2bf740b3 N |
315 | sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE | |
316 | GET_OFFSET | GET_SIZE)); | |
317 | ||
06c7f68e | 318 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { |
8382f19b NB |
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"); | |
8382f19b | 327 | sysfs_free(sra); |
15d4a7e4 N |
328 | rv = 2; |
329 | goto out; | |
8382f19b | 330 | } |
c5afc314 | 331 | info.array.working_disks = 1; |
ad5bc697 N |
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); | |
8382f19b NB |
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; | |
8382f19b | 343 | int err; |
3da92f27 | 344 | struct supertype *st2; |
c5afc314 | 345 | struct mdinfo info2, *d; |
215bb3f7 | 346 | |
2bf740b3 N |
347 | sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE | |
348 | GET_OFFSET | GET_SIZE)); | |
349 | ||
339c2d6c N |
350 | if (mp->path) |
351 | strcpy(chosen_name, mp->path); | |
352 | else | |
353 | strcpy(chosen_name, devnum2devname(mp->devnum)); | |
215bb3f7 | 354 | |
ef83fe7c N |
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 | |
7e83544b N |
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 | |
3a6ec29a | 362 | */ |
ef83fe7c | 363 | if ((info.disk.state & (1<<MD_DISK_SYNC)) != 0 |
7e83544b N |
364 | && ! policy_action_allows(policy, st->ss->name, |
365 | act_re_add) | |
ef83fe7c | 366 | && runstop < 1) { |
1dccfff9 DW |
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) { | |
3a6ec29a N |
377 | fprintf(stderr, Name |
378 | ": not adding %s to active array (without --run) %s\n", | |
379 | devname, chosen_name); | |
15d4a7e4 N |
380 | rv = 2; |
381 | goto out; | |
3a6ec29a N |
382 | } |
383 | } | |
15d4a7e4 N |
384 | if (!sra) { |
385 | rv = 2; | |
386 | goto out; | |
387 | } | |
7c548327 N |
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); | |
7c548327 | 399 | close(dfd2); |
15d4a7e4 N |
400 | rv = 2; |
401 | goto out; | |
7c548327 | 402 | } |
8382f19b | 403 | close(dfd2); |
7c548327 | 404 | memset(&info2, 0, sizeof(info2)); |
a5d85af7 | 405 | st2->ss->getinfo_super(st2, &info2, NULL); |
7c548327 N |
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); | |
15d4a7e4 N |
413 | rv = 2; |
414 | goto out; | |
7c548327 | 415 | } |
8382f19b | 416 | } |
7801ac20 N |
417 | info2.disk.major = major(stb.st_rdev); |
418 | info2.disk.minor = minor(stb.st_rdev); | |
c5afc314 N |
419 | /* add disk needs to know about containers */ |
420 | if (st->ss->external) | |
421 | sra->array.level = LEVEL_CONTAINER; | |
ac7de9d9 | 422 | err = add_disk(mdfd, st, sra, &info2); |
8382f19b NB |
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); | |
ac7de9d9 | 429 | err = add_disk(mdfd, st, sra, &info2); |
8382f19b NB |
430 | } |
431 | if (err < 0) { | |
432 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
433 | devname, chosen_name, strerror(errno)); | |
15d4a7e4 N |
434 | rv = 2; |
435 | goto out; | |
8382f19b | 436 | } |
c5afc314 N |
437 | info.array.working_disks = 0; |
438 | for (d = sra->devs; d; d=d->next) | |
439 | info.array.working_disks ++; | |
440 | ||
8382f19b | 441 | } |
8382f19b NB |
442 | |
443 | /* 7/ Is there enough devices to possibly start the array? */ | |
444 | /* 7a/ if not, finish with success. */ | |
352452c3 N |
445 | if (info.array.level == LEVEL_CONTAINER) { |
446 | /* Try to assemble within the container */ | |
ad5bc697 | 447 | map_unlock(&map); |
97590376 | 448 | sysfs_uevent(&info, "change"); |
c5afc314 N |
449 | if (verbose >= 0) |
450 | fprintf(stderr, Name | |
451 | ": container %s now has %d devices\n", | |
452 | chosen_name, info.array.working_disks); | |
a7c6e3fb N |
453 | wait_for(chosen_name, mdfd); |
454 | close(mdfd); | |
2bf740b3 | 455 | sysfs_free(sra); |
03b7f6c6 | 456 | rv = Incremental(chosen_name, verbose, runstop, |
0ac91628 | 457 | NULL, homehost, require_homehost, autof); |
03b7f6c6 N |
458 | if (rv == 1) |
459 | /* Don't fail the whole -I if a subarray didn't | |
460 | * have enough devices to start yet | |
461 | */ | |
462 | rv = 0; | |
463 | return rv; | |
352452c3 | 464 | } |
f8409e54 | 465 | avail = NULL; |
2bf740b3 N |
466 | /* We have added something to the array, so need to re-read the |
467 | * state. Eventually this state should be kept up-to-date as | |
468 | * things change. | |
469 | */ | |
470 | sysfs_free(sra); | |
471 | sra = sysfs_read(mdfd, -1, (GET_DEVS | GET_STATE | | |
472 | GET_OFFSET | GET_SIZE)); | |
473 | active_disks = count_active(st, sra, mdfd, &avail, &info); | |
8382f19b NB |
474 | if (enough(info.array.level, info.array.raid_disks, |
475 | info.array.layout, info.array.state & 1, | |
3288b419 | 476 | avail, active_disks) == 0) { |
8382f19b NB |
477 | free(avail); |
478 | if (verbose >= 0) | |
479 | fprintf(stderr, Name | |
480 | ": %s attached to %s, not enough to start (%d).\n", | |
481 | devname, chosen_name, active_disks); | |
ad5bc697 | 482 | map_unlock(&map); |
15d4a7e4 N |
483 | rv = 0; |
484 | goto out; | |
8382f19b NB |
485 | } |
486 | free(avail); | |
487 | ||
488 | /* 7b/ if yes, */ | |
489 | /* - if number of OK devices match expected, or -R and there */ | |
490 | /* are enough, */ | |
491 | /* + add any bitmap file */ | |
492 | /* + start the array (auto-readonly). */ | |
8382f19b NB |
493 | |
494 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
495 | if (verbose >= 0) | |
496 | fprintf(stderr, Name | |
497 | ": %s attached to %s which is already active.\n", | |
498 | devname, chosen_name); | |
ad5bc697 | 499 | map_unlock(&map); |
15d4a7e4 N |
500 | rv = 0; |
501 | goto out; | |
8382f19b | 502 | } |
ad5bc697 N |
503 | |
504 | map_unlock(&map); | |
8382f19b | 505 | if (runstop > 0 || active_disks >= info.array.working_disks) { |
2bf740b3 | 506 | struct mdinfo *dsk; |
8382f19b NB |
507 | /* Let's try to start it */ |
508 | if (match && match->bitmap_file) { | |
509 | int bmfd = open(match->bitmap_file, O_RDWR); | |
510 | if (bmfd < 0) { | |
511 | fprintf(stderr, Name | |
512 | ": Could not open bitmap file %s.\n", | |
513 | match->bitmap_file); | |
15d4a7e4 | 514 | goto out; |
8382f19b NB |
515 | } |
516 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
517 | close(bmfd); | |
518 | fprintf(stderr, Name | |
519 | ": Failed to set bitmapfile for %s.\n", | |
520 | chosen_name); | |
15d4a7e4 | 521 | goto out; |
8382f19b NB |
522 | } |
523 | close(bmfd); | |
524 | } | |
2bf740b3 | 525 | |
7b403fef | 526 | if ((sra == NULL || active_disks >= info.array.working_disks) |
215bb3f7 | 527 | && trustworthy != FOREIGN) |
8382f19b NB |
528 | rv = ioctl(mdfd, RUN_ARRAY, NULL); |
529 | else | |
530 | rv = sysfs_set_str(sra, NULL, | |
531 | "array_state", "read-auto"); | |
532 | if (rv == 0) { | |
533 | if (verbose >= 0) | |
534 | fprintf(stderr, Name | |
7e83544b | 535 | ": %s attached to %s, which has been started.\n", |
8382f19b NB |
536 | devname, chosen_name); |
537 | rv = 0; | |
a7c6e3fb | 538 | wait_for(chosen_name, mdfd); |
7e83544b N |
539 | /* We just started the array, so some devices |
540 | * might have been evicted from the array | |
541 | * because their event counts were too old. | |
542 | * If the action=re-add policy is in-force for | |
543 | * those devices we should re-add them now. | |
544 | */ | |
545 | for (dsk = sra->devs; dsk ; dsk = dsk->next) { | |
546 | if (disk_action_allows(dsk, st->ss->name, act_re_add) && | |
547 | add_disk(mdfd, st, sra, dsk) == 0) | |
548 | fprintf(stderr, Name | |
549 | ": %s re-added to %s\n", | |
550 | dsk->sys_name, chosen_name); | |
551 | } | |
8382f19b NB |
552 | } else { |
553 | fprintf(stderr, Name | |
554 | ": %s attached to %s, but failed to start: %s.\n", | |
555 | devname, chosen_name, strerror(errno)); | |
556 | rv = 1; | |
557 | } | |
558 | } else { | |
559 | if (verbose >= 0) | |
560 | fprintf(stderr, Name | |
561 | ": %s attached to %s, not enough to start safely.\n", | |
562 | devname, chosen_name); | |
563 | rv = 0; | |
564 | } | |
15d4a7e4 N |
565 | out: |
566 | if (dfd >= 0) | |
567 | close(dfd); | |
568 | if (mdfd >= 0) | |
569 | close(mdfd); | |
7e83544b N |
570 | if (policy) |
571 | dev_policy_free(policy); | |
2bf740b3 N |
572 | if (sra) |
573 | sysfs_free(sra); | |
8382f19b NB |
574 | return rv; |
575 | } | |
576 | ||
fa56eddb | 577 | static struct mddev_ident *search_mdstat(struct supertype *st, |
3a97f210 N |
578 | struct mdinfo *info, |
579 | char *devname, | |
580 | int verbose, int *rvp) | |
581 | { | |
fa56eddb | 582 | struct mddev_ident *array_list, *match; |
3a97f210 N |
583 | array_list = conf_get_ident(NULL); |
584 | match = NULL; | |
585 | for (; array_list; array_list = array_list->next) { | |
586 | if (array_list->uuid_set && | |
587 | same_uuid(array_list->uuid, info->uuid, st->ss->swapuuid) | |
588 | == 0) { | |
589 | if (verbose >= 2 && array_list->devname) | |
590 | fprintf(stderr, Name | |
591 | ": UUID differs from %s.\n", | |
592 | array_list->devname); | |
593 | continue; | |
594 | } | |
595 | if (array_list->name[0] && | |
596 | strcasecmp(array_list->name, info->name) != 0) { | |
597 | if (verbose >= 2 && array_list->devname) | |
598 | fprintf(stderr, Name | |
599 | ": Name differs from %s.\n", | |
600 | array_list->devname); | |
601 | continue; | |
602 | } | |
603 | if (array_list->devices && | |
604 | !match_oneof(array_list->devices, devname)) { | |
605 | if (verbose >= 2 && array_list->devname) | |
606 | fprintf(stderr, Name | |
607 | ": Not a listed device for %s.\n", | |
608 | array_list->devname); | |
609 | continue; | |
610 | } | |
611 | if (array_list->super_minor != UnSet && | |
612 | array_list->super_minor != info->array.md_minor) { | |
613 | if (verbose >= 2 && array_list->devname) | |
614 | fprintf(stderr, Name | |
615 | ": Different super-minor to %s.\n", | |
616 | array_list->devname); | |
617 | continue; | |
618 | } | |
619 | if (!array_list->uuid_set && | |
620 | !array_list->name[0] && | |
621 | !array_list->devices && | |
622 | array_list->super_minor == UnSet) { | |
623 | if (verbose >= 2 && array_list->devname) | |
624 | fprintf(stderr, Name | |
625 | ": %s doesn't have any identifying information.\n", | |
626 | array_list->devname); | |
627 | continue; | |
628 | } | |
629 | /* FIXME, should I check raid_disks and level too?? */ | |
630 | ||
631 | if (match) { | |
632 | if (verbose >= 0) { | |
633 | if (match->devname && array_list->devname) | |
634 | fprintf(stderr, Name | |
635 | ": we match both %s and %s - cannot decide which to use.\n", | |
636 | match->devname, array_list->devname); | |
637 | else | |
638 | fprintf(stderr, Name | |
639 | ": multiple lines in mdadm.conf match\n"); | |
640 | } | |
641 | *rvp = 2; | |
642 | match = NULL; | |
643 | break; | |
644 | } | |
645 | match = array_list; | |
646 | } | |
647 | return match; | |
648 | } | |
649 | ||
7e0f6979 | 650 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
651 | int number, __u64 events, int verbose, |
652 | char *array_name) | |
653 | { | |
3da92f27 | 654 | /* Find a device attached to this array with a disk.number of number |
8382f19b NB |
655 | * and events less than the passed events, and remove the device. |
656 | */ | |
06c7f68e | 657 | struct mdinfo *d; |
8382f19b NB |
658 | mdu_array_info_t ra; |
659 | ||
660 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
661 | return; /* not safe to remove from active arrays | |
662 | * without thinking more */ | |
663 | ||
664 | for (d = sra->devs; d ; d = d->next) { | |
665 | char dn[10]; | |
666 | int dfd; | |
8382f19b | 667 | struct mdinfo info; |
06c7f68e | 668 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
669 | dfd = dev_open(dn, O_RDONLY); |
670 | if (dfd < 0) | |
671 | continue; | |
3da92f27 | 672 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
673 | close(dfd); |
674 | continue; | |
675 | } | |
a5d85af7 | 676 | st->ss->getinfo_super(st, &info, NULL); |
3da92f27 | 677 | st->ss->free_super(st); |
8382f19b NB |
678 | close(dfd); |
679 | ||
680 | if (info.disk.number != number || | |
681 | info.events >= events) | |
682 | continue; | |
683 | ||
06c7f68e | 684 | if (d->disk.raid_disk > -1) |
8382f19b NB |
685 | sysfs_set_str(sra, d, "slot", "none"); |
686 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
687 | if (verbose >= 0) | |
688 | fprintf(stderr, Name | |
689 | ": removing old device %s from %s\n", | |
06c7f68e | 690 | d->sys_name+4, array_name); |
8382f19b NB |
691 | } |
692 | } | |
693 | ||
2bf740b3 N |
694 | static int count_active(struct supertype *st, struct mdinfo *sra, |
695 | int mdfd, char **availp, | |
8382f19b NB |
696 | struct mdinfo *bestinfo) |
697 | { | |
698 | /* count how many devices in sra think they are active */ | |
06c7f68e | 699 | struct mdinfo *d; |
8382f19b NB |
700 | int cnt = 0, cnt1 = 0; |
701 | __u64 max_events = 0; | |
8382f19b NB |
702 | char *avail = NULL; |
703 | ||
b526e52d DW |
704 | if (!sra) |
705 | return 0; | |
706 | ||
8382f19b NB |
707 | for (d = sra->devs ; d ; d = d->next) { |
708 | char dn[30]; | |
709 | int dfd; | |
8382f19b NB |
710 | int ok; |
711 | struct mdinfo info; | |
712 | ||
06c7f68e | 713 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
714 | dfd = dev_open(dn, O_RDONLY); |
715 | if (dfd < 0) | |
716 | continue; | |
3da92f27 | 717 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
718 | close(dfd); |
719 | if (ok != 0) | |
720 | continue; | |
a5d85af7 | 721 | st->ss->getinfo_super(st, &info, NULL); |
43aaf431 DL |
722 | if (!avail) { |
723 | avail = malloc(info.array.raid_disks); | |
724 | if (!avail) { | |
725 | fprintf(stderr, Name ": out of memory.\n"); | |
726 | exit(1); | |
727 | } | |
728 | memset(avail, 0, info.array.raid_disks); | |
729 | *availp = avail; | |
730 | } | |
731 | ||
8382f19b NB |
732 | if (info.disk.state & (1<<MD_DISK_SYNC)) |
733 | { | |
8382f19b NB |
734 | if (cnt == 0) { |
735 | cnt++; | |
736 | max_events = info.events; | |
737 | avail[info.disk.raid_disk] = 2; | |
a5d85af7 | 738 | st->ss->getinfo_super(st, bestinfo, NULL); |
8382f19b NB |
739 | } else if (info.events == max_events) { |
740 | cnt++; | |
741 | avail[info.disk.raid_disk] = 2; | |
742 | } else if (info.events == max_events-1) { | |
743 | cnt1++; | |
744 | avail[info.disk.raid_disk] = 1; | |
745 | } else if (info.events < max_events - 1) | |
746 | ; | |
747 | else if (info.events == max_events+1) { | |
748 | int i; | |
749 | cnt1 = cnt; | |
750 | cnt = 1; | |
751 | max_events = info.events; | |
752 | for (i=0; i<info.array.raid_disks; i++) | |
753 | if (avail[i]) | |
754 | avail[i]--; | |
755 | avail[info.disk.raid_disk] = 2; | |
a5d85af7 | 756 | st->ss->getinfo_super(st, bestinfo, NULL); |
8382f19b NB |
757 | } else { /* info.events much bigger */ |
758 | cnt = 1; cnt1 = 0; | |
759 | memset(avail, 0, info.disk.raid_disk); | |
760 | max_events = info.events; | |
1d1a9f87 | 761 | avail[info.disk.raid_disk] = 2; |
a5d85af7 | 762 | st->ss->getinfo_super(st, bestinfo, NULL); |
8382f19b NB |
763 | } |
764 | } | |
3da92f27 | 765 | st->ss->free_super(st); |
8382f19b NB |
766 | } |
767 | return cnt + cnt1; | |
768 | } | |
769 | ||
56e8be85 | 770 | static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol, |
d2db3045 | 771 | struct map_ent *target, int bare, |
56e8be85 | 772 | struct supertype *st, int verbose) |
f08605b3 N |
773 | { |
774 | /* This device doesn't have any md metadata | |
d2db3045 N |
775 | * The device policy allows 'spare' and if !bare, it allows spare-same-slot. |
776 | * If 'st' is not set, then we only know that some metadata allows this, | |
777 | * others possibly don't. | |
778 | * So look for a container or array to attach the device to. | |
779 | * Prefer 'target' if that is set and the array is found. | |
780 | * | |
f08605b3 N |
781 | * If st is set, then only arrays of that type are considered |
782 | * Return 0 on success, or some exit code on failure, probably 1. | |
783 | */ | |
784 | int rv = -1; | |
f08605b3 N |
785 | struct stat stb; |
786 | struct map_ent *mp, *map = NULL; | |
787 | struct mdinfo *chosen = NULL; | |
788 | int dfd = *dfdp; | |
789 | ||
f08605b3 N |
790 | if (fstat(dfd, &stb) != 0) |
791 | return 1; | |
f08605b3 | 792 | |
56e8be85 | 793 | /* |
f08605b3 N |
794 | * Now we need to find a suitable array to add this to. |
795 | * We only accept arrays that: | |
796 | * - match 'st' | |
797 | * - are in the same domains as the device | |
798 | * - are of an size for which the device will be useful | |
799 | * and we choose the one that is the most degraded | |
800 | */ | |
801 | ||
802 | if (map_lock(&map)) { | |
803 | fprintf(stderr, Name ": failed to get exclusive lock on " | |
804 | "mapfile\n"); | |
805 | return 1; | |
806 | } | |
807 | for (mp = map ; mp ; mp = mp->next) { | |
808 | struct supertype *st2; | |
809 | struct domainlist *dl = NULL; | |
810 | struct mdinfo *sra; | |
811 | unsigned long long devsize; | |
812 | ||
813 | if (is_subarray(mp->metadata)) | |
814 | continue; | |
815 | if (st) { | |
816 | st2 = st->ss->match_metadata_desc(mp->metadata); | |
817 | if (!st2 || | |
818 | (st->minor_version >= 0 && | |
819 | st->minor_version != st2->minor_version)) { | |
820 | if (verbose > 1) | |
821 | fprintf(stderr, Name ": not adding %s to %s as metadata type doesn't match\n", | |
822 | devname, mp->path); | |
823 | free(st2); | |
824 | continue; | |
825 | } | |
826 | free(st2); | |
827 | } | |
828 | sra = sysfs_read(-1, mp->devnum, | |
829 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE| | |
830 | GET_DEGRADED|GET_COMPONENT|GET_VERSION); | |
831 | if (!sra) { | |
832 | /* Probably a container - no degraded info */ | |
833 | sra = sysfs_read(-1, mp->devnum, | |
834 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE| | |
835 | GET_COMPONENT|GET_VERSION); | |
836 | if (sra) | |
837 | sra->array.failed_disks = 0; | |
838 | } | |
839 | if (!sra) | |
840 | continue; | |
841 | if (st == NULL) { | |
842 | int i; | |
843 | st2 = NULL; | |
844 | for(i=0; !st2 && superlist[i]; i++) | |
845 | st2 = superlist[i]->match_metadata_desc( | |
846 | sra->text_version); | |
6e57f80a N |
847 | if (!st2) { |
848 | if (verbose > 1) | |
849 | fprintf(stderr, Name ": not adding %s to %s" | |
850 | " as metadata not recognised.\n", | |
851 | devname, mp->path); | |
852 | goto next; | |
853 | } | |
625b2507 N |
854 | /* Need to double check the 'act_spare' permissions applies |
855 | * to this metadata. | |
856 | */ | |
857 | if (!policy_action_allows(pol, st2->ss->name, act_spare)) | |
858 | goto next; | |
d2db3045 N |
859 | if (!bare && !policy_action_allows(pol, st2->ss->name, |
860 | act_spare_same_slot)) | |
861 | goto next; | |
f08605b3 N |
862 | } else |
863 | st2 = st; | |
864 | get_dev_size(dfd, NULL, &devsize); | |
865 | if (st2->ss->avail_size(st2, devsize) < sra->component_size) { | |
866 | if (verbose > 1) | |
867 | fprintf(stderr, Name ": not adding %s to %s as it is too small\n", | |
868 | devname, mp->path); | |
869 | goto next; | |
870 | } | |
871 | dl = domain_from_array(sra, st2->ss->name); | |
872 | if (!domain_test(dl, pol, st2->ss->name)) { | |
873 | /* domain test fails */ | |
874 | if (verbose > 1) | |
875 | fprintf(stderr, Name ": not adding %s to %s as it is not in a compatible domain\n", | |
876 | devname, mp->path); | |
877 | ||
878 | goto next; | |
879 | } | |
d2db3045 N |
880 | /* test against target. |
881 | * If 'target' is set and 'bare' is false, we only accept | |
882 | * arrays/containers that match 'target'. | |
883 | * If 'target' is set and 'bare' is true, we prefer the | |
884 | * array which matches 'target'. | |
885 | */ | |
886 | if (target) { | |
887 | if (strcmp(target->metadata, mp->metadata) == 0 && | |
888 | memcmp(target->uuid, mp->uuid, | |
889 | sizeof(target->uuid)) == 0) { | |
890 | /* This is our target!! */ | |
891 | if (chosen) | |
892 | sysfs_free(chosen); | |
893 | chosen = sra; | |
894 | sra = NULL; | |
895 | /* skip to end so we don't check any more */ | |
896 | while (mp->next) | |
897 | mp = mp->next; | |
898 | goto next; | |
899 | } | |
900 | /* not our target */ | |
901 | if (!bare) | |
902 | goto next; | |
903 | } | |
904 | ||
f08605b3 N |
905 | /* all tests passed, OK to add to this array */ |
906 | if (!chosen) { | |
907 | chosen = sra; | |
908 | sra = NULL; | |
909 | } else if (chosen->array.failed_disks < sra->array.failed_disks) { | |
910 | sysfs_free(chosen); | |
911 | chosen = sra; | |
912 | sra = NULL; | |
913 | } | |
914 | next: | |
915 | if (sra) | |
916 | sysfs_free(sra); | |
917 | if (st != st2) | |
918 | free(st2); | |
919 | if (dl) | |
920 | domain_free(dl); | |
921 | } | |
922 | if (chosen) { | |
923 | /* add current device to chosen array as a spare */ | |
924 | int mdfd = open_dev(devname2devnum(chosen->sys_name)); | |
925 | if (mdfd >= 0) { | |
a655e550 | 926 | struct mddev_dev devlist; |
f08605b3 N |
927 | char devname[20]; |
928 | devlist.next = NULL; | |
929 | devlist.used = 0; | |
930 | devlist.re_add = 0; | |
931 | devlist.writemostly = 0; | |
932 | devlist.devname = devname; | |
933 | sprintf(devname, "%d:%d", major(stb.st_rdev), | |
934 | minor(stb.st_rdev)); | |
935 | devlist.disposition = 'a'; | |
936 | close(dfd); | |
937 | *dfdp = -1; | |
938 | rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist, | |
939 | -1, 0); | |
940 | close(mdfd); | |
941 | } | |
942 | if (verbose > 0) { | |
943 | if (rv == 0) | |
944 | fprintf(stderr, Name ": added %s as spare for %s\n", | |
945 | devname, chosen->sys_name); | |
946 | else | |
947 | fprintf(stderr, Name ": failed to add %s as spare for %s\n", | |
948 | devname, chosen->sys_name); | |
949 | } | |
950 | sysfs_free(chosen); | |
951 | } | |
952 | return rv ? 0 : 1; | |
953 | } | |
954 | ||
56e8be85 N |
955 | static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol, |
956 | struct supertype *st, int verbose) | |
957 | { | |
61018da0 N |
958 | /* we know that at least one partition virtual-metadata is |
959 | * allowed to incorporate spares like this device. We need to | |
960 | * find a suitable device to copy partition information from. | |
961 | * | |
962 | * Getting a list of all disk (not partition) devices is | |
963 | * slightly non-trivial. We could look at /sys/block, but | |
964 | * that is theoretically due to be removed. Maybe best to use | |
965 | * /dev/disk/by-path/?* and ignore names ending '-partNN' as | |
966 | * we depend on this directory of 'path' info. But that fails | |
967 | * to find loop devices and probably others. Maybe don't | |
968 | * worry about that, they aren't the real target. | |
969 | * | |
970 | * So: check things in /dev/disk/by-path to see if they are in | |
971 | * a compatible domain, then load the partition table and see | |
972 | * if it is OK for the new device, and choose the largest | |
973 | * partition table that fits. | |
974 | */ | |
975 | DIR *dir; | |
976 | struct dirent *de; | |
977 | char *chosen = NULL; | |
978 | unsigned long long chosen_size; | |
979 | struct supertype *chosen_st = NULL; | |
980 | int fd; | |
981 | ||
982 | dir = opendir("/dev/disk/by-path"); | |
983 | if (!dir) | |
984 | return 1; | |
985 | while ((de = readdir(dir)) != NULL) { | |
986 | char *ep; | |
987 | struct dev_policy *pol2 = NULL; | |
988 | struct domainlist *domlist = NULL; | |
989 | int fd = -1; | |
990 | struct mdinfo info; | |
991 | struct supertype *st2 = NULL; | |
992 | char *devname = NULL; | |
993 | unsigned long long devsectors; | |
994 | ||
995 | if (de->d_ino == 0 || | |
996 | de->d_name[0] == '.' || | |
997 | (de->d_type != DT_LNK && de->d_type != DT_UNKNOWN)) | |
998 | goto next; | |
999 | ||
1000 | ep = de->d_name + strlen(de->d_name); | |
1001 | while (ep > de->d_name && | |
1002 | isdigit(ep[-1])) | |
1003 | ep--; | |
1004 | if (ep > de->d_name + 5 && | |
1005 | strncmp(ep-5, "-part", 5) == 0) | |
1006 | /* This is a partition - skip it */ | |
1007 | goto next; | |
1008 | ||
1009 | pol2 = path_policy(de->d_name, type_disk); | |
1010 | ||
1011 | domain_merge(&domlist, pol2, st ? st->ss->name : NULL); | |
1012 | if (domain_test(domlist, pol, st ? st->ss->name : NULL) == 0) | |
1013 | /* new device is incompatible with this device. */ | |
1014 | goto next; | |
1015 | ||
1016 | domain_free(domlist); | |
1017 | domlist = NULL; | |
1018 | ||
1019 | asprintf(&devname, "/dev/disk/by-path/%s", de->d_name); | |
1020 | fd = open(devname, O_RDONLY); | |
1021 | if (fd < 0) | |
1022 | goto next; | |
1023 | if (get_dev_size(fd, devname, &devsectors) == 0) | |
1024 | goto next; | |
1025 | devsectors >>= 9; | |
1026 | ||
1027 | if (st) | |
1028 | st2 = dup_super(st); | |
1029 | else | |
1030 | st2 = guess_super_type(fd, guess_partitions); | |
1031 | if (st2 == NULL || | |
1032 | st2->ss->load_super(st2, fd, NULL) < 0) | |
1033 | goto next; | |
1034 | ||
1035 | if (!st) { | |
1036 | /* Check domain policy again, this time referring to metadata */ | |
1037 | domain_merge(&domlist, pol2, st2->ss->name); | |
1038 | if (domain_test(domlist, pol, st2->ss->name) == 0) | |
1039 | /* Incompatible devices for this metadata type */ | |
1040 | goto next; | |
625b2507 N |
1041 | if (!policy_action_allows(pol, st2->ss->name, act_spare)) |
1042 | /* Some partition types allow sparing, but not | |
1043 | * this one. | |
1044 | */ | |
1045 | goto next; | |
61018da0 N |
1046 | } |
1047 | ||
a5d85af7 | 1048 | st2->ss->getinfo_super(st2, &info, NULL); |
61018da0 N |
1049 | if (info.component_size > devsectors) |
1050 | /* This partitioning doesn't fit in the device */ | |
1051 | goto next; | |
1052 | ||
1053 | /* This is an acceptable device to copy partition | |
1054 | * metadata from. We could just stop here, but I | |
1055 | * think I want to keep looking incase a larger | |
1056 | * metadata which makes better use of the device can | |
1057 | * be found. | |
1058 | */ | |
1059 | if (chosen == NULL || | |
1060 | chosen_size < info.component_size) { | |
1061 | chosen_size = info.component_size; | |
1062 | free(chosen); | |
1063 | chosen = devname; | |
1064 | devname = NULL; | |
1065 | if (chosen_st) { | |
1066 | chosen_st->ss->free_super(chosen_st); | |
1067 | free(chosen_st); | |
1068 | } | |
1069 | chosen_st = st2; | |
1070 | st2 = NULL; | |
1071 | } | |
1072 | ||
1073 | next: | |
1074 | free(devname); | |
1075 | domain_free(domlist); | |
1076 | dev_policy_free(pol2); | |
1077 | if (st2) | |
1078 | st2->ss->free_super(st2); | |
1079 | free(st2); | |
1080 | ||
1081 | if (fd >= 0) | |
1082 | close(fd); | |
1083 | } | |
1084 | ||
1085 | if (!chosen) | |
1086 | return 1; | |
1087 | ||
1088 | /* 'chosen' is the best device we can find. Let's write its | |
1089 | * metadata to devname dfd is read-only so don't use that | |
1090 | */ | |
1091 | fd = open(devname, O_RDWR); | |
1092 | if (fd >= 0) { | |
1093 | chosen_st->ss->store_super(chosen_st, fd); | |
1094 | close(fd); | |
1095 | } | |
1096 | free(chosen); | |
1097 | chosen_st->ss->free_super(chosen_st); | |
1098 | free(chosen_st); | |
1099 | return 0; | |
56e8be85 N |
1100 | } |
1101 | ||
52e965c2 N |
1102 | static int is_bare(int dfd) |
1103 | { | |
1104 | unsigned long long size = 0; | |
1105 | char bufpad[4096 + 4096]; | |
1106 | char *buf = (char*)(((long)bufpad + 4096) & ~4095); | |
1107 | ||
1108 | if (lseek(dfd, 0, SEEK_SET) != 0 || | |
1109 | read(dfd, buf, 4096) != 4096) | |
1110 | return 0; | |
1111 | ||
1112 | if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff') | |
1113 | return 0; | |
1114 | if (memcmp(buf, buf+1, 4095) != 0) | |
1115 | return 0; | |
1116 | ||
1117 | /* OK, first 4K appear blank, try the end. */ | |
1118 | get_dev_size(dfd, NULL, &size); | |
1119 | if (lseek(dfd, size-4096, SEEK_SET) < 0 || | |
1120 | read(dfd, buf, 4096) != 4096) | |
1121 | return 0; | |
1122 | ||
1123 | if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff') | |
1124 | return 0; | |
1125 | if (memcmp(buf, buf+1, 4095) != 0) | |
1126 | return 0; | |
1127 | ||
1128 | return 1; | |
1129 | } | |
56e8be85 N |
1130 | |
1131 | /* adding a spare to a regular array is quite different from adding one to | |
1132 | * a set-of-partitions virtual array. | |
1133 | * This function determines which is worth trying and tries as appropriate. | |
1134 | * Arrays are given priority over partitions. | |
1135 | */ | |
1136 | static int try_spare(char *devname, int *dfdp, struct dev_policy *pol, | |
d2db3045 | 1137 | struct map_ent *target, |
56e8be85 N |
1138 | struct supertype *st, int verbose) |
1139 | { | |
1140 | int i; | |
1141 | int rv; | |
1142 | int arrays_ok = 0; | |
1143 | int partitions_ok = 0; | |
56e8be85 | 1144 | int dfd = *dfdp; |
d2db3045 | 1145 | int bare; |
56e8be85 | 1146 | |
d2db3045 | 1147 | /* Can only add a spare if device has at least one domain */ |
56e8be85 N |
1148 | if (pol_find(pol, pol_domain) == NULL) |
1149 | return 1; | |
1150 | /* And only if some action allows spares */ | |
1151 | if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare)) | |
1152 | return 1; | |
1153 | ||
d2db3045 N |
1154 | /* Now check if the device is bare. |
1155 | * bare devices can always be added as a spare | |
1156 | * non-bare devices can only be added if spare-same-slot is permitted, | |
1157 | * and this device is replacing a previous device - in which case 'target' | |
1158 | * will be set. | |
56e8be85 | 1159 | */ |
52e965c2 | 1160 | if (!is_bare(dfd)) { |
d2db3045 N |
1161 | /* Must have a target and allow same_slot */ |
1162 | /* Later - may allow force_spare without target */ | |
1163 | if (!target || | |
1164 | !policy_action_allows(pol, st?st->ss->name:NULL, | |
1165 | act_spare_same_slot)) { | |
1166 | if (verbose > 1) | |
1167 | fprintf(stderr, Name ": %s is not bare, so not " | |
1168 | "considering as a spare\n", | |
1169 | devname); | |
1170 | return 1; | |
1171 | } | |
1172 | bare = 0; | |
1173 | } else | |
1174 | bare = 1; | |
56e8be85 | 1175 | |
d2db3045 N |
1176 | /* It might be OK to add this device to an array - need to see |
1177 | * what arrays might be candidates. | |
56e8be85 N |
1178 | */ |
1179 | if (st) { | |
1180 | /* just try try 'array' or 'partition' based on this metadata */ | |
1181 | if (st->ss->add_to_super) | |
d2db3045 | 1182 | return array_try_spare(devname, dfdp, pol, target, bare, |
56e8be85 N |
1183 | st, verbose); |
1184 | else | |
1185 | return partition_try_spare(devname, dfdp, pol, | |
1186 | st, verbose); | |
1187 | } | |
d2db3045 N |
1188 | /* No metadata was specified or found so options are open. |
1189 | * Check for whether any array metadata, or any partition metadata | |
1190 | * might allow adding the spare. This check is just help to avoid | |
1191 | * a more costly scan of all arrays when we can be sure that will | |
1192 | * fail. | |
1193 | */ | |
56e8be85 N |
1194 | for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) { |
1195 | if (superlist[i]->add_to_super && !arrays_ok && | |
1196 | policy_action_allows(pol, superlist[i]->name, act_spare)) | |
1197 | arrays_ok = 1; | |
1198 | if (superlist[i]->add_to_super == NULL && !partitions_ok && | |
1199 | policy_action_allows(pol, superlist[i]->name, act_spare)) | |
1200 | partitions_ok = 1; | |
1201 | } | |
aaccda44 | 1202 | rv = 1; |
56e8be85 | 1203 | if (arrays_ok) |
d2db3045 N |
1204 | rv = array_try_spare(devname, dfdp, pol, target, bare, |
1205 | st, verbose); | |
aaccda44 | 1206 | if (rv != 0 && partitions_ok) |
56e8be85 N |
1207 | rv = partition_try_spare(devname, dfdp, pol, st, verbose); |
1208 | return rv; | |
1209 | } | |
1210 | ||
8382f19b NB |
1211 | int IncrementalScan(int verbose) |
1212 | { | |
1213 | /* look at every device listed in the 'map' file. | |
1214 | * If one is found that is not running then: | |
1215 | * look in mdadm.conf for bitmap file. | |
1216 | * if one exists, but array has none, add it. | |
1217 | * try to start array in auto-readonly mode | |
1218 | */ | |
1219 | struct map_ent *mapl = NULL; | |
1220 | struct map_ent *me; | |
fa56eddb | 1221 | struct mddev_ident *devs, *mddev; |
8382f19b NB |
1222 | int rv = 0; |
1223 | ||
1224 | map_read(&mapl); | |
1225 | devs = conf_get_ident(NULL); | |
1226 | ||
1227 | for (me = mapl ; me ; me = me->next) { | |
8382f19b NB |
1228 | mdu_array_info_t array; |
1229 | mdu_bitmap_file_t bmf; | |
7e0f6979 | 1230 | struct mdinfo *sra; |
339c2d6c | 1231 | int mdfd = open_dev(me->devnum); |
215bb3f7 | 1232 | |
8382f19b NB |
1233 | if (mdfd < 0) |
1234 | continue; | |
1235 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
1236 | errno != ENODEV) { | |
1237 | close(mdfd); | |
1238 | continue; | |
1239 | } | |
1240 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
1241 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
339c2d6c | 1242 | if (mddev->devname && me->path |
2400e6eb | 1243 | && devname_matches(mddev->devname, me->path)) |
8382f19b NB |
1244 | break; |
1245 | if (mddev && mddev->bitmap_file) { | |
1246 | /* | |
1247 | * Note: early kernels will wrongly fail this, so it | |
1248 | * is a hint only | |
1249 | */ | |
1250 | int added = -1; | |
1251 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
1252 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
1253 | if (bmfd >= 0) { | |
1254 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
1255 | bmfd); | |
1256 | close(bmfd); | |
1257 | } | |
1258 | } | |
1259 | if (verbose >= 0) { | |
1260 | if (added == 0) | |
1261 | fprintf(stderr, Name | |
1262 | ": Added bitmap %s to %s\n", | |
1263 | mddev->bitmap_file, me->path); | |
1264 | else if (errno != EEXIST) | |
1265 | fprintf(stderr, Name | |
1266 | ": Failed to add bitmap to %s: %s\n", | |
1267 | me->path, strerror(errno)); | |
1268 | } | |
1269 | } | |
1270 | sra = sysfs_read(mdfd, 0, 0); | |
1271 | if (sra) { | |
1272 | if (sysfs_set_str(sra, NULL, | |
1273 | "array_state", "read-auto") == 0) { | |
1274 | if (verbose >= 0) | |
1275 | fprintf(stderr, Name | |
1276 | ": started array %s\n", | |
339c2d6c | 1277 | me->path ?: devnum2devname(me->devnum)); |
8382f19b NB |
1278 | } else { |
1279 | fprintf(stderr, Name | |
1280 | ": failed to start array %s: %s\n", | |
339c2d6c N |
1281 | me->path ?: devnum2devname(me->devnum), |
1282 | strerror(errno)); | |
8382f19b NB |
1283 | rv = 1; |
1284 | } | |
1285 | } | |
1286 | } | |
1287 | return rv; | |
1288 | } | |
598f0d58 | 1289 | |
1771a6e2 N |
1290 | static char *container2devname(char *devname) |
1291 | { | |
1292 | char *mdname = NULL; | |
1293 | ||
1294 | if (devname[0] == '/') { | |
1295 | int fd = open(devname, O_RDONLY); | |
1296 | if (fd >= 0) { | |
1297 | mdname = devnum2devname(fd2devnum(fd)); | |
1298 | close(fd); | |
1299 | } | |
1300 | } else { | |
1301 | int uuid[4]; | |
1302 | struct map_ent *mp, *map = NULL; | |
1303 | ||
1304 | if (!parse_uuid(devname, uuid)) | |
1305 | return mdname; | |
1306 | mp = map_by_uuid(&map, uuid); | |
1307 | if (mp) | |
1308 | mdname = devnum2devname(mp->devnum); | |
1309 | map_free(map); | |
1310 | } | |
1311 | ||
1312 | return mdname; | |
1313 | } | |
1314 | ||
47c74f3f N |
1315 | static int Incremental_container(struct supertype *st, char *devname, |
1316 | char *homehost, int verbose, | |
1317 | int runstop, int autof) | |
598f0d58 NB |
1318 | { |
1319 | /* Collect the contents of this container and for each | |
1320 | * array, choose a device name and assemble the array. | |
1321 | */ | |
1322 | ||
47c74f3f | 1323 | struct mdinfo *list; |
598f0d58 | 1324 | struct mdinfo *ra; |
ad5bc697 | 1325 | struct map_ent *map = NULL; |
47c74f3f N |
1326 | struct mdinfo info; |
1327 | int trustworthy; | |
fa56eddb | 1328 | struct mddev_ident *match; |
47c74f3f N |
1329 | int rv = 0; |
1330 | ||
1331 | memset(&info, 0, sizeof(info)); | |
1332 | st->ss->getinfo_super(st, &info, NULL); | |
1333 | ||
1334 | if ((runstop > 0 && info.container_enough >= 0) || | |
1335 | info.container_enough > 0) | |
1336 | /* pass */; | |
1337 | else { | |
1338 | if (verbose) | |
1339 | fprintf(stderr, Name ": not enough devices to start the container\n"); | |
1340 | return 0; | |
1341 | } | |
1342 | ||
1343 | match = search_mdstat(st, &info, devname, verbose, &rv); | |
1344 | if (match == NULL && rv == 2) | |
1345 | return rv; | |
1346 | ||
1347 | /* Need to compute 'trustworthy' */ | |
1348 | if (match) | |
1349 | trustworthy = LOCAL; | |
1350 | else if (st->ss->match_home(st, homehost) == 1) | |
1351 | trustworthy = LOCAL; | |
1352 | else if (st->ss->match_home(st, "any") == 1) | |
1353 | trustworthy = LOCAL; | |
1354 | else | |
1355 | trustworthy = FOREIGN; | |
ad5bc697 | 1356 | |
47c74f3f | 1357 | list = st->ss->container_content(st, NULL); |
93c861ee DL |
1358 | if (map_lock(&map)) |
1359 | fprintf(stderr, Name ": failed to get exclusive lock on " | |
1360 | "mapfile\n"); | |
598f0d58 NB |
1361 | |
1362 | for (ra = list ; ra ; ra = ra->next) { | |
598f0d58 NB |
1363 | int mdfd; |
1364 | char chosen_name[1024]; | |
ad5bc697 | 1365 | struct map_ent *mp; |
fa56eddb | 1366 | struct mddev_ident *match = NULL; |
598f0d58 | 1367 | |
c5afc314 | 1368 | mp = map_by_uuid(&map, ra->uuid); |
d7288ddc | 1369 | |
30926600 N |
1370 | if (mp) { |
1371 | mdfd = open_dev(mp->devnum); | |
339c2d6c N |
1372 | if (mp->path) |
1373 | strcpy(chosen_name, mp->path); | |
1374 | else | |
1375 | strcpy(chosen_name, devnum2devname(mp->devnum)); | |
30926600 | 1376 | } else { |
dbb44303 | 1377 | |
112cace6 | 1378 | /* Check in mdadm.conf for container == devname and |
dbb44303 N |
1379 | * member == ra->text_version after second slash. |
1380 | */ | |
1381 | char *sub = strchr(ra->text_version+1, '/'); | |
fa56eddb | 1382 | struct mddev_ident *array_list; |
dbb44303 N |
1383 | if (sub) { |
1384 | sub++; | |
1385 | array_list = conf_get_ident(NULL); | |
1386 | } else | |
1387 | array_list = NULL; | |
1388 | for(; array_list ; array_list = array_list->next) { | |
dbb44303 N |
1389 | char *dn; |
1390 | if (array_list->member == NULL || | |
1391 | array_list->container == NULL) | |
1392 | continue; | |
1393 | if (strcmp(array_list->member, sub) != 0) | |
1394 | continue; | |
71d60c48 DW |
1395 | if (array_list->uuid_set && |
1396 | !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid)) | |
1397 | continue; | |
1771a6e2 N |
1398 | dn = container2devname(array_list->container); |
1399 | if (dn == NULL) | |
dbb44303 | 1400 | continue; |
dbb44303 N |
1401 | if (strncmp(dn, ra->text_version+1, |
1402 | strlen(dn)) != 0 || | |
1403 | ra->text_version[strlen(dn)+1] != '/') { | |
1404 | free(dn); | |
1405 | continue; | |
1406 | } | |
1407 | free(dn); | |
1408 | /* we have a match */ | |
1409 | match = array_list; | |
71d60c48 DW |
1410 | if (verbose>0) |
1411 | fprintf(stderr, Name ": match found for member %s\n", | |
1412 | array_list->member); | |
dbb44303 N |
1413 | break; |
1414 | } | |
dbb44303 | 1415 | |
112cace6 N |
1416 | if (match && match->devname && |
1417 | strcasecmp(match->devname, "<ignore>") == 0) { | |
1418 | if (verbose > 0) | |
1419 | fprintf(stderr, Name ": array %s/%s is " | |
1420 | "explicitly ignored by mdadm.conf\n", | |
1421 | match->container, match->member); | |
1422 | return 2; | |
1423 | } | |
05833051 N |
1424 | if (match) |
1425 | trustworthy = LOCAL; | |
112cace6 | 1426 | |
30926600 N |
1427 | mdfd = create_mddev(match ? match->devname : NULL, |
1428 | ra->name, | |
1429 | autof, | |
1430 | trustworthy, | |
1431 | chosen_name); | |
1432 | } | |
598f0d58 NB |
1433 | |
1434 | if (mdfd < 0) { | |
1435 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
1436 | chosen_name, strerror(errno)); | |
1437 | return 2; | |
1438 | } | |
1439 | ||
03b7f6c6 N |
1440 | assemble_container_content(st, mdfd, ra, runstop, |
1441 | chosen_name, verbose); | |
598f0d58 | 1442 | } |
ad5bc697 | 1443 | map_unlock(&map); |
598f0d58 NB |
1444 | return 0; |
1445 | } | |
29ba4804 N |
1446 | |
1447 | /* | |
1448 | * IncrementalRemove - Attempt to see if the passed in device belongs to any | |
1449 | * raid arrays, and if so first fail (if needed) and then remove the device. | |
1450 | * | |
1451 | * @devname - The device we want to remove | |
950bc344 | 1452 | * @id_path - name as found in /dev/disk/by-path for this device |
29ba4804 N |
1453 | * |
1454 | * Note: the device name must be a kernel name like "sda", so | |
1455 | * that we can find it in /proc/mdstat | |
1456 | */ | |
950bc344 | 1457 | int IncrementalRemove(char *devname, char *id_path, int verbose) |
29ba4804 N |
1458 | { |
1459 | int mdfd; | |
aae3cdc3 | 1460 | int rv; |
29ba4804 | 1461 | struct mdstat_ent *ent; |
a655e550 | 1462 | struct mddev_dev devlist; |
29ba4804 | 1463 | |
950bc344 PC |
1464 | if (!id_path) |
1465 | dprintf(Name ": incremental removal without --path <id_path> " | |
1466 | "lacks the possibility to re-add new device in this " | |
1467 | "port\n"); | |
1468 | ||
29ba4804 N |
1469 | if (strchr(devname, '/')) { |
1470 | fprintf(stderr, Name ": incremental removal requires a " | |
1471 | "kernel device name, not a file: %s\n", devname); | |
1472 | return 1; | |
1473 | } | |
1474 | ent = mdstat_by_component(devname); | |
1475 | if (!ent) { | |
1476 | fprintf(stderr, Name ": %s does not appear to be a component " | |
1477 | "of any array\n", devname); | |
1478 | return 1; | |
1479 | } | |
1480 | mdfd = open_dev(ent->devnum); | |
1481 | if (mdfd < 0) { | |
1482 | fprintf(stderr, Name ": Cannot open array %s!!\n", ent->dev); | |
08387a04 | 1483 | free_mdstat(ent); |
29ba4804 N |
1484 | return 1; |
1485 | } | |
403410eb PC |
1486 | |
1487 | if (id_path) { | |
1488 | struct map_ent *map = NULL, *me; | |
1489 | me = map_by_devnum(&map, ent->devnum); | |
1490 | if (me) | |
1491 | policy_save_path(id_path, me); | |
1492 | map_free(map); | |
1493 | } | |
1494 | ||
29ba4804 N |
1495 | memset(&devlist, 0, sizeof(devlist)); |
1496 | devlist.devname = devname; | |
1497 | devlist.disposition = 'f'; | |
08387a04 N |
1498 | /* for a container, we must fail each member array */ |
1499 | if (ent->metadata_version && | |
1500 | strncmp(ent->metadata_version, "external:", 9) == 0) { | |
1501 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
1502 | struct mdstat_ent *memb; | |
1503 | for (memb = mdstat ; memb ; memb = memb->next) | |
1504 | if (is_container_member(memb, ent->dev)) { | |
1505 | int subfd = open_dev(memb->devnum); | |
1506 | if (subfd >= 0) { | |
1507 | Manage_subdevs(memb->dev, subfd, | |
1508 | &devlist, verbose, 0); | |
1509 | close(subfd); | |
1510 | } | |
1511 | } | |
1512 | free_mdstat(mdstat); | |
1513 | } else | |
1514 | Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0); | |
29ba4804 | 1515 | devlist.disposition = 'r'; |
aae3cdc3 PC |
1516 | rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0); |
1517 | close(mdfd); | |
08387a04 | 1518 | free_mdstat(ent); |
aae3cdc3 | 1519 | return rv; |
29ba4804 | 1520 | } |