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