]>
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 | |
51 | * 3a/ if not, check for homehost match. If no match, reject. | |
52 | * 4/ Determine device number. | |
53 | * - If in mdadm.conf with std name, use that | |
54 | * - UUID in /var/run/mdadm.map use that | |
55 | * - If name is suggestive, use that. unless in use with different uuid. | |
56 | * - Choose a free, high number. | |
57 | * - Use a partitioned device unless strong suggestion not to. | |
58 | * e.g. auto=md | |
352452c3 | 59 | * Don't choose partitioned for containers. |
8382f19b NB |
60 | * 5/ Find out if array already exists |
61 | * 5a/ if it does not | |
62 | * - choose a name, from mdadm.conf or 'name' field in array. | |
63 | * - create the array | |
64 | * - add the device | |
65 | * 5b/ if it does | |
66 | * - check one drive in array to make sure metadata is a reasonably | |
67 | * close match. Reject if not (e.g. different type) | |
68 | * - add the device | |
69 | * 6/ Make sure /var/run/mdadm.map contains this array. | |
70 | * 7/ Is there enough devices to possibly start the array? | |
352452c3 | 71 | * For a container, this means running Incremental_container. |
8382f19b NB |
72 | * 7a/ if not, finish with success. |
73 | * 7b/ if yes, | |
74 | * - read all metadata and arrange devices like -A does | |
75 | * - if number of OK devices match expected, or -R and there are enough, | |
76 | * start the array (auto-readonly). | |
77 | */ | |
78 | struct stat stb; | |
f35f2525 | 79 | struct mdinfo info; |
8382f19b NB |
80 | struct mddev_ident_s *array_list, *match; |
81 | char chosen_name[1024]; | |
82 | int rv; | |
83 | int devnum; | |
84 | struct map_ent *mp, *map = NULL; | |
85 | int dfd, mdfd; | |
86 | char *avail; | |
87 | int active_disks; | |
d7288ddc N |
88 | int uuid_for_name = 0; |
89 | char *name_to_use; | |
90 | char nbuf[64]; | |
8382f19b NB |
91 | |
92 | struct createinfo *ci = conf_get_create_info(); | |
93 | ||
94 | if (autof == 0) | |
95 | autof = ci->autof; | |
96 | ||
352452c3 | 97 | /* 1/ Check if device is permitted by mdadm.conf */ |
8382f19b NB |
98 | |
99 | if (!conf_test_dev(devname)) { | |
100 | if (verbose >= 0) | |
101 | fprintf(stderr, Name | |
102 | ": %s not permitted by mdadm.conf.\n", | |
103 | devname); | |
104 | return 1; | |
105 | } | |
106 | ||
107 | /* 2/ Find metadata, reject if none appropriate (check | |
108 | * version/name from args) */ | |
109 | ||
110 | dfd = dev_open(devname, O_RDONLY|O_EXCL); | |
111 | if (dfd < 0) { | |
112 | if (verbose >= 0) | |
113 | fprintf(stderr, Name ": cannot open %s: %s.\n", | |
114 | devname, strerror(errno)); | |
115 | return 1; | |
116 | } | |
117 | if (fstat(dfd, &stb) < 0) { | |
118 | if (verbose >= 0) | |
119 | fprintf(stderr, Name ": fstat failed for %s: %s.\n", | |
120 | devname, strerror(errno)); | |
121 | close(dfd); | |
122 | return 1; | |
123 | } | |
124 | if ((stb.st_mode & S_IFMT) != S_IFBLK) { | |
125 | if (verbose >= 0) | |
126 | fprintf(stderr, Name ": %s is not a block device.\n", | |
127 | devname); | |
128 | close(dfd); | |
129 | return 1; | |
130 | } | |
131 | ||
132 | if (st == NULL && (st = guess_super(dfd)) == NULL) { | |
133 | if (verbose >= 0) | |
134 | fprintf(stderr, Name | |
135 | ": no recognisable superblock on %s.\n", | |
136 | devname); | |
137 | close(dfd); | |
138 | return 1; | |
139 | } | |
3da92f27 | 140 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
141 | if (verbose >= 0) |
142 | fprintf(stderr, Name ": no RAID superblock on %s.\n", | |
143 | devname); | |
144 | close(dfd); | |
145 | return 1; | |
146 | } | |
8382f19b NB |
147 | close (dfd); |
148 | ||
352452c3 | 149 | if (st->ss->container_content && st->loaded_container) { |
598f0d58 NB |
150 | /* This is a pre-built container array, so we do something |
151 | * rather different. | |
152 | */ | |
153 | return Incremental_container(st, devname, verbose, runstop, | |
154 | autof); | |
155 | } | |
156 | ||
f35f2525 | 157 | memset(&info, 0, sizeof(info)); |
598f0d58 | 158 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
159 | /* 3/ Check if there is a match in mdadm.conf */ |
160 | ||
161 | array_list = conf_get_ident(NULL); | |
162 | match = NULL; | |
163 | for (; array_list; array_list = array_list->next) { | |
164 | if (array_list->uuid_set && | |
165 | same_uuid(array_list->uuid, info.uuid, st->ss->swapuuid) | |
166 | == 0) { | |
167 | if (verbose >= 2) | |
168 | fprintf(stderr, Name | |
169 | ": UUID differs from %s.\n", | |
170 | array_list->devname); | |
171 | continue; | |
172 | } | |
173 | if (array_list->name[0] && | |
174 | strcasecmp(array_list->name, info.name) != 0) { | |
175 | if (verbose >= 2) | |
176 | fprintf(stderr, Name | |
177 | ": Name differs from %s.\n", | |
178 | array_list->devname); | |
179 | continue; | |
180 | } | |
181 | if (array_list->devices && | |
182 | !match_oneof(array_list->devices, devname)) { | |
183 | if (verbose >= 2) | |
184 | fprintf(stderr, Name | |
185 | ": Not a listed device for %s.\n", | |
186 | array_list->devname); | |
187 | continue; | |
188 | } | |
189 | if (array_list->super_minor != UnSet && | |
190 | array_list->super_minor != info.array.md_minor) { | |
191 | if (verbose >= 2) | |
192 | fprintf(stderr, Name | |
193 | ": Different super-minor to %s.\n", | |
194 | array_list->devname); | |
195 | continue; | |
196 | } | |
197 | if (!array_list->uuid_set && | |
198 | !array_list->name[0] && | |
199 | !array_list->devices && | |
200 | array_list->super_minor == UnSet) { | |
201 | if (verbose >= 2) | |
202 | fprintf(stderr, Name | |
203 | ": %s doesn't have any identifying information.\n", | |
204 | array_list->devname); | |
205 | continue; | |
206 | } | |
207 | /* FIXME, should I check raid_disks and level too?? */ | |
208 | ||
209 | if (match) { | |
210 | if (verbose >= 0) | |
211 | fprintf(stderr, Name | |
212 | ": we match both %s and %s - cannot decide which to use.\n", | |
213 | match->devname, array_list->devname); | |
214 | return 2; | |
215 | } | |
216 | match = array_list; | |
217 | } | |
218 | ||
219 | /* 3a/ if not, check for homehost match. If no match, reject. */ | |
220 | if (!match) { | |
221 | if (homehost == NULL || | |
d7288ddc N |
222 | st->ss->match_home(st, homehost) != 1) |
223 | uuid_for_name = 1; | |
8382f19b NB |
224 | } |
225 | /* 4/ Determine device number. */ | |
226 | /* - If in mdadm.conf with std name, use that */ | |
227 | /* - UUID in /var/run/mdadm.map use that */ | |
228 | /* - If name is suggestive, use that. unless in use with */ | |
229 | /* different uuid. */ | |
230 | /* - Choose a free, high number. */ | |
231 | /* - Use a partitioned device unless strong suggestion not to. */ | |
232 | /* e.g. auto=md */ | |
d7288ddc N |
233 | mp = map_by_uuid(&map, info.uuid); |
234 | ||
235 | if (uuid_for_name && ! mp) { | |
ff54de6e | 236 | name_to_use = fname_from_uuid(st, &info, nbuf, '-'); |
d7288ddc N |
237 | if (verbose >= 0) |
238 | fprintf(stderr, Name | |
239 | ": not found in mdadm.conf and not identified by homehost" | |
240 | " - using uuid based name\n"); | |
241 | } else | |
242 | name_to_use = info.name; | |
243 | ||
8382f19b NB |
244 | if (match && is_standard(match->devname, &devnum)) |
245 | /* We have devnum now */; | |
d7288ddc | 246 | else if (mp != NULL) |
8382f19b NB |
247 | devnum = mp->devnum; |
248 | else { | |
249 | /* Have to guess a bit. */ | |
250 | int use_partitions = 1; | |
251 | char *np, *ep; | |
25dbe93a | 252 | char *nm, nbuf[1024]; |
c5afc314 N |
253 | struct stat stb2; |
254 | ||
8382f19b NB |
255 | if ((autof&7) == 3 || (autof&7) == 5) |
256 | use_partitions = 0; | |
352452c3 N |
257 | if (st->ss->external) |
258 | use_partitions = 0; | |
d7288ddc | 259 | np = strchr(name_to_use, ':'); |
8382f19b NB |
260 | if (np) |
261 | np++; | |
262 | else | |
d7288ddc | 263 | np = name_to_use; |
8382f19b NB |
264 | devnum = strtoul(np, &ep, 10); |
265 | if (ep > np && *ep == 0) { | |
266 | /* This is a number. Let check that it is unused. */ | |
267 | if (mddev_busy(use_partitions ? (-1-devnum) : devnum)) | |
268 | devnum = -1; | |
269 | } else | |
270 | devnum = -1; | |
271 | ||
25dbe93a NB |
272 | if (match) |
273 | nm = match->devname; | |
274 | else { | |
275 | sprintf(nbuf, "/dev/md/%s", np); | |
276 | nm = nbuf; | |
277 | } | |
c5afc314 N |
278 | if (stat(nm, &stb2) == 0 && |
279 | S_ISBLK(stb2.st_mode) && | |
280 | major(stb2.st_rdev) == (use_partitions ? | |
281 | get_mdp_major() : MD_MAJOR)) { | |
25dbe93a | 282 | if (use_partitions) |
c5afc314 | 283 | devnum = minor(stb2.st_rdev) >> MdpMinorShift; |
25dbe93a | 284 | else |
c5afc314 | 285 | devnum = minor(stb2.st_rdev); |
25dbe93a NB |
286 | if (mddev_busy(use_partitions ? (-1-devnum) : devnum)) |
287 | devnum = -1; | |
288 | } | |
289 | ||
8382f19b NB |
290 | if (devnum < 0) { |
291 | /* Haven't found anything yet, choose something free */ | |
63152c1b NB |
292 | devnum = find_free_devnum(use_partitions); |
293 | ||
294 | if (devnum == NoMdDev) { | |
8382f19b NB |
295 | fprintf(stderr, Name |
296 | ": No spare md devices!!\n"); | |
297 | return 2; | |
298 | } | |
63152c1b NB |
299 | } else |
300 | devnum = use_partitions ? (-1-devnum) : devnum; | |
8382f19b | 301 | } |
d7288ddc N |
302 | |
303 | mdfd = open_mddev_devnum(match ? match->devname : mp ? mp->path : NULL, | |
8382f19b | 304 | devnum, |
d7288ddc | 305 | name_to_use, |
9a02c62a | 306 | chosen_name, autof >> 3); |
8382f19b NB |
307 | if (mdfd < 0) { |
308 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
309 | chosen_name, strerror(errno)); | |
310 | return 2; | |
311 | } | |
f35f2525 N |
312 | sysfs_init(&info, mdfd, 0); |
313 | ||
8382f19b NB |
314 | /* 5/ Find out if array already exists */ |
315 | if (! mddev_busy(devnum)) { | |
316 | /* 5a/ if it does not */ | |
317 | /* - choose a name, from mdadm.conf or 'name' field in array. */ | |
318 | /* - create the array */ | |
319 | /* - add the device */ | |
7e0f6979 | 320 | struct mdinfo *sra; |
c5afc314 | 321 | struct mdinfo dinfo; |
8382f19b | 322 | |
f35f2525 N |
323 | if (set_array_info(mdfd, st, &info) != 0) { |
324 | fprintf(stderr, Name ": failed to set array info for %s: %s\n", | |
8382f19b NB |
325 | chosen_name, strerror(errno)); |
326 | close(mdfd); | |
327 | return 2; | |
328 | } | |
7801ac20 | 329 | |
c5afc314 N |
330 | dinfo = info; |
331 | dinfo.disk.major = major(stb.st_rdev); | |
332 | dinfo.disk.minor = minor(stb.st_rdev); | |
333 | if (add_disk(mdfd, st, &info, &dinfo) != 0) { | |
8382f19b NB |
334 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", |
335 | devname, chosen_name, strerror(errno)); | |
336 | ioctl(mdfd, STOP_ARRAY, 0); | |
337 | close(mdfd); | |
338 | return 2; | |
339 | } | |
340 | sra = sysfs_read(mdfd, devnum, GET_DEVS); | |
06c7f68e | 341 | if (!sra || !sra->devs || sra->devs->disk.raid_disk >= 0) { |
8382f19b NB |
342 | /* It really should be 'none' - must be old buggy |
343 | * kernel, and mdadm -I may not be able to complete. | |
344 | * So reject it. | |
345 | */ | |
346 | ioctl(mdfd, STOP_ARRAY, NULL); | |
347 | fprintf(stderr, Name | |
348 | ": You have an old buggy kernel which cannot support\n" | |
349 | " --incremental reliably. Aborting.\n"); | |
350 | close(mdfd); | |
351 | sysfs_free(sra); | |
352 | return 2; | |
353 | } | |
c5afc314 | 354 | info.array.working_disks = 1; |
f35f2525 | 355 | sysfs_free(sra); |
8382f19b NB |
356 | } else { |
357 | /* 5b/ if it does */ | |
358 | /* - check one drive in array to make sure metadata is a reasonably */ | |
359 | /* close match. Reject if not (e.g. different type) */ | |
360 | /* - add the device */ | |
361 | char dn[20]; | |
362 | int dfd2; | |
8382f19b | 363 | int err; |
7e0f6979 | 364 | struct mdinfo *sra; |
3da92f27 | 365 | struct supertype *st2; |
c5afc314 | 366 | struct mdinfo info2, *d; |
b8ac1967 | 367 | sra = sysfs_read(mdfd, devnum, (GET_DEVS | GET_STATE)); |
2cdb6489 | 368 | |
06c7f68e NB |
369 | sprintf(dn, "%d:%d", sra->devs->disk.major, |
370 | sra->devs->disk.minor); | |
8382f19b | 371 | dfd2 = dev_open(dn, O_RDONLY); |
3da92f27 | 372 | st2 = dup_super(st); |
b8ac1967 NB |
373 | if (st2->ss->load_super(st2, dfd2, NULL) || |
374 | st->ss->compare_super(st, st2) != 0) { | |
8382f19b | 375 | fprintf(stderr, Name |
b8ac1967 NB |
376 | ": metadata mismatch between %s and " |
377 | "chosen array %s\n", | |
378 | devname, chosen_name); | |
8382f19b NB |
379 | close(mdfd); |
380 | close(dfd2); | |
381 | return 2; | |
382 | } | |
383 | close(dfd2); | |
f35f2525 | 384 | memset(&info2, 0, sizeof(info2)); |
3da92f27 NB |
385 | st2->ss->getinfo_super(st2, &info2); |
386 | st2->ss->free_super(st2); | |
8382f19b NB |
387 | if (info.array.level != info2.array.level || |
388 | memcmp(info.uuid, info2.uuid, 16) != 0 || | |
389 | info.array.raid_disks != info2.array.raid_disks) { | |
390 | fprintf(stderr, Name | |
391 | ": unexpected difference between %s and %s.\n", | |
392 | chosen_name, devname); | |
393 | close(mdfd); | |
394 | return 2; | |
395 | } | |
7801ac20 N |
396 | info2.disk.major = major(stb.st_rdev); |
397 | info2.disk.minor = minor(stb.st_rdev); | |
c5afc314 N |
398 | /* add disk needs to know about containers */ |
399 | if (st->ss->external) | |
400 | sra->array.level = LEVEL_CONTAINER; | |
7801ac20 | 401 | err = add_disk(mdfd, st2, sra, &info2); |
8382f19b NB |
402 | if (err < 0 && errno == EBUSY) { |
403 | /* could be another device present with the same | |
404 | * disk.number. Find and reject any such | |
405 | */ | |
406 | find_reject(mdfd, st, sra, info.disk.number, | |
407 | info.events, verbose, chosen_name); | |
7801ac20 | 408 | err = add_disk(mdfd, st2, sra, &info2); |
8382f19b NB |
409 | } |
410 | if (err < 0) { | |
411 | fprintf(stderr, Name ": failed to add %s to %s: %s.\n", | |
412 | devname, chosen_name, strerror(errno)); | |
413 | close(mdfd); | |
414 | return 2; | |
415 | } | |
c5afc314 N |
416 | info.array.working_disks = 0; |
417 | for (d = sra->devs; d; d=d->next) | |
418 | info.array.working_disks ++; | |
419 | ||
8382f19b NB |
420 | } |
421 | /* 6/ Make sure /var/run/mdadm.map contains this array. */ | |
422 | map_update(&map, devnum, | |
1522c538 | 423 | info.text_version, |
8382f19b NB |
424 | info.uuid, chosen_name); |
425 | ||
426 | /* 7/ Is there enough devices to possibly start the array? */ | |
427 | /* 7a/ if not, finish with success. */ | |
352452c3 N |
428 | if (info.array.level == LEVEL_CONTAINER) { |
429 | /* Try to assemble within the container */ | |
c5afc314 N |
430 | close(mdfd); |
431 | if (verbose >= 0) | |
432 | fprintf(stderr, Name | |
433 | ": container %s now has %d devices\n", | |
434 | chosen_name, info.array.working_disks); | |
352452c3 N |
435 | return Incremental(chosen_name, verbose, runstop, |
436 | NULL, homehost, autof); | |
437 | } | |
f8409e54 | 438 | avail = NULL; |
8382f19b NB |
439 | active_disks = count_active(st, mdfd, &avail, &info); |
440 | if (enough(info.array.level, info.array.raid_disks, | |
441 | info.array.layout, info.array.state & 1, | |
442 | avail, active_disks) == 0) { | |
443 | free(avail); | |
444 | if (verbose >= 0) | |
445 | fprintf(stderr, Name | |
446 | ": %s attached to %s, not enough to start (%d).\n", | |
447 | devname, chosen_name, active_disks); | |
448 | close(mdfd); | |
449 | return 0; | |
450 | } | |
451 | free(avail); | |
452 | ||
453 | /* 7b/ if yes, */ | |
454 | /* - if number of OK devices match expected, or -R and there */ | |
455 | /* are enough, */ | |
456 | /* + add any bitmap file */ | |
457 | /* + start the array (auto-readonly). */ | |
458 | { | |
459 | mdu_array_info_t ainf; | |
460 | ||
461 | if (ioctl(mdfd, GET_ARRAY_INFO, &ainf) == 0) { | |
462 | if (verbose >= 0) | |
463 | fprintf(stderr, Name | |
464 | ": %s attached to %s which is already active.\n", | |
465 | devname, chosen_name); | |
466 | close (mdfd); | |
467 | return 0; | |
468 | } | |
469 | } | |
470 | if (runstop > 0 || active_disks >= info.array.working_disks) { | |
7e0f6979 | 471 | struct mdinfo *sra; |
8382f19b NB |
472 | /* Let's try to start it */ |
473 | if (match && match->bitmap_file) { | |
474 | int bmfd = open(match->bitmap_file, O_RDWR); | |
475 | if (bmfd < 0) { | |
476 | fprintf(stderr, Name | |
477 | ": Could not open bitmap file %s.\n", | |
478 | match->bitmap_file); | |
479 | close(mdfd); | |
480 | return 1; | |
481 | } | |
482 | if (ioctl(mdfd, SET_BITMAP_FILE, bmfd) != 0) { | |
483 | close(bmfd); | |
484 | fprintf(stderr, Name | |
485 | ": Failed to set bitmapfile for %s.\n", | |
486 | chosen_name); | |
487 | close(mdfd); | |
488 | return 1; | |
489 | } | |
490 | close(bmfd); | |
491 | } | |
492 | sra = sysfs_read(mdfd, devnum, 0); | |
493 | if (sra == NULL || active_disks >= info.array.working_disks) | |
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; | |
504 | } else { | |
505 | fprintf(stderr, Name | |
506 | ": %s attached to %s, but failed to start: %s.\n", | |
507 | devname, chosen_name, strerror(errno)); | |
508 | rv = 1; | |
509 | } | |
510 | } else { | |
511 | if (verbose >= 0) | |
512 | fprintf(stderr, Name | |
513 | ": %s attached to %s, not enough to start safely.\n", | |
514 | devname, chosen_name); | |
515 | rv = 0; | |
516 | } | |
517 | close(mdfd); | |
518 | return rv; | |
519 | } | |
520 | ||
7e0f6979 | 521 | static void find_reject(int mdfd, struct supertype *st, struct mdinfo *sra, |
8382f19b NB |
522 | int number, __u64 events, int verbose, |
523 | char *array_name) | |
524 | { | |
3da92f27 | 525 | /* Find a device attached to this array with a disk.number of number |
8382f19b NB |
526 | * and events less than the passed events, and remove the device. |
527 | */ | |
06c7f68e | 528 | struct mdinfo *d; |
8382f19b NB |
529 | mdu_array_info_t ra; |
530 | ||
531 | if (ioctl(mdfd, GET_ARRAY_INFO, &ra) == 0) | |
532 | return; /* not safe to remove from active arrays | |
533 | * without thinking more */ | |
534 | ||
535 | for (d = sra->devs; d ; d = d->next) { | |
536 | char dn[10]; | |
537 | int dfd; | |
8382f19b | 538 | struct mdinfo info; |
06c7f68e | 539 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
540 | dfd = dev_open(dn, O_RDONLY); |
541 | if (dfd < 0) | |
542 | continue; | |
3da92f27 | 543 | if (st->ss->load_super(st, dfd, NULL)) { |
8382f19b NB |
544 | close(dfd); |
545 | continue; | |
546 | } | |
3da92f27 NB |
547 | st->ss->getinfo_super(st, &info); |
548 | st->ss->free_super(st); | |
8382f19b NB |
549 | close(dfd); |
550 | ||
551 | if (info.disk.number != number || | |
552 | info.events >= events) | |
553 | continue; | |
554 | ||
06c7f68e | 555 | if (d->disk.raid_disk > -1) |
8382f19b NB |
556 | sysfs_set_str(sra, d, "slot", "none"); |
557 | if (sysfs_set_str(sra, d, "state", "remove") == 0) | |
558 | if (verbose >= 0) | |
559 | fprintf(stderr, Name | |
560 | ": removing old device %s from %s\n", | |
06c7f68e | 561 | d->sys_name+4, array_name); |
8382f19b NB |
562 | } |
563 | } | |
564 | ||
565 | static int count_active(struct supertype *st, int mdfd, char **availp, | |
566 | struct mdinfo *bestinfo) | |
567 | { | |
568 | /* count how many devices in sra think they are active */ | |
06c7f68e | 569 | struct mdinfo *d; |
8382f19b NB |
570 | int cnt = 0, cnt1 = 0; |
571 | __u64 max_events = 0; | |
7e0f6979 | 572 | struct mdinfo *sra = sysfs_read(mdfd, -1, GET_DEVS | GET_STATE); |
8382f19b NB |
573 | char *avail = NULL; |
574 | ||
575 | for (d = sra->devs ; d ; d = d->next) { | |
576 | char dn[30]; | |
577 | int dfd; | |
8382f19b NB |
578 | int ok; |
579 | struct mdinfo info; | |
580 | ||
06c7f68e | 581 | sprintf(dn, "%d:%d", d->disk.major, d->disk.minor); |
8382f19b NB |
582 | dfd = dev_open(dn, O_RDONLY); |
583 | if (dfd < 0) | |
584 | continue; | |
3da92f27 | 585 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
586 | close(dfd); |
587 | if (ok != 0) | |
588 | continue; | |
3da92f27 | 589 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
590 | if (info.disk.state & (1<<MD_DISK_SYNC)) |
591 | { | |
592 | if (avail == NULL) { | |
593 | avail = malloc(info.array.raid_disks); | |
594 | memset(avail, 0, info.array.raid_disks); | |
595 | } | |
596 | if (cnt == 0) { | |
597 | cnt++; | |
598 | max_events = info.events; | |
599 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 600 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
601 | } else if (info.events == max_events) { |
602 | cnt++; | |
603 | avail[info.disk.raid_disk] = 2; | |
604 | } else if (info.events == max_events-1) { | |
605 | cnt1++; | |
606 | avail[info.disk.raid_disk] = 1; | |
607 | } else if (info.events < max_events - 1) | |
608 | ; | |
609 | else if (info.events == max_events+1) { | |
610 | int i; | |
611 | cnt1 = cnt; | |
612 | cnt = 1; | |
613 | max_events = info.events; | |
614 | for (i=0; i<info.array.raid_disks; i++) | |
615 | if (avail[i]) | |
616 | avail[i]--; | |
617 | avail[info.disk.raid_disk] = 2; | |
3da92f27 | 618 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
619 | } else { /* info.events much bigger */ |
620 | cnt = 1; cnt1 = 0; | |
621 | memset(avail, 0, info.disk.raid_disk); | |
622 | max_events = info.events; | |
3da92f27 | 623 | st->ss->getinfo_super(st, bestinfo); |
8382f19b NB |
624 | } |
625 | } | |
3da92f27 | 626 | st->ss->free_super(st); |
8382f19b NB |
627 | } |
628 | return cnt + cnt1; | |
629 | } | |
630 | ||
631 | void RebuildMap(void) | |
632 | { | |
633 | struct mdstat_ent *mdstat = mdstat_read(0, 0); | |
634 | struct mdstat_ent *md; | |
635 | struct map_ent *map = NULL; | |
636 | int mdp = get_mdp_major(); | |
637 | ||
638 | for (md = mdstat ; md ; md = md->next) { | |
7e0f6979 | 639 | struct mdinfo *sra = sysfs_read(-1, md->devnum, GET_DEVS); |
06c7f68e | 640 | struct mdinfo *sd; |
8382f19b NB |
641 | |
642 | for (sd = sra->devs ; sd ; sd = sd->next) { | |
643 | char dn[30]; | |
644 | int dfd; | |
645 | int ok; | |
646 | struct supertype *st; | |
647 | char *path; | |
8382f19b NB |
648 | struct mdinfo info; |
649 | ||
06c7f68e | 650 | sprintf(dn, "%d:%d", sd->disk.major, sd->disk.minor); |
8382f19b NB |
651 | dfd = dev_open(dn, O_RDONLY); |
652 | if (dfd < 0) | |
653 | continue; | |
654 | st = guess_super(dfd); | |
655 | if ( st == NULL) | |
656 | ok = -1; | |
657 | else | |
3da92f27 | 658 | ok = st->ss->load_super(st, dfd, NULL); |
8382f19b NB |
659 | close(dfd); |
660 | if (ok != 0) | |
661 | continue; | |
3da92f27 | 662 | st->ss->getinfo_super(st, &info); |
8382f19b NB |
663 | if (md->devnum > 0) |
664 | path = map_dev(MD_MAJOR, md->devnum, 0); | |
665 | else | |
666 | path = map_dev(mdp, (-1-md->devnum)<< 6, 0); | |
1522c538 NB |
667 | map_add(&map, md->devnum, |
668 | info.text_version, | |
8382f19b | 669 | info.uuid, path ? : "/unknown"); |
3da92f27 | 670 | st->ss->free_super(st); |
8382f19b NB |
671 | break; |
672 | } | |
673 | } | |
674 | map_write(map); | |
675 | map_free(map); | |
676 | } | |
677 | ||
678 | int IncrementalScan(int verbose) | |
679 | { | |
680 | /* look at every device listed in the 'map' file. | |
681 | * If one is found that is not running then: | |
682 | * look in mdadm.conf for bitmap file. | |
683 | * if one exists, but array has none, add it. | |
684 | * try to start array in auto-readonly mode | |
685 | */ | |
686 | struct map_ent *mapl = NULL; | |
687 | struct map_ent *me; | |
688 | mddev_ident_t devs, mddev; | |
689 | int rv = 0; | |
690 | ||
691 | map_read(&mapl); | |
692 | devs = conf_get_ident(NULL); | |
693 | ||
694 | for (me = mapl ; me ; me = me->next) { | |
695 | char path[1024]; | |
696 | mdu_array_info_t array; | |
697 | mdu_bitmap_file_t bmf; | |
7e0f6979 | 698 | struct mdinfo *sra; |
9a02c62a NB |
699 | int mdfd = open_mddev_devnum(me->path, me->devnum, |
700 | NULL, path, 0); | |
8382f19b NB |
701 | if (mdfd < 0) |
702 | continue; | |
703 | if (ioctl(mdfd, GET_ARRAY_INFO, &array) == 0 || | |
704 | errno != ENODEV) { | |
705 | close(mdfd); | |
706 | continue; | |
707 | } | |
708 | /* Ok, we can try this one. Maybe it needs a bitmap */ | |
709 | for (mddev = devs ; mddev ; mddev = mddev->next) | |
710 | if (strcmp(mddev->devname, me->path) == 0) | |
711 | break; | |
712 | if (mddev && mddev->bitmap_file) { | |
713 | /* | |
714 | * Note: early kernels will wrongly fail this, so it | |
715 | * is a hint only | |
716 | */ | |
717 | int added = -1; | |
718 | if (ioctl(mdfd, GET_ARRAY_INFO, &bmf) < 0) { | |
719 | int bmfd = open(mddev->bitmap_file, O_RDWR); | |
720 | if (bmfd >= 0) { | |
721 | added = ioctl(mdfd, SET_BITMAP_FILE, | |
722 | bmfd); | |
723 | close(bmfd); | |
724 | } | |
725 | } | |
726 | if (verbose >= 0) { | |
727 | if (added == 0) | |
728 | fprintf(stderr, Name | |
729 | ": Added bitmap %s to %s\n", | |
730 | mddev->bitmap_file, me->path); | |
731 | else if (errno != EEXIST) | |
732 | fprintf(stderr, Name | |
733 | ": Failed to add bitmap to %s: %s\n", | |
734 | me->path, strerror(errno)); | |
735 | } | |
736 | } | |
737 | sra = sysfs_read(mdfd, 0, 0); | |
738 | if (sra) { | |
739 | if (sysfs_set_str(sra, NULL, | |
740 | "array_state", "read-auto") == 0) { | |
741 | if (verbose >= 0) | |
742 | fprintf(stderr, Name | |
743 | ": started array %s\n", | |
744 | me->path); | |
745 | } else { | |
746 | fprintf(stderr, Name | |
747 | ": failed to start array %s: %s\n", | |
748 | me->path, strerror(errno)); | |
749 | rv = 1; | |
750 | } | |
751 | } | |
752 | } | |
753 | return rv; | |
754 | } | |
598f0d58 | 755 | |
5f2aace8 DW |
756 | static char *container2devname(char *devname) |
757 | { | |
758 | int fd = open(devname, O_RDONLY); | |
759 | char *mdname = NULL; | |
760 | ||
d7161f3b | 761 | if (fd >= 0) { |
5f2aace8 DW |
762 | mdname = devnum2devname(fd2devnum(fd)); |
763 | close(fd); | |
764 | } | |
765 | ||
766 | return mdname; | |
767 | } | |
768 | ||
598f0d58 NB |
769 | int Incremental_container(struct supertype *st, char *devname, int verbose, |
770 | int runstop, int autof) | |
771 | { | |
772 | /* Collect the contents of this container and for each | |
773 | * array, choose a device name and assemble the array. | |
774 | */ | |
775 | ||
776 | struct mdinfo *list = st->ss->container_content(st); | |
777 | struct mdinfo *ra; | |
5f2aace8 DW |
778 | char *mdname = container2devname(devname); |
779 | ||
780 | if (!mdname) { | |
781 | fprintf(stderr, Name": failed to determine device name\n"); | |
782 | return 2; | |
783 | } | |
598f0d58 NB |
784 | |
785 | for (ra = list ; ra ; ra = ra->next) { | |
04c0634e | 786 | struct mdinfo *dev, *sra; |
598f0d58 NB |
787 | int devnum = -1; |
788 | int mdfd; | |
789 | char chosen_name[1024]; | |
790 | int usepart = 1; | |
791 | char *n; | |
c5afc314 N |
792 | int working = 0, preexist = 0; |
793 | struct map_ent *mp, *map = NULL; | |
d7288ddc N |
794 | char nbuf[64]; |
795 | char *name_to_use; | |
dbb44303 | 796 | struct mddev_ident_s *match = NULL; |
598f0d58 NB |
797 | |
798 | if ((autof&7) == 3 || (autof&7) == 5) | |
799 | usepart = 0; | |
800 | ||
c5afc314 | 801 | mp = map_by_uuid(&map, ra->uuid); |
d7288ddc N |
802 | |
803 | name_to_use = ra->name; | |
804 | if (! name_to_use || | |
805 | ! *name_to_use || | |
806 | (*devname != '/' || strncmp("UUID-", strrchr(devname,'/')+1,5) == 0) | |
807 | ) | |
ff54de6e | 808 | name_to_use = fname_from_uuid(st, ra, nbuf, '-'); |
d7288ddc | 809 | |
dbb44303 N |
810 | if (!mp) { |
811 | ||
812 | /* Check in mdadm.conf for devices == devname and | |
813 | * member == ra->text_version after second slash. | |
814 | */ | |
815 | char *sub = strchr(ra->text_version+1, '/'); | |
816 | struct mddev_ident_s *array_list; | |
817 | if (sub) { | |
818 | sub++; | |
819 | array_list = conf_get_ident(NULL); | |
820 | } else | |
821 | array_list = NULL; | |
822 | for(; array_list ; array_list = array_list->next) { | |
823 | int fd; | |
824 | char *dn; | |
825 | if (array_list->member == NULL || | |
826 | array_list->container == NULL) | |
827 | continue; | |
828 | if (strcmp(array_list->member, sub) != 0) | |
829 | continue; | |
830 | fd = open(array_list->container, O_RDONLY); | |
831 | if (fd < 0) | |
832 | continue; | |
833 | dn = devnum2devname(fd2devnum(fd)); | |
834 | close(fd); | |
835 | if (strncmp(dn, ra->text_version+1, | |
836 | strlen(dn)) != 0 || | |
837 | ra->text_version[strlen(dn)+1] != '/') { | |
838 | free(dn); | |
839 | continue; | |
840 | } | |
841 | free(dn); | |
842 | /* we have a match */ | |
843 | match = array_list; | |
844 | break; | |
845 | } | |
846 | } | |
847 | ||
848 | if (match && is_standard(match->devname, &devnum)) | |
849 | /* we have devnum now */; | |
850 | else if (mp) | |
c5afc314 | 851 | devnum = mp->devnum; |
dbb44303 N |
852 | else if (is_standard(name_to_use, &devnum)) |
853 | /* have devnum */; | |
c5afc314 | 854 | else { |
d7288ddc | 855 | n = name_to_use; |
c5afc314 N |
856 | if (*n == 'd') |
857 | n++; | |
858 | if (*n && devnum < 0) { | |
859 | devnum = strtoul(n, &n, 10); | |
860 | if (devnum >= 0 && (*n == 0 || *n == ' ')) { | |
861 | /* Use this devnum */ | |
d7288ddc | 862 | usepart = (name_to_use[0] == 'd'); |
c5afc314 N |
863 | if (mddev_busy(usepart ? (-1-devnum) : devnum)) |
864 | devnum = -1; | |
865 | } else | |
25dbe93a NB |
866 | devnum = -1; |
867 | } | |
25dbe93a | 868 | |
c5afc314 | 869 | if (devnum < 0) { |
d7288ddc | 870 | char *nm = name_to_use; |
c5afc314 N |
871 | char nbuf[1024]; |
872 | struct stat stb; | |
873 | if (strchr(nm, ':')) | |
874 | nm = strchr(nm, ':')+1; | |
875 | sprintf(nbuf, "/dev/md/%s", nm); | |
876 | ||
877 | if (stat(nbuf, &stb) == 0 && | |
878 | S_ISBLK(stb.st_mode) && | |
879 | major(stb.st_rdev) == (usepart ? | |
880 | get_mdp_major() : MD_MAJOR)){ | |
881 | if (usepart) | |
882 | devnum = minor(stb.st_rdev) | |
883 | >> MdpMinorShift; | |
884 | else | |
885 | devnum = minor(stb.st_rdev); | |
886 | if (mddev_busy(usepart ? (-1-devnum) : devnum)) | |
887 | devnum = -1; | |
888 | } | |
889 | } | |
890 | ||
891 | if (devnum >= 0) | |
892 | devnum = usepart ? (-1-devnum) : devnum; | |
893 | else | |
894 | devnum = find_free_devnum(usepart); | |
895 | } | |
dbb44303 N |
896 | mdfd = open_mddev_devnum(mp ? mp->path : match ? match->devname : NULL, |
897 | devnum, name_to_use, | |
598f0d58 NB |
898 | chosen_name, autof>>3); |
899 | ||
900 | if (mdfd < 0) { | |
901 | fprintf(stderr, Name ": failed to open %s: %s.\n", | |
902 | chosen_name, strerror(errno)); | |
903 | return 2; | |
904 | } | |
905 | ||
04c0634e | 906 | |
f35f2525 | 907 | sysfs_init(ra, mdfd, 0); |
04c0634e N |
908 | |
909 | sra = sysfs_read(mdfd, 0, GET_VERSION); | |
910 | if (sra == NULL || strcmp(sra->text_version, ra->text_version) != 0) | |
911 | if (sysfs_set_array(ra, md_get_version(mdfd)) != 0) | |
912 | return 1; | |
913 | if (sra) | |
914 | sysfs_free(sra); | |
915 | ||
2318b9f0 | 916 | for (dev = ra->devs; dev; dev = dev->next) |
f35f2525 | 917 | if (sysfs_add_disk(ra, dev) == 0) |
598f0d58 | 918 | working++; |
c5afc314 N |
919 | else if (errno == EEXIST) |
920 | preexist++; | |
921 | if (working == 0) | |
922 | /* Nothing new, don't try to start */ ; | |
923 | else if (runstop > 0 || | |
924 | (working + preexist) >= ra->array.working_disks) { | |
598f0d58 NB |
925 | switch(ra->array.level) { |
926 | case LEVEL_LINEAR: | |
927 | case LEVEL_MULTIPATH: | |
928 | case 0: | |
f35f2525 | 929 | sysfs_set_str(ra, NULL, "array_state", |
598f0d58 NB |
930 | "active"); |
931 | break; | |
932 | default: | |
f35f2525 | 933 | sysfs_set_str(ra, NULL, "array_state", |
598f0d58 | 934 | "readonly"); |
75aa18b5 | 935 | /* start mdmon if needed. */ |
8850ee3e N |
936 | if (!mdmon_running(st->container_dev)) |
937 | start_mdmon(st->container_dev); | |
938 | ping_monitor(devnum2devname(st->container_dev)); | |
598f0d58 NB |
939 | break; |
940 | } | |
f35f2525 | 941 | sysfs_set_safemode(ra, ra->safe_mode_delay); |
c5afc314 N |
942 | if (verbose >= 0) { |
943 | fprintf(stderr, Name | |
d7288ddc | 944 | ": Started %s with %d devices", |
c5afc314 N |
945 | chosen_name, working + preexist); |
946 | if (preexist) | |
947 | fprintf(stderr, " (%d new)", working); | |
948 | fprintf(stderr, "\n"); | |
949 | } | |
75aa18b5 | 950 | /* FIXME should have an O_EXCL and wait for read-auto */ |
598f0d58 NB |
951 | } else |
952 | if (verbose >= 0) | |
c5afc314 | 953 | fprintf(stderr, Name |
d7288ddc | 954 | ": %s assembled with %d devices but " |
c5afc314 N |
955 | "not started\n", |
956 | chosen_name, working); | |
598f0d58 | 957 | close(mdfd); |
c5afc314 N |
958 | map_update(&map, devnum, |
959 | ra->text_version, | |
960 | ra->uuid, chosen_name); | |
598f0d58 NB |
961 | } |
962 | return 0; | |
963 | } |