Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 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 Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 19 | #include "ctree.h" |
| 20 | #include "disk-io.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 21 | #include "transaction.h" |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 22 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 23 | int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans, |
| 24 | struct btrfs_root *root, |
| 25 | struct btrfs_path *path, u64 objectid) |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 26 | { |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 27 | struct btrfs_key key; |
| 28 | int ret; |
| 29 | key.objectid = objectid; |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 30 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 31 | key.offset = 0; |
| 32 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 33 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 34 | sizeof(struct btrfs_inode_item)); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 35 | if (ret == 0 && objectid > root->highest_inode) |
| 36 | root->highest_inode = objectid; |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 37 | return ret; |
| 38 | } |
| 39 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 40 | int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 41 | *root, struct btrfs_path *path, |
| 42 | struct btrfs_key *location, int mod) |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 43 | { |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 44 | int ins_len = mod < 0 ? -1 : 0; |
| 45 | int cow = mod != 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 46 | int ret; |
| 47 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 48 | struct extent_buffer *leaf; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 49 | struct btrfs_key found_key; |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 50 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 51 | ret = btrfs_search_slot(trans, root, location, path, ins_len, cow); |
| 52 | if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY && |
| 53 | location->offset == (u64)-1 && path->slots[0] != 0) { |
| 54 | slot = path->slots[0] - 1; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 55 | leaf = path->nodes[0]; |
| 56 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 57 | if (found_key.objectid == location->objectid && |
| 58 | btrfs_key_type(&found_key) == btrfs_key_type(location)) { |
| 59 | path->slots[0]--; |
| 60 | return 0; |
| 61 | } |
| 62 | } |
| 63 | return ret; |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 64 | } |