Replace sysarray with mdinfo
[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)==9)
87                         sprintf(sra->sys_name, "md%d", minor(stb.st_rdev));
88                 else
89                         sprintf(sra->sys_name, "md_d%d",
90                                 minor(stb.st_rdev)>>MdpMinorShift);
91         } else {
92                 if (devnum >= 0)
93                         sprintf(sra->sys_name, "md%d", devnum);
94                 else
95                         sprintf(sra->sys_name, "md_d%d",
96                                 -1-devnum);
97         }
98         sprintf(fname, "/sys/block/%s/md/", sra->sys_name);
99         base = fname + strlen(fname);
100
101         sra->devs = NULL;
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)
107                         sra->array.major_version =
108                                 sra->array.minor_version = -1;
109                 else
110                         sscanf(buf, "%d.%d",
111                                &sra->array.major_version,
112                                &sra->array.minor_version);
113         }
114         if (options & GET_LEVEL) {
115                 strcpy(base, "level");
116                 if (load_sys(fname, buf))
117                         goto abort;
118                 sra->array.level = map_name(pers, buf);
119         }
120         if (options & GET_LAYOUT) {
121                 strcpy(base, "layout");
122                 if (load_sys(fname, buf))
123                         goto abort;
124                 sra->array.layout = strtoul(buf, NULL, 0);
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);
131                 /* sysfs reports "K", but we want sectors */
132                 sra->component_size *= 2;
133         }
134         if (options & GET_CHUNK) {
135                 strcpy(base, "chunk_size");
136                 if (load_sys(fname, buf))
137                         goto abort;
138                 sra->array.chunk_size = strtoul(buf, NULL, 0);
139         }
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         }
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         }
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;
161         sra->array.spare_disks = 0;
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;
177                 strcpy(dev->sys_name, de->d_name);
178
179                 /* Always get slot, major, minor */
180                 strcpy(dbase, "slot");
181                 if (load_sys(fname, buf))
182                         goto abort;
183                 dev->disk.raid_disk = strtoul(buf, &ep, 10);
184                 if (*ep) dev->disk.raid_disk = -1;
185
186                 strcpy(dbase, "block/dev");
187                 if (load_sys(fname, buf))
188                         goto abort;
189                 sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor);
190
191                 if (options & GET_OFFSET) {
192                         strcpy(dbase, "offset");
193                         if (load_sys(fname, buf))
194                                 goto abort;
195                         dev->data_offset = strtoull(buf, NULL, 0);
196                 }
197                 if (options & GET_SIZE) {
198                         strcpy(dbase, "size");
199                         if (load_sys(fname, buf))
200                                 goto abort;
201                         dev->component_size = strtoull(buf, NULL, 0);
202                 }
203                 if (options & GET_STATE) {
204                         dev->disk.state = 0;
205                         strcpy(dbase, "state");
206                         if (load_sys(fname, buf))
207                                 goto abort;
208                         if (strstr(buf, "in_sync"))
209                                 dev->disk.state |= (1<<MD_DISK_SYNC);
210                         if (strstr(buf, "faulty"))
211                                 dev->disk.state |= (1<<MD_DISK_FAULTY);
212                         if (dev->disk.state == 0)
213                                 sra->array.spare_disks++;
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:
225         sysfs_free(sra);
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
235          *
236          * This returns in units of sectors.
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",
247                         minor(stb.st_rdev)>>MdpMinorShift);
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;
256         return strtoull(fname, NULL, 10) * 2;
257 }
258
259 int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev,
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",
266                 sra->sys_name, dev?dev->sys_name:"", name);
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
277 int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev,
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
285 int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev,
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",
294                 sra->sys_name, dev?dev->sys_name:"", name);
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 }