]>
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 | ||
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", | |
7fa42a0b | 72 | minor(stb.st_rdev)>>MdpMinorShift); |
e86c9dd6 NB |
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); | |
353632d9 NB |
101 | /* sysfs reports "K", but we want sectors */ |
102 | sra->component_size *= 2; | |
e86c9dd6 NB |
103 | } |
104 | if (options & GET_CHUNK) { | |
105 | strcpy(base, "chunk_size"); | |
106 | if (load_sys(fname, buf)) | |
107 | goto abort; | |
108 | sra->chunk = strtoul(buf, NULL, 0); | |
109 | } | |
758d3a8e NB |
110 | if (options & GET_CACHE) { |
111 | strcpy(base, "stripe_cache_size"); | |
112 | if (load_sys(fname, buf)) | |
113 | goto abort; | |
114 | sra->cache_size = strtoul(buf, NULL, 0); | |
115 | } | |
e86c9dd6 NB |
116 | |
117 | if (! (options & GET_DEVS)) | |
118 | return sra; | |
119 | ||
120 | /* Get all the devices as well */ | |
121 | *base = 0; | |
122 | dir = opendir(fname); | |
123 | if (!dir) | |
124 | goto abort; | |
125 | sra->spares = 0; | |
126 | ||
127 | while ((de = readdir(dir)) != NULL) { | |
128 | char *ep; | |
129 | if (de->d_ino == 0 || | |
130 | strncmp(de->d_name, "dev-", 4) != 0) | |
131 | continue; | |
132 | strcpy(base, de->d_name); | |
133 | dbase = base + strlen(base); | |
134 | *dbase++ = '/'; | |
135 | ||
136 | dev = malloc(sizeof(*dev)); | |
137 | if (!dev) | |
138 | goto abort; | |
139 | dev->next = sra->devs; | |
140 | sra->devs = dev; | |
141 | ||
142 | /* Always get slot, major, minor */ | |
143 | strcpy(dbase, "slot"); | |
144 | if (load_sys(fname, buf)) | |
145 | goto abort; | |
146 | dev->role = strtoul(buf, &ep, 10); | |
147 | if (*ep) dev->role = -1; | |
148 | ||
149 | strcpy(dbase, "block/dev"); | |
150 | if (load_sys(fname, buf)) | |
151 | goto abort; | |
152 | sscanf(buf, "%d:%d", &dev->major, &dev->minor); | |
153 | ||
154 | if (options & GET_OFFSET) { | |
155 | strcpy(dbase, "offset"); | |
156 | if (load_sys(fname, buf)) | |
157 | goto abort; | |
158 | dev->offset = strtoull(buf, NULL, 0); | |
159 | } | |
160 | if (options & GET_SIZE) { | |
161 | strcpy(dbase, "size"); | |
162 | if (load_sys(fname, buf)) | |
163 | goto abort; | |
164 | dev->size = strtoull(buf, NULL, 0); | |
165 | } | |
166 | if (options & GET_STATE) { | |
167 | dev->state = 0; | |
168 | strcpy(dbase, "state"); | |
169 | if (load_sys(fname, buf)) | |
170 | goto abort; | |
171 | if (strstr(buf, "in_sync")) | |
172 | dev->state |= (1<<MD_DISK_SYNC); | |
173 | if (strstr(buf, "faulty")) | |
174 | dev->state |= (1<<MD_DISK_FAULTY); | |
175 | if (dev->state == 0) | |
176 | sra->spares++; | |
177 | } | |
178 | if (options & GET_ERROR) { | |
179 | strcpy(buf, "errors"); | |
180 | if (load_sys(fname, buf)) | |
181 | goto abort; | |
182 | dev->errors = strtoul(buf, NULL, 0); | |
183 | } | |
184 | } | |
185 | return sra; | |
186 | ||
187 | abort: | |
188 | while (sra && sra->devs) { | |
189 | dev = sra->devs; | |
190 | sra->devs = dev->next; | |
191 | free(dev); | |
192 | } | |
193 | if(sra) free(sra); | |
194 | return NULL; | |
195 | } | |
196 | ||
197 | unsigned long long get_component_size(int fd) | |
198 | { | |
199 | /* Find out the component size of the array. | |
200 | * We cannot trust GET_ARRAY_INFO ioctl as it's | |
201 | * size field is only 32bits. | |
202 | * So look in /sys/block/mdXXX/md/component_size | |
353632d9 | 203 | * |
8686f3ed | 204 | * This returns in units of sectors. |
e86c9dd6 NB |
205 | */ |
206 | struct stat stb; | |
207 | char fname[50]; | |
208 | int n; | |
209 | if (fstat(fd, &stb)) return 0; | |
210 | if (major(stb.st_rdev) == 9) | |
211 | sprintf(fname, "/sys/block/md%d/md/component_size", | |
212 | minor(stb.st_rdev)); | |
213 | else | |
214 | sprintf(fname, "/sys/block/md_d%d/md/component_size", | |
7fa42a0b | 215 | minor(stb.st_rdev)>>MdpMinorShift); |
e86c9dd6 NB |
216 | fd = open(fname, O_RDONLY); |
217 | if (fd < 0) | |
218 | return 0; | |
219 | n = read(fd, fname, sizeof(fname)); | |
220 | close(fd); | |
221 | if (n == sizeof(fname)) | |
222 | return 0; | |
223 | fname[n] = 0; | |
8686f3ed | 224 | return strtoull(fname, NULL, 10) * 2; |
e86c9dd6 NB |
225 | } |
226 | ||
227 | int sysfs_set_str(struct sysarray *sra, struct sysdev *dev, | |
228 | char *name, char *val) | |
229 | { | |
230 | char fname[50]; | |
231 | int n; | |
232 | int fd; | |
233 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
234 | sra->name, dev?dev->name:"", name); | |
235 | fd = open(fname, O_WRONLY); | |
236 | if (fd < 0) | |
237 | return -1; | |
238 | n = write(fd, val, strlen(val)); | |
239 | close(fd); | |
240 | if (n != strlen(val)) | |
241 | return -1; | |
242 | return 0; | |
243 | } | |
244 | ||
245 | int sysfs_set_num(struct sysarray *sra, struct sysdev *dev, | |
246 | char *name, unsigned long long val) | |
247 | { | |
248 | char valstr[50]; | |
249 | sprintf(valstr, "%llu", val); | |
250 | return sysfs_set_str(sra, dev, name, valstr); | |
251 | } | |
252 | ||
253 | int sysfs_get_ll(struct sysarray *sra, struct sysdev *dev, | |
254 | char *name, unsigned long long *val) | |
255 | { | |
256 | char fname[50]; | |
257 | char buf[50]; | |
258 | int n; | |
259 | int fd; | |
260 | char *ep; | |
261 | sprintf(fname, "/sys/block/%s/md/%s/%s", | |
262 | sra->name, dev?dev->name:"", name); | |
263 | fd = open(fname, O_RDONLY); | |
264 | if (fd < 0) | |
265 | return -1; | |
266 | n = read(fd, buf, sizeof(buf)); | |
267 | close(fd); | |
268 | if (n <= 0) | |
269 | return -1; | |
270 | buf[n] = 0; | |
271 | *val = strtoull(buf, &ep, 0); | |
272 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
273 | return -1; | |
274 | return 0; | |
275 | } |