]>
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 | ||
31015d57 N |
223 | if (!match && !conf_test_metadata(st->ss->name)) { |
224 | if (verbose >= 1) | |
225 | fprintf(stderr, Name | |
226 | ": %s has metadata type %s for which " | |
227 | "auto-assembly is disabled\n", | |
228 | devname, st->ss->name); | |
229 | return 1; | |
230 | } | |
231 | ||
7b403fef N |
232 | /* 3a/ if not, check for homehost match. If no match, continue |
233 | * but don't trust the 'name' in the array. Thus a 'random' minor | |
234 | * number will be assigned, and the device name will be based | |
235 | * on that. */ | |
215bb3f7 N |
236 | if (match) |
237 | trustworthy = LOCAL; | |
745f72f6 N |
238 | else if ((homehost == NULL || |
239 | st->ss->match_home(st, homehost) != 1) && | |
240 | st->ss->match_home(st, "any") != 1) | |
215bb3f7 | 241 | trustworthy = FOREIGN; |
215bb3f7 | 242 | else |
ecb02e31 | 243 | trustworthy = LOCAL; |
e12e1d1d | 244 | |
4ef2f11e N |
245 | /* There are three possible sources for 'autof': command line, |
246 | * ARRAY line in mdadm.conf, or CREATE line in mdadm.conf. | |
350ac35d N |
247 | * ARRAY takes precedence, then command line, then |
248 | * CREATE. | |
4ef2f11e | 249 | */ |
350ac35d | 250 | if (match && match->autof) |
4ef2f11e N |
251 | autof = match->autof; |
252 | if (autof == 0) | |
253 | autof = ci->autof; | |
254 | ||
215bb3f7 N |
255 | if (st->ss->container_content && st->loaded_container) { |
256 | /* This is a pre-built container array, so we do something | |
257 | * rather different. | |
258 | */ | |
259 | return Incremental_container(st, devname, verbose, runstop, | |
260 | autof, trustworthy); | |
8382f19b | 261 | } |
ecb02e31 N |
262 | name_to_use = strchr(info.name, ':'); |
263 | if (name_to_use) | |
264 | name_to_use++; | |
265 | else | |
266 | name_to_use = info.name; | |
267 | ||
268 | if ((!name_to_use || name_to_use[0] == 0) && | |
269 | info.array.level == LEVEL_CONTAINER && | |
270 | trustworthy == LOCAL) { | |
271 | name_to_use = info.text_version; | |
272 | trustworthy = METADATA; | |
273 | } | |
274 | ||
ad5bc697 | 275 | /* 4/ Check if array exists. |
215bb3f7 | 276 | */ |
ad5bc697 | 277 | map_lock(&map); |
215bb3f7 | 278 | mp = map_by_uuid(&map, info.uuid); |
ffb3e494 | 279 | if (mp) { |
215bb3f7 | 280 | mdfd = open_mddev(mp->path, 0); |
ffb3e494 N |
281 | if (mdfd < 0 && mddev_busy(mp->devnum)) { |
282 | /* maybe udev hasn't created it yet. */ | |
283 | char buf[50]; | |
284 | sprintf(buf, "%d:%d", dev2major(mp->devnum), | |
285 | dev2minor(mp->devnum)); | |
286 | mdfd = dev_open(buf, O_RDWR); | |
287 | } | |
288 | } else | |
215bb3f7 | 289 | mdfd = -1; |
d7288ddc | 290 | |
8382f19b | 291 | if (mdfd < 0) { |
7e0f6979 | 292 | struct mdinfo *sra; |
c5afc314 | 293 | struct mdinfo dinfo; |
8382f19b | 294 | |
215bb3f7 | 295 | /* Couldn't find an existing array, maybe make a new one */ |
ffb3e494 | 296 | mdfd = create_mddev(match ? match->devname : NULL, |
e12e1d1d | 297 | name_to_use, autof, trustworthy, chosen_name); |
215bb3f7 N |
298 | |
299 | if (mdfd < 0) | |
300 | return 1; | |
301 | ||
302 | sysfs_init(&info, mdfd, 0); | |
303 | ||
f35f2525 N |
304 | if (set_array_info(mdfd, st, &info) != 0) { |
305 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", | |
8382f19b NB |
306 | chosen_name, strerror(errno)); |
307 | close(mdfd); | |
308 | return 2; | |
309 | } | |
7801ac20 | 310 | |
c5afc314 N |
311 | dinfo = info; |
312 | dinfo.disk.major = major(stb.st_rdev); | |
313 | dinfo.disk.minor = minor(stb.st_rdev); | |
314 | if (add_disk(mdfd, st, &info, &dinfo) != 0) { | |
8382f19b NB |
315 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", |
316 | devname, chosen_name, strerror(errno)); | |
317 | ioctl(mdfd, STOP_ARRAY, 0); | |
318 | close(mdfd); | |
319 | return 2; | |
320 | } | |
197e3eb6 | 321 | sra = sysfs_read(mdfd, fd2devnum(mdfd), GET_DEVS); |
06c7f68e | 322 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { |
8382f19b NB |
323 | /* It really should be 'none' - must be old buggy |
324 | * kernel, and mdadm -I may not be able to complete. | |
325 | * So reject it. | |
326 | */ | |
327 | ioctl(mdfd, STOP_ARRAY, NULL); | |
328 | fprintf(stderr, Name | |
329 | ": You have an old buggy kernel which cannot support\n" | |
330 | " --incremental reliably. Aborting.\n"); | |
331 | close(mdfd); | |
332 | sysfs_free(sra); | |
333 | return 2; | |
334 | } | |
c5afc314 | 335 | info.array.working_disks = 1; |
f35f2525 | 336 | sysfs_free(sra); |
ad5bc697 N |
337 | /* 6/ Make sure /var/run/mdadm.map contains this array. */ |
338 | map_update(&map, fd2devnum(mdfd), | |
339 | info.text_version, | |
340 | info.uuid, chosen_name); | |
8382f19b NB |
341 | } else { |
342 | /* 5b/ if it does */ | |
343 | /* - check one drive in array to make sure metadata is a reasonably */ | |
344 | /* close match. Reject if not (e.g. different type) */ | |
345 | /* - add the device */ | |
346 | char dn[20]; | |
347 | int dfd2; | |
8382f19b | 348 | int err; |
7e0f6979 | 349 | struct mdinfo *sra; |
3da92f27 | 350 | struct supertype *st2; |
c5afc314 | 351 | struct mdinfo info2, *d; |
215bb3f7 N |
352 | |
353 | strcpy(chosen_name, mp->path); | |
354 | ||
197e3eb6 | 355 | sra = sysfs_read(mdfd, fd2devnum(mdfd), (GET_DEVS | GET_STATE)); |
2cdb6489 | 356 | |
7c548327 N |
357 | if (sra->devs) { |
358 | sprintf(dn, "%d:%d", sra->devs->disk.major, | |
359 | sra->devs->disk.minor); | |
360 | dfd2 = dev_open(dn, O_RDONLY); | |
361 | st2 = dup_super(st); | |
362 | if (st2->ss->load_super(st2, dfd2, NULL) || | |
363 | st->ss->compare_super(st, st2) != 0) { | |
364 | fprintf(stderr, Name | |
365 | ": metadata mismatch between %s and " | |
366 | "chosen array %s\n", | |
367 | devname, chosen_name); | |
368 | close(mdfd); | |
369 | close(dfd2); | |
370 | return 2; | |
371 | } | |
8382f19b | 372 | close(dfd2); |
7c548327 N |
373 | memset(&info2, 0, sizeof(info2)); |
374 | st2->ss->getinfo_super(st2, &info2); | |
375 | st2->ss->free_super(st2); | |
376 | if (info.array.level != info2.array.level || | |
377 | memcmp(info.uuid, info2.uuid, 16) != 0 || | |
378 | info.array.raid_disks != info2.array.raid_disks) { | |
379 | fprintf(stderr, Name | |
380 | ": unexpected difference between %s and %s.\n", | |
381 | chosen_name, devname); | |
382 | close(mdfd); | |
383 | return 2; | |
384 | } | |
8382f19b | 385 | } |
7801ac20 N |
386 | info2.disk.major = major(stb.st_rdev); |
387 | info2.disk.minor = minor(stb.st_rdev); | |
c5afc314 N |
388 | /* add disk needs to know about containers */ |
389 | if (st->ss->external) | |
390 | sra->array.level = LEVEL_CONTAINER; | |
7801ac20 | 391 | err = add_disk(mdfd, st2, sra, &info2); |
8382f19b NB |
392 | if (err < 0 && errno == EBUSY) { |
393 | /* could be another device present with the same | |
394 | * disk.number. Find and reject any such | |
395 | */ | |
396 | find_reject(mdfd, st, sra, info.disk.number, | |
397 | info.events, verbose, chosen_name); | |
7801ac20 | 398 | err = add_disk(mdfd, st2, sra, &info2); |
8382f19b NB |
399 | } |
400 | if (err < 0) { | |
401 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
402 | devname, chosen_name, strerror(errno)); | |
403 | close(mdfd); | |
404 | return 2; | |
405 | } | |
c5afc314 N |
406 | info.array.working_disks = 0; |
407 | for (d = sra->devs; d; d=d->next) | |
408 | info.array.working_disks ++; | |
409 | ||
8382f19b | 410 | } |
8382f19b NB |
411 | |
412 | /* 7/ Is there enough devices to possibly start the array? */ | |
413 | /* 7a/ if not, finish with success. */ | |
352452c3 N |
414 | if (info.array.level == LEVEL_CONTAINER) { |
415 | /* Try to assemble within the container */ | |
ad5bc697 | 416 | map_unlock(&map); |
97590376 | 417 | sysfs_uevent(&info, "change"); |
c5afc314 N |
418 | if (verbose >= 0) |
419 | fprintf(stderr, Name | |
420 | ": container %s now has %d devices\n", | |
421 | chosen_name, info.array.working_disks); | |
a7c6e3fb N |
422 | wait_for(chosen_name, mdfd); |
423 | close(mdfd); | |
fdb482f9 DW |
424 | if (runstop < 0) |
425 | return 0; /* don't try to assemble */ | |
03b7f6c6 N |
426 | rv = Incremental(chosen_name, verbose, runstop, |
427 | NULL, homehost, autof); | |
428 | if (rv == 1) | |
429 | /* Don't fail the whole -I if a subarray didn't | |
430 | * have enough devices to start yet | |
431 | */ | |
432 | rv = 0; | |
433 | return rv; | |
352452c3 | 434 | } |
f8409e54 | 435 | avail = NULL; |
8382f19b NB |
436 | active_disks = count_active(st, mdfd, &avail, &info); |
437 | if (enough(info.array.level, info.array.raid_disks, | |
438 | info.array.layout, info.array.state & 1, | |
fdb482f9 DW |
439 | avail, active_disks) == 0 || |
440 | (runstop < 0 && active_disks < info.array.raid_disks)) { | |
8382f19b NB |
441 | free(avail); |
442 | if (verbose >= 0) | |
443 | fprintf(stderr, Name | |
444 | ": %s attached to %s, not enough to start (%d).\n", | |
445 | devname, chosen_name, active_disks); | |
ad5bc697 | 446 | map_unlock(&map); |
8382f19b NB |
447 | close(mdfd); |
448 | return 0; | |
449 | } | |
450 | free(avail); | |
451 | ||
452 | /* 7b/ if yes, */ | |
453 | /* - if number of OK devices match expected, or -R and there */ | |
454 | /* are enough, */ | |
455 | /* + add any bitmap file */ | |
456 | /* + start the array (auto-readonly). */ | |
8382f19b NB |
457 | |
458 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
459 | if (verbose >= 0) | |
460 | fprintf(stderr, Name | |
461 | ": %s attached to %s which is already active.\n", | |
462 | devname, chosen_name); | |
ad5bc697 N |
463 | close(mdfd); |
464 | map_unlock(&map); | |
8382f19b NB |
465 | return 0; |
466 | } | |
ad5bc697 N |
467 | |
468 | map_unlock(&map); | |
8382f19b | 469 | if (runstop > 0 || active_disks >= info.array.working_disks) { |
7e0f6979 | 470 | struct mdinfo *sra; |
8382f19b NB |
471 | /* Let's try to start it */ |
472 | if (match && match->bitmap_file) { | |
473 | int bmfd = open(match->bitmap_file, O_RDWR); | |
474 | if (bmfd < 0) { | |
475 | fprintf(stderr, Name | |
476 | ": Could not open bitmap file %s.\n", | |
477 | match->bitmap_file); | |
478 | close(mdfd); | |
479 | return 1; | |
480 | } | |
481 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
482 | close(bmfd); | |
483 | fprintf(stderr, Name | |
484 | ": Failed to set bitmapfile for %s.\n", | |
485 | chosen_name); | |
486 | close(mdfd); | |
487 | return 1; | |
488 | } | |
489 | close(bmfd); | |
490 | } | |
197e3eb6 | 491 | sra = sysfs_read(mdfd, fd2devnum(mdfd), 0); |
7b403fef | 492 | if ((sra == NULL || active_disks >= info.array.working_disks) |
215bb3f7 | 493 | && trustworthy != FOREIGN) |
8382f19b NB |
494 | rv = ioctl(mdfd, RUN_ARRAY, NULL); |
495 | else | |
496 | rv = sysfs_set_str(sra, NULL, | |
497 | "array_state", "read-auto"); | |
498 | if (rv == 0) { | |
499 | if (verbose >= 0) | |
500 | fprintf(stderr, Name | |
501 | ": %s attached to %s, which has been started.\n", | |
502 | devname, chosen_name); | |
503 | rv = 0; | |
a7c6e3fb | 504 | wait_for(chosen_name, mdfd); |
8382f19b NB |
505 | } else { |
506 | fprintf(stderr, Name | |
507 | ": %s attached to %s, but failed to start: %s.\n", | |
508 | devname, chosen_name, strerror(errno)); | |
509 | rv = 1; | |
510 | } | |
511 | } else { | |
512 | if (verbose >= 0) | |
513 | fprintf(stderr, Name | |
514 | ": %s attached to %s, not enough to start safely.\n", | |
515 | devname, chosen_name); | |
516 | rv = 0; | |
517 | } | |
518 | close(mdfd); | |
519 | return rv; | |
520 | } | |
521 | ||
7e0f6979 | 522 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
523 | int number, __u64 events, int verbose, |
524 | char *array_name) | |
525 | { | |
3da92f27 | 526 | /* Find a device attached to this array with a disk.number of number |
8382f19b NB |
527 | * and events less than the passed events, and remove the device. |
528 | */ | |
06c7f68e | 529 | struct mdinfo *d; |
8382f19b NB |
530 | mdu_array_info_t ra; |
531 | ||
532 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
533 | return; /* not safe to remove from active arrays | |
534 | * without thinking more */ | |
535 | ||
536 | for (d = sra->devs; d ; d = d->next) { | |
537 | char dn[10]; | |
538 | int dfd; | |
8382f19b | 539 | struct mdinfo info; |
06c7f68e | 540 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
541 | dfd = dev_open(dn, O_RDONLY); |
542 | if (dfd < 0) | |
543 | continue; | |
3da92f27 | 544 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
545 | close(dfd); |
546 | continue; | |
547 | } | |
3da92f27 NB |
548 | st->ss->getinfo_super(st, &info); |
549 | st->ss->free_super(st); | |
8382f19b NB |
550 | close(dfd); |
551 | ||
552 | if (info.disk.number != number || | |
553 | info.events >= events) | |
554 | continue; | |
555 | ||
06c7f68e | 556 | if (d->disk.raid_disk > -1) |
8382f19b NB |
557 | sysfs_set_str(sra, d, "slot", "none"); |
558 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
559 | if (verbose >= 0) | |
560 | fprintf(stderr, Name | |
561 | ": removing old device %s from %s\n", | |
06c7f68e | 562 | d->sys_name+4, array_name); |
8382f19b NB |
563 | } |
564 | } | |
565 | ||
566 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
567 | struct mdinfo *bestinfo) | |
568 | { | |
569 | /* count how many devices in sra think they are active */ | |
06c7f68e | 570 | struct mdinfo *d; |
8382f19b NB |
571 | int cnt = 0, cnt1 = 0; |
572 | __u64 max_events = 0; | |
7e0f6979 | 573 | struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); |
8382f19b NB |
574 | char *avail = NULL; |
575 | ||
576 | for (d = sra->devs ; d ; d = d->next) { | |
577 | char dn[30]; | |
578 | int dfd; | |
8382f19b NB |
579 | int ok; |
580 | struct mdinfo info; | |
581 | ||
06c7f68e | 582 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
583 | dfd = dev_open(dn, O_RDONLY); |
584 | if (dfd < 0) | |
585 | continue; | |
3da92f27 | 586 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
587 | close(dfd); |
588 | if (ok != 0) | |
589 | continue; | |
3da92f27 | 590 | st->ss->getinfo_super(st, &info); |
43aaf431 DL |
591 | if (!avail) { |
592 | avail = malloc(info.array.raid_disks); | |
593 | if (!avail) { | |
594 | fprintf(stderr, Name ": out of memory.\n"); | |
595 | exit(1); | |
596 | } | |
597 | memset(avail, 0, info.array.raid_disks); | |
598 | *availp = avail; | |
599 | } | |
600 | ||
8382f19b NB |
601 | if (info.disk.state & (1<<MD_DISK_SYNC)) |
602 | { | |
8382f19b NB |
603 | if (cnt == 0) { |
604 | cnt++; | |
605 | max_events = info.events; | |
606 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 607 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
608 | } else if (info.events == max_events) { |
609 | cnt++; | |
610 | avail[info.disk.raid_disk] = 2; | |
611 | } else if (info.events == max_events-1) { | |
612 | cnt1++; | |
613 | avail[info.disk.raid_disk] = 1; | |
614 | } else if (info.events < max_events - 1) | |
615 | ; | |
616 | else if (info.events == max_events+1) { | |
617 | int i; | |
618 | cnt1 = cnt; | |
619 | cnt = 1; | |
620 | max_events = info.events; | |
621 | for (i=0; i<info.array.raid_disks; i++) | |
622 | if (avail[i]) | |
623 | avail[i]--; | |
624 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 625 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
626 | } else { /* info.events much bigger */ |
627 | cnt = 1; cnt1 = 0; | |
628 | memset(avail, 0, info.disk.raid_disk); | |
629 | max_events = info.events; | |
3da92f27 | 630 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
631 | } |
632 | } | |
3da92f27 | 633 | st->ss->free_super(st); |
8382f19b NB |
634 | } |
635 | return cnt + cnt1; | |
636 | } | |
637 | ||
8382f19b NB |
638 | int IncrementalScan(int verbose) |
639 | { | |
640 | /* look at every device listed in the 'map' file. | |
641 | * If one is found that is not running then: | |
642 | * look in mdadm.conf for bitmap file. | |
643 | * if one exists, but array has none, add it. | |
644 | * try to start array in auto-readonly mode | |
645 | */ | |
646 | struct map_ent *mapl = NULL; | |
647 | struct map_ent *me; | |
648 | mddev_ident_t devs, mddev; | |
649 | int rv = 0; | |
650 | ||
651 | map_read(&mapl); | |
652 | devs = conf_get_ident(NULL); | |
653 | ||
654 | for (me = mapl ; me ; me = me->next) { | |
8382f19b NB |
655 | mdu_array_info_t array; |
656 | mdu_bitmap_file_t bmf; | |
7e0f6979 | 657 | struct mdinfo *sra; |
215bb3f7 N |
658 | int mdfd = open_mddev(me->path, 0); |
659 | ||
8382f19b NB |
660 | if (mdfd < 0) |
661 | continue; | |
662 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
663 | errno != ENODEV) { | |
664 | close(mdfd); | |
665 | continue; | |
666 | } | |
667 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
668 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
fe056d1f N |
669 | if (mddev->devname |
670 | && strcmp(mddev->devname, me->path) == 0) | |
8382f19b NB |
671 | break; |
672 | if (mddev && mddev->bitmap_file) { | |
673 | /* | |
674 | * Note: early kernels will wrongly fail this, so it | |
675 | * is a hint only | |
676 | */ | |
677 | int added = -1; | |
678 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
679 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
680 | if (bmfd >= 0) { | |
681 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
682 | bmfd); | |
683 | close(bmfd); | |
684 | } | |
685 | } | |
686 | if (verbose >= 0) { | |
687 | if (added == 0) | |
688 | fprintf(stderr, Name | |
689 | ": Added bitmap %s to %s\n", | |
690 | mddev->bitmap_file, me->path); | |
691 | else if (errno != EEXIST) | |
692 | fprintf(stderr, Name | |
693 | ": Failed to add bitmap to %s: %s\n", | |
694 | me->path, strerror(errno)); | |
695 | } | |
696 | } | |
697 | sra = sysfs_read(mdfd, 0, 0); | |
698 | if (sra) { | |
699 | if (sysfs_set_str(sra, NULL, | |
700 | "array_state", "read-auto") == 0) { | |
701 | if (verbose >= 0) | |
702 | fprintf(stderr, Name | |
703 | ": started array %s\n", | |
704 | me->path); | |
705 | } else { | |
706 | fprintf(stderr, Name | |
707 | ": failed to start array %s: %s\n", | |
708 | me->path, strerror(errno)); | |
709 | rv = 1; | |
710 | } | |
711 | } | |
712 | } | |
713 | return rv; | |
714 | } | |
598f0d58 | 715 | |
1771a6e2 N |
716 | static char *container2devname(char *devname) |
717 | { | |
718 | char *mdname = NULL; | |
719 | ||
720 | if (devname[0] == '/') { | |
721 | int fd = open(devname, O_RDONLY); | |
722 | if (fd >= 0) { | |
723 | mdname = devnum2devname(fd2devnum(fd)); | |
724 | close(fd); | |
725 | } | |
726 | } else { | |
727 | int uuid[4]; | |
728 | struct map_ent *mp, *map = NULL; | |
729 | ||
730 | if (!parse_uuid(devname, uuid)) | |
731 | return mdname; | |
732 | mp = map_by_uuid(&map, uuid); | |
733 | if (mp) | |
734 | mdname = devnum2devname(mp->devnum); | |
735 | map_free(map); | |
736 | } | |
737 | ||
738 | return mdname; | |
739 | } | |
740 | ||
598f0d58 | 741 | int Incremental_container(struct supertype *st, char *devname, int verbose, |
215bb3f7 | 742 | int runstop, int autof, int trustworthy) |
598f0d58 NB |
743 | { |
744 | /* Collect the contents of this container and for each | |
745 | * array, choose a device name and assemble the array. | |
746 | */ | |
747 | ||
748 | struct mdinfo *list = st->ss->container_content(st); | |
749 | struct mdinfo *ra; | |
ad5bc697 N |
750 | struct map_ent *map = NULL; |
751 | ||
752 | map_lock(&map); | |
598f0d58 NB |
753 | |
754 | for (ra = list ; ra ; ra = ra->next) { | |
598f0d58 NB |
755 | int mdfd; |
756 | char chosen_name[1024]; | |
ad5bc697 | 757 | struct map_ent *mp; |
dbb44303 | 758 | struct mddev_ident_s *match = NULL; |
598f0d58 | 759 | |
c5afc314 | 760 | mp = map_by_uuid(&map, ra->uuid); |
d7288ddc | 761 | |
30926600 N |
762 | if (mp) { |
763 | mdfd = open_dev(mp->devnum); | |
764 | strcpy(chosen_name, mp->path); | |
765 | } else { | |
dbb44303 | 766 | |
112cace6 | 767 | /* Check in mdadm.conf for container == devname and |
dbb44303 N |
768 | * member == ra->text_version after second slash. |
769 | */ | |
770 | char *sub = strchr(ra->text_version+1, '/'); | |
771 | struct mddev_ident_s *array_list; | |
772 | if (sub) { | |
773 | sub++; | |
774 | array_list = conf_get_ident(NULL); | |
775 | } else | |
776 | array_list = NULL; | |
777 | for(; array_list ; array_list = array_list->next) { | |
dbb44303 N |
778 | char *dn; |
779 | if (array_list->member == NULL || | |
780 | array_list->container == NULL) | |
781 | continue; | |
782 | if (strcmp(array_list->member, sub) != 0) | |
783 | continue; | |
71d60c48 DW |
784 | if (array_list->uuid_set && |
785 | !same_uuid(ra->uuid, array_list->uuid, st->ss->swapuuid)) | |
786 | continue; | |
1771a6e2 N |
787 | dn = container2devname(array_list->container); |
788 | if (dn == NULL) | |
dbb44303 | 789 | continue; |
dbb44303 N |
790 | if (strncmp(dn, ra->text_version+1, |
791 | strlen(dn)) != 0 || | |
792 | ra->text_version[strlen(dn)+1] != '/') { | |
793 | free(dn); | |
794 | continue; | |
795 | } | |
796 | free(dn); | |
797 | /* we have a match */ | |
798 | match = array_list; | |
71d60c48 DW |
799 | if (verbose>0) |
800 | fprintf(stderr, Name ": match found for member %s\n", | |
801 | array_list->member); | |
dbb44303 N |
802 | break; |
803 | } | |
dbb44303 | 804 | |
112cace6 N |
805 | if (match && match->devname && |
806 | strcasecmp(match->devname, "<ignore>") == 0) { | |
807 | if (verbose > 0) | |
808 | fprintf(stderr, Name ": array %s/%s is " | |
809 | "explicitly ignored by mdadm.conf\n", | |
810 | match->container, match->member); | |
811 | return 2; | |
812 | } | |
813 | ||
30926600 N |
814 | mdfd = create_mddev(match ? match->devname : NULL, |
815 | ra->name, | |
816 | autof, | |
817 | trustworthy, | |
818 | chosen_name); | |
819 | } | |
598f0d58 NB |
820 | |
821 | if (mdfd < 0) { | |
822 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
823 | chosen_name, strerror(errno)); | |
824 | return 2; | |
825 | } | |
826 | ||
03b7f6c6 N |
827 | assemble_container_content(st, mdfd, ra, runstop, |
828 | chosen_name, verbose); | |
598f0d58 | 829 | } |
ad5bc697 | 830 | map_unlock(&map); |
598f0d58 NB |
831 | return 0; |
832 | } |