Skip to contentSkip to conversation

btrfs: Turn delayed_nodes_tree into an XArray

PATCH In review Gabriel Niebler 3 participants 5 messages Apr 26, 2022 – May 9, 2022 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 btrfs: Turn delayed_nodes_tree into an XArray

… in the btrfs_root struct and adjust all usages of this object to use the XArray API, because it is notionally easier to use and unserstand, as it provides array semantics, and also takes care of locking for us, further simplifying the code.

Also use the opportunity to do some light refactoring.

---

Changes from v5:
- Fixed whitespace issues (Roman & Nikolay)
- Moved declaration and initialisation of ints inside loops (Nikolay)

Changes from v4:
- Fixed breaking syntax error

Changes from v3:
- Replaced goto-label construct with do-while loop (Nikolay)
- Replaced break with equivalent return for better understandability (Nikolay)
- Made use of delayed_nodes array more efficient (Nikolay)

Changes from v2:
- Fixed uninitialised index variable (Nikolay)
- Fixed missing storage of node in array (Nikolay)
- Improved commit message to motivate patch (David)

Changes from v1:
- Reworked patch set into single patch (David)
- New member name delayed_nodes is independent of data strutcture used (David)
- Shortened commit message and made it start with 'btrfs:' (David)

---
 fs/btrfs/ctree.h         |  4 +-
 fs/btrfs/delayed-inode.c | 83 +++++++++++++++++++---------------------
 fs/btrfs/disk-io.c       |  2 +-
 fs/btrfs/inode.c         |  2 +-
 4 files changed, 44 insertions(+), 47 deletions(-)
fs/btrfs/ctree.h +2 -2
index b7631b88426e..9377dded9679 100644
@@ -1224,10 +1224,10 @@ struct btrfs_root {
 	struct rb_root inode_tree;
 
 	/*
-	 * radix tree that keeps track of delayed nodes of every inode,
+	 * XArray that keeps track of delayed nodes of every inode,
 	 * protected by inode_lock
 	 */
-	struct radix_tree_root delayed_nodes_tree;
+	struct xarray delayed_nodes;
 	/*
 	 * right now this just gets used so that a root has its own devid
 	 * for stat.  It may be used for more later
fs/btrfs/delayed-inode.c +40 -43

139 lines not on this page · raw message

fs/btrfs/disk-io.c +1 -1
index 126f244cdf88..913261481c1a 100644
@@ -1164,7 +1164,7 @@ static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
 	root->nr_delalloc_inodes = 0;
 	root->nr_ordered_extents = 0;
 	root->inode_tree = RB_ROOT;
-	INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC);
+	xa_init_flags(&root->delayed_nodes, GFP_ATOMIC);
 
 	btrfs_init_root_block_rsv(root);
 
fs/btrfs/inode.c +1 -1
index 5082b9c70f8c..50a699ece606 100644
@@ -3826,7 +3826,7 @@ static int btrfs_read_locked_inode(struct inode *inode,
 	 * cache.
 	 *
 	 * This is required for both inode re-read from disk and delayed inode
-	 * in delayed_nodes_tree.
+	 * in the delayed_nodes XArray.
 	 */
 	if (BTRFS_I(inode)->last_trans == fs_info->generation)
 		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Signature · 1 line
2.35.3
#1 lore raw
Quoted 8 lines · Gabriel Niebler

… in the btrfs_root struct and adjust all usages of this object to use the XArray API, because it is notionally easier to use and unserstand, as it provides array semantics, and also takes care of locking for us, further simplifying the code.

Also use the opportunity to do some light refactoring.

<snip>

Quoted 38 lines
fs/btrfs/delayed-inode.c +12 -11
@@ -1870,32 +1863,36 @@ void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode)
 
  void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
  {
-	u64 inode_id = 0;
+	unsigned long index = 0;
+	struct btrfs_delayed_node *delayed_node;
  	struct btrfs_delayed_node *delayed_nodes[8];
-	int i, n;
 
  	while (1) {
+		int n = 0;
+
  		spin_lock(&root->inode_lock);
-		n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
-					   (void **)delayed_nodes, inode_id,
-					   ARRAY_SIZE(delayed_nodes));
-		if (!n) {
+		if (xa_empty(&root->delayed_nodes)) {
  			spin_unlock(&root->inode_lock);
-			break;
+			return;
  		}
 
-		inode_id = delayed_nodes[n - 1]->inode_id + 1;
-		for (i = 0; i < n; i++) {
+		xa_for_each_start(&root->delayed_nodes, index,
+				  delayed_node, index) {
  			/*
  			 * Don't increase refs in case the node is dead and
  			 * about to be removed from the tree in the loop below
  			 */
-			if (!refcount_inc_not_zero(&delayed_nodes[i]->refs))
-				delayed_nodes[i] = NULL;
+			if (refcount_inc_not_zero(&delayed_node->refs)) {
+				delayed_nodes[n] = delayed_node;
+				n++;
+			}

fs/btrfs/delayed-inode.c void btrfs_kill_delayed_inode_items(struct btrfs_inode *inode) · line 1897

Quoted 10 lines
fs/btrfs/delayed-inode.c +4 -1
+			if (n >= ARRAY_SIZE(delayed_nodes))
+				break;
  		}
+		index++;
  		spin_unlock(&root->inode_lock);
 
-		for (i = 0; i < n; i++) {
+		for (int i = 0; i < n; i++) {
  			if (!delayed_nodes[i])
  				continue;

nit: This check now becomes redundant right, because the way n is modified we are guaranteed that everything from 0..n will actually be populated.

<snip>

#2 lore raw

Am 26.04.22 um 13:11 schrieb Nikolay Borisov:

Quoted 51 lines · Gabriel Niebler
Quoted 8 lines

… in the btrfs_root struct and adjust all usages of this object to use the XArray API, because it is notionally easier to use and unserstand, as it provides array semantics, and also takes care of locking for us, further simplifying the code.

Also use the opportunity to do some light refactoring.

<snip>

Quoted 39 lines
(quoted diff) +14 -11
@@ -1870,32 +1863,36 @@ void btrfs_kill_delayed_inode_items(struct 
 btrfs_inode *inode)
   void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
   {
-    u64 inode_id = 0;
+    unsigned long index = 0;
+    struct btrfs_delayed_node *delayed_node;
       struct btrfs_delayed_node *delayed_nodes[8];
-    int i, n;
       while (1) {
+        int n = 0;
+
           spin_lock(&root->inode_lock);
-        n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
-                       (void **)delayed_nodes, inode_id,
-                       ARRAY_SIZE(delayed_nodes));
-        if (!n) {
+        if (xa_empty(&root->delayed_nodes)) {
               spin_unlock(&root->inode_lock);
-            break;
+            return;
           }
-        inode_id = delayed_nodes[n - 1]->inode_id + 1;
-        for (i = 0; i < n; i++) {
+        xa_for_each_start(&root->delayed_nodes, index,
+                  delayed_node, index) {
               /*
                * Don't increase refs in case the node is dead and
                * about to be removed from the tree in the loop below
                */
-            if (!refcount_inc_not_zero(&delayed_nodes[i]->refs))
-                delayed_nodes[i] = NULL;
+            if (refcount_inc_not_zero(&delayed_node->refs)) {
+                delayed_nodes[n] = delayed_node;
+                n++;
+            }
+            if (n >= ARRAY_SIZE(delayed_nodes))
+                break;
           }
Quoted 8 lines

Review void btrfs_kill_delayed_inode_items(struct · line 1894

Quoted 6 lines
(quoted diff) +2 -1
+        index++;
           spin_unlock(&root->inode_lock);
-        for (i = 0; i < n; i++) {
+        for (int i = 0; i < n; i++) {
               if (!delayed_nodes[i])
                   continue;

nit: This check now becomes redundant right, because the way n is modified we are guaranteed that everything from 0..n will actually be populated.

You're right, these last two lines can now safely be removed like this:

fs/btrfs/delayed-inode.c +0 -2
index 8d302f6a0557..ea9f808bce2a 100644
@@ -1893,8 +1893,6 @@ void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
                spin_unlock(&root->inode_lock);
 
                for (int i = 0; i < n; i++) {
-                       if (!delayed_nodes[i])
-                               continue;
                        __btrfs_kill_delayed_node(delayed_nodes[i]);
                        btrfs_release_delayed_node(delayed_nodes[i]);
                }
#3 lore raw

Should I resend with the small changes outlined below, or will the patch be accepted and the diff folded into it on the maintainer's side?

Am 26.04.22 um 16:31 schrieb Gabriel Niebler:

Quoted 78 lines

Am 26.04.22 um 13:11 schrieb Nikolay Borisov:

Quoted 51 lines · Gabriel Niebler
Quoted 8 lines

… in the btrfs_root struct and adjust all usages of this object to use the XArray API, because it is notionally easier to use and unserstand, as it provides array semantics, and also takes care of locking for us, further simplifying the code.

Also use the opportunity to do some light refactoring.

<snip>

Quoted 39 lines
(quoted diff) +14 -11
@@ -1870,32 +1863,36 @@ void btrfs_kill_delayed_inode_items(struct 
 btrfs_inode *inode)
   void btrfs_kill_all_delayed_nodes(struct btrfs_root *root)
   {
-    u64 inode_id = 0;
+    unsigned long index = 0;
+    struct btrfs_delayed_node *delayed_node;
       struct btrfs_delayed_node *delayed_nodes[8];
-    int i, n;
       while (1) {
+        int n = 0;
+
           spin_lock(&root->inode_lock);
-        n = radix_tree_gang_lookup(&root->delayed_nodes_tree,
-                       (void **)delayed_nodes, inode_id,
-                       ARRAY_SIZE(delayed_nodes));
-        if (!n) {
+        if (xa_empty(&root->delayed_nodes)) {
               spin_unlock(&root->inode_lock);
-            break;
+            return;
           }
-        inode_id = delayed_nodes[n - 1]->inode_id + 1;
-        for (i = 0; i < n; i++) {
+        xa_for_each_start(&root->delayed_nodes, index,
+                  delayed_node, index) {
               /*
                * Don't increase refs in case the node is dead and
                * about to be removed from the tree in the loop below
                */
-            if (!refcount_inc_not_zero(&delayed_nodes[i]->refs))
-                delayed_nodes[i] = NULL;
+            if (refcount_inc_not_zero(&delayed_node->refs)) {
+                delayed_nodes[n] = delayed_node;
+                n++;
+            }
+            if (n >= ARRAY_SIZE(delayed_nodes))
+                break;
           }
Quoted 8 lines

Review void btrfs_kill_delayed_inode_items(struct · line 1894

Quoted 6 lines
(quoted diff) +2 -1
+        index++;
           spin_unlock(&root->inode_lock);
-        for (i = 0; i < n; i++) {
+        for (int i = 0; i < n; i++) {
               if (!delayed_nodes[i])
                   continue;

nit: This check now becomes redundant right, because the way n is modified we are guaranteed that everything from 0..n will actually be populated.

You're right, these last two lines can now safely be removed like this:

fs/btrfs/delayed-inode.c +0 -2
index 8d302f6a0557..ea9f808bce2a 100644
@@ -1893,8 +1893,6 @@ void btrfs_kill_all_delayed_nodes(struct 
 btrfs_root *root)
                 spin_unlock(&root->inode_lock);
 
                 for (int i = 0; i < n; i++) {
-                       if (!delayed_nodes[i])
-                               continue;
                         __btrfs_kill_delayed_node(delayed_nodes[i]);
                         btrfs_release_delayed_node(delayed_nodes[i]);

                }

#4 lore raw
Quoted 2 lines · Gabriel Niebler

Should I resend with the small changes outlined below, or will the patch be accepted and the diff folded into it on the maintainer's side?

No need to resend, the fixup has been applied.