]>
Commit | Line | Data |
---|---|---|
8382f19b NB |
1 | /* |
2 | * Incremental.c - support --incremental. Part of: | |
3 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
4 | * | |
5 | * Copyright (C) 2006 Neil Brown <neilb@suse.de> | |
6 | * | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | * Author: Neil Brown | |
23 | * Email: <neilb@suse.de> | |
24 | * Paper: Neil Brown | |
25 | * Novell Inc | |
26 | * GPO Box Q1283 | |
27 | * QVB Post Office, NSW 1230 | |
28 | * Australia | |
29 | */ | |
30 | ||
31 | #include "mdadm.h" | |
32 | ||
33 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
34 | struct mdinfo *info); | |
7e0f6979 | 35 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
36 | int number, __u64 events, int verbose, |
37 | char *array_name); | |
38 | ||
39 | int Incremental(char *devname, int verbose, int runstop, | |
40 | struct supertype *st, char *homehost, int autof) | |
41 | { | |
42 | /* Add this device to an array, creating the array if necessary | |
598f0d58 | 43 | * and starting the array if sensible or - if runstop>0 - if possible. |
8382f19b NB |
44 | * |
45 | * This has several steps: | |
46 | * | |
47 | * 1/ Check if device is permitted by mdadm.conf, reject if not. | |
48 | * 2/ Find metadata, reject if none appropriate (check | |
49 | * version/name from args) | |
50 | * 3/ Check if there is a match in mdadm.conf | |
215bb3f7 N |
51 | * 3a/ if not, check for homehost match. If no match, assemble as |
52 | * a 'foreign' array. | |
8382f19b NB |
53 | * 4/ Determine device number. |
54 | * - If in mdadm.conf with std name, use that | |
55 | * - UUID in /var/run/mdadm.map use that | |
56 | * - If name is suggestive, use that. unless in use with different uuid. | |
57 | * - Choose a free, high number. | |
58 | * - Use a partitioned device unless strong suggestion not to. | |
59 | * e.g. auto=md | |
352452c3 | 60 | * Don't choose partitioned for containers. |
8382f19b NB |
61 | * 5/ Find out if array already exists |
62 | * 5a/ if it does not | |
63 | * - choose a name, from mdadm.conf or 'name' field in array. | |
64 | * - create the array | |
65 | * - add the device | |
66 | * 5b/ if it does | |
67 | * - check one drive in array to make sure metadata is a reasonably | |
68 | * close match. Reject if not (e.g. different type) | |
69 | * - add the device | |
70 | * 6/ Make sure /var/run/mdadm.map contains this array. | |
71 | * 7/ Is there enough devices to possibly start the array? | |
352452c3 | 72 | * For a container, this means running Incremental_container. |
8382f19b NB |
73 | * 7a/ if not, finish with success. |
74 | * 7b/ if yes, | |
75 | * - read all metadata and arrange devices like -A does | |
76 | * - if number of OK devices match expected, or -R and there are enough, | |
77 | * start the array (auto-readonly). | |
78 | */ | |
79 | struct stat stb; | |
f35f2525 | 80 | struct mdinfo info; |
8382f19b NB |
81 | struct mddev_ident_s *array_list, *match; |
82 | char chosen_name[1024]; | |
83 | int rv; | |
8382f19b NB |
84 | struct map_ent *mp, *map = NULL; |
85 | int dfd, mdfd; | |
86 | char *avail; | |
87 | int active_disks; | |
215bb3f7 | 88 | int trustworthy = FOREIGN; |
d7288ddc | 89 | char *name_to_use; |
ad5bc697 | 90 | mdu_array_info_t ainf; |
8382f19b NB |
91 | |
92 | struct createinfo *ci = conf_get_create_info(); | |
93 | ||
8382f19b | 94 | |
352452c3 | 95 | /* 1/ Check if device is permitted by mdadm.conf */ |
8382f19b NB |
96 | |
97 | if (!conf_test_dev(devname)) { | |
98 | if (verbose >= 0) | |
99 | fprintf(stderr, Name | |
100 | ": %s not permitted by mdadm.conf.\n", | |
101 | devname); | |
102 | return 1; | |
103 | } | |
104 | ||
105 | /* 2/ Find metadata, reject if none appropriate (check | |
106 | * version/name from args) */ | |
107 | ||
108 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
109 | if (dfd < 0) { | |
110 | if (verbose >= 0) | |
111 | fprintf(stderr, Name ": cannot open %s: %s.\n", | |
112 | devname, strerror(errno)); | |
113 | return 1; | |
114 | } | |
115 | if (fstat(dfd, &stb) < 0) { | |
116 | if (verbose >= 0) | |
117 | fprintf(stderr, Name ": fstat failed for %s: %s.\n", | |
118 | devname, strerror(errno)); | |
119 | close(dfd); | |
120 | return 1; | |
121 | } | |
122 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
123 | if (verbose >= 0) | |
124 | fprintf(stderr, Name ": %s is not a block device.\n", | |
125 | devname); | |
126 | close(dfd); | |
127 | return 1; | |
128 | } | |
129 | ||
130 | if (st == NULL && (st = guess_super(dfd)) == NULL) { | |
131 | if (verbose >= 0) | |
132 | fprintf(stderr, Name | |
133 | ": no recognisable superblock on %s.\n", | |
134 | devname); | |
135 | close(dfd); | |
136 | return 1; | |
137 | } | |
3da92f27 | 138 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
139 | if (verbose >= 0) |
140 | fprintf(stderr, Name ": no RAID superblock on %s.\n", | |
141 | devname); | |
142 | close(dfd); | |
143 | return 1; | |
144 | } | |
8382f19b NB |
145 | close (dfd); |
146 | ||
f35f2525 | 147 | memset(&info, 0, sizeof(info)); |
598f0d58 | 148 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
149 | /* 3/ Check if there is a match in mdadm.conf */ |
150 | ||
151 | array_list = conf_get_ident(NULL); | |
152 | match = NULL; | |
153 | for (; array_list; array_list = array_list->next) { | |
154 | if (array_list->uuid_set && | |
155 | same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid) | |
156 | == 0) { | |
fe056d1f | 157 | if (verbose >= 2 && array_list->devname) |
8382f19b NB |
158 | fprintf(stderr, Name |
159 | ": UUID differs from %s.\n", | |
160 | array_list->devname); | |
161 | continue; | |
162 | } | |
163 | if (array_list->name[0] && | |
164 | strcasecmp(array_list->name, info.name) != 0) { | |
fe056d1f | 165 | if (verbose >= 2 && array_list->devname) |
8382f19b NB |
166 | fprintf(stderr, Name |
167 | ": Name differs from %s.\n", | |
168 | array_list->devname); | |
169 | continue; | |
170 | } | |
171 | if (array_list->devices && | |
172 | !match_oneof(array_list->devices, devname)) { | |
fe056d1f | 173 | if (verbose >= 2 && array_list->devname) |
8382f19b NB |
174 | fprintf(stderr, Name |
175 | ": Not a listed device for %s.\n", | |
176 | array_list->devname); | |
177 | continue; | |
178 | } | |
179 | if (array_list->super_minor != UnSet && | |
180 | array_list->super_minor != info.array.md_minor) { | |
fe056d1f | 181 | if (verbose >= 2 && array_list->devname) |
8382f19b NB |
182 | fprintf(stderr, Name |
183 | ": Different super-minor to %s.\n", | |
184 | array_list->devname); | |
185 | continue; | |
186 | } | |
187 | if (!array_list->uuid_set && | |
188 | !array_list->name[0] && | |
189 | !array_list->devices && | |
190 | array_list->super_minor == UnSet) { | |
fe056d1f | 191 | if (verbose >= 2 && array_list->devname) |
8382f19b NB |
192 | fprintf(stderr, Name |
193 | ": %s doesn't have any identifying information.\n", | |
194 | array_list->devname); | |
195 | continue; | |
196 | } | |
197 | /* FIXME, should I check raid_disks and level too?? */ | |
198 | ||
199 | if (match) { | |
fe056d1f N |
200 | if (verbose >= 0) { |
201 | if (match->devname && array_list->devname) | |
202 | fprintf(stderr, Name | |
8382f19b | 203 | ": we match both %s and %s - cannot decide which to use.\n", |
fe056d1f N |
204 | match->devname, array_list->devname); |
205 | else | |
206 | fprintf(stderr, Name | |
207 | ": multiple lines in mdadm.conf match\n"); | |
208 | } | |
8382f19b NB |
209 | return 2; |
210 | } | |
211 | match = array_list; | |
212 | } | |
213 | ||
112cace6 N |
214 | if (match && match->devname |
215 | && strcasecmp(match->devname, "<ignore>") == 0) { | |
216 | if (verbose >= 0) | |
217 | fprintf(stderr, Name ": array containing %s is explicitly" | |
218 | " ignored by mdadm.conf\n", | |
219 | devname); | |
220 | return 1; | |
221 | } | |
222 | ||
7b403fef N |
223 | /* 3a/ if not, check for homehost match. If no match, continue |
224 | * but don't trust the 'name' in the array. Thus a 'random' minor | |
225 | * number will be assigned, and the device name will be based | |
226 | * on that. */ | |
215bb3f7 N |
227 | if (match) |
228 | trustworthy = LOCAL; | |
745f72f6 N |
229 | else if ((homehost == NULL || |
230 | st->ss->match_home(st, homehost) != 1) && | |
231 | st->ss->match_home(st, "any") != 1) | |
215bb3f7 | 232 | trustworthy = FOREIGN; |
215bb3f7 | 233 | else |
ecb02e31 | 234 | trustworthy = LOCAL; |
e12e1d1d | 235 | |
4ef2f11e N |
236 | /* There are three possible sources for 'autof': command line, |
237 | * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf. | |
350ac35d N |
238 | * ARRAY takes precedence, then command line, then |
239 | * CREATE. | |
4ef2f11e | 240 | */ |
350ac35d | 241 | if (match && match->autof) |
4ef2f11e N |
242 | autof = match->autof; |
243 | if (autof == 0) | |
244 | autof = ci->autof; | |
245 | ||
215bb3f7 N |
246 | if (st->ss->container_content && st->loaded_container) { |
247 | /* This is a pre-built container array, so we do something | |
248 | * rather different. | |
249 | */ | |
250 | return Incremental_container(st, devname, verbose, runstop, | |
251 | autof, trustworthy); | |
8382f19b | 252 | } |
ecb02e31 N |
253 | name_to_use = strchr(info.name, ':'); |
254 | if (name_to_use) | |
255 | name_to_use++; | |
256 | else | |
257 | name_to_use = info.name; | |
258 | ||
259 | if ((!name_to_use || name_to_use[0] == 0) && | |
260 | info.array.level == LEVEL_CONTAINER && | |
261 | trustworthy == LOCAL) { | |
262 | name_to_use = info.text_version; | |
263 | trustworthy = METADATA; | |
264 | } | |
265 | ||
ad5bc697 | 266 | /* 4/ Check if array exists. |
215bb3f7 | 267 | */ |
ad5bc697 | 268 | map_lock(&map); |
215bb3f7 | 269 | mp = map_by_uuid(&map, info.uuid); |
ffb3e494 | 270 | if (mp) { |
215bb3f7 | 271 | mdfd = open_mddev(mp->path, 0); |
ffb3e494 N |
272 | if (mdfd < 0 && mddev_busy(mp->devnum)) { |
273 | /* maybe udev hasn't created it yet. */ | |
274 | char buf[50]; | |
275 | sprintf(buf, "%d:%d", dev2major(mp->devnum), | |
276 | dev2minor(mp->devnum)); | |
277 | mdfd = dev_open(buf, O_RDWR); | |
278 | } | |
279 | } else | |
215bb3f7 | 280 | mdfd = -1; |
d7288ddc | 281 | |
8382f19b | 282 | if (mdfd < 0) { |
7e0f6979 | 283 | struct mdinfo *sra; |
c5afc314 | 284 | struct mdinfo dinfo; |
8382f19b | 285 | |
215bb3f7 | 286 | /* Couldn't find an existing array, maybe make a new one */ |
ffb3e494 | 287 | mdfd = create_mddev(match ? match->devname : NULL, |
e12e1d1d | 288 | name_to_use, autof, trustworthy, chosen_name); |
215bb3f7 N |
289 | |
290 | if (mdfd < 0) | |
291 | return 1; | |
292 | ||
293 | sysfs_init(&info, mdfd, 0); | |
294 | ||
f35f2525 N |
295 | if (set_array_info(mdfd, st, &info) != 0) { |
296 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", | |
8382f19b NB |
297 | chosen_name, strerror(errno)); |
298 | close(mdfd); | |
299 | return 2; | |
300 | } | |
7801ac20 | 301 | |
c5afc314 N |
302 | dinfo = info; |
303 | dinfo.disk.major = major(stb.st_rdev); | |
304 | dinfo.disk.minor = minor(stb.st_rdev); | |
305 | if (add_disk(mdfd, st, &info, &dinfo) != 0) { | |
8382f19b NB |
306 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", |
307 | devname, chosen_name, strerror(errno)); | |
308 | ioctl(mdfd, STOP_ARRAY, 0); | |
309 | close(mdfd); | |
310 | return 2; | |
311 | } | |
197e3eb6 | 312 | sra = sysfs_read(mdfd, fd2devnum(mdfd), GET_DEVS); |
06c7f68e | 313 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { |
8382f19b NB |
314 | /* It really should be 'none' - must be old buggy |
315 | * kernel, and mdadm -I may not be able to complete. | |
316 | * So reject it. | |
317 | */ | |
318 | ioctl(mdfd, STOP_ARRAY, NULL); | |
319 | fprintf(stderr, Name | |
320 | ": You have an old buggy kernel which cannot support\n" | |
321 | " --incremental reliably. Aborting.\n"); | |
322 | close(mdfd); | |
323 | sysfs_free(sra); | |
324 | return 2; | |
325 | } | |
c5afc314 | 326 | info.array.working_disks = 1; |
f35f2525 | 327 | sysfs_free(sra); |
ad5bc697 N |
328 | /* 6/ Make sure /var/run/mdadm.map contains this array. */ |
329 | map_update(&map, fd2devnum(mdfd), | |
330 | info.text_version, | |
331 | info.uuid, chosen_name); | |
8382f19b NB |
332 | } else { |
333 | /* 5b/ if it does */ | |
334 | /* - check one drive in array to make sure metadata is a reasonably */ | |
335 | /* close match. Reject if not (e.g. different type) */ | |
336 | /* - add the device */ | |
337 | char dn[20]; | |
338 | int dfd2; | |
8382f19b | 339 | int err; |
7e0f6979 | 340 | struct mdinfo *sra; |
3da92f27 | 341 | struct supertype *st2; |
c5afc314 | 342 | struct mdinfo info2, *d; |
215bb3f7 N |
343 | |
344 | strcpy(chosen_name, mp->path); | |
345 | ||
197e3eb6 | 346 | sra = sysfs_read(mdfd, fd2devnum(mdfd), (GET_DEVS | GET_STATE)); |
2cdb6489 | 347 | |
7c548327 N |
348 | if (sra->devs) { |
349 | sprintf(dn, "%d:%d", sra->devs->disk.major, | |
350 | sra->devs->disk.minor); | |
351 | dfd2 = dev_open(dn, O_RDONLY); | |
352 | st2 = dup_super(st); | |
353 | if (st2->ss->load_super(st2, dfd2, NULL) || | |
354 | st->ss->compare_super(st, st2) != 0) { | |
355 | fprintf(stderr, Name | |
356 | ": metadata mismatch between %s and " | |
357 | "chosen array %s\n", | |
358 | devname, chosen_name); | |
359 | close(mdfd); | |
360 | close(dfd2); | |
361 | return 2; | |
362 | } | |
8382f19b | 363 | close(dfd2); |
7c548327 N |
364 | memset(&info2, 0, sizeof(info2)); |
365 | st2->ss->getinfo_super(st2, &info2); | |
366 | st2->ss->free_super(st2); | |
367 | if (info.array.level != info2.array.level || | |
368 | memcmp(info.uuid, info2.uuid, 16) != 0 || | |
369 | info.array.raid_disks != info2.array.raid_disks) { | |
370 | fprintf(stderr, Name | |
371 | ": unexpected difference between %s and %s.\n", | |
372 | chosen_name, devname); | |
373 | close(mdfd); | |
374 | return 2; | |
375 | } | |
8382f19b | 376 | } |
7801ac20 N |
377 | info2.disk.major = major(stb.st_rdev); |
378 | info2.disk.minor = minor(stb.st_rdev); | |
c5afc314 N |
379 | /* add disk needs to know about containers */ |
380 | if (st->ss->external) | |
381 | sra->array.level = LEVEL_CONTAINER; | |
7801ac20 | 382 | err = add_disk(mdfd, st2, sra, &info2); |
8382f19b NB |
383 | if (err < 0 && errno == EBUSY) { |
384 | /* could be another device present with the same | |
385 | * disk.number. Find and reject any such | |
386 | */ | |
387 | find_reject(mdfd, st, sra, info.disk.number, | |
388 | info.events, verbose, chosen_name); | |
7801ac20 | 389 | err = add_disk(mdfd, st2, sra, &info2); |
8382f19b NB |
390 | } |
391 | if (err < 0) { | |
392 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
393 | devname, chosen_name, strerror(errno)); | |
394 | close(mdfd); | |
395 | return 2; | |
396 | } | |
c5afc314 N |
397 | info.array.working_disks = 0; |
398 | for (d = sra->devs; d; d=d->next) | |
399 | info.array.working_disks ++; | |
400 | ||
8382f19b | 401 | } |
8382f19b NB |
402 | |
403 | /* 7/ Is there enough devices to possibly start the array? */ | |
404 | /* 7a/ if not, finish with success. */ | |
352452c3 N |
405 | if (info.array.level == LEVEL_CONTAINER) { |
406 | /* Try to assemble within the container */ | |
ad5bc697 | 407 | map_unlock(&map); |
97590376 | 408 | sysfs_uevent(&info, "change"); |
c5afc314 N |
409 | if (verbose >= 0) |
410 | fprintf(stderr, Name | |
411 | ": container %s now has %d devices\n", | |
412 | chosen_name, info.array.working_disks); | |
a7c6e3fb N |
413 | wait_for(chosen_name, mdfd); |
414 | close(mdfd); | |
fdb482f9 DW |
415 | if (runstop < 0) |
416 | return 0; /* don't try to assemble */ | |
03b7f6c6 N |
417 | rv = Incremental(chosen_name, verbose, runstop, |
418 | NULL, homehost, autof); | |
419 | if (rv == 1) | |
420 | /* Don't fail the whole -I if a subarray didn't | |
421 | * have enough devices to start yet | |
422 | */ | |
423 | rv = 0; | |
424 | return rv; | |
352452c3 | 425 | } |
f8409e54 | 426 | avail = NULL; |
8382f19b NB |
427 | active_disks = count_active(st, mdfd, &avail, &info); |
428 | if (enough(info.array.level, info.array.raid_disks, | |
429 | info.array.layout, info.array.state & 1, | |
fdb482f9 DW |
430 | avail, active_disks) == 0 || |
431 | (runstop < 0 && active_disks < info.array.raid_disks)) { | |
8382f19b NB |
432 | free(avail); |
433 | if (verbose >= 0) | |
434 | fprintf(stderr, Name | |
435 | ": %s attached to %s, not enough to start (%d).\n", | |
436 | devname, chosen_name, active_disks); | |
ad5bc697 | 437 | map_unlock(&map); |
8382f19b NB |
438 | close(mdfd); |
439 | return 0; | |
440 | } | |
441 | free(avail); | |
442 | ||
443 | /* 7b/ if yes, */ | |
444 | /* - if number of OK devices match expected, or -R and there */ | |
445 | /* are enough, */ | |
446 | /* + add any bitmap file */ | |
447 | /* + start the array (auto-readonly). */ | |
8382f19b NB |
448 | |
449 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
450 | if (verbose >= 0) | |
451 | fprintf(stderr, Name | |
452 | ": %s attached to %s which is already active.\n", | |
453 | devname, chosen_name); | |
ad5bc697 N |
454 | close(mdfd); |
455 | map_unlock(&map); | |
8382f19b NB |
456 | return 0; |
457 | } | |
ad5bc697 N |
458 | |
459 | map_unlock(&map); | |
8382f19b | 460 | if (runstop > 0 || active_disks >= info.array.working_disks) { |
7e0f6979 | 461 | struct mdinfo *sra; |
8382f19b NB |
462 | /* Let's try to start it */ |
463 | if (match && match->bitmap_file) { | |
464 | int bmfd = open(match->bitmap_file, O_RDWR); | |
465 | if (bmfd < 0) { | |
466 | fprintf(stderr, Name | |
467 | ": Could not open bitmap file %s.\n", | |
468 | match->bitmap_file); | |
469 | close(mdfd); | |
470 | return 1; | |
471 | } | |
472 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
473 | close(bmfd); | |
474 | fprintf(stderr, Name | |
475 | ": Failed to set bitmapfile for %s.\n", | |
476 | chosen_name); | |
477 | close(mdfd); | |
478 | return 1; | |
479 | } | |
480 | close(bmfd); | |
481 | } | |
197e3eb6 | 482 | sra = sysfs_read(mdfd, fd2devnum(mdfd), 0); |
7b403fef | 483 | if ((sra == NULL || active_disks >= info.array.working_disks) |
215bb3f7 | 484 | && trustworthy != FOREIGN) |
8382f19b NB |
485 | rv = ioctl(mdfd, RUN_ARRAY, NULL); |
486 | else | |
487 | rv = sysfs_set_str(sra, NULL, | |
488 | "array_state", "read-auto"); | |
489 | if (rv == 0) { | |
490 | if (verbose >= 0) | |
491 | fprintf(stderr, Name | |
492 | ": %s attached to %s, which has been started.\n", | |
493 | devname, chosen_name); | |
494 | rv = 0; | |
a7c6e3fb | 495 | wait_for(chosen_name, mdfd); |
8382f19b NB |
496 | } else { |
497 | fprintf(stderr, Name | |
498 | ": %s attached to %s, but failed to start: %s.\n", | |
499 | devname, chosen_name, strerror(errno)); | |
500 | rv = 1; | |
501 | } | |
502 | } else { | |
503 | if (verbose >= 0) | |
504 | fprintf(stderr, Name | |
505 | ": %s attached to %s, not enough to start safely.\n", | |
506 | devname, chosen_name); | |
507 | rv = 0; | |
508 | } | |
509 | close(mdfd); | |
510 | return rv; | |
511 | } | |
512 | ||
7e0f6979 | 513 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
514 | int number, __u64 events, int verbose, |
515 | char *array_name) | |
516 | { | |
3da92f27 | 517 | /* Find a device attached to this array with a disk.number of number |
8382f19b NB |
518 | * and events less than the passed events, and remove the device. |
519 | */ | |
06c7f68e | 520 | struct mdinfo *d; |
8382f19b NB |
521 | mdu_array_info_t ra; |
522 | ||
523 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
524 | return; /* not safe to remove from active arrays | |
525 | * without thinking more */ | |
526 | ||
527 | for (d = sra->devs; d ; d = d->next) { | |
528 | char dn[10]; | |
529 | int dfd; | |
8382f19b | 530 | struct mdinfo info; |
06c7f68e | 531 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
532 | dfd = dev_open(dn, O_RDONLY); |
533 | if (dfd < 0) | |
534 | continue; | |
3da92f27 | 535 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
536 | close(dfd); |
537 | continue; | |
538 | } | |
3da92f27 NB |
539 | st->ss->getinfo_super(st, &info); |
540 | st->ss->free_super(st); | |
8382f19b NB |
541 | close(dfd); |
542 | ||
543 | if (info.disk.number != number || | |
544 | info.events >= events) | |
545 | continue; | |
546 | ||
06c7f68e | 547 | if (d->disk.raid_disk > -1) |
8382f19b NB |
548 | sysfs_set_str(sra, d, "slot", "none"); |
549 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
550 | if (verbose >= 0) | |
551 | fprintf(stderr, Name | |
552 | ": removing old device %s from %s\n", | |
06c7f68e | 553 | d->sys_name+4, array_name); |
8382f19b NB |
554 | } |
555 | } | |
556 | ||
557 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
558 | struct mdinfo *bestinfo) | |
559 | { | |
560 | /* count how many devices in sra think they are active */ | |
06c7f68e | 561 | struct mdinfo *d; |
8382f19b NB |
562 | int cnt = 0, cnt1 = 0; |
563 | __u64 max_events = 0; | |
7e0f6979 | 564 | struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); |
8382f19b NB |
565 | char *avail = NULL; |
566 | ||
567 | for (d = sra->devs ; d ; d = d->next) { | |
568 | char dn[30]; | |
569 | int dfd; | |
8382f19b NB |
570 | int ok; |
571 | struct mdinfo info; | |
572 | ||
06c7f68e | 573 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
574 | dfd = dev_open(dn, O_RDONLY); |
575 | if (dfd < 0) | |
576 | continue; | |
3da92f27 | 577 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
578 | close(dfd); |
579 | if (ok != 0) | |
580 | continue; | |
3da92f27 | 581 | st->ss->getinfo_super(st, &info); |
43aaf431 DL |
582 | if (!avail) { |
583 | avail = malloc(info.array.raid_disks); | |
584 | if (!avail) { | |
585 | fprintf(stderr, Name ": out of memory.\n"); | |
586 | exit(1); | |
587 | } | |
588 | memset(avail, 0, info.array.raid_disks); | |
589 | *availp = avail; | |
590 | } | |
591 | ||
8382f19b NB |
592 | if (info.disk.state & (1<<MD_DISK_SYNC)) |
593 | { | |
8382f19b NB |
594 | if (cnt == 0) { |
595 | cnt++; | |
596 | max_events = info.events; | |
597 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 598 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
599 | } else if (info.events == max_events) { |
600 | cnt++; | |
601 | avail[info.disk.raid_disk] = 2; | |
602 | } else if (info.events == max_events-1) { | |
603 | cnt1++; | |
604 | avail[info.disk.raid_disk] = 1; | |
605 | } else if (info.events < max_events - 1) | |
606 | ; | |
607 | else if (info.events == max_events+1) { | |
608 | int i; | |
609 | cnt1 = cnt; | |
610 | cnt = 1; | |
611 | max_events = info.events; | |
612 | for (i=0; i<info.array.raid_disks; i++) | |
613 | if (avail[i]) | |
614 | avail[i]--; | |
615 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 616 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
617 | } else { /* info.events much bigger */ |
618 | cnt = 1; cnt1 = 0; | |
619 | memset(avail, 0, info.disk.raid_disk); | |
620 | max_events = info.events; | |
3da92f27 | 621 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
622 | } |
623 | } | |
3da92f27 | 624 | st->ss->free_super(st); |
8382f19b NB |
625 | } |
626 | return cnt + cnt1; | |
627 | } | |
628 | ||
8382f19b NB |
629 | int IncrementalScan(int verbose) |
630 | { | |
631 | /* look at every device listed in the 'map' file. | |
632 | * If one is found that is not running then: | |
633 | * look in mdadm.conf for bitmap file. | |
634 | * if one exists, but array has none, add it. | |
635 | * try to start array in auto-readonly mode | |
636 | */ | |
637 | struct map_ent *mapl = NULL; | |
638 | struct map_ent *me; | |
639 | mddev_ident_t devs, mddev; | |
640 | int rv = 0; | |
641 | ||
642 | map_read(&mapl); | |
643 | devs = conf_get_ident(NULL); | |
644 | ||
645 | for (me = mapl ; me ; me = me->next) { | |
8382f19b NB |
646 | mdu_array_info_t array; |
647 | mdu_bitmap_file_t bmf; | |
7e0f6979 | 648 | struct mdinfo *sra; |
215bb3f7 N |
649 | int mdfd = open_mddev(me->path, 0); |
650 | ||
8382f19b NB |
651 | if (mdfd < 0) |
652 | continue; | |
653 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
654 | errno != ENODEV) { | |
655 | close(mdfd); | |
656 | continue; | |
657 | } | |
658 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
659 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
fe056d1f N |
660 | if (mddev->devname |
661 | && strcmp(mddev->devname, me->path) == 0) | |
8382f19b NB |
662 | break; |
663 | if (mddev && mddev->bitmap_file) { | |
664 | /* | |
665 | * Note: early kernels will wrongly fail this, so it | |
666 | * is a hint only | |
667 | */ | |
668 | int added = -1; | |
669 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
670 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
671 | if (bmfd >= 0) { | |
672 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
673 | bmfd); | |
674 | close(bmfd); | |
675 | } | |
676 | } | |
677 | if (verbose >= 0) { | |
678 | if (added == 0) | |
679 | fprintf(stderr, Name | |
680 | ": Added bitmap %s to %s\n", | |
681 | mddev->bitmap_file, me->path); | |
682 | else if (errno != EEXIST) | |
683 | fprintf(stderr, Name | |
684 | ": Failed to add bitmap to %s: %s\n", | |
685 | me->path, strerror(errno)); | |
686 | } | |
687 | } | |
688 | sra = sysfs_read(mdfd, 0, 0); | |
689 | if (sra) { | |
690 | if (sysfs_set_str(sra, NULL, | |
691 | "array_state", "read-auto") == 0) { | |
692 | if (verbose >= 0) | |
693 | fprintf(stderr, Name | |
694 | ": started array %s\n", | |
695 | me->path); | |
696 | } else { | |
697 | fprintf(stderr, Name | |
698 | ": failed to start array %s: %s\n", | |
699 | me->path, strerror(errno)); | |
700 | rv = 1; | |
701 | } | |
702 | } | |
703 | } | |
704 | return rv; | |
705 | } | |
598f0d58 | 706 | |
1771a6e2 N |
707 | static char *container2devname(char *devname) |
708 | { | |
709 | char *mdname = NULL; | |
710 | ||
711 | if (devname[0] == '/') { | |
712 | int fd = open(devname, O_RDONLY); | |
713 | if (fd >= 0) { | |
714 | mdname = devnum2devname(fd2devnum(fd)); | |
715 | close(fd); | |
716 | } | |
717 | } else { | |
718 | int uuid[4]; | |
719 | struct map_ent *mp, *map = NULL; | |
720 | ||
721 | if (!parse_uuid(devname, uuid)) | |
722 | return mdname; | |
723 | mp = map_by_uuid(&map, uuid); | |
724 | if (mp) | |
725 | mdname = devnum2devname(mp->devnum); | |
726 | map_free(map); | |
727 | } | |
728 | ||
729 | return mdname; | |
730 | } | |
731 | ||
598f0d58 | 732 | int Incremental_container(struct supertype *st, char *devname, int verbose, |
215bb3f7 | 733 | int runstop, int autof, int trustworthy) |
598f0d58 NB |
734 | { |
735 | /* Collect the contents of this container and for each | |
736 | * array, choose a device name and assemble the array. | |
737 | */ | |
738 | ||
739 | struct mdinfo *list = st->ss->container_content(st); | |
740 | struct mdinfo *ra; | |
ad5bc697 N |
741 | struct map_ent *map = NULL; |
742 | ||
743 | map_lock(&map); | |
598f0d58 NB |
744 | |
745 | for (ra = list ; ra ; ra = ra->next) { | |
598f0d58 NB |
746 | int mdfd; |
747 | char chosen_name[1024]; | |
ad5bc697 | 748 | struct map_ent *mp; |
dbb44303 | 749 | struct mddev_ident_s *match = NULL; |
598f0d58 | 750 | |
c5afc314 | 751 | mp = map_by_uuid(&map, ra->uuid); |
d7288ddc | 752 | |
30926600 N |
753 | if (mp) { |
754 | mdfd = open_dev(mp->devnum); | |
755 | strcpy(chosen_name, mp->path); | |
756 | } else { | |
dbb44303 | 757 | |
112cace6 | 758 | /* Check in mdadm.conf for container == devname and |
dbb44303 N |
759 | * member == ra->text_version after second slash. |
760 | */ | |
761 | char *sub = strchr(ra->text_version+1, '/'); | |
762 | struct mddev_ident_s *array_list; | |
763 | if (sub) { | |
764 | sub++; | |
765 | array_list = conf_get_ident(NULL); | |
766 | } else | |
767 | array_list = NULL; | |
768 | for(; array_list ; array_list = array_list->next) { | |
dbb44303 N |
769 | char *dn; |
770 | if (array_list->member == NULL || | |
771 | array_list->container == NULL) | |
772 | continue; | |
773 | if (strcmp(array_list->member, sub) != 0) | |
774 | continue; | |
71d60c48 DW |
775 | if (array_list->uuid_set && |
776 | !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid)) | |
777 | continue; | |
1771a6e2 N |
778 | dn = container2devname(array_list->container); |
779 | if (dn == NULL) | |
dbb44303 | 780 | continue; |
dbb44303 N |
781 | if (strncmp(dn, ra->text_version+1, |
782 | strlen(dn)) != 0 || | |
783 | ra->text_version[strlen(dn)+1] != '/') { | |
784 | free(dn); | |
785 | continue; | |
786 | } | |
787 | free(dn); | |
788 | /* we have a match */ | |
789 | match = array_list; | |
71d60c48 DW |
790 | if (verbose>0) |
791 | fprintf(stderr, Name ": match found for member %s\n", | |
792 | array_list->member); | |
dbb44303 N |
793 | break; |
794 | } | |
dbb44303 | 795 | |
112cace6 N |
796 | if (match && match->devname && |
797 | strcasecmp(match->devname, "<ignore>") == 0) { | |
798 | if (verbose > 0) | |
799 | fprintf(stderr, Name ": array %s/%s is " | |
800 | "explicitly ignored by mdadm.conf\n", | |
801 | match->container, match->member); | |
802 | return 2; | |
803 | } | |
804 | ||
30926600 N |
805 | mdfd = create_mddev(match ? match->devname : NULL, |
806 | ra->name, | |
807 | autof, | |
808 | trustworthy, | |
809 | chosen_name); | |
810 | } | |
598f0d58 NB |
811 | |
812 | if (mdfd < 0) { | |
813 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
814 | chosen_name, strerror(errno)); | |
815 | return 2; | |
816 | } | |
817 | ||
03b7f6c6 N |
818 | assemble_container_content(st, mdfd, ra, runstop, |
819 | chosen_name, verbose); | |
598f0d58 | 820 | } |
ad5bc697 | 821 | map_unlock(&map); |
598f0d58 NB |
822 | return 0; |
823 | } |