root-tree.c 12 KB

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