hfs: propagate extent B-tree insertion errors
Subsystems (2)
PATCH 1/1 hfs: propagate extent B-tree insertion errors
hfs_brec_insert() may fail while inserting a new extent record, but __hfs_ext_write_extent() currently ignores its return value and clears HFS_FLG_EXT_DIRTY and HFS_FLG_EXT_NEW as if the insertion had succeeded.
Propagate the error returned by hfs_brec_insert() and only clear the extent flags after a successful insertion.
Fault injection of -EIO into the extent B-tree insertion path confirmed that __hfs_ext_write_extent() returned success despite the insertion failure. With the error propagated, the failure is returned to the caller instead.
--- fs/hfs/extent.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fs/hfs/extent.c +4 -1
@@ -121,7 +121,10 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) res =hfs_bmap_reserve (fd->tree, fd->tree->depth +1 );if (res)return res; -hfs_brec_insert (fd,HFS_I (inode)->cached_extents,sizeof (hfs_extent_rec)); + res =hfs_brec_insert (fd,HFS_I (inode)->cached_extents, +sizeof (hfs_extent_rec)); +if (res) +return res;HFS_I (inode)->flags &= ~(HFS_FLG_EXT_DIRTY |HFS_FLG_EXT_NEW ); }else {if (res)
Signature · 1 line
2.55.0
Viacheslav Dubeyko
maintainerQuoted 6 lines · Davy Felipe
hfs_brec_insert()may fail while inserting a new extent record, but__hfs_ext_write_extent()currently ignores its return value and clearsHFS_FLG_EXT_DIRTYandHFS_FLG_EXT_NEWas if the insertion had succeeded.
Quoted 8 lines
Propagate the error returned by
hfs_brec_insert()and only clear the extent flags after a successful insertion.Fault injection of
-EIOinto the extent B-tree insertion path confirmed
that__hfs_ext_write_extent()returned success despite the insertion failure. With the error propagated, the failure is returned to the caller instead.
How HFS driver behaves when we return error from hfs_brec_insert()? Are we capable to properly process it? I assume that everything should work well. But have you tested failure use-case?
Quoted 23 lines
Signed-off-by Davy Felipe <[email protected]>---
fs/hfs/extent.c | 5 ++++-1 file changed, 4 insertions(+), 1 deletion(-)fs/hfs/extent.c +0
-0@@ -121,7 +121,10 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd)res =
hfs_bmap_reserve(fd->tree, fd->tree->depth + 1);
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_brec_insert(fd, HFS_I(inode)->cached_extents,
sizeof(hfs_extent_rec));fs/hfs/extent.c +1
-0+ res = hfs_brec_insert(fd, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +3
-0+ sizeof(hfs_extent_rec)); +if (res) +return res;
Quoted 4 lines
HFS_I(inode)->flags &=
~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW);
} else {
if (res)
We ignore potential errors from hfs_bnode_write(). Should we start processing it too?
} else {
if (res)
return res;
hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fd->entryoffset,fd->entrylength);
HFS_I(inode)->flags &= ~HFS_FLG_EXT_DIRTY; }
Thanks,
Slava.
PATCH v3 1/1 hfs: handle extent B-tree write errors
hfs_brec_insert() may fail while inserting a new extent record, but __hfs_ext_write_extent() currently ignores its return value and clears HFS_FLG_EXT_DIRTY and HFS_FLG_EXT_NEW as if the insertion had succeeded.
Propagate errors returned by hfs_brec_insert() and only clear the extent flags after a successful insertion.
When updating an existing extent record, hfs_bnode_write() returns void. The helper may reject an invalid offset or shorten an overlong write without returning status to its caller. Validate the extent record length and write range before calling hfs_bnode_write() so an invalid update is reported as -EIO instead of being treated as successful.
Negative-path testing in QEMU confirmed that an insertion error is propagated to the caller. Testing the existing-record path also confirmed that invalid write parameters are rejected before HFS_FLG_EXT_DIRTY is cleared.
Changes in v3:
- Preserve the original insertion-error propagation.
- Validate the existing extent record before hfs_bnode_write().
- Return -EIO for invalid extent write parameters instead of clearing HFS_FLG_EXT_DIRTY after a rejected write.
- Clarify that hfs_bnode_write() returns void and therefore has no error value for this caller to propagate.
- Keep test-only instrumentation outside the submitted patch.
--- fs/hfs/extent.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
fs/hfs/extent.c +11 -2
@@ -121,12 +121,21 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) res =hfs_bmap_reserve (fd->tree, fd->tree->depth +1 );if (res)return res; -hfs_brec_insert (fd,HFS_I (inode)->cached_extents,sizeof (hfs_extent_rec)); + res =hfs_brec_insert (fd,HFS_I (inode)->cached_extents, +sizeof (hfs_extent_rec)); +if (res) +return res;HFS_I (inode)->flags &= ~(HFS_FLG_EXT_DIRTY |HFS_FLG_EXT_NEW ); }else {if (res)return res; -hfs_bnode_write (fd->bnode,HFS_I (inode)->cached_extents, fd->entryoffset, fd->entrylength); +if (fd->entrylength !=sizeof (hfs_extent_rec) || + fd->entryoffset <0 || + (u64 )fd->entryoffset + fd->entrylength > + fd->tree->node_size) +return -EIO ; +hfs_bnode_write (fd->bnode,HFS_I (inode)->cached_extents, + fd->entryoffset, fd->entrylength);HFS_I (inode)->flags &= ~HFS_FLG_EXT_DIRTY ; }return 0 ;
Signature · 1 line
2.55.0
PATCH v2 1/1 hfs: handle extent B-tree write errors
In reply to Davy Felipe__hfs_ext_write_extent() does not report all failures while updating the extents B-tree.
When inserting a new extent record, the return value of hfs_brec_insert() is ignored and HFS_FLG_EXT_DIRTY and HFS_FLG_EXT_NEW are cleared even if the insertion fails.
When updating an existing extent record, hfs_bnode_write() returns void, so its caller cannot detect a rejected write. Validate the extent record size and node range before calling hfs_bnode_write().
Propagate errors returned by hfs_brec_insert() and return -EIO for an invalid existing extent record. Only clear the extent dirty flags after a successful operation.
Fault injection confirmed both failure paths. Insertion errors are propagated to the caller, and invalid existing-record writes are rejected before hfs_bnode_write() without clearing the dirty state.
Changes in v2:
- Validate the existing extent record size and node range before calling hfs_bnode_write(), following review feedback.
- Return -EIO without clearing HFS_FLG_EXT_DIRTY when validation fails.
- Fault-injection tested the existing-record failure path. Before the change, hfs_bnode_write() rejected an invalid offset internally but __hfs_ext_write_extent() continued and cleared the dirty flag. With v2, the invalid write is rejected before hfs_bnode_write().
--- fs/hfs/extent.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
fs/hfs/extent.c +11 -2
@@ -121,12 +121,21 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) res =hfs_bmap_reserve (fd->tree, fd->tree->depth +1 );if (res)return res; -hfs_brec_insert (fd,HFS_I (inode)->cached_extents,sizeof (hfs_extent_rec)); + res =hfs_brec_insert (fd,HFS_I (inode)->cached_extents, +sizeof (hfs_extent_rec)); +if (res) +return res;HFS_I (inode)->flags &= ~(HFS_FLG_EXT_DIRTY |HFS_FLG_EXT_NEW ); }else {if (res)return res; -hfs_bnode_write (fd->bnode,HFS_I (inode)->cached_extents, fd->entryoffset, fd->entrylength); +if (fd->entrylength !=sizeof (hfs_extent_rec) || + fd->entryoffset <0 || + (u64 )fd->entryoffset + fd->entrylength > + fd->tree->node_size) +return -EIO ; +hfs_bnode_write (fd->bnode,HFS_I (inode)->cached_extents, + fd->entryoffset, fd->entrylength);HFS_I (inode)->flags &= ~HFS_FLG_EXT_DIRTY ; }return 0 ;
Signature · 1 line
2.55.0
Viacheslav Dubeyko
maintainerRe: [PATCH v2] hfs: handle extent B-tree write errors
Quoted 62 lines · Davy Felipe
__hfs_ext_write_extent()does not report all failures while updating the extents B-tree.When inserting a new extent record, the return value of
hfs_brec_insert()is ignored andHFS_FLG_EXT_DIRTYandHFS_FLG_EXT_NEW
are cleared even if the insertion fails.When updating an existing extent record,
hfs_bnode_write()returns void, so its caller cannot detect a rejected write. Validate the extent
record size and node range before callinghfs_bnode_write().Propagate errors returned by
hfs_brec_insert()and return-EIOfor an invalid existing extent record. Only clear the extent dirty flags after
a successful operation.Fault injection confirmed both failure paths. Insertion errors are propagated to the caller, and invalid existing-record writes are rejected before
hfs_bnode_write()without clearing the dirty state.Signed-off-by Davy Felipe <[email protected]>Changes in v2:
- Validate the existing extent record size and node range before callinghfs_bnode_write(), following review feedback.
- Return-EIOwithout clearingHFS_FLG_EXT_DIRTYwhen validation fails.
- Fault-injection tested the existing-record failure path. Before the change,hfs_bnode_write()rejected an invalid offset internally but__hfs_ext_write_extent()continued and cleared the dirty flag. With v2, the invalid write is rejected beforehfs_bnode_write().---
fs/hfs/extent.c | 13 +++++++++++--1 file changed, 11 insertions(+), 2 deletions(-)fs/hfs/extent.c +0
-0@@ -121,12 +121,21 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd)res =
hfs_bmap_reserve(fd->tree, fd->tree->depth + 1);
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_brec_insert(fd, HFS_I(inode)->cached_extents,
sizeof(hfs_extent_rec));fs/hfs/extent.c +1
-0+ res = hfs_brec_insert(fd, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +3
-0+ sizeof(hfs_extent_rec)); +if (res) +return res;HFS_I(inode)->flags &=
~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW);
} else {
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fd->entryoffset,fd->entrylength);
fs/hfs/extent.c static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) · line 135
Quoted 4 lines
fs/hfs/extent.c +4
-0+if (fd->entrylength !=sizeof (hfs_extent_rec) || + fd->entryoffset < 0 || + (u64)fd->entryoffset + fd->entrylength > + fd->tree->node_size)
I think it will be better to introduce a small check function that can be reused then. And code will be cleaner here. What do you think?
fs/hfs/extent.c static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) · line 138
Quoted 4 lines
fs/hfs/extent.c +2
-0+return -EIO ; + hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +1
-0+ fd->entryoffset, fd->entrylength);
I see that you are trying not to go into huge modification. But, frankly speaking, I believe we need the refactoring of hfs_bnode_write() calling. This function should return error code and we need to process this error code in other methods. Maybe, future refactoring work for you? ;)
Thanks,
Slava.
Quoted 3 lines
HFS_I(inode)->flags &= ~HFS_FLG_EXT_DIRTY; }
return 0;
Re: [PATCH v2] hfs: handle extent B-tree write errors
Hi Slava,
Thanks for the feedback.
Yes, I agree. A small reusable check helper would make the code cleaner and avoid duplicating the range validation.
Regarding hfs_bnode_write(), I also agree that its current void interface makes proper error handling difficult. Converting it to return an error code and auditing its callers looks like the right direction for a follow-up refactoring.
For this patch, I will keep the change small, introduce the reusable check helper, and send a v3.
I would be happy to work on the hfs_bnode_write() refactoring as a follow-up as well.
Thanks,
Davy Felipe
Quoted 89 lines · Viacheslav Dubeyko
Quoted 62 lines · Davy Felipe
__hfs_ext_write_extent()does not report all failures while updating the extents B-tree.When inserting a new extent record, the return value of
hfs_brec_insert()is ignored andHFS_FLG_EXT_DIRTYandHFS_FLG_EXT_NEW
are cleared even if the insertion fails.When updating an existing extent record,
hfs_bnode_write()returns void, so its caller cannot detect a rejected write. Validate the extent
record size and node range before callinghfs_bnode_write().Propagate errors returned by
hfs_brec_insert()and return-EIOfor an invalid existing extent record. Only clear the extent dirty flags after
a successful operation.Fault injection confirmed both failure paths. Insertion errors are propagated to the caller, and invalid existing-record writes are rejected before
hfs_bnode_write()without clearing the dirty state.Signed-off-by Davy Felipe <[email protected]>Changes in v2:
- Validate the existing extent record size and node range before callinghfs_bnode_write(), following review feedback.
- Return-EIOwithout clearingHFS_FLG_EXT_DIRTYwhen validation fails.
- Fault-injection tested the existing-record failure path. Before the change,hfs_bnode_write()rejected an invalid offset internally but__hfs_ext_write_extent()continued and cleared the dirty flag. With v2, the invalid write is rejected beforehfs_bnode_write().---
fs/hfs/extent.c | 13 +++++++++++--1 file changed, 11 insertions(+), 2 deletions(-)fs/hfs/extent.c +0
-0@@ -121,12 +121,21 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd)res =
hfs_bmap_reserve(fd->tree, fd->tree->depth + 1);
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_brec_insert(fd, HFS_I(inode)->cached_extents,
sizeof(hfs_extent_rec));fs/hfs/extent.c +1
-0+ res = hfs_brec_insert(fd, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +3
-0+ sizeof(hfs_extent_rec)); +if (res) +return res;HFS_I(inode)->flags &=
~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW);
} else {
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fd->entryoffset,fd->entrylength);fs/hfs/extent.c static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) · line 135
Quoted 4 lines
fs/hfs/extent.c +4
-0+if (fd->entrylength !=sizeof (hfs_extent_rec) || + fd->entryoffset < 0 || + (u64)fd->entryoffset + fd->entrylength > + fd->tree->node_size)I think it will be better to introduce a small check function that can be reused then. And code will be cleaner here. What do you think?
fs/hfs/extent.c static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) · line 138
Quoted 4 lines
fs/hfs/extent.c +2
-0+return -EIO ; + hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +1
-0+ fd->entryoffset, fd->entrylength);I see that you are trying not to go into huge modification. But, frankly speaking, I believe we need the refactoring of
hfs_bnode_write()calling. This function should return error code and we need to process this error code in other methods. Maybe, future refactoring work for you? ;)Thanks,
Slava.Quoted 3 lines
HFS_I(inode)->flags &= ~HFS_FLG_EXT_DIRTY; }
return 0;
PATCH v4 1/1 hfs: handle extent B-tree write errors
In reply to Viacheslav Dubeykohfs_brec_insert() may fail while inserting a new extent record, but __hfs_ext_write_extent() currently ignores its return value and clears HFS_FLG_EXT_DIRTY and HFS_FLG_EXT_NEW as if the insertion had succeeded.
Propagate errors returned by hfs_brec_insert() and only clear the extent flags after a successful insertion.
When updating an existing extent record, hfs_bnode_write() returns void. Validate the extent record size and use a reusable B-tree node range helper to reject invalid write parameters before calling hfs_bnode_write(). This prevents an invalid update from being treated as successful and avoids clearing HFS_FLG_EXT_DIRTY in that case.
Negative-path testing in QEMU confirmed that an insertion error is propagated to the caller. Testing the existing-record path also confirmed that invalid write parameters are rejected before HFS_FLG_EXT_DIRTY is cleared.
Sorry, I missed your suggestion about factoring the validation into a reusable helper in v3. This revision addresses it.
Changes in v4:
- Factor B-tree node range validation into a reusable helper, as suggested by Viacheslav Dubeyko.
- Keep the extent-record size check local to the extent write path.
- Preserve the error propagation and validation behavior from v3.
--- fs/hfs/btree.h | 7 +++++++ fs/hfs/extent.c | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-)
fs/hfs/btree.h +7 -0
@@ -84,6 +84,13 @@ struct hfs_find_data {int entryoffset, entrylength; }; +static inline bool hfs_bnode_is_valid_range (struct hfs_bnode *node, +int off,int len) +{ +return off >=0 && len >0 && + (u64 )off + len <= node->tree->node_size; +} +/* btree.c */ extern struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id,
fs/hfs/extent.c +10 -2
@@ -121,12 +121,20 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) res =hfs_bmap_reserve (fd->tree, fd->tree->depth +1 );if (res)return res; -hfs_brec_insert (fd,HFS_I (inode)->cached_extents,sizeof (hfs_extent_rec)); + res =hfs_brec_insert (fd,HFS_I (inode)->cached_extents, +sizeof (hfs_extent_rec)); +if (res) +return res;HFS_I (inode)->flags &= ~(HFS_FLG_EXT_DIRTY |HFS_FLG_EXT_NEW ); }else {if (res)return res; -hfs_bnode_write (fd->bnode,HFS_I (inode)->cached_extents, fd->entryoffset, fd->entrylength); +if (fd->entrylength !=sizeof (hfs_extent_rec) || + !hfs_bnode_is_valid_range (fd->bnode, fd->entryoffset, + fd->entrylength)) +return -EIO ; +hfs_bnode_write (fd->bnode,HFS_I (inode)->cached_extents, + fd->entryoffset, fd->entrylength);HFS_I (inode)->flags &= ~HFS_FLG_EXT_DIRTY ; }return 0 ;
Signature · 1 line
2.55.0
Viacheslav Dubeyko
maintainerRe: [PATCH v4] hfs: handle extent B-tree write errors
Quoted 44 lines · Davy Felipe
hfs_brec_insert()may fail while inserting a new extent record, but__hfs_ext_write_extent()currently ignores its return value and clearsHFS_FLG_EXT_DIRTYandHFS_FLG_EXT_NEWas if the insertion had succeeded.Propagate errors returned by
hfs_brec_insert()and only clear the extent flags after a successful insertion.When updating an existing extent record,
hfs_bnode_write()returns void. Validate the extent record size and use a reusable B-tree node range helper to reject invalid write parameters before callinghfs_bnode_write(). This prevents an invalid update from being treated as successful and avoids clearingHFS_FLG_EXT_DIRTYin that case.Negative-path testing in QEMU confirmed that an insertion error is propagated to the caller. Testing the existing-record path also confirmed that invalid write parameters are rejected before
HFS_FLG_EXT_DIRTYis cleared.Signed-off-by Davy Felipe <[email protected]>Sorry, I missed your suggestion about factoring the validation into a reusable helper in v3. This revision addresses it.
Changes in v4:
- Factor B-tree node range validation into a reusable helper, as suggested by Viacheslav Dubeyko.
- Keep the extent-record size check local to the extent write path.
- Preserve the error propagation and validation behavior from v3.---
fs/hfs/btree.h | 7 +++++++
fs/hfs/extent.c | 12 ++++++++++--2 files changed, 17 insertions(+), 2 deletions(-)fs/hfs/btree.h +0
-0@@ -84,6 +84,13 @@ struct hfs_find_data { int entryoffset, entrylength; };
fs/hfs/btree.h struct hfs_find_data { · line 88
Quoted 2 lines
fs/hfs/btree.h +2
-0+static inline bool hfs_bnode_is_valid_range (struct hfs_bnode *node, + int off, int len)
You can use struct hfs_find_data *fd. It can make argument list shorter. We need to find the node before read/write. So, I think we should have hfs_find_data available.
fs/hfs/btree.h struct hfs_find_data { · line 91
Quoted 3 lines
fs/hfs/btree.h +3
-0+{ +return off >=0 && len >0 && + (u64)off + len <= node->tree->node_size;
Maybe, it makes sense to check that node->tree pointers are valid?
Quoted 33 lines
fs/hfs/btree.h +2
-0+} +
/*btree.c*/
extern structhfs_btree*hfs_btree_open(structsuper_block*sb, u32 id,fs/hfs/extent.c +0
-0@@ -121,12 +121,20 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd)res =
hfs_bmap_reserve(fd->tree, fd->tree->depth + 1);
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_brec_insert(fd, HFS_I(inode)->cached_extents,
sizeof(hfs_extent_rec));fs/hfs/extent.c +1
-0+ res = hfs_brec_insert(fd, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +3
-0+ sizeof(hfs_extent_rec)); +if (res) +return res;HFS_I(inode)->flags &=
~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW);
} else {
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fd->entryoffset,fd->entrylength);fs/hfs/extent.c +2
-0+if (fd->entrylength !=sizeof (hfs_extent_rec) || + !hfs_bnode_is_valid_range(fd->bnode, fd-Quoted 1 line
entryoffset,
fs/hfs/extent.c static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) · line 134
Quoted 1 line
fs/hfs/extent.c +1
-0+ fd->entrylength))
I think we can share with hfs_bnode_is_valid_range() expected size sizeof(hfs_extent_rec) and hfs_bnode_is_valid_range() will be able to check the fd->entrylength. What do you think?
Thanks,
Slava.
Quoted 7 lines
fs/hfs/extent.c +2
-0+return -EIO ; + hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +1
-0+ fd->entryoffset, fd->entrylength);HFS_I(inode)->flags &= ~HFS_FLG_EXT_DIRTY; }
return 0;
Re: [PATCH v4] hfs: handle extent B-tree write errors
Thanks, that makes sense. Sorry, I missed that point in the previous revision.
Using struct hfs_find_data *fd will make the helper interface shorter and also allow the validation to stay in one place.
I’ll update the helper to validate the bnode/tree pointers, the entry offset and length, and compare fd->entrylength against the expected size passed by the caller. Then the extent path can simply call it with sizeof(hfs_extent_rec).
I’ll send an updated revision.
Thanks,
Davy Felipe
Em 24/09/2026 16:23, Viacheslav Dubeyko escreveu:
Quoted 107 lines · Davy Felipe
Quoted 44 lines
hfs_brec_insert()may fail while inserting a new extent record, but__hfs_ext_write_extent()currently ignores its return value and clearsHFS_FLG_EXT_DIRTYandHFS_FLG_EXT_NEWas if the insertion had succeeded.Propagate errors returned by
hfs_brec_insert()and only clear the extent flags after a successful insertion.When updating an existing extent record,
hfs_bnode_write()returns void. Validate the extent record size and use a reusable B-tree node range helper to reject invalid write parameters before callinghfs_bnode_write(). This prevents an invalid update from being treated as successful and avoids clearingHFS_FLG_EXT_DIRTYin that case.Negative-path testing in QEMU confirmed that an insertion error is propagated to the caller. Testing the existing-record path also confirmed that invalid write parameters are rejected before
HFS_FLG_EXT_DIRTYis cleared.Signed-off-by Davy Felipe <[email protected]>Sorry, I missed your suggestion about factoring the validation into a reusable helper in v3. This revision addresses it.
Changes in v4:
- Factor B-tree node range validation into a reusable helper, as suggested by Viacheslav Dubeyko.
- Keep the extent-record size check local to the extent write path.
- Preserve the error propagation and validation behavior from v3.---
fs/hfs/btree.h | 7 +++++++
fs/hfs/extent.c | 12 ++++++++++--2 files changed, 17 insertions(+), 2 deletions(-)fs/hfs/btree.h +0
-0@@ -84,6 +84,13 @@ struct hfs_find_data { int entryoffset, entrylength; };fs/hfs/btree.h struct hfs_find_data { · line 88
Quoted 2 lines
fs/hfs/btree.h +2
-0+static inline bool hfs_bnode_is_valid_range (struct hfs_bnode *node, + int off, int len)You can use struct
hfs_find_data*fd. It can make argument list shorter. We need to find the node before read/write. So, I think we should havehfs_find_dataavailable.fs/hfs/btree.h struct hfs_find_data { · line 91
Quoted 3 lines
fs/hfs/btree.h +3
-0+{ +return off >=0 && len >0 && + (u64)off + len <= node->tree->node_size;Maybe, it makes sense to check that
node->treepointers are valid?Quoted 33 lines
fs/hfs/btree.h +2
-0+} +
/*btree.c*/
extern structhfs_btree*hfs_btree_open(structsuper_block*sb, u32 id,fs/hfs/extent.c +0
-0@@ -121,12 +121,20 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd)res =
hfs_bmap_reserve(fd->tree, fd->tree->depth + 1);
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_brec_insert(fd, HFS_I(inode)->cached_extents,
sizeof(hfs_extent_rec));fs/hfs/extent.c +1
-0+ res = hfs_brec_insert(fd, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +3
-0+ sizeof(hfs_extent_rec)); +if (res) +return res;HFS_I(inode)->flags &=
~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW);
} else {
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fd->entryoffset,fd->entrylength);fs/hfs/extent.c +2
-0+if (fd->entrylength !=sizeof (hfs_extent_rec) || + !hfs_bnode_is_valid_range(fd->bnode, fd-Quoted 1 line
entryoffset,
fs/hfs/extent.c static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) · line 134
Quoted 1 line
fs/hfs/extent.c +1
-0+ fd->entrylength))I think we can share with
hfs_bnode_is_valid_range()expected sizesizeof(hfs_extent_rec)andhfs_bnode_is_valid_range()will be able to check thefd->entrylength. What do you think?Thanks,
Slava.Quoted 7 lines
fs/hfs/extent.c +2
-0+return -EIO ; + hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +1
-0+ fd->entryoffset, fd->entrylength);HFS_I(inode)->flags &= ~HFS_FLG_EXT_DIRTY; }
return 0;
PATCH v5 1/1 hfs: handle extent B-tree write errors
In reply to Viacheslav Dubeykohfs_brec_insert() may fail while inserting a new extent record, but __hfs_ext_write_extent() currently ignores its return value and clears HFS_FLG_EXT_DIRTY and HFS_FLG_EXT_NEW as if the insertion had succeeded.
Propagate errors returned by hfs_brec_insert() and only clear the extent flags after a successful insertion.
When updating an existing extent record, hfs_bnode_write() returns void. Validate the extent write parameters before calling it so an invalid update is reported as -EIO instead of being treated as successful.
Use a reusable B-tree node range helper that takes the find data and the expected record size. The helper validates the bnode and tree pointers, entry offset and entry length, and ensures that the write range fits within the node.
Negative-path testing in QEMU confirmed that an insertion error is propagated to the caller. Testing the existing-record path also confirmed that invalid write parameters are rejected before HFS_FLG_EXT_DIRTY is cleared.
Thanks for the feedback. I updated the helper to take struct hfs_find_data and the expected entry size so the bnode/tree, entry offset and entry length validation stay in one place.
Changes in v5:
- Pass struct hfs_find_data to hfs_bnode_is_valid_range().
- Validate the bnode and tree pointers in the helper.
- Pass the expected entry size and validate fd->entrylength there.
- Simplify the extent write path to a single validation helper call.
--- fs/hfs/btree.h | 19 +++++++++++++++++++ fs/hfs/extent.c | 10 ++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-)
fs/hfs/btree.h +19 -0
@@ -84,6 +84,25 @@ struct hfs_find_data {int entryoffset, entrylength; }; +static inline bool hfs_bnode_is_valid_range (struct hfs_find_data *fd,int expected_len) +{ +struct hfs_bnode *node; + +if (!fd) +return false; + + node = fd->bnode; +if (!node || !node->tree) +return false; + +if (expected_len <=0 || fd->entryoffset <0 || + fd->entrylength != expected_len) +return false; + +return (u64 )fd->entryoffset + fd->entrylength <= + node->tree->node_size; +} +/* btree.c */ extern struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id,
fs/hfs/extent.c +8 -2
@@ -121,12 +121,18 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) res =hfs_bmap_reserve (fd->tree, fd->tree->depth +1 );if (res)return res; -hfs_brec_insert (fd,HFS_I (inode)->cached_extents,sizeof (hfs_extent_rec)); + res =hfs_brec_insert (fd,HFS_I (inode)->cached_extents, +sizeof (hfs_extent_rec)); +if (res) +return res;HFS_I (inode)->flags &= ~(HFS_FLG_EXT_DIRTY |HFS_FLG_EXT_NEW ); }else {if (res)return res; -hfs_bnode_write (fd->bnode,HFS_I (inode)->cached_extents, fd->entryoffset, fd->entrylength); +if (!hfs_bnode_is_valid_range (fd,sizeof (hfs_extent_rec))) +return -EIO ; +hfs_bnode_write (fd->bnode,HFS_I (inode)->cached_extents, + fd->entryoffset, fd->entrylength);HFS_I (inode)->flags &= ~HFS_FLG_EXT_DIRTY ; }return 0 ;
Signature · 1 line
2.55.0
Viacheslav Dubeyko
maintainerRe: [PATCH v5] hfs: handle extent B-tree write errors
Quoted 19 lines · Davy Felipe
hfs_brec_insert()may fail while inserting a new extent record, but__hfs_ext_write_extent()currently ignores its return value and clearsHFS_FLG_EXT_DIRTYandHFS_FLG_EXT_NEWas if the insertion had succeeded.Propagate errors returned by
hfs_brec_insert()and only clear the extent flags after a successful insertion.When updating an existing extent record,
hfs_bnode_write()returns void. Validate the extent write parameters before calling it so an invalid update is reported as-EIOinstead of being treated as successful.Use a reusable B-tree node range helper that takes the find data and the expected record size. The helper validates the bnode and tree pointers, entry offset and entry length, and ensures that the write range fits within the node.
Quoted 6 lines
Negative-path testing in QEMU confirmed that an insertion error is propagated to the caller. Testing the existing-record path also confirmed that invalid write parameters are rejected before
HFS_FLG_EXT_DIRTYis cleared.Signed-off-by Davy Felipe <[email protected]>
___
Quoted 20 lines
Thanks for the feedback. I updated the helper to take struct
hfs_find_dataand the expected entry size so the bnode/tree, entry offset and entry length validation stay in one place.Changes in v5:
- Pass structhfs_find_datatohfs_bnode_is_valid_range().
- Validate the bnode and tree pointers in the helper.
- Pass the expected entry size and validatefd->entrylengththere.
- Simplify the extent write path to a single validation helper call.---
fs/hfs/btree.h | 19 +++++++++++++++++++
fs/hfs/extent.c | 10 ++++++++--2 files changed, 27 insertions(+), 2 deletions(-)fs/hfs/btree.h +0
-0
fs/hfs/btree.h struct hfs_find_data { · line 85
Quoted 2 lines
fs/hfs/btree.h +0
-0@@ -84,6 +84,25 @@ struct hfs_find_data { int entryoffset, entrylength; };
Mostly, I like the patch. But I have some cosmetic remarks for discussion because I don't want to change the patch without your consent.
Quoted 2 lines
+static inline bool hfs_bnode_is_valid_range(structhfs_find_data
Current name sounds slightly not natural. :) We can consider is_hfs_bnode_range_valid(). What do you think? Maybe you have something better in mind?
Quoted 1 line
*fd, int
expected_len)
Do we really need to use integer for expected_len? Why not size_t? Any particular reason?
fs/hfs/btree.h struct hfs_find_data { · line 98
Quoted 11 lines
fs/hfs/btree.h +11
-0+{ +struct hfs_bnode *node; + +if (!fd) +return false; + + node = fd->bnode; +if (!node || !node->tree) +return false; + + if (expected_len <= 0 || fd->entryoffset < 0 ||
If we have size_t, then we don't need to check that expected_len < 0.
fs/hfs/btree.h struct hfs_find_data { · line 99
Quoted 1 line
fs/hfs/btree.h +1
-0+ fd->entrylength != expected_len)
Do we need to check that expected_len == 0? Should fd->entrylength != expected_len be enough?
Quoted 36 lines
fs/hfs/btree.h +6
-0+return false; + +return (u64 )fd->entryoffset + fd->entrylength <= + node->tree->node_size; +} +
/*btree.c*/
extern structhfs_btree*hfs_btree_open(structsuper_block*sb, u32 id,fs/hfs/extent.c +0
-0@@ -121,12 +121,18 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd)res =
hfs_bmap_reserve(fd->tree, fd->tree->depth + 1);
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_brec_insert(fd, HFS_I(inode)->cached_extents,
sizeof(hfs_extent_rec));fs/hfs/extent.c +1
-0+ res = hfs_brec_insert(fd, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +3
-0+ sizeof(hfs_extent_rec)); +if (res) +return res;HFS_I(inode)->flags &=
~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW);
} else {
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fd->entryoffset,fd->entrylength);fs/hfs/extent.c +1
-0+ if (!hfs_bnode_is_valid_range(fd,
sizeof(hfs_extent_rec)))
fs/hfs/extent.c static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) · line 133
Quoted 1 line
fs/hfs/extent.c +1
-0+return -EIO ;
I am started to guess. Is -EIO correct error code here? It is still operation in memory and we are checking the correctness of struct hfs_find_data content. Maybe, we need to consider another error code here? What's about -ERANGE? What is your preference?
Thanks,
Slava.
Quoted 6 lines
fs/hfs/extent.c +1
-0+ hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +1
-0+ fd->entryoffset, fd->entrylength);HFS_I(inode)->flags &= ~HFS_FLG_EXT_DIRTY; }
return 0;
PATCH v6 1/1 hfs: handle extent B-tree write errors
In reply to Davy Felipehfs_brec_insert() may fail while inserting a new extent record, but __hfs_ext_write_extent() currently ignores its return value and clears HFS_FLG_EXT_DIRTY and HFS_FLG_EXT_NEW as if the insertion had succeeded.
Propagate errors returned by hfs_brec_insert() and only clear the extent flags after a successful insertion.
When updating an existing extent record, hfs_bnode_write() returns void. Validate the extent write parameters before calling it so an invalid update is reported as -ERANGE instead of being treated as successful.
Use a reusable B-tree node range helper that takes the find data and the expected record size. The helper validates the bnode and tree pointers, entry offset and entry length, and ensures that the write range fits within the node.
Negative-path testing in QEMU confirmed that an insertion error is propagated to the caller. Testing the existing-record path also confirmed that invalid write parameters are rejected before HFS_FLG_EXT_DIRTY is cleared.
Changes in v6:
- Rename hfs_bnode_is_valid_range() to is_hfs_bnode_range_valid().
- Use size_t for the expected record size.
- Drop the redundant expected_len validity check.
- Return -ERANGE for invalid find-data/range parameters.
- Incorporate the final review feedback from Viacheslav Dubeyko.
--- fs/hfs/btree.h | 19 +++++++++++++++++++ fs/hfs/extent.c | 10 ++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-)
fs/hfs/btree.h +19 -0
@@ -84,6 +84,25 @@ struct hfs_find_data {int entryoffset, entrylength; }; +static inline bool is_hfs_bnode_range_valid (struct hfs_find_data *fd, +size_t expected_len) +{ +struct hfs_bnode *node; + +if (!fd) +return false; + + node = fd->bnode; +if (!node || !node->tree) +return false; + +if (fd->entryoffset <0 || fd->entrylength != expected_len) +return false; + +return (u64 )fd->entryoffset + fd->entrylength <= + node->tree->node_size; +} +/* btree.c */ extern struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id,
fs/hfs/extent.c +8 -2
@@ -121,12 +121,18 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) res =hfs_bmap_reserve (fd->tree, fd->tree->depth +1 );if (res)return res; -hfs_brec_insert (fd,HFS_I (inode)->cached_extents,sizeof (hfs_extent_rec)); + res =hfs_brec_insert (fd,HFS_I (inode)->cached_extents, +sizeof (hfs_extent_rec)); +if (res) +return res;HFS_I (inode)->flags &= ~(HFS_FLG_EXT_DIRTY |HFS_FLG_EXT_NEW ); }else {if (res)return res; -hfs_bnode_write (fd->bnode,HFS_I (inode)->cached_extents, fd->entryoffset, fd->entrylength); +if (!is_hfs_bnode_range_valid (fd,sizeof (hfs_extent_rec))) +return -ERANGE ; +hfs_bnode_write (fd->bnode,HFS_I (inode)->cached_extents, + fd->entryoffset, fd->entrylength);HFS_I (inode)->flags &= ~HFS_FLG_EXT_DIRTY ; }return 0 ;
Signature · 1 line
2.55.0
Re: [PATCH v6] hfs: handle extent B-tree write errors
Quoted 99 lines · Davy Felipe
hfs_brec_insert()may fail while inserting a new extent record, but__hfs_ext_write_extent()currently ignores its return value and clearsHFS_FLG_EXT_DIRTYandHFS_FLG_EXT_NEWas if the insertion had succeeded.Propagate errors returned by
hfs_brec_insert()and only clear the extent flags after a successful insertion.When updating an existing extent record,
hfs_bnode_write()returns void. Validate the extent write parameters before calling it so an invalid update is reported as-ERANGEinstead of being treated as successful.Use a reusable B-tree node range helper that takes the find data and the expected record size. The helper validates the bnode and tree pointers, entry offset and entry length, and ensures that the write range fits within the node.
Negative-path testing in QEMU confirmed that an insertion error is propagated to the caller. Testing the existing-record path also confirmed that invalid write parameters are rejected before
HFS_FLG_EXT_DIRTYis cleared.Signed-off-by Davy Felipe <[email protected]>Changes in v6:
- Renamehfs_bnode_is_valid_range()tois_hfs_bnode_range_valid().
- Usesize_tfor the expected record size.
- Drop the redundantexpected_lenvalidity check.
- Return-ERANGEfor invalid find-data/range parameters.
- Incorporate the final review feedback from Viacheslav Dubeyko.---
fs/hfs/btree.h | 19 +++++++++++++++++++
fs/hfs/extent.c | 10 ++++++++--2 files changed, 27 insertions(+), 2 deletions(-)fs/hfs/btree.h +0
-0@@ -84,6 +84,25 @@ struct hfs_find_data { int entryoffset, entrylength; };
+static inline bool is_hfs_bnode_range_valid(structhfs_find_data*fd,fs/hfs/btree.h +18
-0+ size_t expected_len) +{ +struct hfs_bnode *node; + +if (!fd) +return false; + + node = fd->bnode; +if (!node || !node->tree) +return false; + +if (fd->entryoffset <0 || fd->entrylength != expected_len) +return false; + +return (u64 )fd->entryoffset + fd->entrylength <= + node->tree->node_size; +} +
/*btree.c*/
extern structhfs_btree*hfs_btree_open(structsuper_block*sb, u32 id,fs/hfs/extent.c +0
-0@@ -121,12 +121,18 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd)res =
hfs_bmap_reserve(fd->tree, fd->tree->depth + 1);
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_brec_insert(fd, HFS_I(inode)->cached_extents,
sizeof(hfs_extent_rec));fs/hfs/extent.c +1
-0+ res = hfs_brec_insert(fd, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +3
-0+ sizeof(hfs_extent_rec)); +if (res) +return res;HFS_I(inode)->flags &=
~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW);
} else {
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fd->entryoffset,fd->entrylength);fs/hfs/extent.c +1
-0+ if (!is_hfs_bnode_range_valid(fd,
sizeof(hfs_extent_rec)))fs/hfs/extent.c +2
-0+return -ERANGE ; + hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +1
-0+ fd->entryoffset, fd->entrylength);
Quoted 3 lines
HFS_I(inode)->flags &= ~HFS_FLG_EXT_DIRTY; }
return 0;
Looks good.
Thanks,
Slava.
Re: [PATCH v4] hfs: handle extent B-tree write errors
In reply to Viacheslav DubeykoHi Slava,
Thanks for the review.
Your suggested changes look reasonable to me. Please feel free to make these adjustments to the patch if you prefer.
I am fine with renaming the helper and using size_t for expected_len. Regarding the error code, I am also fine with using -ERANGE if you think it better represents this condition.
Thanks,
Davy Felipe
Em 24/09/2026 16:23, Viacheslav Dubeyko escreveu:
Quoted 107 lines · Davy Felipe
Quoted 44 lines
hfs_brec_insert()may fail while inserting a new extent record, but__hfs_ext_write_extent()currently ignores its return value and clearsHFS_FLG_EXT_DIRTYandHFS_FLG_EXT_NEWas if the insertion had succeeded.Propagate errors returned by
hfs_brec_insert()and only clear the extent flags after a successful insertion.When updating an existing extent record,
hfs_bnode_write()returns void. Validate the extent record size and use a reusable B-tree node range helper to reject invalid write parameters before callinghfs_bnode_write(). This prevents an invalid update from being treated as successful and avoids clearingHFS_FLG_EXT_DIRTYin that case.Negative-path testing in QEMU confirmed that an insertion error is propagated to the caller. Testing the existing-record path also confirmed that invalid write parameters are rejected before
HFS_FLG_EXT_DIRTYis cleared.Signed-off-by Davy Felipe <[email protected]>Sorry, I missed your suggestion about factoring the validation into a reusable helper in v3. This revision addresses it.
Changes in v4:
- Factor B-tree node range validation into a reusable helper, as suggested by Viacheslav Dubeyko.
- Keep the extent-record size check local to the extent write path.
- Preserve the error propagation and validation behavior from v3.---
fs/hfs/btree.h | 7 +++++++
fs/hfs/extent.c | 12 ++++++++++--2 files changed, 17 insertions(+), 2 deletions(-)fs/hfs/btree.h +0
-0@@ -84,6 +84,13 @@ struct hfs_find_data { int entryoffset, entrylength; };fs/hfs/btree.h struct hfs_find_data { · line 88
Quoted 2 lines
fs/hfs/btree.h +2
-0+static inline bool hfs_bnode_is_valid_range (struct hfs_bnode *node, + int off, int len)You can use struct
hfs_find_data*fd. It can make argument list shorter. We need to find the node before read/write. So, I think we should havehfs_find_dataavailable.fs/hfs/btree.h struct hfs_find_data { · line 91
Quoted 3 lines
fs/hfs/btree.h +3
-0+{ +return off >=0 && len >0 && + (u64)off + len <= node->tree->node_size;Maybe, it makes sense to check that
node->treepointers are valid?Quoted 33 lines
fs/hfs/btree.h +2
-0+} +
/*btree.c*/
extern structhfs_btree*hfs_btree_open(structsuper_block*sb, u32 id,fs/hfs/extent.c +0
-0@@ -121,12 +121,20 @@ static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd)res =
hfs_bmap_reserve(fd->tree, fd->tree->depth + 1);
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_brec_insert(fd, HFS_I(inode)->cached_extents,
sizeof(hfs_extent_rec));fs/hfs/extent.c +1
-0+ res = hfs_brec_insert(fd, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +3
-0+ sizeof(hfs_extent_rec)); +if (res) +return res;HFS_I(inode)->flags &=
~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW);
} else {
if (res)
return res;fs/hfs/extent.c +0
-1- hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fd->entryoffset,fd->entrylength);fs/hfs/extent.c +2
-0+if (fd->entrylength !=sizeof (hfs_extent_rec) || + !hfs_bnode_is_valid_range(fd->bnode, fd-Quoted 1 line
entryoffset,
fs/hfs/extent.c static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd) · line 134
Quoted 1 line
fs/hfs/extent.c +1
-0+ fd->entrylength))I think we can share with
hfs_bnode_is_valid_range()expected sizesizeof(hfs_extent_rec)andhfs_bnode_is_valid_range()will be able to check thefd->entrylength. What do you think?Thanks,
Slava.Quoted 7 lines
fs/hfs/extent.c +2
-0+return -EIO ; + hfs_bnode_write(fd->bnode, HFS_I(inode)-Quoted 1 line
cached_extents,fs/hfs/extent.c +1
-0+ fd->entryoffset, fd->entrylength);HFS_I(inode)->flags &= ~HFS_FLG_EXT_DIRTY; }
return 0;