]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdadm - manage Linux "md" devices aka RAID arrays. | |
3 | * | |
4 | * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de> | |
5 | * | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | * | |
21 | * Author: Neil Brown | |
22 | * Email: <neilb@cse.unsw.edu.au> | |
23 | * Paper: Neil Brown | |
24 | * School of Computer Science and Engineering | |
25 | * The University of New South Wales | |
26 | * Sydney, 2052 | |
27 | * Australia | |
28 | */ | |
29 | ||
30 | #include "mdadm.h" | |
31 | #include "md_p.h" | |
32 | #include "md_u.h" | |
33 | #include <dirent.h> | |
34 | ||
35 | int Detail(char *dev, int brief, int export, int test, char *homehost) | |
36 | { | |
37 | /* | |
38 | * Print out details for an md array by using | |
39 | * GET_ARRAY_INFO and GET_DISK_INFO ioctl calls | |
40 | */ | |
41 | ||
42 | int fd = open(dev, O_RDONLY); | |
43 | int vers; | |
44 | mdu_array_info_t array; | |
45 | mdu_disk_info_t *disks; | |
46 | int next; | |
47 | int d; | |
48 | time_t atime; | |
49 | char *c; | |
50 | char *devices = NULL; | |
51 | int spares = 0; | |
52 | struct stat stb; | |
53 | int is_26 = get_linux_version() >= 2006000; | |
54 | int is_rebuilding = 0; | |
55 | int failed = 0; | |
56 | struct supertype *st = NULL; | |
57 | int max_disks = MD_SB_DISKS; /* just a default */ | |
58 | struct mdinfo info; | |
59 | struct mdinfo *sra; | |
60 | char *member = NULL; | |
61 | char *container = NULL; | |
62 | ||
63 | int rv = test ? 4 : 1; | |
64 | int avail_disks = 0; | |
65 | char *avail; | |
66 | ||
67 | if (fd < 0) { | |
68 | fprintf(stderr, Name ": cannot open %s: %s\n", | |
69 | dev, strerror(errno)); | |
70 | return rv; | |
71 | } | |
72 | vers = md_get_version(fd); | |
73 | if (vers < 0) { | |
74 | fprintf(stderr, Name ": %s does not appear to be an md device\n", | |
75 | dev); | |
76 | close(fd); | |
77 | return rv; | |
78 | } | |
79 | if (vers < 9000) { | |
80 | fprintf(stderr, Name ": cannot get detail for md device %s: driver version too old.\n", | |
81 | dev); | |
82 | close(fd); | |
83 | return rv; | |
84 | } | |
85 | if (ioctl(fd, GET_ARRAY_INFO, &array)<0) { | |
86 | if (errno == ENODEV) | |
87 | fprintf(stderr, Name ": md device %s does not appear to be active.\n", | |
88 | dev); | |
89 | else | |
90 | fprintf(stderr, Name ": cannot get array detail for %s: %s\n", | |
91 | dev, strerror(errno)); | |
92 | close(fd); | |
93 | return rv; | |
94 | } | |
95 | sra = sysfs_read(fd, 0, GET_VERSION); | |
96 | st = super_by_fd(fd); | |
97 | ||
98 | if (fstat(fd, &stb) != 0 && !S_ISBLK(stb.st_mode)) | |
99 | stb.st_rdev = 0; | |
100 | rv = 0; | |
101 | ||
102 | if (st) | |
103 | max_disks = st->max_devs; | |
104 | ||
105 | if (sra && is_subarray(sra->text_version) && | |
106 | strchr(sra->text_version+1, '/')) { | |
107 | /* This is a subarray of some container. | |
108 | * We want the name of the container, and the member | |
109 | */ | |
110 | char *s = strchr(sra->text_version+1, '/'); | |
111 | int dn; | |
112 | *s++ = '\0'; | |
113 | member = s; | |
114 | dn = devname2devnum(sra->text_version+1); | |
115 | container = map_dev(dev2major(dn), dev2minor(dn), 1); | |
116 | } | |
117 | ||
118 | /* try to load a superblock */ | |
119 | for (d= 0; d<max_disks; d++) { | |
120 | mdu_disk_info_t disk; | |
121 | char *dv; | |
122 | disk.number = d; | |
123 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) | |
124 | continue; | |
125 | if (d >= array.raid_disks && | |
126 | disk.major == 0 && | |
127 | disk.minor == 0) | |
128 | continue; | |
129 | if ((dv=map_dev(disk.major, disk.minor, 1))) { | |
130 | if ((!st || !st->sb) && | |
131 | (array.raid_disks == 0 || | |
132 | (disk.state & (1<<MD_DISK_ACTIVE)))) { | |
133 | /* try to read the superblock from this device | |
134 | * to get more info | |
135 | */ | |
136 | int fd2 = dev_open(dv, O_RDONLY); | |
137 | if (fd2 >=0 && st && | |
138 | st->ss->load_super(st, fd2, NULL) == 0) { | |
139 | st->ss->getinfo_super(st, &info); | |
140 | if (array.raid_disks != 0 && /* container */ | |
141 | (info.array.ctime != array.ctime || | |
142 | info.array.level != array.level)) | |
143 | st->ss->free_super(st); | |
144 | } | |
145 | if (fd2 >= 0) close(fd2); | |
146 | } | |
147 | } | |
148 | } | |
149 | ||
150 | /* Ok, we have some info to print... */ | |
151 | c = map_num(pers, array.level); | |
152 | ||
153 | if (export) { | |
154 | if (array.raid_disks) { | |
155 | if (c) | |
156 | printf("MD_LEVEL=%s\n", c); | |
157 | printf("MD_DEVICES=%d\n", array.raid_disks); | |
158 | } else { | |
159 | printf("MD_LEVEL=container\n"); | |
160 | printf("MD_DEVICES=%d\n", array.nr_disks); | |
161 | } | |
162 | if (container) { | |
163 | printf("MD_CONTAINER=%s\n", container); | |
164 | printf("MD_MEMBER=%s\n", member); | |
165 | } else { | |
166 | if (sra && sra->array.major_version < 0) | |
167 | printf("MD_METADATA=%s\n", sra->text_version); | |
168 | else | |
169 | printf("MD_METADATA=%d.%02d\n", | |
170 | array.major_version, array.minor_version); | |
171 | } | |
172 | ||
173 | if (st && st->sb) { | |
174 | struct mdinfo info; | |
175 | char nbuf[64]; | |
176 | struct map_ent *mp, *map = NULL; | |
177 | st->ss->getinfo_super(st, &info); | |
178 | fname_from_uuid(st, &info, nbuf, ':'); | |
179 | printf("MD_UUID=%s\n", nbuf+5); | |
180 | mp = map_by_uuid(&map, info.uuid); | |
181 | if (mp && mp->path && | |
182 | strncmp(mp->path, "/dev/md/", 8) == 0) | |
183 | printf("MD_DEVNAME=%s\n", mp->path+8); | |
184 | ||
185 | if (st->ss->export_detail_super) | |
186 | st->ss->export_detail_super(st); | |
187 | } else { | |
188 | struct map_ent *mp, *map = NULL; | |
189 | mp = map_by_devnum(&map, fd2devnum(fd)); | |
190 | if (mp && mp->path && | |
191 | strncmp(mp->path, "/dev/md/", 8) == 0) | |
192 | printf("MD_DEVNAME=%s\n", mp->path+8); | |
193 | } | |
194 | goto out; | |
195 | } | |
196 | ||
197 | if (brief) { | |
198 | mdu_bitmap_file_t bmf; | |
199 | if (array.raid_disks) | |
200 | printf("ARRAY %s level=%s num-devices=%d", dev, | |
201 | c?c:"-unknown-", | |
202 | array.raid_disks ); | |
203 | else | |
204 | printf("ARRAY %s level=container num-devices=%d", | |
205 | dev, array.nr_disks); | |
206 | ||
207 | if (container) { | |
208 | printf(" container=%s", container); | |
209 | printf(" member=%s", member); | |
210 | } else { | |
211 | if (sra && sra->array.major_version < 0) | |
212 | printf(" metadata=%s", sra->text_version); | |
213 | else | |
214 | printf(" metadata=%d.%02d", | |
215 | array.major_version, array.minor_version); | |
216 | } | |
217 | ||
218 | /* Only try GET_BITMAP_FILE for 0.90.01 and later */ | |
219 | if (vers >= 9001 && | |
220 | ioctl(fd, GET_BITMAP_FILE, &bmf) == 0 && | |
221 | bmf.pathname[0]) { | |
222 | printf(" bitmap=%s", bmf.pathname); | |
223 | } | |
224 | } else { | |
225 | mdu_bitmap_file_t bmf; | |
226 | unsigned long long larray_size; | |
227 | struct mdstat_ent *ms = mdstat_read(0, 0); | |
228 | struct mdstat_ent *e; | |
229 | int devnum = array.md_minor; | |
230 | if (major(stb.st_rdev) == get_mdp_major()) | |
231 | devnum = -1 - devnum; | |
232 | ||
233 | for (e=ms; e; e=e->next) | |
234 | if (e->devnum == devnum) | |
235 | break; | |
236 | if (!get_dev_size(fd, NULL, &larray_size)) | |
237 | larray_size = 0; | |
238 | ||
239 | printf("%s:\n", dev); | |
240 | ||
241 | if (container) | |
242 | printf(" Container : %s, member %s\n", container, member); | |
243 | else { | |
244 | if (sra && sra->array.major_version < 0) | |
245 | printf(" Version : %s\n", sra->text_version); | |
246 | else | |
247 | printf(" Version : %d.%02d\n", | |
248 | array.major_version, array.minor_version); | |
249 | } | |
250 | ||
251 | atime = array.ctime; | |
252 | if (atime) | |
253 | printf(" Creation Time : %.24s\n", ctime(&atime)); | |
254 | if (array.raid_disks == 0) c = "container"; | |
255 | printf(" Raid Level : %s\n", c?c:"-unknown-"); | |
256 | if (larray_size) | |
257 | printf(" Array Size : %llu%s\n", (larray_size>>10), human_size(larray_size)); | |
258 | if (array.level >= 1) { | |
259 | if (array.major_version != 0 && | |
260 | (larray_size >= 0xFFFFFFFFULL|| array.size == 0)) { | |
261 | unsigned long long dsize = get_component_size(fd); | |
262 | if (dsize > 0) | |
263 | printf(" Used Dev Size : %llu%s\n", | |
264 | dsize, | |
265 | human_size((long long)dsize<<10)); | |
266 | else | |
267 | printf(" Used Dev Size : unknown\n"); | |
268 | } else | |
269 | printf(" Used Dev Size : %d%s\n", array.size, | |
270 | human_size((long long)array.size<<10)); | |
271 | } | |
272 | if (array.raid_disks) | |
273 | printf(" Raid Devices : %d\n", array.raid_disks); | |
274 | printf(" Total Devices : %d\n", array.nr_disks); | |
275 | if (!container && | |
276 | ((sra == NULL && array.major_version == 0) || | |
277 | (sra && sra->array.major_version == 0))) | |
278 | printf("Preferred Minor : %d\n", array.md_minor); | |
279 | if (sra == NULL || sra->array.major_version >= 0) | |
280 | printf(" Persistence : Superblock is %spersistent\n", | |
281 | array.not_persistent?"not ":""); | |
282 | printf("\n"); | |
283 | /* Only try GET_BITMAP_FILE for 0.90.01 and later */ | |
284 | if (vers >= 9001 && | |
285 | ioctl(fd, GET_BITMAP_FILE, &bmf) == 0 && | |
286 | bmf.pathname[0]) { | |
287 | printf(" Intent Bitmap : %s\n", bmf.pathname); | |
288 | printf("\n"); | |
289 | } else if (array.state & (1<<MD_SB_BITMAP_PRESENT)) | |
290 | printf(" Intent Bitmap : Internal\n\n"); | |
291 | atime = array.utime; | |
292 | if (atime) | |
293 | printf(" Update Time : %.24s\n", ctime(&atime)); | |
294 | if (array.raid_disks) | |
295 | printf(" State : %s%s%s%s\n", | |
296 | (array.state&(1<<MD_SB_CLEAN))?"clean":"active", | |
297 | array.active_disks < array.raid_disks? ", degraded":"", | |
298 | (!e || e->percent < 0) ? "" : | |
299 | (e->resync) ? ", resyncing": ", recovering", | |
300 | larray_size ? "": ", Not Started"); | |
301 | if (array.raid_disks) | |
302 | printf(" Active Devices : %d\n", array.active_disks); | |
303 | printf("Working Devices : %d\n", array.working_disks); | |
304 | if (array.raid_disks) { | |
305 | printf(" Failed Devices : %d\n", array.failed_disks); | |
306 | printf(" Spare Devices : %d\n", array.spare_disks); | |
307 | } | |
308 | printf("\n"); | |
309 | if (array.level == 5) { | |
310 | c = map_num(r5layout, array.layout); | |
311 | printf(" Layout : %s\n", c?c:"-unknown-"); | |
312 | } | |
313 | if (array.level == 10) { | |
314 | printf(" Layout :"); | |
315 | print_r10_layout(array.layout); | |
316 | printf("\n"); | |
317 | } | |
318 | switch (array.level) { | |
319 | case 0: | |
320 | case 4: | |
321 | case 5: | |
322 | case 10: | |
323 | case 6: | |
324 | if (array.chunk_size) | |
325 | printf(" Chunk Size : %dK\n\n", | |
326 | array.chunk_size/1024); | |
327 | break; | |
328 | case -1: | |
329 | printf(" Rounding : %dK\n\n", array.chunk_size/1024); | |
330 | break; | |
331 | default: break; | |
332 | } | |
333 | ||
334 | if (e && e->percent >= 0) { | |
335 | printf(" Re%s Status : %d%% complete\n", | |
336 | (st && st->sb && info.reshape_active)? | |
337 | "shape":"build", | |
338 | e->percent); | |
339 | is_rebuilding = 1; | |
340 | } | |
341 | free_mdstat(ms); | |
342 | ||
343 | if (st->sb && info.reshape_active) { | |
344 | #if 0 | |
345 | This is pretty boring | |
346 | printf(" Reshape pos'n : %llu%s\n", (unsigned long long) info.reshape_progress<<9, | |
347 | human_size(info.reshape_progress<<9)); | |
348 | #endif | |
349 | if (info.delta_disks > 0) | |
350 | printf(" Delta Devices : %d, (%d->%d)\n", | |
351 | info.delta_disks, array.raid_disks - info.delta_disks, array.raid_disks); | |
352 | if (info.delta_disks < 0) | |
353 | printf(" Delta Devices : %d, (%d->%d)\n", | |
354 | info.delta_disks, array.raid_disks, array.raid_disks + info.delta_disks); | |
355 | if (info.new_level != array.level) { | |
356 | char *c = map_num(pers, info.new_level); | |
357 | printf(" New Level : %s\n", c?c:"-unknown-"); | |
358 | } | |
359 | if (info.new_level != array.level || | |
360 | info.new_layout != array.layout) { | |
361 | if (info.new_level == 5) { | |
362 | char *c = map_num(r5layout, info.new_layout); | |
363 | printf(" New Layout : %s\n", | |
364 | c?c:"-unknown-"); | |
365 | } | |
366 | if (info.new_level == 10) { | |
367 | printf(" New Layout : near=%d, %s=%d\n", | |
368 | info.new_layout&255, | |
369 | (info.new_layout&0x10000)?"offset":"far", | |
370 | (info.new_layout>>8)&255); | |
371 | } | |
372 | } | |
373 | if (info.new_chunk != array.chunk_size) | |
374 | printf(" New Chunksize : %dK\n", info.new_chunk/1024); | |
375 | printf("\n"); | |
376 | } else if (e && e->percent >= 0) | |
377 | printf("\n"); | |
378 | if (st && st->sb) | |
379 | st->ss->detail_super(st, homehost); | |
380 | ||
381 | if (array.raid_disks == 0 && sra && sra->array.major_version == -1 | |
382 | && sra->array.minor_version == -2 && sra->text_version[0] != '/') { | |
383 | /* This looks like a container. Find any active arrays | |
384 | * That claim to be a member. | |
385 | */ | |
386 | DIR *dir = opendir("/sys/block"); | |
387 | struct dirent *de; | |
388 | ||
389 | printf(" Member Arrays :"); | |
390 | ||
391 | while (dir && (de = readdir(dir)) != NULL) { | |
392 | char path[200]; | |
393 | char vbuf[1024]; | |
394 | int nlen = strlen(sra->sys_name); | |
395 | int dn; | |
396 | if (de->d_name[0] == '.') | |
397 | continue; | |
398 | sprintf(path, "/sys/block/%s/md/metadata_version", | |
399 | de->d_name); | |
400 | if (load_sys(path, vbuf) < 0) | |
401 | continue; | |
402 | if (strncmp(vbuf, "external:", 9) != 0 || | |
403 | !is_subarray(sra->sys_name+9) || | |
404 | strncmp(vbuf+10, sra->sys_name, nlen) != 0 || | |
405 | vbuf[10+nlen] != '/') | |
406 | continue; | |
407 | dn = devname2devnum(de->d_name); | |
408 | printf(" %s", map_dev(dev2major(dn), | |
409 | dev2minor(dn), 1)); | |
410 | } | |
411 | if (dir) | |
412 | closedir(dir); | |
413 | printf("\n\n"); | |
414 | } | |
415 | ||
416 | if (array.raid_disks) | |
417 | printf(" Number Major Minor RaidDevice State\n"); | |
418 | else | |
419 | printf(" Number Major Minor RaidDevice\n"); | |
420 | } | |
421 | disks = malloc(max_disks * sizeof(mdu_disk_info_t)); | |
422 | for (d=0; d<max_disks; d++) { | |
423 | disks[d].state = (1<<MD_DISK_REMOVED); | |
424 | disks[d].major = disks[d].minor = 0; | |
425 | disks[d].number = disks[d].raid_disk = d; | |
426 | } | |
427 | ||
428 | next = array.raid_disks; | |
429 | for (d=0; d < max_disks; d++) { | |
430 | mdu_disk_info_t disk; | |
431 | disk.number = d; | |
432 | if (ioctl(fd, GET_DISK_INFO, &disk) < 0) { | |
433 | if (d < array.raid_disks) | |
434 | fprintf(stderr, Name ": cannot get device detail for device %d: %s\n", | |
435 | d, strerror(errno)); | |
436 | continue; | |
437 | } | |
438 | if (disk.major == 0 && disk.minor == 0) | |
439 | continue; | |
440 | if (disk.raid_disk >= 0 && disk.raid_disk < array.raid_disks) | |
441 | disks[disk.raid_disk] = disk; | |
442 | else if (next < max_disks) | |
443 | disks[next++] = disk; | |
444 | } | |
445 | ||
446 | avail = calloc(array.raid_disks, 1); | |
447 | for (d= 0; d < max_disks; d++) { | |
448 | char *dv; | |
449 | mdu_disk_info_t disk = disks[d]; | |
450 | ||
451 | if (d >= array.raid_disks && | |
452 | disk.major == 0 && | |
453 | disk.minor == 0) | |
454 | continue; | |
455 | if (!brief) { | |
456 | if (d == array.raid_disks) printf("\n"); | |
457 | if (disk.raid_disk < 0) | |
458 | printf(" %5d %5d %5d - ", | |
459 | disk.number, disk.major, disk.minor); | |
460 | else | |
461 | printf(" %5d %5d %5d %5d ", | |
462 | disk.number, disk.major, disk.minor, disk.raid_disk); | |
463 | } | |
464 | if (!brief && array.raid_disks) { | |
465 | ||
466 | if (disk.state & (1<<MD_DISK_FAULTY)) { | |
467 | printf(" faulty"); | |
468 | if (disk.raid_disk < array.raid_disks && | |
469 | disk.raid_disk >= 0) | |
470 | failed++; | |
471 | } | |
472 | if (disk.state & (1<<MD_DISK_ACTIVE)) printf(" active"); | |
473 | if (disk.state & (1<<MD_DISK_SYNC)) printf(" sync"); | |
474 | if (disk.state & (1<<MD_DISK_REMOVED)) printf(" removed"); | |
475 | if (disk.state & (1<<MD_DISK_WRITEMOSTLY)) printf(" writemostly"); | |
476 | if ((disk.state & | |
477 | ((1<<MD_DISK_ACTIVE)|(1<<MD_DISK_SYNC)|(1<<MD_DISK_REMOVED))) | |
478 | == 0) { | |
479 | printf(" spare"); | |
480 | if (is_26) { | |
481 | if (disk.raid_disk < array.raid_disks && disk.raid_disk >= 0) | |
482 | printf(" rebuilding"); | |
483 | } else if (is_rebuilding && failed) { | |
484 | /* Taking a bit of a risk here, we remove the | |
485 | * device from the array, and then put it back. | |
486 | * If this fails, we are rebuilding | |
487 | */ | |
488 | int err = ioctl(fd, HOT_REMOVE_DISK, makedev(disk.major, disk.minor)); | |
489 | if (err == 0) ioctl(fd, HOT_ADD_DISK, makedev(disk.major, disk.minor)); | |
490 | if (err && errno == EBUSY) | |
491 | printf(" rebuilding"); | |
492 | } | |
493 | } | |
494 | } | |
495 | if (disk.state == 0) spares++; | |
496 | if (test && d < array.raid_disks | |
497 | && !(disk.state & (1<<MD_DISK_SYNC))) | |
498 | rv |= 1; | |
499 | if (d < array.raid_disks | |
500 | && (disk.state & (1<<MD_DISK_SYNC))) { | |
501 | avail_disks ++; | |
502 | avail[d] = 1; | |
503 | } | |
504 | if ((dv=map_dev(disk.major, disk.minor, 0))) { | |
505 | if (brief) { | |
506 | if (devices) { | |
507 | devices = realloc(devices, | |
508 | strlen(devices)+1+strlen(dv)+1); | |
509 | strcat(strcat(devices,","),dv); | |
510 | } else | |
511 | devices = strdup(dv); | |
512 | } else | |
513 | printf(" %s", dv); | |
514 | } | |
515 | if (!brief) printf("\n"); | |
516 | } | |
517 | if (spares && brief && array.raid_disks) printf(" spares=%d", spares); | |
518 | if (brief && st && st->sb) | |
519 | st->ss->brief_detail_super(st); | |
520 | st->ss->free_super(st); | |
521 | ||
522 | if (brief > 1 && devices) printf("\n devices=%s", devices); | |
523 | if (brief) printf("\n"); | |
524 | if (test && | |
525 | !enough(array.level, array.raid_disks, array.layout, | |
526 | 1, avail, avail_disks)) | |
527 | rv = 2; | |
528 | ||
529 | out: | |
530 | close(fd); | |
531 | return rv; | |
532 | } |