summaryrefslogtreecommitdiff
path: root/cmd_fs.c
blob: 967ac7a7f1c296bd16305ec1b65e082385c0b9d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

#include <stdio.h>
#include <sys/ioctl.h>

#include <uuid/uuid.h>

#include "libbcachefs/bcachefs_ioctl.h"
#include "libbcachefs/opts.h"

#include "cmds.h"
#include "libbcachefs.h"

static void print_fs_usage(const char *path, enum units units)
{
	unsigned i, j;
	char uuid[40];

	struct bchfs_handle fs = bcache_fs_open(path);
	struct bch_ioctl_usage *u = bchu_usage(fs);

	uuid_unparse(fs.uuid.b, uuid);
	printf("Filesystem %s:\n", uuid);

	printf("%-20s%12s\n", "Size:", pr_units(u->fs.capacity, units));
	printf("%-20s%12s\n", "Used:", pr_units(u->fs.used, units));

	printf("%-20s%12s%12s%12s%12s\n",
	       "By replicas:", "1x", "2x", "3x", "4x");

	for (j = BCH_DATA_BTREE; j < BCH_DATA_NR; j++) {
		printf_pad(20, "  %s:", bch2_data_types[j]);

		for (i = 0; i < BCH_REPLICAS_MAX; i++)
			printf("%12s", pr_units(u->fs.sectors[j][i], units));
		printf("\n");
	}

	printf_pad(20, "  %s:", "reserved");
	for (i = 0; i < BCH_REPLICAS_MAX; i++)
		printf("%12s", pr_units(u->fs.persistent_reserved[i], units));
	printf("\n");

	printf("%-20s%12s\n", "  online reserved:", pr_units(u->fs.online_reserved, units));

	for (i = 0; i < u->nr_devices; i++) {
		struct bch_ioctl_dev_usage *d = u->devs + i;
		char *name = NULL;

		if (!d->alive)
			continue;

		printf("\n");
		printf_pad(20, "Device %u usage:", i);
		name = !d->dev ? strdup("(offline)")
			: dev_to_path(d->dev)
			?: strdup("(device not found)");

		printf("%24s%12s\n", name, bch2_dev_state[d->state]);
		free(name);

		printf("%-20s%12s%12s%12s\n",
		       "", "data", "buckets", "fragmented");

		for (j = BCH_DATA_SB; j < BCH_DATA_NR; j++) {
			u64 frag = max((s64) d->buckets[j] * d->bucket_size -
				       (s64) d->sectors[j], 0LL);

			printf_pad(20, "  %s:", bch2_data_types[j]);
			printf("%12s%12llu%12s\n",
			       pr_units(d->sectors[j], units),
			       d->buckets[j],
			       pr_units(frag, units));
		}
	}

	free(u);
	bcache_fs_close(fs);
}

int cmd_fs_usage(int argc, char *argv[])
{
	enum units units = BYTES;
	char *fs;
	int opt;

	while ((opt = getopt(argc, argv, "h")) != -1)
		switch (opt) {
		case 'h':
			units = HUMAN_READABLE;
			break;
		}
	args_shift(optind);

	if (!argc) {
		print_fs_usage(".", units);
	} else {
		while ((fs = arg_pop()))
			print_fs_usage(fs, units);
	}

	return 0;
}