nfsfh.h 10 KB

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