root-tree.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. * search forward for a root, starting with objectid 'search_start'
  24. * if a root key is found, the objectid we find is filled into 'found_objectid'
  25. * and 0 is returned. < 0 is returned on error, 1 if there is nothing
  26. * left in the tree.
  27. */
  28. int btrfs_search_root(struct btrfs_root *root, u64 search_start,
  29. u64 *found_objectid)
  30. {
  31. struct btrfs_path *path;
  32. struct btrfs_key search_key;
  33. int ret;
  34. root = root->fs_info->tree_root;
  35. search_key.objectid = search_start;
  36. search_key.type = (u8)-1;
  37. search_key.offset = (u64)-1;
  38. path = btrfs_alloc_path();
  39. BUG_ON(!path);
  40. again:
  41. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  42. if (ret < 0)
  43. goto out;
  44. if (ret == 0) {
  45. ret = 1;
  46. goto out;
  47. }
  48. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  49. ret = btrfs_next_leaf(root, path);
  50. if (ret)
  51. goto out;
  52. }
  53. btrfs_item_key_to_cpu(path->nodes[0], &search_key, path->slots[0]);
  54. if (search_key.type != BTRFS_ROOT_ITEM_KEY) {
  55. search_key.offset++;
  56. btrfs_release_path(root, path);
  57. goto again;
  58. }
  59. ret = 0;
  60. *found_objectid = search_key.objectid;
  61. out:
  62. btrfs_free_path(path);
  63. return ret;
  64. }
  65. /*
  66. * lookup the root with the highest offset for a given objectid. The key we do
  67. * find is copied into 'key'. If we find something return 0, otherwise 1, < 0
  68. * on error.
  69. */
  70. int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
  71. struct btrfs_root_item *item, struct btrfs_key *key)
  72. {
  73. struct btrfs_path *path;
  74. struct btrfs_key search_key;
  75. struct btrfs_key found_key;
  76. struct extent_buffer *l;
  77. int ret;
  78. int slot;
  79. search_key.objectid = objectid;
  80. search_key.type = BTRFS_ROOT_ITEM_KEY;
  81. search_key.offset = (u64)-1;
  82. path = btrfs_alloc_path();
  83. BUG_ON(!path);
  84. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  85. if (ret < 0)
  86. goto out;
  87. BUG_ON(ret == 0);
  88. if (path->slots[0] == 0) {
  89. ret = 1;
  90. goto out;
  91. }
  92. l = path->nodes[0];
  93. slot = path->slots[0] - 1;
  94. btrfs_item_key_to_cpu(l, &found_key, slot);
  95. if (found_key.objectid != objectid ||
  96. found_key.type != BTRFS_ROOT_ITEM_KEY) {
  97. ret = 1;
  98. goto out;
  99. }
  100. if (item)
  101. read_extent_buffer(l, item, btrfs_item_ptr_offset(l, slot),
  102. sizeof(*item));
  103. if (key)
  104. memcpy(key, &found_key, sizeof(found_key));
  105. ret = 0;
  106. out:
  107. btrfs_free_path(path);
  108. return ret;
  109. }
  110. int btrfs_set_root_node(struct btrfs_root_item *item,
  111. struct extent_buffer *node)
  112. {
  113. btrfs_set_root_bytenr(item, node->start);
  114. btrfs_set_root_level(item, btrfs_header_level(node));
  115. btrfs_set_root_generation(item, btrfs_header_generation(node));
  116. return 0;
  117. }
  118. /*
  119. * copy the data in 'item' into the btree
  120. */
  121. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  122. *root, struct btrfs_key *key, struct btrfs_root_item
  123. *item)
  124. {
  125. struct btrfs_path *path;
  126. struct extent_buffer *l;
  127. int ret;
  128. int slot;
  129. unsigned long ptr;
  130. path = btrfs_alloc_path();
  131. BUG_ON(!path);
  132. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  133. if (ret < 0)
  134. goto out;
  135. if (ret != 0) {
  136. btrfs_print_leaf(root, path->nodes[0]);
  137. printk(KERN_CRIT "unable to update root key %llu %u %llu\n",
  138. (unsigned long long)key->objectid, key->type,
  139. (unsigned long long)key->offset);
  140. BUG_ON(1);
  141. }
  142. l = path->nodes[0];
  143. slot = path->slots[0];
  144. ptr = btrfs_item_ptr_offset(l, slot);
  145. write_extent_buffer(l, item, ptr, sizeof(*item));
  146. btrfs_mark_buffer_dirty(path->nodes[0]);
  147. out:
  148. btrfs_free_path(path);
  149. return ret;
  150. }
  151. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
  152. *root, struct btrfs_key *key, struct btrfs_root_item
  153. *item)
  154. {
  155. int ret;
  156. ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
  157. return ret;
  158. }
  159. /*
  160. * at mount time we want to find all the old transaction snapshots that were in
  161. * the process of being deleted if we crashed. This is any root item with an
  162. * offset lower than the latest root. They need to be queued for deletion to
  163. * finish what was happening when we crashed.
  164. */
  165. int btrfs_find_dead_roots(struct btrfs_root *root, u64 objectid)
  166. {
  167. struct btrfs_root *dead_root;
  168. struct btrfs_root_item *ri;
  169. struct btrfs_key key;
  170. struct btrfs_key found_key;
  171. struct btrfs_path *path;
  172. int ret;
  173. u32 nritems;
  174. struct extent_buffer *leaf;
  175. int slot;
  176. key.objectid = objectid;
  177. btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
  178. key.offset = 0;
  179. path = btrfs_alloc_path();
  180. if (!path)
  181. return -ENOMEM;
  182. again:
  183. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  184. if (ret < 0)
  185. goto err;
  186. while (1) {
  187. leaf = path->nodes[0];
  188. nritems = btrfs_header_nritems(leaf);
  189. slot = path->slots[0];
  190. if (slot >= nritems) {
  191. ret = btrfs_next_leaf(root, path);
  192. if (ret)
  193. break;
  194. leaf = path->nodes[0];
  195. nritems = btrfs_header_nritems(leaf);
  196. slot = path->slots[0];
  197. }
  198. btrfs_item_key_to_cpu(leaf, &key, slot);
  199. if (btrfs_key_type(&key) != BTRFS_ROOT_ITEM_KEY)
  200. goto next;
  201. if (key.objectid < objectid)
  202. goto next;
  203. if (key.objectid > objectid)
  204. break;
  205. ri = btrfs_item_ptr(leaf, slot, struct btrfs_root_item);
  206. if (btrfs_disk_root_refs(leaf, ri) != 0)
  207. goto next;
  208. memcpy(&found_key, &key, sizeof(key));
  209. key.offset++;
  210. btrfs_release_path(root, path);
  211. dead_root =
  212. btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  213. &found_key);
  214. if (IS_ERR(dead_root)) {
  215. ret = PTR_ERR(dead_root);
  216. goto err;
  217. }
  218. ret = btrfs_add_dead_root(dead_root);
  219. if (ret)
  220. goto err;
  221. goto again;
  222. next:
  223. slot++;
  224. path->slots[0]++;
  225. }
  226. ret = 0;
  227. err:
  228. btrfs_free_path(path);
  229. return ret;
  230. }
  231. int btrfs_find_orphan_roots(struct btrfs_root *tree_root)
  232. {
  233. struct extent_buffer *leaf;
  234. struct btrfs_path *path;
  235. struct btrfs_key key;
  236. struct btrfs_key root_key;
  237. struct btrfs_root *root;
  238. int err = 0;
  239. int ret;
  240. path = btrfs_alloc_path();
  241. if (!path)
  242. return -ENOMEM;
  243. key.objectid = BTRFS_ORPHAN_OBJECTID;
  244. key.type = BTRFS_ORPHAN_ITEM_KEY;
  245. key.offset = 0;
  246. root_key.type = BTRFS_ROOT_ITEM_KEY;
  247. root_key.offset = (u64)-1;
  248. while (1) {
  249. ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
  250. if (ret < 0) {
  251. err = ret;
  252. break;
  253. }
  254. leaf = path->nodes[0];
  255. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  256. ret = btrfs_next_leaf(tree_root, path);
  257. if (ret < 0)
  258. err = ret;
  259. if (ret != 0)
  260. break;
  261. leaf = path->nodes[0];
  262. }
  263. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  264. btrfs_release_path(tree_root, path);
  265. if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
  266. key.type != BTRFS_ORPHAN_ITEM_KEY)
  267. break;
  268. root_key.objectid = key.offset;
  269. key.offset++;
  270. root = btrfs_read_fs_root_no_name(tree_root->fs_info,
  271. &root_key);
  272. if (!IS_ERR(root))
  273. continue;
  274. ret = PTR_ERR(root);
  275. if (ret != -ENOENT) {
  276. err = ret;
  277. break;
  278. }
  279. ret = btrfs_find_dead_roots(tree_root, root_key.objectid);
  280. if (ret) {
  281. err = ret;
  282. break;
  283. }
  284. }
  285. btrfs_free_path(path);
  286. return err;
  287. }
  288. /* drop the root item for 'key' from 'root' */
  289. int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  290. struct btrfs_key *key)
  291. {
  292. struct btrfs_path *path;
  293. int ret;
  294. struct btrfs_root_item *ri;
  295. struct extent_buffer *leaf;
  296. path = btrfs_alloc_path();
  297. BUG_ON(!path);
  298. ret = btrfs_search_slot(trans, root, key, path, -1, 1);
  299. if (ret < 0)
  300. goto out;
  301. BUG_ON(ret != 0);
  302. leaf = path->nodes[0];
  303. ri = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_item);
  304. ret = btrfs_del_item(trans, root, path);
  305. out:
  306. btrfs_free_path(path);
  307. return ret;
  308. }
  309. int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
  310. struct btrfs_root *tree_root,
  311. u64 root_id, u64 ref_id, u64 dirid, u64 *sequence,
  312. const char *name, int name_len)
  313. {
  314. struct btrfs_path *path;
  315. struct btrfs_root_ref *ref;
  316. struct extent_buffer *leaf;
  317. struct btrfs_key key;
  318. unsigned long ptr;
  319. int err = 0;
  320. int ret;
  321. path = btrfs_alloc_path();
  322. if (!path)
  323. return -ENOMEM;
  324. key.objectid = root_id;
  325. key.type = BTRFS_ROOT_BACKREF_KEY;
  326. key.offset = ref_id;
  327. again:
  328. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  329. BUG_ON(ret < 0);
  330. if (ret == 0) {
  331. leaf = path->nodes[0];
  332. ref = btrfs_item_ptr(leaf, path->slots[0],
  333. struct btrfs_root_ref);
  334. WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid);
  335. WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len);
  336. ptr = (unsigned long)(ref + 1);
  337. WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len));
  338. *sequence = btrfs_root_ref_sequence(leaf, ref);
  339. ret = btrfs_del_item(trans, tree_root, path);
  340. BUG_ON(ret);
  341. } else
  342. err = -ENOENT;
  343. if (key.type == BTRFS_ROOT_BACKREF_KEY) {
  344. btrfs_release_path(tree_root, path);
  345. key.objectid = ref_id;
  346. key.type = BTRFS_ROOT_REF_KEY;
  347. key.offset = root_id;
  348. goto again;
  349. }
  350. btrfs_free_path(path);
  351. return err;
  352. }
  353. int btrfs_find_root_ref(struct btrfs_root *tree_root,
  354. struct btrfs_path *path,
  355. u64 root_id, u64 ref_id)
  356. {
  357. struct btrfs_key key;
  358. int ret;
  359. key.objectid = root_id;
  360. key.type = BTRFS_ROOT_REF_KEY;
  361. key.offset = ref_id;
  362. ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
  363. return ret;
  364. }
  365. /*
  366. * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
  367. * or BTRFS_ROOT_BACKREF_KEY.
  368. *
  369. * The dirid, sequence, name and name_len refer to the directory entry
  370. * that is referencing the root.
  371. *
  372. * For a forward ref, the root_id is the id of the tree referencing
  373. * the root and ref_id is the id of the subvol or snapshot.
  374. *
  375. * For a back ref the root_id is the id of the subvol or snapshot and
  376. * ref_id is the id of the tree referencing it.
  377. */
  378. int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
  379. struct btrfs_root *tree_root,
  380. u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
  381. const char *name, int name_len)
  382. {
  383. struct btrfs_key key;
  384. int ret;
  385. struct btrfs_path *path;
  386. struct btrfs_root_ref *ref;
  387. struct extent_buffer *leaf;
  388. unsigned long ptr;
  389. path = btrfs_alloc_path();
  390. if (!path)
  391. return -ENOMEM;
  392. key.objectid = root_id;
  393. key.type = BTRFS_ROOT_BACKREF_KEY;
  394. key.offset = ref_id;
  395. again:
  396. ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
  397. sizeof(*ref) + name_len);
  398. BUG_ON(ret);
  399. leaf = path->nodes[0];
  400. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  401. btrfs_set_root_ref_dirid(leaf, ref, dirid);
  402. btrfs_set_root_ref_sequence(leaf, ref, sequence);
  403. btrfs_set_root_ref_name_len(leaf, ref, name_len);
  404. ptr = (unsigned long)(ref + 1);
  405. write_extent_buffer(leaf, name, ptr, name_len);
  406. btrfs_mark_buffer_dirty(leaf);
  407. if (key.type == BTRFS_ROOT_BACKREF_KEY) {
  408. btrfs_release_path(tree_root, path);
  409. key.objectid = ref_id;
  410. key.type = BTRFS_ROOT_REF_KEY;
  411. key.offset = root_id;
  412. goto again;
  413. }
  414. btrfs_free_path(path);
  415. return 0;
  416. }