export.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 (!path)
  143. return ERR_PTR(-ENOMEM);
  144. if (dir->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  145. key.objectid = root->root_key.objectid;
  146. key.type = BTRFS_ROOT_BACKREF_KEY;
  147. key.offset = (u64)-1;
  148. root = root->fs_info->tree_root;
  149. } else {
  150. key.objectid = dir->i_ino;
  151. key.type = BTRFS_INODE_REF_KEY;
  152. key.offset = (u64)-1;
  153. }
  154. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  155. if (ret < 0)
  156. goto fail;
  157. BUG_ON(ret == 0);
  158. if (path->slots[0] == 0) {
  159. ret = -ENOENT;
  160. goto fail;
  161. }
  162. path->slots[0]--;
  163. leaf = path->nodes[0];
  164. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  165. if (found_key.objectid != key.objectid || found_key.type != key.type) {
  166. ret = -ENOENT;
  167. goto fail;
  168. }
  169. if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
  170. ref = btrfs_item_ptr(leaf, path->slots[0],
  171. struct btrfs_root_ref);
  172. key.objectid = btrfs_root_ref_dirid(leaf, ref);
  173. } else {
  174. key.objectid = found_key.offset;
  175. }
  176. btrfs_free_path(path);
  177. if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
  178. return btrfs_get_dentry(root->fs_info->sb, key.objectid,
  179. found_key.offset, 0, 0);
  180. }
  181. key.type = BTRFS_INODE_ITEM_KEY;
  182. key.offset = 0;
  183. return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL));
  184. fail:
  185. btrfs_free_path(path);
  186. return ERR_PTR(ret);
  187. }
  188. static int btrfs_get_name(struct dentry *parent, char *name,
  189. struct dentry *child)
  190. {
  191. struct inode *inode = child->d_inode;
  192. struct inode *dir = parent->d_inode;
  193. struct btrfs_path *path;
  194. struct btrfs_root *root = BTRFS_I(dir)->root;
  195. struct btrfs_inode_ref *iref;
  196. struct btrfs_root_ref *rref;
  197. struct extent_buffer *leaf;
  198. unsigned long name_ptr;
  199. struct btrfs_key key;
  200. int name_len;
  201. int ret;
  202. if (!dir || !inode)
  203. return -EINVAL;
  204. if (!S_ISDIR(dir->i_mode))
  205. return -EINVAL;
  206. path = btrfs_alloc_path();
  207. if (!path)
  208. return -ENOMEM;
  209. path->leave_spinning = 1;
  210. if (inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  211. key.objectid = BTRFS_I(inode)->root->root_key.objectid;
  212. key.type = BTRFS_ROOT_BACKREF_KEY;
  213. key.offset = (u64)-1;
  214. root = root->fs_info->tree_root;
  215. } else {
  216. key.objectid = inode->i_ino;
  217. key.offset = dir->i_ino;
  218. key.type = BTRFS_INODE_REF_KEY;
  219. }
  220. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  221. if (ret < 0) {
  222. btrfs_free_path(path);
  223. return ret;
  224. } else if (ret > 0) {
  225. if (inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  226. path->slots[0]--;
  227. } else {
  228. btrfs_free_path(path);
  229. return -ENOENT;
  230. }
  231. }
  232. leaf = path->nodes[0];
  233. if (inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) {
  234. rref = btrfs_item_ptr(leaf, path->slots[0],
  235. struct btrfs_root_ref);
  236. name_ptr = (unsigned long)(rref + 1);
  237. name_len = btrfs_root_ref_name_len(leaf, rref);
  238. } else {
  239. iref = btrfs_item_ptr(leaf, path->slots[0],
  240. struct btrfs_inode_ref);
  241. name_ptr = (unsigned long)(iref + 1);
  242. name_len = btrfs_inode_ref_name_len(leaf, iref);
  243. }
  244. read_extent_buffer(leaf, name, name_ptr, name_len);
  245. btrfs_free_path(path);
  246. /*
  247. * have to add the null termination to make sure that reconnect_path
  248. * gets the right len for strlen
  249. */
  250. name[name_len] = '\0';
  251. return 0;
  252. }
  253. const struct export_operations btrfs_export_ops = {
  254. .encode_fh = btrfs_encode_fh,
  255. .fh_to_dentry = btrfs_fh_to_dentry,
  256. .fh_to_parent = btrfs_fh_to_parent,
  257. .get_parent = btrfs_get_parent,
  258. .get_name = btrfs_get_name,
  259. };