nfsfh.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * include/linux/nfsd/nfsfh.h
  3. *
  4. * This file describes the layout of the file handles as passed
  5. * over the wire.
  6. *
  7. * Earlier versions of knfsd used to sign file handles using keyed MD5
  8. * or SHA. I've removed this code, because it doesn't give you more
  9. * security than blocking external access to port 2049 on your firewall.
  10. *
  11. * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
  12. */
  13. #ifndef _LINUX_NFSD_FH_H
  14. #define _LINUX_NFSD_FH_H
  15. #include <asm/types.h>
  16. #ifdef __KERNEL__
  17. # include <linux/types.h>
  18. # include <linux/string.h>
  19. # include <linux/fs.h>
  20. #endif
  21. #include <linux/nfsd/const.h>
  22. #include <linux/nfsd/debug.h>
  23. /*
  24. * This is the old "dentry style" Linux NFSv2 file handle.
  25. *
  26. * The xino and xdev fields are currently used to transport the
  27. * ino/dev of the exported inode.
  28. */
  29. struct nfs_fhbase_old {
  30. __u32 fb_dcookie; /* dentry cookie - always 0xfeebbaca */
  31. __u32 fb_ino; /* our inode number */
  32. __u32 fb_dirino; /* dir inode number, 0 for directories */
  33. __u32 fb_dev; /* our device */
  34. __u32 fb_xdev;
  35. __u32 fb_xino;
  36. __u32 fb_generation;
  37. };
  38. /*
  39. * This is the new flexible, extensible style NFSv2/v3 file handle.
  40. * by Neil Brown <neilb@cse.unsw.edu.au> - March 2000
  41. *
  42. * The file handle is seens as a list of 4byte words.
  43. * The first word contains a version number (1) and four descriptor bytes
  44. * that tell how the remaining 3 variable length fields should be handled.
  45. * These three bytes are auth_type, fsid_type and fileid_type.
  46. *
  47. * All 4byte values are in host-byte-order.
  48. *
  49. * The auth_type field specifies how the filehandle can be authenticated
  50. * This might allow a file to be confirmed to be in a writable part of a
  51. * filetree without checking the path from it upto the root.
  52. * Current values:
  53. * 0 - No authentication. fb_auth is 0 bytes long
  54. * Possible future values:
  55. * 1 - 4 bytes taken from MD5 hash of the remainer of the file handle
  56. * prefixed by a secret and with the important export flags.
  57. *
  58. * The fsid_type identifies how the filesystem (or export point) is
  59. * encoded.
  60. * Current values:
  61. * 0 - 4 byte device id (ms-2-bytes major, ls-2-bytes minor), 4byte inode number
  62. * NOTE: we cannot use the kdev_t device id value, because kdev_t.h
  63. * says we mustn't. We must break it up and reassemble.
  64. * 1 - 4 byte user specified identifier
  65. * 2 - 4 byte major, 4 byte minor, 4 byte inode number - DEPRECATED
  66. * 3 - 4 byte device id, encoded for user-space, 4 byte inode number
  67. *
  68. * The fileid_type identified how the file within the filesystem is encoded.
  69. * This is (will be) passed to, and set by, the underlying filesystem if it supports
  70. * filehandle operations. The filesystem must not use the value '0' or '0xff' and may
  71. * only use the values 1 and 2 as defined below:
  72. * Current values:
  73. * 0 - The root, or export point, of the filesystem. fb_fileid is 0 bytes.
  74. * 1 - 32bit inode number, 32 bit generation number.
  75. * 2 - 32bit inode number, 32 bit generation number, 32 bit parent directory inode number.
  76. *
  77. */
  78. struct nfs_fhbase_new {
  79. __u8 fb_version; /* == 1, even => nfs_fhbase_old */
  80. __u8 fb_auth_type;
  81. __u8 fb_fsid_type;
  82. __u8 fb_fileid_type;
  83. __u32 fb_auth[1];
  84. /* __u32 fb_fsid[0]; floating */
  85. /* __u32 fb_fileid[0]; floating */
  86. };
  87. struct knfsd_fh {
  88. unsigned int fh_size; /* significant for NFSv3.
  89. * Points to the current size while building
  90. * a new file handle
  91. */
  92. union {
  93. struct nfs_fhbase_old fh_old;
  94. __u32 fh_pad[NFS4_FHSIZE/4];
  95. struct nfs_fhbase_new fh_new;
  96. } fh_base;
  97. };
  98. #define ofh_dcookie fh_base.fh_old.fb_dcookie
  99. #define ofh_ino fh_base.fh_old.fb_ino
  100. #define ofh_dirino fh_base.fh_old.fb_dirino
  101. #define ofh_dev fh_base.fh_old.fb_dev
  102. #define ofh_xdev fh_base.fh_old.fb_xdev
  103. #define ofh_xino fh_base.fh_old.fb_xino
  104. #define ofh_generation fh_base.fh_old.fb_generation
  105. #define fh_version fh_base.fh_new.fb_version
  106. #define fh_fsid_type fh_base.fh_new.fb_fsid_type
  107. #define fh_auth_type fh_base.fh_new.fb_auth_type
  108. #define fh_fileid_type fh_base.fh_new.fb_fileid_type
  109. #define fh_auth fh_base.fh_new.fb_auth
  110. #define fh_fsid fh_base.fh_new.fb_auth
  111. #ifdef __KERNEL__
  112. static inline __u32 ino_t_to_u32(ino_t ino)
  113. {
  114. return (__u32) ino;
  115. }
  116. static inline ino_t u32_to_ino_t(__u32 uino)
  117. {
  118. return (ino_t) uino;
  119. }
  120. /*
  121. * This is the internal representation of an NFS handle used in knfsd.
  122. * pre_mtime/post_version will be used to support wcc_attr's in NFSv3.
  123. */
  124. typedef struct svc_fh {
  125. struct knfsd_fh fh_handle; /* FH data */
  126. struct dentry * fh_dentry; /* validated dentry */
  127. struct svc_export * fh_export; /* export pointer */
  128. int fh_maxsize; /* max size for fh_handle */
  129. unsigned char fh_locked; /* inode locked by us */
  130. #ifdef CONFIG_NFSD_V3
  131. unsigned char fh_post_saved; /* post-op attrs saved */
  132. unsigned char fh_pre_saved; /* pre-op attrs saved */
  133. /* Pre-op attributes saved during fh_lock */
  134. __u64 fh_pre_size; /* size before operation */
  135. struct timespec fh_pre_mtime; /* mtime before oper */
  136. struct timespec fh_pre_ctime; /* ctime before oper */
  137. /* Post-op attributes saved in fh_unlock */
  138. umode_t fh_post_mode; /* i_mode */
  139. nlink_t fh_post_nlink; /* i_nlink */
  140. uid_t fh_post_uid; /* i_uid */
  141. gid_t fh_post_gid; /* i_gid */
  142. __u64 fh_post_size; /* i_size */
  143. unsigned long fh_post_blocks; /* i_blocks */
  144. unsigned long fh_post_blksize;/* i_blksize */
  145. __u32 fh_post_rdev[2];/* i_rdev */
  146. struct timespec fh_post_atime; /* i_atime */
  147. struct timespec fh_post_mtime; /* i_mtime */
  148. struct timespec fh_post_ctime; /* i_ctime */
  149. #endif /* CONFIG_NFSD_V3 */
  150. } svc_fh;
  151. static inline void mk_fsid_v0(u32 *fsidv, dev_t dev, ino_t ino)
  152. {
  153. fsidv[0] = htonl((MAJOR(dev)<<16) |
  154. MINOR(dev));
  155. fsidv[1] = ino_t_to_u32(ino);
  156. }
  157. static inline void mk_fsid_v1(u32 *fsidv, u32 fsid)
  158. {
  159. fsidv[0] = fsid;
  160. }
  161. static inline void mk_fsid_v2(u32 *fsidv, dev_t dev, ino_t ino)
  162. {
  163. fsidv[0] = htonl(MAJOR(dev));
  164. fsidv[1] = htonl(MINOR(dev));
  165. fsidv[2] = ino_t_to_u32(ino);
  166. }
  167. static inline void mk_fsid_v3(u32 *fsidv, dev_t dev, ino_t ino)
  168. {
  169. fsidv[0] = new_encode_dev(dev);
  170. fsidv[1] = ino_t_to_u32(ino);
  171. }
  172. static inline int key_len(int type)
  173. {
  174. switch(type) {
  175. case 0: return 8;
  176. case 1: return 4;
  177. case 2: return 12;
  178. case 3: return 8;
  179. default: return 0;
  180. }
  181. }
  182. /*
  183. * Shorthand for dprintk()'s
  184. */
  185. extern char * SVCFH_fmt(struct svc_fh *fhp);
  186. /*
  187. * Function prototypes
  188. */
  189. u32 fh_verify(struct svc_rqst *, struct svc_fh *, int, int);
  190. int fh_compose(struct svc_fh *, struct svc_export *, struct dentry *, struct svc_fh *);
  191. int fh_update(struct svc_fh *);
  192. void fh_put(struct svc_fh *);
  193. static __inline__ struct svc_fh *
  194. fh_copy(struct svc_fh *dst, struct svc_fh *src)
  195. {
  196. if (src->fh_dentry || src->fh_locked) {
  197. struct dentry *dentry = src->fh_dentry;
  198. printk(KERN_ERR "fh_copy: copying %s/%s, already verified!\n",
  199. dentry->d_parent->d_name.name, dentry->d_name.name);
  200. }
  201. *dst = *src;
  202. return dst;
  203. }
  204. static __inline__ struct svc_fh *
  205. fh_init(struct svc_fh *fhp, int maxsize)
  206. {
  207. memset(fhp, 0, sizeof(*fhp));
  208. fhp->fh_maxsize = maxsize;
  209. return fhp;
  210. }
  211. #ifdef CONFIG_NFSD_V3
  212. /*
  213. * Fill in the pre_op attr for the wcc data
  214. */
  215. static inline void
  216. fill_pre_wcc(struct svc_fh *fhp)
  217. {
  218. struct inode *inode;
  219. inode = fhp->fh_dentry->d_inode;
  220. if (!fhp->fh_pre_saved) {
  221. fhp->fh_pre_mtime = inode->i_mtime;
  222. fhp->fh_pre_ctime = inode->i_ctime;
  223. fhp->fh_pre_size = inode->i_size;
  224. fhp->fh_pre_saved = 1;
  225. }
  226. }
  227. /*
  228. * Fill in the post_op attr for the wcc data
  229. */
  230. static inline void
  231. fill_post_wcc(struct svc_fh *fhp)
  232. {
  233. struct inode *inode = fhp->fh_dentry->d_inode;
  234. if (fhp->fh_post_saved)
  235. printk("nfsd: inode locked twice during operation.\n");
  236. fhp->fh_post_mode = inode->i_mode;
  237. fhp->fh_post_nlink = inode->i_nlink;
  238. fhp->fh_post_uid = inode->i_uid;
  239. fhp->fh_post_gid = inode->i_gid;
  240. fhp->fh_post_size = inode->i_size;
  241. if (inode->i_blksize) {
  242. fhp->fh_post_blksize = inode->i_blksize;
  243. fhp->fh_post_blocks = inode->i_blocks;
  244. } else {
  245. fhp->fh_post_blksize = BLOCK_SIZE;
  246. /* how much do we care for accuracy with MinixFS? */
  247. fhp->fh_post_blocks = (inode->i_size+511) >> 9;
  248. }
  249. fhp->fh_post_rdev[0] = htonl((u32)imajor(inode));
  250. fhp->fh_post_rdev[1] = htonl((u32)iminor(inode));
  251. fhp->fh_post_atime = inode->i_atime;
  252. fhp->fh_post_mtime = inode->i_mtime;
  253. fhp->fh_post_ctime = inode->i_ctime;
  254. fhp->fh_post_saved = 1;
  255. }
  256. #else
  257. #define fill_pre_wcc(ignored)
  258. #define fill_post_wcc(notused)
  259. #endif /* CONFIG_NFSD_V3 */
  260. /*
  261. * Lock a file handle/inode
  262. * NOTE: both fh_lock and fh_unlock are done "by hand" in
  263. * vfs.c:nfsd_rename as it needs to grab 2 i_mutex's at once
  264. * so, any changes here should be reflected there.
  265. */
  266. static inline void
  267. fh_lock(struct svc_fh *fhp)
  268. {
  269. struct dentry *dentry = fhp->fh_dentry;
  270. struct inode *inode;
  271. dfprintk(FILEOP, "nfsd: fh_lock(%s) locked = %d\n",
  272. SVCFH_fmt(fhp), fhp->fh_locked);
  273. if (!fhp->fh_dentry) {
  274. printk(KERN_ERR "fh_lock: fh not verified!\n");
  275. return;
  276. }
  277. if (fhp->fh_locked) {
  278. printk(KERN_WARNING "fh_lock: %s/%s already locked!\n",
  279. dentry->d_parent->d_name.name, dentry->d_name.name);
  280. return;
  281. }
  282. inode = dentry->d_inode;
  283. mutex_lock(&inode->i_mutex);
  284. fill_pre_wcc(fhp);
  285. fhp->fh_locked = 1;
  286. }
  287. /*
  288. * Unlock a file handle/inode
  289. */
  290. static inline void
  291. fh_unlock(struct svc_fh *fhp)
  292. {
  293. if (!fhp->fh_dentry)
  294. printk(KERN_ERR "fh_unlock: fh not verified!\n");
  295. if (fhp->fh_locked) {
  296. fill_post_wcc(fhp);
  297. mutex_unlock(&fhp->fh_dentry->d_inode->i_mutex);
  298. fhp->fh_locked = 0;
  299. }
  300. }
  301. #endif /* __KERNEL__ */
  302. #endif /* _LINUX_NFSD_FH_H */