]> git.ipfire.org Git - thirdparty/mdadm.git/blob - sysfs.c
Initial reshape support
[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 struct sysarray *sysfs_read(int fd, int devnum, unsigned long options)
46 {
47 /* Longest possible name in sysfs, mounted at /sys, is
48 * /sys/block/md_dXXX/md/dev-XXXXX/block/dev
49 * /sys/block/md_dXXX/md/metadata_version
50 * which is about 41 characters. 50 should do for now
51 */
52 char fname[50];
53 char buf[1024];
54 char *base;
55 char *dbase;
56 struct sysarray *sra;
57 struct sysdev *dev;
58 DIR *dir;
59 struct dirent *de;
60
61 sra = malloc(sizeof(*sra));
62 if (sra == NULL)
63 return sra;
64
65 if (fd >= 0) {
66 struct stat stb;
67 if (fstat(fd, &stb)) return NULL;
68 if (major(stb.st_rdev)==9)
69 sprintf(sra->name, "md%d", minor(stb.st_rdev));
70 else
71 sprintf(sra->name, "md_d%d",
72 minor(stb.st_rdev)/16);
73 } else {
74 if (devnum >= 0)
75 sprintf(sra->name, "md%d", devnum);
76 else
77 sprintf(sra->name, "md_d%d",
78 -1-devnum);
79 }
80 sprintf(fname, "/sys/block/%s/md/", sra->name);
81 base = fname + strlen(fname);
82
83 sra->devs = NULL;
84 if (options & GET_LEVEL) {
85 strcpy(base, "level");
86 if (load_sys(fname, buf))
87 goto abort;
88 sra->level = map_name(pers, buf);
89 }
90 if (options & GET_LAYOUT) {
91 strcpy(base, "layout");
92 if (load_sys(fname, buf))
93 goto abort;
94 sra->layout = strtoul(buf, NULL, 0);
95 }
96 if (options & GET_COMPONENT) {
97 strcpy(base, "component_size");
98 if (load_sys(fname, buf))
99 goto abort;
100 sra->component_size = strtoull(buf, NULL, 0);
101 }
102 if (options & GET_CHUNK) {
103 strcpy(base, "chunk_size");
104 if (load_sys(fname, buf))
105 goto abort;
106 sra->chunk = strtoul(buf, NULL, 0);
107 }
108
109 if (! (options & GET_DEVS))
110 return sra;
111
112 /* Get all the devices as well */
113 *base = 0;
114 dir = opendir(fname);
115 if (!dir)
116 goto abort;
117 sra->spares = 0;
118
119 while ((de = readdir(dir)) != NULL) {
120 char *ep;
121 if (de->d_ino == 0 ||
122 strncmp(de->d_name, "dev-", 4) != 0)
123 continue;
124 strcpy(base, de->d_name);
125 dbase = base + strlen(base);
126 *dbase++ = '/';
127
128 dev = malloc(sizeof(*dev));
129 if (!dev)
130 goto abort;
131 dev->next = sra->devs;
132 sra->devs = dev;
133
134 /* Always get slot, major, minor */
135 strcpy(dbase, "slot");
136 if (load_sys(fname, buf))
137 goto abort;
138 dev->role = strtoul(buf, &ep, 10);
139 if (*ep) dev->role = -1;
140
141 strcpy(dbase, "block/dev");
142 if (load_sys(fname, buf))
143 goto abort;
144 sscanf(buf, "%d:%d", &dev->major, &dev->minor);
145
146 if (options & GET_OFFSET) {
147 strcpy(dbase, "offset");
148 if (load_sys(fname, buf))
149 goto abort;
150 dev->offset = strtoull(buf, NULL, 0);
151 }
152 if (options & GET_SIZE) {
153 strcpy(dbase, "size");
154 if (load_sys(fname, buf))
155 goto abort;
156 dev->size = strtoull(buf, NULL, 0);
157 }
158 if (options & GET_STATE) {
159 dev->state = 0;
160 strcpy(dbase, "state");
161 if (load_sys(fname, buf))
162 goto abort;
163 if (strstr(buf, "in_sync"))
164 dev->state |= (1<<MD_DISK_SYNC);
165 if (strstr(buf, "faulty"))
166 dev->state |= (1<<MD_DISK_FAULTY);
167 if (dev->state == 0)
168 sra->spares++;
169 }
170 if (options & GET_ERROR) {
171 strcpy(buf, "errors");
172 if (load_sys(fname, buf))
173 goto abort;
174 dev->errors = strtoul(buf, NULL, 0);
175 }
176 }
177 return sra;
178
179 abort:
180 while (sra && sra->devs) {
181 dev = sra->devs;
182 sra->devs = dev->next;
183 free(dev);
184 }
185 if(sra) free(sra);
186 return NULL;
187 }
188
189 unsigned long long get_component_size(int fd)
190 {
191 /* Find out the component size of the array.
192 * We cannot trust GET_ARRAY_INFO ioctl as it's
193 * size field is only 32bits.
194 * So look in /sys/block/mdXXX/md/component_size
195 */
196 struct stat stb;
197 char fname[50];
198 int n;
199 if (fstat(fd, &stb)) return 0;
200 if (major(stb.st_rdev) == 9)
201 sprintf(fname, "/sys/block/md%d/md/component_size",
202 minor(stb.st_rdev));
203 else
204 sprintf(fname, "/sys/block/md_d%d/md/component_size",
205 minor(stb.st_rdev)/16);
206 fd = open(fname, O_RDONLY);
207 if (fd < 0)
208 return 0;
209 n = read(fd, fname, sizeof(fname));
210 close(fd);
211 if (n == sizeof(fname))
212 return 0;
213 fname[n] = 0;
214 return strtoull(fname, NULL, 10);
215 }
216
217 int sysfs_set_str(struct sysarray *sra, struct sysdev *dev,
218 char *name, char *val)
219 {
220 char fname[50];
221 int n;
222 int fd;
223 sprintf(fname, "/sys/block/%s/md/%s/%s",
224 sra->name, dev?dev->name:"", name);
225 fd = open(fname, O_WRONLY);
226 if (fd < 0)
227 return -1;
228 n = write(fd, val, strlen(val));
229 close(fd);
230 if (n != strlen(val))
231 return -1;
232 return 0;
233 }
234
235 int sysfs_set_num(struct sysarray *sra, struct sysdev *dev,
236 char *name, unsigned long long val)
237 {
238 char valstr[50];
239 sprintf(valstr, "%llu", val);
240 return sysfs_set_str(sra, dev, name, valstr);
241 }
242
243 int sysfs_get_ll(struct sysarray *sra, struct sysdev *dev,
244 char *name, unsigned long long *val)
245 {
246 char fname[50];
247 char buf[50];
248 int n;
249 int fd;
250 char *ep;
251 sprintf(fname, "/sys/block/%s/md/%s/%s",
252 sra->name, dev?dev->name:"", name);
253 fd = open(fname, O_RDONLY);
254 if (fd < 0)
255 return -1;
256 n = read(fd, buf, sizeof(buf));
257 close(fd);
258 if (n <= 0)
259 return -1;
260 buf[n] = 0;
261 *val = strtoull(buf, &ep, 0);
262 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
263 return -1;
264 return 0;
265 }