summaryrefslogtreecommitdiffstats
path: root/Query.c
blob: c65d83a03e732f8bebea3ba4b2a7c5a3c43dbc4a (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
/*
 * mdadm - manage Linux "md" devices aka RAID arrays.
 *
 * Copyright (C) 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 Query(char *dev)
{
	/* Give a brief description of the device,
	 * whether it is an md device and whether it has 
	 * a superblock
	 */
	int fd = open(dev, O_RDONLY, 0);
	int vers;
	int ioctlerr;
	int superror, superrno;
	mdp_super_t super;
	mdu_array_info_t array;
	unsigned long long larray_size;
	unsigned long array_size;
	struct stat stb;
	char *mddev;
	mdu_disk_info_t disc;
	char *activity;

	if (fd < 0){
		fprintf(stderr, Name ": cannot open %s: %s\n",
			dev, strerror(errno));
		return 1;
	}

	vers = md_get_version(fd);
	if (ioctl(fd, GET_ARRAY_INFO, &array)<0)
		ioctlerr = errno;
	else ioctlerr = 0;
	superror = load_super(fd, &super);
	superrno = errno;
 
	fstat(fd, &stb);

	if (vers>=9000 && !ioctlerr) {
#ifdef BLKGETSIZE64
		if (ioctl(fd, BLKGETSIZE64, &larray_size)==0)
			;
		else
#endif
			if (ioctl(fd, BLKGETSIZE, &array_size)==0)
				larray_size = array_size<<9;
			else larray_size = 0;
	}
	close(fd);

	if (vers < 0) 
		printf("%s: is not an md array\n", dev);
	else if (vers < 9000)
		printf("%s: is an md device, but kernel cannot provide details\n", dev);
	else if (ioctlerr == ENODEV)
		printf("%s: is an md device which is not active\n", dev);
	else if (ioctlerr)
		printf("%s: is an md device, but gives \"%s\" when queried\n",
		       dev, strerror(ioctlerr));
	else {
		printf("%s: %s %s %d devices, %d spare%s. Use mdadm --detail for more detail.\n",
		       dev,
		       human_size_brief(larray_size),
		       map_num(pers, array.level),
		       array.raid_disks,
		       array.spare_disks, array.spare_disks==1?"":"s");
	}
	switch(superror) {
	case 1:
		printf("%s: cannot find device size: %s\n",
		       dev, strerror(superrno));
		break;
	case 2:
		printf("%s: is too small to be an md componenet.\n",
		       dev);
		break;
	case 3:
		printf("%s: Cannot seek to superblock: %s\n",
		       dev, strerror(superrno));
		break;
	case 4:
		printf("%s: Cannot read md superblock.\n",
		       dev);
		break;
	case 5:
		printf("%s: No md super block found, not an md component.\n",
		       dev);
		break;
	case 6:
		printf("%s: md superblock present with wrong version: %d\n",
		       dev, super.major_version);
		break;
	default:
		/* array might be active... */
		mddev = get_md_name(super.md_minor);
		disc.number = super.this_disk.number;
		activity = "inactive";
		if (mddev && (fd = open(mddev, O_RDONLY))>=0) {
			if (md_get_version(fd) >= 9000 &&	
			    ioctl(fd, GET_ARRAY_INFO, &array)>= 0) {
				if (ioctl(fd, GET_DISK_INFO, &disc) >= 0 &&
				    MKDEV(disc.major,disc.minor) == stb.st_rdev)
					activity = "active";
				else
					activity = "mismatch";
			}
			close(fd);
		}
		printf("%s: device %d in %d device %s %s md%d.  Use mdadm --examine for more detail.\n",
		       dev, 
		       super.this_disk.number, super.raid_disks,
		       activity,
		       map_num(pers, super.level),
		       super.md_minor);
		break;
	}
	return 0;
}