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