Add 'supertype' arg to almost all metadata methods.
[thirdparty/mdadm.git] / Query.c
CommitLineData
e0d19036
NB
1/*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4f589ad0 4 * Copyright (C) 2002-2006 Neil Brown <neilb@suse.de>
e0d19036
NB
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,
aba69144 37 * whether it is an md device and whether it has
e0d19036
NB
38 * a superblock
39 */
40 int fd = open(dev, O_RDONLY, 0);
41 int vers;
42 int ioctlerr;
43 int superror, superrno;
4b1ac34b 44 struct mdinfo info;
e0d19036 45 mdu_array_info_t array;
4b1ac34b 46 void *super;
82d9eba6 47 struct supertype *st = NULL;
4b1ac34b 48
e0d19036 49 unsigned long long larray_size;
e0d19036
NB
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;
aba69144 65
e0d19036
NB
66 fstat(fd, &stb);
67
68 if (vers>=9000 && !ioctlerr) {
beae1dfe
NB
69 if (!get_dev_size(fd, NULL, &larray_size))
70 larray_size = 0;
e0d19036 71 }
e0d19036 72
aba69144 73 if (vers < 0)
e0d19036
NB
74 printf("%s: is not an md array\n", dev);
75 else if (vers < 9000)
76 printf("%s: is an md device, but kernel cannot provide details\n", dev);
77 else if (ioctlerr == ENODEV)
78 printf("%s: is an md device which is not active\n", dev);
79 else if (ioctlerr)
80 printf("%s: is an md device, but gives \"%s\" when queried\n",
81 dev, strerror(ioctlerr));
82 else {
83 printf("%s: %s %s %d devices, %d spare%s. Use mdadm --detail for more detail.\n",
84 dev,
85 human_size_brief(larray_size),
86 map_num(pers, array.level),
87 array.raid_disks,
88 array.spare_disks, array.spare_disks==1?"":"s");
89 }
82d9eba6
NB
90 st = guess_super(fd);
91 if (st) {
92 superror = st->ss->load_super(st, fd, &super, dev);
f9ce90ba 93 superrno = errno;
aba69144 94 } else
f9ce90ba 95 superror = -1;
4b1ac34b
NB
96 close(fd);
97 if (superror == 0) {
e0d19036 98 /* array might be active... */
68c7d6d7 99 st->ss->getinfo_super(st, &info, super);
fbf8a0b7
NB
100 if (st->ss->major == 0) {
101 mddev = get_md_name(info.array.md_minor);
102 disc.number = info.disk.number;
103 activity = "undetected";
104 if (mddev && (fd = open(mddev, O_RDONLY))>=0) {
aba69144 105 if (md_get_version(fd) >= 9000 &&
fbf8a0b7
NB
106 ioctl(fd, GET_ARRAY_INFO, &array)>= 0) {
107 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0 &&
108 makedev((unsigned)disc.major,(unsigned)disc.minor) == stb.st_rdev)
109 activity = "active";
110 else
111 activity = "mismatch";
112 }
113 close(fd);
e0d19036 114 }
fbf8a0b7
NB
115 } else {
116 activity = "unknown";
117 mddev = "array";
e0d19036 118 }
fbf8a0b7 119 printf("%s: device %d in %d device %s %s %s. Use mdadm --examine for more detail.\n",
aba69144 120 dev,
4b1ac34b 121 info.disk.number, info.array.raid_disks,
e0d19036 122 activity,
4b1ac34b 123 map_num(pers, info.array.level),
fbf8a0b7 124 mddev);
e7bb5d23
NB
125 if (st->ss->major == 0)
126 put_md_name(mddev);
e0d19036
NB
127 }
128 return 0;
129}
130