From patchwork Wed May 24 06:38:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13253311 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 DD86AC87FDE for ; Wed, 24 May 2023 06:38:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 701C6280002; Wed, 24 May 2023 02:38:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6B1D8280001; Wed, 24 May 2023 02:38:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5A0E6280002; Wed, 24 May 2023 02:38:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4B09D280001 for ; Wed, 24 May 2023 02:38:57 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 21F4BA0893 for ; Wed, 24 May 2023 06:38:57 +0000 (UTC) X-FDA: 80824195914.21.313EDEA Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf17.hostedemail.com (Postfix) with ESMTP id 76AB64000A for ; Wed, 24 May 2023 06:38:55 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=0Qg7PBPM; spf=none (imf17.hostedemail.com: domain of BATV+8349dc162fb675e8d54f+7213+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+8349dc162fb675e8d54f+7213+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=1684910335; 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=DuSI6OMC8P69e9w39tXFOndbEiGmzCe8R8L+odlgjgw=; b=3tOO6tvjjNbh2vPj6KEgjY5gKwwd+n7kZvecehoYdC6PhZisICj1tahqTPojnXUoWB0Hnu fa+StRwTO5B590YbmvzIjmYRqKhCphIfAtVe1TQxNWjYhSJBOX6fhjK05yKnRJF9jvKFH6 QhQPl206dsz6rEr0MMIQ2BuNe70wgd4= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=0Qg7PBPM; spf=none (imf17.hostedemail.com: domain of BATV+8349dc162fb675e8d54f+7213+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+8349dc162fb675e8d54f+7213+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684910335; a=rsa-sha256; cv=none; b=xmUqglJ0581CcB1Potjh9xK49rsB60W68Yo+t1fPTQe19wH0c8hh3txaWHhHBT131ofFME DGFUB1s/8/IfJk0P5Em4HUKKB+19TxtLh3aWWkZC9ygRy7BLdKslsQI+ccY9SYxEaQsGPZ 5TGhVEQJYIOQ7iyHaXyuvlHXt66k7sM= 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=DuSI6OMC8P69e9w39tXFOndbEiGmzCe8R8L+odlgjgw=; b=0Qg7PBPMhzJDDeLv8Rqy6M7JAb 8k8XDfr7gosbki8YW1OPh/Xlmot6rYDT/mDCbXqA3BPW+zZcIe3FXGThCbPkhur2e+HMw+d4wYowy 0CJfJz5vrLO/S4zcFsHJLU99CEw0c2tfwCem9AUTZ0e9Pxa99F5SPl1ChWbKYU+/5C3w2ObMvuTAy 1NXVnYWz+JNuUJ4bHEl40eKIcuM8QL/G0nVxdhkgT1tagjWs1rSoDMrM6MnqYT2ZCc25yRHhNWXyM IZSDR0+FWgC4BldGhUdfB8nRnQAAY0i5yf4TkdO51rKiexpi09016vufJ9PPlGS6Tmjsn3wZP20mW BaPqCKkQ==; Received: from [2001:4bb8:188:23b2:cbb8:fcea:a637:5089] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1q1i8t-00CVho-07; Wed, 24 May 2023 06:38:39 +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/11] fs: factor out a direct_write_fallback helper Date: Wed, 24 May 2023 08:38:08 +0200 Message-Id: <20230524063810.1595778-10-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230524063810.1595778-1-hch@lst.de> References: <20230524063810.1595778-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-Queue-Id: 76AB64000A X-Rspam-User: X-Stat-Signature: ruk3b6yt3hnrrtd6ewhsq16a5onr7jsz X-R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j74jJwnJzVh3+bvsLjAe5pzm4KTOlG0nMzdQSn+QFHGXGvXjcbL84r0Ah6bwV/9kFHZ2+i7fgC0rRHFYSc3cu/sQsVkpC+BNrYMVLw6ho5AhbHxrt5TmFu1GrvzS7nQ59ZwJo3X8kmKZFxA71/uOiWRHprGmcBjdANyKi+2B+JG5BnDJRomVMrLLKg6AzMqqKklr1wpTwA7iWdRD7VAEqnlwGeg7GTEz5VLHM50On8gX/o3VlEgAI0dkl983cOXlYDkbij/Bx20UGWhCfwqMnL1zMf0BrFE2RTsK2DKogvUJABwxQ2q4GJ4xJDKIpdJ3psPp4Eolkb3AvN7FsUJHB6Wt6pbNuHXtEV289oWtYgTJtz74qdqvxm8VbdHYG52XEZK79kvGP7asgs8HT/nYTQNTrZR2NcLzKp6XaRYYroKlraliuz4pjw== 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: Hannes Reinecke --- fs/libfs.c | 36 +++++++++++++++++++++++++ include/linux/fs.h | 2 ++ mm/filemap.c | 66 +++++++++++----------------------------------- 3 files changed, 53 insertions(+), 51 deletions(-) diff --git a/fs/libfs.c b/fs/libfs.c index 89cf614a327158..ad37a49e2ecfb7 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 - 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)) + 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 + */ + 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 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);