export.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* -*- mode: c; c-basic-offset: 8; -*-
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * export.c
  6. *
  7. * Functions to facilitate NFS exporting
  8. *
  9. * Copyright (C) 2002, 2005 Oracle. All rights reserved.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/types.h>
  13. #include <cluster/masklog.h>
  14. #include "ocfs2.h"
  15. #include "alloc.h"
  16. #include "dir.h"
  17. #include "dlmglue.h"
  18. #include "dcache.h"
  19. #include "export.h"
  20. #include "inode.h"
  21. #include "buffer_head_io.h"
  22. #include "suballoc.h"
  23. #include "ocfs2_trace.h"
  24. struct ocfs2_inode_handle
  25. {
  26. u64 ih_blkno;
  27. u32 ih_generation;
  28. };
  29. static struct dentry *ocfs2_get_dentry(struct super_block *sb,
  30. struct ocfs2_inode_handle *handle)
  31. {
  32. struct inode *inode;
  33. struct ocfs2_super *osb = OCFS2_SB(sb);
  34. u64 blkno = handle->ih_blkno;
  35. int status, set;
  36. struct dentry *result;
  37. trace_ocfs2_get_dentry_begin(sb, handle, (unsigned long long)blkno);
  38. if (blkno == 0) {
  39. result = ERR_PTR(-ESTALE);
  40. goto bail;
  41. }
  42. inode = ocfs2_ilookup(sb, blkno);
  43. /*
  44. * If the inode exists in memory, we only need to check it's
  45. * generation number
  46. */
  47. if (inode)
  48. goto check_gen;
  49. /*
  50. * This will synchronize us against ocfs2_delete_inode() on
  51. * all nodes
  52. */
  53. status = ocfs2_nfs_sync_lock(osb, 1);
  54. if (status < 0) {
  55. mlog(ML_ERROR, "getting nfs sync lock(EX) failed %d\n", status);
  56. goto check_err;
  57. }
  58. status = ocfs2_test_inode_bit(osb, blkno, &set);
  59. if (status < 0) {
  60. if (status == -EINVAL) {
  61. /*
  62. * The blkno NFS gave us doesn't even show up
  63. * as an inode, we return -ESTALE to be
  64. * nice
  65. */
  66. status = -ESTALE;
  67. } else
  68. mlog(ML_ERROR, "test inode bit failed %d\n", status);
  69. goto unlock_nfs_sync;
  70. }
  71. trace_ocfs2_get_dentry_test_bit(status, set);
  72. /* If the inode allocator bit is clear, this inode must be stale */
  73. if (!set) {
  74. status = -ESTALE;
  75. goto unlock_nfs_sync;
  76. }
  77. inode = ocfs2_iget(osb, blkno, 0, 0);
  78. unlock_nfs_sync:
  79. ocfs2_nfs_sync_unlock(osb, 1);
  80. check_err:
  81. if (status < 0) {
  82. if (status == -ESTALE) {
  83. trace_ocfs2_get_dentry_stale((unsigned long long)blkno,
  84. handle->ih_generation);
  85. }
  86. result = ERR_PTR(status);
  87. goto bail;
  88. }
  89. if (IS_ERR(inode)) {
  90. mlog_errno(PTR_ERR(inode));
  91. result = ERR_CAST(inode);
  92. goto bail;
  93. }
  94. check_gen:
  95. if (handle->ih_generation != inode->i_generation) {
  96. trace_ocfs2_get_dentry_generation((unsigned long long)blkno,
  97. handle->ih_generation,
  98. inode->i_generation);
  99. iput(inode);
  100. result = ERR_PTR(-ESTALE);
  101. goto bail;
  102. }
  103. result = d_obtain_alias(inode);
  104. if (IS_ERR(result))
  105. mlog_errno(PTR_ERR(result));
  106. bail:
  107. trace_ocfs2_get_dentry_end(result);
  108. return result;
  109. }
  110. static struct dentry *ocfs2_get_parent(struct dentry *child)
  111. {
  112. int status;
  113. u64 blkno;
  114. struct dentry *parent;
  115. struct inode *dir = d_inode(child);
  116. int set;
  117. trace_ocfs2_get_parent(child, child->d_name.len, child->d_name.name,
  118. (unsigned long long)OCFS2_I(dir)->ip_blkno);
  119. status = ocfs2_nfs_sync_lock(OCFS2_SB(dir->i_sb), 1);
  120. if (status < 0) {
  121. mlog(ML_ERROR, "getting nfs sync lock(EX) failed %d\n", status);
  122. parent = ERR_PTR(status);
  123. goto bail;
  124. }
  125. status = ocfs2_inode_lock(dir, NULL, 0);
  126. if (status < 0) {
  127. if (status != -ENOENT)
  128. mlog_errno(status);
  129. parent = ERR_PTR(status);
  130. goto unlock_nfs_sync;
  131. }
  132. status = ocfs2_lookup_ino_from_name(dir, "..", 2, &blkno);
  133. if (status < 0) {
  134. parent = ERR_PTR(-ENOENT);
  135. goto bail_unlock;
  136. }
  137. status = ocfs2_test_inode_bit(OCFS2_SB(dir->i_sb), blkno, &set);
  138. if (status < 0) {
  139. if (status == -EINVAL) {
  140. status = -ESTALE;
  141. } else
  142. mlog(ML_ERROR, "test inode bit failed %d\n", status);
  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(fid->raw[0]) << 32;
  214. handle.ih_blkno |= (u64)le32_to_cpu(fid->raw[1]);
  215. handle.ih_generation = le32_to_cpu(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(fid->raw[3]) << 32;
  225. parent.ih_blkno |= (u64)le32_to_cpu(fid->raw[4]);
  226. parent.ih_generation = le32_to_cpu(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. };