]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Examine.c
Add must_be_container helper.
[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 fprintf(stderr,Name ": 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 if (!container)
91 err = st->ss->load_super(st, fd,
92 (brief||scan) ? NULL
93 :devlist->devname);
94 if (err && st->ss->load_container) {
95 err = st->ss->load_container(st, fd,
96 (brief||scan) ? NULL
97 :devlist->devname);
98 if (!err)
99 have_container = 1;
100 }
101 } else {
102 if (!brief) {
103 fprintf(stderr, Name ": No md superblock detected on %s.\n", devlist->devname);
104 rv = 1;
105 }
106 err = 1;
107 }
108 close(fd);
109 }
110 if (err)
111 continue;
112
113 if (SparcAdjust)
114 st->ss->update_super(st, NULL, "sparc2.2",
115 devlist->devname, 0, 0, NULL);
116 /* Ok, its good enough to try, though the checksum could be wrong */
117
118 if (brief && st->ss->brief_examine_super == NULL) {
119 if (!scan)
120 fprintf(stderr, Name ": No brief listing for %s on %s\n",
121 st->ss->name, devlist->devname);
122 } else if (brief) {
123 struct array *ap;
124 char *d;
125 for (ap=arrays; ap; ap=ap->next) {
126 if (st->ss == ap->st->ss &&
127 st->ss->compare_super(ap->st, st)==0)
128 break;
129 }
130 if (!ap) {
131 ap = malloc(sizeof(*ap));
132 ap->devs = dl_head();
133 ap->next = arrays;
134 ap->spares = 0;
135 ap->st = st;
136 arrays = ap;
137 st->ss->getinfo_super(st, &ap->info, NULL);
138 } else
139 st->ss->getinfo_super(st, &ap->info, NULL);
140 if (!have_container &&
141 !(ap->info.disk.state & (1<<MD_DISK_SYNC)))
142 ap->spares++;
143 d = dl_strdup(devlist->devname);
144 dl_add(ap->devs, d);
145 } else if (export) {
146 if (st->ss->export_examine_super)
147 st->ss->export_examine_super(st);
148 } else {
149 printf("%s:\n",devlist->devname);
150 st->ss->examine_super(st, homehost);
151 st->ss->free_super(st);
152 }
153 }
154 if (brief) {
155 struct array *ap;
156 for (ap=arrays; ap; ap=ap->next) {
157 char sep='=';
158 char *d;
159 int newline = 0;
160
161 ap->st->ss->brief_examine_super(ap->st, brief > 1);
162 if (ap->spares)
163 newline += printf(" spares=%d", ap->spares);
164 if (brief > 1) {
165 newline += printf(" devices");
166 for (d=dl_next(ap->devs); d!= ap->devs; d=dl_next(d)) {
167 printf("%c%s", sep, d);
168 sep=',';
169 }
170 }
171 if (ap->st->ss->brief_examine_subarrays) {
172 if (newline)
173 printf("\n");
174 ap->st->ss->brief_examine_subarrays(ap->st, brief > 1);
175 }
176 ap->st->ss->free_super(ap->st);
177 /* FIXME free ap */
178 if (ap->spares || brief > 1)
179 printf("\n");
180 }
181 }
182 return rv;
183 }