11c6fdbd8SKent Overstreet // SPDX-License-Identifier: GPL-2.0 21c6fdbd8SKent Overstreet #ifndef NO_BCACHEFS_FS 31c6fdbd8SKent Overstreet 41c6fdbd8SKent Overstreet #include "bcachefs.h" 57b3f84eaSKent Overstreet #include "alloc_foreground.h" 61809b8cbSKent Overstreet #include "bkey_buf.h" 71c6fdbd8SKent Overstreet #include "btree_update.h" 81c6fdbd8SKent Overstreet #include "buckets.h" 91c6fdbd8SKent Overstreet #include "clock.h" 101c6fdbd8SKent Overstreet #include "error.h" 11e2d9912cSKent Overstreet #include "extents.h" 1208c07feaSKent Overstreet #include "extent_update.h" 131c6fdbd8SKent Overstreet #include "fs.h" 141c6fdbd8SKent Overstreet #include "fs-io.h" 15dbbfca9fSKent Overstreet #include "fs-io-buffered.h" 16dbbfca9fSKent Overstreet #include "fs-io-pagecache.h" 171c6fdbd8SKent Overstreet #include "fsck.h" 181c6fdbd8SKent Overstreet #include "inode.h" 191c6fdbd8SKent Overstreet #include "journal.h" 201809b8cbSKent Overstreet #include "io_misc.h" 211c6fdbd8SKent Overstreet #include "keylist.h" 221c6fdbd8SKent Overstreet #include "quota.h" 2376426098SKent Overstreet #include "reflink.h" 241c6fdbd8SKent Overstreet #include "trace.h" 251c6fdbd8SKent Overstreet 261c6fdbd8SKent Overstreet #include <linux/aio.h> 271c6fdbd8SKent Overstreet #include <linux/backing-dev.h> 281c6fdbd8SKent Overstreet #include <linux/falloc.h> 291c6fdbd8SKent Overstreet #include <linux/migrate.h> 301c6fdbd8SKent Overstreet #include <linux/mmu_context.h> 311c6fdbd8SKent Overstreet #include <linux/pagevec.h> 329ba2eb25SKent Overstreet #include <linux/rmap.h> 331c6fdbd8SKent Overstreet #include <linux/sched/signal.h> 341c6fdbd8SKent Overstreet #include <linux/task_io_accounting_ops.h> 351c6fdbd8SKent Overstreet #include <linux/uio.h> 361c6fdbd8SKent Overstreet 371c6fdbd8SKent Overstreet #include <trace/events/writeback.h> 381c6fdbd8SKent Overstreet 39a8b3a677SKent Overstreet struct nocow_flush { 40a8b3a677SKent Overstreet struct closure *cl; 41a8b3a677SKent Overstreet struct bch_dev *ca; 42a8b3a677SKent Overstreet struct bio bio; 43a8b3a677SKent Overstreet }; 44a8b3a677SKent Overstreet 45a8b3a677SKent Overstreet static void nocow_flush_endio(struct bio *_bio) 46a8b3a677SKent Overstreet { 47a8b3a677SKent Overstreet 48a8b3a677SKent Overstreet struct nocow_flush *bio = container_of(_bio, struct nocow_flush, bio); 49a8b3a677SKent Overstreet 50a8b3a677SKent Overstreet closure_put(bio->cl); 51a8b3a677SKent Overstreet percpu_ref_put(&bio->ca->io_ref); 52a8b3a677SKent Overstreet bio_put(&bio->bio); 53a8b3a677SKent Overstreet } 54a8b3a677SKent Overstreet 55dbbfca9fSKent Overstreet void bch2_inode_flush_nocow_writes_async(struct bch_fs *c, 56a8b3a677SKent Overstreet struct bch_inode_info *inode, 57a8b3a677SKent Overstreet struct closure *cl) 58a8b3a677SKent Overstreet { 59a8b3a677SKent Overstreet struct nocow_flush *bio; 60a8b3a677SKent Overstreet struct bch_dev *ca; 61a8b3a677SKent Overstreet struct bch_devs_mask devs; 62a8b3a677SKent Overstreet unsigned dev; 63a8b3a677SKent Overstreet 64a8b3a677SKent Overstreet dev = find_first_bit(inode->ei_devs_need_flush.d, BCH_SB_MEMBERS_MAX); 65a8b3a677SKent Overstreet if (dev == BCH_SB_MEMBERS_MAX) 66a8b3a677SKent Overstreet return; 67a8b3a677SKent Overstreet 68a8b3a677SKent Overstreet devs = inode->ei_devs_need_flush; 69a8b3a677SKent Overstreet memset(&inode->ei_devs_need_flush, 0, sizeof(inode->ei_devs_need_flush)); 70a8b3a677SKent Overstreet 71a8b3a677SKent Overstreet for_each_set_bit(dev, devs.d, BCH_SB_MEMBERS_MAX) { 72a8b3a677SKent Overstreet rcu_read_lock(); 73a8b3a677SKent Overstreet ca = rcu_dereference(c->devs[dev]); 74a8b3a677SKent Overstreet if (ca && !percpu_ref_tryget(&ca->io_ref)) 75a8b3a677SKent Overstreet ca = NULL; 76a8b3a677SKent Overstreet rcu_read_unlock(); 77a8b3a677SKent Overstreet 78a8b3a677SKent Overstreet if (!ca) 79a8b3a677SKent Overstreet continue; 80a8b3a677SKent Overstreet 81a8b3a677SKent Overstreet bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev, 0, 82a8b3a677SKent Overstreet REQ_OP_FLUSH, 83a8b3a677SKent Overstreet GFP_KERNEL, 84a8b3a677SKent Overstreet &c->nocow_flush_bioset), 85a8b3a677SKent Overstreet struct nocow_flush, bio); 86a8b3a677SKent Overstreet bio->cl = cl; 87a8b3a677SKent Overstreet bio->ca = ca; 88a8b3a677SKent Overstreet bio->bio.bi_end_io = nocow_flush_endio; 89a8b3a677SKent Overstreet closure_bio_submit(&bio->bio, cl); 90a8b3a677SKent Overstreet } 91a8b3a677SKent Overstreet } 92a8b3a677SKent Overstreet 93a8b3a677SKent Overstreet static int bch2_inode_flush_nocow_writes(struct bch_fs *c, 94a8b3a677SKent Overstreet struct bch_inode_info *inode) 95a8b3a677SKent Overstreet { 96a8b3a677SKent Overstreet struct closure cl; 97a8b3a677SKent Overstreet 98a8b3a677SKent Overstreet closure_init_stack(&cl); 99a8b3a677SKent Overstreet bch2_inode_flush_nocow_writes_async(c, inode, &cl); 100a8b3a677SKent Overstreet closure_sync(&cl); 101a8b3a677SKent Overstreet 102a8b3a677SKent Overstreet return 0; 103a8b3a677SKent Overstreet } 104a8b3a677SKent Overstreet 1051c6fdbd8SKent Overstreet /* i_size updates: */ 1061c6fdbd8SKent Overstreet 1072ea90048SKent Overstreet struct inode_new_size { 1082ea90048SKent Overstreet loff_t new_size; 1092ea90048SKent Overstreet u64 now; 1102ea90048SKent Overstreet unsigned fields; 1112ea90048SKent Overstreet }; 1122ea90048SKent Overstreet 113791236b8SJoshua Ashton static int inode_set_size(struct btree_trans *trans, 114791236b8SJoshua Ashton struct bch_inode_info *inode, 1151c6fdbd8SKent Overstreet struct bch_inode_unpacked *bi, 1161c6fdbd8SKent Overstreet void *p) 1171c6fdbd8SKent Overstreet { 1182ea90048SKent Overstreet struct inode_new_size *s = p; 1191c6fdbd8SKent Overstreet 1202ea90048SKent Overstreet bi->bi_size = s->new_size; 1212ea90048SKent Overstreet if (s->fields & ATTR_ATIME) 1222ea90048SKent Overstreet bi->bi_atime = s->now; 1232ea90048SKent Overstreet if (s->fields & ATTR_MTIME) 1242ea90048SKent Overstreet bi->bi_mtime = s->now; 1252ea90048SKent Overstreet if (s->fields & ATTR_CTIME) 1262ea90048SKent Overstreet bi->bi_ctime = s->now; 1271c6fdbd8SKent Overstreet 1281c6fdbd8SKent Overstreet return 0; 1291c6fdbd8SKent Overstreet } 1301c6fdbd8SKent Overstreet 13176426098SKent Overstreet int __must_check bch2_write_inode_size(struct bch_fs *c, 1321c6fdbd8SKent Overstreet struct bch_inode_info *inode, 1332ea90048SKent Overstreet loff_t new_size, unsigned fields) 1341c6fdbd8SKent Overstreet { 1352ea90048SKent Overstreet struct inode_new_size s = { 1362ea90048SKent Overstreet .new_size = new_size, 1372ea90048SKent Overstreet .now = bch2_current_time(c), 1382ea90048SKent Overstreet .fields = fields, 1392ea90048SKent Overstreet }; 1402ea90048SKent Overstreet 1412ea90048SKent Overstreet return bch2_write_inode(c, inode, inode_set_size, &s, fields); 1421c6fdbd8SKent Overstreet } 1431c6fdbd8SKent Overstreet 144dbbfca9fSKent Overstreet void __bch2_i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode, 145190fa7afSKent Overstreet struct quota_res *quota_res, s64 sectors) 1461c6fdbd8SKent Overstreet { 147b33bf1bcSKent Overstreet bch2_fs_inconsistent_on((s64) inode->v.i_blocks + sectors < 0, c, 148b33bf1bcSKent Overstreet "inode %lu i_blocks underflow: %llu + %lli < 0 (ondisk %lli)", 149b33bf1bcSKent Overstreet inode->v.i_ino, (u64) inode->v.i_blocks, sectors, 150b33bf1bcSKent Overstreet inode->ei_inode.bi_sectors); 151b44a66a6SKent Overstreet inode->v.i_blocks += sectors; 152b44a66a6SKent Overstreet 1531c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA 154cb1b479dSKent Overstreet if (quota_res && 155cb1b479dSKent Overstreet !test_bit(EI_INODE_SNAPSHOT, &inode->ei_flags) && 156cb1b479dSKent Overstreet sectors > 0) { 1571c6fdbd8SKent Overstreet BUG_ON(sectors > quota_res->sectors); 1581c6fdbd8SKent Overstreet BUG_ON(sectors > inode->ei_quota_reserved); 1591c6fdbd8SKent Overstreet 1601c6fdbd8SKent Overstreet quota_res->sectors -= sectors; 1611c6fdbd8SKent Overstreet inode->ei_quota_reserved -= sectors; 1621c6fdbd8SKent Overstreet } else { 16326609b61SKent Overstreet bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors, KEY_TYPE_QUOTA_WARN); 1641c6fdbd8SKent Overstreet } 1651c6fdbd8SKent Overstreet #endif 1666b1b186aSKent Overstreet } 1676b1b186aSKent Overstreet 1681c6fdbd8SKent Overstreet /* fsync: */ 1691c6fdbd8SKent Overstreet 17068a2054dSKent Overstreet /* 17168a2054dSKent Overstreet * inode->ei_inode.bi_journal_seq won't be up to date since it's set in an 17268a2054dSKent Overstreet * insert trigger: look up the btree inode instead 17368a2054dSKent Overstreet */ 174a8b3a677SKent Overstreet static int bch2_flush_inode(struct bch_fs *c, 175a8b3a677SKent Overstreet struct bch_inode_info *inode) 17668a2054dSKent Overstreet { 177a8b3a677SKent Overstreet struct bch_inode_unpacked u; 17868a2054dSKent Overstreet int ret; 17968a2054dSKent Overstreet 18068a2054dSKent Overstreet if (c->opts.journal_flush_disabled) 18168a2054dSKent Overstreet return 0; 18268a2054dSKent Overstreet 183a8b3a677SKent Overstreet ret = bch2_inode_find_by_inum(c, inode_inum(inode), &u); 18468a2054dSKent Overstreet if (ret) 18568a2054dSKent Overstreet return ret; 18668a2054dSKent Overstreet 187a8b3a677SKent Overstreet return bch2_journal_flush_seq(&c->journal, u.bi_journal_seq) ?: 188a8b3a677SKent Overstreet bch2_inode_flush_nocow_writes(c, inode); 18968a2054dSKent Overstreet } 19068a2054dSKent Overstreet 1911c6fdbd8SKent Overstreet int bch2_fsync(struct file *file, loff_t start, loff_t end, int datasync) 1921c6fdbd8SKent Overstreet { 1931c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 1941c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 19568a2054dSKent Overstreet int ret, ret2, ret3; 1961c6fdbd8SKent Overstreet 1971c6fdbd8SKent Overstreet ret = file_write_and_wait_range(file, start, end); 19868a2054dSKent Overstreet ret2 = sync_inode_metadata(&inode->v, 1); 199a8b3a677SKent Overstreet ret3 = bch2_flush_inode(c, inode); 2001c6fdbd8SKent Overstreet 2015c1ef830SKent Overstreet return bch2_err_class(ret ?: ret2 ?: ret3); 2021c6fdbd8SKent Overstreet } 2031c6fdbd8SKent Overstreet 2041c6fdbd8SKent Overstreet /* truncate: */ 2051c6fdbd8SKent Overstreet 2066fed42bbSKent Overstreet static inline int range_has_data(struct bch_fs *c, u32 subvol, 2071c6fdbd8SKent Overstreet struct bpos start, 2081c6fdbd8SKent Overstreet struct bpos end) 2091c6fdbd8SKent Overstreet { 210424eb881SKent Overstreet struct btree_trans trans; 21167e0dd8fSKent Overstreet struct btree_iter iter; 2121c6fdbd8SKent Overstreet struct bkey_s_c k; 2131c6fdbd8SKent Overstreet int ret = 0; 2141c6fdbd8SKent Overstreet 21520bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 2166fed42bbSKent Overstreet retry: 2176fed42bbSKent Overstreet bch2_trans_begin(&trans); 2186fed42bbSKent Overstreet 2196fed42bbSKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, subvol, &start.snapshot); 2206fed42bbSKent Overstreet if (ret) 2216fed42bbSKent Overstreet goto err; 222424eb881SKent Overstreet 223c72f687aSKent Overstreet for_each_btree_key_upto_norestart(&trans, iter, BTREE_ID_extents, start, end, 0, k, ret) 2244ad6aa46SBrian Foster if (bkey_extent_is_data(k.k) && !bkey_extent_is_unwritten(k)) { 2251c6fdbd8SKent Overstreet ret = 1; 2261c6fdbd8SKent Overstreet break; 2271c6fdbd8SKent Overstreet } 2286fed42bbSKent Overstreet start = iter.pos; 22967e0dd8fSKent Overstreet bch2_trans_iter_exit(&trans, &iter); 2306fed42bbSKent Overstreet err: 231549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 2326fed42bbSKent Overstreet goto retry; 2331c6fdbd8SKent Overstreet 2349a796fdbSKent Overstreet bch2_trans_exit(&trans); 2359a796fdbSKent Overstreet return ret; 2361c6fdbd8SKent Overstreet } 2371c6fdbd8SKent Overstreet 238959f7368SKent Overstreet static int __bch2_truncate_folio(struct bch_inode_info *inode, 2391c6fdbd8SKent Overstreet pgoff_t index, loff_t start, loff_t end) 2401c6fdbd8SKent Overstreet { 2411c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 2421c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 2433342ac13SKent Overstreet struct bch_folio *s; 2441c6fdbd8SKent Overstreet unsigned start_offset = start & (PAGE_SIZE - 1); 2451c6fdbd8SKent Overstreet unsigned end_offset = ((end - 1) & (PAGE_SIZE - 1)) + 1; 246a99b1cafSKent Overstreet unsigned i; 24730bff594SKent Overstreet struct folio *folio; 248b19d307dSKent Overstreet s64 i_sectors_delta = 0; 2491c6fdbd8SKent Overstreet int ret = 0; 2506b9857b2SBrian Foster u64 end_pos; 2511c6fdbd8SKent Overstreet 25230bff594SKent Overstreet folio = filemap_lock_folio(mapping, index); 253b6898917SKent Overstreet if (IS_ERR_OR_NULL(folio)) { 2541c6fdbd8SKent Overstreet /* 2551c6fdbd8SKent Overstreet * XXX: we're doing two index lookups when we end up reading the 25630bff594SKent Overstreet * folio 2571c6fdbd8SKent Overstreet */ 2586fed42bbSKent Overstreet ret = range_has_data(c, inode->ei_subvol, 259c72f687aSKent Overstreet POS(inode->v.i_ino, (index << PAGE_SECTORS_SHIFT)), 260c72f687aSKent Overstreet POS(inode->v.i_ino, (index << PAGE_SECTORS_SHIFT) + PAGE_SECTORS)); 2611c6fdbd8SKent Overstreet if (ret <= 0) 2621c6fdbd8SKent Overstreet return ret; 2631c6fdbd8SKent Overstreet 26430bff594SKent Overstreet folio = __filemap_get_folio(mapping, index, 26530bff594SKent Overstreet FGP_LOCK|FGP_CREAT, GFP_KERNEL); 2661e81f89bSKent Overstreet if (IS_ERR_OR_NULL(folio)) { 2671c6fdbd8SKent Overstreet ret = -ENOMEM; 2681c6fdbd8SKent Overstreet goto out; 2691c6fdbd8SKent Overstreet } 2701c6fdbd8SKent Overstreet } 2711c6fdbd8SKent Overstreet 272959f7368SKent Overstreet BUG_ON(start >= folio_end_pos(folio)); 273959f7368SKent Overstreet BUG_ON(end <= folio_pos(folio)); 274959f7368SKent Overstreet 275959f7368SKent Overstreet start_offset = max(start, folio_pos(folio)) - folio_pos(folio); 2766b9857b2SBrian Foster end_offset = min_t(u64, end, folio_end_pos(folio)) - folio_pos(folio); 277959f7368SKent Overstreet 278959f7368SKent Overstreet /* Folio boundary? Nothing to do */ 279959f7368SKent Overstreet if (start_offset == 0 && 280959f7368SKent Overstreet end_offset == folio_size(folio)) { 281959f7368SKent Overstreet ret = 0; 282959f7368SKent Overstreet goto unlock; 283959f7368SKent Overstreet } 284959f7368SKent Overstreet 28530bff594SKent Overstreet s = bch2_folio_create(folio, 0); 286a99b1cafSKent Overstreet if (!s) { 287a99b1cafSKent Overstreet ret = -ENOMEM; 288a99b1cafSKent Overstreet goto unlock; 289a99b1cafSKent Overstreet } 290a99b1cafSKent Overstreet 29130bff594SKent Overstreet if (!folio_test_uptodate(folio)) { 29230bff594SKent Overstreet ret = bch2_read_single_folio(folio, mapping); 2931c6fdbd8SKent Overstreet if (ret) 2941c6fdbd8SKent Overstreet goto unlock; 2951c6fdbd8SKent Overstreet } 2961c6fdbd8SKent Overstreet 29734fdcf06SKent Overstreet ret = bch2_folio_set(c, inode_inum(inode), &folio, 1); 29834fdcf06SKent Overstreet if (ret) 29934fdcf06SKent Overstreet goto unlock; 300c437e153SKent Overstreet 301a99b1cafSKent Overstreet for (i = round_up(start_offset, block_bytes(c)) >> 9; 302a99b1cafSKent Overstreet i < round_down(end_offset, block_bytes(c)) >> 9; 303a99b1cafSKent Overstreet i++) { 304a99b1cafSKent Overstreet s->s[i].nr_replicas = 0; 305a1774a05SKent Overstreet 306a1774a05SKent Overstreet i_sectors_delta -= s->s[i].state == SECTOR_dirty; 307dbbfca9fSKent Overstreet bch2_folio_sector_set(folio, s, i, SECTOR_unallocated); 308a99b1cafSKent Overstreet } 309a99b1cafSKent Overstreet 310dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta); 311b19d307dSKent Overstreet 31274163da7SKent Overstreet /* 31330bff594SKent Overstreet * Caller needs to know whether this folio will be written out by 31474163da7SKent Overstreet * writeback - doing an i_size update if necessary - or whether it will 3154ad6aa46SBrian Foster * be responsible for the i_size update. 3164ad6aa46SBrian Foster * 3174ad6aa46SBrian Foster * Note that we shouldn't ever see a folio beyond EOF, but check and 3184ad6aa46SBrian Foster * warn if so. This has been observed by failure to clean up folios 3194ad6aa46SBrian Foster * after a short write and there's still a chance reclaim will fix 3204ad6aa46SBrian Foster * things up. 32174163da7SKent Overstreet */ 3224ad6aa46SBrian Foster WARN_ON_ONCE(folio_pos(folio) >= inode->v.i_size); 3234ad6aa46SBrian Foster end_pos = folio_end_pos(folio); 3244ad6aa46SBrian Foster if (inode->v.i_size > folio_pos(folio)) 3256b9857b2SBrian Foster end_pos = min_t(u64, inode->v.i_size, end_pos); 326bf98ee10SBrian Foster ret = s->s[folio_pos_to_s(folio, end_pos - 1)].state >= SECTOR_dirty; 32774163da7SKent Overstreet 32830bff594SKent Overstreet folio_zero_segment(folio, start_offset, end_offset); 329a99b1cafSKent Overstreet 3301c6fdbd8SKent Overstreet /* 3311c6fdbd8SKent Overstreet * Bit of a hack - we don't want truncate to fail due to -ENOSPC. 3321c6fdbd8SKent Overstreet * 33330bff594SKent Overstreet * XXX: because we aren't currently tracking whether the folio has actual 3341c6fdbd8SKent Overstreet * data in it (vs. just 0s, or only partially written) this wrong. ick. 3351c6fdbd8SKent Overstreet */ 33630bff594SKent Overstreet BUG_ON(bch2_get_folio_disk_reservation(c, inode, folio, false)); 3371c6fdbd8SKent Overstreet 3389ba2eb25SKent Overstreet /* 3399ba2eb25SKent Overstreet * This removes any writeable userspace mappings; we need to force 3409ba2eb25SKent Overstreet * .page_mkwrite to be called again before any mmapped writes, to 3419ba2eb25SKent Overstreet * redirty the full page: 3429ba2eb25SKent Overstreet */ 34330bff594SKent Overstreet folio_mkclean(folio); 34430bff594SKent Overstreet filemap_dirty_folio(mapping, folio); 3451c6fdbd8SKent Overstreet unlock: 34630bff594SKent Overstreet folio_unlock(folio); 34730bff594SKent Overstreet folio_put(folio); 3481c6fdbd8SKent Overstreet out: 3491c6fdbd8SKent Overstreet return ret; 3501c6fdbd8SKent Overstreet } 3511c6fdbd8SKent Overstreet 352959f7368SKent Overstreet static int bch2_truncate_folio(struct bch_inode_info *inode, loff_t from) 3531c6fdbd8SKent Overstreet { 354959f7368SKent Overstreet return __bch2_truncate_folio(inode, from >> PAGE_SHIFT, 355959f7368SKent Overstreet from, ANYSINT_MAX(loff_t)); 3561c6fdbd8SKent Overstreet } 3571c6fdbd8SKent Overstreet 358959f7368SKent Overstreet static int bch2_truncate_folios(struct bch_inode_info *inode, 35974163da7SKent Overstreet loff_t start, loff_t end) 36074163da7SKent Overstreet { 361959f7368SKent Overstreet int ret = __bch2_truncate_folio(inode, start >> PAGE_SHIFT, 36274163da7SKent Overstreet start, end); 36374163da7SKent Overstreet 36474163da7SKent Overstreet if (ret >= 0 && 36574163da7SKent Overstreet start >> PAGE_SHIFT != end >> PAGE_SHIFT) 366959f7368SKent Overstreet ret = __bch2_truncate_folio(inode, 367959f7368SKent Overstreet (end - 1) >> PAGE_SHIFT, 36874163da7SKent Overstreet start, end); 36974163da7SKent Overstreet return ret; 37074163da7SKent Overstreet } 37174163da7SKent Overstreet 37268a507a2SKent Overstreet static int bch2_extend(struct mnt_idmap *idmap, 37368a507a2SKent Overstreet struct bch_inode_info *inode, 374e0541a93SKent Overstreet struct bch_inode_unpacked *inode_u, 375e0541a93SKent Overstreet struct iattr *iattr) 3761c6fdbd8SKent Overstreet { 3771c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 3781c6fdbd8SKent Overstreet int ret; 3791c6fdbd8SKent Overstreet 380e0541a93SKent Overstreet /* 381e0541a93SKent Overstreet * sync appends: 3822925fc49SKent Overstreet * 3832925fc49SKent Overstreet * this has to be done _before_ extending i_size: 384e0541a93SKent Overstreet */ 385e0541a93SKent Overstreet ret = filemap_write_and_wait_range(mapping, inode_u->bi_size, S64_MAX); 3861c6fdbd8SKent Overstreet if (ret) 3871c6fdbd8SKent Overstreet return ret; 3881c6fdbd8SKent Overstreet 3891c6fdbd8SKent Overstreet truncate_setsize(&inode->v, iattr->ia_size); 3901c6fdbd8SKent Overstreet 39168a507a2SKent Overstreet return bch2_setattr_nonsize(idmap, inode, iattr); 3921c6fdbd8SKent Overstreet } 3931c6fdbd8SKent Overstreet 394*5902cc28SKent Overstreet int bchfs_truncate(struct mnt_idmap *idmap, 39568a507a2SKent Overstreet struct bch_inode_info *inode, struct iattr *iattr) 3961c6fdbd8SKent Overstreet { 3971c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 3981c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 399e0541a93SKent Overstreet struct bch_inode_unpacked inode_u; 4002e87eae1SKent Overstreet s64 i_sectors_delta = 0; 4011c6fdbd8SKent Overstreet int ret = 0; 4021c6fdbd8SKent Overstreet 40368a507a2SKent Overstreet /* 40478d66ab1SDan Robertson * If the truncate call with change the size of the file, the 40578d66ab1SDan Robertson * cmtimes should be updated. If the size will not change, we 40678d66ab1SDan Robertson * do not need to update the cmtimes. 40768a507a2SKent Overstreet */ 40878d66ab1SDan Robertson if (iattr->ia_size != inode->v.i_size) { 40968a507a2SKent Overstreet if (!(iattr->ia_valid & ATTR_MTIME)) 41068a507a2SKent Overstreet ktime_get_coarse_real_ts64(&iattr->ia_mtime); 41168a507a2SKent Overstreet if (!(iattr->ia_valid & ATTR_CTIME)) 41268a507a2SKent Overstreet ktime_get_coarse_real_ts64(&iattr->ia_ctime); 41368a507a2SKent Overstreet iattr->ia_valid |= ATTR_MTIME|ATTR_CTIME; 41478d66ab1SDan Robertson } 41568a507a2SKent Overstreet 4161c6fdbd8SKent Overstreet inode_dio_wait(&inode->v); 417a7ecd30cSKent Overstreet bch2_pagecache_block_get(inode); 4181c6fdbd8SKent Overstreet 4196fed42bbSKent Overstreet ret = bch2_inode_find_by_inum(c, inode_inum(inode), &inode_u); 420e0541a93SKent Overstreet if (ret) 421e0541a93SKent Overstreet goto err; 4221c6fdbd8SKent Overstreet 423c45d473dSKent Overstreet /* 424c45d473dSKent Overstreet * check this before next assertion; on filesystem error our normal 425c45d473dSKent Overstreet * invariants are a bit broken (truncate has to truncate the page cache 426c45d473dSKent Overstreet * before the inode). 427c45d473dSKent Overstreet */ 428c45d473dSKent Overstreet ret = bch2_journal_error(&c->journal); 429c45d473dSKent Overstreet if (ret) 430c45d473dSKent Overstreet goto err; 431c45d473dSKent Overstreet 4328eb71e9eSKent Overstreet WARN_ONCE(!test_bit(EI_INODE_ERROR, &inode->ei_flags) && 4338eb71e9eSKent Overstreet inode->v.i_size < inode_u.bi_size, 4348eb71e9eSKent Overstreet "truncate spotted in mem i_size < btree i_size: %llu < %llu\n", 4358eb71e9eSKent Overstreet (u64) inode->v.i_size, inode_u.bi_size); 436e0541a93SKent Overstreet 437e0541a93SKent Overstreet if (iattr->ia_size > inode->v.i_size) { 43868a507a2SKent Overstreet ret = bch2_extend(idmap, inode, &inode_u, iattr); 43954e2264eSKent Overstreet goto err; 4401c6fdbd8SKent Overstreet } 4411c6fdbd8SKent Overstreet 44268a507a2SKent Overstreet iattr->ia_valid &= ~ATTR_SIZE; 44368a507a2SKent Overstreet 444959f7368SKent Overstreet ret = bch2_truncate_folio(inode, iattr->ia_size); 44574163da7SKent Overstreet if (unlikely(ret < 0)) 44654e2264eSKent Overstreet goto err; 4471c6fdbd8SKent Overstreet 448*5902cc28SKent Overstreet truncate_setsize(&inode->v, iattr->ia_size); 449*5902cc28SKent Overstreet 4506cc3535dSKent Overstreet /* 4516cc3535dSKent Overstreet * When extending, we're going to write the new i_size to disk 4526cc3535dSKent Overstreet * immediately so we need to flush anything above the current on disk 4536cc3535dSKent Overstreet * i_size first: 4546cc3535dSKent Overstreet * 4556cc3535dSKent Overstreet * Also, when extending we need to flush the page that i_size currently 4566cc3535dSKent Overstreet * straddles - if it's mapped to userspace, we need to ensure that 4576cc3535dSKent Overstreet * userspace has to redirty it and call .mkwrite -> set_page_dirty 4586cc3535dSKent Overstreet * again to allocate the part of the page that was extended. 4596cc3535dSKent Overstreet */ 460e0541a93SKent Overstreet if (iattr->ia_size > inode_u.bi_size) 4611c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 462e0541a93SKent Overstreet inode_u.bi_size, 4631c6fdbd8SKent Overstreet iattr->ia_size - 1); 4641c6fdbd8SKent Overstreet else if (iattr->ia_size & (PAGE_SIZE - 1)) 4651c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 4661c6fdbd8SKent Overstreet round_down(iattr->ia_size, PAGE_SIZE), 4671c6fdbd8SKent Overstreet iattr->ia_size - 1); 4681c6fdbd8SKent Overstreet if (ret) 46954e2264eSKent Overstreet goto err; 4701c6fdbd8SKent Overstreet 471*5902cc28SKent Overstreet ret = bch2_truncate(c, inode_inum(inode), iattr->ia_size, &i_sectors_delta); 472dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta); 4732e87eae1SKent Overstreet 474*5902cc28SKent Overstreet if (unlikely(ret)) { 475*5902cc28SKent Overstreet /* 476*5902cc28SKent Overstreet * If we error here, VFS caches are now inconsistent with btree 477*5902cc28SKent Overstreet */ 478*5902cc28SKent Overstreet set_bit(EI_INODE_ERROR, &inode->ei_flags); 479*5902cc28SKent Overstreet goto err; 480*5902cc28SKent Overstreet } 481*5902cc28SKent Overstreet 482b33bf1bcSKent Overstreet bch2_fs_inconsistent_on(!inode->v.i_size && inode->v.i_blocks && 483b33bf1bcSKent Overstreet !bch2_journal_error(&c->journal), c, 484b33bf1bcSKent Overstreet "inode %lu truncated to 0 but i_blocks %llu (ondisk %lli)", 485b33bf1bcSKent Overstreet inode->v.i_ino, (u64) inode->v.i_blocks, 486b33bf1bcSKent Overstreet inode->ei_inode.bi_sectors); 48768a507a2SKent Overstreet 48868a507a2SKent Overstreet ret = bch2_setattr_nonsize(idmap, inode, iattr); 48954e2264eSKent Overstreet err: 490a7ecd30cSKent Overstreet bch2_pagecache_block_put(inode); 4915c1ef830SKent Overstreet return bch2_err_class(ret); 4921c6fdbd8SKent Overstreet } 4931c6fdbd8SKent Overstreet 4941c6fdbd8SKent Overstreet /* fallocate: */ 4951c6fdbd8SKent Overstreet 496791236b8SJoshua Ashton static int inode_update_times_fn(struct btree_trans *trans, 497791236b8SJoshua Ashton struct bch_inode_info *inode, 498050197b1SKent Overstreet struct bch_inode_unpacked *bi, void *p) 499050197b1SKent Overstreet { 500050197b1SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 501050197b1SKent Overstreet 502050197b1SKent Overstreet bi->bi_mtime = bi->bi_ctime = bch2_current_time(c); 503050197b1SKent Overstreet return 0; 504050197b1SKent Overstreet } 505050197b1SKent Overstreet 5062e87eae1SKent Overstreet static long bchfs_fpunch(struct bch_inode_info *inode, loff_t offset, loff_t len) 5071c6fdbd8SKent Overstreet { 5081c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 50974163da7SKent Overstreet u64 end = offset + len; 51074163da7SKent Overstreet u64 block_start = round_up(offset, block_bytes(c)); 51174163da7SKent Overstreet u64 block_end = round_down(end, block_bytes(c)); 51274163da7SKent Overstreet bool truncated_last_page; 5131c6fdbd8SKent Overstreet int ret = 0; 5141c6fdbd8SKent Overstreet 515959f7368SKent Overstreet ret = bch2_truncate_folios(inode, offset, end); 51674163da7SKent Overstreet if (unlikely(ret < 0)) 5171c6fdbd8SKent Overstreet goto err; 5181c6fdbd8SKent Overstreet 51974163da7SKent Overstreet truncated_last_page = ret; 5201c6fdbd8SKent Overstreet 52174163da7SKent Overstreet truncate_pagecache_range(&inode->v, offset, end - 1); 5221c6fdbd8SKent Overstreet 52374163da7SKent Overstreet if (block_start < block_end) { 5242e87eae1SKent Overstreet s64 i_sectors_delta = 0; 5252e87eae1SKent Overstreet 5268c6d298aSKent Overstreet ret = bch2_fpunch(c, inode_inum(inode), 52774163da7SKent Overstreet block_start >> 9, block_end >> 9, 5282e87eae1SKent Overstreet &i_sectors_delta); 529dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta); 5302e87eae1SKent Overstreet } 531050197b1SKent Overstreet 532050197b1SKent Overstreet mutex_lock(&inode->ei_update_lock); 53374163da7SKent Overstreet if (end >= inode->v.i_size && !truncated_last_page) { 53474163da7SKent Overstreet ret = bch2_write_inode_size(c, inode, inode->v.i_size, 53574163da7SKent Overstreet ATTR_MTIME|ATTR_CTIME); 53674163da7SKent Overstreet } else { 537050197b1SKent Overstreet ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL, 53874163da7SKent Overstreet ATTR_MTIME|ATTR_CTIME); 53974163da7SKent Overstreet } 540050197b1SKent Overstreet mutex_unlock(&inode->ei_update_lock); 5411c6fdbd8SKent Overstreet err: 5421c6fdbd8SKent Overstreet return ret; 5431c6fdbd8SKent Overstreet } 5441c6fdbd8SKent Overstreet 5452e87eae1SKent Overstreet static long bchfs_fcollapse_finsert(struct bch_inode_info *inode, 5465f786787SKent Overstreet loff_t offset, loff_t len, 5475f786787SKent Overstreet bool insert) 5481c6fdbd8SKent Overstreet { 5491c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 5501c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 551*5902cc28SKent Overstreet s64 i_sectors_delta = 0; 55250dc0f69SKent Overstreet int ret = 0; 5531c6fdbd8SKent Overstreet 5541c6fdbd8SKent Overstreet if ((offset | len) & (block_bytes(c) - 1)) 5551c6fdbd8SKent Overstreet return -EINVAL; 5561c6fdbd8SKent Overstreet 5575f786787SKent Overstreet if (insert) { 5585f786787SKent Overstreet if (offset >= inode->v.i_size) 55974163da7SKent Overstreet return -EINVAL; 5605f786787SKent Overstreet } else { 5611c6fdbd8SKent Overstreet if (offset + len >= inode->v.i_size) 56274163da7SKent Overstreet return -EINVAL; 5635f786787SKent Overstreet } 5641c6fdbd8SKent Overstreet 565dbbfca9fSKent Overstreet ret = bch2_write_invalidate_inode_pages_range(mapping, offset, LLONG_MAX); 5661c6fdbd8SKent Overstreet if (ret) 56774163da7SKent Overstreet return ret; 5681c6fdbd8SKent Overstreet 569*5902cc28SKent Overstreet if (insert) 570*5902cc28SKent Overstreet i_size_write(&inode->v, inode->v.i_size + len); 5712e87eae1SKent Overstreet 572*5902cc28SKent Overstreet ret = bch2_fcollapse_finsert(c, inode_inum(inode), offset >> 9, len >> 9, 573*5902cc28SKent Overstreet insert, &i_sectors_delta); 574*5902cc28SKent Overstreet if (!ret && !insert) 575*5902cc28SKent Overstreet i_size_write(&inode->v, inode->v.i_size - len); 576dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta); 5772e87eae1SKent Overstreet 5781c6fdbd8SKent Overstreet return ret; 5791c6fdbd8SKent Overstreet } 5801c6fdbd8SKent Overstreet 581694015c2SKent Overstreet static int __bchfs_fallocate(struct bch_inode_info *inode, int mode, 582694015c2SKent Overstreet u64 start_sector, u64 end_sector) 5831c6fdbd8SKent Overstreet { 5841c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 585190fa7afSKent Overstreet struct btree_trans trans; 58667e0dd8fSKent Overstreet struct btree_iter iter; 587694015c2SKent Overstreet struct bpos end_pos = POS(inode->v.i_ino, end_sector); 58801ad6737SKent Overstreet struct bch_io_opts opts; 589694015c2SKent Overstreet int ret = 0; 5901c6fdbd8SKent Overstreet 59101ad6737SKent Overstreet bch2_inode_opts_get(&opts, c, &inode->ei_inode); 592f7beb4caSKent Overstreet bch2_trans_init(&trans, c, BTREE_ITER_MAX, 512); 5931c6fdbd8SKent Overstreet 59467e0dd8fSKent Overstreet bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents, 595694015c2SKent Overstreet POS(inode->v.i_ino, start_sector), 596190fa7afSKent Overstreet BTREE_ITER_SLOTS|BTREE_ITER_INTENT); 5971c6fdbd8SKent Overstreet 598e88a75ebSKent Overstreet while (!ret && bkey_lt(iter.pos, end_pos)) { 5992e87eae1SKent Overstreet s64 i_sectors_delta = 0; 600190fa7afSKent Overstreet struct quota_res quota_res = { 0 }; 6011c6fdbd8SKent Overstreet struct bkey_s_c k; 602694015c2SKent Overstreet unsigned sectors; 603a09818c7SKent Overstreet bool is_allocation; 604a09818c7SKent Overstreet u64 hole_start, hole_end; 6056fed42bbSKent Overstreet u32 snapshot; 6061c6fdbd8SKent Overstreet 607163e885aSKent Overstreet bch2_trans_begin(&trans); 608a8abd3a7SKent Overstreet 6096fed42bbSKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, 6106fed42bbSKent Overstreet inode->ei_subvol, &snapshot); 6116fed42bbSKent Overstreet if (ret) 6126fed42bbSKent Overstreet goto bkey_err; 6136fed42bbSKent Overstreet 6146fed42bbSKent Overstreet bch2_btree_iter_set_snapshot(&iter, snapshot); 6156fed42bbSKent Overstreet 61667e0dd8fSKent Overstreet k = bch2_btree_iter_peek_slot(&iter); 6170f238367SKent Overstreet if ((ret = bkey_err(k))) 6180f238367SKent Overstreet goto bkey_err; 6191c6fdbd8SKent Overstreet 620a09818c7SKent Overstreet hole_start = iter.pos.offset; 621a09818c7SKent Overstreet hole_end = bpos_min(k.k->p, end_pos).offset; 622a09818c7SKent Overstreet is_allocation = bkey_extent_is_allocation(k.k); 623a09818c7SKent Overstreet 6241c6fdbd8SKent Overstreet /* already reserved */ 62579203111SKent Overstreet if (bkey_extent_is_reservation(k) && 62679203111SKent Overstreet bch2_bkey_nr_ptrs_fully_allocated(k) >= opts.data_replicas) { 62767e0dd8fSKent Overstreet bch2_btree_iter_advance(&iter); 6281c6fdbd8SKent Overstreet continue; 6291c6fdbd8SKent Overstreet } 6301c6fdbd8SKent Overstreet 631190fa7afSKent Overstreet if (bkey_extent_is_data(k.k) && 632190fa7afSKent Overstreet !(mode & FALLOC_FL_ZERO_RANGE)) { 63367e0dd8fSKent Overstreet bch2_btree_iter_advance(&iter); 6341c6fdbd8SKent Overstreet continue; 6351c6fdbd8SKent Overstreet } 6361c6fdbd8SKent Overstreet 637a09818c7SKent Overstreet if (!(mode & FALLOC_FL_ZERO_RANGE)) { 6384198bf03SKent Overstreet /* 6394198bf03SKent Overstreet * Lock ordering - can't be holding btree locks while 6404198bf03SKent Overstreet * blocking on a folio lock: 6414198bf03SKent Overstreet */ 6424198bf03SKent Overstreet if (bch2_clamp_data_hole(&inode->v, 6434198bf03SKent Overstreet &hole_start, 6444198bf03SKent Overstreet &hole_end, 6454198bf03SKent Overstreet opts.data_replicas, true)) 646a09818c7SKent Overstreet ret = drop_locks_do(&trans, 647a09818c7SKent Overstreet (bch2_clamp_data_hole(&inode->v, 648a09818c7SKent Overstreet &hole_start, 649a09818c7SKent Overstreet &hole_end, 6504198bf03SKent Overstreet opts.data_replicas, false), 0)); 651a09818c7SKent Overstreet bch2_btree_iter_set_pos(&iter, POS(iter.pos.inode, hole_start)); 652a8b3a677SKent Overstreet 653a09818c7SKent Overstreet if (ret) 654a09818c7SKent Overstreet goto bkey_err; 6551c6fdbd8SKent Overstreet 656a09818c7SKent Overstreet if (hole_start == hole_end) 657a09818c7SKent Overstreet continue; 658a09818c7SKent Overstreet } 659a09818c7SKent Overstreet 660a09818c7SKent Overstreet sectors = hole_end - hole_start; 661a09818c7SKent Overstreet 662a09818c7SKent Overstreet if (!is_allocation) { 6631c6fdbd8SKent Overstreet ret = bch2_quota_reservation_add(c, inode, 664a09818c7SKent Overstreet "a_res, sectors, true); 6651c6fdbd8SKent Overstreet if (unlikely(ret)) 6660f238367SKent Overstreet goto bkey_err; 6671c6fdbd8SKent Overstreet } 6681c6fdbd8SKent Overstreet 66970de7a47SKent Overstreet ret = bch2_extent_fallocate(&trans, inode_inum(inode), &iter, 67070de7a47SKent Overstreet sectors, opts, &i_sectors_delta, 67170de7a47SKent Overstreet writepoint_hashed((unsigned long) current)); 6728810386fSKent Overstreet if (ret) 6738810386fSKent Overstreet goto bkey_err; 67470de7a47SKent Overstreet 675dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, "a_res, i_sectors_delta); 676a09818c7SKent Overstreet 677a09818c7SKent Overstreet drop_locks_do(&trans, 678dbbfca9fSKent Overstreet (bch2_mark_pagecache_reserved(inode, hole_start, iter.pos.offset), 0)); 6790f238367SKent Overstreet bkey_err: 680190fa7afSKent Overstreet bch2_quota_reservation_put(c, inode, "a_res); 681549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 6821c6fdbd8SKent Overstreet ret = 0; 68350dc0f69SKent Overstreet } 68474163da7SKent Overstreet 685098ef98dSKent Overstreet if (bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE)) { 68674163da7SKent Overstreet struct quota_res quota_res = { 0 }; 68774163da7SKent Overstreet s64 i_sectors_delta = 0; 68874163da7SKent Overstreet 68974163da7SKent Overstreet bch2_fpunch_at(&trans, &iter, inode_inum(inode), 69074163da7SKent Overstreet end_sector, &i_sectors_delta); 691dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, "a_res, i_sectors_delta); 69274163da7SKent Overstreet bch2_quota_reservation_put(c, inode, "a_res); 69374163da7SKent Overstreet } 69474163da7SKent Overstreet 69567e0dd8fSKent Overstreet bch2_trans_iter_exit(&trans, &iter); 696694015c2SKent Overstreet bch2_trans_exit(&trans); 697694015c2SKent Overstreet return ret; 698694015c2SKent Overstreet } 69950dc0f69SKent Overstreet 700694015c2SKent Overstreet static long bchfs_fallocate(struct bch_inode_info *inode, int mode, 701694015c2SKent Overstreet loff_t offset, loff_t len) 702694015c2SKent Overstreet { 703694015c2SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 70474163da7SKent Overstreet u64 end = offset + len; 70574163da7SKent Overstreet u64 block_start = round_down(offset, block_bytes(c)); 70674163da7SKent Overstreet u64 block_end = round_up(end, block_bytes(c)); 70774163da7SKent Overstreet bool truncated_last_page = false; 70874163da7SKent Overstreet int ret, ret2 = 0; 709694015c2SKent Overstreet 710694015c2SKent Overstreet if (!(mode & FALLOC_FL_KEEP_SIZE) && end > inode->v.i_size) { 711694015c2SKent Overstreet ret = inode_newsize_ok(&inode->v, end); 712694015c2SKent Overstreet if (ret) 71374163da7SKent Overstreet return ret; 714694015c2SKent Overstreet } 715694015c2SKent Overstreet 716694015c2SKent Overstreet if (mode & FALLOC_FL_ZERO_RANGE) { 717959f7368SKent Overstreet ret = bch2_truncate_folios(inode, offset, end); 71874163da7SKent Overstreet if (unlikely(ret < 0)) 71974163da7SKent Overstreet return ret; 720694015c2SKent Overstreet 72174163da7SKent Overstreet truncated_last_page = ret; 722694015c2SKent Overstreet 723694015c2SKent Overstreet truncate_pagecache_range(&inode->v, offset, end - 1); 72474163da7SKent Overstreet 72574163da7SKent Overstreet block_start = round_up(offset, block_bytes(c)); 72674163da7SKent Overstreet block_end = round_down(end, block_bytes(c)); 727694015c2SKent Overstreet } 728694015c2SKent Overstreet 729694015c2SKent Overstreet ret = __bchfs_fallocate(inode, mode, block_start >> 9, block_end >> 9); 730e0541a93SKent Overstreet 731e0541a93SKent Overstreet /* 73274163da7SKent Overstreet * On -ENOSPC in ZERO_RANGE mode, we still want to do the inode update, 73374163da7SKent Overstreet * so that the VFS cache i_size is consistent with the btree i_size: 734e0541a93SKent Overstreet */ 73574163da7SKent Overstreet if (ret && 736098ef98dSKent Overstreet !(bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE))) 73774163da7SKent Overstreet return ret; 7381c6fdbd8SKent Overstreet 73974163da7SKent Overstreet if (mode & FALLOC_FL_KEEP_SIZE && end > inode->v.i_size) 740e0541a93SKent Overstreet end = inode->v.i_size; 74174163da7SKent Overstreet 74274163da7SKent Overstreet if (end >= inode->v.i_size && 74374163da7SKent Overstreet (((mode & FALLOC_FL_ZERO_RANGE) && !truncated_last_page) || 74474163da7SKent Overstreet !(mode & FALLOC_FL_KEEP_SIZE))) { 74574163da7SKent Overstreet spin_lock(&inode->v.i_lock); 746e0541a93SKent Overstreet i_size_write(&inode->v, end); 74774163da7SKent Overstreet spin_unlock(&inode->v.i_lock); 748e0541a93SKent Overstreet 7491c6fdbd8SKent Overstreet mutex_lock(&inode->ei_update_lock); 75074163da7SKent Overstreet ret2 = bch2_write_inode_size(c, inode, end, 0); 7511c6fdbd8SKent Overstreet mutex_unlock(&inode->ei_update_lock); 7521c6fdbd8SKent Overstreet } 75374163da7SKent Overstreet 75474163da7SKent Overstreet return ret ?: ret2; 7551c6fdbd8SKent Overstreet } 7561c6fdbd8SKent Overstreet 7571c6fdbd8SKent Overstreet long bch2_fallocate_dispatch(struct file *file, int mode, 7581c6fdbd8SKent Overstreet loff_t offset, loff_t len) 7591c6fdbd8SKent Overstreet { 7601c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 7612a9101a9SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 7622a9101a9SKent Overstreet long ret; 7632a9101a9SKent Overstreet 764d94189adSKent Overstreet if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_fallocate)) 7652a9101a9SKent Overstreet return -EROFS; 7661c6fdbd8SKent Overstreet 76774163da7SKent Overstreet inode_lock(&inode->v); 76874163da7SKent Overstreet inode_dio_wait(&inode->v); 769a7ecd30cSKent Overstreet bch2_pagecache_block_get(inode); 77074163da7SKent Overstreet 77107bfcc0bSKent Overstreet ret = file_modified(file); 77207bfcc0bSKent Overstreet if (ret) 77307bfcc0bSKent Overstreet goto err; 77407bfcc0bSKent Overstreet 7751c6fdbd8SKent Overstreet if (!(mode & ~(FALLOC_FL_KEEP_SIZE|FALLOC_FL_ZERO_RANGE))) 7762a9101a9SKent Overstreet ret = bchfs_fallocate(inode, mode, offset, len); 7772a9101a9SKent Overstreet else if (mode == (FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE)) 7782a9101a9SKent Overstreet ret = bchfs_fpunch(inode, offset, len); 7792a9101a9SKent Overstreet else if (mode == FALLOC_FL_INSERT_RANGE) 7802a9101a9SKent Overstreet ret = bchfs_fcollapse_finsert(inode, offset, len, true); 7812a9101a9SKent Overstreet else if (mode == FALLOC_FL_COLLAPSE_RANGE) 7822a9101a9SKent Overstreet ret = bchfs_fcollapse_finsert(inode, offset, len, false); 7832a9101a9SKent Overstreet else 7842a9101a9SKent Overstreet ret = -EOPNOTSUPP; 78507bfcc0bSKent Overstreet err: 786a7ecd30cSKent Overstreet bch2_pagecache_block_put(inode); 78774163da7SKent Overstreet inode_unlock(&inode->v); 788d94189adSKent Overstreet bch2_write_ref_put(c, BCH_WRITE_REF_fallocate); 7891c6fdbd8SKent Overstreet 7905c1ef830SKent Overstreet return bch2_err_class(ret); 7911c6fdbd8SKent Overstreet } 7921c6fdbd8SKent Overstreet 793c72f687aSKent Overstreet /* 794c72f687aSKent Overstreet * Take a quota reservation for unallocated blocks in a given file range 795c72f687aSKent Overstreet * Does not check pagecache 796c72f687aSKent Overstreet */ 797e8540e56SKent Overstreet static int quota_reserve_range(struct bch_inode_info *inode, 798e8540e56SKent Overstreet struct quota_res *res, 799e8540e56SKent Overstreet u64 start, u64 end) 800e8540e56SKent Overstreet { 801e8540e56SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 802e8540e56SKent Overstreet struct btree_trans trans; 803e8540e56SKent Overstreet struct btree_iter iter; 804e8540e56SKent Overstreet struct bkey_s_c k; 805e8540e56SKent Overstreet u32 snapshot; 806e8540e56SKent Overstreet u64 sectors = end - start; 807e8540e56SKent Overstreet u64 pos = start; 808e8540e56SKent Overstreet int ret; 809e8540e56SKent Overstreet 810e8540e56SKent Overstreet bch2_trans_init(&trans, c, 0, 0); 811e8540e56SKent Overstreet retry: 812e8540e56SKent Overstreet bch2_trans_begin(&trans); 813e8540e56SKent Overstreet 814e8540e56SKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, inode->ei_subvol, &snapshot); 815e8540e56SKent Overstreet if (ret) 816e8540e56SKent Overstreet goto err; 817e8540e56SKent Overstreet 818e8540e56SKent Overstreet bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents, 819e8540e56SKent Overstreet SPOS(inode->v.i_ino, pos, snapshot), 0); 820e8540e56SKent Overstreet 821e8540e56SKent Overstreet while (!(ret = btree_trans_too_many_iters(&trans)) && 822e8540e56SKent Overstreet (k = bch2_btree_iter_peek_upto(&iter, POS(inode->v.i_ino, end - 1))).k && 823e8540e56SKent Overstreet !(ret = bkey_err(k))) { 824e8540e56SKent Overstreet if (bkey_extent_is_allocation(k.k)) { 825e8540e56SKent Overstreet u64 s = min(end, k.k->p.offset) - 826e8540e56SKent Overstreet max(start, bkey_start_offset(k.k)); 827e8540e56SKent Overstreet BUG_ON(s > sectors); 828e8540e56SKent Overstreet sectors -= s; 829e8540e56SKent Overstreet } 830e8540e56SKent Overstreet bch2_btree_iter_advance(&iter); 831e8540e56SKent Overstreet } 832e8540e56SKent Overstreet pos = iter.pos.offset; 833e8540e56SKent Overstreet bch2_trans_iter_exit(&trans, &iter); 834e8540e56SKent Overstreet err: 835e8540e56SKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 836e8540e56SKent Overstreet goto retry; 837e8540e56SKent Overstreet 838e8540e56SKent Overstreet bch2_trans_exit(&trans); 839e8540e56SKent Overstreet 840e8540e56SKent Overstreet if (ret) 841e8540e56SKent Overstreet return ret; 842e8540e56SKent Overstreet 843e8540e56SKent Overstreet return bch2_quota_reservation_add(c, inode, res, sectors, true); 844e8540e56SKent Overstreet } 845e8540e56SKent Overstreet 84676426098SKent Overstreet loff_t bch2_remap_file_range(struct file *file_src, loff_t pos_src, 84776426098SKent Overstreet struct file *file_dst, loff_t pos_dst, 84876426098SKent Overstreet loff_t len, unsigned remap_flags) 84976426098SKent Overstreet { 85076426098SKent Overstreet struct bch_inode_info *src = file_bch_inode(file_src); 85176426098SKent Overstreet struct bch_inode_info *dst = file_bch_inode(file_dst); 85276426098SKent Overstreet struct bch_fs *c = src->v.i_sb->s_fs_info; 853e8540e56SKent Overstreet struct quota_res quota_res = { 0 }; 8542e87eae1SKent Overstreet s64 i_sectors_delta = 0; 855677fc056SKent Overstreet u64 aligned_len; 85676426098SKent Overstreet loff_t ret = 0; 85776426098SKent Overstreet 85876426098SKent Overstreet if (remap_flags & ~(REMAP_FILE_DEDUP|REMAP_FILE_ADVISORY)) 85976426098SKent Overstreet return -EINVAL; 86076426098SKent Overstreet 86176426098SKent Overstreet if (remap_flags & REMAP_FILE_DEDUP) 86276426098SKent Overstreet return -EOPNOTSUPP; 86376426098SKent Overstreet 86476426098SKent Overstreet if ((pos_src & (block_bytes(c) - 1)) || 86576426098SKent Overstreet (pos_dst & (block_bytes(c) - 1))) 86676426098SKent Overstreet return -EINVAL; 86776426098SKent Overstreet 86876426098SKent Overstreet if (src == dst && 86976426098SKent Overstreet abs(pos_src - pos_dst) < len) 87076426098SKent Overstreet return -EINVAL; 87176426098SKent Overstreet 87276426098SKent Overstreet bch2_lock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst); 87376426098SKent Overstreet 87476426098SKent Overstreet inode_dio_wait(&src->v); 87576426098SKent Overstreet inode_dio_wait(&dst->v); 87676426098SKent Overstreet 87776426098SKent Overstreet ret = generic_remap_file_range_prep(file_src, pos_src, 87876426098SKent Overstreet file_dst, pos_dst, 87976426098SKent Overstreet &len, remap_flags); 88076426098SKent Overstreet if (ret < 0 || len == 0) 8812e87eae1SKent Overstreet goto err; 88276426098SKent Overstreet 883677fc056SKent Overstreet aligned_len = round_up((u64) len, block_bytes(c)); 88476426098SKent Overstreet 885dbbfca9fSKent Overstreet ret = bch2_write_invalidate_inode_pages_range(dst->v.i_mapping, 886677fc056SKent Overstreet pos_dst, pos_dst + len - 1); 88776426098SKent Overstreet if (ret) 8882e87eae1SKent Overstreet goto err; 88976426098SKent Overstreet 890e8540e56SKent Overstreet ret = quota_reserve_range(dst, "a_res, pos_dst >> 9, 891e8540e56SKent Overstreet (pos_dst + aligned_len) >> 9); 892e8540e56SKent Overstreet if (ret) 893e8540e56SKent Overstreet goto err; 894e8540e56SKent Overstreet 895e8540e56SKent Overstreet file_update_time(file_dst); 896e8540e56SKent Overstreet 897dbbfca9fSKent Overstreet bch2_mark_pagecache_unallocated(src, pos_src >> 9, 898dcfc593fSKent Overstreet (pos_src + aligned_len) >> 9); 89976426098SKent Overstreet 9002e87eae1SKent Overstreet ret = bch2_remap_range(c, 9016fed42bbSKent Overstreet inode_inum(dst), pos_dst >> 9, 9026fed42bbSKent Overstreet inode_inum(src), pos_src >> 9, 90376426098SKent Overstreet aligned_len >> 9, 9042e87eae1SKent Overstreet pos_dst + len, &i_sectors_delta); 9052e87eae1SKent Overstreet if (ret < 0) 9062e87eae1SKent Overstreet goto err; 90776426098SKent Overstreet 9082e87eae1SKent Overstreet /* 9092e87eae1SKent Overstreet * due to alignment, we might have remapped slightly more than requsted 9102e87eae1SKent Overstreet */ 911677fc056SKent Overstreet ret = min((u64) ret << 9, (u64) len); 9122e87eae1SKent Overstreet 913dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, dst, "a_res, i_sectors_delta); 9142e87eae1SKent Overstreet 9152e87eae1SKent Overstreet spin_lock(&dst->v.i_lock); 916677fc056SKent Overstreet if (pos_dst + ret > dst->v.i_size) 917677fc056SKent Overstreet i_size_write(&dst->v, pos_dst + ret); 9182e87eae1SKent Overstreet spin_unlock(&dst->v.i_lock); 919e7084c9cSKent Overstreet 92068a2054dSKent Overstreet if ((file_dst->f_flags & (__O_SYNC | O_DSYNC)) || 92168a2054dSKent Overstreet IS_SYNC(file_inode(file_dst))) 922a8b3a677SKent Overstreet ret = bch2_flush_inode(c, dst); 9232e87eae1SKent Overstreet err: 924e8540e56SKent Overstreet bch2_quota_reservation_put(c, dst, "a_res); 92576426098SKent Overstreet bch2_unlock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst); 92676426098SKent Overstreet 9275c1ef830SKent Overstreet return bch2_err_class(ret); 92876426098SKent Overstreet } 92976426098SKent Overstreet 9301c6fdbd8SKent Overstreet /* fseek: */ 9311c6fdbd8SKent Overstreet 9321c6fdbd8SKent Overstreet static loff_t bch2_seek_data(struct file *file, u64 offset) 9331c6fdbd8SKent Overstreet { 9341c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 9351c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 936424eb881SKent Overstreet struct btree_trans trans; 93767e0dd8fSKent Overstreet struct btree_iter iter; 9381c6fdbd8SKent Overstreet struct bkey_s_c k; 9396fed42bbSKent Overstreet subvol_inum inum = inode_inum(inode); 9401c6fdbd8SKent Overstreet u64 isize, next_data = MAX_LFS_FILESIZE; 9416fed42bbSKent Overstreet u32 snapshot; 9421c6fdbd8SKent Overstreet int ret; 9431c6fdbd8SKent Overstreet 9441c6fdbd8SKent Overstreet isize = i_size_read(&inode->v); 9451c6fdbd8SKent Overstreet if (offset >= isize) 9461c6fdbd8SKent Overstreet return -ENXIO; 9471c6fdbd8SKent Overstreet 94820bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 9496fed42bbSKent Overstreet retry: 9506fed42bbSKent Overstreet bch2_trans_begin(&trans); 9516fed42bbSKent Overstreet 9526fed42bbSKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot); 9536fed42bbSKent Overstreet if (ret) 9546fed42bbSKent Overstreet goto err; 955424eb881SKent Overstreet 956c72f687aSKent Overstreet for_each_btree_key_upto_norestart(&trans, iter, BTREE_ID_extents, 957c72f687aSKent Overstreet SPOS(inode->v.i_ino, offset >> 9, snapshot), 958c72f687aSKent Overstreet POS(inode->v.i_ino, U64_MAX), 959c72f687aSKent Overstreet 0, k, ret) { 960c72f687aSKent Overstreet if (bkey_extent_is_data(k.k)) { 9611c6fdbd8SKent Overstreet next_data = max(offset, bkey_start_offset(k.k) << 9); 9621c6fdbd8SKent Overstreet break; 9631c6fdbd8SKent Overstreet } else if (k.k->p.offset >> 9 > isize) 9641c6fdbd8SKent Overstreet break; 9651c6fdbd8SKent Overstreet } 96667e0dd8fSKent Overstreet bch2_trans_iter_exit(&trans, &iter); 9676fed42bbSKent Overstreet err: 968549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 9696fed42bbSKent Overstreet goto retry; 9701c6fdbd8SKent Overstreet 9719a796fdbSKent Overstreet bch2_trans_exit(&trans); 9721c6fdbd8SKent Overstreet if (ret) 9731c6fdbd8SKent Overstreet return ret; 9741c6fdbd8SKent Overstreet 9751c6fdbd8SKent Overstreet if (next_data > offset) 976543ef2ebSKent Overstreet next_data = bch2_seek_pagecache_data(&inode->v, 9774198bf03SKent Overstreet offset, next_data, 0, false); 9781c6fdbd8SKent Overstreet 979e10d3094SKent Overstreet if (next_data >= isize) 9801c6fdbd8SKent Overstreet return -ENXIO; 9811c6fdbd8SKent Overstreet 9821c6fdbd8SKent Overstreet return vfs_setpos(file, next_data, MAX_LFS_FILESIZE); 9831c6fdbd8SKent Overstreet } 9841c6fdbd8SKent Overstreet 9851c6fdbd8SKent Overstreet static loff_t bch2_seek_hole(struct file *file, u64 offset) 9861c6fdbd8SKent Overstreet { 9871c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 9881c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 989424eb881SKent Overstreet struct btree_trans trans; 99067e0dd8fSKent Overstreet struct btree_iter iter; 9911c6fdbd8SKent Overstreet struct bkey_s_c k; 9926fed42bbSKent Overstreet subvol_inum inum = inode_inum(inode); 9931c6fdbd8SKent Overstreet u64 isize, next_hole = MAX_LFS_FILESIZE; 9946fed42bbSKent Overstreet u32 snapshot; 9951c6fdbd8SKent Overstreet int ret; 9961c6fdbd8SKent Overstreet 9971c6fdbd8SKent Overstreet isize = i_size_read(&inode->v); 9981c6fdbd8SKent Overstreet if (offset >= isize) 9991c6fdbd8SKent Overstreet return -ENXIO; 10001c6fdbd8SKent Overstreet 100120bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 10026fed42bbSKent Overstreet retry: 10036fed42bbSKent Overstreet bch2_trans_begin(&trans); 10046fed42bbSKent Overstreet 10056fed42bbSKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot); 10066fed42bbSKent Overstreet if (ret) 10076fed42bbSKent Overstreet goto err; 1008424eb881SKent Overstreet 1009e5fa91d7SKent Overstreet for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents, 10106fed42bbSKent Overstreet SPOS(inode->v.i_ino, offset >> 9, snapshot), 101194f651e2SKent Overstreet BTREE_ITER_SLOTS, k, ret) { 10121c6fdbd8SKent Overstreet if (k.k->p.inode != inode->v.i_ino) { 1013543ef2ebSKent Overstreet next_hole = bch2_seek_pagecache_hole(&inode->v, 10144198bf03SKent Overstreet offset, MAX_LFS_FILESIZE, 0, false); 10151c6fdbd8SKent Overstreet break; 10161c6fdbd8SKent Overstreet } else if (!bkey_extent_is_data(k.k)) { 1017543ef2ebSKent Overstreet next_hole = bch2_seek_pagecache_hole(&inode->v, 10181c6fdbd8SKent Overstreet max(offset, bkey_start_offset(k.k) << 9), 10194198bf03SKent Overstreet k.k->p.offset << 9, 0, false); 10201c6fdbd8SKent Overstreet 10211c6fdbd8SKent Overstreet if (next_hole < k.k->p.offset << 9) 10221c6fdbd8SKent Overstreet break; 10231c6fdbd8SKent Overstreet } else { 10241c6fdbd8SKent Overstreet offset = max(offset, bkey_start_offset(k.k) << 9); 10251c6fdbd8SKent Overstreet } 10261c6fdbd8SKent Overstreet } 102767e0dd8fSKent Overstreet bch2_trans_iter_exit(&trans, &iter); 10286fed42bbSKent Overstreet err: 1029549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 10306fed42bbSKent Overstreet goto retry; 10311c6fdbd8SKent Overstreet 10329a796fdbSKent Overstreet bch2_trans_exit(&trans); 10331c6fdbd8SKent Overstreet if (ret) 10341c6fdbd8SKent Overstreet return ret; 10351c6fdbd8SKent Overstreet 10361c6fdbd8SKent Overstreet if (next_hole > isize) 10371c6fdbd8SKent Overstreet next_hole = isize; 10381c6fdbd8SKent Overstreet 10391c6fdbd8SKent Overstreet return vfs_setpos(file, next_hole, MAX_LFS_FILESIZE); 10401c6fdbd8SKent Overstreet } 10411c6fdbd8SKent Overstreet 10421c6fdbd8SKent Overstreet loff_t bch2_llseek(struct file *file, loff_t offset, int whence) 10431c6fdbd8SKent Overstreet { 10445c1ef830SKent Overstreet loff_t ret; 10455c1ef830SKent Overstreet 10461c6fdbd8SKent Overstreet switch (whence) { 10471c6fdbd8SKent Overstreet case SEEK_SET: 10481c6fdbd8SKent Overstreet case SEEK_CUR: 10491c6fdbd8SKent Overstreet case SEEK_END: 10505c1ef830SKent Overstreet ret = generic_file_llseek(file, offset, whence); 10515c1ef830SKent Overstreet break; 10521c6fdbd8SKent Overstreet case SEEK_DATA: 10535c1ef830SKent Overstreet ret = bch2_seek_data(file, offset); 10545c1ef830SKent Overstreet break; 10551c6fdbd8SKent Overstreet case SEEK_HOLE: 10565c1ef830SKent Overstreet ret = bch2_seek_hole(file, offset); 10575c1ef830SKent Overstreet break; 10585c1ef830SKent Overstreet default: 10595c1ef830SKent Overstreet ret = -EINVAL; 10605c1ef830SKent Overstreet break; 10611c6fdbd8SKent Overstreet } 10621c6fdbd8SKent Overstreet 10635c1ef830SKent Overstreet return bch2_err_class(ret); 10641c6fdbd8SKent Overstreet } 10651c6fdbd8SKent Overstreet 10661c6fdbd8SKent Overstreet void bch2_fs_fsio_exit(struct bch_fs *c) 10671c6fdbd8SKent Overstreet { 1068a8b3a677SKent Overstreet bioset_exit(&c->nocow_flush_bioset); 10691c6fdbd8SKent Overstreet } 10701c6fdbd8SKent Overstreet 10711c6fdbd8SKent Overstreet int bch2_fs_fsio_init(struct bch_fs *c) 10721c6fdbd8SKent Overstreet { 107365d48e35SKent Overstreet if (bioset_init(&c->nocow_flush_bioset, 1074a8b3a677SKent Overstreet 1, offsetof(struct nocow_flush, bio), 0)) 107565d48e35SKent Overstreet return -BCH_ERR_ENOMEM_nocow_flush_bioset_init; 10761c6fdbd8SKent Overstreet 1077c8b4534dSKent Overstreet return 0; 10781c6fdbd8SKent Overstreet } 10791c6fdbd8SKent Overstreet 10801c6fdbd8SKent Overstreet #endif /* NO_BCACHEFS_FS */ 1081