nfsfh.h 7.7 KB

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