1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <crypto/hash.h>
7 #include <linux/kernel.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/migrate.h>
32 #include <linux/sched/mm.h>
33 #include <linux/iomap.h>
34 #include <asm/unaligned.h>
35 #include <linux/fsverity.h>
39 #include "transaction.h"
40 #include "btrfs_inode.h"
41 #include "print-tree.h"
42 #include "ordered-data.h"
46 #include "compression.h"
48 #include "free-space-cache.h"
51 #include "delalloc-space.h"
52 #include "block-group.h"
53 #include "space-info.h"
57 struct btrfs_iget_args {
59 struct btrfs_root *root;
62 struct btrfs_dio_data {
66 struct extent_changeset *data_reserved;
69 static const struct inode_operations btrfs_dir_inode_operations;
70 static const struct inode_operations btrfs_symlink_inode_operations;
71 static const struct inode_operations btrfs_special_inode_operations;
72 static const struct inode_operations btrfs_file_inode_operations;
73 static const struct address_space_operations btrfs_aops;
74 static const struct file_operations btrfs_dir_file_operations;
76 static struct kmem_cache *btrfs_inode_cachep;
77 struct kmem_cache *btrfs_trans_handle_cachep;
78 struct kmem_cache *btrfs_path_cachep;
79 struct kmem_cache *btrfs_free_space_cachep;
80 struct kmem_cache *btrfs_free_space_bitmap_cachep;
82 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
83 static int btrfs_truncate(struct inode *inode, bool skip_writeback);
84 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
85 static noinline int cow_file_range(struct btrfs_inode *inode,
86 struct page *locked_page,
87 u64 start, u64 end, int *page_started,
88 unsigned long *nr_written, int unlock);
89 static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
90 u64 len, u64 orig_start, u64 block_start,
91 u64 block_len, u64 orig_block_len,
92 u64 ram_bytes, int compress_type,
95 static void __endio_write_update_ordered(struct btrfs_inode *inode,
96 const u64 offset, const u64 bytes,
100 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
102 * ilock_flags can have the following bit set:
104 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
105 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
107 * BTRFS_ILOCK_MMAP - acquire a write lock on the i_mmap_lock
109 int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
111 if (ilock_flags & BTRFS_ILOCK_SHARED) {
112 if (ilock_flags & BTRFS_ILOCK_TRY) {
113 if (!inode_trylock_shared(inode))
118 inode_lock_shared(inode);
120 if (ilock_flags & BTRFS_ILOCK_TRY) {
121 if (!inode_trylock(inode))
128 if (ilock_flags & BTRFS_ILOCK_MMAP)
129 down_write(&BTRFS_I(inode)->i_mmap_lock);
134 * btrfs_inode_unlock - unock inode i_rwsem
136 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
137 * to decide whether the lock acquired is shared or exclusive.
139 void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
141 if (ilock_flags & BTRFS_ILOCK_MMAP)
142 up_write(&BTRFS_I(inode)->i_mmap_lock);
143 if (ilock_flags & BTRFS_ILOCK_SHARED)
144 inode_unlock_shared(inode);
150 * Cleanup all submitted ordered extents in specified range to handle errors
151 * from the btrfs_run_delalloc_range() callback.
153 * NOTE: caller must ensure that when an error happens, it can not call
154 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
155 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
156 * to be released, which we want to happen only when finishing the ordered
157 * extent (btrfs_finish_ordered_io()).
159 static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
160 struct page *locked_page,
161 u64 offset, u64 bytes)
163 unsigned long index = offset >> PAGE_SHIFT;
164 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
165 u64 page_start = page_offset(locked_page);
166 u64 page_end = page_start + PAGE_SIZE - 1;
170 while (index <= end_index) {
172 * For locked page, we will call end_extent_writepage() on it
173 * in run_delalloc_range() for the error handling. That
174 * end_extent_writepage() function will call
175 * btrfs_mark_ordered_io_finished() to clear page Ordered and
176 * run the ordered extent accounting.
178 * Here we can't just clear the Ordered bit, or
179 * btrfs_mark_ordered_io_finished() would skip the accounting
180 * for the page range, and the ordered extent will never finish.
182 if (index == (page_offset(locked_page) >> PAGE_SHIFT)) {
186 page = find_get_page(inode->vfs_inode.i_mapping, index);
192 * Here we just clear all Ordered bits for every page in the
193 * range, then __endio_write_update_ordered() will handle
194 * the ordered extent accounting for the range.
196 btrfs_page_clamp_clear_ordered(inode->root->fs_info, page,
201 /* The locked page covers the full range, nothing needs to be done */
202 if (bytes + offset <= page_offset(locked_page) + PAGE_SIZE)
205 * In case this page belongs to the delalloc range being instantiated
206 * then skip it, since the first page of a range is going to be
207 * properly cleaned up by the caller of run_delalloc_range
209 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
210 bytes = offset + bytes - page_offset(locked_page) - PAGE_SIZE;
211 offset = page_offset(locked_page) + PAGE_SIZE;
214 return __endio_write_update_ordered(inode, offset, bytes, false);
217 static int btrfs_dirty_inode(struct inode *inode);
219 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
220 struct inode *inode, struct inode *dir,
221 const struct qstr *qstr)
225 err = btrfs_init_acl(trans, inode, dir);
227 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
232 * this does all the hard work for inserting an inline extent into
233 * the btree. The caller should have done a btrfs_drop_extents so that
234 * no overlapping inline items exist in the btree
236 static int insert_inline_extent(struct btrfs_trans_handle *trans,
237 struct btrfs_path *path, bool extent_inserted,
238 struct btrfs_root *root, struct inode *inode,
239 u64 start, size_t size, size_t compressed_size,
241 struct page **compressed_pages)
243 struct extent_buffer *leaf;
244 struct page *page = NULL;
247 struct btrfs_file_extent_item *ei;
249 size_t cur_size = size;
250 unsigned long offset;
252 ASSERT((compressed_size > 0 && compressed_pages) ||
253 (compressed_size == 0 && !compressed_pages));
255 if (compressed_size && compressed_pages)
256 cur_size = compressed_size;
258 if (!extent_inserted) {
259 struct btrfs_key key;
262 key.objectid = btrfs_ino(BTRFS_I(inode));
264 key.type = BTRFS_EXTENT_DATA_KEY;
266 datasize = btrfs_file_extent_calc_inline_size(cur_size);
267 ret = btrfs_insert_empty_item(trans, root, path, &key,
272 leaf = path->nodes[0];
273 ei = btrfs_item_ptr(leaf, path->slots[0],
274 struct btrfs_file_extent_item);
275 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
276 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
277 btrfs_set_file_extent_encryption(leaf, ei, 0);
278 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
279 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
280 ptr = btrfs_file_extent_inline_start(ei);
282 if (compress_type != BTRFS_COMPRESS_NONE) {
285 while (compressed_size > 0) {
286 cpage = compressed_pages[i];
287 cur_size = min_t(unsigned long, compressed_size,
290 kaddr = page_address(cpage);
291 write_extent_buffer(leaf, kaddr, ptr, cur_size);
295 compressed_size -= cur_size;
297 btrfs_set_file_extent_compression(leaf, ei,
300 page = find_get_page(inode->i_mapping,
301 start >> PAGE_SHIFT);
302 btrfs_set_file_extent_compression(leaf, ei, 0);
303 kaddr = kmap_atomic(page);
304 offset = offset_in_page(start);
305 write_extent_buffer(leaf, kaddr + offset, ptr, size);
306 kunmap_atomic(kaddr);
309 btrfs_mark_buffer_dirty(leaf);
310 btrfs_release_path(path);
313 * We align size to sectorsize for inline extents just for simplicity
316 size = ALIGN(size, root->fs_info->sectorsize);
317 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
322 * we're an inline extent, so nobody can
323 * extend the file past i_size without locking
324 * a page we already have locked.
326 * We must do any isize and inode updates
327 * before we unlock the pages. Otherwise we
328 * could end up racing with unlink.
330 BTRFS_I(inode)->disk_i_size = inode->i_size;
337 * conditionally insert an inline extent into the file. This
338 * does the checks required to make sure the data is small enough
339 * to fit as an inline extent.
341 static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
342 u64 end, size_t compressed_size,
344 struct page **compressed_pages)
346 struct btrfs_drop_extents_args drop_args = { 0 };
347 struct btrfs_root *root = inode->root;
348 struct btrfs_fs_info *fs_info = root->fs_info;
349 struct btrfs_trans_handle *trans;
350 u64 isize = i_size_read(&inode->vfs_inode);
351 u64 actual_end = min(end + 1, isize);
352 u64 inline_len = actual_end - start;
353 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
354 u64 data_len = inline_len;
356 struct btrfs_path *path;
359 data_len = compressed_size;
362 actual_end > fs_info->sectorsize ||
363 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
365 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
367 data_len > fs_info->max_inline) {
371 path = btrfs_alloc_path();
375 trans = btrfs_join_transaction(root);
377 btrfs_free_path(path);
378 return PTR_ERR(trans);
380 trans->block_rsv = &inode->block_rsv;
382 drop_args.path = path;
383 drop_args.start = start;
384 drop_args.end = aligned_end;
385 drop_args.drop_cache = true;
386 drop_args.replace_extent = true;
388 if (compressed_size && compressed_pages)
389 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
392 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
395 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
397 btrfs_abort_transaction(trans, ret);
401 if (isize > actual_end)
402 inline_len = min_t(u64, isize, actual_end);
403 ret = insert_inline_extent(trans, path, drop_args.extent_inserted,
404 root, &inode->vfs_inode, start,
405 inline_len, compressed_size,
406 compress_type, compressed_pages);
407 if (ret && ret != -ENOSPC) {
408 btrfs_abort_transaction(trans, ret);
410 } else if (ret == -ENOSPC) {
415 btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found);
416 ret = btrfs_update_inode(trans, root, inode);
417 if (ret && ret != -ENOSPC) {
418 btrfs_abort_transaction(trans, ret);
420 } else if (ret == -ENOSPC) {
425 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
428 * Don't forget to free the reserved space, as for inlined extent
429 * it won't count as data extent, free them directly here.
430 * And at reserve time, it's always aligned to page size, so
431 * just free one page here.
433 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
434 btrfs_free_path(path);
435 btrfs_end_transaction(trans);
439 struct async_extent {
444 unsigned long nr_pages;
446 struct list_head list;
451 struct page *locked_page;
454 unsigned int write_flags;
455 struct list_head extents;
456 struct cgroup_subsys_state *blkcg_css;
457 struct btrfs_work work;
462 /* Number of chunks in flight; must be first in the structure */
464 struct async_chunk chunks[];
467 static noinline int add_async_extent(struct async_chunk *cow,
468 u64 start, u64 ram_size,
471 unsigned long nr_pages,
474 struct async_extent *async_extent;
476 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
477 BUG_ON(!async_extent); /* -ENOMEM */
478 async_extent->start = start;
479 async_extent->ram_size = ram_size;
480 async_extent->compressed_size = compressed_size;
481 async_extent->pages = pages;
482 async_extent->nr_pages = nr_pages;
483 async_extent->compress_type = compress_type;
484 list_add_tail(&async_extent->list, &cow->extents);
489 * Check if the inode has flags compatible with compression
491 static inline bool inode_can_compress(struct btrfs_inode *inode)
493 /* Subpage doesn't support compression yet */
494 if (inode->root->fs_info->sectorsize < PAGE_SIZE)
496 if (inode->flags & BTRFS_INODE_NODATACOW ||
497 inode->flags & BTRFS_INODE_NODATASUM)
503 * Check if the inode needs to be submitted to compression, based on mount
504 * options, defragmentation, properties or heuristics.
506 static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
509 struct btrfs_fs_info *fs_info = inode->root->fs_info;
511 if (!inode_can_compress(inode)) {
512 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
513 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
518 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
521 if (inode->defrag_compress)
523 /* bad compression ratios */
524 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
526 if (btrfs_test_opt(fs_info, COMPRESS) ||
527 inode->flags & BTRFS_INODE_COMPRESS ||
528 inode->prop_compress)
529 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
533 static inline void inode_should_defrag(struct btrfs_inode *inode,
534 u64 start, u64 end, u64 num_bytes, u64 small_write)
536 /* If this is a small write inside eof, kick off a defrag */
537 if (num_bytes < small_write &&
538 (start > 0 || end + 1 < inode->disk_i_size))
539 btrfs_add_inode_defrag(NULL, inode);
543 * we create compressed extents in two phases. The first
544 * phase compresses a range of pages that have already been
545 * locked (both pages and state bits are locked).
547 * This is done inside an ordered work queue, and the compression
548 * is spread across many cpus. The actual IO submission is step
549 * two, and the ordered work queue takes care of making sure that
550 * happens in the same order things were put onto the queue by
551 * writepages and friends.
553 * If this code finds it can't get good compression, it puts an
554 * entry onto the work queue to write the uncompressed bytes. This
555 * makes sure that both compressed inodes and uncompressed inodes
556 * are written in the same order that the flusher thread sent them
559 static noinline int compress_file_range(struct async_chunk *async_chunk)
561 struct inode *inode = async_chunk->inode;
562 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
563 u64 blocksize = fs_info->sectorsize;
564 u64 start = async_chunk->start;
565 u64 end = async_chunk->end;
569 struct page **pages = NULL;
570 unsigned long nr_pages;
571 unsigned long total_compressed = 0;
572 unsigned long total_in = 0;
575 int compress_type = fs_info->compress_type;
576 int compressed_extents = 0;
579 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
583 * We need to save i_size before now because it could change in between
584 * us evaluating the size and assigning it. This is because we lock and
585 * unlock the page in truncate and fallocate, and then modify the i_size
588 * The barriers are to emulate READ_ONCE, remove that once i_size_read
592 i_size = i_size_read(inode);
594 actual_end = min_t(u64, i_size, end + 1);
597 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
598 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
599 nr_pages = min_t(unsigned long, nr_pages,
600 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
603 * we don't want to send crud past the end of i_size through
604 * compression, that's just a waste of CPU time. So, if the
605 * end of the file is before the start of our current
606 * requested range of bytes, we bail out to the uncompressed
607 * cleanup code that can deal with all of this.
609 * It isn't really the fastest way to fix things, but this is a
610 * very uncommon corner.
612 if (actual_end <= start)
613 goto cleanup_and_bail_uncompressed;
615 total_compressed = actual_end - start;
618 * skip compression for a small file range(<=blocksize) that
619 * isn't an inline extent, since it doesn't save disk space at all.
621 if (total_compressed <= blocksize &&
622 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
623 goto cleanup_and_bail_uncompressed;
625 total_compressed = min_t(unsigned long, total_compressed,
626 BTRFS_MAX_UNCOMPRESSED);
631 * we do compression for mount -o compress and when the
632 * inode has not been flagged as nocompress. This flag can
633 * change at any time if we discover bad compression ratios.
635 if (inode_need_compress(BTRFS_I(inode), start, end)) {
637 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
639 /* just bail out to the uncompressed code */
644 if (BTRFS_I(inode)->defrag_compress)
645 compress_type = BTRFS_I(inode)->defrag_compress;
646 else if (BTRFS_I(inode)->prop_compress)
647 compress_type = BTRFS_I(inode)->prop_compress;
650 * we need to call clear_page_dirty_for_io on each
651 * page in the range. Otherwise applications with the file
652 * mmap'd can wander in and change the page contents while
653 * we are compressing them.
655 * If the compression fails for any reason, we set the pages
656 * dirty again later on.
658 * Note that the remaining part is redirtied, the start pointer
659 * has moved, the end is the original one.
662 extent_range_clear_dirty_for_io(inode, start, end);
666 /* Compression level is applied here and only here */
667 ret = btrfs_compress_pages(
668 compress_type | (fs_info->compress_level << 4),
669 inode->i_mapping, start,
676 unsigned long offset = offset_in_page(total_compressed);
677 struct page *page = pages[nr_pages - 1];
679 /* zero the tail end of the last page, we might be
680 * sending it down to disk
683 memzero_page(page, offset, PAGE_SIZE - offset);
689 * Check cow_file_range() for why we don't even try to create inline
690 * extent for subpage case.
692 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
693 /* lets try to make an inline extent */
694 if (ret || total_in < actual_end) {
695 /* we didn't compress the entire range, try
696 * to make an uncompressed inline extent.
698 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
699 0, BTRFS_COMPRESS_NONE,
702 /* try making a compressed inline extent */
703 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
705 compress_type, pages);
708 unsigned long clear_flags = EXTENT_DELALLOC |
709 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
710 EXTENT_DO_ACCOUNTING;
711 unsigned long page_error_op;
713 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
716 * inline extent creation worked or returned error,
717 * we don't need to create any more async work items.
718 * Unlock and free up our temp pages.
720 * We use DO_ACCOUNTING here because we need the
721 * delalloc_release_metadata to be done _after_ we drop
722 * our outstanding extent for clearing delalloc for this
725 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
729 PAGE_START_WRITEBACK |
734 * Ensure we only free the compressed pages if we have
735 * them allocated, as we can still reach here with
736 * inode_need_compress() == false.
739 for (i = 0; i < nr_pages; i++) {
740 WARN_ON(pages[i]->mapping);
751 * we aren't doing an inline extent round the compressed size
752 * up to a block size boundary so the allocator does sane
755 total_compressed = ALIGN(total_compressed, blocksize);
758 * one last check to make sure the compression is really a
759 * win, compare the page count read with the blocks on disk,
760 * compression must free at least one sector size
762 total_in = ALIGN(total_in, PAGE_SIZE);
763 if (total_compressed + blocksize <= total_in) {
764 compressed_extents++;
767 * The async work queues will take care of doing actual
768 * allocation on disk for these compressed pages, and
769 * will submit them to the elevator.
771 add_async_extent(async_chunk, start, total_in,
772 total_compressed, pages, nr_pages,
775 if (start + total_in < end) {
781 return compressed_extents;
786 * the compression code ran but failed to make things smaller,
787 * free any pages it allocated and our page pointer array
789 for (i = 0; i < nr_pages; i++) {
790 WARN_ON(pages[i]->mapping);
795 total_compressed = 0;
798 /* flag the file so we don't compress in the future */
799 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
800 !(BTRFS_I(inode)->prop_compress)) {
801 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
804 cleanup_and_bail_uncompressed:
806 * No compression, but we still need to write the pages in the file
807 * we've been given so far. redirty the locked page if it corresponds
808 * to our extent and set things up for the async work queue to run
809 * cow_file_range to do the normal delalloc dance.
811 if (async_chunk->locked_page &&
812 (page_offset(async_chunk->locked_page) >= start &&
813 page_offset(async_chunk->locked_page)) <= end) {
814 __set_page_dirty_nobuffers(async_chunk->locked_page);
815 /* unlocked later on in the async handlers */
819 extent_range_redirty_for_io(inode, start, end);
820 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
821 BTRFS_COMPRESS_NONE);
822 compressed_extents++;
824 return compressed_extents;
827 static void free_async_extent_pages(struct async_extent *async_extent)
831 if (!async_extent->pages)
834 for (i = 0; i < async_extent->nr_pages; i++) {
835 WARN_ON(async_extent->pages[i]->mapping);
836 put_page(async_extent->pages[i]);
838 kfree(async_extent->pages);
839 async_extent->nr_pages = 0;
840 async_extent->pages = NULL;
844 * phase two of compressed writeback. This is the ordered portion
845 * of the code, which only gets called in the order the work was
846 * queued. We walk all the async extents created by compress_file_range
847 * and send them down to the disk.
849 static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
851 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
852 struct btrfs_fs_info *fs_info = inode->root->fs_info;
853 struct async_extent *async_extent;
855 struct btrfs_key ins;
856 struct extent_map *em;
857 struct btrfs_root *root = inode->root;
858 struct extent_io_tree *io_tree = &inode->io_tree;
862 while (!list_empty(&async_chunk->extents)) {
863 async_extent = list_entry(async_chunk->extents.next,
864 struct async_extent, list);
865 list_del(&async_extent->list);
868 lock_extent(io_tree, async_extent->start,
869 async_extent->start + async_extent->ram_size - 1);
870 /* did the compression code fall back to uncompressed IO? */
871 if (!async_extent->pages) {
872 int page_started = 0;
873 unsigned long nr_written = 0;
875 /* allocate blocks */
876 ret = cow_file_range(inode, async_chunk->locked_page,
878 async_extent->start +
879 async_extent->ram_size - 1,
880 &page_started, &nr_written, 0);
885 * if page_started, cow_file_range inserted an
886 * inline extent and took care of all the unlocking
887 * and IO for us. Otherwise, we need to submit
888 * all those pages down to the drive.
890 if (!page_started && !ret)
891 extent_write_locked_range(&inode->vfs_inode,
893 async_extent->start +
894 async_extent->ram_size - 1,
896 else if (ret && async_chunk->locked_page)
897 unlock_page(async_chunk->locked_page);
903 ret = btrfs_reserve_extent(root, async_extent->ram_size,
904 async_extent->compressed_size,
905 async_extent->compressed_size,
906 0, alloc_hint, &ins, 1, 1);
908 free_async_extent_pages(async_extent);
910 if (ret == -ENOSPC) {
911 unlock_extent(io_tree, async_extent->start,
912 async_extent->start +
913 async_extent->ram_size - 1);
916 * we need to redirty the pages if we decide to
917 * fallback to uncompressed IO, otherwise we
918 * will not submit these pages down to lower
921 extent_range_redirty_for_io(&inode->vfs_inode,
923 async_extent->start +
924 async_extent->ram_size - 1);
931 * here we're doing allocation and writeback of the
934 em = create_io_em(inode, async_extent->start,
935 async_extent->ram_size, /* len */
936 async_extent->start, /* orig_start */
937 ins.objectid, /* block_start */
938 ins.offset, /* block_len */
939 ins.offset, /* orig_block_len */
940 async_extent->ram_size, /* ram_bytes */
941 async_extent->compress_type,
942 BTRFS_ORDERED_COMPRESSED);
944 /* ret value is not necessary due to void function */
945 goto out_free_reserve;
948 ret = btrfs_add_ordered_extent_compress(inode,
951 async_extent->ram_size,
953 async_extent->compress_type);
955 btrfs_drop_extent_cache(inode, async_extent->start,
956 async_extent->start +
957 async_extent->ram_size - 1, 0);
958 goto out_free_reserve;
960 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
963 * clear dirty, set writeback and unlock the pages.
965 extent_clear_unlock_delalloc(inode, async_extent->start,
966 async_extent->start +
967 async_extent->ram_size - 1,
968 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
969 PAGE_UNLOCK | PAGE_START_WRITEBACK);
970 if (btrfs_submit_compressed_write(inode, async_extent->start,
971 async_extent->ram_size,
973 ins.offset, async_extent->pages,
974 async_extent->nr_pages,
975 async_chunk->write_flags,
976 async_chunk->blkcg_css)) {
977 struct page *p = async_extent->pages[0];
978 const u64 start = async_extent->start;
979 const u64 end = start + async_extent->ram_size - 1;
981 p->mapping = inode->vfs_inode.i_mapping;
982 btrfs_writepage_endio_finish_ordered(inode, p, start,
986 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
989 free_async_extent_pages(async_extent);
991 alloc_hint = ins.objectid + ins.offset;
997 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
998 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1000 extent_clear_unlock_delalloc(inode, async_extent->start,
1001 async_extent->start +
1002 async_extent->ram_size - 1,
1003 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
1004 EXTENT_DELALLOC_NEW |
1005 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
1006 PAGE_UNLOCK | PAGE_START_WRITEBACK |
1007 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
1008 free_async_extent_pages(async_extent);
1009 kfree(async_extent);
1013 static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
1016 struct extent_map_tree *em_tree = &inode->extent_tree;
1017 struct extent_map *em;
1020 read_lock(&em_tree->lock);
1021 em = search_extent_mapping(em_tree, start, num_bytes);
1024 * if block start isn't an actual block number then find the
1025 * first block in this inode and use that as a hint. If that
1026 * block is also bogus then just don't worry about it.
1028 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1029 free_extent_map(em);
1030 em = search_extent_mapping(em_tree, 0, 0);
1031 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1032 alloc_hint = em->block_start;
1034 free_extent_map(em);
1036 alloc_hint = em->block_start;
1037 free_extent_map(em);
1040 read_unlock(&em_tree->lock);
1046 * when extent_io.c finds a delayed allocation range in the file,
1047 * the call backs end up in this code. The basic idea is to
1048 * allocate extents on disk for the range, and create ordered data structs
1049 * in ram to track those extents.
1051 * locked_page is the page that writepage had locked already. We use
1052 * it to make sure we don't do extra locks or unlocks.
1054 * *page_started is set to one if we unlock locked_page and do everything
1055 * required to start IO on it. It may be clean and already done with
1056 * IO when we return.
1058 static noinline int cow_file_range(struct btrfs_inode *inode,
1059 struct page *locked_page,
1060 u64 start, u64 end, int *page_started,
1061 unsigned long *nr_written, int unlock)
1063 struct btrfs_root *root = inode->root;
1064 struct btrfs_fs_info *fs_info = root->fs_info;
1067 unsigned long ram_size;
1068 u64 cur_alloc_size = 0;
1070 u64 blocksize = fs_info->sectorsize;
1071 struct btrfs_key ins;
1072 struct extent_map *em;
1073 unsigned clear_bits;
1074 unsigned long page_ops;
1075 bool extent_reserved = false;
1078 if (btrfs_is_free_space_inode(inode)) {
1084 num_bytes = ALIGN(end - start + 1, blocksize);
1085 num_bytes = max(blocksize, num_bytes);
1086 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
1088 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
1091 * Due to the page size limit, for subpage we can only trigger the
1092 * writeback for the dirty sectors of page, that means data writeback
1093 * is doing more writeback than what we want.
1095 * This is especially unexpected for some call sites like fallocate,
1096 * where we only increase i_size after everything is done.
1097 * This means we can trigger inline extent even if we didn't want to.
1098 * So here we skip inline extent creation completely.
1100 if (start == 0 && fs_info->sectorsize == PAGE_SIZE) {
1101 /* lets try to make an inline extent */
1102 ret = cow_file_range_inline(inode, start, end, 0,
1103 BTRFS_COMPRESS_NONE, NULL);
1106 * We use DO_ACCOUNTING here because we need the
1107 * delalloc_release_metadata to be run _after_ we drop
1108 * our outstanding extent for clearing delalloc for this
1111 extent_clear_unlock_delalloc(inode, start, end,
1113 EXTENT_LOCKED | EXTENT_DELALLOC |
1114 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1115 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1116 PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
1117 *nr_written = *nr_written +
1118 (end - start + PAGE_SIZE) / PAGE_SIZE;
1121 * locked_page is locked by the caller of
1122 * writepage_delalloc(), not locked by
1123 * __process_pages_contig().
1125 * We can't let __process_pages_contig() to unlock it,
1126 * as it doesn't have any subpage::writers recorded.
1128 * Here we manually unlock the page, since the caller
1129 * can't use page_started to determine if it's an
1130 * inline extent or a compressed extent.
1132 unlock_page(locked_page);
1134 } else if (ret < 0) {
1139 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1140 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
1143 * Relocation relies on the relocated extents to have exactly the same
1144 * size as the original extents. Normally writeback for relocation data
1145 * extents follows a NOCOW path because relocation preallocates the
1146 * extents. However, due to an operation such as scrub turning a block
1147 * group to RO mode, it may fallback to COW mode, so we must make sure
1148 * an extent allocated during COW has exactly the requested size and can
1149 * not be split into smaller extents, otherwise relocation breaks and
1150 * fails during the stage where it updates the bytenr of file extent
1153 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1154 min_alloc_size = num_bytes;
1156 min_alloc_size = fs_info->sectorsize;
1158 while (num_bytes > 0) {
1159 cur_alloc_size = num_bytes;
1160 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
1161 min_alloc_size, 0, alloc_hint,
1165 cur_alloc_size = ins.offset;
1166 extent_reserved = true;
1168 ram_size = ins.offset;
1169 em = create_io_em(inode, start, ins.offset, /* len */
1170 start, /* orig_start */
1171 ins.objectid, /* block_start */
1172 ins.offset, /* block_len */
1173 ins.offset, /* orig_block_len */
1174 ram_size, /* ram_bytes */
1175 BTRFS_COMPRESS_NONE, /* compress_type */
1176 BTRFS_ORDERED_REGULAR /* type */);
1181 free_extent_map(em);
1183 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1184 ram_size, cur_alloc_size,
1185 BTRFS_ORDERED_REGULAR);
1187 goto out_drop_extent_cache;
1189 if (root->root_key.objectid ==
1190 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1191 ret = btrfs_reloc_clone_csums(inode, start,
1194 * Only drop cache here, and process as normal.
1196 * We must not allow extent_clear_unlock_delalloc()
1197 * at out_unlock label to free meta of this ordered
1198 * extent, as its meta should be freed by
1199 * btrfs_finish_ordered_io().
1201 * So we must continue until @start is increased to
1202 * skip current ordered extent.
1205 btrfs_drop_extent_cache(inode, start,
1206 start + ram_size - 1, 0);
1209 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1212 * We're not doing compressed IO, don't unlock the first page
1213 * (which the caller expects to stay locked), don't clear any
1214 * dirty bits and don't set any writeback bits
1216 * Do set the Ordered (Private2) bit so we know this page was
1217 * properly setup for writepage.
1219 page_ops = unlock ? PAGE_UNLOCK : 0;
1220 page_ops |= PAGE_SET_ORDERED;
1222 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
1224 EXTENT_LOCKED | EXTENT_DELALLOC,
1226 if (num_bytes < cur_alloc_size)
1229 num_bytes -= cur_alloc_size;
1230 alloc_hint = ins.objectid + ins.offset;
1231 start += cur_alloc_size;
1232 extent_reserved = false;
1235 * btrfs_reloc_clone_csums() error, since start is increased
1236 * extent_clear_unlock_delalloc() at out_unlock label won't
1237 * free metadata of current ordered extent, we're OK to exit.
1245 out_drop_extent_cache:
1246 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
1248 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1249 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1251 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1252 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
1253 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
1255 * If we reserved an extent for our delalloc range (or a subrange) and
1256 * failed to create the respective ordered extent, then it means that
1257 * when we reserved the extent we decremented the extent's size from
1258 * the data space_info's bytes_may_use counter and incremented the
1259 * space_info's bytes_reserved counter by the same amount. We must make
1260 * sure extent_clear_unlock_delalloc() does not try to decrement again
1261 * the data space_info's bytes_may_use counter, therefore we do not pass
1262 * it the flag EXTENT_CLEAR_DATA_RESV.
1264 if (extent_reserved) {
1265 extent_clear_unlock_delalloc(inode, start,
1266 start + cur_alloc_size - 1,
1270 start += cur_alloc_size;
1274 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1275 clear_bits | EXTENT_CLEAR_DATA_RESV,
1281 * work queue call back to started compression on a file and pages
1283 static noinline void async_cow_start(struct btrfs_work *work)
1285 struct async_chunk *async_chunk;
1286 int compressed_extents;
1288 async_chunk = container_of(work, struct async_chunk, work);
1290 compressed_extents = compress_file_range(async_chunk);
1291 if (compressed_extents == 0) {
1292 btrfs_add_delayed_iput(async_chunk->inode);
1293 async_chunk->inode = NULL;
1298 * work queue call back to submit previously compressed pages
1300 static noinline void async_cow_submit(struct btrfs_work *work)
1302 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1304 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
1305 unsigned long nr_pages;
1307 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
1311 * ->inode could be NULL if async_chunk_start has failed to compress,
1312 * in which case we don't have anything to submit, yet we need to
1313 * always adjust ->async_delalloc_pages as its paired with the init
1314 * happening in cow_file_range_async
1316 if (async_chunk->inode)
1317 submit_compressed_extents(async_chunk);
1319 /* atomic_sub_return implies a barrier */
1320 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1322 cond_wake_up_nomb(&fs_info->async_submit_wait);
1325 static noinline void async_cow_free(struct btrfs_work *work)
1327 struct async_chunk *async_chunk;
1329 async_chunk = container_of(work, struct async_chunk, work);
1330 if (async_chunk->inode)
1331 btrfs_add_delayed_iput(async_chunk->inode);
1332 if (async_chunk->blkcg_css)
1333 css_put(async_chunk->blkcg_css);
1335 * Since the pointer to 'pending' is at the beginning of the array of
1336 * async_chunk's, freeing it ensures the whole array has been freed.
1338 if (atomic_dec_and_test(async_chunk->pending))
1339 kvfree(async_chunk->pending);
1342 static int cow_file_range_async(struct btrfs_inode *inode,
1343 struct writeback_control *wbc,
1344 struct page *locked_page,
1345 u64 start, u64 end, int *page_started,
1346 unsigned long *nr_written)
1348 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1349 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
1350 struct async_cow *ctx;
1351 struct async_chunk *async_chunk;
1352 unsigned long nr_pages;
1354 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1356 bool should_compress;
1358 const unsigned int write_flags = wbc_to_write_flags(wbc);
1360 unlock_extent(&inode->io_tree, start, end);
1362 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
1363 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1365 should_compress = false;
1367 should_compress = true;
1370 nofs_flag = memalloc_nofs_save();
1371 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1372 memalloc_nofs_restore(nofs_flag);
1375 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1376 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1377 EXTENT_DO_ACCOUNTING;
1378 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1379 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
1381 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1382 clear_bits, page_ops);
1386 async_chunk = ctx->chunks;
1387 atomic_set(&ctx->num_chunks, num_chunks);
1389 for (i = 0; i < num_chunks; i++) {
1390 if (should_compress)
1391 cur_end = min(end, start + SZ_512K - 1);
1396 * igrab is called higher up in the call chain, take only the
1397 * lightweight reference for the callback lifetime
1399 ihold(&inode->vfs_inode);
1400 async_chunk[i].pending = &ctx->num_chunks;
1401 async_chunk[i].inode = &inode->vfs_inode;
1402 async_chunk[i].start = start;
1403 async_chunk[i].end = cur_end;
1404 async_chunk[i].write_flags = write_flags;
1405 INIT_LIST_HEAD(&async_chunk[i].extents);
1408 * The locked_page comes all the way from writepage and its
1409 * the original page we were actually given. As we spread
1410 * this large delalloc region across multiple async_chunk
1411 * structs, only the first struct needs a pointer to locked_page
1413 * This way we don't need racey decisions about who is supposed
1418 * Depending on the compressibility, the pages might or
1419 * might not go through async. We want all of them to
1420 * be accounted against wbc once. Let's do it here
1421 * before the paths diverge. wbc accounting is used
1422 * only for foreign writeback detection and doesn't
1423 * need full accuracy. Just account the whole thing
1424 * against the first page.
1426 wbc_account_cgroup_owner(wbc, locked_page,
1428 async_chunk[i].locked_page = locked_page;
1431 async_chunk[i].locked_page = NULL;
1434 if (blkcg_css != blkcg_root_css) {
1436 async_chunk[i].blkcg_css = blkcg_css;
1438 async_chunk[i].blkcg_css = NULL;
1441 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1442 async_cow_submit, async_cow_free);
1444 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
1445 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1447 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
1449 *nr_written += nr_pages;
1450 start = cur_end + 1;
1456 static noinline int run_delalloc_zoned(struct btrfs_inode *inode,
1457 struct page *locked_page, u64 start,
1458 u64 end, int *page_started,
1459 unsigned long *nr_written)
1463 ret = cow_file_range(inode, locked_page, start, end, page_started,
1471 __set_page_dirty_nobuffers(locked_page);
1472 account_page_redirty(locked_page);
1473 extent_write_locked_range(&inode->vfs_inode, start, end, WB_SYNC_ALL);
1479 static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
1480 u64 bytenr, u64 num_bytes)
1483 struct btrfs_ordered_sum *sums;
1486 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
1487 bytenr + num_bytes - 1, &list, 0);
1488 if (ret == 0 && list_empty(&list))
1491 while (!list_empty(&list)) {
1492 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1493 list_del(&sums->list);
1501 static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
1502 const u64 start, const u64 end,
1503 int *page_started, unsigned long *nr_written)
1505 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1506 const bool is_reloc_ino = (inode->root->root_key.objectid ==
1507 BTRFS_DATA_RELOC_TREE_OBJECTID);
1508 const u64 range_bytes = end + 1 - start;
1509 struct extent_io_tree *io_tree = &inode->io_tree;
1510 u64 range_start = start;
1514 * If EXTENT_NORESERVE is set it means that when the buffered write was
1515 * made we had not enough available data space and therefore we did not
1516 * reserve data space for it, since we though we could do NOCOW for the
1517 * respective file range (either there is prealloc extent or the inode
1518 * has the NOCOW bit set).
1520 * However when we need to fallback to COW mode (because for example the
1521 * block group for the corresponding extent was turned to RO mode by a
1522 * scrub or relocation) we need to do the following:
1524 * 1) We increment the bytes_may_use counter of the data space info.
1525 * If COW succeeds, it allocates a new data extent and after doing
1526 * that it decrements the space info's bytes_may_use counter and
1527 * increments its bytes_reserved counter by the same amount (we do
1528 * this at btrfs_add_reserved_bytes()). So we need to increment the
1529 * bytes_may_use counter to compensate (when space is reserved at
1530 * buffered write time, the bytes_may_use counter is incremented);
1532 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1533 * that if the COW path fails for any reason, it decrements (through
1534 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1535 * data space info, which we incremented in the step above.
1537 * If we need to fallback to cow and the inode corresponds to a free
1538 * space cache inode or an inode of the data relocation tree, we must
1539 * also increment bytes_may_use of the data space_info for the same
1540 * reason. Space caches and relocated data extents always get a prealloc
1541 * extent for them, however scrub or balance may have set the block
1542 * group that contains that extent to RO mode and therefore force COW
1543 * when starting writeback.
1545 count = count_range_bits(io_tree, &range_start, end, range_bytes,
1546 EXTENT_NORESERVE, 0);
1547 if (count > 0 || is_space_ino || is_reloc_ino) {
1549 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1550 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1552 if (is_space_ino || is_reloc_ino)
1553 bytes = range_bytes;
1555 spin_lock(&sinfo->lock);
1556 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
1557 spin_unlock(&sinfo->lock);
1560 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1564 return cow_file_range(inode, locked_page, start, end, page_started,
1569 * when nowcow writeback call back. This checks for snapshots or COW copies
1570 * of the extents that exist in the file, and COWs the file as required.
1572 * If no cow copies or snapshots exist, we write directly to the existing
1575 static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
1576 struct page *locked_page,
1577 const u64 start, const u64 end,
1579 unsigned long *nr_written)
1581 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1582 struct btrfs_root *root = inode->root;
1583 struct btrfs_path *path;
1584 u64 cow_start = (u64)-1;
1585 u64 cur_offset = start;
1587 bool check_prev = true;
1588 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1589 u64 ino = btrfs_ino(inode);
1591 u64 disk_bytenr = 0;
1592 const bool force = inode->flags & BTRFS_INODE_NODATACOW;
1594 path = btrfs_alloc_path();
1596 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1597 EXTENT_LOCKED | EXTENT_DELALLOC |
1598 EXTENT_DO_ACCOUNTING |
1599 EXTENT_DEFRAG, PAGE_UNLOCK |
1600 PAGE_START_WRITEBACK |
1601 PAGE_END_WRITEBACK);
1606 struct btrfs_key found_key;
1607 struct btrfs_file_extent_item *fi;
1608 struct extent_buffer *leaf;
1618 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
1624 * If there is no extent for our range when doing the initial
1625 * search, then go back to the previous slot as it will be the
1626 * one containing the search offset
1628 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1629 leaf = path->nodes[0];
1630 btrfs_item_key_to_cpu(leaf, &found_key,
1631 path->slots[0] - 1);
1632 if (found_key.objectid == ino &&
1633 found_key.type == BTRFS_EXTENT_DATA_KEY)
1638 /* Go to next leaf if we have exhausted the current one */
1639 leaf = path->nodes[0];
1640 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1641 ret = btrfs_next_leaf(root, path);
1643 if (cow_start != (u64)-1)
1644 cur_offset = cow_start;
1649 leaf = path->nodes[0];
1652 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1654 /* Didn't find anything for our INO */
1655 if (found_key.objectid > ino)
1658 * Keep searching until we find an EXTENT_ITEM or there are no
1659 * more extents for this inode
1661 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1662 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1667 /* Found key is not EXTENT_DATA_KEY or starts after req range */
1668 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
1669 found_key.offset > end)
1673 * If the found extent starts after requested offset, then
1674 * adjust extent_end to be right before this extent begins
1676 if (found_key.offset > cur_offset) {
1677 extent_end = found_key.offset;
1683 * Found extent which begins before our range and potentially
1686 fi = btrfs_item_ptr(leaf, path->slots[0],
1687 struct btrfs_file_extent_item);
1688 extent_type = btrfs_file_extent_type(leaf, fi);
1690 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1691 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1692 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1693 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1694 extent_offset = btrfs_file_extent_offset(leaf, fi);
1695 extent_end = found_key.offset +
1696 btrfs_file_extent_num_bytes(leaf, fi);
1698 btrfs_file_extent_disk_num_bytes(leaf, fi);
1700 * If the extent we got ends before our current offset,
1701 * skip to the next extent.
1703 if (extent_end <= cur_offset) {
1708 if (disk_bytenr == 0)
1710 /* Skip compressed/encrypted/encoded extents */
1711 if (btrfs_file_extent_compression(leaf, fi) ||
1712 btrfs_file_extent_encryption(leaf, fi) ||
1713 btrfs_file_extent_other_encoding(leaf, fi))
1716 * If extent is created before the last volume's snapshot
1717 * this implies the extent is shared, hence we can't do
1718 * nocow. This is the same check as in
1719 * btrfs_cross_ref_exist but without calling
1720 * btrfs_search_slot.
1722 if (!freespace_inode &&
1723 btrfs_file_extent_generation(leaf, fi) <=
1724 btrfs_root_last_snapshot(&root->root_item))
1726 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1730 * The following checks can be expensive, as they need to
1731 * take other locks and do btree or rbtree searches, so
1732 * release the path to avoid blocking other tasks for too
1735 btrfs_release_path(path);
1737 ret = btrfs_cross_ref_exist(root, ino,
1739 extent_offset, disk_bytenr, false);
1742 * ret could be -EIO if the above fails to read
1746 if (cow_start != (u64)-1)
1747 cur_offset = cow_start;
1751 WARN_ON_ONCE(freespace_inode);
1754 disk_bytenr += extent_offset;
1755 disk_bytenr += cur_offset - found_key.offset;
1756 num_bytes = min(end + 1, extent_end) - cur_offset;
1758 * If there are pending snapshots for this root, we
1759 * fall into common COW way
1761 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
1764 * force cow if csum exists in the range.
1765 * this ensure that csum for a given extent are
1766 * either valid or do not exist.
1768 ret = csum_exist_in_range(fs_info, disk_bytenr,
1772 * ret could be -EIO if the above fails to read
1776 if (cow_start != (u64)-1)
1777 cur_offset = cow_start;
1780 WARN_ON_ONCE(freespace_inode);
1783 /* If the extent's block group is RO, we must COW */
1784 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
1787 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1788 extent_end = found_key.offset + ram_bytes;
1789 extent_end = ALIGN(extent_end, fs_info->sectorsize);
1790 /* Skip extents outside of our requested range */
1791 if (extent_end <= start) {
1796 /* If this triggers then we have a memory corruption */
1801 * If nocow is false then record the beginning of the range
1802 * that needs to be COWed
1805 if (cow_start == (u64)-1)
1806 cow_start = cur_offset;
1807 cur_offset = extent_end;
1808 if (cur_offset > end)
1810 if (!path->nodes[0])
1817 * COW range from cow_start to found_key.offset - 1. As the key
1818 * will contain the beginning of the first extent that can be
1819 * NOCOW, following one which needs to be COW'ed
1821 if (cow_start != (u64)-1) {
1822 ret = fallback_to_cow(inode, locked_page,
1823 cow_start, found_key.offset - 1,
1824 page_started, nr_written);
1827 cow_start = (u64)-1;
1830 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1831 u64 orig_start = found_key.offset - extent_offset;
1832 struct extent_map *em;
1834 em = create_io_em(inode, cur_offset, num_bytes,
1836 disk_bytenr, /* block_start */
1837 num_bytes, /* block_len */
1838 disk_num_bytes, /* orig_block_len */
1839 ram_bytes, BTRFS_COMPRESS_NONE,
1840 BTRFS_ORDERED_PREALLOC);
1845 free_extent_map(em);
1846 ret = btrfs_add_ordered_extent(inode, cur_offset,
1847 disk_bytenr, num_bytes,
1849 BTRFS_ORDERED_PREALLOC);
1851 btrfs_drop_extent_cache(inode, cur_offset,
1852 cur_offset + num_bytes - 1,
1857 ret = btrfs_add_ordered_extent(inode, cur_offset,
1858 disk_bytenr, num_bytes,
1860 BTRFS_ORDERED_NOCOW);
1866 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1869 if (root->root_key.objectid ==
1870 BTRFS_DATA_RELOC_TREE_OBJECTID)
1872 * Error handled later, as we must prevent
1873 * extent_clear_unlock_delalloc() in error handler
1874 * from freeing metadata of created ordered extent.
1876 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1879 extent_clear_unlock_delalloc(inode, cur_offset,
1880 cur_offset + num_bytes - 1,
1881 locked_page, EXTENT_LOCKED |
1883 EXTENT_CLEAR_DATA_RESV,
1884 PAGE_UNLOCK | PAGE_SET_ORDERED);
1886 cur_offset = extent_end;
1889 * btrfs_reloc_clone_csums() error, now we're OK to call error
1890 * handler, as metadata for created ordered extent will only
1891 * be freed by btrfs_finish_ordered_io().
1895 if (cur_offset > end)
1898 btrfs_release_path(path);
1900 if (cur_offset <= end && cow_start == (u64)-1)
1901 cow_start = cur_offset;
1903 if (cow_start != (u64)-1) {
1905 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1906 page_started, nr_written);
1913 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1915 if (ret && cur_offset < end)
1916 extent_clear_unlock_delalloc(inode, cur_offset, end,
1917 locked_page, EXTENT_LOCKED |
1918 EXTENT_DELALLOC | EXTENT_DEFRAG |
1919 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1920 PAGE_START_WRITEBACK |
1921 PAGE_END_WRITEBACK);
1922 btrfs_free_path(path);
1926 static bool should_nocow(struct btrfs_inode *inode, u64 start, u64 end)
1928 if (inode->flags & (BTRFS_INODE_NODATACOW | BTRFS_INODE_PREALLOC)) {
1929 if (inode->defrag_bytes &&
1930 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG,
1939 * Function to process delayed allocation (create CoW) for ranges which are
1940 * being touched for the first time.
1942 int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
1943 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1944 struct writeback_control *wbc)
1947 const bool zoned = btrfs_is_zoned(inode->root->fs_info);
1949 if (should_nocow(inode, start, end)) {
1951 ret = run_delalloc_nocow(inode, locked_page, start, end,
1952 page_started, nr_written);
1953 } else if (!inode_can_compress(inode) ||
1954 !inode_need_compress(inode, start, end)) {
1956 ret = run_delalloc_zoned(inode, locked_page, start, end,
1957 page_started, nr_written);
1959 ret = cow_file_range(inode, locked_page, start, end,
1960 page_started, nr_written, 1);
1962 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1963 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
1964 page_started, nr_written);
1968 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1973 void btrfs_split_delalloc_extent(struct inode *inode,
1974 struct extent_state *orig, u64 split)
1978 /* not delalloc, ignore it */
1979 if (!(orig->state & EXTENT_DELALLOC))
1982 size = orig->end - orig->start + 1;
1983 if (size > BTRFS_MAX_EXTENT_SIZE) {
1988 * See the explanation in btrfs_merge_delalloc_extent, the same
1989 * applies here, just in reverse.
1991 new_size = orig->end - split + 1;
1992 num_extents = count_max_extents(new_size);
1993 new_size = split - orig->start;
1994 num_extents += count_max_extents(new_size);
1995 if (count_max_extents(size) >= num_extents)
1999 spin_lock(&BTRFS_I(inode)->lock);
2000 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
2001 spin_unlock(&BTRFS_I(inode)->lock);
2005 * Handle merged delayed allocation extents so we can keep track of new extents
2006 * that are just merged onto old extents, such as when we are doing sequential
2007 * writes, so we can properly account for the metadata space we'll need.
2009 void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
2010 struct extent_state *other)
2012 u64 new_size, old_size;
2015 /* not delalloc, ignore it */
2016 if (!(other->state & EXTENT_DELALLOC))
2019 if (new->start > other->start)
2020 new_size = new->end - other->start + 1;
2022 new_size = other->end - new->start + 1;
2024 /* we're not bigger than the max, unreserve the space and go */
2025 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
2026 spin_lock(&BTRFS_I(inode)->lock);
2027 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
2028 spin_unlock(&BTRFS_I(inode)->lock);
2033 * We have to add up either side to figure out how many extents were
2034 * accounted for before we merged into one big extent. If the number of
2035 * extents we accounted for is <= the amount we need for the new range
2036 * then we can return, otherwise drop. Think of it like this
2040 * So we've grown the extent by a MAX_SIZE extent, this would mean we
2041 * need 2 outstanding extents, on one side we have 1 and the other side
2042 * we have 1 so they are == and we can return. But in this case
2044 * [MAX_SIZE+4k][MAX_SIZE+4k]
2046 * Each range on their own accounts for 2 extents, but merged together
2047 * they are only 3 extents worth of accounting, so we need to drop in
2050 old_size = other->end - other->start + 1;
2051 num_extents = count_max_extents(old_size);
2052 old_size = new->end - new->start + 1;
2053 num_extents += count_max_extents(old_size);
2054 if (count_max_extents(new_size) >= num_extents)
2057 spin_lock(&BTRFS_I(inode)->lock);
2058 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
2059 spin_unlock(&BTRFS_I(inode)->lock);
2062 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
2063 struct inode *inode)
2065 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2067 spin_lock(&root->delalloc_lock);
2068 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
2069 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2070 &root->delalloc_inodes);
2071 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2072 &BTRFS_I(inode)->runtime_flags);
2073 root->nr_delalloc_inodes++;
2074 if (root->nr_delalloc_inodes == 1) {
2075 spin_lock(&fs_info->delalloc_root_lock);
2076 BUG_ON(!list_empty(&root->delalloc_root));
2077 list_add_tail(&root->delalloc_root,
2078 &fs_info->delalloc_roots);
2079 spin_unlock(&fs_info->delalloc_root_lock);
2082 spin_unlock(&root->delalloc_lock);
2086 void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2087 struct btrfs_inode *inode)
2089 struct btrfs_fs_info *fs_info = root->fs_info;
2091 if (!list_empty(&inode->delalloc_inodes)) {
2092 list_del_init(&inode->delalloc_inodes);
2093 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2094 &inode->runtime_flags);
2095 root->nr_delalloc_inodes--;
2096 if (!root->nr_delalloc_inodes) {
2097 ASSERT(list_empty(&root->delalloc_inodes));
2098 spin_lock(&fs_info->delalloc_root_lock);
2099 BUG_ON(list_empty(&root->delalloc_root));
2100 list_del_init(&root->delalloc_root);
2101 spin_unlock(&fs_info->delalloc_root_lock);
2106 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2107 struct btrfs_inode *inode)
2109 spin_lock(&root->delalloc_lock);
2110 __btrfs_del_delalloc_inode(root, inode);
2111 spin_unlock(&root->delalloc_lock);
2115 * Properly track delayed allocation bytes in the inode and to maintain the
2116 * list of inodes that have pending delalloc work to be done.
2118 void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2121 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2123 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2126 * set_bit and clear bit hooks normally require _irqsave/restore
2127 * but in this case, we are only testing for the DELALLOC
2128 * bit, which is only set or cleared with irqs on
2130 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
2131 struct btrfs_root *root = BTRFS_I(inode)->root;
2132 u64 len = state->end + 1 - state->start;
2133 u32 num_extents = count_max_extents(len);
2134 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
2136 spin_lock(&BTRFS_I(inode)->lock);
2137 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2138 spin_unlock(&BTRFS_I(inode)->lock);
2140 /* For sanity tests */
2141 if (btrfs_is_testing(fs_info))
2144 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2145 fs_info->delalloc_batch);
2146 spin_lock(&BTRFS_I(inode)->lock);
2147 BTRFS_I(inode)->delalloc_bytes += len;
2148 if (*bits & EXTENT_DEFRAG)
2149 BTRFS_I(inode)->defrag_bytes += len;
2150 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2151 &BTRFS_I(inode)->runtime_flags))
2152 btrfs_add_delalloc_inodes(root, inode);
2153 spin_unlock(&BTRFS_I(inode)->lock);
2156 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2157 (*bits & EXTENT_DELALLOC_NEW)) {
2158 spin_lock(&BTRFS_I(inode)->lock);
2159 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2161 spin_unlock(&BTRFS_I(inode)->lock);
2166 * Once a range is no longer delalloc this function ensures that proper
2167 * accounting happens.
2169 void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2170 struct extent_state *state, unsigned *bits)
2172 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2173 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
2174 u64 len = state->end + 1 - state->start;
2175 u32 num_extents = count_max_extents(len);
2177 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2178 spin_lock(&inode->lock);
2179 inode->defrag_bytes -= len;
2180 spin_unlock(&inode->lock);
2184 * set_bit and clear bit hooks normally require _irqsave/restore
2185 * but in this case, we are only testing for the DELALLOC
2186 * bit, which is only set or cleared with irqs on
2188 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
2189 struct btrfs_root *root = inode->root;
2190 bool do_list = !btrfs_is_free_space_inode(inode);
2192 spin_lock(&inode->lock);
2193 btrfs_mod_outstanding_extents(inode, -num_extents);
2194 spin_unlock(&inode->lock);
2197 * We don't reserve metadata space for space cache inodes so we
2198 * don't need to call delalloc_release_metadata if there is an
2201 if (*bits & EXTENT_CLEAR_META_RESV &&
2202 root != fs_info->tree_root)
2203 btrfs_delalloc_release_metadata(inode, len, false);
2205 /* For sanity tests. */
2206 if (btrfs_is_testing(fs_info))
2209 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2210 do_list && !(state->state & EXTENT_NORESERVE) &&
2211 (*bits & EXTENT_CLEAR_DATA_RESV))
2212 btrfs_free_reserved_data_space_noquota(fs_info, len);
2214 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2215 fs_info->delalloc_batch);
2216 spin_lock(&inode->lock);
2217 inode->delalloc_bytes -= len;
2218 if (do_list && inode->delalloc_bytes == 0 &&
2219 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2220 &inode->runtime_flags))
2221 btrfs_del_delalloc_inode(root, inode);
2222 spin_unlock(&inode->lock);
2225 if ((state->state & EXTENT_DELALLOC_NEW) &&
2226 (*bits & EXTENT_DELALLOC_NEW)) {
2227 spin_lock(&inode->lock);
2228 ASSERT(inode->new_delalloc_bytes >= len);
2229 inode->new_delalloc_bytes -= len;
2230 if (*bits & EXTENT_ADD_INODE_BYTES)
2231 inode_add_bytes(&inode->vfs_inode, len);
2232 spin_unlock(&inode->lock);
2237 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2238 * in a chunk's stripe. This function ensures that bios do not span a
2241 * @page - The page we are about to add to the bio
2242 * @size - size we want to add to the bio
2243 * @bio - bio we want to ensure is smaller than a stripe
2244 * @bio_flags - flags of the bio
2246 * return 1 if page cannot be added to the bio
2247 * return 0 if page can be added to the bio
2248 * return error otherwise
2250 int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2251 unsigned long bio_flags)
2253 struct inode *inode = page->mapping->host;
2254 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2255 u64 logical = bio->bi_iter.bi_sector << 9;
2256 u32 bio_len = bio->bi_iter.bi_size;
2257 struct extent_map *em;
2259 struct btrfs_io_geometry geom;
2261 if (bio_flags & EXTENT_BIO_COMPRESSED)
2264 em = btrfs_get_chunk_map(fs_info, logical, fs_info->sectorsize);
2267 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical, &geom);
2271 if (geom.len < bio_len + size)
2274 free_extent_map(em);
2279 * in order to insert checksums into the metadata in large chunks,
2280 * we wait until bio submission time. All the pages in the bio are
2281 * checksummed and sums are attached onto the ordered extent record.
2283 * At IO completion time the cums attached on the ordered extent record
2284 * are inserted into the btree
2286 static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
2287 u64 dio_file_offset)
2289 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
2293 * Split an extent_map at [start, start + len]
2295 * This function is intended to be used only for extract_ordered_extent().
2297 static int split_zoned_em(struct btrfs_inode *inode, u64 start, u64 len,
2300 struct extent_map_tree *em_tree = &inode->extent_tree;
2301 struct extent_map *em;
2302 struct extent_map *split_pre = NULL;
2303 struct extent_map *split_mid = NULL;
2304 struct extent_map *split_post = NULL;
2306 unsigned long flags;
2309 if (pre == 0 && post == 0)
2312 split_pre = alloc_extent_map();
2314 split_mid = alloc_extent_map();
2316 split_post = alloc_extent_map();
2317 if (!split_pre || (pre && !split_mid) || (post && !split_post)) {
2322 ASSERT(pre + post < len);
2324 lock_extent(&inode->io_tree, start, start + len - 1);
2325 write_lock(&em_tree->lock);
2326 em = lookup_extent_mapping(em_tree, start, len);
2332 ASSERT(em->len == len);
2333 ASSERT(!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags));
2334 ASSERT(em->block_start < EXTENT_MAP_LAST_BYTE);
2335 ASSERT(test_bit(EXTENT_FLAG_PINNED, &em->flags));
2336 ASSERT(!test_bit(EXTENT_FLAG_LOGGING, &em->flags));
2337 ASSERT(!list_empty(&em->list));
2340 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
2342 /* First, replace the em with a new extent_map starting from * em->start */
2343 split_pre->start = em->start;
2344 split_pre->len = (pre ? pre : em->len - post);
2345 split_pre->orig_start = split_pre->start;
2346 split_pre->block_start = em->block_start;
2347 split_pre->block_len = split_pre->len;
2348 split_pre->orig_block_len = split_pre->block_len;
2349 split_pre->ram_bytes = split_pre->len;
2350 split_pre->flags = flags;
2351 split_pre->compress_type = em->compress_type;
2352 split_pre->generation = em->generation;
2354 replace_extent_mapping(em_tree, em, split_pre, 1);
2357 * Now we only have an extent_map at:
2358 * [em->start, em->start + pre] if pre != 0
2359 * [em->start, em->start + em->len - post] if pre == 0
2363 /* Insert the middle extent_map */
2364 split_mid->start = em->start + pre;
2365 split_mid->len = em->len - pre - post;
2366 split_mid->orig_start = split_mid->start;
2367 split_mid->block_start = em->block_start + pre;
2368 split_mid->block_len = split_mid->len;
2369 split_mid->orig_block_len = split_mid->block_len;
2370 split_mid->ram_bytes = split_mid->len;
2371 split_mid->flags = flags;
2372 split_mid->compress_type = em->compress_type;
2373 split_mid->generation = em->generation;
2374 add_extent_mapping(em_tree, split_mid, 1);
2378 split_post->start = em->start + em->len - post;
2379 split_post->len = post;
2380 split_post->orig_start = split_post->start;
2381 split_post->block_start = em->block_start + em->len - post;
2382 split_post->block_len = split_post->len;
2383 split_post->orig_block_len = split_post->block_len;
2384 split_post->ram_bytes = split_post->len;
2385 split_post->flags = flags;
2386 split_post->compress_type = em->compress_type;
2387 split_post->generation = em->generation;
2388 add_extent_mapping(em_tree, split_post, 1);
2392 free_extent_map(em);
2393 /* Once for the tree */
2394 free_extent_map(em);
2397 write_unlock(&em_tree->lock);
2398 unlock_extent(&inode->io_tree, start, start + len - 1);
2400 free_extent_map(split_pre);
2401 free_extent_map(split_mid);
2402 free_extent_map(split_post);
2407 static blk_status_t extract_ordered_extent(struct btrfs_inode *inode,
2408 struct bio *bio, loff_t file_offset)
2410 struct btrfs_ordered_extent *ordered;
2411 u64 start = (u64)bio->bi_iter.bi_sector << SECTOR_SHIFT;
2413 u64 len = bio->bi_iter.bi_size;
2414 u64 end = start + len;
2419 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
2420 if (WARN_ON_ONCE(!ordered))
2421 return BLK_STS_IOERR;
2423 /* No need to split */
2424 if (ordered->disk_num_bytes == len)
2427 /* We cannot split once end_bio'd ordered extent */
2428 if (WARN_ON_ONCE(ordered->bytes_left != ordered->disk_num_bytes)) {
2433 /* We cannot split a compressed ordered extent */
2434 if (WARN_ON_ONCE(ordered->disk_num_bytes != ordered->num_bytes)) {
2439 ordered_end = ordered->disk_bytenr + ordered->disk_num_bytes;
2440 /* bio must be in one ordered extent */
2441 if (WARN_ON_ONCE(start < ordered->disk_bytenr || end > ordered_end)) {
2446 /* Checksum list should be empty */
2447 if (WARN_ON_ONCE(!list_empty(&ordered->list))) {
2452 file_len = ordered->num_bytes;
2453 pre = start - ordered->disk_bytenr;
2454 post = ordered_end - end;
2456 ret = btrfs_split_ordered_extent(ordered, pre, post);
2459 ret = split_zoned_em(inode, file_offset, file_len, pre, post);
2462 btrfs_put_ordered_extent(ordered);
2464 return errno_to_blk_status(ret);
2468 * extent_io.c submission hook. This does the right thing for csum calculation
2469 * on write, or reading the csums from the tree before a read.
2471 * Rules about async/sync submit,
2472 * a) read: sync submit
2474 * b) write without checksum: sync submit
2476 * c) write with checksum:
2477 * c-1) if bio is issued by fsync: sync submit
2478 * (sync_writers != 0)
2480 * c-2) if root is reloc root: sync submit
2481 * (only in case of buffered IO)
2483 * c-3) otherwise: async submit
2485 blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2486 int mirror_num, unsigned long bio_flags)
2489 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2490 struct btrfs_root *root = BTRFS_I(inode)->root;
2491 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
2492 blk_status_t ret = 0;
2494 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
2496 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2497 !fs_info->csum_root;
2499 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
2500 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
2502 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
2503 struct page *page = bio_first_bvec_all(bio)->bv_page;
2504 loff_t file_offset = page_offset(page);
2506 ret = extract_ordered_extent(BTRFS_I(inode), bio, file_offset);
2511 if (btrfs_op(bio) != BTRFS_MAP_WRITE) {
2512 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
2516 if (bio_flags & EXTENT_BIO_COMPRESSED) {
2517 ret = btrfs_submit_compressed_read(inode, bio,
2523 * Lookup bio sums does extra checks around whether we
2524 * need to csum or not, which is why we ignore skip_sum
2527 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
2532 } else if (async && !skip_sum) {
2533 /* csum items have already been cloned */
2534 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2536 /* we're doing a write, do the async checksumming */
2537 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2538 0, btrfs_submit_bio_start);
2540 } else if (!skip_sum) {
2541 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
2547 ret = btrfs_map_bio(fs_info, bio, mirror_num);
2551 bio->bi_status = ret;
2558 * given a list of ordered sums record them in the inode. This happens
2559 * at IO completion time based on sums calculated at bio submission time.
2561 static int add_pending_csums(struct btrfs_trans_handle *trans,
2562 struct list_head *list)
2564 struct btrfs_ordered_sum *sum;
2567 list_for_each_entry(sum, list, list) {
2568 trans->adding_csums = true;
2569 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
2570 trans->adding_csums = false;
2577 static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2580 struct extent_state **cached_state)
2582 u64 search_start = start;
2583 const u64 end = start + len - 1;
2585 while (search_start < end) {
2586 const u64 search_len = end - search_start + 1;
2587 struct extent_map *em;
2591 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2595 if (em->block_start != EXTENT_MAP_HOLE)
2599 if (em->start < search_start)
2600 em_len -= search_start - em->start;
2601 if (em_len > search_len)
2602 em_len = search_len;
2604 ret = set_extent_bit(&inode->io_tree, search_start,
2605 search_start + em_len - 1,
2606 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2609 search_start = extent_map_end(em);
2610 free_extent_map(em);
2617 int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
2618 unsigned int extra_bits,
2619 struct extent_state **cached_state)
2621 WARN_ON(PAGE_ALIGNED(end));
2623 if (start >= i_size_read(&inode->vfs_inode) &&
2624 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2626 * There can't be any extents following eof in this case so just
2627 * set the delalloc new bit for the range directly.
2629 extra_bits |= EXTENT_DELALLOC_NEW;
2633 ret = btrfs_find_new_delalloc_bytes(inode, start,
2640 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2644 /* see btrfs_writepage_start_hook for details on why this is required */
2645 struct btrfs_writepage_fixup {
2647 struct inode *inode;
2648 struct btrfs_work work;
2651 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
2653 struct btrfs_writepage_fixup *fixup;
2654 struct btrfs_ordered_extent *ordered;
2655 struct extent_state *cached_state = NULL;
2656 struct extent_changeset *data_reserved = NULL;
2658 struct btrfs_inode *inode;
2662 bool free_delalloc_space = true;
2664 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2666 inode = BTRFS_I(fixup->inode);
2667 page_start = page_offset(page);
2668 page_end = page_offset(page) + PAGE_SIZE - 1;
2671 * This is similar to page_mkwrite, we need to reserve the space before
2672 * we take the page lock.
2674 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2680 * Before we queued this fixup, we took a reference on the page.
2681 * page->mapping may go NULL, but it shouldn't be moved to a different
2684 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2686 * Unfortunately this is a little tricky, either
2688 * 1) We got here and our page had already been dealt with and
2689 * we reserved our space, thus ret == 0, so we need to just
2690 * drop our space reservation and bail. This can happen the
2691 * first time we come into the fixup worker, or could happen
2692 * while waiting for the ordered extent.
2693 * 2) Our page was already dealt with, but we happened to get an
2694 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2695 * this case we obviously don't have anything to release, but
2696 * because the page was already dealt with we don't want to
2697 * mark the page with an error, so make sure we're resetting
2698 * ret to 0. This is why we have this check _before_ the ret
2699 * check, because we do not want to have a surprise ENOSPC
2700 * when the page was already properly dealt with.
2703 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2704 btrfs_delalloc_release_space(inode, data_reserved,
2705 page_start, PAGE_SIZE,
2713 * We can't mess with the page state unless it is locked, so now that
2714 * it is locked bail if we failed to make our space reservation.
2719 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
2721 /* already ordered? We're done */
2722 if (PageOrdered(page))
2725 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
2727 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2730 btrfs_start_ordered_extent(ordered, 1);
2731 btrfs_put_ordered_extent(ordered);
2735 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
2741 * Everything went as planned, we're now the owner of a dirty page with
2742 * delayed allocation bits set and space reserved for our COW
2745 * The page was dirty when we started, nothing should have cleaned it.
2747 BUG_ON(!PageDirty(page));
2748 free_delalloc_space = false;
2750 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2751 if (free_delalloc_space)
2752 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2754 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2759 * We hit ENOSPC or other errors. Update the mapping and page
2760 * to reflect the errors and clean the page.
2762 mapping_set_error(page->mapping, ret);
2763 end_extent_writepage(page, ret, page_start, page_end);
2764 clear_page_dirty_for_io(page);
2767 ClearPageChecked(page);
2771 extent_changeset_free(data_reserved);
2773 * As a precaution, do a delayed iput in case it would be the last iput
2774 * that could need flushing space. Recursing back to fixup worker would
2777 btrfs_add_delayed_iput(&inode->vfs_inode);
2781 * There are a few paths in the higher layers of the kernel that directly
2782 * set the page dirty bit without asking the filesystem if it is a
2783 * good idea. This causes problems because we want to make sure COW
2784 * properly happens and the data=ordered rules are followed.
2786 * In our case any range that doesn't have the ORDERED bit set
2787 * hasn't been properly setup for IO. We kick off an async process
2788 * to fix it up. The async helper will wait for ordered extents, set
2789 * the delalloc bit and make it safe to write the page.
2791 int btrfs_writepage_cow_fixup(struct page *page)
2793 struct inode *inode = page->mapping->host;
2794 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2795 struct btrfs_writepage_fixup *fixup;
2797 /* This page has ordered extent covering it already */
2798 if (PageOrdered(page))
2802 * PageChecked is set below when we create a fixup worker for this page,
2803 * don't try to create another one if we're already PageChecked()
2805 * The extent_io writepage code will redirty the page if we send back
2808 if (PageChecked(page))
2811 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2816 * We are already holding a reference to this inode from
2817 * write_cache_pages. We need to hold it because the space reservation
2818 * takes place outside of the page lock, and we can't trust
2819 * page->mapping outside of the page lock.
2822 SetPageChecked(page);
2824 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
2826 fixup->inode = inode;
2827 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2832 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2833 struct btrfs_inode *inode, u64 file_pos,
2834 struct btrfs_file_extent_item *stack_fi,
2835 const bool update_inode_bytes,
2836 u64 qgroup_reserved)
2838 struct btrfs_root *root = inode->root;
2839 const u64 sectorsize = root->fs_info->sectorsize;
2840 struct btrfs_path *path;
2841 struct extent_buffer *leaf;
2842 struct btrfs_key ins;
2843 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2844 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2845 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2846 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
2847 struct btrfs_drop_extents_args drop_args = { 0 };
2850 path = btrfs_alloc_path();
2855 * we may be replacing one extent in the tree with another.
2856 * The new extent is pinned in the extent map, and we don't want
2857 * to drop it from the cache until it is completely in the btree.
2859 * So, tell btrfs_drop_extents to leave this extent in the cache.
2860 * the caller is expected to unpin it and allow it to be merged
2863 drop_args.path = path;
2864 drop_args.start = file_pos;
2865 drop_args.end = file_pos + num_bytes;
2866 drop_args.replace_extent = true;
2867 drop_args.extent_item_size = sizeof(*stack_fi);
2868 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
2872 if (!drop_args.extent_inserted) {
2873 ins.objectid = btrfs_ino(inode);
2874 ins.offset = file_pos;
2875 ins.type = BTRFS_EXTENT_DATA_KEY;
2877 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2882 leaf = path->nodes[0];
2883 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2884 write_extent_buffer(leaf, stack_fi,
2885 btrfs_item_ptr_offset(leaf, path->slots[0]),
2886 sizeof(struct btrfs_file_extent_item));
2888 btrfs_mark_buffer_dirty(leaf);
2889 btrfs_release_path(path);
2892 * If we dropped an inline extent here, we know the range where it is
2893 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2894 * number of bytes only for that range containing the inline extent.
2895 * The remaining of the range will be processed when clearning the
2896 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2898 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2899 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2901 inline_size = drop_args.bytes_found - inline_size;
2902 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2903 drop_args.bytes_found -= inline_size;
2904 num_bytes -= sectorsize;
2907 if (update_inode_bytes)
2908 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
2910 ins.objectid = disk_bytenr;
2911 ins.offset = disk_num_bytes;
2912 ins.type = BTRFS_EXTENT_ITEM_KEY;
2914 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
2918 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
2919 file_pos, qgroup_reserved, &ins);
2921 btrfs_free_path(path);
2926 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
2929 struct btrfs_block_group *cache;
2931 cache = btrfs_lookup_block_group(fs_info, start);
2934 spin_lock(&cache->lock);
2935 cache->delalloc_bytes -= len;
2936 spin_unlock(&cache->lock);
2938 btrfs_put_block_group(cache);
2941 static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
2942 struct btrfs_ordered_extent *oe)
2944 struct btrfs_file_extent_item stack_fi;
2946 bool update_inode_bytes;
2948 memset(&stack_fi, 0, sizeof(stack_fi));
2949 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2950 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2951 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2952 oe->disk_num_bytes);
2953 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2954 logical_len = oe->truncated_len;
2956 logical_len = oe->num_bytes;
2957 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2958 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2959 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2960 /* Encryption and other encoding is reserved and all 0 */
2963 * For delalloc, when completing an ordered extent we update the inode's
2964 * bytes when clearing the range in the inode's io tree, so pass false
2965 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2966 * except if the ordered extent was truncated.
2968 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2969 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2971 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2972 oe->file_offset, &stack_fi,
2973 update_inode_bytes, oe->qgroup_rsv);
2977 * As ordered data IO finishes, this gets called so we can finish
2978 * an ordered extent if the range of bytes in the file it covers are
2981 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2983 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2984 struct btrfs_root *root = inode->root;
2985 struct btrfs_fs_info *fs_info = root->fs_info;
2986 struct btrfs_trans_handle *trans = NULL;
2987 struct extent_io_tree *io_tree = &inode->io_tree;
2988 struct extent_state *cached_state = NULL;
2990 int compress_type = 0;
2992 u64 logical_len = ordered_extent->num_bytes;
2993 bool freespace_inode;
2994 bool truncated = false;
2995 bool clear_reserved_extent = true;
2996 unsigned int clear_bits = EXTENT_DEFRAG;
2998 start = ordered_extent->file_offset;
2999 end = start + ordered_extent->num_bytes - 1;
3001 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3002 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
3003 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
3004 clear_bits |= EXTENT_DELALLOC_NEW;
3006 freespace_inode = btrfs_is_free_space_inode(inode);
3008 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
3013 if (ordered_extent->bdev)
3014 btrfs_rewrite_logical_zoned(ordered_extent);
3016 btrfs_free_io_failure_record(inode, start, end);
3018 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
3020 logical_len = ordered_extent->truncated_len;
3021 /* Truncated the entire extent, don't bother adding */
3026 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
3027 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
3029 btrfs_inode_safe_disk_i_size_write(inode, 0);
3030 if (freespace_inode)
3031 trans = btrfs_join_transaction_spacecache(root);
3033 trans = btrfs_join_transaction(root);
3034 if (IS_ERR(trans)) {
3035 ret = PTR_ERR(trans);
3039 trans->block_rsv = &inode->block_rsv;
3040 ret = btrfs_update_inode_fallback(trans, root, inode);
3041 if (ret) /* -ENOMEM or corruption */
3042 btrfs_abort_transaction(trans, ret);
3046 clear_bits |= EXTENT_LOCKED;
3047 lock_extent_bits(io_tree, start, end, &cached_state);
3049 if (freespace_inode)
3050 trans = btrfs_join_transaction_spacecache(root);
3052 trans = btrfs_join_transaction(root);
3053 if (IS_ERR(trans)) {
3054 ret = PTR_ERR(trans);
3059 trans->block_rsv = &inode->block_rsv;
3061 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
3062 compress_type = ordered_extent->compress_type;
3063 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3064 BUG_ON(compress_type);
3065 ret = btrfs_mark_extent_written(trans, inode,
3066 ordered_extent->file_offset,
3067 ordered_extent->file_offset +
3070 BUG_ON(root == fs_info->tree_root);
3071 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
3073 clear_reserved_extent = false;
3074 btrfs_release_delalloc_bytes(fs_info,
3075 ordered_extent->disk_bytenr,
3076 ordered_extent->disk_num_bytes);
3079 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
3080 ordered_extent->num_bytes, trans->transid);
3082 btrfs_abort_transaction(trans, ret);
3086 ret = add_pending_csums(trans, &ordered_extent->list);
3088 btrfs_abort_transaction(trans, ret);
3093 * If this is a new delalloc range, clear its new delalloc flag to
3094 * update the inode's number of bytes. This needs to be done first
3095 * before updating the inode item.
3097 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
3098 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
3099 clear_extent_bit(&inode->io_tree, start, end,
3100 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
3101 0, 0, &cached_state);
3103 btrfs_inode_safe_disk_i_size_write(inode, 0);
3104 ret = btrfs_update_inode_fallback(trans, root, inode);
3105 if (ret) { /* -ENOMEM or corruption */
3106 btrfs_abort_transaction(trans, ret);
3111 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
3112 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
3116 btrfs_end_transaction(trans);
3118 if (ret || truncated) {
3119 u64 unwritten_start = start;
3122 * If we failed to finish this ordered extent for any reason we
3123 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
3124 * extent, and mark the inode with the error if it wasn't
3125 * already set. Any error during writeback would have already
3126 * set the mapping error, so we need to set it if we're the ones
3127 * marking this ordered extent as failed.
3129 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
3130 &ordered_extent->flags))
3131 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
3134 unwritten_start += logical_len;
3135 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
3137 /* Drop the cache for the part of the extent we didn't write. */
3138 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
3141 * If the ordered extent had an IOERR or something else went
3142 * wrong we need to return the space for this ordered extent
3143 * back to the allocator. We only free the extent in the
3144 * truncated case if we didn't write out the extent at all.
3146 * If we made it past insert_reserved_file_extent before we
3147 * errored out then we don't need to do this as the accounting
3148 * has already been done.
3150 if ((ret || !logical_len) &&
3151 clear_reserved_extent &&
3152 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
3153 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
3155 * Discard the range before returning it back to the
3158 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
3159 btrfs_discard_extent(fs_info,
3160 ordered_extent->disk_bytenr,
3161 ordered_extent->disk_num_bytes,
3163 btrfs_free_reserved_extent(fs_info,
3164 ordered_extent->disk_bytenr,
3165 ordered_extent->disk_num_bytes, 1);
3170 * This needs to be done to make sure anybody waiting knows we are done
3171 * updating everything for this ordered extent.
3173 btrfs_remove_ordered_extent(inode, ordered_extent);
3176 btrfs_put_ordered_extent(ordered_extent);
3177 /* once for the tree */
3178 btrfs_put_ordered_extent(ordered_extent);
3183 static void finish_ordered_fn(struct btrfs_work *work)
3185 struct btrfs_ordered_extent *ordered_extent;
3186 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
3187 btrfs_finish_ordered_io(ordered_extent);
3190 void btrfs_writepage_endio_finish_ordered(struct btrfs_inode *inode,
3191 struct page *page, u64 start,
3192 u64 end, bool uptodate)
3194 trace_btrfs_writepage_end_io_hook(inode, start, end, uptodate);
3196 btrfs_mark_ordered_io_finished(inode, page, start, end + 1 - start,
3197 finish_ordered_fn, uptodate);
3201 * check_data_csum - verify checksum of one sector of uncompressed data
3203 * @io_bio: btrfs_io_bio which contains the csum
3204 * @bio_offset: offset to the beginning of the bio (in bytes)
3205 * @page: page where is the data to be verified
3206 * @pgoff: offset inside the page
3207 * @start: logical offset in the file
3209 * The length of such check is always one sector size.
3211 static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
3212 u32 bio_offset, struct page *page, u32 pgoff,
3215 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3216 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
3218 u32 len = fs_info->sectorsize;
3219 const u32 csum_size = fs_info->csum_size;
3220 unsigned int offset_sectors;
3222 u8 csum[BTRFS_CSUM_SIZE];
3224 ASSERT(pgoff + len <= PAGE_SIZE);
3226 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
3227 csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
3229 kaddr = kmap_atomic(page);
3230 shash->tfm = fs_info->csum_shash;
3232 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
3234 if (memcmp(csum, csum_expected, csum_size))
3237 kunmap_atomic(kaddr);
3240 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
3241 io_bio->mirror_num);
3243 btrfs_dev_stat_inc_and_print(io_bio->device,
3244 BTRFS_DEV_STAT_CORRUPTION_ERRS);
3245 memset(kaddr + pgoff, 1, len);
3246 flush_dcache_page(page);
3247 kunmap_atomic(kaddr);
3252 * When reads are done, we need to check csums to verify the data is correct.
3253 * if there's a match, we allow the bio to finish. If not, the code in
3254 * extent_io.c will try to find good copies for us.
3256 * @bio_offset: offset to the beginning of the bio (in bytes)
3257 * @start: file offset of the range start
3258 * @end: file offset of the range end (inclusive)
3260 * Return a bitmap where bit set means a csum mismatch, and bit not set means
3263 unsigned int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
3264 struct page *page, u64 start, u64 end)
3266 struct inode *inode = page->mapping->host;
3267 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3268 struct btrfs_root *root = BTRFS_I(inode)->root;
3269 const u32 sectorsize = root->fs_info->sectorsize;
3271 unsigned int result = 0;
3273 if (PageChecked(page)) {
3274 ClearPageChecked(page);
3279 * For subpage case, above PageChecked is not safe as it's not subpage
3281 * But for now only cow fixup and compressed read utilize PageChecked
3282 * flag, while in this context we can easily use io_bio->csum to
3283 * determine if we really need to do csum verification.
3285 * So for now, just exit if io_bio->csum is NULL, as it means it's
3286 * compressed read, and its compressed data csum has already been
3289 if (io_bio->csum == NULL)
3292 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
3295 if (!root->fs_info->csum_root)
3298 ASSERT(page_offset(page) <= start &&
3299 end <= page_offset(page) + PAGE_SIZE - 1);
3300 for (pg_off = offset_in_page(start);
3301 pg_off < offset_in_page(end);
3302 pg_off += sectorsize, bio_offset += sectorsize) {
3303 u64 file_offset = pg_off + page_offset(page);
3306 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
3307 test_range_bit(io_tree, file_offset,
3308 file_offset + sectorsize - 1,
3309 EXTENT_NODATASUM, 1, NULL)) {
3310 /* Skip the range without csum for data reloc inode */
3311 clear_extent_bits(io_tree, file_offset,
3312 file_offset + sectorsize - 1,
3316 ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off,
3317 page_offset(page) + pg_off);
3319 const int nr_bit = (pg_off - offset_in_page(start)) >>
3320 root->fs_info->sectorsize_bits;
3322 result |= (1U << nr_bit);
3329 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3331 * @inode: The inode we want to perform iput on
3333 * This function uses the generic vfs_inode::i_count to track whether we should
3334 * just decrement it (in case it's > 1) or if this is the last iput then link
3335 * the inode to the delayed iput machinery. Delayed iputs are processed at
3336 * transaction commit time/superblock commit/cleaner kthread.
3338 void btrfs_add_delayed_iput(struct inode *inode)
3340 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3341 struct btrfs_inode *binode = BTRFS_I(inode);
3343 if (atomic_add_unless(&inode->i_count, -1, 1))
3346 atomic_inc(&fs_info->nr_delayed_iputs);
3347 spin_lock(&fs_info->delayed_iput_lock);
3348 ASSERT(list_empty(&binode->delayed_iput));
3349 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3350 spin_unlock(&fs_info->delayed_iput_lock);
3351 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3352 wake_up_process(fs_info->cleaner_kthread);
3355 static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3356 struct btrfs_inode *inode)
3358 list_del_init(&inode->delayed_iput);
3359 spin_unlock(&fs_info->delayed_iput_lock);
3360 iput(&inode->vfs_inode);
3361 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3362 wake_up(&fs_info->delayed_iputs_wait);
3363 spin_lock(&fs_info->delayed_iput_lock);
3366 static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3367 struct btrfs_inode *inode)
3369 if (!list_empty(&inode->delayed_iput)) {
3370 spin_lock(&fs_info->delayed_iput_lock);
3371 if (!list_empty(&inode->delayed_iput))
3372 run_delayed_iput_locked(fs_info, inode);
3373 spin_unlock(&fs_info->delayed_iput_lock);
3377 void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
3380 spin_lock(&fs_info->delayed_iput_lock);
3381 while (!list_empty(&fs_info->delayed_iputs)) {
3382 struct btrfs_inode *inode;
3384 inode = list_first_entry(&fs_info->delayed_iputs,
3385 struct btrfs_inode, delayed_iput);
3386 run_delayed_iput_locked(fs_info, inode);
3387 cond_resched_lock(&fs_info->delayed_iput_lock);
3389 spin_unlock(&fs_info->delayed_iput_lock);
3393 * Wait for flushing all delayed iputs
3395 * @fs_info: the filesystem
3397 * This will wait on any delayed iputs that are currently running with KILLABLE
3398 * set. Once they are all done running we will return, unless we are killed in
3399 * which case we return EINTR. This helps in user operations like fallocate etc
3400 * that might get blocked on the iputs.
3402 * Return EINTR if we were killed, 0 if nothing's pending
3404 int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3406 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3407 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3414 * This creates an orphan entry for the given inode in case something goes wrong
3415 * in the middle of an unlink.
3417 int btrfs_orphan_add(struct btrfs_trans_handle *trans,
3418 struct btrfs_inode *inode)
3422 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3423 if (ret && ret != -EEXIST) {
3424 btrfs_abort_transaction(trans, ret);
3432 * We have done the delete so we can go ahead and remove the orphan item for
3433 * this particular inode.
3435 static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3436 struct btrfs_inode *inode)
3438 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
3442 * this cleans up any orphans that may be left on the list from the last use
3445 int btrfs_orphan_cleanup(struct btrfs_root *root)
3447 struct btrfs_fs_info *fs_info = root->fs_info;
3448 struct btrfs_path *path;
3449 struct extent_buffer *leaf;
3450 struct btrfs_key key, found_key;
3451 struct btrfs_trans_handle *trans;
3452 struct inode *inode;
3453 u64 last_objectid = 0;
3454 int ret = 0, nr_unlink = 0;
3456 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
3459 path = btrfs_alloc_path();
3464 path->reada = READA_BACK;
3466 key.objectid = BTRFS_ORPHAN_OBJECTID;
3467 key.type = BTRFS_ORPHAN_ITEM_KEY;
3468 key.offset = (u64)-1;
3471 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3476 * if ret == 0 means we found what we were searching for, which
3477 * is weird, but possible, so only screw with path if we didn't
3478 * find the key and see if we have stuff that matches
3482 if (path->slots[0] == 0)
3487 /* pull out the item */
3488 leaf = path->nodes[0];
3489 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3491 /* make sure the item matches what we want */
3492 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3494 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
3497 /* release the path since we're done with it */
3498 btrfs_release_path(path);
3501 * this is where we are basically btrfs_lookup, without the
3502 * crossing root thing. we store the inode number in the
3503 * offset of the orphan item.
3506 if (found_key.offset == last_objectid) {
3508 "Error removing orphan entry, stopping orphan cleanup");
3513 last_objectid = found_key.offset;
3515 found_key.objectid = found_key.offset;
3516 found_key.type = BTRFS_INODE_ITEM_KEY;
3517 found_key.offset = 0;
3518 inode = btrfs_iget(fs_info->sb, last_objectid, root);
3519 ret = PTR_ERR_OR_ZERO(inode);
3520 if (ret && ret != -ENOENT)
3523 if (ret == -ENOENT && root == fs_info->tree_root) {
3524 struct btrfs_root *dead_root;
3525 int is_dead_root = 0;
3528 * This is an orphan in the tree root. Currently these
3529 * could come from 2 sources:
3530 * a) a root (snapshot/subvolume) deletion in progress
3531 * b) a free space cache inode
3532 * We need to distinguish those two, as the orphan item
3533 * for a root must not get deleted before the deletion
3534 * of the snapshot/subvolume's tree completes.
3536 * btrfs_find_orphan_roots() ran before us, which has
3537 * found all deleted roots and loaded them into
3538 * fs_info->fs_roots_radix. So here we can find if an
3539 * orphan item corresponds to a deleted root by looking
3540 * up the root from that radix tree.
3543 spin_lock(&fs_info->fs_roots_radix_lock);
3544 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3545 (unsigned long)found_key.objectid);
3546 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3548 spin_unlock(&fs_info->fs_roots_radix_lock);
3551 /* prevent this orphan from being found again */
3552 key.offset = found_key.objectid - 1;
3559 * If we have an inode with links, there are a couple of
3562 * 1. We were halfway through creating fsverity metadata for the
3563 * file. In that case, the orphan item represents incomplete
3564 * fsverity metadata which must be cleaned up with
3565 * btrfs_drop_verity_items and deleting the orphan item.
3567 * 2. Old kernels (before v3.12) used to create an
3568 * orphan item for truncate indicating that there were possibly
3569 * extent items past i_size that needed to be deleted. In v3.12,
3570 * truncate was changed to update i_size in sync with the extent
3571 * items, but the (useless) orphan item was still created. Since
3572 * v4.18, we don't create the orphan item for truncate at all.
3574 * So, this item could mean that we need to do a truncate, but
3575 * only if this filesystem was last used on a pre-v3.12 kernel
3576 * and was not cleanly unmounted. The odds of that are quite
3577 * slim, and it's a pain to do the truncate now, so just delete
3580 * It's also possible that this orphan item was supposed to be
3581 * deleted but wasn't. The inode number may have been reused,
3582 * but either way, we can delete the orphan item.
3584 if (ret == -ENOENT || inode->i_nlink) {
3586 ret = btrfs_drop_verity_items(BTRFS_I(inode));
3591 trans = btrfs_start_transaction(root, 1);
3592 if (IS_ERR(trans)) {
3593 ret = PTR_ERR(trans);
3596 btrfs_debug(fs_info, "auto deleting %Lu",
3597 found_key.objectid);
3598 ret = btrfs_del_orphan_item(trans, root,
3599 found_key.objectid);
3600 btrfs_end_transaction(trans);
3608 /* this will do delete_inode and everything for us */
3611 /* release the path since we're done with it */
3612 btrfs_release_path(path);
3614 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3616 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
3617 trans = btrfs_join_transaction(root);
3619 btrfs_end_transaction(trans);
3623 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
3627 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
3628 btrfs_free_path(path);
3633 * very simple check to peek ahead in the leaf looking for xattrs. If we
3634 * don't find any xattrs, we know there can't be any acls.
3636 * slot is the slot the inode is in, objectid is the objectid of the inode
3638 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3639 int slot, u64 objectid,
3640 int *first_xattr_slot)
3642 u32 nritems = btrfs_header_nritems(leaf);
3643 struct btrfs_key found_key;
3644 static u64 xattr_access = 0;
3645 static u64 xattr_default = 0;
3648 if (!xattr_access) {
3649 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3650 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3651 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3652 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
3656 *first_xattr_slot = -1;
3657 while (slot < nritems) {
3658 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3660 /* we found a different objectid, there must not be acls */
3661 if (found_key.objectid != objectid)
3664 /* we found an xattr, assume we've got an acl */
3665 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3666 if (*first_xattr_slot == -1)
3667 *first_xattr_slot = slot;
3668 if (found_key.offset == xattr_access ||
3669 found_key.offset == xattr_default)
3674 * we found a key greater than an xattr key, there can't
3675 * be any acls later on
3677 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3684 * it goes inode, inode backrefs, xattrs, extents,
3685 * so if there are a ton of hard links to an inode there can
3686 * be a lot of backrefs. Don't waste time searching too hard,
3687 * this is just an optimization
3692 /* we hit the end of the leaf before we found an xattr or
3693 * something larger than an xattr. We have to assume the inode
3696 if (*first_xattr_slot == -1)
3697 *first_xattr_slot = slot;
3702 * read an inode from the btree into the in-memory inode
3704 static int btrfs_read_locked_inode(struct inode *inode,
3705 struct btrfs_path *in_path)
3707 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3708 struct btrfs_path *path = in_path;
3709 struct extent_buffer *leaf;
3710 struct btrfs_inode_item *inode_item;
3711 struct btrfs_root *root = BTRFS_I(inode)->root;
3712 struct btrfs_key location;
3717 bool filled = false;
3718 int first_xattr_slot;
3720 ret = btrfs_fill_inode(inode, &rdev);
3725 path = btrfs_alloc_path();
3730 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3732 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
3734 if (path != in_path)
3735 btrfs_free_path(path);
3739 leaf = path->nodes[0];
3744 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3745 struct btrfs_inode_item);
3746 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3747 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3748 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3749 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3750 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
3751 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3752 round_up(i_size_read(inode), fs_info->sectorsize));
3754 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3755 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
3757 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3758 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
3760 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3761 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
3763 BTRFS_I(inode)->i_otime.tv_sec =
3764 btrfs_timespec_sec(leaf, &inode_item->otime);
3765 BTRFS_I(inode)->i_otime.tv_nsec =
3766 btrfs_timespec_nsec(leaf, &inode_item->otime);
3768 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3769 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3770 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3772 inode_set_iversion_queried(inode,
3773 btrfs_inode_sequence(leaf, inode_item));
3774 inode->i_generation = BTRFS_I(inode)->generation;
3776 rdev = btrfs_inode_rdev(leaf, inode_item);
3778 BTRFS_I(inode)->index_cnt = (u64)-1;
3779 btrfs_inode_split_flags(btrfs_inode_flags(leaf, inode_item),
3780 &BTRFS_I(inode)->flags, &BTRFS_I(inode)->ro_flags);
3784 * If we were modified in the current generation and evicted from memory
3785 * and then re-read we need to do a full sync since we don't have any
3786 * idea about which extents were modified before we were evicted from
3789 * This is required for both inode re-read from disk and delayed inode
3790 * in delayed_nodes_tree.
3792 if (BTRFS_I(inode)->last_trans == fs_info->generation)
3793 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3794 &BTRFS_I(inode)->runtime_flags);
3797 * We don't persist the id of the transaction where an unlink operation
3798 * against the inode was last made. So here we assume the inode might
3799 * have been evicted, and therefore the exact value of last_unlink_trans
3800 * lost, and set it to last_trans to avoid metadata inconsistencies
3801 * between the inode and its parent if the inode is fsync'ed and the log
3802 * replayed. For example, in the scenario:
3805 * ln mydir/foo mydir/bar
3808 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3809 * xfs_io -c fsync mydir/foo
3811 * mount fs, triggers fsync log replay
3813 * We must make sure that when we fsync our inode foo we also log its
3814 * parent inode, otherwise after log replay the parent still has the
3815 * dentry with the "bar" name but our inode foo has a link count of 1
3816 * and doesn't have an inode ref with the name "bar" anymore.
3818 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3819 * but it guarantees correctness at the expense of occasional full
3820 * transaction commits on fsync if our inode is a directory, or if our
3821 * inode is not a directory, logging its parent unnecessarily.
3823 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3826 * Same logic as for last_unlink_trans. We don't persist the generation
3827 * of the last transaction where this inode was used for a reflink
3828 * operation, so after eviction and reloading the inode we must be
3829 * pessimistic and assume the last transaction that modified the inode.
3831 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3834 if (inode->i_nlink != 1 ||
3835 path->slots[0] >= btrfs_header_nritems(leaf))
3838 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3839 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
3842 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3843 if (location.type == BTRFS_INODE_REF_KEY) {
3844 struct btrfs_inode_ref *ref;
3846 ref = (struct btrfs_inode_ref *)ptr;
3847 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3848 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3849 struct btrfs_inode_extref *extref;
3851 extref = (struct btrfs_inode_extref *)ptr;
3852 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3857 * try to precache a NULL acl entry for files that don't have
3858 * any xattrs or acls
3860 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3861 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
3862 if (first_xattr_slot != -1) {
3863 path->slots[0] = first_xattr_slot;
3864 ret = btrfs_load_inode_props(inode, path);
3867 "error loading props for ino %llu (root %llu): %d",
3868 btrfs_ino(BTRFS_I(inode)),
3869 root->root_key.objectid, ret);
3871 if (path != in_path)
3872 btrfs_free_path(path);
3875 cache_no_acl(inode);
3877 switch (inode->i_mode & S_IFMT) {
3879 inode->i_mapping->a_ops = &btrfs_aops;
3880 inode->i_fop = &btrfs_file_operations;
3881 inode->i_op = &btrfs_file_inode_operations;
3884 inode->i_fop = &btrfs_dir_file_operations;
3885 inode->i_op = &btrfs_dir_inode_operations;
3888 inode->i_op = &btrfs_symlink_inode_operations;
3889 inode_nohighmem(inode);
3890 inode->i_mapping->a_ops = &btrfs_aops;
3893 inode->i_op = &btrfs_special_inode_operations;
3894 init_special_inode(inode, inode->i_mode, rdev);
3898 btrfs_sync_inode_flags_to_i_flags(inode);
3903 * given a leaf and an inode, copy the inode fields into the leaf
3905 static void fill_inode_item(struct btrfs_trans_handle *trans,
3906 struct extent_buffer *leaf,
3907 struct btrfs_inode_item *item,
3908 struct inode *inode)
3910 struct btrfs_map_token token;
3913 btrfs_init_map_token(&token, leaf);
3915 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3916 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3917 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3918 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3919 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
3921 btrfs_set_token_timespec_sec(&token, &item->atime,
3922 inode->i_atime.tv_sec);
3923 btrfs_set_token_timespec_nsec(&token, &item->atime,
3924 inode->i_atime.tv_nsec);
3926 btrfs_set_token_timespec_sec(&token, &item->mtime,
3927 inode->i_mtime.tv_sec);
3928 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3929 inode->i_mtime.tv_nsec);
3931 btrfs_set_token_timespec_sec(&token, &item->ctime,
3932 inode->i_ctime.tv_sec);
3933 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3934 inode->i_ctime.tv_nsec);
3936 btrfs_set_token_timespec_sec(&token, &item->otime,
3937 BTRFS_I(inode)->i_otime.tv_sec);
3938 btrfs_set_token_timespec_nsec(&token, &item->otime,
3939 BTRFS_I(inode)->i_otime.tv_nsec);
3941 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3942 btrfs_set_token_inode_generation(&token, item,
3943 BTRFS_I(inode)->generation);
3944 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3945 btrfs_set_token_inode_transid(&token, item, trans->transid);
3946 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3947 flags = btrfs_inode_combine_flags(BTRFS_I(inode)->flags,
3948 BTRFS_I(inode)->ro_flags);
3949 btrfs_set_token_inode_flags(&token, item, flags);
3950 btrfs_set_token_inode_block_group(&token, item, 0);
3954 * copy everything in the in-memory inode into the btree.
3956 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3957 struct btrfs_root *root,
3958 struct btrfs_inode *inode)
3960 struct btrfs_inode_item *inode_item;
3961 struct btrfs_path *path;
3962 struct extent_buffer *leaf;
3965 path = btrfs_alloc_path();
3969 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
3976 leaf = path->nodes[0];
3977 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3978 struct btrfs_inode_item);
3980 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
3981 btrfs_mark_buffer_dirty(leaf);
3982 btrfs_set_inode_last_trans(trans, inode);
3985 btrfs_free_path(path);
3990 * copy everything in the in-memory inode into the btree.
3992 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3993 struct btrfs_root *root,
3994 struct btrfs_inode *inode)
3996 struct btrfs_fs_info *fs_info = root->fs_info;
4000 * If the inode is a free space inode, we can deadlock during commit
4001 * if we put it into the delayed code.
4003 * The data relocation inode should also be directly updated
4006 if (!btrfs_is_free_space_inode(inode)
4007 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
4008 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
4009 btrfs_update_root_times(trans, root);
4011 ret = btrfs_delayed_update_inode(trans, root, inode);
4013 btrfs_set_inode_last_trans(trans, inode);
4017 return btrfs_update_inode_item(trans, root, inode);
4020 int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
4021 struct btrfs_root *root, struct btrfs_inode *inode)
4025 ret = btrfs_update_inode(trans, root, inode);
4027 return btrfs_update_inode_item(trans, root, inode);
4032 * unlink helper that gets used here in inode.c and in the tree logging
4033 * recovery code. It remove a link in a directory with a given name, and
4034 * also drops the back refs in the inode to the directory
4036 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4037 struct btrfs_root *root,
4038 struct btrfs_inode *dir,
4039 struct btrfs_inode *inode,
4040 const char *name, int name_len)
4042 struct btrfs_fs_info *fs_info = root->fs_info;
4043 struct btrfs_path *path;
4045 struct btrfs_dir_item *di;
4047 u64 ino = btrfs_ino(inode);
4048 u64 dir_ino = btrfs_ino(dir);
4050 path = btrfs_alloc_path();
4056 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4057 name, name_len, -1);
4058 if (IS_ERR_OR_NULL(di)) {
4059 ret = di ? PTR_ERR(di) : -ENOENT;
4062 ret = btrfs_delete_one_dir_name(trans, root, path, di);
4065 btrfs_release_path(path);
4068 * If we don't have dir index, we have to get it by looking up
4069 * the inode ref, since we get the inode ref, remove it directly,
4070 * it is unnecessary to do delayed deletion.
4072 * But if we have dir index, needn't search inode ref to get it.
4073 * Since the inode ref is close to the inode item, it is better
4074 * that we delay to delete it, and just do this deletion when
4075 * we update the inode item.
4077 if (inode->dir_index) {
4078 ret = btrfs_delayed_delete_inode_ref(inode);
4080 index = inode->dir_index;
4085 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
4089 "failed to delete reference to %.*s, inode %llu parent %llu",
4090 name_len, name, ino, dir_ino);
4091 btrfs_abort_transaction(trans, ret);
4095 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
4097 btrfs_abort_transaction(trans, ret);
4101 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
4103 if (ret != 0 && ret != -ENOENT) {
4104 btrfs_abort_transaction(trans, ret);
4108 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
4113 btrfs_abort_transaction(trans, ret);
4116 * If we have a pending delayed iput we could end up with the final iput
4117 * being run in btrfs-cleaner context. If we have enough of these built
4118 * up we can end up burning a lot of time in btrfs-cleaner without any
4119 * way to throttle the unlinks. Since we're currently holding a ref on
4120 * the inode we can run the delayed iput here without any issues as the
4121 * final iput won't be done until after we drop the ref we're currently
4124 btrfs_run_delayed_iput(fs_info, inode);
4126 btrfs_free_path(path);
4130 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
4131 inode_inc_iversion(&inode->vfs_inode);
4132 inode_inc_iversion(&dir->vfs_inode);
4133 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
4134 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
4135 ret = btrfs_update_inode(trans, root, dir);
4140 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4141 struct btrfs_root *root,
4142 struct btrfs_inode *dir, struct btrfs_inode *inode,
4143 const char *name, int name_len)
4146 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4148 drop_nlink(&inode->vfs_inode);
4149 ret = btrfs_update_inode(trans, root, inode);
4155 * helper to start transaction for unlink and rmdir.
4157 * unlink and rmdir are special in btrfs, they do not always free space, so
4158 * if we cannot make our reservations the normal way try and see if there is
4159 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4160 * allow the unlink to occur.
4162 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
4164 struct btrfs_root *root = BTRFS_I(dir)->root;
4167 * 1 for the possible orphan item
4168 * 1 for the dir item
4169 * 1 for the dir index
4170 * 1 for the inode ref
4173 return btrfs_start_transaction_fallback_global_rsv(root, 5);
4176 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4178 struct btrfs_root *root = BTRFS_I(dir)->root;
4179 struct btrfs_trans_handle *trans;
4180 struct inode *inode = d_inode(dentry);
4183 trans = __unlink_start_trans(dir);
4185 return PTR_ERR(trans);
4187 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4190 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4191 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4192 dentry->d_name.len);
4196 if (inode->i_nlink == 0) {
4197 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
4203 btrfs_end_transaction(trans);
4204 btrfs_btree_balance_dirty(root->fs_info);
4208 static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4209 struct inode *dir, struct dentry *dentry)
4211 struct btrfs_root *root = BTRFS_I(dir)->root;
4212 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
4213 struct btrfs_path *path;
4214 struct extent_buffer *leaf;
4215 struct btrfs_dir_item *di;
4216 struct btrfs_key key;
4217 const char *name = dentry->d_name.name;
4218 int name_len = dentry->d_name.len;
4222 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
4224 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
4225 objectid = inode->root->root_key.objectid;
4226 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4227 objectid = inode->location.objectid;
4233 path = btrfs_alloc_path();
4237 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4238 name, name_len, -1);
4239 if (IS_ERR_OR_NULL(di)) {
4240 ret = di ? PTR_ERR(di) : -ENOENT;
4244 leaf = path->nodes[0];
4245 btrfs_dir_item_key_to_cpu(leaf, di, &key);
4246 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4247 ret = btrfs_delete_one_dir_name(trans, root, path, di);
4249 btrfs_abort_transaction(trans, ret);
4252 btrfs_release_path(path);
4255 * This is a placeholder inode for a subvolume we didn't have a
4256 * reference to at the time of the snapshot creation. In the meantime
4257 * we could have renamed the real subvol link into our snapshot, so
4258 * depending on btrfs_del_root_ref to return -ENOENT here is incorrect.
4259 * Instead simply lookup the dir_index_item for this entry so we can
4260 * remove it. Otherwise we know we have a ref to the root and we can
4261 * call btrfs_del_root_ref, and it _shouldn't_ fail.
4263 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
4264 di = btrfs_search_dir_index_item(root, path, dir_ino,
4266 if (IS_ERR_OR_NULL(di)) {
4271 btrfs_abort_transaction(trans, ret);
4275 leaf = path->nodes[0];
4276 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4278 btrfs_release_path(path);
4280 ret = btrfs_del_root_ref(trans, objectid,
4281 root->root_key.objectid, dir_ino,
4282 &index, name, name_len);
4284 btrfs_abort_transaction(trans, ret);
4289 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
4291 btrfs_abort_transaction(trans, ret);
4295 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
4296 inode_inc_iversion(dir);
4297 dir->i_mtime = dir->i_ctime = current_time(dir);
4298 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
4300 btrfs_abort_transaction(trans, ret);
4302 btrfs_free_path(path);
4307 * Helper to check if the subvolume references other subvolumes or if it's
4310 static noinline int may_destroy_subvol(struct btrfs_root *root)
4312 struct btrfs_fs_info *fs_info = root->fs_info;
4313 struct btrfs_path *path;
4314 struct btrfs_dir_item *di;
4315 struct btrfs_key key;
4319 path = btrfs_alloc_path();
4323 /* Make sure this root isn't set as the default subvol */
4324 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4325 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4326 dir_id, "default", 7, 0);
4327 if (di && !IS_ERR(di)) {
4328 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4329 if (key.objectid == root->root_key.objectid) {
4332 "deleting default subvolume %llu is not allowed",
4336 btrfs_release_path(path);
4339 key.objectid = root->root_key.objectid;
4340 key.type = BTRFS_ROOT_REF_KEY;
4341 key.offset = (u64)-1;
4343 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4349 if (path->slots[0] > 0) {
4351 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4352 if (key.objectid == root->root_key.objectid &&
4353 key.type == BTRFS_ROOT_REF_KEY)
4357 btrfs_free_path(path);
4361 /* Delete all dentries for inodes belonging to the root */
4362 static void btrfs_prune_dentries(struct btrfs_root *root)
4364 struct btrfs_fs_info *fs_info = root->fs_info;
4365 struct rb_node *node;
4366 struct rb_node *prev;
4367 struct btrfs_inode *entry;
4368 struct inode *inode;
4371 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4372 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4374 spin_lock(&root->inode_lock);
4376 node = root->inode_tree.rb_node;
4380 entry = rb_entry(node, struct btrfs_inode, rb_node);
4382 if (objectid < btrfs_ino(entry))
4383 node = node->rb_left;
4384 else if (objectid > btrfs_ino(entry))
4385 node = node->rb_right;
4391 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4392 if (objectid <= btrfs_ino(entry)) {
4396 prev = rb_next(prev);
4400 entry = rb_entry(node, struct btrfs_inode, rb_node);
4401 objectid = btrfs_ino(entry) + 1;
4402 inode = igrab(&entry->vfs_inode);
4404 spin_unlock(&root->inode_lock);
4405 if (atomic_read(&inode->i_count) > 1)
4406 d_prune_aliases(inode);
4408 * btrfs_drop_inode will have it removed from the inode
4409 * cache when its usage count hits zero.
4413 spin_lock(&root->inode_lock);
4417 if (cond_resched_lock(&root->inode_lock))
4420 node = rb_next(node);
4422 spin_unlock(&root->inode_lock);
4425 int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4427 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4428 struct btrfs_root *root = BTRFS_I(dir)->root;
4429 struct inode *inode = d_inode(dentry);
4430 struct btrfs_root *dest = BTRFS_I(inode)->root;
4431 struct btrfs_trans_handle *trans;
4432 struct btrfs_block_rsv block_rsv;
4437 * Don't allow to delete a subvolume with send in progress. This is
4438 * inside the inode lock so the error handling that has to drop the bit
4439 * again is not run concurrently.
4441 spin_lock(&dest->root_item_lock);
4442 if (dest->send_in_progress) {
4443 spin_unlock(&dest->root_item_lock);
4445 "attempt to delete subvolume %llu during send",
4446 dest->root_key.objectid);
4449 root_flags = btrfs_root_flags(&dest->root_item);
4450 btrfs_set_root_flags(&dest->root_item,
4451 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4452 spin_unlock(&dest->root_item_lock);
4454 down_write(&fs_info->subvol_sem);
4456 ret = may_destroy_subvol(dest);
4460 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4462 * One for dir inode,
4463 * two for dir entries,
4464 * two for root ref/backref.
4466 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4470 trans = btrfs_start_transaction(root, 0);
4471 if (IS_ERR(trans)) {
4472 ret = PTR_ERR(trans);
4475 trans->block_rsv = &block_rsv;
4476 trans->bytes_reserved = block_rsv.size;
4478 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4480 ret = btrfs_unlink_subvol(trans, dir, dentry);
4482 btrfs_abort_transaction(trans, ret);
4486 ret = btrfs_record_root_in_trans(trans, dest);
4488 btrfs_abort_transaction(trans, ret);
4492 memset(&dest->root_item.drop_progress, 0,
4493 sizeof(dest->root_item.drop_progress));
4494 btrfs_set_root_drop_level(&dest->root_item, 0);
4495 btrfs_set_root_refs(&dest->root_item, 0);
4497 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4498 ret = btrfs_insert_orphan_item(trans,
4500 dest->root_key.objectid);
4502 btrfs_abort_transaction(trans, ret);
4507 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
4508 BTRFS_UUID_KEY_SUBVOL,
4509 dest->root_key.objectid);
4510 if (ret && ret != -ENOENT) {
4511 btrfs_abort_transaction(trans, ret);
4514 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
4515 ret = btrfs_uuid_tree_remove(trans,
4516 dest->root_item.received_uuid,
4517 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4518 dest->root_key.objectid);
4519 if (ret && ret != -ENOENT) {
4520 btrfs_abort_transaction(trans, ret);
4525 free_anon_bdev(dest->anon_dev);
4528 trans->block_rsv = NULL;
4529 trans->bytes_reserved = 0;
4530 ret = btrfs_end_transaction(trans);
4531 inode->i_flags |= S_DEAD;
4533 btrfs_subvolume_release_metadata(root, &block_rsv);
4535 up_write(&fs_info->subvol_sem);
4537 spin_lock(&dest->root_item_lock);
4538 root_flags = btrfs_root_flags(&dest->root_item);
4539 btrfs_set_root_flags(&dest->root_item,
4540 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4541 spin_unlock(&dest->root_item_lock);
4543 d_invalidate(dentry);
4544 btrfs_prune_dentries(dest);
4545 ASSERT(dest->send_in_progress == 0);
4551 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4553 struct inode *inode = d_inode(dentry);
4555 struct btrfs_root *root = BTRFS_I(dir)->root;
4556 struct btrfs_trans_handle *trans;
4557 u64 last_unlink_trans;
4559 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4561 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
4562 return btrfs_delete_subvolume(dir, dentry);
4564 trans = __unlink_start_trans(dir);
4566 return PTR_ERR(trans);
4568 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4569 err = btrfs_unlink_subvol(trans, dir, dentry);
4573 err = btrfs_orphan_add(trans, BTRFS_I(inode));
4577 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4579 /* now the directory is empty */
4580 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4581 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4582 dentry->d_name.len);
4584 btrfs_i_size_write(BTRFS_I(inode), 0);
4586 * Propagate the last_unlink_trans value of the deleted dir to
4587 * its parent directory. This is to prevent an unrecoverable
4588 * log tree in the case we do something like this:
4590 * 2) create snapshot under dir foo
4591 * 3) delete the snapshot
4594 * 6) fsync foo or some file inside foo
4596 if (last_unlink_trans >= trans->transid)
4597 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4600 btrfs_end_transaction(trans);
4601 btrfs_btree_balance_dirty(root->fs_info);
4607 * Return this if we need to call truncate_block for the last bit of the
4610 #define NEED_TRUNCATE_BLOCK 1
4613 * Remove inode items from a given root.
4615 * @trans: A transaction handle.
4616 * @root: The root from which to remove items.
4617 * @inode: The inode whose items we want to remove.
4618 * @new_size: The new i_size for the inode. This is only applicable when
4619 * @min_type is BTRFS_EXTENT_DATA_KEY, must be 0 otherwise.
4620 * @min_type: The minimum key type to remove. All keys with a type
4621 * greater than this value are removed and all keys with
4622 * this type are removed only if their offset is >= @new_size.
4623 * @extents_found: Output parameter that will contain the number of file
4624 * extent items that were removed or adjusted to the new
4625 * inode i_size. The caller is responsible for initializing
4626 * the counter. Also, it can be NULL if the caller does not
4627 * need this counter.
4629 * Remove all keys associated with the inode from the given root that have a key
4630 * with a type greater than or equals to @min_type. When @min_type has a value of
4631 * BTRFS_EXTENT_DATA_KEY, only remove file extent items that have an offset value
4632 * greater than or equals to @new_size. If a file extent item that starts before
4633 * @new_size and ends after it is found, its length is adjusted.
4635 * Returns: 0 on success, < 0 on error and NEED_TRUNCATE_BLOCK when @min_type is
4636 * BTRFS_EXTENT_DATA_KEY and the caller must truncate the last block.
4638 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4639 struct btrfs_root *root,
4640 struct btrfs_inode *inode,
4641 u64 new_size, u32 min_type,
4644 struct btrfs_fs_info *fs_info = root->fs_info;
4645 struct btrfs_path *path;
4646 struct extent_buffer *leaf;
4647 struct btrfs_file_extent_item *fi;
4648 struct btrfs_key key;
4649 struct btrfs_key found_key;
4650 u64 extent_start = 0;
4651 u64 extent_num_bytes = 0;
4652 u64 extent_offset = 0;
4654 u64 last_size = new_size;
4655 u32 found_type = (u8)-1;
4658 int pending_del_nr = 0;
4659 int pending_del_slot = 0;
4660 int extent_type = -1;
4662 u64 ino = btrfs_ino(inode);
4663 u64 bytes_deleted = 0;
4664 bool be_nice = false;
4665 bool should_throttle = false;
4666 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4667 struct extent_state *cached_state = NULL;
4669 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
4672 * For non-free space inodes and non-shareable roots, we want to back
4673 * off from time to time. This means all inodes in subvolume roots,
4674 * reloc roots, and data reloc roots.
4676 if (!btrfs_is_free_space_inode(inode) &&
4677 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
4680 path = btrfs_alloc_path();
4683 path->reada = READA_BACK;
4685 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4686 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
4690 * We want to drop from the next block forward in case this
4691 * new size is not block aligned since we will be keeping the
4692 * last block of the extent just the way it is.
4694 btrfs_drop_extent_cache(inode, ALIGN(new_size,
4695 fs_info->sectorsize),
4700 * This function is also used to drop the items in the log tree before
4701 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4702 * it is used to drop the logged items. So we shouldn't kill the delayed
4705 if (min_type == 0 && root == inode->root)
4706 btrfs_kill_delayed_inode_items(inode);
4709 key.offset = (u64)-1;
4714 * with a 16K leaf size and 128MB extents, you can actually queue
4715 * up a huge file in a single leaf. Most of the time that
4716 * bytes_deleted is > 0, it will be huge by the time we get here
4718 if (be_nice && bytes_deleted > SZ_32M &&
4719 btrfs_should_end_transaction(trans)) {
4724 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4730 /* there are no items in the tree for us to truncate, we're
4733 if (path->slots[0] == 0)
4739 u64 clear_start = 0, clear_len = 0;
4742 leaf = path->nodes[0];
4743 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4744 found_type = found_key.type;
4746 if (found_key.objectid != ino)
4749 if (found_type < min_type)
4752 item_end = found_key.offset;
4753 if (found_type == BTRFS_EXTENT_DATA_KEY) {
4754 fi = btrfs_item_ptr(leaf, path->slots[0],
4755 struct btrfs_file_extent_item);
4756 extent_type = btrfs_file_extent_type(leaf, fi);
4757 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4759 btrfs_file_extent_num_bytes(leaf, fi);
4761 trace_btrfs_truncate_show_fi_regular(
4762 inode, leaf, fi, found_key.offset);
4763 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4764 item_end += btrfs_file_extent_ram_bytes(leaf,
4767 trace_btrfs_truncate_show_fi_inline(
4768 inode, leaf, fi, path->slots[0],
4773 if (found_type > min_type) {
4776 if (item_end < new_size)
4778 if (found_key.offset >= new_size)
4784 /* FIXME, shrink the extent if the ref count is only 1 */
4785 if (found_type != BTRFS_EXTENT_DATA_KEY)
4788 if (extents_found != NULL)
4791 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4794 clear_start = found_key.offset;
4795 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
4797 u64 orig_num_bytes =
4798 btrfs_file_extent_num_bytes(leaf, fi);
4799 extent_num_bytes = ALIGN(new_size -
4801 fs_info->sectorsize);
4802 clear_start = ALIGN(new_size, fs_info->sectorsize);
4803 btrfs_set_file_extent_num_bytes(leaf, fi,
4805 num_dec = (orig_num_bytes -
4807 if (test_bit(BTRFS_ROOT_SHAREABLE,
4810 inode_sub_bytes(&inode->vfs_inode,
4812 btrfs_mark_buffer_dirty(leaf);
4815 btrfs_file_extent_disk_num_bytes(leaf,
4817 extent_offset = found_key.offset -
4818 btrfs_file_extent_offset(leaf, fi);
4820 /* FIXME blocksize != 4096 */
4821 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
4822 if (extent_start != 0) {
4824 if (test_bit(BTRFS_ROOT_SHAREABLE,
4826 inode_sub_bytes(&inode->vfs_inode,
4830 clear_len = num_dec;
4831 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4833 * we can't truncate inline items that have had
4837 btrfs_file_extent_encryption(leaf, fi) == 0 &&
4838 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4839 btrfs_file_extent_compression(leaf, fi) == 0) {
4840 u32 size = (u32)(new_size - found_key.offset);
4842 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4843 size = btrfs_file_extent_calc_inline_size(size);
4844 btrfs_truncate_item(path, size, 1);
4845 } else if (!del_item) {
4847 * We have to bail so the last_size is set to
4848 * just before this extent.
4850 ret = NEED_TRUNCATE_BLOCK;
4854 * Inline extents are special, we just treat
4855 * them as a full sector worth in the file
4856 * extent tree just for simplicity sake.
4858 clear_len = fs_info->sectorsize;
4861 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
4862 inode_sub_bytes(&inode->vfs_inode,
4863 item_end + 1 - new_size);
4867 * We use btrfs_truncate_inode_items() to clean up log trees for
4868 * multiple fsyncs, and in this case we don't want to clear the
4869 * file extent range because it's just the log.
4871 if (root == inode->root) {
4872 ret = btrfs_inode_clear_file_extent_range(inode,
4873 clear_start, clear_len);
4875 btrfs_abort_transaction(trans, ret);
4881 last_size = found_key.offset;
4883 last_size = new_size;
4885 if (!pending_del_nr) {
4886 /* no pending yet, add ourselves */
4887 pending_del_slot = path->slots[0];
4889 } else if (pending_del_nr &&
4890 path->slots[0] + 1 == pending_del_slot) {
4891 /* hop on the pending chunk */
4893 pending_del_slot = path->slots[0];
4900 should_throttle = false;
4903 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4904 struct btrfs_ref ref = { 0 };
4906 bytes_deleted += extent_num_bytes;
4908 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4909 extent_start, extent_num_bytes, 0);
4910 ref.real_root = root->root_key.objectid;
4911 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4912 ino, extent_offset);
4913 ret = btrfs_free_extent(trans, &ref);
4915 btrfs_abort_transaction(trans, ret);
4919 if (btrfs_should_throttle_delayed_refs(trans))
4920 should_throttle = true;
4924 if (found_type == BTRFS_INODE_ITEM_KEY)
4927 if (path->slots[0] == 0 ||
4928 path->slots[0] != pending_del_slot ||
4930 if (pending_del_nr) {
4931 ret = btrfs_del_items(trans, root, path,
4935 btrfs_abort_transaction(trans, ret);
4940 btrfs_release_path(path);
4943 * We can generate a lot of delayed refs, so we need to
4944 * throttle every once and a while and make sure we're
4945 * adding enough space to keep up with the work we are
4946 * generating. Since we hold a transaction here we
4947 * can't flush, and we don't want to FLUSH_LIMIT because
4948 * we could have generated too many delayed refs to
4949 * actually allocate, so just bail if we're short and
4950 * let the normal reservation dance happen higher up.
4952 if (should_throttle) {
4953 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4954 BTRFS_RESERVE_NO_FLUSH);
4966 if (ret >= 0 && pending_del_nr) {
4969 err = btrfs_del_items(trans, root, path, pending_del_slot,
4972 btrfs_abort_transaction(trans, err);
4976 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4977 ASSERT(last_size >= new_size);
4978 if (!ret && last_size > new_size)
4979 last_size = new_size;
4980 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4981 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4985 btrfs_free_path(path);
4990 * btrfs_truncate_block - read, zero a chunk and write a block
4991 * @inode - inode that we're zeroing
4992 * @from - the offset to start zeroing
4993 * @len - the length to zero, 0 to zero the entire range respective to the
4995 * @front - zero up to the offset instead of from the offset on
4997 * This will find the block for the "from" offset and cow the block and zero the
4998 * part we want to zero. This is used with truncate and hole punching.
5000 int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
5003 struct btrfs_fs_info *fs_info = inode->root->fs_info;
5004 struct address_space *mapping = inode->vfs_inode.i_mapping;
5005 struct extent_io_tree *io_tree = &inode->io_tree;
5006 struct btrfs_ordered_extent *ordered;
5007 struct extent_state *cached_state = NULL;
5008 struct extent_changeset *data_reserved = NULL;
5009 bool only_release_metadata = false;
5010 u32 blocksize = fs_info->sectorsize;
5011 pgoff_t index = from >> PAGE_SHIFT;
5012 unsigned offset = from & (blocksize - 1);
5014 gfp_t mask = btrfs_alloc_write_mask(mapping);
5015 size_t write_bytes = blocksize;
5020 if (IS_ALIGNED(offset, blocksize) &&
5021 (!len || IS_ALIGNED(len, blocksize)))
5024 block_start = round_down(from, blocksize);
5025 block_end = block_start + blocksize - 1;
5027 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
5030 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
5031 /* For nocow case, no need to reserve data space */
5032 only_release_metadata = true;
5037 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
5039 if (!only_release_metadata)
5040 btrfs_free_reserved_data_space(inode, data_reserved,
5041 block_start, blocksize);
5045 page = find_or_create_page(mapping, index, mask);
5047 btrfs_delalloc_release_space(inode, data_reserved, block_start,
5049 btrfs_delalloc_release_extents(inode, blocksize);
5053 ret = set_page_extent_mapped(page);
5057 if (!PageUptodate(page)) {
5058 ret = btrfs_readpage(NULL, page);
5060 if (page->mapping != mapping) {
5065 if (!PageUptodate(page)) {
5070 wait_on_page_writeback(page);
5072 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
5074 ordered = btrfs_lookup_ordered_extent(inode, block_start);
5076 unlock_extent_cached(io_tree, block_start, block_end,
5080 btrfs_start_ordered_extent(ordered, 1);
5081 btrfs_put_ordered_extent(ordered);
5085 clear_extent_bit(&inode->io_tree, block_start, block_end,
5086 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
5087 0, 0, &cached_state);
5089 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
5092 unlock_extent_cached(io_tree, block_start, block_end,
5097 if (offset != blocksize) {
5099 len = blocksize - offset;
5101 memzero_page(page, (block_start - page_offset(page)),
5104 memzero_page(page, (block_start - page_offset(page)) + offset,
5106 flush_dcache_page(page);
5108 ClearPageChecked(page);
5109 btrfs_page_set_dirty(fs_info, page, block_start, block_end + 1 - block_start);
5110 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
5112 if (only_release_metadata)
5113 set_extent_bit(&inode->io_tree, block_start, block_end,
5114 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
5118 if (only_release_metadata)
5119 btrfs_delalloc_release_metadata(inode, blocksize, true);
5121 btrfs_delalloc_release_space(inode, data_reserved,
5122 block_start, blocksize, true);
5124 btrfs_delalloc_release_extents(inode, blocksize);
5128 if (only_release_metadata)
5129 btrfs_check_nocow_unlock(inode);
5130 extent_changeset_free(data_reserved);
5134 static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
5135 u64 offset, u64 len)
5137 struct btrfs_fs_info *fs_info = root->fs_info;
5138 struct btrfs_trans_handle *trans;
5139 struct btrfs_drop_extents_args drop_args = { 0 };
5143 * If NO_HOLES is enabled, we don't need to do anything.
5144 * Later, up in the call chain, either btrfs_set_inode_last_sub_trans()
5145 * or btrfs_update_inode() will be called, which guarantee that the next
5146 * fsync will know this inode was changed and needs to be logged.
5148 if (btrfs_fs_incompat(fs_info, NO_HOLES))
5152 * 1 - for the one we're dropping
5153 * 1 - for the one we're adding
5154 * 1 - for updating the inode.
5156 trans = btrfs_start_transaction(root, 3);
5158 return PTR_ERR(trans);
5160 drop_args.start = offset;
5161 drop_args.end = offset + len;
5162 drop_args.drop_cache = true;
5164 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
5166 btrfs_abort_transaction(trans, ret);
5167 btrfs_end_transaction(trans);
5171 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
5172 offset, 0, 0, len, 0, len, 0, 0, 0);
5174 btrfs_abort_transaction(trans, ret);
5176 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
5177 btrfs_update_inode(trans, root, inode);
5179 btrfs_end_transaction(trans);
5184 * This function puts in dummy file extents for the area we're creating a hole
5185 * for. So if we are truncating this file to a larger size we need to insert
5186 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
5187 * the range between oldsize and size
5189 int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
5191 struct btrfs_root *root = inode->root;
5192 struct btrfs_fs_info *fs_info = root->fs_info;
5193 struct extent_io_tree *io_tree = &inode->io_tree;
5194 struct extent_map *em = NULL;
5195 struct extent_state *cached_state = NULL;
5196 struct extent_map_tree *em_tree = &inode->extent_tree;
5197 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
5198 u64 block_end = ALIGN(size, fs_info->sectorsize);
5205 * If our size started in the middle of a block we need to zero out the
5206 * rest of the block before we expand the i_size, otherwise we could
5207 * expose stale data.
5209 err = btrfs_truncate_block(inode, oldsize, 0, 0);
5213 if (size <= hole_start)
5216 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
5218 cur_offset = hole_start;
5220 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5221 block_end - cur_offset);
5227 last_byte = min(extent_map_end(em), block_end);
5228 last_byte = ALIGN(last_byte, fs_info->sectorsize);
5229 hole_size = last_byte - cur_offset;
5231 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
5232 struct extent_map *hole_em;
5234 err = maybe_insert_hole(root, inode, cur_offset,
5239 err = btrfs_inode_set_file_extent_range(inode,
5240 cur_offset, hole_size);
5244 btrfs_drop_extent_cache(inode, cur_offset,
5245 cur_offset + hole_size - 1, 0);
5246 hole_em = alloc_extent_map();
5248 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
5249 &inode->runtime_flags);
5252 hole_em->start = cur_offset;
5253 hole_em->len = hole_size;
5254 hole_em->orig_start = cur_offset;
5256 hole_em->block_start = EXTENT_MAP_HOLE;
5257 hole_em->block_len = 0;
5258 hole_em->orig_block_len = 0;
5259 hole_em->ram_bytes = hole_size;
5260 hole_em->compress_type = BTRFS_COMPRESS_NONE;
5261 hole_em->generation = fs_info->generation;
5264 write_lock(&em_tree->lock);
5265 err = add_extent_mapping(em_tree, hole_em, 1);
5266 write_unlock(&em_tree->lock);
5269 btrfs_drop_extent_cache(inode, cur_offset,
5273 free_extent_map(hole_em);
5275 err = btrfs_inode_set_file_extent_range(inode,
5276 cur_offset, hole_size);
5281 free_extent_map(em);
5283 cur_offset = last_byte;
5284 if (cur_offset >= block_end)
5287 free_extent_map(em);
5288 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
5292 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
5294 struct btrfs_root *root = BTRFS_I(inode)->root;
5295 struct btrfs_trans_handle *trans;
5296 loff_t oldsize = i_size_read(inode);
5297 loff_t newsize = attr->ia_size;
5298 int mask = attr->ia_valid;
5302 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5303 * special case where we need to update the times despite not having
5304 * these flags set. For all other operations the VFS set these flags
5305 * explicitly if it wants a timestamp update.
5307 if (newsize != oldsize) {
5308 inode_inc_iversion(inode);
5309 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
5310 inode->i_ctime = inode->i_mtime =
5311 current_time(inode);
5314 if (newsize > oldsize) {
5316 * Don't do an expanding truncate while snapshotting is ongoing.
5317 * This is to ensure the snapshot captures a fully consistent
5318 * state of this file - if the snapshot captures this expanding
5319 * truncation, it must capture all writes that happened before
5322 btrfs_drew_write_lock(&root->snapshot_lock);
5323 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
5325 btrfs_drew_write_unlock(&root->snapshot_lock);
5329 trans = btrfs_start_transaction(root, 1);
5330 if (IS_ERR(trans)) {
5331 btrfs_drew_write_unlock(&root->snapshot_lock);
5332 return PTR_ERR(trans);
5335 i_size_write(inode, newsize);
5336 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
5337 pagecache_isize_extended(inode, oldsize, newsize);
5338 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
5339 btrfs_drew_write_unlock(&root->snapshot_lock);
5340 btrfs_end_transaction(trans);
5342 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5344 if (btrfs_is_zoned(fs_info)) {
5345 ret = btrfs_wait_ordered_range(inode,
5346 ALIGN(newsize, fs_info->sectorsize),
5353 * We're truncating a file that used to have good data down to
5354 * zero. Make sure any new writes to the file get on disk
5358 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
5359 &BTRFS_I(inode)->runtime_flags);
5361 truncate_setsize(inode, newsize);
5363 inode_dio_wait(inode);
5365 ret = btrfs_truncate(inode, newsize == oldsize);
5366 if (ret && inode->i_nlink) {
5370 * Truncate failed, so fix up the in-memory size. We
5371 * adjusted disk_i_size down as we removed extents, so
5372 * wait for disk_i_size to be stable and then update the
5373 * in-memory size to match.
5375 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5378 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5385 static int btrfs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5388 struct inode *inode = d_inode(dentry);
5389 struct btrfs_root *root = BTRFS_I(inode)->root;
5392 if (btrfs_root_readonly(root))
5395 err = setattr_prepare(mnt_userns, dentry, attr);
5399 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
5400 err = btrfs_setsize(inode, attr);
5405 if (attr->ia_valid) {
5406 setattr_copy(mnt_userns, inode, attr);
5407 inode_inc_iversion(inode);
5408 err = btrfs_dirty_inode(inode);
5410 if (!err && attr->ia_valid & ATTR_MODE)
5411 err = posix_acl_chmod(mnt_userns, inode, inode->i_mode);
5418 * While truncating the inode pages during eviction, we get the VFS calling
5419 * btrfs_invalidatepage() against each page of the inode. This is slow because
5420 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5421 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5422 * extent_state structures over and over, wasting lots of time.
5424 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5425 * those expensive operations on a per page basis and do only the ordered io
5426 * finishing, while we release here the extent_map and extent_state structures,
5427 * without the excessive merging and splitting.
5429 static void evict_inode_truncate_pages(struct inode *inode)
5431 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5432 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5433 struct rb_node *node;
5435 ASSERT(inode->i_state & I_FREEING);
5436 truncate_inode_pages_final(&inode->i_data);
5438 write_lock(&map_tree->lock);
5439 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
5440 struct extent_map *em;
5442 node = rb_first_cached(&map_tree->map);
5443 em = rb_entry(node, struct extent_map, rb_node);
5444 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5445 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
5446 remove_extent_mapping(map_tree, em);
5447 free_extent_map(em);
5448 if (need_resched()) {
5449 write_unlock(&map_tree->lock);
5451 write_lock(&map_tree->lock);
5454 write_unlock(&map_tree->lock);
5457 * Keep looping until we have no more ranges in the io tree.
5458 * We can have ongoing bios started by readahead that have
5459 * their endio callback (extent_io.c:end_bio_extent_readpage)
5460 * still in progress (unlocked the pages in the bio but did not yet
5461 * unlocked the ranges in the io tree). Therefore this means some
5462 * ranges can still be locked and eviction started because before
5463 * submitting those bios, which are executed by a separate task (work
5464 * queue kthread), inode references (inode->i_count) were not taken
5465 * (which would be dropped in the end io callback of each bio).
5466 * Therefore here we effectively end up waiting for those bios and
5467 * anyone else holding locked ranges without having bumped the inode's
5468 * reference count - if we don't do it, when they access the inode's
5469 * io_tree to unlock a range it may be too late, leading to an
5470 * use-after-free issue.
5472 spin_lock(&io_tree->lock);
5473 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5474 struct extent_state *state;
5475 struct extent_state *cached_state = NULL;
5478 unsigned state_flags;
5480 node = rb_first(&io_tree->state);
5481 state = rb_entry(node, struct extent_state, rb_node);
5482 start = state->start;
5484 state_flags = state->state;
5485 spin_unlock(&io_tree->lock);
5487 lock_extent_bits(io_tree, start, end, &cached_state);
5490 * If still has DELALLOC flag, the extent didn't reach disk,
5491 * and its reserved space won't be freed by delayed_ref.
5492 * So we need to free its reserved space here.
5493 * (Refer to comment in btrfs_invalidatepage, case 2)
5495 * Note, end is the bytenr of last byte, so we need + 1 here.
5497 if (state_flags & EXTENT_DELALLOC)
5498 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5501 clear_extent_bit(io_tree, start, end,
5502 EXTENT_LOCKED | EXTENT_DELALLOC |
5503 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5507 spin_lock(&io_tree->lock);
5509 spin_unlock(&io_tree->lock);
5512 static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
5513 struct btrfs_block_rsv *rsv)
5515 struct btrfs_fs_info *fs_info = root->fs_info;
5516 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5517 struct btrfs_trans_handle *trans;
5518 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
5522 * Eviction should be taking place at some place safe because of our
5523 * delayed iputs. However the normal flushing code will run delayed
5524 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5526 * We reserve the delayed_refs_extra here again because we can't use
5527 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5528 * above. We reserve our extra bit here because we generate a ton of
5529 * delayed refs activity by truncating.
5531 * If we cannot make our reservation we'll attempt to steal from the
5532 * global reserve, because we really want to be able to free up space.
5534 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5535 BTRFS_RESERVE_FLUSH_EVICT);
5538 * Try to steal from the global reserve if there is space for
5541 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5542 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5544 "could not allocate space for delete; will truncate on mount");
5545 return ERR_PTR(-ENOSPC);
5547 delayed_refs_extra = 0;
5550 trans = btrfs_join_transaction(root);
5554 if (delayed_refs_extra) {
5555 trans->block_rsv = &fs_info->trans_block_rsv;
5556 trans->bytes_reserved = delayed_refs_extra;
5557 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5558 delayed_refs_extra, 1);
5563 void btrfs_evict_inode(struct inode *inode)
5565 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5566 struct btrfs_trans_handle *trans;
5567 struct btrfs_root *root = BTRFS_I(inode)->root;
5568 struct btrfs_block_rsv *rsv;
5571 trace_btrfs_inode_evict(inode);
5574 fsverity_cleanup_inode(inode);
5579 evict_inode_truncate_pages(inode);
5581 if (inode->i_nlink &&
5582 ((btrfs_root_refs(&root->root_item) != 0 &&
5583 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5584 btrfs_is_free_space_inode(BTRFS_I(inode))))
5587 if (is_bad_inode(inode))
5590 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
5592 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
5595 if (inode->i_nlink > 0) {
5596 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5597 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
5601 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
5605 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
5608 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
5611 btrfs_i_size_write(BTRFS_I(inode), 0);
5614 trans = evict_refill_and_join(root, rsv);
5618 trans->block_rsv = rsv;
5620 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
5622 trans->block_rsv = &fs_info->trans_block_rsv;
5623 btrfs_end_transaction(trans);
5624 btrfs_btree_balance_dirty(fs_info);
5625 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5632 * Errors here aren't a big deal, it just means we leave orphan items in
5633 * the tree. They will be cleaned up on the next mount. If the inode
5634 * number gets reused, cleanup deletes the orphan item without doing
5635 * anything, and unlink reuses the existing orphan item.
5637 * If it turns out that we are dropping too many of these, we might want
5638 * to add a mechanism for retrying these after a commit.
5640 trans = evict_refill_and_join(root, rsv);
5641 if (!IS_ERR(trans)) {
5642 trans->block_rsv = rsv;
5643 btrfs_orphan_del(trans, BTRFS_I(inode));
5644 trans->block_rsv = &fs_info->trans_block_rsv;
5645 btrfs_end_transaction(trans);
5649 btrfs_free_block_rsv(fs_info, rsv);
5652 * If we didn't successfully delete, the orphan item will still be in
5653 * the tree and we'll retry on the next mount. Again, we might also want
5654 * to retry these periodically in the future.
5656 btrfs_remove_delayed_node(BTRFS_I(inode));
5657 fsverity_cleanup_inode(inode);
5662 * Return the key found in the dir entry in the location pointer, fill @type
5663 * with BTRFS_FT_*, and return 0.
5665 * If no dir entries were found, returns -ENOENT.
5666 * If found a corrupted location in dir entry, returns -EUCLEAN.
5668 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5669 struct btrfs_key *location, u8 *type)
5671 const char *name = dentry->d_name.name;
5672 int namelen = dentry->d_name.len;
5673 struct btrfs_dir_item *di;
5674 struct btrfs_path *path;
5675 struct btrfs_root *root = BTRFS_I(dir)->root;
5678 path = btrfs_alloc_path();
5682 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5684 if (IS_ERR_OR_NULL(di)) {
5685 ret = di ? PTR_ERR(di) : -ENOENT;
5689 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
5690 if (location->type != BTRFS_INODE_ITEM_KEY &&
5691 location->type != BTRFS_ROOT_ITEM_KEY) {
5693 btrfs_warn(root->fs_info,
5694 "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5695 __func__, name, btrfs_ino(BTRFS_I(dir)),
5696 location->objectid, location->type, location->offset);
5699 *type = btrfs_dir_type(path->nodes[0], di);
5701 btrfs_free_path(path);
5706 * when we hit a tree root in a directory, the btrfs part of the inode
5707 * needs to be changed to reflect the root directory of the tree root. This
5708 * is kind of like crossing a mount point.
5710 static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
5712 struct dentry *dentry,
5713 struct btrfs_key *location,
5714 struct btrfs_root **sub_root)
5716 struct btrfs_path *path;
5717 struct btrfs_root *new_root;
5718 struct btrfs_root_ref *ref;
5719 struct extent_buffer *leaf;
5720 struct btrfs_key key;
5724 path = btrfs_alloc_path();
5731 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5732 key.type = BTRFS_ROOT_REF_KEY;
5733 key.offset = location->objectid;
5735 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
5742 leaf = path->nodes[0];
5743 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
5744 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
5745 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5748 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5749 (unsigned long)(ref + 1),
5750 dentry->d_name.len);
5754 btrfs_release_path(path);
5756 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
5757 if (IS_ERR(new_root)) {
5758 err = PTR_ERR(new_root);
5762 *sub_root = new_root;
5763 location->objectid = btrfs_root_dirid(&new_root->root_item);
5764 location->type = BTRFS_INODE_ITEM_KEY;
5765 location->offset = 0;
5768 btrfs_free_path(path);
5772 static void inode_tree_add(struct inode *inode)
5774 struct btrfs_root *root = BTRFS_I(inode)->root;
5775 struct btrfs_inode *entry;
5777 struct rb_node *parent;
5778 struct rb_node *new = &BTRFS_I(inode)->rb_node;
5779 u64 ino = btrfs_ino(BTRFS_I(inode));
5781 if (inode_unhashed(inode))
5784 spin_lock(&root->inode_lock);
5785 p = &root->inode_tree.rb_node;
5788 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5790 if (ino < btrfs_ino(entry))
5791 p = &parent->rb_left;
5792 else if (ino > btrfs_ino(entry))
5793 p = &parent->rb_right;
5795 WARN_ON(!(entry->vfs_inode.i_state &
5796 (I_WILL_FREE | I_FREEING)));
5797 rb_replace_node(parent, new, &root->inode_tree);
5798 RB_CLEAR_NODE(parent);
5799 spin_unlock(&root->inode_lock);
5803 rb_link_node(new, parent, p);
5804 rb_insert_color(new, &root->inode_tree);
5805 spin_unlock(&root->inode_lock);
5808 static void inode_tree_del(struct btrfs_inode *inode)
5810 struct btrfs_root *root = inode->root;
5813 spin_lock(&root->inode_lock);
5814 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5815 rb_erase(&inode->rb_node, &root->inode_tree);
5816 RB_CLEAR_NODE(&inode->rb_node);
5817 empty = RB_EMPTY_ROOT(&root->inode_tree);
5819 spin_unlock(&root->inode_lock);
5821 if (empty && btrfs_root_refs(&root->root_item) == 0) {
5822 spin_lock(&root->inode_lock);
5823 empty = RB_EMPTY_ROOT(&root->inode_tree);
5824 spin_unlock(&root->inode_lock);
5826 btrfs_add_dead_root(root);
5831 static int btrfs_init_locked_inode(struct inode *inode, void *p)
5833 struct btrfs_iget_args *args = p;
5835 inode->i_ino = args->ino;
5836 BTRFS_I(inode)->location.objectid = args->ino;
5837 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5838 BTRFS_I(inode)->location.offset = 0;
5839 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5840 BUG_ON(args->root && !BTRFS_I(inode)->root);
5844 static int btrfs_find_actor(struct inode *inode, void *opaque)
5846 struct btrfs_iget_args *args = opaque;
5848 return args->ino == BTRFS_I(inode)->location.objectid &&
5849 args->root == BTRFS_I(inode)->root;
5852 static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
5853 struct btrfs_root *root)
5855 struct inode *inode;
5856 struct btrfs_iget_args args;
5857 unsigned long hashval = btrfs_inode_hash(ino, root);
5862 inode = iget5_locked(s, hashval, btrfs_find_actor,
5863 btrfs_init_locked_inode,
5869 * Get an inode object given its inode number and corresponding root.
5870 * Path can be preallocated to prevent recursing back to iget through
5871 * allocator. NULL is also valid but may require an additional allocation
5874 struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
5875 struct btrfs_root *root, struct btrfs_path *path)
5877 struct inode *inode;
5879 inode = btrfs_iget_locked(s, ino, root);
5881 return ERR_PTR(-ENOMEM);
5883 if (inode->i_state & I_NEW) {
5886 ret = btrfs_read_locked_inode(inode, path);
5888 inode_tree_add(inode);
5889 unlock_new_inode(inode);
5893 * ret > 0 can come from btrfs_search_slot called by
5894 * btrfs_read_locked_inode, this means the inode item
5899 inode = ERR_PTR(ret);
5906 struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
5908 return btrfs_iget_path(s, ino, root, NULL);
5911 static struct inode *new_simple_dir(struct super_block *s,
5912 struct btrfs_key *key,
5913 struct btrfs_root *root)
5915 struct inode *inode = new_inode(s);
5918 return ERR_PTR(-ENOMEM);
5920 BTRFS_I(inode)->root = btrfs_grab_root(root);
5921 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
5922 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
5924 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
5926 * We only need lookup, the rest is read-only and there's no inode
5927 * associated with the dentry
5929 inode->i_op = &simple_dir_inode_operations;
5930 inode->i_opflags &= ~IOP_XATTR;
5931 inode->i_fop = &simple_dir_operations;
5932 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5933 inode->i_mtime = current_time(inode);
5934 inode->i_atime = inode->i_mtime;
5935 inode->i_ctime = inode->i_mtime;
5936 BTRFS_I(inode)->i_otime = inode->i_mtime;
5941 static inline u8 btrfs_inode_type(struct inode *inode)
5944 * Compile-time asserts that generic FT_* types still match
5947 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5948 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5949 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5950 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5951 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5952 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5953 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5954 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5956 return fs_umode_to_ftype(inode->i_mode);
5959 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5961 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
5962 struct inode *inode;
5963 struct btrfs_root *root = BTRFS_I(dir)->root;
5964 struct btrfs_root *sub_root = root;
5965 struct btrfs_key location;
5969 if (dentry->d_name.len > BTRFS_NAME_LEN)
5970 return ERR_PTR(-ENAMETOOLONG);
5972 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
5974 return ERR_PTR(ret);
5976 if (location.type == BTRFS_INODE_ITEM_KEY) {
5977 inode = btrfs_iget(dir->i_sb, location.objectid, root);
5981 /* Do extra check against inode mode with di_type */
5982 if (btrfs_inode_type(inode) != di_type) {
5984 "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5985 inode->i_mode, btrfs_inode_type(inode),
5988 return ERR_PTR(-EUCLEAN);
5993 ret = fixup_tree_root_location(fs_info, dir, dentry,
5994 &location, &sub_root);
5997 inode = ERR_PTR(ret);
5999 inode = new_simple_dir(dir->i_sb, &location, sub_root);
6001 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
6003 if (root != sub_root)
6004 btrfs_put_root(sub_root);
6006 if (!IS_ERR(inode) && root != sub_root) {
6007 down_read(&fs_info->cleanup_work_sem);
6008 if (!sb_rdonly(inode->i_sb))
6009 ret = btrfs_orphan_cleanup(sub_root);
6010 up_read(&fs_info->cleanup_work_sem);
6013 inode = ERR_PTR(ret);
6020 static int btrfs_dentry_delete(const struct dentry *dentry)
6022 struct btrfs_root *root;
6023 struct inode *inode = d_inode(dentry);
6025 if (!inode && !IS_ROOT(dentry))
6026 inode = d_inode(dentry->d_parent);
6029 root = BTRFS_I(inode)->root;
6030 if (btrfs_root_refs(&root->root_item) == 0)
6033 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
6039 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
6042 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
6044 if (inode == ERR_PTR(-ENOENT))
6046 return d_splice_alias(inode, dentry);
6050 * All this infrastructure exists because dir_emit can fault, and we are holding
6051 * the tree lock when doing readdir. For now just allocate a buffer and copy
6052 * our information into that, and then dir_emit from the buffer. This is
6053 * similar to what NFS does, only we don't keep the buffer around in pagecache
6054 * because I'm afraid I'll mess that up. Long term we need to make filldir do
6055 * copy_to_user_inatomic so we don't have to worry about page faulting under the
6058 static int btrfs_opendir(struct inode *inode, struct file *file)
6060 struct btrfs_file_private *private;
6062 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
6065 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
6066 if (!private->filldir_buf) {
6070 file->private_data = private;
6081 static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
6084 struct dir_entry *entry = addr;
6085 char *name = (char *)(entry + 1);
6087 ctx->pos = get_unaligned(&entry->offset);
6088 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
6089 get_unaligned(&entry->ino),
6090 get_unaligned(&entry->type)))
6092 addr += sizeof(struct dir_entry) +
6093 get_unaligned(&entry->name_len);
6099 static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
6101 struct inode *inode = file_inode(file);
6102 struct btrfs_root *root = BTRFS_I(inode)->root;
6103 struct btrfs_file_private *private = file->private_data;
6104 struct btrfs_dir_item *di;
6105 struct btrfs_key key;
6106 struct btrfs_key found_key;
6107 struct btrfs_path *path;
6109 struct list_head ins_list;
6110 struct list_head del_list;
6112 struct extent_buffer *leaf;
6119 struct btrfs_key location;
6121 if (!dir_emit_dots(file, ctx))
6124 path = btrfs_alloc_path();
6128 addr = private->filldir_buf;
6129 path->reada = READA_FORWARD;
6131 INIT_LIST_HEAD(&ins_list);
6132 INIT_LIST_HEAD(&del_list);
6133 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
6136 key.type = BTRFS_DIR_INDEX_KEY;
6137 key.offset = ctx->pos;
6138 key.objectid = btrfs_ino(BTRFS_I(inode));
6140 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6145 struct dir_entry *entry;
6147 leaf = path->nodes[0];
6148 slot = path->slots[0];
6149 if (slot >= btrfs_header_nritems(leaf)) {
6150 ret = btrfs_next_leaf(root, path);
6158 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6160 if (found_key.objectid != key.objectid)
6162 if (found_key.type != BTRFS_DIR_INDEX_KEY)
6164 if (found_key.offset < ctx->pos)
6166 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
6168 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
6169 name_len = btrfs_dir_name_len(leaf, di);
6170 if ((total_len + sizeof(struct dir_entry) + name_len) >=
6172 btrfs_release_path(path);
6173 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6176 addr = private->filldir_buf;
6183 put_unaligned(name_len, &entry->name_len);
6184 name_ptr = (char *)(entry + 1);
6185 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
6187 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
6189 btrfs_dir_item_key_to_cpu(leaf, di, &location);
6190 put_unaligned(location.objectid, &entry->ino);
6191 put_unaligned(found_key.offset, &entry->offset);
6193 addr += sizeof(struct dir_entry) + name_len;
6194 total_len += sizeof(struct dir_entry) + name_len;
6198 btrfs_release_path(path);
6200 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
6204 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
6209 * Stop new entries from being returned after we return the last
6212 * New directory entries are assigned a strictly increasing
6213 * offset. This means that new entries created during readdir
6214 * are *guaranteed* to be seen in the future by that readdir.
6215 * This has broken buggy programs which operate on names as
6216 * they're returned by readdir. Until we re-use freed offsets
6217 * we have this hack to stop new entries from being returned
6218 * under the assumption that they'll never reach this huge
6221 * This is being careful not to overflow 32bit loff_t unless the
6222 * last entry requires it because doing so has broken 32bit apps
6225 if (ctx->pos >= INT_MAX)
6226 ctx->pos = LLONG_MAX;
6233 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
6234 btrfs_free_path(path);
6239 * This is somewhat expensive, updating the tree every time the
6240 * inode changes. But, it is most likely to find the inode in cache.
6241 * FIXME, needs more benchmarking...there are no reasons other than performance
6242 * to keep or drop this code.
6244 static int btrfs_dirty_inode(struct inode *inode)
6246 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6247 struct btrfs_root *root = BTRFS_I(inode)->root;
6248 struct btrfs_trans_handle *trans;
6251 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
6254 trans = btrfs_join_transaction(root);
6256 return PTR_ERR(trans);
6258 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
6259 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
6260 /* whoops, lets try again with the full transaction */
6261 btrfs_end_transaction(trans);
6262 trans = btrfs_start_transaction(root, 1);
6264 return PTR_ERR(trans);
6266 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
6268 btrfs_end_transaction(trans);
6269 if (BTRFS_I(inode)->delayed_node)
6270 btrfs_balance_delayed_items(fs_info);
6276 * This is a copy of file_update_time. We need this so we can return error on
6277 * ENOSPC for updating the inode in the case of file write and mmap writes.
6279 static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
6282 struct btrfs_root *root = BTRFS_I(inode)->root;
6283 bool dirty = flags & ~S_VERSION;
6285 if (btrfs_root_readonly(root))
6288 if (flags & S_VERSION)
6289 dirty |= inode_maybe_inc_iversion(inode, dirty);
6290 if (flags & S_CTIME)
6291 inode->i_ctime = *now;
6292 if (flags & S_MTIME)
6293 inode->i_mtime = *now;
6294 if (flags & S_ATIME)
6295 inode->i_atime = *now;
6296 return dirty ? btrfs_dirty_inode(inode) : 0;
6300 * find the highest existing sequence number in a directory
6301 * and then set the in-memory index_cnt variable to reflect
6302 * free sequence numbers
6304 static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
6306 struct btrfs_root *root = inode->root;
6307 struct btrfs_key key, found_key;
6308 struct btrfs_path *path;
6309 struct extent_buffer *leaf;
6312 key.objectid = btrfs_ino(inode);
6313 key.type = BTRFS_DIR_INDEX_KEY;
6314 key.offset = (u64)-1;
6316 path = btrfs_alloc_path();
6320 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6323 /* FIXME: we should be able to handle this */
6329 * MAGIC NUMBER EXPLANATION:
6330 * since we search a directory based on f_pos we have to start at 2
6331 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6332 * else has to start at 2
6334 if (path->slots[0] == 0) {
6335 inode->index_cnt = 2;
6341 leaf = path->nodes[0];
6342 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6344 if (found_key.objectid != btrfs_ino(inode) ||
6345 found_key.type != BTRFS_DIR_INDEX_KEY) {
6346 inode->index_cnt = 2;
6350 inode->index_cnt = found_key.offset + 1;
6352 btrfs_free_path(path);
6357 * helper to find a free sequence number in a given directory. This current
6358 * code is very simple, later versions will do smarter things in the btree
6360 int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
6364 if (dir->index_cnt == (u64)-1) {
6365 ret = btrfs_inode_delayed_dir_index_count(dir);
6367 ret = btrfs_set_inode_index_count(dir);
6373 *index = dir->index_cnt;
6379 static int btrfs_insert_inode_locked(struct inode *inode)
6381 struct btrfs_iget_args args;
6383 args.ino = BTRFS_I(inode)->location.objectid;
6384 args.root = BTRFS_I(inode)->root;
6386 return insert_inode_locked4(inode,
6387 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6388 btrfs_find_actor, &args);
6392 * Inherit flags from the parent inode.
6394 * Currently only the compression flags and the cow flags are inherited.
6396 static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6403 flags = BTRFS_I(dir)->flags;
6405 if (flags & BTRFS_INODE_NOCOMPRESS) {
6406 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6407 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6408 } else if (flags & BTRFS_INODE_COMPRESS) {
6409 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6410 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6413 if (flags & BTRFS_INODE_NODATACOW) {
6414 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6415 if (S_ISREG(inode->i_mode))
6416 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6419 btrfs_sync_inode_flags_to_i_flags(inode);
6422 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6423 struct btrfs_root *root,
6424 struct user_namespace *mnt_userns,
6426 const char *name, int name_len,
6427 u64 ref_objectid, u64 objectid,
6428 umode_t mode, u64 *index)
6430 struct btrfs_fs_info *fs_info = root->fs_info;
6431 struct inode *inode;
6432 struct btrfs_inode_item *inode_item;
6433 struct btrfs_key *location;
6434 struct btrfs_path *path;
6435 struct btrfs_inode_ref *ref;
6436 struct btrfs_key key[2];
6438 int nitems = name ? 2 : 1;
6440 unsigned int nofs_flag;
6443 path = btrfs_alloc_path();
6445 return ERR_PTR(-ENOMEM);
6447 nofs_flag = memalloc_nofs_save();
6448 inode = new_inode(fs_info->sb);
6449 memalloc_nofs_restore(nofs_flag);
6451 btrfs_free_path(path);
6452 return ERR_PTR(-ENOMEM);
6456 * O_TMPFILE, set link count to 0, so that after this point,
6457 * we fill in an inode item with the correct link count.
6460 set_nlink(inode, 0);
6463 * we have to initialize this early, so we can reclaim the inode
6464 * number if we fail afterwards in this function.
6466 inode->i_ino = objectid;
6469 trace_btrfs_inode_request(dir);
6471 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
6473 btrfs_free_path(path);
6475 return ERR_PTR(ret);
6481 * index_cnt is ignored for everything but a dir,
6482 * btrfs_set_inode_index_count has an explanation for the magic
6485 BTRFS_I(inode)->index_cnt = 2;
6486 BTRFS_I(inode)->dir_index = *index;
6487 BTRFS_I(inode)->root = btrfs_grab_root(root);
6488 BTRFS_I(inode)->generation = trans->transid;
6489 inode->i_generation = BTRFS_I(inode)->generation;
6492 * We could have gotten an inode number from somebody who was fsynced
6493 * and then removed in this same transaction, so let's just set full
6494 * sync since it will be a full sync anyway and this will blow away the
6495 * old info in the log.
6497 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6499 key[0].objectid = objectid;
6500 key[0].type = BTRFS_INODE_ITEM_KEY;
6503 sizes[0] = sizeof(struct btrfs_inode_item);
6507 * Start new inodes with an inode_ref. This is slightly more
6508 * efficient for small numbers of hard links since they will
6509 * be packed into one item. Extended refs will kick in if we
6510 * add more hard links than can fit in the ref item.
6512 key[1].objectid = objectid;
6513 key[1].type = BTRFS_INODE_REF_KEY;
6514 key[1].offset = ref_objectid;
6516 sizes[1] = name_len + sizeof(*ref);
6519 location = &BTRFS_I(inode)->location;
6520 location->objectid = objectid;
6521 location->offset = 0;
6522 location->type = BTRFS_INODE_ITEM_KEY;
6524 ret = btrfs_insert_inode_locked(inode);
6530 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
6534 inode_init_owner(mnt_userns, inode, dir, mode);
6535 inode_set_bytes(inode, 0);
6537 inode->i_mtime = current_time(inode);
6538 inode->i_atime = inode->i_mtime;
6539 inode->i_ctime = inode->i_mtime;
6540 BTRFS_I(inode)->i_otime = inode->i_mtime;
6542 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6543 struct btrfs_inode_item);
6544 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
6545 sizeof(*inode_item));
6546 fill_inode_item(trans, path->nodes[0], inode_item, inode);
6549 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6550 struct btrfs_inode_ref);
6551 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6552 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6553 ptr = (unsigned long)(ref + 1);
6554 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6557 btrfs_mark_buffer_dirty(path->nodes[0]);
6558 btrfs_free_path(path);
6560 btrfs_inherit_iflags(inode, dir);
6562 if (S_ISREG(mode)) {
6563 if (btrfs_test_opt(fs_info, NODATASUM))
6564 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6565 if (btrfs_test_opt(fs_info, NODATACOW))
6566 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6567 BTRFS_INODE_NODATASUM;
6570 inode_tree_add(inode);
6572 trace_btrfs_inode_new(inode);
6573 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
6575 btrfs_update_root_times(trans, root);
6577 ret = btrfs_inode_inherit_props(trans, inode, dir);
6580 "error inheriting props for ino %llu (root %llu): %d",
6581 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
6586 discard_new_inode(inode);
6589 BTRFS_I(dir)->index_cnt--;
6590 btrfs_free_path(path);
6591 return ERR_PTR(ret);
6595 * utility function to add 'inode' into 'parent_inode' with
6596 * a give name and a given sequence number.
6597 * if 'add_backref' is true, also insert a backref from the
6598 * inode to the parent directory.
6600 int btrfs_add_link(struct btrfs_trans_handle *trans,
6601 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
6602 const char *name, int name_len, int add_backref, u64 index)
6605 struct btrfs_key key;
6606 struct btrfs_root *root = parent_inode->root;
6607 u64 ino = btrfs_ino(inode);
6608 u64 parent_ino = btrfs_ino(parent_inode);
6610 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6611 memcpy(&key, &inode->root->root_key, sizeof(key));
6614 key.type = BTRFS_INODE_ITEM_KEY;
6618 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6619 ret = btrfs_add_root_ref(trans, key.objectid,
6620 root->root_key.objectid, parent_ino,
6621 index, name, name_len);
6622 } else if (add_backref) {
6623 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6627 /* Nothing to clean up yet */
6631 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
6632 btrfs_inode_type(&inode->vfs_inode), index);
6633 if (ret == -EEXIST || ret == -EOVERFLOW)
6636 btrfs_abort_transaction(trans, ret);
6640 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
6642 inode_inc_iversion(&parent_inode->vfs_inode);
6644 * If we are replaying a log tree, we do not want to update the mtime
6645 * and ctime of the parent directory with the current time, since the
6646 * log replay procedure is responsible for setting them to their correct
6647 * values (the ones it had when the fsync was done).
6649 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6650 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6652 parent_inode->vfs_inode.i_mtime = now;
6653 parent_inode->vfs_inode.i_ctime = now;
6655 ret = btrfs_update_inode(trans, root, parent_inode);
6657 btrfs_abort_transaction(trans, ret);
6661 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6664 err = btrfs_del_root_ref(trans, key.objectid,
6665 root->root_key.objectid, parent_ino,
6666 &local_index, name, name_len);
6668 btrfs_abort_transaction(trans, err);
6669 } else if (add_backref) {
6673 err = btrfs_del_inode_ref(trans, root, name, name_len,
6674 ino, parent_ino, &local_index);
6676 btrfs_abort_transaction(trans, err);
6679 /* Return the original error code */
6683 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
6684 struct btrfs_inode *dir, struct dentry *dentry,
6685 struct btrfs_inode *inode, int backref, u64 index)
6687 int err = btrfs_add_link(trans, dir, inode,
6688 dentry->d_name.name, dentry->d_name.len,
6695 static int btrfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
6696 struct dentry *dentry, umode_t mode, dev_t rdev)
6698 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6699 struct btrfs_trans_handle *trans;
6700 struct btrfs_root *root = BTRFS_I(dir)->root;
6701 struct inode *inode = NULL;
6707 * 2 for inode item and ref
6709 * 1 for xattr if selinux is on
6711 trans = btrfs_start_transaction(root, 5);
6713 return PTR_ERR(trans);
6715 err = btrfs_get_free_objectid(root, &objectid);
6719 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
6720 dentry->d_name.name, dentry->d_name.len,
6721 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
6722 if (IS_ERR(inode)) {
6723 err = PTR_ERR(inode);
6729 * If the active LSM wants to access the inode during
6730 * d_instantiate it needs these. Smack checks to see
6731 * if the filesystem supports xattrs by looking at the
6734 inode->i_op = &btrfs_special_inode_operations;
6735 init_special_inode(inode, inode->i_mode, rdev);
6737 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6741 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6746 btrfs_update_inode(trans, root, BTRFS_I(inode));
6747 d_instantiate_new(dentry, inode);
6750 btrfs_end_transaction(trans);
6751 btrfs_btree_balance_dirty(fs_info);
6753 inode_dec_link_count(inode);
6754 discard_new_inode(inode);
6759 static int btrfs_create(struct user_namespace *mnt_userns, struct inode *dir,
6760 struct dentry *dentry, umode_t mode, bool excl)
6762 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6763 struct btrfs_trans_handle *trans;
6764 struct btrfs_root *root = BTRFS_I(dir)->root;
6765 struct inode *inode = NULL;
6771 * 2 for inode item and ref
6773 * 1 for xattr if selinux is on
6775 trans = btrfs_start_transaction(root, 5);
6777 return PTR_ERR(trans);
6779 err = btrfs_get_free_objectid(root, &objectid);
6783 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
6784 dentry->d_name.name, dentry->d_name.len,
6785 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
6786 if (IS_ERR(inode)) {
6787 err = PTR_ERR(inode);
6792 * If the active LSM wants to access the inode during
6793 * d_instantiate it needs these. Smack checks to see
6794 * if the filesystem supports xattrs by looking at the
6797 inode->i_fop = &btrfs_file_operations;
6798 inode->i_op = &btrfs_file_inode_operations;
6799 inode->i_mapping->a_ops = &btrfs_aops;
6801 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6805 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
6809 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6814 d_instantiate_new(dentry, inode);
6817 btrfs_end_transaction(trans);
6819 inode_dec_link_count(inode);
6820 discard_new_inode(inode);
6822 btrfs_btree_balance_dirty(fs_info);
6826 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6827 struct dentry *dentry)
6829 struct btrfs_trans_handle *trans = NULL;
6830 struct btrfs_root *root = BTRFS_I(dir)->root;
6831 struct inode *inode = d_inode(old_dentry);
6832 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6837 /* do not allow sys_link's with other subvols of the same device */
6838 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
6841 if (inode->i_nlink >= BTRFS_LINK_MAX)
6844 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
6849 * 2 items for inode and inode ref
6850 * 2 items for dir items
6851 * 1 item for parent inode
6852 * 1 item for orphan item deletion if O_TMPFILE
6854 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
6855 if (IS_ERR(trans)) {
6856 err = PTR_ERR(trans);
6861 /* There are several dir indexes for this inode, clear the cache. */
6862 BTRFS_I(inode)->dir_index = 0ULL;
6864 inode_inc_iversion(inode);
6865 inode->i_ctime = current_time(inode);
6867 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
6869 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6875 struct dentry *parent = dentry->d_parent;
6877 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
6880 if (inode->i_nlink == 1) {
6882 * If new hard link count is 1, it's a file created
6883 * with open(2) O_TMPFILE flag.
6885 err = btrfs_orphan_del(trans, BTRFS_I(inode));
6889 d_instantiate(dentry, inode);
6890 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
6895 btrfs_end_transaction(trans);
6897 inode_dec_link_count(inode);
6900 btrfs_btree_balance_dirty(fs_info);
6904 static int btrfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
6905 struct dentry *dentry, umode_t mode)
6907 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6908 struct inode *inode = NULL;
6909 struct btrfs_trans_handle *trans;
6910 struct btrfs_root *root = BTRFS_I(dir)->root;
6916 * 2 items for inode and ref
6917 * 2 items for dir items
6918 * 1 for xattr if selinux is on
6920 trans = btrfs_start_transaction(root, 5);
6922 return PTR_ERR(trans);
6924 err = btrfs_get_free_objectid(root, &objectid);
6928 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
6929 dentry->d_name.name, dentry->d_name.len,
6930 btrfs_ino(BTRFS_I(dir)), objectid,
6931 S_IFDIR | mode, &index);
6932 if (IS_ERR(inode)) {
6933 err = PTR_ERR(inode);
6938 /* these must be set before we unlock the inode */
6939 inode->i_op = &btrfs_dir_inode_operations;
6940 inode->i_fop = &btrfs_dir_file_operations;
6942 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6946 btrfs_i_size_write(BTRFS_I(inode), 0);
6947 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
6951 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6952 dentry->d_name.name,
6953 dentry->d_name.len, 0, index);
6957 d_instantiate_new(dentry, inode);
6960 btrfs_end_transaction(trans);
6962 inode_dec_link_count(inode);
6963 discard_new_inode(inode);
6965 btrfs_btree_balance_dirty(fs_info);
6969 static noinline int uncompress_inline(struct btrfs_path *path,
6971 size_t pg_offset, u64 extent_offset,
6972 struct btrfs_file_extent_item *item)
6975 struct extent_buffer *leaf = path->nodes[0];
6978 unsigned long inline_size;
6982 WARN_ON(pg_offset != 0);
6983 compress_type = btrfs_file_extent_compression(leaf, item);
6984 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6985 inline_size = btrfs_file_extent_inline_item_len(leaf,
6986 btrfs_item_nr(path->slots[0]));
6987 tmp = kmalloc(inline_size, GFP_NOFS);
6990 ptr = btrfs_file_extent_inline_start(item);
6992 read_extent_buffer(leaf, tmp, ptr, inline_size);
6994 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
6995 ret = btrfs_decompress(compress_type, tmp, page,
6996 extent_offset, inline_size, max_size);
6999 * decompression code contains a memset to fill in any space between the end
7000 * of the uncompressed data and the end of max_size in case the decompressed
7001 * data ends up shorter than ram_bytes. That doesn't cover the hole between
7002 * the end of an inline extent and the beginning of the next block, so we
7003 * cover that region here.
7006 if (max_size + pg_offset < PAGE_SIZE)
7007 memzero_page(page, pg_offset + max_size,
7008 PAGE_SIZE - max_size - pg_offset);
7014 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
7015 * @inode: file to search in
7016 * @page: page to read extent data into if the extent is inline
7017 * @pg_offset: offset into @page to copy to
7018 * @start: file offset
7019 * @len: length of range starting at @start
7021 * This returns the first &struct extent_map which overlaps with the given
7022 * range, reading it from the B-tree and caching it if necessary. Note that
7023 * there may be more extents which overlap the given range after the returned
7026 * If @page is not NULL and the extent is inline, this also reads the extent
7027 * data directly into the page and marks the extent up to date in the io_tree.
7029 * Return: ERR_PTR on error, non-NULL extent_map on success.
7031 struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
7032 struct page *page, size_t pg_offset,
7035 struct btrfs_fs_info *fs_info = inode->root->fs_info;
7037 u64 extent_start = 0;
7039 u64 objectid = btrfs_ino(inode);
7040 int extent_type = -1;
7041 struct btrfs_path *path = NULL;
7042 struct btrfs_root *root = inode->root;
7043 struct btrfs_file_extent_item *item;
7044 struct extent_buffer *leaf;
7045 struct btrfs_key found_key;
7046 struct extent_map *em = NULL;
7047 struct extent_map_tree *em_tree = &inode->extent_tree;
7048 struct extent_io_tree *io_tree = &inode->io_tree;
7050 read_lock(&em_tree->lock);
7051 em = lookup_extent_mapping(em_tree, start, len);
7052 read_unlock(&em_tree->lock);
7055 if (em->start > start || em->start + em->len <= start)
7056 free_extent_map(em);
7057 else if (em->block_start == EXTENT_MAP_INLINE && page)
7058 free_extent_map(em);
7062 em = alloc_extent_map();
7067 em->start = EXTENT_MAP_HOLE;
7068 em->orig_start = EXTENT_MAP_HOLE;
7070 em->block_len = (u64)-1;
7072 path = btrfs_alloc_path();
7078 /* Chances are we'll be called again, so go ahead and do readahead */
7079 path->reada = READA_FORWARD;
7082 * The same explanation in load_free_space_cache applies here as well,
7083 * we only read when we're loading the free space cache, and at that
7084 * point the commit_root has everything we need.
7086 if (btrfs_is_free_space_inode(inode)) {
7087 path->search_commit_root = 1;
7088 path->skip_locking = 1;
7091 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
7094 } else if (ret > 0) {
7095 if (path->slots[0] == 0)
7101 leaf = path->nodes[0];
7102 item = btrfs_item_ptr(leaf, path->slots[0],
7103 struct btrfs_file_extent_item);
7104 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7105 if (found_key.objectid != objectid ||
7106 found_key.type != BTRFS_EXTENT_DATA_KEY) {
7108 * If we backup past the first extent we want to move forward
7109 * and see if there is an extent in front of us, otherwise we'll
7110 * say there is a hole for our whole search range which can
7117 extent_type = btrfs_file_extent_type(leaf, item);
7118 extent_start = found_key.offset;
7119 extent_end = btrfs_file_extent_end(path);
7120 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7121 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
7122 /* Only regular file could have regular/prealloc extent */
7123 if (!S_ISREG(inode->vfs_inode.i_mode)) {
7126 "regular/prealloc extent found for non-regular inode %llu",
7130 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
7132 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
7133 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
7138 if (start >= extent_end) {
7140 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
7141 ret = btrfs_next_leaf(root, path);
7147 leaf = path->nodes[0];
7149 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7150 if (found_key.objectid != objectid ||
7151 found_key.type != BTRFS_EXTENT_DATA_KEY)
7153 if (start + len <= found_key.offset)
7155 if (start > found_key.offset)
7158 /* New extent overlaps with existing one */
7160 em->orig_start = start;
7161 em->len = found_key.offset - start;
7162 em->block_start = EXTENT_MAP_HOLE;
7166 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
7168 if (extent_type == BTRFS_FILE_EXTENT_REG ||
7169 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
7171 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
7175 size_t extent_offset;
7181 size = btrfs_file_extent_ram_bytes(leaf, item);
7182 extent_offset = page_offset(page) + pg_offset - extent_start;
7183 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
7184 size - extent_offset);
7185 em->start = extent_start + extent_offset;
7186 em->len = ALIGN(copy_size, fs_info->sectorsize);
7187 em->orig_block_len = em->len;
7188 em->orig_start = em->start;
7189 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
7191 if (!PageUptodate(page)) {
7192 if (btrfs_file_extent_compression(leaf, item) !=
7193 BTRFS_COMPRESS_NONE) {
7194 ret = uncompress_inline(path, page, pg_offset,
7195 extent_offset, item);
7199 map = kmap_local_page(page);
7200 read_extent_buffer(leaf, map + pg_offset, ptr,
7202 if (pg_offset + copy_size < PAGE_SIZE) {
7203 memset(map + pg_offset + copy_size, 0,
7204 PAGE_SIZE - pg_offset -
7209 flush_dcache_page(page);
7211 set_extent_uptodate(io_tree, em->start,
7212 extent_map_end(em) - 1, NULL, GFP_NOFS);
7217 em->orig_start = start;
7219 em->block_start = EXTENT_MAP_HOLE;
7222 btrfs_release_path(path);
7223 if (em->start > start || extent_map_end(em) <= start) {
7225 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7226 em->start, em->len, start, len);
7231 write_lock(&em_tree->lock);
7232 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
7233 write_unlock(&em_tree->lock);
7235 btrfs_free_path(path);
7237 trace_btrfs_get_extent(root, inode, em);
7240 free_extent_map(em);
7241 return ERR_PTR(ret);
7246 struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
7249 struct extent_map *em;
7250 struct extent_map *hole_em = NULL;
7251 u64 delalloc_start = start;
7257 em = btrfs_get_extent(inode, NULL, 0, start, len);
7261 * If our em maps to:
7263 * - a pre-alloc extent,
7264 * there might actually be delalloc bytes behind it.
7266 if (em->block_start != EXTENT_MAP_HOLE &&
7267 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7272 /* check to see if we've wrapped (len == -1 or similar) */
7281 /* ok, we didn't find anything, lets look for delalloc */
7282 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
7283 end, len, EXTENT_DELALLOC, 1);
7284 delalloc_end = delalloc_start + delalloc_len;
7285 if (delalloc_end < delalloc_start)
7286 delalloc_end = (u64)-1;
7289 * We didn't find anything useful, return the original results from
7292 if (delalloc_start > end || delalloc_end <= start) {
7299 * Adjust the delalloc_start to make sure it doesn't go backwards from
7300 * the start they passed in
7302 delalloc_start = max(start, delalloc_start);
7303 delalloc_len = delalloc_end - delalloc_start;
7305 if (delalloc_len > 0) {
7308 const u64 hole_end = extent_map_end(hole_em);
7310 em = alloc_extent_map();
7318 * When btrfs_get_extent can't find anything it returns one
7321 * Make sure what it found really fits our range, and adjust to
7322 * make sure it is based on the start from the caller
7324 if (hole_end <= start || hole_em->start > end) {
7325 free_extent_map(hole_em);
7328 hole_start = max(hole_em->start, start);
7329 hole_len = hole_end - hole_start;
7332 if (hole_em && delalloc_start > hole_start) {
7334 * Our hole starts before our delalloc, so we have to
7335 * return just the parts of the hole that go until the
7338 em->len = min(hole_len, delalloc_start - hole_start);
7339 em->start = hole_start;
7340 em->orig_start = hole_start;
7342 * Don't adjust block start at all, it is fixed at
7345 em->block_start = hole_em->block_start;
7346 em->block_len = hole_len;
7347 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7348 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7351 * Hole is out of passed range or it starts after
7354 em->start = delalloc_start;
7355 em->len = delalloc_len;
7356 em->orig_start = delalloc_start;
7357 em->block_start = EXTENT_MAP_DELALLOC;
7358 em->block_len = delalloc_len;
7365 free_extent_map(hole_em);
7367 free_extent_map(em);
7368 return ERR_PTR(err);
7373 static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
7376 const u64 orig_start,
7377 const u64 block_start,
7378 const u64 block_len,
7379 const u64 orig_block_len,
7380 const u64 ram_bytes,
7383 struct extent_map *em = NULL;
7386 if (type != BTRFS_ORDERED_NOCOW) {
7387 em = create_io_em(inode, start, len, orig_start, block_start,
7388 block_len, orig_block_len, ram_bytes,
7389 BTRFS_COMPRESS_NONE, /* compress_type */
7394 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7398 free_extent_map(em);
7399 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
7408 static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
7411 struct btrfs_root *root = inode->root;
7412 struct btrfs_fs_info *fs_info = root->fs_info;
7413 struct extent_map *em;
7414 struct btrfs_key ins;
7418 alloc_hint = get_extent_allocation_hint(inode, start, len);
7419 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
7420 0, alloc_hint, &ins, 1, 1);
7422 return ERR_PTR(ret);
7424 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7425 ins.objectid, ins.offset, ins.offset,
7426 ins.offset, BTRFS_ORDERED_REGULAR);
7427 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
7429 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7435 static bool btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
7437 struct btrfs_block_group *block_group;
7438 bool readonly = false;
7440 block_group = btrfs_lookup_block_group(fs_info, bytenr);
7441 if (!block_group || block_group->ro)
7444 btrfs_put_block_group(block_group);
7449 * Check if we can do nocow write into the range [@offset, @offset + @len)
7451 * @offset: File offset
7452 * @len: The length to write, will be updated to the nocow writeable
7454 * @orig_start: (optional) Return the original file offset of the file extent
7455 * @orig_len: (optional) Return the original on-disk length of the file extent
7456 * @ram_bytes: (optional) Return the ram_bytes of the file extent
7457 * @strict: if true, omit optimizations that might force us into unnecessary
7458 * cow. e.g., don't trust generation number.
7461 * >0 and update @len if we can do nocow write
7462 * 0 if we can't do nocow write
7463 * <0 if error happened
7465 * NOTE: This only checks the file extents, caller is responsible to wait for
7466 * any ordered extents.
7468 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7469 u64 *orig_start, u64 *orig_block_len,
7470 u64 *ram_bytes, bool strict)
7472 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7473 struct btrfs_path *path;
7475 struct extent_buffer *leaf;
7476 struct btrfs_root *root = BTRFS_I(inode)->root;
7477 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7478 struct btrfs_file_extent_item *fi;
7479 struct btrfs_key key;
7486 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
7488 path = btrfs_alloc_path();
7492 ret = btrfs_lookup_file_extent(NULL, root, path,
7493 btrfs_ino(BTRFS_I(inode)), offset, 0);
7497 slot = path->slots[0];
7500 /* can't find the item, must cow */
7507 leaf = path->nodes[0];
7508 btrfs_item_key_to_cpu(leaf, &key, slot);
7509 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
7510 key.type != BTRFS_EXTENT_DATA_KEY) {
7511 /* not our file or wrong item type, must cow */
7515 if (key.offset > offset) {
7516 /* Wrong offset, must cow */
7520 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7521 found_type = btrfs_file_extent_type(leaf, fi);
7522 if (found_type != BTRFS_FILE_EXTENT_REG &&
7523 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7524 /* not a regular extent, must cow */
7528 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7531 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7532 if (extent_end <= offset)
7535 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7536 if (disk_bytenr == 0)
7539 if (btrfs_file_extent_compression(leaf, fi) ||
7540 btrfs_file_extent_encryption(leaf, fi) ||
7541 btrfs_file_extent_other_encoding(leaf, fi))
7545 * Do the same check as in btrfs_cross_ref_exist but without the
7546 * unnecessary search.
7549 (btrfs_file_extent_generation(leaf, fi) <=
7550 btrfs_root_last_snapshot(&root->root_item)))
7553 backref_offset = btrfs_file_extent_offset(leaf, fi);
7556 *orig_start = key.offset - backref_offset;
7557 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7558 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7561 if (btrfs_extent_readonly(fs_info, disk_bytenr))
7564 num_bytes = min(offset + *len, extent_end) - offset;
7565 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7568 range_end = round_up(offset + num_bytes,
7569 root->fs_info->sectorsize) - 1;
7570 ret = test_range_bit(io_tree, offset, range_end,
7571 EXTENT_DELALLOC, 0, NULL);
7578 btrfs_release_path(path);
7581 * look for other files referencing this extent, if we
7582 * find any we must cow
7585 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
7586 key.offset - backref_offset, disk_bytenr,
7594 * adjust disk_bytenr and num_bytes to cover just the bytes
7595 * in this extent we are about to write. If there
7596 * are any csums in that range we have to cow in order
7597 * to keep the csums correct
7599 disk_bytenr += backref_offset;
7600 disk_bytenr += offset - key.offset;
7601 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7604 * all of the above have passed, it is safe to overwrite this extent
7610 btrfs_free_path(path);
7614 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7615 struct extent_state **cached_state, bool writing)
7617 struct btrfs_ordered_extent *ordered;
7621 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7624 * We're concerned with the entire range that we're going to be
7625 * doing DIO to, so we need to make sure there's no ordered
7626 * extents in this range.
7628 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
7629 lockend - lockstart + 1);
7632 * We need to make sure there are no buffered pages in this
7633 * range either, we could have raced between the invalidate in
7634 * generic_file_direct_write and locking the extent. The
7635 * invalidate needs to happen so that reads after a write do not
7639 (!writing || !filemap_range_has_page(inode->i_mapping,
7640 lockstart, lockend)))
7643 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7648 * If we are doing a DIO read and the ordered extent we
7649 * found is for a buffered write, we can not wait for it
7650 * to complete and retry, because if we do so we can
7651 * deadlock with concurrent buffered writes on page
7652 * locks. This happens only if our DIO read covers more
7653 * than one extent map, if at this point has already
7654 * created an ordered extent for a previous extent map
7655 * and locked its range in the inode's io tree, and a
7656 * concurrent write against that previous extent map's
7657 * range and this range started (we unlock the ranges
7658 * in the io tree only when the bios complete and
7659 * buffered writes always lock pages before attempting
7660 * to lock range in the io tree).
7663 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7664 btrfs_start_ordered_extent(ordered, 1);
7667 btrfs_put_ordered_extent(ordered);
7670 * We could trigger writeback for this range (and wait
7671 * for it to complete) and then invalidate the pages for
7672 * this range (through invalidate_inode_pages2_range()),
7673 * but that can lead us to a deadlock with a concurrent
7674 * call to readahead (a buffered read or a defrag call
7675 * triggered a readahead) on a page lock due to an
7676 * ordered dio extent we created before but did not have
7677 * yet a corresponding bio submitted (whence it can not
7678 * complete), which makes readahead wait for that
7679 * ordered extent to complete while holding a lock on
7694 /* The callers of this must take lock_extent() */
7695 static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7696 u64 len, u64 orig_start, u64 block_start,
7697 u64 block_len, u64 orig_block_len,
7698 u64 ram_bytes, int compress_type,
7701 struct extent_map_tree *em_tree;
7702 struct extent_map *em;
7705 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7706 type == BTRFS_ORDERED_COMPRESSED ||
7707 type == BTRFS_ORDERED_NOCOW ||
7708 type == BTRFS_ORDERED_REGULAR);
7710 em_tree = &inode->extent_tree;
7711 em = alloc_extent_map();
7713 return ERR_PTR(-ENOMEM);
7716 em->orig_start = orig_start;
7718 em->block_len = block_len;
7719 em->block_start = block_start;
7720 em->orig_block_len = orig_block_len;
7721 em->ram_bytes = ram_bytes;
7722 em->generation = -1;
7723 set_bit(EXTENT_FLAG_PINNED, &em->flags);
7724 if (type == BTRFS_ORDERED_PREALLOC) {
7725 set_bit(EXTENT_FLAG_FILLING, &em->flags);
7726 } else if (type == BTRFS_ORDERED_COMPRESSED) {
7727 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7728 em->compress_type = compress_type;
7732 btrfs_drop_extent_cache(inode, em->start,
7733 em->start + em->len - 1, 0);
7734 write_lock(&em_tree->lock);
7735 ret = add_extent_mapping(em_tree, em, 1);
7736 write_unlock(&em_tree->lock);
7738 * The caller has taken lock_extent(), who could race with us
7741 } while (ret == -EEXIST);
7744 free_extent_map(em);
7745 return ERR_PTR(ret);
7748 /* em got 2 refs now, callers needs to do free_extent_map once. */
7753 static int btrfs_get_blocks_direct_write(struct extent_map **map,
7754 struct inode *inode,
7755 struct btrfs_dio_data *dio_data,
7758 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7759 struct extent_map *em = *map;
7763 * We don't allocate a new extent in the following cases
7765 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7767 * 2) The extent is marked as PREALLOC. We're good to go here and can
7768 * just use the extent.
7771 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7772 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7773 em->block_start != EXTENT_MAP_HOLE)) {
7775 u64 block_start, orig_start, orig_block_len, ram_bytes;
7777 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7778 type = BTRFS_ORDERED_PREALLOC;
7780 type = BTRFS_ORDERED_NOCOW;
7781 len = min(len, em->len - (start - em->start));
7782 block_start = em->block_start + (start - em->start);
7784 if (can_nocow_extent(inode, start, &len, &orig_start,
7785 &orig_block_len, &ram_bytes, false) == 1 &&
7786 btrfs_inc_nocow_writers(fs_info, block_start)) {
7787 struct extent_map *em2;
7789 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
7790 orig_start, block_start,
7791 len, orig_block_len,
7793 btrfs_dec_nocow_writers(fs_info, block_start);
7794 if (type == BTRFS_ORDERED_PREALLOC) {
7795 free_extent_map(em);
7799 if (em2 && IS_ERR(em2)) {
7804 * For inode marked NODATACOW or extent marked PREALLOC,
7805 * use the existing or preallocated extent, so does not
7806 * need to adjust btrfs_space_info's bytes_may_use.
7808 btrfs_free_reserved_data_space_noquota(fs_info, len);
7813 /* this will cow the extent */
7814 free_extent_map(em);
7815 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
7821 len = min(len, em->len - (start - em->start));
7825 * Need to update the i_size under the extent lock so buffered
7826 * readers will get the updated i_size when we unlock.
7828 if (start + len > i_size_read(inode))
7829 i_size_write(inode, start + len);
7831 dio_data->reserve -= len;
7836 static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7837 loff_t length, unsigned int flags, struct iomap *iomap,
7838 struct iomap *srcmap)
7840 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7841 struct extent_map *em;
7842 struct extent_state *cached_state = NULL;
7843 struct btrfs_dio_data *dio_data = NULL;
7844 u64 lockstart, lockend;
7845 const bool write = !!(flags & IOMAP_WRITE);
7848 bool unlock_extents = false;
7851 len = min_t(u64, len, fs_info->sectorsize);
7854 lockend = start + len - 1;
7857 * The generic stuff only does filemap_write_and_wait_range, which
7858 * isn't enough if we've written compressed pages to this area, so we
7859 * need to flush the dirty pages again to make absolutely sure that any
7860 * outstanding dirty pages are on disk.
7862 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7863 &BTRFS_I(inode)->runtime_flags)) {
7864 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7865 start + length - 1);
7870 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7874 dio_data->length = length;
7876 dio_data->reserve = round_up(length, fs_info->sectorsize);
7877 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7878 &dio_data->data_reserved,
7879 start, dio_data->reserve);
7881 extent_changeset_free(dio_data->data_reserved);
7886 iomap->private = dio_data;
7890 * If this errors out it's because we couldn't invalidate pagecache for
7891 * this range and we need to fallback to buffered.
7893 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
7898 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
7905 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7906 * io. INLINE is special, and we could probably kludge it in here, but
7907 * it's still buffered so for safety lets just fall back to the generic
7910 * For COMPRESSED we _have_ to read the entire extent in so we can
7911 * decompress it, so there will be buffering required no matter what we
7912 * do, so go ahead and fallback to buffered.
7914 * We return -ENOTBLK because that's what makes DIO go ahead and go back
7915 * to buffered IO. Don't blame me, this is the price we pay for using
7918 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7919 em->block_start == EXTENT_MAP_INLINE) {
7920 free_extent_map(em);
7925 len = min(len, em->len - (start - em->start));
7927 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7931 unlock_extents = true;
7932 /* Recalc len in case the new em is smaller than requested */
7933 len = min(len, em->len - (start - em->start));
7936 * We need to unlock only the end area that we aren't using.
7937 * The rest is going to be unlocked by the endio routine.
7939 lockstart = start + len;
7940 if (lockstart < lockend)
7941 unlock_extents = true;
7945 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7946 lockstart, lockend, &cached_state);
7948 free_extent_state(cached_state);
7951 * Translate extent map information to iomap.
7952 * We trim the extents (and move the addr) even though iomap code does
7953 * that, since we have locked only the parts we are performing I/O in.
7955 if ((em->block_start == EXTENT_MAP_HOLE) ||
7956 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7957 iomap->addr = IOMAP_NULL_ADDR;
7958 iomap->type = IOMAP_HOLE;
7960 iomap->addr = em->block_start + (start - em->start);
7961 iomap->type = IOMAP_MAPPED;
7963 iomap->offset = start;
7964 iomap->bdev = fs_info->fs_devices->latest_bdev;
7965 iomap->length = len;
7967 if (write && btrfs_use_zone_append(BTRFS_I(inode), em->block_start))
7968 iomap->flags |= IOMAP_F_ZONE_APPEND;
7970 free_extent_map(em);
7975 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7979 btrfs_delalloc_release_space(BTRFS_I(inode),
7980 dio_data->data_reserved, start,
7981 dio_data->reserve, true);
7982 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7983 extent_changeset_free(dio_data->data_reserved);
7989 static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7990 ssize_t written, unsigned int flags, struct iomap *iomap)
7993 struct btrfs_dio_data *dio_data = iomap->private;
7994 size_t submitted = dio_data->submitted;
7995 const bool write = !!(flags & IOMAP_WRITE);
7997 if (!write && (iomap->type == IOMAP_HOLE)) {
7998 /* If reading from a hole, unlock and return */
7999 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
8003 if (submitted < length) {
8005 length -= submitted;
8007 __endio_write_update_ordered(BTRFS_I(inode), pos,
8010 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
8016 if (dio_data->reserve)
8017 btrfs_delalloc_release_space(BTRFS_I(inode),
8018 dio_data->data_reserved, pos,
8019 dio_data->reserve, true);
8020 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
8021 extent_changeset_free(dio_data->data_reserved);
8025 iomap->private = NULL;
8030 static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
8033 * This implies a barrier so that stores to dio_bio->bi_status before
8034 * this and loads of dio_bio->bi_status after this are fully ordered.
8036 if (!refcount_dec_and_test(&dip->refs))
8039 if (btrfs_op(dip->dio_bio) == BTRFS_MAP_WRITE) {
8040 __endio_write_update_ordered(BTRFS_I(dip->inode),
8041 dip->logical_offset,
8043 !dip->dio_bio->bi_status);
8045 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
8046 dip->logical_offset,
8047 dip->logical_offset + dip->bytes - 1);
8050 bio_endio(dip->dio_bio);
8054 static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
8056 unsigned long bio_flags)
8058 struct btrfs_dio_private *dip = bio->bi_private;
8059 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8062 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
8064 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
8068 refcount_inc(&dip->refs);
8069 ret = btrfs_map_bio(fs_info, bio, mirror_num);
8071 refcount_dec(&dip->refs);
8075 static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
8076 struct btrfs_io_bio *io_bio,
8077 const bool uptodate)
8079 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
8080 const u32 sectorsize = fs_info->sectorsize;
8081 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
8082 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8083 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8084 struct bio_vec bvec;
8085 struct bvec_iter iter;
8086 u64 start = io_bio->logical;
8088 blk_status_t err = BLK_STS_OK;
8090 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
8091 unsigned int i, nr_sectors, pgoff;
8093 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
8094 pgoff = bvec.bv_offset;
8095 for (i = 0; i < nr_sectors; i++) {
8096 ASSERT(pgoff < PAGE_SIZE);
8098 (!csum || !check_data_csum(inode, io_bio,
8099 bio_offset, bvec.bv_page,
8101 clean_io_failure(fs_info, failure_tree, io_tree,
8102 start, bvec.bv_page,
8103 btrfs_ino(BTRFS_I(inode)),
8108 ASSERT((start - io_bio->logical) < UINT_MAX);
8109 ret = btrfs_repair_one_sector(inode,
8111 start - io_bio->logical,
8112 bvec.bv_page, pgoff,
8113 start, io_bio->mirror_num,
8114 submit_dio_repair_bio);
8116 err = errno_to_blk_status(ret);
8118 start += sectorsize;
8119 ASSERT(bio_offset + sectorsize > bio_offset);
8120 bio_offset += sectorsize;
8121 pgoff += sectorsize;
8127 static void __endio_write_update_ordered(struct btrfs_inode *inode,
8128 const u64 offset, const u64 bytes,
8129 const bool uptodate)
8131 btrfs_mark_ordered_io_finished(inode, NULL, offset, bytes,
8132 finish_ordered_fn, uptodate);
8135 static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
8137 u64 dio_file_offset)
8139 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
8142 static void btrfs_end_dio_bio(struct bio *bio)
8144 struct btrfs_dio_private *dip = bio->bi_private;
8145 blk_status_t err = bio->bi_status;
8148 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8149 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
8150 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8151 bio->bi_opf, bio->bi_iter.bi_sector,
8152 bio->bi_iter.bi_size, err);
8154 if (bio_op(bio) == REQ_OP_READ) {
8155 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
8160 dip->dio_bio->bi_status = err;
8162 btrfs_record_physical_zoned(dip->inode, dip->logical_offset, bio);
8165 btrfs_dio_private_put(dip);
8168 static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
8169 struct inode *inode, u64 file_offset, int async_submit)
8171 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8172 struct btrfs_dio_private *dip = bio->bi_private;
8173 bool write = btrfs_op(bio) == BTRFS_MAP_WRITE;
8176 /* Check btrfs_submit_bio_hook() for rules about async submit. */
8178 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8181 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
8186 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
8189 if (write && async_submit) {
8190 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
8191 btrfs_submit_bio_start_direct_io);
8195 * If we aren't doing async submit, calculate the csum of the
8198 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
8204 csum_offset = file_offset - dip->logical_offset;
8205 csum_offset >>= fs_info->sectorsize_bits;
8206 csum_offset *= fs_info->csum_size;
8207 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
8210 ret = btrfs_map_bio(fs_info, bio, 0);
8216 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
8217 * or ordered extents whether or not we submit any bios.
8219 static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
8220 struct inode *inode,
8223 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
8224 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
8226 struct btrfs_dio_private *dip;
8228 dip_size = sizeof(*dip);
8229 if (!write && csum) {
8230 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8233 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
8234 dip_size += fs_info->csum_size * nblocks;
8237 dip = kzalloc(dip_size, GFP_NOFS);
8242 dip->logical_offset = file_offset;
8243 dip->bytes = dio_bio->bi_iter.bi_size;
8244 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
8245 dip->dio_bio = dio_bio;
8246 refcount_set(&dip->refs, 1);
8250 static blk_qc_t btrfs_submit_direct(const struct iomap_iter *iter,
8251 struct bio *dio_bio, loff_t file_offset)
8253 struct inode *inode = iter->inode;
8254 const bool write = (btrfs_op(dio_bio) == BTRFS_MAP_WRITE);
8255 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8256 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
8257 BTRFS_BLOCK_GROUP_RAID56_MASK);
8258 struct btrfs_dio_private *dip;
8261 int async_submit = 0;
8263 u64 clone_offset = 0;
8267 blk_status_t status;
8268 struct btrfs_io_geometry geom;
8269 struct btrfs_dio_data *dio_data = iter->iomap.private;
8270 struct extent_map *em = NULL;
8272 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
8275 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8276 file_offset + dio_bio->bi_iter.bi_size - 1);
8278 dio_bio->bi_status = BLK_STS_RESOURCE;
8280 return BLK_QC_T_NONE;
8285 * Load the csums up front to reduce csum tree searches and
8286 * contention when submitting bios.
8288 * If we have csums disabled this will do nothing.
8290 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
8291 if (status != BLK_STS_OK)
8295 start_sector = dio_bio->bi_iter.bi_sector;
8296 submit_len = dio_bio->bi_iter.bi_size;
8299 logical = start_sector << 9;
8300 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
8302 status = errno_to_blk_status(PTR_ERR(em));
8306 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
8309 status = errno_to_blk_status(ret);
8313 clone_len = min(submit_len, geom.len);
8314 ASSERT(clone_len <= UINT_MAX);
8317 * This will never fail as it's passing GPF_NOFS and
8318 * the allocation is backed by btrfs_bioset.
8320 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
8321 bio->bi_private = dip;
8322 bio->bi_end_io = btrfs_end_dio_bio;
8323 btrfs_io_bio(bio)->logical = file_offset;
8325 if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
8326 status = extract_ordered_extent(BTRFS_I(inode), bio,
8334 ASSERT(submit_len >= clone_len);
8335 submit_len -= clone_len;
8338 * Increase the count before we submit the bio so we know
8339 * the end IO handler won't happen before we increase the
8340 * count. Otherwise, the dip might get freed before we're
8341 * done setting it up.
8343 * We transfer the initial reference to the last bio, so we
8344 * don't need to increment the reference count for the last one.
8346 if (submit_len > 0) {
8347 refcount_inc(&dip->refs);
8349 * If we are submitting more than one bio, submit them
8350 * all asynchronously. The exception is RAID 5 or 6, as
8351 * asynchronous checksums make it difficult to collect
8352 * full stripe writes.
8358 status = btrfs_submit_dio_bio(bio, inode, file_offset,
8363 refcount_dec(&dip->refs);
8367 dio_data->submitted += clone_len;
8368 clone_offset += clone_len;
8369 start_sector += clone_len >> 9;
8370 file_offset += clone_len;
8372 free_extent_map(em);
8373 } while (submit_len > 0);
8374 return BLK_QC_T_NONE;
8377 free_extent_map(em);
8379 dip->dio_bio->bi_status = status;
8380 btrfs_dio_private_put(dip);
8382 return BLK_QC_T_NONE;
8385 const struct iomap_ops btrfs_dio_iomap_ops = {
8386 .iomap_begin = btrfs_dio_iomap_begin,
8387 .iomap_end = btrfs_dio_iomap_end,
8390 const struct iomap_dio_ops btrfs_dio_ops = {
8391 .submit_io = btrfs_submit_direct,
8394 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8399 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
8403 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
8406 int btrfs_readpage(struct file *file, struct page *page)
8408 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8409 u64 start = page_offset(page);
8410 u64 end = start + PAGE_SIZE - 1;
8411 struct btrfs_bio_ctrl bio_ctrl = { 0 };
8414 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8416 ret = btrfs_do_readpage(page, NULL, &bio_ctrl, 0, NULL);
8418 ret = submit_one_bio(bio_ctrl.bio, 0, bio_ctrl.bio_flags);
8422 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8424 struct inode *inode = page->mapping->host;
8427 if (current->flags & PF_MEMALLOC) {
8428 redirty_page_for_writepage(wbc, page);
8434 * If we are under memory pressure we will call this directly from the
8435 * VM, we need to make sure we have the inode referenced for the ordered
8436 * extent. If not just return like we didn't do anything.
8438 if (!igrab(inode)) {
8439 redirty_page_for_writepage(wbc, page);
8440 return AOP_WRITEPAGE_ACTIVATE;
8442 ret = extent_write_full_page(page, wbc);
8443 btrfs_add_delayed_iput(inode);
8447 static int btrfs_writepages(struct address_space *mapping,
8448 struct writeback_control *wbc)
8450 return extent_writepages(mapping, wbc);
8453 static void btrfs_readahead(struct readahead_control *rac)
8455 extent_readahead(rac);
8459 * For releasepage() and invalidatepage() we have a race window where
8460 * end_page_writeback() is called but the subpage spinlock is not yet released.
8461 * If we continue to release/invalidate the page, we could cause use-after-free
8462 * for subpage spinlock. So this function is to spin and wait for subpage
8465 static void wait_subpage_spinlock(struct page *page)
8467 struct btrfs_fs_info *fs_info = btrfs_sb(page->mapping->host->i_sb);
8468 struct btrfs_subpage *subpage;
8470 if (fs_info->sectorsize == PAGE_SIZE)
8473 ASSERT(PagePrivate(page) && page->private);
8474 subpage = (struct btrfs_subpage *)page->private;
8477 * This may look insane as we just acquire the spinlock and release it,
8478 * without doing anything. But we just want to make sure no one is
8479 * still holding the subpage spinlock.
8480 * And since the page is not dirty nor writeback, and we have page
8481 * locked, the only possible way to hold a spinlock is from the endio
8482 * function to clear page writeback.
8484 * Here we just acquire the spinlock so that all existing callers
8485 * should exit and we're safe to release/invalidate the page.
8487 spin_lock_irq(&subpage->lock);
8488 spin_unlock_irq(&subpage->lock);
8491 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8493 int ret = try_release_extent_mapping(page, gfp_flags);
8496 wait_subpage_spinlock(page);
8497 clear_page_extent_mapped(page);
8502 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8504 if (PageWriteback(page) || PageDirty(page))
8506 return __btrfs_releasepage(page, gfp_flags);
8509 #ifdef CONFIG_MIGRATION
8510 static int btrfs_migratepage(struct address_space *mapping,
8511 struct page *newpage, struct page *page,
8512 enum migrate_mode mode)
8516 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8517 if (ret != MIGRATEPAGE_SUCCESS)
8520 if (page_has_private(page))
8521 attach_page_private(newpage, detach_page_private(page));
8523 if (PageOrdered(page)) {
8524 ClearPageOrdered(page);
8525 SetPageOrdered(newpage);
8528 if (mode != MIGRATE_SYNC_NO_COPY)
8529 migrate_page_copy(newpage, page);
8531 migrate_page_states(newpage, page);
8532 return MIGRATEPAGE_SUCCESS;
8536 static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8537 unsigned int length)
8539 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8540 struct btrfs_fs_info *fs_info = inode->root->fs_info;
8541 struct extent_io_tree *tree = &inode->io_tree;
8542 struct extent_state *cached_state = NULL;
8543 u64 page_start = page_offset(page);
8544 u64 page_end = page_start + PAGE_SIZE - 1;
8546 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
8549 * We have page locked so no new ordered extent can be created on this
8550 * page, nor bio can be submitted for this page.
8552 * But already submitted bio can still be finished on this page.
8553 * Furthermore, endio function won't skip page which has Ordered
8554 * (Private2) already cleared, so it's possible for endio and
8555 * invalidatepage to do the same ordered extent accounting twice
8558 * So here we wait for any submitted bios to finish, so that we won't
8559 * do double ordered extent accounting on the same page.
8561 wait_on_page_writeback(page);
8562 wait_subpage_spinlock(page);
8565 * For subpage case, we have call sites like
8566 * btrfs_punch_hole_lock_range() which passes range not aligned to
8568 * If the range doesn't cover the full page, we don't need to and
8569 * shouldn't clear page extent mapped, as page->private can still
8570 * record subpage dirty bits for other part of the range.
8572 * For cases that can invalidate the full even the range doesn't
8573 * cover the full page, like invalidating the last page, we're
8574 * still safe to wait for ordered extent to finish.
8576 if (!(offset == 0 && length == PAGE_SIZE)) {
8577 btrfs_releasepage(page, GFP_NOFS);
8581 if (!inode_evicting)
8582 lock_extent_bits(tree, page_start, page_end, &cached_state);
8585 while (cur < page_end) {
8586 struct btrfs_ordered_extent *ordered;
8591 ordered = btrfs_lookup_first_ordered_range(inode, cur,
8592 page_end + 1 - cur);
8594 range_end = page_end;
8596 * No ordered extent covering this range, we are safe
8597 * to delete all extent states in the range.
8599 delete_states = true;
8602 if (ordered->file_offset > cur) {
8604 * There is a range between [cur, oe->file_offset) not
8605 * covered by any ordered extent.
8606 * We are safe to delete all extent states, and handle
8607 * the ordered extent in the next iteration.
8609 range_end = ordered->file_offset - 1;
8610 delete_states = true;
8614 range_end = min(ordered->file_offset + ordered->num_bytes - 1,
8616 ASSERT(range_end + 1 - cur < U32_MAX);
8617 range_len = range_end + 1 - cur;
8618 if (!btrfs_page_test_ordered(fs_info, page, cur, range_len)) {
8620 * If Ordered (Private2) is cleared, it means endio has
8621 * already been executed for the range.
8622 * We can't delete the extent states as
8623 * btrfs_finish_ordered_io() may still use some of them.
8625 delete_states = false;
8628 btrfs_page_clear_ordered(fs_info, page, cur, range_len);
8631 * IO on this page will never be started, so we need to account
8632 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8633 * here, must leave that up for the ordered extent completion.
8635 * This will also unlock the range for incoming
8636 * btrfs_finish_ordered_io().
8638 if (!inode_evicting)
8639 clear_extent_bit(tree, cur, range_end,
8641 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8642 EXTENT_DEFRAG, 1, 0, &cached_state);
8644 spin_lock_irq(&inode->ordered_tree.lock);
8645 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8646 ordered->truncated_len = min(ordered->truncated_len,
8647 cur - ordered->file_offset);
8648 spin_unlock_irq(&inode->ordered_tree.lock);
8650 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8651 cur, range_end + 1 - cur)) {
8652 btrfs_finish_ordered_io(ordered);
8654 * The ordered extent has finished, now we're again
8655 * safe to delete all extent states of the range.
8657 delete_states = true;
8660 * btrfs_finish_ordered_io() will get executed by endio
8661 * of other pages, thus we can't delete extent states
8664 delete_states = false;
8668 btrfs_put_ordered_extent(ordered);
8670 * Qgroup reserved space handler
8671 * Sector(s) here will be either:
8673 * 1) Already written to disk or bio already finished
8674 * Then its QGROUP_RESERVED bit in io_tree is already cleared.
8675 * Qgroup will be handled by its qgroup_record then.
8676 * btrfs_qgroup_free_data() call will do nothing here.
8678 * 2) Not written to disk yet
8679 * Then btrfs_qgroup_free_data() call will clear the
8680 * QGROUP_RESERVED bit of its io_tree, and free the qgroup
8681 * reserved data space.
8682 * Since the IO will never happen for this page.
8684 btrfs_qgroup_free_data(inode, NULL, cur, range_end + 1 - cur);
8685 if (!inode_evicting) {
8686 clear_extent_bit(tree, cur, range_end, EXTENT_LOCKED |
8687 EXTENT_DELALLOC | EXTENT_UPTODATE |
8688 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
8689 delete_states, &cached_state);
8691 cur = range_end + 1;
8694 * We have iterated through all ordered extents of the page, the page
8695 * should not have Ordered (Private2) anymore, or the above iteration
8696 * did something wrong.
8698 ASSERT(!PageOrdered(page));
8699 if (!inode_evicting)
8700 __btrfs_releasepage(page, GFP_NOFS);
8701 ClearPageChecked(page);
8702 clear_page_extent_mapped(page);
8706 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8707 * called from a page fault handler when a page is first dirtied. Hence we must
8708 * be careful to check for EOF conditions here. We set the page up correctly
8709 * for a written page which means we get ENOSPC checking when writing into
8710 * holes and correct delalloc and unwritten extent mapping on filesystems that
8711 * support these features.
8713 * We are not allowed to take the i_mutex here so we have to play games to
8714 * protect against truncate races as the page could now be beyond EOF. Because
8715 * truncate_setsize() writes the inode size before removing pages, once we have
8716 * the page lock we can determine safely if the page is beyond EOF. If it is not
8717 * beyond EOF, then the page is guaranteed safe against truncation until we
8720 vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
8722 struct page *page = vmf->page;
8723 struct inode *inode = file_inode(vmf->vma->vm_file);
8724 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8725 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8726 struct btrfs_ordered_extent *ordered;
8727 struct extent_state *cached_state = NULL;
8728 struct extent_changeset *data_reserved = NULL;
8729 unsigned long zero_start;
8739 reserved_space = PAGE_SIZE;
8741 sb_start_pagefault(inode->i_sb);
8742 page_start = page_offset(page);
8743 page_end = page_start + PAGE_SIZE - 1;
8747 * Reserving delalloc space after obtaining the page lock can lead to
8748 * deadlock. For example, if a dirty page is locked by this function
8749 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8750 * dirty page write out, then the btrfs_writepage() function could
8751 * end up waiting indefinitely to get a lock on the page currently
8752 * being processed by btrfs_page_mkwrite() function.
8754 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8755 page_start, reserved_space);
8757 ret2 = file_update_time(vmf->vma->vm_file);
8761 ret = vmf_error(ret2);
8767 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
8769 down_read(&BTRFS_I(inode)->i_mmap_lock);
8771 size = i_size_read(inode);
8773 if ((page->mapping != inode->i_mapping) ||
8774 (page_start >= size)) {
8775 /* page got truncated out from underneath us */
8778 wait_on_page_writeback(page);
8780 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
8781 ret2 = set_page_extent_mapped(page);
8783 ret = vmf_error(ret2);
8784 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8789 * we can't set the delalloc bits if there are pending ordered
8790 * extents. Drop our locks and wait for them to finish
8792 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8795 unlock_extent_cached(io_tree, page_start, page_end,
8798 up_read(&BTRFS_I(inode)->i_mmap_lock);
8799 btrfs_start_ordered_extent(ordered, 1);
8800 btrfs_put_ordered_extent(ordered);
8804 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
8805 reserved_space = round_up(size - page_start,
8806 fs_info->sectorsize);
8807 if (reserved_space < PAGE_SIZE) {
8808 end = page_start + reserved_space - 1;
8809 btrfs_delalloc_release_space(BTRFS_I(inode),
8810 data_reserved, page_start,
8811 PAGE_SIZE - reserved_space, true);
8816 * page_mkwrite gets called when the page is firstly dirtied after it's
8817 * faulted in, but write(2) could also dirty a page and set delalloc
8818 * bits, thus in this case for space account reason, we still need to
8819 * clear any delalloc bits within this page range since we have to
8820 * reserve data&meta space before lock_page() (see above comments).
8822 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
8823 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8824 EXTENT_DEFRAG, 0, 0, &cached_state);
8826 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
8829 unlock_extent_cached(io_tree, page_start, page_end,
8831 ret = VM_FAULT_SIGBUS;
8835 /* page is wholly or partially inside EOF */
8836 if (page_start + PAGE_SIZE > size)
8837 zero_start = offset_in_page(size);
8839 zero_start = PAGE_SIZE;
8841 if (zero_start != PAGE_SIZE) {
8842 memzero_page(page, zero_start, PAGE_SIZE - zero_start);
8843 flush_dcache_page(page);
8845 ClearPageChecked(page);
8846 btrfs_page_set_dirty(fs_info, page, page_start, end + 1 - page_start);
8847 btrfs_page_set_uptodate(fs_info, page, page_start, end + 1 - page_start);
8849 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
8851 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
8852 up_read(&BTRFS_I(inode)->i_mmap_lock);
8854 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8855 sb_end_pagefault(inode->i_sb);
8856 extent_changeset_free(data_reserved);
8857 return VM_FAULT_LOCKED;
8861 up_read(&BTRFS_I(inode)->i_mmap_lock);
8863 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8864 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
8865 reserved_space, (ret != 0));
8867 sb_end_pagefault(inode->i_sb);
8868 extent_changeset_free(data_reserved);
8872 static int btrfs_truncate(struct inode *inode, bool skip_writeback)
8874 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8875 struct btrfs_root *root = BTRFS_I(inode)->root;
8876 struct btrfs_block_rsv *rsv;
8878 struct btrfs_trans_handle *trans;
8879 u64 mask = fs_info->sectorsize - 1;
8880 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
8881 u64 extents_found = 0;
8883 if (!skip_writeback) {
8884 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8891 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8892 * things going on here:
8894 * 1) We need to reserve space to update our inode.
8896 * 2) We need to have something to cache all the space that is going to
8897 * be free'd up by the truncate operation, but also have some slack
8898 * space reserved in case it uses space during the truncate (thank you
8899 * very much snapshotting).
8901 * And we need these to be separate. The fact is we can use a lot of
8902 * space doing the truncate, and we have no earthly idea how much space
8903 * we will use, so we need the truncate reservation to be separate so it
8904 * doesn't end up using space reserved for updating the inode. We also
8905 * need to be able to stop the transaction and start a new one, which
8906 * means we need to be able to update the inode several times, and we
8907 * have no idea of knowing how many times that will be, so we can't just
8908 * reserve 1 item for the entirety of the operation, so that has to be
8909 * done separately as well.
8911 * So that leaves us with
8913 * 1) rsv - for the truncate reservation, which we will steal from the
8914 * transaction reservation.
8915 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
8916 * updating the inode.
8918 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
8921 rsv->size = min_size;
8925 * 1 for the truncate slack space
8926 * 1 for updating the inode.
8928 trans = btrfs_start_transaction(root, 2);
8929 if (IS_ERR(trans)) {
8930 ret = PTR_ERR(trans);
8934 /* Migrate the slack space for the truncate to our reserve */
8935 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
8939 trans->block_rsv = rsv;
8942 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
8944 BTRFS_EXTENT_DATA_KEY,
8946 trans->block_rsv = &fs_info->trans_block_rsv;
8947 if (ret != -ENOSPC && ret != -EAGAIN)
8950 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
8954 btrfs_end_transaction(trans);
8955 btrfs_btree_balance_dirty(fs_info);
8957 trans = btrfs_start_transaction(root, 2);
8958 if (IS_ERR(trans)) {
8959 ret = PTR_ERR(trans);
8964 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
8965 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
8966 rsv, min_size, false);
8967 BUG_ON(ret); /* shouldn't happen */
8968 trans->block_rsv = rsv;
8972 * We can't call btrfs_truncate_block inside a trans handle as we could
8973 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8974 * we've truncated everything except the last little bit, and can do
8975 * btrfs_truncate_block and then update the disk_i_size.
8977 if (ret == NEED_TRUNCATE_BLOCK) {
8978 btrfs_end_transaction(trans);
8979 btrfs_btree_balance_dirty(fs_info);
8981 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
8984 trans = btrfs_start_transaction(root, 1);
8985 if (IS_ERR(trans)) {
8986 ret = PTR_ERR(trans);
8989 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
8995 trans->block_rsv = &fs_info->trans_block_rsv;
8996 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
9000 ret2 = btrfs_end_transaction(trans);
9003 btrfs_btree_balance_dirty(fs_info);
9006 btrfs_free_block_rsv(fs_info, rsv);
9008 * So if we truncate and then write and fsync we normally would just
9009 * write the extents that changed, which is a problem if we need to
9010 * first truncate that entire inode. So set this flag so we write out
9011 * all of the extents in the inode to the sync log so we're completely
9014 * If no extents were dropped or trimmed we don't need to force the next
9015 * fsync to truncate all the inode's items from the log and re-log them
9016 * all. This means the truncate operation did not change the file size,
9017 * or changed it to a smaller size but there was only an implicit hole
9018 * between the old i_size and the new i_size, and there were no prealloc
9019 * extents beyond i_size to drop.
9021 if (extents_found > 0)
9022 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
9028 * create a new subvolume directory/inode (helper for the ioctl).
9030 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
9031 struct btrfs_root *new_root,
9032 struct btrfs_root *parent_root,
9033 struct user_namespace *mnt_userns)
9035 struct inode *inode;
9040 err = btrfs_get_free_objectid(new_root, &ino);
9044 inode = btrfs_new_inode(trans, new_root, mnt_userns, NULL, "..", 2,
9046 S_IFDIR | (~current_umask() & S_IRWXUGO),
9049 return PTR_ERR(inode);
9050 inode->i_op = &btrfs_dir_inode_operations;
9051 inode->i_fop = &btrfs_dir_file_operations;
9053 set_nlink(inode, 1);
9054 btrfs_i_size_write(BTRFS_I(inode), 0);
9055 unlock_new_inode(inode);
9057 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9059 btrfs_err(new_root->fs_info,
9060 "error inheriting subvolume %llu properties: %d",
9061 new_root->root_key.objectid, err);
9063 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
9069 struct inode *btrfs_alloc_inode(struct super_block *sb)
9071 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
9072 struct btrfs_inode *ei;
9073 struct inode *inode;
9075 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
9082 ei->last_sub_trans = 0;
9083 ei->logged_trans = 0;
9084 ei->delalloc_bytes = 0;
9085 ei->new_delalloc_bytes = 0;
9086 ei->defrag_bytes = 0;
9087 ei->disk_i_size = 0;
9091 ei->index_cnt = (u64)-1;
9093 ei->last_unlink_trans = 0;
9094 ei->last_reflink_trans = 0;
9095 ei->last_log_commit = 0;
9097 spin_lock_init(&ei->lock);
9098 ei->outstanding_extents = 0;
9099 if (sb->s_magic != BTRFS_TEST_MAGIC)
9100 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
9101 BTRFS_BLOCK_RSV_DELALLOC);
9102 ei->runtime_flags = 0;
9103 ei->prop_compress = BTRFS_COMPRESS_NONE;
9104 ei->defrag_compress = BTRFS_COMPRESS_NONE;
9106 ei->delayed_node = NULL;
9108 ei->i_otime.tv_sec = 0;
9109 ei->i_otime.tv_nsec = 0;
9111 inode = &ei->vfs_inode;
9112 extent_map_tree_init(&ei->extent_tree);
9113 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
9114 extent_io_tree_init(fs_info, &ei->io_failure_tree,
9115 IO_TREE_INODE_IO_FAILURE, inode);
9116 extent_io_tree_init(fs_info, &ei->file_extent_tree,
9117 IO_TREE_INODE_FILE_EXTENT, inode);
9118 ei->io_tree.track_uptodate = true;
9119 ei->io_failure_tree.track_uptodate = true;
9120 atomic_set(&ei->sync_writers, 0);
9121 mutex_init(&ei->log_mutex);
9122 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
9123 INIT_LIST_HEAD(&ei->delalloc_inodes);
9124 INIT_LIST_HEAD(&ei->delayed_iput);
9125 RB_CLEAR_NODE(&ei->rb_node);
9126 init_rwsem(&ei->i_mmap_lock);
9131 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9132 void btrfs_test_destroy_inode(struct inode *inode)
9134 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
9135 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9139 void btrfs_free_inode(struct inode *inode)
9141 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9144 void btrfs_destroy_inode(struct inode *vfs_inode)
9146 struct btrfs_ordered_extent *ordered;
9147 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
9148 struct btrfs_root *root = inode->root;
9150 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
9151 WARN_ON(vfs_inode->i_data.nrpages);
9152 WARN_ON(inode->block_rsv.reserved);
9153 WARN_ON(inode->block_rsv.size);
9154 WARN_ON(inode->outstanding_extents);
9155 WARN_ON(inode->delalloc_bytes);
9156 WARN_ON(inode->new_delalloc_bytes);
9157 WARN_ON(inode->csum_bytes);
9158 WARN_ON(inode->defrag_bytes);
9161 * This can happen where we create an inode, but somebody else also
9162 * created the same inode and we need to destroy the one we already
9169 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9173 btrfs_err(root->fs_info,
9174 "found ordered extent %llu %llu on inode cleanup",
9175 ordered->file_offset, ordered->num_bytes);
9176 btrfs_remove_ordered_extent(inode, ordered);
9177 btrfs_put_ordered_extent(ordered);
9178 btrfs_put_ordered_extent(ordered);
9181 btrfs_qgroup_check_reserved_leak(inode);
9182 inode_tree_del(inode);
9183 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9184 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
9185 btrfs_put_root(inode->root);
9188 int btrfs_drop_inode(struct inode *inode)
9190 struct btrfs_root *root = BTRFS_I(inode)->root;
9195 /* the snap/subvol tree is on deleting */
9196 if (btrfs_root_refs(&root->root_item) == 0)
9199 return generic_drop_inode(inode);
9202 static void init_once(void *foo)
9204 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9206 inode_init_once(&ei->vfs_inode);
9209 void __cold btrfs_destroy_cachep(void)
9212 * Make sure all delayed rcu free inodes are flushed before we
9216 kmem_cache_destroy(btrfs_inode_cachep);
9217 kmem_cache_destroy(btrfs_trans_handle_cachep);
9218 kmem_cache_destroy(btrfs_path_cachep);
9219 kmem_cache_destroy(btrfs_free_space_cachep);
9220 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
9223 int __init btrfs_init_cachep(void)
9225 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
9226 sizeof(struct btrfs_inode), 0,
9227 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9229 if (!btrfs_inode_cachep)
9232 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
9233 sizeof(struct btrfs_trans_handle), 0,
9234 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
9235 if (!btrfs_trans_handle_cachep)
9238 btrfs_path_cachep = kmem_cache_create("btrfs_path",
9239 sizeof(struct btrfs_path), 0,
9240 SLAB_MEM_SPREAD, NULL);
9241 if (!btrfs_path_cachep)
9244 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
9245 sizeof(struct btrfs_free_space), 0,
9246 SLAB_MEM_SPREAD, NULL);
9247 if (!btrfs_free_space_cachep)
9250 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
9251 PAGE_SIZE, PAGE_SIZE,
9252 SLAB_MEM_SPREAD, NULL);
9253 if (!btrfs_free_space_bitmap_cachep)
9258 btrfs_destroy_cachep();
9262 static int btrfs_getattr(struct user_namespace *mnt_userns,
9263 const struct path *path, struct kstat *stat,
9264 u32 request_mask, unsigned int flags)
9268 struct inode *inode = d_inode(path->dentry);
9269 u32 blocksize = inode->i_sb->s_blocksize;
9270 u32 bi_flags = BTRFS_I(inode)->flags;
9271 u32 bi_ro_flags = BTRFS_I(inode)->ro_flags;
9273 stat->result_mask |= STATX_BTIME;
9274 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
9275 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
9276 if (bi_flags & BTRFS_INODE_APPEND)
9277 stat->attributes |= STATX_ATTR_APPEND;
9278 if (bi_flags & BTRFS_INODE_COMPRESS)
9279 stat->attributes |= STATX_ATTR_COMPRESSED;
9280 if (bi_flags & BTRFS_INODE_IMMUTABLE)
9281 stat->attributes |= STATX_ATTR_IMMUTABLE;
9282 if (bi_flags & BTRFS_INODE_NODUMP)
9283 stat->attributes |= STATX_ATTR_NODUMP;
9284 if (bi_ro_flags & BTRFS_INODE_RO_VERITY)
9285 stat->attributes |= STATX_ATTR_VERITY;
9287 stat->attributes_mask |= (STATX_ATTR_APPEND |
9288 STATX_ATTR_COMPRESSED |
9289 STATX_ATTR_IMMUTABLE |
9292 generic_fillattr(mnt_userns, inode, stat);
9293 stat->dev = BTRFS_I(inode)->root->anon_dev;
9295 spin_lock(&BTRFS_I(inode)->lock);
9296 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
9297 inode_bytes = inode_get_bytes(inode);
9298 spin_unlock(&BTRFS_I(inode)->lock);
9299 stat->blocks = (ALIGN(inode_bytes, blocksize) +
9300 ALIGN(delalloc_bytes, blocksize)) >> 9;
9304 static int btrfs_rename_exchange(struct inode *old_dir,
9305 struct dentry *old_dentry,
9306 struct inode *new_dir,
9307 struct dentry *new_dentry)
9309 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9310 struct btrfs_trans_handle *trans;
9311 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9312 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9313 struct inode *new_inode = new_dentry->d_inode;
9314 struct inode *old_inode = old_dentry->d_inode;
9315 struct timespec64 ctime = current_time(old_inode);
9316 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9317 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
9322 bool root_log_pinned = false;
9323 bool dest_log_pinned = false;
9324 bool need_abort = false;
9327 * For non-subvolumes allow exchange only within one subvolume, in the
9328 * same inode namespace. Two subvolumes (represented as directory) can
9329 * be exchanged as they're a logical link and have a fixed inode number.
9332 (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
9333 new_ino != BTRFS_FIRST_FREE_OBJECTID))
9336 /* close the race window with snapshot create/destroy ioctl */
9337 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9338 new_ino == BTRFS_FIRST_FREE_OBJECTID)
9339 down_read(&fs_info->subvol_sem);
9342 * We want to reserve the absolute worst case amount of items. So if
9343 * both inodes are subvols and we need to unlink them then that would
9344 * require 4 item modifications, but if they are both normal inodes it
9345 * would require 5 item modifications, so we'll assume their normal
9346 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9347 * should cover the worst case number of items we'll modify.
9349 trans = btrfs_start_transaction(root, 12);
9350 if (IS_ERR(trans)) {
9351 ret = PTR_ERR(trans);
9356 ret = btrfs_record_root_in_trans(trans, dest);
9362 * We need to find a free sequence number both in the source and
9363 * in the destination directory for the exchange.
9365 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
9368 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
9372 BTRFS_I(old_inode)->dir_index = 0ULL;
9373 BTRFS_I(new_inode)->dir_index = 0ULL;
9375 /* Reference for the source. */
9376 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9377 /* force full log commit if subvolume involved. */
9378 btrfs_set_log_full_commit(trans);
9380 ret = btrfs_insert_inode_ref(trans, dest,
9381 new_dentry->d_name.name,
9382 new_dentry->d_name.len,
9384 btrfs_ino(BTRFS_I(new_dir)),
9391 /* And now for the dest. */
9392 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9393 /* force full log commit if subvolume involved. */
9394 btrfs_set_log_full_commit(trans);
9396 ret = btrfs_insert_inode_ref(trans, root,
9397 old_dentry->d_name.name,
9398 old_dentry->d_name.len,
9400 btrfs_ino(BTRFS_I(old_dir)),
9404 btrfs_abort_transaction(trans, ret);
9409 /* Update inode version and ctime/mtime. */
9410 inode_inc_iversion(old_dir);
9411 inode_inc_iversion(new_dir);
9412 inode_inc_iversion(old_inode);
9413 inode_inc_iversion(new_inode);
9414 old_dir->i_ctime = old_dir->i_mtime = ctime;
9415 new_dir->i_ctime = new_dir->i_mtime = ctime;
9416 old_inode->i_ctime = ctime;
9417 new_inode->i_ctime = ctime;
9419 if (old_dentry->d_parent != new_dentry->d_parent) {
9420 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9421 BTRFS_I(old_inode), 1);
9422 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9423 BTRFS_I(new_inode), 1);
9427 * Now pin the logs of the roots. We do it to ensure that no other task
9428 * can sync the logs while we are in progress with the rename, because
9429 * that could result in an inconsistency in case any of the inodes that
9430 * are part of this rename operation were logged before.
9432 * We pin the logs even if at this precise moment none of the inodes was
9433 * logged before. This is because right after we checked for that, some
9434 * other task fsyncing some other inode not involved with this rename
9435 * operation could log that one of our inodes exists.
9437 * We don't need to pin the logs before the above calls to
9438 * btrfs_insert_inode_ref(), since those don't ever need to change a log.
9440 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
9441 btrfs_pin_log_trans(root);
9442 root_log_pinned = true;
9444 if (new_ino != BTRFS_FIRST_FREE_OBJECTID) {
9445 btrfs_pin_log_trans(dest);
9446 dest_log_pinned = true;
9449 /* src is a subvolume */
9450 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9451 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
9452 } else { /* src is an inode */
9453 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9454 BTRFS_I(old_dentry->d_inode),
9455 old_dentry->d_name.name,
9456 old_dentry->d_name.len);
9458 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
9461 btrfs_abort_transaction(trans, ret);
9465 /* dest is a subvolume */
9466 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9467 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
9468 } else { /* dest is an inode */
9469 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9470 BTRFS_I(new_dentry->d_inode),
9471 new_dentry->d_name.name,
9472 new_dentry->d_name.len);
9474 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
9477 btrfs_abort_transaction(trans, ret);
9481 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
9482 new_dentry->d_name.name,
9483 new_dentry->d_name.len, 0, old_idx);
9485 btrfs_abort_transaction(trans, ret);
9489 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
9490 old_dentry->d_name.name,
9491 old_dentry->d_name.len, 0, new_idx);
9493 btrfs_abort_transaction(trans, ret);
9497 if (old_inode->i_nlink == 1)
9498 BTRFS_I(old_inode)->dir_index = old_idx;
9499 if (new_inode->i_nlink == 1)
9500 BTRFS_I(new_inode)->dir_index = new_idx;
9502 if (root_log_pinned) {
9503 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9504 new_dentry->d_parent);
9505 btrfs_end_log_trans(root);
9506 root_log_pinned = false;
9508 if (dest_log_pinned) {
9509 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9510 old_dentry->d_parent);
9511 btrfs_end_log_trans(dest);
9512 dest_log_pinned = false;
9516 * If we have pinned a log and an error happened, we unpin tasks
9517 * trying to sync the log and force them to fallback to a transaction
9518 * commit if the log currently contains any of the inodes involved in
9519 * this rename operation (to ensure we do not persist a log with an
9520 * inconsistent state for any of these inodes or leading to any
9521 * inconsistencies when replayed). If the transaction was aborted, the
9522 * abortion reason is propagated to userspace when attempting to commit
9523 * the transaction. If the log does not contain any of these inodes, we
9524 * allow the tasks to sync it.
9526 if (ret && (root_log_pinned || dest_log_pinned)) {
9527 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9528 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9529 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
9530 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation))
9531 btrfs_set_log_full_commit(trans);
9533 if (root_log_pinned) {
9534 btrfs_end_log_trans(root);
9535 root_log_pinned = false;
9537 if (dest_log_pinned) {
9538 btrfs_end_log_trans(dest);
9539 dest_log_pinned = false;
9542 ret2 = btrfs_end_transaction(trans);
9543 ret = ret ? ret : ret2;
9545 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9546 old_ino == BTRFS_FIRST_FREE_OBJECTID)
9547 up_read(&fs_info->subvol_sem);
9552 static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9553 struct btrfs_root *root,
9554 struct user_namespace *mnt_userns,
9556 struct dentry *dentry)
9559 struct inode *inode;
9563 ret = btrfs_get_free_objectid(root, &objectid);
9567 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
9568 dentry->d_name.name,
9570 btrfs_ino(BTRFS_I(dir)),
9572 S_IFCHR | WHITEOUT_MODE,
9575 if (IS_ERR(inode)) {
9576 ret = PTR_ERR(inode);
9580 inode->i_op = &btrfs_special_inode_operations;
9581 init_special_inode(inode, inode->i_mode,
9584 ret = btrfs_init_inode_security(trans, inode, dir,
9589 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9590 BTRFS_I(inode), 0, index);
9594 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
9596 unlock_new_inode(inode);
9598 inode_dec_link_count(inode);
9604 static int btrfs_rename(struct user_namespace *mnt_userns,
9605 struct inode *old_dir, struct dentry *old_dentry,
9606 struct inode *new_dir, struct dentry *new_dentry,
9609 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9610 struct btrfs_trans_handle *trans;
9611 unsigned int trans_num_items;
9612 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9613 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9614 struct inode *new_inode = d_inode(new_dentry);
9615 struct inode *old_inode = d_inode(old_dentry);
9619 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9620 bool log_pinned = false;
9622 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
9625 /* we only allow rename subvolume link between subvolumes */
9626 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9629 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9630 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
9633 if (S_ISDIR(old_inode->i_mode) && new_inode &&
9634 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
9638 /* check for collisions, even if the name isn't there */
9639 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
9640 new_dentry->d_name.name,
9641 new_dentry->d_name.len);
9644 if (ret == -EEXIST) {
9646 * eexist without a new_inode */
9647 if (WARN_ON(!new_inode)) {
9651 /* maybe -EOVERFLOW */
9658 * we're using rename to replace one file with another. Start IO on it
9659 * now so we don't add too much work to the end of the transaction
9661 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
9662 filemap_flush(old_inode->i_mapping);
9664 /* close the racy window with snapshot create/destroy ioctl */
9665 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9666 down_read(&fs_info->subvol_sem);
9668 * We want to reserve the absolute worst case amount of items. So if
9669 * both inodes are subvols and we need to unlink them then that would
9670 * require 4 item modifications, but if they are both normal inodes it
9671 * would require 5 item modifications, so we'll assume they are normal
9672 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9673 * should cover the worst case number of items we'll modify.
9674 * If our rename has the whiteout flag, we need more 5 units for the
9675 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9676 * when selinux is enabled).
9678 trans_num_items = 11;
9679 if (flags & RENAME_WHITEOUT)
9680 trans_num_items += 5;
9681 trans = btrfs_start_transaction(root, trans_num_items);
9682 if (IS_ERR(trans)) {
9683 ret = PTR_ERR(trans);
9688 ret = btrfs_record_root_in_trans(trans, dest);
9693 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
9697 BTRFS_I(old_inode)->dir_index = 0ULL;
9698 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9699 /* force full log commit if subvolume involved. */
9700 btrfs_set_log_full_commit(trans);
9702 ret = btrfs_insert_inode_ref(trans, dest,
9703 new_dentry->d_name.name,
9704 new_dentry->d_name.len,
9706 btrfs_ino(BTRFS_I(new_dir)), index);
9711 inode_inc_iversion(old_dir);
9712 inode_inc_iversion(new_dir);
9713 inode_inc_iversion(old_inode);
9714 old_dir->i_ctime = old_dir->i_mtime =
9715 new_dir->i_ctime = new_dir->i_mtime =
9716 old_inode->i_ctime = current_time(old_dir);
9718 if (old_dentry->d_parent != new_dentry->d_parent)
9719 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9720 BTRFS_I(old_inode), 1);
9722 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9723 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
9726 * Now pin the log. We do it to ensure that no other task can
9727 * sync the log while we are in progress with the rename, as
9728 * that could result in an inconsistency in case any of the
9729 * inodes that are part of this rename operation were logged
9732 * We pin the log even if at this precise moment none of the
9733 * inodes was logged before. This is because right after we
9734 * checked for that, some other task fsyncing some other inode
9735 * not involved with this rename operation could log that one of
9736 * our inodes exists.
9738 * We don't need to pin the logs before the above call to
9739 * btrfs_insert_inode_ref(), since that does not need to change
9742 btrfs_pin_log_trans(root);
9744 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9745 BTRFS_I(d_inode(old_dentry)),
9746 old_dentry->d_name.name,
9747 old_dentry->d_name.len);
9749 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
9752 btrfs_abort_transaction(trans, ret);
9757 inode_inc_iversion(new_inode);
9758 new_inode->i_ctime = current_time(new_inode);
9759 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
9760 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9761 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
9762 BUG_ON(new_inode->i_nlink == 0);
9764 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9765 BTRFS_I(d_inode(new_dentry)),
9766 new_dentry->d_name.name,
9767 new_dentry->d_name.len);
9769 if (!ret && new_inode->i_nlink == 0)
9770 ret = btrfs_orphan_add(trans,
9771 BTRFS_I(d_inode(new_dentry)));
9773 btrfs_abort_transaction(trans, ret);
9778 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
9779 new_dentry->d_name.name,
9780 new_dentry->d_name.len, 0, index);
9782 btrfs_abort_transaction(trans, ret);
9786 if (old_inode->i_nlink == 1)
9787 BTRFS_I(old_inode)->dir_index = index;
9790 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9791 new_dentry->d_parent);
9792 btrfs_end_log_trans(root);
9796 if (flags & RENAME_WHITEOUT) {
9797 ret = btrfs_whiteout_for_rename(trans, root, mnt_userns,
9798 old_dir, old_dentry);
9801 btrfs_abort_transaction(trans, ret);
9807 * If we have pinned the log and an error happened, we unpin tasks
9808 * trying to sync the log and force them to fallback to a transaction
9809 * commit if the log currently contains any of the inodes involved in
9810 * this rename operation (to ensure we do not persist a log with an
9811 * inconsistent state for any of these inodes or leading to any
9812 * inconsistencies when replayed). If the transaction was aborted, the
9813 * abortion reason is propagated to userspace when attempting to commit
9814 * the transaction. If the log does not contain any of these inodes, we
9815 * allow the tasks to sync it.
9817 if (ret && log_pinned) {
9818 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9819 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9820 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
9822 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
9823 btrfs_set_log_full_commit(trans);
9825 btrfs_end_log_trans(root);
9828 ret2 = btrfs_end_transaction(trans);
9829 ret = ret ? ret : ret2;
9831 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9832 up_read(&fs_info->subvol_sem);
9837 static int btrfs_rename2(struct user_namespace *mnt_userns, struct inode *old_dir,
9838 struct dentry *old_dentry, struct inode *new_dir,
9839 struct dentry *new_dentry, unsigned int flags)
9841 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
9844 if (flags & RENAME_EXCHANGE)
9845 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9848 return btrfs_rename(mnt_userns, old_dir, old_dentry, new_dir,
9852 struct btrfs_delalloc_work {
9853 struct inode *inode;
9854 struct completion completion;
9855 struct list_head list;
9856 struct btrfs_work work;
9859 static void btrfs_run_delalloc_work(struct btrfs_work *work)
9861 struct btrfs_delalloc_work *delalloc_work;
9862 struct inode *inode;
9864 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9866 inode = delalloc_work->inode;
9867 filemap_flush(inode->i_mapping);
9868 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9869 &BTRFS_I(inode)->runtime_flags))
9870 filemap_flush(inode->i_mapping);
9873 complete(&delalloc_work->completion);
9876 static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
9878 struct btrfs_delalloc_work *work;
9880 work = kmalloc(sizeof(*work), GFP_NOFS);
9884 init_completion(&work->completion);
9885 INIT_LIST_HEAD(&work->list);
9886 work->inode = inode;
9887 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
9893 * some fairly slow code that needs optimization. This walks the list
9894 * of all the inodes with pending delalloc and forces them to disk.
9896 static int start_delalloc_inodes(struct btrfs_root *root,
9897 struct writeback_control *wbc, bool snapshot,
9898 bool in_reclaim_context)
9900 struct btrfs_inode *binode;
9901 struct inode *inode;
9902 struct btrfs_delalloc_work *work, *next;
9903 struct list_head works;
9904 struct list_head splice;
9906 bool full_flush = wbc->nr_to_write == LONG_MAX;
9908 INIT_LIST_HEAD(&works);
9909 INIT_LIST_HEAD(&splice);
9911 mutex_lock(&root->delalloc_mutex);
9912 spin_lock(&root->delalloc_lock);
9913 list_splice_init(&root->delalloc_inodes, &splice);
9914 while (!list_empty(&splice)) {
9915 binode = list_entry(splice.next, struct btrfs_inode,
9918 list_move_tail(&binode->delalloc_inodes,
9919 &root->delalloc_inodes);
9921 if (in_reclaim_context &&
9922 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9925 inode = igrab(&binode->vfs_inode);
9927 cond_resched_lock(&root->delalloc_lock);
9930 spin_unlock(&root->delalloc_lock);
9933 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9934 &binode->runtime_flags);
9936 work = btrfs_alloc_delalloc_work(inode);
9942 list_add_tail(&work->list, &works);
9943 btrfs_queue_work(root->fs_info->flush_workers,
9946 ret = filemap_fdatawrite_wbc(inode->i_mapping, wbc);
9947 btrfs_add_delayed_iput(inode);
9948 if (ret || wbc->nr_to_write <= 0)
9952 spin_lock(&root->delalloc_lock);
9954 spin_unlock(&root->delalloc_lock);
9957 list_for_each_entry_safe(work, next, &works, list) {
9958 list_del_init(&work->list);
9959 wait_for_completion(&work->completion);
9963 if (!list_empty(&splice)) {
9964 spin_lock(&root->delalloc_lock);
9965 list_splice_tail(&splice, &root->delalloc_inodes);
9966 spin_unlock(&root->delalloc_lock);
9968 mutex_unlock(&root->delalloc_mutex);
9972 int btrfs_start_delalloc_snapshot(struct btrfs_root *root, bool in_reclaim_context)
9974 struct writeback_control wbc = {
9975 .nr_to_write = LONG_MAX,
9976 .sync_mode = WB_SYNC_NONE,
9978 .range_end = LLONG_MAX,
9980 struct btrfs_fs_info *fs_info = root->fs_info;
9982 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
9985 return start_delalloc_inodes(root, &wbc, true, in_reclaim_context);
9988 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
9989 bool in_reclaim_context)
9991 struct writeback_control wbc = {
9993 .sync_mode = WB_SYNC_NONE,
9995 .range_end = LLONG_MAX,
9997 struct btrfs_root *root;
9998 struct list_head splice;
10001 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
10004 INIT_LIST_HEAD(&splice);
10006 mutex_lock(&fs_info->delalloc_root_mutex);
10007 spin_lock(&fs_info->delalloc_root_lock);
10008 list_splice_init(&fs_info->delalloc_roots, &splice);
10009 while (!list_empty(&splice)) {
10011 * Reset nr_to_write here so we know that we're doing a full
10014 if (nr == LONG_MAX)
10015 wbc.nr_to_write = LONG_MAX;
10017 root = list_first_entry(&splice, struct btrfs_root,
10019 root = btrfs_grab_root(root);
10021 list_move_tail(&root->delalloc_root,
10022 &fs_info->delalloc_roots);
10023 spin_unlock(&fs_info->delalloc_root_lock);
10025 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
10026 btrfs_put_root(root);
10027 if (ret < 0 || wbc.nr_to_write <= 0)
10029 spin_lock(&fs_info->delalloc_root_lock);
10031 spin_unlock(&fs_info->delalloc_root_lock);
10035 if (!list_empty(&splice)) {
10036 spin_lock(&fs_info->delalloc_root_lock);
10037 list_splice_tail(&splice, &fs_info->delalloc_roots);
10038 spin_unlock(&fs_info->delalloc_root_lock);
10040 mutex_unlock(&fs_info->delalloc_root_mutex);
10044 static int btrfs_symlink(struct user_namespace *mnt_userns, struct inode *dir,
10045 struct dentry *dentry, const char *symname)
10047 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10048 struct btrfs_trans_handle *trans;
10049 struct btrfs_root *root = BTRFS_I(dir)->root;
10050 struct btrfs_path *path;
10051 struct btrfs_key key;
10052 struct inode *inode = NULL;
10059 struct btrfs_file_extent_item *ei;
10060 struct extent_buffer *leaf;
10062 name_len = strlen(symname);
10063 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
10064 return -ENAMETOOLONG;
10067 * 2 items for inode item and ref
10068 * 2 items for dir items
10069 * 1 item for updating parent inode item
10070 * 1 item for the inline extent item
10071 * 1 item for xattr if selinux is on
10073 trans = btrfs_start_transaction(root, 7);
10075 return PTR_ERR(trans);
10077 err = btrfs_get_free_objectid(root, &objectid);
10081 inode = btrfs_new_inode(trans, root, mnt_userns, dir,
10082 dentry->d_name.name, dentry->d_name.len,
10083 btrfs_ino(BTRFS_I(dir)), objectid,
10084 S_IFLNK | S_IRWXUGO, &index);
10085 if (IS_ERR(inode)) {
10086 err = PTR_ERR(inode);
10092 * If the active LSM wants to access the inode during
10093 * d_instantiate it needs these. Smack checks to see
10094 * if the filesystem supports xattrs by looking at the
10097 inode->i_fop = &btrfs_file_operations;
10098 inode->i_op = &btrfs_file_inode_operations;
10099 inode->i_mapping->a_ops = &btrfs_aops;
10101 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10105 path = btrfs_alloc_path();
10110 key.objectid = btrfs_ino(BTRFS_I(inode));
10112 key.type = BTRFS_EXTENT_DATA_KEY;
10113 datasize = btrfs_file_extent_calc_inline_size(name_len);
10114 err = btrfs_insert_empty_item(trans, root, path, &key,
10117 btrfs_free_path(path);
10120 leaf = path->nodes[0];
10121 ei = btrfs_item_ptr(leaf, path->slots[0],
10122 struct btrfs_file_extent_item);
10123 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10124 btrfs_set_file_extent_type(leaf, ei,
10125 BTRFS_FILE_EXTENT_INLINE);
10126 btrfs_set_file_extent_encryption(leaf, ei, 0);
10127 btrfs_set_file_extent_compression(leaf, ei, 0);
10128 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10129 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10131 ptr = btrfs_file_extent_inline_start(ei);
10132 write_extent_buffer(leaf, symname, ptr, name_len);
10133 btrfs_mark_buffer_dirty(leaf);
10134 btrfs_free_path(path);
10136 inode->i_op = &btrfs_symlink_inode_operations;
10137 inode_nohighmem(inode);
10138 inode_set_bytes(inode, name_len);
10139 btrfs_i_size_write(BTRFS_I(inode), name_len);
10140 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
10142 * Last step, add directory indexes for our symlink inode. This is the
10143 * last step to avoid extra cleanup of these indexes if an error happens
10147 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
10148 BTRFS_I(inode), 0, index);
10152 d_instantiate_new(dentry, inode);
10155 btrfs_end_transaction(trans);
10156 if (err && inode) {
10157 inode_dec_link_count(inode);
10158 discard_new_inode(inode);
10160 btrfs_btree_balance_dirty(fs_info);
10164 static struct btrfs_trans_handle *insert_prealloc_file_extent(
10165 struct btrfs_trans_handle *trans_in,
10166 struct btrfs_inode *inode,
10167 struct btrfs_key *ins,
10170 struct btrfs_file_extent_item stack_fi;
10171 struct btrfs_replace_extent_info extent_info;
10172 struct btrfs_trans_handle *trans = trans_in;
10173 struct btrfs_path *path;
10174 u64 start = ins->objectid;
10175 u64 len = ins->offset;
10176 int qgroup_released;
10179 memset(&stack_fi, 0, sizeof(stack_fi));
10181 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
10182 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
10183 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
10184 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
10185 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
10186 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
10187 /* Encryption and other encoding is reserved and all 0 */
10189 qgroup_released = btrfs_qgroup_release_data(inode, file_offset, len);
10190 if (qgroup_released < 0)
10191 return ERR_PTR(qgroup_released);
10194 ret = insert_reserved_file_extent(trans, inode,
10195 file_offset, &stack_fi,
10196 true, qgroup_released);
10202 extent_info.disk_offset = start;
10203 extent_info.disk_len = len;
10204 extent_info.data_offset = 0;
10205 extent_info.data_len = len;
10206 extent_info.file_offset = file_offset;
10207 extent_info.extent_buf = (char *)&stack_fi;
10208 extent_info.is_new_extent = true;
10209 extent_info.qgroup_reserved = qgroup_released;
10210 extent_info.insertions = 0;
10212 path = btrfs_alloc_path();
10218 ret = btrfs_replace_file_extents(inode, path, file_offset,
10219 file_offset + len - 1, &extent_info,
10221 btrfs_free_path(path);
10228 * We have released qgroup data range at the beginning of the function,
10229 * and normally qgroup_released bytes will be freed when committing
10231 * But if we error out early, we have to free what we have released
10232 * or we leak qgroup data reservation.
10234 btrfs_qgroup_free_refroot(inode->root->fs_info,
10235 inode->root->root_key.objectid, qgroup_released,
10236 BTRFS_QGROUP_RSV_DATA);
10237 return ERR_PTR(ret);
10240 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10241 u64 start, u64 num_bytes, u64 min_size,
10242 loff_t actual_len, u64 *alloc_hint,
10243 struct btrfs_trans_handle *trans)
10245 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
10246 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10247 struct extent_map *em;
10248 struct btrfs_root *root = BTRFS_I(inode)->root;
10249 struct btrfs_key ins;
10250 u64 cur_offset = start;
10251 u64 clear_offset = start;
10254 u64 last_alloc = (u64)-1;
10256 bool own_trans = true;
10257 u64 end = start + num_bytes - 1;
10261 while (num_bytes > 0) {
10262 cur_bytes = min_t(u64, num_bytes, SZ_256M);
10263 cur_bytes = max(cur_bytes, min_size);
10265 * If we are severely fragmented we could end up with really
10266 * small allocations, so if the allocator is returning small
10267 * chunks lets make its job easier by only searching for those
10270 cur_bytes = min(cur_bytes, last_alloc);
10271 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10272 min_size, 0, *alloc_hint, &ins, 1, 0);
10277 * We've reserved this space, and thus converted it from
10278 * ->bytes_may_use to ->bytes_reserved. Any error that happens
10279 * from here on out we will only need to clear our reservation
10280 * for the remaining unreserved area, so advance our
10281 * clear_offset by our extent size.
10283 clear_offset += ins.offset;
10285 last_alloc = ins.offset;
10286 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
10289 * Now that we inserted the prealloc extent we can finally
10290 * decrement the number of reservations in the block group.
10291 * If we did it before, we could race with relocation and have
10292 * relocation miss the reserved extent, making it fail later.
10294 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10295 if (IS_ERR(trans)) {
10296 ret = PTR_ERR(trans);
10297 btrfs_free_reserved_extent(fs_info, ins.objectid,
10302 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
10303 cur_offset + ins.offset -1, 0);
10305 em = alloc_extent_map();
10307 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10308 &BTRFS_I(inode)->runtime_flags);
10312 em->start = cur_offset;
10313 em->orig_start = cur_offset;
10314 em->len = ins.offset;
10315 em->block_start = ins.objectid;
10316 em->block_len = ins.offset;
10317 em->orig_block_len = ins.offset;
10318 em->ram_bytes = ins.offset;
10319 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10320 em->generation = trans->transid;
10323 write_lock(&em_tree->lock);
10324 ret = add_extent_mapping(em_tree, em, 1);
10325 write_unlock(&em_tree->lock);
10326 if (ret != -EEXIST)
10328 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
10329 cur_offset + ins.offset - 1,
10332 free_extent_map(em);
10334 num_bytes -= ins.offset;
10335 cur_offset += ins.offset;
10336 *alloc_hint = ins.objectid + ins.offset;
10338 inode_inc_iversion(inode);
10339 inode->i_ctime = current_time(inode);
10340 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
10341 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
10342 (actual_len > inode->i_size) &&
10343 (cur_offset > inode->i_size)) {
10344 if (cur_offset > actual_len)
10345 i_size = actual_len;
10347 i_size = cur_offset;
10348 i_size_write(inode, i_size);
10349 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
10352 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
10355 btrfs_abort_transaction(trans, ret);
10357 btrfs_end_transaction(trans);
10362 btrfs_end_transaction(trans);
10366 if (clear_offset < end)
10367 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
10368 end - clear_offset + 1);
10372 int btrfs_prealloc_file_range(struct inode *inode, int mode,
10373 u64 start, u64 num_bytes, u64 min_size,
10374 loff_t actual_len, u64 *alloc_hint)
10376 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10377 min_size, actual_len, alloc_hint,
10381 int btrfs_prealloc_file_range_trans(struct inode *inode,
10382 struct btrfs_trans_handle *trans, int mode,
10383 u64 start, u64 num_bytes, u64 min_size,
10384 loff_t actual_len, u64 *alloc_hint)
10386 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10387 min_size, actual_len, alloc_hint, trans);
10390 static int btrfs_set_page_dirty(struct page *page)
10392 return __set_page_dirty_nobuffers(page);
10395 static int btrfs_permission(struct user_namespace *mnt_userns,
10396 struct inode *inode, int mask)
10398 struct btrfs_root *root = BTRFS_I(inode)->root;
10399 umode_t mode = inode->i_mode;
10401 if (mask & MAY_WRITE &&
10402 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10403 if (btrfs_root_readonly(root))
10405 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10408 return generic_permission(mnt_userns, inode, mask);
10411 static int btrfs_tmpfile(struct user_namespace *mnt_userns, struct inode *dir,
10412 struct dentry *dentry, umode_t mode)
10414 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10415 struct btrfs_trans_handle *trans;
10416 struct btrfs_root *root = BTRFS_I(dir)->root;
10417 struct inode *inode = NULL;
10423 * 5 units required for adding orphan entry
10425 trans = btrfs_start_transaction(root, 5);
10427 return PTR_ERR(trans);
10429 ret = btrfs_get_free_objectid(root, &objectid);
10433 inode = btrfs_new_inode(trans, root, mnt_userns, dir, NULL, 0,
10434 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
10435 if (IS_ERR(inode)) {
10436 ret = PTR_ERR(inode);
10441 inode->i_fop = &btrfs_file_operations;
10442 inode->i_op = &btrfs_file_inode_operations;
10444 inode->i_mapping->a_ops = &btrfs_aops;
10446 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10450 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
10453 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
10458 * We set number of links to 0 in btrfs_new_inode(), and here we set
10459 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10462 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10464 set_nlink(inode, 1);
10465 d_tmpfile(dentry, inode);
10466 unlock_new_inode(inode);
10467 mark_inode_dirty(inode);
10469 btrfs_end_transaction(trans);
10471 discard_new_inode(inode);
10472 btrfs_btree_balance_dirty(fs_info);
10476 void btrfs_set_range_writeback(struct btrfs_inode *inode, u64 start, u64 end)
10478 struct btrfs_fs_info *fs_info = inode->root->fs_info;
10479 unsigned long index = start >> PAGE_SHIFT;
10480 unsigned long end_index = end >> PAGE_SHIFT;
10484 ASSERT(end + 1 - start <= U32_MAX);
10485 len = end + 1 - start;
10486 while (index <= end_index) {
10487 page = find_get_page(inode->vfs_inode.i_mapping, index);
10488 ASSERT(page); /* Pages should be in the extent_io_tree */
10490 btrfs_page_set_writeback(fs_info, page, start, len);
10498 * Add an entry indicating a block group or device which is pinned by a
10499 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10500 * negative errno on failure.
10502 static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10503 bool is_block_group)
10505 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10506 struct btrfs_swapfile_pin *sp, *entry;
10507 struct rb_node **p;
10508 struct rb_node *parent = NULL;
10510 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10515 sp->is_block_group = is_block_group;
10516 sp->bg_extent_count = 1;
10518 spin_lock(&fs_info->swapfile_pins_lock);
10519 p = &fs_info->swapfile_pins.rb_node;
10522 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10523 if (sp->ptr < entry->ptr ||
10524 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10525 p = &(*p)->rb_left;
10526 } else if (sp->ptr > entry->ptr ||
10527 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10528 p = &(*p)->rb_right;
10530 if (is_block_group)
10531 entry->bg_extent_count++;
10532 spin_unlock(&fs_info->swapfile_pins_lock);
10537 rb_link_node(&sp->node, parent, p);
10538 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10539 spin_unlock(&fs_info->swapfile_pins_lock);
10543 /* Free all of the entries pinned by this swapfile. */
10544 static void btrfs_free_swapfile_pins(struct inode *inode)
10546 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10547 struct btrfs_swapfile_pin *sp;
10548 struct rb_node *node, *next;
10550 spin_lock(&fs_info->swapfile_pins_lock);
10551 node = rb_first(&fs_info->swapfile_pins);
10553 next = rb_next(node);
10554 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10555 if (sp->inode == inode) {
10556 rb_erase(&sp->node, &fs_info->swapfile_pins);
10557 if (sp->is_block_group) {
10558 btrfs_dec_block_group_swap_extents(sp->ptr,
10559 sp->bg_extent_count);
10560 btrfs_put_block_group(sp->ptr);
10566 spin_unlock(&fs_info->swapfile_pins_lock);
10569 struct btrfs_swap_info {
10575 unsigned long nr_pages;
10579 static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10580 struct btrfs_swap_info *bsi)
10582 unsigned long nr_pages;
10583 u64 first_ppage, first_ppage_reported, next_ppage;
10586 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10587 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10588 PAGE_SIZE) >> PAGE_SHIFT;
10590 if (first_ppage >= next_ppage)
10592 nr_pages = next_ppage - first_ppage;
10594 first_ppage_reported = first_ppage;
10595 if (bsi->start == 0)
10596 first_ppage_reported++;
10597 if (bsi->lowest_ppage > first_ppage_reported)
10598 bsi->lowest_ppage = first_ppage_reported;
10599 if (bsi->highest_ppage < (next_ppage - 1))
10600 bsi->highest_ppage = next_ppage - 1;
10602 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10605 bsi->nr_extents += ret;
10606 bsi->nr_pages += nr_pages;
10610 static void btrfs_swap_deactivate(struct file *file)
10612 struct inode *inode = file_inode(file);
10614 btrfs_free_swapfile_pins(inode);
10615 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10618 static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10621 struct inode *inode = file_inode(file);
10622 struct btrfs_root *root = BTRFS_I(inode)->root;
10623 struct btrfs_fs_info *fs_info = root->fs_info;
10624 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10625 struct extent_state *cached_state = NULL;
10626 struct extent_map *em = NULL;
10627 struct btrfs_device *device = NULL;
10628 struct btrfs_swap_info bsi = {
10629 .lowest_ppage = (sector_t)-1ULL,
10636 * If the swap file was just created, make sure delalloc is done. If the
10637 * file changes again after this, the user is doing something stupid and
10638 * we don't really care.
10640 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10645 * The inode is locked, so these flags won't change after we check them.
10647 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10648 btrfs_warn(fs_info, "swapfile must not be compressed");
10651 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10652 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10655 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10656 btrfs_warn(fs_info, "swapfile must not be checksummed");
10661 * Balance or device remove/replace/resize can move stuff around from
10662 * under us. The exclop protection makes sure they aren't running/won't
10663 * run concurrently while we are mapping the swap extents, and
10664 * fs_info->swapfile_pins prevents them from running while the swap
10665 * file is active and moving the extents. Note that this also prevents
10666 * a concurrent device add which isn't actually necessary, but it's not
10667 * really worth the trouble to allow it.
10669 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
10670 btrfs_warn(fs_info,
10671 "cannot activate swapfile while exclusive operation is running");
10676 * Prevent snapshot creation while we are activating the swap file.
10677 * We do not want to race with snapshot creation. If snapshot creation
10678 * already started before we bumped nr_swapfiles from 0 to 1 and
10679 * completes before the first write into the swap file after it is
10680 * activated, than that write would fallback to COW.
10682 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10683 btrfs_exclop_finish(fs_info);
10684 btrfs_warn(fs_info,
10685 "cannot activate swapfile because snapshot creation is in progress");
10689 * Snapshots can create extents which require COW even if NODATACOW is
10690 * set. We use this counter to prevent snapshots. We must increment it
10691 * before walking the extents because we don't want a concurrent
10692 * snapshot to run after we've already checked the extents.
10694 atomic_inc(&root->nr_swapfiles);
10696 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10698 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10700 while (start < isize) {
10701 u64 logical_block_start, physical_block_start;
10702 struct btrfs_block_group *bg;
10703 u64 len = isize - start;
10705 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
10711 if (em->block_start == EXTENT_MAP_HOLE) {
10712 btrfs_warn(fs_info, "swapfile must not have holes");
10716 if (em->block_start == EXTENT_MAP_INLINE) {
10718 * It's unlikely we'll ever actually find ourselves
10719 * here, as a file small enough to fit inline won't be
10720 * big enough to store more than the swap header, but in
10721 * case something changes in the future, let's catch it
10722 * here rather than later.
10724 btrfs_warn(fs_info, "swapfile must not be inline");
10728 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10729 btrfs_warn(fs_info, "swapfile must not be compressed");
10734 logical_block_start = em->block_start + (start - em->start);
10735 len = min(len, em->len - (start - em->start));
10736 free_extent_map(em);
10739 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
10745 btrfs_warn(fs_info,
10746 "swapfile must not be copy-on-write");
10751 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10757 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10758 btrfs_warn(fs_info,
10759 "swapfile must have single data profile");
10764 if (device == NULL) {
10765 device = em->map_lookup->stripes[0].dev;
10766 ret = btrfs_add_swapfile_pin(inode, device, false);
10771 } else if (device != em->map_lookup->stripes[0].dev) {
10772 btrfs_warn(fs_info, "swapfile must be on one device");
10777 physical_block_start = (em->map_lookup->stripes[0].physical +
10778 (logical_block_start - em->start));
10779 len = min(len, em->len - (logical_block_start - em->start));
10780 free_extent_map(em);
10783 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10785 btrfs_warn(fs_info,
10786 "could not find block group containing swapfile");
10791 if (!btrfs_inc_block_group_swap_extents(bg)) {
10792 btrfs_warn(fs_info,
10793 "block group for swapfile at %llu is read-only%s",
10795 atomic_read(&fs_info->scrubs_running) ?
10796 " (scrub running)" : "");
10797 btrfs_put_block_group(bg);
10802 ret = btrfs_add_swapfile_pin(inode, bg, true);
10804 btrfs_put_block_group(bg);
10811 if (bsi.block_len &&
10812 bsi.block_start + bsi.block_len == physical_block_start) {
10813 bsi.block_len += len;
10815 if (bsi.block_len) {
10816 ret = btrfs_add_swap_extent(sis, &bsi);
10821 bsi.block_start = physical_block_start;
10822 bsi.block_len = len;
10829 ret = btrfs_add_swap_extent(sis, &bsi);
10832 if (!IS_ERR_OR_NULL(em))
10833 free_extent_map(em);
10835 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10838 btrfs_swap_deactivate(file);
10840 btrfs_drew_write_unlock(&root->snapshot_lock);
10842 btrfs_exclop_finish(fs_info);
10848 sis->bdev = device->bdev;
10849 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10850 sis->max = bsi.nr_pages;
10851 sis->pages = bsi.nr_pages - 1;
10852 sis->highest_bit = bsi.nr_pages - 1;
10853 return bsi.nr_extents;
10856 static void btrfs_swap_deactivate(struct file *file)
10860 static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10863 return -EOPNOTSUPP;
10868 * Update the number of bytes used in the VFS' inode. When we replace extents in
10869 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10870 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10871 * always get a correct value.
10873 void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10874 const u64 add_bytes,
10875 const u64 del_bytes)
10877 if (add_bytes == del_bytes)
10880 spin_lock(&inode->lock);
10882 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10884 inode_add_bytes(&inode->vfs_inode, add_bytes);
10885 spin_unlock(&inode->lock);
10888 static const struct inode_operations btrfs_dir_inode_operations = {
10889 .getattr = btrfs_getattr,
10890 .lookup = btrfs_lookup,
10891 .create = btrfs_create,
10892 .unlink = btrfs_unlink,
10893 .link = btrfs_link,
10894 .mkdir = btrfs_mkdir,
10895 .rmdir = btrfs_rmdir,
10896 .rename = btrfs_rename2,
10897 .symlink = btrfs_symlink,
10898 .setattr = btrfs_setattr,
10899 .mknod = btrfs_mknod,
10900 .listxattr = btrfs_listxattr,
10901 .permission = btrfs_permission,
10902 .get_acl = btrfs_get_acl,
10903 .set_acl = btrfs_set_acl,
10904 .update_time = btrfs_update_time,
10905 .tmpfile = btrfs_tmpfile,
10906 .fileattr_get = btrfs_fileattr_get,
10907 .fileattr_set = btrfs_fileattr_set,
10910 static const struct file_operations btrfs_dir_file_operations = {
10911 .llseek = generic_file_llseek,
10912 .read = generic_read_dir,
10913 .iterate_shared = btrfs_real_readdir,
10914 .open = btrfs_opendir,
10915 .unlocked_ioctl = btrfs_ioctl,
10916 #ifdef CONFIG_COMPAT
10917 .compat_ioctl = btrfs_compat_ioctl,
10919 .release = btrfs_release_file,
10920 .fsync = btrfs_sync_file,
10924 * btrfs doesn't support the bmap operation because swapfiles
10925 * use bmap to make a mapping of extents in the file. They assume
10926 * these extents won't change over the life of the file and they
10927 * use the bmap result to do IO directly to the drive.
10929 * the btrfs bmap call would return logical addresses that aren't
10930 * suitable for IO and they also will change frequently as COW
10931 * operations happen. So, swapfile + btrfs == corruption.
10933 * For now we're avoiding this by dropping bmap.
10935 static const struct address_space_operations btrfs_aops = {
10936 .readpage = btrfs_readpage,
10937 .writepage = btrfs_writepage,
10938 .writepages = btrfs_writepages,
10939 .readahead = btrfs_readahead,
10940 .direct_IO = noop_direct_IO,
10941 .invalidatepage = btrfs_invalidatepage,
10942 .releasepage = btrfs_releasepage,
10943 #ifdef CONFIG_MIGRATION
10944 .migratepage = btrfs_migratepage,
10946 .set_page_dirty = btrfs_set_page_dirty,
10947 .error_remove_page = generic_error_remove_page,
10948 .swap_activate = btrfs_swap_activate,
10949 .swap_deactivate = btrfs_swap_deactivate,
10952 static const struct inode_operations btrfs_file_inode_operations = {
10953 .getattr = btrfs_getattr,
10954 .setattr = btrfs_setattr,
10955 .listxattr = btrfs_listxattr,
10956 .permission = btrfs_permission,
10957 .fiemap = btrfs_fiemap,
10958 .get_acl = btrfs_get_acl,
10959 .set_acl = btrfs_set_acl,
10960 .update_time = btrfs_update_time,
10961 .fileattr_get = btrfs_fileattr_get,
10962 .fileattr_set = btrfs_fileattr_set,
10964 static const struct inode_operations btrfs_special_inode_operations = {
10965 .getattr = btrfs_getattr,
10966 .setattr = btrfs_setattr,
10967 .permission = btrfs_permission,
10968 .listxattr = btrfs_listxattr,
10969 .get_acl = btrfs_get_acl,
10970 .set_acl = btrfs_set_acl,
10971 .update_time = btrfs_update_time,
10973 static const struct inode_operations btrfs_symlink_inode_operations = {
10974 .get_link = page_get_link,
10975 .getattr = btrfs_getattr,
10976 .setattr = btrfs_setattr,
10977 .permission = btrfs_permission,
10978 .listxattr = btrfs_listxattr,
10979 .update_time = btrfs_update_time,
10982 const struct dentry_operations btrfs_dentry_operations = {
10983 .d_delete = btrfs_dentry_delete,