[PATCH v2 16/18] io/channel-file: Add direct-io support

Fabiano Rosas posted 18 patches 6 months ago
Maintainers: Peter Xu <peterx@redhat.com>, Fabiano Rosas <farosas@suse.de>, "Daniel P. Berrangé" <berrange@redhat.com>, "Dr. David Alan Gilbert" <dave@treblig.org>, Markus Armbruster <armbru@redhat.com>, Eric Blake <eblake@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>, Thomas Huth <thuth@redhat.com>, Laurent Vivier <lvivier@redhat.com>
There is a newer version of this series
[PATCH v2 16/18] io/channel-file: Add direct-io support
Posted by Fabiano Rosas 6 months ago
Add support for setting/clearing the O_DIRECT flag on a file
descriptor. This will be used for enabling O_DIRECT in the main
migration channel when multifd is not in use.

Signed-off-by: Fabiano Rosas <farosas@suse.de>
---
 include/io/channel-file.h |  8 ++++++++
 io/channel-file.c         | 25 +++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/include/io/channel-file.h b/include/io/channel-file.h
index d373a4e44d..ecb4450e8e 100644
--- a/include/io/channel-file.h
+++ b/include/io/channel-file.h
@@ -107,4 +107,12 @@ qio_channel_file_new_path(const char *path,
                           mode_t mode,
                           Error **errp);
 
+/**
+ * qio_channel_file_set_direct_io:
+ * @ioc: the QIOChannel object
+ * @enabled: the desired state of the O_DIRECT flag
+ * @errp: pointer to initialized error object
+ */
+void qio_channel_file_set_direct_io(QIOChannel *ioc, bool enabled,
+                                    Error **errp);
 #endif /* QIO_CHANNEL_FILE_H */
diff --git a/io/channel-file.c b/io/channel-file.c
index 2ea8d08360..a89cd3a6d5 100644
--- a/io/channel-file.c
+++ b/io/channel-file.c
@@ -231,6 +231,31 @@ static int qio_channel_file_set_blocking(QIOChannel *ioc,
 #endif
 }
 
+void qio_channel_file_set_direct_io(QIOChannel *ioc, bool enabled, Error **errp)
+{
+#ifdef O_DIRECT
+    QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
+    int flags = fcntl(fioc->fd, F_GETFL);
+
+    if (flags == -1) {
+        error_setg_errno(errp, errno, "Unable to read file descriptor flags");
+        return;
+    }
+
+    if (enabled) {
+        flags |= O_DIRECT;
+    } else {
+        flags &= ~O_DIRECT;
+    }
+
+    if (fcntl(fioc->fd, F_SETFL, flags) == -1) {
+        error_setg_errno(errp, errno, "Unable to set file descriptor flags");
+        return;
+    }
+#else
+    error_setg(errp, "System does not support O_DIRECT");
+#endif
+}
 
 static off_t qio_channel_file_seek(QIOChannel *ioc,
                                    off_t offset,
-- 
2.35.3
Re: [PATCH v2 16/18] io/channel-file: Add direct-io support
Posted by Daniel P. Berrangé 5 months, 3 weeks ago
On Thu, May 23, 2024 at 04:05:46PM -0300, Fabiano Rosas wrote:
> Add support for setting/clearing the O_DIRECT flag on a file
> descriptor. This will be used for enabling O_DIRECT in the main
> migration channel when multifd is not in use.
> 
> Signed-off-by: Fabiano Rosas <farosas@suse.de>
> ---
>  include/io/channel-file.h |  8 ++++++++
>  io/channel-file.c         | 25 +++++++++++++++++++++++++
>  2 files changed, 33 insertions(+)
> 
> diff --git a/include/io/channel-file.h b/include/io/channel-file.h
> index d373a4e44d..ecb4450e8e 100644
> --- a/include/io/channel-file.h
> +++ b/include/io/channel-file.h
> @@ -107,4 +107,12 @@ qio_channel_file_new_path(const char *path,
>                            mode_t mode,
>                            Error **errp);
>  
> +/**
> + * qio_channel_file_set_direct_io:
> + * @ioc: the QIOChannel object
> + * @enabled: the desired state of the O_DIRECT flag
> + * @errp: pointer to initialized error object
> + */
> +void qio_channel_file_set_direct_io(QIOChannel *ioc, bool enabled,
> +                                    Error **errp);

This should return 'int' rather than void, giving -1 on error,
0 on success, so callers don't deference the errp parameter
to check status.

>  #endif /* QIO_CHANNEL_FILE_H */
> diff --git a/io/channel-file.c b/io/channel-file.c
> index 2ea8d08360..a89cd3a6d5 100644
> --- a/io/channel-file.c
> +++ b/io/channel-file.c
> @@ -231,6 +231,31 @@ static int qio_channel_file_set_blocking(QIOChannel *ioc,
>  #endif
>  }
>  
> +void qio_channel_file_set_direct_io(QIOChannel *ioc, bool enabled, Error **errp)
> +{
> +#ifdef O_DIRECT
> +    QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
> +    int flags = fcntl(fioc->fd, F_GETFL);
> +
> +    if (flags == -1) {
> +        error_setg_errno(errp, errno, "Unable to read file descriptor flags");
> +        return;
> +    }
> +
> +    if (enabled) {
> +        flags |= O_DIRECT;
> +    } else {
> +        flags &= ~O_DIRECT;
> +    }
> +
> +    if (fcntl(fioc->fd, F_SETFL, flags) == -1) {
> +        error_setg_errno(errp, errno, "Unable to set file descriptor flags");
> +        return;
> +    }
> +#else
> +    error_setg(errp, "System does not support O_DIRECT");
> +#endif
> +}
>  
>  static off_t qio_channel_file_seek(QIOChannel *ioc,
>                                     off_t offset,
> -- 
> 2.35.3
> 

With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|