nfsfh.h 9.4 KB

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