export.c 7.8 KB

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