nfsfh.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. struct kstat fh_post_attr; /* full attrs after operation */
  139. #endif /* CONFIG_NFSD_V3 */
  140. } svc_fh;
  141. enum nfsd_fsid {
  142. FSID_DEV = 0,
  143. FSID_NUM,
  144. FSID_MAJOR_MINOR,
  145. FSID_ENCODE_DEV,
  146. FSID_UUID4_INUM,
  147. FSID_UUID8,
  148. FSID_UUID16,
  149. FSID_UUID16_INUM,
  150. };
  151. enum fsid_source {
  152. FSIDSOURCE_DEV,
  153. FSIDSOURCE_FSID,
  154. FSIDSOURCE_UUID,
  155. };
  156. extern enum fsid_source fsid_source(struct svc_fh *fhp);
  157. /* This might look a little large to "inline" but in all calls except
  158. * one, 'vers' is constant so moste of the function disappears.
  159. */
  160. static inline void mk_fsid(int vers, u32 *fsidv, dev_t dev, ino_t ino,
  161. u32 fsid, unsigned char *uuid)
  162. {
  163. u32 *up;
  164. switch(vers) {
  165. case FSID_DEV:
  166. fsidv[0] = htonl((MAJOR(dev)<<16) |
  167. MINOR(dev));
  168. fsidv[1] = ino_t_to_u32(ino);
  169. break;
  170. case FSID_NUM:
  171. fsidv[0] = fsid;
  172. break;
  173. case FSID_MAJOR_MINOR:
  174. fsidv[0] = htonl(MAJOR(dev));
  175. fsidv[1] = htonl(MINOR(dev));
  176. fsidv[2] = ino_t_to_u32(ino);
  177. break;
  178. case FSID_ENCODE_DEV:
  179. fsidv[0] = new_encode_dev(dev);
  180. fsidv[1] = ino_t_to_u32(ino);
  181. break;
  182. case FSID_UUID4_INUM:
  183. /* 4 byte fsid and inode number */
  184. up = (u32*)uuid;
  185. fsidv[0] = ino_t_to_u32(ino);
  186. fsidv[1] = up[0] ^ up[1] ^ up[2] ^ up[3];
  187. break;
  188. case FSID_UUID8:
  189. /* 8 byte fsid */
  190. up = (u32*)uuid;
  191. fsidv[0] = up[0] ^ up[2];
  192. fsidv[1] = up[1] ^ up[3];
  193. break;
  194. case FSID_UUID16:
  195. /* 16 byte fsid - NFSv3+ only */
  196. memcpy(fsidv, uuid, 16);
  197. break;
  198. case FSID_UUID16_INUM:
  199. /* 8 byte inode and 16 byte fsid */
  200. *(u64*)fsidv = (u64)ino;
  201. memcpy(fsidv+2, uuid, 16);
  202. break;
  203. default: BUG();
  204. }
  205. }
  206. static inline int key_len(int type)
  207. {
  208. switch(type) {
  209. case FSID_DEV: return 8;
  210. case FSID_NUM: return 4;
  211. case FSID_MAJOR_MINOR: return 12;
  212. case FSID_ENCODE_DEV: return 8;
  213. case FSID_UUID4_INUM: return 8;
  214. case FSID_UUID8: return 8;
  215. case FSID_UUID16: return 16;
  216. case FSID_UUID16_INUM: return 24;
  217. default: return 0;
  218. }
  219. }
  220. /*
  221. * Shorthand for dprintk()'s
  222. */
  223. extern char * SVCFH_fmt(struct svc_fh *fhp);
  224. /*
  225. * Function prototypes
  226. */
  227. __be32 fh_verify(struct svc_rqst *, struct svc_fh *, int, int);
  228. __be32 fh_compose(struct svc_fh *, struct svc_export *, struct dentry *, struct svc_fh *);
  229. __be32 fh_update(struct svc_fh *);
  230. void fh_put(struct svc_fh *);
  231. static __inline__ struct svc_fh *
  232. fh_copy(struct svc_fh *dst, struct svc_fh *src)
  233. {
  234. WARN_ON(src->fh_dentry || src->fh_locked);
  235. *dst = *src;
  236. return dst;
  237. }
  238. static __inline__ struct svc_fh *
  239. fh_init(struct svc_fh *fhp, int maxsize)
  240. {
  241. memset(fhp, 0, sizeof(*fhp));
  242. fhp->fh_maxsize = maxsize;
  243. return fhp;
  244. }
  245. #ifdef CONFIG_NFSD_V3
  246. /*
  247. * Fill in the pre_op attr for the wcc data
  248. */
  249. static inline void
  250. fill_pre_wcc(struct svc_fh *fhp)
  251. {
  252. struct inode *inode;
  253. inode = fhp->fh_dentry->d_inode;
  254. if (!fhp->fh_pre_saved) {
  255. fhp->fh_pre_mtime = inode->i_mtime;
  256. fhp->fh_pre_ctime = inode->i_ctime;
  257. fhp->fh_pre_size = inode->i_size;
  258. fhp->fh_pre_saved = 1;
  259. }
  260. }
  261. extern void fill_post_wcc(struct svc_fh *);
  262. #else
  263. #define fill_pre_wcc(ignored)
  264. #define fill_post_wcc(notused)
  265. #endif /* CONFIG_NFSD_V3 */
  266. /*
  267. * Lock a file handle/inode
  268. * NOTE: both fh_lock and fh_unlock are done "by hand" in
  269. * vfs.c:nfsd_rename as it needs to grab 2 i_mutex's at once
  270. * so, any changes here should be reflected there.
  271. */
  272. static inline void
  273. fh_lock_nested(struct svc_fh *fhp, unsigned int subclass)
  274. {
  275. struct dentry *dentry = fhp->fh_dentry;
  276. struct inode *inode;
  277. dfprintk(FILEOP, "nfsd: fh_lock(%s) locked = %d\n",
  278. SVCFH_fmt(fhp), fhp->fh_locked);
  279. BUG_ON(!dentry);
  280. if (fhp->fh_locked) {
  281. printk(KERN_WARNING "fh_lock: %s/%s already locked!\n",
  282. dentry->d_parent->d_name.name, dentry->d_name.name);
  283. return;
  284. }
  285. inode = dentry->d_inode;
  286. mutex_lock_nested(&inode->i_mutex, subclass);
  287. fill_pre_wcc(fhp);
  288. fhp->fh_locked = 1;
  289. }
  290. static inline void
  291. fh_lock(struct svc_fh *fhp)
  292. {
  293. fh_lock_nested(fhp, I_MUTEX_NORMAL);
  294. }
  295. /*
  296. * Unlock a file handle/inode
  297. */
  298. static inline void
  299. fh_unlock(struct svc_fh *fhp)
  300. {
  301. BUG_ON(!fhp->fh_dentry);
  302. if (fhp->fh_locked) {
  303. fill_post_wcc(fhp);
  304. mutex_unlock(&fhp->fh_dentry->d_inode->i_mutex);
  305. fhp->fh_locked = 0;
  306. }
  307. }
  308. #endif /* __KERNEL__ */
  309. #endif /* _LINUX_NFSD_FH_H */