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