]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/btrfs/file-item.c
Btrfs: Cleanup and comment ordered-data.c
[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,
f2eb0a24
SW
31 struct btrfs_root *root,
32 u64 objectid, u64 pos,
33 u64 disk_offset, u64 disk_num_bytes,
34 u64 num_bytes, u64 offset)
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);
f2eb0a24 56 btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
db94535d 57 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
f2eb0a24 58 btrfs_set_file_extent_offset(leaf, item, offset);
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 137int btrfs_csum_one_bio(struct btrfs_root *root,
e6dcd2dc 138 struct bio *bio, struct btrfs_ordered_sum **sums_ret)
e015640f 139{
e6dcd2dc
CM
140 struct btrfs_ordered_sum *sums;
141 struct btrfs_sector_sum *sector_sum;
e015640f
CM
142 char *data;
143 struct bio_vec *bvec = bio->bi_io_vec;
144 int bio_index = 0;
145
e6dcd2dc
CM
146 WARN_ON(bio->bi_vcnt <= 0);
147 sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
e015640f
CM
148 if (!sums)
149 return -ENOMEM;
e6dcd2dc
CM
150 *sums_ret = sums;
151 sector_sum = &sums->sums;
152 sums->file_offset = page_offset(bvec->bv_page);
153 sums->len = bio->bi_size;
154 INIT_LIST_HEAD(&sums->list);
e015640f
CM
155
156 while(bio_index < bio->bi_vcnt) {
157 data = kmap_atomic(bvec->bv_page, KM_USER0);
e6dcd2dc
CM
158 sector_sum->sum = ~(u32)0;
159 sector_sum->sum = btrfs_csum_data(root,
160 data + bvec->bv_offset,
161 sector_sum->sum,
162 bvec->bv_len);
e015640f 163 kunmap_atomic(data, KM_USER0);
e6dcd2dc
CM
164 btrfs_csum_final(sector_sum->sum,
165 (char *)&sector_sum->sum);
166 sector_sum->offset = page_offset(bvec->bv_page) +
167 bvec->bv_offset;
168 sector_sum++;
e015640f
CM
169 bio_index++;
170 bvec++;
171 }
172 return 0;
173}
174
065631f6
CM
175int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
176 struct btrfs_root *root, struct inode *inode,
e6dcd2dc 177 struct btrfs_ordered_sum *sums)
f254e52c 178{
065631f6
CM
179 u64 objectid = inode->i_ino;
180 u64 offset;
f254e52c
CM
181 int ret;
182 struct btrfs_key file_key;
6567e837 183 struct btrfs_key found_key;
065631f6 184 u64 next_offset;
e6dcd2dc 185 u64 total_bytes = 0;
065631f6 186 int found_next;
5caf2a00 187 struct btrfs_path *path;
f254e52c 188 struct btrfs_csum_item *item;
065631f6 189 struct btrfs_csum_item *item_end;
ff79f819 190 struct extent_buffer *leaf = NULL;
6567e837 191 u64 csum_offset;
e6dcd2dc 192 struct btrfs_sector_sum *sector_sum;
f578d4bd
CM
193 u32 nritems;
194 u32 ins_size;
6e92f5e6
CM
195 char *eb_map;
196 char *eb_token;
197 unsigned long map_len;
198 unsigned long map_start;
199
5caf2a00
CM
200 path = btrfs_alloc_path();
201 BUG_ON(!path);
e6dcd2dc 202 sector_sum = &sums->sums;
065631f6
CM
203again:
204 next_offset = (u64)-1;
205 found_next = 0;
e6dcd2dc 206 offset = sector_sum->offset;
f254e52c
CM
207 file_key.objectid = objectid;
208 file_key.offset = offset;
f254e52c 209 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
a429e513
CM
210
211 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
ff79f819
CM
212 if (!IS_ERR(item)) {
213 leaf = path->nodes[0];
a429e513 214 goto found;
ff79f819 215 }
a429e513
CM
216 ret = PTR_ERR(item);
217 if (ret == -EFBIG) {
218 u32 item_size;
219 /* we found one, but it isn't big enough yet */
5f39d397
CM
220 leaf = path->nodes[0];
221 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
509659cd 222 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
a429e513
CM
223 /* already at max size, make a new one */
224 goto insert;
225 }
226 } else {
f578d4bd 227 int slot = path->slots[0] + 1;
a429e513 228 /* we didn't find a csum item, insert one */
f578d4bd
CM
229 nritems = btrfs_header_nritems(path->nodes[0]);
230 if (path->slots[0] >= nritems - 1) {
231 ret = btrfs_next_leaf(root, path);
b56baf5b 232 if (ret == 1)
f578d4bd 233 found_next = 1;
b56baf5b 234 if (ret != 0)
f578d4bd 235 goto insert;
b56baf5b 236 slot = 0;
f578d4bd
CM
237 }
238 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
239 if (found_key.objectid != objectid ||
240 found_key.type != BTRFS_CSUM_ITEM_KEY) {
241 found_next = 1;
242 goto insert;
243 }
244 next_offset = found_key.offset;
245 found_next = 1;
a429e513
CM
246 goto insert;
247 }
248
249 /*
250 * at this point, we know the tree has an item, but it isn't big
251 * enough yet to put our csum in. Grow it
252 */
253 btrfs_release_path(root, path);
6567e837 254 ret = btrfs_search_slot(trans, root, &file_key, path,
509659cd 255 BTRFS_CRC32_SIZE, 1);
6567e837
CM
256 if (ret < 0)
257 goto fail;
258 if (ret == 0) {
b18c6685 259 BUG();
6567e837
CM
260 }
261 if (path->slots[0] == 0) {
6567e837
CM
262 goto insert;
263 }
264 path->slots[0]--;
5f39d397
CM
265 leaf = path->nodes[0];
266 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6567e837
CM
267 csum_offset = (offset - found_key.offset) >>
268 root->fs_info->sb->s_blocksize_bits;
269 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
270 found_key.objectid != objectid ||
271 csum_offset >= MAX_CSUM_ITEMS(root)) {
6567e837
CM
272 goto insert;
273 }
5f39d397 274 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
509659cd
CM
275 BTRFS_CRC32_SIZE) {
276 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
5f39d397 277 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
3a686375
CM
278 if (diff != BTRFS_CRC32_SIZE)
279 goto insert;
a429e513 280 ret = btrfs_extend_item(trans, root, path, diff);
6567e837
CM
281 BUG_ON(ret);
282 goto csum;
283 }
284
285insert:
a429e513 286 btrfs_release_path(root, path);
6567e837 287 csum_offset = 0;
f578d4bd
CM
288 if (found_next) {
289 u64 tmp = min((u64)i_size_read(inode), next_offset);
b56baf5b 290 tmp -= offset & ~((u64)root->sectorsize -1);
f578d4bd
CM
291 tmp >>= root->fs_info->sb->s_blocksize_bits;
292 tmp = max((u64)1, tmp);
293 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
294 ins_size = BTRFS_CRC32_SIZE * tmp;
295 } else {
296 ins_size = BTRFS_CRC32_SIZE;
297 }
5caf2a00 298 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
f578d4bd 299 ins_size);
54aa1f4d
CM
300 if (ret < 0)
301 goto fail;
a429e513 302 if (ret != 0) {
a429e513 303 WARN_ON(1);
f254e52c 304 goto fail;
a429e513 305 }
6567e837 306csum:
5f39d397
CM
307 leaf = path->nodes[0];
308 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
f254e52c 309 ret = 0;
509659cd
CM
310 item = (struct btrfs_csum_item *)((unsigned char *)item +
311 csum_offset * BTRFS_CRC32_SIZE);
b18c6685 312found:
065631f6
CM
313 item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
314 item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
315 btrfs_item_size_nr(leaf, path->slots[0]));
6e92f5e6 316 eb_token = NULL;
e6dcd2dc 317next_sector:
aadfeb6e 318
6e92f5e6
CM
319 if (!eb_token ||
320 (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) {
321 int err;
322
323 if (eb_token)
eb20978f 324 unmap_extent_buffer(leaf, eb_token, KM_USER1);
6e92f5e6
CM
325 eb_token = NULL;
326 err = map_private_extent_buffer(leaf, (unsigned long)item,
327 BTRFS_CRC32_SIZE,
328 &eb_token, &eb_map,
eb20978f 329 &map_start, &map_len, KM_USER1);
6e92f5e6
CM
330 if (err)
331 eb_token = NULL;
332 }
333 if (eb_token) {
334 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
e6dcd2dc 335 &sector_sum->sum, BTRFS_CRC32_SIZE);
6e92f5e6 336 } else {
e6dcd2dc
CM
337 write_extent_buffer(leaf, &sector_sum->sum,
338 (unsigned long)item, BTRFS_CRC32_SIZE);
6e92f5e6 339 }
e6dcd2dc
CM
340 total_bytes += root->sectorsize;
341 sector_sum++;
342 if (total_bytes < sums->len) {
6e92f5e6
CM
343 item = (struct btrfs_csum_item *)((char *)item +
344 BTRFS_CRC32_SIZE);
2e1a992e 345 if (item < item_end && offset + PAGE_CACHE_SIZE ==
e6dcd2dc
CM
346 sector_sum->offset) {
347 offset = sector_sum->offset;
348 goto next_sector;
2e1a992e 349 }
065631f6 350 }
6e92f5e6 351 if (eb_token) {
eb20978f 352 unmap_extent_buffer(leaf, eb_token, KM_USER1);
6e92f5e6
CM
353 eb_token = NULL;
354 }
5caf2a00 355 btrfs_mark_buffer_dirty(path->nodes[0]);
e6dcd2dc 356 if (total_bytes < sums->len) {
065631f6
CM
357 btrfs_release_path(root, path);
358 goto again;
359 }
f254e52c 360fail:
5caf2a00 361 btrfs_free_path(path);
f254e52c
CM
362 return ret;
363}
364
1de037a4
CM
365int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
366 struct btrfs_root *root, struct btrfs_path *path,
367 u64 isize)
368{
369 struct btrfs_key key;
5f39d397 370 struct extent_buffer *leaf = path->nodes[0];
1de037a4
CM
371 int slot = path->slots[0];
372 int ret;
373 u32 new_item_size;
374 u64 new_item_span;
375 u64 blocks;
376
5f39d397 377 btrfs_item_key_to_cpu(leaf, &key, slot);
1de037a4
CM
378 if (isize <= key.offset)
379 return 0;
380 new_item_span = isize - key.offset;
5f39d397 381 blocks = (new_item_span + root->sectorsize - 1) >>
84f54cfa 382 root->fs_info->sb->s_blocksize_bits;
1de037a4 383 new_item_size = blocks * BTRFS_CRC32_SIZE;
5f39d397 384 if (new_item_size >= btrfs_item_size_nr(leaf, slot))
1de037a4 385 return 0;
179e29e4 386 ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
1de037a4
CM
387 BUG_ON(ret);
388 return ret;
389}
390