From patchwork Thu Oct 3 23:47:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Al Viro X-Patchwork-Id: 13821684 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [62.89.141.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86FCE1CF5CE for ; Thu, 3 Oct 2024 23:47:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=62.89.141.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999256; cv=none; b=QsAKugztcZHtlzKTyT0t6KBScM+JbQYDzjvAoI/myWIF/KiEOTZqfbOZIs/MrhKSN7HDu/G/p8+hUSBEgNo6aTlEs9J83kv1AXrL3CQYO/at8Zi1tVBbyWcGDrnR7Q8JDPirQHZoKDkskoLo51huFIul+NYAM1gpwuFhtYETzJg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999256; c=relaxed/simple; bh=emPD/oEFgXBRXBtrWDKBc/L+AQIw+LWtCXIkuLM96As=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=S9AKQBi+9fylgeQNq/ejjK2AdN5pMVc3plQSy4f213CQoWETo20kItbPpHtn+h2806laHOJMcEn7EndDE4WVoANwh402Z7WPn5uJM83D/51XWUueqDRyiNwEzExwnczSAvorAOA0EsuHDZQoLr1gxs8aRWdmtmo5zVmbc9tcVRM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zeniv.linux.org.uk; spf=none smtp.mailfrom=ftp.linux.org.uk; dkim=pass (2048-bit key) header.d=linux.org.uk header.i=@linux.org.uk header.b=nbXcX4LK; arc=none smtp.client-ip=62.89.141.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zeniv.linux.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=ftp.linux.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linux.org.uk header.i=@linux.org.uk header.b="nbXcX4LK" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=ecZvfcDQiI7UJbJUMdCSWVfkiuWs1COsxX3tGJirspA=; b=nbXcX4LKw0Ak6EeGrp3cXfmiqv X/uaqQ7GTE4eLvF6O1oqeeIoJtFDyZ1ZO7nMXT4cvaPsy+9NYc5ZeqTQkpTXbtFNubaXdGVdxPan0 d0aptXQGSWwnKAjOc1mFWroeX9gvDI8xUPU5nlbyPFrzMZxNJewXOD6HWBE/zI2ArqvEnbQgVHbkp HLwI+5ydqQIW0UK5rGOwSteNyNTUh13ofm0lCIc4PN2ZCen7CkhBrmWhd6SE01n3zRanZDicE/QGZ mTiRVNoEEzzFpgD4BvxT65bqT1yHvCxVr1eGBUUcHRmOsLn6DOtupXt7pRFCyCsdIulA7nFLI0WV3 +FdOO2tw==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.98 #2 (Red Hat Linux)) id 1swVXg-00000000cTg-0Ju6; Thu, 03 Oct 2024 23:47:32 +0000 Date: Fri, 4 Oct 2024 00:47:32 +0100 From: Al Viro To: linux-fsdevel@vger.kernel.org Cc: Christian Brauner , Jan Kara , Amir Goldstein , Miklos Szeredi Subject: introduce struct fderr, convert overlayfs uses to that Message-ID: <20241003234732.GB147780@ZenIV> References: <20241003234534.GM4017910@ZenIV> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20241003234534.GM4017910@ZenIV> Sender: Al Viro Similar to struct fd; unlike struct fd, it can represent error values. Accessors: * fd_empty(f): true if f represents an error * fd_file(f): just as for struct fd it yields a pointer to struct file if fd_empty(f) is false. If fd_empty(f) is true, fd_file(f) is guaranteed _not_ to be an address of any object (IS_ERR() will be true in that case) * fd_err(f): if f represents an error, returns that error, otherwise the return value is junk. Constructors: * ERR_FDERR(-E...): an instance encoding given error [ERR_FDERR, perhaps?] * BORROWED_FDERR(file): if file points to a struct file instance, return a struct fderr representing that file reference with no flags set. if file is an ERR_PTR(-E...), return a struct fderr representing that error. file MUST NOT be NULL. * CLONED_FDERR(file): similar, but in case when file points to a struct file instance, set FDPUT_FPUT in flags. Same destructor as for struct fd; I'm not entirely convinced that playing with _Generic is a good idea here, but for now let's go that way... See fs/overlayfs/file.c for example of use. Signed-off-by: Al Viro --- fs/overlayfs/file.c | 128 +++++++++++++++++++++---------------------- include/linux/file.h | 37 +++++++++++-- 2 files changed, 95 insertions(+), 70 deletions(-) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index 4504493b20be..c711fa5d802f 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -89,56 +89,46 @@ static int ovl_change_flags(struct file *file, unsigned int flags) return 0; } -static int ovl_real_fdget_meta(const struct file *file, struct fd *real, - bool allow_meta) +static struct fderr ovl_real_fdget_meta(const struct file *file, bool allow_meta) { struct dentry *dentry = file_dentry(file); struct file *realfile = file->private_data; struct path realpath; int err; - real->word = (unsigned long)realfile; - if (allow_meta) { ovl_path_real(dentry, &realpath); } else { /* lazy lookup and verify of lowerdata */ err = ovl_verify_lowerdata(dentry); if (err) - return err; + return ERR_FDERR(err); ovl_path_realdata(dentry, &realpath); } if (!realpath.dentry) - return -EIO; + return ERR_FDERR(-EIO); /* Has it been copied up since we'd opened it? */ - if (unlikely(file_inode(realfile) != d_inode(realpath.dentry))) { - struct file *f = ovl_open_realfile(file, &realpath); - if (IS_ERR(f)) - return PTR_ERR(f); - real->word = (unsigned long)f | FDPUT_FPUT; - return 0; - } + if (unlikely(file_inode(realfile) != d_inode(realpath.dentry))) + return CLONED_FDERR(ovl_open_realfile(file, &realpath)); /* Did the flags change since open? */ - if (unlikely((file->f_flags ^ realfile->f_flags) & ~OVL_OPEN_FLAGS)) - return ovl_change_flags(realfile, file->f_flags); + if (unlikely((file->f_flags ^ realfile->f_flags) & ~OVL_OPEN_FLAGS)) { + err = ovl_change_flags(realfile, file->f_flags); + if (err) + return ERR_FDERR(err); + } - return 0; + return BORROWED_FDERR(realfile); } -static int ovl_real_fdget(const struct file *file, struct fd *real) +static struct fderr ovl_real_fdget(const struct file *file) { - if (d_is_dir(file_dentry(file))) { - struct file *f = ovl_dir_real_file(file, false); - if (IS_ERR(f)) - return PTR_ERR(f); - real->word = (unsigned long)f; - return 0; - } + if (d_is_dir(file_dentry(file))) + return BORROWED_FDERR(ovl_dir_real_file(file, false)); - return ovl_real_fdget_meta(file, real, false); + return ovl_real_fdget_meta(file, false); } static int ovl_open(struct inode *inode, struct file *file) @@ -183,7 +173,7 @@ static int ovl_release(struct inode *inode, struct file *file) static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) { struct inode *inode = file_inode(file); - struct fd real; + struct fderr real; const struct cred *old_cred; loff_t ret; @@ -199,9 +189,9 @@ static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, 0, 0); } - ret = ovl_real_fdget(file, &real); - if (ret) - return ret; + real = ovl_real_fdget(file); + if (fd_empty(real)) + return fd_err(real); /* * Overlay file f_pos is the master copy that is preserved @@ -262,7 +252,7 @@ static void ovl_file_accessed(struct file *file) static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) { struct file *file = iocb->ki_filp; - struct fd real; + struct fderr real; ssize_t ret; struct backing_file_ctx ctx = { .cred = ovl_creds(file_inode(file)->i_sb), @@ -273,9 +263,9 @@ static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) if (!iov_iter_count(iter)) return 0; - ret = ovl_real_fdget(file, &real); - if (ret) - return ret; + real = ovl_real_fdget(file); + if (fd_empty(real)) + return fd_err(real); ret = backing_file_read_iter(fd_file(real), iter, iocb, iocb->ki_flags, &ctx); @@ -288,7 +278,7 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) { struct file *file = iocb->ki_filp; struct inode *inode = file_inode(file); - struct fd real; + struct fderr real; ssize_t ret; int ifl = iocb->ki_flags; struct backing_file_ctx ctx = { @@ -304,9 +294,11 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) /* Update mode */ ovl_copyattr(inode); - ret = ovl_real_fdget(file, &real); - if (ret) + real = ovl_real_fdget(file); + if (fd_empty(real)) { + ret = fd_err(real); goto out_unlock; + } if (!ovl_should_sync(OVL_FS(inode->i_sb))) ifl &= ~(IOCB_DSYNC | IOCB_SYNC); @@ -329,7 +321,7 @@ static ssize_t ovl_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) { - struct fd real; + struct fderr real; ssize_t ret; struct backing_file_ctx ctx = { .cred = ovl_creds(file_inode(in)->i_sb), @@ -337,9 +329,9 @@ static ssize_t ovl_splice_read(struct file *in, loff_t *ppos, .accessed = ovl_file_accessed, }; - ret = ovl_real_fdget(in, &real); - if (ret) - return ret; + real = ovl_real_fdget(in); + if (fd_empty(real)) + return fd_err(real); ret = backing_file_splice_read(fd_file(real), ppos, pipe, len, flags, &ctx); fdput(real); @@ -358,7 +350,7 @@ static ssize_t ovl_splice_read(struct file *in, loff_t *ppos, static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags) { - struct fd real; + struct fderr real; struct inode *inode = file_inode(out); ssize_t ret; struct backing_file_ctx ctx = { @@ -371,9 +363,11 @@ static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, /* Update mode */ ovl_copyattr(inode); - ret = ovl_real_fdget(out, &real); - if (ret) + real = ovl_real_fdget(out); + if (fd_empty(real)) { + ret = fd_err(real); goto out_unlock; + } ret = backing_file_splice_write(pipe, fd_file(real), ppos, len, flags, &ctx); fdput(real); @@ -386,7 +380,7 @@ static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) { - struct fd real; + struct fderr real; const struct cred *old_cred; int ret; @@ -394,9 +388,9 @@ static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) if (ret <= 0) return ret; - ret = ovl_real_fdget_meta(file, &real, !datasync); - if (ret) - return ret; + real = ovl_real_fdget_meta(file, !datasync); + if (fd_empty(real)) + return fd_err(real); /* Don't sync lower file for fear of receiving EROFS error */ if (file_inode(fd_file(real)) == ovl_inode_upper(file_inode(file))) { @@ -425,7 +419,7 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma) static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len) { struct inode *inode = file_inode(file); - struct fd real; + struct fderr real; const struct cred *old_cred; int ret; @@ -435,10 +429,11 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len ret = file_remove_privs(file); if (ret) goto out_unlock; - - ret = ovl_real_fdget(file, &real); - if (ret) + real = ovl_real_fdget(file); + if (fd_empty(real)) { + ret = fd_err(real); goto out_unlock; + } old_cred = ovl_override_creds(file_inode(file)->i_sb); ret = vfs_fallocate(fd_file(real), mode, offset, len); @@ -457,13 +452,13 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice) { - struct fd real; + struct fderr real; const struct cred *old_cred; int ret; - ret = ovl_real_fdget(file, &real); - if (ret) - return ret; + real = ovl_real_fdget(file); + if (fd_empty(real)) + return fd_err(real); old_cred = ovl_override_creds(file_inode(file)->i_sb); ret = vfs_fadvise(fd_file(real), offset, len, advice); @@ -485,7 +480,7 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, loff_t len, unsigned int flags, enum ovl_copyop op) { struct inode *inode_out = file_inode(file_out); - struct fd real_in, real_out; + struct fderr real_in, real_out; const struct cred *old_cred; loff_t ret; @@ -498,13 +493,16 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, goto out_unlock; } - ret = ovl_real_fdget(file_out, &real_out); - if (ret) + real_out = ovl_real_fdget(file_out); + if (fd_empty(real_out)) { + ret = fd_err(real_out); goto out_unlock; + } - ret = ovl_real_fdget(file_in, &real_in); - if (ret) { + real_in = ovl_real_fdget(file_in); + if (fd_empty(real_in)) { fdput(real_out); + ret = fd_err(real_in); goto out_unlock; } @@ -577,13 +575,13 @@ static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in, static int ovl_flush(struct file *file, fl_owner_t id) { - struct fd real; + struct fderr real; const struct cred *old_cred; - int err; + int err = 0; - err = ovl_real_fdget(file, &real); - if (err) - return err; + real = ovl_real_fdget(file); + if (fd_empty(real)) + return fd_err(real); if (fd_file(real)->f_op->flush) { old_cred = ovl_override_creds(file_inode(file)->i_sb); diff --git a/include/linux/file.h b/include/linux/file.h index f98de143245a..d85352523368 100644 --- a/include/linux/file.h +++ b/include/linux/file.h @@ -44,13 +44,26 @@ static inline void fput_light(struct file *file, int fput_needed) struct fd { unsigned long word; }; + +/* either a reference to struct file + flags + * (cloned vs. borrowed, pos locked), with + * flags stored in lower bits of value, + * or an error (represented by small negative value). + */ +struct fderr { + unsigned long word; +}; + #define FDPUT_FPUT 1 #define FDPUT_POS_UNLOCK 2 +#define fd_empty(f) _Generic((f), \ + struct fd: unlikely(!(f).word), \ + struct fderr: IS_ERR_VALUE((f).word)) #define fd_file(f) ((struct file *)((f).word & ~(FDPUT_FPUT|FDPUT_POS_UNLOCK))) -static inline bool fd_empty(struct fd f) +static inline long fd_err(struct fderr f) { - return unlikely(!f.word); + return (long)f.word; } #define EMPTY_FD (struct fd){0} @@ -63,11 +76,25 @@ static inline struct fd CLONED_FD(struct file *f) return (struct fd){(unsigned long)f | FDPUT_FPUT}; } -static inline void fdput(struct fd fd) +static inline struct fderr ERR_FDERR(long n) +{ + return (struct fderr){(unsigned long)n}; +} +static inline struct fderr BORROWED_FDERR(struct file *f) { - if (fd.word & FDPUT_FPUT) - fput(fd_file(fd)); + return (struct fderr){(unsigned long)f}; } +static inline struct fderr CLONED_FDERR(struct file *f) +{ + if (IS_ERR(f)) + return BORROWED_FDERR(f); + return (struct fderr){(unsigned long)f | FDPUT_FPUT}; +} + +#define fdput(f) (void) (_Generic((f), \ + struct fderr: IS_ERR_VALUE((f).word), \ + struct fd: true) && \ + ((f).word & FDPUT_FPUT) && (fput(fd_file(f)),0)) extern struct file *fget(unsigned int fd); extern struct file *fget_raw(unsigned int fd); From patchwork Thu Oct 3 23:48:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Al Viro X-Patchwork-Id: 13821685 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [62.89.141.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BCD2E142E9F for ; Thu, 3 Oct 2024 23:48:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=62.89.141.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999291; cv=none; b=XI9UwqJObcS9xiN65Irw3D8wmNh50CvU6yGu/EPxggQOiNmrEzXFgyYFXqqj0Lt1i2KMY7MV8QCNnQopDiyXL/meuisnde2C8LRB8L7oWH79GHfcur1XsPk6bE1/aQ5qnoOLers2log4x+TX3A9sR0XmHXoTi0YGfuNy/Y9zHjU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999291; c=relaxed/simple; bh=NRJYD9uswGeHIVdjrDI+aH5lLMTvQU2eX9DqTFod7nQ=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=VYhXCoE6hdr7aUKyXBKqlsFTWQME93mMFw5mcfkqJZmZQmaWO8SNzZKiL4V+sWIAurb/cBTbx9PaJl6O1dJyeLj4wDWfiFb5W4e5xRCG6qrzBQrs1tbhUgcWNADKPJnVfXzYwc+g9MKamzCsnmNzJZhU68EDnzgwp2Kl+P8I3bg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zeniv.linux.org.uk; spf=none smtp.mailfrom=ftp.linux.org.uk; dkim=pass (2048-bit key) header.d=linux.org.uk header.i=@linux.org.uk header.b=UY9N6rrD; arc=none smtp.client-ip=62.89.141.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zeniv.linux.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=ftp.linux.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linux.org.uk header.i=@linux.org.uk header.b="UY9N6rrD" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=W3/WN7z4pERvn++L5j78x9US9E1bLG3DUk4YDqylDPw=; b=UY9N6rrDUXYJ02cDgqh2XjznZW Se1QB/eLy520oPLRjiIZxPcC3WKbplohPsiGMvsxn9N039XlsUBb9YLnr4f5bwQ9NKnFQlNJmjSOk 1DhrxugMPdxmSVqDq3pmtyBEzQH7ypIubUihH/vDpIkLFxofg6Pq6sjCUUtxJgywlcdHGVtym6O+n 2gQ6I239pXzYwNJjTm6PUBoHYkxbXT6HcMlOSbrTB4hjvsZplNWUnxsxJS61AtlLpEjgO6ae2Cx7W OWnPLz/bIlriunwF7XJCVFWCyNFEpWqcM1TraTMsw/VDOF6/COohYr4vVU3cKTJteg0lgoyoomi6g KwGnrEGQ==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.98 #2 (Red Hat Linux)) id 1swVYG-00000000cUf-0w8O; Thu, 03 Oct 2024 23:48:08 +0000 Date: Fri, 4 Oct 2024 00:48:08 +0100 From: Al Viro To: linux-fsdevel@vger.kernel.org Cc: Christian Brauner , Jan Kara , Amir Goldstein , Miklos Szeredi Subject: [PATCH 2/3] experimental: convert fs/overlayfs/file.c to CLASS(...) Message-ID: <20241003234808.GC147780@ZenIV> References: <20241003234534.GM4017910@ZenIV> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20241003234534.GM4017910@ZenIV> Sender: Al Viro There are four places where we end up adding an extra scope covering just the range from constructor to destructor; not sure if that's the best way to handle that. The functions in question are ovl_write_iter(), ovl_splice_write(), ovl_fadvise() and ovl_copyfile(). I still don't like the way we have to deal with the scopes, but... use of guard() for inode_lock()/inode_unlock() is a gutter too deep, as far as I'm concerned. Signed-off-by: Al Viro --- fs/overlayfs/file.c | 72 ++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 43 deletions(-) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index c711fa5d802f..a0ab981b13d9 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -131,6 +131,8 @@ static struct fderr ovl_real_fdget(const struct file *file) return ovl_real_fdget_meta(file, false); } +DEFINE_CLASS(fd_real, struct fderr, fdput(_T), ovl_real_fdget(file), struct file *file) + static int ovl_open(struct inode *inode, struct file *file) { struct dentry *dentry = file_dentry(file); @@ -173,7 +175,6 @@ static int ovl_release(struct inode *inode, struct file *file) static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) { struct inode *inode = file_inode(file); - struct fderr real; const struct cred *old_cred; loff_t ret; @@ -189,7 +190,7 @@ static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) return vfs_setpos(file, 0, 0); } - real = ovl_real_fdget(file); + CLASS(fd_real, real)(file); if (fd_empty(real)) return fd_err(real); @@ -210,8 +211,6 @@ static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) file->f_pos = fd_file(real)->f_pos; ovl_inode_unlock(inode); - fdput(real); - return ret; } @@ -252,8 +251,6 @@ static void ovl_file_accessed(struct file *file) static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) { struct file *file = iocb->ki_filp; - struct fderr real; - ssize_t ret; struct backing_file_ctx ctx = { .cred = ovl_creds(file_inode(file)->i_sb), .user_file = file, @@ -263,22 +260,18 @@ static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) if (!iov_iter_count(iter)) return 0; - real = ovl_real_fdget(file); + CLASS(fd_real, real)(file); if (fd_empty(real)) return fd_err(real); - ret = backing_file_read_iter(fd_file(real), iter, iocb, iocb->ki_flags, - &ctx); - fdput(real); - - return ret; + return backing_file_read_iter(fd_file(real), iter, iocb, iocb->ki_flags, + &ctx); } static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) { struct file *file = iocb->ki_filp; struct inode *inode = file_inode(file); - struct fderr real; ssize_t ret; int ifl = iocb->ki_flags; struct backing_file_ctx ctx = { @@ -294,7 +287,9 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) /* Update mode */ ovl_copyattr(inode); - real = ovl_real_fdget(file); + { + + CLASS(fd_real, real)(file); if (fd_empty(real)) { ret = fd_err(real); goto out_unlock; @@ -309,7 +304,8 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) */ ifl &= ~IOCB_DIO_CALLER_COMP; ret = backing_file_write_iter(fd_file(real), iter, iocb, ifl, &ctx); - fdput(real); + + } out_unlock: inode_unlock(inode); @@ -321,22 +317,18 @@ static ssize_t ovl_splice_read(struct file *in, loff_t *ppos, struct pipe_inode_info *pipe, size_t len, unsigned int flags) { - struct fderr real; - ssize_t ret; + CLASS(fd_real, real)(in); struct backing_file_ctx ctx = { .cred = ovl_creds(file_inode(in)->i_sb), .user_file = in, .accessed = ovl_file_accessed, }; - real = ovl_real_fdget(in); if (fd_empty(real)) return fd_err(real); - ret = backing_file_splice_read(fd_file(real), ppos, pipe, len, flags, &ctx); - fdput(real); - - return ret; + return backing_file_splice_read(fd_file(real), ppos, pipe, len, flags, + &ctx); } /* @@ -350,7 +342,6 @@ static ssize_t ovl_splice_read(struct file *in, loff_t *ppos, static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, loff_t *ppos, size_t len, unsigned int flags) { - struct fderr real; struct inode *inode = file_inode(out); ssize_t ret; struct backing_file_ctx ctx = { @@ -363,15 +354,17 @@ static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, /* Update mode */ ovl_copyattr(inode); - real = ovl_real_fdget(out); + { + + CLASS(fd_real, real)(out); if (fd_empty(real)) { ret = fd_err(real); goto out_unlock; } ret = backing_file_splice_write(pipe, fd_file(real), ppos, len, flags, &ctx); - fdput(real); + } out_unlock: inode_unlock(inode); @@ -419,7 +412,6 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma) static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len) { struct inode *inode = file_inode(file); - struct fderr real; const struct cred *old_cred; int ret; @@ -429,7 +421,9 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len ret = file_remove_privs(file); if (ret) goto out_unlock; - real = ovl_real_fdget(file); + { + + CLASS(fd_real, real)(file); if (fd_empty(real)) { ret = fd_err(real); goto out_unlock; @@ -442,8 +436,7 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len /* Update size */ ovl_file_modified(file); - fdput(real); - + } out_unlock: inode_unlock(inode); @@ -452,11 +445,10 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice) { - struct fderr real; + CLASS(fd_real, real)(file); const struct cred *old_cred; int ret; - real = ovl_real_fdget(file); if (fd_empty(real)) return fd_err(real); @@ -464,8 +456,6 @@ static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice) ret = vfs_fadvise(fd_file(real), offset, len, advice); revert_creds(old_cred); - fdput(real); - return ret; } @@ -480,7 +470,6 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, loff_t len, unsigned int flags, enum ovl_copyop op) { struct inode *inode_out = file_inode(file_out); - struct fderr real_in, real_out; const struct cred *old_cred; loff_t ret; @@ -493,15 +482,16 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, goto out_unlock; } - real_out = ovl_real_fdget(file_out); + { + + CLASS(fd_real, real_out)(file_out); if (fd_empty(real_out)) { ret = fd_err(real_out); goto out_unlock; } - real_in = ovl_real_fdget(file_in); + CLASS(fd_real, real_in)(file_in); if (fd_empty(real_in)) { - fdput(real_out); ret = fd_err(real_in); goto out_unlock; } @@ -529,8 +519,7 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, /* Update size */ ovl_file_modified(file_out); - fdput(real_in); - fdput(real_out); + } out_unlock: inode_unlock(inode_out); @@ -575,11 +564,10 @@ static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in, static int ovl_flush(struct file *file, fl_owner_t id) { - struct fderr real; + CLASS(fd_real, real)(file); const struct cred *old_cred; int err = 0; - real = ovl_real_fdget(file); if (fd_empty(real)) return fd_err(real); @@ -588,8 +576,6 @@ static int ovl_flush(struct file *file, fl_owner_t id) err = fd_file(real)->f_op->flush(fd_file(real), id); revert_creds(old_cred); } - fdput(real); - return err; } From patchwork Thu Oct 3 23:48:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Al Viro X-Patchwork-Id: 13821686 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [62.89.141.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3595A12C473 for ; Thu, 3 Oct 2024 23:48:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=62.89.141.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999338; cv=none; b=Rydisppr2+qvUqbXid4uM6JL2FouWWXtAWYJ3efo8AP1aG1oNNY2sX+jlvimWVMzKpEXjcIw8N1PDR2eLK52ypo4/M3DDOzonXJ0ia4h0+r6iQQX+Z9t9v+/NhurZ98YaFoo941LUsNL/ti86dIwn19oT+955xZw7ThxUCLKsKo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999338; c=relaxed/simple; bh=mnCnLepiABnI5/hCJJFpsILxm6zIYpYWy8KTiloXhUg=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rn549vhiRygcUiLVP/iwEdkvIngynbJ9TfQVXWRzr4IJ79icoTbus0bDZ4eOUJUwQuLyHi/GzekWPROskLcJF0pgv/5XaFmwGtOyYyRJHt/EorGRZ6BdNcLNPC7WdVUyPVRaX2KA/9gDouPgiX/qshSaG921u8VEHkbxFBThHLs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zeniv.linux.org.uk; spf=none smtp.mailfrom=ftp.linux.org.uk; dkim=pass (2048-bit key) header.d=linux.org.uk header.i=@linux.org.uk header.b=XyZzrsXG; arc=none smtp.client-ip=62.89.141.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zeniv.linux.org.uk Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=ftp.linux.org.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linux.org.uk header.i=@linux.org.uk header.b="XyZzrsXG" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=FMbhm313/DB8mlBbUjriBTse9joqRo5uvOHvq4d3P/8=; b=XyZzrsXGLcAVwMj+Q0ReaGpAPe NPydWTRCxQ5W+Q3TkxT+kwk9FQmDfy8xwTr0qN8ul6HUV3V3iHmRgeOmOjxUO6v5U+sm2aJOm2gNg MaH41uwiv32/KNM299GlcXXQgcAPl6zvQ2KX7WhVQkkDj3KUdLrY4LhqmEfgcHqJ7ec7TnqZfVRHd 42wlnP67d+/6MnHoq8NxTU8/0BFmuqKaIYVX3y3uzx1fp2NDq1lMxXVrCnVNUxaIQc+DPDaCUsFLz VtP8G3no23ylkmhmkO/jNeaw7UBEOjrEHvcy5DGvlCkvYqTyd7DyurXdqYxWSN802/k5gzc5lMWEM G4luxfZQ==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.98 #2 (Red Hat Linux)) id 1swVZ1-00000000cW1-1fol; Thu, 03 Oct 2024 23:48:55 +0000 Date: Fri, 4 Oct 2024 00:48:55 +0100 From: Al Viro To: linux-fsdevel@vger.kernel.org Cc: Christian Brauner , Jan Kara , Amir Goldstein , Miklos Szeredi Subject: [PATCH 3/3] [experimental] another way to deal with scopes for overlayfs real_fd-under-inode_lock Message-ID: <20241003234855.GD147780@ZenIV> References: <20241003234534.GM4017910@ZenIV> Precedence: bulk X-Mailing-List: linux-fsdevel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20241003234534.GM4017910@ZenIV> Sender: Al Viro [incremental to the previous] Signed-off-by: Al Viro --- fs/overlayfs/file.c | 113 +++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 58 deletions(-) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index a0ab981b13d9..e10a009d32e7 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -268,6 +268,15 @@ static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) &ctx); } +static ssize_t ovl_write_locked(struct kiocb *iocb, struct iov_iter *iter, int ifl, + struct backing_file_ctx *ctx) +{ + CLASS(fd_real, real)(ctx->user_file); + if (fd_empty(real)) + return fd_err(real); + return backing_file_write_iter(fd_file(real), iter, iocb, ifl, ctx); +} + static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) { struct file *file = iocb->ki_filp; @@ -287,14 +296,6 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) /* Update mode */ ovl_copyattr(inode); - { - - CLASS(fd_real, real)(file); - if (fd_empty(real)) { - ret = fd_err(real); - goto out_unlock; - } - if (!ovl_should_sync(OVL_FS(inode->i_sb))) ifl &= ~(IOCB_DSYNC | IOCB_SYNC); @@ -303,11 +304,8 @@ static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) * this property in case it is set by the issuer. */ ifl &= ~IOCB_DIO_CALLER_COMP; - ret = backing_file_write_iter(fd_file(real), iter, iocb, ifl, &ctx); - - } + ret = ovl_write_locked(iocb, iter, ifl, &ctx); -out_unlock: inode_unlock(inode); return ret; @@ -331,6 +329,16 @@ static ssize_t ovl_splice_read(struct file *in, loff_t *ppos, &ctx); } +static ssize_t ovl_splice_locked(struct pipe_inode_info *pipe, + loff_t *ppos, size_t len, unsigned int flags, + struct backing_file_ctx *ctx) +{ + CLASS(fd_real, real)(ctx->user_file); + if (fd_empty(real)) + return fd_err(real); + return backing_file_splice_write(pipe, fd_file(real), ppos, len, flags, ctx); +} + /* * Calling iter_file_splice_write() directly from overlay's f_op may deadlock * due to lock order inversion between pipe->mutex in iter_file_splice_write() @@ -353,19 +361,7 @@ static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out, inode_lock(inode); /* Update mode */ ovl_copyattr(inode); - - { - - CLASS(fd_real, real)(out); - if (fd_empty(real)) { - ret = fd_err(real); - goto out_unlock; - } - - ret = backing_file_splice_write(pipe, fd_file(real), ppos, len, flags, &ctx); - - } -out_unlock: + ret = ovl_splice_locked(pipe, ppos, len, flags, &ctx); inode_unlock(inode); return ret; @@ -409,25 +405,14 @@ static int ovl_mmap(struct file *file, struct vm_area_struct *vma) return backing_file_mmap(realfile, vma, &ctx); } -static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len) +static long ovl_fallocate_locked(struct file *file, int mode, loff_t offset, loff_t len) { - struct inode *inode = file_inode(file); const struct cred *old_cred; int ret; - inode_lock(inode); - /* Update mode */ - ovl_copyattr(inode); - ret = file_remove_privs(file); - if (ret) - goto out_unlock; - { - CLASS(fd_real, real)(file); - if (fd_empty(real)) { - ret = fd_err(real); - goto out_unlock; - } + if (fd_empty(real)) + return fd_err(real); old_cred = ovl_override_creds(file_inode(file)->i_sb); ret = vfs_fallocate(fd_file(real), mode, offset, len); @@ -435,9 +420,20 @@ static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len /* Update size */ ovl_file_modified(file); + return ret; +} - } -out_unlock: +static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len) +{ + struct inode *inode = file_inode(file); + int ret; + + inode_lock(inode); + /* Update mode */ + ovl_copyattr(inode); + ret = file_remove_privs(file); + if (!ret) + ret = ovl_fallocate_locked(file, mode, offset, len); inode_unlock(inode); return ret; @@ -465,36 +461,28 @@ enum ovl_copyop { OVL_DEDUPE, }; -static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, +static loff_t ovl_copyfile_locked(struct file *file_in, loff_t pos_in, struct file *file_out, loff_t pos_out, loff_t len, unsigned int flags, enum ovl_copyop op) { - struct inode *inode_out = file_inode(file_out); const struct cred *old_cred; loff_t ret; - inode_lock(inode_out); if (op != OVL_DEDUPE) { /* Update mode */ - ovl_copyattr(inode_out); + ovl_copyattr(file_inode(file_out)); ret = file_remove_privs(file_out); if (ret) - goto out_unlock; + return ret; } - { - CLASS(fd_real, real_out)(file_out); - if (fd_empty(real_out)) { - ret = fd_err(real_out); - goto out_unlock; - } + if (fd_empty(real_out)) + return fd_err(real_out); CLASS(fd_real, real_in)(file_in); - if (fd_empty(real_in)) { - ret = fd_err(real_in); - goto out_unlock; - } + if (fd_empty(real_in)) + return fd_err(real_in); old_cred = ovl_override_creds(file_inode(file_out)->i_sb); switch (op) { @@ -518,10 +506,19 @@ static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, /* Update size */ ovl_file_modified(file_out); + return ret; +} - } +static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, + struct file *file_out, loff_t pos_out, + loff_t len, unsigned int flags, enum ovl_copyop op) +{ + struct inode *inode_out = file_inode(file_out); + loff_t ret; -out_unlock: + inode_lock(inode_out); + ret = ovl_copyfile_locked(file_in, pos_in, file_out, pos_out, + len, flags, op); inode_unlock(inode_out); return ret;