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" 607a1006aSKent Overstreet #include "bkey_buf.h" 71c6fdbd8SKent Overstreet #include "btree_update.h" 81c6fdbd8SKent Overstreet #include "buckets.h" 91c6fdbd8SKent Overstreet #include "clock.h" 101c6fdbd8SKent Overstreet #include "error.h" 11e2d9912cSKent Overstreet #include "extents.h" 1208c07feaSKent Overstreet #include "extent_update.h" 131c6fdbd8SKent Overstreet #include "fs.h" 141c6fdbd8SKent Overstreet #include "fs-io.h" 151c6fdbd8SKent Overstreet #include "fsck.h" 161c6fdbd8SKent Overstreet #include "inode.h" 171c6fdbd8SKent Overstreet #include "journal.h" 181c6fdbd8SKent Overstreet #include "io.h" 191c6fdbd8SKent Overstreet #include "keylist.h" 201c6fdbd8SKent Overstreet #include "quota.h" 2176426098SKent Overstreet #include "reflink.h" 221c6fdbd8SKent Overstreet #include "trace.h" 231c6fdbd8SKent Overstreet 241c6fdbd8SKent Overstreet #include <linux/aio.h> 251c6fdbd8SKent Overstreet #include <linux/backing-dev.h> 261c6fdbd8SKent Overstreet #include <linux/falloc.h> 271c6fdbd8SKent Overstreet #include <linux/migrate.h> 281c6fdbd8SKent Overstreet #include <linux/mmu_context.h> 291c6fdbd8SKent Overstreet #include <linux/pagevec.h> 309ba2eb25SKent Overstreet #include <linux/rmap.h> 311c6fdbd8SKent Overstreet #include <linux/sched/signal.h> 321c6fdbd8SKent Overstreet #include <linux/task_io_accounting_ops.h> 331c6fdbd8SKent Overstreet #include <linux/uio.h> 341c6fdbd8SKent Overstreet #include <linux/writeback.h> 351c6fdbd8SKent Overstreet 361c6fdbd8SKent Overstreet #include <trace/events/writeback.h> 371c6fdbd8SKent Overstreet 387f5e31e1SKent Overstreet static inline bool bio_full(struct bio *bio, unsigned len) 397f5e31e1SKent Overstreet { 407f5e31e1SKent Overstreet if (bio->bi_vcnt >= bio->bi_max_vecs) 417f5e31e1SKent Overstreet return true; 427f5e31e1SKent Overstreet if (bio->bi_iter.bi_size > UINT_MAX - len) 437f5e31e1SKent Overstreet return true; 447f5e31e1SKent Overstreet return false; 457f5e31e1SKent Overstreet } 467f5e31e1SKent Overstreet 47eb8e6e9cSKent Overstreet static inline struct address_space *faults_disabled_mapping(void) 48eb8e6e9cSKent Overstreet { 49eb8e6e9cSKent Overstreet return (void *) (((unsigned long) current->faults_disabled_mapping) & ~1UL); 50eb8e6e9cSKent Overstreet } 51eb8e6e9cSKent Overstreet 52eb8e6e9cSKent Overstreet static inline void set_fdm_dropped_locks(void) 53eb8e6e9cSKent Overstreet { 54eb8e6e9cSKent Overstreet current->faults_disabled_mapping = 55eb8e6e9cSKent Overstreet (void *) (((unsigned long) current->faults_disabled_mapping)|1); 56eb8e6e9cSKent Overstreet } 57eb8e6e9cSKent Overstreet 58eb8e6e9cSKent Overstreet static inline bool fdm_dropped_locks(void) 59eb8e6e9cSKent Overstreet { 60eb8e6e9cSKent Overstreet return ((unsigned long) current->faults_disabled_mapping) & 1; 61eb8e6e9cSKent Overstreet } 62eb8e6e9cSKent Overstreet 631c6fdbd8SKent Overstreet struct quota_res { 641c6fdbd8SKent Overstreet u64 sectors; 651c6fdbd8SKent Overstreet }; 661c6fdbd8SKent Overstreet 679a3df993SKent Overstreet struct bch_writepage_io { 681c6fdbd8SKent Overstreet struct bch_inode_info *inode; 691c6fdbd8SKent Overstreet 701c6fdbd8SKent Overstreet /* must be last: */ 711c6fdbd8SKent Overstreet struct bch_write_op op; 721c6fdbd8SKent Overstreet }; 731c6fdbd8SKent Overstreet 741c6fdbd8SKent Overstreet struct dio_write { 751c6fdbd8SKent Overstreet struct kiocb *req; 76182c7bbfSKent Overstreet struct address_space *mapping; 77182c7bbfSKent Overstreet struct bch_inode_info *inode; 78ed484030SKent Overstreet struct mm_struct *mm; 791c6fdbd8SKent Overstreet unsigned loop:1, 806b1b186aSKent Overstreet extending:1, 811c6fdbd8SKent Overstreet sync:1, 82a1ee777bSKent Overstreet flush:1, 831c6fdbd8SKent Overstreet free_iov:1; 841c6fdbd8SKent Overstreet struct quota_res quota_res; 85042a1f26SKent Overstreet u64 written; 861c6fdbd8SKent Overstreet 871c6fdbd8SKent Overstreet struct iov_iter iter; 881c6fdbd8SKent Overstreet struct iovec inline_vecs[2]; 891c6fdbd8SKent Overstreet 901c6fdbd8SKent Overstreet /* must be last: */ 919a3df993SKent Overstreet struct bch_write_op op; 921c6fdbd8SKent Overstreet }; 931c6fdbd8SKent Overstreet 941c6fdbd8SKent Overstreet struct dio_read { 951c6fdbd8SKent Overstreet struct closure cl; 961c6fdbd8SKent Overstreet struct kiocb *req; 971c6fdbd8SKent Overstreet long ret; 98b4725cc1SKent Overstreet bool should_dirty; 991c6fdbd8SKent Overstreet struct bch_read_bio rbio; 1001c6fdbd8SKent Overstreet }; 1011c6fdbd8SKent Overstreet 1021c6fdbd8SKent Overstreet /* pagecache_block must be held */ 103a023127aSKent Overstreet static noinline int write_invalidate_inode_pages_range(struct address_space *mapping, 1041c6fdbd8SKent Overstreet loff_t start, loff_t end) 1051c6fdbd8SKent Overstreet { 1061c6fdbd8SKent Overstreet int ret; 1071c6fdbd8SKent Overstreet 1081c6fdbd8SKent Overstreet /* 1091c6fdbd8SKent Overstreet * XXX: the way this is currently implemented, we can spin if a process 1101c6fdbd8SKent Overstreet * is continually redirtying a specific page 1111c6fdbd8SKent Overstreet */ 1121c6fdbd8SKent Overstreet do { 1131c6fdbd8SKent Overstreet if (!mapping->nrpages) 1141c6fdbd8SKent Overstreet return 0; 1151c6fdbd8SKent Overstreet 1161c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, start, end); 1171c6fdbd8SKent Overstreet if (ret) 1181c6fdbd8SKent Overstreet break; 1191c6fdbd8SKent Overstreet 1201c6fdbd8SKent Overstreet if (!mapping->nrpages) 1211c6fdbd8SKent Overstreet return 0; 1221c6fdbd8SKent Overstreet 1231c6fdbd8SKent Overstreet ret = invalidate_inode_pages2_range(mapping, 1241c6fdbd8SKent Overstreet start >> PAGE_SHIFT, 1251c6fdbd8SKent Overstreet end >> PAGE_SHIFT); 1261c6fdbd8SKent Overstreet } while (ret == -EBUSY); 1271c6fdbd8SKent Overstreet 1281c6fdbd8SKent Overstreet return ret; 1291c6fdbd8SKent Overstreet } 1301c6fdbd8SKent Overstreet 1311c6fdbd8SKent Overstreet /* quotas */ 1321c6fdbd8SKent Overstreet 1331c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA 1341c6fdbd8SKent Overstreet 1356b1b186aSKent Overstreet static void __bch2_quota_reservation_put(struct bch_fs *c, 1361c6fdbd8SKent Overstreet struct bch_inode_info *inode, 1371c6fdbd8SKent Overstreet struct quota_res *res) 1381c6fdbd8SKent Overstreet { 1391c6fdbd8SKent Overstreet BUG_ON(res->sectors > inode->ei_quota_reserved); 1401c6fdbd8SKent Overstreet 1411c6fdbd8SKent Overstreet bch2_quota_acct(c, inode->ei_qid, Q_SPC, 14226609b61SKent Overstreet -((s64) res->sectors), KEY_TYPE_QUOTA_PREALLOC); 1431c6fdbd8SKent Overstreet inode->ei_quota_reserved -= res->sectors; 1441c6fdbd8SKent Overstreet res->sectors = 0; 1451c6fdbd8SKent Overstreet } 1461c6fdbd8SKent Overstreet 1476b1b186aSKent Overstreet static void bch2_quota_reservation_put(struct bch_fs *c, 1486b1b186aSKent Overstreet struct bch_inode_info *inode, 1496b1b186aSKent Overstreet struct quota_res *res) 1506b1b186aSKent Overstreet { 1516b1b186aSKent Overstreet if (res->sectors) { 1526b1b186aSKent Overstreet mutex_lock(&inode->ei_quota_lock); 1536b1b186aSKent Overstreet __bch2_quota_reservation_put(c, inode, res); 1546b1b186aSKent Overstreet mutex_unlock(&inode->ei_quota_lock); 1556b1b186aSKent Overstreet } 1566b1b186aSKent Overstreet } 1576b1b186aSKent Overstreet 1581c6fdbd8SKent Overstreet static int bch2_quota_reservation_add(struct bch_fs *c, 1591c6fdbd8SKent Overstreet struct bch_inode_info *inode, 1601c6fdbd8SKent Overstreet struct quota_res *res, 161e8540e56SKent Overstreet u64 sectors, 1621c6fdbd8SKent Overstreet bool check_enospc) 1631c6fdbd8SKent Overstreet { 1641c6fdbd8SKent Overstreet int ret; 1651c6fdbd8SKent Overstreet 1661c6fdbd8SKent Overstreet mutex_lock(&inode->ei_quota_lock); 1671c6fdbd8SKent Overstreet ret = bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors, 16826609b61SKent Overstreet check_enospc ? KEY_TYPE_QUOTA_PREALLOC : KEY_TYPE_QUOTA_NOCHECK); 1691c6fdbd8SKent Overstreet if (likely(!ret)) { 1701c6fdbd8SKent Overstreet inode->ei_quota_reserved += sectors; 1711c6fdbd8SKent Overstreet res->sectors += sectors; 1721c6fdbd8SKent Overstreet } 1731c6fdbd8SKent Overstreet mutex_unlock(&inode->ei_quota_lock); 1741c6fdbd8SKent Overstreet 1751c6fdbd8SKent Overstreet return ret; 1761c6fdbd8SKent Overstreet } 1771c6fdbd8SKent Overstreet 1781c6fdbd8SKent Overstreet #else 1791c6fdbd8SKent Overstreet 1806b1b186aSKent Overstreet static void __bch2_quota_reservation_put(struct bch_fs *c, 1816b1b186aSKent Overstreet struct bch_inode_info *inode, 1826b1b186aSKent Overstreet struct quota_res *res) {} 1836b1b186aSKent Overstreet 1841c6fdbd8SKent Overstreet static void bch2_quota_reservation_put(struct bch_fs *c, 1851c6fdbd8SKent Overstreet struct bch_inode_info *inode, 1866b1b186aSKent Overstreet struct quota_res *res) {} 1871c6fdbd8SKent Overstreet 1881c6fdbd8SKent Overstreet static int bch2_quota_reservation_add(struct bch_fs *c, 1891c6fdbd8SKent Overstreet struct bch_inode_info *inode, 1901c6fdbd8SKent Overstreet struct quota_res *res, 1911c6fdbd8SKent Overstreet unsigned sectors, 1921c6fdbd8SKent Overstreet bool check_enospc) 1931c6fdbd8SKent Overstreet { 1941c6fdbd8SKent Overstreet return 0; 1951c6fdbd8SKent Overstreet } 1961c6fdbd8SKent Overstreet 1971c6fdbd8SKent Overstreet #endif 1981c6fdbd8SKent Overstreet 1991c6fdbd8SKent Overstreet /* i_size updates: */ 2001c6fdbd8SKent Overstreet 2012ea90048SKent Overstreet struct inode_new_size { 2022ea90048SKent Overstreet loff_t new_size; 2032ea90048SKent Overstreet u64 now; 2042ea90048SKent Overstreet unsigned fields; 2052ea90048SKent Overstreet }; 2062ea90048SKent Overstreet 2071c6fdbd8SKent Overstreet static int inode_set_size(struct bch_inode_info *inode, 2081c6fdbd8SKent Overstreet struct bch_inode_unpacked *bi, 2091c6fdbd8SKent Overstreet void *p) 2101c6fdbd8SKent Overstreet { 2112ea90048SKent Overstreet struct inode_new_size *s = p; 2121c6fdbd8SKent Overstreet 2132ea90048SKent Overstreet bi->bi_size = s->new_size; 2142ea90048SKent Overstreet if (s->fields & ATTR_ATIME) 2152ea90048SKent Overstreet bi->bi_atime = s->now; 2162ea90048SKent Overstreet if (s->fields & ATTR_MTIME) 2172ea90048SKent Overstreet bi->bi_mtime = s->now; 2182ea90048SKent Overstreet if (s->fields & ATTR_CTIME) 2192ea90048SKent Overstreet bi->bi_ctime = s->now; 2201c6fdbd8SKent Overstreet 2211c6fdbd8SKent Overstreet return 0; 2221c6fdbd8SKent Overstreet } 2231c6fdbd8SKent Overstreet 22476426098SKent Overstreet int __must_check bch2_write_inode_size(struct bch_fs *c, 2251c6fdbd8SKent Overstreet struct bch_inode_info *inode, 2262ea90048SKent Overstreet loff_t new_size, unsigned fields) 2271c6fdbd8SKent Overstreet { 2282ea90048SKent Overstreet struct inode_new_size s = { 2292ea90048SKent Overstreet .new_size = new_size, 2302ea90048SKent Overstreet .now = bch2_current_time(c), 2312ea90048SKent Overstreet .fields = fields, 2322ea90048SKent Overstreet }; 2332ea90048SKent Overstreet 2342ea90048SKent Overstreet return bch2_write_inode(c, inode, inode_set_size, &s, fields); 2351c6fdbd8SKent Overstreet } 2361c6fdbd8SKent Overstreet 2376b1b186aSKent Overstreet static void __i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode, 238190fa7afSKent Overstreet struct quota_res *quota_res, s64 sectors) 2391c6fdbd8SKent Overstreet { 240b33bf1bcSKent Overstreet bch2_fs_inconsistent_on((s64) inode->v.i_blocks + sectors < 0, c, 241b33bf1bcSKent Overstreet "inode %lu i_blocks underflow: %llu + %lli < 0 (ondisk %lli)", 242b33bf1bcSKent Overstreet inode->v.i_ino, (u64) inode->v.i_blocks, sectors, 243b33bf1bcSKent Overstreet inode->ei_inode.bi_sectors); 244b44a66a6SKent Overstreet inode->v.i_blocks += sectors; 245b44a66a6SKent Overstreet 2461c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA 2471c6fdbd8SKent Overstreet if (quota_res && sectors > 0) { 2481c6fdbd8SKent Overstreet BUG_ON(sectors > quota_res->sectors); 2491c6fdbd8SKent Overstreet BUG_ON(sectors > inode->ei_quota_reserved); 2501c6fdbd8SKent Overstreet 2511c6fdbd8SKent Overstreet quota_res->sectors -= sectors; 2521c6fdbd8SKent Overstreet inode->ei_quota_reserved -= sectors; 2531c6fdbd8SKent Overstreet } else { 25426609b61SKent Overstreet bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors, KEY_TYPE_QUOTA_WARN); 2551c6fdbd8SKent Overstreet } 2561c6fdbd8SKent Overstreet #endif 2576b1b186aSKent Overstreet } 2586b1b186aSKent Overstreet 2596b1b186aSKent Overstreet static void i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode, 2606b1b186aSKent Overstreet struct quota_res *quota_res, s64 sectors) 2616b1b186aSKent Overstreet { 2626b1b186aSKent Overstreet if (sectors) { 2636b1b186aSKent Overstreet mutex_lock(&inode->ei_quota_lock); 2646b1b186aSKent Overstreet __i_sectors_acct(c, inode, quota_res, sectors); 2651c6fdbd8SKent Overstreet mutex_unlock(&inode->ei_quota_lock); 2661c6fdbd8SKent Overstreet } 2676b1b186aSKent Overstreet } 2681c6fdbd8SKent Overstreet 2691c6fdbd8SKent Overstreet /* page state: */ 2701c6fdbd8SKent Overstreet 2711c6fdbd8SKent Overstreet /* stored in page->private: */ 2721c6fdbd8SKent Overstreet 273f57a6a5dSKent Overstreet struct bch_page_sector { 274b44a66a6SKent Overstreet /* Uncompressed, fully allocated replicas (or on disk reservation): */ 275b44a66a6SKent Overstreet unsigned nr_replicas:4; 276f81b648dSKent Overstreet 277b44a66a6SKent Overstreet /* Owns PAGE_SECTORS * replicas_reserved sized in memory reservation: */ 278b44a66a6SKent Overstreet unsigned replicas_reserved:4; 2791c6fdbd8SKent Overstreet 280f57a6a5dSKent Overstreet /* i_sectors: */ 281f57a6a5dSKent Overstreet enum { 282f57a6a5dSKent Overstreet SECTOR_UNALLOCATED, 2832ba5d38bSKent Overstreet SECTOR_RESERVED, 284f57a6a5dSKent Overstreet SECTOR_DIRTY, 285b44a66a6SKent Overstreet SECTOR_DIRTY_RESERVED, 286f57a6a5dSKent Overstreet SECTOR_ALLOCATED, 287b44a66a6SKent Overstreet } state:8; 2881c6fdbd8SKent Overstreet }; 2891c6fdbd8SKent Overstreet 290f57a6a5dSKent Overstreet struct bch_page_state { 2913826ee0bSKent Overstreet spinlock_t lock; 2927f5e31e1SKent Overstreet atomic_t write_count; 293e6ec361fSKent Overstreet bool uptodate; 294f57a6a5dSKent Overstreet struct bch_page_sector s[PAGE_SECTORS]; 295f57a6a5dSKent Overstreet }; 296f57a6a5dSKent Overstreet 297f57a6a5dSKent Overstreet static inline struct bch_page_state *__bch2_page_state(struct page *page) 2981c6fdbd8SKent Overstreet { 299f57a6a5dSKent Overstreet return page_has_private(page) 300f57a6a5dSKent Overstreet ? (struct bch_page_state *) page_private(page) 301f57a6a5dSKent Overstreet : NULL; 302f57a6a5dSKent Overstreet } 3031c6fdbd8SKent Overstreet 304f57a6a5dSKent Overstreet static inline struct bch_page_state *bch2_page_state(struct page *page) 305f57a6a5dSKent Overstreet { 306e1036a2aSKent Overstreet EBUG_ON(!PageLocked(page)); 3071c6fdbd8SKent Overstreet 308f57a6a5dSKent Overstreet return __bch2_page_state(page); 309f57a6a5dSKent Overstreet } 310f57a6a5dSKent Overstreet 311f57a6a5dSKent Overstreet /* for newly allocated pages: */ 312f57a6a5dSKent Overstreet static void __bch2_page_state_release(struct page *page) 313f57a6a5dSKent Overstreet { 31400276f9fSMatthew Wilcox (Oracle) kfree(detach_page_private(page)); 315f57a6a5dSKent Overstreet } 316f57a6a5dSKent Overstreet 317f57a6a5dSKent Overstreet static void bch2_page_state_release(struct page *page) 318f57a6a5dSKent Overstreet { 31900276f9fSMatthew Wilcox (Oracle) EBUG_ON(!PageLocked(page)); 32000276f9fSMatthew Wilcox (Oracle) __bch2_page_state_release(page); 321f57a6a5dSKent Overstreet } 322f57a6a5dSKent Overstreet 323f57a6a5dSKent Overstreet /* for newly allocated pages: */ 324f57a6a5dSKent Overstreet static struct bch_page_state *__bch2_page_state_create(struct page *page, 325f57a6a5dSKent Overstreet gfp_t gfp) 326f57a6a5dSKent Overstreet { 327f57a6a5dSKent Overstreet struct bch_page_state *s; 328f57a6a5dSKent Overstreet 329f57a6a5dSKent Overstreet s = kzalloc(sizeof(*s), GFP_NOFS|gfp); 330f57a6a5dSKent Overstreet if (!s) 331f57a6a5dSKent Overstreet return NULL; 332f57a6a5dSKent Overstreet 3333826ee0bSKent Overstreet spin_lock_init(&s->lock); 33400276f9fSMatthew Wilcox (Oracle) attach_page_private(page, s); 3351c6fdbd8SKent Overstreet return s; 3361c6fdbd8SKent Overstreet } 3371c6fdbd8SKent Overstreet 338f57a6a5dSKent Overstreet static struct bch_page_state *bch2_page_state_create(struct page *page, 339f57a6a5dSKent Overstreet gfp_t gfp) 340f57a6a5dSKent Overstreet { 341f57a6a5dSKent Overstreet return bch2_page_state(page) ?: __bch2_page_state_create(page, gfp); 342f57a6a5dSKent Overstreet } 343f57a6a5dSKent Overstreet 344b44a66a6SKent Overstreet static unsigned bkey_to_sector_state(const struct bkey *k) 345b44a66a6SKent Overstreet { 346b44a66a6SKent Overstreet if (k->type == KEY_TYPE_reservation) 347b44a66a6SKent Overstreet return SECTOR_RESERVED; 348b44a66a6SKent Overstreet if (bkey_extent_is_allocation(k)) 349b44a66a6SKent Overstreet return SECTOR_ALLOCATED; 350b44a66a6SKent Overstreet return SECTOR_UNALLOCATED; 351b44a66a6SKent Overstreet } 352b44a66a6SKent Overstreet 353e6ec361fSKent Overstreet static void __bch2_page_state_set(struct page *page, 354e6ec361fSKent Overstreet unsigned pg_offset, unsigned pg_len, 355e6ec361fSKent Overstreet unsigned nr_ptrs, unsigned state) 356e6ec361fSKent Overstreet { 357e6ec361fSKent Overstreet struct bch_page_state *s = bch2_page_state_create(page, __GFP_NOFAIL); 358e6ec361fSKent Overstreet unsigned i; 359e6ec361fSKent Overstreet 360e6ec361fSKent Overstreet BUG_ON(pg_offset >= PAGE_SECTORS); 361e6ec361fSKent Overstreet BUG_ON(pg_offset + pg_len > PAGE_SECTORS); 362e6ec361fSKent Overstreet 363e6ec361fSKent Overstreet spin_lock(&s->lock); 364e6ec361fSKent Overstreet 365e6ec361fSKent Overstreet for (i = pg_offset; i < pg_offset + pg_len; i++) { 366e6ec361fSKent Overstreet s->s[i].nr_replicas = nr_ptrs; 367e6ec361fSKent Overstreet s->s[i].state = state; 368e6ec361fSKent Overstreet } 369e6ec361fSKent Overstreet 370e6ec361fSKent Overstreet if (i == PAGE_SECTORS) 371e6ec361fSKent Overstreet s->uptodate = true; 372e6ec361fSKent Overstreet 373e6ec361fSKent Overstreet spin_unlock(&s->lock); 374e6ec361fSKent Overstreet } 375e6ec361fSKent Overstreet 376e6ec361fSKent Overstreet static int bch2_page_state_set(struct bch_fs *c, subvol_inum inum, 377e6ec361fSKent Overstreet struct page **pages, unsigned nr_pages) 378e6ec361fSKent Overstreet { 379e6ec361fSKent Overstreet struct btree_trans trans; 380e6ec361fSKent Overstreet struct btree_iter iter; 381e6ec361fSKent Overstreet struct bkey_s_c k; 382e6ec361fSKent Overstreet u64 offset = pages[0]->index << PAGE_SECTORS_SHIFT; 383e6ec361fSKent Overstreet unsigned pg_idx = 0; 384e6ec361fSKent Overstreet u32 snapshot; 385e6ec361fSKent Overstreet int ret; 386e6ec361fSKent Overstreet 387e6ec361fSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 388e6ec361fSKent Overstreet retry: 389e6ec361fSKent Overstreet bch2_trans_begin(&trans); 390e6ec361fSKent Overstreet 391e6ec361fSKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot); 392e6ec361fSKent Overstreet if (ret) 393e6ec361fSKent Overstreet goto err; 394e6ec361fSKent Overstreet 395e6ec361fSKent Overstreet for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents, 396e6ec361fSKent Overstreet SPOS(inum.inum, offset, snapshot), 397e6ec361fSKent Overstreet BTREE_ITER_SLOTS, k, ret) { 398e6ec361fSKent Overstreet unsigned nr_ptrs = bch2_bkey_nr_ptrs_fully_allocated(k); 399e6ec361fSKent Overstreet unsigned state = bkey_to_sector_state(k.k); 400e6ec361fSKent Overstreet 401e6ec361fSKent Overstreet while (pg_idx < nr_pages) { 402e6ec361fSKent Overstreet struct page *page = pages[pg_idx]; 403e6ec361fSKent Overstreet u64 pg_start = page->index << PAGE_SECTORS_SHIFT; 404e6ec361fSKent Overstreet u64 pg_end = (page->index + 1) << PAGE_SECTORS_SHIFT; 405e6ec361fSKent Overstreet unsigned pg_offset = max(bkey_start_offset(k.k), pg_start) - pg_start; 406e6ec361fSKent Overstreet unsigned pg_len = min(k.k->p.offset, pg_end) - pg_offset - pg_start; 407e6ec361fSKent Overstreet 408e6ec361fSKent Overstreet BUG_ON(k.k->p.offset < pg_start); 409e6ec361fSKent Overstreet BUG_ON(bkey_start_offset(k.k) > pg_end); 410e6ec361fSKent Overstreet 411e6ec361fSKent Overstreet if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) 412e6ec361fSKent Overstreet __bch2_page_state_set(page, pg_offset, pg_len, nr_ptrs, state); 413e6ec361fSKent Overstreet 414e6ec361fSKent Overstreet if (k.k->p.offset < pg_end) 415e6ec361fSKent Overstreet break; 416e6ec361fSKent Overstreet pg_idx++; 417e6ec361fSKent Overstreet } 418e6ec361fSKent Overstreet 419e6ec361fSKent Overstreet if (pg_idx == nr_pages) 420e6ec361fSKent Overstreet break; 421e6ec361fSKent Overstreet } 422e6ec361fSKent Overstreet 423e6ec361fSKent Overstreet offset = iter.pos.offset; 424e6ec361fSKent Overstreet bch2_trans_iter_exit(&trans, &iter); 425e6ec361fSKent Overstreet err: 426549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 427e6ec361fSKent Overstreet goto retry; 428e6ec361fSKent Overstreet bch2_trans_exit(&trans); 429e6ec361fSKent Overstreet 430e6ec361fSKent Overstreet return ret; 431e6ec361fSKent Overstreet } 432e6ec361fSKent Overstreet 433b44a66a6SKent Overstreet static void bch2_bio_page_state_set(struct bio *bio, struct bkey_s_c k) 434b44a66a6SKent Overstreet { 435b44a66a6SKent Overstreet struct bvec_iter iter; 436b44a66a6SKent Overstreet struct bio_vec bv; 437b44a66a6SKent Overstreet unsigned nr_ptrs = k.k->type == KEY_TYPE_reflink_v 438b44a66a6SKent Overstreet ? 0 : bch2_bkey_nr_ptrs_fully_allocated(k); 439b44a66a6SKent Overstreet unsigned state = bkey_to_sector_state(k.k); 440b44a66a6SKent Overstreet 441e6ec361fSKent Overstreet bio_for_each_segment(bv, bio, iter) 442e6ec361fSKent Overstreet __bch2_page_state_set(bv.bv_page, bv.bv_offset >> 9, 443e6ec361fSKent Overstreet bv.bv_len >> 9, nr_ptrs, state); 444b44a66a6SKent Overstreet } 445b44a66a6SKent Overstreet 446dcfc593fSKent Overstreet static void mark_pagecache_unallocated(struct bch_inode_info *inode, 447dcfc593fSKent Overstreet u64 start, u64 end) 448dcfc593fSKent Overstreet { 449dcfc593fSKent Overstreet pgoff_t index = start >> PAGE_SECTORS_SHIFT; 450dcfc593fSKent Overstreet pgoff_t end_index = (end - 1) >> PAGE_SECTORS_SHIFT; 451dcfc593fSKent Overstreet struct folio_batch fbatch; 452dcfc593fSKent Overstreet unsigned i, j; 453dcfc593fSKent Overstreet 454dcfc593fSKent Overstreet if (end <= start) 455dcfc593fSKent Overstreet return; 456dcfc593fSKent Overstreet 457dcfc593fSKent Overstreet folio_batch_init(&fbatch); 458dcfc593fSKent Overstreet 459dcfc593fSKent Overstreet while (filemap_get_folios(inode->v.i_mapping, 460dcfc593fSKent Overstreet &index, end_index, &fbatch)) { 461dcfc593fSKent Overstreet for (i = 0; i < folio_batch_count(&fbatch); i++) { 462dcfc593fSKent Overstreet struct folio *folio = fbatch.folios[i]; 463dcfc593fSKent Overstreet u64 pg_start = folio->index << PAGE_SECTORS_SHIFT; 464dcfc593fSKent Overstreet u64 pg_end = (folio->index + 1) << PAGE_SECTORS_SHIFT; 465dcfc593fSKent Overstreet unsigned pg_offset = max(start, pg_start) - pg_start; 466dcfc593fSKent Overstreet unsigned pg_len = min(end, pg_end) - pg_offset - pg_start; 467dcfc593fSKent Overstreet struct bch_page_state *s; 468dcfc593fSKent Overstreet 469dcfc593fSKent Overstreet BUG_ON(end <= pg_start); 470dcfc593fSKent Overstreet BUG_ON(pg_offset >= PAGE_SECTORS); 471dcfc593fSKent Overstreet BUG_ON(pg_offset + pg_len > PAGE_SECTORS); 472dcfc593fSKent Overstreet 473dcfc593fSKent Overstreet folio_lock(folio); 474dcfc593fSKent Overstreet s = bch2_page_state(&folio->page); 475dcfc593fSKent Overstreet 476dcfc593fSKent Overstreet if (s) { 477dcfc593fSKent Overstreet spin_lock(&s->lock); 478dcfc593fSKent Overstreet for (j = pg_offset; j < pg_offset + pg_len; j++) 479dcfc593fSKent Overstreet s->s[j].nr_replicas = 0; 480dcfc593fSKent Overstreet spin_unlock(&s->lock); 481dcfc593fSKent Overstreet } 482dcfc593fSKent Overstreet 483dcfc593fSKent Overstreet folio_unlock(folio); 484dcfc593fSKent Overstreet } 485dcfc593fSKent Overstreet folio_batch_release(&fbatch); 486dcfc593fSKent Overstreet cond_resched(); 487dcfc593fSKent Overstreet } 488dcfc593fSKent Overstreet } 489dcfc593fSKent Overstreet 490dcfc593fSKent Overstreet static void mark_pagecache_reserved(struct bch_inode_info *inode, 491dcfc593fSKent Overstreet u64 start, u64 end) 492dcfc593fSKent Overstreet { 493dcfc593fSKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 494dcfc593fSKent Overstreet pgoff_t index = start >> PAGE_SECTORS_SHIFT; 495dcfc593fSKent Overstreet pgoff_t end_index = (end - 1) >> PAGE_SECTORS_SHIFT; 496dcfc593fSKent Overstreet struct folio_batch fbatch; 497dcfc593fSKent Overstreet s64 i_sectors_delta = 0; 498dcfc593fSKent Overstreet unsigned i, j; 499dcfc593fSKent Overstreet 500dcfc593fSKent Overstreet if (end <= start) 501dcfc593fSKent Overstreet return; 502dcfc593fSKent Overstreet 503dcfc593fSKent Overstreet folio_batch_init(&fbatch); 504dcfc593fSKent Overstreet 505dcfc593fSKent Overstreet while (filemap_get_folios(inode->v.i_mapping, 506dcfc593fSKent Overstreet &index, end_index, &fbatch)) { 507dcfc593fSKent Overstreet for (i = 0; i < folio_batch_count(&fbatch); i++) { 508dcfc593fSKent Overstreet struct folio *folio = fbatch.folios[i]; 509dcfc593fSKent Overstreet u64 pg_start = folio->index << PAGE_SECTORS_SHIFT; 510dcfc593fSKent Overstreet u64 pg_end = (folio->index + 1) << PAGE_SECTORS_SHIFT; 511dcfc593fSKent Overstreet unsigned pg_offset = max(start, pg_start) - pg_start; 512dcfc593fSKent Overstreet unsigned pg_len = min(end, pg_end) - pg_offset - pg_start; 513dcfc593fSKent Overstreet struct bch_page_state *s; 514dcfc593fSKent Overstreet 515dcfc593fSKent Overstreet BUG_ON(end <= pg_start); 516dcfc593fSKent Overstreet BUG_ON(pg_offset >= PAGE_SECTORS); 517dcfc593fSKent Overstreet BUG_ON(pg_offset + pg_len > PAGE_SECTORS); 518dcfc593fSKent Overstreet 519dcfc593fSKent Overstreet folio_lock(folio); 520dcfc593fSKent Overstreet s = bch2_page_state(&folio->page); 521dcfc593fSKent Overstreet 522dcfc593fSKent Overstreet if (s) { 523dcfc593fSKent Overstreet spin_lock(&s->lock); 524dcfc593fSKent Overstreet for (j = pg_offset; j < pg_offset + pg_len; j++) 525dcfc593fSKent Overstreet switch (s->s[j].state) { 526dcfc593fSKent Overstreet case SECTOR_UNALLOCATED: 527dcfc593fSKent Overstreet s->s[j].state = SECTOR_RESERVED; 528dcfc593fSKent Overstreet break; 529dcfc593fSKent Overstreet case SECTOR_DIRTY: 530dcfc593fSKent Overstreet s->s[j].state = SECTOR_DIRTY_RESERVED; 531dcfc593fSKent Overstreet i_sectors_delta--; 532dcfc593fSKent Overstreet break; 533dcfc593fSKent Overstreet default: 534dcfc593fSKent Overstreet break; 535dcfc593fSKent Overstreet } 536dcfc593fSKent Overstreet spin_unlock(&s->lock); 537dcfc593fSKent Overstreet } 538dcfc593fSKent Overstreet 539dcfc593fSKent Overstreet folio_unlock(folio); 540dcfc593fSKent Overstreet } 541dcfc593fSKent Overstreet folio_batch_release(&fbatch); 542dcfc593fSKent Overstreet cond_resched(); 543dcfc593fSKent Overstreet } 544dcfc593fSKent Overstreet 545dcfc593fSKent Overstreet i_sectors_acct(c, inode, NULL, i_sectors_delta); 546dcfc593fSKent Overstreet } 547dcfc593fSKent Overstreet 548e1036a2aSKent Overstreet static inline unsigned inode_nr_replicas(struct bch_fs *c, struct bch_inode_info *inode) 549e1036a2aSKent Overstreet { 550e1036a2aSKent Overstreet /* XXX: this should not be open coded */ 551e1036a2aSKent Overstreet return inode->ei_inode.bi_data_replicas 552e1036a2aSKent Overstreet ? inode->ei_inode.bi_data_replicas - 1 553e1036a2aSKent Overstreet : c->opts.data_replicas; 554e1036a2aSKent Overstreet } 555e1036a2aSKent Overstreet 556f57a6a5dSKent Overstreet static inline unsigned sectors_to_reserve(struct bch_page_sector *s, 557f57a6a5dSKent Overstreet unsigned nr_replicas) 558f57a6a5dSKent Overstreet { 559f57a6a5dSKent Overstreet return max(0, (int) nr_replicas - 560f57a6a5dSKent Overstreet s->nr_replicas - 561f57a6a5dSKent Overstreet s->replicas_reserved); 562f57a6a5dSKent Overstreet } 563f57a6a5dSKent Overstreet 564f57a6a5dSKent Overstreet static int bch2_get_page_disk_reservation(struct bch_fs *c, 565f57a6a5dSKent Overstreet struct bch_inode_info *inode, 5661c6fdbd8SKent Overstreet struct page *page, bool check_enospc) 5671c6fdbd8SKent Overstreet { 568f57a6a5dSKent Overstreet struct bch_page_state *s = bch2_page_state_create(page, 0); 569e1036a2aSKent Overstreet unsigned nr_replicas = inode_nr_replicas(c, inode); 570f57a6a5dSKent Overstreet struct disk_reservation disk_res = { 0 }; 571f57a6a5dSKent Overstreet unsigned i, disk_res_sectors = 0; 572f81b648dSKent Overstreet int ret; 5731c6fdbd8SKent Overstreet 574f57a6a5dSKent Overstreet if (!s) 575f57a6a5dSKent Overstreet return -ENOMEM; 5761c6fdbd8SKent Overstreet 577f57a6a5dSKent Overstreet for (i = 0; i < ARRAY_SIZE(s->s); i++) 578f57a6a5dSKent Overstreet disk_res_sectors += sectors_to_reserve(&s->s[i], nr_replicas); 579f57a6a5dSKent Overstreet 580f57a6a5dSKent Overstreet if (!disk_res_sectors) 581f57a6a5dSKent Overstreet return 0; 582f57a6a5dSKent Overstreet 583f57a6a5dSKent Overstreet ret = bch2_disk_reservation_get(c, &disk_res, 584f57a6a5dSKent Overstreet disk_res_sectors, 1, 585f57a6a5dSKent Overstreet !check_enospc 586f57a6a5dSKent Overstreet ? BCH_DISK_RESERVATION_NOFAIL 587f57a6a5dSKent Overstreet : 0); 5881c6fdbd8SKent Overstreet if (unlikely(ret)) 589f81b648dSKent Overstreet return ret; 590f81b648dSKent Overstreet 591f57a6a5dSKent Overstreet for (i = 0; i < ARRAY_SIZE(s->s); i++) 592f57a6a5dSKent Overstreet s->s[i].replicas_reserved += 593f57a6a5dSKent Overstreet sectors_to_reserve(&s->s[i], nr_replicas); 594f57a6a5dSKent Overstreet 595f57a6a5dSKent Overstreet return 0; 5961c6fdbd8SKent Overstreet } 5971c6fdbd8SKent Overstreet 598d1542e03SKent Overstreet struct bch2_page_reservation { 599d1542e03SKent Overstreet struct disk_reservation disk; 600d1542e03SKent Overstreet struct quota_res quota; 601d1542e03SKent Overstreet }; 602d1542e03SKent Overstreet 603d1542e03SKent Overstreet static void bch2_page_reservation_init(struct bch_fs *c, 604f57a6a5dSKent Overstreet struct bch_inode_info *inode, 605d1542e03SKent Overstreet struct bch2_page_reservation *res) 606d1542e03SKent Overstreet { 607d1542e03SKent Overstreet memset(res, 0, sizeof(*res)); 608d1542e03SKent Overstreet 609d1542e03SKent Overstreet res->disk.nr_replicas = inode_nr_replicas(c, inode); 610d1542e03SKent Overstreet } 611d1542e03SKent Overstreet 612d1542e03SKent Overstreet static void bch2_page_reservation_put(struct bch_fs *c, 613d1542e03SKent Overstreet struct bch_inode_info *inode, 614d1542e03SKent Overstreet struct bch2_page_reservation *res) 615d1542e03SKent Overstreet { 616d1542e03SKent Overstreet bch2_disk_reservation_put(c, &res->disk); 617d1542e03SKent Overstreet bch2_quota_reservation_put(c, inode, &res->quota); 618d1542e03SKent Overstreet } 619d1542e03SKent Overstreet 620d1542e03SKent Overstreet static int bch2_page_reservation_get(struct bch_fs *c, 621d1542e03SKent Overstreet struct bch_inode_info *inode, struct page *page, 622d1542e03SKent Overstreet struct bch2_page_reservation *res, 623bd954215SKent Overstreet unsigned offset, unsigned len) 624f57a6a5dSKent Overstreet { 625f57a6a5dSKent Overstreet struct bch_page_state *s = bch2_page_state_create(page, 0); 626d1542e03SKent Overstreet unsigned i, disk_sectors = 0, quota_sectors = 0; 627f57a6a5dSKent Overstreet int ret; 628f57a6a5dSKent Overstreet 629f57a6a5dSKent Overstreet if (!s) 630f57a6a5dSKent Overstreet return -ENOMEM; 631f57a6a5dSKent Overstreet 632e6ec361fSKent Overstreet BUG_ON(!s->uptodate); 633e6ec361fSKent Overstreet 6344b0a66d5SKent Overstreet for (i = round_down(offset, block_bytes(c)) >> 9; 6354b0a66d5SKent Overstreet i < round_up(offset + len, block_bytes(c)) >> 9; 636d1542e03SKent Overstreet i++) { 637d1542e03SKent Overstreet disk_sectors += sectors_to_reserve(&s->s[i], 638d1542e03SKent Overstreet res->disk.nr_replicas); 639d1542e03SKent Overstreet quota_sectors += s->s[i].state == SECTOR_UNALLOCATED; 6401c6fdbd8SKent Overstreet } 6411c6fdbd8SKent Overstreet 642d1542e03SKent Overstreet if (disk_sectors) { 643bd954215SKent Overstreet ret = bch2_disk_reservation_add(c, &res->disk, disk_sectors, 0); 644d1542e03SKent Overstreet if (unlikely(ret)) 645d1542e03SKent Overstreet return ret; 646d1542e03SKent Overstreet } 647d1542e03SKent Overstreet 648d1542e03SKent Overstreet if (quota_sectors) { 649d1542e03SKent Overstreet ret = bch2_quota_reservation_add(c, inode, &res->quota, 650bd954215SKent Overstreet quota_sectors, true); 651d1542e03SKent Overstreet if (unlikely(ret)) { 652d1542e03SKent Overstreet struct disk_reservation tmp = { 653d1542e03SKent Overstreet .sectors = disk_sectors 654d1542e03SKent Overstreet }; 655d1542e03SKent Overstreet 656d1542e03SKent Overstreet bch2_disk_reservation_put(c, &tmp); 657d1542e03SKent Overstreet res->disk.sectors -= disk_sectors; 658d1542e03SKent Overstreet return ret; 659d1542e03SKent Overstreet } 660d1542e03SKent Overstreet } 661d1542e03SKent Overstreet 662d1542e03SKent Overstreet return 0; 663f57a6a5dSKent Overstreet } 664f57a6a5dSKent Overstreet 6651c6fdbd8SKent Overstreet static void bch2_clear_page_bits(struct page *page) 6661c6fdbd8SKent Overstreet { 6671c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(page->mapping->host); 6681c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 669f57a6a5dSKent Overstreet struct bch_page_state *s = bch2_page_state(page); 670d1542e03SKent Overstreet struct disk_reservation disk_res = { 0 }; 671f57a6a5dSKent Overstreet int i, dirty_sectors = 0; 6721c6fdbd8SKent Overstreet 673f57a6a5dSKent Overstreet if (!s) 6741c6fdbd8SKent Overstreet return; 6751c6fdbd8SKent Overstreet 6763826ee0bSKent Overstreet EBUG_ON(!PageLocked(page)); 6773826ee0bSKent Overstreet EBUG_ON(PageWriteback(page)); 6783826ee0bSKent Overstreet 679f57a6a5dSKent Overstreet for (i = 0; i < ARRAY_SIZE(s->s); i++) { 680d1542e03SKent Overstreet disk_res.sectors += s->s[i].replicas_reserved; 681d1542e03SKent Overstreet s->s[i].replicas_reserved = 0; 682d1542e03SKent Overstreet 683b44a66a6SKent Overstreet switch (s->s[i].state) { 684b44a66a6SKent Overstreet case SECTOR_DIRTY: 685f57a6a5dSKent Overstreet s->s[i].state = SECTOR_UNALLOCATED; 686b44a66a6SKent Overstreet --dirty_sectors; 687b44a66a6SKent Overstreet break; 688b44a66a6SKent Overstreet case SECTOR_DIRTY_RESERVED: 689b44a66a6SKent Overstreet s->s[i].state = SECTOR_RESERVED; 690b44a66a6SKent Overstreet break; 691b44a66a6SKent Overstreet default: 692b44a66a6SKent Overstreet break; 693f57a6a5dSKent Overstreet } 694f57a6a5dSKent Overstreet } 695adfcfaf0SKent Overstreet 696d1542e03SKent Overstreet bch2_disk_reservation_put(c, &disk_res); 697d1542e03SKent Overstreet 698b44a66a6SKent Overstreet i_sectors_acct(c, inode, NULL, dirty_sectors); 699adfcfaf0SKent Overstreet 700f57a6a5dSKent Overstreet bch2_page_state_release(page); 7011c6fdbd8SKent Overstreet } 7021c6fdbd8SKent Overstreet 703d1542e03SKent Overstreet static void bch2_set_page_dirty(struct bch_fs *c, 704d1542e03SKent Overstreet struct bch_inode_info *inode, struct page *page, 705d1542e03SKent Overstreet struct bch2_page_reservation *res, 706d1542e03SKent Overstreet unsigned offset, unsigned len) 7071c6fdbd8SKent Overstreet { 708d1542e03SKent Overstreet struct bch_page_state *s = bch2_page_state(page); 709f57a6a5dSKent Overstreet unsigned i, dirty_sectors = 0; 7101c6fdbd8SKent Overstreet 711877dfb34SKent Overstreet WARN_ON((u64) page_offset(page) + offset + len > 712877dfb34SKent Overstreet round_up((u64) i_size_read(&inode->v), block_bytes(c))); 713fb472ac5SKent Overstreet 7143826ee0bSKent Overstreet spin_lock(&s->lock); 7153826ee0bSKent Overstreet 7164b0a66d5SKent Overstreet for (i = round_down(offset, block_bytes(c)) >> 9; 7174b0a66d5SKent Overstreet i < round_up(offset + len, block_bytes(c)) >> 9; 718d1542e03SKent Overstreet i++) { 719d1542e03SKent Overstreet unsigned sectors = sectors_to_reserve(&s->s[i], 720d1542e03SKent Overstreet res->disk.nr_replicas); 7211c6fdbd8SKent Overstreet 722406d6d5aSKent Overstreet /* 723406d6d5aSKent Overstreet * This can happen if we race with the error path in 724406d6d5aSKent Overstreet * bch2_writepage_io_done(): 725406d6d5aSKent Overstreet */ 726406d6d5aSKent Overstreet sectors = min_t(unsigned, sectors, res->disk.sectors); 727406d6d5aSKent Overstreet 728d1542e03SKent Overstreet s->s[i].replicas_reserved += sectors; 729d1542e03SKent Overstreet res->disk.sectors -= sectors; 730adfcfaf0SKent Overstreet 731b44a66a6SKent Overstreet switch (s->s[i].state) { 732b44a66a6SKent Overstreet case SECTOR_UNALLOCATED: 733b44a66a6SKent Overstreet s->s[i].state = SECTOR_DIRTY; 734f57a6a5dSKent Overstreet dirty_sectors++; 735b44a66a6SKent Overstreet break; 736b44a66a6SKent Overstreet case SECTOR_RESERVED: 737b44a66a6SKent Overstreet s->s[i].state = SECTOR_DIRTY_RESERVED; 738b44a66a6SKent Overstreet break; 739b44a66a6SKent Overstreet default: 740b44a66a6SKent Overstreet break; 741b44a66a6SKent Overstreet } 742f57a6a5dSKent Overstreet } 743f57a6a5dSKent Overstreet 7443826ee0bSKent Overstreet spin_unlock(&s->lock); 7453826ee0bSKent Overstreet 746d1542e03SKent Overstreet i_sectors_acct(c, inode, &res->quota, dirty_sectors); 7471c6fdbd8SKent Overstreet 748d1542e03SKent Overstreet if (!PageDirty(page)) 749d1542e03SKent Overstreet filemap_dirty_folio(inode->v.i_mapping, page_folio(page)); 7501c6fdbd8SKent Overstreet } 7511c6fdbd8SKent Overstreet 7521c6fdbd8SKent Overstreet vm_fault_t bch2_page_fault(struct vm_fault *vmf) 7531c6fdbd8SKent Overstreet { 7541c6fdbd8SKent Overstreet struct file *file = vmf->vma->vm_file; 755eb8e6e9cSKent Overstreet struct address_space *mapping = file->f_mapping; 756eb8e6e9cSKent Overstreet struct address_space *fdm = faults_disabled_mapping(); 7571c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 7581c6fdbd8SKent Overstreet int ret; 7591c6fdbd8SKent Overstreet 760eb8e6e9cSKent Overstreet if (fdm == mapping) 761eb8e6e9cSKent Overstreet return VM_FAULT_SIGBUS; 762eb8e6e9cSKent Overstreet 763eb8e6e9cSKent Overstreet /* Lock ordering: */ 764eb8e6e9cSKent Overstreet if (fdm > mapping) { 765eb8e6e9cSKent Overstreet struct bch_inode_info *fdm_host = to_bch_ei(fdm->host); 766eb8e6e9cSKent Overstreet 767a7ecd30cSKent Overstreet if (bch2_pagecache_add_tryget(inode)) 768eb8e6e9cSKent Overstreet goto got_lock; 769eb8e6e9cSKent Overstreet 770a7ecd30cSKent Overstreet bch2_pagecache_block_put(fdm_host); 771eb8e6e9cSKent Overstreet 772a7ecd30cSKent Overstreet bch2_pagecache_add_get(inode); 773a7ecd30cSKent Overstreet bch2_pagecache_add_put(inode); 774eb8e6e9cSKent Overstreet 775a7ecd30cSKent Overstreet bch2_pagecache_block_get(fdm_host); 776eb8e6e9cSKent Overstreet 777eb8e6e9cSKent Overstreet /* Signal that lock has been dropped: */ 778eb8e6e9cSKent Overstreet set_fdm_dropped_locks(); 779eb8e6e9cSKent Overstreet return VM_FAULT_SIGBUS; 780eb8e6e9cSKent Overstreet } 781eb8e6e9cSKent Overstreet 782a7ecd30cSKent Overstreet bch2_pagecache_add_get(inode); 783eb8e6e9cSKent Overstreet got_lock: 7841c6fdbd8SKent Overstreet ret = filemap_fault(vmf); 785a7ecd30cSKent Overstreet bch2_pagecache_add_put(inode); 7861c6fdbd8SKent Overstreet 7871c6fdbd8SKent Overstreet return ret; 7881c6fdbd8SKent Overstreet } 7891c6fdbd8SKent Overstreet 7901c6fdbd8SKent Overstreet vm_fault_t bch2_page_mkwrite(struct vm_fault *vmf) 7911c6fdbd8SKent Overstreet { 7921c6fdbd8SKent Overstreet struct page *page = vmf->page; 7931c6fdbd8SKent Overstreet struct file *file = vmf->vma->vm_file; 7941c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 7951c6fdbd8SKent Overstreet struct address_space *mapping = file->f_mapping; 7961c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 797d1542e03SKent Overstreet struct bch2_page_reservation res; 7986cc3535dSKent Overstreet unsigned len; 7996cc3535dSKent Overstreet loff_t isize; 800e6ec361fSKent Overstreet int ret; 8011c6fdbd8SKent Overstreet 802d1542e03SKent Overstreet bch2_page_reservation_init(c, inode, &res); 803d1542e03SKent Overstreet 8041c6fdbd8SKent Overstreet sb_start_pagefault(inode->v.i_sb); 8051c6fdbd8SKent Overstreet file_update_time(file); 8061c6fdbd8SKent Overstreet 8071c6fdbd8SKent Overstreet /* 8081c6fdbd8SKent Overstreet * Not strictly necessary, but helps avoid dio writes livelocking in 8091c6fdbd8SKent Overstreet * write_invalidate_inode_pages_range() - can drop this if/when we get 8101c6fdbd8SKent Overstreet * a write_invalidate_inode_pages_range() that works without dropping 8111c6fdbd8SKent Overstreet * page lock before invalidating page 8121c6fdbd8SKent Overstreet */ 813a7ecd30cSKent Overstreet bch2_pagecache_add_get(inode); 8141c6fdbd8SKent Overstreet 8151c6fdbd8SKent Overstreet lock_page(page); 8166cc3535dSKent Overstreet isize = i_size_read(&inode->v); 8176cc3535dSKent Overstreet 8186cc3535dSKent Overstreet if (page->mapping != mapping || page_offset(page) >= isize) { 8191c6fdbd8SKent Overstreet unlock_page(page); 8201c6fdbd8SKent Overstreet ret = VM_FAULT_NOPAGE; 8211c6fdbd8SKent Overstreet goto out; 8221c6fdbd8SKent Overstreet } 8231c6fdbd8SKent Overstreet 824d55460bbSKent Overstreet len = min_t(loff_t, PAGE_SIZE, isize - page_offset(page)); 8256cc3535dSKent Overstreet 826e6ec361fSKent Overstreet if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) { 827e6ec361fSKent Overstreet if (bch2_page_state_set(c, inode_inum(inode), &page, 1)) { 828e6ec361fSKent Overstreet unlock_page(page); 829e6ec361fSKent Overstreet ret = VM_FAULT_SIGBUS; 830e6ec361fSKent Overstreet goto out; 831e6ec361fSKent Overstreet } 832e6ec361fSKent Overstreet } 833e6ec361fSKent Overstreet 834bd954215SKent Overstreet if (bch2_page_reservation_get(c, inode, page, &res, 0, len)) { 8351c6fdbd8SKent Overstreet unlock_page(page); 8361c6fdbd8SKent Overstreet ret = VM_FAULT_SIGBUS; 8371c6fdbd8SKent Overstreet goto out; 8381c6fdbd8SKent Overstreet } 8391c6fdbd8SKent Overstreet 8406cc3535dSKent Overstreet bch2_set_page_dirty(c, inode, page, &res, 0, len); 8411b783a69SKent Overstreet bch2_page_reservation_put(c, inode, &res); 8421b783a69SKent Overstreet 8431c6fdbd8SKent Overstreet wait_for_stable_page(page); 844e6ec361fSKent Overstreet ret = VM_FAULT_LOCKED; 8451c6fdbd8SKent Overstreet out: 846a7ecd30cSKent Overstreet bch2_pagecache_add_put(inode); 8471c6fdbd8SKent Overstreet sb_end_pagefault(inode->v.i_sb); 848d1542e03SKent Overstreet 8491c6fdbd8SKent Overstreet return ret; 8501c6fdbd8SKent Overstreet } 8511c6fdbd8SKent Overstreet 8521c6fdbd8SKent Overstreet void bch2_invalidate_folio(struct folio *folio, size_t offset, size_t length) 8531c6fdbd8SKent Overstreet { 8541c6fdbd8SKent Overstreet if (offset || length < folio_size(folio)) 8551c6fdbd8SKent Overstreet return; 8561c6fdbd8SKent Overstreet 8571c6fdbd8SKent Overstreet bch2_clear_page_bits(&folio->page); 8581c6fdbd8SKent Overstreet } 8591c6fdbd8SKent Overstreet 8601c6fdbd8SKent Overstreet bool bch2_release_folio(struct folio *folio, gfp_t gfp_mask) 8611c6fdbd8SKent Overstreet { 862a6d90385SKent Overstreet if (folio_test_dirty(folio) || folio_test_writeback(folio)) 8631c6fdbd8SKent Overstreet return false; 8641c6fdbd8SKent Overstreet 8651c6fdbd8SKent Overstreet bch2_clear_page_bits(&folio->page); 8661c6fdbd8SKent Overstreet return true; 8671c6fdbd8SKent Overstreet } 8681c6fdbd8SKent Overstreet 8691c6fdbd8SKent Overstreet /* readpage(s): */ 8701c6fdbd8SKent Overstreet 8711c6fdbd8SKent Overstreet static void bch2_readpages_end_io(struct bio *bio) 8721c6fdbd8SKent Overstreet { 8731c6fdbd8SKent Overstreet struct bvec_iter_all iter; 8741c6fdbd8SKent Overstreet struct bio_vec *bv; 8751c6fdbd8SKent Overstreet 8761c6fdbd8SKent Overstreet bio_for_each_segment_all(bv, bio, iter) { 8771c6fdbd8SKent Overstreet struct page *page = bv->bv_page; 8781c6fdbd8SKent Overstreet 8791c6fdbd8SKent Overstreet if (!bio->bi_status) { 8801c6fdbd8SKent Overstreet SetPageUptodate(page); 8811c6fdbd8SKent Overstreet } else { 8821c6fdbd8SKent Overstreet ClearPageUptodate(page); 8831c6fdbd8SKent Overstreet SetPageError(page); 8841c6fdbd8SKent Overstreet } 8851c6fdbd8SKent Overstreet unlock_page(page); 8861c6fdbd8SKent Overstreet } 8871c6fdbd8SKent Overstreet 8881c6fdbd8SKent Overstreet bio_put(bio); 8891c6fdbd8SKent Overstreet } 8901c6fdbd8SKent Overstreet 8911c6fdbd8SKent Overstreet struct readpages_iter { 8921c6fdbd8SKent Overstreet struct address_space *mapping; 8931c6fdbd8SKent Overstreet struct page **pages; 8941c6fdbd8SKent Overstreet unsigned nr_pages; 8951c6fdbd8SKent Overstreet unsigned idx; 8961c6fdbd8SKent Overstreet pgoff_t offset; 8971c6fdbd8SKent Overstreet }; 8981c6fdbd8SKent Overstreet 8991c6fdbd8SKent Overstreet static int readpages_iter_init(struct readpages_iter *iter, 9001c6fdbd8SKent Overstreet struct readahead_control *ractl) 9011c6fdbd8SKent Overstreet { 9021c6fdbd8SKent Overstreet unsigned i, nr_pages = readahead_count(ractl); 9031c6fdbd8SKent Overstreet 9041c6fdbd8SKent Overstreet memset(iter, 0, sizeof(*iter)); 9051c6fdbd8SKent Overstreet 9061c6fdbd8SKent Overstreet iter->mapping = ractl->mapping; 9071c6fdbd8SKent Overstreet iter->offset = readahead_index(ractl); 9081c6fdbd8SKent Overstreet iter->nr_pages = nr_pages; 9091c6fdbd8SKent Overstreet 9101c6fdbd8SKent Overstreet iter->pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOFS); 9111c6fdbd8SKent Overstreet if (!iter->pages) 9121c6fdbd8SKent Overstreet return -ENOMEM; 9131c6fdbd8SKent Overstreet 91489931472SKent Overstreet nr_pages = __readahead_batch(ractl, iter->pages, nr_pages); 9151c6fdbd8SKent Overstreet for (i = 0; i < nr_pages; i++) { 916f57a6a5dSKent Overstreet __bch2_page_state_create(iter->pages[i], __GFP_NOFAIL); 9171c6fdbd8SKent Overstreet put_page(iter->pages[i]); 9181c6fdbd8SKent Overstreet } 9191c6fdbd8SKent Overstreet 9201c6fdbd8SKent Overstreet return 0; 9211c6fdbd8SKent Overstreet } 9221c6fdbd8SKent Overstreet 9231c6fdbd8SKent Overstreet static inline struct page *readpage_iter_next(struct readpages_iter *iter) 9241c6fdbd8SKent Overstreet { 9251c6fdbd8SKent Overstreet if (iter->idx >= iter->nr_pages) 9261c6fdbd8SKent Overstreet return NULL; 9271c6fdbd8SKent Overstreet 9281c6fdbd8SKent Overstreet EBUG_ON(iter->pages[iter->idx]->index != iter->offset + iter->idx); 9291c6fdbd8SKent Overstreet 9301c6fdbd8SKent Overstreet return iter->pages[iter->idx]; 9311c6fdbd8SKent Overstreet } 9321c6fdbd8SKent Overstreet 93335189e09SKent Overstreet static bool extent_partial_reads_expensive(struct bkey_s_c k) 93435189e09SKent Overstreet { 93535189e09SKent Overstreet struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k); 93635189e09SKent Overstreet struct bch_extent_crc_unpacked crc; 93735189e09SKent Overstreet const union bch_extent_entry *i; 93835189e09SKent Overstreet 93935189e09SKent Overstreet bkey_for_each_crc(k.k, ptrs, crc, i) 94035189e09SKent Overstreet if (crc.csum_type || crc.compression_type) 94135189e09SKent Overstreet return true; 94235189e09SKent Overstreet return false; 94335189e09SKent Overstreet } 94435189e09SKent Overstreet 9451c6fdbd8SKent Overstreet static void readpage_bio_extend(struct readpages_iter *iter, 94676426098SKent Overstreet struct bio *bio, 94776426098SKent Overstreet unsigned sectors_this_extent, 9481c6fdbd8SKent Overstreet bool get_more) 9491c6fdbd8SKent Overstreet { 95076426098SKent Overstreet while (bio_sectors(bio) < sectors_this_extent && 9511c6fdbd8SKent Overstreet bio->bi_vcnt < bio->bi_max_vecs) { 9527279c1a2SKent Overstreet pgoff_t page_offset = bio_end_sector(bio) >> PAGE_SECTORS_SHIFT; 9531c6fdbd8SKent Overstreet struct page *page = readpage_iter_next(iter); 9541c6fdbd8SKent Overstreet int ret; 9551c6fdbd8SKent Overstreet 9561c6fdbd8SKent Overstreet if (page) { 9571c6fdbd8SKent Overstreet if (iter->offset + iter->idx != page_offset) 9581c6fdbd8SKent Overstreet break; 9591c6fdbd8SKent Overstreet 9601c6fdbd8SKent Overstreet iter->idx++; 9611c6fdbd8SKent Overstreet } else { 9621c6fdbd8SKent Overstreet if (!get_more) 9631c6fdbd8SKent Overstreet break; 9641c6fdbd8SKent Overstreet 9651c6fdbd8SKent Overstreet page = xa_load(&iter->mapping->i_pages, page_offset); 9661c6fdbd8SKent Overstreet if (page && !xa_is_value(page)) 9671c6fdbd8SKent Overstreet break; 9681c6fdbd8SKent Overstreet 9691c6fdbd8SKent Overstreet page = __page_cache_alloc(readahead_gfp_mask(iter->mapping)); 9701c6fdbd8SKent Overstreet if (!page) 9711c6fdbd8SKent Overstreet break; 9721c6fdbd8SKent Overstreet 973f57a6a5dSKent Overstreet if (!__bch2_page_state_create(page, 0)) { 974f57a6a5dSKent Overstreet put_page(page); 975f57a6a5dSKent Overstreet break; 976f57a6a5dSKent Overstreet } 9771c6fdbd8SKent Overstreet 9781c6fdbd8SKent Overstreet ret = add_to_page_cache_lru(page, iter->mapping, 9791c6fdbd8SKent Overstreet page_offset, GFP_NOFS); 9801c6fdbd8SKent Overstreet if (ret) { 981f57a6a5dSKent Overstreet __bch2_page_state_release(page); 9821c6fdbd8SKent Overstreet put_page(page); 9831c6fdbd8SKent Overstreet break; 9841c6fdbd8SKent Overstreet } 9851c6fdbd8SKent Overstreet 9861c6fdbd8SKent Overstreet put_page(page); 9871c6fdbd8SKent Overstreet } 9881c6fdbd8SKent Overstreet 989885678f6SKent Overstreet BUG_ON(!bio_add_page(bio, page, PAGE_SIZE, 0)); 9901c6fdbd8SKent Overstreet } 9911c6fdbd8SKent Overstreet } 9921c6fdbd8SKent Overstreet 9938c6d298aSKent Overstreet static void bchfs_read(struct btree_trans *trans, 9948c6d298aSKent Overstreet struct bch_read_bio *rbio, 9958c6d298aSKent Overstreet subvol_inum inum, 9961c6fdbd8SKent Overstreet struct readpages_iter *readpages_iter) 9971c6fdbd8SKent Overstreet { 9980f238367SKent Overstreet struct bch_fs *c = trans->c; 9998c6d298aSKent Overstreet struct btree_iter iter; 100007a1006aSKent Overstreet struct bkey_buf sk; 10011c6fdbd8SKent Overstreet int flags = BCH_READ_RETRY_IF_STALE| 10021c6fdbd8SKent Overstreet BCH_READ_MAY_PROMOTE; 10038c6d298aSKent Overstreet u32 snapshot; 100476426098SKent Overstreet int ret = 0; 10051c6fdbd8SKent Overstreet 10061c6fdbd8SKent Overstreet rbio->c = c; 10071c6fdbd8SKent Overstreet rbio->start_time = local_clock(); 10088c6d298aSKent Overstreet rbio->subvol = inum.subvol; 100935189e09SKent Overstreet 101007a1006aSKent Overstreet bch2_bkey_buf_init(&sk); 101176426098SKent Overstreet retry: 1012700c25b3SKent Overstreet bch2_trans_begin(trans); 10138c6d298aSKent Overstreet iter = (struct btree_iter) { NULL }; 1014700c25b3SKent Overstreet 10158c6d298aSKent Overstreet ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot); 10168c6d298aSKent Overstreet if (ret) 10178c6d298aSKent Overstreet goto err; 10188c6d298aSKent Overstreet 10198c6d298aSKent Overstreet bch2_trans_iter_init(trans, &iter, BTREE_ID_extents, 10208c6d298aSKent Overstreet SPOS(inum.inum, rbio->bio.bi_iter.bi_sector, snapshot), 102157cfdd8bSKent Overstreet BTREE_ITER_SLOTS); 10221c6fdbd8SKent Overstreet while (1) { 10231c6fdbd8SKent Overstreet struct bkey_s_c k; 102476426098SKent Overstreet unsigned bytes, sectors, offset_into_extent; 10255ff75ccbSKent Overstreet enum btree_id data_btree = BTREE_ID_extents; 10261c6fdbd8SKent Overstreet 10273737e0ddSKent Overstreet /* 10283737e0ddSKent Overstreet * read_extent -> io_time_reset may cause a transaction restart 10293737e0ddSKent Overstreet * without returning an error, we need to check for that here: 10303737e0ddSKent Overstreet */ 1031549d173cSKent Overstreet ret = bch2_trans_relock(trans); 1032549d173cSKent Overstreet if (ret) 10333737e0ddSKent Overstreet break; 10343737e0ddSKent Overstreet 10358c6d298aSKent Overstreet bch2_btree_iter_set_pos(&iter, 10368c6d298aSKent Overstreet POS(inum.inum, rbio->bio.bi_iter.bi_sector)); 10371c6fdbd8SKent Overstreet 10388c6d298aSKent Overstreet k = bch2_btree_iter_peek_slot(&iter); 103976426098SKent Overstreet ret = bkey_err(k); 104076426098SKent Overstreet if (ret) 104176426098SKent Overstreet break; 10421c6fdbd8SKent Overstreet 10438c6d298aSKent Overstreet offset_into_extent = iter.pos.offset - 104406ed8558SKent Overstreet bkey_start_offset(k.k); 104576426098SKent Overstreet sectors = k.k->size - offset_into_extent; 104676426098SKent Overstreet 104707a1006aSKent Overstreet bch2_bkey_buf_reassemble(&sk, c, k); 104813dcd4abSKent Overstreet 10495ff75ccbSKent Overstreet ret = bch2_read_indirect_extent(trans, &data_btree, 105022d8a33dSYuxuan Shui &offset_into_extent, &sk); 105176426098SKent Overstreet if (ret) 105276426098SKent Overstreet break; 105376426098SKent Overstreet 105413dcd4abSKent Overstreet k = bkey_i_to_s_c(sk.k); 105513dcd4abSKent Overstreet 105676426098SKent Overstreet sectors = min(sectors, k.k->size - offset_into_extent); 105776426098SKent Overstreet 105835189e09SKent Overstreet if (readpages_iter) 105935189e09SKent Overstreet readpage_bio_extend(readpages_iter, &rbio->bio, sectors, 106035189e09SKent Overstreet extent_partial_reads_expensive(k)); 10611c6fdbd8SKent Overstreet 106276426098SKent Overstreet bytes = min(sectors, bio_sectors(&rbio->bio)) << 9; 106306ed8558SKent Overstreet swap(rbio->bio.bi_iter.bi_size, bytes); 10641c6fdbd8SKent Overstreet 106506ed8558SKent Overstreet if (rbio->bio.bi_iter.bi_size == bytes) 10661c6fdbd8SKent Overstreet flags |= BCH_READ_LAST_FRAGMENT; 10671c6fdbd8SKent Overstreet 1068b44a66a6SKent Overstreet bch2_bio_page_state_set(&rbio->bio, k); 10691c6fdbd8SKent Overstreet 10708c6d298aSKent Overstreet bch2_read_extent(trans, rbio, iter.pos, 10715ff75ccbSKent Overstreet data_btree, k, offset_into_extent, flags); 10721c6fdbd8SKent Overstreet 10731c6fdbd8SKent Overstreet if (flags & BCH_READ_LAST_FRAGMENT) 107435189e09SKent Overstreet break; 10751c6fdbd8SKent Overstreet 107606ed8558SKent Overstreet swap(rbio->bio.bi_iter.bi_size, bytes); 107706ed8558SKent Overstreet bio_advance(&rbio->bio, bytes); 1078084d42bbSKent Overstreet 1079084d42bbSKent Overstreet ret = btree_trans_too_many_iters(trans); 1080084d42bbSKent Overstreet if (ret) 1081084d42bbSKent Overstreet break; 10821c6fdbd8SKent Overstreet } 10838c6d298aSKent Overstreet err: 10848c6d298aSKent Overstreet bch2_trans_iter_exit(trans, &iter); 108576426098SKent Overstreet 1086549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 108776426098SKent Overstreet goto retry; 108876426098SKent Overstreet 108935189e09SKent Overstreet if (ret) { 10907fec8266SKent Overstreet bch_err_inum_offset_ratelimited(c, 10917fec8266SKent Overstreet iter.pos.inode, 10927fec8266SKent Overstreet iter.pos.offset << 9, 10930fefe8d8SKent Overstreet "read error %i from btree lookup", ret); 10940fefe8d8SKent Overstreet rbio->bio.bi_status = BLK_STS_IOERR; 109576426098SKent Overstreet bio_endio(&rbio->bio); 10961c6fdbd8SKent Overstreet } 10971c6fdbd8SKent Overstreet 109807a1006aSKent Overstreet bch2_bkey_buf_exit(&sk, c); 109935189e09SKent Overstreet } 110035189e09SKent Overstreet 11011c6fdbd8SKent Overstreet void bch2_readahead(struct readahead_control *ractl) 11021c6fdbd8SKent Overstreet { 11031c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(ractl->mapping->host); 11041c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 11059a3df993SKent Overstreet struct bch_io_opts opts = io_opts(c, &inode->ei_inode); 1106424eb881SKent Overstreet struct btree_trans trans; 11071c6fdbd8SKent Overstreet struct page *page; 11081c6fdbd8SKent Overstreet struct readpages_iter readpages_iter; 11091c6fdbd8SKent Overstreet int ret; 11101c6fdbd8SKent Overstreet 11111c6fdbd8SKent Overstreet ret = readpages_iter_init(&readpages_iter, ractl); 11121c6fdbd8SKent Overstreet BUG_ON(ret); 11131c6fdbd8SKent Overstreet 111420bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 11151c6fdbd8SKent Overstreet 1116a7ecd30cSKent Overstreet bch2_pagecache_add_get(inode); 11171c6fdbd8SKent Overstreet 11181c6fdbd8SKent Overstreet while ((page = readpage_iter_next(&readpages_iter))) { 11191c6fdbd8SKent Overstreet pgoff_t index = readpages_iter.offset + readpages_iter.idx; 11201c6fdbd8SKent Overstreet unsigned n = min_t(unsigned, 11211c6fdbd8SKent Overstreet readpages_iter.nr_pages - 11221c6fdbd8SKent Overstreet readpages_iter.idx, 11231c6fdbd8SKent Overstreet BIO_MAX_VECS); 11241c6fdbd8SKent Overstreet struct bch_read_bio *rbio = 11251c6fdbd8SKent Overstreet rbio_init(bio_alloc_bioset(NULL, n, REQ_OP_READ, 11261c6fdbd8SKent Overstreet GFP_NOFS, &c->bio_read), 11271c6fdbd8SKent Overstreet opts); 11281c6fdbd8SKent Overstreet 11291c6fdbd8SKent Overstreet readpages_iter.idx++; 11301c6fdbd8SKent Overstreet 11317279c1a2SKent Overstreet rbio->bio.bi_iter.bi_sector = (sector_t) index << PAGE_SECTORS_SHIFT; 11321c6fdbd8SKent Overstreet rbio->bio.bi_end_io = bch2_readpages_end_io; 1133885678f6SKent Overstreet BUG_ON(!bio_add_page(&rbio->bio, page, PAGE_SIZE, 0)); 11341c6fdbd8SKent Overstreet 11358c6d298aSKent Overstreet bchfs_read(&trans, rbio, inode_inum(inode), 11360f238367SKent Overstreet &readpages_iter); 11371c6fdbd8SKent Overstreet } 11381c6fdbd8SKent Overstreet 1139a7ecd30cSKent Overstreet bch2_pagecache_add_put(inode); 1140424eb881SKent Overstreet 1141424eb881SKent Overstreet bch2_trans_exit(&trans); 11421c6fdbd8SKent Overstreet kfree(readpages_iter.pages); 11431c6fdbd8SKent Overstreet } 11441c6fdbd8SKent Overstreet 11451c6fdbd8SKent Overstreet static void __bchfs_readpage(struct bch_fs *c, struct bch_read_bio *rbio, 11468c6d298aSKent Overstreet subvol_inum inum, struct page *page) 11471c6fdbd8SKent Overstreet { 1148424eb881SKent Overstreet struct btree_trans trans; 11491c6fdbd8SKent Overstreet 1150f57a6a5dSKent Overstreet bch2_page_state_create(page, __GFP_NOFAIL); 11511c6fdbd8SKent Overstreet 11521c6fdbd8SKent Overstreet rbio->bio.bi_opf = REQ_OP_READ|REQ_SYNC; 11537f5e31e1SKent Overstreet rbio->bio.bi_iter.bi_sector = 11547279c1a2SKent Overstreet (sector_t) page->index << PAGE_SECTORS_SHIFT; 11557f5e31e1SKent Overstreet BUG_ON(!bio_add_page(&rbio->bio, page, PAGE_SIZE, 0)); 11561c6fdbd8SKent Overstreet 115720bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 11588c6d298aSKent Overstreet bchfs_read(&trans, rbio, inum, NULL); 1159424eb881SKent Overstreet bch2_trans_exit(&trans); 11601c6fdbd8SKent Overstreet } 11611c6fdbd8SKent Overstreet 11621c6fdbd8SKent Overstreet static void bch2_read_single_page_end_io(struct bio *bio) 11631c6fdbd8SKent Overstreet { 11641c6fdbd8SKent Overstreet complete(bio->bi_private); 11651c6fdbd8SKent Overstreet } 11661c6fdbd8SKent Overstreet 11671c6fdbd8SKent Overstreet static int bch2_read_single_page(struct page *page, 11681c6fdbd8SKent Overstreet struct address_space *mapping) 11691c6fdbd8SKent Overstreet { 11701c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(mapping->host); 11711c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 11721c6fdbd8SKent Overstreet struct bch_read_bio *rbio; 11731c6fdbd8SKent Overstreet int ret; 11741c6fdbd8SKent Overstreet DECLARE_COMPLETION_ONSTACK(done); 11751c6fdbd8SKent Overstreet 11761c6fdbd8SKent Overstreet rbio = rbio_init(bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS, &c->bio_read), 11779a3df993SKent Overstreet io_opts(c, &inode->ei_inode)); 11781c6fdbd8SKent Overstreet rbio->bio.bi_private = &done; 11791c6fdbd8SKent Overstreet rbio->bio.bi_end_io = bch2_read_single_page_end_io; 11801c6fdbd8SKent Overstreet 11818c6d298aSKent Overstreet __bchfs_readpage(c, rbio, inode_inum(inode), page); 11821c6fdbd8SKent Overstreet wait_for_completion(&done); 11831c6fdbd8SKent Overstreet 11841c6fdbd8SKent Overstreet ret = blk_status_to_errno(rbio->bio.bi_status); 11851c6fdbd8SKent Overstreet bio_put(&rbio->bio); 11861c6fdbd8SKent Overstreet 11871c6fdbd8SKent Overstreet if (ret < 0) 11881c6fdbd8SKent Overstreet return ret; 11891c6fdbd8SKent Overstreet 11901c6fdbd8SKent Overstreet SetPageUptodate(page); 11911c6fdbd8SKent Overstreet return 0; 11921c6fdbd8SKent Overstreet } 11931c6fdbd8SKent Overstreet 11941c6fdbd8SKent Overstreet int bch2_read_folio(struct file *file, struct folio *folio) 11951c6fdbd8SKent Overstreet { 11961c6fdbd8SKent Overstreet struct page *page = &folio->page; 11971c6fdbd8SKent Overstreet int ret; 11981c6fdbd8SKent Overstreet 11991c6fdbd8SKent Overstreet ret = bch2_read_single_page(page, page->mapping); 12001c6fdbd8SKent Overstreet folio_unlock(folio); 12015c1ef830SKent Overstreet return bch2_err_class(ret); 12021c6fdbd8SKent Overstreet } 12031c6fdbd8SKent Overstreet 12041c6fdbd8SKent Overstreet /* writepages: */ 12051c6fdbd8SKent Overstreet 12061c6fdbd8SKent Overstreet struct bch_writepage_state { 12071c6fdbd8SKent Overstreet struct bch_writepage_io *io; 12081c6fdbd8SKent Overstreet struct bch_io_opts opts; 12091c6fdbd8SKent Overstreet }; 12101c6fdbd8SKent Overstreet 12111c6fdbd8SKent Overstreet static inline struct bch_writepage_state bch_writepage_state_init(struct bch_fs *c, 12121c6fdbd8SKent Overstreet struct bch_inode_info *inode) 12131c6fdbd8SKent Overstreet { 12149a3df993SKent Overstreet return (struct bch_writepage_state) { 12159a3df993SKent Overstreet .opts = io_opts(c, &inode->ei_inode) 12169a3df993SKent Overstreet }; 12171c6fdbd8SKent Overstreet } 12181c6fdbd8SKent Overstreet 12199f311f21SKent Overstreet static void bch2_writepage_io_done(struct bch_write_op *op) 12201c6fdbd8SKent Overstreet { 12219f311f21SKent Overstreet struct bch_writepage_io *io = 12229f311f21SKent Overstreet container_of(op, struct bch_writepage_io, op); 12239a3df993SKent Overstreet struct bch_fs *c = io->op.c; 12249a3df993SKent Overstreet struct bio *bio = &io->op.wbio.bio; 12251c6fdbd8SKent Overstreet struct bvec_iter_all iter; 12261c6fdbd8SKent Overstreet struct bio_vec *bvec; 1227b3fce09cSKent Overstreet unsigned i; 12281c6fdbd8SKent Overstreet 12299a3df993SKent Overstreet if (io->op.error) { 123033c74e41SKent Overstreet set_bit(EI_INODE_ERROR, &io->inode->ei_flags); 123133c74e41SKent Overstreet 123275812e70SKent Overstreet bio_for_each_segment_all(bvec, bio, iter) { 1233b3fce09cSKent Overstreet struct bch_page_state *s; 1234b3fce09cSKent Overstreet 12351c6fdbd8SKent Overstreet SetPageError(bvec->bv_page); 123675812e70SKent Overstreet mapping_set_error(bvec->bv_page->mapping, -EIO); 1237b3fce09cSKent Overstreet 12383826ee0bSKent Overstreet s = __bch2_page_state(bvec->bv_page); 12393826ee0bSKent Overstreet spin_lock(&s->lock); 1240b3fce09cSKent Overstreet for (i = 0; i < PAGE_SECTORS; i++) 1241b3fce09cSKent Overstreet s->s[i].nr_replicas = 0; 12423826ee0bSKent Overstreet spin_unlock(&s->lock); 124375812e70SKent Overstreet } 12441c6fdbd8SKent Overstreet } 12451c6fdbd8SKent Overstreet 12464be1a412SKent Overstreet if (io->op.flags & BCH_WRITE_WROTE_DATA_INLINE) { 12474be1a412SKent Overstreet bio_for_each_segment_all(bvec, bio, iter) { 12484be1a412SKent Overstreet struct bch_page_state *s; 12494be1a412SKent Overstreet 12504be1a412SKent Overstreet s = __bch2_page_state(bvec->bv_page); 12514be1a412SKent Overstreet spin_lock(&s->lock); 12524be1a412SKent Overstreet for (i = 0; i < PAGE_SECTORS; i++) 12534be1a412SKent Overstreet s->s[i].nr_replicas = 0; 12544be1a412SKent Overstreet spin_unlock(&s->lock); 12554be1a412SKent Overstreet } 12564be1a412SKent Overstreet } 12574be1a412SKent Overstreet 12581c6fdbd8SKent Overstreet /* 12591c6fdbd8SKent Overstreet * racing with fallocate can cause us to add fewer sectors than 12601c6fdbd8SKent Overstreet * expected - but we shouldn't add more sectors than expected: 12611c6fdbd8SKent Overstreet */ 1262f8494d25SKent Overstreet WARN_ON_ONCE(io->op.i_sectors_delta > 0); 12631c6fdbd8SKent Overstreet 12641c6fdbd8SKent Overstreet /* 12651c6fdbd8SKent Overstreet * (error (due to going RO) halfway through a page can screw that up 12661c6fdbd8SKent Overstreet * slightly) 12671c6fdbd8SKent Overstreet * XXX wtf? 12689a3df993SKent Overstreet BUG_ON(io->op.op.i_sectors_delta >= PAGE_SECTORS); 12691c6fdbd8SKent Overstreet */ 12701c6fdbd8SKent Overstreet 12711c6fdbd8SKent Overstreet /* 12721c6fdbd8SKent Overstreet * PageWriteback is effectively our ref on the inode - fixup i_blocks 12731c6fdbd8SKent Overstreet * before calling end_page_writeback: 12741c6fdbd8SKent Overstreet */ 12759a3df993SKent Overstreet i_sectors_acct(c, io->inode, NULL, io->op.i_sectors_delta); 12761c6fdbd8SKent Overstreet 12777f5e31e1SKent Overstreet bio_for_each_segment_all(bvec, bio, iter) { 12787f5e31e1SKent Overstreet struct bch_page_state *s = __bch2_page_state(bvec->bv_page); 12797f5e31e1SKent Overstreet 12807f5e31e1SKent Overstreet if (atomic_dec_and_test(&s->write_count)) 12811c6fdbd8SKent Overstreet end_page_writeback(bvec->bv_page); 12827f5e31e1SKent Overstreet } 12831c6fdbd8SKent Overstreet 12849f311f21SKent Overstreet bio_put(&io->op.wbio.bio); 12851c6fdbd8SKent Overstreet } 12861c6fdbd8SKent Overstreet 12871c6fdbd8SKent Overstreet static void bch2_writepage_do_io(struct bch_writepage_state *w) 12881c6fdbd8SKent Overstreet { 12891c6fdbd8SKent Overstreet struct bch_writepage_io *io = w->io; 12901c6fdbd8SKent Overstreet 12911c6fdbd8SKent Overstreet w->io = NULL; 12929f311f21SKent Overstreet closure_call(&io->op.cl, bch2_write, NULL, NULL); 12931c6fdbd8SKent Overstreet } 12941c6fdbd8SKent Overstreet 12951c6fdbd8SKent Overstreet /* 12961c6fdbd8SKent Overstreet * Get a bch_writepage_io and add @page to it - appending to an existing one if 12971c6fdbd8SKent Overstreet * possible, else allocating a new one: 12981c6fdbd8SKent Overstreet */ 12991c6fdbd8SKent Overstreet static void bch2_writepage_io_alloc(struct bch_fs *c, 130050fe5bd6SKent Overstreet struct writeback_control *wbc, 13011c6fdbd8SKent Overstreet struct bch_writepage_state *w, 13021c6fdbd8SKent Overstreet struct bch_inode_info *inode, 13037f5e31e1SKent Overstreet u64 sector, 13041c6fdbd8SKent Overstreet unsigned nr_replicas) 13051c6fdbd8SKent Overstreet { 13061c6fdbd8SKent Overstreet struct bch_write_op *op; 13071c6fdbd8SKent Overstreet 13081c6fdbd8SKent Overstreet w->io = container_of(bio_alloc_bioset(NULL, BIO_MAX_VECS, 13091c6fdbd8SKent Overstreet REQ_OP_WRITE, 13101c6fdbd8SKent Overstreet GFP_NOFS, 13111c6fdbd8SKent Overstreet &c->writepage_bioset), 13129a3df993SKent Overstreet struct bch_writepage_io, op.wbio.bio); 13131c6fdbd8SKent Overstreet 13149a3df993SKent Overstreet w->io->inode = inode; 13159a3df993SKent Overstreet op = &w->io->op; 13169a3df993SKent Overstreet bch2_write_op_init(op, c, w->opts); 13179a3df993SKent Overstreet op->target = w->opts.foreground_target; 13181c6fdbd8SKent Overstreet op->nr_replicas = nr_replicas; 13191c6fdbd8SKent Overstreet op->res.nr_replicas = nr_replicas; 13201c6fdbd8SKent Overstreet op->write_point = writepoint_hashed(inode->ei_last_dirtied); 13218c6d298aSKent Overstreet op->subvol = inode->ei_subvol; 13227f5e31e1SKent Overstreet op->pos = POS(inode->v.i_ino, sector); 13239f311f21SKent Overstreet op->end_io = bch2_writepage_io_done; 13247f5e31e1SKent Overstreet op->wbio.bio.bi_iter.bi_sector = sector; 132550fe5bd6SKent Overstreet op->wbio.bio.bi_opf = wbc_to_write_flags(wbc); 13261c6fdbd8SKent Overstreet } 13271c6fdbd8SKent Overstreet 13281c6fdbd8SKent Overstreet static int __bch2_writepage(struct folio *folio, 13291c6fdbd8SKent Overstreet struct writeback_control *wbc, 13301c6fdbd8SKent Overstreet void *data) 13311c6fdbd8SKent Overstreet { 13321c6fdbd8SKent Overstreet struct page *page = &folio->page; 13331c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(page->mapping->host); 13341c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 13351c6fdbd8SKent Overstreet struct bch_writepage_state *w = data; 13367f5e31e1SKent Overstreet struct bch_page_state *s, orig; 13377f5e31e1SKent Overstreet unsigned i, offset, nr_replicas_this_write = U32_MAX; 13381c6fdbd8SKent Overstreet loff_t i_size = i_size_read(&inode->v); 13391c6fdbd8SKent Overstreet pgoff_t end_index = i_size >> PAGE_SHIFT; 1340e1036a2aSKent Overstreet int ret; 13411c6fdbd8SKent Overstreet 13421c6fdbd8SKent Overstreet EBUG_ON(!PageUptodate(page)); 13431c6fdbd8SKent Overstreet 13441c6fdbd8SKent Overstreet /* Is the page fully inside i_size? */ 13451c6fdbd8SKent Overstreet if (page->index < end_index) 13461c6fdbd8SKent Overstreet goto do_io; 13471c6fdbd8SKent Overstreet 13481c6fdbd8SKent Overstreet /* Is the page fully outside i_size? (truncate in progress) */ 13491c6fdbd8SKent Overstreet offset = i_size & (PAGE_SIZE - 1); 13501c6fdbd8SKent Overstreet if (page->index > end_index || !offset) { 13511c6fdbd8SKent Overstreet unlock_page(page); 13521c6fdbd8SKent Overstreet return 0; 13531c6fdbd8SKent Overstreet } 13541c6fdbd8SKent Overstreet 13551c6fdbd8SKent Overstreet /* 13561c6fdbd8SKent Overstreet * The page straddles i_size. It must be zeroed out on each and every 13571c6fdbd8SKent Overstreet * writepage invocation because it may be mmapped. "A file is mapped 13581c6fdbd8SKent Overstreet * in multiples of the page size. For a file that is not a multiple of 13591c6fdbd8SKent Overstreet * the page size, the remaining memory is zeroed when mapped, and 13601c6fdbd8SKent Overstreet * writes to that region are not written out to the file." 13611c6fdbd8SKent Overstreet */ 13621c6fdbd8SKent Overstreet zero_user_segment(page, offset, PAGE_SIZE); 13631c6fdbd8SKent Overstreet do_io: 1364f57a6a5dSKent Overstreet s = bch2_page_state_create(page, __GFP_NOFAIL); 1365f81b648dSKent Overstreet 1366f74a5051SKent Overstreet /* 1367f74a5051SKent Overstreet * Things get really hairy with errors during writeback: 1368f74a5051SKent Overstreet */ 1369f74a5051SKent Overstreet ret = bch2_get_page_disk_reservation(c, inode, page, false); 1370f74a5051SKent Overstreet BUG_ON(ret); 1371e1036a2aSKent Overstreet 13727f5e31e1SKent Overstreet /* Before unlocking the page, get copy of reservations: */ 1373f74a5051SKent Overstreet spin_lock(&s->lock); 13747f5e31e1SKent Overstreet orig = *s; 1375f74a5051SKent Overstreet spin_unlock(&s->lock); 13767f5e31e1SKent Overstreet 13777f5e31e1SKent Overstreet for (i = 0; i < PAGE_SECTORS; i++) { 13782ba5d38bSKent Overstreet if (s->s[i].state < SECTOR_DIRTY) 13797f5e31e1SKent Overstreet continue; 13807f5e31e1SKent Overstreet 1381f81b648dSKent Overstreet nr_replicas_this_write = 1382f57a6a5dSKent Overstreet min_t(unsigned, nr_replicas_this_write, 1383f57a6a5dSKent Overstreet s->s[i].nr_replicas + 1384f57a6a5dSKent Overstreet s->s[i].replicas_reserved); 13857f5e31e1SKent Overstreet } 13861c6fdbd8SKent Overstreet 1387f57a6a5dSKent Overstreet for (i = 0; i < PAGE_SECTORS; i++) { 13882ba5d38bSKent Overstreet if (s->s[i].state < SECTOR_DIRTY) 13897f5e31e1SKent Overstreet continue; 13907f5e31e1SKent Overstreet 1391f57a6a5dSKent Overstreet s->s[i].nr_replicas = w->opts.compression 1392f57a6a5dSKent Overstreet ? 0 : nr_replicas_this_write; 1393e1036a2aSKent Overstreet 1394f57a6a5dSKent Overstreet s->s[i].replicas_reserved = 0; 1395f57a6a5dSKent Overstreet s->s[i].state = SECTOR_ALLOCATED; 1396f57a6a5dSKent Overstreet } 13971c6fdbd8SKent Overstreet 13987f5e31e1SKent Overstreet BUG_ON(atomic_read(&s->write_count)); 13997f5e31e1SKent Overstreet atomic_set(&s->write_count, 1); 14007f5e31e1SKent Overstreet 14011c6fdbd8SKent Overstreet BUG_ON(PageWriteback(page)); 14021c6fdbd8SKent Overstreet set_page_writeback(page); 14037f5e31e1SKent Overstreet 14041c6fdbd8SKent Overstreet unlock_page(page); 14051c6fdbd8SKent Overstreet 14067f5e31e1SKent Overstreet offset = 0; 14077f5e31e1SKent Overstreet while (1) { 1408f74a5051SKent Overstreet unsigned sectors = 0, dirty_sectors = 0, reserved_sectors = 0; 14097f5e31e1SKent Overstreet u64 sector; 14107f5e31e1SKent Overstreet 14117f5e31e1SKent Overstreet while (offset < PAGE_SECTORS && 14122ba5d38bSKent Overstreet orig.s[offset].state < SECTOR_DIRTY) 14137f5e31e1SKent Overstreet offset++; 14147f5e31e1SKent Overstreet 14157f5e31e1SKent Overstreet if (offset == PAGE_SECTORS) 14167f5e31e1SKent Overstreet break; 14177f5e31e1SKent Overstreet 14187f5e31e1SKent Overstreet while (offset + sectors < PAGE_SECTORS && 1419f74a5051SKent Overstreet orig.s[offset + sectors].state >= SECTOR_DIRTY) { 1420f74a5051SKent Overstreet reserved_sectors += orig.s[offset + sectors].replicas_reserved; 1421f74a5051SKent Overstreet dirty_sectors += orig.s[offset + sectors].state == SECTOR_DIRTY; 14227f5e31e1SKent Overstreet sectors++; 14237f5e31e1SKent Overstreet } 1424f74a5051SKent Overstreet BUG_ON(!sectors); 1425f74a5051SKent Overstreet 14267279c1a2SKent Overstreet sector = ((u64) page->index << PAGE_SECTORS_SHIFT) + offset; 14277f5e31e1SKent Overstreet 14281c6fdbd8SKent Overstreet if (w->io && 14299a3df993SKent Overstreet (w->io->op.res.nr_replicas != nr_replicas_this_write || 14309a3df993SKent Overstreet bio_full(&w->io->op.wbio.bio, PAGE_SIZE) || 1431f59b3464SKent Overstreet w->io->op.wbio.bio.bi_iter.bi_size + (sectors << 9) >= 1432f59b3464SKent Overstreet (BIO_MAX_VECS * PAGE_SIZE) || 14339a3df993SKent Overstreet bio_end_sector(&w->io->op.wbio.bio) != sector)) 14341c6fdbd8SKent Overstreet bch2_writepage_do_io(w); 14351c6fdbd8SKent Overstreet 14361c6fdbd8SKent Overstreet if (!w->io) 143750fe5bd6SKent Overstreet bch2_writepage_io_alloc(c, wbc, w, inode, sector, 1438f81b648dSKent Overstreet nr_replicas_this_write); 14391c6fdbd8SKent Overstreet 14407f5e31e1SKent Overstreet atomic_inc(&s->write_count); 14417f5e31e1SKent Overstreet 14429a3df993SKent Overstreet BUG_ON(inode != w->io->inode); 14439a3df993SKent Overstreet BUG_ON(!bio_add_page(&w->io->op.wbio.bio, page, 14447f5e31e1SKent Overstreet sectors << 9, offset << 9)); 14451c6fdbd8SKent Overstreet 14466cc3535dSKent Overstreet /* Check for writing past i_size: */ 14478eb71e9eSKent Overstreet WARN_ONCE((bio_end_sector(&w->io->op.wbio.bio) << 9) > 144880fe580cSKent Overstreet round_up(i_size, block_bytes(c)) && 14498eb71e9eSKent Overstreet !test_bit(BCH_FS_EMERGENCY_RO, &c->flags), 14508eb71e9eSKent Overstreet "writing past i_size: %llu > %llu (unrounded %llu)\n", 14518eb71e9eSKent Overstreet bio_end_sector(&w->io->op.wbio.bio) << 9, 14528eb71e9eSKent Overstreet round_up(i_size, block_bytes(c)), 14538eb71e9eSKent Overstreet i_size); 14546cc3535dSKent Overstreet 14559a3df993SKent Overstreet w->io->op.res.sectors += reserved_sectors; 14569a3df993SKent Overstreet w->io->op.i_sectors_delta -= dirty_sectors; 14571c6fdbd8SKent Overstreet w->io->op.new_i_size = i_size; 14581c6fdbd8SKent Overstreet 14597f5e31e1SKent Overstreet offset += sectors; 14607f5e31e1SKent Overstreet } 14617f5e31e1SKent Overstreet 14627f5e31e1SKent Overstreet if (atomic_dec_and_test(&s->write_count)) 14637f5e31e1SKent Overstreet end_page_writeback(page); 14647f5e31e1SKent Overstreet 14651c6fdbd8SKent Overstreet return 0; 14661c6fdbd8SKent Overstreet } 14671c6fdbd8SKent Overstreet 14681c6fdbd8SKent Overstreet int bch2_writepages(struct address_space *mapping, struct writeback_control *wbc) 14691c6fdbd8SKent Overstreet { 14701c6fdbd8SKent Overstreet struct bch_fs *c = mapping->host->i_sb->s_fs_info; 14711c6fdbd8SKent Overstreet struct bch_writepage_state w = 14721c6fdbd8SKent Overstreet bch_writepage_state_init(c, to_bch_ei(mapping->host)); 14731c6fdbd8SKent Overstreet struct blk_plug plug; 14741c6fdbd8SKent Overstreet int ret; 14751c6fdbd8SKent Overstreet 14761c6fdbd8SKent Overstreet blk_start_plug(&plug); 14771c6fdbd8SKent Overstreet ret = write_cache_pages(mapping, wbc, __bch2_writepage, &w); 14781c6fdbd8SKent Overstreet if (w.io) 14791c6fdbd8SKent Overstreet bch2_writepage_do_io(&w); 14801c6fdbd8SKent Overstreet blk_finish_plug(&plug); 14815c1ef830SKent Overstreet return bch2_err_class(ret); 14821c6fdbd8SKent Overstreet } 14831c6fdbd8SKent Overstreet 14841c6fdbd8SKent Overstreet /* buffered writes: */ 14851c6fdbd8SKent Overstreet 14861c6fdbd8SKent Overstreet int bch2_write_begin(struct file *file, struct address_space *mapping, 14871c6fdbd8SKent Overstreet loff_t pos, unsigned len, 14881c6fdbd8SKent Overstreet struct page **pagep, void **fsdata) 14891c6fdbd8SKent Overstreet { 14901c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(mapping->host); 14911c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 1492d1542e03SKent Overstreet struct bch2_page_reservation *res; 14931c6fdbd8SKent Overstreet pgoff_t index = pos >> PAGE_SHIFT; 14941c6fdbd8SKent Overstreet unsigned offset = pos & (PAGE_SIZE - 1); 14951c6fdbd8SKent Overstreet struct page *page; 14961c6fdbd8SKent Overstreet int ret = -ENOMEM; 14971c6fdbd8SKent Overstreet 1498d1542e03SKent Overstreet res = kmalloc(sizeof(*res), GFP_KERNEL); 1499d1542e03SKent Overstreet if (!res) 1500d1542e03SKent Overstreet return -ENOMEM; 1501d1542e03SKent Overstreet 1502d1542e03SKent Overstreet bch2_page_reservation_init(c, inode, res); 1503d1542e03SKent Overstreet *fsdata = res; 15041c6fdbd8SKent Overstreet 1505a7ecd30cSKent Overstreet bch2_pagecache_add_get(inode); 15061c6fdbd8SKent Overstreet 15071c6fdbd8SKent Overstreet page = grab_cache_page_write_begin(mapping, index); 15081c6fdbd8SKent Overstreet if (!page) 15091c6fdbd8SKent Overstreet goto err_unlock; 15101c6fdbd8SKent Overstreet 15111c6fdbd8SKent Overstreet if (PageUptodate(page)) 15121c6fdbd8SKent Overstreet goto out; 15131c6fdbd8SKent Overstreet 15141c6fdbd8SKent Overstreet /* If we're writing entire page, don't need to read it in first: */ 15151c6fdbd8SKent Overstreet if (len == PAGE_SIZE) 15161c6fdbd8SKent Overstreet goto out; 15171c6fdbd8SKent Overstreet 15181c6fdbd8SKent Overstreet if (!offset && pos + len >= inode->v.i_size) { 15191c6fdbd8SKent Overstreet zero_user_segment(page, len, PAGE_SIZE); 15201c6fdbd8SKent Overstreet flush_dcache_page(page); 15211c6fdbd8SKent Overstreet goto out; 15221c6fdbd8SKent Overstreet } 15231c6fdbd8SKent Overstreet 15241c6fdbd8SKent Overstreet if (index > inode->v.i_size >> PAGE_SHIFT) { 15251c6fdbd8SKent Overstreet zero_user_segments(page, 0, offset, offset + len, PAGE_SIZE); 15261c6fdbd8SKent Overstreet flush_dcache_page(page); 15271c6fdbd8SKent Overstreet goto out; 15281c6fdbd8SKent Overstreet } 15291c6fdbd8SKent Overstreet readpage: 15301c6fdbd8SKent Overstreet ret = bch2_read_single_page(page, mapping); 15311c6fdbd8SKent Overstreet if (ret) 15321c6fdbd8SKent Overstreet goto err; 15331c6fdbd8SKent Overstreet out: 1534e6ec361fSKent Overstreet if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) { 1535e6ec361fSKent Overstreet ret = bch2_page_state_set(c, inode_inum(inode), &page, 1); 1536e6ec361fSKent Overstreet if (ret) 15373a4d3656SKent Overstreet goto err; 1538e6ec361fSKent Overstreet } 1539e6ec361fSKent Overstreet 1540bd954215SKent Overstreet ret = bch2_page_reservation_get(c, inode, page, res, offset, len); 15411c6fdbd8SKent Overstreet if (ret) { 15421c6fdbd8SKent Overstreet if (!PageUptodate(page)) { 15431c6fdbd8SKent Overstreet /* 15441c6fdbd8SKent Overstreet * If the page hasn't been read in, we won't know if we 15451c6fdbd8SKent Overstreet * actually need a reservation - we don't actually need 15461c6fdbd8SKent Overstreet * to read here, we just need to check if the page is 15471c6fdbd8SKent Overstreet * fully backed by uncompressed data: 15481c6fdbd8SKent Overstreet */ 15491c6fdbd8SKent Overstreet goto readpage; 15501c6fdbd8SKent Overstreet } 15511c6fdbd8SKent Overstreet 15521c6fdbd8SKent Overstreet goto err; 15531c6fdbd8SKent Overstreet } 15541c6fdbd8SKent Overstreet 15551c6fdbd8SKent Overstreet *pagep = page; 15561c6fdbd8SKent Overstreet return 0; 15571c6fdbd8SKent Overstreet err: 15581c6fdbd8SKent Overstreet unlock_page(page); 15591c6fdbd8SKent Overstreet put_page(page); 15601c6fdbd8SKent Overstreet *pagep = NULL; 15611c6fdbd8SKent Overstreet err_unlock: 1562a7ecd30cSKent Overstreet bch2_pagecache_add_put(inode); 1563d1542e03SKent Overstreet kfree(res); 1564d1542e03SKent Overstreet *fsdata = NULL; 15655c1ef830SKent Overstreet return bch2_err_class(ret); 15661c6fdbd8SKent Overstreet } 15671c6fdbd8SKent Overstreet 15681c6fdbd8SKent Overstreet int bch2_write_end(struct file *file, struct address_space *mapping, 15691c6fdbd8SKent Overstreet loff_t pos, unsigned len, unsigned copied, 15701c6fdbd8SKent Overstreet struct page *page, void *fsdata) 15711c6fdbd8SKent Overstreet { 15721c6fdbd8SKent Overstreet struct bch_inode_info *inode = to_bch_ei(mapping->host); 15731c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 1574d1542e03SKent Overstreet struct bch2_page_reservation *res = fsdata; 1575d1542e03SKent Overstreet unsigned offset = pos & (PAGE_SIZE - 1); 15761c6fdbd8SKent Overstreet 15771c6fdbd8SKent Overstreet lockdep_assert_held(&inode->v.i_rwsem); 15781c6fdbd8SKent Overstreet 15791c6fdbd8SKent Overstreet if (unlikely(copied < len && !PageUptodate(page))) { 15801c6fdbd8SKent Overstreet /* 15811c6fdbd8SKent Overstreet * The page needs to be read in, but that would destroy 15821c6fdbd8SKent Overstreet * our partial write - simplest thing is to just force 15831c6fdbd8SKent Overstreet * userspace to redo the write: 15841c6fdbd8SKent Overstreet */ 15851c6fdbd8SKent Overstreet zero_user(page, 0, PAGE_SIZE); 15861c6fdbd8SKent Overstreet flush_dcache_page(page); 15871c6fdbd8SKent Overstreet copied = 0; 15881c6fdbd8SKent Overstreet } 15891c6fdbd8SKent Overstreet 15901c6fdbd8SKent Overstreet spin_lock(&inode->v.i_lock); 15911c6fdbd8SKent Overstreet if (pos + copied > inode->v.i_size) 15921c6fdbd8SKent Overstreet i_size_write(&inode->v, pos + copied); 15931c6fdbd8SKent Overstreet spin_unlock(&inode->v.i_lock); 15941c6fdbd8SKent Overstreet 15951c6fdbd8SKent Overstreet if (copied) { 15961c6fdbd8SKent Overstreet if (!PageUptodate(page)) 15971c6fdbd8SKent Overstreet SetPageUptodate(page); 1598d1542e03SKent Overstreet 1599d1542e03SKent Overstreet bch2_set_page_dirty(c, inode, page, res, offset, copied); 16001c6fdbd8SKent Overstreet 16011c6fdbd8SKent Overstreet inode->ei_last_dirtied = (unsigned long) current; 16021c6fdbd8SKent Overstreet } 16031c6fdbd8SKent Overstreet 16041c6fdbd8SKent Overstreet unlock_page(page); 16051c6fdbd8SKent Overstreet put_page(page); 1606a7ecd30cSKent Overstreet bch2_pagecache_add_put(inode); 16071c6fdbd8SKent Overstreet 1608d1542e03SKent Overstreet bch2_page_reservation_put(c, inode, res); 1609d1542e03SKent Overstreet kfree(res); 1610d1542e03SKent Overstreet 16111c6fdbd8SKent Overstreet return copied; 16121c6fdbd8SKent Overstreet } 16131c6fdbd8SKent Overstreet 16141c6fdbd8SKent Overstreet #define WRITE_BATCH_PAGES 32 16151c6fdbd8SKent Overstreet 16161c6fdbd8SKent Overstreet static int __bch2_buffered_write(struct bch_inode_info *inode, 16171c6fdbd8SKent Overstreet struct address_space *mapping, 16181c6fdbd8SKent Overstreet struct iov_iter *iter, 16191c6fdbd8SKent Overstreet loff_t pos, unsigned len) 16201c6fdbd8SKent Overstreet { 16211c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 16221c6fdbd8SKent Overstreet struct page *pages[WRITE_BATCH_PAGES]; 1623d1542e03SKent Overstreet struct bch2_page_reservation res; 16241c6fdbd8SKent Overstreet unsigned long index = pos >> PAGE_SHIFT; 16251c6fdbd8SKent Overstreet unsigned offset = pos & (PAGE_SIZE - 1); 16261c6fdbd8SKent Overstreet unsigned nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE); 1627d1542e03SKent Overstreet unsigned i, reserved = 0, set_dirty = 0; 1628d1542e03SKent Overstreet unsigned copied = 0, nr_pages_copied = 0; 16291c6fdbd8SKent Overstreet int ret = 0; 16301c6fdbd8SKent Overstreet 16311c6fdbd8SKent Overstreet BUG_ON(!len); 16321c6fdbd8SKent Overstreet BUG_ON(nr_pages > ARRAY_SIZE(pages)); 16331c6fdbd8SKent Overstreet 1634d1542e03SKent Overstreet bch2_page_reservation_init(c, inode, &res); 1635d1542e03SKent Overstreet 16361c6fdbd8SKent Overstreet for (i = 0; i < nr_pages; i++) { 16371c6fdbd8SKent Overstreet pages[i] = grab_cache_page_write_begin(mapping, index + i); 16381c6fdbd8SKent Overstreet if (!pages[i]) { 16391c6fdbd8SKent Overstreet nr_pages = i; 16408de819f8SKent Overstreet if (!i) { 16411c6fdbd8SKent Overstreet ret = -ENOMEM; 16421c6fdbd8SKent Overstreet goto out; 16431c6fdbd8SKent Overstreet } 16448de819f8SKent Overstreet len = min_t(unsigned, len, 16458de819f8SKent Overstreet nr_pages * PAGE_SIZE - offset); 16468de819f8SKent Overstreet break; 16478de819f8SKent Overstreet } 16481c6fdbd8SKent Overstreet } 16491c6fdbd8SKent Overstreet 16501c6fdbd8SKent Overstreet if (offset && !PageUptodate(pages[0])) { 16511c6fdbd8SKent Overstreet ret = bch2_read_single_page(pages[0], mapping); 16521c6fdbd8SKent Overstreet if (ret) 16531c6fdbd8SKent Overstreet goto out; 16541c6fdbd8SKent Overstreet } 16551c6fdbd8SKent Overstreet 16561c6fdbd8SKent Overstreet if ((pos + len) & (PAGE_SIZE - 1) && 16571c6fdbd8SKent Overstreet !PageUptodate(pages[nr_pages - 1])) { 16581c6fdbd8SKent Overstreet if ((index + nr_pages - 1) << PAGE_SHIFT >= inode->v.i_size) { 16591c6fdbd8SKent Overstreet zero_user(pages[nr_pages - 1], 0, PAGE_SIZE); 16601c6fdbd8SKent Overstreet } else { 16611c6fdbd8SKent Overstreet ret = bch2_read_single_page(pages[nr_pages - 1], mapping); 16621c6fdbd8SKent Overstreet if (ret) 16631c6fdbd8SKent Overstreet goto out; 16641c6fdbd8SKent Overstreet } 16651c6fdbd8SKent Overstreet } 16661c6fdbd8SKent Overstreet 1667d1542e03SKent Overstreet while (reserved < len) { 1668e6ec361fSKent Overstreet unsigned i = (offset + reserved) >> PAGE_SHIFT; 1669e6ec361fSKent Overstreet struct page *page = pages[i]; 1670d1542e03SKent Overstreet unsigned pg_offset = (offset + reserved) & (PAGE_SIZE - 1); 1671d1542e03SKent Overstreet unsigned pg_len = min_t(unsigned, len - reserved, 1672d1542e03SKent Overstreet PAGE_SIZE - pg_offset); 16731c6fdbd8SKent Overstreet 1674e6ec361fSKent Overstreet if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) { 1675e6ec361fSKent Overstreet ret = bch2_page_state_set(c, inode_inum(inode), 1676e6ec361fSKent Overstreet pages + i, nr_pages - i); 1677e6ec361fSKent Overstreet if (ret) 1678e6ec361fSKent Overstreet goto out; 16791c6fdbd8SKent Overstreet } 16801c6fdbd8SKent Overstreet 1681353448f3SKent Overstreet /* 1682353448f3SKent Overstreet * XXX: per POSIX and fstests generic/275, on -ENOSPC we're 1683353448f3SKent Overstreet * supposed to write as much as we have disk space for. 1684353448f3SKent Overstreet * 1685353448f3SKent Overstreet * On failure here we should still write out a partial page if 1686353448f3SKent Overstreet * we aren't completely out of disk space - we don't do that 1687353448f3SKent Overstreet * yet: 1688353448f3SKent Overstreet */ 1689e6ec361fSKent Overstreet ret = bch2_page_reservation_get(c, inode, page, &res, 1690bd954215SKent Overstreet pg_offset, pg_len); 1691353448f3SKent Overstreet if (unlikely(ret)) { 1692353448f3SKent Overstreet if (!reserved) 16931c6fdbd8SKent Overstreet goto out; 1694353448f3SKent Overstreet break; 1695353448f3SKent Overstreet } 1696d1542e03SKent Overstreet 1697d1542e03SKent Overstreet reserved += pg_len; 16981c6fdbd8SKent Overstreet } 16991c6fdbd8SKent Overstreet 17001c6fdbd8SKent Overstreet if (mapping_writably_mapped(mapping)) 17011c6fdbd8SKent Overstreet for (i = 0; i < nr_pages; i++) 17021c6fdbd8SKent Overstreet flush_dcache_page(pages[i]); 17031c6fdbd8SKent Overstreet 1704353448f3SKent Overstreet while (copied < reserved) { 17051c6fdbd8SKent Overstreet struct page *page = pages[(offset + copied) >> PAGE_SHIFT]; 17061c6fdbd8SKent Overstreet unsigned pg_offset = (offset + copied) & (PAGE_SIZE - 1); 1707353448f3SKent Overstreet unsigned pg_len = min_t(unsigned, reserved - copied, 17081c6fdbd8SKent Overstreet PAGE_SIZE - pg_offset); 17091c6fdbd8SKent Overstreet unsigned pg_copied = copy_page_from_iter_atomic(page, 1710d1542e03SKent Overstreet pg_offset, pg_len, iter); 1711d1542e03SKent Overstreet 1712d1542e03SKent Overstreet if (!pg_copied) 1713d1542e03SKent Overstreet break; 17141c6fdbd8SKent Overstreet 1715912bdf17SKent Overstreet if (!PageUptodate(page) && 1716912bdf17SKent Overstreet pg_copied != PAGE_SIZE && 1717912bdf17SKent Overstreet pos + copied + pg_copied < inode->v.i_size) { 1718912bdf17SKent Overstreet zero_user(page, 0, PAGE_SIZE); 1719912bdf17SKent Overstreet break; 1720912bdf17SKent Overstreet } 1721912bdf17SKent Overstreet 17221c6fdbd8SKent Overstreet flush_dcache_page(page); 17231c6fdbd8SKent Overstreet copied += pg_copied; 1724912bdf17SKent Overstreet 1725912bdf17SKent Overstreet if (pg_copied != pg_len) 1726912bdf17SKent Overstreet break; 17271c6fdbd8SKent Overstreet } 17281c6fdbd8SKent Overstreet 17291c6fdbd8SKent Overstreet if (!copied) 17301c6fdbd8SKent Overstreet goto out; 17311c6fdbd8SKent Overstreet 1732877dfb34SKent Overstreet spin_lock(&inode->v.i_lock); 1733877dfb34SKent Overstreet if (pos + copied > inode->v.i_size) 1734877dfb34SKent Overstreet i_size_write(&inode->v, pos + copied); 1735877dfb34SKent Overstreet spin_unlock(&inode->v.i_lock); 1736877dfb34SKent Overstreet 1737d1542e03SKent Overstreet while (set_dirty < copied) { 1738d1542e03SKent Overstreet struct page *page = pages[(offset + set_dirty) >> PAGE_SHIFT]; 1739d1542e03SKent Overstreet unsigned pg_offset = (offset + set_dirty) & (PAGE_SIZE - 1); 1740d1542e03SKent Overstreet unsigned pg_len = min_t(unsigned, copied - set_dirty, 1741d1542e03SKent Overstreet PAGE_SIZE - pg_offset); 1742d1542e03SKent Overstreet 1743d1542e03SKent Overstreet if (!PageUptodate(page)) 1744d1542e03SKent Overstreet SetPageUptodate(page); 1745d1542e03SKent Overstreet 1746d1542e03SKent Overstreet bch2_set_page_dirty(c, inode, page, &res, pg_offset, pg_len); 1747d1542e03SKent Overstreet unlock_page(page); 1748d1542e03SKent Overstreet put_page(page); 1749d1542e03SKent Overstreet 1750d1542e03SKent Overstreet set_dirty += pg_len; 1751d1542e03SKent Overstreet } 1752877dfb34SKent Overstreet 1753877dfb34SKent Overstreet nr_pages_copied = DIV_ROUND_UP(offset + copied, PAGE_SIZE); 1754877dfb34SKent Overstreet inode->ei_last_dirtied = (unsigned long) current; 17551c6fdbd8SKent Overstreet out: 1756d1542e03SKent Overstreet for (i = nr_pages_copied; i < nr_pages; i++) { 17571c6fdbd8SKent Overstreet unlock_page(pages[i]); 17581c6fdbd8SKent Overstreet put_page(pages[i]); 17591c6fdbd8SKent Overstreet } 17601c6fdbd8SKent Overstreet 1761d1542e03SKent Overstreet bch2_page_reservation_put(c, inode, &res); 17621c6fdbd8SKent Overstreet 17631c6fdbd8SKent Overstreet return copied ?: ret; 17641c6fdbd8SKent Overstreet } 17651c6fdbd8SKent Overstreet 17661c6fdbd8SKent Overstreet static ssize_t bch2_buffered_write(struct kiocb *iocb, struct iov_iter *iter) 17671c6fdbd8SKent Overstreet { 17681c6fdbd8SKent Overstreet struct file *file = iocb->ki_filp; 17691c6fdbd8SKent Overstreet struct address_space *mapping = file->f_mapping; 17701c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 17711c6fdbd8SKent Overstreet loff_t pos = iocb->ki_pos; 17721c6fdbd8SKent Overstreet ssize_t written = 0; 17731c6fdbd8SKent Overstreet int ret = 0; 17741c6fdbd8SKent Overstreet 1775a7ecd30cSKent Overstreet bch2_pagecache_add_get(inode); 17761c6fdbd8SKent Overstreet 17771c6fdbd8SKent Overstreet do { 17781c6fdbd8SKent Overstreet unsigned offset = pos & (PAGE_SIZE - 1); 17791c6fdbd8SKent Overstreet unsigned bytes = min_t(unsigned long, iov_iter_count(iter), 17801c6fdbd8SKent Overstreet PAGE_SIZE * WRITE_BATCH_PAGES - offset); 17811c6fdbd8SKent Overstreet again: 17821c6fdbd8SKent Overstreet /* 17831c6fdbd8SKent Overstreet * Bring in the user page that we will copy from _first_. 17841c6fdbd8SKent Overstreet * Otherwise there's a nasty deadlock on copying from the 17851c6fdbd8SKent Overstreet * same page as we're writing to, without it being marked 17861c6fdbd8SKent Overstreet * up-to-date. 17871c6fdbd8SKent Overstreet * 17881c6fdbd8SKent Overstreet * Not only is this an optimisation, but it is also required 17891c6fdbd8SKent Overstreet * to check that the address is actually valid, when atomic 17901c6fdbd8SKent Overstreet * usercopies are used, below. 17911c6fdbd8SKent Overstreet */ 17921c6fdbd8SKent Overstreet if (unlikely(fault_in_iov_iter_readable(iter, bytes))) { 17931c6fdbd8SKent Overstreet bytes = min_t(unsigned long, iov_iter_count(iter), 17941c6fdbd8SKent Overstreet PAGE_SIZE - offset); 17951c6fdbd8SKent Overstreet 17961c6fdbd8SKent Overstreet if (unlikely(fault_in_iov_iter_readable(iter, bytes))) { 17971c6fdbd8SKent Overstreet ret = -EFAULT; 17981c6fdbd8SKent Overstreet break; 17991c6fdbd8SKent Overstreet } 18001c6fdbd8SKent Overstreet } 18011c6fdbd8SKent Overstreet 18021c6fdbd8SKent Overstreet if (unlikely(fatal_signal_pending(current))) { 18031c6fdbd8SKent Overstreet ret = -EINTR; 18041c6fdbd8SKent Overstreet break; 18051c6fdbd8SKent Overstreet } 18061c6fdbd8SKent Overstreet 18071c6fdbd8SKent Overstreet ret = __bch2_buffered_write(inode, mapping, iter, pos, bytes); 18081c6fdbd8SKent Overstreet if (unlikely(ret < 0)) 18091c6fdbd8SKent Overstreet break; 18101c6fdbd8SKent Overstreet 18111c6fdbd8SKent Overstreet cond_resched(); 18121c6fdbd8SKent Overstreet 18131c6fdbd8SKent Overstreet if (unlikely(ret == 0)) { 18141c6fdbd8SKent Overstreet /* 18151c6fdbd8SKent Overstreet * If we were unable to copy any data at all, we must 18161c6fdbd8SKent Overstreet * fall back to a single segment length write. 18171c6fdbd8SKent Overstreet * 18181c6fdbd8SKent Overstreet * If we didn't fallback here, we could livelock 18191c6fdbd8SKent Overstreet * because not all segments in the iov can be copied at 18201c6fdbd8SKent Overstreet * once without a pagefault. 18211c6fdbd8SKent Overstreet */ 18221c6fdbd8SKent Overstreet bytes = min_t(unsigned long, PAGE_SIZE - offset, 18231c6fdbd8SKent Overstreet iov_iter_single_seg_count(iter)); 18241c6fdbd8SKent Overstreet goto again; 18251c6fdbd8SKent Overstreet } 18261c6fdbd8SKent Overstreet pos += ret; 18271c6fdbd8SKent Overstreet written += ret; 1828912bdf17SKent Overstreet ret = 0; 18291c6fdbd8SKent Overstreet 18301c6fdbd8SKent Overstreet balance_dirty_pages_ratelimited(mapping); 18311c6fdbd8SKent Overstreet } while (iov_iter_count(iter)); 18321c6fdbd8SKent Overstreet 1833a7ecd30cSKent Overstreet bch2_pagecache_add_put(inode); 18341c6fdbd8SKent Overstreet 18351c6fdbd8SKent Overstreet return written ? written : ret; 18361c6fdbd8SKent Overstreet } 18371c6fdbd8SKent Overstreet 18381c6fdbd8SKent Overstreet /* O_DIRECT reads */ 18391c6fdbd8SKent Overstreet 1840b4725cc1SKent Overstreet static void bio_check_or_release(struct bio *bio, bool check_dirty) 1841b4725cc1SKent Overstreet { 1842b4725cc1SKent Overstreet if (check_dirty) { 1843b4725cc1SKent Overstreet bio_check_pages_dirty(bio); 1844b4725cc1SKent Overstreet } else { 1845b4725cc1SKent Overstreet bio_release_pages(bio, false); 1846b4725cc1SKent Overstreet bio_put(bio); 1847b4725cc1SKent Overstreet } 1848b4725cc1SKent Overstreet } 1849b4725cc1SKent Overstreet 18501c6fdbd8SKent Overstreet static void bch2_dio_read_complete(struct closure *cl) 18511c6fdbd8SKent Overstreet { 18521c6fdbd8SKent Overstreet struct dio_read *dio = container_of(cl, struct dio_read, cl); 18531c6fdbd8SKent Overstreet 18541c6fdbd8SKent Overstreet dio->req->ki_complete(dio->req, dio->ret); 1855b4725cc1SKent Overstreet bio_check_or_release(&dio->rbio.bio, dio->should_dirty); 18561c6fdbd8SKent Overstreet } 18571c6fdbd8SKent Overstreet 18581c6fdbd8SKent Overstreet static void bch2_direct_IO_read_endio(struct bio *bio) 18591c6fdbd8SKent Overstreet { 18601c6fdbd8SKent Overstreet struct dio_read *dio = bio->bi_private; 18611c6fdbd8SKent Overstreet 18621c6fdbd8SKent Overstreet if (bio->bi_status) 18631c6fdbd8SKent Overstreet dio->ret = blk_status_to_errno(bio->bi_status); 18641c6fdbd8SKent Overstreet 18651c6fdbd8SKent Overstreet closure_put(&dio->cl); 18661c6fdbd8SKent Overstreet } 18671c6fdbd8SKent Overstreet 18681c6fdbd8SKent Overstreet static void bch2_direct_IO_read_split_endio(struct bio *bio) 18691c6fdbd8SKent Overstreet { 1870b4725cc1SKent Overstreet struct dio_read *dio = bio->bi_private; 1871b4725cc1SKent Overstreet bool should_dirty = dio->should_dirty; 1872b4725cc1SKent Overstreet 18731c6fdbd8SKent Overstreet bch2_direct_IO_read_endio(bio); 1874b4725cc1SKent Overstreet bio_check_or_release(bio, should_dirty); 18751c6fdbd8SKent Overstreet } 18761c6fdbd8SKent Overstreet 18771c6fdbd8SKent Overstreet static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter) 18781c6fdbd8SKent Overstreet { 18791c6fdbd8SKent Overstreet struct file *file = req->ki_filp; 18801c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 18811c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 18829a3df993SKent Overstreet struct bch_io_opts opts = io_opts(c, &inode->ei_inode); 18831c6fdbd8SKent Overstreet struct dio_read *dio; 18841c6fdbd8SKent Overstreet struct bio *bio; 18851c6fdbd8SKent Overstreet loff_t offset = req->ki_pos; 18861c6fdbd8SKent Overstreet bool sync = is_sync_kiocb(req); 18871c6fdbd8SKent Overstreet size_t shorten; 18881c6fdbd8SKent Overstreet ssize_t ret; 18891c6fdbd8SKent Overstreet 18901c6fdbd8SKent Overstreet if ((offset|iter->count) & (block_bytes(c) - 1)) 18911c6fdbd8SKent Overstreet return -EINVAL; 18921c6fdbd8SKent Overstreet 18931c6fdbd8SKent Overstreet ret = min_t(loff_t, iter->count, 18941c6fdbd8SKent Overstreet max_t(loff_t, 0, i_size_read(&inode->v) - offset)); 18951c6fdbd8SKent Overstreet 18961c6fdbd8SKent Overstreet if (!ret) 18971c6fdbd8SKent Overstreet return ret; 18981c6fdbd8SKent Overstreet 18991c6fdbd8SKent Overstreet shorten = iov_iter_count(iter) - round_up(ret, block_bytes(c)); 19001c6fdbd8SKent Overstreet iter->count -= shorten; 19011c6fdbd8SKent Overstreet 19021c6fdbd8SKent Overstreet bio = bio_alloc_bioset(NULL, 19034d126dc8SKent Overstreet bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS), 19041c6fdbd8SKent Overstreet REQ_OP_READ, 19051c6fdbd8SKent Overstreet GFP_KERNEL, 19061c6fdbd8SKent Overstreet &c->dio_read_bioset); 19071c6fdbd8SKent Overstreet 19081c6fdbd8SKent Overstreet bio->bi_end_io = bch2_direct_IO_read_endio; 19091c6fdbd8SKent Overstreet 19101c6fdbd8SKent Overstreet dio = container_of(bio, struct dio_read, rbio.bio); 19111c6fdbd8SKent Overstreet closure_init(&dio->cl, NULL); 19121c6fdbd8SKent Overstreet 19131c6fdbd8SKent Overstreet /* 19141c6fdbd8SKent Overstreet * this is a _really_ horrible hack just to avoid an atomic sub at the 19151c6fdbd8SKent Overstreet * end: 19161c6fdbd8SKent Overstreet */ 19171c6fdbd8SKent Overstreet if (!sync) { 19181c6fdbd8SKent Overstreet set_closure_fn(&dio->cl, bch2_dio_read_complete, NULL); 19191c6fdbd8SKent Overstreet atomic_set(&dio->cl.remaining, 19201c6fdbd8SKent Overstreet CLOSURE_REMAINING_INITIALIZER - 19211c6fdbd8SKent Overstreet CLOSURE_RUNNING + 19221c6fdbd8SKent Overstreet CLOSURE_DESTRUCTOR); 19231c6fdbd8SKent Overstreet } else { 19241c6fdbd8SKent Overstreet atomic_set(&dio->cl.remaining, 19251c6fdbd8SKent Overstreet CLOSURE_REMAINING_INITIALIZER + 1); 19261c6fdbd8SKent Overstreet } 19271c6fdbd8SKent Overstreet 19281c6fdbd8SKent Overstreet dio->req = req; 19291c6fdbd8SKent Overstreet dio->ret = ret; 1930b4725cc1SKent Overstreet /* 1931b4725cc1SKent Overstreet * This is one of the sketchier things I've encountered: we have to skip 1932b4725cc1SKent Overstreet * the dirtying of requests that are internal from the kernel (i.e. from 1933b4725cc1SKent Overstreet * loopback), because we'll deadlock on page_lock. 1934b4725cc1SKent Overstreet */ 1935b4725cc1SKent Overstreet dio->should_dirty = iter_is_iovec(iter); 19361c6fdbd8SKent Overstreet 19371c6fdbd8SKent Overstreet goto start; 19381c6fdbd8SKent Overstreet while (iter->count) { 19391c6fdbd8SKent Overstreet bio = bio_alloc_bioset(NULL, 19404d126dc8SKent Overstreet bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS), 19411c6fdbd8SKent Overstreet REQ_OP_READ, 19421c6fdbd8SKent Overstreet GFP_KERNEL, 19431c6fdbd8SKent Overstreet &c->bio_read); 19441c6fdbd8SKent Overstreet bio->bi_end_io = bch2_direct_IO_read_split_endio; 19451c6fdbd8SKent Overstreet start: 19461c6fdbd8SKent Overstreet bio->bi_opf = REQ_OP_READ|REQ_SYNC; 19471c6fdbd8SKent Overstreet bio->bi_iter.bi_sector = offset >> 9; 19481c6fdbd8SKent Overstreet bio->bi_private = dio; 19491c6fdbd8SKent Overstreet 19501c6fdbd8SKent Overstreet ret = bio_iov_iter_get_pages(bio, iter); 19511c6fdbd8SKent Overstreet if (ret < 0) { 19521c6fdbd8SKent Overstreet /* XXX: fault inject this path */ 19531c6fdbd8SKent Overstreet bio->bi_status = BLK_STS_RESOURCE; 19541c6fdbd8SKent Overstreet bio_endio(bio); 19551c6fdbd8SKent Overstreet break; 19561c6fdbd8SKent Overstreet } 19571c6fdbd8SKent Overstreet 19581c6fdbd8SKent Overstreet offset += bio->bi_iter.bi_size; 1959b4725cc1SKent Overstreet 1960b4725cc1SKent Overstreet if (dio->should_dirty) 19611c6fdbd8SKent Overstreet bio_set_pages_dirty(bio); 19621c6fdbd8SKent Overstreet 19631c6fdbd8SKent Overstreet if (iter->count) 19641c6fdbd8SKent Overstreet closure_get(&dio->cl); 19651c6fdbd8SKent Overstreet 19668c6d298aSKent Overstreet bch2_read(c, rbio_init(bio, opts), inode_inum(inode)); 19671c6fdbd8SKent Overstreet } 19681c6fdbd8SKent Overstreet 19691c6fdbd8SKent Overstreet iter->count += shorten; 19701c6fdbd8SKent Overstreet 19711c6fdbd8SKent Overstreet if (sync) { 19721c6fdbd8SKent Overstreet closure_sync(&dio->cl); 19731c6fdbd8SKent Overstreet closure_debug_destroy(&dio->cl); 19741c6fdbd8SKent Overstreet ret = dio->ret; 1975b4725cc1SKent Overstreet bio_check_or_release(&dio->rbio.bio, dio->should_dirty); 19761c6fdbd8SKent Overstreet return ret; 19771c6fdbd8SKent Overstreet } else { 19781c6fdbd8SKent Overstreet return -EIOCBQUEUED; 19791c6fdbd8SKent Overstreet } 19801c6fdbd8SKent Overstreet } 19811c6fdbd8SKent Overstreet 19821c6fdbd8SKent Overstreet ssize_t bch2_read_iter(struct kiocb *iocb, struct iov_iter *iter) 19831c6fdbd8SKent Overstreet { 19841c6fdbd8SKent Overstreet struct file *file = iocb->ki_filp; 19851c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 19861c6fdbd8SKent Overstreet struct address_space *mapping = file->f_mapping; 19871c6fdbd8SKent Overstreet size_t count = iov_iter_count(iter); 19881c6fdbd8SKent Overstreet ssize_t ret; 19891c6fdbd8SKent Overstreet 19901c6fdbd8SKent Overstreet if (!count) 19911c6fdbd8SKent Overstreet return 0; /* skip atime */ 19921c6fdbd8SKent Overstreet 19931c6fdbd8SKent Overstreet if (iocb->ki_flags & IOCB_DIRECT) { 19941c6fdbd8SKent Overstreet struct blk_plug plug; 19951c6fdbd8SKent Overstreet 1996a023127aSKent Overstreet if (unlikely(mapping->nrpages)) { 19971c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 19981c6fdbd8SKent Overstreet iocb->ki_pos, 19991c6fdbd8SKent Overstreet iocb->ki_pos + count - 1); 20001c6fdbd8SKent Overstreet if (ret < 0) 20015c1ef830SKent Overstreet goto out; 2002a023127aSKent Overstreet } 20031c6fdbd8SKent Overstreet 20041c6fdbd8SKent Overstreet file_accessed(file); 20051c6fdbd8SKent Overstreet 20061c6fdbd8SKent Overstreet blk_start_plug(&plug); 20071c6fdbd8SKent Overstreet ret = bch2_direct_IO_read(iocb, iter); 20081c6fdbd8SKent Overstreet blk_finish_plug(&plug); 20091c6fdbd8SKent Overstreet 20101c6fdbd8SKent Overstreet if (ret >= 0) 20111c6fdbd8SKent Overstreet iocb->ki_pos += ret; 20121c6fdbd8SKent Overstreet } else { 2013a7ecd30cSKent Overstreet bch2_pagecache_add_get(inode); 20141c6fdbd8SKent Overstreet ret = generic_file_read_iter(iocb, iter); 2015a7ecd30cSKent Overstreet bch2_pagecache_add_put(inode); 20161c6fdbd8SKent Overstreet } 20175c1ef830SKent Overstreet out: 20185c1ef830SKent Overstreet return bch2_err_class(ret); 20191c6fdbd8SKent Overstreet } 20201c6fdbd8SKent Overstreet 20211c6fdbd8SKent Overstreet /* O_DIRECT writes */ 20221c6fdbd8SKent Overstreet 20236fed42bbSKent Overstreet static bool bch2_check_range_allocated(struct bch_fs *c, subvol_inum inum, 20246fed42bbSKent Overstreet u64 offset, u64 size, 20256fed42bbSKent Overstreet unsigned nr_replicas, bool compressed) 20266fed42bbSKent Overstreet { 20276fed42bbSKent Overstreet struct btree_trans trans; 20286fed42bbSKent Overstreet struct btree_iter iter; 20296fed42bbSKent Overstreet struct bkey_s_c k; 20306fed42bbSKent Overstreet u64 end = offset + size; 20316fed42bbSKent Overstreet u32 snapshot; 20326fed42bbSKent Overstreet bool ret = true; 20336fed42bbSKent Overstreet int err; 20346fed42bbSKent Overstreet 20356fed42bbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 20366fed42bbSKent Overstreet retry: 20376fed42bbSKent Overstreet bch2_trans_begin(&trans); 20386fed42bbSKent Overstreet 20396fed42bbSKent Overstreet err = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot); 20406fed42bbSKent Overstreet if (err) 20416fed42bbSKent Overstreet goto err; 20426fed42bbSKent Overstreet 2043e5fa91d7SKent Overstreet for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents, 20446fed42bbSKent Overstreet SPOS(inum.inum, offset, snapshot), 20456fed42bbSKent Overstreet BTREE_ITER_SLOTS, k, err) { 2046*e88a75ebSKent Overstreet if (bkey_ge(bkey_start_pos(k.k), POS(inum.inum, end))) 20476fed42bbSKent Overstreet break; 20486fed42bbSKent Overstreet 20498c6d298aSKent Overstreet if (k.k->p.snapshot != snapshot || 20508c6d298aSKent Overstreet nr_replicas > bch2_bkey_replicas(c, k) || 20516fed42bbSKent Overstreet (!compressed && bch2_bkey_sectors_compressed(k))) { 20526fed42bbSKent Overstreet ret = false; 20536fed42bbSKent Overstreet break; 20546fed42bbSKent Overstreet } 20556fed42bbSKent Overstreet } 20566fed42bbSKent Overstreet 20576fed42bbSKent Overstreet offset = iter.pos.offset; 20586fed42bbSKent Overstreet bch2_trans_iter_exit(&trans, &iter); 20596fed42bbSKent Overstreet err: 2060549d173cSKent Overstreet if (bch2_err_matches(err, BCH_ERR_transaction_restart)) 20616fed42bbSKent Overstreet goto retry; 20626fed42bbSKent Overstreet bch2_trans_exit(&trans); 20636fed42bbSKent Overstreet 20646fed42bbSKent Overstreet return err ? false : ret; 20656fed42bbSKent Overstreet } 20666fed42bbSKent Overstreet 2067182c7bbfSKent Overstreet static noinline bool bch2_dio_write_check_allocated(struct dio_write *dio) 2068182c7bbfSKent Overstreet { 2069182c7bbfSKent Overstreet struct bch_fs *c = dio->op.c; 2070182c7bbfSKent Overstreet struct bch_inode_info *inode = dio->inode; 2071182c7bbfSKent Overstreet struct bio *bio = &dio->op.wbio.bio; 2072182c7bbfSKent Overstreet 2073182c7bbfSKent Overstreet return bch2_check_range_allocated(c, inode_inum(inode), 2074182c7bbfSKent Overstreet dio->op.pos.offset, bio_sectors(bio), 2075182c7bbfSKent Overstreet dio->op.opts.data_replicas, 2076182c7bbfSKent Overstreet dio->op.opts.compression != 0); 2077182c7bbfSKent Overstreet } 2078182c7bbfSKent Overstreet 2079a1ee777bSKent Overstreet static void bch2_dio_write_loop_async(struct bch_write_op *); 2080a1ee777bSKent Overstreet static __always_inline long bch2_dio_write_done(struct dio_write *dio); 2081a1ee777bSKent Overstreet 20821c6fdbd8SKent Overstreet /* 20831c6fdbd8SKent Overstreet * We're going to return -EIOCBQUEUED, but we haven't finished consuming the 20841c6fdbd8SKent Overstreet * iov_iter yet, so we need to stash a copy of the iovec: it might be on the 20851c6fdbd8SKent Overstreet * caller's stack, we're not guaranteed that it will live for the duration of 20861c6fdbd8SKent Overstreet * the IO: 20871c6fdbd8SKent Overstreet */ 20881c6fdbd8SKent Overstreet static noinline int bch2_dio_write_copy_iov(struct dio_write *dio) 20891c6fdbd8SKent Overstreet { 20901c6fdbd8SKent Overstreet struct iovec *iov = dio->inline_vecs; 20911c6fdbd8SKent Overstreet 20921c6fdbd8SKent Overstreet /* 20931c6fdbd8SKent Overstreet * iov_iter has a single embedded iovec - nothing to do: 20941c6fdbd8SKent Overstreet */ 20951c6fdbd8SKent Overstreet if (iter_is_ubuf(&dio->iter)) 20961c6fdbd8SKent Overstreet return 0; 20971c6fdbd8SKent Overstreet 20981c6fdbd8SKent Overstreet /* 20991c6fdbd8SKent Overstreet * We don't currently handle non-iovec iov_iters here - return an error, 21001c6fdbd8SKent Overstreet * and we'll fall back to doing the IO synchronously: 21011c6fdbd8SKent Overstreet */ 21021c6fdbd8SKent Overstreet if (!iter_is_iovec(&dio->iter)) 21031c6fdbd8SKent Overstreet return -1; 21041c6fdbd8SKent Overstreet 21051c6fdbd8SKent Overstreet if (dio->iter.nr_segs > ARRAY_SIZE(dio->inline_vecs)) { 21061c6fdbd8SKent Overstreet iov = kmalloc_array(dio->iter.nr_segs, sizeof(*iov), 21071c6fdbd8SKent Overstreet GFP_KERNEL); 21081c6fdbd8SKent Overstreet if (unlikely(!iov)) 21091c6fdbd8SKent Overstreet return -ENOMEM; 21101c6fdbd8SKent Overstreet 21111c6fdbd8SKent Overstreet dio->free_iov = true; 21121c6fdbd8SKent Overstreet } 21131c6fdbd8SKent Overstreet 21141c6fdbd8SKent Overstreet memcpy(iov, dio->iter.__iov, dio->iter.nr_segs * sizeof(*iov)); 21151c6fdbd8SKent Overstreet dio->iter.__iov = iov; 21161c6fdbd8SKent Overstreet return 0; 21171c6fdbd8SKent Overstreet } 21181c6fdbd8SKent Overstreet 2119a1ee777bSKent Overstreet static void bch2_dio_write_flush_done(struct closure *cl) 2120a1ee777bSKent Overstreet { 2121a1ee777bSKent Overstreet struct dio_write *dio = container_of(cl, struct dio_write, op.cl); 2122a1ee777bSKent Overstreet struct bch_fs *c = dio->op.c; 2123a1ee777bSKent Overstreet 2124a1ee777bSKent Overstreet closure_debug_destroy(cl); 2125a1ee777bSKent Overstreet 2126a1ee777bSKent Overstreet dio->op.error = bch2_journal_error(&c->journal); 2127a1ee777bSKent Overstreet 2128a1ee777bSKent Overstreet bch2_dio_write_done(dio); 2129a1ee777bSKent Overstreet } 2130a1ee777bSKent Overstreet 2131a1ee777bSKent Overstreet static noinline void bch2_dio_write_flush(struct dio_write *dio) 2132a1ee777bSKent Overstreet { 2133a1ee777bSKent Overstreet struct bch_fs *c = dio->op.c; 2134a1ee777bSKent Overstreet struct bch_inode_unpacked inode; 2135a1ee777bSKent Overstreet int ret; 2136a1ee777bSKent Overstreet 2137a1ee777bSKent Overstreet dio->flush = 0; 2138a1ee777bSKent Overstreet 2139a1ee777bSKent Overstreet closure_init(&dio->op.cl, NULL); 2140a1ee777bSKent Overstreet 2141a1ee777bSKent Overstreet if (!dio->op.error) { 2142a1ee777bSKent Overstreet ret = bch2_inode_find_by_inum(c, inode_inum(dio->inode), &inode); 2143a1ee777bSKent Overstreet if (ret) 2144a1ee777bSKent Overstreet dio->op.error = ret; 2145a1ee777bSKent Overstreet else 2146a1ee777bSKent Overstreet bch2_journal_flush_seq_async(&c->journal, inode.bi_journal_seq, &dio->op.cl); 2147a1ee777bSKent Overstreet } 2148a1ee777bSKent Overstreet 2149a1ee777bSKent Overstreet if (dio->sync) { 2150a1ee777bSKent Overstreet closure_sync(&dio->op.cl); 2151a1ee777bSKent Overstreet closure_debug_destroy(&dio->op.cl); 2152a1ee777bSKent Overstreet } else { 2153a1ee777bSKent Overstreet continue_at(&dio->op.cl, bch2_dio_write_flush_done, NULL); 2154a1ee777bSKent Overstreet } 2155a1ee777bSKent Overstreet } 2156042a1f26SKent Overstreet 2157182c7bbfSKent Overstreet static __always_inline long bch2_dio_write_done(struct dio_write *dio) 2158182c7bbfSKent Overstreet { 2159182c7bbfSKent Overstreet struct kiocb *req = dio->req; 2160182c7bbfSKent Overstreet struct bch_inode_info *inode = dio->inode; 2161182c7bbfSKent Overstreet bool sync = dio->sync; 2162a1ee777bSKent Overstreet long ret; 2163a1ee777bSKent Overstreet 2164a1ee777bSKent Overstreet if (unlikely(dio->flush)) { 2165a1ee777bSKent Overstreet bch2_dio_write_flush(dio); 2166a1ee777bSKent Overstreet if (!sync) 2167a1ee777bSKent Overstreet return -EIOCBQUEUED; 2168a1ee777bSKent Overstreet } 2169182c7bbfSKent Overstreet 2170a7ecd30cSKent Overstreet bch2_pagecache_block_put(inode); 2171182c7bbfSKent Overstreet 2172182c7bbfSKent Overstreet if (dio->free_iov) 2173182c7bbfSKent Overstreet kfree(dio->iter.__iov); 2174a1ee777bSKent Overstreet 2175a1ee777bSKent Overstreet ret = dio->op.error ?: ((long) dio->written << 9); 2176182c7bbfSKent Overstreet bio_put(&dio->op.wbio.bio); 2177182c7bbfSKent Overstreet 2178182c7bbfSKent Overstreet /* inode->i_dio_count is our ref on inode and thus bch_fs */ 2179182c7bbfSKent Overstreet inode_dio_end(&inode->v); 2180182c7bbfSKent Overstreet 2181182c7bbfSKent Overstreet if (ret < 0) 2182182c7bbfSKent Overstreet ret = bch2_err_class(ret); 2183182c7bbfSKent Overstreet 2184182c7bbfSKent Overstreet if (!sync) { 2185182c7bbfSKent Overstreet req->ki_complete(req, ret); 2186182c7bbfSKent Overstreet ret = -EIOCBQUEUED; 2187182c7bbfSKent Overstreet } 2188182c7bbfSKent Overstreet return ret; 2189182c7bbfSKent Overstreet } 2190182c7bbfSKent Overstreet 2191182c7bbfSKent Overstreet static __always_inline void bch2_dio_write_end(struct dio_write *dio) 2192182c7bbfSKent Overstreet { 2193182c7bbfSKent Overstreet struct bch_fs *c = dio->op.c; 2194182c7bbfSKent Overstreet struct kiocb *req = dio->req; 2195182c7bbfSKent Overstreet struct bch_inode_info *inode = dio->inode; 2196182c7bbfSKent Overstreet struct bio *bio = &dio->op.wbio.bio; 2197182c7bbfSKent Overstreet 2198182c7bbfSKent Overstreet req->ki_pos += (u64) dio->op.written << 9; 2199182c7bbfSKent Overstreet dio->written += dio->op.written; 2200182c7bbfSKent Overstreet 22016b1b186aSKent Overstreet if (dio->extending) { 2202182c7bbfSKent Overstreet spin_lock(&inode->v.i_lock); 2203182c7bbfSKent Overstreet if (req->ki_pos > inode->v.i_size) 2204182c7bbfSKent Overstreet i_size_write(&inode->v, req->ki_pos); 2205182c7bbfSKent Overstreet spin_unlock(&inode->v.i_lock); 22066b1b186aSKent Overstreet } 22076b1b186aSKent Overstreet 22086b1b186aSKent Overstreet if (dio->op.i_sectors_delta || dio->quota_res.sectors) { 22096b1b186aSKent Overstreet mutex_lock(&inode->ei_quota_lock); 22106b1b186aSKent Overstreet __i_sectors_acct(c, inode, &dio->quota_res, dio->op.i_sectors_delta); 22116b1b186aSKent Overstreet __bch2_quota_reservation_put(c, inode, &dio->quota_res); 22126b1b186aSKent Overstreet mutex_unlock(&inode->ei_quota_lock); 22136b1b186aSKent Overstreet } 2214182c7bbfSKent Overstreet 2215182c7bbfSKent Overstreet bio_release_pages(bio, false); 2216182c7bbfSKent Overstreet 2217182c7bbfSKent Overstreet if (unlikely(dio->op.error)) 2218182c7bbfSKent Overstreet set_bit(EI_INODE_ERROR, &inode->ei_flags); 2219182c7bbfSKent Overstreet } 2220182c7bbfSKent Overstreet 22214d868d18SKent Overstreet static __always_inline long bch2_dio_write_loop(struct dio_write *dio) 22221c6fdbd8SKent Overstreet { 2223182c7bbfSKent Overstreet struct bch_fs *c = dio->op.c; 22241c6fdbd8SKent Overstreet struct kiocb *req = dio->req; 2225182c7bbfSKent Overstreet struct address_space *mapping = dio->mapping; 2226182c7bbfSKent Overstreet struct bch_inode_info *inode = dio->inode; 22279a3df993SKent Overstreet struct bio *bio = &dio->op.wbio.bio; 2228eb8e6e9cSKent Overstreet unsigned unaligned, iter_count; 2229eb8e6e9cSKent Overstreet bool sync = dio->sync, dropped_locks; 22301c6fdbd8SKent Overstreet long ret; 22311c6fdbd8SKent Overstreet 22321c6fdbd8SKent Overstreet while (1) { 2233eb8e6e9cSKent Overstreet iter_count = dio->iter.count; 2234eb8e6e9cSKent Overstreet 2235182c7bbfSKent Overstreet EBUG_ON(current->faults_disabled_mapping); 22361c6fdbd8SKent Overstreet current->faults_disabled_mapping = mapping; 22371c6fdbd8SKent Overstreet 22381c6fdbd8SKent Overstreet ret = bio_iov_iter_get_pages(bio, &dio->iter); 22391c6fdbd8SKent Overstreet 2240eb8e6e9cSKent Overstreet dropped_locks = fdm_dropped_locks(); 2241eb8e6e9cSKent Overstreet 22421c6fdbd8SKent Overstreet current->faults_disabled_mapping = NULL; 22431c6fdbd8SKent Overstreet 2244eb8e6e9cSKent Overstreet /* 2245eb8e6e9cSKent Overstreet * If the fault handler returned an error but also signalled 2246eb8e6e9cSKent Overstreet * that it dropped & retook ei_pagecache_lock, we just need to 2247eb8e6e9cSKent Overstreet * re-shoot down the page cache and retry: 2248eb8e6e9cSKent Overstreet */ 2249eb8e6e9cSKent Overstreet if (dropped_locks && ret) 2250eb8e6e9cSKent Overstreet ret = 0; 2251eb8e6e9cSKent Overstreet 22521c6fdbd8SKent Overstreet if (unlikely(ret < 0)) 22531c6fdbd8SKent Overstreet goto err; 22541c6fdbd8SKent Overstreet 2255eb8e6e9cSKent Overstreet if (unlikely(dropped_locks)) { 2256eb8e6e9cSKent Overstreet ret = write_invalidate_inode_pages_range(mapping, 2257eb8e6e9cSKent Overstreet req->ki_pos, 2258eb8e6e9cSKent Overstreet req->ki_pos + iter_count - 1); 2259eb8e6e9cSKent Overstreet if (unlikely(ret)) 2260eb8e6e9cSKent Overstreet goto err; 2261eb8e6e9cSKent Overstreet 2262eb8e6e9cSKent Overstreet if (!bio->bi_iter.bi_size) 2263eb8e6e9cSKent Overstreet continue; 2264eb8e6e9cSKent Overstreet } 2265eb8e6e9cSKent Overstreet 22660a426c32SKent Overstreet unaligned = bio->bi_iter.bi_size & (block_bytes(c) - 1); 22670a426c32SKent Overstreet bio->bi_iter.bi_size -= unaligned; 22680a426c32SKent Overstreet iov_iter_revert(&dio->iter, unaligned); 22690a426c32SKent Overstreet 22700a426c32SKent Overstreet if (!bio->bi_iter.bi_size) { 22710a426c32SKent Overstreet /* 22720a426c32SKent Overstreet * bio_iov_iter_get_pages was only able to get < 22730a426c32SKent Overstreet * blocksize worth of pages: 22740a426c32SKent Overstreet */ 22750a426c32SKent Overstreet ret = -EFAULT; 22760a426c32SKent Overstreet goto err; 22770a426c32SKent Overstreet } 22780a426c32SKent Overstreet 2279042a1f26SKent Overstreet bch2_write_op_init(&dio->op, c, io_opts(c, &inode->ei_inode)); 2280182c7bbfSKent Overstreet dio->op.end_io = sync 2281182c7bbfSKent Overstreet ? NULL 2282182c7bbfSKent Overstreet : bch2_dio_write_loop_async; 2283042a1f26SKent Overstreet dio->op.target = dio->op.opts.foreground_target; 2284042a1f26SKent Overstreet dio->op.write_point = writepoint_hashed((unsigned long) current); 2285042a1f26SKent Overstreet dio->op.nr_replicas = dio->op.opts.data_replicas; 22868c6d298aSKent Overstreet dio->op.subvol = inode->ei_subvol; 2287042a1f26SKent Overstreet dio->op.pos = POS(inode->v.i_ino, (u64) req->ki_pos >> 9); 2288042a1f26SKent Overstreet 22891df3e199SKent Overstreet if (sync) 22901df3e199SKent Overstreet dio->op.flags |= BCH_WRITE_SYNC; 2291a6336910SKent Overstreet dio->op.flags |= BCH_WRITE_CHECK_ENOSPC; 2292042a1f26SKent Overstreet 22936b1b186aSKent Overstreet ret = bch2_quota_reservation_add(c, inode, &dio->quota_res, 22946b1b186aSKent Overstreet bio_sectors(bio), true); 22956b1b186aSKent Overstreet if (unlikely(ret)) 22966b1b186aSKent Overstreet goto err; 22976b1b186aSKent Overstreet 2298042a1f26SKent Overstreet ret = bch2_disk_reservation_get(c, &dio->op.res, bio_sectors(bio), 2299042a1f26SKent Overstreet dio->op.opts.data_replicas, 0); 2300042a1f26SKent Overstreet if (unlikely(ret) && 2301182c7bbfSKent Overstreet !bch2_dio_write_check_allocated(dio)) 2302042a1f26SKent Overstreet goto err; 23031c6fdbd8SKent Overstreet 23041c6fdbd8SKent Overstreet task_io_account_write(bio->bi_iter.bi_size); 23051c6fdbd8SKent Overstreet 2306182c7bbfSKent Overstreet if (unlikely(dio->iter.count) && 2307182c7bbfSKent Overstreet !dio->sync && 2308182c7bbfSKent Overstreet !dio->loop && 2309182c7bbfSKent Overstreet bch2_dio_write_copy_iov(dio)) 2310286d8ad0SKent Overstreet dio->sync = sync = true; 2311182c7bbfSKent Overstreet 23121c6fdbd8SKent Overstreet dio->loop = true; 2313f8f30863SKent Overstreet closure_call(&dio->op.cl, bch2_write, NULL, NULL); 23141c6fdbd8SKent Overstreet 2315182c7bbfSKent Overstreet if (!sync) 23161c6fdbd8SKent Overstreet return -EIOCBQUEUED; 23179a3df993SKent Overstreet 2318182c7bbfSKent Overstreet bch2_dio_write_end(dio); 23199a3df993SKent Overstreet 2320182c7bbfSKent Overstreet if (likely(!dio->iter.count) || dio->op.error) 23211c6fdbd8SKent Overstreet break; 2322f8f30863SKent Overstreet 23231c6fdbd8SKent Overstreet bio_reset(bio, NULL, REQ_OP_WRITE); 23241c6fdbd8SKent Overstreet } 2325182c7bbfSKent Overstreet out: 2326182c7bbfSKent Overstreet return bch2_dio_write_done(dio); 23271c6fdbd8SKent Overstreet err: 2328182c7bbfSKent Overstreet dio->op.error = ret; 23291c6fdbd8SKent Overstreet 23305468f119SKent Overstreet bio_release_pages(bio, false); 23316b1b186aSKent Overstreet 23326b1b186aSKent Overstreet bch2_quota_reservation_put(c, inode, &dio->quota_res); 2333182c7bbfSKent Overstreet goto out; 23341c6fdbd8SKent Overstreet } 23351c6fdbd8SKent Overstreet 23364d868d18SKent Overstreet static noinline __cold void bch2_dio_write_continue(struct dio_write *dio) 23371c6fdbd8SKent Overstreet { 2338182c7bbfSKent Overstreet struct mm_struct *mm = dio->mm; 23391c6fdbd8SKent Overstreet 2340182c7bbfSKent Overstreet bio_reset(&dio->op.wbio.bio, NULL, REQ_OP_WRITE); 2341182c7bbfSKent Overstreet 2342182c7bbfSKent Overstreet if (mm) 2343182c7bbfSKent Overstreet kthread_use_mm(mm); 23441c6fdbd8SKent Overstreet bch2_dio_write_loop(dio); 2345182c7bbfSKent Overstreet if (mm) 2346182c7bbfSKent Overstreet kthread_unuse_mm(mm); 23471c6fdbd8SKent Overstreet } 23481c6fdbd8SKent Overstreet 23494d868d18SKent Overstreet static void bch2_dio_write_loop_async(struct bch_write_op *op) 23504d868d18SKent Overstreet { 23514d868d18SKent Overstreet struct dio_write *dio = container_of(op, struct dio_write, op); 23524d868d18SKent Overstreet 23534d868d18SKent Overstreet bch2_dio_write_end(dio); 23544d868d18SKent Overstreet 23554d868d18SKent Overstreet if (likely(!dio->iter.count) || dio->op.error) 23564d868d18SKent Overstreet bch2_dio_write_done(dio); 23574d868d18SKent Overstreet else 23584d868d18SKent Overstreet bch2_dio_write_continue(dio); 23594d868d18SKent Overstreet } 23604d868d18SKent Overstreet 23611c6fdbd8SKent Overstreet static noinline 23621c6fdbd8SKent Overstreet ssize_t bch2_direct_write(struct kiocb *req, struct iov_iter *iter) 23631c6fdbd8SKent Overstreet { 23641c6fdbd8SKent Overstreet struct file *file = req->ki_filp; 236554847d25SKent Overstreet struct address_space *mapping = file->f_mapping; 23661c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 23671c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 23681c6fdbd8SKent Overstreet struct dio_write *dio; 23691c6fdbd8SKent Overstreet struct bio *bio; 23707edcfbfeSKent Overstreet bool locked = true, extending; 23711c6fdbd8SKent Overstreet ssize_t ret; 23721c6fdbd8SKent Overstreet 23737edcfbfeSKent Overstreet prefetch(&c->opts); 23747edcfbfeSKent Overstreet prefetch((void *) &c->opts + 64); 23757edcfbfeSKent Overstreet prefetch(&inode->ei_inode); 23767edcfbfeSKent Overstreet prefetch((void *) &inode->ei_inode + 64); 23771c6fdbd8SKent Overstreet 23787edcfbfeSKent Overstreet inode_lock(&inode->v); 23797edcfbfeSKent Overstreet 23807edcfbfeSKent Overstreet ret = generic_write_checks(req, iter); 23817edcfbfeSKent Overstreet if (unlikely(ret <= 0)) 23827edcfbfeSKent Overstreet goto err; 23837edcfbfeSKent Overstreet 23847edcfbfeSKent Overstreet ret = file_remove_privs(file); 23857edcfbfeSKent Overstreet if (unlikely(ret)) 23867edcfbfeSKent Overstreet goto err; 23877edcfbfeSKent Overstreet 23887edcfbfeSKent Overstreet ret = file_update_time(file); 23897edcfbfeSKent Overstreet if (unlikely(ret)) 23907edcfbfeSKent Overstreet goto err; 23911c6fdbd8SKent Overstreet 2392919dbbd1SKent Overstreet if (unlikely((req->ki_pos|iter->count) & (block_bytes(c) - 1))) 23937edcfbfeSKent Overstreet goto err; 23947edcfbfeSKent Overstreet 23957edcfbfeSKent Overstreet inode_dio_begin(&inode->v); 2396a7ecd30cSKent Overstreet bch2_pagecache_block_get(inode); 23977edcfbfeSKent Overstreet 23987edcfbfeSKent Overstreet extending = req->ki_pos + iter->count > inode->v.i_size; 23997edcfbfeSKent Overstreet if (!extending) { 24007edcfbfeSKent Overstreet inode_unlock(&inode->v); 24017edcfbfeSKent Overstreet locked = false; 24027edcfbfeSKent Overstreet } 24031c6fdbd8SKent Overstreet 24041c6fdbd8SKent Overstreet bio = bio_alloc_bioset(NULL, 24054d126dc8SKent Overstreet bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS), 24061c6fdbd8SKent Overstreet REQ_OP_WRITE, 24071c6fdbd8SKent Overstreet GFP_KERNEL, 24081c6fdbd8SKent Overstreet &c->dio_write_bioset); 24099a3df993SKent Overstreet dio = container_of(bio, struct dio_write, op.wbio.bio); 24101c6fdbd8SKent Overstreet dio->req = req; 2411182c7bbfSKent Overstreet dio->mapping = mapping; 2412182c7bbfSKent Overstreet dio->inode = inode; 2413ed484030SKent Overstreet dio->mm = current->mm; 24141c6fdbd8SKent Overstreet dio->loop = false; 24156b1b186aSKent Overstreet dio->extending = extending; 24167edcfbfeSKent Overstreet dio->sync = is_sync_kiocb(req) || extending; 2417a1ee777bSKent Overstreet dio->flush = iocb_is_dsync(req) && !c->opts.journal_flush_disabled; 24181c6fdbd8SKent Overstreet dio->free_iov = false; 24191c6fdbd8SKent Overstreet dio->quota_res.sectors = 0; 2420042a1f26SKent Overstreet dio->written = 0; 24211c6fdbd8SKent Overstreet dio->iter = *iter; 2422182c7bbfSKent Overstreet dio->op.c = c; 24239a3df993SKent Overstreet 2424a023127aSKent Overstreet if (unlikely(mapping->nrpages)) { 242554847d25SKent Overstreet ret = write_invalidate_inode_pages_range(mapping, 242654847d25SKent Overstreet req->ki_pos, 242754847d25SKent Overstreet req->ki_pos + iter->count - 1); 242854847d25SKent Overstreet if (unlikely(ret)) 242954847d25SKent Overstreet goto err_put_bio; 2430a023127aSKent Overstreet } 243154847d25SKent Overstreet 24327edcfbfeSKent Overstreet ret = bch2_dio_write_loop(dio); 24331c6fdbd8SKent Overstreet err: 24347edcfbfeSKent Overstreet if (locked) 24357edcfbfeSKent Overstreet inode_unlock(&inode->v); 24367edcfbfeSKent Overstreet return ret; 24377edcfbfeSKent Overstreet err_put_bio: 2438a7ecd30cSKent Overstreet bch2_pagecache_block_put(inode); 24391c6fdbd8SKent Overstreet bio_put(bio); 24407edcfbfeSKent Overstreet inode_dio_end(&inode->v); 24417edcfbfeSKent Overstreet goto err; 24421c6fdbd8SKent Overstreet } 24431c6fdbd8SKent Overstreet 24447edcfbfeSKent Overstreet ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *from) 24451c6fdbd8SKent Overstreet { 24461c6fdbd8SKent Overstreet struct file *file = iocb->ki_filp; 24477edcfbfeSKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 24481c6fdbd8SKent Overstreet ssize_t ret; 24491c6fdbd8SKent Overstreet 24505c1ef830SKent Overstreet if (iocb->ki_flags & IOCB_DIRECT) { 24515c1ef830SKent Overstreet ret = bch2_direct_write(iocb, from); 24525c1ef830SKent Overstreet goto out; 24535c1ef830SKent Overstreet } 24541c6fdbd8SKent Overstreet 24557edcfbfeSKent Overstreet inode_lock(&inode->v); 24567edcfbfeSKent Overstreet 24577edcfbfeSKent Overstreet ret = generic_write_checks(iocb, from); 24587edcfbfeSKent Overstreet if (ret <= 0) 24597edcfbfeSKent Overstreet goto unlock; 24607edcfbfeSKent Overstreet 24611c6fdbd8SKent Overstreet ret = file_remove_privs(file); 24621c6fdbd8SKent Overstreet if (ret) 24637edcfbfeSKent Overstreet goto unlock; 24641c6fdbd8SKent Overstreet 24651c6fdbd8SKent Overstreet ret = file_update_time(file); 24661c6fdbd8SKent Overstreet if (ret) 24677edcfbfeSKent Overstreet goto unlock; 24681c6fdbd8SKent Overstreet 24697edcfbfeSKent Overstreet ret = bch2_buffered_write(iocb, from); 24701c6fdbd8SKent Overstreet if (likely(ret > 0)) 24711c6fdbd8SKent Overstreet iocb->ki_pos += ret; 24727edcfbfeSKent Overstreet unlock: 24731c6fdbd8SKent Overstreet inode_unlock(&inode->v); 24741c6fdbd8SKent Overstreet 24757edcfbfeSKent Overstreet if (ret > 0) 24761c6fdbd8SKent Overstreet ret = generic_write_sync(iocb, ret); 24775c1ef830SKent Overstreet out: 24785c1ef830SKent Overstreet return bch2_err_class(ret); 24791c6fdbd8SKent Overstreet } 24801c6fdbd8SKent Overstreet 24811c6fdbd8SKent Overstreet /* fsync: */ 24821c6fdbd8SKent Overstreet 248368a2054dSKent Overstreet /* 248468a2054dSKent Overstreet * inode->ei_inode.bi_journal_seq won't be up to date since it's set in an 248568a2054dSKent Overstreet * insert trigger: look up the btree inode instead 248668a2054dSKent Overstreet */ 248768a2054dSKent Overstreet static int bch2_flush_inode(struct bch_fs *c, subvol_inum inum) 248868a2054dSKent Overstreet { 248968a2054dSKent Overstreet struct bch_inode_unpacked inode; 249068a2054dSKent Overstreet int ret; 249168a2054dSKent Overstreet 249268a2054dSKent Overstreet if (c->opts.journal_flush_disabled) 249368a2054dSKent Overstreet return 0; 249468a2054dSKent Overstreet 249568a2054dSKent Overstreet ret = bch2_inode_find_by_inum(c, inum, &inode); 249668a2054dSKent Overstreet if (ret) 249768a2054dSKent Overstreet return ret; 249868a2054dSKent Overstreet 249968a2054dSKent Overstreet return bch2_journal_flush_seq(&c->journal, inode.bi_journal_seq); 250068a2054dSKent Overstreet } 250168a2054dSKent Overstreet 25021c6fdbd8SKent Overstreet int bch2_fsync(struct file *file, loff_t start, loff_t end, int datasync) 25031c6fdbd8SKent Overstreet { 25041c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 25051c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 250668a2054dSKent Overstreet int ret, ret2, ret3; 25071c6fdbd8SKent Overstreet 25081c6fdbd8SKent Overstreet ret = file_write_and_wait_range(file, start, end); 250968a2054dSKent Overstreet ret2 = sync_inode_metadata(&inode->v, 1); 251068a2054dSKent Overstreet ret3 = bch2_flush_inode(c, inode_inum(inode)); 25111c6fdbd8SKent Overstreet 25125c1ef830SKent Overstreet return bch2_err_class(ret ?: ret2 ?: ret3); 25131c6fdbd8SKent Overstreet } 25141c6fdbd8SKent Overstreet 25151c6fdbd8SKent Overstreet /* truncate: */ 25161c6fdbd8SKent Overstreet 25176fed42bbSKent Overstreet static inline int range_has_data(struct bch_fs *c, u32 subvol, 25181c6fdbd8SKent Overstreet struct bpos start, 25191c6fdbd8SKent Overstreet struct bpos end) 25201c6fdbd8SKent Overstreet { 2521424eb881SKent Overstreet struct btree_trans trans; 252267e0dd8fSKent Overstreet struct btree_iter iter; 25231c6fdbd8SKent Overstreet struct bkey_s_c k; 25241c6fdbd8SKent Overstreet int ret = 0; 25251c6fdbd8SKent Overstreet 252620bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 25276fed42bbSKent Overstreet retry: 25286fed42bbSKent Overstreet bch2_trans_begin(&trans); 25296fed42bbSKent Overstreet 25306fed42bbSKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, subvol, &start.snapshot); 25316fed42bbSKent Overstreet if (ret) 25326fed42bbSKent Overstreet goto err; 2533424eb881SKent Overstreet 2534e5fa91d7SKent Overstreet for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents, start, 0, k, ret) { 2535*e88a75ebSKent Overstreet if (bkey_ge(bkey_start_pos(k.k), end)) 25361c6fdbd8SKent Overstreet break; 25371c6fdbd8SKent Overstreet 25381c6fdbd8SKent Overstreet if (bkey_extent_is_data(k.k)) { 25391c6fdbd8SKent Overstreet ret = 1; 25401c6fdbd8SKent Overstreet break; 25411c6fdbd8SKent Overstreet } 25421c6fdbd8SKent Overstreet } 25436fed42bbSKent Overstreet start = iter.pos; 254467e0dd8fSKent Overstreet bch2_trans_iter_exit(&trans, &iter); 25456fed42bbSKent Overstreet err: 2546549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 25476fed42bbSKent Overstreet goto retry; 25481c6fdbd8SKent Overstreet 25499a796fdbSKent Overstreet bch2_trans_exit(&trans); 25509a796fdbSKent Overstreet return ret; 25511c6fdbd8SKent Overstreet } 25521c6fdbd8SKent Overstreet 25531c6fdbd8SKent Overstreet static int __bch2_truncate_page(struct bch_inode_info *inode, 25541c6fdbd8SKent Overstreet pgoff_t index, loff_t start, loff_t end) 25551c6fdbd8SKent Overstreet { 25561c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 25571c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 2558a99b1cafSKent Overstreet struct bch_page_state *s; 25591c6fdbd8SKent Overstreet unsigned start_offset = start & (PAGE_SIZE - 1); 25601c6fdbd8SKent Overstreet unsigned end_offset = ((end - 1) & (PAGE_SIZE - 1)) + 1; 2561a99b1cafSKent Overstreet unsigned i; 25621c6fdbd8SKent Overstreet struct page *page; 2563b19d307dSKent Overstreet s64 i_sectors_delta = 0; 25641c6fdbd8SKent Overstreet int ret = 0; 25651c6fdbd8SKent Overstreet 25661c6fdbd8SKent Overstreet /* Page boundary? Nothing to do */ 25671c6fdbd8SKent Overstreet if (!((index == start >> PAGE_SHIFT && start_offset) || 25681c6fdbd8SKent Overstreet (index == end >> PAGE_SHIFT && end_offset != PAGE_SIZE))) 25691c6fdbd8SKent Overstreet return 0; 25701c6fdbd8SKent Overstreet 25711c6fdbd8SKent Overstreet /* Above i_size? */ 25721c6fdbd8SKent Overstreet if (index << PAGE_SHIFT >= inode->v.i_size) 25731c6fdbd8SKent Overstreet return 0; 25741c6fdbd8SKent Overstreet 25751c6fdbd8SKent Overstreet page = find_lock_page(mapping, index); 25761c6fdbd8SKent Overstreet if (!page) { 25771c6fdbd8SKent Overstreet /* 25781c6fdbd8SKent Overstreet * XXX: we're doing two index lookups when we end up reading the 25791c6fdbd8SKent Overstreet * page 25801c6fdbd8SKent Overstreet */ 25816fed42bbSKent Overstreet ret = range_has_data(c, inode->ei_subvol, 25827279c1a2SKent Overstreet POS(inode->v.i_ino, index << PAGE_SECTORS_SHIFT), 25837279c1a2SKent Overstreet POS(inode->v.i_ino, (index + 1) << PAGE_SECTORS_SHIFT)); 25841c6fdbd8SKent Overstreet if (ret <= 0) 25851c6fdbd8SKent Overstreet return ret; 25861c6fdbd8SKent Overstreet 25871c6fdbd8SKent Overstreet page = find_or_create_page(mapping, index, GFP_KERNEL); 25881c6fdbd8SKent Overstreet if (unlikely(!page)) { 25891c6fdbd8SKent Overstreet ret = -ENOMEM; 25901c6fdbd8SKent Overstreet goto out; 25911c6fdbd8SKent Overstreet } 25921c6fdbd8SKent Overstreet } 25931c6fdbd8SKent Overstreet 2594a99b1cafSKent Overstreet s = bch2_page_state_create(page, 0); 2595a99b1cafSKent Overstreet if (!s) { 2596a99b1cafSKent Overstreet ret = -ENOMEM; 2597a99b1cafSKent Overstreet goto unlock; 2598a99b1cafSKent Overstreet } 2599a99b1cafSKent Overstreet 26001c6fdbd8SKent Overstreet if (!PageUptodate(page)) { 26011c6fdbd8SKent Overstreet ret = bch2_read_single_page(page, mapping); 26021c6fdbd8SKent Overstreet if (ret) 26031c6fdbd8SKent Overstreet goto unlock; 26041c6fdbd8SKent Overstreet } 26051c6fdbd8SKent Overstreet 2606a99b1cafSKent Overstreet if (index != start >> PAGE_SHIFT) 2607a99b1cafSKent Overstreet start_offset = 0; 2608a99b1cafSKent Overstreet if (index != end >> PAGE_SHIFT) 2609a99b1cafSKent Overstreet end_offset = PAGE_SIZE; 2610a99b1cafSKent Overstreet 2611a99b1cafSKent Overstreet for (i = round_up(start_offset, block_bytes(c)) >> 9; 2612a99b1cafSKent Overstreet i < round_down(end_offset, block_bytes(c)) >> 9; 2613a99b1cafSKent Overstreet i++) { 2614a99b1cafSKent Overstreet s->s[i].nr_replicas = 0; 2615b19d307dSKent Overstreet if (s->s[i].state == SECTOR_DIRTY) 2616b19d307dSKent Overstreet i_sectors_delta--; 2617a99b1cafSKent Overstreet s->s[i].state = SECTOR_UNALLOCATED; 2618a99b1cafSKent Overstreet } 2619a99b1cafSKent Overstreet 2620b19d307dSKent Overstreet i_sectors_acct(c, inode, NULL, i_sectors_delta); 2621b19d307dSKent Overstreet 262274163da7SKent Overstreet /* 262374163da7SKent Overstreet * Caller needs to know whether this page will be written out by 262474163da7SKent Overstreet * writeback - doing an i_size update if necessary - or whether it will 262574163da7SKent Overstreet * be responsible for the i_size update: 262674163da7SKent Overstreet */ 262774163da7SKent Overstreet ret = s->s[(min_t(u64, inode->v.i_size - (index << PAGE_SHIFT), 262874163da7SKent Overstreet PAGE_SIZE) - 1) >> 9].state >= SECTOR_DIRTY; 262974163da7SKent Overstreet 2630a99b1cafSKent Overstreet zero_user_segment(page, start_offset, end_offset); 2631a99b1cafSKent Overstreet 26321c6fdbd8SKent Overstreet /* 26331c6fdbd8SKent Overstreet * Bit of a hack - we don't want truncate to fail due to -ENOSPC. 26341c6fdbd8SKent Overstreet * 26351c6fdbd8SKent Overstreet * XXX: because we aren't currently tracking whether the page has actual 26361c6fdbd8SKent Overstreet * data in it (vs. just 0s, or only partially written) this wrong. ick. 26371c6fdbd8SKent Overstreet */ 263874163da7SKent Overstreet BUG_ON(bch2_get_page_disk_reservation(c, inode, page, false)); 26391c6fdbd8SKent Overstreet 26409ba2eb25SKent Overstreet /* 26419ba2eb25SKent Overstreet * This removes any writeable userspace mappings; we need to force 26429ba2eb25SKent Overstreet * .page_mkwrite to be called again before any mmapped writes, to 26439ba2eb25SKent Overstreet * redirty the full page: 26449ba2eb25SKent Overstreet */ 26459ba2eb25SKent Overstreet page_mkclean(page); 2646d1542e03SKent Overstreet filemap_dirty_folio(mapping, page_folio(page)); 26471c6fdbd8SKent Overstreet unlock: 26481c6fdbd8SKent Overstreet unlock_page(page); 26491c6fdbd8SKent Overstreet put_page(page); 26501c6fdbd8SKent Overstreet out: 26511c6fdbd8SKent Overstreet return ret; 26521c6fdbd8SKent Overstreet } 26531c6fdbd8SKent Overstreet 26541c6fdbd8SKent Overstreet static int bch2_truncate_page(struct bch_inode_info *inode, loff_t from) 26551c6fdbd8SKent Overstreet { 26561c6fdbd8SKent Overstreet return __bch2_truncate_page(inode, from >> PAGE_SHIFT, 2657a99b1cafSKent Overstreet from, round_up(from, PAGE_SIZE)); 26581c6fdbd8SKent Overstreet } 26591c6fdbd8SKent Overstreet 266074163da7SKent Overstreet static int bch2_truncate_pages(struct bch_inode_info *inode, 266174163da7SKent Overstreet loff_t start, loff_t end) 266274163da7SKent Overstreet { 266374163da7SKent Overstreet int ret = __bch2_truncate_page(inode, start >> PAGE_SHIFT, 266474163da7SKent Overstreet start, end); 266574163da7SKent Overstreet 266674163da7SKent Overstreet if (ret >= 0 && 266774163da7SKent Overstreet start >> PAGE_SHIFT != end >> PAGE_SHIFT) 266874163da7SKent Overstreet ret = __bch2_truncate_page(inode, 266974163da7SKent Overstreet end >> PAGE_SHIFT, 267074163da7SKent Overstreet start, end); 267174163da7SKent Overstreet return ret; 267274163da7SKent Overstreet } 267374163da7SKent Overstreet 267468a507a2SKent Overstreet static int bch2_extend(struct mnt_idmap *idmap, 267568a507a2SKent Overstreet struct bch_inode_info *inode, 2676e0541a93SKent Overstreet struct bch_inode_unpacked *inode_u, 2677e0541a93SKent Overstreet struct iattr *iattr) 26781c6fdbd8SKent Overstreet { 26791c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 26801c6fdbd8SKent Overstreet int ret; 26811c6fdbd8SKent Overstreet 2682e0541a93SKent Overstreet /* 2683e0541a93SKent Overstreet * sync appends: 26842925fc49SKent Overstreet * 26852925fc49SKent Overstreet * this has to be done _before_ extending i_size: 2686e0541a93SKent Overstreet */ 2687e0541a93SKent Overstreet ret = filemap_write_and_wait_range(mapping, inode_u->bi_size, S64_MAX); 26881c6fdbd8SKent Overstreet if (ret) 26891c6fdbd8SKent Overstreet return ret; 26901c6fdbd8SKent Overstreet 26911c6fdbd8SKent Overstreet truncate_setsize(&inode->v, iattr->ia_size); 26921c6fdbd8SKent Overstreet 269368a507a2SKent Overstreet return bch2_setattr_nonsize(idmap, inode, iattr); 26941c6fdbd8SKent Overstreet } 26951c6fdbd8SKent Overstreet 269654e2264eSKent Overstreet static int bch2_truncate_finish_fn(struct bch_inode_info *inode, 269754e2264eSKent Overstreet struct bch_inode_unpacked *bi, 269854e2264eSKent Overstreet void *p) 269954e2264eSKent Overstreet { 270054e2264eSKent Overstreet bi->bi_flags &= ~BCH_INODE_I_SIZE_DIRTY; 270154e2264eSKent Overstreet return 0; 270254e2264eSKent Overstreet } 270354e2264eSKent Overstreet 270454e2264eSKent Overstreet static int bch2_truncate_start_fn(struct bch_inode_info *inode, 270554e2264eSKent Overstreet struct bch_inode_unpacked *bi, void *p) 270654e2264eSKent Overstreet { 270754e2264eSKent Overstreet u64 *new_i_size = p; 270854e2264eSKent Overstreet 270954e2264eSKent Overstreet bi->bi_flags |= BCH_INODE_I_SIZE_DIRTY; 271054e2264eSKent Overstreet bi->bi_size = *new_i_size; 271154e2264eSKent Overstreet return 0; 271254e2264eSKent Overstreet } 271354e2264eSKent Overstreet 271468a507a2SKent Overstreet int bch2_truncate(struct mnt_idmap *idmap, 271568a507a2SKent Overstreet struct bch_inode_info *inode, struct iattr *iattr) 27161c6fdbd8SKent Overstreet { 27171c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 27181c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 2719e0541a93SKent Overstreet struct bch_inode_unpacked inode_u; 272054e2264eSKent Overstreet u64 new_i_size = iattr->ia_size; 27212e87eae1SKent Overstreet s64 i_sectors_delta = 0; 27221c6fdbd8SKent Overstreet int ret = 0; 27231c6fdbd8SKent Overstreet 272468a507a2SKent Overstreet /* 272578d66ab1SDan Robertson * If the truncate call with change the size of the file, the 272678d66ab1SDan Robertson * cmtimes should be updated. If the size will not change, we 272778d66ab1SDan Robertson * do not need to update the cmtimes. 272868a507a2SKent Overstreet */ 272978d66ab1SDan Robertson if (iattr->ia_size != inode->v.i_size) { 273068a507a2SKent Overstreet if (!(iattr->ia_valid & ATTR_MTIME)) 273168a507a2SKent Overstreet ktime_get_coarse_real_ts64(&iattr->ia_mtime); 273268a507a2SKent Overstreet if (!(iattr->ia_valid & ATTR_CTIME)) 273368a507a2SKent Overstreet ktime_get_coarse_real_ts64(&iattr->ia_ctime); 273468a507a2SKent Overstreet iattr->ia_valid |= ATTR_MTIME|ATTR_CTIME; 273578d66ab1SDan Robertson } 273668a507a2SKent Overstreet 27371c6fdbd8SKent Overstreet inode_dio_wait(&inode->v); 2738a7ecd30cSKent Overstreet bch2_pagecache_block_get(inode); 27391c6fdbd8SKent Overstreet 27406fed42bbSKent Overstreet ret = bch2_inode_find_by_inum(c, inode_inum(inode), &inode_u); 2741e0541a93SKent Overstreet if (ret) 2742e0541a93SKent Overstreet goto err; 27431c6fdbd8SKent Overstreet 2744c45d473dSKent Overstreet /* 2745c45d473dSKent Overstreet * check this before next assertion; on filesystem error our normal 2746c45d473dSKent Overstreet * invariants are a bit broken (truncate has to truncate the page cache 2747c45d473dSKent Overstreet * before the inode). 2748c45d473dSKent Overstreet */ 2749c45d473dSKent Overstreet ret = bch2_journal_error(&c->journal); 2750c45d473dSKent Overstreet if (ret) 2751c45d473dSKent Overstreet goto err; 2752c45d473dSKent Overstreet 27538eb71e9eSKent Overstreet WARN_ONCE(!test_bit(EI_INODE_ERROR, &inode->ei_flags) && 27548eb71e9eSKent Overstreet inode->v.i_size < inode_u.bi_size, 27558eb71e9eSKent Overstreet "truncate spotted in mem i_size < btree i_size: %llu < %llu\n", 27568eb71e9eSKent Overstreet (u64) inode->v.i_size, inode_u.bi_size); 2757e0541a93SKent Overstreet 2758e0541a93SKent Overstreet if (iattr->ia_size > inode->v.i_size) { 275968a507a2SKent Overstreet ret = bch2_extend(idmap, inode, &inode_u, iattr); 276054e2264eSKent Overstreet goto err; 27611c6fdbd8SKent Overstreet } 27621c6fdbd8SKent Overstreet 276368a507a2SKent Overstreet iattr->ia_valid &= ~ATTR_SIZE; 276468a507a2SKent Overstreet 27651c6fdbd8SKent Overstreet ret = bch2_truncate_page(inode, iattr->ia_size); 276674163da7SKent Overstreet if (unlikely(ret < 0)) 276754e2264eSKent Overstreet goto err; 27681c6fdbd8SKent Overstreet 27696cc3535dSKent Overstreet /* 27706cc3535dSKent Overstreet * When extending, we're going to write the new i_size to disk 27716cc3535dSKent Overstreet * immediately so we need to flush anything above the current on disk 27726cc3535dSKent Overstreet * i_size first: 27736cc3535dSKent Overstreet * 27746cc3535dSKent Overstreet * Also, when extending we need to flush the page that i_size currently 27756cc3535dSKent Overstreet * straddles - if it's mapped to userspace, we need to ensure that 27766cc3535dSKent Overstreet * userspace has to redirty it and call .mkwrite -> set_page_dirty 27776cc3535dSKent Overstreet * again to allocate the part of the page that was extended. 27786cc3535dSKent Overstreet */ 2779e0541a93SKent Overstreet if (iattr->ia_size > inode_u.bi_size) 27801c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 2781e0541a93SKent Overstreet inode_u.bi_size, 27821c6fdbd8SKent Overstreet iattr->ia_size - 1); 27831c6fdbd8SKent Overstreet else if (iattr->ia_size & (PAGE_SIZE - 1)) 27841c6fdbd8SKent Overstreet ret = filemap_write_and_wait_range(mapping, 27851c6fdbd8SKent Overstreet round_down(iattr->ia_size, PAGE_SIZE), 27861c6fdbd8SKent Overstreet iattr->ia_size - 1); 27871c6fdbd8SKent Overstreet if (ret) 278854e2264eSKent Overstreet goto err; 27891c6fdbd8SKent Overstreet 279054e2264eSKent Overstreet mutex_lock(&inode->ei_update_lock); 279154e2264eSKent Overstreet ret = bch2_write_inode(c, inode, bch2_truncate_start_fn, 279254e2264eSKent Overstreet &new_i_size, 0); 279354e2264eSKent Overstreet mutex_unlock(&inode->ei_update_lock); 27941c6fdbd8SKent Overstreet 27951c6fdbd8SKent Overstreet if (unlikely(ret)) 279654e2264eSKent Overstreet goto err; 27971c6fdbd8SKent Overstreet 27981c6fdbd8SKent Overstreet truncate_setsize(&inode->v, iattr->ia_size); 27991c6fdbd8SKent Overstreet 28008c6d298aSKent Overstreet ret = bch2_fpunch(c, inode_inum(inode), 2801a99b1cafSKent Overstreet round_up(iattr->ia_size, block_bytes(c)) >> 9, 280268a2054dSKent Overstreet U64_MAX, &i_sectors_delta); 28032e87eae1SKent Overstreet i_sectors_acct(c, inode, NULL, i_sectors_delta); 28042e87eae1SKent Overstreet 2805b33bf1bcSKent Overstreet bch2_fs_inconsistent_on(!inode->v.i_size && inode->v.i_blocks && 2806b33bf1bcSKent Overstreet !bch2_journal_error(&c->journal), c, 2807b33bf1bcSKent Overstreet "inode %lu truncated to 0 but i_blocks %llu (ondisk %lli)", 2808b33bf1bcSKent Overstreet inode->v.i_ino, (u64) inode->v.i_blocks, 2809b33bf1bcSKent Overstreet inode->ei_inode.bi_sectors); 28101c6fdbd8SKent Overstreet if (unlikely(ret)) 281154e2264eSKent Overstreet goto err; 28121c6fdbd8SKent Overstreet 281354e2264eSKent Overstreet mutex_lock(&inode->ei_update_lock); 281468a507a2SKent Overstreet ret = bch2_write_inode(c, inode, bch2_truncate_finish_fn, NULL, 0); 281554e2264eSKent Overstreet mutex_unlock(&inode->ei_update_lock); 281668a507a2SKent Overstreet 281768a507a2SKent Overstreet ret = bch2_setattr_nonsize(idmap, inode, iattr); 281854e2264eSKent Overstreet err: 2819a7ecd30cSKent Overstreet bch2_pagecache_block_put(inode); 28205c1ef830SKent Overstreet return bch2_err_class(ret); 28211c6fdbd8SKent Overstreet } 28221c6fdbd8SKent Overstreet 28231c6fdbd8SKent Overstreet /* fallocate: */ 28241c6fdbd8SKent Overstreet 2825050197b1SKent Overstreet static int inode_update_times_fn(struct bch_inode_info *inode, 2826050197b1SKent Overstreet struct bch_inode_unpacked *bi, void *p) 2827050197b1SKent Overstreet { 2828050197b1SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 2829050197b1SKent Overstreet 2830050197b1SKent Overstreet bi->bi_mtime = bi->bi_ctime = bch2_current_time(c); 2831050197b1SKent Overstreet return 0; 2832050197b1SKent Overstreet } 2833050197b1SKent Overstreet 28342e87eae1SKent Overstreet static long bchfs_fpunch(struct bch_inode_info *inode, loff_t offset, loff_t len) 28351c6fdbd8SKent Overstreet { 28361c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 283774163da7SKent Overstreet u64 end = offset + len; 283874163da7SKent Overstreet u64 block_start = round_up(offset, block_bytes(c)); 283974163da7SKent Overstreet u64 block_end = round_down(end, block_bytes(c)); 284074163da7SKent Overstreet bool truncated_last_page; 28411c6fdbd8SKent Overstreet int ret = 0; 28421c6fdbd8SKent Overstreet 284374163da7SKent Overstreet ret = bch2_truncate_pages(inode, offset, end); 284474163da7SKent Overstreet if (unlikely(ret < 0)) 28451c6fdbd8SKent Overstreet goto err; 28461c6fdbd8SKent Overstreet 284774163da7SKent Overstreet truncated_last_page = ret; 28481c6fdbd8SKent Overstreet 284974163da7SKent Overstreet truncate_pagecache_range(&inode->v, offset, end - 1); 28501c6fdbd8SKent Overstreet 285174163da7SKent Overstreet if (block_start < block_end) { 28522e87eae1SKent Overstreet s64 i_sectors_delta = 0; 28532e87eae1SKent Overstreet 28548c6d298aSKent Overstreet ret = bch2_fpunch(c, inode_inum(inode), 285574163da7SKent Overstreet block_start >> 9, block_end >> 9, 28562e87eae1SKent Overstreet &i_sectors_delta); 28572e87eae1SKent Overstreet i_sectors_acct(c, inode, NULL, i_sectors_delta); 28582e87eae1SKent Overstreet } 2859050197b1SKent Overstreet 2860050197b1SKent Overstreet mutex_lock(&inode->ei_update_lock); 286174163da7SKent Overstreet if (end >= inode->v.i_size && !truncated_last_page) { 286274163da7SKent Overstreet ret = bch2_write_inode_size(c, inode, inode->v.i_size, 286374163da7SKent Overstreet ATTR_MTIME|ATTR_CTIME); 286474163da7SKent Overstreet } else { 2865050197b1SKent Overstreet ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL, 286674163da7SKent Overstreet ATTR_MTIME|ATTR_CTIME); 286774163da7SKent Overstreet } 2868050197b1SKent Overstreet mutex_unlock(&inode->ei_update_lock); 28691c6fdbd8SKent Overstreet err: 28701c6fdbd8SKent Overstreet return ret; 28711c6fdbd8SKent Overstreet } 28721c6fdbd8SKent Overstreet 28732e87eae1SKent Overstreet static long bchfs_fcollapse_finsert(struct bch_inode_info *inode, 28745f786787SKent Overstreet loff_t offset, loff_t len, 28755f786787SKent Overstreet bool insert) 28761c6fdbd8SKent Overstreet { 28771c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 28781c6fdbd8SKent Overstreet struct address_space *mapping = inode->v.i_mapping; 287907a1006aSKent Overstreet struct bkey_buf copy; 2880d69f41d6SKent Overstreet struct btree_trans trans; 288167e0dd8fSKent Overstreet struct btree_iter src, dst, del; 28825f786787SKent Overstreet loff_t shift, new_size; 28835f786787SKent Overstreet u64 src_start; 288450dc0f69SKent Overstreet int ret = 0; 28851c6fdbd8SKent Overstreet 28861c6fdbd8SKent Overstreet if ((offset | len) & (block_bytes(c) - 1)) 28871c6fdbd8SKent Overstreet return -EINVAL; 28881c6fdbd8SKent Overstreet 28895f786787SKent Overstreet if (insert) { 28905f786787SKent Overstreet if (inode->v.i_sb->s_maxbytes - inode->v.i_size < len) 289174163da7SKent Overstreet return -EFBIG; 28925f786787SKent Overstreet 28935f786787SKent Overstreet if (offset >= inode->v.i_size) 289474163da7SKent Overstreet return -EINVAL; 28955f786787SKent Overstreet 28965f786787SKent Overstreet src_start = U64_MAX; 28975f786787SKent Overstreet shift = len; 28985f786787SKent Overstreet } else { 28991c6fdbd8SKent Overstreet if (offset + len >= inode->v.i_size) 290074163da7SKent Overstreet return -EINVAL; 29011c6fdbd8SKent Overstreet 29025f786787SKent Overstreet src_start = offset + len; 29035f786787SKent Overstreet shift = -len; 29045f786787SKent Overstreet } 29051c6fdbd8SKent Overstreet 29065f786787SKent Overstreet new_size = inode->v.i_size + shift; 29071c6fdbd8SKent Overstreet 29081c6fdbd8SKent Overstreet ret = write_invalidate_inode_pages_range(mapping, offset, LLONG_MAX); 29091c6fdbd8SKent Overstreet if (ret) 291074163da7SKent Overstreet return ret; 29111c6fdbd8SKent Overstreet 29125f786787SKent Overstreet if (insert) { 29135f786787SKent Overstreet i_size_write(&inode->v, new_size); 29145f786787SKent Overstreet mutex_lock(&inode->ei_update_lock); 29155f786787SKent Overstreet ret = bch2_write_inode_size(c, inode, new_size, 29165f786787SKent Overstreet ATTR_MTIME|ATTR_CTIME); 29175f786787SKent Overstreet mutex_unlock(&inode->ei_update_lock); 29185f786787SKent Overstreet } else { 29192e87eae1SKent Overstreet s64 i_sectors_delta = 0; 29202e87eae1SKent Overstreet 29218c6d298aSKent Overstreet ret = bch2_fpunch(c, inode_inum(inode), 29222e87eae1SKent Overstreet offset >> 9, (offset + len) >> 9, 29232e87eae1SKent Overstreet &i_sectors_delta); 29242e87eae1SKent Overstreet i_sectors_acct(c, inode, NULL, i_sectors_delta); 29252e87eae1SKent Overstreet 292663095894SKent Overstreet if (ret) 292774163da7SKent Overstreet return ret; 29285f786787SKent Overstreet } 29298ef231bdSKent Overstreet 293050dc0f69SKent Overstreet bch2_bkey_buf_init(©); 2931f7beb4caSKent Overstreet bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024); 293267e0dd8fSKent Overstreet bch2_trans_iter_init(&trans, &src, BTREE_ID_extents, 29335f786787SKent Overstreet POS(inode->v.i_ino, src_start >> 9), 293463095894SKent Overstreet BTREE_ITER_INTENT); 293567e0dd8fSKent Overstreet bch2_trans_copy_iter(&dst, &src); 293667e0dd8fSKent Overstreet bch2_trans_copy_iter(&del, &src); 29375f786787SKent Overstreet 2938549d173cSKent Overstreet while (ret == 0 || 2939549d173cSKent Overstreet bch2_err_matches(ret, BCH_ERR_transaction_restart)) { 294063095894SKent Overstreet struct disk_reservation disk_res = 294163095894SKent Overstreet bch2_disk_reservation_init(c, 0); 294263095894SKent Overstreet struct bkey_i delete; 294363095894SKent Overstreet struct bkey_s_c k; 294463095894SKent Overstreet struct bpos next_pos; 29455f786787SKent Overstreet struct bpos move_pos = POS(inode->v.i_ino, offset >> 9); 29465f786787SKent Overstreet struct bpos atomic_end; 29472d594dfbSKent Overstreet unsigned trigger_flags = 0; 29486fed42bbSKent Overstreet u32 snapshot; 29496fed42bbSKent Overstreet 29506fed42bbSKent Overstreet bch2_trans_begin(&trans); 29516fed42bbSKent Overstreet 29526fed42bbSKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, 29536fed42bbSKent Overstreet inode->ei_subvol, &snapshot); 29546fed42bbSKent Overstreet if (ret) 29556fed42bbSKent Overstreet continue; 29566fed42bbSKent Overstreet 29576fed42bbSKent Overstreet bch2_btree_iter_set_snapshot(&src, snapshot); 29586fed42bbSKent Overstreet bch2_btree_iter_set_snapshot(&dst, snapshot); 29596fed42bbSKent Overstreet bch2_btree_iter_set_snapshot(&del, snapshot); 296063095894SKent Overstreet 2961700c25b3SKent Overstreet bch2_trans_begin(&trans); 2962700c25b3SKent Overstreet 29635f786787SKent Overstreet k = insert 296467e0dd8fSKent Overstreet ? bch2_btree_iter_peek_prev(&src) 296567e0dd8fSKent Overstreet : bch2_btree_iter_peek(&src); 296663095894SKent Overstreet if ((ret = bkey_err(k))) 296750dc0f69SKent Overstreet continue; 296863095894SKent Overstreet 296963095894SKent Overstreet if (!k.k || k.k->p.inode != inode->v.i_ino) 297063095894SKent Overstreet break; 297163095894SKent Overstreet 29725f786787SKent Overstreet if (insert && 2973*e88a75ebSKent Overstreet bkey_le(k.k->p, POS(inode->v.i_ino, offset >> 9))) 29745f786787SKent Overstreet break; 29755f786787SKent Overstreet reassemble: 297607a1006aSKent Overstreet bch2_bkey_buf_reassemble(©, c, k); 29775f786787SKent Overstreet 29785f786787SKent Overstreet if (insert && 2979*e88a75ebSKent Overstreet bkey_lt(bkey_start_pos(k.k), move_pos)) 298035189e09SKent Overstreet bch2_cut_front(move_pos, copy.k); 29815f786787SKent Overstreet 298235189e09SKent Overstreet copy.k->k.p.offset += shift >> 9; 298367e0dd8fSKent Overstreet bch2_btree_iter_set_pos(&dst, bkey_start_pos(©.k->k)); 29841c6fdbd8SKent Overstreet 298567e0dd8fSKent Overstreet ret = bch2_extent_atomic_end(&trans, &dst, copy.k, &atomic_end); 29863c7f3b7aSKent Overstreet if (ret) 298750dc0f69SKent Overstreet continue; 2988e2d9912cSKent Overstreet 2989*e88a75ebSKent Overstreet if (!bkey_eq(atomic_end, copy.k->k.p)) { 29905f786787SKent Overstreet if (insert) { 29915f786787SKent Overstreet move_pos = atomic_end; 29925f786787SKent Overstreet move_pos.offset -= shift >> 9; 29935f786787SKent Overstreet goto reassemble; 29945f786787SKent Overstreet } else { 2995085ab693SKent Overstreet bch2_cut_back(atomic_end, copy.k); 29965f786787SKent Overstreet } 29975f786787SKent Overstreet } 29985f786787SKent Overstreet 299963095894SKent Overstreet bkey_init(&delete.k); 3000283eda57SKent Overstreet delete.k.p = copy.k->k.p; 3001283eda57SKent Overstreet delete.k.size = copy.k->k.size; 3002283eda57SKent Overstreet delete.k.p.offset -= shift >> 9; 300367e0dd8fSKent Overstreet bch2_btree_iter_set_pos(&del, bkey_start_pos(&delete.k)); 30041c6fdbd8SKent Overstreet 30055f786787SKent Overstreet next_pos = insert ? bkey_start_pos(&delete.k) : delete.k.p; 300663095894SKent Overstreet 30077c4ca54aSKent Overstreet if (copy.k->k.size != k.k->size) { 300863095894SKent Overstreet /* We might end up splitting compressed extents: */ 300963095894SKent Overstreet unsigned nr_ptrs = 30104de77495SKent Overstreet bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(copy.k)); 301163095894SKent Overstreet 301263095894SKent Overstreet ret = bch2_disk_reservation_get(c, &disk_res, 301335189e09SKent Overstreet copy.k->k.size, nr_ptrs, 30141c6fdbd8SKent Overstreet BCH_DISK_RESERVATION_NOFAIL); 30151c6fdbd8SKent Overstreet BUG_ON(ret); 301663095894SKent Overstreet } 30171c6fdbd8SKent Overstreet 301867e0dd8fSKent Overstreet ret = bch2_btree_iter_traverse(&del) ?: 301967e0dd8fSKent Overstreet bch2_trans_update(&trans, &del, &delete, trigger_flags) ?: 302067e0dd8fSKent Overstreet bch2_trans_update(&trans, &dst, copy.k, trigger_flags) ?: 302168a2054dSKent Overstreet bch2_trans_commit(&trans, &disk_res, NULL, 30222d594dfbSKent Overstreet BTREE_INSERT_NOFAIL); 30231c6fdbd8SKent Overstreet bch2_disk_reservation_put(c, &disk_res); 302450dc0f69SKent Overstreet 302563095894SKent Overstreet if (!ret) 302667e0dd8fSKent Overstreet bch2_btree_iter_set_pos(&src, next_pos); 302750dc0f69SKent Overstreet } 302867e0dd8fSKent Overstreet bch2_trans_iter_exit(&trans, &del); 302967e0dd8fSKent Overstreet bch2_trans_iter_exit(&trans, &dst); 303067e0dd8fSKent Overstreet bch2_trans_iter_exit(&trans, &src); 303150dc0f69SKent Overstreet bch2_trans_exit(&trans); 303250dc0f69SKent Overstreet bch2_bkey_buf_exit(©, c); 303363095894SKent Overstreet 30348ef231bdSKent Overstreet if (ret) 303574163da7SKent Overstreet return ret; 30361c6fdbd8SKent Overstreet 303774163da7SKent Overstreet mutex_lock(&inode->ei_update_lock); 30385f786787SKent Overstreet if (!insert) { 30398ef231bdSKent Overstreet i_size_write(&inode->v, new_size); 30408ef231bdSKent Overstreet ret = bch2_write_inode_size(c, inode, new_size, 30418ef231bdSKent Overstreet ATTR_MTIME|ATTR_CTIME); 304274163da7SKent Overstreet } else { 304374163da7SKent Overstreet /* We need an inode update to update bi_journal_seq for fsync: */ 304474163da7SKent Overstreet ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL, 304574163da7SKent Overstreet ATTR_MTIME|ATTR_CTIME); 30465f786787SKent Overstreet } 304774163da7SKent Overstreet mutex_unlock(&inode->ei_update_lock); 30481c6fdbd8SKent Overstreet return ret; 30491c6fdbd8SKent Overstreet } 30501c6fdbd8SKent Overstreet 3051694015c2SKent Overstreet static int __bchfs_fallocate(struct bch_inode_info *inode, int mode, 3052694015c2SKent Overstreet u64 start_sector, u64 end_sector) 30531c6fdbd8SKent Overstreet { 30541c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 3055190fa7afSKent Overstreet struct btree_trans trans; 305667e0dd8fSKent Overstreet struct btree_iter iter; 3057694015c2SKent Overstreet struct bpos end_pos = POS(inode->v.i_ino, end_sector); 30589a3df993SKent Overstreet unsigned replicas = io_opts(c, &inode->ei_inode).data_replicas; 3059694015c2SKent Overstreet int ret = 0; 30601c6fdbd8SKent Overstreet 3061f7beb4caSKent Overstreet bch2_trans_init(&trans, c, BTREE_ITER_MAX, 512); 30621c6fdbd8SKent Overstreet 306367e0dd8fSKent Overstreet bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents, 3064694015c2SKent Overstreet POS(inode->v.i_ino, start_sector), 3065190fa7afSKent Overstreet BTREE_ITER_SLOTS|BTREE_ITER_INTENT); 30661c6fdbd8SKent Overstreet 3067*e88a75ebSKent Overstreet while (!ret && bkey_lt(iter.pos, end_pos)) { 30682e87eae1SKent Overstreet s64 i_sectors_delta = 0; 30691c6fdbd8SKent Overstreet struct disk_reservation disk_res = { 0 }; 3070190fa7afSKent Overstreet struct quota_res quota_res = { 0 }; 30711c6fdbd8SKent Overstreet struct bkey_i_reservation reservation; 30721c6fdbd8SKent Overstreet struct bkey_s_c k; 3073694015c2SKent Overstreet unsigned sectors; 30746fed42bbSKent Overstreet u32 snapshot; 30751c6fdbd8SKent Overstreet 3076163e885aSKent Overstreet bch2_trans_begin(&trans); 3077a8abd3a7SKent Overstreet 30786fed42bbSKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, 30796fed42bbSKent Overstreet inode->ei_subvol, &snapshot); 30806fed42bbSKent Overstreet if (ret) 30816fed42bbSKent Overstreet goto bkey_err; 30826fed42bbSKent Overstreet 30836fed42bbSKent Overstreet bch2_btree_iter_set_snapshot(&iter, snapshot); 30846fed42bbSKent Overstreet 308567e0dd8fSKent Overstreet k = bch2_btree_iter_peek_slot(&iter); 30860f238367SKent Overstreet if ((ret = bkey_err(k))) 30870f238367SKent Overstreet goto bkey_err; 30881c6fdbd8SKent Overstreet 30891c6fdbd8SKent Overstreet /* already reserved */ 309026609b61SKent Overstreet if (k.k->type == KEY_TYPE_reservation && 30911c6fdbd8SKent Overstreet bkey_s_c_to_reservation(k).v->nr_replicas >= replicas) { 309267e0dd8fSKent Overstreet bch2_btree_iter_advance(&iter); 30931c6fdbd8SKent Overstreet continue; 30941c6fdbd8SKent Overstreet } 30951c6fdbd8SKent Overstreet 3096190fa7afSKent Overstreet if (bkey_extent_is_data(k.k) && 3097190fa7afSKent Overstreet !(mode & FALLOC_FL_ZERO_RANGE)) { 309867e0dd8fSKent Overstreet bch2_btree_iter_advance(&iter); 30991c6fdbd8SKent Overstreet continue; 31001c6fdbd8SKent Overstreet } 31011c6fdbd8SKent Overstreet 31021c6fdbd8SKent Overstreet bkey_reservation_init(&reservation.k_i); 310326609b61SKent Overstreet reservation.k.type = KEY_TYPE_reservation; 31041c6fdbd8SKent Overstreet reservation.k.p = k.k->p; 31051c6fdbd8SKent Overstreet reservation.k.size = k.k->size; 31061c6fdbd8SKent Overstreet 310767e0dd8fSKent Overstreet bch2_cut_front(iter.pos, &reservation.k_i); 3108085ab693SKent Overstreet bch2_cut_back(end_pos, &reservation.k_i); 31091c6fdbd8SKent Overstreet 31101c6fdbd8SKent Overstreet sectors = reservation.k.size; 31114de77495SKent Overstreet reservation.v.nr_replicas = bch2_bkey_nr_ptrs_allocated(k); 31121c6fdbd8SKent Overstreet 31131c6fdbd8SKent Overstreet if (!bkey_extent_is_allocation(k.k)) { 31141c6fdbd8SKent Overstreet ret = bch2_quota_reservation_add(c, inode, 3115190fa7afSKent Overstreet "a_res, 31161c6fdbd8SKent Overstreet sectors, true); 31171c6fdbd8SKent Overstreet if (unlikely(ret)) 31180f238367SKent Overstreet goto bkey_err; 31191c6fdbd8SKent Overstreet } 31201c6fdbd8SKent Overstreet 31211c6fdbd8SKent Overstreet if (reservation.v.nr_replicas < replicas || 31224de77495SKent Overstreet bch2_bkey_sectors_compressed(k)) { 31231c6fdbd8SKent Overstreet ret = bch2_disk_reservation_get(c, &disk_res, sectors, 31241c6fdbd8SKent Overstreet replicas, 0); 31251c6fdbd8SKent Overstreet if (unlikely(ret)) 31260f238367SKent Overstreet goto bkey_err; 31271c6fdbd8SKent Overstreet 31281c6fdbd8SKent Overstreet reservation.v.nr_replicas = disk_res.nr_replicas; 31291c6fdbd8SKent Overstreet } 31301c6fdbd8SKent Overstreet 31318c6d298aSKent Overstreet ret = bch2_extent_update(&trans, inode_inum(inode), &iter, 3132a1ee777bSKent Overstreet &reservation.k_i, &disk_res, 3133a6336910SKent Overstreet 0, &i_sectors_delta, true); 31348810386fSKent Overstreet if (ret) 31358810386fSKent Overstreet goto bkey_err; 31362e87eae1SKent Overstreet i_sectors_acct(c, inode, "a_res, i_sectors_delta); 31370f238367SKent Overstreet bkey_err: 3138190fa7afSKent Overstreet bch2_quota_reservation_put(c, inode, "a_res); 31391c6fdbd8SKent Overstreet bch2_disk_reservation_put(c, &disk_res); 3140549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 31411c6fdbd8SKent Overstreet ret = 0; 314250dc0f69SKent Overstreet } 314374163da7SKent Overstreet 3144dcfc593fSKent Overstreet bch2_trans_unlock(&trans); /* lock ordering, before taking pagecache locks: */ 3145dcfc593fSKent Overstreet mark_pagecache_reserved(inode, start_sector, iter.pos.offset); 3146dcfc593fSKent Overstreet 3147098ef98dSKent Overstreet if (bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE)) { 314874163da7SKent Overstreet struct quota_res quota_res = { 0 }; 314974163da7SKent Overstreet s64 i_sectors_delta = 0; 315074163da7SKent Overstreet 315174163da7SKent Overstreet bch2_fpunch_at(&trans, &iter, inode_inum(inode), 315274163da7SKent Overstreet end_sector, &i_sectors_delta); 315374163da7SKent Overstreet i_sectors_acct(c, inode, "a_res, i_sectors_delta); 315474163da7SKent Overstreet bch2_quota_reservation_put(c, inode, "a_res); 315574163da7SKent Overstreet } 315674163da7SKent Overstreet 315767e0dd8fSKent Overstreet bch2_trans_iter_exit(&trans, &iter); 3158694015c2SKent Overstreet bch2_trans_exit(&trans); 3159694015c2SKent Overstreet return ret; 3160694015c2SKent Overstreet } 316150dc0f69SKent Overstreet 3162694015c2SKent Overstreet static long bchfs_fallocate(struct bch_inode_info *inode, int mode, 3163694015c2SKent Overstreet loff_t offset, loff_t len) 3164694015c2SKent Overstreet { 3165694015c2SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 316674163da7SKent Overstreet u64 end = offset + len; 316774163da7SKent Overstreet u64 block_start = round_down(offset, block_bytes(c)); 316874163da7SKent Overstreet u64 block_end = round_up(end, block_bytes(c)); 316974163da7SKent Overstreet bool truncated_last_page = false; 317074163da7SKent Overstreet int ret, ret2 = 0; 3171694015c2SKent Overstreet 3172694015c2SKent Overstreet if (!(mode & FALLOC_FL_KEEP_SIZE) && end > inode->v.i_size) { 3173694015c2SKent Overstreet ret = inode_newsize_ok(&inode->v, end); 3174694015c2SKent Overstreet if (ret) 317574163da7SKent Overstreet return ret; 3176694015c2SKent Overstreet } 3177694015c2SKent Overstreet 3178694015c2SKent Overstreet if (mode & FALLOC_FL_ZERO_RANGE) { 317974163da7SKent Overstreet ret = bch2_truncate_pages(inode, offset, end); 318074163da7SKent Overstreet if (unlikely(ret < 0)) 318174163da7SKent Overstreet return ret; 3182694015c2SKent Overstreet 318374163da7SKent Overstreet truncated_last_page = ret; 3184694015c2SKent Overstreet 3185694015c2SKent Overstreet truncate_pagecache_range(&inode->v, offset, end - 1); 318674163da7SKent Overstreet 318774163da7SKent Overstreet block_start = round_up(offset, block_bytes(c)); 318874163da7SKent Overstreet block_end = round_down(end, block_bytes(c)); 3189694015c2SKent Overstreet } 3190694015c2SKent Overstreet 3191694015c2SKent Overstreet ret = __bchfs_fallocate(inode, mode, block_start >> 9, block_end >> 9); 3192e0541a93SKent Overstreet 3193e0541a93SKent Overstreet /* 319474163da7SKent Overstreet * On -ENOSPC in ZERO_RANGE mode, we still want to do the inode update, 319574163da7SKent Overstreet * so that the VFS cache i_size is consistent with the btree i_size: 3196e0541a93SKent Overstreet */ 319774163da7SKent Overstreet if (ret && 3198098ef98dSKent Overstreet !(bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE))) 319974163da7SKent Overstreet return ret; 32001c6fdbd8SKent Overstreet 320174163da7SKent Overstreet if (mode & FALLOC_FL_KEEP_SIZE && end > inode->v.i_size) 3202e0541a93SKent Overstreet end = inode->v.i_size; 320374163da7SKent Overstreet 320474163da7SKent Overstreet if (end >= inode->v.i_size && 320574163da7SKent Overstreet (((mode & FALLOC_FL_ZERO_RANGE) && !truncated_last_page) || 320674163da7SKent Overstreet !(mode & FALLOC_FL_KEEP_SIZE))) { 320774163da7SKent Overstreet spin_lock(&inode->v.i_lock); 3208e0541a93SKent Overstreet i_size_write(&inode->v, end); 320974163da7SKent Overstreet spin_unlock(&inode->v.i_lock); 3210e0541a93SKent Overstreet 32111c6fdbd8SKent Overstreet mutex_lock(&inode->ei_update_lock); 321274163da7SKent Overstreet ret2 = bch2_write_inode_size(c, inode, end, 0); 32131c6fdbd8SKent Overstreet mutex_unlock(&inode->ei_update_lock); 32141c6fdbd8SKent Overstreet } 321574163da7SKent Overstreet 321674163da7SKent Overstreet return ret ?: ret2; 32171c6fdbd8SKent Overstreet } 32181c6fdbd8SKent Overstreet 32191c6fdbd8SKent Overstreet long bch2_fallocate_dispatch(struct file *file, int mode, 32201c6fdbd8SKent Overstreet loff_t offset, loff_t len) 32211c6fdbd8SKent Overstreet { 32221c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 32232a9101a9SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 32242a9101a9SKent Overstreet long ret; 32252a9101a9SKent Overstreet 3226a3d7afa5SKent Overstreet if (!percpu_ref_tryget_live(&c->writes)) 32272a9101a9SKent Overstreet return -EROFS; 32281c6fdbd8SKent Overstreet 322974163da7SKent Overstreet inode_lock(&inode->v); 323074163da7SKent Overstreet inode_dio_wait(&inode->v); 3231a7ecd30cSKent Overstreet bch2_pagecache_block_get(inode); 323274163da7SKent Overstreet 323307bfcc0bSKent Overstreet ret = file_modified(file); 323407bfcc0bSKent Overstreet if (ret) 323507bfcc0bSKent Overstreet goto err; 323607bfcc0bSKent Overstreet 32371c6fdbd8SKent Overstreet if (!(mode & ~(FALLOC_FL_KEEP_SIZE|FALLOC_FL_ZERO_RANGE))) 32382a9101a9SKent Overstreet ret = bchfs_fallocate(inode, mode, offset, len); 32392a9101a9SKent Overstreet else if (mode == (FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE)) 32402a9101a9SKent Overstreet ret = bchfs_fpunch(inode, offset, len); 32412a9101a9SKent Overstreet else if (mode == FALLOC_FL_INSERT_RANGE) 32422a9101a9SKent Overstreet ret = bchfs_fcollapse_finsert(inode, offset, len, true); 32432a9101a9SKent Overstreet else if (mode == FALLOC_FL_COLLAPSE_RANGE) 32442a9101a9SKent Overstreet ret = bchfs_fcollapse_finsert(inode, offset, len, false); 32452a9101a9SKent Overstreet else 32462a9101a9SKent Overstreet ret = -EOPNOTSUPP; 324707bfcc0bSKent Overstreet err: 3248a7ecd30cSKent Overstreet bch2_pagecache_block_put(inode); 324974163da7SKent Overstreet inode_unlock(&inode->v); 32502a9101a9SKent Overstreet percpu_ref_put(&c->writes); 32511c6fdbd8SKent Overstreet 32525c1ef830SKent Overstreet return bch2_err_class(ret); 32531c6fdbd8SKent Overstreet } 32541c6fdbd8SKent Overstreet 3255e8540e56SKent Overstreet static int quota_reserve_range(struct bch_inode_info *inode, 3256e8540e56SKent Overstreet struct quota_res *res, 3257e8540e56SKent Overstreet u64 start, u64 end) 3258e8540e56SKent Overstreet { 3259e8540e56SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 3260e8540e56SKent Overstreet struct btree_trans trans; 3261e8540e56SKent Overstreet struct btree_iter iter; 3262e8540e56SKent Overstreet struct bkey_s_c k; 3263e8540e56SKent Overstreet u32 snapshot; 3264e8540e56SKent Overstreet u64 sectors = end - start; 3265e8540e56SKent Overstreet u64 pos = start; 3266e8540e56SKent Overstreet int ret; 3267e8540e56SKent Overstreet 3268e8540e56SKent Overstreet bch2_trans_init(&trans, c, 0, 0); 3269e8540e56SKent Overstreet retry: 3270e8540e56SKent Overstreet bch2_trans_begin(&trans); 3271e8540e56SKent Overstreet 3272e8540e56SKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, inode->ei_subvol, &snapshot); 3273e8540e56SKent Overstreet if (ret) 3274e8540e56SKent Overstreet goto err; 3275e8540e56SKent Overstreet 3276e8540e56SKent Overstreet bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents, 3277e8540e56SKent Overstreet SPOS(inode->v.i_ino, pos, snapshot), 0); 3278e8540e56SKent Overstreet 3279e8540e56SKent Overstreet while (!(ret = btree_trans_too_many_iters(&trans)) && 3280e8540e56SKent Overstreet (k = bch2_btree_iter_peek_upto(&iter, POS(inode->v.i_ino, end - 1))).k && 3281e8540e56SKent Overstreet !(ret = bkey_err(k))) { 3282e8540e56SKent Overstreet if (bkey_extent_is_allocation(k.k)) { 3283e8540e56SKent Overstreet u64 s = min(end, k.k->p.offset) - 3284e8540e56SKent Overstreet max(start, bkey_start_offset(k.k)); 3285e8540e56SKent Overstreet BUG_ON(s > sectors); 3286e8540e56SKent Overstreet sectors -= s; 3287e8540e56SKent Overstreet } 3288e8540e56SKent Overstreet bch2_btree_iter_advance(&iter); 3289e8540e56SKent Overstreet } 3290e8540e56SKent Overstreet pos = iter.pos.offset; 3291e8540e56SKent Overstreet bch2_trans_iter_exit(&trans, &iter); 3292e8540e56SKent Overstreet err: 3293e8540e56SKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 3294e8540e56SKent Overstreet goto retry; 3295e8540e56SKent Overstreet 3296e8540e56SKent Overstreet bch2_trans_exit(&trans); 3297e8540e56SKent Overstreet 3298e8540e56SKent Overstreet if (ret) 3299e8540e56SKent Overstreet return ret; 3300e8540e56SKent Overstreet 3301e8540e56SKent Overstreet return bch2_quota_reservation_add(c, inode, res, sectors, true); 3302e8540e56SKent Overstreet } 3303e8540e56SKent Overstreet 330476426098SKent Overstreet loff_t bch2_remap_file_range(struct file *file_src, loff_t pos_src, 330576426098SKent Overstreet struct file *file_dst, loff_t pos_dst, 330676426098SKent Overstreet loff_t len, unsigned remap_flags) 330776426098SKent Overstreet { 330876426098SKent Overstreet struct bch_inode_info *src = file_bch_inode(file_src); 330976426098SKent Overstreet struct bch_inode_info *dst = file_bch_inode(file_dst); 331076426098SKent Overstreet struct bch_fs *c = src->v.i_sb->s_fs_info; 3311e8540e56SKent Overstreet struct quota_res quota_res = { 0 }; 33122e87eae1SKent Overstreet s64 i_sectors_delta = 0; 3313677fc056SKent Overstreet u64 aligned_len; 331476426098SKent Overstreet loff_t ret = 0; 331576426098SKent Overstreet 331676426098SKent Overstreet if (remap_flags & ~(REMAP_FILE_DEDUP|REMAP_FILE_ADVISORY)) 331776426098SKent Overstreet return -EINVAL; 331876426098SKent Overstreet 331976426098SKent Overstreet if (remap_flags & REMAP_FILE_DEDUP) 332076426098SKent Overstreet return -EOPNOTSUPP; 332176426098SKent Overstreet 332276426098SKent Overstreet if ((pos_src & (block_bytes(c) - 1)) || 332376426098SKent Overstreet (pos_dst & (block_bytes(c) - 1))) 332476426098SKent Overstreet return -EINVAL; 332576426098SKent Overstreet 332676426098SKent Overstreet if (src == dst && 332776426098SKent Overstreet abs(pos_src - pos_dst) < len) 332876426098SKent Overstreet return -EINVAL; 332976426098SKent Overstreet 333076426098SKent Overstreet bch2_lock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst); 333176426098SKent Overstreet 333276426098SKent Overstreet inode_dio_wait(&src->v); 333376426098SKent Overstreet inode_dio_wait(&dst->v); 333476426098SKent Overstreet 333576426098SKent Overstreet ret = generic_remap_file_range_prep(file_src, pos_src, 333676426098SKent Overstreet file_dst, pos_dst, 333776426098SKent Overstreet &len, remap_flags); 333876426098SKent Overstreet if (ret < 0 || len == 0) 33392e87eae1SKent Overstreet goto err; 334076426098SKent Overstreet 3341677fc056SKent Overstreet aligned_len = round_up((u64) len, block_bytes(c)); 334276426098SKent Overstreet 334376426098SKent Overstreet ret = write_invalidate_inode_pages_range(dst->v.i_mapping, 3344677fc056SKent Overstreet pos_dst, pos_dst + len - 1); 334576426098SKent Overstreet if (ret) 33462e87eae1SKent Overstreet goto err; 334776426098SKent Overstreet 3348e8540e56SKent Overstreet ret = quota_reserve_range(dst, "a_res, pos_dst >> 9, 3349e8540e56SKent Overstreet (pos_dst + aligned_len) >> 9); 3350e8540e56SKent Overstreet if (ret) 3351e8540e56SKent Overstreet goto err; 3352e8540e56SKent Overstreet 3353e8540e56SKent Overstreet file_update_time(file_dst); 3354e8540e56SKent Overstreet 3355dcfc593fSKent Overstreet mark_pagecache_unallocated(src, pos_src >> 9, 3356dcfc593fSKent Overstreet (pos_src + aligned_len) >> 9); 335776426098SKent Overstreet 33582e87eae1SKent Overstreet ret = bch2_remap_range(c, 33596fed42bbSKent Overstreet inode_inum(dst), pos_dst >> 9, 33606fed42bbSKent Overstreet inode_inum(src), pos_src >> 9, 336176426098SKent Overstreet aligned_len >> 9, 33622e87eae1SKent Overstreet pos_dst + len, &i_sectors_delta); 33632e87eae1SKent Overstreet if (ret < 0) 33642e87eae1SKent Overstreet goto err; 336576426098SKent Overstreet 33662e87eae1SKent Overstreet /* 33672e87eae1SKent Overstreet * due to alignment, we might have remapped slightly more than requsted 33682e87eae1SKent Overstreet */ 3369677fc056SKent Overstreet ret = min((u64) ret << 9, (u64) len); 33702e87eae1SKent Overstreet 3371e8540e56SKent Overstreet i_sectors_acct(c, dst, "a_res, i_sectors_delta); 33722e87eae1SKent Overstreet 33732e87eae1SKent Overstreet spin_lock(&dst->v.i_lock); 3374677fc056SKent Overstreet if (pos_dst + ret > dst->v.i_size) 3375677fc056SKent Overstreet i_size_write(&dst->v, pos_dst + ret); 33762e87eae1SKent Overstreet spin_unlock(&dst->v.i_lock); 3377e7084c9cSKent Overstreet 337868a2054dSKent Overstreet if ((file_dst->f_flags & (__O_SYNC | O_DSYNC)) || 337968a2054dSKent Overstreet IS_SYNC(file_inode(file_dst))) 338068a2054dSKent Overstreet ret = bch2_flush_inode(c, inode_inum(dst)); 33812e87eae1SKent Overstreet err: 3382e8540e56SKent Overstreet bch2_quota_reservation_put(c, dst, "a_res); 338376426098SKent Overstreet bch2_unlock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst); 338476426098SKent Overstreet 33855c1ef830SKent Overstreet return bch2_err_class(ret); 338676426098SKent Overstreet } 338776426098SKent Overstreet 33881c6fdbd8SKent Overstreet /* fseek: */ 33891c6fdbd8SKent Overstreet 3390543ef2ebSKent Overstreet static int folio_data_offset(struct folio *folio, unsigned offset) 33911c6fdbd8SKent Overstreet { 3392f57a6a5dSKent Overstreet struct bch_page_state *s = bch2_page_state(&folio->page); 3393f57a6a5dSKent Overstreet unsigned i; 3394f81b648dSKent Overstreet 3395543ef2ebSKent Overstreet if (s) 3396543ef2ebSKent Overstreet for (i = offset >> 9; i < PAGE_SECTORS; i++) 3397f57a6a5dSKent Overstreet if (s->s[i].state >= SECTOR_DIRTY) 3398543ef2ebSKent Overstreet return i << 9; 3399f57a6a5dSKent Overstreet 3400543ef2ebSKent Overstreet return -1; 34011c6fdbd8SKent Overstreet } 34021c6fdbd8SKent Overstreet 3403543ef2ebSKent Overstreet static loff_t bch2_seek_pagecache_data(struct inode *vinode, 34041c6fdbd8SKent Overstreet loff_t start_offset, 34051c6fdbd8SKent Overstreet loff_t end_offset) 34061c6fdbd8SKent Overstreet { 34071c6fdbd8SKent Overstreet struct folio_batch fbatch; 34081c6fdbd8SKent Overstreet pgoff_t start_index = start_offset >> PAGE_SHIFT; 34091c6fdbd8SKent Overstreet pgoff_t end_index = end_offset >> PAGE_SHIFT; 34101c6fdbd8SKent Overstreet pgoff_t index = start_index; 34111c6fdbd8SKent Overstreet unsigned i; 3412543ef2ebSKent Overstreet loff_t ret; 3413543ef2ebSKent Overstreet int offset; 34141c6fdbd8SKent Overstreet 34151c6fdbd8SKent Overstreet folio_batch_init(&fbatch); 34161c6fdbd8SKent Overstreet 34171c6fdbd8SKent Overstreet while (filemap_get_folios(vinode->i_mapping, 34181c6fdbd8SKent Overstreet &index, end_index, &fbatch)) { 34191c6fdbd8SKent Overstreet for (i = 0; i < folio_batch_count(&fbatch); i++) { 34201c6fdbd8SKent Overstreet struct folio *folio = fbatch.folios[i]; 34211c6fdbd8SKent Overstreet 34221c6fdbd8SKent Overstreet folio_lock(folio); 3423543ef2ebSKent Overstreet offset = folio_data_offset(folio, 3424543ef2ebSKent Overstreet folio->index == start_index 3425543ef2ebSKent Overstreet ? start_offset & (PAGE_SIZE - 1) 3426543ef2ebSKent Overstreet : 0); 3427543ef2ebSKent Overstreet if (offset >= 0) { 3428543ef2ebSKent Overstreet ret = clamp(((loff_t) folio->index << PAGE_SHIFT) + 3429543ef2ebSKent Overstreet offset, 3430543ef2ebSKent Overstreet start_offset, end_offset); 34311c6fdbd8SKent Overstreet folio_unlock(folio); 34321c6fdbd8SKent Overstreet folio_batch_release(&fbatch); 3433543ef2ebSKent Overstreet return ret; 34341c6fdbd8SKent Overstreet } 34351c6fdbd8SKent Overstreet folio_unlock(folio); 34361c6fdbd8SKent Overstreet } 34371c6fdbd8SKent Overstreet folio_batch_release(&fbatch); 34381c6fdbd8SKent Overstreet cond_resched(); 34391c6fdbd8SKent Overstreet } 34401c6fdbd8SKent Overstreet 34411c6fdbd8SKent Overstreet return end_offset; 34421c6fdbd8SKent Overstreet } 34431c6fdbd8SKent Overstreet 34441c6fdbd8SKent Overstreet static loff_t bch2_seek_data(struct file *file, u64 offset) 34451c6fdbd8SKent Overstreet { 34461c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 34471c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 3448424eb881SKent Overstreet struct btree_trans trans; 344967e0dd8fSKent Overstreet struct btree_iter iter; 34501c6fdbd8SKent Overstreet struct bkey_s_c k; 34516fed42bbSKent Overstreet subvol_inum inum = inode_inum(inode); 34521c6fdbd8SKent Overstreet u64 isize, next_data = MAX_LFS_FILESIZE; 34536fed42bbSKent Overstreet u32 snapshot; 34541c6fdbd8SKent Overstreet int ret; 34551c6fdbd8SKent Overstreet 34561c6fdbd8SKent Overstreet isize = i_size_read(&inode->v); 34571c6fdbd8SKent Overstreet if (offset >= isize) 34581c6fdbd8SKent Overstreet return -ENXIO; 34591c6fdbd8SKent Overstreet 346020bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 34616fed42bbSKent Overstreet retry: 34626fed42bbSKent Overstreet bch2_trans_begin(&trans); 34636fed42bbSKent Overstreet 34646fed42bbSKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot); 34656fed42bbSKent Overstreet if (ret) 34666fed42bbSKent Overstreet goto err; 3467424eb881SKent Overstreet 3468e5fa91d7SKent Overstreet for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents, 34696fed42bbSKent Overstreet SPOS(inode->v.i_ino, offset >> 9, snapshot), 0, k, ret) { 34701c6fdbd8SKent Overstreet if (k.k->p.inode != inode->v.i_ino) { 34711c6fdbd8SKent Overstreet break; 34721c6fdbd8SKent Overstreet } else if (bkey_extent_is_data(k.k)) { 34731c6fdbd8SKent Overstreet next_data = max(offset, bkey_start_offset(k.k) << 9); 34741c6fdbd8SKent Overstreet break; 34751c6fdbd8SKent Overstreet } else if (k.k->p.offset >> 9 > isize) 34761c6fdbd8SKent Overstreet break; 34771c6fdbd8SKent Overstreet } 347867e0dd8fSKent Overstreet bch2_trans_iter_exit(&trans, &iter); 34796fed42bbSKent Overstreet err: 3480549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 34816fed42bbSKent Overstreet goto retry; 34821c6fdbd8SKent Overstreet 34839a796fdbSKent Overstreet bch2_trans_exit(&trans); 34841c6fdbd8SKent Overstreet if (ret) 34851c6fdbd8SKent Overstreet return ret; 34861c6fdbd8SKent Overstreet 34871c6fdbd8SKent Overstreet if (next_data > offset) 3488543ef2ebSKent Overstreet next_data = bch2_seek_pagecache_data(&inode->v, 34891c6fdbd8SKent Overstreet offset, next_data); 34901c6fdbd8SKent Overstreet 3491e10d3094SKent Overstreet if (next_data >= isize) 34921c6fdbd8SKent Overstreet return -ENXIO; 34931c6fdbd8SKent Overstreet 34941c6fdbd8SKent Overstreet return vfs_setpos(file, next_data, MAX_LFS_FILESIZE); 34951c6fdbd8SKent Overstreet } 34961c6fdbd8SKent Overstreet 3497543ef2ebSKent Overstreet static int __page_hole_offset(struct page *page, unsigned offset) 34981c6fdbd8SKent Overstreet { 3499543ef2ebSKent Overstreet struct bch_page_state *s = bch2_page_state(page); 3500543ef2ebSKent Overstreet unsigned i; 3501543ef2ebSKent Overstreet 3502543ef2ebSKent Overstreet if (!s) 3503543ef2ebSKent Overstreet return 0; 3504543ef2ebSKent Overstreet 3505543ef2ebSKent Overstreet for (i = offset >> 9; i < PAGE_SECTORS; i++) 3506543ef2ebSKent Overstreet if (s->s[i].state < SECTOR_DIRTY) 3507543ef2ebSKent Overstreet return i << 9; 3508543ef2ebSKent Overstreet 3509543ef2ebSKent Overstreet return -1; 3510543ef2ebSKent Overstreet } 3511543ef2ebSKent Overstreet 3512543ef2ebSKent Overstreet static loff_t page_hole_offset(struct address_space *mapping, loff_t offset) 3513543ef2ebSKent Overstreet { 3514543ef2ebSKent Overstreet pgoff_t index = offset >> PAGE_SHIFT; 35151c6fdbd8SKent Overstreet struct page *page; 3516543ef2ebSKent Overstreet int pg_offset; 3517543ef2ebSKent Overstreet loff_t ret = -1; 35181c6fdbd8SKent Overstreet 35191c6fdbd8SKent Overstreet page = find_lock_page(mapping, index); 35201c6fdbd8SKent Overstreet if (!page) 3521543ef2ebSKent Overstreet return offset; 35221c6fdbd8SKent Overstreet 3523543ef2ebSKent Overstreet pg_offset = __page_hole_offset(page, offset & (PAGE_SIZE - 1)); 3524543ef2ebSKent Overstreet if (pg_offset >= 0) 3525543ef2ebSKent Overstreet ret = ((loff_t) index << PAGE_SHIFT) + pg_offset; 3526543ef2ebSKent Overstreet 35271c6fdbd8SKent Overstreet unlock_page(page); 35281c6fdbd8SKent Overstreet 35291c6fdbd8SKent Overstreet return ret; 35301c6fdbd8SKent Overstreet } 35311c6fdbd8SKent Overstreet 3532543ef2ebSKent Overstreet static loff_t bch2_seek_pagecache_hole(struct inode *vinode, 35331c6fdbd8SKent Overstreet loff_t start_offset, 35341c6fdbd8SKent Overstreet loff_t end_offset) 35351c6fdbd8SKent Overstreet { 35361c6fdbd8SKent Overstreet struct address_space *mapping = vinode->i_mapping; 3537543ef2ebSKent Overstreet loff_t offset = start_offset, hole; 35381c6fdbd8SKent Overstreet 3539543ef2ebSKent Overstreet while (offset < end_offset) { 3540543ef2ebSKent Overstreet hole = page_hole_offset(mapping, offset); 3541543ef2ebSKent Overstreet if (hole >= 0 && hole <= end_offset) 3542543ef2ebSKent Overstreet return max(start_offset, hole); 3543543ef2ebSKent Overstreet 3544543ef2ebSKent Overstreet offset += PAGE_SIZE; 3545543ef2ebSKent Overstreet offset &= PAGE_MASK; 3546543ef2ebSKent Overstreet } 35471c6fdbd8SKent Overstreet 35481c6fdbd8SKent Overstreet return end_offset; 35491c6fdbd8SKent Overstreet } 35501c6fdbd8SKent Overstreet 35511c6fdbd8SKent Overstreet static loff_t bch2_seek_hole(struct file *file, u64 offset) 35521c6fdbd8SKent Overstreet { 35531c6fdbd8SKent Overstreet struct bch_inode_info *inode = file_bch_inode(file); 35541c6fdbd8SKent Overstreet struct bch_fs *c = inode->v.i_sb->s_fs_info; 3555424eb881SKent Overstreet struct btree_trans trans; 355667e0dd8fSKent Overstreet struct btree_iter iter; 35571c6fdbd8SKent Overstreet struct bkey_s_c k; 35586fed42bbSKent Overstreet subvol_inum inum = inode_inum(inode); 35591c6fdbd8SKent Overstreet u64 isize, next_hole = MAX_LFS_FILESIZE; 35606fed42bbSKent Overstreet u32 snapshot; 35611c6fdbd8SKent Overstreet int ret; 35621c6fdbd8SKent Overstreet 35631c6fdbd8SKent Overstreet isize = i_size_read(&inode->v); 35641c6fdbd8SKent Overstreet if (offset >= isize) 35651c6fdbd8SKent Overstreet return -ENXIO; 35661c6fdbd8SKent Overstreet 356720bceecbSKent Overstreet bch2_trans_init(&trans, c, 0, 0); 35686fed42bbSKent Overstreet retry: 35696fed42bbSKent Overstreet bch2_trans_begin(&trans); 35706fed42bbSKent Overstreet 35716fed42bbSKent Overstreet ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot); 35726fed42bbSKent Overstreet if (ret) 35736fed42bbSKent Overstreet goto err; 3574424eb881SKent Overstreet 3575e5fa91d7SKent Overstreet for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents, 35766fed42bbSKent Overstreet SPOS(inode->v.i_ino, offset >> 9, snapshot), 357794f651e2SKent Overstreet BTREE_ITER_SLOTS, k, ret) { 35781c6fdbd8SKent Overstreet if (k.k->p.inode != inode->v.i_ino) { 3579543ef2ebSKent Overstreet next_hole = bch2_seek_pagecache_hole(&inode->v, 35801c6fdbd8SKent Overstreet offset, MAX_LFS_FILESIZE); 35811c6fdbd8SKent Overstreet break; 35821c6fdbd8SKent Overstreet } else if (!bkey_extent_is_data(k.k)) { 3583543ef2ebSKent Overstreet next_hole = bch2_seek_pagecache_hole(&inode->v, 35841c6fdbd8SKent Overstreet max(offset, bkey_start_offset(k.k) << 9), 35851c6fdbd8SKent Overstreet k.k->p.offset << 9); 35861c6fdbd8SKent Overstreet 35871c6fdbd8SKent Overstreet if (next_hole < k.k->p.offset << 9) 35881c6fdbd8SKent Overstreet break; 35891c6fdbd8SKent Overstreet } else { 35901c6fdbd8SKent Overstreet offset = max(offset, bkey_start_offset(k.k) << 9); 35911c6fdbd8SKent Overstreet } 35921c6fdbd8SKent Overstreet } 359367e0dd8fSKent Overstreet bch2_trans_iter_exit(&trans, &iter); 35946fed42bbSKent Overstreet err: 3595549d173cSKent Overstreet if (bch2_err_matches(ret, BCH_ERR_transaction_restart)) 35966fed42bbSKent Overstreet goto retry; 35971c6fdbd8SKent Overstreet 35989a796fdbSKent Overstreet bch2_trans_exit(&trans); 35991c6fdbd8SKent Overstreet if (ret) 36001c6fdbd8SKent Overstreet return ret; 36011c6fdbd8SKent Overstreet 36021c6fdbd8SKent Overstreet if (next_hole > isize) 36031c6fdbd8SKent Overstreet next_hole = isize; 36041c6fdbd8SKent Overstreet 36051c6fdbd8SKent Overstreet return vfs_setpos(file, next_hole, MAX_LFS_FILESIZE); 36061c6fdbd8SKent Overstreet } 36071c6fdbd8SKent Overstreet 36081c6fdbd8SKent Overstreet loff_t bch2_llseek(struct file *file, loff_t offset, int whence) 36091c6fdbd8SKent Overstreet { 36105c1ef830SKent Overstreet loff_t ret; 36115c1ef830SKent Overstreet 36121c6fdbd8SKent Overstreet switch (whence) { 36131c6fdbd8SKent Overstreet case SEEK_SET: 36141c6fdbd8SKent Overstreet case SEEK_CUR: 36151c6fdbd8SKent Overstreet case SEEK_END: 36165c1ef830SKent Overstreet ret = generic_file_llseek(file, offset, whence); 36175c1ef830SKent Overstreet break; 36181c6fdbd8SKent Overstreet case SEEK_DATA: 36195c1ef830SKent Overstreet ret = bch2_seek_data(file, offset); 36205c1ef830SKent Overstreet break; 36211c6fdbd8SKent Overstreet case SEEK_HOLE: 36225c1ef830SKent Overstreet ret = bch2_seek_hole(file, offset); 36235c1ef830SKent Overstreet break; 36245c1ef830SKent Overstreet default: 36255c1ef830SKent Overstreet ret = -EINVAL; 36265c1ef830SKent Overstreet break; 36271c6fdbd8SKent Overstreet } 36281c6fdbd8SKent Overstreet 36295c1ef830SKent Overstreet return bch2_err_class(ret); 36301c6fdbd8SKent Overstreet } 36311c6fdbd8SKent Overstreet 36321c6fdbd8SKent Overstreet void bch2_fs_fsio_exit(struct bch_fs *c) 36331c6fdbd8SKent Overstreet { 36341c6fdbd8SKent Overstreet bioset_exit(&c->dio_write_bioset); 36351c6fdbd8SKent Overstreet bioset_exit(&c->dio_read_bioset); 36361c6fdbd8SKent Overstreet bioset_exit(&c->writepage_bioset); 36371c6fdbd8SKent Overstreet } 36381c6fdbd8SKent Overstreet 36391c6fdbd8SKent Overstreet int bch2_fs_fsio_init(struct bch_fs *c) 36401c6fdbd8SKent Overstreet { 36411c6fdbd8SKent Overstreet int ret = 0; 36421c6fdbd8SKent Overstreet 36431c6fdbd8SKent Overstreet pr_verbose_init(c->opts, ""); 36441c6fdbd8SKent Overstreet 36451c6fdbd8SKent Overstreet if (bioset_init(&c->writepage_bioset, 36469a3df993SKent Overstreet 4, offsetof(struct bch_writepage_io, op.wbio.bio), 36471c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 36481c6fdbd8SKent Overstreet bioset_init(&c->dio_read_bioset, 36491c6fdbd8SKent Overstreet 4, offsetof(struct dio_read, rbio.bio), 36501c6fdbd8SKent Overstreet BIOSET_NEED_BVECS) || 36511c6fdbd8SKent Overstreet bioset_init(&c->dio_write_bioset, 36529a3df993SKent Overstreet 4, offsetof(struct dio_write, op.wbio.bio), 36531c6fdbd8SKent Overstreet BIOSET_NEED_BVECS)) 36541c6fdbd8SKent Overstreet ret = -ENOMEM; 36551c6fdbd8SKent Overstreet 36561c6fdbd8SKent Overstreet pr_verbose_init(c->opts, "ret %i", ret); 36571c6fdbd8SKent Overstreet return ret; 36581c6fdbd8SKent Overstreet } 36591c6fdbd8SKent Overstreet 36601c6fdbd8SKent Overstreet #endif /* NO_BCACHEFS_FS */ 3661