]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
7c673cae FG |
4 | #ifndef __CEPH_COMMON_BLKDEV_H |
5 | #define __CEPH_COMMON_BLKDEV_H | |
6 | ||
11fdf7f2 TL |
7 | #include <set> |
8 | #include <string> | |
9 | #include "json_spirit/json_spirit_value.h" | |
10 | ||
11 | enum blkdev_prop_t { | |
12 | BLKDEV_PROP_DEV, | |
13 | BLKDEV_PROP_DISCARD_GRANULARITY, | |
14 | BLKDEV_PROP_MODEL, | |
15 | BLKDEV_PROP_ROTATIONAL, | |
16 | BLKDEV_PROP_SERIAL, | |
17 | BLKDEV_PROP_VENDOR, | |
18 | BLKDEV_PROP_NUMA_NODE, | |
19 | BLKDEV_PROP_NUMA_CPUS, | |
20 | BLKDEV_PROP_NUMPROPS, | |
21 | }; | |
22 | ||
23 | extern int get_device_by_path(const char *path, char* partition, char* device, size_t max); | |
24 | ||
25 | extern std::string _decode_model_enc(const std::string& in); // helper, exported only so we can unit test | |
26 | ||
27 | extern std::string get_device_id(const std::string& devname, | |
28 | std::string *err=0); | |
29 | extern void get_dm_parents(const std::string& dev, std::set<std::string> *ls); | |
30 | extern int block_device_get_metrics(const std::string& devname, int timeout, | |
31 | json_spirit::mValue *result); | |
32 | ||
33 | // do everything to translate a device to the raw physical devices that | |
34 | // back it, including partitions -> wholedisks and dm -> constituent devices. | |
35 | extern void get_raw_devices(const std::string& in, | |
36 | std::set<std::string> *ls); | |
37 | ||
38 | // for VDO | |
39 | /// return an op fd for the sysfs stats dir, if this is a VDO device | |
40 | extern int get_vdo_stats_handle(const char *devname, std::string *vdo_name); | |
41 | extern int64_t get_vdo_stat(int fd, const char *property); | |
42 | extern bool get_vdo_utilization(int fd, uint64_t *total, uint64_t *avail); | |
43 | ||
44 | class BlkDev { | |
45 | public: | |
46 | BlkDev(int fd); | |
47 | BlkDev(const std::string& devname); | |
48 | /* GoogleMock requires a virtual destructor */ | |
49 | virtual ~BlkDev() {} | |
50 | ||
51 | // from an fd | |
52 | int discard(int64_t offset, int64_t len) const; | |
53 | int get_size(int64_t *psize) const; | |
54 | int get_devid(dev_t *id) const; | |
55 | int partition(char* partition, size_t max) const; | |
56 | // from a device (e.g., "sdb") | |
57 | bool support_discard() const; | |
58 | bool is_nvme() const; | |
59 | bool is_rotational() const; | |
60 | int get_numa_node(int *node) const; | |
61 | int dev(char *dev, size_t max) const; | |
62 | int vendor(char *vendor, size_t max) const; | |
63 | int model(char *model, size_t max) const; | |
64 | int serial(char *serial, size_t max) const; | |
65 | ||
66 | /* virtual for testing purposes */ | |
67 | virtual const char *sysfsdir() const; | |
68 | virtual int wholedisk(char* device, size_t max) const; | |
69 | int wholedisk(std::string *s) const { | |
70 | char out[PATH_MAX] = {0}; | |
71 | int r = wholedisk(out, sizeof(out)); | |
72 | if (r < 0) { | |
73 | return r; | |
74 | } | |
75 | *s = out; | |
76 | return r; | |
77 | } | |
78 | ||
79 | protected: | |
80 | int64_t get_int_property(blkdev_prop_t prop) const; | |
81 | int64_t get_string_property( blkdev_prop_t prop, char *val, | |
82 | size_t maxlen) const; | |
83 | ||
84 | private: | |
85 | int fd = -1; | |
86 | std::string devname; | |
87 | }; | |
7c673cae FG |
88 | |
89 | #endif |