]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Query.c
mdadm-1.0.0
[thirdparty/mdadm.git] / Query.c
CommitLineData
e0d19036
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 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
34int Query(char *dev)
35{
36 /* Give a brief description of the device,
37 * whether it is an md device and whether it has
38 * a superblock
39 */
40 int fd = open(dev, O_RDONLY, 0);
41 int vers;
42 int ioctlerr;
43 int superror, superrno;
44 mdp_super_t super;
45 mdu_array_info_t array;
46 unsigned long long larray_size;
47 unsigned long array_size;
48 struct stat stb;
49 char *mddev;
50 mdu_disk_info_t disc;
51 char *activity;
52
53 if (fd < 0){
54 fprintf(stderr, Name ": cannot open %s: %s\n",
55 dev, strerror(errno));
56 return 1;
57 }
58
59 vers = md_get_version(fd);
60 if (ioctl(fd, GET_ARRAY_INFO, &array)<0)
61 ioctlerr = errno;
62 else ioctlerr = 0;
63 superror = load_super(fd, &super);
64 superrno = errno;
65
66 fstat(fd, &stb);
67
68 if (vers>=9000 && !ioctlerr) {
69#ifdef BLKGETSIZE64
70 if (ioctl(fd, BLKGETSIZE64, &larray_size)==0)
71 ;
72 else
73#endif
b83d95f3
NB
74 if (ioctl(fd, BLKGETSIZE, &array_size)==0) {
75 larray_size = array_size;
76 larray_size <<= 9;
77 } else larray_size = 0;
e0d19036
NB
78 }
79 close(fd);
80
81 if (vers < 0)
82 printf("%s: is not an md array\n", dev);
83 else if (vers < 9000)
84 printf("%s: is an md device, but kernel cannot provide details\n", dev);
85 else if (ioctlerr == ENODEV)
86 printf("%s: is an md device which is not active\n", dev);
87 else if (ioctlerr)
88 printf("%s: is an md device, but gives \"%s\" when queried\n",
89 dev, strerror(ioctlerr));
90 else {
91 printf("%s: %s %s %d devices, %d spare%s. Use mdadm --detail for more detail.\n",
92 dev,
93 human_size_brief(larray_size),
94 map_num(pers, array.level),
95 array.raid_disks,
96 array.spare_disks, array.spare_disks==1?"":"s");
97 }
98 switch(superror) {
99 case 1:
100 printf("%s: cannot find device size: %s\n",
101 dev, strerror(superrno));
102 break;
103 case 2:
104 printf("%s: is too small to be an md componenet.\n",
105 dev);
106 break;
107 case 3:
108 printf("%s: Cannot seek to superblock: %s\n",
109 dev, strerror(superrno));
110 break;
111 case 4:
112 printf("%s: Cannot read md superblock.\n",
113 dev);
114 break;
115 case 5:
116 printf("%s: No md super block found, not an md component.\n",
117 dev);
118 break;
119 case 6:
120 printf("%s: md superblock present with wrong version: %d\n",
121 dev, super.major_version);
122 break;
123 default:
124 /* array might be active... */
125 mddev = get_md_name(super.md_minor);
126 disc.number = super.this_disk.number;
127 activity = "inactive";
128 if (mddev && (fd = open(mddev, O_RDONLY))>=0) {
129 if (md_get_version(fd) >= 9000 &&
130 ioctl(fd, GET_ARRAY_INFO, &array)>= 0) {
131 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0 &&
132 MKDEV(disc.major,disc.minor) == stb.st_rdev)
133 activity = "active";
134 else
135 activity = "mismatch";
136 }
137 close(fd);
138 }
139 printf("%s: device %d in %d device %s %s md%d. Use mdadm --examine for more detail.\n",
140 dev,
141 super.this_disk.number, super.raid_disks,
142 activity,
143 map_num(pers, super.level),
144 super.md_minor);
145 break;
146 }
147 return 0;
148}
149
150