export.c 5.6 KB

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