]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_device.c
Resizing
[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 "libbcachefs/bcachefs_ioctl.h"
16 #include "libbcachefs/super-io.h"
17 #include "cmds.h"
18 #include "libbcachefs.h"
19 #include "libbcachefs/opts.h"
20 #include "tools-util.h"
21
22 /* This code belongs under show_fs */
23 #if 0
24
25 struct bcache_dev {
26         unsigned        nr;
27         const char      *name;
28
29         unsigned        has_metadata;
30         unsigned        has_data;
31         const char      *state;
32         unsigned        tier;
33
34         u64             bucket_size;
35         u64             first_bucket;
36         u64             nbuckets;
37
38         /* XXX: dirty != used, it doesn't count metadata */
39         u64             bytes_dirty;
40 };
41
42 static struct bcache_dev fill_dev(const char *dev_name, unsigned nr, int dir)
43 {
44         return (struct bcache_dev) {
45                 .nr             = nr,
46                 .name           = dev_name,
47
48                 .has_metadata   = read_file_u64(dir, "has_metadata"),
49                 .has_data       = read_file_u64(dir, "has_data"),
50                 .state          = read_file_str(dir, "state"),
51                 .tier           = read_file_u64(dir, "tier"),
52
53                 .bucket_size    = read_file_u64(dir, "bucket_size_bytes"),
54                 .first_bucket   = read_file_u64(dir, "first_bucket"),
55                 .nbuckets       = read_file_u64(dir, "nbuckets"),
56                 .bytes_dirty    = read_file_u64(dir, "dirty_bytes"),
57         };
58 }
59
60 static void show_dev(struct bcache_dev *dev)
61 {
62         u64 capacity = (dev->nbuckets - dev->first_bucket) *
63                 dev->bucket_size;
64         /*
65          * XXX: show fragmentation information, cached/dirty information
66          */
67
68         printf("Device %u (/dev/%s):\n"
69                "    Has metadata:\t%u\n"
70                "    Has dirty data:\t%u\n"
71                "    State:\t\t%s\n"
72                "    Tier:\t\t%u\n"
73                "    Size:\t\t%llu\n"
74                "    Used:\t\t%llu\n"
75                "    Free:\t\t%llu\n"
76                "    Use%%:\t\t%llu\n",
77                dev->nr, dev->name,
78                dev->has_metadata,
79                dev->has_data,
80                dev->state,
81                dev->tier,
82                capacity,
83                dev->bytes_dirty,
84                capacity - dev->bytes_dirty,
85                (dev->bytes_dirty * 100) / capacity);
86 }
87
88 int cmd_device_show(int argc, char *argv[])
89 {
90         int human_readable = 0;
91         NihOption opts[] = {
92         //      { int shortoption, char *longoption, char *help, NihOptionGroup, char *argname, void *value, NihOptionSetter}
93
94                 { 'h',  "human-readable",               N_("print sizes in powers of 1024 (e.g., 1023M)"),
95                         NULL, NULL,     &human_readable,        NULL},
96                 NIH_OPTION_LAST
97         };
98         char **args = bch_nih_init(argc, argv, opts);
99
100         if (argc != 2)
101                 die("Please supply a single device");
102
103         struct bchfs_handle fs = bcache_fs_open(argv[1]);
104         struct dirent *entry;
105
106         struct bcache_dev devices[256];
107         unsigned i, j, nr_devices = 0, nr_active_tiers = 0;
108
109         unsigned tiers[BCH_TIER_MAX]; /* number of devices in each tier */
110         memset(tiers, 0, sizeof(tiers));
111
112         while ((entry = readdir(fs.sysfs))) {
113                 unsigned nr;
114                 int pos = 0;
115
116                 sscanf(entry->d_name, "cache%u%n", &nr, &pos);
117                 if (pos != strlen(entry->d_name))
118                         continue;
119
120                 char link[PATH_MAX];
121                 if (readlinkat(dirfd(fs.sysfs), entry->d_name,
122                                link, sizeof(link)) < 0)
123                         die("readlink error: %m\n");
124
125                 char *dev_name = basename(dirname(link));
126
127                 int fd = xopenat(dirfd(fs.sysfs), entry->d_name, O_RDONLY);
128
129                 devices[nr_devices] = fill_dev(strdup(dev_name), nr, fd);
130                 tiers[devices[nr_devices].tier]++;
131                 nr_devices++;
132
133                 close(fd);
134         }
135
136         for (i = 0; i < BCH_TIER_MAX; i++)
137                 if (tiers[i])
138                         nr_active_tiers++;
139
140         /* Print out devices sorted by tier: */
141         bool first = true;
142
143         for (i = 0; i < BCH_TIER_MAX; i++) {
144                 if (!tiers[i])
145                         continue;
146
147                 if (nr_active_tiers > 1) {
148                         if (!first)
149                                 printf("\n");
150                         first = false;
151                         printf("Tier %u:\n\n", i);
152                 }
153
154                 for (j = 0; j < nr_devices; j++) {
155                         if (devices[j].tier != i)
156                                 continue;
157
158                         if (!first)
159                                 printf("\n");
160                         first = false;
161                         show_dev(&devices[j]);
162                 }
163         }
164
165         return 0;
166 }
167 #endif
168
169 static void disk_ioctl(const char *fs, const char *dev, int cmd, int flags)
170 {
171         struct bch_ioctl_disk i = { .flags = flags, };
172
173         if (!kstrtoull(dev, 10, &i.dev))
174                 i.flags |= BCH_BY_INDEX;
175         else
176                 i.dev = (u64) dev;
177
178         xioctl(bcache_fs_open(fs).ioctl_fd, cmd, &i);
179 }
180
181 static void device_add_usage(void)
182 {
183         puts("bcachefs device add - add a device to an existing filesystem\n"
184              "Usage: bcachefs device add [OPTION]... filesystem device\n"
185              "\n"
186              "Options:\n"
187              "      --fs_size=size          Size of filesystem on device\n"
188              "      --bucket=size           Bucket size\n"
189              "      --discard               Enable discards\n"
190              "  -t, --tier=#                Higher tier (e.g. 1) indicates slower devices\n"
191              "  -f, --force                 Use device even if it appears to already be formatted\n"
192              "  -h, --help                  Display this help and exit\n"
193              "\n"
194              "Report bugs to <linux-bcache@vger.kernel.org>");
195 }
196
197 int cmd_device_add(int argc, char *argv[])
198 {
199         static const struct option longopts[] = {
200                 { "fs_size",            required_argument,      NULL, 'S' },
201                 { "bucket",             required_argument,      NULL, 'B' },
202                 { "discard",            no_argument,            NULL, 'D' },
203                 { "tier",               required_argument,      NULL, 't' },
204                 { "force",              no_argument,            NULL, 'f' },
205                 { NULL }
206         };
207         struct format_opts format_opts  = format_opts_default();
208         struct dev_opts dev_opts        = dev_opts_default();
209         bool force = false;
210         int opt;
211
212         while ((opt = getopt_long(argc, argv, "t:fh",
213                                   longopts, NULL)) != -1)
214                 switch (opt) {
215                 case 'S':
216                         if (bch2_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 bchfs_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") >> 9;
251         format_opts.btree_node_size =
252                 read_file_u64(fs.sysfs_fd, "btree_node_size") >> 9;
253
254         struct bch_sb *sb = bch2_format(format_opts, &dev_opts, 1);
255         free(sb);
256         fsync(dev_opts.fd);
257         close(dev_opts.fd);
258
259         bchu_disk_add(fs, dev_opts.path);
260         return 0;
261 }
262
263 static void device_remove_usage(void)
264 {
265         puts("bcachefs device_remove - remove a device from a filesystem\n"
266              "Usage: bcachefs device remove filesystem device\n"
267              "\n"
268              "Options:\n"
269              "  -f, --force                 Force removal, even if some data\n"
270              "                              couldn't be migrated\n"
271              "      --force-metadata        Force removal, even if some metadata\n"
272              "                              couldn't be migrated\n"
273              "  -h, --help                  display this help and exit\n"
274              "Report bugs to <linux-bcache@vger.kernel.org>");
275         exit(EXIT_SUCCESS);
276 }
277
278 int cmd_device_remove(int argc, char *argv[])
279 {
280         static const struct option longopts[] = {
281                 { "force",              0, NULL, 'f' },
282                 { "force-metadata",     0, NULL, 'F' },
283                 { "help",               0, NULL, 'h' },
284                 { NULL }
285         };
286         int opt, flags = BCH_FORCE_IF_DEGRADED;
287
288         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
289                 switch (opt) {
290                 case 'f':
291                         flags |= BCH_FORCE_IF_DATA_LOST;
292                         break;
293                 case 'F':
294                         flags |= BCH_FORCE_IF_METADATA_LOST;
295                         break;
296                 case 'h':
297                         device_remove_usage();
298                 }
299
300         if (argc - optind != 2)
301                 die("Please supply a filesystem and at least one device to remove");
302
303         disk_ioctl(argv[optind], argv[optind + 1],
304                    BCH_IOCTL_DISK_REMOVE, flags);
305         return 0;
306 }
307
308 static void device_online_usage(void)
309 {
310         puts("bcachefs device online - readd a device to a running filesystem\n"
311              "Usage: bcachefs device online [OPTION]... filesystem device\n"
312              "\n"
313              "Options:\n"
314              "  -h, --help                  Display this help and exit\n"
315              "\n"
316              "Report bugs to <linux-bcache@vger.kernel.org>");
317 }
318
319 int cmd_device_online(int argc, char *argv[])
320 {
321         int opt;
322
323         while ((opt = getopt(argc, argv, "h")) != -1)
324                 switch (opt) {
325                 case 'h':
326                         device_online_usage();
327                         exit(EXIT_SUCCESS);
328                 }
329
330         if (argc - optind != 2)
331                 die("Please supply a filesystem and a device");
332
333         disk_ioctl(argv[optind], argv[optind + 1], BCH_IOCTL_DISK_ONLINE, 0);
334         return 0;
335 }
336
337 static void device_offline_usage(void)
338 {
339         puts("bcachefs device offline - take a device offline, without removing it\n"
340              "Usage: bcachefs device offline [OPTION]... filesystem device\n"
341              "\n"
342              "Options:\n"
343              "  -f, --force                 Force, if data redundancy will be degraded\n"
344              "  -h, --help                  Display this help and exit\n"
345              "\n"
346              "Report bugs to <linux-bcache@vger.kernel.org>");
347 }
348
349 int cmd_device_offline(int argc, char *argv[])
350 {
351         static const struct option longopts[] = {
352                 { "force",              0, NULL, 'f' },
353                 { NULL }
354         };
355         int opt, flags = 0;
356
357         while ((opt = getopt_long(argc, argv, "fh",
358                                   longopts, NULL)) != -1)
359                 switch (opt) {
360                 case 'f':
361                         flags |= BCH_FORCE_IF_DEGRADED;
362                         break;
363                 case 'h':
364                         device_offline_usage();
365                         exit(EXIT_SUCCESS);
366                 }
367
368         if (argc - optind != 2)
369                 die("Please supply a filesystem and a device");
370
371         disk_ioctl(argv[optind], argv[optind + 1],
372                    BCH_IOCTL_DISK_OFFLINE, flags);
373         return 0;
374 }
375
376 static void device_evacuate_usage(void)
377 {
378         puts("bcachefs device evacuate - move data off of a given device\n"
379              "Usage: bcachefs device evacuate [OPTION]... filesystem device\n"
380              "\n"
381              "Options:\n"
382              "  -h, --help                  Display this help and exit\n"
383              "\n"
384              "Report bugs to <linux-bcache@vger.kernel.org>");
385 }
386
387 int cmd_device_evacuate(int argc, char *argv[])
388 {
389         int opt;
390
391         while ((opt = getopt(argc, argv, "h")) != -1)
392                 switch (opt) {
393                 case 'h':
394                         device_evacuate_usage();
395                         exit(EXIT_SUCCESS);
396                 }
397
398         if (argc - optind != 2)
399                 die("Please supply a filesystem and a device");
400
401         disk_ioctl(argv[optind], argv[optind + 1], BCH_IOCTL_DISK_EVACUATE, 0);
402         return 0;
403 }
404
405 static void device_set_state_usage(void)
406 {
407         puts("bcachefs device set-state\n"
408              "Usage: bcachefs device set-state filesystem device new-state\n"
409              "\n"
410              "Options:\n"
411              "  -f, --force                 Force, if data redundancy will be degraded\n"
412              "  -h, --help                  display this help and exit\n"
413              "Report bugs to <linux-bcache@vger.kernel.org>");
414         exit(EXIT_SUCCESS);
415 }
416
417 int cmd_device_set_state(int argc, char *argv[])
418 {
419         static const struct option longopts[] = {
420                 { "force",                      0, NULL, 'f' },
421                 { "help",                       0, NULL, 'h' },
422                 { NULL }
423         };
424         int opt, flags = 0;
425
426         while ((opt = getopt_long(argc, argv, "fh", longopts, NULL)) != -1)
427                 switch (opt) {
428                 case 'f':
429                         flags |= BCH_FORCE_IF_DEGRADED;
430                         break;
431                 case 'h':
432                         device_set_state_usage();
433                 }
434
435         if (argc - optind != 3)
436                 die("Please supply a filesystem, device and state");
437
438         struct bchfs_handle fs = bcache_fs_open(argv[optind]);
439
440         bchu_disk_set_state(fs, argv[optind + 1],
441                             read_string_list_or_die(argv[optind + 2],
442                                         bch2_dev_state, "device state"),
443                             flags);
444         return 0;
445 }
446
447 static void device_resize_usage(void)
448 {
449         puts("bcachefs device resize \n"
450              "Usage: bcachefs device resize device [ size ]\n"
451              "\n"
452              "Options:\n"
453              "  -h, --help                  display this help and exit\n"
454              "Report bugs to <linux-bcache@vger.kernel.org>");
455         exit(EXIT_SUCCESS);
456 }
457
458 int cmd_device_resize(int argc, char *argv[])
459 {
460         static const struct option longopts[] = {
461                 { "help",                       0, NULL, 'h' },
462                 { NULL }
463         };
464         u64 size;
465         int opt;
466
467         while ((opt = getopt_long(argc, argv, "h", longopts, NULL)) != -1)
468                 switch (opt) {
469                 case 'h':
470                         device_resize_usage();
471                 }
472
473         if (argc < optind + 1)
474                 die("Please supply a device to resize");
475
476         char *dev = argv[optind];
477         int dev_fd = xopen(dev, O_RDONLY);
478
479         if (argc == optind + 1)
480                 size = get_size(dev, dev_fd);
481         else if (bch2_strtoull_h(argv[optind + 1], &size))
482                 die("invalid size");
483
484         size >>= 9;
485
486         if (argc > optind + 2)
487                 die("Too many arguments");
488
489         struct stat dev_stat = xfstat(dev_fd);
490
491         char *mount = dev_to_mount(dev);
492         if (mount) {
493                 if (!S_ISBLK(dev_stat.st_mode))
494                         die("%s is mounted but isn't a block device?!", dev);
495
496                 printf("Doing online resize of %s\n", dev);
497
498                 struct bchfs_handle fs = bcache_fs_open(mount);
499
500                 unsigned idx = bchu_disk_get_idx(fs, dev_stat.st_rdev);
501
502                 struct bch_sb *sb = bchu_read_super(fs, -1);
503                 if (idx >= sb->nr_devices)
504                         die("error reading superblock: dev idx >= sb->nr_devices");
505
506                 struct bch_sb_field_members *mi = bch2_sb_get_members(sb);
507                 if (!mi)
508                         die("error reading superblock: no member info");
509
510                 /* could also just read this out of sysfs... meh */
511                 struct bch_member *m = mi->members + idx;
512
513                 u64 nbuckets = size / le16_to_cpu(m->bucket_size);
514
515                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
516                 bchu_disk_resize(fs, idx, nbuckets);
517         } else {
518                 printf("Doing offline resize of %s\n", dev);
519
520                 struct bch_fs *c = NULL;
521                 struct bch_opts opts = bch2_opts_empty();
522                 const char *err = bch2_fs_open(&dev, 1, opts, &c);
523                 if (err)
524                         die("error opening %s: %s", argv[optind], err);
525
526                 struct bch_dev *ca, *resize = NULL;
527                 unsigned i;
528
529                 for_each_online_member(ca, c, i) {
530                         if (resize)
531                                 die("confused: more than one online device?");
532                         resize = ca;
533                         percpu_ref_get(&resize->io_ref);
534                 }
535
536                 u64 nbuckets = size / le16_to_cpu(resize->mi.bucket_size);
537
538                 printf("resizing %s to %llu buckets\n", dev, nbuckets);
539                 int ret = bch2_dev_resize(c, resize, nbuckets);
540                 if (ret)
541                         fprintf(stderr, "resize error: %s\n", strerror(-ret));
542
543                 percpu_ref_put(&resize->io_ref);
544                 bch2_fs_stop(c);
545         }
546         return 0;
547 }