Skip to contentSkip to conversation

kernfs: fix a subdir count leak

PATCH In review Jianyu Zhan 2 participants 2 messages Apr 17, 2014 kernfs

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernfs: fix a subdir count leak

Currently kernfs_link_sibling() increates parent->dir.subdirs before adding the node into parent's chidren rb tree.

Because it is possible that kernfs_link_sibling() couldn't find a suitable slot and bail out, this leads to a mismatch between elevated subdir count with actual children node numbers.

This patches fix this problem, by moving the subdir accouting after the actual addtion happening.

---
 fs/kernfs/dir.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
fs/kernfs/dir.c +6 -3
index 78f3403..ac127cd 100644
@@ -232,9 +232,6 @@ static int kernfs_link_sibling(struct kernfs_node *kn)
 	struct rb_node **node = &kn->parent->dir.children.rb_node;
 	struct rb_node *parent = NULL;
 
-	if (kernfs_type(kn) == KERNFS_DIR)
-		kn->parent->dir.subdirs++;
-
 	while (*node) {
 		struct kernfs_node *pos;
 		int result;
@@ -249,9 +246,15 @@ static int kernfs_link_sibling(struct kernfs_node *kn)
 		else
 			return -EEXIST;
 	}
+
 	/* add new node and rebalance the tree */
 	rb_link_node(&kn->rb, parent, node);
 	rb_insert_color(&kn->rb, &kn->parent->dir.children);
+
+	/* successfully added, account subdir number */
+	if (kernfs_type(kn) == KERNFS_DIR)
+		kn->parent->dir.subdirs++;
+
 	return 0;
 }
 
Signature · 1 line
1.9.0.GIT
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jianyu Zhan

Currently kernfs_link_sibling() increates parent->dir.subdirs before adding the node into parent's chidren rb tree.

Because it is possible that kernfs_link_sibling() couldn't find a suitable slot and bail out, this leads to a mismatch between elevated subdir count with actual children node numbers.

This patches fix this problem, by moving the subdir accouting after the actual addtion happening.

Thanks.

Signature · 1 line
tejun