]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_fs.c
Resizing
[bcachefs-tools-debian] / cmd_fs.c
1
2 #include <stdio.h>
3 #include <sys/ioctl.h>
4
5 #include <uuid/uuid.h>
6
7 #include "libbcachefs/bcachefs_ioctl.h"
8 #include "libbcachefs/opts.h"
9
10 #include "cmds.h"
11 #include "libbcachefs.h"
12
13 static inline int printf_pad(unsigned pad, const char * fmt, ...)
14 {
15        va_list args;
16        int ret;
17
18        va_start(args, fmt);
19        ret = vprintf(fmt, args);
20        va_end(args);
21
22        while (ret++ < pad)
23                putchar(' ');
24
25        return ret;
26 }
27
28 static void print_fs_usage(const char *path, enum units units)
29 {
30         unsigned i, j;
31         char uuid[40];
32
33         struct bchfs_handle fs = bcache_fs_open(path);
34         struct bch_ioctl_usage *u = bchu_usage(fs);
35
36         uuid_unparse(fs.uuid.b, uuid);
37         printf("Filesystem %s:\n", uuid);
38
39         printf("%-20s%12s\n", "Size:", pr_units(u->fs.capacity, units));
40         printf("%-20s%12s\n", "Used:", pr_units(u->fs.used, units));
41
42         printf("%-20s%12s%12s%12s%12s\n",
43                "By replicas:", "1x", "2x", "3x", "4x");
44
45         for (j = BCH_DATA_BTREE; j < BCH_DATA_NR; j++) {
46                 printf_pad(20, "  %s:", bch2_data_types[j]);
47
48                 for (i = 0; i < BCH_REPLICAS_MAX; i++)
49                         printf("%12s", pr_units(u->fs.sectors[j][i], units));
50                 printf("\n");
51         }
52
53         printf_pad(20, "  %s:", "reserved");
54         for (i = 0; i < BCH_REPLICAS_MAX; i++)
55                 printf("%12s", pr_units(u->fs.persistent_reserved[i], units));
56         printf("\n");
57
58         printf("%-20s%12s\n", "  online reserved:", pr_units(u->fs.online_reserved, units));
59
60         for (i = 0; i < u->nr_devices; i++) {
61                 struct bch_ioctl_dev_usage *d = u->devs + i;
62                 char *name = NULL;
63
64                 if (!d->alive)
65                         continue;
66
67                 printf("\n");
68                 printf_pad(20, "Device %u usage:", i);
69                 name = !d->dev ? strdup("(offline)")
70                         : dev_to_path(d->dev)
71                         ?: strdup("(device not found)");
72
73                 printf("%24s%12s\n", name, bch2_dev_state[d->state]);
74                 free(name);
75
76                 printf("%-20s%12s%12s%12s\n",
77                        "", "data", "buckets", "fragmented");
78
79                 for (j = BCH_DATA_SB; j < BCH_DATA_NR; j++) {
80                         u64 frag = max((s64) d->buckets[j] * d->bucket_size -
81                                        (s64) d->sectors[j], 0LL);
82
83                         printf_pad(20, "  %s:", bch2_data_types[j]);
84                         printf("%12s%12llu%12s\n",
85                                pr_units(d->sectors[j], units),
86                                d->buckets[j],
87                                pr_units(frag, units));
88                 }
89         }
90
91         free(u);
92         bcache_fs_close(fs);
93 }
94
95 int cmd_fs_usage(int argc, char *argv[])
96 {
97         enum units units = BYTES;
98         unsigned i;
99         int opt;
100
101         while ((opt = getopt(argc, argv, "h")) != -1)
102                 switch (opt) {
103                 case 'h':
104                         units = HUMAN_READABLE;
105                         break;
106                 }
107
108         if (argc - optind < 1) {
109                 print_fs_usage(".", units);
110         } else {
111                 for (i = optind; i < argc; i++)
112                         print_fs_usage(argv[i], units);
113         }
114
115         return 0;
116 }