export.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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,
  42. struct ocfs2_inode_handle *handle)
  43. {
  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, 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_obtain_alias(inode);
  59. if (!IS_ERR(result))
  60. result->d_op = &ocfs2_dentry_ops;
  61. mlog_exit_ptr(result);
  62. return result;
  63. }
  64. static struct dentry *ocfs2_get_parent(struct dentry *child)
  65. {
  66. int status;
  67. u64 blkno;
  68. struct dentry *parent;
  69. struct inode *dir = child->d_inode;
  70. mlog_entry("(0x%p, '%.*s')\n", child,
  71. child->d_name.len, child->d_name.name);
  72. mlog(0, "find parent of directory %llu\n",
  73. (unsigned long long)OCFS2_I(dir)->ip_blkno);
  74. status = ocfs2_inode_lock(dir, NULL, 0);
  75. if (status < 0) {
  76. if (status != -ENOENT)
  77. mlog_errno(status);
  78. parent = ERR_PTR(status);
  79. goto bail;
  80. }
  81. status = ocfs2_lookup_ino_from_name(dir, "..", 2, &blkno);
  82. if (status < 0) {
  83. parent = ERR_PTR(-ENOENT);
  84. goto bail_unlock;
  85. }
  86. parent = d_obtain_alias(ocfs2_iget(OCFS2_SB(dir->i_sb), blkno, 0, 0));
  87. if (!IS_ERR(parent))
  88. parent->d_op = &ocfs2_dentry_ops;
  89. bail_unlock:
  90. ocfs2_inode_unlock(dir, 0);
  91. bail:
  92. mlog_exit_ptr(parent);
  93. return parent;
  94. }
  95. static int ocfs2_encode_fh(struct dentry *dentry, u32 *fh_in, int *max_len,
  96. int connectable)
  97. {
  98. struct inode *inode = dentry->d_inode;
  99. int len = *max_len;
  100. int type = 1;
  101. u64 blkno;
  102. u32 generation;
  103. __le32 *fh = (__force __le32 *) fh_in;
  104. mlog_entry("(0x%p, '%.*s', 0x%p, %d, %d)\n", dentry,
  105. dentry->d_name.len, dentry->d_name.name,
  106. fh, len, connectable);
  107. if (len < 3 || (connectable && len < 6)) {
  108. mlog(ML_ERROR, "fh buffer is too small for encoding\n");
  109. type = 255;
  110. goto bail;
  111. }
  112. blkno = OCFS2_I(inode)->ip_blkno;
  113. generation = inode->i_generation;
  114. mlog(0, "Encoding fh: blkno: %llu, generation: %u\n",
  115. (unsigned long long)blkno, generation);
  116. len = 3;
  117. fh[0] = cpu_to_le32((u32)(blkno >> 32));
  118. fh[1] = cpu_to_le32((u32)(blkno & 0xffffffff));
  119. fh[2] = cpu_to_le32(generation);
  120. if (connectable && !S_ISDIR(inode->i_mode)) {
  121. struct inode *parent;
  122. spin_lock(&dentry->d_lock);
  123. parent = dentry->d_parent->d_inode;
  124. blkno = OCFS2_I(parent)->ip_blkno;
  125. generation = parent->i_generation;
  126. fh[3] = cpu_to_le32((u32)(blkno >> 32));
  127. fh[4] = cpu_to_le32((u32)(blkno & 0xffffffff));
  128. fh[5] = cpu_to_le32(generation);
  129. spin_unlock(&dentry->d_lock);
  130. len = 6;
  131. type = 2;
  132. mlog(0, "Encoding parent: blkno: %llu, generation: %u\n",
  133. (unsigned long long)blkno, generation);
  134. }
  135. *max_len = len;
  136. bail:
  137. mlog_exit(type);
  138. return type;
  139. }
  140. static struct dentry *ocfs2_fh_to_dentry(struct super_block *sb,
  141. struct fid *fid, int fh_len, int fh_type)
  142. {
  143. struct ocfs2_inode_handle handle;
  144. if (fh_len < 3 || fh_type > 2)
  145. return NULL;
  146. handle.ih_blkno = (u64)le32_to_cpu(fid->raw[0]) << 32;
  147. handle.ih_blkno |= (u64)le32_to_cpu(fid->raw[1]);
  148. handle.ih_generation = le32_to_cpu(fid->raw[2]);
  149. return ocfs2_get_dentry(sb, &handle);
  150. }
  151. static struct dentry *ocfs2_fh_to_parent(struct super_block *sb,
  152. struct fid *fid, int fh_len, int fh_type)
  153. {
  154. struct ocfs2_inode_handle parent;
  155. if (fh_type != 2 || fh_len < 6)
  156. return NULL;
  157. parent.ih_blkno = (u64)le32_to_cpu(fid->raw[3]) << 32;
  158. parent.ih_blkno |= (u64)le32_to_cpu(fid->raw[4]);
  159. parent.ih_generation = le32_to_cpu(fid->raw[5]);
  160. return ocfs2_get_dentry(sb, &parent);
  161. }
  162. const struct export_operations ocfs2_export_ops = {
  163. .encode_fh = ocfs2_encode_fh,
  164. .fh_to_dentry = ocfs2_fh_to_dentry,
  165. .fh_to_parent = ocfs2_fh_to_parent,
  166. .get_parent = ocfs2_get_parent,
  167. };