f2fs: support block size smaller than PAGE_SIZE
Subsystems (2)
v2 cover f2fs: support block size smaller than PAGE_SIZE
From: Daeho Jeong <[email protected]>
F2FS has required the filesystem block size to equal PAGE_SIZE. On a kernel with 16KB pages, this means an existing F2FS image with 4KB blocks cannot be mounted, e.g. the userdata partition of an Android device that moves from a 4KB page kernel to a 16KB page kernel.
This series lets F2FS mount and use a filesystem whose block size is smaller than PAGE_SIZE, such as 4KB blocks on a 16KB page kernel. The on-disk format does not change.
With subpage blocks, even an order-0 folio holds several blocks, so the series builds on the large folio support from Nanzhe Zhao [1] and tracks uptodate and dirty state per block in f2fs_folio_state for any folio that holds more than one block. Most paths then work in blocks instead of pages:
- Patches 1-2 convert more page based index and size calculations to block based ones, and add the superblock and mount infrastructure. Subpage block sizes are still rejected at this point.
- Patch 3 extends f2fs_folio_state with per-block state, identified by a magic number, and allocates it on demand with __GFP_NOFAIL.
- Patches 4-7 handle directories, the per-inode dirty counters, the read path and GC, and the write path and writeback per block.
- Patches 8-19 adapt the remaining features: range operations and mmap, pinned files, collapse/insert range, move range, defragmentation, swapfile, fs-verity, atomic writes, inline data, online resize and compression, and remove the -EOPNOTSUPP checks that patch 2 added for the features that need them.
- Patch 20 stops rejecting subpage block sizes.
When the block size equals PAGE_SIZE, the behavior does not change, except for the fixes noted in the individual patches. The following are not limited to subpage blocks or large folios:
- Patch 1: __complete_revoke_list() and __clone_blkaddrs() compute offsets in 64 bits on 32-bit architectures.
- Patch 8: an extending direct write updates i_size in ->end_io(), before iomap invalidates the page cache of the written range.
- Patch 10: collapse and insert range hold i_gc_rwsem[WRITE] and invalidate_lock until the moved blocks are written back and the page cache is dropped.
- Patch 11: move range zeroes the cached data past the old EOF of the destination before extending it, and drops the source blocks from the page cache also after an exchange that failed part way.
- Patch 12: defragmentation reads compressed files through ->read_folio(). A block of a compressed cluster that was not in the page cache was read without decompression and written back as file data.
- Patch 15: aborting atomic writes holds i_gc_rwsem[WRITE] and invalidate_lock.
A few of the patches adjust code from the large folio series [1]:
- f2fs_folio_has_ffs() checks the folio state magic instead of requiring a large folio, since order-0 multi-block folios also carry a folio state, and the page private flags can legitimately be set before a state is attached.
- f2fs_ffs_find_or_alloc() cannot fail with __GFP_NOFAIL, so the allocation failure handling of its callers is removed.
- The inode dirty blocks are counted from the dirty bits of the folio state, and ->dirty_folio() marks the uptodate blocks of a folio dirty, since mm does not tell which blocks were modified.
- Defragmentation and swapfile activation mark only the target block dirty instead of the whole page.
Changes in v2:
Addressed the review comments from Chao Yu and Nanzhe Zhao, the warning reported by the kernel test robot, and the findings of the Sashiko review. v1 is at [2].
- Patch 1 (block based calculations): rename the subject ("in more paths"). Use F2FS_BLK_ALIGN() in dir_blocks(). Compute the new i_size in __clone_blkaddrs() in 64 bits.
- Patch 2 (superblock and mount): probe the 2nd superblock from the smallest block size and stop at the first one found, so that a stale superblock of a larger block size is not picked, and return its error instead of the error of the last probe. Bound log_sectorsize by log_blocksize and remove F2FS_MAX_LOG_SECTOR_SIZE. Check the requested inline_xattr_size in f2fs_check_opt_consistency(). Do not set *recovery when no valid superblock is found. Report a failed read of a superblock again.
- Patch 3 (folio state): remove the unused variable in prepare_large_folio_write_begin(). Count the submitted blocks in nwritten also on error. Do not clear the uptodate flag of the folio of an inline inode in f2fs_invalidate_folio(). Explain the use of __GFP_NOFAIL in the commit message.
- Patch 4 (directories): read the name length in f2fs_delete_entry() under the folio lock. Clear the gcing flag of the folio when its last dirty block is freed. Simplify f2fs_dentry_to_lblk() and use f2fs_folio_lblk_address().
- Patch 5 (dirty counters): commit message only.
- Patch 6 (read path and GC): keep the folio state of a folio that is not fully uptodate in ->release_folio while the folio has other users. Read the block again in f2fs_get_read_data_folio() and f2fs_find_data_folio() when the folio state was released while the folio was unlocked, instead of failing. Do not map the blocks past EOF in f2fs_read_data_large_folio(). Read under memalloc_nofs in ra_data_block(), where GC locks are held. Rename f2fs_submit_page_read_sync() to f2fs_submit_block_read_sync().
- Patch 7 (write path and writeback): count the inode dirty blocks from the dirty bits of the folio state instead of per folio. Make ->dirty_folio() mark the uptodate blocks within EOF dirty, and add f2fs_folio_mark_dirty() for the updates made by f2fs, which mark only the blocks they modify. Keep f2fs_balance_fs_folio_locked() in write_begin instead of balancing after the folio is unlocked. Drop the pending write that a failed submission leaves behind in f2fs_convert_inline_folio(). Keep the folio dirty and accounted when flush_inline_data() fails.
- Patch 8 (range operations and mmap): update i_size of an extending direct write in ->end_io(). In page_mkwrite, reserve blocks only where it may allocate, i.e. not for a pinned file or a compressed cluster, do not balance with the folio locked, and read the blocks that are not uptodate after a partial invalidation. Check the dirty state of a reserved block only for SEEK_DATA.
- Patch 9 (pinned files): keep failing page_mkwrite on a device aliasing file with -EIO.
- Patch 10 (collapse and insert range): hold i_gc_rwsem[WRITE] and invalidate_lock from before the first writeback until the moved blocks are written back and the page cache is dropped.
- Patch 11 (move range): zero the cached data past the old EOF of the destination with f2fs_zero_post_eof_page() before the move. Drop the source blocks from the page cache also after an exchange that failed part way. Call pagecache_isize_extended() for the gap between the old EOF and the moved data.
- Patch 12 (defragmentation): read compressed files through ->read_folio() as well, and lock and redirty the folio that was read instead of looking it up again (f2fs_get_defrag_folio()).
- Patch 13 (swapfile): use f2fs_folio_mark_dirty().
- Patch 14 (fs-verity): stop the data reads of a verity file at i_size in f2fs_read_data_large_folio(). Do not zero the blocks past i_size on read while verity is being enabled.
- Patch 15 (atomic writes): hold i_gc_rwsem[WRITE] and invalidate_lock while aborting atomic writes, except at eviction.
- Patch 16 (inline data): redirty the folio and stop on any error of the inline writeback in f2fs_write_cache_folios(). Note the effect on inline inodes with large folios in the commit message.
- Patch 18 (compression): raise the minimum cluster size only when a page has more than four blocks, reject a smaller compress_log_size in the mount option and in F2FS_IOC_SET_COMPRESS_OPTION, fail the open of a file that already has a smaller cluster with -EOPNOTSUPP instead of failing its inode sanity check, and document it. Dirty every block of a partially dirty compressed cluster before it is rewritten. Attach a folio state to the multi-block folios of a rewritten cluster before they are dirtied. Count a read folio of a compressed file once in the iostat. Do not cut the range that redirty_blocks() dirties at EOF. Remove inode_inc_dirty_block() and inode_dec_dirty_block().
- Patches 17, 19 and 20: no change.
- The patches also use the F2FS_BLK_* helpers in more places, as suggested in the review.
Testing:
Built every patch for x86_64, arm64 (16KB pages), arm and i386.
Ran the xfstests auto group (excluding the clone, dedupe and thin groups) on arm64 QEMU with mount options "discard,fsync_mode=strict" in these combinations of page size / block size:
- 16KB/4KB: default, compression (lz4, zstd, lz4hc, lzo-rle with 64KB clusters and checksum), compression+encryption, encryption+verity, mode=lfs, atgc
- 4KB/4KB: default, compression, encryption+verity,
compression+encryption, mode=lfs
- 16KB/16KB: default, compression, encryption+verity, compression+encryption, mode=lfs
plus generic/019, 521, 522 and 780 for each combination, and a 24 hour power-off recovery test with fsstress for each of the three combinations.
There were no crashes or hangs, and the power-off recovery tests found no corruption. Two failures are worth a note, and neither depends on this series:
- generic/455 failed once on 16KB/4KB with encryption+verity, because fsync() returned before its node block was written. [3] fixes it.
- generic/551 was killed by the OOM killer in three of the compression runs. It is killed the same way without this series.
Also ran:
- The xfstests quick group with lockdep (PROVE_LOCKING and DEBUG_ATOMIC_SLEEP) on 16KB/4KB, default and compression. The only reports were in generic/574 with compression, where f2fs_decompress_cluster() may sleep in the read completion. This happens without this series as well, and [4] fixes it.
- 16 processes writing through mmap at the same time until the filesystem is full, on a 1GB image with 16KB pages and 4KB blocks, with and without large folios and with and without mode=lfs, as asked in the review of v1. They got SIGBUS only after the free space ran out, the data read back was correct, and fsck found no error.
- A 32-bit ARM kernel (4KB pages) with files beyond 4GB.
[1] https://lore.kernel.org/linux-f2fs-devel/[email protected]/
[2] https://lore.kernel.org/linux-f2fs-devel/[email protected]/
[3] https://lore.kernel.org/linux-f2fs-devel/[email protected]/
[4] https://lore.kernel.org/linux-f2fs-devel/[email protected]/
Daeho Jeong (20):
f2fs: use block-based index and size calculations in more paths
f2fs: support subpage block size superblock and mount
f2fs: extend f2fs_folio_state for subpage blocks
f2fs: support subpage blocks in directory operations
f2fs: convert inode dirty page counters to dirty block counters
f2fs: support subpage blocks in data read path and GC
f2fs: support subpage blocks in data write path and writeback
f2fs: adapt range operations and mmap for subpage blocks
f2fs: support subpage blocks in pinned files
f2fs: support subpage blocks in collapse and insert range
f2fs: support subpage blocks in move range
f2fs: support subpage blocks in defragmentation
f2fs: support subpage blocks in swapfile activation
f2fs: support subpage blocks in fs-verity enablement
f2fs: support subpage blocks in atomic writes
f2fs: preserve inline data in subpage block mode
f2fs: support subpage blocks in online resize
f2fs: support subpage blocks in compression
f2fs: count compress cache entries in pages for its memory limit
f2fs: enable subpage block size support
Documentation/filesystems/f2fs.rst | 7 +- fs/f2fs/checkpoint.c | 67 +- fs/f2fs/compress.c | 569 ++++++--- fs/f2fs/data.c | 1725 ++++++++++++++++++---------- fs/f2fs/dir.c | 126 +- fs/f2fs/f2fs.h | 237 ++-- fs/f2fs/file.c | 716 ++++++++---- fs/f2fs/gc.c | 103 +- fs/f2fs/inline.c | 23 +- fs/f2fs/inode.c | 3 +- fs/f2fs/node.c | 26 +- fs/f2fs/segment.c | 14 +- fs/f2fs/super.c | 180 ++- fs/f2fs/verity.c | 5 - include/linux/f2fs_fs.h | 2 +- 15 files changed, 2494 insertions(+), 1309 deletions(-)
base-commit: b9d7379d10d43386b59082da8c9a0b111e054781
Signature · 1 line
2.56.0.611.g1030f0b940-goog
PATCH v2 1/20 f2fs: use block-based index and size calculations in more paths
From: Daeho Jeong <[email protected]>
Several paths still convert between file offsets and block indices with PAGE_SHIFT or PAGE_SIZE, assuming that a page holds exactly one filesystem block. Use the F2FS_BLK_* helpers there so that they stay correct when the block size is smaller than PAGE_SIZE.
No functional change when the block size equals PAGE_SIZE, except on 32-bit architectures: __complete_revoke_list() now computes the truncation offset and __clone_blkaddrs() the new i_size in 64 bits, so that they no longer overflow beyond 4GB.
--- fs/f2fs/data.c | 5 +++-- fs/f2fs/dir.c | 3 +-- fs/f2fs/file.c | 50 +++++++++++++++++++++++------------------------ fs/f2fs/segment.c | 5 +++-- 4 files changed, 32 insertions(+), 31 deletions(-)
fs/f2fs/data.c +3 -2
@@ -1897,8 +1897,9 @@ struct folio *f2fs_get_new_data_folio(struct inode *inode, } got_it:if (new_i_size &&i_size_read (inode) < - ((loff_t)(index +1 ) <<PAGE_SHIFT )) -f2fs_i_size_write (inode, ((loff_t)(index +1 ) <<PAGE_SHIFT )); +F2FS_BLK_TO_BYTES (F2FS_I_SB (inode), index +1 )) +f2fs_i_size_write (inode, +F2FS_BLK_TO_BYTES (F2FS_I_SB (inode), index +1 ));return folio; }
fs/f2fs/dir.c +1 -2
@@ -39,8 +39,7 @@ extern struct kmem_cache *f2fs_cf_name_slab;static unsigned long dir_blocks (struct inode *inode) { -return ((unsigned long long ) (i_size_read (inode) +PAGE_SIZE -1 )) - >>PAGE_SHIFT ; +return F2FS_BLK_ALIGN (F2FS_I_SB (inode),i_size_read (inode)); }static unsigned int dir_buckets (unsigned int level,int dir_level)
fs/f2fs/file.c +25 -25
143 lines not on this page · raw message
fs/f2fs/segment.c +3 -2
@@ -285,6 +285,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index,static void __complete_revoke_list (struct inode *inode,struct list_head *head,bool revoke) { +struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct revoke_entry *cur, *tmp;pgoff_t start_index =0 ;bool truncate =is_inode_flag_set (inode,FI_ATOMIC_REPLACE );
@@ -303,7 +304,7 @@ static void __complete_revoke_list(struct inode *inode, struct list_head *head, }if (!revoke && truncate) -f2fs_do_truncate_blocks (inode, start_index *PAGE_SIZE , false); +f2fs_do_truncate_blocks (inode,F2FS_BLK_TO_BYTES (sbi, start_index), false); }static int __f2fs_commit_atomic_write (struct inode *inode)
@@ -315,7 +316,7 @@ static int __f2fs_commit_atomic_write(struct inode *inode)struct list_head revoke_list;block_t blkaddr;struct dnode_of_data dn; -pgoff_t len =DIV_ROUND_UP (i_size_read (inode),PAGE_SIZE ); +pgoff_t len =F2FS_BLK_ALIGN (sbi,i_size_read (inode));pgoff_t off =0 , blen, index;int ret =0 , i;
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 01/20] f2fs: use block-based index and size calculations in more paths
Quoted 7 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
Several paths still convert between file offsets and block indices with
PAGE_SHIFTorPAGE_SIZE, assuming that a page holds exactly one filesystem block. Use the F2FS_BLK_* helpers there so that they stay correct when the block size is smaller thanPAGE_SIZE.
Quoted 6 lines
No functional change when the block size equals
PAGE_SIZE, except on 32-bit architectures:__complete_revoke_list()now computes the truncation offset and__clone_blkaddrs()the newi_sizein 64 bits, so that they no longer overflow beyond 4GB.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 2/20 f2fs: support subpage block size superblock and mount
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
Add the infrastructure to mount F2FS with a block size smaller than PAGE_SIZE, e.g. 4KB blocks on a 16KB page kernel.
- Add helpers to convert between block indices, folio indices and offsets within a folio.
- Take the block size from the 1st superblock, which is at the same offset for every block size. If it is invalid, probe the 2nd superblock with each supported block size from the smallest, and stop at the first one found, even if it is invalid, so that a stale superblock of a larger block size is not picked.
- Allow log_blocksize up to PAGE_SHIFT, and write back only the target superblock block instead of the whole folio.
- Bound log_sectorsize by log_blocksize, so that log_sectors_per_block + log_sectorsize cannot wrap around, and remove F2FS_MAX_LOG_SECTOR_SIZE, which has no user left.
- Check the requested inline_xattr_size, not the current one, against the block size in f2fs_check_opt_consistency().
- Keep rejecting subpage block sizes, and return -EOPNOTSUPP for the features that do not support them yet. Later patches add the support and lift these restrictions.
--- fs/f2fs/compress.c | 2 + fs/f2fs/f2fs.h | 61 +++++++++++--- fs/f2fs/file.c | 42 ++++++++++ fs/f2fs/super.c | 178 +++++++++++++++++++++++++++++----------- fs/f2fs/verity.c | 3 + include/linux/f2fs_fs.h | 2 +- 6 files changed, 226 insertions(+), 62 deletions(-)
fs/f2fs/compress.c +2 -0
@@ -550,6 +550,8 @@ bool f2fs_is_compress_backend_ready(struct inode *inode) {if (!f2fs_compressed_file (inode))return true; +if (f2fs_has_subpage_blocks (F2FS_I_SB (inode))) +return false;return f2fs_cops[F2FS_I (inode)->i_compress_algorithm]; }
fs/f2fs/f2fs.h +50 -11
98 lines not on this page · raw message
fs/f2fs/file.c +42 -0
114 lines not on this page · raw message
fs/f2fs/super.c +128 -50
283 lines not on this page · raw message
fs/f2fs/verity.c +3 -0
@@ -94,6 +94,9 @@ static int f2fs_begin_enable_verity(struct file *filp)struct inode *inode =file_inode (filp);int err; +if (f2fs_has_subpage_blocks (F2FS_I_SB (inode))) +return -EOPNOTSUPP ; +if (f2fs_verity_in_progress (inode))return -EBUSY ;
include/linux/f2fs_fs.h +1 -1
@@ -13,8 +13,8 @@#define F2FS_SUPER_OFFSET 1024/* byte-size offset */ #define F2FS_MIN_LOG_SECTOR_SIZE 9/* 9 bits for 512 bytes */ -#define F2FS_MAX_LOG_SECTOR_SIZE PAGE_SHIFT/* Max is Block Size */ #define F2FS_MIN_LOG_BLOCKSIZE 12 +#define F2FS_MAX_LOG_BLOCKSIZE PAGE_SHIFT#define F2FS_MIN_BLKSIZE 4096UL#define F2FS_MAX_BLKSIZE PAGE_SIZE#define F2FS_MAX_EXTENSION 64/* # of extension entries */
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 02/20] f2fs: support subpage block size superblock and mount
Quoted 23 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
Add the infrastructure to mount F2FS with a block size smaller than
PAGE_SIZE, e.g. 4KB blocks on a 16KB page kernel.- Add helpers to convert between block indices, folio indices and offsets within a folio.
- Take the block size from the 1st superblock, which is at the same offset for every block size. If it is invalid, probe the 2nd superblock with each supported block size from the smallest, and stop at the first one found, even if it is invalid, so that a stale superblock of a larger block size is not picked.
- Allowlog_blocksizeup toPAGE_SHIFT, and write back only the target superblock block instead of the whole folio.
- Boundlog_sectorsizebylog_blocksize, so thatlog_sectors_per_block+log_sectorsizecannot wrap around, and removeF2FS_MAX_LOG_SECTOR_SIZE, which has no user left.
- Check the requestedinline_xattr_size, not the current one, against the block size inf2fs_check_opt_consistency().
- Keep rejecting subpage block sizes, and return-EOPNOTSUPPfor the features that do not support them yet. Later patches add the support and lift these restrictions.
Quoted 1 line
Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 3/20 f2fs: extend f2fs_folio_state for subpage blocks
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
With subpage blocks, even an order-0 folio holds several blocks, so track uptodate and dirty state per block for any folio that holds more than one block, not only for large folios.
- Identify the folio state by a magic number. An order-0 folio can carry one now, so folio_test_large() no longer tells it apart from the context of a compressed page, which folio->private can also point to. Carry the page private flags over when the state is attached.
- Make the uptodate and dirty helpers take block indices, and attach the state on demand in them. Some of their callers cannot return an error, e.g. f2fs_set_link() and f2fs_delete_entry() once a directory folio holds several blocks, so allocate the state with __GFP_NOFAIL, as ifs_alloc() does in iomap, and drop the allocation failure handling of the callers. The write paths attach the state before a folio is dirtied, so writeback does not normally have to allocate it.
- Handle per-block state in invalidation, dirtying and writeback.
- Clear PAGECACHE_TAG_TOWRITE as well as PAGECACHE_TAG_DIRTY in f2fs_clear_page_cache_dirty_tag().
Some of these changes also apply to large folios when the block size equals PAGE_SIZE.
--- fs/f2fs/data.c | 440 ++++++++++++++++++++++++++-------------------- fs/f2fs/f2fs.h | 35 ++-- fs/f2fs/file.c | 18 +- fs/f2fs/segment.c | 2 +- 4 files changed, 269 insertions(+), 226 deletions(-)
fs/f2fs/data.c +247 -193
@@ -96,15 +96,14 @@ struct bio_post_read_ctx {static bool __ffs_mark_subrange_uptodate (struct folio *folio,struct f2fs_folio_state *ffs,size_t offset,size_t len) { -unsigned int nr_subpages =folio_nr_pages (folio); -unsigned int start, end; - - start = offset >>PAGE_SHIFT ; - end = (offset + len +PAGE_SIZE -1 ) >>PAGE_SHIFT ; - end =min (end, nr_subpages); +struct f2fs_sb_info *sbi =F2FS_F_SB (folio); +unsigned int nr_blocks =f2fs_blocks_per_folio (sbi, folio); +unsigned int start =F2FS_BYTES_TO_BLK (sbi, offset); + unsigned int end = min_t(unsigned int, +F2FS_BLK_ALIGN (sbi, offset + len), nr_blocks);bitmap_set (ffs->state, start, end - start); -return bitmap_full (ffs->state, nr_subpages); +return bitmap_full (ffs->state, nr_blocks); }/*
@@ -122,7 +121,7 @@ static bool __ffs_mark_subrange_uptodate(struct folio *folio,* release the bio's reference to the decompress_io_ctx of the page's cluster. */ /* -* Update read_pages_pending. +* Update read_blocks_pending. */ static inline void f2fs_update_read_folio_pending (struct folio *folio,int nr) {
@@ -134,7 +133,7 @@ static inline void f2fs_update_read_folio_pending(struct folio *folio, int nr) ffs = (struct f2fs_folio_state *)folio->private;spin_lock_irqsave (&ffs->state_lock, flags); - ffs->read_pages_pending += nr; + ffs->read_blocks_pending += nr;spin_unlock_irqrestore (&ffs->state_lock, flags); }
@@ -173,8 +172,8 @@ static void f2fs_finish_read_bio(struct bio *bio, bool in_task)if (uptodate) uptodate =__ffs_mark_subrange_uptodate (folio, ffs, fi.offset, fi.length); - ffs->read_pages_pending -= nr_pages; - finished = !ffs->read_pages_pending; + ffs->read_blocks_pending -= nr_pages; + finished = !ffs->read_blocks_pending;spin_unlock_irqrestore (&ffs->state_lock, flags); }
@@ -372,18 +371,18 @@ bool f2fs_ffs_has_dirty_subpage(struct folio *folio) {struct f2fs_folio_state *ffs;unsigned long flags; -unsigned int nr_subpages; +unsigned int nr_blocks;bool dirty;if (!f2fs_folio_has_ffs (folio))return false; ffs = (struct f2fs_folio_state *)folio->private; - nr_subpages =folio_nr_pages (folio); + nr_blocks =f2fs_blocks_per_folio (F2FS_F_SB (folio), folio);spin_lock_irqsave (&ffs->state_lock, flags); - dirty =find_next_bit (ffs->state,2 * nr_subpages, - nr_subpages) <2 * nr_subpages; + dirty =find_next_bit (ffs->state,2 * nr_blocks, + nr_blocks) <2 * nr_blocks;spin_unlock_irqrestore (&ffs->state_lock, flags);return dirty;
@@ -402,7 +401,7 @@ static void f2fs_ffs_put_write_pending(struct folio *folio, unsigned int nr) goto release; ffs = (struct f2fs_folio_state *)folio->private; -if (!atomic_sub_and_test (nr, &ffs->write_pages_pending)) +if (!atomic_sub_and_test (nr, &ffs->write_blocks_pending))return ; release:if (!f2fs_ffs_has_dirty_subpage (folio))
@@ -1572,7 +1571,7 @@ static int f2fs_submit_page_read_sync(struct inode *inode, struct folio *folio, REQ_OP_READ, true);/* Wait until all bios have completed (pending drops back to our bias). */ -while (READ_ONCE (ffs->read_pages_pending) !=1 ) +while (READ_ONCE (ffs->read_blocks_pending) !=1 )f2fs_io_schedule_timeout (DEFAULT_SCHEDULE_TIMEOUT );/* Remove the bias. */
@@ -1693,12 +1692,8 @@ struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index,if (IS_ERR (folio))return folio; -if (folio_test_large (folio)) { -if (!f2fs_ffs_find_or_alloc (folio)) { - err = -ENOMEM ; - goto put_err; - } - } +if (folio_test_large (folio)) +f2fs_ffs_find_or_alloc (folio);if (f2fs_lookup_read_extent_cache_block (inode, index, &dn.data_blkaddr)) {
@@ -1821,12 +1816,8 @@ struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index,* for a fully uptodate folio; a partially uptodate one cannot be * rebuilt and will fail the subpage check below. */ -if (folio_test_large (folio) && !f2fs_folio_has_ffs (folio)) { -if (!f2fs_ffs_find_or_alloc (folio)) { -f2fs_folio_put (folio, true); -return ERR_PTR (-ENOMEM ); - } - } +if (folio_test_large (folio) && !f2fs_folio_has_ffs (folio)) +f2fs_ffs_find_or_alloc (folio);if (unlikely (!f2fs_ffs_test_blk_uptodate (folio, index))) {f2fs_folio_put (folio, true);
@@ -1862,12 +1853,6 @@ struct folio *f2fs_get_new_data_folio(struct inode *inode,return ERR_PTR (-ENOMEM ); } -if (folio_test_large (folio) && !f2fs_ffs_find_or_alloc (folio)) { -f2fs_folio_put (folio, true); -f2fs_put_cache (ientry, true); -return ERR_PTR (-ENOMEM ); - } -set_new_dnode (&dn, inode, ientry,NULL ,0 ); err =f2fs_reserve_block (&dn, index);if (err) {
@@ -2900,29 +2885,43 @@ int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,struct f2fs_folio_state *f2fs_ffs_find_or_alloc (struct folio *folio) { -struct f2fs_folio_state *ffs; -unsigned int nr_subpages =folio_nr_pages (folio); -unsigned long private = (unsigned long )folio->private; - -f2fs_bug_on (F2FS_F_SB (folio), !folio_test_large (folio)); -f2fs_bug_on (F2FS_F_SB (folio), -test_bit (F2FS_FOLIO_PRIVATE_NOT_POINTER , &private)); +struct f2fs_sb_info *sbi =F2FS_F_SB (folio); +struct f2fs_folio_state *ffs = folio->private; +unsigned int nr_blocks; +unsigned long private_flags;if (f2fs_folio_has_ffs (folio)) -return (struct f2fs_folio_state *)folio->private; +return ffs; - ffs =f2fs_kmalloc (F2FS_F_SB (folio), -struct_size (ffs, state,BITS_TO_LONGS (2 * nr_subpages)), -GFP_NOFS | __GFP_ZERO); -if (!ffs) -return NULL ; +/* Carry over the page private flags kept in folio->private. */ + private_flags =f2fs_folio_get_private_flags (folio); + nr_blocks =f2fs_blocks_per_folio (sbi, folio); + ffs =kzalloc (struct_size (ffs, state,BITS_TO_LONGS (2 * nr_blocks)), +GFP_NOIO | __GFP_NOFAIL); + + ffs->magic =F2FS_FOLIO_STATE_MAGIC ;spin_lock_init (&ffs->state_lock); + ffs->private_flags = private_flags;if (folio_test_uptodate (folio)) -bitmap_set (ffs->state,0 , nr_subpages); -if (folio_test_dirty (folio)) -bitmap_set (ffs->state, nr_subpages, nr_subpages); +bitmap_set (ffs->state,0 , nr_blocks); +if (folio_test_dirty (folio)) { +struct inode *inode = folio->mapping->host; +pgoff_t last_lblk =F2FS_BLK_ALIGN (sbi,i_size_read (inode)); +pgoff_t first_lblk =f2fs_folio_lblk (sbi, folio); +pgoff_t valid_blocks =0 ; +if (f2fs_verity_in_progress (inode)) + valid_blocks = nr_blocks; +else if (first_lblk < last_lblk) + valid_blocks =min_t (pgoff_t, nr_blocks, + last_lblk - first_lblk); + +if (valid_blocks) +bitmap_set (ffs->state, nr_blocks, valid_blocks); + } + +folio_detach_private (folio);folio_attach_private (folio, ffs);return ffs; }
@@ -2940,17 +2939,17 @@ static void ffs_detach_free(struct folio *folio)if (!ffs)return ; -WARN_ON_ONCE (ffs->read_pages_pending !=0 ); -WARN_ON_ONCE (atomic_read (&ffs->write_pages_pending)); +WARN_ON_ONCE (ffs->read_blocks_pending !=0 ); +WARN_ON_ONCE (atomic_read (&ffs->write_blocks_pending)); + ffs->magic =0 ;kfree (ffs); }bool f2fs_ffs_test_blk_uptodate (const struct folio *folio,pgoff_t index) { +struct f2fs_sb_info *sbi =F2FS_M_SB (folio->mapping);struct f2fs_folio_state *ffs; -unsigned int idx; - -f2fs_bug_on (F2FS_F_SB (folio), !folio_contains (folio, index)); +pgoff_t offset;if (folio_test_uptodate (folio))return true;
@@ -2958,63 +2957,60 @@ bool f2fs_ffs_test_blk_uptodate(const struct folio *folio, pgoff_t index)if (!f2fs_folio_has_ffs (folio))return false; - ffs = (struct f2fs_folio_state *)folio->private; - idx = index - folio->index; -return test_bit (idx, ffs->state); + ffs = folio->private; + offset =f2fs_folio_blk_offset (sbi, folio, index); +if (WARN_ON_ONCE (offset >=f2fs_blocks_per_folio (sbi, folio))) +return false; + +if (test_bit (offset, ffs->state)) { +/* Order the bit test before reading the block data */ +smp_rmb (); +return true; + } +return false; }void f2fs_ffs_mark_subrange_uptodate (struct folio *folio,size_t offset,size_t len) { +struct f2fs_sb_info *sbi =F2FS_F_SB (folio);struct f2fs_folio_state *ffs;unsigned long flags;bool mark_uptodate = false;f2fs_bug_on (F2FS_F_SB (folio), offset + len >folio_size (folio)); -if (!f2fs_folio_has_ffs (folio)) { +if (!f2fs_is_multiblock_folio (sbi, folio)) {folio_mark_uptodate (folio);return ; } - ffs = (struct f2fs_folio_state *)folio->private; -spin_lock_irqsave (&ffs->state_lock, flags); - mark_uptodate =__ffs_mark_subrange_uptodate (folio, ffs, offset, len) && - !ffs->read_pages_pending; -spin_unlock_irqrestore (&ffs->state_lock, flags); -if (mark_uptodate) +/* +* Without f2fs_folio_state, the folio uptodate flag covers every block, +* so marking the whole folio needs no allocation. This keeps bio +* completion paths such as decompression free of allocation. +*/ +if (!f2fs_folio_has_ffs (folio) && !offset && len >=folio_size (folio)) {folio_mark_uptodate (folio); -} - -static void ffs_clear_subrange_uptodate (struct folio *folio, -size_t offset,size_t len) -{ -struct f2fs_folio_state *ffs; -unsigned int nr_subpages, start, end; -unsigned long flags; - -f2fs_bug_on (F2FS_F_SB (folio), offset + len >folio_size (folio)); - -if (!f2fs_folio_has_ffs (folio))return ; + } - ffs = (struct f2fs_folio_state *)folio->private; - nr_subpages =folio_nr_pages (folio); - start = offset >>PAGE_SHIFT ; - end = (offset + len +PAGE_SIZE -1 ) >>PAGE_SHIFT ; - end =min (end, nr_subpages); - + ffs =f2fs_ffs_find_or_alloc (folio);spin_lock_irqsave (&ffs->state_lock, flags); -bitmap_clear (ffs->state, start, end - start); + mark_uptodate =__ffs_mark_subrange_uptodate (folio, ffs, offset, len) && + !ffs->read_blocks_pending;spin_unlock_irqrestore (&ffs->state_lock, flags); +if (mark_uptodate) +folio_mark_uptodate (folio); }static void f2fs_clear_truncated_page_state (struct folio *folio,pgoff_t i) { +struct f2fs_sb_info *sbi =F2FS_F_SB (folio); +if (f2fs_folio_has_ffs (folio)) { -ffs_clear_subrange_uptodate (folio, - (loff_t )i <<PAGE_SHIFT ,PAGE_SIZE ); -folio_clear_uptodate (folio); +f2fs_ffs_clear_subrange_uptodate (folio, +F2FS_BLK_TO_BYTES (sbi, i),F2FS_BLKSIZE (sbi)); }else {folio_clear_uptodate (folio);folio_clear_f2fs_gcing (folio);
@@ -3023,33 +3019,36 @@ static void f2fs_clear_truncated_page_state(struct folio *folio, pgoff_t i)bool f2fs_ffs_test_blk_dirty (const struct folio *folio,pgoff_t index) { +struct f2fs_sb_info *sbi =F2FS_M_SB (folio->mapping);struct f2fs_folio_state *ffs; -unsigned int idx, nr_subpages; - -f2fs_bug_on (F2FS_F_SB (folio), !folio_contains (folio, index)); +unsigned int nr_blocks; +pgoff_t offset;if (!f2fs_folio_has_ffs (folio))return folio_test_dirty (folio); - ffs = (struct f2fs_folio_state *)folio->private; - nr_subpages =folio_nr_pages (folio); - idx = index - folio->index; -return test_bit (nr_subpages + idx, ffs->state); + ffs = folio->private; + nr_blocks =f2fs_blocks_per_folio (sbi, folio); + offset =f2fs_folio_blk_offset (sbi, folio, index); +if (WARN_ON_ONCE (offset >= nr_blocks)) +return false; + +return test_bit (nr_blocks + offset, ffs->state); }static unsigned int __ffs_mark_subrange_dirty (struct folio *folio,struct f2fs_folio_state *ffs,size_t offset,size_t len) { -unsigned int nr_subpages =folio_nr_pages (folio); +struct f2fs_sb_info *sbi =F2FS_F_SB (folio); +unsigned int nr_blocks =f2fs_blocks_per_folio (sbi, folio);unsigned int start, end, before; - start = offset >>PAGE_SHIFT ; - end = (offset + len +PAGE_SIZE -1 ) >>PAGE_SHIFT ; - end =min (end, nr_subpages); + start =F2FS_BYTES_TO_BLK (sbi, offset); + end = min_t(unsigned int, F2FS_BLK_ALIGN(sbi, offset + len), nr_blocks); - before =bitmap_weight (ffs->state,2 * nr_subpages); -bitmap_set (ffs->state, nr_subpages + start, end - start); -return bitmap_weight (ffs->state,2 * nr_subpages) - before; + before =bitmap_weight (ffs->state,2 * nr_blocks); +bitmap_set (ffs->state, nr_blocks + start, end - start); +return bitmap_weight (ffs->state,2 * nr_blocks) - before; }/*
@@ -3062,16 +3061,17 @@ static unsigned int __ffs_mark_subrange_dirty(struct folio *folio,void f2fs_ffs_mark_subrange_dirty (struct folio *folio,size_t offset,size_t len) { +struct f2fs_sb_info *sbi =F2FS_F_SB (folio);struct f2fs_folio_state *ffs;unsigned int set;unsigned long flags;f2fs_bug_on (F2FS_F_SB (folio), offset + len >folio_size (folio)); -if (!f2fs_folio_has_ffs (folio)) +if (!f2fs_is_multiblock_folio (sbi, folio))return ; - ffs = (struct f2fs_folio_state *)folio->private; + ffs =f2fs_ffs_find_or_alloc (folio);spin_lock_irqsave (&ffs->state_lock, flags); set =__ffs_mark_subrange_dirty (folio, ffs, offset, len);
@@ -3081,22 +3081,54 @@ void f2fs_ffs_mark_subrange_dirty(struct folio *folio,f2fs_account_dirty_blocks (folio->mapping->host, set); } +void f2fs_ffs_clear_subrange_uptodate (struct folio *folio, +size_t offset,size_t len) +{ +struct f2fs_sb_info *sbi =F2FS_F_SB (folio); +struct f2fs_folio_state *ffs; +unsigned int nr_blocks, start, end; +unsigned long flags; + +f2fs_bug_on (sbi, offset + len >folio_size (folio)); + +if (!f2fs_folio_has_ffs (folio)) { +if (!folio_test_uptodate (folio)) +return ; +/* Without ffs, clearing the whole folio needs no allocation. */ +if (!offset && len >=folio_size (folio)) { +folio_clear_uptodate (folio); +return ; + } + } + + ffs =f2fs_ffs_find_or_alloc (folio); + nr_blocks =f2fs_blocks_per_folio (sbi, folio); + start =F2FS_BYTES_TO_BLK (sbi, offset); + end = min_t(unsigned int, F2FS_BLK_ALIGN(sbi, offset + len), nr_blocks); + +spin_lock_irqsave (&ffs->state_lock, flags); +bitmap_clear (ffs->state, start, end - start); +spin_unlock_irqrestore (&ffs->state_lock, flags); +if (folio_test_uptodate (folio)) +folio_clear_uptodate (folio); +} +static bool __ffs_clear_subrange_dirty (struct folio *folio,struct f2fs_folio_state *ffs,size_t offset,size_t len,unsigned int *cleared) { -unsigned int nr_subpages =folio_nr_pages (folio); +struct f2fs_sb_info *sbi =F2FS_F_SB (folio); +unsigned int nr_blocks =f2fs_blocks_per_folio (sbi, folio);unsigned int start, end, before; - start = offset >>PAGE_SHIFT ; - end = (offset + len +PAGE_SIZE -1 ) >>PAGE_SHIFT ; - end =min (end, nr_subpages); + start =F2FS_BYTES_TO_BLK (sbi, offset); + end = min_t(unsigned int, F2FS_BLK_ALIGN(sbi, offset + len), nr_blocks); - before =bitmap_weight (ffs->state,2 * nr_subpages); -bitmap_clear (ffs->state, nr_subpages + start, end - start); - *cleared = before -bitmap_weight (ffs->state,2 * nr_subpages); -return find_next_bit (ffs->state,2 * nr_subpages, nr_subpages) < -2 * nr_subpages; + before =bitmap_weight (ffs->state,2 * nr_blocks); +bitmap_clear (ffs->state, nr_blocks + start, end - start); + *cleared = before -bitmap_weight (ffs->state,2 * nr_blocks); +return find_next_bit (ffs->state,2 * nr_blocks, nr_blocks) < +2 * nr_blocks; }/**
@@ -3117,10 +3149,19 @@ bool f2fs_ffs_clear_subrange_dirty_and_test(struct folio *folio, size_t offset,unsigned int cleared =0 ;bool dirty; -if (!f2fs_folio_has_ffs (folio)) -return false; +if (!f2fs_folio_has_ffs (folio)) { +/* +* Without ffs, the folio dirty flag covers every block. Callers +* that clear the flag first (writeback, GC) must have ffs. +*/ +if (!folio_test_dirty (folio)) +return false; +/* Clearing the whole folio leaves no dirty block. */ +if (!offset && len >=folio_size (folio)) +return false; + } - ffs = (struct f2fs_folio_state *)folio->private; + ffs =f2fs_ffs_find_or_alloc (folio);spin_lock_irqsave (&ffs->state_lock, flags); dirty =__ffs_clear_subrange_dirty (folio, ffs, offset, len, &cleared);spin_unlock_irqrestore (&ffs->state_lock, flags);
@@ -3131,55 +3172,37 @@ bool f2fs_ffs_clear_subrange_dirty_and_test(struct folio *folio, size_t offset,return dirty; } -static unsigned int ffs_next_dirty_subpage (struct f2fs_folio_state *ffs, -const struct folio *folio,unsigned int start, -unsigned int end) -{ -unsigned int nr_subpages =folio_nr_pages (folio); - -return find_next_bit (ffs->state, nr_subpages + end +1 , - nr_subpages + start) - nr_subpages; -} - -static unsigned int ffs_next_clean_subpage (struct f2fs_folio_state *ffs, -const struct folio *folio,unsigned int start, -unsigned int end) -{ -unsigned int nr_subpages =folio_nr_pages (folio); - -return find_next_zero_bit (ffs->state, nr_subpages + end +1 , - nr_subpages + start) - nr_subpages; -} - -static unsigned int ffs_find_dirty_range (struct folio *folio, -u64 *range_start,u64 range_end) +bool f2fs_ffs_dirty_blk_range (struct folio *folio,unsigned int from, +unsigned int to,unsigned int *start, +unsigned int *count) { +struct f2fs_sb_info *sbi =F2FS_F_SB (folio);struct f2fs_folio_state *ffs; -unsigned int start, end, nr_pages; - -if (*range_start >= range_end) -return 0 ; - -if (!f2fs_folio_has_ffs (folio)) -return range_end - *range_start; - - ffs = (struct f2fs_folio_state *)folio->private; - start =offset_in_folio (folio, *range_start) >>PAGE_SHIFT ; - end =DIV_ROUND_UP (min_not_zero (offset_in_folio (folio, range_end), -folio_size (folio)),PAGE_SIZE ) -1 ; +unsigned int nr_blocks =f2fs_blocks_per_folio (sbi, folio); +unsigned long flags; - start =ffs_next_dirty_subpage (ffs, folio, start, end); -if (start > end) -return 0 ; +if (from >= to || to > nr_blocks) +return false; -if (start == end) - nr_pages =1 ; -else - nr_pages =ffs_next_clean_subpage (ffs, folio, - start +1 , end) - start; +if (!f2fs_folio_has_ffs (folio)) { +if (!folio_test_dirty (folio)) +return false; + *start = from; + *count = to - from; +return true; + } - *range_start =folio_pos (folio) + ((u64 )start <<PAGE_SHIFT ); -return (u64 )nr_pages <<PAGE_SHIFT ; + ffs = folio->private; +spin_lock_irqsave (&ffs->state_lock, flags); + *start =find_next_bit (ffs->state, nr_blocks + to, nr_blocks + from) - nr_blocks; +if (*start >= to) { +spin_unlock_irqrestore (&ffs->state_lock, flags); +return false; + } + *count =find_next_zero_bit (ffs->state, nr_blocks + to, + nr_blocks + *start) - nr_blocks - *start; +spin_unlock_irqrestore (&ffs->state_lock, flags); +return true; } static bool find_next_valid_block(const struct folio *folio,
@@ -3307,13 +3330,9 @@ static int f2fs_read_data_large_folio(struct inode *inode,/* * Allocate the folio state before doing anything with the * folio, so that the block mapping helpers below can rely on -* it, and fail early if memory is not available. +* it. */ ffs =f2fs_ffs_find_or_alloc (folio); -if (!ffs) { - ret = -ENOMEM ; - goto err_out; - } } index = folio->index; offset =0 ;
@@ -3393,7 +3412,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,continue ; } -/* We must increment read_pages_pending before possible BIOs submitting +/* We must increment read_blocks_pending before possible BIOs submitting * to prevent from premature folio_end_read() call on folio */ if (folio_test_large (folio)) {
@@ -3847,7 +3866,7 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)__folio_start_writeback (folio,folio_test_dirty (folio));if (f2fs_folio_has_ffs (folio)) { ffs = (struct f2fs_folio_state *)folio->private; -atomic_inc (&ffs->write_pages_pending); +atomic_inc (&ffs->write_blocks_pending); pending_added = true; }
@@ -3927,7 +3946,7 @@ static int f2fs_write_folio_dirty_range(struct folio *folio, int *submitted,if (f2fs_folio_has_ffs (folio)) { ffs = (struct f2fs_folio_state *)folio->private; -atomic_inc (&ffs->write_pages_pending); +atomic_inc (&ffs->write_blocks_pending); }set_new_dnode (&dn, dn_inode,NULL ,NULL ,0 );
@@ -4499,7 +4518,6 @@ static int f2fs_write_cache_folios(struct address_space *mapping,struct f2fs_lock_context lc;u64 pos =0 ;u64 end_pos =0 ; -u32 r_len =0 ;int err =0 ;int submitted =0 ;int nwritten =0 ;
@@ -4633,29 +4651,60 @@ static int f2fs_write_cache_folios(struct address_space *mapping,folio_start_writeback (folio); +/* +* PG_dirty was already cleared by writeback_iter(), and a +* single-block folio has no ffs dirty bitmap, so the dirty +* range cannot be derived. Write the whole folio. +*/ +if (!folio_test_large (folio)) { + err =f2fs_write_folio_dirty_range (folio, &submitted, + wbc, io_type, &pos, end_pos - pos); + folio_submitted += submitted; + nwritten += submitted; +if (err) + goto out; + goto written; + } + +if (!f2fs_folio_has_ffs (folio)) { + ffs =f2fs_ffs_find_or_alloc (folio); +f2fs_ffs_mark_subrange_dirty (folio,0 , end_pos - pos); + }else { + ffs = (struct f2fs_folio_state *)folio->private; + }/* * Large folios can be submitted as multiple bios, which may * complete while we are still submitting the remaining dirty -* ranges. Hold a bias on write_pages_pending to prevent +* ranges. Hold a bias on write_blocks_pending to prevent * folio_end_writeback() from being called early. */ -if (!bias_added &&f2fs_folio_has_ffs (folio)) { - ffs = (struct f2fs_folio_state *)folio->private; -WARN_ON_ONCE (atomic_read (&ffs->write_pages_pending) !=0 ); -atomic_inc (&ffs->write_pages_pending); +if (!bias_added) { +WARN_ON_ONCE (atomic_read (&ffs->write_blocks_pending) !=0 ); +atomic_inc (&ffs->write_blocks_pending); bias_added = true; } -while ((r_len =ffs_find_dirty_range (folio, &pos, end_pos))) { +unsigned int from =0 , start, count; +unsigned int nr_blocks =f2fs_blocks_per_folio (sbi, folio); + unsigned int to_blk = min_t(unsigned int, +F2FS_BLK_ALIGN (sbi, end_pos -folio_pos (folio)), + nr_blocks); + +while (f2fs_ffs_dirty_blk_range (folio, from, to_blk, + &start, &count)) { submitted =0 ; + pos =folio_pos (folio) +F2FS_BLK_TO_BYTES (sbi, start); err =f2fs_write_folio_dirty_range (folio, &submitted, - wbc, io_type, &pos, r_len); + wbc, io_type, &pos, +F2FS_BLK_TO_BYTES (sbi, count)); folio_submitted += submitted; nwritten += submitted;if (err) goto out; + from = start + count; } +written:/* If none of the subpages of a folio is submitted, keep the atomic flag */ if (folio_submitted && f2fs_is_atomic_file(inode) &&
@@ -5141,7 +5190,6 @@ static int prepare_large_folio_write_begin(struct inode *inode, unsigned int len) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode); -struct f2fs_folio_state *ffs;size_t ori_off =offset_in_folio (folio, pos);size_t need_off = ori_off;int err =0 ;
@@ -5170,9 +5218,7 @@ static int prepare_large_folio_write_begin(struct inode *inode, } } - ffs =f2fs_ffs_find_or_alloc (folio); -if (!ffs) -return -ENOMEM ; +f2fs_ffs_find_or_alloc (folio);/* Skip read if the folio is already fully uptodate or the write * covers the entire folio.
@@ -5248,8 +5294,7 @@ static int prepare_large_folio_atomic_write_begin(struct inode *inode, len = min_t(unsigned int, len, folio_size(folio) - ori_off); -if (!f2fs_ffs_find_or_alloc (folio)) -return -ENOMEM ; +f2fs_ffs_find_or_alloc (folio);/* Inline data must have been converted before reaching here. */ f2fs_bug_on (sbi,f2fs_has_inline_data (inode));
@@ -5533,31 +5578,37 @@ void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length)if (inode->i_ino >=F2FS_ROOT_INO (sbi) && (offset || length !=folio_size (folio))) { -size_t clear_start =round_up (offset,PAGE_SIZE ); -size_t clear_end =round_down (offset + length,PAGE_SIZE ); +size_t clear_start =round_up (offset,F2FS_BLKSIZE (sbi)); +size_t clear_end =round_down (offset + length,F2FS_BLKSIZE (sbi));size_t clear_length;bool dirty;/* * If the truncated range falls within a single subpage, no -* subpage state needs to be cleared. +* subpage state needs to be cleared. The folio of an inline +* inode has no subpage state; its uptodate flag tells that the +* inline data was loaded, so it must not be cleared. */ -if (clear_start >= clear_end || !f2fs_folio_has_ffs (folio)) +if (clear_start >= clear_end || + !f2fs_is_multiblock_folio (sbi, folio) || +f2fs_has_inline_data (inode))return ; clear_length = clear_end - clear_start; dirty =f2fs_ffs_clear_subrange_dirty_and_test (folio, clear_start, clear_length); -ffs_clear_subrange_uptodate (folio, clear_start, clear_length); -folio_clear_uptodate (folio); +f2fs_ffs_clear_subrange_uptodate (folio, clear_start, + clear_length);/* * If the truncated subrange happens to clear the remaining * dirty bitmap of the whole folio, cancel the folio-level * dirty state. */ -if (!dirty &&folio_test_dirty (folio)) +if (!dirty &&folio_test_dirty (folio)) { +f2fs_clear_page_cache_dirty_tag (folio);folio_cancel_dirty (folio); + }if (!dirty && !folio_test_writeback (folio))folio_clear_f2fs_gcing (folio);return ;
@@ -5592,6 +5643,7 @@ static bool f2fs_dirty_data_folio(struct address_space *mapping, struct folio *folio) {struct inode *inode = mapping->host; +struct f2fs_sb_info *sbi =F2FS_I_SB (inode);trace_f2fs_set_page_dirty (folio,DATA );
@@ -5599,13 +5651,13 @@ static bool f2fs_dirty_data_folio(struct address_space *mapping,bool uptodate = true;if (f2fs_folio_has_ffs (folio)) { -struct f2fs_folio_state *ffs = - (struct f2fs_folio_state *)folio->private; +struct f2fs_folio_state *ffs = folio->private; +unsigned int nr_blocks =f2fs_blocks_per_folio (sbi, folio);unsigned long flags;spin_lock_irqsave (&ffs->state_lock, flags); - uptodate =bitmap_full (ffs->state,folio_nr_pages (folio)) && - !ffs->read_pages_pending; + uptodate =bitmap_full (ffs->state, nr_blocks) && + !ffs->read_blocks_pending;spin_unlock_irqrestore (&ffs->state_lock, flags); }if (uptodate)
@@ -5957,6 +6009,8 @@ void f2fs_clear_page_cache_dirty_tag(struct folio *folio)xa_lock_irqsave (&mapping->i_pages, flags);__xa_clear_mark (&mapping->i_pages, folio->index,PAGECACHE_TAG_DIRTY ); +__xa_clear_mark (&mapping->i_pages, folio->index, +PAGECACHE_TAG_TOWRITE );xa_unlock_irqrestore (&mapping->i_pages, flags); }
fs/f2fs/f2fs.h +20 -15
@@ -1630,13 +1630,17 @@ static inline void f2fs_clear_bit(unsigned int nr, char *addr);* folio->private is a wrapped pointer. */ +#define F2FS_FOLIO_STATE_MAGIC 0xF5F2FF50 +struct f2fs_folio_state { +u32 magic;spinlock_t state_lock; -unsigned int read_pages_pending; -atomic_t write_pages_pending; +unsigned int read_blocks_pending; +atomic_t write_blocks_pending;unsigned long private_flags; -/* state[0..nr_subpages - 1] tracks uptodate subpages. -* state[nr_subpages..2 * nr_subpages - 1] tracks dirty subpages. +/* +* state[0..nr_blocks - 1] tracks uptodate blocks. +* state[nr_blocks..2 * nr_blocks - 1] tracks dirty blocks. */ unsigned long state[]; };
@@ -2337,6 +2341,12 @@ static inline bool f2fs_has_subpage_blocks(struct f2fs_sb_info *sbi)return F2FS_BLKSIZE (sbi) <PAGE_SIZE ; } +static inline bool f2fs_is_multiblock_folio (struct f2fs_sb_info *sbi, +const struct folio *folio) +{ +return folio_size (folio) >F2FS_BLKSIZE (sbi); +} +static inline pgoff_t f2fs_folio_lblk (struct f2fs_sb_info *sbi,const struct folio *folio) {
@@ -2483,18 +2493,9 @@ static inline bool f2fs_folio_has_ffs(const struct folio *folio) {unsigned long private = (unsigned long )folio->private; -if (!folio_test_large (folio) || !private) +if (!private || (private &BIT (F2FS_FOLIO_PRIVATE_NOT_POINTER )))return false; - -/* -* A large folio should always carry f2fs_folio_state rather than -* non-pointer private flags at its folio->private. -*/ -if (unlikely (test_bit (F2FS_FOLIO_PRIVATE_NOT_POINTER , &private))) { -f2fs_bug_on (F2FS_F_SB (folio),1 ); -return false; - } -return true; +return *((u32 *)private) ==F2FS_FOLIO_STATE_MAGIC ; }static inline void clear_sbi_flag (struct f2fs_sb_info *sbi,unsigned int type)
@@ -4529,6 +4530,10 @@ void f2fs_ffs_mark_subrange_dirty(struct folio *folio, size_t offset, size_t lenbool f2fs_ffs_has_dirty_subpage (struct folio *folio);bool f2fs_ffs_clear_subrange_dirty_and_test (struct folio *folio,size_t offset,size_t len); +void f2fs_ffs_clear_subrange_uptodate (struct folio *folio,size_t offset, +size_t len); +bool f2fs_ffs_dirty_blk_range (struct folio *folio,unsigned int from, +unsigned int to,unsigned int *start,unsigned int *count);void f2fs_write_failed (struct inode *inode,loff_t to);void f2fs_invalidate_folio (struct folio *folio,size_t offset,size_t length);bool f2fs_release_folio (struct folio *folio,gfp_t wait);
fs/f2fs/file.c +1 -17
@@ -238,12 +238,6 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) goto out_sem; } -if (folio_test_large (folio) && !f2fs_ffs_find_or_alloc (folio)) { -folio_unlock (folio); - err = -ENOMEM ; - goto out_sem; - } - nr =DIV_ROUND_UP (dirty_len,PAGE_SIZE );for (i =0 ; i < nr; i++) {
@@ -1628,12 +1622,6 @@ static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,memcpy_folio (fdst, dst_off, fsrc, src_off,F2FS_BLKSIZE (sbi));if (folio_test_large (fdst)) { -if (!f2fs_ffs_find_or_alloc (fdst)) { -f2fs_folio_put (fdst, true); -if (!same_folio) -f2fs_folio_put (fsrc, true); -return -ENOMEM ; - }f2fs_ffs_mark_subrange_uptodate (fdst, dst_off,F2FS_BLKSIZE (sbi)); f2fs_ffs_mark_subrange_dirty(fdst, dst_off,
@@ -4975,16 +4963,12 @@ static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)* bitmap as well, otherwise f2fs_write_cache_folios() * finds no dirty range and skips the whole folio. */ -if (!f2fs_ffs_find_or_alloc (folio)) { - ret = -ENOMEM ; - goto next; - } +f2fs_ffs_find_or_alloc (folio);f2fs_ffs_mark_subrange_dirty (folio, off, dlen); }folio_mark_dirty (folio);folio_set_f2fs_gcing (folio); -next: redirty_idx =folio_next_index (folio);folio_unlock (folio);folio_put_refs (folio,2 );
fs/f2fs/segment.c +1 -1
@@ -4193,7 +4193,7 @@ static void do_write_block(struct f2fs_summary *sum, struct f2fs_io_info *fio) fio->old_blkaddr, fio->new_blkaddr, err);/* * For a large folio, the caller rolls back -* write_pages_pending and ends writeback when the last +* write_blocks_pending and ends writeback when the last * pending write is gone. */ if (!f2fs_folio_has_ffs (folio))
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 03/20] f2fs: extend f2fs_folio_state for subpage blocks
Quoted 21 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
With subpage blocks, even an order-0 folio holds several blocks, so track uptodate and dirty state per block for any folio that holds more than one block, not only for large folios.
- Identify the folio state by a magic number. An order-0 folio can carry one now, so
folio_test_large()no longer tells it apart from the context of a compressed page, whichfolio->privatecan also point to. Carry the page private flags over when the state is attached.
- Make the uptodate and dirty helpers take block indices, and attach the state on demand in them. Some of their callers cannot return an error, e.g.f2fs_set_link()andf2fs_delete_entry()once a directory folio holds several blocks, so allocate the state with__GFP_NOFAIL, asifs_alloc()does in iomap, and drop the allocation failure handling of the callers. The write paths attach the state before a folio is dirtied, so writeback does not normally have to allocate it.
- Handle per-block state in invalidation, dirtying and writeback.
- ClearPAGECACHE_TAG_TOWRITEas well asPAGECACHE_TAG_DIRTYinf2fs_clear_page_cache_dirty_tag().
Quoted 4 lines
Some of these changes also apply to large folios when the block size equals
PAGE_SIZE.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 4/20 f2fs: support subpage blocks in directory operations
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
With subpage blocks, a directory folio holds several dentry blocks. Address a dentry block within its folio by its block index, and mark only the modified dentry block dirty. When an empty dentry block is freed, clear only the state of that block and keep the folio dirty if other blocks in it are still dirty. If no dirty block is left, clear the gcing flag of the folio too.
Also read the name length in f2fs_delete_entry() under the folio lock, so that the dentry bitmap is not updated with a value read without it.
--- fs/f2fs/dir.c | 123 +++++++++++++++++++++++++++++++++-------------- fs/f2fs/f2fs.h | 2 + fs/f2fs/inline.c | 6 +-- 3 files changed, 91 insertions(+), 40 deletions(-)
fs/f2fs/dir.c +87 -36
@@ -37,6 +37,29 @@ static inline bool f2fs_should_fallback_to_linear(struct inode *dir)extern struct kmem_cache *f2fs_cf_name_slab;#endif +static inline pgoff_t f2fs_dentry_to_lblk (struct f2fs_sb_info *sbi, +struct folio *folio,void *de) +{ +return f2fs_folio_lblk (sbi, folio) + +F2FS_BYTES_TO_BLK (sbi, de -folio_address (folio)); +} + +void f2fs_mark_dentry_folio_dirty (struct inode *dir,struct folio *folio, +pgoff_t bidx) +{ +struct f2fs_sb_info *sbi =F2FS_I_SB (dir); + +if (f2fs_has_subpage_blocks (sbi)) { +size_t off =f2fs_folio_byte_offset (sbi, folio, bidx); + +f2fs_ffs_mark_subrange_uptodate (folio, off,F2FS_BLKSIZE (sbi)); +f2fs_ffs_mark_subrange_dirty (folio, off,F2FS_BLKSIZE (sbi)); + }else if (!folio_test_uptodate (folio)) { +folio_mark_uptodate (folio); + } +folio_mark_dirty (folio); +} +static unsigned long dir_blocks (struct inode *inode) {return F2FS_BLK_ALIGN (F2FS_I_SB (inode),i_size_read (inode));
@@ -189,15 +212,16 @@ static unsigned long dir_block_index(unsigned int level, }static struct f2fs_dir_entry *find_in_block (struct inode *dir, -struct folio *dentry_folio, +struct folio *dentry_folio,pgoff_t bidx,const struct f2fs_filename *fname,int *max_slots,bool use_hash) { +struct f2fs_sb_info *sbi =F2FS_I_SB (dir);void *dentry_blk;struct f2fs_dentry_ptr d; - dentry_blk =folio_address (dentry_folio); + dentry_blk =f2fs_folio_lblk_address (sbi, dentry_folio, bidx);make_dentry_ptr_block (dir, &d, dentry_blk);return f2fs_find_target_dentry (&d, fname, max_slots, use_hash);
@@ -288,7 +312,7 @@ static struct f2fs_dir_entry *find_in_level(struct inode *dir,unsigned int nbucket, nblock;unsigned int bidx, end_block, bucket_no;struct f2fs_dir_entry *de =NULL ; -pgoff_t next_pgofs; +pgoff_t next_blkofs;bool room = false;int max_slots;
@@ -305,11 +329,11 @@ static struct f2fs_dir_entry *find_in_level(struct inode *dir,while (bidx < end_block) {/* no need to allocate new dentry pages to all the indices */ struct folio *dentry_folio; - dentry_folio =f2fs_find_data_folio (dir, bidx, &next_pgofs); + dentry_folio =f2fs_find_data_folio (dir, bidx, &next_blkofs);if (IS_ERR (dentry_folio)) {if (PTR_ERR (dentry_folio) == -ENOENT ) { room = true; - bidx = next_pgofs; + bidx = next_blkofs;continue ; }else { *dentry_block = dentry_folio;
@@ -317,7 +341,8 @@ static struct f2fs_dir_entry *find_in_level(struct inode *dir, } } - de =find_in_block (dir, dentry_folio, fname, &max_slots, use_hash); + de =find_in_block (dir, dentry_folio, bidx, fname, &max_slots, + use_hash);if (IS_ERR (de)) {f2fs_folio_put (dentry_folio, false); *dentry_block =ERR_CAST (de);
@@ -353,7 +378,7 @@ struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,const struct f2fs_filename *fname, void **dentry_block) { - unsigned long npages = dir_blocks(dir); + unsigned long nblocks = dir_blocks(dir);struct f2fs_dir_entry *de =NULL ;unsigned int max_depth;unsigned int level;
@@ -369,7 +394,7 @@ struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir, goto out; } -if (npages ==0 ) +if (nblocks ==0 ) goto out; max_depth =F2FS_I (dir)->i_current_depth;
@@ -467,7 +492,8 @@ void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,f2fs_folio_wait_writeback (folio, true, true); de->ino =cpu_to_le32 (inode->i_ino); de->file_type =fs_umode_to_ftype (inode->i_mode); -folio_mark_dirty (folio); +f2fs_mark_dentry_folio_dirty (dir, folio, +f2fs_dentry_to_lblk (F2FS_I_SB (dir), folio, de)); }inode_set_mtime_to_ts (dir,inode_set_ctime_current (dir));
@@ -538,12 +564,12 @@ static int make_empty_dir(struct inode *inode,if (IS_ERR (dentry_folio))return PTR_ERR (dentry_folio); - dentry_blk =folio_address (dentry_folio); + dentry_blk =f2fs_folio_lblk_address (F2FS_I_SB (inode), dentry_folio,0 );make_dentry_ptr_block (inode, &d, dentry_blk);f2fs_do_make_empty_dir (inode, parent, &d); -folio_mark_dirty (dentry_folio); +f2fs_mark_dentry_folio_dirty (inode, dentry_folio,0 );f2fs_folio_put (dentry_folio, true);return 0 ; }
@@ -736,7 +762,8 @@ int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,if (IS_ERR (dentry_folio))return PTR_ERR (dentry_folio); - dentry_blk =folio_address (dentry_folio); + dentry_blk =f2fs_folio_lblk_address (F2FS_I_SB (dir), + dentry_folio, block);make_dentry_ptr_block (dir, &d, dentry_blk); bit_pos =f2fs_room_for_filename (d.bitmap, slots, d.max);if (bit_pos < d.max)
@@ -763,7 +790,7 @@ int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,f2fs_update_dentry (ino, mode, &d, &fname->disk_name, fname->hash, bit_pos); -folio_mark_dirty (dentry_folio); +f2fs_mark_dentry_folio_dirty (dir, dentry_folio, block);if (inode) {f2fs_i_pino_write (inode, dir->i_ino);
@@ -900,26 +927,29 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, void *dentry_block,struct f2fs_dentry_ptr d;struct folio *folio;unsigned int bit_pos; -int slots =GET_DENTRY_SLOTS (le16_to_cpu (dentry->name_len)); +int slots; +struct f2fs_sb_info *sbi =F2FS_I_SB (dir);pgoff_t index;int i; -f2fs_update_time (F2FS_I_SB (dir),REQ_TIME ); +f2fs_update_time (sbi,REQ_TIME ); -if (F2FS_OPTION (F2FS_I_SB (dir)).fsync_mode ==FSYNC_MODE_STRICT ) -f2fs_add_ino_entry (F2FS_I_SB (dir), dir->i_ino,TRANS_DIR_INO ); +if (F2FS_OPTION (sbi).fsync_mode ==FSYNC_MODE_STRICT ) +f2fs_add_ino_entry (sbi, dir->i_ino,TRANS_DIR_INO );if (f2fs_has_inline_dentry (dir))return f2fs_delete_inline_entry (dentry,f2fs_dentry_cache (dentry_block), dir, inode); folio =f2fs_dentry_folio (dentry_block); - index = folio->index;folio_lock (folio);f2fs_folio_wait_writeback (folio, true, true); - dentry_blk =folio_address (folio); +/* Read the dentry under the folio lock. */ + slots =GET_DENTRY_SLOTS (le16_to_cpu (dentry->name_len)); + index =f2fs_dentry_to_lblk (sbi, folio, dentry); + dentry_blk =f2fs_folio_lblk_address (sbi, folio, index);make_dentry_ptr_block (dir, &d, dentry_blk); bit_pos = dentry - d.dentry;for (i =0 ; i < slots; i++)
@@ -927,18 +957,34 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, void *dentry_block,/* Let's check and deallocate this dentry page */ bit_pos =find_next_bit_le (d.bitmap, d.max,0 ); -folio_mark_dirty (folio); +f2fs_mark_dentry_folio_dirty (dir, folio, index);if (bit_pos == d.max && !f2fs_truncate_hole (dir, index, index +1 )) { +if (f2fs_has_subpage_blocks (sbi)) { +size_t off =f2fs_folio_byte_offset (sbi, folio, index); +bool dirty; + + dirty =f2fs_ffs_clear_subrange_dirty_and_test (folio, + off,F2FS_BLKSIZE (sbi)); +f2fs_ffs_clear_subrange_uptodate (folio, off, +F2FS_BLKSIZE (sbi)); +if (dirty) + goto keep_dirty; +/* No dirty block is left to be written for GC. */ +folio_clear_f2fs_gcing (folio); + }f2fs_clear_page_cache_dirty_tag (folio);folio_clear_dirty_for_io (folio); -folio_clear_uptodate (folio); -folio_detach_private (folio); +if (!f2fs_has_subpage_blocks (sbi)) { +folio_clear_uptodate (folio); +folio_detach_private (folio); +inode_dec_dirty_block (dir); + } -inode_dec_dirty_block (dir);f2fs_remove_dirty_inode (dir); } +keep_dirty:f2fs_folio_put (folio, true);inode_set_mtime_to_ts (dir,inode_set_ctime_current (dir));
@@ -960,20 +1006,21 @@ bool f2fs_empty_dir(struct inode *dir)return f2fs_empty_inline_dir (dir);while (bidx < nblock) { -pgoff_t next_pgofs; +pgoff_t next_blkofs;struct folio *dentry_folio; - dentry_folio =f2fs_find_data_folio (dir, bidx, &next_pgofs); + dentry_folio =f2fs_find_data_folio (dir, bidx, &next_blkofs);if (IS_ERR (dentry_folio)) {if (PTR_ERR (dentry_folio) == -ENOENT ) { - bidx = next_pgofs; + bidx = next_blkofs;continue ; }else {return false; } } - dentry_blk =folio_address (dentry_folio); + dentry_blk =f2fs_folio_lblk_address (F2FS_I_SB (dir), + dentry_folio, bidx);make_dentry_ptr_block (dir, &d, dentry_blk);if (bidx ==0 ) bit_pos =2 ;
@@ -1079,7 +1126,7 @@ int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,static int f2fs_readdir (struct file *file,struct dir_context *ctx) {struct inode *inode =file_inode (file); -unsigned long npages =dir_blocks (inode); +unsigned long nblocks =dir_blocks (inode);void *dentry_blk =NULL ;struct file_ra_state *ra = &file->f_ra;loff_t start_pos = ctx->pos;
@@ -1088,6 +1135,8 @@ static int f2fs_readdir(struct file *file, struct dir_context *ctx)unsigned int n = (unsigned long )ctx->pos / entries;struct f2fs_dentry_ptr d;struct fscrypt_str fstr =FSTR_INIT (NULL ,0 ); +pgoff_t f_last = nblocks ?f2fs_lblk_to_folio_index (sbi, nblocks -1 ) :0 ; +pgoff_t f_index, ra_pages;int err =0 ;if (IS_ENCRYPTED (inode)) {
@@ -1105,9 +1154,9 @@ static int f2fs_readdir(struct file *file, struct dir_context *ctx) goto out_free; } -for (; n < npages; ctx->pos = n * entries) { +for (; n < nblocks; ctx->pos = n * entries) {struct folio *dentry_folio; -pgoff_t next_pgofs; +pgoff_t next_blkofs;/* allow readdir() to be interrupted */ if (fatal_signal_pending (current)) {
@@ -1117,23 +1166,25 @@ static int f2fs_readdir(struct file *file, struct dir_context *ctx)cond_resched ();/* readahead for multi pages of dir */ -if (npages - n >1 && !ra_has_index (ra, n)) -page_cache_sync_readahead (inode->i_mapping, ra, file, n, -min (npages - n, (pgoff_t )MAX_DIR_RA_PAGES )); + f_index =f2fs_lblk_to_folio_index (sbi, n); + ra_pages = f_last - f_index +1 ; +if (nblocks - n >1 && !ra_has_index (ra, f_index)) +page_cache_sync_readahead (inode->i_mapping, ra, file, f_index, +min (ra_pages, (pgoff_t )MAX_DIR_RA_PAGES )); - dentry_folio =f2fs_find_data_folio (inode, n, &next_pgofs); + dentry_folio =f2fs_find_data_folio (inode, n, &next_blkofs);if (IS_ERR (dentry_folio)) { err =PTR_ERR (dentry_folio);if (err == -ENOENT ) { err =0 ; - n = next_pgofs; + n = next_blkofs;continue ; }else { goto out_free; } } - dentry_blk =folio_address (dentry_folio); + dentry_blk =f2fs_folio_lblk_address (sbi, dentry_folio, n);make_dentry_ptr_block (inode, &d, dentry_blk);
fs/f2fs/f2fs.h +2 -0
@@ -4193,6 +4193,8 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, void *dentry_blk,int f2fs_do_tmpfile (struct inode *inode,struct inode *dir,struct f2fs_filename *fname);bool f2fs_empty_dir (struct inode *dir); +void f2fs_mark_dentry_folio_dirty (struct inode *dir,struct folio *folio, +pgoff_t bidx);static inline int f2fs_add_link (struct dentry *dentry,struct inode *inode) {
fs/f2fs/inline.c +2 -4
@@ -463,10 +463,6 @@ static int f2fs_move_inline_dirents(struct inode *dir,memcpy (dst.dentry, src.dentry,SIZE_OF_DIR_ENTRY * src.max);memcpy (dst.filename, src.filename, src.max *F2FS_SLOT_LEN ); -if (!folio_test_uptodate (folio)) -folio_mark_uptodate (folio); -folio_mark_dirty (folio); -/* clear inline dir and flag after data writeback */ f2fs_truncate_inline_inode (dir, ientry,0 );
@@ -484,6 +480,8 @@ static int f2fs_move_inline_dirents(struct inode *dir,f2fs_i_depth_write (dir,1 );if (i_size_read (dir) <F2FS_BLKSIZE (F2FS_I_SB (dir)))f2fs_i_size_write (dir,F2FS_BLKSIZE (F2FS_I_SB (dir))); + +f2fs_mark_dentry_folio_dirty (dir, folio,0 ); out:f2fs_folio_put (folio, true);return err;
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 04/20] f2fs: support subpage blocks in directory operations
Quoted 9 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
With subpage blocks, a directory folio holds several dentry blocks. Address a dentry block within its folio by its block index, and mark only the modified dentry block dirty. When an empty dentry block is freed, clear only the state of that block and keep the folio dirty if other blocks in it are still dirty. If no dirty block is left, clear the gcing flag of the folio too.
Quoted 4 lines
Also read the name length in
f2fs_delete_entry()under the folio lock, so that the dentry bitmap is not updated with a value read without it.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 5/20 f2fs: convert inode dirty page counters to dirty block counters
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
The per-inode dirty counter is compared against block based thresholds such as min_fsync_blocks and BLKS_PER_SEG, and it already holds blocks:
its helpers take page counts and convert them. Rename it to dirty_blocks and make the helpers take block counts, and convert to pages where a counter is compared with page based values. The conversions are no-ops when the block size equals PAGE_SIZE.
The callers that account one block for a folio are left as they are, which is still correct when the block size equals PAGE_SIZE. Later patches convert them.
--- fs/f2fs/checkpoint.c | 4 ++-- fs/f2fs/data.c | 13 +++++++------ fs/f2fs/f2fs.h | 36 +++++++++++++++++------------------- fs/f2fs/file.c | 16 ++++++++-------- fs/f2fs/inode.c | 2 +- fs/f2fs/node.c | 3 ++- fs/f2fs/super.c | 2 +- 7 files changed, 38 insertions(+), 38 deletions(-)
fs/f2fs/checkpoint.c +2 -2
@@ -1316,7 +1316,7 @@ static void __remove_dirty_inode(struct inode *inode, enum inode_type type) {int flag = (type ==DIR_INODE ) ?FI_DIRTY_DIR :FI_DIRTY_FILE ; -if (get_dirty_pages (inode) || !is_inode_flag_set (inode, flag)) +if (get_dirty_blocks (inode) || !is_inode_flag_set (inode, flag))return ;list_del_init (&F2FS_I (inode)->dirty_list);
@@ -1325,7 +1325,7 @@ static void __remove_dirty_inode(struct inode *inode, enum inode_type type) }/* -* Account @nr dirty pages (negative when dirtiness is cleared) for @inode. +* Account @nr dirty blocks (negative when dirtiness is cleared) for @inode. */ void f2fs_account_dirty_blocks (struct inode *inode,int nr) {
fs/f2fs/data.c +7 -6
@@ -4279,7 +4279,7 @@ static int f2fs_write_cache_pages(struct address_space *mapping,folio_batch_init (&fbatch); -if (get_dirty_pages (mapping->host) <= +if (get_dirty_blocks (mapping->host) <=SM_I (F2FS_M_SB (mapping))->min_hot_blocks)set_inode_flag (mapping->host,FI_HOT_DATA );else
@@ -4524,7 +4524,7 @@ static int f2fs_write_cache_folios(struct address_space *mapping,bool op_locked = false;bool retry = false; -if (get_dirty_pages (inode) <=SM_I (sbi)->min_hot_blocks) +if (get_dirty_blocks (inode) <=SM_I (sbi)->min_hot_blocks)set_inode_flag (inode,FI_HOT_DATA );else clear_inode_flag (inode,FI_HOT_DATA );
@@ -4788,7 +4788,7 @@ static inline bool __should_serialize_io(struct inode *inode,return true;if (wbc->sync_mode !=WB_SYNC_ALL )return true; -if (get_dirty_pages (inode) >=SM_I (F2FS_I_SB (inode))->min_seq_blocks) +if (get_dirty_blocks (inode) >=SM_I (F2FS_I_SB (inode))->min_seq_blocks)return true;return false; }
@@ -4827,7 +4827,7 @@ static int __f2fs_write_data_pages(struct address_space *mapping,bool locked = false;/* skip writing if there is no dirty page in this inode */ -if (!get_dirty_pages (inode) && wbc->sync_mode ==WB_SYNC_NONE ) +if (!get_dirty_blocks (inode) && wbc->sync_mode ==WB_SYNC_NONE )return 0 ;/* during POR, we don't need to trigger writepage at all. */
@@ -4836,7 +4836,7 @@ static int __f2fs_write_data_pages(struct address_space *mapping,if ((S_ISDIR (inode->i_mode) ||IS_NOQUOTA (inode)) && wbc->sync_mode ==WB_SYNC_NONE && -get_dirty_pages (inode) <nr_caches_to_skip (sbi,DATA ) && +get_dirty_blocks (inode) <nr_caches_to_skip (sbi,DATA ) &&f2fs_available_free_memory (sbi,DIRTY_DENTS )) goto skip_write;
@@ -4895,7 +4895,8 @@ static int __f2fs_write_data_pages(struct address_space *mapping,return ret; skip_write: - wbc->pages_skipped +=get_dirty_pages (inode); + wbc->pages_skipped +=get_dirty_blocks (inode) >> +F2FS_LOG_BLKS_PER_PAGE (sbi);update_skipped_write (sbi, wbc);trace_f2fs_writepages (mapping->host, wbc,DATA );return 0 ;
fs/f2fs/f2fs.h +17 -19
@@ -957,7 +957,7 @@ struct f2fs_inode_info {unsigned long flags[BITS_TO_LONGS (FI_MAX )];/* use to pass per-file flags */ unsigned int ioprio_hint;/* hint for IO priority */ struct f2fs_rwsem i_sem;/* protect fi info */ -atomic_t dirty_pages;/* # of dirty pages */ +atomic_t dirty_blocks;/* # of dirty blocks */ f2fs_hash_t chash;/* hash value of given file name */ unsigned int clevel;/* maximum level of given file name */ struct task_struct *task;/* lookup and create consistency */
@@ -2336,6 +2336,8 @@ static inline unsigned int f2fs_blocks_per_folio(struct f2fs_sb_info *sbi,return F2FS_BYTES_TO_BLK (sbi,folio_size (folio)); } +#define F2FS_LOG_BLKS_PER_PAGE (sbi) (PAGE_SHIFT - (sbi)->log_blocksize) +static inline bool f2fs_has_subpage_blocks (struct f2fs_sb_info *sbi) {return F2FS_BLKSIZE (sbi) <PAGE_SIZE ;
@@ -2979,40 +2981,36 @@ static inline void dec_cache_count(struct f2fs_sb_info *sbi, int count_type)sub_cache_counts (sbi, count_type,1 ); } -/* -* Account @nr dirty pages for @inode; one page is -* (PAGE_SIZE >> log_blocksize) filesystem blocks. -*/ -static inline void inode_add_dirty_blocks (struct inode *inode,int nr) +static inline void inode_add_dirty_blocks (struct inode *inode, +unsigned int nr_blocks) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode); -int blocks = nr * (PAGE_SIZE >> sbi->log_blocksize); -atomic_add (blocks, &F2FS_I (inode)->dirty_pages); +atomic_add (nr_blocks, &F2FS_I (inode)->dirty_blocks);if (S_ISDIR (inode->i_mode)) -add_cache_counts (sbi,F2FS_DIRTY_DENTS , blocks); +add_cache_counts (sbi,F2FS_DIRTY_DENTS , nr_blocks);else -add_cache_counts (sbi,F2FS_DIRTY_DATA , blocks); +add_cache_counts (sbi,F2FS_DIRTY_DATA , nr_blocks);if (IS_NOQUOTA (inode)) -add_cache_counts (sbi,F2FS_DIRTY_QDATA , blocks); +add_cache_counts (sbi,F2FS_DIRTY_QDATA , nr_blocks); } -static inline void inode_sub_dirty_blocks (struct inode *inode,int nr) +static inline void inode_sub_dirty_blocks (struct inode *inode, +unsigned int nr_blocks) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode); -int blocks = nr * (PAGE_SIZE >> sbi->log_blocksize);if (!S_ISDIR (inode->i_mode) && !S_ISREG (inode->i_mode) && !S_ISLNK (inode->i_mode))return ; -atomic_sub (blocks, &F2FS_I (inode)->dirty_pages); +atomic_sub (nr_blocks, &F2FS_I (inode)->dirty_blocks);if (S_ISDIR (inode->i_mode)) -sub_cache_counts (sbi,F2FS_DIRTY_DENTS , blocks); +sub_cache_counts (sbi,F2FS_DIRTY_DENTS , nr_blocks);else -sub_cache_counts (sbi,F2FS_DIRTY_DATA , blocks); +sub_cache_counts (sbi,F2FS_DIRTY_DATA , nr_blocks);if (IS_NOQUOTA (inode)) -sub_cache_counts (sbi,F2FS_DIRTY_QDATA , blocks); +sub_cache_counts (sbi,F2FS_DIRTY_QDATA , nr_blocks); }static inline void inode_inc_dirty_block (struct inode *inode)
@@ -3052,9 +3050,9 @@ static inline s64 get_nr_caches(struct f2fs_sb_info *sbi, int count_type)return atomic_read (&sbi->nr_caches[count_type]); } -static inline int get_dirty_pages (struct inode *inode) +static inline int get_dirty_blocks (struct inode *inode) { -return atomic_read (&F2FS_I (inode)->dirty_pages); +return atomic_read (&F2FS_I (inode)->dirty_blocks); }static inline int get_blocktype_secs (struct f2fs_sb_info *sbi,int block_type)
fs/f2fs/file.c +8 -8
@@ -376,7 +376,7 @@ static int f2fs_do_sync_file(struct file *file, loff_t start, loff_t end, goto go_write;/* if fdatasync is triggered, let's do in-place-update */ -if (datasync ||get_dirty_pages (inode) <=SM_I (sbi)->min_fsync_blocks) +if (datasync ||get_dirty_blocks (inode) <=SM_I (sbi)->min_fsync_blocks)set_inode_flag (inode,FI_NEED_IPU ); ret =file_write_and_wait_range (file, start, end);clear_inode_flag (inode,FI_NEED_IPU );
@@ -2495,9 +2495,9 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)* Should wait end_io to count F2FS_WB_CP_DATA correctly by * f2fs_is_atomic_file. */ -if (get_dirty_pages (inode)) -f2fs_warn (sbi,"Unexpected flush for atomic writes: ino=%llu, npages=%u" , - inode->i_ino,get_dirty_pages (inode)); +if (get_dirty_blocks (inode)) +f2fs_warn (sbi,"Unexpected flush for atomic writes: ino=%llu, nblocks=%u" , + inode->i_ino,get_dirty_blocks (inode)); ret =filemap_write_and_wait_range (inode->i_mapping,0 ,LLONG_MAX );if (ret) goto out_unlock;
@@ -2519,7 +2519,7 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)F2FS_I (fi->cow_inode)->atomic_inode = inode; }else {/* Reuse the already created COW inode */ -f2fs_bug_on (sbi,get_dirty_pages (fi->cow_inode)); +f2fs_bug_on (sbi,get_dirty_blocks (fi->cow_inode));invalidate_mapping_pages (fi->cow_inode->i_mapping,0 ,-1 );
@@ -4877,7 +4877,7 @@ static int f2fs_ioc_set_compress_option(struct file *filp, unsigned long arg) goto out; } -if (f2fs_is_mmap_file (inode) ||get_dirty_pages (inode)) { +if (f2fs_is_mmap_file (inode) ||get_dirty_blocks (inode)) { ret = -EBUSY ; goto out; }
@@ -5033,7 +5033,7 @@ static int f2fs_ioc_decompress_file(struct file *filp)if (ret <0 )break ; -if (get_dirty_pages (inode) >=BLKS_PER_SEG (sbi)) { +if (get_dirty_blocks (inode) >=BLKS_PER_SEG (sbi)) { ret =filemap_fdatawrite (inode->i_mapping);if (ret <0 )break ;
@@ -5123,7 +5123,7 @@ static int f2fs_ioc_compress_file(struct file *filp)if (ret <0 )break ; -if (get_dirty_pages (inode) >=BLKS_PER_SEG (sbi)) { +if (get_dirty_blocks (inode) >=BLKS_PER_SEG (sbi)) { ret =filemap_fdatawrite (inode->i_mapping);if (ret <0 )break ;
fs/f2fs/inode.c +1 -1
@@ -872,7 +872,7 @@ static void f2fs_pre_evict_inode(struct inode *inode) test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))f2fs_invalidate_compress_pages (sbi, inode->i_ino); -f2fs_bug_on (sbi,get_dirty_pages (inode)); +f2fs_bug_on (sbi,get_dirty_blocks (inode));f2fs_remove_dirty_inode (inode);f2fs_remove_donate_inode (inode);
fs/f2fs/node.c +2 -1
@@ -85,7 +85,8 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type) }else if (type ==DIRTY_DENTS ) {if (bdi_wb_dirty_exceeded (sbi->sb->s_bdi))return false; - mem_size =get_nr_caches (sbi,F2FS_DIRTY_DENTS ); + mem_size =get_nr_caches (sbi,F2FS_DIRTY_DENTS ) >> +F2FS_LOG_BLKS_PER_PAGE (sbi); res = mem_size < ((avail_ram * nm_i->ram_thresh /100 ) >>1 ); }else if (type ==INO_ENTRIES ) {int i;
fs/f2fs/super.c +1 -1
@@ -1855,7 +1855,7 @@ static struct inode *f2fs_alloc_inode(struct super_block *sb)init_once ((void *) fi);/* Initialize f2fs-specific inode info */ -atomic_set (&fi->dirty_pages,0 ); +atomic_set (&fi->dirty_blocks,0 );atomic_set (&fi->i_compr_blocks,0 );atomic_set (&fi->open_count,0 );atomic_set (&fi->writeback,0 );
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 05/20] f2fs: convert inode dirty page counters to dirty block counters
Quoted 9 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
The per-inode dirty counter is compared against block based thresholds such as
min_fsync_blocksandBLKS_PER_SEG, and it already holds blocks:
its helpers take page counts and convert them. Rename it todirty_blocksand make the helpers take block counts, and convert to pages where a counter is compared with page based values. The conversions are no-ops when the block size equalsPAGE_SIZE.
Quoted 5 lines
The callers that account one block for a folio are left as they are, which is still correct when the block size equals
PAGE_SIZE. Later patches convert them.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 6/20 f2fs: support subpage blocks in data read path and GC
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
With subpage blocks, a folio can be partially uptodate, so reads and GC have to work per block.
- Read only the non-uptodate blocks of a multi-block folio, and send every inode to this path in subpage mode.
- Do not use the folio state when a single bio reads the whole folio.
- Implement ->is_partially_uptodate.
- Convert block indices to folio indices in the data folio getters and GC, and prefetch the whole folio in GC so that its blocks are read together.
- Fix error handling in f2fs_read_data_large_folio().
- Zero the blocks past EOF in f2fs_read_data_large_folio() instead of reading them, as f2fs_read_single_page() does. With subpage blocks, they share a page with the last block of the file and can be seen through mmap.
- Test the per-block uptodate state in f2fs_find_data_folio() under the folio lock, as ->release_folio can free the folio state otherwise. A fully uptodate folio is still returned without the lock.
- Keep the folio state of a folio that is not fully uptodate in ->release_folio while the folio has other users. Otherwise a block that a lockless reader, e.g. a dentry lookup, is using can be read from disk again.
- Get sbi from the folio in f2fs_finish_read_bio() only after the compressed page check, as nothing pins the inode of a compressed page once its cluster is unlocked.
- Rename helpers that now take block indices, e.g. f2fs_submit_page_read() to f2fs_submit_block_read().
The read changes also apply to large folios when the block size equals PAGE_SIZE.
--- fs/f2fs/data.c | 434 ++++++++++++++++++++++++++++++------------------- fs/f2fs/f2fs.h | 20 ++- fs/f2fs/file.c | 22 +-- fs/f2fs/gc.c | 87 +++++++--- fs/f2fs/node.c | 6 +- 5 files changed, 358 insertions(+), 211 deletions(-)
fs/f2fs/data.c +265 -169
@@ -145,13 +145,8 @@ static void f2fs_finish_read_bio(struct bio *bio, bool in_task)bio_for_each_folio_all (fi, bio) {struct folio *folio = fi.folio; -/* -* Cache sbi before the pending count is dropped: a sync reader -* may then release the folio while this bio is still in its -* end_io, so F2FS_F_SB(folio) is no longer safe to use below. -*/ -struct f2fs_sb_info *sbi =F2FS_F_SB (folio); -unsigned nr_pages = fi.length >>PAGE_SHIFT ; +struct f2fs_sb_info *sbi; +unsigned int nr_blocks;bool finished = true;bool uptodate = bio->bi_status ==BLK_STS_OK ;
@@ -164,6 +159,10 @@ static void f2fs_finish_read_bio(struct bio *bio, bool in_task)continue ; } + sbi =F2FS_F_SB (folio); + nr_blocks =F2FS_BYTES_TO_BLK (sbi, fi.length); +sub_cache_counts (sbi,F2FS_RD_DATA , nr_blocks); +if (f2fs_folio_has_ffs (folio)) {struct f2fs_folio_state *ffs = (struct f2fs_folio_state *)folio->private;
@@ -172,14 +171,11 @@ static void f2fs_finish_read_bio(struct bio *bio, bool in_task)if (uptodate) uptodate =__ffs_mark_subrange_uptodate (folio, ffs, fi.offset, fi.length); - ffs->read_blocks_pending -= nr_pages; + ffs->read_blocks_pending -= nr_blocks; finished = !ffs->read_blocks_pending;spin_unlock_irqrestore (&ffs->state_lock, flags); } -while (nr_pages--) -dec_cache_count (sbi,F2FS_RD_DATA ); -if (finished)folio_end_read (folio, uptodate); }
@@ -1477,7 +1473,7 @@ void f2fs_submit_cache_write(struct f2fs_io_info *fio)static struct bio *f2fs_grab_read_bio (struct inode *inode,struct fsverity_info *vi,block_t blkaddr, -unsigned nr_pages,blk_opf_t op_flag, +unsigned int nr_blocks,blk_opf_t op_flag,pgoff_t first_idx,bool for_write) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);
@@ -1487,7 +1483,7 @@ static struct bio *f2fs_grab_read_bio(struct inode *inode,sector_t sector;struct block_device *bdev =f2fs_target_device (sbi, blkaddr, §or); - bio =bio_alloc_bioset (bdev,bio_max_segs (nr_pages), + bio =bio_alloc_bioset (bdev,bio_max_segs (nr_blocks),REQ_OP_READ | op_flag, for_write ?GFP_NOIO :GFP_KERNEL , &f2fs_bioset); bio->bi_iter.bi_sector = sector;
@@ -1521,27 +1517,29 @@ static struct bio *f2fs_grab_read_bio(struct inode *inode, }/* This can handle encryption stuffs */ -static void f2fs_submit_page_read (struct inode *inode,struct fsverity_info *vi, -struct folio *folio,pgoff_t index, -block_t blkaddr,blk_opf_t op_flags, -bool for_write) +static void f2fs_submit_block_read (struct inode *inode, +struct fsverity_info *vi, +struct folio *folio,pgoff_t bidx, +block_t blkaddr,blk_opf_t op_flags, +bool for_write) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct bio *bio;size_t offset =0 ; -if (f2fs_folio_has_ffs (folio)) { - offset =offset_in_folio (folio, (loff_t )index <<PAGE_SHIFT ); +if (f2fs_is_multiblock_folio (sbi, folio)) { +f2fs_ffs_find_or_alloc (folio); + offset =f2fs_folio_byte_offset (sbi, folio, bidx);f2fs_update_read_folio_pending (folio,1 ); } - bio =f2fs_grab_read_bio (inode, vi, blkaddr,1 , op_flags, index, + bio =f2fs_grab_read_bio (inode, vi, blkaddr,1 , op_flags, bidx, for_write);/* wait for GCed page writeback via generic cache */ f2fs_wait_on_block_writeback (inode, blkaddr); -if (!bio_add_folio (bio, folio,PAGE_SIZE , offset)) +if (!bio_add_folio (bio, folio,F2FS_BLKSIZE (sbi), offset))f2fs_bug_on (sbi,1 );inc_cache_count (sbi,F2FS_RD_DATA );
@@ -1550,12 +1548,12 @@ static void f2fs_submit_page_read(struct inode *inode, struct fsverity_info *vi, }/* -* Synchronously read a single 4K subpage by reusing f2fs_submit_page_read() +* Synchronously read a single block by reusing f2fs_submit_block_read() * so that iostat, trace, blk-crypto and post-read handling are all preserved. * The caller must have already allocated ffs for the folio. */ -static int f2fs_submit_page_read_sync (struct inode *inode,struct folio *folio, -pgoff_t index,block_t blkaddr) +static int f2fs_submit_block_read_sync (struct inode *inode,struct folio *folio, +pgoff_t index,block_t blkaddr) {struct f2fs_folio_state *ffs = (struct f2fs_folio_state *)folio->private;
@@ -1567,8 +1565,8 @@ static int f2fs_submit_page_read_sync(struct inode *inode, struct folio *folio,* We are holding the folio lock, so use GFP_NOIO for the bio * allocation to avoid entering mm reclaim. */ -f2fs_submit_page_read (inode,NULL , folio, index, blkaddr, -REQ_OP_READ , true); +f2fs_submit_block_read (inode,NULL , folio, index, blkaddr, +REQ_OP_READ , true);/* Wait until all bios have completed (pending drops back to our bias). */ while (READ_ONCE (ffs->read_blocks_pending) !=1 )
@@ -1680,24 +1678,23 @@ static inline struct fsverity_info *f2fs_need_verity(const struct inode *inode,return NULL ; } -struct folio *f2fs_get_read_data_folio (struct inode *inode,pgoff_t index, -blk_opf_t op_flags,bool for_write,pgoff_t *next_pgofs) +struct folio *f2fs_get_read_data_folio (struct inode *inode,pgoff_t bidx, +blk_opf_t op_flags,bool for_write,pgoff_t *next_blkofs) { +struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct address_space *mapping = inode->i_mapping; +pgoff_t f_index =f2fs_lblk_to_folio_index (sbi, bidx);struct dnode_of_data dn;struct folio *folio;int err; - folio =f2fs_grab_cache_folio (mapping, index, for_write); + folio =f2fs_grab_cache_folio (mapping, f_index, for_write);if (IS_ERR (folio))return folio; -if (folio_test_large (folio)) -f2fs_ffs_find_or_alloc (folio); - -if (f2fs_lookup_read_extent_cache_block (inode, index, +if (f2fs_lookup_read_extent_cache_block (inode, bidx, &dn.data_blkaddr)) { -if (!f2fs_is_valid_blkaddr (F2FS_I_SB (inode), dn.data_blkaddr, +if (!f2fs_is_valid_blkaddr (sbi, dn.data_blkaddr,DATA_GENERIC_ENHANCE_READ )) { err = -EFSCORRUPTED ; goto put_err;
@@ -1706,29 +1703,29 @@ struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index, }set_new_dnode (&dn, inode,NULL ,NULL ,0 ); - err =f2fs_get_dnode_of_data (&dn, index,LOOKUP_NODE ); + err =f2fs_get_dnode_of_data (&dn, bidx,LOOKUP_NODE );if (err) { -if (err == -ENOENT && next_pgofs) - *next_pgofs =f2fs_get_next_page_offset (&dn, index); +if (err == -ENOENT && next_blkofs) + *next_blkofs =f2fs_get_next_block_offset (&dn, bidx); goto put_err; }f2fs_put_dnode (&dn);if (unlikely (dn.data_blkaddr ==NULL_ADDR )) { err = -ENOENT ; -if (next_pgofs) - *next_pgofs = index +1 ; +if (next_blkofs) + *next_blkofs = bidx +1 ; goto put_err; }if (dn.data_blkaddr !=NEW_ADDR && - !f2fs_is_valid_blkaddr (F2FS_I_SB (inode), + !f2fs_is_valid_blkaddr (sbi, dn.data_blkaddr,DATA_GENERIC_ENHANCE )) { err = -EFSCORRUPTED ; goto put_err; } got_it: -if (f2fs_ffs_test_blk_uptodate (folio, index)) { +if (f2fs_ffs_test_blk_uptodate (folio, bidx)) {folio_unlock (folio);return folio; }
@@ -1741,17 +1738,17 @@ struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index,* f2fs_init_inode_metadata. */ if (dn.data_blkaddr ==NEW_ADDR ) { -size_t offset =offset_in_folio (folio, - (loff_t )index <<PAGE_SHIFT ); +size_t offset =f2fs_folio_byte_offset (sbi, folio, bidx); -folio_zero_segment (folio, offset, offset +PAGE_SIZE ); -f2fs_ffs_mark_subrange_uptodate (folio, offset,PAGE_SIZE ); +folio_zero_segment (folio, offset, offset +F2FS_BLKSIZE (sbi)); +f2fs_ffs_mark_subrange_uptodate (folio, offset,F2FS_BLKSIZE (sbi));folio_unlock (folio);return folio; } -f2fs_submit_page_read (inode,f2fs_need_verity (inode, index), - folio, index, dn.data_blkaddr, op_flags, for_write); +f2fs_submit_block_read (inode,f2fs_need_verity (inode, bidx), + folio, bidx, dn.data_blkaddr, op_flags, + for_write);return folio; put_err:
@@ -1759,32 +1756,53 @@ struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index,return ERR_PTR (err); } -struct folio *f2fs_find_data_folio (struct inode *inode,pgoff_t index, -pgoff_t *next_pgofs) +struct folio *f2fs_find_data_folio (struct inode *inode,pgoff_t bidx, +pgoff_t *next_blkofs) { +struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct address_space *mapping = inode->i_mapping; +pgoff_t f_index =f2fs_lblk_to_folio_index (sbi, bidx);struct folio *folio; - folio =f2fs_filemap_get_folio (mapping, index,FGP_ACCESSED ,0 ); + folio =f2fs_filemap_get_folio (mapping, f_index,FGP_ACCESSED ,0 );if (IS_ERR (folio)) goto read;if (folio_test_uptodate (folio))return folio; -f2fs_folio_put (folio, false); +/* The folio state is freed under the folio lock. */ +folio_lock (folio); +if (folio->mapping == mapping && +f2fs_ffs_test_blk_uptodate (folio, bidx)) { +folio_unlock (folio); +return folio; + } +f2fs_folio_put (folio, true); read: - folio =f2fs_get_read_data_folio (inode, index,0 , false, next_pgofs); + folio =f2fs_get_read_data_folio (inode, bidx,0 , false, next_blkofs);if (IS_ERR (folio))return folio;if (folio_test_uptodate (folio))return folio; -folio_wait_locked (folio); -if (unlikely (!folio_test_uptodate (folio))) { -f2fs_folio_put (folio, false); +folio_lock (folio); +if (unlikely (folio->mapping != mapping || + !f2fs_ffs_test_blk_uptodate (folio, bidx))) { +/* +* Reclaim can free the folio state of a clean folio while it +* is unlocked, losing the uptodate state of the block. +*/ +bool freed = folio->mapping == mapping && +f2fs_is_multiblock_folio (sbi, folio) && + !f2fs_folio_has_ffs (folio); + +f2fs_folio_put (folio, true); +if (freed) + goto read;return ERR_PTR (-EIO ); } +folio_unlock (folio);return folio; }
@@ -1793,13 +1811,15 @@ struct folio *f2fs_find_data_folio(struct inode *inode, pgoff_t index,* Because, the callers, functions in dir.c and GC, should be able to know * whether this page exists or not. */ -struct folio *f2fs_get_lock_data_folio (struct inode *inode,pgoff_t index, +struct folio *f2fs_get_lock_data_folio (struct inode *inode,pgoff_t bidx,bool for_write) { +struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct address_space *mapping = inode->i_mapping;struct folio *folio; - folio =f2fs_get_read_data_folio (inode, index,0 , for_write,NULL ); +repeat: + folio =f2fs_get_read_data_folio (inode, bidx,0 , for_write,NULL );if (IS_ERR (folio))return folio;
@@ -1812,15 +1832,17 @@ struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index, }/* -* ffs can be released while the folio was unlocked above. Rebuild it -* for a fully uptodate folio; a partially uptodate one cannot be -* rebuilt and will fail the subpage check below. +* ffs can be released while the folio was unlocked above, losing the +* uptodate state of the blocks of a partially uptodate folio. Read the +* block again in that case. */ -if (folio_test_large (folio) && !f2fs_folio_has_ffs (folio)) -f2fs_ffs_find_or_alloc (folio); +if (unlikely (!f2fs_ffs_test_blk_uptodate (folio, bidx))) { +bool freed =f2fs_is_multiblock_folio (sbi, folio) && + !f2fs_folio_has_ffs (folio); -if (unlikely (!f2fs_ffs_test_blk_uptodate (folio, index))) {f2fs_folio_put (folio, true); +if (freed) + goto repeat;return ERR_PTR (-EIO ); }return folio;
@@ -1838,12 +1860,14 @@ struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index,struct folio *f2fs_get_new_data_folio (struct inode *inode,struct f2fs_cached_block *ientry,pgoff_t index,bool new_i_size) { +struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct address_space *mapping = inode->i_mapping; +pgoff_t f_index =f2fs_lblk_to_folio_index (sbi, index);struct folio *folio;struct dnode_of_data dn;int err; - folio =f2fs_grab_cache_folio (mapping, index, true); + folio =f2fs_grab_cache_folio (mapping, f_index, true);if (IS_ERR (folio)) {/* * before exiting, we should make sure ientry will be released
@@ -1866,25 +1890,24 @@ struct folio *f2fs_get_new_data_folio(struct inode *inode, goto got_it;if (dn.data_blkaddr ==NEW_ADDR ) { -size_t off =offset_in_folio (folio, - (loff_t )index <<PAGE_SHIFT ); +size_t off =f2fs_folio_byte_offset (sbi, folio, index); -folio_zero_segment (folio, off, off +PAGE_SIZE ); -f2fs_ffs_mark_subrange_uptodate (folio, off,PAGE_SIZE ); +folio_zero_segment (folio, off, off +F2FS_BLKSIZE (sbi)); +f2fs_ffs_mark_subrange_uptodate (folio, off,F2FS_BLKSIZE (sbi)); }else {f2fs_folio_put (folio, true);/* if ientry exists, blkaddr should be NEW_ADDR */ -f2fs_bug_on (F2FS_I_SB (inode), ientry); +f2fs_bug_on (sbi, ientry); folio =f2fs_get_lock_data_folio (inode, index, true);if (IS_ERR (folio))return folio; } got_it:if (new_i_size &&i_size_read (inode) < -F2FS_BLK_TO_BYTES (F2FS_I_SB (inode), index +1 )) +F2FS_BLK_TO_BYTES (sbi, index +1 ))f2fs_i_size_write (inode, -F2FS_BLK_TO_BYTES (F2FS_I_SB (inode), index +1 )); +F2FS_BLK_TO_BYTES (sbi, index +1 ));return folio; }
@@ -1965,7 +1988,7 @@ int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index)static int f2fs_map_no_dnode (struct inode *inode,struct f2fs_map_blocks *map,struct dnode_of_data *dn, -pgoff_t pgoff) +pgoff_t blkofs) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);
@@ -1978,10 +2001,10 @@ static int f2fs_map_no_dnode(struct inode *inode, (is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN) || f2fs_cp_error(sbi))) return -EIO; -if (map->m_next_pgofs) - *map->m_next_pgofs =f2fs_get_next_page_offset (dn, pgoff); +if (map->m_next_blkofs) + *map->m_next_blkofs =f2fs_get_next_block_offset (dn, blkofs);if (map->m_next_extent) - *map->m_next_extent =f2fs_get_next_page_offset (dn, pgoff); + *map->m_next_extent =f2fs_get_next_block_offset (dn, blkofs);return 0 ; }
@@ -2188,19 +2211,19 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag) goto sync_out;case F2FS_GET_BLOCK_FIEMAP :if (blkaddr ==NULL_ADDR ) { -if (map->m_next_pgofs) - *map->m_next_pgofs = pgofs +1 ; +if (map->m_next_blkofs) + *map->m_next_blkofs = pgofs +1 ; goto sync_out; }break ;case F2FS_GET_BLOCK_DIO : -if (map->m_next_pgofs) - *map->m_next_pgofs = pgofs +1 ; +if (map->m_next_blkofs) + *map->m_next_blkofs = pgofs +1 ;break ;default :/* for defragment case */ -if (map->m_next_pgofs) - *map->m_next_pgofs = pgofs +1 ; +if (map->m_next_blkofs) + *map->m_next_blkofs = pgofs +1 ; goto sync_out; } }
@@ -2341,7 +2364,7 @@ static bool __f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len,return false; map.m_lblk =F2FS_BYTES_TO_BLK (F2FS_I_SB (inode), pos); - map.m_next_pgofs =NULL ; + map.m_next_blkofs =NULL ; map.m_next_extent =NULL ; map.m_seg_type =NO_CHECK_TYPE ; map.m_may_create = false;
@@ -2444,7 +2467,7 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct f2fs_map_blocks map;sector_t start_blk, last_blk, blk_len, max_len; -pgoff_t next_pgofs; +pgoff_t next_blkofs;u64 logical =0 , phys =0 , size =0 ;u32 flags =0 ;int ret =0 ;
@@ -2494,7 +2517,7 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,memset (&map,0 ,sizeof (map)); map.m_lblk = start_blk; map.m_len = blk_len; - map.m_next_pgofs = &next_pgofs; + map.m_next_blkofs = &next_blkofs; map.m_seg_type =NO_CHECK_TYPE ;if (compr_cluster) {
@@ -2508,7 +2531,7 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,/* HOLE */ if (!compr_cluster && !(map.m_flags &F2FS_MAP_FLAGS )) { - start_blk = next_pgofs; + start_blk = next_blkofs;if (F2FS_BLK_TO_BYTES (sbi, start_blk) < maxbytes) goto prep_next;
@@ -2644,7 +2667,7 @@ static int f2fs_read_single_page(struct inode *inode, struct fsverity_info *vi,if (block_in_file > map->m_lblk && block_in_file < (map->m_lblk + map->m_len)) goto got_it; - }else if (block_in_file < *map->m_next_pgofs) { + }else if (block_in_file < *map->m_next_blkofs) { goto got_it; }
@@ -3209,23 +3232,26 @@ static bool find_next_valid_block(const struct folio *folio, size_t orig_off, size_t *need_off, size_t len) { + struct f2fs_sb_info *sbi = F2FS_M_SB(folio->mapping);size_t start = orig_off;size_t end = start + len;size_t head, tail;pgoff_t index; -if (start & (PAGE_SIZE -1 )) { - head =round_down (start,PAGE_SIZE ); - index = folio->index + (head >>PAGE_SHIFT ); +if (start &F2FS_BLKSIZE_MASK (sbi)) { + head =round_down (start,F2FS_BLKSIZE (sbi)); + index =f2fs_folio_lblk (sbi, folio) + +F2FS_BYTES_TO_BLK (sbi, head);if (!f2fs_ffs_test_blk_uptodate (folio, index)) { *need_off = head;return true; } } -if (end & (PAGE_SIZE -1 )) { - tail =round_down (end,PAGE_SIZE ); - index = folio->index + (tail >>PAGE_SHIFT ); +if (end &F2FS_BLKSIZE_MASK (sbi)) { + tail =round_down (end,F2FS_BLKSIZE (sbi)); + index =f2fs_folio_lblk (sbi, folio) + +F2FS_BYTES_TO_BLK (sbi, tail);if (!f2fs_ffs_test_blk_uptodate (folio, index)) { *need_off = tail;return true;
@@ -3248,26 +3274,34 @@ static unsigned int ffs_next_nonuptodate_subpage(struct f2fs_folio_state *ffs, }static void f2fs_skip_fully_uptodate_front (struct folio *folio, -pgoff_t *index,pgoff_t *offset,unsigned int *nrpages, -unsigned int *max_nr_pages) +pgoff_t *index,pgoff_t *offset,unsigned int *nr_blocks, +unsigned int *max_nr_blocks) {struct f2fs_folio_state *ffs;unsigned int next, skipped; -if (!f2fs_folio_has_ffs (folio) || !*nrpages) +if (folio_test_uptodate (folio)) { + *index += *nr_blocks; + *offset += *nr_blocks; + *max_nr_blocks -= *nr_blocks; + *nr_blocks =0 ; +return ; + } + +if (!f2fs_folio_has_ffs (folio) || !*nr_blocks)return ; ffs = (struct f2fs_folio_state *)folio->private; next =ffs_next_nonuptodate_subpage (ffs, *offset, - *offset + *nrpages -1 ); + *offset + *nr_blocks -1 ); skipped = next - *offset;if (!skipped)return ; *index += skipped; *offset += skipped; - *nrpages -= skipped; - *max_nr_pages -= skipped; + *nr_blocks -= skipped; + *max_nr_blocks -= skipped; } static void f2fs_truncate_read_extent(struct inode *inode, struct folio *folio,
@@ -3278,7 +3312,7 @@ static void f2fs_truncate_read_extent(struct inode *inode, struct folio *folio,struct f2fs_folio_state *ffs;unsigned int next, end; -if (!f2fs_folio_has_ffs (folio) || *len_blks <=1 ) +if (*len_blks <=1 )return ;if (f2fs_is_multi_device (sbi)) {
@@ -3289,8 +3323,11 @@ static void f2fs_truncate_read_extent(struct inode *inode, struct folio *folio, } *len_blks = min_t(unsigned int, *len_blks, (unsigned int )fscrypt_limit_io_blocks (inode, - folio->index + offset, - *len_blks)); +f2fs_folio_lblk (sbi, folio) + offset, + *len_blks)); + +if (!f2fs_folio_has_ffs (folio) || *len_blks <=1 ) +return ; ffs = (struct f2fs_folio_state *)folio->private; end = offset + *len_blks -1 ;
@@ -3303,13 +3340,16 @@ static int f2fs_read_data_large_folio(struct inode *inode,struct fsverity_info *vi, struct readahead_control *rac, struct folio *folio) { + struct f2fs_sb_info *sbi = F2FS_I_SB(inode);struct bio *bio =NULL ;sector_t last_block_in_bio =0 ;struct f2fs_map_blocks map = {0 , }; -pgoff_t index, offset, next_pgofs =0 ; -unsigned max_nr_pages = rac ?readahead_count (rac) : -folio_nr_pages (folio); -unsigned nrpages, len_blks; +pgoff_t index, offset, next_blkofs =0 ; +unsigned int max_nr_blocks = rac ? + (readahead_count (rac) <<F2FS_LOG_BLKS_PER_PAGE (sbi)) : +f2fs_blocks_per_folio (sbi, folio); +unsigned int nr_blocks, len_blks; +sector_t last_block_in_file;struct f2fs_folio_state *ffs;int ret =0 ;bool folio_in_bio = false;
@@ -3322,35 +3362,35 @@ static int f2fs_read_data_large_folio(struct inode *inode,if (!folio) goto out; -f2fs_update_read_folio_count (F2FS_I_SB (inode), folio); +f2fs_update_read_folio_count (sbi, folio); folio_in_bio = false; - ffs =NULL ; -if (folio_test_large (folio)) { -/* -* Allocate the folio state before doing anything with the -* folio, so that the block mapping helpers below can rely on -* it. -*/ - ffs =f2fs_ffs_find_or_alloc (folio); - } - index = folio->index; + index =f2fs_folio_lblk (sbi, folio); offset =0 ; - nrpages =folio_nr_pages (folio); + nr_blocks =f2fs_blocks_per_folio (sbi, folio); + last_block_in_file =F2FS_BLK_ALIGN (sbi,f2fs_readpage_limit (inode)); -for (; nrpages; - nrpages -= len_blks, max_nr_pages -= len_blks, +for (; nr_blocks; + nr_blocks -= len_blks, max_nr_blocks -= len_blks, index += len_blks, offset += len_blks) {sector_t block_nr; +bool whole_folio_in_bio;unsigned int i; +size_t page_offset; len_blks =1 ;f2fs_skip_fully_uptodate_front (folio, &index, &offset, - &nrpages, &max_nr_pages); -if (!nrpages) + &nr_blocks, &max_nr_blocks); +if (!nr_blocks)break ; + page_offset =F2FS_BLK_TO_BYTES (sbi, offset); + +/* The blocks past EOF are zeroed below without mapping. */ +if (index >= last_block_in_file) + goto got_it; +/* * Map blocks using the previous result first. */
@@ -3358,7 +3398,7 @@ static int f2fs_read_data_large_folio(struct inode *inode,if (index > map.m_lblk && index < (map.m_lblk + map.m_len)) goto got_it; - }else if (index < next_pgofs) { + }else if (index < next_blkofs) {/* hole case */ goto got_it; }
@@ -3368,26 +3408,30 @@ static int f2fs_read_data_large_folio(struct inode *inode,* done with this page. */ memset (&map,0 ,sizeof (map)); - map.m_next_pgofs = &next_pgofs; + map.m_next_blkofs = &next_blkofs; map.m_seg_type =NO_CHECK_TYPE ; map.m_lblk = index; - map.m_len = max_nr_pages; + map.m_len = max_nr_blocks; ret =f2fs_map_blocks (inode, &map,F2FS_GET_BLOCK_DEFAULT );if (ret) goto err_out; got_it: -if ((map.m_flags &F2FS_MAP_MAPPED )) { +/* Zero the blocks past EOF, as f2fs_read_single_page() does. */ +if ((map.m_flags &F2FS_MAP_MAPPED ) && + index < last_block_in_file) { block_nr = map.m_pblk + index - map.m_lblk; - len_blks = min_t(unsigned int, nrpages, max_nr_pages); + len_blks = min_t(unsigned int, nr_blocks, max_nr_blocks); len_blks = min_t(unsigned int, len_blks, (unsigned int )(map.m_lblk + map.m_len - index)); + len_blks = min_t(unsigned int, len_blks, + last_block_in_file - index);f2fs_truncate_read_extent (inode, folio, offset, block_nr, &len_blks);for (i =0 ; i < len_blks; i++) { -if (!f2fs_is_valid_blkaddr (F2FS_I_SB (inode), +if (!f2fs_is_valid_blkaddr (sbi, block_nr + i,DATA_GENERIC_ENHANCE_READ )) { ret = -EFSCORRUPTED ;
@@ -3395,18 +3439,21 @@ static int f2fs_read_data_large_folio(struct inode *inode, } } + whole_folio_in_bio = !f2fs_folio_has_ffs (folio) && + offset ==0 && + len_blks ==f2fs_blocks_per_folio (sbi, folio); }else { -size_t page_offset = offset <<PAGE_SHIFT ; - -folio_zero_range (folio, page_offset,PAGE_SIZE ); -if (vi && !fsverity_verify_blocks (vi, folio,PAGE_SIZE , page_offset)) { +folio_zero_range (folio, page_offset,F2FS_BLKSIZE (sbi)); +if (vi && !fsverity_verify_blocks (vi, folio, +F2FS_BLKSIZE (sbi), page_offset)) { ret = -EIO ; goto err_out; } -if (folio_test_large (folio)) { +if (f2fs_is_multiblock_folio (sbi, folio)) { + ffs =f2fs_ffs_find_or_alloc (folio);spin_lock_irq (&ffs->state_lock);__ffs_mark_subrange_uptodate (folio, ffs, - page_offset,PAGE_SIZE ); + page_offset,F2FS_BLKSIZE (sbi));spin_unlock_irq (&ffs->state_lock); }continue ;
@@ -3415,7 +3462,10 @@ static int f2fs_read_data_large_folio(struct inode *inode,/* We must increment read_blocks_pending before possible BIOs submitting * to prevent from premature folio_end_read() call on folio */ -if (folio_test_large (folio)) { + ffs =NULL ; +if (f2fs_is_multiblock_folio (sbi, folio) && !whole_folio_in_bio) { + ffs =f2fs_ffs_find_or_alloc (folio); +/* set the bitmap to wait */ f2fs_update_read_folio_pending (folio, len_blks); }
@@ -3424,16 +3474,16 @@ static int f2fs_read_data_large_folio(struct inode *inode,* This page will go to BIO. Do we need to send this * BIO off first? */ -if (bio && (!page_is_mergeable (F2FS_I_SB (inode), bio, +if (bio && (!page_is_mergeable (sbi, bio, last_block_in_bio, block_nr) || !f2fs_crypt_mergeable_bio (bio, inode, index,NULL ))) { submit_and_realloc: -f2fs_submit_read_bio (F2FS_I_SB (inode), bio,DATA ); +f2fs_submit_read_bio (sbi, bio,DATA ); bio =NULL ; }if (bio ==NULL ) bio =f2fs_grab_read_bio (inode, vi, - block_nr, max_nr_pages, + block_nr, max_nr_blocks,f2fs_ra_op_flags (rac), index, false);
@@ -3444,33 +3494,33 @@ static int f2fs_read_data_large_folio(struct inode *inode,for (i =0 ; i < len_blks; i++)f2fs_wait_on_block_writeback (inode, block_nr + i); -if (!bio_add_folio (bio, folio, - len_blks *F2FS_BLKSIZE (F2FS_I_SB (inode)), - offset <<PAGE_SHIFT )) +if (!bio_add_folio (bio, folio,F2FS_BLK_TO_BYTES (sbi, len_blks), + page_offset)) goto submit_and_realloc; folio_in_bio = true; -for (i =0 ; i < len_blks; i++) -inc_cache_count (F2FS_I_SB (inode),F2FS_RD_DATA ); -f2fs_update_iostat (F2FS_I_SB (inode),NULL ,FS_DATA_READ_IO , - len_blks *F2FS_BLKSIZE (F2FS_I_SB (inode))); +add_cache_counts (sbi,F2FS_RD_DATA , len_blks); +f2fs_update_iostat (sbi,NULL ,FS_DATA_READ_IO , +F2FS_BLK_TO_BYTES (sbi, len_blks)); last_block_in_bio = block_nr + len_blks -1 ; }trace_f2fs_read_folio (folio,DATA ); -err_out: -if (!folio_in_bio) -folio_end_read (folio, !ret); -if (ret) - goto out; +if (!folio_in_bio) { +if (!folio_test_uptodate (folio)) +folio_mark_uptodate (folio); +folio_unlock (folio); + }if (rac) { folio =readahead_folio (rac); goto next_folio; } out: -f2fs_submit_read_bio (F2FS_I_SB (inode), bio,DATA ); -if (ret && folio_in_bio) { -/* Wait bios and clear uptodate. */ -folio_lock (folio); +f2fs_submit_read_bio (sbi, bio,DATA ); +return ret; +err_out: +f2fs_submit_read_bio (sbi, bio,DATA ); +/* The read completion unlocks a folio in the bio, not uptodate. */ +if (!folio_in_bio) {folio_clear_uptodate (folio);folio_unlock (folio); }
@@ -3501,13 +3551,12 @@ static int f2fs_mpage_readpages(struct inode *inode, struct fsverity_info *vi,pgoff_t nc_cluster_idx =NULL_CLUSTER ;pgoff_t index;#endif -pgoff_t next_pgofs =0 ; +pgoff_t next_blkofs =0 ;unsigned nr_pages = rac ?readahead_count (rac) :1 ; -struct address_space *mapping = rac ? rac->mapping : folio->mapping;unsigned max_nr_pages = nr_pages;int ret =0 ; -if (mapping_large_folio_support (mapping)) +if (f2fs_has_multiblock_folios (inode))return f2fs_read_data_large_folio (inode, vi, rac, folio);#ifdef CONFIG_F2FS_FS_COMPRESSION
@@ -3522,7 +3571,7 @@ static int f2fs_mpage_readpages(struct inode *inode, struct fsverity_info *vi, map.m_lblk =0 ; map.m_len =0 ; map.m_flags =0 ; - map.m_next_pgofs = &next_pgofs; + map.m_next_blkofs = &next_blkofs; map.m_next_extent =NULL ; map.m_seg_type =NO_CHECK_TYPE ; map.m_may_create = false;
@@ -3895,7 +3944,7 @@ int f2fs_do_write_data_page(struct f2fs_io_info *fio)/* * Do not clear gcing flag on failure here: the caller will redirty * the folio and keep the flag so that it can be guaranteed by -* checkpoint, or drop the flag by itself, e.g. move_data_page(). +* checkpoint, or drop the flag by itself, e.g. move_data_cache_block(). */ if (!submitted && pending_added)f2fs_ffs_put_write_pending (folio,1 );
@@ -5276,7 +5325,7 @@ static int prepare_large_folio_write_begin(struct inode *inode, DATA_GENERIC_ENHANCE_READ))return -EFSCORRUPTED ; - err =f2fs_submit_page_read_sync (inode, folio, index, blkaddr); + err =f2fs_submit_block_read_sync (inode, folio, index, blkaddr);if (err)return err; }
@@ -5327,8 +5376,7 @@ static int prepare_large_folio_atomic_write_begin(struct inode *inode, DATA_GENERIC_ENHANCE_READ))return -EFSCORRUPTED ; - err =f2fs_submit_page_read_sync (inode, folio, index, - blkaddr); + err =f2fs_submit_block_read_sync (inode, folio, index, blkaddr);if (err)return err; balance:
@@ -5485,9 +5533,9 @@ static int f2fs_write_begin(const struct kiocb *iocb,* folio's owner; otherwise an unencrypted @inode whose COW inode * is encrypted hits a NULL ->i_crypt_info on decryption. */ -f2fs_submit_page_read (inode, -NULL ,/* can't write to fsverity files */ - folio, index, blkaddr,0 , true); +f2fs_submit_block_read (inode, +NULL ,/* can't write to fsverity files */ + folio, index, blkaddr,0 , true);folio_lock (folio);if (unlikely (folio->mapping != mapping)) {
@@ -5636,6 +5684,16 @@ bool f2fs_release_folio(struct folio *folio, gfp_t wait)if (folio_test_dirty (folio))return false; +/* +* The folio state holds the uptodate state of the blocks of a folio +* that is not fully uptodate. Keep it while the folio has users other +* than the caller, e.g. a dentry lookup that parses a block without +* the folio lock, so that the block is not read again under them. +*/ +if (f2fs_folio_has_ffs (folio) && !folio_test_uptodate (folio) && +folio_ref_count (folio) >folio_expected_ref_count (folio) +1 ) +return false; +ffs_detach_free (folio);return true; }
@@ -5728,7 +5786,7 @@ static sector_t f2fs_bmap(struct address_space *mapping, sector_t block)memset (&map,0 ,sizeof (map)); map.m_lblk = block; map.m_len =1 ; - map.m_next_pgofs =NULL ; + map.m_next_blkofs =NULL ; map.m_seg_type =NO_CHECK_TYPE ;if (!f2fs_map_blocks (inode, &map,F2FS_GET_BLOCK_BMAP ))
@@ -5847,7 +5905,7 @@ static int check_swap_activate(struct swap_info_struct *sis,memset (&map,0 ,sizeof (map)); map.m_lblk = cur_lblock; map.m_len = last_lblock - cur_lblock; - map.m_next_pgofs =NULL ; + map.m_next_blkofs =NULL ; map.m_next_extent =NULL ; map.m_seg_type =NO_CHECK_TYPE ; map.m_may_create = false;
@@ -5987,6 +6045,43 @@ static void f2fs_swap_deactivate(struct file *file) }#endif +static bool f2fs_is_partially_uptodate (struct folio *folio,size_t from, +size_t count) +{ +struct inode *inode = folio->mapping->host; +struct f2fs_sb_info *sbi =F2FS_I_SB (inode); +struct f2fs_folio_state *ffs; +unsigned int first, last, i; + +if (folio_test_uptodate (folio)) +return true; + +if (f2fs_has_inline_data (inode) || !f2fs_is_multiblock_folio (sbi, folio)) +return false; + +if (!f2fs_folio_has_ffs (folio)) +return false; + +if (from >=folio_size (folio)) +return false; + + count =min_t (size_t,folio_size (folio) - from, count); +if (!count) +return false; + + first =F2FS_BYTES_TO_BLK (sbi, from); + last =F2FS_BYTES_TO_BLK (sbi, from + count -1 ); + + ffs = folio->private; + +for (i = first; i <= last; i++) { +if (!test_bit (i, ffs->state)) +return false; + } + +return true; +} +const struct address_space_operations f2fs_dblock_aops = { .read_folio = f2fs_read_data_folio, .readahead = f2fs_readahead,
@@ -5997,6 +6092,7 @@ const struct address_space_operations f2fs_dblock_aops = { .migrate_folio = filemap_migrate_folio, .invalidate_folio = f2fs_invalidate_folio, .release_folio = f2fs_release_folio, + .is_partially_uptodate = f2fs_is_partially_uptodate, .bmap = f2fs_bmap, .swap_activate = f2fs_swap_activate, .swap_deactivate = f2fs_swap_deactivate,
@@ -6087,13 +6183,13 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct f2fs_map_blocks map = {NULL , }; -pgoff_t next_pgofs =0 ; +pgoff_t next_blkofs =0 ;int err; map.m_lblk =F2FS_BYTES_TO_BLK (sbi, offset); map.m_len =F2FS_BYTES_TO_BLK (sbi, offset + length -1 ) - map.m_lblk +1 ; - map.m_next_pgofs = &next_pgofs; + map.m_next_blkofs = &next_blkofs; map.m_seg_type =f2fs_rw_hint_to_seg_type (F2FS_I_SB (inode), inode->i_write_hint);if (flags &IOMAP_WRITE && iomap->private) {
@@ -6146,7 +6242,7 @@ static int f2fs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,return -ENOTBLK ;if (map.m_pblk ==NULL_ADDR ) { - iomap->length =F2FS_BLK_TO_BYTES (sbi, next_pgofs) - + iomap->length =F2FS_BLK_TO_BYTES (sbi, next_blkofs) - iomap->offset; iomap->type =IOMAP_HOLE ; }else if (map.m_pblk ==NEW_ADDR ) {
fs/f2fs/f2fs.h +13 -7
@@ -836,7 +836,7 @@ struct f2fs_map_blocks {unsigned int m_len;unsigned int m_flags;unsigned long m_last_pblk;/* last allocated block, only used for DIO in LFS mode */ -pgoff_t *m_next_pgofs;/* point next possible non-hole pgofs */ +pgoff_t *m_next_blkofs;/* point next possible non-hole blkofs */ pgoff_t *m_next_extent;/* point to next possible extent */ int m_seg_type;bool m_may_create;/* indicate it is from write path */
@@ -2349,6 +2349,12 @@ static inline bool f2fs_is_multiblock_folio(struct f2fs_sb_info *sbi,return folio_size (folio) >F2FS_BLKSIZE (sbi); } +static inline bool f2fs_has_multiblock_folios (struct inode *inode) +{ +return mapping_large_folio_support (inode->i_mapping) || +f2fs_has_subpage_blocks (F2FS_I_SB (inode)); +} +static inline pgoff_t f2fs_folio_lblk (struct f2fs_sb_info *sbi,const struct folio *folio) {
@@ -4240,7 +4246,7 @@ bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);bool f2fs_need_inode_block_update (struct f2fs_sb_info *sbi,nid_t ino);int f2fs_get_node_info (struct f2fs_sb_info *sbi,nid_t nid,struct node_info *ni,bool checkpoint_context); -pgoff_t f2fs_get_next_page_offset (struct dnode_of_data *dn,pgoff_t pgofs); +pgoff_t f2fs_get_next_block_offset (struct dnode_of_data *dn,pgoff_t blkofs);int f2fs_get_dnode_of_data (struct dnode_of_data *dn,pgoff_t index,int mode);int f2fs_truncate_inode_blocks (struct inode *inode,pgoff_t from);int f2fs_truncate_xattr_node (struct inode *inode);
@@ -4503,11 +4509,11 @@ int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);int f2fs_reserve_new_block (struct dnode_of_data *dn);int f2fs_get_block_locked (struct dnode_of_data *dn,pgoff_t index);int f2fs_reserve_block (struct dnode_of_data *dn,pgoff_t index); -struct folio *f2fs_get_read_data_folio (struct inode *inode,pgoff_t index, -blk_opf_t op_flags,bool for_write,pgoff_t *next_pgofs); -struct folio *f2fs_find_data_folio (struct inode *inode,pgoff_t index, -pgoff_t *next_pgofs); -struct folio *f2fs_get_lock_data_folio (struct inode *inode,pgoff_t index, +struct folio *f2fs_get_read_data_folio (struct inode *inode,pgoff_t bidx, +blk_opf_t op_flags,bool for_write,pgoff_t *next_blkofs); +struct folio *f2fs_find_data_folio (struct inode *inode,pgoff_t bidx, +pgoff_t *next_blkofs); +struct folio *f2fs_get_lock_data_folio (struct inode *inode,pgoff_t bidx,bool for_write);struct folio *f2fs_get_new_data_folio (struct inode *inode,struct f2fs_cached_block *ientry,pgoff_t index,bool new_i_size);
fs/f2fs/file.c +11 -11
@@ -571,7 +571,7 @@ static loff_t f2fs_seek_block(struct file *file, loff_t offset, int whence) }else if (err == -ENOENT ) {/* direct node does not exists */ if (whence ==SEEK_DATA ) { - pgofs =f2fs_get_next_page_offset (&dn, pgofs); + pgofs =f2fs_get_next_block_offset (&dn, pgofs);continue ; }else { goto found;
@@ -1356,7 +1356,7 @@ int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end) err =f2fs_get_dnode_of_data (&dn, pg_start,LOOKUP_NODE );if (err) {if (err == -ENOENT ) { - pg_start =f2fs_get_next_page_offset (&dn, + pg_start =f2fs_get_next_block_offset (&dn, pg_start);continue ; }
@@ -2026,7 +2026,7 @@ static int f2fs_expand_inode_data(struct inode *inode, loff_t offset, loff_t len, int mode) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode); -struct f2fs_map_blocks map = { .m_next_pgofs =NULL , +struct f2fs_map_blocks map = { .m_next_blkofs =NULL , .m_next_extent =NULL , .m_seg_type =NO_CHECK_TYPE , .m_may_create = true };struct f2fs_gc_control gc_control = { .victim_segno =NULL_SEGNO ,
@@ -3095,7 +3095,7 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi, .m_seg_type = NO_CHECK_TYPE, .m_may_create = false };struct extent_info ei = {}; -pgoff_t pg_start, pg_end, next_pgofs; +pgoff_t pg_start, pg_end, next_blkofs;unsigned int total =0 , sec_num;block_t blk_end =0 ;bool fragmented = false;
@@ -3139,7 +3139,7 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi, } map.m_lblk = pg_start; - map.m_next_pgofs = &next_pgofs; + map.m_next_blkofs = &next_blkofs;/* * lookup mapping info in dnode page cache, skip defragmenting if all
@@ -3153,7 +3153,7 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi, goto out;if (!(map.m_flags &F2FS_MAP_FLAGS )) { - map.m_lblk = next_pgofs; + map.m_lblk = next_blkofs;continue ; }
@@ -3200,7 +3200,7 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi, goto clear_out;if (!(map.m_flags &F2FS_MAP_FLAGS )) { - map.m_lblk = next_pgofs; + map.m_lblk = next_blkofs; goto check; }
@@ -4092,7 +4092,7 @@ int f2fs_precache_extents(struct inode *inode) map.m_lblk =0 ; map.m_pblk =0 ; - map.m_next_pgofs =NULL ; + map.m_next_blkofs =NULL ; map.m_next_extent = &m_next_extent; map.m_seg_type =NO_CHECK_TYPE ; map.m_may_create = false;
@@ -4385,7 +4385,7 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg)if (ret) {f2fs_unlock_op (sbi, &lc);if (ret == -ENOENT ) { - page_idx =f2fs_get_next_page_offset (&dn, + page_idx =f2fs_get_next_block_offset (&dn, page_idx); ret =0 ;continue ;
@@ -4567,7 +4567,7 @@ static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg)if (ret) {f2fs_unlock_op (sbi, &lc);if (ret == -ENOENT ) { - page_idx =f2fs_get_next_page_offset (&dn, + page_idx =f2fs_get_next_block_offset (&dn, page_idx); ret =0 ;continue ;
@@ -4735,7 +4735,7 @@ static int f2fs_sec_trim_file(struct file *filp, unsigned long arg) ret =f2fs_get_dnode_of_data (&dn, index,LOOKUP_NODE );if (ret) {if (ret == -ENOENT ) { - index =f2fs_get_next_page_offset (&dn, index); + index =f2fs_get_next_block_offset (&dn, index);continue ; } goto out;
fs/f2fs/gc.c +66 -21
181 lines not on this page · raw message
fs/f2fs/node.c +3 -3
@@ -701,7 +701,7 @@ static void f2fs_ra_node_caches(struct f2fs_cached_block *parent, int start, intblk_finish_plug (&plug); } -pgoff_t f2fs_get_next_page_offset (struct dnode_of_data *dn,pgoff_t pgofs) +pgoff_t f2fs_get_next_block_offset (struct dnode_of_data *dn,pgoff_t blkofs) {struct f2fs_sb_info *sbi =F2FS_I_SB (dn->inode);const long direct_index =ADDRS_PER_INODE (dn->inode);
@@ -714,7 +714,7 @@ pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)pgoff_t base =0 ;if (!dn->max_level) -return pgofs +1 ; +return blkofs +1 ;while (max_level-- > cur_level) skipped_unit *=NIDS_PER_BLOCK (sbi);
@@ -733,7 +733,7 @@ pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs)f2fs_bug_on (F2FS_I_SB (dn->inode),1 ); } -return ((pgofs - base) / skipped_unit +1 ) * skipped_unit + base; +return ((blkofs - base) / skipped_unit +1 ) * skipped_unit + base; }/*
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 06/20] f2fs: support subpage blocks in data read path and GC
Quoted 30 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
With subpage blocks, a folio can be partially uptodate, so reads and GC have to work per block.
- Read only the non-uptodate blocks of a multi-block folio, and send every inode to this path in subpage mode.
- Do not use the folio state when a single bio reads the whole folio.
- Implement ->is_partially_uptodate.
- Convert block indices to folio indices in the data folio getters and GC, and prefetch the whole folio in GC so that its blocks are read together.
- Fix error handling inf2fs_read_data_large_folio().
- Zero the blocks past EOF inf2fs_read_data_large_folio()instead of reading them, asf2fs_read_single_page()does. With subpage blocks, they share a page with the last block of the file and can be seen through mmap.
- Test the per-block uptodate state inf2fs_find_data_folio()under the folio lock, as ->release_folio can free the folio state otherwise. A fully uptodate folio is still returned without the lock.
- Keep the folio state of a folio that is not fully uptodate in ->release_folio while the folio has other users. Otherwise a block that a lockless reader, e.g. a dentry lookup, is using can be read from disk again.
- Get sbi from the folio inf2fs_finish_read_bio()only after the compressed page check, as nothing pins the inode of a compressed page once its cluster is unlocked.
- Rename helpers that now take block indices, e.g.f2fs_submit_page_read()tof2fs_submit_block_read().
Quoted 4 lines
The read changes also apply to large folios when the block size equals
PAGE_SIZE.Signed-off-by Daeho Jeong <[email protected]>
Quoted 7 lines
fs/f2fs/data.c +3
-0@@ -5636,6 +5684,16 @@ bool f2fs_release_folio(struct folio *folio, gfp_t wait)if (folio_test_dirty (folio))return false; +/* +* The folio state holds the uptodate state of the blocks of a folio +* that is not fully uptodate. Keep it while the folio has users other
fs/f2fs/data.c bool f2fs_release_folio(struct folio *folio, gfp_t wait) · line 5699
Quoted 10 lines
fs/f2fs/data.c +7
-0+* than the caller, e.g. a dentry lookup that parses a block without +* the folio lock, so that the block is not read again under them. +*/ +if (f2fs_folio_has_ffs (folio) && !folio_test_uptodate (folio) && +folio_ref_count (folio) >folio_expected_ref_count (folio) +1 ) +return false; +ffs_detach_free (folio);return true; }
BTW, we should notice this side effect, dio may be blocked due to it can not invalidate folios which is referenced by other buffered reader.
Thread A (buffered read) Thread B (DIO write, same range)
- filemap_read
- filemap_get_pages
: folio ref taken (batch), folio
partially uptodate, ffs present
- copy_folio_to_iter
- iomap_dio_complete
- kiocb_invalidate_post_direct_write
- invalidate_inode_pages2_range
- folio_unmap_invalidate
- f2fs_release_folio : !uptodate && ref > expected + 1 -> false
: -EBUSY
- dio_warn_stale_pagecache : pr_crit, wb_err = -EIO
Thanks,
Jaegeuk Kim
maintainerRe: [f2fs-dev] [PATCH v2 06/20] f2fs: support subpage blocks in data read path and GC
Quoted 42 lines · Chao Yu
Quoted 30 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
With subpage blocks, a folio can be partially uptodate, so reads and GC have to work per block.
- Read only the non-uptodate blocks of a multi-block folio, and send every inode to this path in subpage mode.
- Do not use the folio state when a single bio reads the whole folio.
- Implement ->is_partially_uptodate.
- Convert block indices to folio indices in the data folio getters and GC, and prefetch the whole folio in GC so that its blocks are read together.
- Fix error handling inf2fs_read_data_large_folio().
- Zero the blocks past EOF inf2fs_read_data_large_folio()instead of reading them, asf2fs_read_single_page()does. With subpage blocks, they share a page with the last block of the file and can be seen through mmap.
- Test the per-block uptodate state inf2fs_find_data_folio()under the folio lock, as ->release_folio can free the folio state otherwise. A fully uptodate folio is still returned without the lock.
- Keep the folio state of a folio that is not fully uptodate in ->release_folio while the folio has other users. Otherwise a block that a lockless reader, e.g. a dentry lookup, is using can be read from disk again.
- Get sbi from the folio inf2fs_finish_read_bio()only after the compressed page check, as nothing pins the inode of a compressed page once its cluster is unlocked.
- Rename helpers that now take block indices, e.g.f2fs_submit_page_read()tof2fs_submit_block_read().Quoted 4 lines
The read changes also apply to large folios when the block size equals
PAGE_SIZE.Signed-off-by Daeho Jeong <[email protected]>Reviewed-by Chao Yu <[email protected]>Quoted 4 lines
fs/f2fs/data.c +0
-0@@ -5636,6 +5684,16 @@ bool f2fs_release_folio(struct folio *folio, gfp_t wait)if (folio_test_dirty (folio))return false;
Quoted 16 lines
fs/f2fs/data.c bool f2fs_release_folio(struct folio *folio, gfp_t wait) · line 5699
Quoted 13 lines
fs/f2fs/data.c +10
-0+/* +* The folio state holds the uptodate state of the blocks of a folio +* that is not fully uptodate. Keep it while the folio has users other +* than the caller, e.g. a dentry lookup that parses a block without +* the folio lock, so that the block is not read again under them. +*/ +if (f2fs_folio_has_ffs (folio) && !folio_test_uptodate (folio) && +folio_ref_count (folio) >folio_expected_ref_count (folio) +1 ) +return false; +ffs_detach_free (folio);return true; }BTW, we should notice this side effect, dio may be blocked due to it can not invalidate folios which is referenced by other buffered reader.
Not from my end. Can we add a bug_on to detect this first with a reproducer?
Quoted 19 lines
Thread A (buffered read) Thread B (DIO write, same range)
-filemap_read
-filemap_get_pages
: folio ref taken (batch), folio
partially uptodate, ffs present
-copy_folio_to_iter
-iomap_dio_complete
-kiocb_invalidate_post_direct_write
-invalidate_inode_pages2_range
-folio_unmap_invalidate
-f2fs_release_folio: !uptodate && ref > expected + 1 -> false
:-EBUSY
-dio_warn_stale_pagecache:pr_crit,wb_err=-EIOThanks,
PATCH v2 7/20 f2fs: support subpage blocks in data write path and writeback
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
With blocks smaller than PAGE_SIZE, a folio holds several blocks, so write_begin, write_end and writeback have to handle individual blocks instead of the whole folio.
- Read, zero and reserve the blocks touched by a write per block in write_begin, and mark only the written blocks dirty in write_end.
- Write back only the dirty blocks of a multi-block folio and complete write I/O per block. In subpage mode every inode is written back by f2fs_write_cache_folios(), which now handles dentry and quota folios and checkpoint errors as f2fs_write_single_data_page() does. Both use the new f2fs_cp_controlled_inode() and f2fs_wb_folio_action() helpers for these rules.
It also resets the error for each folio, so that the remaining folios are still written back after an error, as writeback_iter() expects for integrity writeback, instead of redirtying them. f2fs_write_folio_dirty_range() cleans up each block where it is written or skipped, and advances last_disk_size only up to the last block it submitted. On an error, it clears the dirty state of the blocks before the failed one, so that only the failed block and the ones after it are written again.
- Count inode dirty blocks from the dirty bits of the folio state, and count every block of a folio dirtied without one, e.g. after a read of the whole folio or through folio_mark_dirty(). Attaching a folio state to a dirty folio converts its count to the dirty bits, so the counter stays balanced.
- ->dirty_folio() does not know which blocks were modified, so it marks the uptodate blocks of the folio within EOF dirty. Otherwise, a folio dirtied by mm after writeback cleared its dirty bits, e.g. through a pinned page, is cleaned without being written. The updates made by f2fs mark only the blocks they modify and dirty the folio with f2fs_folio_mark_dirty(), which keeps the other blocks clean.
- Compressed and atomic files are not supported yet in subpage mode:
write_begin returns -EOPNOTSUPP for them. Inline data is converted first.
Some of these changes also apply to large folios when the block size equals PAGE_SIZE.
--- fs/f2fs/checkpoint.c | 65 ++--- fs/f2fs/compress.c | 2 +- fs/f2fs/data.c | 583 ++++++++++++++++++++++++++----------------- fs/f2fs/dir.c | 4 +- fs/f2fs/f2fs.h | 3 + fs/f2fs/file.c | 23 +- fs/f2fs/gc.c | 20 +- fs/f2fs/inline.c | 17 +- fs/f2fs/node.c | 11 +- 9 files changed, 433 insertions(+), 295 deletions(-)
fs/f2fs/checkpoint.c +33 -32
@@ -1324,29 +1324,6 @@ static void __remove_dirty_inode(struct inode *inode, enum inode_type type)stat_dec_dirty_inode (F2FS_I_SB (inode), type); } -/* -* Account @nr dirty blocks (negative when dirtiness is cleared) for @inode. -*/ -void f2fs_account_dirty_blocks (struct inode *inode,int nr) -{ -struct f2fs_sb_info *sbi =F2FS_I_SB (inode); -enum inode_type type =S_ISDIR (inode->i_mode) ?DIR_INODE :FILE_INODE ; - -if (!nr) -return ; - -spin_lock (&sbi->inode_lock[type]); -if (nr >0 ) { -if (type !=FILE_INODE ||test_opt (sbi,DATA_FLUSH )) -__add_dirty_inode (inode, type); -inode_add_dirty_blocks (inode, nr); - }else { -inode_sub_dirty_blocks (inode, -nr); -__remove_dirty_inode (inode, type); - } -spin_unlock (&sbi->inode_lock[type]); -} -void f2fs_update_dirty_folio (struct inode *inode,struct folio *folio) { if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
@@ -1354,16 +1331,13 @@ void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio)return ;/* -* Large folios have a subpage dirty bitmap, so defer their dirty accounting -* to the f2fs_ffs_*subrange_dirty*() helpers. This avoids nesting -* ffs->state_lock with the inode dirty list lock. +* The dirty blocks of a folio with a folio state are accounted when +* their dirty bits are set. Without it, every block of the folio is +* dirty. */ -if (f2fs_folio_has_ffs (folio)) { -folio_set_f2fs_reference (folio); -return ; - } - -f2fs_account_dirty_blocks (inode,1 ); +if (!f2fs_folio_has_ffs (folio)) +f2fs_account_dirty_blocks (inode, +f2fs_blocks_per_folio (F2FS_I_SB (inode), folio));folio_set_f2fs_reference (folio); }
@@ -1385,6 +1359,33 @@ void f2fs_remove_dirty_inode(struct inode *inode)spin_unlock (&sbi->inode_lock[type]); } +/* +* Account @nr dirty blocks (negative when they are cleaned) for @inode. +*/ +void f2fs_account_dirty_blocks (struct inode *inode,int nr) +{ +struct f2fs_sb_info *sbi =F2FS_I_SB (inode); +enum inode_type type =S_ISDIR (inode->i_mode) ?DIR_INODE :FILE_INODE ; + +if (!nr) +return ; + +if (!S_ISDIR (inode->i_mode) && !S_ISREG (inode->i_mode) && + !S_ISLNK (inode->i_mode)) +return ; + +if (nr <0 ) { +inode_sub_dirty_blocks (inode, -nr); +return ; + } + +spin_lock (&sbi->inode_lock[type]); +if (type !=FILE_INODE ||test_opt (sbi,DATA_FLUSH )) +__add_dirty_inode (inode, type); +inode_add_dirty_blocks (inode, nr); +spin_unlock (&sbi->inode_lock[type]); +} +int f2fs_sync_dirty_inodes (struct f2fs_sb_info *sbi,enum inode_type type,bool from_cp) {
fs/f2fs/compress.c +1 -1
@@ -1090,7 +1090,7 @@ static void set_cluster_dirty(struct compress_ctx *cc)if (!cc->rpages[i])continue ; folio =page_folio (cc->rpages[i]); -folio_mark_dirty (folio); +f2fs_folio_mark_dirty (folio);folio_set_f2fs_gcing (folio); } }
fs/f2fs/data.c +350 -233
985 lines not on this page · raw message
fs/f2fs/dir.c +2 -2
@@ -57,7 +57,7 @@ void f2fs_mark_dentry_folio_dirty(struct inode *dir, struct folio *folio, }else if (!folio_test_uptodate (folio)) {folio_mark_uptodate (folio); } -folio_mark_dirty (folio); +f2fs_folio_mark_dirty (folio); }static unsigned long dir_blocks (struct inode *inode)
@@ -979,9 +979,9 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, void *dentry_block,if (!f2fs_has_subpage_blocks (sbi)) {folio_clear_uptodate (folio);folio_detach_private (folio); -inode_dec_dirty_block (dir); } +f2fs_folio_clear_dirty_blocks (folio, true);f2fs_remove_dirty_inode (dir); } keep_dirty:
fs/f2fs/f2fs.h +3 -0
@@ -4531,7 +4531,10 @@ int f2fs_write_single_data_page(struct folio *folio, int *submitted, int compr_blocks, bool allow_balance);bool f2fs_ffs_test_blk_uptodate (const struct folio *folio,pgoff_t index);struct f2fs_folio_state *f2fs_ffs_find_or_alloc (struct folio *folio); +bool f2fs_folio_mark_dirty (struct folio *folio); +void f2fs_folio_clear_dirty_blocks (struct folio *folio,bool dirty);bool f2fs_ffs_test_blk_dirty (const struct folio *folio,pgoff_t index); +void f2fs_ffs_put_write_pending (struct folio *folio,unsigned int nr);void f2fs_ffs_mark_subrange_dirty (struct folio *folio,size_t offset,size_t len);bool f2fs_ffs_has_dirty_subpage (struct folio *folio); bool f2fs_ffs_clear_subrange_dirty_and_test(struct folio *folio, size_t offset,
fs/f2fs/file.c +12 -11
@@ -258,7 +258,7 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)if (folio_test_large (folio))f2fs_ffs_mark_subrange_dirty (folio,0 , dirty_len); -folio_mark_dirty (folio); +f2fs_folio_mark_dirty (folio);f2fs_update_iostat (sbi, inode,APP_MAPPED_IO , dirty_len);f2fs_update_time (sbi,REQ_TIME );
@@ -856,7 +856,7 @@ static int truncate_partial_data_page(struct inode *inode, u64 from,*/ f2fs_ffs_mark_subrange_dirty (folio, folio_off, blk_end - folio_off); -folio_mark_dirty (folio); +f2fs_folio_mark_dirty (folio); }f2fs_folio_put (folio, true);return 0 ;
@@ -1339,7 +1339,7 @@ static int fill_zero(struct inode *inode, pgoff_t index,* f2fs_get_new_data_folio() above. */ f2fs_ffs_mark_subrange_dirty (folio, folio_off, len); -folio_mark_dirty (folio); +f2fs_folio_mark_dirty (folio);f2fs_folio_put (folio, true);return 0 ; }
@@ -1627,7 +1627,7 @@ static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,f2fs_ffs_mark_subrange_dirty (fdst, dst_off,F2FS_BLKSIZE (sbi)); } -folio_mark_dirty (fdst); +f2fs_folio_mark_dirty (fdst);if (same_folio &&i_size_read (dst_inode) <F2FS_BLK_TO_BYTES (sbi, dst + i +1 )) f2fs_i_size_write(dst_inode,
@@ -3222,7 +3222,7 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi,f2fs_ffs_mark_subrange_dirty (folio,offset_in_folio (folio, (loff_t )idx <<PAGE_SHIFT ),PAGE_SIZE ); -folio_mark_dirty (folio); +f2fs_folio_mark_dirty (folio);folio_set_f2fs_gcing (folio);f2fs_folio_put (folio, true);
@@ -4967,7 +4967,7 @@ static int redirty_blocks(struct inode *inode, pgoff_t page_idx, int len)f2fs_ffs_mark_subrange_dirty (folio, off, dlen); } -folio_mark_dirty (folio); +f2fs_folio_mark_dirty (folio);folio_set_f2fs_gcing (folio); redirty_idx =folio_next_index (folio);folio_unlock (folio);
@@ -5561,12 +5561,13 @@ static int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *iter,return ret; } -if (!dio &&mapping_large_folio_support (inode->i_mapping)) { +if (!dio &&f2fs_has_multiblock_folios (inode)) {/* -* Preallocate all blocks touched by a large-folio buffered write so -* the regular write_begin path does not need to unlock the folio for -* f2fs_balance_fs(). Rechecking large-folio state after unlock is -* unreliable since partial truncation can split the folio. +* Preallocate all blocks touched by a multiblock-folio buffered +* write so the regular write_begin path does not need to unlock +* the folio for f2fs_balance_fs(). Rechecking large-folio +* state after unlock is unreliable since partial truncation can +* split the folio. */ map.m_lblk =F2FS_BYTES_TO_BLK (sbi, pos); map.m_len =F2FS_BLK_ALIGN (sbi, pos + count);
fs/f2fs/gc.c +10 -10
@@ -1586,7 +1586,7 @@ static int move_data_cache_block(struct inode *inode, block_t bidx, int gc_type, }if (has_ffs)f2fs_ffs_mark_subrange_dirty (folio, foff,F2FS_BLKSIZE (sbi)); -folio_mark_dirty (folio); +f2fs_folio_mark_dirty (folio);folio_set_f2fs_gcing (folio); }else { struct f2fs_io_info fio = {
@@ -1611,15 +1611,15 @@ static int move_data_cache_block(struct inode *inode, block_t bidx, int gc_type,if (has_ffs)f2fs_ffs_mark_subrange_dirty (folio, foff,F2FS_BLKSIZE (sbi)); -folio_mark_dirty (folio); +f2fs_folio_mark_dirty (folio);if (folio_clear_dirty_for_io (folio)) { -if (!has_ffs) { -inode_dec_dirty_block (inode); -f2fs_remove_dirty_inode (inode); - }else if (f2fs_ffs_clear_subrange_dirty_and_test (folio, - foff,F2FS_BLKSIZE (sbi))) { -folio_mark_dirty (folio); - } +/* The other dirty blocks keep the folio dirty. */ +if (!has_ffs) +f2fs_folio_clear_dirty_blocks (folio, true); +else if (f2fs_ffs_clear_subrange_dirty_and_test (folio, + foff,F2FS_BLKSIZE (sbi))) +f2fs_folio_mark_dirty (folio); +f2fs_remove_dirty_inode (inode); }folio_set_f2fs_gcing (folio);
@@ -1638,7 +1638,7 @@ static int move_data_cache_block(struct inode *inode, block_t bidx, int gc_type,if (has_ffs)f2fs_ffs_mark_subrange_dirty (folio, foff,F2FS_BLKSIZE (sbi)); -folio_mark_dirty (folio); +f2fs_folio_mark_dirty (folio);folio_set_f2fs_gcing (folio); }else if (!f2fs_ffs_has_dirty_subpage (folio)) {/* migrate this block in later GC */
fs/f2fs/inline.c +15 -2
@@ -187,7 +187,7 @@ int f2fs_convert_inline_folio(struct dnode_of_data *dn, struct folio *folio)f2fs_bug_on (F2FS_F_SB (folio),folio_test_writeback (folio));f2fs_do_read_inline_data (folio, dn->inode_entry); -folio_mark_dirty (folio); +f2fs_folio_mark_dirty (folio);/* clear dirty state */ dirty =folio_clear_dirty_for_io (folio);
@@ -196,10 +196,23 @@ int f2fs_convert_inline_folio(struct dnode_of_data *dn, struct folio *folio)folio_start_writeback (folio); fio.old_blkaddr = dn->data_blkaddr;set_inode_flag (dn->inode,FI_HOT_DATA ); +if (f2fs_is_multiblock_folio (fio.sbi, folio)) { +struct f2fs_folio_state *ffs =f2fs_ffs_find_or_alloc (folio); + +atomic_inc (&ffs->write_blocks_pending); + }f2fs_outplace_write_data (dn, &fio); +/* Drop the pending write that a failed submission leaves behind. */ +if (!fio.submitted &&f2fs_is_multiblock_folio (fio.sbi, folio)) +f2fs_ffs_put_write_pending (folio,1 );f2fs_folio_wait_writeback (folio, true, true);if (dirty) { -inode_dec_dirty_block (dn->inode); +/* +* The folio of inline data was dirtied without a folio state, +* so it was accounted as a whole. +*/ +f2fs_account_dirty_blocks (dn->inode, + -(int )f2fs_blocks_per_folio (fio.sbi, folio));f2fs_remove_dirty_inode (dn->inode); }
fs/f2fs/node.c +7 -4
@@ -1700,10 +1700,13 @@ static void flush_inline_data(struct f2fs_sb_info *sbi, nid_t ino) goto folio_out; ret =f2fs_write_inline_data (inode, folio); -inode_dec_dirty_block (inode); -f2fs_remove_dirty_inode (inode); -if (ret) -folio_mark_dirty (folio); +if (ret) { +/* Keep it dirty as it was, with its dirty blocks accounted. */ +filemap_dirty_folio (folio->mapping, folio); + }else { +f2fs_folio_clear_dirty_blocks (folio, true); +f2fs_remove_dirty_inode (inode); + } folio_out:f2fs_folio_put (folio, true); iput_out:
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 07/20] f2fs: support subpage blocks in data write path and writeback
Quoted 37 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
With blocks smaller than
PAGE_SIZE, a folio holds several blocks, sowrite_begin,write_endand writeback have to handle individual blocks instead of the whole folio.- Read, zero and reserve the blocks touched by a write per block in
write_begin, and mark only the written blocks dirty inwrite_end.
- Write back only the dirty blocks of a multi-block folio and complete write I/O per block. In subpage mode every inode is written back byf2fs_write_cache_folios(), which now handles dentry and quota folios and checkpoint errors asf2fs_write_single_data_page()does. Both use the newf2fs_cp_controlled_inode()andf2fs_wb_folio_action()helpers for these rules.
It also resets the error for each folio, so that the remaining folios are still written back after an error, aswriteback_iter()expects for integrity writeback, instead of redirtying them.f2fs_write_folio_dirty_range()cleans up each block where it is written or skipped, and advanceslast_disk_sizeonly up to the last block it submitted. On an error, it clears the dirty state of the blocks before the failed one, so that only the failed block and the ones after it are written again.
- Count inode dirty blocks from the dirty bits of the folio state, and count every block of a folio dirtied without one, e.g. after a read of the whole folio or throughfolio_mark_dirty(). Attaching a folio state to a dirty folio converts its count to the dirty bits, so the counter stays balanced.
- ->dirty_folio() does not know which blocks were modified, so it marks the uptodate blocks of the folio within EOF dirty. Otherwise, a folio dirtied by mm after writeback cleared its dirty bits, e.g. through a pinned page, is cleaned without being written. The updates made by f2fs mark only the blocks they modify and dirty the folio withf2fs_folio_mark_dirty(), which keeps the other blocks clean.
- Compressed and atomic files are not supported yet in subpage mode:
write_beginreturns-EOPNOTSUPPfor them. Inline data is converted first.
Quoted 4 lines
Some of these changes also apply to large folios when the block size equals
PAGE_SIZE.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 8/20 f2fs: adapt range operations and mmap for subpage blocks
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
- A writable PTE covers the whole folio, so page_mkwrite prepares every block of the folio up to EOF and dirties only those blocks. It reserves them only where it may allocate, i.e. not for a pinned file or a compressed cluster.
- Align zero range, punch hole, preallocation and truncation to the block size instead of PAGE_SIZE. When i_size grows, write-protect the folio straddling the old EOF so that stores to its new blocks fault into page_mkwrite.
- Update i_size of an extending direct write in ->end_io(), before iomap invalidates the page cache of the written range, as ext4 does. Otherwise, a read in between zero-fills the blocks beyond the old i_size in the folio straddling it and marks them uptodate, which hides the data just written.
- Unmap every base page overlapping a punched or zeroed range, and truncate the page cache without truncate_pagecache_range(), whose unmap underflows for a range ending in the first page.
- For SEEK_DATA, treat a NEW_ADDR block as data only if the block itself is dirty.
- When truncation zeroes the tail of a page, mark only the block holding the new EOF dirty, not the blocks after it.
The SEEK_DATA change also applies to large folios when the block size equals PAGE_SIZE. The direct write change applies to any block size, so i_size is also updated before the sync of an O_DSYNC direct write.
--- fs/f2fs/data.c | 69 ++++++++++-- fs/f2fs/f2fs.h | 4 +- fs/f2fs/file.c | 300 +++++++++++++++++++++++++++++++++---------------- 3 files changed, 261 insertions(+), 112 deletions(-)
fs/f2fs/data.c +57 -12
@@ -3257,7 +3257,7 @@ bool f2fs_ffs_dirty_blk_range(struct folio *folio, unsigned int from,static bool find_next_valid_block (const struct folio *folio,size_t orig_off,size_t *need_off, -size_t len) +size_t len,bool read_full) {struct f2fs_sb_info *sbi =F2FS_M_SB (folio->mapping);size_t start = orig_off;
@@ -3265,6 +3265,20 @@ static bool find_next_valid_block(const struct folio *folio,size_t head, tail;pgoff_t index; +/* Find every block that is not uptodate, not only the partial ones. */ +if (read_full) { +for (head =round_down (*need_off,F2FS_BLKSIZE (sbi)); head < end; + head +=F2FS_BLKSIZE (sbi)) { + index =f2fs_folio_lblk (sbi, folio) + +F2FS_BYTES_TO_BLK (sbi, head); +if (!f2fs_ffs_test_blk_uptodate (folio, index)) { + *need_off = head; +return true; + } + } +return false; + } +if (start &F2FS_BLKSIZE_MASK (sbi)) { head =round_down (start,F2FS_BLKSIZE (sbi)); index = f2fs_folio_lblk(sbi, folio) +
@@ -5289,7 +5303,8 @@ static bool f2fs_balance_fs_folio_locked(struct folio *folio,static int prepare_large_folio_write_begin (struct inode *inode,struct folio *folio,loff_t pos, -unsigned int len) +unsigned int len,bool read_full, +bool preallocated,bool balance) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);size_t ori_off =offset_in_folio (folio, pos);
@@ -5301,11 +5316,11 @@ static int prepare_large_folio_write_begin(struct inode *inode,return -EOPNOTSUPP ;/* -* when the write did not come through f2fs_write_iter, we preallocate all -* blocks in the write range here. +* f2fs_balance_fs_folio_locked() unlocks the folio to balance. Skip it +* for page_mkwrite (!@balance), which cannot unlock the folio and has +* called f2fs_balance_fs() before locking it. */ -if (!is_inode_flag_set (inode,FI_PREALLOCATED_ALL ) && - !IS_DEVICE_ALIASING (inode)) { +if (!preallocated && !IS_DEVICE_ALIASING (inode)) {pgoff_t first =F2FS_BYTES_TO_BLK (sbi, pos);pgoff_t last =F2FS_BLK_ALIGN (sbi, pos + len);pgoff_t idx;
@@ -5318,8 +5333,9 @@ static int prepare_large_folio_write_begin(struct inode *inode, &node_changed);if (err)return err; -if (f2fs_balance_fs_folio_locked (folio, folio->mapping, - node_changed)) +if (balance && +f2fs_balance_fs_folio_locked (folio, folio->mapping, + node_changed))return -EAGAIN ; } }
@@ -5329,14 +5345,15 @@ static int prepare_large_folio_write_begin(struct inode *inode,/* Skip read if the folio is already fully uptodate or the write * covers the entire folio. */ -if (folio_test_uptodate (folio) || len ==folio_size (folio)) +if (folio_test_uptodate (folio) || + (!read_full && len ==folio_size (folio)))return 0 ;/* * Skip read if the write starts at a block boundary and reaches EOF, * and zero the rest of the folio instead. */ -if (!f2fs_verity_in_progress (inode) && +if (!read_full && !f2fs_verity_in_progress (inode) && !(pos &F2FS_BLKSIZE_MASK (sbi)) && (pos + len) >=i_size_read (inode)) {size_t zoff = ori_off + len;
@@ -5349,7 +5366,7 @@ static int prepare_large_folio_write_begin(struct inode *inode,f2fs_bug_on (sbi,f2fs_has_inline_data (inode));while (find_next_valid_block (folio, ori_off, - &need_off, len)) { + &need_off, len, read_full)) {struct dnode_of_data dn;pgoff_t index =f2fs_folio_lblk (sbi, folio) +F2FS_BYTES_TO_BLK (sbi, need_off);
@@ -5449,6 +5466,20 @@ static int prepare_large_folio_atomic_write_begin(struct inode *inode,return 0 ; } +int f2fs_prepare_subpage_mkwrite (struct inode *inode,struct folio *folio, +loff_t pos,unsigned int len,bool need_alloc) +{ +/* +* mkwrite runs without inode_lock, so FI_PREALLOCATED_ALL may have +* been set by a concurrent write() for a different range: reserve +* the blocks here, unless @need_alloc is false, as for a pinned file +* or a compressed cluster. It calls f2fs_balance_fs() before locking +* the folio, which it cannot unlock here, so do not balance. +*/ +return prepare_large_folio_write_begin (inode, folio, pos, len, true, + !need_alloc, false); +} + static int f2fs_write_begin(const struct kiocb *iocb, struct address_space *mapping, loff_t pos, unsigned len, struct folio **foliop,
@@ -5557,7 +5588,10 @@ static int f2fs_write_begin(const struct kiocb *iocb, err =prepare_large_folio_atomic_write_begin (inode, mapping, folio, pos, len, retried); }else { - err =prepare_large_folio_write_begin (inode, folio, pos, blen); + err =prepare_large_folio_write_begin (inode, folio, pos, blen, + false, +is_inode_flag_set (inode,FI_PREALLOCATED_ALL ), + true); }if (err) {if (err == -EAGAIN ) {
@@ -5633,6 +5667,7 @@ static int f2fs_write_end(const struct kiocb *iocb, struct folio *folio, void *fsdata) { struct inode *inode = folio->mapping->host; +loff_t old_size =i_size_read (inode);trace_f2fs_write_end (inode, pos, len, copied);
@@ -5662,6 +5697,8 @@ static int f2fs_write_end(const struct kiocb *iocb,if (pos + copied >i_size_read (inode) && !f2fs_verity_in_progress (inode))f2fs_i_size_write (inode, pos + copied); +if (i_size_read (inode) > old_size) +pagecache_isize_extended (inode, old_size, pos); return copied; }#endif
@@ -5687,6 +5724,14 @@ static int f2fs_write_end(const struct kiocb *iocb, unlock_out:f2fs_folio_put (folio, true);f2fs_update_time (F2FS_I_SB (inode),REQ_TIME ); + +/* +* The folio straddling the old EOF may still be mapped writable, and +* the blocks between the old EOF and pos in it are not prepared. +* Write-protect it so that mmap stores to them fault. +*/ +if (i_size_read (inode) > old_size) +pagecache_isize_extended (inode, old_size, pos);return copied; }
fs/f2fs/f2fs.h +3 -1
@@ -4094,7 +4094,7 @@ int f2fs_getattr(const struct mnt_idmap *idmap, const struct path *path, struct kstat *stat, u32 request_mask, unsigned int flags);int f2fs_setattr (const struct mnt_idmap *idmap,struct dentry *dentry,struct iattr *attr); -int f2fs_truncate_hole (struct inode *inode,pgoff_t pg_start,pgoff_t pg_end); +int f2fs_truncate_hole (struct inode *inode,pgoff_t blk_start,pgoff_t blk_end);void f2fs_truncate_data_blocks_range (struct dnode_of_data *dn,int count);int f2fs_do_shutdown (struct f2fs_sb_info *sbi,unsigned int flag,bool readonly,bool need_lock);
@@ -4543,6 +4543,8 @@ void f2fs_ffs_clear_subrange_uptodate(struct folio *folio, size_t offset, size_t len);bool f2fs_ffs_dirty_blk_range (struct folio *folio,unsigned int from,unsigned int to,unsigned int *start,unsigned int *count); +int f2fs_prepare_subpage_mkwrite (struct inode *inode,struct folio *folio, +loff_t pos,unsigned int len,bool need_alloc);void f2fs_write_failed (struct inode *inode,loff_t to);void f2fs_invalidate_folio (struct folio *folio,size_t offset,size_t length);bool f2fs_release_folio (struct folio *folio,gfp_t wait);
fs/f2fs/file.c +201 -99
638 lines not on this page · raw message
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 08/20] f2fs: adapt range operations and mmap for subpage blocks
Quoted 23 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
- A writable PTE covers the whole folio, so
page_mkwriteprepares every block of the folio up to EOF and dirties only those blocks. It reserves them only where it may allocate, i.e. not for a pinned file or a compressed cluster.
- Align zero range, punch hole, preallocation and truncation to the block size instead ofPAGE_SIZE. Wheni_sizegrows, write-protect the folio straddling the old EOF so that stores to its new blocks fault intopage_mkwrite.
- Updatei_sizeof an extending direct write in ->end_io(), before iomap invalidates the page cache of the written range, as ext4 does. Otherwise, a read in between zero-fills the blocks beyond the oldi_sizein the folio straddling it and marks them uptodate, which hides the data just written.
- Unmap every base page overlapping a punched or zeroed range, and truncate the page cache withouttruncate_pagecache_range(), whose unmap underflows for a range ending in the first page.
- ForSEEK_DATA, treat aNEW_ADDRblock as data only if the block itself is dirty.
- When truncation zeroes the tail of a page, mark only the block holding the new EOF dirty, not the blocks after it.
Quoted 5 lines
The
SEEK_DATAchange also applies to large folios when the block size equalsPAGE_SIZE. The direct write change applies to any block size, soi_sizeis also updated before the sync of anO_DSYNCdirect write.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 9/20 f2fs: support subpage blocks in pinned files
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
Pinned files must not allocate blocks outside fallocate(). As write() does, return -EIO from page_mkwrite when the folio range up to EOF is not fully allocated, and remove the -EOPNOTSUPP checks.
page_mkwrite keeps failing a device aliasing file with -EIO, as it does when the block size equals PAGE_SIZE. Its block addresses are only in the extent cache, so f2fs_overwrite_io() does not catch it.
--- fs/f2fs/file.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
fs/f2fs/file.c +16 -6
@@ -142,7 +142,8 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf)struct folio *folio =page_folio (vmf->page);struct inode *inode =file_inode (vmf->vma->vm_file);struct f2fs_sb_info *sbi =F2FS_I_SB (inode); -bool need_alloc = !f2fs_is_pinned_file (inode); +bool pinned =f2fs_is_pinned_file (inode); +bool need_alloc = !pinned;pgoff_t pidx = folio->index +folio_page_idx (folio, vmf->page);loff_t pos = (loff_t )pidx <<PAGE_SHIFT ;pgoff_t bidx =F2FS_BYTES_TO_BLK (sbi, pos);
@@ -245,6 +246,19 @@ static vm_fault_t f2fs_vm_page_mkwrite(struct vm_fault *vmf) nr =DIV_ROUND_UP (dirty_len,PAGE_SIZE );if (f2fs_has_subpage_blocks (sbi)) { +/* +* A device aliasing file has its block addresses only in the +* extent cache, where f2fs_overwrite_io() finds them. Fail it +* as f2fs_get_block_mkwrite() does. +*/ +if (pinned && + (IS_DEVICE_ALIASING (inode) || + !f2fs_overwrite_io (inode,folio_pos (folio), dirty_len))) { +folio_unlock (folio); + err = -EIO ; + goto out_sem; + } +/* * A writable PTE covers the whole base folio. Like ext4's * block_page_mkwrite(), prepare every filesystem block through EOF
@@ -2285,8 +2299,7 @@ static long f2fs_fallocate(struct file *file, int mode,if (f2fs_has_subpage_blocks (F2FS_I_SB (inode))) {if ((mode & (FALLOC_FL_COLLAPSE_RANGE |FALLOC_FL_INSERT_RANGE )) || -f2fs_is_pinned_file (inode) ||f2fs_is_atomic_file (inode) || -f2fs_compressed_file (inode)) { +f2fs_is_atomic_file (inode) ||f2fs_compressed_file (inode)) { ret = -EOPNOTSUPP ; goto out; }
@@ -3828,9 +3841,6 @@ static int f2fs_ioc_set_pin_file(struct file *filp, unsigned long arg)__u32 pin;int ret =0 ; -if (f2fs_has_subpage_blocks (sbi)) -return -EOPNOTSUPP ; -if (get_user (pin, (__u32 __user *)arg))return -EFAULT ;
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 09/20] f2fs: support subpage blocks in pinned files
Quoted 11 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
Pinned files must not allocate blocks outside
fallocate(). Aswrite()does, return-EIOfrompage_mkwritewhen the folio range up to EOF is not fully allocated, and remove the-EOPNOTSUPPchecks.
page_mkwritekeeps failing a device aliasing file with-EIO, as it does when the block size equalsPAGE_SIZE. Its block addresses are only in the extent cache, sof2fs_overwrite_io()does not catch it.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 10/20 f2fs: support subpage blocks in collapse and insert range
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
The folio holding offset may hold blocks before the shifted range. Write back the page cache from the PAGE_SIZE boundary below offset under invalidate_lock before truncating it from there, so that no data is lost. Detect src and dst in the same folio by folio index instead of block index, as a folio holds several blocks. Remove the -EOPNOTSUPP check.
Also take i_gc_rwsem[WRITE] and invalidate_lock before that writeback and keep them until the moved blocks are written back and the page cache is dropped, as move range does. After the block exchange, the page cache holds the old data of the source blocks until then. With subpage blocks, page_mkwrite dirties every block of a folio up to EOF, so a store to a neighbouring block in between writes the old data back, over a hole or over the moved data. This also keeps GC away from the inode until then, for any block size.
--- fs/f2fs/file.c | 115 ++++++++++++++++++++++++++++--------------------- 1 file changed, 65 insertions(+), 50 deletions(-)
fs/f2fs/file.c +65 -50
@@ -1671,6 +1671,7 @@ static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode, }else {struct folio *fsrc, *fdst;size_t src_off, dst_off; +pgoff_t dst_fidx =f2fs_lblk_to_folio_index (sbi, dst + i);bool same_folio; fsrc = f2fs_get_lock_data_folio(src_inode,
@@ -1683,11 +1684,11 @@ static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,/* * For same-inode operations (collapse/insert), src and -* dst may fall within the same large folio. Detect this +* dst may fall within the same folio. Detect this * to avoid self-deadlock on folio lock. */ same_folio = src_inode == dst_inode && -folio_contains (fsrc, dst + i); +folio_contains (fsrc, dst_fidx);if (same_folio) {struct dnode_of_data dn;
@@ -1795,30 +1796,30 @@ static int f2fs_do_collapse(struct inode *inode, loff_t offset, loff_t len) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct f2fs_lock_context lc; -pgoff_t nrpages =F2FS_BLK_ALIGN (sbi,i_size_read (inode)); +pgoff_t nrblocks =F2FS_BLK_ALIGN (sbi,i_size_read (inode));pgoff_t start =F2FS_BYTES_TO_BLK (sbi, offset);pgoff_t end =F2FS_BYTES_TO_BLK (sbi, offset + len);int ret; -f2fs_balance_fs (sbi, true); - -/* avoid gc operation during block exchange */ -f2fs_down_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]); -filemap_invalidate_lock (inode->i_mapping); +/* +* Write back under invalidate_lock, which blocks page_mkwrite, so +* that truncate_pagecache() below cannot drop a racing mmap store to +* the blocks before @offset in the head folio. +*/ + ret =filemap_write_and_wait_range (inode->i_mapping, +round_down (offset,PAGE_SIZE ),LLONG_MAX ); +if (ret) +return ret; ret =f2fs_zero_post_eof_page (inode, offset + len, false, false);if (ret) - goto out_unlock; +return ret;f2fs_lock_op (sbi, &lc);f2fs_drop_extent_tree (inode); -truncate_pagecache (inode, offset); - ret =__exchange_data_block (inode, inode, end, start, nrpages - end, true); +truncate_pagecache (inode,round_down (offset,PAGE_SIZE )); + ret =__exchange_data_block (inode, inode, end, start, nrblocks - end, true);f2fs_unlock_op (sbi, &lc); - -out_unlock: -filemap_invalidate_unlock (inode->i_mapping); -f2fs_up_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]);return ret; }
@@ -1839,28 +1840,35 @@ static int f2fs_collapse_range(struct inode *inode, loff_t offset, loff_t len)if (ret)return ret; -/* write out all dirty pages from offset */ - ret =filemap_write_and_wait_range (inode->i_mapping, offset,LLONG_MAX ); -if (ret) -return ret; +f2fs_balance_fs (F2FS_I_SB (inode), true); + +/* +* Avoid GC during the block exchange, and keep both locks until the +* moved blocks are written back and the page cache is dropped. Until +* then, it holds the old data of the source blocks, which neither +* page_mkwrite nor GC may write back. +*/ +f2fs_down_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]); +filemap_invalidate_lock (inode->i_mapping); ret =f2fs_do_collapse (inode, offset, len);if (ret) -return ret; + goto out_unlock;/* write out all moved pages, if possible */ -filemap_invalidate_lock (inode->i_mapping); - ret =filemap_write_and_wait_range (inode->i_mapping, offset,LLONG_MAX ); + ret =filemap_write_and_wait_range (inode->i_mapping, +round_down (offset,PAGE_SIZE ),LLONG_MAX );if (ret) goto out_unlock; -truncate_pagecache (inode, offset); +truncate_pagecache (inode,round_down (offset,PAGE_SIZE )); new_size =i_size_read (inode) - len; ret =f2fs_truncate_blocks (inode, new_size, true); -out_unlock: -filemap_invalidate_unlock (inode->i_mapping);if (!ret)f2fs_i_size_write (inode, new_size); +out_unlock: +filemap_invalidate_unlock (inode->i_mapping); +f2fs_up_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]);return ret; }
@@ -2040,7 +2048,7 @@ static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len) {struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct address_space *mapping = inode->i_mapping; -pgoff_t nr, pg_start, pg_end, delta, idx; +pgoff_t nr, blk_start, blk_end, delta, idx;loff_t new_size;int ret =0 ;
@@ -2053,8 +2061,8 @@ static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)return -EINVAL ;/* insert range should be aligned to block size of f2fs. */ -if (offset &F2FS_BLKSIZE_MASK (F2FS_I_SB (inode)) || - len &F2FS_BLKSIZE_MASK (F2FS_I_SB (inode))) +if (offset &F2FS_BLKSIZE_MASK (sbi) || + len &F2FS_BLKSIZE_MASK (sbi))return -EINVAL ; ret =f2fs_convert_inline_inode (inode);
@@ -2069,29 +2077,34 @@ static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len)if (ret)return ret; -/* write out all dirty pages from offset */ - ret =filemap_write_and_wait_range (mapping, offset,LLONG_MAX ); -if (ret) -return ret; - - pg_start =F2FS_BYTES_TO_BLK (sbi, offset); - pg_end =F2FS_BYTES_TO_BLK (sbi, offset + len); - delta = pg_end - pg_start; + blk_start =F2FS_BYTES_TO_BLK (sbi, offset); + blk_end =F2FS_BYTES_TO_BLK (sbi, offset + len); + delta = blk_end - blk_start; idx =F2FS_BLK_ALIGN (sbi,i_size_read (inode));/* avoid gc operation during block exchange */ f2fs_down_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]);filemap_invalidate_lock (mapping); +/* +* Write back under invalidate_lock, which blocks page_mkwrite, so +* that truncate_pagecache() below cannot drop a racing mmap store to +* the blocks before @offset in the head folio. +*/ + ret =filemap_write_and_wait_range (mapping, +round_down (offset,PAGE_SIZE ),LLONG_MAX ); +if (ret) + goto out_unlock; + ret =f2fs_zero_post_eof_page (inode, offset + len, false, false);if (ret) goto out_unlock; -truncate_pagecache (inode, offset); +truncate_pagecache (inode,round_down (offset,PAGE_SIZE )); -while (!ret && idx > pg_start) { +while (!ret && idx > blk_start) {struct f2fs_lock_context lc; - nr = idx - pg_start; + nr = idx - blk_start;if (nr > delta) nr = delta; idx -= nr;
@@ -2103,20 +2116,23 @@ static int f2fs_insert_range(struct inode *inode, loff_t offset, loff_t len) idx + delta, nr, false);f2fs_unlock_op (sbi, &lc); } -out_unlock: -filemap_invalidate_unlock (mapping); -f2fs_up_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]);if (ret) -return ret; + goto out_unlock; -/* write out all moved pages, if possible */ -filemap_invalidate_lock (mapping); - ret =filemap_write_and_wait_range (mapping, offset,LLONG_MAX ); -truncate_pagecache (inode, offset); -filemap_invalidate_unlock (mapping); +/* +* Write out all moved pages, if possible. Keep both locks until the +* page cache is dropped. Until then, it holds the old data of the +* source blocks, which neither page_mkwrite nor GC may write back. +*/ + ret =filemap_write_and_wait_range (mapping, +round_down (offset,PAGE_SIZE ),LLONG_MAX ); +truncate_pagecache (inode,round_down (offset,PAGE_SIZE ));if (!ret)f2fs_i_size_write (inode, new_size); +out_unlock: +filemap_invalidate_unlock (mapping); +f2fs_up_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]);return ret; }
@@ -2298,8 +2314,7 @@ static long f2fs_fallocate(struct file *file, int mode, }if (f2fs_has_subpage_blocks (F2FS_I_SB (inode))) { -if ((mode & (FALLOC_FL_COLLAPSE_RANGE |FALLOC_FL_INSERT_RANGE )) || -f2fs_is_atomic_file (inode) ||f2fs_compressed_file (inode)) { +if (f2fs_is_atomic_file (inode) ||f2fs_compressed_file (inode)) { ret = -EOPNOTSUPP ; goto out; }
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 10/20] f2fs: support subpage blocks in collapse and insert range
Quoted 18 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
The folio holding offset may hold blocks before the shifted range. Write back the page cache from the
PAGE_SIZEboundary below offset underinvalidate_lockbefore truncating it from there, so that no data is lost. Detect src and dst in the same folio by folio index instead of block index, as a folio holds several blocks. Remove the-EOPNOTSUPPcheck.Also take i_gc_rwsem[WRITE] and
invalidate_lockbefore that writeback and keep them until the moved blocks are written back and the page cache is dropped, as move range does. After the block exchange, the page cache holds the old data of the source blocks until then. With subpage blocks,page_mkwritedirties every block of a folio up to EOF, so a store to a neighbouring block in between writes the old data back, over a hole or over the moved data. This also keeps GC away from the inode until then, for any block size.
Quoted 1 line
Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 11/20 f2fs: support subpage blocks in move range
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
Folios at both ends of the moved ranges can straddle the range boundaries. Write back and truncate the page cache from PAGE_SIZE boundaries, and hold invalidate_lock of both inodes during the exchange so that faults and readahead cannot bring back stale data. After the exchange, also after one that failed part way, drop the source blocks that it read into the page cache, and call pagecache_isize_extended() for the gap between the old EOF of dst and the moved data. The page cache was not truncated before, so this also applies when the block size equals PAGE_SIZE. Remove the -EOPNOTSUPP check.
Zero the cached data past the old EOF of dst with f2fs_zero_post_eof_page() before the move extends dst over it, as the other paths that extend a file do. Otherwise data stored past EOF through a mapping becomes file data. This also applies when the block size equals PAGE_SIZE.
--- fs/f2fs/file.c | 73 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 15 deletions(-)
fs/f2fs/file.c +58 -15
@@ -3435,7 +3435,7 @@ static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,size_t olen = len;loff_t dst_max_i_size =0 ;loff_t dst_osize, dst_end; -int ret; +int ret, err;if (file_in->f_path.mnt != file_out->f_path.mnt || src->i_sb != dst->i_sb)
@@ -3516,17 +3516,6 @@ static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,if (ret) goto out_unlock; -/* write out all dirty pages from offset */ - ret =filemap_write_and_wait_range (src->i_mapping, - pos_in, pos_in + len); -if (ret) - goto out_unlock; - - ret =filemap_write_and_wait_range (dst->i_mapping, - pos_out, pos_out + len); -if (ret) - goto out_unlock; -f2fs_balance_fs (sbi, true);f2fs_down_write (&F2FS_I (src)->i_gc_rwsem[WRITE ]);
@@ -3536,6 +3525,39 @@ static int f2fs_move_file_range(struct file *file_in, loff_t pos_in, goto out_src; } +/* +* Hold invalidate_lock across writeback, truncation and the block +* exchange so neither faults nor readahead can repopulate the page +* cache with pre-exchange data. +*/ +filemap_invalidate_lock_two (src->i_mapping, dst->i_mapping); + +/* +* Zero the cached data past the old EOF of dst, such as mmap stores +* past EOF, before the move extends dst over it. +*/ + ret =f2fs_zero_post_eof_page (dst, dst_end, false, false); +if (ret) + goto out_inval; + +/* write out all dirty pages from offset */ + ret =filemap_write_and_wait_range (src->i_mapping, +round_down (pos_in,PAGE_SIZE ), +round_up (pos_in + len,PAGE_SIZE ) -1 ); +if (ret) + goto out_inval; + + ret =filemap_write_and_wait_range (dst->i_mapping, +round_down (pos_out,PAGE_SIZE ), +round_up (pos_out + len,PAGE_SIZE ) -1 ); +if (ret) + goto out_inval; + +truncate_pagecache_range (src,round_down (pos_in,PAGE_SIZE ), +round_up (pos_in + len,PAGE_SIZE ) -1 ); +truncate_pagecache_range (dst,round_down (pos_out,PAGE_SIZE ), +round_up (pos_out + len,PAGE_SIZE ) -1 ); +f2fs_lock_op (sbi, &lc); ret = __exchange_data_block(src, dst, F2FS_BYTES_TO_BLK(sbi, pos_in), F2FS_BYTES_TO_BLK(sbi, pos_out),
@@ -3549,6 +3571,30 @@ static int f2fs_move_file_range(struct file *file_in, loff_t pos_in, }f2fs_unlock_op (sbi, &lc); +/* +* __clone_blkaddrs() reads the checkpointed source blocks into the +* page cache before punching them, so drop them again. Do it after a +* failed exchange too: the blocks moved before the failure were +* punched as well. +*/ + err =filemap_write_and_wait_range (src->i_mapping, +round_down (pos_in,PAGE_SIZE ), +round_up (pos_in + len,PAGE_SIZE ) -1 ); +if (!err) +truncate_pagecache_range (src,round_down (pos_in,PAGE_SIZE ), +round_up (pos_in + len,PAGE_SIZE ) -1 ); +if (!ret) + ret = err; + +/* +* Write-protect the folio straddling the old EOF of dst, so that mmap +* stores to the blocks between it and the moved data fault. +*/ +if (i_size_read (dst) > dst_osize) +pagecache_isize_extended (dst, dst_osize, pos_out); +out_inval: +filemap_invalidate_unlock_two (src->i_mapping, dst->i_mapping); +if (src != dst)f2fs_up_write (&F2FS_I (dst)->i_gc_rwsem[WRITE ]); out_src:
@@ -3603,9 +3649,6 @@ static int f2fs_ioc_move_range(struct file *filp, unsigned long arg) {struct f2fs_move_range range; -if (f2fs_has_subpage_blocks (F2FS_I_SB (file_inode (filp)))) -return -EOPNOTSUPP ; -if (copy_from_user (&range, (struct f2fs_move_range __user *)arg,sizeof (range)))return -EFAULT ;
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 11/20] f2fs: support subpage blocks in move range
Quoted 12 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
Folios at both ends of the moved ranges can straddle the range boundaries. Write back and truncate the page cache from
PAGE_SIZEboundaries, and holdinvalidate_lockof both inodes during the exchange so that faults and readahead cannot bring back stale data. After the exchange, also after one that failed part way, drop the source blocks that it read into the page cache, and callpagecache_isize_extended()for the gap between the old EOF of dst and the moved data. The page cache was not truncated before, so this also applies when the block size equalsPAGE_SIZE. Remove the-EOPNOTSUPPcheck.
Quoted 7 lines
Zero the cached data past the old EOF of dst with
f2fs_zero_post_eof_page()before the move extends dst over it, as the other paths that extend a file do. Otherwise data stored past EOF through a mapping becomes file data. This also applies when the block size equalsPAGE_SIZE.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 12/20 f2fs: support subpage blocks in defragmentation
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
Mark only the target block, not its whole page, dirty when redirtying data for defragmentation. Remove the -EOPNOTSUPP check.
Read a multi-block folio once through ->read_folio() instead of each block synchronously, and redirty the folio that was read after locking it. This also applies to large folios when the block size equals PAGE_SIZE.
Read compressed files through ->read_folio() as well, with any block size. f2fs_get_lock_data_folio() reads the mapped block as it is, so a block of a compressed cluster that was not in the page cache was read without decompression, and defragmentation wrote the compressed data back as file data.
--- fs/f2fs/file.c | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-)
fs/f2fs/file.c +38 -6
@@ -3202,6 +3202,41 @@ static int f2fs_ioc_write_checkpoint(struct file *filp)return ret; } +/* +* Return the locked folio of a block to redirty it for defragmentation. +* +* A compressed file is read through ->read_folio(), which decompresses the +* cluster: f2fs_get_lock_data_folio() would read the mapped block as it is +* and leave compressed data in the page cache. A multi-block folio is read +* the same way, in one I/O instead of one synchronous read per block. +*/ +static struct folio *f2fs_get_defrag_folio (struct file *filp,pgoff_t bidx) +{ +struct inode *inode =file_inode (filp); +struct address_space *mapping = inode->i_mapping; +pgoff_t index =f2fs_lblk_to_folio_index (F2FS_I_SB (inode), bidx); +struct folio *folio; + +if (!f2fs_compressed_file (inode) && !f2fs_has_multiblock_folios (inode)) +return f2fs_get_lock_data_folio (inode, bidx, true); + +repeat: + folio =read_mapping_folio (mapping, index, filp); +if (IS_ERR (folio)) +return folio; + +folio_lock (folio); +if (unlikely (folio->mapping != mapping)) { +f2fs_folio_put (folio, true); + goto repeat; + } +if (unlikely (!folio_test_uptodate (folio))) { +f2fs_folio_put (folio, true); +return ERR_PTR (-EIO ); + } +return folio; +} +static int f2fs_defragment_range (struct f2fs_sb_info *sbi,struct file *filp,struct f2fs_defragment *range)
@@ -3327,7 +3362,7 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi, cnt < BLKS_PER_SEG(sbi)) {struct folio *folio; - folio =f2fs_get_lock_data_folio (inode, idx, true); + folio =f2fs_get_defrag_folio (filp, idx);if (IS_ERR (folio)) { err =PTR_ERR (folio); goto clear_out;
@@ -3336,8 +3371,8 @@ static int f2fs_defragment_range(struct f2fs_sb_info *sbi,f2fs_folio_wait_writeback (folio, true, true);f2fs_ffs_mark_subrange_dirty (folio, -offset_in_folio (folio, (loff_t )idx <<PAGE_SHIFT ), -PAGE_SIZE ); +f2fs_folio_byte_offset (sbi, folio, idx), +F2FS_BLKSIZE (sbi));f2fs_folio_mark_dirty (folio);folio_set_f2fs_gcing (folio);f2fs_folio_put (folio, true);
@@ -3378,9 +3413,6 @@ static int f2fs_ioc_defragment(struct file *filp, unsigned long arg)struct f2fs_defragment range;int err; -if (f2fs_has_subpage_blocks (sbi)) -return -EOPNOTSUPP ; -if (!capable (CAP_SYS_ADMIN ))return -EPERM ;
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 12/20] f2fs: support subpage blocks in defragmentation
Quoted 10 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
Mark only the target block, not its whole page, dirty when redirtying data for defragmentation. Remove the
-EOPNOTSUPPcheck.Read a multi-block folio once through ->read_folio() instead of each block synchronously, and redirty the folio that was read after locking it. This also applies to large folios when the block size equals
PAGE_SIZE.
Quoted 7 lines
Read compressed files through ->read_folio() as well, with any block size.
f2fs_get_lock_data_folio()reads the mapped block as it is, so a block of a compressed cluster that was not in the page cache was read without decompression, and defragmentation wrote the compressed data back as file data.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 13/20 f2fs: support subpage blocks in swapfile activation
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
The swap subsystem works in PAGE_SIZE units. Ignore a partial tail page, require PAGE_SIZE aligned extents, and convert block counts and addresses to pages for swap. When migrating blocks, mark only the migrated block, not its whole page, dirty in the folio state.
--- fs/f2fs/data.c | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-)
fs/f2fs/data.c +28 -15
@@ -6007,8 +6007,8 @@ static int f2fs_migrate_blocks(struct inode *inode, block_t start_blk, }f2fs_ffs_mark_subrange_dirty (folio, -offset_in_folio (folio, (loff_t )blkidx <<PAGE_SHIFT ), -PAGE_SIZE ); +f2fs_folio_byte_offset (sbi, folio, blkidx), +F2FS_BLKSIZE (sbi));f2fs_folio_mark_dirty (folio);f2fs_folio_put (folio, true); }
@@ -6040,8 +6040,10 @@ static int check_swap_activate(struct swap_info_struct *sis,struct address_space *mapping = swap_file->f_mapping;struct inode *inode = mapping->host;struct f2fs_sb_info *sbi =F2FS_I_SB (inode); +unsigned int blks_per_page =F2FS_BLKS_PER_PAGE (sbi);block_t cur_lblock;block_t last_lblock; +block_t max_lblock;block_t pblock;block_t lowest_pblock =-1 ;block_t highest_pblock =0 ;
@@ -6056,9 +6058,12 @@ static int check_swap_activate(struct swap_info_struct *sis,* to be very smart. */ cur_lblock =0 ; - last_lblock =F2FS_BYTES_TO_BLK (sbi,i_size_read (inode)); +/* swap uses whole pages only; ignore a partial tail page */ + last_lblock =round_down (F2FS_BYTES_TO_BLK (sbi,i_size_read (inode)), + blks_per_page); + max_lblock =min_t (u64, last_lblock, (u64 )sis->max * blks_per_page); -while (cur_lblock < last_lblock && cur_lblock < sis->max) { +while (cur_lblock < max_lblock) {struct f2fs_map_blocks map;bool last_extent = false; retry:
@@ -6093,7 +6098,7 @@ static int check_swap_activate(struct swap_info_struct *sis, not_aligned++; nr_pblocks =roundup (nr_pblocks, blks_per_sec); -if (cur_lblock + nr_pblocks > sis->max) +if (cur_lblock + nr_pblocks > max_lblock) nr_pblocks -= blks_per_sec;/* this extent is last one */
@@ -6114,20 +6119,28 @@ static int check_swap_activate(struct swap_info_struct *sis, goto retry; } -if (cur_lblock + nr_pblocks >= sis->max) - nr_pblocks = sis->max - cur_lblock; +if (cur_lblock + nr_pblocks >= max_lblock) + nr_pblocks = max_lblock - cur_lblock; + +if (pblock % blks_per_page || nr_pblocks % blks_per_page) { +f2fs_err (sbi,"Swapfile extent is not aligned to PAGE_SIZE" ); + ret = -EINVAL ; + goto out; + }if (cur_lblock) {/* exclude the header page */ -if (pblock < lowest_pblock) - lowest_pblock = pblock; -if (pblock + nr_pblocks -1 > highest_pblock) - highest_pblock = pblock + nr_pblocks -1 ; +if (pblock / blks_per_page < lowest_pblock) + lowest_pblock = pblock / blks_per_page; +if ((pblock + nr_pblocks) / blks_per_page -1 > highest_pblock) + highest_pblock = (pblock + nr_pblocks) / blks_per_page -1 ; }/* * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks */ - ret =add_swap_extent (sis, cur_lblock, nr_pblocks, pblock); + ret =add_swap_extent (sis, cur_lblock / blks_per_page, + nr_pblocks / blks_per_page, + pblock / blks_per_page);if (ret <0 ) goto out; nr_extents += ret;
@@ -6136,9 +6149,9 @@ static int check_swap_activate(struct swap_info_struct *sis, ret = nr_extents; *span =1 + highest_pblock - lowest_pblock;if (cur_lblock ==0 ) - cur_lblock =1 ;/* force Empty message */ - sis->max = cur_lblock; - sis->pages = cur_lblock -1 ; + cur_lblock = blks_per_page;/* force Empty message */ + sis->max = cur_lblock / blks_per_page; + sis->pages = sis->max -1 ; out:if (not_aligned) f2fs_warn(sbi, "Swapfile (%u) is not align to section: 1) creat(), 2) ioctl(F2FS_IOC_SET_PIN_FILE), 3) fallocate(%lu * N)",
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 13/20] f2fs: support subpage blocks in swapfile activation
Quoted 8 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
The swap subsystem works in
PAGE_SIZEunits. Ignore a partial tail page, requirePAGE_SIZEaligned extents, and convert block counts and addresses to pages for swap. When migrating blocks, mark only the migrated block, not its whole page, dirty in the folio state.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 14/20 f2fs: support subpage blocks in fs-verity enablement
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
Check verity per block. While enabling verity, dirty and write back only the data blocks below i_size in the folios before the verity metadata. Do not zero the blocks past i_size on read while enabling verity, since GC can read a folio of the verity metadata that is already written. Remove the -EOPNOTSUPP checks. The changes for enabling verity also apply to large folios when the block size equals PAGE_SIZE.
Stop the data reads of a verity file at i_size in f2fs_read_data_large_folio(), so that the blocks past EOF in the folio that spans EOF are zeroed instead of read. A pinned file can have such blocks allocated without being zeroed, and fs-verity rejects non-zero data past EOF. The reads of the verity metadata still go past i_size.
--- fs/f2fs/data.c | 29 +++++++++++++++++++++-------- fs/f2fs/f2fs.h | 5 +++++ fs/f2fs/file.c | 3 --- fs/f2fs/verity.c | 8 -------- 4 files changed, 26 insertions(+), 19 deletions(-)
fs/f2fs/data.c +21 -8
@@ -1671,7 +1671,7 @@ int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index)static inline struct fsverity_info *f2fs_need_verity (const struct inode *inode,pgoff_t idx) { -if (idx <DIV_ROUND_UP (inode->i_size,PAGE_SIZE )) +if (idx <F2FS_BLK_ALIGN (F2FS_SB (inode->i_sb), inode->i_size))return fsverity_get_info (inode);return NULL ; }
@@ -2619,7 +2619,12 @@ int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,static inline loff_t f2fs_readpage_limit (struct inode *inode) { -if (IS_ENABLED (CONFIG_FS_VERITY ) &&IS_VERITY (inode)) +/* +* The verity metadata is past i_size. GC can read it while verity is +* being enabled, so do not zero it. +*/ +if (IS_ENABLED (CONFIG_FS_VERITY ) && + (IS_VERITY (inode) ||f2fs_verity_in_progress (inode)))return F2FS_BLK_TO_BYTES (F2FS_I_SB (inode),max_file_blocks (F2FS_I_SB (inode), inode));
@@ -2913,10 +2918,10 @@ static unsigned int f2fs_folio_valid_blocks(struct inode *inode,pgoff_t last_lblk =F2FS_BLK_ALIGN (sbi,i_size_read (inode));pgoff_t first_lblk =f2fs_folio_lblk (sbi, folio); -if (f2fs_verity_in_progress (inode)) -return nr_blocks;if (first_lblk < last_lblk)return min_t (pgoff_t, nr_blocks, last_lblk - first_lblk); +if (f2fs_verity_in_progress (inode)) +return nr_blocks;return 0 ; }
@@ -3409,7 +3414,14 @@ static int f2fs_read_data_large_folio(struct inode *inode, index =f2fs_folio_lblk (sbi, folio); offset =0 ; nr_blocks =f2fs_blocks_per_folio (sbi, folio); - last_block_in_file =F2FS_BLK_ALIGN (sbi,f2fs_readpage_limit (inode)); +/* +* A data read of a verity file, which has vi, stops at i_size. A +* pinned file can have blocks past EOF that were allocated without +* being zeroed, and fs-verity rejects non-zero data past EOF. Only +* the reads of the verity metadata, which have no vi, go past i_size. +*/ + last_block_in_file =F2FS_BLK_ALIGN (sbi, vi ?i_size_read (inode) : +f2fs_readpage_limit (inode)); for (; nr_blocks; nr_blocks -= len_blks, max_nr_blocks -= len_blks,
@@ -3719,7 +3731,7 @@ static int f2fs_read_data_folio(struct file *file, struct folio *folio)return ret; } - vi =f2fs_need_verity (inode, folio->index); + vi =f2fs_need_verity (inode,f2fs_folio_lblk (F2FS_I_SB (inode), folio));if (vi)fsverity_readahead (vi, folio->index,folio_nr_pages (folio));return f2fs_mpage_readpages (inode, vi,NULL , folio);
@@ -3739,7 +3751,8 @@ static void f2fs_readahead(struct readahead_control *rac)if (f2fs_has_inline_data (inode))return ; - vi =f2fs_need_verity (inode,readahead_index (rac)); + vi =f2fs_need_verity (inode,F2FS_BYTES_TO_BLK (F2FS_I_SB (inode), +readahead_pos (rac)));if (vi)fsverity_readahead (vi,readahead_index (rac),readahead_count (rac));
@@ -4777,7 +4790,7 @@ static int f2fs_write_cache_folios(struct address_space *mapping, op_locked = true; } -if (!verity_in_progress) { +if (!verity_in_progress || end_pos <=f2fs_verity_metadata_pos (inode)) { poff =offset_in_folio (folio, isize); end_index = isize >>PAGE_SHIFT ;
fs/f2fs/f2fs.h +5 -0
@@ -5349,6 +5349,11 @@ static inline bool f2fs_allow_multi_device_dio(struct f2fs_sb_info *sbi,return sbi->aligned_blksize; } +static inline loff_t f2fs_verity_metadata_pos (const struct inode *inode) +{ +return round_up (i_size_read (inode),65536 ); +} +#ifdef CONFIG_F2FS_FAULT_INJECTION extern int f2fs_build_fault_attr (struct f2fs_sb_info *sbi,unsigned long rate,unsigned long type,enum fault_option fo);
fs/f2fs/file.c +0 -3
@@ -4331,9 +4331,6 @@ static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg) {struct inode *inode =file_inode (filp); -if (f2fs_has_subpage_blocks (F2FS_I_SB (inode))) -return -EOPNOTSUPP ; -f2fs_update_time (F2FS_I_SB (inode),REQ_TIME );if (!f2fs_sb_has_verity (F2FS_I_SB (inode))) {
fs/f2fs/verity.c +0 -8
@@ -32,11 +32,6 @@#define F2FS_VERIFY_VER (1) -static inline loff_t f2fs_verity_metadata_pos (const struct inode *inode) -{ -return round_up (inode->i_size,65536 ); -} -/* * Write some verity metadata to the inode for FS_IOC_ENABLE_VERITY. * kernel_write() can't be used because the file descriptor is readonly.
@@ -94,9 +89,6 @@ static int f2fs_begin_enable_verity(struct file *filp)struct inode *inode =file_inode (filp);int err; -if (f2fs_has_subpage_blocks (F2FS_I_SB (inode))) -return -EOPNOTSUPP ; -if (f2fs_verity_in_progress (inode))return -EBUSY ;
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 14/20] f2fs: support subpage blocks in fs-verity enablement
Quoted 9 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
Check verity per block. While enabling verity, dirty and write back only the data blocks below
i_sizein the folios before the verity metadata. Do not zero the blocks pasti_sizeon read while enabling verity, since GC can read a folio of the verity metadata that is already written. Remove the-EOPNOTSUPPchecks. The changes for enabling verity also apply to large folios when the block size equalsPAGE_SIZE.
Quoted 7 lines
Stop the data reads of a verity file at
i_sizeinf2fs_read_data_large_folio(), so that the blocks past EOF in the folio that spans EOF are zeroed instead of read. A pinned file can have such blocks allocated without being zeroed, and fs-verity rejects non-zero data past EOF. The reads of the verity metadata still go pasti_size.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 15/20 f2fs: support subpage blocks in atomic writes
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
Writeback already writes dirty blocks to the COW inode per block. Make write_begin of atomic files reserve and read COW blocks per block as well, and remove the -EOPNOTSUPP checks. Skipping the read of the blocks that a write fully covers also applies to large folios when the block size equals PAGE_SIZE.
Aborting atomic writes drops the page cache and restores i_size without i_gc_rwsem[WRITE] and invalidate_lock. With subpage blocks, a page fault in between can make a folio writable with only the blocks within the smaller i_size prepared, and a read can zero-fill the blocks beyond it and mark them uptodate, while they are inside the restored i_size. GC reads through the page cache as well once FI_ATOMIC_FILE is cleared, and then moves the zeroes. Hold both locks across it as f2fs_ioc_start_atomic_write() does, except at eviction, where no one else uses the inode.
--- fs/f2fs/data.c | 24 ++++++++++++++++-------- fs/f2fs/file.c | 30 ++++++++++++++++++++++-------- fs/f2fs/inode.c | 1 + fs/f2fs/segment.c | 7 +++++++ 4 files changed, 46 insertions(+), 16 deletions(-)
fs/f2fs/data.c +16 -8
@@ -4698,7 +4698,7 @@ static int f2fs_write_cache_folios(struct address_space *mapping, }if (f2fs_has_subpage_blocks (sbi) && - (f2fs_compressed_file (inode) ||f2fs_is_atomic_file (inode))) { +f2fs_compressed_file (inode)) {WARN_ON_ONCE (1 );mapping_set_error (mapping, -EOPNOTSUPP );folio_clear_dirty_for_io (folio);
@@ -5325,7 +5325,7 @@ static int prepare_large_folio_write_begin(struct inode *inode,int err =0 ;if (f2fs_has_subpage_blocks (sbi) && - (f2fs_compressed_file (inode) ||f2fs_is_atomic_file (inode))) +f2fs_compressed_file (inode))return -EOPNOTSUPP ;/*
@@ -5441,13 +5441,16 @@ static int prepare_large_folio_atomic_write_begin(struct inode *inode,/* Inline data must have been converted before reaching here. */ f2fs_bug_on (sbi,f2fs_has_inline_data (inode)); - start = folio->index + (ori_off >>PAGE_SHIFT ); - end = folio->index + ((ori_off + len -1 ) >>PAGE_SHIFT ); + start =f2fs_folio_lblk (sbi, folio) +F2FS_BYTES_TO_BLK (sbi, ori_off); + end =f2fs_folio_lblk (sbi, folio) + +F2FS_BYTES_TO_BLK (sbi, ori_off + len -1 );for (index = start; index <= end; index++) { +loff_t block_start =F2FS_BLK_TO_BYTES (sbi, index); +loff_t block_end = block_start +F2FS_BLKSIZE (sbi);block_t blkaddr =NULL_ADDR ;bool node_changed = false; -size_t off = (index - folio->index) <<PAGE_SHIFT ; +size_t off =f2fs_folio_byte_offset (sbi, folio, index); err = prepare_atomic_write_begin(sbi, inode, index, &blkaddr, &node_changed,
@@ -5455,12 +5458,17 @@ static int prepare_large_folio_atomic_write_begin(struct inode *inode,if (err)return err; +/* Skip read if the write covers the entire block. */ +if (pos <= block_start && pos + len >= block_end) + goto balance; +if (f2fs_ffs_test_blk_uptodate (folio, index)) goto balance;if (blkaddr ==NEW_ADDR ) { -folio_zero_segment (folio, off, off +PAGE_SIZE ); -f2fs_ffs_mark_subrange_uptodate (folio, off,PAGE_SIZE ); +folio_zero_segment (folio, off, off +F2FS_BLKSIZE (sbi)); +f2fs_ffs_mark_subrange_uptodate (folio, off, +F2FS_BLKSIZE (sbi)); goto balance; }
@@ -5517,7 +5525,7 @@ static int f2fs_write_begin(const struct kiocb *iocb, }if (f2fs_has_subpage_blocks (sbi) && - (f2fs_compressed_file (inode) ||f2fs_is_atomic_file (inode))) { +f2fs_compressed_file (inode)) { err = -EOPNOTSUPP ; goto fail; }
fs/f2fs/file.c +22 -8
@@ -2314,7 +2314,7 @@ static long f2fs_fallocate(struct file *file, int mode, }if (f2fs_has_subpage_blocks (F2FS_I_SB (inode))) { -if (f2fs_is_atomic_file (inode) ||f2fs_compressed_file (inode)) { +if (f2fs_compressed_file (inode)) { ret = -EOPNOTSUPP ; goto out; }
@@ -2358,6 +2358,23 @@ static long f2fs_fallocate(struct file *file, int mode,return ret; } +/* +* Abort atomic writes with inode_lock held. Until i_size is restored, GC, +* page faults and reads would see the i_size of the aborted writes, so keep +* them away as f2fs_ioc_start_atomic_write() does. +*/ +static void f2fs_abort_atomic_write_locked (struct inode *inode,bool clean) +{ +if (!f2fs_is_atomic_file (inode)) +return ; + +f2fs_down_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]); +filemap_invalidate_lock (inode->i_mapping); +f2fs_abort_atomic_write (inode, clean); +filemap_invalidate_unlock (inode->i_mapping); +f2fs_up_write (&F2FS_I (inode)->i_gc_rwsem[WRITE ]); +} +static int f2fs_release_file (struct inode *inode,struct file *filp) {if (atomic_dec_and_test (&F2FS_I (inode)->open_count))
@@ -2372,7 +2389,7 @@ static int f2fs_release_file(struct inode *inode, struct file *filp)return 0 ;inode_lock (inode); -f2fs_abort_atomic_write (inode, true); +f2fs_abort_atomic_write_locked (inode, true);inode_unlock (inode);return 0 ;
@@ -2391,7 +2408,7 @@ static int f2fs_file_flush(struct file *file, fl_owner_t id)if (F2FS_I (inode)->atomic_write_task == current && (current->flags &PF_EXITING )) {inode_lock (inode); -f2fs_abort_atomic_write (inode, true); +f2fs_abort_atomic_write_locked (inode, true);inode_unlock (inode); }
@@ -2568,9 +2585,6 @@ static int f2fs_ioc_start_atomic_write(struct file *filp, bool truncate)loff_t isize;int ret; -if (f2fs_has_subpage_blocks (sbi)) -return -EOPNOTSUPP ; -if (!(filp->f_mode &FMODE_WRITE ))return -EBADF ;
@@ -2702,7 +2716,7 @@ static int f2fs_ioc_commit_atomic_write(struct file *filp)if (!ret) ret =f2fs_do_sync_file (filp,0 ,LLONG_MAX ,0 , true); -f2fs_abort_atomic_write (inode, ret); +f2fs_abort_atomic_write_locked (inode, ret); }else { ret =f2fs_do_sync_file (filp,0 ,LLONG_MAX ,1 , false); }
@@ -2730,7 +2744,7 @@ static int f2fs_ioc_abort_atomic_write(struct file *filp)inode_lock (inode); -f2fs_abort_atomic_write (inode, true); +f2fs_abort_atomic_write_locked (inode, true);inode_unlock (inode);
fs/f2fs/inode.c +1 -0
@@ -851,6 +851,7 @@ static void f2fs_pre_evict_inode(struct inode *inode)struct f2fs_sb_info *sbi =F2FS_I_SB (inode);struct f2fs_inode_info *fi =F2FS_I (inode); +/* No one else uses the inode now, so the locks are not needed. */ f2fs_abort_atomic_write (inode, true);if (fi->cow_inode &&f2fs_is_cow_file (fi->cow_inode)) {
fs/f2fs/segment.c +7 -0
@@ -186,6 +186,13 @@ bool f2fs_need_SSR(struct f2fs_sb_info *sbi) SM_I(sbi)->min_ssr_sections + reserved_sections(sbi)); } +/* +* The callers hold i_gc_rwsem[WRITE] and invalidate_lock as +* f2fs_ioc_start_atomic_write() does, so that no page is read, by GC either, +* or made writable between dropping the page cache and restoring i_size, i.e. +* with the i_size of the aborted writes. Only the eviction does not, as no one +* else uses the inode then. +*/ void f2fs_abort_atomic_write (struct inode *inode,bool clean) {struct f2fs_inode_info *fi =F2FS_I (inode);
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 15/20] f2fs: support subpage blocks in atomic writes
Quoted 18 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
Writeback already writes dirty blocks to the COW inode per block. Make
write_beginof atomic files reserve and read COW blocks per block as well, and remove the-EOPNOTSUPPchecks. Skipping the read of the blocks that a write fully covers also applies to large folios when the block size equalsPAGE_SIZE.Aborting atomic writes drops the page cache and restores
i_sizewithout i_gc_rwsem[WRITE] andinvalidate_lock. With subpage blocks, a page fault in between can make a folio writable with only the blocks within the smalleri_sizeprepared, and a read can zero-fill the blocks beyond it and mark them uptodate, while they are inside the restoredi_size. GC reads through the page cache as well onceFI_ATOMIC_FILEis cleared, and then moves the zeroes. Hold both locks across it asf2fs_ioc_start_atomic_write()does, except at eviction, where no one else uses the inode.
Quoted 1 line
Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 16/20 f2fs: preserve inline data in subpage block mode
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
A write to an inline inode converted it to a regular inode whenever the folio held multiple blocks, allocating a block even for small files. Convert only when the write goes beyond MAX_INLINE_DATA; otherwise read the inline data into the folio and write it back to the inode block in f2fs_write_cache_folios(). This also applies to an inline inode that uses large folios when the block size equals PAGE_SIZE: its writeback now goes through f2fs_write_cache_folios() instead of f2fs_write_cache_pages().
--- fs/f2fs/data.c | 54 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 9 deletions(-)
fs/f2fs/data.c +45 -9
@@ -4804,6 +4804,16 @@ static int f2fs_write_cache_folios(struct address_space *mapping, } } +if (f2fs_has_inline_data (inode)) { + err =f2fs_write_inline_data (inode, folio); +if (!err) { +f2fs_folio_clear_dirty_blocks (folio, true); + goto retry_out; + } +folio_redirty_for_writepage (wbc, folio); + goto retry_out; + } +folio_start_writeback (folio);/*
@@ -4992,8 +5002,7 @@ static int __f2fs_write_data_pages(struct address_space *mapping,account_writeback (inode, true);blk_start_plug (&plug); -if (f2fs_has_multiblock_folios (inode) && - !f2fs_has_inline_data (inode)) +if (f2fs_has_multiblock_folios (inode)) ret =f2fs_write_cache_folios (mapping, wbc, io_type);else ret =f2fs_write_cache_pages (mapping, wbc, io_type);
@@ -5060,6 +5069,16 @@ void f2fs_write_failed(struct inode *inode, loff_t to) } } +/* Fill @folio from the inline data for a write within the inline area */ +static void prepare_inline_write_begin (struct inode *inode, +struct folio *folio,struct f2fs_cached_block *ientry) +{ +f2fs_do_read_inline_data (folio, ientry); +set_inode_flag (inode,FI_DATA_EXIST ); +if (inode->i_nlink) +f2fs_cache_set_inline (ientry); +} +static int prepare_write_begin (struct f2fs_sb_info *sbi,struct folio *folio,loff_t pos,unsigned int len,block_t *blk_addr,bool *node_changed)
@@ -5113,10 +5132,7 @@ static int prepare_write_begin(struct f2fs_sb_info *sbi,if (f2fs_has_inline_data (inode)) {if (pos + len <=MAX_INLINE_DATA (inode)) { -f2fs_do_read_inline_data (folio, ientry); -set_inode_flag (inode,FI_DATA_EXIST ); -if (inode->i_nlink) -f2fs_cache_set_inline (ientry); +prepare_inline_write_begin (inode, folio, ientry); goto out; } err =f2fs_convert_inline_folio (&dn, folio);
@@ -5535,7 +5551,8 @@ static int f2fs_write_begin(const struct kiocb *iocb,* and #0 page. The locking rule for inline_data conversion should be: * folio_lock(folio #0) -> folio_lock(inode_page) */ -if (index !=0 ||f2fs_has_subpage_blocks (sbi)) { +if (index !=0 || (f2fs_has_subpage_blocks (sbi) && + pos + len >MAX_INLINE_DATA (inode))) { err =f2fs_convert_inline_inode (inode);if (err) goto fail;
@@ -5592,6 +5609,23 @@ static int f2fs_write_begin(const struct kiocb *iocb, blen = min_t(unsigned int, len,folio_size (folio) -offset_in_folio (folio, pos)); +if (f2fs_has_inline_data (inode) &&f2fs_is_multiblock_folio (sbi, folio)) { +struct f2fs_lock_context lc; +struct f2fs_cached_block *ientry; + +f2fs_map_lock (sbi, &lc,F2FS_GET_BLOCK_PRE_AIO ); + ientry =f2fs_get_inode_cache (sbi, inode->i_ino); +if (IS_ERR (ientry)) { +f2fs_map_unlock (sbi, &lc,F2FS_GET_BLOCK_PRE_AIO ); + err =PTR_ERR (ientry); + goto put_folio; + } +prepare_inline_write_begin (inode, folio, ientry); +f2fs_put_cache (ientry, true); +f2fs_map_unlock (sbi, &lc,F2FS_GET_BLOCK_PRE_AIO ); +return 0 ; + } +/* * Flush the cached IO of this folio before touching it, especially * for the large folio prepare below which reads and updates subpage
@@ -5700,7 +5734,8 @@ static int f2fs_write_end(const struct kiocb *iocb,if (!folio_test_uptodate (folio)) {if (unlikely (copied != len)) { copied =0 ; - }else if (f2fs_is_multiblock_folio (F2FS_I_SB (inode), folio)) { + }else if (f2fs_is_multiblock_folio (F2FS_I_SB (inode), folio) && + !f2fs_has_inline_data (inode)) {f2fs_ffs_mark_subrange_uptodate (folio,offset_in_folio (folio, pos), len); }else {
@@ -5727,7 +5762,8 @@ static int f2fs_write_end(const struct kiocb *iocb,if (!copied) goto unlock_out; -if (f2fs_is_multiblock_folio (F2FS_I_SB (inode), folio)) +if (f2fs_is_multiblock_folio (F2FS_I_SB (inode), folio) && + !f2fs_has_inline_data (inode))f2fs_ffs_mark_subrange_dirty (folio,offset_in_folio (folio, pos), copied);f2fs_folio_mark_dirty (folio);
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 16/20] f2fs: preserve inline data in subpage block mode
Quoted 11 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
A write to an inline inode converted it to a regular inode whenever the folio held multiple blocks, allocating a block even for small files. Convert only when the write goes beyond
MAX_INLINE_DATA; otherwise read the inline data into the folio and write it back to the inode block inf2fs_write_cache_folios(). This also applies to an inline inode that uses large folios when the block size equalsPAGE_SIZE: its writeback now goes throughf2fs_write_cache_folios()instead off2fs_write_cache_pages().
Quoted 1 line
Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 17/20 f2fs: support subpage blocks in online resize
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
Resize works in blocks, segments and sections, independent of PAGE_SIZE, so remove the -EOPNOTSUPP check.
--- fs/f2fs/file.c | 3 --- 1 file changed, 3 deletions(-)
fs/f2fs/file.c +0 -3
@@ -4325,9 +4325,6 @@ static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)struct f2fs_sb_info *sbi =F2FS_I_SB (file_inode (filp));__u64 block_count; -if (f2fs_has_subpage_blocks (sbi)) -return -EOPNOTSUPP ; -if (!capable (CAP_SYS_ADMIN ))return -EPERM ;
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 17/20] f2fs: support subpage blocks in online resize
Quoted 6 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
Resize works in blocks, segments and sections, independent of
PAGE_SIZE, so remove the-EOPNOTSUPPcheck.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 18/20 f2fs: support subpage blocks in compression
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
With subpage blocks, a cluster spans fewer folios than blocks, and several compressed blocks share a bounce folio.
- Require a cluster to cover at least one page. This raises the minimum compress_log_size only when a page has more than four blocks, e.g. to 64KB for 4KB blocks on a 64KB page kernel, where the mount option and F2FS_IOC_SET_COMPRESS_OPTION reject a smaller one. A file that already has a smaller cluster is handled like one with an unsupported compression algorithm: it can be looked up and removed, but opening it fails with -EOPNOTSUPP.
- Use block offsets and sizes for compressed data, keep each compressed block at its block offset within the cluster, and count pending compressed I/O in blocks so that a shared bounce folio is not decompressed or freed early.
- Submit only the written block of a shared bounce folio in f2fs_submit_page_write(), at its offset in the folio.
- Keep compressed files on the page based read and write paths, and compress a cluster only when all of its blocks are dirty and below EOF.
- A folio with per-block state may hold uptodate blocks newer than the on-disk cluster, so decompress into a bounce page and copy back only the blocks that are not uptodate.
- Cache compressed blocks one block at a time in the compress cache.
- Lock only the unlocked pages in cancel_cluster_writeback() to avoid a self-deadlock.
- Attach a folio state to the multi-block folios of a rewritten cluster before they are dirtied, as the other write paths do.
- Attach a folio state to multi-block folios that have none when compressed writeback starts, as the raw write path does. Otherwise the write completion, which does not hold the folio lock, can detach the page private flags while a task is moving them into a new folio state.
- Take sbi from the dic to count the blocks of a compressed page in f2fs_finish_read_bio().
- Write the blocks of a multi-block folio in f2fs_write_single_data_page() as f2fs_write_cache_folios() does, taking the write_blocks_pending bias with a new helper shared by the two and dropping it with f2fs_ffs_put_write_pending().
- Count a read folio of a compressed file once in the iostat:
f2fs_mpage_readpages() counts the folios that go through its loop, and f2fs_read_data_large_folio() counts the others.
- Remove the -EOPNOTSUPP checks.
- Remove inode_inc_dirty_block() and inode_dec_dirty_block(), which have no caller left.
The bounce folios are now freed when the last block of a cluster completes, also when the block size equals PAGE_SIZE.
--- Documentation/filesystems/f2fs.rst | 7 +- fs/f2fs/compress.c | 567 ++++++++++++++++++++--------- fs/f2fs/data.c | 293 +++++++++------ fs/f2fs/f2fs.h | 71 ++-- fs/f2fs/file.c | 55 +-- fs/f2fs/super.c | 7 +- 6 files changed, 657 insertions(+), 343 deletions(-)
Documentation/filesystems/f2fs.rst +6 -1
@@ -319,7 +319,12 @@ compress_algorithm=%s:%d Control compress algorithm and its compress level, now, ========= =========== compress_log_size=%u Support configuring compress cluster size. The size will - be 4KB * (1 << %u). The default and minimum sizes are 16KB. + be block size * (1 << %u). The default and minimum sizes + are four blocks, i.e. 16KB with 4KB blocks. A cluster has + to cover at least one page, so they are one page if a page + has more than four blocks, e.g. 64KB with 4KB blocks on a + 64KB page kernel, where a file with a smaller cluster + cannot be opened (-EOPNOTSUPP). compress_extension=%s Support adding specified extension, so that f2fs can enable compression on those corresponding files, e.g. if all files with '.ext' has high compression rate, we can set the '.ext'
fs/f2fs/compress.c +403 -164
1,208 lines not on this page · raw message
fs/f2fs/data.c +186 -107
595 lines not on this page · raw message
fs/f2fs/f2fs.h +37 -34
167 lines not on this page · raw message
fs/f2fs/file.c +19 -36
143 lines not on this page · raw message
fs/f2fs/super.c +6 -1
@@ -1447,6 +1447,11 @@ static int f2fs_check_compression(struct fs_context *fc,ctx_clear_opt_mask (ctx,F2FS_MOUNT_COMPRESS_CACHE );return 0 ; } +if ((ctx->spec_mask & F2FS_SPEC_compress_log_size) && +F2FS_CTX_INFO (ctx).compress_log_size <min_compress_log_size (sbi)) { +f2fs_err (sbi,"Compress cluster log size is out of range" ); +return -EINVAL ; + }if (ctx->spec_mask & F2FS_SPEC_compress_extension) { cnt =F2FS_CTX_INFO (ctx).compress_ext_cnt;for (i =0 ; i <F2FS_CTX_INFO (ctx).compress_ext_cnt; i++) {
@@ -2630,7 +2635,7 @@ static void default_options(struct f2fs_sb_info *sbi, bool remount)F2FS_OPTION (sbi).s_resgid =make_kgid (&init_user_ns,F2FS_DEF_RESGID );if (f2fs_sb_has_compression (sbi)) {F2FS_OPTION (sbi).compress_algorithm =COMPRESS_LZ4 ; -F2FS_OPTION (sbi).compress_log_size =MIN_COMPRESS_LOG_SIZE ; +F2FS_OPTION (sbi).compress_log_size =min_compress_log_size (sbi);F2FS_OPTION (sbi).compress_ext_cnt =0 ;F2FS_OPTION (sbi).compress_mode =COMPR_MODE_FS ; }
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 18/20] f2fs: support subpage blocks in compression
Quoted 46 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
With subpage blocks, a cluster spans fewer folios than blocks, and several compressed blocks share a bounce folio.
- Require a cluster to cover at least one page. This raises the minimum
compress_log_sizeonly when a page has more than four blocks, e.g. to 64KB for 4KB blocks on a 64KB page kernel, where the mount option andF2FS_IOC_SET_COMPRESS_OPTIONreject a smaller one. A file that already has a smaller cluster is handled like one with an unsupported compression algorithm: it can be looked up and removed, but opening it fails with-EOPNOTSUPP.
- Use block offsets and sizes for compressed data, keep each compressed block at its block offset within the cluster, and count pending compressed I/O in blocks so that a shared bounce folio is not decompressed or freed early.
- Submit only the written block of a shared bounce folio inf2fs_submit_page_write(), at its offset in the folio.
- Keep compressed files on the page based read and write paths, and compress a cluster only when all of its blocks are dirty and below EOF.
- A folio with per-block state may hold uptodate blocks newer than the on-disk cluster, so decompress into a bounce page and copy back only the blocks that are not uptodate.
- Cache compressed blocks one block at a time in the compress cache.
- Lock only the unlocked pages incancel_cluster_writeback()to avoid a self-deadlock.
- Attach a folio state to the multi-block folios of a rewritten cluster before they are dirtied, as the other write paths do.
- Attach a folio state to multi-block folios that have none when compressed writeback starts, as the raw write path does. Otherwise the write completion, which does not hold the folio lock, can detach the page private flags while a task is moving them into a new folio state.
- Take sbi from the dic to count the blocks of a compressed page inf2fs_finish_read_bio().
- Write the blocks of a multi-block folio inf2fs_write_single_data_page()asf2fs_write_cache_folios()does, taking thewrite_blocks_pendingbias with a new helper shared by the two and dropping it withf2fs_ffs_put_write_pending().
- Count a read folio of a compressed file once in the iostat:
f2fs_mpage_readpages()counts the folios that go through its loop, andf2fs_read_data_large_folio()counts the others.
- Remove the-EOPNOTSUPPchecks.
- Removeinode_inc_dirty_block()andinode_dec_dirty_block(), which have no caller left.
Quoted 4 lines
The bounce folios are now freed when the last block of a cluster completes, also when the block size equals
PAGE_SIZE.Signed-off-by Daeho Jeong <[email protected]>
One minor comment below:
Quoted 24 lines
fs/f2fs/data.c +20
-0@@ -4277,6 +4354,46 @@ int f2fs_write_single_data_page(struct folio *folio, int *submitted,folio_zero_segment (folio, offset,folio_size (folio)); write: +if (f2fs_is_multiblock_folio (sbi, folio)) { +struct f2fs_lock_context lc; +unsigned int to_blk =f2fs_blocks_per_folio (sbi, folio); +u64 pos =folio_pos (folio); + +if (!compr_blocks && !f2fs_verity_in_progress (inode)) + to_blk = min_t(unsigned int, +F2FS_BLK_ALIGN (sbi, i_size - pos), + to_blk); + +/* Deadlock due to between page->lock and f2fs_lock_op */ +if (!compr_blocks && !f2fs_trylock_op (sbi, &lc)) { + err = -EAGAIN ; + goto redirty_out; + } + +folio_start_writeback (folio); +f2fs_folio_get_write_bias (folio,F2FS_BLK_TO_BYTES (sbi, to_blk)); + + err = f2fs_write_folio_dirty_range(folio, &local_submitted,
fs/f2fs/data.c int f2fs_write_single_data_page(struct folio *folio, int *submitted, · line 4386
Quoted 10 lines
fs/f2fs/data.c +10
-0+ wbc, io_type, pos, + pos +F2FS_BLK_TO_BYTES (sbi, to_blk), + compr_blocks); + +if (!compr_blocks) +f2fs_unlock_op (sbi, &lc); + +/* The callers drop the submitted count on error. */ +if (err && local_submitted) +f2fs_submit_merged_write_cond (sbi,NULL , folio);
We'd better clean up this if this is not a bug fix.
I think we can pass local_submitted to @submitted_p in f2fs_write_raw_pages() before its error handling, so that its caller (f2fs_write_cache_pages()) can flush the bio cache.
Thanks,
PATCH v2 19/20 f2fs: count compress cache entries in pages for its memory limit
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
The compress cache holds one filesystem block per entry, but f2fs_available_free_memory() compares the number of entries with a percentage of free memory counted in pages. With subpage blocks, this limits the compress cache to a fraction (block size / PAGE_SIZE) of the memory that compress_percent allows. Convert the entry count to pages before the comparison.
No functional change when the block size equals PAGE_SIZE.
--- fs/f2fs/node.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
fs/f2fs/node.c +4 -2
@@ -116,10 +116,12 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)/* * free memory is lower than watermark or cached page count -* exceed threshold, deny caching compress page. +* exceed threshold, deny caching compress page. A cache +* entry holds one block, so convert the entry count to pages. */ res = (free_ram > avail_ram * sbi->compress_watermark /100 ) && - (COMPRESS_CACHE (sbi)->num_entries < + ((COMPRESS_CACHE (sbi)->num_entries >> +F2FS_LOG_BLKS_PER_PAGE (sbi)) < free_ram * sbi->compress_percent /100 );#else res = false;
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 19/20] f2fs: count compress cache entries in pages for its memory limit
Quoted 9 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
The compress cache holds one filesystem block per entry, but
f2fs_available_free_memory()compares the number of entries with a percentage of free memory counted in pages. With subpage blocks, this limits the compress cache to a fraction (block size /PAGE_SIZE) of the memory thatcompress_percentallows. Convert the entry count to pages before the comparison.
Quoted 3 lines
No functional change when the block size equals
PAGE_SIZE.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
PATCH v2 20/20 f2fs: enable subpage block size support
In reply to Daeho JeongFrom: Daeho Jeong <[email protected]>
All paths now handle filesystem blocks smaller than PAGE_SIZE, so stop rejecting a superblock whose log_blocksize is below PAGE_SHIFT.
--- fs/f2fs/super.c | 7 ------- 1 file changed, 7 deletions(-)
fs/f2fs/super.c +0 -7
@@ -4173,13 +4173,6 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,return -EFSCORRUPTED ; } -/* Subpage block size is enabled after all paths support it. */ -if (le32_to_cpu (raw_super->log_blocksize) !=PAGE_SHIFT ) { -f2fs_info (sbi,"Subpage block size (%u) is not supported yet" , -le32_to_cpu (raw_super->log_blocksize)); -return -EOPNOTSUPP ; - } -/* check log blocks per segment */ if (le32_to_cpu (raw_super->log_blocks_per_seg) !=9 ) { f2fs_info(sbi, "Invalid log blocks per segment (%u)",
Signature · 1 line
2.56.0.611.g1030f0b940-goog
Re: [f2fs-dev] [PATCH v2 20/20] f2fs: enable subpage block size support
Quoted 6 lines · Daeho Jeong
From: Daeho Jeong <[email protected]>
All paths now handle filesystem blocks smaller than
PAGE_SIZE, so stop rejecting a superblock whoselog_blocksizeis belowPAGE_SHIFT.Signed-off-by Daeho Jeong <[email protected]>
Thanks,
This series was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:
Hello:
This series was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:
Quoted 8 lines · On Sun, 15 Nov 2026 20:08:31 -0800 you wrote:
From: Daeho Jeong <[email protected]>
F2FS has required the filesystem block size to equal
PAGE_SIZE. On a kernel with 16KB pages, this means an existing F2FS image with 4KB blocks cannot be mounted, e.g. the userdata partition of an Android device that moves from a 4KB page kernel to a 16KB page kernel.[...]
Here is the summary with links:
- [f2fs-dev,v2,01/20] f2fs: use block-based index and size calculations in more paths
https://git.kernel.org/jaegeuk/f2fs/c/7985ae4403bd - [f2fs-dev,v2,02/20] f2fs: support subpage block size superblock and mount https://git.kernel.org/jaegeuk/f2fs/c/4677715aecb4 - [f2fs-dev,v2,03/20] f2fs: extend f2fs_folio_state for subpage blocks https://git.kernel.org/jaegeuk/f2fs/c/6dad9aa2be5b - [f2fs-dev,v2,04/20] f2fs: support subpage blocks in directory operations https://git.kernel.org/jaegeuk/f2fs/c/5cf7185bf5d4 - [f2fs-dev,v2,05/20] f2fs: convert inode dirty page counters to dirty block counters https://git.kernel.org/jaegeuk/f2fs/c/85d94a798481 - [f2fs-dev,v2,06/20] f2fs: support subpage blocks in data read path and GC https://git.kernel.org/jaegeuk/f2fs/c/1f3fb065c53b - [f2fs-dev,v2,07/20] f2fs: support subpage blocks in data write path and writeback https://git.kernel.org/jaegeuk/f2fs/c/075eb224620a - [f2fs-dev,v2,08/20] f2fs: adapt range operations and mmap for subpage blocks https://git.kernel.org/jaegeuk/f2fs/c/b59ad45de360 - [f2fs-dev,v2,09/20] f2fs: support subpage blocks in pinned files https://git.kernel.org/jaegeuk/f2fs/c/1fca7e80ab81 - [f2fs-dev,v2,10/20] f2fs: support subpage blocks in collapse and insert range https://git.kernel.org/jaegeuk/f2fs/c/804fc9fc11a5 - [f2fs-dev,v2,11/20] f2fs: support subpage blocks in move range https://git.kernel.org/jaegeuk/f2fs/c/b012b16ed8ad - [f2fs-dev,v2,12/20] f2fs: support subpage blocks in defragmentation https://git.kernel.org/jaegeuk/f2fs/c/a200065e07d6 - [f2fs-dev,v2,13/20] f2fs: support subpage blocks in swapfile activation https://git.kernel.org/jaegeuk/f2fs/c/0ef07ef01bb5 - [f2fs-dev,v2,14/20] f2fs: support subpage blocks in fs-verity enablement https://git.kernel.org/jaegeuk/f2fs/c/780f86c0f2ed - [f2fs-dev,v2,15/20] f2fs: support subpage blocks in atomic writes https://git.kernel.org/jaegeuk/f2fs/c/d848dac0ab70 - [f2fs-dev,v2,16/20] f2fs: preserve inline data in subpage block mode https://git.kernel.org/jaegeuk/f2fs/c/50af3450d3b6 - [f2fs-dev,v2,17/20] f2fs: support subpage blocks in online resize https://git.kernel.org/jaegeuk/f2fs/c/fa57651dbe6f - [f2fs-dev,v2,18/20] f2fs: support subpage blocks in compression https://git.kernel.org/jaegeuk/f2fs/c/b5c77585f32c - [f2fs-dev,v2,19/20] f2fs: count compress cache entries in pages for its memory limit https://git.kernel.org/jaegeuk/f2fs/c/c86146d870b3 - [f2fs-dev,v2,20/20] f2fs: enable subpage block size support https://git.kernel.org/jaegeuk/f2fs/c/5d4c8a45517f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html