blob: e8b5a9f7c0c24956de75c1717c380a191ef46456 [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 Mason1e1d2702007-03-15 19:03:33 -040020#include "ctree.h"
21#include "disk-io.h"
Chris Masone089f052007-03-16 16:20:31 -040022#include "transaction.h"
Chris Mason1e1d2702007-03-15 19:03:33 -040023
Chris Masone089f052007-03-16 16:20:31 -040024int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root
25 *root, u64 objectid, struct btrfs_inode_item
26 *inode_item)
Chris Mason1e1d2702007-03-15 19:03:33 -040027{
Chris Mason5caf2a02007-04-02 11:20:42 -040028 struct btrfs_path *path;
Chris Mason1e1d2702007-03-15 19:03:33 -040029 struct btrfs_key key;
30 int ret;
31 key.objectid = objectid;
32 key.flags = 0;
33 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
34 key.offset = 0;
35
Chris Mason5caf2a02007-04-02 11:20:42 -040036 path = btrfs_alloc_path();
37 BUG_ON(!path);
Chris Masone089f052007-03-16 16:20:31 -040038 ret = btrfs_insert_item(trans, root, &key, inode_item,
39 sizeof(*inode_item));
Chris Mason5caf2a02007-04-02 11:20:42 -040040 btrfs_release_path(root, path);
41 btrfs_free_path(path);
Chris Mason1b05da22007-04-10 12:13:09 -040042 if (ret == 0 && objectid > root->highest_inode)
43 root->highest_inode = objectid;
Chris Mason1e1d2702007-03-15 19:03:33 -040044 return ret;
45}
46
Chris Masone089f052007-03-16 16:20:31 -040047int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond6e4a422007-04-06 15:37:36 -040048 *root, struct btrfs_path *path,
49 struct btrfs_key *location, int mod)
Chris Mason1e1d2702007-03-15 19:03:33 -040050{
Chris Mason1e1d2702007-03-15 19:03:33 -040051 int ins_len = mod < 0 ? -1 : 0;
52 int cow = mod != 0;
Chris Masond6e4a422007-04-06 15:37:36 -040053 int ret;
54 int slot;
55 struct btrfs_leaf *leaf;
56 struct btrfs_key found_key;
Chris Mason1e1d2702007-03-15 19:03:33 -040057
Chris Masond6e4a422007-04-06 15:37:36 -040058 ret = btrfs_search_slot(trans, root, location, path, ins_len, cow);
59 if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY &&
60 location->offset == (u64)-1 && path->slots[0] != 0) {
61 slot = path->slots[0] - 1;
62 leaf = btrfs_buffer_leaf(path->nodes[0]);
63 btrfs_disk_key_to_cpu(&found_key, &leaf->items[slot].key);
64 if (found_key.objectid == location->objectid &&
65 btrfs_key_type(&found_key) == btrfs_key_type(location)) {
66 path->slots[0]--;
67 return 0;
68 }
69 }
70 return ret;
Chris Mason1e1d2702007-03-15 19:03:33 -040071}