Skip to content

Commit 06582bc

Browse files
Ming Leiaxboe
authored andcommitted
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 claims discard isn't supported. Cc: Christoph Hellwig <[email protected]> Cc: Vivek Goyal <[email protected]> Reported-by: Pei Zhang <[email protected]> Signed-off-by: Ming Lei <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent a12821d commit 06582bc

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

drivers/block/loop.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
#include <linux/ioprio.h>
8080
#include <linux/blk-cgroup.h>
8181
#include <linux/sched/mm.h>
82+
#include <linux/statfs.h>
8283

8384
#include "loop.h"
8485

@@ -774,8 +775,13 @@ static void loop_config_discard(struct loop_device *lo)
774775
granularity = 0;
775776

776777
} else {
778+
struct kstatfs sbuf;
779+
777780
max_discard_sectors = UINT_MAX >> 9;
778-
granularity = inode->i_sb->s_blocksize;
781+
if (!vfs_statfs(&file->f_path, &sbuf))
782+
granularity = sbuf.f_bsize;
783+
else
784+
max_discard_sectors = 0;
779785
}
780786

781787
if (max_discard_sectors) {

0 commit comments

Comments
 (0)