xfs_export.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_mount.h"
  25. #include "xfs_export.h"
  26. static struct dentry dotdot = { .d_name.name = "..", .d_name.len = 2, };
  27. /*
  28. * XFS encodes and decodes the fileid portion of NFS filehandles
  29. * itself instead of letting the generic NFS code do it. This
  30. * allows filesystems with 64 bit inode numbers to be exported.
  31. *
  32. * Note that a side effect is that xfs_vget() won't be passed a
  33. * zero inode/generation pair under normal circumstances. As
  34. * however a malicious client could send us such data, the check
  35. * remains in that code.
  36. */
  37. STATIC struct dentry *
  38. xfs_fs_decode_fh(
  39. struct super_block *sb,
  40. __u32 *fh,
  41. int fh_len,
  42. int fileid_type,
  43. int (*acceptable)(
  44. void *context,
  45. struct dentry *de),
  46. void *context)
  47. {
  48. xfs_fid2_t ifid;
  49. xfs_fid2_t pfid;
  50. void *parent = NULL;
  51. int is64 = 0;
  52. __u32 *p = fh;
  53. #if XFS_BIG_INUMS
  54. is64 = (fileid_type & XFS_FILEID_TYPE_64FLAG);
  55. fileid_type &= ~XFS_FILEID_TYPE_64FLAG;
  56. #endif
  57. /*
  58. * Note that we only accept fileids which are long enough
  59. * rather than allow the parent generation number to default
  60. * to zero. XFS considers zero a valid generation number not
  61. * an invalid/wildcard value. There's little point printk'ing
  62. * a warning here as we don't have the client information
  63. * which would make such a warning useful.
  64. */
  65. if (fileid_type > 2 ||
  66. fh_len < xfs_fileid_length((fileid_type == 2), is64))
  67. return NULL;
  68. p = xfs_fileid_decode_fid2(p, &ifid, is64);
  69. if (fileid_type == 2) {
  70. p = xfs_fileid_decode_fid2(p, &pfid, is64);
  71. parent = &pfid;
  72. }
  73. fh = (__u32 *)&ifid;
  74. return sb->s_export_op->find_exported_dentry(sb, fh, parent, acceptable, context);
  75. }
  76. STATIC int
  77. xfs_fs_encode_fh(
  78. struct dentry *dentry,
  79. __u32 *fh,
  80. int *max_len,
  81. int connectable)
  82. {
  83. struct inode *inode = dentry->d_inode;
  84. int type = 1;
  85. __u32 *p = fh;
  86. int len;
  87. int is64 = 0;
  88. #if XFS_BIG_INUMS
  89. bhv_vfs_t *vfs = vfs_from_sb(inode->i_sb);
  90. if (!(vfs->vfs_flag & VFS_32BITINODES)) {
  91. /* filesystem may contain 64bit inode numbers */
  92. is64 = XFS_FILEID_TYPE_64FLAG;
  93. }
  94. #endif
  95. /* Directories don't need their parent encoded, they have ".." */
  96. if (S_ISDIR(inode->i_mode))
  97. connectable = 0;
  98. /*
  99. * Only encode if there is enough space given. In practice
  100. * this means we can't export a filesystem with 64bit inodes
  101. * over NFSv2 with the subtree_check export option; the other
  102. * seven combinations work. The real answer is "don't use v2".
  103. */
  104. len = xfs_fileid_length(connectable, is64);
  105. if (*max_len < len)
  106. return 255;
  107. *max_len = len;
  108. p = xfs_fileid_encode_inode(p, inode, is64);
  109. if (connectable) {
  110. spin_lock(&dentry->d_lock);
  111. p = xfs_fileid_encode_inode(p, dentry->d_parent->d_inode, is64);
  112. spin_unlock(&dentry->d_lock);
  113. type = 2;
  114. }
  115. BUG_ON((p - fh) != len);
  116. return type | is64;
  117. }
  118. STATIC struct dentry *
  119. xfs_fs_get_dentry(
  120. struct super_block *sb,
  121. void *data)
  122. {
  123. bhv_vnode_t *vp;
  124. struct inode *inode;
  125. struct dentry *result;
  126. bhv_vfs_t *vfsp = vfs_from_sb(sb);
  127. int error;
  128. error = bhv_vfs_vget(vfsp, &vp, (fid_t *)data);
  129. if (error || vp == NULL)
  130. return ERR_PTR(-ESTALE) ;
  131. inode = vn_to_inode(vp);
  132. result = d_alloc_anon(inode);
  133. if (!result) {
  134. iput(inode);
  135. return ERR_PTR(-ENOMEM);
  136. }
  137. return result;
  138. }
  139. STATIC struct dentry *
  140. xfs_fs_get_parent(
  141. struct dentry *child)
  142. {
  143. int error;
  144. bhv_vnode_t *vp, *cvp;
  145. struct dentry *parent;
  146. cvp = NULL;
  147. vp = vn_from_inode(child->d_inode);
  148. error = bhv_vop_lookup(vp, &dotdot, &cvp, 0, NULL, NULL);
  149. if (unlikely(error))
  150. return ERR_PTR(-error);
  151. parent = d_alloc_anon(vn_to_inode(cvp));
  152. if (unlikely(!parent)) {
  153. VN_RELE(cvp);
  154. return ERR_PTR(-ENOMEM);
  155. }
  156. return parent;
  157. }
  158. struct export_operations xfs_export_operations = {
  159. .decode_fh = xfs_fs_decode_fh,
  160. .encode_fh = xfs_fs_encode_fh,
  161. .get_parent = xfs_fs_get_parent,
  162. .get_dentry = xfs_fs_get_dentry,
  163. };