From patchwork Fri May 19 09:35:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13248020 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 62417C7EE2D for ; Fri, 19 May 2023 09:35:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DA349280002; Fri, 19 May 2023 05:35:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D2C35280001; Fri, 19 May 2023 05:35:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B7F1C280002; Fri, 19 May 2023 05:35:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A2066280001 for ; Fri, 19 May 2023 05:35:57 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 72A5F80986 for ; Fri, 19 May 2023 09:35:57 +0000 (UTC) X-FDA: 80806497954.24.94A0947 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf08.hostedemail.com (Postfix) with ESMTP id C59F8160014 for ; Fri, 19 May 2023 09:35:55 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=xArZwU++; dmarc=none; spf=none (imf08.hostedemail.com: domain of BATV+aecf67361b95543ec79f+7208+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+aecf67361b95543ec79f+7208+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=1684488955; 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=tGCXuJSpKeMdDJfNmlfVF+NepybRn/cvj/Ka18QV4Tk=; b=c8wl9JCzs9llJDIwjUHrj7pTZfAT5WyOVOAIgpE8RWa7AqjAtyae09Hb4snWSscLrGDORo WNqnnRPqgvMxVokg19gz4l0IspDtYXmlvU0BEFR+Ucz51QXpaWP1zD1XvOH8hnxVaN+7cI bPTiVNMVghcpEeQK5CsHLJmPFD4RSfQ= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=xArZwU++; dmarc=none; spf=none (imf08.hostedemail.com: domain of BATV+aecf67361b95543ec79f+7208+infradead.org+hch@bombadil.srs.infradead.org has no SPF policy when checking 198.137.202.133) smtp.mailfrom=BATV+aecf67361b95543ec79f+7208+infradead.org+hch@bombadil.srs.infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684488955; a=rsa-sha256; cv=none; b=4aPx5YEsGc6lvwDt9Id8fRXkt6phSZNu/yJzDAhDa4mHEJdiSkeIM15vW3M0eyyTLSrbgm U/FvbhkQ9E4X/Dbk37rnF5RrLufwtG/44YjqRks0QvM7Zs9U+ax5Simp+3k4HTBeP1hV/L jHHMVoUL8OMqiqTXgGOz+0SXN4mmXSc= 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=tGCXuJSpKeMdDJfNmlfVF+NepybRn/cvj/Ka18QV4Tk=; b=xArZwU++0j0FGgKFO7gwbG5SAC a1/RPaFHnsRd+UOwpiKD4Hll+FQbOU/DKkcxbAcjid8e3CwS1tnTNCYZM+x+KyqcKxXicxUupis0e ZB3+K10LpIi0KqMuYdfgZL6dZTSTQbvZkH6r8F/El40AXbo89yVa1KaDyWLp3QCGRzcYOWW19+z/N 2m+4ElhodZEmxP0Tr1ON4pIvchIdcns7DtlQG0WP60hmdAH+rnDgdc0p0Ohgew+bUcpPErepKoeep u14tVprafp/sJaOi2+meSJDf/HCFZ/FFjC0EYErlFi9kYKElkKwIgcEtgjqPYwnbsZPkkX1lMh8UH EPHXXp9Q==; Received: from [2001:4bb8:188:3dd5:e8d0:68bb:e5be:210a] (helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.96 #2 (Red Hat Linux)) id 1pzwWO-00FjeP-1n; Fri, 19 May 2023 09:35:36 +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 (open list:F2FS FILE SYSTEM), cluster-devel@redhat.com, linux-xfs@vger.kernel.org, linux-nfs@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 04/13] filemap: add a kiocb_write_and_wait helper Date: Fri, 19 May 2023 11:35:12 +0200 Message-Id: <20230519093521.133226-5-hch@lst.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230519093521.133226-1-hch@lst.de> References: <20230519093521.133226-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: C59F8160014 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: w8txaosuxosg66n36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+nYb0N3jmFgO5arjW9F4YrwyunEyFEc+gMgm2RsbAFfXhR6y6WXhXlm7WwRVyCD4Uit2ricLZBo6vsSWqdibbOu/uGAdWbf3IXUpHYEp+yjriZT15gefVL6nXoU56Vtvdn6EtS/yF9mclQNH9TYCNxlgPo4+olCTrM8V18z+D46uyfBylcskF6ni0brhAds2Do4ZQTh9INyR6D7xDeRgCVYNd650UAy9Sgd5RQXIO+Sb1IeDtTw8Y9Psdv1pxY/cngWWvZah4LXpUCxm52uvTCfFJBHQFy3u0/1SQvFYUIi3Y/ohlrCkYsHF1FTiTGtWdXOQZS5HozZykL2D5gjtAtBGrDVz6MJaILDAMI8ZuvSWHrZihykhusc0ts8z6ZAIo8VgQG0ycMY6WhXuJ7TCfz0ESsXhDyuWphXkRJP7CFvk6Jm8Y4vQ5JAVsM8= 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 does filemap_write_and_wait_range for a the range covered by a read kiocb, or returns -EAGAIN if the kiocb is marked as nowait and there would be pages to write. Signed-off-by: Christoph Hellwig Reviewed-by: Damien Le Moal --- block/fops.c | 18 +++--------------- include/linux/pagemap.h | 2 ++ mm/filemap.c | 30 ++++++++++++++++++------------ 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/block/fops.c b/block/fops.c index d2e6be4e3d1c7d..c194939b851cfb 100644 --- a/block/fops.c +++ b/block/fops.c @@ -576,21 +576,9 @@ static ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to) goto reexpand; /* skip atime */ if (iocb->ki_flags & IOCB_DIRECT) { - struct address_space *mapping = iocb->ki_filp->f_mapping; - - if (iocb->ki_flags & IOCB_NOWAIT) { - if (filemap_range_needs_writeback(mapping, pos, - pos + count - 1)) { - ret = -EAGAIN; - goto reexpand; - } - } else { - ret = filemap_write_and_wait_range(mapping, pos, - pos + count - 1); - if (ret < 0) - goto reexpand; - } - + ret = kiocb_write_and_wait(iocb, count); + if (ret < 0) + goto reexpand; file_accessed(iocb->ki_filp); ret = blkdev_direct_IO(iocb, to); diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index a56308a9d1a450..36fc2cea13ce20 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 write_inode_now(struct inode *, int sync); int filemap_fdatawrite(struct address_space *); int filemap_flush(struct address_space *); @@ -54,6 +55,7 @@ int filemap_check_errors(struct address_space *mapping); void __filemap_set_wb_err(struct address_space *mapping, int err); int filemap_fdatawrite_wbc(struct address_space *mapping, struct writeback_control *wbc); +int kiocb_write_and_wait(struct kiocb *iocb, size_t count); static inline int filemap_write_and_wait(struct address_space *mapping) { diff --git a/mm/filemap.c b/mm/filemap.c index bf693ad1da1ece..2d7712b13b95c9 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2762,6 +2762,21 @@ ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *iter, } EXPORT_SYMBOL_GPL(filemap_read); +int kiocb_write_and_wait(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; + + if (iocb->ki_flags & IOCB_NOWAIT) { + if (filemap_range_needs_writeback(mapping, pos, end)) + return -EAGAIN; + return 0; + } + + return filemap_write_and_wait_range(mapping, pos, end); +} + /** * generic_file_read_iter - generic filesystem read routine * @iocb: kernel I/O control block @@ -2797,18 +2812,9 @@ generic_file_read_iter(struct kiocb *iocb, struct iov_iter *iter) struct address_space *mapping = file->f_mapping; struct inode *inode = mapping->host; - if (iocb->ki_flags & IOCB_NOWAIT) { - if (filemap_range_needs_writeback(mapping, iocb->ki_pos, - iocb->ki_pos + count - 1)) - return -EAGAIN; - } else { - retval = filemap_write_and_wait_range(mapping, - iocb->ki_pos, - iocb->ki_pos + count - 1); - if (retval < 0) - return retval; - } - + retval = kiocb_write_and_wait(iocb, count); + if (retval < 0) + return retval; file_accessed(file); retval = mapping->a_ops->direct_IO(iocb, iter);