export.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * export.c
  4. *
  5. * Functions to facilitate NFS exporting
  6. *
  7. * Copyright (C) 2002, 2005 Oracle. All rights reserved.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/types.h>
  11. #include <cluster/masklog.h>
  12. #include "ocfs2.h"
  13. #include "alloc.h"
  14. #include "dir.h"
  15. #include "dlmglue.h"
  16. #include "dcache.h"
  17. #include "export.h"
  18. #include "inode.h"
  19. #include "buffer_head_io.h"
  20. #include "suballoc.h"
  21. #include "ocfs2_trace.h"
  22. struct ocfs2_inode_handle
  23. {
  24. u64 ih_blkno;
  25. u32 ih_generation;
  26. };
  27. static struct dentry *ocfs2_get_dentry(struct super_block *sb,
  28. struct ocfs2_inode_handle *handle)
  29. {
  30. struct inode *inode;
  31. struct ocfs2_super *osb = OCFS2_SB(sb);
  32. u64 blkno = handle->ih_blkno;
  33. int status, set;
  34. struct dentry *result;
  35. trace_ocfs2_get_dentry_begin(sb, handle, (unsigned long long)blkno);
  36. if (blkno == 0) {
  37. result = ERR_PTR(-ESTALE);
  38. goto bail;
  39. }
  40. inode = ocfs2_ilookup(sb, blkno);
  41. /*
  42. * If the inode exists in memory, we only need to check it's
  43. * generation number
  44. */
  45. if (inode)
  46. goto check_gen;
  47. /*
  48. * This will synchronize us against ocfs2_delete_inode() on
  49. * all nodes
  50. */
  51. status = ocfs2_nfs_sync_lock(osb, 1);
  52. if (status < 0) {
  53. mlog(ML_ERROR, "getting nfs sync lock(EX) failed %d\n", status);
  54. goto check_err;
  55. }
  56. status = ocfs2_test_inode_bit(osb, blkno, &set);
  57. if (status < 0) {
  58. if (status == -EINVAL) {
  59. /*
  60. * The blkno NFS gave us doesn't even show up
  61. * as an inode, we return -ESTALE to be
  62. * nice
  63. */
  64. status = -ESTALE;
  65. } else
  66. mlog(ML_ERROR, "test inode bit failed %d\n", status);
  67. goto unlock_nfs_sync;
  68. }
  69. trace_ocfs2_get_dentry_test_bit(status, set);
  70. /* If the inode allocator bit is clear, this inode must be stale */
  71. if (!set) {
  72. status = -ESTALE;
  73. goto unlock_nfs_sync;
  74. }
  75. inode = ocfs2_iget(osb, blkno, 0, 0);
  76. unlock_nfs_sync:
  77. ocfs2_nfs_sync_unlock(osb, 1);
  78. check_err:
  79. if (status < 0) {
  80. if (status == -ESTALE) {
  81. trace_ocfs2_get_dentry_stale((unsigned long long)blkno,
  82. handle->ih_generation);
  83. }
  84. result = ERR_PTR(status);
  85. goto bail;
  86. }
  87. if (IS_ERR(inode)) {
  88. mlog_errno(PTR_ERR(inode));
  89. result = ERR_CAST(inode);
  90. goto bail;
  91. }
  92. check_gen:
  93. if (handle->ih_generation != inode->i_generation) {
  94. trace_ocfs2_get_dentry_generation((unsigned long long)blkno,
  95. handle->ih_generation,
  96. inode->i_generation);
  97. iput(inode);
  98. result = ERR_PTR(-ESTALE);
  99. goto bail;
  100. }
  101. result = d_obtain_alias(inode);
  102. if (IS_ERR(result))
  103. mlog_errno(PTR_ERR(result));
  104. bail:
  105. trace_ocfs2_get_dentry_end(result);
  106. return result;
  107. }
  108. static struct dentry *ocfs2_get_parent(struct dentry *child)
  109. {
  110. int status;
  111. u64 blkno;
  112. struct dentry *parent;
  113. struct inode *dir = d_inode(child);
  114. int set;
  115. trace_ocfs2_get_parent(child, child->d_name.len, child->d_name.name,
  116. (unsigned long long)OCFS2_I(dir)->ip_blkno);
  117. status = ocfs2_nfs_sync_lock(OCFS2_SB(dir->i_sb), 1);
  118. if (status < 0) {
  119. mlog(ML_ERROR, "getting nfs sync lock(EX) failed %d\n", status);
  120. parent = ERR_PTR(status);
  121. goto bail;
  122. }
  123. status = ocfs2_inode_lock(dir, NULL, 0);
  124. if (status < 0) {
  125. if (status != -ENOENT)
  126. mlog_errno(status);
  127. parent = ERR_PTR(status);
  128. goto unlock_nfs_sync;
  129. }
  130. status = ocfs2_lookup_ino_from_name(dir, "..", 2, &blkno);
  131. if (status < 0) {
  132. parent = ERR_PTR(-ENOENT);
  133. goto bail_unlock;
  134. }
  135. status = ocfs2_test_inode_bit(OCFS2_SB(dir->i_sb), blkno, &set);
  136. if (status < 0) {
  137. if (status == -EINVAL) {
  138. status = -ESTALE;
  139. } else
  140. mlog(ML_ERROR, "test inode bit failed %d\n", status);
  141. parent = ERR_PTR(status);
  142. goto bail_unlock;
  143. }
  144. trace_ocfs2_get_dentry_test_bit(status, set);
  145. if (!set) {
  146. status = -ESTALE;
  147. parent = ERR_PTR(status);
  148. goto bail_unlock;
  149. }
  150. parent = d_obtain_alias(ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0));
  151. bail_unlock:
  152. ocfs2_inode_unlock(dir, 0);
  153. unlock_nfs_sync:
  154. ocfs2_nfs_sync_unlock(OCFS2_SB(dir->i_sb), 1);
  155. bail:
  156. trace_ocfs2_get_parent_end(parent);
  157. return parent;
  158. }
  159. static int ocfs2_encode_fh(struct inode *inode, u32 *fh_in, int *max_len,
  160. struct inode *parent)
  161. {
  162. int len = *max_len;
  163. int type = 1;
  164. u64 blkno;
  165. u32 generation;
  166. __le32 *fh = (__force __le32 *) fh_in;
  167. #ifdef TRACE_HOOKS_ARE_NOT_BRAINDEAD_IN_YOUR_OPINION
  168. #error "You go ahead and fix that mess, then. Somehow"
  169. trace_ocfs2_encode_fh_begin(dentry, dentry->d_name.len,
  170. dentry->d_name.name,
  171. fh, len, connectable);
  172. #endif
  173. if (parent && (len < 6)) {
  174. *max_len = 6;
  175. type = FILEID_INVALID;
  176. goto bail;
  177. } else if (len < 3) {
  178. *max_len = 3;
  179. type = FILEID_INVALID;
  180. goto bail;
  181. }
  182. blkno = OCFS2_I(inode)->ip_blkno;
  183. generation = inode->i_generation;
  184. trace_ocfs2_encode_fh_self((unsigned long long)blkno, generation);
  185. len = 3;
  186. fh[0] = cpu_to_le32((u32)(blkno >> 32));
  187. fh[1] = cpu_to_le32((u32)(blkno & 0xffffffff));
  188. fh[2] = cpu_to_le32(generation);
  189. if (parent) {
  190. blkno = OCFS2_I(parent)->ip_blkno;
  191. generation = parent->i_generation;
  192. fh[3] = cpu_to_le32((u32)(blkno >> 32));
  193. fh[4] = cpu_to_le32((u32)(blkno & 0xffffffff));
  194. fh[5] = cpu_to_le32(generation);
  195. len = 6;
  196. type = 2;
  197. trace_ocfs2_encode_fh_parent((unsigned long long)blkno,
  198. generation);
  199. }
  200. *max_len = len;
  201. bail:
  202. trace_ocfs2_encode_fh_type(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((__force __le32)fid->raw[0]) << 32;
  212. handle.ih_blkno |= (u64)le32_to_cpu((__force __le32)fid->raw[1]);
  213. handle.ih_generation = le32_to_cpu((__force __le32)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((__force __le32)fid->raw[3]) << 32;
  223. parent.ih_blkno |= (u64)le32_to_cpu((__force __le32)fid->raw[4]);
  224. parent.ih_generation = le32_to_cpu((__force __le32)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. .flags = EXPORT_OP_ASYNC_LOCK,
  233. };