From: Yu Watanabe Date: Tue, 3 Feb 2026 15:27:10 +0000 (+0900) Subject: mountpoint-util: introduce name_to_handle_at_u64() and friends X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e7ce1e22b936c6e77c3d90f681fc211e8888bb21;p=thirdparty%2Fsystemd.git mountpoint-util: introduce name_to_handle_at_u64() and friends They will be used later. --- diff --git a/src/basic/mountpoint-util.c b/src/basic/mountpoint-util.c index 3bb82ef239b..547183395e4 100644 --- a/src/basic/mountpoint-util.c +++ b/src/basic/mountpoint-util.c @@ -17,6 +17,7 @@ #include "stat-util.h" #include "string-util.h" #include "strv.h" +#include "unaligned.h" /* This is the original MAX_HANDLE_SZ definition from the kernel, when the API was introduced. We use that in place of * any more currently defined value to future-proof things: if the size is increased in the API headers, and our code @@ -138,6 +139,27 @@ int name_to_handle_at_try_fid( return name_to_handle_at_loop(fd, path, ret_handle, ret_mnt_id, flags & ~AT_HANDLE_FID); } +int name_to_handle_at_u64(int fd, const char *path, uint64_t *ret) { + _cleanup_free_ struct file_handle *h = NULL; + int r; + + assert(fd >= 0 || fd == AT_FDCWD); + + /* This provides the first 64bit of the file handle. */ + + r = name_to_handle_at_loop(fd, path, &h, /* ret_mnt_id= */ NULL, /* flags= */ 0); + if (r < 0) + return r; + if (h->handle_bytes < sizeof(uint64_t)) + return -EBADMSG; + + if (ret) + /* Note, "struct file_handle" is 32bit aligned usually, but we need to read a 64bit value from it */ + *ret = unaligned_read_ne64(h->f_handle); + + return 0; +} + bool file_handle_equal(const struct file_handle *a, const struct file_handle *b) { if (a == b) return true; diff --git a/src/basic/mountpoint-util.h b/src/basic/mountpoint-util.h index 659f0385560..8799224f3d4 100644 --- a/src/basic/mountpoint-util.h +++ b/src/basic/mountpoint-util.h @@ -36,6 +36,13 @@ bool is_name_to_handle_at_fatal_error(int err); int name_to_handle_at_loop(int fd, const char *path, struct file_handle **ret_handle, int *ret_mnt_id, int flags); int name_to_handle_at_try_fid(int fd, const char *path, struct file_handle **ret_handle, int *ret_mnt_id, int flags); +int name_to_handle_at_u64(int fd, const char *path, uint64_t *ret); +static inline int path_to_handle_u64(const char *path, uint64_t *ret) { + return name_to_handle_at_u64(AT_FDCWD, path, ret); +} +static inline int fd_to_handle_u64(int fd, uint64_t *ret) { + return name_to_handle_at_u64(fd, NULL, ret); +} bool file_handle_equal(const struct file_handle *a, const struct file_handle *b);