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/buffer_head.h>
10 #include <linux/file.h>
12 #include <linux/pagemap.h>
13 #include <linux/highmem.h>
14 #include <linux/time.h>
15 #include <linux/init.h>
16 #include <linux/string.h>
17 #include <linux/backing-dev.h>
18 #include <linux/writeback.h>
19 #include <linux/compat.h>
20 #include <linux/xattr.h>
21 #include <linux/posix_acl.h>
22 #include <linux/falloc.h>
23 #include <linux/slab.h>
24 #include <linux/ratelimit.h>
25 #include <linux/btrfs.h>
26 #include <linux/blkdev.h>
27 #include <linux/posix_acl_xattr.h>
28 #include <linux/uio.h>
29 #include <linux/magic.h>
30 #include <linux/iversion.h>
31 #include <linux/swap.h>
32 #include <linux/migrate.h>
33 #include <linux/sched/mm.h>
34 #include <asm/unaligned.h>
38 #include "transaction.h"
39 #include "btrfs_inode.h"
40 #include "print-tree.h"
41 #include "ordered-data.h"
45 #include "compression.h"
47 #include "free-space-cache.h"
48 #include "inode-map.h"
51 #include "delalloc-space.h"
52 #include "block-group.h"
53 #include "space-info.h"
55 struct btrfs_iget_args
{
57 struct btrfs_root
*root
;
60 struct btrfs_dio_data
{
62 u64 unsubmitted_oe_range_start
;
63 u64 unsubmitted_oe_range_end
;
67 static const struct inode_operations btrfs_dir_inode_operations
;
68 static const struct inode_operations btrfs_symlink_inode_operations
;
69 static const struct inode_operations btrfs_special_inode_operations
;
70 static const struct inode_operations btrfs_file_inode_operations
;
71 static const struct address_space_operations btrfs_aops
;
72 static const struct file_operations btrfs_dir_file_operations
;
73 static const struct extent_io_ops btrfs_extent_io_ops
;
75 static struct kmem_cache
*btrfs_inode_cachep
;
76 struct kmem_cache
*btrfs_trans_handle_cachep
;
77 struct kmem_cache
*btrfs_path_cachep
;
78 struct kmem_cache
*btrfs_free_space_cachep
;
79 struct kmem_cache
*btrfs_free_space_bitmap_cachep
;
81 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
82 static int btrfs_truncate(struct inode
*inode
, bool skip_writeback
);
83 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
84 static noinline
int cow_file_range(struct btrfs_inode
*inode
,
85 struct page
*locked_page
,
86 u64 start
, u64 end
, int *page_started
,
87 unsigned long *nr_written
, int unlock
);
88 static struct extent_map
*create_io_em(struct btrfs_inode
*inode
, u64 start
,
89 u64 len
, u64 orig_start
, u64 block_start
,
90 u64 block_len
, u64 orig_block_len
,
91 u64 ram_bytes
, int compress_type
,
94 static void __endio_write_update_ordered(struct btrfs_inode
*inode
,
95 const u64 offset
, const u64 bytes
,
99 * Cleanup all submitted ordered extents in specified range to handle errors
100 * from the btrfs_run_delalloc_range() callback.
102 * NOTE: caller must ensure that when an error happens, it can not call
103 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
104 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
105 * to be released, which we want to happen only when finishing the ordered
106 * extent (btrfs_finish_ordered_io()).
108 static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode
*inode
,
109 struct page
*locked_page
,
110 u64 offset
, u64 bytes
)
112 unsigned long index
= offset
>> PAGE_SHIFT
;
113 unsigned long end_index
= (offset
+ bytes
- 1) >> PAGE_SHIFT
;
114 u64 page_start
= page_offset(locked_page
);
115 u64 page_end
= page_start
+ PAGE_SIZE
- 1;
119 while (index
<= end_index
) {
120 page
= find_get_page(inode
->vfs_inode
.i_mapping
, index
);
124 ClearPagePrivate2(page
);
129 * In case this page belongs to the delalloc range being instantiated
130 * then skip it, since the first page of a range is going to be
131 * properly cleaned up by the caller of run_delalloc_range
133 if (page_start
>= offset
&& page_end
<= (offset
+ bytes
- 1)) {
138 return __endio_write_update_ordered(inode
, offset
, bytes
, false);
141 static int btrfs_dirty_inode(struct inode
*inode
);
143 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
144 void btrfs_test_inode_set_ops(struct inode
*inode
)
146 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
150 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
151 struct inode
*inode
, struct inode
*dir
,
152 const struct qstr
*qstr
)
156 err
= btrfs_init_acl(trans
, inode
, dir
);
158 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
163 * this does all the hard work for inserting an inline extent into
164 * the btree. The caller should have done a btrfs_drop_extents so that
165 * no overlapping inline items exist in the btree
167 static int insert_inline_extent(struct btrfs_trans_handle
*trans
,
168 struct btrfs_path
*path
, int extent_inserted
,
169 struct btrfs_root
*root
, struct inode
*inode
,
170 u64 start
, size_t size
, size_t compressed_size
,
172 struct page
**compressed_pages
)
174 struct extent_buffer
*leaf
;
175 struct page
*page
= NULL
;
178 struct btrfs_file_extent_item
*ei
;
180 size_t cur_size
= size
;
181 unsigned long offset
;
183 ASSERT((compressed_size
> 0 && compressed_pages
) ||
184 (compressed_size
== 0 && !compressed_pages
));
186 if (compressed_size
&& compressed_pages
)
187 cur_size
= compressed_size
;
189 inode_add_bytes(inode
, size
);
191 if (!extent_inserted
) {
192 struct btrfs_key key
;
195 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
197 key
.type
= BTRFS_EXTENT_DATA_KEY
;
199 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
200 path
->leave_spinning
= 1;
201 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
206 leaf
= path
->nodes
[0];
207 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
208 struct btrfs_file_extent_item
);
209 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
210 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
211 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
212 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
213 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
214 ptr
= btrfs_file_extent_inline_start(ei
);
216 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
219 while (compressed_size
> 0) {
220 cpage
= compressed_pages
[i
];
221 cur_size
= min_t(unsigned long, compressed_size
,
224 kaddr
= kmap_atomic(cpage
);
225 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
226 kunmap_atomic(kaddr
);
230 compressed_size
-= cur_size
;
232 btrfs_set_file_extent_compression(leaf
, ei
,
235 page
= find_get_page(inode
->i_mapping
,
236 start
>> PAGE_SHIFT
);
237 btrfs_set_file_extent_compression(leaf
, ei
, 0);
238 kaddr
= kmap_atomic(page
);
239 offset
= offset_in_page(start
);
240 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
241 kunmap_atomic(kaddr
);
244 btrfs_mark_buffer_dirty(leaf
);
245 btrfs_release_path(path
);
248 * We align size to sectorsize for inline extents just for simplicity
251 size
= ALIGN(size
, root
->fs_info
->sectorsize
);
252 ret
= btrfs_inode_set_file_extent_range(BTRFS_I(inode
), start
, size
);
257 * we're an inline extent, so nobody can
258 * extend the file past i_size without locking
259 * a page we already have locked.
261 * We must do any isize and inode updates
262 * before we unlock the pages. Otherwise we
263 * could end up racing with unlink.
265 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
266 ret
= btrfs_update_inode(trans
, root
, inode
);
274 * conditionally insert an inline extent into the file. This
275 * does the checks required to make sure the data is small enough
276 * to fit as an inline extent.
278 static noinline
int cow_file_range_inline(struct btrfs_inode
*inode
, u64 start
,
279 u64 end
, size_t compressed_size
,
281 struct page
**compressed_pages
)
283 struct btrfs_root
*root
= inode
->root
;
284 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
285 struct btrfs_trans_handle
*trans
;
286 u64 isize
= i_size_read(&inode
->vfs_inode
);
287 u64 actual_end
= min(end
+ 1, isize
);
288 u64 inline_len
= actual_end
- start
;
289 u64 aligned_end
= ALIGN(end
, fs_info
->sectorsize
);
290 u64 data_len
= inline_len
;
292 struct btrfs_path
*path
;
293 int extent_inserted
= 0;
294 u32 extent_item_size
;
297 data_len
= compressed_size
;
300 actual_end
> fs_info
->sectorsize
||
301 data_len
> BTRFS_MAX_INLINE_DATA_SIZE(fs_info
) ||
303 (actual_end
& (fs_info
->sectorsize
- 1)) == 0) ||
305 data_len
> fs_info
->max_inline
) {
309 path
= btrfs_alloc_path();
313 trans
= btrfs_join_transaction(root
);
315 btrfs_free_path(path
);
316 return PTR_ERR(trans
);
318 trans
->block_rsv
= &inode
->block_rsv
;
320 if (compressed_size
&& compressed_pages
)
321 extent_item_size
= btrfs_file_extent_calc_inline_size(
324 extent_item_size
= btrfs_file_extent_calc_inline_size(
327 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
, start
, aligned_end
,
328 NULL
, 1, 1, extent_item_size
,
331 btrfs_abort_transaction(trans
, ret
);
335 if (isize
> actual_end
)
336 inline_len
= min_t(u64
, isize
, actual_end
);
337 ret
= insert_inline_extent(trans
, path
, extent_inserted
,
338 root
, &inode
->vfs_inode
, start
,
339 inline_len
, compressed_size
,
340 compress_type
, compressed_pages
);
341 if (ret
&& ret
!= -ENOSPC
) {
342 btrfs_abort_transaction(trans
, ret
);
344 } else if (ret
== -ENOSPC
) {
349 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &inode
->runtime_flags
);
350 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
353 * Don't forget to free the reserved space, as for inlined extent
354 * it won't count as data extent, free them directly here.
355 * And at reserve time, it's always aligned to page size, so
356 * just free one page here.
358 btrfs_qgroup_free_data(inode
, NULL
, 0, PAGE_SIZE
);
359 btrfs_free_path(path
);
360 btrfs_end_transaction(trans
);
364 struct async_extent
{
369 unsigned long nr_pages
;
371 struct list_head list
;
376 struct page
*locked_page
;
379 unsigned int write_flags
;
380 struct list_head extents
;
381 struct cgroup_subsys_state
*blkcg_css
;
382 struct btrfs_work work
;
387 /* Number of chunks in flight; must be first in the structure */
389 struct async_chunk chunks
[];
392 static noinline
int add_async_extent(struct async_chunk
*cow
,
393 u64 start
, u64 ram_size
,
396 unsigned long nr_pages
,
399 struct async_extent
*async_extent
;
401 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
402 BUG_ON(!async_extent
); /* -ENOMEM */
403 async_extent
->start
= start
;
404 async_extent
->ram_size
= ram_size
;
405 async_extent
->compressed_size
= compressed_size
;
406 async_extent
->pages
= pages
;
407 async_extent
->nr_pages
= nr_pages
;
408 async_extent
->compress_type
= compress_type
;
409 list_add_tail(&async_extent
->list
, &cow
->extents
);
414 * Check if the inode has flags compatible with compression
416 static inline bool inode_can_compress(struct btrfs_inode
*inode
)
418 if (inode
->flags
& BTRFS_INODE_NODATACOW
||
419 inode
->flags
& BTRFS_INODE_NODATASUM
)
425 * Check if the inode needs to be submitted to compression, based on mount
426 * options, defragmentation, properties or heuristics.
428 static inline int inode_need_compress(struct btrfs_inode
*inode
, u64 start
,
431 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
433 if (!inode_can_compress(inode
)) {
434 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG
),
435 KERN_ERR
"BTRFS: unexpected compression for ino %llu\n",
440 if (btrfs_test_opt(fs_info
, FORCE_COMPRESS
))
443 if (inode
->defrag_compress
)
445 /* bad compression ratios */
446 if (inode
->flags
& BTRFS_INODE_NOCOMPRESS
)
448 if (btrfs_test_opt(fs_info
, COMPRESS
) ||
449 inode
->flags
& BTRFS_INODE_COMPRESS
||
450 inode
->prop_compress
)
451 return btrfs_compress_heuristic(&inode
->vfs_inode
, start
, end
);
455 static inline void inode_should_defrag(struct btrfs_inode
*inode
,
456 u64 start
, u64 end
, u64 num_bytes
, u64 small_write
)
458 /* If this is a small write inside eof, kick off a defrag */
459 if (num_bytes
< small_write
&&
460 (start
> 0 || end
+ 1 < inode
->disk_i_size
))
461 btrfs_add_inode_defrag(NULL
, inode
);
465 * we create compressed extents in two phases. The first
466 * phase compresses a range of pages that have already been
467 * locked (both pages and state bits are locked).
469 * This is done inside an ordered work queue, and the compression
470 * is spread across many cpus. The actual IO submission is step
471 * two, and the ordered work queue takes care of making sure that
472 * happens in the same order things were put onto the queue by
473 * writepages and friends.
475 * If this code finds it can't get good compression, it puts an
476 * entry onto the work queue to write the uncompressed bytes. This
477 * makes sure that both compressed inodes and uncompressed inodes
478 * are written in the same order that the flusher thread sent them
481 static noinline
int compress_file_range(struct async_chunk
*async_chunk
)
483 struct inode
*inode
= async_chunk
->inode
;
484 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
485 u64 blocksize
= fs_info
->sectorsize
;
486 u64 start
= async_chunk
->start
;
487 u64 end
= async_chunk
->end
;
491 struct page
**pages
= NULL
;
492 unsigned long nr_pages
;
493 unsigned long total_compressed
= 0;
494 unsigned long total_in
= 0;
497 int compress_type
= fs_info
->compress_type
;
498 int compressed_extents
= 0;
501 inode_should_defrag(BTRFS_I(inode
), start
, end
, end
- start
+ 1,
505 * We need to save i_size before now because it could change in between
506 * us evaluating the size and assigning it. This is because we lock and
507 * unlock the page in truncate and fallocate, and then modify the i_size
510 * The barriers are to emulate READ_ONCE, remove that once i_size_read
514 i_size
= i_size_read(inode
);
516 actual_end
= min_t(u64
, i_size
, end
+ 1);
519 nr_pages
= (end
>> PAGE_SHIFT
) - (start
>> PAGE_SHIFT
) + 1;
520 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED
% PAGE_SIZE
) != 0);
521 nr_pages
= min_t(unsigned long, nr_pages
,
522 BTRFS_MAX_COMPRESSED
/ PAGE_SIZE
);
525 * we don't want to send crud past the end of i_size through
526 * compression, that's just a waste of CPU time. So, if the
527 * end of the file is before the start of our current
528 * requested range of bytes, we bail out to the uncompressed
529 * cleanup code that can deal with all of this.
531 * It isn't really the fastest way to fix things, but this is a
532 * very uncommon corner.
534 if (actual_end
<= start
)
535 goto cleanup_and_bail_uncompressed
;
537 total_compressed
= actual_end
- start
;
540 * skip compression for a small file range(<=blocksize) that
541 * isn't an inline extent, since it doesn't save disk space at all.
543 if (total_compressed
<= blocksize
&&
544 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
545 goto cleanup_and_bail_uncompressed
;
547 total_compressed
= min_t(unsigned long, total_compressed
,
548 BTRFS_MAX_UNCOMPRESSED
);
553 * we do compression for mount -o compress and when the
554 * inode has not been flagged as nocompress. This flag can
555 * change at any time if we discover bad compression ratios.
557 if (inode_need_compress(BTRFS_I(inode
), start
, end
)) {
559 pages
= kcalloc(nr_pages
, sizeof(struct page
*), GFP_NOFS
);
561 /* just bail out to the uncompressed code */
566 if (BTRFS_I(inode
)->defrag_compress
)
567 compress_type
= BTRFS_I(inode
)->defrag_compress
;
568 else if (BTRFS_I(inode
)->prop_compress
)
569 compress_type
= BTRFS_I(inode
)->prop_compress
;
572 * we need to call clear_page_dirty_for_io on each
573 * page in the range. Otherwise applications with the file
574 * mmap'd can wander in and change the page contents while
575 * we are compressing them.
577 * If the compression fails for any reason, we set the pages
578 * dirty again later on.
580 * Note that the remaining part is redirtied, the start pointer
581 * has moved, the end is the original one.
584 extent_range_clear_dirty_for_io(inode
, start
, end
);
588 /* Compression level is applied here and only here */
589 ret
= btrfs_compress_pages(
590 compress_type
| (fs_info
->compress_level
<< 4),
591 inode
->i_mapping
, start
,
598 unsigned long offset
= offset_in_page(total_compressed
);
599 struct page
*page
= pages
[nr_pages
- 1];
602 /* zero the tail end of the last page, we might be
603 * sending it down to disk
606 kaddr
= kmap_atomic(page
);
607 memset(kaddr
+ offset
, 0,
609 kunmap_atomic(kaddr
);
616 /* lets try to make an inline extent */
617 if (ret
|| total_in
< actual_end
) {
618 /* we didn't compress the entire range, try
619 * to make an uncompressed inline extent.
621 ret
= cow_file_range_inline(BTRFS_I(inode
), start
, end
,
622 0, BTRFS_COMPRESS_NONE
,
625 /* try making a compressed inline extent */
626 ret
= cow_file_range_inline(BTRFS_I(inode
), start
, end
,
628 compress_type
, pages
);
631 unsigned long clear_flags
= EXTENT_DELALLOC
|
632 EXTENT_DELALLOC_NEW
| EXTENT_DEFRAG
|
633 EXTENT_DO_ACCOUNTING
;
634 unsigned long page_error_op
;
636 page_error_op
= ret
< 0 ? PAGE_SET_ERROR
: 0;
639 * inline extent creation worked or returned error,
640 * we don't need to create any more async work items.
641 * Unlock and free up our temp pages.
643 * We use DO_ACCOUNTING here because we need the
644 * delalloc_release_metadata to be done _after_ we drop
645 * our outstanding extent for clearing delalloc for this
648 extent_clear_unlock_delalloc(BTRFS_I(inode
), start
, end
,
658 * Ensure we only free the compressed pages if we have
659 * them allocated, as we can still reach here with
660 * inode_need_compress() == false.
663 for (i
= 0; i
< nr_pages
; i
++) {
664 WARN_ON(pages
[i
]->mapping
);
675 * we aren't doing an inline extent round the compressed size
676 * up to a block size boundary so the allocator does sane
679 total_compressed
= ALIGN(total_compressed
, blocksize
);
682 * one last check to make sure the compression is really a
683 * win, compare the page count read with the blocks on disk,
684 * compression must free at least one sector size
686 total_in
= ALIGN(total_in
, PAGE_SIZE
);
687 if (total_compressed
+ blocksize
<= total_in
) {
688 compressed_extents
++;
691 * The async work queues will take care of doing actual
692 * allocation on disk for these compressed pages, and
693 * will submit them to the elevator.
695 add_async_extent(async_chunk
, start
, total_in
,
696 total_compressed
, pages
, nr_pages
,
699 if (start
+ total_in
< end
) {
705 return compressed_extents
;
710 * the compression code ran but failed to make things smaller,
711 * free any pages it allocated and our page pointer array
713 for (i
= 0; i
< nr_pages
; i
++) {
714 WARN_ON(pages
[i
]->mapping
);
719 total_compressed
= 0;
722 /* flag the file so we don't compress in the future */
723 if (!btrfs_test_opt(fs_info
, FORCE_COMPRESS
) &&
724 !(BTRFS_I(inode
)->prop_compress
)) {
725 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
728 cleanup_and_bail_uncompressed
:
730 * No compression, but we still need to write the pages in the file
731 * we've been given so far. redirty the locked page if it corresponds
732 * to our extent and set things up for the async work queue to run
733 * cow_file_range to do the normal delalloc dance.
735 if (async_chunk
->locked_page
&&
736 (page_offset(async_chunk
->locked_page
) >= start
&&
737 page_offset(async_chunk
->locked_page
)) <= end
) {
738 __set_page_dirty_nobuffers(async_chunk
->locked_page
);
739 /* unlocked later on in the async handlers */
743 extent_range_redirty_for_io(inode
, start
, end
);
744 add_async_extent(async_chunk
, start
, end
- start
+ 1, 0, NULL
, 0,
745 BTRFS_COMPRESS_NONE
);
746 compressed_extents
++;
748 return compressed_extents
;
751 static void free_async_extent_pages(struct async_extent
*async_extent
)
755 if (!async_extent
->pages
)
758 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
759 WARN_ON(async_extent
->pages
[i
]->mapping
);
760 put_page(async_extent
->pages
[i
]);
762 kfree(async_extent
->pages
);
763 async_extent
->nr_pages
= 0;
764 async_extent
->pages
= NULL
;
768 * phase two of compressed writeback. This is the ordered portion
769 * of the code, which only gets called in the order the work was
770 * queued. We walk all the async extents created by compress_file_range
771 * and send them down to the disk.
773 static noinline
void submit_compressed_extents(struct async_chunk
*async_chunk
)
775 struct btrfs_inode
*inode
= BTRFS_I(async_chunk
->inode
);
776 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
777 struct async_extent
*async_extent
;
779 struct btrfs_key ins
;
780 struct extent_map
*em
;
781 struct btrfs_root
*root
= inode
->root
;
782 struct extent_io_tree
*io_tree
= &inode
->io_tree
;
786 while (!list_empty(&async_chunk
->extents
)) {
787 async_extent
= list_entry(async_chunk
->extents
.next
,
788 struct async_extent
, list
);
789 list_del(&async_extent
->list
);
792 lock_extent(io_tree
, async_extent
->start
,
793 async_extent
->start
+ async_extent
->ram_size
- 1);
794 /* did the compression code fall back to uncompressed IO? */
795 if (!async_extent
->pages
) {
796 int page_started
= 0;
797 unsigned long nr_written
= 0;
799 /* allocate blocks */
800 ret
= cow_file_range(inode
, async_chunk
->locked_page
,
802 async_extent
->start
+
803 async_extent
->ram_size
- 1,
804 &page_started
, &nr_written
, 0);
809 * if page_started, cow_file_range inserted an
810 * inline extent and took care of all the unlocking
811 * and IO for us. Otherwise, we need to submit
812 * all those pages down to the drive.
814 if (!page_started
&& !ret
)
815 extent_write_locked_range(&inode
->vfs_inode
,
817 async_extent
->start
+
818 async_extent
->ram_size
- 1,
820 else if (ret
&& async_chunk
->locked_page
)
821 unlock_page(async_chunk
->locked_page
);
827 ret
= btrfs_reserve_extent(root
, async_extent
->ram_size
,
828 async_extent
->compressed_size
,
829 async_extent
->compressed_size
,
830 0, alloc_hint
, &ins
, 1, 1);
832 free_async_extent_pages(async_extent
);
834 if (ret
== -ENOSPC
) {
835 unlock_extent(io_tree
, async_extent
->start
,
836 async_extent
->start
+
837 async_extent
->ram_size
- 1);
840 * we need to redirty the pages if we decide to
841 * fallback to uncompressed IO, otherwise we
842 * will not submit these pages down to lower
845 extent_range_redirty_for_io(&inode
->vfs_inode
,
847 async_extent
->start
+
848 async_extent
->ram_size
- 1);
855 * here we're doing allocation and writeback of the
858 em
= create_io_em(inode
, async_extent
->start
,
859 async_extent
->ram_size
, /* len */
860 async_extent
->start
, /* orig_start */
861 ins
.objectid
, /* block_start */
862 ins
.offset
, /* block_len */
863 ins
.offset
, /* orig_block_len */
864 async_extent
->ram_size
, /* ram_bytes */
865 async_extent
->compress_type
,
866 BTRFS_ORDERED_COMPRESSED
);
868 /* ret value is not necessary due to void function */
869 goto out_free_reserve
;
872 ret
= btrfs_add_ordered_extent_compress(inode
,
875 async_extent
->ram_size
,
877 BTRFS_ORDERED_COMPRESSED
,
878 async_extent
->compress_type
);
880 btrfs_drop_extent_cache(inode
, async_extent
->start
,
881 async_extent
->start
+
882 async_extent
->ram_size
- 1, 0);
883 goto out_free_reserve
;
885 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
888 * clear dirty, set writeback and unlock the pages.
890 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
891 async_extent
->start
+
892 async_extent
->ram_size
- 1,
893 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
,
894 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
896 if (btrfs_submit_compressed_write(inode
, async_extent
->start
,
897 async_extent
->ram_size
,
899 ins
.offset
, async_extent
->pages
,
900 async_extent
->nr_pages
,
901 async_chunk
->write_flags
,
902 async_chunk
->blkcg_css
)) {
903 struct page
*p
= async_extent
->pages
[0];
904 const u64 start
= async_extent
->start
;
905 const u64 end
= start
+ async_extent
->ram_size
- 1;
907 p
->mapping
= inode
->vfs_inode
.i_mapping
;
908 btrfs_writepage_endio_finish_ordered(p
, start
, end
, 0);
911 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
, 0,
914 free_async_extent_pages(async_extent
);
916 alloc_hint
= ins
.objectid
+ ins
.offset
;
922 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
923 btrfs_free_reserved_extent(fs_info
, ins
.objectid
, ins
.offset
, 1);
925 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
926 async_extent
->start
+
927 async_extent
->ram_size
- 1,
928 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
|
929 EXTENT_DELALLOC_NEW
|
930 EXTENT_DEFRAG
| EXTENT_DO_ACCOUNTING
,
931 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
932 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
|
934 free_async_extent_pages(async_extent
);
939 static u64
get_extent_allocation_hint(struct btrfs_inode
*inode
, u64 start
,
942 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
943 struct extent_map
*em
;
946 read_lock(&em_tree
->lock
);
947 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
950 * if block start isn't an actual block number then find the
951 * first block in this inode and use that as a hint. If that
952 * block is also bogus then just don't worry about it.
954 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
956 em
= search_extent_mapping(em_tree
, 0, 0);
957 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
958 alloc_hint
= em
->block_start
;
962 alloc_hint
= em
->block_start
;
966 read_unlock(&em_tree
->lock
);
972 * when extent_io.c finds a delayed allocation range in the file,
973 * the call backs end up in this code. The basic idea is to
974 * allocate extents on disk for the range, and create ordered data structs
975 * in ram to track those extents.
977 * locked_page is the page that writepage had locked already. We use
978 * it to make sure we don't do extra locks or unlocks.
980 * *page_started is set to one if we unlock locked_page and do everything
981 * required to start IO on it. It may be clean and already done with
984 static noinline
int cow_file_range(struct btrfs_inode
*inode
,
985 struct page
*locked_page
,
986 u64 start
, u64 end
, int *page_started
,
987 unsigned long *nr_written
, int unlock
)
989 struct btrfs_root
*root
= inode
->root
;
990 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
993 unsigned long ram_size
;
994 u64 cur_alloc_size
= 0;
996 u64 blocksize
= fs_info
->sectorsize
;
997 struct btrfs_key ins
;
998 struct extent_map
*em
;
1000 unsigned long page_ops
;
1001 bool extent_reserved
= false;
1004 if (btrfs_is_free_space_inode(inode
)) {
1010 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
1011 num_bytes
= max(blocksize
, num_bytes
);
1012 ASSERT(num_bytes
<= btrfs_super_total_bytes(fs_info
->super_copy
));
1014 inode_should_defrag(inode
, start
, end
, num_bytes
, SZ_64K
);
1017 /* lets try to make an inline extent */
1018 ret
= cow_file_range_inline(inode
, start
, end
, 0,
1019 BTRFS_COMPRESS_NONE
, NULL
);
1022 * We use DO_ACCOUNTING here because we need the
1023 * delalloc_release_metadata to be run _after_ we drop
1024 * our outstanding extent for clearing delalloc for this
1027 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
,
1028 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1029 EXTENT_DELALLOC_NEW
| EXTENT_DEFRAG
|
1030 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1031 PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
1032 PAGE_END_WRITEBACK
);
1033 *nr_written
= *nr_written
+
1034 (end
- start
+ PAGE_SIZE
) / PAGE_SIZE
;
1037 } else if (ret
< 0) {
1042 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
1043 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
1046 * Relocation relies on the relocated extents to have exactly the same
1047 * size as the original extents. Normally writeback for relocation data
1048 * extents follows a NOCOW path because relocation preallocates the
1049 * extents. However, due to an operation such as scrub turning a block
1050 * group to RO mode, it may fallback to COW mode, so we must make sure
1051 * an extent allocated during COW has exactly the requested size and can
1052 * not be split into smaller extents, otherwise relocation breaks and
1053 * fails during the stage where it updates the bytenr of file extent
1056 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1057 min_alloc_size
= num_bytes
;
1059 min_alloc_size
= fs_info
->sectorsize
;
1061 while (num_bytes
> 0) {
1062 cur_alloc_size
= num_bytes
;
1063 ret
= btrfs_reserve_extent(root
, cur_alloc_size
, cur_alloc_size
,
1064 min_alloc_size
, 0, alloc_hint
,
1068 cur_alloc_size
= ins
.offset
;
1069 extent_reserved
= true;
1071 ram_size
= ins
.offset
;
1072 em
= create_io_em(inode
, start
, ins
.offset
, /* len */
1073 start
, /* orig_start */
1074 ins
.objectid
, /* block_start */
1075 ins
.offset
, /* block_len */
1076 ins
.offset
, /* orig_block_len */
1077 ram_size
, /* ram_bytes */
1078 BTRFS_COMPRESS_NONE
, /* compress_type */
1079 BTRFS_ORDERED_REGULAR
/* type */);
1084 free_extent_map(em
);
1086 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
1087 ram_size
, cur_alloc_size
, 0);
1089 goto out_drop_extent_cache
;
1091 if (root
->root_key
.objectid
==
1092 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1093 ret
= btrfs_reloc_clone_csums(inode
, start
,
1096 * Only drop cache here, and process as normal.
1098 * We must not allow extent_clear_unlock_delalloc()
1099 * at out_unlock label to free meta of this ordered
1100 * extent, as its meta should be freed by
1101 * btrfs_finish_ordered_io().
1103 * So we must continue until @start is increased to
1104 * skip current ordered extent.
1107 btrfs_drop_extent_cache(inode
, start
,
1108 start
+ ram_size
- 1, 0);
1111 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
1113 /* we're not doing compressed IO, don't unlock the first
1114 * page (which the caller expects to stay locked), don't
1115 * clear any dirty bits and don't set any writeback bits
1117 * Do set the Private2 bit so we know this page was properly
1118 * setup for writepage
1120 page_ops
= unlock
? PAGE_UNLOCK
: 0;
1121 page_ops
|= PAGE_SET_PRIVATE2
;
1123 extent_clear_unlock_delalloc(inode
, start
, start
+ ram_size
- 1,
1125 EXTENT_LOCKED
| EXTENT_DELALLOC
,
1127 if (num_bytes
< cur_alloc_size
)
1130 num_bytes
-= cur_alloc_size
;
1131 alloc_hint
= ins
.objectid
+ ins
.offset
;
1132 start
+= cur_alloc_size
;
1133 extent_reserved
= false;
1136 * btrfs_reloc_clone_csums() error, since start is increased
1137 * extent_clear_unlock_delalloc() at out_unlock label won't
1138 * free metadata of current ordered extent, we're OK to exit.
1146 out_drop_extent_cache
:
1147 btrfs_drop_extent_cache(inode
, start
, start
+ ram_size
- 1, 0);
1149 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
1150 btrfs_free_reserved_extent(fs_info
, ins
.objectid
, ins
.offset
, 1);
1152 clear_bits
= EXTENT_LOCKED
| EXTENT_DELALLOC
| EXTENT_DELALLOC_NEW
|
1153 EXTENT_DEFRAG
| EXTENT_CLEAR_META_RESV
;
1154 page_ops
= PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
1157 * If we reserved an extent for our delalloc range (or a subrange) and
1158 * failed to create the respective ordered extent, then it means that
1159 * when we reserved the extent we decremented the extent's size from
1160 * the data space_info's bytes_may_use counter and incremented the
1161 * space_info's bytes_reserved counter by the same amount. We must make
1162 * sure extent_clear_unlock_delalloc() does not try to decrement again
1163 * the data space_info's bytes_may_use counter, therefore we do not pass
1164 * it the flag EXTENT_CLEAR_DATA_RESV.
1166 if (extent_reserved
) {
1167 extent_clear_unlock_delalloc(inode
, start
,
1168 start
+ cur_alloc_size
- 1,
1172 start
+= cur_alloc_size
;
1176 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1177 clear_bits
| EXTENT_CLEAR_DATA_RESV
,
1183 * work queue call back to started compression on a file and pages
1185 static noinline
void async_cow_start(struct btrfs_work
*work
)
1187 struct async_chunk
*async_chunk
;
1188 int compressed_extents
;
1190 async_chunk
= container_of(work
, struct async_chunk
, work
);
1192 compressed_extents
= compress_file_range(async_chunk
);
1193 if (compressed_extents
== 0) {
1194 btrfs_add_delayed_iput(async_chunk
->inode
);
1195 async_chunk
->inode
= NULL
;
1200 * work queue call back to submit previously compressed pages
1202 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1204 struct async_chunk
*async_chunk
= container_of(work
, struct async_chunk
,
1206 struct btrfs_fs_info
*fs_info
= btrfs_work_owner(work
);
1207 unsigned long nr_pages
;
1209 nr_pages
= (async_chunk
->end
- async_chunk
->start
+ PAGE_SIZE
) >>
1212 /* atomic_sub_return implies a barrier */
1213 if (atomic_sub_return(nr_pages
, &fs_info
->async_delalloc_pages
) <
1215 cond_wake_up_nomb(&fs_info
->async_submit_wait
);
1218 * ->inode could be NULL if async_chunk_start has failed to compress,
1219 * in which case we don't have anything to submit, yet we need to
1220 * always adjust ->async_delalloc_pages as its paired with the init
1221 * happening in cow_file_range_async
1223 if (async_chunk
->inode
)
1224 submit_compressed_extents(async_chunk
);
1227 static noinline
void async_cow_free(struct btrfs_work
*work
)
1229 struct async_chunk
*async_chunk
;
1231 async_chunk
= container_of(work
, struct async_chunk
, work
);
1232 if (async_chunk
->inode
)
1233 btrfs_add_delayed_iput(async_chunk
->inode
);
1234 if (async_chunk
->blkcg_css
)
1235 css_put(async_chunk
->blkcg_css
);
1237 * Since the pointer to 'pending' is at the beginning of the array of
1238 * async_chunk's, freeing it ensures the whole array has been freed.
1240 if (atomic_dec_and_test(async_chunk
->pending
))
1241 kvfree(async_chunk
->pending
);
1244 static int cow_file_range_async(struct btrfs_inode
*inode
,
1245 struct writeback_control
*wbc
,
1246 struct page
*locked_page
,
1247 u64 start
, u64 end
, int *page_started
,
1248 unsigned long *nr_written
)
1250 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
1251 struct cgroup_subsys_state
*blkcg_css
= wbc_blkcg_css(wbc
);
1252 struct async_cow
*ctx
;
1253 struct async_chunk
*async_chunk
;
1254 unsigned long nr_pages
;
1256 u64 num_chunks
= DIV_ROUND_UP(end
- start
, SZ_512K
);
1258 bool should_compress
;
1260 const unsigned int write_flags
= wbc_to_write_flags(wbc
);
1262 unlock_extent(&inode
->io_tree
, start
, end
);
1264 if (inode
->flags
& BTRFS_INODE_NOCOMPRESS
&&
1265 !btrfs_test_opt(fs_info
, FORCE_COMPRESS
)) {
1267 should_compress
= false;
1269 should_compress
= true;
1272 nofs_flag
= memalloc_nofs_save();
1273 ctx
= kvmalloc(struct_size(ctx
, chunks
, num_chunks
), GFP_KERNEL
);
1274 memalloc_nofs_restore(nofs_flag
);
1277 unsigned clear_bits
= EXTENT_LOCKED
| EXTENT_DELALLOC
|
1278 EXTENT_DELALLOC_NEW
| EXTENT_DEFRAG
|
1279 EXTENT_DO_ACCOUNTING
;
1280 unsigned long page_ops
= PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
1281 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
|
1284 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1285 clear_bits
, page_ops
);
1289 async_chunk
= ctx
->chunks
;
1290 atomic_set(&ctx
->num_chunks
, num_chunks
);
1292 for (i
= 0; i
< num_chunks
; i
++) {
1293 if (should_compress
)
1294 cur_end
= min(end
, start
+ SZ_512K
- 1);
1299 * igrab is called higher up in the call chain, take only the
1300 * lightweight reference for the callback lifetime
1302 ihold(&inode
->vfs_inode
);
1303 async_chunk
[i
].pending
= &ctx
->num_chunks
;
1304 async_chunk
[i
].inode
= &inode
->vfs_inode
;
1305 async_chunk
[i
].start
= start
;
1306 async_chunk
[i
].end
= cur_end
;
1307 async_chunk
[i
].write_flags
= write_flags
;
1308 INIT_LIST_HEAD(&async_chunk
[i
].extents
);
1311 * The locked_page comes all the way from writepage and its
1312 * the original page we were actually given. As we spread
1313 * this large delalloc region across multiple async_chunk
1314 * structs, only the first struct needs a pointer to locked_page
1316 * This way we don't need racey decisions about who is supposed
1321 * Depending on the compressibility, the pages might or
1322 * might not go through async. We want all of them to
1323 * be accounted against wbc once. Let's do it here
1324 * before the paths diverge. wbc accounting is used
1325 * only for foreign writeback detection and doesn't
1326 * need full accuracy. Just account the whole thing
1327 * against the first page.
1329 wbc_account_cgroup_owner(wbc
, locked_page
,
1331 async_chunk
[i
].locked_page
= locked_page
;
1334 async_chunk
[i
].locked_page
= NULL
;
1337 if (blkcg_css
!= blkcg_root_css
) {
1339 async_chunk
[i
].blkcg_css
= blkcg_css
;
1341 async_chunk
[i
].blkcg_css
= NULL
;
1344 btrfs_init_work(&async_chunk
[i
].work
, async_cow_start
,
1345 async_cow_submit
, async_cow_free
);
1347 nr_pages
= DIV_ROUND_UP(cur_end
- start
, PAGE_SIZE
);
1348 atomic_add(nr_pages
, &fs_info
->async_delalloc_pages
);
1350 btrfs_queue_work(fs_info
->delalloc_workers
, &async_chunk
[i
].work
);
1352 *nr_written
+= nr_pages
;
1353 start
= cur_end
+ 1;
1359 static noinline
int csum_exist_in_range(struct btrfs_fs_info
*fs_info
,
1360 u64 bytenr
, u64 num_bytes
)
1363 struct btrfs_ordered_sum
*sums
;
1366 ret
= btrfs_lookup_csums_range(fs_info
->csum_root
, bytenr
,
1367 bytenr
+ num_bytes
- 1, &list
, 0);
1368 if (ret
== 0 && list_empty(&list
))
1371 while (!list_empty(&list
)) {
1372 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1373 list_del(&sums
->list
);
1381 static int fallback_to_cow(struct btrfs_inode
*inode
, struct page
*locked_page
,
1382 const u64 start
, const u64 end
,
1383 int *page_started
, unsigned long *nr_written
)
1385 const bool is_space_ino
= btrfs_is_free_space_inode(inode
);
1386 const bool is_reloc_ino
= (inode
->root
->root_key
.objectid
==
1387 BTRFS_DATA_RELOC_TREE_OBJECTID
);
1388 const u64 range_bytes
= end
+ 1 - start
;
1389 struct extent_io_tree
*io_tree
= &inode
->io_tree
;
1390 u64 range_start
= start
;
1394 * If EXTENT_NORESERVE is set it means that when the buffered write was
1395 * made we had not enough available data space and therefore we did not
1396 * reserve data space for it, since we though we could do NOCOW for the
1397 * respective file range (either there is prealloc extent or the inode
1398 * has the NOCOW bit set).
1400 * However when we need to fallback to COW mode (because for example the
1401 * block group for the corresponding extent was turned to RO mode by a
1402 * scrub or relocation) we need to do the following:
1404 * 1) We increment the bytes_may_use counter of the data space info.
1405 * If COW succeeds, it allocates a new data extent and after doing
1406 * that it decrements the space info's bytes_may_use counter and
1407 * increments its bytes_reserved counter by the same amount (we do
1408 * this at btrfs_add_reserved_bytes()). So we need to increment the
1409 * bytes_may_use counter to compensate (when space is reserved at
1410 * buffered write time, the bytes_may_use counter is incremented);
1412 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1413 * that if the COW path fails for any reason, it decrements (through
1414 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1415 * data space info, which we incremented in the step above.
1417 * If we need to fallback to cow and the inode corresponds to a free
1418 * space cache inode or an inode of the data relocation tree, we must
1419 * also increment bytes_may_use of the data space_info for the same
1420 * reason. Space caches and relocated data extents always get a prealloc
1421 * extent for them, however scrub or balance may have set the block
1422 * group that contains that extent to RO mode and therefore force COW
1423 * when starting writeback.
1425 count
= count_range_bits(io_tree
, &range_start
, end
, range_bytes
,
1426 EXTENT_NORESERVE
, 0);
1427 if (count
> 0 || is_space_ino
|| is_reloc_ino
) {
1429 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
1430 struct btrfs_space_info
*sinfo
= fs_info
->data_sinfo
;
1432 if (is_space_ino
|| is_reloc_ino
)
1433 bytes
= range_bytes
;
1435 spin_lock(&sinfo
->lock
);
1436 btrfs_space_info_update_bytes_may_use(fs_info
, sinfo
, bytes
);
1437 spin_unlock(&sinfo
->lock
);
1440 clear_extent_bit(io_tree
, start
, end
, EXTENT_NORESERVE
,
1444 return cow_file_range(inode
, locked_page
, start
, end
, page_started
,
1449 * when nowcow writeback call back. This checks for snapshots or COW copies
1450 * of the extents that exist in the file, and COWs the file as required.
1452 * If no cow copies or snapshots exist, we write directly to the existing
1455 static noinline
int run_delalloc_nocow(struct btrfs_inode
*inode
,
1456 struct page
*locked_page
,
1457 const u64 start
, const u64 end
,
1458 int *page_started
, int force
,
1459 unsigned long *nr_written
)
1461 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
1462 struct btrfs_root
*root
= inode
->root
;
1463 struct btrfs_path
*path
;
1464 u64 cow_start
= (u64
)-1;
1465 u64 cur_offset
= start
;
1467 bool check_prev
= true;
1468 const bool freespace_inode
= btrfs_is_free_space_inode(inode
);
1469 u64 ino
= btrfs_ino(inode
);
1471 u64 disk_bytenr
= 0;
1473 path
= btrfs_alloc_path();
1475 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1476 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1477 EXTENT_DO_ACCOUNTING
|
1478 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1480 PAGE_SET_WRITEBACK
|
1481 PAGE_END_WRITEBACK
);
1486 struct btrfs_key found_key
;
1487 struct btrfs_file_extent_item
*fi
;
1488 struct extent_buffer
*leaf
;
1498 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, ino
,
1504 * If there is no extent for our range when doing the initial
1505 * search, then go back to the previous slot as it will be the
1506 * one containing the search offset
1508 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1509 leaf
= path
->nodes
[0];
1510 btrfs_item_key_to_cpu(leaf
, &found_key
,
1511 path
->slots
[0] - 1);
1512 if (found_key
.objectid
== ino
&&
1513 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1518 /* Go to next leaf if we have exhausted the current one */
1519 leaf
= path
->nodes
[0];
1520 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1521 ret
= btrfs_next_leaf(root
, path
);
1523 if (cow_start
!= (u64
)-1)
1524 cur_offset
= cow_start
;
1529 leaf
= path
->nodes
[0];
1532 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1534 /* Didn't find anything for our INO */
1535 if (found_key
.objectid
> ino
)
1538 * Keep searching until we find an EXTENT_ITEM or there are no
1539 * more extents for this inode
1541 if (WARN_ON_ONCE(found_key
.objectid
< ino
) ||
1542 found_key
.type
< BTRFS_EXTENT_DATA_KEY
) {
1547 /* Found key is not EXTENT_DATA_KEY or starts after req range */
1548 if (found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1549 found_key
.offset
> end
)
1553 * If the found extent starts after requested offset, then
1554 * adjust extent_end to be right before this extent begins
1556 if (found_key
.offset
> cur_offset
) {
1557 extent_end
= found_key
.offset
;
1563 * Found extent which begins before our range and potentially
1566 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1567 struct btrfs_file_extent_item
);
1568 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1570 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1571 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1572 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1573 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1574 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1575 extent_end
= found_key
.offset
+
1576 btrfs_file_extent_num_bytes(leaf
, fi
);
1578 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1580 * If the extent we got ends before our current offset,
1581 * skip to the next extent.
1583 if (extent_end
<= cur_offset
) {
1588 if (disk_bytenr
== 0)
1590 /* Skip compressed/encrypted/encoded extents */
1591 if (btrfs_file_extent_compression(leaf
, fi
) ||
1592 btrfs_file_extent_encryption(leaf
, fi
) ||
1593 btrfs_file_extent_other_encoding(leaf
, fi
))
1596 * If extent is created before the last volume's snapshot
1597 * this implies the extent is shared, hence we can't do
1598 * nocow. This is the same check as in
1599 * btrfs_cross_ref_exist but without calling
1600 * btrfs_search_slot.
1602 if (!freespace_inode
&&
1603 btrfs_file_extent_generation(leaf
, fi
) <=
1604 btrfs_root_last_snapshot(&root
->root_item
))
1606 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1608 /* If extent is RO, we must COW it */
1609 if (btrfs_extent_readonly(fs_info
, disk_bytenr
))
1611 ret
= btrfs_cross_ref_exist(root
, ino
,
1613 extent_offset
, disk_bytenr
);
1616 * ret could be -EIO if the above fails to read
1620 if (cow_start
!= (u64
)-1)
1621 cur_offset
= cow_start
;
1625 WARN_ON_ONCE(freespace_inode
);
1628 disk_bytenr
+= extent_offset
;
1629 disk_bytenr
+= cur_offset
- found_key
.offset
;
1630 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1632 * If there are pending snapshots for this root, we
1633 * fall into common COW way
1635 if (!freespace_inode
&& atomic_read(&root
->snapshot_force_cow
))
1638 * force cow if csum exists in the range.
1639 * this ensure that csum for a given extent are
1640 * either valid or do not exist.
1642 ret
= csum_exist_in_range(fs_info
, disk_bytenr
,
1646 * ret could be -EIO if the above fails to read
1650 if (cow_start
!= (u64
)-1)
1651 cur_offset
= cow_start
;
1654 WARN_ON_ONCE(freespace_inode
);
1657 if (!btrfs_inc_nocow_writers(fs_info
, disk_bytenr
))
1660 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1661 extent_end
= found_key
.offset
+ ram_bytes
;
1662 extent_end
= ALIGN(extent_end
, fs_info
->sectorsize
);
1663 /* Skip extents outside of our requested range */
1664 if (extent_end
<= start
) {
1669 /* If this triggers then we have a memory corruption */
1674 * If nocow is false then record the beginning of the range
1675 * that needs to be COWed
1678 if (cow_start
== (u64
)-1)
1679 cow_start
= cur_offset
;
1680 cur_offset
= extent_end
;
1681 if (cur_offset
> end
)
1687 btrfs_release_path(path
);
1690 * COW range from cow_start to found_key.offset - 1. As the key
1691 * will contain the beginning of the first extent that can be
1692 * NOCOW, following one which needs to be COW'ed
1694 if (cow_start
!= (u64
)-1) {
1695 ret
= fallback_to_cow(inode
, locked_page
,
1696 cow_start
, found_key
.offset
- 1,
1697 page_started
, nr_written
);
1700 cow_start
= (u64
)-1;
1703 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1704 u64 orig_start
= found_key
.offset
- extent_offset
;
1705 struct extent_map
*em
;
1707 em
= create_io_em(inode
, cur_offset
, num_bytes
,
1709 disk_bytenr
, /* block_start */
1710 num_bytes
, /* block_len */
1711 disk_num_bytes
, /* orig_block_len */
1712 ram_bytes
, BTRFS_COMPRESS_NONE
,
1713 BTRFS_ORDERED_PREALLOC
);
1718 free_extent_map(em
);
1719 ret
= btrfs_add_ordered_extent(inode
, cur_offset
,
1720 disk_bytenr
, num_bytes
,
1722 BTRFS_ORDERED_PREALLOC
);
1724 btrfs_drop_extent_cache(inode
, cur_offset
,
1725 cur_offset
+ num_bytes
- 1,
1730 ret
= btrfs_add_ordered_extent(inode
, cur_offset
,
1731 disk_bytenr
, num_bytes
,
1733 BTRFS_ORDERED_NOCOW
);
1739 btrfs_dec_nocow_writers(fs_info
, disk_bytenr
);
1742 if (root
->root_key
.objectid
==
1743 BTRFS_DATA_RELOC_TREE_OBJECTID
)
1745 * Error handled later, as we must prevent
1746 * extent_clear_unlock_delalloc() in error handler
1747 * from freeing metadata of created ordered extent.
1749 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1752 extent_clear_unlock_delalloc(inode
, cur_offset
,
1753 cur_offset
+ num_bytes
- 1,
1754 locked_page
, EXTENT_LOCKED
|
1756 EXTENT_CLEAR_DATA_RESV
,
1757 PAGE_UNLOCK
| PAGE_SET_PRIVATE2
);
1759 cur_offset
= extent_end
;
1762 * btrfs_reloc_clone_csums() error, now we're OK to call error
1763 * handler, as metadata for created ordered extent will only
1764 * be freed by btrfs_finish_ordered_io().
1768 if (cur_offset
> end
)
1771 btrfs_release_path(path
);
1773 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1774 cow_start
= cur_offset
;
1776 if (cow_start
!= (u64
)-1) {
1778 ret
= fallback_to_cow(inode
, locked_page
, cow_start
, end
,
1779 page_started
, nr_written
);
1786 btrfs_dec_nocow_writers(fs_info
, disk_bytenr
);
1788 if (ret
&& cur_offset
< end
)
1789 extent_clear_unlock_delalloc(inode
, cur_offset
, end
,
1790 locked_page
, EXTENT_LOCKED
|
1791 EXTENT_DELALLOC
| EXTENT_DEFRAG
|
1792 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1794 PAGE_SET_WRITEBACK
|
1795 PAGE_END_WRITEBACK
);
1796 btrfs_free_path(path
);
1800 static inline int need_force_cow(struct btrfs_inode
*inode
, u64 start
, u64 end
)
1803 if (!(inode
->flags
& BTRFS_INODE_NODATACOW
) &&
1804 !(inode
->flags
& BTRFS_INODE_PREALLOC
))
1808 * @defrag_bytes is a hint value, no spinlock held here,
1809 * if is not zero, it means the file is defragging.
1810 * Force cow if given extent needs to be defragged.
1812 if (inode
->defrag_bytes
&&
1813 test_range_bit(&inode
->io_tree
, start
, end
, EXTENT_DEFRAG
, 0, NULL
))
1820 * Function to process delayed allocation (create CoW) for ranges which are
1821 * being touched for the first time.
1823 int btrfs_run_delalloc_range(struct btrfs_inode
*inode
, struct page
*locked_page
,
1824 u64 start
, u64 end
, int *page_started
, unsigned long *nr_written
,
1825 struct writeback_control
*wbc
)
1828 int force_cow
= need_force_cow(inode
, start
, end
);
1830 if (inode
->flags
& BTRFS_INODE_NODATACOW
&& !force_cow
) {
1831 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1832 page_started
, 1, nr_written
);
1833 } else if (inode
->flags
& BTRFS_INODE_PREALLOC
&& !force_cow
) {
1834 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1835 page_started
, 0, nr_written
);
1836 } else if (!inode_can_compress(inode
) ||
1837 !inode_need_compress(inode
, start
, end
)) {
1838 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1839 page_started
, nr_written
, 1);
1841 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
, &inode
->runtime_flags
);
1842 ret
= cow_file_range_async(inode
, wbc
, locked_page
, start
, end
,
1843 page_started
, nr_written
);
1846 btrfs_cleanup_ordered_extents(inode
, locked_page
, start
,
1851 void btrfs_split_delalloc_extent(struct inode
*inode
,
1852 struct extent_state
*orig
, u64 split
)
1856 /* not delalloc, ignore it */
1857 if (!(orig
->state
& EXTENT_DELALLOC
))
1860 size
= orig
->end
- orig
->start
+ 1;
1861 if (size
> BTRFS_MAX_EXTENT_SIZE
) {
1866 * See the explanation in btrfs_merge_delalloc_extent, the same
1867 * applies here, just in reverse.
1869 new_size
= orig
->end
- split
+ 1;
1870 num_extents
= count_max_extents(new_size
);
1871 new_size
= split
- orig
->start
;
1872 num_extents
+= count_max_extents(new_size
);
1873 if (count_max_extents(size
) >= num_extents
)
1877 spin_lock(&BTRFS_I(inode
)->lock
);
1878 btrfs_mod_outstanding_extents(BTRFS_I(inode
), 1);
1879 spin_unlock(&BTRFS_I(inode
)->lock
);
1883 * Handle merged delayed allocation extents so we can keep track of new extents
1884 * that are just merged onto old extents, such as when we are doing sequential
1885 * writes, so we can properly account for the metadata space we'll need.
1887 void btrfs_merge_delalloc_extent(struct inode
*inode
, struct extent_state
*new,
1888 struct extent_state
*other
)
1890 u64 new_size
, old_size
;
1893 /* not delalloc, ignore it */
1894 if (!(other
->state
& EXTENT_DELALLOC
))
1897 if (new->start
> other
->start
)
1898 new_size
= new->end
- other
->start
+ 1;
1900 new_size
= other
->end
- new->start
+ 1;
1902 /* we're not bigger than the max, unreserve the space and go */
1903 if (new_size
<= BTRFS_MAX_EXTENT_SIZE
) {
1904 spin_lock(&BTRFS_I(inode
)->lock
);
1905 btrfs_mod_outstanding_extents(BTRFS_I(inode
), -1);
1906 spin_unlock(&BTRFS_I(inode
)->lock
);
1911 * We have to add up either side to figure out how many extents were
1912 * accounted for before we merged into one big extent. If the number of
1913 * extents we accounted for is <= the amount we need for the new range
1914 * then we can return, otherwise drop. Think of it like this
1918 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1919 * need 2 outstanding extents, on one side we have 1 and the other side
1920 * we have 1 so they are == and we can return. But in this case
1922 * [MAX_SIZE+4k][MAX_SIZE+4k]
1924 * Each range on their own accounts for 2 extents, but merged together
1925 * they are only 3 extents worth of accounting, so we need to drop in
1928 old_size
= other
->end
- other
->start
+ 1;
1929 num_extents
= count_max_extents(old_size
);
1930 old_size
= new->end
- new->start
+ 1;
1931 num_extents
+= count_max_extents(old_size
);
1932 if (count_max_extents(new_size
) >= num_extents
)
1935 spin_lock(&BTRFS_I(inode
)->lock
);
1936 btrfs_mod_outstanding_extents(BTRFS_I(inode
), -1);
1937 spin_unlock(&BTRFS_I(inode
)->lock
);
1940 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1941 struct inode
*inode
)
1943 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1945 spin_lock(&root
->delalloc_lock
);
1946 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1947 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1948 &root
->delalloc_inodes
);
1949 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1950 &BTRFS_I(inode
)->runtime_flags
);
1951 root
->nr_delalloc_inodes
++;
1952 if (root
->nr_delalloc_inodes
== 1) {
1953 spin_lock(&fs_info
->delalloc_root_lock
);
1954 BUG_ON(!list_empty(&root
->delalloc_root
));
1955 list_add_tail(&root
->delalloc_root
,
1956 &fs_info
->delalloc_roots
);
1957 spin_unlock(&fs_info
->delalloc_root_lock
);
1960 spin_unlock(&root
->delalloc_lock
);
1964 void __btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1965 struct btrfs_inode
*inode
)
1967 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1969 if (!list_empty(&inode
->delalloc_inodes
)) {
1970 list_del_init(&inode
->delalloc_inodes
);
1971 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1972 &inode
->runtime_flags
);
1973 root
->nr_delalloc_inodes
--;
1974 if (!root
->nr_delalloc_inodes
) {
1975 ASSERT(list_empty(&root
->delalloc_inodes
));
1976 spin_lock(&fs_info
->delalloc_root_lock
);
1977 BUG_ON(list_empty(&root
->delalloc_root
));
1978 list_del_init(&root
->delalloc_root
);
1979 spin_unlock(&fs_info
->delalloc_root_lock
);
1984 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1985 struct btrfs_inode
*inode
)
1987 spin_lock(&root
->delalloc_lock
);
1988 __btrfs_del_delalloc_inode(root
, inode
);
1989 spin_unlock(&root
->delalloc_lock
);
1993 * Properly track delayed allocation bytes in the inode and to maintain the
1994 * list of inodes that have pending delalloc work to be done.
1996 void btrfs_set_delalloc_extent(struct inode
*inode
, struct extent_state
*state
,
1999 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2001 if ((*bits
& EXTENT_DEFRAG
) && !(*bits
& EXTENT_DELALLOC
))
2004 * set_bit and clear bit hooks normally require _irqsave/restore
2005 * but in this case, we are only testing for the DELALLOC
2006 * bit, which is only set or cleared with irqs on
2008 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
2009 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2010 u64 len
= state
->end
+ 1 - state
->start
;
2011 u32 num_extents
= count_max_extents(len
);
2012 bool do_list
= !btrfs_is_free_space_inode(BTRFS_I(inode
));
2014 spin_lock(&BTRFS_I(inode
)->lock
);
2015 btrfs_mod_outstanding_extents(BTRFS_I(inode
), num_extents
);
2016 spin_unlock(&BTRFS_I(inode
)->lock
);
2018 /* For sanity tests */
2019 if (btrfs_is_testing(fs_info
))
2022 percpu_counter_add_batch(&fs_info
->delalloc_bytes
, len
,
2023 fs_info
->delalloc_batch
);
2024 spin_lock(&BTRFS_I(inode
)->lock
);
2025 BTRFS_I(inode
)->delalloc_bytes
+= len
;
2026 if (*bits
& EXTENT_DEFRAG
)
2027 BTRFS_I(inode
)->defrag_bytes
+= len
;
2028 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
2029 &BTRFS_I(inode
)->runtime_flags
))
2030 btrfs_add_delalloc_inodes(root
, inode
);
2031 spin_unlock(&BTRFS_I(inode
)->lock
);
2034 if (!(state
->state
& EXTENT_DELALLOC_NEW
) &&
2035 (*bits
& EXTENT_DELALLOC_NEW
)) {
2036 spin_lock(&BTRFS_I(inode
)->lock
);
2037 BTRFS_I(inode
)->new_delalloc_bytes
+= state
->end
+ 1 -
2039 spin_unlock(&BTRFS_I(inode
)->lock
);
2044 * Once a range is no longer delalloc this function ensures that proper
2045 * accounting happens.
2047 void btrfs_clear_delalloc_extent(struct inode
*vfs_inode
,
2048 struct extent_state
*state
, unsigned *bits
)
2050 struct btrfs_inode
*inode
= BTRFS_I(vfs_inode
);
2051 struct btrfs_fs_info
*fs_info
= btrfs_sb(vfs_inode
->i_sb
);
2052 u64 len
= state
->end
+ 1 - state
->start
;
2053 u32 num_extents
= count_max_extents(len
);
2055 if ((state
->state
& EXTENT_DEFRAG
) && (*bits
& EXTENT_DEFRAG
)) {
2056 spin_lock(&inode
->lock
);
2057 inode
->defrag_bytes
-= len
;
2058 spin_unlock(&inode
->lock
);
2062 * set_bit and clear bit hooks normally require _irqsave/restore
2063 * but in this case, we are only testing for the DELALLOC
2064 * bit, which is only set or cleared with irqs on
2066 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
2067 struct btrfs_root
*root
= inode
->root
;
2068 bool do_list
= !btrfs_is_free_space_inode(inode
);
2070 spin_lock(&inode
->lock
);
2071 btrfs_mod_outstanding_extents(inode
, -num_extents
);
2072 spin_unlock(&inode
->lock
);
2075 * We don't reserve metadata space for space cache inodes so we
2076 * don't need to call delalloc_release_metadata if there is an
2079 if (*bits
& EXTENT_CLEAR_META_RESV
&&
2080 root
!= fs_info
->tree_root
)
2081 btrfs_delalloc_release_metadata(inode
, len
, false);
2083 /* For sanity tests. */
2084 if (btrfs_is_testing(fs_info
))
2087 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2088 do_list
&& !(state
->state
& EXTENT_NORESERVE
) &&
2089 (*bits
& EXTENT_CLEAR_DATA_RESV
))
2090 btrfs_free_reserved_data_space_noquota(fs_info
, len
);
2092 percpu_counter_add_batch(&fs_info
->delalloc_bytes
, -len
,
2093 fs_info
->delalloc_batch
);
2094 spin_lock(&inode
->lock
);
2095 inode
->delalloc_bytes
-= len
;
2096 if (do_list
&& inode
->delalloc_bytes
== 0 &&
2097 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
2098 &inode
->runtime_flags
))
2099 btrfs_del_delalloc_inode(root
, inode
);
2100 spin_unlock(&inode
->lock
);
2103 if ((state
->state
& EXTENT_DELALLOC_NEW
) &&
2104 (*bits
& EXTENT_DELALLOC_NEW
)) {
2105 spin_lock(&inode
->lock
);
2106 ASSERT(inode
->new_delalloc_bytes
>= len
);
2107 inode
->new_delalloc_bytes
-= len
;
2108 spin_unlock(&inode
->lock
);
2113 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2114 * in a chunk's stripe. This function ensures that bios do not span a
2117 * @page - The page we are about to add to the bio
2118 * @size - size we want to add to the bio
2119 * @bio - bio we want to ensure is smaller than a stripe
2120 * @bio_flags - flags of the bio
2122 * return 1 if page cannot be added to the bio
2123 * return 0 if page can be added to the bio
2124 * return error otherwise
2126 int btrfs_bio_fits_in_stripe(struct page
*page
, size_t size
, struct bio
*bio
,
2127 unsigned long bio_flags
)
2129 struct inode
*inode
= page
->mapping
->host
;
2130 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2131 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
2135 struct btrfs_io_geometry geom
;
2137 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
2140 length
= bio
->bi_iter
.bi_size
;
2141 map_length
= length
;
2142 ret
= btrfs_get_io_geometry(fs_info
, btrfs_op(bio
), logical
, map_length
,
2147 if (geom
.len
< length
+ size
)
2153 * in order to insert checksums into the metadata in large chunks,
2154 * we wait until bio submission time. All the pages in the bio are
2155 * checksummed and sums are attached onto the ordered extent record.
2157 * At IO completion time the cums attached on the ordered extent record
2158 * are inserted into the btree
2160 static blk_status_t
btrfs_submit_bio_start(void *private_data
, struct bio
*bio
,
2163 struct inode
*inode
= private_data
;
2164 blk_status_t ret
= 0;
2166 ret
= btrfs_csum_one_bio(BTRFS_I(inode
), bio
, 0, 0);
2167 BUG_ON(ret
); /* -ENOMEM */
2172 * extent_io.c submission hook. This does the right thing for csum calculation
2173 * on write, or reading the csums from the tree before a read.
2175 * Rules about async/sync submit,
2176 * a) read: sync submit
2178 * b) write without checksum: sync submit
2180 * c) write with checksum:
2181 * c-1) if bio is issued by fsync: sync submit
2182 * (sync_writers != 0)
2184 * c-2) if root is reloc root: sync submit
2185 * (only in case of buffered IO)
2187 * c-3) otherwise: async submit
2189 static blk_status_t
btrfs_submit_bio_hook(struct inode
*inode
, struct bio
*bio
,
2191 unsigned long bio_flags
)
2194 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2195 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2196 enum btrfs_wq_endio_type metadata
= BTRFS_WQ_ENDIO_DATA
;
2197 blk_status_t ret
= 0;
2199 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
2201 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
2203 if (btrfs_is_free_space_inode(BTRFS_I(inode
)))
2204 metadata
= BTRFS_WQ_ENDIO_FREE_SPACE
;
2206 if (bio_op(bio
) != REQ_OP_WRITE
) {
2207 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, metadata
);
2211 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
2212 ret
= btrfs_submit_compressed_read(inode
, bio
,
2216 } else if (!skip_sum
) {
2217 ret
= btrfs_lookup_bio_sums(inode
, bio
, (u64
)-1, NULL
);
2222 } else if (async
&& !skip_sum
) {
2223 /* csum items have already been cloned */
2224 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
2226 /* we're doing a write, do the async checksumming */
2227 ret
= btrfs_wq_submit_bio(fs_info
, bio
, mirror_num
, bio_flags
,
2228 0, inode
, btrfs_submit_bio_start
);
2230 } else if (!skip_sum
) {
2231 ret
= btrfs_csum_one_bio(BTRFS_I(inode
), bio
, 0, 0);
2237 ret
= btrfs_map_bio(fs_info
, bio
, mirror_num
);
2241 bio
->bi_status
= ret
;
2248 * given a list of ordered sums record them in the inode. This happens
2249 * at IO completion time based on sums calculated at bio submission time.
2251 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
2252 struct inode
*inode
, struct list_head
*list
)
2254 struct btrfs_ordered_sum
*sum
;
2257 list_for_each_entry(sum
, list
, list
) {
2258 trans
->adding_csums
= true;
2259 ret
= btrfs_csum_file_blocks(trans
,
2260 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
2261 trans
->adding_csums
= false;
2268 int btrfs_set_extent_delalloc(struct btrfs_inode
*inode
, u64 start
, u64 end
,
2269 unsigned int extra_bits
,
2270 struct extent_state
**cached_state
)
2272 WARN_ON(PAGE_ALIGNED(end
));
2273 return set_extent_delalloc(&inode
->io_tree
, start
, end
, extra_bits
,
2277 /* see btrfs_writepage_start_hook for details on why this is required */
2278 struct btrfs_writepage_fixup
{
2280 struct inode
*inode
;
2281 struct btrfs_work work
;
2284 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
2286 struct btrfs_writepage_fixup
*fixup
;
2287 struct btrfs_ordered_extent
*ordered
;
2288 struct extent_state
*cached_state
= NULL
;
2289 struct extent_changeset
*data_reserved
= NULL
;
2291 struct btrfs_inode
*inode
;
2295 bool free_delalloc_space
= true;
2297 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
2299 inode
= BTRFS_I(fixup
->inode
);
2300 page_start
= page_offset(page
);
2301 page_end
= page_offset(page
) + PAGE_SIZE
- 1;
2304 * This is similar to page_mkwrite, we need to reserve the space before
2305 * we take the page lock.
2307 ret
= btrfs_delalloc_reserve_space(inode
, &data_reserved
, page_start
,
2313 * Before we queued this fixup, we took a reference on the page.
2314 * page->mapping may go NULL, but it shouldn't be moved to a different
2317 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
2319 * Unfortunately this is a little tricky, either
2321 * 1) We got here and our page had already been dealt with and
2322 * we reserved our space, thus ret == 0, so we need to just
2323 * drop our space reservation and bail. This can happen the
2324 * first time we come into the fixup worker, or could happen
2325 * while waiting for the ordered extent.
2326 * 2) Our page was already dealt with, but we happened to get an
2327 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2328 * this case we obviously don't have anything to release, but
2329 * because the page was already dealt with we don't want to
2330 * mark the page with an error, so make sure we're resetting
2331 * ret to 0. This is why we have this check _before_ the ret
2332 * check, because we do not want to have a surprise ENOSPC
2333 * when the page was already properly dealt with.
2336 btrfs_delalloc_release_extents(inode
, PAGE_SIZE
);
2337 btrfs_delalloc_release_space(inode
, data_reserved
,
2338 page_start
, PAGE_SIZE
,
2346 * We can't mess with the page state unless it is locked, so now that
2347 * it is locked bail if we failed to make our space reservation.
2352 lock_extent_bits(&inode
->io_tree
, page_start
, page_end
, &cached_state
);
2354 /* already ordered? We're done */
2355 if (PagePrivate2(page
))
2358 ordered
= btrfs_lookup_ordered_range(inode
, page_start
, PAGE_SIZE
);
2360 unlock_extent_cached(&inode
->io_tree
, page_start
, page_end
,
2363 btrfs_start_ordered_extent(&inode
->vfs_inode
, ordered
, 1);
2364 btrfs_put_ordered_extent(ordered
);
2368 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
, 0,
2374 * Everything went as planned, we're now the owner of a dirty page with
2375 * delayed allocation bits set and space reserved for our COW
2378 * The page was dirty when we started, nothing should have cleaned it.
2380 BUG_ON(!PageDirty(page
));
2381 free_delalloc_space
= false;
2383 btrfs_delalloc_release_extents(inode
, PAGE_SIZE
);
2384 if (free_delalloc_space
)
2385 btrfs_delalloc_release_space(inode
, data_reserved
, page_start
,
2387 unlock_extent_cached(&inode
->io_tree
, page_start
, page_end
,
2392 * We hit ENOSPC or other errors. Update the mapping and page
2393 * to reflect the errors and clean the page.
2395 mapping_set_error(page
->mapping
, ret
);
2396 end_extent_writepage(page
, ret
, page_start
, page_end
);
2397 clear_page_dirty_for_io(page
);
2400 ClearPageChecked(page
);
2404 extent_changeset_free(data_reserved
);
2406 * As a precaution, do a delayed iput in case it would be the last iput
2407 * that could need flushing space. Recursing back to fixup worker would
2410 btrfs_add_delayed_iput(&inode
->vfs_inode
);
2414 * There are a few paths in the higher layers of the kernel that directly
2415 * set the page dirty bit without asking the filesystem if it is a
2416 * good idea. This causes problems because we want to make sure COW
2417 * properly happens and the data=ordered rules are followed.
2419 * In our case any range that doesn't have the ORDERED bit set
2420 * hasn't been properly setup for IO. We kick off an async process
2421 * to fix it up. The async helper will wait for ordered extents, set
2422 * the delalloc bit and make it safe to write the page.
2424 int btrfs_writepage_cow_fixup(struct page
*page
, u64 start
, u64 end
)
2426 struct inode
*inode
= page
->mapping
->host
;
2427 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2428 struct btrfs_writepage_fixup
*fixup
;
2430 /* this page is properly in the ordered list */
2431 if (TestClearPagePrivate2(page
))
2435 * PageChecked is set below when we create a fixup worker for this page,
2436 * don't try to create another one if we're already PageChecked()
2438 * The extent_io writepage code will redirty the page if we send back
2441 if (PageChecked(page
))
2444 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
2449 * We are already holding a reference to this inode from
2450 * write_cache_pages. We need to hold it because the space reservation
2451 * takes place outside of the page lock, and we can't trust
2452 * page->mapping outside of the page lock.
2455 SetPageChecked(page
);
2457 btrfs_init_work(&fixup
->work
, btrfs_writepage_fixup_worker
, NULL
, NULL
);
2459 fixup
->inode
= inode
;
2460 btrfs_queue_work(fs_info
->fixup_workers
, &fixup
->work
);
2465 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
2466 struct btrfs_inode
*inode
, u64 file_pos
,
2467 struct btrfs_file_extent_item
*stack_fi
,
2468 u64 qgroup_reserved
)
2470 struct btrfs_root
*root
= inode
->root
;
2471 struct btrfs_path
*path
;
2472 struct extent_buffer
*leaf
;
2473 struct btrfs_key ins
;
2474 u64 disk_num_bytes
= btrfs_stack_file_extent_disk_num_bytes(stack_fi
);
2475 u64 disk_bytenr
= btrfs_stack_file_extent_disk_bytenr(stack_fi
);
2476 u64 num_bytes
= btrfs_stack_file_extent_num_bytes(stack_fi
);
2477 u64 ram_bytes
= btrfs_stack_file_extent_ram_bytes(stack_fi
);
2478 int extent_inserted
= 0;
2481 path
= btrfs_alloc_path();
2486 * we may be replacing one extent in the tree with another.
2487 * The new extent is pinned in the extent map, and we don't want
2488 * to drop it from the cache until it is completely in the btree.
2490 * So, tell btrfs_drop_extents to leave this extent in the cache.
2491 * the caller is expected to unpin it and allow it to be merged
2494 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
, file_pos
,
2495 file_pos
+ num_bytes
, NULL
, 0,
2496 1, sizeof(*stack_fi
), &extent_inserted
);
2500 if (!extent_inserted
) {
2501 ins
.objectid
= btrfs_ino(inode
);
2502 ins
.offset
= file_pos
;
2503 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
2505 path
->leave_spinning
= 1;
2506 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
2511 leaf
= path
->nodes
[0];
2512 btrfs_set_stack_file_extent_generation(stack_fi
, trans
->transid
);
2513 write_extent_buffer(leaf
, stack_fi
,
2514 btrfs_item_ptr_offset(leaf
, path
->slots
[0]),
2515 sizeof(struct btrfs_file_extent_item
));
2517 btrfs_mark_buffer_dirty(leaf
);
2518 btrfs_release_path(path
);
2520 inode_add_bytes(&inode
->vfs_inode
, num_bytes
);
2522 ins
.objectid
= disk_bytenr
;
2523 ins
.offset
= disk_num_bytes
;
2524 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2526 ret
= btrfs_inode_set_file_extent_range(inode
, file_pos
, ram_bytes
);
2530 ret
= btrfs_alloc_reserved_file_extent(trans
, root
, btrfs_ino(inode
),
2531 file_pos
, qgroup_reserved
, &ins
);
2533 btrfs_free_path(path
);
2538 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info
*fs_info
,
2541 struct btrfs_block_group
*cache
;
2543 cache
= btrfs_lookup_block_group(fs_info
, start
);
2546 spin_lock(&cache
->lock
);
2547 cache
->delalloc_bytes
-= len
;
2548 spin_unlock(&cache
->lock
);
2550 btrfs_put_block_group(cache
);
2553 static int insert_ordered_extent_file_extent(struct btrfs_trans_handle
*trans
,
2554 struct inode
*inode
,
2555 struct btrfs_ordered_extent
*oe
)
2557 struct btrfs_file_extent_item stack_fi
;
2560 memset(&stack_fi
, 0, sizeof(stack_fi
));
2561 btrfs_set_stack_file_extent_type(&stack_fi
, BTRFS_FILE_EXTENT_REG
);
2562 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi
, oe
->disk_bytenr
);
2563 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi
,
2564 oe
->disk_num_bytes
);
2565 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &oe
->flags
))
2566 logical_len
= oe
->truncated_len
;
2568 logical_len
= oe
->num_bytes
;
2569 btrfs_set_stack_file_extent_num_bytes(&stack_fi
, logical_len
);
2570 btrfs_set_stack_file_extent_ram_bytes(&stack_fi
, logical_len
);
2571 btrfs_set_stack_file_extent_compression(&stack_fi
, oe
->compress_type
);
2572 /* Encryption and other encoding is reserved and all 0 */
2574 return insert_reserved_file_extent(trans
, BTRFS_I(inode
), oe
->file_offset
,
2575 &stack_fi
, oe
->qgroup_rsv
);
2579 * As ordered data IO finishes, this gets called so we can finish
2580 * an ordered extent if the range of bytes in the file it covers are
2583 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2585 struct inode
*inode
= ordered_extent
->inode
;
2586 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2587 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2588 struct btrfs_trans_handle
*trans
= NULL
;
2589 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2590 struct extent_state
*cached_state
= NULL
;
2592 int compress_type
= 0;
2594 u64 logical_len
= ordered_extent
->num_bytes
;
2595 bool freespace_inode
;
2596 bool truncated
= false;
2597 bool range_locked
= false;
2598 bool clear_new_delalloc_bytes
= false;
2599 bool clear_reserved_extent
= true;
2600 unsigned int clear_bits
;
2602 start
= ordered_extent
->file_offset
;
2603 end
= start
+ ordered_extent
->num_bytes
- 1;
2605 if (!test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2606 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
) &&
2607 !test_bit(BTRFS_ORDERED_DIRECT
, &ordered_extent
->flags
))
2608 clear_new_delalloc_bytes
= true;
2610 freespace_inode
= btrfs_is_free_space_inode(BTRFS_I(inode
));
2612 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2617 btrfs_free_io_failure_record(BTRFS_I(inode
), start
, end
);
2619 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2621 logical_len
= ordered_extent
->truncated_len
;
2622 /* Truncated the entire extent, don't bother adding */
2627 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2628 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2630 btrfs_inode_safe_disk_i_size_write(inode
, 0);
2631 if (freespace_inode
)
2632 trans
= btrfs_join_transaction_spacecache(root
);
2634 trans
= btrfs_join_transaction(root
);
2635 if (IS_ERR(trans
)) {
2636 ret
= PTR_ERR(trans
);
2640 trans
->block_rsv
= &BTRFS_I(inode
)->block_rsv
;
2641 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2642 if (ret
) /* -ENOMEM or corruption */
2643 btrfs_abort_transaction(trans
, ret
);
2647 range_locked
= true;
2648 lock_extent_bits(io_tree
, start
, end
, &cached_state
);
2650 if (freespace_inode
)
2651 trans
= btrfs_join_transaction_spacecache(root
);
2653 trans
= btrfs_join_transaction(root
);
2654 if (IS_ERR(trans
)) {
2655 ret
= PTR_ERR(trans
);
2660 trans
->block_rsv
= &BTRFS_I(inode
)->block_rsv
;
2662 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2663 compress_type
= ordered_extent
->compress_type
;
2664 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2665 BUG_ON(compress_type
);
2666 ret
= btrfs_mark_extent_written(trans
, BTRFS_I(inode
),
2667 ordered_extent
->file_offset
,
2668 ordered_extent
->file_offset
+
2671 BUG_ON(root
== fs_info
->tree_root
);
2672 ret
= insert_ordered_extent_file_extent(trans
, inode
,
2675 clear_reserved_extent
= false;
2676 btrfs_release_delalloc_bytes(fs_info
,
2677 ordered_extent
->disk_bytenr
,
2678 ordered_extent
->disk_num_bytes
);
2681 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2682 ordered_extent
->file_offset
,
2683 ordered_extent
->num_bytes
, trans
->transid
);
2685 btrfs_abort_transaction(trans
, ret
);
2689 ret
= add_pending_csums(trans
, inode
, &ordered_extent
->list
);
2691 btrfs_abort_transaction(trans
, ret
);
2695 btrfs_inode_safe_disk_i_size_write(inode
, 0);
2696 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2697 if (ret
) { /* -ENOMEM or corruption */
2698 btrfs_abort_transaction(trans
, ret
);
2703 clear_bits
= EXTENT_DEFRAG
;
2705 clear_bits
|= EXTENT_LOCKED
;
2706 if (clear_new_delalloc_bytes
)
2707 clear_bits
|= EXTENT_DELALLOC_NEW
;
2708 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, clear_bits
,
2709 (clear_bits
& EXTENT_LOCKED
) ? 1 : 0, 0,
2713 btrfs_end_transaction(trans
);
2715 if (ret
|| truncated
) {
2716 u64 unwritten_start
= start
;
2719 unwritten_start
+= logical_len
;
2720 clear_extent_uptodate(io_tree
, unwritten_start
, end
, NULL
);
2722 /* Drop the cache for the part of the extent we didn't write. */
2723 btrfs_drop_extent_cache(BTRFS_I(inode
), unwritten_start
, end
, 0);
2726 * If the ordered extent had an IOERR or something else went
2727 * wrong we need to return the space for this ordered extent
2728 * back to the allocator. We only free the extent in the
2729 * truncated case if we didn't write out the extent at all.
2731 * If we made it past insert_reserved_file_extent before we
2732 * errored out then we don't need to do this as the accounting
2733 * has already been done.
2735 if ((ret
|| !logical_len
) &&
2736 clear_reserved_extent
&&
2737 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2738 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2740 * Discard the range before returning it back to the
2743 if (ret
&& btrfs_test_opt(fs_info
, DISCARD_SYNC
))
2744 btrfs_discard_extent(fs_info
,
2745 ordered_extent
->disk_bytenr
,
2746 ordered_extent
->disk_num_bytes
,
2748 btrfs_free_reserved_extent(fs_info
,
2749 ordered_extent
->disk_bytenr
,
2750 ordered_extent
->disk_num_bytes
, 1);
2755 * This needs to be done to make sure anybody waiting knows we are done
2756 * updating everything for this ordered extent.
2758 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2761 btrfs_put_ordered_extent(ordered_extent
);
2762 /* once for the tree */
2763 btrfs_put_ordered_extent(ordered_extent
);
2768 static void finish_ordered_fn(struct btrfs_work
*work
)
2770 struct btrfs_ordered_extent
*ordered_extent
;
2771 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2772 btrfs_finish_ordered_io(ordered_extent
);
2775 void btrfs_writepage_endio_finish_ordered(struct page
*page
, u64 start
,
2776 u64 end
, int uptodate
)
2778 struct inode
*inode
= page
->mapping
->host
;
2779 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2780 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2781 struct btrfs_workqueue
*wq
;
2783 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2785 ClearPagePrivate2(page
);
2786 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2787 end
- start
+ 1, uptodate
))
2790 if (btrfs_is_free_space_inode(BTRFS_I(inode
)))
2791 wq
= fs_info
->endio_freespace_worker
;
2793 wq
= fs_info
->endio_write_workers
;
2795 btrfs_init_work(&ordered_extent
->work
, finish_ordered_fn
, NULL
, NULL
);
2796 btrfs_queue_work(wq
, &ordered_extent
->work
);
2799 static int check_data_csum(struct inode
*inode
, struct btrfs_io_bio
*io_bio
,
2800 int icsum
, struct page
*page
, int pgoff
, u64 start
,
2803 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2804 SHASH_DESC_ON_STACK(shash
, fs_info
->csum_shash
);
2806 u16 csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
2808 u8 csum
[BTRFS_CSUM_SIZE
];
2810 csum_expected
= ((u8
*)io_bio
->csum
) + icsum
* csum_size
;
2812 kaddr
= kmap_atomic(page
);
2813 shash
->tfm
= fs_info
->csum_shash
;
2815 crypto_shash_digest(shash
, kaddr
+ pgoff
, len
, csum
);
2817 if (memcmp(csum
, csum_expected
, csum_size
))
2820 kunmap_atomic(kaddr
);
2823 btrfs_print_data_csum_error(BTRFS_I(inode
), start
, csum
, csum_expected
,
2824 io_bio
->mirror_num
);
2826 btrfs_dev_stat_inc_and_print(io_bio
->device
,
2827 BTRFS_DEV_STAT_CORRUPTION_ERRS
);
2828 memset(kaddr
+ pgoff
, 1, len
);
2829 flush_dcache_page(page
);
2830 kunmap_atomic(kaddr
);
2835 * when reads are done, we need to check csums to verify the data is correct
2836 * if there's a match, we allow the bio to finish. If not, the code in
2837 * extent_io.c will try to find good copies for us.
2839 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
2840 u64 phy_offset
, struct page
*page
,
2841 u64 start
, u64 end
, int mirror
)
2843 size_t offset
= start
- page_offset(page
);
2844 struct inode
*inode
= page
->mapping
->host
;
2845 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2846 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2848 if (PageChecked(page
)) {
2849 ClearPageChecked(page
);
2853 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2856 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2857 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2858 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
);
2862 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
2863 return check_data_csum(inode
, io_bio
, phy_offset
, page
, offset
, start
,
2864 (size_t)(end
- start
+ 1));
2868 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2870 * @inode: The inode we want to perform iput on
2872 * This function uses the generic vfs_inode::i_count to track whether we should
2873 * just decrement it (in case it's > 1) or if this is the last iput then link
2874 * the inode to the delayed iput machinery. Delayed iputs are processed at
2875 * transaction commit time/superblock commit/cleaner kthread.
2877 void btrfs_add_delayed_iput(struct inode
*inode
)
2879 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2880 struct btrfs_inode
*binode
= BTRFS_I(inode
);
2882 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2885 atomic_inc(&fs_info
->nr_delayed_iputs
);
2886 spin_lock(&fs_info
->delayed_iput_lock
);
2887 ASSERT(list_empty(&binode
->delayed_iput
));
2888 list_add_tail(&binode
->delayed_iput
, &fs_info
->delayed_iputs
);
2889 spin_unlock(&fs_info
->delayed_iput_lock
);
2890 if (!test_bit(BTRFS_FS_CLEANER_RUNNING
, &fs_info
->flags
))
2891 wake_up_process(fs_info
->cleaner_kthread
);
2894 static void run_delayed_iput_locked(struct btrfs_fs_info
*fs_info
,
2895 struct btrfs_inode
*inode
)
2897 list_del_init(&inode
->delayed_iput
);
2898 spin_unlock(&fs_info
->delayed_iput_lock
);
2899 iput(&inode
->vfs_inode
);
2900 if (atomic_dec_and_test(&fs_info
->nr_delayed_iputs
))
2901 wake_up(&fs_info
->delayed_iputs_wait
);
2902 spin_lock(&fs_info
->delayed_iput_lock
);
2905 static void btrfs_run_delayed_iput(struct btrfs_fs_info
*fs_info
,
2906 struct btrfs_inode
*inode
)
2908 if (!list_empty(&inode
->delayed_iput
)) {
2909 spin_lock(&fs_info
->delayed_iput_lock
);
2910 if (!list_empty(&inode
->delayed_iput
))
2911 run_delayed_iput_locked(fs_info
, inode
);
2912 spin_unlock(&fs_info
->delayed_iput_lock
);
2916 void btrfs_run_delayed_iputs(struct btrfs_fs_info
*fs_info
)
2919 spin_lock(&fs_info
->delayed_iput_lock
);
2920 while (!list_empty(&fs_info
->delayed_iputs
)) {
2921 struct btrfs_inode
*inode
;
2923 inode
= list_first_entry(&fs_info
->delayed_iputs
,
2924 struct btrfs_inode
, delayed_iput
);
2925 run_delayed_iput_locked(fs_info
, inode
);
2927 spin_unlock(&fs_info
->delayed_iput_lock
);
2931 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2932 * @fs_info - the fs_info for this fs
2933 * @return - EINTR if we were killed, 0 if nothing's pending
2935 * This will wait on any delayed iputs that are currently running with KILLABLE
2936 * set. Once they are all done running we will return, unless we are killed in
2937 * which case we return EINTR. This helps in user operations like fallocate etc
2938 * that might get blocked on the iputs.
2940 int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info
*fs_info
)
2942 int ret
= wait_event_killable(fs_info
->delayed_iputs_wait
,
2943 atomic_read(&fs_info
->nr_delayed_iputs
) == 0);
2950 * This creates an orphan entry for the given inode in case something goes wrong
2951 * in the middle of an unlink.
2953 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
,
2954 struct btrfs_inode
*inode
)
2958 ret
= btrfs_insert_orphan_item(trans
, inode
->root
, btrfs_ino(inode
));
2959 if (ret
&& ret
!= -EEXIST
) {
2960 btrfs_abort_transaction(trans
, ret
);
2968 * We have done the delete so we can go ahead and remove the orphan item for
2969 * this particular inode.
2971 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
2972 struct btrfs_inode
*inode
)
2974 return btrfs_del_orphan_item(trans
, inode
->root
, btrfs_ino(inode
));
2978 * this cleans up any orphans that may be left on the list from the last use
2981 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
2983 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2984 struct btrfs_path
*path
;
2985 struct extent_buffer
*leaf
;
2986 struct btrfs_key key
, found_key
;
2987 struct btrfs_trans_handle
*trans
;
2988 struct inode
*inode
;
2989 u64 last_objectid
= 0;
2990 int ret
= 0, nr_unlink
= 0;
2992 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
2995 path
= btrfs_alloc_path();
3000 path
->reada
= READA_BACK
;
3002 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3003 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
3004 key
.offset
= (u64
)-1;
3007 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3012 * if ret == 0 means we found what we were searching for, which
3013 * is weird, but possible, so only screw with path if we didn't
3014 * find the key and see if we have stuff that matches
3018 if (path
->slots
[0] == 0)
3023 /* pull out the item */
3024 leaf
= path
->nodes
[0];
3025 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3027 /* make sure the item matches what we want */
3028 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3030 if (found_key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
3033 /* release the path since we're done with it */
3034 btrfs_release_path(path
);
3037 * this is where we are basically btrfs_lookup, without the
3038 * crossing root thing. we store the inode number in the
3039 * offset of the orphan item.
3042 if (found_key
.offset
== last_objectid
) {
3044 "Error removing orphan entry, stopping orphan cleanup");
3049 last_objectid
= found_key
.offset
;
3051 found_key
.objectid
= found_key
.offset
;
3052 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3053 found_key
.offset
= 0;
3054 inode
= btrfs_iget(fs_info
->sb
, last_objectid
, root
);
3055 ret
= PTR_ERR_OR_ZERO(inode
);
3056 if (ret
&& ret
!= -ENOENT
)
3059 if (ret
== -ENOENT
&& root
== fs_info
->tree_root
) {
3060 struct btrfs_root
*dead_root
;
3061 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3062 int is_dead_root
= 0;
3065 * this is an orphan in the tree root. Currently these
3066 * could come from 2 sources:
3067 * a) a snapshot deletion in progress
3068 * b) a free space cache inode
3069 * We need to distinguish those two, as the snapshot
3070 * orphan must not get deleted.
3071 * find_dead_roots already ran before us, so if this
3072 * is a snapshot deletion, we should find the root
3073 * in the fs_roots radix tree.
3076 spin_lock(&fs_info
->fs_roots_radix_lock
);
3077 dead_root
= radix_tree_lookup(&fs_info
->fs_roots_radix
,
3078 (unsigned long)found_key
.objectid
);
3079 if (dead_root
&& btrfs_root_refs(&dead_root
->root_item
) == 0)
3081 spin_unlock(&fs_info
->fs_roots_radix_lock
);
3084 /* prevent this orphan from being found again */
3085 key
.offset
= found_key
.objectid
- 1;
3092 * If we have an inode with links, there are a couple of
3093 * possibilities. Old kernels (before v3.12) used to create an
3094 * orphan item for truncate indicating that there were possibly
3095 * extent items past i_size that needed to be deleted. In v3.12,
3096 * truncate was changed to update i_size in sync with the extent
3097 * items, but the (useless) orphan item was still created. Since
3098 * v4.18, we don't create the orphan item for truncate at all.
3100 * So, this item could mean that we need to do a truncate, but
3101 * only if this filesystem was last used on a pre-v3.12 kernel
3102 * and was not cleanly unmounted. The odds of that are quite
3103 * slim, and it's a pain to do the truncate now, so just delete
3106 * It's also possible that this orphan item was supposed to be
3107 * deleted but wasn't. The inode number may have been reused,
3108 * but either way, we can delete the orphan item.
3110 if (ret
== -ENOENT
|| inode
->i_nlink
) {
3113 trans
= btrfs_start_transaction(root
, 1);
3114 if (IS_ERR(trans
)) {
3115 ret
= PTR_ERR(trans
);
3118 btrfs_debug(fs_info
, "auto deleting %Lu",
3119 found_key
.objectid
);
3120 ret
= btrfs_del_orphan_item(trans
, root
,
3121 found_key
.objectid
);
3122 btrfs_end_transaction(trans
);
3130 /* this will do delete_inode and everything for us */
3133 /* release the path since we're done with it */
3134 btrfs_release_path(path
);
3136 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3138 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
)) {
3139 trans
= btrfs_join_transaction(root
);
3141 btrfs_end_transaction(trans
);
3145 btrfs_debug(fs_info
, "unlinked %d orphans", nr_unlink
);
3149 btrfs_err(fs_info
, "could not do orphan cleanup %d", ret
);
3150 btrfs_free_path(path
);
3155 * very simple check to peek ahead in the leaf looking for xattrs. If we
3156 * don't find any xattrs, we know there can't be any acls.
3158 * slot is the slot the inode is in, objectid is the objectid of the inode
3160 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3161 int slot
, u64 objectid
,
3162 int *first_xattr_slot
)
3164 u32 nritems
= btrfs_header_nritems(leaf
);
3165 struct btrfs_key found_key
;
3166 static u64 xattr_access
= 0;
3167 static u64 xattr_default
= 0;
3170 if (!xattr_access
) {
3171 xattr_access
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS
,
3172 strlen(XATTR_NAME_POSIX_ACL_ACCESS
));
3173 xattr_default
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT
,
3174 strlen(XATTR_NAME_POSIX_ACL_DEFAULT
));
3178 *first_xattr_slot
= -1;
3179 while (slot
< nritems
) {
3180 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3182 /* we found a different objectid, there must not be acls */
3183 if (found_key
.objectid
!= objectid
)
3186 /* we found an xattr, assume we've got an acl */
3187 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3188 if (*first_xattr_slot
== -1)
3189 *first_xattr_slot
= slot
;
3190 if (found_key
.offset
== xattr_access
||
3191 found_key
.offset
== xattr_default
)
3196 * we found a key greater than an xattr key, there can't
3197 * be any acls later on
3199 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3206 * it goes inode, inode backrefs, xattrs, extents,
3207 * so if there are a ton of hard links to an inode there can
3208 * be a lot of backrefs. Don't waste time searching too hard,
3209 * this is just an optimization
3214 /* we hit the end of the leaf before we found an xattr or
3215 * something larger than an xattr. We have to assume the inode
3218 if (*first_xattr_slot
== -1)
3219 *first_xattr_slot
= slot
;
3224 * read an inode from the btree into the in-memory inode
3226 static int btrfs_read_locked_inode(struct inode
*inode
,
3227 struct btrfs_path
*in_path
)
3229 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3230 struct btrfs_path
*path
= in_path
;
3231 struct extent_buffer
*leaf
;
3232 struct btrfs_inode_item
*inode_item
;
3233 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3234 struct btrfs_key location
;
3239 bool filled
= false;
3240 int first_xattr_slot
;
3242 ret
= btrfs_fill_inode(inode
, &rdev
);
3247 path
= btrfs_alloc_path();
3252 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3254 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3256 if (path
!= in_path
)
3257 btrfs_free_path(path
);
3261 leaf
= path
->nodes
[0];
3266 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3267 struct btrfs_inode_item
);
3268 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3269 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3270 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3271 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3272 btrfs_i_size_write(BTRFS_I(inode
), btrfs_inode_size(leaf
, inode_item
));
3273 btrfs_inode_set_file_extent_range(BTRFS_I(inode
), 0,
3274 round_up(i_size_read(inode
), fs_info
->sectorsize
));
3276 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->atime
);
3277 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->atime
);
3279 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->mtime
);
3280 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->mtime
);
3282 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->ctime
);
3283 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->ctime
);
3285 BTRFS_I(inode
)->i_otime
.tv_sec
=
3286 btrfs_timespec_sec(leaf
, &inode_item
->otime
);
3287 BTRFS_I(inode
)->i_otime
.tv_nsec
=
3288 btrfs_timespec_nsec(leaf
, &inode_item
->otime
);
3290 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3291 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3292 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3294 inode_set_iversion_queried(inode
,
3295 btrfs_inode_sequence(leaf
, inode_item
));
3296 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3298 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3300 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3301 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3305 * If we were modified in the current generation and evicted from memory
3306 * and then re-read we need to do a full sync since we don't have any
3307 * idea about which extents were modified before we were evicted from
3310 * This is required for both inode re-read from disk and delayed inode
3311 * in delayed_nodes_tree.
3313 if (BTRFS_I(inode
)->last_trans
== fs_info
->generation
)
3314 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3315 &BTRFS_I(inode
)->runtime_flags
);
3318 * We don't persist the id of the transaction where an unlink operation
3319 * against the inode was last made. So here we assume the inode might
3320 * have been evicted, and therefore the exact value of last_unlink_trans
3321 * lost, and set it to last_trans to avoid metadata inconsistencies
3322 * between the inode and its parent if the inode is fsync'ed and the log
3323 * replayed. For example, in the scenario:
3326 * ln mydir/foo mydir/bar
3329 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3330 * xfs_io -c fsync mydir/foo
3332 * mount fs, triggers fsync log replay
3334 * We must make sure that when we fsync our inode foo we also log its
3335 * parent inode, otherwise after log replay the parent still has the
3336 * dentry with the "bar" name but our inode foo has a link count of 1
3337 * and doesn't have an inode ref with the name "bar" anymore.
3339 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3340 * but it guarantees correctness at the expense of occasional full
3341 * transaction commits on fsync if our inode is a directory, or if our
3342 * inode is not a directory, logging its parent unnecessarily.
3344 BTRFS_I(inode
)->last_unlink_trans
= BTRFS_I(inode
)->last_trans
;
3347 * Same logic as for last_unlink_trans. We don't persist the generation
3348 * of the last transaction where this inode was used for a reflink
3349 * operation, so after eviction and reloading the inode we must be
3350 * pessimistic and assume the last transaction that modified the inode.
3352 BTRFS_I(inode
)->last_reflink_trans
= BTRFS_I(inode
)->last_trans
;
3355 if (inode
->i_nlink
!= 1 ||
3356 path
->slots
[0] >= btrfs_header_nritems(leaf
))
3359 btrfs_item_key_to_cpu(leaf
, &location
, path
->slots
[0]);
3360 if (location
.objectid
!= btrfs_ino(BTRFS_I(inode
)))
3363 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3364 if (location
.type
== BTRFS_INODE_REF_KEY
) {
3365 struct btrfs_inode_ref
*ref
;
3367 ref
= (struct btrfs_inode_ref
*)ptr
;
3368 BTRFS_I(inode
)->dir_index
= btrfs_inode_ref_index(leaf
, ref
);
3369 } else if (location
.type
== BTRFS_INODE_EXTREF_KEY
) {
3370 struct btrfs_inode_extref
*extref
;
3372 extref
= (struct btrfs_inode_extref
*)ptr
;
3373 BTRFS_I(inode
)->dir_index
= btrfs_inode_extref_index(leaf
,
3378 * try to precache a NULL acl entry for files that don't have
3379 * any xattrs or acls
3381 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3382 btrfs_ino(BTRFS_I(inode
)), &first_xattr_slot
);
3383 if (first_xattr_slot
!= -1) {
3384 path
->slots
[0] = first_xattr_slot
;
3385 ret
= btrfs_load_inode_props(inode
, path
);
3388 "error loading props for ino %llu (root %llu): %d",
3389 btrfs_ino(BTRFS_I(inode
)),
3390 root
->root_key
.objectid
, ret
);
3392 if (path
!= in_path
)
3393 btrfs_free_path(path
);
3396 cache_no_acl(inode
);
3398 switch (inode
->i_mode
& S_IFMT
) {
3400 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3401 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3402 inode
->i_fop
= &btrfs_file_operations
;
3403 inode
->i_op
= &btrfs_file_inode_operations
;
3406 inode
->i_fop
= &btrfs_dir_file_operations
;
3407 inode
->i_op
= &btrfs_dir_inode_operations
;
3410 inode
->i_op
= &btrfs_symlink_inode_operations
;
3411 inode_nohighmem(inode
);
3412 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3415 inode
->i_op
= &btrfs_special_inode_operations
;
3416 init_special_inode(inode
, inode
->i_mode
, rdev
);
3420 btrfs_sync_inode_flags_to_i_flags(inode
);
3425 * given a leaf and an inode, copy the inode fields into the leaf
3427 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3428 struct extent_buffer
*leaf
,
3429 struct btrfs_inode_item
*item
,
3430 struct inode
*inode
)
3432 struct btrfs_map_token token
;
3434 btrfs_init_map_token(&token
, leaf
);
3436 btrfs_set_token_inode_uid(&token
, item
, i_uid_read(inode
));
3437 btrfs_set_token_inode_gid(&token
, item
, i_gid_read(inode
));
3438 btrfs_set_token_inode_size(&token
, item
, BTRFS_I(inode
)->disk_i_size
);
3439 btrfs_set_token_inode_mode(&token
, item
, inode
->i_mode
);
3440 btrfs_set_token_inode_nlink(&token
, item
, inode
->i_nlink
);
3442 btrfs_set_token_timespec_sec(&token
, &item
->atime
,
3443 inode
->i_atime
.tv_sec
);
3444 btrfs_set_token_timespec_nsec(&token
, &item
->atime
,
3445 inode
->i_atime
.tv_nsec
);
3447 btrfs_set_token_timespec_sec(&token
, &item
->mtime
,
3448 inode
->i_mtime
.tv_sec
);
3449 btrfs_set_token_timespec_nsec(&token
, &item
->mtime
,
3450 inode
->i_mtime
.tv_nsec
);
3452 btrfs_set_token_timespec_sec(&token
, &item
->ctime
,
3453 inode
->i_ctime
.tv_sec
);
3454 btrfs_set_token_timespec_nsec(&token
, &item
->ctime
,
3455 inode
->i_ctime
.tv_nsec
);
3457 btrfs_set_token_timespec_sec(&token
, &item
->otime
,
3458 BTRFS_I(inode
)->i_otime
.tv_sec
);
3459 btrfs_set_token_timespec_nsec(&token
, &item
->otime
,
3460 BTRFS_I(inode
)->i_otime
.tv_nsec
);
3462 btrfs_set_token_inode_nbytes(&token
, item
, inode_get_bytes(inode
));
3463 btrfs_set_token_inode_generation(&token
, item
,
3464 BTRFS_I(inode
)->generation
);
3465 btrfs_set_token_inode_sequence(&token
, item
, inode_peek_iversion(inode
));
3466 btrfs_set_token_inode_transid(&token
, item
, trans
->transid
);
3467 btrfs_set_token_inode_rdev(&token
, item
, inode
->i_rdev
);
3468 btrfs_set_token_inode_flags(&token
, item
, BTRFS_I(inode
)->flags
);
3469 btrfs_set_token_inode_block_group(&token
, item
, 0);
3473 * copy everything in the in-memory inode into the btree.
3475 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3476 struct btrfs_root
*root
, struct inode
*inode
)
3478 struct btrfs_inode_item
*inode_item
;
3479 struct btrfs_path
*path
;
3480 struct extent_buffer
*leaf
;
3483 path
= btrfs_alloc_path();
3487 path
->leave_spinning
= 1;
3488 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3496 leaf
= path
->nodes
[0];
3497 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3498 struct btrfs_inode_item
);
3500 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3501 btrfs_mark_buffer_dirty(leaf
);
3502 btrfs_set_inode_last_trans(trans
, BTRFS_I(inode
));
3505 btrfs_free_path(path
);
3510 * copy everything in the in-memory inode into the btree.
3512 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3513 struct btrfs_root
*root
, struct inode
*inode
)
3515 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3519 * If the inode is a free space inode, we can deadlock during commit
3520 * if we put it into the delayed code.
3522 * The data relocation inode should also be directly updated
3525 if (!btrfs_is_free_space_inode(BTRFS_I(inode
))
3526 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
3527 && !test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
)) {
3528 btrfs_update_root_times(trans
, root
);
3530 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3532 btrfs_set_inode_last_trans(trans
, BTRFS_I(inode
));
3536 return btrfs_update_inode_item(trans
, root
, inode
);
3539 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3540 struct btrfs_root
*root
,
3541 struct inode
*inode
)
3545 ret
= btrfs_update_inode(trans
, root
, inode
);
3547 return btrfs_update_inode_item(trans
, root
, inode
);
3552 * unlink helper that gets used here in inode.c and in the tree logging
3553 * recovery code. It remove a link in a directory with a given name, and
3554 * also drops the back refs in the inode to the directory
3556 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3557 struct btrfs_root
*root
,
3558 struct btrfs_inode
*dir
,
3559 struct btrfs_inode
*inode
,
3560 const char *name
, int name_len
)
3562 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3563 struct btrfs_path
*path
;
3565 struct btrfs_dir_item
*di
;
3567 u64 ino
= btrfs_ino(inode
);
3568 u64 dir_ino
= btrfs_ino(dir
);
3570 path
= btrfs_alloc_path();
3576 path
->leave_spinning
= 1;
3577 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3578 name
, name_len
, -1);
3579 if (IS_ERR_OR_NULL(di
)) {
3580 ret
= di
? PTR_ERR(di
) : -ENOENT
;
3583 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3586 btrfs_release_path(path
);
3589 * If we don't have dir index, we have to get it by looking up
3590 * the inode ref, since we get the inode ref, remove it directly,
3591 * it is unnecessary to do delayed deletion.
3593 * But if we have dir index, needn't search inode ref to get it.
3594 * Since the inode ref is close to the inode item, it is better
3595 * that we delay to delete it, and just do this deletion when
3596 * we update the inode item.
3598 if (inode
->dir_index
) {
3599 ret
= btrfs_delayed_delete_inode_ref(inode
);
3601 index
= inode
->dir_index
;
3606 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3610 "failed to delete reference to %.*s, inode %llu parent %llu",
3611 name_len
, name
, ino
, dir_ino
);
3612 btrfs_abort_transaction(trans
, ret
);
3616 ret
= btrfs_delete_delayed_dir_index(trans
, dir
, index
);
3618 btrfs_abort_transaction(trans
, ret
);
3622 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
, inode
,
3624 if (ret
!= 0 && ret
!= -ENOENT
) {
3625 btrfs_abort_transaction(trans
, ret
);
3629 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
, dir
,
3634 btrfs_abort_transaction(trans
, ret
);
3637 * If we have a pending delayed iput we could end up with the final iput
3638 * being run in btrfs-cleaner context. If we have enough of these built
3639 * up we can end up burning a lot of time in btrfs-cleaner without any
3640 * way to throttle the unlinks. Since we're currently holding a ref on
3641 * the inode we can run the delayed iput here without any issues as the
3642 * final iput won't be done until after we drop the ref we're currently
3645 btrfs_run_delayed_iput(fs_info
, inode
);
3647 btrfs_free_path(path
);
3651 btrfs_i_size_write(dir
, dir
->vfs_inode
.i_size
- name_len
* 2);
3652 inode_inc_iversion(&inode
->vfs_inode
);
3653 inode_inc_iversion(&dir
->vfs_inode
);
3654 inode
->vfs_inode
.i_ctime
= dir
->vfs_inode
.i_mtime
=
3655 dir
->vfs_inode
.i_ctime
= current_time(&inode
->vfs_inode
);
3656 ret
= btrfs_update_inode(trans
, root
, &dir
->vfs_inode
);
3661 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3662 struct btrfs_root
*root
,
3663 struct btrfs_inode
*dir
, struct btrfs_inode
*inode
,
3664 const char *name
, int name_len
)
3667 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3669 drop_nlink(&inode
->vfs_inode
);
3670 ret
= btrfs_update_inode(trans
, root
, &inode
->vfs_inode
);
3676 * helper to start transaction for unlink and rmdir.
3678 * unlink and rmdir are special in btrfs, they do not always free space, so
3679 * if we cannot make our reservations the normal way try and see if there is
3680 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3681 * allow the unlink to occur.
3683 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
3685 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3688 * 1 for the possible orphan item
3689 * 1 for the dir item
3690 * 1 for the dir index
3691 * 1 for the inode ref
3694 return btrfs_start_transaction_fallback_global_rsv(root
, 5);
3697 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3699 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3700 struct btrfs_trans_handle
*trans
;
3701 struct inode
*inode
= d_inode(dentry
);
3704 trans
= __unlink_start_trans(dir
);
3706 return PTR_ERR(trans
);
3708 btrfs_record_unlink_dir(trans
, BTRFS_I(dir
), BTRFS_I(d_inode(dentry
)),
3711 ret
= btrfs_unlink_inode(trans
, root
, BTRFS_I(dir
),
3712 BTRFS_I(d_inode(dentry
)), dentry
->d_name
.name
,
3713 dentry
->d_name
.len
);
3717 if (inode
->i_nlink
== 0) {
3718 ret
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
3724 btrfs_end_transaction(trans
);
3725 btrfs_btree_balance_dirty(root
->fs_info
);
3729 static int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3730 struct inode
*dir
, struct dentry
*dentry
)
3732 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3733 struct btrfs_inode
*inode
= BTRFS_I(d_inode(dentry
));
3734 struct btrfs_path
*path
;
3735 struct extent_buffer
*leaf
;
3736 struct btrfs_dir_item
*di
;
3737 struct btrfs_key key
;
3738 const char *name
= dentry
->d_name
.name
;
3739 int name_len
= dentry
->d_name
.len
;
3743 u64 dir_ino
= btrfs_ino(BTRFS_I(dir
));
3745 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
) {
3746 objectid
= inode
->root
->root_key
.objectid
;
3747 } else if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
) {
3748 objectid
= inode
->location
.objectid
;
3754 path
= btrfs_alloc_path();
3758 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3759 name
, name_len
, -1);
3760 if (IS_ERR_OR_NULL(di
)) {
3761 ret
= di
? PTR_ERR(di
) : -ENOENT
;
3765 leaf
= path
->nodes
[0];
3766 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3767 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3768 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3770 btrfs_abort_transaction(trans
, ret
);
3773 btrfs_release_path(path
);
3776 * This is a placeholder inode for a subvolume we didn't have a
3777 * reference to at the time of the snapshot creation. In the meantime
3778 * we could have renamed the real subvol link into our snapshot, so
3779 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3780 * Instead simply lookup the dir_index_item for this entry so we can
3781 * remove it. Otherwise we know we have a ref to the root and we can
3782 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3784 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
) {
3785 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3787 if (IS_ERR_OR_NULL(di
)) {
3792 btrfs_abort_transaction(trans
, ret
);
3796 leaf
= path
->nodes
[0];
3797 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3799 btrfs_release_path(path
);
3801 ret
= btrfs_del_root_ref(trans
, objectid
,
3802 root
->root_key
.objectid
, dir_ino
,
3803 &index
, name
, name_len
);
3805 btrfs_abort_transaction(trans
, ret
);
3810 ret
= btrfs_delete_delayed_dir_index(trans
, BTRFS_I(dir
), index
);
3812 btrfs_abort_transaction(trans
, ret
);
3816 btrfs_i_size_write(BTRFS_I(dir
), dir
->i_size
- name_len
* 2);
3817 inode_inc_iversion(dir
);
3818 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
3819 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3821 btrfs_abort_transaction(trans
, ret
);
3823 btrfs_free_path(path
);
3828 * Helper to check if the subvolume references other subvolumes or if it's
3831 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
3833 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3834 struct btrfs_path
*path
;
3835 struct btrfs_dir_item
*di
;
3836 struct btrfs_key key
;
3840 path
= btrfs_alloc_path();
3844 /* Make sure this root isn't set as the default subvol */
3845 dir_id
= btrfs_super_root_dir(fs_info
->super_copy
);
3846 di
= btrfs_lookup_dir_item(NULL
, fs_info
->tree_root
, path
,
3847 dir_id
, "default", 7, 0);
3848 if (di
&& !IS_ERR(di
)) {
3849 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
3850 if (key
.objectid
== root
->root_key
.objectid
) {
3853 "deleting default subvolume %llu is not allowed",
3857 btrfs_release_path(path
);
3860 key
.objectid
= root
->root_key
.objectid
;
3861 key
.type
= BTRFS_ROOT_REF_KEY
;
3862 key
.offset
= (u64
)-1;
3864 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
3870 if (path
->slots
[0] > 0) {
3872 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
3873 if (key
.objectid
== root
->root_key
.objectid
&&
3874 key
.type
== BTRFS_ROOT_REF_KEY
)
3878 btrfs_free_path(path
);
3882 /* Delete all dentries for inodes belonging to the root */
3883 static void btrfs_prune_dentries(struct btrfs_root
*root
)
3885 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3886 struct rb_node
*node
;
3887 struct rb_node
*prev
;
3888 struct btrfs_inode
*entry
;
3889 struct inode
*inode
;
3892 if (!test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
3893 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
3895 spin_lock(&root
->inode_lock
);
3897 node
= root
->inode_tree
.rb_node
;
3901 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3903 if (objectid
< btrfs_ino(entry
))
3904 node
= node
->rb_left
;
3905 else if (objectid
> btrfs_ino(entry
))
3906 node
= node
->rb_right
;
3912 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
3913 if (objectid
<= btrfs_ino(entry
)) {
3917 prev
= rb_next(prev
);
3921 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
3922 objectid
= btrfs_ino(entry
) + 1;
3923 inode
= igrab(&entry
->vfs_inode
);
3925 spin_unlock(&root
->inode_lock
);
3926 if (atomic_read(&inode
->i_count
) > 1)
3927 d_prune_aliases(inode
);
3929 * btrfs_drop_inode will have it removed from the inode
3930 * cache when its usage count hits zero.
3934 spin_lock(&root
->inode_lock
);
3938 if (cond_resched_lock(&root
->inode_lock
))
3941 node
= rb_next(node
);
3943 spin_unlock(&root
->inode_lock
);
3946 int btrfs_delete_subvolume(struct inode
*dir
, struct dentry
*dentry
)
3948 struct btrfs_fs_info
*fs_info
= btrfs_sb(dentry
->d_sb
);
3949 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3950 struct inode
*inode
= d_inode(dentry
);
3951 struct btrfs_root
*dest
= BTRFS_I(inode
)->root
;
3952 struct btrfs_trans_handle
*trans
;
3953 struct btrfs_block_rsv block_rsv
;
3959 * Don't allow to delete a subvolume with send in progress. This is
3960 * inside the inode lock so the error handling that has to drop the bit
3961 * again is not run concurrently.
3963 spin_lock(&dest
->root_item_lock
);
3964 if (dest
->send_in_progress
) {
3965 spin_unlock(&dest
->root_item_lock
);
3967 "attempt to delete subvolume %llu during send",
3968 dest
->root_key
.objectid
);
3971 root_flags
= btrfs_root_flags(&dest
->root_item
);
3972 btrfs_set_root_flags(&dest
->root_item
,
3973 root_flags
| BTRFS_ROOT_SUBVOL_DEAD
);
3974 spin_unlock(&dest
->root_item_lock
);
3976 down_write(&fs_info
->subvol_sem
);
3978 err
= may_destroy_subvol(dest
);
3982 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
3984 * One for dir inode,
3985 * two for dir entries,
3986 * two for root ref/backref.
3988 err
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
, 5, true);
3992 trans
= btrfs_start_transaction(root
, 0);
3993 if (IS_ERR(trans
)) {
3994 err
= PTR_ERR(trans
);
3997 trans
->block_rsv
= &block_rsv
;
3998 trans
->bytes_reserved
= block_rsv
.size
;
4000 btrfs_record_snapshot_destroy(trans
, BTRFS_I(dir
));
4002 ret
= btrfs_unlink_subvol(trans
, dir
, dentry
);
4005 btrfs_abort_transaction(trans
, ret
);
4009 btrfs_record_root_in_trans(trans
, dest
);
4011 memset(&dest
->root_item
.drop_progress
, 0,
4012 sizeof(dest
->root_item
.drop_progress
));
4013 dest
->root_item
.drop_level
= 0;
4014 btrfs_set_root_refs(&dest
->root_item
, 0);
4016 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &dest
->state
)) {
4017 ret
= btrfs_insert_orphan_item(trans
,
4019 dest
->root_key
.objectid
);
4021 btrfs_abort_transaction(trans
, ret
);
4027 ret
= btrfs_uuid_tree_remove(trans
, dest
->root_item
.uuid
,
4028 BTRFS_UUID_KEY_SUBVOL
,
4029 dest
->root_key
.objectid
);
4030 if (ret
&& ret
!= -ENOENT
) {
4031 btrfs_abort_transaction(trans
, ret
);
4035 if (!btrfs_is_empty_uuid(dest
->root_item
.received_uuid
)) {
4036 ret
= btrfs_uuid_tree_remove(trans
,
4037 dest
->root_item
.received_uuid
,
4038 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
4039 dest
->root_key
.objectid
);
4040 if (ret
&& ret
!= -ENOENT
) {
4041 btrfs_abort_transaction(trans
, ret
);
4047 free_anon_bdev(dest
->anon_dev
);
4050 trans
->block_rsv
= NULL
;
4051 trans
->bytes_reserved
= 0;
4052 ret
= btrfs_end_transaction(trans
);
4055 inode
->i_flags
|= S_DEAD
;
4057 btrfs_subvolume_release_metadata(fs_info
, &block_rsv
);
4059 up_write(&fs_info
->subvol_sem
);
4061 spin_lock(&dest
->root_item_lock
);
4062 root_flags
= btrfs_root_flags(&dest
->root_item
);
4063 btrfs_set_root_flags(&dest
->root_item
,
4064 root_flags
& ~BTRFS_ROOT_SUBVOL_DEAD
);
4065 spin_unlock(&dest
->root_item_lock
);
4067 d_invalidate(dentry
);
4068 btrfs_prune_dentries(dest
);
4069 ASSERT(dest
->send_in_progress
== 0);
4072 if (dest
->ino_cache_inode
) {
4073 iput(dest
->ino_cache_inode
);
4074 dest
->ino_cache_inode
= NULL
;
4081 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
4083 struct inode
*inode
= d_inode(dentry
);
4085 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4086 struct btrfs_trans_handle
*trans
;
4087 u64 last_unlink_trans
;
4089 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
4091 if (btrfs_ino(BTRFS_I(inode
)) == BTRFS_FIRST_FREE_OBJECTID
)
4092 return btrfs_delete_subvolume(dir
, dentry
);
4094 trans
= __unlink_start_trans(dir
);
4096 return PTR_ERR(trans
);
4098 if (unlikely(btrfs_ino(BTRFS_I(inode
)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
4099 err
= btrfs_unlink_subvol(trans
, dir
, dentry
);
4103 err
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
4107 last_unlink_trans
= BTRFS_I(inode
)->last_unlink_trans
;
4109 /* now the directory is empty */
4110 err
= btrfs_unlink_inode(trans
, root
, BTRFS_I(dir
),
4111 BTRFS_I(d_inode(dentry
)), dentry
->d_name
.name
,
4112 dentry
->d_name
.len
);
4114 btrfs_i_size_write(BTRFS_I(inode
), 0);
4116 * Propagate the last_unlink_trans value of the deleted dir to
4117 * its parent directory. This is to prevent an unrecoverable
4118 * log tree in the case we do something like this:
4120 * 2) create snapshot under dir foo
4121 * 3) delete the snapshot
4124 * 6) fsync foo or some file inside foo
4126 if (last_unlink_trans
>= trans
->transid
)
4127 BTRFS_I(dir
)->last_unlink_trans
= last_unlink_trans
;
4130 btrfs_end_transaction(trans
);
4131 btrfs_btree_balance_dirty(root
->fs_info
);
4137 * Return this if we need to call truncate_block for the last bit of the
4140 #define NEED_TRUNCATE_BLOCK 1
4143 * this can truncate away extent items, csum items and directory items.
4144 * It starts at a high offset and removes keys until it can't find
4145 * any higher than new_size
4147 * csum items that cross the new i_size are truncated to the new size
4150 * min_type is the minimum key type to truncate down to. If set to 0, this
4151 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4153 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4154 struct btrfs_root
*root
,
4155 struct inode
*inode
,
4156 u64 new_size
, u32 min_type
)
4158 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4159 struct btrfs_path
*path
;
4160 struct extent_buffer
*leaf
;
4161 struct btrfs_file_extent_item
*fi
;
4162 struct btrfs_key key
;
4163 struct btrfs_key found_key
;
4164 u64 extent_start
= 0;
4165 u64 extent_num_bytes
= 0;
4166 u64 extent_offset
= 0;
4168 u64 last_size
= new_size
;
4169 u32 found_type
= (u8
)-1;
4172 int pending_del_nr
= 0;
4173 int pending_del_slot
= 0;
4174 int extent_type
= -1;
4176 u64 ino
= btrfs_ino(BTRFS_I(inode
));
4177 u64 bytes_deleted
= 0;
4178 bool be_nice
= false;
4179 bool should_throttle
= false;
4180 const u64 lock_start
= ALIGN_DOWN(new_size
, fs_info
->sectorsize
);
4181 struct extent_state
*cached_state
= NULL
;
4183 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4186 * For non-free space inodes and non-shareable roots, we want to back
4187 * off from time to time. This means all inodes in subvolume roots,
4188 * reloc roots, and data reloc roots.
4190 if (!btrfs_is_free_space_inode(BTRFS_I(inode
)) &&
4191 test_bit(BTRFS_ROOT_SHAREABLE
, &root
->state
))
4194 path
= btrfs_alloc_path();
4197 path
->reada
= READA_BACK
;
4199 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4200 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, (u64
)-1,
4204 * We want to drop from the next block forward in case this
4205 * new size is not block aligned since we will be keeping the
4206 * last block of the extent just the way it is.
4208 btrfs_drop_extent_cache(BTRFS_I(inode
), ALIGN(new_size
,
4209 fs_info
->sectorsize
),
4214 * This function is also used to drop the items in the log tree before
4215 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4216 * it is used to drop the logged items. So we shouldn't kill the delayed
4219 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4220 btrfs_kill_delayed_inode_items(BTRFS_I(inode
));
4223 key
.offset
= (u64
)-1;
4228 * with a 16K leaf size and 128MB extents, you can actually queue
4229 * up a huge file in a single leaf. Most of the time that
4230 * bytes_deleted is > 0, it will be huge by the time we get here
4232 if (be_nice
&& bytes_deleted
> SZ_32M
&&
4233 btrfs_should_end_transaction(trans
)) {
4238 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4244 /* there are no items in the tree for us to truncate, we're
4247 if (path
->slots
[0] == 0)
4253 u64 clear_start
= 0, clear_len
= 0;
4256 leaf
= path
->nodes
[0];
4257 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4258 found_type
= found_key
.type
;
4260 if (found_key
.objectid
!= ino
)
4263 if (found_type
< min_type
)
4266 item_end
= found_key
.offset
;
4267 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4268 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4269 struct btrfs_file_extent_item
);
4270 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4271 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4273 btrfs_file_extent_num_bytes(leaf
, fi
);
4275 trace_btrfs_truncate_show_fi_regular(
4276 BTRFS_I(inode
), leaf
, fi
,
4278 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4279 item_end
+= btrfs_file_extent_ram_bytes(leaf
,
4282 trace_btrfs_truncate_show_fi_inline(
4283 BTRFS_I(inode
), leaf
, fi
, path
->slots
[0],
4288 if (found_type
> min_type
) {
4291 if (item_end
< new_size
)
4293 if (found_key
.offset
>= new_size
)
4299 /* FIXME, shrink the extent if the ref count is only 1 */
4300 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4303 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4306 clear_start
= found_key
.offset
;
4307 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4309 u64 orig_num_bytes
=
4310 btrfs_file_extent_num_bytes(leaf
, fi
);
4311 extent_num_bytes
= ALIGN(new_size
-
4313 fs_info
->sectorsize
);
4314 clear_start
= ALIGN(new_size
, fs_info
->sectorsize
);
4315 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4317 num_dec
= (orig_num_bytes
-
4319 if (test_bit(BTRFS_ROOT_SHAREABLE
,
4322 inode_sub_bytes(inode
, num_dec
);
4323 btrfs_mark_buffer_dirty(leaf
);
4326 btrfs_file_extent_disk_num_bytes(leaf
,
4328 extent_offset
= found_key
.offset
-
4329 btrfs_file_extent_offset(leaf
, fi
);
4331 /* FIXME blocksize != 4096 */
4332 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4333 if (extent_start
!= 0) {
4335 if (test_bit(BTRFS_ROOT_SHAREABLE
,
4337 inode_sub_bytes(inode
, num_dec
);
4340 clear_len
= num_dec
;
4341 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4343 * we can't truncate inline items that have had
4347 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4348 btrfs_file_extent_other_encoding(leaf
, fi
) == 0 &&
4349 btrfs_file_extent_compression(leaf
, fi
) == 0) {
4350 u32 size
= (u32
)(new_size
- found_key
.offset
);
4352 btrfs_set_file_extent_ram_bytes(leaf
, fi
, size
);
4353 size
= btrfs_file_extent_calc_inline_size(size
);
4354 btrfs_truncate_item(path
, size
, 1);
4355 } else if (!del_item
) {
4357 * We have to bail so the last_size is set to
4358 * just before this extent.
4360 ret
= NEED_TRUNCATE_BLOCK
;
4364 * Inline extents are special, we just treat
4365 * them as a full sector worth in the file
4366 * extent tree just for simplicity sake.
4368 clear_len
= fs_info
->sectorsize
;
4371 if (test_bit(BTRFS_ROOT_SHAREABLE
, &root
->state
))
4372 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4376 * We use btrfs_truncate_inode_items() to clean up log trees for
4377 * multiple fsyncs, and in this case we don't want to clear the
4378 * file extent range because it's just the log.
4380 if (root
== BTRFS_I(inode
)->root
) {
4381 ret
= btrfs_inode_clear_file_extent_range(BTRFS_I(inode
),
4382 clear_start
, clear_len
);
4384 btrfs_abort_transaction(trans
, ret
);
4390 last_size
= found_key
.offset
;
4392 last_size
= new_size
;
4394 if (!pending_del_nr
) {
4395 /* no pending yet, add ourselves */
4396 pending_del_slot
= path
->slots
[0];
4398 } else if (pending_del_nr
&&
4399 path
->slots
[0] + 1 == pending_del_slot
) {
4400 /* hop on the pending chunk */
4402 pending_del_slot
= path
->slots
[0];
4409 should_throttle
= false;
4412 root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4413 struct btrfs_ref ref
= { 0 };
4415 bytes_deleted
+= extent_num_bytes
;
4417 btrfs_init_generic_ref(&ref
, BTRFS_DROP_DELAYED_REF
,
4418 extent_start
, extent_num_bytes
, 0);
4419 ref
.real_root
= root
->root_key
.objectid
;
4420 btrfs_init_data_ref(&ref
, btrfs_header_owner(leaf
),
4421 ino
, extent_offset
);
4422 ret
= btrfs_free_extent(trans
, &ref
);
4424 btrfs_abort_transaction(trans
, ret
);
4428 if (btrfs_should_throttle_delayed_refs(trans
))
4429 should_throttle
= true;
4433 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4436 if (path
->slots
[0] == 0 ||
4437 path
->slots
[0] != pending_del_slot
||
4439 if (pending_del_nr
) {
4440 ret
= btrfs_del_items(trans
, root
, path
,
4444 btrfs_abort_transaction(trans
, ret
);
4449 btrfs_release_path(path
);
4452 * We can generate a lot of delayed refs, so we need to
4453 * throttle every once and a while and make sure we're
4454 * adding enough space to keep up with the work we are
4455 * generating. Since we hold a transaction here we
4456 * can't flush, and we don't want to FLUSH_LIMIT because
4457 * we could have generated too many delayed refs to
4458 * actually allocate, so just bail if we're short and
4459 * let the normal reservation dance happen higher up.
4461 if (should_throttle
) {
4462 ret
= btrfs_delayed_refs_rsv_refill(fs_info
,
4463 BTRFS_RESERVE_NO_FLUSH
);
4475 if (ret
>= 0 && pending_del_nr
) {
4478 err
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4481 btrfs_abort_transaction(trans
, err
);
4485 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4486 ASSERT(last_size
>= new_size
);
4487 if (!ret
&& last_size
> new_size
)
4488 last_size
= new_size
;
4489 btrfs_inode_safe_disk_i_size_write(inode
, last_size
);
4490 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
,
4491 (u64
)-1, &cached_state
);
4494 btrfs_free_path(path
);
4499 * btrfs_truncate_block - read, zero a chunk and write a block
4500 * @inode - inode that we're zeroing
4501 * @from - the offset to start zeroing
4502 * @len - the length to zero, 0 to zero the entire range respective to the
4504 * @front - zero up to the offset instead of from the offset on
4506 * This will find the block for the "from" offset and cow the block and zero the
4507 * part we want to zero. This is used with truncate and hole punching.
4509 int btrfs_truncate_block(struct inode
*inode
, loff_t from
, loff_t len
,
4512 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4513 struct address_space
*mapping
= inode
->i_mapping
;
4514 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4515 struct btrfs_ordered_extent
*ordered
;
4516 struct extent_state
*cached_state
= NULL
;
4517 struct extent_changeset
*data_reserved
= NULL
;
4519 bool only_release_metadata
= false;
4520 u32 blocksize
= fs_info
->sectorsize
;
4521 pgoff_t index
= from
>> PAGE_SHIFT
;
4522 unsigned offset
= from
& (blocksize
- 1);
4524 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4525 size_t write_bytes
= blocksize
;
4530 if (IS_ALIGNED(offset
, blocksize
) &&
4531 (!len
|| IS_ALIGNED(len
, blocksize
)))
4534 block_start
= round_down(from
, blocksize
);
4535 block_end
= block_start
+ blocksize
- 1;
4537 ret
= btrfs_check_data_free_space(BTRFS_I(inode
), &data_reserved
,
4538 block_start
, blocksize
);
4540 if (btrfs_check_nocow_lock(BTRFS_I(inode
), block_start
,
4541 &write_bytes
) > 0) {
4542 /* For nocow case, no need to reserve data space */
4543 only_release_metadata
= true;
4548 ret
= btrfs_delalloc_reserve_metadata(BTRFS_I(inode
), blocksize
);
4550 if (!only_release_metadata
)
4551 btrfs_free_reserved_data_space(BTRFS_I(inode
),
4552 data_reserved
, block_start
, blocksize
);
4556 page
= find_or_create_page(mapping
, index
, mask
);
4558 btrfs_delalloc_release_space(BTRFS_I(inode
), data_reserved
,
4559 block_start
, blocksize
, true);
4560 btrfs_delalloc_release_extents(BTRFS_I(inode
), blocksize
);
4565 if (!PageUptodate(page
)) {
4566 ret
= btrfs_readpage(NULL
, page
);
4568 if (page
->mapping
!= mapping
) {
4573 if (!PageUptodate(page
)) {
4578 wait_on_page_writeback(page
);
4580 lock_extent_bits(io_tree
, block_start
, block_end
, &cached_state
);
4581 set_page_extent_mapped(page
);
4583 ordered
= btrfs_lookup_ordered_extent(BTRFS_I(inode
), block_start
);
4585 unlock_extent_cached(io_tree
, block_start
, block_end
,
4589 btrfs_start_ordered_extent(inode
, ordered
, 1);
4590 btrfs_put_ordered_extent(ordered
);
4594 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, block_start
, block_end
,
4595 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4596 0, 0, &cached_state
);
4598 ret
= btrfs_set_extent_delalloc(BTRFS_I(inode
), block_start
, block_end
, 0,
4601 unlock_extent_cached(io_tree
, block_start
, block_end
,
4606 if (offset
!= blocksize
) {
4608 len
= blocksize
- offset
;
4611 memset(kaddr
+ (block_start
- page_offset(page
)),
4614 memset(kaddr
+ (block_start
- page_offset(page
)) + offset
,
4616 flush_dcache_page(page
);
4619 ClearPageChecked(page
);
4620 set_page_dirty(page
);
4621 unlock_extent_cached(io_tree
, block_start
, block_end
, &cached_state
);
4623 if (only_release_metadata
)
4624 set_extent_bit(&BTRFS_I(inode
)->io_tree
, block_start
,
4625 block_end
, EXTENT_NORESERVE
, NULL
, NULL
,
4630 if (only_release_metadata
)
4631 btrfs_delalloc_release_metadata(BTRFS_I(inode
),
4634 btrfs_delalloc_release_space(BTRFS_I(inode
), data_reserved
,
4635 block_start
, blocksize
, true);
4637 btrfs_delalloc_release_extents(BTRFS_I(inode
), blocksize
);
4641 if (only_release_metadata
)
4642 btrfs_check_nocow_unlock(BTRFS_I(inode
));
4643 extent_changeset_free(data_reserved
);
4647 static int maybe_insert_hole(struct btrfs_root
*root
, struct inode
*inode
,
4648 u64 offset
, u64 len
)
4650 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4651 struct btrfs_trans_handle
*trans
;
4655 * Still need to make sure the inode looks like it's been updated so
4656 * that any holes get logged if we fsync.
4658 if (btrfs_fs_incompat(fs_info
, NO_HOLES
)) {
4659 BTRFS_I(inode
)->last_trans
= fs_info
->generation
;
4660 BTRFS_I(inode
)->last_sub_trans
= root
->log_transid
;
4661 BTRFS_I(inode
)->last_log_commit
= root
->last_log_commit
;
4666 * 1 - for the one we're dropping
4667 * 1 - for the one we're adding
4668 * 1 - for updating the inode.
4670 trans
= btrfs_start_transaction(root
, 3);
4672 return PTR_ERR(trans
);
4674 ret
= btrfs_drop_extents(trans
, root
, inode
, offset
, offset
+ len
, 1);
4676 btrfs_abort_transaction(trans
, ret
);
4677 btrfs_end_transaction(trans
);
4681 ret
= btrfs_insert_file_extent(trans
, root
, btrfs_ino(BTRFS_I(inode
)),
4682 offset
, 0, 0, len
, 0, len
, 0, 0, 0);
4684 btrfs_abort_transaction(trans
, ret
);
4686 btrfs_update_inode(trans
, root
, inode
);
4687 btrfs_end_transaction(trans
);
4692 * This function puts in dummy file extents for the area we're creating a hole
4693 * for. So if we are truncating this file to a larger size we need to insert
4694 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4695 * the range between oldsize and size
4697 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4699 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4700 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4701 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4702 struct extent_map
*em
= NULL
;
4703 struct extent_state
*cached_state
= NULL
;
4704 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4705 u64 hole_start
= ALIGN(oldsize
, fs_info
->sectorsize
);
4706 u64 block_end
= ALIGN(size
, fs_info
->sectorsize
);
4713 * If our size started in the middle of a block we need to zero out the
4714 * rest of the block before we expand the i_size, otherwise we could
4715 * expose stale data.
4717 err
= btrfs_truncate_block(inode
, oldsize
, 0, 0);
4721 if (size
<= hole_start
)
4724 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode
), hole_start
,
4725 block_end
- 1, &cached_state
);
4726 cur_offset
= hole_start
;
4728 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, cur_offset
,
4729 block_end
- cur_offset
);
4735 last_byte
= min(extent_map_end(em
), block_end
);
4736 last_byte
= ALIGN(last_byte
, fs_info
->sectorsize
);
4737 hole_size
= last_byte
- cur_offset
;
4739 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4740 struct extent_map
*hole_em
;
4742 err
= maybe_insert_hole(root
, inode
, cur_offset
,
4747 err
= btrfs_inode_set_file_extent_range(BTRFS_I(inode
),
4748 cur_offset
, hole_size
);
4752 btrfs_drop_extent_cache(BTRFS_I(inode
), cur_offset
,
4753 cur_offset
+ hole_size
- 1, 0);
4754 hole_em
= alloc_extent_map();
4756 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4757 &BTRFS_I(inode
)->runtime_flags
);
4760 hole_em
->start
= cur_offset
;
4761 hole_em
->len
= hole_size
;
4762 hole_em
->orig_start
= cur_offset
;
4764 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4765 hole_em
->block_len
= 0;
4766 hole_em
->orig_block_len
= 0;
4767 hole_em
->ram_bytes
= hole_size
;
4768 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4769 hole_em
->generation
= fs_info
->generation
;
4772 write_lock(&em_tree
->lock
);
4773 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4774 write_unlock(&em_tree
->lock
);
4777 btrfs_drop_extent_cache(BTRFS_I(inode
),
4782 free_extent_map(hole_em
);
4784 err
= btrfs_inode_set_file_extent_range(BTRFS_I(inode
),
4785 cur_offset
, hole_size
);
4790 free_extent_map(em
);
4792 cur_offset
= last_byte
;
4793 if (cur_offset
>= block_end
)
4796 free_extent_map(em
);
4797 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
);
4801 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4803 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4804 struct btrfs_trans_handle
*trans
;
4805 loff_t oldsize
= i_size_read(inode
);
4806 loff_t newsize
= attr
->ia_size
;
4807 int mask
= attr
->ia_valid
;
4811 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4812 * special case where we need to update the times despite not having
4813 * these flags set. For all other operations the VFS set these flags
4814 * explicitly if it wants a timestamp update.
4816 if (newsize
!= oldsize
) {
4817 inode_inc_iversion(inode
);
4818 if (!(mask
& (ATTR_CTIME
| ATTR_MTIME
)))
4819 inode
->i_ctime
= inode
->i_mtime
=
4820 current_time(inode
);
4823 if (newsize
> oldsize
) {
4825 * Don't do an expanding truncate while snapshotting is ongoing.
4826 * This is to ensure the snapshot captures a fully consistent
4827 * state of this file - if the snapshot captures this expanding
4828 * truncation, it must capture all writes that happened before
4831 btrfs_drew_write_lock(&root
->snapshot_lock
);
4832 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4834 btrfs_drew_write_unlock(&root
->snapshot_lock
);
4838 trans
= btrfs_start_transaction(root
, 1);
4839 if (IS_ERR(trans
)) {
4840 btrfs_drew_write_unlock(&root
->snapshot_lock
);
4841 return PTR_ERR(trans
);
4844 i_size_write(inode
, newsize
);
4845 btrfs_inode_safe_disk_i_size_write(inode
, 0);
4846 pagecache_isize_extended(inode
, oldsize
, newsize
);
4847 ret
= btrfs_update_inode(trans
, root
, inode
);
4848 btrfs_drew_write_unlock(&root
->snapshot_lock
);
4849 btrfs_end_transaction(trans
);
4853 * We're truncating a file that used to have good data down to
4854 * zero. Make sure it gets into the ordered flush list so that
4855 * any new writes get down to disk quickly.
4858 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4859 &BTRFS_I(inode
)->runtime_flags
);
4861 truncate_setsize(inode
, newsize
);
4863 /* Disable nonlocked read DIO to avoid the endless truncate */
4864 btrfs_inode_block_unlocked_dio(BTRFS_I(inode
));
4865 inode_dio_wait(inode
);
4866 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode
));
4868 ret
= btrfs_truncate(inode
, newsize
== oldsize
);
4869 if (ret
&& inode
->i_nlink
) {
4873 * Truncate failed, so fix up the in-memory size. We
4874 * adjusted disk_i_size down as we removed extents, so
4875 * wait for disk_i_size to be stable and then update the
4876 * in-memory size to match.
4878 err
= btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4881 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
4888 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4890 struct inode
*inode
= d_inode(dentry
);
4891 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4894 if (btrfs_root_readonly(root
))
4897 err
= setattr_prepare(dentry
, attr
);
4901 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4902 err
= btrfs_setsize(inode
, attr
);
4907 if (attr
->ia_valid
) {
4908 setattr_copy(inode
, attr
);
4909 inode_inc_iversion(inode
);
4910 err
= btrfs_dirty_inode(inode
);
4912 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4913 err
= posix_acl_chmod(inode
, inode
->i_mode
);
4920 * While truncating the inode pages during eviction, we get the VFS calling
4921 * btrfs_invalidatepage() against each page of the inode. This is slow because
4922 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4923 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4924 * extent_state structures over and over, wasting lots of time.
4926 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4927 * those expensive operations on a per page basis and do only the ordered io
4928 * finishing, while we release here the extent_map and extent_state structures,
4929 * without the excessive merging and splitting.
4931 static void evict_inode_truncate_pages(struct inode
*inode
)
4933 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4934 struct extent_map_tree
*map_tree
= &BTRFS_I(inode
)->extent_tree
;
4935 struct rb_node
*node
;
4937 ASSERT(inode
->i_state
& I_FREEING
);
4938 truncate_inode_pages_final(&inode
->i_data
);
4940 write_lock(&map_tree
->lock
);
4941 while (!RB_EMPTY_ROOT(&map_tree
->map
.rb_root
)) {
4942 struct extent_map
*em
;
4944 node
= rb_first_cached(&map_tree
->map
);
4945 em
= rb_entry(node
, struct extent_map
, rb_node
);
4946 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
4947 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
4948 remove_extent_mapping(map_tree
, em
);
4949 free_extent_map(em
);
4950 if (need_resched()) {
4951 write_unlock(&map_tree
->lock
);
4953 write_lock(&map_tree
->lock
);
4956 write_unlock(&map_tree
->lock
);
4959 * Keep looping until we have no more ranges in the io tree.
4960 * We can have ongoing bios started by readahead that have
4961 * their endio callback (extent_io.c:end_bio_extent_readpage)
4962 * still in progress (unlocked the pages in the bio but did not yet
4963 * unlocked the ranges in the io tree). Therefore this means some
4964 * ranges can still be locked and eviction started because before
4965 * submitting those bios, which are executed by a separate task (work
4966 * queue kthread), inode references (inode->i_count) were not taken
4967 * (which would be dropped in the end io callback of each bio).
4968 * Therefore here we effectively end up waiting for those bios and
4969 * anyone else holding locked ranges without having bumped the inode's
4970 * reference count - if we don't do it, when they access the inode's
4971 * io_tree to unlock a range it may be too late, leading to an
4972 * use-after-free issue.
4974 spin_lock(&io_tree
->lock
);
4975 while (!RB_EMPTY_ROOT(&io_tree
->state
)) {
4976 struct extent_state
*state
;
4977 struct extent_state
*cached_state
= NULL
;
4980 unsigned state_flags
;
4982 node
= rb_first(&io_tree
->state
);
4983 state
= rb_entry(node
, struct extent_state
, rb_node
);
4984 start
= state
->start
;
4986 state_flags
= state
->state
;
4987 spin_unlock(&io_tree
->lock
);
4989 lock_extent_bits(io_tree
, start
, end
, &cached_state
);
4992 * If still has DELALLOC flag, the extent didn't reach disk,
4993 * and its reserved space won't be freed by delayed_ref.
4994 * So we need to free its reserved space here.
4995 * (Refer to comment in btrfs_invalidatepage, case 2)
4997 * Note, end is the bytenr of last byte, so we need + 1 here.
4999 if (state_flags
& EXTENT_DELALLOC
)
5000 btrfs_qgroup_free_data(BTRFS_I(inode
), NULL
, start
,
5003 clear_extent_bit(io_tree
, start
, end
,
5004 EXTENT_LOCKED
| EXTENT_DELALLOC
|
5005 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
5009 spin_lock(&io_tree
->lock
);
5011 spin_unlock(&io_tree
->lock
);
5014 static struct btrfs_trans_handle
*evict_refill_and_join(struct btrfs_root
*root
,
5015 struct btrfs_block_rsv
*rsv
)
5017 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5018 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
5019 struct btrfs_trans_handle
*trans
;
5020 u64 delayed_refs_extra
= btrfs_calc_insert_metadata_size(fs_info
, 1);
5024 * Eviction should be taking place at some place safe because of our
5025 * delayed iputs. However the normal flushing code will run delayed
5026 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5028 * We reserve the delayed_refs_extra here again because we can't use
5029 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5030 * above. We reserve our extra bit here because we generate a ton of
5031 * delayed refs activity by truncating.
5033 * If we cannot make our reservation we'll attempt to steal from the
5034 * global reserve, because we really want to be able to free up space.
5036 ret
= btrfs_block_rsv_refill(root
, rsv
, rsv
->size
+ delayed_refs_extra
,
5037 BTRFS_RESERVE_FLUSH_EVICT
);
5040 * Try to steal from the global reserve if there is space for
5043 if (btrfs_check_space_for_delayed_refs(fs_info
) ||
5044 btrfs_block_rsv_migrate(global_rsv
, rsv
, rsv
->size
, 0)) {
5046 "could not allocate space for delete; will truncate on mount");
5047 return ERR_PTR(-ENOSPC
);
5049 delayed_refs_extra
= 0;
5052 trans
= btrfs_join_transaction(root
);
5056 if (delayed_refs_extra
) {
5057 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
5058 trans
->bytes_reserved
= delayed_refs_extra
;
5059 btrfs_block_rsv_migrate(rsv
, trans
->block_rsv
,
5060 delayed_refs_extra
, 1);
5065 void btrfs_evict_inode(struct inode
*inode
)
5067 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5068 struct btrfs_trans_handle
*trans
;
5069 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5070 struct btrfs_block_rsv
*rsv
;
5073 trace_btrfs_inode_evict(inode
);
5080 evict_inode_truncate_pages(inode
);
5082 if (inode
->i_nlink
&&
5083 ((btrfs_root_refs(&root
->root_item
) != 0 &&
5084 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
5085 btrfs_is_free_space_inode(BTRFS_I(inode
))))
5088 if (is_bad_inode(inode
))
5091 btrfs_free_io_failure_record(BTRFS_I(inode
), 0, (u64
)-1);
5093 if (test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
))
5096 if (inode
->i_nlink
> 0) {
5097 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
5098 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
5102 ret
= btrfs_commit_inode_delayed_inode(BTRFS_I(inode
));
5106 rsv
= btrfs_alloc_block_rsv(fs_info
, BTRFS_BLOCK_RSV_TEMP
);
5109 rsv
->size
= btrfs_calc_metadata_size(fs_info
, 1);
5112 btrfs_i_size_write(BTRFS_I(inode
), 0);
5115 trans
= evict_refill_and_join(root
, rsv
);
5119 trans
->block_rsv
= rsv
;
5121 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
5122 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
5123 btrfs_end_transaction(trans
);
5124 btrfs_btree_balance_dirty(fs_info
);
5125 if (ret
&& ret
!= -ENOSPC
&& ret
!= -EAGAIN
)
5132 * Errors here aren't a big deal, it just means we leave orphan items in
5133 * the tree. They will be cleaned up on the next mount. If the inode
5134 * number gets reused, cleanup deletes the orphan item without doing
5135 * anything, and unlink reuses the existing orphan item.
5137 * If it turns out that we are dropping too many of these, we might want
5138 * to add a mechanism for retrying these after a commit.
5140 trans
= evict_refill_and_join(root
, rsv
);
5141 if (!IS_ERR(trans
)) {
5142 trans
->block_rsv
= rsv
;
5143 btrfs_orphan_del(trans
, BTRFS_I(inode
));
5144 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
5145 btrfs_end_transaction(trans
);
5148 if (!(root
== fs_info
->tree_root
||
5149 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
5150 btrfs_return_ino(root
, btrfs_ino(BTRFS_I(inode
)));
5153 btrfs_free_block_rsv(fs_info
, rsv
);
5156 * If we didn't successfully delete, the orphan item will still be in
5157 * the tree and we'll retry on the next mount. Again, we might also want
5158 * to retry these periodically in the future.
5160 btrfs_remove_delayed_node(BTRFS_I(inode
));
5165 * Return the key found in the dir entry in the location pointer, fill @type
5166 * with BTRFS_FT_*, and return 0.
5168 * If no dir entries were found, returns -ENOENT.
5169 * If found a corrupted location in dir entry, returns -EUCLEAN.
5171 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
5172 struct btrfs_key
*location
, u8
*type
)
5174 const char *name
= dentry
->d_name
.name
;
5175 int namelen
= dentry
->d_name
.len
;
5176 struct btrfs_dir_item
*di
;
5177 struct btrfs_path
*path
;
5178 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5181 path
= btrfs_alloc_path();
5185 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(BTRFS_I(dir
)),
5187 if (IS_ERR_OR_NULL(di
)) {
5188 ret
= di
? PTR_ERR(di
) : -ENOENT
;
5192 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
5193 if (location
->type
!= BTRFS_INODE_ITEM_KEY
&&
5194 location
->type
!= BTRFS_ROOT_ITEM_KEY
) {
5196 btrfs_warn(root
->fs_info
,
5197 "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5198 __func__
, name
, btrfs_ino(BTRFS_I(dir
)),
5199 location
->objectid
, location
->type
, location
->offset
);
5202 *type
= btrfs_dir_type(path
->nodes
[0], di
);
5204 btrfs_free_path(path
);
5209 * when we hit a tree root in a directory, the btrfs part of the inode
5210 * needs to be changed to reflect the root directory of the tree root. This
5211 * is kind of like crossing a mount point.
5213 static int fixup_tree_root_location(struct btrfs_fs_info
*fs_info
,
5215 struct dentry
*dentry
,
5216 struct btrfs_key
*location
,
5217 struct btrfs_root
**sub_root
)
5219 struct btrfs_path
*path
;
5220 struct btrfs_root
*new_root
;
5221 struct btrfs_root_ref
*ref
;
5222 struct extent_buffer
*leaf
;
5223 struct btrfs_key key
;
5227 path
= btrfs_alloc_path();
5234 key
.objectid
= BTRFS_I(dir
)->root
->root_key
.objectid
;
5235 key
.type
= BTRFS_ROOT_REF_KEY
;
5236 key
.offset
= location
->objectid
;
5238 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
5245 leaf
= path
->nodes
[0];
5246 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
5247 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(BTRFS_I(dir
)) ||
5248 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
5251 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
5252 (unsigned long)(ref
+ 1),
5253 dentry
->d_name
.len
);
5257 btrfs_release_path(path
);
5259 new_root
= btrfs_get_fs_root(fs_info
, location
->objectid
, true);
5260 if (IS_ERR(new_root
)) {
5261 err
= PTR_ERR(new_root
);
5265 *sub_root
= new_root
;
5266 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
5267 location
->type
= BTRFS_INODE_ITEM_KEY
;
5268 location
->offset
= 0;
5271 btrfs_free_path(path
);
5275 static void inode_tree_add(struct inode
*inode
)
5277 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5278 struct btrfs_inode
*entry
;
5280 struct rb_node
*parent
;
5281 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
5282 u64 ino
= btrfs_ino(BTRFS_I(inode
));
5284 if (inode_unhashed(inode
))
5287 spin_lock(&root
->inode_lock
);
5288 p
= &root
->inode_tree
.rb_node
;
5291 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
5293 if (ino
< btrfs_ino(entry
))
5294 p
= &parent
->rb_left
;
5295 else if (ino
> btrfs_ino(entry
))
5296 p
= &parent
->rb_right
;
5298 WARN_ON(!(entry
->vfs_inode
.i_state
&
5299 (I_WILL_FREE
| I_FREEING
)));
5300 rb_replace_node(parent
, new, &root
->inode_tree
);
5301 RB_CLEAR_NODE(parent
);
5302 spin_unlock(&root
->inode_lock
);
5306 rb_link_node(new, parent
, p
);
5307 rb_insert_color(new, &root
->inode_tree
);
5308 spin_unlock(&root
->inode_lock
);
5311 static void inode_tree_del(struct inode
*inode
)
5313 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5316 spin_lock(&root
->inode_lock
);
5317 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
5318 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
5319 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
5320 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5322 spin_unlock(&root
->inode_lock
);
5324 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
5325 spin_lock(&root
->inode_lock
);
5326 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5327 spin_unlock(&root
->inode_lock
);
5329 btrfs_add_dead_root(root
);
5334 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
5336 struct btrfs_iget_args
*args
= p
;
5338 inode
->i_ino
= args
->ino
;
5339 BTRFS_I(inode
)->location
.objectid
= args
->ino
;
5340 BTRFS_I(inode
)->location
.type
= BTRFS_INODE_ITEM_KEY
;
5341 BTRFS_I(inode
)->location
.offset
= 0;
5342 BTRFS_I(inode
)->root
= btrfs_grab_root(args
->root
);
5343 BUG_ON(args
->root
&& !BTRFS_I(inode
)->root
);
5347 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
5349 struct btrfs_iget_args
*args
= opaque
;
5351 return args
->ino
== BTRFS_I(inode
)->location
.objectid
&&
5352 args
->root
== BTRFS_I(inode
)->root
;
5355 static struct inode
*btrfs_iget_locked(struct super_block
*s
, u64 ino
,
5356 struct btrfs_root
*root
)
5358 struct inode
*inode
;
5359 struct btrfs_iget_args args
;
5360 unsigned long hashval
= btrfs_inode_hash(ino
, root
);
5365 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
5366 btrfs_init_locked_inode
,
5372 * Get an inode object given its inode number and corresponding root.
5373 * Path can be preallocated to prevent recursing back to iget through
5374 * allocator. NULL is also valid but may require an additional allocation
5377 struct inode
*btrfs_iget_path(struct super_block
*s
, u64 ino
,
5378 struct btrfs_root
*root
, struct btrfs_path
*path
)
5380 struct inode
*inode
;
5382 inode
= btrfs_iget_locked(s
, ino
, root
);
5384 return ERR_PTR(-ENOMEM
);
5386 if (inode
->i_state
& I_NEW
) {
5389 ret
= btrfs_read_locked_inode(inode
, path
);
5391 inode_tree_add(inode
);
5392 unlock_new_inode(inode
);
5396 * ret > 0 can come from btrfs_search_slot called by
5397 * btrfs_read_locked_inode, this means the inode item
5402 inode
= ERR_PTR(ret
);
5409 struct inode
*btrfs_iget(struct super_block
*s
, u64 ino
, struct btrfs_root
*root
)
5411 return btrfs_iget_path(s
, ino
, root
, NULL
);
5414 static struct inode
*new_simple_dir(struct super_block
*s
,
5415 struct btrfs_key
*key
,
5416 struct btrfs_root
*root
)
5418 struct inode
*inode
= new_inode(s
);
5421 return ERR_PTR(-ENOMEM
);
5423 BTRFS_I(inode
)->root
= btrfs_grab_root(root
);
5424 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5425 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5427 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5429 * We only need lookup, the rest is read-only and there's no inode
5430 * associated with the dentry
5432 inode
->i_op
= &simple_dir_inode_operations
;
5433 inode
->i_opflags
&= ~IOP_XATTR
;
5434 inode
->i_fop
= &simple_dir_operations
;
5435 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5436 inode
->i_mtime
= current_time(inode
);
5437 inode
->i_atime
= inode
->i_mtime
;
5438 inode
->i_ctime
= inode
->i_mtime
;
5439 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
5444 static inline u8
btrfs_inode_type(struct inode
*inode
)
5447 * Compile-time asserts that generic FT_* types still match
5450 BUILD_BUG_ON(BTRFS_FT_UNKNOWN
!= FT_UNKNOWN
);
5451 BUILD_BUG_ON(BTRFS_FT_REG_FILE
!= FT_REG_FILE
);
5452 BUILD_BUG_ON(BTRFS_FT_DIR
!= FT_DIR
);
5453 BUILD_BUG_ON(BTRFS_FT_CHRDEV
!= FT_CHRDEV
);
5454 BUILD_BUG_ON(BTRFS_FT_BLKDEV
!= FT_BLKDEV
);
5455 BUILD_BUG_ON(BTRFS_FT_FIFO
!= FT_FIFO
);
5456 BUILD_BUG_ON(BTRFS_FT_SOCK
!= FT_SOCK
);
5457 BUILD_BUG_ON(BTRFS_FT_SYMLINK
!= FT_SYMLINK
);
5459 return fs_umode_to_ftype(inode
->i_mode
);
5462 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5464 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
5465 struct inode
*inode
;
5466 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5467 struct btrfs_root
*sub_root
= root
;
5468 struct btrfs_key location
;
5472 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5473 return ERR_PTR(-ENAMETOOLONG
);
5475 ret
= btrfs_inode_by_name(dir
, dentry
, &location
, &di_type
);
5477 return ERR_PTR(ret
);
5479 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5480 inode
= btrfs_iget(dir
->i_sb
, location
.objectid
, root
);
5484 /* Do extra check against inode mode with di_type */
5485 if (btrfs_inode_type(inode
) != di_type
) {
5487 "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5488 inode
->i_mode
, btrfs_inode_type(inode
),
5491 return ERR_PTR(-EUCLEAN
);
5496 ret
= fixup_tree_root_location(fs_info
, dir
, dentry
,
5497 &location
, &sub_root
);
5500 inode
= ERR_PTR(ret
);
5502 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5504 inode
= btrfs_iget(dir
->i_sb
, location
.objectid
, sub_root
);
5506 if (root
!= sub_root
)
5507 btrfs_put_root(sub_root
);
5509 if (!IS_ERR(inode
) && root
!= sub_root
) {
5510 down_read(&fs_info
->cleanup_work_sem
);
5511 if (!sb_rdonly(inode
->i_sb
))
5512 ret
= btrfs_orphan_cleanup(sub_root
);
5513 up_read(&fs_info
->cleanup_work_sem
);
5516 inode
= ERR_PTR(ret
);
5523 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5525 struct btrfs_root
*root
;
5526 struct inode
*inode
= d_inode(dentry
);
5528 if (!inode
&& !IS_ROOT(dentry
))
5529 inode
= d_inode(dentry
->d_parent
);
5532 root
= BTRFS_I(inode
)->root
;
5533 if (btrfs_root_refs(&root
->root_item
) == 0)
5536 if (btrfs_ino(BTRFS_I(inode
)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5542 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5545 struct inode
*inode
= btrfs_lookup_dentry(dir
, dentry
);
5547 if (inode
== ERR_PTR(-ENOENT
))
5549 return d_splice_alias(inode
, dentry
);
5553 * All this infrastructure exists because dir_emit can fault, and we are holding
5554 * the tree lock when doing readdir. For now just allocate a buffer and copy
5555 * our information into that, and then dir_emit from the buffer. This is
5556 * similar to what NFS does, only we don't keep the buffer around in pagecache
5557 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5558 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5561 static int btrfs_opendir(struct inode
*inode
, struct file
*file
)
5563 struct btrfs_file_private
*private;
5565 private = kzalloc(sizeof(struct btrfs_file_private
), GFP_KERNEL
);
5568 private->filldir_buf
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
5569 if (!private->filldir_buf
) {
5573 file
->private_data
= private;
5584 static int btrfs_filldir(void *addr
, int entries
, struct dir_context
*ctx
)
5587 struct dir_entry
*entry
= addr
;
5588 char *name
= (char *)(entry
+ 1);
5590 ctx
->pos
= get_unaligned(&entry
->offset
);
5591 if (!dir_emit(ctx
, name
, get_unaligned(&entry
->name_len
),
5592 get_unaligned(&entry
->ino
),
5593 get_unaligned(&entry
->type
)))
5595 addr
+= sizeof(struct dir_entry
) +
5596 get_unaligned(&entry
->name_len
);
5602 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
5604 struct inode
*inode
= file_inode(file
);
5605 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5606 struct btrfs_file_private
*private = file
->private_data
;
5607 struct btrfs_dir_item
*di
;
5608 struct btrfs_key key
;
5609 struct btrfs_key found_key
;
5610 struct btrfs_path
*path
;
5612 struct list_head ins_list
;
5613 struct list_head del_list
;
5615 struct extent_buffer
*leaf
;
5622 struct btrfs_key location
;
5624 if (!dir_emit_dots(file
, ctx
))
5627 path
= btrfs_alloc_path();
5631 addr
= private->filldir_buf
;
5632 path
->reada
= READA_FORWARD
;
5634 INIT_LIST_HEAD(&ins_list
);
5635 INIT_LIST_HEAD(&del_list
);
5636 put
= btrfs_readdir_get_delayed_items(inode
, &ins_list
, &del_list
);
5639 key
.type
= BTRFS_DIR_INDEX_KEY
;
5640 key
.offset
= ctx
->pos
;
5641 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
5643 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5648 struct dir_entry
*entry
;
5650 leaf
= path
->nodes
[0];
5651 slot
= path
->slots
[0];
5652 if (slot
>= btrfs_header_nritems(leaf
)) {
5653 ret
= btrfs_next_leaf(root
, path
);
5661 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5663 if (found_key
.objectid
!= key
.objectid
)
5665 if (found_key
.type
!= BTRFS_DIR_INDEX_KEY
)
5667 if (found_key
.offset
< ctx
->pos
)
5669 if (btrfs_should_delete_dir_index(&del_list
, found_key
.offset
))
5671 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5672 name_len
= btrfs_dir_name_len(leaf
, di
);
5673 if ((total_len
+ sizeof(struct dir_entry
) + name_len
) >=
5675 btrfs_release_path(path
);
5676 ret
= btrfs_filldir(private->filldir_buf
, entries
, ctx
);
5679 addr
= private->filldir_buf
;
5686 put_unaligned(name_len
, &entry
->name_len
);
5687 name_ptr
= (char *)(entry
+ 1);
5688 read_extent_buffer(leaf
, name_ptr
, (unsigned long)(di
+ 1),
5690 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf
, di
)),
5692 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5693 put_unaligned(location
.objectid
, &entry
->ino
);
5694 put_unaligned(found_key
.offset
, &entry
->offset
);
5696 addr
+= sizeof(struct dir_entry
) + name_len
;
5697 total_len
+= sizeof(struct dir_entry
) + name_len
;
5701 btrfs_release_path(path
);
5703 ret
= btrfs_filldir(private->filldir_buf
, entries
, ctx
);
5707 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
);
5712 * Stop new entries from being returned after we return the last
5715 * New directory entries are assigned a strictly increasing
5716 * offset. This means that new entries created during readdir
5717 * are *guaranteed* to be seen in the future by that readdir.
5718 * This has broken buggy programs which operate on names as
5719 * they're returned by readdir. Until we re-use freed offsets
5720 * we have this hack to stop new entries from being returned
5721 * under the assumption that they'll never reach this huge
5724 * This is being careful not to overflow 32bit loff_t unless the
5725 * last entry requires it because doing so has broken 32bit apps
5728 if (ctx
->pos
>= INT_MAX
)
5729 ctx
->pos
= LLONG_MAX
;
5736 btrfs_readdir_put_delayed_items(inode
, &ins_list
, &del_list
);
5737 btrfs_free_path(path
);
5742 * This is somewhat expensive, updating the tree every time the
5743 * inode changes. But, it is most likely to find the inode in cache.
5744 * FIXME, needs more benchmarking...there are no reasons other than performance
5745 * to keep or drop this code.
5747 static int btrfs_dirty_inode(struct inode
*inode
)
5749 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5750 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5751 struct btrfs_trans_handle
*trans
;
5754 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5757 trans
= btrfs_join_transaction(root
);
5759 return PTR_ERR(trans
);
5761 ret
= btrfs_update_inode(trans
, root
, inode
);
5762 if (ret
&& ret
== -ENOSPC
) {
5763 /* whoops, lets try again with the full transaction */
5764 btrfs_end_transaction(trans
);
5765 trans
= btrfs_start_transaction(root
, 1);
5767 return PTR_ERR(trans
);
5769 ret
= btrfs_update_inode(trans
, root
, inode
);
5771 btrfs_end_transaction(trans
);
5772 if (BTRFS_I(inode
)->delayed_node
)
5773 btrfs_balance_delayed_items(fs_info
);
5779 * This is a copy of file_update_time. We need this so we can return error on
5780 * ENOSPC for updating the inode in the case of file write and mmap writes.
5782 static int btrfs_update_time(struct inode
*inode
, struct timespec64
*now
,
5785 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5786 bool dirty
= flags
& ~S_VERSION
;
5788 if (btrfs_root_readonly(root
))
5791 if (flags
& S_VERSION
)
5792 dirty
|= inode_maybe_inc_iversion(inode
, dirty
);
5793 if (flags
& S_CTIME
)
5794 inode
->i_ctime
= *now
;
5795 if (flags
& S_MTIME
)
5796 inode
->i_mtime
= *now
;
5797 if (flags
& S_ATIME
)
5798 inode
->i_atime
= *now
;
5799 return dirty
? btrfs_dirty_inode(inode
) : 0;
5803 * find the highest existing sequence number in a directory
5804 * and then set the in-memory index_cnt variable to reflect
5805 * free sequence numbers
5807 static int btrfs_set_inode_index_count(struct btrfs_inode
*inode
)
5809 struct btrfs_root
*root
= inode
->root
;
5810 struct btrfs_key key
, found_key
;
5811 struct btrfs_path
*path
;
5812 struct extent_buffer
*leaf
;
5815 key
.objectid
= btrfs_ino(inode
);
5816 key
.type
= BTRFS_DIR_INDEX_KEY
;
5817 key
.offset
= (u64
)-1;
5819 path
= btrfs_alloc_path();
5823 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5826 /* FIXME: we should be able to handle this */
5832 * MAGIC NUMBER EXPLANATION:
5833 * since we search a directory based on f_pos we have to start at 2
5834 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5835 * else has to start at 2
5837 if (path
->slots
[0] == 0) {
5838 inode
->index_cnt
= 2;
5844 leaf
= path
->nodes
[0];
5845 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5847 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5848 found_key
.type
!= BTRFS_DIR_INDEX_KEY
) {
5849 inode
->index_cnt
= 2;
5853 inode
->index_cnt
= found_key
.offset
+ 1;
5855 btrfs_free_path(path
);
5860 * helper to find a free sequence number in a given directory. This current
5861 * code is very simple, later versions will do smarter things in the btree
5863 int btrfs_set_inode_index(struct btrfs_inode
*dir
, u64
*index
)
5867 if (dir
->index_cnt
== (u64
)-1) {
5868 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5870 ret
= btrfs_set_inode_index_count(dir
);
5876 *index
= dir
->index_cnt
;
5882 static int btrfs_insert_inode_locked(struct inode
*inode
)
5884 struct btrfs_iget_args args
;
5886 args
.ino
= BTRFS_I(inode
)->location
.objectid
;
5887 args
.root
= BTRFS_I(inode
)->root
;
5889 return insert_inode_locked4(inode
,
5890 btrfs_inode_hash(inode
->i_ino
, BTRFS_I(inode
)->root
),
5891 btrfs_find_actor
, &args
);
5895 * Inherit flags from the parent inode.
5897 * Currently only the compression flags and the cow flags are inherited.
5899 static void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
5906 flags
= BTRFS_I(dir
)->flags
;
5908 if (flags
& BTRFS_INODE_NOCOMPRESS
) {
5909 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_COMPRESS
;
5910 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
5911 } else if (flags
& BTRFS_INODE_COMPRESS
) {
5912 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
5913 BTRFS_I(inode
)->flags
|= BTRFS_INODE_COMPRESS
;
5916 if (flags
& BTRFS_INODE_NODATACOW
) {
5917 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
5918 if (S_ISREG(inode
->i_mode
))
5919 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5922 btrfs_sync_inode_flags_to_i_flags(inode
);
5925 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5926 struct btrfs_root
*root
,
5928 const char *name
, int name_len
,
5929 u64 ref_objectid
, u64 objectid
,
5930 umode_t mode
, u64
*index
)
5932 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5933 struct inode
*inode
;
5934 struct btrfs_inode_item
*inode_item
;
5935 struct btrfs_key
*location
;
5936 struct btrfs_path
*path
;
5937 struct btrfs_inode_ref
*ref
;
5938 struct btrfs_key key
[2];
5940 int nitems
= name
? 2 : 1;
5942 unsigned int nofs_flag
;
5945 path
= btrfs_alloc_path();
5947 return ERR_PTR(-ENOMEM
);
5949 nofs_flag
= memalloc_nofs_save();
5950 inode
= new_inode(fs_info
->sb
);
5951 memalloc_nofs_restore(nofs_flag
);
5953 btrfs_free_path(path
);
5954 return ERR_PTR(-ENOMEM
);
5958 * O_TMPFILE, set link count to 0, so that after this point,
5959 * we fill in an inode item with the correct link count.
5962 set_nlink(inode
, 0);
5965 * we have to initialize this early, so we can reclaim the inode
5966 * number if we fail afterwards in this function.
5968 inode
->i_ino
= objectid
;
5971 trace_btrfs_inode_request(dir
);
5973 ret
= btrfs_set_inode_index(BTRFS_I(dir
), index
);
5975 btrfs_free_path(path
);
5977 return ERR_PTR(ret
);
5983 * index_cnt is ignored for everything but a dir,
5984 * btrfs_set_inode_index_count has an explanation for the magic
5987 BTRFS_I(inode
)->index_cnt
= 2;
5988 BTRFS_I(inode
)->dir_index
= *index
;
5989 BTRFS_I(inode
)->root
= btrfs_grab_root(root
);
5990 BTRFS_I(inode
)->generation
= trans
->transid
;
5991 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5994 * We could have gotten an inode number from somebody who was fsynced
5995 * and then removed in this same transaction, so let's just set full
5996 * sync since it will be a full sync anyway and this will blow away the
5997 * old info in the log.
5999 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
6001 key
[0].objectid
= objectid
;
6002 key
[0].type
= BTRFS_INODE_ITEM_KEY
;
6005 sizes
[0] = sizeof(struct btrfs_inode_item
);
6009 * Start new inodes with an inode_ref. This is slightly more
6010 * efficient for small numbers of hard links since they will
6011 * be packed into one item. Extended refs will kick in if we
6012 * add more hard links than can fit in the ref item.
6014 key
[1].objectid
= objectid
;
6015 key
[1].type
= BTRFS_INODE_REF_KEY
;
6016 key
[1].offset
= ref_objectid
;
6018 sizes
[1] = name_len
+ sizeof(*ref
);
6021 location
= &BTRFS_I(inode
)->location
;
6022 location
->objectid
= objectid
;
6023 location
->offset
= 0;
6024 location
->type
= BTRFS_INODE_ITEM_KEY
;
6026 ret
= btrfs_insert_inode_locked(inode
);
6032 path
->leave_spinning
= 1;
6033 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, nitems
);
6037 inode_init_owner(inode
, dir
, mode
);
6038 inode_set_bytes(inode
, 0);
6040 inode
->i_mtime
= current_time(inode
);
6041 inode
->i_atime
= inode
->i_mtime
;
6042 inode
->i_ctime
= inode
->i_mtime
;
6043 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
6045 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
6046 struct btrfs_inode_item
);
6047 memzero_extent_buffer(path
->nodes
[0], (unsigned long)inode_item
,
6048 sizeof(*inode_item
));
6049 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
6052 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
6053 struct btrfs_inode_ref
);
6054 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
6055 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
6056 ptr
= (unsigned long)(ref
+ 1);
6057 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
6060 btrfs_mark_buffer_dirty(path
->nodes
[0]);
6061 btrfs_free_path(path
);
6063 btrfs_inherit_iflags(inode
, dir
);
6065 if (S_ISREG(mode
)) {
6066 if (btrfs_test_opt(fs_info
, NODATASUM
))
6067 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
6068 if (btrfs_test_opt(fs_info
, NODATACOW
))
6069 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
6070 BTRFS_INODE_NODATASUM
;
6073 inode_tree_add(inode
);
6075 trace_btrfs_inode_new(inode
);
6076 btrfs_set_inode_last_trans(trans
, BTRFS_I(inode
));
6078 btrfs_update_root_times(trans
, root
);
6080 ret
= btrfs_inode_inherit_props(trans
, inode
, dir
);
6083 "error inheriting props for ino %llu (root %llu): %d",
6084 btrfs_ino(BTRFS_I(inode
)), root
->root_key
.objectid
, ret
);
6089 discard_new_inode(inode
);
6092 BTRFS_I(dir
)->index_cnt
--;
6093 btrfs_free_path(path
);
6094 return ERR_PTR(ret
);
6098 * utility function to add 'inode' into 'parent_inode' with
6099 * a give name and a given sequence number.
6100 * if 'add_backref' is true, also insert a backref from the
6101 * inode to the parent directory.
6103 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
6104 struct btrfs_inode
*parent_inode
, struct btrfs_inode
*inode
,
6105 const char *name
, int name_len
, int add_backref
, u64 index
)
6108 struct btrfs_key key
;
6109 struct btrfs_root
*root
= parent_inode
->root
;
6110 u64 ino
= btrfs_ino(inode
);
6111 u64 parent_ino
= btrfs_ino(parent_inode
);
6113 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6114 memcpy(&key
, &inode
->root
->root_key
, sizeof(key
));
6117 key
.type
= BTRFS_INODE_ITEM_KEY
;
6121 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6122 ret
= btrfs_add_root_ref(trans
, key
.objectid
,
6123 root
->root_key
.objectid
, parent_ino
,
6124 index
, name
, name_len
);
6125 } else if (add_backref
) {
6126 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
6130 /* Nothing to clean up yet */
6134 ret
= btrfs_insert_dir_item(trans
, name
, name_len
, parent_inode
, &key
,
6135 btrfs_inode_type(&inode
->vfs_inode
), index
);
6136 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
6139 btrfs_abort_transaction(trans
, ret
);
6143 btrfs_i_size_write(parent_inode
, parent_inode
->vfs_inode
.i_size
+
6145 inode_inc_iversion(&parent_inode
->vfs_inode
);
6147 * If we are replaying a log tree, we do not want to update the mtime
6148 * and ctime of the parent directory with the current time, since the
6149 * log replay procedure is responsible for setting them to their correct
6150 * values (the ones it had when the fsync was done).
6152 if (!test_bit(BTRFS_FS_LOG_RECOVERING
, &root
->fs_info
->flags
)) {
6153 struct timespec64 now
= current_time(&parent_inode
->vfs_inode
);
6155 parent_inode
->vfs_inode
.i_mtime
= now
;
6156 parent_inode
->vfs_inode
.i_ctime
= now
;
6158 ret
= btrfs_update_inode(trans
, root
, &parent_inode
->vfs_inode
);
6160 btrfs_abort_transaction(trans
, ret
);
6164 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6167 err
= btrfs_del_root_ref(trans
, key
.objectid
,
6168 root
->root_key
.objectid
, parent_ino
,
6169 &local_index
, name
, name_len
);
6171 btrfs_abort_transaction(trans
, err
);
6172 } else if (add_backref
) {
6176 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
6177 ino
, parent_ino
, &local_index
);
6179 btrfs_abort_transaction(trans
, err
);
6182 /* Return the original error code */
6186 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
6187 struct btrfs_inode
*dir
, struct dentry
*dentry
,
6188 struct btrfs_inode
*inode
, int backref
, u64 index
)
6190 int err
= btrfs_add_link(trans
, dir
, inode
,
6191 dentry
->d_name
.name
, dentry
->d_name
.len
,
6198 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
6199 umode_t mode
, dev_t rdev
)
6201 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6202 struct btrfs_trans_handle
*trans
;
6203 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6204 struct inode
*inode
= NULL
;
6210 * 2 for inode item and ref
6212 * 1 for xattr if selinux is on
6214 trans
= btrfs_start_transaction(root
, 5);
6216 return PTR_ERR(trans
);
6218 err
= btrfs_find_free_ino(root
, &objectid
);
6222 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6223 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)), objectid
,
6225 if (IS_ERR(inode
)) {
6226 err
= PTR_ERR(inode
);
6232 * If the active LSM wants to access the inode during
6233 * d_instantiate it needs these. Smack checks to see
6234 * if the filesystem supports xattrs by looking at the
6237 inode
->i_op
= &btrfs_special_inode_operations
;
6238 init_special_inode(inode
, inode
->i_mode
, rdev
);
6240 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6244 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
, BTRFS_I(inode
),
6249 btrfs_update_inode(trans
, root
, inode
);
6250 d_instantiate_new(dentry
, inode
);
6253 btrfs_end_transaction(trans
);
6254 btrfs_btree_balance_dirty(fs_info
);
6256 inode_dec_link_count(inode
);
6257 discard_new_inode(inode
);
6262 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
6263 umode_t mode
, bool excl
)
6265 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6266 struct btrfs_trans_handle
*trans
;
6267 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6268 struct inode
*inode
= NULL
;
6274 * 2 for inode item and ref
6276 * 1 for xattr if selinux is on
6278 trans
= btrfs_start_transaction(root
, 5);
6280 return PTR_ERR(trans
);
6282 err
= btrfs_find_free_ino(root
, &objectid
);
6286 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6287 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)), objectid
,
6289 if (IS_ERR(inode
)) {
6290 err
= PTR_ERR(inode
);
6295 * If the active LSM wants to access the inode during
6296 * d_instantiate it needs these. Smack checks to see
6297 * if the filesystem supports xattrs by looking at the
6300 inode
->i_fop
= &btrfs_file_operations
;
6301 inode
->i_op
= &btrfs_file_inode_operations
;
6302 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6304 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6308 err
= btrfs_update_inode(trans
, root
, inode
);
6312 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
, BTRFS_I(inode
),
6317 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6318 d_instantiate_new(dentry
, inode
);
6321 btrfs_end_transaction(trans
);
6323 inode_dec_link_count(inode
);
6324 discard_new_inode(inode
);
6326 btrfs_btree_balance_dirty(fs_info
);
6330 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
6331 struct dentry
*dentry
)
6333 struct btrfs_trans_handle
*trans
= NULL
;
6334 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6335 struct inode
*inode
= d_inode(old_dentry
);
6336 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
6341 /* do not allow sys_link's with other subvols of the same device */
6342 if (root
->root_key
.objectid
!= BTRFS_I(inode
)->root
->root_key
.objectid
)
6345 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
6348 err
= btrfs_set_inode_index(BTRFS_I(dir
), &index
);
6353 * 2 items for inode and inode ref
6354 * 2 items for dir items
6355 * 1 item for parent inode
6356 * 1 item for orphan item deletion if O_TMPFILE
6358 trans
= btrfs_start_transaction(root
, inode
->i_nlink
? 5 : 6);
6359 if (IS_ERR(trans
)) {
6360 err
= PTR_ERR(trans
);
6365 /* There are several dir indexes for this inode, clear the cache. */
6366 BTRFS_I(inode
)->dir_index
= 0ULL;
6368 inode_inc_iversion(inode
);
6369 inode
->i_ctime
= current_time(inode
);
6371 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
6373 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
, BTRFS_I(inode
),
6379 struct dentry
*parent
= dentry
->d_parent
;
6382 err
= btrfs_update_inode(trans
, root
, inode
);
6385 if (inode
->i_nlink
== 1) {
6387 * If new hard link count is 1, it's a file created
6388 * with open(2) O_TMPFILE flag.
6390 err
= btrfs_orphan_del(trans
, BTRFS_I(inode
));
6394 d_instantiate(dentry
, inode
);
6395 ret
= btrfs_log_new_name(trans
, BTRFS_I(inode
), NULL
, parent
,
6397 if (ret
== BTRFS_NEED_TRANS_COMMIT
) {
6398 err
= btrfs_commit_transaction(trans
);
6405 btrfs_end_transaction(trans
);
6407 inode_dec_link_count(inode
);
6410 btrfs_btree_balance_dirty(fs_info
);
6414 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
6416 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6417 struct inode
*inode
= NULL
;
6418 struct btrfs_trans_handle
*trans
;
6419 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6425 * 2 items for inode and ref
6426 * 2 items for dir items
6427 * 1 for xattr if selinux is on
6429 trans
= btrfs_start_transaction(root
, 5);
6431 return PTR_ERR(trans
);
6433 err
= btrfs_find_free_ino(root
, &objectid
);
6437 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6438 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)), objectid
,
6439 S_IFDIR
| mode
, &index
);
6440 if (IS_ERR(inode
)) {
6441 err
= PTR_ERR(inode
);
6446 /* these must be set before we unlock the inode */
6447 inode
->i_op
= &btrfs_dir_inode_operations
;
6448 inode
->i_fop
= &btrfs_dir_file_operations
;
6450 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6454 btrfs_i_size_write(BTRFS_I(inode
), 0);
6455 err
= btrfs_update_inode(trans
, root
, inode
);
6459 err
= btrfs_add_link(trans
, BTRFS_I(dir
), BTRFS_I(inode
),
6460 dentry
->d_name
.name
,
6461 dentry
->d_name
.len
, 0, index
);
6465 d_instantiate_new(dentry
, inode
);
6468 btrfs_end_transaction(trans
);
6470 inode_dec_link_count(inode
);
6471 discard_new_inode(inode
);
6473 btrfs_btree_balance_dirty(fs_info
);
6477 static noinline
int uncompress_inline(struct btrfs_path
*path
,
6479 size_t pg_offset
, u64 extent_offset
,
6480 struct btrfs_file_extent_item
*item
)
6483 struct extent_buffer
*leaf
= path
->nodes
[0];
6486 unsigned long inline_size
;
6490 WARN_ON(pg_offset
!= 0);
6491 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6492 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6493 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6494 btrfs_item_nr(path
->slots
[0]));
6495 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6498 ptr
= btrfs_file_extent_inline_start(item
);
6500 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6502 max_size
= min_t(unsigned long, PAGE_SIZE
, max_size
);
6503 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6504 extent_offset
, inline_size
, max_size
);
6507 * decompression code contains a memset to fill in any space between the end
6508 * of the uncompressed data and the end of max_size in case the decompressed
6509 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6510 * the end of an inline extent and the beginning of the next block, so we
6511 * cover that region here.
6514 if (max_size
+ pg_offset
< PAGE_SIZE
) {
6515 char *map
= kmap(page
);
6516 memset(map
+ pg_offset
+ max_size
, 0, PAGE_SIZE
- max_size
- pg_offset
);
6524 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6525 * @inode: file to search in
6526 * @page: page to read extent data into if the extent is inline
6527 * @pg_offset: offset into @page to copy to
6528 * @start: file offset
6529 * @len: length of range starting at @start
6531 * This returns the first &struct extent_map which overlaps with the given
6532 * range, reading it from the B-tree and caching it if necessary. Note that
6533 * there may be more extents which overlap the given range after the returned
6536 * If @page is not NULL and the extent is inline, this also reads the extent
6537 * data directly into the page and marks the extent up to date in the io_tree.
6539 * Return: ERR_PTR on error, non-NULL extent_map on success.
6541 struct extent_map
*btrfs_get_extent(struct btrfs_inode
*inode
,
6542 struct page
*page
, size_t pg_offset
,
6545 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
6548 u64 extent_start
= 0;
6550 u64 objectid
= btrfs_ino(inode
);
6551 int extent_type
= -1;
6552 struct btrfs_path
*path
= NULL
;
6553 struct btrfs_root
*root
= inode
->root
;
6554 struct btrfs_file_extent_item
*item
;
6555 struct extent_buffer
*leaf
;
6556 struct btrfs_key found_key
;
6557 struct extent_map
*em
= NULL
;
6558 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
6559 struct extent_io_tree
*io_tree
= &inode
->io_tree
;
6561 read_lock(&em_tree
->lock
);
6562 em
= lookup_extent_mapping(em_tree
, start
, len
);
6563 read_unlock(&em_tree
->lock
);
6566 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6567 free_extent_map(em
);
6568 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6569 free_extent_map(em
);
6573 em
= alloc_extent_map();
6578 em
->start
= EXTENT_MAP_HOLE
;
6579 em
->orig_start
= EXTENT_MAP_HOLE
;
6581 em
->block_len
= (u64
)-1;
6583 path
= btrfs_alloc_path();
6589 /* Chances are we'll be called again, so go ahead and do readahead */
6590 path
->reada
= READA_FORWARD
;
6593 * Unless we're going to uncompress the inline extent, no sleep would
6596 path
->leave_spinning
= 1;
6598 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, objectid
, start
, 0);
6602 } else if (ret
> 0) {
6603 if (path
->slots
[0] == 0)
6608 leaf
= path
->nodes
[0];
6609 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6610 struct btrfs_file_extent_item
);
6611 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6612 if (found_key
.objectid
!= objectid
||
6613 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6615 * If we backup past the first extent we want to move forward
6616 * and see if there is an extent in front of us, otherwise we'll
6617 * say there is a hole for our whole search range which can
6624 extent_type
= btrfs_file_extent_type(leaf
, item
);
6625 extent_start
= found_key
.offset
;
6626 extent_end
= btrfs_file_extent_end(path
);
6627 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
6628 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6629 /* Only regular file could have regular/prealloc extent */
6630 if (!S_ISREG(inode
->vfs_inode
.i_mode
)) {
6633 "regular/prealloc extent found for non-regular inode %llu",
6637 trace_btrfs_get_extent_show_fi_regular(inode
, leaf
, item
,
6639 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
6640 trace_btrfs_get_extent_show_fi_inline(inode
, leaf
, item
,
6645 if (start
>= extent_end
) {
6647 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6648 ret
= btrfs_next_leaf(root
, path
);
6652 } else if (ret
> 0) {
6655 leaf
= path
->nodes
[0];
6657 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6658 if (found_key
.objectid
!= objectid
||
6659 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6661 if (start
+ len
<= found_key
.offset
)
6663 if (start
> found_key
.offset
)
6666 /* New extent overlaps with existing one */
6668 em
->orig_start
= start
;
6669 em
->len
= found_key
.offset
- start
;
6670 em
->block_start
= EXTENT_MAP_HOLE
;
6674 btrfs_extent_item_to_extent_map(inode
, path
, item
, !page
, em
);
6676 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
6677 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6679 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
6683 size_t extent_offset
;
6689 size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6690 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6691 copy_size
= min_t(u64
, PAGE_SIZE
- pg_offset
,
6692 size
- extent_offset
);
6693 em
->start
= extent_start
+ extent_offset
;
6694 em
->len
= ALIGN(copy_size
, fs_info
->sectorsize
);
6695 em
->orig_block_len
= em
->len
;
6696 em
->orig_start
= em
->start
;
6697 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6699 btrfs_set_path_blocking(path
);
6700 if (!PageUptodate(page
)) {
6701 if (btrfs_file_extent_compression(leaf
, item
) !=
6702 BTRFS_COMPRESS_NONE
) {
6703 ret
= uncompress_inline(path
, page
, pg_offset
,
6704 extent_offset
, item
);
6711 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6713 if (pg_offset
+ copy_size
< PAGE_SIZE
) {
6714 memset(map
+ pg_offset
+ copy_size
, 0,
6715 PAGE_SIZE
- pg_offset
-
6720 flush_dcache_page(page
);
6722 set_extent_uptodate(io_tree
, em
->start
,
6723 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6728 em
->orig_start
= start
;
6730 em
->block_start
= EXTENT_MAP_HOLE
;
6732 btrfs_release_path(path
);
6733 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6735 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6736 em
->start
, em
->len
, start
, len
);
6742 write_lock(&em_tree
->lock
);
6743 err
= btrfs_add_extent_mapping(fs_info
, em_tree
, &em
, start
, len
);
6744 write_unlock(&em_tree
->lock
);
6746 btrfs_free_path(path
);
6748 trace_btrfs_get_extent(root
, inode
, em
);
6751 free_extent_map(em
);
6752 return ERR_PTR(err
);
6754 BUG_ON(!em
); /* Error is always set */
6758 struct extent_map
*btrfs_get_extent_fiemap(struct btrfs_inode
*inode
,
6761 struct extent_map
*em
;
6762 struct extent_map
*hole_em
= NULL
;
6763 u64 delalloc_start
= start
;
6769 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
);
6773 * If our em maps to:
6775 * - a pre-alloc extent,
6776 * there might actually be delalloc bytes behind it.
6778 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6779 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6784 /* check to see if we've wrapped (len == -1 or similar) */
6793 /* ok, we didn't find anything, lets look for delalloc */
6794 delalloc_len
= count_range_bits(&inode
->io_tree
, &delalloc_start
,
6795 end
, len
, EXTENT_DELALLOC
, 1);
6796 delalloc_end
= delalloc_start
+ delalloc_len
;
6797 if (delalloc_end
< delalloc_start
)
6798 delalloc_end
= (u64
)-1;
6801 * We didn't find anything useful, return the original results from
6804 if (delalloc_start
> end
|| delalloc_end
<= start
) {
6811 * Adjust the delalloc_start to make sure it doesn't go backwards from
6812 * the start they passed in
6814 delalloc_start
= max(start
, delalloc_start
);
6815 delalloc_len
= delalloc_end
- delalloc_start
;
6817 if (delalloc_len
> 0) {
6820 const u64 hole_end
= extent_map_end(hole_em
);
6822 em
= alloc_extent_map();
6830 * When btrfs_get_extent can't find anything it returns one
6833 * Make sure what it found really fits our range, and adjust to
6834 * make sure it is based on the start from the caller
6836 if (hole_end
<= start
|| hole_em
->start
> end
) {
6837 free_extent_map(hole_em
);
6840 hole_start
= max(hole_em
->start
, start
);
6841 hole_len
= hole_end
- hole_start
;
6844 if (hole_em
&& delalloc_start
> hole_start
) {
6846 * Our hole starts before our delalloc, so we have to
6847 * return just the parts of the hole that go until the
6850 em
->len
= min(hole_len
, delalloc_start
- hole_start
);
6851 em
->start
= hole_start
;
6852 em
->orig_start
= hole_start
;
6854 * Don't adjust block start at all, it is fixed at
6857 em
->block_start
= hole_em
->block_start
;
6858 em
->block_len
= hole_len
;
6859 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6860 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6863 * Hole is out of passed range or it starts after
6866 em
->start
= delalloc_start
;
6867 em
->len
= delalloc_len
;
6868 em
->orig_start
= delalloc_start
;
6869 em
->block_start
= EXTENT_MAP_DELALLOC
;
6870 em
->block_len
= delalloc_len
;
6877 free_extent_map(hole_em
);
6879 free_extent_map(em
);
6880 return ERR_PTR(err
);
6885 static struct extent_map
*btrfs_create_dio_extent(struct btrfs_inode
*inode
,
6888 const u64 orig_start
,
6889 const u64 block_start
,
6890 const u64 block_len
,
6891 const u64 orig_block_len
,
6892 const u64 ram_bytes
,
6895 struct extent_map
*em
= NULL
;
6898 if (type
!= BTRFS_ORDERED_NOCOW
) {
6899 em
= create_io_em(inode
, start
, len
, orig_start
, block_start
,
6900 block_len
, orig_block_len
, ram_bytes
,
6901 BTRFS_COMPRESS_NONE
, /* compress_type */
6906 ret
= btrfs_add_ordered_extent_dio(inode
, start
, block_start
, len
,
6910 free_extent_map(em
);
6911 btrfs_drop_extent_cache(inode
, start
, start
+ len
- 1, 0);
6920 static struct extent_map
*btrfs_new_extent_direct(struct btrfs_inode
*inode
,
6923 struct btrfs_root
*root
= inode
->root
;
6924 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6925 struct extent_map
*em
;
6926 struct btrfs_key ins
;
6930 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6931 ret
= btrfs_reserve_extent(root
, len
, len
, fs_info
->sectorsize
,
6932 0, alloc_hint
, &ins
, 1, 1);
6934 return ERR_PTR(ret
);
6936 em
= btrfs_create_dio_extent(inode
, start
, ins
.offset
, start
,
6937 ins
.objectid
, ins
.offset
, ins
.offset
,
6938 ins
.offset
, BTRFS_ORDERED_REGULAR
);
6939 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
6941 btrfs_free_reserved_extent(fs_info
, ins
.objectid
, ins
.offset
,
6948 * Check if we can do nocow write into the range [@offset, @offset + @len)
6950 * @offset: File offset
6951 * @len: The length to write, will be updated to the nocow writeable
6953 * @orig_start: (optional) Return the original file offset of the file extent
6954 * @orig_len: (optional) Return the original on-disk length of the file extent
6955 * @ram_bytes: (optional) Return the ram_bytes of the file extent
6957 * This function will flush ordered extents in the range to ensure proper
6958 * nocow checks for (nowait == false) case.
6961 * >0 and update @len if we can do nocow write
6962 * 0 if we can't do nocow write
6963 * <0 if error happened
6965 * NOTE: This only checks the file extents, caller is responsible to wait for
6966 * any ordered extents.
6968 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
6969 u64
*orig_start
, u64
*orig_block_len
,
6972 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
6973 struct btrfs_path
*path
;
6975 struct extent_buffer
*leaf
;
6976 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6977 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6978 struct btrfs_file_extent_item
*fi
;
6979 struct btrfs_key key
;
6986 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
6988 path
= btrfs_alloc_path();
6992 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
6993 btrfs_ino(BTRFS_I(inode
)), offset
, 0);
6997 slot
= path
->slots
[0];
7000 /* can't find the item, must cow */
7007 leaf
= path
->nodes
[0];
7008 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
7009 if (key
.objectid
!= btrfs_ino(BTRFS_I(inode
)) ||
7010 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
7011 /* not our file or wrong item type, must cow */
7015 if (key
.offset
> offset
) {
7016 /* Wrong offset, must cow */
7020 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
7021 found_type
= btrfs_file_extent_type(leaf
, fi
);
7022 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
7023 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
7024 /* not a regular extent, must cow */
7028 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
7031 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
7032 if (extent_end
<= offset
)
7035 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
7036 if (disk_bytenr
== 0)
7039 if (btrfs_file_extent_compression(leaf
, fi
) ||
7040 btrfs_file_extent_encryption(leaf
, fi
) ||
7041 btrfs_file_extent_other_encoding(leaf
, fi
))
7045 * Do the same check as in btrfs_cross_ref_exist but without the
7046 * unnecessary search.
7048 if (btrfs_file_extent_generation(leaf
, fi
) <=
7049 btrfs_root_last_snapshot(&root
->root_item
))
7052 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
7055 *orig_start
= key
.offset
- backref_offset
;
7056 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
7057 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
7060 if (btrfs_extent_readonly(fs_info
, disk_bytenr
))
7063 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
7064 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
7067 range_end
= round_up(offset
+ num_bytes
,
7068 root
->fs_info
->sectorsize
) - 1;
7069 ret
= test_range_bit(io_tree
, offset
, range_end
,
7070 EXTENT_DELALLOC
, 0, NULL
);
7077 btrfs_release_path(path
);
7080 * look for other files referencing this extent, if we
7081 * find any we must cow
7084 ret
= btrfs_cross_ref_exist(root
, btrfs_ino(BTRFS_I(inode
)),
7085 key
.offset
- backref_offset
, disk_bytenr
);
7092 * adjust disk_bytenr and num_bytes to cover just the bytes
7093 * in this extent we are about to write. If there
7094 * are any csums in that range we have to cow in order
7095 * to keep the csums correct
7097 disk_bytenr
+= backref_offset
;
7098 disk_bytenr
+= offset
- key
.offset
;
7099 if (csum_exist_in_range(fs_info
, disk_bytenr
, num_bytes
))
7102 * all of the above have passed, it is safe to overwrite this extent
7108 btrfs_free_path(path
);
7112 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
7113 struct extent_state
**cached_state
, int writing
)
7115 struct btrfs_ordered_extent
*ordered
;
7119 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7122 * We're concerned with the entire range that we're going to be
7123 * doing DIO to, so we need to make sure there's no ordered
7124 * extents in this range.
7126 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), lockstart
,
7127 lockend
- lockstart
+ 1);
7130 * We need to make sure there are no buffered pages in this
7131 * range either, we could have raced between the invalidate in
7132 * generic_file_direct_write and locking the extent. The
7133 * invalidate needs to happen so that reads after a write do not
7137 (!writing
|| !filemap_range_has_page(inode
->i_mapping
,
7138 lockstart
, lockend
)))
7141 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7146 * If we are doing a DIO read and the ordered extent we
7147 * found is for a buffered write, we can not wait for it
7148 * to complete and retry, because if we do so we can
7149 * deadlock with concurrent buffered writes on page
7150 * locks. This happens only if our DIO read covers more
7151 * than one extent map, if at this point has already
7152 * created an ordered extent for a previous extent map
7153 * and locked its range in the inode's io tree, and a
7154 * concurrent write against that previous extent map's
7155 * range and this range started (we unlock the ranges
7156 * in the io tree only when the bios complete and
7157 * buffered writes always lock pages before attempting
7158 * to lock range in the io tree).
7161 test_bit(BTRFS_ORDERED_DIRECT
, &ordered
->flags
))
7162 btrfs_start_ordered_extent(inode
, ordered
, 1);
7165 btrfs_put_ordered_extent(ordered
);
7168 * We could trigger writeback for this range (and wait
7169 * for it to complete) and then invalidate the pages for
7170 * this range (through invalidate_inode_pages2_range()),
7171 * but that can lead us to a deadlock with a concurrent
7172 * call to readahead (a buffered read or a defrag call
7173 * triggered a readahead) on a page lock due to an
7174 * ordered dio extent we created before but did not have
7175 * yet a corresponding bio submitted (whence it can not
7176 * complete), which makes readahead wait for that
7177 * ordered extent to complete while holding a lock on
7192 /* The callers of this must take lock_extent() */
7193 static struct extent_map
*create_io_em(struct btrfs_inode
*inode
, u64 start
,
7194 u64 len
, u64 orig_start
, u64 block_start
,
7195 u64 block_len
, u64 orig_block_len
,
7196 u64 ram_bytes
, int compress_type
,
7199 struct extent_map_tree
*em_tree
;
7200 struct extent_map
*em
;
7203 ASSERT(type
== BTRFS_ORDERED_PREALLOC
||
7204 type
== BTRFS_ORDERED_COMPRESSED
||
7205 type
== BTRFS_ORDERED_NOCOW
||
7206 type
== BTRFS_ORDERED_REGULAR
);
7208 em_tree
= &inode
->extent_tree
;
7209 em
= alloc_extent_map();
7211 return ERR_PTR(-ENOMEM
);
7214 em
->orig_start
= orig_start
;
7216 em
->block_len
= block_len
;
7217 em
->block_start
= block_start
;
7218 em
->orig_block_len
= orig_block_len
;
7219 em
->ram_bytes
= ram_bytes
;
7220 em
->generation
= -1;
7221 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
7222 if (type
== BTRFS_ORDERED_PREALLOC
) {
7223 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
7224 } else if (type
== BTRFS_ORDERED_COMPRESSED
) {
7225 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
7226 em
->compress_type
= compress_type
;
7230 btrfs_drop_extent_cache(inode
, em
->start
,
7231 em
->start
+ em
->len
- 1, 0);
7232 write_lock(&em_tree
->lock
);
7233 ret
= add_extent_mapping(em_tree
, em
, 1);
7234 write_unlock(&em_tree
->lock
);
7236 * The caller has taken lock_extent(), who could race with us
7239 } while (ret
== -EEXIST
);
7242 free_extent_map(em
);
7243 return ERR_PTR(ret
);
7246 /* em got 2 refs now, callers needs to do free_extent_map once. */
7251 static int btrfs_get_blocks_direct_read(struct extent_map
*em
,
7252 struct buffer_head
*bh_result
,
7253 struct inode
*inode
,
7256 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7258 if (em
->block_start
== EXTENT_MAP_HOLE
||
7259 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7262 len
= min(len
, em
->len
- (start
- em
->start
));
7264 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
7266 bh_result
->b_size
= len
;
7267 bh_result
->b_bdev
= fs_info
->fs_devices
->latest_bdev
;
7268 set_buffer_mapped(bh_result
);
7273 static int btrfs_get_blocks_direct_write(struct extent_map
**map
,
7274 struct buffer_head
*bh_result
,
7275 struct inode
*inode
,
7276 struct btrfs_dio_data
*dio_data
,
7279 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7280 struct extent_map
*em
= *map
;
7284 * We don't allocate a new extent in the following cases
7286 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7288 * 2) The extent is marked as PREALLOC. We're good to go here and can
7289 * just use the extent.
7292 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
7293 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
7294 em
->block_start
!= EXTENT_MAP_HOLE
)) {
7296 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
7298 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7299 type
= BTRFS_ORDERED_PREALLOC
;
7301 type
= BTRFS_ORDERED_NOCOW
;
7302 len
= min(len
, em
->len
- (start
- em
->start
));
7303 block_start
= em
->block_start
+ (start
- em
->start
);
7305 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
7306 &orig_block_len
, &ram_bytes
) == 1 &&
7307 btrfs_inc_nocow_writers(fs_info
, block_start
)) {
7308 struct extent_map
*em2
;
7310 em2
= btrfs_create_dio_extent(BTRFS_I(inode
), start
, len
,
7311 orig_start
, block_start
,
7312 len
, orig_block_len
,
7314 btrfs_dec_nocow_writers(fs_info
, block_start
);
7315 if (type
== BTRFS_ORDERED_PREALLOC
) {
7316 free_extent_map(em
);
7320 if (em2
&& IS_ERR(em2
)) {
7325 * For inode marked NODATACOW or extent marked PREALLOC,
7326 * use the existing or preallocated extent, so does not
7327 * need to adjust btrfs_space_info's bytes_may_use.
7329 btrfs_free_reserved_data_space_noquota(fs_info
, len
);
7334 /* this will cow the extent */
7335 len
= bh_result
->b_size
;
7336 free_extent_map(em
);
7337 *map
= em
= btrfs_new_extent_direct(BTRFS_I(inode
), start
, len
);
7343 len
= min(len
, em
->len
- (start
- em
->start
));
7346 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
7348 bh_result
->b_size
= len
;
7349 bh_result
->b_bdev
= fs_info
->fs_devices
->latest_bdev
;
7350 set_buffer_mapped(bh_result
);
7352 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7353 set_buffer_new(bh_result
);
7356 * Need to update the i_size under the extent lock so buffered
7357 * readers will get the updated i_size when we unlock.
7359 if (!dio_data
->overwrite
&& start
+ len
> i_size_read(inode
))
7360 i_size_write(inode
, start
+ len
);
7362 WARN_ON(dio_data
->reserve
< len
);
7363 dio_data
->reserve
-= len
;
7364 dio_data
->unsubmitted_oe_range_end
= start
+ len
;
7365 current
->journal_info
= dio_data
;
7370 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
7371 struct buffer_head
*bh_result
, int create
)
7373 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7374 struct extent_map
*em
;
7375 struct extent_state
*cached_state
= NULL
;
7376 struct btrfs_dio_data
*dio_data
= NULL
;
7377 u64 start
= iblock
<< inode
->i_blkbits
;
7378 u64 lockstart
, lockend
;
7379 u64 len
= bh_result
->b_size
;
7383 len
= min_t(u64
, len
, fs_info
->sectorsize
);
7386 lockend
= start
+ len
- 1;
7388 if (current
->journal_info
) {
7390 * Need to pull our outstanding extents and set journal_info to NULL so
7391 * that anything that needs to check if there's a transaction doesn't get
7394 dio_data
= current
->journal_info
;
7395 current
->journal_info
= NULL
;
7399 * If this errors out it's because we couldn't invalidate pagecache for
7400 * this range and we need to fallback to buffered.
7402 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
,
7408 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, start
, len
);
7415 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7416 * io. INLINE is special, and we could probably kludge it in here, but
7417 * it's still buffered so for safety lets just fall back to the generic
7420 * For COMPRESSED we _have_ to read the entire extent in so we can
7421 * decompress it, so there will be buffering required no matter what we
7422 * do, so go ahead and fallback to buffered.
7424 * We return -ENOTBLK because that's what makes DIO go ahead and go back
7425 * to buffered IO. Don't blame me, this is the price we pay for using
7428 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
7429 em
->block_start
== EXTENT_MAP_INLINE
) {
7430 free_extent_map(em
);
7436 ret
= btrfs_get_blocks_direct_write(&em
, bh_result
, inode
,
7437 dio_data
, start
, len
);
7441 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
,
7442 lockend
, &cached_state
);
7444 ret
= btrfs_get_blocks_direct_read(em
, bh_result
, inode
,
7446 /* Can be negative only if we read from a hole */
7449 free_extent_map(em
);
7453 * We need to unlock only the end area that we aren't using.
7454 * The rest is going to be unlocked by the endio routine.
7456 lockstart
= start
+ bh_result
->b_size
;
7457 if (lockstart
< lockend
) {
7458 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
,
7459 lockstart
, lockend
, &cached_state
);
7461 free_extent_state(cached_state
);
7465 free_extent_map(em
);
7470 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7474 current
->journal_info
= dio_data
;
7478 static void btrfs_dio_private_put(struct btrfs_dio_private
*dip
)
7481 * This implies a barrier so that stores to dio_bio->bi_status before
7482 * this and loads of dio_bio->bi_status after this are fully ordered.
7484 if (!refcount_dec_and_test(&dip
->refs
))
7487 if (bio_op(dip
->dio_bio
) == REQ_OP_WRITE
) {
7488 __endio_write_update_ordered(BTRFS_I(dip
->inode
),
7489 dip
->logical_offset
,
7491 !dip
->dio_bio
->bi_status
);
7493 unlock_extent(&BTRFS_I(dip
->inode
)->io_tree
,
7494 dip
->logical_offset
,
7495 dip
->logical_offset
+ dip
->bytes
- 1);
7498 dio_end_io(dip
->dio_bio
);
7502 static blk_status_t
submit_dio_repair_bio(struct inode
*inode
, struct bio
*bio
,
7504 unsigned long bio_flags
)
7506 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7507 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7510 BUG_ON(bio_op(bio
) == REQ_OP_WRITE
);
7512 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, BTRFS_WQ_ENDIO_DATA
);
7516 refcount_inc(&dip
->refs
);
7517 ret
= btrfs_map_bio(fs_info
, bio
, mirror_num
);
7519 refcount_dec(&dip
->refs
);
7523 static blk_status_t
btrfs_check_read_dio_bio(struct inode
*inode
,
7524 struct btrfs_io_bio
*io_bio
,
7525 const bool uptodate
)
7527 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
7528 const u32 sectorsize
= fs_info
->sectorsize
;
7529 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
7530 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7531 const bool csum
= !(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
);
7532 struct bio_vec bvec
;
7533 struct bvec_iter iter
;
7534 u64 start
= io_bio
->logical
;
7536 blk_status_t err
= BLK_STS_OK
;
7538 __bio_for_each_segment(bvec
, &io_bio
->bio
, iter
, io_bio
->iter
) {
7539 unsigned int i
, nr_sectors
, pgoff
;
7541 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
.bv_len
);
7542 pgoff
= bvec
.bv_offset
;
7543 for (i
= 0; i
< nr_sectors
; i
++) {
7544 ASSERT(pgoff
< PAGE_SIZE
);
7546 (!csum
|| !check_data_csum(inode
, io_bio
, icsum
,
7547 bvec
.bv_page
, pgoff
,
7548 start
, sectorsize
))) {
7549 clean_io_failure(fs_info
, failure_tree
, io_tree
,
7550 start
, bvec
.bv_page
,
7551 btrfs_ino(BTRFS_I(inode
)),
7554 blk_status_t status
;
7556 status
= btrfs_submit_read_repair(inode
,
7558 start
- io_bio
->logical
,
7559 bvec
.bv_page
, pgoff
,
7561 start
+ sectorsize
- 1,
7563 submit_dio_repair_bio
);
7567 start
+= sectorsize
;
7569 pgoff
+= sectorsize
;
7575 static void __endio_write_update_ordered(struct btrfs_inode
*inode
,
7576 const u64 offset
, const u64 bytes
,
7577 const bool uptodate
)
7579 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
7580 struct btrfs_ordered_extent
*ordered
= NULL
;
7581 struct btrfs_workqueue
*wq
;
7582 u64 ordered_offset
= offset
;
7583 u64 ordered_bytes
= bytes
;
7586 if (btrfs_is_free_space_inode(inode
))
7587 wq
= fs_info
->endio_freespace_worker
;
7589 wq
= fs_info
->endio_write_workers
;
7591 while (ordered_offset
< offset
+ bytes
) {
7592 last_offset
= ordered_offset
;
7593 if (btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
7597 btrfs_init_work(&ordered
->work
, finish_ordered_fn
, NULL
,
7599 btrfs_queue_work(wq
, &ordered
->work
);
7602 * If btrfs_dec_test_ordered_pending does not find any ordered
7603 * extent in the range, we can exit.
7605 if (ordered_offset
== last_offset
)
7608 * Our bio might span multiple ordered extents. In this case
7609 * we keep going until we have accounted the whole dio.
7611 if (ordered_offset
< offset
+ bytes
) {
7612 ordered_bytes
= offset
+ bytes
- ordered_offset
;
7618 static blk_status_t
btrfs_submit_bio_start_direct_io(void *private_data
,
7619 struct bio
*bio
, u64 offset
)
7621 struct inode
*inode
= private_data
;
7623 ret
= btrfs_csum_one_bio(BTRFS_I(inode
), bio
, offset
, 1);
7624 BUG_ON(ret
); /* -ENOMEM */
7628 static void btrfs_end_dio_bio(struct bio
*bio
)
7630 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7631 blk_status_t err
= bio
->bi_status
;
7634 btrfs_warn(BTRFS_I(dip
->inode
)->root
->fs_info
,
7635 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
7636 btrfs_ino(BTRFS_I(dip
->inode
)), bio_op(bio
),
7638 (unsigned long long)bio
->bi_iter
.bi_sector
,
7639 bio
->bi_iter
.bi_size
, err
);
7641 if (bio_op(bio
) == REQ_OP_READ
) {
7642 err
= btrfs_check_read_dio_bio(dip
->inode
, btrfs_io_bio(bio
),
7647 dip
->dio_bio
->bi_status
= err
;
7650 btrfs_dio_private_put(dip
);
7653 static inline blk_status_t
btrfs_submit_dio_bio(struct bio
*bio
,
7654 struct inode
*inode
, u64 file_offset
, int async_submit
)
7656 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7657 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7658 bool write
= bio_op(bio
) == REQ_OP_WRITE
;
7661 /* Check btrfs_submit_bio_hook() for rules about async submit. */
7663 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
7666 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, BTRFS_WQ_ENDIO_DATA
);
7671 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
7674 if (write
&& async_submit
) {
7675 ret
= btrfs_wq_submit_bio(fs_info
, bio
, 0, 0,
7677 btrfs_submit_bio_start_direct_io
);
7681 * If we aren't doing async submit, calculate the csum of the
7684 ret
= btrfs_csum_one_bio(BTRFS_I(inode
), bio
, file_offset
, 1);
7690 csum_offset
= file_offset
- dip
->logical_offset
;
7691 csum_offset
>>= inode
->i_sb
->s_blocksize_bits
;
7692 csum_offset
*= btrfs_super_csum_size(fs_info
->super_copy
);
7693 btrfs_io_bio(bio
)->csum
= dip
->csums
+ csum_offset
;
7696 ret
= btrfs_map_bio(fs_info
, bio
, 0);
7702 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7703 * or ordered extents whether or not we submit any bios.
7705 static struct btrfs_dio_private
*btrfs_create_dio_private(struct bio
*dio_bio
,
7706 struct inode
*inode
,
7709 const bool write
= (bio_op(dio_bio
) == REQ_OP_WRITE
);
7710 const bool csum
= !(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
);
7712 struct btrfs_dio_private
*dip
;
7714 dip_size
= sizeof(*dip
);
7715 if (!write
&& csum
) {
7716 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7717 const u16 csum_size
= btrfs_super_csum_size(fs_info
->super_copy
);
7720 nblocks
= dio_bio
->bi_iter
.bi_size
>> inode
->i_sb
->s_blocksize_bits
;
7721 dip_size
+= csum_size
* nblocks
;
7724 dip
= kzalloc(dip_size
, GFP_NOFS
);
7729 dip
->logical_offset
= file_offset
;
7730 dip
->bytes
= dio_bio
->bi_iter
.bi_size
;
7731 dip
->disk_bytenr
= (u64
)dio_bio
->bi_iter
.bi_sector
<< 9;
7732 dip
->dio_bio
= dio_bio
;
7733 refcount_set(&dip
->refs
, 1);
7736 struct btrfs_dio_data
*dio_data
= current
->journal_info
;
7739 * Setting range start and end to the same value means that
7740 * no cleanup will happen in btrfs_direct_IO
7742 dio_data
->unsubmitted_oe_range_end
= dip
->logical_offset
+
7744 dio_data
->unsubmitted_oe_range_start
=
7745 dio_data
->unsubmitted_oe_range_end
;
7750 static void btrfs_submit_direct(struct bio
*dio_bio
, struct inode
*inode
,
7753 const bool write
= (bio_op(dio_bio
) == REQ_OP_WRITE
);
7754 const bool csum
= !(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
);
7755 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7756 const bool raid56
= (btrfs_data_alloc_profile(fs_info
) &
7757 BTRFS_BLOCK_GROUP_RAID56_MASK
);
7758 struct btrfs_dio_private
*dip
;
7761 int async_submit
= 0;
7763 int clone_offset
= 0;
7766 blk_status_t status
;
7767 struct btrfs_io_geometry geom
;
7769 dip
= btrfs_create_dio_private(dio_bio
, inode
, file_offset
);
7772 unlock_extent(&BTRFS_I(inode
)->io_tree
, file_offset
,
7773 file_offset
+ dio_bio
->bi_iter
.bi_size
- 1);
7775 dio_bio
->bi_status
= BLK_STS_RESOURCE
;
7776 dio_end_io(dio_bio
);
7780 if (!write
&& csum
) {
7782 * Load the csums up front to reduce csum tree searches and
7783 * contention when submitting bios.
7785 status
= btrfs_lookup_bio_sums(inode
, dio_bio
, file_offset
,
7787 if (status
!= BLK_STS_OK
)
7791 start_sector
= dio_bio
->bi_iter
.bi_sector
;
7792 submit_len
= dio_bio
->bi_iter
.bi_size
;
7795 ret
= btrfs_get_io_geometry(fs_info
, btrfs_op(dio_bio
),
7796 start_sector
<< 9, submit_len
,
7799 status
= errno_to_blk_status(ret
);
7802 ASSERT(geom
.len
<= INT_MAX
);
7804 clone_len
= min_t(int, submit_len
, geom
.len
);
7807 * This will never fail as it's passing GPF_NOFS and
7808 * the allocation is backed by btrfs_bioset.
7810 bio
= btrfs_bio_clone_partial(dio_bio
, clone_offset
, clone_len
);
7811 bio
->bi_private
= dip
;
7812 bio
->bi_end_io
= btrfs_end_dio_bio
;
7813 btrfs_io_bio(bio
)->logical
= file_offset
;
7815 ASSERT(submit_len
>= clone_len
);
7816 submit_len
-= clone_len
;
7819 * Increase the count before we submit the bio so we know
7820 * the end IO handler won't happen before we increase the
7821 * count. Otherwise, the dip might get freed before we're
7822 * done setting it up.
7824 * We transfer the initial reference to the last bio, so we
7825 * don't need to increment the reference count for the last one.
7827 if (submit_len
> 0) {
7828 refcount_inc(&dip
->refs
);
7830 * If we are submitting more than one bio, submit them
7831 * all asynchronously. The exception is RAID 5 or 6, as
7832 * asynchronous checksums make it difficult to collect
7833 * full stripe writes.
7839 status
= btrfs_submit_dio_bio(bio
, inode
, file_offset
,
7844 refcount_dec(&dip
->refs
);
7848 clone_offset
+= clone_len
;
7849 start_sector
+= clone_len
>> 9;
7850 file_offset
+= clone_len
;
7851 } while (submit_len
> 0);
7855 dip
->dio_bio
->bi_status
= status
;
7856 btrfs_dio_private_put(dip
);
7859 static ssize_t
check_direct_IO(struct btrfs_fs_info
*fs_info
,
7860 const struct iov_iter
*iter
, loff_t offset
)
7864 unsigned int blocksize_mask
= fs_info
->sectorsize
- 1;
7865 ssize_t retval
= -EINVAL
;
7867 if (offset
& blocksize_mask
)
7870 if (iov_iter_alignment(iter
) & blocksize_mask
)
7873 /* If this is a write we don't need to check anymore */
7874 if (iov_iter_rw(iter
) != READ
|| !iter_is_iovec(iter
))
7877 * Check to make sure we don't have duplicate iov_base's in this
7878 * iovec, if so return EINVAL, otherwise we'll get csum errors
7879 * when reading back.
7881 for (seg
= 0; seg
< iter
->nr_segs
; seg
++) {
7882 for (i
= seg
+ 1; i
< iter
->nr_segs
; i
++) {
7883 if (iter
->iov
[seg
].iov_base
== iter
->iov
[i
].iov_base
)
7892 static ssize_t
btrfs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
7894 struct file
*file
= iocb
->ki_filp
;
7895 struct inode
*inode
= file
->f_mapping
->host
;
7896 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7897 struct btrfs_dio_data dio_data
= { 0 };
7898 struct extent_changeset
*data_reserved
= NULL
;
7899 loff_t offset
= iocb
->ki_pos
;
7903 bool relock
= false;
7906 if (check_direct_IO(fs_info
, iter
, offset
))
7909 inode_dio_begin(inode
);
7912 * The generic stuff only does filemap_write_and_wait_range, which
7913 * isn't enough if we've written compressed pages to this area, so
7914 * we need to flush the dirty pages again to make absolutely sure
7915 * that any outstanding dirty pages are on disk.
7917 count
= iov_iter_count(iter
);
7918 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
7919 &BTRFS_I(inode
)->runtime_flags
))
7920 filemap_fdatawrite_range(inode
->i_mapping
, offset
,
7921 offset
+ count
- 1);
7923 if (iov_iter_rw(iter
) == WRITE
) {
7925 * If the write DIO is beyond the EOF, we need update
7926 * the isize, but it is protected by i_mutex. So we can
7927 * not unlock the i_mutex at this case.
7929 if (offset
+ count
<= inode
->i_size
) {
7930 dio_data
.overwrite
= 1;
7931 inode_unlock(inode
);
7934 ret
= btrfs_delalloc_reserve_space(BTRFS_I(inode
), &data_reserved
,
7940 * We need to know how many extents we reserved so that we can
7941 * do the accounting properly if we go over the number we
7942 * originally calculated. Abuse current->journal_info for this.
7944 dio_data
.reserve
= round_up(count
,
7945 fs_info
->sectorsize
);
7946 dio_data
.unsubmitted_oe_range_start
= (u64
)offset
;
7947 dio_data
.unsubmitted_oe_range_end
= (u64
)offset
;
7948 current
->journal_info
= &dio_data
;
7949 down_read(&BTRFS_I(inode
)->dio_sem
);
7950 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7951 &BTRFS_I(inode
)->runtime_flags
)) {
7952 inode_dio_end(inode
);
7953 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7957 ret
= __blockdev_direct_IO(iocb
, inode
,
7958 fs_info
->fs_devices
->latest_bdev
,
7959 iter
, btrfs_get_blocks_direct
, NULL
,
7960 btrfs_submit_direct
, flags
);
7961 if (iov_iter_rw(iter
) == WRITE
) {
7962 up_read(&BTRFS_I(inode
)->dio_sem
);
7963 current
->journal_info
= NULL
;
7964 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
7965 if (dio_data
.reserve
)
7966 btrfs_delalloc_release_space(BTRFS_I(inode
),
7967 data_reserved
, offset
, dio_data
.reserve
,
7970 * On error we might have left some ordered extents
7971 * without submitting corresponding bios for them, so
7972 * cleanup them up to avoid other tasks getting them
7973 * and waiting for them to complete forever.
7975 if (dio_data
.unsubmitted_oe_range_start
<
7976 dio_data
.unsubmitted_oe_range_end
)
7977 __endio_write_update_ordered(BTRFS_I(inode
),
7978 dio_data
.unsubmitted_oe_range_start
,
7979 dio_data
.unsubmitted_oe_range_end
-
7980 dio_data
.unsubmitted_oe_range_start
,
7982 } else if (ret
>= 0 && (size_t)ret
< count
)
7983 btrfs_delalloc_release_space(BTRFS_I(inode
), data_reserved
,
7984 offset
, count
- (size_t)ret
, true);
7985 btrfs_delalloc_release_extents(BTRFS_I(inode
), count
);
7989 inode_dio_end(inode
);
7993 extent_changeset_free(data_reserved
);
7997 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
8002 ret
= fiemap_prep(inode
, fieinfo
, start
, &len
, 0);
8006 return extent_fiemap(inode
, fieinfo
, start
, len
);
8009 int btrfs_readpage(struct file
*file
, struct page
*page
)
8011 return extent_read_full_page(page
, btrfs_get_extent
, 0);
8014 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
8016 struct inode
*inode
= page
->mapping
->host
;
8019 if (current
->flags
& PF_MEMALLOC
) {
8020 redirty_page_for_writepage(wbc
, page
);
8026 * If we are under memory pressure we will call this directly from the
8027 * VM, we need to make sure we have the inode referenced for the ordered
8028 * extent. If not just return like we didn't do anything.
8030 if (!igrab(inode
)) {
8031 redirty_page_for_writepage(wbc
, page
);
8032 return AOP_WRITEPAGE_ACTIVATE
;
8034 ret
= extent_write_full_page(page
, wbc
);
8035 btrfs_add_delayed_iput(inode
);
8039 static int btrfs_writepages(struct address_space
*mapping
,
8040 struct writeback_control
*wbc
)
8042 return extent_writepages(mapping
, wbc
);
8045 static void btrfs_readahead(struct readahead_control
*rac
)
8047 extent_readahead(rac
);
8050 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8052 int ret
= try_release_extent_mapping(page
, gfp_flags
);
8054 detach_page_private(page
);
8058 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8060 if (PageWriteback(page
) || PageDirty(page
))
8062 return __btrfs_releasepage(page
, gfp_flags
);
8065 #ifdef CONFIG_MIGRATION
8066 static int btrfs_migratepage(struct address_space
*mapping
,
8067 struct page
*newpage
, struct page
*page
,
8068 enum migrate_mode mode
)
8072 ret
= migrate_page_move_mapping(mapping
, newpage
, page
, 0);
8073 if (ret
!= MIGRATEPAGE_SUCCESS
)
8076 if (page_has_private(page
))
8077 attach_page_private(newpage
, detach_page_private(page
));
8079 if (PagePrivate2(page
)) {
8080 ClearPagePrivate2(page
);
8081 SetPagePrivate2(newpage
);
8084 if (mode
!= MIGRATE_SYNC_NO_COPY
)
8085 migrate_page_copy(newpage
, page
);
8087 migrate_page_states(newpage
, page
);
8088 return MIGRATEPAGE_SUCCESS
;
8092 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
8093 unsigned int length
)
8095 struct inode
*inode
= page
->mapping
->host
;
8096 struct extent_io_tree
*tree
;
8097 struct btrfs_ordered_extent
*ordered
;
8098 struct extent_state
*cached_state
= NULL
;
8099 u64 page_start
= page_offset(page
);
8100 u64 page_end
= page_start
+ PAGE_SIZE
- 1;
8103 int inode_evicting
= inode
->i_state
& I_FREEING
;
8106 * we have the page locked, so new writeback can't start,
8107 * and the dirty bit won't be cleared while we are here.
8109 * Wait for IO on this page so that we can safely clear
8110 * the PagePrivate2 bit and do ordered accounting
8112 wait_on_page_writeback(page
);
8114 tree
= &BTRFS_I(inode
)->io_tree
;
8116 btrfs_releasepage(page
, GFP_NOFS
);
8120 if (!inode_evicting
)
8121 lock_extent_bits(tree
, page_start
, page_end
, &cached_state
);
8124 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), start
,
8125 page_end
- start
+ 1);
8128 ordered
->file_offset
+ ordered
->num_bytes
- 1);
8130 * IO on this page will never be started, so we need
8131 * to account for any ordered extents now
8133 if (!inode_evicting
)
8134 clear_extent_bit(tree
, start
, end
,
8135 EXTENT_DELALLOC
| EXTENT_DELALLOC_NEW
|
8136 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
8137 EXTENT_DEFRAG
, 1, 0, &cached_state
);
8139 * whoever cleared the private bit is responsible
8140 * for the finish_ordered_io
8142 if (TestClearPagePrivate2(page
)) {
8143 struct btrfs_ordered_inode_tree
*tree
;
8146 tree
= &BTRFS_I(inode
)->ordered_tree
;
8148 spin_lock_irq(&tree
->lock
);
8149 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
8150 new_len
= start
- ordered
->file_offset
;
8151 if (new_len
< ordered
->truncated_len
)
8152 ordered
->truncated_len
= new_len
;
8153 spin_unlock_irq(&tree
->lock
);
8155 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
8157 end
- start
+ 1, 1))
8158 btrfs_finish_ordered_io(ordered
);
8160 btrfs_put_ordered_extent(ordered
);
8161 if (!inode_evicting
) {
8162 cached_state
= NULL
;
8163 lock_extent_bits(tree
, start
, end
,
8168 if (start
< page_end
)
8173 * Qgroup reserved space handler
8174 * Page here will be either
8175 * 1) Already written to disk or ordered extent already submitted
8176 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8177 * Qgroup will be handled by its qgroup_record then.
8178 * btrfs_qgroup_free_data() call will do nothing here.
8180 * 2) Not written to disk yet
8181 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8182 * bit of its io_tree, and free the qgroup reserved data space.
8183 * Since the IO will never happen for this page.
8185 btrfs_qgroup_free_data(BTRFS_I(inode
), NULL
, page_start
, PAGE_SIZE
);
8186 if (!inode_evicting
) {
8187 clear_extent_bit(tree
, page_start
, page_end
, EXTENT_LOCKED
|
8188 EXTENT_DELALLOC
| EXTENT_DELALLOC_NEW
|
8189 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
8192 __btrfs_releasepage(page
, GFP_NOFS
);
8195 ClearPageChecked(page
);
8196 detach_page_private(page
);
8200 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8201 * called from a page fault handler when a page is first dirtied. Hence we must
8202 * be careful to check for EOF conditions here. We set the page up correctly
8203 * for a written page which means we get ENOSPC checking when writing into
8204 * holes and correct delalloc and unwritten extent mapping on filesystems that
8205 * support these features.
8207 * We are not allowed to take the i_mutex here so we have to play games to
8208 * protect against truncate races as the page could now be beyond EOF. Because
8209 * truncate_setsize() writes the inode size before removing pages, once we have
8210 * the page lock we can determine safely if the page is beyond EOF. If it is not
8211 * beyond EOF, then the page is guaranteed safe against truncation until we
8214 vm_fault_t
btrfs_page_mkwrite(struct vm_fault
*vmf
)
8216 struct page
*page
= vmf
->page
;
8217 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
8218 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8219 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
8220 struct btrfs_ordered_extent
*ordered
;
8221 struct extent_state
*cached_state
= NULL
;
8222 struct extent_changeset
*data_reserved
= NULL
;
8224 unsigned long zero_start
;
8234 reserved_space
= PAGE_SIZE
;
8236 sb_start_pagefault(inode
->i_sb
);
8237 page_start
= page_offset(page
);
8238 page_end
= page_start
+ PAGE_SIZE
- 1;
8242 * Reserving delalloc space after obtaining the page lock can lead to
8243 * deadlock. For example, if a dirty page is locked by this function
8244 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8245 * dirty page write out, then the btrfs_writepage() function could
8246 * end up waiting indefinitely to get a lock on the page currently
8247 * being processed by btrfs_page_mkwrite() function.
8249 ret2
= btrfs_delalloc_reserve_space(BTRFS_I(inode
), &data_reserved
,
8250 page_start
, reserved_space
);
8252 ret2
= file_update_time(vmf
->vma
->vm_file
);
8256 ret
= vmf_error(ret2
);
8262 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
8265 size
= i_size_read(inode
);
8267 if ((page
->mapping
!= inode
->i_mapping
) ||
8268 (page_start
>= size
)) {
8269 /* page got truncated out from underneath us */
8272 wait_on_page_writeback(page
);
8274 lock_extent_bits(io_tree
, page_start
, page_end
, &cached_state
);
8275 set_page_extent_mapped(page
);
8278 * we can't set the delalloc bits if there are pending ordered
8279 * extents. Drop our locks and wait for them to finish
8281 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), page_start
,
8284 unlock_extent_cached(io_tree
, page_start
, page_end
,
8287 btrfs_start_ordered_extent(inode
, ordered
, 1);
8288 btrfs_put_ordered_extent(ordered
);
8292 if (page
->index
== ((size
- 1) >> PAGE_SHIFT
)) {
8293 reserved_space
= round_up(size
- page_start
,
8294 fs_info
->sectorsize
);
8295 if (reserved_space
< PAGE_SIZE
) {
8296 end
= page_start
+ reserved_space
- 1;
8297 btrfs_delalloc_release_space(BTRFS_I(inode
),
8298 data_reserved
, page_start
,
8299 PAGE_SIZE
- reserved_space
, true);
8304 * page_mkwrite gets called when the page is firstly dirtied after it's
8305 * faulted in, but write(2) could also dirty a page and set delalloc
8306 * bits, thus in this case for space account reason, we still need to
8307 * clear any delalloc bits within this page range since we have to
8308 * reserve data&meta space before lock_page() (see above comments).
8310 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, end
,
8311 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
8312 EXTENT_DEFRAG
, 0, 0, &cached_state
);
8314 ret2
= btrfs_set_extent_delalloc(BTRFS_I(inode
), page_start
, end
, 0,
8317 unlock_extent_cached(io_tree
, page_start
, page_end
,
8319 ret
= VM_FAULT_SIGBUS
;
8323 /* page is wholly or partially inside EOF */
8324 if (page_start
+ PAGE_SIZE
> size
)
8325 zero_start
= offset_in_page(size
);
8327 zero_start
= PAGE_SIZE
;
8329 if (zero_start
!= PAGE_SIZE
) {
8331 memset(kaddr
+ zero_start
, 0, PAGE_SIZE
- zero_start
);
8332 flush_dcache_page(page
);
8335 ClearPageChecked(page
);
8336 set_page_dirty(page
);
8337 SetPageUptodate(page
);
8339 BTRFS_I(inode
)->last_trans
= fs_info
->generation
;
8340 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
8341 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
8343 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
);
8345 btrfs_delalloc_release_extents(BTRFS_I(inode
), PAGE_SIZE
);
8346 sb_end_pagefault(inode
->i_sb
);
8347 extent_changeset_free(data_reserved
);
8348 return VM_FAULT_LOCKED
;
8353 btrfs_delalloc_release_extents(BTRFS_I(inode
), PAGE_SIZE
);
8354 btrfs_delalloc_release_space(BTRFS_I(inode
), data_reserved
, page_start
,
8355 reserved_space
, (ret
!= 0));
8357 sb_end_pagefault(inode
->i_sb
);
8358 extent_changeset_free(data_reserved
);
8362 static int btrfs_truncate(struct inode
*inode
, bool skip_writeback
)
8364 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8365 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8366 struct btrfs_block_rsv
*rsv
;
8368 struct btrfs_trans_handle
*trans
;
8369 u64 mask
= fs_info
->sectorsize
- 1;
8370 u64 min_size
= btrfs_calc_metadata_size(fs_info
, 1);
8372 if (!skip_writeback
) {
8373 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
8380 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8381 * things going on here:
8383 * 1) We need to reserve space to update our inode.
8385 * 2) We need to have something to cache all the space that is going to
8386 * be free'd up by the truncate operation, but also have some slack
8387 * space reserved in case it uses space during the truncate (thank you
8388 * very much snapshotting).
8390 * And we need these to be separate. The fact is we can use a lot of
8391 * space doing the truncate, and we have no earthly idea how much space
8392 * we will use, so we need the truncate reservation to be separate so it
8393 * doesn't end up using space reserved for updating the inode. We also
8394 * need to be able to stop the transaction and start a new one, which
8395 * means we need to be able to update the inode several times, and we
8396 * have no idea of knowing how many times that will be, so we can't just
8397 * reserve 1 item for the entirety of the operation, so that has to be
8398 * done separately as well.
8400 * So that leaves us with
8402 * 1) rsv - for the truncate reservation, which we will steal from the
8403 * transaction reservation.
8404 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
8405 * updating the inode.
8407 rsv
= btrfs_alloc_block_rsv(fs_info
, BTRFS_BLOCK_RSV_TEMP
);
8410 rsv
->size
= min_size
;
8414 * 1 for the truncate slack space
8415 * 1 for updating the inode.
8417 trans
= btrfs_start_transaction(root
, 2);
8418 if (IS_ERR(trans
)) {
8419 ret
= PTR_ERR(trans
);
8423 /* Migrate the slack space for the truncate to our reserve */
8424 ret
= btrfs_block_rsv_migrate(&fs_info
->trans_block_rsv
, rsv
,
8429 * So if we truncate and then write and fsync we normally would just
8430 * write the extents that changed, which is a problem if we need to
8431 * first truncate that entire inode. So set this flag so we write out
8432 * all of the extents in the inode to the sync log so we're completely
8435 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
8436 trans
->block_rsv
= rsv
;
8439 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
8441 BTRFS_EXTENT_DATA_KEY
);
8442 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
8443 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
)
8446 ret
= btrfs_update_inode(trans
, root
, inode
);
8450 btrfs_end_transaction(trans
);
8451 btrfs_btree_balance_dirty(fs_info
);
8453 trans
= btrfs_start_transaction(root
, 2);
8454 if (IS_ERR(trans
)) {
8455 ret
= PTR_ERR(trans
);
8460 btrfs_block_rsv_release(fs_info
, rsv
, -1, NULL
);
8461 ret
= btrfs_block_rsv_migrate(&fs_info
->trans_block_rsv
,
8462 rsv
, min_size
, false);
8463 BUG_ON(ret
); /* shouldn't happen */
8464 trans
->block_rsv
= rsv
;
8468 * We can't call btrfs_truncate_block inside a trans handle as we could
8469 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8470 * we've truncated everything except the last little bit, and can do
8471 * btrfs_truncate_block and then update the disk_i_size.
8473 if (ret
== NEED_TRUNCATE_BLOCK
) {
8474 btrfs_end_transaction(trans
);
8475 btrfs_btree_balance_dirty(fs_info
);
8477 ret
= btrfs_truncate_block(inode
, inode
->i_size
, 0, 0);
8480 trans
= btrfs_start_transaction(root
, 1);
8481 if (IS_ERR(trans
)) {
8482 ret
= PTR_ERR(trans
);
8485 btrfs_inode_safe_disk_i_size_write(inode
, 0);
8491 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
8492 ret2
= btrfs_update_inode(trans
, root
, inode
);
8496 ret2
= btrfs_end_transaction(trans
);
8499 btrfs_btree_balance_dirty(fs_info
);
8502 btrfs_free_block_rsv(fs_info
, rsv
);
8508 * create a new subvolume directory/inode (helper for the ioctl).
8510 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
8511 struct btrfs_root
*new_root
,
8512 struct btrfs_root
*parent_root
,
8515 struct inode
*inode
;
8519 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
8520 new_dirid
, new_dirid
,
8521 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
8524 return PTR_ERR(inode
);
8525 inode
->i_op
= &btrfs_dir_inode_operations
;
8526 inode
->i_fop
= &btrfs_dir_file_operations
;
8528 set_nlink(inode
, 1);
8529 btrfs_i_size_write(BTRFS_I(inode
), 0);
8530 unlock_new_inode(inode
);
8532 err
= btrfs_subvol_inherit_props(trans
, new_root
, parent_root
);
8534 btrfs_err(new_root
->fs_info
,
8535 "error inheriting subvolume %llu properties: %d",
8536 new_root
->root_key
.objectid
, err
);
8538 err
= btrfs_update_inode(trans
, new_root
, inode
);
8544 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
8546 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
8547 struct btrfs_inode
*ei
;
8548 struct inode
*inode
;
8550 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_KERNEL
);
8557 ei
->last_sub_trans
= 0;
8558 ei
->logged_trans
= 0;
8559 ei
->delalloc_bytes
= 0;
8560 ei
->new_delalloc_bytes
= 0;
8561 ei
->defrag_bytes
= 0;
8562 ei
->disk_i_size
= 0;
8565 ei
->index_cnt
= (u64
)-1;
8567 ei
->last_unlink_trans
= 0;
8568 ei
->last_reflink_trans
= 0;
8569 ei
->last_log_commit
= 0;
8571 spin_lock_init(&ei
->lock
);
8572 ei
->outstanding_extents
= 0;
8573 if (sb
->s_magic
!= BTRFS_TEST_MAGIC
)
8574 btrfs_init_metadata_block_rsv(fs_info
, &ei
->block_rsv
,
8575 BTRFS_BLOCK_RSV_DELALLOC
);
8576 ei
->runtime_flags
= 0;
8577 ei
->prop_compress
= BTRFS_COMPRESS_NONE
;
8578 ei
->defrag_compress
= BTRFS_COMPRESS_NONE
;
8580 ei
->delayed_node
= NULL
;
8582 ei
->i_otime
.tv_sec
= 0;
8583 ei
->i_otime
.tv_nsec
= 0;
8585 inode
= &ei
->vfs_inode
;
8586 extent_map_tree_init(&ei
->extent_tree
);
8587 extent_io_tree_init(fs_info
, &ei
->io_tree
, IO_TREE_INODE_IO
, inode
);
8588 extent_io_tree_init(fs_info
, &ei
->io_failure_tree
,
8589 IO_TREE_INODE_IO_FAILURE
, inode
);
8590 extent_io_tree_init(fs_info
, &ei
->file_extent_tree
,
8591 IO_TREE_INODE_FILE_EXTENT
, inode
);
8592 ei
->io_tree
.track_uptodate
= true;
8593 ei
->io_failure_tree
.track_uptodate
= true;
8594 atomic_set(&ei
->sync_writers
, 0);
8595 mutex_init(&ei
->log_mutex
);
8596 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
8597 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
8598 INIT_LIST_HEAD(&ei
->delayed_iput
);
8599 RB_CLEAR_NODE(&ei
->rb_node
);
8600 init_rwsem(&ei
->dio_sem
);
8605 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8606 void btrfs_test_destroy_inode(struct inode
*inode
)
8608 btrfs_drop_extent_cache(BTRFS_I(inode
), 0, (u64
)-1, 0);
8609 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
8613 void btrfs_free_inode(struct inode
*inode
)
8615 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
8618 void btrfs_destroy_inode(struct inode
*inode
)
8620 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8621 struct btrfs_ordered_extent
*ordered
;
8622 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8624 WARN_ON(!hlist_empty(&inode
->i_dentry
));
8625 WARN_ON(inode
->i_data
.nrpages
);
8626 WARN_ON(BTRFS_I(inode
)->block_rsv
.reserved
);
8627 WARN_ON(BTRFS_I(inode
)->block_rsv
.size
);
8628 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
8629 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
8630 WARN_ON(BTRFS_I(inode
)->new_delalloc_bytes
);
8631 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
8632 WARN_ON(BTRFS_I(inode
)->defrag_bytes
);
8635 * This can happen where we create an inode, but somebody else also
8636 * created the same inode and we need to destroy the one we already
8643 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
8648 "found ordered extent %llu %llu on inode cleanup",
8649 ordered
->file_offset
, ordered
->num_bytes
);
8650 btrfs_remove_ordered_extent(inode
, ordered
);
8651 btrfs_put_ordered_extent(ordered
);
8652 btrfs_put_ordered_extent(ordered
);
8655 btrfs_qgroup_check_reserved_leak(BTRFS_I(inode
));
8656 inode_tree_del(inode
);
8657 btrfs_drop_extent_cache(BTRFS_I(inode
), 0, (u64
)-1, 0);
8658 btrfs_inode_clear_file_extent_range(BTRFS_I(inode
), 0, (u64
)-1);
8659 btrfs_put_root(BTRFS_I(inode
)->root
);
8662 int btrfs_drop_inode(struct inode
*inode
)
8664 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8669 /* the snap/subvol tree is on deleting */
8670 if (btrfs_root_refs(&root
->root_item
) == 0)
8673 return generic_drop_inode(inode
);
8676 static void init_once(void *foo
)
8678 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
8680 inode_init_once(&ei
->vfs_inode
);
8683 void __cold
btrfs_destroy_cachep(void)
8686 * Make sure all delayed rcu free inodes are flushed before we
8690 kmem_cache_destroy(btrfs_inode_cachep
);
8691 kmem_cache_destroy(btrfs_trans_handle_cachep
);
8692 kmem_cache_destroy(btrfs_path_cachep
);
8693 kmem_cache_destroy(btrfs_free_space_cachep
);
8694 kmem_cache_destroy(btrfs_free_space_bitmap_cachep
);
8697 int __init
btrfs_init_cachep(void)
8699 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
8700 sizeof(struct btrfs_inode
), 0,
8701 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
| SLAB_ACCOUNT
,
8703 if (!btrfs_inode_cachep
)
8706 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
8707 sizeof(struct btrfs_trans_handle
), 0,
8708 SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
8709 if (!btrfs_trans_handle_cachep
)
8712 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
8713 sizeof(struct btrfs_path
), 0,
8714 SLAB_MEM_SPREAD
, NULL
);
8715 if (!btrfs_path_cachep
)
8718 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
8719 sizeof(struct btrfs_free_space
), 0,
8720 SLAB_MEM_SPREAD
, NULL
);
8721 if (!btrfs_free_space_cachep
)
8724 btrfs_free_space_bitmap_cachep
= kmem_cache_create("btrfs_free_space_bitmap",
8725 PAGE_SIZE
, PAGE_SIZE
,
8726 SLAB_RED_ZONE
, NULL
);
8727 if (!btrfs_free_space_bitmap_cachep
)
8732 btrfs_destroy_cachep();
8736 static int btrfs_getattr(const struct path
*path
, struct kstat
*stat
,
8737 u32 request_mask
, unsigned int flags
)
8740 struct inode
*inode
= d_inode(path
->dentry
);
8741 u32 blocksize
= inode
->i_sb
->s_blocksize
;
8742 u32 bi_flags
= BTRFS_I(inode
)->flags
;
8744 stat
->result_mask
|= STATX_BTIME
;
8745 stat
->btime
.tv_sec
= BTRFS_I(inode
)->i_otime
.tv_sec
;
8746 stat
->btime
.tv_nsec
= BTRFS_I(inode
)->i_otime
.tv_nsec
;
8747 if (bi_flags
& BTRFS_INODE_APPEND
)
8748 stat
->attributes
|= STATX_ATTR_APPEND
;
8749 if (bi_flags
& BTRFS_INODE_COMPRESS
)
8750 stat
->attributes
|= STATX_ATTR_COMPRESSED
;
8751 if (bi_flags
& BTRFS_INODE_IMMUTABLE
)
8752 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
8753 if (bi_flags
& BTRFS_INODE_NODUMP
)
8754 stat
->attributes
|= STATX_ATTR_NODUMP
;
8756 stat
->attributes_mask
|= (STATX_ATTR_APPEND
|
8757 STATX_ATTR_COMPRESSED
|
8758 STATX_ATTR_IMMUTABLE
|
8761 generic_fillattr(inode
, stat
);
8762 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
8764 spin_lock(&BTRFS_I(inode
)->lock
);
8765 delalloc_bytes
= BTRFS_I(inode
)->new_delalloc_bytes
;
8766 spin_unlock(&BTRFS_I(inode
)->lock
);
8767 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
8768 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
8772 static int btrfs_rename_exchange(struct inode
*old_dir
,
8773 struct dentry
*old_dentry
,
8774 struct inode
*new_dir
,
8775 struct dentry
*new_dentry
)
8777 struct btrfs_fs_info
*fs_info
= btrfs_sb(old_dir
->i_sb
);
8778 struct btrfs_trans_handle
*trans
;
8779 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
8780 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
8781 struct inode
*new_inode
= new_dentry
->d_inode
;
8782 struct inode
*old_inode
= old_dentry
->d_inode
;
8783 struct timespec64 ctime
= current_time(old_inode
);
8784 struct dentry
*parent
;
8785 u64 old_ino
= btrfs_ino(BTRFS_I(old_inode
));
8786 u64 new_ino
= btrfs_ino(BTRFS_I(new_inode
));
8790 bool root_log_pinned
= false;
8791 bool dest_log_pinned
= false;
8792 struct btrfs_log_ctx ctx_root
;
8793 struct btrfs_log_ctx ctx_dest
;
8794 bool sync_log_root
= false;
8795 bool sync_log_dest
= false;
8796 bool commit_transaction
= false;
8798 /* we only allow rename subvolume link between subvolumes */
8799 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8802 btrfs_init_log_ctx(&ctx_root
, old_inode
);
8803 btrfs_init_log_ctx(&ctx_dest
, new_inode
);
8805 /* close the race window with snapshot create/destroy ioctl */
8806 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
||
8807 new_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8808 down_read(&fs_info
->subvol_sem
);
8811 * We want to reserve the absolute worst case amount of items. So if
8812 * both inodes are subvols and we need to unlink them then that would
8813 * require 4 item modifications, but if they are both normal inodes it
8814 * would require 5 item modifications, so we'll assume their normal
8815 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8816 * should cover the worst case number of items we'll modify.
8818 trans
= btrfs_start_transaction(root
, 12);
8819 if (IS_ERR(trans
)) {
8820 ret
= PTR_ERR(trans
);
8825 btrfs_record_root_in_trans(trans
, dest
);
8828 * We need to find a free sequence number both in the source and
8829 * in the destination directory for the exchange.
8831 ret
= btrfs_set_inode_index(BTRFS_I(new_dir
), &old_idx
);
8834 ret
= btrfs_set_inode_index(BTRFS_I(old_dir
), &new_idx
);
8838 BTRFS_I(old_inode
)->dir_index
= 0ULL;
8839 BTRFS_I(new_inode
)->dir_index
= 0ULL;
8841 /* Reference for the source. */
8842 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
8843 /* force full log commit if subvolume involved. */
8844 btrfs_set_log_full_commit(trans
);
8846 btrfs_pin_log_trans(root
);
8847 root_log_pinned
= true;
8848 ret
= btrfs_insert_inode_ref(trans
, dest
,
8849 new_dentry
->d_name
.name
,
8850 new_dentry
->d_name
.len
,
8852 btrfs_ino(BTRFS_I(new_dir
)),
8858 /* And now for the dest. */
8859 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
8860 /* force full log commit if subvolume involved. */
8861 btrfs_set_log_full_commit(trans
);
8863 btrfs_pin_log_trans(dest
);
8864 dest_log_pinned
= true;
8865 ret
= btrfs_insert_inode_ref(trans
, root
,
8866 old_dentry
->d_name
.name
,
8867 old_dentry
->d_name
.len
,
8869 btrfs_ino(BTRFS_I(old_dir
)),
8875 /* Update inode version and ctime/mtime. */
8876 inode_inc_iversion(old_dir
);
8877 inode_inc_iversion(new_dir
);
8878 inode_inc_iversion(old_inode
);
8879 inode_inc_iversion(new_inode
);
8880 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8881 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8882 old_inode
->i_ctime
= ctime
;
8883 new_inode
->i_ctime
= ctime
;
8885 if (old_dentry
->d_parent
!= new_dentry
->d_parent
) {
8886 btrfs_record_unlink_dir(trans
, BTRFS_I(old_dir
),
8887 BTRFS_I(old_inode
), 1);
8888 btrfs_record_unlink_dir(trans
, BTRFS_I(new_dir
),
8889 BTRFS_I(new_inode
), 1);
8892 /* src is a subvolume */
8893 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
8894 ret
= btrfs_unlink_subvol(trans
, old_dir
, old_dentry
);
8895 } else { /* src is an inode */
8896 ret
= __btrfs_unlink_inode(trans
, root
, BTRFS_I(old_dir
),
8897 BTRFS_I(old_dentry
->d_inode
),
8898 old_dentry
->d_name
.name
,
8899 old_dentry
->d_name
.len
);
8901 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8904 btrfs_abort_transaction(trans
, ret
);
8908 /* dest is a subvolume */
8909 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
8910 ret
= btrfs_unlink_subvol(trans
, new_dir
, new_dentry
);
8911 } else { /* dest is an inode */
8912 ret
= __btrfs_unlink_inode(trans
, dest
, BTRFS_I(new_dir
),
8913 BTRFS_I(new_dentry
->d_inode
),
8914 new_dentry
->d_name
.name
,
8915 new_dentry
->d_name
.len
);
8917 ret
= btrfs_update_inode(trans
, dest
, new_inode
);
8920 btrfs_abort_transaction(trans
, ret
);
8924 ret
= btrfs_add_link(trans
, BTRFS_I(new_dir
), BTRFS_I(old_inode
),
8925 new_dentry
->d_name
.name
,
8926 new_dentry
->d_name
.len
, 0, old_idx
);
8928 btrfs_abort_transaction(trans
, ret
);
8932 ret
= btrfs_add_link(trans
, BTRFS_I(old_dir
), BTRFS_I(new_inode
),
8933 old_dentry
->d_name
.name
,
8934 old_dentry
->d_name
.len
, 0, new_idx
);
8936 btrfs_abort_transaction(trans
, ret
);
8940 if (old_inode
->i_nlink
== 1)
8941 BTRFS_I(old_inode
)->dir_index
= old_idx
;
8942 if (new_inode
->i_nlink
== 1)
8943 BTRFS_I(new_inode
)->dir_index
= new_idx
;
8945 if (root_log_pinned
) {
8946 parent
= new_dentry
->d_parent
;
8947 ret
= btrfs_log_new_name(trans
, BTRFS_I(old_inode
),
8948 BTRFS_I(old_dir
), parent
,
8950 if (ret
== BTRFS_NEED_LOG_SYNC
)
8951 sync_log_root
= true;
8952 else if (ret
== BTRFS_NEED_TRANS_COMMIT
)
8953 commit_transaction
= true;
8955 btrfs_end_log_trans(root
);
8956 root_log_pinned
= false;
8958 if (dest_log_pinned
) {
8959 if (!commit_transaction
) {
8960 parent
= old_dentry
->d_parent
;
8961 ret
= btrfs_log_new_name(trans
, BTRFS_I(new_inode
),
8962 BTRFS_I(new_dir
), parent
,
8964 if (ret
== BTRFS_NEED_LOG_SYNC
)
8965 sync_log_dest
= true;
8966 else if (ret
== BTRFS_NEED_TRANS_COMMIT
)
8967 commit_transaction
= true;
8970 btrfs_end_log_trans(dest
);
8971 dest_log_pinned
= false;
8975 * If we have pinned a log and an error happened, we unpin tasks
8976 * trying to sync the log and force them to fallback to a transaction
8977 * commit if the log currently contains any of the inodes involved in
8978 * this rename operation (to ensure we do not persist a log with an
8979 * inconsistent state for any of these inodes or leading to any
8980 * inconsistencies when replayed). If the transaction was aborted, the
8981 * abortion reason is propagated to userspace when attempting to commit
8982 * the transaction. If the log does not contain any of these inodes, we
8983 * allow the tasks to sync it.
8985 if (ret
&& (root_log_pinned
|| dest_log_pinned
)) {
8986 if (btrfs_inode_in_log(BTRFS_I(old_dir
), fs_info
->generation
) ||
8987 btrfs_inode_in_log(BTRFS_I(new_dir
), fs_info
->generation
) ||
8988 btrfs_inode_in_log(BTRFS_I(old_inode
), fs_info
->generation
) ||
8990 btrfs_inode_in_log(BTRFS_I(new_inode
), fs_info
->generation
)))
8991 btrfs_set_log_full_commit(trans
);
8993 if (root_log_pinned
) {
8994 btrfs_end_log_trans(root
);
8995 root_log_pinned
= false;
8997 if (dest_log_pinned
) {
8998 btrfs_end_log_trans(dest
);
8999 dest_log_pinned
= false;
9002 if (!ret
&& sync_log_root
&& !commit_transaction
) {
9003 ret
= btrfs_sync_log(trans
, BTRFS_I(old_inode
)->root
,
9006 commit_transaction
= true;
9008 if (!ret
&& sync_log_dest
&& !commit_transaction
) {
9009 ret
= btrfs_sync_log(trans
, BTRFS_I(new_inode
)->root
,
9012 commit_transaction
= true;
9014 if (commit_transaction
) {
9016 * We may have set commit_transaction when logging the new name
9017 * in the destination root, in which case we left the source
9018 * root context in the list of log contextes. So make sure we
9019 * remove it to avoid invalid memory accesses, since the context
9020 * was allocated in our stack frame.
9022 if (sync_log_root
) {
9023 mutex_lock(&root
->log_mutex
);
9024 list_del_init(&ctx_root
.list
);
9025 mutex_unlock(&root
->log_mutex
);
9027 ret
= btrfs_commit_transaction(trans
);
9031 ret2
= btrfs_end_transaction(trans
);
9032 ret
= ret
? ret
: ret2
;
9035 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
||
9036 old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9037 up_read(&fs_info
->subvol_sem
);
9039 ASSERT(list_empty(&ctx_root
.list
));
9040 ASSERT(list_empty(&ctx_dest
.list
));
9045 static int btrfs_whiteout_for_rename(struct btrfs_trans_handle
*trans
,
9046 struct btrfs_root
*root
,
9048 struct dentry
*dentry
)
9051 struct inode
*inode
;
9055 ret
= btrfs_find_free_ino(root
, &objectid
);
9059 inode
= btrfs_new_inode(trans
, root
, dir
,
9060 dentry
->d_name
.name
,
9062 btrfs_ino(BTRFS_I(dir
)),
9064 S_IFCHR
| WHITEOUT_MODE
,
9067 if (IS_ERR(inode
)) {
9068 ret
= PTR_ERR(inode
);
9072 inode
->i_op
= &btrfs_special_inode_operations
;
9073 init_special_inode(inode
, inode
->i_mode
,
9076 ret
= btrfs_init_inode_security(trans
, inode
, dir
,
9081 ret
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
,
9082 BTRFS_I(inode
), 0, index
);
9086 ret
= btrfs_update_inode(trans
, root
, inode
);
9088 unlock_new_inode(inode
);
9090 inode_dec_link_count(inode
);
9096 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
9097 struct inode
*new_dir
, struct dentry
*new_dentry
,
9100 struct btrfs_fs_info
*fs_info
= btrfs_sb(old_dir
->i_sb
);
9101 struct btrfs_trans_handle
*trans
;
9102 unsigned int trans_num_items
;
9103 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
9104 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
9105 struct inode
*new_inode
= d_inode(new_dentry
);
9106 struct inode
*old_inode
= d_inode(old_dentry
);
9109 u64 old_ino
= btrfs_ino(BTRFS_I(old_inode
));
9110 bool log_pinned
= false;
9111 struct btrfs_log_ctx ctx
;
9112 bool sync_log
= false;
9113 bool commit_transaction
= false;
9115 if (btrfs_ino(BTRFS_I(new_dir
)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
9118 /* we only allow rename subvolume link between subvolumes */
9119 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
9122 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
9123 (new_inode
&& btrfs_ino(BTRFS_I(new_inode
)) == BTRFS_FIRST_FREE_OBJECTID
))
9126 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
9127 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
9131 /* check for collisions, even if the name isn't there */
9132 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
9133 new_dentry
->d_name
.name
,
9134 new_dentry
->d_name
.len
);
9137 if (ret
== -EEXIST
) {
9139 * eexist without a new_inode */
9140 if (WARN_ON(!new_inode
)) {
9144 /* maybe -EOVERFLOW */
9151 * we're using rename to replace one file with another. Start IO on it
9152 * now so we don't add too much work to the end of the transaction
9154 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
)
9155 filemap_flush(old_inode
->i_mapping
);
9157 /* close the racy window with snapshot create/destroy ioctl */
9158 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9159 down_read(&fs_info
->subvol_sem
);
9161 * We want to reserve the absolute worst case amount of items. So if
9162 * both inodes are subvols and we need to unlink them then that would
9163 * require 4 item modifications, but if they are both normal inodes it
9164 * would require 5 item modifications, so we'll assume they are normal
9165 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9166 * should cover the worst case number of items we'll modify.
9167 * If our rename has the whiteout flag, we need more 5 units for the
9168 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9169 * when selinux is enabled).
9171 trans_num_items
= 11;
9172 if (flags
& RENAME_WHITEOUT
)
9173 trans_num_items
+= 5;
9174 trans
= btrfs_start_transaction(root
, trans_num_items
);
9175 if (IS_ERR(trans
)) {
9176 ret
= PTR_ERR(trans
);
9181 btrfs_record_root_in_trans(trans
, dest
);
9183 ret
= btrfs_set_inode_index(BTRFS_I(new_dir
), &index
);
9187 BTRFS_I(old_inode
)->dir_index
= 0ULL;
9188 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9189 /* force full log commit if subvolume involved. */
9190 btrfs_set_log_full_commit(trans
);
9192 btrfs_pin_log_trans(root
);
9194 ret
= btrfs_insert_inode_ref(trans
, dest
,
9195 new_dentry
->d_name
.name
,
9196 new_dentry
->d_name
.len
,
9198 btrfs_ino(BTRFS_I(new_dir
)), index
);
9203 inode_inc_iversion(old_dir
);
9204 inode_inc_iversion(new_dir
);
9205 inode_inc_iversion(old_inode
);
9206 old_dir
->i_ctime
= old_dir
->i_mtime
=
9207 new_dir
->i_ctime
= new_dir
->i_mtime
=
9208 old_inode
->i_ctime
= current_time(old_dir
);
9210 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
9211 btrfs_record_unlink_dir(trans
, BTRFS_I(old_dir
),
9212 BTRFS_I(old_inode
), 1);
9214 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9215 ret
= btrfs_unlink_subvol(trans
, old_dir
, old_dentry
);
9217 ret
= __btrfs_unlink_inode(trans
, root
, BTRFS_I(old_dir
),
9218 BTRFS_I(d_inode(old_dentry
)),
9219 old_dentry
->d_name
.name
,
9220 old_dentry
->d_name
.len
);
9222 ret
= btrfs_update_inode(trans
, root
, old_inode
);
9225 btrfs_abort_transaction(trans
, ret
);
9230 inode_inc_iversion(new_inode
);
9231 new_inode
->i_ctime
= current_time(new_inode
);
9232 if (unlikely(btrfs_ino(BTRFS_I(new_inode
)) ==
9233 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
9234 ret
= btrfs_unlink_subvol(trans
, new_dir
, new_dentry
);
9235 BUG_ON(new_inode
->i_nlink
== 0);
9237 ret
= btrfs_unlink_inode(trans
, dest
, BTRFS_I(new_dir
),
9238 BTRFS_I(d_inode(new_dentry
)),
9239 new_dentry
->d_name
.name
,
9240 new_dentry
->d_name
.len
);
9242 if (!ret
&& new_inode
->i_nlink
== 0)
9243 ret
= btrfs_orphan_add(trans
,
9244 BTRFS_I(d_inode(new_dentry
)));
9246 btrfs_abort_transaction(trans
, ret
);
9251 ret
= btrfs_add_link(trans
, BTRFS_I(new_dir
), BTRFS_I(old_inode
),
9252 new_dentry
->d_name
.name
,
9253 new_dentry
->d_name
.len
, 0, index
);
9255 btrfs_abort_transaction(trans
, ret
);
9259 if (old_inode
->i_nlink
== 1)
9260 BTRFS_I(old_inode
)->dir_index
= index
;
9263 struct dentry
*parent
= new_dentry
->d_parent
;
9265 btrfs_init_log_ctx(&ctx
, old_inode
);
9266 ret
= btrfs_log_new_name(trans
, BTRFS_I(old_inode
),
9267 BTRFS_I(old_dir
), parent
,
9269 if (ret
== BTRFS_NEED_LOG_SYNC
)
9271 else if (ret
== BTRFS_NEED_TRANS_COMMIT
)
9272 commit_transaction
= true;
9274 btrfs_end_log_trans(root
);
9278 if (flags
& RENAME_WHITEOUT
) {
9279 ret
= btrfs_whiteout_for_rename(trans
, root
, old_dir
,
9283 btrfs_abort_transaction(trans
, ret
);
9289 * If we have pinned the log and an error happened, we unpin tasks
9290 * trying to sync the log and force them to fallback to a transaction
9291 * commit if the log currently contains any of the inodes involved in
9292 * this rename operation (to ensure we do not persist a log with an
9293 * inconsistent state for any of these inodes or leading to any
9294 * inconsistencies when replayed). If the transaction was aborted, the
9295 * abortion reason is propagated to userspace when attempting to commit
9296 * the transaction. If the log does not contain any of these inodes, we
9297 * allow the tasks to sync it.
9299 if (ret
&& log_pinned
) {
9300 if (btrfs_inode_in_log(BTRFS_I(old_dir
), fs_info
->generation
) ||
9301 btrfs_inode_in_log(BTRFS_I(new_dir
), fs_info
->generation
) ||
9302 btrfs_inode_in_log(BTRFS_I(old_inode
), fs_info
->generation
) ||
9304 btrfs_inode_in_log(BTRFS_I(new_inode
), fs_info
->generation
)))
9305 btrfs_set_log_full_commit(trans
);
9307 btrfs_end_log_trans(root
);
9310 if (!ret
&& sync_log
) {
9311 ret
= btrfs_sync_log(trans
, BTRFS_I(old_inode
)->root
, &ctx
);
9313 commit_transaction
= true;
9314 } else if (sync_log
) {
9315 mutex_lock(&root
->log_mutex
);
9316 list_del(&ctx
.list
);
9317 mutex_unlock(&root
->log_mutex
);
9319 if (commit_transaction
) {
9320 ret
= btrfs_commit_transaction(trans
);
9324 ret2
= btrfs_end_transaction(trans
);
9325 ret
= ret
? ret
: ret2
;
9328 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9329 up_read(&fs_info
->subvol_sem
);
9334 static int btrfs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
9335 struct inode
*new_dir
, struct dentry
*new_dentry
,
9338 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
9341 if (flags
& RENAME_EXCHANGE
)
9342 return btrfs_rename_exchange(old_dir
, old_dentry
, new_dir
,
9345 return btrfs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
9348 struct btrfs_delalloc_work
{
9349 struct inode
*inode
;
9350 struct completion completion
;
9351 struct list_head list
;
9352 struct btrfs_work work
;
9355 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
9357 struct btrfs_delalloc_work
*delalloc_work
;
9358 struct inode
*inode
;
9360 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
9362 inode
= delalloc_work
->inode
;
9363 filemap_flush(inode
->i_mapping
);
9364 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
9365 &BTRFS_I(inode
)->runtime_flags
))
9366 filemap_flush(inode
->i_mapping
);
9369 complete(&delalloc_work
->completion
);
9372 static struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
)
9374 struct btrfs_delalloc_work
*work
;
9376 work
= kmalloc(sizeof(*work
), GFP_NOFS
);
9380 init_completion(&work
->completion
);
9381 INIT_LIST_HEAD(&work
->list
);
9382 work
->inode
= inode
;
9383 btrfs_init_work(&work
->work
, btrfs_run_delalloc_work
, NULL
, NULL
);
9389 * some fairly slow code that needs optimization. This walks the list
9390 * of all the inodes with pending delalloc and forces them to disk.
9392 static int start_delalloc_inodes(struct btrfs_root
*root
, int nr
, bool snapshot
)
9394 struct btrfs_inode
*binode
;
9395 struct inode
*inode
;
9396 struct btrfs_delalloc_work
*work
, *next
;
9397 struct list_head works
;
9398 struct list_head splice
;
9401 INIT_LIST_HEAD(&works
);
9402 INIT_LIST_HEAD(&splice
);
9404 mutex_lock(&root
->delalloc_mutex
);
9405 spin_lock(&root
->delalloc_lock
);
9406 list_splice_init(&root
->delalloc_inodes
, &splice
);
9407 while (!list_empty(&splice
)) {
9408 binode
= list_entry(splice
.next
, struct btrfs_inode
,
9411 list_move_tail(&binode
->delalloc_inodes
,
9412 &root
->delalloc_inodes
);
9413 inode
= igrab(&binode
->vfs_inode
);
9415 cond_resched_lock(&root
->delalloc_lock
);
9418 spin_unlock(&root
->delalloc_lock
);
9421 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH
,
9422 &binode
->runtime_flags
);
9423 work
= btrfs_alloc_delalloc_work(inode
);
9429 list_add_tail(&work
->list
, &works
);
9430 btrfs_queue_work(root
->fs_info
->flush_workers
,
9433 if (nr
!= -1 && ret
>= nr
)
9436 spin_lock(&root
->delalloc_lock
);
9438 spin_unlock(&root
->delalloc_lock
);
9441 list_for_each_entry_safe(work
, next
, &works
, list
) {
9442 list_del_init(&work
->list
);
9443 wait_for_completion(&work
->completion
);
9447 if (!list_empty(&splice
)) {
9448 spin_lock(&root
->delalloc_lock
);
9449 list_splice_tail(&splice
, &root
->delalloc_inodes
);
9450 spin_unlock(&root
->delalloc_lock
);
9452 mutex_unlock(&root
->delalloc_mutex
);
9456 int btrfs_start_delalloc_snapshot(struct btrfs_root
*root
)
9458 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
9461 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
9464 ret
= start_delalloc_inodes(root
, -1, true);
9470 int btrfs_start_delalloc_roots(struct btrfs_fs_info
*fs_info
, int nr
)
9472 struct btrfs_root
*root
;
9473 struct list_head splice
;
9476 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
9479 INIT_LIST_HEAD(&splice
);
9481 mutex_lock(&fs_info
->delalloc_root_mutex
);
9482 spin_lock(&fs_info
->delalloc_root_lock
);
9483 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
9484 while (!list_empty(&splice
) && nr
) {
9485 root
= list_first_entry(&splice
, struct btrfs_root
,
9487 root
= btrfs_grab_root(root
);
9489 list_move_tail(&root
->delalloc_root
,
9490 &fs_info
->delalloc_roots
);
9491 spin_unlock(&fs_info
->delalloc_root_lock
);
9493 ret
= start_delalloc_inodes(root
, nr
, false);
9494 btrfs_put_root(root
);
9502 spin_lock(&fs_info
->delalloc_root_lock
);
9504 spin_unlock(&fs_info
->delalloc_root_lock
);
9508 if (!list_empty(&splice
)) {
9509 spin_lock(&fs_info
->delalloc_root_lock
);
9510 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
9511 spin_unlock(&fs_info
->delalloc_root_lock
);
9513 mutex_unlock(&fs_info
->delalloc_root_mutex
);
9517 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
9518 const char *symname
)
9520 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
9521 struct btrfs_trans_handle
*trans
;
9522 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
9523 struct btrfs_path
*path
;
9524 struct btrfs_key key
;
9525 struct inode
*inode
= NULL
;
9532 struct btrfs_file_extent_item
*ei
;
9533 struct extent_buffer
*leaf
;
9535 name_len
= strlen(symname
);
9536 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(fs_info
))
9537 return -ENAMETOOLONG
;
9540 * 2 items for inode item and ref
9541 * 2 items for dir items
9542 * 1 item for updating parent inode item
9543 * 1 item for the inline extent item
9544 * 1 item for xattr if selinux is on
9546 trans
= btrfs_start_transaction(root
, 7);
9548 return PTR_ERR(trans
);
9550 err
= btrfs_find_free_ino(root
, &objectid
);
9554 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
9555 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)),
9556 objectid
, S_IFLNK
|S_IRWXUGO
, &index
);
9557 if (IS_ERR(inode
)) {
9558 err
= PTR_ERR(inode
);
9564 * If the active LSM wants to access the inode during
9565 * d_instantiate it needs these. Smack checks to see
9566 * if the filesystem supports xattrs by looking at the
9569 inode
->i_fop
= &btrfs_file_operations
;
9570 inode
->i_op
= &btrfs_file_inode_operations
;
9571 inode
->i_mapping
->a_ops
= &btrfs_aops
;
9572 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
9574 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
9578 path
= btrfs_alloc_path();
9583 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
9585 key
.type
= BTRFS_EXTENT_DATA_KEY
;
9586 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
9587 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
9590 btrfs_free_path(path
);
9593 leaf
= path
->nodes
[0];
9594 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
9595 struct btrfs_file_extent_item
);
9596 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
9597 btrfs_set_file_extent_type(leaf
, ei
,
9598 BTRFS_FILE_EXTENT_INLINE
);
9599 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
9600 btrfs_set_file_extent_compression(leaf
, ei
, 0);
9601 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
9602 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
9604 ptr
= btrfs_file_extent_inline_start(ei
);
9605 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
9606 btrfs_mark_buffer_dirty(leaf
);
9607 btrfs_free_path(path
);
9609 inode
->i_op
= &btrfs_symlink_inode_operations
;
9610 inode_nohighmem(inode
);
9611 inode_set_bytes(inode
, name_len
);
9612 btrfs_i_size_write(BTRFS_I(inode
), name_len
);
9613 err
= btrfs_update_inode(trans
, root
, inode
);
9615 * Last step, add directory indexes for our symlink inode. This is the
9616 * last step to avoid extra cleanup of these indexes if an error happens
9620 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
,
9621 BTRFS_I(inode
), 0, index
);
9625 d_instantiate_new(dentry
, inode
);
9628 btrfs_end_transaction(trans
);
9630 inode_dec_link_count(inode
);
9631 discard_new_inode(inode
);
9633 btrfs_btree_balance_dirty(fs_info
);
9637 static int insert_prealloc_file_extent(struct btrfs_trans_handle
*trans
,
9638 struct inode
*inode
, struct btrfs_key
*ins
,
9641 struct btrfs_file_extent_item stack_fi
;
9642 u64 start
= ins
->objectid
;
9643 u64 len
= ins
->offset
;
9646 memset(&stack_fi
, 0, sizeof(stack_fi
));
9648 btrfs_set_stack_file_extent_type(&stack_fi
, BTRFS_FILE_EXTENT_PREALLOC
);
9649 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi
, start
);
9650 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi
, len
);
9651 btrfs_set_stack_file_extent_num_bytes(&stack_fi
, len
);
9652 btrfs_set_stack_file_extent_ram_bytes(&stack_fi
, len
);
9653 btrfs_set_stack_file_extent_compression(&stack_fi
, BTRFS_COMPRESS_NONE
);
9654 /* Encryption and other encoding is reserved and all 0 */
9656 ret
= btrfs_qgroup_release_data(BTRFS_I(inode
), file_offset
, len
);
9659 return insert_reserved_file_extent(trans
, BTRFS_I(inode
), file_offset
,
9662 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
9663 u64 start
, u64 num_bytes
, u64 min_size
,
9664 loff_t actual_len
, u64
*alloc_hint
,
9665 struct btrfs_trans_handle
*trans
)
9667 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
9668 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
9669 struct extent_map
*em
;
9670 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9671 struct btrfs_key ins
;
9672 u64 cur_offset
= start
;
9673 u64 clear_offset
= start
;
9676 u64 last_alloc
= (u64
)-1;
9678 bool own_trans
= true;
9679 u64 end
= start
+ num_bytes
- 1;
9683 while (num_bytes
> 0) {
9685 trans
= btrfs_start_transaction(root
, 3);
9686 if (IS_ERR(trans
)) {
9687 ret
= PTR_ERR(trans
);
9692 cur_bytes
= min_t(u64
, num_bytes
, SZ_256M
);
9693 cur_bytes
= max(cur_bytes
, min_size
);
9695 * If we are severely fragmented we could end up with really
9696 * small allocations, so if the allocator is returning small
9697 * chunks lets make its job easier by only searching for those
9700 cur_bytes
= min(cur_bytes
, last_alloc
);
9701 ret
= btrfs_reserve_extent(root
, cur_bytes
, cur_bytes
,
9702 min_size
, 0, *alloc_hint
, &ins
, 1, 0);
9705 btrfs_end_transaction(trans
);
9710 * We've reserved this space, and thus converted it from
9711 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9712 * from here on out we will only need to clear our reservation
9713 * for the remaining unreserved area, so advance our
9714 * clear_offset by our extent size.
9716 clear_offset
+= ins
.offset
;
9717 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
9719 last_alloc
= ins
.offset
;
9720 ret
= insert_prealloc_file_extent(trans
, inode
, &ins
, cur_offset
);
9722 btrfs_free_reserved_extent(fs_info
, ins
.objectid
,
9724 btrfs_abort_transaction(trans
, ret
);
9726 btrfs_end_transaction(trans
);
9730 btrfs_drop_extent_cache(BTRFS_I(inode
), cur_offset
,
9731 cur_offset
+ ins
.offset
-1, 0);
9733 em
= alloc_extent_map();
9735 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
9736 &BTRFS_I(inode
)->runtime_flags
);
9740 em
->start
= cur_offset
;
9741 em
->orig_start
= cur_offset
;
9742 em
->len
= ins
.offset
;
9743 em
->block_start
= ins
.objectid
;
9744 em
->block_len
= ins
.offset
;
9745 em
->orig_block_len
= ins
.offset
;
9746 em
->ram_bytes
= ins
.offset
;
9747 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
9748 em
->generation
= trans
->transid
;
9751 write_lock(&em_tree
->lock
);
9752 ret
= add_extent_mapping(em_tree
, em
, 1);
9753 write_unlock(&em_tree
->lock
);
9756 btrfs_drop_extent_cache(BTRFS_I(inode
), cur_offset
,
9757 cur_offset
+ ins
.offset
- 1,
9760 free_extent_map(em
);
9762 num_bytes
-= ins
.offset
;
9763 cur_offset
+= ins
.offset
;
9764 *alloc_hint
= ins
.objectid
+ ins
.offset
;
9766 inode_inc_iversion(inode
);
9767 inode
->i_ctime
= current_time(inode
);
9768 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
9769 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
9770 (actual_len
> inode
->i_size
) &&
9771 (cur_offset
> inode
->i_size
)) {
9772 if (cur_offset
> actual_len
)
9773 i_size
= actual_len
;
9775 i_size
= cur_offset
;
9776 i_size_write(inode
, i_size
);
9777 btrfs_inode_safe_disk_i_size_write(inode
, 0);
9780 ret
= btrfs_update_inode(trans
, root
, inode
);
9783 btrfs_abort_transaction(trans
, ret
);
9785 btrfs_end_transaction(trans
);
9790 btrfs_end_transaction(trans
);
9792 if (clear_offset
< end
)
9793 btrfs_free_reserved_data_space(BTRFS_I(inode
), NULL
, clear_offset
,
9794 end
- clear_offset
+ 1);
9798 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
9799 u64 start
, u64 num_bytes
, u64 min_size
,
9800 loff_t actual_len
, u64
*alloc_hint
)
9802 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
9803 min_size
, actual_len
, alloc_hint
,
9807 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
9808 struct btrfs_trans_handle
*trans
, int mode
,
9809 u64 start
, u64 num_bytes
, u64 min_size
,
9810 loff_t actual_len
, u64
*alloc_hint
)
9812 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
9813 min_size
, actual_len
, alloc_hint
, trans
);
9816 static int btrfs_set_page_dirty(struct page
*page
)
9818 return __set_page_dirty_nobuffers(page
);
9821 static int btrfs_permission(struct inode
*inode
, int mask
)
9823 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9824 umode_t mode
= inode
->i_mode
;
9826 if (mask
& MAY_WRITE
&&
9827 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
9828 if (btrfs_root_readonly(root
))
9830 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
9833 return generic_permission(inode
, mask
);
9836 static int btrfs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
9838 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
9839 struct btrfs_trans_handle
*trans
;
9840 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
9841 struct inode
*inode
= NULL
;
9847 * 5 units required for adding orphan entry
9849 trans
= btrfs_start_transaction(root
, 5);
9851 return PTR_ERR(trans
);
9853 ret
= btrfs_find_free_ino(root
, &objectid
);
9857 inode
= btrfs_new_inode(trans
, root
, dir
, NULL
, 0,
9858 btrfs_ino(BTRFS_I(dir
)), objectid
, mode
, &index
);
9859 if (IS_ERR(inode
)) {
9860 ret
= PTR_ERR(inode
);
9865 inode
->i_fop
= &btrfs_file_operations
;
9866 inode
->i_op
= &btrfs_file_inode_operations
;
9868 inode
->i_mapping
->a_ops
= &btrfs_aops
;
9869 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
9871 ret
= btrfs_init_inode_security(trans
, inode
, dir
, NULL
);
9875 ret
= btrfs_update_inode(trans
, root
, inode
);
9878 ret
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
9883 * We set number of links to 0 in btrfs_new_inode(), and here we set
9884 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9887 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9889 set_nlink(inode
, 1);
9890 d_tmpfile(dentry
, inode
);
9891 unlock_new_inode(inode
);
9892 mark_inode_dirty(inode
);
9894 btrfs_end_transaction(trans
);
9896 discard_new_inode(inode
);
9897 btrfs_btree_balance_dirty(fs_info
);
9901 void btrfs_set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
9903 struct inode
*inode
= tree
->private_data
;
9904 unsigned long index
= start
>> PAGE_SHIFT
;
9905 unsigned long end_index
= end
>> PAGE_SHIFT
;
9908 while (index
<= end_index
) {
9909 page
= find_get_page(inode
->i_mapping
, index
);
9910 ASSERT(page
); /* Pages should be in the extent_io_tree */
9911 set_page_writeback(page
);
9919 * Add an entry indicating a block group or device which is pinned by a
9920 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9921 * negative errno on failure.
9923 static int btrfs_add_swapfile_pin(struct inode
*inode
, void *ptr
,
9924 bool is_block_group
)
9926 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
9927 struct btrfs_swapfile_pin
*sp
, *entry
;
9929 struct rb_node
*parent
= NULL
;
9931 sp
= kmalloc(sizeof(*sp
), GFP_NOFS
);
9936 sp
->is_block_group
= is_block_group
;
9938 spin_lock(&fs_info
->swapfile_pins_lock
);
9939 p
= &fs_info
->swapfile_pins
.rb_node
;
9942 entry
= rb_entry(parent
, struct btrfs_swapfile_pin
, node
);
9943 if (sp
->ptr
< entry
->ptr
||
9944 (sp
->ptr
== entry
->ptr
&& sp
->inode
< entry
->inode
)) {
9946 } else if (sp
->ptr
> entry
->ptr
||
9947 (sp
->ptr
== entry
->ptr
&& sp
->inode
> entry
->inode
)) {
9948 p
= &(*p
)->rb_right
;
9950 spin_unlock(&fs_info
->swapfile_pins_lock
);
9955 rb_link_node(&sp
->node
, parent
, p
);
9956 rb_insert_color(&sp
->node
, &fs_info
->swapfile_pins
);
9957 spin_unlock(&fs_info
->swapfile_pins_lock
);
9961 /* Free all of the entries pinned by this swapfile. */
9962 static void btrfs_free_swapfile_pins(struct inode
*inode
)
9964 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
9965 struct btrfs_swapfile_pin
*sp
;
9966 struct rb_node
*node
, *next
;
9968 spin_lock(&fs_info
->swapfile_pins_lock
);
9969 node
= rb_first(&fs_info
->swapfile_pins
);
9971 next
= rb_next(node
);
9972 sp
= rb_entry(node
, struct btrfs_swapfile_pin
, node
);
9973 if (sp
->inode
== inode
) {
9974 rb_erase(&sp
->node
, &fs_info
->swapfile_pins
);
9975 if (sp
->is_block_group
)
9976 btrfs_put_block_group(sp
->ptr
);
9981 spin_unlock(&fs_info
->swapfile_pins_lock
);
9984 struct btrfs_swap_info
{
9990 unsigned long nr_pages
;
9994 static int btrfs_add_swap_extent(struct swap_info_struct
*sis
,
9995 struct btrfs_swap_info
*bsi
)
9997 unsigned long nr_pages
;
9998 u64 first_ppage
, first_ppage_reported
, next_ppage
;
10001 first_ppage
= ALIGN(bsi
->block_start
, PAGE_SIZE
) >> PAGE_SHIFT
;
10002 next_ppage
= ALIGN_DOWN(bsi
->block_start
+ bsi
->block_len
,
10003 PAGE_SIZE
) >> PAGE_SHIFT
;
10005 if (first_ppage
>= next_ppage
)
10007 nr_pages
= next_ppage
- first_ppage
;
10009 first_ppage_reported
= first_ppage
;
10010 if (bsi
->start
== 0)
10011 first_ppage_reported
++;
10012 if (bsi
->lowest_ppage
> first_ppage_reported
)
10013 bsi
->lowest_ppage
= first_ppage_reported
;
10014 if (bsi
->highest_ppage
< (next_ppage
- 1))
10015 bsi
->highest_ppage
= next_ppage
- 1;
10017 ret
= add_swap_extent(sis
, bsi
->nr_pages
, nr_pages
, first_ppage
);
10020 bsi
->nr_extents
+= ret
;
10021 bsi
->nr_pages
+= nr_pages
;
10025 static void btrfs_swap_deactivate(struct file
*file
)
10027 struct inode
*inode
= file_inode(file
);
10029 btrfs_free_swapfile_pins(inode
);
10030 atomic_dec(&BTRFS_I(inode
)->root
->nr_swapfiles
);
10033 static int btrfs_swap_activate(struct swap_info_struct
*sis
, struct file
*file
,
10036 struct inode
*inode
= file_inode(file
);
10037 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
10038 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
10039 struct extent_state
*cached_state
= NULL
;
10040 struct extent_map
*em
= NULL
;
10041 struct btrfs_device
*device
= NULL
;
10042 struct btrfs_swap_info bsi
= {
10043 .lowest_ppage
= (sector_t
)-1ULL,
10050 * If the swap file was just created, make sure delalloc is done. If the
10051 * file changes again after this, the user is doing something stupid and
10052 * we don't really care.
10054 ret
= btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
10059 * The inode is locked, so these flags won't change after we check them.
10061 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
) {
10062 btrfs_warn(fs_info
, "swapfile must not be compressed");
10065 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
)) {
10066 btrfs_warn(fs_info
, "swapfile must not be copy-on-write");
10069 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
10070 btrfs_warn(fs_info
, "swapfile must not be checksummed");
10075 * Balance or device remove/replace/resize can move stuff around from
10076 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10077 * concurrently while we are mapping the swap extents, and
10078 * fs_info->swapfile_pins prevents them from running while the swap file
10079 * is active and moving the extents. Note that this also prevents a
10080 * concurrent device add which isn't actually necessary, but it's not
10081 * really worth the trouble to allow it.
10083 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
10084 btrfs_warn(fs_info
,
10085 "cannot activate swapfile while exclusive operation is running");
10089 * Snapshots can create extents which require COW even if NODATACOW is
10090 * set. We use this counter to prevent snapshots. We must increment it
10091 * before walking the extents because we don't want a concurrent
10092 * snapshot to run after we've already checked the extents.
10094 atomic_inc(&BTRFS_I(inode
)->root
->nr_swapfiles
);
10096 isize
= ALIGN_DOWN(inode
->i_size
, fs_info
->sectorsize
);
10098 lock_extent_bits(io_tree
, 0, isize
- 1, &cached_state
);
10100 while (start
< isize
) {
10101 u64 logical_block_start
, physical_block_start
;
10102 struct btrfs_block_group
*bg
;
10103 u64 len
= isize
- start
;
10105 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, start
, len
);
10111 if (em
->block_start
== EXTENT_MAP_HOLE
) {
10112 btrfs_warn(fs_info
, "swapfile must not have holes");
10116 if (em
->block_start
== EXTENT_MAP_INLINE
) {
10118 * It's unlikely we'll ever actually find ourselves
10119 * here, as a file small enough to fit inline won't be
10120 * big enough to store more than the swap header, but in
10121 * case something changes in the future, let's catch it
10122 * here rather than later.
10124 btrfs_warn(fs_info
, "swapfile must not be inline");
10128 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
10129 btrfs_warn(fs_info
, "swapfile must not be compressed");
10134 logical_block_start
= em
->block_start
+ (start
- em
->start
);
10135 len
= min(len
, em
->len
- (start
- em
->start
));
10136 free_extent_map(em
);
10139 ret
= can_nocow_extent(inode
, start
, &len
, NULL
, NULL
, NULL
);
10145 btrfs_warn(fs_info
,
10146 "swapfile must not be copy-on-write");
10151 em
= btrfs_get_chunk_map(fs_info
, logical_block_start
, len
);
10157 if (em
->map_lookup
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
10158 btrfs_warn(fs_info
,
10159 "swapfile must have single data profile");
10164 if (device
== NULL
) {
10165 device
= em
->map_lookup
->stripes
[0].dev
;
10166 ret
= btrfs_add_swapfile_pin(inode
, device
, false);
10171 } else if (device
!= em
->map_lookup
->stripes
[0].dev
) {
10172 btrfs_warn(fs_info
, "swapfile must be on one device");
10177 physical_block_start
= (em
->map_lookup
->stripes
[0].physical
+
10178 (logical_block_start
- em
->start
));
10179 len
= min(len
, em
->len
- (logical_block_start
- em
->start
));
10180 free_extent_map(em
);
10183 bg
= btrfs_lookup_block_group(fs_info
, logical_block_start
);
10185 btrfs_warn(fs_info
,
10186 "could not find block group containing swapfile");
10191 ret
= btrfs_add_swapfile_pin(inode
, bg
, true);
10193 btrfs_put_block_group(bg
);
10200 if (bsi
.block_len
&&
10201 bsi
.block_start
+ bsi
.block_len
== physical_block_start
) {
10202 bsi
.block_len
+= len
;
10204 if (bsi
.block_len
) {
10205 ret
= btrfs_add_swap_extent(sis
, &bsi
);
10210 bsi
.block_start
= physical_block_start
;
10211 bsi
.block_len
= len
;
10218 ret
= btrfs_add_swap_extent(sis
, &bsi
);
10221 if (!IS_ERR_OR_NULL(em
))
10222 free_extent_map(em
);
10224 unlock_extent_cached(io_tree
, 0, isize
- 1, &cached_state
);
10227 btrfs_swap_deactivate(file
);
10229 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
10235 sis
->bdev
= device
->bdev
;
10236 *span
= bsi
.highest_ppage
- bsi
.lowest_ppage
+ 1;
10237 sis
->max
= bsi
.nr_pages
;
10238 sis
->pages
= bsi
.nr_pages
- 1;
10239 sis
->highest_bit
= bsi
.nr_pages
- 1;
10240 return bsi
.nr_extents
;
10243 static void btrfs_swap_deactivate(struct file
*file
)
10247 static int btrfs_swap_activate(struct swap_info_struct
*sis
, struct file
*file
,
10250 return -EOPNOTSUPP
;
10254 static const struct inode_operations btrfs_dir_inode_operations
= {
10255 .getattr
= btrfs_getattr
,
10256 .lookup
= btrfs_lookup
,
10257 .create
= btrfs_create
,
10258 .unlink
= btrfs_unlink
,
10259 .link
= btrfs_link
,
10260 .mkdir
= btrfs_mkdir
,
10261 .rmdir
= btrfs_rmdir
,
10262 .rename
= btrfs_rename2
,
10263 .symlink
= btrfs_symlink
,
10264 .setattr
= btrfs_setattr
,
10265 .mknod
= btrfs_mknod
,
10266 .listxattr
= btrfs_listxattr
,
10267 .permission
= btrfs_permission
,
10268 .get_acl
= btrfs_get_acl
,
10269 .set_acl
= btrfs_set_acl
,
10270 .update_time
= btrfs_update_time
,
10271 .tmpfile
= btrfs_tmpfile
,
10274 static const struct file_operations btrfs_dir_file_operations
= {
10275 .llseek
= generic_file_llseek
,
10276 .read
= generic_read_dir
,
10277 .iterate_shared
= btrfs_real_readdir
,
10278 .open
= btrfs_opendir
,
10279 .unlocked_ioctl
= btrfs_ioctl
,
10280 #ifdef CONFIG_COMPAT
10281 .compat_ioctl
= btrfs_compat_ioctl
,
10283 .release
= btrfs_release_file
,
10284 .fsync
= btrfs_sync_file
,
10287 static const struct extent_io_ops btrfs_extent_io_ops
= {
10288 /* mandatory callbacks */
10289 .submit_bio_hook
= btrfs_submit_bio_hook
,
10290 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
10294 * btrfs doesn't support the bmap operation because swapfiles
10295 * use bmap to make a mapping of extents in the file. They assume
10296 * these extents won't change over the life of the file and they
10297 * use the bmap result to do IO directly to the drive.
10299 * the btrfs bmap call would return logical addresses that aren't
10300 * suitable for IO and they also will change frequently as COW
10301 * operations happen. So, swapfile + btrfs == corruption.
10303 * For now we're avoiding this by dropping bmap.
10305 static const struct address_space_operations btrfs_aops
= {
10306 .readpage
= btrfs_readpage
,
10307 .writepage
= btrfs_writepage
,
10308 .writepages
= btrfs_writepages
,
10309 .readahead
= btrfs_readahead
,
10310 .direct_IO
= btrfs_direct_IO
,
10311 .invalidatepage
= btrfs_invalidatepage
,
10312 .releasepage
= btrfs_releasepage
,
10313 #ifdef CONFIG_MIGRATION
10314 .migratepage
= btrfs_migratepage
,
10316 .set_page_dirty
= btrfs_set_page_dirty
,
10317 .error_remove_page
= generic_error_remove_page
,
10318 .swap_activate
= btrfs_swap_activate
,
10319 .swap_deactivate
= btrfs_swap_deactivate
,
10322 static const struct inode_operations btrfs_file_inode_operations
= {
10323 .getattr
= btrfs_getattr
,
10324 .setattr
= btrfs_setattr
,
10325 .listxattr
= btrfs_listxattr
,
10326 .permission
= btrfs_permission
,
10327 .fiemap
= btrfs_fiemap
,
10328 .get_acl
= btrfs_get_acl
,
10329 .set_acl
= btrfs_set_acl
,
10330 .update_time
= btrfs_update_time
,
10332 static const struct inode_operations btrfs_special_inode_operations
= {
10333 .getattr
= btrfs_getattr
,
10334 .setattr
= btrfs_setattr
,
10335 .permission
= btrfs_permission
,
10336 .listxattr
= btrfs_listxattr
,
10337 .get_acl
= btrfs_get_acl
,
10338 .set_acl
= btrfs_set_acl
,
10339 .update_time
= btrfs_update_time
,
10341 static const struct inode_operations btrfs_symlink_inode_operations
= {
10342 .get_link
= page_get_link
,
10343 .getattr
= btrfs_getattr
,
10344 .setattr
= btrfs_setattr
,
10345 .permission
= btrfs_permission
,
10346 .listxattr
= btrfs_listxattr
,
10347 .update_time
= btrfs_update_time
,
10350 const struct dentry_operations btrfs_dentry_operations
= {
10351 .d_delete
= btrfs_dentry_delete
,