export.c 7.6 KB

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