qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
	Anton Nefedov <anton.nefedov@virtuozzo.com>,
	Alberto Garcia <berto@igalia.com>,
	qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>,
	Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Subject: [Qemu-devel] [RFC 2/3] file-posix: Inquire rotational status
Date: Fri, 24 May 2019 19:28:11 +0200	[thread overview]
Message-ID: <20190524172812.27308-3-mreitz@redhat.com> (raw)
In-Reply-To: <20190524172812.27308-1-mreitz@redhat.com>

On Linux, we can inquire whether the file is stored on a rotating disk
or on a solid-state drive through the sysfs.  (The BLKROTATIONAL ioctl
only works on device files themselves.)

Signed-off-by: Max Reitz <mreitz@redhat.com>
---
 block/file-posix.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/block/file-posix.c b/block/file-posix.c
index d018429672..f84179c0dc 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -161,6 +161,9 @@ typedef struct BDRVRawState {
     bool check_cache_dropped;
 
     PRManager *pr_mgr;
+
+    bool has_rotational_info;
+    ImageRotationalInfo rotational_info;
 } BDRVRawState;
 
 typedef struct BDRVRawReopenState {
@@ -378,6 +381,68 @@ static void raw_probe_alignment(BlockDriverState *bs, int fd, Error **errp)
     }
 }
 
+/**
+ * Tries to inquire whether the file is stored on a rotating disk or a
+ * solid-state drive.
+ */
+static void raw_update_rotational_info(BDRVRawState *s)
+{
+#ifdef CONFIG_LINUX
+    struct stat st;
+    char *part_fname, *rot_fname;
+    char rot_info[3];
+    dev_t dev;
+    int rot_fd;
+    int ret;
+
+    s->has_rotational_info = false;
+
+    if (fstat(s->fd, &st) < 0) {
+        return;
+    }
+
+    if (st.st_rdev) {
+        dev = st.st_rdev;
+    } else {
+        dev = st.st_dev;
+    }
+
+    part_fname = g_strdup_printf("/sys/dev/block/%u:%u/partition",
+                                 major(dev), minor(dev));
+    if (access(part_fname, F_OK) == 0) {
+        rot_fname = g_strdup_printf("/sys/dev/block/%u:%u/../queue/rotational",
+                                    major(dev), minor(dev));
+    } else {
+        rot_fname = g_strdup_printf("/sys/dev/block/%u:%u/queue/rotational",
+                                    major(dev), minor(dev));
+    }
+    g_free(part_fname);
+
+    rot_fd = open(rot_fname, O_RDONLY);
+    g_free(rot_fname);
+    if (rot_fd < 0) {
+        return;
+    }
+
+    ret = read(rot_fd, rot_info, 2);
+    close(rot_fd);
+    if (ret < 2) {
+        return;
+    }
+
+    rot_info[2] = '\0';
+    if (!strcmp(rot_info, "0\n")) {
+        s->rotational_info = IMAGE_ROTATIONAL_INFO_SOLID_STATE;
+        s->has_rotational_info = true;
+    } else if (!strcmp(rot_info, "1\n")) {
+        s->rotational_info = IMAGE_ROTATIONAL_INFO_ROTATING;
+        s->has_rotational_info = true;
+    }
+#else /* CONFIG_LINUX */
+    s->has_rotational_info = false;
+#endif
+}
+
 static void raw_parse_flags(int bdrv_flags, int *open_flags, bool has_writers)
 {
     bool read_write = false;
@@ -652,6 +717,8 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
     }
 #endif
 
+    raw_update_rotational_info(s);
+
     bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK;
     ret = 0;
 fail:
@@ -1007,6 +1074,8 @@ static void raw_reopen_commit(BDRVReopenState *state)
     qemu_close(s->fd);
     s->fd = rs->fd;
 
+    raw_update_rotational_info(s);
+
     g_free(state->opaque);
     state->opaque = NULL;
 
@@ -2731,6 +2800,10 @@ static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
     BDRVRawState *s = bs->opaque;
 
     bdi->unallocated_blocks_are_zero = s->discard_zeroes;
+
+    bdi->rotational_info = s->rotational_info;
+    bdi->has_rotational_info = s->has_rotational_info;
+
     return 0;
 }
 
-- 
2.21.0



  parent reply	other threads:[~2019-05-24 17:33 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-24 17:28 [Qemu-devel] [RFC 0/3] block: Inquire images’ rotational info Max Reitz
2019-05-24 17:28 ` [Qemu-devel] [RFC 1/3] block: Add ImageRotationalInfo Max Reitz
2019-05-26 15:08   ` Alberto Garcia
2019-05-27 12:16     ` Max Reitz
2019-05-27 12:37       ` Alberto Garcia
2019-05-27 12:57         ` Max Reitz
2019-05-27 13:44           ` Anton Nefedov
2019-05-27 13:51             ` Max Reitz
2019-05-27 13:53             ` Alberto Garcia
2019-05-29 22:10   ` Kevin Wolf
2019-05-31 11:51     ` Max Reitz
2019-05-31 14:02       ` Kevin Wolf
2019-05-24 17:28 ` Max Reitz [this message]
2019-05-24 17:28 ` [Qemu-devel] [RFC 3/3] qcow2: Evaluate rotational info Max Reitz
2019-05-24 17:52 ` [Qemu-devel] [RFC 0/3] block: Inquire images’ " Eric Blake
2019-06-13 16:12 ` Stefan Hajnoczi
2019-06-13 16:20   ` Max Reitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190524172812.27308-3-mreitz@redhat.com \
    --to=mreitz@redhat.com \
    --cc=anton.nefedov@virtuozzo.com \
    --cc=berto@igalia.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).