From patchwork Mon Apr 24 05:49:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 13221668 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 27303C7618E for ; Mon, 24 Apr 2023 05:50:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B79576B0085; Mon, 24 Apr 2023 01:50:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B26226B0087; Mon, 24 Apr 2023 01:50:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9EF606B0088; Mon, 24 Apr 2023 01:50:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 908876B0085 for ; Mon, 24 Apr 2023 01:50:34 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 70B1C1A010E for ; Mon, 24 Apr 2023 05:50:34 +0000 (UTC) X-FDA: 80715209988.06.42B769A Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) by imf19.hostedemail.com (Postfix) with ESMTP id D95F31A0003 for ; Mon, 24 Apr 2023 05:50:32 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=eaGzgbZa; spf=none (imf19.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=1682315432; 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=DeEMnLNut18pjb9zsoS5o5eSPuPyLSQ1uVXA5zuWlvs=; b=Up/W80/XGBsYxYknb2n/UXRP4orMJklnfO7YoIwIbS1HbFBybkGC/5O73rF9GVCJatEBrv eGx3Jz1dC2iCPP6T6f/izk4DbjwBlSktCHsAQ2Q+mbymMkaWw6my5MLI/Iy8yB4CHoc56/ PUsmrlaMEnHoQk9iEWCbP6+HPkzg6ig= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=infradead.org header.s=bombadil.20210309 header.b=eaGzgbZa; spf=none (imf19.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=1682315432; a=rsa-sha256; cv=none; b=eSY1IY8Xa4yB+02uSC2UEhNg0zSR+fWeMbciCyWyGDUfw9VQRwGM36ttjLGifH0MFTX/KC 9VKKi/0b4xHhCqbSoHrgTk18bk6FikDbgEfIETqpGUpXRAzdMb51IC6LR97KLu45qMI4Dn O3t/LgkpTbqWPB8WohXgvesCv/FeCMw= 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=DeEMnLNut18pjb9zsoS5o5eSPuPyLSQ1uVXA5zuWlvs=; b=eaGzgbZaXKx1KyoFV6zGe9nYHJ ZwankVTk2pK/HLEjyW9HXQbEvrT6olrOT1KuAt81GdNbakAo0iy44P4Damhh264EH/+JUBMVsDYUM /kugXtMO6Gv9hObKPdcxPihekCau7Ms1S+MMRj+dlBau86GBhMGLzLMEMWBDJjhuLS9zMvKps3MPN MHHySMYe2QhwxmEt9zHbO8D812jGdIzNJbIexnk/RGuGbtYWA/Htqd9xtyVpBl2gYuBEHgvrzgeQe VeoXtT8G1+qTJKrcrVDwi79thfMJRDrNOMiZDFn8W6VhlJiNx0v1TuyWCgTI6FGUeXrqgZWtaCrV4 p1Am25Kg==; 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 1pqp5e-00FPC9-1d; Mon, 24 Apr 2023 05:50:18 +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 16/17] block: use iomap for writes to block devices Date: Mon, 24 Apr 2023 07:49:25 +0200 Message-Id: <20230424054926.26927-17-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-Stat-Signature: fozkmfxk4ijz13ead3q664t7dh9khux6 X-Rspam-User: X-Rspamd-Queue-Id: D95F31A0003 X-Rspamd-Server: rspam06 X-HE-Tag: 1682315432-612737 X-HE-Meta: U2FsdGVkX18a2jCNBBdB3WyoE1DiPP3Sb4KI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czqZ09CVHj51OQXoqum0WTJujSs7ZdDLXcpEBbM5VEu8YEh/TG6tlnADPVxqf/8KYORBnSvBIsvPtwdats1vxv9CUFcOZqo3RiCmDmfRth2Bq23AmYPgwO/uxAKihZa/+YN+KXceb5CLZITQsERPsRx/6u77yuomXgF5XDEnFn/iouJezzpRC3eYHfS/9gag/oubX29o02EsOtoHkWt6v9RLev2S2SX17dVmz9Bc5Diuz0JrvMjJZ+Tja+V3PJabK0gksquPJFd3Ym/Fa1WnFijTinMD9zz5U79hq 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: Use iomap in buffer_head compat mode to write to block devices. Signed-off-by: Christoph Hellwig --- block/Kconfig | 1 + block/fops.c | 33 +++++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/block/Kconfig b/block/Kconfig index 941b2dca70db73..672b08f0096ab4 100644 --- a/block/Kconfig +++ b/block/Kconfig @@ -5,6 +5,7 @@ menuconfig BLOCK bool "Enable the block layer" if EXPERT default y + select IOMAP select SBITMAP help Provide block layer support for the kernel. diff --git a/block/fops.c b/block/fops.c index 318247832a7bcf..7910636f8df33b 100644 --- a/block/fops.c +++ b/block/fops.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include "blk.h" @@ -386,6 +387,27 @@ static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter) return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages)); } +static int blkdev_iomap_begin(struct inode *inode, loff_t offset, loff_t length, + unsigned int flags, struct iomap *iomap, struct iomap *srcmap) +{ + struct block_device *bdev = I_BDEV(inode); + loff_t isize = i_size_read(inode); + + iomap->bdev = bdev; + iomap->offset = ALIGN_DOWN(offset, bdev_logical_block_size(bdev)); + if (WARN_ON_ONCE(iomap->offset >= isize)) + return -EIO; + iomap->type = IOMAP_MAPPED; + iomap->addr = iomap->offset; + iomap->length = isize - iomap->offset; + iomap->flags |= IOMAP_F_BUFFER_HEAD; + return 0; +} + +static const struct iomap_ops blkdev_iomap_ops = { + .iomap_begin = blkdev_iomap_begin, +}; + static int blkdev_writepage(struct page *page, struct writeback_control *wbc) { return block_write_full_page(page, blkdev_get_block, wbc); @@ -530,6 +552,11 @@ blkdev_direct_write(struct kiocb *iocb, struct iov_iter *from) return written; } +static ssize_t blkdev_buffered_write(struct kiocb *iocb, struct iov_iter *from) +{ + return iomap_file_buffered_write(iocb, from, &blkdev_iomap_ops); +} + /* * Write data to the block device. Only intended for the block device itself * and the raw driver which basically is a fake block device. @@ -575,16 +602,14 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from) iov_iter_truncate(from, size); } - current->backing_dev_info = bdev->bd_disk->bdi; if (iocb->ki_flags & IOCB_DIRECT) { ret = blkdev_direct_write(iocb, from); if (ret >= 0 && iov_iter_count(from)) ret = direct_write_fallback(iocb, from, ret, - generic_perform_write(iocb, from)); + blkdev_buffered_write(iocb, from)); } else { - ret = generic_perform_write(iocb, from); + ret = blkdev_buffered_write(iocb, from); } - current->backing_dev_info = NULL; if (ret > 0) ret = generic_write_sync(iocb, ret);