2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/xattr.h>
36 #include <linux/posix_acl.h>
37 #include <linux/falloc.h>
38 #include <linux/slab.h>
39 #include <linux/ratelimit.h>
40 #include <linux/mount.h>
41 #include <linux/btrfs.h>
42 #include <linux/blkdev.h>
43 #include <linux/posix_acl_xattr.h>
44 #include <linux/uio.h>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "print-tree.h"
50 #include "ordered-data.h"
54 #include "compression.h"
56 #include "free-space-cache.h"
57 #include "inode-map.h"
64 struct btrfs_iget_args
{
65 struct btrfs_key
*location
;
66 struct btrfs_root
*root
;
69 struct btrfs_dio_data
{
70 u64 outstanding_extents
;
72 u64 unsubmitted_oe_range_start
;
73 u64 unsubmitted_oe_range_end
;
77 static const struct inode_operations btrfs_dir_inode_operations
;
78 static const struct inode_operations btrfs_symlink_inode_operations
;
79 static const struct inode_operations btrfs_dir_ro_inode_operations
;
80 static const struct inode_operations btrfs_special_inode_operations
;
81 static const struct inode_operations btrfs_file_inode_operations
;
82 static const struct address_space_operations btrfs_aops
;
83 static const struct address_space_operations btrfs_symlink_aops
;
84 static const struct file_operations btrfs_dir_file_operations
;
85 static const struct extent_io_ops btrfs_extent_io_ops
;
87 static struct kmem_cache
*btrfs_inode_cachep
;
88 struct kmem_cache
*btrfs_trans_handle_cachep
;
89 struct kmem_cache
*btrfs_transaction_cachep
;
90 struct kmem_cache
*btrfs_path_cachep
;
91 struct kmem_cache
*btrfs_free_space_cachep
;
94 static const unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
95 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
96 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
97 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
98 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
99 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
100 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
101 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
104 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
105 static int btrfs_truncate(struct inode
*inode
);
106 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
107 static noinline
int cow_file_range(struct inode
*inode
,
108 struct page
*locked_page
,
109 u64 start
, u64 end
, u64 delalloc_end
,
110 int *page_started
, unsigned long *nr_written
,
111 int unlock
, struct btrfs_dedupe_hash
*hash
);
112 static struct extent_map
*create_io_em(struct inode
*inode
, u64 start
, u64 len
,
113 u64 orig_start
, u64 block_start
,
114 u64 block_len
, u64 orig_block_len
,
115 u64 ram_bytes
, int compress_type
,
118 static int btrfs_dirty_inode(struct inode
*inode
);
120 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121 void btrfs_test_inode_set_ops(struct inode
*inode
)
123 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
127 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
128 struct inode
*inode
, struct inode
*dir
,
129 const struct qstr
*qstr
)
133 err
= btrfs_init_acl(trans
, inode
, dir
);
135 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
144 static int insert_inline_extent(struct btrfs_trans_handle
*trans
,
145 struct btrfs_path
*path
, int extent_inserted
,
146 struct btrfs_root
*root
, struct inode
*inode
,
147 u64 start
, size_t size
, size_t compressed_size
,
149 struct page
**compressed_pages
)
151 struct extent_buffer
*leaf
;
152 struct page
*page
= NULL
;
155 struct btrfs_file_extent_item
*ei
;
158 size_t cur_size
= size
;
159 unsigned long offset
;
161 if (compressed_size
&& compressed_pages
)
162 cur_size
= compressed_size
;
164 inode_add_bytes(inode
, size
);
166 if (!extent_inserted
) {
167 struct btrfs_key key
;
170 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
172 key
.type
= BTRFS_EXTENT_DATA_KEY
;
174 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
175 path
->leave_spinning
= 1;
176 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
183 leaf
= path
->nodes
[0];
184 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
185 struct btrfs_file_extent_item
);
186 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
187 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
188 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
189 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
190 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
191 ptr
= btrfs_file_extent_inline_start(ei
);
193 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
196 while (compressed_size
> 0) {
197 cpage
= compressed_pages
[i
];
198 cur_size
= min_t(unsigned long, compressed_size
,
201 kaddr
= kmap_atomic(cpage
);
202 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
203 kunmap_atomic(kaddr
);
207 compressed_size
-= cur_size
;
209 btrfs_set_file_extent_compression(leaf
, ei
,
212 page
= find_get_page(inode
->i_mapping
,
213 start
>> PAGE_SHIFT
);
214 btrfs_set_file_extent_compression(leaf
, ei
, 0);
215 kaddr
= kmap_atomic(page
);
216 offset
= start
& (PAGE_SIZE
- 1);
217 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
218 kunmap_atomic(kaddr
);
221 btrfs_mark_buffer_dirty(leaf
);
222 btrfs_release_path(path
);
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
233 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
234 ret
= btrfs_update_inode(trans
, root
, inode
);
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
247 static noinline
int cow_file_range_inline(struct btrfs_root
*root
,
248 struct inode
*inode
, u64 start
,
249 u64 end
, size_t compressed_size
,
251 struct page
**compressed_pages
)
253 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
254 struct btrfs_trans_handle
*trans
;
255 u64 isize
= i_size_read(inode
);
256 u64 actual_end
= min(end
+ 1, isize
);
257 u64 inline_len
= actual_end
- start
;
258 u64 aligned_end
= ALIGN(end
, fs_info
->sectorsize
);
259 u64 data_len
= inline_len
;
261 struct btrfs_path
*path
;
262 int extent_inserted
= 0;
263 u32 extent_item_size
;
266 data_len
= compressed_size
;
269 actual_end
> fs_info
->sectorsize
||
270 data_len
> BTRFS_MAX_INLINE_DATA_SIZE(fs_info
) ||
272 (actual_end
& (fs_info
->sectorsize
- 1)) == 0) ||
274 data_len
> fs_info
->max_inline
) {
278 path
= btrfs_alloc_path();
282 trans
= btrfs_join_transaction(root
);
284 btrfs_free_path(path
);
285 return PTR_ERR(trans
);
287 trans
->block_rsv
= &fs_info
->delalloc_block_rsv
;
289 if (compressed_size
&& compressed_pages
)
290 extent_item_size
= btrfs_file_extent_calc_inline_size(
293 extent_item_size
= btrfs_file_extent_calc_inline_size(
296 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
,
297 start
, aligned_end
, NULL
,
298 1, 1, extent_item_size
, &extent_inserted
);
300 btrfs_abort_transaction(trans
, ret
);
304 if (isize
> actual_end
)
305 inline_len
= min_t(u64
, isize
, actual_end
);
306 ret
= insert_inline_extent(trans
, path
, extent_inserted
,
308 inline_len
, compressed_size
,
309 compress_type
, compressed_pages
);
310 if (ret
&& ret
!= -ENOSPC
) {
311 btrfs_abort_transaction(trans
, ret
);
313 } else if (ret
== -ENOSPC
) {
318 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
319 btrfs_delalloc_release_metadata(BTRFS_I(inode
), end
+ 1 - start
);
320 btrfs_drop_extent_cache(BTRFS_I(inode
), start
, aligned_end
- 1, 0);
323 * Don't forget to free the reserved space, as for inlined extent
324 * it won't count as data extent, free them directly here.
325 * And at reserve time, it's always aligned to page size, so
326 * just free one page here.
328 btrfs_qgroup_free_data(inode
, 0, PAGE_SIZE
);
329 btrfs_free_path(path
);
330 btrfs_end_transaction(trans
);
334 struct async_extent
{
339 unsigned long nr_pages
;
341 struct list_head list
;
346 struct btrfs_root
*root
;
347 struct page
*locked_page
;
350 struct list_head extents
;
351 struct btrfs_work work
;
354 static noinline
int add_async_extent(struct async_cow
*cow
,
355 u64 start
, u64 ram_size
,
358 unsigned long nr_pages
,
361 struct async_extent
*async_extent
;
363 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
364 BUG_ON(!async_extent
); /* -ENOMEM */
365 async_extent
->start
= start
;
366 async_extent
->ram_size
= ram_size
;
367 async_extent
->compressed_size
= compressed_size
;
368 async_extent
->pages
= pages
;
369 async_extent
->nr_pages
= nr_pages
;
370 async_extent
->compress_type
= compress_type
;
371 list_add_tail(&async_extent
->list
, &cow
->extents
);
375 static inline int inode_need_compress(struct inode
*inode
)
377 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
380 if (btrfs_test_opt(fs_info
, FORCE_COMPRESS
))
382 /* bad compression ratios */
383 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
385 if (btrfs_test_opt(fs_info
, COMPRESS
) ||
386 BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
||
387 BTRFS_I(inode
)->force_compress
)
392 static inline void inode_should_defrag(struct btrfs_inode
*inode
,
393 u64 start
, u64 end
, u64 num_bytes
, u64 small_write
)
395 /* If this is a small write inside eof, kick off a defrag */
396 if (num_bytes
< small_write
&&
397 (start
> 0 || end
+ 1 < inode
->disk_i_size
))
398 btrfs_add_inode_defrag(NULL
, inode
);
402 * we create compressed extents in two phases. The first
403 * phase compresses a range of pages that have already been
404 * locked (both pages and state bits are locked).
406 * This is done inside an ordered work queue, and the compression
407 * is spread across many cpus. The actual IO submission is step
408 * two, and the ordered work queue takes care of making sure that
409 * happens in the same order things were put onto the queue by
410 * writepages and friends.
412 * If this code finds it can't get good compression, it puts an
413 * entry onto the work queue to write the uncompressed bytes. This
414 * makes sure that both compressed inodes and uncompressed inodes
415 * are written in the same order that the flusher thread sent them
418 static noinline
void compress_file_range(struct inode
*inode
,
419 struct page
*locked_page
,
421 struct async_cow
*async_cow
,
424 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
425 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
427 u64 blocksize
= fs_info
->sectorsize
;
429 u64 isize
= i_size_read(inode
);
431 struct page
**pages
= NULL
;
432 unsigned long nr_pages
;
433 unsigned long nr_pages_ret
= 0;
434 unsigned long total_compressed
= 0;
435 unsigned long total_in
= 0;
436 unsigned long max_compressed
= SZ_128K
;
437 unsigned long max_uncompressed
= SZ_128K
;
440 int compress_type
= fs_info
->compress_type
;
443 inode_should_defrag(BTRFS_I(inode
), start
, end
, end
- start
+ 1,
446 actual_end
= min_t(u64
, isize
, end
+ 1);
449 nr_pages
= (end
>> PAGE_SHIFT
) - (start
>> PAGE_SHIFT
) + 1;
450 nr_pages
= min_t(unsigned long, nr_pages
, SZ_128K
/ PAGE_SIZE
);
453 * we don't want to send crud past the end of i_size through
454 * compression, that's just a waste of CPU time. So, if the
455 * end of the file is before the start of our current
456 * requested range of bytes, we bail out to the uncompressed
457 * cleanup code that can deal with all of this.
459 * It isn't really the fastest way to fix things, but this is a
460 * very uncommon corner.
462 if (actual_end
<= start
)
463 goto cleanup_and_bail_uncompressed
;
465 total_compressed
= actual_end
- start
;
468 * skip compression for a small file range(<=blocksize) that
469 * isn't an inline extent, since it doesn't save disk space at all.
471 if (total_compressed
<= blocksize
&&
472 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
473 goto cleanup_and_bail_uncompressed
;
475 /* we want to make sure that amount of ram required to uncompress
476 * an extent is reasonable, so we limit the total size in ram
477 * of a compressed extent to 128k. This is a crucial number
478 * because it also controls how easily we can spread reads across
479 * cpus for decompression.
481 * We also want to make sure the amount of IO required to do
482 * a random read is reasonably small, so we limit the size of
483 * a compressed extent to 128k.
485 total_compressed
= min(total_compressed
, max_uncompressed
);
486 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
487 num_bytes
= max(blocksize
, num_bytes
);
492 * we do compression for mount -o compress and when the
493 * inode has not been flagged as nocompress. This flag can
494 * change at any time if we discover bad compression ratios.
496 if (inode_need_compress(inode
)) {
498 pages
= kcalloc(nr_pages
, sizeof(struct page
*), GFP_NOFS
);
500 /* just bail out to the uncompressed code */
504 if (BTRFS_I(inode
)->force_compress
)
505 compress_type
= BTRFS_I(inode
)->force_compress
;
508 * we need to call clear_page_dirty_for_io on each
509 * page in the range. Otherwise applications with the file
510 * mmap'd can wander in and change the page contents while
511 * we are compressing them.
513 * If the compression fails for any reason, we set the pages
514 * dirty again later on.
516 extent_range_clear_dirty_for_io(inode
, start
, end
);
518 ret
= btrfs_compress_pages(compress_type
,
519 inode
->i_mapping
, start
,
520 total_compressed
, pages
,
521 nr_pages
, &nr_pages_ret
,
527 unsigned long offset
= total_compressed
&
529 struct page
*page
= pages
[nr_pages_ret
- 1];
532 /* zero the tail end of the last page, we might be
533 * sending it down to disk
536 kaddr
= kmap_atomic(page
);
537 memset(kaddr
+ offset
, 0,
539 kunmap_atomic(kaddr
);
546 /* lets try to make an inline extent */
547 if (ret
|| total_in
< (actual_end
- start
)) {
548 /* we didn't compress the entire range, try
549 * to make an uncompressed inline extent.
551 ret
= cow_file_range_inline(root
, inode
, start
, end
,
552 0, BTRFS_COMPRESS_NONE
, NULL
);
554 /* try making a compressed inline extent */
555 ret
= cow_file_range_inline(root
, inode
, start
, end
,
557 compress_type
, pages
);
560 unsigned long clear_flags
= EXTENT_DELALLOC
|
562 unsigned long page_error_op
;
564 clear_flags
|= (ret
< 0) ? EXTENT_DO_ACCOUNTING
: 0;
565 page_error_op
= ret
< 0 ? PAGE_SET_ERROR
: 0;
568 * inline extent creation worked or returned error,
569 * we don't need to create any more async work items.
570 * Unlock and free up our temp pages.
572 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
579 btrfs_free_reserved_data_space_noquota(inode
, start
,
587 * we aren't doing an inline extent round the compressed size
588 * up to a block size boundary so the allocator does sane
591 total_compressed
= ALIGN(total_compressed
, blocksize
);
594 * one last check to make sure the compression is really a
595 * win, compare the page count read with the blocks on disk
597 total_in
= ALIGN(total_in
, PAGE_SIZE
);
598 if (total_compressed
>= total_in
) {
601 num_bytes
= total_in
;
605 * The async work queues will take care of doing actual
606 * allocation on disk for these compressed pages, and
607 * will submit them to the elevator.
609 add_async_extent(async_cow
, start
, num_bytes
,
610 total_compressed
, pages
, nr_pages_ret
,
613 if (start
+ num_bytes
< end
) {
624 * the compression code ran but failed to make things smaller,
625 * free any pages it allocated and our page pointer array
627 for (i
= 0; i
< nr_pages_ret
; i
++) {
628 WARN_ON(pages
[i
]->mapping
);
633 total_compressed
= 0;
636 /* flag the file so we don't compress in the future */
637 if (!btrfs_test_opt(fs_info
, FORCE_COMPRESS
) &&
638 !(BTRFS_I(inode
)->force_compress
)) {
639 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
642 cleanup_and_bail_uncompressed
:
644 * No compression, but we still need to write the pages in the file
645 * we've been given so far. redirty the locked page if it corresponds
646 * to our extent and set things up for the async work queue to run
647 * cow_file_range to do the normal delalloc dance.
649 if (page_offset(locked_page
) >= start
&&
650 page_offset(locked_page
) <= end
)
651 __set_page_dirty_nobuffers(locked_page
);
652 /* unlocked later on in the async handlers */
655 extent_range_redirty_for_io(inode
, start
, end
);
656 add_async_extent(async_cow
, start
, end
- start
+ 1, 0, NULL
, 0,
657 BTRFS_COMPRESS_NONE
);
663 for (i
= 0; i
< nr_pages_ret
; i
++) {
664 WARN_ON(pages
[i
]->mapping
);
670 static void free_async_extent_pages(struct async_extent
*async_extent
)
674 if (!async_extent
->pages
)
677 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
678 WARN_ON(async_extent
->pages
[i
]->mapping
);
679 put_page(async_extent
->pages
[i
]);
681 kfree(async_extent
->pages
);
682 async_extent
->nr_pages
= 0;
683 async_extent
->pages
= NULL
;
687 * phase two of compressed writeback. This is the ordered portion
688 * of the code, which only gets called in the order the work was
689 * queued. We walk all the async extents created by compress_file_range
690 * and send them down to the disk.
692 static noinline
void submit_compressed_extents(struct inode
*inode
,
693 struct async_cow
*async_cow
)
695 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
696 struct async_extent
*async_extent
;
698 struct btrfs_key ins
;
699 struct extent_map
*em
;
700 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
701 struct extent_io_tree
*io_tree
;
705 while (!list_empty(&async_cow
->extents
)) {
706 async_extent
= list_entry(async_cow
->extents
.next
,
707 struct async_extent
, list
);
708 list_del(&async_extent
->list
);
710 io_tree
= &BTRFS_I(inode
)->io_tree
;
713 /* did the compression code fall back to uncompressed IO? */
714 if (!async_extent
->pages
) {
715 int page_started
= 0;
716 unsigned long nr_written
= 0;
718 lock_extent(io_tree
, async_extent
->start
,
719 async_extent
->start
+
720 async_extent
->ram_size
- 1);
722 /* allocate blocks */
723 ret
= cow_file_range(inode
, async_cow
->locked_page
,
725 async_extent
->start
+
726 async_extent
->ram_size
- 1,
727 async_extent
->start
+
728 async_extent
->ram_size
- 1,
729 &page_started
, &nr_written
, 0,
735 * if page_started, cow_file_range inserted an
736 * inline extent and took care of all the unlocking
737 * and IO for us. Otherwise, we need to submit
738 * all those pages down to the drive.
740 if (!page_started
&& !ret
)
741 extent_write_locked_range(io_tree
,
742 inode
, async_extent
->start
,
743 async_extent
->start
+
744 async_extent
->ram_size
- 1,
748 unlock_page(async_cow
->locked_page
);
754 lock_extent(io_tree
, async_extent
->start
,
755 async_extent
->start
+ async_extent
->ram_size
- 1);
757 ret
= btrfs_reserve_extent(root
, async_extent
->ram_size
,
758 async_extent
->compressed_size
,
759 async_extent
->compressed_size
,
760 0, alloc_hint
, &ins
, 1, 1);
762 free_async_extent_pages(async_extent
);
764 if (ret
== -ENOSPC
) {
765 unlock_extent(io_tree
, async_extent
->start
,
766 async_extent
->start
+
767 async_extent
->ram_size
- 1);
770 * we need to redirty the pages if we decide to
771 * fallback to uncompressed IO, otherwise we
772 * will not submit these pages down to lower
775 extent_range_redirty_for_io(inode
,
777 async_extent
->start
+
778 async_extent
->ram_size
- 1);
785 * here we're doing allocation and writeback of the
788 em
= create_io_em(inode
, async_extent
->start
,
789 async_extent
->ram_size
, /* len */
790 async_extent
->start
, /* orig_start */
791 ins
.objectid
, /* block_start */
792 ins
.offset
, /* block_len */
793 ins
.offset
, /* orig_block_len */
794 async_extent
->ram_size
, /* ram_bytes */
795 async_extent
->compress_type
,
796 BTRFS_ORDERED_COMPRESSED
);
798 /* ret value is not necessary due to void function */
799 goto out_free_reserve
;
802 ret
= btrfs_add_ordered_extent_compress(inode
,
805 async_extent
->ram_size
,
807 BTRFS_ORDERED_COMPRESSED
,
808 async_extent
->compress_type
);
810 btrfs_drop_extent_cache(BTRFS_I(inode
),
812 async_extent
->start
+
813 async_extent
->ram_size
- 1, 0);
814 goto out_free_reserve
;
816 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
819 * clear dirty, set writeback and unlock the pages.
821 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
822 async_extent
->start
+
823 async_extent
->ram_size
- 1,
824 async_extent
->start
+
825 async_extent
->ram_size
- 1,
826 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
,
827 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
829 ret
= btrfs_submit_compressed_write(inode
,
831 async_extent
->ram_size
,
833 ins
.offset
, async_extent
->pages
,
834 async_extent
->nr_pages
);
836 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
837 struct page
*p
= async_extent
->pages
[0];
838 const u64 start
= async_extent
->start
;
839 const u64 end
= start
+ async_extent
->ram_size
- 1;
841 p
->mapping
= inode
->i_mapping
;
842 tree
->ops
->writepage_end_io_hook(p
, start
, end
,
845 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
849 free_async_extent_pages(async_extent
);
851 alloc_hint
= ins
.objectid
+ ins
.offset
;
857 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
858 btrfs_free_reserved_extent(fs_info
, ins
.objectid
, ins
.offset
, 1);
860 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
861 async_extent
->start
+
862 async_extent
->ram_size
- 1,
863 async_extent
->start
+
864 async_extent
->ram_size
- 1,
865 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
|
866 EXTENT_DEFRAG
| EXTENT_DO_ACCOUNTING
,
867 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
868 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
|
870 free_async_extent_pages(async_extent
);
875 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
878 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
879 struct extent_map
*em
;
882 read_lock(&em_tree
->lock
);
883 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
886 * if block start isn't an actual block number then find the
887 * first block in this inode and use that as a hint. If that
888 * block is also bogus then just don't worry about it.
890 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
892 em
= search_extent_mapping(em_tree
, 0, 0);
893 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
894 alloc_hint
= em
->block_start
;
898 alloc_hint
= em
->block_start
;
902 read_unlock(&em_tree
->lock
);
908 * when extent_io.c finds a delayed allocation range in the file,
909 * the call backs end up in this code. The basic idea is to
910 * allocate extents on disk for the range, and create ordered data structs
911 * in ram to track those extents.
913 * locked_page is the page that writepage had locked already. We use
914 * it to make sure we don't do extra locks or unlocks.
916 * *page_started is set to one if we unlock locked_page and do everything
917 * required to start IO on it. It may be clean and already done with
920 static noinline
int cow_file_range(struct inode
*inode
,
921 struct page
*locked_page
,
922 u64 start
, u64 end
, u64 delalloc_end
,
923 int *page_started
, unsigned long *nr_written
,
924 int unlock
, struct btrfs_dedupe_hash
*hash
)
926 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
927 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
930 unsigned long ram_size
;
933 u64 blocksize
= fs_info
->sectorsize
;
934 struct btrfs_key ins
;
935 struct extent_map
*em
;
938 if (btrfs_is_free_space_inode(BTRFS_I(inode
))) {
944 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
945 num_bytes
= max(blocksize
, num_bytes
);
946 disk_num_bytes
= num_bytes
;
948 inode_should_defrag(BTRFS_I(inode
), start
, end
, num_bytes
, SZ_64K
);
951 /* lets try to make an inline extent */
952 ret
= cow_file_range_inline(root
, inode
, start
, end
, 0,
953 BTRFS_COMPRESS_NONE
, NULL
);
955 extent_clear_unlock_delalloc(inode
, start
, end
,
957 EXTENT_LOCKED
| EXTENT_DELALLOC
|
958 EXTENT_DEFRAG
, PAGE_UNLOCK
|
959 PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
961 btrfs_free_reserved_data_space_noquota(inode
, start
,
963 *nr_written
= *nr_written
+
964 (end
- start
+ PAGE_SIZE
) / PAGE_SIZE
;
967 } else if (ret
< 0) {
972 BUG_ON(disk_num_bytes
>
973 btrfs_super_total_bytes(fs_info
->super_copy
));
975 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
976 btrfs_drop_extent_cache(BTRFS_I(inode
), start
,
977 start
+ num_bytes
- 1, 0);
979 while (disk_num_bytes
> 0) {
982 cur_alloc_size
= disk_num_bytes
;
983 ret
= btrfs_reserve_extent(root
, cur_alloc_size
, cur_alloc_size
,
984 fs_info
->sectorsize
, 0, alloc_hint
,
989 ram_size
= ins
.offset
;
990 em
= create_io_em(inode
, start
, ins
.offset
, /* len */
991 start
, /* orig_start */
992 ins
.objectid
, /* block_start */
993 ins
.offset
, /* block_len */
994 ins
.offset
, /* orig_block_len */
995 ram_size
, /* ram_bytes */
996 BTRFS_COMPRESS_NONE
, /* compress_type */
997 BTRFS_ORDERED_REGULAR
/* type */);
1000 free_extent_map(em
);
1002 cur_alloc_size
= ins
.offset
;
1003 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
1004 ram_size
, cur_alloc_size
, 0);
1006 goto out_drop_extent_cache
;
1008 if (root
->root_key
.objectid
==
1009 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1010 ret
= btrfs_reloc_clone_csums(inode
, start
,
1013 goto out_drop_extent_cache
;
1016 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
1018 if (disk_num_bytes
< cur_alloc_size
)
1021 /* we're not doing compressed IO, don't unlock the first
1022 * page (which the caller expects to stay locked), don't
1023 * clear any dirty bits and don't set any writeback bits
1025 * Do set the Private2 bit so we know this page was properly
1026 * setup for writepage
1028 op
= unlock
? PAGE_UNLOCK
: 0;
1029 op
|= PAGE_SET_PRIVATE2
;
1031 extent_clear_unlock_delalloc(inode
, start
,
1032 start
+ ram_size
- 1,
1033 delalloc_end
, locked_page
,
1034 EXTENT_LOCKED
| EXTENT_DELALLOC
,
1036 disk_num_bytes
-= cur_alloc_size
;
1037 num_bytes
-= cur_alloc_size
;
1038 alloc_hint
= ins
.objectid
+ ins
.offset
;
1039 start
+= cur_alloc_size
;
1044 out_drop_extent_cache
:
1045 btrfs_drop_extent_cache(BTRFS_I(inode
), start
, start
+ ram_size
- 1, 0);
1047 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
1048 btrfs_free_reserved_extent(fs_info
, ins
.objectid
, ins
.offset
, 1);
1050 extent_clear_unlock_delalloc(inode
, start
, end
, delalloc_end
,
1052 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
1053 EXTENT_DELALLOC
| EXTENT_DEFRAG
,
1054 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
1055 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
);
1060 * work queue call back to started compression on a file and pages
1062 static noinline
void async_cow_start(struct btrfs_work
*work
)
1064 struct async_cow
*async_cow
;
1066 async_cow
= container_of(work
, struct async_cow
, work
);
1068 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1069 async_cow
->start
, async_cow
->end
, async_cow
,
1071 if (num_added
== 0) {
1072 btrfs_add_delayed_iput(async_cow
->inode
);
1073 async_cow
->inode
= NULL
;
1078 * work queue call back to submit previously compressed pages
1080 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1082 struct btrfs_fs_info
*fs_info
;
1083 struct async_cow
*async_cow
;
1084 struct btrfs_root
*root
;
1085 unsigned long nr_pages
;
1087 async_cow
= container_of(work
, struct async_cow
, work
);
1089 root
= async_cow
->root
;
1090 fs_info
= root
->fs_info
;
1091 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_SIZE
) >>
1095 * atomic_sub_return implies a barrier for waitqueue_active
1097 if (atomic_sub_return(nr_pages
, &fs_info
->async_delalloc_pages
) <
1099 waitqueue_active(&fs_info
->async_submit_wait
))
1100 wake_up(&fs_info
->async_submit_wait
);
1102 if (async_cow
->inode
)
1103 submit_compressed_extents(async_cow
->inode
, async_cow
);
1106 static noinline
void async_cow_free(struct btrfs_work
*work
)
1108 struct async_cow
*async_cow
;
1109 async_cow
= container_of(work
, struct async_cow
, work
);
1110 if (async_cow
->inode
)
1111 btrfs_add_delayed_iput(async_cow
->inode
);
1115 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1116 u64 start
, u64 end
, int *page_started
,
1117 unsigned long *nr_written
)
1119 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1120 struct async_cow
*async_cow
;
1121 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1122 unsigned long nr_pages
;
1125 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1126 1, 0, NULL
, GFP_NOFS
);
1127 while (start
< end
) {
1128 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1129 BUG_ON(!async_cow
); /* -ENOMEM */
1130 async_cow
->inode
= igrab(inode
);
1131 async_cow
->root
= root
;
1132 async_cow
->locked_page
= locked_page
;
1133 async_cow
->start
= start
;
1135 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
&&
1136 !btrfs_test_opt(fs_info
, FORCE_COMPRESS
))
1139 cur_end
= min(end
, start
+ SZ_512K
- 1);
1141 async_cow
->end
= cur_end
;
1142 INIT_LIST_HEAD(&async_cow
->extents
);
1144 btrfs_init_work(&async_cow
->work
,
1145 btrfs_delalloc_helper
,
1146 async_cow_start
, async_cow_submit
,
1149 nr_pages
= (cur_end
- start
+ PAGE_SIZE
) >>
1151 atomic_add(nr_pages
, &fs_info
->async_delalloc_pages
);
1153 btrfs_queue_work(fs_info
->delalloc_workers
, &async_cow
->work
);
1155 while (atomic_read(&fs_info
->async_submit_draining
) &&
1156 atomic_read(&fs_info
->async_delalloc_pages
)) {
1157 wait_event(fs_info
->async_submit_wait
,
1158 (atomic_read(&fs_info
->async_delalloc_pages
) ==
1162 *nr_written
+= nr_pages
;
1163 start
= cur_end
+ 1;
1169 static noinline
int csum_exist_in_range(struct btrfs_fs_info
*fs_info
,
1170 u64 bytenr
, u64 num_bytes
)
1173 struct btrfs_ordered_sum
*sums
;
1176 ret
= btrfs_lookup_csums_range(fs_info
->csum_root
, bytenr
,
1177 bytenr
+ num_bytes
- 1, &list
, 0);
1178 if (ret
== 0 && list_empty(&list
))
1181 while (!list_empty(&list
)) {
1182 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1183 list_del(&sums
->list
);
1190 * when nowcow writeback call back. This checks for snapshots or COW copies
1191 * of the extents that exist in the file, and COWs the file as required.
1193 * If no cow copies or snapshots exist, we write directly to the existing
1196 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1197 struct page
*locked_page
,
1198 u64 start
, u64 end
, int *page_started
, int force
,
1199 unsigned long *nr_written
)
1201 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1202 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1203 struct extent_buffer
*leaf
;
1204 struct btrfs_path
*path
;
1205 struct btrfs_file_extent_item
*fi
;
1206 struct btrfs_key found_key
;
1207 struct extent_map
*em
;
1222 u64 ino
= btrfs_ino(BTRFS_I(inode
));
1224 path
= btrfs_alloc_path();
1226 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
1228 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1229 EXTENT_DO_ACCOUNTING
|
1230 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1232 PAGE_SET_WRITEBACK
|
1233 PAGE_END_WRITEBACK
);
1237 nolock
= btrfs_is_free_space_inode(BTRFS_I(inode
));
1239 cow_start
= (u64
)-1;
1242 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, ino
,
1246 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1247 leaf
= path
->nodes
[0];
1248 btrfs_item_key_to_cpu(leaf
, &found_key
,
1249 path
->slots
[0] - 1);
1250 if (found_key
.objectid
== ino
&&
1251 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1256 leaf
= path
->nodes
[0];
1257 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1258 ret
= btrfs_next_leaf(root
, path
);
1263 leaf
= path
->nodes
[0];
1269 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1271 if (found_key
.objectid
> ino
)
1273 if (WARN_ON_ONCE(found_key
.objectid
< ino
) ||
1274 found_key
.type
< BTRFS_EXTENT_DATA_KEY
) {
1278 if (found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1279 found_key
.offset
> end
)
1282 if (found_key
.offset
> cur_offset
) {
1283 extent_end
= found_key
.offset
;
1288 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1289 struct btrfs_file_extent_item
);
1290 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1292 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1293 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1294 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1295 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1296 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1297 extent_end
= found_key
.offset
+
1298 btrfs_file_extent_num_bytes(leaf
, fi
);
1300 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1301 if (extent_end
<= start
) {
1305 if (disk_bytenr
== 0)
1307 if (btrfs_file_extent_compression(leaf
, fi
) ||
1308 btrfs_file_extent_encryption(leaf
, fi
) ||
1309 btrfs_file_extent_other_encoding(leaf
, fi
))
1311 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1313 if (btrfs_extent_readonly(fs_info
, disk_bytenr
))
1315 if (btrfs_cross_ref_exist(root
, ino
,
1317 extent_offset
, disk_bytenr
))
1319 disk_bytenr
+= extent_offset
;
1320 disk_bytenr
+= cur_offset
- found_key
.offset
;
1321 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1323 * if there are pending snapshots for this root,
1324 * we fall into common COW way.
1327 err
= btrfs_start_write_no_snapshoting(root
);
1332 * force cow if csum exists in the range.
1333 * this ensure that csum for a given extent are
1334 * either valid or do not exist.
1336 if (csum_exist_in_range(fs_info
, disk_bytenr
,
1339 if (!btrfs_inc_nocow_writers(fs_info
, disk_bytenr
))
1342 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1343 extent_end
= found_key
.offset
+
1344 btrfs_file_extent_inline_len(leaf
,
1345 path
->slots
[0], fi
);
1346 extent_end
= ALIGN(extent_end
,
1347 fs_info
->sectorsize
);
1352 if (extent_end
<= start
) {
1354 if (!nolock
&& nocow
)
1355 btrfs_end_write_no_snapshoting(root
);
1357 btrfs_dec_nocow_writers(fs_info
, disk_bytenr
);
1361 if (cow_start
== (u64
)-1)
1362 cow_start
= cur_offset
;
1363 cur_offset
= extent_end
;
1364 if (cur_offset
> end
)
1370 btrfs_release_path(path
);
1371 if (cow_start
!= (u64
)-1) {
1372 ret
= cow_file_range(inode
, locked_page
,
1373 cow_start
, found_key
.offset
- 1,
1374 end
, page_started
, nr_written
, 1,
1377 if (!nolock
&& nocow
)
1378 btrfs_end_write_no_snapshoting(root
);
1380 btrfs_dec_nocow_writers(fs_info
,
1384 cow_start
= (u64
)-1;
1387 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1388 u64 orig_start
= found_key
.offset
- extent_offset
;
1390 em
= create_io_em(inode
, cur_offset
, num_bytes
,
1392 disk_bytenr
, /* block_start */
1393 num_bytes
, /* block_len */
1394 disk_num_bytes
, /* orig_block_len */
1395 ram_bytes
, BTRFS_COMPRESS_NONE
,
1396 BTRFS_ORDERED_PREALLOC
);
1398 if (!nolock
&& nocow
)
1399 btrfs_end_write_no_snapshoting(root
);
1401 btrfs_dec_nocow_writers(fs_info
,
1406 free_extent_map(em
);
1409 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1410 type
= BTRFS_ORDERED_PREALLOC
;
1412 type
= BTRFS_ORDERED_NOCOW
;
1415 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1416 num_bytes
, num_bytes
, type
);
1418 btrfs_dec_nocow_writers(fs_info
, disk_bytenr
);
1419 BUG_ON(ret
); /* -ENOMEM */
1421 if (root
->root_key
.objectid
==
1422 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1423 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1426 if (!nolock
&& nocow
)
1427 btrfs_end_write_no_snapshoting(root
);
1432 extent_clear_unlock_delalloc(inode
, cur_offset
,
1433 cur_offset
+ num_bytes
- 1, end
,
1434 locked_page
, EXTENT_LOCKED
|
1436 EXTENT_CLEAR_DATA_RESV
,
1437 PAGE_UNLOCK
| PAGE_SET_PRIVATE2
);
1439 if (!nolock
&& nocow
)
1440 btrfs_end_write_no_snapshoting(root
);
1441 cur_offset
= extent_end
;
1442 if (cur_offset
> end
)
1445 btrfs_release_path(path
);
1447 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1448 cow_start
= cur_offset
;
1452 if (cow_start
!= (u64
)-1) {
1453 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
, end
,
1454 page_started
, nr_written
, 1, NULL
);
1460 if (ret
&& cur_offset
< end
)
1461 extent_clear_unlock_delalloc(inode
, cur_offset
, end
, end
,
1462 locked_page
, EXTENT_LOCKED
|
1463 EXTENT_DELALLOC
| EXTENT_DEFRAG
|
1464 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1466 PAGE_SET_WRITEBACK
|
1467 PAGE_END_WRITEBACK
);
1468 btrfs_free_path(path
);
1472 static inline int need_force_cow(struct inode
*inode
, u64 start
, u64 end
)
1475 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
1476 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
))
1480 * @defrag_bytes is a hint value, no spinlock held here,
1481 * if is not zero, it means the file is defragging.
1482 * Force cow if given extent needs to be defragged.
1484 if (BTRFS_I(inode
)->defrag_bytes
&&
1485 test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
1486 EXTENT_DEFRAG
, 0, NULL
))
1493 * extent_io.c call back to do delayed allocation processing
1495 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1496 u64 start
, u64 end
, int *page_started
,
1497 unsigned long *nr_written
)
1500 int force_cow
= need_force_cow(inode
, start
, end
);
1502 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
&& !force_cow
) {
1503 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1504 page_started
, 1, nr_written
);
1505 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
&& !force_cow
) {
1506 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1507 page_started
, 0, nr_written
);
1508 } else if (!inode_need_compress(inode
)) {
1509 ret
= cow_file_range(inode
, locked_page
, start
, end
, end
,
1510 page_started
, nr_written
, 1, NULL
);
1512 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1513 &BTRFS_I(inode
)->runtime_flags
);
1514 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1515 page_started
, nr_written
);
1520 static void btrfs_split_extent_hook(struct inode
*inode
,
1521 struct extent_state
*orig
, u64 split
)
1525 /* not delalloc, ignore it */
1526 if (!(orig
->state
& EXTENT_DELALLOC
))
1529 size
= orig
->end
- orig
->start
+ 1;
1530 if (size
> BTRFS_MAX_EXTENT_SIZE
) {
1535 * See the explanation in btrfs_merge_extent_hook, the same
1536 * applies here, just in reverse.
1538 new_size
= orig
->end
- split
+ 1;
1539 num_extents
= count_max_extents(new_size
);
1540 new_size
= split
- orig
->start
;
1541 num_extents
+= count_max_extents(new_size
);
1542 if (count_max_extents(size
) >= num_extents
)
1546 spin_lock(&BTRFS_I(inode
)->lock
);
1547 BTRFS_I(inode
)->outstanding_extents
++;
1548 spin_unlock(&BTRFS_I(inode
)->lock
);
1552 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1553 * extents so we can keep track of new extents that are just merged onto old
1554 * extents, such as when we are doing sequential writes, so we can properly
1555 * account for the metadata space we'll need.
1557 static void btrfs_merge_extent_hook(struct inode
*inode
,
1558 struct extent_state
*new,
1559 struct extent_state
*other
)
1561 u64 new_size
, old_size
;
1564 /* not delalloc, ignore it */
1565 if (!(other
->state
& EXTENT_DELALLOC
))
1568 if (new->start
> other
->start
)
1569 new_size
= new->end
- other
->start
+ 1;
1571 new_size
= other
->end
- new->start
+ 1;
1573 /* we're not bigger than the max, unreserve the space and go */
1574 if (new_size
<= BTRFS_MAX_EXTENT_SIZE
) {
1575 spin_lock(&BTRFS_I(inode
)->lock
);
1576 BTRFS_I(inode
)->outstanding_extents
--;
1577 spin_unlock(&BTRFS_I(inode
)->lock
);
1582 * We have to add up either side to figure out how many extents were
1583 * accounted for before we merged into one big extent. If the number of
1584 * extents we accounted for is <= the amount we need for the new range
1585 * then we can return, otherwise drop. Think of it like this
1589 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1590 * need 2 outstanding extents, on one side we have 1 and the other side
1591 * we have 1 so they are == and we can return. But in this case
1593 * [MAX_SIZE+4k][MAX_SIZE+4k]
1595 * Each range on their own accounts for 2 extents, but merged together
1596 * they are only 3 extents worth of accounting, so we need to drop in
1599 old_size
= other
->end
- other
->start
+ 1;
1600 num_extents
= count_max_extents(old_size
);
1601 old_size
= new->end
- new->start
+ 1;
1602 num_extents
+= count_max_extents(old_size
);
1603 if (count_max_extents(new_size
) >= num_extents
)
1606 spin_lock(&BTRFS_I(inode
)->lock
);
1607 BTRFS_I(inode
)->outstanding_extents
--;
1608 spin_unlock(&BTRFS_I(inode
)->lock
);
1611 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1612 struct inode
*inode
)
1614 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1616 spin_lock(&root
->delalloc_lock
);
1617 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1618 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1619 &root
->delalloc_inodes
);
1620 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1621 &BTRFS_I(inode
)->runtime_flags
);
1622 root
->nr_delalloc_inodes
++;
1623 if (root
->nr_delalloc_inodes
== 1) {
1624 spin_lock(&fs_info
->delalloc_root_lock
);
1625 BUG_ON(!list_empty(&root
->delalloc_root
));
1626 list_add_tail(&root
->delalloc_root
,
1627 &fs_info
->delalloc_roots
);
1628 spin_unlock(&fs_info
->delalloc_root_lock
);
1631 spin_unlock(&root
->delalloc_lock
);
1634 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1635 struct btrfs_inode
*inode
)
1637 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
1639 spin_lock(&root
->delalloc_lock
);
1640 if (!list_empty(&inode
->delalloc_inodes
)) {
1641 list_del_init(&inode
->delalloc_inodes
);
1642 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1643 &inode
->runtime_flags
);
1644 root
->nr_delalloc_inodes
--;
1645 if (!root
->nr_delalloc_inodes
) {
1646 spin_lock(&fs_info
->delalloc_root_lock
);
1647 BUG_ON(list_empty(&root
->delalloc_root
));
1648 list_del_init(&root
->delalloc_root
);
1649 spin_unlock(&fs_info
->delalloc_root_lock
);
1652 spin_unlock(&root
->delalloc_lock
);
1656 * extent_io.c set_bit_hook, used to track delayed allocation
1657 * bytes in this file, and to maintain the list of inodes that
1658 * have pending delalloc work to be done.
1660 static void btrfs_set_bit_hook(struct inode
*inode
,
1661 struct extent_state
*state
, unsigned *bits
)
1664 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1666 if ((*bits
& EXTENT_DEFRAG
) && !(*bits
& EXTENT_DELALLOC
))
1669 * set_bit and clear bit hooks normally require _irqsave/restore
1670 * but in this case, we are only testing for the DELALLOC
1671 * bit, which is only set or cleared with irqs on
1673 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1674 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1675 u64 len
= state
->end
+ 1 - state
->start
;
1676 bool do_list
= !btrfs_is_free_space_inode(BTRFS_I(inode
));
1678 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1679 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1681 spin_lock(&BTRFS_I(inode
)->lock
);
1682 BTRFS_I(inode
)->outstanding_extents
++;
1683 spin_unlock(&BTRFS_I(inode
)->lock
);
1686 /* For sanity tests */
1687 if (btrfs_is_testing(fs_info
))
1690 __percpu_counter_add(&fs_info
->delalloc_bytes
, len
,
1691 fs_info
->delalloc_batch
);
1692 spin_lock(&BTRFS_I(inode
)->lock
);
1693 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1694 if (*bits
& EXTENT_DEFRAG
)
1695 BTRFS_I(inode
)->defrag_bytes
+= len
;
1696 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1697 &BTRFS_I(inode
)->runtime_flags
))
1698 btrfs_add_delalloc_inodes(root
, inode
);
1699 spin_unlock(&BTRFS_I(inode
)->lock
);
1704 * extent_io.c clear_bit_hook, see set_bit_hook for why
1706 static void btrfs_clear_bit_hook(struct btrfs_inode
*inode
,
1707 struct extent_state
*state
,
1710 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
1711 u64 len
= state
->end
+ 1 - state
->start
;
1712 u32 num_extents
= count_max_extents(len
);
1714 spin_lock(&inode
->lock
);
1715 if ((state
->state
& EXTENT_DEFRAG
) && (*bits
& EXTENT_DEFRAG
))
1716 inode
->defrag_bytes
-= len
;
1717 spin_unlock(&inode
->lock
);
1720 * set_bit and clear bit hooks normally require _irqsave/restore
1721 * but in this case, we are only testing for the DELALLOC
1722 * bit, which is only set or cleared with irqs on
1724 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1725 struct btrfs_root
*root
= inode
->root
;
1726 bool do_list
= !btrfs_is_free_space_inode(inode
);
1728 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1729 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1730 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1731 spin_lock(&inode
->lock
);
1732 inode
->outstanding_extents
-= num_extents
;
1733 spin_unlock(&inode
->lock
);
1737 * We don't reserve metadata space for space cache inodes so we
1738 * don't need to call dellalloc_release_metadata if there is an
1741 if (*bits
& EXTENT_DO_ACCOUNTING
&&
1742 root
!= fs_info
->tree_root
)
1743 btrfs_delalloc_release_metadata(inode
, len
);
1745 /* For sanity tests. */
1746 if (btrfs_is_testing(fs_info
))
1749 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1750 && do_list
&& !(state
->state
& EXTENT_NORESERVE
)
1751 && (*bits
& (EXTENT_DO_ACCOUNTING
|
1752 EXTENT_CLEAR_DATA_RESV
)))
1753 btrfs_free_reserved_data_space_noquota(
1757 __percpu_counter_add(&fs_info
->delalloc_bytes
, -len
,
1758 fs_info
->delalloc_batch
);
1759 spin_lock(&inode
->lock
);
1760 inode
->delalloc_bytes
-= len
;
1761 if (do_list
&& inode
->delalloc_bytes
== 0 &&
1762 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1763 &inode
->runtime_flags
))
1764 btrfs_del_delalloc_inode(root
, inode
);
1765 spin_unlock(&inode
->lock
);
1770 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1771 * we don't create bios that span stripes or chunks
1773 * return 1 if page cannot be merged to bio
1774 * return 0 if page can be merged to bio
1775 * return error otherwise
1777 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1778 size_t size
, struct bio
*bio
,
1779 unsigned long bio_flags
)
1781 struct inode
*inode
= page
->mapping
->host
;
1782 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1783 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
1788 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1791 length
= bio
->bi_iter
.bi_size
;
1792 map_length
= length
;
1793 ret
= btrfs_map_block(fs_info
, btrfs_op(bio
), logical
, &map_length
,
1797 if (map_length
< length
+ size
)
1803 * in order to insert checksums into the metadata in large chunks,
1804 * we wait until bio submission time. All the pages in the bio are
1805 * checksummed and sums are attached onto the ordered extent record.
1807 * At IO completion time the cums attached on the ordered extent record
1808 * are inserted into the btree
1810 static int __btrfs_submit_bio_start(struct inode
*inode
, struct bio
*bio
,
1811 int mirror_num
, unsigned long bio_flags
,
1816 ret
= btrfs_csum_one_bio(inode
, bio
, 0, 0);
1817 BUG_ON(ret
); /* -ENOMEM */
1822 * in order to insert checksums into the metadata in large chunks,
1823 * we wait until bio submission time. All the pages in the bio are
1824 * checksummed and sums are attached onto the ordered extent record.
1826 * At IO completion time the cums attached on the ordered extent record
1827 * are inserted into the btree
1829 static int __btrfs_submit_bio_done(struct inode
*inode
, struct bio
*bio
,
1830 int mirror_num
, unsigned long bio_flags
,
1833 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1836 ret
= btrfs_map_bio(fs_info
, bio
, mirror_num
, 1);
1838 bio
->bi_error
= ret
;
1845 * extent_io.c submission hook. This does the right thing for csum calculation
1846 * on write, or reading the csums from the tree before a read
1848 static int btrfs_submit_bio_hook(struct inode
*inode
, struct bio
*bio
,
1849 int mirror_num
, unsigned long bio_flags
,
1852 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1853 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1854 enum btrfs_wq_endio_type metadata
= BTRFS_WQ_ENDIO_DATA
;
1857 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1859 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1861 if (btrfs_is_free_space_inode(BTRFS_I(inode
)))
1862 metadata
= BTRFS_WQ_ENDIO_FREE_SPACE
;
1864 if (bio_op(bio
) != REQ_OP_WRITE
) {
1865 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, metadata
);
1869 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1870 ret
= btrfs_submit_compressed_read(inode
, bio
,
1874 } else if (!skip_sum
) {
1875 ret
= btrfs_lookup_bio_sums(inode
, bio
, NULL
);
1880 } else if (async
&& !skip_sum
) {
1881 /* csum items have already been cloned */
1882 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1884 /* we're doing a write, do the async checksumming */
1885 ret
= btrfs_wq_submit_bio(fs_info
, inode
, bio
, mirror_num
,
1886 bio_flags
, bio_offset
,
1887 __btrfs_submit_bio_start
,
1888 __btrfs_submit_bio_done
);
1890 } else if (!skip_sum
) {
1891 ret
= btrfs_csum_one_bio(inode
, bio
, 0, 0);
1897 ret
= btrfs_map_bio(fs_info
, bio
, mirror_num
, 0);
1901 bio
->bi_error
= ret
;
1908 * given a list of ordered sums record them in the inode. This happens
1909 * at IO completion time based on sums calculated at bio submission time.
1911 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1912 struct inode
*inode
, struct list_head
*list
)
1914 struct btrfs_ordered_sum
*sum
;
1916 list_for_each_entry(sum
, list
, list
) {
1917 trans
->adding_csums
= 1;
1918 btrfs_csum_file_blocks(trans
,
1919 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1920 trans
->adding_csums
= 0;
1925 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1926 struct extent_state
**cached_state
, int dedupe
)
1928 WARN_ON((end
& (PAGE_SIZE
- 1)) == 0);
1929 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1933 /* see btrfs_writepage_start_hook for details on why this is required */
1934 struct btrfs_writepage_fixup
{
1936 struct btrfs_work work
;
1939 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1941 struct btrfs_writepage_fixup
*fixup
;
1942 struct btrfs_ordered_extent
*ordered
;
1943 struct extent_state
*cached_state
= NULL
;
1945 struct inode
*inode
;
1950 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1954 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1955 ClearPageChecked(page
);
1959 inode
= page
->mapping
->host
;
1960 page_start
= page_offset(page
);
1961 page_end
= page_offset(page
) + PAGE_SIZE
- 1;
1963 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1966 /* already ordered? We're done */
1967 if (PagePrivate2(page
))
1970 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), page_start
,
1973 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1974 page_end
, &cached_state
, GFP_NOFS
);
1976 btrfs_start_ordered_extent(inode
, ordered
, 1);
1977 btrfs_put_ordered_extent(ordered
);
1981 ret
= btrfs_delalloc_reserve_space(inode
, page_start
,
1984 mapping_set_error(page
->mapping
, ret
);
1985 end_extent_writepage(page
, ret
, page_start
, page_end
);
1986 ClearPageChecked(page
);
1990 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
,
1992 ClearPageChecked(page
);
1993 set_page_dirty(page
);
1995 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1996 &cached_state
, GFP_NOFS
);
2004 * There are a few paths in the higher layers of the kernel that directly
2005 * set the page dirty bit without asking the filesystem if it is a
2006 * good idea. This causes problems because we want to make sure COW
2007 * properly happens and the data=ordered rules are followed.
2009 * In our case any range that doesn't have the ORDERED bit set
2010 * hasn't been properly setup for IO. We kick off an async process
2011 * to fix it up. The async helper will wait for ordered extents, set
2012 * the delalloc bit and make it safe to write the page.
2014 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
2016 struct inode
*inode
= page
->mapping
->host
;
2017 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2018 struct btrfs_writepage_fixup
*fixup
;
2020 /* this page is properly in the ordered list */
2021 if (TestClearPagePrivate2(page
))
2024 if (PageChecked(page
))
2027 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
2031 SetPageChecked(page
);
2033 btrfs_init_work(&fixup
->work
, btrfs_fixup_helper
,
2034 btrfs_writepage_fixup_worker
, NULL
, NULL
);
2036 btrfs_queue_work(fs_info
->fixup_workers
, &fixup
->work
);
2040 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
2041 struct inode
*inode
, u64 file_pos
,
2042 u64 disk_bytenr
, u64 disk_num_bytes
,
2043 u64 num_bytes
, u64 ram_bytes
,
2044 u8 compression
, u8 encryption
,
2045 u16 other_encoding
, int extent_type
)
2047 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2048 struct btrfs_file_extent_item
*fi
;
2049 struct btrfs_path
*path
;
2050 struct extent_buffer
*leaf
;
2051 struct btrfs_key ins
;
2052 int extent_inserted
= 0;
2055 path
= btrfs_alloc_path();
2060 * we may be replacing one extent in the tree with another.
2061 * The new extent is pinned in the extent map, and we don't want
2062 * to drop it from the cache until it is completely in the btree.
2064 * So, tell btrfs_drop_extents to leave this extent in the cache.
2065 * the caller is expected to unpin it and allow it to be merged
2068 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
, file_pos
,
2069 file_pos
+ num_bytes
, NULL
, 0,
2070 1, sizeof(*fi
), &extent_inserted
);
2074 if (!extent_inserted
) {
2075 ins
.objectid
= btrfs_ino(BTRFS_I(inode
));
2076 ins
.offset
= file_pos
;
2077 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
2079 path
->leave_spinning
= 1;
2080 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
2085 leaf
= path
->nodes
[0];
2086 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2087 struct btrfs_file_extent_item
);
2088 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
2089 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
2090 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
2091 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
2092 btrfs_set_file_extent_offset(leaf
, fi
, 0);
2093 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
2094 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
2095 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
2096 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
2097 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
2099 btrfs_mark_buffer_dirty(leaf
);
2100 btrfs_release_path(path
);
2102 inode_add_bytes(inode
, num_bytes
);
2104 ins
.objectid
= disk_bytenr
;
2105 ins
.offset
= disk_num_bytes
;
2106 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2107 ret
= btrfs_alloc_reserved_file_extent(trans
, root
->root_key
.objectid
,
2108 btrfs_ino(BTRFS_I(inode
)), file_pos
, ram_bytes
, &ins
);
2110 * Release the reserved range from inode dirty range map, as it is
2111 * already moved into delayed_ref_head
2113 btrfs_qgroup_release_data(inode
, file_pos
, ram_bytes
);
2115 btrfs_free_path(path
);
2120 /* snapshot-aware defrag */
2121 struct sa_defrag_extent_backref
{
2122 struct rb_node node
;
2123 struct old_sa_defrag_extent
*old
;
2132 struct old_sa_defrag_extent
{
2133 struct list_head list
;
2134 struct new_sa_defrag_extent
*new;
2143 struct new_sa_defrag_extent
{
2144 struct rb_root root
;
2145 struct list_head head
;
2146 struct btrfs_path
*path
;
2147 struct inode
*inode
;
2155 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
2156 struct sa_defrag_extent_backref
*b2
)
2158 if (b1
->root_id
< b2
->root_id
)
2160 else if (b1
->root_id
> b2
->root_id
)
2163 if (b1
->inum
< b2
->inum
)
2165 else if (b1
->inum
> b2
->inum
)
2168 if (b1
->file_pos
< b2
->file_pos
)
2170 else if (b1
->file_pos
> b2
->file_pos
)
2174 * [------------------------------] ===> (a range of space)
2175 * |<--->| |<---->| =============> (fs/file tree A)
2176 * |<---------------------------->| ===> (fs/file tree B)
2178 * A range of space can refer to two file extents in one tree while
2179 * refer to only one file extent in another tree.
2181 * So we may process a disk offset more than one time(two extents in A)
2182 * and locate at the same extent(one extent in B), then insert two same
2183 * backrefs(both refer to the extent in B).
2188 static void backref_insert(struct rb_root
*root
,
2189 struct sa_defrag_extent_backref
*backref
)
2191 struct rb_node
**p
= &root
->rb_node
;
2192 struct rb_node
*parent
= NULL
;
2193 struct sa_defrag_extent_backref
*entry
;
2198 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2200 ret
= backref_comp(backref
, entry
);
2204 p
= &(*p
)->rb_right
;
2207 rb_link_node(&backref
->node
, parent
, p
);
2208 rb_insert_color(&backref
->node
, root
);
2212 * Note the backref might has changed, and in this case we just return 0.
2214 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2217 struct btrfs_file_extent_item
*extent
;
2218 struct old_sa_defrag_extent
*old
= ctx
;
2219 struct new_sa_defrag_extent
*new = old
->new;
2220 struct btrfs_path
*path
= new->path
;
2221 struct btrfs_key key
;
2222 struct btrfs_root
*root
;
2223 struct sa_defrag_extent_backref
*backref
;
2224 struct extent_buffer
*leaf
;
2225 struct inode
*inode
= new->inode
;
2226 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2232 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2233 inum
== btrfs_ino(BTRFS_I(inode
)))
2236 key
.objectid
= root_id
;
2237 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2238 key
.offset
= (u64
)-1;
2240 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2242 if (PTR_ERR(root
) == -ENOENT
)
2245 btrfs_debug(fs_info
, "inum=%llu, offset=%llu, root_id=%llu",
2246 inum
, offset
, root_id
);
2247 return PTR_ERR(root
);
2250 key
.objectid
= inum
;
2251 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2252 if (offset
> (u64
)-1 << 32)
2255 key
.offset
= offset
;
2257 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2258 if (WARN_ON(ret
< 0))
2265 leaf
= path
->nodes
[0];
2266 slot
= path
->slots
[0];
2268 if (slot
>= btrfs_header_nritems(leaf
)) {
2269 ret
= btrfs_next_leaf(root
, path
);
2272 } else if (ret
> 0) {
2281 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2283 if (key
.objectid
> inum
)
2286 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2289 extent
= btrfs_item_ptr(leaf
, slot
,
2290 struct btrfs_file_extent_item
);
2292 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2296 * 'offset' refers to the exact key.offset,
2297 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2298 * (key.offset - extent_offset).
2300 if (key
.offset
!= offset
)
2303 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2304 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2306 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2307 old
->len
|| extent_offset
+ num_bytes
<=
2308 old
->extent_offset
+ old
->offset
)
2313 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2319 backref
->root_id
= root_id
;
2320 backref
->inum
= inum
;
2321 backref
->file_pos
= offset
;
2322 backref
->num_bytes
= num_bytes
;
2323 backref
->extent_offset
= extent_offset
;
2324 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2326 backref_insert(&new->root
, backref
);
2329 btrfs_release_path(path
);
2334 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2335 struct new_sa_defrag_extent
*new)
2337 struct btrfs_fs_info
*fs_info
= btrfs_sb(new->inode
->i_sb
);
2338 struct old_sa_defrag_extent
*old
, *tmp
;
2343 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2344 ret
= iterate_inodes_from_logical(old
->bytenr
+
2345 old
->extent_offset
, fs_info
,
2346 path
, record_one_backref
,
2348 if (ret
< 0 && ret
!= -ENOENT
)
2351 /* no backref to be processed for this extent */
2353 list_del(&old
->list
);
2358 if (list_empty(&new->head
))
2364 static int relink_is_mergable(struct extent_buffer
*leaf
,
2365 struct btrfs_file_extent_item
*fi
,
2366 struct new_sa_defrag_extent
*new)
2368 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != new->bytenr
)
2371 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2374 if (btrfs_file_extent_compression(leaf
, fi
) != new->compress_type
)
2377 if (btrfs_file_extent_encryption(leaf
, fi
) ||
2378 btrfs_file_extent_other_encoding(leaf
, fi
))
2385 * Note the backref might has changed, and in this case we just return 0.
2387 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2388 struct sa_defrag_extent_backref
*prev
,
2389 struct sa_defrag_extent_backref
*backref
)
2391 struct btrfs_file_extent_item
*extent
;
2392 struct btrfs_file_extent_item
*item
;
2393 struct btrfs_ordered_extent
*ordered
;
2394 struct btrfs_trans_handle
*trans
;
2395 struct btrfs_root
*root
;
2396 struct btrfs_key key
;
2397 struct extent_buffer
*leaf
;
2398 struct old_sa_defrag_extent
*old
= backref
->old
;
2399 struct new_sa_defrag_extent
*new = old
->new;
2400 struct btrfs_fs_info
*fs_info
= btrfs_sb(new->inode
->i_sb
);
2401 struct inode
*inode
;
2402 struct extent_state
*cached
= NULL
;
2411 if (prev
&& prev
->root_id
== backref
->root_id
&&
2412 prev
->inum
== backref
->inum
&&
2413 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2416 /* step 1: get root */
2417 key
.objectid
= backref
->root_id
;
2418 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2419 key
.offset
= (u64
)-1;
2421 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2423 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2425 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2426 if (PTR_ERR(root
) == -ENOENT
)
2428 return PTR_ERR(root
);
2431 if (btrfs_root_readonly(root
)) {
2432 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2436 /* step 2: get inode */
2437 key
.objectid
= backref
->inum
;
2438 key
.type
= BTRFS_INODE_ITEM_KEY
;
2441 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2442 if (IS_ERR(inode
)) {
2443 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2447 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2449 /* step 3: relink backref */
2450 lock_start
= backref
->file_pos
;
2451 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2452 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2455 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2457 btrfs_put_ordered_extent(ordered
);
2461 trans
= btrfs_join_transaction(root
);
2462 if (IS_ERR(trans
)) {
2463 ret
= PTR_ERR(trans
);
2467 key
.objectid
= backref
->inum
;
2468 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2469 key
.offset
= backref
->file_pos
;
2471 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2474 } else if (ret
> 0) {
2479 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2480 struct btrfs_file_extent_item
);
2482 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2483 backref
->generation
)
2486 btrfs_release_path(path
);
2488 start
= backref
->file_pos
;
2489 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2490 start
+= old
->extent_offset
+ old
->offset
-
2491 backref
->extent_offset
;
2493 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2494 old
->extent_offset
+ old
->offset
+ old
->len
);
2495 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2497 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2502 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
2503 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2506 path
->leave_spinning
= 1;
2508 struct btrfs_file_extent_item
*fi
;
2510 struct btrfs_key found_key
;
2512 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2517 leaf
= path
->nodes
[0];
2518 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2520 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2521 struct btrfs_file_extent_item
);
2522 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2524 if (extent_len
+ found_key
.offset
== start
&&
2525 relink_is_mergable(leaf
, fi
, new)) {
2526 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2528 btrfs_mark_buffer_dirty(leaf
);
2529 inode_add_bytes(inode
, len
);
2535 btrfs_release_path(path
);
2540 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2543 btrfs_abort_transaction(trans
, ret
);
2547 leaf
= path
->nodes
[0];
2548 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2549 struct btrfs_file_extent_item
);
2550 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2551 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2552 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2553 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2554 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2555 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2556 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2557 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2558 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2559 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2561 btrfs_mark_buffer_dirty(leaf
);
2562 inode_add_bytes(inode
, len
);
2563 btrfs_release_path(path
);
2565 ret
= btrfs_inc_extent_ref(trans
, fs_info
, new->bytenr
,
2567 backref
->root_id
, backref
->inum
,
2568 new->file_pos
); /* start - extent_offset */
2570 btrfs_abort_transaction(trans
, ret
);
2576 btrfs_release_path(path
);
2577 path
->leave_spinning
= 0;
2578 btrfs_end_transaction(trans
);
2580 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2586 static void free_sa_defrag_extent(struct new_sa_defrag_extent
*new)
2588 struct old_sa_defrag_extent
*old
, *tmp
;
2593 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2599 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2601 struct btrfs_fs_info
*fs_info
= btrfs_sb(new->inode
->i_sb
);
2602 struct btrfs_path
*path
;
2603 struct sa_defrag_extent_backref
*backref
;
2604 struct sa_defrag_extent_backref
*prev
= NULL
;
2605 struct inode
*inode
;
2606 struct btrfs_root
*root
;
2607 struct rb_node
*node
;
2611 root
= BTRFS_I(inode
)->root
;
2613 path
= btrfs_alloc_path();
2617 if (!record_extent_backrefs(path
, new)) {
2618 btrfs_free_path(path
);
2621 btrfs_release_path(path
);
2624 node
= rb_first(&new->root
);
2627 rb_erase(node
, &new->root
);
2629 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2631 ret
= relink_extent_backref(path
, prev
, backref
);
2644 btrfs_free_path(path
);
2646 free_sa_defrag_extent(new);
2648 atomic_dec(&fs_info
->defrag_running
);
2649 wake_up(&fs_info
->transaction_wait
);
2652 static struct new_sa_defrag_extent
*
2653 record_old_file_extents(struct inode
*inode
,
2654 struct btrfs_ordered_extent
*ordered
)
2656 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2657 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2658 struct btrfs_path
*path
;
2659 struct btrfs_key key
;
2660 struct old_sa_defrag_extent
*old
;
2661 struct new_sa_defrag_extent
*new;
2664 new = kmalloc(sizeof(*new), GFP_NOFS
);
2669 new->file_pos
= ordered
->file_offset
;
2670 new->len
= ordered
->len
;
2671 new->bytenr
= ordered
->start
;
2672 new->disk_len
= ordered
->disk_len
;
2673 new->compress_type
= ordered
->compress_type
;
2674 new->root
= RB_ROOT
;
2675 INIT_LIST_HEAD(&new->head
);
2677 path
= btrfs_alloc_path();
2681 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
2682 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2683 key
.offset
= new->file_pos
;
2685 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2688 if (ret
> 0 && path
->slots
[0] > 0)
2691 /* find out all the old extents for the file range */
2693 struct btrfs_file_extent_item
*extent
;
2694 struct extent_buffer
*l
;
2703 slot
= path
->slots
[0];
2705 if (slot
>= btrfs_header_nritems(l
)) {
2706 ret
= btrfs_next_leaf(root
, path
);
2714 btrfs_item_key_to_cpu(l
, &key
, slot
);
2716 if (key
.objectid
!= btrfs_ino(BTRFS_I(inode
)))
2718 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2720 if (key
.offset
>= new->file_pos
+ new->len
)
2723 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2725 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2726 if (key
.offset
+ num_bytes
< new->file_pos
)
2729 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2733 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2735 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2739 offset
= max(new->file_pos
, key
.offset
);
2740 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2742 old
->bytenr
= disk_bytenr
;
2743 old
->extent_offset
= extent_offset
;
2744 old
->offset
= offset
- key
.offset
;
2745 old
->len
= end
- offset
;
2748 list_add_tail(&old
->list
, &new->head
);
2754 btrfs_free_path(path
);
2755 atomic_inc(&fs_info
->defrag_running
);
2760 btrfs_free_path(path
);
2762 free_sa_defrag_extent(new);
2766 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info
*fs_info
,
2769 struct btrfs_block_group_cache
*cache
;
2771 cache
= btrfs_lookup_block_group(fs_info
, start
);
2774 spin_lock(&cache
->lock
);
2775 cache
->delalloc_bytes
-= len
;
2776 spin_unlock(&cache
->lock
);
2778 btrfs_put_block_group(cache
);
2781 /* as ordered data IO finishes, this gets called so we can finish
2782 * an ordered extent if the range of bytes in the file it covers are
2785 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2787 struct inode
*inode
= ordered_extent
->inode
;
2788 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2789 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2790 struct btrfs_trans_handle
*trans
= NULL
;
2791 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2792 struct extent_state
*cached_state
= NULL
;
2793 struct new_sa_defrag_extent
*new = NULL
;
2794 int compress_type
= 0;
2796 u64 logical_len
= ordered_extent
->len
;
2798 bool truncated
= false;
2800 nolock
= btrfs_is_free_space_inode(BTRFS_I(inode
));
2802 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2807 btrfs_free_io_failure_record(BTRFS_I(inode
),
2808 ordered_extent
->file_offset
,
2809 ordered_extent
->file_offset
+
2810 ordered_extent
->len
- 1);
2812 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2814 logical_len
= ordered_extent
->truncated_len
;
2815 /* Truncated the entire extent, don't bother adding */
2820 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2821 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2824 * For mwrite(mmap + memset to write) case, we still reserve
2825 * space for NOCOW range.
2826 * As NOCOW won't cause a new delayed ref, just free the space
2828 btrfs_qgroup_free_data(inode
, ordered_extent
->file_offset
,
2829 ordered_extent
->len
);
2830 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2832 trans
= btrfs_join_transaction_nolock(root
);
2834 trans
= btrfs_join_transaction(root
);
2835 if (IS_ERR(trans
)) {
2836 ret
= PTR_ERR(trans
);
2840 trans
->block_rsv
= &fs_info
->delalloc_block_rsv
;
2841 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2842 if (ret
) /* -ENOMEM or corruption */
2843 btrfs_abort_transaction(trans
, ret
);
2847 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2848 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2851 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2852 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2853 EXTENT_DEFRAG
, 1, cached_state
);
2855 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2856 if (0 && last_snapshot
>= BTRFS_I(inode
)->generation
)
2857 /* the inode is shared */
2858 new = record_old_file_extents(inode
, ordered_extent
);
2860 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2861 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2862 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2866 trans
= btrfs_join_transaction_nolock(root
);
2868 trans
= btrfs_join_transaction(root
);
2869 if (IS_ERR(trans
)) {
2870 ret
= PTR_ERR(trans
);
2875 trans
->block_rsv
= &fs_info
->delalloc_block_rsv
;
2877 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2878 compress_type
= ordered_extent
->compress_type
;
2879 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2880 BUG_ON(compress_type
);
2881 ret
= btrfs_mark_extent_written(trans
, BTRFS_I(inode
),
2882 ordered_extent
->file_offset
,
2883 ordered_extent
->file_offset
+
2886 BUG_ON(root
== fs_info
->tree_root
);
2887 ret
= insert_reserved_file_extent(trans
, inode
,
2888 ordered_extent
->file_offset
,
2889 ordered_extent
->start
,
2890 ordered_extent
->disk_len
,
2891 logical_len
, logical_len
,
2892 compress_type
, 0, 0,
2893 BTRFS_FILE_EXTENT_REG
);
2895 btrfs_release_delalloc_bytes(fs_info
,
2896 ordered_extent
->start
,
2897 ordered_extent
->disk_len
);
2899 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2900 ordered_extent
->file_offset
, ordered_extent
->len
,
2903 btrfs_abort_transaction(trans
, ret
);
2907 add_pending_csums(trans
, inode
, &ordered_extent
->list
);
2909 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2910 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2911 if (ret
) { /* -ENOMEM or corruption */
2912 btrfs_abort_transaction(trans
, ret
);
2917 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2918 ordered_extent
->file_offset
+
2919 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2921 if (root
!= fs_info
->tree_root
)
2922 btrfs_delalloc_release_metadata(BTRFS_I(inode
),
2923 ordered_extent
->len
);
2925 btrfs_end_transaction(trans
);
2927 if (ret
|| truncated
) {
2931 start
= ordered_extent
->file_offset
+ logical_len
;
2933 start
= ordered_extent
->file_offset
;
2934 end
= ordered_extent
->file_offset
+ ordered_extent
->len
- 1;
2935 clear_extent_uptodate(io_tree
, start
, end
, NULL
, GFP_NOFS
);
2937 /* Drop the cache for the part of the extent we didn't write. */
2938 btrfs_drop_extent_cache(BTRFS_I(inode
), start
, end
, 0);
2941 * If the ordered extent had an IOERR or something else went
2942 * wrong we need to return the space for this ordered extent
2943 * back to the allocator. We only free the extent in the
2944 * truncated case if we didn't write out the extent at all.
2946 if ((ret
|| !logical_len
) &&
2947 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2948 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2949 btrfs_free_reserved_extent(fs_info
,
2950 ordered_extent
->start
,
2951 ordered_extent
->disk_len
, 1);
2956 * This needs to be done to make sure anybody waiting knows we are done
2957 * updating everything for this ordered extent.
2959 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2961 /* for snapshot-aware defrag */
2964 free_sa_defrag_extent(new);
2965 atomic_dec(&fs_info
->defrag_running
);
2967 relink_file_extents(new);
2972 btrfs_put_ordered_extent(ordered_extent
);
2973 /* once for the tree */
2974 btrfs_put_ordered_extent(ordered_extent
);
2979 static void finish_ordered_fn(struct btrfs_work
*work
)
2981 struct btrfs_ordered_extent
*ordered_extent
;
2982 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2983 btrfs_finish_ordered_io(ordered_extent
);
2986 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2987 struct extent_state
*state
, int uptodate
)
2989 struct inode
*inode
= page
->mapping
->host
;
2990 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2991 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2992 struct btrfs_workqueue
*wq
;
2993 btrfs_work_func_t func
;
2995 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2997 ClearPagePrivate2(page
);
2998 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2999 end
- start
+ 1, uptodate
))
3002 if (btrfs_is_free_space_inode(BTRFS_I(inode
))) {
3003 wq
= fs_info
->endio_freespace_worker
;
3004 func
= btrfs_freespace_write_helper
;
3006 wq
= fs_info
->endio_write_workers
;
3007 func
= btrfs_endio_write_helper
;
3010 btrfs_init_work(&ordered_extent
->work
, func
, finish_ordered_fn
, NULL
,
3012 btrfs_queue_work(wq
, &ordered_extent
->work
);
3017 static int __readpage_endio_check(struct inode
*inode
,
3018 struct btrfs_io_bio
*io_bio
,
3019 int icsum
, struct page
*page
,
3020 int pgoff
, u64 start
, size_t len
)
3026 csum_expected
= *(((u32
*)io_bio
->csum
) + icsum
);
3028 kaddr
= kmap_atomic(page
);
3029 csum
= btrfs_csum_data(kaddr
+ pgoff
, csum
, len
);
3030 btrfs_csum_final(csum
, (u8
*)&csum
);
3031 if (csum
!= csum_expected
)
3034 kunmap_atomic(kaddr
);
3037 btrfs_print_data_csum_error(BTRFS_I(inode
), start
, csum
, csum_expected
,
3038 io_bio
->mirror_num
);
3039 memset(kaddr
+ pgoff
, 1, len
);
3040 flush_dcache_page(page
);
3041 kunmap_atomic(kaddr
);
3042 if (csum_expected
== 0)
3048 * when reads are done, we need to check csums to verify the data is correct
3049 * if there's a match, we allow the bio to finish. If not, the code in
3050 * extent_io.c will try to find good copies for us.
3052 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
3053 u64 phy_offset
, struct page
*page
,
3054 u64 start
, u64 end
, int mirror
)
3056 size_t offset
= start
- page_offset(page
);
3057 struct inode
*inode
= page
->mapping
->host
;
3058 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3059 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3061 if (PageChecked(page
)) {
3062 ClearPageChecked(page
);
3066 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
3069 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
3070 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
3071 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
);
3075 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
3076 return __readpage_endio_check(inode
, io_bio
, phy_offset
, page
, offset
,
3077 start
, (size_t)(end
- start
+ 1));
3080 void btrfs_add_delayed_iput(struct inode
*inode
)
3082 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3083 struct btrfs_inode
*binode
= BTRFS_I(inode
);
3085 if (atomic_add_unless(&inode
->i_count
, -1, 1))
3088 spin_lock(&fs_info
->delayed_iput_lock
);
3089 if (binode
->delayed_iput_count
== 0) {
3090 ASSERT(list_empty(&binode
->delayed_iput
));
3091 list_add_tail(&binode
->delayed_iput
, &fs_info
->delayed_iputs
);
3093 binode
->delayed_iput_count
++;
3095 spin_unlock(&fs_info
->delayed_iput_lock
);
3098 void btrfs_run_delayed_iputs(struct btrfs_fs_info
*fs_info
)
3101 spin_lock(&fs_info
->delayed_iput_lock
);
3102 while (!list_empty(&fs_info
->delayed_iputs
)) {
3103 struct btrfs_inode
*inode
;
3105 inode
= list_first_entry(&fs_info
->delayed_iputs
,
3106 struct btrfs_inode
, delayed_iput
);
3107 if (inode
->delayed_iput_count
) {
3108 inode
->delayed_iput_count
--;
3109 list_move_tail(&inode
->delayed_iput
,
3110 &fs_info
->delayed_iputs
);
3112 list_del_init(&inode
->delayed_iput
);
3114 spin_unlock(&fs_info
->delayed_iput_lock
);
3115 iput(&inode
->vfs_inode
);
3116 spin_lock(&fs_info
->delayed_iput_lock
);
3118 spin_unlock(&fs_info
->delayed_iput_lock
);
3122 * This is called in transaction commit time. If there are no orphan
3123 * files in the subvolume, it removes orphan item and frees block_rsv
3126 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
3127 struct btrfs_root
*root
)
3129 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3130 struct btrfs_block_rsv
*block_rsv
;
3133 if (atomic_read(&root
->orphan_inodes
) ||
3134 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
3137 spin_lock(&root
->orphan_lock
);
3138 if (atomic_read(&root
->orphan_inodes
)) {
3139 spin_unlock(&root
->orphan_lock
);
3143 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
3144 spin_unlock(&root
->orphan_lock
);
3148 block_rsv
= root
->orphan_block_rsv
;
3149 root
->orphan_block_rsv
= NULL
;
3150 spin_unlock(&root
->orphan_lock
);
3152 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
) &&
3153 btrfs_root_refs(&root
->root_item
) > 0) {
3154 ret
= btrfs_del_orphan_item(trans
, fs_info
->tree_root
,
3155 root
->root_key
.objectid
);
3157 btrfs_abort_transaction(trans
, ret
);
3159 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
,
3164 WARN_ON(block_rsv
->size
> 0);
3165 btrfs_free_block_rsv(fs_info
, block_rsv
);
3170 * This creates an orphan entry for the given inode in case something goes
3171 * wrong in the middle of an unlink/truncate.
3173 * NOTE: caller of this function should reserve 5 units of metadata for
3176 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
,
3177 struct btrfs_inode
*inode
)
3179 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
3180 struct btrfs_root
*root
= inode
->root
;
3181 struct btrfs_block_rsv
*block_rsv
= NULL
;
3186 if (!root
->orphan_block_rsv
) {
3187 block_rsv
= btrfs_alloc_block_rsv(fs_info
,
3188 BTRFS_BLOCK_RSV_TEMP
);
3193 spin_lock(&root
->orphan_lock
);
3194 if (!root
->orphan_block_rsv
) {
3195 root
->orphan_block_rsv
= block_rsv
;
3196 } else if (block_rsv
) {
3197 btrfs_free_block_rsv(fs_info
, block_rsv
);
3201 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3202 &inode
->runtime_flags
)) {
3205 * For proper ENOSPC handling, we should do orphan
3206 * cleanup when mounting. But this introduces backward
3207 * compatibility issue.
3209 if (!xchg(&root
->orphan_item_inserted
, 1))
3215 atomic_inc(&root
->orphan_inodes
);
3218 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3219 &inode
->runtime_flags
))
3221 spin_unlock(&root
->orphan_lock
);
3223 /* grab metadata reservation from transaction handle */
3225 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
3228 atomic_dec(&root
->orphan_inodes
);
3229 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3230 &inode
->runtime_flags
);
3232 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3233 &inode
->runtime_flags
);
3238 /* insert an orphan item to track this unlinked/truncated file */
3240 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
3242 atomic_dec(&root
->orphan_inodes
);
3244 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3245 &inode
->runtime_flags
);
3246 btrfs_orphan_release_metadata(inode
);
3248 if (ret
!= -EEXIST
) {
3249 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3250 &inode
->runtime_flags
);
3251 btrfs_abort_transaction(trans
, ret
);
3258 /* insert an orphan item to track subvolume contains orphan files */
3260 ret
= btrfs_insert_orphan_item(trans
, fs_info
->tree_root
,
3261 root
->root_key
.objectid
);
3262 if (ret
&& ret
!= -EEXIST
) {
3263 btrfs_abort_transaction(trans
, ret
);
3271 * We have done the truncate/delete so we can go ahead and remove the orphan
3272 * item for this particular inode.
3274 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3275 struct btrfs_inode
*inode
)
3277 struct btrfs_root
*root
= inode
->root
;
3278 int delete_item
= 0;
3279 int release_rsv
= 0;
3282 spin_lock(&root
->orphan_lock
);
3283 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3284 &inode
->runtime_flags
))
3287 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3288 &inode
->runtime_flags
))
3290 spin_unlock(&root
->orphan_lock
);
3293 atomic_dec(&root
->orphan_inodes
);
3295 ret
= btrfs_del_orphan_item(trans
, root
,
3300 btrfs_orphan_release_metadata(inode
);
3306 * this cleans up any orphans that may be left on the list from the last use
3309 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3311 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3312 struct btrfs_path
*path
;
3313 struct extent_buffer
*leaf
;
3314 struct btrfs_key key
, found_key
;
3315 struct btrfs_trans_handle
*trans
;
3316 struct inode
*inode
;
3317 u64 last_objectid
= 0;
3318 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3320 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3323 path
= btrfs_alloc_path();
3328 path
->reada
= READA_BACK
;
3330 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3331 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
3332 key
.offset
= (u64
)-1;
3335 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3340 * if ret == 0 means we found what we were searching for, which
3341 * is weird, but possible, so only screw with path if we didn't
3342 * find the key and see if we have stuff that matches
3346 if (path
->slots
[0] == 0)
3351 /* pull out the item */
3352 leaf
= path
->nodes
[0];
3353 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3355 /* make sure the item matches what we want */
3356 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3358 if (found_key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
3361 /* release the path since we're done with it */
3362 btrfs_release_path(path
);
3365 * this is where we are basically btrfs_lookup, without the
3366 * crossing root thing. we store the inode number in the
3367 * offset of the orphan item.
3370 if (found_key
.offset
== last_objectid
) {
3372 "Error removing orphan entry, stopping orphan cleanup");
3377 last_objectid
= found_key
.offset
;
3379 found_key
.objectid
= found_key
.offset
;
3380 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3381 found_key
.offset
= 0;
3382 inode
= btrfs_iget(fs_info
->sb
, &found_key
, root
, NULL
);
3383 ret
= PTR_ERR_OR_ZERO(inode
);
3384 if (ret
&& ret
!= -ENOENT
)
3387 if (ret
== -ENOENT
&& root
== fs_info
->tree_root
) {
3388 struct btrfs_root
*dead_root
;
3389 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3390 int is_dead_root
= 0;
3393 * this is an orphan in the tree root. Currently these
3394 * could come from 2 sources:
3395 * a) a snapshot deletion in progress
3396 * b) a free space cache inode
3397 * We need to distinguish those two, as the snapshot
3398 * orphan must not get deleted.
3399 * find_dead_roots already ran before us, so if this
3400 * is a snapshot deletion, we should find the root
3401 * in the dead_roots list
3403 spin_lock(&fs_info
->trans_lock
);
3404 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3406 if (dead_root
->root_key
.objectid
==
3407 found_key
.objectid
) {
3412 spin_unlock(&fs_info
->trans_lock
);
3414 /* prevent this orphan from being found again */
3415 key
.offset
= found_key
.objectid
- 1;
3420 * Inode is already gone but the orphan item is still there,
3421 * kill the orphan item.
3423 if (ret
== -ENOENT
) {
3424 trans
= btrfs_start_transaction(root
, 1);
3425 if (IS_ERR(trans
)) {
3426 ret
= PTR_ERR(trans
);
3429 btrfs_debug(fs_info
, "auto deleting %Lu",
3430 found_key
.objectid
);
3431 ret
= btrfs_del_orphan_item(trans
, root
,
3432 found_key
.objectid
);
3433 btrfs_end_transaction(trans
);
3440 * add this inode to the orphan list so btrfs_orphan_del does
3441 * the proper thing when we hit it
3443 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3444 &BTRFS_I(inode
)->runtime_flags
);
3445 atomic_inc(&root
->orphan_inodes
);
3447 /* if we have links, this was a truncate, lets do that */
3448 if (inode
->i_nlink
) {
3449 if (WARN_ON(!S_ISREG(inode
->i_mode
))) {
3455 /* 1 for the orphan item deletion. */
3456 trans
= btrfs_start_transaction(root
, 1);
3457 if (IS_ERR(trans
)) {
3459 ret
= PTR_ERR(trans
);
3462 ret
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
3463 btrfs_end_transaction(trans
);
3469 ret
= btrfs_truncate(inode
);
3471 btrfs_orphan_del(NULL
, BTRFS_I(inode
));
3476 /* this will do delete_inode and everything for us */
3481 /* release the path since we're done with it */
3482 btrfs_release_path(path
);
3484 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3486 if (root
->orphan_block_rsv
)
3487 btrfs_block_rsv_release(fs_info
, root
->orphan_block_rsv
,
3490 if (root
->orphan_block_rsv
||
3491 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
)) {
3492 trans
= btrfs_join_transaction(root
);
3494 btrfs_end_transaction(trans
);
3498 btrfs_debug(fs_info
, "unlinked %d orphans", nr_unlink
);
3500 btrfs_debug(fs_info
, "truncated %d orphans", nr_truncate
);
3504 btrfs_err(fs_info
, "could not do orphan cleanup %d", ret
);
3505 btrfs_free_path(path
);
3510 * very simple check to peek ahead in the leaf looking for xattrs. If we
3511 * don't find any xattrs, we know there can't be any acls.
3513 * slot is the slot the inode is in, objectid is the objectid of the inode
3515 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3516 int slot
, u64 objectid
,
3517 int *first_xattr_slot
)
3519 u32 nritems
= btrfs_header_nritems(leaf
);
3520 struct btrfs_key found_key
;
3521 static u64 xattr_access
= 0;
3522 static u64 xattr_default
= 0;
3525 if (!xattr_access
) {
3526 xattr_access
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS
,
3527 strlen(XATTR_NAME_POSIX_ACL_ACCESS
));
3528 xattr_default
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT
,
3529 strlen(XATTR_NAME_POSIX_ACL_DEFAULT
));
3533 *first_xattr_slot
= -1;
3534 while (slot
< nritems
) {
3535 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3537 /* we found a different objectid, there must not be acls */
3538 if (found_key
.objectid
!= objectid
)
3541 /* we found an xattr, assume we've got an acl */
3542 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3543 if (*first_xattr_slot
== -1)
3544 *first_xattr_slot
= slot
;
3545 if (found_key
.offset
== xattr_access
||
3546 found_key
.offset
== xattr_default
)
3551 * we found a key greater than an xattr key, there can't
3552 * be any acls later on
3554 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3561 * it goes inode, inode backrefs, xattrs, extents,
3562 * so if there are a ton of hard links to an inode there can
3563 * be a lot of backrefs. Don't waste time searching too hard,
3564 * this is just an optimization
3569 /* we hit the end of the leaf before we found an xattr or
3570 * something larger than an xattr. We have to assume the inode
3573 if (*first_xattr_slot
== -1)
3574 *first_xattr_slot
= slot
;
3579 * read an inode from the btree into the in-memory inode
3581 static int btrfs_read_locked_inode(struct inode
*inode
)
3583 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3584 struct btrfs_path
*path
;
3585 struct extent_buffer
*leaf
;
3586 struct btrfs_inode_item
*inode_item
;
3587 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3588 struct btrfs_key location
;
3593 bool filled
= false;
3594 int first_xattr_slot
;
3596 ret
= btrfs_fill_inode(inode
, &rdev
);
3600 path
= btrfs_alloc_path();
3606 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3608 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3615 leaf
= path
->nodes
[0];
3620 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3621 struct btrfs_inode_item
);
3622 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3623 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3624 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3625 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3626 btrfs_i_size_write(BTRFS_I(inode
), btrfs_inode_size(leaf
, inode_item
));
3628 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->atime
);
3629 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->atime
);
3631 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->mtime
);
3632 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->mtime
);
3634 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->ctime
);
3635 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->ctime
);
3637 BTRFS_I(inode
)->i_otime
.tv_sec
=
3638 btrfs_timespec_sec(leaf
, &inode_item
->otime
);
3639 BTRFS_I(inode
)->i_otime
.tv_nsec
=
3640 btrfs_timespec_nsec(leaf
, &inode_item
->otime
);
3642 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3643 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3644 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3646 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3647 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3649 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3651 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3652 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3656 * If we were modified in the current generation and evicted from memory
3657 * and then re-read we need to do a full sync since we don't have any
3658 * idea about which extents were modified before we were evicted from
3661 * This is required for both inode re-read from disk and delayed inode
3662 * in delayed_nodes_tree.
3664 if (BTRFS_I(inode
)->last_trans
== fs_info
->generation
)
3665 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3666 &BTRFS_I(inode
)->runtime_flags
);
3669 * We don't persist the id of the transaction where an unlink operation
3670 * against the inode was last made. So here we assume the inode might
3671 * have been evicted, and therefore the exact value of last_unlink_trans
3672 * lost, and set it to last_trans to avoid metadata inconsistencies
3673 * between the inode and its parent if the inode is fsync'ed and the log
3674 * replayed. For example, in the scenario:
3677 * ln mydir/foo mydir/bar
3680 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3681 * xfs_io -c fsync mydir/foo
3683 * mount fs, triggers fsync log replay
3685 * We must make sure that when we fsync our inode foo we also log its
3686 * parent inode, otherwise after log replay the parent still has the
3687 * dentry with the "bar" name but our inode foo has a link count of 1
3688 * and doesn't have an inode ref with the name "bar" anymore.
3690 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3691 * but it guarantees correctness at the expense of occasional full
3692 * transaction commits on fsync if our inode is a directory, or if our
3693 * inode is not a directory, logging its parent unnecessarily.
3695 BTRFS_I(inode
)->last_unlink_trans
= BTRFS_I(inode
)->last_trans
;
3698 if (inode
->i_nlink
!= 1 ||
3699 path
->slots
[0] >= btrfs_header_nritems(leaf
))
3702 btrfs_item_key_to_cpu(leaf
, &location
, path
->slots
[0]);
3703 if (location
.objectid
!= btrfs_ino(BTRFS_I(inode
)))
3706 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3707 if (location
.type
== BTRFS_INODE_REF_KEY
) {
3708 struct btrfs_inode_ref
*ref
;
3710 ref
= (struct btrfs_inode_ref
*)ptr
;
3711 BTRFS_I(inode
)->dir_index
= btrfs_inode_ref_index(leaf
, ref
);
3712 } else if (location
.type
== BTRFS_INODE_EXTREF_KEY
) {
3713 struct btrfs_inode_extref
*extref
;
3715 extref
= (struct btrfs_inode_extref
*)ptr
;
3716 BTRFS_I(inode
)->dir_index
= btrfs_inode_extref_index(leaf
,
3721 * try to precache a NULL acl entry for files that don't have
3722 * any xattrs or acls
3724 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3725 btrfs_ino(BTRFS_I(inode
)), &first_xattr_slot
);
3726 if (first_xattr_slot
!= -1) {
3727 path
->slots
[0] = first_xattr_slot
;
3728 ret
= btrfs_load_inode_props(inode
, path
);
3731 "error loading props for ino %llu (root %llu): %d",
3732 btrfs_ino(BTRFS_I(inode
)),
3733 root
->root_key
.objectid
, ret
);
3735 btrfs_free_path(path
);
3738 cache_no_acl(inode
);
3740 switch (inode
->i_mode
& S_IFMT
) {
3742 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3743 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3744 inode
->i_fop
= &btrfs_file_operations
;
3745 inode
->i_op
= &btrfs_file_inode_operations
;
3748 inode
->i_fop
= &btrfs_dir_file_operations
;
3749 inode
->i_op
= &btrfs_dir_inode_operations
;
3752 inode
->i_op
= &btrfs_symlink_inode_operations
;
3753 inode_nohighmem(inode
);
3754 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3757 inode
->i_op
= &btrfs_special_inode_operations
;
3758 init_special_inode(inode
, inode
->i_mode
, rdev
);
3762 btrfs_update_iflags(inode
);
3766 btrfs_free_path(path
);
3767 make_bad_inode(inode
);
3772 * given a leaf and an inode, copy the inode fields into the leaf
3774 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3775 struct extent_buffer
*leaf
,
3776 struct btrfs_inode_item
*item
,
3777 struct inode
*inode
)
3779 struct btrfs_map_token token
;
3781 btrfs_init_map_token(&token
);
3783 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3784 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3785 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3787 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3788 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3790 btrfs_set_token_timespec_sec(leaf
, &item
->atime
,
3791 inode
->i_atime
.tv_sec
, &token
);
3792 btrfs_set_token_timespec_nsec(leaf
, &item
->atime
,
3793 inode
->i_atime
.tv_nsec
, &token
);
3795 btrfs_set_token_timespec_sec(leaf
, &item
->mtime
,
3796 inode
->i_mtime
.tv_sec
, &token
);
3797 btrfs_set_token_timespec_nsec(leaf
, &item
->mtime
,
3798 inode
->i_mtime
.tv_nsec
, &token
);
3800 btrfs_set_token_timespec_sec(leaf
, &item
->ctime
,
3801 inode
->i_ctime
.tv_sec
, &token
);
3802 btrfs_set_token_timespec_nsec(leaf
, &item
->ctime
,
3803 inode
->i_ctime
.tv_nsec
, &token
);
3805 btrfs_set_token_timespec_sec(leaf
, &item
->otime
,
3806 BTRFS_I(inode
)->i_otime
.tv_sec
, &token
);
3807 btrfs_set_token_timespec_nsec(leaf
, &item
->otime
,
3808 BTRFS_I(inode
)->i_otime
.tv_nsec
, &token
);
3810 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3812 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3814 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3815 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3816 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3817 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3818 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3822 * copy everything in the in-memory inode into the btree.
3824 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3825 struct btrfs_root
*root
, struct inode
*inode
)
3827 struct btrfs_inode_item
*inode_item
;
3828 struct btrfs_path
*path
;
3829 struct extent_buffer
*leaf
;
3832 path
= btrfs_alloc_path();
3836 path
->leave_spinning
= 1;
3837 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3845 leaf
= path
->nodes
[0];
3846 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3847 struct btrfs_inode_item
);
3849 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3850 btrfs_mark_buffer_dirty(leaf
);
3851 btrfs_set_inode_last_trans(trans
, inode
);
3854 btrfs_free_path(path
);
3859 * copy everything in the in-memory inode into the btree.
3861 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3862 struct btrfs_root
*root
, struct inode
*inode
)
3864 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3868 * If the inode is a free space inode, we can deadlock during commit
3869 * if we put it into the delayed code.
3871 * The data relocation inode should also be directly updated
3874 if (!btrfs_is_free_space_inode(BTRFS_I(inode
))
3875 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
3876 && !test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
)) {
3877 btrfs_update_root_times(trans
, root
);
3879 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3881 btrfs_set_inode_last_trans(trans
, inode
);
3885 return btrfs_update_inode_item(trans
, root
, inode
);
3888 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3889 struct btrfs_root
*root
,
3890 struct inode
*inode
)
3894 ret
= btrfs_update_inode(trans
, root
, inode
);
3896 return btrfs_update_inode_item(trans
, root
, inode
);
3901 * unlink helper that gets used here in inode.c and in the tree logging
3902 * recovery code. It remove a link in a directory with a given name, and
3903 * also drops the back refs in the inode to the directory
3905 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3906 struct btrfs_root
*root
,
3907 struct btrfs_inode
*dir
,
3908 struct btrfs_inode
*inode
,
3909 const char *name
, int name_len
)
3911 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3912 struct btrfs_path
*path
;
3914 struct extent_buffer
*leaf
;
3915 struct btrfs_dir_item
*di
;
3916 struct btrfs_key key
;
3918 u64 ino
= btrfs_ino(inode
);
3919 u64 dir_ino
= btrfs_ino(dir
);
3921 path
= btrfs_alloc_path();
3927 path
->leave_spinning
= 1;
3928 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3929 name
, name_len
, -1);
3938 leaf
= path
->nodes
[0];
3939 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3940 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3943 btrfs_release_path(path
);
3946 * If we don't have dir index, we have to get it by looking up
3947 * the inode ref, since we get the inode ref, remove it directly,
3948 * it is unnecessary to do delayed deletion.
3950 * But if we have dir index, needn't search inode ref to get it.
3951 * Since the inode ref is close to the inode item, it is better
3952 * that we delay to delete it, and just do this deletion when
3953 * we update the inode item.
3955 if (inode
->dir_index
) {
3956 ret
= btrfs_delayed_delete_inode_ref(inode
);
3958 index
= inode
->dir_index
;
3963 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3967 "failed to delete reference to %.*s, inode %llu parent %llu",
3968 name_len
, name
, ino
, dir_ino
);
3969 btrfs_abort_transaction(trans
, ret
);
3973 ret
= btrfs_delete_delayed_dir_index(trans
, fs_info
, dir
, index
);
3975 btrfs_abort_transaction(trans
, ret
);
3979 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
, inode
,
3981 if (ret
!= 0 && ret
!= -ENOENT
) {
3982 btrfs_abort_transaction(trans
, ret
);
3986 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
, dir
,
3991 btrfs_abort_transaction(trans
, ret
);
3993 btrfs_free_path(path
);
3997 btrfs_i_size_write(dir
, dir
->vfs_inode
.i_size
- name_len
* 2);
3998 inode_inc_iversion(&inode
->vfs_inode
);
3999 inode_inc_iversion(&dir
->vfs_inode
);
4000 inode
->vfs_inode
.i_ctime
= dir
->vfs_inode
.i_mtime
=
4001 dir
->vfs_inode
.i_ctime
= current_time(&inode
->vfs_inode
);
4002 ret
= btrfs_update_inode(trans
, root
, &dir
->vfs_inode
);
4007 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
4008 struct btrfs_root
*root
,
4009 struct btrfs_inode
*dir
, struct btrfs_inode
*inode
,
4010 const char *name
, int name_len
)
4013 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
4015 drop_nlink(&inode
->vfs_inode
);
4016 ret
= btrfs_update_inode(trans
, root
, &inode
->vfs_inode
);
4022 * helper to start transaction for unlink and rmdir.
4024 * unlink and rmdir are special in btrfs, they do not always free space, so
4025 * if we cannot make our reservations the normal way try and see if there is
4026 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4027 * allow the unlink to occur.
4029 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
4031 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4034 * 1 for the possible orphan item
4035 * 1 for the dir item
4036 * 1 for the dir index
4037 * 1 for the inode ref
4040 return btrfs_start_transaction_fallback_global_rsv(root
, 5, 5);
4043 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
4045 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4046 struct btrfs_trans_handle
*trans
;
4047 struct inode
*inode
= d_inode(dentry
);
4050 trans
= __unlink_start_trans(dir
);
4052 return PTR_ERR(trans
);
4054 btrfs_record_unlink_dir(trans
, BTRFS_I(dir
), BTRFS_I(d_inode(dentry
)),
4057 ret
= btrfs_unlink_inode(trans
, root
, BTRFS_I(dir
),
4058 BTRFS_I(d_inode(dentry
)), dentry
->d_name
.name
,
4059 dentry
->d_name
.len
);
4063 if (inode
->i_nlink
== 0) {
4064 ret
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
4070 btrfs_end_transaction(trans
);
4071 btrfs_btree_balance_dirty(root
->fs_info
);
4075 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
4076 struct btrfs_root
*root
,
4077 struct inode
*dir
, u64 objectid
,
4078 const char *name
, int name_len
)
4080 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4081 struct btrfs_path
*path
;
4082 struct extent_buffer
*leaf
;
4083 struct btrfs_dir_item
*di
;
4084 struct btrfs_key key
;
4087 u64 dir_ino
= btrfs_ino(BTRFS_I(dir
));
4089 path
= btrfs_alloc_path();
4093 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
4094 name
, name_len
, -1);
4095 if (IS_ERR_OR_NULL(di
)) {
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
, fs_info
, objectid
,
4114 root
->root_key
.objectid
, dir_ino
,
4115 &index
, name
, name_len
);
4117 if (ret
!= -ENOENT
) {
4118 btrfs_abort_transaction(trans
, ret
);
4121 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
4123 if (IS_ERR_OR_NULL(di
)) {
4128 btrfs_abort_transaction(trans
, ret
);
4132 leaf
= path
->nodes
[0];
4133 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4134 btrfs_release_path(path
);
4137 btrfs_release_path(path
);
4139 ret
= btrfs_delete_delayed_dir_index(trans
, fs_info
, BTRFS_I(dir
), index
);
4141 btrfs_abort_transaction(trans
, ret
);
4145 btrfs_i_size_write(BTRFS_I(dir
), dir
->i_size
- name_len
* 2);
4146 inode_inc_iversion(dir
);
4147 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
4148 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
4150 btrfs_abort_transaction(trans
, ret
);
4152 btrfs_free_path(path
);
4156 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
4158 struct inode
*inode
= d_inode(dentry
);
4160 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4161 struct btrfs_trans_handle
*trans
;
4162 u64 last_unlink_trans
;
4164 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
4166 if (btrfs_ino(BTRFS_I(inode
)) == BTRFS_FIRST_FREE_OBJECTID
)
4169 trans
= __unlink_start_trans(dir
);
4171 return PTR_ERR(trans
);
4173 if (unlikely(btrfs_ino(BTRFS_I(inode
)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
4174 err
= btrfs_unlink_subvol(trans
, root
, dir
,
4175 BTRFS_I(inode
)->location
.objectid
,
4176 dentry
->d_name
.name
,
4177 dentry
->d_name
.len
);
4181 err
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
4185 last_unlink_trans
= BTRFS_I(inode
)->last_unlink_trans
;
4187 /* now the directory is empty */
4188 err
= btrfs_unlink_inode(trans
, root
, BTRFS_I(dir
),
4189 BTRFS_I(d_inode(dentry
)), dentry
->d_name
.name
,
4190 dentry
->d_name
.len
);
4192 btrfs_i_size_write(BTRFS_I(inode
), 0);
4194 * Propagate the last_unlink_trans value of the deleted dir to
4195 * its parent directory. This is to prevent an unrecoverable
4196 * log tree in the case we do something like this:
4198 * 2) create snapshot under dir foo
4199 * 3) delete the snapshot
4202 * 6) fsync foo or some file inside foo
4204 if (last_unlink_trans
>= trans
->transid
)
4205 BTRFS_I(dir
)->last_unlink_trans
= last_unlink_trans
;
4208 btrfs_end_transaction(trans
);
4209 btrfs_btree_balance_dirty(root
->fs_info
);
4214 static int truncate_space_check(struct btrfs_trans_handle
*trans
,
4215 struct btrfs_root
*root
,
4218 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4222 * This is only used to apply pressure to the enospc system, we don't
4223 * intend to use this reservation at all.
4225 bytes_deleted
= btrfs_csum_bytes_to_leaves(fs_info
, bytes_deleted
);
4226 bytes_deleted
*= fs_info
->nodesize
;
4227 ret
= btrfs_block_rsv_add(root
, &fs_info
->trans_block_rsv
,
4228 bytes_deleted
, BTRFS_RESERVE_NO_FLUSH
);
4230 trace_btrfs_space_reservation(fs_info
, "transaction",
4233 trans
->bytes_reserved
+= bytes_deleted
;
4239 static int truncate_inline_extent(struct inode
*inode
,
4240 struct btrfs_path
*path
,
4241 struct btrfs_key
*found_key
,
4245 struct extent_buffer
*leaf
= path
->nodes
[0];
4246 int slot
= path
->slots
[0];
4247 struct btrfs_file_extent_item
*fi
;
4248 u32 size
= (u32
)(new_size
- found_key
->offset
);
4249 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4251 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
4253 if (btrfs_file_extent_compression(leaf
, fi
) != BTRFS_COMPRESS_NONE
) {
4254 loff_t offset
= new_size
;
4255 loff_t page_end
= ALIGN(offset
, PAGE_SIZE
);
4258 * Zero out the remaining of the last page of our inline extent,
4259 * instead of directly truncating our inline extent here - that
4260 * would be much more complex (decompressing all the data, then
4261 * compressing the truncated data, which might be bigger than
4262 * the size of the inline extent, resize the extent, etc).
4263 * We release the path because to get the page we might need to
4264 * read the extent item from disk (data not in the page cache).
4266 btrfs_release_path(path
);
4267 return btrfs_truncate_block(inode
, offset
, page_end
- offset
,
4271 btrfs_set_file_extent_ram_bytes(leaf
, fi
, size
);
4272 size
= btrfs_file_extent_calc_inline_size(size
);
4273 btrfs_truncate_item(root
->fs_info
, path
, size
, 1);
4275 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4276 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4282 * this can truncate away extent items, csum items and directory items.
4283 * It starts at a high offset and removes keys until it can't find
4284 * any higher than new_size
4286 * csum items that cross the new i_size are truncated to the new size
4289 * min_type is the minimum key type to truncate down to. If set to 0, this
4290 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4292 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4293 struct btrfs_root
*root
,
4294 struct inode
*inode
,
4295 u64 new_size
, u32 min_type
)
4297 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4298 struct btrfs_path
*path
;
4299 struct extent_buffer
*leaf
;
4300 struct btrfs_file_extent_item
*fi
;
4301 struct btrfs_key key
;
4302 struct btrfs_key found_key
;
4303 u64 extent_start
= 0;
4304 u64 extent_num_bytes
= 0;
4305 u64 extent_offset
= 0;
4307 u64 last_size
= new_size
;
4308 u32 found_type
= (u8
)-1;
4311 int pending_del_nr
= 0;
4312 int pending_del_slot
= 0;
4313 int extent_type
= -1;
4316 u64 ino
= btrfs_ino(BTRFS_I(inode
));
4317 u64 bytes_deleted
= 0;
4319 bool should_throttle
= 0;
4320 bool should_end
= 0;
4322 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4325 * for non-free space inodes and ref cows, we want to back off from
4328 if (!btrfs_is_free_space_inode(BTRFS_I(inode
)) &&
4329 test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4332 path
= btrfs_alloc_path();
4335 path
->reada
= READA_BACK
;
4338 * We want to drop from the next block forward in case this new size is
4339 * not block aligned since we will be keeping the last block of the
4340 * extent just the way it is.
4342 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4343 root
== fs_info
->tree_root
)
4344 btrfs_drop_extent_cache(BTRFS_I(inode
), ALIGN(new_size
,
4345 fs_info
->sectorsize
),
4349 * This function is also used to drop the items in the log tree before
4350 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4351 * it is used to drop the loged items. So we shouldn't kill the delayed
4354 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4355 btrfs_kill_delayed_inode_items(BTRFS_I(inode
));
4358 key
.offset
= (u64
)-1;
4363 * with a 16K leaf size and 128MB extents, you can actually queue
4364 * up a huge file in a single leaf. Most of the time that
4365 * bytes_deleted is > 0, it will be huge by the time we get here
4367 if (be_nice
&& bytes_deleted
> SZ_32M
) {
4368 if (btrfs_should_end_transaction(trans
)) {
4375 path
->leave_spinning
= 1;
4376 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4383 /* there are no items in the tree for us to truncate, we're
4386 if (path
->slots
[0] == 0)
4393 leaf
= path
->nodes
[0];
4394 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4395 found_type
= found_key
.type
;
4397 if (found_key
.objectid
!= ino
)
4400 if (found_type
< min_type
)
4403 item_end
= found_key
.offset
;
4404 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4405 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4406 struct btrfs_file_extent_item
);
4407 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4408 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4410 btrfs_file_extent_num_bytes(leaf
, fi
);
4411 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4412 item_end
+= btrfs_file_extent_inline_len(leaf
,
4413 path
->slots
[0], fi
);
4417 if (found_type
> min_type
) {
4420 if (item_end
< new_size
) {
4422 * With NO_HOLES mode, for the following mapping
4424 * [0-4k][hole][8k-12k]
4426 * if truncating isize down to 6k, it ends up
4429 if (btrfs_fs_incompat(root
->fs_info
, NO_HOLES
))
4430 last_size
= new_size
;
4433 if (found_key
.offset
>= new_size
)
4439 /* FIXME, shrink the extent if the ref count is only 1 */
4440 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4444 last_size
= found_key
.offset
;
4446 last_size
= new_size
;
4448 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4450 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4452 u64 orig_num_bytes
=
4453 btrfs_file_extent_num_bytes(leaf
, fi
);
4454 extent_num_bytes
= ALIGN(new_size
-
4456 fs_info
->sectorsize
);
4457 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4459 num_dec
= (orig_num_bytes
-
4461 if (test_bit(BTRFS_ROOT_REF_COWS
,
4464 inode_sub_bytes(inode
, num_dec
);
4465 btrfs_mark_buffer_dirty(leaf
);
4468 btrfs_file_extent_disk_num_bytes(leaf
,
4470 extent_offset
= found_key
.offset
-
4471 btrfs_file_extent_offset(leaf
, fi
);
4473 /* FIXME blocksize != 4096 */
4474 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4475 if (extent_start
!= 0) {
4477 if (test_bit(BTRFS_ROOT_REF_COWS
,
4479 inode_sub_bytes(inode
, num_dec
);
4482 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4484 * we can't truncate inline items that have had
4488 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4489 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4492 * Need to release path in order to truncate a
4493 * compressed extent. So delete any accumulated
4494 * extent items so far.
4496 if (btrfs_file_extent_compression(leaf
, fi
) !=
4497 BTRFS_COMPRESS_NONE
&& pending_del_nr
) {
4498 err
= btrfs_del_items(trans
, root
, path
,
4502 btrfs_abort_transaction(trans
,
4509 err
= truncate_inline_extent(inode
, path
,
4514 btrfs_abort_transaction(trans
, err
);
4517 } else if (test_bit(BTRFS_ROOT_REF_COWS
,
4519 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4524 if (!pending_del_nr
) {
4525 /* no pending yet, add ourselves */
4526 pending_del_slot
= path
->slots
[0];
4528 } else if (pending_del_nr
&&
4529 path
->slots
[0] + 1 == pending_del_slot
) {
4530 /* hop on the pending chunk */
4532 pending_del_slot
= path
->slots
[0];
4539 should_throttle
= 0;
4542 (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4543 root
== fs_info
->tree_root
)) {
4544 btrfs_set_path_blocking(path
);
4545 bytes_deleted
+= extent_num_bytes
;
4546 ret
= btrfs_free_extent(trans
, fs_info
, extent_start
,
4547 extent_num_bytes
, 0,
4548 btrfs_header_owner(leaf
),
4549 ino
, extent_offset
);
4551 if (btrfs_should_throttle_delayed_refs(trans
, fs_info
))
4552 btrfs_async_run_delayed_refs(fs_info
,
4553 trans
->delayed_ref_updates
* 2,
4556 if (truncate_space_check(trans
, root
,
4557 extent_num_bytes
)) {
4560 if (btrfs_should_throttle_delayed_refs(trans
,
4562 should_throttle
= 1;
4566 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4569 if (path
->slots
[0] == 0 ||
4570 path
->slots
[0] != pending_del_slot
||
4571 should_throttle
|| should_end
) {
4572 if (pending_del_nr
) {
4573 ret
= btrfs_del_items(trans
, root
, path
,
4577 btrfs_abort_transaction(trans
, ret
);
4582 btrfs_release_path(path
);
4583 if (should_throttle
) {
4584 unsigned long updates
= trans
->delayed_ref_updates
;
4586 trans
->delayed_ref_updates
= 0;
4587 ret
= btrfs_run_delayed_refs(trans
,
4595 * if we failed to refill our space rsv, bail out
4596 * and let the transaction restart
4608 if (pending_del_nr
) {
4609 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4612 btrfs_abort_transaction(trans
, ret
);
4615 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
4616 btrfs_ordered_update_i_size(inode
, last_size
, NULL
);
4618 btrfs_free_path(path
);
4621 /* only inline file may have last_size != new_size */
4622 if (new_size
>= fs_info
->sectorsize
||
4623 new_size
> fs_info
->max_inline
)
4624 ASSERT(last_size
== new_size
);
4627 if (be_nice
&& bytes_deleted
> SZ_32M
) {
4628 unsigned long updates
= trans
->delayed_ref_updates
;
4630 trans
->delayed_ref_updates
= 0;
4631 ret
= btrfs_run_delayed_refs(trans
, fs_info
,
4641 * btrfs_truncate_block - read, zero a chunk and write a block
4642 * @inode - inode that we're zeroing
4643 * @from - the offset to start zeroing
4644 * @len - the length to zero, 0 to zero the entire range respective to the
4646 * @front - zero up to the offset instead of from the offset on
4648 * This will find the block for the "from" offset and cow the block and zero the
4649 * part we want to zero. This is used with truncate and hole punching.
4651 int btrfs_truncate_block(struct inode
*inode
, loff_t from
, loff_t len
,
4654 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4655 struct address_space
*mapping
= inode
->i_mapping
;
4656 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4657 struct btrfs_ordered_extent
*ordered
;
4658 struct extent_state
*cached_state
= NULL
;
4660 u32 blocksize
= fs_info
->sectorsize
;
4661 pgoff_t index
= from
>> PAGE_SHIFT
;
4662 unsigned offset
= from
& (blocksize
- 1);
4664 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4669 if ((offset
& (blocksize
- 1)) == 0 &&
4670 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4673 ret
= btrfs_delalloc_reserve_space(inode
,
4674 round_down(from
, blocksize
), blocksize
);
4679 page
= find_or_create_page(mapping
, index
, mask
);
4681 btrfs_delalloc_release_space(inode
,
4682 round_down(from
, blocksize
),
4688 block_start
= round_down(from
, blocksize
);
4689 block_end
= block_start
+ blocksize
- 1;
4691 if (!PageUptodate(page
)) {
4692 ret
= btrfs_readpage(NULL
, page
);
4694 if (page
->mapping
!= mapping
) {
4699 if (!PageUptodate(page
)) {
4704 wait_on_page_writeback(page
);
4706 lock_extent_bits(io_tree
, block_start
, block_end
, &cached_state
);
4707 set_page_extent_mapped(page
);
4709 ordered
= btrfs_lookup_ordered_extent(inode
, block_start
);
4711 unlock_extent_cached(io_tree
, block_start
, block_end
,
4712 &cached_state
, GFP_NOFS
);
4715 btrfs_start_ordered_extent(inode
, ordered
, 1);
4716 btrfs_put_ordered_extent(ordered
);
4720 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, block_start
, block_end
,
4721 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4722 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4723 0, 0, &cached_state
, GFP_NOFS
);
4725 ret
= btrfs_set_extent_delalloc(inode
, block_start
, block_end
,
4728 unlock_extent_cached(io_tree
, block_start
, block_end
,
4729 &cached_state
, GFP_NOFS
);
4733 if (offset
!= blocksize
) {
4735 len
= blocksize
- offset
;
4738 memset(kaddr
+ (block_start
- page_offset(page
)),
4741 memset(kaddr
+ (block_start
- page_offset(page
)) + offset
,
4743 flush_dcache_page(page
);
4746 ClearPageChecked(page
);
4747 set_page_dirty(page
);
4748 unlock_extent_cached(io_tree
, block_start
, block_end
, &cached_state
,
4753 btrfs_delalloc_release_space(inode
, block_start
,
4761 static int maybe_insert_hole(struct btrfs_root
*root
, struct inode
*inode
,
4762 u64 offset
, u64 len
)
4764 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4765 struct btrfs_trans_handle
*trans
;
4769 * Still need to make sure the inode looks like it's been updated so
4770 * that any holes get logged if we fsync.
4772 if (btrfs_fs_incompat(fs_info
, NO_HOLES
)) {
4773 BTRFS_I(inode
)->last_trans
= fs_info
->generation
;
4774 BTRFS_I(inode
)->last_sub_trans
= root
->log_transid
;
4775 BTRFS_I(inode
)->last_log_commit
= root
->last_log_commit
;
4780 * 1 - for the one we're dropping
4781 * 1 - for the one we're adding
4782 * 1 - for updating the inode.
4784 trans
= btrfs_start_transaction(root
, 3);
4786 return PTR_ERR(trans
);
4788 ret
= btrfs_drop_extents(trans
, root
, inode
, offset
, offset
+ len
, 1);
4790 btrfs_abort_transaction(trans
, ret
);
4791 btrfs_end_transaction(trans
);
4795 ret
= btrfs_insert_file_extent(trans
, root
, btrfs_ino(BTRFS_I(inode
)),
4796 offset
, 0, 0, len
, 0, len
, 0, 0, 0);
4798 btrfs_abort_transaction(trans
, ret
);
4800 btrfs_update_inode(trans
, root
, inode
);
4801 btrfs_end_transaction(trans
);
4806 * This function puts in dummy file extents for the area we're creating a hole
4807 * for. So if we are truncating this file to a larger size we need to insert
4808 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4809 * the range between oldsize and size
4811 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4813 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4814 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4815 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4816 struct extent_map
*em
= NULL
;
4817 struct extent_state
*cached_state
= NULL
;
4818 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4819 u64 hole_start
= ALIGN(oldsize
, fs_info
->sectorsize
);
4820 u64 block_end
= ALIGN(size
, fs_info
->sectorsize
);
4827 * If our size started in the middle of a block we need to zero out the
4828 * rest of the block before we expand the i_size, otherwise we could
4829 * expose stale data.
4831 err
= btrfs_truncate_block(inode
, oldsize
, 0, 0);
4835 if (size
<= hole_start
)
4839 struct btrfs_ordered_extent
*ordered
;
4841 lock_extent_bits(io_tree
, hole_start
, block_end
- 1,
4843 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), hole_start
,
4844 block_end
- hole_start
);
4847 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4848 &cached_state
, GFP_NOFS
);
4849 btrfs_start_ordered_extent(inode
, ordered
, 1);
4850 btrfs_put_ordered_extent(ordered
);
4853 cur_offset
= hole_start
;
4855 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, cur_offset
,
4856 block_end
- cur_offset
, 0);
4862 last_byte
= min(extent_map_end(em
), block_end
);
4863 last_byte
= ALIGN(last_byte
, fs_info
->sectorsize
);
4864 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4865 struct extent_map
*hole_em
;
4866 hole_size
= last_byte
- cur_offset
;
4868 err
= maybe_insert_hole(root
, inode
, cur_offset
,
4872 btrfs_drop_extent_cache(BTRFS_I(inode
), cur_offset
,
4873 cur_offset
+ hole_size
- 1, 0);
4874 hole_em
= alloc_extent_map();
4876 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4877 &BTRFS_I(inode
)->runtime_flags
);
4880 hole_em
->start
= cur_offset
;
4881 hole_em
->len
= hole_size
;
4882 hole_em
->orig_start
= cur_offset
;
4884 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4885 hole_em
->block_len
= 0;
4886 hole_em
->orig_block_len
= 0;
4887 hole_em
->ram_bytes
= hole_size
;
4888 hole_em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
4889 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4890 hole_em
->generation
= fs_info
->generation
;
4893 write_lock(&em_tree
->lock
);
4894 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4895 write_unlock(&em_tree
->lock
);
4898 btrfs_drop_extent_cache(BTRFS_I(inode
),
4903 free_extent_map(hole_em
);
4906 free_extent_map(em
);
4908 cur_offset
= last_byte
;
4909 if (cur_offset
>= block_end
)
4912 free_extent_map(em
);
4913 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4918 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4920 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4921 struct btrfs_trans_handle
*trans
;
4922 loff_t oldsize
= i_size_read(inode
);
4923 loff_t newsize
= attr
->ia_size
;
4924 int mask
= attr
->ia_valid
;
4928 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4929 * special case where we need to update the times despite not having
4930 * these flags set. For all other operations the VFS set these flags
4931 * explicitly if it wants a timestamp update.
4933 if (newsize
!= oldsize
) {
4934 inode_inc_iversion(inode
);
4935 if (!(mask
& (ATTR_CTIME
| ATTR_MTIME
)))
4936 inode
->i_ctime
= inode
->i_mtime
=
4937 current_time(inode
);
4940 if (newsize
> oldsize
) {
4942 * Don't do an expanding truncate while snapshoting is ongoing.
4943 * This is to ensure the snapshot captures a fully consistent
4944 * state of this file - if the snapshot captures this expanding
4945 * truncation, it must capture all writes that happened before
4948 btrfs_wait_for_snapshot_creation(root
);
4949 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4951 btrfs_end_write_no_snapshoting(root
);
4955 trans
= btrfs_start_transaction(root
, 1);
4956 if (IS_ERR(trans
)) {
4957 btrfs_end_write_no_snapshoting(root
);
4958 return PTR_ERR(trans
);
4961 i_size_write(inode
, newsize
);
4962 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4963 pagecache_isize_extended(inode
, oldsize
, newsize
);
4964 ret
= btrfs_update_inode(trans
, root
, inode
);
4965 btrfs_end_write_no_snapshoting(root
);
4966 btrfs_end_transaction(trans
);
4970 * We're truncating a file that used to have good data down to
4971 * zero. Make sure it gets into the ordered flush list so that
4972 * any new writes get down to disk quickly.
4975 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4976 &BTRFS_I(inode
)->runtime_flags
);
4979 * 1 for the orphan item we're going to add
4980 * 1 for the orphan item deletion.
4982 trans
= btrfs_start_transaction(root
, 2);
4984 return PTR_ERR(trans
);
4987 * We need to do this in case we fail at _any_ point during the
4988 * actual truncate. Once we do the truncate_setsize we could
4989 * invalidate pages which forces any outstanding ordered io to
4990 * be instantly completed which will give us extents that need
4991 * to be truncated. If we fail to get an orphan inode down we
4992 * could have left over extents that were never meant to live,
4993 * so we need to guarantee from this point on that everything
4994 * will be consistent.
4996 ret
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
4997 btrfs_end_transaction(trans
);
5001 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5002 truncate_setsize(inode
, newsize
);
5004 /* Disable nonlocked read DIO to avoid the end less truncate */
5005 btrfs_inode_block_unlocked_dio(BTRFS_I(inode
));
5006 inode_dio_wait(inode
);
5007 btrfs_inode_resume_unlocked_dio(inode
);
5009 ret
= btrfs_truncate(inode
);
5010 if (ret
&& inode
->i_nlink
) {
5013 /* To get a stable disk_i_size */
5014 err
= btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
5016 btrfs_orphan_del(NULL
, BTRFS_I(inode
));
5021 * failed to truncate, disk_i_size is only adjusted down
5022 * as we remove extents, so it should represent the true
5023 * size of the inode, so reset the in memory size and
5024 * delete our orphan entry.
5026 trans
= btrfs_join_transaction(root
);
5027 if (IS_ERR(trans
)) {
5028 btrfs_orphan_del(NULL
, BTRFS_I(inode
));
5031 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
5032 err
= btrfs_orphan_del(trans
, BTRFS_I(inode
));
5034 btrfs_abort_transaction(trans
, err
);
5035 btrfs_end_transaction(trans
);
5042 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
5044 struct inode
*inode
= d_inode(dentry
);
5045 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5048 if (btrfs_root_readonly(root
))
5051 err
= setattr_prepare(dentry
, attr
);
5055 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
5056 err
= btrfs_setsize(inode
, attr
);
5061 if (attr
->ia_valid
) {
5062 setattr_copy(inode
, attr
);
5063 inode_inc_iversion(inode
);
5064 err
= btrfs_dirty_inode(inode
);
5066 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
5067 err
= posix_acl_chmod(inode
, inode
->i_mode
);
5074 * While truncating the inode pages during eviction, we get the VFS calling
5075 * btrfs_invalidatepage() against each page of the inode. This is slow because
5076 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5077 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5078 * extent_state structures over and over, wasting lots of time.
5080 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5081 * those expensive operations on a per page basis and do only the ordered io
5082 * finishing, while we release here the extent_map and extent_state structures,
5083 * without the excessive merging and splitting.
5085 static void evict_inode_truncate_pages(struct inode
*inode
)
5087 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5088 struct extent_map_tree
*map_tree
= &BTRFS_I(inode
)->extent_tree
;
5089 struct rb_node
*node
;
5091 ASSERT(inode
->i_state
& I_FREEING
);
5092 truncate_inode_pages_final(&inode
->i_data
);
5094 write_lock(&map_tree
->lock
);
5095 while (!RB_EMPTY_ROOT(&map_tree
->map
)) {
5096 struct extent_map
*em
;
5098 node
= rb_first(&map_tree
->map
);
5099 em
= rb_entry(node
, struct extent_map
, rb_node
);
5100 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5101 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
5102 remove_extent_mapping(map_tree
, em
);
5103 free_extent_map(em
);
5104 if (need_resched()) {
5105 write_unlock(&map_tree
->lock
);
5107 write_lock(&map_tree
->lock
);
5110 write_unlock(&map_tree
->lock
);
5113 * Keep looping until we have no more ranges in the io tree.
5114 * We can have ongoing bios started by readpages (called from readahead)
5115 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5116 * still in progress (unlocked the pages in the bio but did not yet
5117 * unlocked the ranges in the io tree). Therefore this means some
5118 * ranges can still be locked and eviction started because before
5119 * submitting those bios, which are executed by a separate task (work
5120 * queue kthread), inode references (inode->i_count) were not taken
5121 * (which would be dropped in the end io callback of each bio).
5122 * Therefore here we effectively end up waiting for those bios and
5123 * anyone else holding locked ranges without having bumped the inode's
5124 * reference count - if we don't do it, when they access the inode's
5125 * io_tree to unlock a range it may be too late, leading to an
5126 * use-after-free issue.
5128 spin_lock(&io_tree
->lock
);
5129 while (!RB_EMPTY_ROOT(&io_tree
->state
)) {
5130 struct extent_state
*state
;
5131 struct extent_state
*cached_state
= NULL
;
5135 node
= rb_first(&io_tree
->state
);
5136 state
= rb_entry(node
, struct extent_state
, rb_node
);
5137 start
= state
->start
;
5139 spin_unlock(&io_tree
->lock
);
5141 lock_extent_bits(io_tree
, start
, end
, &cached_state
);
5144 * If still has DELALLOC flag, the extent didn't reach disk,
5145 * and its reserved space won't be freed by delayed_ref.
5146 * So we need to free its reserved space here.
5147 * (Refer to comment in btrfs_invalidatepage, case 2)
5149 * Note, end is the bytenr of last byte, so we need + 1 here.
5151 if (state
->state
& EXTENT_DELALLOC
)
5152 btrfs_qgroup_free_data(inode
, start
, end
- start
+ 1);
5154 clear_extent_bit(io_tree
, start
, end
,
5155 EXTENT_LOCKED
| EXTENT_DIRTY
|
5156 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
5157 EXTENT_DEFRAG
, 1, 1,
5158 &cached_state
, GFP_NOFS
);
5161 spin_lock(&io_tree
->lock
);
5163 spin_unlock(&io_tree
->lock
);
5166 void btrfs_evict_inode(struct inode
*inode
)
5168 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5169 struct btrfs_trans_handle
*trans
;
5170 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5171 struct btrfs_block_rsv
*rsv
, *global_rsv
;
5172 int steal_from_global
= 0;
5176 trace_btrfs_inode_evict(inode
);
5179 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
5183 min_size
= btrfs_calc_trunc_metadata_size(fs_info
, 1);
5185 evict_inode_truncate_pages(inode
);
5187 if (inode
->i_nlink
&&
5188 ((btrfs_root_refs(&root
->root_item
) != 0 &&
5189 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
5190 btrfs_is_free_space_inode(BTRFS_I(inode
))))
5193 if (is_bad_inode(inode
)) {
5194 btrfs_orphan_del(NULL
, BTRFS_I(inode
));
5197 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
5198 if (!special_file(inode
->i_mode
))
5199 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
5201 btrfs_free_io_failure_record(BTRFS_I(inode
), 0, (u64
)-1);
5203 if (test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
)) {
5204 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
5205 &BTRFS_I(inode
)->runtime_flags
));
5209 if (inode
->i_nlink
> 0) {
5210 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
5211 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
5215 ret
= btrfs_commit_inode_delayed_inode(BTRFS_I(inode
));
5217 btrfs_orphan_del(NULL
, BTRFS_I(inode
));
5221 rsv
= btrfs_alloc_block_rsv(fs_info
, BTRFS_BLOCK_RSV_TEMP
);
5223 btrfs_orphan_del(NULL
, BTRFS_I(inode
));
5226 rsv
->size
= min_size
;
5228 global_rsv
= &fs_info
->global_block_rsv
;
5230 btrfs_i_size_write(BTRFS_I(inode
), 0);
5233 * This is a bit simpler than btrfs_truncate since we've already
5234 * reserved our space for our orphan item in the unlink, so we just
5235 * need to reserve some slack space in case we add bytes and update
5236 * inode item when doing the truncate.
5239 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
5240 BTRFS_RESERVE_FLUSH_LIMIT
);
5243 * Try and steal from the global reserve since we will
5244 * likely not use this space anyway, we want to try as
5245 * hard as possible to get this to work.
5248 steal_from_global
++;
5250 steal_from_global
= 0;
5254 * steal_from_global == 0: we reserved stuff, hooray!
5255 * steal_from_global == 1: we didn't reserve stuff, boo!
5256 * steal_from_global == 2: we've committed, still not a lot of
5257 * room but maybe we'll have room in the global reserve this
5259 * steal_from_global == 3: abandon all hope!
5261 if (steal_from_global
> 2) {
5263 "Could not get space for a delete, will truncate on mount %d",
5265 btrfs_orphan_del(NULL
, BTRFS_I(inode
));
5266 btrfs_free_block_rsv(fs_info
, rsv
);
5270 trans
= btrfs_join_transaction(root
);
5271 if (IS_ERR(trans
)) {
5272 btrfs_orphan_del(NULL
, BTRFS_I(inode
));
5273 btrfs_free_block_rsv(fs_info
, rsv
);
5278 * We can't just steal from the global reserve, we need to make
5279 * sure there is room to do it, if not we need to commit and try
5282 if (steal_from_global
) {
5283 if (!btrfs_check_space_for_delayed_refs(trans
, fs_info
))
5284 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
,
5291 * Couldn't steal from the global reserve, we have too much
5292 * pending stuff built up, commit the transaction and try it
5296 ret
= btrfs_commit_transaction(trans
);
5298 btrfs_orphan_del(NULL
, BTRFS_I(inode
));
5299 btrfs_free_block_rsv(fs_info
, rsv
);
5304 steal_from_global
= 0;
5307 trans
->block_rsv
= rsv
;
5309 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
5310 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
)
5313 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
5314 btrfs_end_transaction(trans
);
5316 btrfs_btree_balance_dirty(fs_info
);
5319 btrfs_free_block_rsv(fs_info
, rsv
);
5322 * Errors here aren't a big deal, it just means we leave orphan items
5323 * in the tree. They will be cleaned up on the next mount.
5326 trans
->block_rsv
= root
->orphan_block_rsv
;
5327 btrfs_orphan_del(trans
, BTRFS_I(inode
));
5329 btrfs_orphan_del(NULL
, BTRFS_I(inode
));
5332 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
5333 if (!(root
== fs_info
->tree_root
||
5334 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
5335 btrfs_return_ino(root
, btrfs_ino(BTRFS_I(inode
)));
5337 btrfs_end_transaction(trans
);
5338 btrfs_btree_balance_dirty(fs_info
);
5340 btrfs_remove_delayed_node(BTRFS_I(inode
));
5345 * this returns the key found in the dir entry in the location pointer.
5346 * If no dir entries were found, location->objectid is 0.
5348 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
5349 struct btrfs_key
*location
)
5351 const char *name
= dentry
->d_name
.name
;
5352 int namelen
= dentry
->d_name
.len
;
5353 struct btrfs_dir_item
*di
;
5354 struct btrfs_path
*path
;
5355 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5358 path
= btrfs_alloc_path();
5362 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(BTRFS_I(dir
)),
5367 if (IS_ERR_OR_NULL(di
))
5370 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
5372 btrfs_free_path(path
);
5375 location
->objectid
= 0;
5380 * when we hit a tree root in a directory, the btrfs part of the inode
5381 * needs to be changed to reflect the root directory of the tree root. This
5382 * is kind of like crossing a mount point.
5384 static int fixup_tree_root_location(struct btrfs_fs_info
*fs_info
,
5386 struct dentry
*dentry
,
5387 struct btrfs_key
*location
,
5388 struct btrfs_root
**sub_root
)
5390 struct btrfs_path
*path
;
5391 struct btrfs_root
*new_root
;
5392 struct btrfs_root_ref
*ref
;
5393 struct extent_buffer
*leaf
;
5394 struct btrfs_key key
;
5398 path
= btrfs_alloc_path();
5405 key
.objectid
= BTRFS_I(dir
)->root
->root_key
.objectid
;
5406 key
.type
= BTRFS_ROOT_REF_KEY
;
5407 key
.offset
= location
->objectid
;
5409 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
5416 leaf
= path
->nodes
[0];
5417 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
5418 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(BTRFS_I(dir
)) ||
5419 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
5422 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
5423 (unsigned long)(ref
+ 1),
5424 dentry
->d_name
.len
);
5428 btrfs_release_path(path
);
5430 new_root
= btrfs_read_fs_root_no_name(fs_info
, location
);
5431 if (IS_ERR(new_root
)) {
5432 err
= PTR_ERR(new_root
);
5436 *sub_root
= new_root
;
5437 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
5438 location
->type
= BTRFS_INODE_ITEM_KEY
;
5439 location
->offset
= 0;
5442 btrfs_free_path(path
);
5446 static void inode_tree_add(struct inode
*inode
)
5448 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5449 struct btrfs_inode
*entry
;
5451 struct rb_node
*parent
;
5452 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
5453 u64 ino
= btrfs_ino(BTRFS_I(inode
));
5455 if (inode_unhashed(inode
))
5458 spin_lock(&root
->inode_lock
);
5459 p
= &root
->inode_tree
.rb_node
;
5462 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
5464 if (ino
< btrfs_ino(BTRFS_I(&entry
->vfs_inode
)))
5465 p
= &parent
->rb_left
;
5466 else if (ino
> btrfs_ino(BTRFS_I(&entry
->vfs_inode
)))
5467 p
= &parent
->rb_right
;
5469 WARN_ON(!(entry
->vfs_inode
.i_state
&
5470 (I_WILL_FREE
| I_FREEING
)));
5471 rb_replace_node(parent
, new, &root
->inode_tree
);
5472 RB_CLEAR_NODE(parent
);
5473 spin_unlock(&root
->inode_lock
);
5477 rb_link_node(new, parent
, p
);
5478 rb_insert_color(new, &root
->inode_tree
);
5479 spin_unlock(&root
->inode_lock
);
5482 static void inode_tree_del(struct inode
*inode
)
5484 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5485 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5488 spin_lock(&root
->inode_lock
);
5489 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
5490 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
5491 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
5492 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5494 spin_unlock(&root
->inode_lock
);
5496 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
5497 synchronize_srcu(&fs_info
->subvol_srcu
);
5498 spin_lock(&root
->inode_lock
);
5499 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5500 spin_unlock(&root
->inode_lock
);
5502 btrfs_add_dead_root(root
);
5506 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
5508 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5509 struct rb_node
*node
;
5510 struct rb_node
*prev
;
5511 struct btrfs_inode
*entry
;
5512 struct inode
*inode
;
5515 if (!test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
5516 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
5518 spin_lock(&root
->inode_lock
);
5520 node
= root
->inode_tree
.rb_node
;
5524 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5526 if (objectid
< btrfs_ino(BTRFS_I(&entry
->vfs_inode
)))
5527 node
= node
->rb_left
;
5528 else if (objectid
> btrfs_ino(BTRFS_I(&entry
->vfs_inode
)))
5529 node
= node
->rb_right
;
5535 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
5536 if (objectid
<= btrfs_ino(BTRFS_I(&entry
->vfs_inode
))) {
5540 prev
= rb_next(prev
);
5544 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5545 objectid
= btrfs_ino(BTRFS_I(&entry
->vfs_inode
)) + 1;
5546 inode
= igrab(&entry
->vfs_inode
);
5548 spin_unlock(&root
->inode_lock
);
5549 if (atomic_read(&inode
->i_count
) > 1)
5550 d_prune_aliases(inode
);
5552 * btrfs_drop_inode will have it removed from
5553 * the inode cache when its usage count
5558 spin_lock(&root
->inode_lock
);
5562 if (cond_resched_lock(&root
->inode_lock
))
5565 node
= rb_next(node
);
5567 spin_unlock(&root
->inode_lock
);
5570 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
5572 struct btrfs_iget_args
*args
= p
;
5573 inode
->i_ino
= args
->location
->objectid
;
5574 memcpy(&BTRFS_I(inode
)->location
, args
->location
,
5575 sizeof(*args
->location
));
5576 BTRFS_I(inode
)->root
= args
->root
;
5580 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
5582 struct btrfs_iget_args
*args
= opaque
;
5583 return args
->location
->objectid
== BTRFS_I(inode
)->location
.objectid
&&
5584 args
->root
== BTRFS_I(inode
)->root
;
5587 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
5588 struct btrfs_key
*location
,
5589 struct btrfs_root
*root
)
5591 struct inode
*inode
;
5592 struct btrfs_iget_args args
;
5593 unsigned long hashval
= btrfs_inode_hash(location
->objectid
, root
);
5595 args
.location
= location
;
5598 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
5599 btrfs_init_locked_inode
,
5604 /* Get an inode object given its location and corresponding root.
5605 * Returns in *is_new if the inode was read from disk
5607 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
5608 struct btrfs_root
*root
, int *new)
5610 struct inode
*inode
;
5612 inode
= btrfs_iget_locked(s
, location
, root
);
5614 return ERR_PTR(-ENOMEM
);
5616 if (inode
->i_state
& I_NEW
) {
5619 ret
= btrfs_read_locked_inode(inode
);
5620 if (!is_bad_inode(inode
)) {
5621 inode_tree_add(inode
);
5622 unlock_new_inode(inode
);
5626 unlock_new_inode(inode
);
5629 inode
= ERR_PTR(ret
< 0 ? ret
: -ESTALE
);
5636 static struct inode
*new_simple_dir(struct super_block
*s
,
5637 struct btrfs_key
*key
,
5638 struct btrfs_root
*root
)
5640 struct inode
*inode
= new_inode(s
);
5643 return ERR_PTR(-ENOMEM
);
5645 BTRFS_I(inode
)->root
= root
;
5646 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5647 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5649 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5650 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5651 inode
->i_opflags
&= ~IOP_XATTR
;
5652 inode
->i_fop
= &simple_dir_operations
;
5653 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5654 inode
->i_mtime
= current_time(inode
);
5655 inode
->i_atime
= inode
->i_mtime
;
5656 inode
->i_ctime
= inode
->i_mtime
;
5657 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
5662 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5664 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
5665 struct inode
*inode
;
5666 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5667 struct btrfs_root
*sub_root
= root
;
5668 struct btrfs_key location
;
5672 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5673 return ERR_PTR(-ENAMETOOLONG
);
5675 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5677 return ERR_PTR(ret
);
5679 if (location
.objectid
== 0)
5680 return ERR_PTR(-ENOENT
);
5682 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5683 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5687 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5689 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
5690 ret
= fixup_tree_root_location(fs_info
, dir
, dentry
,
5691 &location
, &sub_root
);
5694 inode
= ERR_PTR(ret
);
5696 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5698 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5700 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5702 if (!IS_ERR(inode
) && root
!= sub_root
) {
5703 down_read(&fs_info
->cleanup_work_sem
);
5704 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5705 ret
= btrfs_orphan_cleanup(sub_root
);
5706 up_read(&fs_info
->cleanup_work_sem
);
5709 inode
= ERR_PTR(ret
);
5716 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5718 struct btrfs_root
*root
;
5719 struct inode
*inode
= d_inode(dentry
);
5721 if (!inode
&& !IS_ROOT(dentry
))
5722 inode
= d_inode(dentry
->d_parent
);
5725 root
= BTRFS_I(inode
)->root
;
5726 if (btrfs_root_refs(&root
->root_item
) == 0)
5729 if (btrfs_ino(BTRFS_I(inode
)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5735 static void btrfs_dentry_release(struct dentry
*dentry
)
5737 kfree(dentry
->d_fsdata
);
5740 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5743 struct inode
*inode
;
5745 inode
= btrfs_lookup_dentry(dir
, dentry
);
5746 if (IS_ERR(inode
)) {
5747 if (PTR_ERR(inode
) == -ENOENT
)
5750 return ERR_CAST(inode
);
5753 return d_splice_alias(inode
, dentry
);
5756 unsigned char btrfs_filetype_table
[] = {
5757 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5760 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
5762 struct inode
*inode
= file_inode(file
);
5763 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5764 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5765 struct btrfs_item
*item
;
5766 struct btrfs_dir_item
*di
;
5767 struct btrfs_key key
;
5768 struct btrfs_key found_key
;
5769 struct btrfs_path
*path
;
5770 struct list_head ins_list
;
5771 struct list_head del_list
;
5773 struct extent_buffer
*leaf
;
5775 unsigned char d_type
;
5781 struct btrfs_key location
;
5783 if (!dir_emit_dots(file
, ctx
))
5786 path
= btrfs_alloc_path();
5790 path
->reada
= READA_FORWARD
;
5792 INIT_LIST_HEAD(&ins_list
);
5793 INIT_LIST_HEAD(&del_list
);
5794 put
= btrfs_readdir_get_delayed_items(inode
, &ins_list
, &del_list
);
5796 key
.type
= BTRFS_DIR_INDEX_KEY
;
5797 key
.offset
= ctx
->pos
;
5798 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
5800 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5805 leaf
= path
->nodes
[0];
5806 slot
= path
->slots
[0];
5807 if (slot
>= btrfs_header_nritems(leaf
)) {
5808 ret
= btrfs_next_leaf(root
, path
);
5816 item
= btrfs_item_nr(slot
);
5817 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5819 if (found_key
.objectid
!= key
.objectid
)
5821 if (found_key
.type
!= BTRFS_DIR_INDEX_KEY
)
5823 if (found_key
.offset
< ctx
->pos
)
5825 if (btrfs_should_delete_dir_index(&del_list
, found_key
.offset
))
5828 ctx
->pos
= found_key
.offset
;
5830 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5831 if (verify_dir_item(fs_info
, leaf
, di
))
5834 name_len
= btrfs_dir_name_len(leaf
, di
);
5835 if (name_len
<= sizeof(tmp_name
)) {
5836 name_ptr
= tmp_name
;
5838 name_ptr
= kmalloc(name_len
, GFP_KERNEL
);
5844 read_extent_buffer(leaf
, name_ptr
, (unsigned long)(di
+ 1),
5847 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5848 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5850 over
= !dir_emit(ctx
, name_ptr
, name_len
, location
.objectid
,
5853 if (name_ptr
!= tmp_name
)
5863 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
);
5868 * Stop new entries from being returned after we return the last
5871 * New directory entries are assigned a strictly increasing
5872 * offset. This means that new entries created during readdir
5873 * are *guaranteed* to be seen in the future by that readdir.
5874 * This has broken buggy programs which operate on names as
5875 * they're returned by readdir. Until we re-use freed offsets
5876 * we have this hack to stop new entries from being returned
5877 * under the assumption that they'll never reach this huge
5880 * This is being careful not to overflow 32bit loff_t unless the
5881 * last entry requires it because doing so has broken 32bit apps
5884 if (ctx
->pos
>= INT_MAX
)
5885 ctx
->pos
= LLONG_MAX
;
5892 btrfs_readdir_put_delayed_items(inode
, &ins_list
, &del_list
);
5893 btrfs_free_path(path
);
5897 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5899 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5900 struct btrfs_trans_handle
*trans
;
5902 bool nolock
= false;
5904 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5907 if (btrfs_fs_closing(root
->fs_info
) &&
5908 btrfs_is_free_space_inode(BTRFS_I(inode
)))
5911 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5913 trans
= btrfs_join_transaction_nolock(root
);
5915 trans
= btrfs_join_transaction(root
);
5917 return PTR_ERR(trans
);
5918 ret
= btrfs_commit_transaction(trans
);
5924 * This is somewhat expensive, updating the tree every time the
5925 * inode changes. But, it is most likely to find the inode in cache.
5926 * FIXME, needs more benchmarking...there are no reasons other than performance
5927 * to keep or drop this code.
5929 static int btrfs_dirty_inode(struct inode
*inode
)
5931 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5932 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5933 struct btrfs_trans_handle
*trans
;
5936 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5939 trans
= btrfs_join_transaction(root
);
5941 return PTR_ERR(trans
);
5943 ret
= btrfs_update_inode(trans
, root
, inode
);
5944 if (ret
&& ret
== -ENOSPC
) {
5945 /* whoops, lets try again with the full transaction */
5946 btrfs_end_transaction(trans
);
5947 trans
= btrfs_start_transaction(root
, 1);
5949 return PTR_ERR(trans
);
5951 ret
= btrfs_update_inode(trans
, root
, inode
);
5953 btrfs_end_transaction(trans
);
5954 if (BTRFS_I(inode
)->delayed_node
)
5955 btrfs_balance_delayed_items(fs_info
);
5961 * This is a copy of file_update_time. We need this so we can return error on
5962 * ENOSPC for updating the inode in the case of file write and mmap writes.
5964 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5967 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5969 if (btrfs_root_readonly(root
))
5972 if (flags
& S_VERSION
)
5973 inode_inc_iversion(inode
);
5974 if (flags
& S_CTIME
)
5975 inode
->i_ctime
= *now
;
5976 if (flags
& S_MTIME
)
5977 inode
->i_mtime
= *now
;
5978 if (flags
& S_ATIME
)
5979 inode
->i_atime
= *now
;
5980 return btrfs_dirty_inode(inode
);
5984 * find the highest existing sequence number in a directory
5985 * and then set the in-memory index_cnt variable to reflect
5986 * free sequence numbers
5988 static int btrfs_set_inode_index_count(struct btrfs_inode
*inode
)
5990 struct btrfs_root
*root
= inode
->root
;
5991 struct btrfs_key key
, found_key
;
5992 struct btrfs_path
*path
;
5993 struct extent_buffer
*leaf
;
5996 key
.objectid
= btrfs_ino(inode
);
5997 key
.type
= BTRFS_DIR_INDEX_KEY
;
5998 key
.offset
= (u64
)-1;
6000 path
= btrfs_alloc_path();
6004 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6007 /* FIXME: we should be able to handle this */
6013 * MAGIC NUMBER EXPLANATION:
6014 * since we search a directory based on f_pos we have to start at 2
6015 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6016 * else has to start at 2
6018 if (path
->slots
[0] == 0) {
6019 inode
->index_cnt
= 2;
6025 leaf
= path
->nodes
[0];
6026 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6028 if (found_key
.objectid
!= btrfs_ino(inode
) ||
6029 found_key
.type
!= BTRFS_DIR_INDEX_KEY
) {
6030 inode
->index_cnt
= 2;
6034 inode
->index_cnt
= found_key
.offset
+ 1;
6036 btrfs_free_path(path
);
6041 * helper to find a free sequence number in a given directory. This current
6042 * code is very simple, later versions will do smarter things in the btree
6044 int btrfs_set_inode_index(struct btrfs_inode
*dir
, u64
*index
)
6048 if (dir
->index_cnt
== (u64
)-1) {
6049 ret
= btrfs_inode_delayed_dir_index_count(dir
);
6051 ret
= btrfs_set_inode_index_count(dir
);
6057 *index
= dir
->index_cnt
;
6063 static int btrfs_insert_inode_locked(struct inode
*inode
)
6065 struct btrfs_iget_args args
;
6066 args
.location
= &BTRFS_I(inode
)->location
;
6067 args
.root
= BTRFS_I(inode
)->root
;
6069 return insert_inode_locked4(inode
,
6070 btrfs_inode_hash(inode
->i_ino
, BTRFS_I(inode
)->root
),
6071 btrfs_find_actor
, &args
);
6074 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
6075 struct btrfs_root
*root
,
6077 const char *name
, int name_len
,
6078 u64 ref_objectid
, u64 objectid
,
6079 umode_t mode
, u64
*index
)
6081 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6082 struct inode
*inode
;
6083 struct btrfs_inode_item
*inode_item
;
6084 struct btrfs_key
*location
;
6085 struct btrfs_path
*path
;
6086 struct btrfs_inode_ref
*ref
;
6087 struct btrfs_key key
[2];
6089 int nitems
= name
? 2 : 1;
6093 path
= btrfs_alloc_path();
6095 return ERR_PTR(-ENOMEM
);
6097 inode
= new_inode(fs_info
->sb
);
6099 btrfs_free_path(path
);
6100 return ERR_PTR(-ENOMEM
);
6104 * O_TMPFILE, set link count to 0, so that after this point,
6105 * we fill in an inode item with the correct link count.
6108 set_nlink(inode
, 0);
6111 * we have to initialize this early, so we can reclaim the inode
6112 * number if we fail afterwards in this function.
6114 inode
->i_ino
= objectid
;
6117 trace_btrfs_inode_request(dir
);
6119 ret
= btrfs_set_inode_index(BTRFS_I(dir
), index
);
6121 btrfs_free_path(path
);
6123 return ERR_PTR(ret
);
6129 * index_cnt is ignored for everything but a dir,
6130 * btrfs_get_inode_index_count has an explanation for the magic
6133 BTRFS_I(inode
)->index_cnt
= 2;
6134 BTRFS_I(inode
)->dir_index
= *index
;
6135 BTRFS_I(inode
)->root
= root
;
6136 BTRFS_I(inode
)->generation
= trans
->transid
;
6137 inode
->i_generation
= BTRFS_I(inode
)->generation
;
6140 * We could have gotten an inode number from somebody who was fsynced
6141 * and then removed in this same transaction, so let's just set full
6142 * sync since it will be a full sync anyway and this will blow away the
6143 * old info in the log.
6145 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
6147 key
[0].objectid
= objectid
;
6148 key
[0].type
= BTRFS_INODE_ITEM_KEY
;
6151 sizes
[0] = sizeof(struct btrfs_inode_item
);
6155 * Start new inodes with an inode_ref. This is slightly more
6156 * efficient for small numbers of hard links since they will
6157 * be packed into one item. Extended refs will kick in if we
6158 * add more hard links than can fit in the ref item.
6160 key
[1].objectid
= objectid
;
6161 key
[1].type
= BTRFS_INODE_REF_KEY
;
6162 key
[1].offset
= ref_objectid
;
6164 sizes
[1] = name_len
+ sizeof(*ref
);
6167 location
= &BTRFS_I(inode
)->location
;
6168 location
->objectid
= objectid
;
6169 location
->offset
= 0;
6170 location
->type
= BTRFS_INODE_ITEM_KEY
;
6172 ret
= btrfs_insert_inode_locked(inode
);
6176 path
->leave_spinning
= 1;
6177 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, nitems
);
6181 inode_init_owner(inode
, dir
, mode
);
6182 inode_set_bytes(inode
, 0);
6184 inode
->i_mtime
= current_time(inode
);
6185 inode
->i_atime
= inode
->i_mtime
;
6186 inode
->i_ctime
= inode
->i_mtime
;
6187 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
6189 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
6190 struct btrfs_inode_item
);
6191 memzero_extent_buffer(path
->nodes
[0], (unsigned long)inode_item
,
6192 sizeof(*inode_item
));
6193 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
6196 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
6197 struct btrfs_inode_ref
);
6198 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
6199 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
6200 ptr
= (unsigned long)(ref
+ 1);
6201 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
6204 btrfs_mark_buffer_dirty(path
->nodes
[0]);
6205 btrfs_free_path(path
);
6207 btrfs_inherit_iflags(inode
, dir
);
6209 if (S_ISREG(mode
)) {
6210 if (btrfs_test_opt(fs_info
, NODATASUM
))
6211 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
6212 if (btrfs_test_opt(fs_info
, NODATACOW
))
6213 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
6214 BTRFS_INODE_NODATASUM
;
6217 inode_tree_add(inode
);
6219 trace_btrfs_inode_new(inode
);
6220 btrfs_set_inode_last_trans(trans
, inode
);
6222 btrfs_update_root_times(trans
, root
);
6224 ret
= btrfs_inode_inherit_props(trans
, inode
, dir
);
6227 "error inheriting props for ino %llu (root %llu): %d",
6228 btrfs_ino(BTRFS_I(inode
)), root
->root_key
.objectid
, ret
);
6233 unlock_new_inode(inode
);
6236 BTRFS_I(dir
)->index_cnt
--;
6237 btrfs_free_path(path
);
6239 return ERR_PTR(ret
);
6242 static inline u8
btrfs_inode_type(struct inode
*inode
)
6244 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
6248 * utility function to add 'inode' into 'parent_inode' with
6249 * a give name and a given sequence number.
6250 * if 'add_backref' is true, also insert a backref from the
6251 * inode to the parent directory.
6253 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
6254 struct btrfs_inode
*parent_inode
, struct btrfs_inode
*inode
,
6255 const char *name
, int name_len
, int add_backref
, u64 index
)
6257 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
6259 struct btrfs_key key
;
6260 struct btrfs_root
*root
= parent_inode
->root
;
6261 u64 ino
= btrfs_ino(inode
);
6262 u64 parent_ino
= btrfs_ino(parent_inode
);
6264 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6265 memcpy(&key
, &inode
->root
->root_key
, sizeof(key
));
6268 key
.type
= BTRFS_INODE_ITEM_KEY
;
6272 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6273 ret
= btrfs_add_root_ref(trans
, fs_info
, key
.objectid
,
6274 root
->root_key
.objectid
, parent_ino
,
6275 index
, name
, name_len
);
6276 } else if (add_backref
) {
6277 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
6281 /* Nothing to clean up yet */
6285 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
6287 btrfs_inode_type(&inode
->vfs_inode
), index
);
6288 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
6291 btrfs_abort_transaction(trans
, ret
);
6295 btrfs_i_size_write(parent_inode
, parent_inode
->vfs_inode
.i_size
+
6297 inode_inc_iversion(&parent_inode
->vfs_inode
);
6298 parent_inode
->vfs_inode
.i_mtime
= parent_inode
->vfs_inode
.i_ctime
=
6299 current_time(&parent_inode
->vfs_inode
);
6300 ret
= btrfs_update_inode(trans
, root
, &parent_inode
->vfs_inode
);
6302 btrfs_abort_transaction(trans
, ret
);
6306 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6309 err
= btrfs_del_root_ref(trans
, fs_info
, key
.objectid
,
6310 root
->root_key
.objectid
, parent_ino
,
6311 &local_index
, name
, name_len
);
6313 } else if (add_backref
) {
6317 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
6318 ino
, parent_ino
, &local_index
);
6323 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
6324 struct btrfs_inode
*dir
, struct dentry
*dentry
,
6325 struct btrfs_inode
*inode
, int backref
, u64 index
)
6327 int err
= btrfs_add_link(trans
, dir
, inode
,
6328 dentry
->d_name
.name
, dentry
->d_name
.len
,
6335 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
6336 umode_t mode
, dev_t rdev
)
6338 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6339 struct btrfs_trans_handle
*trans
;
6340 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6341 struct inode
*inode
= NULL
;
6348 * 2 for inode item and ref
6350 * 1 for xattr if selinux is on
6352 trans
= btrfs_start_transaction(root
, 5);
6354 return PTR_ERR(trans
);
6356 err
= btrfs_find_free_ino(root
, &objectid
);
6360 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6361 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)), objectid
,
6363 if (IS_ERR(inode
)) {
6364 err
= PTR_ERR(inode
);
6369 * If the active LSM wants to access the inode during
6370 * d_instantiate it needs these. Smack checks to see
6371 * if the filesystem supports xattrs by looking at the
6374 inode
->i_op
= &btrfs_special_inode_operations
;
6375 init_special_inode(inode
, inode
->i_mode
, rdev
);
6377 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6379 goto out_unlock_inode
;
6381 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
, BTRFS_I(inode
),
6384 goto out_unlock_inode
;
6386 btrfs_update_inode(trans
, root
, inode
);
6387 unlock_new_inode(inode
);
6388 d_instantiate(dentry
, inode
);
6392 btrfs_end_transaction(trans
);
6393 btrfs_balance_delayed_items(fs_info
);
6394 btrfs_btree_balance_dirty(fs_info
);
6396 inode_dec_link_count(inode
);
6403 unlock_new_inode(inode
);
6408 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
6409 umode_t mode
, bool excl
)
6411 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6412 struct btrfs_trans_handle
*trans
;
6413 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6414 struct inode
*inode
= NULL
;
6415 int drop_inode_on_err
= 0;
6421 * 2 for inode item and ref
6423 * 1 for xattr if selinux is on
6425 trans
= btrfs_start_transaction(root
, 5);
6427 return PTR_ERR(trans
);
6429 err
= btrfs_find_free_ino(root
, &objectid
);
6433 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6434 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)), objectid
,
6436 if (IS_ERR(inode
)) {
6437 err
= PTR_ERR(inode
);
6440 drop_inode_on_err
= 1;
6442 * If the active LSM wants to access the inode during
6443 * d_instantiate it needs these. Smack checks to see
6444 * if the filesystem supports xattrs by looking at the
6447 inode
->i_fop
= &btrfs_file_operations
;
6448 inode
->i_op
= &btrfs_file_inode_operations
;
6449 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6451 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6453 goto out_unlock_inode
;
6455 err
= btrfs_update_inode(trans
, root
, inode
);
6457 goto out_unlock_inode
;
6459 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
, BTRFS_I(inode
),
6462 goto out_unlock_inode
;
6464 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6465 unlock_new_inode(inode
);
6466 d_instantiate(dentry
, inode
);
6469 btrfs_end_transaction(trans
);
6470 if (err
&& drop_inode_on_err
) {
6471 inode_dec_link_count(inode
);
6474 btrfs_balance_delayed_items(fs_info
);
6475 btrfs_btree_balance_dirty(fs_info
);
6479 unlock_new_inode(inode
);
6484 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
6485 struct dentry
*dentry
)
6487 struct btrfs_trans_handle
*trans
= NULL
;
6488 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6489 struct inode
*inode
= d_inode(old_dentry
);
6490 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
6495 /* do not allow sys_link's with other subvols of the same device */
6496 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
6499 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
6502 err
= btrfs_set_inode_index(BTRFS_I(dir
), &index
);
6507 * 2 items for inode and inode ref
6508 * 2 items for dir items
6509 * 1 item for parent inode
6511 trans
= btrfs_start_transaction(root
, 5);
6512 if (IS_ERR(trans
)) {
6513 err
= PTR_ERR(trans
);
6518 /* There are several dir indexes for this inode, clear the cache. */
6519 BTRFS_I(inode
)->dir_index
= 0ULL;
6521 inode_inc_iversion(inode
);
6522 inode
->i_ctime
= current_time(inode
);
6524 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
6526 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
, BTRFS_I(inode
),
6532 struct dentry
*parent
= dentry
->d_parent
;
6533 err
= btrfs_update_inode(trans
, root
, inode
);
6536 if (inode
->i_nlink
== 1) {
6538 * If new hard link count is 1, it's a file created
6539 * with open(2) O_TMPFILE flag.
6541 err
= btrfs_orphan_del(trans
, BTRFS_I(inode
));
6545 d_instantiate(dentry
, inode
);
6546 btrfs_log_new_name(trans
, BTRFS_I(inode
), NULL
, parent
);
6549 btrfs_balance_delayed_items(fs_info
);
6552 btrfs_end_transaction(trans
);
6554 inode_dec_link_count(inode
);
6557 btrfs_btree_balance_dirty(fs_info
);
6561 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
6563 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6564 struct inode
*inode
= NULL
;
6565 struct btrfs_trans_handle
*trans
;
6566 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6568 int drop_on_err
= 0;
6573 * 2 items for inode and ref
6574 * 2 items for dir items
6575 * 1 for xattr if selinux is on
6577 trans
= btrfs_start_transaction(root
, 5);
6579 return PTR_ERR(trans
);
6581 err
= btrfs_find_free_ino(root
, &objectid
);
6585 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6586 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)), objectid
,
6587 S_IFDIR
| mode
, &index
);
6588 if (IS_ERR(inode
)) {
6589 err
= PTR_ERR(inode
);
6594 /* these must be set before we unlock the inode */
6595 inode
->i_op
= &btrfs_dir_inode_operations
;
6596 inode
->i_fop
= &btrfs_dir_file_operations
;
6598 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6600 goto out_fail_inode
;
6602 btrfs_i_size_write(BTRFS_I(inode
), 0);
6603 err
= btrfs_update_inode(trans
, root
, inode
);
6605 goto out_fail_inode
;
6607 err
= btrfs_add_link(trans
, BTRFS_I(dir
), BTRFS_I(inode
),
6608 dentry
->d_name
.name
,
6609 dentry
->d_name
.len
, 0, index
);
6611 goto out_fail_inode
;
6613 d_instantiate(dentry
, inode
);
6615 * mkdir is special. We're unlocking after we call d_instantiate
6616 * to avoid a race with nfsd calling d_instantiate.
6618 unlock_new_inode(inode
);
6622 btrfs_end_transaction(trans
);
6624 inode_dec_link_count(inode
);
6627 btrfs_balance_delayed_items(fs_info
);
6628 btrfs_btree_balance_dirty(fs_info
);
6632 unlock_new_inode(inode
);
6636 /* Find next extent map of a given extent map, caller needs to ensure locks */
6637 static struct extent_map
*next_extent_map(struct extent_map
*em
)
6639 struct rb_node
*next
;
6641 next
= rb_next(&em
->rb_node
);
6644 return container_of(next
, struct extent_map
, rb_node
);
6647 static struct extent_map
*prev_extent_map(struct extent_map
*em
)
6649 struct rb_node
*prev
;
6651 prev
= rb_prev(&em
->rb_node
);
6654 return container_of(prev
, struct extent_map
, rb_node
);
6657 /* helper for btfs_get_extent. Given an existing extent in the tree,
6658 * the existing extent is the nearest extent to map_start,
6659 * and an extent that you want to insert, deal with overlap and insert
6660 * the best fitted new extent into the tree.
6662 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
6663 struct extent_map
*existing
,
6664 struct extent_map
*em
,
6667 struct extent_map
*prev
;
6668 struct extent_map
*next
;
6673 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
6675 if (existing
->start
> map_start
) {
6677 prev
= prev_extent_map(next
);
6680 next
= next_extent_map(prev
);
6683 start
= prev
? extent_map_end(prev
) : em
->start
;
6684 start
= max_t(u64
, start
, em
->start
);
6685 end
= next
? next
->start
: extent_map_end(em
);
6686 end
= min_t(u64
, end
, extent_map_end(em
));
6687 start_diff
= start
- em
->start
;
6689 em
->len
= end
- start
;
6690 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
6691 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
6692 em
->block_start
+= start_diff
;
6693 em
->block_len
-= start_diff
;
6695 return add_extent_mapping(em_tree
, em
, 0);
6698 static noinline
int uncompress_inline(struct btrfs_path
*path
,
6700 size_t pg_offset
, u64 extent_offset
,
6701 struct btrfs_file_extent_item
*item
)
6704 struct extent_buffer
*leaf
= path
->nodes
[0];
6707 unsigned long inline_size
;
6711 WARN_ON(pg_offset
!= 0);
6712 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6713 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6714 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6715 btrfs_item_nr(path
->slots
[0]));
6716 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6719 ptr
= btrfs_file_extent_inline_start(item
);
6721 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6723 max_size
= min_t(unsigned long, PAGE_SIZE
, max_size
);
6724 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6725 extent_offset
, inline_size
, max_size
);
6731 * a bit scary, this does extent mapping from logical file offset to the disk.
6732 * the ugly parts come from merging extents from the disk with the in-ram
6733 * representation. This gets more complex because of the data=ordered code,
6734 * where the in-ram extents might be locked pending data=ordered completion.
6736 * This also copies inline extents directly into the page.
6739 struct extent_map
*btrfs_get_extent(struct btrfs_inode
*inode
,
6741 size_t pg_offset
, u64 start
, u64 len
,
6744 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->vfs_inode
.i_sb
);
6747 u64 extent_start
= 0;
6749 u64 objectid
= btrfs_ino(inode
);
6751 struct btrfs_path
*path
= NULL
;
6752 struct btrfs_root
*root
= inode
->root
;
6753 struct btrfs_file_extent_item
*item
;
6754 struct extent_buffer
*leaf
;
6755 struct btrfs_key found_key
;
6756 struct extent_map
*em
= NULL
;
6757 struct extent_map_tree
*em_tree
= &inode
->extent_tree
;
6758 struct extent_io_tree
*io_tree
= &inode
->io_tree
;
6759 struct btrfs_trans_handle
*trans
= NULL
;
6760 const bool new_inline
= !page
|| create
;
6763 read_lock(&em_tree
->lock
);
6764 em
= lookup_extent_mapping(em_tree
, start
, len
);
6766 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
6767 read_unlock(&em_tree
->lock
);
6770 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6771 free_extent_map(em
);
6772 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6773 free_extent_map(em
);
6777 em
= alloc_extent_map();
6782 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
6783 em
->start
= EXTENT_MAP_HOLE
;
6784 em
->orig_start
= EXTENT_MAP_HOLE
;
6786 em
->block_len
= (u64
)-1;
6789 path
= btrfs_alloc_path();
6795 * Chances are we'll be called again, so go ahead and do
6798 path
->reada
= READA_FORWARD
;
6801 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6802 objectid
, start
, trans
!= NULL
);
6809 if (path
->slots
[0] == 0)
6814 leaf
= path
->nodes
[0];
6815 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6816 struct btrfs_file_extent_item
);
6817 /* are we inside the extent that was found? */
6818 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6819 found_type
= found_key
.type
;
6820 if (found_key
.objectid
!= objectid
||
6821 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6823 * If we backup past the first extent we want to move forward
6824 * and see if there is an extent in front of us, otherwise we'll
6825 * say there is a hole for our whole search range which can
6832 found_type
= btrfs_file_extent_type(leaf
, item
);
6833 extent_start
= found_key
.offset
;
6834 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6835 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6836 extent_end
= extent_start
+
6837 btrfs_file_extent_num_bytes(leaf
, item
);
6838 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6840 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6841 extent_end
= ALIGN(extent_start
+ size
,
6842 fs_info
->sectorsize
);
6845 if (start
>= extent_end
) {
6847 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6848 ret
= btrfs_next_leaf(root
, path
);
6855 leaf
= path
->nodes
[0];
6857 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6858 if (found_key
.objectid
!= objectid
||
6859 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6861 if (start
+ len
<= found_key
.offset
)
6863 if (start
> found_key
.offset
)
6866 em
->orig_start
= start
;
6867 em
->len
= found_key
.offset
- start
;
6871 btrfs_extent_item_to_extent_map(inode
, path
, item
,
6874 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6875 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6877 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6881 size_t extent_offset
;
6887 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6888 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6889 copy_size
= min_t(u64
, PAGE_SIZE
- pg_offset
,
6890 size
- extent_offset
);
6891 em
->start
= extent_start
+ extent_offset
;
6892 em
->len
= ALIGN(copy_size
, fs_info
->sectorsize
);
6893 em
->orig_block_len
= em
->len
;
6894 em
->orig_start
= em
->start
;
6895 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6896 if (create
== 0 && !PageUptodate(page
)) {
6897 if (btrfs_file_extent_compression(leaf
, item
) !=
6898 BTRFS_COMPRESS_NONE
) {
6899 ret
= uncompress_inline(path
, page
, pg_offset
,
6900 extent_offset
, item
);
6907 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6909 if (pg_offset
+ copy_size
< PAGE_SIZE
) {
6910 memset(map
+ pg_offset
+ copy_size
, 0,
6911 PAGE_SIZE
- pg_offset
-
6916 flush_dcache_page(page
);
6917 } else if (create
&& PageUptodate(page
)) {
6921 free_extent_map(em
);
6924 btrfs_release_path(path
);
6925 trans
= btrfs_join_transaction(root
);
6928 return ERR_CAST(trans
);
6932 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6935 btrfs_mark_buffer_dirty(leaf
);
6937 set_extent_uptodate(io_tree
, em
->start
,
6938 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6943 em
->orig_start
= start
;
6946 em
->block_start
= EXTENT_MAP_HOLE
;
6947 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6949 btrfs_release_path(path
);
6950 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6952 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6953 em
->start
, em
->len
, start
, len
);
6959 write_lock(&em_tree
->lock
);
6960 ret
= add_extent_mapping(em_tree
, em
, 0);
6961 /* it is possible that someone inserted the extent into the tree
6962 * while we had the lock dropped. It is also possible that
6963 * an overlapping map exists in the tree
6965 if (ret
== -EEXIST
) {
6966 struct extent_map
*existing
;
6970 existing
= search_extent_mapping(em_tree
, start
, len
);
6972 * existing will always be non-NULL, since there must be
6973 * extent causing the -EEXIST.
6975 if (existing
->start
== em
->start
&&
6976 extent_map_end(existing
) >= extent_map_end(em
) &&
6977 em
->block_start
== existing
->block_start
) {
6979 * The existing extent map already encompasses the
6980 * entire extent map we tried to add.
6982 free_extent_map(em
);
6986 } else if (start
>= extent_map_end(existing
) ||
6987 start
<= existing
->start
) {
6989 * The existing extent map is the one nearest to
6990 * the [start, start + len) range which overlaps
6992 err
= merge_extent_mapping(em_tree
, existing
,
6994 free_extent_map(existing
);
6996 free_extent_map(em
);
7000 free_extent_map(em
);
7005 write_unlock(&em_tree
->lock
);
7008 trace_btrfs_get_extent(root
, inode
, em
);
7010 btrfs_free_path(path
);
7012 ret
= btrfs_end_transaction(trans
);
7017 free_extent_map(em
);
7018 return ERR_PTR(err
);
7020 BUG_ON(!em
); /* Error is always set */
7024 struct extent_map
*btrfs_get_extent_fiemap(struct btrfs_inode
*inode
,
7026 size_t pg_offset
, u64 start
, u64 len
,
7029 struct extent_map
*em
;
7030 struct extent_map
*hole_em
= NULL
;
7031 u64 range_start
= start
;
7037 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
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
))
7054 /* check to see if we've wrapped (len == -1 or similar) */
7063 /* ok, we didn't find anything, lets look for delalloc */
7064 found
= count_range_bits(&inode
->io_tree
, &range_start
,
7065 end
, len
, EXTENT_DELALLOC
, 1);
7066 found_end
= range_start
+ found
;
7067 if (found_end
< range_start
)
7068 found_end
= (u64
)-1;
7071 * we didn't find anything useful, return
7072 * the original results from get_extent()
7074 if (range_start
> end
|| found_end
<= start
) {
7080 /* adjust the range_start to make sure it doesn't
7081 * go backwards from the start they passed in
7083 range_start
= max(start
, range_start
);
7084 found
= found_end
- range_start
;
7087 u64 hole_start
= start
;
7090 em
= alloc_extent_map();
7096 * when btrfs_get_extent can't find anything it
7097 * returns one huge hole
7099 * make sure what it found really fits our range, and
7100 * adjust to make sure it is based on the start from
7104 u64 calc_end
= extent_map_end(hole_em
);
7106 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
7107 free_extent_map(hole_em
);
7110 hole_start
= max(hole_em
->start
, start
);
7111 hole_len
= calc_end
- hole_start
;
7115 if (hole_em
&& range_start
> hole_start
) {
7116 /* our hole starts before our delalloc, so we
7117 * have to return just the parts of the hole
7118 * that go until the delalloc starts
7120 em
->len
= min(hole_len
,
7121 range_start
- hole_start
);
7122 em
->start
= hole_start
;
7123 em
->orig_start
= hole_start
;
7125 * don't adjust block start at all,
7126 * it is fixed at EXTENT_MAP_HOLE
7128 em
->block_start
= hole_em
->block_start
;
7129 em
->block_len
= hole_len
;
7130 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
7131 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
7133 em
->start
= range_start
;
7135 em
->orig_start
= range_start
;
7136 em
->block_start
= EXTENT_MAP_DELALLOC
;
7137 em
->block_len
= found
;
7139 } else if (hole_em
) {
7144 free_extent_map(hole_em
);
7146 free_extent_map(em
);
7147 return ERR_PTR(err
);
7152 static struct extent_map
*btrfs_create_dio_extent(struct inode
*inode
,
7155 const u64 orig_start
,
7156 const u64 block_start
,
7157 const u64 block_len
,
7158 const u64 orig_block_len
,
7159 const u64 ram_bytes
,
7162 struct extent_map
*em
= NULL
;
7165 if (type
!= BTRFS_ORDERED_NOCOW
) {
7166 em
= create_io_em(inode
, start
, len
, orig_start
,
7167 block_start
, block_len
, orig_block_len
,
7169 BTRFS_COMPRESS_NONE
, /* compress_type */
7174 ret
= btrfs_add_ordered_extent_dio(inode
, start
, block_start
,
7175 len
, block_len
, type
);
7178 free_extent_map(em
);
7179 btrfs_drop_extent_cache(BTRFS_I(inode
), start
,
7180 start
+ len
- 1, 0);
7189 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
7192 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7193 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7194 struct extent_map
*em
;
7195 struct btrfs_key ins
;
7199 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
7200 ret
= btrfs_reserve_extent(root
, len
, len
, fs_info
->sectorsize
,
7201 0, alloc_hint
, &ins
, 1, 1);
7203 return ERR_PTR(ret
);
7205 em
= btrfs_create_dio_extent(inode
, start
, ins
.offset
, start
,
7206 ins
.objectid
, ins
.offset
, ins
.offset
,
7207 ins
.offset
, BTRFS_ORDERED_REGULAR
);
7208 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
7210 btrfs_free_reserved_extent(fs_info
, ins
.objectid
,
7217 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7218 * block must be cow'd
7220 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
7221 u64
*orig_start
, u64
*orig_block_len
,
7224 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7225 struct btrfs_path
*path
;
7227 struct extent_buffer
*leaf
;
7228 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7229 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7230 struct btrfs_file_extent_item
*fi
;
7231 struct btrfs_key key
;
7238 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
7240 path
= btrfs_alloc_path();
7244 ret
= btrfs_lookup_file_extent(NULL
, root
, path
,
7245 btrfs_ino(BTRFS_I(inode
)), offset
, 0);
7249 slot
= path
->slots
[0];
7252 /* can't find the item, must cow */
7259 leaf
= path
->nodes
[0];
7260 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
7261 if (key
.objectid
!= btrfs_ino(BTRFS_I(inode
)) ||
7262 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
7263 /* not our file or wrong item type, must cow */
7267 if (key
.offset
> offset
) {
7268 /* Wrong offset, must cow */
7272 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
7273 found_type
= btrfs_file_extent_type(leaf
, fi
);
7274 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
7275 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
7276 /* not a regular extent, must cow */
7280 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
7283 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
7284 if (extent_end
<= offset
)
7287 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
7288 if (disk_bytenr
== 0)
7291 if (btrfs_file_extent_compression(leaf
, fi
) ||
7292 btrfs_file_extent_encryption(leaf
, fi
) ||
7293 btrfs_file_extent_other_encoding(leaf
, fi
))
7296 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
7299 *orig_start
= key
.offset
- backref_offset
;
7300 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
7301 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
7304 if (btrfs_extent_readonly(fs_info
, disk_bytenr
))
7307 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
7308 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
7311 range_end
= round_up(offset
+ num_bytes
,
7312 root
->fs_info
->sectorsize
) - 1;
7313 ret
= test_range_bit(io_tree
, offset
, range_end
,
7314 EXTENT_DELALLOC
, 0, NULL
);
7321 btrfs_release_path(path
);
7324 * look for other files referencing this extent, if we
7325 * find any we must cow
7328 ret
= btrfs_cross_ref_exist(root
, btrfs_ino(BTRFS_I(inode
)),
7329 key
.offset
- backref_offset
, disk_bytenr
);
7336 * adjust disk_bytenr and num_bytes to cover just the bytes
7337 * in this extent we are about to write. If there
7338 * are any csums in that range we have to cow in order
7339 * to keep the csums correct
7341 disk_bytenr
+= backref_offset
;
7342 disk_bytenr
+= offset
- key
.offset
;
7343 if (csum_exist_in_range(fs_info
, disk_bytenr
, num_bytes
))
7346 * all of the above have passed, it is safe to overwrite this extent
7352 btrfs_free_path(path
);
7356 bool btrfs_page_exists_in_range(struct inode
*inode
, loff_t start
, loff_t end
)
7358 struct radix_tree_root
*root
= &inode
->i_mapping
->page_tree
;
7360 void **pagep
= NULL
;
7361 struct page
*page
= NULL
;
7365 start_idx
= start
>> PAGE_SHIFT
;
7368 * end is the last byte in the last page. end == start is legal
7370 end_idx
= end
>> PAGE_SHIFT
;
7374 /* Most of the code in this while loop is lifted from
7375 * find_get_page. It's been modified to begin searching from a
7376 * page and return just the first page found in that range. If the
7377 * found idx is less than or equal to the end idx then we know that
7378 * a page exists. If no pages are found or if those pages are
7379 * outside of the range then we're fine (yay!) */
7380 while (page
== NULL
&&
7381 radix_tree_gang_lookup_slot(root
, &pagep
, NULL
, start_idx
, 1)) {
7382 page
= radix_tree_deref_slot(pagep
);
7383 if (unlikely(!page
))
7386 if (radix_tree_exception(page
)) {
7387 if (radix_tree_deref_retry(page
)) {
7392 * Otherwise, shmem/tmpfs must be storing a swap entry
7393 * here as an exceptional entry: so return it without
7394 * attempting to raise page count.
7397 break; /* TODO: Is this relevant for this use case? */
7400 if (!page_cache_get_speculative(page
)) {
7406 * Has the page moved?
7407 * This is part of the lockless pagecache protocol. See
7408 * include/linux/pagemap.h for details.
7410 if (unlikely(page
!= *pagep
)) {
7417 if (page
->index
<= end_idx
)
7426 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
7427 struct extent_state
**cached_state
, int writing
)
7429 struct btrfs_ordered_extent
*ordered
;
7433 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7436 * We're concerned with the entire range that we're going to be
7437 * doing DIO to, so we need to make sure there's no ordered
7438 * extents in this range.
7440 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), lockstart
,
7441 lockend
- lockstart
+ 1);
7444 * We need to make sure there are no buffered pages in this
7445 * range either, we could have raced between the invalidate in
7446 * generic_file_direct_write and locking the extent. The
7447 * invalidate needs to happen so that reads after a write do not
7452 !btrfs_page_exists_in_range(inode
, lockstart
, lockend
)))
7455 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7456 cached_state
, GFP_NOFS
);
7460 * If we are doing a DIO read and the ordered extent we
7461 * found is for a buffered write, we can not wait for it
7462 * to complete and retry, because if we do so we can
7463 * deadlock with concurrent buffered writes on page
7464 * locks. This happens only if our DIO read covers more
7465 * than one extent map, if at this point has already
7466 * created an ordered extent for a previous extent map
7467 * and locked its range in the inode's io tree, and a
7468 * concurrent write against that previous extent map's
7469 * range and this range started (we unlock the ranges
7470 * in the io tree only when the bios complete and
7471 * buffered writes always lock pages before attempting
7472 * to lock range in the io tree).
7475 test_bit(BTRFS_ORDERED_DIRECT
, &ordered
->flags
))
7476 btrfs_start_ordered_extent(inode
, ordered
, 1);
7479 btrfs_put_ordered_extent(ordered
);
7482 * We could trigger writeback for this range (and wait
7483 * for it to complete) and then invalidate the pages for
7484 * this range (through invalidate_inode_pages2_range()),
7485 * but that can lead us to a deadlock with a concurrent
7486 * call to readpages() (a buffered read or a defrag call
7487 * triggered a readahead) on a page lock due to an
7488 * ordered dio extent we created before but did not have
7489 * yet a corresponding bio submitted (whence it can not
7490 * complete), which makes readpages() wait for that
7491 * ordered extent to complete while holding a lock on
7506 /* The callers of this must take lock_extent() */
7507 static struct extent_map
*create_io_em(struct inode
*inode
, u64 start
, u64 len
,
7508 u64 orig_start
, u64 block_start
,
7509 u64 block_len
, u64 orig_block_len
,
7510 u64 ram_bytes
, int compress_type
,
7513 struct extent_map_tree
*em_tree
;
7514 struct extent_map
*em
;
7515 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7518 ASSERT(type
== BTRFS_ORDERED_PREALLOC
||
7519 type
== BTRFS_ORDERED_COMPRESSED
||
7520 type
== BTRFS_ORDERED_NOCOW
||
7521 type
== BTRFS_ORDERED_REGULAR
);
7523 em_tree
= &BTRFS_I(inode
)->extent_tree
;
7524 em
= alloc_extent_map();
7526 return ERR_PTR(-ENOMEM
);
7529 em
->orig_start
= orig_start
;
7531 em
->block_len
= block_len
;
7532 em
->block_start
= block_start
;
7533 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
7534 em
->orig_block_len
= orig_block_len
;
7535 em
->ram_bytes
= ram_bytes
;
7536 em
->generation
= -1;
7537 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
7538 if (type
== BTRFS_ORDERED_PREALLOC
) {
7539 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
7540 } else if (type
== BTRFS_ORDERED_COMPRESSED
) {
7541 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
7542 em
->compress_type
= compress_type
;
7546 btrfs_drop_extent_cache(BTRFS_I(inode
), em
->start
,
7547 em
->start
+ em
->len
- 1, 0);
7548 write_lock(&em_tree
->lock
);
7549 ret
= add_extent_mapping(em_tree
, em
, 1);
7550 write_unlock(&em_tree
->lock
);
7552 * The caller has taken lock_extent(), who could race with us
7555 } while (ret
== -EEXIST
);
7558 free_extent_map(em
);
7559 return ERR_PTR(ret
);
7562 /* em got 2 refs now, callers needs to do free_extent_map once. */
7566 static void adjust_dio_outstanding_extents(struct inode
*inode
,
7567 struct btrfs_dio_data
*dio_data
,
7570 unsigned num_extents
= count_max_extents(len
);
7573 * If we have an outstanding_extents count still set then we're
7574 * within our reservation, otherwise we need to adjust our inode
7575 * counter appropriately.
7577 if (dio_data
->outstanding_extents
>= num_extents
) {
7578 dio_data
->outstanding_extents
-= num_extents
;
7581 * If dio write length has been split due to no large enough
7582 * contiguous space, we need to compensate our inode counter
7585 u64 num_needed
= num_extents
- dio_data
->outstanding_extents
;
7587 spin_lock(&BTRFS_I(inode
)->lock
);
7588 BTRFS_I(inode
)->outstanding_extents
+= num_needed
;
7589 spin_unlock(&BTRFS_I(inode
)->lock
);
7593 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
7594 struct buffer_head
*bh_result
, int create
)
7596 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7597 struct extent_map
*em
;
7598 struct extent_state
*cached_state
= NULL
;
7599 struct btrfs_dio_data
*dio_data
= NULL
;
7600 u64 start
= iblock
<< inode
->i_blkbits
;
7601 u64 lockstart
, lockend
;
7602 u64 len
= bh_result
->b_size
;
7603 int unlock_bits
= EXTENT_LOCKED
;
7607 unlock_bits
|= EXTENT_DIRTY
;
7609 len
= min_t(u64
, len
, fs_info
->sectorsize
);
7612 lockend
= start
+ len
- 1;
7614 if (current
->journal_info
) {
7616 * Need to pull our outstanding extents and set journal_info to NULL so
7617 * that anything that needs to check if there's a transaction doesn't get
7620 dio_data
= current
->journal_info
;
7621 current
->journal_info
= NULL
;
7625 * If this errors out it's because we couldn't invalidate pagecache for
7626 * this range and we need to fallback to buffered.
7628 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
,
7634 em
= btrfs_get_extent(BTRFS_I(inode
), NULL
, 0, start
, len
, 0);
7641 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7642 * io. INLINE is special, and we could probably kludge it in here, but
7643 * it's still buffered so for safety lets just fall back to the generic
7646 * For COMPRESSED we _have_ to read the entire extent in so we can
7647 * decompress it, so there will be buffering required no matter what we
7648 * do, so go ahead and fallback to buffered.
7650 * We return -ENOTBLK because that's what makes DIO go ahead and go back
7651 * to buffered IO. Don't blame me, this is the price we pay for using
7654 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
7655 em
->block_start
== EXTENT_MAP_INLINE
) {
7656 free_extent_map(em
);
7661 /* Just a good old fashioned hole, return */
7662 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
7663 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
7664 free_extent_map(em
);
7669 * We don't allocate a new extent in the following cases
7671 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7673 * 2) The extent is marked as PREALLOC. We're good to go here and can
7674 * just use the extent.
7678 len
= min(len
, em
->len
- (start
- em
->start
));
7679 lockstart
= start
+ len
;
7683 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
7684 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
7685 em
->block_start
!= EXTENT_MAP_HOLE
)) {
7687 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
7689 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7690 type
= BTRFS_ORDERED_PREALLOC
;
7692 type
= BTRFS_ORDERED_NOCOW
;
7693 len
= min(len
, em
->len
- (start
- em
->start
));
7694 block_start
= em
->block_start
+ (start
- em
->start
);
7696 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
7697 &orig_block_len
, &ram_bytes
) == 1 &&
7698 btrfs_inc_nocow_writers(fs_info
, block_start
)) {
7699 struct extent_map
*em2
;
7701 em2
= btrfs_create_dio_extent(inode
, start
, len
,
7702 orig_start
, block_start
,
7703 len
, orig_block_len
,
7705 btrfs_dec_nocow_writers(fs_info
, block_start
);
7706 if (type
== BTRFS_ORDERED_PREALLOC
) {
7707 free_extent_map(em
);
7710 if (em2
&& IS_ERR(em2
)) {
7715 * For inode marked NODATACOW or extent marked PREALLOC,
7716 * use the existing or preallocated extent, so does not
7717 * need to adjust btrfs_space_info's bytes_may_use.
7719 btrfs_free_reserved_data_space_noquota(inode
,
7726 * this will cow the extent, reset the len in case we changed
7729 len
= bh_result
->b_size
;
7730 free_extent_map(em
);
7731 em
= btrfs_new_extent_direct(inode
, start
, len
);
7736 len
= min(len
, em
->len
- (start
- em
->start
));
7738 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
7740 bh_result
->b_size
= len
;
7741 bh_result
->b_bdev
= em
->bdev
;
7742 set_buffer_mapped(bh_result
);
7744 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7745 set_buffer_new(bh_result
);
7748 * Need to update the i_size under the extent lock so buffered
7749 * readers will get the updated i_size when we unlock.
7751 if (!dio_data
->overwrite
&& start
+ len
> i_size_read(inode
))
7752 i_size_write(inode
, start
+ len
);
7754 adjust_dio_outstanding_extents(inode
, dio_data
, len
);
7755 WARN_ON(dio_data
->reserve
< len
);
7756 dio_data
->reserve
-= len
;
7757 dio_data
->unsubmitted_oe_range_end
= start
+ len
;
7758 current
->journal_info
= dio_data
;
7762 * In the case of write we need to clear and unlock the entire range,
7763 * in the case of read we need to unlock only the end area that we
7764 * aren't using if there is any left over space.
7766 if (lockstart
< lockend
) {
7767 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
7768 lockend
, unlock_bits
, 1, 0,
7769 &cached_state
, GFP_NOFS
);
7771 free_extent_state(cached_state
);
7774 free_extent_map(em
);
7779 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7780 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
7783 current
->journal_info
= dio_data
;
7785 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7786 * write less data then expected, so that we don't underflow our inode's
7787 * outstanding extents counter.
7789 if (create
&& dio_data
)
7790 adjust_dio_outstanding_extents(inode
, dio_data
, len
);
7795 static inline int submit_dio_repair_bio(struct inode
*inode
, struct bio
*bio
,
7798 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7801 BUG_ON(bio_op(bio
) == REQ_OP_WRITE
);
7805 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, BTRFS_WQ_ENDIO_DIO_REPAIR
);
7809 ret
= btrfs_map_bio(fs_info
, bio
, mirror_num
, 0);
7815 static int btrfs_check_dio_repairable(struct inode
*inode
,
7816 struct bio
*failed_bio
,
7817 struct io_failure_record
*failrec
,
7820 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7823 num_copies
= btrfs_num_copies(fs_info
, failrec
->logical
, failrec
->len
);
7824 if (num_copies
== 1) {
7826 * we only have a single copy of the data, so don't bother with
7827 * all the retry and error correction code that follows. no
7828 * matter what the error is, it is very likely to persist.
7830 btrfs_debug(fs_info
,
7831 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7832 num_copies
, failrec
->this_mirror
, failed_mirror
);
7836 failrec
->failed_mirror
= failed_mirror
;
7837 failrec
->this_mirror
++;
7838 if (failrec
->this_mirror
== failed_mirror
)
7839 failrec
->this_mirror
++;
7841 if (failrec
->this_mirror
> num_copies
) {
7842 btrfs_debug(fs_info
,
7843 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7844 num_copies
, failrec
->this_mirror
, failed_mirror
);
7851 static int dio_read_error(struct inode
*inode
, struct bio
*failed_bio
,
7852 struct page
*page
, unsigned int pgoff
,
7853 u64 start
, u64 end
, int failed_mirror
,
7854 bio_end_io_t
*repair_endio
, void *repair_arg
)
7856 struct io_failure_record
*failrec
;
7862 BUG_ON(bio_op(failed_bio
) == REQ_OP_WRITE
);
7864 ret
= btrfs_get_io_failure_record(inode
, start
, end
, &failrec
);
7868 ret
= btrfs_check_dio_repairable(inode
, failed_bio
, failrec
,
7871 free_io_failure(BTRFS_I(inode
), failrec
);
7875 if ((failed_bio
->bi_vcnt
> 1)
7876 || (failed_bio
->bi_io_vec
->bv_len
7877 > btrfs_inode_sectorsize(inode
)))
7878 read_mode
|= REQ_FAILFAST_DEV
;
7880 isector
= start
- btrfs_io_bio(failed_bio
)->logical
;
7881 isector
>>= inode
->i_sb
->s_blocksize_bits
;
7882 bio
= btrfs_create_repair_bio(inode
, failed_bio
, failrec
, page
,
7883 pgoff
, isector
, repair_endio
, repair_arg
);
7885 free_io_failure(BTRFS_I(inode
), failrec
);
7888 bio_set_op_attrs(bio
, REQ_OP_READ
, read_mode
);
7890 btrfs_debug(BTRFS_I(inode
)->root
->fs_info
,
7891 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7892 read_mode
, failrec
->this_mirror
, failrec
->in_validation
);
7894 ret
= submit_dio_repair_bio(inode
, bio
, failrec
->this_mirror
);
7896 free_io_failure(BTRFS_I(inode
), failrec
);
7903 struct btrfs_retry_complete
{
7904 struct completion done
;
7905 struct inode
*inode
;
7910 static void btrfs_retry_endio_nocsum(struct bio
*bio
)
7912 struct btrfs_retry_complete
*done
= bio
->bi_private
;
7913 struct inode
*inode
;
7914 struct bio_vec
*bvec
;
7920 ASSERT(bio
->bi_vcnt
== 1);
7921 inode
= bio
->bi_io_vec
->bv_page
->mapping
->host
;
7922 ASSERT(bio
->bi_io_vec
->bv_len
== btrfs_inode_sectorsize(inode
));
7925 bio_for_each_segment_all(bvec
, bio
, i
)
7926 clean_io_failure(BTRFS_I(done
->inode
), done
->start
, bvec
->bv_page
, 0);
7928 complete(&done
->done
);
7932 static int __btrfs_correct_data_nocsum(struct inode
*inode
,
7933 struct btrfs_io_bio
*io_bio
)
7935 struct btrfs_fs_info
*fs_info
;
7936 struct bio_vec
*bvec
;
7937 struct btrfs_retry_complete done
;
7945 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
7946 sectorsize
= fs_info
->sectorsize
;
7948 start
= io_bio
->logical
;
7951 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
7952 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
->bv_len
);
7953 pgoff
= bvec
->bv_offset
;
7955 next_block_or_try_again
:
7958 init_completion(&done
.done
);
7960 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
,
7961 pgoff
, start
, start
+ sectorsize
- 1,
7963 btrfs_retry_endio_nocsum
, &done
);
7967 wait_for_completion(&done
.done
);
7969 if (!done
.uptodate
) {
7970 /* We might have another mirror, so try again */
7971 goto next_block_or_try_again
;
7974 start
+= sectorsize
;
7977 pgoff
+= sectorsize
;
7978 goto next_block_or_try_again
;
7985 static void btrfs_retry_endio(struct bio
*bio
)
7987 struct btrfs_retry_complete
*done
= bio
->bi_private
;
7988 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
7989 struct inode
*inode
;
7990 struct bio_vec
*bvec
;
8001 start
= done
->start
;
8003 ASSERT(bio
->bi_vcnt
== 1);
8004 inode
= bio
->bi_io_vec
->bv_page
->mapping
->host
;
8005 ASSERT(bio
->bi_io_vec
->bv_len
== btrfs_inode_sectorsize(inode
));
8007 bio_for_each_segment_all(bvec
, bio
, i
) {
8008 ret
= __readpage_endio_check(done
->inode
, io_bio
, i
,
8009 bvec
->bv_page
, bvec
->bv_offset
,
8010 done
->start
, bvec
->bv_len
);
8012 clean_io_failure(BTRFS_I(done
->inode
), done
->start
,
8013 bvec
->bv_page
, bvec
->bv_offset
);
8018 done
->uptodate
= uptodate
;
8020 complete(&done
->done
);
8024 static int __btrfs_subio_endio_read(struct inode
*inode
,
8025 struct btrfs_io_bio
*io_bio
, int err
)
8027 struct btrfs_fs_info
*fs_info
;
8028 struct bio_vec
*bvec
;
8029 struct btrfs_retry_complete done
;
8039 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
8040 sectorsize
= fs_info
->sectorsize
;
8043 start
= io_bio
->logical
;
8046 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
8047 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
->bv_len
);
8049 pgoff
= bvec
->bv_offset
;
8051 csum_pos
= BTRFS_BYTES_TO_BLKS(fs_info
, offset
);
8052 ret
= __readpage_endio_check(inode
, io_bio
, csum_pos
,
8053 bvec
->bv_page
, pgoff
, start
,
8060 init_completion(&done
.done
);
8062 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
,
8063 pgoff
, start
, start
+ sectorsize
- 1,
8065 btrfs_retry_endio
, &done
);
8071 wait_for_completion(&done
.done
);
8073 if (!done
.uptodate
) {
8074 /* We might have another mirror, so try again */
8078 offset
+= sectorsize
;
8079 start
+= sectorsize
;
8084 pgoff
+= sectorsize
;
8092 static int btrfs_subio_endio_read(struct inode
*inode
,
8093 struct btrfs_io_bio
*io_bio
, int err
)
8095 bool skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
8099 return __btrfs_correct_data_nocsum(inode
, io_bio
);
8103 return __btrfs_subio_endio_read(inode
, io_bio
, err
);
8107 static void btrfs_endio_direct_read(struct bio
*bio
)
8109 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8110 struct inode
*inode
= dip
->inode
;
8111 struct bio
*dio_bio
;
8112 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8113 int err
= bio
->bi_error
;
8115 if (dip
->flags
& BTRFS_DIO_ORIG_BIO_SUBMITTED
)
8116 err
= btrfs_subio_endio_read(inode
, io_bio
, err
);
8118 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
8119 dip
->logical_offset
+ dip
->bytes
- 1);
8120 dio_bio
= dip
->dio_bio
;
8124 dio_bio
->bi_error
= bio
->bi_error
;
8125 dio_end_io(dio_bio
, bio
->bi_error
);
8128 io_bio
->end_io(io_bio
, err
);
8132 static void btrfs_endio_direct_write_update_ordered(struct inode
*inode
,
8137 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8138 struct btrfs_ordered_extent
*ordered
= NULL
;
8139 u64 ordered_offset
= offset
;
8140 u64 ordered_bytes
= bytes
;
8144 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
8151 btrfs_init_work(&ordered
->work
, btrfs_endio_write_helper
,
8152 finish_ordered_fn
, NULL
, NULL
);
8153 btrfs_queue_work(fs_info
->endio_write_workers
, &ordered
->work
);
8156 * our bio might span multiple ordered extents. If we haven't
8157 * completed the accounting for the whole dio, go back and try again
8159 if (ordered_offset
< offset
+ bytes
) {
8160 ordered_bytes
= offset
+ bytes
- ordered_offset
;
8166 static void btrfs_endio_direct_write(struct bio
*bio
)
8168 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8169 struct bio
*dio_bio
= dip
->dio_bio
;
8171 btrfs_endio_direct_write_update_ordered(dip
->inode
,
8172 dip
->logical_offset
,
8178 dio_bio
->bi_error
= bio
->bi_error
;
8179 dio_end_io(dio_bio
, bio
->bi_error
);
8183 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
,
8184 struct bio
*bio
, int mirror_num
,
8185 unsigned long bio_flags
, u64 offset
)
8188 ret
= btrfs_csum_one_bio(inode
, bio
, offset
, 1);
8189 BUG_ON(ret
); /* -ENOMEM */
8193 static void btrfs_end_dio_bio(struct bio
*bio
)
8195 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8196 int err
= bio
->bi_error
;
8199 btrfs_warn(BTRFS_I(dip
->inode
)->root
->fs_info
,
8200 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
8201 btrfs_ino(BTRFS_I(dip
->inode
)), bio_op(bio
),
8203 (unsigned long long)bio
->bi_iter
.bi_sector
,
8204 bio
->bi_iter
.bi_size
, err
);
8206 if (dip
->subio_endio
)
8207 err
= dip
->subio_endio(dip
->inode
, btrfs_io_bio(bio
), err
);
8213 * before atomic variable goto zero, we must make sure
8214 * dip->errors is perceived to be set.
8216 smp_mb__before_atomic();
8219 /* if there are more bios still pending for this dio, just exit */
8220 if (!atomic_dec_and_test(&dip
->pending_bios
))
8224 bio_io_error(dip
->orig_bio
);
8226 dip
->dio_bio
->bi_error
= 0;
8227 bio_endio(dip
->orig_bio
);
8233 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
8234 u64 first_sector
, gfp_t gfp_flags
)
8237 bio
= btrfs_bio_alloc(bdev
, first_sector
, BIO_MAX_PAGES
, gfp_flags
);
8239 bio_associate_current(bio
);
8243 static inline int btrfs_lookup_and_bind_dio_csum(struct inode
*inode
,
8244 struct btrfs_dio_private
*dip
,
8248 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8249 struct btrfs_io_bio
*orig_io_bio
= btrfs_io_bio(dip
->orig_bio
);
8253 * We load all the csum data we need when we submit
8254 * the first bio to reduce the csum tree search and
8257 if (dip
->logical_offset
== file_offset
) {
8258 ret
= btrfs_lookup_bio_sums_dio(inode
, dip
->orig_bio
,
8264 if (bio
== dip
->orig_bio
)
8267 file_offset
-= dip
->logical_offset
;
8268 file_offset
>>= inode
->i_sb
->s_blocksize_bits
;
8269 io_bio
->csum
= (u8
*)(((u32
*)orig_io_bio
->csum
) + file_offset
);
8274 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
8275 u64 file_offset
, int skip_sum
,
8278 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8279 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8280 bool write
= bio_op(bio
) == REQ_OP_WRITE
;
8284 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
8289 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, BTRFS_WQ_ENDIO_DATA
);
8297 if (write
&& async_submit
) {
8298 ret
= btrfs_wq_submit_bio(fs_info
, inode
, bio
, 0, 0,
8300 __btrfs_submit_bio_start_direct_io
,
8301 __btrfs_submit_bio_done
);
8305 * If we aren't doing async submit, calculate the csum of the
8308 ret
= btrfs_csum_one_bio(inode
, bio
, file_offset
, 1);
8312 ret
= btrfs_lookup_and_bind_dio_csum(inode
, dip
, bio
,
8318 ret
= btrfs_map_bio(fs_info
, bio
, 0, async_submit
);
8324 static int btrfs_submit_direct_hook(struct btrfs_dio_private
*dip
,
8327 struct inode
*inode
= dip
->inode
;
8328 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8329 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8331 struct bio
*orig_bio
= dip
->orig_bio
;
8332 struct bio_vec
*bvec
;
8333 u64 start_sector
= orig_bio
->bi_iter
.bi_sector
;
8334 u64 file_offset
= dip
->logical_offset
;
8337 u32 blocksize
= fs_info
->sectorsize
;
8338 int async_submit
= 0;
8343 map_length
= orig_bio
->bi_iter
.bi_size
;
8344 ret
= btrfs_map_block(fs_info
, btrfs_op(orig_bio
), start_sector
<< 9,
8345 &map_length
, NULL
, 0);
8349 if (map_length
>= orig_bio
->bi_iter
.bi_size
) {
8351 dip
->flags
|= BTRFS_DIO_ORIG_BIO_SUBMITTED
;
8355 /* async crcs make it difficult to collect full stripe writes. */
8356 if (btrfs_get_alloc_profile(root
, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK
)
8361 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
8365 bio
->bi_opf
= orig_bio
->bi_opf
;
8366 bio
->bi_private
= dip
;
8367 bio
->bi_end_io
= btrfs_end_dio_bio
;
8368 btrfs_io_bio(bio
)->logical
= file_offset
;
8369 atomic_inc(&dip
->pending_bios
);
8371 bio_for_each_segment_all(bvec
, orig_bio
, j
) {
8372 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
->bv_len
);
8375 if (unlikely(map_length
< submit_len
+ blocksize
||
8376 bio_add_page(bio
, bvec
->bv_page
, blocksize
,
8377 bvec
->bv_offset
+ (i
* blocksize
)) < blocksize
)) {
8379 * inc the count before we submit the bio so
8380 * we know the end IO handler won't happen before
8381 * we inc the count. Otherwise, the dip might get freed
8382 * before we're done setting it up
8384 atomic_inc(&dip
->pending_bios
);
8385 ret
= __btrfs_submit_dio_bio(bio
, inode
,
8386 file_offset
, skip_sum
,
8390 atomic_dec(&dip
->pending_bios
);
8394 start_sector
+= submit_len
>> 9;
8395 file_offset
+= submit_len
;
8399 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
8400 start_sector
, GFP_NOFS
);
8403 bio
->bi_opf
= orig_bio
->bi_opf
;
8404 bio
->bi_private
= dip
;
8405 bio
->bi_end_io
= btrfs_end_dio_bio
;
8406 btrfs_io_bio(bio
)->logical
= file_offset
;
8408 map_length
= orig_bio
->bi_iter
.bi_size
;
8409 ret
= btrfs_map_block(fs_info
, btrfs_op(orig_bio
),
8411 &map_length
, NULL
, 0);
8419 submit_len
+= blocksize
;
8428 ret
= __btrfs_submit_dio_bio(bio
, inode
, file_offset
, skip_sum
,
8437 * before atomic variable goto zero, we must
8438 * make sure dip->errors is perceived to be set.
8440 smp_mb__before_atomic();
8441 if (atomic_dec_and_test(&dip
->pending_bios
))
8442 bio_io_error(dip
->orig_bio
);
8444 /* bio_end_io() will handle error, so we needn't return it */
8448 static void btrfs_submit_direct(struct bio
*dio_bio
, struct inode
*inode
,
8451 struct btrfs_dio_private
*dip
= NULL
;
8452 struct bio
*io_bio
= NULL
;
8453 struct btrfs_io_bio
*btrfs_bio
;
8455 bool write
= (bio_op(dio_bio
) == REQ_OP_WRITE
);
8458 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
8460 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
8466 dip
= kzalloc(sizeof(*dip
), GFP_NOFS
);
8472 dip
->private = dio_bio
->bi_private
;
8474 dip
->logical_offset
= file_offset
;
8475 dip
->bytes
= dio_bio
->bi_iter
.bi_size
;
8476 dip
->disk_bytenr
= (u64
)dio_bio
->bi_iter
.bi_sector
<< 9;
8477 io_bio
->bi_private
= dip
;
8478 dip
->orig_bio
= io_bio
;
8479 dip
->dio_bio
= dio_bio
;
8480 atomic_set(&dip
->pending_bios
, 0);
8481 btrfs_bio
= btrfs_io_bio(io_bio
);
8482 btrfs_bio
->logical
= file_offset
;
8485 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
8487 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
8488 dip
->subio_endio
= btrfs_subio_endio_read
;
8492 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8493 * even if we fail to submit a bio, because in such case we do the
8494 * corresponding error handling below and it must not be done a second
8495 * time by btrfs_direct_IO().
8498 struct btrfs_dio_data
*dio_data
= current
->journal_info
;
8500 dio_data
->unsubmitted_oe_range_end
= dip
->logical_offset
+
8502 dio_data
->unsubmitted_oe_range_start
=
8503 dio_data
->unsubmitted_oe_range_end
;
8506 ret
= btrfs_submit_direct_hook(dip
, skip_sum
);
8510 if (btrfs_bio
->end_io
)
8511 btrfs_bio
->end_io(btrfs_bio
, ret
);
8515 * If we arrived here it means either we failed to submit the dip
8516 * or we either failed to clone the dio_bio or failed to allocate the
8517 * dip. If we cloned the dio_bio and allocated the dip, we can just
8518 * call bio_endio against our io_bio so that we get proper resource
8519 * cleanup if we fail to submit the dip, otherwise, we must do the
8520 * same as btrfs_endio_direct_[write|read] because we can't call these
8521 * callbacks - they require an allocated dip and a clone of dio_bio.
8523 if (io_bio
&& dip
) {
8524 io_bio
->bi_error
= -EIO
;
8527 * The end io callbacks free our dip, do the final put on io_bio
8528 * and all the cleanup and final put for dio_bio (through
8535 btrfs_endio_direct_write_update_ordered(inode
,
8537 dio_bio
->bi_iter
.bi_size
,
8540 unlock_extent(&BTRFS_I(inode
)->io_tree
, file_offset
,
8541 file_offset
+ dio_bio
->bi_iter
.bi_size
- 1);
8543 dio_bio
->bi_error
= -EIO
;
8545 * Releases and cleans up our dio_bio, no need to bio_put()
8546 * nor bio_endio()/bio_io_error() against dio_bio.
8548 dio_end_io(dio_bio
, ret
);
8555 static ssize_t
check_direct_IO(struct btrfs_fs_info
*fs_info
,
8557 const struct iov_iter
*iter
, loff_t offset
)
8561 unsigned int blocksize_mask
= fs_info
->sectorsize
- 1;
8562 ssize_t retval
= -EINVAL
;
8564 if (offset
& blocksize_mask
)
8567 if (iov_iter_alignment(iter
) & blocksize_mask
)
8570 /* If this is a write we don't need to check anymore */
8571 if (iov_iter_rw(iter
) != READ
|| !iter_is_iovec(iter
))
8574 * Check to make sure we don't have duplicate iov_base's in this
8575 * iovec, if so return EINVAL, otherwise we'll get csum errors
8576 * when reading back.
8578 for (seg
= 0; seg
< iter
->nr_segs
; seg
++) {
8579 for (i
= seg
+ 1; i
< iter
->nr_segs
; i
++) {
8580 if (iter
->iov
[seg
].iov_base
== iter
->iov
[i
].iov_base
)
8589 static ssize_t
btrfs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
8591 struct file
*file
= iocb
->ki_filp
;
8592 struct inode
*inode
= file
->f_mapping
->host
;
8593 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8594 struct btrfs_dio_data dio_data
= { 0 };
8595 loff_t offset
= iocb
->ki_pos
;
8599 bool relock
= false;
8602 if (check_direct_IO(fs_info
, iocb
, iter
, offset
))
8605 inode_dio_begin(inode
);
8606 smp_mb__after_atomic();
8609 * The generic stuff only does filemap_write_and_wait_range, which
8610 * isn't enough if we've written compressed pages to this area, so
8611 * we need to flush the dirty pages again to make absolutely sure
8612 * that any outstanding dirty pages are on disk.
8614 count
= iov_iter_count(iter
);
8615 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
8616 &BTRFS_I(inode
)->runtime_flags
))
8617 filemap_fdatawrite_range(inode
->i_mapping
, offset
,
8618 offset
+ count
- 1);
8620 if (iov_iter_rw(iter
) == WRITE
) {
8622 * If the write DIO is beyond the EOF, we need update
8623 * the isize, but it is protected by i_mutex. So we can
8624 * not unlock the i_mutex at this case.
8626 if (offset
+ count
<= inode
->i_size
) {
8627 dio_data
.overwrite
= 1;
8628 inode_unlock(inode
);
8631 ret
= btrfs_delalloc_reserve_space(inode
, offset
, count
);
8634 dio_data
.outstanding_extents
= count_max_extents(count
);
8637 * We need to know how many extents we reserved so that we can
8638 * do the accounting properly if we go over the number we
8639 * originally calculated. Abuse current->journal_info for this.
8641 dio_data
.reserve
= round_up(count
,
8642 fs_info
->sectorsize
);
8643 dio_data
.unsubmitted_oe_range_start
= (u64
)offset
;
8644 dio_data
.unsubmitted_oe_range_end
= (u64
)offset
;
8645 current
->journal_info
= &dio_data
;
8646 down_read(&BTRFS_I(inode
)->dio_sem
);
8647 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
8648 &BTRFS_I(inode
)->runtime_flags
)) {
8649 inode_dio_end(inode
);
8650 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
8654 ret
= __blockdev_direct_IO(iocb
, inode
,
8655 fs_info
->fs_devices
->latest_bdev
,
8656 iter
, btrfs_get_blocks_direct
, NULL
,
8657 btrfs_submit_direct
, flags
);
8658 if (iov_iter_rw(iter
) == WRITE
) {
8659 up_read(&BTRFS_I(inode
)->dio_sem
);
8660 current
->journal_info
= NULL
;
8661 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
8662 if (dio_data
.reserve
)
8663 btrfs_delalloc_release_space(inode
, offset
,
8666 * On error we might have left some ordered extents
8667 * without submitting corresponding bios for them, so
8668 * cleanup them up to avoid other tasks getting them
8669 * and waiting for them to complete forever.
8671 if (dio_data
.unsubmitted_oe_range_start
<
8672 dio_data
.unsubmitted_oe_range_end
)
8673 btrfs_endio_direct_write_update_ordered(inode
,
8674 dio_data
.unsubmitted_oe_range_start
,
8675 dio_data
.unsubmitted_oe_range_end
-
8676 dio_data
.unsubmitted_oe_range_start
,
8678 } else if (ret
>= 0 && (size_t)ret
< count
)
8679 btrfs_delalloc_release_space(inode
, offset
,
8680 count
- (size_t)ret
);
8684 inode_dio_end(inode
);
8691 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8693 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
8694 __u64 start
, __u64 len
)
8698 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
8702 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
8705 int btrfs_readpage(struct file
*file
, struct page
*page
)
8707 struct extent_io_tree
*tree
;
8708 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8709 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
8712 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
8714 struct extent_io_tree
*tree
;
8715 struct inode
*inode
= page
->mapping
->host
;
8718 if (current
->flags
& PF_MEMALLOC
) {
8719 redirty_page_for_writepage(wbc
, page
);
8725 * If we are under memory pressure we will call this directly from the
8726 * VM, we need to make sure we have the inode referenced for the ordered
8727 * extent. If not just return like we didn't do anything.
8729 if (!igrab(inode
)) {
8730 redirty_page_for_writepage(wbc
, page
);
8731 return AOP_WRITEPAGE_ACTIVATE
;
8733 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8734 ret
= extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
8735 btrfs_add_delayed_iput(inode
);
8739 static int btrfs_writepages(struct address_space
*mapping
,
8740 struct writeback_control
*wbc
)
8742 struct extent_io_tree
*tree
;
8744 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8745 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
8749 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
8750 struct list_head
*pages
, unsigned nr_pages
)
8752 struct extent_io_tree
*tree
;
8753 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8754 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
8757 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8759 struct extent_io_tree
*tree
;
8760 struct extent_map_tree
*map
;
8763 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8764 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
8765 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
8767 ClearPagePrivate(page
);
8768 set_page_private(page
, 0);
8774 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8776 if (PageWriteback(page
) || PageDirty(page
))
8778 return __btrfs_releasepage(page
, gfp_flags
);
8781 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
8782 unsigned int length
)
8784 struct inode
*inode
= page
->mapping
->host
;
8785 struct extent_io_tree
*tree
;
8786 struct btrfs_ordered_extent
*ordered
;
8787 struct extent_state
*cached_state
= NULL
;
8788 u64 page_start
= page_offset(page
);
8789 u64 page_end
= page_start
+ PAGE_SIZE
- 1;
8792 int inode_evicting
= inode
->i_state
& I_FREEING
;
8795 * we have the page locked, so new writeback can't start,
8796 * and the dirty bit won't be cleared while we are here.
8798 * Wait for IO on this page so that we can safely clear
8799 * the PagePrivate2 bit and do ordered accounting
8801 wait_on_page_writeback(page
);
8803 tree
= &BTRFS_I(inode
)->io_tree
;
8805 btrfs_releasepage(page
, GFP_NOFS
);
8809 if (!inode_evicting
)
8810 lock_extent_bits(tree
, page_start
, page_end
, &cached_state
);
8813 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), start
,
8814 page_end
- start
+ 1);
8816 end
= min(page_end
, ordered
->file_offset
+ ordered
->len
- 1);
8818 * IO on this page will never be started, so we need
8819 * to account for any ordered extents now
8821 if (!inode_evicting
)
8822 clear_extent_bit(tree
, start
, end
,
8823 EXTENT_DIRTY
| EXTENT_DELALLOC
|
8824 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
8825 EXTENT_DEFRAG
, 1, 0, &cached_state
,
8828 * whoever cleared the private bit is responsible
8829 * for the finish_ordered_io
8831 if (TestClearPagePrivate2(page
)) {
8832 struct btrfs_ordered_inode_tree
*tree
;
8835 tree
= &BTRFS_I(inode
)->ordered_tree
;
8837 spin_lock_irq(&tree
->lock
);
8838 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
8839 new_len
= start
- ordered
->file_offset
;
8840 if (new_len
< ordered
->truncated_len
)
8841 ordered
->truncated_len
= new_len
;
8842 spin_unlock_irq(&tree
->lock
);
8844 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
8846 end
- start
+ 1, 1))
8847 btrfs_finish_ordered_io(ordered
);
8849 btrfs_put_ordered_extent(ordered
);
8850 if (!inode_evicting
) {
8851 cached_state
= NULL
;
8852 lock_extent_bits(tree
, start
, end
,
8857 if (start
< page_end
)
8862 * Qgroup reserved space handler
8863 * Page here will be either
8864 * 1) Already written to disk
8865 * In this case, its reserved space is released from data rsv map
8866 * and will be freed by delayed_ref handler finally.
8867 * So even we call qgroup_free_data(), it won't decrease reserved
8869 * 2) Not written to disk
8870 * This means the reserved space should be freed here. However,
8871 * if a truncate invalidates the page (by clearing PageDirty)
8872 * and the page is accounted for while allocating extent
8873 * in btrfs_check_data_free_space() we let delayed_ref to
8874 * free the entire extent.
8876 if (PageDirty(page
))
8877 btrfs_qgroup_free_data(inode
, page_start
, PAGE_SIZE
);
8878 if (!inode_evicting
) {
8879 clear_extent_bit(tree
, page_start
, page_end
,
8880 EXTENT_LOCKED
| EXTENT_DIRTY
|
8881 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
8882 EXTENT_DEFRAG
, 1, 1,
8883 &cached_state
, GFP_NOFS
);
8885 __btrfs_releasepage(page
, GFP_NOFS
);
8888 ClearPageChecked(page
);
8889 if (PagePrivate(page
)) {
8890 ClearPagePrivate(page
);
8891 set_page_private(page
, 0);
8897 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8898 * called from a page fault handler when a page is first dirtied. Hence we must
8899 * be careful to check for EOF conditions here. We set the page up correctly
8900 * for a written page which means we get ENOSPC checking when writing into
8901 * holes and correct delalloc and unwritten extent mapping on filesystems that
8902 * support these features.
8904 * We are not allowed to take the i_mutex here so we have to play games to
8905 * protect against truncate races as the page could now be beyond EOF. Because
8906 * vmtruncate() writes the inode size before removing pages, once we have the
8907 * page lock we can determine safely if the page is beyond EOF. If it is not
8908 * beyond EOF, then the page is guaranteed safe against truncation until we
8911 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
8913 struct page
*page
= vmf
->page
;
8914 struct inode
*inode
= file_inode(vma
->vm_file
);
8915 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8916 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
8917 struct btrfs_ordered_extent
*ordered
;
8918 struct extent_state
*cached_state
= NULL
;
8920 unsigned long zero_start
;
8929 reserved_space
= PAGE_SIZE
;
8931 sb_start_pagefault(inode
->i_sb
);
8932 page_start
= page_offset(page
);
8933 page_end
= page_start
+ PAGE_SIZE
- 1;
8937 * Reserving delalloc space after obtaining the page lock can lead to
8938 * deadlock. For example, if a dirty page is locked by this function
8939 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8940 * dirty page write out, then the btrfs_writepage() function could
8941 * end up waiting indefinitely to get a lock on the page currently
8942 * being processed by btrfs_page_mkwrite() function.
8944 ret
= btrfs_delalloc_reserve_space(inode
, page_start
,
8947 ret
= file_update_time(vma
->vm_file
);
8953 else /* -ENOSPC, -EIO, etc */
8954 ret
= VM_FAULT_SIGBUS
;
8960 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
8963 size
= i_size_read(inode
);
8965 if ((page
->mapping
!= inode
->i_mapping
) ||
8966 (page_start
>= size
)) {
8967 /* page got truncated out from underneath us */
8970 wait_on_page_writeback(page
);
8972 lock_extent_bits(io_tree
, page_start
, page_end
, &cached_state
);
8973 set_page_extent_mapped(page
);
8976 * we can't set the delalloc bits if there are pending ordered
8977 * extents. Drop our locks and wait for them to finish
8979 ordered
= btrfs_lookup_ordered_range(BTRFS_I(inode
), page_start
,
8982 unlock_extent_cached(io_tree
, page_start
, page_end
,
8983 &cached_state
, GFP_NOFS
);
8985 btrfs_start_ordered_extent(inode
, ordered
, 1);
8986 btrfs_put_ordered_extent(ordered
);
8990 if (page
->index
== ((size
- 1) >> PAGE_SHIFT
)) {
8991 reserved_space
= round_up(size
- page_start
,
8992 fs_info
->sectorsize
);
8993 if (reserved_space
< PAGE_SIZE
) {
8994 end
= page_start
+ reserved_space
- 1;
8995 spin_lock(&BTRFS_I(inode
)->lock
);
8996 BTRFS_I(inode
)->outstanding_extents
++;
8997 spin_unlock(&BTRFS_I(inode
)->lock
);
8998 btrfs_delalloc_release_space(inode
, page_start
,
8999 PAGE_SIZE
- reserved_space
);
9004 * page_mkwrite gets called when the page is firstly dirtied after it's
9005 * faulted in, but write(2) could also dirty a page and set delalloc
9006 * bits, thus in this case for space account reason, we still need to
9007 * clear any delalloc bits within this page range since we have to
9008 * reserve data&meta space before lock_page() (see above comments).
9010 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, end
,
9011 EXTENT_DIRTY
| EXTENT_DELALLOC
|
9012 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
9013 0, 0, &cached_state
, GFP_NOFS
);
9015 ret
= btrfs_set_extent_delalloc(inode
, page_start
, end
,
9018 unlock_extent_cached(io_tree
, page_start
, page_end
,
9019 &cached_state
, GFP_NOFS
);
9020 ret
= VM_FAULT_SIGBUS
;
9025 /* page is wholly or partially inside EOF */
9026 if (page_start
+ PAGE_SIZE
> size
)
9027 zero_start
= size
& ~PAGE_MASK
;
9029 zero_start
= PAGE_SIZE
;
9031 if (zero_start
!= PAGE_SIZE
) {
9033 memset(kaddr
+ zero_start
, 0, PAGE_SIZE
- zero_start
);
9034 flush_dcache_page(page
);
9037 ClearPageChecked(page
);
9038 set_page_dirty(page
);
9039 SetPageUptodate(page
);
9041 BTRFS_I(inode
)->last_trans
= fs_info
->generation
;
9042 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
9043 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
9045 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
9049 sb_end_pagefault(inode
->i_sb
);
9050 return VM_FAULT_LOCKED
;
9054 btrfs_delalloc_release_space(inode
, page_start
, reserved_space
);
9056 sb_end_pagefault(inode
->i_sb
);
9060 static int btrfs_truncate(struct inode
*inode
)
9062 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
9063 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9064 struct btrfs_block_rsv
*rsv
;
9067 struct btrfs_trans_handle
*trans
;
9068 u64 mask
= fs_info
->sectorsize
- 1;
9069 u64 min_size
= btrfs_calc_trunc_metadata_size(fs_info
, 1);
9071 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
9077 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
9078 * 3 things going on here
9080 * 1) We need to reserve space for our orphan item and the space to
9081 * delete our orphan item. Lord knows we don't want to have a dangling
9082 * orphan item because we didn't reserve space to remove it.
9084 * 2) We need to reserve space to update our inode.
9086 * 3) We need to have something to cache all the space that is going to
9087 * be free'd up by the truncate operation, but also have some slack
9088 * space reserved in case it uses space during the truncate (thank you
9089 * very much snapshotting).
9091 * And we need these to all be separate. The fact is we can use a lot of
9092 * space doing the truncate, and we have no earthly idea how much space
9093 * we will use, so we need the truncate reservation to be separate so it
9094 * doesn't end up using space reserved for updating the inode or
9095 * removing the orphan item. We also need to be able to stop the
9096 * transaction and start a new one, which means we need to be able to
9097 * update the inode several times, and we have no idea of knowing how
9098 * many times that will be, so we can't just reserve 1 item for the
9099 * entirety of the operation, so that has to be done separately as well.
9100 * Then there is the orphan item, which does indeed need to be held on
9101 * to for the whole operation, and we need nobody to touch this reserved
9102 * space except the orphan code.
9104 * So that leaves us with
9106 * 1) root->orphan_block_rsv - for the orphan deletion.
9107 * 2) rsv - for the truncate reservation, which we will steal from the
9108 * transaction reservation.
9109 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9110 * updating the inode.
9112 rsv
= btrfs_alloc_block_rsv(fs_info
, BTRFS_BLOCK_RSV_TEMP
);
9115 rsv
->size
= min_size
;
9119 * 1 for the truncate slack space
9120 * 1 for updating the inode.
9122 trans
= btrfs_start_transaction(root
, 2);
9123 if (IS_ERR(trans
)) {
9124 err
= PTR_ERR(trans
);
9128 /* Migrate the slack space for the truncate to our reserve */
9129 ret
= btrfs_block_rsv_migrate(&fs_info
->trans_block_rsv
, rsv
,
9134 * So if we truncate and then write and fsync we normally would just
9135 * write the extents that changed, which is a problem if we need to
9136 * first truncate that entire inode. So set this flag so we write out
9137 * all of the extents in the inode to the sync log so we're completely
9140 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
9141 trans
->block_rsv
= rsv
;
9144 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
9146 BTRFS_EXTENT_DATA_KEY
);
9147 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
) {
9152 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
9153 ret
= btrfs_update_inode(trans
, root
, inode
);
9159 btrfs_end_transaction(trans
);
9160 btrfs_btree_balance_dirty(fs_info
);
9162 trans
= btrfs_start_transaction(root
, 2);
9163 if (IS_ERR(trans
)) {
9164 ret
= err
= PTR_ERR(trans
);
9169 btrfs_block_rsv_release(fs_info
, rsv
, -1);
9170 ret
= btrfs_block_rsv_migrate(&fs_info
->trans_block_rsv
,
9172 BUG_ON(ret
); /* shouldn't happen */
9173 trans
->block_rsv
= rsv
;
9176 if (ret
== 0 && inode
->i_nlink
> 0) {
9177 trans
->block_rsv
= root
->orphan_block_rsv
;
9178 ret
= btrfs_orphan_del(trans
, BTRFS_I(inode
));
9184 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
9185 ret
= btrfs_update_inode(trans
, root
, inode
);
9189 ret
= btrfs_end_transaction(trans
);
9190 btrfs_btree_balance_dirty(fs_info
);
9193 btrfs_free_block_rsv(fs_info
, rsv
);
9202 * create a new subvolume directory/inode (helper for the ioctl).
9204 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
9205 struct btrfs_root
*new_root
,
9206 struct btrfs_root
*parent_root
,
9209 struct inode
*inode
;
9213 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
9214 new_dirid
, new_dirid
,
9215 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
9218 return PTR_ERR(inode
);
9219 inode
->i_op
= &btrfs_dir_inode_operations
;
9220 inode
->i_fop
= &btrfs_dir_file_operations
;
9222 set_nlink(inode
, 1);
9223 btrfs_i_size_write(BTRFS_I(inode
), 0);
9224 unlock_new_inode(inode
);
9226 err
= btrfs_subvol_inherit_props(trans
, new_root
, parent_root
);
9228 btrfs_err(new_root
->fs_info
,
9229 "error inheriting subvolume %llu properties: %d",
9230 new_root
->root_key
.objectid
, err
);
9232 err
= btrfs_update_inode(trans
, new_root
, inode
);
9238 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
9240 struct btrfs_inode
*ei
;
9241 struct inode
*inode
;
9243 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
9250 ei
->last_sub_trans
= 0;
9251 ei
->logged_trans
= 0;
9252 ei
->delalloc_bytes
= 0;
9253 ei
->defrag_bytes
= 0;
9254 ei
->disk_i_size
= 0;
9257 ei
->index_cnt
= (u64
)-1;
9259 ei
->last_unlink_trans
= 0;
9260 ei
->last_log_commit
= 0;
9261 ei
->delayed_iput_count
= 0;
9263 spin_lock_init(&ei
->lock
);
9264 ei
->outstanding_extents
= 0;
9265 ei
->reserved_extents
= 0;
9267 ei
->runtime_flags
= 0;
9268 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
9270 ei
->delayed_node
= NULL
;
9272 ei
->i_otime
.tv_sec
= 0;
9273 ei
->i_otime
.tv_nsec
= 0;
9275 inode
= &ei
->vfs_inode
;
9276 extent_map_tree_init(&ei
->extent_tree
);
9277 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
9278 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
9279 ei
->io_tree
.track_uptodate
= 1;
9280 ei
->io_failure_tree
.track_uptodate
= 1;
9281 atomic_set(&ei
->sync_writers
, 0);
9282 mutex_init(&ei
->log_mutex
);
9283 mutex_init(&ei
->delalloc_mutex
);
9284 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
9285 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
9286 INIT_LIST_HEAD(&ei
->delayed_iput
);
9287 RB_CLEAR_NODE(&ei
->rb_node
);
9288 init_rwsem(&ei
->dio_sem
);
9293 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9294 void btrfs_test_destroy_inode(struct inode
*inode
)
9296 btrfs_drop_extent_cache(BTRFS_I(inode
), 0, (u64
)-1, 0);
9297 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9301 static void btrfs_i_callback(struct rcu_head
*head
)
9303 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
9304 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9307 void btrfs_destroy_inode(struct inode
*inode
)
9309 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
9310 struct btrfs_ordered_extent
*ordered
;
9311 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9313 WARN_ON(!hlist_empty(&inode
->i_dentry
));
9314 WARN_ON(inode
->i_data
.nrpages
);
9315 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
9316 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
9317 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
9318 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
9319 WARN_ON(BTRFS_I(inode
)->defrag_bytes
);
9322 * This can happen where we create an inode, but somebody else also
9323 * created the same inode and we need to destroy the one we already
9329 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
9330 &BTRFS_I(inode
)->runtime_flags
)) {
9331 btrfs_info(fs_info
, "inode %llu still on the orphan list",
9332 btrfs_ino(BTRFS_I(inode
)));
9333 atomic_dec(&root
->orphan_inodes
);
9337 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
9342 "found ordered extent %llu %llu on inode cleanup",
9343 ordered
->file_offset
, ordered
->len
);
9344 btrfs_remove_ordered_extent(inode
, ordered
);
9345 btrfs_put_ordered_extent(ordered
);
9346 btrfs_put_ordered_extent(ordered
);
9349 btrfs_qgroup_check_reserved_leak(inode
);
9350 inode_tree_del(inode
);
9351 btrfs_drop_extent_cache(BTRFS_I(inode
), 0, (u64
)-1, 0);
9353 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
9356 int btrfs_drop_inode(struct inode
*inode
)
9358 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9363 /* the snap/subvol tree is on deleting */
9364 if (btrfs_root_refs(&root
->root_item
) == 0)
9367 return generic_drop_inode(inode
);
9370 static void init_once(void *foo
)
9372 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
9374 inode_init_once(&ei
->vfs_inode
);
9377 void btrfs_destroy_cachep(void)
9380 * Make sure all delayed rcu free inodes are flushed before we
9384 kmem_cache_destroy(btrfs_inode_cachep
);
9385 kmem_cache_destroy(btrfs_trans_handle_cachep
);
9386 kmem_cache_destroy(btrfs_transaction_cachep
);
9387 kmem_cache_destroy(btrfs_path_cachep
);
9388 kmem_cache_destroy(btrfs_free_space_cachep
);
9391 int btrfs_init_cachep(void)
9393 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
9394 sizeof(struct btrfs_inode
), 0,
9395 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
| SLAB_ACCOUNT
,
9397 if (!btrfs_inode_cachep
)
9400 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
9401 sizeof(struct btrfs_trans_handle
), 0,
9402 SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
9403 if (!btrfs_trans_handle_cachep
)
9406 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
9407 sizeof(struct btrfs_transaction
), 0,
9408 SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
9409 if (!btrfs_transaction_cachep
)
9412 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
9413 sizeof(struct btrfs_path
), 0,
9414 SLAB_MEM_SPREAD
, NULL
);
9415 if (!btrfs_path_cachep
)
9418 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
9419 sizeof(struct btrfs_free_space
), 0,
9420 SLAB_MEM_SPREAD
, NULL
);
9421 if (!btrfs_free_space_cachep
)
9426 btrfs_destroy_cachep();
9430 static int btrfs_getattr(struct vfsmount
*mnt
,
9431 struct dentry
*dentry
, struct kstat
*stat
)
9434 struct inode
*inode
= d_inode(dentry
);
9435 u32 blocksize
= inode
->i_sb
->s_blocksize
;
9437 generic_fillattr(inode
, stat
);
9438 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
9440 spin_lock(&BTRFS_I(inode
)->lock
);
9441 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
9442 spin_unlock(&BTRFS_I(inode
)->lock
);
9443 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
9444 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
9448 static int btrfs_rename_exchange(struct inode
*old_dir
,
9449 struct dentry
*old_dentry
,
9450 struct inode
*new_dir
,
9451 struct dentry
*new_dentry
)
9453 struct btrfs_fs_info
*fs_info
= btrfs_sb(old_dir
->i_sb
);
9454 struct btrfs_trans_handle
*trans
;
9455 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
9456 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
9457 struct inode
*new_inode
= new_dentry
->d_inode
;
9458 struct inode
*old_inode
= old_dentry
->d_inode
;
9459 struct timespec ctime
= current_time(old_inode
);
9460 struct dentry
*parent
;
9461 u64 old_ino
= btrfs_ino(BTRFS_I(old_inode
));
9462 u64 new_ino
= btrfs_ino(BTRFS_I(new_inode
));
9467 bool root_log_pinned
= false;
9468 bool dest_log_pinned
= false;
9470 /* we only allow rename subvolume link between subvolumes */
9471 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
9474 /* close the race window with snapshot create/destroy ioctl */
9475 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9476 down_read(&fs_info
->subvol_sem
);
9477 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9478 down_read(&fs_info
->subvol_sem
);
9481 * We want to reserve the absolute worst case amount of items. So if
9482 * both inodes are subvols and we need to unlink them then that would
9483 * require 4 item modifications, but if they are both normal inodes it
9484 * would require 5 item modifications, so we'll assume their normal
9485 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9486 * should cover the worst case number of items we'll modify.
9488 trans
= btrfs_start_transaction(root
, 12);
9489 if (IS_ERR(trans
)) {
9490 ret
= PTR_ERR(trans
);
9495 * We need to find a free sequence number both in the source and
9496 * in the destination directory for the exchange.
9498 ret
= btrfs_set_inode_index(BTRFS_I(new_dir
), &old_idx
);
9501 ret
= btrfs_set_inode_index(BTRFS_I(old_dir
), &new_idx
);
9505 BTRFS_I(old_inode
)->dir_index
= 0ULL;
9506 BTRFS_I(new_inode
)->dir_index
= 0ULL;
9508 /* Reference for the source. */
9509 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9510 /* force full log commit if subvolume involved. */
9511 btrfs_set_log_full_commit(fs_info
, trans
);
9513 btrfs_pin_log_trans(root
);
9514 root_log_pinned
= true;
9515 ret
= btrfs_insert_inode_ref(trans
, dest
,
9516 new_dentry
->d_name
.name
,
9517 new_dentry
->d_name
.len
,
9519 btrfs_ino(BTRFS_I(new_dir
)),
9525 /* And now for the dest. */
9526 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9527 /* force full log commit if subvolume involved. */
9528 btrfs_set_log_full_commit(fs_info
, trans
);
9530 btrfs_pin_log_trans(dest
);
9531 dest_log_pinned
= true;
9532 ret
= btrfs_insert_inode_ref(trans
, root
,
9533 old_dentry
->d_name
.name
,
9534 old_dentry
->d_name
.len
,
9536 btrfs_ino(BTRFS_I(old_dir
)),
9542 /* Update inode version and ctime/mtime. */
9543 inode_inc_iversion(old_dir
);
9544 inode_inc_iversion(new_dir
);
9545 inode_inc_iversion(old_inode
);
9546 inode_inc_iversion(new_inode
);
9547 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
9548 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
9549 old_inode
->i_ctime
= ctime
;
9550 new_inode
->i_ctime
= ctime
;
9552 if (old_dentry
->d_parent
!= new_dentry
->d_parent
) {
9553 btrfs_record_unlink_dir(trans
, BTRFS_I(old_dir
),
9554 BTRFS_I(old_inode
), 1);
9555 btrfs_record_unlink_dir(trans
, BTRFS_I(new_dir
),
9556 BTRFS_I(new_inode
), 1);
9559 /* src is a subvolume */
9560 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9561 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
9562 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
,
9564 old_dentry
->d_name
.name
,
9565 old_dentry
->d_name
.len
);
9566 } else { /* src is an inode */
9567 ret
= __btrfs_unlink_inode(trans
, root
, BTRFS_I(old_dir
),
9568 BTRFS_I(old_dentry
->d_inode
),
9569 old_dentry
->d_name
.name
,
9570 old_dentry
->d_name
.len
);
9572 ret
= btrfs_update_inode(trans
, root
, old_inode
);
9575 btrfs_abort_transaction(trans
, ret
);
9579 /* dest is a subvolume */
9580 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9581 root_objectid
= BTRFS_I(new_inode
)->root
->root_key
.objectid
;
9582 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
9584 new_dentry
->d_name
.name
,
9585 new_dentry
->d_name
.len
);
9586 } else { /* dest is an inode */
9587 ret
= __btrfs_unlink_inode(trans
, dest
, BTRFS_I(new_dir
),
9588 BTRFS_I(new_dentry
->d_inode
),
9589 new_dentry
->d_name
.name
,
9590 new_dentry
->d_name
.len
);
9592 ret
= btrfs_update_inode(trans
, dest
, new_inode
);
9595 btrfs_abort_transaction(trans
, ret
);
9599 ret
= btrfs_add_link(trans
, BTRFS_I(new_dir
), BTRFS_I(old_inode
),
9600 new_dentry
->d_name
.name
,
9601 new_dentry
->d_name
.len
, 0, old_idx
);
9603 btrfs_abort_transaction(trans
, ret
);
9607 ret
= btrfs_add_link(trans
, BTRFS_I(old_dir
), BTRFS_I(new_inode
),
9608 old_dentry
->d_name
.name
,
9609 old_dentry
->d_name
.len
, 0, new_idx
);
9611 btrfs_abort_transaction(trans
, ret
);
9615 if (old_inode
->i_nlink
== 1)
9616 BTRFS_I(old_inode
)->dir_index
= old_idx
;
9617 if (new_inode
->i_nlink
== 1)
9618 BTRFS_I(new_inode
)->dir_index
= new_idx
;
9620 if (root_log_pinned
) {
9621 parent
= new_dentry
->d_parent
;
9622 btrfs_log_new_name(trans
, BTRFS_I(old_inode
), BTRFS_I(old_dir
),
9624 btrfs_end_log_trans(root
);
9625 root_log_pinned
= false;
9627 if (dest_log_pinned
) {
9628 parent
= old_dentry
->d_parent
;
9629 btrfs_log_new_name(trans
, BTRFS_I(new_inode
), BTRFS_I(new_dir
),
9631 btrfs_end_log_trans(dest
);
9632 dest_log_pinned
= false;
9636 * If we have pinned a log and an error happened, we unpin tasks
9637 * trying to sync the log and force them to fallback to a transaction
9638 * commit if the log currently contains any of the inodes involved in
9639 * this rename operation (to ensure we do not persist a log with an
9640 * inconsistent state for any of these inodes or leading to any
9641 * inconsistencies when replayed). If the transaction was aborted, the
9642 * abortion reason is propagated to userspace when attempting to commit
9643 * the transaction. If the log does not contain any of these inodes, we
9644 * allow the tasks to sync it.
9646 if (ret
&& (root_log_pinned
|| dest_log_pinned
)) {
9647 if (btrfs_inode_in_log(BTRFS_I(old_dir
), fs_info
->generation
) ||
9648 btrfs_inode_in_log(BTRFS_I(new_dir
), fs_info
->generation
) ||
9649 btrfs_inode_in_log(BTRFS_I(old_inode
), fs_info
->generation
) ||
9651 btrfs_inode_in_log(BTRFS_I(new_inode
), fs_info
->generation
)))
9652 btrfs_set_log_full_commit(fs_info
, trans
);
9654 if (root_log_pinned
) {
9655 btrfs_end_log_trans(root
);
9656 root_log_pinned
= false;
9658 if (dest_log_pinned
) {
9659 btrfs_end_log_trans(dest
);
9660 dest_log_pinned
= false;
9663 ret
= btrfs_end_transaction(trans
);
9665 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9666 up_read(&fs_info
->subvol_sem
);
9667 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9668 up_read(&fs_info
->subvol_sem
);
9673 static int btrfs_whiteout_for_rename(struct btrfs_trans_handle
*trans
,
9674 struct btrfs_root
*root
,
9676 struct dentry
*dentry
)
9679 struct inode
*inode
;
9683 ret
= btrfs_find_free_ino(root
, &objectid
);
9687 inode
= btrfs_new_inode(trans
, root
, dir
,
9688 dentry
->d_name
.name
,
9690 btrfs_ino(BTRFS_I(dir
)),
9692 S_IFCHR
| WHITEOUT_MODE
,
9695 if (IS_ERR(inode
)) {
9696 ret
= PTR_ERR(inode
);
9700 inode
->i_op
= &btrfs_special_inode_operations
;
9701 init_special_inode(inode
, inode
->i_mode
,
9704 ret
= btrfs_init_inode_security(trans
, inode
, dir
,
9709 ret
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
,
9710 BTRFS_I(inode
), 0, index
);
9714 ret
= btrfs_update_inode(trans
, root
, inode
);
9716 unlock_new_inode(inode
);
9718 inode_dec_link_count(inode
);
9724 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
9725 struct inode
*new_dir
, struct dentry
*new_dentry
,
9728 struct btrfs_fs_info
*fs_info
= btrfs_sb(old_dir
->i_sb
);
9729 struct btrfs_trans_handle
*trans
;
9730 unsigned int trans_num_items
;
9731 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
9732 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
9733 struct inode
*new_inode
= d_inode(new_dentry
);
9734 struct inode
*old_inode
= d_inode(old_dentry
);
9738 u64 old_ino
= btrfs_ino(BTRFS_I(old_inode
));
9739 bool log_pinned
= false;
9741 if (btrfs_ino(BTRFS_I(new_dir
)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
9744 /* we only allow rename subvolume link between subvolumes */
9745 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
9748 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
9749 (new_inode
&& btrfs_ino(BTRFS_I(new_inode
)) == BTRFS_FIRST_FREE_OBJECTID
))
9752 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
9753 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
9757 /* check for collisions, even if the name isn't there */
9758 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
9759 new_dentry
->d_name
.name
,
9760 new_dentry
->d_name
.len
);
9763 if (ret
== -EEXIST
) {
9765 * eexist without a new_inode */
9766 if (WARN_ON(!new_inode
)) {
9770 /* maybe -EOVERFLOW */
9777 * we're using rename to replace one file with another. Start IO on it
9778 * now so we don't add too much work to the end of the transaction
9780 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
)
9781 filemap_flush(old_inode
->i_mapping
);
9783 /* close the racy window with snapshot create/destroy ioctl */
9784 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9785 down_read(&fs_info
->subvol_sem
);
9787 * We want to reserve the absolute worst case amount of items. So if
9788 * both inodes are subvols and we need to unlink them then that would
9789 * require 4 item modifications, but if they are both normal inodes it
9790 * would require 5 item modifications, so we'll assume they are normal
9791 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9792 * should cover the worst case number of items we'll modify.
9793 * If our rename has the whiteout flag, we need more 5 units for the
9794 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9795 * when selinux is enabled).
9797 trans_num_items
= 11;
9798 if (flags
& RENAME_WHITEOUT
)
9799 trans_num_items
+= 5;
9800 trans
= btrfs_start_transaction(root
, trans_num_items
);
9801 if (IS_ERR(trans
)) {
9802 ret
= PTR_ERR(trans
);
9807 btrfs_record_root_in_trans(trans
, dest
);
9809 ret
= btrfs_set_inode_index(BTRFS_I(new_dir
), &index
);
9813 BTRFS_I(old_inode
)->dir_index
= 0ULL;
9814 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9815 /* force full log commit if subvolume involved. */
9816 btrfs_set_log_full_commit(fs_info
, trans
);
9818 btrfs_pin_log_trans(root
);
9820 ret
= btrfs_insert_inode_ref(trans
, dest
,
9821 new_dentry
->d_name
.name
,
9822 new_dentry
->d_name
.len
,
9824 btrfs_ino(BTRFS_I(new_dir
)), index
);
9829 inode_inc_iversion(old_dir
);
9830 inode_inc_iversion(new_dir
);
9831 inode_inc_iversion(old_inode
);
9832 old_dir
->i_ctime
= old_dir
->i_mtime
=
9833 new_dir
->i_ctime
= new_dir
->i_mtime
=
9834 old_inode
->i_ctime
= current_time(old_dir
);
9836 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
9837 btrfs_record_unlink_dir(trans
, BTRFS_I(old_dir
),
9838 BTRFS_I(old_inode
), 1);
9840 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9841 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
9842 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
9843 old_dentry
->d_name
.name
,
9844 old_dentry
->d_name
.len
);
9846 ret
= __btrfs_unlink_inode(trans
, root
, BTRFS_I(old_dir
),
9847 BTRFS_I(d_inode(old_dentry
)),
9848 old_dentry
->d_name
.name
,
9849 old_dentry
->d_name
.len
);
9851 ret
= btrfs_update_inode(trans
, root
, old_inode
);
9854 btrfs_abort_transaction(trans
, ret
);
9859 inode_inc_iversion(new_inode
);
9860 new_inode
->i_ctime
= current_time(new_inode
);
9861 if (unlikely(btrfs_ino(BTRFS_I(new_inode
)) ==
9862 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
9863 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
9864 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
9866 new_dentry
->d_name
.name
,
9867 new_dentry
->d_name
.len
);
9868 BUG_ON(new_inode
->i_nlink
== 0);
9870 ret
= btrfs_unlink_inode(trans
, dest
, BTRFS_I(new_dir
),
9871 BTRFS_I(d_inode(new_dentry
)),
9872 new_dentry
->d_name
.name
,
9873 new_dentry
->d_name
.len
);
9875 if (!ret
&& new_inode
->i_nlink
== 0)
9876 ret
= btrfs_orphan_add(trans
,
9877 BTRFS_I(d_inode(new_dentry
)));
9879 btrfs_abort_transaction(trans
, ret
);
9884 ret
= btrfs_add_link(trans
, BTRFS_I(new_dir
), BTRFS_I(old_inode
),
9885 new_dentry
->d_name
.name
,
9886 new_dentry
->d_name
.len
, 0, index
);
9888 btrfs_abort_transaction(trans
, ret
);
9892 if (old_inode
->i_nlink
== 1)
9893 BTRFS_I(old_inode
)->dir_index
= index
;
9896 struct dentry
*parent
= new_dentry
->d_parent
;
9898 btrfs_log_new_name(trans
, BTRFS_I(old_inode
), BTRFS_I(old_dir
),
9900 btrfs_end_log_trans(root
);
9904 if (flags
& RENAME_WHITEOUT
) {
9905 ret
= btrfs_whiteout_for_rename(trans
, root
, old_dir
,
9909 btrfs_abort_transaction(trans
, ret
);
9915 * If we have pinned the log and an error happened, we unpin tasks
9916 * trying to sync the log and force them to fallback to a transaction
9917 * commit if the log currently contains any of the inodes involved in
9918 * this rename operation (to ensure we do not persist a log with an
9919 * inconsistent state for any of these inodes or leading to any
9920 * inconsistencies when replayed). If the transaction was aborted, the
9921 * abortion reason is propagated to userspace when attempting to commit
9922 * the transaction. If the log does not contain any of these inodes, we
9923 * allow the tasks to sync it.
9925 if (ret
&& log_pinned
) {
9926 if (btrfs_inode_in_log(BTRFS_I(old_dir
), fs_info
->generation
) ||
9927 btrfs_inode_in_log(BTRFS_I(new_dir
), fs_info
->generation
) ||
9928 btrfs_inode_in_log(BTRFS_I(old_inode
), fs_info
->generation
) ||
9930 btrfs_inode_in_log(BTRFS_I(new_inode
), fs_info
->generation
)))
9931 btrfs_set_log_full_commit(fs_info
, trans
);
9933 btrfs_end_log_trans(root
);
9936 btrfs_end_transaction(trans
);
9938 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9939 up_read(&fs_info
->subvol_sem
);
9944 static int btrfs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
9945 struct inode
*new_dir
, struct dentry
*new_dentry
,
9948 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
9951 if (flags
& RENAME_EXCHANGE
)
9952 return btrfs_rename_exchange(old_dir
, old_dentry
, new_dir
,
9955 return btrfs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
9958 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
9960 struct btrfs_delalloc_work
*delalloc_work
;
9961 struct inode
*inode
;
9963 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
9965 inode
= delalloc_work
->inode
;
9966 filemap_flush(inode
->i_mapping
);
9967 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
9968 &BTRFS_I(inode
)->runtime_flags
))
9969 filemap_flush(inode
->i_mapping
);
9971 if (delalloc_work
->delay_iput
)
9972 btrfs_add_delayed_iput(inode
);
9975 complete(&delalloc_work
->completion
);
9978 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
9981 struct btrfs_delalloc_work
*work
;
9983 work
= kmalloc(sizeof(*work
), GFP_NOFS
);
9987 init_completion(&work
->completion
);
9988 INIT_LIST_HEAD(&work
->list
);
9989 work
->inode
= inode
;
9990 work
->delay_iput
= delay_iput
;
9991 WARN_ON_ONCE(!inode
);
9992 btrfs_init_work(&work
->work
, btrfs_flush_delalloc_helper
,
9993 btrfs_run_delalloc_work
, NULL
, NULL
);
9998 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
10000 wait_for_completion(&work
->completion
);
10005 * some fairly slow code that needs optimization. This walks the list
10006 * of all the inodes with pending delalloc and forces them to disk.
10008 static int __start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
,
10011 struct btrfs_inode
*binode
;
10012 struct inode
*inode
;
10013 struct btrfs_delalloc_work
*work
, *next
;
10014 struct list_head works
;
10015 struct list_head splice
;
10018 INIT_LIST_HEAD(&works
);
10019 INIT_LIST_HEAD(&splice
);
10021 mutex_lock(&root
->delalloc_mutex
);
10022 spin_lock(&root
->delalloc_lock
);
10023 list_splice_init(&root
->delalloc_inodes
, &splice
);
10024 while (!list_empty(&splice
)) {
10025 binode
= list_entry(splice
.next
, struct btrfs_inode
,
10028 list_move_tail(&binode
->delalloc_inodes
,
10029 &root
->delalloc_inodes
);
10030 inode
= igrab(&binode
->vfs_inode
);
10032 cond_resched_lock(&root
->delalloc_lock
);
10035 spin_unlock(&root
->delalloc_lock
);
10037 work
= btrfs_alloc_delalloc_work(inode
, delay_iput
);
10040 btrfs_add_delayed_iput(inode
);
10046 list_add_tail(&work
->list
, &works
);
10047 btrfs_queue_work(root
->fs_info
->flush_workers
,
10050 if (nr
!= -1 && ret
>= nr
)
10053 spin_lock(&root
->delalloc_lock
);
10055 spin_unlock(&root
->delalloc_lock
);
10058 list_for_each_entry_safe(work
, next
, &works
, list
) {
10059 list_del_init(&work
->list
);
10060 btrfs_wait_and_free_delalloc_work(work
);
10063 if (!list_empty_careful(&splice
)) {
10064 spin_lock(&root
->delalloc_lock
);
10065 list_splice_tail(&splice
, &root
->delalloc_inodes
);
10066 spin_unlock(&root
->delalloc_lock
);
10068 mutex_unlock(&root
->delalloc_mutex
);
10072 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
10074 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
10077 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
10080 ret
= __start_delalloc_inodes(root
, delay_iput
, -1);
10084 * the filemap_flush will queue IO into the worker threads, but
10085 * we have to make sure the IO is actually started and that
10086 * ordered extents get created before we return
10088 atomic_inc(&fs_info
->async_submit_draining
);
10089 while (atomic_read(&fs_info
->nr_async_submits
) ||
10090 atomic_read(&fs_info
->async_delalloc_pages
)) {
10091 wait_event(fs_info
->async_submit_wait
,
10092 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
10093 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
10095 atomic_dec(&fs_info
->async_submit_draining
);
10099 int btrfs_start_delalloc_roots(struct btrfs_fs_info
*fs_info
, int delay_iput
,
10102 struct btrfs_root
*root
;
10103 struct list_head splice
;
10106 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
10109 INIT_LIST_HEAD(&splice
);
10111 mutex_lock(&fs_info
->delalloc_root_mutex
);
10112 spin_lock(&fs_info
->delalloc_root_lock
);
10113 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
10114 while (!list_empty(&splice
) && nr
) {
10115 root
= list_first_entry(&splice
, struct btrfs_root
,
10117 root
= btrfs_grab_fs_root(root
);
10119 list_move_tail(&root
->delalloc_root
,
10120 &fs_info
->delalloc_roots
);
10121 spin_unlock(&fs_info
->delalloc_root_lock
);
10123 ret
= __start_delalloc_inodes(root
, delay_iput
, nr
);
10124 btrfs_put_fs_root(root
);
10132 spin_lock(&fs_info
->delalloc_root_lock
);
10134 spin_unlock(&fs_info
->delalloc_root_lock
);
10137 atomic_inc(&fs_info
->async_submit_draining
);
10138 while (atomic_read(&fs_info
->nr_async_submits
) ||
10139 atomic_read(&fs_info
->async_delalloc_pages
)) {
10140 wait_event(fs_info
->async_submit_wait
,
10141 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
10142 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
10144 atomic_dec(&fs_info
->async_submit_draining
);
10146 if (!list_empty_careful(&splice
)) {
10147 spin_lock(&fs_info
->delalloc_root_lock
);
10148 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
10149 spin_unlock(&fs_info
->delalloc_root_lock
);
10151 mutex_unlock(&fs_info
->delalloc_root_mutex
);
10155 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
10156 const char *symname
)
10158 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
10159 struct btrfs_trans_handle
*trans
;
10160 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
10161 struct btrfs_path
*path
;
10162 struct btrfs_key key
;
10163 struct inode
*inode
= NULL
;
10165 int drop_inode
= 0;
10171 struct btrfs_file_extent_item
*ei
;
10172 struct extent_buffer
*leaf
;
10174 name_len
= strlen(symname
);
10175 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(fs_info
))
10176 return -ENAMETOOLONG
;
10179 * 2 items for inode item and ref
10180 * 2 items for dir items
10181 * 1 item for updating parent inode item
10182 * 1 item for the inline extent item
10183 * 1 item for xattr if selinux is on
10185 trans
= btrfs_start_transaction(root
, 7);
10187 return PTR_ERR(trans
);
10189 err
= btrfs_find_free_ino(root
, &objectid
);
10193 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
10194 dentry
->d_name
.len
, btrfs_ino(BTRFS_I(dir
)),
10195 objectid
, S_IFLNK
|S_IRWXUGO
, &index
);
10196 if (IS_ERR(inode
)) {
10197 err
= PTR_ERR(inode
);
10202 * If the active LSM wants to access the inode during
10203 * d_instantiate it needs these. Smack checks to see
10204 * if the filesystem supports xattrs by looking at the
10207 inode
->i_fop
= &btrfs_file_operations
;
10208 inode
->i_op
= &btrfs_file_inode_operations
;
10209 inode
->i_mapping
->a_ops
= &btrfs_aops
;
10210 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
10212 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
10214 goto out_unlock_inode
;
10216 path
= btrfs_alloc_path();
10219 goto out_unlock_inode
;
10221 key
.objectid
= btrfs_ino(BTRFS_I(inode
));
10223 key
.type
= BTRFS_EXTENT_DATA_KEY
;
10224 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
10225 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
10228 btrfs_free_path(path
);
10229 goto out_unlock_inode
;
10231 leaf
= path
->nodes
[0];
10232 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
10233 struct btrfs_file_extent_item
);
10234 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
10235 btrfs_set_file_extent_type(leaf
, ei
,
10236 BTRFS_FILE_EXTENT_INLINE
);
10237 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
10238 btrfs_set_file_extent_compression(leaf
, ei
, 0);
10239 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
10240 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
10242 ptr
= btrfs_file_extent_inline_start(ei
);
10243 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
10244 btrfs_mark_buffer_dirty(leaf
);
10245 btrfs_free_path(path
);
10247 inode
->i_op
= &btrfs_symlink_inode_operations
;
10248 inode_nohighmem(inode
);
10249 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
10250 inode_set_bytes(inode
, name_len
);
10251 btrfs_i_size_write(BTRFS_I(inode
), name_len
);
10252 err
= btrfs_update_inode(trans
, root
, inode
);
10254 * Last step, add directory indexes for our symlink inode. This is the
10255 * last step to avoid extra cleanup of these indexes if an error happens
10259 err
= btrfs_add_nondir(trans
, BTRFS_I(dir
), dentry
,
10260 BTRFS_I(inode
), 0, index
);
10263 goto out_unlock_inode
;
10266 unlock_new_inode(inode
);
10267 d_instantiate(dentry
, inode
);
10270 btrfs_end_transaction(trans
);
10272 inode_dec_link_count(inode
);
10275 btrfs_btree_balance_dirty(fs_info
);
10280 unlock_new_inode(inode
);
10284 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
10285 u64 start
, u64 num_bytes
, u64 min_size
,
10286 loff_t actual_len
, u64
*alloc_hint
,
10287 struct btrfs_trans_handle
*trans
)
10289 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
10290 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
10291 struct extent_map
*em
;
10292 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
10293 struct btrfs_key ins
;
10294 u64 cur_offset
= start
;
10297 u64 last_alloc
= (u64
)-1;
10299 bool own_trans
= true;
10300 u64 end
= start
+ num_bytes
- 1;
10304 while (num_bytes
> 0) {
10306 trans
= btrfs_start_transaction(root
, 3);
10307 if (IS_ERR(trans
)) {
10308 ret
= PTR_ERR(trans
);
10313 cur_bytes
= min_t(u64
, num_bytes
, SZ_256M
);
10314 cur_bytes
= max(cur_bytes
, min_size
);
10316 * If we are severely fragmented we could end up with really
10317 * small allocations, so if the allocator is returning small
10318 * chunks lets make its job easier by only searching for those
10321 cur_bytes
= min(cur_bytes
, last_alloc
);
10322 ret
= btrfs_reserve_extent(root
, cur_bytes
, cur_bytes
,
10323 min_size
, 0, *alloc_hint
, &ins
, 1, 0);
10326 btrfs_end_transaction(trans
);
10329 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
10331 last_alloc
= ins
.offset
;
10332 ret
= insert_reserved_file_extent(trans
, inode
,
10333 cur_offset
, ins
.objectid
,
10334 ins
.offset
, ins
.offset
,
10335 ins
.offset
, 0, 0, 0,
10336 BTRFS_FILE_EXTENT_PREALLOC
);
10338 btrfs_free_reserved_extent(fs_info
, ins
.objectid
,
10340 btrfs_abort_transaction(trans
, ret
);
10342 btrfs_end_transaction(trans
);
10346 btrfs_drop_extent_cache(BTRFS_I(inode
), cur_offset
,
10347 cur_offset
+ ins
.offset
-1, 0);
10349 em
= alloc_extent_map();
10351 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
10352 &BTRFS_I(inode
)->runtime_flags
);
10356 em
->start
= cur_offset
;
10357 em
->orig_start
= cur_offset
;
10358 em
->len
= ins
.offset
;
10359 em
->block_start
= ins
.objectid
;
10360 em
->block_len
= ins
.offset
;
10361 em
->orig_block_len
= ins
.offset
;
10362 em
->ram_bytes
= ins
.offset
;
10363 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
10364 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
10365 em
->generation
= trans
->transid
;
10368 write_lock(&em_tree
->lock
);
10369 ret
= add_extent_mapping(em_tree
, em
, 1);
10370 write_unlock(&em_tree
->lock
);
10371 if (ret
!= -EEXIST
)
10373 btrfs_drop_extent_cache(BTRFS_I(inode
), cur_offset
,
10374 cur_offset
+ ins
.offset
- 1,
10377 free_extent_map(em
);
10379 num_bytes
-= ins
.offset
;
10380 cur_offset
+= ins
.offset
;
10381 *alloc_hint
= ins
.objectid
+ ins
.offset
;
10383 inode_inc_iversion(inode
);
10384 inode
->i_ctime
= current_time(inode
);
10385 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
10386 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
10387 (actual_len
> inode
->i_size
) &&
10388 (cur_offset
> inode
->i_size
)) {
10389 if (cur_offset
> actual_len
)
10390 i_size
= actual_len
;
10392 i_size
= cur_offset
;
10393 i_size_write(inode
, i_size
);
10394 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
10397 ret
= btrfs_update_inode(trans
, root
, inode
);
10400 btrfs_abort_transaction(trans
, ret
);
10402 btrfs_end_transaction(trans
);
10407 btrfs_end_transaction(trans
);
10409 if (cur_offset
< end
)
10410 btrfs_free_reserved_data_space(inode
, cur_offset
,
10411 end
- cur_offset
+ 1);
10415 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
10416 u64 start
, u64 num_bytes
, u64 min_size
,
10417 loff_t actual_len
, u64
*alloc_hint
)
10419 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
10420 min_size
, actual_len
, alloc_hint
,
10424 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
10425 struct btrfs_trans_handle
*trans
, int mode
,
10426 u64 start
, u64 num_bytes
, u64 min_size
,
10427 loff_t actual_len
, u64
*alloc_hint
)
10429 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
10430 min_size
, actual_len
, alloc_hint
, trans
);
10433 static int btrfs_set_page_dirty(struct page
*page
)
10435 return __set_page_dirty_nobuffers(page
);
10438 static int btrfs_permission(struct inode
*inode
, int mask
)
10440 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
10441 umode_t mode
= inode
->i_mode
;
10443 if (mask
& MAY_WRITE
&&
10444 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
10445 if (btrfs_root_readonly(root
))
10447 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
10450 return generic_permission(inode
, mask
);
10453 static int btrfs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
10455 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
10456 struct btrfs_trans_handle
*trans
;
10457 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
10458 struct inode
*inode
= NULL
;
10464 * 5 units required for adding orphan entry
10466 trans
= btrfs_start_transaction(root
, 5);
10468 return PTR_ERR(trans
);
10470 ret
= btrfs_find_free_ino(root
, &objectid
);
10474 inode
= btrfs_new_inode(trans
, root
, dir
, NULL
, 0,
10475 btrfs_ino(BTRFS_I(dir
)), objectid
, mode
, &index
);
10476 if (IS_ERR(inode
)) {
10477 ret
= PTR_ERR(inode
);
10482 inode
->i_fop
= &btrfs_file_operations
;
10483 inode
->i_op
= &btrfs_file_inode_operations
;
10485 inode
->i_mapping
->a_ops
= &btrfs_aops
;
10486 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
10488 ret
= btrfs_init_inode_security(trans
, inode
, dir
, NULL
);
10492 ret
= btrfs_update_inode(trans
, root
, inode
);
10495 ret
= btrfs_orphan_add(trans
, BTRFS_I(inode
));
10500 * We set number of links to 0 in btrfs_new_inode(), and here we set
10501 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10504 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10506 set_nlink(inode
, 1);
10507 unlock_new_inode(inode
);
10508 d_tmpfile(dentry
, inode
);
10509 mark_inode_dirty(inode
);
10512 btrfs_end_transaction(trans
);
10515 btrfs_balance_delayed_items(fs_info
);
10516 btrfs_btree_balance_dirty(fs_info
);
10520 unlock_new_inode(inode
);
10525 static const struct inode_operations btrfs_dir_inode_operations
= {
10526 .getattr
= btrfs_getattr
,
10527 .lookup
= btrfs_lookup
,
10528 .create
= btrfs_create
,
10529 .unlink
= btrfs_unlink
,
10530 .link
= btrfs_link
,
10531 .mkdir
= btrfs_mkdir
,
10532 .rmdir
= btrfs_rmdir
,
10533 .rename
= btrfs_rename2
,
10534 .symlink
= btrfs_symlink
,
10535 .setattr
= btrfs_setattr
,
10536 .mknod
= btrfs_mknod
,
10537 .listxattr
= btrfs_listxattr
,
10538 .permission
= btrfs_permission
,
10539 .get_acl
= btrfs_get_acl
,
10540 .set_acl
= btrfs_set_acl
,
10541 .update_time
= btrfs_update_time
,
10542 .tmpfile
= btrfs_tmpfile
,
10544 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
10545 .lookup
= btrfs_lookup
,
10546 .permission
= btrfs_permission
,
10547 .update_time
= btrfs_update_time
,
10550 static const struct file_operations btrfs_dir_file_operations
= {
10551 .llseek
= generic_file_llseek
,
10552 .read
= generic_read_dir
,
10553 .iterate_shared
= btrfs_real_readdir
,
10554 .unlocked_ioctl
= btrfs_ioctl
,
10555 #ifdef CONFIG_COMPAT
10556 .compat_ioctl
= btrfs_compat_ioctl
,
10558 .release
= btrfs_release_file
,
10559 .fsync
= btrfs_sync_file
,
10562 static const struct extent_io_ops btrfs_extent_io_ops
= {
10563 .fill_delalloc
= run_delalloc_range
,
10564 .submit_bio_hook
= btrfs_submit_bio_hook
,
10565 .merge_bio_hook
= btrfs_merge_bio_hook
,
10566 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
10567 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
10568 .writepage_start_hook
= btrfs_writepage_start_hook
,
10569 .set_bit_hook
= btrfs_set_bit_hook
,
10570 .clear_bit_hook
= btrfs_clear_bit_hook
,
10571 .merge_extent_hook
= btrfs_merge_extent_hook
,
10572 .split_extent_hook
= btrfs_split_extent_hook
,
10576 * btrfs doesn't support the bmap operation because swapfiles
10577 * use bmap to make a mapping of extents in the file. They assume
10578 * these extents won't change over the life of the file and they
10579 * use the bmap result to do IO directly to the drive.
10581 * the btrfs bmap call would return logical addresses that aren't
10582 * suitable for IO and they also will change frequently as COW
10583 * operations happen. So, swapfile + btrfs == corruption.
10585 * For now we're avoiding this by dropping bmap.
10587 static const struct address_space_operations btrfs_aops
= {
10588 .readpage
= btrfs_readpage
,
10589 .writepage
= btrfs_writepage
,
10590 .writepages
= btrfs_writepages
,
10591 .readpages
= btrfs_readpages
,
10592 .direct_IO
= btrfs_direct_IO
,
10593 .invalidatepage
= btrfs_invalidatepage
,
10594 .releasepage
= btrfs_releasepage
,
10595 .set_page_dirty
= btrfs_set_page_dirty
,
10596 .error_remove_page
= generic_error_remove_page
,
10599 static const struct address_space_operations btrfs_symlink_aops
= {
10600 .readpage
= btrfs_readpage
,
10601 .writepage
= btrfs_writepage
,
10602 .invalidatepage
= btrfs_invalidatepage
,
10603 .releasepage
= btrfs_releasepage
,
10606 static const struct inode_operations btrfs_file_inode_operations
= {
10607 .getattr
= btrfs_getattr
,
10608 .setattr
= btrfs_setattr
,
10609 .listxattr
= btrfs_listxattr
,
10610 .permission
= btrfs_permission
,
10611 .fiemap
= btrfs_fiemap
,
10612 .get_acl
= btrfs_get_acl
,
10613 .set_acl
= btrfs_set_acl
,
10614 .update_time
= btrfs_update_time
,
10616 static const struct inode_operations btrfs_special_inode_operations
= {
10617 .getattr
= btrfs_getattr
,
10618 .setattr
= btrfs_setattr
,
10619 .permission
= btrfs_permission
,
10620 .listxattr
= btrfs_listxattr
,
10621 .get_acl
= btrfs_get_acl
,
10622 .set_acl
= btrfs_set_acl
,
10623 .update_time
= btrfs_update_time
,
10625 static const struct inode_operations btrfs_symlink_inode_operations
= {
10626 .get_link
= page_get_link
,
10627 .getattr
= btrfs_getattr
,
10628 .setattr
= btrfs_setattr
,
10629 .permission
= btrfs_permission
,
10630 .listxattr
= btrfs_listxattr
,
10631 .update_time
= btrfs_update_time
,
10634 const struct dentry_operations btrfs_dentry_operations
= {
10635 .d_delete
= btrfs_dentry_delete
,
10636 .d_release
= btrfs_dentry_release
,