]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/btrfs/file-item.c
Btrfs: Fixes for 2.6.18 enterprise kernels
[mirror_ubuntu-bionic-kernel.git] / fs / btrfs / file-item.c
CommitLineData
6cbd5570
CM
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
065631f6
CM
19#include <linux/bio.h>
20#include <linux/pagemap.h>
21#include <linux/highmem.h>
1e1d2701 22#include "ctree.h"
dee26a9f 23#include "disk-io.h"
9f5fae2f 24#include "transaction.h"
1de037a4 25#include "print-tree.h"
1e1d2701 26
6567e837 27#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
509659cd
CM
28 sizeof(struct btrfs_item) * 2) / \
29 BTRFS_CRC32_SIZE) - 1))
b18c6685 30int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
dee26a9f 31 struct btrfs_root *root,
b18c6685 32 u64 objectid, u64 pos,
db94535d
CM
33 u64 offset, u64 disk_num_bytes,
34 u64 num_bytes)
9f5fae2f 35{
dee26a9f
CM
36 int ret = 0;
37 struct btrfs_file_extent_item *item;
38 struct btrfs_key file_key;
5caf2a00 39 struct btrfs_path *path;
5f39d397 40 struct extent_buffer *leaf;
dee26a9f 41
5caf2a00
CM
42 path = btrfs_alloc_path();
43 BUG_ON(!path);
dee26a9f 44 file_key.objectid = objectid;
b18c6685 45 file_key.offset = pos;
dee26a9f
CM
46 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
47
5caf2a00 48 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
dee26a9f 49 sizeof(*item));
54aa1f4d
CM
50 if (ret < 0)
51 goto out;
9773a788 52 BUG_ON(ret);
5f39d397
CM
53 leaf = path->nodes[0];
54 item = btrfs_item_ptr(leaf, path->slots[0],
dee26a9f 55 struct btrfs_file_extent_item);
db94535d
CM
56 btrfs_set_file_extent_disk_bytenr(leaf, item, offset);
57 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
5f39d397 58 btrfs_set_file_extent_offset(leaf, item, 0);
db94535d 59 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
5f39d397
CM
60 btrfs_set_file_extent_generation(leaf, item, trans->transid);
61 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
62 btrfs_mark_buffer_dirty(leaf);
54aa1f4d 63out:
5caf2a00 64 btrfs_free_path(path);
54aa1f4d 65 return ret;
9f5fae2f 66}
dee26a9f 67
b18c6685
CM
68struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
69 struct btrfs_root *root,
70 struct btrfs_path *path,
71 u64 objectid, u64 offset,
72 int cow)
6567e837
CM
73{
74 int ret;
75 struct btrfs_key file_key;
76 struct btrfs_key found_key;
77 struct btrfs_csum_item *item;
5f39d397 78 struct extent_buffer *leaf;
6567e837 79 u64 csum_offset = 0;
a429e513 80 int csums_in_item;
6567e837
CM
81
82 file_key.objectid = objectid;
83 file_key.offset = offset;
6567e837 84 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
b18c6685 85 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
6567e837
CM
86 if (ret < 0)
87 goto fail;
5f39d397 88 leaf = path->nodes[0];
6567e837
CM
89 if (ret > 0) {
90 ret = 1;
70b2befd 91 if (path->slots[0] == 0)
6567e837
CM
92 goto fail;
93 path->slots[0]--;
5f39d397 94 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6567e837
CM
95 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
96 found_key.objectid != objectid) {
97 goto fail;
98 }
99 csum_offset = (offset - found_key.offset) >>
100 root->fs_info->sb->s_blocksize_bits;
5f39d397 101 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
509659cd 102 csums_in_item /= BTRFS_CRC32_SIZE;
a429e513
CM
103
104 if (csum_offset >= csums_in_item) {
105 ret = -EFBIG;
6567e837
CM
106 goto fail;
107 }
108 }
109 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
509659cd
CM
110 item = (struct btrfs_csum_item *)((unsigned char *)item +
111 csum_offset * BTRFS_CRC32_SIZE);
6567e837
CM
112 return item;
113fail:
114 if (ret > 0)
b18c6685 115 ret = -ENOENT;
6567e837
CM
116 return ERR_PTR(ret);
117}
118
119
dee26a9f
CM
120int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
121 struct btrfs_root *root,
122 struct btrfs_path *path, u64 objectid,
9773a788 123 u64 offset, int mod)
dee26a9f
CM
124{
125 int ret;
126 struct btrfs_key file_key;
127 int ins_len = mod < 0 ? -1 : 0;
128 int cow = mod != 0;
129
130 file_key.objectid = objectid;
70b2befd 131 file_key.offset = offset;
dee26a9f
CM
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}
f254e52c 136
e015640f
CM
137int btrfs_csum_one_bio(struct btrfs_root *root,
138 struct bio *bio, char **sums_ret)
139{
140 u32 *sums;
141 char *data;
142 struct bio_vec *bvec = bio->bi_io_vec;
143 int bio_index = 0;
144
145 sums = kmalloc(bio->bi_vcnt * BTRFS_CRC32_SIZE, GFP_NOFS);
146 if (!sums)
147 return -ENOMEM;
148 *sums_ret = (char *)sums;
149
150 while(bio_index < bio->bi_vcnt) {
151 data = kmap_atomic(bvec->bv_page, KM_USER0);
152 *sums = ~(u32)0;
153 *sums = btrfs_csum_data(root, data + bvec->bv_offset,
154 *sums, bvec->bv_len);
155 kunmap_atomic(data, KM_USER0);
156 btrfs_csum_final(*sums, (char *)sums);
157 sums++;
158 bio_index++;
159 bvec++;
160 }
161 return 0;
162}
163
065631f6
CM
164int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
165 struct btrfs_root *root, struct inode *inode,
e015640f 166 struct bio *bio, char *sums)
f254e52c 167{
065631f6
CM
168 u64 objectid = inode->i_ino;
169 u64 offset;
f254e52c
CM
170 int ret;
171 struct btrfs_key file_key;
6567e837 172 struct btrfs_key found_key;
065631f6
CM
173 u64 next_offset;
174 int found_next;
5caf2a00 175 struct btrfs_path *path;
f254e52c 176 struct btrfs_csum_item *item;
065631f6 177 struct btrfs_csum_item *item_end;
ff79f819 178 struct extent_buffer *leaf = NULL;
6567e837 179 u64 csum_offset;
e015640f 180 u32 *sums32 = (u32 *)sums;
f578d4bd
CM
181 u32 nritems;
182 u32 ins_size;
065631f6
CM
183 int bio_index = 0;
184 struct bio_vec *bvec = bio->bi_io_vec;
6e92f5e6
CM
185 char *eb_map;
186 char *eb_token;
187 unsigned long map_len;
188 unsigned long map_start;
189
5caf2a00
CM
190 path = btrfs_alloc_path();
191 BUG_ON(!path);
065631f6
CM
192again:
193 next_offset = (u64)-1;
194 found_next = 0;
195 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
f254e52c
CM
196 file_key.objectid = objectid;
197 file_key.offset = offset;
f254e52c 198 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
a429e513
CM
199
200 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
ff79f819
CM
201 if (!IS_ERR(item)) {
202 leaf = path->nodes[0];
a429e513 203 goto found;
ff79f819 204 }
a429e513
CM
205 ret = PTR_ERR(item);
206 if (ret == -EFBIG) {
207 u32 item_size;
208 /* we found one, but it isn't big enough yet */
5f39d397
CM
209 leaf = path->nodes[0];
210 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
509659cd 211 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
a429e513
CM
212 /* already at max size, make a new one */
213 goto insert;
214 }
215 } else {
f578d4bd 216 int slot = path->slots[0] + 1;
a429e513 217 /* we didn't find a csum item, insert one */
f578d4bd
CM
218 nritems = btrfs_header_nritems(path->nodes[0]);
219 if (path->slots[0] >= nritems - 1) {
220 ret = btrfs_next_leaf(root, path);
b56baf5b 221 if (ret == 1)
f578d4bd 222 found_next = 1;
b56baf5b 223 if (ret != 0)
f578d4bd 224 goto insert;
b56baf5b 225 slot = 0;
f578d4bd
CM
226 }
227 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
228 if (found_key.objectid != objectid ||
229 found_key.type != BTRFS_CSUM_ITEM_KEY) {
230 found_next = 1;
231 goto insert;
232 }
233 next_offset = found_key.offset;
234 found_next = 1;
a429e513
CM
235 goto insert;
236 }
237
238 /*
239 * at this point, we know the tree has an item, but it isn't big
240 * enough yet to put our csum in. Grow it
241 */
242 btrfs_release_path(root, path);
6567e837 243 ret = btrfs_search_slot(trans, root, &file_key, path,
509659cd 244 BTRFS_CRC32_SIZE, 1);
6567e837
CM
245 if (ret < 0)
246 goto fail;
247 if (ret == 0) {
b18c6685 248 BUG();
6567e837
CM
249 }
250 if (path->slots[0] == 0) {
6567e837
CM
251 goto insert;
252 }
253 path->slots[0]--;
5f39d397
CM
254 leaf = path->nodes[0];
255 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6567e837
CM
256 csum_offset = (offset - found_key.offset) >>
257 root->fs_info->sb->s_blocksize_bits;
258 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
259 found_key.objectid != objectid ||
260 csum_offset >= MAX_CSUM_ITEMS(root)) {
6567e837
CM
261 goto insert;
262 }
5f39d397 263 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
509659cd
CM
264 BTRFS_CRC32_SIZE) {
265 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
5f39d397 266 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
3a686375
CM
267 if (diff != BTRFS_CRC32_SIZE)
268 goto insert;
a429e513 269 ret = btrfs_extend_item(trans, root, path, diff);
6567e837
CM
270 BUG_ON(ret);
271 goto csum;
272 }
273
274insert:
a429e513 275 btrfs_release_path(root, path);
6567e837 276 csum_offset = 0;
f578d4bd
CM
277 if (found_next) {
278 u64 tmp = min((u64)i_size_read(inode), next_offset);
b56baf5b 279 tmp -= offset & ~((u64)root->sectorsize -1);
f578d4bd
CM
280 tmp >>= root->fs_info->sb->s_blocksize_bits;
281 tmp = max((u64)1, tmp);
282 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
283 ins_size = BTRFS_CRC32_SIZE * tmp;
284 } else {
285 ins_size = BTRFS_CRC32_SIZE;
286 }
5caf2a00 287 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
f578d4bd 288 ins_size);
54aa1f4d
CM
289 if (ret < 0)
290 goto fail;
a429e513 291 if (ret != 0) {
a429e513 292 WARN_ON(1);
f254e52c 293 goto fail;
a429e513 294 }
6567e837 295csum:
5f39d397
CM
296 leaf = path->nodes[0];
297 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
f254e52c 298 ret = 0;
509659cd
CM
299 item = (struct btrfs_csum_item *)((unsigned char *)item +
300 csum_offset * BTRFS_CRC32_SIZE);
b18c6685 301found:
065631f6
CM
302 item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
303 item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
304 btrfs_item_size_nr(leaf, path->slots[0]));
6e92f5e6 305 eb_token = NULL;
065631f6 306next_bvec:
aadfeb6e 307
6e92f5e6
CM
308 if (!eb_token ||
309 (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) {
310 int err;
311
312 if (eb_token)
eb20978f 313 unmap_extent_buffer(leaf, eb_token, KM_USER1);
6e92f5e6
CM
314 eb_token = NULL;
315 err = map_private_extent_buffer(leaf, (unsigned long)item,
316 BTRFS_CRC32_SIZE,
317 &eb_token, &eb_map,
eb20978f 318 &map_start, &map_len, KM_USER1);
6e92f5e6
CM
319 if (err)
320 eb_token = NULL;
321 }
322 if (eb_token) {
323 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
e015640f 324 sums32, BTRFS_CRC32_SIZE);
6e92f5e6 325 } else {
e015640f 326 write_extent_buffer(leaf, sums32, (unsigned long)item,
6e92f5e6
CM
327 BTRFS_CRC32_SIZE);
328 }
065631f6
CM
329 bio_index++;
330 bvec++;
e015640f 331 sums32++;
065631f6 332 if (bio_index < bio->bi_vcnt) {
6e92f5e6
CM
333 item = (struct btrfs_csum_item *)((char *)item +
334 BTRFS_CRC32_SIZE);
2e1a992e
CM
335 if (item < item_end && offset + PAGE_CACHE_SIZE ==
336 page_offset(bvec->bv_page)) {
337 offset = page_offset(bvec->bv_page);
065631f6 338 goto next_bvec;
2e1a992e 339 }
065631f6 340 }
6e92f5e6 341 if (eb_token) {
eb20978f 342 unmap_extent_buffer(leaf, eb_token, KM_USER1);
6e92f5e6
CM
343 eb_token = NULL;
344 }
5caf2a00 345 btrfs_mark_buffer_dirty(path->nodes[0]);
065631f6
CM
346 if (bio_index < bio->bi_vcnt) {
347 btrfs_release_path(root, path);
348 goto again;
349 }
f254e52c 350fail:
5caf2a00 351 btrfs_free_path(path);
f254e52c
CM
352 return ret;
353}
354
1de037a4
CM
355int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
356 struct btrfs_root *root, struct btrfs_path *path,
357 u64 isize)
358{
359 struct btrfs_key key;
5f39d397 360 struct extent_buffer *leaf = path->nodes[0];
1de037a4
CM
361 int slot = path->slots[0];
362 int ret;
363 u32 new_item_size;
364 u64 new_item_span;
365 u64 blocks;
366
5f39d397 367 btrfs_item_key_to_cpu(leaf, &key, slot);
1de037a4
CM
368 if (isize <= key.offset)
369 return 0;
370 new_item_span = isize - key.offset;
5f39d397 371 blocks = (new_item_span + root->sectorsize - 1) >>
84f54cfa 372 root->fs_info->sb->s_blocksize_bits;
1de037a4 373 new_item_size = blocks * BTRFS_CRC32_SIZE;
5f39d397 374 if (new_item_size >= btrfs_item_size_nr(leaf, slot))
1de037a4 375 return 0;
179e29e4 376 ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
1de037a4
CM
377 BUG_ON(ret);
378 return ret;
379}
380