diff mbox

[Ocfs2-devel,1/3] ext3/ext4: Factor out disk addressability check

Message ID 20100816191319.GC5786@mail.oracle.com
State Accepted, archived
Headers show

Commit Message

Joel Becker Aug. 16, 2010, 7:13 p.m. UTC
On Mon, Aug 16, 2010 at 09:44:35AM -0500, Eric Sandeen wrote:
> Joel Becker wrote:
> > How about:
> > 
> > 	u64 last_fs_page = last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits);
> > 
> > 	... ||
> > 	(last_fs_page > (pgoff_t)(~0ULL))) {
> > 
> > Is that more readable?
> 
> To me, yes.  Maybe do similar for last_fs_sector.

	last_fs_sector would be shifting up, which could wrap a really
large last_fs_blocks.  So I'm going to keep the sector_t check as-is.
How's this:

>From 8de5cb9164cdc179ba84a07b282a895d0eb794b0 Mon Sep 17 00:00:00 2001
>From: Joel Becker <joel.becker@oracle.com>
Date: Mon, 16 Aug 2010 12:10:17 -0700
Subject: [PATCH] libfs: Fix shift bug in generic_check_addressable()

generic_check_addressable() erroneously shifts pages down by a block
factor when it should be shifting up.  To prevent overflow, we shift
blocks down to pages.

Signed-off-by: Joel Becker <joel.becker@oracle.com>
---
 fs/libfs.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

Comments

Jan Kara Aug. 16, 2010, 7:21 p.m. UTC | #1
On Mon 16-08-10 12:13:19, Joel Becker wrote:
> On Mon, Aug 16, 2010 at 09:44:35AM -0500, Eric Sandeen wrote:
> > Joel Becker wrote:
> > > How about:
> > > 
> > > 	u64 last_fs_page = last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits);
> > > 
> > > 	... ||
> > > 	(last_fs_page > (pgoff_t)(~0ULL))) {
> > > 
> > > Is that more readable?
> > 
> > To me, yes.  Maybe do similar for last_fs_sector.
> 
> 	last_fs_sector would be shifting up, which could wrap a really
> large last_fs_blocks.  So I'm going to keep the sector_t check as-is.
> How's this:
> 
> >From 8de5cb9164cdc179ba84a07b282a895d0eb794b0 Mon Sep 17 00:00:00 2001
> >From: Joel Becker <joel.becker@oracle.com>
> Date: Mon, 16 Aug 2010 12:10:17 -0700
> Subject: [PATCH] libfs: Fix shift bug in generic_check_addressable()
> 
> generic_check_addressable() erroneously shifts pages down by a block
> factor when it should be shifting up.  To prevent overflow, we shift
> blocks down to pages.
> 
> Signed-off-by: Joel Becker <joel.becker@oracle.com>
  Looks good.
Reviewed-by: Jan Kara <jack@suse.cz>

								Honza

> ---
>  fs/libfs.c |    8 ++++----
>  1 files changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/libfs.c b/fs/libfs.c
> index 8debe7b..62baa03 100644
> --- a/fs/libfs.c
> +++ b/fs/libfs.c
> @@ -925,6 +925,8 @@ EXPORT_SYMBOL(generic_file_fsync);
>  int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
>  {
>  	u64 last_fs_block = num_blocks - 1;
> +	u64 last_fs_page =
> +		last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits);
>  
>  	if (unlikely(num_blocks == 0))
>  		return 0;
> @@ -932,10 +934,8 @@ int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
>  	if ((blocksize_bits < 9) || (blocksize_bits > PAGE_CACHE_SHIFT))
>  		return -EINVAL;
>  
> -	if ((last_fs_block >
> -	     (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
> -	    (last_fs_block >
> -	     (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - blocksize_bits))) {
> +	if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
> +	    (last_fs_page > (pgoff_t)(~0ULL))) {
>  		return -EFBIG;
>  	}
>  	return 0;
> -- 
> 1.7.1
> 
> -- 
> 
> Life's Little Instruction Book #267
> 
> 	"Lie on your back and look at the stars."
> 
> Joel Becker
> Consulting Software Developer
> Oracle
> E-mail: joel.becker@oracle.com
> Phone: (650) 506-8127
Joel Becker Aug. 16, 2010, 8:45 p.m. UTC | #2
On Mon, Aug 16, 2010 at 09:21:00PM +0200, Jan Kara wrote:
> On Mon 16-08-10 12:13:19, Joel Becker wrote:
> > On Mon, Aug 16, 2010 at 09:44:35AM -0500, Eric Sandeen wrote:
> > > Joel Becker wrote:
> > > > How about:
> > > > 
> > > > 	u64 last_fs_page = last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits);
> > > > 
> > > > 	... ||
> > > > 	(last_fs_page > (pgoff_t)(~0ULL))) {
> > > > 
> > > > Is that more readable?
> > > 
> > > To me, yes.  Maybe do similar for last_fs_sector.
> > 
> > 	last_fs_sector would be shifting up, which could wrap a really
> > large last_fs_blocks.  So I'm going to keep the sector_t check as-is.
> > How's this:
> > 
> > >From 8de5cb9164cdc179ba84a07b282a895d0eb794b0 Mon Sep 17 00:00:00 2001
> > >From: Joel Becker <joel.becker@oracle.com>
> > Date: Mon, 16 Aug 2010 12:10:17 -0700
> > Subject: [PATCH] libfs: Fix shift bug in generic_check_addressable()
> > 
> > generic_check_addressable() erroneously shifts pages down by a block
> > factor when it should be shifting up.  To prevent overflow, we shift
> > blocks down to pages.
> > 
> > Signed-off-by: Joel Becker <joel.becker@oracle.com>
>   Looks good.
> Reviewed-by: Jan Kara <jack@suse.cz>

	Ok, I'm going to keep this atop my existing branch, and if it
all shakes out in linux-next for a few days, send it along.

Joel
diff mbox

Patch

diff --git a/fs/libfs.c b/fs/libfs.c
index 8debe7b..62baa03 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -925,6 +925,8 @@  EXPORT_SYMBOL(generic_file_fsync);
 int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
 {
 	u64 last_fs_block = num_blocks - 1;
+	u64 last_fs_page =
+		last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits);
 
 	if (unlikely(num_blocks == 0))
 		return 0;
@@ -932,10 +934,8 @@  int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
 	if ((blocksize_bits < 9) || (blocksize_bits > PAGE_CACHE_SHIFT))
 		return -EINVAL;
 
-	if ((last_fs_block >
-	     (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
-	    (last_fs_block >
-	     (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - blocksize_bits))) {
+	if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
+	    (last_fs_page > (pgoff_t)(~0ULL))) {
 		return -EFBIG;
 	}
 	return 0;