root-tree.c 11 KB

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