root-tree.c 13 KB

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