]>
Commit | Line | Data |
---|---|---|
e86c9dd6 NB |
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 | ||
7e0f6979 | 45 | void sysfs_free(struct mdinfo *sra) |
8382f19b | 46 | { |
7e0f6979 NB |
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; | |
8382f19b | 56 | } |
8382f19b NB |
57 | } |
58 | ||
7e0f6979 | 59 | struct mdinfo *sysfs_read(int fd, int devnum, unsigned long options) |
e86c9dd6 NB |
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; | |
7e0f6979 | 70 | struct mdinfo *sra; |
06c7f68e | 71 | struct mdinfo *dev; |
e86c9dd6 NB |
72 | DIR *dir; |
73 | struct dirent *de; | |
74 | ||
75 | sra = malloc(sizeof(*sra)); | |
76 | if (sra == NULL) | |
77 | return sra; | |
7e0f6979 | 78 | sra->next = NULL; |
e86c9dd6 NB |
79 | |
80 | if (fd >= 0) { | |
81 | struct stat stb; | |
2faf1f5f NB |
82 | mdu_version_t vers; |
83 | if (fstat(fd, &stb)) return NULL; | |
84 | if (ioctl(fd, RAID_VERSION, &vers) != 0) | |
85 | return NULL; | |
e86c9dd6 | 86 | if (major(stb.st_rdev)==9) |
7e0f6979 | 87 | sprintf(sra->sys_name, "md%d", minor(stb.st_rdev)); |
e86c9dd6 | 88 | else |
7e0f6979 | 89 | sprintf(sra->sys_name, "md_d%d", |
7fa42a0b | 90 | minor(stb.st_rdev)>>MdpMinorShift); |
e86c9dd6 NB |
91 | } else { |
92 | if (devnum >= 0) | |
7e0f6979 | 93 | sprintf(sra->sys_name, "md%d", devnum); |
e86c9dd6 | 94 | else |
7e0f6979 | 95 | sprintf(sra->sys_name, "md_d%d", |
e86c9dd6 NB |
96 | -1-devnum); |
97 | } | |
7e0f6979 | 98 | sprintf(fname, "/sys/block/%s/md/", sra->sys_name); |
e86c9dd6 NB |
99 | base = fname + strlen(fname); |
100 | ||
101 | sra->devs = NULL; | |
8382f19b NB |
102 | if (options & GET_VERSION) { |
103 | strcpy(base, "metadata_version"); | |
104 | if (load_sys(fname, buf)) | |
105 | goto abort; | |
106 | if (strncmp(buf, "none", 4) == 0) | |
7e0f6979 NB |
107 | sra->array.major_version = |
108 | sra->array.minor_version = -1; | |
8382f19b NB |
109 | else |
110 | sscanf(buf, "%d.%d", | |
7e0f6979 NB |
111 | &sra->array.major_version, |
112 | &sra->array.minor_version); | |
8382f19b | 113 | } |
e86c9dd6 NB |
114 | if (options & GET_LEVEL) { |
115 | strcpy(base, "level"); | |
116 | if (load_sys(fname, buf)) | |
117 | goto abort; | |
7e0f6979 | 118 | sra->array.level = map_name(pers, buf); |
e86c9dd6 NB |
119 | } |
120 | if (options & GET_LAYOUT) { | |
121 | strcpy(base, "layout"); | |
122 | if (load_sys(fname, buf)) | |
123 | goto abort; | |
7e0f6979 | 124 | sra->array.layout = strtoul(buf, NULL, 0); |
e86c9dd6 NB |
125 | } |
126 | if (options & GET_COMPONENT) { | |
127 | strcpy(base, "component_size"); | |
128 | if (load_sys(fname, buf)) | |
129 | goto abort; | |
130 | sra->component_size = strtoull(buf, NULL, 0); | |
353632d9 NB |
131 | /* sysfs reports "K", but we want sectors */ |
132 | sra->component_size *= 2; | |
e86c9dd6 NB |
133 | } |
134 | if (options & GET_CHUNK) { | |
135 | strcpy(base, "chunk_size"); | |
136 | if (load_sys(fname, buf)) | |
137 | goto abort; | |
7e0f6979 | 138 | sra->array.chunk_size = strtoul(buf, NULL, 0); |
e86c9dd6 | 139 | } |
758d3a8e NB |
140 | if (options & GET_CACHE) { |
141 | strcpy(base, "stripe_cache_size"); | |
142 | if (load_sys(fname, buf)) | |
143 | goto abort; | |
144 | sra->cache_size = strtoul(buf, NULL, 0); | |
145 | } | |
37dfc3d6 NB |
146 | if (options & GET_MISMATCH) { |
147 | strcpy(base, "mismatch_cnt"); | |
148 | if (load_sys(fname, buf)) | |
149 | goto abort; | |
150 | sra->mismatch_cnt = strtoul(buf, NULL, 0); | |
151 | } | |
e86c9dd6 NB |
152 | |
153 | if (! (options & GET_DEVS)) | |
154 | return sra; | |
155 | ||
156 | /* Get all the devices as well */ | |
157 | *base = 0; | |
158 | dir = opendir(fname); | |
159 | if (!dir) | |
160 | goto abort; | |
7e0f6979 | 161 | sra->array.spare_disks = 0; |
e86c9dd6 NB |
162 | |
163 | while ((de = readdir(dir)) != NULL) { | |
164 | char *ep; | |
165 | if (de->d_ino == 0 || | |
166 | strncmp(de->d_name, "dev-", 4) != 0) | |
167 | continue; | |
168 | strcpy(base, de->d_name); | |
169 | dbase = base + strlen(base); | |
170 | *dbase++ = '/'; | |
171 | ||
172 | dev = malloc(sizeof(*dev)); | |
173 | if (!dev) | |
174 | goto abort; | |
175 | dev->next = sra->devs; | |
176 | sra->devs = dev; | |
06c7f68e | 177 | strcpy(dev->sys_name, de->d_name); |
e86c9dd6 NB |
178 | |
179 | /* Always get slot, major, minor */ | |
180 | strcpy(dbase, "slot"); | |
181 | if (load_sys(fname, buf)) | |
182 | goto abort; | |
06c7f68e NB |
183 | dev->disk.raid_disk = strtoul(buf, &ep, 10); |
184 | if (*ep) dev->disk.raid_disk = -1; | |
e86c9dd6 NB |
185 | |
186 | strcpy(dbase, "block/dev"); | |
187 | if (load_sys(fname, buf)) | |
188 | goto abort; | |
06c7f68e | 189 | sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor); |
e86c9dd6 NB |
190 | |
191 | if (options & GET_OFFSET) { | |
192 | strcpy(dbase, "offset"); | |
193 | if (load_sys(fname, buf)) | |
194 | goto abort; | |
06c7f68e | 195 | dev->data_offset = strtoull(buf, NULL, 0); |
e86c9dd6 NB |
196 | } |
197 | if (options & GET_SIZE) { | |
198 | strcpy(dbase, "size"); | |
199 | if (load_sys(fname, buf)) | |
200 | goto abort; | |
06c7f68e | 201 | dev->component_size = strtoull(buf, NULL, 0); |
e86c9dd6 NB |
202 | } |
203 | if (options & GET_STATE) { | |
06c7f68e | 204 | dev->disk.state = 0; |
e86c9dd6 NB |
205 | strcpy(dbase, "state"); |
206 | if (load_sys(fname, buf)) | |
207 | goto abort; | |
208 | if (strstr(buf, "in_sync")) | |
06c7f68e | 209 | dev->disk.state |= (1<<MD_DISK_SYNC); |
e86c9dd6 | 210 | if (strstr(buf, "faulty")) |
06c7f68e NB |
211 | dev->disk.state |= (1<<MD_DISK_FAULTY); |
212 | if (dev->disk.state == 0) | |
7e0f6979 | 213 | sra->array.spare_disks++; |
e86c9dd6 NB |
214 | } |
215 | if (options & GET_ERROR) { | |
216 | strcpy(buf, "errors"); | |
217 | if (load_sys(fname, buf)) | |
218 | goto abort; | |
219 | dev->errors = strtoul(buf, NULL, 0); | |
220 | } | |
221 | } | |
222 | return sra; | |
223 | ||
224 | abort: | |
8382f19b | 225 | sysfs_free(sra); |
e86c9dd6 NB |
226 | return NULL; |
227 | } | |
228 | ||
229 | unsigned long long get_component_size(int fd) | |
230 | { | |
231 | /* Find out the component size of the array. | |
232 | * We cannot trust GET_ARRAY_INFO ioctl as it's | |
233 | * size field is only 32bits. | |
234 | * So look in /sys/block/mdXXX/md/component_size | |
353632d9 | 235 | * |
8686f3ed | 236 | * This returns in units of sectors. |
e86c9dd6 NB |
237 | */ |
238 | struct stat stb; | |
239 | char fname[50]; | |
240 | int n; | |
241 | if (fstat(fd, &stb)) return 0; | |
242 | if (major(stb.st_rdev) == 9) | |
243 | sprintf(fname, "/sys/block/md%d/md/component_size", | |
244 | minor(stb.st_rdev)); | |
245 | else | |
246 | sprintf(fname, "/sys/block/md_d%d/md/component_size", | |
7fa42a0b | 247 | minor(stb.st_rdev)>>MdpMinorShift); |
e86c9dd6 NB |
248 | fd = open(fname, O_RDONLY); |
249 | if (fd < 0) | |
250 | return 0; | |
251 | n = read(fd, fname, sizeof(fname)); | |
252 | close(fd); | |
253 | if (n == sizeof(fname)) | |
254 | return 0; | |
255 | fname[n] = 0; | |
8686f3ed | 256 | return strtoull(fname, NULL, 10) * 2; |
e86c9dd6 NB |
257 | } |
258 | ||
7e0f6979 | 259 | int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
260 | char *name, char *val) |
261 | { | |
262 | char fname[50]; | |
263 | int n; | |
264 | int fd; | |
265 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
7e0f6979 | 266 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
267 | fd = open(fname, O_WRONLY); |
268 | if (fd < 0) | |
269 | return -1; | |
270 | n = write(fd, val, strlen(val)); | |
271 | close(fd); | |
272 | if (n != strlen(val)) | |
273 | return -1; | |
274 | return 0; | |
275 | } | |
276 | ||
7e0f6979 | 277 | int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
278 | char *name, unsigned long long val) |
279 | { | |
280 | char valstr[50]; | |
281 | sprintf(valstr, "%llu", val); | |
282 | return sysfs_set_str(sra, dev, name, valstr); | |
283 | } | |
284 | ||
7e0f6979 | 285 | int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev, |
e86c9dd6 NB |
286 | char *name, unsigned long long *val) |
287 | { | |
288 | char fname[50]; | |
289 | char buf[50]; | |
290 | int n; | |
291 | int fd; | |
292 | char *ep; | |
293 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
7e0f6979 | 294 | sra->sys_name, dev?dev->sys_name:"", name); |
e86c9dd6 NB |
295 | fd = open(fname, O_RDONLY); |
296 | if (fd < 0) | |
297 | return -1; | |
298 | n = read(fd, buf, sizeof(buf)); | |
299 | close(fd); | |
300 | if (n <= 0) | |
301 | return -1; | |
302 | buf[n] = 0; | |
303 | *val = strtoull(buf, &ep, 0); | |
304 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
305 | return -1; | |
306 | return 0; | |
307 | } |