Skip to content

Commit e4e1118

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs: vfs: new helper - d_make_root() dcache: use a dispose list in select_parent ceph: d_alloc_root() may fail ext4: fix failure exits isofs: inode leak on mount failure
2 parents 37cfc3f + adc0e91 commit e4e1118

5 files changed

Lines changed: 63 additions & 53 deletions

File tree

‎fs/ceph/super.c‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,19 +636,26 @@ static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
636636
req->r_num_caps = 2;
637637
err = ceph_mdsc_do_request(mdsc, NULL, req);
638638
if (err == 0) {
639+
struct inode *inode = req->r_target_inode;
640+
req->r_target_inode = NULL;
639641
dout("open_root_inode success\n");
640-
if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT &&
642+
if (ceph_ino(inode) == CEPH_INO_ROOT &&
641643
fsc->sb->s_root == NULL) {
642-
root = d_alloc_root(req->r_target_inode);
644+
root = d_alloc_root(inode);
645+
if (!root) {
646+
iput(inode);
647+
root = ERR_PTR(-ENOMEM);
648+
goto out;
649+
}
643650
ceph_init_dentry(root);
644651
} else {
645-
root = d_obtain_alias(req->r_target_inode);
652+
root = d_obtain_alias(inode);
646653
}
647-
req->r_target_inode = NULL;
648654
dout("open_root_inode success, root dentry is %p\n", root);
649655
} else {
650656
root = ERR_PTR(err);
651657
}
658+
out:
652659
ceph_mdsc_put_request(req);
653660
return root;
654661
}

‎fs/dcache.c‎

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,15 @@ static void dentry_lru_prune(struct dentry *dentry)
276276
}
277277
}
278278

279-
static void dentry_lru_move_tail(struct dentry *dentry)
279+
static void dentry_lru_move_list(struct dentry *dentry, struct list_head *list)
280280
{
281281
spin_lock(&dcache_lru_lock);
282282
if (list_empty(&dentry->d_lru)) {
283-
list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
283+
list_add_tail(&dentry->d_lru, list);
284284
dentry->d_sb->s_nr_dentry_unused++;
285285
dentry_stat.nr_unused++;
286286
} else {
287-
list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
287+
list_move_tail(&dentry->d_lru, list);
288288
}
289289
spin_unlock(&dcache_lru_lock);
290290
}
@@ -770,14 +770,18 @@ static void shrink_dentry_list(struct list_head *list)
770770
}
771771

772772
/**
773-
* __shrink_dcache_sb - shrink the dentry LRU on a given superblock
774-
* @sb: superblock to shrink dentry LRU.
775-
* @count: number of entries to prune
776-
* @flags: flags to control the dentry processing
773+
* prune_dcache_sb - shrink the dcache
774+
* @sb: superblock
775+
* @count: number of entries to try to free
776+
*
777+
* Attempt to shrink the superblock dcache LRU by @count entries. This is
778+
* done when we need more memory an called from the superblock shrinker
779+
* function.
777780
*
778-
* If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
781+
* This function may fail to free any resources if all the dentries are in
782+
* use.
779783
*/
780-
static void __shrink_dcache_sb(struct super_block *sb, int count, int flags)
784+
void prune_dcache_sb(struct super_block *sb, int count)
781785
{
782786
struct dentry *dentry;
783787
LIST_HEAD(referenced);
@@ -796,13 +800,7 @@ static void __shrink_dcache_sb(struct super_block *sb, int count, int flags)
796800
goto relock;
797801
}
798802

799-
/*
800-
* If we are honouring the DCACHE_REFERENCED flag and the
801-
* dentry has this flag set, don't free it. Clear the flag
802-
* and put it back on the LRU.
803-
*/
804-
if (flags & DCACHE_REFERENCED &&
805-
dentry->d_flags & DCACHE_REFERENCED) {
803+
if (dentry->d_flags & DCACHE_REFERENCED) {
806804
dentry->d_flags &= ~DCACHE_REFERENCED;
807805
list_move(&dentry->d_lru, &referenced);
808806
spin_unlock(&dentry->d_lock);
@@ -821,23 +819,6 @@ static void __shrink_dcache_sb(struct super_block *sb, int count, int flags)
821819
shrink_dentry_list(&tmp);
822820
}
823821

824-
/**
825-
* prune_dcache_sb - shrink the dcache
826-
* @sb: superblock
827-
* @nr_to_scan: number of entries to try to free
828-
*
829-
* Attempt to shrink the superblock dcache LRU by @nr_to_scan entries. This is
830-
* done when we need more memory an called from the superblock shrinker
831-
* function.
832-
*
833-
* This function may fail to free any resources if all the dentries are in
834-
* use.
835-
*/
836-
void prune_dcache_sb(struct super_block *sb, int nr_to_scan)
837-
{
838-
__shrink_dcache_sb(sb, nr_to_scan, DCACHE_REFERENCED);
839-
}
840-
841822
/**
842823
* shrink_dcache_sb - shrink dcache for a superblock
843824
* @sb: superblock
@@ -1092,7 +1073,7 @@ EXPORT_SYMBOL(have_submounts);
10921073
* drop the lock and return early due to latency
10931074
* constraints.
10941075
*/
1095-
static int select_parent(struct dentry * parent)
1076+
static int select_parent(struct dentry *parent, struct list_head *dispose)
10961077
{
10971078
struct dentry *this_parent;
10981079
struct list_head *next;
@@ -1114,12 +1095,11 @@ static int select_parent(struct dentry * parent)
11141095

11151096
spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
11161097

1117-
/*
1118-
* move only zero ref count dentries to the end
1119-
* of the unused list for prune_dcache
1098+
/*
1099+
* move only zero ref count dentries to the dispose list.
11201100
*/
11211101
if (!dentry->d_count) {
1122-
dentry_lru_move_tail(dentry);
1102+
dentry_lru_move_list(dentry, dispose);
11231103
found++;
11241104
} else {
11251105
dentry_lru_del(dentry);
@@ -1181,14 +1161,13 @@ static int select_parent(struct dentry * parent)
11811161
*
11821162
* Prune the dcache to remove unused children of the parent dentry.
11831163
*/
1184-
11851164
void shrink_dcache_parent(struct dentry * parent)
11861165
{
1187-
struct super_block *sb = parent->d_sb;
1166+
LIST_HEAD(dispose);
11881167
int found;
11891168

1190-
while ((found = select_parent(parent)) != 0)
1191-
__shrink_dcache_sb(sb, found, 0);
1169+
while ((found = select_parent(parent, &dispose)) != 0)
1170+
shrink_dentry_list(&dispose);
11921171
}
11931172
EXPORT_SYMBOL(shrink_dcache_parent);
11941173

@@ -1461,6 +1440,23 @@ struct dentry * d_alloc_root(struct inode * root_inode)
14611440
}
14621441
EXPORT_SYMBOL(d_alloc_root);
14631442

1443+
struct dentry *d_make_root(struct inode *root_inode)
1444+
{
1445+
struct dentry *res = NULL;
1446+
1447+
if (root_inode) {
1448+
static const struct qstr name = { .name = "/", .len = 1 };
1449+
1450+
res = __d_alloc(root_inode->i_sb, &name);
1451+
if (res)
1452+
d_instantiate(res, root_inode);
1453+
else
1454+
iput(root_inode);
1455+
}
1456+
return res;
1457+
}
1458+
EXPORT_SYMBOL(d_make_root);
1459+
14641460
static struct dentry * __d_find_any_alias(struct inode *inode)
14651461
{
14661462
struct dentry *alias;

‎fs/ext4/super.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3733,10 +3733,12 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
37333733
}
37343734
if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
37353735
ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
3736+
iput(root);
37363737
goto failed_mount4;
37373738
}
37383739
sb->s_root = d_alloc_root(root);
37393740
if (!sb->s_root) {
3741+
iput(root);
37403742
ext4_msg(sb, KERN_ERR, "get root dentry failed");
37413743
ret = -ENOMEM;
37423744
goto failed_mount4;
@@ -3773,7 +3775,7 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
37733775
if (err) {
37743776
ext4_msg(sb, KERN_ERR, "failed to initialize system "
37753777
"zone (%d)", err);
3776-
goto failed_mount4;
3778+
goto failed_mount4a;
37773779
}
37783780

37793781
ext4_ext_init(sb);
@@ -3830,13 +3832,14 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent)
38303832
failed_mount7:
38313833
ext4_unregister_li_request(sb);
38323834
failed_mount6:
3833-
ext4_ext_release(sb);
3834-
failed_mount5:
38353835
ext4_mb_release(sb);
3836+
failed_mount5:
3837+
ext4_ext_release(sb);
38363838
ext4_release_system_zone(sb);
3837-
failed_mount4:
3838-
iput(root);
3839+
failed_mount4a:
3840+
dput(sb->s_root);
38393841
sb->s_root = NULL;
3842+
failed_mount4:
38403843
ext4_msg(sb, KERN_ERR, "mount failed");
38413844
destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
38423845
failed_mount_wq:

‎fs/isofs/inode.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,11 @@ static int isofs_fill_super(struct super_block *s, void *data, int silent)
948948

949949
/* get the root dentry */
950950
s->s_root = d_alloc_root(inode);
951-
if (!(s->s_root))
952-
goto out_no_root;
951+
if (!(s->s_root)) {
952+
iput(inode);
953+
error = -ENOMEM;
954+
goto out_no_inode;
955+
}
953956

954957
kfree(opt.iocharset);
955958

‎include/linux/dcache.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ extern int d_invalidate(struct dentry *);
249249

250250
/* only used at mount-time */
251251
extern struct dentry * d_alloc_root(struct inode *);
252+
extern struct dentry * d_make_root(struct inode *);
252253

253254
/* <clickety>-<click> the ramfs-type tree */
254255
extern void d_genocide(struct dentry *);

0 commit comments

Comments
 (0)