]> git.ipfire.org Git - thirdparty/mdadm.git/blob - sysfs.c
Allow an externally managed array to be marked readonly
[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 (n && 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 int sysfs_open(int devnum, char *devname, char *attr)
60 {
61 char fname[50];
62 int fd;
63
64 sprintf(fname, "/sys/block/%s/md/", devnum2devname(devnum));
65 if (devname) {
66 strcat(fname, devname);
67 strcat(fname, "/");
68 }
69 strcat(fname, attr);
70 fd = open(fname, O_RDWR);
71 if (fd < 0 && errno == EACCES)
72 fd = open(fname, O_RDONLY);
73 return fd;
74 }
75
76 struct mdinfo *sysfs_read(int fd, int devnum, unsigned long options)
77 {
78 /* Longest possible name in sysfs, mounted at /sys, is
79 * /sys/block/md_dXXX/md/dev-XXXXX/block/dev
80 * /sys/block/md_dXXX/md/metadata_version
81 * which is about 41 characters. 50 should do for now
82 */
83 char fname[50];
84 char buf[1024];
85 char *base;
86 char *dbase;
87 struct mdinfo *sra;
88 struct mdinfo *dev;
89 DIR *dir = NULL;
90 struct dirent *de;
91
92 sra = malloc(sizeof(*sra));
93 if (sra == NULL)
94 return sra;
95 sra->next = NULL;
96
97 if (fd >= 0) {
98 struct stat stb;
99 mdu_version_t vers;
100 if (fstat(fd, &stb)) return NULL;
101 if (ioctl(fd, RAID_VERSION, &vers) != 0)
102 return NULL;
103 if (major(stb.st_rdev)==9)
104 sprintf(sra->sys_name, "md%d", (int)minor(stb.st_rdev));
105 else
106 sprintf(sra->sys_name, "md_d%d",
107 (int)minor(stb.st_rdev)>>MdpMinorShift);
108 } else {
109 if (devnum >= 0)
110 sprintf(sra->sys_name, "md%d", devnum);
111 else
112 sprintf(sra->sys_name, "md_d%d",
113 -1-devnum);
114 }
115 sprintf(fname, "/sys/block/%s/md/", sra->sys_name);
116 base = fname + strlen(fname);
117
118 sra->devs = NULL;
119 if (options & GET_VERSION) {
120 strcpy(base, "metadata_version");
121 if (load_sys(fname, buf))
122 goto abort;
123 if (strncmp(buf, "none", 4) == 0) {
124 sra->array.major_version =
125 sra->array.minor_version = -1;
126 strcpy(sra->text_version, "");
127 } else if (strncmp(buf, "external:", 9) == 0) {
128 sra->array.major_version = -1;
129 sra->array.minor_version = -2;
130 strcpy(sra->text_version, buf+9);
131 } else {
132 sscanf(buf, "%d.%d",
133 &sra->array.major_version,
134 &sra->array.minor_version);
135 strcpy(sra->text_version, buf);
136 }
137 }
138 if (options & GET_LEVEL) {
139 strcpy(base, "level");
140 if (load_sys(fname, buf))
141 goto abort;
142 sra->array.level = map_name(pers, buf);
143 }
144 if (options & GET_LAYOUT) {
145 strcpy(base, "layout");
146 if (load_sys(fname, buf))
147 goto abort;
148 sra->array.layout = strtoul(buf, NULL, 0);
149 }
150 if (options & GET_DISKS) {
151 strcpy(base, "raid_disks");
152 if (load_sys(fname, buf))
153 goto abort;
154 sra->array.raid_disks = strtoul(buf, NULL, 0);
155 }
156 if (options & GET_DEGRADED) {
157 strcpy(base, "degraded");
158 if (load_sys(fname, buf))
159 goto abort;
160 sra->array.failed_disks = strtoul(buf, NULL, 0);
161 }
162 if (options & GET_COMPONENT) {
163 strcpy(base, "component_size");
164 if (load_sys(fname, buf))
165 goto abort;
166 sra->component_size = strtoull(buf, NULL, 0);
167 /* sysfs reports "K", but we want sectors */
168 sra->component_size *= 2;
169 }
170 if (options & GET_CHUNK) {
171 strcpy(base, "chunk_size");
172 if (load_sys(fname, buf))
173 goto abort;
174 sra->array.chunk_size = strtoul(buf, NULL, 0);
175 }
176 if (options & GET_CACHE) {
177 strcpy(base, "stripe_cache_size");
178 if (load_sys(fname, buf))
179 goto abort;
180 sra->cache_size = strtoul(buf, NULL, 0);
181 }
182 if (options & GET_MISMATCH) {
183 strcpy(base, "mismatch_cnt");
184 if (load_sys(fname, buf))
185 goto abort;
186 sra->mismatch_cnt = strtoul(buf, NULL, 0);
187 }
188
189 if (! (options & GET_DEVS))
190 return sra;
191
192 /* Get all the devices as well */
193 *base = 0;
194 dir = opendir(fname);
195 if (!dir)
196 goto abort;
197 sra->array.spare_disks = 0;
198
199 while ((de = readdir(dir)) != NULL) {
200 char *ep;
201 if (de->d_ino == 0 ||
202 strncmp(de->d_name, "dev-", 4) != 0)
203 continue;
204 strcpy(base, de->d_name);
205 dbase = base + strlen(base);
206 *dbase++ = '/';
207
208 dev = malloc(sizeof(*dev));
209 if (!dev)
210 goto abort;
211 dev->next = sra->devs;
212 sra->devs = dev;
213 strcpy(dev->sys_name, de->d_name);
214
215 /* Always get slot, major, minor */
216 strcpy(dbase, "slot");
217 if (load_sys(fname, buf))
218 goto abort;
219 dev->disk.raid_disk = strtoul(buf, &ep, 10);
220 if (*ep) dev->disk.raid_disk = -1;
221
222 strcpy(dbase, "block/dev");
223 if (load_sys(fname, buf))
224 goto abort;
225 sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor);
226
227 if (options & GET_OFFSET) {
228 strcpy(dbase, "offset");
229 if (load_sys(fname, buf))
230 goto abort;
231 dev->data_offset = strtoull(buf, NULL, 0);
232 }
233 if (options & GET_SIZE) {
234 strcpy(dbase, "size");
235 if (load_sys(fname, buf))
236 goto abort;
237 dev->component_size = strtoull(buf, NULL, 0) * 2;
238 }
239 if (options & GET_STATE) {
240 dev->disk.state = 0;
241 strcpy(dbase, "state");
242 if (load_sys(fname, buf))
243 goto abort;
244 if (strstr(buf, "in_sync"))
245 dev->disk.state |= (1<<MD_DISK_SYNC);
246 if (strstr(buf, "faulty"))
247 dev->disk.state |= (1<<MD_DISK_FAULTY);
248 if (dev->disk.state == 0)
249 sra->array.spare_disks++;
250 }
251 if (options & GET_ERROR) {
252 strcpy(buf, "errors");
253 if (load_sys(fname, buf))
254 goto abort;
255 dev->errors = strtoul(buf, NULL, 0);
256 }
257 }
258 closedir(dir);
259 return sra;
260
261 abort:
262 if (dir)
263 closedir(dir);
264 sysfs_free(sra);
265 return NULL;
266 }
267
268 unsigned long long get_component_size(int fd)
269 {
270 /* Find out the component size of the array.
271 * We cannot trust GET_ARRAY_INFO ioctl as it's
272 * size field is only 32bits.
273 * So look in /sys/block/mdXXX/md/component_size
274 *
275 * This returns in units of sectors.
276 */
277 struct stat stb;
278 char fname[50];
279 int n;
280 if (fstat(fd, &stb)) return 0;
281 if (major(stb.st_rdev) == 9)
282 sprintf(fname, "/sys/block/md%d/md/component_size",
283 (int)minor(stb.st_rdev));
284 else
285 sprintf(fname, "/sys/block/md_d%d/md/component_size",
286 (int)minor(stb.st_rdev)>>MdpMinorShift);
287 fd = open(fname, O_RDONLY);
288 if (fd < 0)
289 return 0;
290 n = read(fd, fname, sizeof(fname));
291 close(fd);
292 if (n == sizeof(fname))
293 return 0;
294 fname[n] = 0;
295 return strtoull(fname, NULL, 10) * 2;
296 }
297
298 int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev,
299 char *name, char *val)
300 {
301 char fname[50];
302 int n;
303 int fd;
304
305 sprintf(fname, "/sys/block/%s/md/%s/%s",
306 sra->sys_name, dev?dev->sys_name:"", name);
307 fd = open(fname, O_WRONLY);
308 if (fd < 0)
309 return -1;
310 n = write(fd, val, strlen(val));
311 close(fd);
312 if (n != strlen(val))
313 return -1;
314 return 0;
315 }
316
317 int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev,
318 char *name, unsigned long long val)
319 {
320 char valstr[50];
321 sprintf(valstr, "%llu", val);
322 return sysfs_set_str(sra, dev, name, valstr);
323 }
324
325 int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev,
326 char *name, unsigned long long *val)
327 {
328 char fname[50];
329 char buf[50];
330 int n;
331 int fd;
332 char *ep;
333 sprintf(fname, "/sys/block/%s/md/%s/%s",
334 sra->sys_name, dev?dev->sys_name:"", name);
335 fd = open(fname, O_RDONLY);
336 if (fd < 0)
337 return -1;
338 n = read(fd, buf, sizeof(buf));
339 close(fd);
340 if (n <= 0)
341 return -1;
342 buf[n] = 0;
343 *val = strtoull(buf, &ep, 0);
344 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
345 return -1;
346 return 0;
347 }
348
349 int sysfs_set_array(struct mdinfo *sra,
350 struct mdinfo *info)
351 {
352 int rv = 0;
353 sra->array = info->array;
354
355 if (info->array.level < 0)
356 return 0; /* FIXME */
357 rv |= sysfs_set_str(sra, NULL, "level",
358 map_num(pers, info->array.level));
359 rv |= sysfs_set_num(sra, NULL, "raid_disks", info->array.raid_disks);
360 rv |= sysfs_set_num(sra, NULL, "chunk_size", info->array.chunk_size);
361 rv |= sysfs_set_num(sra, NULL, "layout", info->array.layout);
362 rv |= sysfs_set_num(sra, NULL, "component_size", info->component_size/2);
363 rv |= sysfs_set_num(sra, NULL, "resync_start", info->resync_start);
364 sra->array = info->array;
365 return rv;
366 }
367
368 int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd)
369 {
370 char dv[100];
371 char nm[100];
372 struct mdinfo *sd2;
373 char *dname;
374 int rv;
375
376 sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor);
377 rv = sysfs_set_str(sra, NULL, "new_dev", dv);
378 if (rv)
379 return rv;
380
381 memset(nm, 0, sizeof(nm));
382 sprintf(dv, "/sys/dev/block/%d:%d", sd->disk.major, sd->disk.minor);
383 rv = readlink(dv, nm, sizeof(nm));
384 if (rv <= 0)
385 return -1;
386 nm[rv] = '\0';
387 dname = strrchr(nm, '/');
388 if (dname) dname++;
389 strcpy(sd->sys_name, "dev-");
390 strcpy(sd->sys_name+4, dname);
391
392 rv = sysfs_set_num(sra, sd, "offset", sd->data_offset);
393 rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2);
394 if (sra->array.level != LEVEL_CONTAINER) {
395 rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk);
396 // rv |= sysfs_set_str(sra, sd, "state", "in_sync");
397 }
398 if (! rv) {
399 sd2 = malloc(sizeof(*sd2));
400 *sd2 = *sd;
401 sd2->next = sra->devs;
402 sra->devs = sd2;
403 }
404 return rv;
405 }
406
407 #if 0
408 int sysfs_disk_to_sg(int fd)
409 {
410 /* from an open block device, try find and open its corresponding
411 * scsi_generic interface
412 */
413 struct stat st;
414 char path[256];
415 char sg_path[256];
416 char sg_major_minor[8];
417 char *c;
418 DIR *dir;
419 struct dirent *de;
420 int major, minor, rv;
421
422 if (fstat(fd, &st))
423 return -1;
424
425 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device",
426 major(st.st_rdev), minor(st.st_rdev));
427
428 dir = opendir(path);
429 if (!dir)
430 return -1;
431
432 de = readdir(dir);
433 while (de) {
434 if (strncmp("scsi_generic:", de->d_name,
435 strlen("scsi_generic:")) == 0)
436 break;
437 de = readdir(dir);
438 }
439 closedir(dir);
440
441 if (!de)
442 return -1;
443
444 snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name);
445 fd = open(sg_path, O_RDONLY);
446 if (fd < 0)
447 return fd;
448
449 rv = read(fd, sg_major_minor, sizeof(sg_major_minor));
450 close(fd);
451 if (rv < 0)
452 return -1;
453 else
454 sg_major_minor[rv - 1] = '\0';
455
456 c = strchr(sg_major_minor, ':');
457 *c = '\0';
458 c++;
459 major = strtol(sg_major_minor, NULL, 10);
460 minor = strtol(c, NULL, 10);
461 snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d",
462 (int) getpid(), major, minor);
463 if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) {
464 fd = open(path, O_RDONLY);
465 unlink(path);
466 return fd;
467 }
468
469 return -1;
470 }
471 #endif
472
473 int sysfs_disk_to_scsi_id(int fd, __u32 *id)
474 {
475 /* from an open block device, try to retrieve it scsi_id */
476 struct stat st;
477 char path[256];
478 char *c1, *c2;
479 DIR *dir;
480 struct dirent *de;
481
482 if (fstat(fd, &st))
483 return 1;
484
485 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device",
486 major(st.st_rdev), minor(st.st_rdev));
487
488 dir = opendir(path);
489 if (!dir)
490 return 1;
491
492 de = readdir(dir);
493 while (de) {
494 if (strncmp("scsi_disk:", de->d_name,
495 strlen("scsi_disk:")) == 0)
496 break;
497 de = readdir(dir);
498 }
499 closedir(dir);
500
501 if (!de)
502 return 1;
503
504 c1 = strchr(de->d_name, ':');
505 c1++;
506 c2 = strchr(c1, ':');
507 *c2 = '\0';
508 *id = strtol(c1, NULL, 10) << 24; /* host */
509 c1 = c2 + 1;
510 c2 = strchr(c1, ':');
511 *c2 = '\0';
512 *id |= strtol(c1, NULL, 10) << 16; /* channel */
513 c1 = c2 + 1;
514 c2 = strchr(c1, ':');
515 *c2 = '\0';
516 *id |= strtol(c1, NULL, 10) << 8; /* lun */
517 c1 = c2 + 1;
518 *id |= strtol(c1, NULL, 10); /* id */
519
520 return 0;
521 }
522
523
524 int sysfs_unique_holder(int devnum, long rdev)
525 {
526 /* Check that devnum is a holder of rdev,
527 * and is the only holder.
528 * we should be locked against races by
529 * an O_EXCL on devnum
530 */
531 DIR *dir;
532 struct dirent *de;
533 char dirname[100];
534 char l;
535 int found = 0;
536 sprintf(dirname, "/sys/dev/block/%d:%d/holders",
537 major(rdev), minor(rdev));
538 dir = opendir(dirname);
539 errno = ENOENT;
540 if (!dir)
541 return 0;
542 l = strlen(dirname);
543 while ((de = readdir(dir)) != NULL) {
544 char buf[10];
545 int n;
546 int mj, mn;
547 char c;
548 int fd;
549
550 if (de->d_ino == 0)
551 continue;
552 if (de->d_name[0] == '.')
553 continue;
554 strcpy(dirname+l, "/");
555 strcat(dirname+l, de->d_name);
556 strcat(dirname+l, "/dev");
557 fd = open(dirname, O_RDONLY);
558 if (fd < 0) {
559 errno = ENOENT;
560 break;
561 }
562 n = read(fd, buf, sizeof(buf)-1);
563 close(fd);
564 buf[n] = 0;
565 if (sscanf(buf, "%d:%d%c", &mj, &mn, &c) != 3 ||
566 c != '\n') {
567 errno = ENOENT;
568 break;
569 }
570 if (mj != MD_MAJOR)
571 mn = -1-(mn>>6);
572
573 if (devnum != mn) {
574 errno = EEXIST;
575 break;
576 }
577 found = 1;
578 }
579 closedir(dir);
580 if (de)
581 return 0;
582 else
583 return found;
584 }