From: Sage Weil Date: Fri, 24 Mar 2017 00:18:18 +0000 (-0500) Subject: common/blkdev: implement get_device_by_fd X-Git-Tag: v12.0.2~164^2~13 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=0c854a9d351de797afd89c9ac8c1cab40addd1da;p=ceph.git common/blkdev: implement get_device_by_fd Complement to get_device_by_uuid, but we provide an fd. Signed-off-by: Sage Weil --- diff --git a/src/common/blkdev.cc b/src/common/blkdev.cc index f3262d53a81c..17fe0e6ac835 100644 --- a/src/common/blkdev.cc +++ b/src/common/blkdev.cc @@ -222,6 +222,27 @@ int get_device_by_uuid(uuid_d dev_uuid, const char* label, char* partition, blkid_put_cache(cache); return rc; } + +int get_device_by_fd(int fd, char *partition, char *device) +{ + struct stat st; + int r = fstat(fd, &st); + if (r < 0) { + return -EINVAL; // hrm. + } + char *t = blkid_devno_to_devname(st.st_rdev); + if (!t) { + return -EINVAL; + } + strcpy(partition, t); + dev_t diskdev; + r = blkid_devno_to_wholedisk(st.st_rdev, device, PATH_MAX, &diskdev); + if (r < 0) { + return -EINVAL; + } + return 0; +} + #elif defined(__APPLE__) #include @@ -291,6 +312,10 @@ int get_device_by_uuid(uuid_d dev_uuid, const char* label, char* partition, { return -EOPNOTSUPP; } +int get_device_by_fd(int fd, char* partition, char* device) +{ + return -EOPNOTSUPP; +} #else int get_block_device_size(int fd, int64_t *psize) { @@ -317,4 +342,9 @@ int get_device_by_uuid(uuid_d dev_uuid, const char* label, char* partition, { return -EOPNOTSUPP; } + +int get_device_by_fd(int fd, char* partition, char* device) +{ + return -EOPNOTSUPP; +} #endif diff --git a/src/common/blkdev.h b/src/common/blkdev.h index e37f5694e84a..b0459b750df7 100644 --- a/src/common/blkdev.h +++ b/src/common/blkdev.h @@ -12,4 +12,5 @@ extern bool block_device_is_rotational(const char *devname); extern int block_device_discard(int fd, int64_t offset, int64_t len); extern int get_device_by_uuid(uuid_d dev_uuid, const char* label, char* partition, char* device); +extern int get_device_by_fd(int fd, char* partition, char* device); #endif