linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] xfs: Fix tail rounding in xfs_alloc_file_space()
@ 2019-09-26 14:22 Max Reitz
  2019-09-26 14:37 ` Darrick J. Wong
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Max Reitz @ 2019-09-26 14:22 UTC (permalink / raw)
  To: linux-xfs; +Cc: linux-kernel, Max Reitz, Darrick J . Wong, Brian Foster

To ensure that all blocks touched by the range [offset, offset + count)
are allocated, we need to calculate the block count from the difference
of the range end (rounded up) and the range start (rounded down).

Before this patch, we just round up the byte count, which may lead to
unaligned ranges not being fully allocated:

$ touch test_file
$ block_size=$(stat -fc '%S' test_file)
$ fallocate -o $((block_size / 2)) -l $block_size test_file
$ xfs_bmap test_file
test_file:
        0: [0..7]: 1396264..1396271
        1: [8..15]: hole

There should not be a hole there.  Instead, the first two blocks should
be fully allocated.

With this patch applied, the result is something like this:

$ touch test_file
$ block_size=$(stat -fc '%S' test_file)
$ fallocate -o $((block_size / 2)) -l $block_size test_file
$ xfs_bmap test_file
test_file:
        0: [0..15]: 11024..11039

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 fs/xfs/xfs_bmap_util.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/fs/xfs/xfs_bmap_util.c b/fs/xfs/xfs_bmap_util.c
index 0910cb75b65d..4f443703065e 100644
--- a/fs/xfs/xfs_bmap_util.c
+++ b/fs/xfs/xfs_bmap_util.c
@@ -864,6 +864,7 @@ xfs_alloc_file_space(
 	xfs_filblks_t		allocatesize_fsb;
 	xfs_extlen_t		extsz, temp;
 	xfs_fileoff_t		startoffset_fsb;
+	xfs_fileoff_t		endoffset_fsb;
 	int			nimaps;
 	int			quota_flag;
 	int			rt;
@@ -891,7 +892,8 @@ xfs_alloc_file_space(
 	imapp = &imaps[0];
 	nimaps = 1;
 	startoffset_fsb	= XFS_B_TO_FSBT(mp, offset);
-	allocatesize_fsb = XFS_B_TO_FSB(mp, count);
+	endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
+	allocatesize_fsb = endoffset_fsb - startoffset_fsb;
 
 	/*
 	 * Allocate file space until done or until there is an error
-- 
2.23.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] xfs: Fix tail rounding in xfs_alloc_file_space()
  2019-09-26 14:22 [PATCH] xfs: Fix tail rounding in xfs_alloc_file_space() Max Reitz
@ 2019-09-26 14:37 ` Darrick J. Wong
  2019-09-26 14:40   ` Max Reitz
  2019-09-30  7:48 ` Christoph Hellwig
  2019-09-30 18:25 ` Darrick J. Wong
  2 siblings, 1 reply; 5+ messages in thread
From: Darrick J. Wong @ 2019-09-26 14:37 UTC (permalink / raw)
  To: Max Reitz; +Cc: linux-xfs, linux-kernel, Brian Foster

On Thu, Sep 26, 2019 at 04:22:38PM +0200, Max Reitz wrote:
> To ensure that all blocks touched by the range [offset, offset + count)
> are allocated, we need to calculate the block count from the difference
> of the range end (rounded up) and the range start (rounded down).
> 
> Before this patch, we just round up the byte count, which may lead to
> unaligned ranges not being fully allocated:
> 
> $ touch test_file
> $ block_size=$(stat -fc '%S' test_file)
> $ fallocate -o $((block_size / 2)) -l $block_size test_file
> $ xfs_bmap test_file
> test_file:
>         0: [0..7]: 1396264..1396271
>         1: [8..15]: hole
> 
> There should not be a hole there.  Instead, the first two blocks should
> be fully allocated.
> 
> With this patch applied, the result is something like this:
> 
> $ touch test_file
> $ block_size=$(stat -fc '%S' test_file)
> $ fallocate -o $((block_size / 2)) -l $block_size test_file
> $ xfs_bmap test_file
> test_file:
>         0: [0..15]: 11024..11039

Code looks ok; by any chance do you have an xfstest we could add to the
regresion test suite?

--D

> Signed-off-by: Max Reitz <mreitz@redhat.com>
> ---
>  fs/xfs/xfs_bmap_util.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/xfs/xfs_bmap_util.c b/fs/xfs/xfs_bmap_util.c
> index 0910cb75b65d..4f443703065e 100644
> --- a/fs/xfs/xfs_bmap_util.c
> +++ b/fs/xfs/xfs_bmap_util.c
> @@ -864,6 +864,7 @@ xfs_alloc_file_space(
>  	xfs_filblks_t		allocatesize_fsb;
>  	xfs_extlen_t		extsz, temp;
>  	xfs_fileoff_t		startoffset_fsb;
> +	xfs_fileoff_t		endoffset_fsb;
>  	int			nimaps;
>  	int			quota_flag;
>  	int			rt;
> @@ -891,7 +892,8 @@ xfs_alloc_file_space(
>  	imapp = &imaps[0];
>  	nimaps = 1;
>  	startoffset_fsb	= XFS_B_TO_FSBT(mp, offset);
> -	allocatesize_fsb = XFS_B_TO_FSB(mp, count);
> +	endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
> +	allocatesize_fsb = endoffset_fsb - startoffset_fsb;
>  
>  	/*
>  	 * Allocate file space until done or until there is an error
> -- 
> 2.23.0
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] xfs: Fix tail rounding in xfs_alloc_file_space()
  2019-09-26 14:37 ` Darrick J. Wong
@ 2019-09-26 14:40   ` Max Reitz
  0 siblings, 0 replies; 5+ messages in thread
From: Max Reitz @ 2019-09-26 14:40 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: linux-xfs, linux-kernel, Brian Foster


[-- Attachment #1.1: Type: text/plain, Size: 1214 bytes --]

On 26.09.19 16:37, Darrick J. Wong wrote:
> On Thu, Sep 26, 2019 at 04:22:38PM +0200, Max Reitz wrote:
>> To ensure that all blocks touched by the range [offset, offset + count)
>> are allocated, we need to calculate the block count from the difference
>> of the range end (rounded up) and the range start (rounded down).
>>
>> Before this patch, we just round up the byte count, which may lead to
>> unaligned ranges not being fully allocated:
>>
>> $ touch test_file
>> $ block_size=$(stat -fc '%S' test_file)
>> $ fallocate -o $((block_size / 2)) -l $block_size test_file
>> $ xfs_bmap test_file
>> test_file:
>>         0: [0..7]: 1396264..1396271
>>         1: [8..15]: hole
>>
>> There should not be a hole there.  Instead, the first two blocks should
>> be fully allocated.
>>
>> With this patch applied, the result is something like this:
>>
>> $ touch test_file
>> $ block_size=$(stat -fc '%S' test_file)
>> $ fallocate -o $((block_size / 2)) -l $block_size test_file
>> $ xfs_bmap test_file
>> test_file:
>>         0: [0..15]: 11024..11039
> 
> Code looks ok; by any chance do you have an xfstest we could add to the
> regresion test suite?
I’ll look into it.

Max


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] xfs: Fix tail rounding in xfs_alloc_file_space()
  2019-09-26 14:22 [PATCH] xfs: Fix tail rounding in xfs_alloc_file_space() Max Reitz
  2019-09-26 14:37 ` Darrick J. Wong
@ 2019-09-30  7:48 ` Christoph Hellwig
  2019-09-30 18:25 ` Darrick J. Wong
  2 siblings, 0 replies; 5+ messages in thread
From: Christoph Hellwig @ 2019-09-30  7:48 UTC (permalink / raw)
  To: Max Reitz; +Cc: linux-xfs, linux-kernel, Darrick J . Wong, Brian Foster

On Thu, Sep 26, 2019 at 04:22:38PM +0200, Max Reitz wrote:
> To ensure that all blocks touched by the range [offset, offset + count)
> are allocated, we need to calculate the block count from the difference
> of the range end (rounded up) and the range start (rounded down).
> 
> Before this patch, we just round up the byte count, which may lead to
> unaligned ranges not being fully allocated:

Looks fine:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] xfs: Fix tail rounding in xfs_alloc_file_space()
  2019-09-26 14:22 [PATCH] xfs: Fix tail rounding in xfs_alloc_file_space() Max Reitz
  2019-09-26 14:37 ` Darrick J. Wong
  2019-09-30  7:48 ` Christoph Hellwig
@ 2019-09-30 18:25 ` Darrick J. Wong
  2 siblings, 0 replies; 5+ messages in thread
From: Darrick J. Wong @ 2019-09-30 18:25 UTC (permalink / raw)
  To: Max Reitz; +Cc: linux-xfs, linux-kernel, Brian Foster

On Thu, Sep 26, 2019 at 04:22:38PM +0200, Max Reitz wrote:
> To ensure that all blocks touched by the range [offset, offset + count)
> are allocated, we need to calculate the block count from the difference
> of the range end (rounded up) and the range start (rounded down).
> 
> Before this patch, we just round up the byte count, which may lead to
> unaligned ranges not being fully allocated:
> 
> $ touch test_file
> $ block_size=$(stat -fc '%S' test_file)
> $ fallocate -o $((block_size / 2)) -l $block_size test_file
> $ xfs_bmap test_file
> test_file:
>         0: [0..7]: 1396264..1396271
>         1: [8..15]: hole
> 
> There should not be a hole there.  Instead, the first two blocks should
> be fully allocated.
> 
> With this patch applied, the result is something like this:
> 
> $ touch test_file
> $ block_size=$(stat -fc '%S' test_file)
> $ fallocate -o $((block_size / 2)) -l $block_size test_file
> $ xfs_bmap test_file
> test_file:
>         0: [0..15]: 11024..11039
> 
> Signed-off-by: Max Reitz <mreitz@redhat.com>

Looks ok, will test...
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>

--D

> ---
>  fs/xfs/xfs_bmap_util.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/xfs/xfs_bmap_util.c b/fs/xfs/xfs_bmap_util.c
> index 0910cb75b65d..4f443703065e 100644
> --- a/fs/xfs/xfs_bmap_util.c
> +++ b/fs/xfs/xfs_bmap_util.c
> @@ -864,6 +864,7 @@ xfs_alloc_file_space(
>  	xfs_filblks_t		allocatesize_fsb;
>  	xfs_extlen_t		extsz, temp;
>  	xfs_fileoff_t		startoffset_fsb;
> +	xfs_fileoff_t		endoffset_fsb;
>  	int			nimaps;
>  	int			quota_flag;
>  	int			rt;
> @@ -891,7 +892,8 @@ xfs_alloc_file_space(
>  	imapp = &imaps[0];
>  	nimaps = 1;
>  	startoffset_fsb	= XFS_B_TO_FSBT(mp, offset);
> -	allocatesize_fsb = XFS_B_TO_FSB(mp, count);
> +	endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
> +	allocatesize_fsb = endoffset_fsb - startoffset_fsb;
>  
>  	/*
>  	 * Allocate file space until done or until there is an error
> -- 
> 2.23.0
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-09-30 23:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-26 14:22 [PATCH] xfs: Fix tail rounding in xfs_alloc_file_space() Max Reitz
2019-09-26 14:37 ` Darrick J. Wong
2019-09-26 14:40   ` Max Reitz
2019-09-30  7:48 ` Christoph Hellwig
2019-09-30 18:25 ` Darrick J. Wong

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).