1 #include <linux/module.h>
4 #include "transaction.h"
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,
14 struct btrfs_file_extent_item *item;
15 struct btrfs_key file_key;
16 struct btrfs_path *path;
18 path = btrfs_alloc_path();
20 btrfs_init_path(path);
21 ret = btrfs_alloc_extent(trans, root, num_blocks, hint_block,
24 file_key.objectid = objectid;
25 file_key.offset = offset;
27 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
29 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
32 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
33 struct btrfs_file_extent_item);
34 btrfs_set_file_extent_disk_blocknr(item, ins.objectid);
35 btrfs_set_file_extent_disk_num_blocks(item, ins.offset);
36 btrfs_set_file_extent_offset(item, 0);
37 btrfs_set_file_extent_num_blocks(item, ins.offset);
38 btrfs_set_file_extent_generation(item, trans->transid);
39 btrfs_mark_buffer_dirty(path->nodes[0]);
40 *result = ins.objectid;
41 btrfs_release_path(root, path);
42 btrfs_free_path(path);
46 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
47 struct btrfs_root *root,
48 struct btrfs_path *path, u64 objectid,
52 struct btrfs_key file_key;
53 int ins_len = mod < 0 ? -1 : 0;
56 file_key.objectid = objectid;
57 file_key.offset = offset;
59 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
60 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
64 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
65 struct btrfs_root *root,
66 u64 objectid, u64 offset,
67 char *data, size_t len)
70 struct btrfs_key file_key;
71 struct btrfs_path *path;
72 struct btrfs_csum_item *item;
74 path = btrfs_alloc_path();
76 btrfs_init_path(path);
77 file_key.objectid = objectid;
78 file_key.offset = offset;
80 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
81 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
83 if (ret != 0 && ret != -EEXIST)
85 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
86 struct btrfs_csum_item);
88 ret = btrfs_csum_data(root, data, len, item->csum);
89 btrfs_mark_buffer_dirty(path->nodes[0]);
91 btrfs_release_path(root, path);
92 btrfs_free_path(path);
96 int btrfs_csum_verify_file_block(struct btrfs_root *root,
97 u64 objectid, u64 offset,
98 char *data, size_t len)
101 struct btrfs_key file_key;
102 struct btrfs_path *path;
103 struct btrfs_csum_item *item;
104 char result[BTRFS_CSUM_SIZE];
106 path = btrfs_alloc_path();
108 btrfs_init_path(path);
109 file_key.objectid = objectid;
110 file_key.offset = offset;
112 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
113 mutex_lock(&root->fs_info->fs_mutex);
114 ret = btrfs_search_slot(NULL, root, &file_key, path, 0, 0);
117 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
118 struct btrfs_csum_item);
120 ret = btrfs_csum_data(root, data, len, result);
122 if (memcmp(result, item->csum, BTRFS_CSUM_SIZE))
125 btrfs_release_path(root, path);
126 btrfs_free_path(path);
127 mutex_unlock(&root->fs_info->fs_mutex);