chardev.c 24.9 KB
Newer Older
1 2 3 4 5 6 7
// SPDX-License-Identifier: GPL-2.0
#ifndef NO_BCACHEFS_CHARDEV

#include "bcachefs.h"
#include "bcachefs_ioctl.h"
#include "buckets.h"
#include "chardev.h"
8
#include "journal.h"
9
#include "move.h"
10
#include "recovery.h"
11
#include "replicas.h"
12 13 14 15 16 17 18 19 20 21 22
#include "super.h"
#include "super-io.h"

#include <linux/anon_inodes.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/ioctl.h>
#include <linux/kthread.h>
#include <linux/major.h>
23
#include <linux/poll.h>
24 25 26 27
#include <linux/sched/task.h>
#include <linux/slab.h>
#include <linux/uaccess.h>

28 29 30 31 32 33
__must_check
static int copy_to_user_errcode(void __user *to, const void *from, unsigned long n)
{
	return copy_to_user(to, from, n) ? -EFAULT : 0;
}

34 35 36
struct thread_with_file {
	struct task_struct	*task;
	int			ret;
37
	bool			done;
38 39 40 41
};

static void thread_with_file_exit(struct thread_with_file *thr)
{
42 43 44 45
	if (thr->task) {
		kthread_stop(thr->task);
		put_task_struct(thr->task);
	}
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
}

__printf(4, 0)
static int run_thread_with_file(struct thread_with_file *thr,
				const struct file_operations *fops,
				int (*fn)(void *), const char *fmt, ...)
{
	va_list args;
	struct file *file = NULL;
	int ret, fd = -1;
	struct printbuf name = PRINTBUF;
	unsigned fd_flags = O_RDONLY|O_CLOEXEC|O_NONBLOCK;

	va_start(args, fmt);
	prt_vprintf(&name, fmt, args);
	va_end(args);

	thr->ret = 0;
	thr->task = kthread_create(fn, thr, name.buf);
	ret = PTR_ERR_OR_ZERO(thr->task);
	if (ret)
		goto err;

	ret = get_unused_fd_flags(fd_flags);
	if (ret < 0)
		goto err_stop_task;
	fd = ret;

	file = anon_inode_getfile(name.buf, fops, thr, fd_flags);
	ret = PTR_ERR_OR_ZERO(file);
	if (ret)
		goto err_put_fd;

	fd_install(fd, file);
	get_task_struct(thr->task);
	wake_up_process(thr->task);
	printbuf_exit(&name);
	return fd;
err_put_fd:
	put_unused_fd(fd);
err_stop_task:
	kthread_stop(thr->task);
err:
	printbuf_exit(&name);
	return ret;
}

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
/* returns with ref on ca->ref */
static struct bch_dev *bch2_device_lookup(struct bch_fs *c, u64 dev,
					  unsigned flags)
{
	struct bch_dev *ca;

	if (flags & BCH_BY_INDEX) {
		if (dev >= c->sb.nr_devices)
			return ERR_PTR(-EINVAL);

		rcu_read_lock();
		ca = rcu_dereference(c->devs[dev]);
		if (ca)
			percpu_ref_get(&ca->ref);
		rcu_read_unlock();

		if (!ca)
			return ERR_PTR(-EINVAL);
	} else {
		char *path;

		path = strndup_user((const char __user *)
				    (unsigned long) dev, PATH_MAX);
		if (IS_ERR(path))
			return ERR_CAST(path);

		ca = bch2_dev_lookup(c, path);
		kfree(path);
	}

	return ca;
}

#if 0
static long bch2_ioctl_assemble(struct bch_ioctl_assemble __user *user_arg)
{
	struct bch_ioctl_assemble arg;
	struct bch_fs *c;
	u64 *user_devs = NULL;
	char **devs = NULL;
	unsigned i;
	int ret = -EFAULT;

	if (copy_from_user(&arg, user_arg, sizeof(arg)))
		return -EFAULT;

	if (arg.flags || arg.pad)
		return -EINVAL;

	user_devs = kmalloc_array(arg.nr_devs, sizeof(u64), GFP_KERNEL);
	if (!user_devs)
		return -ENOMEM;

	devs = kcalloc(arg.nr_devs, sizeof(char *), GFP_KERNEL);

	if (copy_from_user(user_devs, user_arg->devs,
			   sizeof(u64) * arg.nr_devs))
		goto err;

	for (i = 0; i < arg.nr_devs; i++) {
		devs[i] = strndup_user((const char __user *)(unsigned long)
				       user_devs[i],
				       PATH_MAX);
156 157
		ret= PTR_ERR_OR_ZERO(devs[i]);
		if (ret)
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
			goto err;
	}

	c = bch2_fs_open(devs, arg.nr_devs, bch2_opts_empty());
	ret = PTR_ERR_OR_ZERO(c);
	if (!ret)
		closure_put(&c->cl);
err:
	if (devs)
		for (i = 0; i < arg.nr_devs; i++)
			kfree(devs[i]);
	kfree(devs);
	return ret;
}

static long bch2_ioctl_incremental(struct bch_ioctl_incremental __user *user_arg)
{
	struct bch_ioctl_incremental arg;
	const char *err;
	char *path;

	if (copy_from_user(&arg, user_arg, sizeof(arg)))
		return -EFAULT;

	if (arg.flags || arg.pad)
		return -EINVAL;

	path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
186 187 188
	ret = PTR_ERR_OR_ZERO(path);
	if (ret)
		return ret;
189 190 191 192 193 194 195 196 197 198 199 200 201

	err = bch2_fs_open_incremental(path);
	kfree(path);

	if (err) {
		pr_err("Could not register bcachefs devices: %s", err);
		return -EINVAL;
	}

	return 0;
}
#endif

202 203 204
struct fsck_thread {
	struct thread_with_file	thr;
	struct printbuf		buf;
205
	struct bch_fs		*c;
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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	char			**devs;
	size_t			nr_devs;
	struct bch_opts		opts;

	struct log_output	output;
	DARRAY(char)		output2;
};

static void bch2_fsck_thread_free(struct fsck_thread *thr)
{
	thread_with_file_exit(&thr->thr);
	if (thr->devs)
		for (size_t i = 0; i < thr->nr_devs; i++)
			kfree(thr->devs[i]);
	darray_exit(&thr->output2);
	printbuf_exit(&thr->output.buf);
	kfree(thr->devs);
	kfree(thr);
}

static int bch2_fsck_thread_release(struct inode *inode, struct file *file)
{
	struct fsck_thread *thr = container_of(file->private_data, struct fsck_thread, thr);

	bch2_fsck_thread_free(thr);
	return 0;
}

static bool fsck_thread_ready(struct fsck_thread *thr)
{
	return thr->output.buf.pos ||
		thr->output2.nr ||
		thr->thr.done;
}

static ssize_t bch2_fsck_thread_read(struct file *file, char __user *buf,
				     size_t len, loff_t *ppos)
{
	struct fsck_thread *thr = container_of(file->private_data, struct fsck_thread, thr);
	size_t copied = 0, b;
	int ret = 0;

	if ((file->f_flags & O_NONBLOCK) &&
	    !fsck_thread_ready(thr))
		return -EAGAIN;

	ret = wait_event_interruptible(thr->output.wait,
			fsck_thread_ready(thr));
	if (ret)
		return ret;

	if (thr->thr.done)
		return 0;

	while (len) {
		ret = darray_make_room(&thr->output2, thr->output.buf.pos);
		if (ret)
			break;

		spin_lock_irq(&thr->output.lock);
		b = min_t(size_t, darray_room(thr->output2), thr->output.buf.pos);

		memcpy(&darray_top(thr->output2), thr->output.buf.buf, b);
		memmove(thr->output.buf.buf,
			thr->output.buf.buf + b,
			thr->output.buf.pos - b);

		thr->output2.nr += b;
		thr->output.buf.pos -= b;
		spin_unlock_irq(&thr->output.lock);

		b = min(len, thr->output2.nr);
		if (!b)
			break;

		b -= copy_to_user(buf, thr->output2.data, b);
		if (!b) {
			ret = -EFAULT;
			break;
		}

		copied	+= b;
		buf	+= b;
		len	-= b;

		memmove(thr->output2.data,
			thr->output2.data + b,
			thr->output2.nr - b);
		thr->output2.nr -= b;
	}

	return copied ?: ret;
}

static __poll_t bch2_fsck_thread_poll(struct file *file, struct poll_table_struct *wait)
{
	struct fsck_thread *thr = container_of(file->private_data, struct fsck_thread, thr);

	poll_wait(file, &thr->output.wait, wait);

	return fsck_thread_ready(thr)
		? EPOLLIN|EPOLLHUP
		: 0;
}

static const struct file_operations fsck_thread_ops = {
	.release	= bch2_fsck_thread_release,
	.read		= bch2_fsck_thread_read,
	.poll		= bch2_fsck_thread_poll,
	.llseek		= no_llseek,
};

static int bch2_fsck_offline_thread_fn(void *arg)
{
	struct fsck_thread *thr = container_of(arg, struct fsck_thread, thr);
	struct bch_fs *c = bch2_fs_open(thr->devs, thr->nr_devs, thr->opts);

	thr->thr.ret = PTR_ERR_OR_ZERO(c);
	if (!thr->thr.ret)
		bch2_fs_stop(c);

	thr->thr.done = true;
	wake_up(&thr->output.wait);
	return 0;
}

static long bch2_ioctl_fsck_offline(struct bch_ioctl_fsck_offline __user *user_arg)
{
	struct bch_ioctl_fsck_offline arg;
	struct fsck_thread *thr = NULL;
	u64 *devs = NULL;
	long ret = 0;

	if (copy_from_user(&arg, user_arg, sizeof(arg)))
		return -EFAULT;

	if (arg.flags)
		return -EINVAL;

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	if (!(devs = kcalloc(arg.nr_devs, sizeof(*devs), GFP_KERNEL)) ||
	    !(thr = kzalloc(sizeof(*thr), GFP_KERNEL)) ||
	    !(thr->devs = kcalloc(arg.nr_devs, sizeof(*thr->devs), GFP_KERNEL))) {
		ret = -ENOMEM;
		goto err;
	}

355
	thr->opts = bch2_opts_empty();
356 357 358 359 360 361 362
	thr->nr_devs = arg.nr_devs;
	thr->output.buf	= PRINTBUF;
	thr->output.buf.atomic++;
	spin_lock_init(&thr->output.lock);
	init_waitqueue_head(&thr->output.wait);
	darray_init(&thr->output2);

363 364
	if (copy_from_user(devs, &user_arg->devs[0],
			   array_size(sizeof(user_arg->devs[0]), arg.nr_devs))) {
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
		ret = -EINVAL;
		goto err;
	}

	for (size_t i = 0; i < arg.nr_devs; i++) {
		thr->devs[i] = strndup_user((char __user *)(unsigned long) devs[i], PATH_MAX);
		ret = PTR_ERR_OR_ZERO(thr->devs[i]);
		if (ret)
			goto err;
	}

	if (arg.opts) {
		char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);

		ret =   PTR_ERR_OR_ZERO(optstr) ?:
			bch2_parse_mount_opts(NULL, &thr->opts, optstr);
		kfree(optstr);

		if (ret)
			goto err;
	}

	opt_set(thr->opts, log_output, (u64)(unsigned long)&thr->output);

	ret = run_thread_with_file(&thr->thr,
				   &fsck_thread_ops,
				   bch2_fsck_offline_thread_fn,
				   "bch-fsck");
err:
	if (ret < 0) {
		if (thr)
			bch2_fsck_thread_free(thr);
		pr_err("ret %s", bch2_err_str(ret));
	}
	kfree(devs);
	return ret;
}

403 404
static long bch2_global_ioctl(unsigned cmd, void __user *arg)
{
405 406
	long ret;

407 408 409 410 411 412 413
	switch (cmd) {
#if 0
	case BCH_IOCTL_ASSEMBLE:
		return bch2_ioctl_assemble(arg);
	case BCH_IOCTL_INCREMENTAL:
		return bch2_ioctl_incremental(arg);
#endif
414 415 416 417
	case BCH_IOCTL_FSCK_OFFLINE: {
		ret = bch2_ioctl_fsck_offline(arg);
		break;
	}
418
	default:
419 420
		ret = -ENOTTY;
		break;
421
	}
422 423 424 425

	if (ret < 0)
		ret = bch2_err_class(ret);
	return ret;
426 427 428 429 430
}

static long bch2_ioctl_query_uuid(struct bch_fs *c,
			struct bch_ioctl_query_uuid __user *user_arg)
{
431 432
	return copy_to_user_errcode(&user_arg->uuid, &c->sb.user_uuid,
				    sizeof(c->sb.user_uuid));
433 434 435 436 437
}

#if 0
static long bch2_ioctl_start(struct bch_fs *c, struct bch_ioctl_start arg)
{
438 439 440
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

441 442 443
	if (arg.flags || arg.pad)
		return -EINVAL;

444
	return bch2_fs_start(c);
445 446 447 448
}

static long bch2_ioctl_stop(struct bch_fs *c)
{
449 450 451
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

452 453 454 455 456 457 458 459 460 461
	bch2_fs_stop(c);
	return 0;
}
#endif

static long bch2_ioctl_disk_add(struct bch_fs *c, struct bch_ioctl_disk arg)
{
	char *path;
	int ret;

462 463 464
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

465 466 467 468
	if (arg.flags || arg.pad)
		return -EINVAL;

	path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
469 470 471
	ret = PTR_ERR_OR_ZERO(path);
	if (ret)
		return ret;
472 473 474 475 476 477 478 479 480 481 482

	ret = bch2_dev_add(c, path);
	kfree(path);

	return ret;
}

static long bch2_ioctl_disk_remove(struct bch_fs *c, struct bch_ioctl_disk arg)
{
	struct bch_dev *ca;

483 484 485
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
	if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
			   BCH_FORCE_IF_METADATA_LOST|
			   BCH_FORCE_IF_DEGRADED|
			   BCH_BY_INDEX)) ||
	    arg.pad)
		return -EINVAL;

	ca = bch2_device_lookup(c, arg.dev, arg.flags);
	if (IS_ERR(ca))
		return PTR_ERR(ca);

	return bch2_dev_remove(c, ca, arg.flags);
}

static long bch2_ioctl_disk_online(struct bch_fs *c, struct bch_ioctl_disk arg)
{
	char *path;
	int ret;

505 506 507
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

508 509 510 511
	if (arg.flags || arg.pad)
		return -EINVAL;

	path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
512 513 514
	ret = PTR_ERR_OR_ZERO(path);
	if (ret)
		return ret;
515 516 517 518 519 520 521 522 523 524 525

	ret = bch2_dev_online(c, path);
	kfree(path);
	return ret;
}

static long bch2_ioctl_disk_offline(struct bch_fs *c, struct bch_ioctl_disk arg)
{
	struct bch_dev *ca;
	int ret;

526 527 528
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
	if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
			   BCH_FORCE_IF_METADATA_LOST|
			   BCH_FORCE_IF_DEGRADED|
			   BCH_BY_INDEX)) ||
	    arg.pad)
		return -EINVAL;

	ca = bch2_device_lookup(c, arg.dev, arg.flags);
	if (IS_ERR(ca))
		return PTR_ERR(ca);

	ret = bch2_dev_offline(c, ca, arg.flags);
	percpu_ref_put(&ca->ref);
	return ret;
}

static long bch2_ioctl_disk_set_state(struct bch_fs *c,
			struct bch_ioctl_disk_set_state arg)
{
	struct bch_dev *ca;
	int ret;

551 552 553
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

554 555 556 557
	if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
			   BCH_FORCE_IF_METADATA_LOST|
			   BCH_FORCE_IF_DEGRADED|
			   BCH_BY_INDEX)) ||
558 559
	    arg.pad[0] || arg.pad[1] || arg.pad[2] ||
	    arg.new_state >= BCH_MEMBER_STATE_NR)
560 561 562 563 564 565 566
		return -EINVAL;

	ca = bch2_device_lookup(c, arg.dev, arg.flags);
	if (IS_ERR(ca))
		return PTR_ERR(ca);

	ret = bch2_dev_set_state(c, ca, arg.new_state, arg.flags);
567 568
	if (ret)
		bch_err(c, "Error setting device state: %s", bch2_err_str(ret));
569 570 571 572 573 574

	percpu_ref_put(&ca->ref);
	return ret;
}

struct bch_data_ctx {
575 576
	struct thread_with_file		thr;

577 578 579 580 581 582 583
	struct bch_fs			*c;
	struct bch_ioctl_data		arg;
	struct bch_move_stats		stats;
};

static int bch2_data_thread(void *arg)
{
584
	struct bch_data_ctx *ctx = container_of(arg, struct bch_data_ctx, thr);
585

586
	ctx->thr.ret = bch2_data_job(ctx->c, &ctx->stats, ctx->arg);
587 588 589 590 591 592
	ctx->stats.data_type = U8_MAX;
	return 0;
}

static int bch2_data_job_release(struct inode *inode, struct file *file)
{
593
	struct bch_data_ctx *ctx = container_of(file->private_data, struct bch_data_ctx, thr);
594

595
	thread_with_file_exit(&ctx->thr);
596 597 598 599 600 601 602
	kfree(ctx);
	return 0;
}

static ssize_t bch2_data_job_read(struct file *file, char __user *buf,
				  size_t len, loff_t *ppos)
{
603
	struct bch_data_ctx *ctx = container_of(file->private_data, struct bch_data_ctx, thr);
604 605 606 607
	struct bch_fs *c = ctx->c;
	struct bch_ioctl_data_event e = {
		.type			= BCH_DATA_EVENT_PROGRESS,
		.p.data_type		= ctx->stats.data_type,
608 609
		.p.btree_id		= ctx->stats.pos.btree,
		.p.pos			= ctx->stats.pos.pos,
610
		.p.sectors_done		= atomic64_read(&ctx->stats.sectors_seen),
611
		.p.sectors_total	= bch2_fs_usage_read_short(c).used,
612 613 614 615 616
	};

	if (len < sizeof(e))
		return -EINVAL;

617
	return copy_to_user_errcode(buf, &e, sizeof(e)) ?: sizeof(e);
618 619 620 621 622 623 624 625 626 627 628
}

static const struct file_operations bcachefs_data_ops = {
	.release	= bch2_data_job_release,
	.read		= bch2_data_job_read,
	.llseek		= no_llseek,
};

static long bch2_ioctl_data(struct bch_fs *c,
			    struct bch_ioctl_data arg)
{
629 630
	struct bch_data_ctx *ctx;
	int ret;
631

632 633 634
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

635 636 637 638 639 640 641 642 643 644
	if (arg.op >= BCH_DATA_OP_NR || arg.flags)
		return -EINVAL;

	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
	if (!ctx)
		return -ENOMEM;

	ctx->c = c;
	ctx->arg = arg;

645 646 647 648
	ret = run_thread_with_file(&ctx->thr,
				   &bcachefs_data_ops,
				   bch2_data_thread,
				   "bch-data/%s", c->name);
649
	if (ret < 0)
650
		kfree(ctx);
651 652 653
	return ret;
}

654 655
static long bch2_ioctl_fs_usage(struct bch_fs *c,
				struct bch_ioctl_fs_usage __user *user_arg)
656
{
657 658 659 660 661 662
	struct bch_ioctl_fs_usage *arg = NULL;
	struct bch_replicas_usage *dst_e, *dst_end;
	struct bch_fs_usage_online *src;
	u32 replica_entries_bytes;
	unsigned i;
	int ret = 0;
663

664
	if (!test_bit(BCH_FS_started, &c->flags))
665 666
		return -EINVAL;

667
	if (get_user(replica_entries_bytes, &user_arg->replica_entries_bytes))
668 669
		return -EFAULT;

670
	arg = kzalloc(size_add(sizeof(*arg), replica_entries_bytes), GFP_KERNEL);
671 672
	if (!arg)
		return -ENOMEM;
673

674 675 676 677
	src = bch2_fs_usage_read(c);
	if (!src) {
		ret = -ENOMEM;
		goto err;
678 679
	}

680 681 682
	arg->capacity		= c->capacity;
	arg->used		= bch2_fs_sectors_used(c, src);
	arg->online_reserved	= src->online_reserved;
683

684 685
	for (i = 0; i < BCH_REPLICAS_MAX; i++)
		arg->persistent_reserved[i] = src->u.persistent_reserved[i];
686

687 688
	dst_e	= arg->replicas;
	dst_end = (void *) arg->replicas + replica_entries_bytes;
689

690
	for (i = 0; i < c->replicas.nr; i++) {
691
		struct bch_replicas_entry_v1 *src_e =
692
			cpu_replicas_entry(&c->replicas, i);
693

694 695
		/* check that we have enough space for one replicas entry */
		if (dst_e + 1 > dst_end) {
696 697
			ret = -ERANGE;
			break;
698 699
		}

700 701 702 703 704 705 706 707
		dst_e->sectors		= src->u.replicas[i];
		dst_e->r		= *src_e;

		/* recheck after setting nr_devs: */
		if (replicas_usage_next(dst_e) > dst_end) {
			ret = -ERANGE;
			break;
		}
708

709 710 711
		memcpy(dst_e->r.devs, src_e->devs, src_e->nr_devs);

		dst_e = replicas_usage_next(dst_e);
712 713
	}

714
	arg->replica_entries_bytes = (void *) dst_e - (void *) arg->replicas;
715

716 717
	percpu_up_read(&c->mark_lock);
	kfree(src);
718

719 720
	if (ret)
		goto err;
721 722 723

	ret = copy_to_user_errcode(user_arg, arg,
			sizeof(*arg) + arg->replica_entries_bytes);
724 725 726 727 728
err:
	kfree(arg);
	return ret;
}

729
/* obsolete, didn't allow for new data types: */
730 731 732 733 734 735 736 737
static long bch2_ioctl_dev_usage(struct bch_fs *c,
				 struct bch_ioctl_dev_usage __user *user_arg)
{
	struct bch_ioctl_dev_usage arg;
	struct bch_dev_usage src;
	struct bch_dev *ca;
	unsigned i;

738
	if (!test_bit(BCH_FS_started, &c->flags))
739
		return -EINVAL;
740

741 742 743 744 745 746 747 748 749 750 751 752 753
	if (copy_from_user(&arg, user_arg, sizeof(arg)))
		return -EFAULT;

	if ((arg.flags & ~BCH_BY_INDEX) ||
	    arg.pad[0] ||
	    arg.pad[1] ||
	    arg.pad[2])
		return -EINVAL;

	ca = bch2_device_lookup(c, arg.dev, arg.flags);
	if (IS_ERR(ca))
		return PTR_ERR(ca);

754
	src = bch2_dev_usage_read(ca);
755

756 757 758
	arg.state		= ca->mi.state;
	arg.bucket_size		= ca->mi.bucket_size;
	arg.nr_buckets		= ca->mi.nbuckets - ca->mi.first_bucket;
759 760

	for (i = 0; i < BCH_DATA_NR; i++) {
761 762 763
		arg.d[i].buckets	= src.d[i].buckets;
		arg.d[i].sectors	= src.d[i].sectors;
		arg.d[i].fragmented	= src.d[i].fragmented;
764 765
	}

766 767
	percpu_ref_put(&ca->ref);

768 769 770 771 772 773 774 775 776 777 778
	return copy_to_user_errcode(user_arg, &arg, sizeof(arg));
}

static long bch2_ioctl_dev_usage_v2(struct bch_fs *c,
				 struct bch_ioctl_dev_usage_v2 __user *user_arg)
{
	struct bch_ioctl_dev_usage_v2 arg;
	struct bch_dev_usage src;
	struct bch_dev *ca;
	int ret = 0;

779
	if (!test_bit(BCH_FS_started, &c->flags))
780 781 782
		return -EINVAL;

	if (copy_from_user(&arg, user_arg, sizeof(arg)))
783 784
		return -EFAULT;

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
	if ((arg.flags & ~BCH_BY_INDEX) ||
	    arg.pad[0] ||
	    arg.pad[1] ||
	    arg.pad[2])
		return -EINVAL;

	ca = bch2_device_lookup(c, arg.dev, arg.flags);
	if (IS_ERR(ca))
		return PTR_ERR(ca);

	src = bch2_dev_usage_read(ca);

	arg.state		= ca->mi.state;
	arg.bucket_size		= ca->mi.bucket_size;
	arg.nr_data_types	= min(arg.nr_data_types, BCH_DATA_NR);
	arg.nr_buckets		= ca->mi.nbuckets - ca->mi.first_bucket;

	ret = copy_to_user_errcode(user_arg, &arg, sizeof(arg));
	if (ret)
		goto err;

	for (unsigned i = 0; i < arg.nr_data_types; i++) {
		struct bch_ioctl_dev_usage_type t = {
			.buckets	= src.d[i].buckets,
			.sectors	= src.d[i].sectors,
			.fragmented	= src.d[i].fragmented,
		};

		ret = copy_to_user_errcode(&user_arg->d[i], &t, sizeof(t));
		if (ret)
			goto err;
	}
err:
	percpu_ref_put(&ca->ref);
	return ret;
820 821 822 823 824 825 826 827 828
}

static long bch2_ioctl_read_super(struct bch_fs *c,
				  struct bch_ioctl_read_super arg)
{
	struct bch_dev *ca = NULL;
	struct bch_sb *sb;
	int ret = 0;

829 830 831
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
	if ((arg.flags & ~(BCH_BY_INDEX|BCH_READ_DEV)) ||
	    arg.pad)
		return -EINVAL;

	mutex_lock(&c->sb_lock);

	if (arg.flags & BCH_READ_DEV) {
		ca = bch2_device_lookup(c, arg.dev, arg.flags);

		if (IS_ERR(ca)) {
			ret = PTR_ERR(ca);
			goto err;
		}

		sb = ca->disk_sb.sb;
	} else {
		sb = c->disk_sb.sb;
	}

	if (vstruct_bytes(sb) > arg.size) {
		ret = -ERANGE;
		goto err;
	}

856 857
	ret = copy_to_user_errcode((void __user *)(unsigned long)arg.sb, sb,
				   vstruct_bytes(sb));
858
err:
859
	if (!IS_ERR_OR_NULL(ca))
860 861 862 863 864 865 866 867 868 869
		percpu_ref_put(&ca->ref);
	mutex_unlock(&c->sb_lock);
	return ret;
}

static long bch2_ioctl_disk_get_idx(struct bch_fs *c,
				    struct bch_ioctl_disk_get_idx arg)
{
	dev_t dev = huge_decode_dev(arg.dev);

870 871 872
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

Kent Overstreet's avatar
Kent Overstreet committed
873 874 875
	if (!dev)
		return -EINVAL;

876
	for_each_online_member(c, ca)
Kent Overstreet's avatar
Kent Overstreet committed
877
		if (ca->dev == dev) {
878
			percpu_ref_put(&ca->io_ref);
879
			return ca->dev_idx;
880 881
		}

882
	return -BCH_ERR_ENOENT_dev_idx_not_found;
883 884 885 886 887 888 889 890
}

static long bch2_ioctl_disk_resize(struct bch_fs *c,
				   struct bch_ioctl_disk_resize arg)
{
	struct bch_dev *ca;
	int ret;

891 892 893
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

894 895 896 897 898 899 900 901 902 903 904 905 906 907
	if ((arg.flags & ~BCH_BY_INDEX) ||
	    arg.pad)
		return -EINVAL;

	ca = bch2_device_lookup(c, arg.dev, arg.flags);
	if (IS_ERR(ca))
		return PTR_ERR(ca);

	ret = bch2_dev_resize(c, ca, arg.nbuckets);

	percpu_ref_put(&ca->ref);
	return ret;
}

908 909 910 911 912 913
static long bch2_ioctl_disk_resize_journal(struct bch_fs *c,
				   struct bch_ioctl_disk_resize_journal arg)
{
	struct bch_dev *ca;
	int ret;

914 915 916
	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

917 918 919 920
	if ((arg.flags & ~BCH_BY_INDEX) ||
	    arg.pad)
		return -EINVAL;

921 922 923
	if (arg.nbuckets > U32_MAX)
		return -EINVAL;

924 925 926 927 928 929 930 931 932 933
	ca = bch2_device_lookup(c, arg.dev, arg.flags);
	if (IS_ERR(ca))
		return PTR_ERR(ca);

	ret = bch2_set_nr_journal_buckets(c, ca, arg.nbuckets);

	percpu_ref_put(&ca->ref);
	return ret;
}

934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
static int bch2_fsck_online_thread_fn(void *arg)
{
	struct fsck_thread *thr = container_of(arg, struct fsck_thread, thr);
	struct bch_fs *c = thr->c;

	c->output_filter = current;
	c->output = &thr->output;

	/*
	 * XXX: can we figure out a way to do this without mucking with c->opts?
	 */
	if (opt_defined(thr->opts, fix_errors))
		c->opts.fix_errors = thr->opts.fix_errors;
	c->opts.fsck = true;

	c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info;
	bch2_run_online_recovery_passes(c);

	c->output = NULL;
	c->output_filter = NULL;

	thr->thr.done = true;
	wake_up(&thr->output.wait);

	up(&c->online_fsck_mutex);
	bch2_ro_ref_put(c);
	return 0;
}

static long bch2_ioctl_fsck_online(struct bch_fs *c,
				   struct bch_ioctl_fsck_online arg)
{
	struct fsck_thread *thr = NULL;
	long ret = 0;

	if (arg.flags)
		return -EINVAL;

	if (!capable(CAP_SYS_ADMIN))
		return -EPERM;

	if (!bch2_ro_ref_tryget(c))
		return -EROFS;

	if (down_trylock(&c->online_fsck_mutex)) {
		bch2_ro_ref_put(c);
		return -EAGAIN;
	}

	thr = kzalloc(sizeof(*thr), GFP_KERNEL);
	if (!thr) {
		ret = -ENOMEM;
		goto err;
	}

	thr->c = c;
	thr->opts = bch2_opts_empty();
	thr->output.buf	= PRINTBUF;
	thr->output.buf.atomic++;
	spin_lock_init(&thr->output.lock);
	init_waitqueue_head(&thr->output.wait);
	darray_init(&thr->output2);

	if (arg.opts) {
		char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);

		ret =   PTR_ERR_OR_ZERO(optstr) ?:
			bch2_parse_mount_opts(c, &thr->opts, optstr);
		kfree(optstr);

		if (ret)
			goto err;
	}

	ret = run_thread_with_file(&thr->thr,
				   &fsck_thread_ops,
				   bch2_fsck_online_thread_fn,
				   "bch-fsck");
err:
	if (ret < 0) {
		bch_err_fn(c, ret);
		if (thr)
			bch2_fsck_thread_free(thr);
		up(&c->online_fsck_mutex);
		bch2_ro_ref_put(c);
	}
	return ret;
}

1023 1024 1025 1026 1027 1028
#define BCH_IOCTL(_name, _argtype)					\
do {									\
	_argtype i;							\
									\
	if (copy_from_user(&i, arg, sizeof(i)))				\
		return -EFAULT;						\
1029 1030
	ret = bch2_ioctl_##_name(c, i);					\
	goto out;							\
1031 1032 1033 1034
} while (0)

long bch2_fs_ioctl(struct bch_fs *c, unsigned cmd, void __user *arg)
{
1035 1036
	long ret;

1037 1038 1039
	switch (cmd) {
	case BCH_IOCTL_QUERY_UUID:
		return bch2_ioctl_query_uuid(c, arg);
1040 1041 1042 1043
	case BCH_IOCTL_FS_USAGE:
		return bch2_ioctl_fs_usage(c, arg);
	case BCH_IOCTL_DEV_USAGE:
		return bch2_ioctl_dev_usage(c, arg);
1044 1045
	case BCH_IOCTL_DEV_USAGE_V2:
		return bch2_ioctl_dev_usage_v2(c, arg);
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
#if 0
	case BCH_IOCTL_START:
		BCH_IOCTL(start, struct bch_ioctl_start);
	case BCH_IOCTL_STOP:
		return bch2_ioctl_stop(c);
#endif
	case BCH_IOCTL_READ_SUPER:
		BCH_IOCTL(read_super, struct bch_ioctl_read_super);
	case BCH_IOCTL_DISK_GET_IDX:
		BCH_IOCTL(disk_get_idx, struct bch_ioctl_disk_get_idx);
	}

1058
	if (!test_bit(BCH_FS_started, &c->flags))
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
		return -EINVAL;

	switch (cmd) {
	case BCH_IOCTL_DISK_ADD:
		BCH_IOCTL(disk_add, struct bch_ioctl_disk);
	case BCH_IOCTL_DISK_REMOVE:
		BCH_IOCTL(disk_remove, struct bch_ioctl_disk);
	case BCH_IOCTL_DISK_ONLINE:
		BCH_IOCTL(disk_online, struct bch_ioctl_disk);
	case BCH_IOCTL_DISK_OFFLINE:
		BCH_IOCTL(disk_offline, struct bch_ioctl_disk);
	case BCH_IOCTL_DISK_SET_STATE:
		BCH_IOCTL(disk_set_state, struct bch_ioctl_disk_set_state);
	case BCH_IOCTL_DATA:
		BCH_IOCTL(data, struct bch_ioctl_data);
	case BCH_IOCTL_DISK_RESIZE:
		BCH_IOCTL(disk_resize, struct bch_ioctl_disk_resize);
1076 1077
	case BCH_IOCTL_DISK_RESIZE_JOURNAL:
		BCH_IOCTL(disk_resize_journal, struct bch_ioctl_disk_resize_journal);
1078 1079
	case BCH_IOCTL_FSCK_ONLINE:
		BCH_IOCTL(fsck_online, struct bch_ioctl_fsck_online);
1080 1081 1082
	default:
		return -ENOTTY;
	}
1083 1084 1085 1086
out:
	if (ret < 0)
		ret = bch2_err_class(ret);
	return ret;
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
}

static DEFINE_IDR(bch_chardev_minor);

static long bch2_chardev_ioctl(struct file *filp, unsigned cmd, unsigned long v)
{
	unsigned minor = iminor(file_inode(filp));
	struct bch_fs *c = minor < U8_MAX ? idr_find(&bch_chardev_minor, minor) : NULL;
	void __user *arg = (void __user *) v;

	return c
		? bch2_fs_ioctl(c, cmd, arg)
		: bch2_global_ioctl(cmd, arg);
}

static const struct file_operations bch_chardev_fops = {
	.owner		= THIS_MODULE,
	.unlocked_ioctl = bch2_chardev_ioctl,
	.open		= nonseekable_open,
};

static int bch_chardev_major;
static struct class *bch_chardev_class;
static struct device *bch_chardev;

void bch2_fs_chardev_exit(struct bch_fs *c)
{
	if (!IS_ERR_OR_NULL(c->chardev))
		device_unregister(c->chardev);
	if (c->minor >= 0)
		idr_remove(&bch_chardev_minor, c->minor);
}

int bch2_fs_chardev_init(struct bch_fs *c)
{
	c->minor = idr_alloc(&bch_chardev_minor, c, 0, 0, GFP_KERNEL);
	if (c->minor < 0)
		return c->minor;

	c->chardev = device_create(bch_chardev_class, NULL,
				   MKDEV(bch_chardev_major, c->minor), c,
				   "bcachefs%u-ctl", c->minor);
	if (IS_ERR(c->chardev))
		return PTR_ERR(c->chardev);

	return 0;
}

void bch2_chardev_exit(void)
{
	if (!IS_ERR_OR_NULL(bch_chardev_class))
		device_destroy(bch_chardev_class,
			       MKDEV(bch_chardev_major, U8_MAX));
	if (!IS_ERR_OR_NULL(bch_chardev_class))
		class_destroy(bch_chardev_class);
	if (bch_chardev_major > 0)
		unregister_chrdev(bch_chardev_major, "bcachefs");
}

int __init bch2_chardev_init(void)
{
	bch_chardev_major = register_chrdev(0, "bcachefs-ctl", &bch_chardev_fops);
	if (bch_chardev_major < 0)
		return bch_chardev_major;

	bch_chardev_class = class_create("bcachefs");
	if (IS_ERR(bch_chardev_class))
		return PTR_ERR(bch_chardev_class);

	bch_chardev = device_create(bch_chardev_class, NULL,
				    MKDEV(bch_chardev_major, U8_MAX),
				    NULL, "bcachefs-ctl");
	if (IS_ERR(bch_chardev))
		return PTR_ERR(bch_chardev);

	return 0;
}

#endif /* NO_BCACHEFS_CHARDEV */