nfsfh.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 1995, 1996, 1997 Olaf Kirch <[email protected]>
  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. int fh_flags; /* FH flags */
  85. bool fh_post_saved; /* post-op attrs saved */
  86. bool fh_pre_saved; /* pre-op attrs saved */
  87. /* Pre-op attributes saved when inode is locked */
  88. __u64 fh_pre_size; /* size before operation */
  89. struct timespec64 fh_pre_mtime; /* mtime before oper */
  90. struct timespec64 fh_pre_ctime; /* ctime before oper */
  91. /*
  92. * pre-op nfsv4 change attr: note must check IS_I_VERSION(inode)
  93. * to find out if it is valid.
  94. */
  95. u64 fh_pre_change;
  96. /* Post-op attributes saved in fh_fill_post_attrs() */
  97. struct kstat fh_post_attr; /* full attrs after operation */
  98. u64 fh_post_change; /* nfsv4 change; see above */
  99. } svc_fh;
  100. #define NFSD4_FH_FOREIGN (1<<0)
  101. #define SET_FH_FLAG(c, f) ((c)->fh_flags |= (f))
  102. #define HAS_FH_FLAG(c, f) ((c)->fh_flags & (f))
  103. enum nfsd_fsid {
  104. FSID_DEV = 0,
  105. FSID_NUM,
  106. FSID_MAJOR_MINOR,
  107. FSID_ENCODE_DEV,
  108. FSID_UUID4_INUM,
  109. FSID_UUID8,
  110. FSID_UUID16,
  111. FSID_UUID16_INUM,
  112. };
  113. enum fsid_source {
  114. FSIDSOURCE_DEV,
  115. FSIDSOURCE_FSID,
  116. FSIDSOURCE_UUID,
  117. };
  118. extern enum fsid_source fsid_source(const struct svc_fh *fhp);
  119. /*
  120. * This might look a little large to "inline" but in all calls except
  121. * one, 'vers' is constant so moste of the function disappears.
  122. *
  123. * In some cases the values are considered to be host endian and in
  124. * others, net endian. fsidv is always considered to be u32 as the
  125. * callers don't know which it will be. So we must use __force to keep
  126. * sparse from complaining. Since these values are opaque to the
  127. * client, that shouldn't be a problem.
  128. */
  129. static inline void mk_fsid(int vers, u32 *fsidv, dev_t dev, ino_t ino,
  130. u32 fsid, unsigned char *uuid)
  131. {
  132. u32 *up;
  133. switch(vers) {
  134. case FSID_DEV:
  135. fsidv[0] = (__force __u32)htonl((MAJOR(dev)<<16) |
  136. MINOR(dev));
  137. fsidv[1] = ino_t_to_u32(ino);
  138. break;
  139. case FSID_NUM:
  140. fsidv[0] = fsid;
  141. break;
  142. case FSID_MAJOR_MINOR:
  143. fsidv[0] = (__force __u32)htonl(MAJOR(dev));
  144. fsidv[1] = (__force __u32)htonl(MINOR(dev));
  145. fsidv[2] = ino_t_to_u32(ino);
  146. break;
  147. case FSID_ENCODE_DEV:
  148. fsidv[0] = new_encode_dev(dev);
  149. fsidv[1] = ino_t_to_u32(ino);
  150. break;
  151. case FSID_UUID4_INUM:
  152. /* 4 byte fsid and inode number */
  153. up = (u32*)uuid;
  154. fsidv[0] = ino_t_to_u32(ino);
  155. fsidv[1] = up[0] ^ up[1] ^ up[2] ^ up[3];
  156. break;
  157. case FSID_UUID8:
  158. /* 8 byte fsid */
  159. up = (u32*)uuid;
  160. fsidv[0] = up[0] ^ up[2];
  161. fsidv[1] = up[1] ^ up[3];
  162. break;
  163. case FSID_UUID16:
  164. /* 16 byte fsid - NFSv3+ only */
  165. memcpy(fsidv, uuid, 16);
  166. break;
  167. case FSID_UUID16_INUM:
  168. /* 8 byte inode and 16 byte fsid */
  169. *(u64*)fsidv = (u64)ino;
  170. memcpy(fsidv+2, uuid, 16);
  171. break;
  172. default: BUG();
  173. }
  174. }
  175. static inline int key_len(int type)
  176. {
  177. switch(type) {
  178. case FSID_DEV: return 8;
  179. case FSID_NUM: return 4;
  180. case FSID_MAJOR_MINOR: return 12;
  181. case FSID_ENCODE_DEV: return 8;
  182. case FSID_UUID4_INUM: return 8;
  183. case FSID_UUID8: return 8;
  184. case FSID_UUID16: return 16;
  185. case FSID_UUID16_INUM: return 24;
  186. default: return 0;
  187. }
  188. }
  189. /*
  190. * Shorthand for dprintk()'s
  191. */
  192. extern char * SVCFH_fmt(struct svc_fh *fhp);
  193. /*
  194. * Function prototypes
  195. */
  196. __be32 fh_verify(struct svc_rqst *, struct svc_fh *, umode_t, int);
  197. __be32 fh_compose(struct svc_fh *, struct svc_export *, struct dentry *, struct svc_fh *);
  198. __be32 fh_update(struct svc_fh *);
  199. void fh_put(struct svc_fh *);
  200. static __inline__ struct svc_fh *
  201. fh_copy(struct svc_fh *dst, struct svc_fh *src)
  202. {
  203. WARN_ON(src->fh_dentry);
  204. *dst = *src;
  205. return dst;
  206. }
  207. static inline void
  208. fh_copy_shallow(struct knfsd_fh *dst, struct knfsd_fh *src)
  209. {
  210. dst->fh_size = src->fh_size;
  211. memcpy(&dst->fh_raw, &src->fh_raw, src->fh_size);
  212. }
  213. static __inline__ struct svc_fh *
  214. fh_init(struct svc_fh *fhp, int maxsize)
  215. {
  216. memset(fhp, 0, sizeof(*fhp));
  217. fhp->fh_maxsize = maxsize;
  218. return fhp;
  219. }
  220. static inline bool fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
  221. {
  222. if (fh1->fh_size != fh2->fh_size)
  223. return false;
  224. if (memcmp(fh1->fh_raw, fh2->fh_raw, fh1->fh_size) != 0)
  225. return false;
  226. return true;
  227. }
  228. static inline bool fh_fsid_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
  229. {
  230. if (fh1->fh_fsid_type != fh2->fh_fsid_type)
  231. return false;
  232. if (memcmp(fh1->fh_fsid, fh2->fh_fsid, key_len(fh1->fh_fsid_type)) != 0)
  233. return false;
  234. return true;
  235. }
  236. #ifdef CONFIG_CRC32
  237. /**
  238. * knfsd_fh_hash - calculate the crc32 hash for the filehandle
  239. * @fh - pointer to filehandle
  240. *
  241. * returns a crc32 hash for the filehandle that is compatible with
  242. * the one displayed by "wireshark".
  243. */
  244. static inline u32 knfsd_fh_hash(const struct knfsd_fh *fh)
  245. {
  246. return ~crc32_le(0xFFFFFFFF, fh->fh_raw, fh->fh_size);
  247. }
  248. #else
  249. static inline u32 knfsd_fh_hash(const struct knfsd_fh *fh)
  250. {
  251. return 0;
  252. }
  253. #endif
  254. /**
  255. * fh_clear_pre_post_attrs - Reset pre/post attributes
  256. * @fhp: file handle to be updated
  257. *
  258. */
  259. static inline void fh_clear_pre_post_attrs(struct svc_fh *fhp)
  260. {
  261. fhp->fh_post_saved = false;
  262. fhp->fh_pre_saved = false;
  263. }
  264. /*
  265. * We could use i_version alone as the change attribute. However,
  266. * i_version can go backwards after a reboot. On its own that doesn't
  267. * necessarily cause a problem, but if i_version goes backwards and then
  268. * is incremented again it could reuse a value that was previously used
  269. * before boot, and a client who queried the two values might
  270. * incorrectly assume nothing changed.
  271. *
  272. * By using both ctime and the i_version counter we guarantee that as
  273. * long as time doesn't go backwards we never reuse an old value.
  274. */
  275. static inline u64 nfsd4_change_attribute(struct kstat *stat,
  276. struct inode *inode)
  277. {
  278. if (inode->i_sb->s_export_op->fetch_iversion)
  279. return inode->i_sb->s_export_op->fetch_iversion(inode);
  280. else if (IS_I_VERSION(inode)) {
  281. u64 chattr;
  282. chattr = stat->ctime.tv_sec;
  283. chattr <<= 30;
  284. chattr += stat->ctime.tv_nsec;
  285. chattr += inode_query_iversion(inode);
  286. return chattr;
  287. } else
  288. return time_to_chattr(&stat->ctime);
  289. }
  290. extern void fh_fill_pre_attrs(struct svc_fh *fhp);
  291. extern void fh_fill_post_attrs(struct svc_fh *fhp);
  292. extern void fh_fill_both_attrs(struct svc_fh *fhp);
  293. #endif /* _LINUX_NFSD_NFSFH_H */