root-tree.c 14 KB

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