root-tree.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright (C) 2007 Oracle. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include "ctree.h"
  19. #include "transaction.h"
  20. #include "disk-io.h"
  21. #include "print-tree.h"
  22. /*
  23. * lookup the root with the highest offset for a given objectid. The key we do
  24. * find is copied into 'key'. If we find something return 0, otherwise 1, < 0
  25. * on error.
  26. */
  27. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
  28. struct btrfs_root_item *item, struct btrfs_key *key)
  29. {
  30. struct btrfs_path *path;
  31. struct btrfs_key search_key;
  32. struct btrfs_key found_key;
  33. struct extent_buffer *l;
  34. int ret;
  35. int slot;
  36. search_key.objectid = objectid;
  37. search_key.type = BTRFS_ROOT_ITEM_KEY;
  38. search_key.offset = (u64)-1;
  39. path = btrfs_alloc_path();
  40. if (!path)
  41. return -ENOMEM;
  42. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  43. if (ret < 0)
  44. goto out;
  45. BUG_ON(ret == 0);
  46. if (path->slots[0] == 0) {
  47. ret = 1;
  48. goto out;
  49. }
  50. l = path->nodes[0];
  51. slot = path->slots[0] - 1;
  52. btrfs_item_key_to_cpu(l, &found_key, slot);
  53. if (found_key.objectid != objectid ||
  54. found_key.type != BTRFS_ROOT_ITEM_KEY) {
  55. ret = 1;
  56. goto out;
  57. }
  58. if (item)
  59. read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
  60. sizeof(*item));
  61. if (key)
  62. memcpy(key, &found_key, sizeof(found_key));
  63. ret = 0;
  64. out:
  65. btrfs_free_path(path);
  66. return ret;
  67. }
  68. int btrfs_set_root_node(struct btrfs_root_item *item,
  69. struct extent_buffer *node)
  70. {
  71. btrfs_set_root_bytenr(item, node->start);
  72. btrfs_set_root_level(item, btrfs_header_level(node));
  73. btrfs_set_root_generation(item, btrfs_header_generation(node));
  74. return 0;
  75. }
  76. /*
  77. * copy the data in 'item' into the btree
  78. */
  79. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  80. *root, struct btrfs_key *key, struct btrfs_root_item
  81. *item)
  82. {
  83. struct btrfs_path *path;
  84. struct extent_buffer *l;
  85. int ret;
  86. int slot;
  87. unsigned long ptr;
  88. path = btrfs_alloc_path();
  89. BUG_ON(!path);
  90. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  91. if (ret < 0)
  92. goto out;
  93. if (ret != 0) {
  94. btrfs_print_leaf(root, path->nodes[0]);
  95. printk(KERN_CRIT "unable to update root key %llu %u %llu\n",
  96. (unsigned long long)key->objectid, key->type,
  97. (unsigned long long)key->offset);
  98. BUG_ON(1);
  99. }
  100. l = path->nodes[0];
  101. slot = path->slots[0];
  102. ptr = btrfs_item_ptr_offset(l, slot);
  103. write_extent_buffer(l, item, ptr, sizeof(*item));
  104. btrfs_mark_buffer_dirty(path->nodes[0]);
  105. out:
  106. btrfs_free_path(path);
  107. return ret;
  108. }
  109. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  110. *root, struct btrfs_key *key, struct btrfs_root_item
  111. *item)
  112. {
  113. int ret;
  114. ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
  115. return ret;
  116. }
  117. /*
  118. * at mount time we want to find all the old transaction snapshots that were in
  119. * the process of being deleted if we crashed. This is any root item with an
  120. * offset lower than the latest root. They need to be queued for deletion to
  121. * finish what was happening when we crashed.
  122. */
  123. int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid)
  124. {
  125. struct btrfs_root *dead_root;
  126. struct btrfs_root_item *ri;
  127. struct btrfs_key key;
  128. struct btrfs_key found_key;
  129. struct btrfs_path *path;
  130. int ret;
  131. u32 nritems;
  132. struct extent_buffer *leaf;
  133. int slot;
  134. key.objectid = objectid;
  135. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  136. key.offset = 0;
  137. path = btrfs_alloc_path();
  138. if (!path)
  139. return -ENOMEM;
  140. again:
  141. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  142. if (ret < 0)
  143. goto err;
  144. while (1) {
  145. leaf = path->nodes[0];
  146. nritems = btrfs_header_nritems(leaf);
  147. slot = path->slots[0];
  148. if (slot >= nritems) {
  149. ret = btrfs_next_leaf(root, path);
  150. if (ret)
  151. break;
  152. leaf = path->nodes[0];
  153. nritems = btrfs_header_nritems(leaf);
  154. slot = path->slots[0];
  155. }
  156. btrfs_item_key_to_cpu(leaf, &key, slot);
  157. if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
  158. goto next;
  159. if (key.objectid < objectid)
  160. goto next;
  161. if (key.objectid > objectid)
  162. break;
  163. ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
  164. if (btrfs_disk_root_refs(leaf, ri) != 0)
  165. goto next;
  166. memcpy(&found_key, &key, sizeof(key));
  167. key.offset++;
  168. btrfs_release_path(path);
  169. dead_root =
  170. btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  171. &found_key);
  172. if (IS_ERR(dead_root)) {
  173. ret = PTR_ERR(dead_root);
  174. goto err;
  175. }
  176. ret = btrfs_add_dead_root(dead_root);
  177. if (ret)
  178. goto err;
  179. goto again;
  180. next:
  181. slot++;
  182. path->slots[0]++;
  183. }
  184. ret = 0;
  185. err:
  186. btrfs_free_path(path);
  187. return ret;
  188. }
  189. int btrfs_find_orphan_roots(struct btrfs_root *tree_root)
  190. {
  191. struct extent_buffer *leaf;
  192. struct btrfs_path *path;
  193. struct btrfs_key key;
  194. struct btrfs_key root_key;
  195. struct btrfs_root *root;
  196. int err = 0;
  197. int ret;
  198. path = btrfs_alloc_path();
  199. if (!path)
  200. return -ENOMEM;
  201. key.objectid = BTRFS_ORPHAN_OBJECTID;
  202. key.type = BTRFS_ORPHAN_ITEM_KEY;
  203. key.offset = 0;
  204. root_key.type = BTRFS_ROOT_ITEM_KEY;
  205. root_key.offset = (u64)-1;
  206. while (1) {
  207. ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
  208. if (ret < 0) {
  209. err = ret;
  210. break;
  211. }
  212. leaf = path->nodes[0];
  213. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  214. ret = btrfs_next_leaf(tree_root, path);
  215. if (ret < 0)
  216. err = ret;
  217. if (ret != 0)
  218. break;
  219. leaf = path->nodes[0];
  220. }
  221. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  222. btrfs_release_path(path);
  223. if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
  224. key.type != BTRFS_ORPHAN_ITEM_KEY)
  225. break;
  226. root_key.objectid = key.offset;
  227. key.offset++;
  228. root = btrfs_read_fs_root_no_name(tree_root->fs_info,
  229. &root_key);
  230. if (!IS_ERR(root))
  231. continue;
  232. ret = PTR_ERR(root);
  233. if (ret != -ENOENT) {
  234. err = ret;
  235. break;
  236. }
  237. ret = btrfs_find_dead_roots(tree_root, root_key.objectid);
  238. if (ret) {
  239. err = ret;
  240. break;
  241. }
  242. }
  243. btrfs_free_path(path);
  244. return err;
  245. }
  246. /* drop the root item for 'key' from 'root' */
  247. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  248. struct btrfs_key *key)
  249. {
  250. struct btrfs_path *path;
  251. int ret;
  252. struct btrfs_root_item *ri;
  253. struct extent_buffer *leaf;
  254. path = btrfs_alloc_path();
  255. if (!path)
  256. return -ENOMEM;
  257. ret = btrfs_search_slot(trans, root, key, path, -1, 1);
  258. if (ret < 0)
  259. goto out;
  260. BUG_ON(ret != 0);
  261. leaf = path->nodes[0];
  262. ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
  263. ret = btrfs_del_item(trans, root, path);
  264. out:
  265. btrfs_free_path(path);
  266. return ret;
  267. }
  268. int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
  269. struct btrfs_root *tree_root,
  270. u64 root_id, u64 ref_id, u64 dirid, u64 *sequence,
  271. const char *name, int name_len)
  272. {
  273. struct btrfs_path *path;
  274. struct btrfs_root_ref *ref;
  275. struct extent_buffer *leaf;
  276. struct btrfs_key key;
  277. unsigned long ptr;
  278. int err = 0;
  279. int ret;
  280. path = btrfs_alloc_path();
  281. if (!path)
  282. return -ENOMEM;
  283. key.objectid = root_id;
  284. key.type = BTRFS_ROOT_BACKREF_KEY;
  285. key.offset = ref_id;
  286. again:
  287. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  288. BUG_ON(ret < 0);
  289. if (ret == 0) {
  290. leaf = path->nodes[0];
  291. ref = btrfs_item_ptr(leaf, path->slots[0],
  292. struct btrfs_root_ref);
  293. WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid);
  294. WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len);
  295. ptr = (unsigned long)(ref + 1);
  296. WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len));
  297. *sequence = btrfs_root_ref_sequence(leaf, ref);
  298. ret = btrfs_del_item(trans, tree_root, path);
  299. if (ret) {
  300. err = ret;
  301. goto out;
  302. }
  303. } else
  304. err = -ENOENT;
  305. if (key.type == BTRFS_ROOT_BACKREF_KEY) {
  306. btrfs_release_path(path);
  307. key.objectid = ref_id;
  308. key.type = BTRFS_ROOT_REF_KEY;
  309. key.offset = root_id;
  310. goto again;
  311. }
  312. out:
  313. btrfs_free_path(path);
  314. return err;
  315. }
  316. int btrfs_find_root_ref(struct btrfs_root *tree_root,
  317. struct btrfs_path *path,
  318. u64 root_id, u64 ref_id)
  319. {
  320. struct btrfs_key key;
  321. int ret;
  322. key.objectid = root_id;
  323. key.type = BTRFS_ROOT_REF_KEY;
  324. key.offset = ref_id;
  325. ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
  326. return ret;
  327. }
  328. /*
  329. * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
  330. * or BTRFS_ROOT_BACKREF_KEY.
  331. *
  332. * The dirid, sequence, name and name_len refer to the directory entry
  333. * that is referencing the root.
  334. *
  335. * For a forward ref, the root_id is the id of the tree referencing
  336. * the root and ref_id is the id of the subvol or snapshot.
  337. *
  338. * For a back ref the root_id is the id of the subvol or snapshot and
  339. * ref_id is the id of the tree referencing it.
  340. */
  341. int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
  342. struct btrfs_root *tree_root,
  343. u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
  344. const char *name, int name_len)
  345. {
  346. struct btrfs_key key;
  347. int ret;
  348. struct btrfs_path *path;
  349. struct btrfs_root_ref *ref;
  350. struct extent_buffer *leaf;
  351. unsigned long ptr;
  352. path = btrfs_alloc_path();
  353. if (!path)
  354. return -ENOMEM;
  355. key.objectid = root_id;
  356. key.type = BTRFS_ROOT_BACKREF_KEY;
  357. key.offset = ref_id;
  358. again:
  359. ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
  360. sizeof(*ref) + name_len);
  361. BUG_ON(ret);
  362. leaf = path->nodes[0];
  363. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  364. btrfs_set_root_ref_dirid(leaf, ref, dirid);
  365. btrfs_set_root_ref_sequence(leaf, ref, sequence);
  366. btrfs_set_root_ref_name_len(leaf, ref, name_len);
  367. ptr = (unsigned long)(ref + 1);
  368. write_extent_buffer(leaf, name, ptr, name_len);
  369. btrfs_mark_buffer_dirty(leaf);
  370. if (key.type == BTRFS_ROOT_BACKREF_KEY) {
  371. btrfs_release_path(path);
  372. key.objectid = ref_id;
  373. key.type = BTRFS_ROOT_REF_KEY;
  374. key.offset = root_id;
  375. goto again;
  376. }
  377. btrfs_free_path(path);
  378. return 0;
  379. }
  380. /*
  381. * Old btrfs forgets to init root_item->flags and root_item->byte_limit
  382. * for subvolumes. To work around this problem, we steal a bit from
  383. * root_item->inode_item->flags, and use it to indicate if those fields
  384. * have been properly initialized.
  385. */
  386. void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
  387. {
  388. u64 inode_flags = le64_to_cpu(root_item->inode.flags);
  389. if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
  390. inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
  391. root_item->inode.flags = cpu_to_le64(inode_flags);
  392. root_item->flags = 0;
  393. root_item->byte_limit = 0;
  394. }
  395. }