export.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 if (status != -ESTALE) {
  66. mlog(ML_ERROR, "test inode bit failed %d\n", status);
  67. }
  68. goto unlock_nfs_sync;
  69. }
  70. trace_ocfs2_get_dentry_test_bit(status, set);
  71. /* If the inode allocator bit is clear, this inode must be stale */
  72. if (!set) {
  73. status = -ESTALE;
  74. goto unlock_nfs_sync;
  75. }
  76. inode = ocfs2_iget(osb, blkno, 0, 0);
  77. unlock_nfs_sync:
  78. ocfs2_nfs_sync_unlock(osb, 1);
  79. check_err:
  80. if (status < 0) {
  81. if (status == -ESTALE) {
  82. trace_ocfs2_get_dentry_stale((unsigned long long)blkno,
  83. handle->ih_generation);
  84. }
  85. result = ERR_PTR(status);
  86. goto bail;
  87. }
  88. if (IS_ERR(inode)) {
  89. mlog_errno(PTR_ERR(inode));
  90. result = ERR_CAST(inode);
  91. goto bail;
  92. }
  93. check_gen:
  94. if (handle->ih_generation != inode->i_generation) {
  95. trace_ocfs2_get_dentry_generation((unsigned long long)blkno,
  96. handle->ih_generation,
  97. inode->i_generation);
  98. iput(inode);
  99. result = ERR_PTR(-ESTALE);
  100. goto bail;
  101. }
  102. result = d_obtain_alias(inode);
  103. if (IS_ERR(result))
  104. mlog_errno(PTR_ERR(result));
  105. bail:
  106. trace_ocfs2_get_dentry_end(result);
  107. return result;
  108. }
  109. static struct dentry *ocfs2_get_parent(struct dentry *child)
  110. {
  111. int status;
  112. u64 blkno;
  113. struct dentry *parent;
  114. struct inode *dir = d_inode(child);
  115. int set;
  116. trace_ocfs2_get_parent(child, child->d_name.len, child->d_name.name,
  117. (unsigned long long)OCFS2_I(dir)->ip_blkno);
  118. status = ocfs2_nfs_sync_lock(OCFS2_SB(dir->i_sb), 1);
  119. if (status < 0) {
  120. mlog(ML_ERROR, "getting nfs sync lock(EX) failed %d\n", status);
  121. parent = ERR_PTR(status);
  122. goto bail;
  123. }
  124. status = ocfs2_inode_lock(dir, NULL, 0);
  125. if (status < 0) {
  126. if (status != -ENOENT)
  127. mlog_errno(status);
  128. parent = ERR_PTR(status);
  129. goto unlock_nfs_sync;
  130. }
  131. status = ocfs2_lookup_ino_from_name(dir, "..", 2, &blkno);
  132. if (status < 0) {
  133. parent = ERR_PTR(-ENOENT);
  134. goto bail_unlock;
  135. }
  136. status = ocfs2_test_inode_bit(OCFS2_SB(dir->i_sb), blkno, &set);
  137. if (status < 0) {
  138. if (status == -EINVAL) {
  139. status = -ESTALE;
  140. } else if (status != -ESTALE) {
  141. mlog(ML_ERROR, "test inode bit failed %d\n", status);
  142. }
  143. parent = ERR_PTR(status);
  144. goto bail_unlock;
  145. }
  146. trace_ocfs2_get_dentry_test_bit(status, set);
  147. if (!set) {
  148. status = -ESTALE;
  149. parent = ERR_PTR(status);
  150. goto bail_unlock;
  151. }
  152. parent = d_obtain_alias(ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0));
  153. bail_unlock:
  154. ocfs2_inode_unlock(dir, 0);
  155. unlock_nfs_sync:
  156. ocfs2_nfs_sync_unlock(OCFS2_SB(dir->i_sb), 1);
  157. bail:
  158. trace_ocfs2_get_parent_end(parent);
  159. return parent;
  160. }
  161. static int ocfs2_encode_fh(struct inode *inode, u32 *fh_in, int *max_len,
  162. struct inode *parent)
  163. {
  164. int len = *max_len;
  165. int type = 1;
  166. u64 blkno;
  167. u32 generation;
  168. __le32 *fh = (__force __le32 *) fh_in;
  169. #ifdef TRACE_HOOKS_ARE_NOT_BRAINDEAD_IN_YOUR_OPINION
  170. #error "You go ahead and fix that mess, then. Somehow"
  171. trace_ocfs2_encode_fh_begin(dentry, dentry->d_name.len,
  172. dentry->d_name.name,
  173. fh, len, connectable);
  174. #endif
  175. if (parent && (len < 6)) {
  176. *max_len = 6;
  177. type = FILEID_INVALID;
  178. goto bail;
  179. } else if (len < 3) {
  180. *max_len = 3;
  181. type = FILEID_INVALID;
  182. goto bail;
  183. }
  184. blkno = OCFS2_I(inode)->ip_blkno;
  185. generation = inode->i_generation;
  186. trace_ocfs2_encode_fh_self((unsigned long long)blkno, generation);
  187. len = 3;
  188. fh[0] = cpu_to_le32((u32)(blkno >> 32));
  189. fh[1] = cpu_to_le32((u32)(blkno & 0xffffffff));
  190. fh[2] = cpu_to_le32(generation);
  191. if (parent) {
  192. blkno = OCFS2_I(parent)->ip_blkno;
  193. generation = parent->i_generation;
  194. fh[3] = cpu_to_le32((u32)(blkno >> 32));
  195. fh[4] = cpu_to_le32((u32)(blkno & 0xffffffff));
  196. fh[5] = cpu_to_le32(generation);
  197. len = 6;
  198. type = 2;
  199. trace_ocfs2_encode_fh_parent((unsigned long long)blkno,
  200. generation);
  201. }
  202. *max_len = len;
  203. bail:
  204. trace_ocfs2_encode_fh_type(type);
  205. return type;
  206. }
  207. static struct dentry *ocfs2_fh_to_dentry(struct super_block *sb,
  208. struct fid *fid, int fh_len, int fh_type)
  209. {
  210. struct ocfs2_inode_handle handle;
  211. if (fh_len < 3 || fh_type > 2)
  212. return NULL;
  213. handle.ih_blkno = (u64)le32_to_cpu((__force __le32)fid->raw[0]) << 32;
  214. handle.ih_blkno |= (u64)le32_to_cpu((__force __le32)fid->raw[1]);
  215. handle.ih_generation = le32_to_cpu((__force __le32)fid->raw[2]);
  216. return ocfs2_get_dentry(sb, &handle);
  217. }
  218. static struct dentry *ocfs2_fh_to_parent(struct super_block *sb,
  219. struct fid *fid, int fh_len, int fh_type)
  220. {
  221. struct ocfs2_inode_handle parent;
  222. if (fh_type != 2 || fh_len < 6)
  223. return NULL;
  224. parent.ih_blkno = (u64)le32_to_cpu((__force __le32)fid->raw[3]) << 32;
  225. parent.ih_blkno |= (u64)le32_to_cpu((__force __le32)fid->raw[4]);
  226. parent.ih_generation = le32_to_cpu((__force __le32)fid->raw[5]);
  227. return ocfs2_get_dentry(sb, &parent);
  228. }
  229. const struct export_operations ocfs2_export_ops = {
  230. .encode_fh = ocfs2_encode_fh,
  231. .fh_to_dentry = ocfs2_fh_to_dentry,
  232. .fh_to_parent = ocfs2_fh_to_parent,
  233. .get_parent = ocfs2_get_parent,
  234. };