export.c 7.5 KB

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