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], sizeof(user_arg->devs[0]) * arg.nr_devs)) {
368 for (size_t i = 0; i < arg.nr_devs; i++) {
369 thr->devs[i] = strndup_user((char __user *)(unsigned long) devs[i], PATH_MAX);
370 ret = PTR_ERR_OR_ZERO(thr->devs[i]);
376 char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);
378 ret = PTR_ERR_OR_ZERO(optstr) ?:
379 bch2_parse_mount_opts(NULL, &thr->opts, optstr);
386 opt_set(thr->opts, log_output, (u64)(unsigned long)&thr->output);
388 ret = run_thread_with_file(&thr->thr,
390 bch2_fsck_offline_thread_fn,
395 bch2_fsck_thread_free(thr);
396 pr_err("ret %s", bch2_err_str(ret));
402 static long bch2_global_ioctl(unsigned cmd, void __user *arg)
408 case BCH_IOCTL_ASSEMBLE:
409 return bch2_ioctl_assemble(arg);
410 case BCH_IOCTL_INCREMENTAL:
411 return bch2_ioctl_incremental(arg);
413 case BCH_IOCTL_FSCK_OFFLINE: {
414 ret = bch2_ioctl_fsck_offline(arg);
423 ret = bch2_err_class(ret);
427 static long bch2_ioctl_query_uuid(struct bch_fs *c,
428 struct bch_ioctl_query_uuid __user *user_arg)
430 return copy_to_user_errcode(&user_arg->uuid, &c->sb.user_uuid,
431 sizeof(c->sb.user_uuid));
435 static long bch2_ioctl_start(struct bch_fs *c, struct bch_ioctl_start arg)
437 if (!capable(CAP_SYS_ADMIN))
440 if (arg.flags || arg.pad)
443 return bch2_fs_start(c);
446 static long bch2_ioctl_stop(struct bch_fs *c)
448 if (!capable(CAP_SYS_ADMIN))
456 static long bch2_ioctl_disk_add(struct bch_fs *c, struct bch_ioctl_disk arg)
461 if (!capable(CAP_SYS_ADMIN))
464 if (arg.flags || arg.pad)
467 path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
468 ret = PTR_ERR_OR_ZERO(path);
472 ret = bch2_dev_add(c, path);
478 static long bch2_ioctl_disk_remove(struct bch_fs *c, struct bch_ioctl_disk arg)
482 if (!capable(CAP_SYS_ADMIN))
485 if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
486 BCH_FORCE_IF_METADATA_LOST|
487 BCH_FORCE_IF_DEGRADED|
492 ca = bch2_device_lookup(c, arg.dev, arg.flags);
496 return bch2_dev_remove(c, ca, arg.flags);
499 static long bch2_ioctl_disk_online(struct bch_fs *c, struct bch_ioctl_disk arg)
504 if (!capable(CAP_SYS_ADMIN))
507 if (arg.flags || arg.pad)
510 path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
511 ret = PTR_ERR_OR_ZERO(path);
515 ret = bch2_dev_online(c, path);
520 static long bch2_ioctl_disk_offline(struct bch_fs *c, struct bch_ioctl_disk arg)
525 if (!capable(CAP_SYS_ADMIN))
528 if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
529 BCH_FORCE_IF_METADATA_LOST|
530 BCH_FORCE_IF_DEGRADED|
535 ca = bch2_device_lookup(c, arg.dev, arg.flags);
539 ret = bch2_dev_offline(c, ca, arg.flags);
540 percpu_ref_put(&ca->ref);
544 static long bch2_ioctl_disk_set_state(struct bch_fs *c,
545 struct bch_ioctl_disk_set_state arg)
550 if (!capable(CAP_SYS_ADMIN))
553 if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
554 BCH_FORCE_IF_METADATA_LOST|
555 BCH_FORCE_IF_DEGRADED|
557 arg.pad[0] || arg.pad[1] || arg.pad[2] ||
558 arg.new_state >= BCH_MEMBER_STATE_NR)
561 ca = bch2_device_lookup(c, arg.dev, arg.flags);
565 ret = bch2_dev_set_state(c, ca, arg.new_state, arg.flags);
567 bch_err(c, "Error setting device state: %s", bch2_err_str(ret));
569 percpu_ref_put(&ca->ref);
573 struct bch_data_ctx {
574 struct thread_with_file thr;
577 struct bch_ioctl_data arg;
578 struct bch_move_stats stats;
581 static int bch2_data_thread(void *arg)
583 struct bch_data_ctx *ctx = container_of(arg, struct bch_data_ctx, thr);
585 ctx->thr.ret = bch2_data_job(ctx->c, &ctx->stats, ctx->arg);
586 ctx->stats.data_type = U8_MAX;
590 static int bch2_data_job_release(struct inode *inode, struct file *file)
592 struct bch_data_ctx *ctx = container_of(file->private_data, struct bch_data_ctx, thr);
594 thread_with_file_exit(&ctx->thr);
599 static ssize_t bch2_data_job_read(struct file *file, char __user *buf,
600 size_t len, loff_t *ppos)
602 struct bch_data_ctx *ctx = container_of(file->private_data, struct bch_data_ctx, thr);
603 struct bch_fs *c = ctx->c;
604 struct bch_ioctl_data_event e = {
605 .type = BCH_DATA_EVENT_PROGRESS,
606 .p.data_type = ctx->stats.data_type,
607 .p.btree_id = ctx->stats.pos.btree,
608 .p.pos = ctx->stats.pos.pos,
609 .p.sectors_done = atomic64_read(&ctx->stats.sectors_seen),
610 .p.sectors_total = bch2_fs_usage_read_short(c).used,
616 return copy_to_user_errcode(buf, &e, sizeof(e)) ?: sizeof(e);
619 static const struct file_operations bcachefs_data_ops = {
620 .release = bch2_data_job_release,
621 .read = bch2_data_job_read,
625 static long bch2_ioctl_data(struct bch_fs *c,
626 struct bch_ioctl_data arg)
628 struct bch_data_ctx *ctx;
631 if (!capable(CAP_SYS_ADMIN))
634 if (arg.op >= BCH_DATA_OP_NR || arg.flags)
637 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
644 ret = run_thread_with_file(&ctx->thr,
647 "bch-data/%s", c->name);
653 static long bch2_ioctl_fs_usage(struct bch_fs *c,
654 struct bch_ioctl_fs_usage __user *user_arg)
656 struct bch_ioctl_fs_usage *arg = NULL;
657 struct bch_replicas_usage *dst_e, *dst_end;
658 struct bch_fs_usage_online *src;
659 u32 replica_entries_bytes;
663 if (!test_bit(BCH_FS_started, &c->flags))
666 if (get_user(replica_entries_bytes, &user_arg->replica_entries_bytes))
669 arg = kzalloc(size_add(sizeof(*arg), replica_entries_bytes), GFP_KERNEL);
673 src = bch2_fs_usage_read(c);
679 arg->capacity = c->capacity;
680 arg->used = bch2_fs_sectors_used(c, src);
681 arg->online_reserved = src->online_reserved;
683 for (i = 0; i < BCH_REPLICAS_MAX; i++)
684 arg->persistent_reserved[i] = src->u.persistent_reserved[i];
686 dst_e = arg->replicas;
687 dst_end = (void *) arg->replicas + replica_entries_bytes;
689 for (i = 0; i < c->replicas.nr; i++) {
690 struct bch_replicas_entry_v1 *src_e =
691 cpu_replicas_entry(&c->replicas, i);
693 /* check that we have enough space for one replicas entry */
694 if (dst_e + 1 > dst_end) {
699 dst_e->sectors = src->u.replicas[i];
702 /* recheck after setting nr_devs: */
703 if (replicas_usage_next(dst_e) > dst_end) {
708 memcpy(dst_e->r.devs, src_e->devs, src_e->nr_devs);
710 dst_e = replicas_usage_next(dst_e);
713 arg->replica_entries_bytes = (void *) dst_e - (void *) arg->replicas;
715 percpu_up_read(&c->mark_lock);
721 ret = copy_to_user_errcode(user_arg, arg,
722 sizeof(*arg) + arg->replica_entries_bytes);
728 /* obsolete, didn't allow for new data types: */
729 static long bch2_ioctl_dev_usage(struct bch_fs *c,
730 struct bch_ioctl_dev_usage __user *user_arg)
732 struct bch_ioctl_dev_usage arg;
733 struct bch_dev_usage src;
737 if (!test_bit(BCH_FS_started, &c->flags))
740 if (copy_from_user(&arg, user_arg, sizeof(arg)))
743 if ((arg.flags & ~BCH_BY_INDEX) ||
749 ca = bch2_device_lookup(c, arg.dev, arg.flags);
753 src = bch2_dev_usage_read(ca);
755 arg.state = ca->mi.state;
756 arg.bucket_size = ca->mi.bucket_size;
757 arg.nr_buckets = ca->mi.nbuckets - ca->mi.first_bucket;
759 for (i = 0; i < BCH_DATA_NR; i++) {
760 arg.d[i].buckets = src.d[i].buckets;
761 arg.d[i].sectors = src.d[i].sectors;
762 arg.d[i].fragmented = src.d[i].fragmented;
765 percpu_ref_put(&ca->ref);
767 return copy_to_user_errcode(user_arg, &arg, sizeof(arg));
770 static long bch2_ioctl_dev_usage_v2(struct bch_fs *c,
771 struct bch_ioctl_dev_usage_v2 __user *user_arg)
773 struct bch_ioctl_dev_usage_v2 arg;
774 struct bch_dev_usage src;
778 if (!test_bit(BCH_FS_started, &c->flags))
781 if (copy_from_user(&arg, user_arg, sizeof(arg)))
784 if ((arg.flags & ~BCH_BY_INDEX) ||
790 ca = bch2_device_lookup(c, arg.dev, arg.flags);
794 src = bch2_dev_usage_read(ca);
796 arg.state = ca->mi.state;
797 arg.bucket_size = ca->mi.bucket_size;
798 arg.nr_data_types = min(arg.nr_data_types, BCH_DATA_NR);
799 arg.nr_buckets = ca->mi.nbuckets - ca->mi.first_bucket;
801 ret = copy_to_user_errcode(user_arg, &arg, sizeof(arg));
805 for (unsigned i = 0; i < arg.nr_data_types; i++) {
806 struct bch_ioctl_dev_usage_type t = {
807 .buckets = src.d[i].buckets,
808 .sectors = src.d[i].sectors,
809 .fragmented = src.d[i].fragmented,
812 ret = copy_to_user_errcode(&user_arg->d[i], &t, sizeof(t));
817 percpu_ref_put(&ca->ref);
821 static long bch2_ioctl_read_super(struct bch_fs *c,
822 struct bch_ioctl_read_super arg)
824 struct bch_dev *ca = NULL;
828 if (!capable(CAP_SYS_ADMIN))
831 if ((arg.flags & ~(BCH_BY_INDEX|BCH_READ_DEV)) ||
835 mutex_lock(&c->sb_lock);
837 if (arg.flags & BCH_READ_DEV) {
838 ca = bch2_device_lookup(c, arg.dev, arg.flags);
850 if (vstruct_bytes(sb) > arg.size) {
855 ret = copy_to_user_errcode((void __user *)(unsigned long)arg.sb, sb,
858 if (!IS_ERR_OR_NULL(ca))
859 percpu_ref_put(&ca->ref);
860 mutex_unlock(&c->sb_lock);
864 static long bch2_ioctl_disk_get_idx(struct bch_fs *c,
865 struct bch_ioctl_disk_get_idx arg)
867 dev_t dev = huge_decode_dev(arg.dev);
871 if (!capable(CAP_SYS_ADMIN))
877 for_each_online_member(ca, c, i)
878 if (ca->dev == dev) {
879 percpu_ref_put(&ca->io_ref);
883 return -BCH_ERR_ENOENT_dev_idx_not_found;
886 static long bch2_ioctl_disk_resize(struct bch_fs *c,
887 struct bch_ioctl_disk_resize arg)
892 if (!capable(CAP_SYS_ADMIN))
895 if ((arg.flags & ~BCH_BY_INDEX) ||
899 ca = bch2_device_lookup(c, arg.dev, arg.flags);
903 ret = bch2_dev_resize(c, ca, arg.nbuckets);
905 percpu_ref_put(&ca->ref);
909 static long bch2_ioctl_disk_resize_journal(struct bch_fs *c,
910 struct bch_ioctl_disk_resize_journal arg)
915 if (!capable(CAP_SYS_ADMIN))
918 if ((arg.flags & ~BCH_BY_INDEX) ||
922 if (arg.nbuckets > U32_MAX)
925 ca = bch2_device_lookup(c, arg.dev, arg.flags);
929 ret = bch2_set_nr_journal_buckets(c, ca, arg.nbuckets);
931 percpu_ref_put(&ca->ref);
935 static int bch2_fsck_online_thread_fn(void *arg)
937 struct fsck_thread *thr = container_of(arg, struct fsck_thread, thr);
938 struct bch_fs *c = thr->c;
940 c->output_filter = current;
941 c->output = &thr->output;
944 * XXX: can we figure out a way to do this without mucking with c->opts?
946 if (opt_defined(thr->opts, fix_errors))
947 c->opts.fix_errors = thr->opts.fix_errors;
950 c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info;
951 bch2_run_online_recovery_passes(c);
954 c->output_filter = NULL;
956 thr->thr.done = true;
957 wake_up(&thr->output.wait);
959 up(&c->online_fsck_mutex);
964 static long bch2_ioctl_fsck_online(struct bch_fs *c,
965 struct bch_ioctl_fsck_online arg)
967 struct fsck_thread *thr = NULL;
973 if (!capable(CAP_SYS_ADMIN))
976 if (!bch2_ro_ref_tryget(c))
979 if (down_trylock(&c->online_fsck_mutex)) {
984 thr = kzalloc(sizeof(*thr), GFP_KERNEL);
991 thr->opts = bch2_opts_empty();
992 thr->output.buf = PRINTBUF;
993 thr->output.buf.atomic++;
994 spin_lock_init(&thr->output.lock);
995 init_waitqueue_head(&thr->output.wait);
996 darray_init(&thr->output2);
999 char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);
1001 ret = PTR_ERR_OR_ZERO(optstr) ?:
1002 bch2_parse_mount_opts(c, &thr->opts, optstr);
1009 ret = run_thread_with_file(&thr->thr,
1011 bch2_fsck_online_thread_fn,
1017 bch2_fsck_thread_free(thr);
1018 up(&c->online_fsck_mutex);
1024 #define BCH_IOCTL(_name, _argtype) \
1028 if (copy_from_user(&i, arg, sizeof(i))) \
1030 ret = bch2_ioctl_##_name(c, i); \
1034 long bch2_fs_ioctl(struct bch_fs *c, unsigned cmd, void __user *arg)
1039 case BCH_IOCTL_QUERY_UUID:
1040 return bch2_ioctl_query_uuid(c, arg);
1041 case BCH_IOCTL_FS_USAGE:
1042 return bch2_ioctl_fs_usage(c, arg);
1043 case BCH_IOCTL_DEV_USAGE:
1044 return bch2_ioctl_dev_usage(c, arg);
1045 case BCH_IOCTL_DEV_USAGE_V2:
1046 return bch2_ioctl_dev_usage_v2(c, arg);
1048 case BCH_IOCTL_START:
1049 BCH_IOCTL(start, struct bch_ioctl_start);
1050 case BCH_IOCTL_STOP:
1051 return bch2_ioctl_stop(c);
1053 case BCH_IOCTL_READ_SUPER:
1054 BCH_IOCTL(read_super, struct bch_ioctl_read_super);
1055 case BCH_IOCTL_DISK_GET_IDX:
1056 BCH_IOCTL(disk_get_idx, struct bch_ioctl_disk_get_idx);
1059 if (!test_bit(BCH_FS_started, &c->flags))
1063 case BCH_IOCTL_DISK_ADD:
1064 BCH_IOCTL(disk_add, struct bch_ioctl_disk);
1065 case BCH_IOCTL_DISK_REMOVE:
1066 BCH_IOCTL(disk_remove, struct bch_ioctl_disk);
1067 case BCH_IOCTL_DISK_ONLINE:
1068 BCH_IOCTL(disk_online, struct bch_ioctl_disk);
1069 case BCH_IOCTL_DISK_OFFLINE:
1070 BCH_IOCTL(disk_offline, struct bch_ioctl_disk);
1071 case BCH_IOCTL_DISK_SET_STATE:
1072 BCH_IOCTL(disk_set_state, struct bch_ioctl_disk_set_state);
1073 case BCH_IOCTL_DATA:
1074 BCH_IOCTL(data, struct bch_ioctl_data);
1075 case BCH_IOCTL_DISK_RESIZE:
1076 BCH_IOCTL(disk_resize, struct bch_ioctl_disk_resize);
1077 case BCH_IOCTL_DISK_RESIZE_JOURNAL:
1078 BCH_IOCTL(disk_resize_journal, struct bch_ioctl_disk_resize_journal);
1079 case BCH_IOCTL_FSCK_ONLINE:
1080 BCH_IOCTL(fsck_online, struct bch_ioctl_fsck_online);
1086 ret = bch2_err_class(ret);
1090 static DEFINE_IDR(bch_chardev_minor);
1092 static long bch2_chardev_ioctl(struct file *filp, unsigned cmd, unsigned long v)
1094 unsigned minor = iminor(file_inode(filp));
1095 struct bch_fs *c = minor < U8_MAX ? idr_find(&bch_chardev_minor, minor) : NULL;
1096 void __user *arg = (void __user *) v;
1099 ? bch2_fs_ioctl(c, cmd, arg)
1100 : bch2_global_ioctl(cmd, arg);
1103 static const struct file_operations bch_chardev_fops = {
1104 .owner = THIS_MODULE,
1105 .unlocked_ioctl = bch2_chardev_ioctl,
1106 .open = nonseekable_open,
1109 static int bch_chardev_major;
1110 static struct class *bch_chardev_class;
1111 static struct device *bch_chardev;
1113 void bch2_fs_chardev_exit(struct bch_fs *c)
1115 if (!IS_ERR_OR_NULL(c->chardev))
1116 device_unregister(c->chardev);
1118 idr_remove(&bch_chardev_minor, c->minor);
1121 int bch2_fs_chardev_init(struct bch_fs *c)
1123 c->minor = idr_alloc(&bch_chardev_minor, c, 0, 0, GFP_KERNEL);
1127 c->chardev = device_create(bch_chardev_class, NULL,
1128 MKDEV(bch_chardev_major, c->minor), c,
1129 "bcachefs%u-ctl", c->minor);
1130 if (IS_ERR(c->chardev))
1131 return PTR_ERR(c->chardev);
1136 void bch2_chardev_exit(void)
1138 if (!IS_ERR_OR_NULL(bch_chardev_class))
1139 device_destroy(bch_chardev_class,
1140 MKDEV(bch_chardev_major, U8_MAX));
1141 if (!IS_ERR_OR_NULL(bch_chardev_class))
1142 class_destroy(bch_chardev_class);
1143 if (bch_chardev_major > 0)
1144 unregister_chrdev(bch_chardev_major, "bcachefs");
1147 int __init bch2_chardev_init(void)
1149 bch_chardev_major = register_chrdev(0, "bcachefs-ctl", &bch_chardev_fops);
1150 if (bch_chardev_major < 0)
1151 return bch_chardev_major;
1153 bch_chardev_class = class_create("bcachefs");
1154 if (IS_ERR(bch_chardev_class))
1155 return PTR_ERR(bch_chardev_class);
1157 bch_chardev = device_create(bch_chardev_class, NULL,
1158 MKDEV(bch_chardev_major, U8_MAX),
1159 NULL, "bcachefs-ctl");
1160 if (IS_ERR(bch_chardev))
1161 return PTR_ERR(bch_chardev);
1166 #endif /* NO_BCACHEFS_CHARDEV */