Skip to content

Commit 64a989d

Browse files
Benjamin Coddingtonbrauner
authored andcommitted
VFS/knfsd: Teach dentry_create() to use atomic_open()
While knfsd offers combined exclusive create and open results to clients, on some filesystems those results may not be atomic. This behavior can be observed. For example, an open O_CREAT with mode 0 will succeed in creating the file but unexpectedly return -EACCES from vfs_open(). Additionally reducing the number of remote RPC calls required for O_CREAT on network filesystem provides a performance benefit in the open path. Teach knfsd's helper dentry_create() to use atomic_open() for filesystems that support it. The previously const @path is passed up to atomic_open() and may be modified depending on whether an existing entry was found or if the atomic_open() returned an error and consumed the passed-in dentry. Signed-off-by: Benjamin Coddington <bcodding@hammerspace.com> Link: https://patch.msgid.link/8e449bfb64ab055abb9fd82641a171531415a88c.1764259052.git.bcodding@hammerspace.com Reviewed-by: Jeff Layton <jlayton@kernel.org> Reviewed-by: Chuck Lever <chuck.lever@oracle.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 3641155 commit 64a989d

3 files changed

Lines changed: 51 additions & 17 deletions

File tree

fs/namei.c

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4953,25 +4953,54 @@ EXPORT_SYMBOL(start_creating_user_path);
49534953
* On success, returns a "struct file *". Otherwise a ERR_PTR
49544954
* is returned.
49554955
*/
4956-
struct file *dentry_create(const struct path *path, int flags, umode_t mode,
4956+
struct file *dentry_create(struct path *path, int flags, umode_t mode,
49574957
const struct cred *cred)
49584958
{
4959-
struct file *f;
4960-
int error;
4959+
struct file *file __free(fput) = NULL;
4960+
struct dentry *dentry = path->dentry;
4961+
struct dentry *dir = dentry->d_parent;
4962+
struct inode *dir_inode = d_inode(dir);
4963+
struct mnt_idmap *idmap;
4964+
int error, create_error;
49614965

4962-
f = alloc_empty_file(flags, cred);
4963-
if (IS_ERR(f))
4964-
return f;
4966+
file = alloc_empty_file(flags, cred);
4967+
if (IS_ERR(file))
4968+
return file;
49654969

4966-
error = vfs_create(mnt_idmap(path->mnt), path->dentry, mode, NULL);
4967-
if (!error)
4968-
error = vfs_open(path, f);
4970+
idmap = mnt_idmap(path->mnt);
49694971

4970-
if (unlikely(error)) {
4971-
fput(f);
4972-
return ERR_PTR(error);
4972+
if (dir_inode->i_op->atomic_open) {
4973+
path->dentry = dir;
4974+
mode = vfs_prepare_mode(idmap, dir_inode, mode, S_IALLUGO, S_IFREG);
4975+
4976+
create_error = may_o_create(idmap, path, dentry, mode);
4977+
if (create_error)
4978+
flags &= ~O_CREAT;
4979+
4980+
dentry = atomic_open(path, dentry, file, flags, mode);
4981+
error = PTR_ERR_OR_ZERO(dentry);
4982+
4983+
if (unlikely(create_error) && error == -ENOENT)
4984+
error = create_error;
4985+
4986+
if (!error) {
4987+
if (file->f_mode & FMODE_CREATED)
4988+
fsnotify_create(dir->d_inode, dentry);
4989+
if (file->f_mode & FMODE_OPENED)
4990+
fsnotify_open(file);
4991+
}
4992+
4993+
path->dentry = dentry;
4994+
4995+
} else {
4996+
error = vfs_create(mnt_idmap(path->mnt), path->dentry, mode, NULL);
4997+
if (!error)
4998+
error = vfs_open(path, file);
49734999
}
4974-
return f;
5000+
if (unlikely(error))
5001+
return ERR_PTR(error);
5002+
5003+
return no_free_ptr(file);
49755004
}
49765005
EXPORT_SYMBOL(dentry_create);
49775006

fs/nfsd/nfs4proc.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,17 @@ static inline bool nfsd4_create_is_exclusive(int createmode)
194194
}
195195

196196
static __be32
197-
nfsd4_vfs_create(struct svc_fh *fhp, struct dentry *child,
197+
nfsd4_vfs_create(struct svc_fh *fhp, struct dentry **child,
198198
struct nfsd4_open *open)
199199
{
200200
struct file *filp;
201201
struct path path;
202202
int oflags;
203203

204204
oflags = O_CREAT | O_LARGEFILE;
205+
if (nfsd4_create_is_exclusive(open->op_createmode))
206+
oflags |= O_EXCL;
207+
205208
switch (open->op_share_access & NFS4_SHARE_ACCESS_BOTH) {
206209
case NFS4_SHARE_ACCESS_WRITE:
207210
oflags |= O_WRONLY;
@@ -214,9 +217,11 @@ nfsd4_vfs_create(struct svc_fh *fhp, struct dentry *child,
214217
}
215218

216219
path.mnt = fhp->fh_export->ex_path.mnt;
217-
path.dentry = child;
220+
path.dentry = *child;
218221
filp = dentry_create(&path, oflags, open->op_iattr.ia_mode,
219222
current_cred());
223+
*child = path.dentry;
224+
220225
if (IS_ERR(filp))
221226
return nfserrno(PTR_ERR(filp));
222227

@@ -350,7 +355,7 @@ nfsd4_create_file(struct svc_rqst *rqstp, struct svc_fh *fhp,
350355
status = fh_fill_pre_attrs(fhp);
351356
if (status != nfs_ok)
352357
goto out;
353-
status = nfsd4_vfs_create(fhp, child, open);
358+
status = nfsd4_vfs_create(fhp, &child, open);
354359
if (status != nfs_ok)
355360
goto out;
356361
open->op_created = true;

include/linux/fs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2457,7 +2457,7 @@ struct file *dentry_open(const struct path *path, int flags,
24572457
const struct cred *creds);
24582458
struct file *dentry_open_nonotify(const struct path *path, int flags,
24592459
const struct cred *cred);
2460-
struct file *dentry_create(const struct path *path, int flags, umode_t mode,
2460+
struct file *dentry_create(struct path *path, int flags, umode_t mode,
24612461
const struct cred *cred);
24622462
const struct path *backing_file_user_path(const struct file *f);
24632463

0 commit comments

Comments
 (0)