blob: c3b990b661c2717ebb27ca9b97c6af8e2b5518ab [file] [log] [blame]
Chris Mason2e635a22007-03-21 11:12:56 -04001#include <linux/module.h>
Chris Mason1e1d2702007-03-15 19:03:33 -04002#include "ctree.h"
3#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -04004#include "transaction.h"
Chris Mason1e1d2702007-03-15 19:03:33 -04005
Chris Masone089f052007-03-16 16:20:31 -04006int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
7 *root, u64 objectid, struct btrfs_inode_item
8 *inode_item)
Chris Mason1e1d2702007-03-15 19:03:33 -04009{
Chris Mason5caf2a02007-04-02 11:20:42 -040010 struct btrfs_path *path;
Chris Mason1e1d2702007-03-15 19:03:33 -040011 struct btrfs_key key;
12 int ret;
13 key.objectid = objectid;
14 key.flags = 0;
15 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
16 key.offset = 0;
17
Chris Mason5caf2a02007-04-02 11:20:42 -040018 path = btrfs_alloc_path();
19 BUG_ON(!path);
20 btrfs_init_path(path);
Chris Masone089f052007-03-16 16:20:31 -040021 ret = btrfs_insert_item(trans, root, &key, inode_item,
22 sizeof(*inode_item));
Chris Mason5caf2a02007-04-02 11:20:42 -040023 btrfs_release_path(root, path);
24 btrfs_free_path(path);
Chris Mason1b05da22007-04-10 12:13:09 -040025 if (ret == 0 && objectid > root->highest_inode)
26 root->highest_inode = objectid;
Chris Mason1e1d2702007-03-15 19:03:33 -040027 return ret;
28}
29
Chris Masone089f052007-03-16 16:20:31 -040030int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond6e4a422007-04-06 15:37:36 -040031 *root, struct btrfs_path *path,
32 struct btrfs_key *location, int mod)
Chris Mason1e1d2702007-03-15 19:03:33 -040033{
Chris Mason1e1d2702007-03-15 19:03:33 -040034 int ins_len = mod < 0 ? -1 : 0;
35 int cow = mod != 0;
Chris Masond6e4a422007-04-06 15:37:36 -040036 int ret;
37 int slot;
38 struct btrfs_leaf *leaf;
39 struct btrfs_key found_key;
Chris Mason1e1d2702007-03-15 19:03:33 -040040
Chris Masond6e4a422007-04-06 15:37:36 -040041 ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
42 if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY &&
43 location->offset == (u64)-1 && path->slots[0] != 0) {
44 slot = path->slots[0] - 1;
45 leaf = btrfs_buffer_leaf(path->nodes[0]);
46 btrfs_disk_key_to_cpu(&found_key, &leaf->items[slot].key);
47 if (found_key.objectid == location->objectid &&
48 btrfs_key_type(&found_key) == btrfs_key_type(location)) {
49 path->slots[0]--;
50 return 0;
51 }
52 }
53 return ret;
Chris Mason1e1d2702007-03-15 19:03:33 -040054}