From: Yu Kuai <yukuai1@huaweicloud.com>
To: axboe@kernel.dk, roger.pau@citrix.com, colyli@suse.de,
kent.overstreet@gmail.com, joern@lazybastard.org,
miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com,
sth@linux.ibm.com, hoeppner@linux.ibm.com, hca@linux.ibm.com,
gor@linux.ibm.com, agordeev@linux.ibm.com, jejb@linux.ibm.com,
martin.petersen@oracle.com, clm@fb.com, josef@toxicpanda.com,
dsterba@suse.com, viro@zeniv.linux.org.uk, brauner@kernel.org,
nico@fluxnic.net, xiang@kernel.org, chao@kernel.org,
tytso@mit.edu, adilger.kernel@dilger.ca, jack@suse.com,
konishi.ryusuke@gmail.com, willy@infradead.org,
akpm@linux-foundation.org, hare@suse.de, p.raghav@samsung.com
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
xen-devel@lists.xenproject.org, linux-bcache@vger.kernel.org,
linux-mtd@lists.infradead.org, linux-s390@vger.kernel.org,
linux-scsi@vger.kernel.org, linux-bcachefs@vger.kernel.org,
linux-btrfs@vger.kernel.org, linux-fsdevel@vger.kernel.org,
linux-erofs@lists.ozlabs.org, linux-ext4@vger.kernel.org,
linux-nilfs@vger.kernel.org, yukuai3@huawei.com,
yukuai1@huaweicloud.com, yi.zhang@huawei.com,
yangerkun@huawei.com
Subject: [PATCH RFC v3 for-6.8/block 01/17] block: add some bdev apis
Date: Thu, 21 Dec 2023 16:56:56 +0800 [thread overview]
Message-ID: <20231221085712.1766333-2-yukuai1@huaweicloud.com> (raw)
In-Reply-To: <20231221085712.1766333-1-yukuai1@huaweicloud.com>
From: Yu Kuai <yukuai3@huawei.com>
Those apis will be used for other modules, so that bd_inode won't be
accessed directly from other modules.
Signed-off-by: Yu Kuai <yukuai3@huawei.com>
---
block/bdev.c | 148 +++++++++++++++++++++++++++++++++++++++++
block/blk.h | 2 -
include/linux/blkdev.h | 17 +++++
3 files changed, 165 insertions(+), 2 deletions(-)
diff --git a/block/bdev.c b/block/bdev.c
index 750aec178b6a..6204621c6db6 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -89,6 +89,25 @@ void invalidate_bdev(struct block_device *bdev)
}
EXPORT_SYMBOL(invalidate_bdev);
+/**
+ * invalidate_bdev_pages - Invalidate clean unused buffers and pagecache.
+ * @bdev: the block device which holds the cache to invalidate
+ * @start: the offset 'from' which to invalidate
+ * @end: the offset 'to' which to invalidate (inclusive)
+ *
+ * This function removes pages that are clean, unmapped and unlocked,
+ * as well as shadow entries. It will not block on IO activity.
+ *
+ * If you want to remove all the pages of one block device, regardless of
+ * their use and writeback state, use truncate_bdev_range().
+ */
+void invalidate_bdev_range(struct block_device *bdev, pgoff_t start,
+ pgoff_t end)
+{
+ invalidate_mapping_pages(bdev->bd_inode->i_mapping, start, end);
+}
+EXPORT_SYMBOL_GPL(invalidate_bdev_range);
+
/*
* Drop all buffers & page cache for given bdev range. This function bails
* with error if bdev has other exclusive owner (such as filesystem).
@@ -121,6 +140,7 @@ int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode,
lstart >> PAGE_SHIFT,
lend >> PAGE_SHIFT);
}
+EXPORT_SYMBOL_GPL(truncate_bdev_range);
static void set_init_blocksize(struct block_device *bdev)
{
@@ -1102,3 +1122,131 @@ void bdev_statx_dioalign(struct inode *inode, struct kstat *stat)
blkdev_put_no_open(bdev);
}
+
+/**
+ * bdev_read_folio - Read into block device page cache.
+ * @bdev: the block device which holds the cache to read.
+ * @pos: the offset that allocated folio will contain.
+ *
+ * Read one page into the block device page cache. If it succeeds, the folio
+ * returned will contain @pos;
+ *
+ * Return: Uptodate folio on success, ERR_PTR() on failure.
+ */
+struct folio *bdev_read_folio(struct block_device *bdev, loff_t pos)
+{
+ return mapping_read_folio_gfp(bdev->bd_inode->i_mapping,
+ pos >> PAGE_SHIFT, GFP_KERNEL);
+}
+EXPORT_SYMBOL_GPL(bdev_read_folio);
+
+/**
+ * bdev_get_folio - Find and get a reference to a folio.
+ * @bdev: the block device which holds the address_space to search.
+ * @pos: the offset the returned folio will contain.
+ * @fgp_flags: %FGP flags modify how the folio is returned.
+ * @gfp: Memory allocation flags to use if %FGP_CREAT is specified.
+ *
+ * Looks up the page cache entry at @bdev->bd_inode->i_mapping from @pos. If
+ * this function returns a folio, it is returned with an increased refcount.
+ *
+ * Return: The found folio or an ERR_PTR() otherwise.
+ */
+struct folio *bdev_get_folio(struct block_device *bdev, loff_t pos,
+ fgf_t fgp_flags, gfp_t gfp)
+{
+ return __filemap_get_folio(bdev->bd_inode->i_mapping, pos >> PAGE_SHIFT,
+ fgp_flags, gfp);
+}
+EXPORT_SYMBOL_GPL(bdev_get_folio);
+
+/**
+ * bdev_wb_err_check - Has block device writeback error occurred?
+ * @bdev: the block device to check.
+ * @since: Previously-sampled @bdev->bd_inode->i_mapping->wb_err.
+ *
+ * Grab @bdev->bd_inode->i_mapping->wb_err, and see if it has changed @since
+ * the given value was sampled.
+ *
+ * Return: The latest error or 0 if it hasn't changed.
+ */
+int bdev_wb_err_check(struct block_device *bdev, errseq_t since)
+{
+ return errseq_check(&bdev->bd_inode->i_mapping->wb_err, since);
+}
+EXPORT_SYMBOL_GPL(bdev_wb_err_check);
+
+/**
+ * bdev_wb_err_check_and_advance() - Check block device writeback error and
+ * advance to current value.
+ * @bdev: the block device to check;
+ * @since: Pointer to previously-sampled @bdev->bd_inode->i_mapping->wb_err to
+ * check against and advance.
+ *
+ * Grab @bdev->bd_inode->i_mapping->wb_err, and see whether it matches the
+ * value that @since points to. If it does, then just return 0; If it doesn't,
+ * then the value has changed. Set the "seen" flag, and try to swap it into
+ * place as the new eseq value. Then, set that value as the new @since value,
+ * and return whatever the error portion is set to.
+ *
+ * Return: Negative errno if one has been stored, or 0 if no new error has
+ * occurred.
+ */
+int bdev_wb_err_check_and_advance(struct block_device *bdev, errseq_t *since)
+{
+ return errseq_check_and_advance(&bdev->bd_inode->i_mapping->wb_err,
+ since);
+}
+EXPORT_SYMBOL_GPL(bdev_wb_err_check_and_advance);
+
+/**
+ * bdev_balance_dirty_pages_ratelimited - balance dirty memory state.
+ * @bdev: the block device which was dirtied.
+ *
+ * Check the system's dirty state and will initiate writeback if needed.
+ */
+void bdev_balance_dirty_pages_ratelimited(struct block_device *bdev)
+{
+ return balance_dirty_pages_ratelimited(bdev->bd_inode->i_mapping);
+}
+EXPORT_SYMBOL_GPL(bdev_balance_dirty_pages_ratelimited);
+
+/**
+ * bdev_async_readahead - readahead for marked block device pages
+ * @bdev: the block device to read.
+ * @ra: file_ra_state which holds the readahead state.
+ * @file: Used by the filesystem for authentication.
+ * @index: Index of first page to be read.
+ * @req_count: Total number of pages being read by the caller.
+ *
+ * Read multiple pages into the block device page cache. The readahead logic may
+ * decide to piggyback more pages onto the read request if access patterns
+ * suggest it will improve performance.
+ */
+void bdev_sync_readahead(struct block_device *bdev, struct file_ra_state *ra,
+ struct file *file, pgoff_t index,
+ unsigned long req_count)
+{
+ struct file_ra_state tmp_ra = {};
+
+ if (!ra) {
+ ra = &tmp_ra;
+ file_ra_state_init(ra, bdev->bd_inode->i_mapping);
+ }
+ page_cache_sync_readahead(bdev->bd_inode->i_mapping, ra, file, index,
+ req_count);
+}
+EXPORT_SYMBOL_GPL(bdev_sync_readahead);
+
+/**
+ * bdev_attach_wb - associate an block device with its wb
+ * @bdev: block device of interest
+ *
+ * If @bdev->bd_inode doesn't have its wb, associate it with the wb matching the
+ * %current.
+ */
+void bdev_attach_wb(struct block_device *bdev)
+{
+ inode_attach_wb(bdev->bd_inode, NULL);
+}
+EXPORT_SYMBOL_GPL(bdev_attach_wb);
diff --git a/block/blk.h b/block/blk.h
index 1ef920f72e0f..ce5fcd927eaa 100644
--- a/block/blk.h
+++ b/block/blk.h
@@ -465,8 +465,6 @@ extern struct device_attribute dev_attr_events_poll_msecs;
extern struct attribute_group blk_trace_attr_group;
blk_mode_t file_to_blk_mode(struct file *file);
-int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode,
- loff_t lstart, loff_t lend);
long blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg);
long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg);
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index bc236e77d85e..ccac7d32bb86 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -24,6 +24,7 @@
#include <linux/sbitmap.h>
#include <linux/uuid.h>
#include <linux/xarray.h>
+#include <linux/pagemap.h>
struct module;
struct request_queue;
@@ -1475,6 +1476,22 @@ struct block_device *blkdev_get_no_open(dev_t dev);
void blkdev_put_no_open(struct block_device *bdev);
struct block_device *I_BDEV(struct inode *inode);
+void invalidate_bdev_range(struct block_device *bdev, pgoff_t start,
+ pgoff_t end);
+int truncate_bdev_range(struct block_device *bdev, blk_mode_t mode,
+ loff_t lstart, loff_t lend);
+struct folio *bdev_read_folio(struct block_device *bdev, loff_t pos);
+struct folio *bdev_get_folio(struct block_device *bdev, loff_t pos,
+ fgf_t fgp_flags, gfp_t gfp);
+int bdev_wb_err_check(struct block_device *bdev, errseq_t since);
+int bdev_wb_err_check_and_advance(struct block_device *bdev, errseq_t *since);
+void bdev_balance_dirty_pages_ratelimited(struct block_device *bdev);
+void bdev_sync_readahead(struct block_device *bdev, struct file_ra_state *ra,
+ struct file *file, pgoff_t index,
+ unsigned long req_count);
+void bdev_attach_wb(struct block_device *bdev);
+void bdev_associated_mapping(struct block_device *bdev,
+ struct address_space *mapping);
#ifdef CONFIG_BLOCK
void invalidate_bdev(struct block_device *bdev);
--
2.39.2
next prev parent reply other threads:[~2023-12-21 8:59 UTC|newest]
Thread overview: 37+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-21 8:56 [PATCH RFC v3 for-6.8/block 00/17] block: don't access bd_inode directly from other modules Yu Kuai
2023-12-21 8:56 ` Yu Kuai [this message]
2023-12-21 8:56 ` [PATCH RFC v3 for-6.8/block 02/17] xen/blkback: use bdev api in xen_update_blkif_status() Yu Kuai
2024-01-04 11:06 ` Jan Kara
2024-01-04 12:19 ` Yu Kuai
2024-01-04 15:16 ` Jan Kara
2024-01-05 6:08 ` Christoph Hellwig
2023-12-21 8:56 ` [PATCH RFC v3 for-6.8/block 03/17] bcache: use bdev api in read_super() Yu Kuai
2023-12-21 8:56 ` [PATCH RFC v3 for-6.8/block 04/17] mtd: block2mtd: use bdev apis Yu Kuai
2024-01-04 11:28 ` Jan Kara
2024-01-04 12:22 ` Yu Kuai
2024-01-05 6:10 ` Christoph Hellwig
2024-01-05 10:31 ` Yu Kuai
2023-12-21 8:57 ` [PATCH RFC v3 for-6.8/block 05/17] s390/dasd: use bdev api in dasd_format() Yu Kuai
2023-12-21 8:57 ` [PATCH RFC v3 for-6.8/block 06/17] scsicam: use bdev api in scsi_bios_ptable() Yu Kuai
2023-12-21 8:57 ` [PATCH RFC v3 for-6.8/block 07/17] bcachefs: remove dead function bdev_sectors() Yu Kuai
2024-04-11 17:49 ` Kent Overstreet
2023-12-21 8:57 ` [PATCH RFC v3 for-6.8/block 08/17] bio: export bio_add_folio_nofail() Yu Kuai
2023-12-21 8:57 ` [PATCH RFC v3 for-6.8/block 09/17] btrfs: use bdev apis Yu Kuai
2023-12-23 17:31 ` Matthew Wilcox
2023-12-23 18:39 ` Kent Overstreet
2024-01-04 11:49 ` Jan Kara
2024-04-10 17:28 ` David Sterba
2023-12-21 8:57 ` [PATCH RFC v3 for-6.8/block 10/17] cramfs: use bdev apis in cramfs_blkdev_read() Yu Kuai
2023-12-21 8:58 ` [PATCH RFC v3 for-6.8/block 11/17] erofs: use bdev api Yu Kuai
2024-01-04 12:02 ` Jan Kara
2024-01-04 12:32 ` Yu Kuai
2024-01-05 4:43 ` Gao Xiang
2023-12-21 8:58 ` [PATCH RFC v3 for-6.8/block 12/17] nilfs2: use bdev api in nilfs_attach_log_writer() Yu Kuai
2023-12-21 14:54 ` Ryusuke Konishi
2023-12-21 8:58 ` [PATCH RFC v3 for-6.8/block 13/17] jbd2: use bdev apis Yu Kuai
2024-01-04 12:11 ` Jan Kara
2023-12-21 8:58 ` [PATCH RFC v3 for-6.8/block 14/17] buffer: add a new helper to read sb block Yu Kuai
2024-01-04 12:22 ` Jan Kara
2023-12-21 8:58 ` [PATCH RFC v3 for-6.8/block 15/17] ext4: use " Yu Kuai
2023-12-21 8:59 ` [PATCH RFC v3 for-6.8/block 16/17] ext4: remove block_device_ejected() Yu Kuai
2023-12-21 8:59 ` [PATCH RFC v3 for-6.8/block 17/17] ext4: use bdev apis Yu Kuai
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231221085712.1766333-2-yukuai1@huaweicloud.com \
--to=yukuai1@huaweicloud.com \
--cc=adilger.kernel@dilger.ca \
--cc=agordeev@linux.ibm.com \
--cc=akpm@linux-foundation.org \
--cc=axboe@kernel.dk \
--cc=brauner@kernel.org \
--cc=chao@kernel.org \
--cc=clm@fb.com \
--cc=colyli@suse.de \
--cc=dsterba@suse.com \
--cc=gor@linux.ibm.com \
--cc=hare@suse.de \
--cc=hca@linux.ibm.com \
--cc=hoeppner@linux.ibm.com \
--cc=jack@suse.com \
--cc=jejb@linux.ibm.com \
--cc=joern@lazybastard.org \
--cc=josef@toxicpanda.com \
--cc=kent.overstreet@gmail.com \
--cc=konishi.ryusuke@gmail.com \
--cc=linux-bcache@vger.kernel.org \
--cc=linux-bcachefs@vger.kernel.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-erofs@lists.ozlabs.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=linux-nilfs@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=miquel.raynal@bootlin.com \
--cc=nico@fluxnic.net \
--cc=p.raghav@samsung.com \
--cc=richard@nod.at \
--cc=roger.pau@citrix.com \
--cc=sth@linux.ibm.com \
--cc=tytso@mit.edu \
--cc=vigneshr@ti.com \
--cc=viro@zeniv.linux.org.uk \
--cc=willy@infradead.org \
--cc=xen-devel@lists.xenproject.org \
--cc=xiang@kernel.org \
--cc=yangerkun@huawei.com \
--cc=yi.zhang@huawei.com \
--cc=yukuai3@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).