export.c 5.7 KB

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