root-tree.c 13 KB

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