12 #include <sys/ioctl.h>
14 #include <sys/types.h>
17 #include <uuid/uuid.h>
19 #include "ccan/crc/crc.h"
21 #include "linux/bcache-ioctl.h"
22 #include "tools-util.h"
27 struct units_buf __pr_units(u64 v, enum units units)
33 snprintf(ret.b, sizeof(ret.b), "%llu", v << 9);
36 snprintf(ret.b, sizeof(ret.b), "%llu", v);
42 int exp = log(v) / log(1024);
43 snprintf(ret.b, sizeof(ret.b), "%.1f%c",
47 snprintf(ret.b, sizeof(ret.b), "%llu", v);
56 /* Argument parsing stuff: */
58 /* File parsing (i.e. sysfs) */
60 char *read_file_str(int dirfd, const char *path)
62 int fd = openat(dirfd, path, O_RDONLY);
65 die("Unable to open %s\n", path);
68 if (fstat(fd, &statbuf) < 0)
71 char *buf = malloc(statbuf.st_size + 1);
73 int len = read(fd, buf, statbuf.st_size);
75 die("read error while reading from file %s\n", path);
78 if (len && buf[len - 1] == '\n')
86 u64 read_file_u64(int dirfd, const char *path)
88 char *buf = read_file_str(dirfd, path);
89 u64 ret = strtoll(buf, NULL, 10);
95 /* String list options: */
97 ssize_t read_string_list_or_die(const char *opt, const char * const list[],
100 ssize_t v = bch_read_string_list(opt, list);
102 die("Bad %s %s", msg, opt);
107 /* Returns size of file or block device: */
108 u64 get_size(const char *path, int fd)
113 if (fstat(fd, &statbuf))
114 die("Error statting %s: %s", path, strerror(errno));
116 if (!S_ISBLK(statbuf.st_mode))
117 return statbuf.st_size;
119 if (ioctl(fd, BLKGETSIZE64, &ret))
120 die("Error getting block device size on %s: %s\n",
121 path, strerror(errno));
126 /* Returns blocksize in units of 512 byte sectors: */
127 unsigned get_blocksize(const char *path, int fd)
130 if (fstat(fd, &statbuf))
131 die("Error statting %s: %s", path, strerror(errno));
133 if (!S_ISBLK(statbuf.st_mode))
134 return statbuf.st_blksize >> 9;
137 if (ioctl(fd, BLKPBSZGET, &ret))
138 die("Error getting blocksize on %s: %s\n",
139 path, strerror(errno));
144 /* Global control device: */
145 int bcachectl_open(void)
147 int fd = open("/dev/bcache-ctl", O_RDWR);
149 die("Can't open bcache device: %s", strerror(errno));
154 /* Filesystem handles (ioctl, sysfs dir): */
156 #define SYSFS_BASE "/sys/fs/bcache/"
158 struct bcache_handle bcache_fs_open(const char *path)
160 struct bcache_handle ret;
163 if (!uuid_parse(path, tmp)) {
164 /* It's a UUID, look it up in sysfs: */
166 char *sysfs = alloca(strlen(SYSFS_BASE) + strlen(path) + 1);
167 sprintf(sysfs, "%s%s", SYSFS_BASE, path);
169 ret.sysfs_fd = open(sysfs, O_RDONLY);
171 die("Unable to open %s\n", path);
173 char *minor = read_file_str(ret.sysfs_fd, "minor");
174 char *ctl = alloca(20 + strlen(minor));
176 sprintf(ctl, "/dev/bcache%s-ctl", minor);
179 ret.ioctl_fd = open(ctl, O_RDWR);
180 if (ret.ioctl_fd < 0)
181 die("Error opening control device: %s\n",
186 ret.ioctl_fd = open(path, O_RDONLY);
187 if (ret.ioctl_fd < 0)
188 die("Error opening %s: %s\n",
189 path, strerror(errno));
191 struct bch_ioctl_query_uuid uuid;
192 if (ioctl(ret.ioctl_fd, BCH_IOCTL_QUERY_UUID, &uuid))
193 die("ioctl error (not a bcache fs?): %s\n",
197 uuid_unparse(uuid.uuid.b, uuid_str);
199 char *sysfs = alloca(strlen(SYSFS_BASE) + strlen(uuid_str) + 1);
200 sprintf(sysfs, "%s%s", SYSFS_BASE, uuid_str);
202 ret.sysfs_fd = open(sysfs, O_RDONLY);
203 if (ret.sysfs_fd < 0)
204 die("Unable to open sysfs dir %s: %s\n",
205 sysfs, strerror(errno));
213 const char *short_yes = "yY";
218 fputs(" (y,n) ", stdout);
221 if (getline(&buf, &buflen, stdin) < 0)
222 die("error reading from standard input");
224 ret = strchr(short_yes, buf[0]);