]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Detail.c
Document this...
[thirdparty/mdadm.git] / Detail.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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
34 int Detail(char *dev, int brief, int test)
35 {
36 /*
37 * Print out details for an md array by using
38 * GET_ARRAY_INFO and GET_DISK_INFO ioctl calls
39 */
40
41 int fd = open(dev, O_RDONLY, 0);
42 int vers;
43 mdu_array_info_t array;
44 int d;
45 time_t atime;
46 char *c;
47 char *devices = NULL;
48 int spares = 0;
49 struct stat stb;
50 int is_26 = get_linux_version() >= 2006000;
51 int is_rebuilding = 0;
52 int failed = 0;
53 struct supertype *st = NULL;
54 int max_disks = MD_SB_DISKS;
55
56 void *super = NULL;
57 int rv = test ? 4 : 1;
58
59 if (fd < 0) {
60 fprintf(stderr, Name ": cannot open %s: %s\n",
61 dev, strerror(errno));
62 return rv;
63 }
64 vers = md_get_version(fd);
65 if (vers < 0) {
66 fprintf(stderr, Name ": %s does not appear to be an md device\n",
67 dev);
68 close(fd);
69 return rv;
70 }
71 if (vers < 9000) {
72 fprintf(stderr, Name ": cannot get detail for md device %s: driver version too old.\n",
73 dev);
74 close(fd);
75 return rv;
76 }
77 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
78 if (errno == ENODEV)
79 fprintf(stderr, Name ": md device %s does not appear to be active.\n",
80 dev);
81 else
82 fprintf(stderr, Name ": cannot get array detail for %s: %s\n",
83 dev, strerror(errno));
84 close(fd);
85 return rv;
86 }
87 st = super_by_version(array.major_version, array.minor_version);
88
89 if (fstat(fd, &stb) != 0 && !S_ISBLK(stb.st_mode))
90 stb.st_rdev = 0;
91 rv = 0;
92
93 if (st) max_disks = st->max_devs;
94
95 /* try to load a superblock */
96 for (d= 0; d<max_disks; d++) {
97 mdu_disk_info_t disk;
98 char *dv;
99 disk.number = d;
100 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
101 continue;
102 if (d >= array.raid_disks &&
103 disk.major == 0 &&
104 disk.minor == 0)
105 continue;
106 if ((dv=map_dev(disk.major, disk.minor))) {
107 if (!super && (disk.state & (1<<MD_DISK_ACTIVE))) {
108 /* try to read the superblock from this device
109 * to get more info
110 */
111 int fd2 = open(dv, O_RDONLY);
112 if (fd2 >=0 && st &&
113 st->ss->load_super(st, fd2, &super, NULL) == 0) {
114 struct mdinfo info;
115 st->ss->getinfo_super(&info, super);
116 if (info.array.ctime != array.ctime ||
117 info.array.level != array.level) {
118 free(super);
119 super = NULL;
120 }
121 }
122 if (fd2 >= 0) close(fd2);
123 }
124 }
125 }
126
127 /* Ok, we have some info to print... */
128 c = map_num(pers, array.level);
129 if (brief)
130 printf("ARRAY %s level=%s num-devices=%d", dev, c?c:"-unknown-",array.raid_disks );
131 else {
132 unsigned long array_size;
133 unsigned long long larray_size;
134 struct mdstat_ent *ms = mdstat_read(0);
135 struct mdstat_ent *e;
136 int devnum = array.md_minor;
137 if (major(stb.st_rdev) != MD_MAJOR)
138 devnum = -1 - devnum;
139
140 for (e=ms; e; e=e->next)
141 if (e->devnum == devnum)
142 break;
143 #ifdef BLKGETSIZE64
144 if (ioctl(fd, BLKGETSIZE64, &larray_size)==0)
145 ;
146 else
147 #endif
148 if (ioctl(fd, BLKGETSIZE, &array_size)==0) {
149 larray_size = array_size;
150 larray_size <<= 9;
151 }
152
153 else larray_size = 0;
154
155 printf("%s:\n", dev);
156 printf(" Version : %02d.%02d.%02d\n",
157 array.major_version, array.minor_version, array.patch_version);
158 atime = array.ctime;
159 printf(" Creation Time : %.24s\n", ctime(&atime));
160 printf(" Raid Level : %s\n", c?c:"-unknown-");
161 if (larray_size)
162 printf(" Array Size : %llu%s\n", (larray_size>>10), human_size(larray_size));
163 if (array.level >= 1)
164 printf(" Device Size : %d%s\n", array.size, human_size((long long)array.size<<10));
165 printf(" Raid Devices : %d\n", array.raid_disks);
166 printf(" Total Devices : %d\n", array.nr_disks);
167 printf("Preferred Minor : %d\n", array.md_minor);
168 printf(" Persistence : Superblock is %spersistent\n",
169 array.not_persistent?"not ":"");
170 printf("\n");
171 atime = array.utime;
172 printf(" Update Time : %.24s\n", ctime(&atime));
173 printf(" State : %s%s%s\n",
174 (array.state&(1<<MD_SB_CLEAN))?"clean":"active",
175 array.active_disks < array.raid_disks? ", degraded":"",
176 (!e || e->percent < 0) ? "" :
177 (e->resync) ? ", resyncing": ", recovering");
178 printf(" Active Devices : %d\n", array.active_disks);
179 printf("Working Devices : %d\n", array.working_disks);
180 printf(" Failed Devices : %d\n", array.failed_disks);
181 printf(" Spare Devices : %d\n", array.spare_disks);
182 printf("\n");
183 if (array.level == 5) {
184 c = map_num(r5layout, array.layout);
185 printf(" Layout : %s\n", c?c:"-unknown-");
186 }
187 if (array.level == 10) {
188 printf(" Layout : near=%d, far=%d\n",
189 array.layout&255, (array.layout>>8)&255);
190 }
191 switch (array.level) {
192 case 0:
193 case 4:
194 case 5:
195 case 10:
196 case 6:
197 printf(" Chunk Size : %dK\n\n", array.chunk_size/1024);
198 break;
199 case -1:
200 printf(" Rounding : %dK\n\n", array.chunk_size/1024);
201 break;
202 default: break;
203 }
204
205 if (e && e->percent >= 0) {
206 printf(" Rebuild Status : %d%% complete\n\n", e->percent);
207 is_rebuilding = 1;
208 }
209 free_mdstat(ms);
210
211 if (super && st)
212 st->ss->detail_super(super);
213
214 printf(" Number Major Minor RaidDevice State\n");
215 }
216 for (d= 0; d < max_disks; d++) {
217 mdu_disk_info_t disk;
218 char *dv;
219 disk.number = d;
220 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
221 if (d < array.raid_disks)
222 fprintf(stderr, Name ": cannot get device detail for device %d: %s\n",
223 d, strerror(errno));
224 continue;
225 }
226 if (d >= array.raid_disks &&
227 disk.major == 0 &&
228 disk.minor == 0)
229 continue;
230 if (!brief) {
231 if (disk.number == array.raid_disks) printf("\n");
232 if (disk.raid_disk < 0)
233 printf(" %5d %5d %5d - ",
234 disk.number, disk.major, disk.minor);
235 else
236 printf(" %5d %5d %5d %5d ",
237 disk.number, disk.major, disk.minor, disk.raid_disk);
238 if (disk.state & (1<<MD_DISK_FAULTY)) {
239 printf(" faulty");
240 if (disk.raid_disk < array.raid_disks &&
241 disk.raid_disk >= 0)
242 failed++;
243 }
244 if (disk.state & (1<<MD_DISK_ACTIVE)) printf(" active");
245 if (disk.state & (1<<MD_DISK_SYNC)) printf(" sync");
246 if (disk.state & (1<<MD_DISK_REMOVED)) printf(" removed");
247 if (disk.state == 0) printf(" spare");
248 if (disk.state == 0) {
249 if (is_26) {
250 if (disk.raid_disk < array.raid_disks && disk.raid_disk >= 0)
251 printf(" rebuilding");
252 } else if (is_rebuilding && failed) {
253 /* Taking a bit of a risk here, we remove the
254 * device from the array, and then put it back.
255 * If this fails, we are rebuilding
256 */
257 int err = ioctl(fd, HOT_REMOVE_DISK, makedev(disk.major, disk.minor));
258 if (err == 0) ioctl(fd, HOT_ADD_DISK, makedev(disk.major, disk.minor));
259 if (err && errno == EBUSY)
260 printf(" rebuilding");
261 }
262 }
263 }
264 if (disk.state == 0) spares++;
265 if (test && d < array.raid_disks && disk.state & (1<<MD_DISK_FAULTY)) {
266 if ((rv & 1) && (array.level ==4 || array.level == 5))
267 rv |= 2;
268 rv |= 1;
269 }
270 if ((dv=map_dev(disk.major, disk.minor))) {
271 if (brief) {
272 if (devices) {
273 devices = realloc(devices,
274 strlen(devices)+1+strlen(dv)+1);
275 strcat(strcat(devices,","),dv);
276 } else
277 devices = strdup(dv);
278 } else
279 printf(" %s", dv);
280 }
281 if (!brief) printf("\n");
282 }
283 if (spares && brief) printf(" spares=%d", spares);
284 if (super && brief && st)
285 st->ss->brief_detail_super(super);
286
287 if (brief > 1 && devices) printf("\n devices=%s", devices);
288 if (brief) printf("\n");
289 if (test && (rv&2)) rv &= ~1;
290 close(fd);
291 return rv;
292 }