]> git.ipfire.org Git - thirdparty/mdadm.git/blob - sysfs.c
Discard devnum in favour of devnm
[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-2009 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 #include <ctype.h>
29
30 int load_sys(char *path, char *buf)
31 {
32 int fd = open(path, O_RDONLY);
33 int n;
34 if (fd < 0)
35 return -1;
36 n = read(fd, buf, 1024);
37 close(fd);
38 if (n <0 || n >= 1024)
39 return -1;
40 buf[n] = 0;
41 if (n && buf[n-1] == '\n')
42 buf[n-1] = 0;
43 return 0;
44 }
45
46 void sysfs_free(struct mdinfo *sra)
47 {
48 while (sra) {
49 struct mdinfo *sra2 = sra->next;
50 while (sra->devs) {
51 struct mdinfo *d = sra->devs;
52 sra->devs = d->next;
53 free(d);
54 }
55 free(sra);
56 sra = sra2;
57 }
58 }
59
60 int sysfs_open(char *devnm, char *devname, char *attr)
61 {
62 char fname[50];
63 int fd;
64
65 sprintf(fname, "/sys/block/%s/md/", devnm);
66 if (devname) {
67 strcat(fname, devname);
68 strcat(fname, "/");
69 }
70 strcat(fname, attr);
71 fd = open(fname, O_RDWR);
72 if (fd < 0 && errno == EACCES)
73 fd = open(fname, O_RDONLY);
74 return fd;
75 }
76
77 void sysfs_init(struct mdinfo *mdi, int fd, char *devnm)
78 {
79 mdi->sys_name[0] = 0;
80 if (fd >= 0) {
81 mdu_version_t vers;
82 if (ioctl(fd, RAID_VERSION, &vers) != 0)
83 return;
84 devnm = fd2devnm(fd);
85 }
86 if (devnm == NULL)
87 return;
88 strcpy(mdi->sys_name, devnm);
89 }
90
91
92 struct mdinfo *sysfs_read(int fd, char *devnm, unsigned long options)
93 {
94 char fname[PATH_MAX];
95 char buf[PATH_MAX];
96 char *base;
97 char *dbase;
98 struct mdinfo *sra;
99 struct mdinfo *dev;
100 DIR *dir = NULL;
101 struct dirent *de;
102
103 sra = xcalloc(1, sizeof(*sra));
104 sysfs_init(sra, fd, devnm);
105 if (sra->sys_name[0] == 0) {
106 free(sra);
107 return NULL;
108 }
109
110 sprintf(fname, "/sys/block/%s/md/", sra->sys_name);
111 base = fname + strlen(fname);
112
113 sra->devs = NULL;
114 if (options & GET_VERSION) {
115 strcpy(base, "metadata_version");
116 if (load_sys(fname, buf))
117 goto abort;
118 if (strncmp(buf, "none", 4) == 0) {
119 sra->array.major_version =
120 sra->array.minor_version = -1;
121 strcpy(sra->text_version, "");
122 } else if (strncmp(buf, "external:", 9) == 0) {
123 sra->array.major_version = -1;
124 sra->array.minor_version = -2;
125 strcpy(sra->text_version, buf+9);
126 } else {
127 sscanf(buf, "%d.%d",
128 &sra->array.major_version,
129 &sra->array.minor_version);
130 strcpy(sra->text_version, buf);
131 }
132 }
133 if (options & GET_LEVEL) {
134 strcpy(base, "level");
135 if (load_sys(fname, buf))
136 goto abort;
137 sra->array.level = map_name(pers, buf);
138 }
139 if (options & GET_LAYOUT) {
140 strcpy(base, "layout");
141 if (load_sys(fname, buf))
142 goto abort;
143 sra->array.layout = strtoul(buf, NULL, 0);
144 }
145 if (options & GET_DISKS) {
146 strcpy(base, "raid_disks");
147 if (load_sys(fname, buf))
148 goto abort;
149 sra->array.raid_disks = strtoul(buf, NULL, 0);
150 }
151 if (options & GET_DEGRADED) {
152 strcpy(base, "degraded");
153 if (load_sys(fname, buf))
154 goto abort;
155 sra->array.failed_disks = strtoul(buf, NULL, 0);
156 }
157 if (options & GET_COMPONENT) {
158 strcpy(base, "component_size");
159 if (load_sys(fname, buf))
160 goto abort;
161 sra->component_size = strtoull(buf, NULL, 0);
162 /* sysfs reports "K", but we want sectors */
163 sra->component_size *= 2;
164 }
165 if (options & GET_CHUNK) {
166 strcpy(base, "chunk_size");
167 if (load_sys(fname, buf))
168 goto abort;
169 sra->array.chunk_size = strtoul(buf, NULL, 0);
170 }
171 if (options & GET_CACHE) {
172 strcpy(base, "stripe_cache_size");
173 if (load_sys(fname, buf))
174 /* Probably level doesn't support it */
175 sra->cache_size = 0;
176 else
177 sra->cache_size = strtoul(buf, NULL, 0);
178 }
179 if (options & GET_MISMATCH) {
180 strcpy(base, "mismatch_cnt");
181 if (load_sys(fname, buf))
182 goto abort;
183 sra->mismatch_cnt = strtoul(buf, NULL, 0);
184 }
185 if (options & GET_SAFEMODE) {
186 int scale = 1;
187 int dot = 0;
188 unsigned i;
189 unsigned long msec;
190 size_t len;
191
192 strcpy(base, "safe_mode_delay");
193 if (load_sys(fname, buf))
194 goto abort;
195
196 /* remove a period, and count digits after it */
197 len = strlen(buf);
198 for (i = 0; i < len; i++) {
199 if (dot) {
200 if (isdigit(buf[i])) {
201 buf[i-1] = buf[i];
202 scale *= 10;
203 }
204 buf[i] = 0;
205 } else if (buf[i] == '.') {
206 dot=1;
207 buf[i] = 0;
208 }
209 }
210 msec = strtoul(buf, NULL, 10);
211 msec = (msec * 1000) / scale;
212 sra->safe_mode_delay = msec;
213 }
214 if (options & GET_BITMAP_LOCATION) {
215 strcpy(base, "bitmap/location");
216 if (load_sys(fname, buf))
217 goto abort;
218 if (strncmp(buf, "file", 4) == 0)
219 sra->bitmap_offset = 1;
220 else if (strncmp(buf, "none", 4) == 0)
221 sra->bitmap_offset = 0;
222 else if (buf[0] == '+')
223 sra->bitmap_offset = strtol(buf+1, NULL, 10);
224 else
225 goto abort;
226 }
227
228 if (! (options & GET_DEVS))
229 return sra;
230
231 /* Get all the devices as well */
232 *base = 0;
233 dir = opendir(fname);
234 if (!dir)
235 goto abort;
236 sra->array.spare_disks = 0;
237
238 while ((de = readdir(dir)) != NULL) {
239 char *ep;
240 if (de->d_ino == 0 ||
241 strncmp(de->d_name, "dev-", 4) != 0)
242 continue;
243 strcpy(base, de->d_name);
244 dbase = base + strlen(base);
245 *dbase++ = '/';
246
247 dev = xmalloc(sizeof(*dev));
248
249 /* Always get slot, major, minor */
250 strcpy(dbase, "slot");
251 if (load_sys(fname, buf)) {
252 /* hmm... unable to read 'slot' maybe the device
253 * is going away?
254 */
255 strcpy(dbase, "block");
256 if (readlink(fname, buf, sizeof(buf)) < 0 &&
257 errno != ENAMETOOLONG) {
258 /* ...yup device is gone */
259 free(dev);
260 continue;
261 } else {
262 /* slot is unreadable but 'block' link
263 * still intact... something bad is happening
264 * so abort
265 */
266 free(dev);
267 goto abort;
268 }
269
270 }
271 strcpy(dev->sys_name, de->d_name);
272 dev->disk.raid_disk = strtoul(buf, &ep, 10);
273 if (*ep) dev->disk.raid_disk = -1;
274
275 strcpy(dbase, "block/dev");
276 if (load_sys(fname, buf)) {
277 /* assume this is a stale reference to a hot
278 * removed device
279 */
280 free(dev);
281 continue;
282 }
283 sscanf(buf, "%d:%d", &dev->disk.major, &dev->disk.minor);
284
285 /* special case check for block devices that can go 'offline' */
286 strcpy(dbase, "block/device/state");
287 if (load_sys(fname, buf) == 0 &&
288 strncmp(buf, "offline", 7) == 0) {
289 free(dev);
290 continue;
291 }
292
293 /* finally add this disk to the array */
294 dev->next = sra->devs;
295 sra->devs = dev;
296
297 if (options & GET_OFFSET) {
298 strcpy(dbase, "offset");
299 if (load_sys(fname, buf))
300 goto abort;
301 dev->data_offset = strtoull(buf, NULL, 0);
302 strcpy(dbase, "new_offset");
303 if (load_sys(fname, buf) == 0)
304 dev->new_data_offset = strtoull(buf, NULL, 0);
305 else
306 dev->new_data_offset = dev->data_offset;
307 }
308 if (options & GET_SIZE) {
309 strcpy(dbase, "size");
310 if (load_sys(fname, buf))
311 goto abort;
312 dev->component_size = strtoull(buf, NULL, 0) * 2;
313 }
314 if (options & GET_STATE) {
315 dev->disk.state = 0;
316 strcpy(dbase, "state");
317 if (load_sys(fname, buf))
318 goto abort;
319 if (strstr(buf, "in_sync"))
320 dev->disk.state |= (1<<MD_DISK_SYNC);
321 if (strstr(buf, "faulty"))
322 dev->disk.state |= (1<<MD_DISK_FAULTY);
323 if (dev->disk.state == 0)
324 sra->array.spare_disks++;
325 }
326 if (options & GET_ERROR) {
327 strcpy(buf, "errors");
328 if (load_sys(fname, buf))
329 goto abort;
330 dev->errors = strtoul(buf, NULL, 0);
331 }
332 }
333 closedir(dir);
334 return sra;
335
336 abort:
337 if (dir)
338 closedir(dir);
339 sysfs_free(sra);
340 return NULL;
341 }
342
343 int sysfs_attr_match(const char *attr, const char *str)
344 {
345 /* See if attr, read from a sysfs file, matches
346 * str. They must either be the same, or attr can
347 * have a trailing newline or comma
348 */
349 while (*attr && *str && *attr == *str) {
350 attr++;
351 str++;
352 }
353
354 if (*str || (*attr && *attr != ',' && *attr != '\n'))
355 return 0;
356 return 1;
357 }
358
359 int sysfs_match_word(const char *word, char **list)
360 {
361 int n;
362 for (n=0; list[n]; n++)
363 if (sysfs_attr_match(word, list[n]))
364 break;
365 return n;
366 }
367
368 unsigned long long get_component_size(int fd)
369 {
370 /* Find out the component size of the array.
371 * We cannot trust GET_ARRAY_INFO ioctl as it's
372 * size field is only 32bits.
373 * So look in /sys/block/mdXXX/md/component_size
374 *
375 * This returns in units of sectors.
376 */
377 struct stat stb;
378 char fname[50];
379 int n;
380 if (fstat(fd, &stb)) return 0;
381 if (major(stb.st_rdev) != (unsigned)get_mdp_major())
382 sprintf(fname, "/sys/block/md%d/md/component_size",
383 (int)minor(stb.st_rdev));
384 else
385 sprintf(fname, "/sys/block/md_d%d/md/component_size",
386 (int)minor(stb.st_rdev)>>MdpMinorShift);
387 fd = open(fname, O_RDONLY);
388 if (fd < 0)
389 return 0;
390 n = read(fd, fname, sizeof(fname));
391 close(fd);
392 if (n < 0 || n == sizeof(fname))
393 return 0;
394 fname[n] = 0;
395 return strtoull(fname, NULL, 10) * 2;
396 }
397
398 int sysfs_set_str(struct mdinfo *sra, struct mdinfo *dev,
399 char *name, char *val)
400 {
401 char fname[50];
402 unsigned int n;
403 int fd;
404
405 sprintf(fname, "/sys/block/%s/md/%s/%s",
406 sra->sys_name, dev?dev->sys_name:"", name);
407 fd = open(fname, O_WRONLY);
408 if (fd < 0)
409 return -1;
410 n = write(fd, val, strlen(val));
411 close(fd);
412 if (n != strlen(val)) {
413 dprintf(Name ": failed to write '%s' to '%s' (%s)\n",
414 val, fname, strerror(errno));
415 return -1;
416 }
417 return 0;
418 }
419
420 int sysfs_set_num(struct mdinfo *sra, struct mdinfo *dev,
421 char *name, unsigned long long val)
422 {
423 char valstr[50];
424 sprintf(valstr, "%llu", val);
425 return sysfs_set_str(sra, dev, name, valstr);
426 }
427
428 int sysfs_set_num_signed(struct mdinfo *sra, struct mdinfo *dev,
429 char *name, long long val)
430 {
431 char valstr[50];
432 sprintf(valstr, "%lli", val);
433 return sysfs_set_str(sra, dev, name, valstr);
434 }
435
436 int sysfs_uevent(struct mdinfo *sra, char *event)
437 {
438 char fname[50];
439 int n;
440 int fd;
441
442 sprintf(fname, "/sys/block/%s/uevent",
443 sra->sys_name);
444 fd = open(fname, O_WRONLY);
445 if (fd < 0)
446 return -1;
447 n = write(fd, event, strlen(event));
448 close(fd);
449 if (n != (int)strlen(event)) {
450 dprintf(Name ": failed to write '%s' to '%s' (%s)\n",
451 event, fname, strerror(errno));
452 return -1;
453 }
454 return 0;
455 }
456
457 int sysfs_attribute_available(struct mdinfo *sra, struct mdinfo *dev, char *name)
458 {
459 char fname[50];
460 struct stat st;
461
462 sprintf(fname, "/sys/block/%s/md/%s/%s",
463 sra->sys_name, dev?dev->sys_name:"", name);
464
465 return stat(fname, &st) == 0;
466 }
467
468 int sysfs_get_fd(struct mdinfo *sra, struct mdinfo *dev,
469 char *name)
470 {
471 char fname[50];
472 int fd;
473
474 sprintf(fname, "/sys/block/%s/md/%s/%s",
475 sra->sys_name, dev?dev->sys_name:"", name);
476 fd = open(fname, O_RDWR);
477 if (fd < 0)
478 fd = open(fname, O_RDONLY);
479 return fd;
480 }
481
482 int sysfs_fd_get_ll(int fd, unsigned long long *val)
483 {
484 char buf[50];
485 int n;
486 char *ep;
487
488 lseek(fd, 0, 0);
489 n = read(fd, buf, sizeof(buf));
490 if (n <= 0)
491 return -2;
492 buf[n] = 0;
493 *val = strtoull(buf, &ep, 0);
494 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
495 return -1;
496 return 0;
497 }
498
499 int sysfs_get_ll(struct mdinfo *sra, struct mdinfo *dev,
500 char *name, unsigned long long *val)
501 {
502 int n;
503 int fd;
504
505 fd = sysfs_get_fd(sra, dev, name);
506 if (fd < 0)
507 return -1;
508 n = sysfs_fd_get_ll(fd, val);
509 close(fd);
510 return n;
511 }
512
513 int sysfs_fd_get_str(int fd, char *val, int size)
514 {
515 int n;
516
517 lseek(fd, 0, 0);
518 n = read(fd, val, size);
519 if (n <= 0)
520 return -1;
521 val[n] = 0;
522 return n;
523 }
524
525 int sysfs_get_str(struct mdinfo *sra, struct mdinfo *dev,
526 char *name, char *val, int size)
527 {
528 int n;
529 int fd;
530
531 fd = sysfs_get_fd(sra, dev, name);
532 if (fd < 0)
533 return -1;
534 n = sysfs_fd_get_str(fd, val, size);
535 close(fd);
536 return n;
537 }
538
539 int sysfs_set_safemode(struct mdinfo *sra, unsigned long ms)
540 {
541 unsigned long sec;
542 unsigned long msec;
543 char delay[30];
544
545 sec = ms / 1000;
546 msec = ms % 1000;
547
548 sprintf(delay, "%ld.%03ld\n", sec, msec);
549 /* this '\n' ^ needed for kernels older than 2.6.28 */
550 return sysfs_set_str(sra, NULL, "safe_mode_delay", delay);
551 }
552
553 int sysfs_set_array(struct mdinfo *info, int vers)
554 {
555 int rv = 0;
556 char ver[100];
557 int raid_disks = info->array.raid_disks;
558
559 ver[0] = 0;
560 if (info->array.major_version == -1 &&
561 info->array.minor_version == -2) {
562 char buf[1024];
563
564 strcat(strcpy(ver, "external:"), info->text_version);
565
566 /* meta version might already be set if we are setting
567 * new geometry for a reshape. In that case we don't
568 * want to over-write the 'readonly' flag that is
569 * stored in the metadata version. So read the current
570 * version first, and preserve the flag
571 */
572 if (sysfs_get_str(info, NULL, "metadata_version",
573 buf, 1024) > 0)
574 if (strlen(buf) >= 9 && buf[9] == '-')
575 ver[9] = '-';
576
577 if ((vers % 100) < 2 ||
578 sysfs_set_str(info, NULL, "metadata_version",
579 ver) < 0) {
580 pr_err("This kernel does not "
581 "support external metadata.\n");
582 return 1;
583 }
584 }
585 if (info->array.level < 0)
586 return 0; /* FIXME */
587 rv |= sysfs_set_str(info, NULL, "level",
588 map_num(pers, info->array.level));
589 if (info->reshape_active && info->delta_disks != UnSet)
590 raid_disks -= info->delta_disks;
591 rv |= sysfs_set_num(info, NULL, "raid_disks", raid_disks);
592 rv |= sysfs_set_num(info, NULL, "chunk_size", info->array.chunk_size);
593 rv |= sysfs_set_num(info, NULL, "layout", info->array.layout);
594 rv |= sysfs_set_num(info, NULL, "component_size", info->component_size/2);
595 if (info->custom_array_size) {
596 int rc;
597
598 rc = sysfs_set_num(info, NULL, "array_size",
599 info->custom_array_size/2);
600 if (rc && errno == ENOENT) {
601 pr_err("This kernel does not "
602 "have the md/array_size attribute, "
603 "the array may be larger than expected\n");
604 rc = 0;
605 }
606 rv |= rc;
607 }
608
609 if (info->array.level > 0)
610 rv |= sysfs_set_num(info, NULL, "resync_start", info->resync_start);
611
612 if (info->reshape_active) {
613 rv |= sysfs_set_num(info, NULL, "reshape_position",
614 info->reshape_progress);
615 rv |= sysfs_set_num(info, NULL, "chunk_size", info->new_chunk);
616 rv |= sysfs_set_num(info, NULL, "layout", info->new_layout);
617 rv |= sysfs_set_num(info, NULL, "raid_disks",
618 info->array.raid_disks);
619 /* We don't set 'new_level' here. That can only happen
620 * once the reshape completes.
621 */
622 }
623 return rv;
624 }
625
626 int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd, int resume)
627 {
628 char dv[PATH_MAX];
629 char nm[PATH_MAX];
630 char *dname;
631 int rv;
632
633 sprintf(dv, "%d:%d", sd->disk.major, sd->disk.minor);
634 rv = sysfs_set_str(sra, NULL, "new_dev", dv);
635 if (rv)
636 return rv;
637
638 memset(nm, 0, sizeof(nm));
639 dname = devid2devnm(makedev(sd->disk.major, sd->disk.minor));
640 strcpy(sd->sys_name, "dev-");
641 strcpy(sd->sys_name+4, dname);
642
643 /* test write to see if 'recovery_start' is available */
644 if (resume && sd->recovery_start < MaxSector &&
645 sysfs_set_num(sra, sd, "recovery_start", 0)) {
646 sysfs_set_str(sra, sd, "state", "remove");
647 return -1;
648 }
649
650 rv = sysfs_set_num(sra, sd, "offset", sd->data_offset);
651 rv |= sysfs_set_num(sra, sd, "size", (sd->component_size+1) / 2);
652 if (sra->array.level != LEVEL_CONTAINER) {
653 if (sd->recovery_start == MaxSector)
654 /* This can correctly fail if array isn't started,
655 * yet, so just ignore status for now.
656 */
657 sysfs_set_str(sra, sd, "state", "insync");
658 if (sd->disk.raid_disk >= 0)
659 rv |= sysfs_set_num(sra, sd, "slot", sd->disk.raid_disk);
660 if (resume)
661 sysfs_set_num(sra, sd, "recovery_start", sd->recovery_start);
662 }
663 return rv;
664 }
665
666 #if 0
667 int sysfs_disk_to_sg(int fd)
668 {
669 /* from an open block device, try find and open its corresponding
670 * scsi_generic interface
671 */
672 struct stat st;
673 char path[256];
674 char sg_path[256];
675 char sg_major_minor[8];
676 char *c;
677 DIR *dir;
678 struct dirent *de;
679 int major, minor, rv;
680
681 if (fstat(fd, &st))
682 return -1;
683
684 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device",
685 major(st.st_rdev), minor(st.st_rdev));
686
687 dir = opendir(path);
688 if (!dir)
689 return -1;
690
691 de = readdir(dir);
692 while (de) {
693 if (strncmp("scsi_generic:", de->d_name,
694 strlen("scsi_generic:")) == 0)
695 break;
696 de = readdir(dir);
697 }
698 closedir(dir);
699
700 if (!de)
701 return -1;
702
703 snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name);
704 fd = open(sg_path, O_RDONLY);
705 if (fd < 0)
706 return fd;
707
708 rv = read(fd, sg_major_minor, sizeof(sg_major_minor));
709 close(fd);
710 if (rv < 0)
711 return -1;
712 else
713 sg_major_minor[rv - 1] = '\0';
714
715 c = strchr(sg_major_minor, ':');
716 *c = '\0';
717 c++;
718 major = strtol(sg_major_minor, NULL, 10);
719 minor = strtol(c, NULL, 10);
720 snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d",
721 (int) getpid(), major, minor);
722 if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) {
723 fd = open(path, O_RDONLY);
724 unlink(path);
725 return fd;
726 }
727
728 return -1;
729 }
730 #endif
731
732 int sysfs_disk_to_scsi_id(int fd, __u32 *id)
733 {
734 /* from an open block device, try to retrieve it scsi_id */
735 struct stat st;
736 char path[256];
737 DIR *dir;
738 struct dirent *de;
739 int host, bus, target, lun;
740
741 if (fstat(fd, &st))
742 return 1;
743
744 snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device/scsi_device",
745 major(st.st_rdev), minor(st.st_rdev));
746
747 dir = opendir(path);
748 if (!dir)
749 return 1;
750
751 for (de = readdir(dir); de; de = readdir(dir)) {
752 int count;
753
754 if (de->d_type != DT_DIR)
755 continue;
756
757 count = sscanf(de->d_name, "%d:%d:%d:%d", &host, &bus, &target, &lun);
758 if (count == 4)
759 break;
760 }
761 closedir(dir);
762
763 if (!de)
764 return 1;
765
766 *id = (host << 24) | (bus << 16) | (target << 8) | (lun << 0);
767 return 0;
768 }
769
770
771 int sysfs_unique_holder(char *devnm, long rdev)
772 {
773 /* Check that devnm is a holder of rdev,
774 * and is the only holder.
775 * we should be locked against races by
776 * an O_EXCL on devnm
777 * Return values:
778 * 0 - not unique, not even a holder
779 * 1 - unique, this is the only holder.
780 * 2/3 - not unique, there is another holder
781 * -1 - error, cannot find the holders
782 */
783 DIR *dir;
784 struct dirent *de;
785 char dirname[100];
786 char l;
787 int ret = 0;
788 sprintf(dirname, "/sys/dev/block/%d:%d/holders",
789 major(rdev), minor(rdev));
790 dir = opendir(dirname);
791 if (!dir)
792 return -1;
793 l = strlen(dirname);
794 while ((de = readdir(dir)) != NULL) {
795 char buf[100];
796 char *sl;
797 int n;
798
799 if (de->d_ino == 0)
800 continue;
801 if (de->d_name[0] == '.')
802 continue;
803 strcpy(dirname+l, "/");
804 strcat(dirname+l, de->d_name);
805 n = readlink(dirname, buf, sizeof(buf)-1);
806 if (n <= 0)
807 continue;
808 buf[n] = 0;
809 sl = strrchr(buf, '/');
810 if (!sl)
811 continue;
812 sl++;
813
814 if (strcmp(devnm, sl) == 0)
815 ret |= 1;
816 else
817 ret |= 2;
818 }
819 closedir(dir);
820 return ret;
821 }
822
823 int sysfs_freeze_array(struct mdinfo *sra)
824 {
825 /* Try to freeze resync/rebuild on this array/container.
826 * Return -1 if the array is busy,
827 * return 0 if this kernel doesn't support 'frozen'
828 * return 1 if it worked.
829 */
830 char buf[20];
831
832 if (!sysfs_attribute_available(sra, NULL, "sync_action"))
833 return 1; /* no sync_action == frozen */
834 if (sysfs_get_str(sra, NULL, "sync_action", buf, 20) <= 0)
835 return 0;
836 if (strcmp(buf, "frozen\n") == 0)
837 /* Already frozen */
838 return 0;
839 if (strcmp(buf, "idle\n") != 0)
840 return -1;
841 if (sysfs_set_str(sra, NULL, "sync_action", "frozen") < 0)
842 return 0;
843 return 1;
844 }