Skip to contentSkip to conversation

loop: properly observe rotational flag of underlying device

PATCH In review Holger Hoffstätte 4 participants 6 messages Nov 11, 2015 – May 12, 2016 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 drivers/block/loop.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
drivers/block/loop.c +19 -0
index 423f4ca..2984aca 100644
@@ -843,6 +843,24 @@ static void loop_config_discard(struct loop_device *lo)
 	queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
 }
 
+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)
+		queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
+	else
+		queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, q);
+}
+
 static void loop_unprepare_queue(struct loop_device *lo)
 {
 	flush_kthread_worker(&lo->worker);
@@ -939,6 +957,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_flush(lo->lo_queue, REQ_FLUSH);
 
+	loop_update_rotational(lo);
 	loop_update_dio(lo);
 	set_capacity(lo->lo_disk, size);
 	bd_set_size(bdev, size << 9);
Signature · 1 line
2.6.3
#1 lore raw
Quoted 34 lines · Holger Hoffstätte

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.

---
  drivers/block/loop.c | 19 +++++++++++++++++++
  1 file changed, 19 insertions(+)
drivers/block/loop.c +7 -0
index 423f4ca..2984aca 100644
@@ -843,6 +843,24 @@ static void loop_config_discard(struct loop_device *lo)
  	queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  }
 
+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;

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

Quoted 10 lines
drivers/block/loop.c +10 -0
+
+	/* 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)
+		queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
+	else
+		queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, q);
+}

Are we sure we want to change the default from rot to nonrot?

Apart from that, looks good.

Signature · 1 line
Jens Axboe
#2 lore raw
Quoted 39 lines · Jens Axboe
Quoted 38 lines · Holger Hoffstätte

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.

---
  drivers/block/loop.c | 19 +++++++++++++++++++
  1 file changed, 19 insertions(+)
drivers/block/loop.c +11 -0
index 423f4ca..2984aca 100644
@@ -843,6 +843,24 @@ static void loop_config_discard(struct loop_device *lo)
      queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  }
 
+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));
Quoted 8 lines

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

Quoted 6 lines
drivers/block/loop.c +6 -0
+
+    if (nonrot)
+        queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
+    else
+        queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, q);
+}

Are we sure we want to change the default from rot to nonrot?

Well, that's why I asked for a better way to identify tmpfs. It took me several hours to figure out that tmpfs doesn't have an s_bdev, and could not find a better way than to assume that a superblock without backing device is probably something virtual/nonrotational/nvm etc.

Alternatively I could look at sb->s_type and set nonrot for known fs types, but that seemed too ugly - not to mention conceptually weird.

Quoted 1 line

Apart from that, looks good.

phew :)

thanks,
Holger

#3 lore raw
Quoted 49 lines · Holger Hoffstätte
Quoted 39 lines · Jens Axboe
Quoted 38 lines · Holger Hoffstätte

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.

---
  drivers/block/loop.c | 19 +++++++++++++++++++
  1 file changed, 19 insertions(+)
drivers/block/loop.c +11 -0
index 423f4ca..2984aca 100644
@@ -843,6 +843,24 @@ static void loop_config_discard(struct loop_device *lo)
      queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  }
 
+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));
Quoted 8 lines

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

Quoted 6 lines
drivers/block/loop.c +6 -0
+
+    if (nonrot)
+        queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
+    else
+        queue_flag_clear_unlocked(QUEUE_FLAG_NONROT, q);
+}

Are we sure we want to change the default from rot to nonrot?

Quoted 9 lines

Well, that's why I asked for a better way to identify tmpfs. It took me several hours to figure out that tmpfs doesn't have an s_bdev, and could not find a better way than to assume that a superblock without backing device is probably something virtual/nonrotational/nvm etc.

Alternatively I could look at sb->s_type and set nonrot for known fs types, but that seemed too ugly - not to mention conceptually weird.

Quoted 1 line

Apart from that, looks good.

Jens,

I haven't seen this merged in any trees yet and was wondering if there's any chance to get this into 4.5? If there's something left to fix up please let me know.

Thanks,
Holger

#4 lore raw
Quoted 8 lines · Holger Hoffstätte <holger.hoffstaette <at> googlemail.com> writes:
Quoted 6 lines · Holger Hoffstätte
Quoted 5 lines · Jens Axboe
Quoted 4 lines · Holger Hoffstätte

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

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

which are SSD-aware; in my case I frequently forget to tell

mkfs.btrfs

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

that my loop device on tmpfs is nonrotational, and that I really

don't

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

need any automatic metadata redundancy.

The attached patch fixes this by introspecting the rotational flag of

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

mapped file's underlying block device, if it exists. If the mapped

file's

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

filesystem has no associated block device - as is the case on e.g.

tmpfs -

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

we assume nonrotational storage. If there is a better way to identify

such

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

non-devices I'd love to hear them.

Signed-off-by Holger Hoffstätte <holger.hoffstaette <at>

googlemail.com>

Quoted 5 lines

Jens,

I haven't seen this merged in any trees yet and was wondering if there's any chance to get this into 4.5? If there's something left to fix up

please

Quoted 6 lines

let me know.

Thanks,
Holger

This patch proved useful for ureadahead: when we use it on a loop device, it would use the HDD method to place the data in cache using the pack information instead of the SSD method.

#5 lore raw

[cc: linux-block]

Quoted 42 lines · gwendal grignou
Quoted 8 lines · Holger Hoffstätte <holger.hoffstaette <at> googlemail.com> writes:
Quoted 6 lines · Holger Hoffstätte
Quoted 5 lines · Jens Axboe
Quoted 4 lines · Holger Hoffstätte

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

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

which are SSD-aware; in my case I frequently forget to tell

mkfs.btrfs

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

that my loop device on tmpfs is nonrotational, and that I really

don't

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

need any automatic metadata redundancy.

The attached patch fixes this by introspecting the rotational flag of

the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

mapped file's underlying block device, if it exists. If the mapped

file's

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

filesystem has no associated block device - as is the case on e.g.

tmpfs -

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

we assume nonrotational storage. If there is a better way to identify

such

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

non-devices I'd love to hear them.

Signed-off-by Holger Hoffstätte <holger.hoffstaette <at>

googlemail.com>

Quoted 5 lines

Jens,

I haven't seen this merged in any trees yet and was wondering if there's any chance to get this into 4.5? If there's something left to fix up

please

Quoted 6 lines

let me know.

Thanks,
Holger

Quoted 5 lines

This patch proved useful for ureadahead: when we use it on a loop device, it would use the HDD method to place the data in cache using the pack information instead of the SSD method.

I had completely forgotten about this, and apparently so had Jens. ;) Thanks for the feedback, glad to hear it is useful.

Jens, any objections to merge this for 4.7? It should still apply cleanly. The original patch was at: https://lkml.org/lkml/2015/11/11/288

Thanks,
Holger