Skip to content

Commit a97c9bf

Browse files
Dave JohnsonLinus Torvalds
authored andcommitted
[PATCH] fix cramfs making duplicate entries in inode cache
Every time cramfs_lookup() is called to lookup and inode for a dentry, get_cramfs_inode() will allocate a new inode without checking to see if that inode already exists in the inode cache. This is fine the first time, but if the dentry cache entry(ies) associated with that inode are aged out, but the inode entry is not aged out (which can be quite common if the inode has buffer cache linked to it), cramfs_lookup() will be called again and another inode will be allocated and added to the inode cache creating a duplicate in the inode cache. The big issue here is that the buffers associated with each inode cache entry are not shared between the duplicates! The older inode entries are now orphaned as no dentry points to it and won't be freed until the buffer cache assoicated with them are first freed. The newest entry will have to create all new buffer cache for each part of its file as the old buffer cache is now orphaned as well. Patch below fixes this by making get_cramfs_inode() use the inode cache before blindly creating a new entry every time. This eliminates the duplicate inodes and duplicate buffer cache. Cc: Phillip Lougher <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 7f4bde9 commit a97c9bf

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

fs/cramfs/inode.c

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,47 @@ static DECLARE_MUTEX(read_mutex);
3939
#define CRAMINO(x) ((x)->offset?(x)->offset<<2:1)
4040
#define OFFSET(x) ((x)->i_ino)
4141

42-
static struct inode *get_cramfs_inode(struct super_block *sb, struct cramfs_inode * cramfs_inode)
42+
43+
static int cramfs_iget5_test(struct inode *inode, void *opaque)
44+
{
45+
struct cramfs_inode *cramfs_inode = opaque;
46+
47+
if (inode->i_ino != CRAMINO(cramfs_inode))
48+
return 0; /* does not match */
49+
50+
if (inode->i_ino != 1)
51+
return 1;
52+
53+
/* all empty directories, char, block, pipe, and sock, share inode #1 */
54+
55+
if ((inode->i_mode != cramfs_inode->mode) ||
56+
(inode->i_gid != cramfs_inode->gid) ||
57+
(inode->i_uid != cramfs_inode->uid))
58+
return 0; /* does not match */
59+
60+
if ((S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) &&
61+
(inode->i_rdev != old_decode_dev(cramfs_inode->size)))
62+
return 0; /* does not match */
63+
64+
return 1; /* matches */
65+
}
66+
67+
static int cramfs_iget5_set(struct inode *inode, void *opaque)
68+
{
69+
struct cramfs_inode *cramfs_inode = opaque;
70+
inode->i_ino = CRAMINO(cramfs_inode);
71+
return 0;
72+
}
73+
74+
static struct inode *get_cramfs_inode(struct super_block *sb,
75+
struct cramfs_inode * cramfs_inode)
4376
{
44-
struct inode * inode = new_inode(sb);
77+
struct inode *inode = iget5_locked(sb, CRAMINO(cramfs_inode),
78+
cramfs_iget5_test, cramfs_iget5_set,
79+
cramfs_inode);
4580
static struct timespec zerotime;
4681

47-
if (inode) {
82+
if (inode && (inode->i_state & I_NEW)) {
4883
inode->i_mode = cramfs_inode->mode;
4984
inode->i_uid = cramfs_inode->uid;
5085
inode->i_size = cramfs_inode->size;
@@ -58,7 +93,6 @@ static struct inode *get_cramfs_inode(struct super_block *sb, struct cramfs_inod
5893
but it's the best we can do without reading the directory
5994
contents. 1 yields the right result in GNU find, even
6095
without -noleaf option. */
61-
insert_inode_hash(inode);
6296
if (S_ISREG(inode->i_mode)) {
6397
inode->i_fop = &generic_ro_fops;
6498
inode->i_data.a_ops = &cramfs_aops;
@@ -74,6 +108,7 @@ static struct inode *get_cramfs_inode(struct super_block *sb, struct cramfs_inod
74108
init_special_inode(inode, inode->i_mode,
75109
old_decode_dev(cramfs_inode->size));
76110
}
111+
unlock_new_inode(inode);
77112
}
78113
return inode;
79114
}

0 commit comments

Comments
 (0)