summaryrefslogtreecommitdiff
path: root/tools-util.c
blob: bd114af31b7e07ed2f1782493f020fb54f1f8fdf (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <linux/fs.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <blkid.h>
#include <uuid/uuid.h>

#include "ccan/crc/crc.h"

#include "bcachefs_ioctl.h"
#include "linux/sort.h"
#include "tools-util.h"
#include "util.h"

/* Integer stuff: */

struct units_buf __pr_units(u64 v, enum units units)
{
	struct units_buf ret;

	switch (units) {
	case BYTES:
		snprintf(ret.b, sizeof(ret.b), "%llu", v << 9);
		break;
	case SECTORS:
		snprintf(ret.b, sizeof(ret.b), "%llu", v);
		break;
	case HUMAN_READABLE:
		v <<= 9;

		if (v >= 1024) {
			int exp = log(v) / log(1024);
			snprintf(ret.b, sizeof(ret.b), "%.1f%c",
				 v / pow(1024, exp),
				 "KMGTPE"[exp-1]);
		} else {
			snprintf(ret.b, sizeof(ret.b), "%llu", v);
		}

		break;
	}

	return ret;
}

/* Argument parsing stuff: */

/* File parsing (i.e. sysfs) */

char *read_file_str(int dirfd, const char *path)
{
	int fd = xopenat(dirfd, path, O_RDONLY);
	ssize_t len = xfstat(fd).st_size;

	char *buf = xmalloc(len + 1);

	len = read(fd, buf, len);
	if (len < 0)
		die("read error: %s", strerror(errno));

	buf[len] = '\0';
	if (len && buf[len - 1] == '\n')
		buf[len - 1] = '\0';

	close(fd);

	return buf;
}

u64 read_file_u64(int dirfd, const char *path)
{
	char *buf = read_file_str(dirfd, path);
	u64 v;
	if (kstrtou64(buf, 10, &v))
		die("read_file_u64: error parsing %s (got %s)", path, buf);
	free(buf);
	return v;
}

/* String list options: */

ssize_t read_string_list_or_die(const char *opt, const char * const list[],
				const char *msg)
{
	ssize_t v = bch2_read_string_list(opt, list);
	if (v < 0)
		die("Bad %s %s", msg, opt);

	return v;
}

/* Returns size of file or block device: */
u64 get_size(const char *path, int fd)
{
	struct stat statbuf = xfstat(fd);

	if (!S_ISBLK(statbuf.st_mode))
		return statbuf.st_size;

	u64 ret;
	xioctl(fd, BLKGETSIZE64, &ret);
	return ret;
}

/* Returns blocksize in units of 512 byte sectors: */
unsigned get_blocksize(const char *path, int fd)
{
	struct stat statbuf = xfstat(fd);

	if (!S_ISBLK(statbuf.st_mode))
		return statbuf.st_blksize >> 9;

	unsigned ret;
	xioctl(fd, BLKPBSZGET, &ret);
	return ret >> 9;
}

/* Open a block device, do magic blkid stuff to probe for existing filesystems: */
int open_for_format(const char *dev, bool force)
{
	blkid_probe pr;
	const char *fs_type = NULL, *fs_label = NULL;
	size_t fs_type_len, fs_label_len;

	int fd = xopen(dev, O_RDWR|O_EXCL);

	if (force)
		return fd;

	if (!(pr = blkid_new_probe()))
		die("blkid error 1");
	if (blkid_probe_set_device(pr, fd, 0, 0))
		die("blkid error 2");
	if (blkid_probe_enable_partitions(pr, true))
		die("blkid error 3");
	if (blkid_do_fullprobe(pr) < 0)
		die("blkid error 4");

	blkid_probe_lookup_value(pr, "TYPE", &fs_type, &fs_type_len);
	blkid_probe_lookup_value(pr, "LABEL", &fs_label, &fs_label_len);

	if (fs_type) {
		if (fs_label)
			printf("%s contains a %s filesystem labelled '%s'\n",
			       dev, fs_type, fs_label);
		else
			printf("%s contains a %s filesystem\n",
			       dev, fs_type);
		fputs("Proceed anyway?", stdout);
		if (!ask_yn())
			exit(EXIT_FAILURE);
	}

	blkid_free_probe(pr);
	return fd;
}

/* Global control device: */
int bcachectl_open(void)
{
	return xopen("/dev/bcachefs-ctl", O_RDWR);
}

/* Filesystem handles (ioctl, sysfs dir): */

#define SYSFS_BASE "/sys/fs/bcachefs/"

struct bcache_handle bcache_fs_open(const char *path)
{
	struct bcache_handle ret;
	uuid_t tmp;

	if (!uuid_parse(path, tmp)) {
		/* It's a UUID, look it up in sysfs: */
		char *sysfs = mprintf("%s%s", SYSFS_BASE, path);
		ret.sysfs_fd = xopen(sysfs, O_RDONLY);

		char *minor = read_file_str(ret.sysfs_fd, "minor");
		char *ctl = mprintf("/dev/bcachefs%s-ctl", minor);
		ret.ioctl_fd = xopen(ctl, O_RDWR);

		free(sysfs);
		free(minor);
		free(ctl);
	} else {
		/* It's a path: */
		ret.ioctl_fd = xopen(path, O_RDONLY);

		struct bch_ioctl_query_uuid uuid;
		xioctl(ret.ioctl_fd, BCH_IOCTL_QUERY_UUID, &uuid);

		char uuid_str[40];
		uuid_unparse(uuid.uuid.b, uuid_str);

		char *sysfs = mprintf("%s%s", SYSFS_BASE, uuid_str);
		ret.sysfs_fd = xopen(sysfs, O_RDONLY);
		free(sysfs);
	}

	return ret;
}

bool ask_yn(void)
{
	const char *short_yes = "yY";
	char *buf = NULL;
	size_t buflen = 0;
	bool ret;

	fputs(" (y,n) ", stdout);
	fflush(stdout);

	if (getline(&buf, &buflen, stdin) < 0)
		die("error reading from standard input");

	ret = strchr(short_yes, buf[0]);
	free(buf);
	return ret;
}

static int range_cmp(const void *_l, const void *_r)
{
	const struct range *l = _l, *r = _r;

	if (l->start < r->start)
		return -1;
	if (l->start > r->start)
		return  1;
	return 0;
}

void ranges_sort_merge(ranges *r)
{
	struct range *t, *i;
	ranges tmp = { NULL };

	sort(&darray_item(*r, 0), darray_size(*r),
	     sizeof(darray_item(*r, 0)), range_cmp, NULL);

	/* Merge contiguous ranges: */
	darray_foreach(i, *r) {
		t = tmp.size ?  &tmp.item[tmp.size - 1] : NULL;

		if (t && t->end >= i->start)
			t->end = max(t->end, i->end);
		else
			darray_append(tmp, *i);
	}

	darray_free(*r);
	*r = tmp;
}

void ranges_roundup(ranges *r, unsigned block_size)
{
	struct range *i;

	darray_foreach(i, *r) {
		i->start = round_down(i->start, block_size);
		i->end	= round_up(i->end, block_size);
	}
}

void ranges_rounddown(ranges *r, unsigned block_size)
{
	struct range *i;

	darray_foreach(i, *r) {
		i->start = round_up(i->start, block_size);
		i->end	= round_down(i->end, block_size);
		i->end	= max(i->end, i->start);
	}
}

struct fiemap_extent fiemap_iter_next(struct fiemap_iter *iter)
{
	struct fiemap_extent e;

	BUG_ON(iter->idx > iter->f.fm_mapped_extents);

	if (iter->idx == iter->f.fm_mapped_extents) {
		xioctl(iter->fd, FS_IOC_FIEMAP, &iter->f);

		if (!iter->f.fm_mapped_extents)
			return (struct fiemap_extent) { .fe_length = 0 };

		iter->idx = 0;
	}

	e = iter->f.fm_extents[iter->idx++];
	BUG_ON(!e.fe_length);

	iter->f.fm_start = e.fe_logical + e.fe_length;

	return e;
}

const char *strcmp_prefix(const char *a, const char *a_prefix)
{
	while (*a_prefix && *a == *a_prefix) {
		a++;
		a_prefix++;
	}
	return *a_prefix ? NULL : a;
}

unsigned hatoi_validate(const char *s, const char *msg)
{
	u64 v;

	if (bch2_strtoull_h(s, &v))
		die("bad %s %s", msg, s);

	if (v & (v - 1))
		die("%s must be a power of two", msg);

	v /= 512;

	if (v > USHRT_MAX)
		die("%s too large\n", msg);

	if (!v)
		die("%s too small\n", msg);

	return v;
}