root-tree.c 14 KB

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