export.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 (connectable && (len < BTRFS_FID_SIZE_CONNECTABLE)) {
  22. *max_len = BTRFS_FID_SIZE_CONNECTABLE;
  23. return 255;
  24. } else if (len < BTRFS_FID_SIZE_NON_CONNECTABLE) {
  25. *max_len = BTRFS_FID_SIZE_NON_CONNECTABLE;
  26. return 255;
  27. }
  28. len = BTRFS_FID_SIZE_NON_CONNECTABLE;
  29. type = FILEID_BTRFS_WITHOUT_PARENT;
  30. fid->objectid = btrfs_ino(inode);
  31. fid->root_objectid = BTRFS_I(inode)->root->objectid;
  32. fid->gen = inode->i_generation;
  33. if (connectable && !S_ISDIR(inode->i_mode)) {
  34. struct inode *parent;
  35. u64 parent_root_id;
  36. spin_lock(&dentry->d_lock);
  37. parent = dentry->d_parent->d_inode;
  38. fid->parent_objectid = BTRFS_I(parent)->location.objectid;
  39. fid->parent_gen = parent->i_generation;
  40. parent_root_id = BTRFS_I(parent)->root->objectid;
  41. spin_unlock(&dentry->d_lock);
  42. if (parent_root_id != fid->root_objectid) {
  43. fid->parent_root_objectid = parent_root_id;
  44. len = BTRFS_FID_SIZE_CONNECTABLE_ROOT;
  45. type = FILEID_BTRFS_WITH_PARENT_ROOT;
  46. } else {
  47. len = BTRFS_FID_SIZE_CONNECTABLE;
  48. type = FILEID_BTRFS_WITH_PARENT;
  49. }
  50. }
  51. *max_len = len;
  52. return type;
  53. }
  54. static struct dentry *btrfs_get_dentry(struct super_block *sb, u64 objectid,
  55. u64 root_objectid, u32 generation,
  56. int check_generation)
  57. {
  58. struct btrfs_fs_info *fs_info = btrfs_sb(sb)->fs_info;
  59. struct btrfs_root *root;
  60. struct inode *inode;
  61. struct btrfs_key key;
  62. int index;
  63. int err = 0;
  64. if (objectid < BTRFS_FIRST_FREE_OBJECTID)
  65. return ERR_PTR(-ESTALE);
  66. key.objectid = root_objectid;
  67. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  68. key.offset = (u64)-1;
  69. index = srcu_read_lock(&fs_info->subvol_srcu);
  70. root = btrfs_read_fs_root_no_name(fs_info, &key);
  71. if (IS_ERR(root)) {
  72. err = PTR_ERR(root);
  73. goto fail;
  74. }
  75. if (btrfs_root_refs(&root->root_item) == 0) {
  76. err = -ENOENT;
  77. goto fail;
  78. }
  79. key.objectid = objectid;
  80. btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
  81. key.offset = 0;
  82. inode = btrfs_iget(sb, &key, root, NULL);
  83. if (IS_ERR(inode)) {
  84. err = PTR_ERR(inode);
  85. goto fail;
  86. }
  87. srcu_read_unlock(&fs_info->subvol_srcu, index);
  88. if (check_generation && generation != inode->i_generation) {
  89. iput(inode);
  90. return ERR_PTR(-ESTALE);
  91. }
  92. return d_obtain_alias(inode);
  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 btrfs_root *root = BTRFS_I(dir)->root;
  139. struct btrfs_path *path;
  140. struct extent_buffer *leaf;
  141. struct btrfs_root_ref *ref;
  142. struct btrfs_key key;
  143. struct btrfs_key found_key;
  144. int ret;
  145. path = btrfs_alloc_path();
  146. if (!path)
  147. return ERR_PTR(-ENOMEM);
  148. if (btrfs_ino(dir) == BTRFS_FIRST_FREE_OBJECTID) {
  149. key.objectid = root->root_key.objectid;
  150. key.type = BTRFS_ROOT_BACKREF_KEY;
  151. key.offset = (u64)-1;
  152. root = root->fs_info->tree_root;
  153. } else {
  154. key.objectid = btrfs_ino(dir);
  155. key.type = BTRFS_INODE_REF_KEY;
  156. key.offset = (u64)-1;
  157. }
  158. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  159. if (ret < 0)
  160. goto fail;
  161. BUG_ON(ret == 0);
  162. if (path->slots[0] == 0) {
  163. ret = -ENOENT;
  164. goto fail;
  165. }
  166. path->slots[0]--;
  167. leaf = path->nodes[0];
  168. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  169. if (found_key.objectid != key.objectid || found_key.type != key.type) {
  170. ret = -ENOENT;
  171. goto fail;
  172. }
  173. if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
  174. ref = btrfs_item_ptr(leaf, path->slots[0],
  175. struct btrfs_root_ref);
  176. key.objectid = btrfs_root_ref_dirid(leaf, ref);
  177. } else {
  178. key.objectid = found_key.offset;
  179. }
  180. btrfs_free_path(path);
  181. if (found_key.type == BTRFS_ROOT_BACKREF_KEY) {
  182. return btrfs_get_dentry(root->fs_info->sb, key.objectid,
  183. found_key.offset, 0, 0);
  184. }
  185. key.type = BTRFS_INODE_ITEM_KEY;
  186. key.offset = 0;
  187. return d_obtain_alias(btrfs_iget(root->fs_info->sb, &key, root, NULL));
  188. fail:
  189. btrfs_free_path(path);
  190. return ERR_PTR(ret);
  191. }
  192. static int btrfs_get_name(struct dentry *parent, char *name,
  193. struct dentry *child)
  194. {
  195. struct inode *inode = child->d_inode;
  196. struct inode *dir = parent->d_inode;
  197. struct btrfs_path *path;
  198. struct btrfs_root *root = BTRFS_I(dir)->root;
  199. struct btrfs_inode_ref *iref;
  200. struct btrfs_root_ref *rref;
  201. struct extent_buffer *leaf;
  202. unsigned long name_ptr;
  203. struct btrfs_key key;
  204. int name_len;
  205. int ret;
  206. u64 ino;
  207. if (!dir || !inode)
  208. return -EINVAL;
  209. if (!S_ISDIR(dir->i_mode))
  210. return -EINVAL;
  211. ino = btrfs_ino(inode);
  212. path = btrfs_alloc_path();
  213. if (!path)
  214. return -ENOMEM;
  215. path->leave_spinning = 1;
  216. if (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 = ino;
  223. key.offset = btrfs_ino(dir);
  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 (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 (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. };