]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Examine.c
Introduce pr_err for printing error messages.
[thirdparty/mdadm.git] / Examine.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-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 "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, int brief, int export, int scan,
34 int SparcAdjust, struct supertype *forcest,
35 char *homehost)
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 line including devices=
51 * if devlist==NULL, use conf_get_devs()
52 */
53 int fd;
54 int rv = 0;
55 int err = 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
69 fd = dev_open(devlist->devname, O_RDONLY);
70 if (fd < 0) {
71 if (!scan) {
72 pr_err("cannot open %s: %s\n",
73 devlist->devname, strerror(errno));
74 rv = 1;
75 }
76 err = 1;
77 }
78 else {
79 int container = 0;
80 if (forcest)
81 st = dup_super(forcest);
82 else if (must_be_container(fd)) {
83 /* might be a container */
84 st = super_by_fd(fd, NULL);
85 container = 1;
86 } else
87 st = guess_super(fd);
88 if (st) {
89 err = 1;
90 st->ignore_hw_compat = 1;
91 if (!container)
92 err = st->ss->load_super(st, fd,
93 (brief||scan) ? NULL
94 :devlist->devname);
95 if (err && st->ss->load_container) {
96 err = st->ss->load_container(st, fd,
97 (brief||scan) ? NULL
98 :devlist->devname);
99 if (!err)
100 have_container = 1;
101 }
102 st->ignore_hw_compat = 0;
103 } else {
104 if (!brief) {
105 pr_err("No md superblock detected on %s.\n", devlist->devname);
106 rv = 1;
107 }
108 err = 1;
109 }
110 close(fd);
111 }
112 if (err)
113 continue;
114
115 if (SparcAdjust)
116 st->ss->update_super(st, NULL, "sparc2.2",
117 devlist->devname, 0, 0, NULL);
118 /* Ok, its good enough to try, though the checksum could be wrong */
119
120 if (brief && st->ss->brief_examine_super == NULL) {
121 if (!scan)
122 pr_err("No brief listing for %s on %s\n",
123 st->ss->name, devlist->devname);
124 } else if (brief) {
125 struct array *ap;
126 char *d;
127 for (ap=arrays; ap; ap=ap->next) {
128 if (st->ss == ap->st->ss &&
129 st->ss->compare_super(ap->st, st)==0)
130 break;
131 }
132 if (!ap) {
133 ap = malloc(sizeof(*ap));
134 ap->devs = dl_head();
135 ap->next = arrays;
136 ap->spares = 0;
137 ap->st = st;
138 arrays = ap;
139 st->ss->getinfo_super(st, &ap->info, NULL);
140 } else
141 st->ss->getinfo_super(st, &ap->info, NULL);
142 if (!have_container &&
143 !(ap->info.disk.state & (1<<MD_DISK_SYNC)))
144 ap->spares++;
145 d = dl_strdup(devlist->devname);
146 dl_add(ap->devs, d);
147 } else if (export) {
148 if (st->ss->export_examine_super)
149 st->ss->export_examine_super(st);
150 st->ss->free_super(st);
151 } else {
152 printf("%s:\n",devlist->devname);
153 st->ss->examine_super(st, homehost);
154 st->ss->free_super(st);
155 }
156 }
157 if (brief) {
158 struct array *ap;
159 for (ap=arrays; ap; ap=ap->next) {
160 char sep='=';
161 char *d;
162 int newline = 0;
163
164 ap->st->ss->brief_examine_super(ap->st, brief > 1);
165 if (ap->spares)
166 newline += printf(" spares=%d", ap->spares);
167 if (brief > 1) {
168 newline += printf(" devices");
169 for (d=dl_next(ap->devs); d!= ap->devs; d=dl_next(d)) {
170 printf("%c%s", sep, d);
171 sep=',';
172 }
173 }
174 if (ap->st->ss->brief_examine_subarrays) {
175 if (newline)
176 printf("\n");
177 ap->st->ss->brief_examine_subarrays(ap->st, brief > 1);
178 }
179 ap->st->ss->free_super(ap->st);
180 /* FIXME free ap */
181 if (ap->spares || brief > 1)
182 printf("\n");
183 }
184 }
185 return rv;
186 }