]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Query.c
Separate sueprblock handling into separate file
[thirdparty/mdadm.git] / Query.c
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
34 int 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 struct mdinfo info;
45 mdu_array_info_t array;
46 void *super;
47
48 unsigned long long larray_size;
49 unsigned long array_size;
50 struct stat stb;
51 char *mddev;
52 mdu_disk_info_t disc;
53 char *activity;
54
55 if (fd < 0){
56 fprintf(stderr, Name ": cannot open %s: %s\n",
57 dev, strerror(errno));
58 return 1;
59 }
60
61 vers = md_get_version(fd);
62 if (ioctl(fd, GET_ARRAY_INFO, &array)<0)
63 ioctlerr = errno;
64 else ioctlerr = 0;
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
74 if (ioctl(fd, BLKGETSIZE, &array_size)==0) {
75 larray_size = array_size;
76 larray_size <<= 9;
77 } else larray_size = 0;
78 }
79
80 if (vers < 0)
81 printf("%s: is not an md array\n", dev);
82 else if (vers < 9000)
83 printf("%s: is an md device, but kernel cannot provide details\n", dev);
84 else if (ioctlerr == ENODEV)
85 printf("%s: is an md device which is not active\n", dev);
86 else if (ioctlerr)
87 printf("%s: is an md device, but gives \"%s\" when queried\n",
88 dev, strerror(ioctlerr));
89 else {
90 printf("%s: %s %s %d devices, %d spare%s. Use mdadm --detail for more detail.\n",
91 dev,
92 human_size_brief(larray_size),
93 map_num(pers, array.level),
94 array.raid_disks,
95 array.spare_disks, array.spare_disks==1?"":"s");
96 }
97 superror = load_super0(fd, &super, dev);
98 superrno = errno;
99 close(fd);
100 if (superror == 0) {
101 /* array might be active... */
102 getinfo_super0(&info, super);
103 mddev = get_md_name(info.array.md_minor);
104 disc.number = info.disk.number;
105 activity = "undetected";
106 if (mddev && (fd = open(mddev, O_RDONLY))>=0) {
107 if (md_get_version(fd) >= 9000 &&
108 ioctl(fd, GET_ARRAY_INFO, &array)>= 0) {
109 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0 &&
110 makedev((unsigned)disc.major,(unsigned)disc.minor) == stb.st_rdev)
111 activity = "active";
112 else
113 activity = "mismatch";
114 }
115 close(fd);
116 }
117 printf("%s: device %d in %d device %s %s md%d. Use mdadm --examine for more detail.\n",
118 dev,
119 info.disk.number, info.array.raid_disks,
120 activity,
121 map_num(pers, info.array.level),
122 info.array.md_minor);
123 }
124 return 0;
125 }
126
127