Skip to content

Commit 4400372

Browse files
Christoph HellwigAl Viro
authored andcommitted
[PATCH] switch all filesystems over to d_obtain_alias
Switch all users of d_alloc_anon to d_obtain_alias. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 4ea3ada commit 4400372

19 files changed

Lines changed: 78 additions & 305 deletions

File tree

‎fs/dcache.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,17 +1187,17 @@ struct dentry * d_alloc_anon(struct inode *inode)
11871187
* allocating a new one.
11881188
*
11891189
* On successful return, the reference to the inode has been transferred
1190-
* to the dentry. If %NULL is returned (indicating kmalloc failure),
1191-
* the reference on the inode has been released. To make it easier
1192-
* to use in export operations a NULL or IS_ERR inode may be passed in
1193-
* and will be casted to the corresponding NULL or IS_ERR dentry.
1190+
* to the dentry. In case of an error the reference on the inode is released.
1191+
* To make it easier to use in export operations a %NULL or IS_ERR inode may
1192+
* be passed in and will be the error will be propagate to the return value,
1193+
* with a %NULL @inode replaced by ERR_PTR(-ESTALE).
11941194
*/
11951195
struct dentry *d_obtain_alias(struct inode *inode)
11961196
{
11971197
struct dentry *dentry;
11981198

11991199
if (!inode)
1200-
return NULL;
1200+
return ERR_PTR(-ESTALE);
12011201
if (IS_ERR(inode))
12021202
return ERR_CAST(inode);
12031203

‎fs/efs/namei.c‎

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,35 +112,14 @@ struct dentry *efs_fh_to_parent(struct super_block *sb, struct fid *fid,
112112

113113
struct dentry *efs_get_parent(struct dentry *child)
114114
{
115-
struct dentry *parent;
116-
struct inode *inode;
115+
struct dentry *parent = ERR_PTR(-ENOENT);
117116
efs_ino_t ino;
118-
long error;
119117

120118
lock_kernel();
121-
122-
error = -ENOENT;
123119
ino = efs_find_entry(child->d_inode, "..", 2);
124-
if (!ino)
125-
goto fail;
126-
127-
inode = efs_iget(child->d_inode->i_sb, ino);
128-
if (IS_ERR(inode)) {
129-
error = PTR_ERR(inode);
130-
goto fail;
131-
}
132-
133-
error = -ENOMEM;
134-
parent = d_alloc_anon(inode);
135-
if (!parent)
136-
goto fail_iput;
137-
120+
if (ino)
121+
parent = d_obtain_alias(efs_iget(child->d_inode->i_sb, ino));
138122
unlock_kernel();
139-
return parent;
140123

141-
fail_iput:
142-
iput(inode);
143-
fail:
144-
unlock_kernel();
145-
return ERR_PTR(error);
124+
return parent;
146125
}

‎fs/exportfs/expfs.c‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,6 @@ struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
366366
* Try to get any dentry for the given file handle from the filesystem.
367367
*/
368368
result = nop->fh_to_dentry(mnt->mnt_sb, fid, fh_len, fileid_type);
369-
if (!result)
370-
result = ERR_PTR(-ESTALE);
371369
if (IS_ERR(result))
372370
return result;
373371

@@ -422,8 +420,6 @@ struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
422420

423421
target_dir = nop->fh_to_parent(mnt->mnt_sb, fid,
424422
fh_len, fileid_type);
425-
if (!target_dir)
426-
goto err_result;
427423
err = PTR_ERR(target_dir);
428424
if (IS_ERR(target_dir))
429425
goto err_result;

‎fs/ext2/namei.c‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, str
7373
struct dentry *ext2_get_parent(struct dentry *child)
7474
{
7575
unsigned long ino;
76-
struct dentry *parent;
77-
struct inode *inode;
7876
struct dentry dotdot;
7977

8078
dotdot.d_name.name = "..";
@@ -83,16 +81,7 @@ struct dentry *ext2_get_parent(struct dentry *child)
8381
ino = ext2_inode_by_name(child->d_inode, &dotdot);
8482
if (!ino)
8583
return ERR_PTR(-ENOENT);
86-
inode = ext2_iget(child->d_inode->i_sb, ino);
87-
88-
if (IS_ERR(inode))
89-
return ERR_CAST(inode);
90-
parent = d_alloc_anon(inode);
91-
if (!parent) {
92-
iput(inode);
93-
parent = ERR_PTR(-ENOMEM);
94-
}
95-
return parent;
84+
return d_obtain_alias(ext2_iget(child->d_inode->i_sb, ino));
9685
}
9786

9887
/*

‎fs/ext3/namei.c‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,8 +1057,6 @@ static struct dentry *ext3_lookup(struct inode * dir, struct dentry *dentry, str
10571057
struct dentry *ext3_get_parent(struct dentry *child)
10581058
{
10591059
unsigned long ino;
1060-
struct dentry *parent;
1061-
struct inode *inode;
10621060
struct dentry dotdot;
10631061
struct ext3_dir_entry_2 * de;
10641062
struct buffer_head *bh;
@@ -1068,7 +1066,6 @@ struct dentry *ext3_get_parent(struct dentry *child)
10681066
dotdot.d_parent = child; /* confusing, isn't it! */
10691067

10701068
bh = ext3_find_entry(&dotdot, &de);
1071-
inode = NULL;
10721069
if (!bh)
10731070
return ERR_PTR(-ENOENT);
10741071
ino = le32_to_cpu(de->inode);
@@ -1080,16 +1077,7 @@ struct dentry *ext3_get_parent(struct dentry *child)
10801077
return ERR_PTR(-EIO);
10811078
}
10821079

1083-
inode = ext3_iget(child->d_inode->i_sb, ino);
1084-
if (IS_ERR(inode))
1085-
return ERR_CAST(inode);
1086-
1087-
parent = d_alloc_anon(inode);
1088-
if (!parent) {
1089-
iput(inode);
1090-
parent = ERR_PTR(-ENOMEM);
1091-
}
1092-
return parent;
1080+
return d_obtain_alias(ext3_iget(child->d_inode->i_sb, ino));
10931081
}
10941082

10951083
#define S_SHIFT 12

‎fs/ext4/namei.c‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,16 +1083,7 @@ struct dentry *ext4_get_parent(struct dentry *child)
10831083
return ERR_PTR(-EIO);
10841084
}
10851085

1086-
inode = ext4_iget(child->d_inode->i_sb, ino);
1087-
if (IS_ERR(inode))
1088-
return ERR_CAST(inode);
1089-
1090-
parent = d_alloc_anon(inode);
1091-
if (!parent) {
1092-
iput(inode);
1093-
parent = ERR_PTR(-ENOMEM);
1094-
}
1095-
return parent;
1086+
return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino));
10961087
}
10971088

10981089
#define S_SHIFT 12

‎fs/fat/inode.c‎

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -681,33 +681,24 @@ static struct dentry *fat_fh_to_dentry(struct super_block *sb,
681681
inode = NULL;
682682
}
683683
}
684-
if (!inode) {
685-
/* For now, do nothing
686-
* What we could do is:
687-
* follow the file starting at fh[4], and record
688-
* the ".." entry, and the name of the fh[2] entry.
689-
* The follow the ".." file finding the next step up.
690-
* This way we build a path to the root of
691-
* the tree. If this works, we lookup the path and so
692-
* get this inode into the cache.
693-
* Finally try the fat_iget lookup again
694-
* If that fails, then weare totally out of luck
695-
* But all that is for another day
696-
*/
697-
}
698-
if (!inode)
699-
return ERR_PTR(-ESTALE);
700-
701684

702-
/* now to find a dentry.
703-
* If possible, get a well-connected one
685+
/*
686+
* For now, do nothing if the inode is not found.
687+
*
688+
* What we could do is:
689+
*
690+
* - follow the file starting at fh[4], and record the ".." entry,
691+
* and the name of the fh[2] entry.
692+
* - then follow the ".." file finding the next step up.
693+
*
694+
* This way we build a path to the root of the tree. If this works, we
695+
* lookup the path and so get this inode into the cache. Finally try
696+
* the fat_iget lookup again. If that fails, then we are totally out
697+
* of luck. But all that is for another day
704698
*/
705-
result = d_alloc_anon(inode);
706-
if (result == NULL) {
707-
iput(inode);
708-
return ERR_PTR(-ENOMEM);
709-
}
710-
result->d_op = sb->s_root->d_op;
699+
result = d_obtain_alias(inode);
700+
if (!IS_ERR(result))
701+
result->d_op = sb->s_root->d_op;
711702
return result;
712703
}
713704

@@ -754,15 +745,8 @@ static struct dentry *fat_get_parent(struct dentry *child)
754745
}
755746
inode = fat_build_inode(sb, de, i_pos);
756747
brelse(bh);
757-
if (IS_ERR(inode)) {
758-
parent = ERR_CAST(inode);
759-
goto out;
760-
}
761-
parent = d_alloc_anon(inode);
762-
if (!parent) {
763-
iput(inode);
764-
parent = ERR_PTR(-ENOMEM);
765-
}
748+
749+
parent = d_obtain_alias(inode);
766750
out:
767751
unlock_super(sb);
768752

‎fs/fuse/inode.c‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,8 @@ static struct dentry *fuse_get_dentry(struct super_block *sb,
596596
if (inode->i_generation != handle->generation)
597597
goto out_iput;
598598

599-
entry = d_alloc_anon(inode);
600-
err = -ENOMEM;
601-
if (!entry)
602-
goto out_iput;
603-
604-
if (get_node_id(inode) != FUSE_ROOT_ID) {
599+
entry = d_obtain_alias(inode);
600+
if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID) {
605601
entry->d_op = &fuse_dentry_operations;
606602
fuse_invalidate_entry_cache(entry);
607603
}
@@ -696,17 +692,14 @@ static struct dentry *fuse_get_parent(struct dentry *child)
696692
name.name = "..";
697693
err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode),
698694
&name, &outarg, &inode);
699-
if (err && err != -ENOENT)
695+
if (err) {
696+
if (err == -ENOENT)
697+
return ERR_PTR(-ESTALE);
700698
return ERR_PTR(err);
701-
if (err || !inode)
702-
return ERR_PTR(-ESTALE);
703-
704-
parent = d_alloc_anon(inode);
705-
if (!parent) {
706-
iput(inode);
707-
return ERR_PTR(-ENOMEM);
708699
}
709-
if (get_node_id(inode) != FUSE_ROOT_ID) {
700+
701+
parent = d_obtain_alias(inode);
702+
if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID) {
710703
parent->d_op = &fuse_dentry_operations;
711704
fuse_invalidate_entry_cache(parent);
712705
}

‎fs/gfs2/ops_export.c‎

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,28 +130,17 @@ static int gfs2_get_name(struct dentry *parent, char *name,
130130
static struct dentry *gfs2_get_parent(struct dentry *child)
131131
{
132132
struct qstr dotdot;
133-
struct inode *inode;
134133
struct dentry *dentry;
135134

136-
gfs2_str2qstr(&dotdot, "..");
137-
inode = gfs2_lookupi(child->d_inode, &dotdot, 1);
138-
139-
if (!inode)
140-
return ERR_PTR(-ENOENT);
141135
/*
142-
* In case of an error, @inode carries the error value, and we
143-
* have to return that as a(n invalid) pointer to dentry.
136+
* XXX(hch): it would be a good idea to keep this around as a
137+
* static variable.
144138
*/
145-
if (IS_ERR(inode))
146-
return ERR_CAST(inode);
147-
148-
dentry = d_alloc_anon(inode);
149-
if (!dentry) {
150-
iput(inode);
151-
return ERR_PTR(-ENOMEM);
152-
}
139+
gfs2_str2qstr(&dotdot, "..");
153140

154-
dentry->d_op = &gfs2_dops;
141+
dentry = d_obtain_alias(gfs2_lookupi(child->d_inode, &dotdot, 1));
142+
if (!IS_ERR(dentry))
143+
dentry->d_op = &gfs2_dops;
155144
return dentry;
156145
}
157146

@@ -233,13 +222,9 @@ static struct dentry *gfs2_get_dentry(struct super_block *sb,
233222
gfs2_glock_dq_uninit(&i_gh);
234223

235224
out_inode:
236-
dentry = d_alloc_anon(inode);
237-
if (!dentry) {
238-
iput(inode);
239-
return ERR_PTR(-ENOMEM);
240-
}
241-
242-
dentry->d_op = &gfs2_dops;
225+
dentry = d_obtain_alias(inode);
226+
if (!IS_ERR(dentry))
227+
dentry->d_op = &gfs2_dops;
243228
return dentry;
244229

245230
fail_rgd:

‎fs/isofs/export.c‎

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ isofs_export_iget(struct super_block *sb,
2222
__u32 generation)
2323
{
2424
struct inode *inode;
25-
struct dentry *result;
25+
2626
if (block == 0)
2727
return ERR_PTR(-ESTALE);
2828
inode = isofs_iget(sb, block, offset);
@@ -32,12 +32,7 @@ isofs_export_iget(struct super_block *sb,
3232
iput(inode);
3333
return ERR_PTR(-ESTALE);
3434
}
35-
result = d_alloc_anon(inode);
36-
if (!result) {
37-
iput(inode);
38-
return ERR_PTR(-ENOMEM);
39-
}
40-
return result;
35+
return d_obtain_alias(inode);
4136
}
4237

4338
/* This function is surprisingly simple. The trick is understanding
@@ -51,7 +46,6 @@ static struct dentry *isofs_export_get_parent(struct dentry *child)
5146
unsigned long parent_offset = 0;
5247
struct inode *child_inode = child->d_inode;
5348
struct iso_inode_info *e_child_inode = ISOFS_I(child_inode);
54-
struct inode *parent_inode = NULL;
5549
struct iso_directory_record *de = NULL;
5650
struct buffer_head * bh = NULL;
5751
struct dentry *rv = NULL;
@@ -104,28 +98,11 @@ static struct dentry *isofs_export_get_parent(struct dentry *child)
10498
/* Normalize */
10599
isofs_normalize_block_and_offset(de, &parent_block, &parent_offset);
106100

107-
/* Get the inode. */
108-
parent_inode = isofs_iget(child_inode->i_sb,
109-
parent_block,
110-
parent_offset);
111-
if (IS_ERR(parent_inode)) {
112-
rv = ERR_CAST(parent_inode);
113-
if (rv != ERR_PTR(-ENOMEM))
114-
rv = ERR_PTR(-EACCES);
115-
goto out;
116-
}
117-
118-
/* Allocate the dentry. */
119-
rv = d_alloc_anon(parent_inode);
120-
if (rv == NULL) {
121-
rv = ERR_PTR(-ENOMEM);
122-
goto out;
123-
}
124-
101+
rv = d_obtain_alias(isofs_iget(child_inode->i_sb, parent_block,
102+
parent_offset));
125103
out:
126-
if (bh) {
104+
if (bh)
127105
brelse(bh);
128-
}
129106
return rv;
130107
}
131108

0 commit comments

Comments
 (0)