export.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * export.c
  5. *
  6. * Functions to facilitate NFS exporting
  7. *
  8. * Copyright (C) 2002, 2005 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <cluster/masklog.h>
  28. #include "ocfs2.h"
  29. #include "alloc.h"
  30. #include "dir.h"
  31. #include "dlmglue.h"
  32. #include "dcache.h"
  33. #include "export.h"
  34. #include "inode.h"
  35. #include "buffer_head_io.h"
  36. #include "suballoc.h"
  37. #include "ocfs2_trace.h"
  38. struct ocfs2_inode_handle
  39. {
  40. u64 ih_blkno;
  41. u32 ih_generation;
  42. };
  43. static struct dentry *ocfs2_get_dentry(struct super_block *sb,
  44. struct ocfs2_inode_handle *handle)
  45. {
  46. struct inode *inode;
  47. struct ocfs2_super *osb = OCFS2_SB(sb);
  48. u64 blkno = handle->ih_blkno;
  49. int status, set;
  50. struct dentry *result;
  51. trace_ocfs2_get_dentry_begin(sb, handle, (unsigned long long)blkno);
  52. if (blkno == 0) {
  53. result = ERR_PTR(-ESTALE);
  54. goto bail;
  55. }
  56. inode = ocfs2_ilookup(sb, blkno);
  57. /*
  58. * If the inode exists in memory, we only need to check it's
  59. * generation number
  60. */
  61. if (inode)
  62. goto check_gen;
  63. /*
  64. * This will synchronize us against ocfs2_delete_inode() on
  65. * all nodes
  66. */
  67. status = ocfs2_nfs_sync_lock(osb, 1);
  68. if (status < 0) {
  69. mlog(ML_ERROR, "getting nfs sync lock(EX) failed %d\n", status);
  70. goto check_err;
  71. }
  72. status = ocfs2_test_inode_bit(osb, blkno, &set);
  73. if (status < 0) {
  74. if (status == -EINVAL) {
  75. /*
  76. * The blkno NFS gave us doesn't even show up
  77. * as an inode, we return -ESTALE to be
  78. * nice
  79. */
  80. status = -ESTALE;
  81. } else
  82. mlog(ML_ERROR, "test inode bit failed %d\n", status);
  83. goto unlock_nfs_sync;
  84. }
  85. trace_ocfs2_get_dentry_test_bit(status, set);
  86. /* If the inode allocator bit is clear, this inode must be stale */
  87. if (!set) {
  88. status = -ESTALE;
  89. goto unlock_nfs_sync;
  90. }
  91. inode = ocfs2_iget(osb, blkno, 0, 0);
  92. unlock_nfs_sync:
  93. ocfs2_nfs_sync_unlock(osb, 1);
  94. check_err:
  95. if (status < 0) {
  96. if (status == -ESTALE) {
  97. trace_ocfs2_get_dentry_stale((unsigned long long)blkno,
  98. handle->ih_generation);
  99. }
  100. result = ERR_PTR(status);
  101. goto bail;
  102. }
  103. if (IS_ERR(inode)) {
  104. mlog_errno(PTR_ERR(inode));
  105. result = (void *)inode;
  106. goto bail;
  107. }
  108. check_gen:
  109. if (handle->ih_generation != inode->i_generation) {
  110. iput(inode);
  111. trace_ocfs2_get_dentry_generation((unsigned long long)blkno,
  112. handle->ih_generation,
  113. inode->i_generation);
  114. result = ERR_PTR(-ESTALE);
  115. goto bail;
  116. }
  117. result = d_obtain_alias(inode);
  118. if (IS_ERR(result))
  119. mlog_errno(PTR_ERR(result));
  120. bail:
  121. trace_ocfs2_get_dentry_end(result);
  122. return result;
  123. }
  124. static struct dentry *ocfs2_get_parent(struct dentry *child)
  125. {
  126. int status;
  127. u64 blkno;
  128. struct dentry *parent;
  129. struct inode *dir = d_inode(child);
  130. trace_ocfs2_get_parent(child, child->d_name.len, child->d_name.name,
  131. (unsigned long long)OCFS2_I(dir)->ip_blkno);
  132. status = ocfs2_inode_lock(dir, NULL, 0);
  133. if (status < 0) {
  134. if (status != -ENOENT)
  135. mlog_errno(status);
  136. parent = ERR_PTR(status);
  137. goto bail;
  138. }
  139. status = ocfs2_lookup_ino_from_name(dir, "..", 2, &blkno);
  140. if (status < 0) {
  141. parent = ERR_PTR(-ENOENT);
  142. goto bail_unlock;
  143. }
  144. parent = d_obtain_alias(ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0));
  145. bail_unlock:
  146. ocfs2_inode_unlock(dir, 0);
  147. bail:
  148. trace_ocfs2_get_parent_end(parent);
  149. return parent;
  150. }
  151. static int ocfs2_encode_fh(struct inode *inode, u32 *fh_in, int *max_len,
  152. struct inode *parent)
  153. {
  154. int len = *max_len;
  155. int type = 1;
  156. u64 blkno;
  157. u32 generation;
  158. __le32 *fh = (__force __le32 *) fh_in;
  159. #ifdef TRACE_HOOKS_ARE_NOT_BRAINDEAD_IN_YOUR_OPINION
  160. #error "You go ahead and fix that mess, then. Somehow"
  161. trace_ocfs2_encode_fh_begin(dentry, dentry->d_name.len,
  162. dentry->d_name.name,
  163. fh, len, connectable);
  164. #endif
  165. if (parent && (len < 6)) {
  166. *max_len = 6;
  167. type = FILEID_INVALID;
  168. goto bail;
  169. } else if (len < 3) {
  170. *max_len = 3;
  171. type = FILEID_INVALID;
  172. goto bail;
  173. }
  174. blkno = OCFS2_I(inode)->ip_blkno;
  175. generation = inode->i_generation;
  176. trace_ocfs2_encode_fh_self((unsigned long long)blkno, generation);
  177. len = 3;
  178. fh[0] = cpu_to_le32((u32)(blkno >> 32));
  179. fh[1] = cpu_to_le32((u32)(blkno & 0xffffffff));
  180. fh[2] = cpu_to_le32(generation);
  181. if (parent) {
  182. blkno = OCFS2_I(parent)->ip_blkno;
  183. generation = parent->i_generation;
  184. fh[3] = cpu_to_le32((u32)(blkno >> 32));
  185. fh[4] = cpu_to_le32((u32)(blkno & 0xffffffff));
  186. fh[5] = cpu_to_le32(generation);
  187. len = 6;
  188. type = 2;
  189. trace_ocfs2_encode_fh_parent((unsigned long long)blkno,
  190. generation);
  191. }
  192. *max_len = len;
  193. bail:
  194. trace_ocfs2_encode_fh_type(type);
  195. return type;
  196. }
  197. static struct dentry *ocfs2_fh_to_dentry(struct super_block *sb,
  198. struct fid *fid, int fh_len, int fh_type)
  199. {
  200. struct ocfs2_inode_handle handle;
  201. if (fh_len < 3 || fh_type > 2)
  202. return NULL;
  203. handle.ih_blkno = (u64)le32_to_cpu(fid->raw[0]) << 32;
  204. handle.ih_blkno |= (u64)le32_to_cpu(fid->raw[1]);
  205. handle.ih_generation = le32_to_cpu(fid->raw[2]);
  206. return ocfs2_get_dentry(sb, &handle);
  207. }
  208. static struct dentry *ocfs2_fh_to_parent(struct super_block *sb,
  209. struct fid *fid, int fh_len, int fh_type)
  210. {
  211. struct ocfs2_inode_handle parent;
  212. if (fh_type != 2 || fh_len < 6)
  213. return NULL;
  214. parent.ih_blkno = (u64)le32_to_cpu(fid->raw[3]) << 32;
  215. parent.ih_blkno |= (u64)le32_to_cpu(fid->raw[4]);
  216. parent.ih_generation = le32_to_cpu(fid->raw[5]);
  217. return ocfs2_get_dentry(sb, &parent);
  218. }
  219. const struct export_operations ocfs2_export_ops = {
  220. .encode_fh = ocfs2_encode_fh,
  221. .fh_to_dentry = ocfs2_fh_to_dentry,
  222. .fh_to_parent = ocfs2_fh_to_parent,
  223. .get_parent = ocfs2_get_parent,
  224. };