1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/kernel.h>
8 #include <linux/buffer_head.h>
9 #include <linux/file.h>
11 #include <linux/pagemap.h>
12 #include <linux/highmem.h>
13 #include <linux/time.h>
14 #include <linux/init.h>
15 #include <linux/string.h>
16 #include <linux/backing-dev.h>
17 #include <linux/writeback.h>
18 #include <linux/compat.h>
19 #include <linux/xattr.h>
20 #include <linux/posix_acl.h>
21 #include <linux/falloc.h>
22 #include <linux/slab.h>
23 #include <linux/ratelimit.h>
24 #include <linux/btrfs.h>
25 #include <linux/blkdev.h>
26 #include <linux/posix_acl_xattr.h>
27 #include <linux/uio.h>
28 #include <linux/magic.h>
29 #include <linux/iversion.h>
30 #include <linux/swap.h>
31 #include <linux/sched/mm.h>
32 #include <asm/unaligned.h>
36 #include "transaction.h"
37 #include "btrfs_inode.h"
38 #include "print-tree.h"
39 #include "ordered-data.h"
43 #include "compression.h"
45 #include "free-space-cache.h"
46 #include "inode-map.h"
50 #include "delalloc-space.h"
51 #include "block-group.h"
52 #include "space-info.h"
54 struct btrfs_iget_args {
55 struct btrfs_key *location;
56 struct btrfs_root *root;
59 struct btrfs_dio_data {
61 u64 unsubmitted_oe_range_start;
62 u64 unsubmitted_oe_range_end;
66 static const struct inode_operations btrfs_dir_inode_operations;
67 static const struct inode_operations btrfs_symlink_inode_operations;
68 static const struct inode_operations btrfs_dir_ro_inode_operations;
69 static const struct inode_operations btrfs_special_inode_operations;
70 static const struct inode_operations btrfs_file_inode_operations;
71 static const struct address_space_operations btrfs_aops;
72 static const struct file_operations btrfs_dir_file_operations;
73 static const struct extent_io_ops btrfs_extent_io_ops;
75 static struct kmem_cache *btrfs_inode_cachep;
76 struct kmem_cache *btrfs_trans_handle_cachep;
77 struct kmem_cache *btrfs_path_cachep;
78 struct kmem_cache *btrfs_free_space_cachep;
79 struct kmem_cache *btrfs_free_space_bitmap_cachep;
81 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
82 static int btrfs_truncate(struct inode *inode, bool skip_writeback);
83 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
84 static noinline int cow_file_range(struct inode *inode,
85 struct page *locked_page,
86 u64 start, u64 end, int *page_started,
87 unsigned long *nr_written, int unlock);
88 static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
89 u64 orig_start, u64 block_start,
90 u64 block_len, u64 orig_block_len,
91 u64 ram_bytes, int compress_type,
94 static void __endio_write_update_ordered(struct inode *inode,
95 const u64 offset, const u64 bytes,
99 * Cleanup all submitted ordered extents in specified range to handle errors
100 * from the btrfs_run_delalloc_range() callback.
102 * NOTE: caller must ensure that when an error happens, it can not call
103 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
104 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
105 * to be released, which we want to happen only when finishing the ordered
106 * extent (btrfs_finish_ordered_io()).
108 static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
109 struct page *locked_page,
110 u64 offset, u64 bytes)
112 unsigned long index = offset >> PAGE_SHIFT;
113 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
114 u64 page_start = page_offset(locked_page);
115 u64 page_end = page_start + PAGE_SIZE - 1;
119 while (index <= end_index) {
120 page = find_get_page(inode->i_mapping, index);
124 ClearPagePrivate2(page);
129 * In case this page belongs to the delalloc range being instantiated
130 * then skip it, since the first page of a range is going to be
131 * properly cleaned up by the caller of run_delalloc_range
133 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
138 return __endio_write_update_ordered(inode, offset, bytes, false);
141 static int btrfs_dirty_inode(struct inode *inode);
143 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
144 void btrfs_test_inode_set_ops(struct inode *inode)
146 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
150 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
151 struct inode *inode, struct inode *dir,
152 const struct qstr *qstr)
156 err = btrfs_init_acl(trans, inode, dir);
158 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
163 * this does all the hard work for inserting an inline extent into
164 * the btree. The caller should have done a btrfs_drop_extents so that
165 * no overlapping inline items exist in the btree
167 static int insert_inline_extent(struct btrfs_trans_handle *trans,
168 struct btrfs_path *path, int extent_inserted,
169 struct btrfs_root *root, struct inode *inode,
170 u64 start, size_t size, size_t compressed_size,
172 struct page **compressed_pages)
174 struct extent_buffer *leaf;
175 struct page *page = NULL;
178 struct btrfs_file_extent_item *ei;
180 size_t cur_size = size;
181 unsigned long offset;
183 ASSERT((compressed_size > 0 && compressed_pages) ||
184 (compressed_size == 0 && !compressed_pages));
186 if (compressed_size && compressed_pages)
187 cur_size = compressed_size;
189 inode_add_bytes(inode, size);
191 if (!extent_inserted) {
192 struct btrfs_key key;
195 key.objectid = btrfs_ino(BTRFS_I(inode));
197 key.type = BTRFS_EXTENT_DATA_KEY;
199 datasize = btrfs_file_extent_calc_inline_size(cur_size);
200 path->leave_spinning = 1;
201 ret = btrfs_insert_empty_item(trans, root, path, &key,
206 leaf = path->nodes[0];
207 ei = btrfs_item_ptr(leaf, path->slots[0],
208 struct btrfs_file_extent_item);
209 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
210 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
211 btrfs_set_file_extent_encryption(leaf, ei, 0);
212 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
213 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
214 ptr = btrfs_file_extent_inline_start(ei);
216 if (compress_type != BTRFS_COMPRESS_NONE) {
219 while (compressed_size > 0) {
220 cpage = compressed_pages[i];
221 cur_size = min_t(unsigned long, compressed_size,
224 kaddr = kmap_atomic(cpage);
225 write_extent_buffer(leaf, kaddr, ptr, cur_size);
226 kunmap_atomic(kaddr);
230 compressed_size -= cur_size;
232 btrfs_set_file_extent_compression(leaf, ei,
235 page = find_get_page(inode->i_mapping,
236 start >> PAGE_SHIFT);
237 btrfs_set_file_extent_compression(leaf, ei, 0);
238 kaddr = kmap_atomic(page);
239 offset = offset_in_page(start);
240 write_extent_buffer(leaf, kaddr + offset, ptr, size);
241 kunmap_atomic(kaddr);
244 btrfs_mark_buffer_dirty(leaf);
245 btrfs_release_path(path);
248 * we're an inline extent, so nobody can
249 * extend the file past i_size without locking
250 * a page we already have locked.
252 * We must do any isize and inode updates
253 * before we unlock the pages. Otherwise we
254 * could end up racing with unlink.
256 BTRFS_I(inode)->disk_i_size = inode->i_size;
257 ret = btrfs_update_inode(trans, root, inode);
265 * conditionally insert an inline extent into the file. This
266 * does the checks required to make sure the data is small enough
267 * to fit as an inline extent.
269 static noinline int cow_file_range_inline(struct inode *inode, u64 start,
270 u64 end, size_t compressed_size,
272 struct page **compressed_pages)
274 struct btrfs_root *root = BTRFS_I(inode)->root;
275 struct btrfs_fs_info *fs_info = root->fs_info;
276 struct btrfs_trans_handle *trans;
277 u64 isize = i_size_read(inode);
278 u64 actual_end = min(end + 1, isize);
279 u64 inline_len = actual_end - start;
280 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
281 u64 data_len = inline_len;
283 struct btrfs_path *path;
284 int extent_inserted = 0;
285 u32 extent_item_size;
288 data_len = compressed_size;
291 actual_end > fs_info->sectorsize ||
292 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
294 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
296 data_len > fs_info->max_inline) {
300 path = btrfs_alloc_path();
304 trans = btrfs_join_transaction(root);
306 btrfs_free_path(path);
307 return PTR_ERR(trans);
309 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
311 if (compressed_size && compressed_pages)
312 extent_item_size = btrfs_file_extent_calc_inline_size(
315 extent_item_size = btrfs_file_extent_calc_inline_size(
318 ret = __btrfs_drop_extents(trans, root, inode, path,
319 start, aligned_end, NULL,
320 1, 1, extent_item_size, &extent_inserted);
322 btrfs_abort_transaction(trans, ret);
326 if (isize > actual_end)
327 inline_len = min_t(u64, isize, actual_end);
328 ret = insert_inline_extent(trans, path, extent_inserted,
330 inline_len, compressed_size,
331 compress_type, compressed_pages);
332 if (ret && ret != -ENOSPC) {
333 btrfs_abort_transaction(trans, ret);
335 } else if (ret == -ENOSPC) {
340 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
341 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
344 * Don't forget to free the reserved space, as for inlined extent
345 * it won't count as data extent, free them directly here.
346 * And at reserve time, it's always aligned to page size, so
347 * just free one page here.
349 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
350 btrfs_free_path(path);
351 btrfs_end_transaction(trans);
355 struct async_extent {
360 unsigned long nr_pages;
362 struct list_head list;
367 struct page *locked_page;
370 unsigned int write_flags;
371 struct list_head extents;
372 struct btrfs_work work;
377 /* Number of chunks in flight; must be first in the structure */
379 struct async_chunk chunks[];
382 static noinline int add_async_extent(struct async_chunk *cow,
383 u64 start, u64 ram_size,
386 unsigned long nr_pages,
389 struct async_extent *async_extent;
391 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
392 BUG_ON(!async_extent); /* -ENOMEM */
393 async_extent->start = start;
394 async_extent->ram_size = ram_size;
395 async_extent->compressed_size = compressed_size;
396 async_extent->pages = pages;
397 async_extent->nr_pages = nr_pages;
398 async_extent->compress_type = compress_type;
399 list_add_tail(&async_extent->list, &cow->extents);
404 * Check if the inode has flags compatible with compression
406 static inline bool inode_can_compress(struct inode *inode)
408 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
409 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
415 * Check if the inode needs to be submitted to compression, based on mount
416 * options, defragmentation, properties or heuristics.
418 static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
420 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
422 if (!inode_can_compress(inode)) {
423 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
424 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
425 btrfs_ino(BTRFS_I(inode)));
429 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
432 if (BTRFS_I(inode)->defrag_compress)
434 /* bad compression ratios */
435 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
437 if (btrfs_test_opt(fs_info, COMPRESS) ||
438 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
439 BTRFS_I(inode)->prop_compress)
440 return btrfs_compress_heuristic(inode, start, end);
444 static inline void inode_should_defrag(struct btrfs_inode *inode,
445 u64 start, u64 end, u64 num_bytes, u64 small_write)
447 /* If this is a small write inside eof, kick off a defrag */
448 if (num_bytes < small_write &&
449 (start > 0 || end + 1 < inode->disk_i_size))
450 btrfs_add_inode_defrag(NULL, inode);
454 * we create compressed extents in two phases. The first
455 * phase compresses a range of pages that have already been
456 * locked (both pages and state bits are locked).
458 * This is done inside an ordered work queue, and the compression
459 * is spread across many cpus. The actual IO submission is step
460 * two, and the ordered work queue takes care of making sure that
461 * happens in the same order things were put onto the queue by
462 * writepages and friends.
464 * If this code finds it can't get good compression, it puts an
465 * entry onto the work queue to write the uncompressed bytes. This
466 * makes sure that both compressed inodes and uncompressed inodes
467 * are written in the same order that the flusher thread sent them
470 static noinline int compress_file_range(struct async_chunk *async_chunk)
472 struct inode *inode = async_chunk->inode;
473 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
474 u64 blocksize = fs_info->sectorsize;
475 u64 start = async_chunk->start;
476 u64 end = async_chunk->end;
480 struct page **pages = NULL;
481 unsigned long nr_pages;
482 unsigned long total_compressed = 0;
483 unsigned long total_in = 0;
486 int compress_type = fs_info->compress_type;
487 int compressed_extents = 0;
490 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
494 * We need to save i_size before now because it could change in between
495 * us evaluating the size and assigning it. This is because we lock and
496 * unlock the page in truncate and fallocate, and then modify the i_size
499 * The barriers are to emulate READ_ONCE, remove that once i_size_read
503 i_size = i_size_read(inode);
505 actual_end = min_t(u64, i_size, end + 1);
508 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
509 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
510 nr_pages = min_t(unsigned long, nr_pages,
511 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
514 * we don't want to send crud past the end of i_size through
515 * compression, that's just a waste of CPU time. So, if the
516 * end of the file is before the start of our current
517 * requested range of bytes, we bail out to the uncompressed
518 * cleanup code that can deal with all of this.
520 * It isn't really the fastest way to fix things, but this is a
521 * very uncommon corner.
523 if (actual_end <= start)
524 goto cleanup_and_bail_uncompressed;
526 total_compressed = actual_end - start;
529 * skip compression for a small file range(<=blocksize) that
530 * isn't an inline extent, since it doesn't save disk space at all.
532 if (total_compressed <= blocksize &&
533 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
534 goto cleanup_and_bail_uncompressed;
536 total_compressed = min_t(unsigned long, total_compressed,
537 BTRFS_MAX_UNCOMPRESSED);
542 * we do compression for mount -o compress and when the
543 * inode has not been flagged as nocompress. This flag can
544 * change at any time if we discover bad compression ratios.
546 if (inode_need_compress(inode, start, end)) {
548 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
550 /* just bail out to the uncompressed code */
555 if (BTRFS_I(inode)->defrag_compress)
556 compress_type = BTRFS_I(inode)->defrag_compress;
557 else if (BTRFS_I(inode)->prop_compress)
558 compress_type = BTRFS_I(inode)->prop_compress;
561 * we need to call clear_page_dirty_for_io on each
562 * page in the range. Otherwise applications with the file
563 * mmap'd can wander in and change the page contents while
564 * we are compressing them.
566 * If the compression fails for any reason, we set the pages
567 * dirty again later on.
569 * Note that the remaining part is redirtied, the start pointer
570 * has moved, the end is the original one.
573 extent_range_clear_dirty_for_io(inode, start, end);
577 /* Compression level is applied here and only here */
578 ret = btrfs_compress_pages(
579 compress_type | (fs_info->compress_level << 4),
580 inode->i_mapping, start,
587 unsigned long offset = offset_in_page(total_compressed);
588 struct page *page = pages[nr_pages - 1];
591 /* zero the tail end of the last page, we might be
592 * sending it down to disk
595 kaddr = kmap_atomic(page);
596 memset(kaddr + offset, 0,
598 kunmap_atomic(kaddr);
605 /* lets try to make an inline extent */
606 if (ret || total_in < actual_end) {
607 /* we didn't compress the entire range, try
608 * to make an uncompressed inline extent.
610 ret = cow_file_range_inline(inode, start, end, 0,
611 BTRFS_COMPRESS_NONE, NULL);
613 /* try making a compressed inline extent */
614 ret = cow_file_range_inline(inode, start, end,
616 compress_type, pages);
619 unsigned long clear_flags = EXTENT_DELALLOC |
620 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
621 EXTENT_DO_ACCOUNTING;
622 unsigned long page_error_op;
624 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
627 * inline extent creation worked or returned error,
628 * we don't need to create any more async work items.
629 * Unlock and free up our temp pages.
631 * We use DO_ACCOUNTING here because we need the
632 * delalloc_release_metadata to be done _after_ we drop
633 * our outstanding extent for clearing delalloc for this
636 extent_clear_unlock_delalloc(inode, start, end, NULL,
645 * Ensure we only free the compressed pages if we have
646 * them allocated, as we can still reach here with
647 * inode_need_compress() == false.
650 for (i = 0; i < nr_pages; i++) {
651 WARN_ON(pages[i]->mapping);
662 * we aren't doing an inline extent round the compressed size
663 * up to a block size boundary so the allocator does sane
666 total_compressed = ALIGN(total_compressed, blocksize);
669 * one last check to make sure the compression is really a
670 * win, compare the page count read with the blocks on disk,
671 * compression must free at least one sector size
673 total_in = ALIGN(total_in, PAGE_SIZE);
674 if (total_compressed + blocksize <= total_in) {
675 compressed_extents++;
678 * The async work queues will take care of doing actual
679 * allocation on disk for these compressed pages, and
680 * will submit them to the elevator.
682 add_async_extent(async_chunk, start, total_in,
683 total_compressed, pages, nr_pages,
686 if (start + total_in < end) {
692 return compressed_extents;
697 * the compression code ran but failed to make things smaller,
698 * free any pages it allocated and our page pointer array
700 for (i = 0; i < nr_pages; i++) {
701 WARN_ON(pages[i]->mapping);
706 total_compressed = 0;
709 /* flag the file so we don't compress in the future */
710 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
711 !(BTRFS_I(inode)->prop_compress)) {
712 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
715 cleanup_and_bail_uncompressed:
717 * No compression, but we still need to write the pages in the file
718 * we've been given so far. redirty the locked page if it corresponds
719 * to our extent and set things up for the async work queue to run
720 * cow_file_range to do the normal delalloc dance.
722 if (async_chunk->locked_page &&
723 (page_offset(async_chunk->locked_page) >= start &&
724 page_offset(async_chunk->locked_page)) <= end) {
725 __set_page_dirty_nobuffers(async_chunk->locked_page);
726 /* unlocked later on in the async handlers */
730 extent_range_redirty_for_io(inode, start, end);
731 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
732 BTRFS_COMPRESS_NONE);
733 compressed_extents++;
735 return compressed_extents;
738 static void free_async_extent_pages(struct async_extent *async_extent)
742 if (!async_extent->pages)
745 for (i = 0; i < async_extent->nr_pages; i++) {
746 WARN_ON(async_extent->pages[i]->mapping);
747 put_page(async_extent->pages[i]);
749 kfree(async_extent->pages);
750 async_extent->nr_pages = 0;
751 async_extent->pages = NULL;
755 * phase two of compressed writeback. This is the ordered portion
756 * of the code, which only gets called in the order the work was
757 * queued. We walk all the async extents created by compress_file_range
758 * and send them down to the disk.
760 static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
762 struct inode *inode = async_chunk->inode;
763 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
764 struct async_extent *async_extent;
766 struct btrfs_key ins;
767 struct extent_map *em;
768 struct btrfs_root *root = BTRFS_I(inode)->root;
769 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
773 while (!list_empty(&async_chunk->extents)) {
774 async_extent = list_entry(async_chunk->extents.next,
775 struct async_extent, list);
776 list_del(&async_extent->list);
779 lock_extent(io_tree, async_extent->start,
780 async_extent->start + async_extent->ram_size - 1);
781 /* did the compression code fall back to uncompressed IO? */
782 if (!async_extent->pages) {
783 int page_started = 0;
784 unsigned long nr_written = 0;
786 /* allocate blocks */
787 ret = cow_file_range(inode, async_chunk->locked_page,
789 async_extent->start +
790 async_extent->ram_size - 1,
791 &page_started, &nr_written, 0);
796 * if page_started, cow_file_range inserted an
797 * inline extent and took care of all the unlocking
798 * and IO for us. Otherwise, we need to submit
799 * all those pages down to the drive.
801 if (!page_started && !ret)
802 extent_write_locked_range(inode,
804 async_extent->start +
805 async_extent->ram_size - 1,
807 else if (ret && async_chunk->locked_page)
808 unlock_page(async_chunk->locked_page);
814 ret = btrfs_reserve_extent(root, async_extent->ram_size,
815 async_extent->compressed_size,
816 async_extent->compressed_size,
817 0, alloc_hint, &ins, 1, 1);
819 free_async_extent_pages(async_extent);
821 if (ret == -ENOSPC) {
822 unlock_extent(io_tree, async_extent->start,
823 async_extent->start +
824 async_extent->ram_size - 1);
827 * we need to redirty the pages if we decide to
828 * fallback to uncompressed IO, otherwise we
829 * will not submit these pages down to lower
832 extent_range_redirty_for_io(inode,
834 async_extent->start +
835 async_extent->ram_size - 1);
842 * here we're doing allocation and writeback of the
845 em = create_io_em(inode, async_extent->start,
846 async_extent->ram_size, /* len */
847 async_extent->start, /* orig_start */
848 ins.objectid, /* block_start */
849 ins.offset, /* block_len */
850 ins.offset, /* orig_block_len */
851 async_extent->ram_size, /* ram_bytes */
852 async_extent->compress_type,
853 BTRFS_ORDERED_COMPRESSED);
855 /* ret value is not necessary due to void function */
856 goto out_free_reserve;
859 ret = btrfs_add_ordered_extent_compress(inode,
862 async_extent->ram_size,
864 BTRFS_ORDERED_COMPRESSED,
865 async_extent->compress_type);
867 btrfs_drop_extent_cache(BTRFS_I(inode),
869 async_extent->start +
870 async_extent->ram_size - 1, 0);
871 goto out_free_reserve;
873 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
876 * clear dirty, set writeback and unlock the pages.
878 extent_clear_unlock_delalloc(inode, async_extent->start,
879 async_extent->start +
880 async_extent->ram_size - 1,
881 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
882 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
884 if (btrfs_submit_compressed_write(inode,
886 async_extent->ram_size,
888 ins.offset, async_extent->pages,
889 async_extent->nr_pages,
890 async_chunk->write_flags)) {
891 struct page *p = async_extent->pages[0];
892 const u64 start = async_extent->start;
893 const u64 end = start + async_extent->ram_size - 1;
895 p->mapping = inode->i_mapping;
896 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
899 extent_clear_unlock_delalloc(inode, start, end,
903 free_async_extent_pages(async_extent);
905 alloc_hint = ins.objectid + ins.offset;
911 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
912 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
914 extent_clear_unlock_delalloc(inode, async_extent->start,
915 async_extent->start +
916 async_extent->ram_size - 1,
917 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
918 EXTENT_DELALLOC_NEW |
919 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
920 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
921 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
923 free_async_extent_pages(async_extent);
928 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
931 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
932 struct extent_map *em;
935 read_lock(&em_tree->lock);
936 em = search_extent_mapping(em_tree, start, num_bytes);
939 * if block start isn't an actual block number then find the
940 * first block in this inode and use that as a hint. If that
941 * block is also bogus then just don't worry about it.
943 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
945 em = search_extent_mapping(em_tree, 0, 0);
946 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
947 alloc_hint = em->block_start;
951 alloc_hint = em->block_start;
955 read_unlock(&em_tree->lock);
961 * when extent_io.c finds a delayed allocation range in the file,
962 * the call backs end up in this code. The basic idea is to
963 * allocate extents on disk for the range, and create ordered data structs
964 * in ram to track those extents.
966 * locked_page is the page that writepage had locked already. We use
967 * it to make sure we don't do extra locks or unlocks.
969 * *page_started is set to one if we unlock locked_page and do everything
970 * required to start IO on it. It may be clean and already done with
973 static noinline int cow_file_range(struct inode *inode,
974 struct page *locked_page,
975 u64 start, u64 end, int *page_started,
976 unsigned long *nr_written, int unlock)
978 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
979 struct btrfs_root *root = BTRFS_I(inode)->root;
982 unsigned long ram_size;
983 u64 cur_alloc_size = 0;
985 u64 blocksize = fs_info->sectorsize;
986 struct btrfs_key ins;
987 struct extent_map *em;
989 unsigned long page_ops;
990 bool extent_reserved = false;
993 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
999 num_bytes = ALIGN(end - start + 1, blocksize);
1000 num_bytes = max(blocksize, num_bytes);
1001 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
1003 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
1006 /* lets try to make an inline extent */
1007 ret = cow_file_range_inline(inode, start, end, 0,
1008 BTRFS_COMPRESS_NONE, NULL);
1011 * We use DO_ACCOUNTING here because we need the
1012 * delalloc_release_metadata to be run _after_ we drop
1013 * our outstanding extent for clearing delalloc for this
1016 extent_clear_unlock_delalloc(inode, start, end, NULL,
1017 EXTENT_LOCKED | EXTENT_DELALLOC |
1018 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1019 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1020 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1021 PAGE_END_WRITEBACK);
1022 *nr_written = *nr_written +
1023 (end - start + PAGE_SIZE) / PAGE_SIZE;
1026 } else if (ret < 0) {
1031 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1032 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1033 start + num_bytes - 1, 0);
1036 * Relocation relies on the relocated extents to have exactly the same
1037 * size as the original extents. Normally writeback for relocation data
1038 * extents follows a NOCOW path because relocation preallocates the
1039 * extents. However, due to an operation such as scrub turning a block
1040 * group to RO mode, it may fallback to COW mode, so we must make sure
1041 * an extent allocated during COW has exactly the requested size and can
1042 * not be split into smaller extents, otherwise relocation breaks and
1043 * fails during the stage where it updates the bytenr of file extent
1046 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1047 min_alloc_size = num_bytes;
1049 min_alloc_size = fs_info->sectorsize;
1051 while (num_bytes > 0) {
1052 cur_alloc_size = num_bytes;
1053 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
1054 min_alloc_size, 0, alloc_hint,
1058 cur_alloc_size = ins.offset;
1059 extent_reserved = true;
1061 ram_size = ins.offset;
1062 em = create_io_em(inode, start, ins.offset, /* len */
1063 start, /* orig_start */
1064 ins.objectid, /* block_start */
1065 ins.offset, /* block_len */
1066 ins.offset, /* orig_block_len */
1067 ram_size, /* ram_bytes */
1068 BTRFS_COMPRESS_NONE, /* compress_type */
1069 BTRFS_ORDERED_REGULAR /* type */);
1074 free_extent_map(em);
1076 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1077 ram_size, cur_alloc_size, 0);
1079 goto out_drop_extent_cache;
1081 if (root->root_key.objectid ==
1082 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1083 ret = btrfs_reloc_clone_csums(inode, start,
1086 * Only drop cache here, and process as normal.
1088 * We must not allow extent_clear_unlock_delalloc()
1089 * at out_unlock label to free meta of this ordered
1090 * extent, as its meta should be freed by
1091 * btrfs_finish_ordered_io().
1093 * So we must continue until @start is increased to
1094 * skip current ordered extent.
1097 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1098 start + ram_size - 1, 0);
1101 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1103 /* we're not doing compressed IO, don't unlock the first
1104 * page (which the caller expects to stay locked), don't
1105 * clear any dirty bits and don't set any writeback bits
1107 * Do set the Private2 bit so we know this page was properly
1108 * setup for writepage
1110 page_ops = unlock ? PAGE_UNLOCK : 0;
1111 page_ops |= PAGE_SET_PRIVATE2;
1113 extent_clear_unlock_delalloc(inode, start,
1114 start + ram_size - 1,
1116 EXTENT_LOCKED | EXTENT_DELALLOC,
1118 if (num_bytes < cur_alloc_size)
1121 num_bytes -= cur_alloc_size;
1122 alloc_hint = ins.objectid + ins.offset;
1123 start += cur_alloc_size;
1124 extent_reserved = false;
1127 * btrfs_reloc_clone_csums() error, since start is increased
1128 * extent_clear_unlock_delalloc() at out_unlock label won't
1129 * free metadata of current ordered extent, we're OK to exit.
1137 out_drop_extent_cache:
1138 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
1140 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1141 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1143 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1144 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
1145 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1148 * If we reserved an extent for our delalloc range (or a subrange) and
1149 * failed to create the respective ordered extent, then it means that
1150 * when we reserved the extent we decremented the extent's size from
1151 * the data space_info's bytes_may_use counter and incremented the
1152 * space_info's bytes_reserved counter by the same amount. We must make
1153 * sure extent_clear_unlock_delalloc() does not try to decrement again
1154 * the data space_info's bytes_may_use counter, therefore we do not pass
1155 * it the flag EXTENT_CLEAR_DATA_RESV.
1157 if (extent_reserved) {
1158 extent_clear_unlock_delalloc(inode, start,
1159 start + cur_alloc_size - 1,
1163 start += cur_alloc_size;
1167 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1168 clear_bits | EXTENT_CLEAR_DATA_RESV,
1174 * work queue call back to started compression on a file and pages
1176 static noinline void async_cow_start(struct btrfs_work *work)
1178 struct async_chunk *async_chunk;
1179 int compressed_extents;
1181 async_chunk = container_of(work, struct async_chunk, work);
1183 compressed_extents = compress_file_range(async_chunk);
1184 if (compressed_extents == 0) {
1185 btrfs_add_delayed_iput(async_chunk->inode);
1186 async_chunk->inode = NULL;
1191 * work queue call back to submit previously compressed pages
1193 static noinline void async_cow_submit(struct btrfs_work *work)
1195 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1197 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
1198 unsigned long nr_pages;
1200 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
1204 * ->inode could be NULL if async_chunk_start has failed to compress,
1205 * in which case we don't have anything to submit, yet we need to
1206 * always adjust ->async_delalloc_pages as its paired with the init
1207 * happening in cow_file_range_async
1209 if (async_chunk->inode)
1210 submit_compressed_extents(async_chunk);
1212 /* atomic_sub_return implies a barrier */
1213 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1215 cond_wake_up_nomb(&fs_info->async_submit_wait);
1218 static noinline void async_cow_free(struct btrfs_work *work)
1220 struct async_chunk *async_chunk;
1222 async_chunk = container_of(work, struct async_chunk, work);
1223 if (async_chunk->inode)
1224 btrfs_add_delayed_iput(async_chunk->inode);
1226 * Since the pointer to 'pending' is at the beginning of the array of
1227 * async_chunk's, freeing it ensures the whole array has been freed.
1229 if (atomic_dec_and_test(async_chunk->pending))
1230 kvfree(async_chunk->pending);
1233 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1234 u64 start, u64 end, int *page_started,
1235 unsigned long *nr_written,
1236 unsigned int write_flags)
1238 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1239 struct async_cow *ctx;
1240 struct async_chunk *async_chunk;
1241 unsigned long nr_pages;
1243 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1245 bool should_compress;
1248 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
1250 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1251 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1253 should_compress = false;
1255 should_compress = true;
1258 nofs_flag = memalloc_nofs_save();
1259 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1260 memalloc_nofs_restore(nofs_flag);
1263 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1264 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1265 EXTENT_DO_ACCOUNTING;
1266 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1267 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1270 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1271 clear_bits, page_ops);
1275 async_chunk = ctx->chunks;
1276 atomic_set(&ctx->num_chunks, num_chunks);
1278 for (i = 0; i < num_chunks; i++) {
1279 if (should_compress)
1280 cur_end = min(end, start + SZ_512K - 1);
1285 * igrab is called higher up in the call chain, take only the
1286 * lightweight reference for the callback lifetime
1289 async_chunk[i].pending = &ctx->num_chunks;
1290 async_chunk[i].inode = inode;
1291 async_chunk[i].start = start;
1292 async_chunk[i].end = cur_end;
1293 async_chunk[i].write_flags = write_flags;
1294 INIT_LIST_HEAD(&async_chunk[i].extents);
1297 * The locked_page comes all the way from writepage and its
1298 * the original page we were actually given. As we spread
1299 * this large delalloc region across multiple async_chunk
1300 * structs, only the first struct needs a pointer to locked_page
1302 * This way we don't need racey decisions about who is supposed
1306 async_chunk[i].locked_page = locked_page;
1309 async_chunk[i].locked_page = NULL;
1312 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1313 async_cow_submit, async_cow_free);
1315 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
1316 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1318 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
1320 *nr_written += nr_pages;
1321 start = cur_end + 1;
1327 static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
1328 u64 bytenr, u64 num_bytes)
1331 struct btrfs_ordered_sum *sums;
1334 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
1335 bytenr + num_bytes - 1, &list, 0);
1336 if (ret == 0 && list_empty(&list))
1339 while (!list_empty(&list)) {
1340 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1341 list_del(&sums->list);
1349 static int fallback_to_cow(struct inode *inode, struct page *locked_page,
1350 const u64 start, const u64 end,
1351 int *page_started, unsigned long *nr_written)
1353 const bool is_space_ino = btrfs_is_free_space_inode(BTRFS_I(inode));
1354 const bool is_reloc_ino = (BTRFS_I(inode)->root->root_key.objectid ==
1355 BTRFS_DATA_RELOC_TREE_OBJECTID);
1356 const u64 range_bytes = end + 1 - start;
1357 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1358 u64 range_start = start;
1362 * If EXTENT_NORESERVE is set it means that when the buffered write was
1363 * made we had not enough available data space and therefore we did not
1364 * reserve data space for it, since we though we could do NOCOW for the
1365 * respective file range (either there is prealloc extent or the inode
1366 * has the NOCOW bit set).
1368 * However when we need to fallback to COW mode (because for example the
1369 * block group for the corresponding extent was turned to RO mode by a
1370 * scrub or relocation) we need to do the following:
1372 * 1) We increment the bytes_may_use counter of the data space info.
1373 * If COW succeeds, it allocates a new data extent and after doing
1374 * that it decrements the space info's bytes_may_use counter and
1375 * increments its bytes_reserved counter by the same amount (we do
1376 * this at btrfs_add_reserved_bytes()). So we need to increment the
1377 * bytes_may_use counter to compensate (when space is reserved at
1378 * buffered write time, the bytes_may_use counter is incremented);
1380 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1381 * that if the COW path fails for any reason, it decrements (through
1382 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1383 * data space info, which we incremented in the step above.
1385 * If we need to fallback to cow and the inode corresponds to a free
1386 * space cache inode or an inode of the data relocation tree, we must
1387 * also increment bytes_may_use of the data space_info for the same
1388 * reason. Space caches and relocated data extents always get a prealloc
1389 * extent for them, however scrub or balance may have set the block
1390 * group that contains that extent to RO mode and therefore force COW
1391 * when starting writeback.
1393 count = count_range_bits(io_tree, &range_start, end, range_bytes,
1394 EXTENT_NORESERVE, 0);
1395 if (count > 0 || is_space_ino || is_reloc_ino) {
1397 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1398 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1400 if (is_space_ino || is_reloc_ino)
1401 bytes = range_bytes;
1403 spin_lock(&sinfo->lock);
1404 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
1405 spin_unlock(&sinfo->lock);
1408 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1412 return cow_file_range(inode, locked_page, start, end, page_started,
1417 * when nowcow writeback call back. This checks for snapshots or COW copies
1418 * of the extents that exist in the file, and COWs the file as required.
1420 * If no cow copies or snapshots exist, we write directly to the existing
1423 static noinline int run_delalloc_nocow(struct inode *inode,
1424 struct page *locked_page,
1425 const u64 start, const u64 end,
1426 int *page_started, int force,
1427 unsigned long *nr_written)
1429 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1430 struct btrfs_root *root = BTRFS_I(inode)->root;
1431 struct btrfs_path *path;
1432 u64 cow_start = (u64)-1;
1433 u64 cur_offset = start;
1435 bool check_prev = true;
1436 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
1437 u64 ino = btrfs_ino(BTRFS_I(inode));
1439 u64 disk_bytenr = 0;
1441 path = btrfs_alloc_path();
1443 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1444 EXTENT_LOCKED | EXTENT_DELALLOC |
1445 EXTENT_DO_ACCOUNTING |
1446 EXTENT_DEFRAG, PAGE_UNLOCK |
1448 PAGE_SET_WRITEBACK |
1449 PAGE_END_WRITEBACK);
1454 struct btrfs_key found_key;
1455 struct btrfs_file_extent_item *fi;
1456 struct extent_buffer *leaf;
1466 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
1472 * If there is no extent for our range when doing the initial
1473 * search, then go back to the previous slot as it will be the
1474 * one containing the search offset
1476 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1477 leaf = path->nodes[0];
1478 btrfs_item_key_to_cpu(leaf, &found_key,
1479 path->slots[0] - 1);
1480 if (found_key.objectid == ino &&
1481 found_key.type == BTRFS_EXTENT_DATA_KEY)
1486 /* Go to next leaf if we have exhausted the current one */
1487 leaf = path->nodes[0];
1488 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1489 ret = btrfs_next_leaf(root, path);
1491 if (cow_start != (u64)-1)
1492 cur_offset = cow_start;
1497 leaf = path->nodes[0];
1500 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1502 /* Didn't find anything for our INO */
1503 if (found_key.objectid > ino)
1506 * Keep searching until we find an EXTENT_ITEM or there are no
1507 * more extents for this inode
1509 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1510 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1515 /* Found key is not EXTENT_DATA_KEY or starts after req range */
1516 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
1517 found_key.offset > end)
1521 * If the found extent starts after requested offset, then
1522 * adjust extent_end to be right before this extent begins
1524 if (found_key.offset > cur_offset) {
1525 extent_end = found_key.offset;
1531 * Found extent which begins before our range and potentially
1534 fi = btrfs_item_ptr(leaf, path->slots[0],
1535 struct btrfs_file_extent_item);
1536 extent_type = btrfs_file_extent_type(leaf, fi);
1538 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1539 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1540 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1541 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1542 extent_offset = btrfs_file_extent_offset(leaf, fi);
1543 extent_end = found_key.offset +
1544 btrfs_file_extent_num_bytes(leaf, fi);
1546 btrfs_file_extent_disk_num_bytes(leaf, fi);
1548 * If the extent we got ends before our current offset,
1549 * skip to the next extent.
1551 if (extent_end <= cur_offset) {
1556 if (disk_bytenr == 0)
1558 /* Skip compressed/encrypted/encoded extents */
1559 if (btrfs_file_extent_compression(leaf, fi) ||
1560 btrfs_file_extent_encryption(leaf, fi) ||
1561 btrfs_file_extent_other_encoding(leaf, fi))
1564 * If extent is created before the last volume's snapshot
1565 * this implies the extent is shared, hence we can't do
1566 * nocow. This is the same check as in
1567 * btrfs_cross_ref_exist but without calling
1568 * btrfs_search_slot.
1570 if (!freespace_inode &&
1571 btrfs_file_extent_generation(leaf, fi) <=
1572 btrfs_root_last_snapshot(&root->root_item))
1574 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1576 /* If extent is RO, we must COW it */
1577 if (btrfs_extent_readonly(fs_info, disk_bytenr))
1579 ret = btrfs_cross_ref_exist(root, ino,
1581 extent_offset, disk_bytenr, false);
1584 * ret could be -EIO if the above fails to read
1588 if (cow_start != (u64)-1)
1589 cur_offset = cow_start;
1593 WARN_ON_ONCE(freespace_inode);
1596 disk_bytenr += extent_offset;
1597 disk_bytenr += cur_offset - found_key.offset;
1598 num_bytes = min(end + 1, extent_end) - cur_offset;
1600 * If there are pending snapshots for this root, we
1601 * fall into common COW way
1603 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
1606 * force cow if csum exists in the range.
1607 * this ensure that csum for a given extent are
1608 * either valid or do not exist.
1610 ret = csum_exist_in_range(fs_info, disk_bytenr,
1614 * ret could be -EIO if the above fails to read
1618 if (cow_start != (u64)-1)
1619 cur_offset = cow_start;
1622 WARN_ON_ONCE(freespace_inode);
1625 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
1628 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1629 extent_end = found_key.offset + ram_bytes;
1630 extent_end = ALIGN(extent_end, fs_info->sectorsize);
1631 /* Skip extents outside of our requested range */
1632 if (extent_end <= start) {
1637 /* If this triggers then we have a memory corruption */
1642 * If nocow is false then record the beginning of the range
1643 * that needs to be COWed
1646 if (cow_start == (u64)-1)
1647 cow_start = cur_offset;
1648 cur_offset = extent_end;
1649 if (cur_offset > end)
1655 btrfs_release_path(path);
1658 * COW range from cow_start to found_key.offset - 1. As the key
1659 * will contain the beginning of the first extent that can be
1660 * NOCOW, following one which needs to be COW'ed
1662 if (cow_start != (u64)-1) {
1663 ret = fallback_to_cow(inode, locked_page, cow_start,
1664 found_key.offset - 1,
1665 page_started, nr_written);
1668 cow_start = (u64)-1;
1671 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1672 u64 orig_start = found_key.offset - extent_offset;
1673 struct extent_map *em;
1675 em = create_io_em(inode, cur_offset, num_bytes,
1677 disk_bytenr, /* block_start */
1678 num_bytes, /* block_len */
1679 disk_num_bytes, /* orig_block_len */
1680 ram_bytes, BTRFS_COMPRESS_NONE,
1681 BTRFS_ORDERED_PREALLOC);
1686 free_extent_map(em);
1687 ret = btrfs_add_ordered_extent(inode, cur_offset,
1688 disk_bytenr, num_bytes,
1690 BTRFS_ORDERED_PREALLOC);
1692 btrfs_drop_extent_cache(BTRFS_I(inode),
1694 cur_offset + num_bytes - 1,
1699 ret = btrfs_add_ordered_extent(inode, cur_offset,
1700 disk_bytenr, num_bytes,
1702 BTRFS_ORDERED_NOCOW);
1708 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1711 if (root->root_key.objectid ==
1712 BTRFS_DATA_RELOC_TREE_OBJECTID)
1714 * Error handled later, as we must prevent
1715 * extent_clear_unlock_delalloc() in error handler
1716 * from freeing metadata of created ordered extent.
1718 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1721 extent_clear_unlock_delalloc(inode, cur_offset,
1722 cur_offset + num_bytes - 1,
1723 locked_page, EXTENT_LOCKED |
1725 EXTENT_CLEAR_DATA_RESV,
1726 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1728 cur_offset = extent_end;
1731 * btrfs_reloc_clone_csums() error, now we're OK to call error
1732 * handler, as metadata for created ordered extent will only
1733 * be freed by btrfs_finish_ordered_io().
1737 if (cur_offset > end)
1740 btrfs_release_path(path);
1742 if (cur_offset <= end && cow_start == (u64)-1)
1743 cow_start = cur_offset;
1745 if (cow_start != (u64)-1) {
1747 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1748 page_started, nr_written);
1755 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1757 if (ret && cur_offset < end)
1758 extent_clear_unlock_delalloc(inode, cur_offset, end,
1759 locked_page, EXTENT_LOCKED |
1760 EXTENT_DELALLOC | EXTENT_DEFRAG |
1761 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1763 PAGE_SET_WRITEBACK |
1764 PAGE_END_WRITEBACK);
1765 btrfs_free_path(path);
1769 static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1772 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1773 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1777 * @defrag_bytes is a hint value, no spinlock held here,
1778 * if is not zero, it means the file is defragging.
1779 * Force cow if given extent needs to be defragged.
1781 if (BTRFS_I(inode)->defrag_bytes &&
1782 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1783 EXTENT_DEFRAG, 0, NULL))
1790 * Function to process delayed allocation (create CoW) for ranges which are
1791 * being touched for the first time.
1793 int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
1794 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1795 struct writeback_control *wbc)
1798 int force_cow = need_force_cow(inode, start, end);
1799 unsigned int write_flags = wbc_to_write_flags(wbc);
1801 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1802 ret = run_delalloc_nocow(inode, locked_page, start, end,
1803 page_started, 1, nr_written);
1804 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1805 ret = run_delalloc_nocow(inode, locked_page, start, end,
1806 page_started, 0, nr_written);
1807 } else if (!inode_can_compress(inode) ||
1808 !inode_need_compress(inode, start, end)) {
1809 ret = cow_file_range(inode, locked_page, start, end,
1810 page_started, nr_written, 1);
1812 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1813 &BTRFS_I(inode)->runtime_flags);
1814 ret = cow_file_range_async(inode, locked_page, start, end,
1815 page_started, nr_written,
1819 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1824 void btrfs_split_delalloc_extent(struct inode *inode,
1825 struct extent_state *orig, u64 split)
1829 /* not delalloc, ignore it */
1830 if (!(orig->state & EXTENT_DELALLOC))
1833 size = orig->end - orig->start + 1;
1834 if (size > BTRFS_MAX_EXTENT_SIZE) {
1839 * See the explanation in btrfs_merge_delalloc_extent, the same
1840 * applies here, just in reverse.
1842 new_size = orig->end - split + 1;
1843 num_extents = count_max_extents(new_size);
1844 new_size = split - orig->start;
1845 num_extents += count_max_extents(new_size);
1846 if (count_max_extents(size) >= num_extents)
1850 spin_lock(&BTRFS_I(inode)->lock);
1851 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
1852 spin_unlock(&BTRFS_I(inode)->lock);
1856 * Handle merged delayed allocation extents so we can keep track of new extents
1857 * that are just merged onto old extents, such as when we are doing sequential
1858 * writes, so we can properly account for the metadata space we'll need.
1860 void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1861 struct extent_state *other)
1863 u64 new_size, old_size;
1866 /* not delalloc, ignore it */
1867 if (!(other->state & EXTENT_DELALLOC))
1870 if (new->start > other->start)
1871 new_size = new->end - other->start + 1;
1873 new_size = other->end - new->start + 1;
1875 /* we're not bigger than the max, unreserve the space and go */
1876 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1877 spin_lock(&BTRFS_I(inode)->lock);
1878 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
1879 spin_unlock(&BTRFS_I(inode)->lock);
1884 * We have to add up either side to figure out how many extents were
1885 * accounted for before we merged into one big extent. If the number of
1886 * extents we accounted for is <= the amount we need for the new range
1887 * then we can return, otherwise drop. Think of it like this
1891 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1892 * need 2 outstanding extents, on one side we have 1 and the other side
1893 * we have 1 so they are == and we can return. But in this case
1895 * [MAX_SIZE+4k][MAX_SIZE+4k]
1897 * Each range on their own accounts for 2 extents, but merged together
1898 * they are only 3 extents worth of accounting, so we need to drop in
1901 old_size = other->end - other->start + 1;
1902 num_extents = count_max_extents(old_size);
1903 old_size = new->end - new->start + 1;
1904 num_extents += count_max_extents(old_size);
1905 if (count_max_extents(new_size) >= num_extents)
1908 spin_lock(&BTRFS_I(inode)->lock);
1909 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
1910 spin_unlock(&BTRFS_I(inode)->lock);
1913 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1914 struct inode *inode)
1916 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1918 spin_lock(&root->delalloc_lock);
1919 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1920 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1921 &root->delalloc_inodes);
1922 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1923 &BTRFS_I(inode)->runtime_flags);
1924 root->nr_delalloc_inodes++;
1925 if (root->nr_delalloc_inodes == 1) {
1926 spin_lock(&fs_info->delalloc_root_lock);
1927 BUG_ON(!list_empty(&root->delalloc_root));
1928 list_add_tail(&root->delalloc_root,
1929 &fs_info->delalloc_roots);
1930 spin_unlock(&fs_info->delalloc_root_lock);
1933 spin_unlock(&root->delalloc_lock);
1937 void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1938 struct btrfs_inode *inode)
1940 struct btrfs_fs_info *fs_info = root->fs_info;
1942 if (!list_empty(&inode->delalloc_inodes)) {
1943 list_del_init(&inode->delalloc_inodes);
1944 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1945 &inode->runtime_flags);
1946 root->nr_delalloc_inodes--;
1947 if (!root->nr_delalloc_inodes) {
1948 ASSERT(list_empty(&root->delalloc_inodes));
1949 spin_lock(&fs_info->delalloc_root_lock);
1950 BUG_ON(list_empty(&root->delalloc_root));
1951 list_del_init(&root->delalloc_root);
1952 spin_unlock(&fs_info->delalloc_root_lock);
1957 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1958 struct btrfs_inode *inode)
1960 spin_lock(&root->delalloc_lock);
1961 __btrfs_del_delalloc_inode(root, inode);
1962 spin_unlock(&root->delalloc_lock);
1966 * Properly track delayed allocation bytes in the inode and to maintain the
1967 * list of inodes that have pending delalloc work to be done.
1969 void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1972 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1974 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1977 * set_bit and clear bit hooks normally require _irqsave/restore
1978 * but in this case, we are only testing for the DELALLOC
1979 * bit, which is only set or cleared with irqs on
1981 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1982 struct btrfs_root *root = BTRFS_I(inode)->root;
1983 u64 len = state->end + 1 - state->start;
1984 u32 num_extents = count_max_extents(len);
1985 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
1987 spin_lock(&BTRFS_I(inode)->lock);
1988 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1989 spin_unlock(&BTRFS_I(inode)->lock);
1991 /* For sanity tests */
1992 if (btrfs_is_testing(fs_info))
1995 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1996 fs_info->delalloc_batch);
1997 spin_lock(&BTRFS_I(inode)->lock);
1998 BTRFS_I(inode)->delalloc_bytes += len;
1999 if (*bits & EXTENT_DEFRAG)
2000 BTRFS_I(inode)->defrag_bytes += len;
2001 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2002 &BTRFS_I(inode)->runtime_flags))
2003 btrfs_add_delalloc_inodes(root, inode);
2004 spin_unlock(&BTRFS_I(inode)->lock);
2007 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2008 (*bits & EXTENT_DELALLOC_NEW)) {
2009 spin_lock(&BTRFS_I(inode)->lock);
2010 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2012 spin_unlock(&BTRFS_I(inode)->lock);
2017 * Once a range is no longer delalloc this function ensures that proper
2018 * accounting happens.
2020 void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2021 struct extent_state *state, unsigned *bits)
2023 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2024 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
2025 u64 len = state->end + 1 - state->start;
2026 u32 num_extents = count_max_extents(len);
2028 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2029 spin_lock(&inode->lock);
2030 inode->defrag_bytes -= len;
2031 spin_unlock(&inode->lock);
2035 * set_bit and clear bit hooks normally require _irqsave/restore
2036 * but in this case, we are only testing for the DELALLOC
2037 * bit, which is only set or cleared with irqs on
2039 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
2040 struct btrfs_root *root = inode->root;
2041 bool do_list = !btrfs_is_free_space_inode(inode);
2043 spin_lock(&inode->lock);
2044 btrfs_mod_outstanding_extents(inode, -num_extents);
2045 spin_unlock(&inode->lock);
2048 * We don't reserve metadata space for space cache inodes so we
2049 * don't need to call delalloc_release_metadata if there is an
2052 if (*bits & EXTENT_CLEAR_META_RESV &&
2053 root != fs_info->tree_root)
2054 btrfs_delalloc_release_metadata(inode, len, false);
2056 /* For sanity tests. */
2057 if (btrfs_is_testing(fs_info))
2060 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2061 do_list && !(state->state & EXTENT_NORESERVE) &&
2062 (*bits & EXTENT_CLEAR_DATA_RESV))
2063 btrfs_free_reserved_data_space_noquota(
2067 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2068 fs_info->delalloc_batch);
2069 spin_lock(&inode->lock);
2070 inode->delalloc_bytes -= len;
2071 if (do_list && inode->delalloc_bytes == 0 &&
2072 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2073 &inode->runtime_flags))
2074 btrfs_del_delalloc_inode(root, inode);
2075 spin_unlock(&inode->lock);
2078 if ((state->state & EXTENT_DELALLOC_NEW) &&
2079 (*bits & EXTENT_DELALLOC_NEW)) {
2080 spin_lock(&inode->lock);
2081 ASSERT(inode->new_delalloc_bytes >= len);
2082 inode->new_delalloc_bytes -= len;
2083 spin_unlock(&inode->lock);
2088 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2089 * in a chunk's stripe. This function ensures that bios do not span a
2092 * @page - The page we are about to add to the bio
2093 * @size - size we want to add to the bio
2094 * @bio - bio we want to ensure is smaller than a stripe
2095 * @bio_flags - flags of the bio
2097 * return 1 if page cannot be added to the bio
2098 * return 0 if page can be added to the bio
2099 * return error otherwise
2101 int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2102 unsigned long bio_flags)
2104 struct inode *inode = page->mapping->host;
2105 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2106 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
2110 struct btrfs_io_geometry geom;
2112 if (bio_flags & EXTENT_BIO_COMPRESSED)
2115 length = bio->bi_iter.bi_size;
2116 map_length = length;
2117 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2122 if (geom.len < length + size)
2128 * in order to insert checksums into the metadata in large chunks,
2129 * we wait until bio submission time. All the pages in the bio are
2130 * checksummed and sums are attached onto the ordered extent record.
2132 * At IO completion time the cums attached on the ordered extent record
2133 * are inserted into the btree
2135 static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
2138 struct inode *inode = private_data;
2139 blk_status_t ret = 0;
2141 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
2142 BUG_ON(ret); /* -ENOMEM */
2147 * extent_io.c submission hook. This does the right thing for csum calculation
2148 * on write, or reading the csums from the tree before a read.
2150 * Rules about async/sync submit,
2151 * a) read: sync submit
2153 * b) write without checksum: sync submit
2155 * c) write with checksum:
2156 * c-1) if bio is issued by fsync: sync submit
2157 * (sync_writers != 0)
2159 * c-2) if root is reloc root: sync submit
2160 * (only in case of buffered IO)
2162 * c-3) otherwise: async submit
2164 static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
2166 unsigned long bio_flags)
2169 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2170 struct btrfs_root *root = BTRFS_I(inode)->root;
2171 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
2172 blk_status_t ret = 0;
2174 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
2176 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
2178 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
2179 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
2181 if (bio_op(bio) != REQ_OP_WRITE) {
2182 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
2186 if (bio_flags & EXTENT_BIO_COMPRESSED) {
2187 ret = btrfs_submit_compressed_read(inode, bio,
2191 } else if (!skip_sum) {
2192 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
2197 } else if (async && !skip_sum) {
2198 /* csum items have already been cloned */
2199 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2201 /* we're doing a write, do the async checksumming */
2202 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
2203 0, inode, btrfs_submit_bio_start);
2205 } else if (!skip_sum) {
2206 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
2212 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
2216 bio->bi_status = ret;
2223 * given a list of ordered sums record them in the inode. This happens
2224 * at IO completion time based on sums calculated at bio submission time.
2226 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
2227 struct inode *inode, struct list_head *list)
2229 struct btrfs_ordered_sum *sum;
2232 list_for_each_entry(sum, list, list) {
2233 trans->adding_csums = true;
2234 ret = btrfs_csum_file_blocks(trans,
2235 BTRFS_I(inode)->root->fs_info->csum_root, sum);
2236 trans->adding_csums = false;
2243 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
2244 unsigned int extra_bits,
2245 struct extent_state **cached_state)
2247 WARN_ON(PAGE_ALIGNED(end));
2248 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
2249 extra_bits, cached_state);
2252 /* see btrfs_writepage_start_hook for details on why this is required */
2253 struct btrfs_writepage_fixup {
2255 struct inode *inode;
2256 struct btrfs_work work;
2259 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
2261 struct btrfs_writepage_fixup *fixup;
2262 struct btrfs_ordered_extent *ordered;
2263 struct extent_state *cached_state = NULL;
2264 struct extent_changeset *data_reserved = NULL;
2266 struct inode *inode;
2270 bool free_delalloc_space = true;
2272 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2274 inode = fixup->inode;
2275 page_start = page_offset(page);
2276 page_end = page_offset(page) + PAGE_SIZE - 1;
2279 * This is similar to page_mkwrite, we need to reserve the space before
2280 * we take the page lock.
2282 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2288 * Before we queued this fixup, we took a reference on the page.
2289 * page->mapping may go NULL, but it shouldn't be moved to a different
2292 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2294 * Unfortunately this is a little tricky, either
2296 * 1) We got here and our page had already been dealt with and
2297 * we reserved our space, thus ret == 0, so we need to just
2298 * drop our space reservation and bail. This can happen the
2299 * first time we come into the fixup worker, or could happen
2300 * while waiting for the ordered extent.
2301 * 2) Our page was already dealt with, but we happened to get an
2302 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2303 * this case we obviously don't have anything to release, but
2304 * because the page was already dealt with we don't want to
2305 * mark the page with an error, so make sure we're resetting
2306 * ret to 0. This is why we have this check _before_ the ret
2307 * check, because we do not want to have a surprise ENOSPC
2308 * when the page was already properly dealt with.
2311 btrfs_delalloc_release_extents(BTRFS_I(inode),
2313 btrfs_delalloc_release_space(inode, data_reserved,
2314 page_start, PAGE_SIZE,
2322 * We can't mess with the page state unless it is locked, so now that
2323 * it is locked bail if we failed to make our space reservation.
2328 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
2331 /* already ordered? We're done */
2332 if (PagePrivate2(page))
2335 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
2338 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
2339 page_end, &cached_state);
2341 btrfs_start_ordered_extent(inode, ordered, 1);
2342 btrfs_put_ordered_extent(ordered);
2346 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2352 * Everything went as planned, we're now the owner of a dirty page with
2353 * delayed allocation bits set and space reserved for our COW
2356 * The page was dirty when we started, nothing should have cleaned it.
2358 BUG_ON(!PageDirty(page));
2359 free_delalloc_space = false;
2361 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
2362 if (free_delalloc_space)
2363 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2365 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
2370 * We hit ENOSPC or other errors. Update the mapping and page
2371 * to reflect the errors and clean the page.
2373 mapping_set_error(page->mapping, ret);
2374 end_extent_writepage(page, ret, page_start, page_end);
2375 clear_page_dirty_for_io(page);
2378 ClearPageChecked(page);
2382 extent_changeset_free(data_reserved);
2384 * As a precaution, do a delayed iput in case it would be the last iput
2385 * that could need flushing space. Recursing back to fixup worker would
2388 btrfs_add_delayed_iput(inode);
2392 * There are a few paths in the higher layers of the kernel that directly
2393 * set the page dirty bit without asking the filesystem if it is a
2394 * good idea. This causes problems because we want to make sure COW
2395 * properly happens and the data=ordered rules are followed.
2397 * In our case any range that doesn't have the ORDERED bit set
2398 * hasn't been properly setup for IO. We kick off an async process
2399 * to fix it up. The async helper will wait for ordered extents, set
2400 * the delalloc bit and make it safe to write the page.
2402 int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
2404 struct inode *inode = page->mapping->host;
2405 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2406 struct btrfs_writepage_fixup *fixup;
2408 /* this page is properly in the ordered list */
2409 if (TestClearPagePrivate2(page))
2413 * PageChecked is set below when we create a fixup worker for this page,
2414 * don't try to create another one if we're already PageChecked()
2416 * The extent_io writepage code will redirty the page if we send back
2419 if (PageChecked(page))
2422 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2427 * We are already holding a reference to this inode from
2428 * write_cache_pages. We need to hold it because the space reservation
2429 * takes place outside of the page lock, and we can't trust
2430 * page->mapping outside of the page lock.
2433 SetPageChecked(page);
2435 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
2437 fixup->inode = inode;
2438 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2443 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2444 struct inode *inode, u64 file_pos,
2445 u64 disk_bytenr, u64 disk_num_bytes,
2446 u64 num_bytes, u64 ram_bytes,
2447 u8 compression, u8 encryption,
2448 u16 other_encoding, int extent_type)
2450 struct btrfs_root *root = BTRFS_I(inode)->root;
2451 struct btrfs_file_extent_item *fi;
2452 struct btrfs_path *path;
2453 struct extent_buffer *leaf;
2454 struct btrfs_key ins;
2456 int extent_inserted = 0;
2459 path = btrfs_alloc_path();
2464 * we may be replacing one extent in the tree with another.
2465 * The new extent is pinned in the extent map, and we don't want
2466 * to drop it from the cache until it is completely in the btree.
2468 * So, tell btrfs_drop_extents to leave this extent in the cache.
2469 * the caller is expected to unpin it and allow it to be merged
2472 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2473 file_pos + num_bytes, NULL, 0,
2474 1, sizeof(*fi), &extent_inserted);
2478 if (!extent_inserted) {
2479 ins.objectid = btrfs_ino(BTRFS_I(inode));
2480 ins.offset = file_pos;
2481 ins.type = BTRFS_EXTENT_DATA_KEY;
2483 path->leave_spinning = 1;
2484 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2489 leaf = path->nodes[0];
2490 fi = btrfs_item_ptr(leaf, path->slots[0],
2491 struct btrfs_file_extent_item);
2492 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2493 btrfs_set_file_extent_type(leaf, fi, extent_type);
2494 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2495 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2496 btrfs_set_file_extent_offset(leaf, fi, 0);
2497 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2498 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2499 btrfs_set_file_extent_compression(leaf, fi, compression);
2500 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2501 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
2503 btrfs_mark_buffer_dirty(leaf);
2504 btrfs_release_path(path);
2506 inode_add_bytes(inode, num_bytes);
2508 ins.objectid = disk_bytenr;
2509 ins.offset = disk_num_bytes;
2510 ins.type = BTRFS_EXTENT_ITEM_KEY;
2513 * Release the reserved range from inode dirty range map, as it is
2514 * already moved into delayed_ref_head
2516 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2520 ret = btrfs_alloc_reserved_file_extent(trans, root,
2521 btrfs_ino(BTRFS_I(inode)),
2522 file_pos, qg_released, &ins);
2524 btrfs_free_path(path);
2529 /* snapshot-aware defrag */
2530 struct sa_defrag_extent_backref {
2531 struct rb_node node;
2532 struct old_sa_defrag_extent *old;
2541 struct old_sa_defrag_extent {
2542 struct list_head list;
2543 struct new_sa_defrag_extent *new;
2552 struct new_sa_defrag_extent {
2553 struct rb_root root;
2554 struct list_head head;
2555 struct btrfs_path *path;
2556 struct inode *inode;
2564 static int backref_comp(struct sa_defrag_extent_backref *b1,
2565 struct sa_defrag_extent_backref *b2)
2567 if (b1->root_id < b2->root_id)
2569 else if (b1->root_id > b2->root_id)
2572 if (b1->inum < b2->inum)
2574 else if (b1->inum > b2->inum)
2577 if (b1->file_pos < b2->file_pos)
2579 else if (b1->file_pos > b2->file_pos)
2583 * [------------------------------] ===> (a range of space)
2584 * |<--->| |<---->| =============> (fs/file tree A)
2585 * |<---------------------------->| ===> (fs/file tree B)
2587 * A range of space can refer to two file extents in one tree while
2588 * refer to only one file extent in another tree.
2590 * So we may process a disk offset more than one time(two extents in A)
2591 * and locate at the same extent(one extent in B), then insert two same
2592 * backrefs(both refer to the extent in B).
2597 static void backref_insert(struct rb_root *root,
2598 struct sa_defrag_extent_backref *backref)
2600 struct rb_node **p = &root->rb_node;
2601 struct rb_node *parent = NULL;
2602 struct sa_defrag_extent_backref *entry;
2607 entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2609 ret = backref_comp(backref, entry);
2613 p = &(*p)->rb_right;
2616 rb_link_node(&backref->node, parent, p);
2617 rb_insert_color(&backref->node, root);
2621 * Note the backref might has changed, and in this case we just return 0.
2623 static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2626 struct btrfs_file_extent_item *extent;
2627 struct old_sa_defrag_extent *old = ctx;
2628 struct new_sa_defrag_extent *new = old->new;
2629 struct btrfs_path *path = new->path;
2630 struct btrfs_key key;
2631 struct btrfs_root *root;
2632 struct sa_defrag_extent_backref *backref;
2633 struct extent_buffer *leaf;
2634 struct inode *inode = new->inode;
2635 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2641 if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2642 inum == btrfs_ino(BTRFS_I(inode)))
2645 key.objectid = root_id;
2646 key.type = BTRFS_ROOT_ITEM_KEY;
2647 key.offset = (u64)-1;
2649 root = btrfs_read_fs_root_no_name(fs_info, &key);
2651 if (PTR_ERR(root) == -ENOENT)
2654 btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
2655 inum, offset, root_id);
2656 return PTR_ERR(root);
2659 key.objectid = inum;
2660 key.type = BTRFS_EXTENT_DATA_KEY;
2661 if (offset > (u64)-1 << 32)
2664 key.offset = offset;
2666 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2667 if (WARN_ON(ret < 0))
2674 leaf = path->nodes[0];
2675 slot = path->slots[0];
2677 if (slot >= btrfs_header_nritems(leaf)) {
2678 ret = btrfs_next_leaf(root, path);
2681 } else if (ret > 0) {
2690 btrfs_item_key_to_cpu(leaf, &key, slot);
2692 if (key.objectid > inum)
2695 if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2698 extent = btrfs_item_ptr(leaf, slot,
2699 struct btrfs_file_extent_item);
2701 if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2705 * 'offset' refers to the exact key.offset,
2706 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2707 * (key.offset - extent_offset).
2709 if (key.offset != offset)
2712 extent_offset = btrfs_file_extent_offset(leaf, extent);
2713 num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2715 if (extent_offset >= old->extent_offset + old->offset +
2716 old->len || extent_offset + num_bytes <=
2717 old->extent_offset + old->offset)
2722 backref = kmalloc(sizeof(*backref), GFP_NOFS);
2728 backref->root_id = root_id;
2729 backref->inum = inum;
2730 backref->file_pos = offset;
2731 backref->num_bytes = num_bytes;
2732 backref->extent_offset = extent_offset;
2733 backref->generation = btrfs_file_extent_generation(leaf, extent);
2735 backref_insert(&new->root, backref);
2738 btrfs_release_path(path);
2743 static noinline bool record_extent_backrefs(struct btrfs_path *path,
2744 struct new_sa_defrag_extent *new)
2746 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2747 struct old_sa_defrag_extent *old, *tmp;
2752 list_for_each_entry_safe(old, tmp, &new->head, list) {
2753 ret = iterate_inodes_from_logical(old->bytenr +
2754 old->extent_offset, fs_info,
2755 path, record_one_backref,
2757 if (ret < 0 && ret != -ENOENT)
2760 /* no backref to be processed for this extent */
2762 list_del(&old->list);
2767 if (list_empty(&new->head))
2773 static int relink_is_mergable(struct extent_buffer *leaf,
2774 struct btrfs_file_extent_item *fi,
2775 struct new_sa_defrag_extent *new)
2777 if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
2780 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2783 if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2786 if (btrfs_file_extent_encryption(leaf, fi) ||
2787 btrfs_file_extent_other_encoding(leaf, fi))
2794 * Note the backref might has changed, and in this case we just return 0.
2796 static noinline int relink_extent_backref(struct btrfs_path *path,
2797 struct sa_defrag_extent_backref *prev,
2798 struct sa_defrag_extent_backref *backref)
2800 struct btrfs_file_extent_item *extent;
2801 struct btrfs_file_extent_item *item;
2802 struct btrfs_ordered_extent *ordered;
2803 struct btrfs_trans_handle *trans;
2804 struct btrfs_ref ref = { 0 };
2805 struct btrfs_root *root;
2806 struct btrfs_key key;
2807 struct extent_buffer *leaf;
2808 struct old_sa_defrag_extent *old = backref->old;
2809 struct new_sa_defrag_extent *new = old->new;
2810 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2811 struct inode *inode;
2812 struct extent_state *cached = NULL;
2821 if (prev && prev->root_id == backref->root_id &&
2822 prev->inum == backref->inum &&
2823 prev->file_pos + prev->num_bytes == backref->file_pos)
2826 /* step 1: get root */
2827 key.objectid = backref->root_id;
2828 key.type = BTRFS_ROOT_ITEM_KEY;
2829 key.offset = (u64)-1;
2831 index = srcu_read_lock(&fs_info->subvol_srcu);
2833 root = btrfs_read_fs_root_no_name(fs_info, &key);
2835 srcu_read_unlock(&fs_info->subvol_srcu, index);
2836 if (PTR_ERR(root) == -ENOENT)
2838 return PTR_ERR(root);
2841 if (btrfs_root_readonly(root)) {
2842 srcu_read_unlock(&fs_info->subvol_srcu, index);
2846 /* step 2: get inode */
2847 key.objectid = backref->inum;
2848 key.type = BTRFS_INODE_ITEM_KEY;
2851 inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2852 if (IS_ERR(inode)) {
2853 srcu_read_unlock(&fs_info->subvol_srcu, index);
2857 srcu_read_unlock(&fs_info->subvol_srcu, index);
2859 /* step 3: relink backref */
2860 lock_start = backref->file_pos;
2861 lock_end = backref->file_pos + backref->num_bytes - 1;
2862 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2865 ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2867 btrfs_put_ordered_extent(ordered);
2871 trans = btrfs_join_transaction(root);
2872 if (IS_ERR(trans)) {
2873 ret = PTR_ERR(trans);
2877 key.objectid = backref->inum;
2878 key.type = BTRFS_EXTENT_DATA_KEY;
2879 key.offset = backref->file_pos;
2881 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2884 } else if (ret > 0) {
2889 extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2890 struct btrfs_file_extent_item);
2892 if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2893 backref->generation)
2896 btrfs_release_path(path);
2898 start = backref->file_pos;
2899 if (backref->extent_offset < old->extent_offset + old->offset)
2900 start += old->extent_offset + old->offset -
2901 backref->extent_offset;
2903 len = min(backref->extent_offset + backref->num_bytes,
2904 old->extent_offset + old->offset + old->len);
2905 len -= max(backref->extent_offset, old->extent_offset + old->offset);
2907 ret = btrfs_drop_extents(trans, root, inode, start,
2912 key.objectid = btrfs_ino(BTRFS_I(inode));
2913 key.type = BTRFS_EXTENT_DATA_KEY;
2916 path->leave_spinning = 1;
2918 struct btrfs_file_extent_item *fi;
2920 struct btrfs_key found_key;
2922 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2927 leaf = path->nodes[0];
2928 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2930 fi = btrfs_item_ptr(leaf, path->slots[0],
2931 struct btrfs_file_extent_item);
2932 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2934 if (extent_len + found_key.offset == start &&
2935 relink_is_mergable(leaf, fi, new)) {
2936 btrfs_set_file_extent_num_bytes(leaf, fi,
2938 btrfs_mark_buffer_dirty(leaf);
2939 inode_add_bytes(inode, len);
2945 btrfs_release_path(path);
2950 ret = btrfs_insert_empty_item(trans, root, path, &key,
2953 btrfs_abort_transaction(trans, ret);
2957 leaf = path->nodes[0];
2958 item = btrfs_item_ptr(leaf, path->slots[0],
2959 struct btrfs_file_extent_item);
2960 btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2961 btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2962 btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2963 btrfs_set_file_extent_num_bytes(leaf, item, len);
2964 btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2965 btrfs_set_file_extent_generation(leaf, item, trans->transid);
2966 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2967 btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2968 btrfs_set_file_extent_encryption(leaf, item, 0);
2969 btrfs_set_file_extent_other_encoding(leaf, item, 0);
2971 btrfs_mark_buffer_dirty(leaf);
2972 inode_add_bytes(inode, len);
2973 btrfs_release_path(path);
2975 btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr,
2977 btrfs_init_data_ref(&ref, backref->root_id, backref->inum,
2978 new->file_pos); /* start - extent_offset */
2979 ret = btrfs_inc_extent_ref(trans, &ref);
2981 btrfs_abort_transaction(trans, ret);
2987 btrfs_release_path(path);
2988 path->leave_spinning = 0;
2989 btrfs_end_transaction(trans);
2991 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2997 static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2999 struct old_sa_defrag_extent *old, *tmp;
3004 list_for_each_entry_safe(old, tmp, &new->head, list) {
3010 static void relink_file_extents(struct new_sa_defrag_extent *new)
3012 struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
3013 struct btrfs_path *path;
3014 struct sa_defrag_extent_backref *backref;
3015 struct sa_defrag_extent_backref *prev = NULL;
3016 struct rb_node *node;
3019 path = btrfs_alloc_path();
3023 if (!record_extent_backrefs(path, new)) {
3024 btrfs_free_path(path);
3027 btrfs_release_path(path);
3030 node = rb_first(&new->root);
3033 rb_erase(node, &new->root);
3035 backref = rb_entry(node, struct sa_defrag_extent_backref, node);
3037 ret = relink_extent_backref(path, prev, backref);
3050 btrfs_free_path(path);
3052 free_sa_defrag_extent(new);
3054 atomic_dec(&fs_info->defrag_running);
3055 wake_up(&fs_info->transaction_wait);
3058 static struct new_sa_defrag_extent *
3059 record_old_file_extents(struct inode *inode,
3060 struct btrfs_ordered_extent *ordered)
3062 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3063 struct btrfs_root *root = BTRFS_I(inode)->root;
3064 struct btrfs_path *path;
3065 struct btrfs_key key;
3066 struct old_sa_defrag_extent *old;
3067 struct new_sa_defrag_extent *new;
3070 new = kmalloc(sizeof(*new), GFP_NOFS);
3075 new->file_pos = ordered->file_offset;
3076 new->len = ordered->len;
3077 new->bytenr = ordered->start;
3078 new->disk_len = ordered->disk_len;
3079 new->compress_type = ordered->compress_type;
3080 new->root = RB_ROOT;
3081 INIT_LIST_HEAD(&new->head);
3083 path = btrfs_alloc_path();
3087 key.objectid = btrfs_ino(BTRFS_I(inode));
3088 key.type = BTRFS_EXTENT_DATA_KEY;
3089 key.offset = new->file_pos;
3091 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3094 if (ret > 0 && path->slots[0] > 0)
3097 /* find out all the old extents for the file range */
3099 struct btrfs_file_extent_item *extent;
3100 struct extent_buffer *l;
3109 slot = path->slots[0];
3111 if (slot >= btrfs_header_nritems(l)) {
3112 ret = btrfs_next_leaf(root, path);
3120 btrfs_item_key_to_cpu(l, &key, slot);
3122 if (key.objectid != btrfs_ino(BTRFS_I(inode)))
3124 if (key.type != BTRFS_EXTENT_DATA_KEY)
3126 if (key.offset >= new->file_pos + new->len)
3129 extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
3131 num_bytes = btrfs_file_extent_num_bytes(l, extent);
3132 if (key.offset + num_bytes < new->file_pos)
3135 disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
3139 extent_offset = btrfs_file_extent_offset(l, extent);
3141 old = kmalloc(sizeof(*old), GFP_NOFS);
3145 offset = max(new->file_pos, key.offset);
3146 end = min(new->file_pos + new->len, key.offset + num_bytes);
3148 old->bytenr = disk_bytenr;
3149 old->extent_offset = extent_offset;
3150 old->offset = offset - key.offset;
3151 old->len = end - offset;
3154 list_add_tail(&old->list, &new->head);
3160 btrfs_free_path(path);
3161 atomic_inc(&fs_info->defrag_running);
3166 btrfs_free_path(path);
3168 free_sa_defrag_extent(new);
3172 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
3175 struct btrfs_block_group_cache *cache;
3177 cache = btrfs_lookup_block_group(fs_info, start);
3180 spin_lock(&cache->lock);
3181 cache->delalloc_bytes -= len;
3182 spin_unlock(&cache->lock);
3184 btrfs_put_block_group(cache);
3187 /* as ordered data IO finishes, this gets called so we can finish
3188 * an ordered extent if the range of bytes in the file it covers are
3191 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
3193 struct inode *inode = ordered_extent->inode;
3194 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3195 struct btrfs_root *root = BTRFS_I(inode)->root;
3196 struct btrfs_trans_handle *trans = NULL;
3197 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3198 struct extent_state *cached_state = NULL;
3199 struct new_sa_defrag_extent *new = NULL;
3200 int compress_type = 0;
3202 u64 logical_len = ordered_extent->len;
3204 bool truncated = false;
3205 bool range_locked = false;
3206 bool clear_new_delalloc_bytes = false;
3207 bool clear_reserved_extent = true;
3209 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3210 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3211 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
3212 clear_new_delalloc_bytes = true;
3214 nolock = btrfs_is_free_space_inode(BTRFS_I(inode));
3216 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3221 btrfs_free_io_failure_record(BTRFS_I(inode),
3222 ordered_extent->file_offset,
3223 ordered_extent->file_offset +
3224 ordered_extent->len - 1);
3226 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3228 logical_len = ordered_extent->truncated_len;
3229 /* Truncated the entire extent, don't bother adding */
3234 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
3235 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
3238 * For mwrite(mmap + memset to write) case, we still reserve
3239 * space for NOCOW range.
3240 * As NOCOW won't cause a new delayed ref, just free the space
3242 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3243 ordered_extent->len);
3244 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3246 trans = btrfs_join_transaction_nolock(root);
3248 trans = btrfs_join_transaction(root);
3249 if (IS_ERR(trans)) {
3250 ret = PTR_ERR(trans);
3254 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
3255 ret = btrfs_update_inode_fallback(trans, root, inode);
3256 if (ret) /* -ENOMEM or corruption */
3257 btrfs_abort_transaction(trans, ret);
3261 range_locked = true;
3262 lock_extent_bits(io_tree, ordered_extent->file_offset,
3263 ordered_extent->file_offset + ordered_extent->len - 1,
3266 ret = test_range_bit(io_tree, ordered_extent->file_offset,
3267 ordered_extent->file_offset + ordered_extent->len - 1,
3268 EXTENT_DEFRAG, 0, cached_state);
3270 u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
3271 if (0 && last_snapshot >= BTRFS_I(inode)->generation)
3272 /* the inode is shared */
3273 new = record_old_file_extents(inode, ordered_extent);
3275 clear_extent_bit(io_tree, ordered_extent->file_offset,
3276 ordered_extent->file_offset + ordered_extent->len - 1,
3277 EXTENT_DEFRAG, 0, 0, &cached_state);
3281 trans = btrfs_join_transaction_nolock(root);
3283 trans = btrfs_join_transaction(root);
3284 if (IS_ERR(trans)) {
3285 ret = PTR_ERR(trans);
3290 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
3292 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
3293 compress_type = ordered_extent->compress_type;
3294 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3295 BUG_ON(compress_type);
3296 btrfs_qgroup_free_data(inode, NULL, ordered_extent->file_offset,
3297 ordered_extent->len);
3298 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
3299 ordered_extent->file_offset,
3300 ordered_extent->file_offset +
3303 BUG_ON(root == fs_info->tree_root);
3304 ret = insert_reserved_file_extent(trans, inode,
3305 ordered_extent->file_offset,
3306 ordered_extent->start,
3307 ordered_extent->disk_len,
3308 logical_len, logical_len,
3309 compress_type, 0, 0,
3310 BTRFS_FILE_EXTENT_REG);
3312 clear_reserved_extent = false;
3313 btrfs_release_delalloc_bytes(fs_info,
3314 ordered_extent->start,
3315 ordered_extent->disk_len);
3318 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
3319 ordered_extent->file_offset, ordered_extent->len,
3322 btrfs_abort_transaction(trans, ret);
3326 ret = add_pending_csums(trans, inode, &ordered_extent->list);
3328 btrfs_abort_transaction(trans, ret);
3332 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
3333 ret = btrfs_update_inode_fallback(trans, root, inode);
3334 if (ret) { /* -ENOMEM or corruption */
3335 btrfs_abort_transaction(trans, ret);
3340 if (range_locked || clear_new_delalloc_bytes) {
3341 unsigned int clear_bits = 0;
3344 clear_bits |= EXTENT_LOCKED;
3345 if (clear_new_delalloc_bytes)
3346 clear_bits |= EXTENT_DELALLOC_NEW;
3347 clear_extent_bit(&BTRFS_I(inode)->io_tree,
3348 ordered_extent->file_offset,
3349 ordered_extent->file_offset +
3350 ordered_extent->len - 1,
3352 (clear_bits & EXTENT_LOCKED) ? 1 : 0,
3357 btrfs_end_transaction(trans);
3359 if (ret || truncated) {
3363 * If we failed to finish this ordered extent for any reason we
3364 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3365 * extent, and mark the inode with the error if it wasn't
3366 * already set. Any error during writeback would have already
3367 * set the mapping error, so we need to set it if we're the ones
3368 * marking this ordered extent as failed.
3370 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3371 &ordered_extent->flags))
3372 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3375 start = ordered_extent->file_offset + logical_len;
3377 start = ordered_extent->file_offset;
3378 end = ordered_extent->file_offset + ordered_extent->len - 1;
3379 clear_extent_uptodate(io_tree, start, end, NULL);
3381 /* Drop the cache for the part of the extent we didn't write. */
3382 btrfs_drop_extent_cache(BTRFS_I(inode), start, end, 0);
3385 * If the ordered extent had an IOERR or something else went
3386 * wrong we need to return the space for this ordered extent
3387 * back to the allocator. We only free the extent in the
3388 * truncated case if we didn't write out the extent at all.
3390 * If we made it past insert_reserved_file_extent before we
3391 * errored out then we don't need to do this as the accounting
3392 * has already been done.
3394 if ((ret || !logical_len) &&
3395 clear_reserved_extent &&
3396 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3397 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
3398 btrfs_free_reserved_extent(fs_info,
3399 ordered_extent->start,
3400 ordered_extent->disk_len, 1);
3405 * This needs to be done to make sure anybody waiting knows we are done
3406 * updating everything for this ordered extent.
3408 btrfs_remove_ordered_extent(inode, ordered_extent);
3410 /* for snapshot-aware defrag */
3413 free_sa_defrag_extent(new);
3414 atomic_dec(&fs_info->defrag_running);
3416 relink_file_extents(new);
3421 btrfs_put_ordered_extent(ordered_extent);
3422 /* once for the tree */
3423 btrfs_put_ordered_extent(ordered_extent);
3428 static void finish_ordered_fn(struct btrfs_work *work)
3430 struct btrfs_ordered_extent *ordered_extent;
3431 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3432 btrfs_finish_ordered_io(ordered_extent);
3435 void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
3436 u64 end, int uptodate)
3438 struct inode *inode = page->mapping->host;
3439 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3440 struct btrfs_ordered_extent *ordered_extent = NULL;
3441 struct btrfs_workqueue *wq;
3443 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
3445 ClearPagePrivate2(page);
3446 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
3447 end - start + 1, uptodate))
3450 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
3451 wq = fs_info->endio_freespace_worker;
3453 wq = fs_info->endio_write_workers;
3455 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
3456 btrfs_queue_work(wq, &ordered_extent->work);
3459 static int __readpage_endio_check(struct inode *inode,
3460 struct btrfs_io_bio *io_bio,
3461 int icsum, struct page *page,
3462 int pgoff, u64 start, size_t len)
3464 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3465 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
3467 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
3469 u8 csum[BTRFS_CSUM_SIZE];
3471 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
3473 kaddr = kmap_atomic(page);
3474 shash->tfm = fs_info->csum_shash;
3476 crypto_shash_init(shash);
3477 crypto_shash_update(shash, kaddr + pgoff, len);
3478 crypto_shash_final(shash, csum);
3480 if (memcmp(csum, csum_expected, csum_size))
3483 kunmap_atomic(kaddr);
3486 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3487 io_bio->mirror_num);
3488 memset(kaddr + pgoff, 1, len);
3489 flush_dcache_page(page);
3490 kunmap_atomic(kaddr);
3495 * when reads are done, we need to check csums to verify the data is correct
3496 * if there's a match, we allow the bio to finish. If not, the code in
3497 * extent_io.c will try to find good copies for us.
3499 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3500 u64 phy_offset, struct page *page,
3501 u64 start, u64 end, int mirror)
3503 size_t offset = start - page_offset(page);
3504 struct inode *inode = page->mapping->host;
3505 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3506 struct btrfs_root *root = BTRFS_I(inode)->root;
3508 if (PageChecked(page)) {
3509 ClearPageChecked(page);
3513 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
3516 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
3517 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
3518 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
3522 phy_offset >>= inode->i_sb->s_blocksize_bits;
3523 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3524 start, (size_t)(end - start + 1));
3528 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3530 * @inode: The inode we want to perform iput on
3532 * This function uses the generic vfs_inode::i_count to track whether we should
3533 * just decrement it (in case it's > 1) or if this is the last iput then link
3534 * the inode to the delayed iput machinery. Delayed iputs are processed at
3535 * transaction commit time/superblock commit/cleaner kthread.
3537 void btrfs_add_delayed_iput(struct inode *inode)
3539 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3540 struct btrfs_inode *binode = BTRFS_I(inode);
3542 if (atomic_add_unless(&inode->i_count, -1, 1))
3545 atomic_inc(&fs_info->nr_delayed_iputs);
3546 spin_lock(&fs_info->delayed_iput_lock);
3547 ASSERT(list_empty(&binode->delayed_iput));
3548 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3549 spin_unlock(&fs_info->delayed_iput_lock);
3550 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3551 wake_up_process(fs_info->cleaner_kthread);
3554 static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3555 struct btrfs_inode *inode)
3557 list_del_init(&inode->delayed_iput);
3558 spin_unlock(&fs_info->delayed_iput_lock);
3559 iput(&inode->vfs_inode);
3560 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3561 wake_up(&fs_info->delayed_iputs_wait);
3562 spin_lock(&fs_info->delayed_iput_lock);
3565 static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3566 struct btrfs_inode *inode)
3568 if (!list_empty(&inode->delayed_iput)) {
3569 spin_lock(&fs_info->delayed_iput_lock);
3570 if (!list_empty(&inode->delayed_iput))
3571 run_delayed_iput_locked(fs_info, inode);
3572 spin_unlock(&fs_info->delayed_iput_lock);
3576 void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
3579 spin_lock(&fs_info->delayed_iput_lock);
3580 while (!list_empty(&fs_info->delayed_iputs)) {
3581 struct btrfs_inode *inode;
3583 inode = list_first_entry(&fs_info->delayed_iputs,
3584 struct btrfs_inode, delayed_iput);
3585 run_delayed_iput_locked(fs_info, inode);
3586 cond_resched_lock(&fs_info->delayed_iput_lock);
3588 spin_unlock(&fs_info->delayed_iput_lock);
3592 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3593 * @fs_info - the fs_info for this fs
3594 * @return - EINTR if we were killed, 0 if nothing's pending
3596 * This will wait on any delayed iputs that are currently running with KILLABLE
3597 * set. Once they are all done running we will return, unless we are killed in
3598 * which case we return EINTR. This helps in user operations like fallocate etc
3599 * that might get blocked on the iputs.
3601 int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3603 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3604 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3611 * This creates an orphan entry for the given inode in case something goes wrong
3612 * in the middle of an unlink.
3614 int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3615 struct btrfs_inode *inode)
3619 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3620 if (ret && ret != -EEXIST) {
3621 btrfs_abort_transaction(trans, ret);
3629 * We have done the delete so we can go ahead and remove the orphan item for
3630 * this particular inode.
3632 static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3633 struct btrfs_inode *inode)
3635 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
3639 * this cleans up any orphans that may be left on the list from the last use
3642 int btrfs_orphan_cleanup(struct btrfs_root *root)
3644 struct btrfs_fs_info *fs_info = root->fs_info;
3645 struct btrfs_path *path;
3646 struct extent_buffer *leaf;
3647 struct btrfs_key key, found_key;
3648 struct btrfs_trans_handle *trans;
3649 struct inode *inode;
3650 u64 last_objectid = 0;
3651 int ret = 0, nr_unlink = 0;
3653 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
3656 path = btrfs_alloc_path();
3661 path->reada = READA_BACK;
3663 key.objectid = BTRFS_ORPHAN_OBJECTID;
3664 key.type = BTRFS_ORPHAN_ITEM_KEY;
3665 key.offset = (u64)-1;
3668 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3673 * if ret == 0 means we found what we were searching for, which
3674 * is weird, but possible, so only screw with path if we didn't
3675 * find the key and see if we have stuff that matches
3679 if (path->slots[0] == 0)
3684 /* pull out the item */
3685 leaf = path->nodes[0];
3686 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3688 /* make sure the item matches what we want */
3689 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3691 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
3694 /* release the path since we're done with it */
3695 btrfs_release_path(path);
3698 * this is where we are basically btrfs_lookup, without the
3699 * crossing root thing. we store the inode number in the
3700 * offset of the orphan item.
3703 if (found_key.offset == last_objectid) {
3705 "Error removing orphan entry, stopping orphan cleanup");
3710 last_objectid = found_key.offset;
3712 found_key.objectid = found_key.offset;
3713 found_key.type = BTRFS_INODE_ITEM_KEY;
3714 found_key.offset = 0;
3715 inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
3716 ret = PTR_ERR_OR_ZERO(inode);
3717 if (ret && ret != -ENOENT)
3720 if (ret == -ENOENT && root == fs_info->tree_root) {
3721 struct btrfs_root *dead_root;
3722 struct btrfs_fs_info *fs_info = root->fs_info;
3723 int is_dead_root = 0;
3726 * this is an orphan in the tree root. Currently these
3727 * could come from 2 sources:
3728 * a) a snapshot deletion in progress
3729 * b) a free space cache inode
3730 * We need to distinguish those two, as the snapshot
3731 * orphan must not get deleted.
3732 * find_dead_roots already ran before us, so if this
3733 * is a snapshot deletion, we should find the root
3734 * in the dead_roots list
3736 spin_lock(&fs_info->trans_lock);
3737 list_for_each_entry(dead_root, &fs_info->dead_roots,
3739 if (dead_root->root_key.objectid ==
3740 found_key.objectid) {
3745 spin_unlock(&fs_info->trans_lock);
3747 /* prevent this orphan from being found again */
3748 key.offset = found_key.objectid - 1;
3755 * If we have an inode with links, there are a couple of
3756 * possibilities. Old kernels (before v3.12) used to create an
3757 * orphan item for truncate indicating that there were possibly
3758 * extent items past i_size that needed to be deleted. In v3.12,
3759 * truncate was changed to update i_size in sync with the extent
3760 * items, but the (useless) orphan item was still created. Since
3761 * v4.18, we don't create the orphan item for truncate at all.
3763 * So, this item could mean that we need to do a truncate, but
3764 * only if this filesystem was last used on a pre-v3.12 kernel
3765 * and was not cleanly unmounted. The odds of that are quite
3766 * slim, and it's a pain to do the truncate now, so just delete
3769 * It's also possible that this orphan item was supposed to be
3770 * deleted but wasn't. The inode number may have been reused,
3771 * but either way, we can delete the orphan item.
3773 if (ret == -ENOENT || inode->i_nlink) {
3776 trans = btrfs_start_transaction(root, 1);
3777 if (IS_ERR(trans)) {
3778 ret = PTR_ERR(trans);
3781 btrfs_debug(fs_info, "auto deleting %Lu",
3782 found_key.objectid);
3783 ret = btrfs_del_orphan_item(trans, root,
3784 found_key.objectid);
3785 btrfs_end_transaction(trans);
3793 /* this will do delete_inode and everything for us */
3796 /* release the path since we're done with it */
3797 btrfs_release_path(path);
3799 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3801 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
3802 trans = btrfs_join_transaction(root);
3804 btrfs_end_transaction(trans);
3808 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
3812 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
3813 btrfs_free_path(path);
3818 * very simple check to peek ahead in the leaf looking for xattrs. If we
3819 * don't find any xattrs, we know there can't be any acls.
3821 * slot is the slot the inode is in, objectid is the objectid of the inode
3823 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3824 int slot, u64 objectid,
3825 int *first_xattr_slot)
3827 u32 nritems = btrfs_header_nritems(leaf);
3828 struct btrfs_key found_key;
3829 static u64 xattr_access = 0;
3830 static u64 xattr_default = 0;
3833 if (!xattr_access) {
3834 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3835 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3836 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3837 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
3841 *first_xattr_slot = -1;
3842 while (slot < nritems) {
3843 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3845 /* we found a different objectid, there must not be acls */
3846 if (found_key.objectid != objectid)
3849 /* we found an xattr, assume we've got an acl */
3850 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3851 if (*first_xattr_slot == -1)
3852 *first_xattr_slot = slot;
3853 if (found_key.offset == xattr_access ||
3854 found_key.offset == xattr_default)
3859 * we found a key greater than an xattr key, there can't
3860 * be any acls later on
3862 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3869 * it goes inode, inode backrefs, xattrs, extents,
3870 * so if there are a ton of hard links to an inode there can
3871 * be a lot of backrefs. Don't waste time searching too hard,
3872 * this is just an optimization
3877 /* we hit the end of the leaf before we found an xattr or
3878 * something larger than an xattr. We have to assume the inode
3881 if (*first_xattr_slot == -1)
3882 *first_xattr_slot = slot;
3887 * read an inode from the btree into the in-memory inode
3889 static int btrfs_read_locked_inode(struct inode *inode,
3890 struct btrfs_path *in_path)
3892 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3893 struct btrfs_path *path = in_path;
3894 struct extent_buffer *leaf;
3895 struct btrfs_inode_item *inode_item;
3896 struct btrfs_root *root = BTRFS_I(inode)->root;
3897 struct btrfs_key location;
3902 bool filled = false;
3903 int first_xattr_slot;
3905 ret = btrfs_fill_inode(inode, &rdev);
3910 path = btrfs_alloc_path();
3915 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3917 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
3919 if (path != in_path)
3920 btrfs_free_path(path);
3924 leaf = path->nodes[0];
3929 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3930 struct btrfs_inode_item);
3931 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3932 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3933 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3934 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3935 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
3937 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3938 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
3940 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3941 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
3943 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3944 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
3946 BTRFS_I(inode)->i_otime.tv_sec =
3947 btrfs_timespec_sec(leaf, &inode_item->otime);
3948 BTRFS_I(inode)->i_otime.tv_nsec =
3949 btrfs_timespec_nsec(leaf, &inode_item->otime);
3951 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3952 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3953 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3955 inode_set_iversion_queried(inode,
3956 btrfs_inode_sequence(leaf, inode_item));
3957 inode->i_generation = BTRFS_I(inode)->generation;
3959 rdev = btrfs_inode_rdev(leaf, inode_item);
3961 BTRFS_I(inode)->index_cnt = (u64)-1;
3962 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
3966 * If we were modified in the current generation and evicted from memory
3967 * and then re-read we need to do a full sync since we don't have any
3968 * idea about which extents were modified before we were evicted from
3971 * This is required for both inode re-read from disk and delayed inode
3972 * in delayed_nodes_tree.
3974 if (BTRFS_I(inode)->last_trans == fs_info->generation)
3975 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3976 &BTRFS_I(inode)->runtime_flags);
3979 * We don't persist the id of the transaction where an unlink operation
3980 * against the inode was last made. So here we assume the inode might
3981 * have been evicted, and therefore the exact value of last_unlink_trans
3982 * lost, and set it to last_trans to avoid metadata inconsistencies
3983 * between the inode and its parent if the inode is fsync'ed and the log
3984 * replayed. For example, in the scenario:
3987 * ln mydir/foo mydir/bar
3990 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3991 * xfs_io -c fsync mydir/foo
3993 * mount fs, triggers fsync log replay
3995 * We must make sure that when we fsync our inode foo we also log its
3996 * parent inode, otherwise after log replay the parent still has the
3997 * dentry with the "bar" name but our inode foo has a link count of 1
3998 * and doesn't have an inode ref with the name "bar" anymore.
4000 * Setting last_unlink_trans to last_trans is a pessimistic approach,
4001 * but it guarantees correctness at the expense of occasional full
4002 * transaction commits on fsync if our inode is a directory, or if our
4003 * inode is not a directory, logging its parent unnecessarily.
4005 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
4008 if (inode->i_nlink != 1 ||
4009 path->slots[0] >= btrfs_header_nritems(leaf))
4012 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
4013 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
4016 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4017 if (location.type == BTRFS_INODE_REF_KEY) {
4018 struct btrfs_inode_ref *ref;
4020 ref = (struct btrfs_inode_ref *)ptr;
4021 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
4022 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
4023 struct btrfs_inode_extref *extref;
4025 extref = (struct btrfs_inode_extref *)ptr;
4026 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
4031 * try to precache a NULL acl entry for files that don't have
4032 * any xattrs or acls
4034 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
4035 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
4036 if (first_xattr_slot != -1) {
4037 path->slots[0] = first_xattr_slot;
4038 ret = btrfs_load_inode_props(inode, path);
4041 "error loading props for ino %llu (root %llu): %d",
4042 btrfs_ino(BTRFS_I(inode)),
4043 root->root_key.objectid, ret);
4045 if (path != in_path)
4046 btrfs_free_path(path);
4049 cache_no_acl(inode);
4051 switch (inode->i_mode & S_IFMT) {
4053 inode->i_mapping->a_ops = &btrfs_aops;
4054 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4055 inode->i_fop = &btrfs_file_operations;
4056 inode->i_op = &btrfs_file_inode_operations;
4059 inode->i_fop = &btrfs_dir_file_operations;
4060 inode->i_op = &btrfs_dir_inode_operations;
4063 inode->i_op = &btrfs_symlink_inode_operations;
4064 inode_nohighmem(inode);
4065 inode->i_mapping->a_ops = &btrfs_aops;
4068 inode->i_op = &btrfs_special_inode_operations;
4069 init_special_inode(inode, inode->i_mode, rdev);
4073 btrfs_sync_inode_flags_to_i_flags(inode);
4078 * given a leaf and an inode, copy the inode fields into the leaf
4080 static void fill_inode_item(struct btrfs_trans_handle *trans,
4081 struct extent_buffer *leaf,
4082 struct btrfs_inode_item *item,
4083 struct inode *inode)
4085 struct btrfs_map_token token;
4087 btrfs_init_map_token(&token, leaf);
4089 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
4090 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
4091 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
4093 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
4094 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
4096 btrfs_set_token_timespec_sec(leaf, &item->atime,
4097 inode->i_atime.tv_sec, &token);
4098 btrfs_set_token_timespec_nsec(leaf, &item->atime,
4099 inode->i_atime.tv_nsec, &token);
4101 btrfs_set_token_timespec_sec(leaf, &item->mtime,
4102 inode->i_mtime.tv_sec, &token);
4103 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
4104 inode->i_mtime.tv_nsec, &token);
4106 btrfs_set_token_timespec_sec(leaf, &item->ctime,
4107 inode->i_ctime.tv_sec, &token);
4108 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
4109 inode->i_ctime.tv_nsec, &token);
4111 btrfs_set_token_timespec_sec(leaf, &item->otime,
4112 BTRFS_I(inode)->i_otime.tv_sec, &token);
4113 btrfs_set_token_timespec_nsec(leaf, &item->otime,
4114 BTRFS_I(inode)->i_otime.tv_nsec, &token);
4116 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
4118 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
4120 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
4122 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
4123 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
4124 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
4125 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
4129 * copy everything in the in-memory inode into the btree.
4131 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
4132 struct btrfs_root *root, struct inode *inode)
4134 struct btrfs_inode_item *inode_item;
4135 struct btrfs_path *path;
4136 struct extent_buffer *leaf;
4139 path = btrfs_alloc_path();
4143 path->leave_spinning = 1;
4144 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
4152 leaf = path->nodes[0];
4153 inode_item = btrfs_item_ptr(leaf, path->slots[0],
4154 struct btrfs_inode_item);
4156 fill_inode_item(trans, leaf, inode_item, inode);
4157 btrfs_mark_buffer_dirty(leaf);
4158 btrfs_set_inode_last_trans(trans, inode);
4161 btrfs_free_path(path);
4166 * copy everything in the in-memory inode into the btree.
4168 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
4169 struct btrfs_root *root, struct inode *inode)
4171 struct btrfs_fs_info *fs_info = root->fs_info;
4175 * If the inode is a free space inode, we can deadlock during commit
4176 * if we put it into the delayed code.
4178 * The data relocation inode should also be directly updated
4181 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
4182 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
4183 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
4184 btrfs_update_root_times(trans, root);
4186 ret = btrfs_delayed_update_inode(trans, root, inode);
4188 btrfs_set_inode_last_trans(trans, inode);
4192 return btrfs_update_inode_item(trans, root, inode);
4195 noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4196 struct btrfs_root *root,
4197 struct inode *inode)
4201 ret = btrfs_update_inode(trans, root, inode);
4203 return btrfs_update_inode_item(trans, root, inode);
4208 * unlink helper that gets used here in inode.c and in the tree logging
4209 * recovery code. It remove a link in a directory with a given name, and
4210 * also drops the back refs in the inode to the directory
4212 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4213 struct btrfs_root *root,
4214 struct btrfs_inode *dir,
4215 struct btrfs_inode *inode,
4216 const char *name, int name_len)
4218 struct btrfs_fs_info *fs_info = root->fs_info;
4219 struct btrfs_path *path;
4221 struct btrfs_dir_item *di;
4223 u64 ino = btrfs_ino(inode);
4224 u64 dir_ino = btrfs_ino(dir);
4226 path = btrfs_alloc_path();
4232 path->leave_spinning = 1;
4233 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4234 name, name_len, -1);
4235 if (IS_ERR_OR_NULL(di)) {
4236 ret = di ? PTR_ERR(di) : -ENOENT;
4239 ret = btrfs_delete_one_dir_name(trans, root, path, di);
4242 btrfs_release_path(path);
4245 * If we don't have dir index, we have to get it by looking up
4246 * the inode ref, since we get the inode ref, remove it directly,
4247 * it is unnecessary to do delayed deletion.
4249 * But if we have dir index, needn't search inode ref to get it.
4250 * Since the inode ref is close to the inode item, it is better
4251 * that we delay to delete it, and just do this deletion when
4252 * we update the inode item.
4254 if (inode->dir_index) {
4255 ret = btrfs_delayed_delete_inode_ref(inode);
4257 index = inode->dir_index;
4262 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4266 "failed to delete reference to %.*s, inode %llu parent %llu",
4267 name_len, name, ino, dir_ino);
4268 btrfs_abort_transaction(trans, ret);
4272 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
4274 btrfs_abort_transaction(trans, ret);
4278 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4280 if (ret != 0 && ret != -ENOENT) {
4281 btrfs_abort_transaction(trans, ret);
4285 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4290 btrfs_abort_transaction(trans, ret);
4293 * If we have a pending delayed iput we could end up with the final iput
4294 * being run in btrfs-cleaner context. If we have enough of these built
4295 * up we can end up burning a lot of time in btrfs-cleaner without any
4296 * way to throttle the unlinks. Since we're currently holding a ref on
4297 * the inode we can run the delayed iput here without any issues as the
4298 * final iput won't be done until after we drop the ref we're currently
4301 btrfs_run_delayed_iput(fs_info, inode);
4303 btrfs_free_path(path);
4307 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
4308 inode_inc_iversion(&inode->vfs_inode);
4309 inode_inc_iversion(&dir->vfs_inode);
4310 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4311 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4312 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
4317 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4318 struct btrfs_root *root,
4319 struct btrfs_inode *dir, struct btrfs_inode *inode,
4320 const char *name, int name_len)
4323 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4325 drop_nlink(&inode->vfs_inode);
4326 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
4332 * helper to start transaction for unlink and rmdir.
4334 * unlink and rmdir are special in btrfs, they do not always free space, so
4335 * if we cannot make our reservations the normal way try and see if there is
4336 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4337 * allow the unlink to occur.
4339 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
4341 struct btrfs_root *root = BTRFS_I(dir)->root;
4344 * 1 for the possible orphan item
4345 * 1 for the dir item
4346 * 1 for the dir index
4347 * 1 for the inode ref
4350 return btrfs_start_transaction_fallback_global_rsv(root, 5);
4353 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4355 struct btrfs_root *root = BTRFS_I(dir)->root;
4356 struct btrfs_trans_handle *trans;
4357 struct inode *inode = d_inode(dentry);
4360 trans = __unlink_start_trans(dir);
4362 return PTR_ERR(trans);
4364 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4367 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4368 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4369 dentry->d_name.len);
4373 if (inode->i_nlink == 0) {
4374 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
4380 btrfs_end_transaction(trans);
4381 btrfs_btree_balance_dirty(root->fs_info);
4385 static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4386 struct inode *dir, struct dentry *dentry)
4388 struct btrfs_root *root = BTRFS_I(dir)->root;
4389 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
4390 struct btrfs_path *path;
4391 struct extent_buffer *leaf;
4392 struct btrfs_dir_item *di;
4393 struct btrfs_key key;
4394 const char *name = dentry->d_name.name;
4395 int name_len = dentry->d_name.len;
4399 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
4401 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4402 objectid = inode->root->root_key.objectid;
4403 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4404 objectid = inode->location.objectid;
4410 path = btrfs_alloc_path();
4414 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4415 name, name_len, -1);
4416 if (IS_ERR_OR_NULL(di)) {
4417 ret = di ? PTR_ERR(di) : -ENOENT;
4421 leaf = path->nodes[0];
4422 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4423 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4424 ret = btrfs_delete_one_dir_name(trans, root, path, di);
4426 btrfs_abort_transaction(trans, ret);
4429 btrfs_release_path(path);
4432 * This is a placeholder inode for a subvolume we didn't have a
4433 * reference to at the time of the snapshot creation. In the meantime
4434 * we could have renamed the real subvol link into our snapshot, so
4435 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
4436 * Instead simply lookup the dir_index_item for this entry so we can
4437 * remove it. Otherwise we know we have a ref to the root and we can
4438 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4440 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4441 di = btrfs_search_dir_index_item(root, path, dir_ino,
4443 if (IS_ERR_OR_NULL(di)) {
4448 btrfs_abort_transaction(trans, ret);
4452 leaf = path->nodes[0];
4453 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4455 btrfs_release_path(path);
4457 ret = btrfs_del_root_ref(trans, objectid,
4458 root->root_key.objectid, dir_ino,
4459 &index, name, name_len);
4461 btrfs_abort_transaction(trans, ret);
4466 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
4468 btrfs_abort_transaction(trans, ret);
4472 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
4473 inode_inc_iversion(dir);
4474 dir->i_mtime = dir->i_ctime = current_time(dir);
4475 ret = btrfs_update_inode_fallback(trans, root, dir);
4477 btrfs_abort_transaction(trans, ret);
4479 btrfs_free_path(path);
4484 * Helper to check if the subvolume references other subvolumes or if it's
4487 static noinline int may_destroy_subvol(struct btrfs_root *root)
4489 struct btrfs_fs_info *fs_info = root->fs_info;
4490 struct btrfs_path *path;
4491 struct btrfs_dir_item *di;
4492 struct btrfs_key key;
4496 path = btrfs_alloc_path();
4500 /* Make sure this root isn't set as the default subvol */
4501 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4502 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4503 dir_id, "default", 7, 0);
4504 if (di && !IS_ERR(di)) {
4505 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4506 if (key.objectid == root->root_key.objectid) {
4509 "deleting default subvolume %llu is not allowed",
4513 btrfs_release_path(path);
4516 key.objectid = root->root_key.objectid;
4517 key.type = BTRFS_ROOT_REF_KEY;
4518 key.offset = (u64)-1;
4520 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4526 if (path->slots[0] > 0) {
4528 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4529 if (key.objectid == root->root_key.objectid &&
4530 key.type == BTRFS_ROOT_REF_KEY)
4534 btrfs_free_path(path);
4538 /* Delete all dentries for inodes belonging to the root */
4539 static void btrfs_prune_dentries(struct btrfs_root *root)
4541 struct btrfs_fs_info *fs_info = root->fs_info;
4542 struct rb_node *node;
4543 struct rb_node *prev;
4544 struct btrfs_inode *entry;
4545 struct inode *inode;
4548 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4549 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4551 spin_lock(&root->inode_lock);
4553 node = root->inode_tree.rb_node;
4557 entry = rb_entry(node, struct btrfs_inode, rb_node);
4559 if (objectid < btrfs_ino(entry))
4560 node = node->rb_left;
4561 else if (objectid > btrfs_ino(entry))
4562 node = node->rb_right;
4568 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4569 if (objectid <= btrfs_ino(entry)) {
4573 prev = rb_next(prev);
4577 entry = rb_entry(node, struct btrfs_inode, rb_node);
4578 objectid = btrfs_ino(entry) + 1;
4579 inode = igrab(&entry->vfs_inode);
4581 spin_unlock(&root->inode_lock);
4582 if (atomic_read(&inode->i_count) > 1)
4583 d_prune_aliases(inode);
4585 * btrfs_drop_inode will have it removed from the inode
4586 * cache when its usage count hits zero.
4590 spin_lock(&root->inode_lock);
4594 if (cond_resched_lock(&root->inode_lock))
4597 node = rb_next(node);
4599 spin_unlock(&root->inode_lock);
4602 int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4604 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4605 struct btrfs_root *root = BTRFS_I(dir)->root;
4606 struct inode *inode = d_inode(dentry);
4607 struct btrfs_root *dest = BTRFS_I(inode)->root;
4608 struct btrfs_trans_handle *trans;
4609 struct btrfs_block_rsv block_rsv;
4615 * Don't allow to delete a subvolume with send in progress. This is
4616 * inside the inode lock so the error handling that has to drop the bit
4617 * again is not run concurrently.
4619 spin_lock(&dest->root_item_lock);
4620 if (dest->send_in_progress) {
4621 spin_unlock(&dest->root_item_lock);
4623 "attempt to delete subvolume %llu during send",
4624 dest->root_key.objectid);
4627 root_flags = btrfs_root_flags(&dest->root_item);
4628 btrfs_set_root_flags(&dest->root_item,
4629 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4630 spin_unlock(&dest->root_item_lock);
4632 down_write(&fs_info->subvol_sem);
4634 err = may_destroy_subvol(dest);
4638 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4640 * One for dir inode,
4641 * two for dir entries,
4642 * two for root ref/backref.
4644 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4648 trans = btrfs_start_transaction(root, 0);
4649 if (IS_ERR(trans)) {
4650 err = PTR_ERR(trans);
4653 trans->block_rsv = &block_rsv;
4654 trans->bytes_reserved = block_rsv.size;
4656 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4658 ret = btrfs_unlink_subvol(trans, dir, dentry);
4661 btrfs_abort_transaction(trans, ret);
4665 btrfs_record_root_in_trans(trans, dest);
4667 memset(&dest->root_item.drop_progress, 0,
4668 sizeof(dest->root_item.drop_progress));
4669 dest->root_item.drop_level = 0;
4670 btrfs_set_root_refs(&dest->root_item, 0);
4672 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4673 ret = btrfs_insert_orphan_item(trans,
4675 dest->root_key.objectid);
4677 btrfs_abort_transaction(trans, ret);
4683 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
4684 BTRFS_UUID_KEY_SUBVOL,
4685 dest->root_key.objectid);
4686 if (ret && ret != -ENOENT) {
4687 btrfs_abort_transaction(trans, ret);
4691 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
4692 ret = btrfs_uuid_tree_remove(trans,
4693 dest->root_item.received_uuid,
4694 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4695 dest->root_key.objectid);
4696 if (ret && ret != -ENOENT) {
4697 btrfs_abort_transaction(trans, ret);
4703 free_anon_bdev(dest->anon_dev);
4706 trans->block_rsv = NULL;
4707 trans->bytes_reserved = 0;
4708 ret = btrfs_end_transaction(trans);
4711 inode->i_flags |= S_DEAD;
4713 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4715 up_write(&fs_info->subvol_sem);
4717 spin_lock(&dest->root_item_lock);
4718 root_flags = btrfs_root_flags(&dest->root_item);
4719 btrfs_set_root_flags(&dest->root_item,
4720 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4721 spin_unlock(&dest->root_item_lock);
4723 d_invalidate(dentry);
4724 btrfs_prune_dentries(dest);
4725 ASSERT(dest->send_in_progress == 0);
4728 if (dest->ino_cache_inode) {
4729 iput(dest->ino_cache_inode);
4730 dest->ino_cache_inode = NULL;
4737 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4739 struct inode *inode = d_inode(dentry);
4741 struct btrfs_root *root = BTRFS_I(dir)->root;
4742 struct btrfs_trans_handle *trans;
4743 u64 last_unlink_trans;
4745 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4747 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
4748 return btrfs_delete_subvolume(dir, dentry);
4750 trans = __unlink_start_trans(dir);
4752 return PTR_ERR(trans);
4754 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4755 err = btrfs_unlink_subvol(trans, dir, dentry);
4759 err = btrfs_orphan_add(trans, BTRFS_I(inode));
4763 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4765 /* now the directory is empty */
4766 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4767 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4768 dentry->d_name.len);
4770 btrfs_i_size_write(BTRFS_I(inode), 0);
4772 * Propagate the last_unlink_trans value of the deleted dir to
4773 * its parent directory. This is to prevent an unrecoverable
4774 * log tree in the case we do something like this:
4776 * 2) create snapshot under dir foo
4777 * 3) delete the snapshot
4780 * 6) fsync foo or some file inside foo
4782 if (last_unlink_trans >= trans->transid)
4783 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4786 btrfs_end_transaction(trans);
4787 btrfs_btree_balance_dirty(root->fs_info);
4793 * Return this if we need to call truncate_block for the last bit of the
4796 #define NEED_TRUNCATE_BLOCK 1
4799 * this can truncate away extent items, csum items and directory items.
4800 * It starts at a high offset and removes keys until it can't find
4801 * any higher than new_size
4803 * csum items that cross the new i_size are truncated to the new size
4806 * min_type is the minimum key type to truncate down to. If set to 0, this
4807 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4809 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4810 struct btrfs_root *root,
4811 struct inode *inode,
4812 u64 new_size, u32 min_type)
4814 struct btrfs_fs_info *fs_info = root->fs_info;
4815 struct btrfs_path *path;
4816 struct extent_buffer *leaf;
4817 struct btrfs_file_extent_item *fi;
4818 struct btrfs_key key;
4819 struct btrfs_key found_key;
4820 u64 extent_start = 0;
4821 u64 extent_num_bytes = 0;
4822 u64 extent_offset = 0;
4824 u64 last_size = new_size;
4825 u32 found_type = (u8)-1;
4828 int pending_del_nr = 0;
4829 int pending_del_slot = 0;
4830 int extent_type = -1;
4832 u64 ino = btrfs_ino(BTRFS_I(inode));
4833 u64 bytes_deleted = 0;
4834 bool be_nice = false;
4835 bool should_throttle = false;
4836 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4837 struct extent_state *cached_state = NULL;
4839 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
4842 * for non-free space inodes and ref cows, we want to back off from
4845 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
4846 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4849 path = btrfs_alloc_path();
4852 path->reada = READA_BACK;
4854 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4855 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4859 * We want to drop from the next block forward in case this new size is
4860 * not block aligned since we will be keeping the last block of the
4861 * extent just the way it is.
4863 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4864 root == fs_info->tree_root)
4865 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
4866 fs_info->sectorsize),
4870 * This function is also used to drop the items in the log tree before
4871 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4872 * it is used to drop the logged items. So we shouldn't kill the delayed
4875 if (min_type == 0 && root == BTRFS_I(inode)->root)
4876 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
4879 key.offset = (u64)-1;
4884 * with a 16K leaf size and 128MB extents, you can actually queue
4885 * up a huge file in a single leaf. Most of the time that
4886 * bytes_deleted is > 0, it will be huge by the time we get here
4888 if (be_nice && bytes_deleted > SZ_32M &&
4889 btrfs_should_end_transaction(trans)) {
4894 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4900 /* there are no items in the tree for us to truncate, we're
4903 if (path->slots[0] == 0)
4910 leaf = path->nodes[0];
4911 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4912 found_type = found_key.type;
4914 if (found_key.objectid != ino)
4917 if (found_type < min_type)
4920 item_end = found_key.offset;
4921 if (found_type == BTRFS_EXTENT_DATA_KEY) {
4922 fi = btrfs_item_ptr(leaf, path->slots[0],
4923 struct btrfs_file_extent_item);
4924 extent_type = btrfs_file_extent_type(leaf, fi);
4925 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4927 btrfs_file_extent_num_bytes(leaf, fi);
4929 trace_btrfs_truncate_show_fi_regular(
4930 BTRFS_I(inode), leaf, fi,
4932 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4933 item_end += btrfs_file_extent_ram_bytes(leaf,
4936 trace_btrfs_truncate_show_fi_inline(
4937 BTRFS_I(inode), leaf, fi, path->slots[0],
4942 if (found_type > min_type) {
4945 if (item_end < new_size)
4947 if (found_key.offset >= new_size)
4953 /* FIXME, shrink the extent if the ref count is only 1 */
4954 if (found_type != BTRFS_EXTENT_DATA_KEY)
4957 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4959 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
4961 u64 orig_num_bytes =
4962 btrfs_file_extent_num_bytes(leaf, fi);
4963 extent_num_bytes = ALIGN(new_size -
4965 fs_info->sectorsize);
4966 btrfs_set_file_extent_num_bytes(leaf, fi,
4968 num_dec = (orig_num_bytes -
4970 if (test_bit(BTRFS_ROOT_REF_COWS,
4973 inode_sub_bytes(inode, num_dec);
4974 btrfs_mark_buffer_dirty(leaf);
4977 btrfs_file_extent_disk_num_bytes(leaf,
4979 extent_offset = found_key.offset -
4980 btrfs_file_extent_offset(leaf, fi);
4982 /* FIXME blocksize != 4096 */
4983 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
4984 if (extent_start != 0) {
4986 if (test_bit(BTRFS_ROOT_REF_COWS,
4988 inode_sub_bytes(inode, num_dec);
4991 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4993 * we can't truncate inline items that have had
4997 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4998 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4999 btrfs_file_extent_compression(leaf, fi) == 0) {
5000 u32 size = (u32)(new_size - found_key.offset);
5002 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
5003 size = btrfs_file_extent_calc_inline_size(size);
5004 btrfs_truncate_item(path, size, 1);
5005 } else if (!del_item) {
5007 * We have to bail so the last_size is set to
5008 * just before this extent.
5010 ret = NEED_TRUNCATE_BLOCK;
5014 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
5015 inode_sub_bytes(inode, item_end + 1 - new_size);
5019 last_size = found_key.offset;
5021 last_size = new_size;
5023 if (!pending_del_nr) {
5024 /* no pending yet, add ourselves */
5025 pending_del_slot = path->slots[0];
5027 } else if (pending_del_nr &&
5028 path->slots[0] + 1 == pending_del_slot) {
5029 /* hop on the pending chunk */
5031 pending_del_slot = path->slots[0];
5038 should_throttle = false;
5041 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
5042 root == fs_info->tree_root)) {
5043 struct btrfs_ref ref = { 0 };
5045 bytes_deleted += extent_num_bytes;
5047 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
5048 extent_start, extent_num_bytes, 0);
5049 ref.real_root = root->root_key.objectid;
5050 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
5051 ino, extent_offset);
5052 ret = btrfs_free_extent(trans, &ref);
5054 btrfs_abort_transaction(trans, ret);
5058 if (btrfs_should_throttle_delayed_refs(trans))
5059 should_throttle = true;
5063 if (found_type == BTRFS_INODE_ITEM_KEY)
5066 if (path->slots[0] == 0 ||
5067 path->slots[0] != pending_del_slot ||
5069 if (pending_del_nr) {
5070 ret = btrfs_del_items(trans, root, path,
5074 btrfs_abort_transaction(trans, ret);
5079 btrfs_release_path(path);
5082 * We can generate a lot of delayed refs, so we need to
5083 * throttle every once and a while and make sure we're
5084 * adding enough space to keep up with the work we are
5085 * generating. Since we hold a transaction here we
5086 * can't flush, and we don't want to FLUSH_LIMIT because
5087 * we could have generated too many delayed refs to
5088 * actually allocate, so just bail if we're short and
5089 * let the normal reservation dance happen higher up.
5091 if (should_throttle) {
5092 ret = btrfs_delayed_refs_rsv_refill(fs_info,
5093 BTRFS_RESERVE_NO_FLUSH);
5105 if (ret >= 0 && pending_del_nr) {
5108 err = btrfs_del_items(trans, root, path, pending_del_slot,
5111 btrfs_abort_transaction(trans, err);
5115 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5116 ASSERT(last_size >= new_size);
5117 if (!ret && last_size > new_size)
5118 last_size = new_size;
5119 btrfs_ordered_update_i_size(inode, last_size, NULL);
5120 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
5121 (u64)-1, &cached_state);
5124 btrfs_free_path(path);
5129 * btrfs_truncate_block - read, zero a chunk and write a block
5130 * @inode - inode that we're zeroing
5131 * @from - the offset to start zeroing
5132 * @len - the length to zero, 0 to zero the entire range respective to the
5134 * @front - zero up to the offset instead of from the offset on
5136 * This will find the block for the "from" offset and cow the block and zero the
5137 * part we want to zero. This is used with truncate and hole punching.
5139 int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
5142 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5143 struct address_space *mapping = inode->i_mapping;
5144 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5145 struct btrfs_ordered_extent *ordered;
5146 struct extent_state *cached_state = NULL;
5147 struct extent_changeset *data_reserved = NULL;
5149 bool only_release_metadata = false;
5150 u32 blocksize = fs_info->sectorsize;
5151 pgoff_t index = from >> PAGE_SHIFT;
5152 unsigned offset = from & (blocksize - 1);
5154 gfp_t mask = btrfs_alloc_write_mask(mapping);
5155 size_t write_bytes = blocksize;
5160 if (IS_ALIGNED(offset, blocksize) &&
5161 (!len || IS_ALIGNED(len, blocksize)))
5164 block_start = round_down(from, blocksize);
5165 block_end = block_start + blocksize - 1;
5168 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
5171 if ((BTRFS_I(inode)->flags & (BTRFS_INODE_NODATACOW |
5172 BTRFS_INODE_PREALLOC)) &&
5173 btrfs_check_can_nocow(BTRFS_I(inode), block_start,
5174 &write_bytes) > 0) {
5175 /* For nocow case, no need to reserve data space */
5176 only_release_metadata = true;
5181 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
5183 if (!only_release_metadata)
5184 btrfs_free_reserved_data_space(inode, data_reserved,
5185 block_start, blocksize);
5189 page = find_or_create_page(mapping, index, mask);
5191 btrfs_delalloc_release_space(inode, data_reserved,
5192 block_start, blocksize, true);
5193 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
5198 if (!PageUptodate(page)) {
5199 ret = btrfs_readpage(NULL, page);
5201 if (page->mapping != mapping) {
5206 if (!PageUptodate(page)) {
5211 wait_on_page_writeback(page);
5213 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
5214 set_page_extent_mapped(page);
5216 ordered = btrfs_lookup_ordered_extent(inode, block_start);
5218 unlock_extent_cached(io_tree, block_start, block_end,
5222 btrfs_start_ordered_extent(inode, ordered, 1);
5223 btrfs_put_ordered_extent(ordered);
5227 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
5228 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
5229 0, 0, &cached_state);
5231 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
5234 unlock_extent_cached(io_tree, block_start, block_end,
5239 if (offset != blocksize) {
5241 len = blocksize - offset;
5244 memset(kaddr + (block_start - page_offset(page)),
5247 memset(kaddr + (block_start - page_offset(page)) + offset,
5249 flush_dcache_page(page);
5252 ClearPageChecked(page);
5253 set_page_dirty(page);
5254 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
5256 if (only_release_metadata)
5257 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
5258 block_end, EXTENT_NORESERVE, NULL, NULL,
5263 if (only_release_metadata)
5264 btrfs_delalloc_release_metadata(BTRFS_I(inode),
5267 btrfs_delalloc_release_space(inode, data_reserved,
5268 block_start, blocksize, true);
5270 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
5274 if (only_release_metadata)
5275 btrfs_end_write_no_snapshotting(BTRFS_I(inode)->root);
5276 extent_changeset_free(data_reserved);
5280 static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
5281 u64 offset, u64 len)
5283 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5284 struct btrfs_trans_handle *trans;
5288 * Still need to make sure the inode looks like it's been updated so
5289 * that any holes get logged if we fsync.
5291 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
5292 BTRFS_I(inode)->last_trans = fs_info->generation;
5293 BTRFS_I(inode)->last_sub_trans = root->log_transid;
5294 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
5299 * 1 - for the one we're dropping
5300 * 1 - for the one we're adding
5301 * 1 - for updating the inode.
5303 trans = btrfs_start_transaction(root, 3);
5305 return PTR_ERR(trans);
5307 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
5309 btrfs_abort_transaction(trans, ret);
5310 btrfs_end_transaction(trans);
5314 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
5315 offset, 0, 0, len, 0, len, 0, 0, 0);
5317 btrfs_abort_transaction(trans, ret);
5319 btrfs_update_inode(trans, root, inode);
5320 btrfs_end_transaction(trans);
5325 * This function puts in dummy file extents for the area we're creating a hole
5326 * for. So if we are truncating this file to a larger size we need to insert
5327 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5328 * the range between oldsize and size
5330 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
5332 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5333 struct btrfs_root *root = BTRFS_I(inode)->root;
5334 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5335 struct extent_map *em = NULL;
5336 struct extent_state *cached_state = NULL;
5337 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5338 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5339 u64 block_end = ALIGN(size, fs_info->sectorsize);
5346 * If our size started in the middle of a block we need to zero out the
5347 * rest of the block before we expand the i_size, otherwise we could
5348 * expose stale data.
5350 err = btrfs_truncate_block(inode, oldsize, 0, 0);
5354 if (size <= hole_start)
5357 btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start,
5358 block_end - 1, &cached_state);
5359 cur_offset = hole_start;
5361 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
5362 block_end - cur_offset, 0);
5368 last_byte = min(extent_map_end(em), block_end);
5369 last_byte = ALIGN(last_byte, fs_info->sectorsize);
5370 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
5371 struct extent_map *hole_em;
5372 hole_size = last_byte - cur_offset;
5374 err = maybe_insert_hole(root, inode, cur_offset,
5378 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
5379 cur_offset + hole_size - 1, 0);
5380 hole_em = alloc_extent_map();
5382 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5383 &BTRFS_I(inode)->runtime_flags);
5386 hole_em->start = cur_offset;
5387 hole_em->len = hole_size;
5388 hole_em->orig_start = cur_offset;
5390 hole_em->block_start = EXTENT_MAP_HOLE;
5391 hole_em->block_len = 0;
5392 hole_em->orig_block_len = 0;
5393 hole_em->ram_bytes = hole_size;
5394 hole_em->bdev = fs_info->fs_devices->latest_bdev;
5395 hole_em->compress_type = BTRFS_COMPRESS_NONE;
5396 hole_em->generation = fs_info->generation;
5399 write_lock(&em_tree->lock);
5400 err = add_extent_mapping(em_tree, hole_em, 1);
5401 write_unlock(&em_tree->lock);
5404 btrfs_drop_extent_cache(BTRFS_I(inode),
5409 free_extent_map(hole_em);
5412 free_extent_map(em);
5414 cur_offset = last_byte;
5415 if (cur_offset >= block_end)
5418 free_extent_map(em);
5419 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
5423 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
5425 struct btrfs_root *root = BTRFS_I(inode)->root;
5426 struct btrfs_trans_handle *trans;
5427 loff_t oldsize = i_size_read(inode);
5428 loff_t newsize = attr->ia_size;
5429 int mask = attr->ia_valid;
5433 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5434 * special case where we need to update the times despite not having
5435 * these flags set. For all other operations the VFS set these flags
5436 * explicitly if it wants a timestamp update.
5438 if (newsize != oldsize) {
5439 inode_inc_iversion(inode);
5440 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5441 inode->i_ctime = inode->i_mtime =
5442 current_time(inode);
5445 if (newsize > oldsize) {
5447 * Don't do an expanding truncate while snapshotting is ongoing.
5448 * This is to ensure the snapshot captures a fully consistent
5449 * state of this file - if the snapshot captures this expanding
5450 * truncation, it must capture all writes that happened before
5453 btrfs_wait_for_snapshot_creation(root);
5454 ret = btrfs_cont_expand(inode, oldsize, newsize);
5456 btrfs_end_write_no_snapshotting(root);
5460 trans = btrfs_start_transaction(root, 1);
5461 if (IS_ERR(trans)) {
5462 btrfs_end_write_no_snapshotting(root);
5463 return PTR_ERR(trans);
5466 i_size_write(inode, newsize);
5467 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
5468 pagecache_isize_extended(inode, oldsize, newsize);
5469 ret = btrfs_update_inode(trans, root, inode);
5470 btrfs_end_write_no_snapshotting(root);
5471 btrfs_end_transaction(trans);
5475 * We're truncating a file that used to have good data down to
5476 * zero. Make sure it gets into the ordered flush list so that
5477 * any new writes get down to disk quickly.
5480 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
5481 &BTRFS_I(inode)->runtime_flags);
5483 truncate_setsize(inode, newsize);
5485 /* Disable nonlocked read DIO to avoid the endless truncate */
5486 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
5487 inode_dio_wait(inode);
5488 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
5490 ret = btrfs_truncate(inode, newsize == oldsize);
5491 if (ret && inode->i_nlink) {
5495 * Truncate failed, so fix up the in-memory size. We
5496 * adjusted disk_i_size down as we removed extents, so
5497 * wait for disk_i_size to be stable and then update the
5498 * in-memory size to match.
5500 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5503 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5510 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5512 struct inode *inode = d_inode(dentry);
5513 struct btrfs_root *root = BTRFS_I(inode)->root;
5516 if (btrfs_root_readonly(root))
5519 err = setattr_prepare(dentry, attr);
5523 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
5524 err = btrfs_setsize(inode, attr);
5529 if (attr->ia_valid) {
5530 setattr_copy(inode, attr);
5531 inode_inc_iversion(inode);
5532 err = btrfs_dirty_inode(inode);
5534 if (!err && attr->ia_valid & ATTR_MODE)
5535 err = posix_acl_chmod(inode, inode->i_mode);
5542 * While truncating the inode pages during eviction, we get the VFS calling
5543 * btrfs_invalidatepage() against each page of the inode. This is slow because
5544 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5545 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5546 * extent_state structures over and over, wasting lots of time.
5548 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5549 * those expensive operations on a per page basis and do only the ordered io
5550 * finishing, while we release here the extent_map and extent_state structures,
5551 * without the excessive merging and splitting.
5553 static void evict_inode_truncate_pages(struct inode *inode)
5555 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5556 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5557 struct rb_node *node;
5559 ASSERT(inode->i_state & I_FREEING);
5560 truncate_inode_pages_final(&inode->i_data);
5562 write_lock(&map_tree->lock);
5563 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
5564 struct extent_map *em;
5566 node = rb_first_cached(&map_tree->map);
5567 em = rb_entry(node, struct extent_map, rb_node);
5568 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5569 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
5570 remove_extent_mapping(map_tree, em);
5571 free_extent_map(em);
5572 if (need_resched()) {
5573 write_unlock(&map_tree->lock);
5575 write_lock(&map_tree->lock);
5578 write_unlock(&map_tree->lock);
5581 * Keep looping until we have no more ranges in the io tree.
5582 * We can have ongoing bios started by readpages (called from readahead)
5583 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5584 * still in progress (unlocked the pages in the bio but did not yet
5585 * unlocked the ranges in the io tree). Therefore this means some
5586 * ranges can still be locked and eviction started because before
5587 * submitting those bios, which are executed by a separate task (work
5588 * queue kthread), inode references (inode->i_count) were not taken
5589 * (which would be dropped in the end io callback of each bio).
5590 * Therefore here we effectively end up waiting for those bios and
5591 * anyone else holding locked ranges without having bumped the inode's
5592 * reference count - if we don't do it, when they access the inode's
5593 * io_tree to unlock a range it may be too late, leading to an
5594 * use-after-free issue.
5596 spin_lock(&io_tree->lock);
5597 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5598 struct extent_state *state;
5599 struct extent_state *cached_state = NULL;
5602 unsigned state_flags;
5604 node = rb_first(&io_tree->state);
5605 state = rb_entry(node, struct extent_state, rb_node);
5606 start = state->start;
5608 state_flags = state->state;
5609 spin_unlock(&io_tree->lock);
5611 lock_extent_bits(io_tree, start, end, &cached_state);
5614 * If still has DELALLOC flag, the extent didn't reach disk,
5615 * and its reserved space won't be freed by delayed_ref.
5616 * So we need to free its reserved space here.
5617 * (Refer to comment in btrfs_invalidatepage, case 2)
5619 * Note, end is the bytenr of last byte, so we need + 1 here.
5621 if (state_flags & EXTENT_DELALLOC)
5622 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
5624 clear_extent_bit(io_tree, start, end,
5625 EXTENT_LOCKED | EXTENT_DELALLOC |
5626 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5630 spin_lock(&io_tree->lock);
5632 spin_unlock(&io_tree->lock);
5635 static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
5636 struct btrfs_block_rsv *rsv)
5638 struct btrfs_fs_info *fs_info = root->fs_info;
5639 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5640 struct btrfs_trans_handle *trans;
5641 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
5645 * Eviction should be taking place at some place safe because of our
5646 * delayed iputs. However the normal flushing code will run delayed
5647 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5649 * We reserve the delayed_refs_extra here again because we can't use
5650 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5651 * above. We reserve our extra bit here because we generate a ton of
5652 * delayed refs activity by truncating.
5654 * If we cannot make our reservation we'll attempt to steal from the
5655 * global reserve, because we really want to be able to free up space.
5657 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5658 BTRFS_RESERVE_FLUSH_EVICT);
5661 * Try to steal from the global reserve if there is space for
5664 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5665 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5667 "could not allocate space for delete; will truncate on mount");
5668 return ERR_PTR(-ENOSPC);
5670 delayed_refs_extra = 0;
5673 trans = btrfs_join_transaction(root);
5677 if (delayed_refs_extra) {
5678 trans->block_rsv = &fs_info->trans_block_rsv;
5679 trans->bytes_reserved = delayed_refs_extra;
5680 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5681 delayed_refs_extra, 1);
5686 void btrfs_evict_inode(struct inode *inode)
5688 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5689 struct btrfs_trans_handle *trans;
5690 struct btrfs_root *root = BTRFS_I(inode)->root;
5691 struct btrfs_block_rsv *rsv;
5694 trace_btrfs_inode_evict(inode);
5701 evict_inode_truncate_pages(inode);
5703 if (inode->i_nlink &&
5704 ((btrfs_root_refs(&root->root_item) != 0 &&
5705 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5706 btrfs_is_free_space_inode(BTRFS_I(inode))))
5709 if (is_bad_inode(inode))
5712 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
5714 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
5717 if (inode->i_nlink > 0) {
5718 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5719 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
5723 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
5727 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
5730 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
5733 btrfs_i_size_write(BTRFS_I(inode), 0);
5736 trans = evict_refill_and_join(root, rsv);
5740 trans->block_rsv = rsv;
5742 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
5743 trans->block_rsv = &fs_info->trans_block_rsv;
5744 btrfs_end_transaction(trans);
5745 btrfs_btree_balance_dirty(fs_info);
5746 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5753 * Errors here aren't a big deal, it just means we leave orphan items in
5754 * the tree. They will be cleaned up on the next mount. If the inode
5755 * number gets reused, cleanup deletes the orphan item without doing
5756 * anything, and unlink reuses the existing orphan item.
5758 * If it turns out that we are dropping too many of these, we might want
5759 * to add a mechanism for retrying these after a commit.
5761 trans = evict_refill_and_join(root, rsv);
5762 if (!IS_ERR(trans)) {
5763 trans->block_rsv = rsv;
5764 btrfs_orphan_del(trans, BTRFS_I(inode));
5765 trans->block_rsv = &fs_info->trans_block_rsv;
5766 btrfs_end_transaction(trans);
5769 if (!(root == fs_info->tree_root ||
5770 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
5771 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
5774 btrfs_free_block_rsv(fs_info, rsv);
5777 * If we didn't successfully delete, the orphan item will still be in
5778 * the tree and we'll retry on the next mount. Again, we might also want
5779 * to retry these periodically in the future.
5781 btrfs_remove_delayed_node(BTRFS_I(inode));
5786 * Return the key found in the dir entry in the location pointer, fill @type
5787 * with BTRFS_FT_*, and return 0.
5789 * If no dir entries were found, returns -ENOENT.
5790 * If found a corrupted location in dir entry, returns -EUCLEAN.
5792 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5793 struct btrfs_key *location, u8 *type)
5795 const char *name = dentry->d_name.name;
5796 int namelen = dentry->d_name.len;
5797 struct btrfs_dir_item *di;
5798 struct btrfs_path *path;
5799 struct btrfs_root *root = BTRFS_I(dir)->root;
5802 path = btrfs_alloc_path();
5806 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5808 if (IS_ERR_OR_NULL(di)) {
5809 ret = di ? PTR_ERR(di) : -ENOENT;
5813 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
5814 if (location->type != BTRFS_INODE_ITEM_KEY &&
5815 location->type != BTRFS_ROOT_ITEM_KEY) {
5817 btrfs_warn(root->fs_info,
5818 "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5819 __func__, name, btrfs_ino(BTRFS_I(dir)),
5820 location->objectid, location->type, location->offset);
5823 *type = btrfs_dir_type(path->nodes[0], di);
5825 btrfs_free_path(path);
5830 * when we hit a tree root in a directory, the btrfs part of the inode
5831 * needs to be changed to reflect the root directory of the tree root. This
5832 * is kind of like crossing a mount point.
5834 static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
5836 struct dentry *dentry,
5837 struct btrfs_key *location,
5838 struct btrfs_root **sub_root)
5840 struct btrfs_path *path;
5841 struct btrfs_root *new_root;
5842 struct btrfs_root_ref *ref;
5843 struct extent_buffer *leaf;
5844 struct btrfs_key key;
5848 path = btrfs_alloc_path();
5855 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5856 key.type = BTRFS_ROOT_REF_KEY;
5857 key.offset = location->objectid;
5859 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
5866 leaf = path->nodes[0];
5867 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
5868 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
5869 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5872 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5873 (unsigned long)(ref + 1),
5874 dentry->d_name.len);
5878 btrfs_release_path(path);
5880 new_root = btrfs_read_fs_root_no_name(fs_info, location);
5881 if (IS_ERR(new_root)) {
5882 err = PTR_ERR(new_root);
5886 *sub_root = new_root;
5887 location->objectid = btrfs_root_dirid(&new_root->root_item);
5888 location->type = BTRFS_INODE_ITEM_KEY;
5889 location->offset = 0;
5892 btrfs_free_path(path);
5896 static void inode_tree_add(struct inode *inode)
5898 struct btrfs_root *root = BTRFS_I(inode)->root;
5899 struct btrfs_inode *entry;
5901 struct rb_node *parent;
5902 struct rb_node *new = &BTRFS_I(inode)->rb_node;
5903 u64 ino = btrfs_ino(BTRFS_I(inode));
5905 if (inode_unhashed(inode))
5908 spin_lock(&root->inode_lock);
5909 p = &root->inode_tree.rb_node;
5912 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5914 if (ino < btrfs_ino(entry))
5915 p = &parent->rb_left;
5916 else if (ino > btrfs_ino(entry))
5917 p = &parent->rb_right;
5919 WARN_ON(!(entry->vfs_inode.i_state &
5920 (I_WILL_FREE | I_FREEING)));
5921 rb_replace_node(parent, new, &root->inode_tree);
5922 RB_CLEAR_NODE(parent);
5923 spin_unlock(&root->inode_lock);
5927 rb_link_node(new, parent, p);
5928 rb_insert_color(new, &root->inode_tree);
5929 spin_unlock(&root->inode_lock);
5932 static void inode_tree_del(struct inode *inode)
5934 struct btrfs_root *root = BTRFS_I(inode)->root;
5937 spin_lock(&root->inode_lock);
5938 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
5939 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
5940 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
5941 empty = RB_EMPTY_ROOT(&root->inode_tree);
5943 spin_unlock(&root->inode_lock);
5945 if (empty && btrfs_root_refs(&root->root_item) == 0) {
5946 spin_lock(&root->inode_lock);
5947 empty = RB_EMPTY_ROOT(&root->inode_tree);
5948 spin_unlock(&root->inode_lock);
5950 btrfs_add_dead_root(root);
5955 static int btrfs_init_locked_inode(struct inode *inode, void *p)
5957 struct btrfs_iget_args *args = p;
5958 inode->i_ino = args->location->objectid;
5959 memcpy(&BTRFS_I(inode)->location, args->location,
5960 sizeof(*args->location));
5961 BTRFS_I(inode)->root = args->root;
5965 static int btrfs_find_actor(struct inode *inode, void *opaque)
5967 struct btrfs_iget_args *args = opaque;
5968 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
5969 args->root == BTRFS_I(inode)->root;
5972 static struct inode *btrfs_iget_locked(struct super_block *s,
5973 struct btrfs_key *location,
5974 struct btrfs_root *root)
5976 struct inode *inode;
5977 struct btrfs_iget_args args;
5978 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
5980 args.location = location;
5983 inode = iget5_locked(s, hashval, btrfs_find_actor,
5984 btrfs_init_locked_inode,
5989 /* Get an inode object given its location and corresponding root.
5990 * Returns in *is_new if the inode was read from disk
5992 struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
5993 struct btrfs_root *root, int *new,
5994 struct btrfs_path *path)
5996 struct inode *inode;
5998 inode = btrfs_iget_locked(s, location, root);
6000 return ERR_PTR(-ENOMEM);
6002 if (inode->i_state & I_NEW) {
6005 ret = btrfs_read_locked_inode(inode, path);
6007 inode_tree_add(inode);
6008 unlock_new_inode(inode);
6014 * ret > 0 can come from btrfs_search_slot called by
6015 * btrfs_read_locked_inode, this means the inode item
6020 inode = ERR_PTR(ret);
6027 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
6028 struct btrfs_root *root, int *new)
6030 return btrfs_iget_path(s, location, root, new, NULL);
6033 static struct inode *new_simple_dir(struct super_block *s,
6034 struct btrfs_key *key,
6035 struct btrfs_root *root)
6037 struct inode *inode = new_inode(s);
6040 return ERR_PTR(-ENOMEM);
6042 BTRFS_I(inode)->root = root;
6043 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
6044 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
6046 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
6047 inode->i_op = &btrfs_dir_ro_inode_operations;
6048 inode->i_opflags &= ~IOP_XATTR;
6049 inode->i_fop = &simple_dir_operations;
6050 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
6051 inode->i_mtime = current_time(inode);
6052 inode->i_atime = inode->i_mtime;
6053 inode->i_ctime = inode->i_mtime;
6054 BTRFS_I(inode)->i_otime = inode->i_mtime;
6059 static inline u8 btrfs_inode_type(struct inode *inode)
6062 * Compile-time asserts that generic FT_* types still match
6065 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
6066 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
6067 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
6068 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
6069 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
6070 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
6071 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
6072 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
6074 return fs_umode_to_ftype(inode->i_mode);
6077 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
6079 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6080 struct inode *inode;
6081 struct btrfs_root *root = BTRFS_I(dir)->root;
6082 struct btrfs_root *sub_root = root;
6083 struct btrfs_key location;
6088 if (dentry->d_name.len > BTRFS_NAME_LEN)
6089 return ERR_PTR(-ENAMETOOLONG);
6091 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
6093 return ERR_PTR(ret);
6095 if (location.type == BTRFS_INODE_ITEM_KEY) {
6096 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
6100 /* Do extra check against inode mode with di_type */
6101 if (btrfs_inode_type(inode) != di_type) {
6103 "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
6104 inode->i_mode, btrfs_inode_type(inode),
6107 return ERR_PTR(-EUCLEAN);
6112 index = srcu_read_lock(&fs_info->subvol_srcu);
6113 ret = fixup_tree_root_location(fs_info, dir, dentry,
6114 &location, &sub_root);
6117 inode = ERR_PTR(ret);
6119 inode = new_simple_dir(dir->i_sb, &location, sub_root);
6121 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
6123 srcu_read_unlock(&fs_info->subvol_srcu, index);
6125 if (!IS_ERR(inode) && root != sub_root) {
6126 down_read(&fs_info->cleanup_work_sem);
6127 if (!sb_rdonly(inode->i_sb))
6128 ret = btrfs_orphan_cleanup(sub_root);
6129 up_read(&fs_info->cleanup_work_sem);
6132 inode = ERR_PTR(ret);
6139 static int btrfs_dentry_delete(const struct dentry *dentry)
6141 struct btrfs_root *root;
6142 struct inode *inode = d_inode(dentry);
6144 if (!inode && !IS_ROOT(dentry))
6145 inode = d_inode(dentry->d_parent);
6148 root = BTRFS_I(inode)->root;
6149 if (btrfs_root_refs(&root->root_item) == 0)
6152 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6158 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
6161 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
6163 if (inode == ERR_PTR(-ENOENT))
6165 return d_splice_alias(inode, dentry);
6169 * All this infrastructure exists because dir_emit can fault, and we are holding
6170 * the tree lock when doing readdir. For now just allocate a buffer and copy
6171 * our information into that, and then dir_emit from the buffer. This is
6172 * similar to what NFS does, only we don't keep the buffer around in pagecache
6173 * because I'm afraid I'll mess that up. Long term we need to make filldir do
6174 * copy_to_user_inatomic so we don't have to worry about page faulting under the
6177 static int btrfs_opendir(struct inode *inode, struct file *file)
6179 struct btrfs_file_private *private;
6181 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
6184 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
6185 if (!private->filldir_buf) {
6189 file->private_data = private;
6200 static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
6203 struct dir_entry *entry = addr;
6204 char *name = (char *)(entry + 1);
6206 ctx->pos = get_unaligned(&entry->offset);
6207 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
6208 get_unaligned(&entry->ino),
6209 get_unaligned(&entry->type)))
6211 addr += sizeof(struct dir_entry) +
6212 get_unaligned(&entry->name_len);
6218 static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
6220 struct inode *inode = file_inode(file);
6221 struct btrfs_root *root = BTRFS_I(inode)->root;
6222 struct btrfs_file_private *private = file->private_data;
6223 struct btrfs_dir_item *di;
6224 struct btrfs_key key;
6225 struct btrfs_key found_key;
6226 struct btrfs_path *path;
6228 struct list_head ins_list;
6229 struct list_head del_list;
6231 struct extent_buffer *leaf;
6238 struct btrfs_key location;
6240 if (!dir_emit_dots(file, ctx))
6243 path = btrfs_alloc_path();
6247 addr = private->filldir_buf;
6248 path->reada = READA_FORWARD;
6250 INIT_LIST_HEAD(&ins_list);
6251 INIT_LIST_HEAD(&del_list);
6252 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
6255 key.type = BTRFS_DIR_INDEX_KEY;
6256 key.offset = ctx->pos;
6257 key.objectid = btrfs_ino(BTRFS_I(inode));
6259 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6264 struct dir_entry *entry;
6266 leaf = path->nodes[0];
6267 slot = path->slots[0];
6268 if (slot >= btrfs_header_nritems(leaf)) {
6269 ret = btrfs_next_leaf(root, path);
6277 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6279 if (found_key.objectid != key.objectid)
6281 if (found_key.type != BTRFS_DIR_INDEX_KEY)
6283 if (found_key.offset < ctx->pos)
6285 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
6287 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
6288 name_len = btrfs_dir_name_len(leaf, di);
6289 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6291 btrfs_release_path(path);
6292 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6295 addr = private->filldir_buf;
6302 put_unaligned(name_len, &entry->name_len);
6303 name_ptr = (char *)(entry + 1);
6304 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6306 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
6308 btrfs_dir_item_key_to_cpu(leaf, di, &location);
6309 put_unaligned(location.objectid, &entry->ino);
6310 put_unaligned(found_key.offset, &entry->offset);
6312 addr += sizeof(struct dir_entry) + name_len;
6313 total_len += sizeof(struct dir_entry) + name_len;
6317 btrfs_release_path(path);
6319 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6323 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
6328 * Stop new entries from being returned after we return the last
6331 * New directory entries are assigned a strictly increasing
6332 * offset. This means that new entries created during readdir
6333 * are *guaranteed* to be seen in the future by that readdir.
6334 * This has broken buggy programs which operate on names as
6335 * they're returned by readdir. Until we re-use freed offsets
6336 * we have this hack to stop new entries from being returned
6337 * under the assumption that they'll never reach this huge
6340 * This is being careful not to overflow 32bit loff_t unless the
6341 * last entry requires it because doing so has broken 32bit apps
6344 if (ctx->pos >= INT_MAX)
6345 ctx->pos = LLONG_MAX;
6352 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
6353 btrfs_free_path(path);
6358 * This is somewhat expensive, updating the tree every time the
6359 * inode changes. But, it is most likely to find the inode in cache.
6360 * FIXME, needs more benchmarking...there are no reasons other than performance
6361 * to keep or drop this code.
6363 static int btrfs_dirty_inode(struct inode *inode)
6365 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6366 struct btrfs_root *root = BTRFS_I(inode)->root;
6367 struct btrfs_trans_handle *trans;
6370 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
6373 trans = btrfs_join_transaction(root);
6375 return PTR_ERR(trans);
6377 ret = btrfs_update_inode(trans, root, inode);
6378 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
6379 /* whoops, lets try again with the full transaction */
6380 btrfs_end_transaction(trans);
6381 trans = btrfs_start_transaction(root, 1);
6383 return PTR_ERR(trans);
6385 ret = btrfs_update_inode(trans, root, inode);
6387 btrfs_end_transaction(trans);
6388 if (BTRFS_I(inode)->delayed_node)
6389 btrfs_balance_delayed_items(fs_info);
6395 * This is a copy of file_update_time. We need this so we can return error on
6396 * ENOSPC for updating the inode in the case of file write and mmap writes.
6398 static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
6401 struct btrfs_root *root = BTRFS_I(inode)->root;
6402 bool dirty = flags & ~S_VERSION;
6404 if (btrfs_root_readonly(root))
6407 if (flags & S_VERSION)
6408 dirty |= inode_maybe_inc_iversion(inode, dirty);
6409 if (flags & S_CTIME)
6410 inode->i_ctime = *now;
6411 if (flags & S_MTIME)
6412 inode->i_mtime = *now;
6413 if (flags & S_ATIME)
6414 inode->i_atime = *now;
6415 return dirty ? btrfs_dirty_inode(inode) : 0;
6419 * find the highest existing sequence number in a directory
6420 * and then set the in-memory index_cnt variable to reflect
6421 * free sequence numbers
6423 static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
6425 struct btrfs_root *root = inode->root;
6426 struct btrfs_key key, found_key;
6427 struct btrfs_path *path;
6428 struct extent_buffer *leaf;
6431 key.objectid = btrfs_ino(inode);
6432 key.type = BTRFS_DIR_INDEX_KEY;
6433 key.offset = (u64)-1;
6435 path = btrfs_alloc_path();
6439 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6442 /* FIXME: we should be able to handle this */
6448 * MAGIC NUMBER EXPLANATION:
6449 * since we search a directory based on f_pos we have to start at 2
6450 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6451 * else has to start at 2
6453 if (path->slots[0] == 0) {
6454 inode->index_cnt = 2;
6460 leaf = path->nodes[0];
6461 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6463 if (found_key.objectid != btrfs_ino(inode) ||
6464 found_key.type != BTRFS_DIR_INDEX_KEY) {
6465 inode->index_cnt = 2;
6469 inode->index_cnt = found_key.offset + 1;
6471 btrfs_free_path(path);
6476 * helper to find a free sequence number in a given directory. This current
6477 * code is very simple, later versions will do smarter things in the btree
6479 int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
6483 if (dir->index_cnt == (u64)-1) {
6484 ret = btrfs_inode_delayed_dir_index_count(dir);
6486 ret = btrfs_set_inode_index_count(dir);
6492 *index = dir->index_cnt;
6498 static int btrfs_insert_inode_locked(struct inode *inode)
6500 struct btrfs_iget_args args;
6501 args.location = &BTRFS_I(inode)->location;
6502 args.root = BTRFS_I(inode)->root;
6504 return insert_inode_locked4(inode,
6505 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6506 btrfs_find_actor, &args);
6510 * Inherit flags from the parent inode.
6512 * Currently only the compression flags and the cow flags are inherited.
6514 static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6521 flags = BTRFS_I(dir)->flags;
6523 if (flags & BTRFS_INODE_NOCOMPRESS) {
6524 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6525 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6526 } else if (flags & BTRFS_INODE_COMPRESS) {
6527 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6528 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6531 if (flags & BTRFS_INODE_NODATACOW) {
6532 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6533 if (S_ISREG(inode->i_mode))
6534 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6537 btrfs_sync_inode_flags_to_i_flags(inode);
6540 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6541 struct btrfs_root *root,
6543 const char *name, int name_len,
6544 u64 ref_objectid, u64 objectid,
6545 umode_t mode, u64 *index)
6547 struct btrfs_fs_info *fs_info = root->fs_info;
6548 struct inode *inode;
6549 struct btrfs_inode_item *inode_item;
6550 struct btrfs_key *location;
6551 struct btrfs_path *path;
6552 struct btrfs_inode_ref *ref;
6553 struct btrfs_key key[2];
6555 int nitems = name ? 2 : 1;
6557 unsigned int nofs_flag;
6560 path = btrfs_alloc_path();
6562 return ERR_PTR(-ENOMEM);
6564 nofs_flag = memalloc_nofs_save();
6565 inode = new_inode(fs_info->sb);
6566 memalloc_nofs_restore(nofs_flag);
6568 btrfs_free_path(path);
6569 return ERR_PTR(-ENOMEM);
6573 * O_TMPFILE, set link count to 0, so that after this point,
6574 * we fill in an inode item with the correct link count.
6577 set_nlink(inode, 0);
6580 * we have to initialize this early, so we can reclaim the inode
6581 * number if we fail afterwards in this function.
6583 inode->i_ino = objectid;
6586 trace_btrfs_inode_request(dir);
6588 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
6590 btrfs_free_path(path);
6592 return ERR_PTR(ret);
6598 * index_cnt is ignored for everything but a dir,
6599 * btrfs_set_inode_index_count has an explanation for the magic
6602 BTRFS_I(inode)->index_cnt = 2;
6603 BTRFS_I(inode)->dir_index = *index;
6604 BTRFS_I(inode)->root = root;
6605 BTRFS_I(inode)->generation = trans->transid;
6606 inode->i_generation = BTRFS_I(inode)->generation;
6609 * We could have gotten an inode number from somebody who was fsynced
6610 * and then removed in this same transaction, so let's just set full
6611 * sync since it will be a full sync anyway and this will blow away the
6612 * old info in the log.
6614 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6616 key[0].objectid = objectid;
6617 key[0].type = BTRFS_INODE_ITEM_KEY;
6620 sizes[0] = sizeof(struct btrfs_inode_item);
6624 * Start new inodes with an inode_ref. This is slightly more
6625 * efficient for small numbers of hard links since they will
6626 * be packed into one item. Extended refs will kick in if we
6627 * add more hard links than can fit in the ref item.
6629 key[1].objectid = objectid;
6630 key[1].type = BTRFS_INODE_REF_KEY;
6631 key[1].offset = ref_objectid;
6633 sizes[1] = name_len + sizeof(*ref);
6636 location = &BTRFS_I(inode)->location;
6637 location->objectid = objectid;
6638 location->offset = 0;
6639 location->type = BTRFS_INODE_ITEM_KEY;
6641 ret = btrfs_insert_inode_locked(inode);
6647 path->leave_spinning = 1;
6648 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
6652 inode_init_owner(inode, dir, mode);
6653 inode_set_bytes(inode, 0);
6655 inode->i_mtime = current_time(inode);
6656 inode->i_atime = inode->i_mtime;
6657 inode->i_ctime = inode->i_mtime;
6658 BTRFS_I(inode)->i_otime = inode->i_mtime;
6660 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6661 struct btrfs_inode_item);
6662 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
6663 sizeof(*inode_item));
6664 fill_inode_item(trans, path->nodes[0], inode_item, inode);
6667 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6668 struct btrfs_inode_ref);
6669 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6670 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6671 ptr = (unsigned long)(ref + 1);
6672 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6675 btrfs_mark_buffer_dirty(path->nodes[0]);
6676 btrfs_free_path(path);
6678 btrfs_inherit_iflags(inode, dir);
6680 if (S_ISREG(mode)) {
6681 if (btrfs_test_opt(fs_info, NODATASUM))
6682 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6683 if (btrfs_test_opt(fs_info, NODATACOW))
6684 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6685 BTRFS_INODE_NODATASUM;
6688 inode_tree_add(inode);
6690 trace_btrfs_inode_new(inode);
6691 btrfs_set_inode_last_trans(trans, inode);
6693 btrfs_update_root_times(trans, root);
6695 ret = btrfs_inode_inherit_props(trans, inode, dir);
6698 "error inheriting props for ino %llu (root %llu): %d",
6699 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
6704 discard_new_inode(inode);
6707 BTRFS_I(dir)->index_cnt--;
6708 btrfs_free_path(path);
6709 return ERR_PTR(ret);
6713 * utility function to add 'inode' into 'parent_inode' with
6714 * a give name and a given sequence number.
6715 * if 'add_backref' is true, also insert a backref from the
6716 * inode to the parent directory.
6718 int btrfs_add_link(struct btrfs_trans_handle *trans,
6719 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
6720 const char *name, int name_len, int add_backref, u64 index)
6723 struct btrfs_key key;
6724 struct btrfs_root *root = parent_inode->root;
6725 u64 ino = btrfs_ino(inode);
6726 u64 parent_ino = btrfs_ino(parent_inode);
6728 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6729 memcpy(&key, &inode->root->root_key, sizeof(key));
6732 key.type = BTRFS_INODE_ITEM_KEY;
6736 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6737 ret = btrfs_add_root_ref(trans, key.objectid,
6738 root->root_key.objectid, parent_ino,
6739 index, name, name_len);
6740 } else if (add_backref) {
6741 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6745 /* Nothing to clean up yet */
6749 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
6750 btrfs_inode_type(&inode->vfs_inode), index);
6751 if (ret == -EEXIST || ret == -EOVERFLOW)
6754 btrfs_abort_transaction(trans, ret);
6758 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
6760 inode_inc_iversion(&parent_inode->vfs_inode);
6762 * If we are replaying a log tree, we do not want to update the mtime
6763 * and ctime of the parent directory with the current time, since the
6764 * log replay procedure is responsible for setting them to their correct
6765 * values (the ones it had when the fsync was done).
6767 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6768 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6770 parent_inode->vfs_inode.i_mtime = now;
6771 parent_inode->vfs_inode.i_ctime = now;
6773 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
6775 btrfs_abort_transaction(trans, ret);
6779 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6782 err = btrfs_del_root_ref(trans, key.objectid,
6783 root->root_key.objectid, parent_ino,
6784 &local_index, name, name_len);
6786 btrfs_abort_transaction(trans, err);
6787 } else if (add_backref) {
6791 err = btrfs_del_inode_ref(trans, root, name, name_len,
6792 ino, parent_ino, &local_index);
6794 btrfs_abort_transaction(trans, err);
6797 /* Return the original error code */
6801 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
6802 struct btrfs_inode *dir, struct dentry *dentry,
6803 struct btrfs_inode *inode, int backref, u64 index)
6805 int err = btrfs_add_link(trans, dir, inode,
6806 dentry->d_name.name, dentry->d_name.len,
6813 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
6814 umode_t mode, dev_t rdev)
6816 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6817 struct btrfs_trans_handle *trans;
6818 struct btrfs_root *root = BTRFS_I(dir)->root;
6819 struct inode *inode = NULL;
6825 * 2 for inode item and ref
6827 * 1 for xattr if selinux is on
6829 trans = btrfs_start_transaction(root, 5);
6831 return PTR_ERR(trans);
6833 err = btrfs_find_free_objectid(root, &objectid);
6837 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6838 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6840 if (IS_ERR(inode)) {
6841 err = PTR_ERR(inode);
6847 * If the active LSM wants to access the inode during
6848 * d_instantiate it needs these. Smack checks to see
6849 * if the filesystem supports xattrs by looking at the
6852 inode->i_op = &btrfs_special_inode_operations;
6853 init_special_inode(inode, inode->i_mode, rdev);
6855 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6859 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6864 btrfs_update_inode(trans, root, inode);
6865 d_instantiate_new(dentry, inode);
6868 btrfs_end_transaction(trans);
6869 btrfs_btree_balance_dirty(fs_info);
6871 inode_dec_link_count(inode);
6872 discard_new_inode(inode);
6877 static int btrfs_create(struct inode *dir, struct dentry *dentry,
6878 umode_t mode, bool excl)
6880 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6881 struct btrfs_trans_handle *trans;
6882 struct btrfs_root *root = BTRFS_I(dir)->root;
6883 struct inode *inode = NULL;
6889 * 2 for inode item and ref
6891 * 1 for xattr if selinux is on
6893 trans = btrfs_start_transaction(root, 5);
6895 return PTR_ERR(trans);
6897 err = btrfs_find_free_objectid(root, &objectid);
6901 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6902 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6904 if (IS_ERR(inode)) {
6905 err = PTR_ERR(inode);
6910 * If the active LSM wants to access the inode during
6911 * d_instantiate it needs these. Smack checks to see
6912 * if the filesystem supports xattrs by looking at the
6915 inode->i_fop = &btrfs_file_operations;
6916 inode->i_op = &btrfs_file_inode_operations;
6917 inode->i_mapping->a_ops = &btrfs_aops;
6919 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6923 err = btrfs_update_inode(trans, root, inode);
6927 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6932 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
6933 d_instantiate_new(dentry, inode);
6936 btrfs_end_transaction(trans);
6938 inode_dec_link_count(inode);
6939 discard_new_inode(inode);
6941 btrfs_btree_balance_dirty(fs_info);
6945 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6946 struct dentry *dentry)
6948 struct btrfs_trans_handle *trans = NULL;
6949 struct btrfs_root *root = BTRFS_I(dir)->root;
6950 struct inode *inode = d_inode(old_dentry);
6951 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6956 /* do not allow sys_link's with other subvols of the same device */
6957 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
6960 if (inode->i_nlink >= BTRFS_LINK_MAX)
6963 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
6968 * 2 items for inode and inode ref
6969 * 2 items for dir items
6970 * 1 item for parent inode
6971 * 1 item for orphan item deletion if O_TMPFILE
6973 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
6974 if (IS_ERR(trans)) {
6975 err = PTR_ERR(trans);
6980 /* There are several dir indexes for this inode, clear the cache. */
6981 BTRFS_I(inode)->dir_index = 0ULL;
6983 inode_inc_iversion(inode);
6984 inode->i_ctime = current_time(inode);
6986 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
6988 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6994 struct dentry *parent = dentry->d_parent;
6996 err = btrfs_update_inode(trans, root, inode);
6999 if (inode->i_nlink == 1) {
7001 * If new hard link count is 1, it's a file created
7002 * with open(2) O_TMPFILE flag.
7004 err = btrfs_orphan_del(trans, BTRFS_I(inode));
7008 d_instantiate(dentry, inode);
7009 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
7014 btrfs_end_transaction(trans);
7016 inode_dec_link_count(inode);
7019 btrfs_btree_balance_dirty(fs_info);
7023 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
7025 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
7026 struct inode *inode = NULL;
7027 struct btrfs_trans_handle *trans;
7028 struct btrfs_root *root = BTRFS_I(dir)->root;
7034 * 2 items for inode and ref
7035 * 2 items for dir items
7036 * 1 for xattr if selinux is on
7038 trans = btrfs_start_transaction(root, 5);
7040 return PTR_ERR(trans);
7042 err = btrfs_find_free_objectid(root, &objectid);
7046 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7047 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
7048 S_IFDIR | mode, &index);
7049 if (IS_ERR(inode)) {
7050 err = PTR_ERR(inode);
7055 /* these must be set before we unlock the inode */
7056 inode->i_op = &btrfs_dir_inode_operations;
7057 inode->i_fop = &btrfs_dir_file_operations;
7059 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
7063 btrfs_i_size_write(BTRFS_I(inode), 0);
7064 err = btrfs_update_inode(trans, root, inode);
7068 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
7069 dentry->d_name.name,
7070 dentry->d_name.len, 0, index);
7074 d_instantiate_new(dentry, inode);
7077 btrfs_end_transaction(trans);
7079 inode_dec_link_count(inode);
7080 discard_new_inode(inode);
7082 btrfs_btree_balance_dirty(fs_info);
7086 static noinline int uncompress_inline(struct btrfs_path *path,
7088 size_t pg_offset, u64 extent_offset,
7089 struct btrfs_file_extent_item *item)
7092 struct extent_buffer *leaf = path->nodes[0];
7095 unsigned long inline_size;
7099 WARN_ON(pg_offset != 0);
7100 compress_type = btrfs_file_extent_compression(leaf, item);
7101 max_size = btrfs_file_extent_ram_bytes(leaf, item);
7102 inline_size = btrfs_file_extent_inline_item_len(leaf,
7103 btrfs_item_nr(path->slots[0]));
7104 tmp = kmalloc(inline_size, GFP_NOFS);
7107 ptr = btrfs_file_extent_inline_start(item);
7109 read_extent_buffer(leaf, tmp, ptr, inline_size);
7111 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
7112 ret = btrfs_decompress(compress_type, tmp, page,
7113 extent_offset, inline_size, max_size);
7116 * decompression code contains a memset to fill in any space between the end
7117 * of the uncompressed data and the end of max_size in case the decompressed
7118 * data ends up shorter than ram_bytes. That doesn't cover the hole between
7119 * the end of an inline extent and the beginning of the next block, so we
7120 * cover that region here.
7123 if (max_size + pg_offset < PAGE_SIZE) {
7124 char *map = kmap(page);
7125 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
7133 * a bit scary, this does extent mapping from logical file offset to the disk.
7134 * the ugly parts come from merging extents from the disk with the in-ram
7135 * representation. This gets more complex because of the data=ordered code,
7136 * where the in-ram extents might be locked pending data=ordered completion.
7138 * This also copies inline extents directly into the page.
7140 struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
7142 size_t pg_offset, u64 start, u64 len,
7145 struct btrfs_fs_info *fs_info = inode->root->fs_info;
7148 u64 extent_start = 0;
7150 u64 objectid = btrfs_ino(inode);
7151 int extent_type = -1;
7152 struct btrfs_path *path = NULL;
7153 struct btrfs_root *root = inode->root;
7154 struct btrfs_file_extent_item *item;
7155 struct extent_buffer *leaf;
7156 struct btrfs_key found_key;
7157 struct extent_map *em = NULL;
7158 struct extent_map_tree *em_tree = &inode->extent_tree;
7159 struct extent_io_tree *io_tree = &inode->io_tree;
7160 const bool new_inline = !page || create;
7162 read_lock(&em_tree->lock);
7163 em = lookup_extent_mapping(em_tree, start, len);
7165 em->bdev = fs_info->fs_devices->latest_bdev;
7166 read_unlock(&em_tree->lock);
7169 if (em->start > start || em->start + em->len <= start)
7170 free_extent_map(em);
7171 else if (em->block_start == EXTENT_MAP_INLINE && page)
7172 free_extent_map(em);
7176 em = alloc_extent_map();
7181 em->bdev = fs_info->fs_devices->latest_bdev;
7182 em->start = EXTENT_MAP_HOLE;
7183 em->orig_start = EXTENT_MAP_HOLE;
7185 em->block_len = (u64)-1;
7187 path = btrfs_alloc_path();
7193 /* Chances are we'll be called again, so go ahead and do readahead */
7194 path->reada = READA_FORWARD;
7197 * Unless we're going to uncompress the inline extent, no sleep would
7200 path->leave_spinning = 1;
7202 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
7206 } else if (ret > 0) {
7207 if (path->slots[0] == 0)
7212 leaf = path->nodes[0];
7213 item = btrfs_item_ptr(leaf, path->slots[0],
7214 struct btrfs_file_extent_item);
7215 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7216 if (found_key.objectid != objectid ||
7217 found_key.type != BTRFS_EXTENT_DATA_KEY) {
7219 * If we backup past the first extent we want to move forward
7220 * and see if there is an extent in front of us, otherwise we'll
7221 * say there is a hole for our whole search range which can
7228 extent_type = btrfs_file_extent_type(leaf, item);
7229 extent_start = found_key.offset;
7230 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7231 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
7232 /* Only regular file could have regular/prealloc extent */
7233 if (!S_ISREG(inode->vfs_inode.i_mode)) {
7236 "regular/prealloc extent found for non-regular inode %llu",
7240 extent_end = extent_start +
7241 btrfs_file_extent_num_bytes(leaf, item);
7243 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7245 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
7248 size = btrfs_file_extent_ram_bytes(leaf, item);
7249 extent_end = ALIGN(extent_start + size,
7250 fs_info->sectorsize);
7252 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7257 if (start >= extent_end) {
7259 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7260 ret = btrfs_next_leaf(root, path);
7264 } else if (ret > 0) {
7267 leaf = path->nodes[0];
7269 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7270 if (found_key.objectid != objectid ||
7271 found_key.type != BTRFS_EXTENT_DATA_KEY)
7273 if (start + len <= found_key.offset)
7275 if (start > found_key.offset)
7278 /* New extent overlaps with existing one */
7280 em->orig_start = start;
7281 em->len = found_key.offset - start;
7282 em->block_start = EXTENT_MAP_HOLE;
7286 btrfs_extent_item_to_extent_map(inode, path, item,
7289 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7290 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
7292 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
7296 size_t extent_offset;
7302 size = btrfs_file_extent_ram_bytes(leaf, item);
7303 extent_offset = page_offset(page) + pg_offset - extent_start;
7304 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7305 size - extent_offset);
7306 em->start = extent_start + extent_offset;
7307 em->len = ALIGN(copy_size, fs_info->sectorsize);
7308 em->orig_block_len = em->len;
7309 em->orig_start = em->start;
7310 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
7312 btrfs_set_path_blocking(path);
7313 if (!PageUptodate(page)) {
7314 if (btrfs_file_extent_compression(leaf, item) !=
7315 BTRFS_COMPRESS_NONE) {
7316 ret = uncompress_inline(path, page, pg_offset,
7317 extent_offset, item);
7324 read_extent_buffer(leaf, map + pg_offset, ptr,
7326 if (pg_offset + copy_size < PAGE_SIZE) {
7327 memset(map + pg_offset + copy_size, 0,
7328 PAGE_SIZE - pg_offset -
7333 flush_dcache_page(page);
7335 set_extent_uptodate(io_tree, em->start,
7336 extent_map_end(em) - 1, NULL, GFP_NOFS);
7341 em->orig_start = start;
7343 em->block_start = EXTENT_MAP_HOLE;
7345 btrfs_release_path(path);
7346 if (em->start > start || extent_map_end(em) <= start) {
7348 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7349 em->start, em->len, start, len);
7355 write_lock(&em_tree->lock);
7356 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
7357 write_unlock(&em_tree->lock);
7359 btrfs_free_path(path);
7361 trace_btrfs_get_extent(root, inode, em);
7364 free_extent_map(em);
7365 return ERR_PTR(err);
7367 BUG_ON(!em); /* Error is always set */
7371 struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7374 struct extent_map *em;
7375 struct extent_map *hole_em = NULL;
7376 u64 delalloc_start = start;
7382 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
7386 * If our em maps to:
7388 * - a pre-alloc extent,
7389 * there might actually be delalloc bytes behind it.
7391 if (em->block_start != EXTENT_MAP_HOLE &&
7392 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7397 /* check to see if we've wrapped (len == -1 or similar) */
7406 /* ok, we didn't find anything, lets look for delalloc */
7407 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
7408 end, len, EXTENT_DELALLOC, 1);
7409 delalloc_end = delalloc_start + delalloc_len;
7410 if (delalloc_end < delalloc_start)
7411 delalloc_end = (u64)-1;
7414 * We didn't find anything useful, return the original results from
7417 if (delalloc_start > end || delalloc_end <= start) {
7424 * Adjust the delalloc_start to make sure it doesn't go backwards from
7425 * the start they passed in
7427 delalloc_start = max(start, delalloc_start);
7428 delalloc_len = delalloc_end - delalloc_start;
7430 if (delalloc_len > 0) {
7433 const u64 hole_end = extent_map_end(hole_em);
7435 em = alloc_extent_map();
7444 * When btrfs_get_extent can't find anything it returns one
7447 * Make sure what it found really fits our range, and adjust to
7448 * make sure it is based on the start from the caller
7450 if (hole_end <= start || hole_em->start > end) {
7451 free_extent_map(hole_em);
7454 hole_start = max(hole_em->start, start);
7455 hole_len = hole_end - hole_start;
7458 if (hole_em && delalloc_start > hole_start) {
7460 * Our hole starts before our delalloc, so we have to
7461 * return just the parts of the hole that go until the
7464 em->len = min(hole_len, delalloc_start - hole_start);
7465 em->start = hole_start;
7466 em->orig_start = hole_start;
7468 * Don't adjust block start at all, it is fixed at
7471 em->block_start = hole_em->block_start;
7472 em->block_len = hole_len;
7473 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7474 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7477 * Hole is out of passed range or it starts after
7480 em->start = delalloc_start;
7481 em->len = delalloc_len;
7482 em->orig_start = delalloc_start;
7483 em->block_start = EXTENT_MAP_DELALLOC;
7484 em->block_len = delalloc_len;
7491 free_extent_map(hole_em);
7493 free_extent_map(em);
7494 return ERR_PTR(err);
7499 static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7502 const u64 orig_start,
7503 const u64 block_start,
7504 const u64 block_len,
7505 const u64 orig_block_len,
7506 const u64 ram_bytes,
7509 struct extent_map *em = NULL;
7512 if (type != BTRFS_ORDERED_NOCOW) {
7513 em = create_io_em(inode, start, len, orig_start,
7514 block_start, block_len, orig_block_len,
7516 BTRFS_COMPRESS_NONE, /* compress_type */
7521 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7522 len, block_len, type);
7525 free_extent_map(em);
7526 btrfs_drop_extent_cache(BTRFS_I(inode), start,
7527 start + len - 1, 0);
7536 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7539 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7540 struct btrfs_root *root = BTRFS_I(inode)->root;
7541 struct extent_map *em;
7542 struct btrfs_key ins;
7546 alloc_hint = get_extent_allocation_hint(inode, start, len);
7547 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
7548 0, alloc_hint, &ins, 1, 1);
7550 return ERR_PTR(ret);
7552 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7553 ins.objectid, ins.offset, ins.offset,
7554 ins.offset, BTRFS_ORDERED_REGULAR);
7555 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
7557 btrfs_free_reserved_extent(fs_info, ins.objectid,
7564 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7565 * block must be cow'd
7567 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7568 u64 *orig_start, u64 *orig_block_len,
7569 u64 *ram_bytes, bool strict)
7571 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7572 struct btrfs_path *path;
7574 struct extent_buffer *leaf;
7575 struct btrfs_root *root = BTRFS_I(inode)->root;
7576 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7577 struct btrfs_file_extent_item *fi;
7578 struct btrfs_key key;
7585 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
7587 path = btrfs_alloc_path();
7591 ret = btrfs_lookup_file_extent(NULL, root, path,
7592 btrfs_ino(BTRFS_I(inode)), offset, 0);
7596 slot = path->slots[0];
7599 /* can't find the item, must cow */
7606 leaf = path->nodes[0];
7607 btrfs_item_key_to_cpu(leaf, &key, slot);
7608 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
7609 key.type != BTRFS_EXTENT_DATA_KEY) {
7610 /* not our file or wrong item type, must cow */
7614 if (key.offset > offset) {
7615 /* Wrong offset, must cow */
7619 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7620 found_type = btrfs_file_extent_type(leaf, fi);
7621 if (found_type != BTRFS_FILE_EXTENT_REG &&
7622 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7623 /* not a regular extent, must cow */
7627 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7630 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7631 if (extent_end <= offset)
7634 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7635 if (disk_bytenr == 0)
7638 if (btrfs_file_extent_compression(leaf, fi) ||
7639 btrfs_file_extent_encryption(leaf, fi) ||
7640 btrfs_file_extent_other_encoding(leaf, fi))
7644 * Do the same check as in btrfs_cross_ref_exist but without the
7645 * unnecessary search.
7648 (btrfs_file_extent_generation(leaf, fi) <=
7649 btrfs_root_last_snapshot(&root->root_item)))
7652 backref_offset = btrfs_file_extent_offset(leaf, fi);
7655 *orig_start = key.offset - backref_offset;
7656 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7657 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7660 if (btrfs_extent_readonly(fs_info, disk_bytenr))
7663 num_bytes = min(offset + *len, extent_end) - offset;
7664 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7667 range_end = round_up(offset + num_bytes,
7668 root->fs_info->sectorsize) - 1;
7669 ret = test_range_bit(io_tree, offset, range_end,
7670 EXTENT_DELALLOC, 0, NULL);
7677 btrfs_release_path(path);
7680 * look for other files referencing this extent, if we
7681 * find any we must cow
7684 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
7685 key.offset - backref_offset, disk_bytenr,
7693 * adjust disk_bytenr and num_bytes to cover just the bytes
7694 * in this extent we are about to write. If there
7695 * are any csums in that range we have to cow in order
7696 * to keep the csums correct
7698 disk_bytenr += backref_offset;
7699 disk_bytenr += offset - key.offset;
7700 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7703 * all of the above have passed, it is safe to overwrite this extent
7709 btrfs_free_path(path);
7713 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7714 struct extent_state **cached_state, int writing)
7716 struct btrfs_ordered_extent *ordered;
7720 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7723 * We're concerned with the entire range that we're going to be
7724 * doing DIO to, so we need to make sure there's no ordered
7725 * extents in this range.
7727 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
7728 lockend - lockstart + 1);
7731 * We need to make sure there are no buffered pages in this
7732 * range either, we could have raced between the invalidate in
7733 * generic_file_direct_write and locking the extent. The
7734 * invalidate needs to happen so that reads after a write do not
7738 (!writing || !filemap_range_has_page(inode->i_mapping,
7739 lockstart, lockend)))
7742 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7747 * If we are doing a DIO read and the ordered extent we
7748 * found is for a buffered write, we can not wait for it
7749 * to complete and retry, because if we do so we can
7750 * deadlock with concurrent buffered writes on page
7751 * locks. This happens only if our DIO read covers more
7752 * than one extent map, if at this point has already
7753 * created an ordered extent for a previous extent map
7754 * and locked its range in the inode's io tree, and a
7755 * concurrent write against that previous extent map's
7756 * range and this range started (we unlock the ranges
7757 * in the io tree only when the bios complete and
7758 * buffered writes always lock pages before attempting
7759 * to lock range in the io tree).
7762 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7763 btrfs_start_ordered_extent(inode, ordered, 1);
7766 btrfs_put_ordered_extent(ordered);
7769 * We could trigger writeback for this range (and wait
7770 * for it to complete) and then invalidate the pages for
7771 * this range (through invalidate_inode_pages2_range()),
7772 * but that can lead us to a deadlock with a concurrent
7773 * call to readpages() (a buffered read or a defrag call
7774 * triggered a readahead) on a page lock due to an
7775 * ordered dio extent we created before but did not have
7776 * yet a corresponding bio submitted (whence it can not
7777 * complete), which makes readpages() wait for that
7778 * ordered extent to complete while holding a lock on
7793 /* The callers of this must take lock_extent() */
7794 static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7795 u64 orig_start, u64 block_start,
7796 u64 block_len, u64 orig_block_len,
7797 u64 ram_bytes, int compress_type,
7800 struct extent_map_tree *em_tree;
7801 struct extent_map *em;
7802 struct btrfs_root *root = BTRFS_I(inode)->root;
7805 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7806 type == BTRFS_ORDERED_COMPRESSED ||
7807 type == BTRFS_ORDERED_NOCOW ||
7808 type == BTRFS_ORDERED_REGULAR);
7810 em_tree = &BTRFS_I(inode)->extent_tree;
7811 em = alloc_extent_map();
7813 return ERR_PTR(-ENOMEM);
7816 em->orig_start = orig_start;
7818 em->block_len = block_len;
7819 em->block_start = block_start;
7820 em->bdev = root->fs_info->fs_devices->latest_bdev;
7821 em->orig_block_len = orig_block_len;
7822 em->ram_bytes = ram_bytes;
7823 em->generation = -1;
7824 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7825 if (type == BTRFS_ORDERED_PREALLOC) {
7826 set_bit(EXTENT_FLAG_FILLING, &em->flags);
7827 } else if (type == BTRFS_ORDERED_COMPRESSED) {
7828 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7829 em->compress_type = compress_type;
7833 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
7834 em->start + em->len - 1, 0);
7835 write_lock(&em_tree->lock);
7836 ret = add_extent_mapping(em_tree, em, 1);
7837 write_unlock(&em_tree->lock);
7839 * The caller has taken lock_extent(), who could race with us
7842 } while (ret == -EEXIST);
7845 free_extent_map(em);
7846 return ERR_PTR(ret);
7849 /* em got 2 refs now, callers needs to do free_extent_map once. */
7854 static int btrfs_get_blocks_direct_read(struct extent_map *em,
7855 struct buffer_head *bh_result,
7856 struct inode *inode,
7859 if (em->block_start == EXTENT_MAP_HOLE ||
7860 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7863 len = min(len, em->len - (start - em->start));
7865 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7867 bh_result->b_size = len;
7868 bh_result->b_bdev = em->bdev;
7869 set_buffer_mapped(bh_result);
7874 static int btrfs_get_blocks_direct_write(struct extent_map **map,
7875 struct buffer_head *bh_result,
7876 struct inode *inode,
7877 struct btrfs_dio_data *dio_data,
7880 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7881 struct extent_map *em = *map;
7885 * We don't allocate a new extent in the following cases
7887 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7889 * 2) The extent is marked as PREALLOC. We're good to go here and can
7890 * just use the extent.
7893 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7894 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7895 em->block_start != EXTENT_MAP_HOLE)) {
7897 u64 block_start, orig_start, orig_block_len, ram_bytes;
7899 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7900 type = BTRFS_ORDERED_PREALLOC;
7902 type = BTRFS_ORDERED_NOCOW;
7903 len = min(len, em->len - (start - em->start));
7904 block_start = em->block_start + (start - em->start);
7906 if (can_nocow_extent(inode, start, &len, &orig_start,
7907 &orig_block_len, &ram_bytes, false) == 1 &&
7908 btrfs_inc_nocow_writers(fs_info, block_start)) {
7909 struct extent_map *em2;
7911 em2 = btrfs_create_dio_extent(inode, start, len,
7912 orig_start, block_start,
7913 len, orig_block_len,
7915 btrfs_dec_nocow_writers(fs_info, block_start);
7916 if (type == BTRFS_ORDERED_PREALLOC) {
7917 free_extent_map(em);
7921 if (em2 && IS_ERR(em2)) {
7926 * For inode marked NODATACOW or extent marked PREALLOC,
7927 * use the existing or preallocated extent, so does not
7928 * need to adjust btrfs_space_info's bytes_may_use.
7930 btrfs_free_reserved_data_space_noquota(inode, start,
7936 /* this will cow the extent */
7937 len = bh_result->b_size;
7938 free_extent_map(em);
7939 *map = em = btrfs_new_extent_direct(inode, start, len);
7945 len = min(len, em->len - (start - em->start));
7948 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7950 bh_result->b_size = len;
7951 bh_result->b_bdev = em->bdev;
7952 set_buffer_mapped(bh_result);
7954 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7955 set_buffer_new(bh_result);
7958 * Need to update the i_size under the extent lock so buffered
7959 * readers will get the updated i_size when we unlock.
7961 if (!dio_data->overwrite && start + len > i_size_read(inode))
7962 i_size_write(inode, start + len);
7964 WARN_ON(dio_data->reserve < len);
7965 dio_data->reserve -= len;
7966 dio_data->unsubmitted_oe_range_end = start + len;
7967 current->journal_info = dio_data;
7972 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7973 struct buffer_head *bh_result, int create)
7975 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7976 struct extent_map *em;
7977 struct extent_state *cached_state = NULL;
7978 struct btrfs_dio_data *dio_data = NULL;
7979 u64 start = iblock << inode->i_blkbits;
7980 u64 lockstart, lockend;
7981 u64 len = bh_result->b_size;
7985 len = min_t(u64, len, fs_info->sectorsize);
7988 lockend = start + len - 1;
7990 if (current->journal_info) {
7992 * Need to pull our outstanding extents and set journal_info to NULL so
7993 * that anything that needs to check if there's a transaction doesn't get
7996 dio_data = current->journal_info;
7997 current->journal_info = NULL;
8001 * If this errors out it's because we couldn't invalidate pagecache for
8002 * this range and we need to fallback to buffered.
8004 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
8010 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
8017 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
8018 * io. INLINE is special, and we could probably kludge it in here, but
8019 * it's still buffered so for safety lets just fall back to the generic
8022 * For COMPRESSED we _have_ to read the entire extent in so we can
8023 * decompress it, so there will be buffering required no matter what we
8024 * do, so go ahead and fallback to buffered.
8026 * We return -ENOTBLK because that's what makes DIO go ahead and go back
8027 * to buffered IO. Don't blame me, this is the price we pay for using
8030 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
8031 em->block_start == EXTENT_MAP_INLINE) {
8032 free_extent_map(em);
8038 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
8039 dio_data, start, len);
8043 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
8044 lockend, &cached_state);
8046 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
8048 /* Can be negative only if we read from a hole */
8051 free_extent_map(em);
8055 * We need to unlock only the end area that we aren't using.
8056 * The rest is going to be unlocked by the endio routine.
8058 lockstart = start + bh_result->b_size;
8059 if (lockstart < lockend) {
8060 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
8061 lockstart, lockend, &cached_state);
8063 free_extent_state(cached_state);
8067 free_extent_map(em);
8072 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
8076 current->journal_info = dio_data;
8080 static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
8084 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8087 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
8089 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
8093 ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
8098 static int btrfs_check_dio_repairable(struct inode *inode,
8099 struct bio *failed_bio,
8100 struct io_failure_record *failrec,
8103 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8106 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
8107 if (num_copies == 1) {
8109 * we only have a single copy of the data, so don't bother with
8110 * all the retry and error correction code that follows. no
8111 * matter what the error is, it is very likely to persist.
8113 btrfs_debug(fs_info,
8114 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
8115 num_copies, failrec->this_mirror, failed_mirror);
8119 failrec->failed_mirror = failed_mirror;
8120 failrec->this_mirror++;
8121 if (failrec->this_mirror == failed_mirror)
8122 failrec->this_mirror++;
8124 if (failrec->this_mirror > num_copies) {
8125 btrfs_debug(fs_info,
8126 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
8127 num_copies, failrec->this_mirror, failed_mirror);
8134 static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
8135 struct page *page, unsigned int pgoff,
8136 u64 start, u64 end, int failed_mirror,
8137 bio_end_io_t *repair_endio, void *repair_arg)
8139 struct io_failure_record *failrec;
8140 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8141 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
8144 unsigned int read_mode = 0;
8147 blk_status_t status;
8148 struct bio_vec bvec;
8150 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
8152 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
8154 return errno_to_blk_status(ret);
8156 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
8159 free_io_failure(failure_tree, io_tree, failrec);
8160 return BLK_STS_IOERR;
8163 segs = bio_segments(failed_bio);
8164 bio_get_first_bvec(failed_bio, &bvec);
8166 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
8167 read_mode |= REQ_FAILFAST_DEV;
8169 isector = start - btrfs_io_bio(failed_bio)->logical;
8170 isector >>= inode->i_sb->s_blocksize_bits;
8171 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
8172 pgoff, isector, repair_endio, repair_arg);
8173 bio->bi_opf = REQ_OP_READ | read_mode;
8175 btrfs_debug(BTRFS_I(inode)->root->fs_info,
8176 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
8177 read_mode, failrec->this_mirror, failrec->in_validation);
8179 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
8181 free_io_failure(failure_tree, io_tree, failrec);
8188 struct btrfs_retry_complete {
8189 struct completion done;
8190 struct inode *inode;
8195 static void btrfs_retry_endio_nocsum(struct bio *bio)
8197 struct btrfs_retry_complete *done = bio->bi_private;
8198 struct inode *inode = done->inode;
8199 struct bio_vec *bvec;
8200 struct extent_io_tree *io_tree, *failure_tree;
8201 struct bvec_iter_all iter_all;
8206 ASSERT(bio->bi_vcnt == 1);
8207 io_tree = &BTRFS_I(inode)->io_tree;
8208 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8209 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
8212 ASSERT(!bio_flagged(bio, BIO_CLONED));
8213 bio_for_each_segment_all(bvec, bio, iter_all)
8214 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
8215 io_tree, done->start, bvec->bv_page,
8216 btrfs_ino(BTRFS_I(inode)), 0);
8218 complete(&done->done);
8222 static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
8223 struct btrfs_io_bio *io_bio)
8225 struct btrfs_fs_info *fs_info;
8226 struct bio_vec bvec;
8227 struct bvec_iter iter;
8228 struct btrfs_retry_complete done;
8234 blk_status_t err = BLK_STS_OK;
8236 fs_info = BTRFS_I(inode)->root->fs_info;
8237 sectorsize = fs_info->sectorsize;
8239 start = io_bio->logical;
8241 io_bio->bio.bi_iter = io_bio->iter;
8243 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8244 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8245 pgoff = bvec.bv_offset;
8247 next_block_or_try_again:
8250 init_completion(&done.done);
8252 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8253 pgoff, start, start + sectorsize - 1,
8255 btrfs_retry_endio_nocsum, &done);
8261 wait_for_completion_io(&done.done);
8263 if (!done.uptodate) {
8264 /* We might have another mirror, so try again */
8265 goto next_block_or_try_again;
8269 start += sectorsize;
8273 pgoff += sectorsize;
8274 ASSERT(pgoff < PAGE_SIZE);
8275 goto next_block_or_try_again;
8282 static void btrfs_retry_endio(struct bio *bio)
8284 struct btrfs_retry_complete *done = bio->bi_private;
8285 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8286 struct extent_io_tree *io_tree, *failure_tree;
8287 struct inode *inode = done->inode;
8288 struct bio_vec *bvec;
8292 struct bvec_iter_all iter_all;
8299 ASSERT(bio->bi_vcnt == 1);
8300 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
8302 io_tree = &BTRFS_I(inode)->io_tree;
8303 failure_tree = &BTRFS_I(inode)->io_failure_tree;
8305 ASSERT(!bio_flagged(bio, BIO_CLONED));
8306 bio_for_each_segment_all(bvec, bio, iter_all) {
8307 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
8308 bvec->bv_offset, done->start,
8311 clean_io_failure(BTRFS_I(inode)->root->fs_info,
8312 failure_tree, io_tree, done->start,
8314 btrfs_ino(BTRFS_I(inode)),
8321 done->uptodate = uptodate;
8323 complete(&done->done);
8327 static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
8328 struct btrfs_io_bio *io_bio, blk_status_t err)
8330 struct btrfs_fs_info *fs_info;
8331 struct bio_vec bvec;
8332 struct bvec_iter iter;
8333 struct btrfs_retry_complete done;
8340 bool uptodate = (err == 0);
8342 blk_status_t status;
8344 fs_info = BTRFS_I(inode)->root->fs_info;
8345 sectorsize = fs_info->sectorsize;
8348 start = io_bio->logical;
8350 io_bio->bio.bi_iter = io_bio->iter;
8352 bio_for_each_segment(bvec, &io_bio->bio, iter) {
8353 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8355 pgoff = bvec.bv_offset;
8358 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8359 ret = __readpage_endio_check(inode, io_bio, csum_pos,
8360 bvec.bv_page, pgoff, start, sectorsize);
8367 init_completion(&done.done);
8369 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
8370 pgoff, start, start + sectorsize - 1,
8371 io_bio->mirror_num, btrfs_retry_endio,
8378 wait_for_completion_io(&done.done);
8380 if (!done.uptodate) {
8381 /* We might have another mirror, so try again */
8385 offset += sectorsize;
8386 start += sectorsize;
8392 pgoff += sectorsize;
8393 ASSERT(pgoff < PAGE_SIZE);
8401 static blk_status_t btrfs_subio_endio_read(struct inode *inode,
8402 struct btrfs_io_bio *io_bio, blk_status_t err)
8404 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8408 return __btrfs_correct_data_nocsum(inode, io_bio);
8412 return __btrfs_subio_endio_read(inode, io_bio, err);
8416 static void btrfs_endio_direct_read(struct bio *bio)
8418 struct btrfs_dio_private *dip = bio->bi_private;
8419 struct inode *inode = dip->inode;
8420 struct bio *dio_bio;
8421 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8422 blk_status_t err = bio->bi_status;
8424 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8425 err = btrfs_subio_endio_read(inode, io_bio, err);
8427 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
8428 dip->logical_offset + dip->bytes - 1);
8429 dio_bio = dip->dio_bio;
8433 dio_bio->bi_status = err;
8434 dio_end_io(dio_bio);
8435 btrfs_io_bio_free_csum(io_bio);
8439 static void __endio_write_update_ordered(struct inode *inode,
8440 const u64 offset, const u64 bytes,
8441 const bool uptodate)
8443 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8444 struct btrfs_ordered_extent *ordered = NULL;
8445 struct btrfs_workqueue *wq;
8446 u64 ordered_offset = offset;
8447 u64 ordered_bytes = bytes;
8450 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
8451 wq = fs_info->endio_freespace_worker;
8453 wq = fs_info->endio_write_workers;
8455 while (ordered_offset < offset + bytes) {
8456 last_offset = ordered_offset;
8457 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
8461 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
8463 btrfs_queue_work(wq, &ordered->work);
8466 * If btrfs_dec_test_ordered_pending does not find any ordered
8467 * extent in the range, we can exit.
8469 if (ordered_offset == last_offset)
8472 * Our bio might span multiple ordered extents. In this case
8473 * we keep going until we have accounted the whole dio.
8475 if (ordered_offset < offset + bytes) {
8476 ordered_bytes = offset + bytes - ordered_offset;
8482 static void btrfs_endio_direct_write(struct bio *bio)
8484 struct btrfs_dio_private *dip = bio->bi_private;
8485 struct bio *dio_bio = dip->dio_bio;
8487 __endio_write_update_ordered(dip->inode, dip->logical_offset,
8488 dip->bytes, !bio->bi_status);
8492 dio_bio->bi_status = bio->bi_status;
8493 dio_end_io(dio_bio);
8497 static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
8498 struct bio *bio, u64 offset)
8500 struct inode *inode = private_data;
8502 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
8503 BUG_ON(ret); /* -ENOMEM */
8507 static void btrfs_end_dio_bio(struct bio *bio)
8509 struct btrfs_dio_private *dip = bio->bi_private;
8510 blk_status_t err = bio->bi_status;
8513 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8514 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
8515 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8517 (unsigned long long)bio->bi_iter.bi_sector,
8518 bio->bi_iter.bi_size, err);
8520 if (dip->subio_endio)
8521 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
8525 * We want to perceive the errors flag being set before
8526 * decrementing the reference count. We don't need a barrier
8527 * since atomic operations with a return value are fully
8528 * ordered as per atomic_t.txt
8533 /* if there are more bios still pending for this dio, just exit */
8534 if (!atomic_dec_and_test(&dip->pending_bios))
8538 bio_io_error(dip->orig_bio);
8540 dip->dio_bio->bi_status = BLK_STS_OK;
8541 bio_endio(dip->orig_bio);
8547 static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
8548 struct btrfs_dio_private *dip,
8552 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8553 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8558 * We load all the csum data we need when we submit
8559 * the first bio to reduce the csum tree search and
8562 if (dip->logical_offset == file_offset) {
8563 ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
8569 if (bio == dip->orig_bio)
8572 file_offset -= dip->logical_offset;
8573 file_offset >>= inode->i_sb->s_blocksize_bits;
8574 csum_size = btrfs_super_csum_size(btrfs_sb(inode->i_sb)->super_copy);
8575 io_bio->csum = orig_io_bio->csum + csum_size * file_offset;
8580 static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8581 struct inode *inode, u64 file_offset, int async_submit)
8583 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8584 struct btrfs_dio_private *dip = bio->bi_private;
8585 bool write = bio_op(bio) == REQ_OP_WRITE;
8588 /* Check btrfs_submit_bio_hook() for rules about async submit. */
8590 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8593 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
8598 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
8601 if (write && async_submit) {
8602 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
8604 btrfs_submit_bio_start_direct_io);
8608 * If we aren't doing async submit, calculate the csum of the
8611 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
8615 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
8621 ret = btrfs_map_bio(fs_info, bio, 0, 0);
8627 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8628 * or ordered extents whether or not we submit any bios.
8630 static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8631 struct inode *inode,
8634 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
8635 struct btrfs_dio_private *dip;
8638 dip = kzalloc(sizeof(*dip), GFP_NOFS);
8642 bio = btrfs_bio_clone(dio_bio);
8643 bio->bi_private = dip;
8644 btrfs_io_bio(bio)->logical = file_offset;
8646 dip->private = dio_bio->bi_private;
8648 dip->logical_offset = file_offset;
8649 dip->bytes = dio_bio->bi_iter.bi_size;
8650 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
8651 dip->orig_bio = bio;
8652 dip->dio_bio = dio_bio;
8653 atomic_set(&dip->pending_bios, 1);
8656 struct btrfs_dio_data *dio_data = current->journal_info;
8659 * Setting range start and end to the same value means that
8660 * no cleanup will happen in btrfs_direct_IO
8662 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8664 dio_data->unsubmitted_oe_range_start =
8665 dio_data->unsubmitted_oe_range_end;
8667 bio->bi_end_io = btrfs_endio_direct_write;
8669 bio->bi_end_io = btrfs_endio_direct_read;
8670 dip->subio_endio = btrfs_subio_endio_read;
8675 static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8678 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
8679 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8680 struct btrfs_dio_private *dip;
8682 struct bio *orig_bio;
8684 int async_submit = 0;
8686 int clone_offset = 0;
8689 blk_status_t status;
8690 struct btrfs_io_geometry geom;
8692 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8695 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8696 file_offset + dio_bio->bi_iter.bi_size - 1);
8698 dio_bio->bi_status = BLK_STS_RESOURCE;
8699 dio_end_io(dio_bio);
8703 orig_bio = dip->orig_bio;
8704 start_sector = orig_bio->bi_iter.bi_sector;
8705 submit_len = orig_bio->bi_iter.bi_size;
8706 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8707 start_sector << 9, submit_len, &geom);
8711 if (geom.len >= submit_len) {
8713 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
8717 /* async crcs make it difficult to collect full stripe writes. */
8718 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
8724 ASSERT(geom.len <= INT_MAX);
8726 clone_len = min_t(int, submit_len, geom.len);
8729 * This will never fail as it's passing GPF_NOFS and
8730 * the allocation is backed by btrfs_bioset.
8732 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
8734 bio->bi_private = dip;
8735 bio->bi_end_io = btrfs_end_dio_bio;
8736 btrfs_io_bio(bio)->logical = file_offset;
8738 ASSERT(submit_len >= clone_len);
8739 submit_len -= clone_len;
8740 if (submit_len == 0)
8744 * Increase the count before we submit the bio so we know
8745 * the end IO handler won't happen before we increase the
8746 * count. Otherwise, the dip might get freed before we're
8747 * done setting it up.
8749 atomic_inc(&dip->pending_bios);
8751 status = btrfs_submit_dio_bio(bio, inode, file_offset,
8755 atomic_dec(&dip->pending_bios);
8759 clone_offset += clone_len;
8760 start_sector += clone_len >> 9;
8761 file_offset += clone_len;
8763 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8764 start_sector << 9, submit_len, &geom);
8767 } while (submit_len > 0);
8770 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
8774 if (bio != orig_bio)
8779 * Before atomic variable goto zero, we must make sure dip->errors is
8780 * perceived to be set. This ordering is ensured by the fact that an
8781 * atomic operations with a return value are fully ordered as per
8784 if (atomic_dec_and_test(&dip->pending_bios))
8785 bio_io_error(dip->orig_bio);
8788 static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8789 const struct iov_iter *iter, loff_t offset)
8793 unsigned int blocksize_mask = fs_info->sectorsize - 1;
8794 ssize_t retval = -EINVAL;
8796 if (offset & blocksize_mask)
8799 if (iov_iter_alignment(iter) & blocksize_mask)
8802 /* If this is a write we don't need to check anymore */
8803 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
8806 * Check to make sure we don't have duplicate iov_base's in this
8807 * iovec, if so return EINVAL, otherwise we'll get csum errors
8808 * when reading back.
8810 for (seg = 0; seg < iter->nr_segs; seg++) {
8811 for (i = seg + 1; i < iter->nr_segs; i++) {
8812 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
8821 static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
8823 struct file *file = iocb->ki_filp;
8824 struct inode *inode = file->f_mapping->host;
8825 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8826 struct btrfs_dio_data dio_data = { 0 };
8827 struct extent_changeset *data_reserved = NULL;
8828 loff_t offset = iocb->ki_pos;
8832 bool relock = false;
8835 if (check_direct_IO(fs_info, iter, offset))
8838 inode_dio_begin(inode);
8841 * The generic stuff only does filemap_write_and_wait_range, which
8842 * isn't enough if we've written compressed pages to this area, so
8843 * we need to flush the dirty pages again to make absolutely sure
8844 * that any outstanding dirty pages are on disk.
8846 count = iov_iter_count(iter);
8847 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8848 &BTRFS_I(inode)->runtime_flags))
8849 filemap_fdatawrite_range(inode->i_mapping, offset,
8850 offset + count - 1);
8852 if (iov_iter_rw(iter) == WRITE) {
8854 * If the write DIO is beyond the EOF, we need update
8855 * the isize, but it is protected by i_mutex. So we can
8856 * not unlock the i_mutex at this case.
8858 if (offset + count <= inode->i_size) {
8859 dio_data.overwrite = 1;
8860 inode_unlock(inode);
8863 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8869 * We need to know how many extents we reserved so that we can
8870 * do the accounting properly if we go over the number we
8871 * originally calculated. Abuse current->journal_info for this.
8873 dio_data.reserve = round_up(count,
8874 fs_info->sectorsize);
8875 dio_data.unsubmitted_oe_range_start = (u64)offset;
8876 dio_data.unsubmitted_oe_range_end = (u64)offset;
8877 current->journal_info = &dio_data;
8878 down_read(&BTRFS_I(inode)->dio_sem);
8879 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8880 &BTRFS_I(inode)->runtime_flags)) {
8881 inode_dio_end(inode);
8882 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8886 ret = __blockdev_direct_IO(iocb, inode,
8887 fs_info->fs_devices->latest_bdev,
8888 iter, btrfs_get_blocks_direct, NULL,
8889 btrfs_submit_direct, flags);
8890 if (iov_iter_rw(iter) == WRITE) {
8891 up_read(&BTRFS_I(inode)->dio_sem);
8892 current->journal_info = NULL;
8893 if (ret < 0 && ret != -EIOCBQUEUED) {
8894 if (dio_data.reserve)
8895 btrfs_delalloc_release_space(inode, data_reserved,
8896 offset, dio_data.reserve, true);
8898 * On error we might have left some ordered extents
8899 * without submitting corresponding bios for them, so
8900 * cleanup them up to avoid other tasks getting them
8901 * and waiting for them to complete forever.
8903 if (dio_data.unsubmitted_oe_range_start <
8904 dio_data.unsubmitted_oe_range_end)
8905 __endio_write_update_ordered(inode,
8906 dio_data.unsubmitted_oe_range_start,
8907 dio_data.unsubmitted_oe_range_end -
8908 dio_data.unsubmitted_oe_range_start,
8910 } else if (ret >= 0 && (size_t)ret < count)
8911 btrfs_delalloc_release_space(inode, data_reserved,
8912 offset, count - (size_t)ret, true);
8913 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
8917 inode_dio_end(inode);
8921 extent_changeset_free(data_reserved);
8925 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8927 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8928 __u64 start, __u64 len)
8932 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8936 return extent_fiemap(inode, fieinfo, start, len);
8939 int btrfs_readpage(struct file *file, struct page *page)
8941 struct extent_io_tree *tree;
8942 tree = &BTRFS_I(page->mapping->host)->io_tree;
8943 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
8946 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8948 struct inode *inode = page->mapping->host;
8951 if (current->flags & PF_MEMALLOC) {
8952 redirty_page_for_writepage(wbc, page);
8958 * If we are under memory pressure we will call this directly from the
8959 * VM, we need to make sure we have the inode referenced for the ordered
8960 * extent. If not just return like we didn't do anything.
8962 if (!igrab(inode)) {
8963 redirty_page_for_writepage(wbc, page);
8964 return AOP_WRITEPAGE_ACTIVATE;
8966 ret = extent_write_full_page(page, wbc);
8967 btrfs_add_delayed_iput(inode);
8971 static int btrfs_writepages(struct address_space *mapping,
8972 struct writeback_control *wbc)
8974 return extent_writepages(mapping, wbc);
8978 btrfs_readpages(struct file *file, struct address_space *mapping,
8979 struct list_head *pages, unsigned nr_pages)
8981 return extent_readpages(mapping, pages, nr_pages);
8984 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8986 int ret = try_release_extent_mapping(page, gfp_flags);
8988 ClearPagePrivate(page);
8989 set_page_private(page, 0);
8995 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8997 if (PageWriteback(page) || PageDirty(page))
8999 return __btrfs_releasepage(page, gfp_flags);
9002 static void btrfs_invalidatepage(struct page *page, unsigned int offset,
9003 unsigned int length)
9005 struct inode *inode = page->mapping->host;
9006 struct extent_io_tree *tree;
9007 struct btrfs_ordered_extent *ordered;
9008 struct extent_state *cached_state = NULL;
9009 u64 page_start = page_offset(page);
9010 u64 page_end = page_start + PAGE_SIZE - 1;
9013 int inode_evicting = inode->i_state & I_FREEING;
9016 * we have the page locked, so new writeback can't start,
9017 * and the dirty bit won't be cleared while we are here.
9019 * Wait for IO on this page so that we can safely clear
9020 * the PagePrivate2 bit and do ordered accounting
9022 wait_on_page_writeback(page);
9024 tree = &BTRFS_I(inode)->io_tree;
9026 btrfs_releasepage(page, GFP_NOFS);
9030 if (!inode_evicting)
9031 lock_extent_bits(tree, page_start, page_end, &cached_state);
9034 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
9035 page_end - start + 1);
9037 end = min(page_end, ordered->file_offset + ordered->len - 1);
9039 * IO on this page will never be started, so we need
9040 * to account for any ordered extents now
9042 if (!inode_evicting)
9043 clear_extent_bit(tree, start, end,
9044 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9045 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
9046 EXTENT_DEFRAG, 1, 0, &cached_state);
9048 * whoever cleared the private bit is responsible
9049 * for the finish_ordered_io
9051 if (TestClearPagePrivate2(page)) {
9052 struct btrfs_ordered_inode_tree *tree;
9055 tree = &BTRFS_I(inode)->ordered_tree;
9057 spin_lock_irq(&tree->lock);
9058 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
9059 new_len = start - ordered->file_offset;
9060 if (new_len < ordered->truncated_len)
9061 ordered->truncated_len = new_len;
9062 spin_unlock_irq(&tree->lock);
9064 if (btrfs_dec_test_ordered_pending(inode, &ordered,
9066 end - start + 1, 1))
9067 btrfs_finish_ordered_io(ordered);
9069 btrfs_put_ordered_extent(ordered);
9070 if (!inode_evicting) {
9071 cached_state = NULL;
9072 lock_extent_bits(tree, start, end,
9077 if (start < page_end)
9082 * Qgroup reserved space handler
9083 * Page here will be either
9084 * 1) Already written to disk or ordered extent already submitted
9085 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
9086 * Qgroup will be handled by its qgroup_record then.
9087 * btrfs_qgroup_free_data() call will do nothing here.
9089 * 2) Not written to disk yet
9090 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
9091 * bit of its io_tree, and free the qgroup reserved data space.
9092 * Since the IO will never happen for this page.
9094 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
9095 if (!inode_evicting) {
9096 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
9097 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
9098 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
9101 __btrfs_releasepage(page, GFP_NOFS);
9104 ClearPageChecked(page);
9105 if (PagePrivate(page)) {
9106 ClearPagePrivate(page);
9107 set_page_private(page, 0);
9113 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
9114 * called from a page fault handler when a page is first dirtied. Hence we must
9115 * be careful to check for EOF conditions here. We set the page up correctly
9116 * for a written page which means we get ENOSPC checking when writing into
9117 * holes and correct delalloc and unwritten extent mapping on filesystems that
9118 * support these features.
9120 * We are not allowed to take the i_mutex here so we have to play games to
9121 * protect against truncate races as the page could now be beyond EOF. Because
9122 * truncate_setsize() writes the inode size before removing pages, once we have
9123 * the page lock we can determine safely if the page is beyond EOF. If it is not
9124 * beyond EOF, then the page is guaranteed safe against truncation until we
9127 vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
9129 struct page *page = vmf->page;
9130 struct inode *inode = file_inode(vmf->vma->vm_file);
9131 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9132 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9133 struct btrfs_ordered_extent *ordered;
9134 struct extent_state *cached_state = NULL;
9135 struct extent_changeset *data_reserved = NULL;
9137 unsigned long zero_start;
9147 reserved_space = PAGE_SIZE;
9149 sb_start_pagefault(inode->i_sb);
9150 page_start = page_offset(page);
9151 page_end = page_start + PAGE_SIZE - 1;
9155 * Reserving delalloc space after obtaining the page lock can lead to
9156 * deadlock. For example, if a dirty page is locked by this function
9157 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9158 * dirty page write out, then the btrfs_writepage() function could
9159 * end up waiting indefinitely to get a lock on the page currently
9160 * being processed by btrfs_page_mkwrite() function.
9162 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
9165 ret2 = file_update_time(vmf->vma->vm_file);
9169 ret = vmf_error(ret2);
9175 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
9178 size = i_size_read(inode);
9180 if ((page->mapping != inode->i_mapping) ||
9181 (page_start >= size)) {
9182 /* page got truncated out from underneath us */
9185 wait_on_page_writeback(page);
9187 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
9188 set_page_extent_mapped(page);
9191 * we can't set the delalloc bits if there are pending ordered
9192 * extents. Drop our locks and wait for them to finish
9194 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
9197 unlock_extent_cached(io_tree, page_start, page_end,
9200 btrfs_start_ordered_extent(inode, ordered, 1);
9201 btrfs_put_ordered_extent(ordered);
9205 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
9206 reserved_space = round_up(size - page_start,
9207 fs_info->sectorsize);
9208 if (reserved_space < PAGE_SIZE) {
9209 end = page_start + reserved_space - 1;
9210 btrfs_delalloc_release_space(inode, data_reserved,
9211 page_start, PAGE_SIZE - reserved_space,
9217 * page_mkwrite gets called when the page is firstly dirtied after it's
9218 * faulted in, but write(2) could also dirty a page and set delalloc
9219 * bits, thus in this case for space account reason, we still need to
9220 * clear any delalloc bits within this page range since we have to
9221 * reserve data&meta space before lock_page() (see above comments).
9223 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
9224 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
9225 EXTENT_DEFRAG, 0, 0, &cached_state);
9227 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
9230 unlock_extent_cached(io_tree, page_start, page_end,
9232 ret = VM_FAULT_SIGBUS;
9237 /* page is wholly or partially inside EOF */
9238 if (page_start + PAGE_SIZE > size)
9239 zero_start = offset_in_page(size);
9241 zero_start = PAGE_SIZE;
9243 if (zero_start != PAGE_SIZE) {
9245 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
9246 flush_dcache_page(page);
9249 ClearPageChecked(page);
9250 set_page_dirty(page);
9251 SetPageUptodate(page);
9253 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
9255 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
9258 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
9259 sb_end_pagefault(inode->i_sb);
9260 extent_changeset_free(data_reserved);
9261 return VM_FAULT_LOCKED;
9267 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
9268 btrfs_delalloc_release_space(inode, data_reserved, page_start,
9269 reserved_space, (ret != 0));
9271 sb_end_pagefault(inode->i_sb);
9272 extent_changeset_free(data_reserved);
9276 static int btrfs_truncate(struct inode *inode, bool skip_writeback)
9278 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9279 struct btrfs_root *root = BTRFS_I(inode)->root;
9280 struct btrfs_block_rsv *rsv;
9282 struct btrfs_trans_handle *trans;
9283 u64 mask = fs_info->sectorsize - 1;
9284 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
9286 if (!skip_writeback) {
9287 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9294 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9295 * things going on here:
9297 * 1) We need to reserve space to update our inode.
9299 * 2) We need to have something to cache all the space that is going to
9300 * be free'd up by the truncate operation, but also have some slack
9301 * space reserved in case it uses space during the truncate (thank you
9302 * very much snapshotting).
9304 * And we need these to be separate. The fact is we can use a lot of
9305 * space doing the truncate, and we have no earthly idea how much space
9306 * we will use, so we need the truncate reservation to be separate so it
9307 * doesn't end up using space reserved for updating the inode. We also
9308 * need to be able to stop the transaction and start a new one, which
9309 * means we need to be able to update the inode several times, and we
9310 * have no idea of knowing how many times that will be, so we can't just
9311 * reserve 1 item for the entirety of the operation, so that has to be
9312 * done separately as well.
9314 * So that leaves us with
9316 * 1) rsv - for the truncate reservation, which we will steal from the
9317 * transaction reservation.
9318 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
9319 * updating the inode.
9321 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
9324 rsv->size = min_size;
9328 * 1 for the truncate slack space
9329 * 1 for updating the inode.
9331 trans = btrfs_start_transaction(root, 2);
9332 if (IS_ERR(trans)) {
9333 ret = PTR_ERR(trans);
9337 /* Migrate the slack space for the truncate to our reserve */
9338 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
9343 * So if we truncate and then write and fsync we normally would just
9344 * write the extents that changed, which is a problem if we need to
9345 * first truncate that entire inode. So set this flag so we write out
9346 * all of the extents in the inode to the sync log so we're completely
9349 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
9350 trans->block_rsv = rsv;
9353 ret = btrfs_truncate_inode_items(trans, root, inode,
9355 BTRFS_EXTENT_DATA_KEY);
9356 trans->block_rsv = &fs_info->trans_block_rsv;
9357 if (ret != -ENOSPC && ret != -EAGAIN)
9360 ret = btrfs_update_inode(trans, root, inode);
9364 btrfs_end_transaction(trans);
9365 btrfs_btree_balance_dirty(fs_info);
9367 trans = btrfs_start_transaction(root, 2);
9368 if (IS_ERR(trans)) {
9369 ret = PTR_ERR(trans);
9374 btrfs_block_rsv_release(fs_info, rsv, -1);
9375 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
9376 rsv, min_size, false);
9377 BUG_ON(ret); /* shouldn't happen */
9378 trans->block_rsv = rsv;
9382 * We can't call btrfs_truncate_block inside a trans handle as we could
9383 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9384 * we've truncated everything except the last little bit, and can do
9385 * btrfs_truncate_block and then update the disk_i_size.
9387 if (ret == NEED_TRUNCATE_BLOCK) {
9388 btrfs_end_transaction(trans);
9389 btrfs_btree_balance_dirty(fs_info);
9391 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
9394 trans = btrfs_start_transaction(root, 1);
9395 if (IS_ERR(trans)) {
9396 ret = PTR_ERR(trans);
9399 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
9405 trans->block_rsv = &fs_info->trans_block_rsv;
9406 ret2 = btrfs_update_inode(trans, root, inode);
9410 ret2 = btrfs_end_transaction(trans);
9413 btrfs_btree_balance_dirty(fs_info);
9416 btrfs_free_block_rsv(fs_info, rsv);
9422 * create a new subvolume directory/inode (helper for the ioctl).
9424 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
9425 struct btrfs_root *new_root,
9426 struct btrfs_root *parent_root,
9429 struct inode *inode;
9433 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9434 new_dirid, new_dirid,
9435 S_IFDIR | (~current_umask() & S_IRWXUGO),
9438 return PTR_ERR(inode);
9439 inode->i_op = &btrfs_dir_inode_operations;
9440 inode->i_fop = &btrfs_dir_file_operations;
9442 set_nlink(inode, 1);
9443 btrfs_i_size_write(BTRFS_I(inode), 0);
9444 unlock_new_inode(inode);
9446 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9448 btrfs_err(new_root->fs_info,
9449 "error inheriting subvolume %llu properties: %d",
9450 new_root->root_key.objectid, err);
9452 err = btrfs_update_inode(trans, new_root, inode);
9458 struct inode *btrfs_alloc_inode(struct super_block *sb)
9460 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
9461 struct btrfs_inode *ei;
9462 struct inode *inode;
9464 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
9471 ei->last_sub_trans = 0;
9472 ei->logged_trans = 0;
9473 ei->delalloc_bytes = 0;
9474 ei->new_delalloc_bytes = 0;
9475 ei->defrag_bytes = 0;
9476 ei->disk_i_size = 0;
9479 ei->index_cnt = (u64)-1;
9481 ei->last_unlink_trans = 0;
9482 ei->last_log_commit = 0;
9484 spin_lock_init(&ei->lock);
9485 ei->outstanding_extents = 0;
9486 if (sb->s_magic != BTRFS_TEST_MAGIC)
9487 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9488 BTRFS_BLOCK_RSV_DELALLOC);
9489 ei->runtime_flags = 0;
9490 ei->prop_compress = BTRFS_COMPRESS_NONE;
9491 ei->defrag_compress = BTRFS_COMPRESS_NONE;
9493 ei->delayed_node = NULL;
9495 ei->i_otime.tv_sec = 0;
9496 ei->i_otime.tv_nsec = 0;
9498 inode = &ei->vfs_inode;
9499 extent_map_tree_init(&ei->extent_tree);
9500 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9501 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9502 IO_TREE_INODE_IO_FAILURE, inode);
9503 ei->io_tree.track_uptodate = true;
9504 ei->io_failure_tree.track_uptodate = true;
9505 atomic_set(&ei->sync_writers, 0);
9506 mutex_init(&ei->log_mutex);
9507 mutex_init(&ei->delalloc_mutex);
9508 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
9509 INIT_LIST_HEAD(&ei->delalloc_inodes);
9510 INIT_LIST_HEAD(&ei->delayed_iput);
9511 RB_CLEAR_NODE(&ei->rb_node);
9512 init_rwsem(&ei->dio_sem);
9517 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9518 void btrfs_test_destroy_inode(struct inode *inode)
9520 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
9521 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9525 void btrfs_free_inode(struct inode *inode)
9527 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9530 void btrfs_destroy_inode(struct inode *inode)
9532 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9533 struct btrfs_ordered_extent *ordered;
9534 struct btrfs_root *root = BTRFS_I(inode)->root;
9536 WARN_ON(!hlist_empty(&inode->i_dentry));
9537 WARN_ON(inode->i_data.nrpages);
9538 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
9539 WARN_ON(BTRFS_I(inode)->block_rsv.size);
9540 WARN_ON(BTRFS_I(inode)->outstanding_extents);
9541 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9542 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
9543 WARN_ON(BTRFS_I(inode)->csum_bytes);
9544 WARN_ON(BTRFS_I(inode)->defrag_bytes);
9547 * This can happen where we create an inode, but somebody else also
9548 * created the same inode and we need to destroy the one we already
9555 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9560 "found ordered extent %llu %llu on inode cleanup",
9561 ordered->file_offset, ordered->len);
9562 btrfs_remove_ordered_extent(inode, ordered);
9563 btrfs_put_ordered_extent(ordered);
9564 btrfs_put_ordered_extent(ordered);
9567 btrfs_qgroup_check_reserved_leak(BTRFS_I(inode));
9568 inode_tree_del(inode);
9569 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
9572 int btrfs_drop_inode(struct inode *inode)
9574 struct btrfs_root *root = BTRFS_I(inode)->root;
9579 /* the snap/subvol tree is on deleting */
9580 if (btrfs_root_refs(&root->root_item) == 0)
9583 return generic_drop_inode(inode);
9586 static void init_once(void *foo)
9588 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9590 inode_init_once(&ei->vfs_inode);
9593 void __cold btrfs_destroy_cachep(void)
9596 * Make sure all delayed rcu free inodes are flushed before we
9600 kmem_cache_destroy(btrfs_inode_cachep);
9601 kmem_cache_destroy(btrfs_trans_handle_cachep);
9602 kmem_cache_destroy(btrfs_path_cachep);
9603 kmem_cache_destroy(btrfs_free_space_cachep);
9604 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
9607 int __init btrfs_init_cachep(void)
9609 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
9610 sizeof(struct btrfs_inode), 0,
9611 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9613 if (!btrfs_inode_cachep)
9616 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
9617 sizeof(struct btrfs_trans_handle), 0,
9618 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
9619 if (!btrfs_trans_handle_cachep)
9622 btrfs_path_cachep = kmem_cache_create("btrfs_path",
9623 sizeof(struct btrfs_path), 0,
9624 SLAB_MEM_SPREAD, NULL);
9625 if (!btrfs_path_cachep)
9628 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
9629 sizeof(struct btrfs_free_space), 0,
9630 SLAB_MEM_SPREAD, NULL);
9631 if (!btrfs_free_space_cachep)
9634 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9635 PAGE_SIZE, PAGE_SIZE,
9636 SLAB_MEM_SPREAD, NULL);
9637 if (!btrfs_free_space_bitmap_cachep)
9642 btrfs_destroy_cachep();
9646 static int btrfs_getattr(const struct path *path, struct kstat *stat,
9647 u32 request_mask, unsigned int flags)
9650 struct inode *inode = d_inode(path->dentry);
9651 u32 blocksize = inode->i_sb->s_blocksize;
9652 u32 bi_flags = BTRFS_I(inode)->flags;
9654 stat->result_mask |= STATX_BTIME;
9655 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9656 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9657 if (bi_flags & BTRFS_INODE_APPEND)
9658 stat->attributes |= STATX_ATTR_APPEND;
9659 if (bi_flags & BTRFS_INODE_COMPRESS)
9660 stat->attributes |= STATX_ATTR_COMPRESSED;
9661 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9662 stat->attributes |= STATX_ATTR_IMMUTABLE;
9663 if (bi_flags & BTRFS_INODE_NODUMP)
9664 stat->attributes |= STATX_ATTR_NODUMP;
9666 stat->attributes_mask |= (STATX_ATTR_APPEND |
9667 STATX_ATTR_COMPRESSED |
9668 STATX_ATTR_IMMUTABLE |
9671 generic_fillattr(inode, stat);
9672 stat->dev = BTRFS_I(inode)->root->anon_dev;
9674 spin_lock(&BTRFS_I(inode)->lock);
9675 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
9676 spin_unlock(&BTRFS_I(inode)->lock);
9677 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
9678 ALIGN(delalloc_bytes, blocksize)) >> 9;
9682 static int btrfs_rename_exchange(struct inode *old_dir,
9683 struct dentry *old_dentry,
9684 struct inode *new_dir,
9685 struct dentry *new_dentry)
9687 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9688 struct btrfs_trans_handle *trans;
9689 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9690 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9691 struct inode *new_inode = new_dentry->d_inode;
9692 struct inode *old_inode = old_dentry->d_inode;
9693 struct timespec64 ctime = current_time(old_inode);
9694 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9695 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
9700 bool root_log_pinned = false;
9701 bool dest_log_pinned = false;
9704 * For non-subvolumes allow exchange only within one subvolume, in the
9705 * same inode namespace. Two subvolumes (represented as directory) can
9706 * be exchanged as they're a logical link and have a fixed inode number.
9709 (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
9710 new_ino != BTRFS_FIRST_FREE_OBJECTID))
9713 /* close the race window with snapshot create/destroy ioctl */
9714 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9715 new_ino == BTRFS_FIRST_FREE_OBJECTID)
9716 down_read(&fs_info->subvol_sem);
9719 * We want to reserve the absolute worst case amount of items. So if
9720 * both inodes are subvols and we need to unlink them then that would
9721 * require 4 item modifications, but if they are both normal inodes it
9722 * would require 5 item modifications, so we'll assume their normal
9723 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9724 * should cover the worst case number of items we'll modify.
9726 trans = btrfs_start_transaction(root, 12);
9727 if (IS_ERR(trans)) {
9728 ret = PTR_ERR(trans);
9733 btrfs_record_root_in_trans(trans, dest);
9736 * We need to find a free sequence number both in the source and
9737 * in the destination directory for the exchange.
9739 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
9742 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
9746 BTRFS_I(old_inode)->dir_index = 0ULL;
9747 BTRFS_I(new_inode)->dir_index = 0ULL;
9749 /* Reference for the source. */
9750 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9751 /* force full log commit if subvolume involved. */
9752 btrfs_set_log_full_commit(trans);
9754 ret = btrfs_insert_inode_ref(trans, dest,
9755 new_dentry->d_name.name,
9756 new_dentry->d_name.len,
9758 btrfs_ino(BTRFS_I(new_dir)),
9764 /* And now for the dest. */
9765 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9766 /* force full log commit if subvolume involved. */
9767 btrfs_set_log_full_commit(trans);
9769 ret = btrfs_insert_inode_ref(trans, root,
9770 old_dentry->d_name.name,
9771 old_dentry->d_name.len,
9773 btrfs_ino(BTRFS_I(old_dir)),
9779 /* Update inode version and ctime/mtime. */
9780 inode_inc_iversion(old_dir);
9781 inode_inc_iversion(new_dir);
9782 inode_inc_iversion(old_inode);
9783 inode_inc_iversion(new_inode);
9784 old_dir->i_ctime = old_dir->i_mtime = ctime;
9785 new_dir->i_ctime = new_dir->i_mtime = ctime;
9786 old_inode->i_ctime = ctime;
9787 new_inode->i_ctime = ctime;
9789 if (old_dentry->d_parent != new_dentry->d_parent) {
9790 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9791 BTRFS_I(old_inode), 1);
9792 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9793 BTRFS_I(new_inode), 1);
9797 * Now pin the logs of the roots. We do it to ensure that no other task
9798 * can sync the logs while we are in progress with the rename, because
9799 * that could result in an inconsistency in case any of the inodes that
9800 * are part of this rename operation were logged before.
9802 * We pin the logs even if at this precise moment none of the inodes was
9803 * logged before. This is because right after we checked for that, some
9804 * other task fsyncing some other inode not involved with this rename
9805 * operation could log that one of our inodes exists.
9807 * We don't need to pin the logs before the above calls to
9808 * btrfs_insert_inode_ref(), since those don't ever need to change a log.
9810 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
9811 btrfs_pin_log_trans(root);
9812 root_log_pinned = true;
9814 if (new_ino != BTRFS_FIRST_FREE_OBJECTID) {
9815 btrfs_pin_log_trans(dest);
9816 dest_log_pinned = true;
9819 /* src is a subvolume */
9820 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9821 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
9822 } else { /* src is an inode */
9823 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9824 BTRFS_I(old_dentry->d_inode),
9825 old_dentry->d_name.name,
9826 old_dentry->d_name.len);
9828 ret = btrfs_update_inode(trans, root, old_inode);
9831 btrfs_abort_transaction(trans, ret);
9835 /* dest is a subvolume */
9836 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9837 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
9838 } else { /* dest is an inode */
9839 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9840 BTRFS_I(new_dentry->d_inode),
9841 new_dentry->d_name.name,
9842 new_dentry->d_name.len);
9844 ret = btrfs_update_inode(trans, dest, new_inode);
9847 btrfs_abort_transaction(trans, ret);
9851 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
9852 new_dentry->d_name.name,
9853 new_dentry->d_name.len, 0, old_idx);
9855 btrfs_abort_transaction(trans, ret);
9859 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
9860 old_dentry->d_name.name,
9861 old_dentry->d_name.len, 0, new_idx);
9863 btrfs_abort_transaction(trans, ret);
9867 if (old_inode->i_nlink == 1)
9868 BTRFS_I(old_inode)->dir_index = old_idx;
9869 if (new_inode->i_nlink == 1)
9870 BTRFS_I(new_inode)->dir_index = new_idx;
9872 if (root_log_pinned) {
9873 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9874 new_dentry->d_parent);
9875 btrfs_end_log_trans(root);
9876 root_log_pinned = false;
9878 if (dest_log_pinned) {
9879 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9880 old_dentry->d_parent);
9881 btrfs_end_log_trans(dest);
9882 dest_log_pinned = false;
9886 * If we have pinned a log and an error happened, we unpin tasks
9887 * trying to sync the log and force them to fallback to a transaction
9888 * commit if the log currently contains any of the inodes involved in
9889 * this rename operation (to ensure we do not persist a log with an
9890 * inconsistent state for any of these inodes or leading to any
9891 * inconsistencies when replayed). If the transaction was aborted, the
9892 * abortion reason is propagated to userspace when attempting to commit
9893 * the transaction. If the log does not contain any of these inodes, we
9894 * allow the tasks to sync it.
9896 if (ret && (root_log_pinned || dest_log_pinned)) {
9897 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9898 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9899 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
9901 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
9902 btrfs_set_log_full_commit(trans);
9904 if (root_log_pinned) {
9905 btrfs_end_log_trans(root);
9906 root_log_pinned = false;
9908 if (dest_log_pinned) {
9909 btrfs_end_log_trans(dest);
9910 dest_log_pinned = false;
9913 ret2 = btrfs_end_transaction(trans);
9914 ret = ret ? ret : ret2;
9916 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9917 old_ino == BTRFS_FIRST_FREE_OBJECTID)
9918 up_read(&fs_info->subvol_sem);
9923 static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9924 struct btrfs_root *root,
9926 struct dentry *dentry)
9929 struct inode *inode;
9933 ret = btrfs_find_free_objectid(root, &objectid);
9937 inode = btrfs_new_inode(trans, root, dir,
9938 dentry->d_name.name,
9940 btrfs_ino(BTRFS_I(dir)),
9942 S_IFCHR | WHITEOUT_MODE,
9945 if (IS_ERR(inode)) {
9946 ret = PTR_ERR(inode);
9950 inode->i_op = &btrfs_special_inode_operations;
9951 init_special_inode(inode, inode->i_mode,
9954 ret = btrfs_init_inode_security(trans, inode, dir,
9959 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9960 BTRFS_I(inode), 0, index);
9964 ret = btrfs_update_inode(trans, root, inode);
9966 unlock_new_inode(inode);
9968 inode_dec_link_count(inode);
9974 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9975 struct inode *new_dir, struct dentry *new_dentry,
9978 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9979 struct btrfs_trans_handle *trans;
9980 unsigned int trans_num_items;
9981 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9982 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9983 struct inode *new_inode = d_inode(new_dentry);
9984 struct inode *old_inode = d_inode(old_dentry);
9988 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9989 bool log_pinned = false;
9991 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
9994 /* we only allow rename subvolume link between subvolumes */
9995 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9998 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9999 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
10002 if (S_ISDIR(old_inode->i_mode) && new_inode &&
10003 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
10007 /* check for collisions, even if the name isn't there */
10008 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
10009 new_dentry->d_name.name,
10010 new_dentry->d_name.len);
10013 if (ret == -EEXIST) {
10014 /* we shouldn't get
10015 * eexist without a new_inode */
10016 if (WARN_ON(!new_inode)) {
10020 /* maybe -EOVERFLOW */
10027 * we're using rename to replace one file with another. Start IO on it
10028 * now so we don't add too much work to the end of the transaction
10030 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
10031 filemap_flush(old_inode->i_mapping);
10033 /* close the racy window with snapshot create/destroy ioctl */
10034 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
10035 down_read(&fs_info->subvol_sem);
10037 * We want to reserve the absolute worst case amount of items. So if
10038 * both inodes are subvols and we need to unlink them then that would
10039 * require 4 item modifications, but if they are both normal inodes it
10040 * would require 5 item modifications, so we'll assume they are normal
10041 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
10042 * should cover the worst case number of items we'll modify.
10043 * If our rename has the whiteout flag, we need more 5 units for the
10044 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
10045 * when selinux is enabled).
10047 trans_num_items = 11;
10048 if (flags & RENAME_WHITEOUT)
10049 trans_num_items += 5;
10050 trans = btrfs_start_transaction(root, trans_num_items);
10051 if (IS_ERR(trans)) {
10052 ret = PTR_ERR(trans);
10057 btrfs_record_root_in_trans(trans, dest);
10059 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
10063 BTRFS_I(old_inode)->dir_index = 0ULL;
10064 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
10065 /* force full log commit if subvolume involved. */
10066 btrfs_set_log_full_commit(trans);
10068 ret = btrfs_insert_inode_ref(trans, dest,
10069 new_dentry->d_name.name,
10070 new_dentry->d_name.len,
10072 btrfs_ino(BTRFS_I(new_dir)), index);
10077 inode_inc_iversion(old_dir);
10078 inode_inc_iversion(new_dir);
10079 inode_inc_iversion(old_inode);
10080 old_dir->i_ctime = old_dir->i_mtime =
10081 new_dir->i_ctime = new_dir->i_mtime =
10082 old_inode->i_ctime = current_time(old_dir);
10084 if (old_dentry->d_parent != new_dentry->d_parent)
10085 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
10086 BTRFS_I(old_inode), 1);
10088 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
10089 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
10092 * Now pin the log. We do it to ensure that no other task can
10093 * sync the log while we are in progress with the rename, as
10094 * that could result in an inconsistency in case any of the
10095 * inodes that are part of this rename operation were logged
10098 * We pin the log even if at this precise moment none of the
10099 * inodes was logged before. This is because right after we
10100 * checked for that, some other task fsyncing some other inode
10101 * not involved with this rename operation could log that one of
10102 * our inodes exists.
10104 * We don't need to pin the logs before the above call to
10105 * btrfs_insert_inode_ref(), since that does not need to change
10108 btrfs_pin_log_trans(root);
10110 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
10111 BTRFS_I(d_inode(old_dentry)),
10112 old_dentry->d_name.name,
10113 old_dentry->d_name.len);
10115 ret = btrfs_update_inode(trans, root, old_inode);
10118 btrfs_abort_transaction(trans, ret);
10123 inode_inc_iversion(new_inode);
10124 new_inode->i_ctime = current_time(new_inode);
10125 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
10126 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
10127 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
10128 BUG_ON(new_inode->i_nlink == 0);
10130 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
10131 BTRFS_I(d_inode(new_dentry)),
10132 new_dentry->d_name.name,
10133 new_dentry->d_name.len);
10135 if (!ret && new_inode->i_nlink == 0)
10136 ret = btrfs_orphan_add(trans,
10137 BTRFS_I(d_inode(new_dentry)));
10139 btrfs_abort_transaction(trans, ret);
10144 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
10145 new_dentry->d_name.name,
10146 new_dentry->d_name.len, 0, index);
10148 btrfs_abort_transaction(trans, ret);
10152 if (old_inode->i_nlink == 1)
10153 BTRFS_I(old_inode)->dir_index = index;
10156 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
10157 new_dentry->d_parent);
10158 btrfs_end_log_trans(root);
10159 log_pinned = false;
10162 if (flags & RENAME_WHITEOUT) {
10163 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
10167 btrfs_abort_transaction(trans, ret);
10173 * If we have pinned the log and an error happened, we unpin tasks
10174 * trying to sync the log and force them to fallback to a transaction
10175 * commit if the log currently contains any of the inodes involved in
10176 * this rename operation (to ensure we do not persist a log with an
10177 * inconsistent state for any of these inodes or leading to any
10178 * inconsistencies when replayed). If the transaction was aborted, the
10179 * abortion reason is propagated to userspace when attempting to commit
10180 * the transaction. If the log does not contain any of these inodes, we
10181 * allow the tasks to sync it.
10183 if (ret && log_pinned) {
10184 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
10185 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
10186 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
10188 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
10189 btrfs_set_log_full_commit(trans);
10191 btrfs_end_log_trans(root);
10192 log_pinned = false;
10194 ret2 = btrfs_end_transaction(trans);
10195 ret = ret ? ret : ret2;
10197 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
10198 up_read(&fs_info->subvol_sem);
10203 static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
10204 struct inode *new_dir, struct dentry *new_dentry,
10205 unsigned int flags)
10207 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
10210 if (flags & RENAME_EXCHANGE)
10211 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
10214 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
10217 struct btrfs_delalloc_work {
10218 struct inode *inode;
10219 struct completion completion;
10220 struct list_head list;
10221 struct btrfs_work work;
10224 static void btrfs_run_delalloc_work(struct btrfs_work *work)
10226 struct btrfs_delalloc_work *delalloc_work;
10227 struct inode *inode;
10229 delalloc_work = container_of(work, struct btrfs_delalloc_work,
10231 inode = delalloc_work->inode;
10232 filemap_flush(inode->i_mapping);
10233 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
10234 &BTRFS_I(inode)->runtime_flags))
10235 filemap_flush(inode->i_mapping);
10238 complete(&delalloc_work->completion);
10241 static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
10243 struct btrfs_delalloc_work *work;
10245 work = kmalloc(sizeof(*work), GFP_NOFS);
10249 init_completion(&work->completion);
10250 INIT_LIST_HEAD(&work->list);
10251 work->inode = inode;
10252 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
10258 * some fairly slow code that needs optimization. This walks the list
10259 * of all the inodes with pending delalloc and forces them to disk.
10261 static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
10263 struct btrfs_inode *binode;
10264 struct inode *inode;
10265 struct btrfs_delalloc_work *work, *next;
10266 struct list_head works;
10267 struct list_head splice;
10270 INIT_LIST_HEAD(&works);
10271 INIT_LIST_HEAD(&splice);
10273 mutex_lock(&root->delalloc_mutex);
10274 spin_lock(&root->delalloc_lock);
10275 list_splice_init(&root->delalloc_inodes, &splice);
10276 while (!list_empty(&splice)) {
10277 binode = list_entry(splice.next, struct btrfs_inode,
10280 list_move_tail(&binode->delalloc_inodes,
10281 &root->delalloc_inodes);
10282 inode = igrab(&binode->vfs_inode);
10284 cond_resched_lock(&root->delalloc_lock);
10287 spin_unlock(&root->delalloc_lock);
10290 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
10291 &binode->runtime_flags);
10292 work = btrfs_alloc_delalloc_work(inode);
10298 list_add_tail(&work->list, &works);
10299 btrfs_queue_work(root->fs_info->flush_workers,
10302 if (nr != -1 && ret >= nr)
10305 spin_lock(&root->delalloc_lock);
10307 spin_unlock(&root->delalloc_lock);
10310 list_for_each_entry_safe(work, next, &works, list) {
10311 list_del_init(&work->list);
10312 wait_for_completion(&work->completion);
10316 if (!list_empty(&splice)) {
10317 spin_lock(&root->delalloc_lock);
10318 list_splice_tail(&splice, &root->delalloc_inodes);
10319 spin_unlock(&root->delalloc_lock);
10321 mutex_unlock(&root->delalloc_mutex);
10325 int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
10327 struct btrfs_fs_info *fs_info = root->fs_info;
10330 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
10333 ret = start_delalloc_inodes(root, -1, true);
10339 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
10341 struct btrfs_root *root;
10342 struct list_head splice;
10345 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
10348 INIT_LIST_HEAD(&splice);
10350 mutex_lock(&fs_info->delalloc_root_mutex);
10351 spin_lock(&fs_info->delalloc_root_lock);
10352 list_splice_init(&fs_info->delalloc_roots, &splice);
10353 while (!list_empty(&splice) && nr) {
10354 root = list_first_entry(&splice, struct btrfs_root,
10356 root = btrfs_grab_fs_root(root);
10358 list_move_tail(&root->delalloc_root,
10359 &fs_info->delalloc_roots);
10360 spin_unlock(&fs_info->delalloc_root_lock);
10362 ret = start_delalloc_inodes(root, nr, false);
10363 btrfs_put_fs_root(root);
10371 spin_lock(&fs_info->delalloc_root_lock);
10373 spin_unlock(&fs_info->delalloc_root_lock);
10377 if (!list_empty(&splice)) {
10378 spin_lock(&fs_info->delalloc_root_lock);
10379 list_splice_tail(&splice, &fs_info->delalloc_roots);
10380 spin_unlock(&fs_info->delalloc_root_lock);
10382 mutex_unlock(&fs_info->delalloc_root_mutex);
10386 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10387 const char *symname)
10389 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10390 struct btrfs_trans_handle *trans;
10391 struct btrfs_root *root = BTRFS_I(dir)->root;
10392 struct btrfs_path *path;
10393 struct btrfs_key key;
10394 struct inode *inode = NULL;
10401 struct btrfs_file_extent_item *ei;
10402 struct extent_buffer *leaf;
10404 name_len = strlen(symname);
10405 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
10406 return -ENAMETOOLONG;
10409 * 2 items for inode item and ref
10410 * 2 items for dir items
10411 * 1 item for updating parent inode item
10412 * 1 item for the inline extent item
10413 * 1 item for xattr if selinux is on
10415 trans = btrfs_start_transaction(root, 7);
10417 return PTR_ERR(trans);
10419 err = btrfs_find_free_objectid(root, &objectid);
10423 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
10424 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10425 objectid, S_IFLNK|S_IRWXUGO, &index);
10426 if (IS_ERR(inode)) {
10427 err = PTR_ERR(inode);
10433 * If the active LSM wants to access the inode during
10434 * d_instantiate it needs these. Smack checks to see
10435 * if the filesystem supports xattrs by looking at the
10438 inode->i_fop = &btrfs_file_operations;
10439 inode->i_op = &btrfs_file_inode_operations;
10440 inode->i_mapping->a_ops = &btrfs_aops;
10441 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10443 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10447 path = btrfs_alloc_path();
10452 key.objectid = btrfs_ino(BTRFS_I(inode));
10454 key.type = BTRFS_EXTENT_DATA_KEY;
10455 datasize = btrfs_file_extent_calc_inline_size(name_len);
10456 err = btrfs_insert_empty_item(trans, root, path, &key,
10459 btrfs_free_path(path);
10462 leaf = path->nodes[0];
10463 ei = btrfs_item_ptr(leaf, path->slots[0],
10464 struct btrfs_file_extent_item);
10465 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10466 btrfs_set_file_extent_type(leaf, ei,
10467 BTRFS_FILE_EXTENT_INLINE);
10468 btrfs_set_file_extent_encryption(leaf, ei, 0);
10469 btrfs_set_file_extent_compression(leaf, ei, 0);
10470 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10471 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10473 ptr = btrfs_file_extent_inline_start(ei);
10474 write_extent_buffer(leaf, symname, ptr, name_len);
10475 btrfs_mark_buffer_dirty(leaf);
10476 btrfs_free_path(path);
10478 inode->i_op = &btrfs_symlink_inode_operations;
10479 inode_nohighmem(inode);
10480 inode_set_bytes(inode, name_len);
10481 btrfs_i_size_write(BTRFS_I(inode), name_len);
10482 err = btrfs_update_inode(trans, root, inode);
10484 * Last step, add directory indexes for our symlink inode. This is the
10485 * last step to avoid extra cleanup of these indexes if an error happens
10489 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10490 BTRFS_I(inode), 0, index);
10494 d_instantiate_new(dentry, inode);
10497 btrfs_end_transaction(trans);
10498 if (err && inode) {
10499 inode_dec_link_count(inode);
10500 discard_new_inode(inode);
10502 btrfs_btree_balance_dirty(fs_info);
10506 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10507 u64 start, u64 num_bytes, u64 min_size,
10508 loff_t actual_len, u64 *alloc_hint,
10509 struct btrfs_trans_handle *trans)
10511 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
10512 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10513 struct extent_map *em;
10514 struct btrfs_root *root = BTRFS_I(inode)->root;
10515 struct btrfs_key ins;
10516 u64 cur_offset = start;
10517 u64 clear_offset = start;
10520 u64 last_alloc = (u64)-1;
10522 bool own_trans = true;
10523 u64 end = start + num_bytes - 1;
10527 while (num_bytes > 0) {
10529 trans = btrfs_start_transaction(root, 3);
10530 if (IS_ERR(trans)) {
10531 ret = PTR_ERR(trans);
10536 cur_bytes = min_t(u64, num_bytes, SZ_256M);
10537 cur_bytes = max(cur_bytes, min_size);
10539 * If we are severely fragmented we could end up with really
10540 * small allocations, so if the allocator is returning small
10541 * chunks lets make its job easier by only searching for those
10544 cur_bytes = min(cur_bytes, last_alloc);
10545 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10546 min_size, 0, *alloc_hint, &ins, 1, 0);
10549 btrfs_end_transaction(trans);
10554 * We've reserved this space, and thus converted it from
10555 * ->bytes_may_use to ->bytes_reserved. Any error that happens
10556 * from here on out we will only need to clear our reservation
10557 * for the remaining unreserved area, so advance our
10558 * clear_offset by our extent size.
10560 clear_offset += ins.offset;
10561 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10563 last_alloc = ins.offset;
10564 ret = insert_reserved_file_extent(trans, inode,
10565 cur_offset, ins.objectid,
10566 ins.offset, ins.offset,
10567 ins.offset, 0, 0, 0,
10568 BTRFS_FILE_EXTENT_PREALLOC);
10570 btrfs_free_reserved_extent(fs_info, ins.objectid,
10572 btrfs_abort_transaction(trans, ret);
10574 btrfs_end_transaction(trans);
10578 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
10579 cur_offset + ins.offset -1, 0);
10581 em = alloc_extent_map();
10583 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10584 &BTRFS_I(inode)->runtime_flags);
10588 em->start = cur_offset;
10589 em->orig_start = cur_offset;
10590 em->len = ins.offset;
10591 em->block_start = ins.objectid;
10592 em->block_len = ins.offset;
10593 em->orig_block_len = ins.offset;
10594 em->ram_bytes = ins.offset;
10595 em->bdev = fs_info->fs_devices->latest_bdev;
10596 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10597 em->generation = trans->transid;
10600 write_lock(&em_tree->lock);
10601 ret = add_extent_mapping(em_tree, em, 1);
10602 write_unlock(&em_tree->lock);
10603 if (ret != -EEXIST)
10605 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
10606 cur_offset + ins.offset - 1,
10609 free_extent_map(em);
10611 num_bytes -= ins.offset;
10612 cur_offset += ins.offset;
10613 *alloc_hint = ins.objectid + ins.offset;
10615 inode_inc_iversion(inode);
10616 inode->i_ctime = current_time(inode);
10617 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
10618 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
10619 (actual_len > inode->i_size) &&
10620 (cur_offset > inode->i_size)) {
10621 if (cur_offset > actual_len)
10622 i_size = actual_len;
10624 i_size = cur_offset;
10625 i_size_write(inode, i_size);
10626 btrfs_ordered_update_i_size(inode, i_size, NULL);
10629 ret = btrfs_update_inode(trans, root, inode);
10632 btrfs_abort_transaction(trans, ret);
10634 btrfs_end_transaction(trans);
10639 btrfs_end_transaction(trans);
10641 if (clear_offset < end)
10642 btrfs_free_reserved_data_space(inode, NULL, clear_offset,
10643 end - clear_offset + 1);
10647 int btrfs_prealloc_file_range(struct inode *inode, int mode,
10648 u64 start, u64 num_bytes, u64 min_size,
10649 loff_t actual_len, u64 *alloc_hint)
10651 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10652 min_size, actual_len, alloc_hint,
10656 int btrfs_prealloc_file_range_trans(struct inode *inode,
10657 struct btrfs_trans_handle *trans, int mode,
10658 u64 start, u64 num_bytes, u64 min_size,
10659 loff_t actual_len, u64 *alloc_hint)
10661 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10662 min_size, actual_len, alloc_hint, trans);
10665 static int btrfs_set_page_dirty(struct page *page)
10667 return __set_page_dirty_nobuffers(page);
10670 static int btrfs_permission(struct inode *inode, int mask)
10672 struct btrfs_root *root = BTRFS_I(inode)->root;
10673 umode_t mode = inode->i_mode;
10675 if (mask & MAY_WRITE &&
10676 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10677 if (btrfs_root_readonly(root))
10679 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10682 return generic_permission(inode, mask);
10685 static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10687 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10688 struct btrfs_trans_handle *trans;
10689 struct btrfs_root *root = BTRFS_I(dir)->root;
10690 struct inode *inode = NULL;
10696 * 5 units required for adding orphan entry
10698 trans = btrfs_start_transaction(root, 5);
10700 return PTR_ERR(trans);
10702 ret = btrfs_find_free_objectid(root, &objectid);
10706 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10707 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
10708 if (IS_ERR(inode)) {
10709 ret = PTR_ERR(inode);
10714 inode->i_fop = &btrfs_file_operations;
10715 inode->i_op = &btrfs_file_inode_operations;
10717 inode->i_mapping->a_ops = &btrfs_aops;
10718 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10720 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10724 ret = btrfs_update_inode(trans, root, inode);
10727 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
10732 * We set number of links to 0 in btrfs_new_inode(), and here we set
10733 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10736 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10738 set_nlink(inode, 1);
10739 d_tmpfile(dentry, inode);
10740 unlock_new_inode(inode);
10741 mark_inode_dirty(inode);
10743 btrfs_end_transaction(trans);
10745 discard_new_inode(inode);
10746 btrfs_btree_balance_dirty(fs_info);
10750 void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
10752 struct inode *inode = tree->private_data;
10753 unsigned long index = start >> PAGE_SHIFT;
10754 unsigned long end_index = end >> PAGE_SHIFT;
10757 while (index <= end_index) {
10758 page = find_get_page(inode->i_mapping, index);
10759 ASSERT(page); /* Pages should be in the extent_io_tree */
10760 set_page_writeback(page);
10768 * Add an entry indicating a block group or device which is pinned by a
10769 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10770 * negative errno on failure.
10772 static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10773 bool is_block_group)
10775 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10776 struct btrfs_swapfile_pin *sp, *entry;
10777 struct rb_node **p;
10778 struct rb_node *parent = NULL;
10780 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10785 sp->is_block_group = is_block_group;
10787 spin_lock(&fs_info->swapfile_pins_lock);
10788 p = &fs_info->swapfile_pins.rb_node;
10791 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10792 if (sp->ptr < entry->ptr ||
10793 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10794 p = &(*p)->rb_left;
10795 } else if (sp->ptr > entry->ptr ||
10796 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10797 p = &(*p)->rb_right;
10799 spin_unlock(&fs_info->swapfile_pins_lock);
10804 rb_link_node(&sp->node, parent, p);
10805 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10806 spin_unlock(&fs_info->swapfile_pins_lock);
10810 /* Free all of the entries pinned by this swapfile. */
10811 static void btrfs_free_swapfile_pins(struct inode *inode)
10813 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10814 struct btrfs_swapfile_pin *sp;
10815 struct rb_node *node, *next;
10817 spin_lock(&fs_info->swapfile_pins_lock);
10818 node = rb_first(&fs_info->swapfile_pins);
10820 next = rb_next(node);
10821 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10822 if (sp->inode == inode) {
10823 rb_erase(&sp->node, &fs_info->swapfile_pins);
10824 if (sp->is_block_group)
10825 btrfs_put_block_group(sp->ptr);
10830 spin_unlock(&fs_info->swapfile_pins_lock);
10833 struct btrfs_swap_info {
10839 unsigned long nr_pages;
10843 static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10844 struct btrfs_swap_info *bsi)
10846 unsigned long nr_pages;
10847 unsigned long max_pages;
10848 u64 first_ppage, first_ppage_reported, next_ppage;
10852 * Our swapfile may have had its size extended after the swap header was
10853 * written. In that case activating the swapfile should not go beyond
10854 * the max size set in the swap header.
10856 if (bsi->nr_pages >= sis->max)
10859 max_pages = sis->max - bsi->nr_pages;
10860 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10861 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10862 PAGE_SIZE) >> PAGE_SHIFT;
10864 if (first_ppage >= next_ppage)
10866 nr_pages = next_ppage - first_ppage;
10867 nr_pages = min(nr_pages, max_pages);
10869 first_ppage_reported = first_ppage;
10870 if (bsi->start == 0)
10871 first_ppage_reported++;
10872 if (bsi->lowest_ppage > first_ppage_reported)
10873 bsi->lowest_ppage = first_ppage_reported;
10874 if (bsi->highest_ppage < (next_ppage - 1))
10875 bsi->highest_ppage = next_ppage - 1;
10877 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10880 bsi->nr_extents += ret;
10881 bsi->nr_pages += nr_pages;
10885 static void btrfs_swap_deactivate(struct file *file)
10887 struct inode *inode = file_inode(file);
10889 btrfs_free_swapfile_pins(inode);
10890 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10893 static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10896 struct inode *inode = file_inode(file);
10897 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10898 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10899 struct extent_state *cached_state = NULL;
10900 struct extent_map *em = NULL;
10901 struct btrfs_device *device = NULL;
10902 struct btrfs_swap_info bsi = {
10903 .lowest_ppage = (sector_t)-1ULL,
10910 * If the swap file was just created, make sure delalloc is done. If the
10911 * file changes again after this, the user is doing something stupid and
10912 * we don't really care.
10914 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10919 * The inode is locked, so these flags won't change after we check them.
10921 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10922 btrfs_warn(fs_info, "swapfile must not be compressed");
10925 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10926 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10929 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10930 btrfs_warn(fs_info, "swapfile must not be checksummed");
10935 * Balance or device remove/replace/resize can move stuff around from
10936 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10937 * concurrently while we are mapping the swap extents, and
10938 * fs_info->swapfile_pins prevents them from running while the swap file
10939 * is active and moving the extents. Note that this also prevents a
10940 * concurrent device add which isn't actually necessary, but it's not
10941 * really worth the trouble to allow it.
10943 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10944 btrfs_warn(fs_info,
10945 "cannot activate swapfile while exclusive operation is running");
10949 * Snapshots can create extents which require COW even if NODATACOW is
10950 * set. We use this counter to prevent snapshots. We must increment it
10951 * before walking the extents because we don't want a concurrent
10952 * snapshot to run after we've already checked the extents.
10954 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10956 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10958 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10960 while (start < isize) {
10961 u64 logical_block_start, physical_block_start;
10962 struct btrfs_block_group_cache *bg;
10963 u64 len = isize - start;
10965 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0);
10971 if (em->block_start == EXTENT_MAP_HOLE) {
10972 btrfs_warn(fs_info, "swapfile must not have holes");
10976 if (em->block_start == EXTENT_MAP_INLINE) {
10978 * It's unlikely we'll ever actually find ourselves
10979 * here, as a file small enough to fit inline won't be
10980 * big enough to store more than the swap header, but in
10981 * case something changes in the future, let's catch it
10982 * here rather than later.
10984 btrfs_warn(fs_info, "swapfile must not be inline");
10988 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10989 btrfs_warn(fs_info, "swapfile must not be compressed");
10994 logical_block_start = em->block_start + (start - em->start);
10995 len = min(len, em->len - (start - em->start));
10996 free_extent_map(em);
10999 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
11005 btrfs_warn(fs_info,
11006 "swapfile must not be copy-on-write");
11011 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
11017 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
11018 btrfs_warn(fs_info,
11019 "swapfile must have single data profile");
11024 if (device == NULL) {
11025 device = em->map_lookup->stripes[0].dev;
11026 ret = btrfs_add_swapfile_pin(inode, device, false);
11031 } else if (device != em->map_lookup->stripes[0].dev) {
11032 btrfs_warn(fs_info, "swapfile must be on one device");
11037 physical_block_start = (em->map_lookup->stripes[0].physical +
11038 (logical_block_start - em->start));
11039 len = min(len, em->len - (logical_block_start - em->start));
11040 free_extent_map(em);
11043 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
11045 btrfs_warn(fs_info,
11046 "could not find block group containing swapfile");
11051 ret = btrfs_add_swapfile_pin(inode, bg, true);
11053 btrfs_put_block_group(bg);
11060 if (bsi.block_len &&
11061 bsi.block_start + bsi.block_len == physical_block_start) {
11062 bsi.block_len += len;
11064 if (bsi.block_len) {
11065 ret = btrfs_add_swap_extent(sis, &bsi);
11070 bsi.block_start = physical_block_start;
11071 bsi.block_len = len;
11078 ret = btrfs_add_swap_extent(sis, &bsi);
11081 if (!IS_ERR_OR_NULL(em))
11082 free_extent_map(em);
11084 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
11087 btrfs_swap_deactivate(file);
11089 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
11095 sis->bdev = device->bdev;
11096 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
11097 sis->max = bsi.nr_pages;
11098 sis->pages = bsi.nr_pages - 1;
11099 sis->highest_bit = bsi.nr_pages - 1;
11100 return bsi.nr_extents;
11103 static void btrfs_swap_deactivate(struct file *file)
11107 static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
11110 return -EOPNOTSUPP;
11114 static const struct inode_operations btrfs_dir_inode_operations = {
11115 .getattr = btrfs_getattr,
11116 .lookup = btrfs_lookup,
11117 .create = btrfs_create,
11118 .unlink = btrfs_unlink,
11119 .link = btrfs_link,
11120 .mkdir = btrfs_mkdir,
11121 .rmdir = btrfs_rmdir,
11122 .rename = btrfs_rename2,
11123 .symlink = btrfs_symlink,
11124 .setattr = btrfs_setattr,
11125 .mknod = btrfs_mknod,
11126 .listxattr = btrfs_listxattr,
11127 .permission = btrfs_permission,
11128 .get_acl = btrfs_get_acl,
11129 .set_acl = btrfs_set_acl,
11130 .update_time = btrfs_update_time,
11131 .tmpfile = btrfs_tmpfile,
11133 static const struct inode_operations btrfs_dir_ro_inode_operations = {
11134 .lookup = btrfs_lookup,
11135 .permission = btrfs_permission,
11136 .update_time = btrfs_update_time,
11139 static const struct file_operations btrfs_dir_file_operations = {
11140 .llseek = generic_file_llseek,
11141 .read = generic_read_dir,
11142 .iterate_shared = btrfs_real_readdir,
11143 .open = btrfs_opendir,
11144 .unlocked_ioctl = btrfs_ioctl,
11145 #ifdef CONFIG_COMPAT
11146 .compat_ioctl = btrfs_compat_ioctl,
11148 .release = btrfs_release_file,
11149 .fsync = btrfs_sync_file,
11152 static const struct extent_io_ops btrfs_extent_io_ops = {
11153 /* mandatory callbacks */
11154 .submit_bio_hook = btrfs_submit_bio_hook,
11155 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
11159 * btrfs doesn't support the bmap operation because swapfiles
11160 * use bmap to make a mapping of extents in the file. They assume
11161 * these extents won't change over the life of the file and they
11162 * use the bmap result to do IO directly to the drive.
11164 * the btrfs bmap call would return logical addresses that aren't
11165 * suitable for IO and they also will change frequently as COW
11166 * operations happen. So, swapfile + btrfs == corruption.
11168 * For now we're avoiding this by dropping bmap.
11170 static const struct address_space_operations btrfs_aops = {
11171 .readpage = btrfs_readpage,
11172 .writepage = btrfs_writepage,
11173 .writepages = btrfs_writepages,
11174 .readpages = btrfs_readpages,
11175 .direct_IO = btrfs_direct_IO,
11176 .invalidatepage = btrfs_invalidatepage,
11177 .releasepage = btrfs_releasepage,
11178 .set_page_dirty = btrfs_set_page_dirty,
11179 .error_remove_page = generic_error_remove_page,
11180 .swap_activate = btrfs_swap_activate,
11181 .swap_deactivate = btrfs_swap_deactivate,
11184 static const struct inode_operations btrfs_file_inode_operations = {
11185 .getattr = btrfs_getattr,
11186 .setattr = btrfs_setattr,
11187 .listxattr = btrfs_listxattr,
11188 .permission = btrfs_permission,
11189 .fiemap = btrfs_fiemap,
11190 .get_acl = btrfs_get_acl,
11191 .set_acl = btrfs_set_acl,
11192 .update_time = btrfs_update_time,
11194 static const struct inode_operations btrfs_special_inode_operations = {
11195 .getattr = btrfs_getattr,
11196 .setattr = btrfs_setattr,
11197 .permission = btrfs_permission,
11198 .listxattr = btrfs_listxattr,
11199 .get_acl = btrfs_get_acl,
11200 .set_acl = btrfs_set_acl,
11201 .update_time = btrfs_update_time,
11203 static const struct inode_operations btrfs_symlink_inode_operations = {
11204 .get_link = page_get_link,
11205 .getattr = btrfs_getattr,
11206 .setattr = btrfs_setattr,
11207 .permission = btrfs_permission,
11208 .listxattr = btrfs_listxattr,
11209 .update_time = btrfs_update_time,
11212 const struct dentry_operations btrfs_dentry_operations = {
11213 .d_delete = btrfs_dentry_delete,