1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef NO_BCACHEFS_CHARDEV
5 #include "bcachefs_ioctl.h"
15 #include <linux/anon_inodes.h>
16 #include <linux/cdev.h>
17 #include <linux/device.h>
18 #include <linux/file.h>
20 #include <linux/ioctl.h>
21 #include <linux/kthread.h>
22 #include <linux/major.h>
23 #include <linux/poll.h>
24 #include <linux/sched/task.h>
25 #include <linux/slab.h>
26 #include <linux/uaccess.h>
29 static int copy_to_user_errcode(void __user *to, const void *from, unsigned long n)
31 return copy_to_user(to, from, n) ? -EFAULT : 0;
34 struct thread_with_file {
35 struct task_struct *task;
40 static void thread_with_file_exit(struct thread_with_file *thr)
43 kthread_stop(thr->task);
44 put_task_struct(thr->task);
49 static int run_thread_with_file(struct thread_with_file *thr,
50 const struct file_operations *fops,
51 int (*fn)(void *), const char *fmt, ...)
54 struct file *file = NULL;
56 struct printbuf name = PRINTBUF;
57 unsigned fd_flags = O_RDONLY|O_CLOEXEC|O_NONBLOCK;
60 prt_vprintf(&name, fmt, args);
64 thr->task = kthread_create(fn, thr, name.buf);
65 ret = PTR_ERR_OR_ZERO(thr->task);
69 ret = get_unused_fd_flags(fd_flags);
74 file = anon_inode_getfile(name.buf, fops, thr, fd_flags);
75 ret = PTR_ERR_OR_ZERO(file);
80 get_task_struct(thr->task);
81 wake_up_process(thr->task);
87 kthread_stop(thr->task);
93 /* returns with ref on ca->ref */
94 static struct bch_dev *bch2_device_lookup(struct bch_fs *c, u64 dev,
99 if (flags & BCH_BY_INDEX) {
100 if (dev >= c->sb.nr_devices)
101 return ERR_PTR(-EINVAL);
104 ca = rcu_dereference(c->devs[dev]);
106 percpu_ref_get(&ca->ref);
110 return ERR_PTR(-EINVAL);
114 path = strndup_user((const char __user *)
115 (unsigned long) dev, PATH_MAX);
117 return ERR_CAST(path);
119 ca = bch2_dev_lookup(c, path);
127 static long bch2_ioctl_assemble(struct bch_ioctl_assemble __user *user_arg)
129 struct bch_ioctl_assemble arg;
131 u64 *user_devs = NULL;
136 if (copy_from_user(&arg, user_arg, sizeof(arg)))
139 if (arg.flags || arg.pad)
142 user_devs = kmalloc_array(arg.nr_devs, sizeof(u64), GFP_KERNEL);
146 devs = kcalloc(arg.nr_devs, sizeof(char *), GFP_KERNEL);
148 if (copy_from_user(user_devs, user_arg->devs,
149 sizeof(u64) * arg.nr_devs))
152 for (i = 0; i < arg.nr_devs; i++) {
153 devs[i] = strndup_user((const char __user *)(unsigned long)
156 ret= PTR_ERR_OR_ZERO(devs[i]);
161 c = bch2_fs_open(devs, arg.nr_devs, bch2_opts_empty());
162 ret = PTR_ERR_OR_ZERO(c);
167 for (i = 0; i < arg.nr_devs; i++)
173 static long bch2_ioctl_incremental(struct bch_ioctl_incremental __user *user_arg)
175 struct bch_ioctl_incremental arg;
179 if (copy_from_user(&arg, user_arg, sizeof(arg)))
182 if (arg.flags || arg.pad)
185 path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
186 ret = PTR_ERR_OR_ZERO(path);
190 err = bch2_fs_open_incremental(path);
194 pr_err("Could not register bcachefs devices: %s", err);
203 struct thread_with_file thr;
208 struct bch_opts opts;
210 struct log_output output;
211 DARRAY(char) output2;
214 static void bch2_fsck_thread_free(struct fsck_thread *thr)
216 thread_with_file_exit(&thr->thr);
218 for (size_t i = 0; i < thr->nr_devs; i++)
220 darray_exit(&thr->output2);
221 printbuf_exit(&thr->output.buf);
226 static int bch2_fsck_thread_release(struct inode *inode, struct file *file)
228 struct fsck_thread *thr = container_of(file->private_data, struct fsck_thread, thr);
230 bch2_fsck_thread_free(thr);
234 static bool fsck_thread_ready(struct fsck_thread *thr)
236 return thr->output.buf.pos ||
241 static ssize_t bch2_fsck_thread_read(struct file *file, char __user *buf,
242 size_t len, loff_t *ppos)
244 struct fsck_thread *thr = container_of(file->private_data, struct fsck_thread, thr);
245 size_t copied = 0, b;
248 if ((file->f_flags & O_NONBLOCK) &&
249 !fsck_thread_ready(thr))
252 ret = wait_event_interruptible(thr->output.wait,
253 fsck_thread_ready(thr));
261 ret = darray_make_room(&thr->output2, thr->output.buf.pos);
265 spin_lock_irq(&thr->output.lock);
266 b = min_t(size_t, darray_room(thr->output2), thr->output.buf.pos);
268 memcpy(&darray_top(thr->output2), thr->output.buf.buf, b);
269 memmove(thr->output.buf.buf,
270 thr->output.buf.buf + b,
271 thr->output.buf.pos - b);
273 thr->output2.nr += b;
274 thr->output.buf.pos -= b;
275 spin_unlock_irq(&thr->output.lock);
277 b = min(len, thr->output2.nr);
281 b -= copy_to_user(buf, thr->output2.data, b);
291 memmove(thr->output2.data,
292 thr->output2.data + b,
293 thr->output2.nr - b);
294 thr->output2.nr -= b;
297 return copied ?: ret;
300 static __poll_t bch2_fsck_thread_poll(struct file *file, struct poll_table_struct *wait)
302 struct fsck_thread *thr = container_of(file->private_data, struct fsck_thread, thr);
304 poll_wait(file, &thr->output.wait, wait);
306 return fsck_thread_ready(thr)
311 static const struct file_operations fsck_thread_ops = {
312 .release = bch2_fsck_thread_release,
313 .read = bch2_fsck_thread_read,
314 .poll = bch2_fsck_thread_poll,
318 static int bch2_fsck_offline_thread_fn(void *arg)
320 struct fsck_thread *thr = container_of(arg, struct fsck_thread, thr);
321 struct bch_fs *c = bch2_fs_open(thr->devs, thr->nr_devs, thr->opts);
323 thr->thr.ret = PTR_ERR_OR_ZERO(c);
327 thr->thr.done = true;
328 wake_up(&thr->output.wait);
332 static long bch2_ioctl_fsck_offline(struct bch_ioctl_fsck_offline __user *user_arg)
334 struct bch_ioctl_fsck_offline arg;
335 struct fsck_thread *thr = NULL;
339 if (copy_from_user(&arg, user_arg, sizeof(arg)))
345 if (!capable(CAP_SYS_ADMIN))
348 if (!(devs = kcalloc(arg.nr_devs, sizeof(*devs), GFP_KERNEL)) ||
349 !(thr = kzalloc(sizeof(*thr), GFP_KERNEL)) ||
350 !(thr->devs = kcalloc(arg.nr_devs, sizeof(*thr->devs), GFP_KERNEL))) {
355 thr->opts = bch2_opts_empty();
356 thr->nr_devs = arg.nr_devs;
357 thr->output.buf = PRINTBUF;
358 thr->output.buf.atomic++;
359 spin_lock_init(&thr->output.lock);
360 init_waitqueue_head(&thr->output.wait);
361 darray_init(&thr->output2);
363 if (copy_from_user(devs, &user_arg->devs[0],
364 array_size(sizeof(user_arg->devs[0]), arg.nr_devs))) {
369 for (size_t i = 0; i < arg.nr_devs; i++) {
370 thr->devs[i] = strndup_user((char __user *)(unsigned long) devs[i], PATH_MAX);
371 ret = PTR_ERR_OR_ZERO(thr->devs[i]);
377 char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);
379 ret = PTR_ERR_OR_ZERO(optstr) ?:
380 bch2_parse_mount_opts(NULL, &thr->opts, optstr);
387 opt_set(thr->opts, log_output, (u64)(unsigned long)&thr->output);
389 ret = run_thread_with_file(&thr->thr,
391 bch2_fsck_offline_thread_fn,
396 bch2_fsck_thread_free(thr);
397 pr_err("ret %s", bch2_err_str(ret));
403 static long bch2_global_ioctl(unsigned cmd, void __user *arg)
409 case BCH_IOCTL_ASSEMBLE:
410 return bch2_ioctl_assemble(arg);
411 case BCH_IOCTL_INCREMENTAL:
412 return bch2_ioctl_incremental(arg);
414 case BCH_IOCTL_FSCK_OFFLINE: {
415 ret = bch2_ioctl_fsck_offline(arg);
424 ret = bch2_err_class(ret);
428 static long bch2_ioctl_query_uuid(struct bch_fs *c,
429 struct bch_ioctl_query_uuid __user *user_arg)
431 return copy_to_user_errcode(&user_arg->uuid, &c->sb.user_uuid,
432 sizeof(c->sb.user_uuid));
436 static long bch2_ioctl_start(struct bch_fs *c, struct bch_ioctl_start arg)
438 if (!capable(CAP_SYS_ADMIN))
441 if (arg.flags || arg.pad)
444 return bch2_fs_start(c);
447 static long bch2_ioctl_stop(struct bch_fs *c)
449 if (!capable(CAP_SYS_ADMIN))
457 static long bch2_ioctl_disk_add(struct bch_fs *c, struct bch_ioctl_disk arg)
462 if (!capable(CAP_SYS_ADMIN))
465 if (arg.flags || arg.pad)
468 path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
469 ret = PTR_ERR_OR_ZERO(path);
473 ret = bch2_dev_add(c, path);
479 static long bch2_ioctl_disk_remove(struct bch_fs *c, struct bch_ioctl_disk arg)
483 if (!capable(CAP_SYS_ADMIN))
486 if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
487 BCH_FORCE_IF_METADATA_LOST|
488 BCH_FORCE_IF_DEGRADED|
493 ca = bch2_device_lookup(c, arg.dev, arg.flags);
497 return bch2_dev_remove(c, ca, arg.flags);
500 static long bch2_ioctl_disk_online(struct bch_fs *c, struct bch_ioctl_disk arg)
505 if (!capable(CAP_SYS_ADMIN))
508 if (arg.flags || arg.pad)
511 path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
512 ret = PTR_ERR_OR_ZERO(path);
516 ret = bch2_dev_online(c, path);
521 static long bch2_ioctl_disk_offline(struct bch_fs *c, struct bch_ioctl_disk arg)
526 if (!capable(CAP_SYS_ADMIN))
529 if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
530 BCH_FORCE_IF_METADATA_LOST|
531 BCH_FORCE_IF_DEGRADED|
536 ca = bch2_device_lookup(c, arg.dev, arg.flags);
540 ret = bch2_dev_offline(c, ca, arg.flags);
541 percpu_ref_put(&ca->ref);
545 static long bch2_ioctl_disk_set_state(struct bch_fs *c,
546 struct bch_ioctl_disk_set_state arg)
551 if (!capable(CAP_SYS_ADMIN))
554 if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
555 BCH_FORCE_IF_METADATA_LOST|
556 BCH_FORCE_IF_DEGRADED|
558 arg.pad[0] || arg.pad[1] || arg.pad[2] ||
559 arg.new_state >= BCH_MEMBER_STATE_NR)
562 ca = bch2_device_lookup(c, arg.dev, arg.flags);
566 ret = bch2_dev_set_state(c, ca, arg.new_state, arg.flags);
568 bch_err(c, "Error setting device state: %s", bch2_err_str(ret));
570 percpu_ref_put(&ca->ref);
574 struct bch_data_ctx {
575 struct thread_with_file thr;
578 struct bch_ioctl_data arg;
579 struct bch_move_stats stats;
582 static int bch2_data_thread(void *arg)
584 struct bch_data_ctx *ctx = container_of(arg, struct bch_data_ctx, thr);
586 ctx->thr.ret = bch2_data_job(ctx->c, &ctx->stats, ctx->arg);
587 ctx->stats.data_type = U8_MAX;
591 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);
595 thread_with_file_exit(&ctx->thr);
600 static ssize_t bch2_data_job_read(struct file *file, char __user *buf,
601 size_t len, loff_t *ppos)
603 struct bch_data_ctx *ctx = container_of(file->private_data, struct bch_data_ctx, thr);
604 struct bch_fs *c = ctx->c;
605 struct bch_ioctl_data_event e = {
606 .type = BCH_DATA_EVENT_PROGRESS,
607 .p.data_type = ctx->stats.data_type,
608 .p.btree_id = ctx->stats.pos.btree,
609 .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,
617 return copy_to_user_errcode(buf, &e, sizeof(e)) ?: sizeof(e);
620 static const struct file_operations bcachefs_data_ops = {
621 .release = bch2_data_job_release,
622 .read = bch2_data_job_read,
626 static long bch2_ioctl_data(struct bch_fs *c,
627 struct bch_ioctl_data arg)
629 struct bch_data_ctx *ctx;
632 if (!capable(CAP_SYS_ADMIN))
635 if (arg.op >= BCH_DATA_OP_NR || arg.flags)
638 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
645 ret = run_thread_with_file(&ctx->thr,
648 "bch-data/%s", c->name);
654 static long bch2_ioctl_fs_usage(struct bch_fs *c,
655 struct bch_ioctl_fs_usage __user *user_arg)
657 struct bch_ioctl_fs_usage *arg = NULL;
658 struct bch_replicas_usage *dst_e, *dst_end;
659 struct bch_fs_usage_online *src;
660 u32 replica_entries_bytes;
664 if (!test_bit(BCH_FS_started, &c->flags))
667 if (get_user(replica_entries_bytes, &user_arg->replica_entries_bytes))
670 arg = kzalloc(size_add(sizeof(*arg), replica_entries_bytes), GFP_KERNEL);
674 src = bch2_fs_usage_read(c);
680 arg->capacity = c->capacity;
681 arg->used = bch2_fs_sectors_used(c, src);
682 arg->online_reserved = src->online_reserved;
684 for (i = 0; i < BCH_REPLICAS_MAX; i++)
685 arg->persistent_reserved[i] = src->u.persistent_reserved[i];
687 dst_e = arg->replicas;
688 dst_end = (void *) arg->replicas + replica_entries_bytes;
690 for (i = 0; i < c->replicas.nr; i++) {
691 struct bch_replicas_entry_v1 *src_e =
692 cpu_replicas_entry(&c->replicas, i);
694 /* check that we have enough space for one replicas entry */
695 if (dst_e + 1 > dst_end) {
700 dst_e->sectors = src->u.replicas[i];
703 /* recheck after setting nr_devs: */
704 if (replicas_usage_next(dst_e) > dst_end) {
709 memcpy(dst_e->r.devs, src_e->devs, src_e->nr_devs);
711 dst_e = replicas_usage_next(dst_e);
714 arg->replica_entries_bytes = (void *) dst_e - (void *) arg->replicas;
716 percpu_up_read(&c->mark_lock);
722 ret = copy_to_user_errcode(user_arg, arg,
723 sizeof(*arg) + arg->replica_entries_bytes);
729 /* obsolete, didn't allow for new data types: */
730 static long bch2_ioctl_dev_usage(struct bch_fs *c,
731 struct bch_ioctl_dev_usage __user *user_arg)
733 struct bch_ioctl_dev_usage arg;
734 struct bch_dev_usage src;
738 if (!test_bit(BCH_FS_started, &c->flags))
741 if (copy_from_user(&arg, user_arg, sizeof(arg)))
744 if ((arg.flags & ~BCH_BY_INDEX) ||
750 ca = bch2_device_lookup(c, arg.dev, arg.flags);
754 src = bch2_dev_usage_read(ca);
756 arg.state = ca->mi.state;
757 arg.bucket_size = ca->mi.bucket_size;
758 arg.nr_buckets = ca->mi.nbuckets - ca->mi.first_bucket;
760 for (i = 0; i < BCH_DATA_NR; i++) {
761 arg.d[i].buckets = src.d[i].buckets;
762 arg.d[i].sectors = src.d[i].sectors;
763 arg.d[i].fragmented = src.d[i].fragmented;
766 percpu_ref_put(&ca->ref);
768 return copy_to_user_errcode(user_arg, &arg, sizeof(arg));
771 static long bch2_ioctl_dev_usage_v2(struct bch_fs *c,
772 struct bch_ioctl_dev_usage_v2 __user *user_arg)
774 struct bch_ioctl_dev_usage_v2 arg;
775 struct bch_dev_usage src;
779 if (!test_bit(BCH_FS_started, &c->flags))
782 if (copy_from_user(&arg, user_arg, sizeof(arg)))
785 if ((arg.flags & ~BCH_BY_INDEX) ||
791 ca = bch2_device_lookup(c, arg.dev, arg.flags);
795 src = bch2_dev_usage_read(ca);
797 arg.state = ca->mi.state;
798 arg.bucket_size = ca->mi.bucket_size;
799 arg.nr_data_types = min(arg.nr_data_types, BCH_DATA_NR);
800 arg.nr_buckets = ca->mi.nbuckets - ca->mi.first_bucket;
802 ret = copy_to_user_errcode(user_arg, &arg, sizeof(arg));
806 for (unsigned i = 0; i < arg.nr_data_types; i++) {
807 struct bch_ioctl_dev_usage_type t = {
808 .buckets = src.d[i].buckets,
809 .sectors = src.d[i].sectors,
810 .fragmented = src.d[i].fragmented,
813 ret = copy_to_user_errcode(&user_arg->d[i], &t, sizeof(t));
818 percpu_ref_put(&ca->ref);
822 static long bch2_ioctl_read_super(struct bch_fs *c,
823 struct bch_ioctl_read_super arg)
825 struct bch_dev *ca = NULL;
829 if (!capable(CAP_SYS_ADMIN))
832 if ((arg.flags & ~(BCH_BY_INDEX|BCH_READ_DEV)) ||
836 mutex_lock(&c->sb_lock);
838 if (arg.flags & BCH_READ_DEV) {
839 ca = bch2_device_lookup(c, arg.dev, arg.flags);
851 if (vstruct_bytes(sb) > arg.size) {
856 ret = copy_to_user_errcode((void __user *)(unsigned long)arg.sb, sb,
859 if (!IS_ERR_OR_NULL(ca))
860 percpu_ref_put(&ca->ref);
861 mutex_unlock(&c->sb_lock);
865 static long bch2_ioctl_disk_get_idx(struct bch_fs *c,
866 struct bch_ioctl_disk_get_idx arg)
868 dev_t dev = huge_decode_dev(arg.dev);
870 if (!capable(CAP_SYS_ADMIN))
876 for_each_online_member(c, ca)
877 if (ca->dev == dev) {
878 percpu_ref_put(&ca->io_ref);
882 return -BCH_ERR_ENOENT_dev_idx_not_found;
885 static long bch2_ioctl_disk_resize(struct bch_fs *c,
886 struct bch_ioctl_disk_resize arg)
891 if (!capable(CAP_SYS_ADMIN))
894 if ((arg.flags & ~BCH_BY_INDEX) ||
898 ca = bch2_device_lookup(c, arg.dev, arg.flags);
902 ret = bch2_dev_resize(c, ca, arg.nbuckets);
904 percpu_ref_put(&ca->ref);
908 static long bch2_ioctl_disk_resize_journal(struct bch_fs *c,
909 struct bch_ioctl_disk_resize_journal arg)
914 if (!capable(CAP_SYS_ADMIN))
917 if ((arg.flags & ~BCH_BY_INDEX) ||
921 if (arg.nbuckets > U32_MAX)
924 ca = bch2_device_lookup(c, arg.dev, arg.flags);
928 ret = bch2_set_nr_journal_buckets(c, ca, arg.nbuckets);
930 percpu_ref_put(&ca->ref);
934 static int bch2_fsck_online_thread_fn(void *arg)
936 struct fsck_thread *thr = container_of(arg, struct fsck_thread, thr);
937 struct bch_fs *c = thr->c;
939 c->output_filter = current;
940 c->output = &thr->output;
943 * XXX: can we figure out a way to do this without mucking with c->opts?
945 if (opt_defined(thr->opts, fix_errors))
946 c->opts.fix_errors = thr->opts.fix_errors;
949 c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info;
950 bch2_run_online_recovery_passes(c);
953 c->output_filter = NULL;
955 thr->thr.done = true;
956 wake_up(&thr->output.wait);
958 up(&c->online_fsck_mutex);
963 static long bch2_ioctl_fsck_online(struct bch_fs *c,
964 struct bch_ioctl_fsck_online arg)
966 struct fsck_thread *thr = NULL;
972 if (!capable(CAP_SYS_ADMIN))
975 if (!bch2_ro_ref_tryget(c))
978 if (down_trylock(&c->online_fsck_mutex)) {
983 thr = kzalloc(sizeof(*thr), GFP_KERNEL);
990 thr->opts = bch2_opts_empty();
991 thr->output.buf = PRINTBUF;
992 thr->output.buf.atomic++;
993 spin_lock_init(&thr->output.lock);
994 init_waitqueue_head(&thr->output.wait);
995 darray_init(&thr->output2);
998 char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);
1000 ret = PTR_ERR_OR_ZERO(optstr) ?:
1001 bch2_parse_mount_opts(c, &thr->opts, optstr);
1008 ret = run_thread_with_file(&thr->thr,
1010 bch2_fsck_online_thread_fn,
1016 bch2_fsck_thread_free(thr);
1017 up(&c->online_fsck_mutex);
1023 #define BCH_IOCTL(_name, _argtype) \
1027 if (copy_from_user(&i, arg, sizeof(i))) \
1029 ret = bch2_ioctl_##_name(c, i); \
1033 long bch2_fs_ioctl(struct bch_fs *c, unsigned cmd, void __user *arg)
1038 case BCH_IOCTL_QUERY_UUID:
1039 return bch2_ioctl_query_uuid(c, arg);
1040 case BCH_IOCTL_FS_USAGE:
1041 return bch2_ioctl_fs_usage(c, arg);
1042 case BCH_IOCTL_DEV_USAGE:
1043 return bch2_ioctl_dev_usage(c, arg);
1044 case BCH_IOCTL_DEV_USAGE_V2:
1045 return bch2_ioctl_dev_usage_v2(c, arg);
1047 case BCH_IOCTL_START:
1048 BCH_IOCTL(start, struct bch_ioctl_start);
1049 case BCH_IOCTL_STOP:
1050 return bch2_ioctl_stop(c);
1052 case BCH_IOCTL_READ_SUPER:
1053 BCH_IOCTL(read_super, struct bch_ioctl_read_super);
1054 case BCH_IOCTL_DISK_GET_IDX:
1055 BCH_IOCTL(disk_get_idx, struct bch_ioctl_disk_get_idx);
1058 if (!test_bit(BCH_FS_started, &c->flags))
1062 case BCH_IOCTL_DISK_ADD:
1063 BCH_IOCTL(disk_add, struct bch_ioctl_disk);
1064 case BCH_IOCTL_DISK_REMOVE:
1065 BCH_IOCTL(disk_remove, struct bch_ioctl_disk);
1066 case BCH_IOCTL_DISK_ONLINE:
1067 BCH_IOCTL(disk_online, struct bch_ioctl_disk);
1068 case BCH_IOCTL_DISK_OFFLINE:
1069 BCH_IOCTL(disk_offline, struct bch_ioctl_disk);
1070 case BCH_IOCTL_DISK_SET_STATE:
1071 BCH_IOCTL(disk_set_state, struct bch_ioctl_disk_set_state);
1072 case BCH_IOCTL_DATA:
1073 BCH_IOCTL(data, struct bch_ioctl_data);
1074 case BCH_IOCTL_DISK_RESIZE:
1075 BCH_IOCTL(disk_resize, struct bch_ioctl_disk_resize);
1076 case BCH_IOCTL_DISK_RESIZE_JOURNAL:
1077 BCH_IOCTL(disk_resize_journal, struct bch_ioctl_disk_resize_journal);
1078 case BCH_IOCTL_FSCK_ONLINE:
1079 BCH_IOCTL(fsck_online, struct bch_ioctl_fsck_online);
1085 ret = bch2_err_class(ret);
1089 static DEFINE_IDR(bch_chardev_minor);
1091 static long bch2_chardev_ioctl(struct file *filp, unsigned cmd, unsigned long v)
1093 unsigned minor = iminor(file_inode(filp));
1094 struct bch_fs *c = minor < U8_MAX ? idr_find(&bch_chardev_minor, minor) : NULL;
1095 void __user *arg = (void __user *) v;
1098 ? bch2_fs_ioctl(c, cmd, arg)
1099 : bch2_global_ioctl(cmd, arg);
1102 static const struct file_operations bch_chardev_fops = {
1103 .owner = THIS_MODULE,
1104 .unlocked_ioctl = bch2_chardev_ioctl,
1105 .open = nonseekable_open,
1108 static int bch_chardev_major;
1109 static struct class *bch_chardev_class;
1110 static struct device *bch_chardev;
1112 void bch2_fs_chardev_exit(struct bch_fs *c)
1114 if (!IS_ERR_OR_NULL(c->chardev))
1115 device_unregister(c->chardev);
1117 idr_remove(&bch_chardev_minor, c->minor);
1120 int bch2_fs_chardev_init(struct bch_fs *c)
1122 c->minor = idr_alloc(&bch_chardev_minor, c, 0, 0, GFP_KERNEL);
1126 c->chardev = device_create(bch_chardev_class, NULL,
1127 MKDEV(bch_chardev_major, c->minor), c,
1128 "bcachefs%u-ctl", c->minor);
1129 if (IS_ERR(c->chardev))
1130 return PTR_ERR(c->chardev);
1135 void bch2_chardev_exit(void)
1137 if (!IS_ERR_OR_NULL(bch_chardev_class))
1138 device_destroy(bch_chardev_class,
1139 MKDEV(bch_chardev_major, U8_MAX));
1140 if (!IS_ERR_OR_NULL(bch_chardev_class))
1141 class_destroy(bch_chardev_class);
1142 if (bch_chardev_major > 0)
1143 unregister_chrdev(bch_chardev_major, "bcachefs");
1146 int __init bch2_chardev_init(void)
1148 bch_chardev_major = register_chrdev(0, "bcachefs-ctl", &bch_chardev_fops);
1149 if (bch_chardev_major < 0)
1150 return bch_chardev_major;
1152 bch_chardev_class = class_create("bcachefs");
1153 if (IS_ERR(bch_chardev_class))
1154 return PTR_ERR(bch_chardev_class);
1156 bch_chardev = device_create(bch_chardev_class, NULL,
1157 MKDEV(bch_chardev_major, U8_MAX),
1158 NULL, "bcachefs-ctl");
1159 if (IS_ERR(bch_chardev))
1160 return PTR_ERR(bch_chardev);
1165 #endif /* NO_BCACHEFS_CHARDEV */