summaryrefslogtreecommitdiffstats
path: root/Detail.c
blob: 5028ae29c40346c0991c4e33a3deb1f1d04a1077 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * mdadm - manage Linux "md" devices aka RAID arrays.
 *
 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
 *
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    Author: Neil Brown
 *    Email: <neilb@cse.unsw.edu.au>
 *    Paper: Neil Brown
 *           School of Computer Science and Engineering
 *           The University of New South Wales
 *           Sydney, 2052
 *           Australia
 */

#include	"mdadm.h"
#include	"md_p.h"
#include	"md_u.h"

int Detail(char *dev, int brief, int test)
{
	/*
	 * Print out details for an md array by using
	 * GET_ARRAY_INFO and GET_DISK_INFO ioctl calls
	 */

	int fd = open(dev, O_RDONLY, 0);
	int vers;
	mdu_array_info_t array;
	int d;
	time_t atime;
	char *c;
	char *devices = NULL;
	int spares = 0;
	struct stat stb;

	mdp_super_t super;
	int have_super = 0;
	int rv = test ? 4 : 1;

	if (fd < 0) {
		fprintf(stderr, Name ": cannot open %s: %s\n",
			dev, strerror(errno));
		return rv;
	}
	vers = md_get_version(fd);
	if (vers < 0) {
		fprintf(stderr, Name ": %s does not appear to be an md device\n",
			dev);
		close(fd);
		return rv;
	}
	if (vers < 9000) {
		fprintf(stderr, Name ": cannot get detail for md device %s: driver version too old.\n",
			dev);
		close(fd);
		return rv;
	}
	if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
		if (errno == ENODEV)
			fprintf(stderr, Name ": md device %s does not appear to be active.\n",
				dev);
		else
			fprintf(stderr, Name ": cannot get array detail for %s: %s\n",
				dev, strerror(errno));
		close(fd);
		return rv;
	}
	if (fstat(fd, &stb) != 0 && !S_ISBLK(stb.st_mode))
		stb.st_rdev = 0;
	rv = 0;
	/* Ok, we have some info to print... */
	c = map_num(pers, array.level);
	if (brief) 
		printf("ARRAY %s level=%s num-devices=%d", dev, c?c:"-unknown-",array.raid_disks );
	else {
		unsigned long array_size;
		unsigned long long larray_size;
		struct mdstat_ent *ms = mdstat_read(0);
		struct mdstat_ent *e;
		int devnum = array.md_minor;
		if (MAJOR(stb.st_rdev) != MD_MAJOR)
			devnum = -1 - devnum;

		for (e=ms; e; e=e->next)
			if (e->devnum == devnum)
				break;
#ifdef BLKGETSIZE64
		if (ioctl(fd, BLKGETSIZE64, &larray_size)==0)
			;
		else
#endif
			if (ioctl(fd, BLKGETSIZE, &array_size)==0) {
				larray_size = array_size;
				larray_size <<= 9;
			}
		
		else larray_size = 0;

		printf("%s:\n", dev);
		printf("        Version : %02d.%02d.%02d\n",
		       array.major_version, array.minor_version, array.patch_version);
		atime = array.ctime;
		printf("  Creation Time : %.24s\n", ctime(&atime));
		printf("     Raid Level : %s\n", c?c:"-unknown-");
		if (larray_size)
			printf("     Array Size : %llu%s\n", (larray_size>>10), human_size(larray_size));
		if (array.level >= 1)
			printf("    Device Size : %d%s\n", array.size, human_size((long long)array.size<<10));
		printf("   Raid Devices : %d\n", array.raid_disks);
		printf("  Total Devices : %d\n", array.nr_disks);
		printf("Preferred Minor : %d\n", array.md_minor);
		printf("    Persistence : Superblock is %spersistent\n",
		       array.not_persistent?"not ":"");
		printf("\n");
		atime = array.utime;
		printf("    Update Time : %.24s\n", ctime(&atime));
		printf("          State : %s%s%s\n",
		       (array.state&(1<<MD_SB_CLEAN))?"clean":"dirty",
		       array.active_disks < array.raid_disks? ", degraded":"",
		       (e && e->percent >= 0) ? ", recovering": "");
		printf(" Active Devices : %d\n", array.active_disks);
		printf("Working Devices : %d\n", array.working_disks);
		printf(" Failed Devices : %d\n", array.failed_disks);
		printf("  Spare Devices : %d\n", array.spare_disks);
		printf("\n");
		if (array.level == 5) {
			c = map_num(r5layout, array.layout);
			printf("         Layout : %s\n", c?c:"-unknown-");
		}
		switch (array.level) {
		case 0:
		case 4:
		case 5:
			printf("     Chunk Size : %dK\n", array.chunk_size/1024);
			break;
		case -1:
			printf("       Rounding : %dK\n", array.chunk_size/1024);
			break;
		default: break;
		}
	
		printf("\n");

		if (e && e->percent >= 0)
			printf(" Rebuild Status : %d%% complete\n\n", e->percent);
		free_mdstat(ms);

		printf("    Number   Major   Minor   RaidDevice State\n");
	}
	for (d= 0; d<MD_SB_DISKS; d++) {
		mdu_disk_info_t disk;
		char *dv;
		disk.number = d;
		if (ioctl(fd, GET_DISK_INFO, &disk) < 0) {
			if (d < array.raid_disks)
				fprintf(stderr, Name ": cannot get device detail for device %d: %s\n",
					d, strerror(errno));
			continue;
		}
		if (d >= array.raid_disks &&
		    disk.major == 0 &&
		    disk.minor == 0)
			continue;
		if (!brief) {
			printf("   %5d   %5d    %5d    %5d     ", 
			       disk.number, disk.major, disk.minor, disk.raid_disk);
			if (disk.state & (1<<MD_DISK_FAULTY)) printf(" faulty");
			if (disk.state & (1<<MD_DISK_ACTIVE)) printf(" active");
			if (disk.state & (1<<MD_DISK_SYNC)) printf(" sync");
			if (disk.state & (1<<MD_DISK_REMOVED)) printf(" removed");
			if (disk.state == 0) { printf(" spare"); spares++; }
		}
		if (test && d < array.raid_disks && disk.state & (1<<MD_DISK_FAULTY)) {
			if ((rv & 1) && (array.level ==4 || array.level == 5))
				rv |= 2;
			rv |= 1;
		}
		if ((dv=map_dev(disk.major, disk.minor))) {
			if (brief) {
				if (devices) {
					devices = realloc(devices,
							  strlen(devices)+1+strlen(dv)+1);
					strcat(strcat(devices,","),dv);
				} else
					devices = strdup(dv);
			} else
				printf("   %s", dv);
			if (!have_super && (disk.state & (1<<MD_DISK_ACTIVE))) {
				/* try to read the superblock from this device
				 * to get more info
				 */
				int fd = open(dv, O_RDONLY);
				if (fd >=0 &&
				    load_super(fd, &super) ==0 &&
				    (unsigned long)super.ctime == (unsigned long)array.ctime &&
				    (unsigned int)super.level == (unsigned int)array.level)
					have_super = 1;
			}
		}
		if (!brief) printf("\n");
	}
	if (spares && brief) printf(" spares=%d", spares);
	if (have_super) {
		if (brief) printf(" UUID=");
		else printf("           UUID : ");
	    	if (super.minor_version >= 90)
			printf("%08x:%08x:%08x:%08x", super.set_uuid0, super.set_uuid1,
			       super.set_uuid2, super.set_uuid3);
		else
			printf("%08x", super.set_uuid0);
		if (!brief) 
			printf("\n         Events : %d.%d\n", super.events_hi, super.events_lo);
	}
	if (brief && devices) printf("\n   devices=%s", devices);
	if (brief) printf("\n");
	if (test && (rv&2)) rv &= ~1;
	return rv;
}