export.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \
  10. parent_objectid) / 4)
  11. #define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, \
  12. parent_root_objectid) / 4)
  13. #define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid) / 4)
  14. static int btrfs_encode_fh(struct dentry *dentry, u32 *fh, int *max_len,
  15. int connectable)
  16. {
  17. struct btrfs_fid *fid = (struct btrfs_fid *)fh;
  18. struct inode *inode = dentry->d_inode;
  19. int len = *max_len;
  20. int type;
  21. if ((len < BTRFS_FID_SIZE_NON_CONNECTABLE) ||
  22. (connectable && len < BTRFS_FID_SIZE_CONNECTABLE))
  23. return 255;
  24. len = BTRFS_FID_SIZE_NON_CONNECTABLE;
  25. type = FILEID_BTRFS_WITHOUT_PARENT;
  26. fid->objectid = BTRFS_I(inode)->location.objectid;
  27. fid->root_objectid = BTRFS_I(inode)->root->objectid;
  28. fid->gen = inode->i_generation;
  29. if (connectable && !S_ISDIR(inode->i_mode)) {
  30. struct inode *parent;
  31. u64 parent_root_id;
  32. spin_lock(&dentry->d_lock);
  33. parent = dentry->d_parent->d_inode;
  34. fid->parent_objectid = BTRFS_I(parent)->location.objectid;
  35. fid->parent_gen = parent->i_generation;
  36. parent_root_id = BTRFS_I(parent)->root->objectid;
  37. spin_unlock(&dentry->d_lock);
  38. if (parent_root_id != fid->root_objectid) {
  39. fid->parent_root_objectid = parent_root_id;
  40. len = BTRFS_FID_SIZE_CONNECTABLE_ROOT;
  41. type = FILEID_BTRFS_WITH_PARENT_ROOT;
  42. } else {
  43. len = BTRFS_FID_SIZE_CONNECTABLE;
  44. type = FILEID_BTRFS_WITH_PARENT;
  45. }
  46. }
  47. *max_len = len;
  48. return type;
  49. }
  50. static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
  51. u64 root_objectid, u32 generation)
  52. {
  53. struct btrfs_root *root;
  54. struct inode *inode;
  55. struct btrfs_key key;
  56. key.objectid = root_objectid;
  57. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  58. key.offset = (u64)-1;
  59. root = btrfs_read_fs_root_no_name(btrfs_sb(sb)->fs_info, &key);
  60. if (IS_ERR(root))
  61. return ERR_CAST(root);
  62. key.objectid = objectid;
  63. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  64. key.offset = 0;
  65. inode = btrfs_iget(sb, &key, root);
  66. if (IS_ERR(inode))
  67. return (void *)inode;
  68. if (generation != inode->i_generation) {
  69. iput(inode);
  70. return ERR_PTR(-ESTALE);
  71. }
  72. return d_obtain_alias(inode);
  73. }
  74. static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
  75. int fh_len, int fh_type)
  76. {
  77. struct btrfs_fid *fid = (struct btrfs_fid *) fh;
  78. u64 objectid, root_objectid;
  79. u32 generation;
  80. if (fh_type == FILEID_BTRFS_WITH_PARENT) {
  81. if (fh_len != BTRFS_FID_SIZE_CONNECTABLE)
  82. return NULL;
  83. root_objectid = fid->root_objectid;
  84. } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
  85. if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT)
  86. return NULL;
  87. root_objectid = fid->parent_root_objectid;
  88. } else
  89. return NULL;
  90. objectid = fid->parent_objectid;
  91. generation = fid->parent_gen;
  92. return btrfs_get_dentry(sb, objectid, root_objectid, generation);
  93. }
  94. static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
  95. int fh_len, int fh_type)
  96. {
  97. struct btrfs_fid *fid = (struct btrfs_fid *) fh;
  98. u64 objectid, root_objectid;
  99. u32 generation;
  100. if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
  101. fh_len != BTRFS_FID_SIZE_CONNECTABLE) &&
  102. (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
  103. fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
  104. (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
  105. fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE))
  106. return NULL;
  107. objectid = fid->objectid;
  108. root_objectid = fid->root_objectid;
  109. generation = fid->gen;
  110. return btrfs_get_dentry(sb, objectid, root_objectid, generation);
  111. }
  112. static struct dentry *btrfs_get_parent(struct dentry *child)
  113. {
  114. struct inode *dir = child->d_inode;
  115. struct btrfs_root *root = BTRFS_I(dir)->root;
  116. struct btrfs_key key;
  117. struct btrfs_path *path;
  118. struct extent_buffer *leaf;
  119. int slot;
  120. u64 objectid;
  121. int ret;
  122. path = btrfs_alloc_path();
  123. key.objectid = dir->i_ino;
  124. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  125. key.offset = (u64)-1;
  126. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  127. if (ret < 0) {
  128. /* Error */
  129. btrfs_free_path(path);
  130. return ERR_PTR(ret);
  131. }
  132. leaf = path->nodes[0];
  133. slot = path->slots[0];
  134. if (ret) {
  135. /* btrfs_search_slot() returns the slot where we'd want to
  136. insert a backref for parent inode #0xFFFFFFFFFFFFFFFF.
  137. The _real_ backref, telling us what the parent inode
  138. _actually_ is, will be in the slot _before_ the one
  139. that btrfs_search_slot() returns. */
  140. if (!slot) {
  141. /* Unless there is _no_ key in the tree before... */
  142. btrfs_free_path(path);
  143. return ERR_PTR(-EIO);
  144. }
  145. slot--;
  146. }
  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. return ERR_PTR(-EINVAL);
  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. return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root));
  160. }
  161. const struct export_operations btrfs_export_ops = {
  162. .encode_fh = btrfs_encode_fh,
  163. .fh_to_dentry = btrfs_fh_to_dentry,
  164. .fh_to_parent = btrfs_fh_to_parent,
  165. .get_parent = btrfs_get_parent,
  166. };