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