export.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 btrfs_key key;
  59. key.objectid = root_objectid;
  60. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  61. key.offset = (u64)-1;
  62. root = btrfs_read_fs_root_no_name(btrfs_sb(sb)->fs_info, &key);
  63. if (IS_ERR(root))
  64. return ERR_CAST(root);
  65. key.objectid = objectid;
  66. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  67. key.offset = 0;
  68. inode = btrfs_iget(sb, &key, root, NULL);
  69. if (IS_ERR(inode))
  70. return (void *)inode;
  71. if (generation != inode->i_generation) {
  72. iput(inode);
  73. return ERR_PTR(-ESTALE);
  74. }
  75. return d_obtain_alias(inode);
  76. }
  77. static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
  78. int fh_len, int fh_type)
  79. {
  80. struct btrfs_fid *fid = (struct btrfs_fid *) fh;
  81. u64 objectid, root_objectid;
  82. u32 generation;
  83. if (fh_type == FILEID_BTRFS_WITH_PARENT) {
  84. if (fh_len != BTRFS_FID_SIZE_CONNECTABLE)
  85. return NULL;
  86. root_objectid = fid->root_objectid;
  87. } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
  88. if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT)
  89. return NULL;
  90. root_objectid = fid->parent_root_objectid;
  91. } else
  92. return NULL;
  93. objectid = fid->parent_objectid;
  94. generation = fid->parent_gen;
  95. return btrfs_get_dentry(sb, objectid, root_objectid, generation);
  96. }
  97. static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
  98. int fh_len, int fh_type)
  99. {
  100. struct btrfs_fid *fid = (struct btrfs_fid *) fh;
  101. u64 objectid, root_objectid;
  102. u32 generation;
  103. if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
  104. fh_len != BTRFS_FID_SIZE_CONNECTABLE) &&
  105. (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
  106. fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
  107. (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
  108. fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE))
  109. return NULL;
  110. objectid = fid->objectid;
  111. root_objectid = fid->root_objectid;
  112. generation = fid->gen;
  113. return btrfs_get_dentry(sb, objectid, root_objectid, generation);
  114. }
  115. static struct dentry *btrfs_get_parent(struct dentry *child)
  116. {
  117. struct inode *dir = child->d_inode;
  118. struct btrfs_root *root = BTRFS_I(dir)->root;
  119. struct btrfs_key key;
  120. struct btrfs_path *path;
  121. struct extent_buffer *leaf;
  122. int slot;
  123. u64 objectid;
  124. int ret;
  125. path = btrfs_alloc_path();
  126. key.objectid = dir->i_ino;
  127. btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
  128. key.offset = (u64)-1;
  129. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  130. if (ret < 0) {
  131. /* Error */
  132. btrfs_free_path(path);
  133. return ERR_PTR(ret);
  134. }
  135. leaf = path->nodes[0];
  136. slot = path->slots[0];
  137. if (ret) {
  138. /* btrfs_search_slot() returns the slot where we'd want to
  139. insert a backref for parent inode #0xFFFFFFFFFFFFFFFF.
  140. The _real_ backref, telling us what the parent inode
  141. _actually_ is, will be in the slot _before_ the one
  142. that btrfs_search_slot() returns. */
  143. if (!slot) {
  144. /* Unless there is _no_ key in the tree before... */
  145. btrfs_free_path(path);
  146. return ERR_PTR(-EIO);
  147. }
  148. slot--;
  149. }
  150. btrfs_item_key_to_cpu(leaf, &key, slot);
  151. btrfs_free_path(path);
  152. if (key.objectid != dir->i_ino || key.type != BTRFS_INODE_REF_KEY)
  153. return ERR_PTR(-EINVAL);
  154. objectid = key.offset;
  155. /* If we are already at the root of a subvol, return the real root */
  156. if (objectid == dir->i_ino)
  157. return dget(dir->i_sb->s_root);
  158. /* Build a new key for the inode item */
  159. key.objectid = objectid;
  160. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  161. key.offset = 0;
  162. return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL));
  163. }
  164. const struct export_operations btrfs_export_ops = {
  165. .encode_fh = btrfs_encode_fh,
  166. .fh_to_dentry = btrfs_fh_to_dentry,
  167. .fh_to_parent = btrfs_fh_to_parent,
  168. .get_parent = btrfs_get_parent,
  169. };