1 // SPDX-License-Identifier: GPL-2.0 1 2 #ifndef NO_BCACHEFS_FS 3 4 #include "bcachefs.h" 5 #include "alloc_foreground.h" 6 #include "bkey_buf.h" 7 #include "btree_update.h" 8 #include "buckets.h" 9 #include "clock.h" 10 #include "error.h" 11 #include "extents.h" 12 #include "extent_update.h" 13 #include "fs.h" 14 #include "fs-io.h" 15 #include "fs-io-buffered.h" 16 #include "fs-io-pagecache.h" 17 #include "fsck.h" 18 #include "inode.h" 19 #include "journal.h" 20 #include "io_misc.h" 21 #include "keylist.h" 22 #include "quota.h" 23 #include "reflink.h" 24 #include "trace.h" 25 26 #include <linux/aio.h> 27 #include <linux/backing-dev.h> 28 #include <linux/falloc.h> 29 #include <linux/migrate.h> 30 #include <linux/mmu_context.h> 31 #include <linux/pagevec.h> 32 #include <linux/rmap.h> 33 #include <linux/sched/signal.h> 34 #include <linux/task_io_accounting_ops.h> 35 #include <linux/uio.h> 36 37 #include <trace/events/writeback.h> 38 39 struct nocow_flush { 40 struct closure *cl; 41 struct bch_dev *ca; 42 struct bio bio; 43 }; 44 45 static void nocow_flush_endio(struct bio *_bio 46 { 47 48 struct nocow_flush *bio = container_of 49 50 closure_put(bio->cl); 51 percpu_ref_put(&bio->ca->io_ref); 52 bio_put(&bio->bio); 53 } 54 55 void bch2_inode_flush_nocow_writes_async(struc 56 struc 57 struc 58 { 59 struct nocow_flush *bio; 60 struct bch_dev *ca; 61 struct bch_devs_mask devs; 62 unsigned dev; 63 64 dev = find_first_bit(inode->ei_devs_ne 65 if (dev == BCH_SB_MEMBERS_MAX) 66 return; 67 68 devs = inode->ei_devs_need_flush; 69 memset(&inode->ei_devs_need_flush, 0, 70 71 for_each_set_bit(dev, devs.d, BCH_SB_M 72 rcu_read_lock(); 73 ca = rcu_dereference(c->devs[d 74 if (ca && !percpu_ref_tryget(& 75 ca = NULL; 76 rcu_read_unlock(); 77 78 if (!ca) 79 continue; 80 81 bio = container_of(bio_alloc_b 82 83 84 85 struct noco 86 bio->cl = cl; 87 bio->ca = ca; 88 bio->bio.bi_end_io = noco 89 closure_bio_submit(&bio->bio, 90 } 91 } 92 93 static int bch2_inode_flush_nocow_writes(struc 94 struc 95 { 96 struct closure cl; 97 98 closure_init_stack(&cl); 99 bch2_inode_flush_nocow_writes_async(c, 100 closure_sync(&cl); 101 102 return 0; 103 } 104 105 /* i_size updates: */ 106 107 struct inode_new_size { 108 loff_t new_size; 109 u64 now; 110 unsigned fields; 111 }; 112 113 static int inode_set_size(struct btree_trans * 114 struct bch_inode_inf 115 struct bch_inode_unp 116 void *p) 117 { 118 struct inode_new_size *s = p; 119 120 bi->bi_size = s->new_size; 121 if (s->fields & ATTR_ATIME) 122 bi->bi_atime = s->now; 123 if (s->fields & ATTR_MTIME) 124 bi->bi_mtime = s->now; 125 if (s->fields & ATTR_CTIME) 126 bi->bi_ctime = s->now; 127 128 return 0; 129 } 130 131 int __must_check bch2_write_inode_size(struct 132 struct 133 loff_t 134 { 135 struct inode_new_size s = { 136 .new_size = new_size, 137 .now = bch2_current 138 .fields = fields, 139 }; 140 141 return bch2_write_inode(c, inode, inod 142 } 143 144 void __bch2_i_sectors_acct(struct bch_fs *c, s 145 struct quota_res *q 146 { 147 bch2_fs_inconsistent_on((s64) inode->v 148 "inode %lu i_b 149 inode->v.i_ino 150 inode->ei_inod 151 inode->v.i_blocks += sectors; 152 153 #ifdef CONFIG_BCACHEFS_QUOTA 154 if (quota_res && 155 !test_bit(EI_INODE_SNAPSHOT, &inod 156 sectors > 0) { 157 BUG_ON(sectors > quota_res->se 158 BUG_ON(sectors > inode->ei_quo 159 160 quota_res->sectors -= sectors; 161 inode->ei_quota_reserved -= se 162 } else { 163 bch2_quota_acct(c, inode->ei_q 164 } 165 #endif 166 } 167 168 /* fsync: */ 169 170 /* 171 * inode->ei_inode.bi_journal_seq won't be up 172 * insert trigger: look up the btree inode ins 173 */ 174 static int bch2_flush_inode(struct bch_fs *c, 175 struct bch_inode_i 176 { 177 if (c->opts.journal_flush_disabled) 178 return 0; 179 180 if (!bch2_write_ref_tryget(c, BCH_WRIT 181 return -EROFS; 182 183 struct bch_inode_unpacked u; 184 int ret = bch2_inode_find_by_inum(c, i 185 bch2_journal_flush_seq(&c->j 186 bch2_inode_flush_nocow_write 187 bch2_write_ref_put(c, BCH_WRITE_REF_fs 188 return ret; 189 } 190 191 int bch2_fsync(struct file *file, loff_t start 192 { 193 struct bch_inode_info *inode = file_bc 194 struct bch_fs *c = inode->v.i_sb->s_fs 195 int ret, err; 196 197 trace_bch2_fsync(file, datasync); 198 199 ret = file_write_and_wait_range(file, 200 if (ret) 201 goto out; 202 ret = sync_inode_metadata(&inode->v, 1 203 if (ret) 204 goto out; 205 ret = bch2_flush_inode(c, inode); 206 out: 207 ret = bch2_err_class(ret); 208 if (ret == -EROFS) 209 ret = -EIO; 210 211 err = file_check_and_advance_wb_err(fi 212 if (!ret) 213 ret = err; 214 215 return ret; 216 } 217 218 /* truncate: */ 219 220 static inline int range_has_data(struct bch_fs 221 struct bpos s 222 struct bpos e 223 { 224 return bch2_trans_run(c, 225 for_each_btree_key_in_subvolum 226 227 bkey_extent_is_data(k. 228 }))); 229 } 230 231 static int __bch2_truncate_folio(struct bch_in 232 pgoff_t index 233 { 234 struct bch_fs *c = inode->v.i_sb->s_fs 235 struct address_space *mapping = inode- 236 struct bch_folio *s; 237 unsigned start_offset; 238 unsigned end_offset; 239 unsigned i; 240 struct folio *folio; 241 s64 i_sectors_delta = 0; 242 int ret = 0; 243 u64 end_pos; 244 245 folio = filemap_lock_folio(mapping, in 246 if (IS_ERR_OR_NULL(folio)) { 247 /* 248 * XXX: we're doing two index 249 * folio 250 */ 251 ret = range_has_data(c, inode- 252 POS(inode->v.i 253 POS(inode->v.i 254 if (ret <= 0) 255 return ret; 256 257 folio = __filemap_get_folio(ma 258 FG 259 if (IS_ERR_OR_NULL(folio)) { 260 ret = -ENOMEM; 261 goto out; 262 } 263 } 264 265 BUG_ON(start >= folio_end_pos(folio 266 BUG_ON(end <= folio_pos(folio)); 267 268 start_offset = max(start, folio_pos 269 end_offset = min_t(u64, end, foli 270 271 /* Folio boundary? Nothing to do */ 272 if (start_offset == 0 && 273 end_offset == folio_size(folio)) { 274 ret = 0; 275 goto unlock; 276 } 277 278 s = bch2_folio_create(folio, 0); 279 if (!s) { 280 ret = -ENOMEM; 281 goto unlock; 282 } 283 284 if (!folio_test_uptodate(folio)) { 285 ret = bch2_read_single_folio(f 286 if (ret) 287 goto unlock; 288 } 289 290 ret = bch2_folio_set(c, inode_inum(ino 291 if (ret) 292 goto unlock; 293 294 for (i = round_up(start_offset, block_ 295 i < round_down(end_offset, block_ 296 i++) { 297 s->s[i].nr_replicas = 0; 298 299 i_sectors_delta -= s->s[i].sta 300 bch2_folio_sector_set(folio, s 301 } 302 303 bch2_i_sectors_acct(c, inode, NULL, i_ 304 305 /* 306 * Caller needs to know whether this f 307 * writeback - doing an i_size update 308 * be responsible for the i_size updat 309 * 310 * Note that we shouldn't ever see a f 311 * warn if so. This has been observed 312 * after a short write and there's sti 313 * things up. 314 */ 315 WARN_ON_ONCE(folio_pos(folio) >= inode 316 end_pos = folio_end_pos(folio); 317 if (inode->v.i_size > folio_pos(folio) 318 end_pos = min_t(u64, inode->v. 319 ret = s->s[folio_pos_to_s(folio, end_p 320 321 folio_zero_segment(folio, start_offset 322 323 /* 324 * Bit of a hack - we don't want trunc 325 * 326 * XXX: because we aren't currently tr 327 * data in it (vs. just 0s, or only pa 328 */ 329 BUG_ON(bch2_get_folio_disk_reservation 330 331 /* 332 * This removes any writeable userspac 333 * .page_mkwrite to be called again be 334 * redirty the full page: 335 */ 336 folio_mkclean(folio); 337 filemap_dirty_folio(mapping, folio); 338 unlock: 339 folio_unlock(folio); 340 folio_put(folio); 341 out: 342 return ret; 343 } 344 345 static int bch2_truncate_folio(struct bch_inod 346 { 347 return __bch2_truncate_folio(inode, fr 348 from, ANY 349 } 350 351 static int bch2_truncate_folios(struct bch_ino 352 loff_t start, 353 { 354 int ret = __bch2_truncate_folio(inode, 355 start, 356 357 if (ret >= 0 && 358 start >> PAGE_SHIFT != end >> PAGE 359 ret = __bch2_truncate_folio(in 360 (end - 361 start, 362 return ret; 363 } 364 365 static int bch2_extend(struct mnt_idmap *idmap 366 struct bch_inode_info * 367 struct bch_inode_unpack 368 struct iattr *iattr) 369 { 370 struct address_space *mapping = inode- 371 int ret; 372 373 /* 374 * sync appends: 375 * 376 * this has to be done _before_ extend 377 */ 378 ret = filemap_write_and_wait_range(map 379 if (ret) 380 return ret; 381 382 truncate_setsize(&inode->v, iattr->ia_ 383 384 return bch2_setattr_nonsize(idmap, ino 385 } 386 387 int bchfs_truncate(struct mnt_idmap *idmap, 388 struct bch_inode_info *inode 389 { 390 struct bch_fs *c = inode->v.i_sb->s_fs 391 struct address_space *mapping = inode- 392 struct bch_inode_unpacked inode_u; 393 s64 i_sectors_delta = 0; 394 int ret = 0; 395 396 /* 397 * If the truncate call with change th 398 * cmtimes should be updated. If the s 399 * do not need to update the cmtimes. 400 */ 401 if (iattr->ia_size != inode->v.i_size) 402 if (!(iattr->ia_valid & ATTR_M 403 ktime_get_coarse_real_ 404 if (!(iattr->ia_valid & ATTR_C 405 ktime_get_coarse_real_ 406 iattr->ia_valid |= ATTR_MTIME| 407 } 408 409 inode_dio_wait(&inode->v); 410 bch2_pagecache_block_get(inode); 411 412 ret = bch2_inode_find_by_inum(c, inode 413 if (ret) 414 goto err; 415 416 /* 417 * check this before next assertion; o 418 * invariants are a bit broken (trunca 419 * before the inode). 420 */ 421 ret = bch2_journal_error(&c->journal); 422 if (ret) 423 goto err; 424 425 WARN_ONCE(!test_bit(EI_INODE_ERROR, &i 426 inode->v.i_size < inode_u.bi 427 "truncate spotted in mem i_s 428 (u64) inode->v.i_size, inode 429 430 if (iattr->ia_size > inode->v.i_size) 431 ret = bch2_extend(idmap, inode 432 goto err; 433 } 434 435 iattr->ia_valid &= ~ATTR_SIZE; 436 437 ret = bch2_truncate_folio(inode, iattr 438 if (unlikely(ret < 0)) 439 goto err; 440 441 truncate_setsize(&inode->v, iattr->ia_ 442 443 /* 444 * When extending, we're going to writ 445 * immediately so we need to flush any 446 * i_size first: 447 * 448 * Also, when extending we need to flu 449 * straddles - if it's mapped to users 450 * userspace has to redirty it and cal 451 * again to allocate the part of the p 452 */ 453 if (iattr->ia_size > inode_u.bi_size) 454 ret = filemap_write_and_wait_r 455 inode_u.bi_siz 456 iattr->ia_size 457 else if (iattr->ia_size & (PAGE_SIZE - 458 ret = filemap_write_and_wait_r 459 round_down(iat 460 iattr->ia_size 461 if (ret) 462 goto err; 463 464 ret = bch2_truncate(c, inode_inum(inod 465 bch2_i_sectors_acct(c, inode, NULL, i_ 466 467 if (unlikely(ret)) { 468 /* 469 * If we error here, VFS cache 470 */ 471 set_bit(EI_INODE_ERROR, &inode 472 goto err; 473 } 474 475 bch2_fs_inconsistent_on(!inode->v.i_si 476 !bch2_journal_ 477 "inode %lu tru 478 inode->v.i_ino 479 inode->ei_inod 480 481 ret = bch2_setattr_nonsize(idmap, inod 482 err: 483 bch2_pagecache_block_put(inode); 484 return bch2_err_class(ret); 485 } 486 487 /* fallocate: */ 488 489 static int inode_update_times_fn(struct btree_ 490 struct bch_in 491 struct bch_in 492 { 493 struct bch_fs *c = inode->v.i_sb->s_fs 494 495 bi->bi_mtime = bi->bi_ctime = bch2_cur 496 return 0; 497 } 498 499 static noinline long bchfs_fpunch(struct bch_i 500 { 501 struct bch_fs *c = inode->v.i_sb->s_fs 502 u64 end = offset + len; 503 u64 block_start = round_up(offset, blo 504 u64 block_end = round_down(end, bloc 505 bool truncated_last_page; 506 int ret = 0; 507 508 ret = bch2_truncate_folios(inode, offs 509 if (unlikely(ret < 0)) 510 goto err; 511 512 truncated_last_page = ret; 513 514 truncate_pagecache_range(&inode->v, of 515 516 if (block_start < block_end) { 517 s64 i_sectors_delta = 0; 518 519 ret = bch2_fpunch(c, inode_inu 520 block_start 521 &i_sectors_d 522 bch2_i_sectors_acct(c, inode, 523 } 524 525 mutex_lock(&inode->ei_update_lock); 526 if (end >= inode->v.i_size && !truncat 527 ret = bch2_write_inode_size(c, 528 AT 529 } else { 530 ret = bch2_write_inode(c, inod 531 ATTR_MT 532 } 533 mutex_unlock(&inode->ei_update_lock); 534 err: 535 return ret; 536 } 537 538 static noinline long bchfs_fcollapse_finsert(s 539 loff_t offs 540 bool insert 541 { 542 struct bch_fs *c = inode->v.i_sb->s_fs 543 struct address_space *mapping = inode- 544 s64 i_sectors_delta = 0; 545 int ret = 0; 546 547 if ((offset | len) & (block_bytes(c) - 548 return -EINVAL; 549 550 if (insert) { 551 if (offset >= inode->v.i_size) 552 return -EINVAL; 553 } else { 554 if (offset + len >= inode->v.i 555 return -EINVAL; 556 } 557 558 ret = bch2_write_invalidate_inode_page 559 if (ret) 560 return ret; 561 562 if (insert) 563 i_size_write(&inode->v, inode- 564 565 ret = bch2_fcollapse_finsert(c, inode_ 566 insert, & 567 if (!ret && !insert) 568 i_size_write(&inode->v, inode- 569 bch2_i_sectors_acct(c, inode, NULL, i_ 570 571 return ret; 572 } 573 574 static noinline int __bchfs_fallocate(struct b 575 u64 start_sector, 576 { 577 struct bch_fs *c = inode->v.i_sb->s_fs 578 struct btree_trans *trans = bch2_trans 579 struct btree_iter iter; 580 struct bpos end_pos = POS(inode->v.i_i 581 struct bch_io_opts opts; 582 int ret = 0; 583 584 bch2_inode_opts_get(&opts, c, &inode-> 585 586 bch2_trans_iter_init(trans, &iter, BTR 587 POS(inode->v.i_ino, st 588 BTREE_ITER_slots|BTREE 589 590 while (!ret) { 591 s64 i_sectors_delta = 0; 592 struct quota_res quota_res = { 593 struct bkey_s_c k; 594 unsigned sectors; 595 bool is_allocation; 596 u64 hole_start, hole_end; 597 u32 snapshot; 598 599 bch2_trans_begin(trans); 600 601 if (bkey_ge(iter.pos, end_pos) 602 break; 603 604 ret = bch2_subvolume_get_snaps 605 inode- 606 if (ret) 607 goto bkey_err; 608 609 bch2_btree_iter_set_snapshot(& 610 611 k = bch2_btree_iter_peek_slot( 612 if ((ret = bkey_err(k))) 613 goto bkey_err; 614 615 hole_start = iter.pos.off 616 hole_end = bpos_min(k.k 617 is_allocation = bkey_extent_ 618 619 /* already reserved */ 620 if (bkey_extent_is_reservation 621 bch2_bkey_nr_ptrs_fully_al 622 bch2_btree_iter_advanc 623 continue; 624 } 625 626 if (bkey_extent_is_data(k.k) & 627 !(mode & FALLOC_FL_ZERO_RA 628 bch2_btree_iter_advanc 629 continue; 630 } 631 632 if (!(mode & FALLOC_FL_ZERO_RA 633 /* 634 * Lock ordering - can 635 * blocking on a folio 636 */ 637 if (bch2_clamp_data_ho 638 639 640 641 ret = drop_loc 642 (bch2_ 643 644 645 646 if (ret) 647 goto b 648 } 649 bch2_btree_iter_set_po 650 651 if (ret) 652 goto bkey_err; 653 654 if (hole_start == hole 655 continue; 656 } 657 658 sectors = hole_end - hole_star 659 660 if (!is_allocation) { 661 ret = bch2_quota_reser 662 "a 663 if (unlikely(ret)) 664 goto bkey_err; 665 } 666 667 ret = bch2_extent_fallocate(tr 668 se 669 wr 670 if (ret) 671 goto bkey_err; 672 673 bch2_i_sectors_acct(c, inode, 674 675 if (bch2_mark_pagecache_reserv 676 677 ret = drop_locks_do(tr 678 bch2_mark_page 679 680 if (ret) 681 goto bkey_err; 682 } 683 bkey_err: 684 bch2_quota_reservation_put(c, 685 if (bch2_err_matches(ret, BCH_ 686 ret = 0; 687 } 688 689 if (bch2_err_matches(ret, ENOSPC) && ( 690 struct quota_res quota_res = { 691 s64 i_sectors_delta = 0; 692 693 bch2_fpunch_at(trans, &iter, i 694 end_sector, &i_ 695 bch2_i_sectors_acct(c, inode, 696 bch2_quota_reservation_put(c, 697 } 698 699 bch2_trans_iter_exit(trans, &iter); 700 bch2_trans_put(trans); 701 return ret; 702 } 703 704 static noinline long bchfs_fallocate(struct bc 705 loff_t offset, lof 706 { 707 struct bch_fs *c = inode->v.i_sb->s_fs 708 u64 end = offset + len; 709 u64 block_start = round_down(offset, 710 u64 block_end = round_up(end, 711 bool truncated_last_page = false; 712 int ret, ret2 = 0; 713 714 if (!(mode & FALLOC_FL_KEEP_SIZE) && e 715 ret = inode_newsize_ok(&inode- 716 if (ret) 717 return ret; 718 } 719 720 if (mode & FALLOC_FL_ZERO_RANGE) { 721 ret = bch2_truncate_folios(ino 722 if (unlikely(ret < 0)) 723 return ret; 724 725 truncated_last_page = ret; 726 727 truncate_pagecache_range(&inod 728 729 block_start = round_up(off 730 block_end = round_down(e 731 } 732 733 ret = __bchfs_fallocate(inode, mode, b 734 735 /* 736 * On -ENOSPC in ZERO_RANGE mode, we s 737 * so that the VFS cache i_size is con 738 */ 739 if (ret && 740 !(bch2_err_matches(ret, ENOSPC) && 741 return ret; 742 743 if (mode & FALLOC_FL_KEEP_SIZE && end 744 end = inode->v.i_size; 745 746 if (end >= inode->v.i_size && 747 (((mode & FALLOC_FL_ZERO_RANGE) && 748 !(mode & FALLOC_FL_KEEP_SIZE))) { 749 spin_lock(&inode->v.i_lock); 750 i_size_write(&inode->v, end); 751 spin_unlock(&inode->v.i_lock); 752 753 mutex_lock(&inode->ei_update_l 754 ret2 = bch2_write_inode_size(c 755 mutex_unlock(&inode->ei_update 756 } 757 758 return ret ?: ret2; 759 } 760 761 long bch2_fallocate_dispatch(struct file *file 762 loff_t offset, lo 763 { 764 struct bch_inode_info *inode = file_bc 765 struct bch_fs *c = inode->v.i_sb->s_fs 766 long ret; 767 768 if (!bch2_write_ref_tryget(c, BCH_WRIT 769 return -EROFS; 770 771 inode_lock(&inode->v); 772 inode_dio_wait(&inode->v); 773 bch2_pagecache_block_get(inode); 774 775 ret = file_modified(file); 776 if (ret) 777 goto err; 778 779 if (!(mode & ~(FALLOC_FL_KEEP_SIZE|FAL 780 ret = bchfs_fallocate(inode, m 781 else if (mode == (FALLOC_FL_PUNCH_HOLE 782 ret = bchfs_fpunch(inode, offs 783 else if (mode == FALLOC_FL_INSERT_RANG 784 ret = bchfs_fcollapse_finsert( 785 else if (mode == FALLOC_FL_COLLAPSE_RA 786 ret = bchfs_fcollapse_finsert( 787 else 788 ret = -EOPNOTSUPP; 789 err: 790 bch2_pagecache_block_put(inode); 791 inode_unlock(&inode->v); 792 bch2_write_ref_put(c, BCH_WRITE_REF_fa 793 794 return bch2_err_class(ret); 795 } 796 797 /* 798 * Take a quota reservation for unallocated bl 799 * Does not check pagecache 800 */ 801 static int quota_reserve_range(struct bch_inod 802 struct quota_re 803 u64 start, u64 804 { 805 struct bch_fs *c = inode->v.i_sb->s_fs 806 u64 sectors = end - start; 807 808 int ret = bch2_trans_run(c, 809 for_each_btree_key_in_subvolum 810 BTREE_ID_exten 811 POS(inode->v.i 812 POS(inode->v.i 813 inode->ei_inum 814 if (bkey_extent_is_all 815 u64 s = min(en 816 max(st 817 BUG_ON(s > sec 818 sectors -= s; 819 } 820 821 0; 822 }))); 823 824 return ret ?: bch2_quota_reservation_a 825 } 826 827 loff_t bch2_remap_file_range(struct file *file 828 struct file *file 829 loff_t len, unsig 830 { 831 struct bch_inode_info *src = file_bch_ 832 struct bch_inode_info *dst = file_bch_ 833 struct bch_fs *c = src->v.i_sb->s_fs_i 834 struct quota_res quota_res = { 0 }; 835 s64 i_sectors_delta = 0; 836 u64 aligned_len; 837 loff_t ret = 0; 838 839 if (remap_flags & ~(REMAP_FILE_DEDUP|R 840 return -EINVAL; 841 842 if ((pos_src & (block_bytes(c) - 1)) | 843 (pos_dst & (block_bytes(c) - 1))) 844 return -EINVAL; 845 846 if (src == dst && 847 abs(pos_src - pos_dst) < len) 848 return -EINVAL; 849 850 lock_two_nondirectories(&src->v, &dst- 851 bch2_lock_inodes(INODE_PAGECACHE_BLOCK 852 853 inode_dio_wait(&src->v); 854 inode_dio_wait(&dst->v); 855 856 ret = generic_remap_file_range_prep(fi 857 fi 858 &l 859 if (ret < 0 || len == 0) 860 goto err; 861 862 aligned_len = round_up((u64) len, bloc 863 864 ret = bch2_write_invalidate_inode_page 865 pos_dst, pos_d 866 if (ret) 867 goto err; 868 869 ret = quota_reserve_range(dst, "a_ 870 (pos_dst + a 871 if (ret) 872 goto err; 873 874 if (!(remap_flags & REMAP_FILE_DEDUP)) 875 file_update_time(file_dst); 876 877 bch2_mark_pagecache_unallocated(src, p 878 (pos_src + 879 880 ret = bch2_remap_range(c, 881 inode_inum(dst) 882 inode_inum(src) 883 aligned_len >> 884 pos_dst + len, 885 if (ret < 0) 886 goto err; 887 888 /* 889 * due to alignment, we might have rem 890 */ 891 ret = min((u64) ret << 9, (u64) len); 892 893 bch2_i_sectors_acct(c, dst, "a_res 894 895 spin_lock(&dst->v.i_lock); 896 if (pos_dst + ret > dst->v.i_size) 897 i_size_write(&dst->v, pos_dst 898 spin_unlock(&dst->v.i_lock); 899 900 if ((file_dst->f_flags & (__O_SYNC | O 901 IS_SYNC(file_inode(file_dst))) 902 ret = bch2_flush_inode(c, dst) 903 err: 904 bch2_quota_reservation_put(c, dst, &qu 905 bch2_unlock_inodes(INODE_PAGECACHE_BLO 906 unlock_two_nondirectories(&src->v, &ds 907 908 return bch2_err_class(ret); 909 } 910 911 /* fseek: */ 912 913 static loff_t bch2_seek_data(struct file *file 914 { 915 struct bch_inode_info *inode = file_bc 916 struct bch_fs *c = inode->v.i_sb->s_fs 917 subvol_inum inum = inode_inum(inode); 918 u64 isize, next_data = MAX_LFS_FILESIZ 919 920 isize = i_size_read(&inode->v); 921 if (offset >= isize) 922 return -ENXIO; 923 924 int ret = bch2_trans_run(c, 925 for_each_btree_key_in_subvolum 926 POS(inode-> 927 POS(inode-> 928 inum.subvol 929 if (bkey_extent_is_dat 930 next_data = ma 931 break; 932 } else if (k.k->p.offs 933 break; 934 0; 935 }))); 936 if (ret) 937 return ret; 938 939 if (next_data > offset) 940 next_data = bch2_seek_pagecach 941 offset 942 943 if (next_data >= isize) 944 return -ENXIO; 945 946 return vfs_setpos(file, next_data, MAX 947 } 948 949 static loff_t bch2_seek_hole(struct file *file 950 { 951 struct bch_inode_info *inode = file_bc 952 struct bch_fs *c = inode->v.i_sb->s_fs 953 subvol_inum inum = inode_inum(inode); 954 u64 isize, next_hole = MAX_LFS_FILESIZ 955 956 isize = i_size_read(&inode->v); 957 if (offset >= isize) 958 return -ENXIO; 959 960 int ret = bch2_trans_run(c, 961 for_each_btree_key_in_subvolum 962 POS(inode-> 963 POS(inode-> 964 inum.subvol 965 if (k.k->p.inode != in 966 next_hole = bc 967 968 break; 969 } else if (!bkey_exten 970 next_hole = bc 971 972 973 974 if (next_hole 975 break; 976 } else { 977 offset = max(o 978 } 979 0; 980 }))); 981 if (ret) 982 return ret; 983 984 if (next_hole > isize) 985 next_hole = isize; 986 987 return vfs_setpos(file, next_hole, MAX 988 } 989 990 loff_t bch2_llseek(struct file *file, loff_t o 991 { 992 loff_t ret; 993 994 switch (whence) { 995 case SEEK_SET: 996 case SEEK_CUR: 997 case SEEK_END: 998 ret = generic_file_llseek(file 999 break; 1000 case SEEK_DATA: 1001 ret = bch2_seek_data(file, of 1002 break; 1003 case SEEK_HOLE: 1004 ret = bch2_seek_hole(file, of 1005 break; 1006 default: 1007 ret = -EINVAL; 1008 break; 1009 } 1010 1011 return bch2_err_class(ret); 1012 } 1013 1014 void bch2_fs_fsio_exit(struct bch_fs *c) 1015 { 1016 bioset_exit(&c->nocow_flush_bioset); 1017 } 1018 1019 int bch2_fs_fsio_init(struct bch_fs *c) 1020 { 1021 if (bioset_init(&c->nocow_flush_biose 1022 1, offsetof(struct no 1023 return -BCH_ERR_ENOMEM_nocow_ 1024 1025 return 0; 1026 } 1027 1028 #endif /* NO_BCACHEFS_FS */ 1029
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.