nfsfh.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #include "export.h"
  16. /*
  17. * The file handle starts with a sequence of four-byte words.
  18. * The first word contains a version number (1) and three descriptor bytes
  19. * that tell how the remaining 3 variable length fields should be handled.
  20. * These three bytes are auth_type, fsid_type and fileid_type.
  21. *
  22. * All four-byte values are in host-byte-order.
  23. *
  24. * The auth_type field is deprecated and must be set to 0.
  25. *
  26. * The fsid_type identifies how the filesystem (or export point) is
  27. * encoded.
  28. * Current values:
  29. * 0 - 4 byte device id (ms-2-bytes major, ls-2-bytes minor), 4byte inode number
  30. * NOTE: we cannot use the kdev_t device id value, because kdev_t.h
  31. * says we mustn't. We must break it up and reassemble.
  32. * 1 - 4 byte user specified identifier
  33. * 2 - 4 byte major, 4 byte minor, 4 byte inode number - DEPRECATED
  34. * 3 - 4 byte device id, encoded for user-space, 4 byte inode number
  35. * 4 - 4 byte inode number and 4 byte uuid
  36. * 5 - 8 byte uuid
  37. * 6 - 16 byte uuid
  38. * 7 - 8 byte inode number and 16 byte uuid
  39. *
  40. * The fileid_type identifies how the file within the filesystem is encoded.
  41. * The values for this field are filesystem specific, exccept that
  42. * filesystems must not use the values '0' or '0xff'. 'See enum fid_type'
  43. * in include/linux/exportfs.h for currently registered values.
  44. */
  45. struct knfsd_fh {
  46. unsigned int fh_size; /*
  47. * Points to the current size while
  48. * building a new file handle.
  49. */
  50. u8 fh_raw[NFS4_FHSIZE];
  51. };
  52. #define fh_version fh_raw[0]
  53. #define fh_auth_type fh_raw[1]
  54. #define fh_fsid_type fh_raw[2]
  55. #define fh_fileid_type fh_raw[3]
  56. static inline u32 *fh_fsid(const struct knfsd_fh *fh)
  57. {
  58. return (u32 *)&fh->fh_raw[4];
  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_getattr(const struct svc_fh *fhp, struct kstat *stat);
  202. __be32 fh_compose(struct svc_fh *, struct svc_export *, struct dentry *, struct svc_fh *);
  203. __be32 fh_update(struct svc_fh *);
  204. void fh_put(struct svc_fh *);
  205. static __inline__ struct svc_fh *
  206. fh_copy(struct svc_fh *dst, const struct svc_fh *src)
  207. {
  208. WARN_ON(src->fh_dentry);
  209. *dst = *src;
  210. return dst;
  211. }
  212. static inline void
  213. fh_copy_shallow(struct knfsd_fh *dst, const struct knfsd_fh *src)
  214. {
  215. dst->fh_size = src->fh_size;
  216. memcpy(&dst->fh_raw, &src->fh_raw, src->fh_size);
  217. }
  218. static __inline__ struct svc_fh *
  219. fh_init(struct svc_fh *fhp, int maxsize)
  220. {
  221. memset(fhp, 0, sizeof(*fhp));
  222. fhp->fh_maxsize = maxsize;
  223. return fhp;
  224. }
  225. static inline bool fh_match(const struct knfsd_fh *fh1,
  226. const struct knfsd_fh *fh2)
  227. {
  228. if (fh1->fh_size != fh2->fh_size)
  229. return false;
  230. if (memcmp(fh1->fh_raw, fh2->fh_raw, fh1->fh_size) != 0)
  231. return false;
  232. return true;
  233. }
  234. static inline bool fh_fsid_match(const struct knfsd_fh *fh1,
  235. const struct knfsd_fh *fh2)
  236. {
  237. u32 *fsid1 = fh_fsid(fh1);
  238. u32 *fsid2 = fh_fsid(fh2);
  239. if (fh1->fh_fsid_type != fh2->fh_fsid_type)
  240. return false;
  241. if (memcmp(fsid1, fsid2, key_len(fh1->fh_fsid_type)) != 0)
  242. return false;
  243. return true;
  244. }
  245. /**
  246. * fh_want_write - Get write access to an export
  247. * @fhp: File handle of file to be written
  248. *
  249. * Caller must invoke fh_drop_write() when its write operation
  250. * is complete.
  251. *
  252. * Returns 0 if the file handle's export can be written to. Otherwise
  253. * the export is not prepared for updates, and the returned negative
  254. * errno value reflects the reason for the failure.
  255. */
  256. static inline int fh_want_write(struct svc_fh *fhp)
  257. {
  258. int ret;
  259. if (fhp->fh_want_write)
  260. return 0;
  261. ret = mnt_want_write(fhp->fh_export->ex_path.mnt);
  262. if (!ret)
  263. fhp->fh_want_write = true;
  264. return ret;
  265. }
  266. /**
  267. * fh_drop_write - Release write access on an export
  268. * @fhp: File handle of file on which fh_want_write() was previously called
  269. */
  270. static inline void fh_drop_write(struct svc_fh *fhp)
  271. {
  272. if (fhp->fh_want_write) {
  273. fhp->fh_want_write = false;
  274. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  275. }
  276. }
  277. /**
  278. * knfsd_fh_hash - calculate the crc32 hash for the filehandle
  279. * @fh - pointer to filehandle
  280. *
  281. * returns a crc32 hash for the filehandle that is compatible with
  282. * the one displayed by "wireshark".
  283. */
  284. static inline u32 knfsd_fh_hash(const struct knfsd_fh *fh)
  285. {
  286. return ~crc32_le(0xFFFFFFFF, fh->fh_raw, fh->fh_size);
  287. }
  288. /**
  289. * fh_clear_pre_post_attrs - Reset pre/post attributes
  290. * @fhp: file handle to be updated
  291. *
  292. */
  293. static inline void fh_clear_pre_post_attrs(struct svc_fh *fhp)
  294. {
  295. fhp->fh_post_saved = false;
  296. fhp->fh_pre_saved = false;
  297. }
  298. u64 nfsd4_change_attribute(const struct kstat *stat);
  299. __be32 __must_check fh_fill_pre_attrs(struct svc_fh *fhp);
  300. __be32 fh_fill_post_attrs(struct svc_fh *fhp);
  301. __be32 __must_check fh_fill_both_attrs(struct svc_fh *fhp);
  302. #endif /* _LINUX_NFSD_NFSFH_H */