export.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. result->d_op = &ocfs2_dentry_ops;
  123. else
  124. mlog_errno(PTR_ERR(result));
  125. bail:
  126. mlog_exit_ptr(result);
  127. return result;
  128. }
  129. static struct dentry *ocfs2_get_parent(struct dentry *child)
  130. {
  131. int status;
  132. u64 blkno;
  133. struct dentry *parent;
  134. struct inode *dir = child->d_inode;
  135. mlog_entry("(0x%p, '%.*s')\n", child,
  136. child->d_name.len, child->d_name.name);
  137. mlog(0, "find parent of directory %llu\n",
  138. (unsigned long long)OCFS2_I(dir)->ip_blkno);
  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 bail;
  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. parent = d_obtain_alias(ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0));
  152. if (!IS_ERR(parent))
  153. parent->d_op = &ocfs2_dentry_ops;
  154. bail_unlock:
  155. ocfs2_inode_unlock(dir, 0);
  156. bail:
  157. mlog_exit_ptr(parent);
  158. return parent;
  159. }
  160. static int ocfs2_encode_fh(struct dentry *dentry, u32 *fh_in, int *max_len,
  161. int connectable)
  162. {
  163. struct inode *inode = dentry->d_inode;
  164. int len = *max_len;
  165. int type = 1;
  166. u64 blkno;
  167. u32 generation;
  168. __le32 *fh = (__force __le32 *) fh_in;
  169. mlog_entry("(0x%p, '%.*s', 0x%p, %d, %d)\n", dentry,
  170. dentry->d_name.len, dentry->d_name.name,
  171. fh, len, connectable);
  172. if (len < 3 || (connectable && len < 6)) {
  173. mlog(ML_ERROR, "fh buffer is too small for encoding\n");
  174. type = 255;
  175. goto bail;
  176. }
  177. blkno = OCFS2_I(inode)->ip_blkno;
  178. generation = inode->i_generation;
  179. mlog(0, "Encoding fh: blkno: %llu, generation: %u\n",
  180. (unsigned long long)blkno, generation);
  181. len = 3;
  182. fh[0] = cpu_to_le32((u32)(blkno >> 32));
  183. fh[1] = cpu_to_le32((u32)(blkno & 0xffffffff));
  184. fh[2] = cpu_to_le32(generation);
  185. if (connectable && !S_ISDIR(inode->i_mode)) {
  186. struct inode *parent;
  187. spin_lock(&dentry->d_lock);
  188. parent = dentry->d_parent->d_inode;
  189. blkno = OCFS2_I(parent)->ip_blkno;
  190. generation = parent->i_generation;
  191. fh[3] = cpu_to_le32((u32)(blkno >> 32));
  192. fh[4] = cpu_to_le32((u32)(blkno & 0xffffffff));
  193. fh[5] = cpu_to_le32(generation);
  194. spin_unlock(&dentry->d_lock);
  195. len = 6;
  196. type = 2;
  197. mlog(0, "Encoding parent: blkno: %llu, generation: %u\n",
  198. (unsigned long long)blkno, generation);
  199. }
  200. *max_len = len;
  201. bail:
  202. mlog_exit(type);
  203. return type;
  204. }
  205. static struct dentry *ocfs2_fh_to_dentry(struct super_block *sb,
  206. struct fid *fid, int fh_len, int fh_type)
  207. {
  208. struct ocfs2_inode_handle handle;
  209. if (fh_len < 3 || fh_type > 2)
  210. return NULL;
  211. handle.ih_blkno = (u64)le32_to_cpu(fid->raw[0]) << 32;
  212. handle.ih_blkno |= (u64)le32_to_cpu(fid->raw[1]);
  213. handle.ih_generation = le32_to_cpu(fid->raw[2]);
  214. return ocfs2_get_dentry(sb, &handle);
  215. }
  216. static struct dentry *ocfs2_fh_to_parent(struct super_block *sb,
  217. struct fid *fid, int fh_len, int fh_type)
  218. {
  219. struct ocfs2_inode_handle parent;
  220. if (fh_type != 2 || fh_len < 6)
  221. return NULL;
  222. parent.ih_blkno = (u64)le32_to_cpu(fid->raw[3]) << 32;
  223. parent.ih_blkno |= (u64)le32_to_cpu(fid->raw[4]);
  224. parent.ih_generation = le32_to_cpu(fid->raw[5]);
  225. return ocfs2_get_dentry(sb, &parent);
  226. }
  227. const struct export_operations ocfs2_export_ops = {
  228. .encode_fh = ocfs2_encode_fh,
  229. .fh_to_dentry = ocfs2_fh_to_dentry,
  230. .fh_to_parent = ocfs2_fh_to_parent,
  231. .get_parent = ocfs2_get_parent,
  232. };