Skip to content

Commit

Permalink
kernfs: fix ino wrap-around detection
Browse files Browse the repository at this point in the history
When the 32bit ino wraps around, kernfs increments the generation
number to distinguish reused ino instances.  The wrap-around detection
tests whether the allocated ino is lower than what the cursor but the
cursor is pointing to the next ino to allocate so the condition never
triggers.

Fix it by remembering the last ino and comparing against that.

Signed-off-by: Tejun Heo <[email protected]>
Reviewed-by: Greg Kroah-Hartman <[email protected]>
Fixes: 4a3ef68 ("kernfs: implement i_generation")
Cc: Namhyung Kim <[email protected]>
Cc: [email protected] # v4.14+
  • Loading branch information
htejun committed Nov 12, 2019
1 parent d671fa6 commit e23f568
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
5 changes: 2 additions & 3 deletions fs/kernfs/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,6 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,
{
struct kernfs_node *kn;
u32 gen;
int cursor;
int ret;

name = kstrdup_const(name, GFP_KERNEL);
Expand All @@ -635,11 +634,11 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root,

idr_preload(GFP_KERNEL);
spin_lock(&kernfs_idr_lock);
cursor = idr_get_cursor(&root->ino_idr);
ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC);
if (ret >= 0 && ret < cursor)
if (ret >= 0 && ret < root->last_ino)
root->next_generation++;
gen = root->next_generation;
root->last_ino = ret;
spin_unlock(&kernfs_idr_lock);
idr_preload_end();
if (ret < 0)
Expand Down
1 change: 1 addition & 0 deletions include/linux/kernfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ struct kernfs_root {

/* private fields, do not use outside kernfs proper */
struct idr ino_idr;
u32 last_ino;
u32 next_generation;
struct kernfs_syscall_ops *syscall_ops;

Expand Down

0 comments on commit e23f568

Please sign in to comment.