Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / mdadm / Detail.c
blobca34f1d48f125618bf33b554c7c96385296772d9
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
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.
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.
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
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
30 #include "mdadm.h"
31 #include "md_p.h"
32 #include "md_u.h"
34 int Detail(char *dev, int brief, int test, char *homehost)
37 * Print out details for an md array by using
38 * GET_ARRAY_INFO and GET_DISK_INFO ioctl calls
41 int fd = open(dev, O_RDONLY, 0);
42 int vers;
43 mdu_array_info_t array;
44 mdu_disk_info_t *disks;
45 int next;
46 int d;
47 time_t atime;
48 char *c;
49 char *devices = NULL;
50 int spares = 0;
51 struct stat stb;
52 int is_26 = get_linux_version() >= 2006000;
53 int is_rebuilding = 0;
54 int failed = 0;
55 struct supertype *st = NULL;
56 int max_disks = MD_SB_DISKS; /* just a default */
57 struct mdinfo info;
59 void *super = NULL;
60 int rv = test ? 4 : 1;
62 if (fd < 0) {
63 fprintf(stderr, Name ": cannot open %s: %s\n",
64 dev, strerror(errno));
65 return rv;
67 vers = md_get_version(fd);
68 if (vers < 0) {
69 fprintf(stderr, Name ": %s does not appear to be an md device\n",
70 dev);
71 close(fd);
72 return rv;
74 if (vers < 9000) {
75 fprintf(stderr, Name ": cannot get detail for md device %s: driver version too old.\n",
76 dev);
77 close(fd);
78 return rv;
80 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
81 if (errno == ENODEV)
82 fprintf(stderr, Name ": md device %s does not appear to be active.\n",
83 dev);
84 else
85 fprintf(stderr, Name ": cannot get array detail for %s: %s\n",
86 dev, strerror(errno));
87 close(fd);
88 return rv;
90 st = super_by_version(array.major_version, array.minor_version);
92 if (fstat(fd, &stb) != 0 && !S_ISBLK(stb.st_mode))
93 stb.st_rdev = 0;
94 rv = 0;
96 if (st) max_disks = st->max_devs;
98 /* try to load a superblock */
99 for (d= 0; d<max_disks; d++) {
100 mdu_disk_info_t disk;
101 char *dv;
102 disk.number = d;
103 if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
104 continue;
105 if (d >= array.raid_disks &&
106 disk.major == 0 &&
107 disk.minor == 0)
108 continue;
109 if ((dv=map_dev(disk.major, disk.minor, 1))) {
110 if (!super && (disk.state & (1<<MD_DISK_ACTIVE))) {
111 /* try to read the superblock from this device
112 * to get more info
114 int fd2 = dev_open(dv, O_RDONLY);
115 if (fd2 >=0 && st &&
116 st->ss->load_super(st, fd2, &super, NULL) == 0) {
117 st->ss->getinfo_super(&info, super);
118 if (info.array.ctime != array.ctime ||
119 info.array.level != array.level) {
120 free(super);
121 super = NULL;
124 if (fd2 >= 0) close(fd2);
129 /* Ok, we have some info to print... */
130 c = map_num(pers, array.level);
131 if (brief)
132 printf("ARRAY %s level=%s num-devices=%d", dev, c?c:"-unknown-",array.raid_disks );
133 else {
134 mdu_bitmap_file_t bmf;
135 unsigned long long larray_size;
136 struct mdstat_ent *ms = mdstat_read(0, 0);
137 struct mdstat_ent *e;
138 int devnum = array.md_minor;
139 if (major(stb.st_rdev) != MD_MAJOR)
140 devnum = -1 - devnum;
142 for (e=ms; e; e=e->next)
143 if (e->devnum == devnum)
144 break;
145 if (!get_dev_size(fd, NULL, &larray_size))
146 larray_size = 0;
148 printf("%s:\n", dev);
149 printf(" Version : %02d.%02d.%02d\n",
150 array.major_version, array.minor_version, array.patch_version);
151 atime = array.ctime;
152 printf(" Creation Time : %.24s\n", ctime(&atime));
153 printf(" Raid Level : %s\n", c?c:"-unknown-");
154 if (larray_size)
155 printf(" Array Size : %llu%s\n", (larray_size>>10), human_size(larray_size));
156 if (array.level >= 1) {
157 if (array.major_version != 0 &&
158 (larray_size >= 0xFFFFFFFFULL|| array.size == 0)) {
159 unsigned long long dsize = get_component_size(fd);
160 if (dsize > 0)
161 printf(" Used Dev Size : %llu%s\n",
162 dsize,
163 human_size((long long)array.size<<10));
164 else
165 printf(" Used Dev Size : unknown\n");
166 } else
167 printf(" Used Dev Size : %d%s\n", array.size,
168 human_size((long long)array.size<<10));
170 printf(" Raid Devices : %d\n", array.raid_disks);
171 printf(" Total Devices : %d\n", array.nr_disks);
172 printf("Preferred Minor : %d\n", array.md_minor);
173 printf(" Persistence : Superblock is %spersistent\n",
174 array.not_persistent?"not ":"");
175 printf("\n");
176 /* Only try GET_BITMAP_FILE for 0.90.01 and later */
177 if (vers >= 9001 &&
178 ioctl(fd, GET_BITMAP_FILE, &bmf) == 0 &&
179 bmf.pathname[0]) {
180 printf(" Intent Bitmap : %s\n", bmf.pathname);
181 printf("\n");
182 } else if (array.state & (1<<MD_SB_BITMAP_PRESENT))
183 printf(" Intent Bitmap : Internal\n\n");
184 atime = array.utime;
185 printf(" Update Time : %.24s\n", ctime(&atime));
186 printf(" State : %s%s%s%s\n",
187 (array.state&(1<<MD_SB_CLEAN))?"clean":"active",
188 array.active_disks < array.raid_disks? ", degraded":"",
189 (!e || e->percent < 0) ? "" :
190 (e->resync) ? ", resyncing": ", recovering",
191 larray_size ? "": ", Not Started");
192 printf(" Active Devices : %d\n", array.active_disks);
193 printf("Working Devices : %d\n", array.working_disks);
194 printf(" Failed Devices : %d\n", array.failed_disks);
195 printf(" Spare Devices : %d\n", array.spare_disks);
196 printf("\n");
197 if (array.level == 5) {
198 c = map_num(r5layout, array.layout);
199 printf(" Layout : %s\n", c?c:"-unknown-");
201 if (array.level == 10) {
202 printf(" Layout : near=%d, %s=%d\n",
203 array.layout&255, (array.layout&0x10000)?"offset":"far",
204 (array.layout>>8)&255);
206 switch (array.level) {
207 case 0:
208 case 4:
209 case 5:
210 case 10:
211 case 6:
212 printf(" Chunk Size : %dK\n\n", array.chunk_size/1024);
213 break;
214 case -1:
215 printf(" Rounding : %dK\n\n", array.chunk_size/1024);
216 break;
217 default: break;
220 if (e && e->percent >= 0) {
221 printf(" Re%s Status : %d%% complete\n",
222 (super && info.reshape_active)? "shape":"build",
223 e->percent);
224 is_rebuilding = 1;
226 free_mdstat(ms);
228 if (super && info.reshape_active) {
229 #if 0
230 This is pretty boring
231 printf(" Reshape pos'n : %llu%s\n", (unsigned long long) info.reshape_progress<<9,
232 human_size(info.reshape_progress<<9));
233 #endif
234 if (info.delta_disks > 0)
235 printf(" Delta Devices : %d, (%d->%d)\n",
236 info.delta_disks, array.raid_disks - info.delta_disks, array.raid_disks);
237 if (info.delta_disks < 0)
238 printf(" Delta Devices : %d, (%d->%d)\n",
239 info.delta_disks, array.raid_disks, array.raid_disks + info.delta_disks);
240 if (info.new_level != array.level) {
241 char *c = map_num(pers, info.new_level);
242 printf(" New Level : %s\n", c?c:"-unknown-");
244 if (info.new_level != array.level ||
245 info.new_layout != array.layout) {
246 if (info.new_level == 5) {
247 char *c = map_num(r5layout, info.new_layout);
248 printf(" New Layout : %s\n",
249 c?c:"-unknown-");
251 if (info.new_level == 10) {
252 printf(" New Layout : near=%d, %s=%d\n",
253 info.new_layout&255,
254 (info.new_layout&0x10000)?"offset":"far",
255 (info.new_layout>>8)&255);
258 if (info.new_chunk != array.chunk_size)
259 printf(" New Chunksize : %dK\n", info.new_chunk/1024);
260 printf("\n");
261 } else if (e && e->percent >= 0)
262 printf("\n");
263 if (super && st)
264 st->ss->detail_super(super, homehost);
266 printf(" Number Major Minor RaidDevice State\n");
268 disks = malloc(max_disks * sizeof(mdu_disk_info_t));
269 for (d=0; d<max_disks; d++) {
270 disks[d].state = (1<<MD_DISK_REMOVED);
271 disks[d].major = disks[d].minor = 0;
272 disks[d].number = disks[d].raid_disk = d;
275 next = array.raid_disks;
276 for (d=0; d < max_disks; d++) {
277 mdu_disk_info_t disk;
278 disk.number = d;
279 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
280 if (d < array.raid_disks)
281 fprintf(stderr, Name ": cannot get device detail for device %d: %s\n",
282 d, strerror(errno));
283 continue;
285 if (disk.major == 0 && disk.minor == 0)
286 continue;
287 if (disk.raid_disk >= 0 && disk.raid_disk < array.raid_disks)
288 disks[disk.raid_disk] = disk;
289 else if (next < max_disks)
290 disks[next++] = disk;
293 for (d= 0; d < max_disks; d++) {
294 char *dv;
295 mdu_disk_info_t disk = disks[d];
297 if (d >= array.raid_disks &&
298 disk.major == 0 &&
299 disk.minor == 0)
300 continue;
301 if (!brief) {
302 if (d == array.raid_disks) printf("\n");
303 if (disk.raid_disk < 0)
304 printf(" %5d %5d %5d - ",
305 disk.number, disk.major, disk.minor);
306 else
307 printf(" %5d %5d %5d %5d ",
308 disk.number, disk.major, disk.minor, disk.raid_disk);
309 if (disk.state & (1<<MD_DISK_FAULTY)) {
310 printf(" faulty");
311 if (disk.raid_disk < array.raid_disks &&
312 disk.raid_disk >= 0)
313 failed++;
315 if (disk.state & (1<<MD_DISK_ACTIVE)) printf(" active");
316 if (disk.state & (1<<MD_DISK_SYNC)) printf(" sync");
317 if (disk.state & (1<<MD_DISK_REMOVED)) printf(" removed");
318 if (disk.state & (1<<MD_DISK_WRITEMOSTLY)) printf(" writemostly");
319 if ((disk.state &
320 ((1<<MD_DISK_ACTIVE)|(1<<MD_DISK_SYNC)|(1<<MD_DISK_REMOVED)))
321 == 0) {
322 printf(" spare");
323 if (is_26) {
324 if (disk.raid_disk < array.raid_disks && disk.raid_disk >= 0)
325 printf(" rebuilding");
326 } else if (is_rebuilding && failed) {
327 /* Taking a bit of a risk here, we remove the
328 * device from the array, and then put it back.
329 * If this fails, we are rebuilding
331 int err = ioctl(fd, HOT_REMOVE_DISK, makedev(disk.major, disk.minor));
332 if (err == 0) ioctl(fd, HOT_ADD_DISK, makedev(disk.major, disk.minor));
333 if (err && errno == EBUSY)
334 printf(" rebuilding");
338 if (disk.state == 0) spares++;
339 if (test && d < array.raid_disks && disk.state & (1<<MD_DISK_FAULTY)) {
340 if ((rv & 1) && (array.level ==4 || array.level == 5))
341 rv |= 2;
342 rv |= 1;
344 if ((dv=map_dev(disk.major, disk.minor, 0))) {
345 if (brief) {
346 if (devices) {
347 devices = realloc(devices,
348 strlen(devices)+1+strlen(dv)+1);
349 strcat(strcat(devices,","),dv);
350 } else
351 devices = strdup(dv);
352 } else
353 printf(" %s", dv);
355 if (!brief) printf("\n");
357 if (spares && brief) printf(" spares=%d", spares);
358 if (super && brief && st)
359 st->ss->brief_detail_super(super);
361 if (brief > 1 && devices) printf("\n devices=%s", devices);
362 if (brief) printf("\n");
363 if (test && (rv&2)) rv &= ~1;
364 close(fd);
365 return rv;