From 4d2f77074802ac8b2d05f7cdfecef9bebfba2647 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 27 Aug 2012 16:07:39 -0400 Subject: [PATCH] core: Use O_NOATIME to open metadata We really don't need atime for metadata, it's just a speed hit. --- src/libostree/ostree-repo.c | 8 ++------ src/libotutil/ot-unix-utils.c | 30 ++++++++++++++++++++++++++++++ src/libotutil/ot-unix-utils.h | 2 ++ src/libotutil/ot-variant-utils.c | 7 ++++++- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/libostree/ostree-repo.c b/src/libostree/ostree-repo.c index ab41e30d..1503dd7c 100644 --- a/src/libostree/ostree-repo.c +++ b/src/libostree/ostree-repo.c @@ -812,12 +812,8 @@ ensure_file_data_synced (GFile *file, gboolean ret = FALSE; int fd = -1; - fd = g_open (ot_gfile_get_path_cached (file), O_RDONLY | O_NOATIME | O_CLOEXEC | O_LARGEFILE, 0); - if (fd < 0) - { - ot_util_set_error_from_errno (error, errno); - goto out; - } + if (!ot_unix_open_noatime (ot_gfile_get_path_cached (file), &fd, error)) + goto out; if (!ot_unix_fdatasync (fd, error)) goto out; diff --git a/src/libotutil/ot-unix-utils.c b/src/libotutil/ot-unix-utils.c index 600082d6..3b0645f9 100644 --- a/src/libotutil/ot-unix-utils.c +++ b/src/libotutil/ot-unix-utils.c @@ -22,13 +22,17 @@ #include "config.h" +#define _GNU_SOURCE + #include "otutil.h" #include +#include #include #include #include +#include #include #include #include @@ -225,3 +229,29 @@ ot_unix_close (int fd, GError **error) } return TRUE; } + +/** + * ot_unix_open_noatime: + * + * Open a file for reading, using O_NOATIME if possible. + */ +gboolean +ot_unix_open_noatime (const char *path, + int *out_fd, + GError **error) +{ + int fd; + +#ifdef O_NOATIME + fd = g_open (path, O_RDONLY | O_NOATIME | O_CLOEXEC, 0); + if (fd == -1 && errno == EPERM) +#endif + fd = g_open (path, O_RDONLY | O_CLOEXEC, 0); + if (fd == -1) + { + ot_util_set_error_from_errno (error, errno); + return FALSE; + } + *out_fd = fd; + return TRUE; +} diff --git a/src/libotutil/ot-unix-utils.h b/src/libotutil/ot-unix-utils.h index 2022dea8..18f07494 100644 --- a/src/libotutil/ot-unix-utils.h +++ b/src/libotutil/ot-unix-utils.h @@ -59,6 +59,8 @@ gboolean ot_unix_fdatasync (int fd, GError **error); gboolean ot_unix_close (int fd, GError **error); +gboolean ot_unix_open_noatime (const char *path, int *out_fd, GError **error); + G_END_DECLS #endif diff --git a/src/libotutil/ot-variant-utils.c b/src/libotutil/ot-variant-utils.c index 93b69b84..973dbf16 100644 --- a/src/libotutil/ot-variant-utils.c +++ b/src/libotutil/ot-variant-utils.c @@ -118,11 +118,16 @@ ot_util_variant_map (GFile *src, const char *path = NULL; ot_lvariant GVariant *ret_variant = NULL; GMappedFile *mfile = NULL; + int fd; path = ot_gfile_get_path_cached (src); - mfile = g_mapped_file_new (path, FALSE, error); + if (!ot_unix_open_noatime (path, &fd, error)) + goto out; + mfile = g_mapped_file_new_from_fd (fd, FALSE, error); if (!mfile) goto out; + if (!ot_unix_close (fd, error)) + goto out; ret_variant = g_variant_new_from_data (type, g_mapped_file_get_contents (mfile),