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