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