Commit 3817d4b1 authored by Jan Kara's avatar Jan Kara Committed by Christian Brauner

mtd: block2mtd: Convert to bdev_open_by_dev/path()

Convert block2mtd to use bdev_open_by_dev() and bdev_open_by_path() and
pass the handle around.

CC: Joern Engel <joern@lazybastard.org>
CC: linux-mtd@lists.infradead.org
Acked-by: default avatarChristoph Hellwig <hch@lst.de>
Acked-by: default avatarChristian Brauner <brauner@kernel.org>
Signed-off-by: default avatarJan Kara <jack@suse.cz>
Link: https://lore.kernel.org/r/20230927093442.25915-12-jack@suse.czSigned-off-by: default avatarChristian Brauner <brauner@kernel.org>
parent 9f0f5a30
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
/* Info for the block device */ /* Info for the block device */
struct block2mtd_dev { struct block2mtd_dev {
struct list_head list; struct list_head list;
struct block_device *blkdev; struct bdev_handle *bdev_handle;
struct mtd_info mtd; struct mtd_info mtd;
struct mutex write_mutex; struct mutex write_mutex;
}; };
...@@ -55,7 +55,8 @@ static struct page *page_read(struct address_space *mapping, pgoff_t index) ...@@ -55,7 +55,8 @@ static struct page *page_read(struct address_space *mapping, pgoff_t index)
/* erase a specified part of the device */ /* erase a specified part of the device */
static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len) static int _block2mtd_erase(struct block2mtd_dev *dev, loff_t to, size_t len)
{ {
struct address_space *mapping = dev->blkdev->bd_inode->i_mapping; struct address_space *mapping =
dev->bdev_handle->bdev->bd_inode->i_mapping;
struct page *page; struct page *page;
pgoff_t index = to >> PAGE_SHIFT; // page index pgoff_t index = to >> PAGE_SHIFT; // page index
int pages = len >> PAGE_SHIFT; int pages = len >> PAGE_SHIFT;
...@@ -105,6 +106,8 @@ static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -105,6 +106,8 @@ static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
size_t *retlen, u_char *buf) size_t *retlen, u_char *buf)
{ {
struct block2mtd_dev *dev = mtd->priv; struct block2mtd_dev *dev = mtd->priv;
struct address_space *mapping =
dev->bdev_handle->bdev->bd_inode->i_mapping;
struct page *page; struct page *page;
pgoff_t index = from >> PAGE_SHIFT; pgoff_t index = from >> PAGE_SHIFT;
int offset = from & (PAGE_SIZE-1); int offset = from & (PAGE_SIZE-1);
...@@ -117,7 +120,7 @@ static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -117,7 +120,7 @@ static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
cpylen = len; // this page cpylen = len; // this page
len = len - cpylen; len = len - cpylen;
page = page_read(dev->blkdev->bd_inode->i_mapping, index); page = page_read(mapping, index);
if (IS_ERR(page)) if (IS_ERR(page))
return PTR_ERR(page); return PTR_ERR(page);
...@@ -139,7 +142,8 @@ static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf, ...@@ -139,7 +142,8 @@ static int _block2mtd_write(struct block2mtd_dev *dev, const u_char *buf,
loff_t to, size_t len, size_t *retlen) loff_t to, size_t len, size_t *retlen)
{ {
struct page *page; struct page *page;
struct address_space *mapping = dev->blkdev->bd_inode->i_mapping; struct address_space *mapping =
dev->bdev_handle->bdev->bd_inode->i_mapping;
pgoff_t index = to >> PAGE_SHIFT; // page index pgoff_t index = to >> PAGE_SHIFT; // page index
int offset = to & ~PAGE_MASK; // page offset int offset = to & ~PAGE_MASK; // page offset
int cpylen; int cpylen;
...@@ -194,7 +198,7 @@ static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -194,7 +198,7 @@ static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
static void block2mtd_sync(struct mtd_info *mtd) static void block2mtd_sync(struct mtd_info *mtd)
{ {
struct block2mtd_dev *dev = mtd->priv; struct block2mtd_dev *dev = mtd->priv;
sync_blockdev(dev->blkdev); sync_blockdev(dev->bdev_handle->bdev);
return; return;
} }
...@@ -206,10 +210,10 @@ static void block2mtd_free_device(struct block2mtd_dev *dev) ...@@ -206,10 +210,10 @@ static void block2mtd_free_device(struct block2mtd_dev *dev)
kfree(dev->mtd.name); kfree(dev->mtd.name);
if (dev->blkdev) { if (dev->bdev_handle) {
invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, invalidate_mapping_pages(
0, -1); dev->bdev_handle->bdev->bd_inode->i_mapping, 0, -1);
blkdev_put(dev->blkdev, NULL); bdev_release(dev->bdev_handle);
} }
kfree(dev); kfree(dev);
...@@ -219,10 +223,10 @@ static void block2mtd_free_device(struct block2mtd_dev *dev) ...@@ -219,10 +223,10 @@ static void block2mtd_free_device(struct block2mtd_dev *dev)
* This function is marked __ref because it calls the __init marked * This function is marked __ref because it calls the __init marked
* early_lookup_bdev when called from the early boot code. * early_lookup_bdev when called from the early boot code.
*/ */
static struct block_device __ref *mdtblock_early_get_bdev(const char *devname, static struct bdev_handle __ref *mdtblock_early_get_bdev(const char *devname,
blk_mode_t mode, int timeout, struct block2mtd_dev *dev) blk_mode_t mode, int timeout, struct block2mtd_dev *dev)
{ {
struct block_device *bdev = ERR_PTR(-ENODEV); struct bdev_handle *bdev_handle = ERR_PTR(-ENODEV);
#ifndef MODULE #ifndef MODULE
int i; int i;
...@@ -230,7 +234,7 @@ static struct block_device __ref *mdtblock_early_get_bdev(const char *devname, ...@@ -230,7 +234,7 @@ static struct block_device __ref *mdtblock_early_get_bdev(const char *devname,
* We can't use early_lookup_bdev from a running system. * We can't use early_lookup_bdev from a running system.
*/ */
if (system_state >= SYSTEM_RUNNING) if (system_state >= SYSTEM_RUNNING)
return bdev; return bdev_handle;
/* /*
* We might not have the root device mounted at this point. * We might not have the root device mounted at this point.
...@@ -249,19 +253,20 @@ static struct block_device __ref *mdtblock_early_get_bdev(const char *devname, ...@@ -249,19 +253,20 @@ static struct block_device __ref *mdtblock_early_get_bdev(const char *devname,
wait_for_device_probe(); wait_for_device_probe();
if (!early_lookup_bdev(devname, &devt)) { if (!early_lookup_bdev(devname, &devt)) {
bdev = blkdev_get_by_dev(devt, mode, dev, NULL); bdev_handle = bdev_open_by_dev(devt, mode, dev, NULL);
if (!IS_ERR(bdev)) if (!IS_ERR(bdev_handle))
break; break;
} }
} }
#endif #endif
return bdev; return bdev_handle;
} }
static struct block2mtd_dev *add_device(char *devname, int erase_size, static struct block2mtd_dev *add_device(char *devname, int erase_size,
char *label, int timeout) char *label, int timeout)
{ {
const blk_mode_t mode = BLK_OPEN_READ | BLK_OPEN_WRITE; const blk_mode_t mode = BLK_OPEN_READ | BLK_OPEN_WRITE;
struct bdev_handle *bdev_handle;
struct block_device *bdev; struct block_device *bdev;
struct block2mtd_dev *dev; struct block2mtd_dev *dev;
char *name; char *name;
...@@ -274,21 +279,23 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size, ...@@ -274,21 +279,23 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size,
return NULL; return NULL;
/* Get a handle on the device */ /* Get a handle on the device */
bdev = blkdev_get_by_path(devname, mode, dev, NULL); bdev_handle = bdev_open_by_path(devname, mode, dev, NULL);
if (IS_ERR(bdev)) if (IS_ERR(bdev_handle))
bdev = mdtblock_early_get_bdev(devname, mode, timeout, dev); bdev_handle = mdtblock_early_get_bdev(devname, mode, timeout,
if (IS_ERR(bdev)) { dev);
if (IS_ERR(bdev_handle)) {
pr_err("error: cannot open device %s\n", devname); pr_err("error: cannot open device %s\n", devname);
goto err_free_block2mtd; goto err_free_block2mtd;
} }
dev->blkdev = bdev; dev->bdev_handle = bdev_handle;
bdev = bdev_handle->bdev;
if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) { if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
pr_err("attempting to use an MTD device as a block device\n"); pr_err("attempting to use an MTD device as a block device\n");
goto err_free_block2mtd; goto err_free_block2mtd;
} }
if ((long)dev->blkdev->bd_inode->i_size % erase_size) { if ((long)bdev->bd_inode->i_size % erase_size) {
pr_err("erasesize must be a divisor of device size\n"); pr_err("erasesize must be a divisor of device size\n");
goto err_free_block2mtd; goto err_free_block2mtd;
} }
...@@ -306,7 +313,7 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size, ...@@ -306,7 +313,7 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size,
dev->mtd.name = name; dev->mtd.name = name;
dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK; dev->mtd.size = bdev->bd_inode->i_size & PAGE_MASK;
dev->mtd.erasesize = erase_size; dev->mtd.erasesize = erase_size;
dev->mtd.writesize = 1; dev->mtd.writesize = 1;
dev->mtd.writebufsize = PAGE_SIZE; dev->mtd.writebufsize = PAGE_SIZE;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment