export.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <linux/fs.h>
  2. #include <linux/types.h>
  3. #include "ctree.h"
  4. #include "disk-io.h"
  5. #include "btrfs_inode.h"
  6. #include "print-tree.h"
  7. #include "export.h"
  8. #include "compat.h"
  9. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  10. #define FILEID_BTRFS_WITHOUT_PARENT 0x4d
  11. #define FILEID_BTRFS_WITH_PARENT 0x4e
  12. #define FILEID_BTRFS_WITH_PARENT_ROOT 0x4f
  13. #endif
  14. #define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, parent_objectid)/4)
  15. #define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, parent_root_objectid)/4)
  16. #define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid)/4)
  17. static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
  18. int connectable)
  19. {
  20. struct btrfs_fid *fid = (struct btrfs_fid *)fh;
  21. struct inode *inode = dentry->d_inode;
  22. int len = *max_len;
  23. int type;
  24. if ((len < BTRFS_FID_SIZE_NON_CONNECTABLE) ||
  25. (connectable && len < BTRFS_FID_SIZE_CONNECTABLE))
  26. return 255;
  27. len = BTRFS_FID_SIZE_NON_CONNECTABLE;
  28. type = FILEID_BTRFS_WITHOUT_PARENT;
  29. fid->objectid = BTRFS_I(inode)->location.objectid;
  30. fid->root_objectid = BTRFS_I(inode)->root->objectid;
  31. fid->gen = inode->i_generation;
  32. if (connectable && !S_ISDIR(inode->i_mode)) {
  33. struct inode *parent;
  34. u64 parent_root_id;
  35. spin_lock(&dentry->d_lock);
  36. parent = dentry->d_parent->d_inode;
  37. fid->parent_objectid = BTRFS_I(parent)->location.objectid;
  38. fid->parent_gen = parent->i_generation;
  39. parent_root_id = BTRFS_I(parent)->root->objectid;
  40. spin_unlock(&dentry->d_lock);
  41. if (parent_root_id != fid->root_objectid) {
  42. fid->parent_root_objectid = parent_root_id;
  43. len = BTRFS_FID_SIZE_CONNECTABLE_ROOT;
  44. type = FILEID_BTRFS_WITH_PARENT_ROOT;
  45. } else {
  46. len = BTRFS_FID_SIZE_CONNECTABLE;
  47. type = FILEID_BTRFS_WITH_PARENT;
  48. }
  49. }
  50. *max_len = len;
  51. return type;
  52. }
  53. static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
  54. u64 root_objectid, u32 generation)
  55. {
  56. struct btrfs_root *root;
  57. struct inode *inode;
  58. struct dentry *result;
  59. struct btrfs_key key;
  60. key.objectid = root_objectid;
  61. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  62. key.offset = (u64)-1;
  63. root = btrfs_read_fs_root_no_name(btrfs_sb(sb)->fs_info, &key);
  64. if (IS_ERR(root))
  65. return ERR_CAST(root);
  66. key.objectid = objectid;
  67. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  68. key.offset = 0;
  69. inode = btrfs_iget(sb, &key, root, NULL);
  70. if (IS_ERR(inode))
  71. return (void *)inode;
  72. if (generation != inode->i_generation) {
  73. iput(inode);
  74. return ERR_PTR(-ESTALE);
  75. }
  76. result = d_obtain_alias(inode);
  77. if (!result)
  78. return ERR_PTR(-ENOMEM);
  79. return result;
  80. }
  81. static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
  82. int fh_len, int fh_type)
  83. {
  84. struct btrfs_fid *fid = (struct btrfs_fid *) fh;
  85. u64 objectid, root_objectid;
  86. u32 generation;
  87. if (fh_type == FILEID_BTRFS_WITH_PARENT) {
  88. if (fh_len != BTRFS_FID_SIZE_CONNECTABLE)
  89. return NULL;
  90. root_objectid = fid->root_objectid;
  91. } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
  92. if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT)
  93. return NULL;
  94. root_objectid = fid->parent_root_objectid;
  95. } else
  96. return NULL;
  97. objectid = fid->parent_objectid;
  98. generation = fid->parent_gen;
  99. return btrfs_get_dentry(sb, objectid, root_objectid, generation);
  100. }
  101. static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
  102. int fh_len, int fh_type)
  103. {
  104. struct btrfs_fid *fid = (struct btrfs_fid *) fh;
  105. u64 objectid, root_objectid;
  106. u32 generation;
  107. if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
  108. fh_len != BTRFS_FID_SIZE_CONNECTABLE) &&
  109. (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
  110. fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
  111. (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
  112. fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE))
  113. return NULL;
  114. objectid = fid->objectid;
  115. root_objectid = fid->root_objectid;
  116. generation = fid->gen;
  117. return btrfs_get_dentry(sb, objectid, root_objectid, generation);
  118. }
  119. static struct dentry *btrfs_get_parent(struct dentry *child)
  120. {
  121. struct inode *dir = child->d_inode;
  122. struct inode *inode;
  123. struct dentry *parent;
  124. struct btrfs_root *root = BTRFS_I(dir)->root;
  125. struct btrfs_key key;
  126. struct btrfs_path *path;
  127. struct extent_buffer *leaf;
  128. int slot;
  129. u64 objectid;
  130. int ret;
  131. path = btrfs_alloc_path();
  132. key.objectid = dir->i_ino;
  133. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  134. key.offset = (u64)-1;
  135. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  136. leaf = path->nodes[0];
  137. slot = path->slots[0];
  138. if (ret < 0 || slot == 0) {
  139. btrfs_free_path(path);
  140. goto out;
  141. }
  142. /* btrfs_search_slot() returns the slot where we'd want to insert
  143. an INODE_REF_KEY for parent inode #0xFFFFFFFFFFFFFFFF. The _real_
  144. one, telling us what the parent inode _actually_ is, will be in
  145. the slot _before_ the one that btrfs_search_slot() returns. */
  146. slot--;
  147. btrfs_item_key_to_cpu(leaf, &key, slot);
  148. btrfs_free_path(path);
  149. if (key.objectid != dir->i_ino || key.type != BTRFS_INODE_REF_KEY)
  150. goto out;
  151. objectid = key.offset;
  152. /* If we are already at the root of a subvol, return the real root */
  153. if (objectid == dir->i_ino)
  154. return dget(dir->i_sb->s_root);
  155. /* Build a new key for the inode item */
  156. key.objectid = objectid;
  157. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  158. key.offset = 0;
  159. inode = btrfs_iget(root->fs_info->sb, &key, root, NULL);
  160. parent = d_obtain_alias(inode);
  161. if (!parent)
  162. parent = ERR_PTR(-ENOMEM);
  163. return parent;
  164. out:
  165. btrfs_free_path(path);
  166. return ERR_PTR(-EINVAL);
  167. }
  168. const struct export_operations btrfs_export_ops = {
  169. .encode_fh = btrfs_encode_fh,
  170. .fh_to_dentry = btrfs_fh_to_dentry,
  171. .fh_to_parent = btrfs_fh_to_parent,
  172. .get_parent = btrfs_get_parent,
  173. };