summaryrefslogtreecommitdiff
path: root/bcache-super-show.c
blob: fab5e810a3d68177ca4cc8a88ebaabe348bf387f (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Author: Gabriel de Perthuis <g2p.code@gmail.com>
 *
 * GPLv2
 */

#define _FILE_OFFSET_BITS	64
#define __USE_FILE_OFFSET64
#define _XOPEN_SOURCE 500

#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/fs.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <uuid/uuid.h>

#include "bcache.h"


static void usage()
{
	fprintf(stderr, "Usage: bcache-super-show [-f] <device>\n");
}


int main(int argc, char **argv)
{
	bool force_csum = false;
	int o;
	extern char *optarg;
	struct cache_sb sb;
	char uuid[40];
	uint64_t expected_csum;

	while ((o = getopt(argc, argv, "f")) != EOF)
		switch (o) {
			case 'f':
				force_csum = 1;
				break;

			default:
				usage();
				exit(1);
		}

	argv += optind;
	argc -= optind;

	if (argc != 1) {
		usage();
		exit(1);
	}

	int fd = open(argv[0], O_RDONLY);
	if (fd < 0) {
		printf("Can't open dev %s: %s\n", argv[0], strerror(errno));
		exit(2);
	}

	if (pread(fd, &sb, sizeof(sb), SB_START) != sizeof(sb)) {
		fprintf(stderr, "Couldn't read\n");
		exit(2);
	}

	printf("sb.magic\t\t");
	if (!memcmp(sb.magic, bcache_magic, 16)) {
		printf("ok\n");
	} else {
		printf("bad magic\n");
		fprintf(stderr, "Invalid superblock (bad magic)\n");
		exit(2);
	}

	printf("sb.first_sector\t\t%" PRIu64, sb.offset);
	if (sb.offset == SB_SECTOR) {
		printf(" [match]\n");
	} else {
		printf(" [expected %ds]\n", SB_SECTOR);
		fprintf(stderr, "Invalid superblock (bad sector)\n");
		exit(2);
	}

	printf("sb.csum\t\t\t%" PRIX64, sb.csum);
	expected_csum = csum_set(&sb);
	if (sb.csum == expected_csum) {
		printf(" [match]\n");
	} else {
		printf(" [expected %" PRIX64 "]\n", expected_csum);
		if (!force_csum) {
			fprintf(stderr, "Corrupt superblock (bad csum)\n");
			exit(2);
		}
	}

	printf("sb.version\t\t%" PRIu64, sb.version);
	switch (sb.version) {
		// These are handled the same by the kernel
		case BCACHE_SB_VERSION_CDEV:
		case BCACHE_SB_VERSION_CDEV_WITH_UUID:
			printf(" [cache device]\n");
			break;

		// The second adds data offset support
		case BCACHE_SB_VERSION_BDEV:
		case BCACHE_SB_VERSION_BDEV_WITH_OFFSET:
			printf(" [backing device]\n");
			break;

		default:
			printf(" [unknown]\n");
			// exit code?
			return 0;
	}

	putchar('\n');

	uuid_unparse(sb.uuid, uuid);
	printf("dev.uuid\t\t%s\n", uuid);

	printf("dev.sectors_per_block\t%u\n"
	       "dev.sectors_per_bucket\t%u\n",
	       sb.block_size,
	       sb.bucket_size);

	if (!SB_IS_BDEV(&sb)) {
		// total_sectors includes the superblock;
		printf("dev.cache.first_sector\t%u\n"
		       "dev.cache.cache_sectors\t%ju\n"
		       "dev.cache.total_sectors\t%ju\n"
		       "dev.cache.discard\t%s\n"
		       "dev.cache.pos\t\t%u\n",
		       sb.bucket_size * sb.first_bucket,
		       sb.bucket_size * (sb.nbuckets - sb.first_bucket),
		       sb.bucket_size * sb.nbuckets,
		       CACHE_DISCARD(&sb) ? "yes" : "no",
		       sb.nr_this_dev);
	} else {
		uint64_t first_sector;
		if (sb.version == BCACHE_SB_VERSION_BDEV) {
			first_sector = BDEV_DATA_START_DEFAULT;
		} else {
			if (sb.keys == 1 || sb.d[0]) {
				fprintf(stderr,
					"Possible experimental format detected, bailing\n");
				exit(3);
			}
			first_sector = sb.data_offset;
		}

		printf("dev.data.first_sector\t%ju\n"
		       "dev.data.cache_mode\t%ju",
		       first_sector,
		       BDEV_CACHE_MODE(&sb));
		switch (BDEV_CACHE_MODE(&sb)) {
			case CACHE_MODE_WRITETHROUGH:
				printf(" [writethrough]\n");
				break;
			case CACHE_MODE_WRITEBACK:
				printf(" [writeback]\n");
				break;
			case CACHE_MODE_WRITEAROUND:
				printf(" [writearound]\n");
				break;
			case CACHE_MODE_NONE:
				printf(" [no caching]\n");
				break;
			default:
				putchar('\n');
		}

		printf("dev.data.cache_state\t%ju",
		       BDEV_STATE(&sb));
		switch (BDEV_STATE(&sb)) {
			case BDEV_STATE_NONE:
				printf(" [detached]\n");
				break;
			case BDEV_STATE_CLEAN:
				printf(" [clean]\n");
				break;
			case BDEV_STATE_DIRTY:
				printf(" [dirty]\n");
				break;
			case BDEV_STATE_STALE:
				printf(" [inconsistent]\n");
				break;
			default:
				putchar('\n');
		}
	}
	putchar('\n');

	uuid_unparse(sb.set_uuid, uuid);
	printf("cset.uuid\t\t%s\n", uuid);

	return 0;
}