]> git.ipfire.org Git - thirdparty/mdadm.git/blob - sysfs.c
Adjust major number testing to allow for extended minor number in 2.6.28
[thirdparty/mdadm.git] / sysfs.c
1 /*
2 * sysfs - extract md related information from sysfs. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
4 *
5 * Copyright (C) 2006 Neil Brown <neilb@suse.de>
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * Author: Neil Brown
23 * Email: <neilb@suse.de>
24 */
25
26 #include "mdadm.h"
27 #include <dirent.h>
28
29 int load_sys(char *path, char *buf)
30 {
31 int fd = open(path, O_RDONLY);
32 int n;
33 if (fd < 0)
34 return -1;
35 n = read(fd, buf, 1024);
36 close(fd);
37 if (n <=0 || n >= 1024)
38 return -1;
39 buf[n] = 0;
40 if (buf[n-1] == '\n')
41 buf[n-1] = 0;
42 return 0;
43 }
44
45 void sysfs_free(struct mdinfo *sra)
46 {
47 while (sra) {
48 struct mdinfo *sra2 = sra->next;
49 while (sra->devs) {
50 struct mdinfo *d = sra->devs;
51 sra->devs = d->next;
52 free(d);
53 }
54 free(sra);
55 sra = sra2;
56 }
57 }
58
59 struct mdinfo *sysfs_read(int fd, int devnum, unsigned long options)
60 {
61 /* Longest possible name in sysfs, mounted at /sys, is
62 * /sys/block/md_dXXX/md/dev-XXXXX/block/dev
63 * /sys/block/md_dXXX/md/metadata_version
64 * which is about 41 characters. 50 should do for now
65 */
66 char fname[50];
67 char buf[1024];
68 char *base;
69 char *dbase;
70 struct mdinfo *sra;
71 struct mdinfo *dev;
72 DIR *dir;
73 struct dirent *de;
74
75 sra = malloc(sizeof(*sra));
76 if (sra == NULL)
77 return sra;
78 sra->next = NULL;
79
80 if (fd >= 0) {
81 struct stat stb;
82 mdu_version_t vers;
83 if (fstat(fd, &stb)) return NULL;
84 if (ioctl(fd, RAID_VERSION, &vers) != 0)
85 return NULL;
86 if (major(stb.st_rdev) == MD_MAJOR)
87 sprintf(sra->sys_name, "md%d", (int)minor(stb.st_rdev));
88 else if (major(stb.st_rdev) == get_mdp_major())
89 sprintf(sra->sys_name, "md_d%d",
90 (int)minor(stb.st_rdev)>>MdpMinorShift);
91 else {
92 /* must be an extended-minor partition. Look at the
93 * /sys/dev/block/%d:%d link which must look like
94 * ../../block/mdXXX/mdXXXpYY
95 */
96 char path[30];
97 char link[200];
98 char *cp;
99 int n;
100 sprintf(path, "/sys/dev/block/%d:%d", major(stb.st_rdev),
101 minor(stb.st_rdev));
102 n = readlink(path, link, sizeof(link)-1);
103 if (n <= 0)
104 return NULL;
105 link[n] = 0;
106 cp = strrchr(link, '/');
107 if (cp) *cp = 0;
108 cp = strchr(link, '/');
109 if (cp && strncmp(cp, "/md", 3) == 0)
110 strcpy(sra->sys_name, cp+1);
111 else
112 return NULL;
113 }
114 } else {
115 if (devnum >= 0)
116 sprintf(sra->sys_name, "md%d", devnum);
117 else
118 sprintf(sra->sys_name, "md_d%d",
119 -1-devnum);
120 }
121 sprintf(fname, "/sys/block/%s/md/", sra->sys_name);
122 base = fname + strlen(fname);
123
124 sra->devs = NULL;
125 if (options & GET_VERSION) {
126 strcpy(base, "metadata_version");
127 if (load_sys(fname, buf))
128 goto abort;
129 if (strncmp(buf, "none", 4) == 0) {
130 sra->array.major_version =
131 sra->array.minor_version = -1;
132 strcpy(sra->text_version, "");
133 } else if (strncmp(buf, "external:", 9) == 0) {
134 sra->array.major_version = -1;
135 sra->array.minor_version = -2;
136 strcpy(sra->text_version, buf+9);
137 } else
138 sscanf(buf, "%d.%d",
139 &sra->array.major_version,
140 &sra->array.minor_version);
141 }
142 if (options & GET_LEVEL) {
143 strcpy(base, "level");
144 if (load_sys(fname, buf))
145 goto abort;
146 sra->array.level = map_name(pers, buf);
147 }
148 if (options & GET_LAYOUT) {
149 strcpy(base, "layout");
150 if (load_sys(fname, buf))
151 goto abort;
152 sra->array.layout = strtoul(buf, NULL, 0);
153 }
154 if (options & GET_COMPONENT) {
155 strcpy(base, "component_size");
156 if (load_sys(fname, buf))
157 goto abort;
158 sra->component_size = strtoull(buf, NULL, 0);
159 /* sysfs reports "K", but we want sectors */
160 sra->component_size *= 2;
161 }
162 if (options & GET_CHUNK) {
163 strcpy(base, "chunk_size");
164 if (load_sys(fname, buf))
165 goto abort;
166 sra->array.chunk_size = strtoul(buf, NULL, 0);
167 }
168 if (options & GET_CACHE) {
169 strcpy(base, "stripe_cache_size");
170 if (load_sys(fname, buf))
171 goto abort;
172 sra->cache_size = strtoul(buf, NULL, 0);
173 }
174 if (options & GET_MISMATCH) {
175 strcpy(base, "mismatch_cnt");
176 if (load_sys(fname, buf))
177 goto abort;
178 sra->mismatch_cnt = strtoul(buf, NULL, 0);
179 }
180
181 if (! (options & GET_DEVS))
182 return sra;
183
184 /* Get all the devices as well */
185 *base = 0;
186 dir = opendir(fname);
187 if (!dir)
188 goto abort;
189 sra->array.spare_disks = 0;
190
191 while ((de = readdir(dir)) != NULL) {
192 char *ep;
193 if (de->d_ino == 0 ||
194 strncmp(de->d_name, "dev-", 4) != 0)
195 continue;
196 strcpy(base, de->d_name);
197 dbase = base + strlen(base);
198 *dbase++ = '/';
199
200 dev = malloc(sizeof(*dev));
201 if (!dev)
202 goto abort;
203 dev->next = sra->devs;
204 sra->devs = dev;
205 strcpy(dev->sys_name, de->d_name);
206
207 /* Always get slot, major, minor */
208 strcpy(dbase, "slot");
209 if (load_sys(fname, buf))
210 goto abort;
211 dev->disk.raid_disk = strtoul(buf, &ep, 10);
212 if (*ep) dev->disk.raid_disk = -1;
213
214 strcpy(dbase, "block/dev");
215 if (load_sys(fname, buf))
216 goto abort;
217 sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor);
218
219 if (options & GET_OFFSET) {
220 strcpy(dbase, "offset");
221 if (load_sys(fname, buf))
222 goto abort;
223 dev->data_offset = strtoull(buf, NULL, 0);
224 }
225 if (options & GET_SIZE) {
226 strcpy(dbase, "size");
227 if (load_sys(fname, buf))
228 goto abort;
229 dev->component_size = strtoull(buf, NULL, 0);
230 }
231 if (options & GET_STATE) {
232 dev->disk.state = 0;
233 strcpy(dbase, "state");
234 if (load_sys(fname, buf))
235 goto abort;
236 if (strstr(buf, "in_sync"))
237 dev->disk.state |= (1<<MD_DISK_SYNC);
238 if (strstr(buf, "faulty"))
239 dev->disk.state |= (1<<MD_DISK_FAULTY);
240 if (dev->disk.state == 0)
241 sra->array.spare_disks++;
242 }
243 if (options & GET_ERROR) {
244 strcpy(buf, "errors");
245 if (load_sys(fname, buf))
246 goto abort;
247 dev->errors = strtoul(buf, NULL, 0);
248 }
249 }
250 return sra;
251
252 abort:
253 sysfs_free(sra);
254 return NULL;
255 }
256
257 unsigned long long get_component_size(int fd)
258 {
259 /* Find out the component size of the array.
260 * We cannot trust GET_ARRAY_INFO ioctl as it's
261 * size field is only 32bits.
262 * So look in /sys/block/mdXXX/md/component_size
263 *
264 * This returns in units of sectors.
265 */
266 struct stat stb;
267 char fname[50];
268 int n;
269 if (fstat(fd, &stb)) return 0;
270 if (major(stb.st_rdev) != get_mdp_major())
271 sprintf(fname, "/sys/block/md%d/md/component_size",
272 (int)minor(stb.st_rdev));
273 else
274 sprintf(fname, "/sys/block/md_d%d/md/component_size",
275 (int)minor(stb.st_rdev)>>MdpMinorShift);
276 fd = open(fname, O_RDONLY);
277 if (fd < 0)
278 return 0;
279 n = read(fd, fname, sizeof(fname));
280 close(fd);
281 if (n == sizeof(fname))
282 return 0;
283 fname[n] = 0;
284 return strtoull(fname, NULL, 10) * 2;
285 }
286
287 int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev,
288 char *name, char *val)
289 {
290 char fname[50];
291 int n;
292 int fd;
293 sprintf(fname, "/sys/block/%s/md/%s/%s",
294 sra->sys_name, dev?dev->sys_name:"", name);
295 fd = open(fname, O_WRONLY);
296 if (fd < 0)
297 return -1;
298 n = write(fd, val, strlen(val));
299 close(fd);
300 if (n != strlen(val))
301 return -1;
302 return 0;
303 }
304
305 int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev,
306 char *name, unsigned long long val)
307 {
308 char valstr[50];
309 sprintf(valstr, "%llu", val);
310 return sysfs_set_str(sra, dev, name, valstr);
311 }
312
313 int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev,
314 char *name, unsigned long long *val)
315 {
316 char fname[50];
317 char buf[50];
318 int n;
319 int fd;
320 char *ep;
321 sprintf(fname, "/sys/block/%s/md/%s/%s",
322 sra->sys_name, dev?dev->sys_name:"", name);
323 fd = open(fname, O_RDONLY);
324 if (fd < 0)
325 return -1;
326 n = read(fd, buf, sizeof(buf));
327 close(fd);
328 if (n <= 0)
329 return -1;
330 buf[n] = 0;
331 *val = strtoull(buf, &ep, 0);
332 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
333 return -1;
334 return 0;
335 }