blob: 2d2c23ca7cbfd30a872c09de172c3de23fd82db8 [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"
Chris Masondee26a92007-03-26 16:00:06 -04003#include "disk-io.h"
Chris Mason9f5fae22007-03-20 14:38:32 -04004#include "transaction.h"
Chris Mason1e1d2702007-03-15 19:03:33 -04005
Chris Masondee26a92007-03-26 16:00:06 -04006int 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 Mason9f5fae22007-03-20 14:38:32 -040011{
Chris Masondee26a92007-03-26 16:00:06 -040012 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 Mason9773a782007-03-27 11:26:26 -040029 BUG_ON(ret);
Chris Masondee26a92007-03-26 16:00:06 -040030 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 Mason71951f32007-03-27 09:16:29 -040036 btrfs_set_file_extent_generation(item, trans->transid);
Chris Masondee26a92007-03-26 16:00:06 -040037 mark_buffer_dirty(path.nodes[0]);
38 *result = ins.objectid;
39 btrfs_release_path(root, &path);
Chris Mason9f5fae22007-03-20 14:38:32 -040040 return 0;
41}
Chris Masondee26a92007-03-26 16:00:06 -040042
43int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
44 struct btrfs_root *root,
45 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -040046 u64 offset, int mod)
Chris Masondee26a92007-03-26 16:00:06 -040047{
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 Mason9773a782007-03-27 11:26:26 -040054 file_key.offset = offset;
Chris Masondee26a92007-03-26 16:00:06 -040055 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}
Chris Masonf254e522007-03-29 15:15:27 -040060
61int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
62 struct btrfs_root *root,
63 u64 objectid, u64 offset,
64 char *data, size_t len)
65{
66 int ret;
67 struct btrfs_key file_key;
68 struct btrfs_path path;
69 struct btrfs_csum_item *item;
70
71 btrfs_init_path(&path);
72 file_key.objectid = objectid;
73 file_key.offset = offset;
74 file_key.flags = 0;
75 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
76 ret = btrfs_insert_empty_item(trans, root, &path, &file_key,
77 BTRFS_CSUM_SIZE);
78 if (ret != 0 && ret != -EEXIST)
79 goto fail;
80 item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
81 struct btrfs_csum_item);
82 ret = 0;
83 ret = btrfs_csum_data(root, data, len, item->csum);
84 mark_buffer_dirty(path.nodes[0]);
85fail:
86 btrfs_release_path(root, &path);
87 return ret;
88}
89
90int btrfs_csum_verify_file_block(struct btrfs_root *root,
91 u64 objectid, u64 offset,
92 char *data, size_t len)
93{
94 int ret;
95 struct btrfs_key file_key;
96 struct btrfs_path path;
97 struct btrfs_csum_item *item;
98 char result[BTRFS_CSUM_SIZE];
99
100 btrfs_init_path(&path);
101 file_key.objectid = objectid;
102 file_key.offset = offset;
103 file_key.flags = 0;
104 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
105 ret = btrfs_search_slot(NULL, root, &file_key, &path, 0, 0);
106 if (ret)
107 goto fail;
108 item = btrfs_item_ptr(btrfs_buffer_leaf(path.nodes[0]), path.slots[0],
109 struct btrfs_csum_item);
110 ret = 0;
111 ret = btrfs_csum_data(root, data, len, result);
112 WARN_ON(ret);
113 if (memcmp(result, item->csum, BTRFS_CSUM_SIZE))
114 ret = 1;
115fail:
116 btrfs_release_path(root, &path);
117 return ret;
118}
119