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>
46 #include <linux/iversion.h>
49 #include "transaction.h"
50 #include "btrfs_inode.h"
51 #include "print-tree.h"
52 #include "ordered-data.h"
56 #include "compression.h"
58 #include "free-space-cache.h"
59 #include "inode-map.h"
66 struct btrfs_iget_args {
67 struct btrfs_key *location;
68 struct btrfs_root *root;
71 struct btrfs_dio_data {
73 u64 unsubmitted_oe_range_start;
74 u64 unsubmitted_oe_range_end;
78 static const struct inode_operations btrfs_dir_inode_operations;
79 static const struct inode_operations btrfs_symlink_inode_operations;
80 static const struct inode_operations btrfs_dir_ro_inode_operations;
81 static const struct inode_operations btrfs_special_inode_operations;
82 static const struct inode_operations btrfs_file_inode_operations;
83 static const struct address_space_operations btrfs_aops;
84 static const struct address_space_operations btrfs_symlink_aops;
85 static const struct file_operations btrfs_dir_file_operations;
86 static const struct extent_io_ops btrfs_extent_io_ops;
88 static struct kmem_cache *btrfs_inode_cachep;
89 struct kmem_cache *btrfs_trans_handle_cachep;
90 struct kmem_cache *btrfs_path_cachep;
91 struct kmem_cache *btrfs_free_space_cachep;
94 static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
95 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
96 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
97 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
98 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
99 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
100 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
101 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
104 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
105 static int btrfs_truncate(struct inode *inode);
106 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
107 static noinline int cow_file_range(struct inode *inode,
108 struct page *locked_page,
109 u64 start, u64 end, u64 delalloc_end,
110 int *page_started, unsigned long *nr_written,
111 int unlock, struct btrfs_dedupe_hash *hash);
112 static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 u64 orig_start, u64 block_start,
114 u64 block_len, u64 orig_block_len,
115 u64 ram_bytes, int compress_type,
118 static void __endio_write_update_ordered(struct inode *inode,
119 const u64 offset, const u64 bytes,
120 const bool uptodate);
123 * Cleanup all submitted ordered extents in specified range to handle errors
124 * from the fill_dellaloc() callback.
126 * NOTE: caller must ensure that when an error happens, it can not call
127 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
128 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
129 * to be released, which we want to happen only when finishing the ordered
130 * extent (btrfs_finish_ordered_io()). Also note that the caller of the
131 * fill_delalloc() callback already does proper cleanup for the first page of
132 * the range, that is, it invokes the callback writepage_end_io_hook() for the
133 * range of the first page.
135 static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
139 unsigned long index = offset >> PAGE_SHIFT;
140 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
143 while (index <= end_index) {
144 page = find_get_page(inode->i_mapping, index);
148 ClearPagePrivate2(page);
151 return __endio_write_update_ordered(inode, offset + PAGE_SIZE,
152 bytes - PAGE_SIZE, false);
155 static int btrfs_dirty_inode(struct inode *inode);
157 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
158 void btrfs_test_inode_set_ops(struct inode *inode)
160 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
164 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
165 struct inode *inode, struct inode *dir,
166 const struct qstr *qstr)
170 err = btrfs_init_acl(trans, inode, dir);
172 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
177 * this does all the hard work for inserting an inline extent into
178 * the btree. The caller should have done a btrfs_drop_extents so that
179 * no overlapping inline items exist in the btree
181 static int insert_inline_extent(struct btrfs_trans_handle *trans,
182 struct btrfs_path *path, int extent_inserted,
183 struct btrfs_root *root, struct inode *inode,
184 u64 start, size_t size, size_t compressed_size,
186 struct page **compressed_pages)
188 struct extent_buffer *leaf;
189 struct page *page = NULL;
192 struct btrfs_file_extent_item *ei;
194 size_t cur_size = size;
195 unsigned long offset;
197 if (compressed_size && compressed_pages)
198 cur_size = compressed_size;
200 inode_add_bytes(inode, size);
202 if (!extent_inserted) {
203 struct btrfs_key key;
206 key.objectid = btrfs_ino(BTRFS_I(inode));
208 key.type = BTRFS_EXTENT_DATA_KEY;
210 datasize = btrfs_file_extent_calc_inline_size(cur_size);
211 path->leave_spinning = 1;
212 ret = btrfs_insert_empty_item(trans, root, path, &key,
217 leaf = path->nodes[0];
218 ei = btrfs_item_ptr(leaf, path->slots[0],
219 struct btrfs_file_extent_item);
220 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
221 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
222 btrfs_set_file_extent_encryption(leaf, ei, 0);
223 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
224 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
225 ptr = btrfs_file_extent_inline_start(ei);
227 if (compress_type != BTRFS_COMPRESS_NONE) {
230 while (compressed_size > 0) {
231 cpage = compressed_pages[i];
232 cur_size = min_t(unsigned long, compressed_size,
235 kaddr = kmap_atomic(cpage);
236 write_extent_buffer(leaf, kaddr, ptr, cur_size);
237 kunmap_atomic(kaddr);
241 compressed_size -= cur_size;
243 btrfs_set_file_extent_compression(leaf, ei,
246 page = find_get_page(inode->i_mapping,
247 start >> PAGE_SHIFT);
248 btrfs_set_file_extent_compression(leaf, ei, 0);
249 kaddr = kmap_atomic(page);
250 offset = start & (PAGE_SIZE - 1);
251 write_extent_buffer(leaf, kaddr + offset, ptr, size);
252 kunmap_atomic(kaddr);
255 btrfs_mark_buffer_dirty(leaf);
256 btrfs_release_path(path);
259 * we're an inline extent, so nobody can
260 * extend the file past i_size without locking
261 * a page we already have locked.
263 * We must do any isize and inode updates
264 * before we unlock the pages. Otherwise we
265 * could end up racing with unlink.
267 BTRFS_I(inode)->disk_i_size = inode->i_size;
268 ret = btrfs_update_inode(trans, root, inode);
276 * conditionally insert an inline extent into the file. This
277 * does the checks required to make sure the data is small enough
278 * to fit as an inline extent.
280 static noinline int cow_file_range_inline(struct btrfs_root *root,
281 struct inode *inode, u64 start,
282 u64 end, size_t compressed_size,
284 struct page **compressed_pages)
286 struct btrfs_fs_info *fs_info = root->fs_info;
287 struct btrfs_trans_handle *trans;
288 u64 isize = i_size_read(inode);
289 u64 actual_end = min(end + 1, isize);
290 u64 inline_len = actual_end - start;
291 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
292 u64 data_len = inline_len;
294 struct btrfs_path *path;
295 int extent_inserted = 0;
296 u32 extent_item_size;
299 data_len = compressed_size;
302 actual_end > fs_info->sectorsize ||
303 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
305 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
307 data_len > fs_info->max_inline) {
311 path = btrfs_alloc_path();
315 trans = btrfs_join_transaction(root);
317 btrfs_free_path(path);
318 return PTR_ERR(trans);
320 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
322 if (compressed_size && compressed_pages)
323 extent_item_size = btrfs_file_extent_calc_inline_size(
326 extent_item_size = btrfs_file_extent_calc_inline_size(
329 ret = __btrfs_drop_extents(trans, root, inode, path,
330 start, aligned_end, NULL,
331 1, 1, extent_item_size, &extent_inserted);
333 btrfs_abort_transaction(trans, ret);
337 if (isize > actual_end)
338 inline_len = min_t(u64, isize, actual_end);
339 ret = insert_inline_extent(trans, path, extent_inserted,
341 inline_len, compressed_size,
342 compress_type, compressed_pages);
343 if (ret && ret != -ENOSPC) {
344 btrfs_abort_transaction(trans, ret);
346 } else if (ret == -ENOSPC) {
351 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
352 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
355 * Don't forget to free the reserved space, as for inlined extent
356 * it won't count as data extent, free them directly here.
357 * And at reserve time, it's always aligned to page size, so
358 * just free one page here.
360 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
361 btrfs_free_path(path);
362 btrfs_end_transaction(trans);
366 struct async_extent {
371 unsigned long nr_pages;
373 struct list_head list;
378 struct btrfs_root *root;
379 struct page *locked_page;
382 unsigned int write_flags;
383 struct list_head extents;
384 struct btrfs_work work;
387 static noinline int add_async_extent(struct async_cow *cow,
388 u64 start, u64 ram_size,
391 unsigned long nr_pages,
394 struct async_extent *async_extent;
396 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
397 BUG_ON(!async_extent); /* -ENOMEM */
398 async_extent->start = start;
399 async_extent->ram_size = ram_size;
400 async_extent->compressed_size = compressed_size;
401 async_extent->pages = pages;
402 async_extent->nr_pages = nr_pages;
403 async_extent->compress_type = compress_type;
404 list_add_tail(&async_extent->list, &cow->extents);
408 static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
410 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
413 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
416 if (BTRFS_I(inode)->defrag_compress)
418 /* bad compression ratios */
419 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
421 if (btrfs_test_opt(fs_info, COMPRESS) ||
422 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
423 BTRFS_I(inode)->prop_compress)
424 return btrfs_compress_heuristic(inode, start, end);
428 static inline void inode_should_defrag(struct btrfs_inode *inode,
429 u64 start, u64 end, u64 num_bytes, u64 small_write)
431 /* If this is a small write inside eof, kick off a defrag */
432 if (num_bytes < small_write &&
433 (start > 0 || end + 1 < inode->disk_i_size))
434 btrfs_add_inode_defrag(NULL, inode);
438 * we create compressed extents in two phases. The first
439 * phase compresses a range of pages that have already been
440 * locked (both pages and state bits are locked).
442 * This is done inside an ordered work queue, and the compression
443 * is spread across many cpus. The actual IO submission is step
444 * two, and the ordered work queue takes care of making sure that
445 * happens in the same order things were put onto the queue by
446 * writepages and friends.
448 * If this code finds it can't get good compression, it puts an
449 * entry onto the work queue to write the uncompressed bytes. This
450 * makes sure that both compressed inodes and uncompressed inodes
451 * are written in the same order that the flusher thread sent them
454 static noinline void compress_file_range(struct inode *inode,
455 struct page *locked_page,
457 struct async_cow *async_cow,
460 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
461 struct btrfs_root *root = BTRFS_I(inode)->root;
462 u64 blocksize = fs_info->sectorsize;
464 u64 isize = i_size_read(inode);
466 struct page **pages = NULL;
467 unsigned long nr_pages;
468 unsigned long total_compressed = 0;
469 unsigned long total_in = 0;
472 int compress_type = fs_info->compress_type;
475 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
478 actual_end = min_t(u64, isize, end + 1);
481 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
482 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
483 nr_pages = min_t(unsigned long, nr_pages,
484 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
487 * we don't want to send crud past the end of i_size through
488 * compression, that's just a waste of CPU time. So, if the
489 * end of the file is before the start of our current
490 * requested range of bytes, we bail out to the uncompressed
491 * cleanup code that can deal with all of this.
493 * It isn't really the fastest way to fix things, but this is a
494 * very uncommon corner.
496 if (actual_end <= start)
497 goto cleanup_and_bail_uncompressed;
499 total_compressed = actual_end - start;
502 * skip compression for a small file range(<=blocksize) that
503 * isn't an inline extent, since it doesn't save disk space at all.
505 if (total_compressed <= blocksize &&
506 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
507 goto cleanup_and_bail_uncompressed;
509 total_compressed = min_t(unsigned long, total_compressed,
510 BTRFS_MAX_UNCOMPRESSED);
515 * we do compression for mount -o compress and when the
516 * inode has not been flagged as nocompress. This flag can
517 * change at any time if we discover bad compression ratios.
519 if (inode_need_compress(inode, start, end)) {
521 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
523 /* just bail out to the uncompressed code */
527 if (BTRFS_I(inode)->defrag_compress)
528 compress_type = BTRFS_I(inode)->defrag_compress;
529 else if (BTRFS_I(inode)->prop_compress)
530 compress_type = BTRFS_I(inode)->prop_compress;
533 * we need to call clear_page_dirty_for_io on each
534 * page in the range. Otherwise applications with the file
535 * mmap'd can wander in and change the page contents while
536 * we are compressing them.
538 * If the compression fails for any reason, we set the pages
539 * dirty again later on.
541 * Note that the remaining part is redirtied, the start pointer
542 * has moved, the end is the original one.
545 extent_range_clear_dirty_for_io(inode, start, end);
549 /* Compression level is applied here and only here */
550 ret = btrfs_compress_pages(
551 compress_type | (fs_info->compress_level << 4),
552 inode->i_mapping, start,
559 unsigned long offset = total_compressed &
561 struct page *page = pages[nr_pages - 1];
564 /* zero the tail end of the last page, we might be
565 * sending it down to disk
568 kaddr = kmap_atomic(page);
569 memset(kaddr + offset, 0,
571 kunmap_atomic(kaddr);
578 /* lets try to make an inline extent */
579 if (ret || total_in < actual_end) {
580 /* we didn't compress the entire range, try
581 * to make an uncompressed inline extent.
583 ret = cow_file_range_inline(root, inode, start, end,
584 0, BTRFS_COMPRESS_NONE, NULL);
586 /* try making a compressed inline extent */
587 ret = cow_file_range_inline(root, inode, start, end,
589 compress_type, pages);
592 unsigned long clear_flags = EXTENT_DELALLOC |
593 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
594 EXTENT_DO_ACCOUNTING;
595 unsigned long page_error_op;
597 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
600 * inline extent creation worked or returned error,
601 * we don't need to create any more async work items.
602 * Unlock and free up our temp pages.
604 * We use DO_ACCOUNTING here because we need the
605 * delalloc_release_metadata to be done _after_ we drop
606 * our outstanding extent for clearing delalloc for this
609 extent_clear_unlock_delalloc(inode, start, end, end,
622 * we aren't doing an inline extent round the compressed size
623 * up to a block size boundary so the allocator does sane
626 total_compressed = ALIGN(total_compressed, blocksize);
629 * one last check to make sure the compression is really a
630 * win, compare the page count read with the blocks on disk,
631 * compression must free at least one sector size
633 total_in = ALIGN(total_in, PAGE_SIZE);
634 if (total_compressed + blocksize <= total_in) {
638 * The async work queues will take care of doing actual
639 * allocation on disk for these compressed pages, and
640 * will submit them to the elevator.
642 add_async_extent(async_cow, start, total_in,
643 total_compressed, pages, nr_pages,
646 if (start + total_in < end) {
657 * the compression code ran but failed to make things smaller,
658 * free any pages it allocated and our page pointer array
660 for (i = 0; i < nr_pages; i++) {
661 WARN_ON(pages[i]->mapping);
666 total_compressed = 0;
669 /* flag the file so we don't compress in the future */
670 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
671 !(BTRFS_I(inode)->prop_compress)) {
672 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
675 cleanup_and_bail_uncompressed:
677 * No compression, but we still need to write the pages in the file
678 * we've been given so far. redirty the locked page if it corresponds
679 * to our extent and set things up for the async work queue to run
680 * cow_file_range to do the normal delalloc dance.
682 if (page_offset(locked_page) >= start &&
683 page_offset(locked_page) <= end)
684 __set_page_dirty_nobuffers(locked_page);
685 /* unlocked later on in the async handlers */
688 extent_range_redirty_for_io(inode, start, end);
689 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
690 BTRFS_COMPRESS_NONE);
696 for (i = 0; i < nr_pages; i++) {
697 WARN_ON(pages[i]->mapping);
703 static void free_async_extent_pages(struct async_extent *async_extent)
707 if (!async_extent->pages)
710 for (i = 0; i < async_extent->nr_pages; i++) {
711 WARN_ON(async_extent->pages[i]->mapping);
712 put_page(async_extent->pages[i]);
714 kfree(async_extent->pages);
715 async_extent->nr_pages = 0;
716 async_extent->pages = NULL;
720 * phase two of compressed writeback. This is the ordered portion
721 * of the code, which only gets called in the order the work was
722 * queued. We walk all the async extents created by compress_file_range
723 * and send them down to the disk.
725 static noinline void submit_compressed_extents(struct inode *inode,
726 struct async_cow *async_cow)
728 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
729 struct async_extent *async_extent;
731 struct btrfs_key ins;
732 struct extent_map *em;
733 struct btrfs_root *root = BTRFS_I(inode)->root;
734 struct extent_io_tree *io_tree;
738 while (!list_empty(&async_cow->extents)) {
739 async_extent = list_entry(async_cow->extents.next,
740 struct async_extent, list);
741 list_del(&async_extent->list);
743 io_tree = &BTRFS_I(inode)->io_tree;
746 /* did the compression code fall back to uncompressed IO? */
747 if (!async_extent->pages) {
748 int page_started = 0;
749 unsigned long nr_written = 0;
751 lock_extent(io_tree, async_extent->start,
752 async_extent->start +
753 async_extent->ram_size - 1);
755 /* allocate blocks */
756 ret = cow_file_range(inode, async_cow->locked_page,
758 async_extent->start +
759 async_extent->ram_size - 1,
760 async_extent->start +
761 async_extent->ram_size - 1,
762 &page_started, &nr_written, 0,
768 * if page_started, cow_file_range inserted an
769 * inline extent and took care of all the unlocking
770 * and IO for us. Otherwise, we need to submit
771 * all those pages down to the drive.
773 if (!page_started && !ret)
774 extent_write_locked_range(inode,
776 async_extent->start +
777 async_extent->ram_size - 1,
780 unlock_page(async_cow->locked_page);
786 lock_extent(io_tree, async_extent->start,
787 async_extent->start + async_extent->ram_size - 1);
789 ret = btrfs_reserve_extent(root, async_extent->ram_size,
790 async_extent->compressed_size,
791 async_extent->compressed_size,
792 0, alloc_hint, &ins, 1, 1);
794 free_async_extent_pages(async_extent);
796 if (ret == -ENOSPC) {
797 unlock_extent(io_tree, async_extent->start,
798 async_extent->start +
799 async_extent->ram_size - 1);
802 * we need to redirty the pages if we decide to
803 * fallback to uncompressed IO, otherwise we
804 * will not submit these pages down to lower
807 extent_range_redirty_for_io(inode,
809 async_extent->start +
810 async_extent->ram_size - 1);
817 * here we're doing allocation and writeback of the
820 em = create_io_em(inode, async_extent->start,
821 async_extent->ram_size, /* len */
822 async_extent->start, /* orig_start */
823 ins.objectid, /* block_start */
824 ins.offset, /* block_len */
825 ins.offset, /* orig_block_len */
826 async_extent->ram_size, /* ram_bytes */
827 async_extent->compress_type,
828 BTRFS_ORDERED_COMPRESSED);
830 /* ret value is not necessary due to void function */
831 goto out_free_reserve;
834 ret = btrfs_add_ordered_extent_compress(inode,
837 async_extent->ram_size,
839 BTRFS_ORDERED_COMPRESSED,
840 async_extent->compress_type);
842 btrfs_drop_extent_cache(BTRFS_I(inode),
844 async_extent->start +
845 async_extent->ram_size - 1, 0);
846 goto out_free_reserve;
848 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
851 * clear dirty, set writeback and unlock the pages.
853 extent_clear_unlock_delalloc(inode, async_extent->start,
854 async_extent->start +
855 async_extent->ram_size - 1,
856 async_extent->start +
857 async_extent->ram_size - 1,
858 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
859 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
861 if (btrfs_submit_compressed_write(inode,
863 async_extent->ram_size,
865 ins.offset, async_extent->pages,
866 async_extent->nr_pages,
867 async_cow->write_flags)) {
868 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
869 struct page *p = async_extent->pages[0];
870 const u64 start = async_extent->start;
871 const u64 end = start + async_extent->ram_size - 1;
873 p->mapping = inode->i_mapping;
874 tree->ops->writepage_end_io_hook(p, start, end,
877 extent_clear_unlock_delalloc(inode, start, end, end,
881 free_async_extent_pages(async_extent);
883 alloc_hint = ins.objectid + ins.offset;
889 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
890 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
892 extent_clear_unlock_delalloc(inode, async_extent->start,
893 async_extent->start +
894 async_extent->ram_size - 1,
895 async_extent->start +
896 async_extent->ram_size - 1,
897 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
898 EXTENT_DELALLOC_NEW |
899 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
900 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
901 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
903 free_async_extent_pages(async_extent);
908 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
911 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
912 struct extent_map *em;
915 read_lock(&em_tree->lock);
916 em = search_extent_mapping(em_tree, start, num_bytes);
919 * if block start isn't an actual block number then find the
920 * first block in this inode and use that as a hint. If that
921 * block is also bogus then just don't worry about it.
923 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
925 em = search_extent_mapping(em_tree, 0, 0);
926 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
927 alloc_hint = em->block_start;
931 alloc_hint = em->block_start;
935 read_unlock(&em_tree->lock);
941 * when extent_io.c finds a delayed allocation range in the file,
942 * the call backs end up in this code. The basic idea is to
943 * allocate extents on disk for the range, and create ordered data structs
944 * in ram to track those extents.
946 * locked_page is the page that writepage had locked already. We use
947 * it to make sure we don't do extra locks or unlocks.
949 * *page_started is set to one if we unlock locked_page and do everything
950 * required to start IO on it. It may be clean and already done with
953 static noinline int cow_file_range(struct inode *inode,
954 struct page *locked_page,
955 u64 start, u64 end, u64 delalloc_end,
956 int *page_started, unsigned long *nr_written,
957 int unlock, struct btrfs_dedupe_hash *hash)
959 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
960 struct btrfs_root *root = BTRFS_I(inode)->root;
963 unsigned long ram_size;
964 u64 cur_alloc_size = 0;
965 u64 blocksize = fs_info->sectorsize;
966 struct btrfs_key ins;
967 struct extent_map *em;
969 unsigned long page_ops;
970 bool extent_reserved = false;
973 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
979 num_bytes = ALIGN(end - start + 1, blocksize);
980 num_bytes = max(blocksize, num_bytes);
981 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
983 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
986 /* lets try to make an inline extent */
987 ret = cow_file_range_inline(root, inode, start, end, 0,
988 BTRFS_COMPRESS_NONE, NULL);
991 * We use DO_ACCOUNTING here because we need the
992 * delalloc_release_metadata to be run _after_ we drop
993 * our outstanding extent for clearing delalloc for this
996 extent_clear_unlock_delalloc(inode, start, end,
998 EXTENT_LOCKED | EXTENT_DELALLOC |
999 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1000 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1001 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1002 PAGE_END_WRITEBACK);
1003 *nr_written = *nr_written +
1004 (end - start + PAGE_SIZE) / PAGE_SIZE;
1007 } else if (ret < 0) {
1012 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1013 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1014 start + num_bytes - 1, 0);
1016 while (num_bytes > 0) {
1017 cur_alloc_size = num_bytes;
1018 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
1019 fs_info->sectorsize, 0, alloc_hint,
1023 cur_alloc_size = ins.offset;
1024 extent_reserved = true;
1026 ram_size = ins.offset;
1027 em = create_io_em(inode, start, ins.offset, /* len */
1028 start, /* orig_start */
1029 ins.objectid, /* block_start */
1030 ins.offset, /* block_len */
1031 ins.offset, /* orig_block_len */
1032 ram_size, /* ram_bytes */
1033 BTRFS_COMPRESS_NONE, /* compress_type */
1034 BTRFS_ORDERED_REGULAR /* type */);
1037 free_extent_map(em);
1039 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1040 ram_size, cur_alloc_size, 0);
1042 goto out_drop_extent_cache;
1044 if (root->root_key.objectid ==
1045 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1046 ret = btrfs_reloc_clone_csums(inode, start,
1049 * Only drop cache here, and process as normal.
1051 * We must not allow extent_clear_unlock_delalloc()
1052 * at out_unlock label to free meta of this ordered
1053 * extent, as its meta should be freed by
1054 * btrfs_finish_ordered_io().
1056 * So we must continue until @start is increased to
1057 * skip current ordered extent.
1060 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1061 start + ram_size - 1, 0);
1064 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1066 /* we're not doing compressed IO, don't unlock the first
1067 * page (which the caller expects to stay locked), don't
1068 * clear any dirty bits and don't set any writeback bits
1070 * Do set the Private2 bit so we know this page was properly
1071 * setup for writepage
1073 page_ops = unlock ? PAGE_UNLOCK : 0;
1074 page_ops |= PAGE_SET_PRIVATE2;
1076 extent_clear_unlock_delalloc(inode, start,
1077 start + ram_size - 1,
1078 delalloc_end, locked_page,
1079 EXTENT_LOCKED | EXTENT_DELALLOC,
1081 if (num_bytes < cur_alloc_size)
1084 num_bytes -= cur_alloc_size;
1085 alloc_hint = ins.objectid + ins.offset;
1086 start += cur_alloc_size;
1087 extent_reserved = false;
1090 * btrfs_reloc_clone_csums() error, since start is increased
1091 * extent_clear_unlock_delalloc() at out_unlock label won't
1092 * free metadata of current ordered extent, we're OK to exit.
1100 out_drop_extent_cache:
1101 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
1103 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1104 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1106 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1107 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
1108 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1111 * If we reserved an extent for our delalloc range (or a subrange) and
1112 * failed to create the respective ordered extent, then it means that
1113 * when we reserved the extent we decremented the extent's size from
1114 * the data space_info's bytes_may_use counter and incremented the
1115 * space_info's bytes_reserved counter by the same amount. We must make
1116 * sure extent_clear_unlock_delalloc() does not try to decrement again
1117 * the data space_info's bytes_may_use counter, therefore we do not pass
1118 * it the flag EXTENT_CLEAR_DATA_RESV.
1120 if (extent_reserved) {
1121 extent_clear_unlock_delalloc(inode, start,
1122 start + cur_alloc_size,
1123 start + cur_alloc_size,
1127 start += cur_alloc_size;
1131 extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1133 clear_bits | EXTENT_CLEAR_DATA_RESV,
1139 * work queue call back to started compression on a file and pages
1141 static noinline void async_cow_start(struct btrfs_work *work)
1143 struct async_cow *async_cow;
1145 async_cow = container_of(work, struct async_cow, work);
1147 compress_file_range(async_cow->inode, async_cow->locked_page,
1148 async_cow->start, async_cow->end, async_cow,
1150 if (num_added == 0) {
1151 btrfs_add_delayed_iput(async_cow->inode);
1152 async_cow->inode = NULL;
1157 * work queue call back to submit previously compressed pages
1159 static noinline void async_cow_submit(struct btrfs_work *work)
1161 struct btrfs_fs_info *fs_info;
1162 struct async_cow *async_cow;
1163 struct btrfs_root *root;
1164 unsigned long nr_pages;
1166 async_cow = container_of(work, struct async_cow, work);
1168 root = async_cow->root;
1169 fs_info = root->fs_info;
1170 nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1174 * atomic_sub_return implies a barrier for waitqueue_active
1176 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1178 waitqueue_active(&fs_info->async_submit_wait))
1179 wake_up(&fs_info->async_submit_wait);
1181 if (async_cow->inode)
1182 submit_compressed_extents(async_cow->inode, async_cow);
1185 static noinline void async_cow_free(struct btrfs_work *work)
1187 struct async_cow *async_cow;
1188 async_cow = container_of(work, struct async_cow, work);
1189 if (async_cow->inode)
1190 btrfs_add_delayed_iput(async_cow->inode);
1194 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1195 u64 start, u64 end, int *page_started,
1196 unsigned long *nr_written,
1197 unsigned int write_flags)
1199 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1200 struct async_cow *async_cow;
1201 struct btrfs_root *root = BTRFS_I(inode)->root;
1202 unsigned long nr_pages;
1205 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1207 while (start < end) {
1208 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
1209 BUG_ON(!async_cow); /* -ENOMEM */
1210 async_cow->inode = igrab(inode);
1211 async_cow->root = root;
1212 async_cow->locked_page = locked_page;
1213 async_cow->start = start;
1214 async_cow->write_flags = write_flags;
1216 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1217 !btrfs_test_opt(fs_info, FORCE_COMPRESS))
1220 cur_end = min(end, start + SZ_512K - 1);
1222 async_cow->end = cur_end;
1223 INIT_LIST_HEAD(&async_cow->extents);
1225 btrfs_init_work(&async_cow->work,
1226 btrfs_delalloc_helper,
1227 async_cow_start, async_cow_submit,
1230 nr_pages = (cur_end - start + PAGE_SIZE) >>
1232 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1234 btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
1236 *nr_written += nr_pages;
1237 start = cur_end + 1;
1243 static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
1244 u64 bytenr, u64 num_bytes)
1247 struct btrfs_ordered_sum *sums;
1250 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
1251 bytenr + num_bytes - 1, &list, 0);
1252 if (ret == 0 && list_empty(&list))
1255 while (!list_empty(&list)) {
1256 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1257 list_del(&sums->list);
1264 * when nowcow writeback call back. This checks for snapshots or COW copies
1265 * of the extents that exist in the file, and COWs the file as required.
1267 * If no cow copies or snapshots exist, we write directly to the existing
1270 static noinline int run_delalloc_nocow(struct inode *inode,
1271 struct page *locked_page,
1272 u64 start, u64 end, int *page_started, int force,
1273 unsigned long *nr_written)
1275 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1276 struct btrfs_root *root = BTRFS_I(inode)->root;
1277 struct extent_buffer *leaf;
1278 struct btrfs_path *path;
1279 struct btrfs_file_extent_item *fi;
1280 struct btrfs_key found_key;
1281 struct extent_map *em;
1296 u64 ino = btrfs_ino(BTRFS_I(inode));
1298 path = btrfs_alloc_path();
1300 extent_clear_unlock_delalloc(inode, start, end, end,
1302 EXTENT_LOCKED | EXTENT_DELALLOC |
1303 EXTENT_DO_ACCOUNTING |
1304 EXTENT_DEFRAG, PAGE_UNLOCK |
1306 PAGE_SET_WRITEBACK |
1307 PAGE_END_WRITEBACK);
1311 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
1313 cow_start = (u64)-1;
1316 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
1320 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1321 leaf = path->nodes[0];
1322 btrfs_item_key_to_cpu(leaf, &found_key,
1323 path->slots[0] - 1);
1324 if (found_key.objectid == ino &&
1325 found_key.type == BTRFS_EXTENT_DATA_KEY)
1330 leaf = path->nodes[0];
1331 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1332 ret = btrfs_next_leaf(root, path);
1334 if (cow_start != (u64)-1)
1335 cur_offset = cow_start;
1340 leaf = path->nodes[0];
1346 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1348 if (found_key.objectid > ino)
1350 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1351 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1355 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
1356 found_key.offset > end)
1359 if (found_key.offset > cur_offset) {
1360 extent_end = found_key.offset;
1365 fi = btrfs_item_ptr(leaf, path->slots[0],
1366 struct btrfs_file_extent_item);
1367 extent_type = btrfs_file_extent_type(leaf, fi);
1369 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1370 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1371 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1372 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1373 extent_offset = btrfs_file_extent_offset(leaf, fi);
1374 extent_end = found_key.offset +
1375 btrfs_file_extent_num_bytes(leaf, fi);
1377 btrfs_file_extent_disk_num_bytes(leaf, fi);
1378 if (extent_end <= start) {
1382 if (disk_bytenr == 0)
1384 if (btrfs_file_extent_compression(leaf, fi) ||
1385 btrfs_file_extent_encryption(leaf, fi) ||
1386 btrfs_file_extent_other_encoding(leaf, fi))
1388 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1390 if (btrfs_extent_readonly(fs_info, disk_bytenr))
1392 if (btrfs_cross_ref_exist(root, ino,
1394 extent_offset, disk_bytenr))
1396 disk_bytenr += extent_offset;
1397 disk_bytenr += cur_offset - found_key.offset;
1398 num_bytes = min(end + 1, extent_end) - cur_offset;
1400 * if there are pending snapshots for this root,
1401 * we fall into common COW way.
1404 err = btrfs_start_write_no_snapshotting(root);
1409 * force cow if csum exists in the range.
1410 * this ensure that csum for a given extent are
1411 * either valid or do not exist.
1413 if (csum_exist_in_range(fs_info, disk_bytenr,
1416 btrfs_end_write_no_snapshotting(root);
1419 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr)) {
1421 btrfs_end_write_no_snapshotting(root);
1425 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1426 extent_end = found_key.offset +
1427 btrfs_file_extent_inline_len(leaf,
1428 path->slots[0], fi);
1429 extent_end = ALIGN(extent_end,
1430 fs_info->sectorsize);
1435 if (extent_end <= start) {
1437 if (!nolock && nocow)
1438 btrfs_end_write_no_snapshotting(root);
1440 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1444 if (cow_start == (u64)-1)
1445 cow_start = cur_offset;
1446 cur_offset = extent_end;
1447 if (cur_offset > end)
1453 btrfs_release_path(path);
1454 if (cow_start != (u64)-1) {
1455 ret = cow_file_range(inode, locked_page,
1456 cow_start, found_key.offset - 1,
1457 end, page_started, nr_written, 1,
1460 if (!nolock && nocow)
1461 btrfs_end_write_no_snapshotting(root);
1463 btrfs_dec_nocow_writers(fs_info,
1467 cow_start = (u64)-1;
1470 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1471 u64 orig_start = found_key.offset - extent_offset;
1473 em = create_io_em(inode, cur_offset, num_bytes,
1475 disk_bytenr, /* block_start */
1476 num_bytes, /* block_len */
1477 disk_num_bytes, /* orig_block_len */
1478 ram_bytes, BTRFS_COMPRESS_NONE,
1479 BTRFS_ORDERED_PREALLOC);
1481 if (!nolock && nocow)
1482 btrfs_end_write_no_snapshotting(root);
1484 btrfs_dec_nocow_writers(fs_info,
1489 free_extent_map(em);
1492 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1493 type = BTRFS_ORDERED_PREALLOC;
1495 type = BTRFS_ORDERED_NOCOW;
1498 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1499 num_bytes, num_bytes, type);
1501 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1502 BUG_ON(ret); /* -ENOMEM */
1504 if (root->root_key.objectid ==
1505 BTRFS_DATA_RELOC_TREE_OBJECTID)
1507 * Error handled later, as we must prevent
1508 * extent_clear_unlock_delalloc() in error handler
1509 * from freeing metadata of created ordered extent.
1511 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1514 extent_clear_unlock_delalloc(inode, cur_offset,
1515 cur_offset + num_bytes - 1, end,
1516 locked_page, EXTENT_LOCKED |
1518 EXTENT_CLEAR_DATA_RESV,
1519 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1521 if (!nolock && nocow)
1522 btrfs_end_write_no_snapshotting(root);
1523 cur_offset = extent_end;
1526 * btrfs_reloc_clone_csums() error, now we're OK to call error
1527 * handler, as metadata for created ordered extent will only
1528 * be freed by btrfs_finish_ordered_io().
1532 if (cur_offset > end)
1535 btrfs_release_path(path);
1537 if (cur_offset <= end && cow_start == (u64)-1) {
1538 cow_start = cur_offset;
1542 if (cow_start != (u64)-1) {
1543 ret = cow_file_range(inode, locked_page, cow_start, end, end,
1544 page_started, nr_written, 1, NULL);
1550 if (ret && cur_offset < end)
1551 extent_clear_unlock_delalloc(inode, cur_offset, end, end,
1552 locked_page, EXTENT_LOCKED |
1553 EXTENT_DELALLOC | EXTENT_DEFRAG |
1554 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1556 PAGE_SET_WRITEBACK |
1557 PAGE_END_WRITEBACK);
1558 btrfs_free_path(path);
1562 static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1565 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1566 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1570 * @defrag_bytes is a hint value, no spinlock held here,
1571 * if is not zero, it means the file is defragging.
1572 * Force cow if given extent needs to be defragged.
1574 if (BTRFS_I(inode)->defrag_bytes &&
1575 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1576 EXTENT_DEFRAG, 0, NULL))
1583 * extent_io.c call back to do delayed allocation processing
1585 static int run_delalloc_range(void *private_data, struct page *locked_page,
1586 u64 start, u64 end, int *page_started,
1587 unsigned long *nr_written,
1588 struct writeback_control *wbc)
1590 struct inode *inode = private_data;
1592 int force_cow = need_force_cow(inode, start, end);
1593 unsigned int write_flags = wbc_to_write_flags(wbc);
1595 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1596 ret = run_delalloc_nocow(inode, locked_page, start, end,
1597 page_started, 1, nr_written);
1598 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1599 ret = run_delalloc_nocow(inode, locked_page, start, end,
1600 page_started, 0, nr_written);
1601 } else if (!inode_need_compress(inode, start, end)) {
1602 ret = cow_file_range(inode, locked_page, start, end, end,
1603 page_started, nr_written, 1, NULL);
1605 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1606 &BTRFS_I(inode)->runtime_flags);
1607 ret = cow_file_range_async(inode, locked_page, start, end,
1608 page_started, nr_written,
1612 btrfs_cleanup_ordered_extents(inode, start, end - start + 1);
1616 static void btrfs_split_extent_hook(void *private_data,
1617 struct extent_state *orig, u64 split)
1619 struct inode *inode = private_data;
1622 /* not delalloc, ignore it */
1623 if (!(orig->state & EXTENT_DELALLOC))
1626 size = orig->end - orig->start + 1;
1627 if (size > BTRFS_MAX_EXTENT_SIZE) {
1632 * See the explanation in btrfs_merge_extent_hook, the same
1633 * applies here, just in reverse.
1635 new_size = orig->end - split + 1;
1636 num_extents = count_max_extents(new_size);
1637 new_size = split - orig->start;
1638 num_extents += count_max_extents(new_size);
1639 if (count_max_extents(size) >= num_extents)
1643 spin_lock(&BTRFS_I(inode)->lock);
1644 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1645 spin_unlock(&BTRFS_I(inode)->lock);
1649 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1650 * extents so we can keep track of new extents that are just merged onto old
1651 * extents, such as when we are doing sequential writes, so we can properly
1652 * account for the metadata space we'll need.
1654 static void btrfs_merge_extent_hook(void *private_data,
1655 struct extent_state *new,
1656 struct extent_state *other)
1658 struct inode *inode = private_data;
1659 u64 new_size, old_size;
1662 /* not delalloc, ignore it */
1663 if (!(other->state & EXTENT_DELALLOC))
1666 if (new->start > other->start)
1667 new_size = new->end - other->start + 1;
1669 new_size = other->end - new->start + 1;
1671 /* we're not bigger than the max, unreserve the space and go */
1672 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1673 spin_lock(&BTRFS_I(inode)->lock);
1674 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
1675 spin_unlock(&BTRFS_I(inode)->lock);
1680 * We have to add up either side to figure out how many extents were
1681 * accounted for before we merged into one big extent. If the number of
1682 * extents we accounted for is <= the amount we need for the new range
1683 * then we can return, otherwise drop. Think of it like this
1687 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1688 * need 2 outstanding extents, on one side we have 1 and the other side
1689 * we have 1 so they are == and we can return. But in this case
1691 * [MAX_SIZE+4k][MAX_SIZE+4k]
1693 * Each range on their own accounts for 2 extents, but merged together
1694 * they are only 3 extents worth of accounting, so we need to drop in
1697 old_size = other->end - other->start + 1;
1698 num_extents = count_max_extents(old_size);
1699 old_size = new->end - new->start + 1;
1700 num_extents += count_max_extents(old_size);
1701 if (count_max_extents(new_size) >= num_extents)
1704 spin_lock(&BTRFS_I(inode)->lock);
1705 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
1706 spin_unlock(&BTRFS_I(inode)->lock);
1709 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1710 struct inode *inode)
1712 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1714 spin_lock(&root->delalloc_lock);
1715 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1716 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1717 &root->delalloc_inodes);
1718 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1719 &BTRFS_I(inode)->runtime_flags);
1720 root->nr_delalloc_inodes++;
1721 if (root->nr_delalloc_inodes == 1) {
1722 spin_lock(&fs_info->delalloc_root_lock);
1723 BUG_ON(!list_empty(&root->delalloc_root));
1724 list_add_tail(&root->delalloc_root,
1725 &fs_info->delalloc_roots);
1726 spin_unlock(&fs_info->delalloc_root_lock);
1729 spin_unlock(&root->delalloc_lock);
1732 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1733 struct btrfs_inode *inode)
1735 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
1737 spin_lock(&root->delalloc_lock);
1738 if (!list_empty(&inode->delalloc_inodes)) {
1739 list_del_init(&inode->delalloc_inodes);
1740 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1741 &inode->runtime_flags);
1742 root->nr_delalloc_inodes--;
1743 if (!root->nr_delalloc_inodes) {
1744 spin_lock(&fs_info->delalloc_root_lock);
1745 BUG_ON(list_empty(&root->delalloc_root));
1746 list_del_init(&root->delalloc_root);
1747 spin_unlock(&fs_info->delalloc_root_lock);
1750 spin_unlock(&root->delalloc_lock);
1754 * extent_io.c set_bit_hook, used to track delayed allocation
1755 * bytes in this file, and to maintain the list of inodes that
1756 * have pending delalloc work to be done.
1758 static void btrfs_set_bit_hook(void *private_data,
1759 struct extent_state *state, unsigned *bits)
1761 struct inode *inode = private_data;
1763 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1765 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1768 * set_bit and clear bit hooks normally require _irqsave/restore
1769 * but in this case, we are only testing for the DELALLOC
1770 * bit, which is only set or cleared with irqs on
1772 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1773 struct btrfs_root *root = BTRFS_I(inode)->root;
1774 u64 len = state->end + 1 - state->start;
1775 u32 num_extents = count_max_extents(len);
1776 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
1778 spin_lock(&BTRFS_I(inode)->lock);
1779 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1780 spin_unlock(&BTRFS_I(inode)->lock);
1782 /* For sanity tests */
1783 if (btrfs_is_testing(fs_info))
1786 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1787 fs_info->delalloc_batch);
1788 spin_lock(&BTRFS_I(inode)->lock);
1789 BTRFS_I(inode)->delalloc_bytes += len;
1790 if (*bits & EXTENT_DEFRAG)
1791 BTRFS_I(inode)->defrag_bytes += len;
1792 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1793 &BTRFS_I(inode)->runtime_flags))
1794 btrfs_add_delalloc_inodes(root, inode);
1795 spin_unlock(&BTRFS_I(inode)->lock);
1798 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1799 (*bits & EXTENT_DELALLOC_NEW)) {
1800 spin_lock(&BTRFS_I(inode)->lock);
1801 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1803 spin_unlock(&BTRFS_I(inode)->lock);
1808 * extent_io.c clear_bit_hook, see set_bit_hook for why
1810 static void btrfs_clear_bit_hook(void *private_data,
1811 struct extent_state *state,
1814 struct btrfs_inode *inode = BTRFS_I((struct inode *)private_data);
1815 struct btrfs_fs_info *fs_info = btrfs_sb(inode->vfs_inode.i_sb);
1816 u64 len = state->end + 1 - state->start;
1817 u32 num_extents = count_max_extents(len);
1819 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1820 spin_lock(&inode->lock);
1821 inode->defrag_bytes -= len;
1822 spin_unlock(&inode->lock);
1826 * set_bit and clear bit hooks normally require _irqsave/restore
1827 * but in this case, we are only testing for the DELALLOC
1828 * bit, which is only set or cleared with irqs on
1830 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1831 struct btrfs_root *root = inode->root;
1832 bool do_list = !btrfs_is_free_space_inode(inode);
1834 spin_lock(&inode->lock);
1835 btrfs_mod_outstanding_extents(inode, -num_extents);
1836 spin_unlock(&inode->lock);
1839 * We don't reserve metadata space for space cache inodes so we
1840 * don't need to call dellalloc_release_metadata if there is an
1843 if (*bits & EXTENT_CLEAR_META_RESV &&
1844 root != fs_info->tree_root)
1845 btrfs_delalloc_release_metadata(inode, len);
1847 /* For sanity tests. */
1848 if (btrfs_is_testing(fs_info))
1851 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
1852 do_list && !(state->state & EXTENT_NORESERVE) &&
1853 (*bits & EXTENT_CLEAR_DATA_RESV))
1854 btrfs_free_reserved_data_space_noquota(
1858 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
1859 fs_info->delalloc_batch);
1860 spin_lock(&inode->lock);
1861 inode->delalloc_bytes -= len;
1862 if (do_list && inode->delalloc_bytes == 0 &&
1863 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1864 &inode->runtime_flags))
1865 btrfs_del_delalloc_inode(root, inode);
1866 spin_unlock(&inode->lock);
1869 if ((state->state & EXTENT_DELALLOC_NEW) &&
1870 (*bits & EXTENT_DELALLOC_NEW)) {
1871 spin_lock(&inode->lock);
1872 ASSERT(inode->new_delalloc_bytes >= len);
1873 inode->new_delalloc_bytes -= len;
1874 spin_unlock(&inode->lock);
1879 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1880 * we don't create bios that span stripes or chunks
1882 * return 1 if page cannot be merged to bio
1883 * return 0 if page can be merged to bio
1884 * return error otherwise
1886 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1887 size_t size, struct bio *bio,
1888 unsigned long bio_flags)
1890 struct inode *inode = page->mapping->host;
1891 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1892 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
1897 if (bio_flags & EXTENT_BIO_COMPRESSED)
1900 length = bio->bi_iter.bi_size;
1901 map_length = length;
1902 ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1906 if (map_length < length + size)
1912 * in order to insert checksums into the metadata in large chunks,
1913 * we wait until bio submission time. All the pages in the bio are
1914 * checksummed and sums are attached onto the ordered extent record.
1916 * At IO completion time the cums attached on the ordered extent record
1917 * are inserted into the btree
1919 static blk_status_t __btrfs_submit_bio_start(void *private_data, struct bio *bio,
1920 int mirror_num, unsigned long bio_flags,
1923 struct inode *inode = private_data;
1924 blk_status_t ret = 0;
1926 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
1927 BUG_ON(ret); /* -ENOMEM */
1932 * in order to insert checksums into the metadata in large chunks,
1933 * we wait until bio submission time. All the pages in the bio are
1934 * checksummed and sums are attached onto the ordered extent record.
1936 * At IO completion time the cums attached on the ordered extent record
1937 * are inserted into the btree
1939 static blk_status_t __btrfs_submit_bio_done(void *private_data, struct bio *bio,
1940 int mirror_num, unsigned long bio_flags,
1943 struct inode *inode = private_data;
1944 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1947 ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
1949 bio->bi_status = ret;
1956 * extent_io.c submission hook. This does the right thing for csum calculation
1957 * on write, or reading the csums from the tree before a read.
1959 * Rules about async/sync submit,
1960 * a) read: sync submit
1962 * b) write without checksum: sync submit
1964 * c) write with checksum:
1965 * c-1) if bio is issued by fsync: sync submit
1966 * (sync_writers != 0)
1968 * c-2) if root is reloc root: sync submit
1969 * (only in case of buffered IO)
1971 * c-3) otherwise: async submit
1973 static blk_status_t btrfs_submit_bio_hook(void *private_data, struct bio *bio,
1974 int mirror_num, unsigned long bio_flags,
1977 struct inode *inode = private_data;
1978 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1979 struct btrfs_root *root = BTRFS_I(inode)->root;
1980 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
1981 blk_status_t ret = 0;
1983 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
1985 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1987 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
1988 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
1990 if (bio_op(bio) != REQ_OP_WRITE) {
1991 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
1995 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1996 ret = btrfs_submit_compressed_read(inode, bio,
2000 } else if (!skip_sum) {
2001 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
2006 } else if (async && !skip_sum) {
2007 /* csum items have already been cloned */
2008 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2010 /* we're doing a write, do the async checksumming */
2011 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2013 __btrfs_submit_bio_start,
2014 __btrfs_submit_bio_done);
2016 } else if (!skip_sum) {
2017 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
2023 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
2027 bio->bi_status = ret;
2034 * given a list of ordered sums record them in the inode. This happens
2035 * at IO completion time based on sums calculated at bio submission time.
2037 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
2038 struct inode *inode, struct list_head *list)
2040 struct btrfs_ordered_sum *sum;
2043 list_for_each_entry(sum, list, list) {
2044 trans->adding_csums = true;
2045 ret = btrfs_csum_file_blocks(trans,
2046 BTRFS_I(inode)->root->fs_info->csum_root, sum);
2047 trans->adding_csums = false;
2054 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
2055 unsigned int extra_bits,
2056 struct extent_state **cached_state, int dedupe)
2058 WARN_ON((end & (PAGE_SIZE - 1)) == 0);
2059 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
2060 extra_bits, cached_state);
2063 /* see btrfs_writepage_start_hook for details on why this is required */
2064 struct btrfs_writepage_fixup {
2066 struct btrfs_work work;
2069 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
2071 struct btrfs_writepage_fixup *fixup;
2072 struct btrfs_ordered_extent *ordered;
2073 struct extent_state *cached_state = NULL;
2074 struct extent_changeset *data_reserved = NULL;
2076 struct inode *inode;
2081 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2085 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2086 ClearPageChecked(page);
2090 inode = page->mapping->host;
2091 page_start = page_offset(page);
2092 page_end = page_offset(page) + PAGE_SIZE - 1;
2094 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
2097 /* already ordered? We're done */
2098 if (PagePrivate2(page))
2101 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
2104 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2105 page_end, &cached_state);
2107 btrfs_start_ordered_extent(inode, ordered, 1);
2108 btrfs_put_ordered_extent(ordered);
2112 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2115 mapping_set_error(page->mapping, ret);
2116 end_extent_writepage(page, ret, page_start, page_end);
2117 ClearPageChecked(page);
2121 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2124 mapping_set_error(page->mapping, ret);
2125 end_extent_writepage(page, ret, page_start, page_end);
2126 ClearPageChecked(page);
2130 ClearPageChecked(page);
2131 set_page_dirty(page);
2132 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
2134 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2140 extent_changeset_free(data_reserved);
2144 * There are a few paths in the higher layers of the kernel that directly
2145 * set the page dirty bit without asking the filesystem if it is a
2146 * good idea. This causes problems because we want to make sure COW
2147 * properly happens and the data=ordered rules are followed.
2149 * In our case any range that doesn't have the ORDERED bit set
2150 * hasn't been properly setup for IO. We kick off an async process
2151 * to fix it up. The async helper will wait for ordered extents, set
2152 * the delalloc bit and make it safe to write the page.
2154 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
2156 struct inode *inode = page->mapping->host;
2157 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2158 struct btrfs_writepage_fixup *fixup;
2160 /* this page is properly in the ordered list */
2161 if (TestClearPagePrivate2(page))
2164 if (PageChecked(page))
2167 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2171 SetPageChecked(page);
2173 btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2174 btrfs_writepage_fixup_worker, NULL, NULL);
2176 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2180 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2181 struct inode *inode, u64 file_pos,
2182 u64 disk_bytenr, u64 disk_num_bytes,
2183 u64 num_bytes, u64 ram_bytes,
2184 u8 compression, u8 encryption,
2185 u16 other_encoding, int extent_type)
2187 struct btrfs_root *root = BTRFS_I(inode)->root;
2188 struct btrfs_file_extent_item *fi;
2189 struct btrfs_path *path;
2190 struct extent_buffer *leaf;
2191 struct btrfs_key ins;
2193 int extent_inserted = 0;
2196 path = btrfs_alloc_path();
2201 * we may be replacing one extent in the tree with another.
2202 * The new extent is pinned in the extent map, and we don't want
2203 * to drop it from the cache until it is completely in the btree.
2205 * So, tell btrfs_drop_extents to leave this extent in the cache.
2206 * the caller is expected to unpin it and allow it to be merged
2209 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2210 file_pos + num_bytes, NULL, 0,
2211 1, sizeof(*fi), &extent_inserted);
2215 if (!extent_inserted) {
2216 ins.objectid = btrfs_ino(BTRFS_I(inode));
2217 ins.offset = file_pos;
2218 ins.type = BTRFS_EXTENT_DATA_KEY;
2220 path->leave_spinning = 1;
2221 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2226 leaf = path->nodes[0];
2227 fi = btrfs_item_ptr(leaf, path->slots[0],
2228 struct btrfs_file_extent_item);
2229 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2230 btrfs_set_file_extent_type(leaf, fi, extent_type);
2231 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2232 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2233 btrfs_set_file_extent_offset(leaf, fi, 0);
2234 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2235 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2236 btrfs_set_file_extent_compression(leaf, fi, compression);
2237 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2238 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
2240 btrfs_mark_buffer_dirty(leaf);
2241 btrfs_release_path(path);
2243 inode_add_bytes(inode, num_bytes);
2245 ins.objectid = disk_bytenr;
2246 ins.offset = disk_num_bytes;
2247 ins.type = BTRFS_EXTENT_ITEM_KEY;
2250 * Release the reserved range from inode dirty range map, as it is
2251 * already moved into delayed_ref_head
2253 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2257 ret = btrfs_alloc_reserved_file_extent(trans, root,
2258 btrfs_ino(BTRFS_I(inode)),
2259 file_pos, qg_released, &ins);
2261 btrfs_free_path(path);
2266 /* snapshot-aware defrag */
2267 struct sa_defrag_extent_backref {
2268 struct rb_node node;
2269 struct old_sa_defrag_extent *old;
2278 struct old_sa_defrag_extent {
2279 struct list_head list;
2280 struct new_sa_defrag_extent *new;
2289 struct new_sa_defrag_extent {
2290 struct rb_root root;
2291 struct list_head head;
2292 struct btrfs_path *path;
2293 struct inode *inode;
2301 static int backref_comp(struct sa_defrag_extent_backref *b1,
2302 struct sa_defrag_extent_backref *b2)
2304 if (b1->root_id < b2->root_id)
2306 else if (b1->root_id > b2->root_id)
2309 if (b1->inum < b2->inum)
2311 else if (b1->inum > b2->inum)
2314 if (b1->file_pos < b2->file_pos)
2316 else if (b1->file_pos > b2->file_pos)
2320 * [------------------------------] ===> (a range of space)
2321 * |<--->| |<---->| =============> (fs/file tree A)
2322 * |<---------------------------->| ===> (fs/file tree B)
2324 * A range of space can refer to two file extents in one tree while
2325 * refer to only one file extent in another tree.
2327 * So we may process a disk offset more than one time(two extents in A)
2328 * and locate at the same extent(one extent in B), then insert two same
2329 * backrefs(both refer to the extent in B).
2334 static void backref_insert(struct rb_root *root,
2335 struct sa_defrag_extent_backref *backref)
2337 struct rb_node **p = &root->rb_node;
2338 struct rb_node *parent = NULL;
2339 struct sa_defrag_extent_backref *entry;
2344 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2346 ret = backref_comp(backref, entry);
2350 p = &(*p)->rb_right;
2353 rb_link_node(&backref->node, parent, p);
2354 rb_insert_color(&backref->node, root);
2358 * Note the backref might has changed, and in this case we just return 0.
2360 static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2363 struct btrfs_file_extent_item *extent;
2364 struct old_sa_defrag_extent *old = ctx;
2365 struct new_sa_defrag_extent *new = old->new;
2366 struct btrfs_path *path = new->path;
2367 struct btrfs_key key;
2368 struct btrfs_root *root;
2369 struct sa_defrag_extent_backref *backref;
2370 struct extent_buffer *leaf;
2371 struct inode *inode = new->inode;
2372 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2378 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2379 inum == btrfs_ino(BTRFS_I(inode)))
2382 key.objectid = root_id;
2383 key.type = BTRFS_ROOT_ITEM_KEY;
2384 key.offset = (u64)-1;
2386 root = btrfs_read_fs_root_no_name(fs_info, &key);
2388 if (PTR_ERR(root) == -ENOENT)
2391 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
2392 inum, offset, root_id);
2393 return PTR_ERR(root);
2396 key.objectid = inum;
2397 key.type = BTRFS_EXTENT_DATA_KEY;
2398 if (offset > (u64)-1 << 32)
2401 key.offset = offset;
2403 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2404 if (WARN_ON(ret < 0))
2411 leaf = path->nodes[0];
2412 slot = path->slots[0];
2414 if (slot >= btrfs_header_nritems(leaf)) {
2415 ret = btrfs_next_leaf(root, path);
2418 } else if (ret > 0) {
2427 btrfs_item_key_to_cpu(leaf, &key, slot);
2429 if (key.objectid > inum)
2432 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2435 extent = btrfs_item_ptr(leaf, slot,
2436 struct btrfs_file_extent_item);
2438 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2442 * 'offset' refers to the exact key.offset,
2443 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2444 * (key.offset - extent_offset).
2446 if (key.offset != offset)
2449 extent_offset = btrfs_file_extent_offset(leaf, extent);
2450 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2452 if (extent_offset >= old->extent_offset + old->offset +
2453 old->len || extent_offset + num_bytes <=
2454 old->extent_offset + old->offset)
2459 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2465 backref->root_id = root_id;
2466 backref->inum = inum;
2467 backref->file_pos = offset;
2468 backref->num_bytes = num_bytes;
2469 backref->extent_offset = extent_offset;
2470 backref->generation = btrfs_file_extent_generation(leaf, extent);
2472 backref_insert(&new->root, backref);
2475 btrfs_release_path(path);
2480 static noinline bool record_extent_backrefs(struct btrfs_path *path,
2481 struct new_sa_defrag_extent *new)
2483 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2484 struct old_sa_defrag_extent *old, *tmp;
2489 list_for_each_entry_safe(old, tmp, &new->head, list) {
2490 ret = iterate_inodes_from_logical(old->bytenr +
2491 old->extent_offset, fs_info,
2492 path, record_one_backref,
2494 if (ret < 0 && ret != -ENOENT)
2497 /* no backref to be processed for this extent */
2499 list_del(&old->list);
2504 if (list_empty(&new->head))
2510 static int relink_is_mergable(struct extent_buffer *leaf,
2511 struct btrfs_file_extent_item *fi,
2512 struct new_sa_defrag_extent *new)
2514 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
2517 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2520 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2523 if (btrfs_file_extent_encryption(leaf, fi) ||
2524 btrfs_file_extent_other_encoding(leaf, fi))
2531 * Note the backref might has changed, and in this case we just return 0.
2533 static noinline int relink_extent_backref(struct btrfs_path *path,
2534 struct sa_defrag_extent_backref *prev,
2535 struct sa_defrag_extent_backref *backref)
2537 struct btrfs_file_extent_item *extent;
2538 struct btrfs_file_extent_item *item;
2539 struct btrfs_ordered_extent *ordered;
2540 struct btrfs_trans_handle *trans;
2541 struct btrfs_root *root;
2542 struct btrfs_key key;
2543 struct extent_buffer *leaf;
2544 struct old_sa_defrag_extent *old = backref->old;
2545 struct new_sa_defrag_extent *new = old->new;
2546 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2547 struct inode *inode;
2548 struct extent_state *cached = NULL;
2557 if (prev && prev->root_id == backref->root_id &&
2558 prev->inum == backref->inum &&
2559 prev->file_pos + prev->num_bytes == backref->file_pos)
2562 /* step 1: get root */
2563 key.objectid = backref->root_id;
2564 key.type = BTRFS_ROOT_ITEM_KEY;
2565 key.offset = (u64)-1;
2567 index = srcu_read_lock(&fs_info->subvol_srcu);
2569 root = btrfs_read_fs_root_no_name(fs_info, &key);
2571 srcu_read_unlock(&fs_info->subvol_srcu, index);
2572 if (PTR_ERR(root) == -ENOENT)
2574 return PTR_ERR(root);
2577 if (btrfs_root_readonly(root)) {
2578 srcu_read_unlock(&fs_info->subvol_srcu, index);
2582 /* step 2: get inode */
2583 key.objectid = backref->inum;
2584 key.type = BTRFS_INODE_ITEM_KEY;
2587 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2588 if (IS_ERR(inode)) {
2589 srcu_read_unlock(&fs_info->subvol_srcu, index);
2593 srcu_read_unlock(&fs_info->subvol_srcu, index);
2595 /* step 3: relink backref */
2596 lock_start = backref->file_pos;
2597 lock_end = backref->file_pos + backref->num_bytes - 1;
2598 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2601 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2603 btrfs_put_ordered_extent(ordered);
2607 trans = btrfs_join_transaction(root);
2608 if (IS_ERR(trans)) {
2609 ret = PTR_ERR(trans);
2613 key.objectid = backref->inum;
2614 key.type = BTRFS_EXTENT_DATA_KEY;
2615 key.offset = backref->file_pos;
2617 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2620 } else if (ret > 0) {
2625 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2626 struct btrfs_file_extent_item);
2628 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2629 backref->generation)
2632 btrfs_release_path(path);
2634 start = backref->file_pos;
2635 if (backref->extent_offset < old->extent_offset + old->offset)
2636 start += old->extent_offset + old->offset -
2637 backref->extent_offset;
2639 len = min(backref->extent_offset + backref->num_bytes,
2640 old->extent_offset + old->offset + old->len);
2641 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2643 ret = btrfs_drop_extents(trans, root, inode, start,
2648 key.objectid = btrfs_ino(BTRFS_I(inode));
2649 key.type = BTRFS_EXTENT_DATA_KEY;
2652 path->leave_spinning = 1;
2654 struct btrfs_file_extent_item *fi;
2656 struct btrfs_key found_key;
2658 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2663 leaf = path->nodes[0];
2664 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2666 fi = btrfs_item_ptr(leaf, path->slots[0],
2667 struct btrfs_file_extent_item);
2668 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2670 if (extent_len + found_key.offset == start &&
2671 relink_is_mergable(leaf, fi, new)) {
2672 btrfs_set_file_extent_num_bytes(leaf, fi,
2674 btrfs_mark_buffer_dirty(leaf);
2675 inode_add_bytes(inode, len);
2681 btrfs_release_path(path);
2686 ret = btrfs_insert_empty_item(trans, root, path, &key,
2689 btrfs_abort_transaction(trans, ret);
2693 leaf = path->nodes[0];
2694 item = btrfs_item_ptr(leaf, path->slots[0],
2695 struct btrfs_file_extent_item);
2696 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2697 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2698 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2699 btrfs_set_file_extent_num_bytes(leaf, item, len);
2700 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2701 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2702 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2703 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2704 btrfs_set_file_extent_encryption(leaf, item, 0);
2705 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2707 btrfs_mark_buffer_dirty(leaf);
2708 inode_add_bytes(inode, len);
2709 btrfs_release_path(path);
2711 ret = btrfs_inc_extent_ref(trans, root, new->bytenr,
2713 backref->root_id, backref->inum,
2714 new->file_pos); /* start - extent_offset */
2716 btrfs_abort_transaction(trans, ret);
2722 btrfs_release_path(path);
2723 path->leave_spinning = 0;
2724 btrfs_end_transaction(trans);
2726 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2732 static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2734 struct old_sa_defrag_extent *old, *tmp;
2739 list_for_each_entry_safe(old, tmp, &new->head, list) {
2745 static void relink_file_extents(struct new_sa_defrag_extent *new)
2747 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2748 struct btrfs_path *path;
2749 struct sa_defrag_extent_backref *backref;
2750 struct sa_defrag_extent_backref *prev = NULL;
2751 struct inode *inode;
2752 struct btrfs_root *root;
2753 struct rb_node *node;
2757 root = BTRFS_I(inode)->root;
2759 path = btrfs_alloc_path();
2763 if (!record_extent_backrefs(path, new)) {
2764 btrfs_free_path(path);
2767 btrfs_release_path(path);
2770 node = rb_first(&new->root);
2773 rb_erase(node, &new->root);
2775 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2777 ret = relink_extent_backref(path, prev, backref);
2790 btrfs_free_path(path);
2792 free_sa_defrag_extent(new);
2794 atomic_dec(&fs_info->defrag_running);
2795 wake_up(&fs_info->transaction_wait);
2798 static struct new_sa_defrag_extent *
2799 record_old_file_extents(struct inode *inode,
2800 struct btrfs_ordered_extent *ordered)
2802 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2803 struct btrfs_root *root = BTRFS_I(inode)->root;
2804 struct btrfs_path *path;
2805 struct btrfs_key key;
2806 struct old_sa_defrag_extent *old;
2807 struct new_sa_defrag_extent *new;
2810 new = kmalloc(sizeof(*new), GFP_NOFS);
2815 new->file_pos = ordered->file_offset;
2816 new->len = ordered->len;
2817 new->bytenr = ordered->start;
2818 new->disk_len = ordered->disk_len;
2819 new->compress_type = ordered->compress_type;
2820 new->root = RB_ROOT;
2821 INIT_LIST_HEAD(&new->head);
2823 path = btrfs_alloc_path();
2827 key.objectid = btrfs_ino(BTRFS_I(inode));
2828 key.type = BTRFS_EXTENT_DATA_KEY;
2829 key.offset = new->file_pos;
2831 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2834 if (ret > 0 && path->slots[0] > 0)
2837 /* find out all the old extents for the file range */
2839 struct btrfs_file_extent_item *extent;
2840 struct extent_buffer *l;
2849 slot = path->slots[0];
2851 if (slot >= btrfs_header_nritems(l)) {
2852 ret = btrfs_next_leaf(root, path);
2860 btrfs_item_key_to_cpu(l, &key, slot);
2862 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
2864 if (key.type != BTRFS_EXTENT_DATA_KEY)
2866 if (key.offset >= new->file_pos + new->len)
2869 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2871 num_bytes = btrfs_file_extent_num_bytes(l, extent);
2872 if (key.offset + num_bytes < new->file_pos)
2875 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2879 extent_offset = btrfs_file_extent_offset(l, extent);
2881 old = kmalloc(sizeof(*old), GFP_NOFS);
2885 offset = max(new->file_pos, key.offset);
2886 end = min(new->file_pos + new->len, key.offset + num_bytes);
2888 old->bytenr = disk_bytenr;
2889 old->extent_offset = extent_offset;
2890 old->offset = offset - key.offset;
2891 old->len = end - offset;
2894 list_add_tail(&old->list, &new->head);
2900 btrfs_free_path(path);
2901 atomic_inc(&fs_info->defrag_running);
2906 btrfs_free_path(path);
2908 free_sa_defrag_extent(new);
2912 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
2915 struct btrfs_block_group_cache *cache;
2917 cache = btrfs_lookup_block_group(fs_info, start);
2920 spin_lock(&cache->lock);
2921 cache->delalloc_bytes -= len;
2922 spin_unlock(&cache->lock);
2924 btrfs_put_block_group(cache);
2927 /* as ordered data IO finishes, this gets called so we can finish
2928 * an ordered extent if the range of bytes in the file it covers are
2931 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2933 struct inode *inode = ordered_extent->inode;
2934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2935 struct btrfs_root *root = BTRFS_I(inode)->root;
2936 struct btrfs_trans_handle *trans = NULL;
2937 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2938 struct extent_state *cached_state = NULL;
2939 struct new_sa_defrag_extent *new = NULL;
2940 int compress_type = 0;
2942 u64 logical_len = ordered_extent->len;
2944 bool truncated = false;
2945 bool range_locked = false;
2946 bool clear_new_delalloc_bytes = false;
2948 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2949 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2950 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2951 clear_new_delalloc_bytes = true;
2953 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
2955 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2960 btrfs_free_io_failure_record(BTRFS_I(inode),
2961 ordered_extent->file_offset,
2962 ordered_extent->file_offset +
2963 ordered_extent->len - 1);
2965 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2967 logical_len = ordered_extent->truncated_len;
2968 /* Truncated the entire extent, don't bother adding */
2973 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
2974 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
2977 * For mwrite(mmap + memset to write) case, we still reserve
2978 * space for NOCOW range.
2979 * As NOCOW won't cause a new delayed ref, just free the space
2981 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
2982 ordered_extent->len);
2983 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2985 trans = btrfs_join_transaction_nolock(root);
2987 trans = btrfs_join_transaction(root);
2988 if (IS_ERR(trans)) {
2989 ret = PTR_ERR(trans);
2993 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
2994 ret = btrfs_update_inode_fallback(trans, root, inode);
2995 if (ret) /* -ENOMEM or corruption */
2996 btrfs_abort_transaction(trans, ret);
3000 range_locked = true;
3001 lock_extent_bits(io_tree, ordered_extent->file_offset,
3002 ordered_extent->file_offset + ordered_extent->len - 1,
3005 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3006 ordered_extent->file_offset + ordered_extent->len - 1,
3007 EXTENT_DEFRAG, 0, cached_state);
3009 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
3010 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
3011 /* the inode is shared */
3012 new = record_old_file_extents(inode, ordered_extent);
3014 clear_extent_bit(io_tree, ordered_extent->file_offset,
3015 ordered_extent->file_offset + ordered_extent->len - 1,
3016 EXTENT_DEFRAG, 0, 0, &cached_state);
3020 trans = btrfs_join_transaction_nolock(root);
3022 trans = btrfs_join_transaction(root);
3023 if (IS_ERR(trans)) {
3024 ret = PTR_ERR(trans);
3029 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
3031 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
3032 compress_type = ordered_extent->compress_type;
3033 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3034 BUG_ON(compress_type);
3035 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3036 ordered_extent->len);
3037 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
3038 ordered_extent->file_offset,
3039 ordered_extent->file_offset +
3042 BUG_ON(root == fs_info->tree_root);
3043 ret = insert_reserved_file_extent(trans, inode,
3044 ordered_extent->file_offset,
3045 ordered_extent->start,
3046 ordered_extent->disk_len,
3047 logical_len, logical_len,
3048 compress_type, 0, 0,
3049 BTRFS_FILE_EXTENT_REG);
3051 btrfs_release_delalloc_bytes(fs_info,
3052 ordered_extent->start,
3053 ordered_extent->disk_len);
3055 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3056 ordered_extent->file_offset, ordered_extent->len,
3059 btrfs_abort_transaction(trans, ret);
3063 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3065 btrfs_abort_transaction(trans, ret);
3069 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3070 ret = btrfs_update_inode_fallback(trans, root, inode);
3071 if (ret) { /* -ENOMEM or corruption */
3072 btrfs_abort_transaction(trans, ret);