10 #include <sys/ioctl.h>
12 #include <sys/types.h>
16 #include "libbcache.h"
17 #include "linux/bcache-ioctl.h"
19 /* This code belongs under show_fs */
26 unsigned has_metadata;
35 /* XXX: dirty != used, it doesn't count metadata */
39 static struct bcache_dev fill_dev(const char *dev_name, unsigned nr, int dir)
41 return (struct bcache_dev) {
45 .has_metadata = read_file_u64(dir, "has_metadata"),
46 .has_data = read_file_u64(dir, "has_data"),
47 .state = read_file_str(dir, "state"),
48 .tier = read_file_u64(dir, "tier"),
50 .bucket_size = read_file_u64(dir, "bucket_size_bytes"),
51 .first_bucket = read_file_u64(dir, "first_bucket"),
52 .nbuckets = read_file_u64(dir, "nbuckets"),
53 .bytes_dirty = read_file_u64(dir, "dirty_bytes"),
57 static void show_dev(struct bcache_dev *dev)
59 u64 capacity = (dev->nbuckets - dev->first_bucket) *
62 * XXX: show fragmentation information, cached/dirty information
65 printf("Device %u (/dev/%s):\n"
66 " Has metadata:\t%u\n"
67 " Has dirty data:\t%u\n"
81 capacity - dev->bytes_dirty,
82 (dev->bytes_dirty * 100) / capacity);
85 int cmd_device_show(int argc, char *argv[])
87 int human_readable = 0;
89 // { int shortoption, char *longoption, char *help, NihOptionGroup, char *argname, void *value, NihOptionSetter}
91 { 'h', "human-readable", N_("print sizes in powers of 1024 (e.g., 1023M)"),
92 NULL, NULL, &human_readable, NULL},
95 char **args = bch_nih_init(argc, argv, opts);
98 die("Please supply a single device");
100 struct bcache_handle fs = bcache_fs_open(argv[1]);
101 struct dirent *entry;
103 struct bcache_dev devices[256];
104 unsigned i, j, nr_devices = 0, nr_active_tiers = 0;
106 unsigned tiers[CACHE_TIERS]; /* number of devices in each tier */
107 memset(tiers, 0, sizeof(tiers));
109 while ((entry = readdir(fs.sysfs))) {
113 sscanf(entry->d_name, "cache%u%n", &nr, &pos);
114 if (pos != strlen(entry->d_name))
118 if (readlinkat(dirfd(fs.sysfs), entry->d_name,
119 link, sizeof(link)) < 0)
120 die("readlink error: %s\n", strerror(errno));
122 char *dev_name = basename(dirname(link));
124 int fd = openat(dirfd(fs.sysfs), entry->d_name, O_RDONLY);
126 die("couldn't open device %s: %s\n",
127 entry->d_name, strerror(errno));
129 devices[nr_devices] = fill_dev(strdup(dev_name), nr, fd);
130 tiers[devices[nr_devices].tier]++;
136 for (i = 0; i < CACHE_TIERS; i++)
140 /* Print out devices sorted by tier: */
143 for (i = 0; i < CACHE_TIERS; i++) {
147 if (nr_active_tiers > 1) {
151 printf("Tier %u:\n\n", i);
154 for (j = 0; j < nr_devices; j++) {
155 if (devices[j].tier != i)
161 show_dev(&devices[j]);
169 int cmd_device_show(int argc, char *argv[])
174 die("please supply a single device");
176 sb = bcache_super_read(argv[1]);
177 bcache_super_print(sb, HUMAN_READABLE);
182 int cmd_device_add(int argc, char *argv[])
185 die("Please supply a filesystem and at least one device to add");
187 struct bcache_handle fs = bcache_fs_open(argv[1]);
189 for (unsigned i = 2; i < argc; i++) {
190 struct bch_ioctl_disk_add ia = {
191 .dev = (__u64) argv[i],
194 if (ioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ADD, &ia))
195 die("BCH_IOCTL_DISK_ADD error: %s", strerror(errno));
201 static void usage(void)
203 puts("bcache device_remove - remove one or more devices from a filesystem\n"
204 "Usage: bcache device_remove filesystem [devices]\n"
207 " -f, --force Force removal, even if some data\n"
208 " couldn't be migrated\n"
209 " --force-metadata Force removal, even if some metadata\n"
210 " couldn't be migrated\n"
211 " -h, --help display this help and exit\n"
212 "Report bugs to <linux-bcache@vger.kernel.org>");
216 int cmd_device_remove(int argc, char *argv[])
218 static const struct option longopts[] = {
219 { "force", 0, NULL, 'f' },
220 { "force-metadata", 0, NULL, 'F' },
221 { "help", 0, NULL, 'h' },
224 int opt, force_data = 0, force_metadata = 0;
226 while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
239 die("Please supply a filesystem and at least one device to add");
241 struct bcache_handle fs = bcache_fs_open(argv[1]);
243 for (unsigned i = 2; i < argc; i++) {
244 struct bch_ioctl_disk_remove ir = {
245 .dev = (__u64) argv[i],
249 ir.flags |= BCH_FORCE_IF_DATA_MISSING;
251 ir.flags |= BCH_FORCE_IF_METADATA_MISSING;
253 if (ioctl(fs.ioctl_fd, BCH_IOCTL_DISK_REMOVE, &ir))
254 die("BCH_IOCTL_DISK_REMOVE error: %s\n", strerror(errno));