Skip to contentSkip to conversation

block: loop:use kstatfs.f_bsize of backing file to set discard granularity

PATCH Superseded Ming Lei 2 participants 4 messages Jan 25, 2022 Block layer

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ming Lei

author
#0 lore raw

PATCH v2 1/1 block: loop:use kstatfs.f_bsize of backing file to set discard granularity

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 <[email protected]>
Cc Vivek Goyal <[email protected]>

---
V2:
- take Christoph's suggestion to use kstatfs.f_bsize

 drivers/block/loop.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/block/loop.c +7 -1
index b1b05c45c07c..0991f08f79d7 100644
@@ -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) {
Signature · 1 line
2.31.1
#1 lore raw

drivers/block/loop.c static void loop_config_discard(struct loop_device *lo) · line 784

Quoted 9 lines · Ming Lei
drivers/block/loop.c +6 -1
 	} 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.

author
#2 lore raw
Quoted 13 lines · Christoph Hellwig

drivers/block/loop.c static void loop_config_discard(struct loop_device *lo) · line 784

Quoted 9 lines · Ming Lei
drivers/block/loop.c +6 -1
 	} 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

#3 lore raw
Quoted 6 lines · Ming Lei

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.