export.c 7.6 KB

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