Skip to contentSkip to conversation

btrfs: code optimize use btrfs_get_bdev_and_sb() at btrfs_scan_one_device

RFC PATCH In review Anand Jain 4 participants 6 messages Jul 7, 2014 – Jul 23, 2014 Btrfs file system

lore
Share & tools
lore

Anand Jain

author
#0 lore raw

RFC PATCH 1/1 btrfs: code optimize use btrfs_get_bdev_and_sb() at btrfs_scan_one_device

(for review comments pls).

btrfs_scan_one_device() needs SB, instead of doing it from scratch could use btrfs_get_bdev_and_sb()

---
 fs/btrfs/volumes.c | 51 ++++++---------------------------------------------
 1 file changed, 6 insertions(+), 45 deletions(-)
fs/btrfs/volumes.c +6 -45
index c166355..94e6131 100644
@@ -1053,14 +1053,11 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 {
 	struct btrfs_super_block *disk_super;
 	struct block_device *bdev;
-	struct page *page;
-	void *p;
 	int ret = -EINVAL;
 	u64 devid;
 	u64 transid;
 	u64 total_devices;
-	u64 bytenr;
-	pgoff_t index;
+	struct buffer_head *bh;
 
 	/*
 	 * we would like to check all the supers, but that would make
@@ -1068,44 +1065,12 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 	 * So, we need to add a special mount option to scan for
 	 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
 	 */
-	bytenr = btrfs_sb_offset(0);
 	mutex_lock(&uuid_mutex);
 
-	bdev = blkdev_get_by_path(path, flags, holder);
-
-	if (IS_ERR(bdev)) {
-		ret = PTR_ERR(bdev);
+	ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
+	if (ret)
 		goto error;
-	}
-
-	/* make sure our super fits in the device */
-	if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
-		goto error_bdev_put;
-
-	/* make sure our super fits in the page */
-	if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
-		goto error_bdev_put;
-
-	/* make sure our super doesn't straddle pages on disk */
-	index = bytenr >> PAGE_CACHE_SHIFT;
-	if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
-		goto error_bdev_put;
-
-	/* pull in the page with our super */
-	page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
-				   index, GFP_NOFS);
-
-	if (IS_ERR_OR_NULL(page))
-		goto error_bdev_put;
-
-	p = kmap(page);
-
-	/* align our pointer to the offset of the super block */
-	disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
-
-	if (btrfs_super_bytenr(disk_super) != bytenr ||
-	    btrfs_super_magic(disk_super) != BTRFS_MAGIC)
-		goto error_unmap;
+	disk_super = (struct btrfs_super_block *) bh->b_data;
 
 	devid = btrfs_stack_device_id(&disk_super->dev_item);
 	transid = btrfs_super_generation(disk_super);
@@ -1125,13 +1090,9 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 		printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
 	}
 
-
-error_unmap:
-	kunmap(page);
-	page_cache_release(page);
-
-error_bdev_put:
+	brelse(bh);
 	blkdev_put(bdev, flags);
+
 error:
 	mutex_unlock(&uuid_mutex);
 	return ret;
Signature · 1 line
2.0.0.257.g75cc6c6
#1 lore raw
Quoted 39 lines · Anand Jain

(for review comments pls).

btrfs_scan_one_device() needs SB, instead of doing it from scratch could use btrfs_get_bdev_and_sb()

---
 fs/btrfs/volumes.c | 51 ++++++---------------------------------------------
 1 file changed, 6 insertions(+), 45 deletions(-)
fs/btrfs/volumes.c +1 -7
index c166355..94e6131 100644
@@ -1053,14 +1053,11 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 {
 	struct btrfs_super_block *disk_super;
 	struct block_device *bdev;
-	struct page *page;
-	void *p;
 	int ret = -EINVAL;
 	u64 devid;
 	u64 transid;
 	u64 total_devices;
-	u64 bytenr;
-	pgoff_t index;
+	struct buffer_head *bh;
 
 	/*
 	 * we would like to check all the supers, but that would make
@@ -1068,44 +1065,12 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 	 * So, we need to add a special mount option to scan for
 	 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
 	 */
-	bytenr = btrfs_sb_offset(0);
 	mutex_lock(&uuid_mutex);
 
-	bdev = blkdev_get_by_path(path, flags, holder);
-

fs/btrfs/volumes.c int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder, · line 1083

Quoted 10 lines
fs/btrfs/volumes.c +2 -7
-	if (IS_ERR(bdev)) {
-		ret = PTR_ERR(bdev);
+	ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
+	if (ret)
 		goto error;
-	}
-
-	/* make sure our super fits in the device */
-	if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
-		goto error_bdev_put;

I think moving this check into btrfs_get_bdev_and_sb is better. The other is OK.

Thanks
Miao

Quoted 46 lines
fs/btrfs/volumes.c +3 -31
-
-	/* make sure our super fits in the page */
-	if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
-		goto error_bdev_put;
-
-	/* make sure our super doesn't straddle pages on disk */
-	index = bytenr >> PAGE_CACHE_SHIFT;
-	if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
-		goto error_bdev_put;
-
-	/* pull in the page with our super */
-	page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
-				   index, GFP_NOFS);
-
-	if (IS_ERR_OR_NULL(page))
-		goto error_bdev_put;
-
-	p = kmap(page);
-
-	/* align our pointer to the offset of the super block */
-	disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
-
-	if (btrfs_super_bytenr(disk_super) != bytenr ||
-	    btrfs_super_magic(disk_super) != BTRFS_MAGIC)
-		goto error_unmap;
+	disk_super = (struct btrfs_super_block *) bh->b_data;
 
 	devid = btrfs_stack_device_id(&disk_super->dev_item);
 	transid = btrfs_super_generation(disk_super);
@@ -1125,13 +1090,9 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 		printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
 	}
 
-
-error_unmap:
-	kunmap(page);
-	page_cache_release(page);
-
-error_bdev_put:
+	brelse(bh);
 	blkdev_put(bdev, flags);
+
 error:
 	mutex_unlock(&uuid_mutex);
 	return ret;
#2 lore raw
In reply to Anand Jain
Quoted 48 lines · Anand Jain

(for review comments pls).

btrfs_scan_one_device() needs SB, instead of doing it from scratch could use btrfs_get_bdev_and_sb()

---
 fs/btrfs/volumes.c | 51 ++++++---------------------------------------------
 1 file changed, 6 insertions(+), 45 deletions(-)
fs/btrfs/volumes.c +3 -13
index c166355..94e6131 100644
@@ -1053,14 +1053,11 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 {
 	struct btrfs_super_block *disk_super;
 	struct block_device *bdev;
-	struct page *page;
-	void *p;
 	int ret = -EINVAL;
 	u64 devid;
 	u64 transid;
 	u64 total_devices;
-	u64 bytenr;
-	pgoff_t index;
+	struct buffer_head *bh;
 
 	/*
 	 * we would like to check all the supers, but that would make
@@ -1068,44 +1065,12 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 	 * So, we need to add a special mount option to scan for
 	 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
 	 */
-	bytenr = btrfs_sb_offset(0);
 	mutex_lock(&uuid_mutex);
 
-	bdev = blkdev_get_by_path(path, flags, holder);
-
-	if (IS_ERR(bdev)) {
-		ret = PTR_ERR(bdev);
+	ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
+	if (ret)
 		goto error;
-	}
-
-	/* make sure our super fits in the device */
-	if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))

fs/btrfs/volumes.c int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder, · line 1092

Quoted 10 lines
fs/btrfs/volumes.c +0 -10
-		goto error_bdev_put;
-
-	/* make sure our super fits in the page */
-	if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
-		goto error_bdev_put;
-
-	/* make sure our super doesn't straddle pages on disk */
-	index = bytenr >> PAGE_CACHE_SHIFT;
-	if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
-		goto error_bdev_put;

Apparently btrfs_get_bdev_and_sb() lacks the above two checks, otherwise looks good.

thanks,
-liubo

Quoted 43 lines
fs/btrfs/volumes.c +3 -22
-
-	/* pull in the page with our super */
-	page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
-				   index, GFP_NOFS);
-
-	if (IS_ERR_OR_NULL(page))
-		goto error_bdev_put;
-
-	p = kmap(page);
-
-	/* align our pointer to the offset of the super block */
-	disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
-
-	if (btrfs_super_bytenr(disk_super) != bytenr ||
-	    btrfs_super_magic(disk_super) != BTRFS_MAGIC)
-		goto error_unmap;
+	disk_super = (struct btrfs_super_block *) bh->b_data;
 
 	devid = btrfs_stack_device_id(&disk_super->dev_item);
 	transid = btrfs_super_generation(disk_super);
@@ -1125,13 +1090,9 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 		printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
 	}
 
-
-error_unmap:
-	kunmap(page);
-	page_cache_release(page);
-
-error_bdev_put:
+	brelse(bh);
 	blkdev_put(bdev, flags);
+
 error:
 	mutex_unlock(&uuid_mutex);
 	return ret;
Signature · 6 lines
2.0.0.257.g75cc6c6

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw
Quoted 53 lines · Liu Bo
Quoted 52 lines · Anand Jain

(for review comments pls).

btrfs_scan_one_device() needs SB, instead of doing it from scratch could use btrfs_get_bdev_and_sb()

---
 fs/btrfs/volumes.c | 51 ++++++---------------------------------------------
 1 file changed, 6 insertions(+), 45 deletions(-)
fs/btrfs/volumes.c +3 -17
index c166355..94e6131 100644
@@ -1053,14 +1053,11 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 {
 	struct btrfs_super_block *disk_super;
 	struct block_device *bdev;
-	struct page *page;
-	void *p;
 	int ret = -EINVAL;
 	u64 devid;
 	u64 transid;
 	u64 total_devices;
-	u64 bytenr;
-	pgoff_t index;
+	struct buffer_head *bh;
 
 	/*
 	 * we would like to check all the supers, but that would make
@@ -1068,44 +1065,12 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 	 * So, we need to add a special mount option to scan for
 	 * later supers, using BTRFS_SUPER_MIRROR_MAX instead
 	 */
-	bytenr = btrfs_sb_offset(0);
 	mutex_lock(&uuid_mutex);
 
-	bdev = blkdev_get_by_path(path, flags, holder);
-
-	if (IS_ERR(bdev)) {
-		ret = PTR_ERR(bdev);
+	ret = btrfs_get_bdev_and_sb(path, flags, holder, 0, &bdev, &bh);
+	if (ret)
 		goto error;
-	}
-
-	/* make sure our super fits in the device */
-	if (bytenr + PAGE_CACHE_SIZE >= i_size_read(bdev->bd_inode))
-		goto error_bdev_put;
-
-	/* make sure our super fits in the page */
-	if (sizeof(*disk_super) > PAGE_CACHE_SIZE)
Quoted 8 lines

fs/btrfs/volumes.c int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder, · line 1092

Quoted 6 lines
fs/btrfs/volumes.c +0 -6
-		goto error_bdev_put;
-
-	/* make sure our super doesn't straddle pages on disk */
-	index = bytenr >> PAGE_CACHE_SHIFT;
-	if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_CACHE_SHIFT != index)
-		goto error_bdev_put;

Apparently btrfs_get_bdev_and_sb() lacks the above two checks, otherwise looks good.

In fact, our disk_super size is constant and <= min page size (4K), and we are sure that it is impossible that the super block is cross the block, so the above two checks are unnecessary.

Thanks
Miao

Quoted 52 lines

thanks,
-liubo

Quoted 43 lines
fs/btrfs/volumes.c +3 -22
-
-	/* pull in the page with our super */
-	page = read_cache_page_gfp(bdev->bd_inode->i_mapping,
-				   index, GFP_NOFS);
-
-	if (IS_ERR_OR_NULL(page))
-		goto error_bdev_put;
-
-	p = kmap(page);
-
-	/* align our pointer to the offset of the super block */
-	disk_super = p + (bytenr & ~PAGE_CACHE_MASK);
-
-	if (btrfs_super_bytenr(disk_super) != bytenr ||
-	    btrfs_super_magic(disk_super) != BTRFS_MAGIC)
-		goto error_unmap;
+	disk_super = (struct btrfs_super_block *) bh->b_data;
 
 	devid = btrfs_stack_device_id(&disk_super->dev_item);
 	transid = btrfs_super_generation(disk_super);
@@ -1125,13 +1090,9 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
 		printk(KERN_CONT "devid %llu transid %llu %s\n", devid, transid, path);
 	}
 
-
-error_unmap:
-	kunmap(page);
-	page_cache_release(page);
-
-error_bdev_put:
+	brelse(bh);
 	blkdev_put(bdev, flags);
+
 error:
 	mutex_unlock(&uuid_mutex);
 	return ret;
Signature · 6 lines
2.0.0.257.g75cc6c6

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
In reply to Anand Jain
Quoted 4 lines · Anand Jain

(for review comments pls).

btrfs_scan_one_device() needs SB, instead of doing it from scratch could use btrfs_get_bdev_and_sb()

Please see
http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=6f60cbd3ae442cb35861bb522f388db123d42ec1

your patch actually reverts the changes. btrfs_get_bdev_and_sb does not work and it was a pretty nasty bug, we've spent a long afternoon catching it.

Some of the check may seem unnecessary, but they 'make sure' that something unexpected will not happen.

author
#5 lore raw
Quoted 6 lines · David Sterba
Quoted 4 lines · Anand Jain

(for review comments pls).

btrfs_scan_one_device() needs SB, instead of doing it from scratch could use btrfs_get_bdev_and_sb()

Quoted 6 lines

Please see
http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=6f60cbd3ae442cb35861bb522f388db123d42ec1

your patch actually reverts the changes. btrfs_get_bdev_and_sb does not work and it was a pretty nasty bug, we've spent a long afternoon catching it.

Ah! It explains why we had a different btrfs_read_dev_super().

Thanks.
Anand