From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf0-f194.google.com ([209.85.192.194]:39176 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750756AbeDXWOp (ORCPT ); Tue, 24 Apr 2018 18:14:45 -0400 Received: by mail-pf0-f194.google.com with SMTP id z9so13428201pfe.6 for ; Tue, 24 Apr 2018 15:14:45 -0700 (PDT) Date: Tue, 24 Apr 2018 15:14:43 -0700 From: Omar Sandoval To: Al Viro , linux-fsdevel@vger.kernel.org Cc: Linus Torvalds , linux-api@vger.kernel.org, kernel-team@fb.com, Xi Wang Subject: Re: [RFC PATCH v3 1/2] fs: add AT_REPLACE flag for linkat() which replaces the target Message-ID: <20180424221443.GA9228@vader> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Sender: linux-fsdevel-owner@vger.kernel.org List-ID: On Mon, Apr 23, 2018 at 11:19:41PM -0700, Omar Sandoval wrote: > From: Omar Sandoval > > One of the most common uses of temporary files is the classic atomic > replacement pattern, i.e., > > - write temporary file > - fsync temporary file > - rename temporary file over real file > - fsync parent directory > > Now, we have O_TMPFILE, which gives us a much better way to create > temporary files, but it's not possible to use it for this pattern. > > This patch introduces an AT_REPLACE flag which allows linkat() to > replace the target file. Now, the temporary file in the pattern above > can be a proper O_TMPFILE. Even without O_TMPFILE, this is a new > primitive which might be useful in other contexts. > > The implementation on the VFS side mimics sys_renameat2(). > > Cc: Xi Wang > Signed-off-by: Omar Sandoval > --- > fs/ecryptfs/inode.c | 2 +- > fs/namei.c | 181 +++++++++++++++++++++++++++++-------- > fs/nfsd/vfs.c | 2 +- > fs/overlayfs/overlayfs.h | 2 +- > include/linux/fs.h | 3 +- > include/uapi/linux/fcntl.h | 1 + > 6 files changed, 150 insertions(+), 41 deletions(-) > > diff --git a/fs/namei.c b/fs/namei.c > index 186bd2464fd5..2cc2b1deaa12 100644 > --- a/fs/namei.c > +++ b/fs/namei.c > @@ -4149,6 +4149,7 @@ SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newn > * @dir: new parent > * @new_dentry: where to create the new link > * @delegated_inode: returns inode needing a delegation break > + * @flags: link flags > * > * The caller must hold dir->i_mutex > * > @@ -4162,16 +4163,26 @@ SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newn > * be appropriate for callers that expect the underlying filesystem not > * to be NFS exported. > */ > -int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode) > +int vfs_link(struct dentry *old_dentry, struct inode *dir, > + struct dentry *new_dentry, struct inode **delegated_inode, > + unsigned int flags) > { > struct inode *inode = old_dentry->d_inode; > + struct inode *target = new_dentry->d_inode; > unsigned max_links = dir->i_sb->s_max_links; > int error; > > if (!inode) > return -ENOENT; > > - error = may_create(dir, new_dentry); > + if (target) { > + if (flags & AT_REPLACE) This needs an if (inode == target) return 0; I'll fix this in v4. > + error = may_delete(dir, new_dentry, d_is_dir(old_dentry)); > + else > + error = -EEXIST; > + } else { > + error = may_create(dir, new_dentry); > + } > if (error) > return error; >