Skip to content

Commit 1d1fe1e

Browse files
dhowellsLinus Torvalds
authored andcommitted
iget: stop EXT4 from using iget() and read_inode()
Stop the EXT4 filesystem from using iget() and read_inode(). Replace ext4_read_inode() with ext4_iget(), and call that instead of iget(). ext4_iget() then uses iget_locked() directly and returns a proper error code instead of an inode in the event of an error. ext4_fill_super() returns any error incurred when getting the root inode instead of EINVAL. Signed-off-by: David Howells <[email protected]> Acked-by: "Theodore Ts'o" <[email protected]> Acked-by: Jan Kara <[email protected]> Cc: <[email protected]> Acked-by: Christoph Hellwig <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 473043d commit 1d1fe1e

File tree

6 files changed

+87
-70
lines changed

6 files changed

+87
-70
lines changed

fs/ext4/ialloc.c

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -782,14 +782,15 @@ struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
782782
unsigned long max_ino = le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count);
783783
ext4_group_t block_group;
784784
int bit;
785-
struct buffer_head *bitmap_bh = NULL;
785+
struct buffer_head *bitmap_bh;
786786
struct inode *inode = NULL;
787+
long err = -EIO;
787788

788789
/* Error cases - e2fsck has already cleaned up for us */
789790
if (ino > max_ino) {
790791
ext4_warning(sb, __FUNCTION__,
791792
"bad orphan ino %lu! e2fsck was run?", ino);
792-
goto out;
793+
goto error;
793794
}
794795

795796
block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
@@ -798,38 +799,49 @@ struct inode *ext4_orphan_get(struct super_block *sb, unsigned long ino)
798799
if (!bitmap_bh) {
799800
ext4_warning(sb, __FUNCTION__,
800801
"inode bitmap error for orphan %lu", ino);
801-
goto out;
802+
goto error;
802803
}
803804

804805
/* Having the inode bit set should be a 100% indicator that this
805806
* is a valid orphan (no e2fsck run on fs). Orphans also include
806807
* inodes that were being truncated, so we can't check i_nlink==0.
807808
*/
808-
if (!ext4_test_bit(bit, bitmap_bh->b_data) ||
809-
!(inode = iget(sb, ino)) || is_bad_inode(inode) ||
810-
NEXT_ORPHAN(inode) > max_ino) {
811-
ext4_warning(sb, __FUNCTION__,
812-
"bad orphan inode %lu! e2fsck was run?", ino);
813-
printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
814-
bit, (unsigned long long)bitmap_bh->b_blocknr,
815-
ext4_test_bit(bit, bitmap_bh->b_data));
816-
printk(KERN_NOTICE "inode=%p\n", inode);
817-
if (inode) {
818-
printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
819-
is_bad_inode(inode));
820-
printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
821-
NEXT_ORPHAN(inode));
822-
printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
823-
}
809+
if (!ext4_test_bit(bit, bitmap_bh->b_data))
810+
goto bad_orphan;
811+
812+
inode = ext4_iget(sb, ino);
813+
if (IS_ERR(inode))
814+
goto iget_failed;
815+
816+
if (NEXT_ORPHAN(inode) > max_ino)
817+
goto bad_orphan;
818+
brelse(bitmap_bh);
819+
return inode;
820+
821+
iget_failed:
822+
err = PTR_ERR(inode);
823+
inode = NULL;
824+
bad_orphan:
825+
ext4_warning(sb, __FUNCTION__,
826+
"bad orphan inode %lu! e2fsck was run?", ino);
827+
printk(KERN_NOTICE "ext4_test_bit(bit=%d, block=%llu) = %d\n",
828+
bit, (unsigned long long)bitmap_bh->b_blocknr,
829+
ext4_test_bit(bit, bitmap_bh->b_data));
830+
printk(KERN_NOTICE "inode=%p\n", inode);
831+
if (inode) {
832+
printk(KERN_NOTICE "is_bad_inode(inode)=%d\n",
833+
is_bad_inode(inode));
834+
printk(KERN_NOTICE "NEXT_ORPHAN(inode)=%u\n",
835+
NEXT_ORPHAN(inode));
836+
printk(KERN_NOTICE "max_ino=%lu\n", max_ino);
824837
/* Avoid freeing blocks if we got a bad deleted inode */
825-
if (inode && inode->i_nlink == 0)
838+
if (inode->i_nlink == 0)
826839
inode->i_blocks = 0;
827840
iput(inode);
828-
inode = NULL;
829841
}
830-
out:
831842
brelse(bitmap_bh);
832-
return inode;
843+
error:
844+
return ERR_PTR(err);
833845
}
834846

835847
unsigned long ext4_count_free_inodes (struct super_block * sb)

fs/ext4/inode.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,21 +2680,31 @@ static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
26802680
}
26812681
}
26822682

2683-
void ext4_read_inode(struct inode * inode)
2683+
struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
26842684
{
26852685
struct ext4_iloc iloc;
26862686
struct ext4_inode *raw_inode;
2687-
struct ext4_inode_info *ei = EXT4_I(inode);
2687+
struct ext4_inode_info *ei;
26882688
struct buffer_head *bh;
2689+
struct inode *inode;
2690+
long ret;
26892691
int block;
26902692

2693+
inode = iget_locked(sb, ino);
2694+
if (!inode)
2695+
return ERR_PTR(-ENOMEM);
2696+
if (!(inode->i_state & I_NEW))
2697+
return inode;
2698+
2699+
ei = EXT4_I(inode);
26912700
#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
26922701
ei->i_acl = EXT4_ACL_NOT_CACHED;
26932702
ei->i_default_acl = EXT4_ACL_NOT_CACHED;
26942703
#endif
26952704
ei->i_block_alloc_info = NULL;
26962705

2697-
if (__ext4_get_inode_loc(inode, &iloc, 0))
2706+
ret = __ext4_get_inode_loc(inode, &iloc, 0);
2707+
if (ret < 0)
26982708
goto bad_inode;
26992709
bh = iloc.bh;
27002710
raw_inode = ext4_raw_inode(&iloc);
@@ -2720,6 +2730,7 @@ void ext4_read_inode(struct inode * inode)
27202730
!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
27212731
/* this inode is deleted */
27222732
brelse (bh);
2733+
ret = -ESTALE;
27232734
goto bad_inode;
27242735
}
27252736
/* The only unlinked inodes we let through here have
@@ -2758,6 +2769,7 @@ void ext4_read_inode(struct inode * inode)
27582769
if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
27592770
EXT4_INODE_SIZE(inode->i_sb)) {
27602771
brelse (bh);
2772+
ret = -EIO;
27612773
goto bad_inode;
27622774
}
27632775
if (ei->i_extra_isize == 0) {
@@ -2811,11 +2823,12 @@ void ext4_read_inode(struct inode * inode)
28112823
}
28122824
brelse (iloc.bh);
28132825
ext4_set_inode_flags(inode);
2814-
return;
2826+
unlock_new_inode(inode);
2827+
return inode;
28152828

28162829
bad_inode:
2817-
make_bad_inode(inode);
2818-
return;
2830+
iget_failed(inode);
2831+
return ERR_PTR(ret);
28192832
}
28202833

28212834
static int ext4_inode_blocks_set(handle_t *handle,

fs/ext4/namei.c

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,17 +1039,11 @@ static struct dentry *ext4_lookup(struct inode * dir, struct dentry *dentry, str
10391039
if (!ext4_valid_inum(dir->i_sb, ino)) {
10401040
ext4_error(dir->i_sb, "ext4_lookup",
10411041
"bad inode number: %lu", ino);
1042-
inode = NULL;
1043-
} else
1044-
inode = iget(dir->i_sb, ino);
1045-
1046-
if (!inode)
1047-
return ERR_PTR(-EACCES);
1048-
1049-
if (is_bad_inode(inode)) {
1050-
iput(inode);
1051-
return ERR_PTR(-ENOENT);
1042+
return ERR_PTR(-EIO);
10521043
}
1044+
inode = ext4_iget(dir->i_sb, ino);
1045+
if (IS_ERR(inode))
1046+
return ERR_CAST(inode);
10531047
}
10541048
return d_splice_alias(inode, dentry);
10551049
}
@@ -1078,18 +1072,13 @@ struct dentry *ext4_get_parent(struct dentry *child)
10781072
if (!ext4_valid_inum(child->d_inode->i_sb, ino)) {
10791073
ext4_error(child->d_inode->i_sb, "ext4_get_parent",
10801074
"bad inode number: %lu", ino);
1081-
inode = NULL;
1082-
} else
1083-
inode = iget(child->d_inode->i_sb, ino);
1084-
1085-
if (!inode)
1086-
return ERR_PTR(-EACCES);
1087-
1088-
if (is_bad_inode(inode)) {
1089-
iput(inode);
1090-
return ERR_PTR(-ENOENT);
1075+
return ERR_PTR(-EIO);
10911076
}
10921077

1078+
inode = ext4_iget(child->d_inode->i_sb, ino);
1079+
if (IS_ERR(inode))
1080+
return ERR_CAST(inode);
1081+
10931082
parent = d_alloc_anon(inode);
10941083
if (!parent) {
10951084
iput(inode);

fs/ext4/resize.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -779,12 +779,11 @@ int ext4_group_add(struct super_block *sb, struct ext4_new_group_data *input)
779779
"No reserved GDT blocks, can't resize");
780780
return -EPERM;
781781
}
782-
inode = iget(sb, EXT4_RESIZE_INO);
783-
if (!inode || is_bad_inode(inode)) {
782+
inode = ext4_iget(sb, EXT4_RESIZE_INO);
783+
if (IS_ERR(inode)) {
784784
ext4_warning(sb, __FUNCTION__,
785785
"Error opening resize inode");
786-
iput(inode);
787-
return -ENOENT;
786+
return PTR_ERR(inode);
788787
}
789788
}
790789

fs/ext4/super.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -777,11 +777,10 @@ static struct inode *ext4_nfs_get_inode(struct super_block *sb,
777777
* Currently we don't know the generation for parent directory, so
778778
* a generation of 0 means "accept any"
779779
*/
780-
inode = iget(sb, ino);
781-
if (inode == NULL)
782-
return ERR_PTR(-ENOMEM);
783-
if (is_bad_inode(inode) ||
784-
(generation && inode->i_generation != generation)) {
780+
inode = ext4_iget(sb, ino);
781+
if (IS_ERR(inode))
782+
return ERR_CAST(inode);
783+
if (generation && inode->i_generation != generation) {
785784
iput(inode);
786785
return ERR_PTR(-ESTALE);
787786
}
@@ -850,7 +849,6 @@ static struct quotactl_ops ext4_qctl_operations = {
850849
static const struct super_operations ext4_sops = {
851850
.alloc_inode = ext4_alloc_inode,
852851
.destroy_inode = ext4_destroy_inode,
853-
.read_inode = ext4_read_inode,
854852
.write_inode = ext4_write_inode,
855853
.dirty_inode = ext4_dirty_inode,
856854
.delete_inode = ext4_delete_inode,
@@ -1805,6 +1803,7 @@ static int ext4_fill_super (struct super_block *sb, void *data, int silent)
18051803
unsigned long journal_devnum = 0;
18061804
unsigned long def_mount_opts;
18071805
struct inode *root;
1806+
int ret = -EINVAL;
18081807
int blocksize;
18091808
int db_count;
18101809
int i;
@@ -2237,19 +2236,24 @@ static int ext4_fill_super (struct super_block *sb, void *data, int silent)
22372236
* so we can safely mount the rest of the filesystem now.
22382237
*/
22392238

2240-
root = iget(sb, EXT4_ROOT_INO);
2241-
sb->s_root = d_alloc_root(root);
2242-
if (!sb->s_root) {
2239+
root = ext4_iget(sb, EXT4_ROOT_INO);
2240+
if (IS_ERR(root)) {
22432241
printk(KERN_ERR "EXT4-fs: get root inode failed\n");
2244-
iput(root);
2242+
ret = PTR_ERR(root);
22452243
goto failed_mount4;
22462244
}
22472245
if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
2248-
dput(sb->s_root);
2249-
sb->s_root = NULL;
2246+
iput(root);
22502247
printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
22512248
goto failed_mount4;
22522249
}
2250+
sb->s_root = d_alloc_root(root);
2251+
if (!sb->s_root) {
2252+
printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2253+
iput(root);
2254+
ret = -ENOMEM;
2255+
goto failed_mount4;
2256+
}
22532257

22542258
ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
22552259

@@ -2330,7 +2334,7 @@ static int ext4_fill_super (struct super_block *sb, void *data, int silent)
23302334
sb->s_fs_info = NULL;
23312335
kfree(sbi);
23322336
lock_kernel();
2333-
return -EINVAL;
2337+
return ret;
23342338
}
23352339

23362340
/*
@@ -2366,8 +2370,8 @@ static journal_t *ext4_get_journal(struct super_block *sb,
23662370
* things happen if we iget() an unused inode, as the subsequent
23672371
* iput() will try to delete it. */
23682372

2369-
journal_inode = iget(sb, journal_inum);
2370-
if (!journal_inode) {
2373+
journal_inode = ext4_iget(sb, journal_inum);
2374+
if (IS_ERR(journal_inode)) {
23712375
printk(KERN_ERR "EXT4-fs: no journal found.\n");
23722376
return NULL;
23732377
}
@@ -2380,7 +2384,7 @@ static journal_t *ext4_get_journal(struct super_block *sb,
23802384

23812385
jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
23822386
journal_inode, journal_inode->i_size);
2383-
if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
2387+
if (!S_ISREG(journal_inode->i_mode)) {
23842388
printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
23852389
iput(journal_inode);
23862390
return NULL;

include/linux/ext4_fs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ int ext4_get_blocks_handle(handle_t *handle, struct inode *inode,
10241024
struct buffer_head *bh_result,
10251025
int create, int extend_disksize);
10261026

1027-
extern void ext4_read_inode (struct inode *);
1027+
extern struct inode *ext4_iget(struct super_block *, unsigned long);
10281028
extern int ext4_write_inode (struct inode *, int);
10291029
extern int ext4_setattr (struct dentry *, struct iattr *);
10301030
extern void ext4_delete_inode (struct inode *);

0 commit comments

Comments
 (0)