mdadm-1.0.1
[thirdparty/mdadm.git] / Examine.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        "dlink.h"
32
33 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
34 #error no endian defined
35 #endif
36 #include        "md_u.h"
37 #include        "md_p.h"
38 int Examine(mddev_dev_t devlist, int brief, int scan, int SparcAdjust)
39 {
40
41         /* Read the raid superblock from a device and
42          * display important content.
43          *
44          * If cannot be found, print reason: too small, bad magic
45          *
46          * Print:
47          *   version, ctime, level, size, raid+spare+
48          *   prefered minor
49          *   uuid
50          *
51          *   utime, state etc
52          *
53          * If (brief) gather devices for same array and just print a mdadm.conf line including devices=
54          * if devlist==NULL, use conf_get_devs(
55          */
56         int fd; 
57         time_t atime;
58         mdp_super_t super;
59         int d;
60         char *c;
61         int rv = 0;
62         int err;
63
64         struct array {
65                 mdp_super_t super;
66                 void *devs;
67                 struct array *next;
68         } *arrays = NULL;
69
70         for (; devlist ; devlist=devlist->next) {
71                 fd = open(devlist->devname, O_RDONLY);
72                 if (fd < 0) {
73                         if (!scan)
74                                 fprintf(stderr,Name ": cannot open %s: %s\n",
75                                         devlist->devname, strerror(errno));
76                         err = 1;
77                 }
78                 else {
79                         err = load_super(fd, &super);
80                         close(fd);
81                 }
82                 if (err && (brief||scan))
83                         continue;
84                 if (err) rv =1;
85                 switch(err) {
86                 case 1:
87                         fprintf(stderr, Name ": cannot find device size for %s: %s\n",
88                                 devlist->devname, strerror(errno));
89                         continue;
90                 case 2:
91 /*              fprintf(stderr, Name ": %s is too small for md: size is %ld sectors\n",
92                 devlist->devname, size);
93 */
94                         fprintf(stderr, Name ": %s is too small for md\n",
95                                 devlist->devname);
96                         continue;
97                 case 3:
98                         fprintf(stderr, Name ": Cannot seek to superblock on %s: %s\n",
99                                 devlist->devname, strerror(errno));
100                         continue;
101                 case 4:
102                         fprintf(stderr, Name ": Cannot read superblock on %s\n",
103                                 devlist->devname);
104                         continue;
105                 case 5:
106                         fprintf(stderr, Name ": No super block found on %s (Expected magic %08x, got %08x)\n",
107                                 devlist->devname, MD_SB_MAGIC, super.md_magic);
108                         continue;
109                 case 6:
110                         fprintf(stderr, Name ": Cannot interpret superblock on %s - version is %d\n",
111                                 devlist->devname, super.major_version);
112                         continue;
113                 }
114     
115                 /* Ok, its good enough to try, though the checksum could be wrong */
116                 if (brief) {
117                         struct array *ap;
118                         char *d;
119                         for (ap=arrays; ap; ap=ap->next) {
120                                 if (compare_super(&ap->super, &super)==0)
121                                         break;
122                         }
123                         if (!ap) {
124                                 ap = malloc(sizeof(*ap));
125                                 ap->super = super;
126                                 ap->devs = dl_head();
127                                 ap->next = arrays;
128                                 arrays = ap;
129                         }
130                         d = dl_strdup(devlist->devname);
131                         dl_add(ap->devs, d);
132                 } else {
133                         printf("%s:\n",devlist->devname);
134                         printf("          Magic : %08x\n", super.md_magic);
135                         printf("        Version : %02d.%02d.%02d\n", super.major_version, super.minor_version,
136                                super.patch_version);
137                         if (super.minor_version >= 90)
138                                 printf("           UUID : %08x:%08x:%08x:%08x\n", super.set_uuid0, super.set_uuid1,
139                                        super.set_uuid2, super.set_uuid3);
140                         else
141                                 printf("           UUID : %08x\n", super.set_uuid0);
142
143                         atime = super.ctime;
144                         printf("  Creation Time : %.24s\n", ctime(&atime));
145                         c=map_num(pers, super.level);
146                         printf("     Raid Level : %s\n", c?c:"-unknown-");
147                         printf("    Device Size : %d%s\n", super.size, human_size((long long)super.size<<10));
148                         printf("   Raid Devices : %d\n", super.raid_disks);
149                         printf("  Total Devices : %d\n", super.nr_disks);
150                         printf("Preferred Minor : %d\n", super.md_minor);
151                         printf("\n");
152                         atime = super.utime;
153                         printf("    Update Time : %.24s\n", ctime(&atime));
154                         printf("          State : %s, %serrors\n",
155                                (super.state&(1<<MD_SB_CLEAN))?"clean":"dirty",
156                                (super.state&(1<<MD_SB_ERRORS))?"":"no-");
157                         printf(" Active Devices : %d\n", super.active_disks);
158                         printf("Working Devices : %d\n", super.working_disks);
159                         printf(" Failed Devices : %d\n", super.failed_disks);
160                         printf("  Spare Devices : %d\n", super.spare_disks);
161                         if (calc_sb_csum(&super) == super.sb_csum)
162                                 printf("       Checksum : %x - correct\n", super.sb_csum);
163                         else
164                                 printf("       Checksum : %x - expected %x\n", super.sb_csum, calc_sb_csum(&super));
165                         if (SparcAdjust) {
166                                 /* 2.2 sparc put the events in the wrong place
167                                  * So we copy the tail of the superblock
168                                  * up 4 bytes before continuing
169                                  */
170                                 __u32 *sb32 = (__u32*)&super;
171                                 memcpy(sb32+MD_SB_GENERIC_CONSTANT_WORDS+7,
172                                        sb32+MD_SB_GENERIC_CONSTANT_WORDS+7+1,
173                                        MD_SB_WORDS - (MD_SB_GENERIC_CONSTANT_WORDS+7+1));
174                                 printf (" --- adjusting superblock for 2.2/sparc compatability ---\n");
175                         }
176                         printf("         Events : %d.%d\n", super.events_hi, super.events_lo);
177                         printf("\n");
178                         if (super.level == 5) {
179                                 c = map_num(r5layout, super.layout);
180                                 printf("         Layout : %s\n", c?c:"-unknown-");
181                         }
182                         switch(super.level) {
183                         case 0:
184                         case 4:
185                         case 5:
186                                 printf("     Chunk Size : %dK\n", super.chunk_size/1024);
187                                 break;
188                         case -1:
189                                 printf("       Rounding : %dK\n", super.chunk_size/1024);
190                                 break;
191                         default: break;         
192                         }
193                         printf("\n");
194                         printf("      Number   Major   Minor   RaidDevice State\n");
195                         for (d= -1; d<(signed int)(super.raid_disks+super.spare_disks); d++) {
196                                 mdp_disk_t *dp;
197                                 char *dv;
198                                 char nb[5];
199                                 if (d>=0) dp = &super.disks[d];
200                                 else dp = &super.this_disk;
201                                 sprintf(nb, "%4d", d);
202                                 printf("%4s %5d   %5d    %5d    %5d     ", d < 0 ? "this" :  nb,
203                                        dp->number, dp->major, dp->minor, dp->raid_disk);
204                                 if (dp->state & (1<<MD_DISK_FAULTY)) printf(" faulty");
205                                 if (dp->state & (1<<MD_DISK_ACTIVE)) printf(" active");
206                                 if (dp->state & (1<<MD_DISK_SYNC)) printf(" sync");
207                                 if (dp->state & (1<<MD_DISK_REMOVED)) printf(" removed");
208                                 if ((dv=map_dev(dp->major, dp->minor)))
209                                         printf("   %s", dv);
210                                 printf("\n");
211                         }
212                 }
213                 if (SparcAdjust == 2) {
214                         printf(" ----- updating superblock on device ----\n");
215                         fd = open(devlist->devname, O_RDWR);
216                         if (fd < 0) {
217                                 fprintf(stderr, Name ": cannot open %s to update superblock: %s\n",
218                                         devlist->devname, strerror(errno));
219                                 err = 1;
220                         } else {
221                                 super.sb_csum = calc_sb_csum(&super);
222                                 if (store_super(fd, &super)) {
223                                         fprintf(stderr, Name ": Count not re-write superblock on %s\n",
224                                                 devlist->devname);
225                                         err = 1;
226                                 }
227                                 close(fd);
228                         }
229                 }
230         }
231         if (brief) {
232                 struct array *ap;
233                 for (ap=arrays; ap; ap=ap->next) {
234                         char sep='=';
235                         char *c=map_num(pers, ap->super.level);
236                         char *d;
237                         printf("ARRAY /dev/md%d level=%s num-devices=%d UUID=",
238                                ap->super.md_minor, c?c:"-unknown-", ap->super.raid_disks);
239                         if (ap->super.minor_version >= 90)
240                                 printf("%08x:%08x:%08x:%08x", ap->super.set_uuid0, ap->super.set_uuid1,
241                                        ap->super.set_uuid2, ap->super.set_uuid3);
242                         else
243                                 printf("%08x", ap->super.set_uuid0);
244                         printf("\n   devices");
245                         for (d=dl_next(ap->devs); d!= ap->devs; d=dl_next(d)) {
246                                 printf("%c%s", sep, d);
247                                 sep=',';
248                         }
249                         printf("\n");
250                 }
251         }
252         return rv;
253 }