Linux 6.13-rc4
[linux.git] / fs / bcachefs / chardev.c
blob2182b555c112cc6c66949e2ebf84937971a185ba
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef NO_BCACHEFS_CHARDEV
4 #include "bcachefs.h"
5 #include "bcachefs_ioctl.h"
6 #include "buckets.h"
7 #include "chardev.h"
8 #include "disk_accounting.h"
9 #include "journal.h"
10 #include "move.h"
11 #include "recovery_passes.h"
12 #include "replicas.h"
13 #include "super.h"
14 #include "super-io.h"
15 #include "thread_with_file.h"
17 #include <linux/cdev.h>
18 #include <linux/device.h>
19 #include <linux/fs.h>
20 #include <linux/ioctl.h>
21 #include <linux/major.h>
22 #include <linux/sched/task.h>
23 #include <linux/slab.h>
24 #include <linux/uaccess.h>
26 /* returns with ref on ca->ref */
27 static struct bch_dev *bch2_device_lookup(struct bch_fs *c, u64 dev,
28 unsigned flags)
30 struct bch_dev *ca;
32 if (flags & BCH_BY_INDEX) {
33 if (dev >= c->sb.nr_devices)
34 return ERR_PTR(-EINVAL);
36 ca = bch2_dev_tryget_noerror(c, dev);
37 if (!ca)
38 return ERR_PTR(-EINVAL);
39 } else {
40 char *path;
42 path = strndup_user((const char __user *)
43 (unsigned long) dev, PATH_MAX);
44 if (IS_ERR(path))
45 return ERR_CAST(path);
47 ca = bch2_dev_lookup(c, path);
48 kfree(path);
51 return ca;
54 #if 0
55 static long bch2_ioctl_assemble(struct bch_ioctl_assemble __user *user_arg)
57 struct bch_ioctl_assemble arg;
58 struct bch_fs *c;
59 u64 *user_devs = NULL;
60 char **devs = NULL;
61 unsigned i;
62 int ret = -EFAULT;
64 if (copy_from_user(&arg, user_arg, sizeof(arg)))
65 return -EFAULT;
67 if (arg.flags || arg.pad)
68 return -EINVAL;
70 user_devs = kmalloc_array(arg.nr_devs, sizeof(u64), GFP_KERNEL);
71 if (!user_devs)
72 return -ENOMEM;
74 devs = kcalloc(arg.nr_devs, sizeof(char *), GFP_KERNEL);
76 if (copy_from_user(user_devs, user_arg->devs,
77 sizeof(u64) * arg.nr_devs))
78 goto err;
80 for (i = 0; i < arg.nr_devs; i++) {
81 devs[i] = strndup_user((const char __user *)(unsigned long)
82 user_devs[i],
83 PATH_MAX);
84 ret= PTR_ERR_OR_ZERO(devs[i]);
85 if (ret)
86 goto err;
89 c = bch2_fs_open(devs, arg.nr_devs, bch2_opts_empty());
90 ret = PTR_ERR_OR_ZERO(c);
91 if (!ret)
92 closure_put(&c->cl);
93 err:
94 if (devs)
95 for (i = 0; i < arg.nr_devs; i++)
96 kfree(devs[i]);
97 kfree(devs);
98 return ret;
101 static long bch2_ioctl_incremental(struct bch_ioctl_incremental __user *user_arg)
103 struct bch_ioctl_incremental arg;
104 const char *err;
105 char *path;
107 if (copy_from_user(&arg, user_arg, sizeof(arg)))
108 return -EFAULT;
110 if (arg.flags || arg.pad)
111 return -EINVAL;
113 path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
114 ret = PTR_ERR_OR_ZERO(path);
115 if (ret)
116 return ret;
118 err = bch2_fs_open_incremental(path);
119 kfree(path);
121 if (err) {
122 pr_err("Could not register bcachefs devices: %s", err);
123 return -EINVAL;
126 return 0;
128 #endif
130 struct fsck_thread {
131 struct thread_with_stdio thr;
132 struct bch_fs *c;
133 struct bch_opts opts;
136 static void bch2_fsck_thread_exit(struct thread_with_stdio *_thr)
138 struct fsck_thread *thr = container_of(_thr, struct fsck_thread, thr);
139 kfree(thr);
142 static int bch2_fsck_offline_thread_fn(struct thread_with_stdio *stdio)
144 struct fsck_thread *thr = container_of(stdio, struct fsck_thread, thr);
145 struct bch_fs *c = thr->c;
147 int ret = PTR_ERR_OR_ZERO(c);
148 if (ret)
149 return ret;
151 ret = bch2_fs_start(thr->c);
152 if (ret)
153 goto err;
155 if (test_bit(BCH_FS_errors_fixed, &c->flags)) {
156 bch2_stdio_redirect_printf(&stdio->stdio, false, "%s: errors fixed\n", c->name);
157 ret |= 1;
159 if (test_bit(BCH_FS_error, &c->flags)) {
160 bch2_stdio_redirect_printf(&stdio->stdio, false, "%s: still has errors\n", c->name);
161 ret |= 4;
163 err:
164 bch2_fs_stop(c);
165 return ret;
168 static const struct thread_with_stdio_ops bch2_offline_fsck_ops = {
169 .exit = bch2_fsck_thread_exit,
170 .fn = bch2_fsck_offline_thread_fn,
173 static long bch2_ioctl_fsck_offline(struct bch_ioctl_fsck_offline __user *user_arg)
175 struct bch_ioctl_fsck_offline arg;
176 struct fsck_thread *thr = NULL;
177 darray_str(devs) = {};
178 long ret = 0;
180 if (copy_from_user(&arg, user_arg, sizeof(arg)))
181 return -EFAULT;
183 if (arg.flags)
184 return -EINVAL;
186 if (!capable(CAP_SYS_ADMIN))
187 return -EPERM;
189 for (size_t i = 0; i < arg.nr_devs; i++) {
190 u64 dev_u64;
191 ret = copy_from_user_errcode(&dev_u64, &user_arg->devs[i], sizeof(u64));
192 if (ret)
193 goto err;
195 char *dev_str = strndup_user((char __user *)(unsigned long) dev_u64, PATH_MAX);
196 ret = PTR_ERR_OR_ZERO(dev_str);
197 if (ret)
198 goto err;
200 ret = darray_push(&devs, dev_str);
201 if (ret) {
202 kfree(dev_str);
203 goto err;
207 thr = kzalloc(sizeof(*thr), GFP_KERNEL);
208 if (!thr) {
209 ret = -ENOMEM;
210 goto err;
213 thr->opts = bch2_opts_empty();
215 if (arg.opts) {
216 char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);
217 ret = PTR_ERR_OR_ZERO(optstr) ?:
218 bch2_parse_mount_opts(NULL, &thr->opts, NULL, optstr);
219 if (!IS_ERR(optstr))
220 kfree(optstr);
222 if (ret)
223 goto err;
226 opt_set(thr->opts, stdio, (u64)(unsigned long)&thr->thr.stdio);
227 opt_set(thr->opts, read_only, 1);
228 opt_set(thr->opts, ratelimit_errors, 0);
230 /* We need request_key() to be called before we punt to kthread: */
231 opt_set(thr->opts, nostart, true);
233 bch2_thread_with_stdio_init(&thr->thr, &bch2_offline_fsck_ops);
235 thr->c = bch2_fs_open(devs.data, arg.nr_devs, thr->opts);
237 if (!IS_ERR(thr->c) &&
238 thr->c->opts.errors == BCH_ON_ERROR_panic)
239 thr->c->opts.errors = BCH_ON_ERROR_ro;
241 ret = __bch2_run_thread_with_stdio(&thr->thr);
242 out:
243 darray_for_each(devs, i)
244 kfree(*i);
245 darray_exit(&devs);
246 return ret;
247 err:
248 if (thr)
249 bch2_fsck_thread_exit(&thr->thr);
250 pr_err("ret %s", bch2_err_str(ret));
251 goto out;
254 static long bch2_global_ioctl(unsigned cmd, void __user *arg)
256 long ret;
258 switch (cmd) {
259 #if 0
260 case BCH_IOCTL_ASSEMBLE:
261 return bch2_ioctl_assemble(arg);
262 case BCH_IOCTL_INCREMENTAL:
263 return bch2_ioctl_incremental(arg);
264 #endif
265 case BCH_IOCTL_FSCK_OFFLINE: {
266 ret = bch2_ioctl_fsck_offline(arg);
267 break;
269 default:
270 ret = -ENOTTY;
271 break;
274 if (ret < 0)
275 ret = bch2_err_class(ret);
276 return ret;
279 static long bch2_ioctl_query_uuid(struct bch_fs *c,
280 struct bch_ioctl_query_uuid __user *user_arg)
282 return copy_to_user_errcode(&user_arg->uuid, &c->sb.user_uuid,
283 sizeof(c->sb.user_uuid));
286 #if 0
287 static long bch2_ioctl_start(struct bch_fs *c, struct bch_ioctl_start arg)
289 if (!capable(CAP_SYS_ADMIN))
290 return -EPERM;
292 if (arg.flags || arg.pad)
293 return -EINVAL;
295 return bch2_fs_start(c);
298 static long bch2_ioctl_stop(struct bch_fs *c)
300 if (!capable(CAP_SYS_ADMIN))
301 return -EPERM;
303 bch2_fs_stop(c);
304 return 0;
306 #endif
308 static long bch2_ioctl_disk_add(struct bch_fs *c, struct bch_ioctl_disk arg)
310 char *path;
311 int ret;
313 if (!capable(CAP_SYS_ADMIN))
314 return -EPERM;
316 if (arg.flags || arg.pad)
317 return -EINVAL;
319 path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
320 ret = PTR_ERR_OR_ZERO(path);
321 if (ret)
322 return ret;
324 ret = bch2_dev_add(c, path);
325 if (!IS_ERR(path))
326 kfree(path);
328 return ret;
331 static long bch2_ioctl_disk_remove(struct bch_fs *c, struct bch_ioctl_disk arg)
333 struct bch_dev *ca;
335 if (!capable(CAP_SYS_ADMIN))
336 return -EPERM;
338 if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
339 BCH_FORCE_IF_METADATA_LOST|
340 BCH_FORCE_IF_DEGRADED|
341 BCH_BY_INDEX)) ||
342 arg.pad)
343 return -EINVAL;
345 ca = bch2_device_lookup(c, arg.dev, arg.flags);
346 if (IS_ERR(ca))
347 return PTR_ERR(ca);
349 return bch2_dev_remove(c, ca, arg.flags);
352 static long bch2_ioctl_disk_online(struct bch_fs *c, struct bch_ioctl_disk arg)
354 char *path;
355 int ret;
357 if (!capable(CAP_SYS_ADMIN))
358 return -EPERM;
360 if (arg.flags || arg.pad)
361 return -EINVAL;
363 path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
364 ret = PTR_ERR_OR_ZERO(path);
365 if (ret)
366 return ret;
368 ret = bch2_dev_online(c, path);
369 kfree(path);
370 return ret;
373 static long bch2_ioctl_disk_offline(struct bch_fs *c, struct bch_ioctl_disk arg)
375 struct bch_dev *ca;
376 int ret;
378 if (!capable(CAP_SYS_ADMIN))
379 return -EPERM;
381 if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
382 BCH_FORCE_IF_METADATA_LOST|
383 BCH_FORCE_IF_DEGRADED|
384 BCH_BY_INDEX)) ||
385 arg.pad)
386 return -EINVAL;
388 ca = bch2_device_lookup(c, arg.dev, arg.flags);
389 if (IS_ERR(ca))
390 return PTR_ERR(ca);
392 ret = bch2_dev_offline(c, ca, arg.flags);
393 bch2_dev_put(ca);
394 return ret;
397 static long bch2_ioctl_disk_set_state(struct bch_fs *c,
398 struct bch_ioctl_disk_set_state arg)
400 struct bch_dev *ca;
401 int ret;
403 if (!capable(CAP_SYS_ADMIN))
404 return -EPERM;
406 if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
407 BCH_FORCE_IF_METADATA_LOST|
408 BCH_FORCE_IF_DEGRADED|
409 BCH_BY_INDEX)) ||
410 arg.pad[0] || arg.pad[1] || arg.pad[2] ||
411 arg.new_state >= BCH_MEMBER_STATE_NR)
412 return -EINVAL;
414 ca = bch2_device_lookup(c, arg.dev, arg.flags);
415 if (IS_ERR(ca))
416 return PTR_ERR(ca);
418 ret = bch2_dev_set_state(c, ca, arg.new_state, arg.flags);
419 if (ret)
420 bch_err(c, "Error setting device state: %s", bch2_err_str(ret));
422 bch2_dev_put(ca);
423 return ret;
426 struct bch_data_ctx {
427 struct thread_with_file thr;
429 struct bch_fs *c;
430 struct bch_ioctl_data arg;
431 struct bch_move_stats stats;
434 static int bch2_data_thread(void *arg)
436 struct bch_data_ctx *ctx = container_of(arg, struct bch_data_ctx, thr);
438 ctx->thr.ret = bch2_data_job(ctx->c, &ctx->stats, ctx->arg);
439 ctx->stats.data_type = U8_MAX;
440 return 0;
443 static int bch2_data_job_release(struct inode *inode, struct file *file)
445 struct bch_data_ctx *ctx = container_of(file->private_data, struct bch_data_ctx, thr);
447 bch2_thread_with_file_exit(&ctx->thr);
448 kfree(ctx);
449 return 0;
452 static ssize_t bch2_data_job_read(struct file *file, char __user *buf,
453 size_t len, loff_t *ppos)
455 struct bch_data_ctx *ctx = container_of(file->private_data, struct bch_data_ctx, thr);
456 struct bch_fs *c = ctx->c;
457 struct bch_ioctl_data_event e = {
458 .type = BCH_DATA_EVENT_PROGRESS,
459 .p.data_type = ctx->stats.data_type,
460 .p.btree_id = ctx->stats.pos.btree,
461 .p.pos = ctx->stats.pos.pos,
462 .p.sectors_done = atomic64_read(&ctx->stats.sectors_seen),
463 .p.sectors_total = bch2_fs_usage_read_short(c).used,
466 if (len < sizeof(e))
467 return -EINVAL;
469 return copy_to_user_errcode(buf, &e, sizeof(e)) ?: sizeof(e);
472 static const struct file_operations bcachefs_data_ops = {
473 .release = bch2_data_job_release,
474 .read = bch2_data_job_read,
477 static long bch2_ioctl_data(struct bch_fs *c,
478 struct bch_ioctl_data arg)
480 struct bch_data_ctx *ctx;
481 int ret;
483 if (!capable(CAP_SYS_ADMIN))
484 return -EPERM;
486 if (arg.op >= BCH_DATA_OP_NR || arg.flags)
487 return -EINVAL;
489 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
490 if (!ctx)
491 return -ENOMEM;
493 ctx->c = c;
494 ctx->arg = arg;
496 ret = bch2_run_thread_with_file(&ctx->thr,
497 &bcachefs_data_ops,
498 bch2_data_thread);
499 if (ret < 0)
500 kfree(ctx);
501 return ret;
504 static long bch2_ioctl_fs_usage(struct bch_fs *c,
505 struct bch_ioctl_fs_usage __user *user_arg)
507 struct bch_ioctl_fs_usage arg = {};
508 darray_char replicas = {};
509 u32 replica_entries_bytes;
510 int ret = 0;
512 if (!test_bit(BCH_FS_started, &c->flags))
513 return -EINVAL;
515 if (get_user(replica_entries_bytes, &user_arg->replica_entries_bytes))
516 return -EFAULT;
518 ret = bch2_fs_replicas_usage_read(c, &replicas) ?:
519 (replica_entries_bytes < replicas.nr ? -ERANGE : 0) ?:
520 copy_to_user_errcode(&user_arg->replicas, replicas.data, replicas.nr);
521 if (ret)
522 goto err;
524 struct bch_fs_usage_short u = bch2_fs_usage_read_short(c);
525 arg.capacity = c->capacity;
526 arg.used = u.used;
527 arg.online_reserved = percpu_u64_get(c->online_reserved);
528 arg.replica_entries_bytes = replicas.nr;
530 for (unsigned i = 0; i < BCH_REPLICAS_MAX; i++) {
531 struct disk_accounting_pos k = {
532 .type = BCH_DISK_ACCOUNTING_persistent_reserved,
533 .persistent_reserved.nr_replicas = i,
536 bch2_accounting_mem_read(c,
537 disk_accounting_pos_to_bpos(&k),
538 &arg.persistent_reserved[i], 1);
541 ret = copy_to_user_errcode(user_arg, &arg, sizeof(arg));
542 err:
543 darray_exit(&replicas);
544 return ret;
547 static long bch2_ioctl_query_accounting(struct bch_fs *c,
548 struct bch_ioctl_query_accounting __user *user_arg)
550 struct bch_ioctl_query_accounting arg;
551 darray_char accounting = {};
552 int ret = 0;
554 if (!test_bit(BCH_FS_started, &c->flags))
555 return -EINVAL;
557 ret = copy_from_user_errcode(&arg, user_arg, sizeof(arg)) ?:
558 bch2_fs_accounting_read(c, &accounting, arg.accounting_types_mask) ?:
559 (arg.accounting_u64s * sizeof(u64) < accounting.nr ? -ERANGE : 0) ?:
560 copy_to_user_errcode(&user_arg->accounting, accounting.data, accounting.nr);
561 if (ret)
562 goto err;
564 arg.capacity = c->capacity;
565 arg.used = bch2_fs_usage_read_short(c).used;
566 arg.online_reserved = percpu_u64_get(c->online_reserved);
567 arg.accounting_u64s = accounting.nr / sizeof(u64);
569 ret = copy_to_user_errcode(user_arg, &arg, sizeof(arg));
570 err:
571 darray_exit(&accounting);
572 return ret;
575 /* obsolete, didn't allow for new data types: */
576 static long bch2_ioctl_dev_usage(struct bch_fs *c,
577 struct bch_ioctl_dev_usage __user *user_arg)
579 struct bch_ioctl_dev_usage arg;
580 struct bch_dev_usage src;
581 struct bch_dev *ca;
582 unsigned i;
584 if (!test_bit(BCH_FS_started, &c->flags))
585 return -EINVAL;
587 if (copy_from_user(&arg, user_arg, sizeof(arg)))
588 return -EFAULT;
590 if ((arg.flags & ~BCH_BY_INDEX) ||
591 arg.pad[0] ||
592 arg.pad[1] ||
593 arg.pad[2])
594 return -EINVAL;
596 ca = bch2_device_lookup(c, arg.dev, arg.flags);
597 if (IS_ERR(ca))
598 return PTR_ERR(ca);
600 src = bch2_dev_usage_read(ca);
602 arg.state = ca->mi.state;
603 arg.bucket_size = ca->mi.bucket_size;
604 arg.nr_buckets = ca->mi.nbuckets - ca->mi.first_bucket;
606 for (i = 0; i < ARRAY_SIZE(arg.d); i++) {
607 arg.d[i].buckets = src.d[i].buckets;
608 arg.d[i].sectors = src.d[i].sectors;
609 arg.d[i].fragmented = src.d[i].fragmented;
612 bch2_dev_put(ca);
614 return copy_to_user_errcode(user_arg, &arg, sizeof(arg));
617 static long bch2_ioctl_dev_usage_v2(struct bch_fs *c,
618 struct bch_ioctl_dev_usage_v2 __user *user_arg)
620 struct bch_ioctl_dev_usage_v2 arg;
621 struct bch_dev_usage src;
622 struct bch_dev *ca;
623 int ret = 0;
625 if (!test_bit(BCH_FS_started, &c->flags))
626 return -EINVAL;
628 if (copy_from_user(&arg, user_arg, sizeof(arg)))
629 return -EFAULT;
631 if ((arg.flags & ~BCH_BY_INDEX) ||
632 arg.pad[0] ||
633 arg.pad[1] ||
634 arg.pad[2])
635 return -EINVAL;
637 ca = bch2_device_lookup(c, arg.dev, arg.flags);
638 if (IS_ERR(ca))
639 return PTR_ERR(ca);
641 src = bch2_dev_usage_read(ca);
643 arg.state = ca->mi.state;
644 arg.bucket_size = ca->mi.bucket_size;
645 arg.nr_data_types = min(arg.nr_data_types, BCH_DATA_NR);
646 arg.nr_buckets = ca->mi.nbuckets - ca->mi.first_bucket;
648 ret = copy_to_user_errcode(user_arg, &arg, sizeof(arg));
649 if (ret)
650 goto err;
652 for (unsigned i = 0; i < arg.nr_data_types; i++) {
653 struct bch_ioctl_dev_usage_type t = {
654 .buckets = src.d[i].buckets,
655 .sectors = src.d[i].sectors,
656 .fragmented = src.d[i].fragmented,
659 ret = copy_to_user_errcode(&user_arg->d[i], &t, sizeof(t));
660 if (ret)
661 goto err;
663 err:
664 bch2_dev_put(ca);
665 return ret;
668 static long bch2_ioctl_read_super(struct bch_fs *c,
669 struct bch_ioctl_read_super arg)
671 struct bch_dev *ca = NULL;
672 struct bch_sb *sb;
673 int ret = 0;
675 if (!capable(CAP_SYS_ADMIN))
676 return -EPERM;
678 if ((arg.flags & ~(BCH_BY_INDEX|BCH_READ_DEV)) ||
679 arg.pad)
680 return -EINVAL;
682 mutex_lock(&c->sb_lock);
684 if (arg.flags & BCH_READ_DEV) {
685 ca = bch2_device_lookup(c, arg.dev, arg.flags);
686 ret = PTR_ERR_OR_ZERO(ca);
687 if (ret)
688 goto err_unlock;
690 sb = ca->disk_sb.sb;
691 } else {
692 sb = c->disk_sb.sb;
695 if (vstruct_bytes(sb) > arg.size) {
696 ret = -ERANGE;
697 goto err;
700 ret = copy_to_user_errcode((void __user *)(unsigned long)arg.sb, sb,
701 vstruct_bytes(sb));
702 err:
703 bch2_dev_put(ca);
704 err_unlock:
705 mutex_unlock(&c->sb_lock);
706 return ret;
709 static long bch2_ioctl_disk_get_idx(struct bch_fs *c,
710 struct bch_ioctl_disk_get_idx arg)
712 dev_t dev = huge_decode_dev(arg.dev);
714 if (!capable(CAP_SYS_ADMIN))
715 return -EPERM;
717 if (!dev)
718 return -EINVAL;
720 for_each_online_member(c, ca)
721 if (ca->dev == dev) {
722 percpu_ref_put(&ca->io_ref);
723 return ca->dev_idx;
726 return -BCH_ERR_ENOENT_dev_idx_not_found;
729 static long bch2_ioctl_disk_resize(struct bch_fs *c,
730 struct bch_ioctl_disk_resize arg)
732 struct bch_dev *ca;
733 int ret;
735 if (!capable(CAP_SYS_ADMIN))
736 return -EPERM;
738 if ((arg.flags & ~BCH_BY_INDEX) ||
739 arg.pad)
740 return -EINVAL;
742 ca = bch2_device_lookup(c, arg.dev, arg.flags);
743 if (IS_ERR(ca))
744 return PTR_ERR(ca);
746 ret = bch2_dev_resize(c, ca, arg.nbuckets);
748 bch2_dev_put(ca);
749 return ret;
752 static long bch2_ioctl_disk_resize_journal(struct bch_fs *c,
753 struct bch_ioctl_disk_resize_journal arg)
755 struct bch_dev *ca;
756 int ret;
758 if (!capable(CAP_SYS_ADMIN))
759 return -EPERM;
761 if ((arg.flags & ~BCH_BY_INDEX) ||
762 arg.pad)
763 return -EINVAL;
765 if (arg.nbuckets > U32_MAX)
766 return -EINVAL;
768 ca = bch2_device_lookup(c, arg.dev, arg.flags);
769 if (IS_ERR(ca))
770 return PTR_ERR(ca);
772 ret = bch2_set_nr_journal_buckets(c, ca, arg.nbuckets);
774 bch2_dev_put(ca);
775 return ret;
778 static int bch2_fsck_online_thread_fn(struct thread_with_stdio *stdio)
780 struct fsck_thread *thr = container_of(stdio, struct fsck_thread, thr);
781 struct bch_fs *c = thr->c;
783 c->stdio_filter = current;
784 c->stdio = &thr->thr.stdio;
787 * XXX: can we figure out a way to do this without mucking with c->opts?
789 unsigned old_fix_errors = c->opts.fix_errors;
790 if (opt_defined(thr->opts, fix_errors))
791 c->opts.fix_errors = thr->opts.fix_errors;
792 else
793 c->opts.fix_errors = FSCK_FIX_ask;
795 c->opts.fsck = true;
796 set_bit(BCH_FS_fsck_running, &c->flags);
798 c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info;
799 int ret = bch2_run_online_recovery_passes(c);
801 clear_bit(BCH_FS_fsck_running, &c->flags);
802 bch_err_fn(c, ret);
804 c->stdio = NULL;
805 c->stdio_filter = NULL;
806 c->opts.fix_errors = old_fix_errors;
808 up(&c->online_fsck_mutex);
809 bch2_ro_ref_put(c);
810 return ret;
813 static const struct thread_with_stdio_ops bch2_online_fsck_ops = {
814 .exit = bch2_fsck_thread_exit,
815 .fn = bch2_fsck_online_thread_fn,
818 static long bch2_ioctl_fsck_online(struct bch_fs *c,
819 struct bch_ioctl_fsck_online arg)
821 struct fsck_thread *thr = NULL;
822 long ret = 0;
824 if (arg.flags)
825 return -EINVAL;
827 if (!capable(CAP_SYS_ADMIN))
828 return -EPERM;
830 if (!bch2_ro_ref_tryget(c))
831 return -EROFS;
833 if (down_trylock(&c->online_fsck_mutex)) {
834 bch2_ro_ref_put(c);
835 return -EAGAIN;
838 thr = kzalloc(sizeof(*thr), GFP_KERNEL);
839 if (!thr) {
840 ret = -ENOMEM;
841 goto err;
844 thr->c = c;
845 thr->opts = bch2_opts_empty();
847 if (arg.opts) {
848 char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);
850 ret = PTR_ERR_OR_ZERO(optstr) ?:
851 bch2_parse_mount_opts(c, &thr->opts, NULL, optstr);
852 if (!IS_ERR(optstr))
853 kfree(optstr);
855 if (ret)
856 goto err;
859 ret = bch2_run_thread_with_stdio(&thr->thr, &bch2_online_fsck_ops);
860 err:
861 if (ret < 0) {
862 bch_err_fn(c, ret);
863 if (thr)
864 bch2_fsck_thread_exit(&thr->thr);
865 up(&c->online_fsck_mutex);
866 bch2_ro_ref_put(c);
868 return ret;
871 #define BCH_IOCTL(_name, _argtype) \
872 do { \
873 _argtype i; \
875 if (copy_from_user(&i, arg, sizeof(i))) \
876 return -EFAULT; \
877 ret = bch2_ioctl_##_name(c, i); \
878 goto out; \
879 } while (0)
881 long bch2_fs_ioctl(struct bch_fs *c, unsigned cmd, void __user *arg)
883 long ret;
885 switch (cmd) {
886 case BCH_IOCTL_QUERY_UUID:
887 return bch2_ioctl_query_uuid(c, arg);
888 case BCH_IOCTL_FS_USAGE:
889 return bch2_ioctl_fs_usage(c, arg);
890 case BCH_IOCTL_DEV_USAGE:
891 return bch2_ioctl_dev_usage(c, arg);
892 case BCH_IOCTL_DEV_USAGE_V2:
893 return bch2_ioctl_dev_usage_v2(c, arg);
894 #if 0
895 case BCH_IOCTL_START:
896 BCH_IOCTL(start, struct bch_ioctl_start);
897 case BCH_IOCTL_STOP:
898 return bch2_ioctl_stop(c);
899 #endif
900 case BCH_IOCTL_READ_SUPER:
901 BCH_IOCTL(read_super, struct bch_ioctl_read_super);
902 case BCH_IOCTL_DISK_GET_IDX:
903 BCH_IOCTL(disk_get_idx, struct bch_ioctl_disk_get_idx);
906 if (!test_bit(BCH_FS_started, &c->flags))
907 return -EINVAL;
909 switch (cmd) {
910 case BCH_IOCTL_DISK_ADD:
911 BCH_IOCTL(disk_add, struct bch_ioctl_disk);
912 case BCH_IOCTL_DISK_REMOVE:
913 BCH_IOCTL(disk_remove, struct bch_ioctl_disk);
914 case BCH_IOCTL_DISK_ONLINE:
915 BCH_IOCTL(disk_online, struct bch_ioctl_disk);
916 case BCH_IOCTL_DISK_OFFLINE:
917 BCH_IOCTL(disk_offline, struct bch_ioctl_disk);
918 case BCH_IOCTL_DISK_SET_STATE:
919 BCH_IOCTL(disk_set_state, struct bch_ioctl_disk_set_state);
920 case BCH_IOCTL_DATA:
921 BCH_IOCTL(data, struct bch_ioctl_data);
922 case BCH_IOCTL_DISK_RESIZE:
923 BCH_IOCTL(disk_resize, struct bch_ioctl_disk_resize);
924 case BCH_IOCTL_DISK_RESIZE_JOURNAL:
925 BCH_IOCTL(disk_resize_journal, struct bch_ioctl_disk_resize_journal);
926 case BCH_IOCTL_FSCK_ONLINE:
927 BCH_IOCTL(fsck_online, struct bch_ioctl_fsck_online);
928 case BCH_IOCTL_QUERY_ACCOUNTING:
929 return bch2_ioctl_query_accounting(c, arg);
930 default:
931 return -ENOTTY;
933 out:
934 if (ret < 0)
935 ret = bch2_err_class(ret);
936 return ret;
939 static DEFINE_IDR(bch_chardev_minor);
941 static long bch2_chardev_ioctl(struct file *filp, unsigned cmd, unsigned long v)
943 unsigned minor = iminor(file_inode(filp));
944 struct bch_fs *c = minor < U8_MAX ? idr_find(&bch_chardev_minor, minor) : NULL;
945 void __user *arg = (void __user *) v;
947 return c
948 ? bch2_fs_ioctl(c, cmd, arg)
949 : bch2_global_ioctl(cmd, arg);
952 static const struct file_operations bch_chardev_fops = {
953 .owner = THIS_MODULE,
954 .unlocked_ioctl = bch2_chardev_ioctl,
955 .open = nonseekable_open,
958 static int bch_chardev_major;
959 static const struct class bch_chardev_class = {
960 .name = "bcachefs",
962 static struct device *bch_chardev;
964 void bch2_fs_chardev_exit(struct bch_fs *c)
966 if (!IS_ERR_OR_NULL(c->chardev))
967 device_unregister(c->chardev);
968 if (c->minor >= 0)
969 idr_remove(&bch_chardev_minor, c->minor);
972 int bch2_fs_chardev_init(struct bch_fs *c)
974 c->minor = idr_alloc(&bch_chardev_minor, c, 0, 0, GFP_KERNEL);
975 if (c->minor < 0)
976 return c->minor;
978 c->chardev = device_create(&bch_chardev_class, NULL,
979 MKDEV(bch_chardev_major, c->minor), c,
980 "bcachefs%u-ctl", c->minor);
981 if (IS_ERR(c->chardev))
982 return PTR_ERR(c->chardev);
984 return 0;
987 void bch2_chardev_exit(void)
989 device_destroy(&bch_chardev_class, MKDEV(bch_chardev_major, U8_MAX));
990 class_unregister(&bch_chardev_class);
991 if (bch_chardev_major > 0)
992 unregister_chrdev(bch_chardev_major, "bcachefs");
995 int __init bch2_chardev_init(void)
997 int ret;
999 bch_chardev_major = register_chrdev(0, "bcachefs-ctl", &bch_chardev_fops);
1000 if (bch_chardev_major < 0)
1001 return bch_chardev_major;
1003 ret = class_register(&bch_chardev_class);
1004 if (ret)
1005 goto major_out;
1007 bch_chardev = device_create(&bch_chardev_class, NULL,
1008 MKDEV(bch_chardev_major, U8_MAX),
1009 NULL, "bcachefs-ctl");
1010 if (IS_ERR(bch_chardev)) {
1011 ret = PTR_ERR(bch_chardev);
1012 goto class_out;
1015 return 0;
1017 class_out:
1018 class_unregister(&bch_chardev_class);
1019 major_out:
1020 unregister_chrdev(bch_chardev_major, "bcachefs-ctl");
1021 return ret;
1024 #endif /* NO_BCACHEFS_CHARDEV */