export.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. return (void *)inode;
  54. if (handle->ih_generation != inode->i_generation) {
  55. iput(inode);
  56. return ERR_PTR(-ESTALE);
  57. }
  58. result = d_alloc_anon(inode);
  59. if (!result) {
  60. iput(inode);
  61. mlog_errno(-ENOMEM);
  62. return ERR_PTR(-ENOMEM);
  63. }
  64. result->d_op = &ocfs2_dentry_ops;
  65. mlog_exit_ptr(result);
  66. return result;
  67. }
  68. static struct dentry *ocfs2_get_parent(struct dentry *child)
  69. {
  70. int status;
  71. u64 blkno;
  72. struct dentry *parent;
  73. struct inode *inode;
  74. struct inode *dir = child->d_inode;
  75. struct buffer_head *dirent_bh = NULL;
  76. struct ocfs2_dir_entry *dirent;
  77. mlog_entry("(0x%p, '%.*s')\n", child,
  78. child->d_name.len, child->d_name.name);
  79. mlog(0, "find parent of directory %llu\n",
  80. (unsigned long long)OCFS2_I(dir)->ip_blkno);
  81. status = ocfs2_meta_lock(dir, NULL, 0);
  82. if (status < 0) {
  83. if (status != -ENOENT)
  84. mlog_errno(status);
  85. parent = ERR_PTR(status);
  86. goto bail;
  87. }
  88. status = ocfs2_find_files_on_disk("..", 2, &blkno, dir, &dirent_bh,
  89. &dirent);
  90. if (status < 0) {
  91. parent = ERR_PTR(-ENOENT);
  92. goto bail_unlock;
  93. }
  94. inode = ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0);
  95. if (IS_ERR(inode)) {
  96. mlog(ML_ERROR, "Unable to create inode %llu\n",
  97. (unsigned long long)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. parent->d_op = &ocfs2_dentry_ops;
  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. };