aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/file-item.c
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2007-04-17 13:26:50 -0400
committerDavid Woodhouse <dwmw2@hera.kernel.org>2007-04-17 13:26:50 -0400
commitb18c6685810af8e6763760711aece31ccc7a8ea8 (patch)
treedb7220ed6fb418fbdc069ac422fc8b1c1598e92f /fs/btrfs/file-item.c
parent6567e837df07e43bffc08ac40858af8133a007bf (diff)
Btrfs: progress on file_write
Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/file-item.c')
-rw-r--r--fs/btrfs/file-item.c61
1 files changed, 41 insertions, 20 deletions
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index 93d42d650820..f49968ad0a07 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -6,13 +6,11 @@
6#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \ 6#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
7 sizeof(struct btrfs_item)) / \ 7 sizeof(struct btrfs_item)) / \
8 sizeof(struct btrfs_csum_item)) - 1)) 8 sizeof(struct btrfs_csum_item)) - 1))
9int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans, 9int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
10 struct btrfs_root *root, 10 struct btrfs_root *root,
11 u64 objectid, u64 offset, 11 u64 objectid, u64 pos,
12 u64 num_blocks, u64 hint_block, 12 u64 offset, u64 num_blocks)
13 u64 *result)
14{ 13{
15 struct btrfs_key ins;
16 int ret = 0; 14 int ret = 0;
17 struct btrfs_file_extent_item *item; 15 struct btrfs_file_extent_item *item;
18 struct btrfs_key file_key; 16 struct btrfs_key file_key;
@@ -21,11 +19,13 @@ int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans,
21 path = btrfs_alloc_path(); 19 path = btrfs_alloc_path();
22 BUG_ON(!path); 20 BUG_ON(!path);
23 btrfs_init_path(path); 21 btrfs_init_path(path);
22 /*
24 ret = btrfs_alloc_extent(trans, root, num_blocks, hint_block, 23 ret = btrfs_alloc_extent(trans, root, num_blocks, hint_block,
25 (u64)-1, &ins); 24 (u64)-1, &ins);
25 */
26 BUG_ON(ret); 26 BUG_ON(ret);
27 file_key.objectid = objectid; 27 file_key.objectid = objectid;
28 file_key.offset = offset; 28 file_key.offset = pos;
29 file_key.flags = 0; 29 file_key.flags = 0;
30 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 30 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
31 31
@@ -34,21 +34,22 @@ int btrfs_alloc_file_extent(struct btrfs_trans_handle *trans,
34 BUG_ON(ret); 34 BUG_ON(ret);
35 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], 35 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
36 struct btrfs_file_extent_item); 36 struct btrfs_file_extent_item);
37 btrfs_set_file_extent_disk_blocknr(item, ins.objectid); 37 btrfs_set_file_extent_disk_blocknr(item, offset);
38 btrfs_set_file_extent_disk_num_blocks(item, ins.offset); 38 btrfs_set_file_extent_disk_num_blocks(item, num_blocks);
39 btrfs_set_file_extent_offset(item, 0); 39 btrfs_set_file_extent_offset(item, 0);
40 btrfs_set_file_extent_num_blocks(item, ins.offset); 40 btrfs_set_file_extent_num_blocks(item, num_blocks);
41 btrfs_set_file_extent_generation(item, trans->transid); 41 btrfs_set_file_extent_generation(item, trans->transid);
42 btrfs_mark_buffer_dirty(path->nodes[0]); 42 btrfs_mark_buffer_dirty(path->nodes[0]);
43 *result = ins.objectid;
44 btrfs_release_path(root, path); 43 btrfs_release_path(root, path);
45 btrfs_free_path(path); 44 btrfs_free_path(path);
46 return 0; 45 return 0;
47} 46}
48 47
49static struct btrfs_csum_item *__lookup_csum_item(struct btrfs_root *root, 48struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
50 struct btrfs_path *path, 49 struct btrfs_root *root,
51 u64 objectid, u64 offset) 50 struct btrfs_path *path,
51 u64 objectid, u64 offset,
52 int cow)
52{ 53{
53 int ret; 54 int ret;
54 struct btrfs_key file_key; 55 struct btrfs_key file_key;
@@ -61,19 +62,23 @@ static struct btrfs_csum_item *__lookup_csum_item(struct btrfs_root *root,
61 file_key.offset = offset; 62 file_key.offset = offset;
62 file_key.flags = 0; 63 file_key.flags = 0;
63 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 64 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
64 ret = btrfs_search_slot(NULL, root, &file_key, path, 0, 0); 65printk("__lookup for %Lu\n", offset);
66 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
65 if (ret < 0) 67 if (ret < 0)
66 goto fail; 68 goto fail;
67 leaf = btrfs_buffer_leaf(path->nodes[0]); 69 leaf = btrfs_buffer_leaf(path->nodes[0]);
68 if (ret > 0) { 70 if (ret > 0) {
69 ret = 1; 71 ret = 1;
70 if (path->slots[0] == 0) 72 if (path->slots[0] == 0) {
73printk("fail1\n");
71 goto fail; 74 goto fail;
75 }
72 path->slots[0]--; 76 path->slots[0]--;
73 btrfs_disk_key_to_cpu(&found_key, 77 btrfs_disk_key_to_cpu(&found_key,
74 &leaf->items[path->slots[0]].key); 78 &leaf->items[path->slots[0]].key);
75 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || 79 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
76 found_key.objectid != objectid) { 80 found_key.objectid != objectid) {
81printk("fail2 type %u %Lu %Lu\n", btrfs_key_type(&found_key), found_key.objectid, objectid);
77 goto fail; 82 goto fail;
78 } 83 }
79 csum_offset = (offset - found_key.offset) >> 84 csum_offset = (offset - found_key.offset) >>
@@ -81,6 +86,7 @@ static struct btrfs_csum_item *__lookup_csum_item(struct btrfs_root *root,
81 if (csum_offset >= 86 if (csum_offset >=
82 btrfs_item_size(leaf->items + path->slots[0]) / 87 btrfs_item_size(leaf->items + path->slots[0]) /
83 sizeof(struct btrfs_csum_item)) { 88 sizeof(struct btrfs_csum_item)) {
89printk("fail3, csum offset %lu size %u\n", csum_offset, btrfs_item_size(leaf->items + path->slots[0]) / sizeof(struct btrfs_csum_item));
84 goto fail; 90 goto fail;
85 } 91 }
86 } 92 }
@@ -89,7 +95,7 @@ static struct btrfs_csum_item *__lookup_csum_item(struct btrfs_root *root,
89 return item; 95 return item;
90fail: 96fail:
91 if (ret > 0) 97 if (ret > 0)
92 ret = -EIO; 98 ret = -ENOENT;
93 return ERR_PTR(ret); 99 return ERR_PTR(ret);
94} 100}
95 101
@@ -105,7 +111,7 @@ int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
105 int cow = mod != 0; 111 int cow = mod != 0;
106 struct btrfs_csum_item *csum_item; 112 struct btrfs_csum_item *csum_item;
107 113
108 csum_item = __lookup_csum_item(root, path, objectid, offset); 114 csum_item = btrfs_lookup_csum(trans, root, path, objectid, offset, 0);
109 if (IS_ERR(csum_item)) 115 if (IS_ERR(csum_item))
110 return PTR_ERR(csum_item); 116 return PTR_ERR(csum_item);
111 file_key.objectid = objectid; 117 file_key.objectid = objectid;
@@ -113,7 +119,9 @@ int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
113 file_key.flags = 0; 119 file_key.flags = 0;
114 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); 120 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
115 btrfs_release_path(root, path); 121 btrfs_release_path(root, path);
122printk("lookup file extent searches for %Lu\n", file_key.offset);
116 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); 123 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
124printk("ret is %d\n", ret);
117 return ret; 125 return ret;
118} 126}
119 127
@@ -134,17 +142,23 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
134 path = btrfs_alloc_path(); 142 path = btrfs_alloc_path();
135 BUG_ON(!path); 143 BUG_ON(!path);
136 btrfs_init_path(path); 144 btrfs_init_path(path);
145
146 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 0);
147 if (!IS_ERR(item))
148 goto found;
149 btrfs_release_path(root, path);
137 file_key.objectid = objectid; 150 file_key.objectid = objectid;
138 file_key.offset = offset; 151 file_key.offset = offset;
139 file_key.flags = 0; 152 file_key.flags = 0;
140 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 153 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
154printk("searching for csum %Lu %Lu\n", objectid, offset);
141 ret = btrfs_search_slot(trans, root, &file_key, path, 155 ret = btrfs_search_slot(trans, root, &file_key, path,
142 sizeof(struct btrfs_csum_item), 1); 156 sizeof(struct btrfs_csum_item), 1);
157printk("ret %d\n", ret);
143 if (ret < 0) 158 if (ret < 0)
144 goto fail; 159 goto fail;
145 if (ret == 0) { 160 if (ret == 0) {
146 csum_offset = 0; 161 BUG();
147 goto csum;
148 } 162 }
149 if (path->slots[0] == 0) { 163 if (path->slots[0] == 0) {
150 btrfs_release_path(root, path); 164 btrfs_release_path(root, path);
@@ -153,12 +167,15 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
153 path->slots[0]--; 167 path->slots[0]--;
154 leaf = btrfs_buffer_leaf(path->nodes[0]); 168 leaf = btrfs_buffer_leaf(path->nodes[0]);
155 btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key); 169 btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key);
170printk("found key %Lu %Lu %u\n", found_key.objectid, found_key.offset, found_key.flags);
156 csum_offset = (offset - found_key.offset) >> 171 csum_offset = (offset - found_key.offset) >>
157 root->fs_info->sb->s_blocksize_bits; 172 root->fs_info->sb->s_blocksize_bits;
173printk("csum_offset %Lu\n", csum_offset);
158 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || 174 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
159 found_key.objectid != objectid || 175 found_key.objectid != objectid ||
160 csum_offset >= MAX_CSUM_ITEMS(root)) { 176 csum_offset >= MAX_CSUM_ITEMS(root)) {
161 btrfs_release_path(root, path); 177 btrfs_release_path(root, path);
178printk("insert1\n");
162 goto insert; 179 goto insert;
163 } 180 }
164 if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) / 181 if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) /
@@ -166,11 +183,13 @@ int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
166 ret = btrfs_extend_item(trans, root, path, 183 ret = btrfs_extend_item(trans, root, path,
167 sizeof(struct btrfs_csum_item)); 184 sizeof(struct btrfs_csum_item));
168 BUG_ON(ret); 185 BUG_ON(ret);
186printk("item extended\n");
169 goto csum; 187 goto csum;
170 } 188 }
171 189
172insert: 190insert:
173 csum_offset = 0; 191 csum_offset = 0;
192printk("inserting item %Lu %Lu %u\n", file_key.objectid, file_key.offset, file_key.flags);
174 ret = btrfs_insert_empty_item(trans, root, path, &file_key, 193 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
175 sizeof(struct btrfs_csum_item)); 194 sizeof(struct btrfs_csum_item));
176 if (ret != 0 && ret != -EEXIST) 195 if (ret != 0 && ret != -EEXIST)
@@ -180,12 +199,14 @@ csum:
180 struct btrfs_csum_item); 199 struct btrfs_csum_item);
181 ret = 0; 200 ret = 0;
182 item += csum_offset; 201 item += csum_offset;
202found:
183 ret = btrfs_csum_data(root, data, len, item->csum); 203 ret = btrfs_csum_data(root, data, len, item->csum);
184 btrfs_set_csum_extent_offset(item, extent_offset); 204 btrfs_set_csum_extent_offset(item, extent_offset);
185 btrfs_mark_buffer_dirty(path->nodes[0]); 205 btrfs_mark_buffer_dirty(path->nodes[0]);
186fail: 206fail:
187 btrfs_release_path(root, path); 207 btrfs_release_path(root, path);
188 btrfs_free_path(path); 208 btrfs_free_path(path);
209printk("return ret %d\n", ret);
189 return ret; 210 return ret;
190} 211}
191 212
@@ -208,7 +229,7 @@ int btrfs_csum_verify_file_block(struct btrfs_root *root,
208 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); 229 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
209 mutex_lock(&root->fs_info->fs_mutex); 230 mutex_lock(&root->fs_info->fs_mutex);
210 231
211 item = __lookup_csum_item(root, path, objectid, offset); 232 item = btrfs_lookup_csum(NULL, root, path, objectid, offset, 0);
212 if (IS_ERR(item)) { 233 if (IS_ERR(item)) {
213 ret = PTR_ERR(item); 234 ret = PTR_ERR(item);
214 goto fail; 235 goto fail;