export.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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", blkno);
  91. status = -ESTALE;
  92. goto unlock_nfs_sync;
  93. }
  94. inode = ocfs2_iget(osb, blkno, 0, 0);
  95. unlock_nfs_sync:
  96. ocfs2_nfs_sync_unlock(osb, 1);
  97. check_err:
  98. if (status < 0) {
  99. if (status == -ESTALE) {
  100. mlog(0, "stale inode ino: %llu generation: %u\n",
  101. blkno, handle->ih_generation);
  102. }
  103. result = ERR_PTR(status);
  104. goto bail;
  105. }
  106. if (IS_ERR(inode)) {
  107. mlog_errno(PTR_ERR(inode));
  108. result = (void *)inode;
  109. goto bail;
  110. }
  111. check_gen:
  112. if (handle->ih_generation != inode->i_generation) {
  113. iput(inode);
  114. mlog(0, "stale inode ino: %llu generation: %u\n", blkno,
  115. handle->ih_generation);
  116. result = ERR_PTR(-ESTALE);
  117. goto bail;
  118. }
  119. result = d_obtain_alias(inode);
  120. if (!IS_ERR(result))
  121. result->d_op = &ocfs2_dentry_ops;
  122. else
  123. mlog_errno(PTR_ERR(result));
  124. bail:
  125. mlog_exit_ptr(result);
  126. return result;
  127. }
  128. static struct dentry *ocfs2_get_parent(struct dentry *child)
  129. {
  130. int status;
  131. u64 blkno;
  132. struct dentry *parent;
  133. struct inode *dir = child->d_inode;
  134. mlog_entry("(0x%p, '%.*s')\n", child,
  135. child->d_name.len, child->d_name.name);
  136. mlog(0, "find parent of directory %llu\n",
  137. (unsigned long long)OCFS2_I(dir)->ip_blkno);
  138. status = ocfs2_inode_lock(dir, NULL, 0);
  139. if (status < 0) {
  140. if (status != -ENOENT)
  141. mlog_errno(status);
  142. parent = ERR_PTR(status);
  143. goto bail;
  144. }
  145. status = ocfs2_lookup_ino_from_name(dir, "..", 2, &blkno);
  146. if (status < 0) {
  147. parent = ERR_PTR(-ENOENT);
  148. goto bail_unlock;
  149. }
  150. parent = d_obtain_alias(ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0));
  151. if (!IS_ERR(parent))
  152. parent->d_op = &ocfs2_dentry_ops;
  153. bail_unlock:
  154. ocfs2_inode_unlock(dir, 0);
  155. bail:
  156. mlog_exit_ptr(parent);
  157. return parent;
  158. }
  159. static int ocfs2_encode_fh(struct dentry *dentry, u32 *fh_in, int *max_len,
  160. int connectable)
  161. {
  162. struct inode *inode = dentry->d_inode;
  163. int len = *max_len;
  164. int type = 1;
  165. u64 blkno;
  166. u32 generation;
  167. __le32 *fh = (__force __le32 *) fh_in;
  168. mlog_entry("(0x%p, '%.*s', 0x%p, %d, %d)\n", dentry,
  169. dentry->d_name.len, dentry->d_name.name,
  170. fh, len, connectable);
  171. if (len < 3 || (connectable && len < 6)) {
  172. mlog(ML_ERROR, "fh buffer is too small for encoding\n");
  173. type = 255;
  174. goto bail;
  175. }
  176. blkno = OCFS2_I(inode)->ip_blkno;
  177. generation = inode->i_generation;
  178. mlog(0, "Encoding fh: blkno: %llu, generation: %u\n",
  179. (unsigned long long)blkno, generation);
  180. len = 3;
  181. fh[0] = cpu_to_le32((u32)(blkno >> 32));
  182. fh[1] = cpu_to_le32((u32)(blkno & 0xffffffff));
  183. fh[2] = cpu_to_le32(generation);
  184. if (connectable && !S_ISDIR(inode->i_mode)) {
  185. struct inode *parent;
  186. spin_lock(&dentry->d_lock);
  187. parent = dentry->d_parent->d_inode;
  188. blkno = OCFS2_I(parent)->ip_blkno;
  189. generation = parent->i_generation;
  190. fh[3] = cpu_to_le32((u32)(blkno >> 32));
  191. fh[4] = cpu_to_le32((u32)(blkno & 0xffffffff));
  192. fh[5] = cpu_to_le32(generation);
  193. spin_unlock(&dentry->d_lock);
  194. len = 6;
  195. type = 2;
  196. mlog(0, "Encoding parent: blkno: %llu, generation: %u\n",
  197. (unsigned long long)blkno, generation);
  198. }
  199. *max_len = len;
  200. bail:
  201. mlog_exit(type);
  202. return type;
  203. }
  204. static struct dentry *ocfs2_fh_to_dentry(struct super_block *sb,
  205. struct fid *fid, int fh_len, int fh_type)
  206. {
  207. struct ocfs2_inode_handle handle;
  208. if (fh_len < 3 || fh_type > 2)
  209. return NULL;
  210. handle.ih_blkno = (u64)le32_to_cpu(fid->raw[0]) << 32;
  211. handle.ih_blkno |= (u64)le32_to_cpu(fid->raw[1]);
  212. handle.ih_generation = le32_to_cpu(fid->raw[2]);
  213. return ocfs2_get_dentry(sb, &handle);
  214. }
  215. static struct dentry *ocfs2_fh_to_parent(struct super_block *sb,
  216. struct fid *fid, int fh_len, int fh_type)
  217. {
  218. struct ocfs2_inode_handle parent;
  219. if (fh_type != 2 || fh_len < 6)
  220. return NULL;
  221. parent.ih_blkno = (u64)le32_to_cpu(fid->raw[3]) << 32;
  222. parent.ih_blkno |= (u64)le32_to_cpu(fid->raw[4]);
  223. parent.ih_generation = le32_to_cpu(fid->raw[5]);
  224. return ocfs2_get_dentry(sb, &parent);
  225. }
  226. const struct export_operations ocfs2_export_ops = {
  227. .encode_fh = ocfs2_encode_fh,
  228. .fh_to_dentry = ocfs2_fh_to_dentry,
  229. .fh_to_parent = ocfs2_fh_to_parent,
  230. .get_parent = ocfs2_get_parent,
  231. };