From patchwork Mon Apr 24 05:49:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13221660 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 E9257C7EE22 for ; Mon, 24 Apr 2023 05:50:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8B7696B0080; Mon, 24 Apr 2023 01:50:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 866E26B0081; Mon, 24 Apr 2023 01:50:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 66C3D6B0082; Mon, 24 Apr 2023 01:50:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 586C66B0080 for ; Mon, 24 Apr 2023 01:50:02 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 14215A00F9 for ; Mon, 24 Apr 2023 05:50:02 +0000 (UTC) X-FDA: 80715208644.26.0950787 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf10.hostedemail.com (Postfix) with ESMTP id 6E88DC000F for ; Mon, 24 Apr 2023 05:50:00 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=LPBHKUTF; spf=none (imf10.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=1682315400; 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=FZ/oX6CCuoV1bgBWAGOx8K6ORykPZ4voWywQ/l5w2pE=; b=kFNhU+exfyIjh3AcGQ7lLIAUqBd2RVcfBCaR/rLUgNR/fXXdTaT2REcSgAcHtcEs6bynVj JHNHfClo63j4DrYVuyeOBZKncgCTUlb00Ppl1+81TZybXzVa9E0BJrzDA8cjNyM7psF3Tu TRYtNXnBuZxJ3wWDJh/qfCYPHx39CfQ= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=LPBHKUTF; spf=none (imf10.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=1682315400; a=rsa-sha256; cv=none; b=WqwfrGvk8XCvqXJ1fccIdEeuhNIKDaCAYKcCfV+sfjXAGb6xSi0aexznRAiwMhdmnXBRqK ZN1WZ+tUujGL6DbRB9iFwjjgfFqhgzOwCVe3aUmIax4Lwpprjrdynl5g15Ih5h+XGlGy8w X6fKUH+e/dEDp4OaDHWvjRWb4ApENiE= 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=FZ/oX6CCuoV1bgBWAGOx8K6ORykPZ4voWywQ/l5w2pE=; b=LPBHKUTFShS6lFcY9XAvrDO0zy 2lv9lSVBz58fAlT2EQi21jkueDOTS4twSStbFX5syhvx3ExuYVTYHf7Uj4O+4gRkholvUzQfsBcGM tdLNzfyJevRkb6vfEGgqTSROsy9NbDiGm1YGQ+aUl4dzdtQ7j5D4onehk+I1FYoOC/PnDCk9rdzOP Jw8L43J/TAXvVuHlvExzZqOHSCZWdqzV2HplCDpDQjup3RzBEOjFY66ho4n+n3SwT6MAzQBaWtIUq 5KlfoqP7K2wthTABWhRz6wnhfsdxVwTKphcpG74JgjuxMocweMuLa0NGt3sY0/TpYPdgIZSJTnEL8 OEkt1oog==; 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 1pqp59-00FOvi-2z; Mon, 24 Apr 2023 05:49:48 +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 06/17] filemap: add a kiocb_write_and_wait helper Date: Mon, 24 Apr 2023 07:49:15 +0200 Message-Id: <20230424054926.26927-7-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-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 6E88DC000F X-Stat-Signature: 4rxc5qybz8admdj7bstywooz1n5mnurw X-HE-Tag: 1682315400-560161 X-HE-Meta: U2FsdGVkX18ipAUARHM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Z49NE5yJq2/Ml8xO6YSTTawWHsrc0e9eg5yrhaQa1mQmhD70qHBdajWtdStpVOZRSvQPT2QSK+lEBFtZmT0sqsp3rb5n8iwhvNMjse0Xf+zKSaTzLPUyMaGTYmvA2UQ2uCu5Y2Gk1OKgEjX7+TR9iX5/xLhL78cD+Ko0qzmwuZ7Onw+mS9iHV7tq713tIBzjUIr9nrXeMA9igYx4JsIrJzeug9+xT8eavIkw10+unvgK/72G25HjT8F1sM+tlLzFIQJGdZNRp51Qak3tYaQB8CiTN0gU22r7hOlxGNxuS4DZDQ8LgO7mX/Wh9xsGAf+0wQPljxB/j5Ie61ceo3Snx8dBQNy4VBtvnPqUaaLEd7PLkbOOQjDie1SXrEpDUeoAwYcEm1OhT+gV9HPRErV5Vs= 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 --- 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 0acb8e1fb7afdc..51f7aea51169c4 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 0110bde3708b3f..28ea9804191d6b 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -2765,6 +2765,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 @@ -2800,18 +2815,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);