nfsfh.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * 4 - 4 byte inode number and 4 byte uuid
  68. * 5 - 8 byte uuid
  69. * 6 - 16 byte uuid
  70. * 7 - 8 byte inode number and 16 byte uuid
  71. *
  72. * The fileid_type identified how the file within the filesystem is encoded.
  73. * This is (will be) passed to, and set by, the underlying filesystem if it supports
  74. * filehandle operations. The filesystem must not use the value '0' or '0xff' and may
  75. * only use the values 1 and 2 as defined below:
  76. * Current values:
  77. * 0 - The root, or export point, of the filesystem. fb_fileid is 0 bytes.
  78. * 1 - 32bit inode number, 32 bit generation number.
  79. * 2 - 32bit inode number, 32 bit generation number, 32 bit parent directory inode number.
  80. *
  81. */
  82. struct nfs_fhbase_new {
  83. __u8 fb_version; /* == 1, even => nfs_fhbase_old */
  84. __u8 fb_auth_type;
  85. __u8 fb_fsid_type;
  86. __u8 fb_fileid_type;
  87. __u32 fb_auth[1];
  88. /* __u32 fb_fsid[0]; floating */
  89. /* __u32 fb_fileid[0]; floating */
  90. };
  91. struct knfsd_fh {
  92. unsigned int fh_size; /* significant for NFSv3.
  93. * Points to the current size while building
  94. * a new file handle
  95. */
  96. union {
  97. struct nfs_fhbase_old fh_old;
  98. __u32 fh_pad[NFS4_FHSIZE/4];
  99. struct nfs_fhbase_new fh_new;
  100. } fh_base;
  101. };
  102. #define ofh_dcookie fh_base.fh_old.fb_dcookie
  103. #define ofh_ino fh_base.fh_old.fb_ino
  104. #define ofh_dirino fh_base.fh_old.fb_dirino
  105. #define ofh_dev fh_base.fh_old.fb_dev
  106. #define ofh_xdev fh_base.fh_old.fb_xdev
  107. #define ofh_xino fh_base.fh_old.fb_xino
  108. #define ofh_generation fh_base.fh_old.fb_generation
  109. #define fh_version fh_base.fh_new.fb_version
  110. #define fh_fsid_type fh_base.fh_new.fb_fsid_type
  111. #define fh_auth_type fh_base.fh_new.fb_auth_type
  112. #define fh_fileid_type fh_base.fh_new.fb_fileid_type
  113. #define fh_auth fh_base.fh_new.fb_auth
  114. #define fh_fsid fh_base.fh_new.fb_auth
  115. #ifdef __KERNEL__
  116. static inline __u32 ino_t_to_u32(ino_t ino)
  117. {
  118. return (__u32) ino;
  119. }
  120. static inline ino_t u32_to_ino_t(__u32 uino)
  121. {
  122. return (ino_t) uino;
  123. }
  124. /*
  125. * This is the internal representation of an NFS handle used in knfsd.
  126. * pre_mtime/post_version will be used to support wcc_attr's in NFSv3.
  127. */
  128. typedef struct svc_fh {
  129. struct knfsd_fh fh_handle; /* FH data */
  130. struct dentry * fh_dentry; /* validated dentry */
  131. struct svc_export * fh_export; /* export pointer */
  132. int fh_maxsize; /* max size for fh_handle */
  133. unsigned char fh_locked; /* inode locked by us */
  134. #ifdef CONFIG_NFSD_V3
  135. unsigned char fh_post_saved; /* post-op attrs saved */
  136. unsigned char fh_pre_saved; /* pre-op attrs saved */
  137. /* Pre-op attributes saved during fh_lock */
  138. __u64 fh_pre_size; /* size before operation */
  139. struct timespec fh_pre_mtime; /* mtime before oper */
  140. struct timespec fh_pre_ctime; /* ctime before oper */
  141. /* Post-op attributes saved in fh_unlock */
  142. struct kstat fh_post_attr; /* full attrs after operation */
  143. #endif /* CONFIG_NFSD_V3 */
  144. } svc_fh;
  145. enum nfsd_fsid {
  146. FSID_DEV = 0,
  147. FSID_NUM,
  148. FSID_MAJOR_MINOR,
  149. FSID_ENCODE_DEV,
  150. FSID_UUID4_INUM,
  151. FSID_UUID8,
  152. FSID_UUID16,
  153. FSID_UUID16_INUM,
  154. };
  155. enum fsid_source {
  156. FSIDSOURCE_DEV,
  157. FSIDSOURCE_FSID,
  158. FSIDSOURCE_UUID,
  159. };
  160. extern enum fsid_source fsid_source(struct svc_fh *fhp);
  161. /* This might look a little large to "inline" but in all calls except
  162. * one, 'vers' is constant so moste of the function disappears.
  163. */
  164. static inline void mk_fsid(int vers, u32 *fsidv, dev_t dev, ino_t ino,
  165. u32 fsid, unsigned char *uuid)
  166. {
  167. u32 *up;
  168. switch(vers) {
  169. case FSID_DEV:
  170. fsidv[0] = htonl((MAJOR(dev)<<16) |
  171. MINOR(dev));
  172. fsidv[1] = ino_t_to_u32(ino);
  173. break;
  174. case FSID_NUM:
  175. fsidv[0] = fsid;
  176. break;
  177. case FSID_MAJOR_MINOR:
  178. fsidv[0] = htonl(MAJOR(dev));
  179. fsidv[1] = htonl(MINOR(dev));
  180. fsidv[2] = ino_t_to_u32(ino);
  181. break;
  182. case FSID_ENCODE_DEV:
  183. fsidv[0] = new_encode_dev(dev);
  184. fsidv[1] = ino_t_to_u32(ino);
  185. break;
  186. case FSID_UUID4_INUM:
  187. /* 4 byte fsid and inode number */
  188. up = (u32*)uuid;
  189. fsidv[0] = ino_t_to_u32(ino);
  190. fsidv[1] = up[0] ^ up[1] ^ up[2] ^ up[3];
  191. break;
  192. case FSID_UUID8:
  193. /* 8 byte fsid */
  194. up = (u32*)uuid;
  195. fsidv[0] = up[0] ^ up[2];
  196. fsidv[1] = up[1] ^ up[3];
  197. break;
  198. case FSID_UUID16:
  199. /* 16 byte fsid - NFSv3+ only */
  200. memcpy(fsidv, uuid, 16);
  201. break;
  202. case FSID_UUID16_INUM:
  203. /* 8 byte inode and 16 byte fsid */
  204. *(u64*)fsidv = (u64)ino;
  205. memcpy(fsidv+2, uuid, 16);
  206. break;
  207. default: BUG();
  208. }
  209. }
  210. static inline int key_len(int type)
  211. {
  212. switch(type) {
  213. case FSID_DEV: return 8;
  214. case FSID_NUM: return 4;
  215. case FSID_MAJOR_MINOR: return 12;
  216. case FSID_ENCODE_DEV: return 8;
  217. case FSID_UUID4_INUM: return 8;
  218. case FSID_UUID8: return 8;
  219. case FSID_UUID16: return 16;
  220. case FSID_UUID16_INUM: return 24;
  221. default: return 0;
  222. }
  223. }
  224. /*
  225. * Shorthand for dprintk()'s
  226. */
  227. extern char * SVCFH_fmt(struct svc_fh *fhp);
  228. /*
  229. * Function prototypes
  230. */
  231. __be32 fh_verify(struct svc_rqst *, struct svc_fh *, int, int);
  232. __be32 fh_compose(struct svc_fh *, struct svc_export *, struct dentry *, struct svc_fh *);
  233. __be32 fh_update(struct svc_fh *);
  234. void fh_put(struct svc_fh *);
  235. static __inline__ struct svc_fh *
  236. fh_copy(struct svc_fh *dst, struct svc_fh *src)
  237. {
  238. WARN_ON(src->fh_dentry || src->fh_locked);
  239. *dst = *src;
  240. return dst;
  241. }
  242. static __inline__ struct svc_fh *
  243. fh_init(struct svc_fh *fhp, int maxsize)
  244. {
  245. memset(fhp, 0, sizeof(*fhp));
  246. fhp->fh_maxsize = maxsize;
  247. return fhp;
  248. }
  249. #ifdef CONFIG_NFSD_V3
  250. /*
  251. * Fill in the pre_op attr for the wcc data
  252. */
  253. static inline void
  254. fill_pre_wcc(struct svc_fh *fhp)
  255. {
  256. struct inode *inode;
  257. inode = fhp->fh_dentry->d_inode;
  258. if (!fhp->fh_pre_saved) {
  259. fhp->fh_pre_mtime = inode->i_mtime;
  260. fhp->fh_pre_ctime = inode->i_ctime;
  261. fhp->fh_pre_size = inode->i_size;
  262. fhp->fh_pre_saved = 1;
  263. }
  264. }
  265. extern void fill_post_wcc(struct svc_fh *);
  266. #else
  267. #define fill_pre_wcc(ignored)
  268. #define fill_post_wcc(notused)
  269. #endif /* CONFIG_NFSD_V3 */
  270. /*
  271. * Lock a file handle/inode
  272. * NOTE: both fh_lock and fh_unlock are done "by hand" in
  273. * vfs.c:nfsd_rename as it needs to grab 2 i_mutex's at once
  274. * so, any changes here should be reflected there.
  275. */
  276. static inline void
  277. fh_lock_nested(struct svc_fh *fhp, unsigned int subclass)
  278. {
  279. struct dentry *dentry = fhp->fh_dentry;
  280. struct inode *inode;
  281. dfprintk(FILEOP, "nfsd: fh_lock(%s) locked = %d\n",
  282. SVCFH_fmt(fhp), fhp->fh_locked);
  283. BUG_ON(!dentry);
  284. if (fhp->fh_locked) {
  285. printk(KERN_WARNING "fh_lock: %s/%s already locked!\n",
  286. dentry->d_parent->d_name.name, dentry->d_name.name);
  287. return;
  288. }
  289. inode = dentry->d_inode;
  290. mutex_lock_nested(&inode->i_mutex, subclass);
  291. fill_pre_wcc(fhp);
  292. fhp->fh_locked = 1;
  293. }
  294. static inline void
  295. fh_lock(struct svc_fh *fhp)
  296. {
  297. fh_lock_nested(fhp, I_MUTEX_NORMAL);
  298. }
  299. /*
  300. * Unlock a file handle/inode
  301. */
  302. static inline void
  303. fh_unlock(struct svc_fh *fhp)
  304. {
  305. BUG_ON(!fhp->fh_dentry);
  306. if (fhp->fh_locked) {
  307. fill_post_wcc(fhp);
  308. mutex_unlock(&fhp->fh_dentry->d_inode->i_mutex);
  309. fhp->fh_locked = 0;
  310. }
  311. }
  312. #endif /* __KERNEL__ */
  313. #endif /* _LINUX_NFSD_FH_H */