Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 2 | #include "ctree.h" |
| 3 | #include "disk-io.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 4 | #include "transaction.h" |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 5 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 6 | int btrfs_insert_inode(struct btrfs_trans_handle *trans, struct btrfs_root |
| 7 | *root, u64 objectid, struct btrfs_inode_item |
| 8 | *inode_item) |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 9 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 10 | struct btrfs_path *path; |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 11 | 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 Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 18 | path = btrfs_alloc_path(); |
| 19 | BUG_ON(!path); |
| 20 | btrfs_init_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 21 | ret = btrfs_insert_item(trans, root, &key, inode_item, |
| 22 | sizeof(*inode_item)); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 23 | btrfs_release_path(root, path); |
| 24 | btrfs_free_path(path); |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 25 | return ret; |
| 26 | } |
| 27 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 28 | int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame^] | 29 | *root, struct btrfs_path *path, |
| 30 | struct btrfs_key *location, int mod) |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 31 | { |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 32 | int ins_len = mod < 0 ? -1 : 0; |
| 33 | int cow = mod != 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame^] | 34 | int ret; |
| 35 | int slot; |
| 36 | struct btrfs_leaf *leaf; |
| 37 | struct btrfs_key found_key; |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 38 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame^] | 39 | ret = btrfs_search_slot(trans, root, location, path, ins_len, cow); |
| 40 | if (ret > 0 && btrfs_key_type(location) == BTRFS_ROOT_ITEM_KEY && |
| 41 | location->offset == (u64)-1 && path->slots[0] != 0) { |
| 42 | slot = path->slots[0] - 1; |
| 43 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 44 | btrfs_disk_key_to_cpu(&found_key, &leaf->items[slot].key); |
| 45 | if (found_key.objectid == location->objectid && |
| 46 | btrfs_key_type(&found_key) == btrfs_key_type(location)) { |
| 47 | path->slots[0]--; |
| 48 | return 0; |
| 49 | } |
| 50 | } |
| 51 | return ret; |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 52 | } |