]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Query.c
b761c470b268702d6191f93a2507588c8b68f126
[thirdparty/mdadm.git] / Query.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2002-2009 Neil Brown <neilb@suse.de>
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@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include "md_p.h"
27 #include "md_u.h"
28
29 int Query(char *dev)
30 {
31 /* Give a brief description of the device,
32 * whether it is an md device and whether it has
33 * a superblock
34 */
35 int fd;
36 int ioctlerr, staterr;
37 int superror;
38 int level, raid_disks, spare_disks;
39 struct mdinfo info;
40 struct mdinfo *sra;
41 mdu_array_info_t array;
42 struct supertype *st = NULL;
43 unsigned long long larray_size;
44 struct stat stb;
45 char *mddev;
46 mdu_disk_info_t disc;
47 char *activity;
48
49 fd = open(dev, O_RDONLY);
50 if (fd < 0){
51 pr_err("cannot open %s: %s\n", dev, strerror(errno));
52 return 1;
53 }
54
55 if (fstat(fd, &stb) < 0)
56 staterr = errno;
57 else
58 staterr = 0;
59
60 ioctlerr = 0;
61
62 sra = sysfs_read(fd, dev, GET_DISKS | GET_LEVEL | GET_DEVS | GET_STATE);
63 if (sra) {
64 level = sra->array.level;
65 raid_disks = sra->array.raid_disks;
66 spare_disks = sra->array.spare_disks;
67 } else {
68 if (md_get_array_info(fd, &array) < 0) {
69 ioctlerr = errno;
70 } else {
71 level = array.level;
72 raid_disks = array.raid_disks;
73 spare_disks = array.spare_disks;
74 }
75 }
76
77 if (!ioctlerr && !staterr) {
78 if (!get_dev_size(fd, NULL, &larray_size))
79 larray_size = 0;
80 }
81
82 if (ioctlerr == ENODEV)
83 printf("%s: is an md device which is not active\n", dev);
84 else if (ioctlerr)
85 printf("%s: is an md device, but gives \"%s\" when queried\n",
86 dev, strerror(ioctlerr));
87 else if (staterr)
88 printf("%s: is not a valid md device, returning %s\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, human_size_brief(larray_size,IEC),
93 map_num(pers, level), raid_disks,
94 spare_disks, spare_disks == 1 ? "" : "s");
95 }
96 st = guess_super(fd);
97 if (st && st->ss->compare_super != NULL)
98 superror = st->ss->load_super(st, fd, dev);
99 else
100 superror = -1;
101 close(fd);
102 if (superror == 0) {
103 /* array might be active... */
104 int uuid[4];
105 struct map_ent *me, *map = NULL;
106 st->ss->getinfo_super(st, &info, NULL);
107 st->ss->uuid_from_super(st, uuid);
108 me = map_by_uuid(&map, uuid);
109 if (me) {
110 mddev = me->path;
111 disc.number = info.disk.number;
112 activity = "undetected";
113 if (mddev && (fd = open(mddev, O_RDONLY))>=0) {
114 if (md_get_array_info(fd, &array) >= 0) {
115 if (md_get_disk_info(fd, &disc) >= 0 &&
116 makedev((unsigned)disc.major,(unsigned)disc.minor) == stb.st_rdev)
117 activity = "active";
118 else
119 activity = "mismatch";
120 }
121 close(fd);
122 }
123 } else {
124 activity = "inactive";
125 mddev = "array";
126 }
127 printf("%s: device %d in %d device %s %s %s. Use mdadm --examine for more detail.\n",
128 dev,
129 info.disk.number, info.array.raid_disks,
130 activity,
131 map_num(pers, info.array.level),
132 mddev);
133 if (st->ss == &super0)
134 put_md_name(mddev);
135 }
136 return 0;
137 }