nfsfh.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 <uapi/linux/nfsd/nfsfh.h>
  13. #include <linux/iversion.h>
  14. static inline __u32 ino_t_to_u32(ino_t ino)
  15. {
  16. return (__u32) ino;
  17. }
  18. static inline ino_t u32_to_ino_t(__u32 uino)
  19. {
  20. return (ino_t) uino;
  21. }
  22. /*
  23. * This is the internal representation of an NFS handle used in knfsd.
  24. * pre_mtime/post_version will be used to support wcc_attr's in NFSv3.
  25. */
  26. typedef struct svc_fh {
  27. struct knfsd_fh fh_handle; /* FH data */
  28. int fh_maxsize; /* max size for fh_handle */
  29. struct dentry * fh_dentry; /* validated dentry */
  30. struct svc_export * fh_export; /* export pointer */
  31. bool fh_locked; /* inode locked by us */
  32. bool fh_want_write; /* remount protection taken */
  33. #ifdef CONFIG_NFSD_V3
  34. bool fh_post_saved; /* post-op attrs saved */
  35. bool fh_pre_saved; /* pre-op attrs saved */
  36. /* Pre-op attributes saved during fh_lock */
  37. __u64 fh_pre_size; /* size before operation */
  38. struct timespec fh_pre_mtime; /* mtime before oper */
  39. struct timespec fh_pre_ctime; /* ctime before oper */
  40. /*
  41. * pre-op nfsv4 change attr: note must check IS_I_VERSION(inode)
  42. * to find out if it is valid.
  43. */
  44. u64 fh_pre_change;
  45. /* Post-op attributes saved in fh_unlock */
  46. struct kstat fh_post_attr; /* full attrs after operation */
  47. u64 fh_post_change; /* nfsv4 change; see above */
  48. #endif /* CONFIG_NFSD_V3 */
  49. } svc_fh;
  50. enum nfsd_fsid {
  51. FSID_DEV = 0,
  52. FSID_NUM,
  53. FSID_MAJOR_MINOR,
  54. FSID_ENCODE_DEV,
  55. FSID_UUID4_INUM,
  56. FSID_UUID8,
  57. FSID_UUID16,
  58. FSID_UUID16_INUM,
  59. };
  60. enum fsid_source {
  61. FSIDSOURCE_DEV,
  62. FSIDSOURCE_FSID,
  63. FSIDSOURCE_UUID,
  64. };
  65. extern enum fsid_source fsid_source(struct svc_fh *fhp);
  66. /*
  67. * This might look a little large to "inline" but in all calls except
  68. * one, 'vers' is constant so moste of the function disappears.
  69. *
  70. * In some cases the values are considered to be host endian and in
  71. * others, net endian. fsidv is always considered to be u32 as the
  72. * callers don't know which it will be. So we must use __force to keep
  73. * sparse from complaining. Since these values are opaque to the
  74. * client, that shouldn't be a problem.
  75. */
  76. static inline void mk_fsid(int vers, u32 *fsidv, dev_t dev, ino_t ino,
  77. u32 fsid, unsigned char *uuid)
  78. {
  79. u32 *up;
  80. switch(vers) {
  81. case FSID_DEV:
  82. fsidv[0] = (__force __u32)htonl((MAJOR(dev)<<16) |
  83. MINOR(dev));
  84. fsidv[1] = ino_t_to_u32(ino);
  85. break;
  86. case FSID_NUM:
  87. fsidv[0] = fsid;
  88. break;
  89. case FSID_MAJOR_MINOR:
  90. fsidv[0] = (__force __u32)htonl(MAJOR(dev));
  91. fsidv[1] = (__force __u32)htonl(MINOR(dev));
  92. fsidv[2] = ino_t_to_u32(ino);
  93. break;
  94. case FSID_ENCODE_DEV:
  95. fsidv[0] = new_encode_dev(dev);
  96. fsidv[1] = ino_t_to_u32(ino);
  97. break;
  98. case FSID_UUID4_INUM:
  99. /* 4 byte fsid and inode number */
  100. up = (u32*)uuid;
  101. fsidv[0] = ino_t_to_u32(ino);
  102. fsidv[1] = up[0] ^ up[1] ^ up[2] ^ up[3];
  103. break;
  104. case FSID_UUID8:
  105. /* 8 byte fsid */
  106. up = (u32*)uuid;
  107. fsidv[0] = up[0] ^ up[2];
  108. fsidv[1] = up[1] ^ up[3];
  109. break;
  110. case FSID_UUID16:
  111. /* 16 byte fsid - NFSv3+ only */
  112. memcpy(fsidv, uuid, 16);
  113. break;
  114. case FSID_UUID16_INUM:
  115. /* 8 byte inode and 16 byte fsid */
  116. *(u64*)fsidv = (u64)ino;
  117. memcpy(fsidv+2, uuid, 16);
  118. break;
  119. default: BUG();
  120. }
  121. }
  122. static inline int key_len(int type)
  123. {
  124. switch(type) {
  125. case FSID_DEV: return 8;
  126. case FSID_NUM: return 4;
  127. case FSID_MAJOR_MINOR: return 12;
  128. case FSID_ENCODE_DEV: return 8;
  129. case FSID_UUID4_INUM: return 8;
  130. case FSID_UUID8: return 8;
  131. case FSID_UUID16: return 16;
  132. case FSID_UUID16_INUM: return 24;
  133. default: return 0;
  134. }
  135. }
  136. /*
  137. * Shorthand for dprintk()'s
  138. */
  139. extern char * SVCFH_fmt(struct svc_fh *fhp);
  140. /*
  141. * Function prototypes
  142. */
  143. __be32 fh_verify(struct svc_rqst *, struct svc_fh *, umode_t, int);
  144. __be32 fh_compose(struct svc_fh *, struct svc_export *, struct dentry *, struct svc_fh *);
  145. __be32 fh_update(struct svc_fh *);
  146. void fh_put(struct svc_fh *);
  147. static __inline__ struct svc_fh *
  148. fh_copy(struct svc_fh *dst, struct svc_fh *src)
  149. {
  150. WARN_ON(src->fh_dentry || src->fh_locked);
  151. *dst = *src;
  152. return dst;
  153. }
  154. static inline void
  155. fh_copy_shallow(struct knfsd_fh *dst, struct knfsd_fh *src)
  156. {
  157. dst->fh_size = src->fh_size;
  158. memcpy(&dst->fh_base, &src->fh_base, src->fh_size);
  159. }
  160. static __inline__ struct svc_fh *
  161. fh_init(struct svc_fh *fhp, int maxsize)
  162. {
  163. memset(fhp, 0, sizeof(*fhp));
  164. fhp->fh_maxsize = maxsize;
  165. return fhp;
  166. }
  167. static inline bool fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
  168. {
  169. if (fh1->fh_size != fh2->fh_size)
  170. return false;
  171. if (memcmp(fh1->fh_base.fh_pad, fh2->fh_base.fh_pad, fh1->fh_size) != 0)
  172. return false;
  173. return true;
  174. }
  175. static inline bool fh_fsid_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
  176. {
  177. if (fh1->fh_fsid_type != fh2->fh_fsid_type)
  178. return false;
  179. if (memcmp(fh1->fh_fsid, fh2->fh_fsid, key_len(fh1->fh_fsid_type)) != 0)
  180. return false;
  181. return true;
  182. }
  183. #ifdef CONFIG_CRC32
  184. /**
  185. * knfsd_fh_hash - calculate the crc32 hash for the filehandle
  186. * @fh - pointer to filehandle
  187. *
  188. * returns a crc32 hash for the filehandle that is compatible with
  189. * the one displayed by "wireshark".
  190. */
  191. static inline u32
  192. knfsd_fh_hash(struct knfsd_fh *fh)
  193. {
  194. return ~crc32_le(0xFFFFFFFF, (unsigned char *)&fh->fh_base, fh->fh_size);
  195. }
  196. #else
  197. static inline u32
  198. knfsd_fh_hash(struct knfsd_fh *fh)
  199. {
  200. return 0;
  201. }
  202. #endif
  203. #ifdef CONFIG_NFSD_V3
  204. /*
  205. * The wcc data stored in current_fh should be cleared
  206. * between compound ops.
  207. */
  208. static inline void
  209. fh_clear_wcc(struct svc_fh *fhp)
  210. {
  211. fhp->fh_post_saved = false;
  212. fhp->fh_pre_saved = false;
  213. }
  214. /*
  215. * We could use i_version alone as the change attribute. However,
  216. * i_version can go backwards after a reboot. On its own that doesn't
  217. * necessarily cause a problem, but if i_version goes backwards and then
  218. * is incremented again it could reuse a value that was previously used
  219. * before boot, and a client who queried the two values might
  220. * incorrectly assume nothing changed.
  221. *
  222. * By using both ctime and the i_version counter we guarantee that as
  223. * long as time doesn't go backwards we never reuse an old value.
  224. */
  225. static inline u64 nfsd4_change_attribute(struct kstat *stat,
  226. struct inode *inode)
  227. {
  228. u64 chattr;
  229. chattr = stat->ctime.tv_sec;
  230. chattr <<= 30;
  231. chattr += stat->ctime.tv_nsec;
  232. chattr += inode_query_iversion(inode);
  233. return chattr;
  234. }
  235. extern void fill_pre_wcc(struct svc_fh *fhp);
  236. extern void fill_post_wcc(struct svc_fh *fhp);
  237. #else
  238. #define fh_clear_wcc(ignored)
  239. #define fill_pre_wcc(ignored)
  240. #define fill_post_wcc(notused)
  241. #endif /* CONFIG_NFSD_V3 */
  242. /*
  243. * Lock a file handle/inode
  244. * NOTE: both fh_lock and fh_unlock are done "by hand" in
  245. * vfs.c:nfsd_rename as it needs to grab 2 i_mutex's at once
  246. * so, any changes here should be reflected there.
  247. */
  248. static inline void
  249. fh_lock_nested(struct svc_fh *fhp, unsigned int subclass)
  250. {
  251. struct dentry *dentry = fhp->fh_dentry;
  252. struct inode *inode;
  253. BUG_ON(!dentry);
  254. if (fhp->fh_locked) {
  255. printk(KERN_WARNING "fh_lock: %pd2 already locked!\n",
  256. dentry);
  257. return;
  258. }
  259. inode = d_inode(dentry);
  260. inode_lock_nested(inode, subclass);
  261. fill_pre_wcc(fhp);
  262. fhp->fh_locked = true;
  263. }
  264. static inline void
  265. fh_lock(struct svc_fh *fhp)
  266. {
  267. fh_lock_nested(fhp, I_MUTEX_NORMAL);
  268. }
  269. /*
  270. * Unlock a file handle/inode
  271. */
  272. static inline void
  273. fh_unlock(struct svc_fh *fhp)
  274. {
  275. if (fhp->fh_locked) {
  276. fill_post_wcc(fhp);
  277. inode_unlock(d_inode(fhp->fh_dentry));
  278. fhp->fh_locked = false;
  279. }
  280. }
  281. #endif /* _LINUX_NFSD_NFSFH_H */