From patchwork Fri May 19 09:35:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13248026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0BA15C77B75 for ; Fri, 19 May 2023 09:36:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 899B0280008; Fri, 19 May 2023 05:36:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 82467280001; Fri, 19 May 2023 05:36:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65067280008; Fri, 19 May 2023 05:36:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 47849280001 for ; Fri, 19 May 2023 05:36:11 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1D682AE4CE for ; Fri, 19 May 2023 09:36:11 +0000 (UTC) X-FDA: 80806498542.14.100C85D Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf11.hostedemail.com (Postfix) with ESMTP id 676E540016 for ; Fri, 19 May 2023 09:36:09 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=wuR5MfjM; dmarc=none; spf=none (imf11.hostedemail.com: domain of BATV+aecf67361b95543ec79f+7208+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+aecf67361b95543ec79f+7208+infradead.org+hch@bombadil.srs.infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684488969; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bXtYYEEzQwvZPWpQse6yFS2DlWFyrRFvHXQe3dgGWLU=; b=qmRj2cYVd628NxxKMuXerPfrh5VJywdAzt0vHWeFNXcBvcXNYOq3ceBweTgJ+EAGPIRPiR C0358ehutd1G2od77Sti+dVp1GLjfNKmfG8Rv+74pB4ykt3XF26yvKlEelFAiX8jBWjHQS Z+xNyKsomEwI0pyoUwBu8KDOLbUWjWA= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=wuR5MfjM; dmarc=none; spf=none (imf11.hostedemail.com: domain of BATV+aecf67361b95543ec79f+7208+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+aecf67361b95543ec79f+7208+infradead.org+hch@bombadil.srs.infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684488969; a=rsa-sha256; cv=none; b=n3Qb78SkyV8kckGdMUjoHZ888qeTmzYyjdsts5T164zVVfCOv71iTj0Ou+9QKXIHXZ4kG8 0sl1XYs+z9IiyeuSci0fGI068BGOBc0URxcFGHCqG1dI27KRYFZsgjNTnUJWcwaHF/ojI9 kpx2cCklalWg4vdgcGibF+zvd9sUA04= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=bXtYYEEzQwvZPWpQse6yFS2DlWFyrRFvHXQe3dgGWLU=; b=wuR5MfjM+D3bTZWoEm9c9T3FyQ Jue/kbHK6tmzfma8e+SaYSkRo0i0M95KfzwDKIl7yAZarwpeGlFg8SOccbWg+5smRc3AQ4HdXO5Wr nxv736/1Pra0TlJRc6gOvRj0yJnIoeus3Ea9ZcLZX9vsbfDhxOOcJdu2Sh3OLpn1iJaVfJZVoL+PO 6JYYrGM32hNNnf9ay0x08Yy8FXV4AEv6azjWxZA7R3S8/gudYOrARKgoCUAPoEHO6BpLCM3Eu/iHd Ze6v9o/MZ4FMpWwAoU8d5cZ1OtyW+xcKH2yx0ol+dIujh/Uz9vLoSMKaWdOFEP9SsxpBhnAOmgW1i fsiOwFhw==; Received: from [2001:4bb8:188:3dd5:e8d0:68bb:e5be:210a] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1pzwWe-00FjiA-3B; Fri, 19 May 2023 09:35:53 +0000 From: Christoph Hellwig To: Matthew Wilcox Cc: Jens Axboe , Xiubo Li , Ilya Dryomov , Alexander Viro , Christian Brauner , "Theodore Ts'o" , Jaegeuk Kim , Chao Yu , Miklos Szeredi , Andreas Gruenbacher , "Darrick J. Wong" , Trond Myklebust , Anna Schumaker , Damien Le Moal , Andrew Morton , linux-block@vger.kernel.org, ceph-devel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net (open list:F2FS FILE SYSTEM), cluster-devel@redhat.com, linux-xfs@vger.kernel.org, linux-nfs@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 10/13] fs: factor out a direct_write_fallback helper Date: Fri, 19 May 2023 11:35:18 +0200 Message-Id: <20230519093521.133226-11-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230519093521.133226-1-hch@lst.de> References: <20230519093521.133226-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 676E540016 X-Stat-Signature: 93f1uo7zth1639z5o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k2vaV6pt9j6hW4O7sy6958bP3BVcYskWJC7fZjvuB/HjvYg4Y1foHinodg6CQK8YE63XtVpPoBz2jebulBtSlE1uT/bI7LTGAd4LbHC5S2jXWUZVHMN3byfuGRReCPY/iONcXowlaLlu1OyY61Vc5NHlBIXigwI1nDMGDsQyy44gnUiHJBNsVjJOHHbPK1rjxeLRTWWXc/86RWkQBnrqsIlh3ka9KiP24d388dz6n0j8mXg3G7+HJyjhkG0KIrNL+A6eCxwF+C862gJ2SWmZpDid5TM4KuZBg9w7qDQNwEHTRcKs8jghET+Ns61W9HNKd1ItqWYuVEudv89uOtKpDEwvZZSu4/cY+CE+w8meaa2VSiuJbyYkzfpagdedjNZ7sh1/52bM792Cty454/4AN7RtDjjleitIG9wXx2POarw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Add a helper dealing with handling the syncing of a buffered write fallback for direct I/O. Signed-off-by: Christoph Hellwig Reviewed-by: Damien Le Moal --- fs/libfs.c | 36 ++++++++++++++++++++++++++++ include/linux/fs.h | 2 ++ mm/filemap.c | 59 ++++++++++------------------------------------ 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 89cf614a327158..9f3791fc6e0715 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -1613,3 +1613,39 @@ u64 inode_query_iversion(struct inode *inode) return cur >> I_VERSION_QUERIED_SHIFT; } EXPORT_SYMBOL(inode_query_iversion); + +ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter, + ssize_t direct_written, ssize_t buffered_written) +{ + struct address_space *mapping = iocb->ki_filp->f_mapping; + loff_t pos = iocb->ki_pos, end; + int err; + + /* + * If the buffered write fallback returned an error, we want to return + * the number of bytes which were written by direct I/O, or the error + * code if that was zero. + * + * Note that this differs from normal direct-io semantics, which will + * return -EFOO even if some bytes were written. + */ + if (unlikely(buffered_written < 0)) + return buffered_written; + + /* + * We need to ensure that the page cache pages are written to disk and + * invalidated to preserve the expected O_DIRECT semantics. + */ + end = pos + buffered_written - 1; + err = filemap_write_and_wait_range(mapping, pos, end); + if (err < 0) { + /* + * We don't know how much we wrote, so just return the number of + * bytes which were direct-written + */ + return err; + } + invalidate_mapping_pages(mapping, pos >> PAGE_SHIFT, end >> PAGE_SHIFT); + return direct_written + buffered_written; +} +EXPORT_SYMBOL_GPL(direct_write_fallback); diff --git a/include/linux/fs.h b/include/linux/fs.h index e4efc1792a877a..576a945db178ef 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2738,6 +2738,8 @@ extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *); extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *); extern ssize_t generic_file_direct_write(struct kiocb *, struct iov_iter *); ssize_t generic_perform_write(struct kiocb *, struct iov_iter *); +ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter, + ssize_t direct_written, ssize_t buffered_written); ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos, rwf_t flags); diff --git a/mm/filemap.c b/mm/filemap.c index c1b988199aece5..875b2108d0a05f 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -4008,25 +4008,21 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from) { struct file *file = iocb->ki_filp; struct address_space *mapping = file->f_mapping; - struct inode *inode = mapping->host; - ssize_t written = 0; - ssize_t err; - ssize_t status; + struct inode *inode = mapping->host; + ssize_t ret; /* We can write back this queue in page reclaim */ current->backing_dev_info = inode_to_bdi(inode); - err = file_remove_privs(file); - if (err) + ret = file_remove_privs(file); + if (ret) goto out; - err = file_update_time(file); - if (err) + ret = file_update_time(file); + if (ret) goto out; if (iocb->ki_flags & IOCB_DIRECT) { - loff_t pos, endbyte; - - written = generic_file_direct_write(iocb, from); + ret = generic_file_direct_write(iocb, from); /* * If the write stopped short of completing, fall back to * buffered writes. Some filesystems do this for writes to @@ -4034,46 +4030,15 @@ ssize_t __generic_file_write_iter(struct kiocb *iocb, struct iov_iter *from) * not succeed (even if it did, DAX does not handle dirty * page-cache pages correctly). */ - if (written < 0 || !iov_iter_count(from) || IS_DAX(inode)) - goto out; - - pos = iocb->ki_pos; - status = generic_perform_write(iocb, from); - /* - * If generic_perform_write() returned a synchronous error - * then we want to return the number of bytes which were - * direct-written, or the error code if that was zero. Note - * that this differs from normal direct-io semantics, which - * will return -EFOO even if some bytes were written. - */ - if (unlikely(status < 0)) { - err = status; - goto out; - } - /* - * We need to ensure that the page cache pages are written to - * disk and invalidated to preserve the expected O_DIRECT - * semantics. - */ - endbyte = pos + status - 1; - err = filemap_write_and_wait_range(mapping, pos, endbyte); - if (err == 0) { - written += status; - invalidate_mapping_pages(mapping, - pos >> PAGE_SHIFT, - endbyte >> PAGE_SHIFT); - } else { - /* - * We don't know how much we wrote, so just return - * the number of bytes which were direct-written - */ - } + if (ret >= 0 && iov_iter_count(from) && !IS_DAX(inode)) + ret = direct_write_fallback(iocb, from, ret, + generic_perform_write(iocb, from)); } else { - written = generic_perform_write(iocb, from); + ret = generic_perform_write(iocb, from); } out: current->backing_dev_info = NULL; - return written ? written : err; + return ret; } EXPORT_SYMBOL(__generic_file_write_iter);