btrfs: Make btrfs_read_disk_super return struct btrfs_disk_super
PATCH 1/1 btrfs: Make btrfs_read_disk_super return struct btrfs_disk_super
Instead of returning both the page and the super block structure, make btrfs_read_disk_super just return a pointer to struct btrfs_disk_super. As a result the function signature is simplified. Also, read_cache_page_gfp can never return NULL so check its return value only for IS_ERR.
--- fs/btrfs/volumes.c | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-)
fs/btrfs/volumes.c +23 -23
@@ -1251,49 +1251,49 @@ void btrfs_release_disk_super(struct btrfs_super_block *super)put_page (page); } -static int btrfs_read_disk_super (struct block_device *bdev,u64 bytenr, -struct page **page, -struct btrfs_super_block **disk_super) +static struct btrfs_super_block *btrfs_read_disk_super (struct block_device *bdev, +u64 bytenr) { +struct btrfs_super_block *disk_super; +struct page *page;void *p;pgoff_t index;/* make sure our super fits in the device */ if (bytenr +PAGE_SIZE >=i_size_read (bdev->bd_inode)) -return 1 ; +return ERR_PTR (-EINVAL );/* make sure our super fits in the page */ -if (sizeof (**disk_super) >PAGE_SIZE ) -return 1 ; +if (sizeof (*disk_super) >PAGE_SIZE ) +return ERR_PTR (-EINVAL );/* make sure our super doesn't straddle pages on disk */ index = bytenr >>PAGE_SHIFT ; -if ((bytenr +sizeof (**disk_super) -1 ) >>PAGE_SHIFT != index) -return 1 ; +if ((bytenr +sizeof (*disk_super) -1 ) >>PAGE_SHIFT != index) +return ERR_PTR (-EINVAL );/* pull in the page with our super */ - *page =read_cache_page_gfp (bdev->bd_inode->i_mapping, - index,GFP_KERNEL ); + page =read_cache_page_gfp (bdev->bd_inode->i_mapping, index, +GFP_KERNEL ); -if (IS_ERR (*page)) -return 1 ; +if (IS_ERR (page)) +return ERR_CAST (page); - p =page_address (*page); + p =page_address (page);/* align our pointer to the offset of the super block */ - *disk_super = p +offset_in_page (bytenr); + disk_super = p +offset_in_page (bytenr); -if (btrfs_super_bytenr (*disk_super) != bytenr || -btrfs_super_magic (*disk_super) !=BTRFS_MAGIC ) { +if (btrfs_super_bytenr (disk_super) != bytenr || +btrfs_super_magic (disk_super) !=BTRFS_MAGIC ) {btrfs_release_disk_super (p); -return 1 ; +return ERR_PTR (-EUCLEAN ); } -if ((*disk_super)->label[0 ] && - (*disk_super)->label[BTRFS_LABEL_SIZE -1 ]) - (*disk_super)->label[BTRFS_LABEL_SIZE -1 ] ='\0' ; +if (disk_super->label[0 ] && disk_super->label[BTRFS_LABEL_SIZE -1 ]) + disk_super->label[BTRFS_LABEL_SIZE -1 ] ='\0' ; -return 0 ; +return disk_super; }int btrfs_forget_devices (const char *path)
@@ -1319,7 +1319,6 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,bool new_device_added = false;struct btrfs_device *device =NULL ;struct block_device *bdev; -struct page *page;u64 bytenr;lockdep_assert_held (&uuid_mutex);
@@ -1337,7 +1336,8 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,if (IS_ERR (bdev))return ERR_CAST (bdev); -if (btrfs_read_disk_super (bdev, bytenr, &page, &disk_super)) { + disk_super =btrfs_read_disk_super (bdev, bytenr); +if (IS_ERR (disk_super)) { device =ERR_PTR (-EINVAL ); goto error_bdev_put; }
Signature · 1 line
2.17.1
Quoted 5 lines · Nikolay Borisov
Instead of returning both the page and the super block structure, make
btrfs_read_disk_superjust return a pointer to structbtrfs_disk_super. As a result the function signature is simplified. Also,read_cache_page_gfpcan never return NULL so check its return value only forIS_ERR.
^^^^^^^^^^^^^
fs/btrfs/volumes.c void btrfs_release_disk_super(struct btrfs_super_block *super) · line 1280
Quoted 10 lines
fs/btrfs/volumes.c +4
-4/* pull in the page with our super */ - *page =read_cache_page_gfp (bdev->bd_inode->i_mapping, - index,GFP_KERNEL ); + page =read_cache_page_gfp (bdev->bd_inode->i_mapping, index, +GFP_KERNEL ); -if (IS_ERR (*page)) -return 1 ; +if (IS_ERR (page)) +return ERR_CAST (page);
There's no NULL check as mentioned in the changelog, or do you mean to make it explicit that there's no NULL check? The docs of read_cache_page_gfp say it returns ERR_PTR so I consider it clear enough. The changelog wording confuses me a bit, but otherwise the patch looks good.
Quoted 47 lines
fs/btrfs/volumes.c +10
-11- p =page_address (*page); + p =page_address (page);/* align our pointer to the offset of the super block */ - *disk_super = p +offset_in_page (bytenr); + disk_super = p +offset_in_page (bytenr); -if (btrfs_super_bytenr (*disk_super) != bytenr || -btrfs_super_magic (*disk_super) !=BTRFS_MAGIC ) { +if (btrfs_super_bytenr (disk_super) != bytenr || +btrfs_super_magic (disk_super) !=BTRFS_MAGIC ) {btrfs_release_disk_super (p); -return 1 ; +return ERR_PTR (-EUCLEAN ); } -if ((*disk_super)->label[0 ] && - (*disk_super)->label[BTRFS_LABEL_SIZE -1 ]) - (*disk_super)->label[BTRFS_LABEL_SIZE -1 ] ='\0' ; +if (disk_super->label[0 ] && disk_super->label[BTRFS_LABEL_SIZE -1 ]) + disk_super->label[BTRFS_LABEL_SIZE -1 ] ='\0' ; -return 0 ; +return disk_super; }int btrfs_forget_devices (const char *path)@@ -1319,7 +1319,6 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,bool new_device_added = false;struct btrfs_device *device =NULL ;struct block_device *bdev; -struct page *page;u64 bytenr;lockdep_assert_held (&uuid_mutex);@@ -1337,7 +1336,8 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,if (IS_ERR (bdev))return ERR_CAST (bdev); -if (btrfs_read_disk_super (bdev, bytenr, &page, &disk_super)) { + disk_super =btrfs_read_disk_super (bdev, bytenr); +if (IS_ERR (disk_super)) { device =ERR_PTR (-EINVAL ); goto error_bdev_put; }Signature · 1 line
2.17.1
Quoted 5 lines · David Sterba
There's no NULL check as mentioned in the changelog, or do you mean to make it explicit that there's no NULL check? The docs of
read_cache_page_gfpsay it returnsERR_PTRso I consider it clear enough. The changelog wording confuses me a bit, but otherwise the patch looks good.
Yes I wanted to make it explicit why I'm not checking for NULL.
fs/btrfs/volumes.c struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags, · line 1341
Quoted 8 lines · Nikolay Borisov
fs/btrfs/volumes.c +2
-1@@ -1337,7 +1336,8 @@ struct btrfs_device *btrfs_scan_one_device(const char *path, fmode_t flags,if (IS_ERR (bdev))return ERR_CAST (bdev); -if (btrfs_read_disk_super (bdev, bytenr, &page, &disk_super)) { + disk_super =btrfs_read_disk_super (bdev, bytenr); +if (IS_ERR (disk_super)) { device =ERR_PTR (-EINVAL );
This should forward the error value from btrfs_read_disk_super, like
device = disk_super;
fs/btrfs/volumes.c void btrfs_release_disk_super(struct btrfs_super_block *super) · line 1290
Quoted 7 lines · Nikolay Borisov
fs/btrfs/volumes.c +3
-3-if (btrfs_super_bytenr (*disk_super) != bytenr || -btrfs_super_magic (*disk_super) !=BTRFS_MAGIC ) { +if (btrfs_super_bytenr (disk_super) != bytenr || +btrfs_super_magic (disk_super) !=BTRFS_MAGIC ) {btrfs_release_disk_super (p); -return 1 ; +return ERR_PTR (-EUCLEAN );
One more question: what's the reason for -EUCLEAN? The condition fails if the superblock offset is wrong or there's no magic number. Clearly not a btrfs superblock, this could be returned to the scanning ioctl, or is called during mount.
At this point the decision is 'is this potentially a btrfs device?' and the lightest check we do is the offset and magic number. Only after that the checksum is verified and all the superblock items and from that point on it's all EUCLEAN, but before that it's EINVAL.
Quoted 14 lines · David Sterba
fs/btrfs/volumes.c void btrfs_release_disk_super(struct btrfs_super_block *super) · line 1290
Quoted 7 lines · Nikolay Borisov
fs/btrfs/volumes.c +3
-3-if (btrfs_super_bytenr (*disk_super) != bytenr || -btrfs_super_magic (*disk_super) !=BTRFS_MAGIC ) { +if (btrfs_super_bytenr (disk_super) != bytenr || +btrfs_super_magic (disk_super) !=BTRFS_MAGIC ) {btrfs_release_disk_super (p); -return 1 ; +return ERR_PTR (-EUCLEAN );One more question: what's the reason for
-EUCLEAN? The condition fails if the superblock offset is wrong or there's no magic number. Clearly not a btrfs superblock, this could be returned to the scanning ioctl, or is called during mount.
Quoted 5 lines
At this point the decision is 'is this potentially a btrfs device?' and the lightest check we do is the offset and magic number. Only after that the checksum is verified and all the superblock items and from that point on it's all EUCLEAN, but before that it's EINVAL.
Good point, even though EINVAL was always returned if btrfs_read_disk_super returned an error. But I will incorporate this and the previous suggested change - forward error from btrfs_read_disk_super to the caller.