2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/xattr.h>
36 #include <linux/posix_acl.h>
37 #include <linux/falloc.h>
38 #include <linux/slab.h>
39 #include <linux/ratelimit.h>
40 #include <linux/mount.h>
41 #include <linux/btrfs.h>
42 #include <linux/blkdev.h>
43 #include <linux/posix_acl_xattr.h>
44 #include <linux/uio.h>
45 #include <linux/magic.h>
48 #include "transaction.h"
49 #include "btrfs_inode.h"
50 #include "print-tree.h"
51 #include "ordered-data.h"
55 #include "compression.h"
57 #include "free-space-cache.h"
58 #include "inode-map.h"
65 struct btrfs_iget_args {
66 struct btrfs_key *location;
67 struct btrfs_root *root;
70 struct btrfs_dio_data {
72 u64 unsubmitted_oe_range_start;
73 u64 unsubmitted_oe_range_end;
77 static const struct inode_operations btrfs_dir_inode_operations;
78 static const struct inode_operations btrfs_symlink_inode_operations;
79 static const struct inode_operations btrfs_dir_ro_inode_operations;
80 static const struct inode_operations btrfs_special_inode_operations;
81 static const struct inode_operations btrfs_file_inode_operations;
82 static const struct address_space_operations btrfs_aops;
83 static const struct address_space_operations btrfs_symlink_aops;
84 static const struct file_operations btrfs_dir_file_operations;
85 static const struct extent_io_ops btrfs_extent_io_ops;
87 static struct kmem_cache *btrfs_inode_cachep;
88 struct kmem_cache *btrfs_trans_handle_cachep;
89 struct kmem_cache *btrfs_path_cachep;
90 struct kmem_cache *btrfs_free_space_cachep;
93 static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
94 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
95 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
96 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
97 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
98 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
99 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
100 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
103 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
104 static int btrfs_truncate(struct inode *inode);
105 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
106 static noinline int cow_file_range(struct inode *inode,
107 struct page *locked_page,
108 u64 start, u64 end, u64 delalloc_end,
109 int *page_started, unsigned long *nr_written,
110 int unlock, struct btrfs_dedupe_hash *hash);
111 static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
112 u64 orig_start, u64 block_start,
113 u64 block_len, u64 orig_block_len,
114 u64 ram_bytes, int compress_type,
117 static void __endio_write_update_ordered(struct inode *inode,
118 const u64 offset, const u64 bytes,
119 const bool uptodate);
122 * Cleanup all submitted ordered extents in specified range to handle errors
123 * from the fill_dellaloc() callback.
125 * NOTE: caller must ensure that when an error happens, it can not call
126 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
127 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
128 * to be released, which we want to happen only when finishing the ordered
129 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
130 * fill_delalloc() callback already does proper cleanup for the first page of
131 * the range, that is, it invokes the callback writepage_end_io_hook() for the
132 * range of the first page.
134 static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
138 unsigned long index = offset >> PAGE_SHIFT;
139 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
142 while (index <= end_index) {
143 page = find_get_page(inode->i_mapping, index);
147 ClearPagePrivate2(page);
150 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
151 bytes - PAGE_SIZE, false);
154 static int btrfs_dirty_inode(struct inode *inode);
156 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
157 void btrfs_test_inode_set_ops(struct inode *inode)
159 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
163 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
164 struct inode *inode, struct inode *dir,
165 const struct qstr *qstr)
169 err = btrfs_init_acl(trans, inode, dir);
171 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
176 * this does all the hard work for inserting an inline extent into
177 * the btree. The caller should have done a btrfs_drop_extents so that
178 * no overlapping inline items exist in the btree
180 static int insert_inline_extent(struct btrfs_trans_handle *trans,
181 struct btrfs_path *path, int extent_inserted,
182 struct btrfs_root *root, struct inode *inode,
183 u64 start, size_t size, size_t compressed_size,
185 struct page **compressed_pages)
187 struct extent_buffer *leaf;
188 struct page *page = NULL;
191 struct btrfs_file_extent_item *ei;
193 size_t cur_size = size;
194 unsigned long offset;
196 if (compressed_size && compressed_pages)
197 cur_size = compressed_size;
199 inode_add_bytes(inode, size);
201 if (!extent_inserted) {
202 struct btrfs_key key;
205 key.objectid = btrfs_ino(BTRFS_I(inode));
207 key.type = BTRFS_EXTENT_DATA_KEY;
209 datasize = btrfs_file_extent_calc_inline_size(cur_size);
210 path->leave_spinning = 1;
211 ret = btrfs_insert_empty_item(trans, root, path, &key,
216 leaf = path->nodes[0];
217 ei = btrfs_item_ptr(leaf, path->slots[0],
218 struct btrfs_file_extent_item);
219 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
220 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
221 btrfs_set_file_extent_encryption(leaf, ei, 0);
222 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
223 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
224 ptr = btrfs_file_extent_inline_start(ei);
226 if (compress_type != BTRFS_COMPRESS_NONE) {
229 while (compressed_size > 0) {
230 cpage = compressed_pages[i];
231 cur_size = min_t(unsigned long, compressed_size,
234 kaddr = kmap_atomic(cpage);
235 write_extent_buffer(leaf, kaddr, ptr, cur_size);
236 kunmap_atomic(kaddr);
240 compressed_size -= cur_size;
242 btrfs_set_file_extent_compression(leaf, ei,
245 page = find_get_page(inode->i_mapping,
246 start >> PAGE_SHIFT);
247 btrfs_set_file_extent_compression(leaf, ei, 0);
248 kaddr = kmap_atomic(page);
249 offset = start & (PAGE_SIZE - 1);
250 write_extent_buffer(leaf, kaddr + offset, ptr, size);
251 kunmap_atomic(kaddr);
254 btrfs_mark_buffer_dirty(leaf);
255 btrfs_release_path(path);
258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
266 BTRFS_I(inode)->disk_i_size = inode->i_size;
267 ret = btrfs_update_inode(trans, root, inode);
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
279 static noinline int cow_file_range_inline(struct btrfs_root *root,
280 struct inode *inode, u64 start,
281 u64 end, size_t compressed_size,
283 struct page **compressed_pages)
285 struct btrfs_fs_info *fs_info = root->fs_info;
286 struct btrfs_trans_handle *trans;
287 u64 isize = i_size_read(inode);
288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
291 u64 data_len = inline_len;
293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
298 data_len = compressed_size;
301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
306 data_len > fs_info->max_inline) {
310 path = btrfs_alloc_path();
314 trans = btrfs_join_transaction(root);
316 btrfs_free_path(path);
317 return PTR_ERR(trans);
319 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
325 extent_item_size = btrfs_file_extent_calc_inline_size(
328 ret = __btrfs_drop_extents(trans, root, inode, path,
329 start, aligned_end, NULL,
330 1, 1, extent_item_size, &extent_inserted);
332 btrfs_abort_transaction(trans, ret);
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
338 ret = insert_inline_extent(trans, path, extent_inserted,
340 inline_len, compressed_size,
341 compress_type, compressed_pages);
342 if (ret && ret != -ENOSPC) {
343 btrfs_abort_transaction(trans, ret);
345 } else if (ret == -ENOSPC) {
350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
351 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
360 btrfs_free_path(path);
361 btrfs_end_transaction(trans);
365 struct async_extent {
370 unsigned long nr_pages;
372 struct list_head list;
377 struct btrfs_root *root;
378 struct page *locked_page;
381 unsigned int write_flags;
382 struct list_head extents;
383 struct btrfs_work work;
386 static noinline int add_async_extent(struct async_cow *cow,
387 u64 start, u64 ram_size,
390 unsigned long nr_pages,
393 struct async_extent *async_extent;
395 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
396 BUG_ON(!async_extent); /* -ENOMEM */
397 async_extent->start = start;
398 async_extent->ram_size = ram_size;
399 async_extent->compressed_size = compressed_size;
400 async_extent->pages = pages;
401 async_extent->nr_pages = nr_pages;
402 async_extent->compress_type = compress_type;
403 list_add_tail(&async_extent->list, &cow->extents);
407 static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
412 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
415 if (BTRFS_I(inode)->defrag_compress)
417 /* bad compression ratios */
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
420 if (btrfs_test_opt(fs_info, COMPRESS) ||
421 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
422 BTRFS_I(inode)->prop_compress)
423 return btrfs_compress_heuristic(inode, start, end);
427 static inline void inode_should_defrag(struct btrfs_inode *inode,
428 u64 start, u64 end, u64 num_bytes, u64 small_write)
430 /* If this is a small write inside eof, kick off a defrag */
431 if (num_bytes < small_write &&
432 (start > 0 || end + 1 < inode->disk_i_size))
433 btrfs_add_inode_defrag(NULL, inode);
437 * we create compressed extents in two phases. The first
438 * phase compresses a range of pages that have already been
439 * locked (both pages and state bits are locked).
441 * This is done inside an ordered work queue, and the compression
442 * is spread across many cpus. The actual IO submission is step
443 * two, and the ordered work queue takes care of making sure that
444 * happens in the same order things were put onto the queue by
445 * writepages and friends.
447 * If this code finds it can't get good compression, it puts an
448 * entry onto the work queue to write the uncompressed bytes. This
449 * makes sure that both compressed inodes and uncompressed inodes
450 * are written in the same order that the flusher thread sent them
453 static noinline void compress_file_range(struct inode *inode,
454 struct page *locked_page,
456 struct async_cow *async_cow,
459 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
460 struct btrfs_root *root = BTRFS_I(inode)->root;
461 u64 blocksize = fs_info->sectorsize;
463 u64 isize = i_size_read(inode);
465 struct page **pages = NULL;
466 unsigned long nr_pages;
467 unsigned long total_compressed = 0;
468 unsigned long total_in = 0;
471 int compress_type = fs_info->compress_type;
474 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
477 actual_end = min_t(u64, isize, end + 1);
480 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
481 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
482 nr_pages = min_t(unsigned long, nr_pages,
483 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
486 * we don't want to send crud past the end of i_size through
487 * compression, that's just a waste of CPU time. So, if the
488 * end of the file is before the start of our current
489 * requested range of bytes, we bail out to the uncompressed
490 * cleanup code that can deal with all of this.
492 * It isn't really the fastest way to fix things, but this is a
493 * very uncommon corner.
495 if (actual_end <= start)
496 goto cleanup_and_bail_uncompressed;
498 total_compressed = actual_end - start;
501 * skip compression for a small file range(<=blocksize) that
502 * isn't an inline extent, since it doesn't save disk space at all.
504 if (total_compressed <= blocksize &&
505 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
506 goto cleanup_and_bail_uncompressed;
508 total_compressed = min_t(unsigned long, total_compressed,
509 BTRFS_MAX_UNCOMPRESSED);
514 * we do compression for mount -o compress and when the
515 * inode has not been flagged as nocompress. This flag can
516 * change at any time if we discover bad compression ratios.
518 if (inode_need_compress(inode, start, end)) {
520 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
522 /* just bail out to the uncompressed code */
526 if (BTRFS_I(inode)->defrag_compress)
527 compress_type = BTRFS_I(inode)->defrag_compress;
528 else if (BTRFS_I(inode)->prop_compress)
529 compress_type = BTRFS_I(inode)->prop_compress;
532 * we need to call clear_page_dirty_for_io on each
533 * page in the range. Otherwise applications with the file
534 * mmap'd can wander in and change the page contents while
535 * we are compressing them.
537 * If the compression fails for any reason, we set the pages
538 * dirty again later on.
540 extent_range_clear_dirty_for_io(inode, start, end);
543 /* Compression level is applied here and only here */
544 ret = btrfs_compress_pages(
545 compress_type | (fs_info->compress_level << 4),
546 inode->i_mapping, start,
553 unsigned long offset = total_compressed &
555 struct page *page = pages[nr_pages - 1];
558 /* zero the tail end of the last page, we might be
559 * sending it down to disk
562 kaddr = kmap_atomic(page);
563 memset(kaddr + offset, 0,
565 kunmap_atomic(kaddr);
572 /* lets try to make an inline extent */
573 if (ret || total_in < actual_end) {
574 /* we didn't compress the entire range, try
575 * to make an uncompressed inline extent.
577 ret = cow_file_range_inline(root, inode, start, end,
578 0, BTRFS_COMPRESS_NONE, NULL);
580 /* try making a compressed inline extent */
581 ret = cow_file_range_inline(root, inode, start, end,
583 compress_type, pages);
586 unsigned long clear_flags = EXTENT_DELALLOC |
587 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
588 EXTENT_DO_ACCOUNTING;
589 unsigned long page_error_op;
591 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
594 * inline extent creation worked or returned error,
595 * we don't need to create any more async work items.
596 * Unlock and free up our temp pages.
598 * We use DO_ACCOUNTING here because we need the
599 * delalloc_release_metadata to be done _after_ we drop
600 * our outstanding extent for clearing delalloc for this
603 extent_clear_unlock_delalloc(inode, start, end, end,
616 * we aren't doing an inline extent round the compressed size
617 * up to a block size boundary so the allocator does sane
620 total_compressed = ALIGN(total_compressed, blocksize);
623 * one last check to make sure the compression is really a
624 * win, compare the page count read with the blocks on disk,
625 * compression must free at least one sector size
627 total_in = ALIGN(total_in, PAGE_SIZE);
628 if (total_compressed + blocksize <= total_in) {
632 * The async work queues will take care of doing actual
633 * allocation on disk for these compressed pages, and
634 * will submit them to the elevator.
636 add_async_extent(async_cow, start, total_in,
637 total_compressed, pages, nr_pages,
640 if (start + total_in < end) {
651 * the compression code ran but failed to make things smaller,
652 * free any pages it allocated and our page pointer array
654 for (i = 0; i < nr_pages; i++) {
655 WARN_ON(pages[i]->mapping);
660 total_compressed = 0;
663 /* flag the file so we don't compress in the future */
664 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
665 !(BTRFS_I(inode)->prop_compress)) {
666 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
669 cleanup_and_bail_uncompressed:
671 * No compression, but we still need to write the pages in the file
672 * we've been given so far. redirty the locked page if it corresponds
673 * to our extent and set things up for the async work queue to run
674 * cow_file_range to do the normal delalloc dance.
676 if (page_offset(locked_page) >= start &&
677 page_offset(locked_page) <= end)
678 __set_page_dirty_nobuffers(locked_page);
679 /* unlocked later on in the async handlers */
682 extent_range_redirty_for_io(inode, start, end);
683 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
684 BTRFS_COMPRESS_NONE);
690 for (i = 0; i < nr_pages; i++) {
691 WARN_ON(pages[i]->mapping);
697 static void free_async_extent_pages(struct async_extent *async_extent)
701 if (!async_extent->pages)
704 for (i = 0; i < async_extent->nr_pages; i++) {
705 WARN_ON(async_extent->pages[i]->mapping);
706 put_page(async_extent->pages[i]);
708 kfree(async_extent->pages);
709 async_extent->nr_pages = 0;
710 async_extent->pages = NULL;
714 * phase two of compressed writeback. This is the ordered portion
715 * of the code, which only gets called in the order the work was
716 * queued. We walk all the async extents created by compress_file_range
717 * and send them down to the disk.
719 static noinline void submit_compressed_extents(struct inode *inode,
720 struct async_cow *async_cow)
722 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
723 struct async_extent *async_extent;
725 struct btrfs_key ins;
726 struct extent_map *em;
727 struct btrfs_root *root = BTRFS_I(inode)->root;
728 struct extent_io_tree *io_tree;
732 while (!list_empty(&async_cow->extents)) {
733 async_extent = list_entry(async_cow->extents.next,
734 struct async_extent, list);
735 list_del(&async_extent->list);
737 io_tree = &BTRFS_I(inode)->io_tree;
740 /* did the compression code fall back to uncompressed IO? */
741 if (!async_extent->pages) {
742 int page_started = 0;
743 unsigned long nr_written = 0;
745 lock_extent(io_tree, async_extent->start,
746 async_extent->start +
747 async_extent->ram_size - 1);
749 /* allocate blocks */
750 ret = cow_file_range(inode, async_cow->locked_page,
752 async_extent->start +
753 async_extent->ram_size - 1,
754 async_extent->start +
755 async_extent->ram_size - 1,
756 &page_started, &nr_written, 0,
762 * if page_started, cow_file_range inserted an
763 * inline extent and took care of all the unlocking
764 * and IO for us. Otherwise, we need to submit
765 * all those pages down to the drive.
767 if (!page_started && !ret)
768 extent_write_locked_range(io_tree,
769 inode, async_extent->start,
770 async_extent->start +
771 async_extent->ram_size - 1,
774 unlock_page(async_cow->locked_page);
780 lock_extent(io_tree, async_extent->start,
781 async_extent->start + async_extent->ram_size - 1);
783 ret = btrfs_reserve_extent(root, async_extent->ram_size,
784 async_extent->compressed_size,
785 async_extent->compressed_size,
786 0, alloc_hint, &ins, 1, 1);
788 free_async_extent_pages(async_extent);
790 if (ret == -ENOSPC) {
791 unlock_extent(io_tree, async_extent->start,
792 async_extent->start +
793 async_extent->ram_size - 1);
796 * we need to redirty the pages if we decide to
797 * fallback to uncompressed IO, otherwise we
798 * will not submit these pages down to lower
801 extent_range_redirty_for_io(inode,
803 async_extent->start +
804 async_extent->ram_size - 1);
811 * here we're doing allocation and writeback of the
814 em = create_io_em(inode, async_extent->start,
815 async_extent->ram_size, /* len */
816 async_extent->start, /* orig_start */
817 ins.objectid, /* block_start */
818 ins.offset, /* block_len */
819 ins.offset, /* orig_block_len */
820 async_extent->ram_size, /* ram_bytes */
821 async_extent->compress_type,
822 BTRFS_ORDERED_COMPRESSED);
824 /* ret value is not necessary due to void function */
825 goto out_free_reserve;
828 ret = btrfs_add_ordered_extent_compress(inode,
831 async_extent->ram_size,
833 BTRFS_ORDERED_COMPRESSED,
834 async_extent->compress_type);
836 btrfs_drop_extent_cache(BTRFS_I(inode),
838 async_extent->start +
839 async_extent->ram_size - 1, 0);
840 goto out_free_reserve;
842 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
845 * clear dirty, set writeback and unlock the pages.
847 extent_clear_unlock_delalloc(inode, async_extent->start,
848 async_extent->start +
849 async_extent->ram_size - 1,
850 async_extent->start +
851 async_extent->ram_size - 1,
852 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
853 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
855 if (btrfs_submit_compressed_write(inode,
857 async_extent->ram_size,
859 ins.offset, async_extent->pages,
860 async_extent->nr_pages,
861 async_cow->write_flags)) {
862 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
863 struct page *p = async_extent->pages[0];
864 const u64 start = async_extent->start;
865 const u64 end = start + async_extent->ram_size - 1;
867 p->mapping = inode->i_mapping;
868 tree->ops->writepage_end_io_hook(p, start, end,
871 extent_clear_unlock_delalloc(inode, start, end, end,
875 free_async_extent_pages(async_extent);
877 alloc_hint = ins.objectid + ins.offset;
883 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
884 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
886 extent_clear_unlock_delalloc(inode, async_extent->start,
887 async_extent->start +
888 async_extent->ram_size - 1,
889 async_extent->start +
890 async_extent->ram_size - 1,
891 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
892 EXTENT_DELALLOC_NEW |
893 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
894 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
895 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
897 free_async_extent_pages(async_extent);
902 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
905 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
906 struct extent_map *em;
909 read_lock(&em_tree->lock);
910 em = search_extent_mapping(em_tree, start, num_bytes);
913 * if block start isn't an actual block number then find the
914 * first block in this inode and use that as a hint. If that
915 * block is also bogus then just don't worry about it.
917 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
919 em = search_extent_mapping(em_tree, 0, 0);
920 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
921 alloc_hint = em->block_start;
925 alloc_hint = em->block_start;
929 read_unlock(&em_tree->lock);
935 * when extent_io.c finds a delayed allocation range in the file,
936 * the call backs end up in this code. The basic idea is to
937 * allocate extents on disk for the range, and create ordered data structs
938 * in ram to track those extents.
940 * locked_page is the page that writepage had locked already. We use
941 * it to make sure we don't do extra locks or unlocks.
943 * *page_started is set to one if we unlock locked_page and do everything
944 * required to start IO on it. It may be clean and already done with
947 static noinline int cow_file_range(struct inode *inode,
948 struct page *locked_page,
949 u64 start, u64 end, u64 delalloc_end,
950 int *page_started, unsigned long *nr_written,
951 int unlock, struct btrfs_dedupe_hash *hash)
953 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
954 struct btrfs_root *root = BTRFS_I(inode)->root;
957 unsigned long ram_size;
959 u64 cur_alloc_size = 0;
960 u64 blocksize = fs_info->sectorsize;
961 struct btrfs_key ins;
962 struct extent_map *em;
964 unsigned long page_ops;
965 bool extent_reserved = false;
968 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
974 num_bytes = ALIGN(end - start + 1, blocksize);
975 num_bytes = max(blocksize, num_bytes);
976 disk_num_bytes = num_bytes;
978 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
981 /* lets try to make an inline extent */
982 ret = cow_file_range_inline(root, inode, start, end, 0,
983 BTRFS_COMPRESS_NONE, NULL);
986 * We use DO_ACCOUNTING here because we need the
987 * delalloc_release_metadata to be run _after_ we drop
988 * our outstanding extent for clearing delalloc for this
991 extent_clear_unlock_delalloc(inode, start, end,
993 EXTENT_LOCKED | EXTENT_DELALLOC |
994 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
995 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
996 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
998 *nr_written = *nr_written +
999 (end - start + PAGE_SIZE) / PAGE_SIZE;
1002 } else if (ret < 0) {
1007 BUG_ON(disk_num_bytes >
1008 btrfs_super_total_bytes(fs_info->super_copy));
1010 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1011 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1012 start + num_bytes - 1, 0);
1014 while (disk_num_bytes > 0) {
1015 cur_alloc_size = disk_num_bytes;
1016 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
1017 fs_info->sectorsize, 0, alloc_hint,
1021 cur_alloc_size = ins.offset;
1022 extent_reserved = true;
1024 ram_size = ins.offset;
1025 em = create_io_em(inode, start, ins.offset, /* len */
1026 start, /* orig_start */
1027 ins.objectid, /* block_start */
1028 ins.offset, /* block_len */
1029 ins.offset, /* orig_block_len */
1030 ram_size, /* ram_bytes */
1031 BTRFS_COMPRESS_NONE, /* compress_type */
1032 BTRFS_ORDERED_REGULAR /* type */);
1035 free_extent_map(em);
1037 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1038 ram_size, cur_alloc_size, 0);
1040 goto out_drop_extent_cache;
1042 if (root->root_key.objectid ==
1043 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1044 ret = btrfs_reloc_clone_csums(inode, start,
1047 * Only drop cache here, and process as normal.
1049 * We must not allow extent_clear_unlock_delalloc()
1050 * at out_unlock label to free meta of this ordered
1051 * extent, as its meta should be freed by
1052 * btrfs_finish_ordered_io().
1054 * So we must continue until @start is increased to
1055 * skip current ordered extent.
1058 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1059 start + ram_size - 1, 0);
1062 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1064 /* we're not doing compressed IO, don't unlock the first
1065 * page (which the caller expects to stay locked), don't
1066 * clear any dirty bits and don't set any writeback bits
1068 * Do set the Private2 bit so we know this page was properly
1069 * setup for writepage
1071 page_ops = unlock ? PAGE_UNLOCK : 0;
1072 page_ops |= PAGE_SET_PRIVATE2;
1074 extent_clear_unlock_delalloc(inode, start,
1075 start + ram_size - 1,
1076 delalloc_end, locked_page,
1077 EXTENT_LOCKED | EXTENT_DELALLOC,
1079 if (disk_num_bytes < cur_alloc_size)
1082 disk_num_bytes -= cur_alloc_size;
1083 num_bytes -= cur_alloc_size;
1084 alloc_hint = ins.objectid + ins.offset;
1085 start += cur_alloc_size;
1086 extent_reserved = false;
1089 * btrfs_reloc_clone_csums() error, since start is increased
1090 * extent_clear_unlock_delalloc() at out_unlock label won't
1091 * free metadata of current ordered extent, we're OK to exit.
1099 out_drop_extent_cache:
1100 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
1102 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1103 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1105 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1106 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
1107 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1110 * If we reserved an extent for our delalloc range (or a subrange) and
1111 * failed to create the respective ordered extent, then it means that
1112 * when we reserved the extent we decremented the extent's size from
1113 * the data space_info's bytes_may_use counter and incremented the
1114 * space_info's bytes_reserved counter by the same amount. We must make
1115 * sure extent_clear_unlock_delalloc() does not try to decrement again
1116 * the data space_info's bytes_may_use counter, therefore we do not pass
1117 * it the flag EXTENT_CLEAR_DATA_RESV.
1119 if (extent_reserved) {
1120 extent_clear_unlock_delalloc(inode, start,
1121 start + cur_alloc_size,
1122 start + cur_alloc_size,
1126 start += cur_alloc_size;
1130 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1132 clear_bits | EXTENT_CLEAR_DATA_RESV,
1138 * work queue call back to started compression on a file and pages
1140 static noinline void async_cow_start(struct btrfs_work *work)
1142 struct async_cow *async_cow;
1144 async_cow = container_of(work, struct async_cow, work);
1146 compress_file_range(async_cow->inode, async_cow->locked_page,
1147 async_cow->start, async_cow->end, async_cow,
1149 if (num_added == 0) {
1150 btrfs_add_delayed_iput(async_cow->inode);
1151 async_cow->inode = NULL;
1156 * work queue call back to submit previously compressed pages
1158 static noinline void async_cow_submit(struct btrfs_work *work)
1160 struct btrfs_fs_info *fs_info;
1161 struct async_cow *async_cow;
1162 struct btrfs_root *root;
1163 unsigned long nr_pages;
1165 async_cow = container_of(work, struct async_cow, work);
1167 root = async_cow->root;
1168 fs_info = root->fs_info;
1169 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1173 * atomic_sub_return implies a barrier for waitqueue_active
1175 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1177 waitqueue_active(&fs_info->async_submit_wait))
1178 wake_up(&fs_info->async_submit_wait);
1180 if (async_cow->inode)
1181 submit_compressed_extents(async_cow->inode, async_cow);
1184 static noinline void async_cow_free(struct btrfs_work *work)
1186 struct async_cow *async_cow;
1187 async_cow = container_of(work, struct async_cow, work);
1188 if (async_cow->inode)
1189 btrfs_add_delayed_iput(async_cow->inode);
1193 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1194 u64 start, u64 end, int *page_started,
1195 unsigned long *nr_written,
1196 unsigned int write_flags)
1198 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1199 struct async_cow *async_cow;
1200 struct btrfs_root *root = BTRFS_I(inode)->root;
1201 unsigned long nr_pages;
1204 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1206 while (start < end) {
1207 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
1208 BUG_ON(!async_cow); /* -ENOMEM */
1209 async_cow->inode = igrab(inode);
1210 async_cow->root = root;
1211 async_cow->locked_page = locked_page;
1212 async_cow->start = start;
1213 async_cow->write_flags = write_flags;
1215 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1216 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
1219 cur_end = min(end, start + SZ_512K - 1);
1221 async_cow->end = cur_end;
1222 INIT_LIST_HEAD(&async_cow->extents);
1224 btrfs_init_work(&async_cow->work,
1225 btrfs_delalloc_helper,
1226 async_cow_start, async_cow_submit,
1229 nr_pages = (cur_end - start + PAGE_SIZE) >>
1231 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1233 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
1235 *nr_written += nr_pages;
1236 start = cur_end + 1;
1242 static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
1243 u64 bytenr, u64 num_bytes)
1246 struct btrfs_ordered_sum *sums;
1249 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
1250 bytenr + num_bytes - 1, &list, 0);
1251 if (ret == 0 && list_empty(&list))
1254 while (!list_empty(&list)) {
1255 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1256 list_del(&sums->list);
1263 * when nowcow writeback call back. This checks for snapshots or COW copies
1264 * of the extents that exist in the file, and COWs the file as required.
1266 * If no cow copies or snapshots exist, we write directly to the existing
1269 static noinline int run_delalloc_nocow(struct inode *inode,
1270 struct page *locked_page,
1271 u64 start, u64 end, int *page_started, int force,
1272 unsigned long *nr_written)
1274 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1275 struct btrfs_root *root = BTRFS_I(inode)->root;
1276 struct extent_buffer *leaf;
1277 struct btrfs_path *path;
1278 struct btrfs_file_extent_item *fi;
1279 struct btrfs_key found_key;
1280 struct extent_map *em;
1295 u64 ino = btrfs_ino(BTRFS_I(inode));
1297 path = btrfs_alloc_path();
1299 extent_clear_unlock_delalloc(inode, start, end, end,
1301 EXTENT_LOCKED | EXTENT_DELALLOC |
1302 EXTENT_DO_ACCOUNTING |
1303 EXTENT_DEFRAG, PAGE_UNLOCK |
1305 PAGE_SET_WRITEBACK |
1306 PAGE_END_WRITEBACK);
1310 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
1312 cow_start = (u64)-1;
1315 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
1319 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1320 leaf = path->nodes[0];
1321 btrfs_item_key_to_cpu(leaf, &found_key,
1322 path->slots[0] - 1);
1323 if (found_key.objectid == ino &&
1324 found_key.type == BTRFS_EXTENT_DATA_KEY)
1329 leaf = path->nodes[0];
1330 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1331 ret = btrfs_next_leaf(root, path);
1336 leaf = path->nodes[0];
1342 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1344 if (found_key.objectid > ino)
1346 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1347 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1351 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
1352 found_key.offset > end)
1355 if (found_key.offset > cur_offset) {
1356 extent_end = found_key.offset;
1361 fi = btrfs_item_ptr(leaf, path->slots[0],
1362 struct btrfs_file_extent_item);
1363 extent_type = btrfs_file_extent_type(leaf, fi);
1365 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1366 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1367 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1368 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1369 extent_offset = btrfs_file_extent_offset(leaf, fi);
1370 extent_end = found_key.offset +
1371 btrfs_file_extent_num_bytes(leaf, fi);
1373 btrfs_file_extent_disk_num_bytes(leaf, fi);
1374 if (extent_end <= start) {
1378 if (disk_bytenr == 0)
1380 if (btrfs_file_extent_compression(leaf, fi) ||
1381 btrfs_file_extent_encryption(leaf, fi) ||
1382 btrfs_file_extent_other_encoding(leaf, fi))
1384 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1386 if (btrfs_extent_readonly(fs_info, disk_bytenr))
1388 if (btrfs_cross_ref_exist(root, ino,
1390 extent_offset, disk_bytenr))
1392 disk_bytenr += extent_offset;
1393 disk_bytenr += cur_offset - found_key.offset;
1394 num_bytes = min(end + 1, extent_end) - cur_offset;
1396 * if there are pending snapshots for this root,
1397 * we fall into common COW way.
1400 err = btrfs_start_write_no_snapshotting(root);
1405 * force cow if csum exists in the range.
1406 * this ensure that csum for a given extent are
1407 * either valid or do not exist.
1409 if (csum_exist_in_range(fs_info, disk_bytenr,
1412 btrfs_end_write_no_snapshotting(root);
1415 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1417 btrfs_end_write_no_snapshotting(root);
1421 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1422 extent_end = found_key.offset +
1423 btrfs_file_extent_inline_len(leaf,
1424 path->slots[0], fi);
1425 extent_end = ALIGN(extent_end,
1426 fs_info->sectorsize);
1431 if (extent_end <= start) {
1433 if (!nolock && nocow)
1434 btrfs_end_write_no_snapshotting(root);
1436 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1440 if (cow_start == (u64)-1)
1441 cow_start = cur_offset;
1442 cur_offset = extent_end;
1443 if (cur_offset > end)
1449 btrfs_release_path(path);
1450 if (cow_start != (u64)-1) {
1451 ret = cow_file_range(inode, locked_page,
1452 cow_start, found_key.offset - 1,
1453 end, page_started, nr_written, 1,
1456 if (!nolock && nocow)
1457 btrfs_end_write_no_snapshotting(root);
1459 btrfs_dec_nocow_writers(fs_info,
1463 cow_start = (u64)-1;
1466 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1467 u64 orig_start = found_key.offset - extent_offset;
1469 em = create_io_em(inode, cur_offset, num_bytes,
1471 disk_bytenr, /* block_start */
1472 num_bytes, /* block_len */
1473 disk_num_bytes, /* orig_block_len */
1474 ram_bytes, BTRFS_COMPRESS_NONE,
1475 BTRFS_ORDERED_PREALLOC);
1477 if (!nolock && nocow)
1478 btrfs_end_write_no_snapshotting(root);
1480 btrfs_dec_nocow_writers(fs_info,
1485 free_extent_map(em);
1488 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1489 type = BTRFS_ORDERED_PREALLOC;
1491 type = BTRFS_ORDERED_NOCOW;
1494 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1495 num_bytes, num_bytes, type);
1497 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1498 BUG_ON(ret); /* -ENOMEM */
1500 if (root->root_key.objectid ==
1501 BTRFS_DATA_RELOC_TREE_OBJECTID)
1503 * Error handled later, as we must prevent
1504 * extent_clear_unlock_delalloc() in error handler
1505 * from freeing metadata of created ordered extent.
1507 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1510 extent_clear_unlock_delalloc(inode, cur_offset,
1511 cur_offset + num_bytes - 1, end,
1512 locked_page, EXTENT_LOCKED |
1514 EXTENT_CLEAR_DATA_RESV,
1515 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1517 if (!nolock && nocow)
1518 btrfs_end_write_no_snapshotting(root);
1519 cur_offset = extent_end;
1522 * btrfs_reloc_clone_csums() error, now we're OK to call error
1523 * handler, as metadata for created ordered extent will only
1524 * be freed by btrfs_finish_ordered_io().
1528 if (cur_offset > end)
1531 btrfs_release_path(path);
1533 if (cur_offset <= end && cow_start == (u64)-1) {
1534 cow_start = cur_offset;
1538 if (cow_start != (u64)-1) {
1539 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1540 page_started, nr_written, 1, NULL);
1546 if (ret && cur_offset < end)
1547 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
1548 locked_page, EXTENT_LOCKED |
1549 EXTENT_DELALLOC | EXTENT_DEFRAG |
1550 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1552 PAGE_SET_WRITEBACK |
1553 PAGE_END_WRITEBACK);
1554 btrfs_free_path(path);
1558 static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1561 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1562 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1566 * @defrag_bytes is a hint value, no spinlock held here,
1567 * if is not zero, it means the file is defragging.
1568 * Force cow if given extent needs to be defragged.
1570 if (BTRFS_I(inode)->defrag_bytes &&
1571 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1572 EXTENT_DEFRAG, 0, NULL))
1579 * extent_io.c call back to do delayed allocation processing
1581 static int run_delalloc_range(void *private_data, struct page *locked_page,
1582 u64 start, u64 end, int *page_started,
1583 unsigned long *nr_written,
1584 struct writeback_control *wbc)
1586 struct inode *inode = private_data;
1588 int force_cow = need_force_cow(inode, start, end);
1589 unsigned int write_flags = wbc_to_write_flags(wbc);
1591 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1592 ret = run_delalloc_nocow(inode, locked_page, start, end,
1593 page_started, 1, nr_written);
1594 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1595 ret = run_delalloc_nocow(inode, locked_page, start, end,
1596 page_started, 0, nr_written);
1597 } else if (!inode_need_compress(inode, start, end)) {
1598 ret = cow_file_range(inode, locked_page, start, end, end,
1599 page_started, nr_written, 1, NULL);
1601 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1602 &BTRFS_I(inode)->runtime_flags);
1603 ret = cow_file_range_async(inode, locked_page, start, end,
1604 page_started, nr_written,
1608 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
1612 static void btrfs_split_extent_hook(void *private_data,
1613 struct extent_state *orig, u64 split)
1615 struct inode *inode = private_data;
1618 /* not delalloc, ignore it */
1619 if (!(orig->state & EXTENT_DELALLOC))
1622 size = orig->end - orig->start + 1;
1623 if (size > BTRFS_MAX_EXTENT_SIZE) {
1628 * See the explanation in btrfs_merge_extent_hook, the same
1629 * applies here, just in reverse.
1631 new_size = orig->end - split + 1;
1632 num_extents = count_max_extents(new_size);
1633 new_size = split - orig->start;
1634 num_extents += count_max_extents(new_size);
1635 if (count_max_extents(size) >= num_extents)
1639 spin_lock(&BTRFS_I(inode)->lock);
1640 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1641 spin_unlock(&BTRFS_I(inode)->lock);
1645 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1646 * extents so we can keep track of new extents that are just merged onto old
1647 * extents, such as when we are doing sequential writes, so we can properly
1648 * account for the metadata space we'll need.
1650 static void btrfs_merge_extent_hook(void *private_data,
1651 struct extent_state *new,
1652 struct extent_state *other)
1654 struct inode *inode = private_data;
1655 u64 new_size, old_size;
1658 /* not delalloc, ignore it */
1659 if (!(other->state & EXTENT_DELALLOC))
1662 if (new->start > other->start)
1663 new_size = new->end - other->start + 1;
1665 new_size = other->end - new->start + 1;
1667 /* we're not bigger than the max, unreserve the space and go */
1668 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1669 spin_lock(&BTRFS_I(inode)->lock);
1670 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
1671 spin_unlock(&BTRFS_I(inode)->lock);
1676 * We have to add up either side to figure out how many extents were
1677 * accounted for before we merged into one big extent. If the number of
1678 * extents we accounted for is <= the amount we need for the new range
1679 * then we can return, otherwise drop. Think of it like this
1683 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1684 * need 2 outstanding extents, on one side we have 1 and the other side
1685 * we have 1 so they are == and we can return. But in this case
1687 * [MAX_SIZE+4k][MAX_SIZE+4k]
1689 * Each range on their own accounts for 2 extents, but merged together
1690 * they are only 3 extents worth of accounting, so we need to drop in
1693 old_size = other->end - other->start + 1;
1694 num_extents = count_max_extents(old_size);
1695 old_size = new->end - new->start + 1;
1696 num_extents += count_max_extents(old_size);
1697 if (count_max_extents(new_size) >= num_extents)
1700 spin_lock(&BTRFS_I(inode)->lock);
1701 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
1702 spin_unlock(&BTRFS_I(inode)->lock);
1705 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1706 struct inode *inode)
1708 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1710 spin_lock(&root->delalloc_lock);
1711 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1712 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1713 &root->delalloc_inodes);
1714 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1715 &BTRFS_I(inode)->runtime_flags);
1716 root->nr_delalloc_inodes++;
1717 if (root->nr_delalloc_inodes == 1) {
1718 spin_lock(&fs_info->delalloc_root_lock);
1719 BUG_ON(!list_empty(&root->delalloc_root));
1720 list_add_tail(&root->delalloc_root,
1721 &fs_info->delalloc_roots);
1722 spin_unlock(&fs_info->delalloc_root_lock);
1725 spin_unlock(&root->delalloc_lock);
1728 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1729 struct btrfs_inode *inode)
1731 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
1733 spin_lock(&root->delalloc_lock);
1734 if (!list_empty(&inode->delalloc_inodes)) {
1735 list_del_init(&inode->delalloc_inodes);
1736 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1737 &inode->runtime_flags);
1738 root->nr_delalloc_inodes--;
1739 if (!root->nr_delalloc_inodes) {
1740 spin_lock(&fs_info->delalloc_root_lock);
1741 BUG_ON(list_empty(&root->delalloc_root));
1742 list_del_init(&root->delalloc_root);
1743 spin_unlock(&fs_info->delalloc_root_lock);
1746 spin_unlock(&root->delalloc_lock);
1750 * extent_io.c set_bit_hook, used to track delayed allocation
1751 * bytes in this file, and to maintain the list of inodes that
1752 * have pending delalloc work to be done.
1754 static void btrfs_set_bit_hook(void *private_data,
1755 struct extent_state *state, unsigned *bits)
1757 struct inode *inode = private_data;
1759 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1761 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1764 * set_bit and clear bit hooks normally require _irqsave/restore
1765 * but in this case, we are only testing for the DELALLOC
1766 * bit, which is only set or cleared with irqs on
1768 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1769 struct btrfs_root *root = BTRFS_I(inode)->root;
1770 u64 len = state->end + 1 - state->start;
1771 u32 num_extents = count_max_extents(len);
1772 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
1774 spin_lock(&BTRFS_I(inode)->lock);
1775 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1776 spin_unlock(&BTRFS_I(inode)->lock);
1778 /* For sanity tests */
1779 if (btrfs_is_testing(fs_info))
1782 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1783 fs_info->delalloc_batch);
1784 spin_lock(&BTRFS_I(inode)->lock);
1785 BTRFS_I(inode)->delalloc_bytes += len;
1786 if (*bits & EXTENT_DEFRAG)
1787 BTRFS_I(inode)->defrag_bytes += len;
1788 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1789 &BTRFS_I(inode)->runtime_flags))
1790 btrfs_add_delalloc_inodes(root, inode);
1791 spin_unlock(&BTRFS_I(inode)->lock);
1794 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1795 (*bits & EXTENT_DELALLOC_NEW)) {
1796 spin_lock(&BTRFS_I(inode)->lock);
1797 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1799 spin_unlock(&BTRFS_I(inode)->lock);
1804 * extent_io.c clear_bit_hook, see set_bit_hook for why
1806 static void btrfs_clear_bit_hook(void *private_data,
1807 struct extent_state *state,
1810 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
1811 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
1812 u64 len = state->end + 1 - state->start;
1813 u32 num_extents = count_max_extents(len);
1815 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1816 spin_lock(&inode->lock);
1817 inode->defrag_bytes -= len;
1818 spin_unlock(&inode->lock);
1822 * set_bit and clear bit hooks normally require _irqsave/restore
1823 * but in this case, we are only testing for the DELALLOC
1824 * bit, which is only set or cleared with irqs on
1826 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1827 struct btrfs_root *root = inode->root;
1828 bool do_list = !btrfs_is_free_space_inode(inode);
1830 spin_lock(&inode->lock);
1831 btrfs_mod_outstanding_extents(inode, -num_extents);
1832 spin_unlock(&inode->lock);
1835 * We don't reserve metadata space for space cache inodes so we
1836 * don't need to call dellalloc_release_metadata if there is an
1839 if (*bits & EXTENT_CLEAR_META_RESV &&
1840 root != fs_info->tree_root)
1841 btrfs_delalloc_release_metadata(inode, len);
1843 /* For sanity tests. */
1844 if (btrfs_is_testing(fs_info))
1847 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1848 do_list && !(state->state & EXTENT_NORESERVE) &&
1849 (*bits & EXTENT_CLEAR_DATA_RESV))
1850 btrfs_free_reserved_data_space_noquota(
1854 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1855 fs_info->delalloc_batch);
1856 spin_lock(&inode->lock);
1857 inode->delalloc_bytes -= len;
1858 if (do_list && inode->delalloc_bytes == 0 &&
1859 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1860 &inode->runtime_flags))
1861 btrfs_del_delalloc_inode(root, inode);
1862 spin_unlock(&inode->lock);
1865 if ((state->state & EXTENT_DELALLOC_NEW) &&
1866 (*bits & EXTENT_DELALLOC_NEW)) {
1867 spin_lock(&inode->lock);
1868 ASSERT(inode->new_delalloc_bytes >= len);
1869 inode->new_delalloc_bytes -= len;
1870 spin_unlock(&inode->lock);
1875 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1876 * we don't create bios that span stripes or chunks
1878 * return 1 if page cannot be merged to bio
1879 * return 0 if page can be merged to bio
1880 * return error otherwise
1882 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1883 size_t size, struct bio *bio,
1884 unsigned long bio_flags)
1886 struct inode *inode = page->mapping->host;
1887 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1888 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
1893 if (bio_flags & EXTENT_BIO_COMPRESSED)
1896 length = bio->bi_iter.bi_size;
1897 map_length = length;
1898 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1902 if (map_length < length + size)
1908 * in order to insert checksums into the metadata in large chunks,
1909 * we wait until bio submission time. All the pages in the bio are
1910 * checksummed and sums are attached onto the ordered extent record.
1912 * At IO completion time the cums attached on the ordered extent record
1913 * are inserted into the btree
1915 static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
1916 int mirror_num, unsigned long bio_flags,
1919 struct inode *inode = private_data;
1920 blk_status_t ret = 0;
1922 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
1923 BUG_ON(ret); /* -ENOMEM */
1928 * in order to insert checksums into the metadata in large chunks,
1929 * we wait until bio submission time. All the pages in the bio are
1930 * checksummed and sums are attached onto the ordered extent record.
1932 * At IO completion time the cums attached on the ordered extent record
1933 * are inserted into the btree
1935 static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
1936 int mirror_num, unsigned long bio_flags,
1939 struct inode *inode = private_data;
1940 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1943 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
1945 bio->bi_status = ret;
1952 * extent_io.c submission hook. This does the right thing for csum calculation
1953 * on write, or reading the csums from the tree before a read.
1955 * Rules about async/sync submit,
1956 * a) read: sync submit
1958 * b) write without checksum: sync submit
1960 * c) write with checksum:
1961 * c-1) if bio is issued by fsync: sync submit
1962 * (sync_writers != 0)
1964 * c-2) if root is reloc root: sync submit
1965 * (only in case of buffered IO)
1967 * c-3) otherwise: async submit
1969 static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
1970 int mirror_num, unsigned long bio_flags,
1973 struct inode *inode = private_data;
1974 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1975 struct btrfs_root *root = BTRFS_I(inode)->root;
1976 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
1977 blk_status_t ret = 0;
1979 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
1981 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1983 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
1984 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
1986 if (bio_op(bio) != REQ_OP_WRITE) {
1987 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
1991 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1992 ret = btrfs_submit_compressed_read(inode, bio,
1996 } else if (!skip_sum) {
1997 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
2002 } else if (async && !skip_sum) {
2003 /* csum items have already been cloned */
2004 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2006 /* we're doing a write, do the async checksumming */
2007 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2009 __btrfs_submit_bio_start,
2010 __btrfs_submit_bio_done);
2012 } else if (!skip_sum) {
2013 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
2019 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
2023 bio->bi_status = ret;
2030 * given a list of ordered sums record them in the inode. This happens
2031 * at IO completion time based on sums calculated at bio submission time.
2033 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
2034 struct inode *inode, struct list_head *list)
2036 struct btrfs_ordered_sum *sum;
2038 list_for_each_entry(sum, list, list) {
2039 trans->adding_csums = 1;
2040 btrfs_csum_file_blocks(trans,
2041 BTRFS_I(inode)->root->fs_info->csum_root, sum);
2042 trans->adding_csums = 0;
2047 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
2048 unsigned int extra_bits,
2049 struct extent_state **cached_state, int dedupe)
2051 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
2052 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
2053 extra_bits, cached_state);
2056 /* see btrfs_writepage_start_hook for details on why this is required */
2057 struct btrfs_writepage_fixup {
2059 struct btrfs_work work;
2062 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
2064 struct btrfs_writepage_fixup *fixup;
2065 struct btrfs_ordered_extent *ordered;
2066 struct extent_state *cached_state = NULL;
2067 struct extent_changeset *data_reserved = NULL;
2069 struct inode *inode;
2074 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2078 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2079 ClearPageChecked(page);
2083 inode = page->mapping->host;
2084 page_start = page_offset(page);
2085 page_end = page_offset(page) + PAGE_SIZE - 1;
2087 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
2090 /* already ordered? We're done */
2091 if (PagePrivate2(page))
2094 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
2097 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2098 page_end, &cached_state, GFP_NOFS);
2100 btrfs_start_ordered_extent(inode, ordered, 1);
2101 btrfs_put_ordered_extent(ordered);
2105 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2108 mapping_set_error(page->mapping, ret);
2109 end_extent_writepage(page, ret, page_start, page_end);
2110 ClearPageChecked(page);
2114 btrfs_set_extent_delalloc(inode, page_start, page_end, 0, &cached_state,
2116 ClearPageChecked(page);
2117 set_page_dirty(page);
2118 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
2120 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2121 &cached_state, GFP_NOFS);
2126 extent_changeset_free(data_reserved);
2130 * There are a few paths in the higher layers of the kernel that directly
2131 * set the page dirty bit without asking the filesystem if it is a
2132 * good idea. This causes problems because we want to make sure COW
2133 * properly happens and the data=ordered rules are followed.
2135 * In our case any range that doesn't have the ORDERED bit set
2136 * hasn't been properly setup for IO. We kick off an async process
2137 * to fix it up. The async helper will wait for ordered extents, set
2138 * the delalloc bit and make it safe to write the page.
2140 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
2142 struct inode *inode = page->mapping->host;
2143 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2144 struct btrfs_writepage_fixup *fixup;
2146 /* this page is properly in the ordered list */
2147 if (TestClearPagePrivate2(page))
2150 if (PageChecked(page))
2153 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2157 SetPageChecked(page);
2159 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2160 btrfs_writepage_fixup_worker, NULL, NULL);
2162 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2166 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2167 struct inode *inode, u64 file_pos,
2168 u64 disk_bytenr, u64 disk_num_bytes,
2169 u64 num_bytes, u64 ram_bytes,
2170 u8 compression, u8 encryption,
2171 u16 other_encoding, int extent_type)
2173 struct btrfs_root *root = BTRFS_I(inode)->root;
2174 struct btrfs_file_extent_item *fi;
2175 struct btrfs_path *path;
2176 struct extent_buffer *leaf;
2177 struct btrfs_key ins;
2179 int extent_inserted = 0;
2182 path = btrfs_alloc_path();
2187 * we may be replacing one extent in the tree with another.
2188 * The new extent is pinned in the extent map, and we don't want
2189 * to drop it from the cache until it is completely in the btree.
2191 * So, tell btrfs_drop_extents to leave this extent in the cache.
2192 * the caller is expected to unpin it and allow it to be merged
2195 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2196 file_pos + num_bytes, NULL, 0,
2197 1, sizeof(*fi), &extent_inserted);
2201 if (!extent_inserted) {
2202 ins.objectid = btrfs_ino(BTRFS_I(inode));
2203 ins.offset = file_pos;
2204 ins.type = BTRFS_EXTENT_DATA_KEY;
2206 path->leave_spinning = 1;
2207 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2212 leaf = path->nodes[0];
2213 fi = btrfs_item_ptr(leaf, path->slots[0],
2214 struct btrfs_file_extent_item);
2215 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2216 btrfs_set_file_extent_type(leaf, fi, extent_type);
2217 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2218 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2219 btrfs_set_file_extent_offset(leaf, fi, 0);
2220 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2221 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2222 btrfs_set_file_extent_compression(leaf, fi, compression);
2223 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2224 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
2226 btrfs_mark_buffer_dirty(leaf);
2227 btrfs_release_path(path);
2229 inode_add_bytes(inode, num_bytes);
2231 ins.objectid = disk_bytenr;
2232 ins.offset = disk_num_bytes;
2233 ins.type = BTRFS_EXTENT_ITEM_KEY;
2236 * Release the reserved range from inode dirty range map, as it is
2237 * already moved into delayed_ref_head
2239 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2243 ret = btrfs_alloc_reserved_file_extent(trans, root,
2244 btrfs_ino(BTRFS_I(inode)),
2245 file_pos, qg_released, &ins);
2247 btrfs_free_path(path);
2252 /* snapshot-aware defrag */
2253 struct sa_defrag_extent_backref {
2254 struct rb_node node;
2255 struct old_sa_defrag_extent *old;
2264 struct old_sa_defrag_extent {
2265 struct list_head list;
2266 struct new_sa_defrag_extent *new;
2275 struct new_sa_defrag_extent {
2276 struct rb_root root;
2277 struct list_head head;
2278 struct btrfs_path *path;
2279 struct inode *inode;
2287 static int backref_comp(struct sa_defrag_extent_backref *b1,
2288 struct sa_defrag_extent_backref *b2)
2290 if (b1->root_id < b2->root_id)
2292 else if (b1->root_id > b2->root_id)
2295 if (b1->inum < b2->inum)
2297 else if (b1->inum > b2->inum)
2300 if (b1->file_pos < b2->file_pos)
2302 else if (b1->file_pos > b2->file_pos)
2306 * [------------------------------] ===> (a range of space)
2307 * |<--->| |<---->| =============> (fs/file tree A)
2308 * |<---------------------------->| ===> (fs/file tree B)
2310 * A range of space can refer to two file extents in one tree while
2311 * refer to only one file extent in another tree.
2313 * So we may process a disk offset more than one time(two extents in A)
2314 * and locate at the same extent(one extent in B), then insert two same
2315 * backrefs(both refer to the extent in B).
2320 static void backref_insert(struct rb_root *root,
2321 struct sa_defrag_extent_backref *backref)
2323 struct rb_node **p = &root->rb_node;
2324 struct rb_node *parent = NULL;
2325 struct sa_defrag_extent_backref *entry;
2330 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2332 ret = backref_comp(backref, entry);
2336 p = &(*p)->rb_right;
2339 rb_link_node(&backref->node, parent, p);
2340 rb_insert_color(&backref->node, root);
2344 * Note the backref might has changed, and in this case we just return 0.
2346 static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2349 struct btrfs_file_extent_item *extent;
2350 struct old_sa_defrag_extent *old = ctx;
2351 struct new_sa_defrag_extent *new = old->new;
2352 struct btrfs_path *path = new->path;
2353 struct btrfs_key key;
2354 struct btrfs_root *root;
2355 struct sa_defrag_extent_backref *backref;
2356 struct extent_buffer *leaf;
2357 struct inode *inode = new->inode;
2358 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2364 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2365 inum == btrfs_ino(BTRFS_I(inode)))
2368 key.objectid = root_id;
2369 key.type = BTRFS_ROOT_ITEM_KEY;
2370 key.offset = (u64)-1;
2372 root = btrfs_read_fs_root_no_name(fs_info, &key);
2374 if (PTR_ERR(root) == -ENOENT)
2377 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
2378 inum, offset, root_id);
2379 return PTR_ERR(root);
2382 key.objectid = inum;
2383 key.type = BTRFS_EXTENT_DATA_KEY;
2384 if (offset > (u64)-1 << 32)
2387 key.offset = offset;
2389 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2390 if (WARN_ON(ret < 0))
2397 leaf = path->nodes[0];
2398 slot = path->slots[0];
2400 if (slot >= btrfs_header_nritems(leaf)) {
2401 ret = btrfs_next_leaf(root, path);
2404 } else if (ret > 0) {
2413 btrfs_item_key_to_cpu(leaf, &key, slot);
2415 if (key.objectid > inum)
2418 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2421 extent = btrfs_item_ptr(leaf, slot,
2422 struct btrfs_file_extent_item);
2424 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2428 * 'offset' refers to the exact key.offset,
2429 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2430 * (key.offset - extent_offset).
2432 if (key.offset != offset)
2435 extent_offset = btrfs_file_extent_offset(leaf, extent);
2436 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2438 if (extent_offset >= old->extent_offset + old->offset +
2439 old->len || extent_offset + num_bytes <=
2440 old->extent_offset + old->offset)
2445 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2451 backref->root_id = root_id;
2452 backref->inum = inum;
2453 backref->file_pos = offset;
2454 backref->num_bytes = num_bytes;
2455 backref->extent_offset = extent_offset;
2456 backref->generation = btrfs_file_extent_generation(leaf, extent);
2458 backref_insert(&new->root, backref);
2461 btrfs_release_path(path);
2466 static noinline bool record_extent_backrefs(struct btrfs_path *path,
2467 struct new_sa_defrag_extent *new)
2469 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2470 struct old_sa_defrag_extent *old, *tmp;
2475 list_for_each_entry_safe(old, tmp, &new->head, list) {
2476 ret = iterate_inodes_from_logical(old->bytenr +
2477 old->extent_offset, fs_info,
2478 path, record_one_backref,
2480 if (ret < 0 && ret != -ENOENT)
2483 /* no backref to be processed for this extent */
2485 list_del(&old->list);
2490 if (list_empty(&new->head))
2496 static int relink_is_mergable(struct extent_buffer *leaf,
2497 struct btrfs_file_extent_item *fi,
2498 struct new_sa_defrag_extent *new)
2500 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
2503 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2506 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2509 if (btrfs_file_extent_encryption(leaf, fi) ||
2510 btrfs_file_extent_other_encoding(leaf, fi))
2517 * Note the backref might has changed, and in this case we just return 0.
2519 static noinline int relink_extent_backref(struct btrfs_path *path,
2520 struct sa_defrag_extent_backref *prev,
2521 struct sa_defrag_extent_backref *backref)
2523 struct btrfs_file_extent_item *extent;
2524 struct btrfs_file_extent_item *item;
2525 struct btrfs_ordered_extent *ordered;
2526 struct btrfs_trans_handle *trans;
2527 struct btrfs_root *root;
2528 struct btrfs_key key;
2529 struct extent_buffer *leaf;
2530 struct old_sa_defrag_extent *old = backref->old;
2531 struct new_sa_defrag_extent *new = old->new;
2532 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2533 struct inode *inode;
2534 struct extent_state *cached = NULL;
2543 if (prev && prev->root_id == backref->root_id &&
2544 prev->inum == backref->inum &&
2545 prev->file_pos + prev->num_bytes == backref->file_pos)
2548 /* step 1: get root */
2549 key.objectid = backref->root_id;
2550 key.type = BTRFS_ROOT_ITEM_KEY;
2551 key.offset = (u64)-1;
2553 index = srcu_read_lock(&fs_info->subvol_srcu);
2555 root = btrfs_read_fs_root_no_name(fs_info, &key);
2557 srcu_read_unlock(&fs_info->subvol_srcu, index);
2558 if (PTR_ERR(root) == -ENOENT)
2560 return PTR_ERR(root);
2563 if (btrfs_root_readonly(root)) {
2564 srcu_read_unlock(&fs_info->subvol_srcu, index);
2568 /* step 2: get inode */
2569 key.objectid = backref->inum;
2570 key.type = BTRFS_INODE_ITEM_KEY;
2573 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2574 if (IS_ERR(inode)) {
2575 srcu_read_unlock(&fs_info->subvol_srcu, index);
2579 srcu_read_unlock(&fs_info->subvol_srcu, index);
2581 /* step 3: relink backref */
2582 lock_start = backref->file_pos;
2583 lock_end = backref->file_pos + backref->num_bytes - 1;
2584 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2587 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2589 btrfs_put_ordered_extent(ordered);
2593 trans = btrfs_join_transaction(root);
2594 if (IS_ERR(trans)) {
2595 ret = PTR_ERR(trans);
2599 key.objectid = backref->inum;
2600 key.type = BTRFS_EXTENT_DATA_KEY;
2601 key.offset = backref->file_pos;
2603 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2606 } else if (ret > 0) {
2611 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2612 struct btrfs_file_extent_item);
2614 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2615 backref->generation)
2618 btrfs_release_path(path);
2620 start = backref->file_pos;
2621 if (backref->extent_offset < old->extent_offset + old->offset)
2622 start += old->extent_offset + old->offset -
2623 backref->extent_offset;
2625 len = min(backref->extent_offset + backref->num_bytes,
2626 old->extent_offset + old->offset + old->len);
2627 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2629 ret = btrfs_drop_extents(trans, root, inode, start,
2634 key.objectid = btrfs_ino(BTRFS_I(inode));
2635 key.type = BTRFS_EXTENT_DATA_KEY;
2638 path->leave_spinning = 1;
2640 struct btrfs_file_extent_item *fi;
2642 struct btrfs_key found_key;
2644 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2649 leaf = path->nodes[0];
2650 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2652 fi = btrfs_item_ptr(leaf, path->slots[0],
2653 struct btrfs_file_extent_item);
2654 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2656 if (extent_len + found_key.offset == start &&
2657 relink_is_mergable(leaf, fi, new)) {
2658 btrfs_set_file_extent_num_bytes(leaf, fi,
2660 btrfs_mark_buffer_dirty(leaf);
2661 inode_add_bytes(inode, len);
2667 btrfs_release_path(path);
2672 ret = btrfs_insert_empty_item(trans, root, path, &key,
2675 btrfs_abort_transaction(trans, ret);
2679 leaf = path->nodes[0];
2680 item = btrfs_item_ptr(leaf, path->slots[0],
2681 struct btrfs_file_extent_item);
2682 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2683 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2684 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2685 btrfs_set_file_extent_num_bytes(leaf, item, len);
2686 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2687 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2688 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2689 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2690 btrfs_set_file_extent_encryption(leaf, item, 0);
2691 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2693 btrfs_mark_buffer_dirty(leaf);
2694 inode_add_bytes(inode, len);
2695 btrfs_release_path(path);
2697 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2699 backref->root_id, backref->inum,
2700 new->file_pos); /* start - extent_offset */
2702 btrfs_abort_transaction(trans, ret);
2708 btrfs_release_path(path);
2709 path->leave_spinning = 0;
2710 btrfs_end_transaction(trans);
2712 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2718 static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2720 struct old_sa_defrag_extent *old, *tmp;
2725 list_for_each_entry_safe(old, tmp, &new->head, list) {
2731 static void relink_file_extents(struct new_sa_defrag_extent *new)
2733 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2734 struct btrfs_path *path;
2735 struct sa_defrag_extent_backref *backref;
2736 struct sa_defrag_extent_backref *prev = NULL;
2737 struct inode *inode;
2738 struct btrfs_root *root;
2739 struct rb_node *node;
2743 root = BTRFS_I(inode)->root;
2745 path = btrfs_alloc_path();
2749 if (!record_extent_backrefs(path, new)) {
2750 btrfs_free_path(path);
2753 btrfs_release_path(path);
2756 node = rb_first(&new->root);
2759 rb_erase(node, &new->root);
2761 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2763 ret = relink_extent_backref(path, prev, backref);
2776 btrfs_free_path(path);
2778 free_sa_defrag_extent(new);
2780 atomic_dec(&fs_info->defrag_running);
2781 wake_up(&fs_info->transaction_wait);
2784 static struct new_sa_defrag_extent *
2785 record_old_file_extents(struct inode *inode,
2786 struct btrfs_ordered_extent *ordered)
2788 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2789 struct btrfs_root *root = BTRFS_I(inode)->root;
2790 struct btrfs_path *path;
2791 struct btrfs_key key;
2792 struct old_sa_defrag_extent *old;
2793 struct new_sa_defrag_extent *new;
2796 new = kmalloc(sizeof(*new), GFP_NOFS);
2801 new->file_pos = ordered->file_offset;
2802 new->len = ordered->len;
2803 new->bytenr = ordered->start;
2804 new->disk_len = ordered->disk_len;
2805 new->compress_type = ordered->compress_type;
2806 new->root = RB_ROOT;
2807 INIT_LIST_HEAD(&new->head);
2809 path = btrfs_alloc_path();
2813 key.objectid = btrfs_ino(BTRFS_I(inode));
2814 key.type = BTRFS_EXTENT_DATA_KEY;
2815 key.offset = new->file_pos;
2817 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2820 if (ret > 0 && path->slots[0] > 0)
2823 /* find out all the old extents for the file range */
2825 struct btrfs_file_extent_item *extent;
2826 struct extent_buffer *l;
2835 slot = path->slots[0];
2837 if (slot >= btrfs_header_nritems(l)) {
2838 ret = btrfs_next_leaf(root, path);
2846 btrfs_item_key_to_cpu(l, &key, slot);
2848 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
2850 if (key.type != BTRFS_EXTENT_DATA_KEY)
2852 if (key.offset >= new->file_pos + new->len)
2855 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2857 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2858 if (key.offset + num_bytes < new->file_pos)
2861 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2865 extent_offset = btrfs_file_extent_offset(l, extent);
2867 old = kmalloc(sizeof(*old), GFP_NOFS);
2871 offset = max(new->file_pos, key.offset);
2872 end = min(new->file_pos + new->len, key.offset + num_bytes);
2874 old->bytenr = disk_bytenr;
2875 old->extent_offset = extent_offset;
2876 old->offset = offset - key.offset;
2877 old->len = end - offset;
2880 list_add_tail(&old->list, &new->head);
2886 btrfs_free_path(path);
2887 atomic_inc(&fs_info->defrag_running);
2892 btrfs_free_path(path);
2894 free_sa_defrag_extent(new);
2898 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
2901 struct btrfs_block_group_cache *cache;
2903 cache = btrfs_lookup_block_group(fs_info, start);
2906 spin_lock(&cache->lock);
2907 cache->delalloc_bytes -= len;
2908 spin_unlock(&cache->lock);
2910 btrfs_put_block_group(cache);
2913 /* as ordered data IO finishes, this gets called so we can finish
2914 * an ordered extent if the range of bytes in the file it covers are
2917 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2919 struct inode *inode = ordered_extent->inode;
2920 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2921 struct btrfs_root *root = BTRFS_I(inode)->root;
2922 struct btrfs_trans_handle *trans = NULL;
2923 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2924 struct extent_state *cached_state = NULL;
2925 struct new_sa_defrag_extent *new = NULL;
2926 int compress_type = 0;
2928 u64 logical_len = ordered_extent->len;
2930 bool truncated = false;
2931 bool range_locked = false;
2932 bool clear_new_delalloc_bytes = false;
2934 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2935 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2936 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2937 clear_new_delalloc_bytes = true;
2939 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
2941 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2946 btrfs_free_io_failure_record(BTRFS_I(inode),
2947 ordered_extent->file_offset,
2948 ordered_extent->file_offset +
2949 ordered_extent->len - 1);
2951 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2953 logical_len = ordered_extent->truncated_len;
2954 /* Truncated the entire extent, don't bother adding */
2959 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
2960 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
2963 * For mwrite(mmap + memset to write) case, we still reserve
2964 * space for NOCOW range.
2965 * As NOCOW won't cause a new delayed ref, just free the space
2967 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
2968 ordered_extent->len);
2969 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2971 trans = btrfs_join_transaction_nolock(root);
2973 trans = btrfs_join_transaction(root);
2974 if (IS_ERR(trans)) {
2975 ret = PTR_ERR(trans);
2979 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
2980 ret = btrfs_update_inode_fallback(trans, root, inode);
2981 if (ret) /* -ENOMEM or corruption */
2982 btrfs_abort_transaction(trans, ret);
2986 range_locked = true;
2987 lock_extent_bits(io_tree, ordered_extent->file_offset,
2988 ordered_extent->file_offset + ordered_extent->len - 1,
2991 ret = test_range_bit(io_tree, ordered_extent->file_offset,
2992 ordered_extent->file_offset + ordered_extent->len - 1,
2993 EXTENT_DEFRAG, 0, cached_state);
2995 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2996 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
2997 /* the inode is shared */
2998 new = record_old_file_extents(inode, ordered_extent);
3000 clear_extent_bit(io_tree, ordered_extent->file_offset,
3001 ordered_extent->file_offset + ordered_extent->len - 1,
3002 EXTENT_DEFRAG, 0, 0, &cached_state);
3006 trans = btrfs_join_transaction_nolock(root);
3008 trans = btrfs_join_transaction(root);
3009 if (IS_ERR(trans)) {
3010 ret = PTR_ERR(trans);
3015 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
3017 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
3018 compress_type = ordered_extent->compress_type;
3019 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3020 BUG_ON(compress_type);
3021 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3022 ordered_extent->len);
3023 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
3024 ordered_extent->file_offset,
3025 ordered_extent->file_offset +
3028 BUG_ON(root == fs_info->tree_root);
3029 ret = insert_reserved_file_extent(trans, inode,
3030 ordered_extent->file_offset,
3031 ordered_extent->start,
3032 ordered_extent->disk_len,
3033 logical_len, logical_len,
3034 compress_type, 0, 0,
3035 BTRFS_FILE_EXTENT_REG);
3037 btrfs_release_delalloc_bytes(fs_info,
3038 ordered_extent->start,
3039 ordered_extent->disk_len);
3041 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3042 ordered_extent->file_offset, ordered_extent->len,
3045 btrfs_abort_transaction(trans, ret);
3049 add_pending_csums(trans, inode, &ordered_extent->list);
3051 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3052 ret = btrfs_update_inode_fallback(trans, root, inode);
3053 if (ret) { /* -ENOMEM or corruption */
3054 btrfs_abort_transaction(trans, ret);
3059 if (range_locked || clear_new_delalloc_bytes) {
3060 unsigned int clear_bits = 0;
3063 clear_bits |= EXTENT_LOCKED;
3064 if (clear_new_delalloc_bytes)
3065 clear_bits |= EXTENT_DELALLOC_NEW;
3066 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3067 ordered_extent->file_offset,
3068 ordered_extent->file_offset +
3069 ordered_extent->len - 1,
3071 (clear_bits & EXTENT_LOCKED) ? 1 : 0,