Skip to content

Commit

Permalink
kernfs: fix a subdir count leak
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Jianyu Zhan <[email protected]>
Acked-by: Tejun Heo <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
JianyuZhan authored and gregkh committed Apr 25, 2014
1 parent a798c10 commit c1befb8
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions fs/kernfs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down

0 comments on commit c1befb8

Please sign in to comment.