xfs_export.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2004-2005 Silicon Graphics, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it would be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. *
  12. * Further, this software is distributed without any warranty that it is
  13. * free of the rightful claim of any third person regarding infringement
  14. * or the like. Any license provided herein, whether implied or
  15. * otherwise, applies only to this software file. Patent licenses, if
  16. * any, provided herein do not apply to combinations of this program with
  17. * other software, or any other product whatsoever.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston MA 02111-1307, USA.
  22. *
  23. * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
  24. * Mountain View, CA 94043, or:
  25. *
  26. * http://www.sgi.com
  27. *
  28. * For further information regarding this notice, see:
  29. *
  30. * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
  31. */
  32. #include "xfs.h"
  33. #include "xfs_types.h"
  34. #include "xfs_dmapi.h"
  35. #include "xfs_log.h"
  36. #include "xfs_trans.h"
  37. #include "xfs_sb.h"
  38. #include "xfs_dir.h"
  39. #include "xfs_mount.h"
  40. #include "xfs_export.h"
  41. /*
  42. * XFS encode and decodes the fileid portion of NFS filehandles
  43. * itself instead of letting the generic NFS code do it. This
  44. * allows filesystems with 64 bit inode numbers to be exported.
  45. *
  46. * Note that a side effect is that xfs_vget() won't be passed a
  47. * zero inode/generation pair under normal circumstances. As
  48. * however a malicious client could send us such data, the check
  49. * remains in that code.
  50. */
  51. STATIC struct dentry *
  52. linvfs_decode_fh(
  53. struct super_block *sb,
  54. __u32 *fh,
  55. int fh_len,
  56. int fileid_type,
  57. int (*acceptable)(
  58. void *context,
  59. struct dentry *de),
  60. void *context)
  61. {
  62. xfs_fid2_t ifid;
  63. xfs_fid2_t pfid;
  64. void *parent = NULL;
  65. int is64 = 0;
  66. __u32 *p = fh;
  67. #if XFS_BIG_INUMS
  68. is64 = (fileid_type & XFS_FILEID_TYPE_64FLAG);
  69. fileid_type &= ~XFS_FILEID_TYPE_64FLAG;
  70. #endif
  71. /*
  72. * Note that we only accept fileids which are long enough
  73. * rather than allow the parent generation number to default
  74. * to zero. XFS considers zero a valid generation number not
  75. * an invalid/wildcard value. There's little point printk'ing
  76. * a warning here as we don't have the client information
  77. * which would make such a warning useful.
  78. */
  79. if (fileid_type > 2 ||
  80. fh_len < xfs_fileid_length((fileid_type == 2), is64))
  81. return NULL;
  82. p = xfs_fileid_decode_fid2(p, &ifid, is64);
  83. if (fileid_type == 2) {
  84. p = xfs_fileid_decode_fid2(p, &pfid, is64);
  85. parent = &pfid;
  86. }
  87. fh = (__u32 *)&ifid;
  88. return find_exported_dentry(sb, fh, parent, acceptable, context);
  89. }
  90. STATIC int
  91. linvfs_encode_fh(
  92. struct dentry *dentry,
  93. __u32 *fh,
  94. int *max_len,
  95. int connectable)
  96. {
  97. struct inode *inode = dentry->d_inode;
  98. int type = 1;
  99. __u32 *p = fh;
  100. int len;
  101. int is64 = 0;
  102. #if XFS_BIG_INUMS
  103. vfs_t *vfs = LINVFS_GET_VFS(inode->i_sb);
  104. xfs_mount_t *mp = XFS_VFSTOM(vfs);
  105. if (!(mp->m_flags & XFS_MOUNT_32BITINOOPT)) {
  106. /* filesystem may contain 64bit inode numbers */
  107. is64 = XFS_FILEID_TYPE_64FLAG;
  108. }
  109. #endif
  110. /* Directories don't need their parent encoded, they have ".." */
  111. if (S_ISDIR(inode->i_mode))
  112. connectable = 0;
  113. /*
  114. * Only encode if there is enough space given. In practice
  115. * this means we can't export a filesystem with 64bit inodes
  116. * over NFSv2 with the subtree_check export option; the other
  117. * seven combinations work. The real answer is "don't use v2".
  118. */
  119. len = xfs_fileid_length(connectable, is64);
  120. if (*max_len < len)
  121. return 255;
  122. *max_len = len;
  123. p = xfs_fileid_encode_inode(p, inode, is64);
  124. if (connectable) {
  125. spin_lock(&dentry->d_lock);
  126. p = xfs_fileid_encode_inode(p, dentry->d_parent->d_inode, is64);
  127. spin_unlock(&dentry->d_lock);
  128. type = 2;
  129. }
  130. BUG_ON((p - fh) != len);
  131. return type | is64;
  132. }
  133. STATIC struct dentry *
  134. linvfs_get_dentry(
  135. struct super_block *sb,
  136. void *data)
  137. {
  138. vnode_t *vp;
  139. struct inode *inode;
  140. struct dentry *result;
  141. vfs_t *vfsp = LINVFS_GET_VFS(sb);
  142. int error;
  143. VFS_VGET(vfsp, &vp, (fid_t *)data, error);
  144. if (error || vp == NULL)
  145. return ERR_PTR(-ESTALE) ;
  146. inode = LINVFS_GET_IP(vp);
  147. result = d_alloc_anon(inode);
  148. if (!result) {
  149. iput(inode);
  150. return ERR_PTR(-ENOMEM);
  151. }
  152. return result;
  153. }
  154. STATIC struct dentry *
  155. linvfs_get_parent(
  156. struct dentry *child)
  157. {
  158. int error;
  159. vnode_t *vp, *cvp;
  160. struct dentry *parent;
  161. struct dentry dotdot;
  162. dotdot.d_name.name = "..";
  163. dotdot.d_name.len = 2;
  164. dotdot.d_inode = NULL;
  165. cvp = NULL;
  166. vp = LINVFS_GET_VP(child->d_inode);
  167. VOP_LOOKUP(vp, &dotdot, &cvp, 0, NULL, NULL, error);
  168. if (unlikely(error))
  169. return ERR_PTR(-error);
  170. parent = d_alloc_anon(LINVFS_GET_IP(cvp));
  171. if (unlikely(!parent)) {
  172. VN_RELE(cvp);
  173. return ERR_PTR(-ENOMEM);
  174. }
  175. return parent;
  176. }
  177. struct export_operations linvfs_export_ops = {
  178. .decode_fh = linvfs_decode_fh,
  179. .encode_fh = linvfs_encode_fh,
  180. .get_parent = linvfs_get_parent,
  181. .get_dentry = linvfs_get_dentry,
  182. };