nfsfh.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
  4. *
  5. * This file describes the layout of the file handles as passed
  6. * over the wire.
  7. */
  8. #ifndef _LINUX_NFSD_NFSFH_H
  9. #define _LINUX_NFSD_NFSFH_H
  10. #include <linux/crc32.h>
  11. #include <linux/sunrpc/svc.h>
  12. #include <linux/iversion.h>
  13. #include <linux/exportfs.h>
  14. #include <linux/nfs4.h>
  15. /*
  16. * The file handle starts with a sequence of four-byte words.
  17. * The first word contains a version number (1) and three descriptor bytes
  18. * that tell how the remaining 3 variable length fields should be handled.
  19. * These three bytes are auth_type, fsid_type and fileid_type.
  20. *
  21. * All four-byte values are in host-byte-order.
  22. *
  23. * The auth_type field is deprecated and must be set to 0.
  24. *
  25. * The fsid_type identifies how the filesystem (or export point) is
  26. * encoded.
  27. * Current values:
  28. * 0 - 4 byte device id (ms-2-bytes major, ls-2-bytes minor), 4byte inode number
  29. * NOTE: we cannot use the kdev_t device id value, because kdev_t.h
  30. * says we mustn't. We must break it up and reassemble.
  31. * 1 - 4 byte user specified identifier
  32. * 2 - 4 byte major, 4 byte minor, 4 byte inode number - DEPRECATED
  33. * 3 - 4 byte device id, encoded for user-space, 4 byte inode number
  34. * 4 - 4 byte inode number and 4 byte uuid
  35. * 5 - 8 byte uuid
  36. * 6 - 16 byte uuid
  37. * 7 - 8 byte inode number and 16 byte uuid
  38. *
  39. * The fileid_type identifies how the file within the filesystem is encoded.
  40. * The values for this field are filesystem specific, exccept that
  41. * filesystems must not use the values '0' or '0xff'. 'See enum fid_type'
  42. * in include/linux/exportfs.h for currently registered values.
  43. */
  44. struct knfsd_fh {
  45. unsigned int fh_size; /*
  46. * Points to the current size while
  47. * building a new file handle.
  48. */
  49. union {
  50. char fh_raw[NFS4_FHSIZE];
  51. struct {
  52. u8 fh_version; /* == 1 */
  53. u8 fh_auth_type; /* deprecated */
  54. u8 fh_fsid_type;
  55. u8 fh_fileid_type;
  56. u32 fh_fsid[]; /* flexible-array member */
  57. };
  58. };
  59. };
  60. static inline __u32 ino_t_to_u32(ino_t ino)
  61. {
  62. return (__u32) ino;
  63. }
  64. static inline ino_t u32_to_ino_t(__u32 uino)
  65. {
  66. return (ino_t) uino;
  67. }
  68. /*
  69. * This is the internal representation of an NFS handle used in knfsd.
  70. * pre_mtime/post_version will be used to support wcc_attr's in NFSv3.
  71. */
  72. typedef struct svc_fh {
  73. struct knfsd_fh fh_handle; /* FH data */
  74. int fh_maxsize; /* max size for fh_handle */
  75. struct dentry * fh_dentry; /* validated dentry */
  76. struct svc_export * fh_export; /* export pointer */
  77. bool fh_want_write; /* remount protection taken */
  78. bool fh_no_wcc; /* no wcc data needed */
  79. bool fh_no_atomic_attr;
  80. /*
  81. * wcc data is not atomic with
  82. * operation
  83. */
  84. bool fh_use_wgather; /* NFSv2 wgather option */
  85. bool fh_64bit_cookies;/* readdir cookie size */
  86. int fh_flags; /* FH flags */
  87. bool fh_post_saved; /* post-op attrs saved */
  88. bool fh_pre_saved; /* pre-op attrs saved */
  89. /* Pre-op attributes saved when inode is locked */
  90. __u64 fh_pre_size; /* size before operation */
  91. struct timespec64 fh_pre_mtime; /* mtime before oper */
  92. struct timespec64 fh_pre_ctime; /* ctime before oper */
  93. /*
  94. * pre-op nfsv4 change attr: note must check IS_I_VERSION(inode)
  95. * to find out if it is valid.
  96. */
  97. u64 fh_pre_change;
  98. /* Post-op attributes saved in fh_fill_post_attrs() */
  99. struct kstat fh_post_attr; /* full attrs after operation */
  100. u64 fh_post_change; /* nfsv4 change; see above */
  101. } svc_fh;
  102. #define NFSD4_FH_FOREIGN (1<<0)
  103. #define SET_FH_FLAG(c, f) ((c)->fh_flags |= (f))
  104. #define HAS_FH_FLAG(c, f) ((c)->fh_flags & (f))
  105. enum nfsd_fsid {
  106. FSID_DEV = 0,
  107. FSID_NUM,
  108. FSID_MAJOR_MINOR,
  109. FSID_ENCODE_DEV,
  110. FSID_UUID4_INUM,
  111. FSID_UUID8,
  112. FSID_UUID16,
  113. FSID_UUID16_INUM,
  114. };
  115. enum fsid_source {
  116. FSIDSOURCE_DEV,
  117. FSIDSOURCE_FSID,
  118. FSIDSOURCE_UUID,
  119. };
  120. extern enum fsid_source fsid_source(const struct svc_fh *fhp);
  121. /*
  122. * This might look a little large to "inline" but in all calls except
  123. * one, 'vers' is constant so moste of the function disappears.
  124. *
  125. * In some cases the values are considered to be host endian and in
  126. * others, net endian. fsidv is always considered to be u32 as the
  127. * callers don't know which it will be. So we must use __force to keep
  128. * sparse from complaining. Since these values are opaque to the
  129. * client, that shouldn't be a problem.
  130. */
  131. static inline void mk_fsid(int vers, u32 *fsidv, dev_t dev, ino_t ino,
  132. u32 fsid, unsigned char *uuid)
  133. {
  134. u32 *up;
  135. switch(vers) {
  136. case FSID_DEV:
  137. fsidv[0] = (__force __u32)htonl((MAJOR(dev)<<16) |
  138. MINOR(dev));
  139. fsidv[1] = ino_t_to_u32(ino);
  140. break;
  141. case FSID_NUM:
  142. fsidv[0] = fsid;
  143. break;
  144. case FSID_MAJOR_MINOR:
  145. fsidv[0] = (__force __u32)htonl(MAJOR(dev));
  146. fsidv[1] = (__force __u32)htonl(MINOR(dev));
  147. fsidv[2] = ino_t_to_u32(ino);
  148. break;
  149. case FSID_ENCODE_DEV:
  150. fsidv[0] = new_encode_dev(dev);
  151. fsidv[1] = ino_t_to_u32(ino);
  152. break;
  153. case FSID_UUID4_INUM:
  154. /* 4 byte fsid and inode number */
  155. up = (u32*)uuid;
  156. fsidv[0] = ino_t_to_u32(ino);
  157. fsidv[1] = up[0] ^ up[1] ^ up[2] ^ up[3];
  158. break;
  159. case FSID_UUID8:
  160. /* 8 byte fsid */
  161. up = (u32*)uuid;
  162. fsidv[0] = up[0] ^ up[2];
  163. fsidv[1] = up[1] ^ up[3];
  164. break;
  165. case FSID_UUID16:
  166. /* 16 byte fsid - NFSv3+ only */
  167. memcpy(fsidv, uuid, 16);
  168. break;
  169. case FSID_UUID16_INUM:
  170. /* 8 byte inode and 16 byte fsid */
  171. *(u64*)fsidv = (u64)ino;
  172. memcpy(fsidv+2, uuid, 16);
  173. break;
  174. default: BUG();
  175. }
  176. }
  177. static inline int key_len(int type)
  178. {
  179. switch(type) {
  180. case FSID_DEV: return 8;
  181. case FSID_NUM: return 4;
  182. case FSID_MAJOR_MINOR: return 12;
  183. case FSID_ENCODE_DEV: return 8;
  184. case FSID_UUID4_INUM: return 8;
  185. case FSID_UUID8: return 8;
  186. case FSID_UUID16: return 16;
  187. case FSID_UUID16_INUM: return 24;
  188. default: return 0;
  189. }
  190. }
  191. /*
  192. * Shorthand for dprintk()'s
  193. */
  194. extern char * SVCFH_fmt(struct svc_fh *fhp);
  195. /*
  196. * Function prototypes
  197. */
  198. __be32 fh_verify(struct svc_rqst *, struct svc_fh *, umode_t, int);
  199. __be32 fh_verify_local(struct net *, struct svc_cred *, struct auth_domain *,
  200. struct svc_fh *, umode_t, int);
  201. __be32 fh_compose(struct svc_fh *, struct svc_export *, struct dentry *, struct svc_fh *);
  202. __be32 fh_update(struct svc_fh *);
  203. void fh_put(struct svc_fh *);
  204. static __inline__ struct svc_fh *
  205. fh_copy(struct svc_fh *dst, const struct svc_fh *src)
  206. {
  207. WARN_ON(src->fh_dentry);
  208. *dst = *src;
  209. return dst;
  210. }
  211. static inline void
  212. fh_copy_shallow(struct knfsd_fh *dst, const struct knfsd_fh *src)
  213. {
  214. dst->fh_size = src->fh_size;
  215. memcpy(&dst->fh_raw, &src->fh_raw, src->fh_size);
  216. }
  217. static __inline__ struct svc_fh *
  218. fh_init(struct svc_fh *fhp, int maxsize)
  219. {
  220. memset(fhp, 0, sizeof(*fhp));
  221. fhp->fh_maxsize = maxsize;
  222. return fhp;
  223. }
  224. static inline bool fh_match(const struct knfsd_fh *fh1,
  225. const struct knfsd_fh *fh2)
  226. {
  227. if (fh1->fh_size != fh2->fh_size)
  228. return false;
  229. if (memcmp(fh1->fh_raw, fh2->fh_raw, fh1->fh_size) != 0)
  230. return false;
  231. return true;
  232. }
  233. static inline bool fh_fsid_match(const struct knfsd_fh *fh1,
  234. const struct knfsd_fh *fh2)
  235. {
  236. if (fh1->fh_fsid_type != fh2->fh_fsid_type)
  237. return false;
  238. if (memcmp(fh1->fh_fsid, fh2->fh_fsid, key_len(fh1->fh_fsid_type)) != 0)
  239. return false;
  240. return true;
  241. }
  242. /**
  243. * knfsd_fh_hash - calculate the crc32 hash for the filehandle
  244. * @fh - pointer to filehandle
  245. *
  246. * returns a crc32 hash for the filehandle that is compatible with
  247. * the one displayed by "wireshark".
  248. */
  249. static inline u32 knfsd_fh_hash(const struct knfsd_fh *fh)
  250. {
  251. return ~crc32_le(0xFFFFFFFF, fh->fh_raw, fh->fh_size);
  252. }
  253. /**
  254. * fh_clear_pre_post_attrs - Reset pre/post attributes
  255. * @fhp: file handle to be updated
  256. *
  257. */
  258. static inline void fh_clear_pre_post_attrs(struct svc_fh *fhp)
  259. {
  260. fhp->fh_post_saved = false;
  261. fhp->fh_pre_saved = false;
  262. }
  263. u64 nfsd4_change_attribute(const struct kstat *stat);
  264. __be32 __must_check fh_fill_pre_attrs(struct svc_fh *fhp);
  265. __be32 fh_fill_post_attrs(struct svc_fh *fhp);
  266. __be32 __must_check fh_fill_both_attrs(struct svc_fh *fhp);
  267. #endif /* _LINUX_NFSD_NFSFH_H */