xfs_export.c 4.6 KB

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