btrfs: Code cleanup
[safe/jmp/linux-2.6] / fs / btrfs / file-item.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
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.
7  *
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.
12  *
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.
17  */
18
19 #include <linux/module.h>
20 #include "ctree.h"
21 #include "disk-io.h"
22 #include "transaction.h"
23 #include "print-tree.h"
24
25 #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
26                                sizeof(struct btrfs_item) * 2) / \
27                                BTRFS_CRC32_SIZE) - 1))
28 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
29                                struct btrfs_root *root,
30                                u64 objectid, u64 pos,
31                                u64 offset, u64 disk_num_blocks,
32                                u64 num_blocks)
33 {
34         int ret = 0;
35         struct btrfs_file_extent_item *item;
36         struct btrfs_key file_key;
37         struct btrfs_path *path;
38
39         path = btrfs_alloc_path();
40         BUG_ON(!path);
41         file_key.objectid = objectid;
42         file_key.offset = pos;
43         file_key.flags = 0;
44         btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
45
46         ret = btrfs_insert_empty_item(trans, root, path, &file_key,
47                                       sizeof(*item));
48         BUG_ON(ret);
49         item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
50                               struct btrfs_file_extent_item);
51         btrfs_set_file_extent_disk_blocknr(item, offset);
52         btrfs_set_file_extent_disk_num_blocks(item, disk_num_blocks);
53         btrfs_set_file_extent_offset(item, 0);
54         btrfs_set_file_extent_num_blocks(item, num_blocks);
55         btrfs_set_file_extent_generation(item, trans->transid);
56         btrfs_set_file_extent_type(item, BTRFS_FILE_EXTENT_REG);
57         btrfs_mark_buffer_dirty(path->nodes[0]);
58
59         btrfs_release_path(root, path);
60         btrfs_free_path(path);
61         return 0;
62 }
63
64 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
65                                           struct btrfs_root *root,
66                                           struct btrfs_path *path,
67                                           u64 objectid, u64 offset,
68                                           int cow)
69 {
70         int ret;
71         struct btrfs_key file_key;
72         struct btrfs_key found_key;
73         struct btrfs_csum_item *item;
74         struct btrfs_leaf *leaf;
75         u64 csum_offset = 0;
76         int csums_in_item;
77
78         file_key.objectid = objectid;
79         file_key.offset = offset;
80         file_key.flags = 0;
81         btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
82         ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
83         if (ret < 0)
84                 goto fail;
85         leaf = btrfs_buffer_leaf(path->nodes[0]);
86         if (ret > 0) {
87                 ret = 1;
88                 if (path->slots[0] == 0)
89                         goto fail;
90                 path->slots[0]--;
91                 btrfs_disk_key_to_cpu(&found_key,
92                                       &leaf->items[path->slots[0]].key);
93                 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
94                     found_key.objectid != objectid) {
95                         goto fail;
96                 }
97                 csum_offset = (offset - found_key.offset) >>
98                                 root->fs_info->sb->s_blocksize_bits;
99                 csums_in_item = btrfs_item_size(leaf->items + path->slots[0]);
100                 csums_in_item /= BTRFS_CRC32_SIZE;
101
102                 if (csum_offset >= csums_in_item) {
103                         ret = -EFBIG;
104                         goto fail;
105                 }
106         }
107         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
108         item = (struct btrfs_csum_item *)((unsigned char *)item +
109                                           csum_offset * BTRFS_CRC32_SIZE);
110         return item;
111 fail:
112         if (ret > 0)
113                 ret = -ENOENT;
114         return ERR_PTR(ret);
115 }
116
117
118 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
119                              struct btrfs_root *root,
120                              struct btrfs_path *path, u64 objectid,
121                              u64 offset, int mod)
122 {
123         int ret;
124         struct btrfs_key file_key;
125         int ins_len = mod < 0 ? -1 : 0;
126         int cow = mod != 0;
127
128         file_key.objectid = objectid;
129         file_key.offset = offset;
130         file_key.flags = 0;
131         btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
132         ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
133         return ret;
134 }
135
136 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
137                           struct btrfs_root *root,
138                           u64 objectid, u64 offset,
139                           char *data, size_t len)
140 {
141         int ret;
142         struct btrfs_key file_key;
143         struct btrfs_key found_key;
144         struct btrfs_path *path;
145         struct btrfs_csum_item *item;
146         struct btrfs_leaf *leaf;
147         u64 csum_offset;
148
149         path = btrfs_alloc_path();
150         BUG_ON(!path);
151
152         file_key.objectid = objectid;
153         file_key.offset = offset;
154         file_key.flags = 0;
155         btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
156
157         item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
158         if (!IS_ERR(item))
159                 goto found;
160         ret = PTR_ERR(item);
161         if (ret == -EFBIG) {
162                 u32 item_size;
163                 /* we found one, but it isn't big enough yet */
164                 leaf = btrfs_buffer_leaf(path->nodes[0]);
165                 item_size = btrfs_item_size(leaf->items + path->slots[0]);
166                 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
167                         /* already at max size, make a new one */
168                         goto insert;
169                 }
170         } else {
171                 /* we didn't find a csum item, insert one */
172                 goto insert;
173         }
174
175         /*
176          * at this point, we know the tree has an item, but it isn't big
177          * enough yet to put our csum in.  Grow it
178          */
179         btrfs_release_path(root, path);
180         ret = btrfs_search_slot(trans, root, &file_key, path,
181                                 BTRFS_CRC32_SIZE, 1);
182         if (ret < 0)
183                 goto fail;
184         if (ret == 0) {
185                 BUG();
186         }
187         if (path->slots[0] == 0) {
188                 goto insert;
189         }
190         path->slots[0]--;
191         leaf = btrfs_buffer_leaf(path->nodes[0]);
192         btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key);
193         csum_offset = (offset - found_key.offset) >>
194                         root->fs_info->sb->s_blocksize_bits;
195         if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
196             found_key.objectid != objectid ||
197             csum_offset >= MAX_CSUM_ITEMS(root)) {
198                 goto insert;
199         }
200         if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) /
201             BTRFS_CRC32_SIZE) {
202                 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
203                 diff = diff - btrfs_item_size(leaf->items + path->slots[0]);
204                 if (diff != BTRFS_CRC32_SIZE)
205                         goto insert;
206                 ret = btrfs_extend_item(trans, root, path, diff);
207                 BUG_ON(ret);
208                 goto csum;
209         }
210
211 insert:
212         btrfs_release_path(root, path);
213         csum_offset = 0;
214         ret = btrfs_insert_empty_item(trans, root, path, &file_key,
215                                       BTRFS_CRC32_SIZE);
216         if (ret != 0) {
217                 WARN_ON(1);
218                 goto fail;
219         }
220 csum:
221         item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
222                               struct btrfs_csum_item);
223         ret = 0;
224         item = (struct btrfs_csum_item *)((unsigned char *)item +
225                                           csum_offset * BTRFS_CRC32_SIZE);
226 found:
227         btrfs_check_bounds(&item->csum, BTRFS_CRC32_SIZE,
228                            path->nodes[0]->b_data,
229                            root->fs_info->sb->s_blocksize);
230         ret = btrfs_csum_data(root, data, len, &item->csum);
231         btrfs_mark_buffer_dirty(path->nodes[0]);
232 fail:
233         btrfs_release_path(root, path);
234         btrfs_free_path(path);
235         return ret;
236 }
237
238 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
239                         struct btrfs_root *root, struct btrfs_path *path,
240                         u64 isize)
241 {
242         struct btrfs_key key;
243         struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[0]);
244         int slot = path->slots[0];
245         int ret;
246         u32 new_item_size;
247         u64 new_item_span;
248         u64 blocks;
249
250         btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
251         if (isize <= key.offset)
252                 return 0;
253         new_item_span = isize - key.offset;
254         blocks = (new_item_span + root->blocksize - 1) >>
255                 root->fs_info->sb->s_blocksize_bits;
256         new_item_size = blocks * BTRFS_CRC32_SIZE;
257         if (new_item_size >= btrfs_item_size(leaf->items + slot))
258                 return 0;
259         ret = btrfs_truncate_item(trans, root, path, new_item_size);
260         BUG_ON(ret);
261         return ret;
262 }
263
264 int btrfs_csum_verify_file_block(struct btrfs_root *root,
265                                  u64 objectid, u64 offset,
266                                  char *data, size_t len)
267 {
268         int ret;
269         struct btrfs_key file_key;
270         struct btrfs_path *path;
271         struct btrfs_csum_item *item;
272         char result[BTRFS_CRC32_SIZE];
273
274         path = btrfs_alloc_path();
275         BUG_ON(!path);
276         file_key.objectid = objectid;
277         file_key.offset = offset;
278         file_key.flags = 0;
279         btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
280         mutex_lock(&root->fs_info->fs_mutex);
281
282         item = btrfs_lookup_csum(NULL, root, path, objectid, offset, 0);
283         if (IS_ERR(item)) {
284                 ret = PTR_ERR(item);
285                 /* a csum that isn't present is a preallocated region. */
286                 if (ret == -ENOENT || ret == -EFBIG)
287                         ret = -ENOENT;
288                 goto fail;
289         }
290
291         ret = btrfs_csum_data(root, data, len, result);
292         WARN_ON(ret);
293         if (memcmp(result, &item->csum, BTRFS_CRC32_SIZE))
294                 ret = 1;
295 fail:
296         btrfs_release_path(root, path);
297         btrfs_free_path(path);
298         mutex_unlock(&root->fs_info->fs_mutex);
299         return ret;
300 }
301