Skip to contentSkip to conversation

loop: properly observe rotational flag of underlying device

PATCH In review Benjamin Gordon 3 participants 3 messages Feb 12, 2019 – Mar 26, 2019 1 message not in the archive Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 loop: properly observe rotational flag of underlying device

From: Holger Hoffstätte <[email protected]>

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.

---
This is a resend of Holger's original patch from
https://lkml.org/lkml/2015/11/11/288 with the _unlocked functions updated. We keep running into the same problem on Chrome OS that this originally solved; any chance it can go in?

 drivers/block/loop.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/block/loop.c +19 -0
index cf5538942834..6c0fc0d49dc0 100644
@@ -900,6 +900,24 @@ static int loop_prepare_queue(struct loop_device *lo)
 	return 0;
 }
 
+static void loop_update_rotational(struct loop_device *lo)
+{
+	struct file *file = lo->lo_backing_file;
+	struct inode *file_inode = file->f_mapping->host;
+	struct block_device *file_bdev = file_inode->i_sb->s_bdev;
+	struct request_queue *q = lo->lo_queue;
+	bool nonrot = true;
+
+	/* not all filesystems (e.g. tmpfs) have a sb->s_bdev */
+	if (file_bdev)
+		nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev));
+
+	if (nonrot)
+		blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
+	else
+		blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
+}
+
 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
 		       struct block_device *bdev, unsigned int arg)
 {
@@ -963,6 +981,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
 	if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
 		blk_queue_write_cache(lo->lo_queue, true, false);
 
+	loop_update_rotational(lo);
 	loop_update_dio(lo);
 	set_capacity(lo->lo_disk, size);
 	bd_set_size(bdev, size << 9);
Signature · 1 line
2.20.1.791.gb4d0f1c61a-goog
#1 lore raw

Ping! Jens, can we please let this finally land in 5.2?

thanks,
Holger

Quoted 69 lines · Benjamin Gordon

From: Holger Hoffstätte <[email protected]>

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.

---
This is a resend of Holger's original patch from
https://lkml.org/lkml/2015/11/11/288 with the _unlocked functions updated. We keep running into the same problem on Chrome OS that this originally solved; any chance it can go in?

  drivers/block/loop.c | 19 +++++++++++++++++++
  1 file changed, 19 insertions(+)
drivers/block/loop.c +19 -0
index cf5538942834..6c0fc0d49dc0 100644
@@ -900,6 +900,24 @@ static int loop_prepare_queue(struct loop_device *lo)
  	return 0;
  }
 
+static void loop_update_rotational(struct loop_device *lo)
+{
+	struct file *file = lo->lo_backing_file;
+	struct inode *file_inode = file->f_mapping->host;
+	struct block_device *file_bdev = file_inode->i_sb->s_bdev;
+	struct request_queue *q = lo->lo_queue;
+	bool nonrot = true;
+
+	/* not all filesystems (e.g. tmpfs) have a sb->s_bdev */
+	if (file_bdev)
+		nonrot = blk_queue_nonrot(bdev_get_queue(file_bdev));
+
+	if (nonrot)
+		blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
+	else
+		blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
+}
+
  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
  		       struct block_device *bdev, unsigned int arg)
  {
@@ -963,6 +981,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
  	if (!(lo_flags & LO_FLAGS_READ_ONLY) && file->f_op->fsync)
  		blk_queue_write_cache(lo->lo_queue, true, false);
 
+	loop_update_rotational(lo);
  	loop_update_dio(lo);
  	set_capacity(lo->lo_disk, size);
  	bd_set_size(bdev, size << 9);

A message that is not in the archive (sent off-list or not yet received): CADaYBq32aM4QpHmOmevFWSkjYqWmBWdhgd7CDWh+Se6q_2byHg@mail.gmail.com

maintainer
#3 lore raw
Quoted 7 lines · Benjamin Gordon

On Tue, Mar 26, 2019 at 10:55 AM Holger Hoffstätte <[email protected] <mailto:[email protected]>> wrote:

Ping! Jens, can we please let this finally land in 5.2?

Yes, please!  I've just double-checked and this patch still applies cleanly.

I've added it for 5.2, thanks everyone.

Signature · 1 line
Jens Axboe