public inbox for linux-block@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH V3] block: loop:use kstatfs.f_bsize of backing file to set discard granularity
@ 2022-01-26  3:58 Ming Lei
  2022-01-26  5:15 ` Christoph Hellwig
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ming Lei @ 2022-01-26  3:58 UTC (permalink / raw)
  To: Jens Axboe
  Cc: linux-block, Ming Lei, Christoph Hellwig, Vivek Goyal, Pei Zhang

If backing file's filesystem has implemented ->fallocate(), we think the
loop device can support discard, then pass sb->s_blocksize as
discard_granularity. However, some underlying FS, such as overlayfs,
doesn't set sb->s_blocksize, and causes discard_granularity to be set as
zero, then the warning in __blkdev_issue_discard() is triggered.

Christoph suggested to pass kstatfs.f_bsize as discard granularity, and
this way is fine because kstatfs.f_bsize means 'Optimal transfer block
size', which still matches with definition of discard granularity.

So fix the issue by setting discard_granularity as kstatfs.f_bsize if it
is available, otherwise claims discard isn't supported.

Cc: Christoph Hellwig <hch@lst.de>
Cc: Vivek Goyal <vgoyal@redhat.com>
Reported-by: Pei Zhang <pezhang@redhat.com>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
V3:
	- following Christoph's suggestion to not claim discard support if
	vfs_statfs() fails 
V2:
	- take Christoph's suggestion to use kstatfs.f_bsize

 drivers/block/loop.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/block/loop.c b/drivers/block/loop.c
index b1b05c45c07c..8b56eeb7e144 100644
--- a/drivers/block/loop.c
+++ b/drivers/block/loop.c
@@ -79,6 +79,7 @@
 #include <linux/ioprio.h>
 #include <linux/blk-cgroup.h>
 #include <linux/sched/mm.h>
+#include <linux/statfs.h>
 
 #include "loop.h"
 
@@ -774,8 +775,13 @@ static void loop_config_discard(struct loop_device *lo)
 		granularity = 0;
 
 	} else {
+		struct kstatfs sbuf;
+
 		max_discard_sectors = UINT_MAX >> 9;
-		granularity = inode->i_sb->s_blocksize;
+		if (!vfs_statfs(&file->f_path, &sbuf))
+			granularity = sbuf.f_bsize;
+		else
+			max_discard_sectors = 0;
 	}
 
 	if (max_discard_sectors) {
-- 
2.31.1


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

* Re: [PATCH V3] block: loop:use kstatfs.f_bsize of backing file to set discard granularity
  2022-01-26  3:58 [PATCH V3] block: loop:use kstatfs.f_bsize of backing file to set discard granularity Ming Lei
@ 2022-01-26  5:15 ` Christoph Hellwig
  2022-02-08  3:22 ` Ming Lei
  2022-02-11 22:11 ` Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2022-01-26  5:15 UTC (permalink / raw)
  To: Ming Lei; +Cc: Jens Axboe, linux-block, Christoph Hellwig, Vivek Goyal,
	Pei Zhang

Looks good,

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

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

* Re: [PATCH V3] block: loop:use kstatfs.f_bsize of backing file to set discard granularity
  2022-01-26  3:58 [PATCH V3] block: loop:use kstatfs.f_bsize of backing file to set discard granularity Ming Lei
  2022-01-26  5:15 ` Christoph Hellwig
@ 2022-02-08  3:22 ` Ming Lei
  2022-02-11 22:11 ` Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Ming Lei @ 2022-02-08  3:22 UTC (permalink / raw)
  To: Jens Axboe; +Cc: linux-block, Christoph Hellwig, Vivek Goyal, Pei Zhang

On Wed, Jan 26, 2022 at 11:58:30AM +0800, Ming Lei wrote:
> If backing file's filesystem has implemented ->fallocate(), we think the
> loop device can support discard, then pass sb->s_blocksize as
> discard_granularity. However, some underlying FS, such as overlayfs,
> doesn't set sb->s_blocksize, and causes discard_granularity to be set as
> zero, then the warning in __blkdev_issue_discard() is triggered.
> 
> Christoph suggested to pass kstatfs.f_bsize as discard granularity, and
> this way is fine because kstatfs.f_bsize means 'Optimal transfer block
> size', which still matches with definition of discard granularity.
> 
> So fix the issue by setting discard_granularity as kstatfs.f_bsize if it
> is available, otherwise claims discard isn't supported.
> 
> Cc: Christoph Hellwig <hch@lst.de>
> Cc: Vivek Goyal <vgoyal@redhat.com>
> Reported-by: Pei Zhang <pezhang@redhat.com>
> Signed-off-by: Ming Lei <ming.lei@redhat.com>
> ---
> V3:
> 	- following Christoph's suggestion to not claim discard support if
> 	vfs_statfs() fails 

Hi Jens,

Any chance to merge it to v5.17?


Thanks,
Ming


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

* Re: [PATCH V3] block: loop:use kstatfs.f_bsize of backing file to set discard granularity
  2022-01-26  3:58 [PATCH V3] block: loop:use kstatfs.f_bsize of backing file to set discard granularity Ming Lei
  2022-01-26  5:15 ` Christoph Hellwig
  2022-02-08  3:22 ` Ming Lei
@ 2022-02-11 22:11 ` Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Jens Axboe @ 2022-02-11 22:11 UTC (permalink / raw)
  To: Ming Lei; +Cc: Vivek Goyal, linux-block, Christoph Hellwig, Pei Zhang

On Wed, 26 Jan 2022 11:58:30 +0800, Ming Lei wrote:
> If backing file's filesystem has implemented ->fallocate(), we think the
> loop device can support discard, then pass sb->s_blocksize as
> discard_granularity. However, some underlying FS, such as overlayfs,
> doesn't set sb->s_blocksize, and causes discard_granularity to be set as
> zero, then the warning in __blkdev_issue_discard() is triggered.
> 
> Christoph suggested to pass kstatfs.f_bsize as discard granularity, and
> this way is fine because kstatfs.f_bsize means 'Optimal transfer block
> size', which still matches with definition of discard granularity.
> 
> [...]

Applied, thanks!

[1/1] block: loop:use kstatfs.f_bsize of backing file to set discard granularity
      (no commit info)

Best regards,
-- 
Jens Axboe



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

end of thread, other threads:[~2022-02-11 22:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-01-26  3:58 [PATCH V3] block: loop:use kstatfs.f_bsize of backing file to set discard granularity Ming Lei
2022-01-26  5:15 ` Christoph Hellwig
2022-02-08  3:22 ` Ming Lei
2022-02-11 22:11 ` Jens Axboe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox