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" |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 3 | #include "disk-io.h" |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 4 | #include "transaction.h" |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 5 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 6 | int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans, |
| 7 | struct btrfs_root *root, |
| 8 | u64 objectid, u64 offset, |
| 9 | u64 num_blocks, u64 hint_block, |
| 10 | u64 *result) |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 11 | { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 12 | struct btrfs_key ins; |
| 13 | int ret = 0; |
| 14 | struct btrfs_file_extent_item *item; |
| 15 | struct btrfs_key file_key; |
| 16 | struct btrfs_path path; |
| 17 | |
| 18 | btrfs_init_path(&path); |
| 19 | ret = btrfs_alloc_extent(trans, root, num_blocks, hint_block, |
| 20 | (u64)-1, objectid, &ins); |
| 21 | BUG_ON(ret); |
| 22 | file_key.objectid = objectid; |
| 23 | file_key.offset = offset; |
| 24 | file_key.flags = 0; |
| 25 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); |
| 26 | |
| 27 | ret = btrfs_insert_empty_item(trans, root, &path, &file_key, |
| 28 | sizeof(*item)); |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame^] | 29 | BUG_ON(ret); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 30 | item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0], |
| 31 | struct btrfs_file_extent_item); |
| 32 | btrfs_set_file_extent_disk_blocknr(item, ins.objectid); |
| 33 | btrfs_set_file_extent_disk_num_blocks(item, ins.offset); |
| 34 | btrfs_set_file_extent_offset(item, 0); |
| 35 | btrfs_set_file_extent_num_blocks(item, ins.offset); |
Chris Mason | 71951f3 | 2007-03-27 09:16:29 -0400 | [diff] [blame] | 36 | btrfs_set_file_extent_generation(item, trans->transid); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 37 | mark_buffer_dirty(path.nodes[0]); |
| 38 | *result = ins.objectid; |
| 39 | btrfs_release_path(root, &path); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 40 | return 0; |
| 41 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 42 | |
| 43 | int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, |
| 44 | struct btrfs_root *root, |
| 45 | struct btrfs_path *path, u64 objectid, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame^] | 46 | u64 offset, int mod) |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 47 | { |
| 48 | int ret; |
| 49 | struct btrfs_key file_key; |
| 50 | int ins_len = mod < 0 ? -1 : 0; |
| 51 | int cow = mod != 0; |
| 52 | |
| 53 | file_key.objectid = objectid; |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame^] | 54 | file_key.offset = offset; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 55 | file_key.flags = 0; |
| 56 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); |
| 57 | ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); |
| 58 | return ret; |
| 59 | } |