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