From patchwork Mon Apr 24 05:49: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: 13221662 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 7E500C77B78 for ; Mon, 24 Apr 2023 05:50:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 168756B0082; Mon, 24 Apr 2023 01:50:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 09E86900002; Mon, 24 Apr 2023 01:50:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E81E16B0085; Mon, 24 Apr 2023 01:50:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D7F116B0082 for ; Mon, 24 Apr 2023 01:50:16 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id AF6531C5D61 for ; Mon, 24 Apr 2023 05:50:16 +0000 (UTC) X-FDA: 80715209232.18.37C8672 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf28.hostedemail.com (Postfix) with ESMTP id 153A4C0004 for ; Mon, 24 Apr 2023 05:50:14 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=YyUrIXOA; spf=none (imf28.hostedemail.com: domain of BATV+e16e2fc4419b117693a1+7183+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+e16e2fc4419b117693a1+7183+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=1682315415; 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=OgIbgYEwKQKejMeYOKJTDG1ASpjdjHjMo1Pk1taujZE=; b=eiua8GCGTkHdpn7lAMj4fvipUnhM4TNlD7n9QUobZNgPFI1vLOtTWXQbdDaJMELdFIkait F++pv3gClyMtnzCloXA3vxQMhZppwVbu49xo3eHK/sFpLFkjg2WJpNNKhKfIVzE3cW02J1 /VWWbp49O1c4U4I7y/KvH10QYN9qDHQ= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=YyUrIXOA; spf=none (imf28.hostedemail.com: domain of BATV+e16e2fc4419b117693a1+7183+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+e16e2fc4419b117693a1+7183+infradead.org+hch@bombadil.srs.infradead.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1682315415; a=rsa-sha256; cv=none; b=8eZAwtIL0oNjoFYt6KOsjzDI7sFy/CbbTzZeemOz3tnBBWTNqZG0WBLb+BowcTG00xP8q1 MRhs4gwhbS0u4wanm14iVtYj54VxdHBeOqY63vToaur1BASdwbpjmc9sFnLWIzhFHCZeQS XmHCLaWbsh4ZaU4M8VYjGrPCJyl4JCo= 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=OgIbgYEwKQKejMeYOKJTDG1ASpjdjHjMo1Pk1taujZE=; b=YyUrIXOAsfXAxFsM0tPFGM9Qan uPkwIWFjNZRqG+oOXH7PQi1PsPlxfQZlifkTyUEDMKFh5+CU+O7VeR8qiAa6/cLTOcnziGg7FrZdN WHQ80UGCfElD1uMeRdK6iygkWMafy6tsanELOlc6CyYNZDI6bKED31apEBgrrT2rM987Itn8e43SG OMUz8tuWcsyJS+AMMKPPnMB2J6IfA8wNUtc2+Tftlwv0i3hg4/L2GodrSJjuU3MnxwrzjDt9YdQRa BIqOMz0e0+Xfnk0MI6bMMTnZ+tb0QSw5yCUbhjzc6aFqMcLNg7zx5IRNiZyuxF1hS2jGP82Mbj/5v r7PgOY5Q==; Received: from [2001:4bb8:189:a74f:e8a5:5f73:6d2:23b8] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1pqp5I-00FOy6-1x; Mon, 24 Apr 2023 05:49:57 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Miklos Szeredi , "Darrick J. Wong" , Andrew Morton , David Howells , Matthew Wilcox , linux-block@vger.kernel.org, linux-fsdevel@vger.kernel.org, ceph-devel@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, linux-kernel@vger.kernel.org Subject: [PATCH 09/17] fs: factor out a direct_write_fallback helper Date: Mon, 24 Apr 2023 07:49:18 +0200 Message-Id: <20230424054926.26927-10-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230424054926.26927-1-hch@lst.de> References: <20230424054926.26927-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: rspam07 X-Rspamd-Queue-Id: 153A4C0004 X-Rspam-User: X-Stat-Signature: d8masu1ors63erqtyejbsi6dqcjj1tw8 X-HE-Tag: 1682315414-482830 X-HE-Meta: U2FsdGVkX18IzJu9scqtF4IIgXudtQ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ItTrMoKs5Vk8myhU8nqRVtCvBQH+zFH1vNQZLfDDkGf/UAfbQWUvtQzYhsRZOrxNxhPZec6O3P3N0//QZnYPe2yPD8x9iePSOUy/1R2JWmrmhDJk7Lhnf9XguVd7/vPPc6A+B6lP/0ksNjydldWgHklxqzRF007Rz5P+HjD/Qm45Q+ZBuW2+WP+SFmlfmNBX+fP8qlBO7BpWEjJQ81bOdw/q2c0XgVHMj6Cl7tcFmNRxOIWQlZHs7H1QNdYJy6mftx5QJ3RP+X03FIA8WCYEhTYU3gSYBLDEtql5niztTRMX+L8pGohE35MK5QK0L1IRjnL/rXylbq8k3LvqGsEgLRodNKs6dNYYIw== 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 --- 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 4eda519c30022f..a530d36e0d1a3d 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -1618,3 +1618,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 b8ca376e606255..337afdb5024dcb 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2719,6 +2719,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 8c5196cf93a454..bb769746f78b08 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -4009,25 +4009,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 @@ -4035,46 +4031,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);