Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2 | #include "ctree.h" |
| 3 | #include "disk-io.h" |
| 4 | #include "print-tree.h" |
| 5 | |
| 6 | int btrfs_find_last_root(struct btrfs_root *root, u64 objectid, |
| 7 | struct btrfs_root_item *item, struct btrfs_key *key) |
| 8 | { |
| 9 | struct btrfs_path path; |
| 10 | struct btrfs_key search_key; |
| 11 | struct btrfs_leaf *l; |
| 12 | int ret; |
| 13 | int slot; |
| 14 | |
| 15 | search_key.objectid = objectid; |
| 16 | search_key.flags = (u32)-1; |
| 17 | search_key.offset = (u32)-1; |
| 18 | |
| 19 | btrfs_init_path(&path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 20 | ret = btrfs_search_slot(NULL, root, &search_key, &path, 0, 0); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 21 | if (ret < 0) |
| 22 | goto out; |
| 23 | BUG_ON(ret == 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 24 | l = btrfs_buffer_leaf(path.nodes[0]); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 25 | BUG_ON(path.slots[0] == 0); |
| 26 | slot = path.slots[0] - 1; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 27 | if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 28 | ret = 1; |
| 29 | goto out; |
| 30 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 31 | memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item), |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 32 | sizeof(*item)); |
| 33 | btrfs_disk_key_to_cpu(key, &l->items[slot].key); |
| 34 | btrfs_release_path(root, &path); |
| 35 | ret = 0; |
| 36 | out: |
| 37 | return ret; |
| 38 | } |
| 39 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 40 | int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 41 | *root, struct btrfs_key *key, struct btrfs_root_item |
| 42 | *item) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 43 | { |
| 44 | struct btrfs_path path; |
| 45 | struct btrfs_leaf *l; |
| 46 | int ret; |
| 47 | int slot; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 48 | struct btrfs_root_item *update_item; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 49 | |
| 50 | btrfs_init_path(&path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 51 | ret = btrfs_search_slot(trans, root, key, &path, 0, 1); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 52 | if (ret < 0) |
| 53 | goto out; |
| 54 | BUG_ON(ret != 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 55 | l = btrfs_buffer_leaf(path.nodes[0]); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 56 | slot = path.slots[0]; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 57 | update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item); |
| 58 | btrfs_memcpy(root, l, update_item, item, sizeof(*item)); |
| 59 | btrfs_mark_buffer_dirty(path.nodes[0]); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 60 | out: |
| 61 | btrfs_release_path(root, &path); |
| 62 | return ret; |
| 63 | } |
| 64 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 65 | int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 66 | *root, struct btrfs_key *key, struct btrfs_root_item |
| 67 | *item) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 68 | { |
| 69 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 70 | ret = btrfs_insert_item(trans, root, key, item, sizeof(*item)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 71 | BUG_ON(ret); |
| 72 | return ret; |
| 73 | } |
| 74 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 75 | int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 76 | struct btrfs_key *key) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 77 | { |
| 78 | struct btrfs_path path; |
| 79 | int ret; |
| 80 | |
| 81 | btrfs_init_path(&path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 82 | ret = btrfs_search_slot(trans, root, key, &path, -1, 1); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 83 | if (ret < 0) |
| 84 | goto out; |
| 85 | BUG_ON(ret != 0); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 86 | ret = btrfs_del_item(trans, root, &path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 87 | out: |
| 88 | btrfs_release_path(root, &path); |
| 89 | return ret; |
| 90 | } |