Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5339276rwd; Tue, 23 May 2023 23:40:40 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4+4MQtmz4DQ/xQmjbol29mVydmuoHlMZwq4NjAZhJn+MmNRxqdyMvOTzX7Nvzbp9Ssx5ji X-Received: by 2002:a05:6a00:2488:b0:643:980:65b with SMTP id c8-20020a056a00248800b006430980065bmr2346290pfv.2.1684910439884; Tue, 23 May 2023 23:40:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684910439; cv=none; d=google.com; s=arc-20160816; b=nPKZf8i11tUkzllPyi538knbT3MLnDuCjnTfwuYaqqbUWNMTLrrx9vk4swRlXwa1e8 DVY72Duo6quoxeay9B94o7F7RZNHgtCynJ1apjdmrQH35Vl0PWrIkPKR+mGvqcl7Z0P0 m27m56gLyz6ke8hhGbWbgLt8qC54RuRZj/7gmOBYvL1g49DNKaxN9He27tCT3Zfc1MpE tMwl+2WDpSaRLzubOr03ywymcOBaNf5L4LlIwITfFyJWvxp9d+WwBBtlsjiQWlTmwUNk Qfv+Kwyknw5iLM//kmyFJ7J+qqIa5fFW285Hyqqe+b4Km1byEWG2IuiM/mgOVYqZ16+o ls8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=zJveT6acb7t2j3iomHi3xo9Jbx75D5jbjB5CrMVejjo=; b=wOucQbTm6Hqc8iXXpafoaoaLEdmRt6JAoUYY//xF6AldmU6VFfbX1C/9gtuqn9VLKu nIHvzXefptBMslmF2QPJ8nS5GXDNHilRY9rBX/B3w0ExsomO6ME73dWPs2r9YZMNGJEJ HxP0YPZxg2831VEZWBskjKOD8N/PrTb+25KHAl58cn+WdbMFCw5JW4eGbVO9fjEJ7J1K Hp8nfGeJwQ2+118b6R/qNQ3ZP/ILHut40Y2oPokZ/2j1lHwGjIUi787FnTnhh3T2hxJ4 //v5FyIBPqCJNDn7vIcXuHSL02GgKpie/cbUZ0QUUH5hXmmPuoslxmh3G+FOFnYjGyV2 5Oqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=q6Wu9SA2; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b30-20020a63931e000000b005348f910e65si193060pge.824.2023.05.23.23.40.27; Tue, 23 May 2023 23:40:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=bombadil.20210309 header.b=q6Wu9SA2; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239793AbjEXGit (ORCPT + 99 others); Wed, 24 May 2023 02:38:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239734AbjEXGih (ORCPT ); Wed, 24 May 2023 02:38:37 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E0C3CA3; Tue, 23 May 2023 23:38:36 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html X-Spam-Status: No, score=-4.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org 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 --- 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; -- 2.39.2