]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_device.c
cmd_device_add improvements
[bcachefs-tools-debian] / cmd_device.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <getopt.h>
4 #include <libgen.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/ioctl.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.h>
14
15 #include "cmds.h"
16 #include "libbcache.h"
17 #include "linux/bcache-ioctl.h"
18 #include "tools-util.h"
19
20 /* This code belongs under show_fs */
21 #if 0
22
23 struct bcache_dev {
24         unsigned        nr;
25         const char      *name;
26
27         unsigned        has_metadata;
28         unsigned        has_data;
29         const char      *state;
30         unsigned        tier;
31
32         u64             bucket_size;
33         u64             first_bucket;
34         u64             nbuckets;
35
36         /* XXX: dirty != used, it doesn't count metadata */
37         u64             bytes_dirty;
38 };
39
40 static struct bcache_dev fill_dev(const char *dev_name, unsigned nr, int dir)
41 {
42         return (struct bcache_dev) {
43                 .nr             = nr,
44                 .name           = dev_name,
45
46                 .has_metadata   = read_file_u64(dir, "has_metadata"),
47                 .has_data       = read_file_u64(dir, "has_data"),
48                 .state          = read_file_str(dir, "state"),
49                 .tier           = read_file_u64(dir, "tier"),
50
51                 .bucket_size    = read_file_u64(dir, "bucket_size_bytes"),
52                 .first_bucket   = read_file_u64(dir, "first_bucket"),
53                 .nbuckets       = read_file_u64(dir, "nbuckets"),
54                 .bytes_dirty    = read_file_u64(dir, "dirty_bytes"),
55         };
56 }
57
58 static void show_dev(struct bcache_dev *dev)
59 {
60         u64 capacity = (dev->nbuckets - dev->first_bucket) *
61                 dev->bucket_size;
62         /*
63          * XXX: show fragmentation information, cached/dirty information
64          */
65
66         printf("Device %u (/dev/%s):\n"
67                "    Has metadata:\t%u\n"
68                "    Has dirty data:\t%u\n"
69                "    State:\t\t%s\n"
70                "    Tier:\t\t%u\n"
71                "    Size:\t\t%llu\n"
72                "    Used:\t\t%llu\n"
73                "    Free:\t\t%llu\n"
74                "    Use%%:\t\t%llu\n",
75                dev->nr, dev->name,
76                dev->has_metadata,
77                dev->has_data,
78                dev->state,
79                dev->tier,
80                capacity,
81                dev->bytes_dirty,
82                capacity - dev->bytes_dirty,
83                (dev->bytes_dirty * 100) / capacity);
84 }
85
86 int cmd_device_show(int argc, char *argv[])
87 {
88         int human_readable = 0;
89         NihOption opts[] = {
90         //      { int shortoption, char *longoption, char *help, NihOptionGroup, char *argname, void *value, NihOptionSetter}
91
92                 { 'h',  "human-readable",               N_("print sizes in powers of 1024 (e.g., 1023M)"),
93                         NULL, NULL,     &human_readable,        NULL},
94                 NIH_OPTION_LAST
95         };
96         char **args = bch_nih_init(argc, argv, opts);
97
98         if (argc != 2)
99                 die("Please supply a single device");
100
101         struct bcache_handle fs = bcache_fs_open(argv[1]);
102         struct dirent *entry;
103
104         struct bcache_dev devices[256];
105         unsigned i, j, nr_devices = 0, nr_active_tiers = 0;
106
107         unsigned tiers[BCH_TIER_MAX]; /* number of devices in each tier */
108         memset(tiers, 0, sizeof(tiers));
109
110         while ((entry = readdir(fs.sysfs))) {
111                 unsigned nr;
112                 int pos = 0;
113
114                 sscanf(entry->d_name, "cache%u%n", &nr, &pos);
115                 if (pos != strlen(entry->d_name))
116                         continue;
117
118                 char link[PATH_MAX];
119                 if (readlinkat(dirfd(fs.sysfs), entry->d_name,
120                                link, sizeof(link)) < 0)
121                         die("readlink error: %s\n", strerror(errno));
122
123                 char *dev_name = basename(dirname(link));
124
125                 int fd = xopenat(dirfd(fs.sysfs), entry->d_name, O_RDONLY);
126
127                 devices[nr_devices] = fill_dev(strdup(dev_name), nr, fd);
128                 tiers[devices[nr_devices].tier]++;
129                 nr_devices++;
130
131                 close(fd);
132         }
133
134         for (i = 0; i < BCH_TIER_MAX; i++)
135                 if (tiers[i])
136                         nr_active_tiers++;
137
138         /* Print out devices sorted by tier: */
139         bool first = true;
140
141         for (i = 0; i < BCH_TIER_MAX; i++) {
142                 if (!tiers[i])
143                         continue;
144
145                 if (nr_active_tiers > 1) {
146                         if (!first)
147                                 printf("\n");
148                         first = false;
149                         printf("Tier %u:\n\n", i);
150                 }
151
152                 for (j = 0; j < nr_devices; j++) {
153                         if (devices[j].tier != i)
154                                 continue;
155
156                         if (!first)
157                                 printf("\n");
158                         first = false;
159                         show_dev(&devices[j]);
160                 }
161         }
162
163         return 0;
164 }
165 #endif
166
167 int cmd_device_show(int argc, char *argv[])
168 {
169         struct bch_sb *sb;
170
171         if (argc != 2)
172                 die("please supply a single device");
173
174         sb = bcache_super_read(argv[1]);
175         bcache_super_print(sb, HUMAN_READABLE);
176
177         return 0;
178 }
179
180 static void device_add_usage(void)
181 {
182         puts("bcache device_add - add a device to an existing filesystem\n"
183              "Usage: bcache device_add [OPTION]... filesystem device\n"
184              "\n"
185              "Options:\n"
186              "      --fs_size=size          Size of filesystem on device\n"
187              "      --bucket=size           Bucket size\n"
188              "      --discard               Enable discards\n"
189              "  -t, --tier=#                Higher tier (e.g. 1) indicates slower devices\n"
190              "  -f, --force                 Use device even if it appears to already be formatted\n"
191              "  -h, --help                  Display this help and exit\n"
192              "\n"
193              "Report bugs to <linux-bcache@vger.kernel.org>");
194 }
195
196 static const struct option device_add_opts[] = {
197         { "fs_size",            required_argument,      NULL, 'S' },
198         { "bucket",             required_argument,      NULL, 'B' },
199         { "discard",            no_argument,            NULL, 'D' },
200         { "tier",               required_argument,      NULL, 't' },
201         { "force",              no_argument,            NULL, 'f' },
202         { NULL }
203 };
204
205 int cmd_device_add(int argc, char *argv[])
206 {
207         struct format_opts format_opts = format_opts_default();
208         struct dev_opts dev_opts = { 0 };
209         bool force = false;
210         int opt;
211
212         while ((opt = getopt_long(argc, argv, "t:fh",
213                                   device_add_opts, NULL)) != -1)
214                 switch (opt) {
215                 case 'S':
216                         if (bch_strtoull_h(optarg, &dev_opts.size))
217                                 die("invalid filesystem size");
218
219                         dev_opts.size >>= 9;
220                         break;
221                 case 'B':
222                         dev_opts.bucket_size =
223                                 hatoi_validate(optarg, "bucket size");
224                         break;
225                 case 'D':
226                         dev_opts.discard = true;
227                         break;
228                 case 't':
229                         if (kstrtouint(optarg, 10, &dev_opts.tier) ||
230                             dev_opts.tier >= BCH_TIER_MAX)
231                                 die("invalid tier");
232                         break;
233                 case 'f':
234                         force = true;
235                         break;
236                 case 'h':
237                         device_add_usage();
238                         exit(EXIT_SUCCESS);
239                 }
240
241         if (argc - optind != 2)
242                 die("Please supply a filesystem and a device to add");
243
244         struct bcache_handle fs = bcache_fs_open(argv[optind]);
245
246         dev_opts.path = argv[optind + 1];
247         dev_opts.fd = open_for_format(dev_opts.path, force);
248
249         format_opts.block_size  =
250                 read_file_u64(fs.sysfs_fd, "block_size_bytes") >> 9;
251         format_opts.btree_node_size =
252                 read_file_u64(fs.sysfs_fd, "btree_node_size_bytes") >> 9;
253
254         struct bch_sb *sb = bcache_format(format_opts, &dev_opts, 1);
255         free(sb);
256         fsync(dev_opts.fd);
257         close(dev_opts.fd);
258
259         struct bch_ioctl_disk_add ia = {
260                 .dev = (__u64) dev_opts.path,
261         };
262
263         xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_ADD, &ia);
264
265         return 0;
266 }
267
268 static void device_fail_usage(void)
269 {
270         puts("bcache device_fail - mark a device as failed\n"
271              "Usage: bcache device_fail filesystem [devices]\n"
272              "\n"
273              "Options:\n"
274              "  -f, --force                 Force removal, even if some data\n"
275              "                              couldn't be migrated\n"
276              "      --force-metadata        Force removal, even if some metadata\n"
277              "                              couldn't be migrated\n"
278              "  -h, --help                  display this help and exit\n"
279              "Report bugs to <linux-bcache@vger.kernel.org>");
280         exit(EXIT_SUCCESS);
281 }
282
283 int cmd_device_fail(int argc, char *argv[])
284 {
285         static const struct option longopts[] = {
286                 { "force-degraded",             0, NULL, 'f' },
287                 //{ "force-data-lost",          0, NULL, 'F' },
288                 //{ "force-metadata-lost",      0, NULL, 'F' },
289                 { "help",                       0, NULL, 'h' },
290                 { NULL }
291         };
292         int opt, force_degraded = 0, force_data = 0, force_metadata = 0;
293
294         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
295                 switch (opt) {
296                 case 'f':
297                         force_degraded = 1;
298                         break;
299                 case 'h':
300                         device_fail_usage();
301                 }
302
303         if (argc - optind < 2)
304                 die("Please supply a filesystem and at least one device to fail");
305
306         struct bcache_handle fs = bcache_fs_open(argv[optind]);
307
308         for (unsigned i = optind + 1; i < argc; i++) {
309                 struct bch_ioctl_disk_set_state ir = {
310                         .dev            = (__u64) argv[i],
311                         .new_state      = BCH_MEMBER_STATE_FAILED,
312                 };
313
314                 if (force_degraded)
315                         ir.flags |= BCH_FORCE_IF_DEGRADED;
316                 if (force_data)
317                         ir.flags |= BCH_FORCE_IF_DATA_LOST;
318                 if (force_metadata)
319                         ir.flags |= BCH_FORCE_IF_METADATA_LOST;
320
321                 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_SET_STATE, &ir);
322         }
323
324         return 0;
325 }
326
327 static void device_remove_usage(void)
328 {
329         puts("bcache device_remove - remove one or more devices from a filesystem\n"
330              "Usage: bcache device_remove filesystem [devices]\n"
331              "\n"
332              "Options:\n"
333              "  -f, --force                 Force removal, even if some data\n"
334              "                              couldn't be migrated\n"
335              "      --force-metadata        Force removal, even if some metadata\n"
336              "                              couldn't be migrated\n"
337              "  -h, --help                  display this help and exit\n"
338              "Report bugs to <linux-bcache@vger.kernel.org>");
339         exit(EXIT_SUCCESS);
340 }
341
342 int cmd_device_remove(int argc, char *argv[])
343 {
344         static const struct option longopts[] = {
345                 { "force",              0, NULL, 'f' },
346                 { "force-metadata",     0, NULL, 'F' },
347                 { "help",               0, NULL, 'h' },
348                 { NULL }
349         };
350         int opt, force_data = 0, force_metadata = 0;
351
352         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
353                 switch (opt) {
354                 case 'f':
355                         force_data = 1;
356                         break;
357                 case 'F':
358                         force_metadata = 1;
359                         break;
360                 case 'h':
361                         device_remove_usage();
362                 }
363
364         if (argc - optind < 2)
365                 die("Please supply a filesystem and at least one device to remove");
366
367         struct bcache_handle fs = bcache_fs_open(argv[optind]);
368
369         for (unsigned i = optind + 1; i < argc; i++) {
370                 struct bch_ioctl_disk_remove ir = {
371                         .dev = (__u64) argv[i],
372                 };
373
374                 if (force_data)
375                         ir.flags |= BCH_FORCE_IF_DATA_LOST;
376                 if (force_metadata)
377                         ir.flags |= BCH_FORCE_IF_METADATA_LOST;
378
379                 xioctl(fs.ioctl_fd, BCH_IOCTL_DISK_REMOVE, &ir);
380         }
381
382         return 0;
383 }