summaryrefslogtreecommitdiffstats
path: root/sysfs.c
diff options
context:
space:
mode:
authorDan Williams <dan.j.williams@intel.com>2008-06-13 17:27:30 -0700
committerDan Williams <dan.j.williams@intel.com>2008-06-13 17:27:30 -0700
commit90c8b707149a069a934bd0e2a0824edf1f6dfdf0 (patch)
treecbf55fdeb882dd7282166ddf438e329e8893cc70 /sysfs.c
parentc42ec1ed43a811ad866be58f8f7fa460e22a3109 (diff)
downloadmdadm-90c8b707149a069a934bd0e2a0824edf1f6dfdf0.tar.gz
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 'sysfs.c')
-rw-r--r--sysfs.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/sysfs.c b/sysfs.c
index bbb9cd3..37b8b09 100644
--- a/sysfs.c
+++ b/sysfs.c
@@ -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;
+}
+