btrfs: handle -ENOMEM errors in some synchronous dirops without aborting
v2 cover btrfs: handle -ENOMEM errors in some synchronous dirops without aborting
We've had a (relatively small) number of ENOMEM btrfs aborts occur in synchronous directory morphing codepaths. It's not terribly common, but there are a few places where an memory allocation failure results in an abort.
This patchset reworks the code to do the allocations up front, before the point where we'd have to abort the fs if it fails.
This does not cover all potential cases where this can currently occur:
In particular, a rename that overwrites the target can still abort the fs if a memory allocation fails. Fixing that is substantially more work, unfortunately.
This also doesn't cover orphaning a new inode on failure (which can trigger new memory allocations), so this series is designed to work in conjunction with with Boris' GFP_NOFAIL series [1].
AFAICT, these are ancient problems, dating back at least to ~2011. I didn't bother adding Fixes: tags.
AI disclosure: I made heavy use of an LLM in this patchset, from drafting the initial series to helping test it.
[1] https://lore.kernel.org/linux-btrfs/[email protected]/
---
Changes in v2:
- Use an on-stack btrfs_path in btrfs_insert_orphan_item() so the ENOMEM recovery does not itself fail on a path allocation.
- Simplify the recovery in btrfs_create_new_inode() to rely on btrfs_orphan_add()'s internal abort instead of aborting twice.
- Add ALLOW_ERROR_INJECTION() on btrfs_prealloc_delayed_dir_index() and a new fstest (btrfs/341) to exercise the ENOMEM path.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Jeff Layton (5):
btrfs: use an on-stack path in btrfs_insert_orphan_item()
btrfs: split btrfs_insert_delayed_dir_index() into prealloc and commit phases
btrfs: pre-allocate delayed dir index before btree modification
btrfs: handle ENOMEM from btrfs_insert_dir_item() without aborting
btrfs: pre-allocate delayed dir index for non-overwrite rename
fs/btrfs/btrfs_inode.h | 4 ++- fs/btrfs/delayed-inode.c | 90 +++++++++++++++++++++++++++++++++++------------- fs/btrfs/delayed-inode.h | 22 +++++++++--- fs/btrfs/dir-item.c | 30 ++++++++++++---- fs/btrfs/dir-item.h | 5 +-- fs/btrfs/inode.c | 57 ++++++++++++++++++++++++------ fs/btrfs/orphan.c | 11 +++--- fs/btrfs/transaction.c | 2 +- fs/btrfs/tree-log.c | 4 +-- 9 files changed, 168 insertions(+), 57 deletions(-)
---
base-commit: 76a2100a0e8d81636586449024e7558656300481
change-id: 20260715-btrfs-enomem-988f2cc36ffd
Best regards,
Signature · 1 line
Jeff Layton <[email protected]>
PATCH v2 1/5 btrfs: use an on-stack path in btrfs_insert_orphan_item()
btrfs_insert_orphan_item() allocated a btrfs_path with btrfs_alloc_path() which returns -ENOMEM on failure. It is called from btrfs_orphan_add(), so a path allocation failure there turns a recoverable error into a transaction abort.
btrfs_path is only ~112 bytes, so allocate it on the stack instead.
--- fs/btrfs/orphan.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
fs/btrfs/orphan.c +5 -6
@@ -9,18 +9,17 @@int btrfs_insert_orphan_item (struct btrfs_trans_handle *trans,struct btrfs_root *root,u64 offset) { -BTRFS_PATH_AUTO_FREE (path); +struct btrfs_path path = {0 };struct btrfs_key key; +int ret; key.objectid =BTRFS_ORPHAN_OBJECTID ; key.type =BTRFS_ORPHAN_ITEM_KEY ; key.offset = offset; - path =btrfs_alloc_path (); -if (!path) -return -ENOMEM ; - -return btrfs_insert_empty_item (trans, root, path, &key,0 ); + ret =btrfs_insert_empty_item (trans, root, &path, &key,0 ); +btrfs_release_path (&path); +return ret; } int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
Signature · 1 line
2.55.0
Re: [PATCH v2 1/5] btrfs: use an on-stack path in btrfs_insert_orphan_item()
Quoted 10 lines · Jeff Layton
btrfs_insert_orphan_item()allocated abtrfs_pathwithbtrfs_alloc_path()which returns-ENOMEMon failure. It is called frombtrfs_orphan_add(), so a path allocation failure there turns a recoverable error into a transaction abort.
btrfs_pathis only ~112 bytes, so allocate it on the stack instead.Assisted-by LLMSuggested-by Qu Wenruo <[email protected]>Signed-off-by Jeff Layton <[email protected]>
Thanks,
Qu
Quoted 33 lines
--- fs/btrfs/orphan.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)fs/btrfs/orphan.c +5
-6@@ -9,18 +9,17 @@int btrfs_insert_orphan_item (struct btrfs_trans_handle *trans,struct btrfs_root *root,u64 offset) { -BTRFS_PATH_AUTO_FREE (path); +struct btrfs_path path = {0 };struct btrfs_key key; +int ret; key.objectid =BTRFS_ORPHAN_OBJECTID ; key.type =BTRFS_ORPHAN_ITEM_KEY ; key.offset = offset; - path =btrfs_alloc_path (); -if (!path) -return -ENOMEM ; - -return btrfs_insert_empty_item (trans, root, path, &key,0 ); + ret =btrfs_insert_empty_item (trans, root, &path, &key,0 ); +btrfs_release_path (&path); +return ret; } int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
PATCH v2 2/5 btrfs: split btrfs_insert_delayed_dir_index() into prealloc and commit phases
In reply to Jeff LaytonSplit btrfs_insert_delayed_dir_index() into three functions using a new btrfs_dir_index_prealloc struct to bundle the pre-allocated resources:
- btrfs_prealloc_delayed_dir_index(): performs the two GFP_NOFS allocations (delayed node + delayed item) that can fail with -ENOMEM.
- btrfs_insert_delayed_dir_index_prealloc(): populates the item data, inserts into the rb-tree, and reserves metadata space. Cannot fail with -ENOMEM since all allocations were done in the prealloc step.
- btrfs_free_delayed_dir_index_prealloc(): frees pre-allocated resources when the caller's btree insertion fails.
The original btrfs_insert_delayed_dir_index() is refactored into a thin wrapper that calls the prealloc and commit functions.
This split allows callers to move the fallible memory allocations before the point of no return (the DIR_ITEM btree insertion), so that -ENOMEM can be returned cleanly without aborting the transaction.
--- fs/btrfs/delayed-inode.c | 111 +++++++++++++++++++++++++++++++++++++---------- fs/btrfs/delayed-inode.h | 17 ++++++++ 2 files changed, 104 insertions(+), 24 deletions(-)
fs/btrfs/delayed-inode.c +87 -24
@@ -6,6 +6,7 @@#include <linux/slab.h> #include <linux/iversion.h> +#include <linux/error-injection.h> #include "ctree.h" #include "fs.h" #include "messages.h"
@@ -1469,35 +1470,74 @@ static void btrfs_release_dir_index_item_space(struct btrfs_trans_handle *trans) trans->bytes_reserved -= bytes; } -/* Will return 0, -ENOMEM or -EEXIST (index number collision, unexpected). */ -int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, -const char *name,int name_len, -struct btrfs_inode *dir, -const struct btrfs_disk_key *disk_key,u8 flags, -u64 index) +/* +* Pre-allocate a delayed node and delayed item for a dir index insertion. +* Call this before modifying the btree so that ENOMEM can be returned +* before any on-disk state has changed. +* +* Returns 0 on success, -ENOMEM on allocation failure. +*/ +int btrfs_prealloc_delayed_dir_index (struct btrfs_inode *dir, +const char *name,int name_len, +struct btrfs_dir_index_prealloc *prealloc) +{ +struct btrfs_delayed_node *node; +struct btrfs_delayed_item *item; + + node =btrfs_get_or_create_delayed_node (dir, &prealloc->tracker); +if (IS_ERR (node)) +return PTR_ERR (node); + + item =btrfs_alloc_delayed_item (sizeof (struct btrfs_dir_item ) + name_len, + node,BTRFS_DELAYED_INSERTION_ITEM ); +if (!item) { +btrfs_release_delayed_node (node, &prealloc->tracker); +return -ENOMEM ; + } + + prealloc->node = node; + prealloc->item = item; +return 0 ; +} +ALLOW_ERROR_INJECTION (btrfs_prealloc_delayed_dir_index,ERRNO ); + +/* +* Free resources from btrfs_prealloc_delayed_dir_index() when the btree +* insertion failed and we will not commit the delayed dir index. +*/ +void btrfs_free_delayed_dir_index_prealloc (struct btrfs_trans_handle *trans, +struct btrfs_dir_index_prealloc *prealloc) { +btrfs_release_delayed_item (prealloc->item); +btrfs_release_dir_index_item_space (trans); +btrfs_release_delayed_node (prealloc->node, &prealloc->tracker); +} + +/* +* Commit a pre-allocated delayed dir index item. The delayed node and item +* must have been returned by btrfs_prealloc_delayed_dir_index(). This +* populates the item, adds it to the delayed node's rb-tree, and reserves +* metadata space. It cannot fail with ENOMEM. +* +* Will return 0 or -EEXIST (index number collision, unexpected). +*/ +int btrfs_insert_delayed_dir_index_prealloc (struct btrfs_trans_handle *trans, +struct btrfs_inode *dir, +struct btrfs_dir_index_prealloc *prealloc, +const struct btrfs_disk_key *disk_key, +u8 flags,u64 index) +{ +struct btrfs_delayed_node *delayed_node = prealloc->node; +struct btrfs_ref_tracker *tracker = &prealloc->tracker; +struct btrfs_delayed_item *delayed_item = prealloc->item;struct btrfs_fs_info *fs_info = trans->fs_info;const unsigned int leaf_data_size =BTRFS_LEAF_DATA_SIZE (fs_info); -struct btrfs_delayed_node *delayed_node; -struct btrfs_ref_tracker delayed_node_tracker; -struct btrfs_delayed_item *delayed_item; +const int name_len = delayed_item->data_len -sizeof (struct btrfs_dir_item );struct btrfs_dir_item *dir_item;bool reserve_leaf_space;u32 data_len;int ret; - delayed_node =btrfs_get_or_create_delayed_node (dir, &delayed_node_tracker); -if (IS_ERR (delayed_node)) -return PTR_ERR (delayed_node); - - delayed_item =btrfs_alloc_delayed_item (sizeof (*dir_item) + name_len, - delayed_node, -BTRFS_DELAYED_INSERTION_ITEM ); -if (!delayed_item) { - ret = -ENOMEM ; - goto release_node; - } - delayed_item->index = index; dir_item = (struct btrfs_dir_item *)delayed_item->data;
@@ -1506,7 +1546,7 @@ int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,btrfs_set_stack_dir_data_len (dir_item,0 );btrfs_set_stack_dir_name_len (dir_item, name_len);btrfs_set_stack_dir_flags (dir_item, flags); -memcpy ((char *)(dir_item +1 ), name, name_len); +/* Name was already copied into delayed_item->data by the caller. */ data_len = delayed_item->data_len +sizeof (struct btrfs_item );
@@ -1524,7 +1564,9 @@ int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,if (unlikely (ret)) {btrfs_err (trans->fs_info,"error adding delayed dir index item, name: %.*s, index: %llu, root: %llu, dir: %llu, dir->index_cnt: %llu, delayed_node->index_cnt: %llu, error: %pe" , - name_len, name, index,btrfs_root_id (delayed_node->root), + name_len, + (const char *)(dir_item +1 ), + index,btrfs_root_id (delayed_node->root), delayed_node->inode_id, dir->index_cnt, delayed_node->index_cnt,ERR_PTR (ret));btrfs_release_delayed_item (delayed_item);
@@ -1562,10 +1604,31 @@ int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans,mutex_unlock (&delayed_node->mutex); release_node: -btrfs_release_delayed_node (delayed_node, &delayed_node_tracker); +btrfs_release_delayed_node (delayed_node, tracker);return ret; } +/* Will return 0, -ENOMEM or -EEXIST (index number collision, unexpected). */ +int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, +const char *name,int name_len, +struct btrfs_inode *dir, +const struct btrfs_disk_key *disk_key,u8 flags, +u64 index) +{ +struct btrfs_dir_index_prealloc prealloc; +int ret; + + ret =btrfs_prealloc_delayed_dir_index (dir, name, name_len, &prealloc); +if (ret) +return ret; + +memcpy (prealloc.item->data +sizeof (struct btrfs_dir_item ), name, + name_len); + +return btrfs_insert_delayed_dir_index_prealloc (trans, dir, &prealloc, + disk_key, flags, index); +} +static bool btrfs_delete_delayed_insertion_item (struct btrfs_delayed_node *node,u64 index) {
fs/btrfs/delayed-inode.h +17 -0
@@ -121,6 +121,23 @@ int btrfs_insert_delayed_dir_index(struct btrfs_trans_handle *trans, const struct btrfs_disk_key *disk_key, u8 flags, u64 index); +struct btrfs_dir_index_prealloc { +struct btrfs_delayed_node *node; +struct btrfs_ref_tracker tracker; +struct btrfs_delayed_item *item; +}; + +int btrfs_prealloc_delayed_dir_index (struct btrfs_inode *dir, +const char *name,int name_len, +struct btrfs_dir_index_prealloc *prealloc); +void btrfs_free_delayed_dir_index_prealloc (struct btrfs_trans_handle *trans, +struct btrfs_dir_index_prealloc *prealloc); +int btrfs_insert_delayed_dir_index_prealloc (struct btrfs_trans_handle *trans, +struct btrfs_inode *dir, +struct btrfs_dir_index_prealloc *prealloc, +const struct btrfs_disk_key *disk_key, +u8 flags,u64 index); +int btrfs_delete_delayed_dir_index (struct btrfs_trans_handle *trans,struct btrfs_inode *dir,u64 index);
Signature · 1 line
2.55.0
Re: [PATCH v2 2/5] btrfs: split btrfs_insert_delayed_dir_index() into prealloc and commit phases
Quoted 47 lines · Jeff Layton
Split
btrfs_insert_delayed_dir_index()into three functions using a newbtrfs_dir_index_preallocstruct to bundle the pre-allocated resources:-
btrfs_prealloc_delayed_dir_index(): performs the twoGFP_NOFSallocations (delayed node + delayed item) that can fail with-ENOMEM.
-btrfs_insert_delayed_dir_index_prealloc(): populates the item data, inserts into the rb-tree, and reserves metadata space. Cannot fail with-ENOMEMsince all allocations were done in the prealloc step.
-btrfs_free_delayed_dir_index_prealloc(): frees pre-allocated resources when the caller's btree insertion fails.The original
btrfs_insert_delayed_dir_index()is refactored into a thin wrapper that calls the prealloc and commit functions.This split allows callers to move the fallible memory allocations before the point of no return (the
DIR_ITEMbtree insertion), so that-ENOMEMcan be returned cleanly without aborting the transaction.Assisted-by LLMSigned-off-by Jeff Layton <[email protected]>--- fs/btrfs/delayed-inode.c | 111 +++++++++++++++++++++++++++++++++++++---------- fs/btrfs/delayed-inode.h | 17 ++++++++ 2 files changed, 104 insertions(+), 24 deletions(-)fs/btrfs/delayed-inode.c +1
-6@@ -6,6 +6,7 @@#include <linux/slab.h> #include <linux/iversion.h> +#include <linux/error-injection.h> #include "ctree.h" #include "fs.h" #include "messages.h" @@ -1469,35 +1470,74 @@ static void btrfs_release_dir_index_item_space(struct btrfs_trans_handle *trans) trans->bytes_reserved -= bytes; } -/* Will return 0, -ENOMEM or -EEXIST (index number collision, unexpected). */ -int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, -const char *name,int name_len, -struct btrfs_inode *dir, -const struct btrfs_disk_key *disk_key,u8 flags, -u64 index)
fs/btrfs/delayed-inode.c static void btrfs_release_dir_index_item_space(struct btrfs_trans_handle *trans) · line 1482
Quoted 10 lines
fs/btrfs/delayed-inode.c +10
-0+/* +* Pre-allocate a delayed node and delayed item for a dir index insertion. +* Call this before modifying the btree so that ENOMEM can be returned +* before any on-disk state has changed. +* +* Returns 0 on success, -ENOMEM on allocation failure. +*/ +int btrfs_prealloc_delayed_dir_index (struct btrfs_inode *dir, +const char *name,int name_len, +struct btrfs_dir_index_prealloc *prealloc)
We can directly return a btrfs_dir_index_prealloc pointer, which reduce the parameter list.
Otherwise looks good to me.
Thanks,
Qu
Re: [PATCH v2 2/5] btrfs: split btrfs_insert_delayed_dir_index() into prealloc and commit phases
Quoted 54 lines · Qu Wenruo' via Kernel Team
Quoted 52 lines · Jeff Layton
Split
btrfs_insert_delayed_dir_index()into three functions using a newbtrfs_dir_index_preallocstruct to bundle the pre-allocated resources:-
btrfs_prealloc_delayed_dir_index(): performs the twoGFP_NOFSallocations (delayed node + delayed item) that can fail with-ENOMEM.
-btrfs_insert_delayed_dir_index_prealloc(): populates the item data, inserts into the rb-tree, and reserves metadata space. Cannot fail with-ENOMEMsince all allocations were done in the prealloc step.
-btrfs_free_delayed_dir_index_prealloc(): frees pre-allocated resources when the caller's btree insertion fails.The original
btrfs_insert_delayed_dir_index()is refactored into a thin wrapper that calls the prealloc and commit functions.This split allows callers to move the fallible memory allocations before the point of no return (the
DIR_ITEMbtree insertion), so that-ENOMEMcan be returned cleanly without aborting the transaction.Assisted-by LLMSigned-off-by Jeff Layton <[email protected]>--- fs/btrfs/delayed-inode.c | 111 +++++++++++++++++++++++++++++++++++++---------- fs/btrfs/delayed-inode.h | 17 ++++++++ 2 files changed, 104 insertions(+), 24 deletions(-)fs/btrfs/delayed-inode.c +6
-6@@ -6,6 +6,7 @@#include <linux/slab.h> #include <linux/iversion.h> +#include <linux/error-injection.h> #include "ctree.h" #include "fs.h" #include "messages.h" @@ -1469,35 +1470,74 @@ static void btrfs_release_dir_index_item_space(struct btrfs_trans_handle *trans) trans->bytes_reserved -= bytes; } -/* Will return 0, -ENOMEM or -EEXIST (index number collision, unexpected). */ -int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, -const char *name,int name_len, -struct btrfs_inode *dir, -const struct btrfs_disk_key *disk_key,u8 flags, -u64 index) +/* +* Pre-allocate a delayed node and delayed item for a dir index insertion. +* Call this before modifying the btree so that ENOMEM can be returned +* before any on-disk state has changed. +*
Quoted 9 lines
fs/btrfs/delayed-inode.c static void btrfs_release_dir_index_item_space(struct btrfs_trans_handle *trans) · line 1482
Quoted 5 lines
fs/btrfs/delayed-inode.c +5
-0+* Returns 0 on success, -ENOMEM on allocation failure. +*/ +int btrfs_prealloc_delayed_dir_index (struct btrfs_inode *dir, +const char *name,int name_len, +struct btrfs_dir_index_prealloc *prealloc)We can directly return a
btrfs_dir_index_preallocpointer, which reduce the parameter list.
...but then we'd have to allocate it separately. It's currently allocated on the stack by the caller. I think we ought to keep it this way, but I'm willing to be convinced otherwise.
Quoted 4 lines
Otherwise looks good to me.
Reviewed-by Qu Wenruo <[email protected]>
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH v2 2/5] btrfs: split btrfs_insert_delayed_dir_index() into prealloc and commit phases
Quoted 65 lines · Jeff Layton
Quoted 54 lines · Qu Wenruo' via Kernel Team
Quoted 52 lines · Jeff Layton
Split
btrfs_insert_delayed_dir_index()into three functions using a newbtrfs_dir_index_preallocstruct to bundle the pre-allocated resources:-
btrfs_prealloc_delayed_dir_index(): performs the twoGFP_NOFSallocations (delayed node + delayed item) that can fail with-ENOMEM.
-btrfs_insert_delayed_dir_index_prealloc(): populates the item data, inserts into the rb-tree, and reserves metadata space. Cannot fail with-ENOMEMsince all allocations were done in the prealloc step.
-btrfs_free_delayed_dir_index_prealloc(): frees pre-allocated resources when the caller's btree insertion fails.The original
btrfs_insert_delayed_dir_index()is refactored into a thin wrapper that calls the prealloc and commit functions.This split allows callers to move the fallible memory allocations before the point of no return (the
DIR_ITEMbtree insertion), so that-ENOMEMcan be returned cleanly without aborting the transaction.Assisted-by LLMSigned-off-by Jeff Layton <[email protected]>--- fs/btrfs/delayed-inode.c | 111 +++++++++++++++++++++++++++++++++++++---------- fs/btrfs/delayed-inode.h | 17 ++++++++ 2 files changed, 104 insertions(+), 24 deletions(-)fs/btrfs/delayed-inode.c +6
-6@@ -6,6 +6,7 @@#include <linux/slab.h> #include <linux/iversion.h> +#include <linux/error-injection.h> #include "ctree.h" #include "fs.h" #include "messages.h" @@ -1469,35 +1470,74 @@ static void btrfs_release_dir_index_item_space(struct btrfs_trans_handle *trans) trans->bytes_reserved -= bytes; } -/* Will return 0, -ENOMEM or -EEXIST (index number collision, unexpected). */ -int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, -const char *name,int name_len, -struct btrfs_inode *dir, -const struct btrfs_disk_key *disk_key,u8 flags, -u64 index) +/* +* Pre-allocate a delayed node and delayed item for a dir index insertion. +* Call this before modifying the btree so that ENOMEM can be returned +* before any on-disk state has changed. +* Quoted 9 lines
fs/btrfs/delayed-inode.c static void btrfs_release_dir_index_item_space(struct btrfs_trans_handle *trans) · line 1482
Quoted 5 lines
fs/btrfs/delayed-inode.c +5
-0+* Returns 0 on success, -ENOMEM on allocation failure. +*/ +int btrfs_prealloc_delayed_dir_index (struct btrfs_inode *dir, +const char *name,int name_len, +struct btrfs_dir_index_prealloc *prealloc)We can directly return a
btrfs_dir_index_preallocpointer, which reduce the parameter list.
Quoted 3 lines
...but then we'd have to allocate it separately. It's currently allocated on the stack by the caller. I think we ought to keep it this way, but I'm willing to be convinced otherwise.
In the end, there are only two callsites using on-stack memory for prealloc structure, one in btrfs_rename() and one in btrfs_insert_dir_item().
So it would be not that hard to convert to heap memory based solution.
One thing I found a little weird is related to the lifespan management of btrfs_dir_index_prealloc.
We have several call sites doing things like:
struct btrfs_dir_index_prealloc prealloc = { };
ret = btrfs_prealloc_delayed_dir_index();
ret = btrfs_add_link(..., prealloc.item ? prealloc : NULL);
prealloc.item = NULL;Which looks a little weird to me, as we're using item to indicate if the structure is properly allocated, not the structure itself.
If we go regular heap memory allocation, it would be more straightforward and more common:
struct btrfs_dir_index_prealloc *prealloc = NULL; prealloc = btrfs_prealloc_delayed_dir_index(); ret = btrfs_add_link(..., prealloc); prealloc = NULL;
Feel free to ignore this one, it's mostly preference.
Thanks,
Qu
Quoted 6 lines
Quoted 5 lines
Otherwise looks good to me.
Reviewed-by Qu Wenruo <[email protected]>
Re: [PATCH v2 2/5] btrfs: split btrfs_insert_delayed_dir_index() into prealloc and commit phases
Quoted 96 lines · Qu Wenruo
Quoted 65 lines · Jeff Layton
Quoted 54 lines · Qu Wenruo' via Kernel Team
Quoted 52 lines · Jeff Layton
Split
btrfs_insert_delayed_dir_index()into three functions using a newbtrfs_dir_index_preallocstruct to bundle the pre-allocated resources:-
btrfs_prealloc_delayed_dir_index(): performs the twoGFP_NOFSallocations (delayed node + delayed item) that can fail with-ENOMEM.
-btrfs_insert_delayed_dir_index_prealloc(): populates the item data, inserts into the rb-tree, and reserves metadata space. Cannot fail with-ENOMEMsince all allocations were done in the prealloc step.
-btrfs_free_delayed_dir_index_prealloc(): frees pre-allocated resources when the caller's btree insertion fails.The original
btrfs_insert_delayed_dir_index()is refactored into a thin wrapper that calls the prealloc and commit functions.This split allows callers to move the fallible memory allocations before the point of no return (the
DIR_ITEMbtree insertion), so that-ENOMEMcan be returned cleanly without aborting the transaction.Assisted-by LLMSigned-off-by Jeff Layton <[email protected]>--- fs/btrfs/delayed-inode.c | 111 +++++++++++++++++++++++++++++++++++++---------- fs/btrfs/delayed-inode.h | 17 ++++++++ 2 files changed, 104 insertions(+), 24 deletions(-)fs/btrfs/delayed-inode.c +6
-6@@ -6,6 +6,7 @@#include <linux/slab.h> #include <linux/iversion.h> +#include <linux/error-injection.h> #include "ctree.h" #include "fs.h" #include "messages.h" @@ -1469,35 +1470,74 @@ static void btrfs_release_dir_index_item_space(struct btrfs_trans_handle *trans) trans->bytes_reserved -= bytes; } -/* Will return 0, -ENOMEM or -EEXIST (index number collision, unexpected). */ -int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, -const char *name,int name_len, -struct btrfs_inode *dir, -const struct btrfs_disk_key *disk_key,u8 flags, -u64 index) +/* +* Pre-allocate a delayed node and delayed item for a dir index insertion. +* Call this before modifying the btree so that ENOMEM can be returned +* before any on-disk state has changed. +* Quoted 9 lines
fs/btrfs/delayed-inode.c static void btrfs_release_dir_index_item_space(struct btrfs_trans_handle *trans) · line 1482
Quoted 5 lines
fs/btrfs/delayed-inode.c +5
-0+* Returns 0 on success, -ENOMEM on allocation failure. +*/ +int btrfs_prealloc_delayed_dir_index (struct btrfs_inode *dir, +const char *name,int name_len, +struct btrfs_dir_index_prealloc *prealloc)We can directly return a
btrfs_dir_index_preallocpointer, which reduce the parameter list.Quoted 3 lines
...but then we'd have to allocate it separately. It's currently allocated on the stack by the caller. I think we ought to keep it this way, but I'm willing to be convinced otherwise.
In the end, there are only two callsites using on-stack memory for prealloc structure, one in
btrfs_rename()and one inbtrfs_insert_dir_item().So it would be not that hard to convert to heap memory based solution.
One thing I found a little weird is related to the lifespan management of
btrfs_dir_index_prealloc.We have several call sites doing things like:
struct btrfs_dir_index_prealloc prealloc = { }; ret = btrfs_prealloc_delayed_dir_index(); ret = btrfs_add_link(..., prealloc.item ? prealloc : NULL); prealloc.item = NULL;Which looks a little weird to me, as we're using item to indicate if the structure is properly allocated, not the structure itself.
If we go regular heap memory allocation, it would be more straightforward and more common:
Quoted 9 lines
struct btrfs_dir_index_prealloc *prealloc = NULL; prealloc = btrfs_prealloc_delayed_dir_index(); ret = btrfs_add_link(..., prealloc); prealloc = NULL;Feel free to ignore this one, it's mostly preference.
It's a good point. It is more natural to be able to just look and say "NULL pointer means no prealloc".
OTOH, the entire reason we're doing this is to deal with low-memory situations. Do we want to add another heap allocation into this codepath?
Either way, I'll think about it.
Thanks again for the review!
--
Jeff Layton <[email protected]>
PATCH v2 3/5 btrfs: pre-allocate delayed dir index before btree modification
In reply to Jeff LaytonMove the delayed dir index allocation in btrfs_insert_dir_item() before the insert_with_overflow() call that modifies the btree. Previously, the allocations happened after the DIR_ITEM was already inserted, meaning an ENOMEM failure left the btree in a partially-modified state that could only be resolved by aborting the transaction.
Add an optional caller-provided btrfs_dir_index_prealloc parameter to btrfs_insert_dir_item(). When non-NULL, ownership of the prealloc transfers to btrfs_insert_dir_item(). When NULL, it allocates internally. All existing callers pass NULL to preserve the current behavior.
Remove the btrfs_insert_delayed_dir_index() wrapper, as there are no more callers.
--- fs/btrfs/delayed-inode.c | 21 --------------------- fs/btrfs/delayed-inode.h | 5 ----- fs/btrfs/dir-item.c | 30 ++++++++++++++++++++++++------ fs/btrfs/dir-item.h | 5 +++-- fs/btrfs/inode.c | 2 +- fs/btrfs/transaction.c | 2 +- 6 files changed, 29 insertions(+), 36 deletions(-)
fs/btrfs/delayed-inode.c +0 -21
@@ -1608,27 +1608,6 @@ int btrfs_insert_delayed_dir_index_prealloc(struct btrfs_trans_handle *trans,return ret; } -/* Will return 0, -ENOMEM or -EEXIST (index number collision, unexpected). */ -int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, -const char *name,int name_len, -struct btrfs_inode *dir, -const struct btrfs_disk_key *disk_key,u8 flags, -u64 index) -{ -struct btrfs_dir_index_prealloc prealloc; -int ret; - - ret =btrfs_prealloc_delayed_dir_index (dir, name, name_len, &prealloc); -if (ret) -return ret; - -memcpy (prealloc.item->data +sizeof (struct btrfs_dir_item ), name, - name_len); - -return btrfs_insert_delayed_dir_index_prealloc (trans, dir, &prealloc, - disk_key, flags, index); -} -static bool btrfs_delete_delayed_insertion_item (struct btrfs_delayed_node *node,u64 index) {
fs/btrfs/delayed-inode.h +0 -5
@@ -115,11 +115,6 @@ struct btrfs_delayed_item { };void btrfs_init_delayed_root (struct btrfs_delayed_root *delayed_root); -int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, -const char *name,int name_len, -struct btrfs_inode *dir, -const struct btrfs_disk_key *disk_key,u8 flags, -u64 index);struct btrfs_dir_index_prealloc {struct btrfs_delayed_node *node;
fs/btrfs/dir-item.c +24 -6
@@ -106,8 +106,11 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,* Will return 0 or -ENOMEM */ int btrfs_insert_dir_item (struct btrfs_trans_handle *trans, -const struct fscrypt_str *name,struct btrfs_inode *dir, -const struct btrfs_key *location,u8 type,u64 index) +const struct fscrypt_str *name, +struct btrfs_inode *dir, +const struct btrfs_key *location,u8 type, +u64 index, +struct btrfs_dir_index_prealloc *prealloc) {int ret =0 ;int ret2 =0 ;
@@ -119,6 +122,8 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,struct btrfs_key key;struct btrfs_disk_key disk_key;u32 data_size; +const bool need_delayed_index = (root != root->fs_info->tree_root); +struct btrfs_dir_index_prealloc local_prealloc; key.objectid =btrfs_ino (dir); key.type =BTRFS_DIR_ITEM_KEY ;
@@ -130,6 +135,18 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,btrfs_cpu_key_to_disk (&disk_key, location); +/* Pre-allocate the delayed dir index before modifying the btree. */ +if (need_delayed_index && !prealloc) { + ret =btrfs_prealloc_delayed_dir_index (dir, name->name, + name->len, + &local_prealloc); +if (ret) +return ret; +memcpy (local_prealloc.item->data +sizeof (struct btrfs_dir_item ), + name->name, name->len); + prealloc = &local_prealloc; + } + data_size =sizeof (*dir_item) + name->len; dir_item =insert_with_overflow (trans, root, path, &key, data_size, name->name, name->len);
@@ -137,6 +154,8 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, ret =PTR_ERR (dir_item);if (ret == -EEXIST ) goto second_insert; +if (need_delayed_index) +btrfs_free_delayed_dir_index_prealloc (trans, prealloc); goto out_free; }
@@ -154,15 +173,14 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,write_extent_buffer (leaf, name->name, name_ptr, name->len); second_insert: -/* FIXME, use some real flag for selecting the extra index */ -if (root == root->fs_info->tree_root) { +if (!need_delayed_index) { ret =0 ; goto out_free; }btrfs_release_path (path); - ret2 =btrfs_insert_delayed_dir_index (trans, name->name, name->len, dir, - &disk_key, type, index); + ret2 =btrfs_insert_delayed_dir_index_prealloc (trans, dir, prealloc, + &disk_key, type, index); out_free:if (ret)return ret;
fs/btrfs/dir-item.h +3 -2
@@ -16,9 +16,11 @@ struct btrfs_trans_handle;int btrfs_check_dir_item_collision (struct btrfs_root *root,u64 dir_ino,const struct fscrypt_str *name); +struct btrfs_dir_index_prealloc ;int btrfs_insert_dir_item (struct btrfs_trans_handle *trans,const struct fscrypt_str *name,struct btrfs_inode *dir, -const struct btrfs_key *location,u8 type,u64 index); +const struct btrfs_key *location,u8 type,u64 index, +struct btrfs_dir_index_prealloc *prealloc); struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, u64 dir,
@@ -53,5 +55,4 @@ static inline u64 btrfs_name_hash(const char *name, int len) {return crc32c ((u32 )~1 , name, len); } -#endif
fs/btrfs/inode.c +1 -1
@@ -6924,7 +6924,7 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,return ret; ret =btrfs_insert_dir_item (trans, name, parent_inode, &key, -btrfs_inode_type (inode), index); +btrfs_inode_type (inode), index,NULL );if (ret == -EEXIST || ret == -EOVERFLOW ) goto fail_dir_item;else if (unlikely (ret)) {
fs/btrfs/transaction.c +1 -1
@@ -1882,7 +1882,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, ret =btrfs_insert_dir_item (trans, &fname.disk_name, parent_inode, &key,BTRFS_FT_DIR , - index); + index,NULL );if (unlikely (ret)) {btrfs_abort_transaction (trans, ret); goto fail;
Signature · 1 line
2.55.0
Re: [PATCH v2 3/5] btrfs: pre-allocate delayed dir index before btree modification
Quoted 102 lines · Jeff Layton
Move the delayed dir index allocation in
btrfs_insert_dir_item()before theinsert_with_overflow()call that modifies the btree. Previously, the allocations happened after theDIR_ITEMwas already inserted, meaning an ENOMEM failure left the btree in a partially-modified state that could only be resolved by aborting the transaction.Add an optional caller-provided
btrfs_dir_index_preallocparameter tobtrfs_insert_dir_item(). When non-NULL, ownership of the prealloc transfers tobtrfs_insert_dir_item(). When NULL, it allocates internally. All existing callers pass NULL to preserve the current behavior.Remove the
btrfs_insert_delayed_dir_index()wrapper, as there are no more callers.Assisted-by LLMSuggested-by Qu Wenruo <[email protected]>Signed-off-by Jeff Layton <[email protected]>--- fs/btrfs/delayed-inode.c | 21 --------------------- fs/btrfs/delayed-inode.h | 5 ----- fs/btrfs/dir-item.c | 30 ++++++++++++++++++++++++------ fs/btrfs/dir-item.h | 5 +++-- fs/btrfs/inode.c | 2 +- fs/btrfs/transaction.c | 2 +- 6 files changed, 29 insertions(+), 36 deletions(-)fs/btrfs/delayed-inode.c +0
-21@@ -1608,27 +1608,6 @@ int btrfs_insert_delayed_dir_index_prealloc(struct btrfs_trans_handle *trans,return ret; } -/* Will return 0, -ENOMEM or -EEXIST (index number collision, unexpected). */ -int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, -const char *name,int name_len, -struct btrfs_inode *dir, -const struct btrfs_disk_key *disk_key,u8 flags, -u64 index) -{ -struct btrfs_dir_index_prealloc prealloc; -int ret; - - ret =btrfs_prealloc_delayed_dir_index (dir, name, name_len, &prealloc); -if (ret) -return ret; - -memcpy (prealloc.item->data +sizeof (struct btrfs_dir_item ), name, - name_len); - -return btrfs_insert_delayed_dir_index_prealloc (trans, dir, &prealloc, - disk_key, flags, index); -} -static bool btrfs_delete_delayed_insertion_item (struct btrfs_delayed_node *node,u64 index) {fs/btrfs/delayed-inode.h +0
-5@@ -115,11 +115,6 @@ struct btrfs_delayed_item { };void btrfs_init_delayed_root (struct btrfs_delayed_root *delayed_root); -int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, -const char *name,int name_len, -struct btrfs_inode *dir, -const struct btrfs_disk_key *disk_key,u8 flags, -u64 index);struct btrfs_dir_index_prealloc {struct btrfs_delayed_node *node;fs/btrfs/dir-item.c +7
-2@@ -106,8 +106,11 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,* Will return 0 or -ENOMEM */ int btrfs_insert_dir_item (struct btrfs_trans_handle *trans, -const struct fscrypt_str *name,struct btrfs_inode *dir, -const struct btrfs_key *location,u8 type,u64 index) +const struct fscrypt_str *name, +struct btrfs_inode *dir, +const struct btrfs_key *location,u8 type, +u64 index, +struct btrfs_dir_index_prealloc *prealloc) {int ret =0 ;int ret2 =0 ;@@ -119,6 +122,8 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,struct btrfs_key key;struct btrfs_disk_key disk_key;u32 data_size; +const bool need_delayed_index = (root != root->fs_info->tree_root); +struct btrfs_dir_index_prealloc local_prealloc; key.objectid =btrfs_ino (dir); key.type =BTRFS_DIR_ITEM_KEY ;
fs/btrfs/dir-item.c int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, · line 139
Quoted 5 lines
fs/btrfs/dir-item.c +2
-0@@ -130,6 +135,18 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,btrfs_cpu_key_to_disk (&disk_key, location); +/* Pre-allocate the delayed dir index before modifying the btree. */ +if (need_delayed_index && !prealloc) {
This is exposed by sashiko.
If we have @prealloc passed in, and before we even hit insert_with_overflow(), the previous btrfs_alloc_path() failed, we return -ENOMEM directly, leaking the @prealloc.
fs/btrfs/dir-item.c int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, · line 142
Quoted 3 lines
fs/btrfs/dir-item.c +3
-0+ ret =btrfs_prealloc_delayed_dir_index (dir, name->name, + name->len, + &local_prealloc);
Sashiko also pointed out that, the preallocation itself doesn't really utilize name->name.
Thus it may be a good idea to merge the later memcpy() into the preallocation function.
Thanks,
Qu
Quoted 88 lines
fs/btrfs/dir-item.c +12
-4+if (ret) +return ret; +memcpy (local_prealloc.item->data +sizeof (struct btrfs_dir_item ), + name->name, name->len); + prealloc = &local_prealloc; + } + data_size =sizeof (*dir_item) + name->len; dir_item =insert_with_overflow (trans, root, path, &key, data_size, name->name, name->len);@@ -137,6 +154,8 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, ret =PTR_ERR (dir_item);if (ret == -EEXIST ) goto second_insert; +if (need_delayed_index) +btrfs_free_delayed_dir_index_prealloc (trans, prealloc); goto out_free; }@@ -154,15 +173,14 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,write_extent_buffer (leaf, name->name, name_ptr, name->len); second_insert: -/* FIXME, use some real flag for selecting the extra index */ -if (root == root->fs_info->tree_root) { +if (!need_delayed_index) { ret =0 ; goto out_free; }btrfs_release_path (path); - ret2 =btrfs_insert_delayed_dir_index (trans, name->name, name->len, dir, - &disk_key, type, index); + ret2 =btrfs_insert_delayed_dir_index_prealloc (trans, dir, prealloc, + &disk_key, type, index); out_free:if (ret)return ret;fs/btrfs/dir-item.h +3
-2@@ -16,9 +16,11 @@ struct btrfs_trans_handle;int btrfs_check_dir_item_collision (struct btrfs_root *root,u64 dir_ino,const struct fscrypt_str *name); +struct btrfs_dir_index_prealloc ;int btrfs_insert_dir_item (struct btrfs_trans_handle *trans,const struct fscrypt_str *name,struct btrfs_inode *dir, -const struct btrfs_key *location,u8 type,u64 index); +const struct btrfs_key *location,u8 type,u64 index, +struct btrfs_dir_index_prealloc *prealloc); struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct btrfs_path *path, u64 dir,@@ -53,5 +55,4 @@ static inline u64 btrfs_name_hash(const char *name, int len) {return crc32c ((u32 )~1 , name, len); } -#endif fs/btrfs/inode.c +1
-1@@ -6924,7 +6924,7 @@ int btrfs_add_link(struct btrfs_trans_handle *trans,return ret; ret =btrfs_insert_dir_item (trans, name, parent_inode, &key, -btrfs_inode_type (inode), index); +btrfs_inode_type (inode), index,NULL );if (ret == -EEXIST || ret == -EOVERFLOW ) goto fail_dir_item;else if (unlikely (ret)) {fs/btrfs/transaction.c +1
-1@@ -1882,7 +1882,7 @@ static noinline int create_pending_snapshot(struct btrfs_trans_handle *trans, ret =btrfs_insert_dir_item (trans, &fname.disk_name, parent_inode, &key,BTRFS_FT_DIR , - index); + index,NULL );if (unlikely (ret)) {btrfs_abort_transaction (trans, ret); goto fail;
Re: [PATCH v2 3/5] btrfs: pre-allocate delayed dir index before btree modification
Quoted 107 lines · Qu Wenruo
Quoted 105 lines · Jeff Layton
Move the delayed dir index allocation in
btrfs_insert_dir_item()before theinsert_with_overflow()call that modifies the btree. Previously, the allocations happened after theDIR_ITEMwas already inserted, meaning an ENOMEM failure left the btree in a partially-modified state that could only be resolved by aborting the transaction.Add an optional caller-provided
btrfs_dir_index_preallocparameter tobtrfs_insert_dir_item(). When non-NULL, ownership of the prealloc transfers tobtrfs_insert_dir_item(). When NULL, it allocates internally. All existing callers pass NULL to preserve the current behavior.Remove the
btrfs_insert_delayed_dir_index()wrapper, as there are no more callers.Assisted-by LLMSuggested-by Qu Wenruo <[email protected]>Signed-off-by Jeff Layton <[email protected]>--- fs/btrfs/delayed-inode.c | 21 --------------------- fs/btrfs/delayed-inode.h | 5 ----- fs/btrfs/dir-item.c | 30 ++++++++++++++++++++++++------ fs/btrfs/dir-item.h | 5 +++-- fs/btrfs/inode.c | 2 +- fs/btrfs/transaction.c | 2 +- 6 files changed, 29 insertions(+), 36 deletions(-)fs/btrfs/delayed-inode.c +0
-21@@ -1608,27 +1608,6 @@ int btrfs_insert_delayed_dir_index_prealloc(struct btrfs_trans_handle *trans,return ret; } -/* Will return 0, -ENOMEM or -EEXIST (index number collision, unexpected). */ -int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, -const char *name,int name_len, -struct btrfs_inode *dir, -const struct btrfs_disk_key *disk_key,u8 flags, -u64 index) -{ -struct btrfs_dir_index_prealloc prealloc; -int ret; - - ret =btrfs_prealloc_delayed_dir_index (dir, name, name_len, &prealloc); -if (ret) -return ret; - -memcpy (prealloc.item->data +sizeof (struct btrfs_dir_item ), name, - name_len); - -return btrfs_insert_delayed_dir_index_prealloc (trans, dir, &prealloc, - disk_key, flags, index); -} -static bool btrfs_delete_delayed_insertion_item (struct btrfs_delayed_node *node,u64 index) {fs/btrfs/delayed-inode.h +0
-5@@ -115,11 +115,6 @@ struct btrfs_delayed_item { };void btrfs_init_delayed_root (struct btrfs_delayed_root *delayed_root); -int btrfs_insert_delayed_dir_index (struct btrfs_trans_handle *trans, -const char *name,int name_len, -struct btrfs_inode *dir, -const struct btrfs_disk_key *disk_key,u8 flags, -u64 index);struct btrfs_dir_index_prealloc {struct btrfs_delayed_node *node;fs/btrfs/dir-item.c +7
-2@@ -106,8 +106,11 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,* Will return 0 or -ENOMEM */ int btrfs_insert_dir_item (struct btrfs_trans_handle *trans, -const struct fscrypt_str *name,struct btrfs_inode *dir, -const struct btrfs_key *location,u8 type,u64 index) +const struct fscrypt_str *name, +struct btrfs_inode *dir, +const struct btrfs_key *location,u8 type, +u64 index, +struct btrfs_dir_index_prealloc *prealloc) {int ret =0 ;int ret2 =0 ;@@ -119,6 +122,8 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,struct btrfs_key key;struct btrfs_disk_key disk_key;u32 data_size; +const bool need_delayed_index = (root != root->fs_info->tree_root); +struct btrfs_dir_index_prealloc local_prealloc; key.objectid =btrfs_ino (dir); key.type =BTRFS_DIR_ITEM_KEY ;@@ -130,6 +135,18 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans,btrfs_cpu_key_to_disk (&disk_key, location);
Quoted 10 lines
fs/btrfs/dir-item.c int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, · line 139
Quoted 2 lines
fs/btrfs/dir-item.c +2
-0+/* Pre-allocate the delayed dir index before modifying the btree. */ +if (need_delayed_index && !prealloc) {This is exposed by sashiko.
If we have @prealloc passed in, and before we even hit
insert_with_overflow(), the previousbtrfs_alloc_path()failed, we return-ENOMEMdirectly, leaking the @prealloc.
Good catch. It looks like sashiko also flagged some other similar issues nearby too that we might as well fix while we're in here. I'll plan to spin up a v3 and resend the series. Thanks very much for the review so far!
Signature · 1 line
Jeff Layton <[email protected]>
PATCH v2 4/5 btrfs: handle ENOMEM from btrfs_insert_dir_item() without aborting
In reply to Jeff LaytonNow that btrfs_insert_dir_item() returns -ENOMEM before modifying the btree (thanks to delayed dir index pre-allocation), callers can handle ENOMEM gracefully instead of aborting the transaction.
- btrfs_add_link(): add -ENOMEM to the recoverable errors alongside -EEXIST and -EOVERFLOW.
- btrfs_create_new_inode(): on -ENOMEM from btrfs_add_link(), clear nlink and orphan the newly-created inode instead of aborting.
This turns a filesystem-killing abort into a graceful -ENOMEM return for create(), mkdir(), mknod(), symlink(), and link() under memory pressure.
--- fs/btrfs/inode.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
fs/btrfs/inode.c +12 -2
@@ -6863,7 +6863,17 @@ int btrfs_create_new_inode(struct btrfs_trans_handle *trans, }else { ret =btrfs_add_link (trans,BTRFS_I (dir),BTRFS_I (inode), name, false,BTRFS_I (inode)->dir_index); -if (unlikely (ret)) { +if (ret == -ENOMEM ) { +clear_nlink (inode); +/* +* btrfs_orphan_add() aborts the transaction itself if it +* fails, so only override ret to -ENOMEM on success. +*/ + ret =btrfs_orphan_add (trans,BTRFS_I (inode)); +if (!ret) + ret = -ENOMEM ; + goto discard; + }else if (unlikely (ret)) {btrfs_abort_transaction (trans, ret); goto discard; }
@@ -6925,7 +6935,7 @@ int btrfs_add_link(struct btrfs_trans_handle *trans, ret =btrfs_insert_dir_item (trans, name, parent_inode, &key,btrfs_inode_type (inode), index,NULL ); -if (ret == -EEXIST || ret == -EOVERFLOW ) +if (ret == -EEXIST || ret == -EOVERFLOW || ret == -ENOMEM ) goto fail_dir_item;else if (unlikely (ret)) {btrfs_abort_transaction (trans, ret);
Signature · 1 line
2.55.0
PATCH v2 5/5 btrfs: pre-allocate delayed dir index for non-overwrite rename
In reply to Jeff LaytonFor rename() without an overwrite target, pre-allocate the delayed dir index before any btree modifications so that ENOMEM can be returned before the source is unlinked from the old directory.
Add a prealloc parameter to btrfs_add_link() that allows callers to pass pre-allocated delayed dir index resources. When provided, btrfs_add_link() takes ownership: it either passes the prealloc to btrfs_insert_dir_item() (which commits or frees it), or frees it on early error. All existing callers pass NULL to preserve the current behavior.
In btrfs_rename(), when new_inode is NULL (no overwrite), call btrfs_prealloc_delayed_dir_index() before the first btree modification and pass the result through to btrfs_add_link(). If the prealloc fails, -ENOMEM is returned before any btree state has changed.
For overwrite rename (new_inode != NULL), the transaction still aborts on ENOMEM since earlier unlink operations have already made irreversible btree modifications.
--- fs/btrfs/btrfs_inode.h | 4 +++- fs/btrfs/inode.c | 43 +++++++++++++++++++++++++++++++++++-------- fs/btrfs/tree-log.c | 4 ++-- 3 files changed, 40 insertions(+), 11 deletions(-)
fs/btrfs/btrfs_inode.h +3 -1
@@ -525,9 +525,11 @@ int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);int btrfs_unlink_inode (struct btrfs_trans_handle *trans,struct btrfs_inode *dir,struct btrfs_inode *inode,const struct fscrypt_str *name); +struct btrfs_dir_index_prealloc ;int btrfs_add_link (struct btrfs_trans_handle *trans,struct btrfs_inode *parent_inode,struct btrfs_inode *inode, -const struct fscrypt_str *name,bool add_backref,u64 index); +const struct fscrypt_str *name,bool add_backref,u64 index, +struct btrfs_dir_index_prealloc *prealloc);int btrfs_delete_subvolume (struct btrfs_inode *dir,struct dentry *dentry);int btrfs_truncate_block (struct btrfs_inode *inode,u64 offset,u64 start,u64 end);
fs/btrfs/inode.c +35 -8
106 lines not on this page · raw message
fs/btrfs/tree-log.c +2 -2
@@ -1683,7 +1683,7 @@ static noinline int add_inode_ref(struct walk_control *wc) }/* insert our name */ - ret =btrfs_add_link (trans, dir, inode, &name, false, ref_index); + ret =btrfs_add_link (trans, dir, inode, &name, false, ref_index,NULL );if (ret) { btrfs_abort_log_replay(wc, ret, "failed to add link for inode %llu in dir %llu ref_index %llu name %.*s root %llu",
@@ -2031,7 +2031,7 @@ static noinline int insert_one_name(struct btrfs_trans_handle *trans,return PTR_ERR (dir); } - ret =btrfs_add_link (trans, dir, inode, name, true, index); + ret =btrfs_add_link (trans, dir, inode, name, true, index,NULL );/* FIXME, put inode into FIXUP list */
Signature · 1 line
2.55.0