2 * sysfs - extract md related information from sysfs. Part of:
3 * mdadm - manage Linux "md" devices aka RAID arrays.
5 * Copyright (C) 2006-2009 Neil Brown <neilb@suse.de>
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.
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.
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
23 * Email: <neilb@suse.de>
30 #define MAX_SYSFS_PATH_LEN 120
32 int load_sys(char *path
, char *buf
, int len
)
34 int fd
= open(path
, O_RDONLY
);
38 n
= read(fd
, buf
, len
);
43 if (n
&& buf
[n
-1] == '\n')
48 void sysfs_free(struct mdinfo
*sra
)
51 struct mdinfo
*sra2
= sra
->next
;
53 struct mdinfo
*d
= sra
->devs
;
58 free(sra
->bb
.entries
);
64 int sysfs_open(char *devnm
, char *devname
, char *attr
)
66 char fname
[MAX_SYSFS_PATH_LEN
];
69 snprintf(fname
, MAX_SYSFS_PATH_LEN
, "/sys/block/%s/md/", devnm
);
71 strncat(fname
, devname
, MAX_SYSFS_PATH_LEN
- strlen(fname
));
72 strncat(fname
, "/", MAX_SYSFS_PATH_LEN
- strlen(fname
));
74 strncat(fname
, attr
, MAX_SYSFS_PATH_LEN
- strlen(fname
));
75 fd
= open(fname
, O_RDWR
);
76 if (fd
< 0 && errno
== EACCES
)
77 fd
= open(fname
, O_RDONLY
);
81 void sysfs_init_dev(struct mdinfo
*mdi
, dev_t devid
)
83 snprintf(mdi
->sys_name
,
84 sizeof(mdi
->sys_name
), "dev-%s", devid2kname(devid
));
87 int sysfs_init(struct mdinfo
*mdi
, int fd
, char *devnm
)
90 char fname
[MAX_SYSFS_PATH_LEN
];
100 snprintf(fname
, MAX_SYSFS_PATH_LEN
, "/sys/block/%s/md", devnm
);
102 if (stat(fname
, &stb
))
104 if (!S_ISDIR(stb
.st_mode
))
106 strcpy(mdi
->sys_name
, devnm
);
113 struct mdinfo
*sysfs_read(int fd
, char *devnm
, unsigned long options
)
115 char fname
[PATH_MAX
];
120 struct mdinfo
*dev
, **devp
;
124 sra
= xcalloc(1, sizeof(*sra
));
125 if (sysfs_init(sra
, fd
, devnm
)) {
130 sprintf(fname
, "/sys/block/%s/md/", sra
->sys_name
);
131 base
= fname
+ strlen(fname
);
134 if (options
& GET_VERSION
) {
135 strcpy(base
, "metadata_version");
136 if (load_sys(fname
, buf
, sizeof(buf
)))
138 if (strncmp(buf
, "none", 4) == 0) {
139 sra
->array
.major_version
=
140 sra
->array
.minor_version
= -1;
141 strcpy(sra
->text_version
, "");
142 } else if (strncmp(buf
, "external:", 9) == 0) {
143 sra
->array
.major_version
= -1;
144 sra
->array
.minor_version
= -2;
145 strcpy(sra
->text_version
, buf
+9);
148 &sra
->array
.major_version
,
149 &sra
->array
.minor_version
);
150 strcpy(sra
->text_version
, buf
);
153 if (options
& GET_LEVEL
) {
154 strcpy(base
, "level");
155 if (load_sys(fname
, buf
, sizeof(buf
)))
157 sra
->array
.level
= map_name(pers
, buf
);
159 if (options
& GET_LAYOUT
) {
160 strcpy(base
, "layout");
161 if (load_sys(fname
, buf
, sizeof(buf
)))
163 sra
->array
.layout
= strtoul(buf
, NULL
, 0);
165 if (options
& (GET_DISKS
|GET_STATE
)) {
166 strcpy(base
, "raid_disks");
167 if (load_sys(fname
, buf
, sizeof(buf
)))
169 sra
->array
.raid_disks
= strtoul(buf
, NULL
, 0);
171 if (options
& GET_COMPONENT
) {
172 strcpy(base
, "component_size");
173 if (load_sys(fname
, buf
, sizeof(buf
)))
175 sra
->component_size
= strtoull(buf
, NULL
, 0);
176 /* sysfs reports "K", but we want sectors */
177 sra
->component_size
*= 2;
179 if (options
& GET_CHUNK
) {
180 strcpy(base
, "chunk_size");
181 if (load_sys(fname
, buf
, sizeof(buf
)))
183 sra
->array
.chunk_size
= strtoul(buf
, NULL
, 0);
185 if (options
& GET_CACHE
) {
186 strcpy(base
, "stripe_cache_size");
187 if (load_sys(fname
, buf
, sizeof(buf
)))
188 /* Probably level doesn't support it */
191 sra
->cache_size
= strtoul(buf
, NULL
, 0);
193 if (options
& GET_MISMATCH
) {
194 strcpy(base
, "mismatch_cnt");
195 if (load_sys(fname
, buf
, sizeof(buf
)))
197 sra
->mismatch_cnt
= strtoul(buf
, NULL
, 0);
199 if (options
& GET_SAFEMODE
) {
206 strcpy(base
, "safe_mode_delay");
207 if (load_sys(fname
, buf
, sizeof(buf
)))
210 /* remove a period, and count digits after it */
212 for (i
= 0; i
< len
; i
++) {
214 if (isdigit(buf
[i
])) {
219 } else if (buf
[i
] == '.') {
224 msec
= strtoul(buf
, NULL
, 10);
225 msec
= (msec
* 1000) / scale
;
226 sra
->safe_mode_delay
= msec
;
228 if (options
& GET_BITMAP_LOCATION
) {
229 strcpy(base
, "bitmap/location");
230 if (load_sys(fname
, buf
, sizeof(buf
)))
232 if (strncmp(buf
, "file", 4) == 0)
233 sra
->bitmap_offset
= 1;
234 else if (strncmp(buf
, "none", 4) == 0)
235 sra
->bitmap_offset
= 0;
236 else if (buf
[0] == '+')
237 sra
->bitmap_offset
= strtol(buf
+1, NULL
, 10);
242 if (options
& GET_ARRAY_STATE
) {
243 strcpy(base
, "array_state");
244 if (load_sys(fname
, buf
, sizeof(buf
)))
246 sra
->array_state
= map_name(sysfs_array_states
, buf
);
249 if (options
& GET_CONSISTENCY_POLICY
) {
250 strcpy(base
, "consistency_policy");
251 if (load_sys(fname
, buf
, sizeof(buf
)))
252 sra
->consistency_policy
= CONSISTENCY_POLICY_UNKNOWN
;
254 sra
->consistency_policy
= map_name(consistency_policies
,
258 if (! (options
& GET_DEVS
))
261 /* Get all the devices as well */
263 dir
= opendir(fname
);
266 sra
->array
.spare_disks
= 0;
267 sra
->array
.active_disks
= 0;
268 sra
->array
.failed_disks
= 0;
269 sra
->array
.working_disks
= 0;
273 while ((de
= readdir(dir
)) != NULL
) {
275 if (de
->d_ino
== 0 ||
276 strncmp(de
->d_name
, "dev-", 4) != 0)
278 strcpy(base
, de
->d_name
);
279 dbase
= base
+ strlen(base
);
282 dev
= xcalloc(1, sizeof(*dev
));
284 /* Always get slot, major, minor */
285 strcpy(dbase
, "slot");
286 if (load_sys(fname
, buf
, sizeof(buf
))) {
287 /* hmm... unable to read 'slot' maybe the device
290 strcpy(dbase
, "block");
291 if (readlink(fname
, buf
, sizeof(buf
)) < 0 &&
292 errno
!= ENAMETOOLONG
) {
293 /* ...yup device is gone */
297 /* slot is unreadable but 'block' link
298 * still intact... something bad is happening
306 strcpy(dev
->sys_name
, de
->d_name
);
307 dev
->disk
.raid_disk
= strtoul(buf
, &ep
, 10);
308 if (*ep
) dev
->disk
.raid_disk
= -1;
310 sra
->array
.nr_disks
++;
311 strcpy(dbase
, "block/dev");
312 if (load_sys(fname
, buf
, sizeof(buf
))) {
313 /* assume this is a stale reference to a hot
316 if (!(options
& GET_DEVS_ALL
)) {
321 sscanf(buf
, "%d:%d", &dev
->disk
.major
, &dev
->disk
.minor
);
324 if (!(options
& GET_DEVS_ALL
)) {
325 /* special case check for block devices that can go 'offline' */
326 strcpy(dbase
, "block/device/state");
327 if (load_sys(fname
, buf
, sizeof(buf
)) == 0 &&
328 strncmp(buf
, "offline", 7) == 0) {
334 /* finally add this disk to the array */
339 if (options
& GET_OFFSET
) {
340 strcpy(dbase
, "offset");
341 if (load_sys(fname
, buf
, sizeof(buf
)))
343 dev
->data_offset
= strtoull(buf
, NULL
, 0);
344 strcpy(dbase
, "new_offset");
345 if (load_sys(fname
, buf
, sizeof(buf
)) == 0)
346 dev
->new_data_offset
= strtoull(buf
, NULL
, 0);
348 dev
->new_data_offset
= dev
->data_offset
;
350 if (options
& GET_SIZE
) {
351 strcpy(dbase
, "size");
352 if (load_sys(fname
, buf
, sizeof(buf
)))
354 dev
->component_size
= strtoull(buf
, NULL
, 0) * 2;
356 if (options
& GET_STATE
) {
358 strcpy(dbase
, "state");
359 if (load_sys(fname
, buf
, sizeof(buf
)))
361 if (strstr(buf
, "faulty"))
362 dev
->disk
.state
|= (1<<MD_DISK_FAULTY
);
364 sra
->array
.working_disks
++;
365 if (strstr(buf
, "in_sync")) {
366 dev
->disk
.state
|= (1<<MD_DISK_SYNC
);
367 sra
->array
.active_disks
++;
369 if (dev
->disk
.state
== 0)
370 sra
->array
.spare_disks
++;
373 if (options
& GET_ERROR
) {
374 strcpy(buf
, "errors");
375 if (load_sys(fname
, buf
, sizeof(buf
)))
377 dev
->errors
= strtoul(buf
, NULL
, 0);
381 if ((options
& GET_STATE
) && sra
->array
.raid_disks
)
382 sra
->array
.failed_disks
= sra
->array
.raid_disks
-
383 sra
->array
.active_disks
- sra
->array
.spare_disks
;
395 int sysfs_attr_match(const char *attr
, const char *str
)
397 /* See if attr, read from a sysfs file, matches
398 * str. They must either be the same, or attr can
399 * have a trailing newline or comma
401 while (*attr
&& *str
&& *attr
== *str
) {
406 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
411 int sysfs_match_word(const char *word
, char **list
)
414 for (n
=0; list
[n
]; n
++)
415 if (sysfs_attr_match(word
, list
[n
]))
420 unsigned long long get_component_size(int fd
)
422 /* Find out the component size of the array.
423 * We cannot trust GET_ARRAY_INFO ioctl as it's
424 * size field is only 32bits.
425 * So look in /sys/block/mdXXX/md/component_size
427 * This returns in units of sectors.
430 char fname
[MAX_SYSFS_PATH_LEN
];
434 snprintf(fname
, MAX_SYSFS_PATH_LEN
,
435 "/sys/block/%s/md/component_size", stat2devnm(&stb
));
436 fd
= open(fname
, O_RDONLY
);
439 n
= read(fd
, fname
, sizeof(fname
));
441 if (n
< 0 || n
== sizeof(fname
))
444 return strtoull(fname
, NULL
, 10) * 2;
447 int sysfs_set_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
448 char *name
, char *val
)
450 char fname
[MAX_SYSFS_PATH_LEN
];
454 snprintf(fname
, MAX_SYSFS_PATH_LEN
, "/sys/block/%s/md/%s/%s",
455 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
456 fd
= open(fname
, O_WRONLY
);
459 n
= write(fd
, val
, strlen(val
));
461 if (n
!= strlen(val
)) {
462 dprintf("failed to write '%s' to '%s' (%s)\n",
463 val
, fname
, strerror(errno
));
469 int sysfs_set_num(struct mdinfo
*sra
, struct mdinfo
*dev
,
470 char *name
, unsigned long long val
)
473 sprintf(valstr
, "%llu", val
);
474 return sysfs_set_str(sra
, dev
, name
, valstr
);
477 int sysfs_set_num_signed(struct mdinfo
*sra
, struct mdinfo
*dev
,
478 char *name
, long long val
)
481 sprintf(valstr
, "%lli", val
);
482 return sysfs_set_str(sra
, dev
, name
, valstr
);
485 int sysfs_uevent(struct mdinfo
*sra
, char *event
)
487 char fname
[MAX_SYSFS_PATH_LEN
];
491 snprintf(fname
, MAX_SYSFS_PATH_LEN
, "/sys/block/%s/uevent",
493 fd
= open(fname
, O_WRONLY
);
496 n
= write(fd
, event
, strlen(event
));
498 if (n
!= (int)strlen(event
)) {
499 dprintf("failed to write '%s' to '%s' (%s)\n",
500 event
, fname
, strerror(errno
));
506 int sysfs_attribute_available(struct mdinfo
*sra
, struct mdinfo
*dev
, char *name
)
508 char fname
[MAX_SYSFS_PATH_LEN
];
511 snprintf(fname
, MAX_SYSFS_PATH_LEN
, "/sys/block/%s/md/%s/%s",
512 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
514 return stat(fname
, &st
) == 0;
517 int sysfs_get_fd(struct mdinfo
*sra
, struct mdinfo
*dev
,
520 char fname
[MAX_SYSFS_PATH_LEN
];
523 snprintf(fname
, MAX_SYSFS_PATH_LEN
, "/sys/block/%s/md/%s/%s",
524 sra
->sys_name
, dev
?dev
->sys_name
:"", name
);
525 fd
= open(fname
, O_RDWR
);
527 fd
= open(fname
, O_RDONLY
);
531 int sysfs_fd_get_ll(int fd
, unsigned long long *val
)
538 n
= read(fd
, buf
, sizeof(buf
));
539 if (n
<= 0 || n
== sizeof(buf
))
542 *val
= strtoull(buf
, &ep
, 0);
543 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
548 int sysfs_get_ll(struct mdinfo
*sra
, struct mdinfo
*dev
,
549 char *name
, unsigned long long *val
)
554 fd
= sysfs_get_fd(sra
, dev
, name
);
557 n
= sysfs_fd_get_ll(fd
, val
);
562 int sysfs_fd_get_two(int fd
, unsigned long long *v1
, unsigned long long *v2
)
564 /* two numbers in this sysfs file, either
574 n
= read(fd
, buf
, sizeof(buf
));
575 if (n
<= 0 || n
== sizeof(buf
))
578 *v1
= strtoull(buf
, &ep
, 0);
579 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
581 while (*ep
== ' ' || *ep
== '/' || *ep
== '(')
583 *v2
= strtoull(ep
, &ep2
, 0);
584 if (ep2
== ep
|| (*ep2
!= 0 && *ep2
!= '\n' && *ep2
!= ' ' && *ep2
!= ')')) {
591 int sysfs_get_two(struct mdinfo
*sra
, struct mdinfo
*dev
,
592 char *name
, unsigned long long *v1
, unsigned long long *v2
)
597 fd
= sysfs_get_fd(sra
, dev
, name
);
600 n
= sysfs_fd_get_two(fd
, v1
, v2
);
605 int sysfs_fd_get_str(int fd
, char *val
, int size
)
610 n
= read(fd
, val
, size
);
611 if (n
<= 0 || n
== size
)
617 int sysfs_get_str(struct mdinfo
*sra
, struct mdinfo
*dev
,
618 char *name
, char *val
, int size
)
623 fd
= sysfs_get_fd(sra
, dev
, name
);
626 n
= sysfs_fd_get_str(fd
, val
, size
);
631 int sysfs_set_safemode(struct mdinfo
*sra
, unsigned long ms
)
640 sprintf(delay
, "%ld.%03ld\n", sec
, msec
);
641 /* this '\n' ^ needed for kernels older than 2.6.28 */
642 return sysfs_set_str(sra
, NULL
, "safe_mode_delay", delay
);
645 int sysfs_set_array(struct mdinfo
*info
, int vers
)
649 int raid_disks
= info
->array
.raid_disks
;
652 if (info
->array
.major_version
== -1 &&
653 info
->array
.minor_version
== -2) {
656 strcat(strcpy(ver
, "external:"), info
->text_version
);
658 /* meta version might already be set if we are setting
659 * new geometry for a reshape. In that case we don't
660 * want to over-write the 'readonly' flag that is
661 * stored in the metadata version. So read the current
662 * version first, and preserve the flag
664 if (sysfs_get_str(info
, NULL
, "metadata_version",
666 if (strlen(buf
) >= 9 && buf
[9] == '-')
669 if ((vers
% 100) < 2 ||
670 sysfs_set_str(info
, NULL
, "metadata_version",
672 pr_err("This kernel does not support external metadata.\n");
676 if (info
->array
.level
< 0)
677 return 0; /* FIXME */
678 rv
|= sysfs_set_str(info
, NULL
, "level",
679 map_num(pers
, info
->array
.level
));
680 if (info
->reshape_active
&& info
->delta_disks
!= UnSet
)
681 raid_disks
-= info
->delta_disks
;
682 rv
|= sysfs_set_num(info
, NULL
, "raid_disks", raid_disks
);
683 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->array
.chunk_size
);
684 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->array
.layout
);
685 rv
|= sysfs_set_num(info
, NULL
, "component_size", info
->component_size
/2);
686 if (info
->custom_array_size
) {
689 rc
= sysfs_set_num(info
, NULL
, "array_size",
690 info
->custom_array_size
/2);
691 if (rc
&& errno
== ENOENT
) {
692 pr_err("This kernel does not have the md/array_size attribute, the array may be larger than expected\n");
698 if (info
->array
.level
> 0)
699 rv
|= sysfs_set_num(info
, NULL
, "resync_start", info
->resync_start
);
701 if (info
->reshape_active
) {
702 rv
|= sysfs_set_num(info
, NULL
, "reshape_position",
703 info
->reshape_progress
);
704 rv
|= sysfs_set_num(info
, NULL
, "chunk_size", info
->new_chunk
);
705 rv
|= sysfs_set_num(info
, NULL
, "layout", info
->new_layout
);
706 rv
|= sysfs_set_num(info
, NULL
, "raid_disks",
707 info
->array
.raid_disks
);
708 /* We don't set 'new_level' here. That can only happen
709 * once the reshape completes.
713 if (info
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
714 if (sysfs_set_str(info
, NULL
, "consistency_policy",
715 map_num(consistency_policies
,
716 info
->consistency_policy
))) {
717 pr_err("This kernel does not support PPL. Falling back to consistency-policy=resync.\n");
718 info
->consistency_policy
= CONSISTENCY_POLICY_RESYNC
;
725 int sysfs_add_disk(struct mdinfo
*sra
, struct mdinfo
*sd
, int resume
)
733 sprintf(dv
, "%d:%d", sd
->disk
.major
, sd
->disk
.minor
);
734 rv
= sysfs_set_str(sra
, NULL
, "new_dev", dv
);
738 memset(nm
, 0, sizeof(nm
));
739 dname
= devid2kname(makedev(sd
->disk
.major
, sd
->disk
.minor
));
740 strcpy(sd
->sys_name
, "dev-");
741 strcpy(sd
->sys_name
+4, dname
);
743 /* test write to see if 'recovery_start' is available */
744 if (resume
&& sd
->recovery_start
< MaxSector
&&
745 sysfs_set_num(sra
, sd
, "recovery_start", 0)) {
746 sysfs_set_str(sra
, sd
, "state", "remove");
750 rv
= sysfs_set_num(sra
, sd
, "offset", sd
->data_offset
);
751 rv
|= sysfs_set_num(sra
, sd
, "size", (sd
->component_size
+1) / 2);
752 if (sra
->array
.level
!= LEVEL_CONTAINER
) {
753 if (sra
->consistency_policy
== CONSISTENCY_POLICY_PPL
) {
754 rv
|= sysfs_set_num(sra
, sd
, "ppl_sector", sd
->ppl_sector
);
755 rv
|= sysfs_set_num(sra
, sd
, "ppl_size", sd
->ppl_size
);
757 if (sd
->recovery_start
== MaxSector
)
758 /* This can correctly fail if array isn't started,
759 * yet, so just ignore status for now.
761 sysfs_set_str(sra
, sd
, "state", "insync");
762 if (sd
->disk
.raid_disk
>= 0)
763 rv
|= sysfs_set_num(sra
, sd
, "slot", sd
->disk
.raid_disk
);
765 sysfs_set_num(sra
, sd
, "recovery_start", sd
->recovery_start
);
767 if (sd
->bb
.supported
) {
768 if (sysfs_set_str(sra
, sd
, "state", "external_bbl")) {
770 * backward compatibility - if kernel doesn't support
771 * bad blocks for external metadata, let it continue
772 * as long as there are none known so far
775 pr_err("The kernel has no support for bad blocks in external metadata\n");
780 for (i
= 0; i
< sd
->bb
.count
; i
++) {
782 const struct md_bb_entry
*entry
= &sd
->bb
.entries
[i
];
784 snprintf(s
, sizeof(s
) - 1, "%llu %d\n", entry
->sector
,
786 rv
|= sysfs_set_str(sra
, sd
, "bad_blocks", s
);
793 int sysfs_disk_to_sg(int fd
)
795 /* from an open block device, try find and open its corresponding
796 * scsi_generic interface
801 char sg_major_minor
[10];
805 int major
, minor
, rv
;
810 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device",
811 major(st
.st_rdev
), minor(st
.st_rdev
));
819 if (strncmp("scsi_generic:", de
->d_name
,
820 strlen("scsi_generic:")) == 0)
829 snprintf(sg_path
, sizeof(sg_path
), "%s/%s/dev", path
, de
->d_name
);
830 fd
= open(sg_path
, O_RDONLY
);
834 rv
= read(fd
, sg_major_minor
, sizeof(sg_major_minor
));
836 if (rv
< 0 || rv
== sizeof(sg_major_minor
))
839 sg_major_minor
[rv
- 1] = '\0';
841 c
= strchr(sg_major_minor
, ':');
844 major
= strtol(sg_major_minor
, NULL
, 10);
845 minor
= strtol(c
, NULL
, 10);
846 snprintf(path
, sizeof(path
), "/dev/.tmp.md.%d:%d:%d",
847 (int) getpid(), major
, minor
);
848 if (mknod(path
, S_IFCHR
|0600, makedev(major
, minor
))==0) {
849 fd
= open(path
, O_RDONLY
);
858 int sysfs_disk_to_scsi_id(int fd
, __u32
*id
)
860 /* from an open block device, try to retrieve it scsi_id */
865 int host
, bus
, target
, lun
;
870 snprintf(path
, sizeof(path
), "/sys/dev/block/%d:%d/device/scsi_device",
871 major(st
.st_rdev
), minor(st
.st_rdev
));
877 for (de
= readdir(dir
); de
; de
= readdir(dir
)) {
880 if (de
->d_type
!= DT_DIR
)
883 count
= sscanf(de
->d_name
, "%d:%d:%d:%d", &host
, &bus
, &target
, &lun
);
892 *id
= (host
<< 24) | (bus
<< 16) | (target
<< 8) | (lun
<< 0);
896 int sysfs_unique_holder(char *devnm
, long rdev
)
898 /* Check that devnm is a holder of rdev,
899 * and is the only holder.
900 * we should be locked against races by
903 * 0 - not unique, not even a holder
904 * 1 - unique, this is the only holder.
905 * 2/3 - not unique, there is another holder
906 * -1 - error, cannot find the holders
913 sprintf(dirname
, "/sys/dev/block/%d:%d/holders",
914 major(rdev
), minor(rdev
));
915 dir
= opendir(dirname
);
919 while ((de
= readdir(dir
)) != NULL
) {
926 if (de
->d_name
[0] == '.')
928 strcpy(dirname
+l
, "/");
929 strcat(dirname
+l
, de
->d_name
);
930 n
= readlink(dirname
, buf
, sizeof(buf
)-1);
934 sl
= strrchr(buf
, '/');
939 if (strcmp(devnm
, sl
) == 0)
948 int sysfs_freeze_array(struct mdinfo
*sra
)
950 /* Try to freeze resync/rebuild on this array/container.
951 * Return -1 if the array is busy,
952 * return 0 if this kernel doesn't support 'frozen'
953 * return 1 if it worked.
957 if (!sysfs_attribute_available(sra
, NULL
, "sync_action"))
958 return 1; /* no sync_action == frozen */
959 if (sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) <= 0)
961 if (strcmp(buf
, "frozen\n") == 0)
964 if (strcmp(buf
, "idle\n") != 0 && strcmp(buf
, "recover\n") != 0)
966 if (sysfs_set_str(sra
, NULL
, "sync_action", "frozen") < 0)
971 int sysfs_wait(int fd
, int *msec
)
973 /* Wait up to '*msec' for fd to have an exception condition.
974 * if msec == NULL, wait indefinitely.
981 n
= select(fd
+1, NULL
, NULL
, &fds
, NULL
);
985 struct timeval start
, end
, tv
;
986 gettimeofday(&start
, NULL
);
989 tv
.tv_usec
= (*msec
)*1000;
991 tv
.tv_sec
= (*msec
)/1000;
994 n
= select(fd
+1, NULL
, NULL
, &fds
, &tv
);
995 gettimeofday(&end
, NULL
);
996 end
.tv_sec
-= start
.tv_sec
;
997 *msec
-= (end
.tv_sec
* 1000 + end
.tv_usec
/1000
998 - start
.tv_usec
/1000) + 1;