export.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #define MLOG_MASK_PREFIX ML_EXPORT
  28. #include <cluster/masklog.h>
  29. #include "ocfs2.h"
  30. #include "alloc.h"
  31. #include "dir.h"
  32. #include "dlmglue.h"
  33. #include "dcache.h"
  34. #include "export.h"
  35. #include "inode.h"
  36. #include "buffer_head_io.h"
  37. #include "suballoc.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. mlog_entry("(0x%p, 0x%p)\n", sb, handle);
  52. if (blkno == 0) {
  53. mlog(0, "nfs wants inode with blkno: 0\n");
  54. result = ERR_PTR(-ESTALE);
  55. goto bail;
  56. }
  57. inode = ocfs2_ilookup(sb, blkno);
  58. /*
  59. * If the inode exists in memory, we only need to check it's
  60. * generation number
  61. */
  62. if (inode)
  63. goto check_gen;
  64. /*
  65. * This will synchronize us against ocfs2_delete_inode() on
  66. * all nodes
  67. */
  68. status = ocfs2_nfs_sync_lock(osb, 1);
  69. if (status < 0) {
  70. mlog(ML_ERROR, "getting nfs sync lock(EX) failed %d\n", status);
  71. goto check_err;
  72. }
  73. status = ocfs2_test_inode_bit(osb, blkno, &set);
  74. if (status < 0) {
  75. if (status == -EINVAL) {
  76. /*
  77. * The blkno NFS gave us doesn't even show up
  78. * as an inode, we return -ESTALE to be
  79. * nice
  80. */
  81. mlog(0, "test inode bit failed %d\n", status);
  82. status = -ESTALE;
  83. } else {
  84. mlog(ML_ERROR, "test inode bit failed %d\n", status);
  85. }
  86. goto unlock_nfs_sync;
  87. }
  88. /* If the inode allocator bit is clear, this inode must be stale */
  89. if (!set) {
  90. mlog(0, "inode %llu suballoc bit is clear\n",
  91. (unsigned long long)blkno);
  92. status = -ESTALE;
  93. goto unlock_nfs_sync;
  94. }
  95. inode = ocfs2_iget(osb, blkno, 0, 0);
  96. unlock_nfs_sync:
  97. ocfs2_nfs_sync_unlock(osb, 1);
  98. check_err:
  99. if (status < 0) {
  100. if (status == -ESTALE) {
  101. mlog(0, "stale inode ino: %llu generation: %u\n",
  102. (unsigned long long)blkno, handle->ih_generation);
  103. }
  104. result = ERR_PTR(status);
  105. goto bail;
  106. }
  107. if (IS_ERR(inode)) {
  108. mlog_errno(PTR_ERR(inode));
  109. result = (void *)inode;
  110. goto bail;
  111. }
  112. check_gen:
  113. if (handle->ih_generation != inode->i_generation) {
  114. iput(inode);
  115. mlog(0, "stale inode ino: %llu generation: %u\n",
  116. (unsigned long long)blkno, handle->ih_generation);
  117. result = ERR_PTR(-ESTALE);
  118. goto bail;
  119. }
  120. result = d_obtain_alias(inode);
  121. if (IS_ERR(result))
  122. mlog_errno(PTR_ERR(result));
  123. bail:
  124. mlog_exit_ptr(result);
  125. return result;
  126. }
  127. static struct dentry *ocfs2_get_parent(struct dentry *child)
  128. {
  129. int status;
  130. u64 blkno;
  131. struct dentry *parent;
  132. struct inode *dir = child->d_inode;
  133. mlog_entry("(0x%p, '%.*s')\n", child,
  134. child->d_name.len, child->d_name.name);
  135. mlog(0, "find parent of directory %llu\n",
  136. (unsigned long long)OCFS2_I(dir)->ip_blkno);
  137. status = ocfs2_inode_lock(dir, NULL, 0);
  138. if (status < 0) {
  139. if (status != -ENOENT)
  140. mlog_errno(status);
  141. parent = ERR_PTR(status);
  142. goto bail;
  143. }
  144. status = ocfs2_lookup_ino_from_name(dir, "..", 2, &blkno);
  145. if (status < 0) {
  146. parent = ERR_PTR(-ENOENT);
  147. goto bail_unlock;
  148. }
  149. parent = d_obtain_alias(ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0));
  150. bail_unlock:
  151. ocfs2_inode_unlock(dir, 0);
  152. bail:
  153. mlog_exit_ptr(parent);
  154. return parent;
  155. }
  156. static int ocfs2_encode_fh(struct dentry *dentry, u32 *fh_in, int *max_len,
  157. int connectable)
  158. {
  159. struct inode *inode = dentry->d_inode;
  160. int len = *max_len;
  161. int type = 1;
  162. u64 blkno;
  163. u32 generation;
  164. __le32 *fh = (__force __le32 *) fh_in;
  165. mlog_entry("(0x%p, '%.*s', 0x%p, %d, %d)\n", dentry,
  166. dentry->d_name.len, dentry->d_name.name,
  167. fh, len, connectable);
  168. if (len < 3 || (connectable && len < 6)) {
  169. mlog(ML_ERROR, "fh buffer is too small for encoding\n");
  170. type = 255;
  171. goto bail;
  172. }
  173. blkno = OCFS2_I(inode)->ip_blkno;
  174. generation = inode->i_generation;
  175. mlog(0, "Encoding fh: blkno: %llu, generation: %u\n",
  176. (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 (connectable && !S_ISDIR(inode->i_mode)) {
  182. struct inode *parent;
  183. spin_lock(&dentry->d_lock);
  184. parent = dentry->d_parent->d_inode;
  185. blkno = OCFS2_I(parent)->ip_blkno;
  186. generation = parent->i_generation;
  187. fh[3] = cpu_to_le32((u32)(blkno >> 32));
  188. fh[4] = cpu_to_le32((u32)(blkno & 0xffffffff));
  189. fh[5] = cpu_to_le32(generation);
  190. spin_unlock(&dentry->d_lock);
  191. len = 6;
  192. type = 2;
  193. mlog(0, "Encoding parent: blkno: %llu, generation: %u\n",
  194. (unsigned long long)blkno, generation);
  195. }
  196. *max_len = len;
  197. bail:
  198. mlog_exit(type);
  199. return type;
  200. }
  201. static struct dentry *ocfs2_fh_to_dentry(struct super_block *sb,
  202. struct fid *fid, int fh_len, int fh_type)
  203. {
  204. struct ocfs2_inode_handle handle;
  205. if (fh_len < 3 || fh_type > 2)
  206. return NULL;
  207. handle.ih_blkno = (u64)le32_to_cpu(fid->raw[0]) << 32;
  208. handle.ih_blkno |= (u64)le32_to_cpu(fid->raw[1]);
  209. handle.ih_generation = le32_to_cpu(fid->raw[2]);
  210. return ocfs2_get_dentry(sb, &handle);
  211. }
  212. static struct dentry *ocfs2_fh_to_parent(struct super_block *sb,
  213. struct fid *fid, int fh_len, int fh_type)
  214. {
  215. struct ocfs2_inode_handle parent;
  216. if (fh_type != 2 || fh_len < 6)
  217. return NULL;
  218. parent.ih_blkno = (u64)le32_to_cpu(fid->raw[3]) << 32;
  219. parent.ih_blkno |= (u64)le32_to_cpu(fid->raw[4]);
  220. parent.ih_generation = le32_to_cpu(fid->raw[5]);
  221. return ocfs2_get_dentry(sb, &parent);
  222. }
  223. const struct export_operations ocfs2_export_ops = {
  224. .encode_fh = ocfs2_encode_fh,
  225. .fh_to_dentry = ocfs2_fh_to_dentry,
  226. .fh_to_parent = ocfs2_fh_to_parent,
  227. .get_parent = ocfs2_get_parent,
  228. };