export.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 %llu\n",
  81. (unsigned long long)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 %llu\n",
  98. (unsigned long long)blkno);
  99. parent = ERR_PTR(-EACCES);
  100. goto bail_unlock;
  101. }
  102. parent = d_alloc_anon(inode);
  103. if (!parent) {
  104. iput(inode);
  105. parent = ERR_PTR(-ENOMEM);
  106. }
  107. bail_unlock:
  108. ocfs2_meta_unlock(dir, 0);
  109. if (dirent_bh)
  110. brelse(dirent_bh);
  111. bail:
  112. mlog_exit_ptr(parent);
  113. return parent;
  114. }
  115. static int ocfs2_encode_fh(struct dentry *dentry, __be32 *fh, int *max_len,
  116. int connectable)
  117. {
  118. struct inode *inode = dentry->d_inode;
  119. int len = *max_len;
  120. int type = 1;
  121. u64 blkno;
  122. u32 generation;
  123. mlog_entry("(0x%p, '%.*s', 0x%p, %d, %d)\n", dentry,
  124. dentry->d_name.len, dentry->d_name.name,
  125. fh, len, connectable);
  126. if (len < 3 || (connectable && len < 6)) {
  127. mlog(ML_ERROR, "fh buffer is too small for encoding\n");
  128. type = 255;
  129. goto bail;
  130. }
  131. blkno = OCFS2_I(inode)->ip_blkno;
  132. generation = inode->i_generation;
  133. mlog(0, "Encoding fh: blkno: %llu, generation: %u\n",
  134. (unsigned long long)blkno, generation);
  135. len = 3;
  136. fh[0] = cpu_to_le32((u32)(blkno >> 32));
  137. fh[1] = cpu_to_le32((u32)(blkno & 0xffffffff));
  138. fh[2] = cpu_to_le32(generation);
  139. if (connectable && !S_ISDIR(inode->i_mode)) {
  140. struct inode *parent;
  141. spin_lock(&dentry->d_lock);
  142. parent = dentry->d_parent->d_inode;
  143. blkno = OCFS2_I(parent)->ip_blkno;
  144. generation = parent->i_generation;
  145. fh[3] = cpu_to_le32((u32)(blkno >> 32));
  146. fh[4] = cpu_to_le32((u32)(blkno & 0xffffffff));
  147. fh[5] = cpu_to_le32(generation);
  148. spin_unlock(&dentry->d_lock);
  149. len = 6;
  150. type = 2;
  151. mlog(0, "Encoding parent: blkno: %llu, generation: %u\n",
  152. (unsigned long long)blkno, generation);
  153. }
  154. *max_len = len;
  155. bail:
  156. mlog_exit(type);
  157. return type;
  158. }
  159. static struct dentry *ocfs2_decode_fh(struct super_block *sb, __be32 *fh,
  160. int fh_len, int fileid_type,
  161. int (*acceptable)(void *context,
  162. struct dentry *de),
  163. void *context)
  164. {
  165. struct ocfs2_inode_handle handle, parent;
  166. struct dentry *ret = NULL;
  167. mlog_entry("(0x%p, 0x%p, %d, %d, 0x%p, 0x%p)\n",
  168. sb, fh, fh_len, fileid_type, acceptable, context);
  169. if (fh_len < 3 || fileid_type > 2)
  170. goto bail;
  171. if (fileid_type == 2) {
  172. if (fh_len < 6)
  173. goto bail;
  174. parent.ih_blkno = (u64)le32_to_cpu(fh[3]) << 32;
  175. parent.ih_blkno |= (u64)le32_to_cpu(fh[4]);
  176. parent.ih_generation = le32_to_cpu(fh[5]);
  177. mlog(0, "Decoding parent: blkno: %llu, generation: %u\n",
  178. (unsigned long long)parent.ih_blkno,
  179. parent.ih_generation);
  180. }
  181. handle.ih_blkno = (u64)le32_to_cpu(fh[0]) << 32;
  182. handle.ih_blkno |= (u64)le32_to_cpu(fh[1]);
  183. handle.ih_generation = le32_to_cpu(fh[2]);
  184. mlog(0, "Encoding fh: blkno: %llu, generation: %u\n",
  185. (unsigned long long)handle.ih_blkno, handle.ih_generation);
  186. ret = ocfs2_export_ops.find_exported_dentry(sb, &handle, &parent,
  187. acceptable, context);
  188. bail:
  189. mlog_exit_ptr(ret);
  190. return ret;
  191. }
  192. struct export_operations ocfs2_export_ops = {
  193. .decode_fh = ocfs2_decode_fh,
  194. .encode_fh = ocfs2_encode_fh,
  195. .get_parent = ocfs2_get_parent,
  196. .get_dentry = ocfs2_get_dentry,
  197. };