xfs_export.c 4.6 KB

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