Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

loop: properly observe rotational flag of underlying device

The loop driver always declares the rotational flag of its device as
rotational, even when the device of the mapped file is nonrotational,
as is the case with SSDs or on tmpfs. This can confuse filesystem tools
which are SSD-aware; in my case I frequently forget to tell mkfs.btrfs
that my loop device on tmpfs is nonrotational, and that I really don't
need any automatic metadata redundancy.

The attached patch fixes this by introspecting the rotational flag of the
mapped file's underlying block device, if it exists. If the mapped file's
filesystem has no associated block device - as is the case on e.g. tmpfs -
we assume nonrotational storage. If there is a better way to identify such
non-devices I'd love to hear them.

Cc: Jens Axboe <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org
Cc: holger@applied-asynchrony.com
Signed-off-by: Holger Hoffstätte <holger.hoffstaette@googlemail.com>
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Signed-off-by: Benjamin Gordon <bmgordon@chromium.org>
Reviewed-by: Guenter Roeck <groeck@chromium.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Holger Hoffstätte and committed by
Jens Axboe
56a85fd8 4438cf50

+19
+19
drivers/block/loop.c
··· 900 900 return 0; 901 901 } 902 902 903 + static void loop_update_rotational(struct loop_device *lo) 904 + { 905 + struct file *file = lo->lo_backing_file; 906 + struct inode *file_inode = file->f_mapping->host; 907 + struct block_device *file_bdev = file_inode->i_sb->s_bdev; 908 + struct request_queue *q = lo->lo_queue; 909 + bool nonrot = true; 910 + 911 + /* not all filesystems (e.g. tmpfs) have a sb->s_bdev */ 912 + if (file_bdev) 913 + nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev)); 914 + 915 + if (nonrot) 916 + blk_queue_flag_set(QUEUE_FLAG_NONROT, q); 917 + else 918 + blk_queue_flag_clear(QUEUE_FLAG_NONROT, q); 919 + } 920 + 903 921 static int loop_set_fd(struct loop_device *lo, fmode_t mode, 904 922 struct block_device *bdev, unsigned int arg) 905 923 { ··· 981 963 if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync) 982 964 blk_queue_write_cache(lo->lo_queue, true, false); 983 965 966 + loop_update_rotational(lo); 984 967 loop_update_dio(lo); 985 968 set_capacity(lo->lo_disk, size); 986 969 bd_set_size(bdev, size << 9);