1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/kernel.h>
8 #include <linux/buffer_head.h>
9 #include <linux/file.h>
11 #include <linux/pagemap.h>
12 #include <linux/highmem.h>
13 #include <linux/time.h>
14 #include <linux/init.h>
15 #include <linux/string.h>
16 #include <linux/backing-dev.h>
17 #include <linux/writeback.h>
18 #include <linux/compat.h>
19 #include <linux/xattr.h>
20 #include <linux/posix_acl.h>
21 #include <linux/falloc.h>
22 #include <linux/slab.h>
23 #include <linux/ratelimit.h>
24 #include <linux/btrfs.h>
25 #include <linux/blkdev.h>
26 #include <linux/posix_acl_xattr.h>
27 #include <linux/uio.h>
28 #include <linux/magic.h>
29 #include <linux/iversion.h>
30 #include <linux/swap.h>
31 #include <asm/unaligned.h>
34 #include "transaction.h"
35 #include "btrfs_inode.h"
36 #include "print-tree.h"
37 #include "ordered-data.h"
41 #include "compression.h"
43 #include "free-space-cache.h"
44 #include "inode-map.h"
50 struct btrfs_iget_args
{
51 struct btrfs_key
*location
;
52 struct btrfs_root
*root
;
55 struct btrfs_dio_data
{
57 u64 unsubmitted_oe_range_start
;
58 u64 unsubmitted_oe_range_end
;
62 static const struct inode_operations btrfs_dir_inode_operations
;
63 static const struct inode_operations btrfs_symlink_inode_operations
;
64 static const struct inode_operations btrfs_dir_ro_inode_operations
;
65 static const struct inode_operations btrfs_special_inode_operations
;
66 static const struct inode_operations btrfs_file_inode_operations
;
67 static const struct address_space_operations btrfs_aops
;
68 static const struct file_operations btrfs_dir_file_operations
;
69 static const struct extent_io_ops btrfs_extent_io_ops
;
71 static struct kmem_cache
*btrfs_inode_cachep
;
72 struct kmem_cache
*btrfs_trans_handle_cachep
;
73 struct kmem_cache
*btrfs_path_cachep
;
74 struct kmem_cache
*btrfs_free_space_cachep
;
76 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
77 static int btrfs_truncate(struct inode
*inode
, bool skip_writeback
);
78 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
79 static noinline
int cow_file_range(struct inode
*inode
,
80 struct page
*locked_page
,
81 u64 start
, u64 end
, u64 delalloc_end
,
82 int *page_started
, unsigned long *nr_written
,
83 int unlock
, struct btrfs_dedupe_hash
*hash
);
84 static struct extent_map
*create_io_em(struct inode
*inode
, u64 start
, u64 len
,
85 u64 orig_start
, u64 block_start
,
86 u64 block_len
, u64 orig_block_len
,
87 u64 ram_bytes
, int compress_type
,
90 static void __endio_write_update_ordered(struct inode
*inode
,
91 const u64 offset
, const u64 bytes
,
95 * Cleanup all submitted ordered extents in specified range to handle errors
96 * from the btrfs_run_delalloc_range() callback.
98 * NOTE: caller must ensure that when an error happens, it can not call
99 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
100 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
101 * to be released, which we want to happen only when finishing the ordered
102 * extent (btrfs_finish_ordered_io()).
104 static inline void btrfs_cleanup_ordered_extents(struct inode
*inode
,
105 struct page
*locked_page
,
106 u64 offset
, u64 bytes
)
108 unsigned long index
= offset
>> PAGE_SHIFT
;
109 unsigned long end_index
= (offset
+ bytes
- 1) >> PAGE_SHIFT
;
110 u64 page_start
= page_offset(locked_page
);
111 u64 page_end
= page_start
+ PAGE_SIZE
- 1;
115 while (index
<= end_index
) {
116 page
= find_get_page(inode
->i_mapping
, index
);
120 ClearPagePrivate2(page
);
125 * In case this page belongs to the delalloc range being instantiated
126 * then skip it, since the first page of a range is going to be
127 * properly cleaned up by the caller of run_delalloc_range
129 if (page_start
>= offset
&& page_end
<= (offset
+ bytes
- 1)) {
134 return __endio_write_update_ordered(inode
, offset
, bytes
, false);
137 static int btrfs_dirty_inode(struct inode
*inode
);
139 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
140 void btrfs_test_inode_set_ops(struct inode
*inode
)
142 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
146 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
147 struct inode
*inode
, struct inode
*dir
,
148 const struct qstr
*qstr
)
152 err
= btrfs_init_acl(trans
, inode
, dir
);
154 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
159 * this does all the hard work for inserting an inline extent into
160 * the btree. The caller should have done a btrfs_drop_extents so that
161 * no overlapping inline items exist in the btree
163 static int insert_inline_extent(struct btrfs_trans_handle
*trans
,
164 struct btrfs_path
*path
, int extent_inserted
,
165 struct btrfs_root
*root
, struct inode
*inode
,
166 u64 start
, size_t size
, size_t compressed_size
,
168 struct page
**compressed_pages
)
170 struct extent_buffer
*leaf
;
171 struct page
*page
= NULL
;
174 struct btrfs_file_extent_item
*ei
;
176 size_t cur_size
= size
;
177 unsigned long offset
;
179 if (compressed_size
&& compressed_pages
)
180 cur_size
= compressed_size
;
182 inode_add_bytes(inode
, size
);
184 if (!extent_inserted
) {
185 struct btrfs_key key
;
188 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
190 key
.type
= BTRFS_EXTENT_DATA_KEY
;
192 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
193 path
->leave_spinning
= 1;
194 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
199 leaf
= path
->nodes
[0];
200 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
201 struct btrfs_file_extent_item
);
202 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
203 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
204 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
205 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
206 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
207 ptr
= btrfs_file_extent_inline_start(ei
);
209 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
212 while (compressed_size
> 0) {
213 cpage
= compressed_pages
[i
];
214 cur_size
= min_t(unsigned long, compressed_size
,
217 kaddr
= kmap_atomic(cpage
);
218 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
219 kunmap_atomic(kaddr
);
223 compressed_size
-= cur_size
;
225 btrfs_set_file_extent_compression(leaf
, ei
,
228 page
= find_get_page(inode
->i_mapping
,
229 start
>> PAGE_SHIFT
);
230 btrfs_set_file_extent_compression(leaf
, ei
, 0);
231 kaddr
= kmap_atomic(page
);
232 offset
= offset_in_page(start
);
233 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
234 kunmap_atomic(kaddr
);
237 btrfs_mark_buffer_dirty(leaf
);
238 btrfs_release_path(path
);
241 * we're an inline extent, so nobody can
242 * extend the file past i_size without locking
243 * a page we already have locked.
245 * We must do any isize and inode updates
246 * before we unlock the pages. Otherwise we
247 * could end up racing with unlink.
249 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
250 ret
= btrfs_update_inode(trans
, root
, inode
);
258 * conditionally insert an inline extent into the file. This
259 * does the checks required to make sure the data is small enough
260 * to fit as an inline extent.
262 static noinline
int cow_file_range_inline(struct inode
*inode
, u64 start
,
263 u64 end
, size_t compressed_size
,
265 struct page
**compressed_pages
)
267 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
268 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
269 struct btrfs_trans_handle
*trans
;
270 u64 isize
= i_size_read(inode
);
271 u64 actual_end
= min(end
+ 1, isize
);
272 u64 inline_len
= actual_end
- start
;
273 u64 aligned_end
= ALIGN(end
, fs_info
->sectorsize
);
274 u64 data_len
= inline_len
;
276 struct btrfs_path
*path
;
277 int extent_inserted
= 0;
278 u32 extent_item_size
;
281 data_len
= compressed_size
;
284 actual_end
> fs_info
->sectorsize
||
285 data_len
> BTRFS_MAX_INLINE_DATA_SIZE(fs_info
) ||
287 (actual_end
& (fs_info
->sectorsize
- 1)) == 0) ||
289 data_len
> fs_info
->max_inline
) {
293 path
= btrfs_alloc_path();
297 trans
= btrfs_join_transaction(root
);
299 btrfs_free_path(path
);
300 return PTR_ERR(trans
);
302 trans
->block_rsv
= &BTRFS_I(inode
)->block_rsv
;
304 if (compressed_size
&& compressed_pages
)
305 extent_item_size
= btrfs_file_extent_calc_inline_size(
308 extent_item_size
= btrfs_file_extent_calc_inline_size(
311 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
,
312 start
, aligned_end
, NULL
,
313 1, 1, extent_item_size
, &extent_inserted
);
315 btrfs_abort_transaction(trans
, ret
);
319 if (isize
> actual_end
)
320 inline_len
= min_t(u64
, isize
, actual_end
);
321 ret
= insert_inline_extent(trans
, path
, extent_inserted
,
323 inline_len
, compressed_size
,
324 compress_type
, compressed_pages
);
325 if (ret
&& ret
!= -ENOSPC
) {
326 btrfs_abort_transaction(trans
, ret
);
328 } else if (ret
== -ENOSPC
) {
333 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
334 btrfs_drop_extent_cache(BTRFS_I(inode
), start
, aligned_end
- 1, 0);
337 * Don't forget to free the reserved space, as for inlined extent
338 * it won't count as data extent, free them directly here.
339 * And at reserve time, it's always aligned to page size, so
340 * just free one page here.
342 btrfs_qgroup_free_data(inode
, NULL
, 0, PAGE_SIZE
);
343 btrfs_free_path(path
);
344 btrfs_end_transaction(trans
);
348 struct async_extent
{
353 unsigned long nr_pages
;
355 struct list_head list
;
360 struct btrfs_fs_info
*fs_info
;
361 struct page
*locked_page
;
364 unsigned int write_flags
;
365 struct list_head extents
;
366 struct btrfs_work work
;
369 static noinline
int add_async_extent(struct async_cow
*cow
,
370 u64 start
, u64 ram_size
,
373 unsigned long nr_pages
,
376 struct async_extent
*async_extent
;
378 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
379 BUG_ON(!async_extent
); /* -ENOMEM */
380 async_extent
->start
= start
;
381 async_extent
->ram_size
= ram_size
;
382 async_extent
->compressed_size
= compressed_size
;
383 async_extent
->pages
= pages
;
384 async_extent
->nr_pages
= nr_pages
;
385 async_extent
->compress_type
= compress_type
;
386 list_add_tail(&async_extent
->list
, &cow
->extents
);
390 static inline int inode_need_compress(struct inode
*inode
, u64 start
, u64 end
)
392 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
395 if (btrfs_test_opt(fs_info
, FORCE_COMPRESS
))
398 if (BTRFS_I(inode
)->defrag_compress
)
400 /* bad compression ratios */
401 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
403 if (btrfs_test_opt(fs_info
, COMPRESS
) ||
404 BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
||
405 BTRFS_I(inode
)->prop_compress
)
406 return btrfs_compress_heuristic(inode
, start
, end
);
410 static inline void inode_should_defrag(struct btrfs_inode
*inode
,
411 u64 start
, u64 end
, u64 num_bytes
, u64 small_write
)
413 /* If this is a small write inside eof, kick off a defrag */
414 if (num_bytes
< small_write
&&
415 (start
> 0 || end
+ 1 < inode
->disk_i_size
))
416 btrfs_add_inode_defrag(NULL
, inode
);
420 * we create compressed extents in two phases. The first
421 * phase compresses a range of pages that have already been
422 * locked (both pages and state bits are locked).
424 * This is done inside an ordered work queue, and the compression
425 * is spread across many cpus. The actual IO submission is step
426 * two, and the ordered work queue takes care of making sure that
427 * happens in the same order things were put onto the queue by
428 * writepages and friends.
430 * If this code finds it can't get good compression, it puts an
431 * entry onto the work queue to write the uncompressed bytes. This
432 * makes sure that both compressed inodes and uncompressed inodes
433 * are written in the same order that the flusher thread sent them
436 static noinline
void compress_file_range(struct inode
*inode
,
437 struct page
*locked_page
,
439 struct async_cow
*async_cow
,
442 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
443 u64 blocksize
= fs_info
->sectorsize
;
446 struct page
**pages
= NULL
;
447 unsigned long nr_pages
;
448 unsigned long total_compressed
= 0;
449 unsigned long total_in
= 0;
452 int compress_type
= fs_info
->compress_type
;
455 inode_should_defrag(BTRFS_I(inode
), start
, end
, end
- start
+ 1,
458 actual_end
= min_t(u64
, i_size_read(inode
), end
+ 1);
461 nr_pages
= (end
>> PAGE_SHIFT
) - (start
>> PAGE_SHIFT
) + 1;
462 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED
% PAGE_SIZE
) != 0);
463 nr_pages
= min_t(unsigned long, nr_pages
,
464 BTRFS_MAX_COMPRESSED
/ PAGE_SIZE
);
467 * we don't want to send crud past the end of i_size through
468 * compression, that's just a waste of CPU time. So, if the
469 * end of the file is before the start of our current
470 * requested range of bytes, we bail out to the uncompressed
471 * cleanup code that can deal with all of this.
473 * It isn't really the fastest way to fix things, but this is a
474 * very uncommon corner.
476 if (actual_end
<= start
)
477 goto cleanup_and_bail_uncompressed
;
479 total_compressed
= actual_end
- start
;
482 * skip compression for a small file range(<=blocksize) that
483 * isn't an inline extent, since it doesn't save disk space at all.
485 if (total_compressed
<= blocksize
&&
486 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
487 goto cleanup_and_bail_uncompressed
;
489 total_compressed
= min_t(unsigned long, total_compressed
,
490 BTRFS_MAX_UNCOMPRESSED
);
495 * we do compression for mount -o compress and when the
496 * inode has not been flagged as nocompress. This flag can
497 * change at any time if we discover bad compression ratios.
499 if (inode_need_compress(inode
, start
, end
)) {
501 pages
= kcalloc(nr_pages
, sizeof(struct page
*), GFP_NOFS
);
503 /* just bail out to the uncompressed code */
508 if (BTRFS_I(inode
)->defrag_compress
)
509 compress_type
= BTRFS_I(inode
)->defrag_compress
;
510 else if (BTRFS_I(inode
)->prop_compress
)
511 compress_type
= BTRFS_I(inode
)->prop_compress
;
514 * we need to call clear_page_dirty_for_io on each
515 * page in the range. Otherwise applications with the file
516 * mmap'd can wander in and change the page contents while
517 * we are compressing them.
519 * If the compression fails for any reason, we set the pages
520 * dirty again later on.
522 * Note that the remaining part is redirtied, the start pointer
523 * has moved, the end is the original one.
526 extent_range_clear_dirty_for_io(inode
, start
, end
);
530 /* Compression level is applied here and only here */
531 ret
= btrfs_compress_pages(
532 compress_type
| (fs_info
->compress_level
<< 4),
533 inode
->i_mapping
, start
,
540 unsigned long offset
= offset_in_page(total_compressed
);
541 struct page
*page
= pages
[nr_pages
- 1];
544 /* zero the tail end of the last page, we might be
545 * sending it down to disk
548 kaddr
= kmap_atomic(page
);
549 memset(kaddr
+ offset
, 0,
551 kunmap_atomic(kaddr
);
558 /* lets try to make an inline extent */
559 if (ret
|| total_in
< actual_end
) {
560 /* we didn't compress the entire range, try
561 * to make an uncompressed inline extent.
563 ret
= cow_file_range_inline(inode
, start
, end
, 0,
564 BTRFS_COMPRESS_NONE
, NULL
);
566 /* try making a compressed inline extent */
567 ret
= cow_file_range_inline(inode
, start
, end
,
569 compress_type
, pages
);
572 unsigned long clear_flags
= EXTENT_DELALLOC
|
573 EXTENT_DELALLOC_NEW
| EXTENT_DEFRAG
|
574 EXTENT_DO_ACCOUNTING
;
575 unsigned long page_error_op
;
577 page_error_op
= ret
< 0 ? PAGE_SET_ERROR
: 0;
580 * inline extent creation worked or returned error,
581 * we don't need to create any more async work items.
582 * Unlock and free up our temp pages.
584 * We use DO_ACCOUNTING here because we need the
585 * delalloc_release_metadata to be done _after_ we drop
586 * our outstanding extent for clearing delalloc for this
589 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
602 * we aren't doing an inline extent round the compressed size
603 * up to a block size boundary so the allocator does sane
606 total_compressed
= ALIGN(total_compressed
, blocksize
);
609 * one last check to make sure the compression is really a
610 * win, compare the page count read with the blocks on disk,
611 * compression must free at least one sector size
613 total_in
= ALIGN(total_in
, PAGE_SIZE
);
614 if (total_compressed
+ blocksize
<= total_in
) {
618 * The async work queues will take care of doing actual
619 * allocation on disk for these compressed pages, and
620 * will submit them to the elevator.
622 add_async_extent(async_cow
, start
, total_in
,
623 total_compressed
, pages
, nr_pages
,
626 if (start
+ total_in
< end
) {
637 * the compression code ran but failed to make things smaller,
638 * free any pages it allocated and our page pointer array
640 for (i
= 0; i
< nr_pages
; i
++) {
641 WARN_ON(pages
[i
]->mapping
);
646 total_compressed
= 0;
649 /* flag the file so we don't compress in the future */
650 if (!btrfs_test_opt(fs_info
, FORCE_COMPRESS
) &&
651 !(BTRFS_I(inode
)->prop_compress
)) {
652 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
655 cleanup_and_bail_uncompressed
:
657 * No compression, but we still need to write the pages in the file
658 * we've been given so far. redirty the locked page if it corresponds
659 * to our extent and set things up for the async work queue to run
660 * cow_file_range to do the normal delalloc dance.
662 if (page_offset(locked_page
) >= start
&&
663 page_offset(locked_page
) <= end
)
664 __set_page_dirty_nobuffers(locked_page
);
665 /* unlocked later on in the async handlers */
668 extent_range_redirty_for_io(inode
, start
, end
);
669 add_async_extent(async_cow
, start
, end
- start
+ 1, 0, NULL
, 0,
670 BTRFS_COMPRESS_NONE
);
676 for (i
= 0; i
< nr_pages
; i
++) {
677 WARN_ON(pages
[i
]->mapping
);
683 static void free_async_extent_pages(struct async_extent
*async_extent
)
687 if (!async_extent
->pages
)
690 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
691 WARN_ON(async_extent
->pages
[i
]->mapping
);
692 put_page(async_extent
->pages
[i
]);
694 kfree(async_extent
->pages
);
695 async_extent
->nr_pages
= 0;
696 async_extent
->pages
= NULL
;
700 * phase two of compressed writeback. This is the ordered portion
701 * of the code, which only gets called in the order the work was
702 * queued. We walk all the async extents created by compress_file_range
703 * and send them down to the disk.
705 static noinline
void submit_compressed_extents(struct async_cow
*async_cow
)
707 struct inode
*inode
= async_cow
->inode
;
708 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
709 struct async_extent
*async_extent
;
711 struct btrfs_key ins
;
712 struct extent_map
*em
;
713 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
714 struct extent_io_tree
*io_tree
;
718 while (!list_empty(&async_cow
->extents
)) {
719 async_extent
= list_entry(async_cow
->extents
.next
,
720 struct async_extent
, list
);
721 list_del(&async_extent
->list
);
723 io_tree
= &BTRFS_I(inode
)->io_tree
;
726 /* did the compression code fall back to uncompressed IO? */
727 if (!async_extent
->pages
) {
728 int page_started
= 0;
729 unsigned long nr_written
= 0;
731 lock_extent(io_tree
, async_extent
->start
,
732 async_extent
->start
+
733 async_extent
->ram_size
- 1);
735 /* allocate blocks */
736 ret
= cow_file_range(inode
, async_cow
->locked_page
,
738 async_extent
->start
+
739 async_extent
->ram_size
- 1,
740 async_extent
->start
+
741 async_extent
->ram_size
- 1,
742 &page_started
, &nr_written
, 0,
748 * if page_started, cow_file_range inserted an
749 * inline extent and took care of all the unlocking
750 * and IO for us. Otherwise, we need to submit
751 * all those pages down to the drive.
753 if (!page_started
&& !ret
)
754 extent_write_locked_range(inode
,
756 async_extent
->start
+
757 async_extent
->ram_size
- 1,
760 unlock_page(async_cow
->locked_page
);
766 lock_extent(io_tree
, async_extent
->start
,
767 async_extent
->start
+ async_extent
->ram_size
- 1);
769 ret
= btrfs_reserve_extent(root
, async_extent
->ram_size
,
770 async_extent
->compressed_size
,
771 async_extent
->compressed_size
,
772 0, alloc_hint
, &ins
, 1, 1);
774 free_async_extent_pages(async_extent
);
776 if (ret
== -ENOSPC
) {
777 unlock_extent(io_tree
, async_extent
->start
,
778 async_extent
->start
+
779 async_extent
->ram_size
- 1);
782 * we need to redirty the pages if we decide to
783 * fallback to uncompressed IO, otherwise we
784 * will not submit these pages down to lower
787 extent_range_redirty_for_io(inode
,
789 async_extent
->start
+
790 async_extent
->ram_size
- 1);
797 * here we're doing allocation and writeback of the
800 em
= create_io_em(inode
, async_extent
->start
,
801 async_extent
->ram_size
, /* len */
802 async_extent
->start
, /* orig_start */
803 ins
.objectid
, /* block_start */
804 ins
.offset
, /* block_len */
805 ins
.offset
, /* orig_block_len */
806 async_extent
->ram_size
, /* ram_bytes */
807 async_extent
->compress_type
,
808 BTRFS_ORDERED_COMPRESSED
);
810 /* ret value is not necessary due to void function */
811 goto out_free_reserve
;
814 ret
= btrfs_add_ordered_extent_compress(inode
,
817 async_extent
->ram_size
,
819 BTRFS_ORDERED_COMPRESSED
,
820 async_extent
->compress_type
);
822 btrfs_drop_extent_cache(BTRFS_I(inode
),
824 async_extent
->start
+
825 async_extent
->ram_size
- 1, 0);
826 goto out_free_reserve
;
828 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
831 * clear dirty, set writeback and unlock the pages.
833 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
834 async_extent
->start
+
835 async_extent
->ram_size
- 1,
836 async_extent
->start
+
837 async_extent
->ram_size
- 1,
838 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
,
839 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
841 if (btrfs_submit_compressed_write(inode
,
843 async_extent
->ram_size
,
845 ins
.offset
, async_extent
->pages
,
846 async_extent
->nr_pages
,
847 async_cow
->write_flags
)) {
848 struct page
*p
= async_extent
->pages
[0];
849 const u64 start
= async_extent
->start
;
850 const u64 end
= start
+ async_extent
->ram_size
- 1;
852 p
->mapping
= inode
->i_mapping
;
853 btrfs_writepage_endio_finish_ordered(p
, start
, end
, 0);
856 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
860 free_async_extent_pages(async_extent
);
862 alloc_hint
= ins
.objectid
+ ins
.offset
;
868 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
869 btrfs_free_reserved_extent(fs_info
, ins
.objectid
, ins
.offset
, 1);
871 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
872 async_extent
->start
+
873 async_extent
->ram_size
- 1,
874 async_extent
->start
+
875 async_extent
->ram_size
- 1,
876 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
|
877 EXTENT_DELALLOC_NEW
|
878 EXTENT_DEFRAG
| EXTENT_DO_ACCOUNTING
,
879 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
880 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
|
882 free_async_extent_pages(async_extent
);
887 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
890 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
891 struct extent_map
*em
;
894 read_lock(&em_tree
->lock
);
895 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
898 * if block start isn't an actual block number then find the
899 * first block in this inode and use that as a hint. If that
900 * block is also bogus then just don't worry about it.
902 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
904 em
= search_extent_mapping(em_tree
, 0, 0);
905 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
906 alloc_hint
= em
->block_start
;
910 alloc_hint
= em
->block_start
;
914 read_unlock(&em_tree
->lock
);
920 * when extent_io.c finds a delayed allocation range in the file,
921 * the call backs end up in this code. The basic idea is to
922 * allocate extents on disk for the range, and create ordered data structs
923 * in ram to track those extents.
925 * locked_page is the page that writepage had locked already. We use
926 * it to make sure we don't do extra locks or unlocks.
928 * *page_started is set to one if we unlock locked_page and do everything
929 * required to start IO on it. It may be clean and already done with
932 static noinline
int cow_file_range(struct inode
*inode
,
933 struct page
*locked_page
,
934 u64 start
, u64 end
, u64 delalloc_end
,
935 int *page_started
, unsigned long *nr_written
,
936 int unlock
, struct btrfs_dedupe_hash
*hash
)
938 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
939 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
942 unsigned long ram_size
;
943 u64 cur_alloc_size
= 0;
944 u64 blocksize
= fs_info
->sectorsize
;
945 struct btrfs_key ins
;
946 struct extent_map
*em
;
948 unsigned long page_ops
;
949 bool extent_reserved
= false;
952 if (btrfs_is_free_space_inode(BTRFS_I(inode
))) {
958 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
959 num_bytes
= max(blocksize
, num_bytes
);
960 ASSERT(num_bytes
<= btrfs_super_total_bytes(fs_info
->super_copy
));
962 inode_should_defrag(BTRFS_I(inode
), start
, end
, num_bytes
, SZ_64K
);
965 /* lets try to make an inline extent */
966 ret
= cow_file_range_inline(inode
, start
, end
, 0,
967 BTRFS_COMPRESS_NONE
, NULL
);
970 * We use DO_ACCOUNTING here because we need the
971 * delalloc_release_metadata to be run _after_ we drop
972 * our outstanding extent for clearing delalloc for this
975 extent_clear_unlock_delalloc(inode
, start
, end
,
977 EXTENT_LOCKED
| EXTENT_DELALLOC
|
978 EXTENT_DELALLOC_NEW
| EXTENT_DEFRAG
|
979 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
980 PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
982 *nr_written
= *nr_written
+
983 (end
- start
+ PAGE_SIZE
) / PAGE_SIZE
;
986 } else if (ret
< 0) {
991 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
992 btrfs_drop_extent_cache(BTRFS_I(inode
), start
,
993 start
+ num_bytes
- 1, 0);
995 while (num_bytes
> 0) {
996 cur_alloc_size
= num_bytes
;
997 ret
= btrfs_reserve_extent(root
, cur_alloc_size
, cur_alloc_size
,
998 fs_info
->sectorsize
, 0, alloc_hint
,
1002 cur_alloc_size
= ins
.offset
;
1003 extent_reserved
= true;
1005 ram_size
= ins
.offset
;
1006 em
= create_io_em(inode
, start
, ins
.offset
, /* len */
1007 start
, /* orig_start */
1008 ins
.objectid
, /* block_start */
1009 ins
.offset
, /* block_len */
1010 ins
.offset
, /* orig_block_len */
1011 ram_size
, /* ram_bytes */
1012 BTRFS_COMPRESS_NONE
, /* compress_type */
1013 BTRFS_ORDERED_REGULAR
/* type */);
1018 free_extent_map(em
);
1020 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
1021 ram_size
, cur_alloc_size
, 0);
1023 goto out_drop_extent_cache
;
1025 if (root
->root_key
.objectid
==
1026 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1027 ret
= btrfs_reloc_clone_csums(inode
, start
,
1030 * Only drop cache here, and process as normal.
1032 * We must not allow extent_clear_unlock_delalloc()
1033 * at out_unlock label to free meta of this ordered
1034 * extent, as its meta should be freed by
1035 * btrfs_finish_ordered_io().
1037 * So we must continue until @start is increased to
1038 * skip current ordered extent.
1041 btrfs_drop_extent_cache(BTRFS_I(inode
), start
,
1042 start
+ ram_size
- 1, 0);
1045 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
1047 /* we're not doing compressed IO, don't unlock the first
1048 * page (which the caller expects to stay locked), don't
1049 * clear any dirty bits and don't set any writeback bits
1051 * Do set the Private2 bit so we know this page was properly
1052 * setup for writepage
1054 page_ops
= unlock
? PAGE_UNLOCK
: 0;
1055 page_ops
|= PAGE_SET_PRIVATE2
;
1057 extent_clear_unlock_delalloc(inode
, start
,
1058 start
+ ram_size
- 1,
1059 delalloc_end
, locked_page
,
1060 EXTENT_LOCKED
| EXTENT_DELALLOC
,
1062 if (num_bytes
< cur_alloc_size
)
1065 num_bytes
-= cur_alloc_size
;
1066 alloc_hint
= ins
.objectid
+ ins
.offset
;
1067 start
+= cur_alloc_size
;
1068 extent_reserved
= false;
1071 * btrfs_reloc_clone_csums() error, since start is increased
1072 * extent_clear_unlock_delalloc() at out_unlock label won't
1073 * free metadata of current ordered extent, we're OK to exit.
1081 out_drop_extent_cache
:
1082 btrfs_drop_extent_cache(BTRFS_I(inode
), start
, start
+ ram_size
- 1, 0);
1084 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
1085 btrfs_free_reserved_extent(fs_info
, ins
.objectid
, ins
.offset
, 1);
1087 clear_bits
= EXTENT_LOCKED
| EXTENT_DELALLOC
| EXTENT_DELALLOC_NEW
|
1088 EXTENT_DEFRAG
| EXTENT_CLEAR_META_RESV
;
1089 page_ops
= PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
1092 * If we reserved an extent for our delalloc range (or a subrange) and
1093 * failed to create the respective ordered extent, then it means that
1094 * when we reserved the extent we decremented the extent's size from
1095 * the data space_info's bytes_may_use counter and incremented the
1096 * space_info's bytes_reserved counter by the same amount. We must make
1097 * sure extent_clear_unlock_delalloc() does not try to decrement again
1098 * the data space_info's bytes_may_use counter, therefore we do not pass
1099 * it the flag EXTENT_CLEAR_DATA_RESV.
1101 if (extent_reserved
) {
1102 extent_clear_unlock_delalloc(inode
, start
,
1103 start
+ cur_alloc_size
,
1104 start
+ cur_alloc_size
,
1108 start
+= cur_alloc_size
;
1112 extent_clear_unlock_delalloc(inode
, start
, end
, delalloc_end
,
1114 clear_bits
| EXTENT_CLEAR_DATA_RESV
,
1120 * work queue call back to started compression on a file and pages
1122 static noinline
void async_cow_start(struct btrfs_work
*work
)
1124 struct async_cow
*async_cow
;
1126 async_cow
= container_of(work
, struct async_cow
, work
);
1128 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1129 async_cow
->start
, async_cow
->end
, async_cow
,
1131 if (num_added
== 0) {
1132 btrfs_add_delayed_iput(async_cow
->inode
);
1133 async_cow
->inode
= NULL
;
1138 * work queue call back to submit previously compressed pages
1140 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1142 struct btrfs_fs_info
*fs_info
;
1143 struct async_cow
*async_cow
;
1144 unsigned long nr_pages
;
1146 async_cow
= container_of(work
, struct async_cow
, work
);
1148 fs_info
= async_cow
->fs_info
;
1149 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_SIZE
) >>
1152 /* atomic_sub_return implies a barrier */
1153 if (atomic_sub_return(nr_pages
, &fs_info
->async_delalloc_pages
) <
1155 cond_wake_up_nomb(&fs_info
->async_submit_wait
);
1158 * ->inode could be NULL if async_cow_start has failed to compress,
1159 * in which case we don't have anything to submit, yet we need to
1160 * always adjust ->async_delalloc_pages as its paired with the init
1161 * happening in cow_file_range_async
1163 if (async_cow
->inode
)
1164 submit_compressed_extents(async_cow
);
1167 static noinline
void async_cow_free(struct btrfs_work
*work
)
1169 struct async_cow
*async_cow
;
1170 async_cow
= container_of(work
, struct async_cow
, work
);
1171 if (async_cow
->inode
)
1172 btrfs_add_delayed_iput(async_cow
->inode
);
1176 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1177 u64 start
, u64 end
, int *page_started
,
1178 unsigned long *nr_written
,
1179 unsigned int write_flags
)
1181 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1182 struct async_cow
*async_cow
;
1183 unsigned long nr_pages
;
1186 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1188 while (start
< end
) {
1189 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1190 BUG_ON(!async_cow
); /* -ENOMEM */
1192 * igrab is called higher up in the call chain, take only the
1193 * lightweight reference for the callback lifetime
1196 async_cow
->inode
= inode
;
1197 async_cow
->fs_info
= fs_info
;
1198 async_cow
->locked_page
= locked_page
;
1199 async_cow
->start
= start
;
1200 async_cow
->write_flags
= write_flags
;
1202 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
&&
1203 !btrfs_test_opt(fs_info
, FORCE_COMPRESS
))
1206 cur_end
= min(end
, start
+ SZ_512K
- 1);
1208 async_cow
->end
= cur_end
;
1209 INIT_LIST_HEAD(&async_cow
->extents
);
1211 btrfs_init_work(&async_cow
->work
,
1212 btrfs_delalloc_helper
,
1213 async_cow_start
, async_cow_submit
,
1216 nr_pages
= (cur_end
- start
+ PAGE_SIZE
) >>
1218 atomic_add(nr_pages
, &fs_info
->async_delalloc_pages
);
1220 btrfs_queue_work(fs_info
->delalloc_workers
, &async_cow
->work
);
1222 *nr_written
+= nr_pages
;
1223 start
= cur_end
+ 1;
1229 static noinline
int csum_exist_in_range(struct btrfs_fs_info
*fs_info
,
1230 u64 bytenr
, u64 num_bytes
)
1233 struct btrfs_ordered_sum
*sums
;
1236 ret
= btrfs_lookup_csums_range(fs_info
->csum_root
, bytenr
,
1237 bytenr
+ num_bytes
- 1, &list
, 0);
1238 if (ret
== 0 && list_empty(&list
))
1241 while (!list_empty(&list
)) {
1242 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1243 list_del(&sums
->list
);
1252 * when nowcow writeback call back. This checks for snapshots or COW copies
1253 * of the extents that exist in the file, and COWs the file as required.
1255 * If no cow copies or snapshots exist, we write directly to the existing
1258 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1259 struct page
*locked_page
,
1260 u64 start
, u64 end
, int *page_started
, int force
,
1261 unsigned long *nr_written
)
1263 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1264 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1265 struct extent_buffer
*leaf
;
1266 struct btrfs_path
*path
;
1267 struct btrfs_file_extent_item
*fi
;
1268 struct btrfs_key found_key
;
1269 struct extent_map
*em
;
1284 u64 ino
= btrfs_ino(BTRFS_I(inode
));
1286 path
= btrfs_alloc_path();
1288 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
1290 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1291 EXTENT_DO_ACCOUNTING
|
1292 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1294 PAGE_SET_WRITEBACK
|
1295 PAGE_END_WRITEBACK
);
1299 nolock
= btrfs_is_free_space_inode(BTRFS_I(inode
));
1301 cow_start
= (u64
)-1;
1304 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, ino
,
1308 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1309 leaf
= path
->nodes
[0];
1310 btrfs_item_key_to_cpu(leaf
, &found_key
,
1311 path
->slots
[0] - 1);
1312 if (found_key
.objectid
== ino
&&
1313 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1318 leaf
= path
->nodes
[0];
1319 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1320 ret
= btrfs_next_leaf(root
, path
);
1322 if (cow_start
!= (u64
)-1)
1323 cur_offset
= cow_start
;
1328 leaf
= path
->nodes
[0];
1334 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1336 if (found_key
.objectid
> ino
)
1338 if (WARN_ON_ONCE(found_key
.objectid
< ino
) ||
1339 found_key
.type
< BTRFS_EXTENT_DATA_KEY
) {
1343 if (found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1344 found_key
.offset
> end
)
1347 if (found_key
.offset
> cur_offset
) {
1348 extent_end
= found_key
.offset
;
1353 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1354 struct btrfs_file_extent_item
);
1355 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1357 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1358 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1359 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1360 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1361 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1362 extent_end
= found_key
.offset
+
1363 btrfs_file_extent_num_bytes(leaf
, fi
);
1365 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1366 if (extent_end
<= start
) {
1370 if (disk_bytenr
== 0)
1372 if (btrfs_file_extent_compression(leaf
, fi
) ||
1373 btrfs_file_extent_encryption(leaf
, fi
) ||
1374 btrfs_file_extent_other_encoding(leaf
, fi
))
1377 * Do the same check as in btrfs_cross_ref_exist but
1378 * without the unnecessary search.
1381 btrfs_file_extent_generation(leaf
, fi
) <=
1382 btrfs_root_last_snapshot(&root
->root_item
))
1384 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1386 if (btrfs_extent_readonly(fs_info
, disk_bytenr
))
1388 ret
= btrfs_cross_ref_exist(root
, ino
,
1390 extent_offset
, disk_bytenr
);
1393 * ret could be -EIO if the above fails to read
1397 if (cow_start
!= (u64
)-1)
1398 cur_offset
= cow_start
;
1402 WARN_ON_ONCE(nolock
);
1405 disk_bytenr
+= extent_offset
;
1406 disk_bytenr
+= cur_offset
- found_key
.offset
;
1407 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1409 * if there are pending snapshots for this root,
1410 * we fall into common COW way.
1412 if (!nolock
&& atomic_read(&root
->snapshot_force_cow
))
1415 * force cow if csum exists in the range.
1416 * this ensure that csum for a given extent are
1417 * either valid or do not exist.
1419 ret
= csum_exist_in_range(fs_info
, disk_bytenr
,
1423 * ret could be -EIO if the above fails to read
1427 if (cow_start
!= (u64
)-1)
1428 cur_offset
= cow_start
;
1431 WARN_ON_ONCE(nolock
);
1434 if (!btrfs_inc_nocow_writers(fs_info
, disk_bytenr
))
1437 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1438 extent_end
= found_key
.offset
+
1439 btrfs_file_extent_ram_bytes(leaf
, fi
);
1440 extent_end
= ALIGN(extent_end
,
1441 fs_info
->sectorsize
);
1446 if (extent_end
<= start
) {
1449 btrfs_dec_nocow_writers(fs_info
, disk_bytenr
);
1453 if (cow_start
== (u64
)-1)
1454 cow_start
= cur_offset
;
1455 cur_offset
= extent_end
;
1456 if (cur_offset
> end
)
1462 btrfs_release_path(path
);
1463 if (cow_start
!= (u64
)-1) {
1464 ret
= cow_file_range(inode
, locked_page
,
1465 cow_start
, found_key
.offset
- 1,
1466 end
, page_started
, nr_written
, 1,
1470 btrfs_dec_nocow_writers(fs_info
,
1474 cow_start
= (u64
)-1;
1477 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1478 u64 orig_start
= found_key
.offset
- extent_offset
;
1480 em
= create_io_em(inode
, cur_offset
, num_bytes
,
1482 disk_bytenr
, /* block_start */
1483 num_bytes
, /* block_len */
1484 disk_num_bytes
, /* orig_block_len */
1485 ram_bytes
, BTRFS_COMPRESS_NONE
,
1486 BTRFS_ORDERED_PREALLOC
);
1489 btrfs_dec_nocow_writers(fs_info
,
1494 free_extent_map(em
);
1497 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1498 type
= BTRFS_ORDERED_PREALLOC
;
1500 type
= BTRFS_ORDERED_NOCOW
;
1503 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1504 num_bytes
, num_bytes
, type
);
1506 btrfs_dec_nocow_writers(fs_info
, disk_bytenr
);
1507 BUG_ON(ret
); /* -ENOMEM */
1509 if (root
->root_key
.objectid
==
1510 BTRFS_DATA_RELOC_TREE_OBJECTID
)
1512 * Error handled later, as we must prevent
1513 * extent_clear_unlock_delalloc() in error handler
1514 * from freeing metadata of created ordered extent.
1516 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1519 extent_clear_unlock_delalloc(inode
, cur_offset
,
1520 cur_offset
+ num_bytes
- 1, end
,
1521 locked_page
, EXTENT_LOCKED
|
1523 EXTENT_CLEAR_DATA_RESV
,
1524 PAGE_UNLOCK
| PAGE_SET_PRIVATE2
);
1526 cur_offset
= extent_end
;
1529 * btrfs_reloc_clone_csums() error, now we're OK to call error
1530 * handler, as metadata for created ordered extent will only
1531 * be freed by btrfs_finish_ordered_io().
1535 if (cur_offset
> end
)
1538 btrfs_release_path(path
);
1540 if (cur_offset
<= end
&& cow_start
== (u64
)-1)
1541 cow_start
= cur_offset
;
1543 if (cow_start
!= (u64
)-1) {
1545 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
, end
,
1546 page_started
, nr_written
, 1, NULL
);
1552 if (ret
&& cur_offset
< end
)
1553 extent_clear_unlock_delalloc(inode
, cur_offset
, end
, end
,
1554 locked_page
, EXTENT_LOCKED
|
1555 EXTENT_DELALLOC
| EXTENT_DEFRAG
|
1556 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1558 PAGE_SET_WRITEBACK
|
1559 PAGE_END_WRITEBACK
);
1560 btrfs_free_path(path
);
1564 static inline int need_force_cow(struct inode
*inode
, u64 start
, u64 end
)
1567 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
1568 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
))
1572 * @defrag_bytes is a hint value, no spinlock held here,
1573 * if is not zero, it means the file is defragging.
1574 * Force cow if given extent needs to be defragged.
1576 if (BTRFS_I(inode
)->defrag_bytes
&&
1577 test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
1578 EXTENT_DEFRAG
, 0, NULL
))
1585 * Function to process delayed allocation (create CoW) for ranges which are
1586 * being touched for the first time.
1588 int btrfs_run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1589 u64 start
, u64 end
, int *page_started
, unsigned long *nr_written
,
1590 struct writeback_control
*wbc
)
1593 int force_cow
= need_force_cow(inode
, start
, end
);
1594 unsigned int write_flags
= wbc_to_write_flags(wbc
);
1596 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
&& !force_cow
) {
1597 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1598 page_started
, 1, nr_written
);
1599 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
&& !force_cow
) {
1600 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1601 page_started
, 0, nr_written
);
1602 } else if (!inode_need_compress(inode
, start
, end
)) {
1603 ret
= cow_file_range(inode
, locked_page
, start
, end
, end
,
1604 page_started
, nr_written
, 1, NULL
);
1606 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1607 &BTRFS_I(inode
)->runtime_flags
);
1608 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1609 page_started
, nr_written
,
1613 btrfs_cleanup_ordered_extents(inode
, locked_page
, start
,
1618 void btrfs_split_delalloc_extent(struct inode
*inode
,
1619 struct extent_state
*orig
, u64 split
)
1623 /* not delalloc, ignore it */
1624 if (!(orig
->state
& EXTENT_DELALLOC
))
1627 size
= orig
->end
- orig
->start
+ 1;
1628 if (size
> BTRFS_MAX_EXTENT_SIZE
) {
1633 * See the explanation in btrfs_merge_delalloc_extent, the same
1634 * applies here, just in reverse.
1636 new_size
= orig
->end
- split
+ 1;
1637 num_extents
= count_max_extents(new_size
);
1638 new_size
= split
- orig
->start
;
1639 num_extents
+= count_max_extents(new_size
);
1640 if (count_max_extents(size
) >= num_extents
)
1644 spin_lock(&BTRFS_I(inode
)->lock
);
1645 btrfs_mod_outstanding_extents(BTRFS_I(inode
), 1);
1646 spin_unlock(&BTRFS_I(inode
)->lock
);
1650 * Handle merged delayed allocation extents so we can keep track of new extents
1651 * that are just merged onto old extents, such as when we are doing sequential
1652 * writes, so we can properly account for the metadata space we'll need.
1654 void btrfs_merge_delalloc_extent(struct inode
*inode
, struct extent_state
*new,
1655 struct extent_state
*other
)
1657 u64 new_size
, old_size
;
1660 /* not delalloc, ignore it */
1661 if (!(other
->state
& EXTENT_DELALLOC
))
1664 if (new->start
> other
->start
)
1665 new_size
= new->end
- other
->start
+ 1;
1667 new_size
= other
->end
- new->start
+ 1;
1669 /* we're not bigger than the max, unreserve the space and go */
1670 if (new_size
<= BTRFS_MAX_EXTENT_SIZE
) {
1671 spin_lock(&BTRFS_I(inode
)->lock
);
1672 btrfs_mod_outstanding_extents(BTRFS_I(inode
), -1);
1673 spin_unlock(&BTRFS_I(inode
)->lock
);
1678 * We have to add up either side to figure out how many extents were
1679 * accounted for before we merged into one big extent. If the number of
1680 * extents we accounted for is <= the amount we need for the new range
1681 * then we can return, otherwise drop. Think of it like this
1685 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1686 * need 2 outstanding extents, on one side we have 1 and the other side
1687 * we have 1 so they are == and we can return. But in this case
1689 * [MAX_SIZE+4k][MAX_SIZE+4k]
1691 * Each range on their own accounts for 2 extents, but merged together
1692 * they are only 3 extents worth of accounting, so we need to drop in
1695 old_size
= other
->end
- other
->start
+ 1;
1696 num_extents
= count_max_extents(old_size
);
1697 old_size
= new->end
- new->start
+ 1;
1698 num_extents
+= count_max_extents(old_size
);
1699 if (count_max_extents(new_size
) >= num_extents
)
1702 spin_lock(&BTRFS_I(inode
)->lock
);
1703 btrfs_mod_outstanding_extents(BTRFS_I(inode
), -1);
1704 spin_unlock(&BTRFS_I(inode
)->lock
);
1707 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1708 struct inode
*inode
)
1710 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1712 spin_lock(&root
->delalloc_lock
);
1713 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1714 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1715 &root
->delalloc_inodes
);
1716 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1717 &BTRFS_I(inode
)->runtime_flags
);
1718 root
->nr_delalloc_inodes
++;
1719 if (root
->nr_delalloc_inodes
== 1) {
1720 spin_lock(&fs_info
->delalloc_root_lock
);
1721 BUG_ON(!list_empty(&root
->delalloc_root
));
1722 list_add_tail(&root
->delalloc_root
,
1723 &fs_info
->delalloc_roots
);
1724 spin_unlock(&fs_info
->delalloc_root_lock
);
1727 spin_unlock(&root
->delalloc_lock
);
1731 void __btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1732 struct btrfs_inode
*inode
)
1734 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
1736 if (!list_empty(&inode
->delalloc_inodes
)) {
1737 list_del_init(&inode
->delalloc_inodes
);
1738 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1739 &inode
->runtime_flags
);
1740 root
->nr_delalloc_inodes
--;
1741 if (!root
->nr_delalloc_inodes
) {
1742 ASSERT(list_empty(&root
->delalloc_inodes
));
1743 spin_lock(&fs_info
->delalloc_root_lock
);
1744 BUG_ON(list_empty(&root
->delalloc_root
));
1745 list_del_init(&root
->delalloc_root
);
1746 spin_unlock(&fs_info
->delalloc_root_lock
);
1751 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1752 struct btrfs_inode
*inode
)
1754 spin_lock(&root
->delalloc_lock
);
1755 __btrfs_del_delalloc_inode(root
, inode
);
1756 spin_unlock(&root
->delalloc_lock
);
1760 * Properly track delayed allocation bytes in the inode and to maintain the
1761 * list of inodes that have pending delalloc work to be done.
1763 void btrfs_set_delalloc_extent(struct inode
*inode
, struct extent_state
*state
,
1766 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1768 if ((*bits
& EXTENT_DEFRAG
) && !(*bits
& EXTENT_DELALLOC
))
1771 * set_bit and clear bit hooks normally require _irqsave/restore
1772 * but in this case, we are only testing for the DELALLOC
1773 * bit, which is only set or cleared with irqs on
1775 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1776 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1777 u64 len
= state
->end
+ 1 - state
->start
;
1778 u32 num_extents
= count_max_extents(len
);
1779 bool do_list
= !btrfs_is_free_space_inode(BTRFS_I(inode
));
1781 spin_lock(&BTRFS_I(inode
)->lock
);
1782 btrfs_mod_outstanding_extents(BTRFS_I(inode
), num_extents
);
1783 spin_unlock(&BTRFS_I(inode
)->lock
);
1785 /* For sanity tests */
1786 if (btrfs_is_testing(fs_info
))
1789 percpu_counter_add_batch(&fs_info
->delalloc_bytes
, len
,
1790 fs_info
->delalloc_batch
);
1791 spin_lock(&BTRFS_I(inode
)->lock
);
1792 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1793 if (*bits
& EXTENT_DEFRAG
)
1794 BTRFS_I(inode
)->defrag_bytes
+= len
;
1795 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1796 &BTRFS_I(inode
)->runtime_flags
))
1797 btrfs_add_delalloc_inodes(root
, inode
);
1798 spin_unlock(&BTRFS_I(inode
)->lock
);
1801 if (!(state
->state
& EXTENT_DELALLOC_NEW
) &&
1802 (*bits
& EXTENT_DELALLOC_NEW
)) {
1803 spin_lock(&BTRFS_I(inode
)->lock
);
1804 BTRFS_I(inode
)->new_delalloc_bytes
+= state
->end
+ 1 -
1806 spin_unlock(&BTRFS_I(inode
)->lock
);
1811 * Once a range is no longer delalloc this function ensures that proper
1812 * accounting happens.
1814 void btrfs_clear_delalloc_extent(struct inode
*vfs_inode
,
1815 struct extent_state
*state
, unsigned *bits
)
1817 struct btrfs_inode
*inode
= BTRFS_I(vfs_inode
);
1818 struct btrfs_fs_info
*fs_info
= btrfs_sb(vfs_inode
->i_sb
);
1819 u64 len
= state
->end
+ 1 - state
->start
;
1820 u32 num_extents
= count_max_extents(len
);
1822 if ((state
->state
& EXTENT_DEFRAG
) && (*bits
& EXTENT_DEFRAG
)) {
1823 spin_lock(&inode
->lock
);
1824 inode
->defrag_bytes
-= len
;
1825 spin_unlock(&inode
->lock
);
1829 * set_bit and clear bit hooks normally require _irqsave/restore
1830 * but in this case, we are only testing for the DELALLOC
1831 * bit, which is only set or cleared with irqs on
1833 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1834 struct btrfs_root
*root
= inode
->root
;
1835 bool do_list
= !btrfs_is_free_space_inode(inode
);
1837 spin_lock(&inode
->lock
);
1838 btrfs_mod_outstanding_extents(inode
, -num_extents
);
1839 spin_unlock(&inode
->lock
);
1842 * We don't reserve metadata space for space cache inodes so we
1843 * don't need to call delalloc_release_metadata if there is an
1846 if (*bits
& EXTENT_CLEAR_META_RESV
&&
1847 root
!= fs_info
->tree_root
)
1848 btrfs_delalloc_release_metadata(inode
, len
, false);
1850 /* For sanity tests. */
1851 if (btrfs_is_testing(fs_info
))
1854 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
&&
1855 do_list
&& !(state
->state
& EXTENT_NORESERVE
) &&
1856 (*bits
& EXTENT_CLEAR_DATA_RESV
))
1857 btrfs_free_reserved_data_space_noquota(
1861 percpu_counter_add_batch(&fs_info
->delalloc_bytes
, -len
,
1862 fs_info
->delalloc_batch
);
1863 spin_lock(&inode
->lock
);
1864 inode
->delalloc_bytes
-= len
;
1865 if (do_list
&& inode
->delalloc_bytes
== 0 &&
1866 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1867 &inode
->runtime_flags
))
1868 btrfs_del_delalloc_inode(root
, inode
);
1869 spin_unlock(&inode
->lock
);
1872 if ((state
->state
& EXTENT_DELALLOC_NEW
) &&
1873 (*bits
& EXTENT_DELALLOC_NEW
)) {
1874 spin_lock(&inode
->lock
);
1875 ASSERT(inode
->new_delalloc_bytes
>= len
);
1876 inode
->new_delalloc_bytes
-= len
;
1877 spin_unlock(&inode
->lock
);
1882 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
1883 * in a chunk's stripe. This function ensures that bios do not span a
1886 * @page - The page we are about to add to the bio
1887 * @size - size we want to add to the bio
1888 * @bio - bio we want to ensure is smaller than a stripe
1889 * @bio_flags - flags of the bio
1891 * return 1 if page cannot be added to the bio
1892 * return 0 if page can be added to the bio
1893 * return error otherwise
1895 int btrfs_bio_fits_in_stripe(struct page
*page
, size_t size
, struct bio
*bio
,
1896 unsigned long bio_flags
)
1898 struct inode
*inode
= page
->mapping
->host
;
1899 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1900 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
1905 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1908 length
= bio
->bi_iter
.bi_size
;
1909 map_length
= length
;
1910 ret
= btrfs_map_block(fs_info
, btrfs_op(bio
), logical
, &map_length
,
1914 if (map_length
< length
+ size
)
1920 * in order to insert checksums into the metadata in large chunks,
1921 * we wait until bio submission time. All the pages in the bio are
1922 * checksummed and sums are attached onto the ordered extent record.
1924 * At IO completion time the cums attached on the ordered extent record
1925 * are inserted into the btree
1927 static blk_status_t
btrfs_submit_bio_start(void *private_data
, struct bio
*bio
,
1930 struct inode
*inode
= private_data
;
1931 blk_status_t ret
= 0;
1933 ret
= btrfs_csum_one_bio(inode
, bio
, 0, 0);
1934 BUG_ON(ret
); /* -ENOMEM */
1939 * extent_io.c submission hook. This does the right thing for csum calculation
1940 * on write, or reading the csums from the tree before a read.
1942 * Rules about async/sync submit,
1943 * a) read: sync submit
1945 * b) write without checksum: sync submit
1947 * c) write with checksum:
1948 * c-1) if bio is issued by fsync: sync submit
1949 * (sync_writers != 0)
1951 * c-2) if root is reloc root: sync submit
1952 * (only in case of buffered IO)
1954 * c-3) otherwise: async submit
1956 static blk_status_t
btrfs_submit_bio_hook(struct inode
*inode
, struct bio
*bio
,
1958 unsigned long bio_flags
)
1961 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1962 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1963 enum btrfs_wq_endio_type metadata
= BTRFS_WQ_ENDIO_DATA
;
1964 blk_status_t ret
= 0;
1966 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1968 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1970 if (btrfs_is_free_space_inode(BTRFS_I(inode
)))
1971 metadata
= BTRFS_WQ_ENDIO_FREE_SPACE
;
1973 if (bio_op(bio
) != REQ_OP_WRITE
) {
1974 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, metadata
);
1978 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1979 ret
= btrfs_submit_compressed_read(inode
, bio
,
1983 } else if (!skip_sum
) {
1984 ret
= btrfs_lookup_bio_sums(inode
, bio
, NULL
);
1989 } else if (async
&& !skip_sum
) {
1990 /* csum items have already been cloned */
1991 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1993 /* we're doing a write, do the async checksumming */
1994 ret
= btrfs_wq_submit_bio(fs_info
, bio
, mirror_num
, bio_flags
,
1995 0, inode
, btrfs_submit_bio_start
);
1997 } else if (!skip_sum
) {
1998 ret
= btrfs_csum_one_bio(inode
, bio
, 0, 0);
2004 ret
= btrfs_map_bio(fs_info
, bio
, mirror_num
, 0);
2008 bio
->bi_status
= ret
;
2015 * given a list of ordered sums record them in the inode. This happens
2016 * at IO completion time based on sums calculated at bio submission time.
2018 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
2019 struct inode
*inode
, struct list_head
*list
)
2021 struct btrfs_ordered_sum
*sum
;
2024 list_for_each_entry(sum
, list
, list
) {
2025 trans
->adding_csums
= true;
2026 ret
= btrfs_csum_file_blocks(trans
,
2027 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
2028 trans
->adding_csums
= false;
2035 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
2036 unsigned int extra_bits
,
2037 struct extent_state
**cached_state
, int dedupe
)
2039 WARN_ON(PAGE_ALIGNED(end
));
2040 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
2041 extra_bits
, cached_state
);
2044 /* see btrfs_writepage_start_hook for details on why this is required */
2045 struct btrfs_writepage_fixup
{
2047 struct btrfs_work work
;
2050 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
2052 struct btrfs_writepage_fixup
*fixup
;
2053 struct btrfs_ordered_extent
*ordered
;
2054 struct extent_state
*cached_state
= NULL
;
2055 struct extent_changeset
*data_reserved
= NULL
;
2057 struct inode
*inode
;
2062 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
2066 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
2067 ClearPageChecked(page
);
2071 inode
= page
->mapping
->host
;
2072 page_start
= page_offset(page
);
2073 page_end
= page_offset(page
) + PAGE_SIZE
- 1;
2075 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
2078 /* already ordered? We're done */
2079 if (PagePrivate2(page
))
2082 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), page_start
,
2085 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
2086 page_end
, &cached_state
);
2088 btrfs_start_ordered_extent(inode
, ordered
, 1);
2089 btrfs_put_ordered_extent(ordered
);
2093 ret
= btrfs_delalloc_reserve_space(inode
, &data_reserved
, page_start
,
2096 mapping_set_error(page
->mapping
, ret
);
2097 end_extent_writepage(page
, ret
, page_start
, page_end
);
2098 ClearPageChecked(page
);
2102 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
, 0,
2105 mapping_set_error(page
->mapping
, ret
);
2106 end_extent_writepage(page
, ret
, page_start
, page_end
);
2107 ClearPageChecked(page
);
2111 ClearPageChecked(page
);
2112 set_page_dirty(page
);
2113 btrfs_delalloc_release_extents(BTRFS_I(inode
), PAGE_SIZE
, false);
2115 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
2121 extent_changeset_free(data_reserved
);
2125 * There are a few paths in the higher layers of the kernel that directly
2126 * set the page dirty bit without asking the filesystem if it is a
2127 * good idea. This causes problems because we want to make sure COW
2128 * properly happens and the data=ordered rules are followed.
2130 * In our case any range that doesn't have the ORDERED bit set
2131 * hasn't been properly setup for IO. We kick off an async process
2132 * to fix it up. The async helper will wait for ordered extents, set
2133 * the delalloc bit and make it safe to write the page.
2135 int btrfs_writepage_cow_fixup(struct page
*page
, u64 start
, u64 end
)
2137 struct inode
*inode
= page
->mapping
->host
;
2138 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2139 struct btrfs_writepage_fixup
*fixup
;
2141 /* this page is properly in the ordered list */
2142 if (TestClearPagePrivate2(page
))
2145 if (PageChecked(page
))
2148 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
2152 SetPageChecked(page
);
2154 btrfs_init_work(&fixup
->work
, btrfs_fixup_helper
,
2155 btrfs_writepage_fixup_worker
, NULL
, NULL
);
2157 btrfs_queue_work(fs_info
->fixup_workers
, &fixup
->work
);
2161 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
2162 struct inode
*inode
, u64 file_pos
,
2163 u64 disk_bytenr
, u64 disk_num_bytes
,
2164 u64 num_bytes
, u64 ram_bytes
,
2165 u8 compression
, u8 encryption
,
2166 u16 other_encoding
, int extent_type
)
2168 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2169 struct btrfs_file_extent_item
*fi
;
2170 struct btrfs_path
*path
;
2171 struct extent_buffer
*leaf
;
2172 struct btrfs_key ins
;
2174 int extent_inserted
= 0;
2177 path
= btrfs_alloc_path();
2182 * we may be replacing one extent in the tree with another.
2183 * The new extent is pinned in the extent map, and we don't want
2184 * to drop it from the cache until it is completely in the btree.
2186 * So, tell btrfs_drop_extents to leave this extent in the cache.
2187 * the caller is expected to unpin it and allow it to be merged
2190 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
, file_pos
,
2191 file_pos
+ num_bytes
, NULL
, 0,
2192 1, sizeof(*fi
), &extent_inserted
);
2196 if (!extent_inserted
) {
2197 ins
.objectid
= btrfs_ino(BTRFS_I(inode
));
2198 ins
.offset
= file_pos
;
2199 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
2201 path
->leave_spinning
= 1;
2202 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
2207 leaf
= path
->nodes
[0];
2208 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2209 struct btrfs_file_extent_item
);
2210 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
2211 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
2212 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
2213 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
2214 btrfs_set_file_extent_offset(leaf
, fi
, 0);
2215 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
2216 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
2217 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
2218 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
2219 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
2221 btrfs_mark_buffer_dirty(leaf
);
2222 btrfs_release_path(path
);
2224 inode_add_bytes(inode
, num_bytes
);
2226 ins
.objectid
= disk_bytenr
;
2227 ins
.offset
= disk_num_bytes
;
2228 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2231 * Release the reserved range from inode dirty range map, as it is
2232 * already moved into delayed_ref_head
2234 ret
= btrfs_qgroup_release_data(inode
, file_pos
, ram_bytes
);
2238 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
2239 btrfs_ino(BTRFS_I(inode
)),
2240 file_pos
, qg_released
, &ins
);
2242 btrfs_free_path(path
);
2247 /* snapshot-aware defrag */
2248 struct sa_defrag_extent_backref
{
2249 struct rb_node node
;
2250 struct old_sa_defrag_extent
*old
;
2259 struct old_sa_defrag_extent
{
2260 struct list_head list
;
2261 struct new_sa_defrag_extent
*new;
2270 struct new_sa_defrag_extent
{
2271 struct rb_root root
;
2272 struct list_head head
;
2273 struct btrfs_path
*path
;
2274 struct inode
*inode
;
2282 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
2283 struct sa_defrag_extent_backref
*b2
)
2285 if (b1
->root_id
< b2
->root_id
)
2287 else if (b1
->root_id
> b2
->root_id
)
2290 if (b1
->inum
< b2
->inum
)
2292 else if (b1
->inum
> b2
->inum
)
2295 if (b1
->file_pos
< b2
->file_pos
)
2297 else if (b1
->file_pos
> b2
->file_pos
)
2301 * [------------------------------] ===> (a range of space)
2302 * |<--->| |<---->| =============> (fs/file tree A)
2303 * |<---------------------------->| ===> (fs/file tree B)
2305 * A range of space can refer to two file extents in one tree while
2306 * refer to only one file extent in another tree.
2308 * So we may process a disk offset more than one time(two extents in A)
2309 * and locate at the same extent(one extent in B), then insert two same
2310 * backrefs(both refer to the extent in B).
2315 static void backref_insert(struct rb_root
*root
,
2316 struct sa_defrag_extent_backref
*backref
)
2318 struct rb_node
**p
= &root
->rb_node
;
2319 struct rb_node
*parent
= NULL
;
2320 struct sa_defrag_extent_backref
*entry
;
2325 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2327 ret
= backref_comp(backref
, entry
);
2331 p
= &(*p
)->rb_right
;
2334 rb_link_node(&backref
->node
, parent
, p
);
2335 rb_insert_color(&backref
->node
, root
);
2339 * Note the backref might has changed, and in this case we just return 0.
2341 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2344 struct btrfs_file_extent_item
*extent
;
2345 struct old_sa_defrag_extent
*old
= ctx
;
2346 struct new_sa_defrag_extent
*new = old
->new;
2347 struct btrfs_path
*path
= new->path
;
2348 struct btrfs_key key
;
2349 struct btrfs_root
*root
;
2350 struct sa_defrag_extent_backref
*backref
;
2351 struct extent_buffer
*leaf
;
2352 struct inode
*inode
= new->inode
;
2353 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2359 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2360 inum
== btrfs_ino(BTRFS_I(inode
)))
2363 key
.objectid
= root_id
;
2364 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2365 key
.offset
= (u64
)-1;
2367 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2369 if (PTR_ERR(root
) == -ENOENT
)
2372 btrfs_debug(fs_info
, "inum=%llu, offset=%llu, root_id=%llu",
2373 inum
, offset
, root_id
);
2374 return PTR_ERR(root
);
2377 key
.objectid
= inum
;
2378 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2379 if (offset
> (u64
)-1 << 32)
2382 key
.offset
= offset
;
2384 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2385 if (WARN_ON(ret
< 0))
2392 leaf
= path
->nodes
[0];
2393 slot
= path
->slots
[0];
2395 if (slot
>= btrfs_header_nritems(leaf
)) {
2396 ret
= btrfs_next_leaf(root
, path
);
2399 } else if (ret
> 0) {
2408 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2410 if (key
.objectid
> inum
)
2413 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2416 extent
= btrfs_item_ptr(leaf
, slot
,
2417 struct btrfs_file_extent_item
);
2419 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2423 * 'offset' refers to the exact key.offset,
2424 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2425 * (key.offset - extent_offset).
2427 if (key
.offset
!= offset
)
2430 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2431 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2433 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2434 old
->len
|| extent_offset
+ num_bytes
<=
2435 old
->extent_offset
+ old
->offset
)
2440 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2446 backref
->root_id
= root_id
;
2447 backref
->inum
= inum
;
2448 backref
->file_pos
= offset
;
2449 backref
->num_bytes
= num_bytes
;
2450 backref
->extent_offset
= extent_offset
;
2451 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2453 backref_insert(&new->root
, backref
);
2456 btrfs_release_path(path
);
2461 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2462 struct new_sa_defrag_extent
*new)
2464 struct btrfs_fs_info
*fs_info
= btrfs_sb(new->inode
->i_sb
);
2465 struct old_sa_defrag_extent
*old
, *tmp
;
2470 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2471 ret
= iterate_inodes_from_logical(old
->bytenr
+
2472 old
->extent_offset
, fs_info
,
2473 path
, record_one_backref
,
2475 if (ret
< 0 && ret
!= -ENOENT
)
2478 /* no backref to be processed for this extent */
2480 list_del(&old
->list
);
2485 if (list_empty(&new->head
))
2491 static int relink_is_mergable(struct extent_buffer
*leaf
,
2492 struct btrfs_file_extent_item
*fi
,
2493 struct new_sa_defrag_extent
*new)
2495 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != new->bytenr
)
2498 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2501 if (btrfs_file_extent_compression(leaf
, fi
) != new->compress_type
)
2504 if (btrfs_file_extent_encryption(leaf
, fi
) ||
2505 btrfs_file_extent_other_encoding(leaf
, fi
))
2512 * Note the backref might has changed, and in this case we just return 0.
2514 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2515 struct sa_defrag_extent_backref
*prev
,
2516 struct sa_defrag_extent_backref
*backref
)
2518 struct btrfs_file_extent_item
*extent
;
2519 struct btrfs_file_extent_item
*item
;
2520 struct btrfs_ordered_extent
*ordered
;
2521 struct btrfs_trans_handle
*trans
;
2522 struct btrfs_ref ref
= { 0 };
2523 struct btrfs_root
*root
;
2524 struct btrfs_key key
;
2525 struct extent_buffer
*leaf
;
2526 struct old_sa_defrag_extent
*old
= backref
->old
;
2527 struct new_sa_defrag_extent
*new = old
->new;
2528 struct btrfs_fs_info
*fs_info
= btrfs_sb(new->inode
->i_sb
);
2529 struct inode
*inode
;
2530 struct extent_state
*cached
= NULL
;
2539 if (prev
&& prev
->root_id
== backref
->root_id
&&
2540 prev
->inum
== backref
->inum
&&
2541 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2544 /* step 1: get root */
2545 key
.objectid
= backref
->root_id
;
2546 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2547 key
.offset
= (u64
)-1;
2549 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2551 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2553 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2554 if (PTR_ERR(root
) == -ENOENT
)
2556 return PTR_ERR(root
);
2559 if (btrfs_root_readonly(root
)) {
2560 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2564 /* step 2: get inode */
2565 key
.objectid
= backref
->inum
;
2566 key
.type
= BTRFS_INODE_ITEM_KEY
;
2569 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2570 if (IS_ERR(inode
)) {
2571 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2575 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2577 /* step 3: relink backref */
2578 lock_start
= backref
->file_pos
;
2579 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2580 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2583 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2585 btrfs_put_ordered_extent(ordered
);
2589 trans
= btrfs_join_transaction(root
);
2590 if (IS_ERR(trans
)) {
2591 ret
= PTR_ERR(trans
);
2595 key
.objectid
= backref
->inum
;
2596 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2597 key
.offset
= backref
->file_pos
;
2599 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2602 } else if (ret
> 0) {
2607 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2608 struct btrfs_file_extent_item
);
2610 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2611 backref
->generation
)
2614 btrfs_release_path(path
);
2616 start
= backref
->file_pos
;
2617 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2618 start
+= old
->extent_offset
+ old
->offset
-
2619 backref
->extent_offset
;
2621 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2622 old
->extent_offset
+ old
->offset
+ old
->len
);
2623 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2625 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2630 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
2631 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2634 path
->leave_spinning
= 1;
2636 struct btrfs_file_extent_item
*fi
;
2638 struct btrfs_key found_key
;
2640 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2645 leaf
= path
->nodes
[0];
2646 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2648 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2649 struct btrfs_file_extent_item
);
2650 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2652 if (extent_len
+ found_key
.offset
== start
&&
2653 relink_is_mergable(leaf
, fi
, new)) {
2654 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2656 btrfs_mark_buffer_dirty(leaf
);
2657 inode_add_bytes(inode
, len
);
2663 btrfs_release_path(path
);
2668 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2671 btrfs_abort_transaction(trans
, ret
);
2675 leaf
= path
->nodes
[0];
2676 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2677 struct btrfs_file_extent_item
);
2678 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2679 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2680 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2681 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2682 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2683 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2684 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2685 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2686 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2687 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2689 btrfs_mark_buffer_dirty(leaf
);
2690 inode_add_bytes(inode
, len
);
2691 btrfs_release_path(path
);
2693 btrfs_init_generic_ref(&ref
, BTRFS_ADD_DELAYED_REF
, new->bytenr
,
2695 btrfs_init_data_ref(&ref
, backref
->root_id
, backref
->inum
,
2696 new->file_pos
); /* start - extent_offset */
2697 ret
= btrfs_inc_extent_ref(trans
, &ref
);
2699 btrfs_abort_transaction(trans
, ret
);
2705 btrfs_release_path(path
);
2706 path
->leave_spinning
= 0;
2707 btrfs_end_transaction(trans
);
2709 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2715 static void free_sa_defrag_extent(struct new_sa_defrag_extent
*new)
2717 struct old_sa_defrag_extent
*old
, *tmp
;
2722 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2728 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2730 struct btrfs_fs_info
*fs_info
= btrfs_sb(new->inode
->i_sb
);
2731 struct btrfs_path
*path
;
2732 struct sa_defrag_extent_backref
*backref
;
2733 struct sa_defrag_extent_backref
*prev
= NULL
;
2734 struct rb_node
*node
;
2737 path
= btrfs_alloc_path();
2741 if (!record_extent_backrefs(path
, new)) {
2742 btrfs_free_path(path
);
2745 btrfs_release_path(path
);
2748 node
= rb_first(&new->root
);
2751 rb_erase(node
, &new->root
);
2753 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2755 ret
= relink_extent_backref(path
, prev
, backref
);
2768 btrfs_free_path(path
);
2770 free_sa_defrag_extent(new);
2772 atomic_dec(&fs_info
->defrag_running
);
2773 wake_up(&fs_info
->transaction_wait
);
2776 static struct new_sa_defrag_extent
*
2777 record_old_file_extents(struct inode
*inode
,
2778 struct btrfs_ordered_extent
*ordered
)
2780 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2781 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2782 struct btrfs_path
*path
;
2783 struct btrfs_key key
;
2784 struct old_sa_defrag_extent
*old
;
2785 struct new_sa_defrag_extent
*new;
2788 new = kmalloc(sizeof(*new), GFP_NOFS
);
2793 new->file_pos
= ordered
->file_offset
;
2794 new->len
= ordered
->len
;
2795 new->bytenr
= ordered
->start
;
2796 new->disk_len
= ordered
->disk_len
;
2797 new->compress_type
= ordered
->compress_type
;
2798 new->root
= RB_ROOT
;
2799 INIT_LIST_HEAD(&new->head
);
2801 path
= btrfs_alloc_path();
2805 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
2806 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2807 key
.offset
= new->file_pos
;
2809 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2812 if (ret
> 0 && path
->slots
[0] > 0)
2815 /* find out all the old extents for the file range */
2817 struct btrfs_file_extent_item
*extent
;
2818 struct extent_buffer
*l
;
2827 slot
= path
->slots
[0];
2829 if (slot
>= btrfs_header_nritems(l
)) {
2830 ret
= btrfs_next_leaf(root
, path
);
2838 btrfs_item_key_to_cpu(l
, &key
, slot
);
2840 if (key
.objectid
!= btrfs_ino(BTRFS_I(inode
)))
2842 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2844 if (key
.offset
>= new->file_pos
+ new->len
)
2847 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2849 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2850 if (key
.offset
+ num_bytes
< new->file_pos
)
2853 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2857 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2859 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2863 offset
= max(new->file_pos
, key
.offset
);
2864 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2866 old
->bytenr
= disk_bytenr
;
2867 old
->extent_offset
= extent_offset
;
2868 old
->offset
= offset
- key
.offset
;
2869 old
->len
= end
- offset
;
2872 list_add_tail(&old
->list
, &new->head
);
2878 btrfs_free_path(path
);
2879 atomic_inc(&fs_info
->defrag_running
);
2884 btrfs_free_path(path
);
2886 free_sa_defrag_extent(new);
2890 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info
*fs_info
,
2893 struct btrfs_block_group_cache
*cache
;
2895 cache
= btrfs_lookup_block_group(fs_info
, start
);
2898 spin_lock(&cache
->lock
);
2899 cache
->delalloc_bytes
-= len
;
2900 spin_unlock(&cache
->lock
);
2902 btrfs_put_block_group(cache
);
2905 /* as ordered data IO finishes, this gets called so we can finish
2906 * an ordered extent if the range of bytes in the file it covers are
2909 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2911 struct inode
*inode
= ordered_extent
->inode
;
2912 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2913 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2914 struct btrfs_trans_handle
*trans
= NULL
;
2915 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2916 struct extent_state
*cached_state
= NULL
;
2917 struct new_sa_defrag_extent
*new = NULL
;
2918 int compress_type
= 0;
2920 u64 logical_len
= ordered_extent
->len
;
2922 bool truncated
= false;
2923 bool range_locked
= false;
2924 bool clear_new_delalloc_bytes
= false;
2925 bool clear_reserved_extent
= true;
2927 if (!test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2928 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
) &&
2929 !test_bit(BTRFS_ORDERED_DIRECT
, &ordered_extent
->flags
))
2930 clear_new_delalloc_bytes
= true;
2932 nolock
= btrfs_is_free_space_inode(BTRFS_I(inode
));
2934 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2939 btrfs_free_io_failure_record(BTRFS_I(inode
),
2940 ordered_extent
->file_offset
,
2941 ordered_extent
->file_offset
+
2942 ordered_extent
->len
- 1);
2944 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2946 logical_len
= ordered_extent
->truncated_len
;
2947 /* Truncated the entire extent, don't bother adding */
2952 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2953 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2956 * For mwrite(mmap + memset to write) case, we still reserve
2957 * space for NOCOW range.
2958 * As NOCOW won't cause a new delayed ref, just free the space
2960 btrfs_qgroup_free_data(inode
, NULL
, ordered_extent
->file_offset
,
2961 ordered_extent
->len
);
2962 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2964 trans
= btrfs_join_transaction_nolock(root
);
2966 trans
= btrfs_join_transaction(root
);
2967 if (IS_ERR(trans
)) {
2968 ret
= PTR_ERR(trans
);
2972 trans
->block_rsv
= &BTRFS_I(inode
)->block_rsv
;
2973 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2974 if (ret
) /* -ENOMEM or corruption */
2975 btrfs_abort_transaction(trans
, ret
);
2979 range_locked
= true;
2980 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2981 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2984 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2985 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2986 EXTENT_DEFRAG
, 0, cached_state
);
2988 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2989 if (0 && last_snapshot
>= BTRFS_I(inode
)->generation
)
2990 /* the inode is shared */
2991 new = record_old_file_extents(inode
, ordered_extent
);
2993 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2994 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2995 EXTENT_DEFRAG
, 0, 0, &cached_state
);
2999 trans
= btrfs_join_transaction_nolock(root
);
3001 trans
= btrfs_join_transaction(root
);
3002 if (IS_ERR(trans
)) {
3003 ret
= PTR_ERR(trans
);
3008 trans
->block_rsv
= &BTRFS_I(inode
)->block_rsv
;
3010 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
3011 compress_type
= ordered_extent
->compress_type
;
3012 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
3013 BUG_ON(compress_type
);
3014 btrfs_qgroup_free_data(inode
, NULL
, ordered_extent
->file_offset
,
3015 ordered_extent
->len
);
3016 ret
= btrfs_mark_extent_written(trans
, BTRFS_I(inode
),
3017 ordered_extent
->file_offset
,
3018 ordered_extent
->file_offset
+
3021 BUG_ON(root
== fs_info
->tree_root
);
3022 ret
= insert_reserved_file_extent(trans
, inode
,
3023 ordered_extent
->file_offset
,
3024 ordered_extent
->start
,
3025 ordered_extent
->disk_len
,
3026 logical_len
, logical_len
,
3027 compress_type
, 0, 0,
3028 BTRFS_FILE_EXTENT_REG
);
3030 clear_reserved_extent
= false;
3031 btrfs_release_delalloc_bytes(fs_info
,
3032 ordered_extent
->start
,
3033 ordered_extent
->disk_len
);
3036 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
3037 ordered_extent
->file_offset
, ordered_extent
->len
,
3040 btrfs_abort_transaction(trans
, ret
);
3044 ret
= add_pending_csums(trans
, inode
, &ordered_extent
->list
);
3046 btrfs_abort_transaction(trans
, ret
);
3050 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
3051 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
3052 if (ret
) { /* -ENOMEM or corruption */
3053 btrfs_abort_transaction(trans
, ret
);
3058 if (range_locked
|| clear_new_delalloc_bytes
) {
3059 unsigned int clear_bits
= 0;
3062 clear_bits
|= EXTENT_LOCKED
;
3063 if (clear_new_delalloc_bytes
)
3064 clear_bits
|= EXTENT_DELALLOC_NEW
;
3065 clear_extent_bit(&BTRFS_I(inode
)->io_tree
,
3066 ordered_extent
->file_offset
,
3067 ordered_extent
->file_offset
+
3068 ordered_extent
->len
- 1,
3070 (clear_bits
& EXTENT_LOCKED
) ? 1 : 0,
3075 btrfs_end_transaction(trans
);
3077 if (ret
|| truncated
) {
3081 start
= ordered_extent
->file_offset
+ logical_len
;
3083 start
= ordered_extent
->file_offset
;
3084 end
= ordered_extent
->file_offset
+ ordered_extent
->len
- 1;
3085 clear_extent_uptodate(io_tree
, start
, end
, NULL
);
3087 /* Drop the cache for the part of the extent we didn't write. */
3088 btrfs_drop_extent_cache(BTRFS_I(inode
), start
, end
, 0);
3091 * If the ordered extent had an IOERR or something else went
3092 * wrong we need to return the space for this ordered extent
3093 * back to the allocator. We only free the extent in the
3094 * truncated case if we didn't write out the extent at all.
3096 * If we made it past insert_reserved_file_extent before we
3097 * errored out then we don't need to do this as the accounting
3098 * has already been done.
3100 if ((ret
|| !logical_len
) &&
3101 clear_reserved_extent
&&
3102 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
3103 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
3104 btrfs_free_reserved_extent(fs_info
,
3105 ordered_extent
->start
,
3106 ordered_extent
->disk_len
, 1);
3111 * This needs to be done to make sure anybody waiting knows we are done
3112 * updating everything for this ordered extent.
3114 btrfs_remove_ordered_extent(inode
, ordered_extent
);
3116 /* for snapshot-aware defrag */
3119 free_sa_defrag_extent(new);
3120 atomic_dec(&fs_info
->defrag_running
);
3122 relink_file_extents(new);
3127 btrfs_put_ordered_extent(ordered_extent
);
3128 /* once for the tree */
3129 btrfs_put_ordered_extent(ordered_extent
);
3134 static void finish_ordered_fn(struct btrfs_work
*work
)
3136 struct btrfs_ordered_extent
*ordered_extent
;
3137 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
3138 btrfs_finish_ordered_io(ordered_extent
);
3141 void btrfs_writepage_endio_finish_ordered(struct page
*page
, u64 start
,
3142 u64 end
, int uptodate
)
3144 struct inode
*inode
= page
->mapping
->host
;
3145 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3146 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
3147 struct btrfs_workqueue
*wq
;
3148 btrfs_work_func_t func
;
3150 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
3152 ClearPagePrivate2(page
);
3153 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
3154 end
- start
+ 1, uptodate
))
3157 if (btrfs_is_free_space_inode(BTRFS_I(inode
))) {
3158 wq
= fs_info
->endio_freespace_worker
;
3159 func
= btrfs_freespace_write_helper
;
3161 wq
= fs_info
->endio_write_workers
;
3162 func
= btrfs_endio_write_helper
;
3165 btrfs_init_work(&ordered_extent
->work
, func
, finish_ordered_fn
, NULL
,
3167 btrfs_queue_work(wq
, &ordered_extent
->work
);
3170 static int __readpage_endio_check(struct inode
*inode
,
3171 struct btrfs_io_bio
*io_bio
,
3172 int icsum
, struct page
*page
,
3173 int pgoff
, u64 start
, size_t len
)
3179 csum_expected
= *(((u32
*)io_bio
->csum
) + icsum
);
3181 kaddr
= kmap_atomic(page
);
3182 csum
= btrfs_csum_data(kaddr
+ pgoff
, csum
, len
);
3183 btrfs_csum_final(csum
, (u8
*)&csum
);
3184 if (csum
!= csum_expected
)
3187 kunmap_atomic(kaddr
);
3190 btrfs_print_data_csum_error(BTRFS_I(inode
), start
, csum
, csum_expected
,
3191 io_bio
->mirror_num
);
3192 memset(kaddr
+ pgoff
, 1, len
);
3193 flush_dcache_page(page
);
3194 kunmap_atomic(kaddr
);
3199 * when reads are done, we need to check csums to verify the data is correct
3200 * if there's a match, we allow the bio to finish. If not, the code in
3201 * extent_io.c will try to find good copies for us.
3203 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
3204 u64 phy_offset
, struct page
*page
,
3205 u64 start
, u64 end
, int mirror
)
3207 size_t offset
= start
- page_offset(page
);
3208 struct inode
*inode
= page
->mapping
->host
;
3209 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3210 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3212 if (PageChecked(page
)) {
3213 ClearPageChecked(page
);
3217 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
3220 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
3221 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
3222 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
);
3226 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
3227 return __readpage_endio_check(inode
, io_bio
, phy_offset
, page
, offset
,
3228 start
, (size_t)(end
- start
+ 1));
3232 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3234 * @inode: The inode we want to perform iput on
3236 * This function uses the generic vfs_inode::i_count to track whether we should
3237 * just decrement it (in case it's > 1) or if this is the last iput then link
3238 * the inode to the delayed iput machinery. Delayed iputs are processed at
3239 * transaction commit time/superblock commit/cleaner kthread.
3241 void btrfs_add_delayed_iput(struct inode
*inode
)
3243 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3244 struct btrfs_inode
*binode
= BTRFS_I(inode
);
3246 if (atomic_add_unless(&inode
->i_count
, -1, 1))
3249 atomic_inc(&fs_info
->nr_delayed_iputs
);
3250 spin_lock(&fs_info
->delayed_iput_lock
);
3251 ASSERT(list_empty(&binode
->delayed_iput
));
3252 list_add_tail(&binode
->delayed_iput
, &fs_info
->delayed_iputs
);
3253 spin_unlock(&fs_info
->delayed_iput_lock
);
3254 if (!test_bit(BTRFS_FS_CLEANER_RUNNING
, &fs_info
->flags
))
3255 wake_up_process(fs_info
->cleaner_kthread
);
3258 void btrfs_run_delayed_iputs(struct btrfs_fs_info
*fs_info
)
3261 spin_lock(&fs_info
->delayed_iput_lock
);
3262 while (!list_empty(&fs_info
->delayed_iputs
)) {
3263 struct btrfs_inode
*inode
;
3265 inode
= list_first_entry(&fs_info
->delayed_iputs
,
3266 struct btrfs_inode
, delayed_iput
);
3267 list_del_init(&inode
->delayed_iput
);
3268 spin_unlock(&fs_info
->delayed_iput_lock
);
3269 iput(&inode
->vfs_inode
);
3270 if (atomic_dec_and_test(&fs_info
->nr_delayed_iputs
))
3271 wake_up(&fs_info
->delayed_iputs_wait
);
3272 spin_lock(&fs_info
->delayed_iput_lock
);
3274 spin_unlock(&fs_info
->delayed_iput_lock
);
3278 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3279 * @fs_info - the fs_info for this fs
3280 * @return - EINTR if we were killed, 0 if nothing's pending
3282 * This will wait on any delayed iputs that are currently running with KILLABLE
3283 * set. Once they are all done running we will return, unless we are killed in
3284 * which case we return EINTR. This helps in user operations like fallocate etc
3285 * that might get blocked on the iputs.
3287 int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info
*fs_info
)
3289 int ret
= wait_event_killable(fs_info
->delayed_iputs_wait
,
3290 atomic_read(&fs_info
->nr_delayed_iputs
) == 0);
3297 * This creates an orphan entry for the given inode in case something goes wrong
3298 * in the middle of an unlink.
3300 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
,
3301 struct btrfs_inode
*inode
)
3305 ret
= btrfs_insert_orphan_item(trans
, inode
->root
, btrfs_ino(inode
));
3306 if (ret
&& ret
!= -EEXIST
) {
3307 btrfs_abort_transaction(trans
, ret
);
3315 * We have done the delete so we can go ahead and remove the orphan item for
3316 * this particular inode.
3318 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3319 struct btrfs_inode
*inode
)
3321 return btrfs_del_orphan_item(trans
, inode
->root
, btrfs_ino(inode
));
3325 * this cleans up any orphans that may be left on the list from the last use
3328 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3330 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3331 struct btrfs_path
*path
;
3332 struct extent_buffer
*leaf
;
3333 struct btrfs_key key
, found_key
;
3334 struct btrfs_trans_handle
*trans
;
3335 struct inode
*inode
;
3336 u64 last_objectid
= 0;
3337 int ret
= 0, nr_unlink
= 0;
3339 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3342 path
= btrfs_alloc_path();
3347 path
->reada
= READA_BACK
;
3349 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3350 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
3351 key
.offset
= (u64
)-1;
3354 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3359 * if ret == 0 means we found what we were searching for, which
3360 * is weird, but possible, so only screw with path if we didn't
3361 * find the key and see if we have stuff that matches
3365 if (path
->slots
[0] == 0)
3370 /* pull out the item */
3371 leaf
= path
->nodes
[0];
3372 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3374 /* make sure the item matches what we want */
3375 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3377 if (found_key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
3380 /* release the path since we're done with it */
3381 btrfs_release_path(path
);
3384 * this is where we are basically btrfs_lookup, without the
3385 * crossing root thing. we store the inode number in the
3386 * offset of the orphan item.
3389 if (found_key
.offset
== last_objectid
) {
3391 "Error removing orphan entry, stopping orphan cleanup");
3396 last_objectid
= found_key
.offset
;
3398 found_key
.objectid
= found_key
.offset
;
3399 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3400 found_key
.offset
= 0;
3401 inode
= btrfs_iget(fs_info
->sb
, &found_key
, root
, NULL
);
3402 ret
= PTR_ERR_OR_ZERO(inode
);
3403 if (ret
&& ret
!= -ENOENT
)
3406 if (ret
== -ENOENT
&& root
== fs_info
->tree_root
) {
3407 struct btrfs_root
*dead_root
;
3408 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3409 int is_dead_root
= 0;
3412 * this is an orphan in the tree root. Currently these
3413 * could come from 2 sources:
3414 * a) a snapshot deletion in progress
3415 * b) a free space cache inode
3416 * We need to distinguish those two, as the snapshot
3417 * orphan must not get deleted.
3418 * find_dead_roots already ran before us, so if this
3419 * is a snapshot deletion, we should find the root
3420 * in the dead_roots list
3422 spin_lock(&fs_info
->trans_lock
);
3423 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3425 if (dead_root
->root_key
.objectid
==
3426 found_key
.objectid
) {
3431 spin_unlock(&fs_info
->trans_lock
);
3433 /* prevent this orphan from being found again */
3434 key
.offset
= found_key
.objectid
- 1;
3441 * If we have an inode with links, there are a couple of
3442 * possibilities. Old kernels (before v3.12) used to create an
3443 * orphan item for truncate indicating that there were possibly
3444 * extent items past i_size that needed to be deleted. In v3.12,
3445 * truncate was changed to update i_size in sync with the extent
3446 * items, but the (useless) orphan item was still created. Since
3447 * v4.18, we don't create the orphan item for truncate at all.
3449 * So, this item could mean that we need to do a truncate, but
3450 * only if this filesystem was last used on a pre-v3.12 kernel
3451 * and was not cleanly unmounted. The odds of that are quite
3452 * slim, and it's a pain to do the truncate now, so just delete
3455 * It's also possible that this orphan item was supposed to be
3456 * deleted but wasn't. The inode number may have been reused,
3457 * but either way, we can delete the orphan item.
3459 if (ret
== -ENOENT
|| inode
->i_nlink
) {
3462 trans
= btrfs_start_transaction(root
, 1);
3463 if (IS_ERR(trans
)) {
3464 ret
= PTR_ERR(trans
);
3467 btrfs_debug(fs_info
, "auto deleting %Lu",
3468 found_key
.objectid
);
3469 ret
= btrfs_del_orphan_item(trans
, root
,
3470 found_key
.objectid
);
3471 btrfs_end_transaction(trans
);
3479 /* this will do delete_inode and everything for us */
3482 /* release the path since we're done with it */
3483 btrfs_release_path(path
);
3485 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3487 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
)) {
3488 trans
= btrfs_join_transaction(root
);
3490 btrfs_end_transaction(trans
);
3494 btrfs_debug(fs_info
, "unlinked %d orphans", nr_unlink
);
3498 btrfs_err(fs_info
, "could not do orphan cleanup %d", ret
);
3499 btrfs_free_path(path
);
3504 * very simple check to peek ahead in the leaf looking for xattrs. If we
3505 * don't find any xattrs, we know there can't be any acls.
3507 * slot is the slot the inode is in, objectid is the objectid of the inode
3509 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3510 int slot
, u64 objectid
,
3511 int *first_xattr_slot
)
3513 u32 nritems
= btrfs_header_nritems(leaf
);
3514 struct btrfs_key found_key
;
3515 static u64 xattr_access
= 0;
3516 static u64 xattr_default
= 0;
3519 if (!xattr_access
) {
3520 xattr_access
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS
,
3521 strlen(XATTR_NAME_POSIX_ACL_ACCESS
));
3522 xattr_default
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT
,
3523 strlen(XATTR_NAME_POSIX_ACL_DEFAULT
));
3527 *first_xattr_slot
= -1;
3528 while (slot
< nritems
) {
3529 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3531 /* we found a different objectid, there must not be acls */
3532 if (found_key
.objectid
!= objectid
)
3535 /* we found an xattr, assume we've got an acl */
3536 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3537 if (*first_xattr_slot
== -1)
3538 *first_xattr_slot
= slot
;
3539 if (found_key
.offset
== xattr_access
||
3540 found_key
.offset
== xattr_default
)
3545 * we found a key greater than an xattr key, there can't
3546 * be any acls later on
3548 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3555 * it goes inode, inode backrefs, xattrs, extents,
3556 * so if there are a ton of hard links to an inode there can
3557 * be a lot of backrefs. Don't waste time searching too hard,
3558 * this is just an optimization
3563 /* we hit the end of the leaf before we found an xattr or
3564 * something larger than an xattr. We have to assume the inode
3567 if (*first_xattr_slot
== -1)
3568 *first_xattr_slot
= slot
;
3573 * read an inode from the btree into the in-memory inode
3575 static int btrfs_read_locked_inode(struct inode
*inode
,
3576 struct btrfs_path
*in_path
)
3578 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3579 struct btrfs_path
*path
= in_path
;
3580 struct extent_buffer
*leaf
;
3581 struct btrfs_inode_item
*inode_item
;
3582 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3583 struct btrfs_key location
;
3588 bool filled
= false;
3589 int first_xattr_slot
;
3591 ret
= btrfs_fill_inode(inode
, &rdev
);
3596 path
= btrfs_alloc_path();
3601 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3603 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3605 if (path
!= in_path
)
3606 btrfs_free_path(path
);
3610 leaf
= path
->nodes
[0];
3615 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3616 struct btrfs_inode_item
);
3617 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3618 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3619 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3620 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3621 btrfs_i_size_write(BTRFS_I(inode
), btrfs_inode_size(leaf
, inode_item
));
3623 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->atime
);
3624 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->atime
);
3626 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->mtime
);
3627 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->mtime
);
3629 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->ctime
);
3630 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->ctime
);
3632 BTRFS_I(inode
)->i_otime
.tv_sec
=
3633 btrfs_timespec_sec(leaf
, &inode_item
->otime
);
3634 BTRFS_I(inode
)->i_otime
.tv_nsec
=
3635 btrfs_timespec_nsec(leaf
, &inode_item
->otime
);
3637 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3638 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3639 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3641 inode_set_iversion_queried(inode
,
3642 btrfs_inode_sequence(leaf
, inode_item
));
3643 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3645 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3647 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3648 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3652 * If we were modified in the current generation and evicted from memory
3653 * and then re-read we need to do a full sync since we don't have any
3654 * idea about which extents were modified before we were evicted from
3657 * This is required for both inode re-read from disk and delayed inode
3658 * in delayed_nodes_tree.
3660 if (BTRFS_I(inode
)->last_trans
== fs_info
->generation
)
3661 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3662 &BTRFS_I(inode
)->runtime_flags
);
3665 * We don't persist the id of the transaction where an unlink operation
3666 * against the inode was last made. So here we assume the inode might
3667 * have been evicted, and therefore the exact value of last_unlink_trans
3668 * lost, and set it to last_trans to avoid metadata inconsistencies
3669 * between the inode and its parent if the inode is fsync'ed and the log
3670 * replayed. For example, in the scenario:
3673 * ln mydir/foo mydir/bar
3676 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3677 * xfs_io -c fsync mydir/foo
3679 * mount fs, triggers fsync log replay
3681 * We must make sure that when we fsync our inode foo we also log its
3682 * parent inode, otherwise after log replay the parent still has the
3683 * dentry with the "bar" name but our inode foo has a link count of 1
3684 * and doesn't have an inode ref with the name "bar" anymore.
3686 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3687 * but it guarantees correctness at the expense of occasional full
3688 * transaction commits on fsync if our inode is a directory, or if our
3689 * inode is not a directory, logging its parent unnecessarily.
3691 BTRFS_I(inode
)->last_unlink_trans
= BTRFS_I(inode
)->last_trans
;
3693 * Similar reasoning for last_link_trans, needs to be set otherwise
3694 * for a case like the following:
3699 * echo 2 > /proc/sys/vm/drop_caches
3703 * Would result in link bar and directory A not existing after the power
3706 BTRFS_I(inode
)->last_link_trans
= BTRFS_I(inode
)->last_trans
;
3709 if (inode
->i_nlink
!= 1 ||
3710 path
->slots
[0] >= btrfs_header_nritems(leaf
))
3713 btrfs_item_key_to_cpu(leaf
, &location
, path
->slots
[0]);
3714 if (location
.objectid
!= btrfs_ino(BTRFS_I(inode
)))
3717 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3718 if (location
.type
== BTRFS_INODE_REF_KEY
) {
3719 struct btrfs_inode_ref
*ref
;
3721 ref
= (struct btrfs_inode_ref
*)ptr
;
3722 BTRFS_I(inode
)->dir_index
= btrfs_inode_ref_index(leaf
, ref
);
3723 } else if (location
.type
== BTRFS_INODE_EXTREF_KEY
) {
3724 struct btrfs_inode_extref
*extref
;
3726 extref
= (struct btrfs_inode_extref
*)ptr
;
3727 BTRFS_I(inode
)->dir_index
= btrfs_inode_extref_index(leaf
,
3732 * try to precache a NULL acl entry for files that don't have
3733 * any xattrs or acls
3735 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3736 btrfs_ino(BTRFS_I(inode
)), &first_xattr_slot
);
3737 if (first_xattr_slot
!= -1) {
3738 path
->slots
[0] = first_xattr_slot
;
3739 ret
= btrfs_load_inode_props(inode
, path
);
3742 "error loading props for ino %llu (root %llu): %d",
3743 btrfs_ino(BTRFS_I(inode
)),
3744 root
->root_key
.objectid
, ret
);
3746 if (path
!= in_path
)
3747 btrfs_free_path(path
);
3750 cache_no_acl(inode
);
3752 switch (inode
->i_mode
& S_IFMT
) {
3754 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3755 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3756 inode
->i_fop
= &btrfs_file_operations
;
3757 inode
->i_op
= &btrfs_file_inode_operations
;
3760 inode
->i_fop
= &btrfs_dir_file_operations
;
3761 inode
->i_op
= &btrfs_dir_inode_operations
;
3764 inode
->i_op
= &btrfs_symlink_inode_operations
;
3765 inode_nohighmem(inode
);
3766 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3769 inode
->i_op
= &btrfs_special_inode_operations
;
3770 init_special_inode(inode
, inode
->i_mode
, rdev
);
3774 btrfs_sync_inode_flags_to_i_flags(inode
);
3779 * given a leaf and an inode, copy the inode fields into the leaf
3781 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3782 struct extent_buffer
*leaf
,
3783 struct btrfs_inode_item
*item
,
3784 struct inode
*inode
)
3786 struct btrfs_map_token token
;
3788 btrfs_init_map_token(&token
);
3790 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3791 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3792 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3794 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3795 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3797 btrfs_set_token_timespec_sec(leaf
, &item
->atime
,
3798 inode
->i_atime
.tv_sec
, &token
);
3799 btrfs_set_token_timespec_nsec(leaf
, &item
->atime
,
3800 inode
->i_atime
.tv_nsec
, &token
);
3802 btrfs_set_token_timespec_sec(leaf
, &item
->mtime
,
3803 inode
->i_mtime
.tv_sec
, &token
);
3804 btrfs_set_token_timespec_nsec(leaf
, &item
->mtime
,
3805 inode
->i_mtime
.tv_nsec
, &token
);
3807 btrfs_set_token_timespec_sec(leaf
, &item
->ctime
,
3808 inode
->i_ctime
.tv_sec
, &token
);
3809 btrfs_set_token_timespec_nsec(leaf
, &item
->ctime
,
3810 inode
->i_ctime
.tv_nsec
, &token
);
3812 btrfs_set_token_timespec_sec(leaf
, &item
->otime
,
3813 BTRFS_I(inode
)->i_otime
.tv_sec
, &token
);
3814 btrfs_set_token_timespec_nsec(leaf
, &item
->otime
,
3815 BTRFS_I(inode
)->i_otime
.tv_nsec
, &token
);
3817 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3819 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3821 btrfs_set_token_inode_sequence(leaf
, item
, inode_peek_iversion(inode
),
3823 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3824 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3825 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3826 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3830 * copy everything in the in-memory inode into the btree.
3832 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3833 struct btrfs_root
*root
, struct inode
*inode
)
3835 struct btrfs_inode_item
*inode_item
;
3836 struct btrfs_path
*path
;
3837 struct extent_buffer
*leaf
;
3840 path
= btrfs_alloc_path();
3844 path
->leave_spinning
= 1;
3845 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3853 leaf
= path
->nodes
[0];
3854 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3855 struct btrfs_inode_item
);
3857 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3858 btrfs_mark_buffer_dirty(leaf
);
3859 btrfs_set_inode_last_trans(trans
, inode
);
3862 btrfs_free_path(path
);
3867 * copy everything in the in-memory inode into the btree.
3869 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3870 struct btrfs_root
*root
, struct inode
*inode
)
3872 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3876 * If the inode is a free space inode, we can deadlock during commit
3877 * if we put it into the delayed code.
3879 * The data relocation inode should also be directly updated
3882 if (!btrfs_is_free_space_inode(BTRFS_I(inode
))
3883 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
3884 && !test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
)) {
3885 btrfs_update_root_times(trans
, root
);
3887 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3889 btrfs_set_inode_last_trans(trans
, inode
);
3893 return btrfs_update_inode_item(trans
, root
, inode
);
3896 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3897 struct btrfs_root
*root
,
3898 struct inode
*inode
)
3902 ret
= btrfs_update_inode(trans
, root
, inode
);
3904 return btrfs_update_inode_item(trans
, root
, inode
);
3909 * unlink helper that gets used here in inode.c and in the tree logging
3910 * recovery code. It remove a link in a directory with a given name, and
3911 * also drops the back refs in the inode to the directory
3913 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3914 struct btrfs_root
*root
,
3915 struct btrfs_inode
*dir
,
3916 struct btrfs_inode
*inode
,
3917 const char *name
, int name_len
)
3919 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3920 struct btrfs_path
*path
;
3922 struct extent_buffer
*leaf
;
3923 struct btrfs_dir_item
*di
;
3924 struct btrfs_key key
;
3926 u64 ino
= btrfs_ino(inode
);
3927 u64 dir_ino
= btrfs_ino(dir
);
3929 path
= btrfs_alloc_path();
3935 path
->leave_spinning
= 1;
3936 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3937 name
, name_len
, -1);
3938 if (IS_ERR_OR_NULL(di
)) {
3939 ret
= di
? PTR_ERR(di
) : -ENOENT
;
3942 leaf
= path
->nodes
[0];
3943 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3944 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3947 btrfs_release_path(path
);
3950 * If we don't have dir index, we have to get it by looking up
3951 * the inode ref, since we get the inode ref, remove it directly,
3952 * it is unnecessary to do delayed deletion.
3954 * But if we have dir index, needn't search inode ref to get it.
3955 * Since the inode ref is close to the inode item, it is better
3956 * that we delay to delete it, and just do this deletion when
3957 * we update the inode item.
3959 if (inode
->dir_index
) {
3960 ret
= btrfs_delayed_delete_inode_ref(inode
);
3962 index
= inode
->dir_index
;
3967 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3971 "failed to delete reference to %.*s, inode %llu parent %llu",
3972 name_len
, name
, ino
, dir_ino
);
3973 btrfs_abort_transaction(trans
, ret
);
3977 ret
= btrfs_delete_delayed_dir_index(trans
, dir
, index
);
3979 btrfs_abort_transaction(trans
, ret
);
3983 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
, inode
,
3985 if (ret
!= 0 && ret
!= -ENOENT
) {
3986 btrfs_abort_transaction(trans
, ret
);
3990 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
, dir
,
3995 btrfs_abort_transaction(trans
, ret
);
3997 btrfs_free_path(path
);
4001 btrfs_i_size_write(dir
, dir
->vfs_inode
.i_size
- name_len
* 2);
4002 inode_inc_iversion(&inode
->vfs_inode
);
4003 inode_inc_iversion(&dir
->vfs_inode
);
4004 inode
->vfs_inode
.i_ctime
= dir
->vfs_inode
.i_mtime
=
4005 dir
->vfs_inode
.i_ctime
= current_time(&inode
->vfs_inode
);
4006 ret
= btrfs_update_inode(trans
, root
, &dir
->vfs_inode
);
4011 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
4012 struct btrfs_root
*root
,
4013 struct btrfs_inode
*dir
, struct btrfs_inode
*inode
,
4014 const char *name
, int name_len
)
4017 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
4019 drop_nlink(&inode
->vfs_inode
);
4020 ret
= btrfs_update_inode(trans
, root
, &inode
->vfs_inode
);
4026 * helper to start transaction for unlink and rmdir.
4028 * unlink and rmdir are special in btrfs, they do not always free space, so
4029 * if we cannot make our reservations the normal way try and see if there is
4030 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4031 * allow the unlink to occur.
4033 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
4035 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4038 * 1 for the possible orphan item
4039 * 1 for the dir item
4040 * 1 for the dir index
4041 * 1 for the inode ref
4044 return btrfs_start_transaction_fallback_global_rsv(root
, 5, 5);
4047 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
4049 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4050 struct btrfs_trans_handle
*trans
;
4051 struct inode
*inode
= d_inode(dentry
);
4054 trans
= __unlink_start_trans(dir
);
4056 return PTR_ERR(trans
);
4058 btrfs_record_unlink_dir(trans
, BTRFS_I(dir
), BTRFS_I(d_inode(dentry
)),
4061 ret
= btrfs_unlink_inode(trans
, root
, BTRFS_I(dir
),
4062 BTRFS_I(d_inode(dentry
)), dentry
->d_name
.name
,
4063 dentry
->d_name
.len
);
4067 if (inode
->i_nlink
== 0) {
4068 ret
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
4074 btrfs_end_transaction(trans
);
4075 btrfs_btree_balance_dirty(root
->fs_info
);
4079 static int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
4080 struct inode
*dir
, u64 objectid
,
4081 const char *name
, int name_len
)
4083 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4084 struct btrfs_path
*path
;
4085 struct extent_buffer
*leaf
;
4086 struct btrfs_dir_item
*di
;
4087 struct btrfs_key key
;
4090 u64 dir_ino
= btrfs_ino(BTRFS_I(dir
));
4092 path
= btrfs_alloc_path();
4096 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
4097 name
, name_len
, -1);
4098 if (IS_ERR_OR_NULL(di
)) {
4099 ret
= di
? PTR_ERR(di
) : -ENOENT
;
4103 leaf
= path
->nodes
[0];
4104 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
4105 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
4106 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
4108 btrfs_abort_transaction(trans
, ret
);
4111 btrfs_release_path(path
);
4113 ret
= btrfs_del_root_ref(trans
, objectid
, root
->root_key
.objectid
,
4114 dir_ino
, &index
, name
, name_len
);
4116 if (ret
!= -ENOENT
) {
4117 btrfs_abort_transaction(trans
, ret
);
4120 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
4122 if (IS_ERR_OR_NULL(di
)) {
4127 btrfs_abort_transaction(trans
, ret
);
4131 leaf
= path
->nodes
[0];
4132 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4135 btrfs_release_path(path
);
4137 ret
= btrfs_delete_delayed_dir_index(trans
, BTRFS_I(dir
), index
);
4139 btrfs_abort_transaction(trans
, ret
);
4143 btrfs_i_size_write(BTRFS_I(dir
), dir
->i_size
- name_len
* 2);
4144 inode_inc_iversion(dir
);
4145 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
4146 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
4148 btrfs_abort_transaction(trans
, ret
);
4150 btrfs_free_path(path
);
4155 * Helper to check if the subvolume references other subvolumes or if it's
4158 static noinline
int may_destroy_subvol(struct btrfs_root
*root
)
4160 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4161 struct btrfs_path
*path
;
4162 struct btrfs_dir_item
*di
;
4163 struct btrfs_key key
;
4167 path
= btrfs_alloc_path();
4171 /* Make sure this root isn't set as the default subvol */
4172 dir_id
= btrfs_super_root_dir(fs_info
->super_copy
);
4173 di
= btrfs_lookup_dir_item(NULL
, fs_info
->tree_root
, path
,
4174 dir_id
, "default", 7, 0);
4175 if (di
&& !IS_ERR(di
)) {
4176 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, &key
);
4177 if (key
.objectid
== root
->root_key
.objectid
) {
4180 "deleting default subvolume %llu is not allowed",
4184 btrfs_release_path(path
);
4187 key
.objectid
= root
->root_key
.objectid
;
4188 key
.type
= BTRFS_ROOT_REF_KEY
;
4189 key
.offset
= (u64
)-1;
4191 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
4197 if (path
->slots
[0] > 0) {
4199 btrfs_item_key_to_cpu(path
->nodes
[0], &key
, path
->slots
[0]);
4200 if (key
.objectid
== root
->root_key
.objectid
&&
4201 key
.type
== BTRFS_ROOT_REF_KEY
)
4205 btrfs_free_path(path
);
4209 /* Delete all dentries for inodes belonging to the root */
4210 static void btrfs_prune_dentries(struct btrfs_root
*root
)
4212 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4213 struct rb_node
*node
;
4214 struct rb_node
*prev
;
4215 struct btrfs_inode
*entry
;
4216 struct inode
*inode
;
4219 if (!test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
4220 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4222 spin_lock(&root
->inode_lock
);
4224 node
= root
->inode_tree
.rb_node
;
4228 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4230 if (objectid
< btrfs_ino(entry
))
4231 node
= node
->rb_left
;
4232 else if (objectid
> btrfs_ino(entry
))
4233 node
= node
->rb_right
;
4239 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4240 if (objectid
<= btrfs_ino(entry
)) {
4244 prev
= rb_next(prev
);
4248 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4249 objectid
= btrfs_ino(entry
) + 1;
4250 inode
= igrab(&entry
->vfs_inode
);
4252 spin_unlock(&root
->inode_lock
);
4253 if (atomic_read(&inode
->i_count
) > 1)
4254 d_prune_aliases(inode
);
4256 * btrfs_drop_inode will have it removed from the inode
4257 * cache when its usage count hits zero.
4261 spin_lock(&root
->inode_lock
);
4265 if (cond_resched_lock(&root
->inode_lock
))
4268 node
= rb_next(node
);
4270 spin_unlock(&root
->inode_lock
);
4273 int btrfs_delete_subvolume(struct inode
*dir
, struct dentry
*dentry
)
4275 struct btrfs_fs_info
*fs_info
= btrfs_sb(dentry
->d_sb
);
4276 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4277 struct inode
*inode
= d_inode(dentry
);
4278 struct btrfs_root
*dest
= BTRFS_I(inode
)->root
;
4279 struct btrfs_trans_handle
*trans
;
4280 struct btrfs_block_rsv block_rsv
;
4286 * Don't allow to delete a subvolume with send in progress. This is
4287 * inside the inode lock so the error handling that has to drop the bit
4288 * again is not run concurrently.
4290 spin_lock(&dest
->root_item_lock
);
4291 if (dest
->send_in_progress
) {
4292 spin_unlock(&dest
->root_item_lock
);
4294 "attempt to delete subvolume %llu during send",
4295 dest
->root_key
.objectid
);
4298 root_flags
= btrfs_root_flags(&dest
->root_item
);
4299 btrfs_set_root_flags(&dest
->root_item
,
4300 root_flags
| BTRFS_ROOT_SUBVOL_DEAD
);
4301 spin_unlock(&dest
->root_item_lock
);
4303 down_write(&fs_info
->subvol_sem
);
4305 err
= may_destroy_subvol(dest
);
4309 btrfs_init_block_rsv(&block_rsv
, BTRFS_BLOCK_RSV_TEMP
);
4311 * One for dir inode,
4312 * two for dir entries,
4313 * two for root ref/backref.
4315 err
= btrfs_subvolume_reserve_metadata(root
, &block_rsv
, 5, true);
4319 trans
= btrfs_start_transaction(root
, 0);
4320 if (IS_ERR(trans
)) {
4321 err
= PTR_ERR(trans
);
4324 trans
->block_rsv
= &block_rsv
;
4325 trans
->bytes_reserved
= block_rsv
.size
;
4327 btrfs_record_snapshot_destroy(trans
, BTRFS_I(dir
));
4329 ret
= btrfs_unlink_subvol(trans
, dir
, dest
->root_key
.objectid
,
4330 dentry
->d_name
.name
, dentry
->d_name
.len
);
4333 btrfs_abort_transaction(trans
, ret
);
4337 btrfs_record_root_in_trans(trans
, dest
);
4339 memset(&dest
->root_item
.drop_progress
, 0,
4340 sizeof(dest
->root_item
.drop_progress
));
4341 dest
->root_item
.drop_level
= 0;
4342 btrfs_set_root_refs(&dest
->root_item
, 0);
4344 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &dest
->state
)) {
4345 ret
= btrfs_insert_orphan_item(trans
,
4347 dest
->root_key
.objectid
);
4349 btrfs_abort_transaction(trans
, ret
);
4355 ret
= btrfs_uuid_tree_remove(trans
, dest
->root_item
.uuid
,
4356 BTRFS_UUID_KEY_SUBVOL
,
4357 dest
->root_key
.objectid
);
4358 if (ret
&& ret
!= -ENOENT
) {
4359 btrfs_abort_transaction(trans
, ret
);
4363 if (!btrfs_is_empty_uuid(dest
->root_item
.received_uuid
)) {
4364 ret
= btrfs_uuid_tree_remove(trans
,
4365 dest
->root_item
.received_uuid
,
4366 BTRFS_UUID_KEY_RECEIVED_SUBVOL
,
4367 dest
->root_key
.objectid
);
4368 if (ret
&& ret
!= -ENOENT
) {
4369 btrfs_abort_transaction(trans
, ret
);
4376 trans
->block_rsv
= NULL
;
4377 trans
->bytes_reserved
= 0;
4378 ret
= btrfs_end_transaction(trans
);
4381 inode
->i_flags
|= S_DEAD
;
4383 btrfs_subvolume_release_metadata(fs_info
, &block_rsv
);
4385 up_write(&fs_info
->subvol_sem
);
4387 spin_lock(&dest
->root_item_lock
);
4388 root_flags
= btrfs_root_flags(&dest
->root_item
);
4389 btrfs_set_root_flags(&dest
->root_item
,
4390 root_flags
& ~BTRFS_ROOT_SUBVOL_DEAD
);
4391 spin_unlock(&dest
->root_item_lock
);
4393 d_invalidate(dentry
);
4394 btrfs_prune_dentries(dest
);
4395 ASSERT(dest
->send_in_progress
== 0);
4398 if (dest
->ino_cache_inode
) {
4399 iput(dest
->ino_cache_inode
);
4400 dest
->ino_cache_inode
= NULL
;
4407 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
4409 struct inode
*inode
= d_inode(dentry
);
4411 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4412 struct btrfs_trans_handle
*trans
;
4413 u64 last_unlink_trans
;
4415 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
4417 if (btrfs_ino(BTRFS_I(inode
)) == BTRFS_FIRST_FREE_OBJECTID
)
4418 return btrfs_delete_subvolume(dir
, dentry
);
4420 trans
= __unlink_start_trans(dir
);
4422 return PTR_ERR(trans
);
4424 if (unlikely(btrfs_ino(BTRFS_I(inode
)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
4425 err
= btrfs_unlink_subvol(trans
, dir
,
4426 BTRFS_I(inode
)->location
.objectid
,
4427 dentry
->d_name
.name
,
4428 dentry
->d_name
.len
);
4432 err
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
4436 last_unlink_trans
= BTRFS_I(inode
)->last_unlink_trans
;
4438 /* now the directory is empty */
4439 err
= btrfs_unlink_inode(trans
, root
, BTRFS_I(dir
),
4440 BTRFS_I(d_inode(dentry
)), dentry
->d_name
.name
,
4441 dentry
->d_name
.len
);
4443 btrfs_i_size_write(BTRFS_I(inode
), 0);
4445 * Propagate the last_unlink_trans value of the deleted dir to
4446 * its parent directory. This is to prevent an unrecoverable
4447 * log tree in the case we do something like this:
4449 * 2) create snapshot under dir foo
4450 * 3) delete the snapshot
4453 * 6) fsync foo or some file inside foo
4455 if (last_unlink_trans
>= trans
->transid
)
4456 BTRFS_I(dir
)->last_unlink_trans
= last_unlink_trans
;
4459 btrfs_end_transaction(trans
);
4460 btrfs_btree_balance_dirty(root
->fs_info
);
4466 * Return this if we need to call truncate_block for the last bit of the
4469 #define NEED_TRUNCATE_BLOCK 1
4472 * this can truncate away extent items, csum items and directory items.
4473 * It starts at a high offset and removes keys until it can't find
4474 * any higher than new_size
4476 * csum items that cross the new i_size are truncated to the new size
4479 * min_type is the minimum key type to truncate down to. If set to 0, this
4480 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4482 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4483 struct btrfs_root
*root
,
4484 struct inode
*inode
,
4485 u64 new_size
, u32 min_type
)
4487 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4488 struct btrfs_path
*path
;
4489 struct extent_buffer
*leaf
;
4490 struct btrfs_file_extent_item
*fi
;
4491 struct btrfs_key key
;
4492 struct btrfs_key found_key
;
4493 u64 extent_start
= 0;
4494 u64 extent_num_bytes
= 0;
4495 u64 extent_offset
= 0;
4497 u64 last_size
= new_size
;
4498 u32 found_type
= (u8
)-1;
4501 int pending_del_nr
= 0;
4502 int pending_del_slot
= 0;
4503 int extent_type
= -1;
4505 u64 ino
= btrfs_ino(BTRFS_I(inode
));
4506 u64 bytes_deleted
= 0;
4507 bool be_nice
= false;
4508 bool should_throttle
= false;
4510 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4513 * for non-free space inodes and ref cows, we want to back off from
4516 if (!btrfs_is_free_space_inode(BTRFS_I(inode
)) &&
4517 test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4520 path
= btrfs_alloc_path();
4523 path
->reada
= READA_BACK
;
4526 * We want to drop from the next block forward in case this new size is
4527 * not block aligned since we will be keeping the last block of the
4528 * extent just the way it is.
4530 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4531 root
== fs_info
->tree_root
)
4532 btrfs_drop_extent_cache(BTRFS_I(inode
), ALIGN(new_size
,
4533 fs_info
->sectorsize
),
4537 * This function is also used to drop the items in the log tree before
4538 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4539 * it is used to drop the logged items. So we shouldn't kill the delayed
4542 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4543 btrfs_kill_delayed_inode_items(BTRFS_I(inode
));
4546 key
.offset
= (u64
)-1;
4551 * with a 16K leaf size and 128MB extents, you can actually queue
4552 * up a huge file in a single leaf. Most of the time that
4553 * bytes_deleted is > 0, it will be huge by the time we get here
4555 if (be_nice
&& bytes_deleted
> SZ_32M
&&
4556 btrfs_should_end_transaction(trans
)) {
4561 path
->leave_spinning
= 1;
4562 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4568 /* there are no items in the tree for us to truncate, we're
4571 if (path
->slots
[0] == 0)
4578 leaf
= path
->nodes
[0];
4579 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4580 found_type
= found_key
.type
;
4582 if (found_key
.objectid
!= ino
)
4585 if (found_type
< min_type
)
4588 item_end
= found_key
.offset
;
4589 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4590 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4591 struct btrfs_file_extent_item
);
4592 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4593 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4595 btrfs_file_extent_num_bytes(leaf
, fi
);
4597 trace_btrfs_truncate_show_fi_regular(
4598 BTRFS_I(inode
), leaf
, fi
,
4600 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4601 item_end
+= btrfs_file_extent_ram_bytes(leaf
,
4604 trace_btrfs_truncate_show_fi_inline(
4605 BTRFS_I(inode
), leaf
, fi
, path
->slots
[0],
4610 if (found_type
> min_type
) {
4613 if (item_end
< new_size
)
4615 if (found_key
.offset
>= new_size
)
4621 /* FIXME, shrink the extent if the ref count is only 1 */
4622 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4625 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4627 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4629 u64 orig_num_bytes
=
4630 btrfs_file_extent_num_bytes(leaf
, fi
);
4631 extent_num_bytes
= ALIGN(new_size
-
4633 fs_info
->sectorsize
);
4634 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4636 num_dec
= (orig_num_bytes
-
4638 if (test_bit(BTRFS_ROOT_REF_COWS
,
4641 inode_sub_bytes(inode
, num_dec
);
4642 btrfs_mark_buffer_dirty(leaf
);
4645 btrfs_file_extent_disk_num_bytes(leaf
,
4647 extent_offset
= found_key
.offset
-
4648 btrfs_file_extent_offset(leaf
, fi
);
4650 /* FIXME blocksize != 4096 */
4651 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4652 if (extent_start
!= 0) {
4654 if (test_bit(BTRFS_ROOT_REF_COWS
,
4656 inode_sub_bytes(inode
, num_dec
);
4659 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4661 * we can't truncate inline items that have had
4665 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4666 btrfs_file_extent_other_encoding(leaf
, fi
) == 0 &&
4667 btrfs_file_extent_compression(leaf
, fi
) == 0) {
4668 u32 size
= (u32
)(new_size
- found_key
.offset
);
4670 btrfs_set_file_extent_ram_bytes(leaf
, fi
, size
);
4671 size
= btrfs_file_extent_calc_inline_size(size
);
4672 btrfs_truncate_item(path
, size
, 1);
4673 } else if (!del_item
) {
4675 * We have to bail so the last_size is set to
4676 * just before this extent.
4678 ret
= NEED_TRUNCATE_BLOCK
;
4682 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4683 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4687 last_size
= found_key
.offset
;
4689 last_size
= new_size
;
4691 if (!pending_del_nr
) {
4692 /* no pending yet, add ourselves */
4693 pending_del_slot
= path
->slots
[0];
4695 } else if (pending_del_nr
&&
4696 path
->slots
[0] + 1 == pending_del_slot
) {
4697 /* hop on the pending chunk */
4699 pending_del_slot
= path
->slots
[0];
4706 should_throttle
= false;
4709 (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4710 root
== fs_info
->tree_root
)) {
4711 struct btrfs_ref ref
= { 0 };
4713 btrfs_set_path_blocking(path
);
4714 bytes_deleted
+= extent_num_bytes
;
4716 btrfs_init_generic_ref(&ref
, BTRFS_DROP_DELAYED_REF
,
4717 extent_start
, extent_num_bytes
, 0);
4718 ref
.real_root
= root
->root_key
.objectid
;
4719 btrfs_init_data_ref(&ref
, btrfs_header_owner(leaf
),
4720 ino
, extent_offset
);
4721 ret
= btrfs_free_extent(trans
, &ref
);
4723 btrfs_abort_transaction(trans
, ret
);
4727 if (btrfs_should_throttle_delayed_refs(trans
))
4728 should_throttle
= true;
4732 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4735 if (path
->slots
[0] == 0 ||
4736 path
->slots
[0] != pending_del_slot
||
4738 if (pending_del_nr
) {
4739 ret
= btrfs_del_items(trans
, root
, path
,
4743 btrfs_abort_transaction(trans
, ret
);
4748 btrfs_release_path(path
);
4751 * We can generate a lot of delayed refs, so we need to
4752 * throttle every once and a while and make sure we're
4753 * adding enough space to keep up with the work we are
4754 * generating. Since we hold a transaction here we
4755 * can't flush, and we don't want to FLUSH_LIMIT because
4756 * we could have generated too many delayed refs to
4757 * actually allocate, so just bail if we're short and
4758 * let the normal reservation dance happen higher up.
4760 if (should_throttle
) {
4761 ret
= btrfs_delayed_refs_rsv_refill(fs_info
,
4762 BTRFS_RESERVE_NO_FLUSH
);
4774 if (ret
>= 0 && pending_del_nr
) {
4777 err
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4780 btrfs_abort_transaction(trans
, err
);
4784 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
) {
4785 ASSERT(last_size
>= new_size
);
4786 if (!ret
&& last_size
> new_size
)
4787 last_size
= new_size
;
4788 btrfs_ordered_update_i_size(inode
, last_size
, NULL
);
4791 btrfs_free_path(path
);
4796 * btrfs_truncate_block - read, zero a chunk and write a block
4797 * @inode - inode that we're zeroing
4798 * @from - the offset to start zeroing
4799 * @len - the length to zero, 0 to zero the entire range respective to the
4801 * @front - zero up to the offset instead of from the offset on
4803 * This will find the block for the "from" offset and cow the block and zero the
4804 * part we want to zero. This is used with truncate and hole punching.
4806 int btrfs_truncate_block(struct inode
*inode
, loff_t from
, loff_t len
,
4809 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4810 struct address_space
*mapping
= inode
->i_mapping
;
4811 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4812 struct btrfs_ordered_extent
*ordered
;
4813 struct extent_state
*cached_state
= NULL
;
4814 struct extent_changeset
*data_reserved
= NULL
;
4816 u32 blocksize
= fs_info
->sectorsize
;
4817 pgoff_t index
= from
>> PAGE_SHIFT
;
4818 unsigned offset
= from
& (blocksize
- 1);
4820 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4825 if (IS_ALIGNED(offset
, blocksize
) &&
4826 (!len
|| IS_ALIGNED(len
, blocksize
)))
4829 block_start
= round_down(from
, blocksize
);
4830 block_end
= block_start
+ blocksize
- 1;
4832 ret
= btrfs_delalloc_reserve_space(inode
, &data_reserved
,
4833 block_start
, blocksize
);
4838 page
= find_or_create_page(mapping
, index
, mask
);
4840 btrfs_delalloc_release_space(inode
, data_reserved
,
4841 block_start
, blocksize
, true);
4842 btrfs_delalloc_release_extents(BTRFS_I(inode
), blocksize
, true);
4847 if (!PageUptodate(page
)) {
4848 ret
= btrfs_readpage(NULL
, page
);
4850 if (page
->mapping
!= mapping
) {
4855 if (!PageUptodate(page
)) {
4860 wait_on_page_writeback(page
);
4862 lock_extent_bits(io_tree
, block_start
, block_end
, &cached_state
);
4863 set_page_extent_mapped(page
);
4865 ordered
= btrfs_lookup_ordered_extent(inode
, block_start
);
4867 unlock_extent_cached(io_tree
, block_start
, block_end
,
4871 btrfs_start_ordered_extent(inode
, ordered
, 1);
4872 btrfs_put_ordered_extent(ordered
);
4876 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, block_start
, block_end
,
4877 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4878 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4879 0, 0, &cached_state
);
4881 ret
= btrfs_set_extent_delalloc(inode
, block_start
, block_end
, 0,
4884 unlock_extent_cached(io_tree
, block_start
, block_end
,
4889 if (offset
!= blocksize
) {
4891 len
= blocksize
- offset
;
4894 memset(kaddr
+ (block_start
- page_offset(page
)),
4897 memset(kaddr
+ (block_start
- page_offset(page
)) + offset
,
4899 flush_dcache_page(page
);
4902 ClearPageChecked(page
);
4903 set_page_dirty(page
);
4904 unlock_extent_cached(io_tree
, block_start
, block_end
, &cached_state
);
4908 btrfs_delalloc_release_space(inode
, data_reserved
, block_start
,
4910 btrfs_delalloc_release_extents(BTRFS_I(inode
), blocksize
, (ret
!= 0));
4914 extent_changeset_free(data_reserved
);
4918 static int maybe_insert_hole(struct btrfs_root
*root
, struct inode
*inode
,
4919 u64 offset
, u64 len
)
4921 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4922 struct btrfs_trans_handle
*trans
;
4926 * Still need to make sure the inode looks like it's been updated so
4927 * that any holes get logged if we fsync.
4929 if (btrfs_fs_incompat(fs_info
, NO_HOLES
)) {
4930 BTRFS_I(inode
)->last_trans
= fs_info
->generation
;
4931 BTRFS_I(inode
)->last_sub_trans
= root
->log_transid
;
4932 BTRFS_I(inode
)->last_log_commit
= root
->last_log_commit
;
4937 * 1 - for the one we're dropping
4938 * 1 - for the one we're adding
4939 * 1 - for updating the inode.
4941 trans
= btrfs_start_transaction(root
, 3);
4943 return PTR_ERR(trans
);
4945 ret
= btrfs_drop_extents(trans
, root
, inode
, offset
, offset
+ len
, 1);
4947 btrfs_abort_transaction(trans
, ret
);
4948 btrfs_end_transaction(trans
);
4952 ret
= btrfs_insert_file_extent(trans
, root
, btrfs_ino(BTRFS_I(inode
)),
4953 offset
, 0, 0, len
, 0, len
, 0, 0, 0);
4955 btrfs_abort_transaction(trans
, ret
);
4957 btrfs_update_inode(trans
, root
, inode
);
4958 btrfs_end_transaction(trans
);
4963 * This function puts in dummy file extents for the area we're creating a hole
4964 * for. So if we are truncating this file to a larger size we need to insert
4965 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4966 * the range between oldsize and size
4968 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4970 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4971 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4972 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4973 struct extent_map
*em
= NULL
;
4974 struct extent_state
*cached_state
= NULL
;
4975 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4976 u64 hole_start
= ALIGN(oldsize
, fs_info
->sectorsize
);
4977 u64 block_end
= ALIGN(size
, fs_info
->sectorsize
);
4984 * If our size started in the middle of a block we need to zero out the
4985 * rest of the block before we expand the i_size, otherwise we could
4986 * expose stale data.
4988 err
= btrfs_truncate_block(inode
, oldsize
, 0, 0);
4992 if (size
<= hole_start
)
4996 struct btrfs_ordered_extent
*ordered
;
4998 lock_extent_bits(io_tree
, hole_start
, block_end
- 1,
5000 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), hole_start
,
5001 block_end
- hole_start
);
5004 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
5006 btrfs_start_ordered_extent(inode
, ordered
, 1);
5007 btrfs_put_ordered_extent(ordered
);
5010 cur_offset
= hole_start
;
5012 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, cur_offset
,
5013 block_end
- cur_offset
, 0);
5019 last_byte
= min(extent_map_end(em
), block_end
);
5020 last_byte
= ALIGN(last_byte
, fs_info
->sectorsize
);
5021 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
5022 struct extent_map
*hole_em
;
5023 hole_size
= last_byte
- cur_offset
;
5025 err
= maybe_insert_hole(root
, inode
, cur_offset
,
5029 btrfs_drop_extent_cache(BTRFS_I(inode
), cur_offset
,
5030 cur_offset
+ hole_size
- 1, 0);
5031 hole_em
= alloc_extent_map();
5033 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
5034 &BTRFS_I(inode
)->runtime_flags
);
5037 hole_em
->start
= cur_offset
;
5038 hole_em
->len
= hole_size
;
5039 hole_em
->orig_start
= cur_offset
;
5041 hole_em
->block_start
= EXTENT_MAP_HOLE
;
5042 hole_em
->block_len
= 0;
5043 hole_em
->orig_block_len
= 0;
5044 hole_em
->ram_bytes
= hole_size
;
5045 hole_em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
5046 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
5047 hole_em
->generation
= fs_info
->generation
;
5050 write_lock(&em_tree
->lock
);
5051 err
= add_extent_mapping(em_tree
, hole_em
, 1);
5052 write_unlock(&em_tree
->lock
);
5055 btrfs_drop_extent_cache(BTRFS_I(inode
),
5060 free_extent_map(hole_em
);
5063 free_extent_map(em
);
5065 cur_offset
= last_byte
;
5066 if (cur_offset
>= block_end
)
5069 free_extent_map(em
);
5070 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
);
5074 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
5076 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5077 struct btrfs_trans_handle
*trans
;
5078 loff_t oldsize
= i_size_read(inode
);
5079 loff_t newsize
= attr
->ia_size
;
5080 int mask
= attr
->ia_valid
;
5084 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5085 * special case where we need to update the times despite not having
5086 * these flags set. For all other operations the VFS set these flags
5087 * explicitly if it wants a timestamp update.
5089 if (newsize
!= oldsize
) {
5090 inode_inc_iversion(inode
);
5091 if (!(mask
& (ATTR_CTIME
| ATTR_MTIME
)))
5092 inode
->i_ctime
= inode
->i_mtime
=
5093 current_time(inode
);
5096 if (newsize
> oldsize
) {
5098 * Don't do an expanding truncate while snapshotting is ongoing.
5099 * This is to ensure the snapshot captures a fully consistent
5100 * state of this file - if the snapshot captures this expanding
5101 * truncation, it must capture all writes that happened before
5104 btrfs_wait_for_snapshot_creation(root
);
5105 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
5107 btrfs_end_write_no_snapshotting(root
);
5111 trans
= btrfs_start_transaction(root
, 1);
5112 if (IS_ERR(trans
)) {
5113 btrfs_end_write_no_snapshotting(root
);
5114 return PTR_ERR(trans
);
5117 i_size_write(inode
, newsize
);
5118 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
5119 pagecache_isize_extended(inode
, oldsize
, newsize
);
5120 ret
= btrfs_update_inode(trans
, root
, inode
);
5121 btrfs_end_write_no_snapshotting(root
);
5122 btrfs_end_transaction(trans
);
5126 * We're truncating a file that used to have good data down to
5127 * zero. Make sure it gets into the ordered flush list so that
5128 * any new writes get down to disk quickly.
5131 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
5132 &BTRFS_I(inode
)->runtime_flags
);
5134 truncate_setsize(inode
, newsize
);
5136 /* Disable nonlocked read DIO to avoid the endless truncate */
5137 btrfs_inode_block_unlocked_dio(BTRFS_I(inode
));
5138 inode_dio_wait(inode
);
5139 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode
));
5141 ret
= btrfs_truncate(inode
, newsize
== oldsize
);
5142 if (ret
&& inode
->i_nlink
) {
5146 * Truncate failed, so fix up the in-memory size. We
5147 * adjusted disk_i_size down as we removed extents, so
5148 * wait for disk_i_size to be stable and then update the
5149 * in-memory size to match.
5151 err
= btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
5154 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
5161 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
5163 struct inode
*inode
= d_inode(dentry
);
5164 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5167 if (btrfs_root_readonly(root
))
5170 err
= setattr_prepare(dentry
, attr
);
5174 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
5175 err
= btrfs_setsize(inode
, attr
);
5180 if (attr
->ia_valid
) {
5181 setattr_copy(inode
, attr
);
5182 inode_inc_iversion(inode
);
5183 err
= btrfs_dirty_inode(inode
);
5185 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
5186 err
= posix_acl_chmod(inode
, inode
->i_mode
);
5193 * While truncating the inode pages during eviction, we get the VFS calling
5194 * btrfs_invalidatepage() against each page of the inode. This is slow because
5195 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5196 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5197 * extent_state structures over and over, wasting lots of time.
5199 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5200 * those expensive operations on a per page basis and do only the ordered io
5201 * finishing, while we release here the extent_map and extent_state structures,
5202 * without the excessive merging and splitting.
5204 static void evict_inode_truncate_pages(struct inode
*inode
)
5206 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5207 struct extent_map_tree
*map_tree
= &BTRFS_I(inode
)->extent_tree
;
5208 struct rb_node
*node
;
5210 ASSERT(inode
->i_state
& I_FREEING
);
5211 truncate_inode_pages_final(&inode
->i_data
);
5213 write_lock(&map_tree
->lock
);
5214 while (!RB_EMPTY_ROOT(&map_tree
->map
.rb_root
)) {
5215 struct extent_map
*em
;
5217 node
= rb_first_cached(&map_tree
->map
);
5218 em
= rb_entry(node
, struct extent_map
, rb_node
);
5219 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5220 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
5221 remove_extent_mapping(map_tree
, em
);
5222 free_extent_map(em
);
5223 if (need_resched()) {
5224 write_unlock(&map_tree
->lock
);
5226 write_lock(&map_tree
->lock
);
5229 write_unlock(&map_tree
->lock
);
5232 * Keep looping until we have no more ranges in the io tree.
5233 * We can have ongoing bios started by readpages (called from readahead)
5234 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5235 * still in progress (unlocked the pages in the bio but did not yet
5236 * unlocked the ranges in the io tree). Therefore this means some
5237 * ranges can still be locked and eviction started because before
5238 * submitting those bios, which are executed by a separate task (work
5239 * queue kthread), inode references (inode->i_count) were not taken
5240 * (which would be dropped in the end io callback of each bio).
5241 * Therefore here we effectively end up waiting for those bios and
5242 * anyone else holding locked ranges without having bumped the inode's
5243 * reference count - if we don't do it, when they access the inode's
5244 * io_tree to unlock a range it may be too late, leading to an
5245 * use-after-free issue.
5247 spin_lock(&io_tree
->lock
);
5248 while (!RB_EMPTY_ROOT(&io_tree
->state
)) {
5249 struct extent_state
*state
;
5250 struct extent_state
*cached_state
= NULL
;
5253 unsigned state_flags
;
5255 node
= rb_first(&io_tree
->state
);
5256 state
= rb_entry(node
, struct extent_state
, rb_node
);
5257 start
= state
->start
;
5259 state_flags
= state
->state
;
5260 spin_unlock(&io_tree
->lock
);
5262 lock_extent_bits(io_tree
, start
, end
, &cached_state
);
5265 * If still has DELALLOC flag, the extent didn't reach disk,
5266 * and its reserved space won't be freed by delayed_ref.
5267 * So we need to free its reserved space here.
5268 * (Refer to comment in btrfs_invalidatepage, case 2)
5270 * Note, end is the bytenr of last byte, so we need + 1 here.
5272 if (state_flags
& EXTENT_DELALLOC
)
5273 btrfs_qgroup_free_data(inode
, NULL
, start
, end
- start
+ 1);
5275 clear_extent_bit(io_tree
, start
, end
,
5276 EXTENT_LOCKED
| EXTENT_DIRTY
|
5277 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
5278 EXTENT_DEFRAG
, 1, 1, &cached_state
);
5281 spin_lock(&io_tree
->lock
);
5283 spin_unlock(&io_tree
->lock
);
5286 static struct btrfs_trans_handle
*evict_refill_and_join(struct btrfs_root
*root
,
5287 struct btrfs_block_rsv
*rsv
)
5289 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5290 struct btrfs_block_rsv
*global_rsv
= &fs_info
->global_block_rsv
;
5291 u64 delayed_refs_extra
= btrfs_calc_trans_metadata_size(fs_info
, 1);
5295 struct btrfs_trans_handle
*trans
;
5298 ret
= btrfs_block_rsv_refill(root
, rsv
,
5299 rsv
->size
+ delayed_refs_extra
,
5300 BTRFS_RESERVE_FLUSH_LIMIT
);
5302 if (ret
&& ++failures
> 2) {
5304 "could not allocate space for a delete; will truncate on mount");
5305 return ERR_PTR(-ENOSPC
);
5309 * Evict can generate a large amount of delayed refs without
5310 * having a way to add space back since we exhaust our temporary
5311 * block rsv. We aren't allowed to do FLUSH_ALL in this case
5312 * because we could deadlock with so many things in the flushing
5313 * code, so we have to try and hold some extra space to
5314 * compensate for our delayed ref generation. If we can't get
5315 * that space then we need see if we can steal our minimum from
5316 * the global reserve. We will be ratelimited by the amount of
5317 * space we have for the delayed refs rsv, so we'll end up
5318 * committing and trying again.
5320 trans
= btrfs_join_transaction(root
);
5321 if (IS_ERR(trans
) || !ret
) {
5322 if (!IS_ERR(trans
)) {
5323 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
5324 trans
->bytes_reserved
= delayed_refs_extra
;
5325 btrfs_block_rsv_migrate(rsv
, trans
->block_rsv
,
5326 delayed_refs_extra
, 1);
5332 * Try to steal from the global reserve if there is space for
5335 if (!btrfs_check_space_for_delayed_refs(fs_info
) &&
5336 !btrfs_block_rsv_migrate(global_rsv
, rsv
, rsv
->size
, 0))
5339 /* If not, commit and try again. */
5340 ret
= btrfs_commit_transaction(trans
);
5342 return ERR_PTR(ret
);
5346 void btrfs_evict_inode(struct inode
*inode
)
5348 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5349 struct btrfs_trans_handle
*trans
;
5350 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5351 struct btrfs_block_rsv
*rsv
;
5354 trace_btrfs_inode_evict(inode
);
5361 evict_inode_truncate_pages(inode
);
5363 if (inode
->i_nlink
&&
5364 ((btrfs_root_refs(&root
->root_item
) != 0 &&
5365 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
5366 btrfs_is_free_space_inode(BTRFS_I(inode
))))
5369 if (is_bad_inode(inode
))
5372 btrfs_free_io_failure_record(BTRFS_I(inode
), 0, (u64
)-1);
5374 if (test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
))
5377 if (inode
->i_nlink
> 0) {
5378 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
5379 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
5383 ret
= btrfs_commit_inode_delayed_inode(BTRFS_I(inode
));
5387 rsv
= btrfs_alloc_block_rsv(fs_info
, BTRFS_BLOCK_RSV_TEMP
);
5390 rsv
->size
= btrfs_calc_trunc_metadata_size(fs_info
, 1);
5393 btrfs_i_size_write(BTRFS_I(inode
), 0);
5396 trans
= evict_refill_and_join(root
, rsv
);
5400 trans
->block_rsv
= rsv
;
5402 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
5403 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
5404 btrfs_end_transaction(trans
);
5405 btrfs_btree_balance_dirty(fs_info
);
5406 if (ret
&& ret
!= -ENOSPC
&& ret
!= -EAGAIN
)
5413 * Errors here aren't a big deal, it just means we leave orphan items in
5414 * the tree. They will be cleaned up on the next mount. If the inode
5415 * number gets reused, cleanup deletes the orphan item without doing
5416 * anything, and unlink reuses the existing orphan item.
5418 * If it turns out that we are dropping too many of these, we might want
5419 * to add a mechanism for retrying these after a commit.
5421 trans
= evict_refill_and_join(root
, rsv
);
5422 if (!IS_ERR(trans
)) {
5423 trans
->block_rsv
= rsv
;
5424 btrfs_orphan_del(trans
, BTRFS_I(inode
));
5425 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
5426 btrfs_end_transaction(trans
);
5429 if (!(root
== fs_info
->tree_root
||
5430 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
5431 btrfs_return_ino(root
, btrfs_ino(BTRFS_I(inode
)));
5434 btrfs_free_block_rsv(fs_info
, rsv
);
5437 * If we didn't successfully delete, the orphan item will still be in
5438 * the tree and we'll retry on the next mount. Again, we might also want
5439 * to retry these periodically in the future.
5441 btrfs_remove_delayed_node(BTRFS_I(inode
));
5446 * Return the key found in the dir entry in the location pointer, fill @type
5447 * with BTRFS_FT_*, and return 0.
5449 * If no dir entries were found, returns -ENOENT.
5450 * If found a corrupted location in dir entry, returns -EUCLEAN.
5452 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
5453 struct btrfs_key
*location
, u8
*type
)
5455 const char *name
= dentry
->d_name
.name
;
5456 int namelen
= dentry
->d_name
.len
;
5457 struct btrfs_dir_item
*di
;
5458 struct btrfs_path
*path
;
5459 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5462 path
= btrfs_alloc_path();
5466 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(BTRFS_I(dir
)),
5468 if (IS_ERR_OR_NULL(di
)) {
5469 ret
= di
? PTR_ERR(di
) : -ENOENT
;
5473 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
5474 if (location
->type
!= BTRFS_INODE_ITEM_KEY
&&
5475 location
->type
!= BTRFS_ROOT_ITEM_KEY
) {
5477 btrfs_warn(root
->fs_info
,
5478 "%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5479 __func__
, name
, btrfs_ino(BTRFS_I(dir
)),
5480 location
->objectid
, location
->type
, location
->offset
);
5483 *type
= btrfs_dir_type(path
->nodes
[0], di
);
5485 btrfs_free_path(path
);
5490 * when we hit a tree root in a directory, the btrfs part of the inode
5491 * needs to be changed to reflect the root directory of the tree root. This
5492 * is kind of like crossing a mount point.
5494 static int fixup_tree_root_location(struct btrfs_fs_info
*fs_info
,
5496 struct dentry
*dentry
,
5497 struct btrfs_key
*location
,
5498 struct btrfs_root
**sub_root
)
5500 struct btrfs_path
*path
;
5501 struct btrfs_root
*new_root
;
5502 struct btrfs_root_ref
*ref
;
5503 struct extent_buffer
*leaf
;
5504 struct btrfs_key key
;
5508 path
= btrfs_alloc_path();
5515 key
.objectid
= BTRFS_I(dir
)->root
->root_key
.objectid
;
5516 key
.type
= BTRFS_ROOT_REF_KEY
;
5517 key
.offset
= location
->objectid
;
5519 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
5526 leaf
= path
->nodes
[0];
5527 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
5528 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(BTRFS_I(dir
)) ||
5529 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
5532 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
5533 (unsigned long)(ref
+ 1),
5534 dentry
->d_name
.len
);
5538 btrfs_release_path(path
);
5540 new_root
= btrfs_read_fs_root_no_name(fs_info
, location
);
5541 if (IS_ERR(new_root
)) {
5542 err
= PTR_ERR(new_root
);
5546 *sub_root
= new_root
;
5547 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
5548 location
->type
= BTRFS_INODE_ITEM_KEY
;
5549 location
->offset
= 0;
5552 btrfs_free_path(path
);
5556 static void inode_tree_add(struct inode
*inode
)
5558 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5559 struct btrfs_inode
*entry
;
5561 struct rb_node
*parent
;
5562 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
5563 u64 ino
= btrfs_ino(BTRFS_I(inode
));
5565 if (inode_unhashed(inode
))
5568 spin_lock(&root
->inode_lock
);
5569 p
= &root
->inode_tree
.rb_node
;
5572 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
5574 if (ino
< btrfs_ino(entry
))
5575 p
= &parent
->rb_left
;
5576 else if (ino
> btrfs_ino(entry
))
5577 p
= &parent
->rb_right
;
5579 WARN_ON(!(entry
->vfs_inode
.i_state
&
5580 (I_WILL_FREE
| I_FREEING
)));
5581 rb_replace_node(parent
, new, &root
->inode_tree
);
5582 RB_CLEAR_NODE(parent
);
5583 spin_unlock(&root
->inode_lock
);
5587 rb_link_node(new, parent
, p
);
5588 rb_insert_color(new, &root
->inode_tree
);
5589 spin_unlock(&root
->inode_lock
);
5592 static void inode_tree_del(struct inode
*inode
)
5594 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5595 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5598 spin_lock(&root
->inode_lock
);
5599 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
5600 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
5601 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
5602 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5604 spin_unlock(&root
->inode_lock
);
5606 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
5607 synchronize_srcu(&fs_info
->subvol_srcu
);
5608 spin_lock(&root
->inode_lock
);
5609 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5610 spin_unlock(&root
->inode_lock
);
5612 btrfs_add_dead_root(root
);
5617 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
5619 struct btrfs_iget_args
*args
= p
;
5620 inode
->i_ino
= args
->location
->objectid
;
5621 memcpy(&BTRFS_I(inode
)->location
, args
->location
,
5622 sizeof(*args
->location
));
5623 BTRFS_I(inode
)->root
= args
->root
;
5627 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
5629 struct btrfs_iget_args
*args
= opaque
;
5630 return args
->location
->objectid
== BTRFS_I(inode
)->location
.objectid
&&
5631 args
->root
== BTRFS_I(inode
)->root
;
5634 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
5635 struct btrfs_key
*location
,
5636 struct btrfs_root
*root
)
5638 struct inode
*inode
;
5639 struct btrfs_iget_args args
;
5640 unsigned long hashval
= btrfs_inode_hash(location
->objectid
, root
);
5642 args
.location
= location
;
5645 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
5646 btrfs_init_locked_inode
,
5651 /* Get an inode object given its location and corresponding root.
5652 * Returns in *is_new if the inode was read from disk
5654 struct inode
*btrfs_iget_path(struct super_block
*s
, struct btrfs_key
*location
,
5655 struct btrfs_root
*root
, int *new,
5656 struct btrfs_path
*path
)
5658 struct inode
*inode
;
5660 inode
= btrfs_iget_locked(s
, location
, root
);
5662 return ERR_PTR(-ENOMEM
);
5664 if (inode
->i_state
& I_NEW
) {
5667 ret
= btrfs_read_locked_inode(inode
, path
);
5669 inode_tree_add(inode
);
5670 unlock_new_inode(inode
);
5676 * ret > 0 can come from btrfs_search_slot called by
5677 * btrfs_read_locked_inode, this means the inode item
5682 inode
= ERR_PTR(ret
);
5689 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
5690 struct btrfs_root
*root
, int *new)
5692 return btrfs_iget_path(s
, location
, root
, new, NULL
);
5695 static struct inode
*new_simple_dir(struct super_block
*s
,
5696 struct btrfs_key
*key
,
5697 struct btrfs_root
*root
)
5699 struct inode
*inode
= new_inode(s
);
5702 return ERR_PTR(-ENOMEM
);
5704 BTRFS_I(inode
)->root
= root
;
5705 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5706 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5708 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5709 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5710 inode
->i_opflags
&= ~IOP_XATTR
;
5711 inode
->i_fop
= &simple_dir_operations
;
5712 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5713 inode
->i_mtime
= current_time(inode
);
5714 inode
->i_atime
= inode
->i_mtime
;
5715 inode
->i_ctime
= inode
->i_mtime
;
5716 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
5721 static inline u8
btrfs_inode_type(struct inode
*inode
)
5724 * Compile-time asserts that generic FT_* types still match
5727 BUILD_BUG_ON(BTRFS_FT_UNKNOWN
!= FT_UNKNOWN
);
5728 BUILD_BUG_ON(BTRFS_FT_REG_FILE
!= FT_REG_FILE
);
5729 BUILD_BUG_ON(BTRFS_FT_DIR
!= FT_DIR
);
5730 BUILD_BUG_ON(BTRFS_FT_CHRDEV
!= FT_CHRDEV
);
5731 BUILD_BUG_ON(BTRFS_FT_BLKDEV
!= FT_BLKDEV
);
5732 BUILD_BUG_ON(BTRFS_FT_FIFO
!= FT_FIFO
);
5733 BUILD_BUG_ON(BTRFS_FT_SOCK
!= FT_SOCK
);
5734 BUILD_BUG_ON(BTRFS_FT_SYMLINK
!= FT_SYMLINK
);
5736 return fs_umode_to_ftype(inode
->i_mode
);
5739 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5741 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
5742 struct inode
*inode
;
5743 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5744 struct btrfs_root
*sub_root
= root
;
5745 struct btrfs_key location
;
5750 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5751 return ERR_PTR(-ENAMETOOLONG
);
5753 ret
= btrfs_inode_by_name(dir
, dentry
, &location
, &di_type
);
5755 return ERR_PTR(ret
);
5757 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5758 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5762 /* Do extra check against inode mode with di_type */
5763 if (btrfs_inode_type(inode
) != di_type
) {
5765 "inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5766 inode
->i_mode
, btrfs_inode_type(inode
),
5769 return ERR_PTR(-EUCLEAN
);
5774 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
5775 ret
= fixup_tree_root_location(fs_info
, dir
, dentry
,
5776 &location
, &sub_root
);
5779 inode
= ERR_PTR(ret
);
5781 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5783 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5785 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5787 if (!IS_ERR(inode
) && root
!= sub_root
) {
5788 down_read(&fs_info
->cleanup_work_sem
);
5789 if (!sb_rdonly(inode
->i_sb
))
5790 ret
= btrfs_orphan_cleanup(sub_root
);
5791 up_read(&fs_info
->cleanup_work_sem
);
5794 inode
= ERR_PTR(ret
);
5801 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5803 struct btrfs_root
*root
;
5804 struct inode
*inode
= d_inode(dentry
);
5806 if (!inode
&& !IS_ROOT(dentry
))
5807 inode
= d_inode(dentry
->d_parent
);
5810 root
= BTRFS_I(inode
)->root
;
5811 if (btrfs_root_refs(&root
->root_item
) == 0)
5814 if (btrfs_ino(BTRFS_I(inode
)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5820 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5823 struct inode
*inode
= btrfs_lookup_dentry(dir
, dentry
);
5825 if (inode
== ERR_PTR(-ENOENT
))
5827 return d_splice_alias(inode
, dentry
);
5831 * All this infrastructure exists because dir_emit can fault, and we are holding
5832 * the tree lock when doing readdir. For now just allocate a buffer and copy
5833 * our information into that, and then dir_emit from the buffer. This is
5834 * similar to what NFS does, only we don't keep the buffer around in pagecache
5835 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5836 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5839 static int btrfs_opendir(struct inode
*inode
, struct file
*file
)
5841 struct btrfs_file_private
*private;
5843 private = kzalloc(sizeof(struct btrfs_file_private
), GFP_KERNEL
);
5846 private->filldir_buf
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
5847 if (!private->filldir_buf
) {
5851 file
->private_data
= private;
5862 static int btrfs_filldir(void *addr
, int entries
, struct dir_context
*ctx
)
5865 struct dir_entry
*entry
= addr
;
5866 char *name
= (char *)(entry
+ 1);
5868 ctx
->pos
= get_unaligned(&entry
->offset
);
5869 if (!dir_emit(ctx
, name
, get_unaligned(&entry
->name_len
),
5870 get_unaligned(&entry
->ino
),
5871 get_unaligned(&entry
->type
)))
5873 addr
+= sizeof(struct dir_entry
) +
5874 get_unaligned(&entry
->name_len
);
5880 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
5882 struct inode
*inode
= file_inode(file
);
5883 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5884 struct btrfs_file_private
*private = file
->private_data
;
5885 struct btrfs_dir_item
*di
;
5886 struct btrfs_key key
;
5887 struct btrfs_key found_key
;
5888 struct btrfs_path
*path
;
5890 struct list_head ins_list
;
5891 struct list_head del_list
;
5893 struct extent_buffer
*leaf
;
5900 struct btrfs_key location
;
5902 if (!dir_emit_dots(file
, ctx
))
5905 path
= btrfs_alloc_path();
5909 addr
= private->filldir_buf
;
5910 path
->reada
= READA_FORWARD
;
5912 INIT_LIST_HEAD(&ins_list
);
5913 INIT_LIST_HEAD(&del_list
);
5914 put
= btrfs_readdir_get_delayed_items(inode
, &ins_list
, &del_list
);
5917 key
.type
= BTRFS_DIR_INDEX_KEY
;
5918 key
.offset
= ctx
->pos
;
5919 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
5921 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5926 struct dir_entry
*entry
;
5928 leaf
= path
->nodes
[0];
5929 slot
= path
->slots
[0];
5930 if (slot
>= btrfs_header_nritems(leaf
)) {
5931 ret
= btrfs_next_leaf(root
, path
);
5939 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5941 if (found_key
.objectid
!= key
.objectid
)
5943 if (found_key
.type
!= BTRFS_DIR_INDEX_KEY
)
5945 if (found_key
.offset
< ctx
->pos
)
5947 if (btrfs_should_delete_dir_index(&del_list
, found_key
.offset
))
5949 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5950 name_len
= btrfs_dir_name_len(leaf
, di
);
5951 if ((total_len
+ sizeof(struct dir_entry
) + name_len
) >=
5953 btrfs_release_path(path
);
5954 ret
= btrfs_filldir(private->filldir_buf
, entries
, ctx
);
5957 addr
= private->filldir_buf
;
5964 put_unaligned(name_len
, &entry
->name_len
);
5965 name_ptr
= (char *)(entry
+ 1);
5966 read_extent_buffer(leaf
, name_ptr
, (unsigned long)(di
+ 1),
5968 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf
, di
)),
5970 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5971 put_unaligned(location
.objectid
, &entry
->ino
);
5972 put_unaligned(found_key
.offset
, &entry
->offset
);
5974 addr
+= sizeof(struct dir_entry
) + name_len
;
5975 total_len
+= sizeof(struct dir_entry
) + name_len
;
5979 btrfs_release_path(path
);
5981 ret
= btrfs_filldir(private->filldir_buf
, entries
, ctx
);
5985 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
);
5990 * Stop new entries from being returned after we return the last
5993 * New directory entries are assigned a strictly increasing
5994 * offset. This means that new entries created during readdir
5995 * are *guaranteed* to be seen in the future by that readdir.
5996 * This has broken buggy programs which operate on names as
5997 * they're returned by readdir. Until we re-use freed offsets
5998 * we have this hack to stop new entries from being returned
5999 * under the assumption that they'll never reach this huge
6002 * This is being careful not to overflow 32bit loff_t unless the
6003 * last entry requires it because doing so has broken 32bit apps
6006 if (ctx
->pos
>= INT_MAX
)
6007 ctx
->pos
= LLONG_MAX
;
6014 btrfs_readdir_put_delayed_items(inode
, &ins_list
, &del_list
);
6015 btrfs_free_path(path
);
6020 * This is somewhat expensive, updating the tree every time the
6021 * inode changes. But, it is most likely to find the inode in cache.
6022 * FIXME, needs more benchmarking...there are no reasons other than performance
6023 * to keep or drop this code.
6025 static int btrfs_dirty_inode(struct inode
*inode
)
6027 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
6028 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6029 struct btrfs_trans_handle
*trans
;
6032 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
6035 trans
= btrfs_join_transaction(root
);
6037 return PTR_ERR(trans
);
6039 ret
= btrfs_update_inode(trans
, root
, inode
);
6040 if (ret
&& ret
== -ENOSPC
) {
6041 /* whoops, lets try again with the full transaction */
6042 btrfs_end_transaction(trans
);
6043 trans
= btrfs_start_transaction(root
, 1);
6045 return PTR_ERR(trans
);
6047 ret
= btrfs_update_inode(trans
, root
, inode
);
6049 btrfs_end_transaction(trans
);
6050 if (BTRFS_I(inode
)->delayed_node
)
6051 btrfs_balance_delayed_items(fs_info
);
6057 * This is a copy of file_update_time. We need this so we can return error on
6058 * ENOSPC for updating the inode in the case of file write and mmap writes.
6060 static int btrfs_update_time(struct inode
*inode
, struct timespec64
*now
,
6063 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6064 bool dirty
= flags
& ~S_VERSION
;
6066 if (btrfs_root_readonly(root
))
6069 if (flags
& S_VERSION
)
6070 dirty
|= inode_maybe_inc_iversion(inode
, dirty
);
6071 if (flags
& S_CTIME
)
6072 inode
->i_ctime
= *now
;
6073 if (flags
& S_MTIME
)
6074 inode
->i_mtime
= *now
;
6075 if (flags
& S_ATIME
)
6076 inode
->i_atime
= *now
;
6077 return dirty
? btrfs_dirty_inode(inode
) : 0;
6081 * find the highest existing sequence number in a directory
6082 * and then set the in-memory index_cnt variable to reflect
6083 * free sequence numbers
6085 static int btrfs_set_inode_index_count(struct btrfs_inode
*inode
)
6087 struct btrfs_root
*root
= inode
->root
;
6088 struct btrfs_key key
, found_key
;
6089 struct btrfs_path
*path
;
6090 struct extent_buffer
*leaf
;
6093 key
.objectid
= btrfs_ino(inode
);
6094 key
.type
= BTRFS_DIR_INDEX_KEY
;
6095 key
.offset
= (u64
)-1;
6097 path
= btrfs_alloc_path();
6101 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6104 /* FIXME: we should be able to handle this */
6110 * MAGIC NUMBER EXPLANATION:
6111 * since we search a directory based on f_pos we have to start at 2
6112 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6113 * else has to start at 2
6115 if (path
->slots
[0] == 0) {
6116 inode
->index_cnt
= 2;
6122 leaf
= path
->nodes
[0];
6123 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6125 if (found_key
.objectid
!= btrfs_ino(inode
) ||
6126 found_key
.type
!= BTRFS_DIR_INDEX_KEY
) {
6127 inode
->index_cnt
= 2;
6131 inode
->index_cnt
= found_key
.offset
+ 1;
6133 btrfs_free_path(path
);
6138 * helper to find a free sequence number in a given directory. This current
6139 * code is very simple, later versions will do smarter things in the btree
6141 int btrfs_set_inode_index(struct btrfs_inode
*dir
, u64
*index
)
6145 if (dir
->index_cnt
== (u64
)-1) {
6146 ret
= btrfs_inode_delayed_dir_index_count(dir
);
6148 ret
= btrfs_set_inode_index_count(dir
);
6154 *index
= dir
->index_cnt
;
6160 static int btrfs_insert_inode_locked(struct inode
*inode
)
6162 struct btrfs_iget_args args
;
6163 args
.location
= &BTRFS_I(inode
)->location
;
6164 args
.root
= BTRFS_I(inode
)->root
;
6166 return insert_inode_locked4(inode
,
6167 btrfs_inode_hash(inode
->i_ino
, BTRFS_I(inode
)->root
),
6168 btrfs_find_actor
, &args
);
6172 * Inherit flags from the parent inode.
6174 * Currently only the compression flags and the cow flags are inherited.
6176 static void btrfs_inherit_iflags(struct inode
*inode
, struct inode
*dir
)
6183 flags
= BTRFS_I(dir
)->flags
;
6185 if (flags
& BTRFS_INODE_NOCOMPRESS
) {
6186 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_COMPRESS
;
6187 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
6188 } else if (flags
& BTRFS_INODE_COMPRESS
) {
6189 BTRFS_I(inode
)->flags
&= ~BTRFS_INODE_NOCOMPRESS
;
6190 BTRFS_I(inode
)->flags
|= BTRFS_INODE_COMPRESS
;
6193 if (flags
& BTRFS_INODE_NODATACOW
) {
6194 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
;
6195 if (S_ISREG(inode
->i_mode
))
6196 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
6199 btrfs_sync_inode_flags_to_i_flags(inode
);
6202 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
6203 struct btrfs_root
*root
,
6205 const char *name
, int name_len
,
6206 u64 ref_objectid
, u64 objectid
,
6207 umode_t mode
, u64
*index
)
6209 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6210 struct inode
*inode
;
6211 struct btrfs_inode_item
*inode_item
;
6212 struct btrfs_key
*location
;
6213 struct btrfs_path
*path
;
6214 struct btrfs_inode_ref
*ref
;
6215 struct btrfs_key key
[2];
6217 int nitems
= name
? 2 : 1;
6221 path
= btrfs_alloc_path();
6223 return ERR_PTR(-ENOMEM
);
6225 inode
= new_inode(fs_info
->sb
);
6227 btrfs_free_path(path
);
6228 return ERR_PTR(-ENOMEM
);
6232 * O_TMPFILE, set link count to 0, so that after this point,
6233 * we fill in an inode item with the correct link count.
6236 set_nlink(inode
, 0);
6239 * we have to initialize this early, so we can reclaim the inode
6240 * number if we fail afterwards in this function.
6242 inode
->i_ino
= objectid
;
6245 trace_btrfs_inode_request(dir
);
6247 ret
= btrfs_set_inode_index(BTRFS_I(dir
), index
);
6249 btrfs_free_path(path
);
6251 return ERR_PTR(ret
);
6257 * index_cnt is ignored for everything but a dir,
6258 * btrfs_set_inode_index_count has an explanation for the magic
6261 BTRFS_I(inode
)->index_cnt
= 2;
6262 BTRFS_I(inode
)->dir_index
= *index
;
6263 BTRFS_I(inode
)->root
= root
;
6264 BTRFS_I(inode
)->generation
= trans
->transid
;
6265 inode
->i_generation
= BTRFS_I(inode
)->generation
;
6268 * We could have gotten an inode number from somebody who was fsynced
6269 * and then removed in this same transaction, so let's just set full
6270 * sync since it will be a full sync anyway and this will blow away the
6271 * old info in the log.
6273 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
6275 key
[0].objectid
= objectid
;
6276 key
[0].type
= BTRFS_INODE_ITEM_KEY
;
6279 sizes
[0] = sizeof(struct btrfs_inode_item
);
6283 * Start new inodes with an inode_ref. This is slightly more
6284 * efficient for small numbers of hard links since they will
6285 * be packed into one item. Extended refs will kick in if we
6286 * add more hard links than can fit in the ref item.
6288 key
[1].objectid
= objectid
;
6289 key
[1].type
= BTRFS_INODE_REF_KEY
;
6290 key
[1].offset
= ref_objectid
;
6292 sizes
[1] = name_len
+ sizeof(*ref
);
6295 location
= &BTRFS_I(inode
)->location
;
6296 location
->objectid
= objectid
;
6297 location
->offset
= 0;
6298 location
->type
= BTRFS_INODE_ITEM_KEY
;
6300 ret
= btrfs_insert_inode_locked(inode
);
6306 path
->leave_spinning
= 1;
6307 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, nitems
);
6311 inode_init_owner(inode
, dir
, mode
);
6312 inode_set_bytes(inode
, 0);
6314 inode
->i_mtime
= current_time(inode
);
6315 inode
->i_atime
= inode
->i_mtime
;
6316 inode
->i_ctime
= inode
->i_mtime
;
6317 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
6319 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
6320 struct btrfs_inode_item
);
6321 memzero_extent_buffer(path
->nodes
[0], (unsigned long)inode_item
,
6322 sizeof(*inode_item
));
6323 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
6326 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
6327 struct btrfs_inode_ref
);
6328 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
6329 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
6330 ptr
= (unsigned long)(ref
+ 1);
6331 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
6334 btrfs_mark_buffer_dirty(path
->nodes
[0]);
6335 btrfs_free_path(path
);
6337 btrfs_inherit_iflags(inode
, dir
);
6339 if (S_ISREG(mode
)) {
6340 if (btrfs_test_opt(fs_info
, NODATASUM
))
6341 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
6342 if (btrfs_test_opt(fs_info
, NODATACOW
))
6343 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
6344 BTRFS_INODE_NODATASUM
;
6347 inode_tree_add(inode
);
6349 trace_btrfs_inode_new(inode
);
6350 btrfs_set_inode_last_trans(trans
, inode
);
6352 btrfs_update_root_times(trans
, root
);
6354 ret
= btrfs_inode_inherit_props(trans
, inode
, dir
);
6357 "error inheriting props for ino %llu (root %llu): %d",
6358 btrfs_ino(BTRFS_I(inode
)), root
->root_key
.objectid
, ret
);
6363 discard_new_inode(inode
);
6366 BTRFS_I(dir
)->index_cnt
--;
6367 btrfs_free_path(path
);
6368 return ERR_PTR(ret
);
6372 * utility function to add 'inode' into 'parent_inode' with
6373 * a give name and a given sequence number.
6374 * if 'add_backref' is true, also insert a backref from the
6375 * inode to the parent directory.
6377 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
6378 struct btrfs_inode
*parent_inode
, struct btrfs_inode
*inode
,
6379 const char *name
, int name_len
, int add_backref
, u64 index
)
6382 struct btrfs_key key
;
6383 struct btrfs_root
*root
= parent_inode
->root
;
6384 u64 ino
= btrfs_ino(inode
);
6385 u64 parent_ino
= btrfs_ino(parent_inode
);
6387 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6388 memcpy(&key
, &inode
->root
->root_key
, sizeof(key
));
6391 key
.type
= BTRFS_INODE_ITEM_KEY
;
6395 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6396 ret
= btrfs_add_root_ref(trans
, key
.objectid
,
6397 root
->root_key
.objectid
, parent_ino
,
6398 index
, name
, name_len
);
6399 } else if (add_backref
) {
6400 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
6404 /* Nothing to clean up yet */
6408 ret
= btrfs_insert_dir_item(trans
, name
, name_len
, parent_inode
, &key
,
6409 btrfs_inode_type(&inode
->vfs_inode
), index
);
6410 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
6413 btrfs_abort_transaction(trans
, ret
);
6417 btrfs_i_size_write(parent_inode
, parent_inode
->vfs_inode
.i_size
+
6419 inode_inc_iversion(&parent_inode
->vfs_inode
);
6420 parent_inode
->vfs_inode
.i_mtime
= parent_inode
->vfs_inode
.i_ctime
=
6421 current_time(&parent_inode
->vfs_inode
);
6422 ret
= btrfs_update_inode(trans
, root
, &parent_inode
->vfs_inode
);
6424 btrfs_abort_transaction(trans
, ret
);
6428 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6431 err
= btrfs_del_root_ref(trans
, key
.objectid
,
6432 root
->root_key
.objectid
, parent_ino
,
6433 &local_index
, name
, name_len
);
6435 btrfs_abort_transaction(trans
, err
);
6436 } else if (add_backref
) {
6440 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
6441 ino
, parent_ino
, &local_index
);
6443 btrfs_abort_transaction(trans
, err
);
6446 /* Return the original error code */
6450 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
6451 struct btrfs_inode
*dir
, struct dentry
*dentry
,
6452 struct btrfs_inode
*inode
, int backref
, u64 index
)
6454 int err
= btrfs_add_link(trans
, dir
, inode
,
6455 dentry
->d_name
.name
, dentry
->d_name
.len
,
6462 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
6463 umode_t mode
, dev_t rdev
)
6465 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6466 struct btrfs_trans_handle
*trans
;
6467 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6468 struct inode
*inode
= NULL
;
6474 * 2 for inode item and ref
6476 * 1 for xattr if selinux is on
6478 trans
= btrfs_start_transaction(root
, 5);
6480 return PTR_ERR(trans
);
6482 err
= btrfs_find_free_ino(root
, &objectid
);
6486 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6487 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)), objectid
,
6489 if (IS_ERR(inode
)) {
6490 err
= PTR_ERR(inode
);
6496 * If the active LSM wants to access the inode during
6497 * d_instantiate it needs these. Smack checks to see
6498 * if the filesystem supports xattrs by looking at the
6501 inode
->i_op
= &btrfs_special_inode_operations
;
6502 init_special_inode(inode
, inode
->i_mode
, rdev
);
6504 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6508 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
, BTRFS_I(inode
),
6513 btrfs_update_inode(trans
, root
, inode
);
6514 d_instantiate_new(dentry
, inode
);
6517 btrfs_end_transaction(trans
);
6518 btrfs_btree_balance_dirty(fs_info
);
6520 inode_dec_link_count(inode
);
6521 discard_new_inode(inode
);
6526 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
6527 umode_t mode
, bool excl
)
6529 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6530 struct btrfs_trans_handle
*trans
;
6531 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6532 struct inode
*inode
= NULL
;
6538 * 2 for inode item and ref
6540 * 1 for xattr if selinux is on
6542 trans
= btrfs_start_transaction(root
, 5);
6544 return PTR_ERR(trans
);
6546 err
= btrfs_find_free_ino(root
, &objectid
);
6550 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6551 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)), objectid
,
6553 if (IS_ERR(inode
)) {
6554 err
= PTR_ERR(inode
);
6559 * If the active LSM wants to access the inode during
6560 * d_instantiate it needs these. Smack checks to see
6561 * if the filesystem supports xattrs by looking at the
6564 inode
->i_fop
= &btrfs_file_operations
;
6565 inode
->i_op
= &btrfs_file_inode_operations
;
6566 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6568 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6572 err
= btrfs_update_inode(trans
, root
, inode
);
6576 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
, BTRFS_I(inode
),
6581 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6582 d_instantiate_new(dentry
, inode
);
6585 btrfs_end_transaction(trans
);
6587 inode_dec_link_count(inode
);
6588 discard_new_inode(inode
);
6590 btrfs_btree_balance_dirty(fs_info
);
6594 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
6595 struct dentry
*dentry
)
6597 struct btrfs_trans_handle
*trans
= NULL
;
6598 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6599 struct inode
*inode
= d_inode(old_dentry
);
6600 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
6605 /* do not allow sys_link's with other subvols of the same device */
6606 if (root
->root_key
.objectid
!= BTRFS_I(inode
)->root
->root_key
.objectid
)
6609 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
6612 err
= btrfs_set_inode_index(BTRFS_I(dir
), &index
);
6617 * 2 items for inode and inode ref
6618 * 2 items for dir items
6619 * 1 item for parent inode
6620 * 1 item for orphan item deletion if O_TMPFILE
6622 trans
= btrfs_start_transaction(root
, inode
->i_nlink
? 5 : 6);
6623 if (IS_ERR(trans
)) {
6624 err
= PTR_ERR(trans
);
6629 /* There are several dir indexes for this inode, clear the cache. */
6630 BTRFS_I(inode
)->dir_index
= 0ULL;
6632 inode_inc_iversion(inode
);
6633 inode
->i_ctime
= current_time(inode
);
6635 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
6637 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
, BTRFS_I(inode
),
6643 struct dentry
*parent
= dentry
->d_parent
;
6646 err
= btrfs_update_inode(trans
, root
, inode
);
6649 if (inode
->i_nlink
== 1) {
6651 * If new hard link count is 1, it's a file created
6652 * with open(2) O_TMPFILE flag.
6654 err
= btrfs_orphan_del(trans
, BTRFS_I(inode
));
6658 BTRFS_I(inode
)->last_link_trans
= trans
->transid
;
6659 d_instantiate(dentry
, inode
);
6660 ret
= btrfs_log_new_name(trans
, BTRFS_I(inode
), NULL
, parent
,
6662 if (ret
== BTRFS_NEED_TRANS_COMMIT
) {
6663 err
= btrfs_commit_transaction(trans
);
6670 btrfs_end_transaction(trans
);
6672 inode_dec_link_count(inode
);
6675 btrfs_btree_balance_dirty(fs_info
);
6679 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
6681 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6682 struct inode
*inode
= NULL
;
6683 struct btrfs_trans_handle
*trans
;
6684 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6690 * 2 items for inode and ref
6691 * 2 items for dir items
6692 * 1 for xattr if selinux is on
6694 trans
= btrfs_start_transaction(root
, 5);
6696 return PTR_ERR(trans
);
6698 err
= btrfs_find_free_ino(root
, &objectid
);
6702 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6703 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)), objectid
,
6704 S_IFDIR
| mode
, &index
);
6705 if (IS_ERR(inode
)) {
6706 err
= PTR_ERR(inode
);
6711 /* these must be set before we unlock the inode */
6712 inode
->i_op
= &btrfs_dir_inode_operations
;
6713 inode
->i_fop
= &btrfs_dir_file_operations
;
6715 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6719 btrfs_i_size_write(BTRFS_I(inode
), 0);
6720 err
= btrfs_update_inode(trans
, root
, inode
);
6724 err
= btrfs_add_link(trans
, BTRFS_I(dir
), BTRFS_I(inode
),
6725 dentry
->d_name
.name
,
6726 dentry
->d_name
.len
, 0, index
);
6730 d_instantiate_new(dentry
, inode
);
6733 btrfs_end_transaction(trans
);
6735 inode_dec_link_count(inode
);
6736 discard_new_inode(inode
);
6738 btrfs_btree_balance_dirty(fs_info
);
6742 static noinline
int uncompress_inline(struct btrfs_path
*path
,
6744 size_t pg_offset
, u64 extent_offset
,
6745 struct btrfs_file_extent_item
*item
)
6748 struct extent_buffer
*leaf
= path
->nodes
[0];
6751 unsigned long inline_size
;
6755 WARN_ON(pg_offset
!= 0);
6756 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6757 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6758 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6759 btrfs_item_nr(path
->slots
[0]));
6760 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6763 ptr
= btrfs_file_extent_inline_start(item
);
6765 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6767 max_size
= min_t(unsigned long, PAGE_SIZE
, max_size
);
6768 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6769 extent_offset
, inline_size
, max_size
);
6772 * decompression code contains a memset to fill in any space between the end
6773 * of the uncompressed data and the end of max_size in case the decompressed
6774 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6775 * the end of an inline extent and the beginning of the next block, so we
6776 * cover that region here.
6779 if (max_size
+ pg_offset
< PAGE_SIZE
) {
6780 char *map
= kmap(page
);
6781 memset(map
+ pg_offset
+ max_size
, 0, PAGE_SIZE
- max_size
- pg_offset
);
6789 * a bit scary, this does extent mapping from logical file offset to the disk.
6790 * the ugly parts come from merging extents from the disk with the in-ram
6791 * representation. This gets more complex because of the data=ordered code,
6792 * where the in-ram extents might be locked pending data=ordered completion.
6794 * This also copies inline extents directly into the page.
6796 struct extent_map
*btrfs_get_extent(struct btrfs_inode
*inode
,
6798 size_t pg_offset
, u64 start
, u64 len
,
6801 struct btrfs_fs_info
*fs_info
= inode
->root
->fs_info
;
6804 u64 extent_start
= 0;
6806 u64 objectid
= btrfs_ino(inode
);
6808 struct btrfs_path
*path
= NULL
;
6809 struct btrfs_root
*root
= inode
->root
;
6810 struct btrfs_file_extent_item
*item
;
6811 struct extent_buffer
*leaf
;
6812 struct btrfs_key found_key
;
6813 struct extent_map
*em
= NULL
;
6814 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
6815 struct extent_io_tree
*io_tree
= &inode
->io_tree
;
6816 const bool new_inline
= !page
|| create
;
6818 read_lock(&em_tree
->lock
);
6819 em
= lookup_extent_mapping(em_tree
, start
, len
);
6821 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
6822 read_unlock(&em_tree
->lock
);
6825 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6826 free_extent_map(em
);
6827 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6828 free_extent_map(em
);
6832 em
= alloc_extent_map();
6837 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
6838 em
->start
= EXTENT_MAP_HOLE
;
6839 em
->orig_start
= EXTENT_MAP_HOLE
;
6841 em
->block_len
= (u64
)-1;
6843 path
= btrfs_alloc_path();
6849 /* Chances are we'll be called again, so go ahead and do readahead */
6850 path
->reada
= READA_FORWARD
;
6853 * Unless we're going to uncompress the inline extent, no sleep would
6856 path
->leave_spinning
= 1;
6858 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, objectid
, start
, 0);
6862 } else if (ret
> 0) {
6863 if (path
->slots
[0] == 0)
6868 leaf
= path
->nodes
[0];
6869 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6870 struct btrfs_file_extent_item
);
6871 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6872 if (found_key
.objectid
!= objectid
||
6873 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6875 * If we backup past the first extent we want to move forward
6876 * and see if there is an extent in front of us, otherwise we'll
6877 * say there is a hole for our whole search range which can
6884 extent_type
= btrfs_file_extent_type(leaf
, item
);
6885 extent_start
= found_key
.offset
;
6886 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
6887 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6888 /* Only regular file could have regular/prealloc extent */
6889 if (!S_ISREG(inode
->vfs_inode
.i_mode
)) {
6892 "regular/prealloc extent found for non-regular inode %llu",
6896 extent_end
= extent_start
+
6897 btrfs_file_extent_num_bytes(leaf
, item
);
6899 trace_btrfs_get_extent_show_fi_regular(inode
, leaf
, item
,
6901 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
6904 size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6905 extent_end
= ALIGN(extent_start
+ size
,
6906 fs_info
->sectorsize
);
6908 trace_btrfs_get_extent_show_fi_inline(inode
, leaf
, item
,
6913 if (start
>= extent_end
) {
6915 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6916 ret
= btrfs_next_leaf(root
, path
);
6920 } else if (ret
> 0) {
6923 leaf
= path
->nodes
[0];
6925 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6926 if (found_key
.objectid
!= objectid
||
6927 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6929 if (start
+ len
<= found_key
.offset
)
6931 if (start
> found_key
.offset
)
6934 /* New extent overlaps with existing one */
6936 em
->orig_start
= start
;
6937 em
->len
= found_key
.offset
- start
;
6938 em
->block_start
= EXTENT_MAP_HOLE
;
6942 btrfs_extent_item_to_extent_map(inode
, path
, item
,
6945 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
6946 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6948 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
6952 size_t extent_offset
;
6958 size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6959 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6960 copy_size
= min_t(u64
, PAGE_SIZE
- pg_offset
,
6961 size
- extent_offset
);
6962 em
->start
= extent_start
+ extent_offset
;
6963 em
->len
= ALIGN(copy_size
, fs_info
->sectorsize
);
6964 em
->orig_block_len
= em
->len
;
6965 em
->orig_start
= em
->start
;
6966 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6968 btrfs_set_path_blocking(path
);
6969 if (!PageUptodate(page
)) {
6970 if (btrfs_file_extent_compression(leaf
, item
) !=
6971 BTRFS_COMPRESS_NONE
) {
6972 ret
= uncompress_inline(path
, page
, pg_offset
,
6973 extent_offset
, item
);
6980 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6982 if (pg_offset
+ copy_size
< PAGE_SIZE
) {
6983 memset(map
+ pg_offset
+ copy_size
, 0,
6984 PAGE_SIZE
- pg_offset
-
6989 flush_dcache_page(page
);
6991 set_extent_uptodate(io_tree
, em
->start
,
6992 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6997 em
->orig_start
= start
;
6999 em
->block_start
= EXTENT_MAP_HOLE
;
7001 btrfs_release_path(path
);
7002 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
7004 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7005 em
->start
, em
->len
, start
, len
);
7011 write_lock(&em_tree
->lock
);
7012 err
= btrfs_add_extent_mapping(fs_info
, em_tree
, &em
, start
, len
);
7013 write_unlock(&em_tree
->lock
);
7015 btrfs_free_path(path
);
7017 trace_btrfs_get_extent(root
, inode
, em
);
7020 free_extent_map(em
);
7021 return ERR_PTR(err
);
7023 BUG_ON(!em
); /* Error is always set */
7027 struct extent_map
*btrfs_get_extent_fiemap(struct btrfs_inode
*inode
,
7030 struct extent_map
*em
;
7031 struct extent_map
*hole_em
= NULL
;
7032 u64 delalloc_start
= start
;
7038 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
7042 * If our em maps to:
7044 * - a pre-alloc extent,
7045 * there might actually be delalloc bytes behind it.
7047 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
7048 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7053 /* check to see if we've wrapped (len == -1 or similar) */
7062 /* ok, we didn't find anything, lets look for delalloc */
7063 delalloc_len
= count_range_bits(&inode
->io_tree
, &delalloc_start
,
7064 end
, len
, EXTENT_DELALLOC
, 1);
7065 delalloc_end
= delalloc_start
+ delalloc_len
;
7066 if (delalloc_end
< delalloc_start
)
7067 delalloc_end
= (u64
)-1;
7070 * We didn't find anything useful, return the original results from
7073 if (delalloc_start
> end
|| delalloc_end
<= start
) {
7080 * Adjust the delalloc_start to make sure it doesn't go backwards from
7081 * the start they passed in
7083 delalloc_start
= max(start
, delalloc_start
);
7084 delalloc_len
= delalloc_end
- delalloc_start
;
7086 if (delalloc_len
> 0) {
7089 const u64 hole_end
= extent_map_end(hole_em
);
7091 em
= alloc_extent_map();
7100 * When btrfs_get_extent can't find anything it returns one
7103 * Make sure what it found really fits our range, and adjust to
7104 * make sure it is based on the start from the caller
7106 if (hole_end
<= start
|| hole_em
->start
> end
) {
7107 free_extent_map(hole_em
);
7110 hole_start
= max(hole_em
->start
, start
);
7111 hole_len
= hole_end
- hole_start
;
7114 if (hole_em
&& delalloc_start
> hole_start
) {
7116 * Our hole starts before our delalloc, so we have to
7117 * return just the parts of the hole that go until the
7120 em
->len
= min(hole_len
, delalloc_start
- hole_start
);
7121 em
->start
= hole_start
;
7122 em
->orig_start
= hole_start
;
7124 * Don't adjust block start at all, it is fixed at
7127 em
->block_start
= hole_em
->block_start
;
7128 em
->block_len
= hole_len
;
7129 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
7130 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
7133 * Hole is out of passed range or it starts after
7136 em
->start
= delalloc_start
;
7137 em
->len
= delalloc_len
;
7138 em
->orig_start
= delalloc_start
;
7139 em
->block_start
= EXTENT_MAP_DELALLOC
;
7140 em
->block_len
= delalloc_len
;
7147 free_extent_map(hole_em
);
7149 free_extent_map(em
);
7150 return ERR_PTR(err
);
7155 static struct extent_map
*btrfs_create_dio_extent(struct inode
*inode
,
7158 const u64 orig_start
,
7159 const u64 block_start
,
7160 const u64 block_len
,
7161 const u64 orig_block_len
,
7162 const u64 ram_bytes
,
7165 struct extent_map
*em
= NULL
;
7168 if (type
!= BTRFS_ORDERED_NOCOW
) {
7169 em
= create_io_em(inode
, start
, len
, orig_start
,
7170 block_start
, block_len
, orig_block_len
,
7172 BTRFS_COMPRESS_NONE
, /* compress_type */
7177 ret
= btrfs_add_ordered_extent_dio(inode
, start
, block_start
,
7178 len
, block_len
, type
);
7181 free_extent_map(em
);
7182 btrfs_drop_extent_cache(BTRFS_I(inode
), start
,
7183 start
+ len
- 1, 0);
7192 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
7195 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7196 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7197 struct extent_map
*em
;
7198 struct btrfs_key ins
;
7202 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
7203 ret
= btrfs_reserve_extent(root
, len
, len
, fs_info
->sectorsize
,
7204 0, alloc_hint
, &ins
, 1, 1);
7206 return ERR_PTR(ret
);
7208 em
= btrfs_create_dio_extent(inode
, start
, ins
.offset
, start
,
7209 ins
.objectid
, ins
.offset
, ins
.offset
,
7210 ins
.offset
, BTRFS_ORDERED_REGULAR
);
7211 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
7213 btrfs_free_reserved_extent(fs_info
, ins
.objectid
,
7220 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7221 * block must be cow'd
7223 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
7224 u64
*orig_start
, u64
*orig_block_len
,
7227 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7228 struct btrfs_path
*path
;
7230 struct extent_buffer
*leaf
;
7231 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7232 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7233 struct btrfs_file_extent_item
*fi
;
7234 struct btrfs_key key
;
7241 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
7243 path
= btrfs_alloc_path();
7247 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
7248 btrfs_ino(BTRFS_I(inode
)), offset
, 0);
7252 slot
= path
->slots
[0];
7255 /* can't find the item, must cow */
7262 leaf
= path
->nodes
[0];
7263 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
7264 if (key
.objectid
!= btrfs_ino(BTRFS_I(inode
)) ||
7265 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
7266 /* not our file or wrong item type, must cow */
7270 if (key
.offset
> offset
) {
7271 /* Wrong offset, must cow */
7275 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
7276 found_type
= btrfs_file_extent_type(leaf
, fi
);
7277 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
7278 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
7279 /* not a regular extent, must cow */
7283 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
7286 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
7287 if (extent_end
<= offset
)
7290 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
7291 if (disk_bytenr
== 0)
7294 if (btrfs_file_extent_compression(leaf
, fi
) ||
7295 btrfs_file_extent_encryption(leaf
, fi
) ||
7296 btrfs_file_extent_other_encoding(leaf
, fi
))
7300 * Do the same check as in btrfs_cross_ref_exist but without the
7301 * unnecessary search.
7303 if (btrfs_file_extent_generation(leaf
, fi
) <=
7304 btrfs_root_last_snapshot(&root
->root_item
))
7307 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
7310 *orig_start
= key
.offset
- backref_offset
;
7311 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
7312 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
7315 if (btrfs_extent_readonly(fs_info
, disk_bytenr
))
7318 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
7319 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
7322 range_end
= round_up(offset
+ num_bytes
,
7323 root
->fs_info
->sectorsize
) - 1;
7324 ret
= test_range_bit(io_tree
, offset
, range_end
,
7325 EXTENT_DELALLOC
, 0, NULL
);
7332 btrfs_release_path(path
);
7335 * look for other files referencing this extent, if we
7336 * find any we must cow
7339 ret
= btrfs_cross_ref_exist(root
, btrfs_ino(BTRFS_I(inode
)),
7340 key
.offset
- backref_offset
, disk_bytenr
);
7347 * adjust disk_bytenr and num_bytes to cover just the bytes
7348 * in this extent we are about to write. If there
7349 * are any csums in that range we have to cow in order
7350 * to keep the csums correct
7352 disk_bytenr
+= backref_offset
;
7353 disk_bytenr
+= offset
- key
.offset
;
7354 if (csum_exist_in_range(fs_info
, disk_bytenr
, num_bytes
))
7357 * all of the above have passed, it is safe to overwrite this extent
7363 btrfs_free_path(path
);
7367 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
7368 struct extent_state
**cached_state
, int writing
)
7370 struct btrfs_ordered_extent
*ordered
;
7374 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7377 * We're concerned with the entire range that we're going to be
7378 * doing DIO to, so we need to make sure there's no ordered
7379 * extents in this range.
7381 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), lockstart
,
7382 lockend
- lockstart
+ 1);
7385 * We need to make sure there are no buffered pages in this
7386 * range either, we could have raced between the invalidate in
7387 * generic_file_direct_write and locking the extent. The
7388 * invalidate needs to happen so that reads after a write do not
7392 (!writing
|| !filemap_range_has_page(inode
->i_mapping
,
7393 lockstart
, lockend
)))
7396 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7401 * If we are doing a DIO read and the ordered extent we
7402 * found is for a buffered write, we can not wait for it
7403 * to complete and retry, because if we do so we can
7404 * deadlock with concurrent buffered writes on page
7405 * locks. This happens only if our DIO read covers more
7406 * than one extent map, if at this point has already
7407 * created an ordered extent for a previous extent map
7408 * and locked its range in the inode's io tree, and a
7409 * concurrent write against that previous extent map's
7410 * range and this range started (we unlock the ranges
7411 * in the io tree only when the bios complete and
7412 * buffered writes always lock pages before attempting
7413 * to lock range in the io tree).
7416 test_bit(BTRFS_ORDERED_DIRECT
, &ordered
->flags
))
7417 btrfs_start_ordered_extent(inode
, ordered
, 1);
7420 btrfs_put_ordered_extent(ordered
);
7423 * We could trigger writeback for this range (and wait
7424 * for it to complete) and then invalidate the pages for
7425 * this range (through invalidate_inode_pages2_range()),
7426 * but that can lead us to a deadlock with a concurrent
7427 * call to readpages() (a buffered read or a defrag call
7428 * triggered a readahead) on a page lock due to an
7429 * ordered dio extent we created before but did not have
7430 * yet a corresponding bio submitted (whence it can not
7431 * complete), which makes readpages() wait for that
7432 * ordered extent to complete while holding a lock on
7447 /* The callers of this must take lock_extent() */
7448 static struct extent_map
*create_io_em(struct inode
*inode
, u64 start
, u64 len
,
7449 u64 orig_start
, u64 block_start
,
7450 u64 block_len
, u64 orig_block_len
,
7451 u64 ram_bytes
, int compress_type
,
7454 struct extent_map_tree
*em_tree
;
7455 struct extent_map
*em
;
7456 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7459 ASSERT(type
== BTRFS_ORDERED_PREALLOC
||
7460 type
== BTRFS_ORDERED_COMPRESSED
||
7461 type
== BTRFS_ORDERED_NOCOW
||
7462 type
== BTRFS_ORDERED_REGULAR
);
7464 em_tree
= &BTRFS_I(inode
)->extent_tree
;
7465 em
= alloc_extent_map();
7467 return ERR_PTR(-ENOMEM
);
7470 em
->orig_start
= orig_start
;
7472 em
->block_len
= block_len
;
7473 em
->block_start
= block_start
;
7474 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
7475 em
->orig_block_len
= orig_block_len
;
7476 em
->ram_bytes
= ram_bytes
;
7477 em
->generation
= -1;
7478 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
7479 if (type
== BTRFS_ORDERED_PREALLOC
) {
7480 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
7481 } else if (type
== BTRFS_ORDERED_COMPRESSED
) {
7482 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
7483 em
->compress_type
= compress_type
;
7487 btrfs_drop_extent_cache(BTRFS_I(inode
), em
->start
,
7488 em
->start
+ em
->len
- 1, 0);
7489 write_lock(&em_tree
->lock
);
7490 ret
= add_extent_mapping(em_tree
, em
, 1);
7491 write_unlock(&em_tree
->lock
);
7493 * The caller has taken lock_extent(), who could race with us
7496 } while (ret
== -EEXIST
);
7499 free_extent_map(em
);
7500 return ERR_PTR(ret
);
7503 /* em got 2 refs now, callers needs to do free_extent_map once. */
7508 static int btrfs_get_blocks_direct_read(struct extent_map
*em
,
7509 struct buffer_head
*bh_result
,
7510 struct inode
*inode
,
7513 if (em
->block_start
== EXTENT_MAP_HOLE
||
7514 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7517 len
= min(len
, em
->len
- (start
- em
->start
));
7519 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
7521 bh_result
->b_size
= len
;
7522 bh_result
->b_bdev
= em
->bdev
;
7523 set_buffer_mapped(bh_result
);
7528 static int btrfs_get_blocks_direct_write(struct extent_map
**map
,
7529 struct buffer_head
*bh_result
,
7530 struct inode
*inode
,
7531 struct btrfs_dio_data
*dio_data
,
7534 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7535 struct extent_map
*em
= *map
;
7539 * We don't allocate a new extent in the following cases
7541 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7543 * 2) The extent is marked as PREALLOC. We're good to go here and can
7544 * just use the extent.
7547 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
7548 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
7549 em
->block_start
!= EXTENT_MAP_HOLE
)) {
7551 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
7553 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7554 type
= BTRFS_ORDERED_PREALLOC
;
7556 type
= BTRFS_ORDERED_NOCOW
;
7557 len
= min(len
, em
->len
- (start
- em
->start
));
7558 block_start
= em
->block_start
+ (start
- em
->start
);
7560 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
7561 &orig_block_len
, &ram_bytes
) == 1 &&
7562 btrfs_inc_nocow_writers(fs_info
, block_start
)) {
7563 struct extent_map
*em2
;
7565 em2
= btrfs_create_dio_extent(inode
, start
, len
,
7566 orig_start
, block_start
,
7567 len
, orig_block_len
,
7569 btrfs_dec_nocow_writers(fs_info
, block_start
);
7570 if (type
== BTRFS_ORDERED_PREALLOC
) {
7571 free_extent_map(em
);
7575 if (em2
&& IS_ERR(em2
)) {
7580 * For inode marked NODATACOW or extent marked PREALLOC,
7581 * use the existing or preallocated extent, so does not
7582 * need to adjust btrfs_space_info's bytes_may_use.
7584 btrfs_free_reserved_data_space_noquota(inode
, start
,
7590 /* this will cow the extent */
7591 len
= bh_result
->b_size
;
7592 free_extent_map(em
);
7593 *map
= em
= btrfs_new_extent_direct(inode
, start
, len
);
7599 len
= min(len
, em
->len
- (start
- em
->start
));
7602 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
7604 bh_result
->b_size
= len
;
7605 bh_result
->b_bdev
= em
->bdev
;
7606 set_buffer_mapped(bh_result
);
7608 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7609 set_buffer_new(bh_result
);
7612 * Need to update the i_size under the extent lock so buffered
7613 * readers will get the updated i_size when we unlock.
7615 if (!dio_data
->overwrite
&& start
+ len
> i_size_read(inode
))
7616 i_size_write(inode
, start
+ len
);
7618 WARN_ON(dio_data
->reserve
< len
);
7619 dio_data
->reserve
-= len
;
7620 dio_data
->unsubmitted_oe_range_end
= start
+ len
;
7621 current
->journal_info
= dio_data
;
7626 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
7627 struct buffer_head
*bh_result
, int create
)
7629 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7630 struct extent_map
*em
;
7631 struct extent_state
*cached_state
= NULL
;
7632 struct btrfs_dio_data
*dio_data
= NULL
;
7633 u64 start
= iblock
<< inode
->i_blkbits
;
7634 u64 lockstart
, lockend
;
7635 u64 len
= bh_result
->b_size
;
7636 int unlock_bits
= EXTENT_LOCKED
;
7640 unlock_bits
|= EXTENT_DIRTY
;
7642 len
= min_t(u64
, len
, fs_info
->sectorsize
);
7645 lockend
= start
+ len
- 1;
7647 if (current
->journal_info
) {
7649 * Need to pull our outstanding extents and set journal_info to NULL so
7650 * that anything that needs to check if there's a transaction doesn't get
7653 dio_data
= current
->journal_info
;
7654 current
->journal_info
= NULL
;
7658 * If this errors out it's because we couldn't invalidate pagecache for
7659 * this range and we need to fallback to buffered.
7661 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
,
7667 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, start
, len
, 0);
7674 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7675 * io. INLINE is special, and we could probably kludge it in here, but
7676 * it's still buffered so for safety lets just fall back to the generic
7679 * For COMPRESSED we _have_ to read the entire extent in so we can
7680 * decompress it, so there will be buffering required no matter what we
7681 * do, so go ahead and fallback to buffered.
7683 * We return -ENOTBLK because that's what makes DIO go ahead and go back
7684 * to buffered IO. Don't blame me, this is the price we pay for using
7687 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
7688 em
->block_start
== EXTENT_MAP_INLINE
) {
7689 free_extent_map(em
);
7695 ret
= btrfs_get_blocks_direct_write(&em
, bh_result
, inode
,
7696 dio_data
, start
, len
);
7700 /* clear and unlock the entire range */
7701 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7702 unlock_bits
, 1, 0, &cached_state
);
7704 ret
= btrfs_get_blocks_direct_read(em
, bh_result
, inode
,
7706 /* Can be negative only if we read from a hole */
7709 free_extent_map(em
);
7713 * We need to unlock only the end area that we aren't using.
7714 * The rest is going to be unlocked by the endio routine.
7716 lockstart
= start
+ bh_result
->b_size
;
7717 if (lockstart
< lockend
) {
7718 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
7719 lockend
, unlock_bits
, 1, 0,
7722 free_extent_state(cached_state
);
7726 free_extent_map(em
);
7731 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7732 unlock_bits
, 1, 0, &cached_state
);
7735 current
->journal_info
= dio_data
;
7739 static inline blk_status_t
submit_dio_repair_bio(struct inode
*inode
,
7743 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7746 BUG_ON(bio_op(bio
) == REQ_OP_WRITE
);
7748 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, BTRFS_WQ_ENDIO_DIO_REPAIR
);
7752 ret
= btrfs_map_bio(fs_info
, bio
, mirror_num
, 0);
7757 static int btrfs_check_dio_repairable(struct inode
*inode
,
7758 struct bio
*failed_bio
,
7759 struct io_failure_record
*failrec
,
7762 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7765 num_copies
= btrfs_num_copies(fs_info
, failrec
->logical
, failrec
->len
);
7766 if (num_copies
== 1) {
7768 * we only have a single copy of the data, so don't bother with
7769 * all the retry and error correction code that follows. no
7770 * matter what the error is, it is very likely to persist.
7772 btrfs_debug(fs_info
,
7773 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7774 num_copies
, failrec
->this_mirror
, failed_mirror
);
7778 failrec
->failed_mirror
= failed_mirror
;
7779 failrec
->this_mirror
++;
7780 if (failrec
->this_mirror
== failed_mirror
)
7781 failrec
->this_mirror
++;
7783 if (failrec
->this_mirror
> num_copies
) {
7784 btrfs_debug(fs_info
,
7785 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7786 num_copies
, failrec
->this_mirror
, failed_mirror
);
7793 static blk_status_t
dio_read_error(struct inode
*inode
, struct bio
*failed_bio
,
7794 struct page
*page
, unsigned int pgoff
,
7795 u64 start
, u64 end
, int failed_mirror
,
7796 bio_end_io_t
*repair_endio
, void *repair_arg
)
7798 struct io_failure_record
*failrec
;
7799 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7800 struct extent_io_tree
*failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
7803 unsigned int read_mode
= 0;
7806 blk_status_t status
;
7807 struct bio_vec bvec
;
7809 BUG_ON(bio_op(failed_bio
) == REQ_OP_WRITE
);
7811 ret
= btrfs_get_io_failure_record(inode
, start
, end
, &failrec
);
7813 return errno_to_blk_status(ret
);
7815 ret
= btrfs_check_dio_repairable(inode
, failed_bio
, failrec
,
7818 free_io_failure(failure_tree
, io_tree
, failrec
);
7819 return BLK_STS_IOERR
;
7822 segs
= bio_segments(failed_bio
);
7823 bio_get_first_bvec(failed_bio
, &bvec
);
7825 (bvec
.bv_len
> btrfs_inode_sectorsize(inode
)))
7826 read_mode
|= REQ_FAILFAST_DEV
;
7828 isector
= start
- btrfs_io_bio(failed_bio
)->logical
;
7829 isector
>>= inode
->i_sb
->s_blocksize_bits
;
7830 bio
= btrfs_create_repair_bio(inode
, failed_bio
, failrec
, page
,
7831 pgoff
, isector
, repair_endio
, repair_arg
);
7832 bio
->bi_opf
= REQ_OP_READ
| read_mode
;
7834 btrfs_debug(BTRFS_I(inode
)->root
->fs_info
,
7835 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
7836 read_mode
, failrec
->this_mirror
, failrec
->in_validation
);
7838 status
= submit_dio_repair_bio(inode
, bio
, failrec
->this_mirror
);
7840 free_io_failure(failure_tree
, io_tree
, failrec
);
7847 struct btrfs_retry_complete
{
7848 struct completion done
;
7849 struct inode
*inode
;
7854 static void btrfs_retry_endio_nocsum(struct bio
*bio
)
7856 struct btrfs_retry_complete
*done
= bio
->bi_private
;
7857 struct inode
*inode
= done
->inode
;
7858 struct bio_vec
*bvec
;
7859 struct extent_io_tree
*io_tree
, *failure_tree
;
7861 struct bvec_iter_all iter_all
;
7866 ASSERT(bio
->bi_vcnt
== 1);
7867 io_tree
= &BTRFS_I(inode
)->io_tree
;
7868 failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
7869 ASSERT(bio_first_bvec_all(bio
)->bv_len
== btrfs_inode_sectorsize(inode
));
7872 ASSERT(!bio_flagged(bio
, BIO_CLONED
));
7873 bio_for_each_segment_all(bvec
, bio
, i
, iter_all
)
7874 clean_io_failure(BTRFS_I(inode
)->root
->fs_info
, failure_tree
,
7875 io_tree
, done
->start
, bvec
->bv_page
,
7876 btrfs_ino(BTRFS_I(inode
)), 0);
7878 complete(&done
->done
);
7882 static blk_status_t
__btrfs_correct_data_nocsum(struct inode
*inode
,
7883 struct btrfs_io_bio
*io_bio
)
7885 struct btrfs_fs_info
*fs_info
;
7886 struct bio_vec bvec
;
7887 struct bvec_iter iter
;
7888 struct btrfs_retry_complete done
;
7894 blk_status_t err
= BLK_STS_OK
;
7896 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
7897 sectorsize
= fs_info
->sectorsize
;
7899 start
= io_bio
->logical
;
7901 io_bio
->bio
.bi_iter
= io_bio
->iter
;
7903 bio_for_each_segment(bvec
, &io_bio
->bio
, iter
) {
7904 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
.bv_len
);
7905 pgoff
= bvec
.bv_offset
;
7907 next_block_or_try_again
:
7910 init_completion(&done
.done
);
7912 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
.bv_page
,
7913 pgoff
, start
, start
+ sectorsize
- 1,
7915 btrfs_retry_endio_nocsum
, &done
);
7921 wait_for_completion_io(&done
.done
);
7923 if (!done
.uptodate
) {
7924 /* We might have another mirror, so try again */
7925 goto next_block_or_try_again
;
7929 start
+= sectorsize
;
7933 pgoff
+= sectorsize
;
7934 ASSERT(pgoff
< PAGE_SIZE
);
7935 goto next_block_or_try_again
;
7942 static void btrfs_retry_endio(struct bio
*bio
)
7944 struct btrfs_retry_complete
*done
= bio
->bi_private
;
7945 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
7946 struct extent_io_tree
*io_tree
, *failure_tree
;
7947 struct inode
*inode
= done
->inode
;
7948 struct bio_vec
*bvec
;
7952 struct bvec_iter_all iter_all
;
7959 ASSERT(bio
->bi_vcnt
== 1);
7960 ASSERT(bio_first_bvec_all(bio
)->bv_len
== btrfs_inode_sectorsize(done
->inode
));
7962 io_tree
= &BTRFS_I(inode
)->io_tree
;
7963 failure_tree
= &BTRFS_I(inode
)->io_failure_tree
;
7965 ASSERT(!bio_flagged(bio
, BIO_CLONED
));
7966 bio_for_each_segment_all(bvec
, bio
, i
, iter_all
) {
7967 ret
= __readpage_endio_check(inode
, io_bio
, i
, bvec
->bv_page
,
7968 bvec
->bv_offset
, done
->start
,
7971 clean_io_failure(BTRFS_I(inode
)->root
->fs_info
,
7972 failure_tree
, io_tree
, done
->start
,
7974 btrfs_ino(BTRFS_I(inode
)),
7980 done
->uptodate
= uptodate
;
7982 complete(&done
->done
);
7986 static blk_status_t
__btrfs_subio_endio_read(struct inode
*inode
,
7987 struct btrfs_io_bio
*io_bio
, blk_status_t err
)
7989 struct btrfs_fs_info
*fs_info
;
7990 struct bio_vec bvec
;
7991 struct bvec_iter iter
;
7992 struct btrfs_retry_complete done
;
7999 bool uptodate
= (err
== 0);
8001 blk_status_t status
;
8003 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
8004 sectorsize
= fs_info
->sectorsize
;
8007 start
= io_bio
->logical
;
8009 io_bio
->bio
.bi_iter
= io_bio
->iter
;
8011 bio_for_each_segment(bvec
, &io_bio
->bio
, iter
) {
8012 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
.bv_len
);
8014 pgoff
= bvec
.bv_offset
;
8017 csum_pos
= BTRFS_BYTES_TO_BLKS(fs_info
, offset
);
8018 ret
= __readpage_endio_check(inode
, io_bio
, csum_pos
,
8019 bvec
.bv_page
, pgoff
, start
, sectorsize
);
8026 init_completion(&done
.done
);
8028 status
= dio_read_error(inode
, &io_bio
->bio
, bvec
.bv_page
,
8029 pgoff
, start
, start
+ sectorsize
- 1,
8030 io_bio
->mirror_num
, btrfs_retry_endio
,
8037 wait_for_completion_io(&done
.done
);
8039 if (!done
.uptodate
) {
8040 /* We might have another mirror, so try again */
8044 offset
+= sectorsize
;
8045 start
+= sectorsize
;
8051 pgoff
+= sectorsize
;
8052 ASSERT(pgoff
< PAGE_SIZE
);
8060 static blk_status_t
btrfs_subio_endio_read(struct inode
*inode
,
8061 struct btrfs_io_bio
*io_bio
, blk_status_t err
)
8063 bool skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
8067 return __btrfs_correct_data_nocsum(inode
, io_bio
);
8071 return __btrfs_subio_endio_read(inode
, io_bio
, err
);
8075 static void btrfs_endio_direct_read(struct bio
*bio
)
8077 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8078 struct inode
*inode
= dip
->inode
;
8079 struct bio
*dio_bio
;
8080 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8081 blk_status_t err
= bio
->bi_status
;
8083 if (dip
->flags
& BTRFS_DIO_ORIG_BIO_SUBMITTED
)
8084 err
= btrfs_subio_endio_read(inode
, io_bio
, err
);
8086 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
8087 dip
->logical_offset
+ dip
->bytes
- 1);
8088 dio_bio
= dip
->dio_bio
;
8092 dio_bio
->bi_status
= err
;
8093 dio_end_io(dio_bio
);
8094 btrfs_io_bio_free_csum(io_bio
);
8098 static void __endio_write_update_ordered(struct inode
*inode
,
8099 const u64 offset
, const u64 bytes
,
8100 const bool uptodate
)
8102 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8103 struct btrfs_ordered_extent
*ordered
= NULL
;
8104 struct btrfs_workqueue
*wq
;
8105 btrfs_work_func_t func
;
8106 u64 ordered_offset
= offset
;
8107 u64 ordered_bytes
= bytes
;
8110 if (btrfs_is_free_space_inode(BTRFS_I(inode
))) {
8111 wq
= fs_info
->endio_freespace_worker
;
8112 func
= btrfs_freespace_write_helper
;
8114 wq
= fs_info
->endio_write_workers
;
8115 func
= btrfs_endio_write_helper
;
8118 while (ordered_offset
< offset
+ bytes
) {
8119 last_offset
= ordered_offset
;
8120 if (btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
8124 btrfs_init_work(&ordered
->work
, func
,
8127 btrfs_queue_work(wq
, &ordered
->work
);
8130 * If btrfs_dec_test_ordered_pending does not find any ordered
8131 * extent in the range, we can exit.
8133 if (ordered_offset
== last_offset
)
8136 * Our bio might span multiple ordered extents. In this case
8137 * we keep going until we have accounted the whole dio.
8139 if (ordered_offset
< offset
+ bytes
) {
8140 ordered_bytes
= offset
+ bytes
- ordered_offset
;
8146 static void btrfs_endio_direct_write(struct bio
*bio
)
8148 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8149 struct bio
*dio_bio
= dip
->dio_bio
;
8151 __endio_write_update_ordered(dip
->inode
, dip
->logical_offset
,
8152 dip
->bytes
, !bio
->bi_status
);
8156 dio_bio
->bi_status
= bio
->bi_status
;
8157 dio_end_io(dio_bio
);
8161 static blk_status_t
btrfs_submit_bio_start_direct_io(void *private_data
,
8162 struct bio
*bio
, u64 offset
)
8164 struct inode
*inode
= private_data
;
8166 ret
= btrfs_csum_one_bio(inode
, bio
, offset
, 1);
8167 BUG_ON(ret
); /* -ENOMEM */
8171 static void btrfs_end_dio_bio(struct bio
*bio
)
8173 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8174 blk_status_t err
= bio
->bi_status
;
8177 btrfs_warn(BTRFS_I(dip
->inode
)->root
->fs_info
,
8178 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
8179 btrfs_ino(BTRFS_I(dip
->inode
)), bio_op(bio
),
8181 (unsigned long long)bio
->bi_iter
.bi_sector
,
8182 bio
->bi_iter
.bi_size
, err
);
8184 if (dip
->subio_endio
)
8185 err
= dip
->subio_endio(dip
->inode
, btrfs_io_bio(bio
), err
);
8189 * We want to perceive the errors flag being set before
8190 * decrementing the reference count. We don't need a barrier
8191 * since atomic operations with a return value are fully
8192 * ordered as per atomic_t.txt
8197 /* if there are more bios still pending for this dio, just exit */
8198 if (!atomic_dec_and_test(&dip
->pending_bios
))
8202 bio_io_error(dip
->orig_bio
);
8204 dip
->dio_bio
->bi_status
= BLK_STS_OK
;
8205 bio_endio(dip
->orig_bio
);
8211 static inline blk_status_t
btrfs_lookup_and_bind_dio_csum(struct inode
*inode
,
8212 struct btrfs_dio_private
*dip
,
8216 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8217 struct btrfs_io_bio
*orig_io_bio
= btrfs_io_bio(dip
->orig_bio
);
8221 * We load all the csum data we need when we submit
8222 * the first bio to reduce the csum tree search and
8225 if (dip
->logical_offset
== file_offset
) {
8226 ret
= btrfs_lookup_bio_sums_dio(inode
, dip
->orig_bio
,
8232 if (bio
== dip
->orig_bio
)
8235 file_offset
-= dip
->logical_offset
;
8236 file_offset
>>= inode
->i_sb
->s_blocksize_bits
;
8237 io_bio
->csum
= (u8
*)(((u32
*)orig_io_bio
->csum
) + file_offset
);
8242 static inline blk_status_t
btrfs_submit_dio_bio(struct bio
*bio
,
8243 struct inode
*inode
, u64 file_offset
, int async_submit
)
8245 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8246 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8247 bool write
= bio_op(bio
) == REQ_OP_WRITE
;
8250 /* Check btrfs_submit_bio_hook() for rules about async submit. */
8252 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
8255 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, BTRFS_WQ_ENDIO_DATA
);
8260 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
8263 if (write
&& async_submit
) {
8264 ret
= btrfs_wq_submit_bio(fs_info
, bio
, 0, 0,
8266 btrfs_submit_bio_start_direct_io
);
8270 * If we aren't doing async submit, calculate the csum of the
8273 ret
= btrfs_csum_one_bio(inode
, bio
, file_offset
, 1);
8277 ret
= btrfs_lookup_and_bind_dio_csum(inode
, dip
, bio
,
8283 ret
= btrfs_map_bio(fs_info
, bio
, 0, 0);
8288 static int btrfs_submit_direct_hook(struct btrfs_dio_private
*dip
)
8290 struct inode
*inode
= dip
->inode
;
8291 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8293 struct bio
*orig_bio
= dip
->orig_bio
;
8294 u64 start_sector
= orig_bio
->bi_iter
.bi_sector
;
8295 u64 file_offset
= dip
->logical_offset
;
8297 int async_submit
= 0;
8299 int clone_offset
= 0;
8302 blk_status_t status
;
8304 map_length
= orig_bio
->bi_iter
.bi_size
;
8305 submit_len
= map_length
;
8306 ret
= btrfs_map_block(fs_info
, btrfs_op(orig_bio
), start_sector
<< 9,
8307 &map_length
, NULL
, 0);
8311 if (map_length
>= submit_len
) {
8313 dip
->flags
|= BTRFS_DIO_ORIG_BIO_SUBMITTED
;
8317 /* async crcs make it difficult to collect full stripe writes. */
8318 if (btrfs_data_alloc_profile(fs_info
) & BTRFS_BLOCK_GROUP_RAID56_MASK
)
8324 ASSERT(map_length
<= INT_MAX
);
8325 atomic_inc(&dip
->pending_bios
);
8327 clone_len
= min_t(int, submit_len
, map_length
);
8330 * This will never fail as it's passing GPF_NOFS and
8331 * the allocation is backed by btrfs_bioset.
8333 bio
= btrfs_bio_clone_partial(orig_bio
, clone_offset
,
8335 bio
->bi_private
= dip
;
8336 bio
->bi_end_io
= btrfs_end_dio_bio
;
8337 btrfs_io_bio(bio
)->logical
= file_offset
;
8339 ASSERT(submit_len
>= clone_len
);
8340 submit_len
-= clone_len
;
8341 if (submit_len
== 0)
8345 * Increase the count before we submit the bio so we know
8346 * the end IO handler won't happen before we increase the
8347 * count. Otherwise, the dip might get freed before we're
8348 * done setting it up.
8350 atomic_inc(&dip
->pending_bios
);
8352 status
= btrfs_submit_dio_bio(bio
, inode
, file_offset
,
8356 atomic_dec(&dip
->pending_bios
);
8360 clone_offset
+= clone_len
;
8361 start_sector
+= clone_len
>> 9;
8362 file_offset
+= clone_len
;
8364 map_length
= submit_len
;
8365 ret
= btrfs_map_block(fs_info
, btrfs_op(orig_bio
),
8366 start_sector
<< 9, &map_length
, NULL
, 0);
8369 } while (submit_len
> 0);
8372 status
= btrfs_submit_dio_bio(bio
, inode
, file_offset
, async_submit
);
8380 * Before atomic variable goto zero, we must make sure dip->errors is
8381 * perceived to be set. This ordering is ensured by the fact that an
8382 * atomic operations with a return value are fully ordered as per
8385 if (atomic_dec_and_test(&dip
->pending_bios
))
8386 bio_io_error(dip
->orig_bio
);
8388 /* bio_end_io() will handle error, so we needn't return it */
8392 static void btrfs_submit_direct(struct bio
*dio_bio
, struct inode
*inode
,
8395 struct btrfs_dio_private
*dip
= NULL
;
8396 struct bio
*bio
= NULL
;
8397 struct btrfs_io_bio
*io_bio
;
8398 bool write
= (bio_op(dio_bio
) == REQ_OP_WRITE
);
8401 bio
= btrfs_bio_clone(dio_bio
);
8403 dip
= kzalloc(sizeof(*dip
), GFP_NOFS
);
8409 dip
->private = dio_bio
->bi_private
;
8411 dip
->logical_offset
= file_offset
;
8412 dip
->bytes
= dio_bio
->bi_iter
.bi_size
;
8413 dip
->disk_bytenr
= (u64
)dio_bio
->bi_iter
.bi_sector
<< 9;
8414 bio
->bi_private
= dip
;
8415 dip
->orig_bio
= bio
;
8416 dip
->dio_bio
= dio_bio
;
8417 atomic_set(&dip
->pending_bios
, 0);
8418 io_bio
= btrfs_io_bio(bio
);
8419 io_bio
->logical
= file_offset
;
8422 bio
->bi_end_io
= btrfs_endio_direct_write
;
8424 bio
->bi_end_io
= btrfs_endio_direct_read
;
8425 dip
->subio_endio
= btrfs_subio_endio_read
;
8429 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8430 * even if we fail to submit a bio, because in such case we do the
8431 * corresponding error handling below and it must not be done a second
8432 * time by btrfs_direct_IO().
8435 struct btrfs_dio_data
*dio_data
= current
->journal_info
;
8437 dio_data
->unsubmitted_oe_range_end
= dip
->logical_offset
+
8439 dio_data
->unsubmitted_oe_range_start
=
8440 dio_data
->unsubmitted_oe_range_end
;
8443 ret
= btrfs_submit_direct_hook(dip
);
8447 btrfs_io_bio_free_csum(io_bio
);
8451 * If we arrived here it means either we failed to submit the dip
8452 * or we either failed to clone the dio_bio or failed to allocate the
8453 * dip. If we cloned the dio_bio and allocated the dip, we can just
8454 * call bio_endio against our io_bio so that we get proper resource
8455 * cleanup if we fail to submit the dip, otherwise, we must do the
8456 * same as btrfs_endio_direct_[write|read] because we can't call these
8457 * callbacks - they require an allocated dip and a clone of dio_bio.
8462 * The end io callbacks free our dip, do the final put on bio
8463 * and all the cleanup and final put for dio_bio (through
8470 __endio_write_update_ordered(inode
,
8472 dio_bio
->bi_iter
.bi_size
,
8475 unlock_extent(&BTRFS_I(inode
)->io_tree
, file_offset
,
8476 file_offset
+ dio_bio
->bi_iter
.bi_size
- 1);
8478 dio_bio
->bi_status
= BLK_STS_IOERR
;
8480 * Releases and cleans up our dio_bio, no need to bio_put()
8481 * nor bio_endio()/bio_io_error() against dio_bio.
8483 dio_end_io(dio_bio
);
8490 static ssize_t
check_direct_IO(struct btrfs_fs_info
*fs_info
,
8491 const struct iov_iter
*iter
, loff_t offset
)
8495 unsigned int blocksize_mask
= fs_info
->sectorsize
- 1;
8496 ssize_t retval
= -EINVAL
;
8498 if (offset
& blocksize_mask
)
8501 if (iov_iter_alignment(iter
) & blocksize_mask
)
8504 /* If this is a write we don't need to check anymore */
8505 if (iov_iter_rw(iter
) != READ
|| !iter_is_iovec(iter
))
8508 * Check to make sure we don't have duplicate iov_base's in this
8509 * iovec, if so return EINVAL, otherwise we'll get csum errors
8510 * when reading back.
8512 for (seg
= 0; seg
< iter
->nr_segs
; seg
++) {
8513 for (i
= seg
+ 1; i
< iter
->nr_segs
; i
++) {
8514 if (iter
->iov
[seg
].iov_base
== iter
->iov
[i
].iov_base
)
8523 static ssize_t
btrfs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
8525 struct file
*file
= iocb
->ki_filp
;
8526 struct inode
*inode
= file
->f_mapping
->host
;
8527 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8528 struct btrfs_dio_data dio_data
= { 0 };
8529 struct extent_changeset
*data_reserved
= NULL
;
8530 loff_t offset
= iocb
->ki_pos
;
8534 bool relock
= false;
8537 if (check_direct_IO(fs_info
, iter
, offset
))
8540 inode_dio_begin(inode
);
8543 * The generic stuff only does filemap_write_and_wait_range, which
8544 * isn't enough if we've written compressed pages to this area, so
8545 * we need to flush the dirty pages again to make absolutely sure
8546 * that any outstanding dirty pages are on disk.
8548 count
= iov_iter_count(iter
);
8549 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
8550 &BTRFS_I(inode
)->runtime_flags
))
8551 filemap_fdatawrite_range(inode
->i_mapping
, offset
,
8552 offset
+ count
- 1);
8554 if (iov_iter_rw(iter
) == WRITE
) {
8556 * If the write DIO is beyond the EOF, we need update
8557 * the isize, but it is protected by i_mutex. So we can
8558 * not unlock the i_mutex at this case.
8560 if (offset
+ count
<= inode
->i_size
) {
8561 dio_data
.overwrite
= 1;
8562 inode_unlock(inode
);
8564 } else if (iocb
->ki_flags
& IOCB_NOWAIT
) {
8568 ret
= btrfs_delalloc_reserve_space(inode
, &data_reserved
,
8574 * We need to know how many extents we reserved so that we can
8575 * do the accounting properly if we go over the number we
8576 * originally calculated. Abuse current->journal_info for this.
8578 dio_data
.reserve
= round_up(count
,
8579 fs_info
->sectorsize
);
8580 dio_data
.unsubmitted_oe_range_start
= (u64
)offset
;
8581 dio_data
.unsubmitted_oe_range_end
= (u64
)offset
;
8582 current
->journal_info
= &dio_data
;
8583 down_read(&BTRFS_I(inode
)->dio_sem
);
8584 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
8585 &BTRFS_I(inode
)->runtime_flags
)) {
8586 inode_dio_end(inode
);
8587 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
8591 ret
= __blockdev_direct_IO(iocb
, inode
,
8592 fs_info
->fs_devices
->latest_bdev
,
8593 iter
, btrfs_get_blocks_direct
, NULL
,
8594 btrfs_submit_direct
, flags
);
8595 if (iov_iter_rw(iter
) == WRITE
) {
8596 up_read(&BTRFS_I(inode
)->dio_sem
);
8597 current
->journal_info
= NULL
;
8598 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
8599 if (dio_data
.reserve
)
8600 btrfs_delalloc_release_space(inode
, data_reserved
,
8601 offset
, dio_data
.reserve
, true);
8603 * On error we might have left some ordered extents
8604 * without submitting corresponding bios for them, so
8605 * cleanup them up to avoid other tasks getting them
8606 * and waiting for them to complete forever.
8608 if (dio_data
.unsubmitted_oe_range_start
<
8609 dio_data
.unsubmitted_oe_range_end
)
8610 __endio_write_update_ordered(inode
,
8611 dio_data
.unsubmitted_oe_range_start
,
8612 dio_data
.unsubmitted_oe_range_end
-
8613 dio_data
.unsubmitted_oe_range_start
,
8615 } else if (ret
>= 0 && (size_t)ret
< count
)
8616 btrfs_delalloc_release_space(inode
, data_reserved
,
8617 offset
, count
- (size_t)ret
, true);
8618 btrfs_delalloc_release_extents(BTRFS_I(inode
), count
, false);
8622 inode_dio_end(inode
);
8626 extent_changeset_free(data_reserved
);
8630 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8632 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
8633 __u64 start
, __u64 len
)
8637 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
8641 return extent_fiemap(inode
, fieinfo
, start
, len
);
8644 int btrfs_readpage(struct file
*file
, struct page
*page
)
8646 struct extent_io_tree
*tree
;
8647 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8648 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
8651 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
8653 struct inode
*inode
= page
->mapping
->host
;
8656 if (current
->flags
& PF_MEMALLOC
) {
8657 redirty_page_for_writepage(wbc
, page
);
8663 * If we are under memory pressure we will call this directly from the
8664 * VM, we need to make sure we have the inode referenced for the ordered
8665 * extent. If not just return like we didn't do anything.
8667 if (!igrab(inode
)) {
8668 redirty_page_for_writepage(wbc
, page
);
8669 return AOP_WRITEPAGE_ACTIVATE
;
8671 ret
= extent_write_full_page(page
, wbc
);
8672 btrfs_add_delayed_iput(inode
);
8676 static int btrfs_writepages(struct address_space
*mapping
,
8677 struct writeback_control
*wbc
)
8679 return extent_writepages(mapping
, wbc
);
8683 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
8684 struct list_head
*pages
, unsigned nr_pages
)
8686 return extent_readpages(mapping
, pages
, nr_pages
);
8689 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8691 int ret
= try_release_extent_mapping(page
, gfp_flags
);
8693 ClearPagePrivate(page
);
8694 set_page_private(page
, 0);
8700 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8702 if (PageWriteback(page
) || PageDirty(page
))
8704 return __btrfs_releasepage(page
, gfp_flags
);
8707 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
8708 unsigned int length
)
8710 struct inode
*inode
= page
->mapping
->host
;
8711 struct extent_io_tree
*tree
;
8712 struct btrfs_ordered_extent
*ordered
;
8713 struct extent_state
*cached_state
= NULL
;
8714 u64 page_start
= page_offset(page
);
8715 u64 page_end
= page_start
+ PAGE_SIZE
- 1;
8718 int inode_evicting
= inode
->i_state
& I_FREEING
;
8721 * we have the page locked, so new writeback can't start,
8722 * and the dirty bit won't be cleared while we are here.
8724 * Wait for IO on this page so that we can safely clear
8725 * the PagePrivate2 bit and do ordered accounting
8727 wait_on_page_writeback(page
);
8729 tree
= &BTRFS_I(inode
)->io_tree
;
8731 btrfs_releasepage(page
, GFP_NOFS
);
8735 if (!inode_evicting
)
8736 lock_extent_bits(tree
, page_start
, page_end
, &cached_state
);
8739 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), start
,
8740 page_end
- start
+ 1);
8742 end
= min(page_end
, ordered
->file_offset
+ ordered
->len
- 1);
8744 * IO on this page will never be started, so we need
8745 * to account for any ordered extents now
8747 if (!inode_evicting
)
8748 clear_extent_bit(tree
, start
, end
,
8749 EXTENT_DIRTY
| EXTENT_DELALLOC
|
8750 EXTENT_DELALLOC_NEW
|
8751 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
8752 EXTENT_DEFRAG
, 1, 0, &cached_state
);
8754 * whoever cleared the private bit is responsible
8755 * for the finish_ordered_io
8757 if (TestClearPagePrivate2(page
)) {
8758 struct btrfs_ordered_inode_tree
*tree
;
8761 tree
= &BTRFS_I(inode
)->ordered_tree
;
8763 spin_lock_irq(&tree
->lock
);
8764 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
8765 new_len
= start
- ordered
->file_offset
;
8766 if (new_len
< ordered
->truncated_len
)
8767 ordered
->truncated_len
= new_len
;
8768 spin_unlock_irq(&tree
->lock
);
8770 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
8772 end
- start
+ 1, 1))
8773 btrfs_finish_ordered_io(ordered
);
8775 btrfs_put_ordered_extent(ordered
);
8776 if (!inode_evicting
) {
8777 cached_state
= NULL
;
8778 lock_extent_bits(tree
, start
, end
,
8783 if (start
< page_end
)
8788 * Qgroup reserved space handler
8789 * Page here will be either
8790 * 1) Already written to disk
8791 * In this case, its reserved space is released from data rsv map
8792 * and will be freed by delayed_ref handler finally.
8793 * So even we call qgroup_free_data(), it won't decrease reserved
8795 * 2) Not written to disk
8796 * This means the reserved space should be freed here. However,
8797 * if a truncate invalidates the page (by clearing PageDirty)
8798 * and the page is accounted for while allocating extent
8799 * in btrfs_check_data_free_space() we let delayed_ref to
8800 * free the entire extent.
8802 if (PageDirty(page
))
8803 btrfs_qgroup_free_data(inode
, NULL
, page_start
, PAGE_SIZE
);
8804 if (!inode_evicting
) {
8805 clear_extent_bit(tree
, page_start
, page_end
,
8806 EXTENT_LOCKED
| EXTENT_DIRTY
|
8807 EXTENT_DELALLOC
| EXTENT_DELALLOC_NEW
|
8808 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
8811 __btrfs_releasepage(page
, GFP_NOFS
);
8814 ClearPageChecked(page
);
8815 if (PagePrivate(page
)) {
8816 ClearPagePrivate(page
);
8817 set_page_private(page
, 0);
8823 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8824 * called from a page fault handler when a page is first dirtied. Hence we must
8825 * be careful to check for EOF conditions here. We set the page up correctly
8826 * for a written page which means we get ENOSPC checking when writing into
8827 * holes and correct delalloc and unwritten extent mapping on filesystems that
8828 * support these features.
8830 * We are not allowed to take the i_mutex here so we have to play games to
8831 * protect against truncate races as the page could now be beyond EOF. Because
8832 * truncate_setsize() writes the inode size before removing pages, once we have
8833 * the page lock we can determine safely if the page is beyond EOF. If it is not
8834 * beyond EOF, then the page is guaranteed safe against truncation until we
8837 vm_fault_t
btrfs_page_mkwrite(struct vm_fault
*vmf
)
8839 struct page
*page
= vmf
->page
;
8840 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
8841 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8842 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
8843 struct btrfs_ordered_extent
*ordered
;
8844 struct extent_state
*cached_state
= NULL
;
8845 struct extent_changeset
*data_reserved
= NULL
;
8847 unsigned long zero_start
;
8857 reserved_space
= PAGE_SIZE
;
8859 sb_start_pagefault(inode
->i_sb
);
8860 page_start
= page_offset(page
);
8861 page_end
= page_start
+ PAGE_SIZE
- 1;
8865 * Reserving delalloc space after obtaining the page lock can lead to
8866 * deadlock. For example, if a dirty page is locked by this function
8867 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8868 * dirty page write out, then the btrfs_writepage() function could
8869 * end up waiting indefinitely to get a lock on the page currently
8870 * being processed by btrfs_page_mkwrite() function.
8872 ret2
= btrfs_delalloc_reserve_space(inode
, &data_reserved
, page_start
,
8875 ret2
= file_update_time(vmf
->vma
->vm_file
);
8879 ret
= vmf_error(ret2
);
8885 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
8888 size
= i_size_read(inode
);
8890 if ((page
->mapping
!= inode
->i_mapping
) ||
8891 (page_start
>= size
)) {
8892 /* page got truncated out from underneath us */
8895 wait_on_page_writeback(page
);
8897 lock_extent_bits(io_tree
, page_start
, page_end
, &cached_state
);
8898 set_page_extent_mapped(page
);
8901 * we can't set the delalloc bits if there are pending ordered
8902 * extents. Drop our locks and wait for them to finish
8904 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), page_start
,
8907 unlock_extent_cached(io_tree
, page_start
, page_end
,
8910 btrfs_start_ordered_extent(inode
, ordered
, 1);
8911 btrfs_put_ordered_extent(ordered
);
8915 if (page
->index
== ((size
- 1) >> PAGE_SHIFT
)) {
8916 reserved_space
= round_up(size
- page_start
,
8917 fs_info
->sectorsize
);
8918 if (reserved_space
< PAGE_SIZE
) {
8919 end
= page_start
+ reserved_space
- 1;
8920 btrfs_delalloc_release_space(inode
, data_reserved
,
8921 page_start
, PAGE_SIZE
- reserved_space
,
8927 * page_mkwrite gets called when the page is firstly dirtied after it's
8928 * faulted in, but write(2) could also dirty a page and set delalloc
8929 * bits, thus in this case for space account reason, we still need to
8930 * clear any delalloc bits within this page range since we have to
8931 * reserve data&meta space before lock_page() (see above comments).
8933 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, end
,
8934 EXTENT_DIRTY
| EXTENT_DELALLOC
|
8935 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
8936 0, 0, &cached_state
);
8938 ret2
= btrfs_set_extent_delalloc(inode
, page_start
, end
, 0,
8941 unlock_extent_cached(io_tree
, page_start
, page_end
,
8943 ret
= VM_FAULT_SIGBUS
;
8948 /* page is wholly or partially inside EOF */
8949 if (page_start
+ PAGE_SIZE
> size
)
8950 zero_start
= offset_in_page(size
);
8952 zero_start
= PAGE_SIZE
;
8954 if (zero_start
!= PAGE_SIZE
) {
8956 memset(kaddr
+ zero_start
, 0, PAGE_SIZE
- zero_start
);
8957 flush_dcache_page(page
);
8960 ClearPageChecked(page
);
8961 set_page_dirty(page
);
8962 SetPageUptodate(page
);
8964 BTRFS_I(inode
)->last_trans
= fs_info
->generation
;
8965 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
8966 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
8968 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
);
8971 btrfs_delalloc_release_extents(BTRFS_I(inode
), PAGE_SIZE
, true);
8972 sb_end_pagefault(inode
->i_sb
);
8973 extent_changeset_free(data_reserved
);
8974 return VM_FAULT_LOCKED
;
8980 btrfs_delalloc_release_extents(BTRFS_I(inode
), PAGE_SIZE
, (ret
!= 0));
8981 btrfs_delalloc_release_space(inode
, data_reserved
, page_start
,
8982 reserved_space
, (ret
!= 0));
8984 sb_end_pagefault(inode
->i_sb
);
8985 extent_changeset_free(data_reserved
);
8989 static int btrfs_truncate(struct inode
*inode
, bool skip_writeback
)
8991 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8992 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8993 struct btrfs_block_rsv
*rsv
;
8995 struct btrfs_trans_handle
*trans
;
8996 u64 mask
= fs_info
->sectorsize
- 1;
8997 u64 min_size
= btrfs_calc_trunc_metadata_size(fs_info
, 1);
8999 if (!skip_writeback
) {
9000 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
9007 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
9008 * things going on here:
9010 * 1) We need to reserve space to update our inode.
9012 * 2) We need to have something to cache all the space that is going to
9013 * be free'd up by the truncate operation, but also have some slack
9014 * space reserved in case it uses space during the truncate (thank you
9015 * very much snapshotting).
9017 * And we need these to be separate. The fact is we can use a lot of
9018 * space doing the truncate, and we have no earthly idea how much space
9019 * we will use, so we need the truncate reservation to be separate so it
9020 * doesn't end up using space reserved for updating the inode. We also
9021 * need to be able to stop the transaction and start a new one, which
9022 * means we need to be able to update the inode several times, and we
9023 * have no idea of knowing how many times that will be, so we can't just
9024 * reserve 1 item for the entirety of the operation, so that has to be
9025 * done separately as well.
9027 * So that leaves us with
9029 * 1) rsv - for the truncate reservation, which we will steal from the
9030 * transaction reservation.
9031 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
9032 * updating the inode.
9034 rsv
= btrfs_alloc_block_rsv(fs_info
, BTRFS_BLOCK_RSV_TEMP
);
9037 rsv
->size
= min_size
;
9041 * 1 for the truncate slack space
9042 * 1 for updating the inode.
9044 trans
= btrfs_start_transaction(root
, 2);
9045 if (IS_ERR(trans
)) {
9046 ret
= PTR_ERR(trans
);
9050 /* Migrate the slack space for the truncate to our reserve */
9051 ret
= btrfs_block_rsv_migrate(&fs_info
->trans_block_rsv
, rsv
,
9056 * So if we truncate and then write and fsync we normally would just
9057 * write the extents that changed, which is a problem if we need to
9058 * first truncate that entire inode. So set this flag so we write out
9059 * all of the extents in the inode to the sync log so we're completely
9062 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
9063 trans
->block_rsv
= rsv
;
9066 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
9068 BTRFS_EXTENT_DATA_KEY
);
9069 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
9070 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
)
9073 ret
= btrfs_update_inode(trans
, root
, inode
);
9077 btrfs_end_transaction(trans
);
9078 btrfs_btree_balance_dirty(fs_info
);
9080 trans
= btrfs_start_transaction(root
, 2);
9081 if (IS_ERR(trans
)) {
9082 ret
= PTR_ERR(trans
);
9087 btrfs_block_rsv_release(fs_info
, rsv
, -1);
9088 ret
= btrfs_block_rsv_migrate(&fs_info
->trans_block_rsv
,
9089 rsv
, min_size
, false);
9090 BUG_ON(ret
); /* shouldn't happen */
9091 trans
->block_rsv
= rsv
;
9095 * We can't call btrfs_truncate_block inside a trans handle as we could
9096 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
9097 * we've truncated everything except the last little bit, and can do
9098 * btrfs_truncate_block and then update the disk_i_size.
9100 if (ret
== NEED_TRUNCATE_BLOCK
) {
9101 btrfs_end_transaction(trans
);
9102 btrfs_btree_balance_dirty(fs_info
);
9104 ret
= btrfs_truncate_block(inode
, inode
->i_size
, 0, 0);
9107 trans
= btrfs_start_transaction(root
, 1);
9108 if (IS_ERR(trans
)) {
9109 ret
= PTR_ERR(trans
);
9112 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
9118 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
9119 ret2
= btrfs_update_inode(trans
, root
, inode
);
9123 ret2
= btrfs_end_transaction(trans
);
9126 btrfs_btree_balance_dirty(fs_info
);
9129 btrfs_free_block_rsv(fs_info
, rsv
);
9135 * create a new subvolume directory/inode (helper for the ioctl).
9137 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
9138 struct btrfs_root
*new_root
,
9139 struct btrfs_root
*parent_root
,
9142 struct inode
*inode
;
9146 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
9147 new_dirid
, new_dirid
,
9148 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
9151 return PTR_ERR(inode
);
9152 inode
->i_op
= &btrfs_dir_inode_operations
;
9153 inode
->i_fop
= &btrfs_dir_file_operations
;
9155 set_nlink(inode
, 1);
9156 btrfs_i_size_write(BTRFS_I(inode
), 0);
9157 unlock_new_inode(inode
);
9159 err
= btrfs_subvol_inherit_props(trans
, new_root
, parent_root
);
9161 btrfs_err(new_root
->fs_info
,
9162 "error inheriting subvolume %llu properties: %d",
9163 new_root
->root_key
.objectid
, err
);
9165 err
= btrfs_update_inode(trans
, new_root
, inode
);
9171 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
9173 struct btrfs_fs_info
*fs_info
= btrfs_sb(sb
);
9174 struct btrfs_inode
*ei
;
9175 struct inode
*inode
;
9177 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_KERNEL
);
9184 ei
->last_sub_trans
= 0;
9185 ei
->logged_trans
= 0;
9186 ei
->delalloc_bytes
= 0;
9187 ei
->new_delalloc_bytes
= 0;
9188 ei
->defrag_bytes
= 0;
9189 ei
->disk_i_size
= 0;
9192 ei
->index_cnt
= (u64
)-1;
9194 ei
->last_unlink_trans
= 0;
9195 ei
->last_link_trans
= 0;
9196 ei
->last_log_commit
= 0;
9198 spin_lock_init(&ei
->lock
);
9199 ei
->outstanding_extents
= 0;
9200 if (sb
->s_magic
!= BTRFS_TEST_MAGIC
)
9201 btrfs_init_metadata_block_rsv(fs_info
, &ei
->block_rsv
,
9202 BTRFS_BLOCK_RSV_DELALLOC
);
9203 ei
->runtime_flags
= 0;
9204 ei
->prop_compress
= BTRFS_COMPRESS_NONE
;
9205 ei
->defrag_compress
= BTRFS_COMPRESS_NONE
;
9207 ei
->delayed_node
= NULL
;
9209 ei
->i_otime
.tv_sec
= 0;
9210 ei
->i_otime
.tv_nsec
= 0;
9212 inode
= &ei
->vfs_inode
;
9213 extent_map_tree_init(&ei
->extent_tree
);
9214 extent_io_tree_init(fs_info
, &ei
->io_tree
, IO_TREE_INODE_IO
, inode
);
9215 extent_io_tree_init(fs_info
, &ei
->io_failure_tree
,
9216 IO_TREE_INODE_IO_FAILURE
, inode
);
9217 ei
->io_tree
.track_uptodate
= true;
9218 ei
->io_failure_tree
.track_uptodate
= true;
9219 atomic_set(&ei
->sync_writers
, 0);
9220 mutex_init(&ei
->log_mutex
);
9221 mutex_init(&ei
->delalloc_mutex
);
9222 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
9223 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
9224 INIT_LIST_HEAD(&ei
->delayed_iput
);
9225 RB_CLEAR_NODE(&ei
->rb_node
);
9226 init_rwsem(&ei
->dio_sem
);
9231 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9232 void btrfs_test_destroy_inode(struct inode
*inode
)
9234 btrfs_drop_extent_cache(BTRFS_I(inode
), 0, (u64
)-1, 0);
9235 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9239 static void btrfs_i_callback(struct rcu_head
*head
)
9241 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
9242 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9245 void btrfs_destroy_inode(struct inode
*inode
)
9247 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
9248 struct btrfs_ordered_extent
*ordered
;
9249 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9251 WARN_ON(!hlist_empty(&inode
->i_dentry
));
9252 WARN_ON(inode
->i_data
.nrpages
);
9253 WARN_ON(BTRFS_I(inode
)->block_rsv
.reserved
);
9254 WARN_ON(BTRFS_I(inode
)->block_rsv
.size
);
9255 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
9256 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
9257 WARN_ON(BTRFS_I(inode
)->new_delalloc_bytes
);
9258 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
9259 WARN_ON(BTRFS_I(inode
)->defrag_bytes
);
9262 * This can happen where we create an inode, but somebody else also
9263 * created the same inode and we need to destroy the one we already
9270 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
9275 "found ordered extent %llu %llu on inode cleanup",
9276 ordered
->file_offset
, ordered
->len
);
9277 btrfs_remove_ordered_extent(inode
, ordered
);
9278 btrfs_put_ordered_extent(ordered
);
9279 btrfs_put_ordered_extent(ordered
);
9282 btrfs_qgroup_check_reserved_leak(inode
);
9283 inode_tree_del(inode
);
9284 btrfs_drop_extent_cache(BTRFS_I(inode
), 0, (u64
)-1, 0);
9286 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
9289 int btrfs_drop_inode(struct inode
*inode
)
9291 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9296 /* the snap/subvol tree is on deleting */
9297 if (btrfs_root_refs(&root
->root_item
) == 0)
9300 return generic_drop_inode(inode
);
9303 static void init_once(void *foo
)
9305 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
9307 inode_init_once(&ei
->vfs_inode
);
9310 void __cold
btrfs_destroy_cachep(void)
9313 * Make sure all delayed rcu free inodes are flushed before we
9317 kmem_cache_destroy(btrfs_inode_cachep
);
9318 kmem_cache_destroy(btrfs_trans_handle_cachep
);
9319 kmem_cache_destroy(btrfs_path_cachep
);
9320 kmem_cache_destroy(btrfs_free_space_cachep
);
9323 int __init
btrfs_init_cachep(void)
9325 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
9326 sizeof(struct btrfs_inode
), 0,
9327 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
| SLAB_ACCOUNT
,
9329 if (!btrfs_inode_cachep
)
9332 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
9333 sizeof(struct btrfs_trans_handle
), 0,
9334 SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
9335 if (!btrfs_trans_handle_cachep
)
9338 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
9339 sizeof(struct btrfs_path
), 0,
9340 SLAB_MEM_SPREAD
, NULL
);
9341 if (!btrfs_path_cachep
)
9344 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
9345 sizeof(struct btrfs_free_space
), 0,
9346 SLAB_MEM_SPREAD
, NULL
);
9347 if (!btrfs_free_space_cachep
)
9352 btrfs_destroy_cachep();
9356 static int btrfs_getattr(const struct path
*path
, struct kstat
*stat
,
9357 u32 request_mask
, unsigned int flags
)
9360 struct inode
*inode
= d_inode(path
->dentry
);
9361 u32 blocksize
= inode
->i_sb
->s_blocksize
;
9362 u32 bi_flags
= BTRFS_I(inode
)->flags
;
9364 stat
->result_mask
|= STATX_BTIME
;
9365 stat
->btime
.tv_sec
= BTRFS_I(inode
)->i_otime
.tv_sec
;
9366 stat
->btime
.tv_nsec
= BTRFS_I(inode
)->i_otime
.tv_nsec
;
9367 if (bi_flags
& BTRFS_INODE_APPEND
)
9368 stat
->attributes
|= STATX_ATTR_APPEND
;
9369 if (bi_flags
& BTRFS_INODE_COMPRESS
)
9370 stat
->attributes
|= STATX_ATTR_COMPRESSED
;
9371 if (bi_flags
& BTRFS_INODE_IMMUTABLE
)
9372 stat
->attributes
|= STATX_ATTR_IMMUTABLE
;
9373 if (bi_flags
& BTRFS_INODE_NODUMP
)
9374 stat
->attributes
|= STATX_ATTR_NODUMP
;
9376 stat
->attributes_mask
|= (STATX_ATTR_APPEND
|
9377 STATX_ATTR_COMPRESSED
|
9378 STATX_ATTR_IMMUTABLE
|
9381 generic_fillattr(inode
, stat
);
9382 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
9384 spin_lock(&BTRFS_I(inode
)->lock
);
9385 delalloc_bytes
= BTRFS_I(inode
)->new_delalloc_bytes
;
9386 spin_unlock(&BTRFS_I(inode
)->lock
);
9387 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
9388 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
9392 static int btrfs_rename_exchange(struct inode
*old_dir
,
9393 struct dentry
*old_dentry
,
9394 struct inode
*new_dir
,
9395 struct dentry
*new_dentry
)
9397 struct btrfs_fs_info
*fs_info
= btrfs_sb(old_dir
->i_sb
);
9398 struct btrfs_trans_handle
*trans
;
9399 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
9400 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
9401 struct inode
*new_inode
= new_dentry
->d_inode
;
9402 struct inode
*old_inode
= old_dentry
->d_inode
;
9403 struct timespec64 ctime
= current_time(old_inode
);
9404 struct dentry
*parent
;
9405 u64 old_ino
= btrfs_ino(BTRFS_I(old_inode
));
9406 u64 new_ino
= btrfs_ino(BTRFS_I(new_inode
));
9411 bool root_log_pinned
= false;
9412 bool dest_log_pinned
= false;
9413 struct btrfs_log_ctx ctx_root
;
9414 struct btrfs_log_ctx ctx_dest
;
9415 bool sync_log_root
= false;
9416 bool sync_log_dest
= false;
9417 bool commit_transaction
= false;
9419 /* we only allow rename subvolume link between subvolumes */
9420 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
9423 btrfs_init_log_ctx(&ctx_root
, old_inode
);
9424 btrfs_init_log_ctx(&ctx_dest
, new_inode
);
9426 /* close the race window with snapshot create/destroy ioctl */
9427 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9428 down_read(&fs_info
->subvol_sem
);
9429 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9430 down_read(&fs_info
->subvol_sem
);
9433 * We want to reserve the absolute worst case amount of items. So if
9434 * both inodes are subvols and we need to unlink them then that would
9435 * require 4 item modifications, but if they are both normal inodes it
9436 * would require 5 item modifications, so we'll assume their normal
9437 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9438 * should cover the worst case number of items we'll modify.
9440 trans
= btrfs_start_transaction(root
, 12);
9441 if (IS_ERR(trans
)) {
9442 ret
= PTR_ERR(trans
);
9447 * We need to find a free sequence number both in the source and
9448 * in the destination directory for the exchange.
9450 ret
= btrfs_set_inode_index(BTRFS_I(new_dir
), &old_idx
);
9453 ret
= btrfs_set_inode_index(BTRFS_I(old_dir
), &new_idx
);
9457 BTRFS_I(old_inode
)->dir_index
= 0ULL;
9458 BTRFS_I(new_inode
)->dir_index
= 0ULL;
9460 /* Reference for the source. */
9461 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9462 /* force full log commit if subvolume involved. */
9463 btrfs_set_log_full_commit(trans
);
9465 btrfs_pin_log_trans(root
);
9466 root_log_pinned
= true;
9467 ret
= btrfs_insert_inode_ref(trans
, dest
,
9468 new_dentry
->d_name
.name
,
9469 new_dentry
->d_name
.len
,
9471 btrfs_ino(BTRFS_I(new_dir
)),
9477 /* And now for the dest. */
9478 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9479 /* force full log commit if subvolume involved. */
9480 btrfs_set_log_full_commit(trans
);
9482 btrfs_pin_log_trans(dest
);
9483 dest_log_pinned
= true;
9484 ret
= btrfs_insert_inode_ref(trans
, root
,
9485 old_dentry
->d_name
.name
,
9486 old_dentry
->d_name
.len
,
9488 btrfs_ino(BTRFS_I(old_dir
)),
9494 /* Update inode version and ctime/mtime. */
9495 inode_inc_iversion(old_dir
);
9496 inode_inc_iversion(new_dir
);
9497 inode_inc_iversion(old_inode
);
9498 inode_inc_iversion(new_inode
);
9499 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
9500 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
9501 old_inode
->i_ctime
= ctime
;
9502 new_inode
->i_ctime
= ctime
;
9504 if (old_dentry
->d_parent
!= new_dentry
->d_parent
) {
9505 btrfs_record_unlink_dir(trans
, BTRFS_I(old_dir
),
9506 BTRFS_I(old_inode
), 1);
9507 btrfs_record_unlink_dir(trans
, BTRFS_I(new_dir
),
9508 BTRFS_I(new_inode
), 1);
9511 /* src is a subvolume */
9512 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9513 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
9514 ret
= btrfs_unlink_subvol(trans
, old_dir
, root_objectid
,
9515 old_dentry
->d_name
.name
,
9516 old_dentry
->d_name
.len
);
9517 } else { /* src is an inode */
9518 ret
= __btrfs_unlink_inode(trans
, root
, BTRFS_I(old_dir
),
9519 BTRFS_I(old_dentry
->d_inode
),
9520 old_dentry
->d_name
.name
,
9521 old_dentry
->d_name
.len
);
9523 ret
= btrfs_update_inode(trans
, root
, old_inode
);
9526 btrfs_abort_transaction(trans
, ret
);
9530 /* dest is a subvolume */
9531 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9532 root_objectid
= BTRFS_I(new_inode
)->root
->root_key
.objectid
;
9533 ret
= btrfs_unlink_subvol(trans
, new_dir
, root_objectid
,
9534 new_dentry
->d_name
.name
,
9535 new_dentry
->d_name
.len
);
9536 } else { /* dest is an inode */
9537 ret
= __btrfs_unlink_inode(trans
, dest
, BTRFS_I(new_dir
),
9538 BTRFS_I(new_dentry
->d_inode
),
9539 new_dentry
->d_name
.name
,
9540 new_dentry
->d_name
.len
);
9542 ret
= btrfs_update_inode(trans
, dest
, new_inode
);
9545 btrfs_abort_transaction(trans
, ret
);
9549 ret
= btrfs_add_link(trans
, BTRFS_I(new_dir
), BTRFS_I(old_inode
),
9550 new_dentry
->d_name
.name
,
9551 new_dentry
->d_name
.len
, 0, old_idx
);
9553 btrfs_abort_transaction(trans
, ret
);
9557 ret
= btrfs_add_link(trans
, BTRFS_I(old_dir
), BTRFS_I(new_inode
),
9558 old_dentry
->d_name
.name
,
9559 old_dentry
->d_name
.len
, 0, new_idx
);
9561 btrfs_abort_transaction(trans
, ret
);
9565 if (old_inode
->i_nlink
== 1)
9566 BTRFS_I(old_inode
)->dir_index
= old_idx
;
9567 if (new_inode
->i_nlink
== 1)
9568 BTRFS_I(new_inode
)->dir_index
= new_idx
;
9570 if (root_log_pinned
) {
9571 parent
= new_dentry
->d_parent
;
9572 ret
= btrfs_log_new_name(trans
, BTRFS_I(old_inode
),
9573 BTRFS_I(old_dir
), parent
,
9575 if (ret
== BTRFS_NEED_LOG_SYNC
)
9576 sync_log_root
= true;
9577 else if (ret
== BTRFS_NEED_TRANS_COMMIT
)
9578 commit_transaction
= true;
9580 btrfs_end_log_trans(root
);
9581 root_log_pinned
= false;
9583 if (dest_log_pinned
) {
9584 if (!commit_transaction
) {
9585 parent
= old_dentry
->d_parent
;
9586 ret
= btrfs_log_new_name(trans
, BTRFS_I(new_inode
),
9587 BTRFS_I(new_dir
), parent
,
9589 if (ret
== BTRFS_NEED_LOG_SYNC
)
9590 sync_log_dest
= true;
9591 else if (ret
== BTRFS_NEED_TRANS_COMMIT
)
9592 commit_transaction
= true;
9595 btrfs_end_log_trans(dest
);
9596 dest_log_pinned
= false;
9600 * If we have pinned a log and an error happened, we unpin tasks
9601 * trying to sync the log and force them to fallback to a transaction
9602 * commit if the log currently contains any of the inodes involved in
9603 * this rename operation (to ensure we do not persist a log with an
9604 * inconsistent state for any of these inodes or leading to any
9605 * inconsistencies when replayed). If the transaction was aborted, the
9606 * abortion reason is propagated to userspace when attempting to commit
9607 * the transaction. If the log does not contain any of these inodes, we
9608 * allow the tasks to sync it.
9610 if (ret
&& (root_log_pinned
|| dest_log_pinned
)) {
9611 if (btrfs_inode_in_log(BTRFS_I(old_dir
), fs_info
->generation
) ||
9612 btrfs_inode_in_log(BTRFS_I(new_dir
), fs_info
->generation
) ||
9613 btrfs_inode_in_log(BTRFS_I(old_inode
), fs_info
->generation
) ||
9615 btrfs_inode_in_log(BTRFS_I(new_inode
), fs_info
->generation
)))
9616 btrfs_set_log_full_commit(trans
);
9618 if (root_log_pinned
) {
9619 btrfs_end_log_trans(root
);
9620 root_log_pinned
= false;
9622 if (dest_log_pinned
) {
9623 btrfs_end_log_trans(dest
);
9624 dest_log_pinned
= false;
9627 if (!ret
&& sync_log_root
&& !commit_transaction
) {
9628 ret
= btrfs_sync_log(trans
, BTRFS_I(old_inode
)->root
,
9631 commit_transaction
= true;
9633 if (!ret
&& sync_log_dest
&& !commit_transaction
) {
9634 ret
= btrfs_sync_log(trans
, BTRFS_I(new_inode
)->root
,
9637 commit_transaction
= true;
9639 if (commit_transaction
) {
9640 ret
= btrfs_commit_transaction(trans
);
9644 ret2
= btrfs_end_transaction(trans
);
9645 ret
= ret
? ret
: ret2
;
9648 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9649 up_read(&fs_info
->subvol_sem
);
9650 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9651 up_read(&fs_info
->subvol_sem
);
9656 static int btrfs_whiteout_for_rename(struct btrfs_trans_handle
*trans
,
9657 struct btrfs_root
*root
,
9659 struct dentry
*dentry
)
9662 struct inode
*inode
;
9666 ret
= btrfs_find_free_ino(root
, &objectid
);
9670 inode
= btrfs_new_inode(trans
, root
, dir
,
9671 dentry
->d_name
.name
,
9673 btrfs_ino(BTRFS_I(dir
)),
9675 S_IFCHR
| WHITEOUT_MODE
,
9678 if (IS_ERR(inode
)) {
9679 ret
= PTR_ERR(inode
);
9683 inode
->i_op
= &btrfs_special_inode_operations
;
9684 init_special_inode(inode
, inode
->i_mode
,
9687 ret
= btrfs_init_inode_security(trans
, inode
, dir
,
9692 ret
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
,
9693 BTRFS_I(inode
), 0, index
);
9697 ret
= btrfs_update_inode(trans
, root
, inode
);
9699 unlock_new_inode(inode
);
9701 inode_dec_link_count(inode
);
9707 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
9708 struct inode
*new_dir
, struct dentry
*new_dentry
,
9711 struct btrfs_fs_info
*fs_info
= btrfs_sb(old_dir
->i_sb
);
9712 struct btrfs_trans_handle
*trans
;
9713 unsigned int trans_num_items
;
9714 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
9715 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
9716 struct inode
*new_inode
= d_inode(new_dentry
);
9717 struct inode
*old_inode
= d_inode(old_dentry
);
9721 u64 old_ino
= btrfs_ino(BTRFS_I(old_inode
));
9722 bool log_pinned
= false;
9723 struct btrfs_log_ctx ctx
;
9724 bool sync_log
= false;
9725 bool commit_transaction
= false;
9727 if (btrfs_ino(BTRFS_I(new_dir
)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
9730 /* we only allow rename subvolume link between subvolumes */
9731 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
9734 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
9735 (new_inode
&& btrfs_ino(BTRFS_I(new_inode
)) == BTRFS_FIRST_FREE_OBJECTID
))
9738 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
9739 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
9743 /* check for collisions, even if the name isn't there */
9744 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
9745 new_dentry
->d_name
.name
,
9746 new_dentry
->d_name
.len
);
9749 if (ret
== -EEXIST
) {
9751 * eexist without a new_inode */
9752 if (WARN_ON(!new_inode
)) {
9756 /* maybe -EOVERFLOW */
9763 * we're using rename to replace one file with another. Start IO on it
9764 * now so we don't add too much work to the end of the transaction
9766 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
)
9767 filemap_flush(old_inode
->i_mapping
);
9769 /* close the racy window with snapshot create/destroy ioctl */
9770 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9771 down_read(&fs_info
->subvol_sem
);
9773 * We want to reserve the absolute worst case amount of items. So if
9774 * both inodes are subvols and we need to unlink them then that would
9775 * require 4 item modifications, but if they are both normal inodes it
9776 * would require 5 item modifications, so we'll assume they are normal
9777 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9778 * should cover the worst case number of items we'll modify.
9779 * If our rename has the whiteout flag, we need more 5 units for the
9780 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9781 * when selinux is enabled).
9783 trans_num_items
= 11;
9784 if (flags
& RENAME_WHITEOUT
)
9785 trans_num_items
+= 5;
9786 trans
= btrfs_start_transaction(root
, trans_num_items
);
9787 if (IS_ERR(trans
)) {
9788 ret
= PTR_ERR(trans
);
9793 btrfs_record_root_in_trans(trans
, dest
);
9795 ret
= btrfs_set_inode_index(BTRFS_I(new_dir
), &index
);
9799 BTRFS_I(old_inode
)->dir_index
= 0ULL;
9800 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9801 /* force full log commit if subvolume involved. */
9802 btrfs_set_log_full_commit(trans
);
9804 btrfs_pin_log_trans(root
);
9806 ret
= btrfs_insert_inode_ref(trans
, dest
,
9807 new_dentry
->d_name
.name
,
9808 new_dentry
->d_name
.len
,
9810 btrfs_ino(BTRFS_I(new_dir
)), index
);
9815 inode_inc_iversion(old_dir
);
9816 inode_inc_iversion(new_dir
);
9817 inode_inc_iversion(old_inode
);
9818 old_dir
->i_ctime
= old_dir
->i_mtime
=
9819 new_dir
->i_ctime
= new_dir
->i_mtime
=
9820 old_inode
->i_ctime
= current_time(old_dir
);
9822 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
9823 btrfs_record_unlink_dir(trans
, BTRFS_I(old_dir
),
9824 BTRFS_I(old_inode
), 1);
9826 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9827 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
9828 ret
= btrfs_unlink_subvol(trans
, old_dir
, root_objectid
,
9829 old_dentry
->d_name
.name
,
9830 old_dentry
->d_name
.len
);
9832 ret
= __btrfs_unlink_inode(trans
, root
, BTRFS_I(old_dir
),
9833 BTRFS_I(d_inode(old_dentry
)),
9834 old_dentry
->d_name
.name
,
9835 old_dentry
->d_name
.len
);
9837 ret
= btrfs_update_inode(trans
, root
, old_inode
);
9840 btrfs_abort_transaction(trans
, ret
);
9845 inode_inc_iversion(new_inode
);
9846 new_inode
->i_ctime
= current_time(new_inode
);
9847 if (unlikely(btrfs_ino(BTRFS_I(new_inode
)) ==
9848 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
9849 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
9850 ret
= btrfs_unlink_subvol(trans
, new_dir
, root_objectid
,
9851 new_dentry
->d_name
.name
,
9852 new_dentry
->d_name
.len
);
9853 BUG_ON(new_inode
->i_nlink
== 0);
9855 ret
= btrfs_unlink_inode(trans
, dest
, BTRFS_I(new_dir
),
9856 BTRFS_I(d_inode(new_dentry
)),
9857 new_dentry
->d_name
.name
,
9858 new_dentry
->d_name
.len
);
9860 if (!ret
&& new_inode
->i_nlink
== 0)
9861 ret
= btrfs_orphan_add(trans
,
9862 BTRFS_I(d_inode(new_dentry
)));
9864 btrfs_abort_transaction(trans
, ret
);
9869 ret
= btrfs_add_link(trans
, BTRFS_I(new_dir
), BTRFS_I(old_inode
),
9870 new_dentry
->d_name
.name
,
9871 new_dentry
->d_name
.len
, 0, index
);
9873 btrfs_abort_transaction(trans
, ret
);
9877 if (old_inode
->i_nlink
== 1)
9878 BTRFS_I(old_inode
)->dir_index
= index
;
9881 struct dentry
*parent
= new_dentry
->d_parent
;
9883 btrfs_init_log_ctx(&ctx
, old_inode
);
9884 ret
= btrfs_log_new_name(trans
, BTRFS_I(old_inode
),
9885 BTRFS_I(old_dir
), parent
,
9887 if (ret
== BTRFS_NEED_LOG_SYNC
)
9889 else if (ret
== BTRFS_NEED_TRANS_COMMIT
)
9890 commit_transaction
= true;
9892 btrfs_end_log_trans(root
);
9896 if (flags
& RENAME_WHITEOUT
) {
9897 ret
= btrfs_whiteout_for_rename(trans
, root
, old_dir
,
9901 btrfs_abort_transaction(trans
, ret
);
9907 * If we have pinned the log and an error happened, we unpin tasks
9908 * trying to sync the log and force them to fallback to a transaction
9909 * commit if the log currently contains any of the inodes involved in
9910 * this rename operation (to ensure we do not persist a log with an
9911 * inconsistent state for any of these inodes or leading to any
9912 * inconsistencies when replayed). If the transaction was aborted, the
9913 * abortion reason is propagated to userspace when attempting to commit
9914 * the transaction. If the log does not contain any of these inodes, we
9915 * allow the tasks to sync it.
9917 if (ret
&& log_pinned
) {
9918 if (btrfs_inode_in_log(BTRFS_I(old_dir
), fs_info
->generation
) ||
9919 btrfs_inode_in_log(BTRFS_I(new_dir
), fs_info
->generation
) ||
9920 btrfs_inode_in_log(BTRFS_I(old_inode
), fs_info
->generation
) ||
9922 btrfs_inode_in_log(BTRFS_I(new_inode
), fs_info
->generation
)))
9923 btrfs_set_log_full_commit(trans
);
9925 btrfs_end_log_trans(root
);
9928 if (!ret
&& sync_log
) {
9929 ret
= btrfs_sync_log(trans
, BTRFS_I(old_inode
)->root
, &ctx
);
9931 commit_transaction
= true;
9933 if (commit_transaction
) {
9934 ret
= btrfs_commit_transaction(trans
);
9938 ret2
= btrfs_end_transaction(trans
);
9939 ret
= ret
? ret
: ret2
;
9942 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9943 up_read(&fs_info
->subvol_sem
);
9948 static int btrfs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
9949 struct inode
*new_dir
, struct dentry
*new_dentry
,
9952 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
9955 if (flags
& RENAME_EXCHANGE
)
9956 return btrfs_rename_exchange(old_dir
, old_dentry
, new_dir
,
9959 return btrfs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
9962 struct btrfs_delalloc_work
{
9963 struct inode
*inode
;
9964 struct completion completion
;
9965 struct list_head list
;
9966 struct btrfs_work work
;
9969 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
9971 struct btrfs_delalloc_work
*delalloc_work
;
9972 struct inode
*inode
;
9974 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
9976 inode
= delalloc_work
->inode
;
9977 filemap_flush(inode
->i_mapping
);
9978 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
9979 &BTRFS_I(inode
)->runtime_flags
))
9980 filemap_flush(inode
->i_mapping
);
9983 complete(&delalloc_work
->completion
);
9986 static struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
)
9988 struct btrfs_delalloc_work
*work
;
9990 work
= kmalloc(sizeof(*work
), GFP_NOFS
);
9994 init_completion(&work
->completion
);
9995 INIT_LIST_HEAD(&work
->list
);
9996 work
->inode
= inode
;
9997 btrfs_init_work(&work
->work
, btrfs_flush_delalloc_helper
,
9998 btrfs_run_delalloc_work
, NULL
, NULL
);
10004 * some fairly slow code that needs optimization. This walks the list
10005 * of all the inodes with pending delalloc and forces them to disk.
10007 static int start_delalloc_inodes(struct btrfs_root
*root
, int nr
, bool snapshot
)
10009 struct btrfs_inode
*binode
;
10010 struct inode
*inode
;
10011 struct btrfs_delalloc_work
*work
, *next
;
10012 struct list_head works
;
10013 struct list_head splice
;
10016 INIT_LIST_HEAD(&works
);
10017 INIT_LIST_HEAD(&splice
);
10019 mutex_lock(&root
->delalloc_mutex
);
10020 spin_lock(&root
->delalloc_lock
);
10021 list_splice_init(&root
->delalloc_inodes
, &splice
);
10022 while (!list_empty(&splice
)) {
10023 binode
= list_entry(splice
.next
, struct btrfs_inode
,
10026 list_move_tail(&binode
->delalloc_inodes
,
10027 &root
->delalloc_inodes
);
10028 inode
= igrab(&binode
->vfs_inode
);
10030 cond_resched_lock(&root
->delalloc_lock
);
10033 spin_unlock(&root
->delalloc_lock
);
10036 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH
,
10037 &binode
->runtime_flags
);
10038 work
= btrfs_alloc_delalloc_work(inode
);
10044 list_add_tail(&work
->list
, &works
);
10045 btrfs_queue_work(root
->fs_info
->flush_workers
,
10048 if (nr
!= -1 && ret
>= nr
)
10051 spin_lock(&root
->delalloc_lock
);
10053 spin_unlock(&root
->delalloc_lock
);
10056 list_for_each_entry_safe(work
, next
, &works
, list
) {
10057 list_del_init(&work
->list
);
10058 wait_for_completion(&work
->completion
);
10062 if (!list_empty(&splice
)) {
10063 spin_lock(&root
->delalloc_lock
);
10064 list_splice_tail(&splice
, &root
->delalloc_inodes
);
10065 spin_unlock(&root
->delalloc_lock
);
10067 mutex_unlock(&root
->delalloc_mutex
);
10071 int btrfs_start_delalloc_snapshot(struct btrfs_root
*root
)
10073 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
10076 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
10079 ret
= start_delalloc_inodes(root
, -1, true);
10085 int btrfs_start_delalloc_roots(struct btrfs_fs_info
*fs_info
, int nr
)
10087 struct btrfs_root
*root
;
10088 struct list_head splice
;
10091 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
10094 INIT_LIST_HEAD(&splice
);
10096 mutex_lock(&fs_info
->delalloc_root_mutex
);
10097 spin_lock(&fs_info
->delalloc_root_lock
);
10098 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
10099 while (!list_empty(&splice
) && nr
) {
10100 root
= list_first_entry(&splice
, struct btrfs_root
,
10102 root
= btrfs_grab_fs_root(root
);
10104 list_move_tail(&root
->delalloc_root
,
10105 &fs_info
->delalloc_roots
);
10106 spin_unlock(&fs_info
->delalloc_root_lock
);
10108 ret
= start_delalloc_inodes(root
, nr
, false);
10109 btrfs_put_fs_root(root
);
10117 spin_lock(&fs_info
->delalloc_root_lock
);
10119 spin_unlock(&fs_info
->delalloc_root_lock
);
10123 if (!list_empty(&splice
)) {
10124 spin_lock(&fs_info
->delalloc_root_lock
);
10125 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
10126 spin_unlock(&fs_info
->delalloc_root_lock
);
10128 mutex_unlock(&fs_info
->delalloc_root_mutex
);
10132 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
10133 const char *symname
)
10135 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
10136 struct btrfs_trans_handle
*trans
;
10137 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
10138 struct btrfs_path
*path
;
10139 struct btrfs_key key
;
10140 struct inode
*inode
= NULL
;
10147 struct btrfs_file_extent_item
*ei
;
10148 struct extent_buffer
*leaf
;
10150 name_len
= strlen(symname
);
10151 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(fs_info
))
10152 return -ENAMETOOLONG
;
10155 * 2 items for inode item and ref
10156 * 2 items for dir items
10157 * 1 item for updating parent inode item
10158 * 1 item for the inline extent item
10159 * 1 item for xattr if selinux is on
10161 trans
= btrfs_start_transaction(root
, 7);
10163 return PTR_ERR(trans
);
10165 err
= btrfs_find_free_ino(root
, &objectid
);
10169 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
10170 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)),
10171 objectid
, S_IFLNK
|S_IRWXUGO
, &index
);
10172 if (IS_ERR(inode
)) {
10173 err
= PTR_ERR(inode
);
10179 * If the active LSM wants to access the inode during
10180 * d_instantiate it needs these. Smack checks to see
10181 * if the filesystem supports xattrs by looking at the
10184 inode
->i_fop
= &btrfs_file_operations
;
10185 inode
->i_op
= &btrfs_file_inode_operations
;
10186 inode
->i_mapping
->a_ops
= &btrfs_aops
;
10187 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
10189 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
10193 path
= btrfs_alloc_path();
10198 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
10200 key
.type
= BTRFS_EXTENT_DATA_KEY
;
10201 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
10202 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
10205 btrfs_free_path(path
);
10208 leaf
= path
->nodes
[0];
10209 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
10210 struct btrfs_file_extent_item
);
10211 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
10212 btrfs_set_file_extent_type(leaf
, ei
,
10213 BTRFS_FILE_EXTENT_INLINE
);
10214 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
10215 btrfs_set_file_extent_compression(leaf
, ei
, 0);
10216 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
10217 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
10219 ptr
= btrfs_file_extent_inline_start(ei
);
10220 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
10221 btrfs_mark_buffer_dirty(leaf
);
10222 btrfs_free_path(path
);
10224 inode
->i_op
= &btrfs_symlink_inode_operations
;
10225 inode_nohighmem(inode
);
10226 inode_set_bytes(inode
, name_len
);
10227 btrfs_i_size_write(BTRFS_I(inode
), name_len
);
10228 err
= btrfs_update_inode(trans
, root
, inode
);
10230 * Last step, add directory indexes for our symlink inode. This is the
10231 * last step to avoid extra cleanup of these indexes if an error happens
10235 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
,
10236 BTRFS_I(inode
), 0, index
);
10240 d_instantiate_new(dentry
, inode
);
10243 btrfs_end_transaction(trans
);
10244 if (err
&& inode
) {
10245 inode_dec_link_count(inode
);
10246 discard_new_inode(inode
);
10248 btrfs_btree_balance_dirty(fs_info
);
10252 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
10253 u64 start
, u64 num_bytes
, u64 min_size
,
10254 loff_t actual_len
, u64
*alloc_hint
,
10255 struct btrfs_trans_handle
*trans
)
10257 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
10258 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
10259 struct extent_map
*em
;
10260 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
10261 struct btrfs_key ins
;
10262 u64 cur_offset
= start
;
10265 u64 last_alloc
= (u64
)-1;
10267 bool own_trans
= true;
10268 u64 end
= start
+ num_bytes
- 1;
10272 while (num_bytes
> 0) {
10274 trans
= btrfs_start_transaction(root
, 3);
10275 if (IS_ERR(trans
)) {
10276 ret
= PTR_ERR(trans
);
10281 cur_bytes
= min_t(u64
, num_bytes
, SZ_256M
);
10282 cur_bytes
= max(cur_bytes
, min_size
);
10284 * If we are severely fragmented we could end up with really
10285 * small allocations, so if the allocator is returning small
10286 * chunks lets make its job easier by only searching for those
10289 cur_bytes
= min(cur_bytes
, last_alloc
);
10290 ret
= btrfs_reserve_extent(root
, cur_bytes
, cur_bytes
,
10291 min_size
, 0, *alloc_hint
, &ins
, 1, 0);
10294 btrfs_end_transaction(trans
);
10297 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
10299 last_alloc
= ins
.offset
;
10300 ret
= insert_reserved_file_extent(trans
, inode
,
10301 cur_offset
, ins
.objectid
,
10302 ins
.offset
, ins
.offset
,
10303 ins
.offset
, 0, 0, 0,
10304 BTRFS_FILE_EXTENT_PREALLOC
);
10306 btrfs_free_reserved_extent(fs_info
, ins
.objectid
,
10308 btrfs_abort_transaction(trans
, ret
);
10310 btrfs_end_transaction(trans
);
10314 btrfs_drop_extent_cache(BTRFS_I(inode
), cur_offset
,
10315 cur_offset
+ ins
.offset
-1, 0);
10317 em
= alloc_extent_map();
10319 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
10320 &BTRFS_I(inode
)->runtime_flags
);
10324 em
->start
= cur_offset
;
10325 em
->orig_start
= cur_offset
;
10326 em
->len
= ins
.offset
;
10327 em
->block_start
= ins
.objectid
;
10328 em
->block_len
= ins
.offset
;
10329 em
->orig_block_len
= ins
.offset
;
10330 em
->ram_bytes
= ins
.offset
;
10331 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
10332 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
10333 em
->generation
= trans
->transid
;
10336 write_lock(&em_tree
->lock
);
10337 ret
= add_extent_mapping(em_tree
, em
, 1);
10338 write_unlock(&em_tree
->lock
);
10339 if (ret
!= -EEXIST
)
10341 btrfs_drop_extent_cache(BTRFS_I(inode
), cur_offset
,
10342 cur_offset
+ ins
.offset
- 1,
10345 free_extent_map(em
);
10347 num_bytes
-= ins
.offset
;
10348 cur_offset
+= ins
.offset
;
10349 *alloc_hint
= ins
.objectid
+ ins
.offset
;
10351 inode_inc_iversion(inode
);
10352 inode
->i_ctime
= current_time(inode
);
10353 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
10354 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
10355 (actual_len
> inode
->i_size
) &&
10356 (cur_offset
> inode
->i_size
)) {
10357 if (cur_offset
> actual_len
)
10358 i_size
= actual_len
;
10360 i_size
= cur_offset
;
10361 i_size_write(inode
, i_size
);
10362 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
10365 ret
= btrfs_update_inode(trans
, root
, inode
);
10368 btrfs_abort_transaction(trans
, ret
);
10370 btrfs_end_transaction(trans
);
10375 btrfs_end_transaction(trans
);
10377 if (cur_offset
< end
)
10378 btrfs_free_reserved_data_space(inode
, NULL
, cur_offset
,
10379 end
- cur_offset
+ 1);
10383 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
10384 u64 start
, u64 num_bytes
, u64 min_size
,
10385 loff_t actual_len
, u64
*alloc_hint
)
10387 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
10388 min_size
, actual_len
, alloc_hint
,
10392 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
10393 struct btrfs_trans_handle
*trans
, int mode
,
10394 u64 start
, u64 num_bytes
, u64 min_size
,
10395 loff_t actual_len
, u64
*alloc_hint
)
10397 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
10398 min_size
, actual_len
, alloc_hint
, trans
);
10401 static int btrfs_set_page_dirty(struct page
*page
)
10403 return __set_page_dirty_nobuffers(page
);
10406 static int btrfs_permission(struct inode
*inode
, int mask
)
10408 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
10409 umode_t mode
= inode
->i_mode
;
10411 if (mask
& MAY_WRITE
&&
10412 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
10413 if (btrfs_root_readonly(root
))
10415 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
10418 return generic_permission(inode
, mask
);
10421 static int btrfs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
10423 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
10424 struct btrfs_trans_handle
*trans
;
10425 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
10426 struct inode
*inode
= NULL
;
10432 * 5 units required for adding orphan entry
10434 trans
= btrfs_start_transaction(root
, 5);
10436 return PTR_ERR(trans
);
10438 ret
= btrfs_find_free_ino(root
, &objectid
);
10442 inode
= btrfs_new_inode(trans
, root
, dir
, NULL
, 0,
10443 btrfs_ino(BTRFS_I(dir
)), objectid
, mode
, &index
);
10444 if (IS_ERR(inode
)) {
10445 ret
= PTR_ERR(inode
);
10450 inode
->i_fop
= &btrfs_file_operations
;
10451 inode
->i_op
= &btrfs_file_inode_operations
;
10453 inode
->i_mapping
->a_ops
= &btrfs_aops
;
10454 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
10456 ret
= btrfs_init_inode_security(trans
, inode
, dir
, NULL
);
10460 ret
= btrfs_update_inode(trans
, root
, inode
);
10463 ret
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
10468 * We set number of links to 0 in btrfs_new_inode(), and here we set
10469 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10472 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10474 set_nlink(inode
, 1);
10475 d_tmpfile(dentry
, inode
);
10476 unlock_new_inode(inode
);
10477 mark_inode_dirty(inode
);
10479 btrfs_end_transaction(trans
);
10481 discard_new_inode(inode
);
10482 btrfs_btree_balance_dirty(fs_info
);
10486 void btrfs_set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
10488 struct inode
*inode
= tree
->private_data
;
10489 unsigned long index
= start
>> PAGE_SHIFT
;
10490 unsigned long end_index
= end
>> PAGE_SHIFT
;
10493 while (index
<= end_index
) {
10494 page
= find_get_page(inode
->i_mapping
, index
);
10495 ASSERT(page
); /* Pages should be in the extent_io_tree */
10496 set_page_writeback(page
);
10504 * Add an entry indicating a block group or device which is pinned by a
10505 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10506 * negative errno on failure.
10508 static int btrfs_add_swapfile_pin(struct inode
*inode
, void *ptr
,
10509 bool is_block_group
)
10511 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
10512 struct btrfs_swapfile_pin
*sp
, *entry
;
10513 struct rb_node
**p
;
10514 struct rb_node
*parent
= NULL
;
10516 sp
= kmalloc(sizeof(*sp
), GFP_NOFS
);
10521 sp
->is_block_group
= is_block_group
;
10523 spin_lock(&fs_info
->swapfile_pins_lock
);
10524 p
= &fs_info
->swapfile_pins
.rb_node
;
10527 entry
= rb_entry(parent
, struct btrfs_swapfile_pin
, node
);
10528 if (sp
->ptr
< entry
->ptr
||
10529 (sp
->ptr
== entry
->ptr
&& sp
->inode
< entry
->inode
)) {
10530 p
= &(*p
)->rb_left
;
10531 } else if (sp
->ptr
> entry
->ptr
||
10532 (sp
->ptr
== entry
->ptr
&& sp
->inode
> entry
->inode
)) {
10533 p
= &(*p
)->rb_right
;
10535 spin_unlock(&fs_info
->swapfile_pins_lock
);
10540 rb_link_node(&sp
->node
, parent
, p
);
10541 rb_insert_color(&sp
->node
, &fs_info
->swapfile_pins
);
10542 spin_unlock(&fs_info
->swapfile_pins_lock
);
10546 /* Free all of the entries pinned by this swapfile. */
10547 static void btrfs_free_swapfile_pins(struct inode
*inode
)
10549 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
10550 struct btrfs_swapfile_pin
*sp
;
10551 struct rb_node
*node
, *next
;
10553 spin_lock(&fs_info
->swapfile_pins_lock
);
10554 node
= rb_first(&fs_info
->swapfile_pins
);
10556 next
= rb_next(node
);
10557 sp
= rb_entry(node
, struct btrfs_swapfile_pin
, node
);
10558 if (sp
->inode
== inode
) {
10559 rb_erase(&sp
->node
, &fs_info
->swapfile_pins
);
10560 if (sp
->is_block_group
)
10561 btrfs_put_block_group(sp
->ptr
);
10566 spin_unlock(&fs_info
->swapfile_pins_lock
);
10569 struct btrfs_swap_info
{
10575 unsigned long nr_pages
;
10579 static int btrfs_add_swap_extent(struct swap_info_struct
*sis
,
10580 struct btrfs_swap_info
*bsi
)
10582 unsigned long nr_pages
;
10583 u64 first_ppage
, first_ppage_reported
, next_ppage
;
10586 first_ppage
= ALIGN(bsi
->block_start
, PAGE_SIZE
) >> PAGE_SHIFT
;
10587 next_ppage
= ALIGN_DOWN(bsi
->block_start
+ bsi
->block_len
,
10588 PAGE_SIZE
) >> PAGE_SHIFT
;
10590 if (first_ppage
>= next_ppage
)
10592 nr_pages
= next_ppage
- first_ppage
;
10594 first_ppage_reported
= first_ppage
;
10595 if (bsi
->start
== 0)
10596 first_ppage_reported
++;
10597 if (bsi
->lowest_ppage
> first_ppage_reported
)
10598 bsi
->lowest_ppage
= first_ppage_reported
;
10599 if (bsi
->highest_ppage
< (next_ppage
- 1))
10600 bsi
->highest_ppage
= next_ppage
- 1;
10602 ret
= add_swap_extent(sis
, bsi
->nr_pages
, nr_pages
, first_ppage
);
10605 bsi
->nr_extents
+= ret
;
10606 bsi
->nr_pages
+= nr_pages
;
10610 static void btrfs_swap_deactivate(struct file
*file
)
10612 struct inode
*inode
= file_inode(file
);
10614 btrfs_free_swapfile_pins(inode
);
10615 atomic_dec(&BTRFS_I(inode
)->root
->nr_swapfiles
);
10618 static int btrfs_swap_activate(struct swap_info_struct
*sis
, struct file
*file
,
10621 struct inode
*inode
= file_inode(file
);
10622 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
10623 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
10624 struct extent_state
*cached_state
= NULL
;
10625 struct extent_map
*em
= NULL
;
10626 struct btrfs_device
*device
= NULL
;
10627 struct btrfs_swap_info bsi
= {
10628 .lowest_ppage
= (sector_t
)-1ULL,
10635 * If the swap file was just created, make sure delalloc is done. If the
10636 * file changes again after this, the user is doing something stupid and
10637 * we don't really care.
10639 ret
= btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
10644 * The inode is locked, so these flags won't change after we check them.
10646 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
) {
10647 btrfs_warn(fs_info
, "swapfile must not be compressed");
10650 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
)) {
10651 btrfs_warn(fs_info
, "swapfile must not be copy-on-write");
10654 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
10655 btrfs_warn(fs_info
, "swapfile must not be checksummed");
10660 * Balance or device remove/replace/resize can move stuff around from
10661 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10662 * concurrently while we are mapping the swap extents, and
10663 * fs_info->swapfile_pins prevents them from running while the swap file
10664 * is active and moving the extents. Note that this also prevents a
10665 * concurrent device add which isn't actually necessary, but it's not
10666 * really worth the trouble to allow it.
10668 if (test_and_set_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
)) {
10669 btrfs_warn(fs_info
,
10670 "cannot activate swapfile while exclusive operation is running");
10674 * Snapshots can create extents which require COW even if NODATACOW is
10675 * set. We use this counter to prevent snapshots. We must increment it
10676 * before walking the extents because we don't want a concurrent
10677 * snapshot to run after we've already checked the extents.
10679 atomic_inc(&BTRFS_I(inode
)->root
->nr_swapfiles
);
10681 isize
= ALIGN_DOWN(inode
->i_size
, fs_info
->sectorsize
);
10683 lock_extent_bits(io_tree
, 0, isize
- 1, &cached_state
);
10685 while (start
< isize
) {
10686 u64 logical_block_start
, physical_block_start
;
10687 struct btrfs_block_group_cache
*bg
;
10688 u64 len
= isize
- start
;
10690 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, start
, len
, 0);
10696 if (em
->block_start
== EXTENT_MAP_HOLE
) {
10697 btrfs_warn(fs_info
, "swapfile must not have holes");
10701 if (em
->block_start
== EXTENT_MAP_INLINE
) {
10703 * It's unlikely we'll ever actually find ourselves
10704 * here, as a file small enough to fit inline won't be
10705 * big enough to store more than the swap header, but in
10706 * case something changes in the future, let's catch it
10707 * here rather than later.
10709 btrfs_warn(fs_info
, "swapfile must not be inline");
10713 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
10714 btrfs_warn(fs_info
, "swapfile must not be compressed");
10719 logical_block_start
= em
->block_start
+ (start
- em
->start
);
10720 len
= min(len
, em
->len
- (start
- em
->start
));
10721 free_extent_map(em
);
10724 ret
= can_nocow_extent(inode
, start
, &len
, NULL
, NULL
, NULL
);
10730 btrfs_warn(fs_info
,
10731 "swapfile must not be copy-on-write");
10736 em
= btrfs_get_chunk_map(fs_info
, logical_block_start
, len
);
10742 if (em
->map_lookup
->type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) {
10743 btrfs_warn(fs_info
,
10744 "swapfile must have single data profile");
10749 if (device
== NULL
) {
10750 device
= em
->map_lookup
->stripes
[0].dev
;
10751 ret
= btrfs_add_swapfile_pin(inode
, device
, false);
10756 } else if (device
!= em
->map_lookup
->stripes
[0].dev
) {
10757 btrfs_warn(fs_info
, "swapfile must be on one device");
10762 physical_block_start
= (em
->map_lookup
->stripes
[0].physical
+
10763 (logical_block_start
- em
->start
));
10764 len
= min(len
, em
->len
- (logical_block_start
- em
->start
));
10765 free_extent_map(em
);
10768 bg
= btrfs_lookup_block_group(fs_info
, logical_block_start
);
10770 btrfs_warn(fs_info
,
10771 "could not find block group containing swapfile");
10776 ret
= btrfs_add_swapfile_pin(inode
, bg
, true);
10778 btrfs_put_block_group(bg
);
10785 if (bsi
.block_len
&&
10786 bsi
.block_start
+ bsi
.block_len
== physical_block_start
) {
10787 bsi
.block_len
+= len
;
10789 if (bsi
.block_len
) {
10790 ret
= btrfs_add_swap_extent(sis
, &bsi
);
10795 bsi
.block_start
= physical_block_start
;
10796 bsi
.block_len
= len
;
10803 ret
= btrfs_add_swap_extent(sis
, &bsi
);
10806 if (!IS_ERR_OR_NULL(em
))
10807 free_extent_map(em
);
10809 unlock_extent_cached(io_tree
, 0, isize
- 1, &cached_state
);
10812 btrfs_swap_deactivate(file
);
10814 clear_bit(BTRFS_FS_EXCL_OP
, &fs_info
->flags
);
10820 sis
->bdev
= device
->bdev
;
10821 *span
= bsi
.highest_ppage
- bsi
.lowest_ppage
+ 1;
10822 sis
->max
= bsi
.nr_pages
;
10823 sis
->pages
= bsi
.nr_pages
- 1;
10824 sis
->highest_bit
= bsi
.nr_pages
- 1;
10825 return bsi
.nr_extents
;
10828 static void btrfs_swap_deactivate(struct file
*file
)
10832 static int btrfs_swap_activate(struct swap_info_struct
*sis
, struct file
*file
,
10835 return -EOPNOTSUPP
;
10839 static const struct inode_operations btrfs_dir_inode_operations
= {
10840 .getattr
= btrfs_getattr
,
10841 .lookup
= btrfs_lookup
,
10842 .create
= btrfs_create
,
10843 .unlink
= btrfs_unlink
,
10844 .link
= btrfs_link
,
10845 .mkdir
= btrfs_mkdir
,
10846 .rmdir
= btrfs_rmdir
,
10847 .rename
= btrfs_rename2
,
10848 .symlink
= btrfs_symlink
,
10849 .setattr
= btrfs_setattr
,
10850 .mknod
= btrfs_mknod
,
10851 .listxattr
= btrfs_listxattr
,
10852 .permission
= btrfs_permission
,
10853 .get_acl
= btrfs_get_acl
,
10854 .set_acl
= btrfs_set_acl
,
10855 .update_time
= btrfs_update_time
,
10856 .tmpfile
= btrfs_tmpfile
,
10858 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
10859 .lookup
= btrfs_lookup
,
10860 .permission
= btrfs_permission
,
10861 .update_time
= btrfs_update_time
,
10864 static const struct file_operations btrfs_dir_file_operations
= {
10865 .llseek
= generic_file_llseek
,
10866 .read
= generic_read_dir
,
10867 .iterate_shared
= btrfs_real_readdir
,
10868 .open
= btrfs_opendir
,
10869 .unlocked_ioctl
= btrfs_ioctl
,
10870 #ifdef CONFIG_COMPAT
10871 .compat_ioctl
= btrfs_compat_ioctl
,
10873 .release
= btrfs_release_file
,
10874 .fsync
= btrfs_sync_file
,
10877 static const struct extent_io_ops btrfs_extent_io_ops
= {
10878 /* mandatory callbacks */
10879 .submit_bio_hook
= btrfs_submit_bio_hook
,
10880 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
10884 * btrfs doesn't support the bmap operation because swapfiles
10885 * use bmap to make a mapping of extents in the file. They assume
10886 * these extents won't change over the life of the file and they
10887 * use the bmap result to do IO directly to the drive.
10889 * the btrfs bmap call would return logical addresses that aren't
10890 * suitable for IO and they also will change frequently as COW
10891 * operations happen. So, swapfile + btrfs == corruption.
10893 * For now we're avoiding this by dropping bmap.
10895 static const struct address_space_operations btrfs_aops
= {
10896 .readpage
= btrfs_readpage
,
10897 .writepage
= btrfs_writepage
,
10898 .writepages
= btrfs_writepages
,
10899 .readpages
= btrfs_readpages
,
10900 .direct_IO
= btrfs_direct_IO
,
10901 .invalidatepage
= btrfs_invalidatepage
,
10902 .releasepage
= btrfs_releasepage
,
10903 .set_page_dirty
= btrfs_set_page_dirty
,
10904 .error_remove_page
= generic_error_remove_page
,
10905 .swap_activate
= btrfs_swap_activate
,
10906 .swap_deactivate
= btrfs_swap_deactivate
,
10909 static const struct inode_operations btrfs_file_inode_operations
= {
10910 .getattr
= btrfs_getattr
,
10911 .setattr
= btrfs_setattr
,
10912 .listxattr
= btrfs_listxattr
,
10913 .permission
= btrfs_permission
,
10914 .fiemap
= btrfs_fiemap
,
10915 .get_acl
= btrfs_get_acl
,
10916 .set_acl
= btrfs_set_acl
,
10917 .update_time
= btrfs_update_time
,
10919 static const struct inode_operations btrfs_special_inode_operations
= {
10920 .getattr
= btrfs_getattr
,
10921 .setattr
= btrfs_setattr
,
10922 .permission
= btrfs_permission
,
10923 .listxattr
= btrfs_listxattr
,
10924 .get_acl
= btrfs_get_acl
,
10925 .set_acl
= btrfs_set_acl
,
10926 .update_time
= btrfs_update_time
,
10928 static const struct inode_operations btrfs_symlink_inode_operations
= {
10929 .get_link
= page_get_link
,
10930 .getattr
= btrfs_getattr
,
10931 .setattr
= btrfs_setattr
,
10932 .permission
= btrfs_permission
,
10933 .listxattr
= btrfs_listxattr
,
10934 .update_time
= btrfs_update_time
,
10937 const struct dentry_operations btrfs_dentry_operations
= {
10938 .d_delete
= btrfs_dentry_delete
,