root-tree.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/err.h>
  6. #include <linux/uuid.h>
  7. #include "ctree.h"
  8. #include "transaction.h"
  9. #include "disk-io.h"
  10. #include "print-tree.h"
  11. /*
  12. * Read a root item from the tree. In case we detect a root item smaller then
  13. * sizeof(root_item), we know it's an old version of the root structure and
  14. * initialize all new fields to zero. The same happens if we detect mismatching
  15. * generation numbers as then we know the root was once mounted with an older
  16. * kernel that was not aware of the root item structure change.
  17. */
  18. static void btrfs_read_root_item(struct extent_buffer *eb, int slot,
  19. struct btrfs_root_item *item)
  20. {
  21. uuid_le uuid;
  22. int len;
  23. int need_reset = 0;
  24. len = btrfs_item_size_nr(eb, slot);
  25. read_extent_buffer(eb, item, btrfs_item_ptr_offset(eb, slot),
  26. min_t(int, len, (int)sizeof(*item)));
  27. if (len < sizeof(*item))
  28. need_reset = 1;
  29. if (!need_reset && btrfs_root_generation(item)
  30. != btrfs_root_generation_v2(item)) {
  31. if (btrfs_root_generation_v2(item) != 0) {
  32. btrfs_warn(eb->fs_info,
  33. "mismatching generation and generation_v2 found in root item. This root was probably mounted with an older kernel. Resetting all new fields.");
  34. }
  35. need_reset = 1;
  36. }
  37. if (need_reset) {
  38. memset(&item->generation_v2, 0,
  39. sizeof(*item) - offsetof(struct btrfs_root_item,
  40. generation_v2));
  41. uuid_le_gen(&uuid);
  42. memcpy(item->uuid, uuid.b, BTRFS_UUID_SIZE);
  43. }
  44. }
  45. /*
  46. * btrfs_find_root - lookup the root by the key.
  47. * root: the root of the root tree
  48. * search_key: the key to search
  49. * path: the path we search
  50. * root_item: the root item of the tree we look for
  51. * root_key: the root key of the tree we look for
  52. *
  53. * If ->offset of 'search_key' is -1ULL, it means we are not sure the offset
  54. * of the search key, just lookup the root with the highest offset for a
  55. * given objectid.
  56. *
  57. * If we find something return 0, otherwise > 0, < 0 on error.
  58. */
  59. int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
  60. struct btrfs_path *path, struct btrfs_root_item *root_item,
  61. struct btrfs_key *root_key)
  62. {
  63. struct btrfs_key found_key;
  64. struct extent_buffer *l;
  65. int ret;
  66. int slot;
  67. ret = btrfs_search_slot(NULL, root, search_key, path, 0, 0);
  68. if (ret < 0)
  69. return ret;
  70. if (search_key->offset != -1ULL) { /* the search key is exact */
  71. if (ret > 0)
  72. goto out;
  73. } else {
  74. BUG_ON(ret == 0); /* Logical error */
  75. if (path->slots[0] == 0)
  76. goto out;
  77. path->slots[0]--;
  78. ret = 0;
  79. }
  80. l = path->nodes[0];
  81. slot = path->slots[0];
  82. btrfs_item_key_to_cpu(l, &found_key, slot);
  83. if (found_key.objectid != search_key->objectid ||
  84. found_key.type != BTRFS_ROOT_ITEM_KEY) {
  85. ret = 1;
  86. goto out;
  87. }
  88. if (root_item)
  89. btrfs_read_root_item(l, slot, root_item);
  90. if (root_key)
  91. memcpy(root_key, &found_key, sizeof(found_key));
  92. out:
  93. btrfs_release_path(path);
  94. return ret;
  95. }
  96. void btrfs_set_root_node(struct btrfs_root_item *item,
  97. struct extent_buffer *node)
  98. {
  99. btrfs_set_root_bytenr(item, node->start);
  100. btrfs_set_root_level(item, btrfs_header_level(node));
  101. btrfs_set_root_generation(item, btrfs_header_generation(node));
  102. }
  103. /*
  104. * copy the data in 'item' into the btree
  105. */
  106. int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
  107. *root, struct btrfs_key *key, struct btrfs_root_item
  108. *item)
  109. {
  110. struct btrfs_fs_info *fs_info = root->fs_info;
  111. struct btrfs_path *path;
  112. struct extent_buffer *l;
  113. int ret;
  114. int slot;
  115. unsigned long ptr;
  116. u32 old_len;
  117. path = btrfs_alloc_path();
  118. if (!path)
  119. return -ENOMEM;
  120. ret = btrfs_search_slot(trans, root, key, path, 0, 1);
  121. if (ret < 0) {
  122. btrfs_abort_transaction(trans, ret);
  123. goto out;
  124. }
  125. if (ret != 0) {
  126. btrfs_print_leaf(path->nodes[0]);
  127. btrfs_crit(fs_info, "unable to update root key %llu %u %llu",
  128. key->objectid, key->type, key->offset);
  129. BUG_ON(1);
  130. }
  131. l = path->nodes[0];
  132. slot = path->slots[0];
  133. ptr = btrfs_item_ptr_offset(l, slot);
  134. old_len = btrfs_item_size_nr(l, slot);
  135. /*
  136. * If this is the first time we update the root item which originated
  137. * from an older kernel, we need to enlarge the item size to make room
  138. * for the added fields.
  139. */
  140. if (old_len < sizeof(*item)) {
  141. btrfs_release_path(path);
  142. ret = btrfs_search_slot(trans, root, key, path,
  143. -1, 1);
  144. if (ret < 0) {
  145. btrfs_abort_transaction(trans, ret);
  146. goto out;
  147. }
  148. ret = btrfs_del_item(trans, root, path);
  149. if (ret < 0) {
  150. btrfs_abort_transaction(trans, ret);
  151. goto out;
  152. }
  153. btrfs_release_path(path);
  154. ret = btrfs_insert_empty_item(trans, root, path,
  155. key, sizeof(*item));
  156. if (ret < 0) {
  157. btrfs_abort_transaction(trans, ret);
  158. goto out;
  159. }
  160. l = path->nodes[0];
  161. slot = path->slots[0];
  162. ptr = btrfs_item_ptr_offset(l, slot);
  163. }
  164. /*
  165. * Update generation_v2 so at the next mount we know the new root
  166. * fields are valid.
  167. */
  168. btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
  169. write_extent_buffer(l, item, ptr, sizeof(*item));
  170. btrfs_mark_buffer_dirty(path->nodes[0]);
  171. out:
  172. btrfs_free_path(path);
  173. return ret;
  174. }
  175. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  176. const struct btrfs_key *key, struct btrfs_root_item *item)
  177. {
  178. /*
  179. * Make sure generation v1 and v2 match. See update_root for details.
  180. */
  181. btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
  182. return btrfs_insert_item(trans, root, key, item, sizeof(*item));
  183. }
  184. int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
  185. {
  186. struct btrfs_root *tree_root = fs_info->tree_root;
  187. struct extent_buffer *leaf;
  188. struct btrfs_path *path;
  189. struct btrfs_key key;
  190. struct btrfs_key root_key;
  191. struct btrfs_root *root;
  192. int err = 0;
  193. int ret;
  194. path = btrfs_alloc_path();
  195. if (!path)
  196. return -ENOMEM;
  197. key.objectid = BTRFS_ORPHAN_OBJECTID;
  198. key.type = BTRFS_ORPHAN_ITEM_KEY;
  199. key.offset = 0;
  200. root_key.type = BTRFS_ROOT_ITEM_KEY;
  201. root_key.offset = (u64)-1;
  202. while (1) {
  203. ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
  204. if (ret < 0) {
  205. err = ret;
  206. break;
  207. }
  208. leaf = path->nodes[0];
  209. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  210. ret = btrfs_next_leaf(tree_root, path);
  211. if (ret < 0)
  212. err = ret;
  213. if (ret != 0)
  214. break;
  215. leaf = path->nodes[0];
  216. }
  217. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  218. btrfs_release_path(path);
  219. if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
  220. key.type != BTRFS_ORPHAN_ITEM_KEY)
  221. break;
  222. root_key.objectid = key.offset;
  223. key.offset++;
  224. /*
  225. * The root might have been inserted already, as before we look
  226. * for orphan roots, log replay might have happened, which
  227. * triggers a transaction commit and qgroup accounting, which
  228. * in turn reads and inserts fs roots while doing backref
  229. * walking.
  230. */
  231. root = btrfs_lookup_fs_root(fs_info, root_key.objectid);
  232. if (root) {
  233. WARN_ON(!test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
  234. &root->state));
  235. if (btrfs_root_refs(&root->root_item) == 0)
  236. btrfs_add_dead_root(root);
  237. continue;
  238. }
  239. root = btrfs_read_fs_root(tree_root, &root_key);
  240. err = PTR_ERR_OR_ZERO(root);
  241. if (err && err != -ENOENT) {
  242. break;
  243. } else if (err == -ENOENT) {
  244. struct btrfs_trans_handle *trans;
  245. btrfs_release_path(path);
  246. trans = btrfs_join_transaction(tree_root);
  247. if (IS_ERR(trans)) {
  248. err = PTR_ERR(trans);
  249. btrfs_handle_fs_error(fs_info, err,
  250. "Failed to start trans to delete orphan item");
  251. break;
  252. }
  253. err = btrfs_del_orphan_item(trans, tree_root,
  254. root_key.objectid);
  255. btrfs_end_transaction(trans);
  256. if (err) {
  257. btrfs_handle_fs_error(fs_info, err,
  258. "Failed to delete root orphan item");
  259. break;
  260. }
  261. continue;
  262. }
  263. err = btrfs_init_fs_root(root);
  264. if (err) {
  265. btrfs_free_fs_root(root);
  266. break;
  267. }
  268. set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
  269. err = btrfs_insert_fs_root(fs_info, root);
  270. if (err) {
  271. BUG_ON(err == -EEXIST);
  272. btrfs_free_fs_root(root);
  273. break;
  274. }
  275. if (btrfs_root_refs(&root->root_item) == 0)
  276. btrfs_add_dead_root(root);
  277. }
  278. btrfs_free_path(path);
  279. return err;
  280. }
  281. /* drop the root item for 'key' from the tree root */
  282. int btrfs_del_root(struct btrfs_trans_handle *trans,
  283. struct btrfs_fs_info *fs_info, const struct btrfs_key *key)
  284. {
  285. struct btrfs_root *root = fs_info->tree_root;
  286. struct btrfs_path *path;
  287. int ret;
  288. path = btrfs_alloc_path();
  289. if (!path)
  290. return -ENOMEM;
  291. ret = btrfs_search_slot(trans, root, key, path, -1, 1);
  292. if (ret < 0)
  293. goto out;
  294. BUG_ON(ret != 0);
  295. ret = btrfs_del_item(trans, root, path);
  296. out:
  297. btrfs_free_path(path);
  298. return ret;
  299. }
  300. int btrfs_del_root_ref(struct btrfs_trans_handle *trans,
  301. struct btrfs_fs_info *fs_info,
  302. u64 root_id, u64 ref_id, u64 dirid, u64 *sequence,
  303. const char *name, int name_len)
  304. {
  305. struct btrfs_root *tree_root = fs_info->tree_root;
  306. struct btrfs_path *path;
  307. struct btrfs_root_ref *ref;
  308. struct extent_buffer *leaf;
  309. struct btrfs_key key;
  310. unsigned long ptr;
  311. int err = 0;
  312. int ret;
  313. path = btrfs_alloc_path();
  314. if (!path)
  315. return -ENOMEM;
  316. key.objectid = root_id;
  317. key.type = BTRFS_ROOT_BACKREF_KEY;
  318. key.offset = ref_id;
  319. again:
  320. ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
  321. BUG_ON(ret < 0);
  322. if (ret == 0) {
  323. leaf = path->nodes[0];
  324. ref = btrfs_item_ptr(leaf, path->slots[0],
  325. struct btrfs_root_ref);
  326. WARN_ON(btrfs_root_ref_dirid(leaf, ref) != dirid);
  327. WARN_ON(btrfs_root_ref_name_len(leaf, ref) != name_len);
  328. ptr = (unsigned long)(ref + 1);
  329. WARN_ON(memcmp_extent_buffer(leaf, name, ptr, name_len));
  330. *sequence = btrfs_root_ref_sequence(leaf, ref);
  331. ret = btrfs_del_item(trans, tree_root, path);
  332. if (ret) {
  333. err = ret;
  334. goto out;
  335. }
  336. } else
  337. err = -ENOENT;
  338. if (key.type == BTRFS_ROOT_BACKREF_KEY) {
  339. btrfs_release_path(path);
  340. key.objectid = ref_id;
  341. key.type = BTRFS_ROOT_REF_KEY;
  342. key.offset = root_id;
  343. goto again;
  344. }
  345. out:
  346. btrfs_free_path(path);
  347. return err;
  348. }
  349. /*
  350. * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
  351. * or BTRFS_ROOT_BACKREF_KEY.
  352. *
  353. * The dirid, sequence, name and name_len refer to the directory entry
  354. * that is referencing the root.
  355. *
  356. * For a forward ref, the root_id is the id of the tree referencing
  357. * the root and ref_id is the id of the subvol or snapshot.
  358. *
  359. * For a back ref the root_id is the id of the subvol or snapshot and
  360. * ref_id is the id of the tree referencing it.
  361. *
  362. * Will return 0, -ENOMEM, or anything from the CoW path
  363. */
  364. int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
  365. struct btrfs_fs_info *fs_info,
  366. u64 root_id, u64 ref_id, u64 dirid, u64 sequence,
  367. const char *name, int name_len)
  368. {
  369. struct btrfs_root *tree_root = fs_info->tree_root;
  370. struct btrfs_key key;
  371. int ret;
  372. struct btrfs_path *path;
  373. struct btrfs_root_ref *ref;
  374. struct extent_buffer *leaf;
  375. unsigned long ptr;
  376. path = btrfs_alloc_path();
  377. if (!path)
  378. return -ENOMEM;
  379. key.objectid = root_id;
  380. key.type = BTRFS_ROOT_BACKREF_KEY;
  381. key.offset = ref_id;
  382. again:
  383. ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
  384. sizeof(*ref) + name_len);
  385. if (ret) {
  386. btrfs_abort_transaction(trans, ret);
  387. btrfs_free_path(path);
  388. return ret;
  389. }
  390. leaf = path->nodes[0];
  391. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  392. btrfs_set_root_ref_dirid(leaf, ref, dirid);
  393. btrfs_set_root_ref_sequence(leaf, ref, sequence);
  394. btrfs_set_root_ref_name_len(leaf, ref, name_len);
  395. ptr = (unsigned long)(ref + 1);
  396. write_extent_buffer(leaf, name, ptr, name_len);
  397. btrfs_mark_buffer_dirty(leaf);
  398. if (key.type == BTRFS_ROOT_BACKREF_KEY) {
  399. btrfs_release_path(path);
  400. key.objectid = ref_id;
  401. key.type = BTRFS_ROOT_REF_KEY;
  402. key.offset = root_id;
  403. goto again;
  404. }
  405. btrfs_free_path(path);
  406. return 0;
  407. }
  408. /*
  409. * Old btrfs forgets to init root_item->flags and root_item->byte_limit
  410. * for subvolumes. To work around this problem, we steal a bit from
  411. * root_item->inode_item->flags, and use it to indicate if those fields
  412. * have been properly initialized.
  413. */
  414. void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
  415. {
  416. u64 inode_flags = btrfs_stack_inode_flags(&root_item->inode);
  417. if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
  418. inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
  419. btrfs_set_stack_inode_flags(&root_item->inode, inode_flags);
  420. btrfs_set_root_flags(root_item, 0);
  421. btrfs_set_root_limit(root_item, 0);
  422. }
  423. }
  424. void btrfs_update_root_times(struct btrfs_trans_handle *trans,
  425. struct btrfs_root *root)
  426. {
  427. struct btrfs_root_item *item = &root->root_item;
  428. struct timespec64 ct;
  429. ktime_get_real_ts64(&ct);
  430. spin_lock(&root->root_item_lock);
  431. btrfs_set_root_ctransid(item, trans->transid);
  432. btrfs_set_stack_timespec_sec(&item->ctime, ct.tv_sec);
  433. btrfs_set_stack_timespec_nsec(&item->ctime, ct.tv_nsec);
  434. spin_unlock(&root->root_item_lock);
  435. }