root-tree.c 13 KB

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