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, 82*3e44f325SChristoph Hellwig REQ_OP_WRITE|REQ_PREFLUSH, 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; 1955a11b5feSBrian Foster int ret; 1961c6fdbd8SKent Overstreet 1971c6fdbd8SKent Overstreet ret = file_write_and_wait_range(file, start, end); 1985a11b5feSBrian Foster if (ret) 1995a11b5feSBrian Foster goto out; 2005a11b5feSBrian Foster ret = sync_inode_metadata(&inode->v, 1); 2015a11b5feSBrian Foster if (ret) 2025a11b5feSBrian Foster goto out; 2035a11b5feSBrian Foster ret = bch2_flush_inode(c, inode); 2045a11b5feSBrian Foster out: 2055a11b5feSBrian Foster return bch2_err_class(ret); 2061c6fdbd8SKent Overstreet } 2071c6fdbd8SKent Overstreet 2081c6fdbd8SKent Overstreet /* truncate: */ 2091c6fdbd8SKent Overstreet 2106fed42bbSKent Overstreet static inline int range_has_data(struct bch_fs *c, u32 subvol, 2111c6fdbd8SKent Overstreet struct bpos start, 2121c6fdbd8SKent Overstreet struct bpos end) 2131c6fdbd8SKent Overstreet { 2146bd68ec2SKent Overstreet struct btree_trans *trans = bch2_trans_get(c); 21567e0dd8fSKent Overstreet struct btree_iter iter; 2161c6fdbd8SKent Overstreet struct bkey_s_c k; 2171c6fdbd8SKent Overstreet int ret = 0; 2186fed42bbSKent Overstreet retry: 2196bd68ec2SKent Overstreet bch2_trans_begin(trans); 2206fed42bbSKent Overstreet 2216bd68ec2SKent Overstreet ret = bch2_subvolume_get_snapshot(trans, subvol, &start.snapshot); 2226fed42bbSKent Overstreet if (ret) 2236fed42bbSKent Overstreet goto err; 224424eb881SKent Overstreet 2256bd68ec2SKent Overstreet for_each_btree_key_upto_norestart(trans, iter, BTREE_ID_extents, start, end, 0, k, ret) 2264ad6aa46SBrian Foster if (bkey_extent_is_data(k.k) && !bkey_extent_is_unwritten(k)) { 2271c6fdbd8SKent Overstreet ret = 1; 2281c6fdbd8SKent Overstreet break; 2291c6fdbd8SKent Overstreet } 2306fed42bbSKent Overstreet start = iter.pos; 2316bd68ec2SKent Overstreet bch2_trans_iter_exit(trans, &iter); 2326fed42bbSKent Overstreet err: 233549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 2346fed42bbSKent Overstreet goto retry; 2351c6fdbd8SKent Overstreet 2366bd68ec2SKent Overstreet bch2_trans_put(trans); 2379a796fdbSKent Overstreet return ret; 2381c6fdbd8SKent Overstreet } 2391c6fdbd8SKent Overstreet 240959f7368SKent Overstreet static int __bch2_truncate_folio(struct bch_inode_info *inode, 2411c6fdbd8SKent Overstreet pgoff_t index, loff_t start, loff_t end) 2421c6fdbd8SKent Overstreet { 2431c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 2441c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 2453342ac13SKent Overstreet struct bch_folio *s; 246c04cbc0dSColin Ian King unsigned start_offset; 247c04cbc0dSColin Ian King unsigned end_offset; 248a99b1cafSKent Overstreet unsigned i; 24930bff594SKent Overstreet struct folio *folio; 250b19d307dSKent Overstreet s64 i_sectors_delta = 0; 2511c6fdbd8SKent Overstreet int ret = 0; 2526b9857b2SBrian Foster u64 end_pos; 2531c6fdbd8SKent Overstreet 25430bff594SKent Overstreet folio = filemap_lock_folio(mapping, index); 255b6898917SKent Overstreet if (IS_ERR_OR_NULL(folio)) { 2561c6fdbd8SKent Overstreet /* 2571c6fdbd8SKent Overstreet * XXX: we're doing two index lookups when we end up reading the 25830bff594SKent Overstreet * folio 2591c6fdbd8SKent Overstreet */ 2606fed42bbSKent Overstreet ret = range_has_data(c, inode->ei_subvol, 261c72f687aSKent Overstreet POS(inode->v.i_ino, (index << PAGE_SECTORS_SHIFT)), 262c72f687aSKent Overstreet POS(inode->v.i_ino, (index << PAGE_SECTORS_SHIFT) + PAGE_SECTORS)); 2631c6fdbd8SKent Overstreet if (ret <= 0) 2641c6fdbd8SKent Overstreet return ret; 2651c6fdbd8SKent Overstreet 26630bff594SKent Overstreet folio = __filemap_get_folio(mapping, index, 26730bff594SKent Overstreet FGP_LOCK|FGP_CREAT, GFP_KERNEL); 2681e81f89bSKent Overstreet if (IS_ERR_OR_NULL(folio)) { 2691c6fdbd8SKent Overstreet ret = -ENOMEM; 2701c6fdbd8SKent Overstreet goto out; 2711c6fdbd8SKent Overstreet } 2721c6fdbd8SKent Overstreet } 2731c6fdbd8SKent Overstreet 274959f7368SKent Overstreet BUG_ON(start >= folio_end_pos(folio)); 275959f7368SKent Overstreet BUG_ON(end <= folio_pos(folio)); 276959f7368SKent Overstreet 277959f7368SKent Overstreet start_offset = max(start, folio_pos(folio)) - folio_pos(folio); 2786b9857b2SBrian Foster end_offset = min_t(u64, end, folio_end_pos(folio)) - folio_pos(folio); 279959f7368SKent Overstreet 280959f7368SKent Overstreet /* Folio boundary? Nothing to do */ 281959f7368SKent Overstreet if (start_offset == 0 && 282959f7368SKent Overstreet end_offset == folio_size(folio)) { 283959f7368SKent Overstreet ret = 0; 284959f7368SKent Overstreet goto unlock; 285959f7368SKent Overstreet } 286959f7368SKent Overstreet 28730bff594SKent Overstreet s = bch2_folio_create(folio, 0); 288a99b1cafSKent Overstreet if (!s) { 289a99b1cafSKent Overstreet ret = -ENOMEM; 290a99b1cafSKent Overstreet goto unlock; 291a99b1cafSKent Overstreet } 292a99b1cafSKent Overstreet 29330bff594SKent Overstreet if (!folio_test_uptodate(folio)) { 29430bff594SKent Overstreet ret = bch2_read_single_folio(folio, mapping); 2951c6fdbd8SKent Overstreet if (ret) 2961c6fdbd8SKent Overstreet goto unlock; 2971c6fdbd8SKent Overstreet } 2981c6fdbd8SKent Overstreet 29934fdcf06SKent Overstreet ret = bch2_folio_set(c, inode_inum(inode), &folio, 1); 30034fdcf06SKent Overstreet if (ret) 30134fdcf06SKent Overstreet goto unlock; 302c437e153SKent Overstreet 303a99b1cafSKent Overstreet for (i = round_up(start_offset, block_bytes(c)) >> 9; 304a99b1cafSKent Overstreet i < round_down(end_offset, block_bytes(c)) >> 9; 305a99b1cafSKent Overstreet i++) { 306a99b1cafSKent Overstreet s->s[i].nr_replicas = 0; 307a1774a05SKent Overstreet 308a1774a05SKent Overstreet i_sectors_delta -= s->s[i].state == SECTOR_dirty; 309dbbfca9fSKent Overstreet bch2_folio_sector_set(folio, s, i, SECTOR_unallocated); 310a99b1cafSKent Overstreet } 311a99b1cafSKent Overstreet 312dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta); 313b19d307dSKent Overstreet 31474163da7SKent Overstreet /* 31530bff594SKent Overstreet * Caller needs to know whether this folio will be written out by 31674163da7SKent Overstreet * writeback - doing an i_size update if necessary - or whether it will 3174ad6aa46SBrian Foster * be responsible for the i_size update. 3184ad6aa46SBrian Foster * 3194ad6aa46SBrian Foster * Note that we shouldn't ever see a folio beyond EOF, but check and 3204ad6aa46SBrian Foster * warn if so. This has been observed by failure to clean up folios 3214ad6aa46SBrian Foster * after a short write and there's still a chance reclaim will fix 3224ad6aa46SBrian Foster * things up. 32374163da7SKent Overstreet */ 3244ad6aa46SBrian Foster WARN_ON_ONCE(folio_pos(folio) >= inode->v.i_size); 3254ad6aa46SBrian Foster end_pos = folio_end_pos(folio); 3264ad6aa46SBrian Foster if (inode->v.i_size > folio_pos(folio)) 3276b9857b2SBrian Foster end_pos = min_t(u64, inode->v.i_size, end_pos); 328bf98ee10SBrian Foster ret = s->s[folio_pos_to_s(folio, end_pos - 1)].state >= SECTOR_dirty; 32974163da7SKent Overstreet 33030bff594SKent Overstreet folio_zero_segment(folio, start_offset, end_offset); 331a99b1cafSKent Overstreet 3321c6fdbd8SKent Overstreet /* 3331c6fdbd8SKent Overstreet * Bit of a hack - we don't want truncate to fail due to -ENOSPC. 3341c6fdbd8SKent Overstreet * 33530bff594SKent Overstreet * XXX: because we aren't currently tracking whether the folio has actual 3361c6fdbd8SKent Overstreet * data in it (vs. just 0s, or only partially written) this wrong. ick. 3371c6fdbd8SKent Overstreet */ 33830bff594SKent Overstreet BUG_ON(bch2_get_folio_disk_reservation(c, inode, folio, false)); 3391c6fdbd8SKent Overstreet 3409ba2eb25SKent Overstreet /* 3419ba2eb25SKent Overstreet * This removes any writeable userspace mappings; we need to force 3429ba2eb25SKent Overstreet * .page_mkwrite to be called again before any mmapped writes, to 3439ba2eb25SKent Overstreet * redirty the full page: 3449ba2eb25SKent Overstreet */ 34530bff594SKent Overstreet folio_mkclean(folio); 34630bff594SKent Overstreet filemap_dirty_folio(mapping, folio); 3471c6fdbd8SKent Overstreet unlock: 34830bff594SKent Overstreet folio_unlock(folio); 34930bff594SKent Overstreet folio_put(folio); 3501c6fdbd8SKent Overstreet out: 3511c6fdbd8SKent Overstreet return ret; 3521c6fdbd8SKent Overstreet } 3531c6fdbd8SKent Overstreet 354959f7368SKent Overstreet static int bch2_truncate_folio(struct bch_inode_info *inode, loff_t from) 3551c6fdbd8SKent Overstreet { 356959f7368SKent Overstreet return __bch2_truncate_folio(inode, from >> PAGE_SHIFT, 357959f7368SKent Overstreet from, ANYSINT_MAX(loff_t)); 3581c6fdbd8SKent Overstreet } 3591c6fdbd8SKent Overstreet 360959f7368SKent Overstreet static int bch2_truncate_folios(struct bch_inode_info *inode, 36174163da7SKent Overstreet loff_t start, loff_t end) 36274163da7SKent Overstreet { 363959f7368SKent Overstreet int ret = __bch2_truncate_folio(inode, start >> PAGE_SHIFT, 36474163da7SKent Overstreet start, end); 36574163da7SKent Overstreet 36674163da7SKent Overstreet if (ret >= 0 && 36774163da7SKent Overstreet start >> PAGE_SHIFT != end >> PAGE_SHIFT) 368959f7368SKent Overstreet ret = __bch2_truncate_folio(inode, 369959f7368SKent Overstreet (end - 1) >> PAGE_SHIFT, 37074163da7SKent Overstreet start, end); 37174163da7SKent Overstreet return ret; 37274163da7SKent Overstreet } 37374163da7SKent Overstreet 37468a507a2SKent Overstreet static int bch2_extend(struct mnt_idmap *idmap, 37568a507a2SKent Overstreet struct bch_inode_info *inode, 376e0541a93SKent Overstreet struct bch_inode_unpacked *inode_u, 377e0541a93SKent Overstreet struct iattr *iattr) 3781c6fdbd8SKent Overstreet { 3791c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 3801c6fdbd8SKent Overstreet int ret; 3811c6fdbd8SKent Overstreet 382e0541a93SKent Overstreet /* 383e0541a93SKent Overstreet * sync appends: 3842925fc49SKent Overstreet * 3852925fc49SKent Overstreet * this has to be done _before_ extending i_size: 386e0541a93SKent Overstreet */ 387e0541a93SKent Overstreet ret = filemap_write_and_wait_range(mapping, inode_u->bi_size, S64_MAX); 3881c6fdbd8SKent Overstreet if (ret) 3891c6fdbd8SKent Overstreet return ret; 3901c6fdbd8SKent Overstreet 3911c6fdbd8SKent Overstreet truncate_setsize(&inode->v, iattr->ia_size); 3921c6fdbd8SKent Overstreet 39368a507a2SKent Overstreet return bch2_setattr_nonsize(idmap, inode, iattr); 3941c6fdbd8SKent Overstreet } 3951c6fdbd8SKent Overstreet 3965902cc28SKent Overstreet int bchfs_truncate(struct mnt_idmap *idmap, 39768a507a2SKent Overstreet struct bch_inode_info *inode, struct iattr *iattr) 3981c6fdbd8SKent Overstreet { 3991c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 4001c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 401e0541a93SKent Overstreet struct bch_inode_unpacked inode_u; 4022e87eae1SKent Overstreet s64 i_sectors_delta = 0; 4031c6fdbd8SKent Overstreet int ret = 0; 4041c6fdbd8SKent Overstreet 40568a507a2SKent Overstreet /* 40678d66ab1SDan Robertson * If the truncate call with change the size of the file, the 40778d66ab1SDan Robertson * cmtimes should be updated. If the size will not change, we 40878d66ab1SDan Robertson * do not need to update the cmtimes. 40968a507a2SKent Overstreet */ 41078d66ab1SDan Robertson if (iattr->ia_size != inode->v.i_size) { 41168a507a2SKent Overstreet if (!(iattr->ia_valid & ATTR_MTIME)) 41268a507a2SKent Overstreet ktime_get_coarse_real_ts64(&iattr->ia_mtime); 41368a507a2SKent Overstreet if (!(iattr->ia_valid & ATTR_CTIME)) 41468a507a2SKent Overstreet ktime_get_coarse_real_ts64(&iattr->ia_ctime); 41568a507a2SKent Overstreet iattr->ia_valid |= ATTR_MTIME|ATTR_CTIME; 41678d66ab1SDan Robertson } 41768a507a2SKent Overstreet 4181c6fdbd8SKent Overstreet inode_dio_wait(&inode->v); 419a7ecd30cSKent Overstreet bch2_pagecache_block_get(inode); 4201c6fdbd8SKent Overstreet 4216fed42bbSKent Overstreet ret = bch2_inode_find_by_inum(c, inode_inum(inode), &inode_u); 422e0541a93SKent Overstreet if (ret) 423e0541a93SKent Overstreet goto err; 4241c6fdbd8SKent Overstreet 425c45d473dSKent Overstreet /* 426c45d473dSKent Overstreet * check this before next assertion; on filesystem error our normal 427c45d473dSKent Overstreet * invariants are a bit broken (truncate has to truncate the page cache 428c45d473dSKent Overstreet * before the inode). 429c45d473dSKent Overstreet */ 430c45d473dSKent Overstreet ret = bch2_journal_error(&c->journal); 431c45d473dSKent Overstreet if (ret) 432c45d473dSKent Overstreet goto err; 433c45d473dSKent Overstreet 4348eb71e9eSKent Overstreet WARN_ONCE(!test_bit(EI_INODE_ERROR, &inode->ei_flags) && 4358eb71e9eSKent Overstreet inode->v.i_size < inode_u.bi_size, 4368eb71e9eSKent Overstreet "truncate spotted in mem i_size < btree i_size: %llu < %llu\n", 4378eb71e9eSKent Overstreet (u64) inode->v.i_size, inode_u.bi_size); 438e0541a93SKent Overstreet 439e0541a93SKent Overstreet if (iattr->ia_size > inode->v.i_size) { 44068a507a2SKent Overstreet ret = bch2_extend(idmap, inode, &inode_u, iattr); 44154e2264eSKent Overstreet goto err; 4421c6fdbd8SKent Overstreet } 4431c6fdbd8SKent Overstreet 44468a507a2SKent Overstreet iattr->ia_valid &= ~ATTR_SIZE; 44568a507a2SKent Overstreet 446959f7368SKent Overstreet ret = bch2_truncate_folio(inode, iattr->ia_size); 44774163da7SKent Overstreet if (unlikely(ret < 0)) 44854e2264eSKent Overstreet goto err; 4491c6fdbd8SKent Overstreet 4505902cc28SKent Overstreet truncate_setsize(&inode->v, iattr->ia_size); 4515902cc28SKent Overstreet 4526cc3535dSKent Overstreet /* 4536cc3535dSKent Overstreet * When extending, we're going to write the new i_size to disk 4546cc3535dSKent Overstreet * immediately so we need to flush anything above the current on disk 4556cc3535dSKent Overstreet * i_size first: 4566cc3535dSKent Overstreet * 4576cc3535dSKent Overstreet * Also, when extending we need to flush the page that i_size currently 4586cc3535dSKent Overstreet * straddles - if it's mapped to userspace, we need to ensure that 4596cc3535dSKent Overstreet * userspace has to redirty it and call .mkwrite -> set_page_dirty 4606cc3535dSKent Overstreet * again to allocate the part of the page that was extended. 4616cc3535dSKent Overstreet */ 462e0541a93SKent Overstreet if (iattr->ia_size > inode_u.bi_size) 4631c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 464e0541a93SKent Overstreet inode_u.bi_size, 4651c6fdbd8SKent Overstreet iattr->ia_size - 1); 4661c6fdbd8SKent Overstreet else if (iattr->ia_size & (PAGE_SIZE - 1)) 4671c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 4681c6fdbd8SKent Overstreet round_down(iattr->ia_size, PAGE_SIZE), 4691c6fdbd8SKent Overstreet iattr->ia_size - 1); 4701c6fdbd8SKent Overstreet if (ret) 47154e2264eSKent Overstreet goto err; 4721c6fdbd8SKent Overstreet 4735902cc28SKent Overstreet ret = bch2_truncate(c, inode_inum(inode), iattr->ia_size, &i_sectors_delta); 474dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta); 4752e87eae1SKent Overstreet 4765902cc28SKent Overstreet if (unlikely(ret)) { 4775902cc28SKent Overstreet /* 4785902cc28SKent Overstreet * If we error here, VFS caches are now inconsistent with btree 4795902cc28SKent Overstreet */ 4805902cc28SKent Overstreet set_bit(EI_INODE_ERROR, &inode->ei_flags); 4815902cc28SKent Overstreet goto err; 4825902cc28SKent Overstreet } 4835902cc28SKent Overstreet 484b33bf1bcSKent Overstreet bch2_fs_inconsistent_on(!inode->v.i_size && inode->v.i_blocks && 485b33bf1bcSKent Overstreet !bch2_journal_error(&c->journal), c, 486b33bf1bcSKent Overstreet "inode %lu truncated to 0 but i_blocks %llu (ondisk %lli)", 487b33bf1bcSKent Overstreet inode->v.i_ino, (u64) inode->v.i_blocks, 488b33bf1bcSKent Overstreet inode->ei_inode.bi_sectors); 48968a507a2SKent Overstreet 49068a507a2SKent Overstreet ret = bch2_setattr_nonsize(idmap, inode, iattr); 49154e2264eSKent Overstreet err: 492a7ecd30cSKent Overstreet bch2_pagecache_block_put(inode); 4935c1ef830SKent Overstreet return bch2_err_class(ret); 4941c6fdbd8SKent Overstreet } 4951c6fdbd8SKent Overstreet 4961c6fdbd8SKent Overstreet /* fallocate: */ 4971c6fdbd8SKent Overstreet 498791236b8SJoshua Ashton static int inode_update_times_fn(struct btree_trans *trans, 499791236b8SJoshua Ashton struct bch_inode_info *inode, 500050197b1SKent Overstreet struct bch_inode_unpacked *bi, void *p) 501050197b1SKent Overstreet { 502050197b1SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 503050197b1SKent Overstreet 504050197b1SKent Overstreet bi->bi_mtime = bi->bi_ctime = bch2_current_time(c); 505050197b1SKent Overstreet return 0; 506050197b1SKent Overstreet } 507050197b1SKent Overstreet 5082e87eae1SKent Overstreet static long bchfs_fpunch(struct bch_inode_info *inode, loff_t offset, loff_t len) 5091c6fdbd8SKent Overstreet { 5101c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 51174163da7SKent Overstreet u64 end = offset + len; 51274163da7SKent Overstreet u64 block_start = round_up(offset, block_bytes(c)); 51374163da7SKent Overstreet u64 block_end = round_down(end, block_bytes(c)); 51474163da7SKent Overstreet bool truncated_last_page; 5151c6fdbd8SKent Overstreet int ret = 0; 5161c6fdbd8SKent Overstreet 517959f7368SKent Overstreet ret = bch2_truncate_folios(inode, offset, end); 51874163da7SKent Overstreet if (unlikely(ret < 0)) 5191c6fdbd8SKent Overstreet goto err; 5201c6fdbd8SKent Overstreet 52174163da7SKent Overstreet truncated_last_page = ret; 5221c6fdbd8SKent Overstreet 52374163da7SKent Overstreet truncate_pagecache_range(&inode->v, offset, end - 1); 5241c6fdbd8SKent Overstreet 52574163da7SKent Overstreet if (block_start < block_end) { 5262e87eae1SKent Overstreet s64 i_sectors_delta = 0; 5272e87eae1SKent Overstreet 5288c6d298aSKent Overstreet ret = bch2_fpunch(c, inode_inum(inode), 52974163da7SKent Overstreet block_start >> 9, block_end >> 9, 5302e87eae1SKent Overstreet &i_sectors_delta); 531dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta); 5322e87eae1SKent Overstreet } 533050197b1SKent Overstreet 534050197b1SKent Overstreet mutex_lock(&inode->ei_update_lock); 53574163da7SKent Overstreet if (end >= inode->v.i_size && !truncated_last_page) { 53674163da7SKent Overstreet ret = bch2_write_inode_size(c, inode, inode->v.i_size, 53774163da7SKent Overstreet ATTR_MTIME|ATTR_CTIME); 53874163da7SKent Overstreet } else { 539050197b1SKent Overstreet ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL, 54074163da7SKent Overstreet ATTR_MTIME|ATTR_CTIME); 54174163da7SKent Overstreet } 542050197b1SKent Overstreet mutex_unlock(&inode->ei_update_lock); 5431c6fdbd8SKent Overstreet err: 5441c6fdbd8SKent Overstreet return ret; 5451c6fdbd8SKent Overstreet } 5461c6fdbd8SKent Overstreet 5472e87eae1SKent Overstreet static long bchfs_fcollapse_finsert(struct bch_inode_info *inode, 5485f786787SKent Overstreet loff_t offset, loff_t len, 5495f786787SKent Overstreet bool insert) 5501c6fdbd8SKent Overstreet { 5511c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 5521c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 5535902cc28SKent Overstreet s64 i_sectors_delta = 0; 55450dc0f69SKent Overstreet int ret = 0; 5551c6fdbd8SKent Overstreet 5561c6fdbd8SKent Overstreet if ((offset | len) & (block_bytes(c) - 1)) 5571c6fdbd8SKent Overstreet return -EINVAL; 5581c6fdbd8SKent Overstreet 5595f786787SKent Overstreet if (insert) { 5605f786787SKent Overstreet if (offset >= inode->v.i_size) 56174163da7SKent Overstreet return -EINVAL; 5625f786787SKent Overstreet } else { 5631c6fdbd8SKent Overstreet if (offset + len >= inode->v.i_size) 56474163da7SKent Overstreet return -EINVAL; 5655f786787SKent Overstreet } 5661c6fdbd8SKent Overstreet 567dbbfca9fSKent Overstreet ret = bch2_write_invalidate_inode_pages_range(mapping, offset, LLONG_MAX); 5681c6fdbd8SKent Overstreet if (ret) 56974163da7SKent Overstreet return ret; 5701c6fdbd8SKent Overstreet 5715902cc28SKent Overstreet if (insert) 5725902cc28SKent Overstreet i_size_write(&inode->v, inode->v.i_size + len); 5732e87eae1SKent Overstreet 5745902cc28SKent Overstreet ret = bch2_fcollapse_finsert(c, inode_inum(inode), offset >> 9, len >> 9, 5755902cc28SKent Overstreet insert, &i_sectors_delta); 5765902cc28SKent Overstreet if (!ret && !insert) 5775902cc28SKent Overstreet i_size_write(&inode->v, inode->v.i_size - len); 578dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, NULL, i_sectors_delta); 5792e87eae1SKent Overstreet 5801c6fdbd8SKent Overstreet return ret; 5811c6fdbd8SKent Overstreet } 5821c6fdbd8SKent Overstreet 583694015c2SKent Overstreet static int __bchfs_fallocate(struct bch_inode_info *inode, int mode, 584694015c2SKent Overstreet u64 start_sector, u64 end_sector) 5851c6fdbd8SKent Overstreet { 5861c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 5876bd68ec2SKent Overstreet struct btree_trans *trans = bch2_trans_get(c); 58867e0dd8fSKent Overstreet struct btree_iter iter; 589694015c2SKent Overstreet struct bpos end_pos = POS(inode->v.i_ino, end_sector); 59001ad6737SKent Overstreet struct bch_io_opts opts; 591694015c2SKent Overstreet int ret = 0; 5921c6fdbd8SKent Overstreet 59301ad6737SKent Overstreet bch2_inode_opts_get(&opts, c, &inode->ei_inode); 5941c6fdbd8SKent Overstreet 5956bd68ec2SKent Overstreet bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, 596694015c2SKent Overstreet POS(inode->v.i_ino, start_sector), 597190fa7afSKent Overstreet BTREE_ITER_SLOTS|BTREE_ITER_INTENT); 5981c6fdbd8SKent Overstreet 599e88a75ebSKent Overstreet while (!ret && bkey_lt(iter.pos, end_pos)) { 6002e87eae1SKent Overstreet s64 i_sectors_delta = 0; 601190fa7afSKent Overstreet struct quota_res quota_res = { 0 }; 6021c6fdbd8SKent Overstreet struct bkey_s_c k; 603694015c2SKent Overstreet unsigned sectors; 604a09818c7SKent Overstreet bool is_allocation; 605a09818c7SKent Overstreet u64 hole_start, hole_end; 6066fed42bbSKent Overstreet u32 snapshot; 6071c6fdbd8SKent Overstreet 6086bd68ec2SKent Overstreet bch2_trans_begin(trans); 609a8abd3a7SKent Overstreet 6106bd68ec2SKent Overstreet ret = bch2_subvolume_get_snapshot(trans, 6116fed42bbSKent Overstreet inode->ei_subvol, &snapshot); 6126fed42bbSKent Overstreet if (ret) 6136fed42bbSKent Overstreet goto bkey_err; 6146fed42bbSKent Overstreet 6156fed42bbSKent Overstreet bch2_btree_iter_set_snapshot(&iter, snapshot); 6166fed42bbSKent Overstreet 61767e0dd8fSKent Overstreet k = bch2_btree_iter_peek_slot(&iter); 6180f238367SKent Overstreet if ((ret = bkey_err(k))) 6190f238367SKent Overstreet goto bkey_err; 6201c6fdbd8SKent Overstreet 621a09818c7SKent Overstreet hole_start = iter.pos.offset; 622a09818c7SKent Overstreet hole_end = bpos_min(k.k->p, end_pos).offset; 623a09818c7SKent Overstreet is_allocation = bkey_extent_is_allocation(k.k); 624a09818c7SKent Overstreet 6251c6fdbd8SKent Overstreet /* already reserved */ 62679203111SKent Overstreet if (bkey_extent_is_reservation(k) && 62779203111SKent Overstreet bch2_bkey_nr_ptrs_fully_allocated(k) >= opts.data_replicas) { 62867e0dd8fSKent Overstreet bch2_btree_iter_advance(&iter); 6291c6fdbd8SKent Overstreet continue; 6301c6fdbd8SKent Overstreet } 6311c6fdbd8SKent Overstreet 632190fa7afSKent Overstreet if (bkey_extent_is_data(k.k) && 633190fa7afSKent Overstreet !(mode & FALLOC_FL_ZERO_RANGE)) { 63467e0dd8fSKent Overstreet bch2_btree_iter_advance(&iter); 6351c6fdbd8SKent Overstreet continue; 6361c6fdbd8SKent Overstreet } 6371c6fdbd8SKent Overstreet 638a09818c7SKent Overstreet if (!(mode & FALLOC_FL_ZERO_RANGE)) { 6394198bf03SKent Overstreet /* 6404198bf03SKent Overstreet * Lock ordering - can't be holding btree locks while 6414198bf03SKent Overstreet * blocking on a folio lock: 6424198bf03SKent Overstreet */ 6434198bf03SKent Overstreet if (bch2_clamp_data_hole(&inode->v, 6444198bf03SKent Overstreet &hole_start, 6454198bf03SKent Overstreet &hole_end, 6464198bf03SKent Overstreet opts.data_replicas, true)) 6476bd68ec2SKent Overstreet ret = drop_locks_do(trans, 648a09818c7SKent Overstreet (bch2_clamp_data_hole(&inode->v, 649a09818c7SKent Overstreet &hole_start, 650a09818c7SKent Overstreet &hole_end, 6514198bf03SKent Overstreet opts.data_replicas, false), 0)); 652a09818c7SKent Overstreet bch2_btree_iter_set_pos(&iter, POS(iter.pos.inode, hole_start)); 653a8b3a677SKent Overstreet 654a09818c7SKent Overstreet if (ret) 655a09818c7SKent Overstreet goto bkey_err; 6561c6fdbd8SKent Overstreet 657a09818c7SKent Overstreet if (hole_start == hole_end) 658a09818c7SKent Overstreet continue; 659a09818c7SKent Overstreet } 660a09818c7SKent Overstreet 661a09818c7SKent Overstreet sectors = hole_end - hole_start; 662a09818c7SKent Overstreet 663a09818c7SKent Overstreet if (!is_allocation) { 6641c6fdbd8SKent Overstreet ret = bch2_quota_reservation_add(c, inode, 665a09818c7SKent Overstreet "a_res, sectors, true); 6661c6fdbd8SKent Overstreet if (unlikely(ret)) 6670f238367SKent Overstreet goto bkey_err; 6681c6fdbd8SKent Overstreet } 6691c6fdbd8SKent Overstreet 6706bd68ec2SKent Overstreet ret = bch2_extent_fallocate(trans, inode_inum(inode), &iter, 67170de7a47SKent Overstreet sectors, opts, &i_sectors_delta, 67270de7a47SKent Overstreet writepoint_hashed((unsigned long) current)); 6738810386fSKent Overstreet if (ret) 6748810386fSKent Overstreet goto bkey_err; 67570de7a47SKent Overstreet 676dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, "a_res, i_sectors_delta); 677a09818c7SKent Overstreet 67846bf2e9cSKent Overstreet if (bch2_mark_pagecache_reserved(inode, &hole_start, 67946bf2e9cSKent Overstreet iter.pos.offset, true)) 6806bd68ec2SKent Overstreet drop_locks_do(trans, 68146bf2e9cSKent Overstreet bch2_mark_pagecache_reserved(inode, &hole_start, 68246bf2e9cSKent Overstreet iter.pos.offset, false)); 6830f238367SKent Overstreet bkey_err: 684190fa7afSKent Overstreet bch2_quota_reservation_put(c, inode, "a_res); 685549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 6861c6fdbd8SKent Overstreet ret = 0; 68750dc0f69SKent Overstreet } 68874163da7SKent Overstreet 689098ef98dSKent Overstreet if (bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE)) { 69074163da7SKent Overstreet struct quota_res quota_res = { 0 }; 69174163da7SKent Overstreet s64 i_sectors_delta = 0; 69274163da7SKent Overstreet 6936bd68ec2SKent Overstreet bch2_fpunch_at(trans, &iter, inode_inum(inode), 69474163da7SKent Overstreet end_sector, &i_sectors_delta); 695dbbfca9fSKent Overstreet bch2_i_sectors_acct(c, inode, "a_res, i_sectors_delta); 69674163da7SKent Overstreet bch2_quota_reservation_put(c, inode, "a_res); 69774163da7SKent Overstreet } 69874163da7SKent Overstreet 6996bd68ec2SKent Overstreet bch2_trans_iter_exit(trans, &iter); 7006bd68ec2SKent Overstreet bch2_trans_put(trans); 701694015c2SKent Overstreet return ret; 702694015c2SKent Overstreet } 70350dc0f69SKent Overstreet 704694015c2SKent Overstreet static long bchfs_fallocate(struct bch_inode_info *inode, int mode, 705694015c2SKent Overstreet loff_t offset, loff_t len) 706694015c2SKent Overstreet { 707694015c2SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 70874163da7SKent Overstreet u64 end = offset + len; 70974163da7SKent Overstreet u64 block_start = round_down(offset, block_bytes(c)); 71074163da7SKent Overstreet u64 block_end = round_up(end, block_bytes(c)); 71174163da7SKent Overstreet bool truncated_last_page = false; 71274163da7SKent Overstreet int ret, ret2 = 0; 713694015c2SKent Overstreet 714694015c2SKent Overstreet if (!(mode & FALLOC_FL_KEEP_SIZE) && end > inode->v.i_size) { 715694015c2SKent Overstreet ret = inode_newsize_ok(&inode->v, end); 716694015c2SKent Overstreet if (ret) 71774163da7SKent Overstreet return ret; 718694015c2SKent Overstreet } 719694015c2SKent Overstreet 720694015c2SKent Overstreet if (mode & FALLOC_FL_ZERO_RANGE) { 721959f7368SKent Overstreet ret = bch2_truncate_folios(inode, offset, end); 72274163da7SKent Overstreet if (unlikely(ret < 0)) 72374163da7SKent Overstreet return ret; 724694015c2SKent Overstreet 72574163da7SKent Overstreet truncated_last_page = ret; 726694015c2SKent Overstreet 727694015c2SKent Overstreet truncate_pagecache_range(&inode->v, offset, end - 1); 72874163da7SKent Overstreet 72974163da7SKent Overstreet block_start = round_up(offset, block_bytes(c)); 73074163da7SKent Overstreet block_end = round_down(end, block_bytes(c)); 731694015c2SKent Overstreet } 732694015c2SKent Overstreet 733694015c2SKent Overstreet ret = __bchfs_fallocate(inode, mode, block_start >> 9, block_end >> 9); 734e0541a93SKent Overstreet 735e0541a93SKent Overstreet /* 73674163da7SKent Overstreet * On -ENOSPC in ZERO_RANGE mode, we still want to do the inode update, 73774163da7SKent Overstreet * so that the VFS cache i_size is consistent with the btree i_size: 738e0541a93SKent Overstreet */ 73974163da7SKent Overstreet if (ret && 740098ef98dSKent Overstreet !(bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE))) 74174163da7SKent Overstreet return ret; 7421c6fdbd8SKent Overstreet 74374163da7SKent Overstreet if (mode & FALLOC_FL_KEEP_SIZE && end > inode->v.i_size) 744e0541a93SKent Overstreet end = inode->v.i_size; 74574163da7SKent Overstreet 74674163da7SKent Overstreet if (end >= inode->v.i_size && 74774163da7SKent Overstreet (((mode & FALLOC_FL_ZERO_RANGE) && !truncated_last_page) || 74874163da7SKent Overstreet !(mode & FALLOC_FL_KEEP_SIZE))) { 74974163da7SKent Overstreet spin_lock(&inode->v.i_lock); 750e0541a93SKent Overstreet i_size_write(&inode->v, end); 75174163da7SKent Overstreet spin_unlock(&inode->v.i_lock); 752e0541a93SKent Overstreet 7531c6fdbd8SKent Overstreet mutex_lock(&inode->ei_update_lock); 75474163da7SKent Overstreet ret2 = bch2_write_inode_size(c, inode, end, 0); 7551c6fdbd8SKent Overstreet mutex_unlock(&inode->ei_update_lock); 7561c6fdbd8SKent Overstreet } 75774163da7SKent Overstreet 75874163da7SKent Overstreet return ret ?: ret2; 7591c6fdbd8SKent Overstreet } 7601c6fdbd8SKent Overstreet 7611c6fdbd8SKent Overstreet long bch2_fallocate_dispatch(struct file *file, int mode, 7621c6fdbd8SKent Overstreet loff_t offset, loff_t len) 7631c6fdbd8SKent Overstreet { 7641c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 7652a9101a9SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 7662a9101a9SKent Overstreet long ret; 7672a9101a9SKent Overstreet 768d94189adSKent Overstreet if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_fallocate)) 7692a9101a9SKent Overstreet return -EROFS; 7701c6fdbd8SKent Overstreet 77174163da7SKent Overstreet inode_lock(&inode->v); 77274163da7SKent Overstreet inode_dio_wait(&inode->v); 773a7ecd30cSKent Overstreet bch2_pagecache_block_get(inode); 77474163da7SKent Overstreet 77507bfcc0bSKent Overstreet ret = file_modified(file); 77607bfcc0bSKent Overstreet if (ret) 77707bfcc0bSKent Overstreet goto err; 77807bfcc0bSKent Overstreet 7791c6fdbd8SKent Overstreet if (!(mode & ~(FALLOC_FL_KEEP_SIZE|FALLOC_FL_ZERO_RANGE))) 7802a9101a9SKent Overstreet ret = bchfs_fallocate(inode, mode, offset, len); 7812a9101a9SKent Overstreet else if (mode == (FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE)) 7822a9101a9SKent Overstreet ret = bchfs_fpunch(inode, offset, len); 7832a9101a9SKent Overstreet else if (mode == FALLOC_FL_INSERT_RANGE) 7842a9101a9SKent Overstreet ret = bchfs_fcollapse_finsert(inode, offset, len, true); 7852a9101a9SKent Overstreet else if (mode == FALLOC_FL_COLLAPSE_RANGE) 7862a9101a9SKent Overstreet ret = bchfs_fcollapse_finsert(inode, offset, len, false); 7872a9101a9SKent Overstreet else 7882a9101a9SKent Overstreet ret = -EOPNOTSUPP; 78907bfcc0bSKent Overstreet err: 790a7ecd30cSKent Overstreet bch2_pagecache_block_put(inode); 79174163da7SKent Overstreet inode_unlock(&inode->v); 792d94189adSKent Overstreet bch2_write_ref_put(c, BCH_WRITE_REF_fallocate); 7931c6fdbd8SKent Overstreet 7945c1ef830SKent Overstreet return bch2_err_class(ret); 7951c6fdbd8SKent Overstreet } 7961c6fdbd8SKent Overstreet 797c72f687aSKent Overstreet /* 798c72f687aSKent Overstreet * Take a quota reservation for unallocated blocks in a given file range 799c72f687aSKent Overstreet * Does not check pagecache 800c72f687aSKent Overstreet */ 801e8540e56SKent Overstreet static int quota_reserve_range(struct bch_inode_info *inode, 802e8540e56SKent Overstreet struct quota_res *res, 803e8540e56SKent Overstreet u64 start, u64 end) 804e8540e56SKent Overstreet { 805e8540e56SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 8066bd68ec2SKent Overstreet struct btree_trans *trans = bch2_trans_get(c); 807e8540e56SKent Overstreet struct btree_iter iter; 808e8540e56SKent Overstreet struct bkey_s_c k; 809e8540e56SKent Overstreet u32 snapshot; 810e8540e56SKent Overstreet u64 sectors = end - start; 811e8540e56SKent Overstreet u64 pos = start; 812e8540e56SKent Overstreet int ret; 813e8540e56SKent Overstreet retry: 8146bd68ec2SKent Overstreet bch2_trans_begin(trans); 815e8540e56SKent Overstreet 8166bd68ec2SKent Overstreet ret = bch2_subvolume_get_snapshot(trans, inode->ei_subvol, &snapshot); 817e8540e56SKent Overstreet if (ret) 818e8540e56SKent Overstreet goto err; 819e8540e56SKent Overstreet 8206bd68ec2SKent Overstreet bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, 821e8540e56SKent Overstreet SPOS(inode->v.i_ino, pos, snapshot), 0); 822e8540e56SKent Overstreet 8236bd68ec2SKent Overstreet while (!(ret = btree_trans_too_many_iters(trans)) && 824e8540e56SKent Overstreet (k = bch2_btree_iter_peek_upto(&iter, POS(inode->v.i_ino, end - 1))).k && 825e8540e56SKent Overstreet !(ret = bkey_err(k))) { 826e8540e56SKent Overstreet if (bkey_extent_is_allocation(k.k)) { 827e8540e56SKent Overstreet u64 s = min(end, k.k->p.offset) - 828e8540e56SKent Overstreet max(start, bkey_start_offset(k.k)); 829e8540e56SKent Overstreet BUG_ON(s > sectors); 830e8540e56SKent Overstreet sectors -= s; 831e8540e56SKent Overstreet } 832e8540e56SKent Overstreet bch2_btree_iter_advance(&iter); 833e8540e56SKent Overstreet } 834e8540e56SKent Overstreet pos = iter.pos.offset; 8356bd68ec2SKent Overstreet bch2_trans_iter_exit(trans, &iter); 836e8540e56SKent Overstreet err: 837e8540e56SKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 838e8540e56SKent Overstreet goto retry; 839e8540e56SKent Overstreet 8406bd68ec2SKent Overstreet bch2_trans_put(trans); 841e8540e56SKent Overstreet 8426bd68ec2SKent Overstreet return ret ?: bch2_quota_reservation_add(c, inode, res, sectors, true); 843e8540e56SKent Overstreet } 844e8540e56SKent Overstreet 84576426098SKent Overstreet loff_t bch2_remap_file_range(struct file *file_src, loff_t pos_src, 84676426098SKent Overstreet struct file *file_dst, loff_t pos_dst, 84776426098SKent Overstreet loff_t len, unsigned remap_flags) 84876426098SKent Overstreet { 84976426098SKent Overstreet struct bch_inode_info *src = file_bch_inode(file_src); 85076426098SKent Overstreet struct bch_inode_info *dst = file_bch_inode(file_dst); 85176426098SKent Overstreet struct bch_fs *c = src->v.i_sb->s_fs_info; 852e8540e56SKent Overstreet struct quota_res quota_res = { 0 }; 8532e87eae1SKent Overstreet s64 i_sectors_delta = 0; 854677fc056SKent Overstreet u64 aligned_len; 85576426098SKent Overstreet loff_t ret = 0; 85676426098SKent Overstreet 85776426098SKent Overstreet if (remap_flags & ~(REMAP_FILE_DEDUP|REMAP_FILE_ADVISORY)) 85876426098SKent Overstreet return -EINVAL; 85976426098SKent Overstreet 86076426098SKent Overstreet if (remap_flags & REMAP_FILE_DEDUP) 86176426098SKent Overstreet return -EOPNOTSUPP; 86276426098SKent Overstreet 86376426098SKent Overstreet if ((pos_src & (block_bytes(c) - 1)) || 86476426098SKent Overstreet (pos_dst & (block_bytes(c) - 1))) 86576426098SKent Overstreet return -EINVAL; 86676426098SKent Overstreet 86776426098SKent Overstreet if (src == dst && 86876426098SKent Overstreet abs(pos_src - pos_dst) < len) 86976426098SKent Overstreet return -EINVAL; 87076426098SKent Overstreet 871ecf8a74dSKent Overstreet lock_two_nondirectories(&src->v, &dst->v); 872ecf8a74dSKent Overstreet bch2_lock_inodes(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); 925ecf8a74dSKent Overstreet bch2_unlock_inodes(INODE_PAGECACHE_BLOCK, src, dst); 926ecf8a74dSKent Overstreet unlock_two_nondirectories(&src->v, &dst->v); 92776426098SKent Overstreet 9285c1ef830SKent Overstreet return bch2_err_class(ret); 92976426098SKent Overstreet } 93076426098SKent Overstreet 9311c6fdbd8SKent Overstreet /* fseek: */ 9321c6fdbd8SKent Overstreet 9331c6fdbd8SKent Overstreet static loff_t bch2_seek_data(struct file *file, u64 offset) 9341c6fdbd8SKent Overstreet { 9351c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 9361c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 9376bd68ec2SKent Overstreet struct btree_trans *trans; 93867e0dd8fSKent Overstreet struct btree_iter iter; 9391c6fdbd8SKent Overstreet struct bkey_s_c k; 9406fed42bbSKent Overstreet subvol_inum inum = inode_inum(inode); 9411c6fdbd8SKent Overstreet u64 isize, next_data = MAX_LFS_FILESIZE; 9426fed42bbSKent Overstreet u32 snapshot; 9431c6fdbd8SKent Overstreet int ret; 9441c6fdbd8SKent Overstreet 9451c6fdbd8SKent Overstreet isize = i_size_read(&inode->v); 9461c6fdbd8SKent Overstreet if (offset >= isize) 9471c6fdbd8SKent Overstreet return -ENXIO; 9481c6fdbd8SKent Overstreet 9496bd68ec2SKent Overstreet trans = bch2_trans_get(c); 9506fed42bbSKent Overstreet retry: 9516bd68ec2SKent Overstreet bch2_trans_begin(trans); 9526fed42bbSKent Overstreet 9536bd68ec2SKent Overstreet ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot); 9546fed42bbSKent Overstreet if (ret) 9556fed42bbSKent Overstreet goto err; 956424eb881SKent Overstreet 9576bd68ec2SKent Overstreet for_each_btree_key_upto_norestart(trans, iter, BTREE_ID_extents, 958c72f687aSKent Overstreet SPOS(inode->v.i_ino, offset >> 9, snapshot), 959c72f687aSKent Overstreet POS(inode->v.i_ino, U64_MAX), 960c72f687aSKent Overstreet 0, k, ret) { 961c72f687aSKent Overstreet if (bkey_extent_is_data(k.k)) { 9621c6fdbd8SKent Overstreet next_data = max(offset, bkey_start_offset(k.k) << 9); 9631c6fdbd8SKent Overstreet break; 9641c6fdbd8SKent Overstreet } else if (k.k->p.offset >> 9 > isize) 9651c6fdbd8SKent Overstreet break; 9661c6fdbd8SKent Overstreet } 9676bd68ec2SKent Overstreet bch2_trans_iter_exit(trans, &iter); 9686fed42bbSKent Overstreet err: 969549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 9706fed42bbSKent Overstreet goto retry; 9711c6fdbd8SKent Overstreet 9726bd68ec2SKent Overstreet bch2_trans_put(trans); 9731c6fdbd8SKent Overstreet if (ret) 9741c6fdbd8SKent Overstreet return ret; 9751c6fdbd8SKent Overstreet 9761c6fdbd8SKent Overstreet if (next_data > offset) 977543ef2ebSKent Overstreet next_data = bch2_seek_pagecache_data(&inode->v, 9784198bf03SKent Overstreet offset, next_data, 0, false); 9791c6fdbd8SKent Overstreet 980e10d3094SKent Overstreet if (next_data >= isize) 9811c6fdbd8SKent Overstreet return -ENXIO; 9821c6fdbd8SKent Overstreet 9831c6fdbd8SKent Overstreet return vfs_setpos(file, next_data, MAX_LFS_FILESIZE); 9841c6fdbd8SKent Overstreet } 9851c6fdbd8SKent Overstreet 9861c6fdbd8SKent Overstreet static loff_t bch2_seek_hole(struct file *file, u64 offset) 9871c6fdbd8SKent Overstreet { 9881c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 9891c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 9906bd68ec2SKent Overstreet struct btree_trans *trans; 99167e0dd8fSKent Overstreet struct btree_iter iter; 9921c6fdbd8SKent Overstreet struct bkey_s_c k; 9936fed42bbSKent Overstreet subvol_inum inum = inode_inum(inode); 9941c6fdbd8SKent Overstreet u64 isize, next_hole = MAX_LFS_FILESIZE; 9956fed42bbSKent Overstreet u32 snapshot; 9961c6fdbd8SKent Overstreet int ret; 9971c6fdbd8SKent Overstreet 9981c6fdbd8SKent Overstreet isize = i_size_read(&inode->v); 9991c6fdbd8SKent Overstreet if (offset >= isize) 10001c6fdbd8SKent Overstreet return -ENXIO; 10011c6fdbd8SKent Overstreet 10026bd68ec2SKent Overstreet trans = bch2_trans_get(c); 10036fed42bbSKent Overstreet retry: 10046bd68ec2SKent Overstreet bch2_trans_begin(trans); 10056fed42bbSKent Overstreet 10066bd68ec2SKent Overstreet ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot); 10076fed42bbSKent Overstreet if (ret) 10086fed42bbSKent Overstreet goto err; 1009424eb881SKent Overstreet 10106bd68ec2SKent Overstreet for_each_btree_key_norestart(trans, iter, BTREE_ID_extents, 10116fed42bbSKent Overstreet SPOS(inode->v.i_ino, offset >> 9, snapshot), 101294f651e2SKent Overstreet BTREE_ITER_SLOTS, k, ret) { 10131c6fdbd8SKent Overstreet if (k.k->p.inode != inode->v.i_ino) { 1014543ef2ebSKent Overstreet next_hole = bch2_seek_pagecache_hole(&inode->v, 10154198bf03SKent Overstreet offset, MAX_LFS_FILESIZE, 0, false); 10161c6fdbd8SKent Overstreet break; 10171c6fdbd8SKent Overstreet } else if (!bkey_extent_is_data(k.k)) { 1018543ef2ebSKent Overstreet next_hole = bch2_seek_pagecache_hole(&inode->v, 10191c6fdbd8SKent Overstreet max(offset, bkey_start_offset(k.k) << 9), 10204198bf03SKent Overstreet k.k->p.offset << 9, 0, false); 10211c6fdbd8SKent Overstreet 10221c6fdbd8SKent Overstreet if (next_hole < k.k->p.offset << 9) 10231c6fdbd8SKent Overstreet break; 10241c6fdbd8SKent Overstreet } else { 10251c6fdbd8SKent Overstreet offset = max(offset, bkey_start_offset(k.k) << 9); 10261c6fdbd8SKent Overstreet } 10271c6fdbd8SKent Overstreet } 10286bd68ec2SKent Overstreet bch2_trans_iter_exit(trans, &iter); 10296fed42bbSKent Overstreet err: 1030549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 10316fed42bbSKent Overstreet goto retry; 10321c6fdbd8SKent Overstreet 10336bd68ec2SKent Overstreet bch2_trans_put(trans); 10341c6fdbd8SKent Overstreet if (ret) 10351c6fdbd8SKent Overstreet return ret; 10361c6fdbd8SKent Overstreet 10371c6fdbd8SKent Overstreet if (next_hole > isize) 10381c6fdbd8SKent Overstreet next_hole = isize; 10391c6fdbd8SKent Overstreet 10401c6fdbd8SKent Overstreet return vfs_setpos(file, next_hole, MAX_LFS_FILESIZE); 10411c6fdbd8SKent Overstreet } 10421c6fdbd8SKent Overstreet 10431c6fdbd8SKent Overstreet loff_t bch2_llseek(struct file *file, loff_t offset, int whence) 10441c6fdbd8SKent Overstreet { 10455c1ef830SKent Overstreet loff_t ret; 10465c1ef830SKent Overstreet 10471c6fdbd8SKent Overstreet switch (whence) { 10481c6fdbd8SKent Overstreet case SEEK_SET: 10491c6fdbd8SKent Overstreet case SEEK_CUR: 10501c6fdbd8SKent Overstreet case SEEK_END: 10515c1ef830SKent Overstreet ret = generic_file_llseek(file, offset, whence); 10525c1ef830SKent Overstreet break; 10531c6fdbd8SKent Overstreet case SEEK_DATA: 10545c1ef830SKent Overstreet ret = bch2_seek_data(file, offset); 10555c1ef830SKent Overstreet break; 10561c6fdbd8SKent Overstreet case SEEK_HOLE: 10575c1ef830SKent Overstreet ret = bch2_seek_hole(file, offset); 10585c1ef830SKent Overstreet break; 10595c1ef830SKent Overstreet default: 10605c1ef830SKent Overstreet ret = -EINVAL; 10615c1ef830SKent Overstreet break; 10621c6fdbd8SKent Overstreet } 10631c6fdbd8SKent Overstreet 10645c1ef830SKent Overstreet return bch2_err_class(ret); 10651c6fdbd8SKent Overstreet } 10661c6fdbd8SKent Overstreet 10671c6fdbd8SKent Overstreet void bch2_fs_fsio_exit(struct bch_fs *c) 10681c6fdbd8SKent Overstreet { 1069a8b3a677SKent Overstreet bioset_exit(&c->nocow_flush_bioset); 10701c6fdbd8SKent Overstreet } 10711c6fdbd8SKent Overstreet 10721c6fdbd8SKent Overstreet int bch2_fs_fsio_init(struct bch_fs *c) 10731c6fdbd8SKent Overstreet { 107465d48e35SKent Overstreet if (bioset_init(&c->nocow_flush_bioset, 1075a8b3a677SKent Overstreet 1, offsetof(struct nocow_flush, bio), 0)) 107665d48e35SKent Overstreet return -BCH_ERR_ENOMEM_nocow_flush_bioset_init; 10771c6fdbd8SKent Overstreet 1078c8b4534dSKent Overstreet return 0; 10791c6fdbd8SKent Overstreet } 10801c6fdbd8SKent Overstreet 10811c6fdbd8SKent Overstreet #endif /* NO_BCACHEFS_FS */ 1082