root-tree.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. goto out;
  123. if (ret > 0) {
  124. btrfs_crit(fs_info,
  125. "unable to find root key (%llu %u %llu) in tree %llu",
  126. key->objectid, key->type, key->offset,
  127. root->root_key.objectid);
  128. ret = -EUCLEAN;
  129. btrfs_abort_transaction(trans, ret);
  130. goto out;
  131. }
  132. l = path->nodes[0];
  133. slot = path->slots[0];
  134. ptr = btrfs_item_ptr_offset(l, slot);
  135. old_len = btrfs_item_size_nr(l, slot);
  136. /*
  137. * If this is the first time we update the root item which originated
  138. * from an older kernel, we need to enlarge the item size to make room
  139. * for the added fields.
  140. */
  141. if (old_len < sizeof(*item)) {
  142. btrfs_release_path(path);
  143. ret = btrfs_search_slot(trans, root, key, path,
  144. -1, 1);
  145. if (ret < 0) {
  146. btrfs_abort_transaction(trans, ret);
  147. goto out;
  148. }
  149. ret = btrfs_del_item(trans, root, path);
  150. if (ret < 0) {
  151. btrfs_abort_transaction(trans, ret);
  152. goto out;
  153. }
  154. btrfs_release_path(path);
  155. ret = btrfs_insert_empty_item(trans, root, path,
  156. key, sizeof(*item));
  157. if (ret < 0) {
  158. btrfs_abort_transaction(trans, ret);
  159. goto out;
  160. }
  161. l = path->nodes[0];
  162. slot = path->slots[0];
  163. ptr = btrfs_item_ptr_offset(l, slot);
  164. }
  165. /*
  166. * Update generation_v2 so at the next mount we know the new root
  167. * fields are valid.
  168. */
  169. btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
  170. write_extent_buffer(l, item, ptr, sizeof(*item));
  171. btrfs_mark_buffer_dirty(path->nodes[0]);
  172. out:
  173. btrfs_free_path(path);
  174. return ret;
  175. }
  176. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  177. const struct btrfs_key *key, struct btrfs_root_item *item)
  178. {
  179. /*
  180. * Make sure generation v1 and v2 match. See update_root for details.
  181. */
  182. btrfs_set_root_generation_v2(item, btrfs_root_generation(item));
  183. return btrfs_insert_item(trans, root, key, item, sizeof(*item));
  184. }
  185. int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info)
  186. {
  187. struct btrfs_root *tree_root = fs_info->tree_root;
  188. struct extent_buffer *leaf;
  189. struct btrfs_path *path;
  190. struct btrfs_key key;
  191. struct btrfs_key root_key;
  192. struct btrfs_root *root;
  193. int err = 0;
  194. int ret;
  195. path = btrfs_alloc_path();
  196. if (!path)
  197. return -ENOMEM;
  198. key.objectid = BTRFS_ORPHAN_OBJECTID;
  199. key.type = BTRFS_ORPHAN_ITEM_KEY;
  200. key.offset = 0;
  201. root_key.type = BTRFS_ROOT_ITEM_KEY;
  202. root_key.offset = (u64)-1;
  203. while (1) {
  204. ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
  205. if (ret < 0) {
  206. err = ret;
  207. break;
  208. }
  209. leaf = path->nodes[0];
  210. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  211. ret = btrfs_next_leaf(tree_root, path);
  212. if (ret < 0)
  213. err = ret;
  214. if (ret != 0)
  215. break;
  216. leaf = path->nodes[0];
  217. }
  218. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  219. btrfs_release_path(path);
  220. if (key.objectid != BTRFS_ORPHAN_OBJECTID ||
  221. key.type != BTRFS_ORPHAN_ITEM_KEY)
  222. break;
  223. root_key.objectid = key.offset;
  224. key.offset++;
  225. /*
  226. * The root might have been inserted already, as before we look
  227. * for orphan roots, log replay might have happened, which
  228. * triggers a transaction commit and qgroup accounting, which
  229. * in turn reads and inserts fs roots while doing backref
  230. * walking.
  231. */
  232. root = btrfs_lookup_fs_root(fs_info, root_key.objectid);
  233. if (root) {
  234. WARN_ON(!test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
  235. &root->state));
  236. if (btrfs_root_refs(&root->root_item) == 0)
  237. btrfs_add_dead_root(root);
  238. continue;
  239. }
  240. root = btrfs_read_fs_root(tree_root, &root_key);
  241. err = PTR_ERR_OR_ZERO(root);
  242. if (err && err != -ENOENT) {
  243. break;
  244. } else if (err == -ENOENT) {
  245. struct btrfs_trans_handle *trans;
  246. btrfs_release_path(path);
  247. trans = btrfs_join_transaction(tree_root);
  248. if (IS_ERR(trans)) {
  249. err = PTR_ERR(trans);
  250. btrfs_handle_fs_error(fs_info, err,
  251. "Failed to start trans to delete orphan item");
  252. break;
  253. }
  254. err = btrfs_del_orphan_item(trans, tree_root,
  255. root_key.objectid);
  256. btrfs_end_transaction(trans);
  257. if (err) {
  258. btrfs_handle_fs_error(fs_info, err,
  259. "Failed to delete root orphan item");
  260. break;
  261. }
  262. continue;
  263. }
  264. err = btrfs_init_fs_root(root);
  265. if (err) {
  266. btrfs_free_fs_root(root);
  267. break;
  268. }
  269. set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
  270. err = btrfs_insert_fs_root(fs_info, root);
  271. if (err) {
  272. BUG_ON(err == -EEXIST);
  273. btrfs_free_fs_root(root);
  274. break;
  275. }
  276. if (btrfs_root_refs(&root->root_item) == 0)
  277. btrfs_add_dead_root(root);
  278. }
  279. btrfs_free_path(path);
  280. return err;
  281. }
  282. /* drop the root item for 'key' from the tree root */
  283. int btrfs_del_root(struct btrfs_trans_handle *trans,
  284. const struct btrfs_key *key)
  285. {
  286. struct btrfs_root *root = trans->fs_info->tree_root;
  287. struct btrfs_path *path;
  288. int ret;
  289. path = btrfs_alloc_path();
  290. if (!path)
  291. return -ENOMEM;
  292. ret = btrfs_search_slot(trans, root, key, path, -1, 1);
  293. if (ret < 0)
  294. goto out;
  295. BUG_ON(ret != 0);
  296. ret = btrfs_del_item(trans, root, path);
  297. out:
  298. btrfs_free_path(path);
  299. return ret;
  300. }
  301. int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
  302. u64 ref_id, u64 dirid, u64 *sequence, const char *name,
  303. int name_len)
  304. {
  305. struct btrfs_root *tree_root = trans->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. ptr = (unsigned long)(ref + 1);
  327. if ((btrfs_root_ref_dirid(leaf, ref) != dirid) ||
  328. (btrfs_root_ref_name_len(leaf, ref) != name_len) ||
  329. memcmp_extent_buffer(leaf, name, ptr, name_len)) {
  330. err = -ENOENT;
  331. goto out;
  332. }
  333. *sequence = btrfs_root_ref_sequence(leaf, ref);
  334. ret = btrfs_del_item(trans, tree_root, path);
  335. if (ret) {
  336. err = ret;
  337. goto out;
  338. }
  339. } else
  340. err = -ENOENT;
  341. if (key.type == BTRFS_ROOT_BACKREF_KEY) {
  342. btrfs_release_path(path);
  343. key.objectid = ref_id;
  344. key.type = BTRFS_ROOT_REF_KEY;
  345. key.offset = root_id;
  346. goto again;
  347. }
  348. out:
  349. btrfs_free_path(path);
  350. return err;
  351. }
  352. /*
  353. * add a btrfs_root_ref item. type is either BTRFS_ROOT_REF_KEY
  354. * or BTRFS_ROOT_BACKREF_KEY.
  355. *
  356. * The dirid, sequence, name and name_len refer to the directory entry
  357. * that is referencing the root.
  358. *
  359. * For a forward ref, the root_id is the id of the tree referencing
  360. * the root and ref_id is the id of the subvol or snapshot.
  361. *
  362. * For a back ref the root_id is the id of the subvol or snapshot and
  363. * ref_id is the id of the tree referencing it.
  364. *
  365. * Will return 0, -ENOMEM, or anything from the CoW path
  366. */
  367. int btrfs_add_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
  368. u64 ref_id, u64 dirid, u64 sequence, const char *name,
  369. int name_len)
  370. {
  371. struct btrfs_root *tree_root = trans->fs_info->tree_root;
  372. struct btrfs_key key;
  373. int ret;
  374. struct btrfs_path *path;
  375. struct btrfs_root_ref *ref;
  376. struct extent_buffer *leaf;
  377. unsigned long ptr;
  378. path = btrfs_alloc_path();
  379. if (!path)
  380. return -ENOMEM;
  381. key.objectid = root_id;
  382. key.type = BTRFS_ROOT_BACKREF_KEY;
  383. key.offset = ref_id;
  384. again:
  385. ret = btrfs_insert_empty_item(trans, tree_root, path, &key,
  386. sizeof(*ref) + name_len);
  387. if (ret) {
  388. btrfs_abort_transaction(trans, ret);
  389. btrfs_free_path(path);
  390. return ret;
  391. }
  392. leaf = path->nodes[0];
  393. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
  394. btrfs_set_root_ref_dirid(leaf, ref, dirid);
  395. btrfs_set_root_ref_sequence(leaf, ref, sequence);
  396. btrfs_set_root_ref_name_len(leaf, ref, name_len);
  397. ptr = (unsigned long)(ref + 1);
  398. write_extent_buffer(leaf, name, ptr, name_len);
  399. btrfs_mark_buffer_dirty(leaf);
  400. if (key.type == BTRFS_ROOT_BACKREF_KEY) {
  401. btrfs_release_path(path);
  402. key.objectid = ref_id;
  403. key.type = BTRFS_ROOT_REF_KEY;
  404. key.offset = root_id;
  405. goto again;
  406. }
  407. btrfs_free_path(path);
  408. return 0;
  409. }
  410. /*
  411. * Old btrfs forgets to init root_item->flags and root_item->byte_limit
  412. * for subvolumes. To work around this problem, we steal a bit from
  413. * root_item->inode_item->flags, and use it to indicate if those fields
  414. * have been properly initialized.
  415. */
  416. void btrfs_check_and_init_root_item(struct btrfs_root_item *root_item)
  417. {
  418. u64 inode_flags = btrfs_stack_inode_flags(&root_item->inode);
  419. if (!(inode_flags & BTRFS_INODE_ROOT_ITEM_INIT)) {
  420. inode_flags |= BTRFS_INODE_ROOT_ITEM_INIT;
  421. btrfs_set_stack_inode_flags(&root_item->inode, inode_flags);
  422. btrfs_set_root_flags(root_item, 0);
  423. btrfs_set_root_limit(root_item, 0);
  424. }
  425. }
  426. void btrfs_update_root_times(struct btrfs_trans_handle *trans,
  427. struct btrfs_root *root)
  428. {
  429. struct btrfs_root_item *item = &root->root_item;
  430. struct timespec64 ct;
  431. ktime_get_real_ts64(&ct);
  432. spin_lock(&root->root_item_lock);
  433. btrfs_set_root_ctransid(item, trans->transid);
  434. btrfs_set_stack_timespec_sec(&item->ctime, ct.tv_sec);
  435. btrfs_set_stack_timespec_nsec(&item->ctime, ct.tv_nsec);
  436. spin_unlock(&root->root_item_lock);
  437. }