From patchwork Wed May 24 06:38:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13253308 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 5C60AC7EE2F for ; Wed, 24 May 2023 06:38:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 90123280005; Wed, 24 May 2023 02:38:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8124D280001; Wed, 24 May 2023 02:38:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B33F280005; Wed, 24 May 2023 02:38:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 57858280001 for ; Wed, 24 May 2023 02:38:45 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1DDDE4089C for ; Wed, 24 May 2023 06:38:45 +0000 (UTC) X-FDA: 80824195410.21.CA1DB02 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf17.hostedemail.com (Postfix) with ESMTP id 860CD40012 for ; Wed, 24 May 2023 06:38:43 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=q6Wu9SA2; dmarc=none; 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684910323; a=rsa-sha256; cv=none; b=IgohJiAMzUELdKpuAeaa+U8fbK0wQHmaplX4f2f3OVZo2ptqtGGF9fVMBWO4D3j38mZo2W WmBRMfPrYEn2fIeITQHjAisqaxE4PmMfAoaqgw+4TFrkTBhVudus1P8JHYsLvSZb3US4or 3d6clX8sGZsLRzbvb52VLYV58DHuvCg= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=q6Wu9SA2; dmarc=none; 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684910323; 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=zJveT6acb7t2j3iomHi3xo9Jbx75D5jbjB5CrMVejjo=; b=hmFMsO8dx+lRcwIrArgwGgS+tfgf9ee/90qqsWbEMN1F7Kl8TeMA3eyUArMy4UtwynRcD9 qTwO5OoGWoLDJ3Eb3xtG44z1e8yCq8Pmz+TH9i6JbInX42QK3ILjY3/OP2zLhVHsGo4G7K cxTCHBqw9x1G1Jxrp3Vy/kDrxDAip7Y= 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=zJveT6acb7t2j3iomHi3xo9Jbx75D5jbjB5CrMVejjo=; b=q6Wu9SA2gFSAIh9AiPOt14soTX 8DdCnoqFBQBu9bVMEqanr24O7/zWkWVuK1A3e4GSuOZym5FMwUCETTPqH/YxzZmXnJ60fvuu4y4Cw zSelNT1zRkORnz6HOunLGdkPo9/2iwMfyLLsa6YucU7Ax7LkK+Bxf18JLpANp7XwpxLxolfGBpyzd pEl9ELdZf8XF8Zy0Fe5jt5VYKTQvXvte0rXpg7K5/ebo7gGyug/xu0fIimIGoBr+PsNE9xIJ6abMo cu2VIUtOdIWHXchhn2Fc52BZisN7YgWLK7XyMLHJzwyCVRxere7W7z82n7/oeqEU/DTy9CVwNsr/t oK1WlLZg==; 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 1q1i8i-00CVfn-1t; Wed, 24 May 2023 06:38:28 +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 05/11] filemap: add a kiocb_invalidate_pages helper Date: Wed, 24 May 2023 08:38:04 +0200 Message-Id: <20230524063810.1595778-6-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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 860CD40012 X-Stat-Signature: 6c5qjmj6gjm49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AZeXVBgDNz7UbuiYto7CcC9xmWP47fnBccXKFw/u3IuDi3ZzCy9U/yq8bKQfa4zBr6NszrhoKTVzewp7wbpwI/gASvKrlNegygBGXTWtpK3d225ZgWSeTxzRYNQLzoO5zB3yw5/Nflek7tja/PeBst6nBh7/7NaGjoU6q2TKByNJYdtJzMMNiF8dS/mZhLeCzrep5hmdM7qt4oi8xYr0R5W0NKX1JB92Cd5R0BIqARKCGsUIjVJwmfVNHOhqHrKpyPivzyWEPE1lgc9fV3fT/cijYRHFJWjRxc7umYDIm9eawM46jTG35sB6Rw13Czz1tDQjxaKR3YKnqE0doawsTxdLS6FKWJi+o190+pjxaOcEYZCcoe0BOzeYdI7CHvZUZgAEIbUqL+MqX8vs4zE+9xDht+geedFV0dLnBafhtVgG49v9pZTdQaQ7kzFA8ePJp3Gk7iN+TCDW3vwzKhIG6JfmbX52jQ8fo4GmqSpiVWzNAZEz/WqxWmYvAv6UOh7T+aUGtvrl4ZQm5y8UqlgRo= 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: Factor out a helper that calls filemap_write_and_wait_range and invalidate_inode_pages2_range for the range covered by a write kiocb or returns -EAGAIN if the kiocb is marked as nowait and there would be pages to write or invalidate. Signed-off-by: Christoph Hellwig Reviewed-by: Damien Le Moal Acked-by: Darrick J. Wong Reviewed-by: Hannes Reinecke --- include/linux/pagemap.h | 1 + mm/filemap.c | 48 ++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index 36fc2cea13ce20..6e4c9ee40baa99 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -30,6 +30,7 @@ static inline void invalidate_remote_inode(struct inode *inode) int invalidate_inode_pages2(struct address_space *mapping); int invalidate_inode_pages2_range(struct address_space *mapping, pgoff_t start, pgoff_t end); +int kiocb_invalidate_pages(struct kiocb *iocb, size_t count); int write_inode_now(struct inode *, int sync); int filemap_fdatawrite(struct address_space *); diff --git a/mm/filemap.c b/mm/filemap.c index 5fcd5227f9cae2..a1cb01a4b8046a 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2777,6 +2777,33 @@ int kiocb_write_and_wait(struct kiocb *iocb, size_t count) return filemap_write_and_wait_range(mapping, pos, end); } +int kiocb_invalidate_pages(struct kiocb *iocb, size_t count) +{ + struct address_space *mapping = iocb->ki_filp->f_mapping; + loff_t pos = iocb->ki_pos; + loff_t end = pos + count - 1; + int ret; + + if (iocb->ki_flags & IOCB_NOWAIT) { + /* we could block if there are any pages in the range */ + if (filemap_range_has_page(mapping, pos, end)) + return -EAGAIN; + } else { + ret = filemap_write_and_wait_range(mapping, pos, end); + if (ret) + return ret; + } + + /* + * After a write we want buffered reads to be sure to go to disk to get + * the new data. We invalidate clean cached page from the region we're + * about to write. We do this *before* the write so that we can return + * without clobbering -EIOCBQUEUED from ->direct_IO(). + */ + return invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT, + end >> PAGE_SHIFT); +} + /** * generic_file_read_iter - generic filesystem read routine * @iocb: kernel I/O control block @@ -3820,30 +3847,11 @@ generic_file_direct_write(struct kiocb *iocb, struct iov_iter *from) write_len = iov_iter_count(from); end = (pos + write_len - 1) >> PAGE_SHIFT; - if (iocb->ki_flags & IOCB_NOWAIT) { - /* If there are pages to writeback, return */ - if (filemap_range_has_page(file->f_mapping, pos, - pos + write_len - 1)) - return -EAGAIN; - } else { - written = filemap_write_and_wait_range(mapping, pos, - pos + write_len - 1); - if (written) - goto out; - } - - /* - * After a write we want buffered reads to be sure to go to disk to get - * the new data. We invalidate clean cached page from the region we're - * about to write. We do this *before* the write so that we can return - * without clobbering -EIOCBQUEUED from ->direct_IO(). - */ - written = invalidate_inode_pages2_range(mapping, - pos >> PAGE_SHIFT, end); /* * If a page can not be invalidated, return 0 to fall back * to buffered write. */ + written = kiocb_invalidate_pages(iocb, write_len); if (written) { if (written == -EBUSY) return 0;