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