export.c 7.6 KB

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