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" 61c6fdbd8SKent Overstreet #include "btree_update.h" 71c6fdbd8SKent Overstreet #include "buckets.h" 81c6fdbd8SKent Overstreet #include "clock.h" 91c6fdbd8SKent Overstreet #include "error.h" 10e2d9912cSKent Overstreet #include "extents.h" 111c6fdbd8SKent Overstreet #include "fs.h" 121c6fdbd8SKent Overstreet #include "fs-io.h" 131c6fdbd8SKent Overstreet #include "fsck.h" 141c6fdbd8SKent Overstreet #include "inode.h" 151c6fdbd8SKent Overstreet #include "journal.h" 161c6fdbd8SKent Overstreet #include "io.h" 171c6fdbd8SKent Overstreet #include "keylist.h" 181c6fdbd8SKent Overstreet #include "quota.h" 19*76426098SKent Overstreet #include "reflink.h" 201c6fdbd8SKent Overstreet #include "trace.h" 211c6fdbd8SKent Overstreet 221c6fdbd8SKent Overstreet #include <linux/aio.h> 231c6fdbd8SKent Overstreet #include <linux/backing-dev.h> 241c6fdbd8SKent Overstreet #include <linux/falloc.h> 251c6fdbd8SKent Overstreet #include <linux/migrate.h> 261c6fdbd8SKent Overstreet #include <linux/mmu_context.h> 271c6fdbd8SKent Overstreet #include <linux/pagevec.h> 281c6fdbd8SKent Overstreet #include <linux/sched/signal.h> 291c6fdbd8SKent Overstreet #include <linux/task_io_accounting_ops.h> 301c6fdbd8SKent Overstreet #include <linux/uio.h> 311c6fdbd8SKent Overstreet #include <linux/writeback.h> 321c6fdbd8SKent Overstreet 331c6fdbd8SKent Overstreet #include <trace/events/writeback.h> 341c6fdbd8SKent Overstreet 357f5e31e1SKent Overstreet static inline bool bio_full(struct bio *bio, unsigned len) 367f5e31e1SKent Overstreet { 377f5e31e1SKent Overstreet if (bio->bi_vcnt >= bio->bi_max_vecs) 387f5e31e1SKent Overstreet return true; 397f5e31e1SKent Overstreet if (bio->bi_iter.bi_size > UINT_MAX - len) 407f5e31e1SKent Overstreet return true; 417f5e31e1SKent Overstreet return false; 427f5e31e1SKent Overstreet } 437f5e31e1SKent Overstreet 441c6fdbd8SKent Overstreet struct quota_res { 451c6fdbd8SKent Overstreet u64 sectors; 461c6fdbd8SKent Overstreet }; 471c6fdbd8SKent Overstreet 481c6fdbd8SKent Overstreet struct bchfs_write_op { 491c6fdbd8SKent Overstreet struct bch_inode_info *inode; 501c6fdbd8SKent Overstreet s64 sectors_added; 511c6fdbd8SKent Overstreet bool is_dio; 521c6fdbd8SKent Overstreet bool unalloc; 531c6fdbd8SKent Overstreet u64 new_i_size; 541c6fdbd8SKent Overstreet 551c6fdbd8SKent Overstreet /* must be last: */ 561c6fdbd8SKent Overstreet struct bch_write_op op; 571c6fdbd8SKent Overstreet }; 581c6fdbd8SKent Overstreet 591c6fdbd8SKent Overstreet struct bch_writepage_io { 601c6fdbd8SKent Overstreet struct closure cl; 611c6fdbd8SKent Overstreet u64 new_sectors; 621c6fdbd8SKent Overstreet 631c6fdbd8SKent Overstreet /* must be last: */ 641c6fdbd8SKent Overstreet struct bchfs_write_op op; 651c6fdbd8SKent Overstreet }; 661c6fdbd8SKent Overstreet 671c6fdbd8SKent Overstreet struct dio_write { 681c6fdbd8SKent Overstreet struct closure cl; 691c6fdbd8SKent Overstreet struct kiocb *req; 70ed484030SKent Overstreet struct mm_struct *mm; 711c6fdbd8SKent Overstreet unsigned loop:1, 721c6fdbd8SKent Overstreet sync:1, 731c6fdbd8SKent Overstreet free_iov:1; 741c6fdbd8SKent Overstreet struct quota_res quota_res; 751c6fdbd8SKent Overstreet 761c6fdbd8SKent Overstreet struct iov_iter iter; 771c6fdbd8SKent Overstreet struct iovec inline_vecs[2]; 781c6fdbd8SKent Overstreet 791c6fdbd8SKent Overstreet /* must be last: */ 801c6fdbd8SKent Overstreet struct bchfs_write_op iop; 811c6fdbd8SKent Overstreet }; 821c6fdbd8SKent Overstreet 831c6fdbd8SKent Overstreet struct dio_read { 841c6fdbd8SKent Overstreet struct closure cl; 851c6fdbd8SKent Overstreet struct kiocb *req; 861c6fdbd8SKent Overstreet long ret; 871c6fdbd8SKent Overstreet struct bch_read_bio rbio; 881c6fdbd8SKent Overstreet }; 891c6fdbd8SKent Overstreet 901c6fdbd8SKent Overstreet /* pagecache_block must be held */ 911c6fdbd8SKent Overstreet static int write_invalidate_inode_pages_range(struct address_space *mapping, 921c6fdbd8SKent Overstreet loff_t start, loff_t end) 931c6fdbd8SKent Overstreet { 941c6fdbd8SKent Overstreet int ret; 951c6fdbd8SKent Overstreet 961c6fdbd8SKent Overstreet /* 971c6fdbd8SKent Overstreet * XXX: the way this is currently implemented, we can spin if a process 981c6fdbd8SKent Overstreet * is continually redirtying a specific page 991c6fdbd8SKent Overstreet */ 1001c6fdbd8SKent Overstreet do { 1011c6fdbd8SKent Overstreet if (!mapping->nrpages) 1021c6fdbd8SKent Overstreet return 0; 1031c6fdbd8SKent Overstreet 1041c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, start, end); 1051c6fdbd8SKent Overstreet if (ret) 1061c6fdbd8SKent Overstreet break; 1071c6fdbd8SKent Overstreet 1081c6fdbd8SKent Overstreet if (!mapping->nrpages) 1091c6fdbd8SKent Overstreet return 0; 1101c6fdbd8SKent Overstreet 1111c6fdbd8SKent Overstreet ret = invalidate_inode_pages2_range(mapping, 1121c6fdbd8SKent Overstreet start >> PAGE_SHIFT, 1131c6fdbd8SKent Overstreet end >> PAGE_SHIFT); 1141c6fdbd8SKent Overstreet } while (ret == -EBUSY); 1151c6fdbd8SKent Overstreet 1161c6fdbd8SKent Overstreet return ret; 1171c6fdbd8SKent Overstreet } 1181c6fdbd8SKent Overstreet 1191c6fdbd8SKent Overstreet /* quotas */ 1201c6fdbd8SKent Overstreet 1211c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA 1221c6fdbd8SKent Overstreet 1231c6fdbd8SKent Overstreet static void bch2_quota_reservation_put(struct bch_fs *c, 1241c6fdbd8SKent Overstreet struct bch_inode_info *inode, 1251c6fdbd8SKent Overstreet struct quota_res *res) 1261c6fdbd8SKent Overstreet { 1271c6fdbd8SKent Overstreet if (!res->sectors) 1281c6fdbd8SKent Overstreet return; 1291c6fdbd8SKent Overstreet 1301c6fdbd8SKent Overstreet mutex_lock(&inode->ei_quota_lock); 1311c6fdbd8SKent Overstreet BUG_ON(res->sectors > inode->ei_quota_reserved); 1321c6fdbd8SKent Overstreet 1331c6fdbd8SKent Overstreet bch2_quota_acct(c, inode->ei_qid, Q_SPC, 13426609b61SKent Overstreet -((s64) res->sectors), KEY_TYPE_QUOTA_PREALLOC); 1351c6fdbd8SKent Overstreet inode->ei_quota_reserved -= res->sectors; 1361c6fdbd8SKent Overstreet mutex_unlock(&inode->ei_quota_lock); 1371c6fdbd8SKent Overstreet 1381c6fdbd8SKent Overstreet res->sectors = 0; 1391c6fdbd8SKent Overstreet } 1401c6fdbd8SKent Overstreet 1411c6fdbd8SKent Overstreet static int bch2_quota_reservation_add(struct bch_fs *c, 1421c6fdbd8SKent Overstreet struct bch_inode_info *inode, 1431c6fdbd8SKent Overstreet struct quota_res *res, 1441c6fdbd8SKent Overstreet unsigned sectors, 1451c6fdbd8SKent Overstreet bool check_enospc) 1461c6fdbd8SKent Overstreet { 1471c6fdbd8SKent Overstreet int ret; 1481c6fdbd8SKent Overstreet 1491c6fdbd8SKent Overstreet mutex_lock(&inode->ei_quota_lock); 1501c6fdbd8SKent Overstreet ret = bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors, 15126609b61SKent Overstreet check_enospc ? KEY_TYPE_QUOTA_PREALLOC : KEY_TYPE_QUOTA_NOCHECK); 1521c6fdbd8SKent Overstreet if (likely(!ret)) { 1531c6fdbd8SKent Overstreet inode->ei_quota_reserved += sectors; 1541c6fdbd8SKent Overstreet res->sectors += sectors; 1551c6fdbd8SKent Overstreet } 1561c6fdbd8SKent Overstreet mutex_unlock(&inode->ei_quota_lock); 1571c6fdbd8SKent Overstreet 1581c6fdbd8SKent Overstreet return ret; 1591c6fdbd8SKent Overstreet } 1601c6fdbd8SKent Overstreet 1611c6fdbd8SKent Overstreet #else 1621c6fdbd8SKent Overstreet 1631c6fdbd8SKent Overstreet static void bch2_quota_reservation_put(struct bch_fs *c, 1641c6fdbd8SKent Overstreet struct bch_inode_info *inode, 1651c6fdbd8SKent Overstreet struct quota_res *res) 1661c6fdbd8SKent Overstreet { 1671c6fdbd8SKent Overstreet } 1681c6fdbd8SKent Overstreet 1691c6fdbd8SKent Overstreet static int bch2_quota_reservation_add(struct bch_fs *c, 1701c6fdbd8SKent Overstreet struct bch_inode_info *inode, 1711c6fdbd8SKent Overstreet struct quota_res *res, 1721c6fdbd8SKent Overstreet unsigned sectors, 1731c6fdbd8SKent Overstreet bool check_enospc) 1741c6fdbd8SKent Overstreet { 1751c6fdbd8SKent Overstreet return 0; 1761c6fdbd8SKent Overstreet } 1771c6fdbd8SKent Overstreet 1781c6fdbd8SKent Overstreet #endif 1791c6fdbd8SKent Overstreet 1801c6fdbd8SKent Overstreet /* i_size updates: */ 1811c6fdbd8SKent Overstreet 1822ea90048SKent Overstreet struct inode_new_size { 1832ea90048SKent Overstreet loff_t new_size; 1842ea90048SKent Overstreet u64 now; 1852ea90048SKent Overstreet unsigned fields; 1862ea90048SKent Overstreet }; 1872ea90048SKent Overstreet 1881c6fdbd8SKent Overstreet static int inode_set_size(struct bch_inode_info *inode, 1891c6fdbd8SKent Overstreet struct bch_inode_unpacked *bi, 1901c6fdbd8SKent Overstreet void *p) 1911c6fdbd8SKent Overstreet { 1922ea90048SKent Overstreet struct inode_new_size *s = p; 1931c6fdbd8SKent Overstreet 1942ea90048SKent Overstreet bi->bi_size = s->new_size; 1952ea90048SKent Overstreet if (s->fields & ATTR_ATIME) 1962ea90048SKent Overstreet bi->bi_atime = s->now; 1972ea90048SKent Overstreet if (s->fields & ATTR_MTIME) 1982ea90048SKent Overstreet bi->bi_mtime = s->now; 1992ea90048SKent Overstreet if (s->fields & ATTR_CTIME) 2002ea90048SKent Overstreet bi->bi_ctime = s->now; 2011c6fdbd8SKent Overstreet 2021c6fdbd8SKent Overstreet return 0; 2031c6fdbd8SKent Overstreet } 2041c6fdbd8SKent Overstreet 205*76426098SKent Overstreet int __must_check bch2_write_inode_size(struct bch_fs *c, 2061c6fdbd8SKent Overstreet struct bch_inode_info *inode, 2072ea90048SKent Overstreet loff_t new_size, unsigned fields) 2081c6fdbd8SKent Overstreet { 2092ea90048SKent Overstreet struct inode_new_size s = { 2102ea90048SKent Overstreet .new_size = new_size, 2112ea90048SKent Overstreet .now = bch2_current_time(c), 2122ea90048SKent Overstreet .fields = fields, 2132ea90048SKent Overstreet }; 2142ea90048SKent Overstreet 2152ea90048SKent Overstreet return bch2_write_inode(c, inode, inode_set_size, &s, fields); 2161c6fdbd8SKent Overstreet } 2171c6fdbd8SKent Overstreet 2181c6fdbd8SKent Overstreet static void i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode, 219190fa7afSKent Overstreet struct quota_res *quota_res, s64 sectors) 2201c6fdbd8SKent Overstreet { 221190fa7afSKent Overstreet if (!sectors) 222190fa7afSKent Overstreet return; 223190fa7afSKent Overstreet 2241c6fdbd8SKent Overstreet mutex_lock(&inode->ei_quota_lock); 2251c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA 2261c6fdbd8SKent Overstreet if (quota_res && sectors > 0) { 2271c6fdbd8SKent Overstreet BUG_ON(sectors > quota_res->sectors); 2281c6fdbd8SKent Overstreet BUG_ON(sectors > inode->ei_quota_reserved); 2291c6fdbd8SKent Overstreet 2301c6fdbd8SKent Overstreet quota_res->sectors -= sectors; 2311c6fdbd8SKent Overstreet inode->ei_quota_reserved -= sectors; 2321c6fdbd8SKent Overstreet } else { 23326609b61SKent Overstreet bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors, KEY_TYPE_QUOTA_WARN); 2341c6fdbd8SKent Overstreet } 2351c6fdbd8SKent Overstreet #endif 2361c6fdbd8SKent Overstreet inode->v.i_blocks += sectors; 2371c6fdbd8SKent Overstreet mutex_unlock(&inode->ei_quota_lock); 2381c6fdbd8SKent Overstreet } 2391c6fdbd8SKent Overstreet 2401c6fdbd8SKent Overstreet /* normal i_size/i_sectors update machinery: */ 2411c6fdbd8SKent Overstreet 242446c562cSKent Overstreet static int sum_sector_overwrites(struct btree_trans *trans, 243446c562cSKent Overstreet struct btree_iter *extent_iter, 244446c562cSKent Overstreet struct bkey_i *new, bool *allocating, 245446c562cSKent Overstreet s64 *delta) 24608af47dfSKent Overstreet { 247446c562cSKent Overstreet struct btree_iter *iter; 24808af47dfSKent Overstreet struct bkey_s_c old; 2491c6fdbd8SKent Overstreet 250446c562cSKent Overstreet *delta = 0; 25108af47dfSKent Overstreet 252446c562cSKent Overstreet iter = bch2_trans_copy_iter(trans, extent_iter); 253446c562cSKent Overstreet if (IS_ERR(iter)) 254446c562cSKent Overstreet return PTR_ERR(iter); 25508af47dfSKent Overstreet 256446c562cSKent Overstreet old = bch2_btree_iter_peek_slot(iter); 25701a0108fSKent Overstreet 25801a0108fSKent Overstreet while (1) { 25901a0108fSKent Overstreet /* 26001a0108fSKent Overstreet * should not be possible to get an error here, since we're 26101a0108fSKent Overstreet * carefully not advancing past @new and thus whatever leaf node 26201a0108fSKent Overstreet * @_iter currently points to: 26301a0108fSKent Overstreet */ 2640f238367SKent Overstreet BUG_ON(bkey_err(old)); 26508af47dfSKent Overstreet 26608af47dfSKent Overstreet if (allocating && 2677ef2a73aSKent Overstreet !*allocating && 2687ef2a73aSKent Overstreet bch2_bkey_nr_ptrs_allocated(old) < 2697ef2a73aSKent Overstreet bch2_bkey_nr_dirty_ptrs(bkey_i_to_s_c(new))) 27008af47dfSKent Overstreet *allocating = true; 27108af47dfSKent Overstreet 272446c562cSKent Overstreet *delta += (min(new->k.p.offset, 27308af47dfSKent Overstreet old.k->p.offset) - 27408af47dfSKent Overstreet max(bkey_start_offset(&new->k), 27508af47dfSKent Overstreet bkey_start_offset(old.k))) * 27608af47dfSKent Overstreet (bkey_extent_is_allocation(&new->k) - 27708af47dfSKent Overstreet bkey_extent_is_allocation(old.k)); 27801a0108fSKent Overstreet 27901a0108fSKent Overstreet if (bkey_cmp(old.k->p, new->k.p) >= 0) 28001a0108fSKent Overstreet break; 28101a0108fSKent Overstreet 282446c562cSKent Overstreet old = bch2_btree_iter_next_slot(iter); 28308af47dfSKent Overstreet } 28408af47dfSKent Overstreet 285446c562cSKent Overstreet bch2_trans_iter_free(trans, iter); 286446c562cSKent Overstreet return 0; 28708af47dfSKent Overstreet } 28808af47dfSKent Overstreet 289b17657d0SKent Overstreet int bch2_extent_update(struct btree_trans *trans, 29008af47dfSKent Overstreet struct bch_inode_info *inode, 29108af47dfSKent Overstreet struct disk_reservation *disk_res, 29208af47dfSKent Overstreet struct quota_res *quota_res, 29308af47dfSKent Overstreet struct btree_iter *extent_iter, 29408af47dfSKent Overstreet struct bkey_i *k, 29508af47dfSKent Overstreet u64 new_i_size, 29608af47dfSKent Overstreet bool may_allocate, 29708af47dfSKent Overstreet bool direct, 29808af47dfSKent Overstreet s64 *total_delta) 29908af47dfSKent Overstreet { 30061f321fcSKent Overstreet struct bch_fs *c = trans->c; 30161f321fcSKent Overstreet struct btree_iter *inode_iter = NULL; 3021c6fdbd8SKent Overstreet struct bch_inode_unpacked inode_u; 3031c6fdbd8SKent Overstreet struct bkey_inode_buf inode_p; 30408af47dfSKent Overstreet bool allocating = false; 30508af47dfSKent Overstreet bool extended = false; 3065154704bSKent Overstreet bool inode_locked = false; 30708af47dfSKent Overstreet s64 i_sectors_delta; 30808af47dfSKent Overstreet int ret; 3091c6fdbd8SKent Overstreet 31008af47dfSKent Overstreet ret = bch2_btree_iter_traverse(extent_iter); 31108af47dfSKent Overstreet if (ret) 31208af47dfSKent Overstreet return ret; 3131c6fdbd8SKent Overstreet 3143c7f3b7aSKent Overstreet ret = bch2_extent_trim_atomic(k, extent_iter); 3153c7f3b7aSKent Overstreet if (ret) 3163c7f3b7aSKent Overstreet return ret; 3171c6fdbd8SKent Overstreet 318446c562cSKent Overstreet ret = sum_sector_overwrites(trans, extent_iter, 319446c562cSKent Overstreet k, &allocating, 320446c562cSKent Overstreet &i_sectors_delta); 321446c562cSKent Overstreet if (ret) 322446c562cSKent Overstreet return ret; 323446c562cSKent Overstreet 32408af47dfSKent Overstreet if (!may_allocate && allocating) 32508af47dfSKent Overstreet return -ENOSPC; 32608af47dfSKent Overstreet 32708af47dfSKent Overstreet bch2_trans_update(trans, BTREE_INSERT_ENTRY(extent_iter, k)); 32808af47dfSKent Overstreet 32908af47dfSKent Overstreet new_i_size = min(k->k.p.offset << 9, new_i_size); 3301c6fdbd8SKent Overstreet 3311c6fdbd8SKent Overstreet /* XXX: inode->i_size locking */ 33208af47dfSKent Overstreet if (i_sectors_delta || 33308af47dfSKent Overstreet new_i_size > inode->ei_inode.bi_size) { 33461f321fcSKent Overstreet if (c->opts.new_inode_updates) { 33558fbf808SKent Overstreet bch2_trans_unlock(trans); 3365154704bSKent Overstreet mutex_lock(&inode->ei_update_lock); 33708af47dfSKent Overstreet 33858fbf808SKent Overstreet if (!bch2_trans_relock(trans)) { 3395154704bSKent Overstreet mutex_unlock(&inode->ei_update_lock); 3405154704bSKent Overstreet return -EINTR; 3415154704bSKent Overstreet } 3425154704bSKent Overstreet 3435154704bSKent Overstreet inode_locked = true; 3445154704bSKent Overstreet 3455154704bSKent Overstreet if (!inode->ei_inode_update) 3465154704bSKent Overstreet inode->ei_inode_update = 34761f321fcSKent Overstreet bch2_deferred_update_alloc(c, 3485154704bSKent Overstreet BTREE_ID_INODES, 64); 34908af47dfSKent Overstreet 35008af47dfSKent Overstreet inode_u = inode->ei_inode; 35108af47dfSKent Overstreet inode_u.bi_sectors += i_sectors_delta; 35208af47dfSKent Overstreet 35308af47dfSKent Overstreet /* XXX: this is slightly suspect */ 35408af47dfSKent Overstreet if (!(inode_u.bi_flags & BCH_INODE_I_SIZE_DIRTY) && 35508af47dfSKent Overstreet new_i_size > inode_u.bi_size) { 35608af47dfSKent Overstreet inode_u.bi_size = new_i_size; 35708af47dfSKent Overstreet extended = true; 3581c6fdbd8SKent Overstreet } 3591c6fdbd8SKent Overstreet 36008af47dfSKent Overstreet bch2_inode_pack(&inode_p, &inode_u); 36108af47dfSKent Overstreet bch2_trans_update(trans, 3625154704bSKent Overstreet BTREE_INSERT_DEFERRED(inode->ei_inode_update, 3635154704bSKent Overstreet &inode_p.inode.k_i)); 36461f321fcSKent Overstreet } else { 36561f321fcSKent Overstreet inode_iter = bch2_trans_get_iter(trans, 36661f321fcSKent Overstreet BTREE_ID_INODES, 36761f321fcSKent Overstreet POS(k->k.p.inode, 0), 36861f321fcSKent Overstreet BTREE_ITER_SLOTS|BTREE_ITER_INTENT); 36961f321fcSKent Overstreet if (IS_ERR(inode_iter)) 37061f321fcSKent Overstreet return PTR_ERR(inode_iter); 37161f321fcSKent Overstreet 37261f321fcSKent Overstreet ret = bch2_btree_iter_traverse(inode_iter); 37361f321fcSKent Overstreet if (ret) 37461f321fcSKent Overstreet goto err; 37561f321fcSKent Overstreet 37661f321fcSKent Overstreet inode_u = inode->ei_inode; 37761f321fcSKent Overstreet inode_u.bi_sectors += i_sectors_delta; 37861f321fcSKent Overstreet 37961f321fcSKent Overstreet /* XXX: this is slightly suspect */ 38061f321fcSKent Overstreet if (!(inode_u.bi_flags & BCH_INODE_I_SIZE_DIRTY) && 38161f321fcSKent Overstreet new_i_size > inode_u.bi_size) { 38261f321fcSKent Overstreet inode_u.bi_size = new_i_size; 38361f321fcSKent Overstreet extended = true; 38461f321fcSKent Overstreet } 38561f321fcSKent Overstreet 38661f321fcSKent Overstreet bch2_inode_pack(&inode_p, &inode_u); 38761f321fcSKent Overstreet bch2_trans_update(trans, 38861f321fcSKent Overstreet BTREE_INSERT_ENTRY(inode_iter, &inode_p.inode.k_i)); 38961f321fcSKent Overstreet } 39008af47dfSKent Overstreet } 3911c6fdbd8SKent Overstreet 392fc3268c1SKent Overstreet ret = bch2_trans_commit(trans, disk_res, 39308af47dfSKent Overstreet &inode->ei_journal_seq, 39408af47dfSKent Overstreet BTREE_INSERT_NOFAIL| 39508af47dfSKent Overstreet BTREE_INSERT_ATOMIC| 39608af47dfSKent Overstreet BTREE_INSERT_NOUNLOCK| 39708af47dfSKent Overstreet BTREE_INSERT_USE_RESERVE); 39808af47dfSKent Overstreet if (ret) 39908af47dfSKent Overstreet goto err; 4001c6fdbd8SKent Overstreet 40108af47dfSKent Overstreet inode->ei_inode.bi_sectors += i_sectors_delta; 40208af47dfSKent Overstreet 40308af47dfSKent Overstreet EBUG_ON(i_sectors_delta && 40408af47dfSKent Overstreet inode->ei_inode.bi_sectors != inode_u.bi_sectors); 40508af47dfSKent Overstreet 40608af47dfSKent Overstreet if (extended) { 40708af47dfSKent Overstreet inode->ei_inode.bi_size = new_i_size; 40808af47dfSKent Overstreet 40908af47dfSKent Overstreet if (direct) { 4101c6fdbd8SKent Overstreet spin_lock(&inode->v.i_lock); 41108af47dfSKent Overstreet if (new_i_size > inode->v.i_size) 41208af47dfSKent Overstreet i_size_write(&inode->v, new_i_size); 4131c6fdbd8SKent Overstreet spin_unlock(&inode->v.i_lock); 4141c6fdbd8SKent Overstreet } 4151c6fdbd8SKent Overstreet } 4161c6fdbd8SKent Overstreet 41708af47dfSKent Overstreet if (direct) 41861f321fcSKent Overstreet i_sectors_acct(c, inode, quota_res, i_sectors_delta); 4191c6fdbd8SKent Overstreet 42008af47dfSKent Overstreet if (total_delta) 42108af47dfSKent Overstreet *total_delta += i_sectors_delta; 42208af47dfSKent Overstreet err: 42361f321fcSKent Overstreet if (!IS_ERR_OR_NULL(inode_iter)) 42461f321fcSKent Overstreet bch2_trans_iter_put(trans, inode_iter); 4255154704bSKent Overstreet if (inode_locked) 4265154704bSKent Overstreet mutex_unlock(&inode->ei_update_lock); 4275154704bSKent Overstreet 42808af47dfSKent Overstreet return ret; 4291c6fdbd8SKent Overstreet } 4301c6fdbd8SKent Overstreet 4311c6fdbd8SKent Overstreet static int bchfs_write_index_update(struct bch_write_op *wop) 4321c6fdbd8SKent Overstreet { 4335154704bSKent Overstreet struct bch_fs *c = wop->c; 4341c6fdbd8SKent Overstreet struct bchfs_write_op *op = container_of(wop, 4351c6fdbd8SKent Overstreet struct bchfs_write_op, op); 43608af47dfSKent Overstreet struct quota_res *quota_res = op->is_dio 43708af47dfSKent Overstreet ? &container_of(op, struct dio_write, iop)->quota_res 43808af47dfSKent Overstreet : NULL; 43908af47dfSKent Overstreet struct bch_inode_info *inode = op->inode; 4401c6fdbd8SKent Overstreet struct keylist *keys = &op->op.insert_keys; 4411c6fdbd8SKent Overstreet struct bkey_i *k = bch2_keylist_front(keys); 44208af47dfSKent Overstreet struct btree_trans trans; 44308af47dfSKent Overstreet struct btree_iter *iter; 4441c6fdbd8SKent Overstreet int ret; 4451c6fdbd8SKent Overstreet 44608af47dfSKent Overstreet BUG_ON(k->k.p.inode != inode->v.i_ino); 4471c6fdbd8SKent Overstreet 44820bceecbSKent Overstreet bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024); 449d69f41d6SKent Overstreet 45008af47dfSKent Overstreet iter = bch2_trans_get_iter(&trans, 451d69f41d6SKent Overstreet BTREE_ID_EXTENTS, 45208af47dfSKent Overstreet bkey_start_pos(&k->k), 4531c6fdbd8SKent Overstreet BTREE_ITER_INTENT); 4541c6fdbd8SKent Overstreet 4551c6fdbd8SKent Overstreet do { 456e2d9912cSKent Overstreet BKEY_PADDED(k) tmp; 4571c6fdbd8SKent Overstreet 458e2d9912cSKent Overstreet bkey_copy(&tmp.k, bch2_keylist_front(keys)); 459e2d9912cSKent Overstreet 460b17657d0SKent Overstreet bch2_trans_begin_updates(&trans); 461b17657d0SKent Overstreet 46208af47dfSKent Overstreet ret = bch2_extent_update(&trans, inode, 46308af47dfSKent Overstreet &wop->res, quota_res, 46408af47dfSKent Overstreet iter, &tmp.k, 46508af47dfSKent Overstreet op->new_i_size, 46608af47dfSKent Overstreet !op->unalloc, 46708af47dfSKent Overstreet op->is_dio, 46808af47dfSKent Overstreet &op->sectors_added); 4691c6fdbd8SKent Overstreet if (ret == -EINTR) 4701c6fdbd8SKent Overstreet continue; 4711c6fdbd8SKent Overstreet if (ret) 4721c6fdbd8SKent Overstreet break; 4731c6fdbd8SKent Overstreet 47408af47dfSKent Overstreet if (bkey_cmp(iter->pos, bch2_keylist_front(keys)->k.p) < 0) 47508af47dfSKent Overstreet bch2_cut_front(iter->pos, bch2_keylist_front(keys)); 476e2d9912cSKent Overstreet else 4771c6fdbd8SKent Overstreet bch2_keylist_pop_front(keys); 4781c6fdbd8SKent Overstreet } while (!bch2_keylist_empty(keys)); 4791c6fdbd8SKent Overstreet 480d69f41d6SKent Overstreet bch2_trans_exit(&trans); 4811c6fdbd8SKent Overstreet 4821c6fdbd8SKent Overstreet return ret; 4831c6fdbd8SKent Overstreet } 4841c6fdbd8SKent Overstreet 4851c6fdbd8SKent Overstreet static inline void bch2_fswrite_op_init(struct bchfs_write_op *op, 4861c6fdbd8SKent Overstreet struct bch_fs *c, 4871c6fdbd8SKent Overstreet struct bch_inode_info *inode, 4881c6fdbd8SKent Overstreet struct bch_io_opts opts, 4891c6fdbd8SKent Overstreet bool is_dio) 4901c6fdbd8SKent Overstreet { 4911c6fdbd8SKent Overstreet op->inode = inode; 4921c6fdbd8SKent Overstreet op->sectors_added = 0; 4931c6fdbd8SKent Overstreet op->is_dio = is_dio; 4941c6fdbd8SKent Overstreet op->unalloc = false; 4951c6fdbd8SKent Overstreet op->new_i_size = U64_MAX; 4961c6fdbd8SKent Overstreet 4971c6fdbd8SKent Overstreet bch2_write_op_init(&op->op, c, opts); 4981c6fdbd8SKent Overstreet op->op.target = opts.foreground_target; 4991c6fdbd8SKent Overstreet op->op.index_update_fn = bchfs_write_index_update; 5001c6fdbd8SKent Overstreet op_journal_seq_set(&op->op, &inode->ei_journal_seq); 5011c6fdbd8SKent Overstreet } 5021c6fdbd8SKent Overstreet 5031c6fdbd8SKent Overstreet static inline struct bch_io_opts io_opts(struct bch_fs *c, struct bch_inode_info *inode) 5041c6fdbd8SKent Overstreet { 5051c6fdbd8SKent Overstreet struct bch_io_opts opts = bch2_opts_to_inode_opts(c->opts); 5061c6fdbd8SKent Overstreet 5071c6fdbd8SKent Overstreet bch2_io_opts_apply(&opts, bch2_inode_opts_get(&inode->ei_inode)); 5081c6fdbd8SKent Overstreet return opts; 5091c6fdbd8SKent Overstreet } 5101c6fdbd8SKent Overstreet 5111c6fdbd8SKent Overstreet /* page state: */ 5121c6fdbd8SKent Overstreet 5131c6fdbd8SKent Overstreet /* stored in page->private: */ 5141c6fdbd8SKent Overstreet 515f57a6a5dSKent Overstreet struct bch_page_sector { 516f81b648dSKent Overstreet /* Uncompressed, fully allocated replicas: */ 517f57a6a5dSKent Overstreet unsigned nr_replicas:3; 518f81b648dSKent Overstreet 519f81b648dSKent Overstreet /* Owns PAGE_SECTORS * replicas_reserved sized reservation: */ 520f57a6a5dSKent Overstreet unsigned replicas_reserved:3; 5211c6fdbd8SKent Overstreet 522f57a6a5dSKent Overstreet /* i_sectors: */ 523f57a6a5dSKent Overstreet enum { 524f57a6a5dSKent Overstreet SECTOR_UNALLOCATED, 5252ba5d38bSKent Overstreet SECTOR_RESERVED, 526f57a6a5dSKent Overstreet SECTOR_DIRTY, 527f57a6a5dSKent Overstreet SECTOR_ALLOCATED, 528f57a6a5dSKent Overstreet } state:2; 5291c6fdbd8SKent Overstreet }; 5301c6fdbd8SKent Overstreet 531f57a6a5dSKent Overstreet struct bch_page_state { 5327f5e31e1SKent Overstreet atomic_t write_count; 533f57a6a5dSKent Overstreet struct bch_page_sector s[PAGE_SECTORS]; 534f57a6a5dSKent Overstreet }; 535f57a6a5dSKent Overstreet 536f57a6a5dSKent Overstreet static inline struct bch_page_state *__bch2_page_state(struct page *page) 5371c6fdbd8SKent Overstreet { 538f57a6a5dSKent Overstreet return page_has_private(page) 539f57a6a5dSKent Overstreet ? (struct bch_page_state *) page_private(page) 540f57a6a5dSKent Overstreet : NULL; 541f57a6a5dSKent Overstreet } 5421c6fdbd8SKent Overstreet 543f57a6a5dSKent Overstreet static inline struct bch_page_state *bch2_page_state(struct page *page) 544f57a6a5dSKent Overstreet { 545e1036a2aSKent Overstreet EBUG_ON(!PageLocked(page)); 5461c6fdbd8SKent Overstreet 547f57a6a5dSKent Overstreet return __bch2_page_state(page); 548f57a6a5dSKent Overstreet } 549f57a6a5dSKent Overstreet 550f57a6a5dSKent Overstreet /* for newly allocated pages: */ 551f57a6a5dSKent Overstreet static void __bch2_page_state_release(struct page *page) 552f57a6a5dSKent Overstreet { 553f57a6a5dSKent Overstreet struct bch_page_state *s = __bch2_page_state(page); 554f57a6a5dSKent Overstreet 555f57a6a5dSKent Overstreet if (!s) 556f57a6a5dSKent Overstreet return; 557f57a6a5dSKent Overstreet 558f57a6a5dSKent Overstreet ClearPagePrivate(page); 559f57a6a5dSKent Overstreet set_page_private(page, 0); 560f57a6a5dSKent Overstreet put_page(page); 561f57a6a5dSKent Overstreet kfree(s); 562f57a6a5dSKent Overstreet } 563f57a6a5dSKent Overstreet 564f57a6a5dSKent Overstreet static void bch2_page_state_release(struct page *page) 565f57a6a5dSKent Overstreet { 566f57a6a5dSKent Overstreet struct bch_page_state *s = bch2_page_state(page); 567f57a6a5dSKent Overstreet 568f57a6a5dSKent Overstreet if (!s) 569f57a6a5dSKent Overstreet return; 570f57a6a5dSKent Overstreet 571f57a6a5dSKent Overstreet ClearPagePrivate(page); 572f57a6a5dSKent Overstreet set_page_private(page, 0); 573f57a6a5dSKent Overstreet put_page(page); 574f57a6a5dSKent Overstreet kfree(s); 575f57a6a5dSKent Overstreet } 576f57a6a5dSKent Overstreet 577f57a6a5dSKent Overstreet /* for newly allocated pages: */ 578f57a6a5dSKent Overstreet static struct bch_page_state *__bch2_page_state_create(struct page *page, 579f57a6a5dSKent Overstreet gfp_t gfp) 580f57a6a5dSKent Overstreet { 581f57a6a5dSKent Overstreet struct bch_page_state *s; 582f57a6a5dSKent Overstreet 583f57a6a5dSKent Overstreet s = kzalloc(sizeof(*s), GFP_NOFS|gfp); 584f57a6a5dSKent Overstreet if (!s) 585f57a6a5dSKent Overstreet return NULL; 586f57a6a5dSKent Overstreet 587f57a6a5dSKent Overstreet /* 588f57a6a5dSKent Overstreet * migrate_page_move_mapping() assumes that pages with private data 589f57a6a5dSKent Overstreet * have their count elevated by 1. 590f57a6a5dSKent Overstreet */ 591f57a6a5dSKent Overstreet get_page(page); 592f57a6a5dSKent Overstreet set_page_private(page, (unsigned long) s); 5931c6fdbd8SKent Overstreet SetPagePrivate(page); 5941c6fdbd8SKent Overstreet return s; 5951c6fdbd8SKent Overstreet } 5961c6fdbd8SKent Overstreet 597f57a6a5dSKent Overstreet static struct bch_page_state *bch2_page_state_create(struct page *page, 598f57a6a5dSKent Overstreet gfp_t gfp) 599f57a6a5dSKent Overstreet { 600f57a6a5dSKent Overstreet return bch2_page_state(page) ?: __bch2_page_state_create(page, gfp); 601f57a6a5dSKent Overstreet } 602f57a6a5dSKent Overstreet 603e1036a2aSKent Overstreet static inline unsigned inode_nr_replicas(struct bch_fs *c, struct bch_inode_info *inode) 604e1036a2aSKent Overstreet { 605e1036a2aSKent Overstreet /* XXX: this should not be open coded */ 606e1036a2aSKent Overstreet return inode->ei_inode.bi_data_replicas 607e1036a2aSKent Overstreet ? inode->ei_inode.bi_data_replicas - 1 608e1036a2aSKent Overstreet : c->opts.data_replicas; 609e1036a2aSKent Overstreet } 610e1036a2aSKent Overstreet 611f57a6a5dSKent Overstreet static inline unsigned sectors_to_reserve(struct bch_page_sector *s, 612f57a6a5dSKent Overstreet unsigned nr_replicas) 613f57a6a5dSKent Overstreet { 614f57a6a5dSKent Overstreet return max(0, (int) nr_replicas - 615f57a6a5dSKent Overstreet s->nr_replicas - 616f57a6a5dSKent Overstreet s->replicas_reserved); 617f57a6a5dSKent Overstreet } 618f57a6a5dSKent Overstreet 619f57a6a5dSKent Overstreet static int bch2_get_page_disk_reservation(struct bch_fs *c, 620f57a6a5dSKent Overstreet struct bch_inode_info *inode, 6211c6fdbd8SKent Overstreet struct page *page, bool check_enospc) 6221c6fdbd8SKent Overstreet { 623f57a6a5dSKent Overstreet struct bch_page_state *s = bch2_page_state_create(page, 0); 624e1036a2aSKent Overstreet unsigned nr_replicas = inode_nr_replicas(c, inode); 625f57a6a5dSKent Overstreet struct disk_reservation disk_res = { 0 }; 626f57a6a5dSKent Overstreet unsigned i, disk_res_sectors = 0; 627f81b648dSKent Overstreet int ret; 6281c6fdbd8SKent Overstreet 629f57a6a5dSKent Overstreet if (!s) 630f57a6a5dSKent Overstreet return -ENOMEM; 6311c6fdbd8SKent Overstreet 632f57a6a5dSKent Overstreet for (i = 0; i < ARRAY_SIZE(s->s); i++) 633f57a6a5dSKent Overstreet disk_res_sectors += sectors_to_reserve(&s->s[i], nr_replicas); 634f57a6a5dSKent Overstreet 635f57a6a5dSKent Overstreet if (!disk_res_sectors) 636f57a6a5dSKent Overstreet return 0; 637f57a6a5dSKent Overstreet 638f57a6a5dSKent Overstreet ret = bch2_disk_reservation_get(c, &disk_res, 639f57a6a5dSKent Overstreet disk_res_sectors, 1, 640f57a6a5dSKent Overstreet !check_enospc 641f57a6a5dSKent Overstreet ? BCH_DISK_RESERVATION_NOFAIL 642f57a6a5dSKent Overstreet : 0); 6431c6fdbd8SKent Overstreet if (unlikely(ret)) 644f81b648dSKent Overstreet return ret; 645f81b648dSKent Overstreet 646f57a6a5dSKent Overstreet for (i = 0; i < ARRAY_SIZE(s->s); i++) 647f57a6a5dSKent Overstreet s->s[i].replicas_reserved += 648f57a6a5dSKent Overstreet sectors_to_reserve(&s->s[i], nr_replicas); 649f57a6a5dSKent Overstreet 650f57a6a5dSKent Overstreet return 0; 6511c6fdbd8SKent Overstreet } 6521c6fdbd8SKent Overstreet 653d1542e03SKent Overstreet struct bch2_page_reservation { 654d1542e03SKent Overstreet struct disk_reservation disk; 655d1542e03SKent Overstreet struct quota_res quota; 656d1542e03SKent Overstreet }; 657d1542e03SKent Overstreet 658d1542e03SKent Overstreet static void bch2_page_reservation_init(struct bch_fs *c, 659f57a6a5dSKent Overstreet struct bch_inode_info *inode, 660d1542e03SKent Overstreet struct bch2_page_reservation *res) 661d1542e03SKent Overstreet { 662d1542e03SKent Overstreet memset(res, 0, sizeof(*res)); 663d1542e03SKent Overstreet 664d1542e03SKent Overstreet res->disk.nr_replicas = inode_nr_replicas(c, inode); 665d1542e03SKent Overstreet } 666d1542e03SKent Overstreet 667d1542e03SKent Overstreet static void bch2_page_reservation_put(struct bch_fs *c, 668d1542e03SKent Overstreet struct bch_inode_info *inode, 669d1542e03SKent Overstreet struct bch2_page_reservation *res) 670d1542e03SKent Overstreet { 671d1542e03SKent Overstreet bch2_disk_reservation_put(c, &res->disk); 672d1542e03SKent Overstreet bch2_quota_reservation_put(c, inode, &res->quota); 673d1542e03SKent Overstreet } 674d1542e03SKent Overstreet 675d1542e03SKent Overstreet static int bch2_page_reservation_get(struct bch_fs *c, 676d1542e03SKent Overstreet struct bch_inode_info *inode, struct page *page, 677d1542e03SKent Overstreet struct bch2_page_reservation *res, 678d1542e03SKent Overstreet unsigned offset, unsigned len, bool check_enospc) 679f57a6a5dSKent Overstreet { 680f57a6a5dSKent Overstreet struct bch_page_state *s = bch2_page_state_create(page, 0); 681d1542e03SKent Overstreet unsigned i, disk_sectors = 0, quota_sectors = 0; 682f57a6a5dSKent Overstreet int ret; 683f57a6a5dSKent Overstreet 684f57a6a5dSKent Overstreet if (!s) 685f57a6a5dSKent Overstreet return -ENOMEM; 686f57a6a5dSKent Overstreet 687d1542e03SKent Overstreet for (i = offset / 512; 688d1542e03SKent Overstreet i < DIV_ROUND_UP(offset + len, 512); 689d1542e03SKent Overstreet i++) { 690d1542e03SKent Overstreet disk_sectors += sectors_to_reserve(&s->s[i], 691d1542e03SKent Overstreet res->disk.nr_replicas); 692d1542e03SKent Overstreet quota_sectors += s->s[i].state == SECTOR_UNALLOCATED; 6931c6fdbd8SKent Overstreet } 6941c6fdbd8SKent Overstreet 695d1542e03SKent Overstreet if (disk_sectors) { 696d1542e03SKent Overstreet ret = bch2_disk_reservation_add(c, &res->disk, 697d1542e03SKent Overstreet disk_sectors, 698d1542e03SKent Overstreet !check_enospc 699d1542e03SKent Overstreet ? BCH_DISK_RESERVATION_NOFAIL 700d1542e03SKent Overstreet : 0); 701d1542e03SKent Overstreet if (unlikely(ret)) 702d1542e03SKent Overstreet return ret; 703d1542e03SKent Overstreet } 704d1542e03SKent Overstreet 705d1542e03SKent Overstreet if (quota_sectors) { 706d1542e03SKent Overstreet ret = bch2_quota_reservation_add(c, inode, &res->quota, 707d1542e03SKent Overstreet quota_sectors, 708d1542e03SKent Overstreet check_enospc); 709d1542e03SKent Overstreet if (unlikely(ret)) { 710d1542e03SKent Overstreet struct disk_reservation tmp = { 711d1542e03SKent Overstreet .sectors = disk_sectors 712d1542e03SKent Overstreet }; 713d1542e03SKent Overstreet 714d1542e03SKent Overstreet bch2_disk_reservation_put(c, &tmp); 715d1542e03SKent Overstreet res->disk.sectors -= disk_sectors; 716d1542e03SKent Overstreet return ret; 717d1542e03SKent Overstreet } 718d1542e03SKent Overstreet } 719d1542e03SKent Overstreet 720d1542e03SKent Overstreet return 0; 721f57a6a5dSKent Overstreet } 722f57a6a5dSKent Overstreet 7231c6fdbd8SKent Overstreet static void bch2_clear_page_bits(struct page *page) 7241c6fdbd8SKent Overstreet { 7251c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(page->mapping->host); 7261c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 727f57a6a5dSKent Overstreet struct bch_page_state *s = bch2_page_state(page); 728d1542e03SKent Overstreet struct disk_reservation disk_res = { 0 }; 729f57a6a5dSKent Overstreet int i, dirty_sectors = 0; 7301c6fdbd8SKent Overstreet 731f57a6a5dSKent Overstreet if (!s) 7321c6fdbd8SKent Overstreet return; 7331c6fdbd8SKent Overstreet 734f57a6a5dSKent Overstreet for (i = 0; i < ARRAY_SIZE(s->s); i++) { 735d1542e03SKent Overstreet disk_res.sectors += s->s[i].replicas_reserved; 736d1542e03SKent Overstreet s->s[i].replicas_reserved = 0; 737d1542e03SKent Overstreet 738f57a6a5dSKent Overstreet if (s->s[i].state == SECTOR_DIRTY) { 739f57a6a5dSKent Overstreet dirty_sectors++; 740f57a6a5dSKent Overstreet s->s[i].state = SECTOR_UNALLOCATED; 741f57a6a5dSKent Overstreet } 742f57a6a5dSKent Overstreet } 743adfcfaf0SKent Overstreet 744d1542e03SKent Overstreet bch2_disk_reservation_put(c, &disk_res); 745d1542e03SKent Overstreet 746f57a6a5dSKent Overstreet if (dirty_sectors) 747f57a6a5dSKent Overstreet i_sectors_acct(c, inode, NULL, -dirty_sectors); 748adfcfaf0SKent Overstreet 749f57a6a5dSKent Overstreet bch2_page_state_release(page); 7501c6fdbd8SKent Overstreet } 7511c6fdbd8SKent Overstreet 752d1542e03SKent Overstreet static void bch2_set_page_dirty(struct bch_fs *c, 753d1542e03SKent Overstreet struct bch_inode_info *inode, struct page *page, 754d1542e03SKent Overstreet struct bch2_page_reservation *res, 755d1542e03SKent Overstreet unsigned offset, unsigned len) 7561c6fdbd8SKent Overstreet { 757d1542e03SKent Overstreet struct bch_page_state *s = bch2_page_state(page); 758f57a6a5dSKent Overstreet unsigned i, dirty_sectors = 0; 7591c6fdbd8SKent Overstreet 760d1542e03SKent Overstreet for (i = offset / 512; 761d1542e03SKent Overstreet i < DIV_ROUND_UP(offset + len, 512); 762d1542e03SKent Overstreet i++) { 763d1542e03SKent Overstreet unsigned sectors = sectors_to_reserve(&s->s[i], 764d1542e03SKent Overstreet res->disk.nr_replicas); 7651c6fdbd8SKent Overstreet 766d1542e03SKent Overstreet BUG_ON(sectors > res->disk.sectors); 767d1542e03SKent Overstreet s->s[i].replicas_reserved += sectors; 768d1542e03SKent Overstreet res->disk.sectors -= sectors; 769adfcfaf0SKent Overstreet 7702ba5d38bSKent Overstreet if (s->s[i].state == SECTOR_UNALLOCATED) 771f57a6a5dSKent Overstreet dirty_sectors++; 7722ba5d38bSKent Overstreet 7732ba5d38bSKent Overstreet s->s[i].state = max_t(unsigned, s->s[i].state, SECTOR_DIRTY); 774f57a6a5dSKent Overstreet } 775f57a6a5dSKent Overstreet 776f57a6a5dSKent Overstreet if (dirty_sectors) 777d1542e03SKent Overstreet i_sectors_acct(c, inode, &res->quota, dirty_sectors); 7781c6fdbd8SKent Overstreet 779d1542e03SKent Overstreet if (!PageDirty(page)) 780d1542e03SKent Overstreet filemap_dirty_folio(inode->v.i_mapping, page_folio(page)); 7811c6fdbd8SKent Overstreet } 7821c6fdbd8SKent Overstreet 7831c6fdbd8SKent Overstreet vm_fault_t bch2_page_fault(struct vm_fault *vmf) 7841c6fdbd8SKent Overstreet { 7851c6fdbd8SKent Overstreet struct file *file = vmf->vma->vm_file; 7861c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 7871c6fdbd8SKent Overstreet int ret; 7881c6fdbd8SKent Overstreet 7891c6fdbd8SKent Overstreet bch2_pagecache_add_get(&inode->ei_pagecache_lock); 7901c6fdbd8SKent Overstreet ret = filemap_fault(vmf); 7911c6fdbd8SKent Overstreet bch2_pagecache_add_put(&inode->ei_pagecache_lock); 7921c6fdbd8SKent Overstreet 7931c6fdbd8SKent Overstreet return ret; 7941c6fdbd8SKent Overstreet } 7951c6fdbd8SKent Overstreet 7961c6fdbd8SKent Overstreet vm_fault_t bch2_page_mkwrite(struct vm_fault *vmf) 7971c6fdbd8SKent Overstreet { 7981c6fdbd8SKent Overstreet struct page *page = vmf->page; 7991c6fdbd8SKent Overstreet struct file *file = vmf->vma->vm_file; 8001c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 8011c6fdbd8SKent Overstreet struct address_space *mapping = file->f_mapping; 8021c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 803d1542e03SKent Overstreet struct bch2_page_reservation res; 8041c6fdbd8SKent Overstreet int ret = VM_FAULT_LOCKED; 8051c6fdbd8SKent Overstreet 806d1542e03SKent Overstreet bch2_page_reservation_init(c, inode, &res); 807d1542e03SKent Overstreet 8081c6fdbd8SKent Overstreet sb_start_pagefault(inode->v.i_sb); 8091c6fdbd8SKent Overstreet file_update_time(file); 8101c6fdbd8SKent Overstreet 8111c6fdbd8SKent Overstreet /* 8121c6fdbd8SKent Overstreet * Not strictly necessary, but helps avoid dio writes livelocking in 8131c6fdbd8SKent Overstreet * write_invalidate_inode_pages_range() - can drop this if/when we get 8141c6fdbd8SKent Overstreet * a write_invalidate_inode_pages_range() that works without dropping 8151c6fdbd8SKent Overstreet * page lock before invalidating page 8161c6fdbd8SKent Overstreet */ 8171c6fdbd8SKent Overstreet bch2_pagecache_add_get(&inode->ei_pagecache_lock); 8181c6fdbd8SKent Overstreet 8191c6fdbd8SKent Overstreet lock_page(page); 8201c6fdbd8SKent Overstreet if (page->mapping != mapping || 8211c6fdbd8SKent Overstreet page_offset(page) > i_size_read(&inode->v)) { 8221c6fdbd8SKent Overstreet unlock_page(page); 8231c6fdbd8SKent Overstreet ret = VM_FAULT_NOPAGE; 8241c6fdbd8SKent Overstreet goto out; 8251c6fdbd8SKent Overstreet } 8261c6fdbd8SKent Overstreet 827d1542e03SKent Overstreet if (bch2_page_reservation_get(c, inode, page, &res, 828d1542e03SKent Overstreet 0, PAGE_SIZE, true)) { 8291c6fdbd8SKent Overstreet unlock_page(page); 8301c6fdbd8SKent Overstreet ret = VM_FAULT_SIGBUS; 8311c6fdbd8SKent Overstreet goto out; 8321c6fdbd8SKent Overstreet } 8331c6fdbd8SKent Overstreet 834d1542e03SKent Overstreet bch2_set_page_dirty(c, inode, page, &res, 0, PAGE_SIZE); 8351c6fdbd8SKent Overstreet wait_for_stable_page(page); 8361c6fdbd8SKent Overstreet out: 8371c6fdbd8SKent Overstreet bch2_pagecache_add_put(&inode->ei_pagecache_lock); 8381c6fdbd8SKent Overstreet sb_end_pagefault(inode->v.i_sb); 839d1542e03SKent Overstreet 840d1542e03SKent Overstreet bch2_page_reservation_put(c, inode, &res); 841d1542e03SKent Overstreet 8421c6fdbd8SKent Overstreet return ret; 8431c6fdbd8SKent Overstreet } 8441c6fdbd8SKent Overstreet 8451c6fdbd8SKent Overstreet void bch2_invalidate_folio(struct folio *folio, size_t offset, size_t length) 8461c6fdbd8SKent Overstreet { 8471c6fdbd8SKent Overstreet if (offset || length < folio_size(folio)) 8481c6fdbd8SKent Overstreet return; 8491c6fdbd8SKent Overstreet 8501c6fdbd8SKent Overstreet bch2_clear_page_bits(&folio->page); 8511c6fdbd8SKent Overstreet } 8521c6fdbd8SKent Overstreet 8531c6fdbd8SKent Overstreet bool bch2_release_folio(struct folio *folio, gfp_t gfp_mask) 8541c6fdbd8SKent Overstreet { 8551c6fdbd8SKent Overstreet /* XXX: this can't take locks that are held while we allocate memory */ 8561c6fdbd8SKent Overstreet EBUG_ON(!PageLocked(&folio->page)); 8571c6fdbd8SKent Overstreet EBUG_ON(folio_test_writeback(folio)); 8581c6fdbd8SKent Overstreet 859a6d90385SKent Overstreet if (folio_test_dirty(folio) || folio_test_writeback(folio)) 8601c6fdbd8SKent Overstreet return false; 8611c6fdbd8SKent Overstreet 8621c6fdbd8SKent Overstreet bch2_clear_page_bits(&folio->page); 8631c6fdbd8SKent Overstreet return true; 8641c6fdbd8SKent Overstreet } 8651c6fdbd8SKent Overstreet 8661c6fdbd8SKent Overstreet /* readpage(s): */ 8671c6fdbd8SKent Overstreet 8681c6fdbd8SKent Overstreet static void bch2_readpages_end_io(struct bio *bio) 8691c6fdbd8SKent Overstreet { 8701c6fdbd8SKent Overstreet struct bvec_iter_all iter; 8711c6fdbd8SKent Overstreet struct bio_vec *bv; 8721c6fdbd8SKent Overstreet 8731c6fdbd8SKent Overstreet bio_for_each_segment_all(bv, bio, iter) { 8741c6fdbd8SKent Overstreet struct page *page = bv->bv_page; 8751c6fdbd8SKent Overstreet 8761c6fdbd8SKent Overstreet if (!bio->bi_status) { 8771c6fdbd8SKent Overstreet SetPageUptodate(page); 8781c6fdbd8SKent Overstreet } else { 8791c6fdbd8SKent Overstreet ClearPageUptodate(page); 8801c6fdbd8SKent Overstreet SetPageError(page); 8811c6fdbd8SKent Overstreet } 8821c6fdbd8SKent Overstreet unlock_page(page); 8831c6fdbd8SKent Overstreet } 8841c6fdbd8SKent Overstreet 8851c6fdbd8SKent Overstreet bio_put(bio); 8861c6fdbd8SKent Overstreet } 8871c6fdbd8SKent Overstreet 8881c6fdbd8SKent Overstreet static inline void page_state_init_for_read(struct page *page) 8891c6fdbd8SKent Overstreet { 890f81b648dSKent Overstreet SetPagePrivate(page); 891f81b648dSKent Overstreet page->private = 0; 8921c6fdbd8SKent Overstreet } 8931c6fdbd8SKent Overstreet 8941c6fdbd8SKent Overstreet struct readpages_iter { 8951c6fdbd8SKent Overstreet struct address_space *mapping; 8961c6fdbd8SKent Overstreet struct page **pages; 8971c6fdbd8SKent Overstreet unsigned nr_pages; 8981c6fdbd8SKent Overstreet unsigned idx; 8991c6fdbd8SKent Overstreet pgoff_t offset; 9001c6fdbd8SKent Overstreet }; 9011c6fdbd8SKent Overstreet 9021c6fdbd8SKent Overstreet static int readpages_iter_init(struct readpages_iter *iter, 9031c6fdbd8SKent Overstreet struct readahead_control *ractl) 9041c6fdbd8SKent Overstreet { 9051c6fdbd8SKent Overstreet unsigned i, nr_pages = readahead_count(ractl); 9061c6fdbd8SKent Overstreet 9071c6fdbd8SKent Overstreet memset(iter, 0, sizeof(*iter)); 9081c6fdbd8SKent Overstreet 9091c6fdbd8SKent Overstreet iter->mapping = ractl->mapping; 9101c6fdbd8SKent Overstreet iter->offset = readahead_index(ractl); 9111c6fdbd8SKent Overstreet iter->nr_pages = nr_pages; 9121c6fdbd8SKent Overstreet 9131c6fdbd8SKent Overstreet iter->pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOFS); 9141c6fdbd8SKent Overstreet if (!iter->pages) 9151c6fdbd8SKent Overstreet return -ENOMEM; 9161c6fdbd8SKent Overstreet 9171c6fdbd8SKent Overstreet __readahead_batch(ractl, iter->pages, nr_pages); 9181c6fdbd8SKent Overstreet for (i = 0; i < nr_pages; i++) { 919f57a6a5dSKent Overstreet __bch2_page_state_create(iter->pages[i], __GFP_NOFAIL); 9201c6fdbd8SKent Overstreet put_page(iter->pages[i]); 9211c6fdbd8SKent Overstreet } 9221c6fdbd8SKent Overstreet 9231c6fdbd8SKent Overstreet return 0; 9241c6fdbd8SKent Overstreet } 9251c6fdbd8SKent Overstreet 9261c6fdbd8SKent Overstreet static inline struct page *readpage_iter_next(struct readpages_iter *iter) 9271c6fdbd8SKent Overstreet { 9281c6fdbd8SKent Overstreet if (iter->idx >= iter->nr_pages) 9291c6fdbd8SKent Overstreet return NULL; 9301c6fdbd8SKent Overstreet 9311c6fdbd8SKent Overstreet EBUG_ON(iter->pages[iter->idx]->index != iter->offset + iter->idx); 9321c6fdbd8SKent Overstreet 9331c6fdbd8SKent Overstreet return iter->pages[iter->idx]; 9341c6fdbd8SKent Overstreet } 9351c6fdbd8SKent Overstreet 9361c6fdbd8SKent Overstreet static void bch2_add_page_sectors(struct bio *bio, struct bkey_s_c k) 9371c6fdbd8SKent Overstreet { 9381c6fdbd8SKent Overstreet struct bvec_iter iter; 9391c6fdbd8SKent Overstreet struct bio_vec bv; 940*76426098SKent Overstreet unsigned nr_ptrs = k.k->type == KEY_TYPE_reflink_v 941*76426098SKent Overstreet ? 0 : bch2_bkey_nr_ptrs_allocated(k); 9422ba5d38bSKent Overstreet unsigned state = k.k->type == KEY_TYPE_reservation 9432ba5d38bSKent Overstreet ? SECTOR_RESERVED 9442ba5d38bSKent Overstreet : SECTOR_ALLOCATED; 9451c6fdbd8SKent Overstreet 9461c6fdbd8SKent Overstreet bio_for_each_segment(bv, bio, iter) { 947f57a6a5dSKent Overstreet struct bch_page_state *s = bch2_page_state(bv.bv_page); 948f57a6a5dSKent Overstreet unsigned i; 949f81b648dSKent Overstreet 950f57a6a5dSKent Overstreet for (i = bv.bv_offset >> 9; 951f57a6a5dSKent Overstreet i < (bv.bv_offset + bv.bv_len) >> 9; 952f57a6a5dSKent Overstreet i++) { 953f57a6a5dSKent Overstreet s->s[i].nr_replicas = nr_ptrs; 9542ba5d38bSKent Overstreet s->s[i].state = state; 955f57a6a5dSKent Overstreet } 9561c6fdbd8SKent Overstreet } 9571c6fdbd8SKent Overstreet } 9581c6fdbd8SKent Overstreet 9591c6fdbd8SKent Overstreet static void readpage_bio_extend(struct readpages_iter *iter, 960*76426098SKent Overstreet struct bio *bio, 961*76426098SKent Overstreet unsigned sectors_this_extent, 9621c6fdbd8SKent Overstreet bool get_more) 9631c6fdbd8SKent Overstreet { 964*76426098SKent Overstreet while (bio_sectors(bio) < sectors_this_extent && 9651c6fdbd8SKent Overstreet bio->bi_vcnt < bio->bi_max_vecs) { 9661c6fdbd8SKent Overstreet pgoff_t page_offset = bio_end_sector(bio) >> PAGE_SECTOR_SHIFT; 9671c6fdbd8SKent Overstreet struct page *page = readpage_iter_next(iter); 9681c6fdbd8SKent Overstreet int ret; 9691c6fdbd8SKent Overstreet 9701c6fdbd8SKent Overstreet if (page) { 9711c6fdbd8SKent Overstreet if (iter->offset + iter->idx != page_offset) 9721c6fdbd8SKent Overstreet break; 9731c6fdbd8SKent Overstreet 9741c6fdbd8SKent Overstreet iter->idx++; 9751c6fdbd8SKent Overstreet } else { 9761c6fdbd8SKent Overstreet if (!get_more) 9771c6fdbd8SKent Overstreet break; 9781c6fdbd8SKent Overstreet 9791c6fdbd8SKent Overstreet page = xa_load(&iter->mapping->i_pages, page_offset); 9801c6fdbd8SKent Overstreet if (page && !xa_is_value(page)) 9811c6fdbd8SKent Overstreet break; 9821c6fdbd8SKent Overstreet 9831c6fdbd8SKent Overstreet page = __page_cache_alloc(readahead_gfp_mask(iter->mapping)); 9841c6fdbd8SKent Overstreet if (!page) 9851c6fdbd8SKent Overstreet break; 9861c6fdbd8SKent Overstreet 987f57a6a5dSKent Overstreet if (!__bch2_page_state_create(page, 0)) { 988f57a6a5dSKent Overstreet put_page(page); 989f57a6a5dSKent Overstreet break; 990f57a6a5dSKent Overstreet } 9911c6fdbd8SKent Overstreet 9921c6fdbd8SKent Overstreet ret = add_to_page_cache_lru(page, iter->mapping, 9931c6fdbd8SKent Overstreet page_offset, GFP_NOFS); 9941c6fdbd8SKent Overstreet if (ret) { 995f57a6a5dSKent Overstreet __bch2_page_state_release(page); 9961c6fdbd8SKent Overstreet put_page(page); 9971c6fdbd8SKent Overstreet break; 9981c6fdbd8SKent Overstreet } 9991c6fdbd8SKent Overstreet 10001c6fdbd8SKent Overstreet put_page(page); 10011c6fdbd8SKent Overstreet } 10021c6fdbd8SKent Overstreet 1003885678f6SKent Overstreet BUG_ON(!bio_add_page(bio, page, PAGE_SIZE, 0)); 10041c6fdbd8SKent Overstreet } 10051c6fdbd8SKent Overstreet } 10061c6fdbd8SKent Overstreet 10070f238367SKent Overstreet static void bchfs_read(struct btree_trans *trans, struct btree_iter *iter, 10081c6fdbd8SKent Overstreet struct bch_read_bio *rbio, u64 inum, 10091c6fdbd8SKent Overstreet struct readpages_iter *readpages_iter) 10101c6fdbd8SKent Overstreet { 10110f238367SKent Overstreet struct bch_fs *c = trans->c; 10121c6fdbd8SKent Overstreet int flags = BCH_READ_RETRY_IF_STALE| 10131c6fdbd8SKent Overstreet BCH_READ_MAY_PROMOTE; 1014*76426098SKent Overstreet int ret = 0; 10151c6fdbd8SKent Overstreet 10161c6fdbd8SKent Overstreet rbio->c = c; 10171c6fdbd8SKent Overstreet rbio->start_time = local_clock(); 1018*76426098SKent Overstreet retry: 10191c6fdbd8SKent Overstreet while (1) { 10201c6fdbd8SKent Overstreet BKEY_PADDED(k) tmp; 10211c6fdbd8SKent Overstreet struct bkey_s_c k; 1022*76426098SKent Overstreet unsigned bytes, sectors, offset_into_extent; 10231c6fdbd8SKent Overstreet 102406ed8558SKent Overstreet bch2_btree_iter_set_pos(iter, 102506ed8558SKent Overstreet POS(inum, rbio->bio.bi_iter.bi_sector)); 10261c6fdbd8SKent Overstreet 10271c6fdbd8SKent Overstreet k = bch2_btree_iter_peek_slot(iter); 1028*76426098SKent Overstreet ret = bkey_err(k); 1029*76426098SKent Overstreet if (ret) 1030*76426098SKent Overstreet break; 10311c6fdbd8SKent Overstreet 10321c6fdbd8SKent Overstreet bkey_reassemble(&tmp.k, k); 10331c6fdbd8SKent Overstreet k = bkey_i_to_s_c(&tmp.k); 10341c6fdbd8SKent Overstreet 103506ed8558SKent Overstreet offset_into_extent = iter->pos.offset - 103606ed8558SKent Overstreet bkey_start_offset(k.k); 1037*76426098SKent Overstreet sectors = k.k->size - offset_into_extent; 1038*76426098SKent Overstreet 1039*76426098SKent Overstreet ret = bch2_read_indirect_extent(trans, iter, 1040*76426098SKent Overstreet &offset_into_extent, &tmp.k); 1041*76426098SKent Overstreet if (ret) 1042*76426098SKent Overstreet break; 1043*76426098SKent Overstreet 1044*76426098SKent Overstreet sectors = min(sectors, k.k->size - offset_into_extent); 1045*76426098SKent Overstreet 1046*76426098SKent Overstreet bch2_trans_unlock(trans); 104706ed8558SKent Overstreet 10481c6fdbd8SKent Overstreet if (readpages_iter) { 10491c6fdbd8SKent Overstreet bool want_full_extent = false; 10501c6fdbd8SKent Overstreet 10511c6fdbd8SKent Overstreet if (bkey_extent_is_data(k.k)) { 105299aaf570SKent Overstreet struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); 10531c6fdbd8SKent Overstreet const union bch_extent_entry *i; 10541742237bSKent Overstreet struct extent_ptr_decoded p; 10551c6fdbd8SKent Overstreet 105699aaf570SKent Overstreet bkey_for_each_ptr_decode(k.k, ptrs, p, i) 10571742237bSKent Overstreet want_full_extent |= ((p.crc.csum_type != 0) | 10581742237bSKent Overstreet (p.crc.compression_type != 0)); 10591c6fdbd8SKent Overstreet } 10601c6fdbd8SKent Overstreet 1061*76426098SKent Overstreet readpage_bio_extend(readpages_iter, &rbio->bio, 1062*76426098SKent Overstreet sectors, want_full_extent); 10631c6fdbd8SKent Overstreet } 10641c6fdbd8SKent Overstreet 1065*76426098SKent Overstreet bytes = min(sectors, bio_sectors(&rbio->bio)) << 9; 106606ed8558SKent Overstreet swap(rbio->bio.bi_iter.bi_size, bytes); 10671c6fdbd8SKent Overstreet 106806ed8558SKent Overstreet if (rbio->bio.bi_iter.bi_size == bytes) 10691c6fdbd8SKent Overstreet flags |= BCH_READ_LAST_FRAGMENT; 10701c6fdbd8SKent Overstreet 10711c6fdbd8SKent Overstreet if (bkey_extent_is_allocation(k.k)) 107206ed8558SKent Overstreet bch2_add_page_sectors(&rbio->bio, k); 10731c6fdbd8SKent Overstreet 107406ed8558SKent Overstreet bch2_read_extent(c, rbio, k, offset_into_extent, flags); 10751c6fdbd8SKent Overstreet 10761c6fdbd8SKent Overstreet if (flags & BCH_READ_LAST_FRAGMENT) 10771c6fdbd8SKent Overstreet return; 10781c6fdbd8SKent Overstreet 107906ed8558SKent Overstreet swap(rbio->bio.bi_iter.bi_size, bytes); 108006ed8558SKent Overstreet bio_advance(&rbio->bio, bytes); 10811c6fdbd8SKent Overstreet } 1082*76426098SKent Overstreet 1083*76426098SKent Overstreet if (ret == -EINTR) 1084*76426098SKent Overstreet goto retry; 1085*76426098SKent Overstreet 1086*76426098SKent Overstreet bcache_io_error(c, &rbio->bio, "btree IO error %i", ret); 1087*76426098SKent Overstreet bio_endio(&rbio->bio); 10881c6fdbd8SKent Overstreet } 10891c6fdbd8SKent Overstreet 10901c6fdbd8SKent Overstreet void bch2_readahead(struct readahead_control *ractl) 10911c6fdbd8SKent Overstreet { 10921c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(ractl->mapping->host); 10931c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 10941c6fdbd8SKent Overstreet struct bch_io_opts opts = io_opts(c, inode); 1095424eb881SKent Overstreet struct btree_trans trans; 1096424eb881SKent Overstreet struct btree_iter *iter; 10971c6fdbd8SKent Overstreet struct page *page; 10981c6fdbd8SKent Overstreet struct readpages_iter readpages_iter; 10991c6fdbd8SKent Overstreet int ret; 11001c6fdbd8SKent Overstreet 11011c6fdbd8SKent Overstreet ret = readpages_iter_init(&readpages_iter, ractl); 11021c6fdbd8SKent Overstreet BUG_ON(ret); 11031c6fdbd8SKent Overstreet 110420bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 1105424eb881SKent Overstreet 1106424eb881SKent Overstreet iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS, POS_MIN, 11071c6fdbd8SKent Overstreet BTREE_ITER_SLOTS); 11081c6fdbd8SKent Overstreet 11091c6fdbd8SKent Overstreet bch2_pagecache_add_get(&inode->ei_pagecache_lock); 11101c6fdbd8SKent Overstreet 11111c6fdbd8SKent Overstreet while ((page = readpage_iter_next(&readpages_iter))) { 11121c6fdbd8SKent Overstreet pgoff_t index = readpages_iter.offset + readpages_iter.idx; 11131c6fdbd8SKent Overstreet unsigned n = min_t(unsigned, 11141c6fdbd8SKent Overstreet readpages_iter.nr_pages - 11151c6fdbd8SKent Overstreet readpages_iter.idx, 11161c6fdbd8SKent Overstreet BIO_MAX_VECS); 11171c6fdbd8SKent Overstreet struct bch_read_bio *rbio = 11181c6fdbd8SKent Overstreet rbio_init(bio_alloc_bioset(NULL, n, REQ_OP_READ, 11191c6fdbd8SKent Overstreet GFP_NOFS, &c->bio_read), 11201c6fdbd8SKent Overstreet opts); 11211c6fdbd8SKent Overstreet 11221c6fdbd8SKent Overstreet readpages_iter.idx++; 11231c6fdbd8SKent Overstreet 11241c6fdbd8SKent Overstreet rbio->bio.bi_iter.bi_sector = (sector_t) index << PAGE_SECTOR_SHIFT; 11251c6fdbd8SKent Overstreet rbio->bio.bi_end_io = bch2_readpages_end_io; 1126885678f6SKent Overstreet BUG_ON(!bio_add_page(&rbio->bio, page, PAGE_SIZE, 0)); 11271c6fdbd8SKent Overstreet 11280f238367SKent Overstreet bchfs_read(&trans, iter, rbio, inode->v.i_ino, 11290f238367SKent Overstreet &readpages_iter); 11301c6fdbd8SKent Overstreet } 11311c6fdbd8SKent Overstreet 11321c6fdbd8SKent Overstreet bch2_pagecache_add_put(&inode->ei_pagecache_lock); 1133424eb881SKent Overstreet 1134424eb881SKent Overstreet bch2_trans_exit(&trans); 11351c6fdbd8SKent Overstreet kfree(readpages_iter.pages); 11361c6fdbd8SKent Overstreet } 11371c6fdbd8SKent Overstreet 11381c6fdbd8SKent Overstreet static void __bchfs_readpage(struct bch_fs *c, struct bch_read_bio *rbio, 11391c6fdbd8SKent Overstreet u64 inum, struct page *page) 11401c6fdbd8SKent Overstreet { 1141424eb881SKent Overstreet struct btree_trans trans; 1142424eb881SKent Overstreet struct btree_iter *iter; 11431c6fdbd8SKent Overstreet 1144f57a6a5dSKent Overstreet bch2_page_state_create(page, __GFP_NOFAIL); 11451c6fdbd8SKent Overstreet 11461c6fdbd8SKent Overstreet rbio->bio.bi_opf = REQ_OP_READ|REQ_SYNC; 11477f5e31e1SKent Overstreet rbio->bio.bi_iter.bi_sector = 11487f5e31e1SKent Overstreet (sector_t) page->index << PAGE_SECTOR_SHIFT; 11497f5e31e1SKent Overstreet BUG_ON(!bio_add_page(&rbio->bio, page, PAGE_SIZE, 0)); 11501c6fdbd8SKent Overstreet 115120bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 1152424eb881SKent Overstreet iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS, POS_MIN, 11531c6fdbd8SKent Overstreet BTREE_ITER_SLOTS); 1154424eb881SKent Overstreet 11550f238367SKent Overstreet bchfs_read(&trans, iter, rbio, inum, NULL); 1156424eb881SKent Overstreet 1157424eb881SKent Overstreet bch2_trans_exit(&trans); 11581c6fdbd8SKent Overstreet } 11591c6fdbd8SKent Overstreet 11601c6fdbd8SKent Overstreet static void bch2_read_single_page_end_io(struct bio *bio) 11611c6fdbd8SKent Overstreet { 11621c6fdbd8SKent Overstreet complete(bio->bi_private); 11631c6fdbd8SKent Overstreet } 11641c6fdbd8SKent Overstreet 11651c6fdbd8SKent Overstreet static int bch2_read_single_page(struct page *page, 11661c6fdbd8SKent Overstreet struct address_space *mapping) 11671c6fdbd8SKent Overstreet { 11681c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(mapping->host); 11691c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 11701c6fdbd8SKent Overstreet struct bch_read_bio *rbio; 11711c6fdbd8SKent Overstreet int ret; 11721c6fdbd8SKent Overstreet DECLARE_COMPLETION_ONSTACK(done); 11731c6fdbd8SKent Overstreet 11741c6fdbd8SKent Overstreet rbio = rbio_init(bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS, &c->bio_read), 11751c6fdbd8SKent Overstreet io_opts(c, inode)); 11761c6fdbd8SKent Overstreet rbio->bio.bi_private = &done; 11771c6fdbd8SKent Overstreet rbio->bio.bi_end_io = bch2_read_single_page_end_io; 11781c6fdbd8SKent Overstreet 11791c6fdbd8SKent Overstreet __bchfs_readpage(c, rbio, inode->v.i_ino, page); 11801c6fdbd8SKent Overstreet wait_for_completion(&done); 11811c6fdbd8SKent Overstreet 11821c6fdbd8SKent Overstreet ret = blk_status_to_errno(rbio->bio.bi_status); 11831c6fdbd8SKent Overstreet bio_put(&rbio->bio); 11841c6fdbd8SKent Overstreet 11851c6fdbd8SKent Overstreet if (ret < 0) 11861c6fdbd8SKent Overstreet return ret; 11871c6fdbd8SKent Overstreet 11881c6fdbd8SKent Overstreet SetPageUptodate(page); 11891c6fdbd8SKent Overstreet return 0; 11901c6fdbd8SKent Overstreet } 11911c6fdbd8SKent Overstreet 11921c6fdbd8SKent Overstreet int bch2_read_folio(struct file *file, struct folio *folio) 11931c6fdbd8SKent Overstreet { 11941c6fdbd8SKent Overstreet struct page *page = &folio->page; 11951c6fdbd8SKent Overstreet int ret; 11961c6fdbd8SKent Overstreet 11971c6fdbd8SKent Overstreet ret = bch2_read_single_page(page, page->mapping); 11981c6fdbd8SKent Overstreet folio_unlock(folio); 11991c6fdbd8SKent Overstreet return ret; 12001c6fdbd8SKent Overstreet } 12011c6fdbd8SKent Overstreet 12021c6fdbd8SKent Overstreet /* writepages: */ 12031c6fdbd8SKent Overstreet 12041c6fdbd8SKent Overstreet struct bch_writepage_state { 12051c6fdbd8SKent Overstreet struct bch_writepage_io *io; 12061c6fdbd8SKent Overstreet struct bch_io_opts opts; 12071c6fdbd8SKent Overstreet }; 12081c6fdbd8SKent Overstreet 12091c6fdbd8SKent Overstreet static inline struct bch_writepage_state bch_writepage_state_init(struct bch_fs *c, 12101c6fdbd8SKent Overstreet struct bch_inode_info *inode) 12111c6fdbd8SKent Overstreet { 12121c6fdbd8SKent Overstreet return (struct bch_writepage_state) { .opts = io_opts(c, inode) }; 12131c6fdbd8SKent Overstreet } 12141c6fdbd8SKent Overstreet 12151c6fdbd8SKent Overstreet static void bch2_writepage_io_free(struct closure *cl) 12161c6fdbd8SKent Overstreet { 12171c6fdbd8SKent Overstreet struct bch_writepage_io *io = container_of(cl, 12181c6fdbd8SKent Overstreet struct bch_writepage_io, cl); 12191c6fdbd8SKent Overstreet 12201c6fdbd8SKent Overstreet bio_put(&io->op.op.wbio.bio); 12211c6fdbd8SKent Overstreet } 12221c6fdbd8SKent Overstreet 12231c6fdbd8SKent Overstreet static void bch2_writepage_io_done(struct closure *cl) 12241c6fdbd8SKent Overstreet { 12251c6fdbd8SKent Overstreet struct bch_writepage_io *io = container_of(cl, 12261c6fdbd8SKent Overstreet struct bch_writepage_io, cl); 12271c6fdbd8SKent Overstreet struct bch_fs *c = io->op.op.c; 12281c6fdbd8SKent Overstreet struct bio *bio = &io->op.op.wbio.bio; 12291c6fdbd8SKent Overstreet struct bvec_iter_all iter; 12301c6fdbd8SKent Overstreet struct bio_vec *bvec; 1231b3fce09cSKent Overstreet unsigned i; 12321c6fdbd8SKent Overstreet 12331c6fdbd8SKent Overstreet if (io->op.op.error) { 123475812e70SKent Overstreet bio_for_each_segment_all(bvec, bio, iter) { 1235b3fce09cSKent Overstreet struct bch_page_state *s; 1236b3fce09cSKent Overstreet 12371c6fdbd8SKent Overstreet SetPageError(bvec->bv_page); 123875812e70SKent Overstreet mapping_set_error(bvec->bv_page->mapping, -EIO); 1239b3fce09cSKent Overstreet 1240b3fce09cSKent Overstreet lock_page(bvec->bv_page); 1241b3fce09cSKent Overstreet s = bch2_page_state(bvec->bv_page); 1242b3fce09cSKent Overstreet for (i = 0; i < PAGE_SECTORS; i++) 1243b3fce09cSKent Overstreet s->s[i].nr_replicas = 0; 1244b3fce09cSKent Overstreet unlock_page(bvec->bv_page); 124575812e70SKent Overstreet } 12461c6fdbd8SKent Overstreet } 12471c6fdbd8SKent Overstreet 12481c6fdbd8SKent Overstreet /* 12491c6fdbd8SKent Overstreet * racing with fallocate can cause us to add fewer sectors than 12501c6fdbd8SKent Overstreet * expected - but we shouldn't add more sectors than expected: 12511c6fdbd8SKent Overstreet */ 12521c6fdbd8SKent Overstreet BUG_ON(io->op.sectors_added > (s64) io->new_sectors); 12531c6fdbd8SKent Overstreet 12541c6fdbd8SKent Overstreet /* 12551c6fdbd8SKent Overstreet * (error (due to going RO) halfway through a page can screw that up 12561c6fdbd8SKent Overstreet * slightly) 12571c6fdbd8SKent Overstreet * XXX wtf? 12581c6fdbd8SKent Overstreet BUG_ON(io->op.sectors_added - io->new_sectors >= (s64) PAGE_SECTORS); 12591c6fdbd8SKent Overstreet */ 12601c6fdbd8SKent Overstreet 12611c6fdbd8SKent Overstreet /* 12621c6fdbd8SKent Overstreet * PageWriteback is effectively our ref on the inode - fixup i_blocks 12631c6fdbd8SKent Overstreet * before calling end_page_writeback: 12641c6fdbd8SKent Overstreet */ 12651c6fdbd8SKent Overstreet if (io->op.sectors_added != io->new_sectors) 12661c6fdbd8SKent Overstreet i_sectors_acct(c, io->op.inode, NULL, 12671c6fdbd8SKent Overstreet io->op.sectors_added - (s64) io->new_sectors); 12681c6fdbd8SKent Overstreet 12697f5e31e1SKent Overstreet bio_for_each_segment_all(bvec, bio, iter) { 12707f5e31e1SKent Overstreet struct bch_page_state *s = __bch2_page_state(bvec->bv_page); 12717f5e31e1SKent Overstreet 12727f5e31e1SKent Overstreet if (atomic_dec_and_test(&s->write_count)) 12731c6fdbd8SKent Overstreet end_page_writeback(bvec->bv_page); 12747f5e31e1SKent Overstreet } 12751c6fdbd8SKent Overstreet 12761c6fdbd8SKent Overstreet closure_return_with_destructor(&io->cl, bch2_writepage_io_free); 12771c6fdbd8SKent Overstreet } 12781c6fdbd8SKent Overstreet 12791c6fdbd8SKent Overstreet static void bch2_writepage_do_io(struct bch_writepage_state *w) 12801c6fdbd8SKent Overstreet { 12811c6fdbd8SKent Overstreet struct bch_writepage_io *io = w->io; 12821c6fdbd8SKent Overstreet 12831c6fdbd8SKent Overstreet w->io = NULL; 12841c6fdbd8SKent Overstreet closure_call(&io->op.op.cl, bch2_write, NULL, &io->cl); 12851c6fdbd8SKent Overstreet continue_at(&io->cl, bch2_writepage_io_done, NULL); 12861c6fdbd8SKent Overstreet } 12871c6fdbd8SKent Overstreet 12881c6fdbd8SKent Overstreet /* 12891c6fdbd8SKent Overstreet * Get a bch_writepage_io and add @page to it - appending to an existing one if 12901c6fdbd8SKent Overstreet * possible, else allocating a new one: 12911c6fdbd8SKent Overstreet */ 12921c6fdbd8SKent Overstreet static void bch2_writepage_io_alloc(struct bch_fs *c, 12931c6fdbd8SKent Overstreet struct bch_writepage_state *w, 12941c6fdbd8SKent Overstreet struct bch_inode_info *inode, 12957f5e31e1SKent Overstreet u64 sector, 12961c6fdbd8SKent Overstreet unsigned nr_replicas) 12971c6fdbd8SKent Overstreet { 12981c6fdbd8SKent Overstreet struct bch_write_op *op; 12991c6fdbd8SKent Overstreet 13001c6fdbd8SKent Overstreet w->io = container_of(bio_alloc_bioset(NULL, BIO_MAX_VECS, 13011c6fdbd8SKent Overstreet REQ_OP_WRITE, 13021c6fdbd8SKent Overstreet GFP_NOFS, 13031c6fdbd8SKent Overstreet &c->writepage_bioset), 13041c6fdbd8SKent Overstreet struct bch_writepage_io, op.op.wbio.bio); 13051c6fdbd8SKent Overstreet 13061c6fdbd8SKent Overstreet closure_init(&w->io->cl, NULL); 13071c6fdbd8SKent Overstreet w->io->new_sectors = 0; 13081c6fdbd8SKent Overstreet bch2_fswrite_op_init(&w->io->op, c, inode, w->opts, false); 13091c6fdbd8SKent Overstreet op = &w->io->op.op; 13101c6fdbd8SKent Overstreet op->nr_replicas = nr_replicas; 13111c6fdbd8SKent Overstreet op->res.nr_replicas = nr_replicas; 13121c6fdbd8SKent Overstreet op->write_point = writepoint_hashed(inode->ei_last_dirtied); 13137f5e31e1SKent Overstreet op->pos = POS(inode->v.i_ino, sector); 13147f5e31e1SKent Overstreet op->wbio.bio.bi_iter.bi_sector = sector; 13151c6fdbd8SKent Overstreet } 13161c6fdbd8SKent Overstreet 13171c6fdbd8SKent Overstreet static int __bch2_writepage(struct folio *folio, 13181c6fdbd8SKent Overstreet struct writeback_control *wbc, 13191c6fdbd8SKent Overstreet void *data) 13201c6fdbd8SKent Overstreet { 13211c6fdbd8SKent Overstreet struct page *page = &folio->page; 13221c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(page->mapping->host); 13231c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 13241c6fdbd8SKent Overstreet struct bch_writepage_state *w = data; 13257f5e31e1SKent Overstreet struct bch_page_state *s, orig; 13267f5e31e1SKent Overstreet unsigned i, offset, nr_replicas_this_write = U32_MAX; 13271c6fdbd8SKent Overstreet loff_t i_size = i_size_read(&inode->v); 13281c6fdbd8SKent Overstreet pgoff_t end_index = i_size >> PAGE_SHIFT; 1329e1036a2aSKent Overstreet int ret; 13301c6fdbd8SKent Overstreet 13311c6fdbd8SKent Overstreet EBUG_ON(!PageUptodate(page)); 13321c6fdbd8SKent Overstreet 13331c6fdbd8SKent Overstreet /* Is the page fully inside i_size? */ 13341c6fdbd8SKent Overstreet if (page->index < end_index) 13351c6fdbd8SKent Overstreet goto do_io; 13361c6fdbd8SKent Overstreet 13371c6fdbd8SKent Overstreet /* Is the page fully outside i_size? (truncate in progress) */ 13381c6fdbd8SKent Overstreet offset = i_size & (PAGE_SIZE - 1); 13391c6fdbd8SKent Overstreet if (page->index > end_index || !offset) { 13401c6fdbd8SKent Overstreet unlock_page(page); 13411c6fdbd8SKent Overstreet return 0; 13421c6fdbd8SKent Overstreet } 13431c6fdbd8SKent Overstreet 13441c6fdbd8SKent Overstreet /* 13451c6fdbd8SKent Overstreet * The page straddles i_size. It must be zeroed out on each and every 13461c6fdbd8SKent Overstreet * writepage invocation because it may be mmapped. "A file is mapped 13471c6fdbd8SKent Overstreet * in multiples of the page size. For a file that is not a multiple of 13481c6fdbd8SKent Overstreet * the page size, the remaining memory is zeroed when mapped, and 13491c6fdbd8SKent Overstreet * writes to that region are not written out to the file." 13501c6fdbd8SKent Overstreet */ 13511c6fdbd8SKent Overstreet zero_user_segment(page, offset, PAGE_SIZE); 13521c6fdbd8SKent Overstreet do_io: 1353f57a6a5dSKent Overstreet s = bch2_page_state_create(page, __GFP_NOFAIL); 1354f81b648dSKent Overstreet 1355f57a6a5dSKent Overstreet ret = bch2_get_page_disk_reservation(c, inode, page, true); 1356e1036a2aSKent Overstreet if (ret) { 1357e1036a2aSKent Overstreet SetPageError(page); 1358e1036a2aSKent Overstreet mapping_set_error(page->mapping, ret); 1359e1036a2aSKent Overstreet unlock_page(page); 1360e1036a2aSKent Overstreet return 0; 1361e1036a2aSKent Overstreet } 1362e1036a2aSKent Overstreet 13637f5e31e1SKent Overstreet /* Before unlocking the page, get copy of reservations: */ 13647f5e31e1SKent Overstreet orig = *s; 13657f5e31e1SKent Overstreet 13667f5e31e1SKent Overstreet for (i = 0; i < PAGE_SECTORS; i++) { 13672ba5d38bSKent Overstreet if (s->s[i].state < SECTOR_DIRTY) 13687f5e31e1SKent Overstreet continue; 13697f5e31e1SKent Overstreet 1370f81b648dSKent Overstreet nr_replicas_this_write = 1371f57a6a5dSKent Overstreet min_t(unsigned, nr_replicas_this_write, 1372f57a6a5dSKent Overstreet s->s[i].nr_replicas + 1373f57a6a5dSKent Overstreet s->s[i].replicas_reserved); 13747f5e31e1SKent Overstreet } 13751c6fdbd8SKent Overstreet 1376f57a6a5dSKent Overstreet for (i = 0; i < PAGE_SECTORS; i++) { 13772ba5d38bSKent Overstreet if (s->s[i].state < SECTOR_DIRTY) 13787f5e31e1SKent Overstreet continue; 13797f5e31e1SKent Overstreet 1380f57a6a5dSKent Overstreet s->s[i].nr_replicas = w->opts.compression 1381f57a6a5dSKent Overstreet ? 0 : nr_replicas_this_write; 1382e1036a2aSKent Overstreet 1383f57a6a5dSKent Overstreet s->s[i].replicas_reserved = 0; 1384f57a6a5dSKent Overstreet s->s[i].state = SECTOR_ALLOCATED; 1385f57a6a5dSKent Overstreet } 13861c6fdbd8SKent Overstreet 13877f5e31e1SKent Overstreet BUG_ON(atomic_read(&s->write_count)); 13887f5e31e1SKent Overstreet atomic_set(&s->write_count, 1); 13897f5e31e1SKent Overstreet 13901c6fdbd8SKent Overstreet BUG_ON(PageWriteback(page)); 13911c6fdbd8SKent Overstreet set_page_writeback(page); 13927f5e31e1SKent Overstreet 13931c6fdbd8SKent Overstreet unlock_page(page); 13941c6fdbd8SKent Overstreet 13957f5e31e1SKent Overstreet offset = 0; 13967f5e31e1SKent Overstreet while (1) { 13977f5e31e1SKent Overstreet unsigned sectors = 1, dirty_sectors = 0, reserved_sectors = 0; 13987f5e31e1SKent Overstreet u64 sector; 13997f5e31e1SKent Overstreet 14007f5e31e1SKent Overstreet while (offset < PAGE_SECTORS && 14012ba5d38bSKent Overstreet orig.s[offset].state < SECTOR_DIRTY) 14027f5e31e1SKent Overstreet offset++; 14037f5e31e1SKent Overstreet 14047f5e31e1SKent Overstreet if (offset == PAGE_SECTORS) 14057f5e31e1SKent Overstreet break; 14067f5e31e1SKent Overstreet 14077f5e31e1SKent Overstreet sector = ((u64) page->index << PAGE_SECTOR_SHIFT) + offset; 14087f5e31e1SKent Overstreet 14097f5e31e1SKent Overstreet while (offset + sectors < PAGE_SECTORS && 14102ba5d38bSKent Overstreet orig.s[offset + sectors].state >= SECTOR_DIRTY) 14117f5e31e1SKent Overstreet sectors++; 14127f5e31e1SKent Overstreet 14137f5e31e1SKent Overstreet for (i = offset; i < offset + sectors; i++) { 14147f5e31e1SKent Overstreet reserved_sectors += orig.s[i].replicas_reserved; 14157f5e31e1SKent Overstreet dirty_sectors += orig.s[i].state == SECTOR_DIRTY; 14167f5e31e1SKent Overstreet } 14177f5e31e1SKent Overstreet 14181c6fdbd8SKent Overstreet if (w->io && 1419f81b648dSKent Overstreet (w->io->op.op.res.nr_replicas != nr_replicas_this_write || 14207f5e31e1SKent Overstreet bio_full(&w->io->op.op.wbio.bio, PAGE_SIZE) || 14217f5e31e1SKent Overstreet bio_end_sector(&w->io->op.op.wbio.bio) != sector)) 14221c6fdbd8SKent Overstreet bch2_writepage_do_io(w); 14231c6fdbd8SKent Overstreet 14241c6fdbd8SKent Overstreet if (!w->io) 14257f5e31e1SKent Overstreet bch2_writepage_io_alloc(c, w, inode, sector, 1426f81b648dSKent Overstreet nr_replicas_this_write); 14271c6fdbd8SKent Overstreet 1428e1036a2aSKent Overstreet w->io->new_sectors += dirty_sectors; 14291c6fdbd8SKent Overstreet 14307f5e31e1SKent Overstreet atomic_inc(&s->write_count); 14317f5e31e1SKent Overstreet 14321c6fdbd8SKent Overstreet BUG_ON(inode != w->io->op.inode); 14337f5e31e1SKent Overstreet BUG_ON(!bio_add_page(&w->io->op.op.wbio.bio, page, 14347f5e31e1SKent Overstreet sectors << 9, offset << 9)); 14351c6fdbd8SKent Overstreet 1436f57a6a5dSKent Overstreet w->io->op.op.res.sectors += reserved_sectors; 14371c6fdbd8SKent Overstreet w->io->op.new_i_size = i_size; 14381c6fdbd8SKent Overstreet 14391c6fdbd8SKent Overstreet if (wbc->sync_mode == WB_SYNC_ALL) 14401c6fdbd8SKent Overstreet w->io->op.op.wbio.bio.bi_opf |= REQ_SYNC; 14411c6fdbd8SKent Overstreet 14427f5e31e1SKent Overstreet offset += sectors; 14437f5e31e1SKent Overstreet } 14447f5e31e1SKent Overstreet 14457f5e31e1SKent Overstreet if (atomic_dec_and_test(&s->write_count)) 14467f5e31e1SKent Overstreet end_page_writeback(page); 14477f5e31e1SKent Overstreet 14481c6fdbd8SKent Overstreet return 0; 14491c6fdbd8SKent Overstreet } 14501c6fdbd8SKent Overstreet 14511c6fdbd8SKent Overstreet int bch2_writepages(struct address_space *mapping, struct writeback_control *wbc) 14521c6fdbd8SKent Overstreet { 14531c6fdbd8SKent Overstreet struct bch_fs *c = mapping->host->i_sb->s_fs_info; 14541c6fdbd8SKent Overstreet struct bch_writepage_state w = 14551c6fdbd8SKent Overstreet bch_writepage_state_init(c, to_bch_ei(mapping->host)); 14561c6fdbd8SKent Overstreet struct blk_plug plug; 14571c6fdbd8SKent Overstreet int ret; 14581c6fdbd8SKent Overstreet 14591c6fdbd8SKent Overstreet blk_start_plug(&plug); 14601c6fdbd8SKent Overstreet ret = write_cache_pages(mapping, wbc, __bch2_writepage, &w); 14611c6fdbd8SKent Overstreet if (w.io) 14621c6fdbd8SKent Overstreet bch2_writepage_do_io(&w); 14631c6fdbd8SKent Overstreet blk_finish_plug(&plug); 14641c6fdbd8SKent Overstreet return ret; 14651c6fdbd8SKent Overstreet } 14661c6fdbd8SKent Overstreet 14671c6fdbd8SKent Overstreet int bch2_writepage(struct page *page, struct writeback_control *wbc) 14681c6fdbd8SKent Overstreet { 14691c6fdbd8SKent Overstreet struct bch_fs *c = page->mapping->host->i_sb->s_fs_info; 14701c6fdbd8SKent Overstreet struct bch_writepage_state w = 14711c6fdbd8SKent Overstreet bch_writepage_state_init(c, to_bch_ei(page->mapping->host)); 14721c6fdbd8SKent Overstreet int ret; 14731c6fdbd8SKent Overstreet 14741c6fdbd8SKent Overstreet ret = __bch2_writepage(page_folio(page), wbc, &w); 14751c6fdbd8SKent Overstreet if (w.io) 14761c6fdbd8SKent Overstreet bch2_writepage_do_io(&w); 14771c6fdbd8SKent Overstreet 14781c6fdbd8SKent Overstreet return ret; 14791c6fdbd8SKent Overstreet } 14801c6fdbd8SKent Overstreet 14811c6fdbd8SKent Overstreet /* buffered writes: */ 14821c6fdbd8SKent Overstreet 14831c6fdbd8SKent Overstreet int bch2_write_begin(struct file *file, struct address_space *mapping, 14841c6fdbd8SKent Overstreet loff_t pos, unsigned len, 14851c6fdbd8SKent Overstreet struct page **pagep, void **fsdata) 14861c6fdbd8SKent Overstreet { 14871c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(mapping->host); 14881c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 1489d1542e03SKent Overstreet struct bch2_page_reservation *res; 14901c6fdbd8SKent Overstreet pgoff_t index = pos >> PAGE_SHIFT; 14911c6fdbd8SKent Overstreet unsigned offset = pos & (PAGE_SIZE - 1); 14921c6fdbd8SKent Overstreet struct page *page; 14931c6fdbd8SKent Overstreet int ret = -ENOMEM; 14941c6fdbd8SKent Overstreet 1495d1542e03SKent Overstreet res = kmalloc(sizeof(*res), GFP_KERNEL); 1496d1542e03SKent Overstreet if (!res) 1497d1542e03SKent Overstreet return -ENOMEM; 1498d1542e03SKent Overstreet 1499d1542e03SKent Overstreet bch2_page_reservation_init(c, inode, res); 1500d1542e03SKent Overstreet *fsdata = res; 15011c6fdbd8SKent Overstreet 15021c6fdbd8SKent Overstreet bch2_pagecache_add_get(&inode->ei_pagecache_lock); 15031c6fdbd8SKent Overstreet 15041c6fdbd8SKent Overstreet page = grab_cache_page_write_begin(mapping, index); 15051c6fdbd8SKent Overstreet if (!page) 15061c6fdbd8SKent Overstreet goto err_unlock; 15071c6fdbd8SKent Overstreet 15081c6fdbd8SKent Overstreet if (PageUptodate(page)) 15091c6fdbd8SKent Overstreet goto out; 15101c6fdbd8SKent Overstreet 15111c6fdbd8SKent Overstreet /* If we're writing entire page, don't need to read it in first: */ 15121c6fdbd8SKent Overstreet if (len == PAGE_SIZE) 15131c6fdbd8SKent Overstreet goto out; 15141c6fdbd8SKent Overstreet 15151c6fdbd8SKent Overstreet if (!offset && pos + len >= inode->v.i_size) { 15161c6fdbd8SKent Overstreet zero_user_segment(page, len, PAGE_SIZE); 15171c6fdbd8SKent Overstreet flush_dcache_page(page); 15181c6fdbd8SKent Overstreet goto out; 15191c6fdbd8SKent Overstreet } 15201c6fdbd8SKent Overstreet 15211c6fdbd8SKent Overstreet if (index > inode->v.i_size >> PAGE_SHIFT) { 15221c6fdbd8SKent Overstreet zero_user_segments(page, 0, offset, offset + len, PAGE_SIZE); 15231c6fdbd8SKent Overstreet flush_dcache_page(page); 15241c6fdbd8SKent Overstreet goto out; 15251c6fdbd8SKent Overstreet } 15261c6fdbd8SKent Overstreet readpage: 15271c6fdbd8SKent Overstreet ret = bch2_read_single_page(page, mapping); 15281c6fdbd8SKent Overstreet if (ret) 15291c6fdbd8SKent Overstreet goto err; 15301c6fdbd8SKent Overstreet out: 1531d1542e03SKent Overstreet ret = bch2_page_reservation_get(c, inode, page, res, 1532d1542e03SKent Overstreet offset, len, true); 15331c6fdbd8SKent Overstreet if (ret) { 15341c6fdbd8SKent Overstreet if (!PageUptodate(page)) { 15351c6fdbd8SKent Overstreet /* 15361c6fdbd8SKent Overstreet * If the page hasn't been read in, we won't know if we 15371c6fdbd8SKent Overstreet * actually need a reservation - we don't actually need 15381c6fdbd8SKent Overstreet * to read here, we just need to check if the page is 15391c6fdbd8SKent Overstreet * fully backed by uncompressed data: 15401c6fdbd8SKent Overstreet */ 15411c6fdbd8SKent Overstreet goto readpage; 15421c6fdbd8SKent Overstreet } 15431c6fdbd8SKent Overstreet 15441c6fdbd8SKent Overstreet goto err; 15451c6fdbd8SKent Overstreet } 15461c6fdbd8SKent Overstreet 15471c6fdbd8SKent Overstreet *pagep = page; 15481c6fdbd8SKent Overstreet return 0; 15491c6fdbd8SKent Overstreet err: 15501c6fdbd8SKent Overstreet unlock_page(page); 15511c6fdbd8SKent Overstreet put_page(page); 15521c6fdbd8SKent Overstreet *pagep = NULL; 15531c6fdbd8SKent Overstreet err_unlock: 15541c6fdbd8SKent Overstreet bch2_pagecache_add_put(&inode->ei_pagecache_lock); 1555d1542e03SKent Overstreet kfree(res); 1556d1542e03SKent Overstreet *fsdata = NULL; 15571c6fdbd8SKent Overstreet return ret; 15581c6fdbd8SKent Overstreet } 15591c6fdbd8SKent Overstreet 15601c6fdbd8SKent Overstreet int bch2_write_end(struct file *file, struct address_space *mapping, 15611c6fdbd8SKent Overstreet loff_t pos, unsigned len, unsigned copied, 15621c6fdbd8SKent Overstreet struct page *page, void *fsdata) 15631c6fdbd8SKent Overstreet { 15641c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(mapping->host); 15651c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 1566d1542e03SKent Overstreet struct bch2_page_reservation *res = fsdata; 1567d1542e03SKent Overstreet unsigned offset = pos & (PAGE_SIZE - 1); 15681c6fdbd8SKent Overstreet 15691c6fdbd8SKent Overstreet lockdep_assert_held(&inode->v.i_rwsem); 15701c6fdbd8SKent Overstreet 15711c6fdbd8SKent Overstreet if (unlikely(copied < len && !PageUptodate(page))) { 15721c6fdbd8SKent Overstreet /* 15731c6fdbd8SKent Overstreet * The page needs to be read in, but that would destroy 15741c6fdbd8SKent Overstreet * our partial write - simplest thing is to just force 15751c6fdbd8SKent Overstreet * userspace to redo the write: 15761c6fdbd8SKent Overstreet */ 15771c6fdbd8SKent Overstreet zero_user(page, 0, PAGE_SIZE); 15781c6fdbd8SKent Overstreet flush_dcache_page(page); 15791c6fdbd8SKent Overstreet copied = 0; 15801c6fdbd8SKent Overstreet } 15811c6fdbd8SKent Overstreet 15821c6fdbd8SKent Overstreet spin_lock(&inode->v.i_lock); 15831c6fdbd8SKent Overstreet if (pos + copied > inode->v.i_size) 15841c6fdbd8SKent Overstreet i_size_write(&inode->v, pos + copied); 15851c6fdbd8SKent Overstreet spin_unlock(&inode->v.i_lock); 15861c6fdbd8SKent Overstreet 15871c6fdbd8SKent Overstreet if (copied) { 15881c6fdbd8SKent Overstreet if (!PageUptodate(page)) 15891c6fdbd8SKent Overstreet SetPageUptodate(page); 1590d1542e03SKent Overstreet 1591d1542e03SKent Overstreet bch2_set_page_dirty(c, inode, page, res, offset, copied); 15921c6fdbd8SKent Overstreet 15931c6fdbd8SKent Overstreet inode->ei_last_dirtied = (unsigned long) current; 15941c6fdbd8SKent Overstreet } 15951c6fdbd8SKent Overstreet 15961c6fdbd8SKent Overstreet unlock_page(page); 15971c6fdbd8SKent Overstreet put_page(page); 15981c6fdbd8SKent Overstreet bch2_pagecache_add_put(&inode->ei_pagecache_lock); 15991c6fdbd8SKent Overstreet 1600d1542e03SKent Overstreet bch2_page_reservation_put(c, inode, res); 1601d1542e03SKent Overstreet kfree(res); 1602d1542e03SKent Overstreet 16031c6fdbd8SKent Overstreet return copied; 16041c6fdbd8SKent Overstreet } 16051c6fdbd8SKent Overstreet 16061c6fdbd8SKent Overstreet #define WRITE_BATCH_PAGES 32 16071c6fdbd8SKent Overstreet 16081c6fdbd8SKent Overstreet static int __bch2_buffered_write(struct bch_inode_info *inode, 16091c6fdbd8SKent Overstreet struct address_space *mapping, 16101c6fdbd8SKent Overstreet struct iov_iter *iter, 16111c6fdbd8SKent Overstreet loff_t pos, unsigned len) 16121c6fdbd8SKent Overstreet { 16131c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 16141c6fdbd8SKent Overstreet struct page *pages[WRITE_BATCH_PAGES]; 1615d1542e03SKent Overstreet struct bch2_page_reservation res; 16161c6fdbd8SKent Overstreet unsigned long index = pos >> PAGE_SHIFT; 16171c6fdbd8SKent Overstreet unsigned offset = pos & (PAGE_SIZE - 1); 16181c6fdbd8SKent Overstreet unsigned nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE); 1619d1542e03SKent Overstreet unsigned i, reserved = 0, set_dirty = 0; 1620d1542e03SKent Overstreet unsigned copied = 0, nr_pages_copied = 0; 16211c6fdbd8SKent Overstreet int ret = 0; 16221c6fdbd8SKent Overstreet 16231c6fdbd8SKent Overstreet BUG_ON(!len); 16241c6fdbd8SKent Overstreet BUG_ON(nr_pages > ARRAY_SIZE(pages)); 16251c6fdbd8SKent Overstreet 1626d1542e03SKent Overstreet bch2_page_reservation_init(c, inode, &res); 1627d1542e03SKent Overstreet 16281c6fdbd8SKent Overstreet for (i = 0; i < nr_pages; i++) { 16291c6fdbd8SKent Overstreet pages[i] = grab_cache_page_write_begin(mapping, index + i); 16301c6fdbd8SKent Overstreet if (!pages[i]) { 16311c6fdbd8SKent Overstreet nr_pages = i; 16321c6fdbd8SKent Overstreet ret = -ENOMEM; 16331c6fdbd8SKent Overstreet goto out; 16341c6fdbd8SKent Overstreet } 16351c6fdbd8SKent Overstreet } 16361c6fdbd8SKent Overstreet 16371c6fdbd8SKent Overstreet if (offset && !PageUptodate(pages[0])) { 16381c6fdbd8SKent Overstreet ret = bch2_read_single_page(pages[0], mapping); 16391c6fdbd8SKent Overstreet if (ret) 16401c6fdbd8SKent Overstreet goto out; 16411c6fdbd8SKent Overstreet } 16421c6fdbd8SKent Overstreet 16431c6fdbd8SKent Overstreet if ((pos + len) & (PAGE_SIZE - 1) && 16441c6fdbd8SKent Overstreet !PageUptodate(pages[nr_pages - 1])) { 16451c6fdbd8SKent Overstreet if ((index + nr_pages - 1) << PAGE_SHIFT >= inode->v.i_size) { 16461c6fdbd8SKent Overstreet zero_user(pages[nr_pages - 1], 0, PAGE_SIZE); 16471c6fdbd8SKent Overstreet } else { 16481c6fdbd8SKent Overstreet ret = bch2_read_single_page(pages[nr_pages - 1], mapping); 16491c6fdbd8SKent Overstreet if (ret) 16501c6fdbd8SKent Overstreet goto out; 16511c6fdbd8SKent Overstreet } 16521c6fdbd8SKent Overstreet } 16531c6fdbd8SKent Overstreet 1654d1542e03SKent Overstreet while (reserved < len) { 1655d1542e03SKent Overstreet struct page *page = pages[(offset + reserved) >> PAGE_SHIFT]; 1656d1542e03SKent Overstreet unsigned pg_offset = (offset + reserved) & (PAGE_SIZE - 1); 1657d1542e03SKent Overstreet unsigned pg_len = min_t(unsigned, len - reserved, 1658d1542e03SKent Overstreet PAGE_SIZE - pg_offset); 1659d1542e03SKent Overstreet retry_reservation: 1660d1542e03SKent Overstreet ret = bch2_page_reservation_get(c, inode, page, &res, 1661d1542e03SKent Overstreet pg_offset, pg_len, true); 16621c6fdbd8SKent Overstreet 1663d1542e03SKent Overstreet if (ret && !PageUptodate(page)) { 1664d1542e03SKent Overstreet ret = bch2_read_single_page(page, mapping); 1665d1542e03SKent Overstreet if (!ret) 1666d1542e03SKent Overstreet goto retry_reservation; 16671c6fdbd8SKent Overstreet } 16681c6fdbd8SKent Overstreet 16691c6fdbd8SKent Overstreet if (ret) 16701c6fdbd8SKent Overstreet goto out; 1671d1542e03SKent Overstreet 1672d1542e03SKent Overstreet reserved += pg_len; 16731c6fdbd8SKent Overstreet } 16741c6fdbd8SKent Overstreet 16751c6fdbd8SKent Overstreet if (mapping_writably_mapped(mapping)) 16761c6fdbd8SKent Overstreet for (i = 0; i < nr_pages; i++) 16771c6fdbd8SKent Overstreet flush_dcache_page(pages[i]); 16781c6fdbd8SKent Overstreet 16791c6fdbd8SKent Overstreet while (copied < len) { 16801c6fdbd8SKent Overstreet struct page *page = pages[(offset + copied) >> PAGE_SHIFT]; 16811c6fdbd8SKent Overstreet unsigned pg_offset = (offset + copied) & (PAGE_SIZE - 1); 1682d1542e03SKent Overstreet unsigned pg_len = min_t(unsigned, len - copied, 16831c6fdbd8SKent Overstreet PAGE_SIZE - pg_offset); 16841c6fdbd8SKent Overstreet unsigned pg_copied = copy_page_from_iter_atomic(page, 1685d1542e03SKent Overstreet pg_offset, pg_len, iter); 1686d1542e03SKent Overstreet 1687d1542e03SKent Overstreet if (!pg_copied) 1688d1542e03SKent Overstreet break; 16891c6fdbd8SKent Overstreet 16901c6fdbd8SKent Overstreet flush_dcache_page(page); 16911c6fdbd8SKent Overstreet copied += pg_copied; 16921c6fdbd8SKent Overstreet } 16931c6fdbd8SKent Overstreet 16941c6fdbd8SKent Overstreet if (!copied) 16951c6fdbd8SKent Overstreet goto out; 16961c6fdbd8SKent Overstreet 16971c6fdbd8SKent Overstreet nr_pages_copied = DIV_ROUND_UP(offset + copied, PAGE_SIZE); 16981c6fdbd8SKent Overstreet inode->ei_last_dirtied = (unsigned long) current; 16991c6fdbd8SKent Overstreet 17001c6fdbd8SKent Overstreet spin_lock(&inode->v.i_lock); 17011c6fdbd8SKent Overstreet if (pos + copied > inode->v.i_size) 17021c6fdbd8SKent Overstreet i_size_write(&inode->v, pos + copied); 17031c6fdbd8SKent Overstreet spin_unlock(&inode->v.i_lock); 17041c6fdbd8SKent Overstreet 17051c6fdbd8SKent Overstreet if (copied < len && 17061c6fdbd8SKent Overstreet ((offset + copied) & (PAGE_SIZE - 1))) { 17071c6fdbd8SKent Overstreet struct page *page = pages[(offset + copied) >> PAGE_SHIFT]; 17081c6fdbd8SKent Overstreet 17091c6fdbd8SKent Overstreet if (!PageUptodate(page)) { 17101c6fdbd8SKent Overstreet zero_user(page, 0, PAGE_SIZE); 17111c6fdbd8SKent Overstreet copied -= (offset + copied) & (PAGE_SIZE - 1); 17121c6fdbd8SKent Overstreet } 17131c6fdbd8SKent Overstreet } 1714d1542e03SKent Overstreet 1715d1542e03SKent Overstreet while (set_dirty < copied) { 1716d1542e03SKent Overstreet struct page *page = pages[(offset + set_dirty) >> PAGE_SHIFT]; 1717d1542e03SKent Overstreet unsigned pg_offset = (offset + set_dirty) & (PAGE_SIZE - 1); 1718d1542e03SKent Overstreet unsigned pg_len = min_t(unsigned, copied - set_dirty, 1719d1542e03SKent Overstreet PAGE_SIZE - pg_offset); 1720d1542e03SKent Overstreet 1721d1542e03SKent Overstreet if (!PageUptodate(page)) 1722d1542e03SKent Overstreet SetPageUptodate(page); 1723d1542e03SKent Overstreet 1724d1542e03SKent Overstreet bch2_set_page_dirty(c, inode, page, &res, pg_offset, pg_len); 1725d1542e03SKent Overstreet unlock_page(page); 1726d1542e03SKent Overstreet put_page(page); 1727d1542e03SKent Overstreet 1728d1542e03SKent Overstreet set_dirty += pg_len; 1729d1542e03SKent Overstreet } 17301c6fdbd8SKent Overstreet out: 1731d1542e03SKent Overstreet for (i = nr_pages_copied; i < nr_pages; i++) { 17321c6fdbd8SKent Overstreet unlock_page(pages[i]); 17331c6fdbd8SKent Overstreet put_page(pages[i]); 17341c6fdbd8SKent Overstreet } 17351c6fdbd8SKent Overstreet 1736d1542e03SKent Overstreet bch2_page_reservation_put(c, inode, &res); 17371c6fdbd8SKent Overstreet 17381c6fdbd8SKent Overstreet return copied ?: ret; 17391c6fdbd8SKent Overstreet } 17401c6fdbd8SKent Overstreet 17411c6fdbd8SKent Overstreet static ssize_t bch2_buffered_write(struct kiocb *iocb, struct iov_iter *iter) 17421c6fdbd8SKent Overstreet { 17431c6fdbd8SKent Overstreet struct file *file = iocb->ki_filp; 17441c6fdbd8SKent Overstreet struct address_space *mapping = file->f_mapping; 17451c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 17461c6fdbd8SKent Overstreet loff_t pos = iocb->ki_pos; 17471c6fdbd8SKent Overstreet ssize_t written = 0; 17481c6fdbd8SKent Overstreet int ret = 0; 17491c6fdbd8SKent Overstreet 17501c6fdbd8SKent Overstreet bch2_pagecache_add_get(&inode->ei_pagecache_lock); 17511c6fdbd8SKent Overstreet 17521c6fdbd8SKent Overstreet do { 17531c6fdbd8SKent Overstreet unsigned offset = pos & (PAGE_SIZE - 1); 17541c6fdbd8SKent Overstreet unsigned bytes = min_t(unsigned long, iov_iter_count(iter), 17551c6fdbd8SKent Overstreet PAGE_SIZE * WRITE_BATCH_PAGES - offset); 17561c6fdbd8SKent Overstreet again: 17571c6fdbd8SKent Overstreet /* 17581c6fdbd8SKent Overstreet * Bring in the user page that we will copy from _first_. 17591c6fdbd8SKent Overstreet * Otherwise there's a nasty deadlock on copying from the 17601c6fdbd8SKent Overstreet * same page as we're writing to, without it being marked 17611c6fdbd8SKent Overstreet * up-to-date. 17621c6fdbd8SKent Overstreet * 17631c6fdbd8SKent Overstreet * Not only is this an optimisation, but it is also required 17641c6fdbd8SKent Overstreet * to check that the address is actually valid, when atomic 17651c6fdbd8SKent Overstreet * usercopies are used, below. 17661c6fdbd8SKent Overstreet */ 17671c6fdbd8SKent Overstreet if (unlikely(fault_in_iov_iter_readable(iter, bytes))) { 17681c6fdbd8SKent Overstreet bytes = min_t(unsigned long, iov_iter_count(iter), 17691c6fdbd8SKent Overstreet PAGE_SIZE - offset); 17701c6fdbd8SKent Overstreet 17711c6fdbd8SKent Overstreet if (unlikely(fault_in_iov_iter_readable(iter, bytes))) { 17721c6fdbd8SKent Overstreet ret = -EFAULT; 17731c6fdbd8SKent Overstreet break; 17741c6fdbd8SKent Overstreet } 17751c6fdbd8SKent Overstreet } 17761c6fdbd8SKent Overstreet 17771c6fdbd8SKent Overstreet if (unlikely(fatal_signal_pending(current))) { 17781c6fdbd8SKent Overstreet ret = -EINTR; 17791c6fdbd8SKent Overstreet break; 17801c6fdbd8SKent Overstreet } 17811c6fdbd8SKent Overstreet 17821c6fdbd8SKent Overstreet ret = __bch2_buffered_write(inode, mapping, iter, pos, bytes); 17831c6fdbd8SKent Overstreet if (unlikely(ret < 0)) 17841c6fdbd8SKent Overstreet break; 17851c6fdbd8SKent Overstreet 17861c6fdbd8SKent Overstreet cond_resched(); 17871c6fdbd8SKent Overstreet 17881c6fdbd8SKent Overstreet if (unlikely(ret == 0)) { 17891c6fdbd8SKent Overstreet /* 17901c6fdbd8SKent Overstreet * If we were unable to copy any data at all, we must 17911c6fdbd8SKent Overstreet * fall back to a single segment length write. 17921c6fdbd8SKent Overstreet * 17931c6fdbd8SKent Overstreet * If we didn't fallback here, we could livelock 17941c6fdbd8SKent Overstreet * because not all segments in the iov can be copied at 17951c6fdbd8SKent Overstreet * once without a pagefault. 17961c6fdbd8SKent Overstreet */ 17971c6fdbd8SKent Overstreet bytes = min_t(unsigned long, PAGE_SIZE - offset, 17981c6fdbd8SKent Overstreet iov_iter_single_seg_count(iter)); 17991c6fdbd8SKent Overstreet goto again; 18001c6fdbd8SKent Overstreet } 18011c6fdbd8SKent Overstreet pos += ret; 18021c6fdbd8SKent Overstreet written += ret; 18031c6fdbd8SKent Overstreet 18041c6fdbd8SKent Overstreet balance_dirty_pages_ratelimited(mapping); 18051c6fdbd8SKent Overstreet } while (iov_iter_count(iter)); 18061c6fdbd8SKent Overstreet 18071c6fdbd8SKent Overstreet bch2_pagecache_add_put(&inode->ei_pagecache_lock); 18081c6fdbd8SKent Overstreet 18091c6fdbd8SKent Overstreet return written ? written : ret; 18101c6fdbd8SKent Overstreet } 18111c6fdbd8SKent Overstreet 18121c6fdbd8SKent Overstreet /* O_DIRECT reads */ 18131c6fdbd8SKent Overstreet 18141c6fdbd8SKent Overstreet static void bch2_dio_read_complete(struct closure *cl) 18151c6fdbd8SKent Overstreet { 18161c6fdbd8SKent Overstreet struct dio_read *dio = container_of(cl, struct dio_read, cl); 18171c6fdbd8SKent Overstreet 18181c6fdbd8SKent Overstreet dio->req->ki_complete(dio->req, dio->ret); 18191c6fdbd8SKent Overstreet bio_check_pages_dirty(&dio->rbio.bio); /* transfers ownership */ 18201c6fdbd8SKent Overstreet } 18211c6fdbd8SKent Overstreet 18221c6fdbd8SKent Overstreet static void bch2_direct_IO_read_endio(struct bio *bio) 18231c6fdbd8SKent Overstreet { 18241c6fdbd8SKent Overstreet struct dio_read *dio = bio->bi_private; 18251c6fdbd8SKent Overstreet 18261c6fdbd8SKent Overstreet if (bio->bi_status) 18271c6fdbd8SKent Overstreet dio->ret = blk_status_to_errno(bio->bi_status); 18281c6fdbd8SKent Overstreet 18291c6fdbd8SKent Overstreet closure_put(&dio->cl); 18301c6fdbd8SKent Overstreet } 18311c6fdbd8SKent Overstreet 18321c6fdbd8SKent Overstreet static void bch2_direct_IO_read_split_endio(struct bio *bio) 18331c6fdbd8SKent Overstreet { 18341c6fdbd8SKent Overstreet bch2_direct_IO_read_endio(bio); 18351c6fdbd8SKent Overstreet bio_check_pages_dirty(bio); /* transfers ownership */ 18361c6fdbd8SKent Overstreet } 18371c6fdbd8SKent Overstreet 18381c6fdbd8SKent Overstreet static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter) 18391c6fdbd8SKent Overstreet { 18401c6fdbd8SKent Overstreet struct file *file = req->ki_filp; 18411c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 18421c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 18431c6fdbd8SKent Overstreet struct bch_io_opts opts = io_opts(c, inode); 18441c6fdbd8SKent Overstreet struct dio_read *dio; 18451c6fdbd8SKent Overstreet struct bio *bio; 18461c6fdbd8SKent Overstreet loff_t offset = req->ki_pos; 18471c6fdbd8SKent Overstreet bool sync = is_sync_kiocb(req); 18481c6fdbd8SKent Overstreet size_t shorten; 18491c6fdbd8SKent Overstreet ssize_t ret; 18501c6fdbd8SKent Overstreet 18511c6fdbd8SKent Overstreet if ((offset|iter->count) & (block_bytes(c) - 1)) 18521c6fdbd8SKent Overstreet return -EINVAL; 18531c6fdbd8SKent Overstreet 18541c6fdbd8SKent Overstreet ret = min_t(loff_t, iter->count, 18551c6fdbd8SKent Overstreet max_t(loff_t, 0, i_size_read(&inode->v) - offset)); 18561c6fdbd8SKent Overstreet 18571c6fdbd8SKent Overstreet if (!ret) 18581c6fdbd8SKent Overstreet return ret; 18591c6fdbd8SKent Overstreet 18601c6fdbd8SKent Overstreet shorten = iov_iter_count(iter) - round_up(ret, block_bytes(c)); 18611c6fdbd8SKent Overstreet iter->count -= shorten; 18621c6fdbd8SKent Overstreet 18631c6fdbd8SKent Overstreet bio = bio_alloc_bioset(NULL, 18641c6fdbd8SKent Overstreet iov_iter_npages(iter, BIO_MAX_VECS), 18651c6fdbd8SKent Overstreet REQ_OP_READ, 18661c6fdbd8SKent Overstreet GFP_KERNEL, 18671c6fdbd8SKent Overstreet &c->dio_read_bioset); 18681c6fdbd8SKent Overstreet 18691c6fdbd8SKent Overstreet bio->bi_end_io = bch2_direct_IO_read_endio; 18701c6fdbd8SKent Overstreet 18711c6fdbd8SKent Overstreet dio = container_of(bio, struct dio_read, rbio.bio); 18721c6fdbd8SKent Overstreet closure_init(&dio->cl, NULL); 18731c6fdbd8SKent Overstreet 18741c6fdbd8SKent Overstreet /* 18751c6fdbd8SKent Overstreet * this is a _really_ horrible hack just to avoid an atomic sub at the 18761c6fdbd8SKent Overstreet * end: 18771c6fdbd8SKent Overstreet */ 18781c6fdbd8SKent Overstreet if (!sync) { 18791c6fdbd8SKent Overstreet set_closure_fn(&dio->cl, bch2_dio_read_complete, NULL); 18801c6fdbd8SKent Overstreet atomic_set(&dio->cl.remaining, 18811c6fdbd8SKent Overstreet CLOSURE_REMAINING_INITIALIZER - 18821c6fdbd8SKent Overstreet CLOSURE_RUNNING + 18831c6fdbd8SKent Overstreet CLOSURE_DESTRUCTOR); 18841c6fdbd8SKent Overstreet } else { 18851c6fdbd8SKent Overstreet atomic_set(&dio->cl.remaining, 18861c6fdbd8SKent Overstreet CLOSURE_REMAINING_INITIALIZER + 1); 18871c6fdbd8SKent Overstreet } 18881c6fdbd8SKent Overstreet 18891c6fdbd8SKent Overstreet dio->req = req; 18901c6fdbd8SKent Overstreet dio->ret = ret; 18911c6fdbd8SKent Overstreet 18921c6fdbd8SKent Overstreet goto start; 18931c6fdbd8SKent Overstreet while (iter->count) { 18941c6fdbd8SKent Overstreet bio = bio_alloc_bioset(NULL, 18951c6fdbd8SKent Overstreet iov_iter_npages(iter, BIO_MAX_VECS), 18961c6fdbd8SKent Overstreet REQ_OP_READ, 18971c6fdbd8SKent Overstreet GFP_KERNEL, 18981c6fdbd8SKent Overstreet &c->bio_read); 18991c6fdbd8SKent Overstreet bio->bi_end_io = bch2_direct_IO_read_split_endio; 19001c6fdbd8SKent Overstreet start: 19011c6fdbd8SKent Overstreet bio->bi_opf = REQ_OP_READ|REQ_SYNC; 19021c6fdbd8SKent Overstreet bio->bi_iter.bi_sector = offset >> 9; 19031c6fdbd8SKent Overstreet bio->bi_private = dio; 19041c6fdbd8SKent Overstreet 19051c6fdbd8SKent Overstreet ret = bio_iov_iter_get_pages(bio, iter); 19061c6fdbd8SKent Overstreet if (ret < 0) { 19071c6fdbd8SKent Overstreet /* XXX: fault inject this path */ 19081c6fdbd8SKent Overstreet bio->bi_status = BLK_STS_RESOURCE; 19091c6fdbd8SKent Overstreet bio_endio(bio); 19101c6fdbd8SKent Overstreet break; 19111c6fdbd8SKent Overstreet } 19121c6fdbd8SKent Overstreet 19131c6fdbd8SKent Overstreet offset += bio->bi_iter.bi_size; 19141c6fdbd8SKent Overstreet bio_set_pages_dirty(bio); 19151c6fdbd8SKent Overstreet 19161c6fdbd8SKent Overstreet if (iter->count) 19171c6fdbd8SKent Overstreet closure_get(&dio->cl); 19181c6fdbd8SKent Overstreet 19191c6fdbd8SKent Overstreet bch2_read(c, rbio_init(bio, opts), inode->v.i_ino); 19201c6fdbd8SKent Overstreet } 19211c6fdbd8SKent Overstreet 19221c6fdbd8SKent Overstreet iter->count += shorten; 19231c6fdbd8SKent Overstreet 19241c6fdbd8SKent Overstreet if (sync) { 19251c6fdbd8SKent Overstreet closure_sync(&dio->cl); 19261c6fdbd8SKent Overstreet closure_debug_destroy(&dio->cl); 19271c6fdbd8SKent Overstreet ret = dio->ret; 19281c6fdbd8SKent Overstreet bio_check_pages_dirty(&dio->rbio.bio); /* transfers ownership */ 19291c6fdbd8SKent Overstreet return ret; 19301c6fdbd8SKent Overstreet } else { 19311c6fdbd8SKent Overstreet return -EIOCBQUEUED; 19321c6fdbd8SKent Overstreet } 19331c6fdbd8SKent Overstreet } 19341c6fdbd8SKent Overstreet 19351c6fdbd8SKent Overstreet ssize_t bch2_read_iter(struct kiocb *iocb, struct iov_iter *iter) 19361c6fdbd8SKent Overstreet { 19371c6fdbd8SKent Overstreet struct file *file = iocb->ki_filp; 19381c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 19391c6fdbd8SKent Overstreet struct address_space *mapping = file->f_mapping; 19401c6fdbd8SKent Overstreet size_t count = iov_iter_count(iter); 19411c6fdbd8SKent Overstreet ssize_t ret; 19421c6fdbd8SKent Overstreet 19431c6fdbd8SKent Overstreet if (!count) 19441c6fdbd8SKent Overstreet return 0; /* skip atime */ 19451c6fdbd8SKent Overstreet 19461c6fdbd8SKent Overstreet if (iocb->ki_flags & IOCB_DIRECT) { 19471c6fdbd8SKent Overstreet struct blk_plug plug; 19481c6fdbd8SKent Overstreet 19491c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 19501c6fdbd8SKent Overstreet iocb->ki_pos, 19511c6fdbd8SKent Overstreet iocb->ki_pos + count - 1); 19521c6fdbd8SKent Overstreet if (ret < 0) 19531c6fdbd8SKent Overstreet return ret; 19541c6fdbd8SKent Overstreet 19551c6fdbd8SKent Overstreet file_accessed(file); 19561c6fdbd8SKent Overstreet 19571c6fdbd8SKent Overstreet blk_start_plug(&plug); 19581c6fdbd8SKent Overstreet ret = bch2_direct_IO_read(iocb, iter); 19591c6fdbd8SKent Overstreet blk_finish_plug(&plug); 19601c6fdbd8SKent Overstreet 19611c6fdbd8SKent Overstreet if (ret >= 0) 19621c6fdbd8SKent Overstreet iocb->ki_pos += ret; 19631c6fdbd8SKent Overstreet } else { 19641c6fdbd8SKent Overstreet bch2_pagecache_add_get(&inode->ei_pagecache_lock); 19651c6fdbd8SKent Overstreet ret = generic_file_read_iter(iocb, iter); 19661c6fdbd8SKent Overstreet bch2_pagecache_add_put(&inode->ei_pagecache_lock); 19671c6fdbd8SKent Overstreet } 19681c6fdbd8SKent Overstreet 19691c6fdbd8SKent Overstreet return ret; 19701c6fdbd8SKent Overstreet } 19711c6fdbd8SKent Overstreet 19721c6fdbd8SKent Overstreet /* O_DIRECT writes */ 19731c6fdbd8SKent Overstreet 19741c6fdbd8SKent Overstreet /* 19751c6fdbd8SKent Overstreet * We're going to return -EIOCBQUEUED, but we haven't finished consuming the 19761c6fdbd8SKent Overstreet * iov_iter yet, so we need to stash a copy of the iovec: it might be on the 19771c6fdbd8SKent Overstreet * caller's stack, we're not guaranteed that it will live for the duration of 19781c6fdbd8SKent Overstreet * the IO: 19791c6fdbd8SKent Overstreet */ 19801c6fdbd8SKent Overstreet static noinline int bch2_dio_write_copy_iov(struct dio_write *dio) 19811c6fdbd8SKent Overstreet { 19821c6fdbd8SKent Overstreet struct iovec *iov = dio->inline_vecs; 19831c6fdbd8SKent Overstreet 19841c6fdbd8SKent Overstreet /* 19851c6fdbd8SKent Overstreet * iov_iter has a single embedded iovec - nothing to do: 19861c6fdbd8SKent Overstreet */ 19871c6fdbd8SKent Overstreet if (iter_is_ubuf(&dio->iter)) 19881c6fdbd8SKent Overstreet return 0; 19891c6fdbd8SKent Overstreet 19901c6fdbd8SKent Overstreet /* 19911c6fdbd8SKent Overstreet * We don't currently handle non-iovec iov_iters here - return an error, 19921c6fdbd8SKent Overstreet * and we'll fall back to doing the IO synchronously: 19931c6fdbd8SKent Overstreet */ 19941c6fdbd8SKent Overstreet if (!iter_is_iovec(&dio->iter)) 19951c6fdbd8SKent Overstreet return -1; 19961c6fdbd8SKent Overstreet 19971c6fdbd8SKent Overstreet if (dio->iter.nr_segs > ARRAY_SIZE(dio->inline_vecs)) { 19981c6fdbd8SKent Overstreet iov = kmalloc_array(dio->iter.nr_segs, sizeof(*iov), 19991c6fdbd8SKent Overstreet GFP_KERNEL); 20001c6fdbd8SKent Overstreet if (unlikely(!iov)) 20011c6fdbd8SKent Overstreet return -ENOMEM; 20021c6fdbd8SKent Overstreet 20031c6fdbd8SKent Overstreet dio->free_iov = true; 20041c6fdbd8SKent Overstreet } 20051c6fdbd8SKent Overstreet 20061c6fdbd8SKent Overstreet memcpy(iov, dio->iter.__iov, dio->iter.nr_segs * sizeof(*iov)); 20071c6fdbd8SKent Overstreet dio->iter.__iov = iov; 20081c6fdbd8SKent Overstreet return 0; 20091c6fdbd8SKent Overstreet } 20101c6fdbd8SKent Overstreet 20111c6fdbd8SKent Overstreet static void bch2_dio_write_loop_async(struct closure *); 20121c6fdbd8SKent Overstreet 20131c6fdbd8SKent Overstreet static long bch2_dio_write_loop(struct dio_write *dio) 20141c6fdbd8SKent Overstreet { 2015ed484030SKent Overstreet bool kthread = (current->flags & PF_KTHREAD) != 0; 20161c6fdbd8SKent Overstreet struct kiocb *req = dio->req; 20171c6fdbd8SKent Overstreet struct address_space *mapping = req->ki_filp->f_mapping; 20181c6fdbd8SKent Overstreet struct bch_inode_info *inode = dio->iop.inode; 20191c6fdbd8SKent Overstreet struct bio *bio = &dio->iop.op.wbio.bio; 20201c6fdbd8SKent Overstreet struct bvec_iter_all iter; 20211c6fdbd8SKent Overstreet struct bio_vec *bv; 2022919dbbd1SKent Overstreet loff_t offset; 20231c6fdbd8SKent Overstreet bool sync; 20241c6fdbd8SKent Overstreet long ret; 20251c6fdbd8SKent Overstreet 20261c6fdbd8SKent Overstreet if (dio->loop) 20271c6fdbd8SKent Overstreet goto loop; 20281c6fdbd8SKent Overstreet 20291c6fdbd8SKent Overstreet inode_dio_begin(&inode->v); 20301c6fdbd8SKent Overstreet bch2_pagecache_block_get(&inode->ei_pagecache_lock); 20311c6fdbd8SKent Overstreet 20321c6fdbd8SKent Overstreet /* Write and invalidate pagecache range that we're writing to: */ 2033919dbbd1SKent Overstreet offset = req->ki_pos + (dio->iop.op.written << 9); 2034919dbbd1SKent Overstreet ret = write_invalidate_inode_pages_range(mapping, 2035919dbbd1SKent Overstreet offset, 2036919dbbd1SKent Overstreet offset + iov_iter_count(&dio->iter) - 1); 20371c6fdbd8SKent Overstreet if (unlikely(ret)) 20381c6fdbd8SKent Overstreet goto err; 20391c6fdbd8SKent Overstreet 20401c6fdbd8SKent Overstreet while (1) { 2041919dbbd1SKent Overstreet offset = req->ki_pos + (dio->iop.op.written << 9); 2042919dbbd1SKent Overstreet 2043ed484030SKent Overstreet if (kthread) 2044ed484030SKent Overstreet kthread_use_mm(dio->mm); 20451c6fdbd8SKent Overstreet BUG_ON(current->faults_disabled_mapping); 20461c6fdbd8SKent Overstreet current->faults_disabled_mapping = mapping; 20471c6fdbd8SKent Overstreet 20481c6fdbd8SKent Overstreet ret = bio_iov_iter_get_pages(bio, &dio->iter); 20491c6fdbd8SKent Overstreet 20501c6fdbd8SKent Overstreet current->faults_disabled_mapping = NULL; 2051ed484030SKent Overstreet if (kthread) 2052ed484030SKent Overstreet kthread_unuse_mm(dio->mm); 20531c6fdbd8SKent Overstreet 20541c6fdbd8SKent Overstreet if (unlikely(ret < 0)) 20551c6fdbd8SKent Overstreet goto err; 20561c6fdbd8SKent Overstreet 20571c6fdbd8SKent Overstreet /* gup might have faulted pages back in: */ 20581c6fdbd8SKent Overstreet ret = write_invalidate_inode_pages_range(mapping, 2059919dbbd1SKent Overstreet offset, 2060919dbbd1SKent Overstreet offset + bio->bi_iter.bi_size - 1); 20611c6fdbd8SKent Overstreet if (unlikely(ret)) 20621c6fdbd8SKent Overstreet goto err; 20631c6fdbd8SKent Overstreet 2064919dbbd1SKent Overstreet dio->iop.op.pos = POS(inode->v.i_ino, offset >> 9); 20651c6fdbd8SKent Overstreet 20661c6fdbd8SKent Overstreet task_io_account_write(bio->bi_iter.bi_size); 20671c6fdbd8SKent Overstreet 20681c6fdbd8SKent Overstreet closure_call(&dio->iop.op.cl, bch2_write, NULL, &dio->cl); 20691c6fdbd8SKent Overstreet 20701c6fdbd8SKent Overstreet if (!dio->sync && !dio->loop && dio->iter.count) { 20711c6fdbd8SKent Overstreet if (bch2_dio_write_copy_iov(dio)) { 20721c6fdbd8SKent Overstreet dio->iop.op.error = -ENOMEM; 20731c6fdbd8SKent Overstreet goto err_wait_io; 20741c6fdbd8SKent Overstreet } 20751c6fdbd8SKent Overstreet } 20761c6fdbd8SKent Overstreet err_wait_io: 20771c6fdbd8SKent Overstreet dio->loop = true; 20781c6fdbd8SKent Overstreet 20791c6fdbd8SKent Overstreet if (!dio->sync) { 20801c6fdbd8SKent Overstreet continue_at(&dio->cl, bch2_dio_write_loop_async, NULL); 20811c6fdbd8SKent Overstreet return -EIOCBQUEUED; 20821c6fdbd8SKent Overstreet } 20831c6fdbd8SKent Overstreet 20841c6fdbd8SKent Overstreet closure_sync(&dio->cl); 20851c6fdbd8SKent Overstreet loop: 20861c6fdbd8SKent Overstreet bio_for_each_segment_all(bv, bio, iter) 20871c6fdbd8SKent Overstreet put_page(bv->bv_page); 20881c6fdbd8SKent Overstreet if (!dio->iter.count || dio->iop.op.error) 20891c6fdbd8SKent Overstreet break; 20901c6fdbd8SKent Overstreet bio_reset(bio, NULL, REQ_OP_WRITE); 20911c6fdbd8SKent Overstreet } 20921c6fdbd8SKent Overstreet 20931c6fdbd8SKent Overstreet ret = dio->iop.op.error ?: ((long) dio->iop.op.written << 9); 20941c6fdbd8SKent Overstreet err: 20951c6fdbd8SKent Overstreet bch2_pagecache_block_put(&inode->ei_pagecache_lock); 20961c6fdbd8SKent Overstreet bch2_disk_reservation_put(dio->iop.op.c, &dio->iop.op.res); 20971c6fdbd8SKent Overstreet bch2_quota_reservation_put(dio->iop.op.c, inode, &dio->quota_res); 20981c6fdbd8SKent Overstreet 20991c6fdbd8SKent Overstreet if (dio->free_iov) 21001c6fdbd8SKent Overstreet kfree(dio->iter.__iov); 21011c6fdbd8SKent Overstreet 21021c6fdbd8SKent Overstreet closure_debug_destroy(&dio->cl); 21031c6fdbd8SKent Overstreet 21041c6fdbd8SKent Overstreet sync = dio->sync; 21051c6fdbd8SKent Overstreet bio_put(bio); 21061c6fdbd8SKent Overstreet 21071c6fdbd8SKent Overstreet /* inode->i_dio_count is our ref on inode and thus bch_fs */ 21081c6fdbd8SKent Overstreet inode_dio_end(&inode->v); 21091c6fdbd8SKent Overstreet 21101c6fdbd8SKent Overstreet if (!sync) { 21111c6fdbd8SKent Overstreet req->ki_complete(req, ret); 21121c6fdbd8SKent Overstreet ret = -EIOCBQUEUED; 21131c6fdbd8SKent Overstreet } 21141c6fdbd8SKent Overstreet return ret; 21151c6fdbd8SKent Overstreet } 21161c6fdbd8SKent Overstreet 21171c6fdbd8SKent Overstreet static void bch2_dio_write_loop_async(struct closure *cl) 21181c6fdbd8SKent Overstreet { 21191c6fdbd8SKent Overstreet struct dio_write *dio = container_of(cl, struct dio_write, cl); 21201c6fdbd8SKent Overstreet 21211c6fdbd8SKent Overstreet bch2_dio_write_loop(dio); 21221c6fdbd8SKent Overstreet } 21231c6fdbd8SKent Overstreet 21241c6fdbd8SKent Overstreet static noinline 21251c6fdbd8SKent Overstreet ssize_t bch2_direct_write(struct kiocb *req, struct iov_iter *iter) 21261c6fdbd8SKent Overstreet { 21271c6fdbd8SKent Overstreet struct file *file = req->ki_filp; 21281c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 21291c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 21301c6fdbd8SKent Overstreet struct dio_write *dio; 21311c6fdbd8SKent Overstreet struct bio *bio; 21321c6fdbd8SKent Overstreet ssize_t ret; 21331c6fdbd8SKent Overstreet 21341c6fdbd8SKent Overstreet lockdep_assert_held(&inode->v.i_rwsem); 21351c6fdbd8SKent Overstreet 21361c6fdbd8SKent Overstreet if (unlikely(!iter->count)) 21371c6fdbd8SKent Overstreet return 0; 21381c6fdbd8SKent Overstreet 2139919dbbd1SKent Overstreet if (unlikely((req->ki_pos|iter->count) & (block_bytes(c) - 1))) 21401c6fdbd8SKent Overstreet return -EINVAL; 21411c6fdbd8SKent Overstreet 21421c6fdbd8SKent Overstreet bio = bio_alloc_bioset(NULL, 21431c6fdbd8SKent Overstreet iov_iter_npages(iter, BIO_MAX_VECS), 21441c6fdbd8SKent Overstreet REQ_OP_WRITE, 21451c6fdbd8SKent Overstreet GFP_KERNEL, 21461c6fdbd8SKent Overstreet &c->dio_write_bioset); 21471c6fdbd8SKent Overstreet dio = container_of(bio, struct dio_write, iop.op.wbio.bio); 21481c6fdbd8SKent Overstreet closure_init(&dio->cl, NULL); 21491c6fdbd8SKent Overstreet dio->req = req; 2150ed484030SKent Overstreet dio->mm = current->mm; 21511c6fdbd8SKent Overstreet dio->loop = false; 21521c6fdbd8SKent Overstreet dio->sync = is_sync_kiocb(req) || 2153919dbbd1SKent Overstreet req->ki_pos + iter->count > inode->v.i_size; 21541c6fdbd8SKent Overstreet dio->free_iov = false; 21551c6fdbd8SKent Overstreet dio->quota_res.sectors = 0; 21561c6fdbd8SKent Overstreet dio->iter = *iter; 21571c6fdbd8SKent Overstreet bch2_fswrite_op_init(&dio->iop, c, inode, io_opts(c, inode), true); 2158ed484030SKent Overstreet dio->iop.op.write_point = writepoint_hashed((unsigned long) current); 21591c6fdbd8SKent Overstreet dio->iop.op.flags |= BCH_WRITE_NOPUT_RESERVATION; 21601c6fdbd8SKent Overstreet 21611c6fdbd8SKent Overstreet if ((req->ki_flags & IOCB_DSYNC) && 21621c6fdbd8SKent Overstreet !c->opts.journal_flush_disabled) 21631c6fdbd8SKent Overstreet dio->iop.op.flags |= BCH_WRITE_FLUSH; 21641c6fdbd8SKent Overstreet 21651c6fdbd8SKent Overstreet ret = bch2_quota_reservation_add(c, inode, &dio->quota_res, 21661c6fdbd8SKent Overstreet iter->count >> 9, true); 21671c6fdbd8SKent Overstreet if (unlikely(ret)) 21681c6fdbd8SKent Overstreet goto err; 21691c6fdbd8SKent Overstreet 21707ef2a73aSKent Overstreet dio->iop.op.nr_replicas = dio->iop.op.opts.data_replicas; 21717ef2a73aSKent Overstreet 21721c6fdbd8SKent Overstreet ret = bch2_disk_reservation_get(c, &dio->iop.op.res, iter->count >> 9, 21731c6fdbd8SKent Overstreet dio->iop.op.opts.data_replicas, 0); 21741c6fdbd8SKent Overstreet if (unlikely(ret)) { 21757ef2a73aSKent Overstreet if (!bch2_check_range_allocated(c, POS(inode->v.i_ino, 2176919dbbd1SKent Overstreet req->ki_pos >> 9), 21777ef2a73aSKent Overstreet iter->count >> 9, 21787ef2a73aSKent Overstreet dio->iop.op.opts.data_replicas)) 21791c6fdbd8SKent Overstreet goto err; 21801c6fdbd8SKent Overstreet 21811c6fdbd8SKent Overstreet dio->iop.unalloc = true; 21821c6fdbd8SKent Overstreet } 21831c6fdbd8SKent Overstreet 21841c6fdbd8SKent Overstreet return bch2_dio_write_loop(dio); 21851c6fdbd8SKent Overstreet err: 21861c6fdbd8SKent Overstreet bch2_disk_reservation_put(c, &dio->iop.op.res); 21871c6fdbd8SKent Overstreet bch2_quota_reservation_put(c, inode, &dio->quota_res); 21881c6fdbd8SKent Overstreet closure_debug_destroy(&dio->cl); 21891c6fdbd8SKent Overstreet bio_put(bio); 21901c6fdbd8SKent Overstreet return ret; 21911c6fdbd8SKent Overstreet } 21921c6fdbd8SKent Overstreet 21931c6fdbd8SKent Overstreet static ssize_t __bch2_write_iter(struct kiocb *iocb, struct iov_iter *from) 21941c6fdbd8SKent Overstreet { 21951c6fdbd8SKent Overstreet struct file *file = iocb->ki_filp; 21961c6fdbd8SKent Overstreet ssize_t ret; 21971c6fdbd8SKent Overstreet 21981c6fdbd8SKent Overstreet if (iocb->ki_flags & IOCB_DIRECT) 21991c6fdbd8SKent Overstreet return bch2_direct_write(iocb, from); 22001c6fdbd8SKent Overstreet 22011c6fdbd8SKent Overstreet ret = file_remove_privs(file); 22021c6fdbd8SKent Overstreet if (ret) 22031c6fdbd8SKent Overstreet return ret; 22041c6fdbd8SKent Overstreet 22051c6fdbd8SKent Overstreet ret = file_update_time(file); 22061c6fdbd8SKent Overstreet if (ret) 22071c6fdbd8SKent Overstreet return ret; 22081c6fdbd8SKent Overstreet 22091c6fdbd8SKent Overstreet ret = iocb->ki_flags & IOCB_DIRECT 22101c6fdbd8SKent Overstreet ? bch2_direct_write(iocb, from) 22111c6fdbd8SKent Overstreet : bch2_buffered_write(iocb, from); 22121c6fdbd8SKent Overstreet 22131c6fdbd8SKent Overstreet if (likely(ret > 0)) 22141c6fdbd8SKent Overstreet iocb->ki_pos += ret; 22151c6fdbd8SKent Overstreet 22161c6fdbd8SKent Overstreet return ret; 22171c6fdbd8SKent Overstreet } 22181c6fdbd8SKent Overstreet 22191c6fdbd8SKent Overstreet ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *from) 22201c6fdbd8SKent Overstreet { 22211c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(iocb->ki_filp); 22221c6fdbd8SKent Overstreet bool direct = iocb->ki_flags & IOCB_DIRECT; 22231c6fdbd8SKent Overstreet ssize_t ret; 22241c6fdbd8SKent Overstreet 22251c6fdbd8SKent Overstreet inode_lock(&inode->v); 22261c6fdbd8SKent Overstreet ret = generic_write_checks(iocb, from); 22271c6fdbd8SKent Overstreet if (ret > 0) 22281c6fdbd8SKent Overstreet ret = __bch2_write_iter(iocb, from); 22291c6fdbd8SKent Overstreet inode_unlock(&inode->v); 22301c6fdbd8SKent Overstreet 22311c6fdbd8SKent Overstreet if (ret > 0 && !direct) 22321c6fdbd8SKent Overstreet ret = generic_write_sync(iocb, ret); 22331c6fdbd8SKent Overstreet 22341c6fdbd8SKent Overstreet return ret; 22351c6fdbd8SKent Overstreet } 22361c6fdbd8SKent Overstreet 22371c6fdbd8SKent Overstreet /* fsync: */ 22381c6fdbd8SKent Overstreet 22391c6fdbd8SKent Overstreet int bch2_fsync(struct file *file, loff_t start, loff_t end, int datasync) 22401c6fdbd8SKent Overstreet { 22411c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 22421c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 2243bb1b3658SKent Overstreet int ret, ret2; 22441c6fdbd8SKent Overstreet 22451c6fdbd8SKent Overstreet ret = file_write_and_wait_range(file, start, end); 22461c6fdbd8SKent Overstreet if (ret) 22471c6fdbd8SKent Overstreet return ret; 22481c6fdbd8SKent Overstreet 22491c6fdbd8SKent Overstreet if (datasync && !(inode->v.i_state & I_DIRTY_DATASYNC)) 22501c6fdbd8SKent Overstreet goto out; 22511c6fdbd8SKent Overstreet 22521c6fdbd8SKent Overstreet ret = sync_inode_metadata(&inode->v, 1); 22531c6fdbd8SKent Overstreet if (ret) 22541c6fdbd8SKent Overstreet return ret; 22551c6fdbd8SKent Overstreet out: 225675812e70SKent Overstreet if (!c->opts.journal_flush_disabled) 225775812e70SKent Overstreet ret = bch2_journal_flush_seq(&c->journal, 225875812e70SKent Overstreet inode->ei_journal_seq); 2259bb1b3658SKent Overstreet ret2 = file_check_and_advance_wb_err(file); 2260bb1b3658SKent Overstreet 2261bb1b3658SKent Overstreet return ret ?: ret2; 22621c6fdbd8SKent Overstreet } 22631c6fdbd8SKent Overstreet 22641c6fdbd8SKent Overstreet /* truncate: */ 22651c6fdbd8SKent Overstreet 2266*76426098SKent Overstreet int bch2_fpunch_at(struct btree_trans *trans, struct btree_iter *iter, 2267*76426098SKent Overstreet struct bpos end, struct bch_inode_info *inode, 2268*76426098SKent Overstreet u64 new_i_size) 226954e2264eSKent Overstreet { 2270*76426098SKent Overstreet struct bch_fs *c = trans->c; 227154e2264eSKent Overstreet unsigned max_sectors = KEY_SIZE_MAX & (~0 << c->block_bits); 227254e2264eSKent Overstreet struct bkey_s_c k; 2273*76426098SKent Overstreet int ret = 0, ret2 = 0; 227454e2264eSKent Overstreet 227554e2264eSKent Overstreet while ((k = bch2_btree_iter_peek(iter)).k && 227654e2264eSKent Overstreet bkey_cmp(iter->pos, end) < 0) { 227754e2264eSKent Overstreet struct disk_reservation disk_res = 227854e2264eSKent Overstreet bch2_disk_reservation_init(c, 0); 227954e2264eSKent Overstreet struct bkey_i delete; 228054e2264eSKent Overstreet 2281*76426098SKent Overstreet ret = bkey_err(k); 2282*76426098SKent Overstreet if (ret) 2283*76426098SKent Overstreet goto btree_err; 2284*76426098SKent Overstreet 228554e2264eSKent Overstreet bkey_init(&delete.k); 228654e2264eSKent Overstreet delete.k.p = iter->pos; 228754e2264eSKent Overstreet 228854e2264eSKent Overstreet /* create the biggest key we can */ 228954e2264eSKent Overstreet bch2_key_resize(&delete.k, max_sectors); 229054e2264eSKent Overstreet bch2_cut_back(end, &delete.k); 229154e2264eSKent Overstreet 2292*76426098SKent Overstreet bch2_trans_begin_updates(trans); 2293b17657d0SKent Overstreet 2294*76426098SKent Overstreet ret = bch2_extent_update(trans, inode, 229554e2264eSKent Overstreet &disk_res, NULL, iter, &delete, 2296*76426098SKent Overstreet new_i_size, false, true, NULL); 229754e2264eSKent Overstreet bch2_disk_reservation_put(c, &disk_res); 2298*76426098SKent Overstreet btree_err: 2299*76426098SKent Overstreet if (ret == -EINTR) { 2300*76426098SKent Overstreet ret2 = ret; 2301*76426098SKent Overstreet ret = 0; 2302*76426098SKent Overstreet } 2303*76426098SKent Overstreet if (ret) 2304*76426098SKent Overstreet break; 2305*76426098SKent Overstreet } 2306*76426098SKent Overstreet 2307*76426098SKent Overstreet if (bkey_cmp(iter->pos, end) > 0) { 2308*76426098SKent Overstreet bch2_btree_iter_set_pos(iter, end); 2309*76426098SKent Overstreet ret = bch2_btree_iter_traverse(iter); 2310*76426098SKent Overstreet } 2311*76426098SKent Overstreet 2312*76426098SKent Overstreet return ret ?: ret2; 2313*76426098SKent Overstreet } 2314*76426098SKent Overstreet 2315*76426098SKent Overstreet static int __bch2_fpunch(struct bch_fs *c, struct bch_inode_info *inode, 2316*76426098SKent Overstreet u64 start_offset, u64 end_offset) 2317*76426098SKent Overstreet { 2318*76426098SKent Overstreet struct btree_trans trans; 2319*76426098SKent Overstreet struct btree_iter *iter; 2320*76426098SKent Overstreet int ret = 0; 2321*76426098SKent Overstreet 2322*76426098SKent Overstreet bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024); 2323*76426098SKent Overstreet 2324*76426098SKent Overstreet iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS, 2325*76426098SKent Overstreet POS(inode->v.i_ino, start_offset), 2326*76426098SKent Overstreet BTREE_ITER_INTENT); 2327*76426098SKent Overstreet 2328*76426098SKent Overstreet ret = bch2_fpunch_at(&trans, iter, 2329*76426098SKent Overstreet POS(inode->v.i_ino, end_offset), 2330*76426098SKent Overstreet inode, 0); 2331*76426098SKent Overstreet 2332*76426098SKent Overstreet bch2_trans_exit(&trans); 233354e2264eSKent Overstreet 233454e2264eSKent Overstreet if (ret == -EINTR) 233554e2264eSKent Overstreet ret = 0; 233654e2264eSKent Overstreet 233754e2264eSKent Overstreet return ret; 233854e2264eSKent Overstreet } 233954e2264eSKent Overstreet 23401c6fdbd8SKent Overstreet static inline int range_has_data(struct bch_fs *c, 23411c6fdbd8SKent Overstreet struct bpos start, 23421c6fdbd8SKent Overstreet struct bpos end) 23431c6fdbd8SKent Overstreet { 2344424eb881SKent Overstreet struct btree_trans trans; 2345424eb881SKent Overstreet struct btree_iter *iter; 23461c6fdbd8SKent Overstreet struct bkey_s_c k; 23471c6fdbd8SKent Overstreet int ret = 0; 23481c6fdbd8SKent Overstreet 234920bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 2350424eb881SKent Overstreet 235194f651e2SKent Overstreet for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS, start, 0, k, ret) { 23521c6fdbd8SKent Overstreet if (bkey_cmp(bkey_start_pos(k.k), end) >= 0) 23531c6fdbd8SKent Overstreet break; 23541c6fdbd8SKent Overstreet 23551c6fdbd8SKent Overstreet if (bkey_extent_is_data(k.k)) { 23561c6fdbd8SKent Overstreet ret = 1; 23571c6fdbd8SKent Overstreet break; 23581c6fdbd8SKent Overstreet } 23591c6fdbd8SKent Overstreet } 23601c6fdbd8SKent Overstreet 2361424eb881SKent Overstreet return bch2_trans_exit(&trans) ?: ret; 23621c6fdbd8SKent Overstreet } 23631c6fdbd8SKent Overstreet 23641c6fdbd8SKent Overstreet static int __bch2_truncate_page(struct bch_inode_info *inode, 23651c6fdbd8SKent Overstreet pgoff_t index, loff_t start, loff_t end) 23661c6fdbd8SKent Overstreet { 23671c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 23681c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 2369a99b1cafSKent Overstreet struct bch_page_state *s; 23701c6fdbd8SKent Overstreet unsigned start_offset = start & (PAGE_SIZE - 1); 23711c6fdbd8SKent Overstreet unsigned end_offset = ((end - 1) & (PAGE_SIZE - 1)) + 1; 2372a99b1cafSKent Overstreet unsigned i; 23731c6fdbd8SKent Overstreet struct page *page; 23741c6fdbd8SKent Overstreet int ret = 0; 23751c6fdbd8SKent Overstreet 23761c6fdbd8SKent Overstreet /* Page boundary? Nothing to do */ 23771c6fdbd8SKent Overstreet if (!((index == start >> PAGE_SHIFT && start_offset) || 23781c6fdbd8SKent Overstreet (index == end >> PAGE_SHIFT && end_offset != PAGE_SIZE))) 23791c6fdbd8SKent Overstreet return 0; 23801c6fdbd8SKent Overstreet 23811c6fdbd8SKent Overstreet /* Above i_size? */ 23821c6fdbd8SKent Overstreet if (index << PAGE_SHIFT >= inode->v.i_size) 23831c6fdbd8SKent Overstreet return 0; 23841c6fdbd8SKent Overstreet 23851c6fdbd8SKent Overstreet page = find_lock_page(mapping, index); 23861c6fdbd8SKent Overstreet if (!page) { 23871c6fdbd8SKent Overstreet /* 23881c6fdbd8SKent Overstreet * XXX: we're doing two index lookups when we end up reading the 23891c6fdbd8SKent Overstreet * page 23901c6fdbd8SKent Overstreet */ 23911c6fdbd8SKent Overstreet ret = range_has_data(c, 23921c6fdbd8SKent Overstreet POS(inode->v.i_ino, index << PAGE_SECTOR_SHIFT), 23931c6fdbd8SKent Overstreet POS(inode->v.i_ino, (index + 1) << PAGE_SECTOR_SHIFT)); 23941c6fdbd8SKent Overstreet if (ret <= 0) 23951c6fdbd8SKent Overstreet return ret; 23961c6fdbd8SKent Overstreet 23971c6fdbd8SKent Overstreet page = find_or_create_page(mapping, index, GFP_KERNEL); 23981c6fdbd8SKent Overstreet if (unlikely(!page)) { 23991c6fdbd8SKent Overstreet ret = -ENOMEM; 24001c6fdbd8SKent Overstreet goto out; 24011c6fdbd8SKent Overstreet } 24021c6fdbd8SKent Overstreet } 24031c6fdbd8SKent Overstreet 2404a99b1cafSKent Overstreet s = bch2_page_state_create(page, 0); 2405a99b1cafSKent Overstreet if (!s) { 2406a99b1cafSKent Overstreet ret = -ENOMEM; 2407a99b1cafSKent Overstreet goto unlock; 2408a99b1cafSKent Overstreet } 2409a99b1cafSKent Overstreet 24101c6fdbd8SKent Overstreet if (!PageUptodate(page)) { 24111c6fdbd8SKent Overstreet ret = bch2_read_single_page(page, mapping); 24121c6fdbd8SKent Overstreet if (ret) 24131c6fdbd8SKent Overstreet goto unlock; 24141c6fdbd8SKent Overstreet } 24151c6fdbd8SKent Overstreet 2416a99b1cafSKent Overstreet if (index != start >> PAGE_SHIFT) 2417a99b1cafSKent Overstreet start_offset = 0; 2418a99b1cafSKent Overstreet if (index != end >> PAGE_SHIFT) 2419a99b1cafSKent Overstreet end_offset = PAGE_SIZE; 2420a99b1cafSKent Overstreet 2421a99b1cafSKent Overstreet for (i = round_up(start_offset, block_bytes(c)) >> 9; 2422a99b1cafSKent Overstreet i < round_down(end_offset, block_bytes(c)) >> 9; 2423a99b1cafSKent Overstreet i++) { 2424a99b1cafSKent Overstreet s->s[i].nr_replicas = 0; 2425a99b1cafSKent Overstreet s->s[i].state = SECTOR_UNALLOCATED; 2426a99b1cafSKent Overstreet } 2427a99b1cafSKent Overstreet 2428a99b1cafSKent Overstreet zero_user_segment(page, start_offset, end_offset); 2429a99b1cafSKent Overstreet 24301c6fdbd8SKent Overstreet /* 24311c6fdbd8SKent Overstreet * Bit of a hack - we don't want truncate to fail due to -ENOSPC. 24321c6fdbd8SKent Overstreet * 24331c6fdbd8SKent Overstreet * XXX: because we aren't currently tracking whether the page has actual 24341c6fdbd8SKent Overstreet * data in it (vs. just 0s, or only partially written) this wrong. ick. 24351c6fdbd8SKent Overstreet */ 2436d1542e03SKent Overstreet ret = bch2_get_page_disk_reservation(c, inode, page, false); 24371c6fdbd8SKent Overstreet BUG_ON(ret); 24381c6fdbd8SKent Overstreet 2439d1542e03SKent Overstreet filemap_dirty_folio(mapping, page_folio(page)); 24401c6fdbd8SKent Overstreet unlock: 24411c6fdbd8SKent Overstreet unlock_page(page); 24421c6fdbd8SKent Overstreet put_page(page); 24431c6fdbd8SKent Overstreet out: 24441c6fdbd8SKent Overstreet return ret; 24451c6fdbd8SKent Overstreet } 24461c6fdbd8SKent Overstreet 24471c6fdbd8SKent Overstreet static int bch2_truncate_page(struct bch_inode_info *inode, loff_t from) 24481c6fdbd8SKent Overstreet { 24491c6fdbd8SKent Overstreet return __bch2_truncate_page(inode, from >> PAGE_SHIFT, 2450a99b1cafSKent Overstreet from, round_up(from, PAGE_SIZE)); 24511c6fdbd8SKent Overstreet } 24521c6fdbd8SKent Overstreet 24531c6fdbd8SKent Overstreet static int bch2_extend(struct bch_inode_info *inode, struct iattr *iattr) 24541c6fdbd8SKent Overstreet { 24551c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 24561c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 24571c6fdbd8SKent Overstreet int ret; 24581c6fdbd8SKent Overstreet 24591c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 24601c6fdbd8SKent Overstreet inode->ei_inode.bi_size, S64_MAX); 24611c6fdbd8SKent Overstreet if (ret) 24621c6fdbd8SKent Overstreet return ret; 24631c6fdbd8SKent Overstreet 24641c6fdbd8SKent Overstreet truncate_setsize(&inode->v, iattr->ia_size); 24651c6fdbd8SKent Overstreet /* ATTR_MODE will never be set here, ns argument isn't needed: */ 24661c6fdbd8SKent Overstreet setattr_copy(NULL, &inode->v, iattr); 24671c6fdbd8SKent Overstreet 24681c6fdbd8SKent Overstreet mutex_lock(&inode->ei_update_lock); 24692ea90048SKent Overstreet ret = bch2_write_inode_size(c, inode, inode->v.i_size, 24702ea90048SKent Overstreet ATTR_MTIME|ATTR_CTIME); 24711c6fdbd8SKent Overstreet mutex_unlock(&inode->ei_update_lock); 24721c6fdbd8SKent Overstreet 24731c6fdbd8SKent Overstreet return ret; 24741c6fdbd8SKent Overstreet } 24751c6fdbd8SKent Overstreet 247654e2264eSKent Overstreet static int bch2_truncate_finish_fn(struct bch_inode_info *inode, 247754e2264eSKent Overstreet struct bch_inode_unpacked *bi, 247854e2264eSKent Overstreet void *p) 247954e2264eSKent Overstreet { 248054e2264eSKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 248154e2264eSKent Overstreet 248254e2264eSKent Overstreet bi->bi_flags &= ~BCH_INODE_I_SIZE_DIRTY; 248354e2264eSKent Overstreet bi->bi_mtime = bi->bi_ctime = bch2_current_time(c); 248454e2264eSKent Overstreet return 0; 248554e2264eSKent Overstreet } 248654e2264eSKent Overstreet 248754e2264eSKent Overstreet static int bch2_truncate_start_fn(struct bch_inode_info *inode, 248854e2264eSKent Overstreet struct bch_inode_unpacked *bi, void *p) 248954e2264eSKent Overstreet { 249054e2264eSKent Overstreet u64 *new_i_size = p; 249154e2264eSKent Overstreet 249254e2264eSKent Overstreet bi->bi_flags |= BCH_INODE_I_SIZE_DIRTY; 249354e2264eSKent Overstreet bi->bi_size = *new_i_size; 249454e2264eSKent Overstreet return 0; 249554e2264eSKent Overstreet } 249654e2264eSKent Overstreet 24971c6fdbd8SKent Overstreet int bch2_truncate(struct bch_inode_info *inode, struct iattr *iattr) 24981c6fdbd8SKent Overstreet { 24991c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 25001c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 250154e2264eSKent Overstreet u64 new_i_size = iattr->ia_size; 25021c6fdbd8SKent Overstreet bool shrink; 25031c6fdbd8SKent Overstreet int ret = 0; 25041c6fdbd8SKent Overstreet 25051c6fdbd8SKent Overstreet inode_dio_wait(&inode->v); 25061c6fdbd8SKent Overstreet bch2_pagecache_block_get(&inode->ei_pagecache_lock); 25071c6fdbd8SKent Overstreet 25081c6fdbd8SKent Overstreet BUG_ON(inode->v.i_size < inode->ei_inode.bi_size); 25091c6fdbd8SKent Overstreet 25101c6fdbd8SKent Overstreet shrink = iattr->ia_size <= inode->v.i_size; 25111c6fdbd8SKent Overstreet 25121c6fdbd8SKent Overstreet if (!shrink) { 25131c6fdbd8SKent Overstreet ret = bch2_extend(inode, iattr); 251454e2264eSKent Overstreet goto err; 25151c6fdbd8SKent Overstreet } 25161c6fdbd8SKent Overstreet 25171c6fdbd8SKent Overstreet ret = bch2_truncate_page(inode, iattr->ia_size); 25181c6fdbd8SKent Overstreet if (unlikely(ret)) 251954e2264eSKent Overstreet goto err; 25201c6fdbd8SKent Overstreet 25211c6fdbd8SKent Overstreet if (iattr->ia_size > inode->ei_inode.bi_size) 25221c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 25231c6fdbd8SKent Overstreet inode->ei_inode.bi_size, 25241c6fdbd8SKent Overstreet iattr->ia_size - 1); 25251c6fdbd8SKent Overstreet else if (iattr->ia_size & (PAGE_SIZE - 1)) 25261c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 25271c6fdbd8SKent Overstreet round_down(iattr->ia_size, PAGE_SIZE), 25281c6fdbd8SKent Overstreet iattr->ia_size - 1); 25291c6fdbd8SKent Overstreet if (ret) 253054e2264eSKent Overstreet goto err; 25311c6fdbd8SKent Overstreet 253254e2264eSKent Overstreet mutex_lock(&inode->ei_update_lock); 253354e2264eSKent Overstreet ret = bch2_write_inode(c, inode, bch2_truncate_start_fn, 253454e2264eSKent Overstreet &new_i_size, 0); 253554e2264eSKent Overstreet mutex_unlock(&inode->ei_update_lock); 25361c6fdbd8SKent Overstreet 25371c6fdbd8SKent Overstreet if (unlikely(ret)) 253854e2264eSKent Overstreet goto err; 25391c6fdbd8SKent Overstreet 25401c6fdbd8SKent Overstreet truncate_setsize(&inode->v, iattr->ia_size); 25411c6fdbd8SKent Overstreet 254254e2264eSKent Overstreet ret = __bch2_fpunch(c, inode, 2543a99b1cafSKent Overstreet round_up(iattr->ia_size, block_bytes(c)) >> 9, 2544*76426098SKent Overstreet U64_MAX); 25451c6fdbd8SKent Overstreet if (unlikely(ret)) 254654e2264eSKent Overstreet goto err; 25471c6fdbd8SKent Overstreet 25481c6fdbd8SKent Overstreet /* ATTR_MODE will never be set here, ns argument isn't needed: */ 25491c6fdbd8SKent Overstreet setattr_copy(NULL, &inode->v, iattr); 255054e2264eSKent Overstreet 255154e2264eSKent Overstreet mutex_lock(&inode->ei_update_lock); 255254e2264eSKent Overstreet ret = bch2_write_inode(c, inode, bch2_truncate_finish_fn, NULL, 255354e2264eSKent Overstreet ATTR_MTIME|ATTR_CTIME); 255454e2264eSKent Overstreet mutex_unlock(&inode->ei_update_lock); 255554e2264eSKent Overstreet err: 25561c6fdbd8SKent Overstreet bch2_pagecache_block_put(&inode->ei_pagecache_lock); 25571c6fdbd8SKent Overstreet return ret; 25581c6fdbd8SKent Overstreet } 25591c6fdbd8SKent Overstreet 25601c6fdbd8SKent Overstreet /* fallocate: */ 25611c6fdbd8SKent Overstreet 25621c6fdbd8SKent Overstreet static long bch2_fpunch(struct bch_inode_info *inode, loff_t offset, loff_t len) 25631c6fdbd8SKent Overstreet { 25641c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 2565a99b1cafSKent Overstreet u64 discard_start = round_up(offset, block_bytes(c)) >> 9; 2566a99b1cafSKent Overstreet u64 discard_end = round_down(offset + len, block_bytes(c)) >> 9; 25671c6fdbd8SKent Overstreet int ret = 0; 25681c6fdbd8SKent Overstreet 25691c6fdbd8SKent Overstreet inode_lock(&inode->v); 25701c6fdbd8SKent Overstreet inode_dio_wait(&inode->v); 25711c6fdbd8SKent Overstreet bch2_pagecache_block_get(&inode->ei_pagecache_lock); 25721c6fdbd8SKent Overstreet 25731c6fdbd8SKent Overstreet ret = __bch2_truncate_page(inode, 25741c6fdbd8SKent Overstreet offset >> PAGE_SHIFT, 25751c6fdbd8SKent Overstreet offset, offset + len); 25761c6fdbd8SKent Overstreet if (unlikely(ret)) 25771c6fdbd8SKent Overstreet goto err; 25781c6fdbd8SKent Overstreet 25791c6fdbd8SKent Overstreet if (offset >> PAGE_SHIFT != 25801c6fdbd8SKent Overstreet (offset + len) >> PAGE_SHIFT) { 25811c6fdbd8SKent Overstreet ret = __bch2_truncate_page(inode, 25821c6fdbd8SKent Overstreet (offset + len) >> PAGE_SHIFT, 25831c6fdbd8SKent Overstreet offset, offset + len); 25841c6fdbd8SKent Overstreet if (unlikely(ret)) 25851c6fdbd8SKent Overstreet goto err; 25861c6fdbd8SKent Overstreet } 25871c6fdbd8SKent Overstreet 25881c6fdbd8SKent Overstreet truncate_pagecache_range(&inode->v, offset, offset + len - 1); 25891c6fdbd8SKent Overstreet 25905f461e01SKent Overstreet if (discard_start < discard_end) 2591*76426098SKent Overstreet ret = __bch2_fpunch(c, inode, discard_start, discard_end); 25921c6fdbd8SKent Overstreet err: 25931c6fdbd8SKent Overstreet bch2_pagecache_block_put(&inode->ei_pagecache_lock); 25941c6fdbd8SKent Overstreet inode_unlock(&inode->v); 25951c6fdbd8SKent Overstreet 25961c6fdbd8SKent Overstreet return ret; 25971c6fdbd8SKent Overstreet } 25981c6fdbd8SKent Overstreet 25991c6fdbd8SKent Overstreet static long bch2_fcollapse(struct bch_inode_info *inode, 26001c6fdbd8SKent Overstreet loff_t offset, loff_t len) 26011c6fdbd8SKent Overstreet { 26021c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 26031c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 2604d69f41d6SKent Overstreet struct btree_trans trans; 2605d69f41d6SKent Overstreet struct btree_iter *src, *dst; 26061c6fdbd8SKent Overstreet BKEY_PADDED(k) copy; 26071c6fdbd8SKent Overstreet struct bkey_s_c k; 26081c6fdbd8SKent Overstreet loff_t new_size; 26091c6fdbd8SKent Overstreet int ret; 26101c6fdbd8SKent Overstreet 26111c6fdbd8SKent Overstreet if ((offset | len) & (block_bytes(c) - 1)) 26121c6fdbd8SKent Overstreet return -EINVAL; 26131c6fdbd8SKent Overstreet 261420bceecbSKent Overstreet bch2_trans_init(&trans, c, BTREE_ITER_MAX, 256); 26151c6fdbd8SKent Overstreet 26161c6fdbd8SKent Overstreet /* 26171c6fdbd8SKent Overstreet * We need i_mutex to keep the page cache consistent with the extents 26181c6fdbd8SKent Overstreet * btree, and the btree consistent with i_size - we don't need outside 26191c6fdbd8SKent Overstreet * locking for the extents btree itself, because we're using linked 26201c6fdbd8SKent Overstreet * iterators 26211c6fdbd8SKent Overstreet */ 26221c6fdbd8SKent Overstreet inode_lock(&inode->v); 26231c6fdbd8SKent Overstreet inode_dio_wait(&inode->v); 26241c6fdbd8SKent Overstreet bch2_pagecache_block_get(&inode->ei_pagecache_lock); 26251c6fdbd8SKent Overstreet 26261c6fdbd8SKent Overstreet ret = -EINVAL; 26271c6fdbd8SKent Overstreet if (offset + len >= inode->v.i_size) 26281c6fdbd8SKent Overstreet goto err; 26291c6fdbd8SKent Overstreet 26301c6fdbd8SKent Overstreet if (inode->v.i_size < len) 26311c6fdbd8SKent Overstreet goto err; 26321c6fdbd8SKent Overstreet 26331c6fdbd8SKent Overstreet new_size = inode->v.i_size - len; 26341c6fdbd8SKent Overstreet 26351c6fdbd8SKent Overstreet ret = write_invalidate_inode_pages_range(mapping, offset, LLONG_MAX); 26361c6fdbd8SKent Overstreet if (ret) 26371c6fdbd8SKent Overstreet goto err; 26381c6fdbd8SKent Overstreet 26398ef231bdSKent Overstreet dst = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS, 26408ef231bdSKent Overstreet POS(inode->v.i_ino, offset >> 9), 26418ef231bdSKent Overstreet BTREE_ITER_SLOTS|BTREE_ITER_INTENT); 26428ef231bdSKent Overstreet BUG_ON(IS_ERR_OR_NULL(dst)); 26438ef231bdSKent Overstreet 26448ef231bdSKent Overstreet src = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS, 26458ef231bdSKent Overstreet POS_MIN, BTREE_ITER_SLOTS); 26468ef231bdSKent Overstreet BUG_ON(IS_ERR_OR_NULL(src)); 26471c6fdbd8SKent Overstreet 2648d69f41d6SKent Overstreet while (bkey_cmp(dst->pos, 26491c6fdbd8SKent Overstreet POS(inode->v.i_ino, 2650a99b1cafSKent Overstreet round_up(new_size, block_bytes(c)) >> 9)) < 0) { 26511c6fdbd8SKent Overstreet struct disk_reservation disk_res; 26521c6fdbd8SKent Overstreet 26538ef231bdSKent Overstreet ret = bch2_btree_iter_traverse(dst); 26548ef231bdSKent Overstreet if (ret) 26550f238367SKent Overstreet goto bkey_err; 26568ef231bdSKent Overstreet 2657d69f41d6SKent Overstreet bch2_btree_iter_set_pos(src, 2658d69f41d6SKent Overstreet POS(dst->pos.inode, dst->pos.offset + (len >> 9))); 26591c6fdbd8SKent Overstreet 2660d69f41d6SKent Overstreet k = bch2_btree_iter_peek_slot(src); 26610f238367SKent Overstreet if ((ret = bkey_err(k))) 26620f238367SKent Overstreet goto bkey_err; 26631c6fdbd8SKent Overstreet 26641c6fdbd8SKent Overstreet bkey_reassemble(©.k, k); 26651c6fdbd8SKent Overstreet 2666d69f41d6SKent Overstreet bch2_cut_front(src->pos, ©.k); 26671c6fdbd8SKent Overstreet copy.k.k.p.offset -= len >> 9; 26681c6fdbd8SKent Overstreet 26693c7f3b7aSKent Overstreet ret = bch2_extent_trim_atomic(©.k, dst); 26703c7f3b7aSKent Overstreet if (ret) 26713c7f3b7aSKent Overstreet goto bkey_err; 2672e2d9912cSKent Overstreet 2673d69f41d6SKent Overstreet BUG_ON(bkey_cmp(dst->pos, bkey_start_pos(©.k.k))); 26741c6fdbd8SKent Overstreet 26751c6fdbd8SKent Overstreet ret = bch2_disk_reservation_get(c, &disk_res, copy.k.k.size, 267626609b61SKent Overstreet bch2_bkey_nr_dirty_ptrs(bkey_i_to_s_c(©.k)), 26771c6fdbd8SKent Overstreet BCH_DISK_RESERVATION_NOFAIL); 26781c6fdbd8SKent Overstreet BUG_ON(ret); 26791c6fdbd8SKent Overstreet 2680b17657d0SKent Overstreet bch2_trans_begin_updates(&trans); 2681b17657d0SKent Overstreet 26828ef231bdSKent Overstreet ret = bch2_extent_update(&trans, inode, 26838ef231bdSKent Overstreet &disk_res, NULL, 26848ef231bdSKent Overstreet dst, ©.k, 26858ef231bdSKent Overstreet 0, true, true, NULL); 26861c6fdbd8SKent Overstreet bch2_disk_reservation_put(c, &disk_res); 26870f238367SKent Overstreet bkey_err: 26881c6fdbd8SKent Overstreet if (ret == -EINTR) 26891c6fdbd8SKent Overstreet ret = 0; 26908ef231bdSKent Overstreet if (ret) 26918ef231bdSKent Overstreet goto err; 26921c6fdbd8SKent Overstreet /* 26931c6fdbd8SKent Overstreet * XXX: if we error here we've left data with multiple 26941c6fdbd8SKent Overstreet * pointers... which isn't a _super_ serious problem... 26951c6fdbd8SKent Overstreet */ 26961c6fdbd8SKent Overstreet 2697424eb881SKent Overstreet bch2_trans_cond_resched(&trans); 26981c6fdbd8SKent Overstreet } 26998ef231bdSKent Overstreet bch2_trans_unlock(&trans); 27001c6fdbd8SKent Overstreet 27018ef231bdSKent Overstreet ret = __bch2_fpunch(c, inode, 27021c6fdbd8SKent Overstreet round_up(new_size, block_bytes(c)) >> 9, 2703*76426098SKent Overstreet U64_MAX); 27041c6fdbd8SKent Overstreet if (ret) 27058ef231bdSKent Overstreet goto err; 27061c6fdbd8SKent Overstreet 27078ef231bdSKent Overstreet i_size_write(&inode->v, new_size); 27088ef231bdSKent Overstreet mutex_lock(&inode->ei_update_lock); 27098ef231bdSKent Overstreet ret = bch2_write_inode_size(c, inode, new_size, 27108ef231bdSKent Overstreet ATTR_MTIME|ATTR_CTIME); 27118ef231bdSKent Overstreet mutex_unlock(&inode->ei_update_lock); 27121c6fdbd8SKent Overstreet err: 27138ef231bdSKent Overstreet bch2_trans_exit(&trans); 27141c6fdbd8SKent Overstreet bch2_pagecache_block_put(&inode->ei_pagecache_lock); 27151c6fdbd8SKent Overstreet inode_unlock(&inode->v); 27161c6fdbd8SKent Overstreet return ret; 27171c6fdbd8SKent Overstreet } 27181c6fdbd8SKent Overstreet 27191c6fdbd8SKent Overstreet static long bch2_fallocate(struct bch_inode_info *inode, int mode, 27201c6fdbd8SKent Overstreet loff_t offset, loff_t len) 27211c6fdbd8SKent Overstreet { 27221c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 27231c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 2724190fa7afSKent Overstreet struct btree_trans trans; 2725190fa7afSKent Overstreet struct btree_iter *iter; 27261c6fdbd8SKent Overstreet struct bpos end_pos; 27271c6fdbd8SKent Overstreet loff_t end = offset + len; 2728a99b1cafSKent Overstreet loff_t block_start = round_down(offset, block_bytes(c)); 2729a99b1cafSKent Overstreet loff_t block_end = round_up(end, block_bytes(c)); 27301c6fdbd8SKent Overstreet unsigned sectors; 27311c6fdbd8SKent Overstreet unsigned replicas = io_opts(c, inode).data_replicas; 27321c6fdbd8SKent Overstreet int ret; 27331c6fdbd8SKent Overstreet 273420bceecbSKent Overstreet bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0); 27351c6fdbd8SKent Overstreet 27361c6fdbd8SKent Overstreet inode_lock(&inode->v); 27371c6fdbd8SKent Overstreet inode_dio_wait(&inode->v); 27381c6fdbd8SKent Overstreet bch2_pagecache_block_get(&inode->ei_pagecache_lock); 27391c6fdbd8SKent Overstreet 27401c6fdbd8SKent Overstreet if (!(mode & FALLOC_FL_KEEP_SIZE) && end > inode->v.i_size) { 27411c6fdbd8SKent Overstreet ret = inode_newsize_ok(&inode->v, end); 27421c6fdbd8SKent Overstreet if (ret) 27431c6fdbd8SKent Overstreet goto err; 27441c6fdbd8SKent Overstreet } 27451c6fdbd8SKent Overstreet 27461c6fdbd8SKent Overstreet if (mode & FALLOC_FL_ZERO_RANGE) { 27471c6fdbd8SKent Overstreet ret = __bch2_truncate_page(inode, 27481c6fdbd8SKent Overstreet offset >> PAGE_SHIFT, 27491c6fdbd8SKent Overstreet offset, end); 27501c6fdbd8SKent Overstreet 27511c6fdbd8SKent Overstreet if (!ret && 27521c6fdbd8SKent Overstreet offset >> PAGE_SHIFT != end >> PAGE_SHIFT) 27531c6fdbd8SKent Overstreet ret = __bch2_truncate_page(inode, 27541c6fdbd8SKent Overstreet end >> PAGE_SHIFT, 27551c6fdbd8SKent Overstreet offset, end); 27561c6fdbd8SKent Overstreet 27571c6fdbd8SKent Overstreet if (unlikely(ret)) 27581c6fdbd8SKent Overstreet goto err; 27591c6fdbd8SKent Overstreet 27601c6fdbd8SKent Overstreet truncate_pagecache_range(&inode->v, offset, end - 1); 27611c6fdbd8SKent Overstreet } 27621c6fdbd8SKent Overstreet 2763190fa7afSKent Overstreet iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS, 2764190fa7afSKent Overstreet POS(inode->v.i_ino, block_start >> 9), 2765190fa7afSKent Overstreet BTREE_ITER_SLOTS|BTREE_ITER_INTENT); 27661c6fdbd8SKent Overstreet end_pos = POS(inode->v.i_ino, block_end >> 9); 27671c6fdbd8SKent Overstreet 2768190fa7afSKent Overstreet while (bkey_cmp(iter->pos, end_pos) < 0) { 27691c6fdbd8SKent Overstreet struct disk_reservation disk_res = { 0 }; 2770190fa7afSKent Overstreet struct quota_res quota_res = { 0 }; 27711c6fdbd8SKent Overstreet struct bkey_i_reservation reservation; 27721c6fdbd8SKent Overstreet struct bkey_s_c k; 27731c6fdbd8SKent Overstreet 2774190fa7afSKent Overstreet k = bch2_btree_iter_peek_slot(iter); 27750f238367SKent Overstreet if ((ret = bkey_err(k))) 27760f238367SKent Overstreet goto bkey_err; 27771c6fdbd8SKent Overstreet 27781c6fdbd8SKent Overstreet /* already reserved */ 277926609b61SKent Overstreet if (k.k->type == KEY_TYPE_reservation && 27801c6fdbd8SKent Overstreet bkey_s_c_to_reservation(k).v->nr_replicas >= replicas) { 2781190fa7afSKent Overstreet bch2_btree_iter_next_slot(iter); 27821c6fdbd8SKent Overstreet continue; 27831c6fdbd8SKent Overstreet } 27841c6fdbd8SKent Overstreet 2785190fa7afSKent Overstreet if (bkey_extent_is_data(k.k) && 2786190fa7afSKent Overstreet !(mode & FALLOC_FL_ZERO_RANGE)) { 2787190fa7afSKent Overstreet bch2_btree_iter_next_slot(iter); 27881c6fdbd8SKent Overstreet continue; 27891c6fdbd8SKent Overstreet } 27901c6fdbd8SKent Overstreet 27911c6fdbd8SKent Overstreet bkey_reservation_init(&reservation.k_i); 279226609b61SKent Overstreet reservation.k.type = KEY_TYPE_reservation; 27931c6fdbd8SKent Overstreet reservation.k.p = k.k->p; 27941c6fdbd8SKent Overstreet reservation.k.size = k.k->size; 27951c6fdbd8SKent Overstreet 2796190fa7afSKent Overstreet bch2_cut_front(iter->pos, &reservation.k_i); 27971c6fdbd8SKent Overstreet bch2_cut_back(end_pos, &reservation.k); 27981c6fdbd8SKent Overstreet 27991c6fdbd8SKent Overstreet sectors = reservation.k.size; 280026609b61SKent Overstreet reservation.v.nr_replicas = bch2_bkey_nr_dirty_ptrs(k); 28011c6fdbd8SKent Overstreet 28021c6fdbd8SKent Overstreet if (!bkey_extent_is_allocation(k.k)) { 28031c6fdbd8SKent Overstreet ret = bch2_quota_reservation_add(c, inode, 2804190fa7afSKent Overstreet "a_res, 28051c6fdbd8SKent Overstreet sectors, true); 28061c6fdbd8SKent Overstreet if (unlikely(ret)) 28070f238367SKent Overstreet goto bkey_err; 28081c6fdbd8SKent Overstreet } 28091c6fdbd8SKent Overstreet 28101c6fdbd8SKent Overstreet if (reservation.v.nr_replicas < replicas || 28111c6fdbd8SKent Overstreet bch2_extent_is_compressed(k)) { 28121c6fdbd8SKent Overstreet ret = bch2_disk_reservation_get(c, &disk_res, sectors, 28131c6fdbd8SKent Overstreet replicas, 0); 28141c6fdbd8SKent Overstreet if (unlikely(ret)) 28150f238367SKent Overstreet goto bkey_err; 28161c6fdbd8SKent Overstreet 28171c6fdbd8SKent Overstreet reservation.v.nr_replicas = disk_res.nr_replicas; 28181c6fdbd8SKent Overstreet } 28191c6fdbd8SKent Overstreet 2820b17657d0SKent Overstreet bch2_trans_begin_updates(&trans); 2821b17657d0SKent Overstreet 2822190fa7afSKent Overstreet ret = bch2_extent_update(&trans, inode, 2823190fa7afSKent Overstreet &disk_res, "a_res, 2824190fa7afSKent Overstreet iter, &reservation.k_i, 2825190fa7afSKent Overstreet 0, true, true, NULL); 28260f238367SKent Overstreet bkey_err: 2827190fa7afSKent Overstreet bch2_quota_reservation_put(c, inode, "a_res); 28281c6fdbd8SKent Overstreet bch2_disk_reservation_put(c, &disk_res); 28291c6fdbd8SKent Overstreet if (ret == -EINTR) 28301c6fdbd8SKent Overstreet ret = 0; 2831190fa7afSKent Overstreet if (ret) 2832190fa7afSKent Overstreet goto err; 28331c6fdbd8SKent Overstreet } 2834190fa7afSKent Overstreet bch2_trans_unlock(&trans); 28351c6fdbd8SKent Overstreet 28361c6fdbd8SKent Overstreet if (!(mode & FALLOC_FL_KEEP_SIZE) && 28371c6fdbd8SKent Overstreet end > inode->v.i_size) { 28381c6fdbd8SKent Overstreet i_size_write(&inode->v, end); 28391c6fdbd8SKent Overstreet 28401c6fdbd8SKent Overstreet mutex_lock(&inode->ei_update_lock); 28412ea90048SKent Overstreet ret = bch2_write_inode_size(c, inode, inode->v.i_size, 0); 28421c6fdbd8SKent Overstreet mutex_unlock(&inode->ei_update_lock); 28431c6fdbd8SKent Overstreet } 28441c6fdbd8SKent Overstreet 28451c6fdbd8SKent Overstreet /* blech */ 28461c6fdbd8SKent Overstreet if ((mode & FALLOC_FL_KEEP_SIZE) && 28471c6fdbd8SKent Overstreet (mode & FALLOC_FL_ZERO_RANGE) && 28481c6fdbd8SKent Overstreet inode->ei_inode.bi_size != inode->v.i_size) { 28491c6fdbd8SKent Overstreet /* sync appends.. */ 28501c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 28511c6fdbd8SKent Overstreet inode->ei_inode.bi_size, S64_MAX); 28521c6fdbd8SKent Overstreet if (ret) 28531c6fdbd8SKent Overstreet goto err; 28541c6fdbd8SKent Overstreet 28551c6fdbd8SKent Overstreet if (inode->ei_inode.bi_size != inode->v.i_size) { 28561c6fdbd8SKent Overstreet mutex_lock(&inode->ei_update_lock); 28572ea90048SKent Overstreet ret = bch2_write_inode_size(c, inode, 28582ea90048SKent Overstreet inode->v.i_size, 0); 28591c6fdbd8SKent Overstreet mutex_unlock(&inode->ei_update_lock); 28601c6fdbd8SKent Overstreet } 28611c6fdbd8SKent Overstreet } 28621c6fdbd8SKent Overstreet err: 2863190fa7afSKent Overstreet bch2_trans_exit(&trans); 28641c6fdbd8SKent Overstreet bch2_pagecache_block_put(&inode->ei_pagecache_lock); 28651c6fdbd8SKent Overstreet inode_unlock(&inode->v); 28661c6fdbd8SKent Overstreet return ret; 28671c6fdbd8SKent Overstreet } 28681c6fdbd8SKent Overstreet 28691c6fdbd8SKent Overstreet long bch2_fallocate_dispatch(struct file *file, int mode, 28701c6fdbd8SKent Overstreet loff_t offset, loff_t len) 28711c6fdbd8SKent Overstreet { 28721c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 28731c6fdbd8SKent Overstreet 28741c6fdbd8SKent Overstreet if (!(mode & ~(FALLOC_FL_KEEP_SIZE|FALLOC_FL_ZERO_RANGE))) 28751c6fdbd8SKent Overstreet return bch2_fallocate(inode, mode, offset, len); 28761c6fdbd8SKent Overstreet 28771c6fdbd8SKent Overstreet if (mode == (FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE)) 28781c6fdbd8SKent Overstreet return bch2_fpunch(inode, offset, len); 28791c6fdbd8SKent Overstreet 28801c6fdbd8SKent Overstreet if (mode == FALLOC_FL_COLLAPSE_RANGE) 28811c6fdbd8SKent Overstreet return bch2_fcollapse(inode, offset, len); 28821c6fdbd8SKent Overstreet 28831c6fdbd8SKent Overstreet return -EOPNOTSUPP; 28841c6fdbd8SKent Overstreet } 28851c6fdbd8SKent Overstreet 2886*76426098SKent Overstreet static void mark_range_unallocated(struct bch_inode_info *inode, 2887*76426098SKent Overstreet loff_t start, loff_t end) 2888*76426098SKent Overstreet { 2889*76426098SKent Overstreet pgoff_t index = start >> PAGE_SHIFT; 2890*76426098SKent Overstreet pgoff_t end_index = (end - 1) >> PAGE_SHIFT; 2891*76426098SKent Overstreet struct folio_batch fbatch; 2892*76426098SKent Overstreet unsigned i, j; 2893*76426098SKent Overstreet 2894*76426098SKent Overstreet folio_batch_init(&fbatch); 2895*76426098SKent Overstreet 2896*76426098SKent Overstreet while (filemap_get_folios(inode->v.i_mapping, 2897*76426098SKent Overstreet &index, end_index, &fbatch)) { 2898*76426098SKent Overstreet for (i = 0; i < folio_batch_count(&fbatch); i++) { 2899*76426098SKent Overstreet struct folio *folio = fbatch.folios[i]; 2900*76426098SKent Overstreet struct bch_page_state *s; 2901*76426098SKent Overstreet 2902*76426098SKent Overstreet folio_lock(folio); 2903*76426098SKent Overstreet s = bch2_page_state(&folio->page); 2904*76426098SKent Overstreet 2905*76426098SKent Overstreet if (s) 2906*76426098SKent Overstreet for (j = 0; j < PAGE_SECTORS; j++) 2907*76426098SKent Overstreet s->s[j].nr_replicas = 0; 2908*76426098SKent Overstreet 2909*76426098SKent Overstreet folio_unlock(folio); 2910*76426098SKent Overstreet } 2911*76426098SKent Overstreet folio_batch_release(&fbatch); 2912*76426098SKent Overstreet cond_resched(); 2913*76426098SKent Overstreet } 2914*76426098SKent Overstreet } 2915*76426098SKent Overstreet 2916*76426098SKent Overstreet loff_t bch2_remap_file_range(struct file *file_src, loff_t pos_src, 2917*76426098SKent Overstreet struct file *file_dst, loff_t pos_dst, 2918*76426098SKent Overstreet loff_t len, unsigned remap_flags) 2919*76426098SKent Overstreet { 2920*76426098SKent Overstreet struct bch_inode_info *src = file_bch_inode(file_src); 2921*76426098SKent Overstreet struct bch_inode_info *dst = file_bch_inode(file_dst); 2922*76426098SKent Overstreet struct bch_fs *c = src->v.i_sb->s_fs_info; 2923*76426098SKent Overstreet loff_t ret = 0; 2924*76426098SKent Overstreet loff_t aligned_len; 2925*76426098SKent Overstreet 2926*76426098SKent Overstreet if (remap_flags & ~(REMAP_FILE_DEDUP|REMAP_FILE_ADVISORY)) 2927*76426098SKent Overstreet return -EINVAL; 2928*76426098SKent Overstreet 2929*76426098SKent Overstreet if (remap_flags & REMAP_FILE_DEDUP) 2930*76426098SKent Overstreet return -EOPNOTSUPP; 2931*76426098SKent Overstreet 2932*76426098SKent Overstreet if ((pos_src & (block_bytes(c) - 1)) || 2933*76426098SKent Overstreet (pos_dst & (block_bytes(c) - 1))) 2934*76426098SKent Overstreet return -EINVAL; 2935*76426098SKent Overstreet 2936*76426098SKent Overstreet if (src == dst && 2937*76426098SKent Overstreet abs(pos_src - pos_dst) < len) 2938*76426098SKent Overstreet return -EINVAL; 2939*76426098SKent Overstreet 2940*76426098SKent Overstreet bch2_lock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst); 2941*76426098SKent Overstreet 2942*76426098SKent Overstreet inode_dio_wait(&src->v); 2943*76426098SKent Overstreet inode_dio_wait(&dst->v); 2944*76426098SKent Overstreet 2945*76426098SKent Overstreet ret = generic_remap_file_range_prep(file_src, pos_src, 2946*76426098SKent Overstreet file_dst, pos_dst, 2947*76426098SKent Overstreet &len, remap_flags); 2948*76426098SKent Overstreet if (ret < 0 || len == 0) 2949*76426098SKent Overstreet goto out_unlock; 2950*76426098SKent Overstreet 2951*76426098SKent Overstreet aligned_len = round_up(len, block_bytes(c)); 2952*76426098SKent Overstreet 2953*76426098SKent Overstreet ret = write_invalidate_inode_pages_range(dst->v.i_mapping, 2954*76426098SKent Overstreet pos_dst, pos_dst + aligned_len); 2955*76426098SKent Overstreet if (ret) 2956*76426098SKent Overstreet goto out_unlock; 2957*76426098SKent Overstreet 2958*76426098SKent Overstreet mark_range_unallocated(src, pos_src, pos_src + aligned_len); 2959*76426098SKent Overstreet 2960*76426098SKent Overstreet ret = bch2_remap_range(c, dst, 2961*76426098SKent Overstreet POS(dst->v.i_ino, pos_dst >> 9), 2962*76426098SKent Overstreet POS(src->v.i_ino, pos_src >> 9), 2963*76426098SKent Overstreet aligned_len >> 9, 2964*76426098SKent Overstreet pos_dst + len); 2965*76426098SKent Overstreet if (ret > 0) 2966*76426098SKent Overstreet ret = min(ret << 9, len); 2967*76426098SKent Overstreet 2968*76426098SKent Overstreet out_unlock: 2969*76426098SKent Overstreet bch2_unlock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst); 2970*76426098SKent Overstreet 2971*76426098SKent Overstreet return ret; 2972*76426098SKent Overstreet } 2973*76426098SKent Overstreet 29741c6fdbd8SKent Overstreet /* fseek: */ 29751c6fdbd8SKent Overstreet 2976543ef2ebSKent Overstreet static int folio_data_offset(struct folio *folio, unsigned offset) 29771c6fdbd8SKent Overstreet { 2978f57a6a5dSKent Overstreet struct bch_page_state *s = bch2_page_state(&folio->page); 2979f57a6a5dSKent Overstreet unsigned i; 2980f81b648dSKent Overstreet 2981543ef2ebSKent Overstreet if (s) 2982543ef2ebSKent Overstreet for (i = offset >> 9; i < PAGE_SECTORS; i++) 2983f57a6a5dSKent Overstreet if (s->s[i].state >= SECTOR_DIRTY) 2984543ef2ebSKent Overstreet return i << 9; 2985f57a6a5dSKent Overstreet 2986543ef2ebSKent Overstreet return -1; 29871c6fdbd8SKent Overstreet } 29881c6fdbd8SKent Overstreet 2989543ef2ebSKent Overstreet static loff_t bch2_seek_pagecache_data(struct inode *vinode, 29901c6fdbd8SKent Overstreet loff_t start_offset, 29911c6fdbd8SKent Overstreet loff_t end_offset) 29921c6fdbd8SKent Overstreet { 29931c6fdbd8SKent Overstreet struct folio_batch fbatch; 29941c6fdbd8SKent Overstreet pgoff_t start_index = start_offset >> PAGE_SHIFT; 29951c6fdbd8SKent Overstreet pgoff_t end_index = end_offset >> PAGE_SHIFT; 29961c6fdbd8SKent Overstreet pgoff_t index = start_index; 29971c6fdbd8SKent Overstreet unsigned i; 2998543ef2ebSKent Overstreet loff_t ret; 2999543ef2ebSKent Overstreet int offset; 30001c6fdbd8SKent Overstreet 30011c6fdbd8SKent Overstreet folio_batch_init(&fbatch); 30021c6fdbd8SKent Overstreet 30031c6fdbd8SKent Overstreet while (filemap_get_folios(vinode->i_mapping, 30041c6fdbd8SKent Overstreet &index, end_index, &fbatch)) { 30051c6fdbd8SKent Overstreet for (i = 0; i < folio_batch_count(&fbatch); i++) { 30061c6fdbd8SKent Overstreet struct folio *folio = fbatch.folios[i]; 30071c6fdbd8SKent Overstreet 30081c6fdbd8SKent Overstreet folio_lock(folio); 3009543ef2ebSKent Overstreet offset = folio_data_offset(folio, 3010543ef2ebSKent Overstreet folio->index == start_index 3011543ef2ebSKent Overstreet ? start_offset & (PAGE_SIZE - 1) 3012543ef2ebSKent Overstreet : 0); 3013543ef2ebSKent Overstreet if (offset >= 0) { 3014543ef2ebSKent Overstreet ret = clamp(((loff_t) folio->index << PAGE_SHIFT) + 3015543ef2ebSKent Overstreet offset, 3016543ef2ebSKent Overstreet start_offset, end_offset); 30171c6fdbd8SKent Overstreet folio_unlock(folio); 30181c6fdbd8SKent Overstreet folio_batch_release(&fbatch); 3019543ef2ebSKent Overstreet return ret; 30201c6fdbd8SKent Overstreet } 30211c6fdbd8SKent Overstreet folio_unlock(folio); 30221c6fdbd8SKent Overstreet } 30231c6fdbd8SKent Overstreet folio_batch_release(&fbatch); 30241c6fdbd8SKent Overstreet cond_resched(); 30251c6fdbd8SKent Overstreet } 30261c6fdbd8SKent Overstreet 30271c6fdbd8SKent Overstreet return end_offset; 30281c6fdbd8SKent Overstreet } 30291c6fdbd8SKent Overstreet 30301c6fdbd8SKent Overstreet static loff_t bch2_seek_data(struct file *file, u64 offset) 30311c6fdbd8SKent Overstreet { 30321c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 30331c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 3034424eb881SKent Overstreet struct btree_trans trans; 3035424eb881SKent Overstreet struct btree_iter *iter; 30361c6fdbd8SKent Overstreet struct bkey_s_c k; 30371c6fdbd8SKent Overstreet u64 isize, next_data = MAX_LFS_FILESIZE; 30381c6fdbd8SKent Overstreet int ret; 30391c6fdbd8SKent Overstreet 30401c6fdbd8SKent Overstreet isize = i_size_read(&inode->v); 30411c6fdbd8SKent Overstreet if (offset >= isize) 30421c6fdbd8SKent Overstreet return -ENXIO; 30431c6fdbd8SKent Overstreet 304420bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 3045424eb881SKent Overstreet 3046424eb881SKent Overstreet for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS, 304794f651e2SKent Overstreet POS(inode->v.i_ino, offset >> 9), 0, k, ret) { 30481c6fdbd8SKent Overstreet if (k.k->p.inode != inode->v.i_ino) { 30491c6fdbd8SKent Overstreet break; 30501c6fdbd8SKent Overstreet } else if (bkey_extent_is_data(k.k)) { 30511c6fdbd8SKent Overstreet next_data = max(offset, bkey_start_offset(k.k) << 9); 30521c6fdbd8SKent Overstreet break; 30531c6fdbd8SKent Overstreet } else if (k.k->p.offset >> 9 > isize) 30541c6fdbd8SKent Overstreet break; 30551c6fdbd8SKent Overstreet } 30561c6fdbd8SKent Overstreet 305794f651e2SKent Overstreet ret = bch2_trans_exit(&trans) ?: ret; 30581c6fdbd8SKent Overstreet if (ret) 30591c6fdbd8SKent Overstreet return ret; 30601c6fdbd8SKent Overstreet 30611c6fdbd8SKent Overstreet if (next_data > offset) 3062543ef2ebSKent Overstreet next_data = bch2_seek_pagecache_data(&inode->v, 30631c6fdbd8SKent Overstreet offset, next_data); 30641c6fdbd8SKent Overstreet 3065e10d3094SKent Overstreet if (next_data >= isize) 30661c6fdbd8SKent Overstreet return -ENXIO; 30671c6fdbd8SKent Overstreet 30681c6fdbd8SKent Overstreet return vfs_setpos(file, next_data, MAX_LFS_FILESIZE); 30691c6fdbd8SKent Overstreet } 30701c6fdbd8SKent Overstreet 3071543ef2ebSKent Overstreet static int __page_hole_offset(struct page *page, unsigned offset) 30721c6fdbd8SKent Overstreet { 3073543ef2ebSKent Overstreet struct bch_page_state *s = bch2_page_state(page); 3074543ef2ebSKent Overstreet unsigned i; 3075543ef2ebSKent Overstreet 3076543ef2ebSKent Overstreet if (!s) 3077543ef2ebSKent Overstreet return 0; 3078543ef2ebSKent Overstreet 3079543ef2ebSKent Overstreet for (i = offset >> 9; i < PAGE_SECTORS; i++) 3080543ef2ebSKent Overstreet if (s->s[i].state < SECTOR_DIRTY) 3081543ef2ebSKent Overstreet return i << 9; 3082543ef2ebSKent Overstreet 3083543ef2ebSKent Overstreet return -1; 3084543ef2ebSKent Overstreet } 3085543ef2ebSKent Overstreet 3086543ef2ebSKent Overstreet static loff_t page_hole_offset(struct address_space *mapping, loff_t offset) 3087543ef2ebSKent Overstreet { 3088543ef2ebSKent Overstreet pgoff_t index = offset >> PAGE_SHIFT; 30891c6fdbd8SKent Overstreet struct page *page; 3090543ef2ebSKent Overstreet int pg_offset; 3091543ef2ebSKent Overstreet loff_t ret = -1; 30921c6fdbd8SKent Overstreet 30931c6fdbd8SKent Overstreet page = find_lock_page(mapping, index); 30941c6fdbd8SKent Overstreet if (!page) 3095543ef2ebSKent Overstreet return offset; 30961c6fdbd8SKent Overstreet 3097543ef2ebSKent Overstreet pg_offset = __page_hole_offset(page, offset & (PAGE_SIZE - 1)); 3098543ef2ebSKent Overstreet if (pg_offset >= 0) 3099543ef2ebSKent Overstreet ret = ((loff_t) index << PAGE_SHIFT) + pg_offset; 3100543ef2ebSKent Overstreet 31011c6fdbd8SKent Overstreet unlock_page(page); 31021c6fdbd8SKent Overstreet 31031c6fdbd8SKent Overstreet return ret; 31041c6fdbd8SKent Overstreet } 31051c6fdbd8SKent Overstreet 3106543ef2ebSKent Overstreet static loff_t bch2_seek_pagecache_hole(struct inode *vinode, 31071c6fdbd8SKent Overstreet loff_t start_offset, 31081c6fdbd8SKent Overstreet loff_t end_offset) 31091c6fdbd8SKent Overstreet { 31101c6fdbd8SKent Overstreet struct address_space *mapping = vinode->i_mapping; 3111543ef2ebSKent Overstreet loff_t offset = start_offset, hole; 31121c6fdbd8SKent Overstreet 3113543ef2ebSKent Overstreet while (offset < end_offset) { 3114543ef2ebSKent Overstreet hole = page_hole_offset(mapping, offset); 3115543ef2ebSKent Overstreet if (hole >= 0 && hole <= end_offset) 3116543ef2ebSKent Overstreet return max(start_offset, hole); 3117543ef2ebSKent Overstreet 3118543ef2ebSKent Overstreet offset += PAGE_SIZE; 3119543ef2ebSKent Overstreet offset &= PAGE_MASK; 3120543ef2ebSKent Overstreet } 31211c6fdbd8SKent Overstreet 31221c6fdbd8SKent Overstreet return end_offset; 31231c6fdbd8SKent Overstreet } 31241c6fdbd8SKent Overstreet 31251c6fdbd8SKent Overstreet static loff_t bch2_seek_hole(struct file *file, u64 offset) 31261c6fdbd8SKent Overstreet { 31271c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 31281c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 3129424eb881SKent Overstreet struct btree_trans trans; 3130424eb881SKent Overstreet struct btree_iter *iter; 31311c6fdbd8SKent Overstreet struct bkey_s_c k; 31321c6fdbd8SKent Overstreet u64 isize, next_hole = MAX_LFS_FILESIZE; 31331c6fdbd8SKent Overstreet int ret; 31341c6fdbd8SKent Overstreet 31351c6fdbd8SKent Overstreet isize = i_size_read(&inode->v); 31361c6fdbd8SKent Overstreet if (offset >= isize) 31371c6fdbd8SKent Overstreet return -ENXIO; 31381c6fdbd8SKent Overstreet 313920bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 3140424eb881SKent Overstreet 3141424eb881SKent Overstreet for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS, 31421c6fdbd8SKent Overstreet POS(inode->v.i_ino, offset >> 9), 314394f651e2SKent Overstreet BTREE_ITER_SLOTS, k, ret) { 31441c6fdbd8SKent Overstreet if (k.k->p.inode != inode->v.i_ino) { 3145543ef2ebSKent Overstreet next_hole = bch2_seek_pagecache_hole(&inode->v, 31461c6fdbd8SKent Overstreet offset, MAX_LFS_FILESIZE); 31471c6fdbd8SKent Overstreet break; 31481c6fdbd8SKent Overstreet } else if (!bkey_extent_is_data(k.k)) { 3149543ef2ebSKent Overstreet next_hole = bch2_seek_pagecache_hole(&inode->v, 31501c6fdbd8SKent Overstreet max(offset, bkey_start_offset(k.k) << 9), 31511c6fdbd8SKent Overstreet k.k->p.offset << 9); 31521c6fdbd8SKent Overstreet 31531c6fdbd8SKent Overstreet if (next_hole < k.k->p.offset << 9) 31541c6fdbd8SKent Overstreet break; 31551c6fdbd8SKent Overstreet } else { 31561c6fdbd8SKent Overstreet offset = max(offset, bkey_start_offset(k.k) << 9); 31571c6fdbd8SKent Overstreet } 31581c6fdbd8SKent Overstreet } 31591c6fdbd8SKent Overstreet 316094f651e2SKent Overstreet ret = bch2_trans_exit(&trans) ?: ret; 31611c6fdbd8SKent Overstreet if (ret) 31621c6fdbd8SKent Overstreet return ret; 31631c6fdbd8SKent Overstreet 31641c6fdbd8SKent Overstreet if (next_hole > isize) 31651c6fdbd8SKent Overstreet next_hole = isize; 31661c6fdbd8SKent Overstreet 31671c6fdbd8SKent Overstreet return vfs_setpos(file, next_hole, MAX_LFS_FILESIZE); 31681c6fdbd8SKent Overstreet } 31691c6fdbd8SKent Overstreet 31701c6fdbd8SKent Overstreet loff_t bch2_llseek(struct file *file, loff_t offset, int whence) 31711c6fdbd8SKent Overstreet { 31721c6fdbd8SKent Overstreet switch (whence) { 31731c6fdbd8SKent Overstreet case SEEK_SET: 31741c6fdbd8SKent Overstreet case SEEK_CUR: 31751c6fdbd8SKent Overstreet case SEEK_END: 31761c6fdbd8SKent Overstreet return generic_file_llseek(file, offset, whence); 31771c6fdbd8SKent Overstreet case SEEK_DATA: 31781c6fdbd8SKent Overstreet return bch2_seek_data(file, offset); 31791c6fdbd8SKent Overstreet case SEEK_HOLE: 31801c6fdbd8SKent Overstreet return bch2_seek_hole(file, offset); 31811c6fdbd8SKent Overstreet } 31821c6fdbd8SKent Overstreet 31831c6fdbd8SKent Overstreet return -EINVAL; 31841c6fdbd8SKent Overstreet } 31851c6fdbd8SKent Overstreet 31861c6fdbd8SKent Overstreet void bch2_fs_fsio_exit(struct bch_fs *c) 31871c6fdbd8SKent Overstreet { 31881c6fdbd8SKent Overstreet bioset_exit(&c->dio_write_bioset); 31891c6fdbd8SKent Overstreet bioset_exit(&c->dio_read_bioset); 31901c6fdbd8SKent Overstreet bioset_exit(&c->writepage_bioset); 31911c6fdbd8SKent Overstreet } 31921c6fdbd8SKent Overstreet 31931c6fdbd8SKent Overstreet int bch2_fs_fsio_init(struct bch_fs *c) 31941c6fdbd8SKent Overstreet { 31951c6fdbd8SKent Overstreet int ret = 0; 31961c6fdbd8SKent Overstreet 31971c6fdbd8SKent Overstreet pr_verbose_init(c->opts, ""); 31981c6fdbd8SKent Overstreet 31991c6fdbd8SKent Overstreet if (bioset_init(&c->writepage_bioset, 32001c6fdbd8SKent Overstreet 4, offsetof(struct bch_writepage_io, op.op.wbio.bio), 32011c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 32021c6fdbd8SKent Overstreet bioset_init(&c->dio_read_bioset, 32031c6fdbd8SKent Overstreet 4, offsetof(struct dio_read, rbio.bio), 32041c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 32051c6fdbd8SKent Overstreet bioset_init(&c->dio_write_bioset, 32061c6fdbd8SKent Overstreet 4, offsetof(struct dio_write, iop.op.wbio.bio), 32071c6fdbd8SKent Overstreet BIOSET_NEED_BVECS)) 32081c6fdbd8SKent Overstreet ret = -ENOMEM; 32091c6fdbd8SKent Overstreet 32101c6fdbd8SKent Overstreet pr_verbose_init(c->opts, "ret %i", ret); 32111c6fdbd8SKent Overstreet return ret; 32121c6fdbd8SKent Overstreet } 32131c6fdbd8SKent Overstreet 32141c6fdbd8SKent Overstreet #endif /* NO_BCACHEFS_FS */ 3215