export.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 = inode->i_ino;
  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. int check_generation)
  53. {
  54. struct btrfs_fs_info *fs_info = btrfs_sb(sb)->fs_info;
  55. struct btrfs_root *root;
  56. struct inode *inode;
  57. struct btrfs_key key;
  58. int index;
  59. int err = 0;
  60. if (objectid < BTRFS_FIRST_FREE_OBJECTID)
  61. return ERR_PTR(-ESTALE);
  62. key.objectid = root_objectid;
  63. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  64. key.offset = (u64)-1;
  65. index = srcu_read_lock(&fs_info->subvol_srcu);
  66. root = btrfs_read_fs_root_no_name(fs_info, &key);
  67. if (IS_ERR(root)) {
  68. err = PTR_ERR(root);
  69. goto fail;
  70. }
  71. if (btrfs_root_refs(&root->root_item) == 0) {
  72. err = -ENOENT;
  73. goto fail;
  74. }
  75. key.objectid = objectid;
  76. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  77. key.offset = 0;
  78. inode = btrfs_iget(sb, &key, root, NULL);
  79. if (IS_ERR(inode)) {
  80. err = PTR_ERR(inode);
  81. goto fail;
  82. }
  83. srcu_read_unlock(&fs_info->subvol_srcu, index);
  84. if (check_generation && generation != inode->i_generation) {
  85. iput(inode);
  86. return ERR_PTR(-ESTALE);
  87. }
  88. return d_obtain_alias(inode);
  89. fail:
  90. srcu_read_unlock(&fs_info->subvol_srcu, index);
  91. return ERR_PTR(err);
  92. }
  93. static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
  94. int fh_len, int fh_type)
  95. {
  96. struct btrfs_fid *fid = (struct btrfs_fid *) fh;
  97. u64 objectid, root_objectid;
  98. u32 generation;
  99. if (fh_type == FILEID_BTRFS_WITH_PARENT) {
  100. if (fh_len != BTRFS_FID_SIZE_CONNECTABLE)
  101. return NULL;
  102. root_objectid = fid->root_objectid;
  103. } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
  104. if (fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT)
  105. return NULL;
  106. root_objectid = fid->parent_root_objectid;
  107. } else
  108. return NULL;
  109. objectid = fid->parent_objectid;
  110. generation = fid->parent_gen;
  111. return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
  112. }
  113. static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
  114. int fh_len, int fh_type)
  115. {
  116. struct btrfs_fid *fid = (struct btrfs_fid *) fh;
  117. u64 objectid, root_objectid;
  118. u32 generation;
  119. if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
  120. fh_len != BTRFS_FID_SIZE_CONNECTABLE) &&
  121. (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
  122. fh_len != BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
  123. (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
  124. fh_len != BTRFS_FID_SIZE_NON_CONNECTABLE))
  125. return NULL;
  126. objectid = fid->objectid;
  127. root_objectid = fid->root_objectid;
  128. generation = fid->gen;
  129. return btrfs_get_dentry(sb, objectid, root_objectid, generation, 1);
  130. }
  131. static struct dentry *btrfs_get_parent(struct dentry *child)
  132. {
  133. struct inode *dir = child->d_inode;
  134. struct btrfs_root *root = BTRFS_I(dir)->root;
  135. struct btrfs_path *path;
  136. struct extent_buffer *leaf;
  137. struct btrfs_root_ref *ref;
  138. struct btrfs_key key;
  139. struct btrfs_key found_key;
  140. int ret;
  141. path = btrfs_alloc_path();
  142. if (dir->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  143. key.objectid = root->root_key.objectid;
  144. key.type = BTRFS_ROOT_BACKREF_KEY;
  145. key.offset = (u64)-1;
  146. root = root->fs_info->tree_root;
  147. } else {
  148. key.objectid = dir->i_ino;
  149. key.type = BTRFS_INODE_REF_KEY;
  150. key.offset = (u64)-1;
  151. }
  152. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  153. if (ret < 0)
  154. goto fail;
  155. BUG_ON(ret == 0);
  156. if (path->slots[0] == 0) {
  157. ret = -ENOENT;
  158. goto fail;
  159. }
  160. path->slots[0]--;
  161. leaf = path->nodes[0];
  162. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  163. if (found_key.objectid != key.objectid || found_key.type != key.type) {
  164. ret = -ENOENT;
  165. goto fail;
  166. }
  167. if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
  168. ref = btrfs_item_ptr(leaf, path->slots[0],
  169. struct btrfs_root_ref);
  170. key.objectid = btrfs_root_ref_dirid(leaf, ref);
  171. } else {
  172. key.objectid = found_key.offset;
  173. }
  174. btrfs_free_path(path);
  175. if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
  176. return btrfs_get_dentry(root->fs_info->sb, key.objectid,
  177. found_key.offset, 0, 0);
  178. }
  179. key.type = BTRFS_INODE_ITEM_KEY;
  180. key.offset = 0;
  181. return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL));
  182. fail:
  183. btrfs_free_path(path);
  184. return ERR_PTR(ret);
  185. }
  186. static int btrfs_get_name(struct dentry *parent, char *name,
  187. struct dentry *child)
  188. {
  189. struct inode *inode = child->d_inode;
  190. struct inode *dir = parent->d_inode;
  191. struct btrfs_path *path;
  192. struct btrfs_root *root = BTRFS_I(dir)->root;
  193. struct btrfs_inode_ref *iref;
  194. struct btrfs_root_ref *rref;
  195. struct extent_buffer *leaf;
  196. unsigned long name_ptr;
  197. struct btrfs_key key;
  198. int name_len;
  199. int ret;
  200. if (!dir || !inode)
  201. return -EINVAL;
  202. if (!S_ISDIR(dir->i_mode))
  203. return -EINVAL;
  204. path = btrfs_alloc_path();
  205. if (!path)
  206. return -ENOMEM;
  207. path->leave_spinning = 1;
  208. if (inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  209. key.objectid = BTRFS_I(inode)->root->root_key.objectid;
  210. key.type = BTRFS_ROOT_BACKREF_KEY;
  211. key.offset = (u64)-1;
  212. root = root->fs_info->tree_root;
  213. } else {
  214. key.objectid = inode->i_ino;
  215. key.offset = dir->i_ino;
  216. key.type = BTRFS_INODE_REF_KEY;
  217. }
  218. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  219. if (ret < 0) {
  220. btrfs_free_path(path);
  221. return ret;
  222. } else if (ret > 0) {
  223. if (inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  224. path->slots[0]--;
  225. } else {
  226. btrfs_free_path(path);
  227. return -ENOENT;
  228. }
  229. }
  230. leaf = path->nodes[0];
  231. if (inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  232. rref = btrfs_item_ptr(leaf, path->slots[0],
  233. struct btrfs_root_ref);
  234. name_ptr = (unsigned long)(rref + 1);
  235. name_len = btrfs_root_ref_name_len(leaf, rref);
  236. } else {
  237. iref = btrfs_item_ptr(leaf, path->slots[0],
  238. struct btrfs_inode_ref);
  239. name_ptr = (unsigned long)(iref + 1);
  240. name_len = btrfs_inode_ref_name_len(leaf, iref);
  241. }
  242. read_extent_buffer(leaf, name, name_ptr, name_len);
  243. btrfs_free_path(path);
  244. /*
  245. * have to add the null termination to make sure that reconnect_path
  246. * gets the right len for strlen
  247. */
  248. name[name_len] = '\0';
  249. return 0;
  250. }
  251. const struct export_operations btrfs_export_ops = {
  252. .encode_fh = btrfs_encode_fh,
  253. .fh_to_dentry = btrfs_fh_to_dentry,
  254. .fh_to_parent = btrfs_fh_to_parent,
  255. .get_parent = btrfs_get_parent,
  256. .get_name = btrfs_get_name,
  257. };