btrfs: remove redundant csum buffer in btrfs_io_bio
[sfrench/cifs-2.6.git] / fs / btrfs / file-item.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2007 Oracle.  All rights reserved.
4  */
5
6 #include <linux/bio.h>
7 #include <linux/slab.h>
8 #include <linux/pagemap.h>
9 #include <linux/highmem.h>
10 #include "ctree.h"
11 #include "disk-io.h"
12 #include "transaction.h"
13 #include "volumes.h"
14 #include "print-tree.h"
15 #include "compression.h"
16
17 #define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \
18                                    sizeof(struct btrfs_item) * 2) / \
19                                   size) - 1))
20
21 #define MAX_CSUM_ITEMS(r, size) (min_t(u32, __MAX_CSUM_ITEMS(r, size), \
22                                        PAGE_SIZE))
23
24 #define MAX_ORDERED_SUM_BYTES(fs_info) ((PAGE_SIZE - \
25                                    sizeof(struct btrfs_ordered_sum)) / \
26                                    sizeof(u32) * (fs_info)->sectorsize)
27
28 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
29                              struct btrfs_root *root,
30                              u64 objectid, u64 pos,
31                              u64 disk_offset, u64 disk_num_bytes,
32                              u64 num_bytes, u64 offset, u64 ram_bytes,
33                              u8 compression, u8 encryption, u16 other_encoding)
34 {
35         int ret = 0;
36         struct btrfs_file_extent_item *item;
37         struct btrfs_key file_key;
38         struct btrfs_path *path;
39         struct extent_buffer *leaf;
40
41         path = btrfs_alloc_path();
42         if (!path)
43                 return -ENOMEM;
44         file_key.objectid = objectid;
45         file_key.offset = pos;
46         file_key.type = BTRFS_EXTENT_DATA_KEY;
47
48         path->leave_spinning = 1;
49         ret = btrfs_insert_empty_item(trans, root, path, &file_key,
50                                       sizeof(*item));
51         if (ret < 0)
52                 goto out;
53         BUG_ON(ret); /* Can't happen */
54         leaf = path->nodes[0];
55         item = btrfs_item_ptr(leaf, path->slots[0],
56                               struct btrfs_file_extent_item);
57         btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
58         btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
59         btrfs_set_file_extent_offset(leaf, item, offset);
60         btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
61         btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
62         btrfs_set_file_extent_generation(leaf, item, trans->transid);
63         btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
64         btrfs_set_file_extent_compression(leaf, item, compression);
65         btrfs_set_file_extent_encryption(leaf, item, encryption);
66         btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
67
68         btrfs_mark_buffer_dirty(leaf);
69 out:
70         btrfs_free_path(path);
71         return ret;
72 }
73
74 static struct btrfs_csum_item *
75 btrfs_lookup_csum(struct btrfs_trans_handle *trans,
76                   struct btrfs_root *root,
77                   struct btrfs_path *path,
78                   u64 bytenr, int cow)
79 {
80         struct btrfs_fs_info *fs_info = root->fs_info;
81         int ret;
82         struct btrfs_key file_key;
83         struct btrfs_key found_key;
84         struct btrfs_csum_item *item;
85         struct extent_buffer *leaf;
86         u64 csum_offset = 0;
87         u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
88         int csums_in_item;
89
90         file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
91         file_key.offset = bytenr;
92         file_key.type = BTRFS_EXTENT_CSUM_KEY;
93         ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
94         if (ret < 0)
95                 goto fail;
96         leaf = path->nodes[0];
97         if (ret > 0) {
98                 ret = 1;
99                 if (path->slots[0] == 0)
100                         goto fail;
101                 path->slots[0]--;
102                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
103                 if (found_key.type != BTRFS_EXTENT_CSUM_KEY)
104                         goto fail;
105
106                 csum_offset = (bytenr - found_key.offset) >>
107                                 fs_info->sb->s_blocksize_bits;
108                 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
109                 csums_in_item /= csum_size;
110
111                 if (csum_offset == csums_in_item) {
112                         ret = -EFBIG;
113                         goto fail;
114                 } else if (csum_offset > csums_in_item) {
115                         goto fail;
116                 }
117         }
118         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
119         item = (struct btrfs_csum_item *)((unsigned char *)item +
120                                           csum_offset * csum_size);
121         return item;
122 fail:
123         if (ret > 0)
124                 ret = -ENOENT;
125         return ERR_PTR(ret);
126 }
127
128 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
129                              struct btrfs_root *root,
130                              struct btrfs_path *path, u64 objectid,
131                              u64 offset, int mod)
132 {
133         int ret;
134         struct btrfs_key file_key;
135         int ins_len = mod < 0 ? -1 : 0;
136         int cow = mod != 0;
137
138         file_key.objectid = objectid;
139         file_key.offset = offset;
140         file_key.type = BTRFS_EXTENT_DATA_KEY;
141         ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
142         return ret;
143 }
144
145 static void btrfs_io_bio_endio_readpage(struct btrfs_io_bio *bio, int err)
146 {
147         if (bio->csum != bio->csum_inline) {
148                 kfree(bio->csum);
149                 bio->csum = NULL;
150         }
151 }
152
153 static blk_status_t __btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio,
154                                    u64 logical_offset, u32 *dst, int dio)
155 {
156         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
157         struct bio_vec bvec;
158         struct bvec_iter iter;
159         struct btrfs_io_bio *btrfs_bio = btrfs_io_bio(bio);
160         struct btrfs_csum_item *item = NULL;
161         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
162         struct btrfs_path *path;
163         u8 *csum;
164         u64 offset = 0;
165         u64 item_start_offset = 0;
166         u64 item_last_offset = 0;
167         u64 disk_bytenr;
168         u64 page_bytes_left;
169         u32 diff;
170         int nblocks;
171         int count = 0;
172         u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
173
174         path = btrfs_alloc_path();
175         if (!path)
176                 return BLK_STS_RESOURCE;
177
178         nblocks = bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
179         if (!dst) {
180                 if (nblocks * csum_size > BTRFS_BIO_INLINE_CSUM_SIZE) {
181                         btrfs_bio->csum = kmalloc_array(nblocks, csum_size,
182                                                         GFP_NOFS);
183                         if (!btrfs_bio->csum) {
184                                 btrfs_free_path(path);
185                                 return BLK_STS_RESOURCE;
186                         }
187                         btrfs_bio->end_io = btrfs_io_bio_endio_readpage;
188                 } else {
189                         btrfs_bio->csum = btrfs_bio->csum_inline;
190                 }
191                 csum = btrfs_bio->csum;
192         } else {
193                 csum = (u8 *)dst;
194         }
195
196         if (bio->bi_iter.bi_size > PAGE_SIZE * 8)
197                 path->reada = READA_FORWARD;
198
199         /*
200          * the free space stuff is only read when it hasn't been
201          * updated in the current transaction.  So, we can safely
202          * read from the commit root and sidestep a nasty deadlock
203          * between reading the free space cache and updating the csum tree.
204          */
205         if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
206                 path->search_commit_root = 1;
207                 path->skip_locking = 1;
208         }
209
210         disk_bytenr = (u64)bio->bi_iter.bi_sector << 9;
211         if (dio)
212                 offset = logical_offset;
213
214         bio_for_each_segment(bvec, bio, iter) {
215                 page_bytes_left = bvec.bv_len;
216                 if (count)
217                         goto next;
218
219                 if (!dio)
220                         offset = page_offset(bvec.bv_page) + bvec.bv_offset;
221                 count = btrfs_find_ordered_sum(inode, offset, disk_bytenr,
222                                                (u32 *)csum, nblocks);
223                 if (count)
224                         goto found;
225
226                 if (!item || disk_bytenr < item_start_offset ||
227                     disk_bytenr >= item_last_offset) {
228                         struct btrfs_key found_key;
229                         u32 item_size;
230
231                         if (item)
232                                 btrfs_release_path(path);
233                         item = btrfs_lookup_csum(NULL, fs_info->csum_root,
234                                                  path, disk_bytenr, 0);
235                         if (IS_ERR(item)) {
236                                 count = 1;
237                                 memset(csum, 0, csum_size);
238                                 if (BTRFS_I(inode)->root->root_key.objectid ==
239                                     BTRFS_DATA_RELOC_TREE_OBJECTID) {
240                                         set_extent_bits(io_tree, offset,
241                                                 offset + fs_info->sectorsize - 1,
242                                                 EXTENT_NODATASUM);
243                                 } else {
244                                         btrfs_info_rl(fs_info,
245                                                    "no csum found for inode %llu start %llu",
246                                                btrfs_ino(BTRFS_I(inode)), offset);
247                                 }
248                                 item = NULL;
249                                 btrfs_release_path(path);
250                                 goto found;
251                         }
252                         btrfs_item_key_to_cpu(path->nodes[0], &found_key,
253                                               path->slots[0]);
254
255                         item_start_offset = found_key.offset;
256                         item_size = btrfs_item_size_nr(path->nodes[0],
257                                                        path->slots[0]);
258                         item_last_offset = item_start_offset +
259                                 (item_size / csum_size) *
260                                 fs_info->sectorsize;
261                         item = btrfs_item_ptr(path->nodes[0], path->slots[0],
262                                               struct btrfs_csum_item);
263                 }
264                 /*
265                  * this byte range must be able to fit inside
266                  * a single leaf so it will also fit inside a u32
267                  */
268                 diff = disk_bytenr - item_start_offset;
269                 diff = diff / fs_info->sectorsize;
270                 diff = diff * csum_size;
271                 count = min_t(int, nblocks, (item_last_offset - disk_bytenr) >>
272                                             inode->i_sb->s_blocksize_bits);
273                 read_extent_buffer(path->nodes[0], csum,
274                                    ((unsigned long)item) + diff,
275                                    csum_size * count);
276 found:
277                 csum += count * csum_size;
278                 nblocks -= count;
279 next:
280                 while (count--) {
281                         disk_bytenr += fs_info->sectorsize;
282                         offset += fs_info->sectorsize;
283                         page_bytes_left -= fs_info->sectorsize;
284                         if (!page_bytes_left)
285                                 break; /* move to next bio */
286                 }
287         }
288
289         WARN_ON_ONCE(count);
290         btrfs_free_path(path);
291         return 0;
292 }
293
294 blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio, u32 *dst)
295 {
296         return __btrfs_lookup_bio_sums(inode, bio, 0, dst, 0);
297 }
298
299 blk_status_t btrfs_lookup_bio_sums_dio(struct inode *inode, struct bio *bio, u64 offset)
300 {
301         return __btrfs_lookup_bio_sums(inode, bio, offset, NULL, 1);
302 }
303
304 int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
305                              struct list_head *list, int search_commit)
306 {
307         struct btrfs_fs_info *fs_info = root->fs_info;
308         struct btrfs_key key;
309         struct btrfs_path *path;
310         struct extent_buffer *leaf;
311         struct btrfs_ordered_sum *sums;
312         struct btrfs_csum_item *item;
313         LIST_HEAD(tmplist);
314         unsigned long offset;
315         int ret;
316         size_t size;
317         u64 csum_end;
318         u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
319
320         ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
321                IS_ALIGNED(end + 1, fs_info->sectorsize));
322
323         path = btrfs_alloc_path();
324         if (!path)
325                 return -ENOMEM;
326
327         if (search_commit) {
328                 path->skip_locking = 1;
329                 path->reada = READA_FORWARD;
330                 path->search_commit_root = 1;
331         }
332
333         key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
334         key.offset = start;
335         key.type = BTRFS_EXTENT_CSUM_KEY;
336
337         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
338         if (ret < 0)
339                 goto fail;
340         if (ret > 0 && path->slots[0] > 0) {
341                 leaf = path->nodes[0];
342                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
343                 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
344                     key.type == BTRFS_EXTENT_CSUM_KEY) {
345                         offset = (start - key.offset) >>
346                                  fs_info->sb->s_blocksize_bits;
347                         if (offset * csum_size <
348                             btrfs_item_size_nr(leaf, path->slots[0] - 1))
349                                 path->slots[0]--;
350                 }
351         }
352
353         while (start <= end) {
354                 leaf = path->nodes[0];
355                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
356                         ret = btrfs_next_leaf(root, path);
357                         if (ret < 0)
358                                 goto fail;
359                         if (ret > 0)
360                                 break;
361                         leaf = path->nodes[0];
362                 }
363
364                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
365                 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
366                     key.type != BTRFS_EXTENT_CSUM_KEY ||
367                     key.offset > end)
368                         break;
369
370                 if (key.offset > start)
371                         start = key.offset;
372
373                 size = btrfs_item_size_nr(leaf, path->slots[0]);
374                 csum_end = key.offset + (size / csum_size) * fs_info->sectorsize;
375                 if (csum_end <= start) {
376                         path->slots[0]++;
377                         continue;
378                 }
379
380                 csum_end = min(csum_end, end + 1);
381                 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
382                                       struct btrfs_csum_item);
383                 while (start < csum_end) {
384                         size = min_t(size_t, csum_end - start,
385                                      MAX_ORDERED_SUM_BYTES(fs_info));
386                         sums = kzalloc(btrfs_ordered_sum_size(fs_info, size),
387                                        GFP_NOFS);
388                         if (!sums) {
389                                 ret = -ENOMEM;
390                                 goto fail;
391                         }
392
393                         sums->bytenr = start;
394                         sums->len = (int)size;
395
396                         offset = (start - key.offset) >>
397                                 fs_info->sb->s_blocksize_bits;
398                         offset *= csum_size;
399                         size >>= fs_info->sb->s_blocksize_bits;
400
401                         read_extent_buffer(path->nodes[0],
402                                            sums->sums,
403                                            ((unsigned long)item) + offset,
404                                            csum_size * size);
405
406                         start += fs_info->sectorsize * size;
407                         list_add_tail(&sums->list, &tmplist);
408                 }
409                 path->slots[0]++;
410         }
411         ret = 0;
412 fail:
413         while (ret < 0 && !list_empty(&tmplist)) {
414                 sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list);
415                 list_del(&sums->list);
416                 kfree(sums);
417         }
418         list_splice_tail(&tmplist, list);
419
420         btrfs_free_path(path);
421         return ret;
422 }
423
424 blk_status_t btrfs_csum_one_bio(struct inode *inode, struct bio *bio,
425                        u64 file_start, int contig)
426 {
427         struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
428         struct btrfs_ordered_sum *sums;
429         struct btrfs_ordered_extent *ordered = NULL;
430         char *data;
431         struct bvec_iter iter;
432         struct bio_vec bvec;
433         int index;
434         int nr_sectors;
435         unsigned long total_bytes = 0;
436         unsigned long this_sum_bytes = 0;
437         int i;
438         u64 offset;
439
440         sums = kzalloc(btrfs_ordered_sum_size(fs_info, bio->bi_iter.bi_size),
441                        GFP_NOFS);
442         if (!sums)
443                 return BLK_STS_RESOURCE;
444
445         sums->len = bio->bi_iter.bi_size;
446         INIT_LIST_HEAD(&sums->list);
447
448         if (contig)
449                 offset = file_start;
450         else
451                 offset = 0; /* shut up gcc */
452
453         sums->bytenr = (u64)bio->bi_iter.bi_sector << 9;
454         index = 0;
455
456         bio_for_each_segment(bvec, bio, iter) {
457                 if (!contig)
458                         offset = page_offset(bvec.bv_page) + bvec.bv_offset;
459
460                 if (!ordered) {
461                         ordered = btrfs_lookup_ordered_extent(inode, offset);
462                         BUG_ON(!ordered); /* Logic error */
463                 }
464
465                 data = kmap_atomic(bvec.bv_page);
466
467                 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info,
468                                                  bvec.bv_len + fs_info->sectorsize
469                                                  - 1);
470
471                 for (i = 0; i < nr_sectors; i++) {
472                         if (offset >= ordered->file_offset + ordered->len ||
473                                 offset < ordered->file_offset) {
474                                 unsigned long bytes_left;
475
476                                 kunmap_atomic(data);
477                                 sums->len = this_sum_bytes;
478                                 this_sum_bytes = 0;
479                                 btrfs_add_ordered_sum(inode, ordered, sums);
480                                 btrfs_put_ordered_extent(ordered);
481
482                                 bytes_left = bio->bi_iter.bi_size - total_bytes;
483
484                                 sums = kzalloc(btrfs_ordered_sum_size(fs_info, bytes_left),
485                                                GFP_NOFS);
486                                 BUG_ON(!sums); /* -ENOMEM */
487                                 sums->len = bytes_left;
488                                 ordered = btrfs_lookup_ordered_extent(inode,
489                                                                 offset);
490                                 ASSERT(ordered); /* Logic error */
491                                 sums->bytenr = ((u64)bio->bi_iter.bi_sector << 9)
492                                         + total_bytes;
493                                 index = 0;
494
495                                 data = kmap_atomic(bvec.bv_page);
496                         }
497
498                         sums->sums[index] = ~(u32)0;
499                         sums->sums[index]
500                                 = btrfs_csum_data(data + bvec.bv_offset
501                                                 + (i * fs_info->sectorsize),
502                                                 sums->sums[index],
503                                                 fs_info->sectorsize);
504                         btrfs_csum_final(sums->sums[index],
505                                         (char *)(sums->sums + index));
506                         index++;
507                         offset += fs_info->sectorsize;
508                         this_sum_bytes += fs_info->sectorsize;
509                         total_bytes += fs_info->sectorsize;
510                 }
511
512                 kunmap_atomic(data);
513         }
514         this_sum_bytes = 0;
515         btrfs_add_ordered_sum(inode, ordered, sums);
516         btrfs_put_ordered_extent(ordered);
517         return 0;
518 }
519
520 /*
521  * helper function for csum removal, this expects the
522  * key to describe the csum pointed to by the path, and it expects
523  * the csum to overlap the range [bytenr, len]
524  *
525  * The csum should not be entirely contained in the range and the
526  * range should not be entirely contained in the csum.
527  *
528  * This calls btrfs_truncate_item with the correct args based on the
529  * overlap, and fixes up the key as required.
530  */
531 static noinline void truncate_one_csum(struct btrfs_fs_info *fs_info,
532                                        struct btrfs_path *path,
533                                        struct btrfs_key *key,
534                                        u64 bytenr, u64 len)
535 {
536         struct extent_buffer *leaf;
537         u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
538         u64 csum_end;
539         u64 end_byte = bytenr + len;
540         u32 blocksize_bits = fs_info->sb->s_blocksize_bits;
541
542         leaf = path->nodes[0];
543         csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
544         csum_end <<= fs_info->sb->s_blocksize_bits;
545         csum_end += key->offset;
546
547         if (key->offset < bytenr && csum_end <= end_byte) {
548                 /*
549                  *         [ bytenr - len ]
550                  *         [   ]
551                  *   [csum     ]
552                  *   A simple truncate off the end of the item
553                  */
554                 u32 new_size = (bytenr - key->offset) >> blocksize_bits;
555                 new_size *= csum_size;
556                 btrfs_truncate_item(fs_info, path, new_size, 1);
557         } else if (key->offset >= bytenr && csum_end > end_byte &&
558                    end_byte > key->offset) {
559                 /*
560                  *         [ bytenr - len ]
561                  *                 [ ]
562                  *                 [csum     ]
563                  * we need to truncate from the beginning of the csum
564                  */
565                 u32 new_size = (csum_end - end_byte) >> blocksize_bits;
566                 new_size *= csum_size;
567
568                 btrfs_truncate_item(fs_info, path, new_size, 0);
569
570                 key->offset = end_byte;
571                 btrfs_set_item_key_safe(fs_info, path, key);
572         } else {
573                 BUG();
574         }
575 }
576
577 /*
578  * deletes the csum items from the csum tree for a given
579  * range of bytes.
580  */
581 int btrfs_del_csums(struct btrfs_trans_handle *trans,
582                     struct btrfs_fs_info *fs_info, u64 bytenr, u64 len)
583 {
584         struct btrfs_root *root = fs_info->csum_root;
585         struct btrfs_path *path;
586         struct btrfs_key key;
587         u64 end_byte = bytenr + len;
588         u64 csum_end;
589         struct extent_buffer *leaf;
590         int ret;
591         u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
592         int blocksize_bits = fs_info->sb->s_blocksize_bits;
593
594         path = btrfs_alloc_path();
595         if (!path)
596                 return -ENOMEM;
597
598         while (1) {
599                 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
600                 key.offset = end_byte - 1;
601                 key.type = BTRFS_EXTENT_CSUM_KEY;
602
603                 path->leave_spinning = 1;
604                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
605                 if (ret > 0) {
606                         if (path->slots[0] == 0)
607                                 break;
608                         path->slots[0]--;
609                 } else if (ret < 0) {
610                         break;
611                 }
612
613                 leaf = path->nodes[0];
614                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
615
616                 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
617                     key.type != BTRFS_EXTENT_CSUM_KEY) {
618                         break;
619                 }
620
621                 if (key.offset >= end_byte)
622                         break;
623
624                 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
625                 csum_end <<= blocksize_bits;
626                 csum_end += key.offset;
627
628                 /* this csum ends before we start, we're done */
629                 if (csum_end <= bytenr)
630                         break;
631
632                 /* delete the entire item, it is inside our range */
633                 if (key.offset >= bytenr && csum_end <= end_byte) {
634                         int del_nr = 1;
635
636                         /*
637                          * Check how many csum items preceding this one in this
638                          * leaf correspond to our range and then delete them all
639                          * at once.
640                          */
641                         if (key.offset > bytenr && path->slots[0] > 0) {
642                                 int slot = path->slots[0] - 1;
643
644                                 while (slot >= 0) {
645                                         struct btrfs_key pk;
646
647                                         btrfs_item_key_to_cpu(leaf, &pk, slot);
648                                         if (pk.offset < bytenr ||
649                                             pk.type != BTRFS_EXTENT_CSUM_KEY ||
650                                             pk.objectid !=
651                                             BTRFS_EXTENT_CSUM_OBJECTID)
652                                                 break;
653                                         path->slots[0] = slot;
654                                         del_nr++;
655                                         key.offset = pk.offset;
656                                         slot--;
657                                 }
658                         }
659                         ret = btrfs_del_items(trans, root, path,
660                                               path->slots[0], del_nr);
661                         if (ret)
662                                 goto out;
663                         if (key.offset == bytenr)
664                                 break;
665                 } else if (key.offset < bytenr && csum_end > end_byte) {
666                         unsigned long offset;
667                         unsigned long shift_len;
668                         unsigned long item_offset;
669                         /*
670                          *        [ bytenr - len ]
671                          *     [csum                ]
672                          *
673                          * Our bytes are in the middle of the csum,
674                          * we need to split this item and insert a new one.
675                          *
676                          * But we can't drop the path because the
677                          * csum could change, get removed, extended etc.
678                          *
679                          * The trick here is the max size of a csum item leaves
680                          * enough room in the tree block for a single
681                          * item header.  So, we split the item in place,
682                          * adding a new header pointing to the existing
683                          * bytes.  Then we loop around again and we have
684                          * a nicely formed csum item that we can neatly
685                          * truncate.
686                          */
687                         offset = (bytenr - key.offset) >> blocksize_bits;
688                         offset *= csum_size;
689
690                         shift_len = (len >> blocksize_bits) * csum_size;
691
692                         item_offset = btrfs_item_ptr_offset(leaf,
693                                                             path->slots[0]);
694
695                         memzero_extent_buffer(leaf, item_offset + offset,
696                                              shift_len);
697                         key.offset = bytenr;
698
699                         /*
700                          * btrfs_split_item returns -EAGAIN when the
701                          * item changed size or key
702                          */
703                         ret = btrfs_split_item(trans, root, path, &key, offset);
704                         if (ret && ret != -EAGAIN) {
705                                 btrfs_abort_transaction(trans, ret);
706                                 goto out;
707                         }
708
709                         key.offset = end_byte - 1;
710                 } else {
711                         truncate_one_csum(fs_info, path, &key, bytenr, len);
712                         if (key.offset < bytenr)
713                                 break;
714                 }
715                 btrfs_release_path(path);
716         }
717         ret = 0;
718 out:
719         btrfs_free_path(path);
720         return ret;
721 }
722
723 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
724                            struct btrfs_root *root,
725                            struct btrfs_ordered_sum *sums)
726 {
727         struct btrfs_fs_info *fs_info = root->fs_info;
728         struct btrfs_key file_key;
729         struct btrfs_key found_key;
730         struct btrfs_path *path;
731         struct btrfs_csum_item *item;
732         struct btrfs_csum_item *item_end;
733         struct extent_buffer *leaf = NULL;
734         u64 next_offset;
735         u64 total_bytes = 0;
736         u64 csum_offset;
737         u64 bytenr;
738         u32 nritems;
739         u32 ins_size;
740         int index = 0;
741         int found_next;
742         int ret;
743         u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
744
745         path = btrfs_alloc_path();
746         if (!path)
747                 return -ENOMEM;
748 again:
749         next_offset = (u64)-1;
750         found_next = 0;
751         bytenr = sums->bytenr + total_bytes;
752         file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
753         file_key.offset = bytenr;
754         file_key.type = BTRFS_EXTENT_CSUM_KEY;
755
756         item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
757         if (!IS_ERR(item)) {
758                 ret = 0;
759                 leaf = path->nodes[0];
760                 item_end = btrfs_item_ptr(leaf, path->slots[0],
761                                           struct btrfs_csum_item);
762                 item_end = (struct btrfs_csum_item *)((char *)item_end +
763                            btrfs_item_size_nr(leaf, path->slots[0]));
764                 goto found;
765         }
766         ret = PTR_ERR(item);
767         if (ret != -EFBIG && ret != -ENOENT)
768                 goto fail_unlock;
769
770         if (ret == -EFBIG) {
771                 u32 item_size;
772                 /* we found one, but it isn't big enough yet */
773                 leaf = path->nodes[0];
774                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
775                 if ((item_size / csum_size) >=
776                     MAX_CSUM_ITEMS(fs_info, csum_size)) {
777                         /* already at max size, make a new one */
778                         goto insert;
779                 }
780         } else {
781                 int slot = path->slots[0] + 1;
782                 /* we didn't find a csum item, insert one */
783                 nritems = btrfs_header_nritems(path->nodes[0]);
784                 if (!nritems || (path->slots[0] >= nritems - 1)) {
785                         ret = btrfs_next_leaf(root, path);
786                         if (ret == 1)
787                                 found_next = 1;
788                         if (ret != 0)
789                                 goto insert;
790                         slot = path->slots[0];
791                 }
792                 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
793                 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
794                     found_key.type != BTRFS_EXTENT_CSUM_KEY) {
795                         found_next = 1;
796                         goto insert;
797                 }
798                 next_offset = found_key.offset;
799                 found_next = 1;
800                 goto insert;
801         }
802
803         /*
804          * at this point, we know the tree has an item, but it isn't big
805          * enough yet to put our csum in.  Grow it
806          */
807         btrfs_release_path(path);
808         ret = btrfs_search_slot(trans, root, &file_key, path,
809                                 csum_size, 1);
810         if (ret < 0)
811                 goto fail_unlock;
812
813         if (ret > 0) {
814                 if (path->slots[0] == 0)
815                         goto insert;
816                 path->slots[0]--;
817         }
818
819         leaf = path->nodes[0];
820         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
821         csum_offset = (bytenr - found_key.offset) >>
822                         fs_info->sb->s_blocksize_bits;
823
824         if (found_key.type != BTRFS_EXTENT_CSUM_KEY ||
825             found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
826             csum_offset >= MAX_CSUM_ITEMS(fs_info, csum_size)) {
827                 goto insert;
828         }
829
830         if (csum_offset == btrfs_item_size_nr(leaf, path->slots[0]) /
831             csum_size) {
832                 int extend_nr;
833                 u64 tmp;
834                 u32 diff;
835                 u32 free_space;
836
837                 if (btrfs_leaf_free_space(fs_info, leaf) <
838                                  sizeof(struct btrfs_item) + csum_size * 2)
839                         goto insert;
840
841                 free_space = btrfs_leaf_free_space(fs_info, leaf) -
842                                          sizeof(struct btrfs_item) - csum_size;
843                 tmp = sums->len - total_bytes;
844                 tmp >>= fs_info->sb->s_blocksize_bits;
845                 WARN_ON(tmp < 1);
846
847                 extend_nr = max_t(int, 1, (int)tmp);
848                 diff = (csum_offset + extend_nr) * csum_size;
849                 diff = min(diff,
850                            MAX_CSUM_ITEMS(fs_info, csum_size) * csum_size);
851
852                 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
853                 diff = min(free_space, diff);
854                 diff /= csum_size;
855                 diff *= csum_size;
856
857                 btrfs_extend_item(fs_info, path, diff);
858                 ret = 0;
859                 goto csum;
860         }
861
862 insert:
863         btrfs_release_path(path);
864         csum_offset = 0;
865         if (found_next) {
866                 u64 tmp;
867
868                 tmp = sums->len - total_bytes;
869                 tmp >>= fs_info->sb->s_blocksize_bits;
870                 tmp = min(tmp, (next_offset - file_key.offset) >>
871                                          fs_info->sb->s_blocksize_bits);
872
873                 tmp = max_t(u64, 1, tmp);
874                 tmp = min_t(u64, tmp, MAX_CSUM_ITEMS(fs_info, csum_size));
875                 ins_size = csum_size * tmp;
876         } else {
877                 ins_size = csum_size;
878         }
879         path->leave_spinning = 1;
880         ret = btrfs_insert_empty_item(trans, root, path, &file_key,
881                                       ins_size);
882         path->leave_spinning = 0;
883         if (ret < 0)
884                 goto fail_unlock;
885         if (WARN_ON(ret != 0))
886                 goto fail_unlock;
887         leaf = path->nodes[0];
888 csum:
889         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
890         item_end = (struct btrfs_csum_item *)((unsigned char *)item +
891                                       btrfs_item_size_nr(leaf, path->slots[0]));
892         item = (struct btrfs_csum_item *)((unsigned char *)item +
893                                           csum_offset * csum_size);
894 found:
895         ins_size = (u32)(sums->len - total_bytes) >>
896                    fs_info->sb->s_blocksize_bits;
897         ins_size *= csum_size;
898         ins_size = min_t(u32, (unsigned long)item_end - (unsigned long)item,
899                               ins_size);
900         write_extent_buffer(leaf, sums->sums + index, (unsigned long)item,
901                             ins_size);
902
903         ins_size /= csum_size;
904         total_bytes += ins_size * fs_info->sectorsize;
905         index += ins_size;
906
907         btrfs_mark_buffer_dirty(path->nodes[0]);
908         if (total_bytes < sums->len) {
909                 btrfs_release_path(path);
910                 cond_resched();
911                 goto again;
912         }
913 out:
914         btrfs_free_path(path);
915         return ret;
916
917 fail_unlock:
918         goto out;
919 }
920
921 void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
922                                      const struct btrfs_path *path,
923                                      struct btrfs_file_extent_item *fi,
924                                      const bool new_inline,
925                                      struct extent_map *em)
926 {
927         struct btrfs_fs_info *fs_info = inode->root->fs_info;
928         struct btrfs_root *root = inode->root;
929         struct extent_buffer *leaf = path->nodes[0];
930         const int slot = path->slots[0];
931         struct btrfs_key key;
932         u64 extent_start, extent_end;
933         u64 bytenr;
934         u8 type = btrfs_file_extent_type(leaf, fi);
935         int compress_type = btrfs_file_extent_compression(leaf, fi);
936
937         em->bdev = fs_info->fs_devices->latest_bdev;
938         btrfs_item_key_to_cpu(leaf, &key, slot);
939         extent_start = key.offset;
940
941         if (type == BTRFS_FILE_EXTENT_REG ||
942             type == BTRFS_FILE_EXTENT_PREALLOC) {
943                 extent_end = extent_start +
944                         btrfs_file_extent_num_bytes(leaf, fi);
945         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
946                 size_t size;
947                 size = btrfs_file_extent_ram_bytes(leaf, fi);
948                 extent_end = ALIGN(extent_start + size,
949                                    fs_info->sectorsize);
950         }
951
952         em->ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
953         if (type == BTRFS_FILE_EXTENT_REG ||
954             type == BTRFS_FILE_EXTENT_PREALLOC) {
955                 em->start = extent_start;
956                 em->len = extent_end - extent_start;
957                 em->orig_start = extent_start -
958                         btrfs_file_extent_offset(leaf, fi);
959                 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
960                 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
961                 if (bytenr == 0) {
962                         em->block_start = EXTENT_MAP_HOLE;
963                         return;
964                 }
965                 if (compress_type != BTRFS_COMPRESS_NONE) {
966                         set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
967                         em->compress_type = compress_type;
968                         em->block_start = bytenr;
969                         em->block_len = em->orig_block_len;
970                 } else {
971                         bytenr += btrfs_file_extent_offset(leaf, fi);
972                         em->block_start = bytenr;
973                         em->block_len = em->len;
974                         if (type == BTRFS_FILE_EXTENT_PREALLOC)
975                                 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
976                 }
977         } else if (type == BTRFS_FILE_EXTENT_INLINE) {
978                 em->block_start = EXTENT_MAP_INLINE;
979                 em->start = extent_start;
980                 em->len = extent_end - extent_start;
981                 /*
982                  * Initialize orig_start and block_len with the same values
983                  * as in inode.c:btrfs_get_extent().
984                  */
985                 em->orig_start = EXTENT_MAP_HOLE;
986                 em->block_len = (u64)-1;
987                 if (!new_inline && compress_type != BTRFS_COMPRESS_NONE) {
988                         set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
989                         em->compress_type = compress_type;
990                 }
991         } else {
992                 btrfs_err(fs_info,
993                           "unknown file extent item type %d, inode %llu, offset %llu, "
994                           "root %llu", type, btrfs_ino(inode), extent_start,
995                           root->root_key.objectid);
996         }
997 }