]>
Commit | Line | Data |
---|---|---|
8382f19b NB |
1 | /* |
2 | * Incremental.c - support --incremental. Part of: | |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
4 | * | |
e736b623 | 5 | * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de> |
8382f19b NB |
6 | * |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | * Author: Neil Brown | |
23 | * Email: <neilb@suse.de> | |
24 | * Paper: Neil Brown | |
25 | * Novell Inc | |
26 | * GPO Box Q1283 | |
27 | * QVB Post Office, NSW 1230 | |
28 | * Australia | |
29 | */ | |
30 | ||
31 | #include "mdadm.h" | |
32 | ||
33 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
34 | struct mdinfo *info); | |
7e0f6979 | 35 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
36 | int number, __u64 events, int verbose, |
37 | char *array_name); | |
f08605b3 N |
38 | static int try_spare(char *devname, int *dfdp, struct dev_policy *pol, |
39 | struct supertype *st, int verbose); | |
8382f19b NB |
40 | |
41 | int Incremental(char *devname, int verbose, int runstop, | |
0ac91628 N |
42 | struct supertype *st, char *homehost, int require_homehost, |
43 | int autof) | |
8382f19b NB |
44 | { |
45 | /* Add this device to an array, creating the array if necessary | |
598f0d58 | 46 | * and starting the array if sensible or - if runstop>0 - if possible. |
8382f19b NB |
47 | * |
48 | * This has several steps: | |
49 | * | |
50 | * 1/ Check if device is permitted by mdadm.conf, reject if not. | |
51 | * 2/ Find metadata, reject if none appropriate (check | |
52 | * version/name from args) | |
53 | * 3/ Check if there is a match in mdadm.conf | |
215bb3f7 N |
54 | * 3a/ if not, check for homehost match. If no match, assemble as |
55 | * a 'foreign' array. | |
8382f19b NB |
56 | * 4/ Determine device number. |
57 | * - If in mdadm.conf with std name, use that | |
58 | * - UUID in /var/run/mdadm.map use that | |
59 | * - If name is suggestive, use that. unless in use with different uuid. | |
60 | * - Choose a free, high number. | |
61 | * - Use a partitioned device unless strong suggestion not to. | |
62 | * e.g. auto=md | |
352452c3 | 63 | * Don't choose partitioned for containers. |
8382f19b NB |
64 | * 5/ Find out if array already exists |
65 | * 5a/ if it does not | |
66 | * - choose a name, from mdadm.conf or 'name' field in array. | |
67 | * - create the array | |
68 | * - add the device | |
69 | * 5b/ if it does | |
70 | * - check one drive in array to make sure metadata is a reasonably | |
71 | * close match. Reject if not (e.g. different type) | |
72 | * - add the device | |
73 | * 6/ Make sure /var/run/mdadm.map contains this array. | |
74 | * 7/ Is there enough devices to possibly start the array? | |
352452c3 | 75 | * For a container, this means running Incremental_container. |
8382f19b NB |
76 | * 7a/ if not, finish with success. |
77 | * 7b/ if yes, | |
78 | * - read all metadata and arrange devices like -A does | |
79 | * - if number of OK devices match expected, or -R and there are enough, | |
80 | * start the array (auto-readonly). | |
81 | */ | |
82 | struct stat stb; | |
7e83544b | 83 | struct mdinfo info, dinfo; |
8382f19b NB |
84 | struct mddev_ident_s *array_list, *match; |
85 | char chosen_name[1024]; | |
15d4a7e4 | 86 | int rv = 1; |
8382f19b | 87 | struct map_ent *mp, *map = NULL; |
15d4a7e4 | 88 | int dfd = -1, mdfd = -1; |
8382f19b NB |
89 | char *avail; |
90 | int active_disks; | |
215bb3f7 | 91 | int trustworthy = FOREIGN; |
d7288ddc | 92 | char *name_to_use; |
ad5bc697 | 93 | mdu_array_info_t ainf; |
7e83544b | 94 | struct dev_policy *policy = NULL; |
8382f19b NB |
95 | |
96 | struct createinfo *ci = conf_get_create_info(); | |
97 | ||
8382f19b | 98 | |
352452c3 | 99 | /* 1/ Check if device is permitted by mdadm.conf */ |
8382f19b NB |
100 | |
101 | if (!conf_test_dev(devname)) { | |
102 | if (verbose >= 0) | |
103 | fprintf(stderr, Name | |
104 | ": %s not permitted by mdadm.conf.\n", | |
105 | devname); | |
15d4a7e4 | 106 | goto out; |
8382f19b NB |
107 | } |
108 | ||
109 | /* 2/ Find metadata, reject if none appropriate (check | |
110 | * version/name from args) */ | |
111 | ||
112 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
113 | if (dfd < 0) { | |
114 | if (verbose >= 0) | |
115 | fprintf(stderr, Name ": cannot open %s: %s.\n", | |
116 | devname, strerror(errno)); | |
15d4a7e4 | 117 | goto out; |
8382f19b NB |
118 | } |
119 | if (fstat(dfd, &stb) < 0) { | |
120 | if (verbose >= 0) | |
121 | fprintf(stderr, Name ": fstat failed for %s: %s.\n", | |
122 | devname, strerror(errno)); | |
15d4a7e4 | 123 | goto out; |
8382f19b NB |
124 | } |
125 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
126 | if (verbose >= 0) | |
127 | fprintf(stderr, Name ": %s is not a block device.\n", | |
128 | devname); | |
15d4a7e4 | 129 | goto out; |
8382f19b NB |
130 | } |
131 | ||
7e83544b N |
132 | dinfo.disk.major = major(stb.st_rdev); |
133 | dinfo.disk.minor = minor(stb.st_rdev); | |
134 | ||
135 | policy = disk_policy(&dinfo); | |
136 | ||
8382f19b NB |
137 | if (st == NULL && (st = guess_super(dfd)) == NULL) { |
138 | if (verbose >= 0) | |
139 | fprintf(stderr, Name | |
140 | ": no recognisable superblock on %s.\n", | |
141 | devname); | |
f08605b3 | 142 | rv = try_spare(devname, &dfd, policy, st, verbose); |
15d4a7e4 | 143 | goto out; |
8382f19b | 144 | } |
0f22b998 N |
145 | if (st->ss->compare_super == NULL || |
146 | st->ss->load_super(st, dfd, NULL)) { | |
8382f19b NB |
147 | if (verbose >= 0) |
148 | fprintf(stderr, Name ": no RAID superblock on %s.\n", | |
149 | devname); | |
f08605b3 | 150 | rv = try_spare(devname, &dfd, policy, st, verbose); |
0f22b998 | 151 | free(st); |
15d4a7e4 | 152 | goto out; |
8382f19b | 153 | } |
15d4a7e4 | 154 | close (dfd); dfd = -1; |
8382f19b | 155 | |
f35f2525 | 156 | memset(&info, 0, sizeof(info)); |
598f0d58 | 157 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
158 | /* 3/ Check if there is a match in mdadm.conf */ |
159 | ||
160 | array_list = conf_get_ident(NULL); | |
161 | match = NULL; | |
162 | for (; array_list; array_list = array_list->next) { | |
163 | if (array_list->uuid_set && | |
164 | same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid) | |
165 | == 0) { | |
fe056d1f | 166 | if (verbose >= 2 && array_list->devname) |
8382f19b NB |
167 | fprintf(stderr, Name |
168 | ": UUID differs from %s.\n", | |
169 | array_list->devname); | |
170 | continue; | |
171 | } | |
172 | if (array_list->name[0] && | |
173 | strcasecmp(array_list->name, info.name) != 0) { | |
fe056d1f | 174 | if (verbose >= 2 && array_list->devname) |
8382f19b NB |
175 | fprintf(stderr, Name |
176 | ": Name differs from %s.\n", | |
177 | array_list->devname); | |
178 | continue; | |
179 | } | |
180 | if (array_list->devices && | |
181 | !match_oneof(array_list->devices, devname)) { | |
fe056d1f | 182 | if (verbose >= 2 && array_list->devname) |
8382f19b NB |
183 | fprintf(stderr, Name |
184 | ": Not a listed device for %s.\n", | |
185 | array_list->devname); | |
186 | continue; | |
187 | } | |
188 | if (array_list->super_minor != UnSet && | |
189 | array_list->super_minor != info.array.md_minor) { | |
fe056d1f | 190 | if (verbose >= 2 && array_list->devname) |
8382f19b NB |
191 | fprintf(stderr, Name |
192 | ": Different super-minor to %s.\n", | |
193 | array_list->devname); | |
194 | continue; | |
195 | } | |
196 | if (!array_list->uuid_set && | |
197 | !array_list->name[0] && | |
198 | !array_list->devices && | |
199 | array_list->super_minor == UnSet) { | |
fe056d1f | 200 | if (verbose >= 2 && array_list->devname) |
8382f19b NB |
201 | fprintf(stderr, Name |
202 | ": %s doesn't have any identifying information.\n", | |
203 | array_list->devname); | |
204 | continue; | |
205 | } | |
206 | /* FIXME, should I check raid_disks and level too?? */ | |
207 | ||
208 | if (match) { | |
fe056d1f N |
209 | if (verbose >= 0) { |
210 | if (match->devname && array_list->devname) | |
211 | fprintf(stderr, Name | |
8382f19b | 212 | ": we match both %s and %s - cannot decide which to use.\n", |
fe056d1f N |
213 | match->devname, array_list->devname); |
214 | else | |
215 | fprintf(stderr, Name | |
216 | ": multiple lines in mdadm.conf match\n"); | |
217 | } | |
15d4a7e4 N |
218 | rv = 2; |
219 | goto out; | |
8382f19b NB |
220 | } |
221 | match = array_list; | |
222 | } | |
223 | ||
112cace6 N |
224 | if (match && match->devname |
225 | && strcasecmp(match->devname, "<ignore>") == 0) { | |
226 | if (verbose >= 0) | |
227 | fprintf(stderr, Name ": array containing %s is explicitly" | |
228 | " ignored by mdadm.conf\n", | |
229 | devname); | |
15d4a7e4 | 230 | goto out; |
112cace6 N |
231 | } |
232 | ||
7b403fef N |
233 | /* 3a/ if not, check for homehost match. If no match, continue |
234 | * but don't trust the 'name' in the array. Thus a 'random' minor | |
235 | * number will be assigned, and the device name will be based | |
236 | * on that. */ | |
215bb3f7 N |
237 | if (match) |
238 | trustworthy = LOCAL; | |
d1d3482b N |
239 | else if (st->ss->match_home(st, homehost) == 1) |
240 | trustworthy = LOCAL; | |
241 | else if (st->ss->match_home(st, "any") == 1) | |
242 | trustworthy = LOCAL_ANY; | |
215bb3f7 | 243 | else |
d1d3482b N |
244 | trustworthy = FOREIGN; |
245 | ||
246 | ||
247 | if (!match && !conf_test_metadata(st->ss->name, | |
248 | (trustworthy == LOCAL))) { | |
249 | if (verbose >= 1) | |
250 | fprintf(stderr, Name | |
251 | ": %s has metadata type %s for which " | |
252 | "auto-assembly is disabled\n", | |
253 | devname, st->ss->name); | |
15d4a7e4 | 254 | goto out; |
d1d3482b N |
255 | } |
256 | if (trustworthy == LOCAL_ANY) | |
ecb02e31 | 257 | trustworthy = LOCAL; |
e12e1d1d | 258 | |
4ef2f11e N |
259 | /* There are three possible sources for 'autof': command line, |
260 | * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf. | |
350ac35d N |
261 | * ARRAY takes precedence, then command line, then |
262 | * CREATE. | |
4ef2f11e | 263 | */ |
350ac35d | 264 | if (match && match->autof) |
4ef2f11e N |
265 | autof = match->autof; |
266 | if (autof == 0) | |
267 | autof = ci->autof; | |
268 | ||
215bb3f7 | 269 | if (st->ss->container_content && st->loaded_container) { |
97b4d0e9 DW |
270 | if ((runstop > 0 && info.container_enough >= 0) || |
271 | info.container_enough > 0) | |
272 | /* pass */; | |
273 | else { | |
274 | if (verbose) | |
275 | fprintf(stderr, Name ": not enough devices to start the container\n"); | |
15d4a7e4 N |
276 | rv = 0; |
277 | goto out; | |
97b4d0e9 DW |
278 | } |
279 | ||
215bb3f7 N |
280 | /* This is a pre-built container array, so we do something |
281 | * rather different. | |
282 | */ | |
15d4a7e4 | 283 | rv = Incremental_container(st, devname, verbose, runstop, |
215bb3f7 | 284 | autof, trustworthy); |
15d4a7e4 | 285 | goto out; |
8382f19b | 286 | } |
ecb02e31 | 287 | |
7cdc0872 | 288 | name_to_use = info.name; |
0ac91628 | 289 | if (name_to_use[0] == 0 && |
ecb02e31 N |
290 | info.array.level == LEVEL_CONTAINER && |
291 | trustworthy == LOCAL) { | |
292 | name_to_use = info.text_version; | |
293 | trustworthy = METADATA; | |
294 | } | |
0ac91628 N |
295 | if (name_to_use[0] && trustworthy != LOCAL && |
296 | ! require_homehost && | |
297 | conf_name_is_free(name_to_use)) | |
298 | trustworthy = LOCAL; | |
ecb02e31 | 299 | |
7cdc0872 N |
300 | /* strip "hostname:" prefix from name if we have decided |
301 | * to treat it as LOCAL | |
302 | */ | |
303 | if (trustworthy == LOCAL && strchr(name_to_use, ':') != NULL) | |
304 | name_to_use = strchr(name_to_use, ':')+1; | |
305 | ||
ad5bc697 | 306 | /* 4/ Check if array exists. |
215bb3f7 | 307 | */ |
93c861ee DL |
308 | if (map_lock(&map)) |
309 | fprintf(stderr, Name ": failed to get exclusive lock on " | |
310 | "mapfile\n"); | |
215bb3f7 | 311 | mp = map_by_uuid(&map, info.uuid); |
339c2d6c N |
312 | if (mp) |
313 | mdfd = open_dev(mp->devnum); | |
314 | else | |
215bb3f7 | 315 | mdfd = -1; |
d7288ddc | 316 | |
8382f19b | 317 | if (mdfd < 0) { |
7e0f6979 | 318 | struct mdinfo *sra; |
8382f19b | 319 | |
215bb3f7 | 320 | /* Couldn't find an existing array, maybe make a new one */ |
ffb3e494 | 321 | mdfd = create_mddev(match ? match->devname : NULL, |
e12e1d1d | 322 | name_to_use, autof, trustworthy, chosen_name); |
215bb3f7 N |
323 | |
324 | if (mdfd < 0) | |
15d4a7e4 | 325 | goto out; |
215bb3f7 N |
326 | |
327 | sysfs_init(&info, mdfd, 0); | |
328 | ||
f35f2525 N |
329 | if (set_array_info(mdfd, st, &info) != 0) { |
330 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", | |
8382f19b | 331 | chosen_name, strerror(errno)); |
15d4a7e4 N |
332 | rv = 2; |
333 | goto out; | |
8382f19b | 334 | } |
7801ac20 | 335 | |
c5afc314 N |
336 | dinfo = info; |
337 | dinfo.disk.major = major(stb.st_rdev); | |
338 | dinfo.disk.minor = minor(stb.st_rdev); | |
339 | if (add_disk(mdfd, st, &info, &dinfo) != 0) { | |
8382f19b NB |
340 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", |
341 | devname, chosen_name, strerror(errno)); | |
342 | ioctl(mdfd, STOP_ARRAY, 0); | |
15d4a7e4 N |
343 | rv = 2; |
344 | goto out; | |
8382f19b | 345 | } |
197e3eb6 | 346 | sra = sysfs_read(mdfd, fd2devnum(mdfd), GET_DEVS); |
06c7f68e | 347 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { |
8382f19b NB |
348 | /* It really should be 'none' - must be old buggy |
349 | * kernel, and mdadm -I may not be able to complete. | |
350 | * So reject it. | |
351 | */ | |
352 | ioctl(mdfd, STOP_ARRAY, NULL); | |
353 | fprintf(stderr, Name | |
354 | ": You have an old buggy kernel which cannot support\n" | |
355 | " --incremental reliably. Aborting.\n"); | |
8382f19b | 356 | sysfs_free(sra); |
15d4a7e4 N |
357 | rv = 2; |
358 | goto out; | |
8382f19b | 359 | } |
c5afc314 | 360 | info.array.working_disks = 1; |
f35f2525 | 361 | sysfs_free(sra); |
ad5bc697 N |
362 | /* 6/ Make sure /var/run/mdadm.map contains this array. */ |
363 | map_update(&map, fd2devnum(mdfd), | |
364 | info.text_version, | |
365 | info.uuid, chosen_name); | |
8382f19b NB |
366 | } else { |
367 | /* 5b/ if it does */ | |
368 | /* - check one drive in array to make sure metadata is a reasonably */ | |
369 | /* close match. Reject if not (e.g. different type) */ | |
370 | /* - add the device */ | |
371 | char dn[20]; | |
372 | int dfd2; | |
8382f19b | 373 | int err; |
7e0f6979 | 374 | struct mdinfo *sra; |
3da92f27 | 375 | struct supertype *st2; |
c5afc314 | 376 | struct mdinfo info2, *d; |
215bb3f7 | 377 | |
339c2d6c N |
378 | if (mp->path) |
379 | strcpy(chosen_name, mp->path); | |
380 | else | |
381 | strcpy(chosen_name, devnum2devname(mp->devnum)); | |
215bb3f7 | 382 | |
ef83fe7c N |
383 | /* It is generally not OK to add non-spare drives to a |
384 | * running array as they are probably missing because | |
385 | * they failed. However if runstop is 1, then the | |
7e83544b N |
386 | * array was possibly started early and our best bet is |
387 | * to add this anyway. | |
388 | * Also if action policy is re-add or better we allow | |
389 | * re-add | |
3a6ec29a | 390 | */ |
ef83fe7c | 391 | if ((info.disk.state & (1<<MD_DISK_SYNC)) != 0 |
7e83544b N |
392 | && ! policy_action_allows(policy, st->ss->name, |
393 | act_re_add) | |
ef83fe7c | 394 | && runstop < 1) { |
1dccfff9 DW |
395 | int active = 0; |
396 | ||
397 | if (st->ss->external) { | |
398 | char *devname = devnum2devname(fd2devnum(mdfd)); | |
399 | ||
400 | active = devname && is_container_active(devname); | |
401 | free(devname); | |
402 | } else if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) | |
403 | active = 1; | |
404 | if (active) { | |
3a6ec29a N |
405 | fprintf(stderr, Name |
406 | ": not adding %s to active array (without --run) %s\n", | |
407 | devname, chosen_name); | |
15d4a7e4 N |
408 | rv = 2; |
409 | goto out; | |
3a6ec29a N |
410 | } |
411 | } | |
197e3eb6 | 412 | sra = sysfs_read(mdfd, fd2devnum(mdfd), (GET_DEVS | GET_STATE)); |
15d4a7e4 N |
413 | if (!sra) { |
414 | rv = 2; | |
415 | goto out; | |
416 | } | |
7c548327 N |
417 | if (sra->devs) { |
418 | sprintf(dn, "%d:%d", sra->devs->disk.major, | |
419 | sra->devs->disk.minor); | |
420 | dfd2 = dev_open(dn, O_RDONLY); | |
421 | st2 = dup_super(st); | |
422 | if (st2->ss->load_super(st2, dfd2, NULL) || | |
423 | st->ss->compare_super(st, st2) != 0) { | |
424 | fprintf(stderr, Name | |
425 | ": metadata mismatch between %s and " | |
426 | "chosen array %s\n", | |
427 | devname, chosen_name); | |
7c548327 | 428 | close(dfd2); |
15d4a7e4 N |
429 | rv = 2; |
430 | goto out; | |
7c548327 | 431 | } |
8382f19b | 432 | close(dfd2); |
7c548327 N |
433 | memset(&info2, 0, sizeof(info2)); |
434 | st2->ss->getinfo_super(st2, &info2); | |
435 | st2->ss->free_super(st2); | |
436 | if (info.array.level != info2.array.level || | |
437 | memcmp(info.uuid, info2.uuid, 16) != 0 || | |
438 | info.array.raid_disks != info2.array.raid_disks) { | |
439 | fprintf(stderr, Name | |
440 | ": unexpected difference between %s and %s.\n", | |
441 | chosen_name, devname); | |
15d4a7e4 N |
442 | rv = 2; |
443 | goto out; | |
7c548327 | 444 | } |
8382f19b | 445 | } |
7801ac20 N |
446 | info2.disk.major = major(stb.st_rdev); |
447 | info2.disk.minor = minor(stb.st_rdev); | |
c5afc314 N |
448 | /* add disk needs to know about containers */ |
449 | if (st->ss->external) | |
450 | sra->array.level = LEVEL_CONTAINER; | |
ac7de9d9 | 451 | err = add_disk(mdfd, st, sra, &info2); |
8382f19b NB |
452 | if (err < 0 && errno == EBUSY) { |
453 | /* could be another device present with the same | |
454 | * disk.number. Find and reject any such | |
455 | */ | |
456 | find_reject(mdfd, st, sra, info.disk.number, | |
457 | info.events, verbose, chosen_name); | |
ac7de9d9 | 458 | err = add_disk(mdfd, st, sra, &info2); |
8382f19b NB |
459 | } |
460 | if (err < 0) { | |
461 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
462 | devname, chosen_name, strerror(errno)); | |
15d4a7e4 N |
463 | rv = 2; |
464 | goto out; | |
8382f19b | 465 | } |
c5afc314 N |
466 | info.array.working_disks = 0; |
467 | for (d = sra->devs; d; d=d->next) | |
468 | info.array.working_disks ++; | |
469 | ||
8382f19b | 470 | } |
8382f19b NB |
471 | |
472 | /* 7/ Is there enough devices to possibly start the array? */ | |
473 | /* 7a/ if not, finish with success. */ | |
352452c3 N |
474 | if (info.array.level == LEVEL_CONTAINER) { |
475 | /* Try to assemble within the container */ | |
ad5bc697 | 476 | map_unlock(&map); |
97590376 | 477 | sysfs_uevent(&info, "change"); |
c5afc314 N |
478 | if (verbose >= 0) |
479 | fprintf(stderr, Name | |
480 | ": container %s now has %d devices\n", | |
481 | chosen_name, info.array.working_disks); | |
a7c6e3fb N |
482 | wait_for(chosen_name, mdfd); |
483 | close(mdfd); | |
03b7f6c6 | 484 | rv = Incremental(chosen_name, verbose, runstop, |
0ac91628 | 485 | NULL, homehost, require_homehost, autof); |
03b7f6c6 N |
486 | if (rv == 1) |
487 | /* Don't fail the whole -I if a subarray didn't | |
488 | * have enough devices to start yet | |
489 | */ | |
490 | rv = 0; | |
491 | return rv; | |
352452c3 | 492 | } |
f8409e54 | 493 | avail = NULL; |
8382f19b NB |
494 | active_disks = count_active(st, mdfd, &avail, &info); |
495 | if (enough(info.array.level, info.array.raid_disks, | |
496 | info.array.layout, info.array.state & 1, | |
3288b419 | 497 | avail, active_disks) == 0) { |
8382f19b NB |
498 | free(avail); |
499 | if (verbose >= 0) | |
500 | fprintf(stderr, Name | |
501 | ": %s attached to %s, not enough to start (%d).\n", | |
502 | devname, chosen_name, active_disks); | |
ad5bc697 | 503 | map_unlock(&map); |
15d4a7e4 N |
504 | rv = 0; |
505 | goto out; | |
8382f19b NB |
506 | } |
507 | free(avail); | |
508 | ||
509 | /* 7b/ if yes, */ | |
510 | /* - if number of OK devices match expected, or -R and there */ | |
511 | /* are enough, */ | |
512 | /* + add any bitmap file */ | |
513 | /* + start the array (auto-readonly). */ | |
8382f19b NB |
514 | |
515 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
516 | if (verbose >= 0) | |
517 | fprintf(stderr, Name | |
518 | ": %s attached to %s which is already active.\n", | |
519 | devname, chosen_name); | |
ad5bc697 | 520 | map_unlock(&map); |
15d4a7e4 N |
521 | rv = 0; |
522 | goto out; | |
8382f19b | 523 | } |
ad5bc697 N |
524 | |
525 | map_unlock(&map); | |
8382f19b | 526 | if (runstop > 0 || active_disks >= info.array.working_disks) { |
7e83544b | 527 | struct mdinfo *sra, *dsk; |
8382f19b NB |
528 | /* Let's try to start it */ |
529 | if (match && match->bitmap_file) { | |
530 | int bmfd = open(match->bitmap_file, O_RDWR); | |
531 | if (bmfd < 0) { | |
532 | fprintf(stderr, Name | |
533 | ": Could not open bitmap file %s.\n", | |
534 | match->bitmap_file); | |
15d4a7e4 | 535 | goto out; |
8382f19b NB |
536 | } |
537 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
538 | close(bmfd); | |
539 | fprintf(stderr, Name | |
540 | ": Failed to set bitmapfile for %s.\n", | |
541 | chosen_name); | |
15d4a7e4 | 542 | goto out; |
8382f19b NB |
543 | } |
544 | close(bmfd); | |
545 | } | |
7e83544b N |
546 | /* GET_* needed so add_disk works below */ |
547 | sra = sysfs_read(mdfd, fd2devnum(mdfd), | |
548 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE); | |
7b403fef | 549 | if ((sra == NULL || active_disks >= info.array.working_disks) |
215bb3f7 | 550 | && trustworthy != FOREIGN) |
8382f19b NB |
551 | rv = ioctl(mdfd, RUN_ARRAY, NULL); |
552 | else | |
553 | rv = sysfs_set_str(sra, NULL, | |
554 | "array_state", "read-auto"); | |
555 | if (rv == 0) { | |
556 | if (verbose >= 0) | |
557 | fprintf(stderr, Name | |
7e83544b | 558 | ": %s attached to %s, which has been started.\n", |
8382f19b NB |
559 | devname, chosen_name); |
560 | rv = 0; | |
a7c6e3fb | 561 | wait_for(chosen_name, mdfd); |
7e83544b N |
562 | /* We just started the array, so some devices |
563 | * might have been evicted from the array | |
564 | * because their event counts were too old. | |
565 | * If the action=re-add policy is in-force for | |
566 | * those devices we should re-add them now. | |
567 | */ | |
568 | for (dsk = sra->devs; dsk ; dsk = dsk->next) { | |
569 | if (disk_action_allows(dsk, st->ss->name, act_re_add) && | |
570 | add_disk(mdfd, st, sra, dsk) == 0) | |
571 | fprintf(stderr, Name | |
572 | ": %s re-added to %s\n", | |
573 | dsk->sys_name, chosen_name); | |
574 | } | |
8382f19b NB |
575 | } else { |
576 | fprintf(stderr, Name | |
577 | ": %s attached to %s, but failed to start: %s.\n", | |
578 | devname, chosen_name, strerror(errno)); | |
579 | rv = 1; | |
580 | } | |
581 | } else { | |
582 | if (verbose >= 0) | |
583 | fprintf(stderr, Name | |
584 | ": %s attached to %s, not enough to start safely.\n", | |
585 | devname, chosen_name); | |
586 | rv = 0; | |
587 | } | |
15d4a7e4 N |
588 | out: |
589 | if (dfd >= 0) | |
590 | close(dfd); | |
591 | if (mdfd >= 0) | |
592 | close(mdfd); | |
7e83544b N |
593 | if (policy) |
594 | dev_policy_free(policy); | |
8382f19b NB |
595 | return rv; |
596 | } | |
597 | ||
7e0f6979 | 598 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
599 | int number, __u64 events, int verbose, |
600 | char *array_name) | |
601 | { | |
3da92f27 | 602 | /* Find a device attached to this array with a disk.number of number |
8382f19b NB |
603 | * and events less than the passed events, and remove the device. |
604 | */ | |
06c7f68e | 605 | struct mdinfo *d; |
8382f19b NB |
606 | mdu_array_info_t ra; |
607 | ||
608 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
609 | return; /* not safe to remove from active arrays | |
610 | * without thinking more */ | |
611 | ||
612 | for (d = sra->devs; d ; d = d->next) { | |
613 | char dn[10]; | |
614 | int dfd; | |
8382f19b | 615 | struct mdinfo info; |
06c7f68e | 616 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
617 | dfd = dev_open(dn, O_RDONLY); |
618 | if (dfd < 0) | |
619 | continue; | |
3da92f27 | 620 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
621 | close(dfd); |
622 | continue; | |
623 | } | |
3da92f27 NB |
624 | st->ss->getinfo_super(st, &info); |
625 | st->ss->free_super(st); | |
8382f19b NB |
626 | close(dfd); |
627 | ||
628 | if (info.disk.number != number || | |
629 | info.events >= events) | |
630 | continue; | |
631 | ||
06c7f68e | 632 | if (d->disk.raid_disk > -1) |
8382f19b NB |
633 | sysfs_set_str(sra, d, "slot", "none"); |
634 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
635 | if (verbose >= 0) | |
636 | fprintf(stderr, Name | |
637 | ": removing old device %s from %s\n", | |
06c7f68e | 638 | d->sys_name+4, array_name); |
8382f19b NB |
639 | } |
640 | } | |
641 | ||
642 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
643 | struct mdinfo *bestinfo) | |
644 | { | |
645 | /* count how many devices in sra think they are active */ | |
06c7f68e | 646 | struct mdinfo *d; |
8382f19b NB |
647 | int cnt = 0, cnt1 = 0; |
648 | __u64 max_events = 0; | |
7e0f6979 | 649 | struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); |
8382f19b NB |
650 | char *avail = NULL; |
651 | ||
b526e52d DW |
652 | if (!sra) |
653 | return 0; | |
654 | ||
8382f19b NB |
655 | for (d = sra->devs ; d ; d = d->next) { |
656 | char dn[30]; | |
657 | int dfd; | |
8382f19b NB |
658 | int ok; |
659 | struct mdinfo info; | |
660 | ||
06c7f68e | 661 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
662 | dfd = dev_open(dn, O_RDONLY); |
663 | if (dfd < 0) | |
664 | continue; | |
3da92f27 | 665 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
666 | close(dfd); |
667 | if (ok != 0) | |
668 | continue; | |
3da92f27 | 669 | st->ss->getinfo_super(st, &info); |
43aaf431 DL |
670 | if (!avail) { |
671 | avail = malloc(info.array.raid_disks); | |
672 | if (!avail) { | |
673 | fprintf(stderr, Name ": out of memory.\n"); | |
674 | exit(1); | |
675 | } | |
676 | memset(avail, 0, info.array.raid_disks); | |
677 | *availp = avail; | |
678 | } | |
679 | ||
8382f19b NB |
680 | if (info.disk.state & (1<<MD_DISK_SYNC)) |
681 | { | |
8382f19b NB |
682 | if (cnt == 0) { |
683 | cnt++; | |
684 | max_events = info.events; | |
685 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 686 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
687 | } else if (info.events == max_events) { |
688 | cnt++; | |
689 | avail[info.disk.raid_disk] = 2; | |
690 | } else if (info.events == max_events-1) { | |
691 | cnt1++; | |
692 | avail[info.disk.raid_disk] = 1; | |
693 | } else if (info.events < max_events - 1) | |
694 | ; | |
695 | else if (info.events == max_events+1) { | |
696 | int i; | |
697 | cnt1 = cnt; | |
698 | cnt = 1; | |
699 | max_events = info.events; | |
700 | for (i=0; i<info.array.raid_disks; i++) | |
701 | if (avail[i]) | |
702 | avail[i]--; | |
703 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 704 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
705 | } else { /* info.events much bigger */ |
706 | cnt = 1; cnt1 = 0; | |
707 | memset(avail, 0, info.disk.raid_disk); | |
708 | max_events = info.events; | |
3da92f27 | 709 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
710 | } |
711 | } | |
3da92f27 | 712 | st->ss->free_super(st); |
8382f19b NB |
713 | } |
714 | return cnt + cnt1; | |
715 | } | |
716 | ||
56e8be85 N |
717 | static int array_try_spare(char *devname, int *dfdp, struct dev_policy *pol, |
718 | struct supertype *st, int verbose) | |
f08605b3 N |
719 | { |
720 | /* This device doesn't have any md metadata | |
721 | * If it is 'bare' and theh device policy allows 'spare' look for | |
722 | * an array or container to attach it to. | |
723 | * If st is set, then only arrays of that type are considered | |
724 | * Return 0 on success, or some exit code on failure, probably 1. | |
725 | */ | |
726 | int rv = -1; | |
f08605b3 N |
727 | struct stat stb; |
728 | struct map_ent *mp, *map = NULL; | |
729 | struct mdinfo *chosen = NULL; | |
730 | int dfd = *dfdp; | |
731 | ||
f08605b3 N |
732 | if (fstat(dfd, &stb) != 0) |
733 | return 1; | |
f08605b3 | 734 | |
56e8be85 | 735 | /* |
f08605b3 N |
736 | * Now we need to find a suitable array to add this to. |
737 | * We only accept arrays that: | |
738 | * - match 'st' | |
739 | * - are in the same domains as the device | |
740 | * - are of an size for which the device will be useful | |
741 | * and we choose the one that is the most degraded | |
742 | */ | |
743 | ||
744 | if (map_lock(&map)) { | |
745 | fprintf(stderr, Name ": failed to get exclusive lock on " | |
746 | "mapfile\n"); | |
747 | return 1; | |
748 | } | |
749 | for (mp = map ; mp ; mp = mp->next) { | |
750 | struct supertype *st2; | |
751 | struct domainlist *dl = NULL; | |
752 | struct mdinfo *sra; | |
753 | unsigned long long devsize; | |
754 | ||
755 | if (is_subarray(mp->metadata)) | |
756 | continue; | |
757 | if (st) { | |
758 | st2 = st->ss->match_metadata_desc(mp->metadata); | |
759 | if (!st2 || | |
760 | (st->minor_version >= 0 && | |
761 | st->minor_version != st2->minor_version)) { | |
762 | if (verbose > 1) | |
763 | fprintf(stderr, Name ": not adding %s to %s as metadata type doesn't match\n", | |
764 | devname, mp->path); | |
765 | free(st2); | |
766 | continue; | |
767 | } | |
768 | free(st2); | |
769 | } | |
770 | sra = sysfs_read(-1, mp->devnum, | |
771 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE| | |
772 | GET_DEGRADED|GET_COMPONENT|GET_VERSION); | |
773 | if (!sra) { | |
774 | /* Probably a container - no degraded info */ | |
775 | sra = sysfs_read(-1, mp->devnum, | |
776 | GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE| | |
777 | GET_COMPONENT|GET_VERSION); | |
778 | if (sra) | |
779 | sra->array.failed_disks = 0; | |
780 | } | |
781 | if (!sra) | |
782 | continue; | |
783 | if (st == NULL) { | |
784 | int i; | |
785 | st2 = NULL; | |
786 | for(i=0; !st2 && superlist[i]; i++) | |
787 | st2 = superlist[i]->match_metadata_desc( | |
788 | sra->text_version); | |
789 | } else | |
790 | st2 = st; | |
791 | get_dev_size(dfd, NULL, &devsize); | |
792 | if (st2->ss->avail_size(st2, devsize) < sra->component_size) { | |
793 | if (verbose > 1) | |
794 | fprintf(stderr, Name ": not adding %s to %s as it is too small\n", | |
795 | devname, mp->path); | |
796 | goto next; | |
797 | } | |
798 | dl = domain_from_array(sra, st2->ss->name); | |
799 | if (!domain_test(dl, pol, st2->ss->name)) { | |
800 | /* domain test fails */ | |
801 | if (verbose > 1) | |
802 | fprintf(stderr, Name ": not adding %s to %s as it is not in a compatible domain\n", | |
803 | devname, mp->path); | |
804 | ||
805 | goto next; | |
806 | } | |
807 | /* all tests passed, OK to add to this array */ | |
808 | if (!chosen) { | |
809 | chosen = sra; | |
810 | sra = NULL; | |
811 | } else if (chosen->array.failed_disks < sra->array.failed_disks) { | |
812 | sysfs_free(chosen); | |
813 | chosen = sra; | |
814 | sra = NULL; | |
815 | } | |
816 | next: | |
817 | if (sra) | |
818 | sysfs_free(sra); | |
819 | if (st != st2) | |
820 | free(st2); | |
821 | if (dl) | |
822 | domain_free(dl); | |
823 | } | |
824 | if (chosen) { | |
825 | /* add current device to chosen array as a spare */ | |
826 | int mdfd = open_dev(devname2devnum(chosen->sys_name)); | |
827 | if (mdfd >= 0) { | |
828 | struct mddev_dev_s devlist; | |
829 | char devname[20]; | |
830 | devlist.next = NULL; | |
831 | devlist.used = 0; | |
832 | devlist.re_add = 0; | |
833 | devlist.writemostly = 0; | |
834 | devlist.devname = devname; | |
835 | sprintf(devname, "%d:%d", major(stb.st_rdev), | |
836 | minor(stb.st_rdev)); | |
837 | devlist.disposition = 'a'; | |
838 | close(dfd); | |
839 | *dfdp = -1; | |
840 | rv = Manage_subdevs(chosen->sys_name, mdfd, &devlist, | |
841 | -1, 0); | |
842 | close(mdfd); | |
843 | } | |
844 | if (verbose > 0) { | |
845 | if (rv == 0) | |
846 | fprintf(stderr, Name ": added %s as spare for %s\n", | |
847 | devname, chosen->sys_name); | |
848 | else | |
849 | fprintf(stderr, Name ": failed to add %s as spare for %s\n", | |
850 | devname, chosen->sys_name); | |
851 | } | |
852 | sysfs_free(chosen); | |
853 | } | |
854 | return rv ? 0 : 1; | |
855 | } | |
856 | ||
56e8be85 N |
857 | static int partition_try_spare(char *devname, int *dfdp, struct dev_policy *pol, |
858 | struct supertype *st, int verbose) | |
859 | { | |
860 | return 1; | |
861 | } | |
862 | ||
863 | ||
864 | /* adding a spare to a regular array is quite different from adding one to | |
865 | * a set-of-partitions virtual array. | |
866 | * This function determines which is worth trying and tries as appropriate. | |
867 | * Arrays are given priority over partitions. | |
868 | */ | |
869 | static int try_spare(char *devname, int *dfdp, struct dev_policy *pol, | |
870 | struct supertype *st, int verbose) | |
871 | { | |
872 | int i; | |
873 | int rv; | |
874 | int arrays_ok = 0; | |
875 | int partitions_ok = 0; | |
876 | char bufpad[4096 + 4096]; | |
877 | char *buf = (char*)(((long)bufpad + 4096) & ~4095); | |
878 | int dfd = *dfdp; | |
879 | ||
880 | /* Can only add a spare if device has at least one domains */ | |
881 | if (pol_find(pol, pol_domain) == NULL) | |
882 | return 1; | |
883 | /* And only if some action allows spares */ | |
884 | if (!policy_action_allows(pol, st?st->ss->name:NULL, act_spare)) | |
885 | return 1; | |
886 | ||
887 | /* Now check if the device is bare - we don't add non-bare devices | |
888 | * yet even if action=-spare | |
889 | */ | |
890 | ||
891 | if (lseek(dfd, 0, SEEK_SET) != 0 || | |
892 | read(dfd, buf, 4096) != 4096) { | |
893 | not_bare: | |
894 | if (verbose > 1) | |
895 | fprintf(stderr, Name ": %s is not bare, so not considering as a spare\n", | |
896 | devname); | |
897 | return 1; | |
898 | } | |
899 | if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff') | |
900 | goto not_bare; | |
901 | if (memcmp(buf, buf+1, 4095) != 0) | |
902 | goto not_bare; | |
903 | ||
904 | /* OK, first 4K appear blank, try the end. */ | |
905 | if (lseek(dfd, -4096, SEEK_END) < 0 || | |
906 | read(dfd, buf, 4096) != 4096) | |
907 | goto not_bare; | |
908 | ||
909 | if (buf[0] != '\0' && buf[0] != '\x5a' && buf[0] != '\xff') | |
910 | goto not_bare; | |
911 | if (memcmp(buf, buf+1, 4095) != 0) | |
912 | goto not_bare; | |
913 | ||
914 | /* This device passes our test for 'is bare'. | |
915 | * Let's see what policy allows for such things. | |
916 | */ | |
917 | if (st) { | |
918 | /* just try try 'array' or 'partition' based on this metadata */ | |
919 | if (st->ss->add_to_super) | |
920 | return array_try_spare(devname, dfdp, pol, | |
921 | st, verbose); | |
922 | else | |
923 | return partition_try_spare(devname, dfdp, pol, | |
924 | st, verbose); | |
925 | } | |
926 | /* Now see which metadata type support spare */ | |
927 | for (i = 0; (!arrays_ok || !partitions_ok) && superlist[i] ; i++) { | |
928 | if (superlist[i]->add_to_super && !arrays_ok && | |
929 | policy_action_allows(pol, superlist[i]->name, act_spare)) | |
930 | arrays_ok = 1; | |
931 | if (superlist[i]->add_to_super == NULL && !partitions_ok && | |
932 | policy_action_allows(pol, superlist[i]->name, act_spare)) | |
933 | partitions_ok = 1; | |
934 | } | |
935 | rv = 0; | |
936 | if (arrays_ok) | |
937 | rv = array_try_spare(devname, dfdp, pol, st, verbose); | |
938 | if (rv == 0 && partitions_ok) | |
939 | rv = partition_try_spare(devname, dfdp, pol, st, verbose); | |
940 | return rv; | |
941 | } | |
942 | ||
8382f19b NB |
943 | int IncrementalScan(int verbose) |
944 | { | |
945 | /* look at every device listed in the 'map' file. | |
946 | * If one is found that is not running then: | |
947 | * look in mdadm.conf for bitmap file. | |
948 | * if one exists, but array has none, add it. | |
949 | * try to start array in auto-readonly mode | |
950 | */ | |
951 | struct map_ent *mapl = NULL; | |
952 | struct map_ent *me; | |
953 | mddev_ident_t devs, mddev; | |
954 | int rv = 0; | |
955 | ||
956 | map_read(&mapl); | |
957 | devs = conf_get_ident(NULL); | |
958 | ||
959 | for (me = mapl ; me ; me = me->next) { | |
8382f19b NB |
960 | mdu_array_info_t array; |
961 | mdu_bitmap_file_t bmf; | |
7e0f6979 | 962 | struct mdinfo *sra; |
339c2d6c | 963 | int mdfd = open_dev(me->devnum); |
215bb3f7 | 964 | |
8382f19b NB |
965 | if (mdfd < 0) |
966 | continue; | |
967 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
968 | errno != ENODEV) { | |
969 | close(mdfd); | |
970 | continue; | |
971 | } | |
972 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
973 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
339c2d6c | 974 | if (mddev->devname && me->path |
2400e6eb | 975 | && devname_matches(mddev->devname, me->path)) |
8382f19b NB |
976 | break; |
977 | if (mddev && mddev->bitmap_file) { | |
978 | /* | |
979 | * Note: early kernels will wrongly fail this, so it | |
980 | * is a hint only | |
981 | */ | |
982 | int added = -1; | |
983 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
984 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
985 | if (bmfd >= 0) { | |
986 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
987 | bmfd); | |
988 | close(bmfd); | |
989 | } | |
990 | } | |
991 | if (verbose >= 0) { | |
992 | if (added == 0) | |
993 | fprintf(stderr, Name | |
994 | ": Added bitmap %s to %s\n", | |
995 | mddev->bitmap_file, me->path); | |
996 | else if (errno != EEXIST) | |
997 | fprintf(stderr, Name | |
998 | ": Failed to add bitmap to %s: %s\n", | |
999 | me->path, strerror(errno)); | |
1000 | } | |
1001 | } | |
1002 | sra = sysfs_read(mdfd, 0, 0); | |
1003 | if (sra) { | |
1004 | if (sysfs_set_str(sra, NULL, | |
1005 | "array_state", "read-auto") == 0) { | |
1006 | if (verbose >= 0) | |
1007 | fprintf(stderr, Name | |
1008 | ": started array %s\n", | |
339c2d6c | 1009 | me->path ?: devnum2devname(me->devnum)); |
8382f19b NB |
1010 | } else { |
1011 | fprintf(stderr, Name | |
1012 | ": failed to start array %s: %s\n", | |
339c2d6c N |
1013 | me->path ?: devnum2devname(me->devnum), |
1014 | strerror(errno)); | |
8382f19b NB |
1015 | rv = 1; |
1016 | } | |
1017 | } | |
1018 | } | |
1019 | return rv; | |
1020 | } | |
598f0d58 | 1021 | |
1771a6e2 N |
1022 | static char *container2devname(char *devname) |
1023 | { | |
1024 | char *mdname = NULL; | |
1025 | ||
1026 | if (devname[0] == '/') { | |
1027 | int fd = open(devname, O_RDONLY); | |
1028 | if (fd >= 0) { | |
1029 | mdname = devnum2devname(fd2devnum(fd)); | |
1030 | close(fd); | |
1031 | } | |
1032 | } else { | |
1033 | int uuid[4]; | |
1034 | struct map_ent *mp, *map = NULL; | |
1035 | ||
1036 | if (!parse_uuid(devname, uuid)) | |
1037 | return mdname; | |
1038 | mp = map_by_uuid(&map, uuid); | |
1039 | if (mp) | |
1040 | mdname = devnum2devname(mp->devnum); | |
1041 | map_free(map); | |
1042 | } | |
1043 | ||
1044 | return mdname; | |
1045 | } | |
1046 | ||
598f0d58 | 1047 | int Incremental_container(struct supertype *st, char *devname, int verbose, |
215bb3f7 | 1048 | int runstop, int autof, int trustworthy) |
598f0d58 NB |
1049 | { |
1050 | /* Collect the contents of this container and for each | |
1051 | * array, choose a device name and assemble the array. | |
1052 | */ | |
1053 | ||
1054 | struct mdinfo *list = st->ss->container_content(st); | |
1055 | struct mdinfo *ra; | |
ad5bc697 N |
1056 | struct map_ent *map = NULL; |
1057 | ||
93c861ee DL |
1058 | if (map_lock(&map)) |
1059 | fprintf(stderr, Name ": failed to get exclusive lock on " | |
1060 | "mapfile\n"); | |
598f0d58 NB |
1061 | |
1062 | for (ra = list ; ra ; ra = ra->next) { | |
598f0d58 NB |
1063 | int mdfd; |
1064 | char chosen_name[1024]; | |
ad5bc697 | 1065 | struct map_ent *mp; |
dbb44303 | 1066 | struct mddev_ident_s *match = NULL; |
598f0d58 | 1067 | |
c5afc314 | 1068 | mp = map_by_uuid(&map, ra->uuid); |
d7288ddc | 1069 | |
30926600 N |
1070 | if (mp) { |
1071 | mdfd = open_dev(mp->devnum); | |
339c2d6c N |
1072 | if (mp->path) |
1073 | strcpy(chosen_name, mp->path); | |
1074 | else | |
1075 | strcpy(chosen_name, devnum2devname(mp->devnum)); | |
30926600 | 1076 | } else { |
dbb44303 | 1077 | |
112cace6 | 1078 | /* Check in mdadm.conf for container == devname and |
dbb44303 N |
1079 | * member == ra->text_version after second slash. |
1080 | */ | |
1081 | char *sub = strchr(ra->text_version+1, '/'); | |
1082 | struct mddev_ident_s *array_list; | |
1083 | if (sub) { | |
1084 | sub++; | |
1085 | array_list = conf_get_ident(NULL); | |
1086 | } else | |
1087 | array_list = NULL; | |
1088 | for(; array_list ; array_list = array_list->next) { | |
dbb44303 N |
1089 | char *dn; |
1090 | if (array_list->member == NULL || | |
1091 | array_list->container == NULL) | |
1092 | continue; | |
1093 | if (strcmp(array_list->member, sub) != 0) | |
1094 | continue; | |
71d60c48 DW |
1095 | if (array_list->uuid_set && |
1096 | !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid)) | |
1097 | continue; | |
1771a6e2 N |
1098 | dn = container2devname(array_list->container); |
1099 | if (dn == NULL) | |
dbb44303 | 1100 | continue; |
dbb44303 N |
1101 | if (strncmp(dn, ra->text_version+1, |
1102 | strlen(dn)) != 0 || | |
1103 | ra->text_version[strlen(dn)+1] != '/') { | |
1104 | free(dn); | |
1105 | continue; | |
1106 | } | |
1107 | free(dn); | |
1108 | /* we have a match */ | |
1109 | match = array_list; | |
71d60c48 DW |
1110 | if (verbose>0) |
1111 | fprintf(stderr, Name ": match found for member %s\n", | |
1112 | array_list->member); | |
dbb44303 N |
1113 | break; |
1114 | } | |
dbb44303 | 1115 | |
112cace6 N |
1116 | if (match && match->devname && |
1117 | strcasecmp(match->devname, "<ignore>") == 0) { | |
1118 | if (verbose > 0) | |
1119 | fprintf(stderr, Name ": array %s/%s is " | |
1120 | "explicitly ignored by mdadm.conf\n", | |
1121 | match->container, match->member); | |
1122 | return 2; | |
1123 | } | |
05833051 N |
1124 | if (match) |
1125 | trustworthy = LOCAL; | |
112cace6 | 1126 | |
30926600 N |
1127 | mdfd = create_mddev(match ? match->devname : NULL, |
1128 | ra->name, | |
1129 | autof, | |
1130 | trustworthy, | |
1131 | chosen_name); | |
1132 | } | |
598f0d58 NB |
1133 | |
1134 | if (mdfd < 0) { | |
1135 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
1136 | chosen_name, strerror(errno)); | |
1137 | return 2; | |
1138 | } | |
1139 | ||
03b7f6c6 N |
1140 | assemble_container_content(st, mdfd, ra, runstop, |
1141 | chosen_name, verbose); | |
598f0d58 | 1142 | } |
ad5bc697 | 1143 | map_unlock(&map); |
598f0d58 NB |
1144 | return 0; |
1145 | } | |
29ba4804 N |
1146 | |
1147 | /* | |
1148 | * IncrementalRemove - Attempt to see if the passed in device belongs to any | |
1149 | * raid arrays, and if so first fail (if needed) and then remove the device. | |
1150 | * | |
1151 | * @devname - The device we want to remove | |
1152 | * | |
1153 | * Note: the device name must be a kernel name like "sda", so | |
1154 | * that we can find it in /proc/mdstat | |
1155 | */ | |
1156 | int IncrementalRemove(char *devname, int verbose) | |
1157 | { | |
1158 | int mdfd; | |
aae3cdc3 | 1159 | int rv; |
29ba4804 N |
1160 | struct mdstat_ent *ent; |
1161 | struct mddev_dev_s devlist; | |
1162 | ||
1163 | if (strchr(devname, '/')) { | |
1164 | fprintf(stderr, Name ": incremental removal requires a " | |
1165 | "kernel device name, not a file: %s\n", devname); | |
1166 | return 1; | |
1167 | } | |
1168 | ent = mdstat_by_component(devname); | |
1169 | if (!ent) { | |
1170 | fprintf(stderr, Name ": %s does not appear to be a component " | |
1171 | "of any array\n", devname); | |
1172 | return 1; | |
1173 | } | |
1174 | mdfd = open_dev(ent->devnum); | |
1175 | if (mdfd < 0) { | |
1176 | fprintf(stderr, Name ": Cannot open array %s!!\n", ent->dev); | |
1177 | return 1; | |
1178 | } | |
1179 | memset(&devlist, 0, sizeof(devlist)); | |
1180 | devlist.devname = devname; | |
1181 | devlist.disposition = 'f'; | |
7d2e6486 | 1182 | Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0); |
29ba4804 | 1183 | devlist.disposition = 'r'; |
aae3cdc3 PC |
1184 | rv = Manage_subdevs(ent->dev, mdfd, &devlist, verbose, 0); |
1185 | close(mdfd); | |
1186 | return rv; | |
29ba4804 | 1187 | } |