diff options
author | Dan Williams <dan.j.williams@intel.com> | 2008-06-14 02:27:30 +0200 |
---|---|---|
committer | Dan Williams <dan.j.williams@intel.com> | 2008-06-14 02:27:30 +0200 |
commit | 90c8b707149a069a934bd0e2a0824edf1f6dfdf0 (patch) | |
tree | cbf55fdeb882dd7282166ddf438e329e8893cc70 /sysfs.c | |
parent | ddf: fix up validate_geometry to ignore other containers (diff) | |
download | mdadm-90c8b707149a069a934bd0e2a0824edf1f6dfdf0.tar.xz mdadm-90c8b707149a069a934bd0e2a0824edf1f6dfdf0.zip |
sysfs: provide a helper function for locating scsi_generic interfaces
imsm records and validates this data in its metadata
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Diffstat (limited to '')
-rw-r--r-- | sysfs.c | 65 |
1 files changed, 65 insertions, 0 deletions
@@ -399,3 +399,68 @@ int sysfs_add_disk(struct mdinfo *sra, struct mdinfo *sd) } return rv; } + +int sysfs_disk_to_sg(int fd) +{ + /* from an open block device, try find and open its corresponding + * scsi_generic interface + */ + struct stat st; + char path[256]; + char sg_path[256]; + char sg_major_minor[8]; + char *c; + DIR *dir; + struct dirent *de; + int major, minor, rv; + + if (fstat(fd, &st)) + return -1; + + snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/device", + major(st.st_rdev), minor(st.st_rdev)); + + dir = opendir(path); + if (!dir) + return -1; + + de = readdir(dir); + while (de) { + if (strncmp("scsi_generic:", de->d_name, + strlen("scsi_generic:")) == 0) + break; + de = readdir(dir); + } + closedir(dir); + + if (!de) + return -1; + + snprintf(sg_path, sizeof(sg_path), "%s/%s/dev", path, de->d_name); + fd = open(sg_path, O_RDONLY); + if (fd < 0) + return fd; + + rv = read(fd, sg_major_minor, sizeof(sg_major_minor)); + close(fd); + if (rv < 0) + return -1; + else + sg_major_minor[rv - 1] = '\0'; + + c = strchr(sg_major_minor, ':'); + *c = '\0'; + c++; + major = strtol(sg_major_minor, NULL, 10); + minor = strtol(c, NULL, 10); + snprintf(path, sizeof(path), "/dev/.tmp.md.%d:%d:%d", + (int) getpid(), major, minor); + if (mknod(path, S_IFCHR|0600, makedev(major, minor))==0) { + fd = open(path, O_RDONLY); + unlink(path); + return fd; + } + + return -1; +} + |