export.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 "dir.h"
  31. #include "dlmglue.h"
  32. #include "dcache.h"
  33. #include "export.h"
  34. #include "inode.h"
  35. #include "buffer_head_io.h"
  36. struct ocfs2_inode_handle
  37. {
  38. u64 ih_blkno;
  39. u32 ih_generation;
  40. };
  41. static struct dentry *ocfs2_get_dentry(struct super_block *sb, void *vobjp)
  42. {
  43. struct ocfs2_inode_handle *handle = vobjp;
  44. struct inode *inode;
  45. struct dentry *result;
  46. mlog_entry("(0x%p, 0x%p)\n", sb, handle);
  47. if (handle->ih_blkno == 0) {
  48. mlog_errno(-ESTALE);
  49. return ERR_PTR(-ESTALE);
  50. }
  51. inode = ocfs2_iget(OCFS2_SB(sb), handle->ih_blkno, 0);
  52. if (IS_ERR(inode)) {
  53. mlog_errno(PTR_ERR(inode));
  54. return (void *)inode;
  55. }
  56. if (handle->ih_generation != inode->i_generation) {
  57. iput(inode);
  58. mlog_errno(-ESTALE);
  59. return ERR_PTR(-ESTALE);
  60. }
  61. result = d_alloc_anon(inode);
  62. if (!result) {
  63. iput(inode);
  64. mlog_errno(-ENOMEM);
  65. return ERR_PTR(-ENOMEM);
  66. }
  67. result->d_op = &ocfs2_dentry_ops;
  68. mlog_exit_ptr(result);
  69. return result;
  70. }
  71. static struct dentry *ocfs2_get_parent(struct dentry *child)
  72. {
  73. int status;
  74. u64 blkno;
  75. struct dentry *parent;
  76. struct inode *inode;
  77. struct inode *dir = child->d_inode;
  78. struct buffer_head *dirent_bh = NULL;
  79. struct ocfs2_dir_entry *dirent;
  80. mlog_entry("(0x%p, '%.*s')\n", child,
  81. child->d_name.len, child->d_name.name);
  82. mlog(0, "find parent of directory %llu\n",
  83. (unsigned long long)OCFS2_I(dir)->ip_blkno);
  84. status = ocfs2_meta_lock(dir, NULL, NULL, 0);
  85. if (status < 0) {
  86. if (status != -ENOENT)
  87. mlog_errno(status);
  88. parent = ERR_PTR(status);
  89. goto bail;
  90. }
  91. status = ocfs2_find_files_on_disk("..", 2, &blkno, dir, &dirent_bh,
  92. &dirent);
  93. if (status < 0) {
  94. parent = ERR_PTR(-ENOENT);
  95. goto bail_unlock;
  96. }
  97. inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
  98. if (IS_ERR(inode)) {
  99. mlog(ML_ERROR, "Unable to create inode %llu\n",
  100. (unsigned long long)blkno);
  101. parent = ERR_PTR(-EACCES);
  102. goto bail_unlock;
  103. }
  104. parent = d_alloc_anon(inode);
  105. if (!parent) {
  106. iput(inode);
  107. parent = ERR_PTR(-ENOMEM);
  108. }
  109. parent->d_op = &ocfs2_dentry_ops;
  110. bail_unlock:
  111. ocfs2_meta_unlock(dir, 0);
  112. if (dirent_bh)
  113. brelse(dirent_bh);
  114. bail:
  115. mlog_exit_ptr(parent);
  116. return parent;
  117. }
  118. static int ocfs2_encode_fh(struct dentry *dentry, __be32 *fh, int *max_len,
  119. int connectable)
  120. {
  121. struct inode *inode = dentry->d_inode;
  122. int len = *max_len;
  123. int type = 1;
  124. u64 blkno;
  125. u32 generation;
  126. mlog_entry("(0x%p, '%.*s', 0x%p, %d, %d)\n", dentry,
  127. dentry->d_name.len, dentry->d_name.name,
  128. fh, len, connectable);
  129. if (len < 3 || (connectable && len < 6)) {
  130. mlog(ML_ERROR, "fh buffer is too small for encoding\n");
  131. type = 255;
  132. goto bail;
  133. }
  134. blkno = OCFS2_I(inode)->ip_blkno;
  135. generation = inode->i_generation;
  136. mlog(0, "Encoding fh: blkno: %llu, generation: %u\n",
  137. (unsigned long long)blkno, generation);
  138. len = 3;
  139. fh[0] = cpu_to_le32((u32)(blkno >> 32));
  140. fh[1] = cpu_to_le32((u32)(blkno & 0xffffffff));
  141. fh[2] = cpu_to_le32(generation);
  142. if (connectable && !S_ISDIR(inode->i_mode)) {
  143. struct inode *parent;
  144. spin_lock(&dentry->d_lock);
  145. parent = dentry->d_parent->d_inode;
  146. blkno = OCFS2_I(parent)->ip_blkno;
  147. generation = parent->i_generation;
  148. fh[3] = cpu_to_le32((u32)(blkno >> 32));
  149. fh[4] = cpu_to_le32((u32)(blkno & 0xffffffff));
  150. fh[5] = cpu_to_le32(generation);
  151. spin_unlock(&dentry->d_lock);
  152. len = 6;
  153. type = 2;
  154. mlog(0, "Encoding parent: blkno: %llu, generation: %u\n",
  155. (unsigned long long)blkno, generation);
  156. }
  157. *max_len = len;
  158. bail:
  159. mlog_exit(type);
  160. return type;
  161. }
  162. static struct dentry *ocfs2_decode_fh(struct super_block *sb, __be32 *fh,
  163. int fh_len, int fileid_type,
  164. int (*acceptable)(void *context,
  165. struct dentry *de),
  166. void *context)
  167. {
  168. struct ocfs2_inode_handle handle, parent;
  169. struct dentry *ret = NULL;
  170. mlog_entry("(0x%p, 0x%p, %d, %d, 0x%p, 0x%p)\n",
  171. sb, fh, fh_len, fileid_type, acceptable, context);
  172. if (fh_len < 3 || fileid_type > 2)
  173. goto bail;
  174. if (fileid_type == 2) {
  175. if (fh_len < 6)
  176. goto bail;
  177. parent.ih_blkno = (u64)le32_to_cpu(fh[3]) << 32;
  178. parent.ih_blkno |= (u64)le32_to_cpu(fh[4]);
  179. parent.ih_generation = le32_to_cpu(fh[5]);
  180. mlog(0, "Decoding parent: blkno: %llu, generation: %u\n",
  181. (unsigned long long)parent.ih_blkno,
  182. parent.ih_generation);
  183. }
  184. handle.ih_blkno = (u64)le32_to_cpu(fh[0]) << 32;
  185. handle.ih_blkno |= (u64)le32_to_cpu(fh[1]);
  186. handle.ih_generation = le32_to_cpu(fh[2]);
  187. mlog(0, "Encoding fh: blkno: %llu, generation: %u\n",
  188. (unsigned long long)handle.ih_blkno, handle.ih_generation);
  189. ret = ocfs2_export_ops.find_exported_dentry(sb, &handle, &parent,
  190. acceptable, context);
  191. bail:
  192. mlog_exit_ptr(ret);
  193. return ret;
  194. }
  195. struct export_operations ocfs2_export_ops = {
  196. .decode_fh = ocfs2_decode_fh,
  197. .encode_fh = ocfs2_encode_fh,
  198. .get_parent = ocfs2_get_parent,
  199. .get_dentry = ocfs2_get_dentry,
  200. };