export.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 = ERR_CAST(inode);
  106. goto bail;
  107. }
  108. check_gen:
  109. if (handle->ih_generation != inode->i_generation) {
  110. trace_ocfs2_get_dentry_generation((unsigned long long)blkno,
  111. handle->ih_generation,
  112. inode->i_generation);
  113. iput(inode);
  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. int set;
  131. trace_ocfs2_get_parent(child, child->d_name.len, child->d_name.name,
  132. (unsigned long long)OCFS2_I(dir)->ip_blkno);
  133. status = ocfs2_nfs_sync_lock(OCFS2_SB(dir->i_sb), 1);
  134. if (status < 0) {
  135. mlog(ML_ERROR, "getting nfs sync lock(EX) failed %d\n", status);
  136. parent = ERR_PTR(status);
  137. goto bail;
  138. }
  139. status = ocfs2_inode_lock(dir, NULL, 0);
  140. if (status < 0) {
  141. if (status != -ENOENT)
  142. mlog_errno(status);
  143. parent = ERR_PTR(status);
  144. goto unlock_nfs_sync;
  145. }
  146. status = ocfs2_lookup_ino_from_name(dir, "..", 2, &blkno);
  147. if (status < 0) {
  148. parent = ERR_PTR(-ENOENT);
  149. goto bail_unlock;
  150. }
  151. status = ocfs2_test_inode_bit(OCFS2_SB(dir->i_sb), blkno, &set);
  152. if (status < 0) {
  153. if (status == -EINVAL) {
  154. status = -ESTALE;
  155. } else
  156. mlog(ML_ERROR, "test inode bit failed %d\n", status);
  157. parent = ERR_PTR(status);
  158. goto bail_unlock;
  159. }
  160. trace_ocfs2_get_dentry_test_bit(status, set);
  161. if (!set) {
  162. status = -ESTALE;
  163. parent = ERR_PTR(status);
  164. goto bail_unlock;
  165. }
  166. parent = d_obtain_alias(ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0));
  167. bail_unlock:
  168. ocfs2_inode_unlock(dir, 0);
  169. unlock_nfs_sync:
  170. ocfs2_nfs_sync_unlock(OCFS2_SB(dir->i_sb), 1);
  171. bail:
  172. trace_ocfs2_get_parent_end(parent);
  173. return parent;
  174. }
  175. static int ocfs2_encode_fh(struct inode *inode, u32 *fh_in, int *max_len,
  176. struct inode *parent)
  177. {
  178. int len = *max_len;
  179. int type = 1;
  180. u64 blkno;
  181. u32 generation;
  182. __le32 *fh = (__force __le32 *) fh_in;
  183. #ifdef TRACE_HOOKS_ARE_NOT_BRAINDEAD_IN_YOUR_OPINION
  184. #error "You go ahead and fix that mess, then. Somehow"
  185. trace_ocfs2_encode_fh_begin(dentry, dentry->d_name.len,
  186. dentry->d_name.name,
  187. fh, len, connectable);
  188. #endif
  189. if (parent && (len < 6)) {
  190. *max_len = 6;
  191. type = FILEID_INVALID;
  192. goto bail;
  193. } else if (len < 3) {
  194. *max_len = 3;
  195. type = FILEID_INVALID;
  196. goto bail;
  197. }
  198. blkno = OCFS2_I(inode)->ip_blkno;
  199. generation = inode->i_generation;
  200. trace_ocfs2_encode_fh_self((unsigned long long)blkno, generation);
  201. len = 3;
  202. fh[0] = cpu_to_le32((u32)(blkno >> 32));
  203. fh[1] = cpu_to_le32((u32)(blkno & 0xffffffff));
  204. fh[2] = cpu_to_le32(generation);
  205. if (parent) {
  206. blkno = OCFS2_I(parent)->ip_blkno;
  207. generation = parent->i_generation;
  208. fh[3] = cpu_to_le32((u32)(blkno >> 32));
  209. fh[4] = cpu_to_le32((u32)(blkno & 0xffffffff));
  210. fh[5] = cpu_to_le32(generation);
  211. len = 6;
  212. type = 2;
  213. trace_ocfs2_encode_fh_parent((unsigned long long)blkno,
  214. generation);
  215. }
  216. *max_len = len;
  217. bail:
  218. trace_ocfs2_encode_fh_type(type);
  219. return type;
  220. }
  221. static struct dentry *ocfs2_fh_to_dentry(struct super_block *sb,
  222. struct fid *fid, int fh_len, int fh_type)
  223. {
  224. struct ocfs2_inode_handle handle;
  225. if (fh_len < 3 || fh_type > 2)
  226. return NULL;
  227. handle.ih_blkno = (u64)le32_to_cpu(fid->raw[0]) << 32;
  228. handle.ih_blkno |= (u64)le32_to_cpu(fid->raw[1]);
  229. handle.ih_generation = le32_to_cpu(fid->raw[2]);
  230. return ocfs2_get_dentry(sb, &handle);
  231. }
  232. static struct dentry *ocfs2_fh_to_parent(struct super_block *sb,
  233. struct fid *fid, int fh_len, int fh_type)
  234. {
  235. struct ocfs2_inode_handle parent;
  236. if (fh_type != 2 || fh_len < 6)
  237. return NULL;
  238. parent.ih_blkno = (u64)le32_to_cpu(fid->raw[3]) << 32;
  239. parent.ih_blkno |= (u64)le32_to_cpu(fid->raw[4]);
  240. parent.ih_generation = le32_to_cpu(fid->raw[5]);
  241. return ocfs2_get_dentry(sb, &parent);
  242. }
  243. const struct export_operations ocfs2_export_ops = {
  244. .encode_fh = ocfs2_encode_fh,
  245. .fh_to_dentry = ocfs2_fh_to_dentry,
  246. .fh_to_parent = ocfs2_fh_to_parent,
  247. .get_parent = ocfs2_get_parent,
  248. };