mdadm-1.6.0
[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
51         mdp_super_t super;
52         int have_super = 0;
53         int rv = test ? 4 : 1;
54
55         if (fd < 0) {
56                 fprintf(stderr, Name ": cannot open %s: %s\n",
57                         dev, strerror(errno));
58                 return rv;
59         }
60         vers = md_get_version(fd);
61         if (vers < 0) {
62                 fprintf(stderr, Name ": %s does not appear to be an md device\n",
63                         dev);
64                 close(fd);
65                 return rv;
66         }
67         if (vers < 9000) {
68                 fprintf(stderr, Name ": cannot get detail for md device %s: driver version too old.\n",
69                         dev);
70                 close(fd);
71                 return rv;
72         }
73         if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
74                 if (errno == ENODEV)
75                         fprintf(stderr, Name ": md device %s does not appear to be active.\n",
76                                 dev);
77                 else
78                         fprintf(stderr, Name ": cannot get array detail for %s: %s\n",
79                                 dev, strerror(errno));
80                 close(fd);
81                 return rv;
82         }
83         if (fstat(fd, &stb) != 0 && !S_ISBLK(stb.st_mode))
84                 stb.st_rdev = 0;
85         rv = 0;
86         /* Ok, we have some info to print... */
87         c = map_num(pers, array.level);
88         if (brief) 
89                 printf("ARRAY %s level=%s num-devices=%d", dev, c?c:"-unknown-",array.raid_disks );
90         else {
91                 unsigned long array_size;
92                 unsigned long long larray_size;
93                 struct mdstat_ent *ms = mdstat_read(0);
94                 struct mdstat_ent *e;
95                 int devnum = array.md_minor;
96                 if (MAJOR(stb.st_rdev) != MD_MAJOR)
97                         devnum = -1 - devnum;
98
99                 for (e=ms; e; e=e->next)
100                         if (e->devnum == devnum)
101                                 break;
102 #ifdef BLKGETSIZE64
103                 if (ioctl(fd, BLKGETSIZE64, &larray_size)==0)
104                         ;
105                 else
106 #endif
107                         if (ioctl(fd, BLKGETSIZE, &array_size)==0) {
108                                 larray_size = array_size;
109                                 larray_size <<= 9;
110                         }
111                 
112                 else larray_size = 0;
113
114                 printf("%s:\n", dev);
115                 printf("        Version : %02d.%02d.%02d\n",
116                        array.major_version, array.minor_version, array.patch_version);
117                 atime = array.ctime;
118                 printf("  Creation Time : %.24s\n", ctime(&atime));
119                 printf("     Raid Level : %s\n", c?c:"-unknown-");
120                 if (larray_size)
121                         printf("     Array Size : %llu%s\n", (larray_size>>10), human_size(larray_size));
122                 if (array.level >= 1)
123                         printf("    Device Size : %d%s\n", array.size, human_size((long long)array.size<<10));
124                 printf("   Raid Devices : %d\n", array.raid_disks);
125                 printf("  Total Devices : %d\n", array.nr_disks);
126                 printf("Preferred Minor : %d\n", array.md_minor);
127                 printf("    Persistence : Superblock is %spersistent\n",
128                        array.not_persistent?"not ":"");
129                 printf("\n");
130                 atime = array.utime;
131                 printf("    Update Time : %.24s\n", ctime(&atime));
132                 printf("          State : %s%s%s\n",
133                        (array.state&(1<<MD_SB_CLEAN))?"clean":"dirty",
134                        array.active_disks < array.raid_disks? ", degraded":"",
135                        (e && e->percent >= 0) ? ", recovering": "");
136                 printf(" Active Devices : %d\n", array.active_disks);
137                 printf("Working Devices : %d\n", array.working_disks);
138                 printf(" Failed Devices : %d\n", array.failed_disks);
139                 printf("  Spare Devices : %d\n", array.spare_disks);
140                 printf("\n");
141                 if (array.level == 5) {
142                         c = map_num(r5layout, array.layout);
143                         printf("         Layout : %s\n", c?c:"-unknown-");
144                 }
145                 switch (array.level) {
146                 case 0:
147                 case 4:
148                 case 5:
149                         printf("     Chunk Size : %dK\n", array.chunk_size/1024);
150                         break;
151                 case -1:
152                         printf("       Rounding : %dK\n", array.chunk_size/1024);
153                         break;
154                 default: break;
155                 }
156         
157                 printf("\n");
158
159                 if (e && e->percent >= 0)
160                         printf(" Rebuild Status : %d%% complete\n\n", e->percent);
161                 free_mdstat(ms);
162
163                 printf("    Number   Major   Minor   RaidDevice State\n");
164         }
165         for (d= 0; d<MD_SB_DISKS; d++) {
166                 mdu_disk_info_t disk;
167                 char *dv;
168                 disk.number = d;
169                 if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
170                         if (d < array.raid_disks)
171                                 fprintf(stderr, Name ": cannot get device detail for device %d: %s\n",
172                                         d, strerror(errno));
173                         continue;
174                 }
175                 if (d >= array.raid_disks &&
176                     disk.major == 0 &&
177                     disk.minor == 0)
178                         continue;
179                 if (!brief) {
180                         printf("   %5d   %5d    %5d    %5d     ", 
181                                disk.number, disk.major, disk.minor, disk.raid_disk);
182                         if (disk.state & (1<<MD_DISK_FAULTY)) printf(" faulty");
183                         if (disk.state & (1<<MD_DISK_ACTIVE)) printf(" active");
184                         if (disk.state & (1<<MD_DISK_SYNC)) printf(" sync");
185                         if (disk.state & (1<<MD_DISK_REMOVED)) printf(" removed");
186                         if (disk.state == 0) { printf(" spare"); spares++; }
187                 }
188                 if (test && d < array.raid_disks && disk.state & (1<<MD_DISK_FAULTY)) {
189                         if ((rv & 1) && (array.level ==4 || array.level == 5))
190                                 rv |= 2;
191                         rv |= 1;
192                 }
193                 if ((dv=map_dev(disk.major, disk.minor))) {
194                         if (brief) {
195                                 if (devices) {
196                                         devices = realloc(devices,
197                                                           strlen(devices)+1+strlen(dv)+1);
198                                         strcat(strcat(devices,","),dv);
199                                 } else
200                                         devices = strdup(dv);
201                         } else
202                                 printf("   %s", dv);
203                         if (!have_super && (disk.state & (1<<MD_DISK_ACTIVE))) {
204                                 /* try to read the superblock from this device
205                                  * to get more info
206                                  */
207                                 int fd = open(dv, O_RDONLY);
208                                 if (fd >=0 &&
209                                     load_super(fd, &super) ==0 &&
210                                     (unsigned long)super.ctime == (unsigned long)array.ctime &&
211                                     (unsigned int)super.level == (unsigned int)array.level)
212                                         have_super = 1;
213                         }
214                 }
215                 if (!brief) printf("\n");
216         }
217         if (spares && brief) printf(" spares=%d", spares);
218         if (have_super) {
219                 if (brief) printf(" UUID=");
220                 else printf("           UUID : ");
221                 if (super.minor_version >= 90)
222                         printf("%08x:%08x:%08x:%08x", super.set_uuid0, super.set_uuid1,
223                                super.set_uuid2, super.set_uuid3);
224                 else
225                         printf("%08x", super.set_uuid0);
226                 if (!brief) 
227                         printf("\n         Events : %d.%d\n", super.events_hi, super.events_lo);
228         }
229         if (brief && devices) printf("\n   devices=%s", devices);
230         if (brief) printf("\n");
231         if (test && (rv&2)) rv &= ~1;
232         return rv;
233 }