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