Skip to content

Commit 4ea3ada

Browse files
Christoph HellwigAl Viro
authored andcommitted
[PATCH] new helper: d_obtain_alias
The calling conventions of d_alloc_anon are rather unfortunate for all users, and it's name is not very descriptive either. Add d_obtain_alias as a new exported helper that drops the inode reference in the failure case, too and allows to pass-through NULL pointers and inodes to allow for tail-calls in the export operations. Incidentally this helper already existed as a private function in libfs.c as exportfs_d_alloc so kill that one and switch the callers to d_obtain_alias. Signed-off-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 3a8cff4 commit 4ea3ada

3 files changed

Lines changed: 38 additions & 24 deletions

File tree

‎fs/dcache.c‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,41 @@ struct dentry * d_alloc_anon(struct inode *inode)
11741174
return res;
11751175
}
11761176

1177+
/**
1178+
* d_obtain_alias - find or allocate a dentry for a given inode
1179+
* @inode: inode to allocate the dentry for
1180+
*
1181+
* Obtain a dentry for an inode resulting from NFS filehandle conversion or
1182+
* similar open by handle operations. The returned dentry may be anonymous,
1183+
* or may have a full name (if the inode was already in the cache).
1184+
*
1185+
* When called on a directory inode, we must ensure that the inode only ever
1186+
* has one dentry. If a dentry is found, that is returned instead of
1187+
* allocating a new one.
1188+
*
1189+
* 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.
1194+
*/
1195+
struct dentry *d_obtain_alias(struct inode *inode)
1196+
{
1197+
struct dentry *dentry;
1198+
1199+
if (!inode)
1200+
return NULL;
1201+
if (IS_ERR(inode))
1202+
return ERR_CAST(inode);
1203+
1204+
dentry = d_alloc_anon(inode);
1205+
if (!dentry) {
1206+
iput(inode);
1207+
dentry = ERR_PTR(-ENOMEM);
1208+
}
1209+
return dentry;
1210+
}
1211+
EXPORT_SYMBOL_GPL(d_obtain_alias);
11771212

11781213
/**
11791214
* d_splice_alias - splice a disconnected dentry into the tree if one exists

‎fs/libfs.c‎

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -732,28 +732,6 @@ ssize_t simple_attr_write(struct file *file, const char __user *buf,
732732
return ret;
733733
}
734734

735-
/*
736-
* This is what d_alloc_anon should have been. Once the exportfs
737-
* argument transition has been finished I will update d_alloc_anon
738-
* to this prototype and this wrapper will go away. --hch
739-
*/
740-
static struct dentry *exportfs_d_alloc(struct inode *inode)
741-
{
742-
struct dentry *dentry;
743-
744-
if (!inode)
745-
return NULL;
746-
if (IS_ERR(inode))
747-
return ERR_PTR(PTR_ERR(inode));
748-
749-
dentry = d_alloc_anon(inode);
750-
if (!dentry) {
751-
iput(inode);
752-
dentry = ERR_PTR(-ENOMEM);
753-
}
754-
return dentry;
755-
}
756-
757735
/**
758736
* generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
759737
* @sb: filesystem to do the file handle conversion on
@@ -782,7 +760,7 @@ struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
782760
break;
783761
}
784762

785-
return exportfs_d_alloc(inode);
763+
return d_obtain_alias(inode);
786764
}
787765
EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
788766

@@ -815,7 +793,7 @@ struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
815793
break;
816794
}
817795

818-
return exportfs_d_alloc(inode);
796+
return d_obtain_alias(inode);
819797
}
820798
EXPORT_SYMBOL_GPL(generic_fh_to_parent);
821799

‎include/linux/dcache.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ extern struct dentry * d_alloc(struct dentry *, const struct qstr *);
231231
extern struct dentry * d_alloc_anon(struct inode *);
232232
extern struct dentry * d_splice_alias(struct inode *, struct dentry *);
233233
extern struct dentry * d_add_ci(struct dentry *, struct inode *, struct qstr *);
234+
extern struct dentry * d_obtain_alias(struct inode *);
234235
extern void shrink_dcache_sb(struct super_block *);
235236
extern void shrink_dcache_parent(struct dentry *);
236237
extern void shrink_dcache_for_umount(struct super_block *);

0 commit comments

Comments
 (0)