blob: 8e1b9046d5ec2fbc63f3e2115878c6cd1b3c5d1e [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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
Chris Mason2e635a22007-03-21 11:12:56 -040019#include <linux/module.h>
Chris Mason3768f362007-03-13 16:47:54 -040020#include "ctree.h"
21#include "disk-io.h"
22#include "print-tree.h"
23
24int btrfs_find_last_root(struct btrfs_root *root, u64 objectid,
25 struct btrfs_root_item *item, struct btrfs_key *key)
26{
Chris Mason5caf2a02007-04-02 11:20:42 -040027 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040028 struct btrfs_key search_key;
29 struct btrfs_leaf *l;
30 int ret;
31 int slot;
32
33 search_key.objectid = objectid;
34 search_key.flags = (u32)-1;
35 search_key.offset = (u32)-1;
36
Chris Mason5caf2a02007-04-02 11:20:42 -040037 path = btrfs_alloc_path();
38 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -040039 ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
Chris Mason3768f362007-03-13 16:47:54 -040040 if (ret < 0)
41 goto out;
42 BUG_ON(ret == 0);
Chris Mason5caf2a02007-04-02 11:20:42 -040043 l = btrfs_buffer_leaf(path->nodes[0]);
44 BUG_ON(path->slots[0] == 0);
45 slot = path->slots[0] - 1;
Chris Mason62e27492007-03-15 12:56:47 -040046 if (btrfs_disk_key_objectid(&l->items[slot].key) != objectid) {
Chris Mason3768f362007-03-13 16:47:54 -040047 ret = 1;
48 goto out;
49 }
Chris Mason123abc82007-03-14 14:14:43 -040050 memcpy(item, btrfs_item_ptr(l, slot, struct btrfs_root_item),
Chris Mason3768f362007-03-13 16:47:54 -040051 sizeof(*item));
52 btrfs_disk_key_to_cpu(key, &l->items[slot].key);
Chris Mason3768f362007-03-13 16:47:54 -040053 ret = 0;
54out:
Chris Mason5caf2a02007-04-02 11:20:42 -040055 btrfs_release_path(root, path);
56 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -040057 return ret;
58}
59
Chris Masone089f052007-03-16 16:20:31 -040060int btrfs_update_root(struct btrfs_trans_handle *trans, struct btrfs_root
61 *root, struct btrfs_key *key, struct btrfs_root_item
62 *item)
Chris Mason3768f362007-03-13 16:47:54 -040063{
Chris Mason5caf2a02007-04-02 11:20:42 -040064 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -040065 struct btrfs_leaf *l;
66 int ret;
67 int slot;
Chris Masond6025572007-03-30 14:27:56 -040068 struct btrfs_root_item *update_item;
Chris Mason3768f362007-03-13 16:47:54 -040069
Chris Mason5caf2a02007-04-02 11:20:42 -040070 path = btrfs_alloc_path();
71 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -040072 ret = btrfs_search_slot(trans, root, key, path, 0, 1);
Chris Mason3768f362007-03-13 16:47:54 -040073 if (ret < 0)
74 goto out;
75 BUG_ON(ret != 0);
Chris Mason5caf2a02007-04-02 11:20:42 -040076 l = btrfs_buffer_leaf(path->nodes[0]);
77 slot = path->slots[0];
Chris Masond6025572007-03-30 14:27:56 -040078 update_item = btrfs_item_ptr(l, slot, struct btrfs_root_item);
79 btrfs_memcpy(root, l, update_item, item, sizeof(*item));
Chris Mason5caf2a02007-04-02 11:20:42 -040080 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason3768f362007-03-13 16:47:54 -040081out:
Chris Mason5caf2a02007-04-02 11:20:42 -040082 btrfs_release_path(root, path);
83 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -040084 return ret;
85}
86
Chris Masone089f052007-03-16 16:20:31 -040087int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root
88 *root, struct btrfs_key *key, struct btrfs_root_item
89 *item)
Chris Mason3768f362007-03-13 16:47:54 -040090{
91 int ret;
Chris Masone089f052007-03-16 16:20:31 -040092 ret = btrfs_insert_item(trans, root, key, item, sizeof(*item));
Chris Mason3768f362007-03-13 16:47:54 -040093 BUG_ON(ret);
94 return ret;
95}
96
Chris Masone089f052007-03-16 16:20:31 -040097int btrfs_del_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
98 struct btrfs_key *key)
Chris Mason3768f362007-03-13 16:47:54 -040099{
Chris Mason5caf2a02007-04-02 11:20:42 -0400100 struct btrfs_path *path;
Chris Mason3768f362007-03-13 16:47:54 -0400101 int ret;
Chris Masonc5739bb2007-04-10 09:27:04 -0400102 u32 refs;
103 struct btrfs_root_item *ri;
Chris Mason3768f362007-03-13 16:47:54 -0400104
Chris Mason5caf2a02007-04-02 11:20:42 -0400105 path = btrfs_alloc_path();
106 BUG_ON(!path);
Chris Mason5caf2a02007-04-02 11:20:42 -0400107 ret = btrfs_search_slot(trans, root, key, path, -1, 1);
Chris Mason3768f362007-03-13 16:47:54 -0400108 if (ret < 0)
109 goto out;
110 BUG_ON(ret != 0);
Chris Masonc5739bb2007-04-10 09:27:04 -0400111 ri = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
112 path->slots[0], struct btrfs_root_item);
113
114 refs = btrfs_root_refs(ri);
115 BUG_ON(refs == 0);
116 if (refs == 1) {
117 ret = btrfs_del_item(trans, root, path);
Chris Masonc5739bb2007-04-10 09:27:04 -0400118 } else {
119 btrfs_set_root_refs(ri, refs - 1);
Chris Mason5af39812007-06-12 07:50:13 -0400120 WARN_ON(1);
Chris Masonc5739bb2007-04-10 09:27:04 -0400121 mark_buffer_dirty(path->nodes[0]);
122 }
Chris Mason3768f362007-03-13 16:47:54 -0400123out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400124 btrfs_release_path(root, path);
125 btrfs_free_path(path);
Chris Mason3768f362007-03-13 16:47:54 -0400126 return ret;
127}