export.c 5.3 KB

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