nfsfh.h 9.6 KB

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