]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/chardev.c
Disable pristine-tar option in gbp.conf, since there is no pristine-tar branch.
[bcachefs-tools-debian] / libbcachefs / chardev.c
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef NO_BCACHEFS_CHARDEV
3
4 #include "bcachefs.h"
5 #include "bcachefs_ioctl.h"
6 #include "buckets.h"
7 #include "chardev.h"
8 #include "journal.h"
9 #include "move.h"
10 #include "recovery.h"
11 #include "replicas.h"
12 #include "super.h"
13 #include "super-io.h"
14
15 #include <linux/cdev.h>
16 #include <linux/device.h>
17 #include <linux/fs.h>
18 #include <linux/ioctl.h>
19 #include <linux/major.h>
20 #include <linux/sched/task.h>
21 #include <linux/slab.h>
22 #include <linux/thread_with_file.h>
23 #include <linux/uaccess.h>
24
25 __must_check
26 static int copy_to_user_errcode(void __user *to, const void *from, unsigned long n)
27 {
28         return copy_to_user(to, from, n) ? -EFAULT : 0;
29 }
30
31 /* returns with ref on ca->ref */
32 static struct bch_dev *bch2_device_lookup(struct bch_fs *c, u64 dev,
33                                           unsigned flags)
34 {
35         struct bch_dev *ca;
36
37         if (flags & BCH_BY_INDEX) {
38                 if (dev >= c->sb.nr_devices)
39                         return ERR_PTR(-EINVAL);
40
41                 rcu_read_lock();
42                 ca = rcu_dereference(c->devs[dev]);
43                 if (ca)
44                         percpu_ref_get(&ca->ref);
45                 rcu_read_unlock();
46
47                 if (!ca)
48                         return ERR_PTR(-EINVAL);
49         } else {
50                 char *path;
51
52                 path = strndup_user((const char __user *)
53                                     (unsigned long) dev, PATH_MAX);
54                 if (IS_ERR(path))
55                         return ERR_CAST(path);
56
57                 ca = bch2_dev_lookup(c, path);
58                 kfree(path);
59         }
60
61         return ca;
62 }
63
64 #if 0
65 static long bch2_ioctl_assemble(struct bch_ioctl_assemble __user *user_arg)
66 {
67         struct bch_ioctl_assemble arg;
68         struct bch_fs *c;
69         u64 *user_devs = NULL;
70         char **devs = NULL;
71         unsigned i;
72         int ret = -EFAULT;
73
74         if (copy_from_user(&arg, user_arg, sizeof(arg)))
75                 return -EFAULT;
76
77         if (arg.flags || arg.pad)
78                 return -EINVAL;
79
80         user_devs = kmalloc_array(arg.nr_devs, sizeof(u64), GFP_KERNEL);
81         if (!user_devs)
82                 return -ENOMEM;
83
84         devs = kcalloc(arg.nr_devs, sizeof(char *), GFP_KERNEL);
85
86         if (copy_from_user(user_devs, user_arg->devs,
87                            sizeof(u64) * arg.nr_devs))
88                 goto err;
89
90         for (i = 0; i < arg.nr_devs; i++) {
91                 devs[i] = strndup_user((const char __user *)(unsigned long)
92                                        user_devs[i],
93                                        PATH_MAX);
94                 ret= PTR_ERR_OR_ZERO(devs[i]);
95                 if (ret)
96                         goto err;
97         }
98
99         c = bch2_fs_open(devs, arg.nr_devs, bch2_opts_empty());
100         ret = PTR_ERR_OR_ZERO(c);
101         if (!ret)
102                 closure_put(&c->cl);
103 err:
104         if (devs)
105                 for (i = 0; i < arg.nr_devs; i++)
106                         kfree(devs[i]);
107         kfree(devs);
108         return ret;
109 }
110
111 static long bch2_ioctl_incremental(struct bch_ioctl_incremental __user *user_arg)
112 {
113         struct bch_ioctl_incremental arg;
114         const char *err;
115         char *path;
116
117         if (copy_from_user(&arg, user_arg, sizeof(arg)))
118                 return -EFAULT;
119
120         if (arg.flags || arg.pad)
121                 return -EINVAL;
122
123         path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
124         ret = PTR_ERR_OR_ZERO(path);
125         if (ret)
126                 return ret;
127
128         err = bch2_fs_open_incremental(path);
129         kfree(path);
130
131         if (err) {
132                 pr_err("Could not register bcachefs devices: %s", err);
133                 return -EINVAL;
134         }
135
136         return 0;
137 }
138 #endif
139
140 struct fsck_thread {
141         struct thread_with_stdio thr;
142         struct bch_fs           *c;
143         char                    **devs;
144         size_t                  nr_devs;
145         struct bch_opts         opts;
146 };
147
148 static void bch2_fsck_thread_exit(struct thread_with_stdio *_thr)
149 {
150         struct fsck_thread *thr = container_of(_thr, struct fsck_thread, thr);
151         if (thr->devs)
152                 for (size_t i = 0; i < thr->nr_devs; i++)
153                         kfree(thr->devs[i]);
154         kfree(thr->devs);
155         kfree(thr);
156 }
157
158 static int bch2_fsck_offline_thread_fn(struct thread_with_stdio *stdio)
159 {
160         struct fsck_thread *thr = container_of(stdio, struct fsck_thread, thr);
161         struct bch_fs *c = bch2_fs_open(thr->devs, thr->nr_devs, thr->opts);
162
163         if (IS_ERR(c))
164                 return PTR_ERR(c);
165
166         int ret = 0;
167         if (test_bit(BCH_FS_errors_fixed, &c->flags))
168                 ret |= 1;
169         if (test_bit(BCH_FS_error, &c->flags))
170                 ret |= 4;
171
172         bch2_fs_stop(c);
173
174         if (ret & 1)
175                 stdio_redirect_printf(&stdio->stdio, false, "%s: errors fixed\n", c->name);
176         if (ret & 4)
177                 stdio_redirect_printf(&stdio->stdio, false, "%s: still has errors\n", c->name);
178
179         return ret;
180 }
181
182 static const struct thread_with_stdio_ops bch2_offline_fsck_ops = {
183         .exit           = bch2_fsck_thread_exit,
184         .fn             = bch2_fsck_offline_thread_fn,
185 };
186
187 static long bch2_ioctl_fsck_offline(struct bch_ioctl_fsck_offline __user *user_arg)
188 {
189         struct bch_ioctl_fsck_offline arg;
190         struct fsck_thread *thr = NULL;
191         u64 *devs = NULL;
192         long ret = 0;
193
194         if (copy_from_user(&arg, user_arg, sizeof(arg)))
195                 return -EFAULT;
196
197         if (arg.flags)
198                 return -EINVAL;
199
200         if (!capable(CAP_SYS_ADMIN))
201                 return -EPERM;
202
203         if (!(devs = kcalloc(arg.nr_devs, sizeof(*devs), GFP_KERNEL)) ||
204             !(thr = kzalloc(sizeof(*thr), GFP_KERNEL)) ||
205             !(thr->devs = kcalloc(arg.nr_devs, sizeof(*thr->devs), GFP_KERNEL))) {
206                 ret = -ENOMEM;
207                 goto err;
208         }
209
210         thr->opts = bch2_opts_empty();
211         thr->nr_devs = arg.nr_devs;
212
213         if (copy_from_user(devs, &user_arg->devs[0],
214                            array_size(sizeof(user_arg->devs[0]), arg.nr_devs))) {
215                 ret = -EINVAL;
216                 goto err;
217         }
218
219         for (size_t i = 0; i < arg.nr_devs; i++) {
220                 thr->devs[i] = strndup_user((char __user *)(unsigned long) devs[i], PATH_MAX);
221                 ret = PTR_ERR_OR_ZERO(thr->devs[i]);
222                 if (ret)
223                         goto err;
224         }
225
226         if (arg.opts) {
227                 char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);
228
229                 ret =   PTR_ERR_OR_ZERO(optstr) ?:
230                         bch2_parse_mount_opts(NULL, &thr->opts, optstr);
231                 kfree(optstr);
232
233                 if (ret)
234                         goto err;
235         }
236
237         opt_set(thr->opts, stdio, (u64)(unsigned long)&thr->thr.stdio);
238
239         ret = run_thread_with_stdio(&thr->thr, &bch2_offline_fsck_ops);
240 err:
241         if (ret < 0) {
242                 if (thr)
243                         bch2_fsck_thread_exit(&thr->thr);
244                 pr_err("ret %s", bch2_err_str(ret));
245         }
246         kfree(devs);
247         return ret;
248 }
249
250 static long bch2_global_ioctl(unsigned cmd, void __user *arg)
251 {
252         long ret;
253
254         switch (cmd) {
255 #if 0
256         case BCH_IOCTL_ASSEMBLE:
257                 return bch2_ioctl_assemble(arg);
258         case BCH_IOCTL_INCREMENTAL:
259                 return bch2_ioctl_incremental(arg);
260 #endif
261         case BCH_IOCTL_FSCK_OFFLINE: {
262                 ret = bch2_ioctl_fsck_offline(arg);
263                 break;
264         }
265         default:
266                 ret = -ENOTTY;
267                 break;
268         }
269
270         if (ret < 0)
271                 ret = bch2_err_class(ret);
272         return ret;
273 }
274
275 static long bch2_ioctl_query_uuid(struct bch_fs *c,
276                         struct bch_ioctl_query_uuid __user *user_arg)
277 {
278         return copy_to_user_errcode(&user_arg->uuid, &c->sb.user_uuid,
279                                     sizeof(c->sb.user_uuid));
280 }
281
282 #if 0
283 static long bch2_ioctl_start(struct bch_fs *c, struct bch_ioctl_start arg)
284 {
285         if (!capable(CAP_SYS_ADMIN))
286                 return -EPERM;
287
288         if (arg.flags || arg.pad)
289                 return -EINVAL;
290
291         return bch2_fs_start(c);
292 }
293
294 static long bch2_ioctl_stop(struct bch_fs *c)
295 {
296         if (!capable(CAP_SYS_ADMIN))
297                 return -EPERM;
298
299         bch2_fs_stop(c);
300         return 0;
301 }
302 #endif
303
304 static long bch2_ioctl_disk_add(struct bch_fs *c, struct bch_ioctl_disk arg)
305 {
306         char *path;
307         int ret;
308
309         if (!capable(CAP_SYS_ADMIN))
310                 return -EPERM;
311
312         if (arg.flags || arg.pad)
313                 return -EINVAL;
314
315         path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
316         ret = PTR_ERR_OR_ZERO(path);
317         if (ret)
318                 return ret;
319
320         ret = bch2_dev_add(c, path);
321         kfree(path);
322
323         return ret;
324 }
325
326 static long bch2_ioctl_disk_remove(struct bch_fs *c, struct bch_ioctl_disk arg)
327 {
328         struct bch_dev *ca;
329
330         if (!capable(CAP_SYS_ADMIN))
331                 return -EPERM;
332
333         if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
334                            BCH_FORCE_IF_METADATA_LOST|
335                            BCH_FORCE_IF_DEGRADED|
336                            BCH_BY_INDEX)) ||
337             arg.pad)
338                 return -EINVAL;
339
340         ca = bch2_device_lookup(c, arg.dev, arg.flags);
341         if (IS_ERR(ca))
342                 return PTR_ERR(ca);
343
344         return bch2_dev_remove(c, ca, arg.flags);
345 }
346
347 static long bch2_ioctl_disk_online(struct bch_fs *c, struct bch_ioctl_disk arg)
348 {
349         char *path;
350         int ret;
351
352         if (!capable(CAP_SYS_ADMIN))
353                 return -EPERM;
354
355         if (arg.flags || arg.pad)
356                 return -EINVAL;
357
358         path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX);
359         ret = PTR_ERR_OR_ZERO(path);
360         if (ret)
361                 return ret;
362
363         ret = bch2_dev_online(c, path);
364         kfree(path);
365         return ret;
366 }
367
368 static long bch2_ioctl_disk_offline(struct bch_fs *c, struct bch_ioctl_disk arg)
369 {
370         struct bch_dev *ca;
371         int ret;
372
373         if (!capable(CAP_SYS_ADMIN))
374                 return -EPERM;
375
376         if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
377                            BCH_FORCE_IF_METADATA_LOST|
378                            BCH_FORCE_IF_DEGRADED|
379                            BCH_BY_INDEX)) ||
380             arg.pad)
381                 return -EINVAL;
382
383         ca = bch2_device_lookup(c, arg.dev, arg.flags);
384         if (IS_ERR(ca))
385                 return PTR_ERR(ca);
386
387         ret = bch2_dev_offline(c, ca, arg.flags);
388         percpu_ref_put(&ca->ref);
389         return ret;
390 }
391
392 static long bch2_ioctl_disk_set_state(struct bch_fs *c,
393                         struct bch_ioctl_disk_set_state arg)
394 {
395         struct bch_dev *ca;
396         int ret;
397
398         if (!capable(CAP_SYS_ADMIN))
399                 return -EPERM;
400
401         if ((arg.flags & ~(BCH_FORCE_IF_DATA_LOST|
402                            BCH_FORCE_IF_METADATA_LOST|
403                            BCH_FORCE_IF_DEGRADED|
404                            BCH_BY_INDEX)) ||
405             arg.pad[0] || arg.pad[1] || arg.pad[2] ||
406             arg.new_state >= BCH_MEMBER_STATE_NR)
407                 return -EINVAL;
408
409         ca = bch2_device_lookup(c, arg.dev, arg.flags);
410         if (IS_ERR(ca))
411                 return PTR_ERR(ca);
412
413         ret = bch2_dev_set_state(c, ca, arg.new_state, arg.flags);
414         if (ret)
415                 bch_err(c, "Error setting device state: %s", bch2_err_str(ret));
416
417         percpu_ref_put(&ca->ref);
418         return ret;
419 }
420
421 struct bch_data_ctx {
422         struct thread_with_file         thr;
423
424         struct bch_fs                   *c;
425         struct bch_ioctl_data           arg;
426         struct bch_move_stats           stats;
427 };
428
429 static int bch2_data_thread(void *arg)
430 {
431         struct bch_data_ctx *ctx = container_of(arg, struct bch_data_ctx, thr);
432
433         ctx->thr.ret = bch2_data_job(ctx->c, &ctx->stats, ctx->arg);
434         ctx->stats.data_type = U8_MAX;
435         return 0;
436 }
437
438 static int bch2_data_job_release(struct inode *inode, struct file *file)
439 {
440         struct bch_data_ctx *ctx = container_of(file->private_data, struct bch_data_ctx, thr);
441
442         thread_with_file_exit(&ctx->thr);
443         kfree(ctx);
444         return 0;
445 }
446
447 static ssize_t bch2_data_job_read(struct file *file, char __user *buf,
448                                   size_t len, loff_t *ppos)
449 {
450         struct bch_data_ctx *ctx = container_of(file->private_data, struct bch_data_ctx, thr);
451         struct bch_fs *c = ctx->c;
452         struct bch_ioctl_data_event e = {
453                 .type                   = BCH_DATA_EVENT_PROGRESS,
454                 .p.data_type            = ctx->stats.data_type,
455                 .p.btree_id             = ctx->stats.pos.btree,
456                 .p.pos                  = ctx->stats.pos.pos,
457                 .p.sectors_done         = atomic64_read(&ctx->stats.sectors_seen),
458                 .p.sectors_total        = bch2_fs_usage_read_short(c).used,
459         };
460
461         if (len < sizeof(e))
462                 return -EINVAL;
463
464         return copy_to_user_errcode(buf, &e, sizeof(e)) ?: sizeof(e);
465 }
466
467 static const struct file_operations bcachefs_data_ops = {
468         .release        = bch2_data_job_release,
469         .read           = bch2_data_job_read,
470         .llseek         = no_llseek,
471 };
472
473 static long bch2_ioctl_data(struct bch_fs *c,
474                             struct bch_ioctl_data arg)
475 {
476         struct bch_data_ctx *ctx;
477         int ret;
478
479         if (!capable(CAP_SYS_ADMIN))
480                 return -EPERM;
481
482         if (arg.op >= BCH_DATA_OP_NR || arg.flags)
483                 return -EINVAL;
484
485         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
486         if (!ctx)
487                 return -ENOMEM;
488
489         ctx->c = c;
490         ctx->arg = arg;
491
492         ret = run_thread_with_file(&ctx->thr,
493                         &bcachefs_data_ops,
494                         bch2_data_thread);
495         if (ret < 0)
496                 kfree(ctx);
497         return ret;
498 }
499
500 static long bch2_ioctl_fs_usage(struct bch_fs *c,
501                                 struct bch_ioctl_fs_usage __user *user_arg)
502 {
503         struct bch_ioctl_fs_usage *arg = NULL;
504         struct bch_replicas_usage *dst_e, *dst_end;
505         struct bch_fs_usage_online *src;
506         u32 replica_entries_bytes;
507         unsigned i;
508         int ret = 0;
509
510         if (!test_bit(BCH_FS_started, &c->flags))
511                 return -EINVAL;
512
513         if (get_user(replica_entries_bytes, &user_arg->replica_entries_bytes))
514                 return -EFAULT;
515
516         arg = kzalloc(size_add(sizeof(*arg), replica_entries_bytes), GFP_KERNEL);
517         if (!arg)
518                 return -ENOMEM;
519
520         src = bch2_fs_usage_read(c);
521         if (!src) {
522                 ret = -ENOMEM;
523                 goto err;
524         }
525
526         arg->capacity           = c->capacity;
527         arg->used               = bch2_fs_sectors_used(c, src);
528         arg->online_reserved    = src->online_reserved;
529
530         for (i = 0; i < BCH_REPLICAS_MAX; i++)
531                 arg->persistent_reserved[i] = src->u.persistent_reserved[i];
532
533         dst_e   = arg->replicas;
534         dst_end = (void *) arg->replicas + replica_entries_bytes;
535
536         for (i = 0; i < c->replicas.nr; i++) {
537                 struct bch_replicas_entry_v1 *src_e =
538                         cpu_replicas_entry(&c->replicas, i);
539
540                 /* check that we have enough space for one replicas entry */
541                 if (dst_e + 1 > dst_end) {
542                         ret = -ERANGE;
543                         break;
544                 }
545
546                 dst_e->sectors          = src->u.replicas[i];
547                 dst_e->r                = *src_e;
548
549                 /* recheck after setting nr_devs: */
550                 if (replicas_usage_next(dst_e) > dst_end) {
551                         ret = -ERANGE;
552                         break;
553                 }
554
555                 memcpy(dst_e->r.devs, src_e->devs, src_e->nr_devs);
556
557                 dst_e = replicas_usage_next(dst_e);
558         }
559
560         arg->replica_entries_bytes = (void *) dst_e - (void *) arg->replicas;
561
562         percpu_up_read(&c->mark_lock);
563         kfree(src);
564
565         if (ret)
566                 goto err;
567
568         ret = copy_to_user_errcode(user_arg, arg,
569                         sizeof(*arg) + arg->replica_entries_bytes);
570 err:
571         kfree(arg);
572         return ret;
573 }
574
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)
578 {
579         struct bch_ioctl_dev_usage arg;
580         struct bch_dev_usage src;
581         struct bch_dev *ca;
582         unsigned i;
583
584         if (!test_bit(BCH_FS_started, &c->flags))
585                 return -EINVAL;
586
587         if (copy_from_user(&arg, user_arg, sizeof(arg)))
588                 return -EFAULT;
589
590         if ((arg.flags & ~BCH_BY_INDEX) ||
591             arg.pad[0] ||
592             arg.pad[1] ||
593             arg.pad[2])
594                 return -EINVAL;
595
596         ca = bch2_device_lookup(c, arg.dev, arg.flags);
597         if (IS_ERR(ca))
598                 return PTR_ERR(ca);
599
600         src = bch2_dev_usage_read(ca);
601
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;
605
606         for (i = 0; i < BCH_DATA_NR; 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;
610         }
611
612         percpu_ref_put(&ca->ref);
613
614         return copy_to_user_errcode(user_arg, &arg, sizeof(arg));
615 }
616
617 static long bch2_ioctl_dev_usage_v2(struct bch_fs *c,
618                                  struct bch_ioctl_dev_usage_v2 __user *user_arg)
619 {
620         struct bch_ioctl_dev_usage_v2 arg;
621         struct bch_dev_usage src;
622         struct bch_dev *ca;
623         int ret = 0;
624
625         if (!test_bit(BCH_FS_started, &c->flags))
626                 return -EINVAL;
627
628         if (copy_from_user(&arg, user_arg, sizeof(arg)))
629                 return -EFAULT;
630
631         if ((arg.flags & ~BCH_BY_INDEX) ||
632             arg.pad[0] ||
633             arg.pad[1] ||
634             arg.pad[2])
635                 return -EINVAL;
636
637         ca = bch2_device_lookup(c, arg.dev, arg.flags);
638         if (IS_ERR(ca))
639                 return PTR_ERR(ca);
640
641         src = bch2_dev_usage_read(ca);
642
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;
647
648         ret = copy_to_user_errcode(user_arg, &arg, sizeof(arg));
649         if (ret)
650                 goto err;
651
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,
657                 };
658
659                 ret = copy_to_user_errcode(&user_arg->d[i], &t, sizeof(t));
660                 if (ret)
661                         goto err;
662         }
663 err:
664         percpu_ref_put(&ca->ref);
665         return ret;
666 }
667
668 static long bch2_ioctl_read_super(struct bch_fs *c,
669                                   struct bch_ioctl_read_super arg)
670 {
671         struct bch_dev *ca = NULL;
672         struct bch_sb *sb;
673         int ret = 0;
674
675         if (!capable(CAP_SYS_ADMIN))
676                 return -EPERM;
677
678         if ((arg.flags & ~(BCH_BY_INDEX|BCH_READ_DEV)) ||
679             arg.pad)
680                 return -EINVAL;
681
682         mutex_lock(&c->sb_lock);
683
684         if (arg.flags & BCH_READ_DEV) {
685                 ca = bch2_device_lookup(c, arg.dev, arg.flags);
686
687                 if (IS_ERR(ca)) {
688                         ret = PTR_ERR(ca);
689                         goto err;
690                 }
691
692                 sb = ca->disk_sb.sb;
693         } else {
694                 sb = c->disk_sb.sb;
695         }
696
697         if (vstruct_bytes(sb) > arg.size) {
698                 ret = -ERANGE;
699                 goto err;
700         }
701
702         ret = copy_to_user_errcode((void __user *)(unsigned long)arg.sb, sb,
703                                    vstruct_bytes(sb));
704 err:
705         if (!IS_ERR_OR_NULL(ca))
706                 percpu_ref_put(&ca->ref);
707         mutex_unlock(&c->sb_lock);
708         return ret;
709 }
710
711 static long bch2_ioctl_disk_get_idx(struct bch_fs *c,
712                                     struct bch_ioctl_disk_get_idx arg)
713 {
714         dev_t dev = huge_decode_dev(arg.dev);
715
716         if (!capable(CAP_SYS_ADMIN))
717                 return -EPERM;
718
719         if (!dev)
720                 return -EINVAL;
721
722         for_each_online_member(c, ca)
723                 if (ca->dev == dev) {
724                         percpu_ref_put(&ca->io_ref);
725                         return ca->dev_idx;
726                 }
727
728         return -BCH_ERR_ENOENT_dev_idx_not_found;
729 }
730
731 static long bch2_ioctl_disk_resize(struct bch_fs *c,
732                                    struct bch_ioctl_disk_resize arg)
733 {
734         struct bch_dev *ca;
735         int ret;
736
737         if (!capable(CAP_SYS_ADMIN))
738                 return -EPERM;
739
740         if ((arg.flags & ~BCH_BY_INDEX) ||
741             arg.pad)
742                 return -EINVAL;
743
744         ca = bch2_device_lookup(c, arg.dev, arg.flags);
745         if (IS_ERR(ca))
746                 return PTR_ERR(ca);
747
748         ret = bch2_dev_resize(c, ca, arg.nbuckets);
749
750         percpu_ref_put(&ca->ref);
751         return ret;
752 }
753
754 static long bch2_ioctl_disk_resize_journal(struct bch_fs *c,
755                                    struct bch_ioctl_disk_resize_journal arg)
756 {
757         struct bch_dev *ca;
758         int ret;
759
760         if (!capable(CAP_SYS_ADMIN))
761                 return -EPERM;
762
763         if ((arg.flags & ~BCH_BY_INDEX) ||
764             arg.pad)
765                 return -EINVAL;
766
767         if (arg.nbuckets > U32_MAX)
768                 return -EINVAL;
769
770         ca = bch2_device_lookup(c, arg.dev, arg.flags);
771         if (IS_ERR(ca))
772                 return PTR_ERR(ca);
773
774         ret = bch2_set_nr_journal_buckets(c, ca, arg.nbuckets);
775
776         percpu_ref_put(&ca->ref);
777         return ret;
778 }
779
780 static int bch2_fsck_online_thread_fn(struct thread_with_stdio *stdio)
781 {
782         struct fsck_thread *thr = container_of(stdio, struct fsck_thread, thr);
783         struct bch_fs *c = thr->c;
784
785         c->stdio_filter = current;
786         c->stdio = &thr->thr.stdio;
787
788         /*
789          * XXX: can we figure out a way to do this without mucking with c->opts?
790          */
791         unsigned old_fix_errors = c->opts.fix_errors;
792         if (opt_defined(thr->opts, fix_errors))
793                 c->opts.fix_errors = thr->opts.fix_errors;
794         else
795                 c->opts.fix_errors = FSCK_FIX_ask;
796
797         c->opts.fsck = true;
798         set_bit(BCH_FS_fsck_running, &c->flags);
799
800         c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info;
801         int ret = bch2_run_online_recovery_passes(c);
802
803         clear_bit(BCH_FS_fsck_running, &c->flags);
804         bch_err_fn(c, ret);
805
806         c->stdio = NULL;
807         c->stdio_filter = NULL;
808         c->opts.fix_errors = old_fix_errors;
809
810         up(&c->online_fsck_mutex);
811         bch2_ro_ref_put(c);
812         return ret;
813 }
814
815 static const struct thread_with_stdio_ops bch2_online_fsck_ops = {
816         .exit           = bch2_fsck_thread_exit,
817         .fn             = bch2_fsck_online_thread_fn,
818 };
819
820 static long bch2_ioctl_fsck_online(struct bch_fs *c,
821                                    struct bch_ioctl_fsck_online arg)
822 {
823         struct fsck_thread *thr = NULL;
824         long ret = 0;
825
826         if (arg.flags)
827                 return -EINVAL;
828
829         if (!capable(CAP_SYS_ADMIN))
830                 return -EPERM;
831
832         if (!bch2_ro_ref_tryget(c))
833                 return -EROFS;
834
835         if (down_trylock(&c->online_fsck_mutex)) {
836                 bch2_ro_ref_put(c);
837                 return -EAGAIN;
838         }
839
840         thr = kzalloc(sizeof(*thr), GFP_KERNEL);
841         if (!thr) {
842                 ret = -ENOMEM;
843                 goto err;
844         }
845
846         thr->c = c;
847         thr->opts = bch2_opts_empty();
848
849         if (arg.opts) {
850                 char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16);
851
852                 ret =   PTR_ERR_OR_ZERO(optstr) ?:
853                         bch2_parse_mount_opts(c, &thr->opts, optstr);
854                 kfree(optstr);
855
856                 if (ret)
857                         goto err;
858         }
859
860         ret = run_thread_with_stdio(&thr->thr, &bch2_online_fsck_ops);
861 err:
862         if (ret < 0) {
863                 bch_err_fn(c, ret);
864                 if (thr)
865                         bch2_fsck_thread_exit(&thr->thr);
866                 up(&c->online_fsck_mutex);
867                 bch2_ro_ref_put(c);
868         }
869         return ret;
870 }
871
872 #define BCH_IOCTL(_name, _argtype)                                      \
873 do {                                                                    \
874         _argtype i;                                                     \
875                                                                         \
876         if (copy_from_user(&i, arg, sizeof(i)))                         \
877                 return -EFAULT;                                         \
878         ret = bch2_ioctl_##_name(c, i);                                 \
879         goto out;                                                       \
880 } while (0)
881
882 long bch2_fs_ioctl(struct bch_fs *c, unsigned cmd, void __user *arg)
883 {
884         long ret;
885
886         switch (cmd) {
887         case BCH_IOCTL_QUERY_UUID:
888                 return bch2_ioctl_query_uuid(c, arg);
889         case BCH_IOCTL_FS_USAGE:
890                 return bch2_ioctl_fs_usage(c, arg);
891         case BCH_IOCTL_DEV_USAGE:
892                 return bch2_ioctl_dev_usage(c, arg);
893         case BCH_IOCTL_DEV_USAGE_V2:
894                 return bch2_ioctl_dev_usage_v2(c, arg);
895 #if 0
896         case BCH_IOCTL_START:
897                 BCH_IOCTL(start, struct bch_ioctl_start);
898         case BCH_IOCTL_STOP:
899                 return bch2_ioctl_stop(c);
900 #endif
901         case BCH_IOCTL_READ_SUPER:
902                 BCH_IOCTL(read_super, struct bch_ioctl_read_super);
903         case BCH_IOCTL_DISK_GET_IDX:
904                 BCH_IOCTL(disk_get_idx, struct bch_ioctl_disk_get_idx);
905         }
906
907         if (!test_bit(BCH_FS_started, &c->flags))
908                 return -EINVAL;
909
910         switch (cmd) {
911         case BCH_IOCTL_DISK_ADD:
912                 BCH_IOCTL(disk_add, struct bch_ioctl_disk);
913         case BCH_IOCTL_DISK_REMOVE:
914                 BCH_IOCTL(disk_remove, struct bch_ioctl_disk);
915         case BCH_IOCTL_DISK_ONLINE:
916                 BCH_IOCTL(disk_online, struct bch_ioctl_disk);
917         case BCH_IOCTL_DISK_OFFLINE:
918                 BCH_IOCTL(disk_offline, struct bch_ioctl_disk);
919         case BCH_IOCTL_DISK_SET_STATE:
920                 BCH_IOCTL(disk_set_state, struct bch_ioctl_disk_set_state);
921         case BCH_IOCTL_DATA:
922                 BCH_IOCTL(data, struct bch_ioctl_data);
923         case BCH_IOCTL_DISK_RESIZE:
924                 BCH_IOCTL(disk_resize, struct bch_ioctl_disk_resize);
925         case BCH_IOCTL_DISK_RESIZE_JOURNAL:
926                 BCH_IOCTL(disk_resize_journal, struct bch_ioctl_disk_resize_journal);
927         case BCH_IOCTL_FSCK_ONLINE:
928                 BCH_IOCTL(fsck_online, struct bch_ioctl_fsck_online);
929         default:
930                 return -ENOTTY;
931         }
932 out:
933         if (ret < 0)
934                 ret = bch2_err_class(ret);
935         return ret;
936 }
937
938 static DEFINE_IDR(bch_chardev_minor);
939
940 static long bch2_chardev_ioctl(struct file *filp, unsigned cmd, unsigned long v)
941 {
942         unsigned minor = iminor(file_inode(filp));
943         struct bch_fs *c = minor < U8_MAX ? idr_find(&bch_chardev_minor, minor) : NULL;
944         void __user *arg = (void __user *) v;
945
946         return c
947                 ? bch2_fs_ioctl(c, cmd, arg)
948                 : bch2_global_ioctl(cmd, arg);
949 }
950
951 static const struct file_operations bch_chardev_fops = {
952         .owner          = THIS_MODULE,
953         .unlocked_ioctl = bch2_chardev_ioctl,
954         .open           = nonseekable_open,
955 };
956
957 static int bch_chardev_major;
958 static struct class *bch_chardev_class;
959 static struct device *bch_chardev;
960
961 void bch2_fs_chardev_exit(struct bch_fs *c)
962 {
963         if (!IS_ERR_OR_NULL(c->chardev))
964                 device_unregister(c->chardev);
965         if (c->minor >= 0)
966                 idr_remove(&bch_chardev_minor, c->minor);
967 }
968
969 int bch2_fs_chardev_init(struct bch_fs *c)
970 {
971         c->minor = idr_alloc(&bch_chardev_minor, c, 0, 0, GFP_KERNEL);
972         if (c->minor < 0)
973                 return c->minor;
974
975         c->chardev = device_create(bch_chardev_class, NULL,
976                                    MKDEV(bch_chardev_major, c->minor), c,
977                                    "bcachefs%u-ctl", c->minor);
978         if (IS_ERR(c->chardev))
979                 return PTR_ERR(c->chardev);
980
981         return 0;
982 }
983
984 void bch2_chardev_exit(void)
985 {
986         if (!IS_ERR_OR_NULL(bch_chardev_class))
987                 device_destroy(bch_chardev_class,
988                                MKDEV(bch_chardev_major, U8_MAX));
989         if (!IS_ERR_OR_NULL(bch_chardev_class))
990                 class_destroy(bch_chardev_class);
991         if (bch_chardev_major > 0)
992                 unregister_chrdev(bch_chardev_major, "bcachefs");
993 }
994
995 int __init bch2_chardev_init(void)
996 {
997         bch_chardev_major = register_chrdev(0, "bcachefs-ctl", &bch_chardev_fops);
998         if (bch_chardev_major < 0)
999                 return bch_chardev_major;
1000
1001         bch_chardev_class = class_create("bcachefs");
1002         if (IS_ERR(bch_chardev_class))
1003                 return PTR_ERR(bch_chardev_class);
1004
1005         bch_chardev = device_create(bch_chardev_class, NULL,
1006                                     MKDEV(bch_chardev_major, U8_MAX),
1007                                     NULL, "bcachefs-ctl");
1008         if (IS_ERR(bch_chardev))
1009                 return PTR_ERR(bch_chardev);
1010
1011         return 0;
1012 }
1013
1014 #endif /* NO_BCACHEFS_CHARDEV */