export.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "export.h"
  8. #include "accessors.h"
  9. #include "super.h"
  10. #define BTRFS_FID_SIZE_NON_CONNECTABLE (offsetof(struct btrfs_fid, \
  11. parent_objectid) / 4)
  12. #define BTRFS_FID_SIZE_CONNECTABLE (offsetof(struct btrfs_fid, \
  13. parent_root_objectid) / 4)
  14. #define BTRFS_FID_SIZE_CONNECTABLE_ROOT (sizeof(struct btrfs_fid) / 4)
  15. static int btrfs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
  16. struct inode *parent)
  17. {
  18. struct btrfs_fid *fid = (struct btrfs_fid *)fh;
  19. int len = *max_len;
  20. int type;
  21. if (parent && (len < BTRFS_FID_SIZE_CONNECTABLE)) {
  22. if (btrfs_root_id(BTRFS_I(inode)->root) !=
  23. btrfs_root_id(BTRFS_I(parent)->root))
  24. *max_len = BTRFS_FID_SIZE_CONNECTABLE_ROOT;
  25. else
  26. *max_len = BTRFS_FID_SIZE_CONNECTABLE;
  27. return FILEID_INVALID;
  28. } else if (len < BTRFS_FID_SIZE_NON_CONNECTABLE) {
  29. *max_len = BTRFS_FID_SIZE_NON_CONNECTABLE;
  30. return FILEID_INVALID;
  31. }
  32. len = BTRFS_FID_SIZE_NON_CONNECTABLE;
  33. type = FILEID_BTRFS_WITHOUT_PARENT;
  34. fid->objectid = btrfs_ino(BTRFS_I(inode));
  35. fid->root_objectid = btrfs_root_id(BTRFS_I(inode)->root);
  36. fid->gen = inode->i_generation;
  37. if (parent) {
  38. u64 parent_root_id;
  39. fid->parent_objectid = btrfs_ino(BTRFS_I(parent));
  40. fid->parent_gen = parent->i_generation;
  41. parent_root_id = btrfs_root_id(BTRFS_I(parent)->root);
  42. if (parent_root_id != fid->root_objectid) {
  43. if (*max_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT)
  44. return FILEID_INVALID;
  45. fid->parent_root_objectid = parent_root_id;
  46. len = BTRFS_FID_SIZE_CONNECTABLE_ROOT;
  47. type = FILEID_BTRFS_WITH_PARENT_ROOT;
  48. } else {
  49. len = BTRFS_FID_SIZE_CONNECTABLE;
  50. type = FILEID_BTRFS_WITH_PARENT;
  51. }
  52. }
  53. *max_len = len;
  54. return type;
  55. }
  56. /*
  57. * Read dentry of inode with @objectid from filesystem root @root_objectid.
  58. *
  59. * @sb: the filesystem super block
  60. * @objectid: inode objectid
  61. * @root_objectid: object id of the subvolume root where to look up the inode
  62. * @generation: optional, if not zero, verify that the found inode
  63. * generation matches
  64. *
  65. * Return dentry alias for the inode, otherwise an error. In case the
  66. * generation does not match return ESTALE.
  67. */
  68. struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
  69. u64 root_objectid, u64 generation)
  70. {
  71. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  72. struct btrfs_root *root;
  73. struct btrfs_inode *inode;
  74. if (objectid < BTRFS_FIRST_FREE_OBJECTID)
  75. return ERR_PTR(-ESTALE);
  76. root = btrfs_get_fs_root(fs_info, root_objectid, true);
  77. if (IS_ERR(root))
  78. return ERR_CAST(root);
  79. inode = btrfs_iget(objectid, root);
  80. btrfs_put_root(root);
  81. if (IS_ERR(inode))
  82. return ERR_CAST(inode);
  83. if (generation != 0 && generation != inode->vfs_inode.i_generation) {
  84. iput(&inode->vfs_inode);
  85. return ERR_PTR(-ESTALE);
  86. }
  87. return d_obtain_alias(&inode->vfs_inode);
  88. }
  89. static struct dentry *btrfs_fh_to_parent(struct super_block *sb, struct fid *fh,
  90. int fh_len, int fh_type)
  91. {
  92. struct btrfs_fid *fid = (struct btrfs_fid *) fh;
  93. u64 objectid, root_objectid;
  94. u32 generation;
  95. if (fh_type == FILEID_BTRFS_WITH_PARENT) {
  96. if (fh_len < BTRFS_FID_SIZE_CONNECTABLE)
  97. return NULL;
  98. root_objectid = fid->root_objectid;
  99. } else if (fh_type == FILEID_BTRFS_WITH_PARENT_ROOT) {
  100. if (fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT)
  101. return NULL;
  102. root_objectid = fid->parent_root_objectid;
  103. } else
  104. return NULL;
  105. objectid = fid->parent_objectid;
  106. generation = fid->parent_gen;
  107. return btrfs_get_dentry(sb, objectid, root_objectid, generation);
  108. }
  109. static struct dentry *btrfs_fh_to_dentry(struct super_block *sb, struct fid *fh,
  110. int fh_len, int fh_type)
  111. {
  112. struct btrfs_fid *fid = (struct btrfs_fid *) fh;
  113. u64 objectid, root_objectid;
  114. u32 generation;
  115. if ((fh_type != FILEID_BTRFS_WITH_PARENT ||
  116. fh_len < BTRFS_FID_SIZE_CONNECTABLE) &&
  117. (fh_type != FILEID_BTRFS_WITH_PARENT_ROOT ||
  118. fh_len < BTRFS_FID_SIZE_CONNECTABLE_ROOT) &&
  119. (fh_type != FILEID_BTRFS_WITHOUT_PARENT ||
  120. fh_len < BTRFS_FID_SIZE_NON_CONNECTABLE))
  121. return NULL;
  122. objectid = fid->objectid;
  123. root_objectid = fid->root_objectid;
  124. generation = fid->gen;
  125. return btrfs_get_dentry(sb, objectid, root_objectid, generation);
  126. }
  127. struct dentry *btrfs_get_parent(struct dentry *child)
  128. {
  129. struct btrfs_inode *dir = BTRFS_I(d_inode(child));
  130. struct btrfs_inode *inode;
  131. struct btrfs_root *root = dir->root;
  132. struct btrfs_fs_info *fs_info = root->fs_info;
  133. struct btrfs_path *path;
  134. struct extent_buffer *leaf;
  135. struct btrfs_root_ref *ref;
  136. struct btrfs_key key;
  137. struct btrfs_key found_key;
  138. int ret;
  139. path = btrfs_alloc_path();
  140. if (!path)
  141. return ERR_PTR(-ENOMEM);
  142. if (btrfs_ino(dir) == BTRFS_FIRST_FREE_OBJECTID) {
  143. key.objectid = btrfs_root_id(root);
  144. key.type = BTRFS_ROOT_BACKREF_KEY;
  145. key.offset = (u64)-1;
  146. root = fs_info->tree_root;
  147. } else {
  148. key.objectid = btrfs_ino(dir);
  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. if (unlikely(ret == 0)) {
  156. /*
  157. * Key with offset of -1 found, there would have to exist an
  158. * inode with such number or a root with such id.
  159. */
  160. ret = -EUCLEAN;
  161. goto fail;
  162. }
  163. if (path->slots[0] == 0) {
  164. ret = -ENOENT;
  165. goto fail;
  166. }
  167. path->slots[0]--;
  168. leaf = path->nodes[0];
  169. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  170. if (found_key.objectid != key.objectid || found_key.type != key.type) {
  171. ret = -ENOENT;
  172. goto fail;
  173. }
  174. if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
  175. ref = btrfs_item_ptr(leaf, path->slots[0],
  176. struct btrfs_root_ref);
  177. key.objectid = btrfs_root_ref_dirid(leaf, ref);
  178. } else {
  179. key.objectid = found_key.offset;
  180. }
  181. btrfs_free_path(path);
  182. if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
  183. return btrfs_get_dentry(fs_info->sb, key.objectid,
  184. found_key.offset, 0);
  185. }
  186. inode = btrfs_iget(key.objectid, root);
  187. if (IS_ERR(inode))
  188. return ERR_CAST(inode);
  189. return d_obtain_alias(&inode->vfs_inode);
  190. fail:
  191. btrfs_free_path(path);
  192. return ERR_PTR(ret);
  193. }
  194. static int btrfs_get_name(struct dentry *parent, char *name,
  195. struct dentry *child)
  196. {
  197. struct btrfs_inode *inode = BTRFS_I(d_inode(child));
  198. struct btrfs_inode *dir = BTRFS_I(d_inode(parent));
  199. struct btrfs_root *root = dir->root;
  200. struct btrfs_fs_info *fs_info = root->fs_info;
  201. BTRFS_PATH_AUTO_FREE(path);
  202. struct btrfs_inode_ref *iref;
  203. struct btrfs_root_ref *rref;
  204. struct extent_buffer *leaf;
  205. unsigned long name_ptr;
  206. struct btrfs_key key;
  207. int name_len;
  208. int ret;
  209. u64 ino;
  210. if (!S_ISDIR(dir->vfs_inode.i_mode))
  211. return -EINVAL;
  212. ino = btrfs_ino(inode);
  213. path = btrfs_alloc_path();
  214. if (!path)
  215. return -ENOMEM;
  216. if (ino == BTRFS_FIRST_FREE_OBJECTID) {
  217. key.objectid = btrfs_root_id(inode->root);
  218. key.type = BTRFS_ROOT_BACKREF_KEY;
  219. key.offset = (u64)-1;
  220. root = fs_info->tree_root;
  221. } else {
  222. key.objectid = ino;
  223. key.type = BTRFS_INODE_REF_KEY;
  224. key.offset = btrfs_ino(dir);
  225. }
  226. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  227. if (ret < 0) {
  228. return ret;
  229. } else if (ret > 0) {
  230. if (ino == BTRFS_FIRST_FREE_OBJECTID)
  231. path->slots[0]--;
  232. else
  233. return -ENOENT;
  234. }
  235. leaf = path->nodes[0];
  236. if (ino == BTRFS_FIRST_FREE_OBJECTID) {
  237. rref = btrfs_item_ptr(leaf, path->slots[0],
  238. struct btrfs_root_ref);
  239. name_ptr = (unsigned long)(rref + 1);
  240. name_len = btrfs_root_ref_name_len(leaf, rref);
  241. } else {
  242. iref = btrfs_item_ptr(leaf, path->slots[0],
  243. struct btrfs_inode_ref);
  244. name_ptr = (unsigned long)(iref + 1);
  245. name_len = btrfs_inode_ref_name_len(leaf, iref);
  246. }
  247. read_extent_buffer(leaf, name, name_ptr, name_len);
  248. /*
  249. * have to add the null termination to make sure that reconnect_path
  250. * gets the right len for strlen
  251. */
  252. name[name_len] = '\0';
  253. return 0;
  254. }
  255. const struct export_operations btrfs_export_ops = {
  256. .encode_fh = btrfs_encode_fh,
  257. .fh_to_dentry = btrfs_fh_to_dentry,
  258. .fh_to_parent = btrfs_fh_to_parent,
  259. .get_parent = btrfs_get_parent,
  260. .get_name = btrfs_get_name,
  261. };