root-tree.c 12 KB

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