2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
25 #include "transaction.h"
26 #include "print-tree.h"
29 #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
30 sizeof(struct btrfs_item) * 2) / \
31 BTRFS_CRC32_SIZE) - 1))
32 int btrfs_create_file(struct btrfs_trans_handle
*trans
,
33 struct btrfs_root
*root
, u64 dirid
, u64
*objectid
)
38 int btrfs_insert_file_extent(struct btrfs_trans_handle
*trans
,
39 struct btrfs_root
*root
,
40 u64 objectid
, u64 pos
, u64 offset
,
41 u64 disk_num_bytes
, u64 num_bytes
)
44 struct btrfs_file_extent_item
*item
;
45 struct btrfs_key file_key
;
46 struct btrfs_path
*path
;
47 struct extent_buffer
*leaf
;
49 path
= btrfs_alloc_path();
51 file_key
.objectid
= objectid
;
52 file_key
.offset
= pos
;
53 btrfs_set_key_type(&file_key
, BTRFS_EXTENT_DATA_KEY
);
55 ret
= btrfs_insert_empty_item(trans
, root
, path
, &file_key
,
60 leaf
= path
->nodes
[0];
61 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
62 struct btrfs_file_extent_item
);
63 btrfs_set_file_extent_disk_bytenr(leaf
, item
, offset
);
64 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, disk_num_bytes
);
65 btrfs_set_file_extent_offset(leaf
, item
, 0);
66 btrfs_set_file_extent_num_bytes(leaf
, item
, num_bytes
);
67 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
68 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
69 btrfs_mark_buffer_dirty(leaf
);
71 btrfs_free_path(path
);
75 int btrfs_insert_inline_extent(struct btrfs_trans_handle
*trans
,
76 struct btrfs_root
*root
, u64 objectid
,
77 u64 offset
, char *buffer
, size_t size
)
80 struct btrfs_path
*path
;
81 struct extent_buffer
*leaf
;
83 struct btrfs_file_extent_item
*ei
;
88 path
= btrfs_alloc_path();
92 key
.objectid
= objectid
;
94 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
96 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
102 struct btrfs_key found_key
;
104 if (path
->slots
[0] == 0)
108 leaf
= path
->nodes
[0];
109 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
111 if (found_key
.objectid
!= objectid
)
114 if (found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
116 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
117 struct btrfs_file_extent_item
);
119 if (btrfs_file_extent_type(leaf
, ei
) !=
120 BTRFS_FILE_EXTENT_INLINE
) {
123 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
130 leaf
= path
->nodes
[0];
131 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
132 struct btrfs_file_extent_item
);
134 if (btrfs_file_extent_type(leaf
, ei
) !=
135 BTRFS_FILE_EXTENT_INLINE
) {
137 btrfs_print_leaf(root
, leaf
);
138 printk("found wasn't inline offset %llu inode %llu\n",
139 (unsigned long long)offset
,
140 (unsigned long long)objectid
);
143 found_size
= btrfs_file_extent_inline_len(leaf
,
144 btrfs_item_nr(leaf
, path
->slots
[0]));
145 found_end
= key
.offset
+ found_size
;
147 if (found_end
< offset
+ size
) {
148 btrfs_release_path(root
, path
);
149 ret
= btrfs_search_slot(trans
, root
, &key
, path
,
150 offset
+ size
- found_end
, 1);
153 ret
= btrfs_extend_item(trans
, root
, path
,
154 offset
+ size
- found_end
);
159 leaf
= path
->nodes
[0];
160 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
161 struct btrfs_file_extent_item
);
163 if (found_end
< offset
) {
164 ptr
= btrfs_file_extent_inline_start(ei
) + found_size
;
165 memset_extent_buffer(leaf
, 0, ptr
, offset
- found_end
);
169 btrfs_release_path(root
, path
);
170 datasize
= offset
+ size
- key
.offset
;
171 datasize
= btrfs_file_extent_calc_inline_size(datasize
);
172 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
176 printk("got bad ret %d\n", ret
);
179 leaf
= path
->nodes
[0];
180 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
181 struct btrfs_file_extent_item
);
182 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
183 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
185 ptr
= btrfs_file_extent_inline_start(ei
) + offset
- key
.offset
;
186 write_extent_buffer(leaf
, buffer
, ptr
, size
);
187 btrfs_mark_buffer_dirty(leaf
);
189 btrfs_free_path(path
);
193 struct btrfs_csum_item
*btrfs_lookup_csum(struct btrfs_trans_handle
*trans
,
194 struct btrfs_root
*root
,
195 struct btrfs_path
*path
,
196 u64 objectid
, u64 offset
, int cow
)
199 struct btrfs_key file_key
;
200 struct btrfs_key found_key
;
201 struct btrfs_csum_item
*item
;
202 struct extent_buffer
*leaf
;
206 file_key
.objectid
= objectid
;
207 file_key
.offset
= offset
;
208 btrfs_set_key_type(&file_key
, BTRFS_CSUM_ITEM_KEY
);
209 ret
= btrfs_search_slot(trans
, root
, &file_key
, path
, 0, cow
);
212 leaf
= path
->nodes
[0];
215 if (path
->slots
[0] == 0)
218 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
219 if (btrfs_key_type(&found_key
) != BTRFS_CSUM_ITEM_KEY
||
220 found_key
.objectid
!= objectid
) {
223 csum_offset
= (offset
- found_key
.offset
) >> root
->sectorsize
;
224 csums_in_item
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
225 csums_in_item
/= BTRFS_CRC32_SIZE
;
227 if (csum_offset
>= csums_in_item
) {
232 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_csum_item
);
233 item
= (struct btrfs_csum_item
*)((unsigned char *)item
+
234 csum_offset
* BTRFS_CRC32_SIZE
);
243 int btrfs_lookup_file_extent(struct btrfs_trans_handle
*trans
,
244 struct btrfs_root
*root
,
245 struct btrfs_path
*path
, u64 objectid
,
249 struct btrfs_key file_key
;
250 int ins_len
= mod
< 0 ? -1 : 0;
253 file_key
.objectid
= objectid
;
254 file_key
.offset
= offset
;
255 btrfs_set_key_type(&file_key
, BTRFS_EXTENT_DATA_KEY
);
256 ret
= btrfs_search_slot(trans
, root
, &file_key
, path
, ins_len
, cow
);
260 int btrfs_csum_file_block(struct btrfs_trans_handle
*trans
,
261 struct btrfs_root
*root
,
262 struct btrfs_inode_item
*inode
,
263 u64 objectid
, u64 offset
,
264 char *data
, size_t len
)
267 struct btrfs_key file_key
;
268 struct btrfs_key found_key
;
269 u64 next_offset
= (u64
)-1;
271 struct btrfs_path
*path
;
272 struct btrfs_csum_item
*item
;
273 struct extent_buffer
*leaf
= NULL
;
275 u32 csum_result
= ~(u32
)0;
279 path
= btrfs_alloc_path();
282 file_key
.objectid
= objectid
;
283 file_key
.offset
= offset
;
284 btrfs_set_key_type(&file_key
, BTRFS_CSUM_ITEM_KEY
);
286 item
= btrfs_lookup_csum(trans
, root
, path
, objectid
, offset
, 1);
288 leaf
= path
->nodes
[0];
294 /* we found one, but it isn't big enough yet */
295 leaf
= path
->nodes
[0];
296 item_size
= btrfs_item_size_nr(leaf
, path
->slots
[0]);
297 if ((item_size
/ BTRFS_CRC32_SIZE
) >= MAX_CSUM_ITEMS(root
)) {
298 /* already at max size, make a new one */
302 int slot
= path
->slots
[0] + 1;
303 /* we didn't find a csum item, insert one */
304 nritems
= btrfs_header_nritems(path
->nodes
[0]);
305 if (path
->slots
[0] >= nritems
- 1) {
306 ret
= btrfs_next_leaf(root
, path
);
313 btrfs_item_key_to_cpu(path
->nodes
[0], &found_key
, slot
);
314 if (found_key
.objectid
!= objectid
||
315 found_key
.type
!= BTRFS_CSUM_ITEM_KEY
) {
319 next_offset
= found_key
.offset
;
325 * at this point, we know the tree has an item, but it isn't big
326 * enough yet to put our csum in. Grow it
328 btrfs_release_path(root
, path
);
329 ret
= btrfs_search_slot(trans
, root
, &file_key
, path
,
330 BTRFS_CRC32_SIZE
, 1);
336 if (path
->slots
[0] == 0) {
340 leaf
= path
->nodes
[0];
341 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
342 csum_offset
= (offset
- found_key
.offset
) / root
->sectorsize
;
343 if (btrfs_key_type(&found_key
) != BTRFS_CSUM_ITEM_KEY
||
344 found_key
.objectid
!= objectid
||
345 csum_offset
>= MAX_CSUM_ITEMS(root
)) {
348 if (csum_offset
>= btrfs_item_size_nr(leaf
, path
->slots
[0]) /
350 u32 diff
= (csum_offset
+ 1) * BTRFS_CRC32_SIZE
;
351 diff
= diff
- btrfs_item_size_nr(leaf
, path
->slots
[0]);
352 if (diff
!= BTRFS_CRC32_SIZE
)
354 ret
= btrfs_extend_item(trans
, root
, path
, diff
);
360 btrfs_release_path(root
, path
);
363 u64 tmp
= min(btrfs_stack_inode_size(inode
), next_offset
);
364 tmp
-= offset
& ~((u64
)root
->sectorsize
-1);
365 tmp
/= root
->sectorsize
;
366 tmp
= max((u64
)1, tmp
);
367 tmp
= min(tmp
, (u64
)MAX_CSUM_ITEMS(root
));
368 ins_size
= BTRFS_CRC32_SIZE
* tmp
;
370 ins_size
= BTRFS_CRC32_SIZE
;
372 ret
= btrfs_insert_empty_item(trans
, root
, path
, &file_key
,
381 leaf
= path
->nodes
[0];
382 item
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_csum_item
);
384 item
= (struct btrfs_csum_item
*)((unsigned char *)item
+
385 csum_offset
* BTRFS_CRC32_SIZE
);
387 csum_result
= btrfs_csum_data(root
, data
, csum_result
, len
);
388 btrfs_csum_final(csum_result
, (char *)&csum_result
);
389 if (csum_result
== 0) {
390 printk("csum result is 0 for inode %llu offset %llu\n",
391 (unsigned long long)objectid
,
392 (unsigned long long)offset
);
395 write_extent_buffer(leaf
, &csum_result
, (unsigned long)item
,
397 btrfs_mark_buffer_dirty(path
->nodes
[0]);
399 btrfs_release_path(root
, path
);
400 btrfs_free_path(path
);
404 int btrfs_csum_truncate(struct btrfs_trans_handle
*trans
,
405 struct btrfs_root
*root
, struct btrfs_path
*path
,
408 struct btrfs_key key
;
409 struct extent_buffer
*leaf
= path
->nodes
[0];
410 int slot
= path
->slots
[0];
416 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
417 if (isize
<= key
.offset
)
419 new_item_span
= isize
- key
.offset
;
420 blocks
= (new_item_span
+ root
->sectorsize
- 1) / root
->sectorsize
;
421 new_item_size
= blocks
* BTRFS_CRC32_SIZE
;
422 if (new_item_size
>= btrfs_item_size_nr(leaf
, slot
))
424 ret
= btrfs_truncate_item(trans
, root
, path
, new_item_size
, 1);