* [PATCH V2] block: loop:use kstatfs.f_bsize of backing file to set discard granularity
@ 2022-01-25 4:40 Ming Lei
2022-01-25 6:10 ` Christoph Hellwig
0 siblings, 1 reply; 4+ messages in thread
From: Ming Lei @ 2022-01-25 4:40 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 fackback to PAGE_SIZE.
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>
---
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..0991f08f79d7 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
+ granularity = PAGE_SIZE;
}
if (max_discard_sectors) {
--
2.31.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH V2] block: loop:use kstatfs.f_bsize of backing file to set discard granularity
2022-01-25 4:40 [PATCH V2] block: loop:use kstatfs.f_bsize of backing file to set discard granularity Ming Lei
@ 2022-01-25 6:10 ` Christoph Hellwig
2022-01-25 9:27 ` Ming Lei
0 siblings, 1 reply; 4+ messages in thread
From: Christoph Hellwig @ 2022-01-25 6:10 UTC (permalink / raw)
To: Ming Lei
Cc: Jens Axboe, linux-block, Christoph Hellwig, Vivek Goyal, Pei Zhang
On Tue, Jan 25, 2022 at 12:40:05PM +0800, Ming Lei wrote:
> } 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
> + granularity = PAGE_SIZE;
If vfs_statfs fails we're pretty much toast and there isn't really any
point in continuing here.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH V2] block: loop:use kstatfs.f_bsize of backing file to set discard granularity
2022-01-25 6:10 ` Christoph Hellwig
@ 2022-01-25 9:27 ` Ming Lei
2022-01-25 15:40 ` Christoph Hellwig
0 siblings, 1 reply; 4+ messages in thread
From: Ming Lei @ 2022-01-25 9:27 UTC (permalink / raw)
To: Christoph Hellwig; +Cc: Jens Axboe, linux-block, Vivek Goyal, Pei Zhang
On Tue, Jan 25, 2022 at 07:10:57AM +0100, Christoph Hellwig wrote:
> On Tue, Jan 25, 2022 at 12:40:05PM +0800, Ming Lei wrote:
> > } 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
> > + granularity = PAGE_SIZE;
>
> If vfs_statfs fails we're pretty much toast and there isn't really any
> point in continuing here.
But it is configure code path, even though vfs_statfs() fails,
loop_config_discard() still need to keep discard setting consistent.
Setting granularity as PAGE_SIZE is just for making discard granularity
matched with max_discard_sectors. Or you have better/simpler handling?
Thanks,
Ming
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH V2] block: loop:use kstatfs.f_bsize of backing file to set discard granularity
2022-01-25 9:27 ` Ming Lei
@ 2022-01-25 15:40 ` Christoph Hellwig
0 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2022-01-25 15:40 UTC (permalink / raw)
To: Ming Lei
Cc: Christoph Hellwig, Jens Axboe, linux-block, Vivek Goyal, Pei Zhang
On Tue, Jan 25, 2022 at 05:27:36PM +0800, Ming Lei wrote:
>
> But it is configure code path, even though vfs_statfs() fails,
> loop_config_discard() still need to keep discard setting consistent.
>
> Setting granularity as PAGE_SIZE is just for making discard granularity
> matched with max_discard_sectors. Or you have better/simpler handling?
I'd fail the entire configuration preferably. Or at least not support
discard at all.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-01-25 15:43 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-25 4:40 [PATCH V2] block: loop:use kstatfs.f_bsize of backing file to set discard granularity Ming Lei
2022-01-25 6:10 ` Christoph Hellwig
2022-01-25 9:27 ` Ming Lei
2022-01-25 15:40 ` Christoph Hellwig
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.