/** @file file_handle_tools.h @brief Tools for creating and working with file handles @details Copyright (c) 2024 Acronis International GmbH @author Bruce Wang ([email protected]) @since $Id: $ */ #pragma once #include "compat.h" #include "memory.h" typedef struct { uint32_t handle_bytes; int handle_type; int mnt_id; unsigned char* f_handle; } file_handle_info_t; // init_empty initialize message enough so 'free' can work static inline void file_handle_info_init_empty(file_handle_info_t* info) { info->f_handle = NULL; } // It is a bit counterintuitive but 'info->mnt_id' is always filled in despite the return value. // If 'f_handle' is non-null, it can be used static inline int file_handle_info_make_with_alloc_flags(file_handle_info_t* info, const struct path *path, bool nowait) { int dwords = MAX_HANDLE_SZ >> 2; struct dentry *dentry = path->dentry; struct vfsmount* mnt = path->mnt; info->mnt_id = mnt ? get_mnt_id(mnt) : 0; info->f_handle = NULL; info->handle_bytes = 0; #ifdef HAVE_EXPORTFS_ENCODE_INODE_FH if (!dentry || !dentry->d_inode || !dentry->d_inode->i_sb || !dentry->d_inode->i_sb->s_export_op) { return -EFAULT; } #else if (!dentry || !dentry->d_sb || !dentry->d_sb->s_export_op) { return -EFAULT; } #endif info->f_handle = mem_alloc_with_alloc_flags(MAX_HANDLE_SZ, nowait); if (!info->f_handle) { return -ENOMEM; } // Returns an enum fid_type or a negative on errno. info->handle_type = exportfs_encode_fh(dentry, (struct fid *)info->f_handle, &dwords, 0); if (info->handle_type < 0) { goto err; } info->handle_bytes = dwords * sizeof(u32); return 0; err: mem_free(info->f_handle); info->f_handle = NULL; return info->handle_type; } static inline int file_handle_info_make(file_handle_info_t* info, const struct path *path) { return file_handle_info_make_with_alloc_flags(info, path, false /*nowait*/); } static inline int file_handle_info_make_nowait(file_handle_info_t* info, const struct path *path) { return file_handle_info_make_with_alloc_flags(info, path, true /*nowait*/); } static inline void file_handle_info_free(file_handle_info_t* info) { if (info->f_handle) { mem_free(info->f_handle); } }