Skip to contentSkip to conversation

f2fs: fix extent merge failure when largest extent is not in rb-tree

PATCH RESEND Superseded Yongpeng Yang 3 participants 32 messages Jun 12 – Jun 25 F2FS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/5 f2fs: fix extent merge failure when largest extent is not in rb-tree

From: Yongpeng Yang <[email protected]>

The following scenario can cause fiemap to report incorrect extents:

$ mkfs.f2fs /dev/vdb -f
$ mount -o mode=lfs /dev/vdb /mnt/f2fs/
$ dd if=/dev/urandom of=data bs=4K count=874 conv=notrunc
$ f2fs_io fiemap 0 1000000 data 1
$ shrink all extent
$ dd if=/dev/urandom of=data bs=4K count=150 seek=874 conv=notrunc
$ f2fs_io fiemap 0 1000000 data 1
Fiemap: offset = 0 len = 1000000
        logical addr.    physical addr.   length           flags
0       0000000000000000 00000002868d4000 000000000036a000 00001000
1       000000000036a000 0000000286c3e000 0000000000096000 00001001

The root cause is that when the largest extent is not in the extent tree, mergeable extents are not merged, causing f2fs_map_blocks to misjudge and output an incorrect extent list.

Fix this by allowing the extent being inserted to merge with the largest extent. When updating the extent tree range, if the new extent can be front-merged or back-merged with the largest extent and the largest extent is not in the rb-tree, merge them before the normal lookup.

Fixes 429511cdf8b3 ("f2fs: add core functions for rb-tree extent cache")
---
 fs/f2fs/extent_cache.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
fs/f2fs/extent_cache.c +21 -0
index 61f6b9714366..aa368a01b035 100644
@@ -702,6 +702,27 @@ static void __update_extent_tree_range(struct inode *inode,
 		__drop_largest_extent(et, fofs, len);
 	}
 
+	if (et->largest.len != 0 &&
+			(__is_front_mergeable(tei, &et->largest, type) ||
+			 __is_back_mergeable(tei, &et->largest, type))) {
+		/* 0. try to merge with largest extent. */
+		en = __lookup_extent_node_ret(&et->root,
+				et->cached_en, et->largest.fofs,
+				&prev_en, &next_en,
+				&insert_p, &insert_parent,
+				&leftmost);
+		if (!en) {
+			if (__is_back_mergeable(tei, &et->largest, type)) {
+				tei->fofs = et->largest.fofs;
+				tei->blk = et->largest.blk;
+				fofs = tei->fofs;
+			}
+			tei->len += et->largest.len;
+			len = tei->len;
+			end = fofs + len;
+		}
+	}
+
 	/* 1. lookup first extent node in range [fofs, fofs + len - 1] */
 	en = __lookup_extent_node_ret(&et->root,
 					et->cached_en, fofs,
Signature · 8 lines
2.43.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

PATCH RESEND 2/5 f2fs: only initialize largest extent without extent_node at inode init

From: Yongpeng Yang <[email protected]>

The largest extent takes effect during both read mapping and write mapping lookups, while read mapping does not need to access the extent_node. For write mapping, the case where the largest extent is not in the extent tree can already be handled by the merge logic, and cases that cannot be merged do not require the largest extent to participate either.

Therefore, the largest extent does not need to initialize a corresponding extent_node, reducing memory footprint.

---
 fs/f2fs/extent_cache.c | 18 +-----------------
 1 file changed, 1 insertion(+), 17 deletions(-)
fs/f2fs/extent_cache.c +1 -17
index aa368a01b035..f8d94db60dc6 100644
@@ -410,10 +410,8 @@ static void __drop_largest_extent(struct extent_tree *et,
 void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
-	struct extent_tree_info *eti = &sbi->extent_tree[EX_READ];
 	struct f2fs_extent *i_ext = &F2FS_INODE(ifolio)->i_ext;
 	struct extent_tree *et;
-	struct extent_node *en;
 	struct extent_info ei = {0};
 
 	if (!__may_extent_tree(inode, EX_READ)) {
@@ -435,21 +433,7 @@ void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
 	if (atomic_read(&et->node_cnt) || !ei.len)
 		goto skip;
 
-	if (IS_DEVICE_ALIASING(inode)) {
-		et->largest = ei;
-		goto skip;
-	}
-
-	en = __attach_extent_node(sbi, et, &ei, NULL,
-				&et->root.rb_root.rb_node, true);
-	if (en) {
-		et->largest = en->ei;
-		et->cached_en = en;
-
-		spin_lock(&eti->extent_lock);
-		list_add_tail(&en->list, &eti->extent_list);
-		spin_unlock(&eti->extent_lock);
-	}
+	et->largest = ei;
 skip:
 	/* Let's drop, if checkpoint got corrupted. */
 	if (f2fs_cp_error(sbi)) {
Signature · 8 lines
2.43.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#2 lore raw

Re: [f2fs-dev] [PATCH RESEND 2/5] f2fs: only initialize largest extent without extent_node at inode init

Quoted 42 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

The largest extent takes effect during both read mapping and write mapping lookups, while read mapping does not need to access the extent_node. For write mapping, the case where the largest extent is not in the extent tree can already be handled by the merge logic, and cases that cannot be merged do not require the largest extent to participate either.

Therefore, the largest extent does not need to initialize a corresponding extent_node, reducing memory footprint.

---
 fs/f2fs/extent_cache.c | 18 +-----------------
 1 file changed, 1 insertion(+), 17 deletions(-)
fs/f2fs/extent_cache.c +0 -8
index aa368a01b035..f8d94db60dc6 100644
@@ -410,10 +410,8 @@ static void __drop_largest_extent(struct extent_tree *et,
 void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
-	struct extent_tree_info *eti = &sbi->extent_tree[EX_READ];
 	struct f2fs_extent *i_ext = &F2FS_INODE(ifolio)->i_ext;
 	struct extent_tree *et;
-	struct extent_node *en;
 	struct extent_info ei = {0};
 
 	if (!__may_extent_tree(inode, EX_READ)) {
@@ -435,21 +433,7 @@ void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
 	if (atomic_read(&et->node_cnt) || !ei.len)
 		goto skip;
 
-	if (IS_DEVICE_ALIASING(inode)) {
-		et->largest = ei;
-		goto skip;
-	}
-
-	en = __attach_extent_node(sbi, et, &ei, NULL,

fs/f2fs/extent_cache.c void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio) · line 436

Quoted 10 lines
fs/f2fs/extent_cache.c +1 -9
-				&et->root.rb_root.rb_node, true);
-	if (en) {
-		et->largest = en->ei;
-		et->cached_en = en;
-
-		spin_lock(&eti->extent_lock);
-		list_add_tail(&en->list, &eti->extent_list);
-		spin_unlock(&eti->extent_lock);
-	}
+	et->largest = ei;

Previously, we can split largest extent node to two if we punched it, now we can not? IIUC.

Thanks,

fs/f2fs/extent_cache.c void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio) · line 439

Quoted 3 lines
fs/f2fs/extent_cache.c +0 -0
 skip:
 	/* Let's drop, if checkpoint got corrupted. */
 	if (f2fs_cp_error(sbi)) {

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#3 lore raw

Re: [f2fs-dev] [PATCH RESEND 2/5] f2fs: only initialize largest extent without extent_node at inode init

Quoted 48 lines · On 6/15/26 7:55 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 47 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

The largest extent takes effect during both read mapping and write mapping lookups, while read mapping does not need to access the extent_node. For write mapping, the case where the largest extent is not in the extent tree can already be handled by the merge logic, and cases that cannot be merged do not require the largest extent to participate either.

Therefore, the largest extent does not need to initialize a corresponding extent_node, reducing memory footprint.

---
  fs/f2fs/extent_cache.c | 18 +-----------------
  1 file changed, 1 insertion(+), 17 deletions(-)
fs/f2fs/extent_cache.c +0 -13
index aa368a01b035..f8d94db60dc6 100644
@@ -410,10 +410,8 @@ static void __drop_largest_extent(struct extent_tree *et,
  void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
  {
  	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
-	struct extent_tree_info *eti = &sbi->extent_tree[EX_READ];
  	struct f2fs_extent *i_ext = &F2FS_INODE(ifolio)->i_ext;
  	struct extent_tree *et;
-	struct extent_node *en;
  	struct extent_info ei = {0};
 
  	if (!__may_extent_tree(inode, EX_READ)) {
@@ -435,21 +433,7 @@ void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
  	if (atomic_read(&et->node_cnt) || !ei.len)
  		goto skip;
 
-	if (IS_DEVICE_ALIASING(inode)) {
-		et->largest = ei;
-		goto skip;
-	}
-
-	en = __attach_extent_node(sbi, et, &ei, NULL,
-				&et->root.rb_root.rb_node, true);
-	if (en) {
-		et->largest = en->ei;
-		et->cached_en = en;
-
Quoted 8 lines

fs/f2fs/extent_cache.c void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio) · line 436

Quoted 5 lines
fs/f2fs/extent_cache.c +1 -4
-		spin_lock(&eti->extent_lock);
-		list_add_tail(&en->list, &eti->extent_list);
-		spin_unlock(&eti->extent_lock);
-	}
+	et->largest = ei;

Previously, we can split largest extent node to two if we punched it, now we can not? IIUC.

Prior to this change, the largest extent could also be shrunk, so the set of scenarios that need handling during punch remains identical before and after the modification.

For the largest extent, it only needs to guarantee that the mapping information it records stays consistent with the mappings tracked in the extent tree and multi-level indirect indices. The punch operation does not break this consistency. Moreover, the largest extent is not required to be the longest extent among all entries in the extent tree. It merely needs to match the mappings stored in multi-level indirect indices and be no smaller than the maximum extent present in the extent tree.

Thanks
Yongpeng,

Quoted 9 lines

Thanks,

fs/f2fs/extent_cache.c void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio) · line 439

Quoted 3 lines
fs/f2fs/extent_cache.c +0 -0
  skip:
  	/* Let's drop, if checkpoint got corrupted. */
  	if (f2fs_cp_error(sbi)) {
Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#4 lore raw

Re: [f2fs-dev] [PATCH RESEND 2/5] f2fs: only initialize largest extent without extent_node at inode init

Quoted 61 lines · Yongpeng Yang
Quoted 46 lines · On 6/15/26 7:55 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 45 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

The largest extent takes effect during both read mapping and write mapping lookups, while read mapping does not need to access the extent_node. For write mapping, the case where the largest extent is not in the extent tree can already be handled by the merge logic, and cases that cannot be merged do not require the largest extent to participate either.

Therefore, the largest extent does not need to initialize a corresponding extent_node, reducing memory footprint.

---
  fs/f2fs/extent_cache.c | 18 +-----------------

  1 file changed, 1 insertion(+), 17 deletions(-)
fs/f2fs/extent_cache.c +0 -13
index aa368a01b035..f8d94db60dc6 100644
@@ -410,10 +410,8 @@ static void __drop_largest_extent(struct extent_tree *et,
   void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
   {
       struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
-    struct extent_tree_info *eti = &sbi->extent_tree[EX_READ];
       struct f2fs_extent *i_ext = &F2FS_INODE(ifolio)->i_ext;
       struct extent_tree *et;
-    struct extent_node *en;
       struct extent_info ei = {0};
       if (!__may_extent_tree(inode, EX_READ)) {
@@ -435,21 +433,7 @@ void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
       if (atomic_read(&et->node_cnt) || !ei.len)
           goto skip;
-    if (IS_DEVICE_ALIASING(inode)) {
-        et->largest = ei;
-        goto skip;
-    }
-
-    en = __attach_extent_node(sbi, et, &ei, NULL,
-                &et->root.rb_root.rb_node, true);
-    if (en) {
-        et->largest = en->ei;
-        et->cached_en = en;
-
Quoted 8 lines

fs/f2fs/extent_cache.c void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio) · line 436

Quoted 5 lines
fs/f2fs/extent_cache.c +1 -4
-        spin_lock(&eti->extent_lock);
-        list_add_tail(&en->list, &eti->extent_list);
-        spin_unlock(&eti->extent_lock);
-    }
+    et->largest = ei;

Previously, we can split largest extent node to two if we punched it, now we can not? IIUC.

Prior to this change, the largest extent could also be shrunk, so the set of scenarios that need handling during punch remains identical before and after the modification.

Quoted 7 lines

For the largest extent, it only needs to guarantee that the mapping information it records stays consistent with the mappings tracked in the extent tree and multi-level indirect indices. The punch operation does not break this consistency. Moreover, the largest extent is not required to be the longest extent among all entries in the extent tree. It merely needs to match the mappings stored in multi-level indirect indices and be no smaller than the maximum extent present in the extent tree.

What I meant is: e.g. previously, if largest extent is 1024, punching in the middle of the extent will make it being split to two extents (512, 511) in __update_extent_tree_range().

If we do not add largest extent node into rb tree in f2fs_init_read_extent_tree(), then we may has no chance to keep small-sized(511) extent in above punch scenario, can you verify that?

Thanks,

Quoted 19 lines

Thanks
Yongpeng,

Quoted 13 lines

Thanks,

Quoted 3 lines

  skip:
      /* Let's drop, if checkpoint got corrupted. */ if (f2fs_cp_error(sbi)) {

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#5 lore raw

Re: [f2fs-dev] [PATCH RESEND 2/5] f2fs: only initialize largest extent without extent_node at inode init

Quoted 72 lines · On 6/20/26 11:31 AM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 63 lines · Yongpeng Yang
Quoted 48 lines · On 6/15/26 7:55 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 47 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

The largest extent takes effect during both read mapping and write mapping lookups, while read mapping does not need to access the extent_node. For write mapping, the case where the largest extent is not in the extent tree can already be handled by the merge logic, and cases that cannot be merged do not require the largest extent to participate either.

Therefore, the largest extent does not need to initialize a corresponding extent_node, reducing memory footprint.

---
  fs/f2fs/extent_cache.c | 18 +-----------------

  1 file changed, 1 insertion(+), 17 deletions(-)
fs/f2fs/extent_cache.c +0 -13
index aa368a01b035..f8d94db60dc6 100644
@@ -410,10 +410,8 @@ static void __drop_largest_extent(struct 
 extent_tree *et,
   void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
   {
       struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
-    struct extent_tree_info *eti = &sbi->extent_tree[EX_READ];
       struct f2fs_extent *i_ext = &F2FS_INODE(ifolio)->i_ext;
       struct extent_tree *et;
-    struct extent_node *en;
       struct extent_info ei = {0};
       if (!__may_extent_tree(inode, EX_READ)) {
@@ -435,21 +433,7 @@ void f2fs_init_read_extent_tree(struct inode 
 *inode, struct folio *ifolio)
       if (atomic_read(&et->node_cnt) || !ei.len)
           goto skip;
-    if (IS_DEVICE_ALIASING(inode)) {
-        et->largest = ei;
-        goto skip;
-    }
-
-    en = __attach_extent_node(sbi, et, &ei, NULL,
-                &et->root.rb_root.rb_node, true);
-    if (en) {
-        et->largest = en->ei;
-        et->cached_en = en;
-
Quoted 8 lines

fs/f2fs/extent_cache.c void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio) · line 436

Quoted 5 lines
fs/f2fs/extent_cache.c +1 -4
-        spin_lock(&eti->extent_lock);
-        list_add_tail(&en->list, &eti->extent_list);
-        spin_unlock(&eti->extent_lock);
-    }
+    et->largest = ei;

Previously, we can split largest extent node to two if we punched it, now we can not? IIUC.

Prior to this change, the largest extent could also be shrunk, so the set of scenarios that need handling during punch remains identical before and after the modification.

Quoted 7 lines

For the largest extent, it only needs to guarantee that the mapping information it records stays consistent with the mappings tracked in the extent tree and multi-level indirect indices. The punch operation does not break this consistency. Moreover, the largest extent is not required to be the longest extent among all entries in the extent tree. It merely needs to match the mappings stored in multi-level indirect indices and be no smaller than the maximum extent present in the extent tree.

Quoted 7 lines

What I meant is: e.g. previously, if largest extent is 1024, punching in the middle of the extent will make it being split to two extents (512, 511) in __update_extent_tree_range().

If we do not add largest extent node into rb tree in f2fs_init_read_extent_tree(), then we may has no chance to keep small-sized(511) extent in above punch scenario, can you verify that?

Yes, the 511-block mapping will be dropped. I considered this case earlier: when the newly inserted mapping overlaps with or adjacent with the largest extent, we reinsert the largest extent back into the extent tree. With this logic, the 511-block split extent will remain present in the extent tree. Instead of performing punch handling on the largest extent, we can simply drop it directly. However, this approach does not work well for updates targeting the tail of the original extent. For instance, punch 1024 into 1022 and 1 for overwrite case, which will also alloc one more extent_node.

Thanks
Yongpeng,

Quoted 25 lines

Thanks,

Quoted 19 lines

Thanks
Yongpeng,

Quoted 13 lines

Thanks,

Quoted 3 lines

  skip:
      /* Let's drop, if checkpoint got corrupted. */ if (f2fs_cp_error(sbi)) {

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#6 lore raw

Re: [f2fs-dev] [PATCH RESEND 2/5] f2fs: only initialize largest extent without extent_node at inode init

Quoted 75 lines · Yongpeng Yang
Quoted 74 lines · On 6/20/26 11:31 AM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 61 lines · Yongpeng Yang
Quoted 46 lines · On 6/15/26 7:55 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 45 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

The largest extent takes effect during both read mapping and write mapping lookups, while read mapping does not need to access the extent_node. For write mapping, the case where the largest extent is not in the extent tree can already be handled by the merge logic, and cases that cannot be merged do not require the largest extent to participate either.

Therefore, the largest extent does not need to initialize a corresponding extent_node, reducing memory footprint.

---
  fs/f2fs/extent_cache.c | 18 +-----------------

  1 file changed, 1 insertion(+), 17 deletions(-)
fs/f2fs/extent_cache.c +0 -13
index aa368a01b035..f8d94db60dc6 100644
@@ -410,10 +410,8 @@ static void __drop_largest_extent(struct extent_tree *et,
   void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
   {
       struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
-    struct extent_tree_info *eti = &sbi->extent_tree[EX_READ];
       struct f2fs_extent *i_ext = &F2FS_INODE(ifolio)->i_ext;
       struct extent_tree *et;
-    struct extent_node *en;
       struct extent_info ei = {0};
       if (!__may_extent_tree(inode, EX_READ)) {
@@ -435,21 +433,7 @@ void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
       if (atomic_read(&et->node_cnt) || !ei.len)
           goto skip;
-    if (IS_DEVICE_ALIASING(inode)) {
-        et->largest = ei;
-        goto skip;
-    }
-
-    en = __attach_extent_node(sbi, et, &ei, NULL,
-                &et->root.rb_root.rb_node, true);
-    if (en) {
-        et->largest = en->ei;
-        et->cached_en = en;
-
Quoted 8 lines

fs/f2fs/extent_cache.c void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio) · line 436

Quoted 5 lines
fs/f2fs/extent_cache.c +1 -4
-        spin_lock(&eti->extent_lock);
-        list_add_tail(&en->list, &eti->extent_list);
-        spin_unlock(&eti->extent_lock);
-    }
+    et->largest = ei;

Previously, we can split largest extent node to two if we punched it, now we can not? IIUC.

Prior to this change, the largest extent could also be shrunk, so the set of scenarios that need handling during punch remains identical before and after the modification.

Quoted 7 lines

For the largest extent, it only needs to guarantee that the mapping information it records stays consistent with the mappings tracked in the extent tree and multi-level indirect indices. The punch operation does not break this consistency. Moreover, the largest extent is not required to be the longest extent among all entries in the extent tree. It merely needs to match the mappings stored in multi-level indirect indices and be no smaller than the maximum extent present in the extent tree.

What I meant is: e.g. previously, if largest extent is 1024, punching in the middle of the extent will make it being split to two extents (512, 511) in __update_extent_tree_range().

Quoted 6 lines
Quoted 3 lines

If we do not add largest extent node into rb tree in f2fs_init_read_extent_tree(), then we may has no chance to keep small-sized(511) extent in above punch scenario, can you verify that?

Yes, the 511-block mapping will be dropped. I considered this case earlier: when the newly inserted mapping overlaps with or adjacent with the largest extent, we reinsert the largest extent back into the extent tree. With this logic, the 511-block split extent will remain present in the extent tree. Instead of performing punch handling on the largest extent, we can simply drop it directly. However, this approach does not work well for updates targeting the tail of the original extent. For instance, punch 1024 into 1022 and 1 for overwrite case, which will also alloc one more extent_node.

We will only add extent which has size >= F2FS_MIN_EXTENT_LEN, so 1022 will be added, and 1 will be dropped.

Thanks,

Quoted 34 lines

Thanks
Yongpeng,

Quoted 29 lines

Thanks,

Quoted 19 lines

Thanks
Yongpeng,

Quoted 13 lines

Thanks,

Quoted 3 lines

  skip:
      /* Let's drop, if checkpoint got corrupted. */ if (f2fs_cp_error(sbi)) {

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#7 lore raw

Re: [f2fs-dev] [PATCH RESEND 2/5] f2fs: only initialize largest extent without extent_node at inode init

Quoted 101 lines · On 6/22/26 08:42, Chao Yu via Linux-f2fs-devel wrote:
Quoted 90 lines · Yongpeng Yang
Quoted 83 lines · On 6/20/26 11:31 AM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 65 lines · Yongpeng Yang
Quoted 50 lines · On 6/15/26 7:55 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 49 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

The largest extent takes effect during both read mapping and write mapping lookups, while read mapping does not need to access the extent_node. For write mapping, the case where the largest extent is not in the extent tree can already be handled by the merge logic, and cases that cannot be merged do not require the largest extent to participate either.

Therefore, the largest extent does not need to initialize a corresponding extent_node, reducing memory footprint.

---
  fs/f2fs/extent_cache.c | 18 +-----------------

  1 file changed, 1 insertion(+), 17 deletions(-)
fs/f2fs/extent_cache.c +0 -0
index aa368a01b035..f8d94db60dc6 100644
@@ -410,10 +410,8 @@ static void __drop_largest_extent(struct
 extent_tree *et,

  void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
  {
      struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
-    struct extent_tree_info *eti = &sbi->extent_tree[EX_READ]; struct f2fs_extent *i_ext = &F2FS_INODE(ifolio)->i_ext; struct extent_tree *et;
-    struct extent_node *en;
      struct extent_info ei = {0};
      if (!__may_extent_tree(inode, EX_READ)) {

fs/f2fs/extent_cache.c +0 -12
@@ -435,21 +433,7 @@ void f2fs_init_read_extent_tree(struct inode
 *inode, struct folio *ifolio)
       if (atomic_read(&et->node_cnt) || !ei.len)
           goto skip;
-    if (IS_DEVICE_ALIASING(inode)) {
-        et->largest = ei;
-        goto skip;
-    }
-
-    en = __attach_extent_node(sbi, et, &ei, NULL,
-                &et->root.rb_root.rb_node, true);
-    if (en) {
-        et->largest = en->ei;
-        et->cached_en = en;
-
-        spin_lock(&eti->extent_lock);
Quoted 8 lines

fs/f2fs/extent_cache.c void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio) · line 436

Quoted 4 lines
fs/f2fs/extent_cache.c +1 -3
-        list_add_tail(&en->list, &eti->extent_list);
-        spin_unlock(&eti->extent_lock);
-    }
+    et->largest = ei;

Previously, we can split largest extent node to two if we punched it, now
we can not? IIUC.

Prior to this change, the largest extent could also be shrunk, so the set of scenarios that need handling during punch remains identical before and after the modification.

Quoted 10 lines

For the largest extent, it only needs to guarantee that the mapping information it records stays consistent with the mappings tracked in the
extent tree and multi-level indirect indices. The punch operation does not break this consistency. Moreover, the largest extent is not required
to be the longest extent among all entries in the extent tree. It merely
needs to match the mappings stored in multi-level indirect indices and be no smaller than the maximum extent present in the extent tree.

What I meant is: e.g. previously, if largest extent is 1024, punching in the
middle of the extent will make it being split to two extents (512,
511) in
__update_extent_tree_range().

Quoted 5 lines

If we do not add largest extent node into rb tree in f2fs_init_read_extent_tree(),
then we may has no chance to keep small-sized(511) extent in above punch scenario,
can you verify that?

Quoted 9 lines

Yes, the 511-block mapping will be dropped. I considered this case
earlier: when the newly inserted mapping overlaps with or adjacent with the largest extent, we reinsert the largest extent back into the extent tree. With this logic, the 511-block split extent will remain present in the extent tree. Instead of performing punch handling on the largest extent, we can simply drop it directly. However, this approach does not work well for updates targeting the tail of the original extent. For instance, punch 1024 into 1022 and 1 for overwrite case, which will also alloc one more extent_node.

Quoted 2 lines

We will only add extent which has size >= F2FS_MIN_EXTENT_LEN, so 1022 will be added, and 1 will be dropped.

Oh, yes, this example is not appropriate. Do you think this approach would be better, or is [PATCH 1/5] and [PATCH 3/5] preferable?

Thanks
Yongpeng,

Quoted 40 lines

Thanks,

Quoted 34 lines

Thanks
Yongpeng,

Quoted 29 lines

Thanks,

Quoted 19 lines

Thanks
Yongpeng,

Quoted 13 lines

Thanks,

Quoted 3 lines

  skip:
      /* Let's drop, if checkpoint got corrupted. */ if (f2fs_cp_error(sbi)) {

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#8 lore raw

Re: [f2fs-dev] [PATCH RESEND 2/5] f2fs: only initialize largest extent without extent_node at inode init

Quoted 103 lines · Yongpeng Yang
Quoted 101 lines · On 6/22/26 08:42, Chao Yu via Linux-f2fs-devel wrote:
Quoted 99 lines · Yongpeng Yang
Quoted 83 lines · On 6/20/26 11:31 AM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 65 lines · Yongpeng Yang
Quoted 50 lines · On 6/15/26 7:55 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 49 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

The largest extent takes effect during both read mapping and write mapping lookups, while read mapping does not need to access the extent_node. For write mapping, the case where the largest extent is not in the extent tree can already be handled by the merge logic, and cases that cannot be merged do not require the largest extent to participate either.

Therefore, the largest extent does not need to initialize a corresponding extent_node, reducing memory footprint.

---
  fs/f2fs/extent_cache.c | 18 +-----------------

  1 file changed, 1 insertion(+), 17 deletions(-)
fs/f2fs/extent_cache.c +0 -0
index aa368a01b035..f8d94db60dc6 100644
@@ -410,10 +410,8 @@ static void __drop_largest_extent(struct
 extent_tree *et,

  void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
  {
      struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
-    struct extent_tree_info *eti = &sbi->extent_tree[EX_READ]; struct f2fs_extent *i_ext = &F2FS_INODE(ifolio)->i_ext; struct extent_tree *et;
-    struct extent_node *en;
      struct extent_info ei = {0};
      if (!__may_extent_tree(inode, EX_READ)) {

fs/f2fs/extent_cache.c +0 -12
@@ -435,21 +433,7 @@ void f2fs_init_read_extent_tree(struct inode
 *inode, struct folio *ifolio)
       if (atomic_read(&et->node_cnt) || !ei.len)
           goto skip;
-    if (IS_DEVICE_ALIASING(inode)) {
-        et->largest = ei;
-        goto skip;
-    }
-
-    en = __attach_extent_node(sbi, et, &ei, NULL,
-                &et->root.rb_root.rb_node, true);
-    if (en) {
-        et->largest = en->ei;
-        et->cached_en = en;
-
-        spin_lock(&eti->extent_lock);
Quoted 8 lines

fs/f2fs/extent_cache.c void f2fs_init_read_extent_tree(struct inode · line 436

Quoted 4 lines
fs/f2fs/extent_cache.c +1 -3
-        list_add_tail(&en->list, &eti->extent_list);
-        spin_unlock(&eti->extent_lock);
-    }
+    et->largest = ei;

Previously, we can split largest extent node to two if we punched it, now
we can not? IIUC.

Prior to this change, the largest extent could also be shrunk, so the set of scenarios that need handling during punch remains identical before and after the modification.

Quoted 10 lines

For the largest extent, it only needs to guarantee that the mapping information it records stays consistent with the mappings tracked in the
extent tree and multi-level indirect indices. The punch operation does not break this consistency. Moreover, the largest extent is not required
to be the longest extent among all entries in the extent tree. It merely
needs to match the mappings stored in multi-level indirect indices and be no smaller than the maximum extent present in the extent tree.

What I meant is: e.g. previously, if largest extent is 1024, punching in the
middle of the extent will make it being split to two extents (512,
511) in
__update_extent_tree_range().

Quoted 5 lines

If we do not add largest extent node into rb tree in f2fs_init_read_extent_tree(),
then we may has no chance to keep small-sized(511) extent in above punch scenario,
can you verify that?

Yes, the 511-block mapping will be dropped. I considered this case
earlier: when the newly inserted mapping overlaps with or adjacent with the largest extent, we reinsert the largest extent back into the extent tree. With this logic, the 511-block split extent will remain present in the extent tree. Instead of performing punch handling on the largest extent, we can simply drop it directly. However, this approach does not work well for updates targeting the tail of the original extent. For instance, punch 1024 into 1022 and 1 for overwrite case, which will also alloc one more extent_node.

Quoted 4 lines
Quoted 2 lines

We will only add extent which has size >= F2FS_MIN_EXTENT_LEN, so 1022 will be added, and 1 will be dropped.

Oh, yes, this example is not appropriate. Do you think this approach

Since there will be potential side-effect w/ current patch, so I prefer to keep it as it is.

Thanks,

Quoted 50 lines

would be better, or is [PATCH 1/5] and [PATCH 3/5] preferable?

Thanks
Yongpeng,

Quoted 44 lines

Thanks,

Quoted 34 lines

Thanks
Yongpeng,

Quoted 29 lines

Thanks,

Quoted 19 lines

Thanks
Yongpeng,

Quoted 13 lines

Thanks,

Quoted 3 lines

  skip:
      /* Let's drop, if checkpoint got corrupted. */ if (f2fs_cp_error(sbi)) {

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#9 lore raw

Re: [f2fs-dev] [PATCH RESEND 2/5] f2fs: only initialize largest extent without extent_node at inode init

Quoted 104 lines · On 6/25/26 15:25, Chao Yu via Linux-f2fs-devel wrote:
Quoted 103 lines · Yongpeng Yang
Quoted 101 lines · On 6/22/26 08:42, Chao Yu via Linux-f2fs-devel wrote:
Quoted 99 lines · Yongpeng Yang
Quoted 83 lines · On 6/20/26 11:31 AM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 65 lines · Yongpeng Yang
Quoted 50 lines · On 6/15/26 7:55 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 49 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

The largest extent takes effect during both read mapping and write mapping lookups, while read mapping does not need to access the extent_node. For write mapping, the case where the largest extent is not in the extent tree can already be handled by the merge logic, and cases that cannot be merged do not require the largest extent to participate either.

Therefore, the largest extent does not need to initialize a corresponding extent_node, reducing memory footprint.

---
  fs/f2fs/extent_cache.c | 18 +-----------------

  1 file changed, 1 insertion(+), 17 deletions(-)
fs/f2fs/extent_cache.c +0 -0
index aa368a01b035..f8d94db60dc6 100644
@@ -410,10 +410,8 @@ static void __drop_largest_extent(struct
 extent_tree *et,

  void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
  {
      struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
-    struct extent_tree_info *eti = &sbi->extent_tree[EX_READ]; struct f2fs_extent *i_ext = &F2FS_INODE(ifolio)->i_ext; struct extent_tree *et;
-    struct extent_node *en;
      struct extent_info ei = {0};
      if (!__may_extent_tree(inode, EX_READ)) {

fs/f2fs/extent_cache.c +0 -12
@@ -435,21 +433,7 @@ void f2fs_init_read_extent_tree(struct inode
 *inode, struct folio *ifolio)
       if (atomic_read(&et->node_cnt) || !ei.len)
           goto skip;
-    if (IS_DEVICE_ALIASING(inode)) {
-        et->largest = ei;
-        goto skip;
-    }
-
-    en = __attach_extent_node(sbi, et, &ei, NULL,
-                &et->root.rb_root.rb_node, true);
-    if (en) {
-        et->largest = en->ei;
-        et->cached_en = en;
-
-        spin_lock(&eti->extent_lock);
Quoted 8 lines

fs/f2fs/extent_cache.c void f2fs_init_read_extent_tree(struct inode · line 436

Quoted 4 lines
fs/f2fs/extent_cache.c +1 -3
-        list_add_tail(&en->list, &eti->extent_list);
-        spin_unlock(&eti->extent_lock);
-    }
+    et->largest = ei;

Previously, we can split largest extent node to two if we punched it, now
we can not? IIUC.

Prior to this change, the largest extent could also be shrunk, so the set of scenarios that need handling during punch remains identical before and after the modification.

Quoted 10 lines

For the largest extent, it only needs to guarantee that the mapping information it records stays consistent with the mappings tracked in the
extent tree and multi-level indirect indices. The punch operation does not break this consistency. Moreover, the largest extent is not required
to be the longest extent among all entries in the extent tree. It merely
needs to match the mappings stored in multi-level indirect indices and be no smaller than the maximum extent present in the extent tree.

What I meant is: e.g. previously, if largest extent is 1024, punching in the
middle of the extent will make it being split to two extents (512,
511) in
__update_extent_tree_range().

Quoted 5 lines

If we do not add largest extent node into rb tree in f2fs_init_read_extent_tree(),
then we may has no chance to keep small-sized(511) extent in above punch scenario,
can you verify that?

Yes, the 511-block mapping will be dropped. I considered this case
earlier: when the newly inserted mapping overlaps with or adjacent with the largest extent, we reinsert the largest extent back into the extent tree. With this logic, the 511-block split extent will remain present in the extent tree. Instead of performing punch handling on the largest extent, we can simply drop it directly. However, this approach does not work well for updates targeting the tail of the original extent. For instance, punch 1024 into 1022 and 1 for overwrite case, which will also alloc one more extent_node.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

We will only add extent which has size >= F2FS_MIN_EXTENT_LEN, so 1022 will be added, and 1 will be dropped.

Oh, yes, this example is not appropriate. Do you think this approach

Since there will be potential side-effect w/ current patch, so I prefer to keep it as it is.

We have observed that the "Hit ratio" reported by "Extent Cache (Read)" is quite low. The main reason is that the total number of extent nodes in the read extent cache is limited, but increasing that limit may not be easy.

I'm wondering whether we can improve cache utilization instead. Adding an extent_node for the largest extent and inserting it into the read extent tree does not seem to help the hit ratio directly. On the other hand, when a file is reopened, its largest extent causes the corresponding extent_node to be added to the tail of sbi->extent_tree[EX_READ].extent_list. Under memory pressure, this may result in other non-largest extents being reclaimed first.

Would it make sense to place the largest extent_node at the head of sbi-> extent_tree[EX_READ].extent_list when restoring the largest extent in f2fs_init_read_extent_tree() instead? This seems like a simpler approach to improve the utilization of the existing read extent cache.

Thanks
Yongpeng,

Quoted 56 lines

Thanks,

Quoted 50 lines

would be better, or is [PATCH 1/5] and [PATCH 3/5] preferable?

Thanks
Yongpeng,

Quoted 44 lines

Thanks,

Quoted 34 lines

Thanks
Yongpeng,

Quoted 29 lines

Thanks,

Quoted 19 lines

Thanks
Yongpeng,

Quoted 13 lines

Thanks,

Quoted 3 lines

  skip:
      /* Let's drop, if checkpoint got corrupted. */ if (f2fs_cp_error(sbi)) {

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#10 lore raw

PATCH RESEND 3/5 f2fs: punch largest extent instead of dropping it entirely on overlap

In reply to Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Previously, when an extent being inserted overlaps with the largest extent, the largest extent is dropped entirely. This was done to handle the case where the largest extent is not in memory, avoiding inconsistency between the largest extent and the extent tree.

This patch changes the semantics of __drop_largest_extent (renamed to __punch_largest_extent): instead of discarding the entire largest extent when any overlap is detected, keep the larger remaining portion (left or right) after the punch. This preserves extent cache coverage for truncate and overwrite operations that only partially overlap the largest extent.

---
 fs/f2fs/extent_cache.c | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)
fs/f2fs/extent_cache.c +24 -7
index f8d94db60dc6..82d84c4e98b2 100644
@@ -397,14 +397,31 @@ static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
 	return count;
 }
 
-static void __drop_largest_extent(struct extent_tree *et,
+static void __punch_largest_extent(struct extent_tree *et,
 					pgoff_t fofs, unsigned int len)
 {
-	if (fofs < (pgoff_t)et->largest.fofs + et->largest.len &&
-			fofs + len > et->largest.fofs) {
-		et->largest.len = 0;
-		et->largest_updated = true;
+	unsigned int largest_end, punch_end;
+	unsigned int left_len, right_len;
+
+	if (fofs >= (pgoff_t)et->largest.fofs + et->largest.len ||
+			fofs + len <= et->largest.fofs)
+		return;
+
+	/* Punch [fofs, fofs + len) from largest extent. */
+	largest_end = et->largest.fofs + et->largest.len;
+	punch_end = fofs + len;
+
+	left_len = fofs > et->largest.fofs ? fofs - et->largest.fofs : 0;
+	right_len = largest_end > punch_end ? largest_end - punch_end : 0;
+
+	if (left_len >= right_len) {
+		et->largest.len = left_len;
+	} else {
+		et->largest.blk += punch_end - et->largest.fofs;
+		et->largest.fofs = punch_end;
+		et->largest.len = right_len;
 	}
+	et->largest_updated = true;
 }
 
 void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
@@ -680,10 +697,10 @@ static void __update_extent_tree_range(struct inode *inode,
 		dei.len = 0;
 
 		/*
-		 * drop largest extent before lookup, in case it's already
+		 * punch largest extent before lookup, in case it's already
 		 * been shrunk from extent tree
 		 */
-		__drop_largest_extent(et, fofs, len);
+		__punch_largest_extent(et, fofs, len);
 	}
 
 	if (et->largest.len != 0 &&
Signature · 8 lines
2.43.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#11 lore raw

Re: [f2fs-dev] [PATCH RESEND 3/5] f2fs: punch largest extent instead of dropping it entirely on overlap

Quoted 4 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Previously, when an extent being inserted overlaps with the largest extent, the largest extent is dropped entirely. This was done to handle

Please correct me if I missed anything, I remember that we will add largest extent in below path?

- __update_extent_tree_range
- __insert_extent_tree
- __try_update_largest_extent : update largest w/ right extent
- __try_update_largest_extent : update largest w/ left extent

Thanks,

Quoted 59 lines

the case where the largest extent is not in memory, avoiding inconsistency between the largest extent and the extent tree.

This patch changes the semantics of __drop_largest_extent (renamed to __punch_largest_extent): instead of discarding the entire largest extent when any overlap is detected, keep the larger remaining portion (left or right) after the punch. This preserves extent cache coverage for truncate and overwrite operations that only partially overlap the largest extent.

---
 fs/f2fs/extent_cache.c | 31 ++++++++++++++++++++++++-------
 1 file changed, 24 insertions(+), 7 deletions(-)
fs/f2fs/extent_cache.c +22 -5
index f8d94db60dc6..82d84c4e98b2 100644
@@ -397,14 +397,31 @@ static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
 	return count;
 }
 
-static void __drop_largest_extent(struct extent_tree *et,
+static void __punch_largest_extent(struct extent_tree *et,
 					pgoff_t fofs, unsigned int len)
 {
-	if (fofs < (pgoff_t)et->largest.fofs + et->largest.len &&
-			fofs + len > et->largest.fofs) {
-		et->largest.len = 0;
-		et->largest_updated = true;
+	unsigned int largest_end, punch_end;
+	unsigned int left_len, right_len;
+
+	if (fofs >= (pgoff_t)et->largest.fofs + et->largest.len ||
+			fofs + len <= et->largest.fofs)
+		return;
+
+	/* Punch [fofs, fofs + len) from largest extent. */
+	largest_end = et->largest.fofs + et->largest.len;
+	punch_end = fofs + len;
+
+	left_len = fofs > et->largest.fofs ? fofs - et->largest.fofs : 0;
+	right_len = largest_end > punch_end ? largest_end - punch_end : 0;
+
+	if (left_len >= right_len) {
+		et->largest.len = left_len;
+	} else {
+		et->largest.blk += punch_end - et->largest.fofs;
+		et->largest.fofs = punch_end;
+		et->largest.len = right_len;
 	}
+	et->largest_updated = true;
 }
 
 void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
@@ -680,10 +697,10 @@ static void __update_extent_tree_range(struct inode *inode,
 		dei.len = 0;
 

fs/f2fs/extent_cache.c static void __update_extent_tree_range(struct inode *inode, · line 706

Quoted 10 lines
fs/f2fs/extent_cache.c +2 -2
 		/*
-		 * drop largest extent before lookup, in case it's already
+		 * punch largest extent before lookup, in case it's already
 		 * been shrunk from extent tree
 		 */
-		__drop_largest_extent(et, fofs, len);
+		__punch_largest_extent(et, fofs, len);
 	}
 
 	if (et->largest.len != 0 &&

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#12 lore raw

Re: [f2fs-dev] [PATCH RESEND 3/5] f2fs: punch largest extent instead of dropping it entirely on overlap

Quoted 6 lines · On 6/15/26 8:05 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 4 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Previously, when an extent being inserted overlaps with the largest extent, the largest extent is dropped entirely. This was done to handle

Quoted 7 lines

Please correct me if I missed anything, I remember that we will add largest extent in below path?

- __update_extent_tree_range
- __insert_extent_tree
- __try_update_largest_extent : update largest w/ right extent
- __try_update_largest_extent : update largest w/ left extent

The largest extent might not reside in the extent tree. If __update_extent_tree_range invokes __drop_largest_extent, the length of the largest extent will have been set to zero. As a result, the largest extent updated inside __try_update_largest_extent can end up smaller than the largest extent obtained after __punch_largest_extent completes its punch operation.

Thanks
Yongpeng,

Quoted 71 lines

Thanks,

Quoted 68 lines

the case where the largest extent is not in memory, avoiding inconsistency between the largest extent and the extent tree.

This patch changes the semantics of __drop_largest_extent (renamed to __punch_largest_extent): instead of discarding the entire largest extent when any overlap is detected, keep the larger remaining portion (left or right) after the punch. This preserves extent cache coverage for truncate and overwrite operations that only partially overlap the largest extent.

---
  fs/f2fs/extent_cache.c | 31 ++++++++++++++++++++++++-------
  1 file changed, 24 insertions(+), 7 deletions(-)
fs/f2fs/extent_cache.c +24 -7
index f8d94db60dc6..82d84c4e98b2 100644
@@ -397,14 +397,31 @@ static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
  	return count;
  }
 
-static void __drop_largest_extent(struct extent_tree *et,
+static void __punch_largest_extent(struct extent_tree *et,
  					pgoff_t fofs, unsigned int len)
  {
-	if (fofs < (pgoff_t)et->largest.fofs + et->largest.len &&
-			fofs + len > et->largest.fofs) {
-		et->largest.len = 0;
-		et->largest_updated = true;
+	unsigned int largest_end, punch_end;
+	unsigned int left_len, right_len;
+
+	if (fofs >= (pgoff_t)et->largest.fofs + et->largest.len ||
+			fofs + len <= et->largest.fofs)
+		return;
+
+	/* Punch [fofs, fofs + len) from largest extent. */
+	largest_end = et->largest.fofs + et->largest.len;
+	punch_end = fofs + len;
+
+	left_len = fofs > et->largest.fofs ? fofs - et->largest.fofs : 0;
+	right_len = largest_end > punch_end ? largest_end - punch_end : 0;
+
+	if (left_len >= right_len) {
+		et->largest.len = left_len;
+	} else {
+		et->largest.blk += punch_end - et->largest.fofs;
+		et->largest.fofs = punch_end;
+		et->largest.len = right_len;
  	}
+	et->largest_updated = true;
  }
 
  void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
@@ -680,10 +697,10 @@ static void __update_extent_tree_range(struct inode *inode,
  		dei.len = 0;
 
  		/*
-		 * drop largest extent before lookup, in case it's already
+		 * punch largest extent before lookup, in case it's already
  		 * been shrunk from extent tree
  		 */
-		__drop_largest_extent(et, fofs, len);
+		__punch_largest_extent(et, fofs, len);
  	}
 
Quoted 8 lines

fs/f2fs/extent_cache.c static void __update_extent_tree_range(struct inode *inode, · line 706

Quoted 1 line
fs/f2fs/extent_cache.c +0 -0
  	if (et->largest.len != 0 &&

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#13 lore raw

Re: [f2fs-dev] [PATCH RESEND 3/5] f2fs: punch largest extent instead of dropping it entirely on overlap

Quoted 11 lines · Yongpeng Yang
Quoted 9 lines · On 6/15/26 8:05 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 4 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Previously, when an extent being inserted overlaps with the largest extent, the largest extent is dropped entirely. This was done to handle

Please correct me if I missed anything, I remember that we will add largest extent in below path?

Quoted 7 lines
Quoted 4 lines

- __update_extent_tree_range
  - __insert_extent_tree
   - __try_update_largest_extent : update largest w/ right extent
  - __try_update_largest_extent : update largest w/ left extent

The largest extent might not reside in the extent tree. If __update_extent_tree_range invokes __drop_largest_extent, the length of

I meant __update_extent_tree_range invokes __drop_largest_extent to drop largest first, and then, it tries to update largest extent w/ larger-size one of separated two extents.

Quoted 4 lines

the largest extent will have been set to zero. As a result, the largest extent updated inside __try_update_largest_extent can end up smaller than the largest extent obtained after __punch_largest_extent completes its punch operation.

I didn't get it, can you give an example for this?

Thanks,

Quoted 80 lines

Thanks
Yongpeng,

Quoted 75 lines

Thanks,

Quoted 64 lines

the case where the largest extent is not in memory, avoiding inconsistency between the largest extent and the extent tree.

This patch changes the semantics of __drop_largest_extent (renamed to __punch_largest_extent): instead of discarding the entire largest extent when any overlap is detected, keep the larger remaining portion (left or right) after the punch. This preserves extent cache coverage for truncate and overwrite operations that only partially overlap the largest extent.

---
  fs/f2fs/extent_cache.c | 31 ++++++++++++++++++++++++-------

  1 file changed, 24 insertions(+), 7 deletions(-)
fs/f2fs/extent_cache.c +24 -7
index f8d94db60dc6..82d84c4e98b2 100644
@@ -397,14 +397,31 @@ static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
       return count;
   }
-static void __drop_largest_extent(struct extent_tree *et,
+static void __punch_largest_extent(struct extent_tree *et,
                       pgoff_t fofs, unsigned int len)
   {
-    if (fofs < (pgoff_t)et->largest.fofs + et->largest.len &&
-            fofs + len > et->largest.fofs) {
-        et->largest.len = 0;
-        et->largest_updated = true;
+    unsigned int largest_end, punch_end;
+    unsigned int left_len, right_len;
+
+    if (fofs >= (pgoff_t)et->largest.fofs + et->largest.len ||
+            fofs + len <= et->largest.fofs)
+        return;
+
+    /* Punch [fofs, fofs + len) from largest extent. */
+    largest_end = et->largest.fofs + et->largest.len;
+    punch_end = fofs + len;
+
+    left_len = fofs > et->largest.fofs ? fofs - et->largest.fofs : 0;
+    right_len = largest_end > punch_end ? largest_end - punch_end : 0;
+
+    if (left_len >= right_len) {
+        et->largest.len = left_len;
+    } else {
+        et->largest.blk += punch_end - et->largest.fofs;
+        et->largest.fofs = punch_end;
+        et->largest.len = right_len;
       }
+    et->largest_updated = true;
   }
   void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
@@ -680,10 +697,10 @@ static void __update_extent_tree_range(struct inode *inode,
           dei.len = 0;
           /*
-         * drop largest extent before lookup, in case it's already
+         * punch largest extent before lookup, in case it's already
            * been shrunk from extent tree
            */
-        __drop_largest_extent(et, fofs, len);
+        __punch_largest_extent(et, fofs, len);
       }
Quoted 1 line

      if (et->largest.len != 0 &&

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#14 lore raw

Re: [f2fs-dev] [PATCH RESEND 3/5] f2fs: punch largest extent instead of dropping it entirely on overlap

Quoted 24 lines · On 6/20/26 2:15 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 11 lines · Yongpeng Yang
Quoted 9 lines · On 6/15/26 8:05 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 4 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Previously, when an extent being inserted overlaps with the largest extent, the largest extent is dropped entirely. This was done to handle

Please correct me if I missed anything, I remember that we will add largest extent in below path?

Quoted 7 lines
Quoted 4 lines

- __update_extent_tree_range
  - __insert_extent_tree
   - __try_update_largest_extent : update largest w/ right extent
  - __try_update_largest_extent : update largest w/ left extent

The largest extent might not reside in the extent tree. If __update_extent_tree_range invokes __drop_largest_extent, the length of

I meant __update_extent_tree_range invokes __drop_largest_extent to drop largest first, and then, it tries to update largest extent w/ larger-size one of separated two extents.

Quoted 6 lines
Quoted 4 lines

the largest extent will have been set to zero. As a result, the largest extent updated inside __try_update_largest_extent can end up smaller than the largest extent obtained after __punch_largest_extent completes its punch operation.

I didn't get it, can you give an example for this?

The only distinction between punch and drop arises when the largest extent is not present in the extent tree.

Before this patch(extent format [fofs, len, blk])
1. inital state

    largest extent: [0, 1024, 10], extent tree: empty
2. insert [511, 1, 10000]
    largest extent: [511, 1, 10000], extent tree: [511, 1, 10000]

After this patch:
1. inital state

    largest extent: [0, 1024, 10], extent tree: empty
2. insert [511, 1, 10000]
    largest extent: [512, 512, 522], extent tree: [511, 1, 10000]

Thanks
Yongpeng,

Quoted 88 lines

Thanks,

Quoted 82 lines

Thanks
Yongpeng,

Quoted 77 lines

Thanks,

Quoted 66 lines

the case where the largest extent is not in memory, avoiding inconsistency between the largest extent and the extent tree.

This patch changes the semantics of __drop_largest_extent (renamed to __punch_largest_extent): instead of discarding the entire largest extent when any overlap is detected, keep the larger remaining portion (left or right) after the punch. This preserves extent cache coverage for truncate and overwrite operations that only partially overlap the largest extent.

---
  fs/f2fs/extent_cache.c | 31 ++++++++++++++++++++++++-------

  1 file changed, 24 insertions(+), 7 deletions(-)
fs/f2fs/extent_cache.c +24 -7
index f8d94db60dc6..82d84c4e98b2 100644
@@ -397,14 +397,31 @@ static unsigned int __free_extent_tree(struct 
 f2fs_sb_info *sbi,
       return count;
   }
-static void __drop_largest_extent(struct extent_tree *et,
+static void __punch_largest_extent(struct extent_tree *et,
                       pgoff_t fofs, unsigned int len)
   {
-    if (fofs < (pgoff_t)et->largest.fofs + et->largest.len &&
-            fofs + len > et->largest.fofs) {
-        et->largest.len = 0;
-        et->largest_updated = true;
+    unsigned int largest_end, punch_end;
+    unsigned int left_len, right_len;
+
+    if (fofs >= (pgoff_t)et->largest.fofs + et->largest.len ||
+            fofs + len <= et->largest.fofs)
+        return;
+
+    /* Punch [fofs, fofs + len) from largest extent. */
+    largest_end = et->largest.fofs + et->largest.len;
+    punch_end = fofs + len;
+
+    left_len = fofs > et->largest.fofs ? fofs - et->largest.fofs : 0;
+    right_len = largest_end > punch_end ? largest_end - punch_end : 0;
+
+    if (left_len >= right_len) {
+        et->largest.len = left_len;
+    } else {
+        et->largest.blk += punch_end - et->largest.fofs;
+        et->largest.fofs = punch_end;
+        et->largest.len = right_len;
       }
+    et->largest_updated = true;
   }
   void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
@@ -680,10 +697,10 @@ static void __update_extent_tree_range(struct 
 inode *inode,
           dei.len = 0;
           /*
-         * drop largest extent before lookup, in case it's already
+         * punch largest extent before lookup, in case it's already
            * been shrunk from extent tree
            */
-        __drop_largest_extent(et, fofs, len);
+        __punch_largest_extent(et, fofs, len);
       }
Quoted 1 line

      if (et->largest.len != 0 &&

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#15 lore raw

Re: [f2fs-dev] [PATCH RESEND 3/5] f2fs: punch largest extent instead of dropping it entirely on overlap

Quoted 31 lines · Yongpeng Yang
Quoted 29 lines · On 6/20/26 2:15 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 11 lines · Yongpeng Yang
Quoted 9 lines · On 6/15/26 8:05 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 4 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Previously, when an extent being inserted overlaps with the largest extent, the largest extent is dropped entirely. This was done to handle

Please correct me if I missed anything, I remember that we will add largest extent in below path?

Quoted 7 lines
Quoted 4 lines

- __update_extent_tree_range
  - __insert_extent_tree
   - __try_update_largest_extent : update largest w/ right extent
  - __try_update_largest_extent : update largest w/ left extent

The largest extent might not reside in the extent tree. If __update_extent_tree_range invokes __drop_largest_extent, the length of

I meant __update_extent_tree_range invokes __drop_largest_extent to drop largest first, and then, it tries to update largest extent w/ larger-size one of separated two extents.

Quoted 4 lines

the largest extent will have been set to zero. As a result, the largest extent updated inside __try_update_largest_extent can end up smaller than the largest extent obtained after __punch_largest_extent completes its punch operation.

Quoted 8 lines
Quoted 1 line

I didn't get it, can you give an example for this?

The only distinction between punch and drop arises when the largest extent is not present in the extent tree.

Before this patch(extent format [fofs, len, blk])
1. inital state
    largest extent: [0, 1024, 10], extent tree: empty

                                                   ^^^^^

It's not empty w/o patch 2/5.

Thanks,

Quoted 103 lines

2. insert [511, 1, 10000]
    largest extent: [511, 1, 10000], extent tree: [511, 1, 10000]

After this patch:
1. inital state
    largest extent: [0, 1024, 10], extent tree: empty
2. insert [511, 1, 10000]
    largest extent: [512, 512, 522], extent tree: [511, 1, 10000]

Thanks
Yongpeng,

Quoted 90 lines

Thanks,

Quoted 80 lines

Thanks
Yongpeng,

Quoted 75 lines

Thanks,

Quoted 64 lines

the case where the largest extent is not in memory, avoiding inconsistency between the largest extent and the extent tree.

This patch changes the semantics of __drop_largest_extent (renamed to __punch_largest_extent): instead of discarding the entire largest extent when any overlap is detected, keep the larger remaining portion (left or right) after the punch. This preserves extent cache coverage for truncate and overwrite operations that only partially overlap the largest extent.

---
  fs/f2fs/extent_cache.c | 31 ++++++++++++++++++++++++-------

  1 file changed, 24 insertions(+), 7 deletions(-)
fs/f2fs/extent_cache.c +24 -7
index f8d94db60dc6..82d84c4e98b2 100644
@@ -397,14 +397,31 @@ static unsigned int __free_extent_tree(struct f2fs_sb_info *sbi,
       return count;
   }
-static void __drop_largest_extent(struct extent_tree *et,
+static void __punch_largest_extent(struct extent_tree *et,
                       pgoff_t fofs, unsigned int len)
   {
-    if (fofs < (pgoff_t)et->largest.fofs + et->largest.len &&
-            fofs + len > et->largest.fofs) {
-        et->largest.len = 0;
-        et->largest_updated = true;
+    unsigned int largest_end, punch_end;
+    unsigned int left_len, right_len;
+
+    if (fofs >= (pgoff_t)et->largest.fofs + et->largest.len ||
+            fofs + len <= et->largest.fofs)
+        return;
+
+    /* Punch [fofs, fofs + len) from largest extent. */
+    largest_end = et->largest.fofs + et->largest.len;
+    punch_end = fofs + len;
+
+    left_len = fofs > et->largest.fofs ? fofs - et->largest.fofs : 0;
+    right_len = largest_end > punch_end ? largest_end - punch_end : 0;
+
+    if (left_len >= right_len) {
+        et->largest.len = left_len;
+    } else {
+        et->largest.blk += punch_end - et->largest.fofs;
+        et->largest.fofs = punch_end;
+        et->largest.len = right_len;
       }
+    et->largest_updated = true;
   }
   void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)
@@ -680,10 +697,10 @@ static void __update_extent_tree_range(struct inode *inode,
           dei.len = 0;
           /*
-         * drop largest extent before lookup, in case it's already
+         * punch largest extent before lookup, in case it's already
            * been shrunk from extent tree
            */
-        __drop_largest_extent(et, fofs, len);
+        __punch_largest_extent(et, fofs, len);
       }
Quoted 1 line

      if (et->largest.len != 0 &&

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#16 lore raw

Re: [f2fs-dev] [PATCH RESEND 3/5] f2fs: punch largest extent instead of dropping it entirely on overlap

Quoted 40 lines · On 6/22/26 08:45, Chao Yu via Linux-f2fs-devel wrote:
Quoted 39 lines · Yongpeng Yang
Quoted 27 lines · On 6/20/26 2:15 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 13 lines · Yongpeng Yang
Quoted 11 lines · On 6/15/26 8:05 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 5 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Previously, when an extent being inserted overlaps with the largest extent, the largest extent is dropped entirely. This was done to handle

Please correct me if I missed anything, I remember that we will add largest
extent in below path?

Quoted 7 lines
Quoted 4 lines

- __update_extent_tree_range
  - __insert_extent_tree
   - __try_update_largest_extent : update largest w/ right extent
  - __try_update_largest_extent : update largest w/ left extent

The largest extent might not reside in the extent tree. If __update_extent_tree_range invokes __drop_largest_extent, the length of

I meant __update_extent_tree_range invokes __drop_largest_extent to drop largest first, and then, it tries to update largest extent w/ larger- size
one of separated two extents.

Quoted 6 lines
Quoted 4 lines

the largest extent will have been set to zero. As a result, the largest extent updated inside __try_update_largest_extent can end up smaller than the largest extent obtained after __punch_largest_extent completes its punch operation.

I didn't get it, can you give an example for this?

The only distinction between punch and drop arises when the largest extent is not present in the extent tree.

Quoted 7 lines
Quoted 3 lines

Before this patch(extent format [fofs, len, blk])
1. inital state
     largest extent: [0, 1024, 10], extent tree: empty

                                                   ^^^^^

It's not empty w/o patch 2/5.

Sorry for the confusion. "empty" means that the largest extent has been shrunk.

Thanks
Yongpeng,

Quoted 115 lines

Thanks,

Quoted 109 lines

2. insert [511, 1, 10000]
     largest extent: [511, 1, 10000], extent tree: [511, 1, 10000]

After this patch:
1. inital state
     largest extent: [0, 1024, 10], extent tree: empty
2. insert [511, 1, 10000]
     largest extent: [512, 512, 522], extent tree: [511, 1, 10000]

Thanks
Yongpeng,

Quoted 96 lines

Thanks,

Quoted 86 lines

Thanks
Yongpeng,

Quoted 81 lines

Thanks,

Quoted 70 lines

the case where the largest extent is not in memory, avoiding inconsistency between the largest extent and the extent tree.

This patch changes the semantics of __drop_largest_extent (renamed to __punch_largest_extent): instead of discarding the entire largest extent when any overlap is detected, keep the larger remaining portion
(left or right) after the punch. This preserves extent cache coverage for truncate and overwrite operations that only partially overlap the largest extent.

---
  fs/f2fs/extent_cache.c | 31 ++++++++++++++++++++++++-------

  1 file changed, 24 insertions(+), 7 deletions(-)
fs/f2fs/extent_cache.c +22 -5
index f8d94db60dc6..82d84c4e98b2 100644
@@ -397,14 +397,31 @@ static unsigned int
 __free_extent_tree(struct f2fs_sb_info *sbi,
       return count;
   }
-static void __drop_largest_extent(struct extent_tree *et,
+static void __punch_largest_extent(struct extent_tree *et,
                       pgoff_t fofs, unsigned int len)
   {
-    if (fofs < (pgoff_t)et->largest.fofs + et->largest.len &&
-            fofs + len > et->largest.fofs) {
-        et->largest.len = 0;
-        et->largest_updated = true;
+    unsigned int largest_end, punch_end;
+    unsigned int left_len, right_len;
+
+    if (fofs >= (pgoff_t)et->largest.fofs + et->largest.len ||
+            fofs + len <= et->largest.fofs)
+        return;
+
+    /* Punch [fofs, fofs + len) from largest extent. */
+    largest_end = et->largest.fofs + et->largest.len;
+    punch_end = fofs + len;
+
+    left_len = fofs > et->largest.fofs ? fofs - et-
 >largest.fofs : 0;
+    right_len = largest_end > punch_end ? largest_end -
 punch_end : 0;
+
+    if (left_len >= right_len) {
+        et->largest.len = left_len;
+    } else {
+        et->largest.blk += punch_end - et->largest.fofs;
+        et->largest.fofs = punch_end;
+        et->largest.len = right_len;
       }
+    et->largest_updated = true;
   }

  void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)

fs/f2fs/extent_cache.c +2 -2
@@ -680,10 +697,10 @@ static void
 __update_extent_tree_range(struct inode *inode,
           dei.len = 0;
           /*
-         * drop largest extent before lookup, in case it's already
+         * punch largest extent before lookup, in case it's already
            * been shrunk from extent tree
            */
-        __drop_largest_extent(et, fofs, len);
+        __punch_largest_extent(et, fofs, len);
       }
Quoted 1 line

      if (et->largest.len != 0 &&

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#17 lore raw

Re: [f2fs-dev] [PATCH RESEND 3/5] f2fs: punch largest extent instead of dropping it entirely on overlap

Quoted 46 lines · Yongpeng Yang
Quoted 44 lines · On 6/22/26 08:45, Chao Yu via Linux-f2fs-devel wrote:
Quoted 42 lines · Yongpeng Yang
Quoted 27 lines · On 6/20/26 2:15 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 13 lines · Yongpeng Yang
Quoted 11 lines · On 6/15/26 8:05 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 5 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Previously, when an extent being inserted overlaps with the largest extent, the largest extent is dropped entirely. This was done to handle

Please correct me if I missed anything, I remember that we will add largest
extent in below path?

Quoted 7 lines
Quoted 4 lines

- __update_extent_tree_range
  - __insert_extent_tree
   - __try_update_largest_extent : update largest w/ right extent
  - __try_update_largest_extent : update largest w/ left extent

The largest extent might not reside in the extent tree. If __update_extent_tree_range invokes __drop_largest_extent, the length of

I meant __update_extent_tree_range invokes __drop_largest_extent to drop largest first, and then, it tries to update largest extent w/ larger- size
one of separated two extents.

Quoted 6 lines
Quoted 4 lines

the largest extent will have been set to zero. As a result, the largest extent updated inside __try_update_largest_extent can end up smaller than the largest extent obtained after __punch_largest_extent completes its punch operation.

I didn't get it, can you give an example for this?

The only distinction between punch and drop arises when the largest extent is not present in the extent tree.

Before this patch(extent format [fofs, len, blk])
1. inital state
     largest extent: [0, 1024, 10], extent tree: empty

Quoted 6 lines
Quoted 3 lines

                                                   ^^^^^

It's not empty w/o patch 2/5.

Sorry for the confusion. "empty" means that the largest extent has been shrunk.

Okay,

Hmm...I found it's hard to reproduce such condition "only shrink extent slab cache and keep inode slab cache", so I wonder how common this issue will be in product.

Have you suffered real issues in product? or just insight from LLM or code review?

Thanks,

Quoted 124 lines

Thanks
Yongpeng,

Quoted 119 lines

Thanks,

Quoted 109 lines

2. insert [511, 1, 10000]
     largest extent: [511, 1, 10000], extent tree: [511, 1, 10000]

After this patch:
1. inital state
     largest extent: [0, 1024, 10], extent tree: empty
2. insert [511, 1, 10000]
     largest extent: [512, 512, 522], extent tree: [511, 1, 10000]

Thanks
Yongpeng,

Quoted 96 lines

Thanks,

Quoted 86 lines

Thanks
Yongpeng,

Quoted 81 lines

Thanks,

Quoted 70 lines

the case where the largest extent is not in memory, avoiding inconsistency between the largest extent and the extent tree.

This patch changes the semantics of __drop_largest_extent (renamed to __punch_largest_extent): instead of discarding the entire largest extent when any overlap is detected, keep the larger remaining portion
(left or right) after the punch. This preserves extent cache coverage for truncate and overwrite operations that only partially overlap the largest extent.

---
  fs/f2fs/extent_cache.c | 31 ++++++++++++++++++++++++-------

  1 file changed, 24 insertions(+), 7 deletions(-)
fs/f2fs/extent_cache.c +22 -5
index f8d94db60dc6..82d84c4e98b2 100644
@@ -397,14 +397,31 @@ static unsigned int
 __free_extent_tree(struct f2fs_sb_info *sbi,
       return count;
   }
-static void __drop_largest_extent(struct extent_tree *et,
+static void __punch_largest_extent(struct extent_tree *et,
                       pgoff_t fofs, unsigned int len)
   {
-    if (fofs < (pgoff_t)et->largest.fofs + et->largest.len &&
-            fofs + len > et->largest.fofs) {
-        et->largest.len = 0;
-        et->largest_updated = true;
+    unsigned int largest_end, punch_end;
+    unsigned int left_len, right_len;
+
+    if (fofs >= (pgoff_t)et->largest.fofs + et->largest.len ||
+            fofs + len <= et->largest.fofs)
+        return;
+
+    /* Punch [fofs, fofs + len) from largest extent. */
+    largest_end = et->largest.fofs + et->largest.len;
+    punch_end = fofs + len;
+
+    left_len = fofs > et->largest.fofs ? fofs - et-
 > largest.fofs : 0;
+    right_len = largest_end > punch_end ? largest_end -
 punch_end : 0;
+
+    if (left_len >= right_len) {
+        et->largest.len = left_len;
+    } else {
+        et->largest.blk += punch_end - et->largest.fofs;
+        et->largest.fofs = punch_end;
+        et->largest.len = right_len;
       }
+    et->largest_updated = true;
   }

  void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)

fs/f2fs/extent_cache.c +2 -2
@@ -680,10 +697,10 @@ static void
 __update_extent_tree_range(struct inode *inode,
           dei.len = 0;
           /*
-         * drop largest extent before lookup, in case it's already
+         * punch largest extent before lookup, in case it's already
            * been shrunk from extent tree
            */
-        __drop_largest_extent(et, fofs, len);
+        __punch_largest_extent(et, fofs, len);
       }
Quoted 1 line

      if (et->largest.len != 0 &&

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#18 lore raw

Re: [f2fs-dev] [PATCH RESEND 3/5] f2fs: punch largest extent instead of dropping it entirely on overlap

Quoted 54 lines · On 6/25/26 15:49, Chao Yu via Linux-f2fs-devel wrote:
Quoted 46 lines · Yongpeng Yang
Quoted 44 lines · On 6/22/26 08:45, Chao Yu via Linux-f2fs-devel wrote:
Quoted 42 lines · Yongpeng Yang
Quoted 27 lines · On 6/20/26 2:15 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 13 lines · Yongpeng Yang
Quoted 11 lines · On 6/15/26 8:05 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 5 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Previously, when an extent being inserted overlaps with the largest extent, the largest extent is dropped entirely. This was done to handle

Please correct me if I missed anything, I remember that we will add largest
extent in below path?

Quoted 7 lines
Quoted 4 lines

- __update_extent_tree_range
  - __insert_extent_tree
   - __try_update_largest_extent : update largest w/ right extent
  - __try_update_largest_extent : update largest w/ left extent

The largest extent might not reside in the extent tree. If __update_extent_tree_range invokes __drop_largest_extent, the length of

I meant __update_extent_tree_range invokes __drop_largest_extent to drop largest first, and then, it tries to update largest extent w/ larger- size
one of separated two extents.

Quoted 6 lines
Quoted 4 lines

the largest extent will have been set to zero. As a result, the largest extent updated inside __try_update_largest_extent can end up smaller than the largest extent obtained after __punch_largest_extent completes its punch operation.

I didn't get it, can you give an example for this?

The only distinction between punch and drop arises when the largest extent is not present in the extent tree.

Before this patch(extent format [fofs, len, blk])
1. inital state
     largest extent: [0, 1024, 10], extent tree: empty

Quoted 6 lines
Quoted 3 lines

                                                   ^^^^^

It's not empty w/o patch 2/5.

Sorry for the confusion. "empty" means that the largest extent has been shrunk.

Quoted 8 lines

Okay,

Hmm...I found it's hard to reproduce such condition "only shrink extent slab cache and keep inode slab cache", so I wonder how common this issue will be in product.

Have you suffered real issues in product? or just insight from LLM or code review?

I was able to reproduce cases where the largest extent was shrunk, and then a 4K overwrite on that largest extent caused it to be dropped. The test scenario is roughly as follows, although the reproduction is not 100% reliable:

1. Construct a largest extent, then perform sparse writes to other regions so that the largest extent is neither updated nor accessed.
2. Keep a background thread continuously opening the file to prevent the inode from being shrunk.
3. Trigger memory reclaim.
4. Perform a 4K overwrite within the largest extent.

I have not observed this in a production environment. It was identified through code review while investigating the fix for [PATCH 1/5], and then verified with the above test case.

Thanks
Yongpeng,

Quoted 130 lines

Thanks,

Quoted 124 lines

Thanks
Yongpeng,

Quoted 119 lines

Thanks,

Quoted 109 lines

2. insert [511, 1, 10000]
     largest extent: [511, 1, 10000], extent tree: [511, 1, 10000]

After this patch:
1. inital state
     largest extent: [0, 1024, 10], extent tree: empty
2. insert [511, 1, 10000]
     largest extent: [512, 512, 522], extent tree: [511, 1, 10000]

Thanks
Yongpeng,

Quoted 96 lines

Thanks,

Quoted 86 lines

Thanks
Yongpeng,

Quoted 81 lines

Thanks,

Quoted 70 lines

the case where the largest extent is not in memory, avoiding inconsistency between the largest extent and the extent tree.

This patch changes the semantics of __drop_largest_extent (renamed to __punch_largest_extent): instead of discarding the entire largest extent when any overlap is detected, keep the larger remaining portion
(left or right) after the punch. This preserves extent cache coverage for truncate and overwrite operations that only partially overlap the largest extent.

---
  fs/f2fs/extent_cache.c | 31 ++++++++++++++++++++++++-------

  1 file changed, 24 insertions(+), 7 deletions(-)
fs/f2fs/extent_cache.c +22 -5
index f8d94db60dc6..82d84c4e98b2 100644
@@ -397,14 +397,31 @@ static unsigned int
 __free_extent_tree(struct f2fs_sb_info *sbi,
       return count;
   }
-static void __drop_largest_extent(struct extent_tree *et,
+static void __punch_largest_extent(struct extent_tree *et,
                       pgoff_t fofs, unsigned int len)
   {
-    if (fofs < (pgoff_t)et->largest.fofs + et->largest.len &&
-            fofs + len > et->largest.fofs) {
-        et->largest.len = 0;
-        et->largest_updated = true;
+    unsigned int largest_end, punch_end;
+    unsigned int left_len, right_len;
+
+    if (fofs >= (pgoff_t)et->largest.fofs + et->largest.len ||
+            fofs + len <= et->largest.fofs)
+        return;
+
+    /* Punch [fofs, fofs + len) from largest extent. */
+    largest_end = et->largest.fofs + et->largest.len;
+    punch_end = fofs + len;
+
+    left_len = fofs > et->largest.fofs ? fofs - et-
 > largest.fofs : 0;
+    right_len = largest_end > punch_end ? largest_end -
 punch_end : 0;
+
+    if (left_len >= right_len) {
+        et->largest.len = left_len;
+    } else {
+        et->largest.blk += punch_end - et->largest.fofs;
+        et->largest.fofs = punch_end;
+        et->largest.len = right_len;
       }
+    et->largest_updated = true;
   }

  void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio)

fs/f2fs/extent_cache.c +2 -2
@@ -680,10 +697,10 @@ static void
 __update_extent_tree_range(struct inode *inode,
           dei.len = 0;
           /*
-         * drop largest extent before lookup, in case it's already
+         * punch largest extent before lookup, in case it's already
            * been shrunk from extent tree
            */
-        __drop_largest_extent(et, fofs, len);
+        __punch_largest_extent(et, fofs, len);
       }
Quoted 1 line

      if (et->largest.len != 0 &&

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#19 lore raw

PATCH RESEND 4/5 f2fs: add extent_access_mode to track extent cache access patterns

In reply to Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Introduce enum extent_access_mode to classify how each extent node is accessed or created (READ, WRITE, PRECACHE, TRUNCATE, LARGEST). This metadata optimize LRU eviction decisions:

1. Extents only accessed as the largest extent (never read-hit) are deprioritized in the LRU list since reads can still use the largest extent directly.

2. Sparse single-block write extents that were never merged are moved to the head of LRU for earlier reclaim, preserving extents with better continuity and higher read-hit probability.

---
 fs/f2fs/data.c         |  4 ++--
 fs/f2fs/extent_cache.c | 29 ++++++++++++++++++++++++++++-
 fs/f2fs/f2fs.h         | 14 +++++++++++++-
 fs/f2fs/file.c         |  6 ++++--
 4 files changed, 47 insertions(+), 6 deletions(-)
fs/f2fs/data.c +2 -2
index 9c6440a7db0e..2d38135005fe 100644
@@ -1873,7 +1873,7 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
 
 			f2fs_update_read_extent_cache_range(&dn,
 				start_pgofs, map->m_pblk + ofs,
-				map->m_len - ofs);
+				map->m_len - ofs, EX_ACCESS_PRECACHE);
 		}
 	}
 
@@ -1919,7 +1919,7 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
 			if (map->m_len > ofs)
 				f2fs_update_read_extent_cache_range(&dn,
 					start_pgofs, map->m_pblk + ofs,
-					map->m_len - ofs);
+					map->m_len - ofs, EX_ACCESS_PRECACHE);
 		}
 		if (map->m_next_extent)
 			*map->m_next_extent = is_hole ? pgofs + 1 : pgofs;
fs/f2fs/extent_cache.c +28 -1
index 82d84c4e98b2..e141ffb64e5f 100644
@@ -142,6 +142,7 @@ static void __try_update_largest_extent(struct extent_tree *et,
 	if (en->ei.len <= et->largest.len)
 		return;
 
+	en->ei.last_access_mode = EX_ACCESS_LARGEST;
 	et->largest = en->ei;
 	et->largest_updated = true;
 }
@@ -518,6 +519,7 @@ static bool __lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
 		stat_inc_rbtree_node_hit(sbi, type);
 
 	*ei = en->ei;
+	en->ei.last_access_mode = EX_ACCESS_READ;
 	spin_lock(&eti->extent_lock);
 	if (!list_empty(&en->list)) {
 		list_move_tail(&en->list, &eti->extent_list);
@@ -624,6 +626,21 @@ static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
 
 	/* update in global extent list */
 	spin_lock(&eti->extent_lock);
+	/*
+	 * 1. For the largest extent, if subsequent writes are not merged into
+	 * it, the write path will most likely not use the largest extent_node,
+	 * while read requests can still access the mapping through the largest
+	 * extent.
+	 *
+	 * 2. For sparse writes, if the extent length is 1 and no extent merging
+	 * occurs, this extent should be reclaimed with higher priority to avoid
+	 * evicting extents with better continuity and higher read-hit.
+	 */
+	if (et->type == EX_READ && et->cached_en &&
+		(et->cached_en->ei.last_access_mode == EX_ACCESS_LARGEST ||
+		 (et->cached_en->ei.len == 1 &&
+		  et->cached_en->ei.last_access_mode == EX_ACCESS_WRITE)))
+		list_move(&et->cached_en->list, &eti->extent_list);
 	list_add_tail(&en->list, &eti->extent_list);
 	et->cached_en = en;
 	spin_unlock(&eti->extent_lock);
@@ -747,6 +764,8 @@ static void __update_extent_tree_range(struct inode *inode,
 		if (fofs > dei.fofs && (type != EX_READ ||
 				fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN)) {
 			en->ei.len = fofs - en->ei.fofs;
+			if (type == EX_READ)
+				en->ei.last_access_mode = EX_ACCESS_TRUNCATE;
 			prev_en = en;
 			parts = 1;
 		}
@@ -761,6 +780,8 @@ static void __update_extent_tree_range(struct inode *inode,
 					end - dei.fofs + dei.blk, false,
 					dei.age, dei.last_blocks,
 					type);
+				if (type == EX_READ)
+					ei.last_access_mode = EX_ACCESS_TRUNCATE;
 				en1 = __insert_extent_tree(sbi, et, &ei,
 							NULL, NULL, true);
 				next_en = en1;
@@ -770,6 +791,8 @@ static void __update_extent_tree_range(struct inode *inode,
 					en->ei.blk + (end - dei.fofs), true,
 					dei.age, dei.last_blocks,
 					type);
+				if (type == EX_READ)
+					en->ei.last_access_mode = EX_ACCESS_TRUNCATE;
 				next_en = en;
 			}
 			parts++;
@@ -808,6 +831,7 @@ static void __update_extent_tree_range(struct inode *inode,
 	if (tei->blk) {
 		__set_extent_info(&ei, fofs, len, tei->blk, false,
 				  0, 0, EX_READ);
+		ei.last_access_mode = tei->last_access_mode;
 		if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
 			__insert_extent_tree(sbi, et, &ei,
 					insert_p, insert_parent, leftmost);
@@ -978,6 +1002,7 @@ static void __update_extent_cache(struct dnode_of_data *dn, enum extent_type typ
 			ei.blk = NULL_ADDR;
 		else
 			ei.blk = dn->data_blkaddr;
+		ei.last_access_mode = EX_ACCESS_WRITE;
 	} else if (type == EX_BLOCK_AGE) {
 		if (__get_new_block_age(dn->inode, &ei, dn->data_blkaddr))
 			return;
@@ -1091,12 +1116,14 @@ void f2fs_update_read_extent_cache(struct dnode_of_data *dn)
 }
 
 void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
-				pgoff_t fofs, block_t blkaddr, unsigned int len)
+				pgoff_t fofs, block_t blkaddr, unsigned int len,
+				enum extent_access_mode access_mode)
 {
 	struct extent_info ei = {
 		.fofs = fofs,
 		.len = len,
 		.blk = blkaddr,
+		.last_access_mode = access_mode,
 	};
 
 	if (!__may_extent_tree(dn->inode, EX_READ))
fs/f2fs/f2fs.h +13 -1
index fffb516b78f4..1588b64d04a3 100644
@@ -763,6 +763,15 @@ enum extent_type {
 	NR_EXTENT_CACHES,
 };
 
+/* extent acces mode for cache hit or extent add */
+enum extent_access_mode {
+	EX_ACCESS_READ,
+	EX_ACCESS_WRITE,
+	EX_ACCESS_PRECACHE,
+	EX_ACCESS_TRUNCATE,
+	EX_ACCESS_LARGEST,
+};
+
 /*
  * Reserved value to mark invalid age extents, hence valid block range
  * from 0 to ULLONG_MAX-1
@@ -781,6 +790,8 @@ struct extent_info {
 			/* physical extent length of compressed blocks */
 			unsigned int c_len;
 #endif
+			/* record last access mode */
+			enum extent_access_mode last_access_mode;
 		};
 		/* block age extent_cache */
 		struct {
@@ -4577,7 +4588,8 @@ bool f2fs_lookup_read_extent_cache_block(struct inode *inode, pgoff_t index,
 			block_t *blkaddr);
 void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
 void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
-			pgoff_t fofs, block_t blkaddr, unsigned int len);
+				pgoff_t fofs, block_t blkaddr, unsigned int len,
+				enum extent_access_mode access_mode);
 unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
 			int nr_shrink);
 
fs/f2fs/file.c +4 -2
index 633e9ade654f..a3a5d499eadf 100644
@@ -733,7 +733,8 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
 		 */
 		fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_folio),
 							dn->inode) + ofs;
-		f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
+		f2fs_update_read_extent_cache_range(dn, fofs, 0, len,
+							EX_ACCESS_TRUNCATE);
 		f2fs_update_age_extent_cache_range(dn, fofs, len);
 		dec_valid_block_count(sbi, dn->inode, nr_free);
 	}
@@ -1672,7 +1673,8 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
 
 	if (index > start) {
 		f2fs_update_read_extent_cache_range(dn, start, 0,
-							index - start);
+							index - start,
+							EX_ACCESS_TRUNCATE);
 		f2fs_update_age_extent_cache_range(dn, start, index - start);
 	}
 
Signature · 8 lines
2.43.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#20 lore raw

Re: [f2fs-dev] [PATCH RESEND 4/5] f2fs: add extent_access_mode to track extent cache access patterns

Quoted 5 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Introduce enum extent_access_mode to classify how each extent node is accessed or created (READ, WRITE, PRECACHE, TRUNCATE, LARGEST). This metadata optimize LRU eviction decisions:

Can you please give some numbers for this change?

Quoted 161 lines

1. Extents only accessed as the largest extent (never read-hit) are deprioritized in the LRU list since reads can still use the largest extent directly.

2. Sparse single-block write extents that were never merged are moved to the head of LRU for earlier reclaim, preserving extents with better continuity and higher read-hit probability.

---
 fs/f2fs/data.c         |  4 ++--
 fs/f2fs/extent_cache.c | 29 ++++++++++++++++++++++++++++-
 fs/f2fs/f2fs.h         | 14 +++++++++++++-
 fs/f2fs/file.c         |  6 ++++--
 4 files changed, 47 insertions(+), 6 deletions(-)
fs/f2fs/data.c +2 -2
index 9c6440a7db0e..2d38135005fe 100644
@@ -1873,7 +1873,7 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
 
 			f2fs_update_read_extent_cache_range(&dn,
 				start_pgofs, map->m_pblk + ofs,
-				map->m_len - ofs);
+				map->m_len - ofs, EX_ACCESS_PRECACHE);
 		}
 	}
 
@@ -1919,7 +1919,7 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
 			if (map->m_len > ofs)
 				f2fs_update_read_extent_cache_range(&dn,
 					start_pgofs, map->m_pblk + ofs,
-					map->m_len - ofs);
+					map->m_len - ofs, EX_ACCESS_PRECACHE);
 		}
 		if (map->m_next_extent)
 			*map->m_next_extent = is_hole ? pgofs + 1 : pgofs;
fs/f2fs/extent_cache.c +28 -1
index 82d84c4e98b2..e141ffb64e5f 100644
@@ -142,6 +142,7 @@ static void __try_update_largest_extent(struct extent_tree *et,
 	if (en->ei.len <= et->largest.len)
 		return;
 
+	en->ei.last_access_mode = EX_ACCESS_LARGEST;
 	et->largest = en->ei;
 	et->largest_updated = true;
 }
@@ -518,6 +519,7 @@ static bool __lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
 		stat_inc_rbtree_node_hit(sbi, type);
 
 	*ei = en->ei;
+	en->ei.last_access_mode = EX_ACCESS_READ;
 	spin_lock(&eti->extent_lock);
 	if (!list_empty(&en->list)) {
 		list_move_tail(&en->list, &eti->extent_list);
@@ -624,6 +626,21 @@ static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
 
 	/* update in global extent list */
 	spin_lock(&eti->extent_lock);
+	/*
+	 * 1. For the largest extent, if subsequent writes are not merged into
+	 * it, the write path will most likely not use the largest extent_node,
+	 * while read requests can still access the mapping through the largest
+	 * extent.
+	 *
+	 * 2. For sparse writes, if the extent length is 1 and no extent merging
+	 * occurs, this extent should be reclaimed with higher priority to avoid
+	 * evicting extents with better continuity and higher read-hit.
+	 */
+	if (et->type == EX_READ && et->cached_en &&
+		(et->cached_en->ei.last_access_mode == EX_ACCESS_LARGEST ||
+		 (et->cached_en->ei.len == 1 &&
+		  et->cached_en->ei.last_access_mode == EX_ACCESS_WRITE)))
+		list_move(&et->cached_en->list, &eti->extent_list);
 	list_add_tail(&en->list, &eti->extent_list);
 	et->cached_en = en;
 	spin_unlock(&eti->extent_lock);
@@ -747,6 +764,8 @@ static void __update_extent_tree_range(struct inode *inode,
 		if (fofs > dei.fofs && (type != EX_READ ||
 				fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN)) {
 			en->ei.len = fofs - en->ei.fofs;
+			if (type == EX_READ)
+				en->ei.last_access_mode = EX_ACCESS_TRUNCATE;
 			prev_en = en;
 			parts = 1;
 		}
@@ -761,6 +780,8 @@ static void __update_extent_tree_range(struct inode *inode,
 					end - dei.fofs + dei.blk, false,
 					dei.age, dei.last_blocks,
 					type);
+				if (type == EX_READ)
+					ei.last_access_mode = EX_ACCESS_TRUNCATE;
 				en1 = __insert_extent_tree(sbi, et, &ei,
 							NULL, NULL, true);
 				next_en = en1;
@@ -770,6 +791,8 @@ static void __update_extent_tree_range(struct inode *inode,
 					en->ei.blk + (end - dei.fofs), true,
 					dei.age, dei.last_blocks,
 					type);
+				if (type == EX_READ)
+					en->ei.last_access_mode = EX_ACCESS_TRUNCATE;
 				next_en = en;
 			}
 			parts++;
@@ -808,6 +831,7 @@ static void __update_extent_tree_range(struct inode *inode,
 	if (tei->blk) {
 		__set_extent_info(&ei, fofs, len, tei->blk, false,
 				  0, 0, EX_READ);
+		ei.last_access_mode = tei->last_access_mode;
 		if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
 			__insert_extent_tree(sbi, et, &ei,
 					insert_p, insert_parent, leftmost);
@@ -978,6 +1002,7 @@ static void __update_extent_cache(struct dnode_of_data *dn, enum extent_type typ
 			ei.blk = NULL_ADDR;
 		else
 			ei.blk = dn->data_blkaddr;
+		ei.last_access_mode = EX_ACCESS_WRITE;
 	} else if (type == EX_BLOCK_AGE) {
 		if (__get_new_block_age(dn->inode, &ei, dn->data_blkaddr))
 			return;
@@ -1091,12 +1116,14 @@ void f2fs_update_read_extent_cache(struct dnode_of_data *dn)
 }
 
 void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
-				pgoff_t fofs, block_t blkaddr, unsigned int len)
+				pgoff_t fofs, block_t blkaddr, unsigned int len,
+				enum extent_access_mode access_mode)
 {
 	struct extent_info ei = {
 		.fofs = fofs,
 		.len = len,
 		.blk = blkaddr,
+		.last_access_mode = access_mode,
 	};
 
 	if (!__may_extent_tree(dn->inode, EX_READ))
fs/f2fs/f2fs.h +9 -0
index fffb516b78f4..1588b64d04a3 100644
@@ -763,6 +763,15 @@ enum extent_type {
 	NR_EXTENT_CACHES,
 };
 
+/* extent acces mode for cache hit or extent add */
+enum extent_access_mode {
+	EX_ACCESS_READ,
+	EX_ACCESS_WRITE,
+	EX_ACCESS_PRECACHE,
+	EX_ACCESS_TRUNCATE,
+	EX_ACCESS_LARGEST,
+};
+
 /*
  * Reserved value to mark invalid age extents, hence valid block range
  * from 0 to ULLONG_MAX-1

fs/f2fs/f2fs.h struct extent_info { · line 794

Quoted 5 lines
fs/f2fs/f2fs.h +2 -0
@@ -781,6 +790,8 @@ struct extent_info {
 			/* physical extent length of compressed blocks */
 			unsigned int c_len;
 #endif
+			/* record last access mode */
+			enum extent_access_mode last_access_mode;

As we know, memory is expensive, :P, I'd like to know if we can enable this optionally if there is benefits.

Thanks,

Quoted 28 lines
fs/f2fs/f2fs.h +2 -1
 		};
 		/* block age extent_cache */
 		struct {
@@ -4577,7 +4588,8 @@ bool f2fs_lookup_read_extent_cache_block(struct inode *inode, pgoff_t index,
 			block_t *blkaddr);
 void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
 void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
-			pgoff_t fofs, block_t blkaddr, unsigned int len);
+				pgoff_t fofs, block_t blkaddr, unsigned int len,
+				enum extent_access_mode access_mode);
 unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
 			int nr_shrink);
 
fs/f2fs/file.c +2 -1
index 633e9ade654f..a3a5d499eadf 100644
@@ -733,7 +733,8 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
 		 */
 		fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_folio),
 							dn->inode) + ofs;
-		f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
+		f2fs_update_read_extent_cache_range(dn, fofs, 0, len,
+							EX_ACCESS_TRUNCATE);
 		f2fs_update_age_extent_cache_range(dn, fofs, len);
 		dec_valid_block_count(sbi, dn->inode, nr_free);
 	}

fs/f2fs/file.c static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start, · line 1680

Quoted 9 lines
fs/f2fs/file.c +2 -1
@@ -1672,7 +1673,8 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
 
 	if (index > start) {
 		f2fs_update_read_extent_cache_range(dn, start, 0,
-							index - start);
+							index - start,
+							EX_ACCESS_TRUNCATE);
 		f2fs_update_age_extent_cache_range(dn, start, index - start);
 	}
 

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#21 lore raw

Re: [f2fs-dev] [PATCH RESEND 4/5] f2fs: add extent_access_mode to track extent cache access patterns

Quoted 8 lines · On 6/15/26 8:05 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 5 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Introduce enum extent_access_mode to classify how each extent node is accessed or created (READ, WRITE, PRECACHE, TRUNCATE, LARGEST). This metadata optimize LRU eviction decisions:

Can you please give some numbers for this change?

This is a qualitative analysis. This patch aims to prioritize shrinking extents that are not read-hit and the largest extent.

Quoted 162 lines
Quoted 162 lines

1. Extents only accessed as the largest extent (never read-hit) are deprioritized in the LRU list since reads can still use the largest extent directly.

2. Sparse single-block write extents that were never merged are moved to the head of LRU for earlier reclaim, preserving extents with better continuity and higher read-hit probability.

---
  fs/f2fs/data.c         |  4 ++--
  fs/f2fs/extent_cache.c | 29 ++++++++++++++++++++++++++++-
  fs/f2fs/f2fs.h         | 14 +++++++++++++-
  fs/f2fs/file.c         |  6 ++++--
  4 files changed, 47 insertions(+), 6 deletions(-)
fs/f2fs/data.c +2 -2
index 9c6440a7db0e..2d38135005fe 100644
@@ -1873,7 +1873,7 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
 
  			f2fs_update_read_extent_cache_range(&dn,
  				start_pgofs, map->m_pblk + ofs,
-				map->m_len - ofs);
+				map->m_len - ofs, EX_ACCESS_PRECACHE);
  		}
  	}
 
@@ -1919,7 +1919,7 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
  			if (map->m_len > ofs)
  				f2fs_update_read_extent_cache_range(&dn,
  					start_pgofs, map->m_pblk + ofs,
-					map->m_len - ofs);
+					map->m_len - ofs, EX_ACCESS_PRECACHE);
  		}
  		if (map->m_next_extent)
  			*map->m_next_extent = is_hole ? pgofs + 1 : pgofs;
fs/f2fs/extent_cache.c +28 -1
index 82d84c4e98b2..e141ffb64e5f 100644
@@ -142,6 +142,7 @@ static void __try_update_largest_extent(struct extent_tree *et,
  	if (en->ei.len <= et->largest.len)
  		return;
 
+	en->ei.last_access_mode = EX_ACCESS_LARGEST;
  	et->largest = en->ei;
  	et->largest_updated = true;
  }
@@ -518,6 +519,7 @@ static bool __lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
  		stat_inc_rbtree_node_hit(sbi, type);
 
  	*ei = en->ei;
+	en->ei.last_access_mode = EX_ACCESS_READ;
  	spin_lock(&eti->extent_lock);
  	if (!list_empty(&en->list)) {
  		list_move_tail(&en->list, &eti->extent_list);
@@ -624,6 +626,21 @@ static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
 
  	/* update in global extent list */
  	spin_lock(&eti->extent_lock);
+	/*
+	 * 1. For the largest extent, if subsequent writes are not merged into
+	 * it, the write path will most likely not use the largest extent_node,
+	 * while read requests can still access the mapping through the largest
+	 * extent.
+	 *
+	 * 2. For sparse writes, if the extent length is 1 and no extent merging
+	 * occurs, this extent should be reclaimed with higher priority to avoid
+	 * evicting extents with better continuity and higher read-hit.
+	 */
+	if (et->type == EX_READ && et->cached_en &&
+		(et->cached_en->ei.last_access_mode == EX_ACCESS_LARGEST ||
+		 (et->cached_en->ei.len == 1 &&
+		  et->cached_en->ei.last_access_mode == EX_ACCESS_WRITE)))
+		list_move(&et->cached_en->list, &eti->extent_list);
  	list_add_tail(&en->list, &eti->extent_list);
  	et->cached_en = en;
  	spin_unlock(&eti->extent_lock);
@@ -747,6 +764,8 @@ static void __update_extent_tree_range(struct inode *inode,
  		if (fofs > dei.fofs && (type != EX_READ ||
  				fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN)) {
  			en->ei.len = fofs - en->ei.fofs;
+			if (type == EX_READ)
+				en->ei.last_access_mode = EX_ACCESS_TRUNCATE;
  			prev_en = en;
  			parts = 1;
  		}
@@ -761,6 +780,8 @@ static void __update_extent_tree_range(struct inode *inode,
  					end - dei.fofs + dei.blk, false,
  					dei.age, dei.last_blocks,
  					type);
+				if (type == EX_READ)
+					ei.last_access_mode = EX_ACCESS_TRUNCATE;
  				en1 = __insert_extent_tree(sbi, et, &ei,
  							NULL, NULL, true);
  				next_en = en1;
@@ -770,6 +791,8 @@ static void __update_extent_tree_range(struct inode *inode,
  					en->ei.blk + (end - dei.fofs), true,
  					dei.age, dei.last_blocks,
  					type);
+				if (type == EX_READ)
+					en->ei.last_access_mode = EX_ACCESS_TRUNCATE;
  				next_en = en;
  			}
  			parts++;
@@ -808,6 +831,7 @@ static void __update_extent_tree_range(struct inode *inode,
  	if (tei->blk) {
  		__set_extent_info(&ei, fofs, len, tei->blk, false,
  				  0, 0, EX_READ);
+		ei.last_access_mode = tei->last_access_mode;
  		if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
  			__insert_extent_tree(sbi, et, &ei,
  					insert_p, insert_parent, leftmost);
@@ -978,6 +1002,7 @@ static void __update_extent_cache(struct dnode_of_data *dn, enum extent_type typ
  			ei.blk = NULL_ADDR;
  		else
  			ei.blk = dn->data_blkaddr;
+		ei.last_access_mode = EX_ACCESS_WRITE;
  	} else if (type == EX_BLOCK_AGE) {
  		if (__get_new_block_age(dn->inode, &ei, dn->data_blkaddr))
  			return;
@@ -1091,12 +1116,14 @@ void f2fs_update_read_extent_cache(struct dnode_of_data *dn)
  }
 
  void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
-				pgoff_t fofs, block_t blkaddr, unsigned int len)
+				pgoff_t fofs, block_t blkaddr, unsigned int len,
+				enum extent_access_mode access_mode)
  {
  	struct extent_info ei = {
  		.fofs = fofs,
  		.len = len,
  		.blk = blkaddr,
+		.last_access_mode = access_mode,
  	};
 
  	if (!__may_extent_tree(dn->inode, EX_READ))
fs/f2fs/f2fs.h +9 -0
index fffb516b78f4..1588b64d04a3 100644
@@ -763,6 +763,15 @@ enum extent_type {
  	NR_EXTENT_CACHES,
  };
 
+/* extent acces mode for cache hit or extent add */
+enum extent_access_mode {
+	EX_ACCESS_READ,
+	EX_ACCESS_WRITE,
+	EX_ACCESS_PRECACHE,
+	EX_ACCESS_TRUNCATE,
+	EX_ACCESS_LARGEST,
+};
+
  /*
   * Reserved value to mark invalid age extents, hence valid block range
   * from 0 to ULLONG_MAX-1
@@ -781,6 +790,8 @@ struct extent_info {
Quoted 8 lines

fs/f2fs/f2fs.h struct extent_info { · line 794

Quoted 5 lines
fs/f2fs/f2fs.h +2 -0
  			/* physical extent length of compressed blocks */
  			unsigned int c_len;
  #endif
+			/* record last access mode */
+			enum extent_access_mode last_access_mode;

As we know, memory is expensive, :P, I'd like to know if we can enable this optionally if there is benefits.

extent_access_mode consumes only padding holes within the union and brings no additional memory overhead. The union is sized to 16 bytes in total, whereas blk, c_len and extent_access_mode together take up just 12 bytes.

Thanks
Yongpeng,

Quoted 33 lines

Thanks,

Quoted 29 lines
fs/f2fs/f2fs.h +2 -1
  		};
  		/* block age extent_cache */
  		struct {
@@ -4577,7 +4588,8 @@ bool f2fs_lookup_read_extent_cache_block(struct inode *inode, pgoff_t index,
  			block_t *blkaddr);
  void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
  void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
-			pgoff_t fofs, block_t blkaddr, unsigned int len);
+				pgoff_t fofs, block_t blkaddr, unsigned int len,
+				enum extent_access_mode access_mode);
  unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
  			int nr_shrink);
 
fs/f2fs/file.c +2 -1
index 633e9ade654f..a3a5d499eadf 100644
@@ -733,7 +733,8 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
  		 */
  		fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_folio),
  							dn->inode) + ofs;
-		f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
+		f2fs_update_read_extent_cache_range(dn, fofs, 0, len,
+							EX_ACCESS_TRUNCATE);
  		f2fs_update_age_extent_cache_range(dn, fofs, len);
  		dec_valid_block_count(sbi, dn->inode, nr_free);
  	}
@@ -1672,7 +1673,8 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
 
Quoted 15 lines

fs/f2fs/file.c static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start, · line 1680

Quoted 8 lines
fs/f2fs/file.c +2 -1
  	if (index > start) {
  		f2fs_update_read_extent_cache_range(dn, start, 0,
-							index - start);
+							index - start,
+							EX_ACCESS_TRUNCATE);
  		f2fs_update_age_extent_cache_range(dn, start, index - start);
  	}
 

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#22 lore raw

Re: [f2fs-dev] [PATCH RESEND 4/5] f2fs: add extent_access_mode to track extent cache access patterns

Quoted 4 lines · Yongpeng Yang
Quoted 3 lines · On 6/15/26 8:05 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 2 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Introduce enum extent_access_mode to classify how each extent node is accessed or created (READ, WRITE, PRECACHE, TRUNCATE, LARGEST). This metadata optimize LRU eviction decisions:

Can you please give some numbers for this change?

This is a qualitative analysis. This patch aims to prioritize shrinking extents that are not read-hit and the largest extent.

I think it needs to give some numbers to show benefits.

Quoted 165 lines
Quoted 156 lines
Quoted 156 lines

1. Extents only accessed as the largest extent (never read-hit) are deprioritized in the LRU list since reads can still use the largest extent directly.

2. Sparse single-block write extents that were never merged are moved to the head of LRU for earlier reclaim, preserving extents with better continuity and higher read-hit probability.

---
  fs/f2fs/data.c         |  4 ++-- fs/f2fs/extent_cache.c | 29 ++++++++++++++++++++++++++++- fs/f2fs/f2fs.h         | 14 +++++++++++++- fs/f2fs/file.c         |  6 ++++--

  4 files changed, 47 insertions(+), 6 deletions(-)
fs/f2fs/data.c +2 -2
index 9c6440a7db0e..2d38135005fe 100644
@@ -1873,7 +1873,7 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
               f2fs_update_read_extent_cache_range(&dn,
                   start_pgofs, map->m_pblk + ofs,
-                map->m_len - ofs);
+                map->m_len - ofs, EX_ACCESS_PRECACHE);
           }
       }
@@ -1919,7 +1919,7 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag)
               if (map->m_len > ofs)
                   f2fs_update_read_extent_cache_range(&dn,
                       start_pgofs, map->m_pblk + ofs,
-                    map->m_len - ofs);
+                    map->m_len - ofs, EX_ACCESS_PRECACHE);
           }
           if (map->m_next_extent)
               *map->m_next_extent = is_hole ? pgofs + 1 : pgofs;
fs/f2fs/extent_cache.c +17 -0
index 82d84c4e98b2..e141ffb64e5f 100644
@@ -142,6 +142,7 @@ static void __try_update_largest_extent(struct extent_tree *et,
       if (en->ei.len <= et->largest.len)
           return;
+    en->ei.last_access_mode = EX_ACCESS_LARGEST;
       et->largest = en->ei;
       et->largest_updated = true;
   }
@@ -518,6 +519,7 @@ static bool __lookup_extent_tree(struct inode *inode, pgoff_t pgofs,
           stat_inc_rbtree_node_hit(sbi, type);
       *ei = en->ei;
+    en->ei.last_access_mode = EX_ACCESS_READ;
       spin_lock(&eti->extent_lock);
       if (!list_empty(&en->list)) {
           list_move_tail(&en->list, &eti->extent_list);
@@ -624,6 +626,21 @@ static struct extent_node *__insert_extent_tree(struct f2fs_sb_info *sbi,
       /* update in global extent list */
       spin_lock(&eti->extent_lock);
+    /*
+     * 1. For the largest extent, if subsequent writes are not merged into
+     * it, the write path will most likely not use the largest extent_node,
+     * while read requests can still access the mapping through the largest
+     * extent.
+     *
+     * 2. For sparse writes, if the extent length is 1 and no extent merging
+     * occurs, this extent should be reclaimed with higher priority to avoid
+     * evicting extents with better continuity and higher read-hit.
+     */
+    if (et->type == EX_READ && et->cached_en &&
+        (et->cached_en->ei.last_access_mode == EX_ACCESS_LARGEST ||
+         (et->cached_en->ei.len == 1 &&
+          et->cached_en->ei.last_access_mode == EX_ACCESS_WRITE)))
+        list_move(&et->cached_en->list, &eti->extent_list);
       list_add_tail(&en->list, &eti->extent_list);
       et->cached_en = en;
       spin_unlock(&eti->extent_lock);
@@ -747,6 +764,8 @@ static void __update_extent_tree_range(struct inode *inode,

          if (fofs > dei.fofs && (type != EX_READ || fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN)) { en->ei.len = fofs - en->ei.fofs;
+            if (type == EX_READ)
+                en->ei.last_access_mode = EX_ACCESS_TRUNCATE; prev_en = en;
              parts = 1;
          }

fs/f2fs/extent_cache.c +5 -0
@@ -761,6 +780,8 @@ static void __update_extent_tree_range(struct inode *inode,
                       end - dei.fofs + dei.blk, false,
                       dei.age, dei.last_blocks,
                       type);
+                if (type == EX_READ)
+                    ei.last_access_mode = EX_ACCESS_TRUNCATE;
                   en1 = __insert_extent_tree(sbi, et, &ei,
                               NULL, NULL, true);
                   next_en = en1;
@@ -770,6 +791,8 @@ static void __update_extent_tree_range(struct inode *inode,
                       en->ei.blk + (end - dei.fofs), true,
                       dei.age, dei.last_blocks,
                       type);
+                if (type == EX_READ)
+                    en->ei.last_access_mode = EX_ACCESS_TRUNCATE;
                   next_en = en;
               }
               parts++;
@@ -808,6 +831,7 @@ static void __update_extent_tree_range(struct inode *inode,
       if (tei->blk) {
           __set_extent_info(&ei, fofs, len, tei->blk, false,
                     0, 0, EX_READ);
+        ei.last_access_mode = tei->last_access_mode;
           if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
               __insert_extent_tree(sbi, et, &ei,
                       insert_p, insert_parent, leftmost);
@@ -978,6 +1002,7 @@ static void __update_extent_cache(struct dnode_of_data *dn, enum extent_type typ
               ei.blk = NULL_ADDR;

          else
              ei.blk = dn->data_blkaddr;
+        ei.last_access_mode = EX_ACCESS_WRITE;
      } else if (type == EX_BLOCK_AGE) {
          if (__get_new_block_age(dn->inode, &ei, dn->data_blkaddr)) return;

fs/f2fs/extent_cache.c +3 -1
@@ -1091,12 +1116,14 @@ void f2fs_update_read_extent_cache(struct dnode_of_data *dn)
   }
   void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
-                pgoff_t fofs, block_t blkaddr, unsigned int len)
+                pgoff_t fofs, block_t blkaddr, unsigned int len,
+                enum extent_access_mode access_mode)
   {
       struct extent_info ei = {
           .fofs = fofs,
           .len = len,
           .blk = blkaddr,
+        .last_access_mode = access_mode,
       };
       if (!__may_extent_tree(dn->inode, EX_READ))
fs/f2fs/f2fs.h +9 -0
index fffb516b78f4..1588b64d04a3 100644
@@ -763,6 +763,15 @@ enum extent_type {
       NR_EXTENT_CACHES,
   };
+/* extent acces mode for cache hit or extent add */
+enum extent_access_mode {
+    EX_ACCESS_READ,
+    EX_ACCESS_WRITE,
+    EX_ACCESS_PRECACHE,
+    EX_ACCESS_TRUNCATE,
+    EX_ACCESS_LARGEST,
+};
+
   /*
    * Reserved value to mark invalid age extents, hence valid block range
    * from 0 to ULLONG_MAX-1
@@ -781,6 +790,8 @@ struct extent_info {
Quoted 8 lines

fs/f2fs/f2fs.h struct extent_info { · line 794

Quoted 5 lines
fs/f2fs/f2fs.h +2 -0
               /* physical extent length of compressed blocks */
               unsigned int c_len;
   #endif
+            /* record last access mode */
+            enum extent_access_mode last_access_mode;

As we know, memory is expensive, :P, I'd like to know if we can enable this optionally if there is benefits.

Quoted 4 lines

extent_access_mode consumes only padding holes within the union and brings no additional memory overhead. The union is sized to 16 bytes in total, whereas blk, c_len and extent_access_mode together take up just 12 bytes.

Well, seems fine now, but I just notice block age extent costs more space than read extent.

Thanks,

Quoted 51 lines

Thanks
Yongpeng,

Quoted 45 lines

Thanks,

Quoted 27 lines
fs/f2fs/f2fs.h +2 -1
           };
           /* block age extent_cache */
           struct {
@@ -4577,7 +4588,8 @@ bool f2fs_lookup_read_extent_cache_block(struct inode *inode, pgoff_t index,
               block_t *blkaddr);
   void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
   void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
-            pgoff_t fofs, block_t blkaddr, unsigned int len);
+                pgoff_t fofs, block_t blkaddr, unsigned int len,
+                enum extent_access_mode access_mode);
   unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
               int nr_shrink);
fs/f2fs/file.c +2 -1
index 633e9ade654f..a3a5d499eadf 100644
@@ -733,7 +733,8 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
            */
           fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_folio),
                               dn->inode) + ofs;
-        f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
+        f2fs_update_read_extent_cache_range(dn, fofs, 0, len,
+                            EX_ACCESS_TRUNCATE);
           f2fs_update_age_extent_cache_range(dn, fofs, len);
           dec_valid_block_count(sbi, dn->inode, nr_free);
       }

fs/f2fs/file.c static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start, · line 1678

Quoted 7 lines
fs/f2fs/file.c +2 -1
@@ -1672,7 +1673,8 @@ static int f2fs_do_zero_range(struct dnode_of_data *dn, pgoff_t start,
       if (index > start) {
           f2fs_update_read_extent_cache_range(dn, start, 0,
-                            index - start);
+                            index - start,
+                            EX_ACCESS_TRUNCATE);
           f2fs_update_age_extent_cache_range(dn, start, index - start);
       }

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#23 lore raw

Re: [f2fs-dev] [PATCH RESEND 4/5] f2fs: add extent_access_mode to track extent cache access patterns

Quoted 9 lines · On 6/20/26 2:58 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 8 lines · Yongpeng Yang
Quoted 7 lines · On 6/15/26 8:05 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 5 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Introduce enum extent_access_mode to classify how each extent node is accessed or created (READ, WRITE, PRECACHE, TRUNCATE, LARGEST). This metadata optimize LRU eviction decisions:

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Can you please give some numbers for this change?

This is a qualitative analysis. This patch aims to prioritize shrinking extents that are not read-hit and the largest extent.

I think it needs to give some numbers to show benefits.

OK, I'll try to profile and show the benefits.

Thanks
Yongpeng,

Quoted 249 lines
Quoted 182 lines
Quoted 173 lines
Quoted 173 lines

1. Extents only accessed as the largest extent (never read-hit) are deprioritized in the LRU list since reads can still use the largest extent directly.

2. Sparse single-block write extents that were never merged are moved to the head of LRU for earlier reclaim, preserving extents with better continuity and higher read-hit probability.

---
  fs/f2fs/data.c         |  4 ++-- fs/f2fs/extent_cache.c | 29 ++++++++++++++++++++++++++++- fs/f2fs/f2fs.h         | 14 +++++++++++++- fs/f2fs/file.c         |  6 ++++--

  4 files changed, 47 insertions(+), 6 deletions(-)
fs/f2fs/data.c +2 -2
index 9c6440a7db0e..2d38135005fe 100644
@@ -1873,7 +1873,7 @@ int f2fs_map_blocks(struct inode *inode, 
 struct f2fs_map_blocks *map, int flag)
               f2fs_update_read_extent_cache_range(&dn,
                   start_pgofs, map->m_pblk + ofs,
-                map->m_len - ofs);
+                map->m_len - ofs, EX_ACCESS_PRECACHE);
           }
       }
@@ -1919,7 +1919,7 @@ int f2fs_map_blocks(struct inode *inode, 
 struct f2fs_map_blocks *map, int flag)
               if (map->m_len > ofs)
                   f2fs_update_read_extent_cache_range(&dn,
                       start_pgofs, map->m_pblk + ofs,
-                    map->m_len - ofs);
+                    map->m_len - ofs, EX_ACCESS_PRECACHE);
           }
           if (map->m_next_extent)

              *map->m_next_extent = is_hole ? pgofs + 1 : pgofs;

fs/f2fs/extent_cache.c +4 -0
index 82d84c4e98b2..e141ffb64e5f 100644
@@ -142,6 +142,7 @@ static void __try_update_largest_extent(struct 
 extent_tree *et,
       if (en->ei.len <= et->largest.len)
           return;
+    en->ei.last_access_mode = EX_ACCESS_LARGEST;
       et->largest = en->ei;
       et->largest_updated = true;
   }
@@ -518,6 +519,7 @@ static bool __lookup_extent_tree(struct inode 
 *inode, pgoff_t pgofs,
           stat_inc_rbtree_node_hit(sbi, type);
       *ei = en->ei;
+    en->ei.last_access_mode = EX_ACCESS_READ;
       spin_lock(&eti->extent_lock);
       if (!list_empty(&en->list)) {
           list_move_tail(&en->list, &eti->extent_list);
@@ -624,6 +626,21 @@ static struct extent_node 
 *__insert_extent_tree(struct f2fs_sb_info *sbi,
       /* update in global extent list */
       spin_lock(&eti->extent_lock);
+    /*
+     * 1. For the largest extent, if subsequent writes are not 

merged into
+     * it, the write path will most likely not use the largest extent_node,
+     * while read requests can still access the mapping through the largest
+     * extent.
+     *
+     * 2. For sparse writes, if the extent length is 1 and no extent merging
+     * occurs, this extent should be reclaimed with higher priority to avoid
+     * evicting extents with better continuity and higher read-hit. +     */
+    if (et->type == EX_READ && et->cached_en &&
+        (et->cached_en->ei.last_access_mode == EX_ACCESS_LARGEST || +         (et->cached_en->ei.len == 1 &&
+          et->cached_en->ei.last_access_mode == EX_ACCESS_WRITE))) +        list_move(&et->cached_en->list, &eti->extent_list); list_add_tail(&en->list, &eti->extent_list); et->cached_en = en;
      spin_unlock(&eti->extent_lock);

fs/f2fs/extent_cache.c +0 -0
@@ -747,6 +764,8 @@ static void __update_extent_tree_range(struct 
 inode *inode,

          if (fofs > dei.fofs && (type != EX_READ || fofs - dei.fofs >= F2FS_MIN_EXTENT_LEN)) { en->ei.len = fofs - en->ei.fofs;
+            if (type == EX_READ)
+                en->ei.last_access_mode = EX_ACCESS_TRUNCATE; prev_en = en;
              parts = 1;
          }

fs/f2fs/extent_cache.c +2 -0
@@ -761,6 +780,8 @@ static void __update_extent_tree_range(struct 
 inode *inode,
                       end - dei.fofs + dei.blk, false,
                       dei.age, dei.last_blocks,
                       type);
+                if (type == EX_READ)
+                    ei.last_access_mode = EX_ACCESS_TRUNCATE;
                   en1 = __insert_extent_tree(sbi, et, &ei,
                               NULL, NULL, true);

                  next_en = en1;

fs/f2fs/extent_cache.c +2 -0
@@ -770,6 +791,8 @@ static void __update_extent_tree_range(struct 
 inode *inode,
                       en->ei.blk + (end - dei.fofs), true,
                       dei.age, dei.last_blocks,
                       type);
+                if (type == EX_READ)
+                    en->ei.last_access_mode = EX_ACCESS_TRUNCATE;
                   next_en = en;
               }

              parts++;

fs/f2fs/extent_cache.c +1 -0
@@ -808,6 +831,7 @@ static void __update_extent_tree_range(struct 
 inode *inode,
       if (tei->blk) {
           __set_extent_info(&ei, fofs, len, tei->blk, false,
                     0, 0, EX_READ);
+        ei.last_access_mode = tei->last_access_mode;
           if (!__try_merge_extent_node(sbi, et, &ei, prev_en, next_en))
               __insert_extent_tree(sbi, et, &ei,

                      insert_p, insert_parent, leftmost);

fs/f2fs/extent_cache.c +0 -0
@@ -978,6 +1002,7 @@ static void __update_extent_cache(struct 

dnode_of_data *dn, enum extent_type typ
              ei.blk = NULL_ADDR;
          else
              ei.blk = dn->data_blkaddr;
+        ei.last_access_mode = EX_ACCESS_WRITE;
      } else if (type == EX_BLOCK_AGE) {
          if (__get_new_block_age(dn->inode, &ei, dn->data_blkaddr)) return;

fs/f2fs/extent_cache.c +3 -1
@@ -1091,12 +1116,14 @@ void f2fs_update_read_extent_cache(struct 
 dnode_of_data *dn)
   }
   void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
-                pgoff_t fofs, block_t blkaddr, unsigned int len)
+                pgoff_t fofs, block_t blkaddr, unsigned int len,
+                enum extent_access_mode access_mode)
   {
       struct extent_info ei = {
           .fofs = fofs,
           .len = len,
           .blk = blkaddr,
+        .last_access_mode = access_mode,
       };
       if (!__may_extent_tree(dn->inode, EX_READ))
fs/f2fs/f2fs.h +9 -0
index fffb516b78f4..1588b64d04a3 100644
@@ -763,6 +763,15 @@ enum extent_type {
       NR_EXTENT_CACHES,
   };
+/* extent acces mode for cache hit or extent add */
+enum extent_access_mode {
+    EX_ACCESS_READ,
+    EX_ACCESS_WRITE,
+    EX_ACCESS_PRECACHE,
+    EX_ACCESS_TRUNCATE,
+    EX_ACCESS_LARGEST,
+};
+
   /*
    * Reserved value to mark invalid age extents, hence valid block range
    * from 0 to ULLONG_MAX-1
@@ -781,6 +790,8 @@ struct extent_info {
Quoted 8 lines

fs/f2fs/f2fs.h struct extent_info { · line 794

Quoted 5 lines
fs/f2fs/f2fs.h +2 -0
               /* physical extent length of compressed blocks */
               unsigned int c_len;
   #endif
+            /* record last access mode */
+            enum extent_access_mode last_access_mode;

As we know, memory is expensive, :P, I'd like to know if we can enable this optionally if there is benefits.

Quoted 4 lines

extent_access_mode consumes only padding holes within the union and brings no additional memory overhead. The union is sized to 16 bytes in total, whereas blk, c_len and extent_access_mode together take up just 12 bytes.

Well, seems fine now, but I just notice block age extent costs more space than read extent.

Thanks,

Quoted 54 lines

Thanks
Yongpeng,

Quoted 48 lines

Thanks,

Quoted 29 lines
fs/f2fs/f2fs.h +2 -1
           };
           /* block age extent_cache */
           struct {
@@ -4577,7 +4588,8 @@ bool 
 f2fs_lookup_read_extent_cache_block(struct inode *inode, pgoff_t index,
               block_t *blkaddr);
   void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
   void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
-            pgoff_t fofs, block_t blkaddr, unsigned int len);
+                pgoff_t fofs, block_t blkaddr, unsigned int len,
+                enum extent_access_mode access_mode);
   unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
               int nr_shrink);
fs/f2fs/file.c +2 -1
index 633e9ade654f..a3a5d499eadf 100644
@@ -733,7 +733,8 @@ void f2fs_truncate_data_blocks_range(struct 
 dnode_of_data *dn, int count)
            */
           fofs = f2fs_start_bidx_of_node(ofs_of_node(dn->node_folio),
                               dn->inode) + ofs;
-        f2fs_update_read_extent_cache_range(dn, fofs, 0, len);
+        f2fs_update_read_extent_cache_range(dn, fofs, 0, len,
+                            EX_ACCESS_TRUNCATE);
           f2fs_update_age_extent_cache_range(dn, fofs, len);
           dec_valid_block_count(sbi, dn->inode, nr_free);

      }

fs/f2fs/file.c static int f2fs_do_zero_range(struct · line 1679

Quoted 8 lines
fs/f2fs/file.c +2 -1
@@ -1672,7 +1673,8 @@ static int f2fs_do_zero_range(struct 
 dnode_of_data *dn, pgoff_t start,
       if (index > start) {
           f2fs_update_read_extent_cache_range(dn, start, 0,
-                            index - start);
+                            index - start,
+                            EX_ACCESS_TRUNCATE);
           f2fs_update_age_extent_cache_range(dn, start, index - start);
       }

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#24 lore raw

PATCH RESEND 5/5 f2fs: add ioctl to export read extent cache to userspace for debug

In reply to Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Add F2FS_IOC_GET_READ_CACHE_EXTENTS ioctl that allows userspace to retrieve all cached read extents for a given file. This uses a two-call pattern similar to fiemap: the first call with ext_count=0 queries the node_count, and the second call fetches the actual extent entries.

---
 fs/f2fs/extent_cache.c    | 70 +++++++++++++++++++++++++++++++++++++++
 fs/f2fs/f2fs.h            |  3 ++
 fs/f2fs/file.c            | 11 ++++++
 include/uapi/linux/f2fs.h | 21 ++++++++++++
 4 files changed, 105 insertions(+)
fs/f2fs/extent_cache.c +70 -0
index e141ffb64e5f..0c10d5639d68 100644
@@ -14,6 +14,7 @@
 
 #include <linux/fs.h>
 #include <linux/f2fs_fs.h>
+#include <uapi/linux/f2fs.h>
 
 #include "f2fs.h"
 #include "node.h"
@@ -1267,6 +1268,75 @@ static void __init_extent_tree_info(struct extent_tree_info *eti)
 	atomic_set(&eti->total_ext_node, 0);
 }
 
+int f2fs_get_read_cache_extents(struct inode *inode,
+			struct f2fs_read_cache_extent __user *uarg)
+{
+	struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
+	struct f2fs_cache_extent_info *kbuf = NULL;
+	struct f2fs_cache_extent_info largest = {};
+	struct rb_node *node;
+	struct extent_node *en;
+	unsigned int capacity, count = 0;
+	__u32 flags = 0;
+	int ret = 0;
+
+	if (get_user(capacity, &uarg->ext_count))
+		return -EFAULT;
+
+	if (is_inode_flag_set(inode, FI_NO_EXTENT))
+		flags |= F2FS_EXT_FL_NO_EXTENT;
+
+	if (!et || (flags & F2FS_EXT_FL_NO_EXTENT)) {
+		if (put_user(0U, &uarg->ext_count) ||
+		    put_user(flags, &uarg->flags) ||
+		    put_user(0U, &uarg->node_count))
+			return -EFAULT;
+		return 0;
+	}
+
+	if (capacity) {
+		kbuf = f2fs_kvmalloc(F2FS_I_SB(inode), capacity * sizeof(*kbuf), GFP_KERNEL);
+		if (!kbuf)
+			return -ENOMEM;
+	}
+
+	read_lock(&et->lock);
+
+	largest.fofs = et->largest.fofs;
+	largest.blk = et->largest.blk;
+	largest.len = et->largest.len;
+	largest.last_access_mode = et->largest.last_access_mode;
+
+	for (node = rb_first_cached(&et->root); node; node = rb_next(node)) {
+		if (count >= capacity)
+			break;
+		en = rb_entry(node, struct extent_node, rb_node);
+
+		kbuf[count].fofs = en->ei.fofs;
+		kbuf[count].blk = en->ei.blk;
+		kbuf[count].len = en->ei.len;
+		kbuf[count].last_access_mode = en->ei.last_access_mode;
+		count++;
+	}
+
+	read_unlock(&et->lock);
+
+	if (count && copy_to_user(uarg->extents, kbuf,
+				  count * sizeof(*kbuf))) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	if (put_user(count, &uarg->ext_count) ||
+	    put_user(flags, &uarg->flags) ||
+	    put_user((u32)atomic_read(&et->node_cnt), &uarg->node_count) ||
+	    copy_to_user(&uarg->largest, &largest, sizeof(largest)))
+		ret = -EFAULT;
+out:
+	kvfree(kbuf);
+	return ret;
+}
+
 void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi)
 {
 	__init_extent_tree_info(&sbi->extent_tree[EX_READ]);
fs/f2fs/f2fs.h +3 -0
index 1588b64d04a3..69641fc31c51 100644
@@ -26,6 +26,7 @@
 #include <linux/part_stat.h>
 #include <linux/rw_hint.h>
 
+#include <uapi/linux/f2fs.h>
 #include <linux/fscrypt.h>
 #include <linux/fsverity.h>
 
@@ -4590,6 +4591,8 @@ void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
 void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
 				pgoff_t fofs, block_t blkaddr, unsigned int len,
 				enum extent_access_mode access_mode);
+int f2fs_get_read_cache_extents(struct inode *inode,
+			struct f2fs_read_cache_extent __user *uarg);
 unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
 			int nr_shrink);
 
fs/f2fs/file.c +11 -0
index a3a5d499eadf..66ec9927d667 100644
@@ -3672,6 +3672,14 @@ static int f2fs_ioc_precache_extents(struct file *filp)
 	return f2fs_precache_extents(file_inode(filp));
 }
 
+static int f2fs_ioc_get_read_cache_extents(struct file *filp, unsigned long arg)
+{
+	struct inode *inode = file_inode(filp);
+
+	return f2fs_get_read_cache_extents(inode,
+			(struct f2fs_read_cache_extent __user *)arg);
+}
+
 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
@@ -4744,6 +4752,8 @@ static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 		return f2fs_ioc_get_dev_alias_file(filp, arg);
 	case F2FS_IOC_IO_PRIO:
 		return f2fs_ioc_io_prio(filp, arg);
+	case F2FS_IOC_GET_READ_CACHE_EXTENTS:
+		return f2fs_ioc_get_read_cache_extents(filp, arg);
 	default:
 		return -ENOTTY;
 	}
@@ -5506,6 +5516,7 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	case F2FS_IOC_COMPRESS_FILE:
 	case F2FS_IOC_GET_DEV_ALIAS_FILE:
 	case F2FS_IOC_IO_PRIO:
+	case F2FS_IOC_GET_READ_CACHE_EXTENTS:
 		break;
 	default:
 		return -ENOIOCTLCMD;
include/uapi/linux/f2fs.h +21 -0
index 795e26258355..6ff9003bc030 100644
@@ -45,6 +45,8 @@
 #define F2FS_IOC_START_ATOMIC_REPLACE	_IO(F2FS_IOCTL_MAGIC, 25)
 #define F2FS_IOC_GET_DEV_ALIAS_FILE	_IOR(F2FS_IOCTL_MAGIC, 26, __u32)
 #define F2FS_IOC_IO_PRIO		_IOW(F2FS_IOCTL_MAGIC, 27, __u32)
+#define F2FS_IOC_GET_READ_CACHE_EXTENTS	_IOWR(F2FS_IOCTL_MAGIC, 28,	\
+						struct f2fs_read_cache_extent)
 
 /*
  * should be same as XFS_IOC_GOINGDOWN.
@@ -104,4 +106,23 @@ struct f2fs_comp_option {
 	__u8 log_cluster_size;
 };
 
+struct f2fs_cache_extent_info {
+	__u32 fofs;		/* start file offset in blocks */
+	__u32 blk;		/* start block address */
+	__u32 len;		/* length in blocks */
+	__u32 last_access_mode; /* last access mode of extent_node */
+};
+
+/* flags for f2fs_read_cache_extent */
+#define F2FS_EXT_FL_NO_EXTENT	0x1	/* extent cache disabled for this inode */
+
+struct f2fs_read_cache_extent {
+	__u32 ext_count;	/* in: array capacity; out: mapped extent count */
+	__u32 flags;		/* out: status flags */
+	__u32 node_count;	/* out: total extent nodes in tree */
+	__u32 reserved;
+	struct f2fs_cache_extent_info largest;		/* out: largest extent */
+	struct f2fs_cache_extent_info extents[];	/* out: extent array */
+};
+
 #endif /* _UAPI_LINUX_F2FS_H */
Signature · 8 lines
2.43.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#25 lore raw

Re: [f2fs-dev] [PATCH RESEND 5/5] f2fs: add ioctl to export read extent cache to userspace for debug

Quoted 6 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Add F2FS_IOC_GET_READ_CACHE_EXTENTS ioctl that allows userspace to retrieve all cached read extents for a given file. This uses a two-call pattern similar to fiemap: the first call with ext_count=0 queries the node_count, and the second call fetches the actual extent entries.

It looks a little bit heavy to maintain a debug purpose ioctl interface.

Maybe set ino via sysfs and dump extent cache via procfs? only enabled if F2FS_CHECK_FS=y?

Jaegeuk, do you have any suggestion?

Thanks,

Quoted 181 lines
---
 fs/f2fs/extent_cache.c    | 70 +++++++++++++++++++++++++++++++++++++++
 fs/f2fs/f2fs.h            |  3 ++
 fs/f2fs/file.c            | 11 ++++++
 include/uapi/linux/f2fs.h | 21 ++++++++++++
 4 files changed, 105 insertions(+)
fs/f2fs/extent_cache.c +70 -0
index e141ffb64e5f..0c10d5639d68 100644
@@ -14,6 +14,7 @@
 
 #include <linux/fs.h>
 #include <linux/f2fs_fs.h>
+#include <uapi/linux/f2fs.h>
 
 #include "f2fs.h"
 #include "node.h"
@@ -1267,6 +1268,75 @@ static void __init_extent_tree_info(struct extent_tree_info *eti)
 	atomic_set(&eti->total_ext_node, 0);
 }
 
+int f2fs_get_read_cache_extents(struct inode *inode,
+			struct f2fs_read_cache_extent __user *uarg)
+{
+	struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
+	struct f2fs_cache_extent_info *kbuf = NULL;
+	struct f2fs_cache_extent_info largest = {};
+	struct rb_node *node;
+	struct extent_node *en;
+	unsigned int capacity, count = 0;
+	__u32 flags = 0;
+	int ret = 0;
+
+	if (get_user(capacity, &uarg->ext_count))
+		return -EFAULT;
+
+	if (is_inode_flag_set(inode, FI_NO_EXTENT))
+		flags |= F2FS_EXT_FL_NO_EXTENT;
+
+	if (!et || (flags & F2FS_EXT_FL_NO_EXTENT)) {
+		if (put_user(0U, &uarg->ext_count) ||
+		    put_user(flags, &uarg->flags) ||
+		    put_user(0U, &uarg->node_count))
+			return -EFAULT;
+		return 0;
+	}
+
+	if (capacity) {
+		kbuf = f2fs_kvmalloc(F2FS_I_SB(inode), capacity * sizeof(*kbuf), GFP_KERNEL);
+		if (!kbuf)
+			return -ENOMEM;
+	}
+
+	read_lock(&et->lock);
+
+	largest.fofs = et->largest.fofs;
+	largest.blk = et->largest.blk;
+	largest.len = et->largest.len;
+	largest.last_access_mode = et->largest.last_access_mode;
+
+	for (node = rb_first_cached(&et->root); node; node = rb_next(node)) {
+		if (count >= capacity)
+			break;
+		en = rb_entry(node, struct extent_node, rb_node);
+
+		kbuf[count].fofs = en->ei.fofs;
+		kbuf[count].blk = en->ei.blk;
+		kbuf[count].len = en->ei.len;
+		kbuf[count].last_access_mode = en->ei.last_access_mode;
+		count++;
+	}
+
+	read_unlock(&et->lock);
+
+	if (count && copy_to_user(uarg->extents, kbuf,
+				  count * sizeof(*kbuf))) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	if (put_user(count, &uarg->ext_count) ||
+	    put_user(flags, &uarg->flags) ||
+	    put_user((u32)atomic_read(&et->node_cnt), &uarg->node_count) ||
+	    copy_to_user(&uarg->largest, &largest, sizeof(largest)))
+		ret = -EFAULT;
+out:
+	kvfree(kbuf);
+	return ret;
+}
+
 void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi)
 {
 	__init_extent_tree_info(&sbi->extent_tree[EX_READ]);
fs/f2fs/f2fs.h +3 -0
index 1588b64d04a3..69641fc31c51 100644
@@ -26,6 +26,7 @@
 #include <linux/part_stat.h>
 #include <linux/rw_hint.h>
 
+#include <uapi/linux/f2fs.h>
 #include <linux/fscrypt.h>
 #include <linux/fsverity.h>
 
@@ -4590,6 +4591,8 @@ void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
 void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
 				pgoff_t fofs, block_t blkaddr, unsigned int len,
 				enum extent_access_mode access_mode);
+int f2fs_get_read_cache_extents(struct inode *inode,
+			struct f2fs_read_cache_extent __user *uarg);
 unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
 			int nr_shrink);
 
fs/f2fs/file.c +11 -0
index a3a5d499eadf..66ec9927d667 100644
@@ -3672,6 +3672,14 @@ static int f2fs_ioc_precache_extents(struct file *filp)
 	return f2fs_precache_extents(file_inode(filp));
 }
 
+static int f2fs_ioc_get_read_cache_extents(struct file *filp, unsigned long arg)
+{
+	struct inode *inode = file_inode(filp);
+
+	return f2fs_get_read_cache_extents(inode,
+			(struct f2fs_read_cache_extent __user *)arg);
+}
+
 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
@@ -4744,6 +4752,8 @@ static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 		return f2fs_ioc_get_dev_alias_file(filp, arg);
 	case F2FS_IOC_IO_PRIO:
 		return f2fs_ioc_io_prio(filp, arg);
+	case F2FS_IOC_GET_READ_CACHE_EXTENTS:
+		return f2fs_ioc_get_read_cache_extents(filp, arg);
 	default:
 		return -ENOTTY;
 	}
@@ -5506,6 +5516,7 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	case F2FS_IOC_COMPRESS_FILE:
 	case F2FS_IOC_GET_DEV_ALIAS_FILE:
 	case F2FS_IOC_IO_PRIO:
+	case F2FS_IOC_GET_READ_CACHE_EXTENTS:
 		break;
 	default:
 		return -ENOIOCTLCMD;
include/uapi/linux/f2fs.h +12 -0
index 795e26258355..6ff9003bc030 100644
@@ -45,6 +45,8 @@
 #define F2FS_IOC_START_ATOMIC_REPLACE	_IO(F2FS_IOCTL_MAGIC, 25)
 #define F2FS_IOC_GET_DEV_ALIAS_FILE	_IOR(F2FS_IOCTL_MAGIC, 26, __u32)
 #define F2FS_IOC_IO_PRIO		_IOW(F2FS_IOCTL_MAGIC, 27, __u32)
+#define F2FS_IOC_GET_READ_CACHE_EXTENTS	_IOWR(F2FS_IOCTL_MAGIC, 28,	\
+						struct f2fs_read_cache_extent)
 
 /*
  * should be same as XFS_IOC_GOINGDOWN.
@@ -104,4 +106,23 @@ struct f2fs_comp_option {
 	__u8 log_cluster_size;
 };
 
+struct f2fs_cache_extent_info {
+	__u32 fofs;		/* start file offset in blocks */
+	__u32 blk;		/* start block address */
+	__u32 len;		/* length in blocks */
+	__u32 last_access_mode; /* last access mode of extent_node */
+};
+
+/* flags for f2fs_read_cache_extent */
+#define F2FS_EXT_FL_NO_EXTENT	0x1	/* extent cache disabled for this inode */
+

include/uapi/linux/f2fs.h struct f2fs_comp_option { · line 128

Quoted 10 lines
include/uapi/linux/f2fs.h +9 -0
+struct f2fs_read_cache_extent {
+	__u32 ext_count;	/* in: array capacity; out: mapped extent count */
+	__u32 flags;		/* out: status flags */
+	__u32 node_count;	/* out: total extent nodes in tree */
+	__u32 reserved;
+	struct f2fs_cache_extent_info largest;		/* out: largest extent */
+	struct f2fs_cache_extent_info extents[];	/* out: extent array */
+};
+
 #endif /* _UAPI_LINUX_F2FS_H */

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#26 lore raw

Re: [f2fs-dev] [PATCH RESEND 5/5] f2fs: add ioctl to export read extent cache to userspace for debug

Quoted 8 lines · On 06/15, Chao Yu via Linux-f2fs-devel wrote:
Quoted 6 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Add F2FS_IOC_GET_READ_CACHE_EXTENTS ioctl that allows userspace to retrieve all cached read extents for a given file. This uses a two-call pattern similar to fiemap: the first call with ext_count=0 queries the node_count, and the second call fetches the actual extent entries.

Quoted 6 lines

It looks a little bit heavy to maintain a debug purpose ioctl interface.

Maybe set ino via sysfs and dump extent cache via procfs? only enabled if F2FS_CHECK_FS=y?

Jaegeuk, do you have any suggestion?

Agreed, and even not sure we need sysfs or procfs. Can we dump the extents via fsck?

Quoted 193 lines

Thanks,

Quoted 190 lines
---
 fs/f2fs/extent_cache.c    | 70 +++++++++++++++++++++++++++++++++++++++
 fs/f2fs/f2fs.h            |  3 ++
 fs/f2fs/file.c            | 11 ++++++
 include/uapi/linux/f2fs.h | 21 ++++++++++++
 4 files changed, 105 insertions(+)
fs/f2fs/extent_cache.c +70 -0
index e141ffb64e5f..0c10d5639d68 100644
@@ -14,6 +14,7 @@
 
 #include <linux/fs.h>
 #include <linux/f2fs_fs.h>
+#include <uapi/linux/f2fs.h>
 
 #include "f2fs.h"
 #include "node.h"
@@ -1267,6 +1268,75 @@ static void __init_extent_tree_info(struct extent_tree_info *eti)
 	atomic_set(&eti->total_ext_node, 0);
 }
 
+int f2fs_get_read_cache_extents(struct inode *inode,
+			struct f2fs_read_cache_extent __user *uarg)
+{
+	struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
+	struct f2fs_cache_extent_info *kbuf = NULL;
+	struct f2fs_cache_extent_info largest = {};
+	struct rb_node *node;
+	struct extent_node *en;
+	unsigned int capacity, count = 0;
+	__u32 flags = 0;
+	int ret = 0;
+
+	if (get_user(capacity, &uarg->ext_count))
+		return -EFAULT;
+
+	if (is_inode_flag_set(inode, FI_NO_EXTENT))
+		flags |= F2FS_EXT_FL_NO_EXTENT;
+
+	if (!et || (flags & F2FS_EXT_FL_NO_EXTENT)) {
+		if (put_user(0U, &uarg->ext_count) ||
+		    put_user(flags, &uarg->flags) ||
+		    put_user(0U, &uarg->node_count))
+			return -EFAULT;
+		return 0;
+	}
+
+	if (capacity) {
+		kbuf = f2fs_kvmalloc(F2FS_I_SB(inode), capacity * sizeof(*kbuf), GFP_KERNEL);
+		if (!kbuf)
+			return -ENOMEM;
+	}
+
+	read_lock(&et->lock);
+
+	largest.fofs = et->largest.fofs;
+	largest.blk = et->largest.blk;
+	largest.len = et->largest.len;
+	largest.last_access_mode = et->largest.last_access_mode;
+
+	for (node = rb_first_cached(&et->root); node; node = rb_next(node)) {
+		if (count >= capacity)
+			break;
+		en = rb_entry(node, struct extent_node, rb_node);
+
+		kbuf[count].fofs = en->ei.fofs;
+		kbuf[count].blk = en->ei.blk;
+		kbuf[count].len = en->ei.len;
+		kbuf[count].last_access_mode = en->ei.last_access_mode;
+		count++;
+	}
+
+	read_unlock(&et->lock);
+
+	if (count && copy_to_user(uarg->extents, kbuf,
+				  count * sizeof(*kbuf))) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	if (put_user(count, &uarg->ext_count) ||
+	    put_user(flags, &uarg->flags) ||
+	    put_user((u32)atomic_read(&et->node_cnt), &uarg->node_count) ||
+	    copy_to_user(&uarg->largest, &largest, sizeof(largest)))
+		ret = -EFAULT;
+out:
+	kvfree(kbuf);
+	return ret;
+}
+
 void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi)
 {
 	__init_extent_tree_info(&sbi->extent_tree[EX_READ]);
fs/f2fs/f2fs.h +3 -0
index 1588b64d04a3..69641fc31c51 100644
@@ -26,6 +26,7 @@
 #include <linux/part_stat.h>
 #include <linux/rw_hint.h>
 
+#include <uapi/linux/f2fs.h>
 #include <linux/fscrypt.h>
 #include <linux/fsverity.h>
 
@@ -4590,6 +4591,8 @@ void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
 void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
 				pgoff_t fofs, block_t blkaddr, unsigned int len,
 				enum extent_access_mode access_mode);
+int f2fs_get_read_cache_extents(struct inode *inode,
+			struct f2fs_read_cache_extent __user *uarg);
 unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
 			int nr_shrink);
 
fs/f2fs/file.c +11 -0
index a3a5d499eadf..66ec9927d667 100644
@@ -3672,6 +3672,14 @@ static int f2fs_ioc_precache_extents(struct file *filp)
 	return f2fs_precache_extents(file_inode(filp));
 }
 
+static int f2fs_ioc_get_read_cache_extents(struct file *filp, unsigned long arg)
+{
+	struct inode *inode = file_inode(filp);
+
+	return f2fs_get_read_cache_extents(inode,
+			(struct f2fs_read_cache_extent __user *)arg);
+}
+
 static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
@@ -4744,6 +4752,8 @@ static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 		return f2fs_ioc_get_dev_alias_file(filp, arg);
 	case F2FS_IOC_IO_PRIO:
 		return f2fs_ioc_io_prio(filp, arg);
+	case F2FS_IOC_GET_READ_CACHE_EXTENTS:
+		return f2fs_ioc_get_read_cache_extents(filp, arg);
 	default:
 		return -ENOTTY;
 	}
@@ -5506,6 +5516,7 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	case F2FS_IOC_COMPRESS_FILE:
 	case F2FS_IOC_GET_DEV_ALIAS_FILE:
 	case F2FS_IOC_IO_PRIO:
+	case F2FS_IOC_GET_READ_CACHE_EXTENTS:
 		break;
 	default:
 		return -ENOIOCTLCMD;
include/uapi/linux/f2fs.h +21 -0
index 795e26258355..6ff9003bc030 100644
@@ -45,6 +45,8 @@
 #define F2FS_IOC_START_ATOMIC_REPLACE	_IO(F2FS_IOCTL_MAGIC, 25)
 #define F2FS_IOC_GET_DEV_ALIAS_FILE	_IOR(F2FS_IOCTL_MAGIC, 26, __u32)
 #define F2FS_IOC_IO_PRIO		_IOW(F2FS_IOCTL_MAGIC, 27, __u32)
+#define F2FS_IOC_GET_READ_CACHE_EXTENTS	_IOWR(F2FS_IOCTL_MAGIC, 28,	\
+						struct f2fs_read_cache_extent)
 
 /*
  * should be same as XFS_IOC_GOINGDOWN.
@@ -104,4 +106,23 @@ struct f2fs_comp_option {
 	__u8 log_cluster_size;
 };
 
+struct f2fs_cache_extent_info {
+	__u32 fofs;		/* start file offset in blocks */
+	__u32 blk;		/* start block address */
+	__u32 len;		/* length in blocks */
+	__u32 last_access_mode; /* last access mode of extent_node */
+};
+
+/* flags for f2fs_read_cache_extent */
+#define F2FS_EXT_FL_NO_EXTENT	0x1	/* extent cache disabled for this inode */
+
+struct f2fs_read_cache_extent {
+	__u32 ext_count;	/* in: array capacity; out: mapped extent count */
+	__u32 flags;		/* out: status flags */
+	__u32 node_count;	/* out: total extent nodes in tree */
+	__u32 reserved;
+	struct f2fs_cache_extent_info largest;		/* out: largest extent */
+	struct f2fs_cache_extent_info extents[];	/* out: extent array */
+};
+
Quoted 8 lines

include/uapi/linux/f2fs.h struct f2fs_comp_option { · line 128

Quoted 1 line
include/uapi/linux/f2fs.h +0 -0
 #endif /* _UAPI_LINUX_F2FS_H */

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#27 lore raw

Re: [f2fs-dev] [PATCH RESEND 5/5] f2fs: add ioctl to export read extent cache to userspace for debug

Quoted 11 lines · On 6/15/26 11:51 PM, Jaegeuk Kim via Linux-f2fs-devel wrote:
Quoted 10 lines · On 06/15, Chao Yu via Linux-f2fs-devel wrote:
Quoted 6 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Add F2FS_IOC_GET_READ_CACHE_EXTENTS ioctl that allows userspace to retrieve all cached read extents for a given file. This uses a two-call pattern similar to fiemap: the first call with ext_count=0 queries the node_count, and the second call fetches the actual extent entries.

It looks a little bit heavy to maintain a debug purpose ioctl interface.

Quoted 7 lines
Quoted 4 lines

Maybe set ino via sysfs and dump extent cache via procfs? only enabled if F2FS_CHECK_FS=y?

Jaegeuk, do you have any suggestion?

Agreed, and even not sure we need sysfs or procfs. Can we dump the extents via fsck?

It is indeed quite heavy. Our primary goal here is debugging inode extent trees, and fsck cannot meet this requirement. If we rely on procfs or sysfs interfaces, we would have to open dedicated files within sysfs.c, which feels like an inappropriate approach.

Thanks
Yongpeng,

Quoted 204 lines
Quoted 194 lines

Thanks,

Quoted 190 lines
---
  fs/f2fs/extent_cache.c    | 70 +++++++++++++++++++++++++++++++++++++++
  fs/f2fs/f2fs.h            |  3 ++
  fs/f2fs/file.c            | 11 ++++++
  include/uapi/linux/f2fs.h | 21 ++++++++++++
  4 files changed, 105 insertions(+)
fs/f2fs/extent_cache.c +70 -0
index e141ffb64e5f..0c10d5639d68 100644
@@ -14,6 +14,7 @@
 
  #include <linux/fs.h>
  #include <linux/f2fs_fs.h>
+#include <uapi/linux/f2fs.h>
 
  #include "f2fs.h"
  #include "node.h"
@@ -1267,6 +1268,75 @@ static void __init_extent_tree_info(struct extent_tree_info *eti)
  	atomic_set(&eti->total_ext_node, 0);
  }
 
+int f2fs_get_read_cache_extents(struct inode *inode,
+			struct f2fs_read_cache_extent __user *uarg)
+{
+	struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
+	struct f2fs_cache_extent_info *kbuf = NULL;
+	struct f2fs_cache_extent_info largest = {};
+	struct rb_node *node;
+	struct extent_node *en;
+	unsigned int capacity, count = 0;
+	__u32 flags = 0;
+	int ret = 0;
+
+	if (get_user(capacity, &uarg->ext_count))
+		return -EFAULT;
+
+	if (is_inode_flag_set(inode, FI_NO_EXTENT))
+		flags |= F2FS_EXT_FL_NO_EXTENT;
+
+	if (!et || (flags & F2FS_EXT_FL_NO_EXTENT)) {
+		if (put_user(0U, &uarg->ext_count) ||
+		    put_user(flags, &uarg->flags) ||
+		    put_user(0U, &uarg->node_count))
+			return -EFAULT;
+		return 0;
+	}
+
+	if (capacity) {
+		kbuf = f2fs_kvmalloc(F2FS_I_SB(inode), capacity * sizeof(*kbuf), GFP_KERNEL);
+		if (!kbuf)
+			return -ENOMEM;
+	}
+
+	read_lock(&et->lock);
+
+	largest.fofs = et->largest.fofs;
+	largest.blk = et->largest.blk;
+	largest.len = et->largest.len;
+	largest.last_access_mode = et->largest.last_access_mode;
+
+	for (node = rb_first_cached(&et->root); node; node = rb_next(node)) {
+		if (count >= capacity)
+			break;
+		en = rb_entry(node, struct extent_node, rb_node);
+
+		kbuf[count].fofs = en->ei.fofs;
+		kbuf[count].blk = en->ei.blk;
+		kbuf[count].len = en->ei.len;
+		kbuf[count].last_access_mode = en->ei.last_access_mode;
+		count++;
+	}
+
+	read_unlock(&et->lock);
+
+	if (count && copy_to_user(uarg->extents, kbuf,
+				  count * sizeof(*kbuf))) {
+		ret = -EFAULT;
+		goto out;
+	}
+
+	if (put_user(count, &uarg->ext_count) ||
+	    put_user(flags, &uarg->flags) ||
+	    put_user((u32)atomic_read(&et->node_cnt), &uarg->node_count) ||
+	    copy_to_user(&uarg->largest, &largest, sizeof(largest)))
+		ret = -EFAULT;
+out:
+	kvfree(kbuf);
+	return ret;
+}
+
  void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi)
  {
  	__init_extent_tree_info(&sbi->extent_tree[EX_READ]);
fs/f2fs/f2fs.h +3 -0
index 1588b64d04a3..69641fc31c51 100644
@@ -26,6 +26,7 @@
  #include <linux/part_stat.h>
  #include <linux/rw_hint.h>
 
+#include <uapi/linux/f2fs.h>
  #include <linux/fscrypt.h>
  #include <linux/fsverity.h>
 
@@ -4590,6 +4591,8 @@ void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
  void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
  				pgoff_t fofs, block_t blkaddr, unsigned int len,
  				enum extent_access_mode access_mode);
+int f2fs_get_read_cache_extents(struct inode *inode,
+			struct f2fs_read_cache_extent __user *uarg);
  unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
  			int nr_shrink);
 
fs/f2fs/file.c +11 -0
index a3a5d499eadf..66ec9927d667 100644
@@ -3672,6 +3672,14 @@ static int f2fs_ioc_precache_extents(struct file *filp)
  	return f2fs_precache_extents(file_inode(filp));
  }
 
+static int f2fs_ioc_get_read_cache_extents(struct file *filp, unsigned long arg)
+{
+	struct inode *inode = file_inode(filp);
+
+	return f2fs_get_read_cache_extents(inode,
+			(struct f2fs_read_cache_extent __user *)arg);
+}
+
  static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
  {
  	struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
@@ -4744,6 +4752,8 @@ static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  		return f2fs_ioc_get_dev_alias_file(filp, arg);
  	case F2FS_IOC_IO_PRIO:
  		return f2fs_ioc_io_prio(filp, arg);
+	case F2FS_IOC_GET_READ_CACHE_EXTENTS:
+		return f2fs_ioc_get_read_cache_extents(filp, arg);
  	default:
  		return -ENOTTY;
  	}
@@ -5506,6 +5516,7 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  	case F2FS_IOC_COMPRESS_FILE:
  	case F2FS_IOC_GET_DEV_ALIAS_FILE:
  	case F2FS_IOC_IO_PRIO:
+	case F2FS_IOC_GET_READ_CACHE_EXTENTS:
  		break;
  	default:
  		return -ENOIOCTLCMD;
include/uapi/linux/f2fs.h +21 -0
index 795e26258355..6ff9003bc030 100644
@@ -45,6 +45,8 @@
  #define F2FS_IOC_START_ATOMIC_REPLACE	_IO(F2FS_IOCTL_MAGIC, 25)
  #define F2FS_IOC_GET_DEV_ALIAS_FILE	_IOR(F2FS_IOCTL_MAGIC, 26, __u32)
  #define F2FS_IOC_IO_PRIO		_IOW(F2FS_IOCTL_MAGIC, 27, __u32)
+#define F2FS_IOC_GET_READ_CACHE_EXTENTS	_IOWR(F2FS_IOCTL_MAGIC, 28,	\
+						struct f2fs_read_cache_extent)
 
  /*
   * should be same as XFS_IOC_GOINGDOWN.
@@ -104,4 +106,23 @@ struct f2fs_comp_option {
  	__u8 log_cluster_size;
  };
 
+struct f2fs_cache_extent_info {
+	__u32 fofs;		/* start file offset in blocks */
+	__u32 blk;		/* start block address */
+	__u32 len;		/* length in blocks */
+	__u32 last_access_mode; /* last access mode of extent_node */
+};
+
+/* flags for f2fs_read_cache_extent */
+#define F2FS_EXT_FL_NO_EXTENT	0x1	/* extent cache disabled for this inode */
+
+struct f2fs_read_cache_extent {
+	__u32 ext_count;	/* in: array capacity; out: mapped extent count */
+	__u32 flags;		/* out: status flags */
+	__u32 node_count;	/* out: total extent nodes in tree */
+	__u32 reserved;
+	struct f2fs_cache_extent_info largest;		/* out: largest extent */
+	struct f2fs_cache_extent_info extents[];	/* out: extent array */
+};
+
Quoted 8 lines

include/uapi/linux/f2fs.h struct f2fs_comp_option { · line 128

Quoted 1 line
include/uapi/linux/f2fs.h +0 -0
  #endif /* _UAPI_LINUX_F2FS_H */

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#28 lore raw

Re: [f2fs-dev] [PATCH RESEND 5/5] f2fs: add ioctl to export read extent cache to userspace for debug

Quoted 18 lines · Yongpeng Yang
Quoted 16 lines · On 6/15/26 11:51 PM, Jaegeuk Kim via Linux-f2fs-devel wrote:
Quoted 14 lines · On 06/15, Chao Yu via Linux-f2fs-devel wrote:
Quoted 6 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Add F2FS_IOC_GET_READ_CACHE_EXTENTS ioctl that allows userspace to retrieve all cached read extents for a given file. This uses a two-call pattern similar to fiemap: the first call with ext_count=0 queries the node_count, and the second call fetches the actual extent entries.

It looks a little bit heavy to maintain a debug purpose ioctl interface.

Maybe set ino via sysfs and dump extent cache via procfs? only enabled if F2FS_CHECK_FS=y?

Jaegeuk, do you have any suggestion?

Quoted 7 lines
Quoted 2 lines

Agreed, and even not sure we need sysfs or procfs. Can we dump the extents via fsck?

It is indeed quite heavy. Our primary goal here is debugging inode extent trees, and fsck cannot meet this requirement. If we rely on procfs or sysfs interfaces, we would have to open dedicated files within sysfs.c, which feels like an inappropriate approach.

call access() in userspace application or touch in script to load inode via lookup() first, then access sysfs and procfs node?

Thanks,

Quoted 204 lines

Thanks
Yongpeng,

Quoted 199 lines
Quoted 185 lines

Thanks,

Quoted 181 lines

---
  fs/f2fs/extent_cache.c    | 70 +++++++++++++++++++++++++++++++++++++++ fs/f2fs/f2fs.h            |  3 ++
  fs/f2fs/file.c            | 11 ++++++ include/uapi/linux/f2fs.h | 21 ++++++++++++

  4 files changed, 105 insertions(+)
fs/f2fs/extent_cache.c +70 -0
index e141ffb64e5f..0c10d5639d68 100644
@@ -14,6 +14,7 @@
   #include <linux/fs.h>
   #include <linux/f2fs_fs.h>
+#include <uapi/linux/f2fs.h>
   #include "f2fs.h"
   #include "node.h"
@@ -1267,6 +1268,75 @@ static void __init_extent_tree_info(struct extent_tree_info *eti)
       atomic_set(&eti->total_ext_node, 0);
   }
+int f2fs_get_read_cache_extents(struct inode *inode,
+            struct f2fs_read_cache_extent __user *uarg)
+{
+    struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
+    struct f2fs_cache_extent_info *kbuf = NULL;
+    struct f2fs_cache_extent_info largest = {};
+    struct rb_node *node;
+    struct extent_node *en;
+    unsigned int capacity, count = 0;
+    __u32 flags = 0;
+    int ret = 0;
+
+    if (get_user(capacity, &uarg->ext_count))
+        return -EFAULT;
+
+    if (is_inode_flag_set(inode, FI_NO_EXTENT))
+        flags |= F2FS_EXT_FL_NO_EXTENT;
+
+    if (!et || (flags & F2FS_EXT_FL_NO_EXTENT)) {
+        if (put_user(0U, &uarg->ext_count) ||
+            put_user(flags, &uarg->flags) ||
+            put_user(0U, &uarg->node_count))
+            return -EFAULT;
+        return 0;
+    }
+
+    if (capacity) {
+        kbuf = f2fs_kvmalloc(F2FS_I_SB(inode), capacity * sizeof(*kbuf), GFP_KERNEL);
+        if (!kbuf)
+            return -ENOMEM;
+    }
+
+    read_lock(&et->lock);
+
+    largest.fofs = et->largest.fofs;
+    largest.blk = et->largest.blk;
+    largest.len = et->largest.len;
+    largest.last_access_mode = et->largest.last_access_mode;
+
+    for (node = rb_first_cached(&et->root); node; node = rb_next(node)) {
+        if (count >= capacity)
+            break;
+        en = rb_entry(node, struct extent_node, rb_node);
+
+        kbuf[count].fofs = en->ei.fofs;
+        kbuf[count].blk = en->ei.blk;
+        kbuf[count].len = en->ei.len;
+        kbuf[count].last_access_mode = en->ei.last_access_mode;
+        count++;
+    }
+
+    read_unlock(&et->lock);
+
+    if (count && copy_to_user(uarg->extents, kbuf,
+                  count * sizeof(*kbuf))) {
+        ret = -EFAULT;
+        goto out;
+    }
+
+    if (put_user(count, &uarg->ext_count) ||
+        put_user(flags, &uarg->flags) ||
+        put_user((u32)atomic_read(&et->node_cnt), &uarg->node_count) ||
+        copy_to_user(&uarg->largest, &largest, sizeof(largest)))
+        ret = -EFAULT;
+out:
+    kvfree(kbuf);
+    return ret;
+}
+
   void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi)
   {
       __init_extent_tree_info(&sbi->extent_tree[EX_READ]);
fs/f2fs/f2fs.h +3 -0
index 1588b64d04a3..69641fc31c51 100644
@@ -26,6 +26,7 @@
   #include <linux/part_stat.h>
   #include <linux/rw_hint.h>
+#include <uapi/linux/f2fs.h>
   #include <linux/fscrypt.h>
   #include <linux/fsverity.h>
@@ -4590,6 +4591,8 @@ void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
   void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
                   pgoff_t fofs, block_t blkaddr, unsigned int len,
                   enum extent_access_mode access_mode);
+int f2fs_get_read_cache_extents(struct inode *inode,
+            struct f2fs_read_cache_extent __user *uarg);
   unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
               int nr_shrink);
fs/f2fs/file.c +8 -0
index a3a5d499eadf..66ec9927d667 100644
@@ -3672,6 +3672,14 @@ static int f2fs_ioc_precache_extents(struct file *filp)
       return f2fs_precache_extents(file_inode(filp));
   }
+static int f2fs_ioc_get_read_cache_extents(struct file *filp, unsigned long arg)
+{
+    struct inode *inode = file_inode(filp);
+
+    return f2fs_get_read_cache_extents(inode,
+            (struct f2fs_read_cache_extent __user *)arg);
+}
+
   static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
   {
       struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
@@ -4744,6 +4752,8 @@ static long __f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
           return f2fs_ioc_get_dev_alias_file(filp, arg);

      case F2FS_IOC_IO_PRIO:
          return f2fs_ioc_io_prio(filp, arg);
+    case F2FS_IOC_GET_READ_CACHE_EXTENTS:
+        return f2fs_ioc_get_read_cache_extents(filp, arg); default:
          return -ENOTTY;
      }

fs/f2fs/file.c +0 -0
@@ -5506,6 +5516,7 @@ long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)

      case F2FS_IOC_COMPRESS_FILE:
      case F2FS_IOC_GET_DEV_ALIAS_FILE:
      case F2FS_IOC_IO_PRIO:
+    case F2FS_IOC_GET_READ_CACHE_EXTENTS:
          break;
      default:
          return -ENOIOCTLCMD;

include/uapi/linux/f2fs.h +21 -0
index 795e26258355..6ff9003bc030 100644
@@ -45,6 +45,8 @@
   #define F2FS_IOC_START_ATOMIC_REPLACE    _IO(F2FS_IOCTL_MAGIC, 25)
   #define F2FS_IOC_GET_DEV_ALIAS_FILE    _IOR(F2FS_IOCTL_MAGIC, 26, __u32)
   #define F2FS_IOC_IO_PRIO        _IOW(F2FS_IOCTL_MAGIC, 27, __u32)
+#define F2FS_IOC_GET_READ_CACHE_EXTENTS    _IOWR(F2FS_IOCTL_MAGIC, 28,    \
+                        struct f2fs_read_cache_extent)
   /*
    * should be same as XFS_IOC_GOINGDOWN.
@@ -104,4 +106,23 @@ struct f2fs_comp_option {
       __u8 log_cluster_size;
   };
+struct f2fs_cache_extent_info {
+    __u32 fofs;        /* start file offset in blocks */
+    __u32 blk;        /* start block address */
+    __u32 len;        /* length in blocks */
+    __u32 last_access_mode; /* last access mode of extent_node */
+};
+
+/* flags for f2fs_read_cache_extent */
+#define F2FS_EXT_FL_NO_EXTENT    0x1    /* extent cache disabled for this inode */
+
+struct f2fs_read_cache_extent {
+    __u32 ext_count;    /* in: array capacity; out: mapped extent count */
+    __u32 flags;        /* out: status flags */
+    __u32 node_count;    /* out: total extent nodes in tree */
+    __u32 reserved;
+    struct f2fs_cache_extent_info largest;        /* out: largest extent */
+    struct f2fs_cache_extent_info extents[];    /* out: extent array */
+};
+
Quoted 8 lines

include/uapi/linux/f2fs.h struct f2fs_comp_option { · line 128

Quoted 1 line
include/uapi/linux/f2fs.h +0 -0
   #endif /* _UAPI_LINUX_F2FS_H */

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#29 lore raw

Re: [f2fs-dev] [PATCH RESEND 5/5] f2fs: add ioctl to export read extent cache to userspace for debug

Quoted 22 lines · On 6/20/26 3:05 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 21 lines · Yongpeng Yang
Quoted 18 lines · On 6/15/26 11:51 PM, Jaegeuk Kim via Linux-f2fs-devel wrote:
Quoted 8 lines · On 06/15, Chao Yu via Linux-f2fs-devel wrote:
Quoted 6 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

Add F2FS_IOC_GET_READ_CACHE_EXTENTS ioctl that allows userspace to retrieve all cached read extents for a given file. This uses a two- call pattern similar to fiemap: the first call with ext_count=0 queries the node_count, and the second call fetches the actual extent entries.

Quoted 6 lines

It looks a little bit heavy to maintain a debug purpose ioctl interface.

Maybe set ino via sysfs and dump extent cache via procfs? only enabled if F2FS_CHECK_FS=y?

Jaegeuk, do you have any suggestion?

Agreed, and even not sure we need sysfs or procfs. Can we dump the extents via fsck?

Quoted 7 lines
Quoted 4 lines

It is indeed quite heavy. Our primary goal here is debugging inode extent trees, and fsck cannot meet this requirement. If we rely on procfs or sysfs interfaces, we would have to open dedicated files within sysfs.c, which feels like an inappropriate approach.

call access() in userspace application or touch in script to load inode via lookup() first, then access sysfs and procfs node?

Thanks for the suggestion, this is more lightweight. I'll modify it in v2 patch.

Thanks
Yongpeng,

Quoted 225 lines

Thanks,

Quoted 219 lines

Thanks
Yongpeng,

Quoted 214 lines
Quoted 200 lines

Thanks,

Quoted 196 lines

---
  fs/f2fs/extent_cache.c    | 70 ++++++++++++++++++++++++++++++++++
+++++
  fs/f2fs/f2fs.h            |  3 ++
  fs/f2fs/file.c            | 11 ++++++ include/uapi/linux/f2fs.h | 21 ++++++++++++

  4 files changed, 105 insertions(+)
fs/f2fs/extent_cache.c +63 -0
index e141ffb64e5f..0c10d5639d68 100644
@@ -14,6 +14,7 @@
   #include <linux/fs.h>
   #include <linux/f2fs_fs.h>
+#include <uapi/linux/f2fs.h>
   #include "f2fs.h"
   #include "node.h"
@@ -1267,6 +1268,75 @@ static void __init_extent_tree_info(struct 
 extent_tree_info *eti)
       atomic_set(&eti->total_ext_node, 0);
   }
+int f2fs_get_read_cache_extents(struct inode *inode,
+            struct f2fs_read_cache_extent __user *uarg)
+{
+    struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
+    struct f2fs_cache_extent_info *kbuf = NULL;
+    struct f2fs_cache_extent_info largest = {};
+    struct rb_node *node;
+    struct extent_node *en;
+    unsigned int capacity, count = 0;
+    __u32 flags = 0;
+    int ret = 0;
+
+    if (get_user(capacity, &uarg->ext_count))
+        return -EFAULT;
+
+    if (is_inode_flag_set(inode, FI_NO_EXTENT))
+        flags |= F2FS_EXT_FL_NO_EXTENT;
+
+    if (!et || (flags & F2FS_EXT_FL_NO_EXTENT)) {
+        if (put_user(0U, &uarg->ext_count) ||
+            put_user(flags, &uarg->flags) ||
+            put_user(0U, &uarg->node_count))
+            return -EFAULT;
+        return 0;
+    }
+
+    if (capacity) {
+        kbuf = f2fs_kvmalloc(F2FS_I_SB(inode), capacity * 
 sizeof(*kbuf), GFP_KERNEL);
+        if (!kbuf)
+            return -ENOMEM;
+    }
+
+    read_lock(&et->lock);
+
+    largest.fofs = et->largest.fofs;
+    largest.blk = et->largest.blk;
+    largest.len = et->largest.len;
+    largest.last_access_mode = et->largest.last_access_mode;
+
+    for (node = rb_first_cached(&et->root); node; node = 
 rb_next(node)) {
+        if (count >= capacity)
+            break;
+        en = rb_entry(node, struct extent_node, rb_node);
+
+        kbuf[count].fofs = en->ei.fofs;
+        kbuf[count].blk = en->ei.blk;
+        kbuf[count].len = en->ei.len;
+        kbuf[count].last_access_mode = en->ei.last_access_mode;
+        count++;
+    }
+
+    read_unlock(&et->lock);
+
+    if (count && copy_to_user(uarg->extents, kbuf,
+                  count * sizeof(*kbuf))) {
+        ret = -EFAULT;
+        goto out;
+    }
+
+    if (put_user(count, &uarg->ext_count) ||
+        put_user(flags, &uarg->flags) ||
+        put_user((u32)atomic_read(&et->node_cnt), &uarg- 
Quoted 1 line

node_count) ||

+        copy_to_user(&uarg->largest, &largest, sizeof(largest))) +        ret = -EFAULT;

fs/f2fs/extent_cache.c +5 -0
+out:
+    kvfree(kbuf);
+    return ret;
+}
+

  void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi) { __init_extent_tree_info(&sbi->extent_tree[EX_READ]);

fs/f2fs/f2fs.h +3 -0
index 1588b64d04a3..69641fc31c51 100644
@@ -26,6 +26,7 @@
   #include <linux/part_stat.h>
   #include <linux/rw_hint.h>
+#include <uapi/linux/f2fs.h>
   #include <linux/fscrypt.h>
   #include <linux/fsverity.h>
@@ -4590,6 +4591,8 @@ void f2fs_update_read_extent_cache(struct 
 dnode_of_data *dn);
   void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
                   pgoff_t fofs, block_t blkaddr, unsigned int len,
                   enum extent_access_mode access_mode);
+int f2fs_get_read_cache_extents(struct inode *inode,
+            struct f2fs_read_cache_extent __user *uarg);
   unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
               int nr_shrink);
fs/f2fs/file.c +8 -0
index a3a5d499eadf..66ec9927d667 100644
@@ -3672,6 +3672,14 @@ static int f2fs_ioc_precache_extents(struct 
 file *filp)
       return f2fs_precache_extents(file_inode(filp));
   }
+static int f2fs_ioc_get_read_cache_extents(struct file *filp, 
 unsigned long arg)
+{
+    struct inode *inode = file_inode(filp);
+
+    return f2fs_get_read_cache_extents(inode,
+            (struct f2fs_read_cache_extent __user *)arg);
+}
+
   static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
   {

      struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));

fs/f2fs/file.c +0 -0
@@ -4744,6 +4752,8 @@ static long __f2fs_ioctl(struct file *filp, 
 unsigned int cmd, unsigned long arg)
           return f2fs_ioc_get_dev_alias_file(filp, arg);

      case F2FS_IOC_IO_PRIO:
          return f2fs_ioc_io_prio(filp, arg);
+    case F2FS_IOC_GET_READ_CACHE_EXTENTS:
+        return f2fs_ioc_get_read_cache_extents(filp, arg); default:
          return -ENOTTY;
      }

fs/f2fs/file.c +0 -0
@@ -5506,6 +5516,7 @@ long f2fs_compat_ioctl(struct file *file, 
 unsigned int cmd, unsigned long arg)

      case F2FS_IOC_COMPRESS_FILE:
      case F2FS_IOC_GET_DEV_ALIAS_FILE:
      case F2FS_IOC_IO_PRIO:
+    case F2FS_IOC_GET_READ_CACHE_EXTENTS:
          break;
      default:
          return -ENOIOCTLCMD;

include/uapi/linux/f2fs.h +11 -0
index 795e26258355..6ff9003bc030 100644
@@ -45,6 +45,8 @@
   #define F2FS_IOC_START_ATOMIC_REPLACE    _IO(F2FS_IOCTL_MAGIC, 25)
   #define F2FS_IOC_GET_DEV_ALIAS_FILE    _IOR(F2FS_IOCTL_MAGIC, 26, __u32)
   #define F2FS_IOC_IO_PRIO        _IOW(F2FS_IOCTL_MAGIC, 27, __u32)
+#define F2FS_IOC_GET_READ_CACHE_EXTENTS    _IOWR(F2FS_IOCTL_MAGIC, 
 28,    \
+                        struct f2fs_read_cache_extent)
   /*
    * should be same as XFS_IOC_GOINGDOWN.
@@ -104,4 +106,23 @@ struct f2fs_comp_option {
       __u8 log_cluster_size;
   };
+struct f2fs_cache_extent_info {
+    __u32 fofs;        /* start file offset in blocks */
+    __u32 blk;        /* start block address */
+    __u32 len;        /* length in blocks */
+    __u32 last_access_mode; /* last access mode of extent_node */
+};
+
+/* flags for f2fs_read_cache_extent */
+#define F2FS_EXT_FL_NO_EXTENT    0x1    /* extent cache disabled 

for this inode */

include/uapi/linux/f2fs.h +3 -0
+
+struct f2fs_read_cache_extent {
+    __u32 ext_count;    /* in: array capacity; out: mapped extent 

count */
+    __u32 flags;        /* out: status flags */
+    __u32 node_count;    /* out: total extent nodes in tree */ +    __u32 reserved;
+    struct f2fs_cache_extent_info largest;        /* out: largest extent */
+    struct f2fs_cache_extent_info extents[];    /* out: extent array */

include/uapi/linux/f2fs.h +2 -0
+};
+
Quoted 8 lines
Quoted 1 line

  #endif /* _UAPI_LINUX_F2FS_H */

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Quoted 4 lines

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#30 lore raw
In reply to Yongpeng Yang
Quoted 36 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

The following scenario can cause fiemap to report incorrect extents:

$ mkfs.f2fs /dev/vdb -f
$ mount -o mode=lfs /dev/vdb /mnt/f2fs/
$ dd if=/dev/urandom of=data bs=4K count=874 conv=notrunc
$ f2fs_io fiemap 0 1000000 data 1
$ shrink all extent
$ dd if=/dev/urandom of=data bs=4K count=150 seek=874 conv=notrunc
$ f2fs_io fiemap 0 1000000 data 1
Fiemap: offset = 0 len = 1000000
        logical addr.    physical addr.   length           flags
0       0000000000000000 00000002868d4000 000000000036a000 00001000
1       000000000036a000 0000000286c3e000 0000000000096000 00001001

The root cause is that when the largest extent is not in the extent tree, mergeable extents are not merged, causing f2fs_map_blocks to misjudge and output an incorrect extent list.

Fix this by allowing the extent being inserted to merge with the largest extent. When updating the extent tree range, if the new extent can be front-merged or back-merged with the largest extent and the largest extent is not in the rb-tree, merge them before the normal lookup.

Fixes 429511cdf8b3 ("f2fs: add core functions for rb-tree extent cache")
---
 fs/f2fs/extent_cache.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
fs/f2fs/extent_cache.c +0 -0
index 61f6b9714366..aa368a01b035 100644

fs/f2fs/extent_cache.c static void __update_extent_tree_range(struct inode *inode, · line 704

Quoted 3 lines
fs/f2fs/extent_cache.c +0 -0
@@ -702,6 +702,27 @@ static void __update_extent_tree_range(struct inode *inode,
 		__drop_largest_extent(et, fofs, len);
 	}
 

This can only be used for EX_READ?

fs/f2fs/extent_cache.c static void __update_extent_tree_range(struct inode *inode, · line 715

Quoted 11 lines
fs/f2fs/extent_cache.c +11 -0
+	if (et->largest.len != 0 &&
+			(__is_front_mergeable(tei, &et->largest, type) ||
+			 __is_back_mergeable(tei, &et->largest, type))) {
+		/* 0. try to merge with largest extent. */
+		en = __lookup_extent_node_ret(&et->root,
+				et->cached_en, et->largest.fofs,
+				&prev_en, &next_en,
+				&insert_p, &insert_parent,
+				&leftmost);
+		if (!en) {
+			if (__is_back_mergeable(tei, &et->largest, type)) {

Can we reuse result of previous __is_back_mergeable()?

Thanks,

fs/f2fs/extent_cache.c static void __update_extent_tree_range(struct inode *inode, · line 728

Quoted 13 lines
fs/f2fs/extent_cache.c +10 -0
+				tei->fofs = et->largest.fofs;
+				tei->blk = et->largest.blk;
+				fofs = tei->fofs;
+			}
+			tei->len += et->largest.len;
+			len = tei->len;
+			end = fofs + len;
+		}
+	}
+
 	/* 1. lookup first extent node in range [fofs, fofs + len - 1] */
 	en = __lookup_extent_node_ret(&et->root,
 					et->cached_en, fofs,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

#31 lore raw
Quoted 32 lines · On 6/15/26 7:26 PM, Chao Yu via Linux-f2fs-devel wrote:
Quoted 31 lines · Yongpeng Yang

From: Yongpeng Yang <[email protected]>

The following scenario can cause fiemap to report incorrect extents:

$ mkfs.f2fs /dev/vdb -f
$ mount -o mode=lfs /dev/vdb /mnt/f2fs/
$ dd if=/dev/urandom of=data bs=4K count=874 conv=notrunc
$ f2fs_io fiemap 0 1000000 data 1
$ shrink all extent
$ dd if=/dev/urandom of=data bs=4K count=150 seek=874 conv=notrunc
$ f2fs_io fiemap 0 1000000 data 1
Fiemap: offset = 0 len = 1000000
         logical addr.    physical addr.   length           flags
0       0000000000000000 00000002868d4000 000000000036a000 00001000
1       000000000036a000 0000000286c3e000 0000000000096000 00001001

The root cause is that when the largest extent is not in the extent tree, mergeable extents are not merged, causing f2fs_map_blocks to misjudge and output an incorrect extent list.

Fix this by allowing the extent being inserted to merge with the largest extent. When updating the extent tree range, if the new extent can be front-merged or back-merged with the largest extent and the largest extent is not in the rb-tree, merge them before the normal lookup.

Fixes 429511cdf8b3 ("f2fs: add core functions for rb-tree extent cache")
---
  fs/f2fs/extent_cache.c | 21 +++++++++++++++++++++
  1 file changed, 21 insertions(+)
Quoted 10 lines

fs/f2fs/extent_cache.c static void __update_extent_tree_range(struct inode *inode, · line 704

Quoted 8 lines
fs/f2fs/extent_cache.c +0 -0
index 61f6b9714366..aa368a01b035 100644
@@ -702,6 +702,27 @@ static void __update_extent_tree_range(struct inode *inode,
  		__drop_largest_extent(et, fofs, len);
  	}
 

This can only be used for EX_READ?

Yes, we only handle the case where the newly inserted mapping of EX_READ can be merged with the largest extent.

Quoted 14 lines

fs/f2fs/extent_cache.c static void __update_extent_tree_range(struct inode *inode, · line 715

Quoted 12 lines
fs/f2fs/extent_cache.c +11 -0
+	if (et->largest.len != 0 &&
+			(__is_front_mergeable(tei, &et->largest, type) ||
+			 __is_back_mergeable(tei, &et->largest, type))) {
+		/* 0. try to merge with largest extent. */
+		en = __lookup_extent_node_ret(&et->root,
+				et->cached_en, et->largest.fofs,
+				&prev_en, &next_en,
+				&insert_p, &insert_parent,
+				&leftmost);
+		if (!en) {
+			if (__is_back_mergeable(tei, &et->largest, type)) {

Can we reuse result of previous __is_back_mergeable()?

OK, I'll change it in v2 patch.

Thanks
Yongpeng,

Quoted 15 lines

Thanks,

Quoted 12 lines
fs/f2fs/extent_cache.c +10 -0
+				tei->fofs = et->largest.fofs;
+				tei->blk = et->largest.blk;
+				fofs = tei->fofs;
+			}
+			tei->len += et->largest.len;
+			len = tei->len;
+			end = fofs + len;
+		}
+	}
+
  	/* 1. lookup first extent node in range [fofs, fofs + len - 1] */
  	en = __lookup_extent_node_ret(&et->root,
Quoted 8 lines

fs/f2fs/extent_cache.c static void __update_extent_tree_range(struct inode *inode, · line 728

Quoted 1 line
fs/f2fs/extent_cache.c +0 -0
  					et->cached_en, fofs,

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel