From patchwork Wed May 31 07:50:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13261646 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 50545C7EE24 for ; Wed, 31 May 2023 07:51:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E1AEF6B0075; Wed, 31 May 2023 03:51:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DCB1C900002; Wed, 31 May 2023 03:51:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C6C6F6B0081; Wed, 31 May 2023 03:51:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B66306B0075 for ; Wed, 31 May 2023 03:51:08 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 88E3BA025D for ; Wed, 31 May 2023 07:51:08 +0000 (UTC) X-FDA: 80849779416.20.B99C029 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf06.hostedemail.com (Postfix) with ESMTP id F1831180012 for ; Wed, 31 May 2023 07:51:06 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b="N1IkSc/F"; spf=none (imf06.hostedemail.com: domain of BATV+b81a8c9a6d22e8bb2302+7220+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+b81a8c9a6d22e8bb2302+7220+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685519467; 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=11fdSgTG4jIa+lyPH9HFZjOssjJQOFkzGuhPffE88Ak=; b=tzboRNDWBg1wWzBVmErCHlt8UMGxN54jEuJH4/NFWMLTh8ULT8NumyR9/fzRoLtjGY020b IH2J7ghIwx0vqh1nhekgMWGagpJXALijRaB6RxOV6bc9Fdf6I/l0HXxhbYOdRb0sK/T8NP aNLvPyL0y8xxHrUTrmSipbzBvwPycKY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685519467; a=rsa-sha256; cv=none; b=2Nx/8ef+wSg3mn2PPUvFQhrjosqz7r1pkPaLmX1t1nyH2Vc61UtHXyVvfITMdc0QWsELy0 PbFwjv9xa0mNAacFGYAHqgSSXr8OUWBB5KIdchGbR9XXas0I1PEUEllyAqociQGYwJTS69 4YSOvrF1MerYBNYXZx5GrgMHrDEntWs= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b="N1IkSc/F"; spf=none (imf06.hostedemail.com: domain of BATV+b81a8c9a6d22e8bb2302+7220+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+b81a8c9a6d22e8bb2302+7220+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none 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=11fdSgTG4jIa+lyPH9HFZjOssjJQOFkzGuhPffE88Ak=; b=N1IkSc/F+8A4KQZ69jwKm+YdVJ ttzm96cZrwRIglbmBQ7IM5r/NkpF04kG82W7HAoa2XeT3JqbLH9NHTWBanjy9ttbehUxPmZv1ziAa ZPjwzx263cmBsH2k08HRlZqhWrrJEQTRDLSj81pBtTdaeRZiQwJuIF1V7Pjb70zz1bLEwjIef8fS0 ejj0atwMoeXbA3dxf6snc8QgefUF7DakmT61oqvfsJn+DYJ3h5KFnE4H7KYgfmLDNYjp6DkzRQY0J 4VgxKppjSfcbo3TDcefjJe52TouvoD56hyuShTmjKbP30TJToxprGqjLVwDlsUJmVLEsYAiJvNStV 1fwL7WUA==; Received: from [2001:4bb8:182:6d06:f5c3:53d7:b5aa:b6a7] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1q4Gbc-00GVv5-2z; Wed, 31 May 2023 07:50: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, cluster-devel@redhat.com, linux-xfs@vger.kernel.org, linux-nfs@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 09/12] fs: factor out a direct_write_fallback helper Date: Wed, 31 May 2023 09:50:23 +0200 Message-Id: <20230531075026.480237-10-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230531075026.480237-1-hch@lst.de> References: <20230531075026.480237-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-Stat-Signature: co3zp1d64t8gwkipxu7fyz69hjjimexj X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: F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uJPp190UoKKAOroppjwWE3JYTdBJ7e13k/DX/tSC7vUbBD5TGVLzff6SrpZWZ42fRJo9I93P+gOLeCgtKcE04RXvFzChJ3v+9ecjn6jDmRdqNkDepMgbZQdPV3Z7kAt2f5pLI+4isQoKPs86WI3rSOLJ9Hx/eg18Khsz7ivq1AFTCKq/tUm7mI2+i8w5Dk92/rlIN+vuB1JOSnr5x1jO+1u2H5Tnedn/ZPrk66BHmd3PgRs6yAsIDgKsueVz4zCVKBrr46dqRFk7DOg5Hb+dLX2Apxb2VdKHqs0SfURWKVz7iT+CUWuDvSxNwATvfK9UyelFfTLUoU1n4TUY69oVyWHgg4/cm2E5m3yuP4ZtIuUYsMw5XIG7Cb7e0x7Ve3V9s6gSc29wMJUiR5E24ooyMJuZyWkVKWzDQq/NC0vEC2aw== 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 Reviewed-by: Miklos Szeredi --- fs/libfs.c | 41 ++++++++++++++++++++++++++++ include/linux/fs.h | 2 ++ mm/filemap.c | 66 +++++++++++----------------------------------- 3 files changed, 58 insertions(+), 51 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 89cf614a327158..5b851315eeed03 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -1613,3 +1613,44 @@ 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 - buffered_written; + loff_t end = iocb->ki_pos - 1; + 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)) { + if (direct_written) + return direct_written; + 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. + */ + 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 + */ + if (direct_written) + return 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 91021b4e1f6f48..6af25137543824 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 ddb6f8aa86d6ca..137508da5525b6 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -4006,23 +4006,19 @@ 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; - err = file_remove_privs(file); - if (err) - goto out; + ret = file_remove_privs(file); + if (ret) + return ret; - err = file_update_time(file); - if (err) - goto out; + ret = file_update_time(file); + if (ret) + return ret; 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 @@ -4030,45 +4026,13 @@ 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 - */ - } - } else { - written = generic_perform_write(iocb, from); + if (ret < 0 || !iov_iter_count(from) || IS_DAX(inode)) + return ret; + return direct_write_fallback(iocb, from, ret, + generic_perform_write(iocb, from)); } -out: - return written ? written : err; + + return generic_perform_write(iocb, from); } EXPORT_SYMBOL(__generic_file_write_iter);