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 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 9 | struct btrfs_path *path; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 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 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 19 | path = btrfs_alloc_path(); |
| 20 | BUG_ON(!path); |
| 21 | btrfs_init_path(path); |
| 22 | ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 23 | if (ret < 0) |
| 24 | goto out; |
| 25 | BUG_ON(ret == 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 26 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 27 | BUG_ON(path->slots[0] == 0); |
| 28 | slot = path->slots[0] - 1; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 29 | if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 30 | ret = 1; |
| 31 | goto out; |
| 32 | } |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 33 | memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item), |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 34 | sizeof(*item)); |
| 35 | btrfs_disk_key_to_cpu(key, &l->items[slot].key); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 36 | ret = 0; |
| 37 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 38 | btrfs_release_path(root, path); |
| 39 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 40 | return ret; |
| 41 | } |
| 42 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 43 | int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 44 | *root, struct btrfs_key *key, struct btrfs_root_item |
| 45 | *item) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 46 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 47 | struct btrfs_path *path; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 48 | struct btrfs_leaf *l; |
| 49 | int ret; |
| 50 | int slot; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 51 | struct btrfs_root_item *update_item; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 52 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 53 | path = btrfs_alloc_path(); |
| 54 | BUG_ON(!path); |
| 55 | btrfs_init_path(path); |
| 56 | ret = btrfs_search_slot(trans, root, key, path, 0, 1); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 57 | if (ret < 0) |
| 58 | goto out; |
| 59 | BUG_ON(ret != 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 60 | l = btrfs_buffer_leaf(path->nodes[0]); |
| 61 | slot = path->slots[0]; |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 62 | update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item); |
| 63 | btrfs_memcpy(root, l, update_item, item, sizeof(*item)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 64 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 65 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 66 | btrfs_release_path(root, path); |
| 67 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 68 | return ret; |
| 69 | } |
| 70 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 71 | int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root |
| 72 | *root, struct btrfs_key *key, struct btrfs_root_item |
| 73 | *item) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 74 | { |
| 75 | int ret; |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 76 | ret = btrfs_insert_item(trans, root, key, item, sizeof(*item)); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 77 | BUG_ON(ret); |
| 78 | return ret; |
| 79 | } |
| 80 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 81 | int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 82 | struct btrfs_key *key) |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 83 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 84 | struct btrfs_path *path; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 85 | int ret; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame^] | 86 | u32 refs; |
| 87 | struct btrfs_root_item *ri; |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 88 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 89 | path = btrfs_alloc_path(); |
| 90 | BUG_ON(!path); |
| 91 | btrfs_init_path(path); |
| 92 | ret = btrfs_search_slot(trans, root, key, path, -1, 1); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 93 | if (ret < 0) |
| 94 | goto out; |
| 95 | BUG_ON(ret != 0); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame^] | 96 | ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 97 | path->slots[0], struct btrfs_root_item); |
| 98 | |
| 99 | refs = btrfs_root_refs(ri); |
| 100 | BUG_ON(refs == 0); |
| 101 | if (refs == 1) { |
| 102 | ret = btrfs_del_item(trans, root, path); |
| 103 | printk("deleting root %Lu %Lu %u\n", key->objectid, key->offset, key->flags); |
| 104 | } else { |
| 105 | btrfs_set_root_refs(ri, refs - 1); |
| 106 | printk("ref now %u root %Lu %Lu %u\n", refs -1, key->objectid, key->offset, key->flags); |
| 107 | mark_buffer_dirty(path->nodes[0]); |
| 108 | } |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 109 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 110 | btrfs_release_path(root, path); |
| 111 | btrfs_free_path(path); |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 112 | return ret; |
| 113 | } |