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 */ 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 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 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 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 229 /* We need request_key() to be called before we punt to kthread: */ 230 opt_set(thr->opts, nostart, true); 231 232 bch2_thread_with_stdio_init(&thr->thr, &bch2_offline_fsck_ops); 233 234 thr->c = bch2_fs_open(devs.data, arg.nr_devs, thr->opts); 235 236 if (!IS_ERR(thr->c) && 237 thr->c->opts.errors == BCH_ON_ERROR_panic) 238 thr->c->opts.errors = BCH_ON_ERROR_ro; 239 240 ret = __bch2_run_thread_with_stdio(&thr->thr); 241 out: 242 darray_for_each(devs, i) 243 kfree(*i); 244 darray_exit(&devs); 245 return ret; 246 err: 247 if (thr) 248 bch2_fsck_thread_exit(&thr->thr); 249 pr_err("ret %s", bch2_err_str(ret)); 250 goto out; 251 } 252 253 static long bch2_global_ioctl(unsigned cmd, void __user *arg) 254 { 255 long ret; 256 257 switch (cmd) { 258 #if 0 259 case BCH_IOCTL_ASSEMBLE: 260 return bch2_ioctl_assemble(arg); 261 case BCH_IOCTL_INCREMENTAL: 262 return bch2_ioctl_incremental(arg); 263 #endif 264 case BCH_IOCTL_FSCK_OFFLINE: { 265 ret = bch2_ioctl_fsck_offline(arg); 266 break; 267 } 268 default: 269 ret = -ENOTTY; 270 break; 271 } 272 273 if (ret < 0) 274 ret = bch2_err_class(ret); 275 return ret; 276 } 277 278 static long bch2_ioctl_query_uuid(struct bch_fs *c, 279 struct bch_ioctl_query_uuid __user *user_arg) 280 { 281 return copy_to_user_errcode(&user_arg->uuid, &c->sb.user_uuid, 282 sizeof(c->sb.user_uuid)); 283 } 284 285 #if 0 286 static long bch2_ioctl_start(struct bch_fs *c, struct bch_ioctl_start arg) 287 { 288 if (!capable(CAP_SYS_ADMIN)) 289 return -EPERM; 290 291 if (arg.flags || arg.pad) 292 return -EINVAL; 293 294 return bch2_fs_start(c); 295 } 296 297 static long bch2_ioctl_stop(struct bch_fs *c) 298 { 299 if (!capable(CAP_SYS_ADMIN)) 300 return -EPERM; 301 302 bch2_fs_stop(c); 303 return 0; 304 } 305 #endif 306 307 static long bch2_ioctl_disk_add(struct bch_fs *c, struct bch_ioctl_disk arg) 308 { 309 char *path; 310 int ret; 311 312 if (!capable(CAP_SYS_ADMIN)) 313 return -EPERM; 314 315 if (arg.flags || arg.pad) 316 return -EINVAL; 317 318 path = strndup_user((const char __user *)(unsigned long) arg.dev, PATH_MAX); 319 ret = PTR_ERR_OR_ZERO(path); 320 if (ret) 321 return ret; 322 323 ret = bch2_dev_add(c, path); 324 if (!IS_ERR(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 bch2_dev_put(ca); 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 bch2_dev_put(ca); 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 }; 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 = {}; 507 darray_char replicas = {}; 508 u32 replica_entries_bytes; 509 int ret = 0; 510 511 if (!test_bit(BCH_FS_started, &c->flags)) 512 return -EINVAL; 513 514 if (get_user(replica_entries_bytes, &user_arg->replica_entries_bytes)) 515 return -EFAULT; 516 517 ret = bch2_fs_replicas_usage_read(c, &replicas) ?: 518 (replica_entries_bytes < replicas.nr ? -ERANGE : 0) ?: 519 copy_to_user_errcode(&user_arg->replicas, replicas.data, replicas.nr); 520 if (ret) 521 goto err; 522 523 struct bch_fs_usage_short u = bch2_fs_usage_read_short(c); 524 arg.capacity = c->capacity; 525 arg.used = u.used; 526 arg.online_reserved = percpu_u64_get(c->online_reserved); 527 arg.replica_entries_bytes = replicas.nr; 528 529 for (unsigned i = 0; i < BCH_REPLICAS_MAX; i++) { 530 struct disk_accounting_pos k = { 531 .type = BCH_DISK_ACCOUNTING_persistent_reserved, 532 .persistent_reserved.nr_replicas = i, 533 }; 534 535 bch2_accounting_mem_read(c, 536 disk_accounting_pos_to_bpos(&k), 537 &arg.persistent_reserved[i], 1); 538 } 539 540 ret = copy_to_user_errcode(user_arg, &arg, sizeof(arg)); 541 err: 542 darray_exit(&replicas); 543 return ret; 544 } 545 546 static long bch2_ioctl_query_accounting(struct bch_fs *c, 547 struct bch_ioctl_query_accounting __user *user_arg) 548 { 549 struct bch_ioctl_query_accounting arg; 550 darray_char accounting = {}; 551 int ret = 0; 552 553 if (!test_bit(BCH_FS_started, &c->flags)) 554 return -EINVAL; 555 556 ret = copy_from_user_errcode(&arg, user_arg, sizeof(arg)) ?: 557 bch2_fs_accounting_read(c, &accounting, arg.accounting_types_mask) ?: 558 (arg.accounting_u64s * sizeof(u64) < accounting.nr ? -ERANGE : 0) ?: 559 copy_to_user_errcode(&user_arg->accounting, accounting.data, accounting.nr); 560 if (ret) 561 goto err; 562 563 arg.capacity = c->capacity; 564 arg.used = bch2_fs_usage_read_short(c).used; 565 arg.online_reserved = percpu_u64_get(c->online_reserved); 566 arg.accounting_u64s = accounting.nr / sizeof(u64); 567 568 ret = copy_to_user_errcode(user_arg, &arg, sizeof(arg)); 569 err: 570 darray_exit(&accounting); 571 return ret; 572 } 573 574 /* obsolete, didn't allow for new data types: */ 575 static long bch2_ioctl_dev_usage(struct bch_fs *c, 576 struct bch_ioctl_dev_usage __user *user_arg) 577 { 578 struct bch_ioctl_dev_usage arg; 579 struct bch_dev_usage src; 580 struct bch_dev *ca; 581 unsigned i; 582 583 if (!test_bit(BCH_FS_started, &c->flags)) 584 return -EINVAL; 585 586 if (copy_from_user(&arg, user_arg, sizeof(arg))) 587 return -EFAULT; 588 589 if ((arg.flags & ~BCH_BY_INDEX) || 590 arg.pad[0] || 591 arg.pad[1] || 592 arg.pad[2]) 593 return -EINVAL; 594 595 ca = bch2_device_lookup(c, arg.dev, arg.flags); 596 if (IS_ERR(ca)) 597 return PTR_ERR(ca); 598 599 src = bch2_dev_usage_read(ca); 600 601 arg.state = ca->mi.state; 602 arg.bucket_size = ca->mi.bucket_size; 603 arg.nr_buckets = ca->mi.nbuckets - ca->mi.first_bucket; 604 605 for (i = 0; i < ARRAY_SIZE(arg.d); i++) { 606 arg.d[i].buckets = src.d[i].buckets; 607 arg.d[i].sectors = src.d[i].sectors; 608 arg.d[i].fragmented = src.d[i].fragmented; 609 } 610 611 bch2_dev_put(ca); 612 613 return copy_to_user_errcode(user_arg, &arg, sizeof(arg)); 614 } 615 616 static long bch2_ioctl_dev_usage_v2(struct bch_fs *c, 617 struct bch_ioctl_dev_usage_v2 __user *user_arg) 618 { 619 struct bch_ioctl_dev_usage_v2 arg; 620 struct bch_dev_usage src; 621 struct bch_dev *ca; 622 int ret = 0; 623 624 if (!test_bit(BCH_FS_started, &c->flags)) 625 return -EINVAL; 626 627 if (copy_from_user(&arg, user_arg, sizeof(arg))) 628 return -EFAULT; 629 630 if ((arg.flags & ~BCH_BY_INDEX) || 631 arg.pad[0] || 632 arg.pad[1] || 633 arg.pad[2]) 634 return -EINVAL; 635 636 ca = bch2_device_lookup(c, arg.dev, arg.flags); 637 if (IS_ERR(ca)) 638 return PTR_ERR(ca); 639 640 src = bch2_dev_usage_read(ca); 641 642 arg.state = ca->mi.state; 643 arg.bucket_size = ca->mi.bucket_size; 644 arg.nr_data_types = min(arg.nr_data_types, BCH_DATA_NR); 645 arg.nr_buckets = ca->mi.nbuckets - ca->mi.first_bucket; 646 647 ret = copy_to_user_errcode(user_arg, &arg, sizeof(arg)); 648 if (ret) 649 goto err; 650 651 for (unsigned i = 0; i < arg.nr_data_types; i++) { 652 struct bch_ioctl_dev_usage_type t = { 653 .buckets = src.d[i].buckets, 654 .sectors = src.d[i].sectors, 655 .fragmented = src.d[i].fragmented, 656 }; 657 658 ret = copy_to_user_errcode(&user_arg->d[i], &t, sizeof(t)); 659 if (ret) 660 goto err; 661 } 662 err: 663 bch2_dev_put(ca); 664 return ret; 665 } 666 667 static long bch2_ioctl_read_super(struct bch_fs *c, 668 struct bch_ioctl_read_super arg) 669 { 670 struct bch_dev *ca = NULL; 671 struct bch_sb *sb; 672 int ret = 0; 673 674 if (!capable(CAP_SYS_ADMIN)) 675 return -EPERM; 676 677 if ((arg.flags & ~(BCH_BY_INDEX|BCH_READ_DEV)) || 678 arg.pad) 679 return -EINVAL; 680 681 mutex_lock(&c->sb_lock); 682 683 if (arg.flags & BCH_READ_DEV) { 684 ca = bch2_device_lookup(c, arg.dev, arg.flags); 685 ret = PTR_ERR_OR_ZERO(ca); 686 if (ret) 687 goto err_unlock; 688 689 sb = ca->disk_sb.sb; 690 } else { 691 sb = c->disk_sb.sb; 692 } 693 694 if (vstruct_bytes(sb) > arg.size) { 695 ret = -ERANGE; 696 goto err; 697 } 698 699 ret = copy_to_user_errcode((void __user *)(unsigned long)arg.sb, sb, 700 vstruct_bytes(sb)); 701 err: 702 bch2_dev_put(ca); 703 err_unlock: 704 mutex_unlock(&c->sb_lock); 705 return ret; 706 } 707 708 static long bch2_ioctl_disk_get_idx(struct bch_fs *c, 709 struct bch_ioctl_disk_get_idx arg) 710 { 711 dev_t dev = huge_decode_dev(arg.dev); 712 713 if (!capable(CAP_SYS_ADMIN)) 714 return -EPERM; 715 716 if (!dev) 717 return -EINVAL; 718 719 for_each_online_member(c, ca) 720 if (ca->dev == dev) { 721 percpu_ref_put(&ca->io_ref); 722 return ca->dev_idx; 723 } 724 725 return -BCH_ERR_ENOENT_dev_idx_not_found; 726 } 727 728 static long bch2_ioctl_disk_resize(struct bch_fs *c, 729 struct bch_ioctl_disk_resize arg) 730 { 731 struct bch_dev *ca; 732 int ret; 733 734 if (!capable(CAP_SYS_ADMIN)) 735 return -EPERM; 736 737 if ((arg.flags & ~BCH_BY_INDEX) || 738 arg.pad) 739 return -EINVAL; 740 741 ca = bch2_device_lookup(c, arg.dev, arg.flags); 742 if (IS_ERR(ca)) 743 return PTR_ERR(ca); 744 745 ret = bch2_dev_resize(c, ca, arg.nbuckets); 746 747 bch2_dev_put(ca); 748 return ret; 749 } 750 751 static long bch2_ioctl_disk_resize_journal(struct bch_fs *c, 752 struct bch_ioctl_disk_resize_journal arg) 753 { 754 struct bch_dev *ca; 755 int ret; 756 757 if (!capable(CAP_SYS_ADMIN)) 758 return -EPERM; 759 760 if ((arg.flags & ~BCH_BY_INDEX) || 761 arg.pad) 762 return -EINVAL; 763 764 if (arg.nbuckets > U32_MAX) 765 return -EINVAL; 766 767 ca = bch2_device_lookup(c, arg.dev, arg.flags); 768 if (IS_ERR(ca)) 769 return PTR_ERR(ca); 770 771 ret = bch2_set_nr_journal_buckets(c, ca, arg.nbuckets); 772 773 bch2_dev_put(ca); 774 return ret; 775 } 776 777 static int bch2_fsck_online_thread_fn(struct thread_with_stdio *stdio) 778 { 779 struct fsck_thread *thr = container_of(stdio, struct fsck_thread, thr); 780 struct bch_fs *c = thr->c; 781 782 c->stdio_filter = current; 783 c->stdio = &thr->thr.stdio; 784 785 /* 786 * XXX: can we figure out a way to do this without mucking with c->opts? 787 */ 788 unsigned old_fix_errors = c->opts.fix_errors; 789 if (opt_defined(thr->opts, fix_errors)) 790 c->opts.fix_errors = thr->opts.fix_errors; 791 else 792 c->opts.fix_errors = FSCK_FIX_ask; 793 794 c->opts.fsck = true; 795 set_bit(BCH_FS_fsck_running, &c->flags); 796 797 c->curr_recovery_pass = BCH_RECOVERY_PASS_check_alloc_info; 798 int ret = bch2_run_online_recovery_passes(c); 799 800 clear_bit(BCH_FS_fsck_running, &c->flags); 801 bch_err_fn(c, ret); 802 803 c->stdio = NULL; 804 c->stdio_filter = NULL; 805 c->opts.fix_errors = old_fix_errors; 806 807 up(&c->online_fsck_mutex); 808 bch2_ro_ref_put(c); 809 return ret; 810 } 811 812 static const struct thread_with_stdio_ops bch2_online_fsck_ops = { 813 .exit = bch2_fsck_thread_exit, 814 .fn = bch2_fsck_online_thread_fn, 815 }; 816 817 static long bch2_ioctl_fsck_online(struct bch_fs *c, 818 struct bch_ioctl_fsck_online arg) 819 { 820 struct fsck_thread *thr = NULL; 821 long ret = 0; 822 823 if (arg.flags) 824 return -EINVAL; 825 826 if (!capable(CAP_SYS_ADMIN)) 827 return -EPERM; 828 829 if (!bch2_ro_ref_tryget(c)) 830 return -EROFS; 831 832 if (down_trylock(&c->online_fsck_mutex)) { 833 bch2_ro_ref_put(c); 834 return -EAGAIN; 835 } 836 837 thr = kzalloc(sizeof(*thr), GFP_KERNEL); 838 if (!thr) { 839 ret = -ENOMEM; 840 goto err; 841 } 842 843 thr->c = c; 844 thr->opts = bch2_opts_empty(); 845 846 if (arg.opts) { 847 char *optstr = strndup_user((char __user *)(unsigned long) arg.opts, 1 << 16); 848 849 ret = PTR_ERR_OR_ZERO(optstr) ?: 850 bch2_parse_mount_opts(c, &thr->opts, NULL, optstr); 851 if (!IS_ERR(optstr)) 852 kfree(optstr); 853 854 if (ret) 855 goto err; 856 } 857 858 ret = bch2_run_thread_with_stdio(&thr->thr, &bch2_online_fsck_ops); 859 err: 860 if (ret < 0) { 861 bch_err_fn(c, ret); 862 if (thr) 863 bch2_fsck_thread_exit(&thr->thr); 864 up(&c->online_fsck_mutex); 865 bch2_ro_ref_put(c); 866 } 867 return ret; 868 } 869 870 #define BCH_IOCTL(_name, _argtype) \ 871 do { \ 872 _argtype i; \ 873 \ 874 if (copy_from_user(&i, arg, sizeof(i))) \ 875 return -EFAULT; \ 876 ret = bch2_ioctl_##_name(c, i); \ 877 goto out; \ 878 } while (0) 879 880 long bch2_fs_ioctl(struct bch_fs *c, unsigned cmd, void __user *arg) 881 { 882 long ret; 883 884 switch (cmd) { 885 case BCH_IOCTL_QUERY_UUID: 886 return bch2_ioctl_query_uuid(c, arg); 887 case BCH_IOCTL_FS_USAGE: 888 return bch2_ioctl_fs_usage(c, arg); 889 case BCH_IOCTL_DEV_USAGE: 890 return bch2_ioctl_dev_usage(c, arg); 891 case BCH_IOCTL_DEV_USAGE_V2: 892 return bch2_ioctl_dev_usage_v2(c, arg); 893 #if 0 894 case BCH_IOCTL_START: 895 BCH_IOCTL(start, struct bch_ioctl_start); 896 case BCH_IOCTL_STOP: 897 return bch2_ioctl_stop(c); 898 #endif 899 case BCH_IOCTL_READ_SUPER: 900 BCH_IOCTL(read_super, struct bch_ioctl_read_super); 901 case BCH_IOCTL_DISK_GET_IDX: 902 BCH_IOCTL(disk_get_idx, struct bch_ioctl_disk_get_idx); 903 } 904 905 if (!test_bit(BCH_FS_started, &c->flags)) 906 return -EINVAL; 907 908 switch (cmd) { 909 case BCH_IOCTL_DISK_ADD: 910 BCH_IOCTL(disk_add, struct bch_ioctl_disk); 911 case BCH_IOCTL_DISK_REMOVE: 912 BCH_IOCTL(disk_remove, struct bch_ioctl_disk); 913 case BCH_IOCTL_DISK_ONLINE: 914 BCH_IOCTL(disk_online, struct bch_ioctl_disk); 915 case BCH_IOCTL_DISK_OFFLINE: 916 BCH_IOCTL(disk_offline, struct bch_ioctl_disk); 917 case BCH_IOCTL_DISK_SET_STATE: 918 BCH_IOCTL(disk_set_state, struct bch_ioctl_disk_set_state); 919 case BCH_IOCTL_DATA: 920 BCH_IOCTL(data, struct bch_ioctl_data); 921 case BCH_IOCTL_DISK_RESIZE: 922 BCH_IOCTL(disk_resize, struct bch_ioctl_disk_resize); 923 case BCH_IOCTL_DISK_RESIZE_JOURNAL: 924 BCH_IOCTL(disk_resize_journal, struct bch_ioctl_disk_resize_journal); 925 case BCH_IOCTL_FSCK_ONLINE: 926 BCH_IOCTL(fsck_online, struct bch_ioctl_fsck_online); 927 case BCH_IOCTL_QUERY_ACCOUNTING: 928 return bch2_ioctl_query_accounting(c, arg); 929 default: 930 return -ENOTTY; 931 } 932 out: 933 if (ret < 0) 934 ret = bch2_err_class(ret); 935 return ret; 936 } 937 938 static DEFINE_IDR(bch_chardev_minor); 939 940 static long bch2_chardev_ioctl(struct file *filp, unsigned cmd, unsigned long v) 941 { 942 unsigned minor = iminor(file_inode(filp)); 943 struct bch_fs *c = minor < U8_MAX ? idr_find(&bch_chardev_minor, minor) : NULL; 944 void __user *arg = (void __user *) v; 945 946 return c 947 ? bch2_fs_ioctl(c, cmd, arg) 948 : bch2_global_ioctl(cmd, arg); 949 } 950 951 static const struct file_operations bch_chardev_fops = { 952 .owner = THIS_MODULE, 953 .unlocked_ioctl = bch2_chardev_ioctl, 954 .open = nonseekable_open, 955 }; 956 957 static int bch_chardev_major; 958 static const struct class bch_chardev_class = { 959 .name = "bcachefs", 960 }; 961 static struct device *bch_chardev; 962 963 void bch2_fs_chardev_exit(struct bch_fs *c) 964 { 965 if (!IS_ERR_OR_NULL(c->chardev)) 966 device_unregister(c->chardev); 967 if (c->minor >= 0) 968 idr_remove(&bch_chardev_minor, c->minor); 969 } 970 971 int bch2_fs_chardev_init(struct bch_fs *c) 972 { 973 c->minor = idr_alloc(&bch_chardev_minor, c, 0, 0, GFP_KERNEL); 974 if (c->minor < 0) 975 return c->minor; 976 977 c->chardev = device_create(&bch_chardev_class, NULL, 978 MKDEV(bch_chardev_major, c->minor), c, 979 "bcachefs%u-ctl", c->minor); 980 if (IS_ERR(c->chardev)) 981 return PTR_ERR(c->chardev); 982 983 return 0; 984 } 985 986 void bch2_chardev_exit(void) 987 { 988 device_destroy(&bch_chardev_class, MKDEV(bch_chardev_major, U8_MAX)); 989 class_unregister(&bch_chardev_class); 990 if (bch_chardev_major > 0) 991 unregister_chrdev(bch_chardev_major, "bcachefs"); 992 } 993 994 int __init bch2_chardev_init(void) 995 { 996 int ret; 997 998 bch_chardev_major = register_chrdev(0, "bcachefs-ctl", &bch_chardev_fops); 999 if (bch_chardev_major < 0) 1000 return bch_chardev_major; 1001 1002 ret = class_register(&bch_chardev_class); 1003 if (ret) 1004 goto major_out; 1005 1006 bch_chardev = device_create(&bch_chardev_class, NULL, 1007 MKDEV(bch_chardev_major, U8_MAX), 1008 NULL, "bcachefs-ctl"); 1009 if (IS_ERR(bch_chardev)) { 1010 ret = PTR_ERR(bch_chardev); 1011 goto class_out; 1012 } 1013 1014 return 0; 1015 1016 class_out: 1017 class_unregister(&bch_chardev_class); 1018 major_out: 1019 unregister_chrdev(bch_chardev_major, "bcachefs-ctl"); 1020 return ret; 1021 } 1022 1023 #endif /* NO_BCACHEFS_CHARDEV */ 1024