btrfs-progs: convert: Fix a bug that makes convert asserts at scan time
[platform/upstream/btrfs-progs.git] / 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 <stdio.h>
20 #include <stdlib.h>
21 #include "kerncompat.h"
22 #include "radix-tree.h"
23 #include "ctree.h"
24 #include "disk-io.h"
25 #include "transaction.h"
26 #include "print-tree.h"
27 #include "crc32c.h"
28 #include "internal.h"
29
30 #define MAX_CSUM_ITEMS(r,size) ((((BTRFS_LEAF_DATA_SIZE(r) - \
31                                sizeof(struct btrfs_item) * 2) / \
32                                size) - 1))
33 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
34                              struct btrfs_root *root,
35                              u64 objectid, u64 pos, u64 offset,
36                              u64 disk_num_bytes, u64 num_bytes)
37 {
38         int ret = 0;
39         struct btrfs_file_extent_item *item;
40         struct btrfs_key file_key;
41         struct btrfs_path *path;
42         struct extent_buffer *leaf;
43
44         path = btrfs_alloc_path();
45         BUG_ON(!path);
46         file_key.objectid = objectid;
47         file_key.offset = pos;
48         btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
49
50         ret = btrfs_insert_empty_item(trans, root, path, &file_key,
51                                       sizeof(*item));
52         if (ret < 0)
53                 goto out;
54         BUG_ON(ret);
55         leaf = path->nodes[0];
56         item = btrfs_item_ptr(leaf, path->slots[0],
57                               struct btrfs_file_extent_item);
58         btrfs_set_file_extent_disk_bytenr(leaf, item, offset);
59         btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
60         btrfs_set_file_extent_offset(leaf, item, 0);
61         btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
62         btrfs_set_file_extent_ram_bytes(leaf, item, num_bytes);
63         btrfs_set_file_extent_generation(leaf, item, trans->transid);
64         btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
65         btrfs_set_file_extent_compression(leaf, item, 0);
66         btrfs_set_file_extent_encryption(leaf, item, 0);
67         btrfs_set_file_extent_other_encoding(leaf, item, 0);
68         btrfs_mark_buffer_dirty(leaf);
69 out:
70         btrfs_free_path(path);
71         return ret;
72 }
73
74 int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
75                                struct btrfs_root *root, u64 objectid,
76                                u64 offset, char *buffer, size_t size)
77 {
78         struct btrfs_key key;
79         struct btrfs_path *path;
80         struct extent_buffer *leaf;
81         unsigned long ptr;
82         struct btrfs_file_extent_item *ei;
83         u32 datasize;
84         int err = 0;
85         int ret;
86
87         path = btrfs_alloc_path();
88         if (!path)
89                 return -ENOMEM;
90
91         key.objectid = objectid;
92         key.offset = offset;
93         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
94
95         datasize = btrfs_file_extent_calc_inline_size(size);
96         ret = btrfs_insert_empty_item(trans, root, path, &key, datasize);
97         if (ret) {
98                 err = ret;
99                 goto fail;
100         }
101
102         leaf = path->nodes[0];
103         ei = btrfs_item_ptr(leaf, path->slots[0],
104                             struct btrfs_file_extent_item);
105         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
106         btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
107         btrfs_set_file_extent_ram_bytes(leaf, ei, size);
108         btrfs_set_file_extent_compression(leaf, ei, 0);
109         btrfs_set_file_extent_encryption(leaf, ei, 0);
110         btrfs_set_file_extent_other_encoding(leaf, ei, 0);
111
112         ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
113         write_extent_buffer(leaf, buffer, ptr, size);
114         btrfs_mark_buffer_dirty(leaf);
115 fail:
116         btrfs_free_path(path);
117         return err;
118 }
119
120 static struct btrfs_csum_item *
121 btrfs_lookup_csum(struct btrfs_trans_handle *trans,
122                   struct btrfs_root *root,
123                   struct btrfs_path *path,
124                   u64 bytenr, int cow)
125 {
126         int ret;
127         struct btrfs_key file_key;
128         struct btrfs_key found_key;
129         struct btrfs_csum_item *item;
130         struct extent_buffer *leaf;
131         u64 csum_offset = 0;
132         u16 csum_size =
133                 btrfs_super_csum_size(root->fs_info->super_copy);
134         int csums_in_item;
135
136         file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
137         file_key.offset = bytenr;
138         btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
139         ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
140         if (ret < 0)
141                 goto fail;
142         leaf = path->nodes[0];
143         if (ret > 0) {
144                 ret = 1;
145                 if (path->slots[0] == 0)
146                         goto fail;
147                 path->slots[0]--;
148                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
149                 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY)
150                         goto fail;
151
152                 csum_offset = (bytenr - found_key.offset) / root->sectorsize;
153                 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
154                 csums_in_item /= csum_size;
155
156                 if (csum_offset >= csums_in_item) {
157                         ret = -EFBIG;
158                         goto fail;
159                 }
160         }
161         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
162         item = (struct btrfs_csum_item *)((unsigned char *)item +
163                                           csum_offset * csum_size);
164         return item;
165 fail:
166         if (ret > 0)
167                 ret = -ENOENT;
168         return ERR_PTR(ret);
169 }
170
171 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
172                           struct btrfs_root *root, u64 alloc_end,
173                           u64 bytenr, char *data, size_t len)
174 {
175         int ret = 0;
176         struct btrfs_key file_key;
177         struct btrfs_key found_key;
178         u64 next_offset = (u64)-1;
179         int found_next = 0;
180         struct btrfs_path *path;
181         struct btrfs_csum_item *item;
182         struct extent_buffer *leaf = NULL;
183         u64 csum_offset;
184         u32 csum_result = ~(u32)0;
185         u32 nritems;
186         u32 ins_size;
187         u16 csum_size =
188                 btrfs_super_csum_size(root->fs_info->super_copy);
189
190         path = btrfs_alloc_path();
191         BUG_ON(!path);
192
193         file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
194         file_key.offset = bytenr;
195         file_key.type = BTRFS_EXTENT_CSUM_KEY;
196
197         item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
198         if (!IS_ERR(item)) {
199                 leaf = path->nodes[0];
200                 ret = 0;
201                 goto found;
202         }
203         ret = PTR_ERR(item);
204         if (ret == -EFBIG) {
205                 u32 item_size;
206                 /* we found one, but it isn't big enough yet */
207                 leaf = path->nodes[0];
208                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
209                 if ((item_size / csum_size) >= MAX_CSUM_ITEMS(root, csum_size)) {
210                         /* already at max size, make a new one */
211                         goto insert;
212                 }
213         } else {
214                 int slot = path->slots[0] + 1;
215                 /* we didn't find a csum item, insert one */
216                 nritems = btrfs_header_nritems(path->nodes[0]);
217                 if (path->slots[0] >= nritems - 1) {
218                         ret = btrfs_next_leaf(root, path);
219                         if (ret == 1)
220                                 found_next = 1;
221                         if (ret != 0)
222                                 goto insert;
223                         slot = 0;
224                 }
225                 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
226                 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
227                     found_key.type != BTRFS_EXTENT_CSUM_KEY) {
228                         found_next = 1;
229                         goto insert;
230                 }
231                 next_offset = found_key.offset;
232                 found_next = 1;
233                 goto insert;
234         }
235
236         /*
237          * at this point, we know the tree has an item, but it isn't big
238          * enough yet to put our csum in.  Grow it
239          */
240         btrfs_release_path(path);
241         ret = btrfs_search_slot(trans, root, &file_key, path,
242                                 csum_size, 1);
243         if (ret < 0)
244                 goto fail;
245         if (ret == 0) {
246                 BUG();
247         }
248         if (path->slots[0] == 0) {
249                 goto insert;
250         }
251         path->slots[0]--;
252         leaf = path->nodes[0];
253         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
254         csum_offset = (file_key.offset - found_key.offset) / root->sectorsize;
255         if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
256             found_key.type != BTRFS_EXTENT_CSUM_KEY ||
257             csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
258                 goto insert;
259         }
260         if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
261             csum_size) {
262                 u32 diff = (csum_offset + 1) * csum_size;
263                 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
264                 if (diff != csum_size)
265                         goto insert;
266                 ret = btrfs_extend_item(trans, root, path, diff);
267                 BUG_ON(ret);
268                 goto csum;
269         }
270
271 insert:
272         btrfs_release_path(path);
273         csum_offset = 0;
274         if (found_next) {
275                 u64 tmp = min(alloc_end, next_offset);
276                 tmp -= file_key.offset;
277                 tmp /= root->sectorsize;
278                 tmp = max((u64)1, tmp);
279                 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
280                 ins_size = csum_size * tmp;
281         } else {
282                 ins_size = csum_size;
283         }
284         ret = btrfs_insert_empty_item(trans, root, path, &file_key,
285                                       ins_size);
286         if (ret < 0)
287                 goto fail;
288         if (ret != 0) {
289                 WARN_ON(1);
290                 goto fail;
291         }
292 csum:
293         leaf = path->nodes[0];
294         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
295         ret = 0;
296         item = (struct btrfs_csum_item *)((unsigned char *)item +
297                                           csum_offset * csum_size);
298 found:
299         csum_result = btrfs_csum_data(root, data, csum_result, len);
300         btrfs_csum_final(csum_result, (char *)&csum_result);
301         if (csum_result == 0) {
302                 printk("csum result is 0 for block %llu\n",
303                        (unsigned long long)bytenr);
304         }
305
306         write_extent_buffer(leaf, &csum_result, (unsigned long)item,
307                             csum_size);
308         btrfs_mark_buffer_dirty(path->nodes[0]);
309 fail:
310         btrfs_free_path(path);
311         return ret;
312 }
313
314 /*
315  * helper function for csum removal, this expects the
316  * key to describe the csum pointed to by the path, and it expects
317  * the csum to overlap the range [bytenr, len]
318  *
319  * The csum should not be entirely contained in the range and the
320  * range should not be entirely contained in the csum.
321  *
322  * This calls btrfs_truncate_item with the correct args based on the
323  * overlap, and fixes up the key as required.
324  */
325 static noinline int truncate_one_csum(struct btrfs_trans_handle *trans,
326                                       struct btrfs_root *root,
327                                       struct btrfs_path *path,
328                                       struct btrfs_key *key,
329                                       u64 bytenr, u64 len)
330 {
331         struct extent_buffer *leaf;
332         u16 csum_size =
333                 btrfs_super_csum_size(root->fs_info->super_copy);
334         u64 csum_end;
335         u64 end_byte = bytenr + len;
336         u32 blocksize = root->sectorsize;
337         int ret;
338
339         leaf = path->nodes[0];
340         csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
341         csum_end *= root->sectorsize;
342         csum_end += key->offset;
343
344         if (key->offset < bytenr && csum_end <= end_byte) {
345                 /*
346                  *         [ bytenr - len ]
347                  *         [   ]
348                  *   [csum     ]
349                  *   A simple truncate off the end of the item
350                  */
351                 u32 new_size = (bytenr - key->offset) / blocksize;
352                 new_size *= csum_size;
353                 ret = btrfs_truncate_item(trans, root, path, new_size, 1);
354                 BUG_ON(ret);
355         } else if (key->offset >= bytenr && csum_end > end_byte &&
356                    end_byte > key->offset) {
357                 /*
358                  *         [ bytenr - len ]
359                  *                 [ ]
360                  *                 [csum     ]
361                  * we need to truncate from the beginning of the csum
362                  */
363                 u32 new_size = (csum_end - end_byte) / blocksize;
364                 new_size *= csum_size;
365
366                 ret = btrfs_truncate_item(trans, root, path, new_size, 0);
367                 BUG_ON(ret);
368
369                 key->offset = end_byte;
370                 ret = btrfs_set_item_key_safe(root, path, key);
371                 BUG_ON(ret);
372         } else {
373                 BUG();
374         }
375         return 0;
376 }
377
378 /*
379  * deletes the csum items from the csum tree for a given
380  * range of bytes.
381  */
382 int btrfs_del_csums(struct btrfs_trans_handle *trans,
383                     struct btrfs_root *root, u64 bytenr, u64 len)
384 {
385         struct btrfs_path *path;
386         struct btrfs_key key;
387         u64 end_byte = bytenr + len;
388         u64 csum_end;
389         struct extent_buffer *leaf;
390         int ret;
391         u16 csum_size =
392                 btrfs_super_csum_size(root->fs_info->super_copy);
393         int blocksize = root->sectorsize;
394
395         root = root->fs_info->csum_root;
396
397         path = btrfs_alloc_path();
398         if (!path)
399                 return -ENOMEM;
400
401         while (1) {
402                 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
403                 key.offset = end_byte - 1;
404                 key.type = BTRFS_EXTENT_CSUM_KEY;
405
406                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
407                 if (ret > 0) {
408                         if (path->slots[0] == 0)
409                                 goto out;
410                         path->slots[0]--;
411                 }
412                 leaf = path->nodes[0];
413                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
414
415                 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
416                     key.type != BTRFS_EXTENT_CSUM_KEY) {
417                         break;
418                 }
419
420                 if (key.offset >= end_byte)
421                         break;
422
423                 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
424                 csum_end *= blocksize;
425                 csum_end += key.offset;
426
427                 /* this csum ends before we start, we're done */
428                 if (csum_end <= bytenr)
429                         break;
430
431                 /* delete the entire item, it is inside our range */
432                 if (key.offset >= bytenr && csum_end <= end_byte) {
433                         ret = btrfs_del_item(trans, root, path);
434                         BUG_ON(ret);
435                 } else if (key.offset < bytenr && csum_end > end_byte) {
436                         unsigned long offset;
437                         unsigned long shift_len;
438                         unsigned long item_offset;
439                         /*
440                          *        [ bytenr - len ]
441                          *     [csum                ]
442                          *
443                          * Our bytes are in the middle of the csum,
444                          * we need to split this item and insert a new one.
445                          *
446                          * But we can't drop the path because the
447                          * csum could change, get removed, extended etc.
448                          *
449                          * The trick here is the max size of a csum item leaves
450                          * enough room in the tree block for a single
451                          * item header.  So, we split the item in place,
452                          * adding a new header pointing to the existing
453                          * bytes.  Then we loop around again and we have
454                          * a nicely formed csum item that we can neatly
455                          * truncate.
456                          */
457                         offset = (bytenr - key.offset) / blocksize;
458                         offset *= csum_size;
459
460                         shift_len = (len / blocksize) * csum_size;
461
462                         item_offset = btrfs_item_ptr_offset(leaf,
463                                                             path->slots[0]);
464
465                         memset_extent_buffer(leaf, 0, item_offset + offset,
466                                              shift_len);
467                         key.offset = bytenr;
468
469                         /*
470                          * btrfs_split_item returns -EAGAIN when the
471                          * item changed size or key
472                          */
473                         ret = btrfs_split_item(trans, root, path, &key, offset);
474                         BUG_ON(ret && ret != -EAGAIN);
475
476                         key.offset = end_byte - 1;
477                 } else {
478                         ret = truncate_one_csum(trans, root, path,
479                                                 &key, bytenr, len);
480                         BUG_ON(ret);
481                 }
482                 btrfs_release_path(path);
483         }
484 out:
485         btrfs_free_path(path);
486         return 0;
487 }