NFS: Add linux/nfs_fh.h

Plenty of spots around the kernel need the full definition of struct
nfs_fh but not the cred, sunrpc, and uapi dependencies that
linux/nfs.h pulls in along with it.

Relocate struct nfs_fh to its own header, and include that header in
linux/nfs.h so existing consumers keep building. Over time, consumers
can then replace

  #include <linux/nfs.h>

with

  #include <linux/nfs_fh.h>

While relocating the code, add kernel-doc comments for the FH
operations and convert nfs_compare_fh() to return bool.

Link: https://patch.msgid.link/20260720141442.783935-2-cel@kernel.org
Signed-off-by: Chuck Lever <cel@kernel.org>
This commit is contained in:
Chuck Lever 2026-07-20 10:14:40 -04:00
parent f913d5d213
commit 3675a2d128
2 changed files with 65 additions and 37 deletions

View File

@ -11,8 +11,8 @@
#include <linux/cred.h>
#include <linux/sunrpc/auth.h>
#include <linux/sunrpc/msg_prot.h>
#include <linux/string.h>
#include <linux/crc32.h>
#include <linux/nfs_fh.h>
#include <uapi/linux/nfs.h>
/* The LOCALIO program is entirely private to Linux and is
@ -22,30 +22,6 @@
#define LOCALIOPROC_NULL 0
#define LOCALIOPROC_UUID_IS_LOCAL 1
/*
* This is the kernel NFS client file handle representation
*/
#define NFS_MAXFHSIZE 128
struct nfs_fh {
unsigned short size;
unsigned char data[NFS_MAXFHSIZE];
};
/*
* Returns a zero iff the size and data fields match.
* Checks only "size" bytes in the data field.
*/
static inline int nfs_compare_fh(const struct nfs_fh *a, const struct nfs_fh *b)
{
return a->size != b->size || memcmp(a->data, b->data, a->size) != 0;
}
static inline void nfs_copy_fh(struct nfs_fh *target, const struct nfs_fh *source)
{
target->size = source->size;
memcpy(target->data, source->data, source->size);
}
enum nfs3_stable_how {
NFS_UNSTABLE = 0,
NFS_DATA_SYNC = 1,
@ -55,15 +31,4 @@ enum nfs3_stable_how {
NFS_INVALID_STABLE_HOW = -1
};
/**
* nfs_fhandle_hash - calculate the crc32 hash for the filehandle
* @fh - pointer to filehandle
*
* returns a crc32 hash for the filehandle that is compatible with
* the one displayed by "wireshark".
*/
static inline u32 nfs_fhandle_hash(const struct nfs_fh *fh)
{
return ~crc32_le(0xFFFFFFFF, &fh->data[0], fh->size);
}
#endif /* _LINUX_NFS_H */

63
include/linux/nfs_fh.h Normal file
View File

@ -0,0 +1,63 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* struct nfs_fh is an NFS version-agnostic data structure that
* stores an NFS file handle. It is also commonly used in NFS
* related APIs.
*/
#ifndef _LINUX_NFS_FH_H
#define _LINUX_NFS_FH_H
#include <linux/types.h>
#include <linux/string.h>
#include <linux/crc32.h>
/*
* The largest file handle size today is an NFSv4 file handle,
* which can be up to 128 octets long.
*/
#define NFS_MAXFHSIZE 128
struct nfs_fh {
unsigned short size;
unsigned char data[NFS_MAXFHSIZE];
};
/**
* nfs_compare_fh - Compare two NFS file handles
* @a: An NFS file handle to be compared
* @b: An NFS file handle to be compared
*
* Checks only "size" bytes in each data field.
*
* Return: %false if the two file handles are equal, otherwise %true
*/
static inline bool nfs_compare_fh(const struct nfs_fh *a, const struct nfs_fh *b)
{
return a->size != b->size || memcmp(a->data, b->data, a->size) != 0;
}
/**
* nfs_copy_fh - Copy an NFS file handle
* @target: Destination file handle
* @source: Source file handle
*
* Copies source->size bytes of file handle data into target.
*/
static inline void nfs_copy_fh(struct nfs_fh *target, const struct nfs_fh *source)
{
target->size = source->size;
memcpy(target->data, source->data, source->size);
}
/**
* nfs_fhandle_hash - Calculate the crc32 hash for the filehandle
* @fh: An NFS file handle to hash
*
* Return: a crc32 hash for the filehandle that is compatible with
* the one displayed by "wireshark"
*/
static inline u32 nfs_fhandle_hash(const struct nfs_fh *fh)
{
return ~crc32_le(0xFFFFFFFF, &fh->data[0], fh->size);
}
#endif /* _LINUX_NFS_FH_H */