From patchwork Wed May 31 07:50:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13261642 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 DFEB7C88C83 for ; Wed, 31 May 2023 07:50:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DCD4C6B007D; Wed, 31 May 2023 03:50:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D800A6B007E; Wed, 31 May 2023 03:50:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA9B9900002; Wed, 31 May 2023 03:50:53 -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 A6CE66B007D for ; Wed, 31 May 2023 03:50:53 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 80CE7160236 for ; Wed, 31 May 2023 07:50:53 +0000 (UTC) X-FDA: 80849778786.05.5FF00E8 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf17.hostedemail.com (Postfix) with ESMTP id B3CBD4001A for ; Wed, 31 May 2023 07:50:51 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=UwK1FHTH; spf=none (imf17.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=1685519451; 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=oeUDNAIuBGOOZ56XkyqMnjDXKnQawdRHpRl2i305rtg=; b=KPir+te76Ty253BzOTE2TD9ZZnI+r4n7mOuFhqbHRs7nqjN6+zMoj4tmQnUFKX59nW0gpO NAwlzYzPJIhvZYoNffSDBxYPEEKwDe8vjY3GO+T3smwndcO8KgQZd0YQJAe79qe/XFk2vA m2Qp0B7BhapA5hyJP3JMWN8yWOtHcNk= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=UwK1FHTH; spf=none (imf17.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685519451; a=rsa-sha256; cv=none; b=Inj9c/vycmRwJOzUgzwtJey7MUPiX3ZtHWUYpSedhanLzA27aThOkGA2oggvdVAGYd2fT7 QyAi7uQyvRDJM4+5Er89zIfZb29OZiJaGw0RJw/rqhyLNAeHJD68b7manKxzhavTmiMvfb xXfF8kNWww7dFadx+Cb8YXaVssPq2uw= 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=oeUDNAIuBGOOZ56XkyqMnjDXKnQawdRHpRl2i305rtg=; b=UwK1FHTHtOyBkGhF5SwrqCYJaI V5Un7eCVkDVa2V6bOZ5kcaWv3HZlwGRURrEg1/HxkiYB6u5glE/X4tofTPC+PKyi2g6J3lruXgfP3 UHhLKQUSgT8GtfPqc0agUDElsY8y9lGiTs5xfOfH746K52QwP2aT6XR7+J+Qy5DxdIoQ0710G/Cm+ xsB/kIaUPj2s++pH24XeVlWjAza25Q0tRJOfURvfha9P6ipbqROKE1VC+9VUe9b5gp2jGi0+kt9r/ +G7NSTe6LjzFZbPWe2mPeEvoiWJYNa+d00SyQ9qv1QS47XF0PEnXMrGpmL4zg4e3WT1Ul4gE8Z8jH fBjN8G+w==; 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 1q4GbS-00GVpj-1y; Wed, 31 May 2023 07:50:43 +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, Hannes Reinecke Subject: [PATCH 05/12] filemap: add a kiocb_invalidate_pages helper Date: Wed, 31 May 2023 09:50:19 +0200 Message-Id: <20230531075026.480237-6-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-Rspamd-Queue-Id: B3CBD4001A X-Rspam-User: X-Stat-Signature: w974q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/YrlpMY/f7Pm270xvEBLjZ4RqqJ1lAE7YsWAePt7G/F+5qqsMOzTabEbs9h0cK1lE1iIkzRBLqzck57m9BwVbb9pIeSCYkPXlKAdCNdq1rMhWBhMKwJNO2dDwNORmzYVbfyzBwgAv5qn5xdaRm+ULx2CARVDo+WqYPfkwDomuZbJEOX1dS7gG8eDOJ1LKPpfcdfoAdMzk1z63Co0gyeNTVgkqQoumPN7fXl2S73ONKwtIdf+OhC5Ymr3vhObvw/rbzJTtReW4CncOlhxLG3o6xbQADF0LDMEFR2D3UUHOspnPfn1PpS5Ty6Y3ckLIksmsCW2RwFQzlEXS2t1Eq0f0jm7kce5/Il1VUj9DOnxtxQHOZQRVFnD/3dcDqiyzEiKxifA3j4Wt9vtqGKn8pPv5fKqZ4VfcDAlUHL1hP4hWTw0nYuMjNH0rmM5c4xkCcH5tjW5v02BnyIqnrUFdH2zEsEx0T4A22VRdOKne5xdNI/Ge8DMeEhhERjyVZTiTkFW1oobRY9zu8hwWkIJv2kovYZZD322ojRCtWEEjGbE= 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 Reviewed-by: Hannes Reinecke Acked-by: Darrick J. Wong --- 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;