]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Examine.c
sysfs: Use the presence of /sys/block/<dev>/md as indicator of valid device
[thirdparty/mdadm.git] / Examine.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2013 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 "dlink.h"
27
28 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
29 #error no endian defined
30 #endif
31 #include "md_u.h"
32 #include "md_p.h"
33 int Examine(struct mddev_dev *devlist,
34 struct context *c,
35 struct supertype *forcest)
36 {
37
38 /* Read the raid superblock from a device and
39 * display important content.
40 *
41 * If cannot be found, print reason: too small, bad magic
42 *
43 * Print:
44 * version, ctime, level, size, raid+spare+
45 * prefered minor
46 * uuid
47 *
48 * utime, state etc
49 *
50 * If (brief) gather devices for same array and just print a mdadm.conf
51 * line including devices=
52 * if devlist==NULL, use conf_get_devs()
53 */
54 int fd;
55 int rv = 0;
56
57 struct array {
58 struct supertype *st;
59 struct mdinfo info;
60 void *devs;
61 struct array *next;
62 int spares;
63 } *arrays = NULL;
64
65 for (; devlist ; devlist = devlist->next) {
66 struct supertype *st;
67 int have_container = 0;
68 int err = 0;
69 int container = 0;
70
71 fd = dev_open(devlist->devname, O_RDONLY);
72 if (fd < 0) {
73 if (!c->scan) {
74 pr_err("cannot open %s: %s\n",
75 devlist->devname, strerror(errno));
76 rv = 1;
77 }
78 continue;
79 }
80
81 if (forcest)
82 st = dup_super(forcest);
83 else if (must_be_container(fd)) {
84 /* might be a container */
85 st = super_by_fd(fd, NULL);
86 container = 1;
87 } else
88 st = guess_super(fd);
89 if (st) {
90 err = 1;
91 st->ignore_hw_compat = 1;
92 if (!container)
93 err = st->ss->load_super(st, fd,
94 (c->brief||c->scan) ? NULL
95 :devlist->devname);
96 if (err && st->ss->load_container) {
97 err = st->ss->load_container(st, fd,
98 (c->brief||c->scan) ? NULL
99 :devlist->devname);
100 if (!err)
101 have_container = 1;
102 }
103 st->ignore_hw_compat = 0;
104 } else {
105 if (!c->brief) {
106 pr_err("No md superblock detected on %s.\n", devlist->devname);
107 rv = 1;
108 }
109 err = 1;
110 }
111 close(fd);
112
113 if (err) {
114 if (st)
115 st->ss->free_super(st);
116 continue;
117 }
118
119 if (c->SparcAdjust)
120 st->ss->update_super(st, NULL, "sparc2.2",
121 devlist->devname, 0, 0, NULL);
122 /* Ok, its good enough to try, though the checksum could be wrong */
123
124 if (c->brief && st->ss->brief_examine_super == NULL) {
125 if (!c->scan)
126 pr_err("No brief listing for %s on %s\n",
127 st->ss->name, devlist->devname);
128 } else if (c->brief) {
129 struct array *ap;
130 char *d;
131 for (ap = arrays; ap; ap = ap->next) {
132 if (st->ss == ap->st->ss &&
133 st->ss->compare_super(ap->st, st) == 0)
134 break;
135 }
136 if (!ap) {
137 ap = xmalloc(sizeof(*ap));
138 ap->devs = dl_head();
139 ap->next = arrays;
140 ap->spares = 0;
141 ap->st = st;
142 arrays = ap;
143 st->ss->getinfo_super(st, &ap->info, NULL);
144 } else
145 st->ss->getinfo_super(st, &ap->info, NULL);
146 if (!have_container &&
147 !(ap->info.disk.state & (1<<MD_DISK_SYNC)))
148 ap->spares++;
149 d = dl_strdup(devlist->devname);
150 dl_add(ap->devs, d);
151 } else if (c->export) {
152 if (st->ss->export_examine_super)
153 st->ss->export_examine_super(st);
154 st->ss->free_super(st);
155 } else {
156 printf("%s:\n",devlist->devname);
157 st->ss->examine_super(st, c->homehost);
158 st->ss->free_super(st);
159 }
160 }
161 if (c->brief) {
162 struct array *ap;
163 for (ap = arrays; ap; ap = ap->next) {
164 char sep='=';
165 char *d;
166 int newline = 0;
167
168 ap->st->ss->brief_examine_super(ap->st, c->verbose > 0);
169 if (ap->spares)
170 newline += printf(" spares=%d", ap->spares);
171 if (c->verbose > 0) {
172 newline += printf(" devices");
173 for (d = dl_next(ap->devs);
174 d != ap->devs;
175 d=dl_next(d)) {
176 printf("%c%s", sep, d);
177 sep=',';
178 }
179 }
180 if (ap->st->ss->brief_examine_subarrays) {
181 if (newline)
182 printf("\n");
183 ap->st->ss->brief_examine_subarrays(ap->st, c->verbose);
184 }
185 ap->st->ss->free_super(ap->st);
186 /* FIXME free ap */
187 if (ap->spares || c->verbose > 0)
188 printf("\n");
189 }
190 }
191 return rv;
192 }
193
194 int ExamineBadblocks(char *devname, int brief, struct supertype *forcest)
195 {
196 int fd = dev_open(devname, O_RDONLY);
197 struct supertype *st = forcest;
198 int err = 1;
199
200 if (fd < 0) {
201 pr_err("cannot open %s: %s\n", devname, strerror(errno));
202 return 1;
203 }
204 if (!st)
205 st = guess_super(fd);
206 if (!st) {
207 if (!brief)
208 pr_err("No md superblock detected on %s\n", devname);
209 goto out;
210 }
211 if (!st->ss->examine_badblocks) {
212 pr_err("%s metadata does not support badblocks\n", st->ss->name);
213 goto out;
214 }
215 err = st->ss->load_super(st, fd, brief ? NULL : devname);
216 if (err)
217 goto out;
218 err = st->ss->examine_badblocks(st, fd, devname);
219
220 out:
221 if (fd >= 0)
222 close(fd);
223 if (st) {
224 st->ss->free_super(st);
225 free(st);
226 }
227 return err;
228 }