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
;
76 static const struct inode_operations btrfs_dir_inode_operations
;
77 static const struct inode_operations btrfs_symlink_inode_operations
;
78 static const struct inode_operations btrfs_dir_ro_inode_operations
;
79 static const struct inode_operations btrfs_special_inode_operations
;
80 static const struct inode_operations btrfs_file_inode_operations
;
81 static const struct address_space_operations btrfs_aops
;
82 static const struct address_space_operations btrfs_symlink_aops
;
83 static const struct file_operations btrfs_dir_file_operations
;
84 static const struct extent_io_ops btrfs_extent_io_ops
;
86 static struct kmem_cache
*btrfs_inode_cachep
;
87 struct kmem_cache
*btrfs_trans_handle_cachep
;
88 struct kmem_cache
*btrfs_transaction_cachep
;
89 struct kmem_cache
*btrfs_path_cachep
;
90 struct kmem_cache
*btrfs_free_space_cachep
;
93 static const unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
94 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
95 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
96 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
97 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
98 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
99 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
100 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
103 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
104 static int btrfs_truncate(struct inode
*inode
);
105 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
106 static noinline
int cow_file_range(struct inode
*inode
,
107 struct page
*locked_page
,
108 u64 start
, u64 end
, u64 delalloc_end
,
109 int *page_started
, unsigned long *nr_written
,
110 int unlock
, struct btrfs_dedupe_hash
*hash
);
111 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
112 u64 len
, u64 orig_start
,
113 u64 block_start
, u64 block_len
,
114 u64 orig_block_len
, u64 ram_bytes
,
117 static int btrfs_dirty_inode(struct inode
*inode
);
119 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
120 void btrfs_test_inode_set_ops(struct inode
*inode
)
122 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
126 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
127 struct inode
*inode
, struct inode
*dir
,
128 const struct qstr
*qstr
)
132 err
= btrfs_init_acl(trans
, inode
, dir
);
134 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
139 * this does all the hard work for inserting an inline extent into
140 * the btree. The caller should have done a btrfs_drop_extents so that
141 * no overlapping inline items exist in the btree
143 static int insert_inline_extent(struct btrfs_trans_handle
*trans
,
144 struct btrfs_path
*path
, int extent_inserted
,
145 struct btrfs_root
*root
, struct inode
*inode
,
146 u64 start
, size_t size
, size_t compressed_size
,
148 struct page
**compressed_pages
)
150 struct extent_buffer
*leaf
;
151 struct page
*page
= NULL
;
154 struct btrfs_file_extent_item
*ei
;
157 size_t cur_size
= size
;
158 unsigned long offset
;
160 if (compressed_size
&& compressed_pages
)
161 cur_size
= compressed_size
;
163 inode_add_bytes(inode
, size
);
165 if (!extent_inserted
) {
166 struct btrfs_key key
;
169 key
.objectid
= btrfs_ino(inode
);
171 key
.type
= BTRFS_EXTENT_DATA_KEY
;
173 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
174 path
->leave_spinning
= 1;
175 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
182 leaf
= path
->nodes
[0];
183 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
184 struct btrfs_file_extent_item
);
185 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
186 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
187 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
188 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
189 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
190 ptr
= btrfs_file_extent_inline_start(ei
);
192 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
195 while (compressed_size
> 0) {
196 cpage
= compressed_pages
[i
];
197 cur_size
= min_t(unsigned long, compressed_size
,
200 kaddr
= kmap_atomic(cpage
);
201 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
202 kunmap_atomic(kaddr
);
206 compressed_size
-= cur_size
;
208 btrfs_set_file_extent_compression(leaf
, ei
,
211 page
= find_get_page(inode
->i_mapping
,
212 start
>> PAGE_SHIFT
);
213 btrfs_set_file_extent_compression(leaf
, ei
, 0);
214 kaddr
= kmap_atomic(page
);
215 offset
= start
& (PAGE_SIZE
- 1);
216 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
217 kunmap_atomic(kaddr
);
220 btrfs_mark_buffer_dirty(leaf
);
221 btrfs_release_path(path
);
224 * we're an inline extent, so nobody can
225 * extend the file past i_size without locking
226 * a page we already have locked.
228 * We must do any isize and inode updates
229 * before we unlock the pages. Otherwise we
230 * could end up racing with unlink.
232 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
233 ret
= btrfs_update_inode(trans
, root
, inode
);
242 * conditionally insert an inline extent into the file. This
243 * does the checks required to make sure the data is small enough
244 * to fit as an inline extent.
246 static noinline
int cow_file_range_inline(struct btrfs_root
*root
,
247 struct inode
*inode
, u64 start
,
248 u64 end
, size_t compressed_size
,
250 struct page
**compressed_pages
)
252 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
253 struct btrfs_trans_handle
*trans
;
254 u64 isize
= i_size_read(inode
);
255 u64 actual_end
= min(end
+ 1, isize
);
256 u64 inline_len
= actual_end
- start
;
257 u64 aligned_end
= ALIGN(end
, fs_info
->sectorsize
);
258 u64 data_len
= inline_len
;
260 struct btrfs_path
*path
;
261 int extent_inserted
= 0;
262 u32 extent_item_size
;
265 data_len
= compressed_size
;
268 actual_end
> fs_info
->sectorsize
||
269 data_len
> BTRFS_MAX_INLINE_DATA_SIZE(fs_info
) ||
271 (actual_end
& (fs_info
->sectorsize
- 1)) == 0) ||
273 data_len
> fs_info
->max_inline
) {
277 path
= btrfs_alloc_path();
281 trans
= btrfs_join_transaction(root
);
283 btrfs_free_path(path
);
284 return PTR_ERR(trans
);
286 trans
->block_rsv
= &fs_info
->delalloc_block_rsv
;
288 if (compressed_size
&& compressed_pages
)
289 extent_item_size
= btrfs_file_extent_calc_inline_size(
292 extent_item_size
= btrfs_file_extent_calc_inline_size(
295 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
,
296 start
, aligned_end
, NULL
,
297 1, 1, extent_item_size
, &extent_inserted
);
299 btrfs_abort_transaction(trans
, ret
);
303 if (isize
> actual_end
)
304 inline_len
= min_t(u64
, isize
, actual_end
);
305 ret
= insert_inline_extent(trans
, path
, extent_inserted
,
307 inline_len
, compressed_size
,
308 compress_type
, compressed_pages
);
309 if (ret
&& ret
!= -ENOSPC
) {
310 btrfs_abort_transaction(trans
, ret
);
312 } else if (ret
== -ENOSPC
) {
317 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
318 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
319 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
322 * Don't forget to free the reserved space, as for inlined extent
323 * it won't count as data extent, free them directly here.
324 * And at reserve time, it's always aligned to page size, so
325 * just free one page here.
327 btrfs_qgroup_free_data(inode
, 0, PAGE_SIZE
);
328 btrfs_free_path(path
);
329 btrfs_end_transaction(trans
);
333 struct async_extent
{
338 unsigned long nr_pages
;
340 struct list_head list
;
345 struct btrfs_root
*root
;
346 struct page
*locked_page
;
349 struct list_head extents
;
350 struct btrfs_work work
;
353 static noinline
int add_async_extent(struct async_cow
*cow
,
354 u64 start
, u64 ram_size
,
357 unsigned long nr_pages
,
360 struct async_extent
*async_extent
;
362 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
363 BUG_ON(!async_extent
); /* -ENOMEM */
364 async_extent
->start
= start
;
365 async_extent
->ram_size
= ram_size
;
366 async_extent
->compressed_size
= compressed_size
;
367 async_extent
->pages
= pages
;
368 async_extent
->nr_pages
= nr_pages
;
369 async_extent
->compress_type
= compress_type
;
370 list_add_tail(&async_extent
->list
, &cow
->extents
);
374 static inline int inode_need_compress(struct inode
*inode
)
376 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
379 if (btrfs_test_opt(fs_info
, FORCE_COMPRESS
))
381 /* bad compression ratios */
382 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
384 if (btrfs_test_opt(fs_info
, COMPRESS
) ||
385 BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
||
386 BTRFS_I(inode
)->force_compress
)
392 * we create compressed extents in two phases. The first
393 * phase compresses a range of pages that have already been
394 * locked (both pages and state bits are locked).
396 * This is done inside an ordered work queue, and the compression
397 * is spread across many cpus. The actual IO submission is step
398 * two, and the ordered work queue takes care of making sure that
399 * happens in the same order things were put onto the queue by
400 * writepages and friends.
402 * If this code finds it can't get good compression, it puts an
403 * entry onto the work queue to write the uncompressed bytes. This
404 * makes sure that both compressed inodes and uncompressed inodes
405 * are written in the same order that the flusher thread sent them
408 static noinline
void compress_file_range(struct inode
*inode
,
409 struct page
*locked_page
,
411 struct async_cow
*async_cow
,
414 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
415 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
417 u64 blocksize
= fs_info
->sectorsize
;
419 u64 isize
= i_size_read(inode
);
421 struct page
**pages
= NULL
;
422 unsigned long nr_pages
;
423 unsigned long nr_pages_ret
= 0;
424 unsigned long total_compressed
= 0;
425 unsigned long total_in
= 0;
426 unsigned long max_compressed
= SZ_128K
;
427 unsigned long max_uncompressed
= SZ_128K
;
430 int compress_type
= fs_info
->compress_type
;
433 /* if this is a small write inside eof, kick off a defrag */
434 if ((end
- start
+ 1) < SZ_16K
&&
435 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
436 btrfs_add_inode_defrag(NULL
, inode
);
438 actual_end
= min_t(u64
, isize
, end
+ 1);
441 nr_pages
= (end
>> PAGE_SHIFT
) - (start
>> PAGE_SHIFT
) + 1;
442 nr_pages
= min_t(unsigned long, nr_pages
, SZ_128K
/ PAGE_SIZE
);
445 * we don't want to send crud past the end of i_size through
446 * compression, that's just a waste of CPU time. So, if the
447 * end of the file is before the start of our current
448 * requested range of bytes, we bail out to the uncompressed
449 * cleanup code that can deal with all of this.
451 * It isn't really the fastest way to fix things, but this is a
452 * very uncommon corner.
454 if (actual_end
<= start
)
455 goto cleanup_and_bail_uncompressed
;
457 total_compressed
= actual_end
- start
;
460 * skip compression for a small file range(<=blocksize) that
461 * isn't an inline extent, since it doesn't save disk space at all.
463 if (total_compressed
<= blocksize
&&
464 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
465 goto cleanup_and_bail_uncompressed
;
467 /* we want to make sure that amount of ram required to uncompress
468 * an extent is reasonable, so we limit the total size in ram
469 * of a compressed extent to 128k. This is a crucial number
470 * because it also controls how easily we can spread reads across
471 * cpus for decompression.
473 * We also want to make sure the amount of IO required to do
474 * a random read is reasonably small, so we limit the size of
475 * a compressed extent to 128k.
477 total_compressed
= min(total_compressed
, max_uncompressed
);
478 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
479 num_bytes
= max(blocksize
, num_bytes
);
484 * we do compression for mount -o compress and when the
485 * inode has not been flagged as nocompress. This flag can
486 * change at any time if we discover bad compression ratios.
488 if (inode_need_compress(inode
)) {
490 pages
= kcalloc(nr_pages
, sizeof(struct page
*), GFP_NOFS
);
492 /* just bail out to the uncompressed code */
496 if (BTRFS_I(inode
)->force_compress
)
497 compress_type
= BTRFS_I(inode
)->force_compress
;
500 * we need to call clear_page_dirty_for_io on each
501 * page in the range. Otherwise applications with the file
502 * mmap'd can wander in and change the page contents while
503 * we are compressing them.
505 * If the compression fails for any reason, we set the pages
506 * dirty again later on.
508 extent_range_clear_dirty_for_io(inode
, start
, end
);
510 ret
= btrfs_compress_pages(compress_type
,
511 inode
->i_mapping
, start
,
512 total_compressed
, pages
,
513 nr_pages
, &nr_pages_ret
,
519 unsigned long offset
= total_compressed
&
521 struct page
*page
= pages
[nr_pages_ret
- 1];
524 /* zero the tail end of the last page, we might be
525 * sending it down to disk
528 kaddr
= kmap_atomic(page
);
529 memset(kaddr
+ offset
, 0,
531 kunmap_atomic(kaddr
);
538 /* lets try to make an inline extent */
539 if (ret
|| total_in
< (actual_end
- start
)) {
540 /* we didn't compress the entire range, try
541 * to make an uncompressed inline extent.
543 ret
= cow_file_range_inline(root
, inode
, start
, end
,
546 /* try making a compressed inline extent */
547 ret
= cow_file_range_inline(root
, inode
, start
, end
,
549 compress_type
, pages
);
552 unsigned long clear_flags
= EXTENT_DELALLOC
|
554 unsigned long page_error_op
;
556 clear_flags
|= (ret
< 0) ? EXTENT_DO_ACCOUNTING
: 0;
557 page_error_op
= ret
< 0 ? PAGE_SET_ERROR
: 0;
560 * inline extent creation worked or returned error,
561 * we don't need to create any more async work items.
562 * Unlock and free up our temp pages.
564 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
571 btrfs_free_reserved_data_space_noquota(inode
, start
,
579 * we aren't doing an inline extent round the compressed size
580 * up to a block size boundary so the allocator does sane
583 total_compressed
= ALIGN(total_compressed
, blocksize
);
586 * one last check to make sure the compression is really a
587 * win, compare the page count read with the blocks on disk
589 total_in
= ALIGN(total_in
, PAGE_SIZE
);
590 if (total_compressed
>= total_in
) {
593 num_bytes
= total_in
;
597 * The async work queues will take care of doing actual
598 * allocation on disk for these compressed pages, and
599 * will submit them to the elevator.
601 add_async_extent(async_cow
, start
, num_bytes
,
602 total_compressed
, pages
, nr_pages_ret
,
605 if (start
+ num_bytes
< end
) {
616 * the compression code ran but failed to make things smaller,
617 * free any pages it allocated and our page pointer array
619 for (i
= 0; i
< nr_pages_ret
; i
++) {
620 WARN_ON(pages
[i
]->mapping
);
625 total_compressed
= 0;
628 /* flag the file so we don't compress in the future */
629 if (!btrfs_test_opt(fs_info
, FORCE_COMPRESS
) &&
630 !(BTRFS_I(inode
)->force_compress
)) {
631 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
634 cleanup_and_bail_uncompressed
:
636 * No compression, but we still need to write the pages in the file
637 * we've been given so far. redirty the locked page if it corresponds
638 * to our extent and set things up for the async work queue to run
639 * cow_file_range to do the normal delalloc dance.
641 if (page_offset(locked_page
) >= start
&&
642 page_offset(locked_page
) <= end
)
643 __set_page_dirty_nobuffers(locked_page
);
644 /* unlocked later on in the async handlers */
647 extent_range_redirty_for_io(inode
, start
, end
);
648 add_async_extent(async_cow
, start
, end
- start
+ 1, 0, NULL
, 0,
649 BTRFS_COMPRESS_NONE
);
655 for (i
= 0; i
< nr_pages_ret
; i
++) {
656 WARN_ON(pages
[i
]->mapping
);
662 static void free_async_extent_pages(struct async_extent
*async_extent
)
666 if (!async_extent
->pages
)
669 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
670 WARN_ON(async_extent
->pages
[i
]->mapping
);
671 put_page(async_extent
->pages
[i
]);
673 kfree(async_extent
->pages
);
674 async_extent
->nr_pages
= 0;
675 async_extent
->pages
= NULL
;
679 * phase two of compressed writeback. This is the ordered portion
680 * of the code, which only gets called in the order the work was
681 * queued. We walk all the async extents created by compress_file_range
682 * and send them down to the disk.
684 static noinline
void submit_compressed_extents(struct inode
*inode
,
685 struct async_cow
*async_cow
)
687 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
688 struct async_extent
*async_extent
;
690 struct btrfs_key ins
;
691 struct extent_map
*em
;
692 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
693 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
694 struct extent_io_tree
*io_tree
;
698 while (!list_empty(&async_cow
->extents
)) {
699 async_extent
= list_entry(async_cow
->extents
.next
,
700 struct async_extent
, list
);
701 list_del(&async_extent
->list
);
703 io_tree
= &BTRFS_I(inode
)->io_tree
;
706 /* did the compression code fall back to uncompressed IO? */
707 if (!async_extent
->pages
) {
708 int page_started
= 0;
709 unsigned long nr_written
= 0;
711 lock_extent(io_tree
, async_extent
->start
,
712 async_extent
->start
+
713 async_extent
->ram_size
- 1);
715 /* allocate blocks */
716 ret
= cow_file_range(inode
, async_cow
->locked_page
,
718 async_extent
->start
+
719 async_extent
->ram_size
- 1,
720 async_extent
->start
+
721 async_extent
->ram_size
- 1,
722 &page_started
, &nr_written
, 0,
728 * if page_started, cow_file_range inserted an
729 * inline extent and took care of all the unlocking
730 * and IO for us. Otherwise, we need to submit
731 * all those pages down to the drive.
733 if (!page_started
&& !ret
)
734 extent_write_locked_range(io_tree
,
735 inode
, async_extent
->start
,
736 async_extent
->start
+
737 async_extent
->ram_size
- 1,
741 unlock_page(async_cow
->locked_page
);
747 lock_extent(io_tree
, async_extent
->start
,
748 async_extent
->start
+ async_extent
->ram_size
- 1);
750 ret
= btrfs_reserve_extent(root
, async_extent
->ram_size
,
751 async_extent
->compressed_size
,
752 async_extent
->compressed_size
,
753 0, alloc_hint
, &ins
, 1, 1);
755 free_async_extent_pages(async_extent
);
757 if (ret
== -ENOSPC
) {
758 unlock_extent(io_tree
, async_extent
->start
,
759 async_extent
->start
+
760 async_extent
->ram_size
- 1);
763 * we need to redirty the pages if we decide to
764 * fallback to uncompressed IO, otherwise we
765 * will not submit these pages down to lower
768 extent_range_redirty_for_io(inode
,
770 async_extent
->start
+
771 async_extent
->ram_size
- 1);
778 * here we're doing allocation and writeback of the
781 btrfs_drop_extent_cache(inode
, async_extent
->start
,
782 async_extent
->start
+
783 async_extent
->ram_size
- 1, 0);
785 em
= alloc_extent_map();
788 goto out_free_reserve
;
790 em
->start
= async_extent
->start
;
791 em
->len
= async_extent
->ram_size
;
792 em
->orig_start
= em
->start
;
793 em
->mod_start
= em
->start
;
794 em
->mod_len
= em
->len
;
796 em
->block_start
= ins
.objectid
;
797 em
->block_len
= ins
.offset
;
798 em
->orig_block_len
= ins
.offset
;
799 em
->ram_bytes
= async_extent
->ram_size
;
800 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
801 em
->compress_type
= async_extent
->compress_type
;
802 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
803 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
807 write_lock(&em_tree
->lock
);
808 ret
= add_extent_mapping(em_tree
, em
, 1);
809 write_unlock(&em_tree
->lock
);
810 if (ret
!= -EEXIST
) {
814 btrfs_drop_extent_cache(inode
, async_extent
->start
,
815 async_extent
->start
+
816 async_extent
->ram_size
- 1, 0);
820 goto out_free_reserve
;
822 ret
= btrfs_add_ordered_extent_compress(inode
,
825 async_extent
->ram_size
,
827 BTRFS_ORDERED_COMPRESSED
,
828 async_extent
->compress_type
);
830 btrfs_drop_extent_cache(inode
, async_extent
->start
,
831 async_extent
->start
+
832 async_extent
->ram_size
- 1, 0);
833 goto out_free_reserve
;
835 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
838 * clear dirty, set writeback and unlock the pages.
840 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
841 async_extent
->start
+
842 async_extent
->ram_size
- 1,
843 async_extent
->start
+
844 async_extent
->ram_size
- 1,
845 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
,
846 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
848 ret
= btrfs_submit_compressed_write(inode
,
850 async_extent
->ram_size
,
852 ins
.offset
, async_extent
->pages
,
853 async_extent
->nr_pages
);
855 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
856 struct page
*p
= async_extent
->pages
[0];
857 const u64 start
= async_extent
->start
;
858 const u64 end
= start
+ async_extent
->ram_size
- 1;
860 p
->mapping
= inode
->i_mapping
;
861 tree
->ops
->writepage_end_io_hook(p
, start
, end
,
864 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
868 free_async_extent_pages(async_extent
);
870 alloc_hint
= ins
.objectid
+ ins
.offset
;
876 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
877 btrfs_free_reserved_extent(fs_info
, ins
.objectid
, ins
.offset
, 1);
879 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
880 async_extent
->start
+
881 async_extent
->ram_size
- 1,
882 async_extent
->start
+
883 async_extent
->ram_size
- 1,
884 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
|
885 EXTENT_DEFRAG
| EXTENT_DO_ACCOUNTING
,
886 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
887 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
|
889 free_async_extent_pages(async_extent
);
894 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
897 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
898 struct extent_map
*em
;
901 read_lock(&em_tree
->lock
);
902 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
905 * if block start isn't an actual block number then find the
906 * first block in this inode and use that as a hint. If that
907 * block is also bogus then just don't worry about it.
909 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
911 em
= search_extent_mapping(em_tree
, 0, 0);
912 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
913 alloc_hint
= em
->block_start
;
917 alloc_hint
= em
->block_start
;
921 read_unlock(&em_tree
->lock
);
927 * when extent_io.c finds a delayed allocation range in the file,
928 * the call backs end up in this code. The basic idea is to
929 * allocate extents on disk for the range, and create ordered data structs
930 * in ram to track those extents.
932 * locked_page is the page that writepage had locked already. We use
933 * it to make sure we don't do extra locks or unlocks.
935 * *page_started is set to one if we unlock locked_page and do everything
936 * required to start IO on it. It may be clean and already done with
939 static noinline
int cow_file_range(struct inode
*inode
,
940 struct page
*locked_page
,
941 u64 start
, u64 end
, u64 delalloc_end
,
942 int *page_started
, unsigned long *nr_written
,
943 int unlock
, struct btrfs_dedupe_hash
*hash
)
945 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
946 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
949 unsigned long ram_size
;
952 u64 blocksize
= fs_info
->sectorsize
;
953 struct btrfs_key ins
;
954 struct extent_map
*em
;
955 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
958 if (btrfs_is_free_space_inode(inode
)) {
964 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
965 num_bytes
= max(blocksize
, num_bytes
);
966 disk_num_bytes
= num_bytes
;
968 /* if this is a small write inside eof, kick off defrag */
969 if (num_bytes
< SZ_64K
&&
970 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
971 btrfs_add_inode_defrag(NULL
, inode
);
974 /* lets try to make an inline extent */
975 ret
= cow_file_range_inline(root
, inode
, start
, end
, 0, 0,
978 extent_clear_unlock_delalloc(inode
, start
, end
,
980 EXTENT_LOCKED
| EXTENT_DELALLOC
|
981 EXTENT_DEFRAG
, PAGE_UNLOCK
|
982 PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
984 btrfs_free_reserved_data_space_noquota(inode
, start
,
986 *nr_written
= *nr_written
+
987 (end
- start
+ PAGE_SIZE
) / PAGE_SIZE
;
990 } else if (ret
< 0) {
995 BUG_ON(disk_num_bytes
>
996 btrfs_super_total_bytes(fs_info
->super_copy
));
998 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
999 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
1001 while (disk_num_bytes
> 0) {
1004 cur_alloc_size
= disk_num_bytes
;
1005 ret
= btrfs_reserve_extent(root
, cur_alloc_size
, cur_alloc_size
,
1006 fs_info
->sectorsize
, 0, alloc_hint
,
1011 em
= alloc_extent_map();
1017 em
->orig_start
= em
->start
;
1018 ram_size
= ins
.offset
;
1019 em
->len
= ins
.offset
;
1020 em
->mod_start
= em
->start
;
1021 em
->mod_len
= em
->len
;
1023 em
->block_start
= ins
.objectid
;
1024 em
->block_len
= ins
.offset
;
1025 em
->orig_block_len
= ins
.offset
;
1026 em
->ram_bytes
= ram_size
;
1027 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
1028 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1029 em
->generation
= -1;
1032 write_lock(&em_tree
->lock
);
1033 ret
= add_extent_mapping(em_tree
, em
, 1);
1034 write_unlock(&em_tree
->lock
);
1035 if (ret
!= -EEXIST
) {
1036 free_extent_map(em
);
1039 btrfs_drop_extent_cache(inode
, start
,
1040 start
+ ram_size
- 1, 0);
1045 cur_alloc_size
= ins
.offset
;
1046 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
1047 ram_size
, cur_alloc_size
, 0);
1049 goto out_drop_extent_cache
;
1051 if (root
->root_key
.objectid
==
1052 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1053 ret
= btrfs_reloc_clone_csums(inode
, start
,
1056 goto out_drop_extent_cache
;
1059 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
1061 if (disk_num_bytes
< cur_alloc_size
)
1064 /* we're not doing compressed IO, don't unlock the first
1065 * page (which the caller expects to stay locked), don't
1066 * clear any dirty bits and don't set any writeback bits
1068 * Do set the Private2 bit so we know this page was properly
1069 * setup for writepage
1071 op
= unlock
? PAGE_UNLOCK
: 0;
1072 op
|= PAGE_SET_PRIVATE2
;
1074 extent_clear_unlock_delalloc(inode
, start
,
1075 start
+ ram_size
- 1,
1076 delalloc_end
, locked_page
,
1077 EXTENT_LOCKED
| EXTENT_DELALLOC
,
1079 disk_num_bytes
-= cur_alloc_size
;
1080 num_bytes
-= cur_alloc_size
;
1081 alloc_hint
= ins
.objectid
+ ins
.offset
;
1082 start
+= cur_alloc_size
;
1087 out_drop_extent_cache
:
1088 btrfs_drop_extent_cache(inode
, start
, start
+ ram_size
- 1, 0);
1090 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
1091 btrfs_free_reserved_extent(fs_info
, ins
.objectid
, ins
.offset
, 1);
1093 extent_clear_unlock_delalloc(inode
, start
, end
, delalloc_end
,
1095 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
1096 EXTENT_DELALLOC
| EXTENT_DEFRAG
,
1097 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
1098 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
);
1103 * work queue call back to started compression on a file and pages
1105 static noinline
void async_cow_start(struct btrfs_work
*work
)
1107 struct async_cow
*async_cow
;
1109 async_cow
= container_of(work
, struct async_cow
, work
);
1111 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1112 async_cow
->start
, async_cow
->end
, async_cow
,
1114 if (num_added
== 0) {
1115 btrfs_add_delayed_iput(async_cow
->inode
);
1116 async_cow
->inode
= NULL
;
1121 * work queue call back to submit previously compressed pages
1123 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1125 struct btrfs_fs_info
*fs_info
;
1126 struct async_cow
*async_cow
;
1127 struct btrfs_root
*root
;
1128 unsigned long nr_pages
;
1130 async_cow
= container_of(work
, struct async_cow
, work
);
1132 root
= async_cow
->root
;
1133 fs_info
= root
->fs_info
;
1134 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_SIZE
) >>
1138 * atomic_sub_return implies a barrier for waitqueue_active
1140 if (atomic_sub_return(nr_pages
, &fs_info
->async_delalloc_pages
) <
1142 waitqueue_active(&fs_info
->async_submit_wait
))
1143 wake_up(&fs_info
->async_submit_wait
);
1145 if (async_cow
->inode
)
1146 submit_compressed_extents(async_cow
->inode
, async_cow
);
1149 static noinline
void async_cow_free(struct btrfs_work
*work
)
1151 struct async_cow
*async_cow
;
1152 async_cow
= container_of(work
, struct async_cow
, work
);
1153 if (async_cow
->inode
)
1154 btrfs_add_delayed_iput(async_cow
->inode
);
1158 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1159 u64 start
, u64 end
, int *page_started
,
1160 unsigned long *nr_written
)
1162 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1163 struct async_cow
*async_cow
;
1164 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1165 unsigned long nr_pages
;
1167 int limit
= 10 * SZ_1M
;
1169 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1170 1, 0, NULL
, GFP_NOFS
);
1171 while (start
< end
) {
1172 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1173 BUG_ON(!async_cow
); /* -ENOMEM */
1174 async_cow
->inode
= igrab(inode
);
1175 async_cow
->root
= root
;
1176 async_cow
->locked_page
= locked_page
;
1177 async_cow
->start
= start
;
1179 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
&&
1180 !btrfs_test_opt(fs_info
, FORCE_COMPRESS
))
1183 cur_end
= min(end
, start
+ SZ_512K
- 1);
1185 async_cow
->end
= cur_end
;
1186 INIT_LIST_HEAD(&async_cow
->extents
);
1188 btrfs_init_work(&async_cow
->work
,
1189 btrfs_delalloc_helper
,
1190 async_cow_start
, async_cow_submit
,
1193 nr_pages
= (cur_end
- start
+ PAGE_SIZE
) >>
1195 atomic_add(nr_pages
, &fs_info
->async_delalloc_pages
);
1197 btrfs_queue_work(fs_info
->delalloc_workers
, &async_cow
->work
);
1199 if (atomic_read(&fs_info
->async_delalloc_pages
) > limit
) {
1200 wait_event(fs_info
->async_submit_wait
,
1201 (atomic_read(&fs_info
->async_delalloc_pages
) <
1205 while (atomic_read(&fs_info
->async_submit_draining
) &&
1206 atomic_read(&fs_info
->async_delalloc_pages
)) {
1207 wait_event(fs_info
->async_submit_wait
,
1208 (atomic_read(&fs_info
->async_delalloc_pages
) ==
1212 *nr_written
+= nr_pages
;
1213 start
= cur_end
+ 1;
1219 static noinline
int csum_exist_in_range(struct btrfs_fs_info
*fs_info
,
1220 u64 bytenr
, u64 num_bytes
)
1223 struct btrfs_ordered_sum
*sums
;
1226 ret
= btrfs_lookup_csums_range(fs_info
->csum_root
, bytenr
,
1227 bytenr
+ num_bytes
- 1, &list
, 0);
1228 if (ret
== 0 && list_empty(&list
))
1231 while (!list_empty(&list
)) {
1232 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1233 list_del(&sums
->list
);
1240 * when nowcow writeback call back. This checks for snapshots or COW copies
1241 * of the extents that exist in the file, and COWs the file as required.
1243 * If no cow copies or snapshots exist, we write directly to the existing
1246 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1247 struct page
*locked_page
,
1248 u64 start
, u64 end
, int *page_started
, int force
,
1249 unsigned long *nr_written
)
1251 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1252 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1253 struct btrfs_trans_handle
*trans
;
1254 struct extent_buffer
*leaf
;
1255 struct btrfs_path
*path
;
1256 struct btrfs_file_extent_item
*fi
;
1257 struct btrfs_key found_key
;
1272 u64 ino
= btrfs_ino(inode
);
1274 path
= btrfs_alloc_path();
1276 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
1278 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1279 EXTENT_DO_ACCOUNTING
|
1280 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1282 PAGE_SET_WRITEBACK
|
1283 PAGE_END_WRITEBACK
);
1287 nolock
= btrfs_is_free_space_inode(inode
);
1290 trans
= btrfs_join_transaction_nolock(root
);
1292 trans
= btrfs_join_transaction(root
);
1294 if (IS_ERR(trans
)) {
1295 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
1297 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1298 EXTENT_DO_ACCOUNTING
|
1299 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1301 PAGE_SET_WRITEBACK
|
1302 PAGE_END_WRITEBACK
);
1303 btrfs_free_path(path
);
1304 return PTR_ERR(trans
);
1307 trans
->block_rsv
= &fs_info
->delalloc_block_rsv
;
1309 cow_start
= (u64
)-1;
1312 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1316 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1317 leaf
= path
->nodes
[0];
1318 btrfs_item_key_to_cpu(leaf
, &found_key
,
1319 path
->slots
[0] - 1);
1320 if (found_key
.objectid
== ino
&&
1321 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1326 leaf
= path
->nodes
[0];
1327 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1328 ret
= btrfs_next_leaf(root
, path
);
1333 leaf
= path
->nodes
[0];
1339 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1341 if (found_key
.objectid
> ino
)
1343 if (WARN_ON_ONCE(found_key
.objectid
< ino
) ||
1344 found_key
.type
< BTRFS_EXTENT_DATA_KEY
) {
1348 if (found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1349 found_key
.offset
> end
)
1352 if (found_key
.offset
> cur_offset
) {
1353 extent_end
= found_key
.offset
;
1358 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1359 struct btrfs_file_extent_item
);
1360 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1362 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1363 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1364 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1365 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1366 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1367 extent_end
= found_key
.offset
+
1368 btrfs_file_extent_num_bytes(leaf
, fi
);
1370 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1371 if (extent_end
<= start
) {
1375 if (disk_bytenr
== 0)
1377 if (btrfs_file_extent_compression(leaf
, fi
) ||
1378 btrfs_file_extent_encryption(leaf
, fi
) ||
1379 btrfs_file_extent_other_encoding(leaf
, fi
))
1381 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1383 if (btrfs_extent_readonly(fs_info
, disk_bytenr
))
1385 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1387 extent_offset
, disk_bytenr
))
1389 disk_bytenr
+= extent_offset
;
1390 disk_bytenr
+= cur_offset
- found_key
.offset
;
1391 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1393 * if there are pending snapshots for this root,
1394 * we fall into common COW way.
1397 err
= btrfs_start_write_no_snapshoting(root
);
1402 * force cow if csum exists in the range.
1403 * this ensure that csum for a given extent are
1404 * either valid or do not exist.
1406 if (csum_exist_in_range(fs_info
, disk_bytenr
,
1409 if (!btrfs_inc_nocow_writers(fs_info
, disk_bytenr
))
1412 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1413 extent_end
= found_key
.offset
+
1414 btrfs_file_extent_inline_len(leaf
,
1415 path
->slots
[0], fi
);
1416 extent_end
= ALIGN(extent_end
,
1417 fs_info
->sectorsize
);
1422 if (extent_end
<= start
) {
1424 if (!nolock
&& nocow
)
1425 btrfs_end_write_no_snapshoting(root
);
1427 btrfs_dec_nocow_writers(fs_info
, disk_bytenr
);
1431 if (cow_start
== (u64
)-1)
1432 cow_start
= cur_offset
;
1433 cur_offset
= extent_end
;
1434 if (cur_offset
> end
)
1440 btrfs_release_path(path
);
1441 if (cow_start
!= (u64
)-1) {
1442 ret
= cow_file_range(inode
, locked_page
,
1443 cow_start
, found_key
.offset
- 1,
1444 end
, page_started
, nr_written
, 1,
1447 if (!nolock
&& nocow
)
1448 btrfs_end_write_no_snapshoting(root
);
1450 btrfs_dec_nocow_writers(fs_info
,
1454 cow_start
= (u64
)-1;
1457 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1458 struct extent_map
*em
;
1459 struct extent_map_tree
*em_tree
;
1460 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1461 em
= alloc_extent_map();
1462 BUG_ON(!em
); /* -ENOMEM */
1463 em
->start
= cur_offset
;
1464 em
->orig_start
= found_key
.offset
- extent_offset
;
1465 em
->len
= num_bytes
;
1466 em
->block_len
= num_bytes
;
1467 em
->block_start
= disk_bytenr
;
1468 em
->orig_block_len
= disk_num_bytes
;
1469 em
->ram_bytes
= ram_bytes
;
1470 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
1471 em
->mod_start
= em
->start
;
1472 em
->mod_len
= em
->len
;
1473 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1474 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1475 em
->generation
= -1;
1477 write_lock(&em_tree
->lock
);
1478 ret
= add_extent_mapping(em_tree
, em
, 1);
1479 write_unlock(&em_tree
->lock
);
1480 if (ret
!= -EEXIST
) {
1481 free_extent_map(em
);
1484 btrfs_drop_extent_cache(inode
, em
->start
,
1485 em
->start
+ em
->len
- 1, 0);
1487 type
= BTRFS_ORDERED_PREALLOC
;
1489 type
= BTRFS_ORDERED_NOCOW
;
1492 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1493 num_bytes
, num_bytes
, type
);
1495 btrfs_dec_nocow_writers(fs_info
, disk_bytenr
);
1496 BUG_ON(ret
); /* -ENOMEM */
1498 if (root
->root_key
.objectid
==
1499 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1500 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1503 if (!nolock
&& nocow
)
1504 btrfs_end_write_no_snapshoting(root
);
1509 extent_clear_unlock_delalloc(inode
, cur_offset
,
1510 cur_offset
+ num_bytes
- 1, end
,
1511 locked_page
, EXTENT_LOCKED
|
1513 EXTENT_CLEAR_DATA_RESV
,
1514 PAGE_UNLOCK
| PAGE_SET_PRIVATE2
);
1516 if (!nolock
&& nocow
)
1517 btrfs_end_write_no_snapshoting(root
);
1518 cur_offset
= extent_end
;
1519 if (cur_offset
> end
)
1522 btrfs_release_path(path
);
1524 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1525 cow_start
= cur_offset
;
1529 if (cow_start
!= (u64
)-1) {
1530 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
, end
,
1531 page_started
, nr_written
, 1, NULL
);
1537 err
= btrfs_end_transaction(trans
);
1541 if (ret
&& cur_offset
< end
)
1542 extent_clear_unlock_delalloc(inode
, cur_offset
, end
, end
,
1543 locked_page
, EXTENT_LOCKED
|
1544 EXTENT_DELALLOC
| EXTENT_DEFRAG
|
1545 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1547 PAGE_SET_WRITEBACK
|
1548 PAGE_END_WRITEBACK
);
1549 btrfs_free_path(path
);
1553 static inline int need_force_cow(struct inode
*inode
, u64 start
, u64 end
)
1556 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
1557 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
))
1561 * @defrag_bytes is a hint value, no spinlock held here,
1562 * if is not zero, it means the file is defragging.
1563 * Force cow if given extent needs to be defragged.
1565 if (BTRFS_I(inode
)->defrag_bytes
&&
1566 test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
1567 EXTENT_DEFRAG
, 0, NULL
))
1574 * extent_io.c call back to do delayed allocation processing
1576 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1577 u64 start
, u64 end
, int *page_started
,
1578 unsigned long *nr_written
)
1581 int force_cow
= need_force_cow(inode
, start
, end
);
1583 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
&& !force_cow
) {
1584 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1585 page_started
, 1, nr_written
);
1586 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
&& !force_cow
) {
1587 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1588 page_started
, 0, nr_written
);
1589 } else if (!inode_need_compress(inode
)) {
1590 ret
= cow_file_range(inode
, locked_page
, start
, end
, end
,
1591 page_started
, nr_written
, 1, NULL
);
1593 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1594 &BTRFS_I(inode
)->runtime_flags
);
1595 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1596 page_started
, nr_written
);
1601 static void btrfs_split_extent_hook(struct inode
*inode
,
1602 struct extent_state
*orig
, u64 split
)
1606 /* not delalloc, ignore it */
1607 if (!(orig
->state
& EXTENT_DELALLOC
))
1610 size
= orig
->end
- orig
->start
+ 1;
1611 if (size
> BTRFS_MAX_EXTENT_SIZE
) {
1616 * See the explanation in btrfs_merge_extent_hook, the same
1617 * applies here, just in reverse.
1619 new_size
= orig
->end
- split
+ 1;
1620 num_extents
= div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1621 BTRFS_MAX_EXTENT_SIZE
);
1622 new_size
= split
- orig
->start
;
1623 num_extents
+= div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1624 BTRFS_MAX_EXTENT_SIZE
);
1625 if (div64_u64(size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1626 BTRFS_MAX_EXTENT_SIZE
) >= num_extents
)
1630 spin_lock(&BTRFS_I(inode
)->lock
);
1631 BTRFS_I(inode
)->outstanding_extents
++;
1632 spin_unlock(&BTRFS_I(inode
)->lock
);
1636 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1637 * extents so we can keep track of new extents that are just merged onto old
1638 * extents, such as when we are doing sequential writes, so we can properly
1639 * account for the metadata space we'll need.
1641 static void btrfs_merge_extent_hook(struct inode
*inode
,
1642 struct extent_state
*new,
1643 struct extent_state
*other
)
1645 u64 new_size
, old_size
;
1648 /* not delalloc, ignore it */
1649 if (!(other
->state
& EXTENT_DELALLOC
))
1652 if (new->start
> other
->start
)
1653 new_size
= new->end
- other
->start
+ 1;
1655 new_size
= other
->end
- new->start
+ 1;
1657 /* we're not bigger than the max, unreserve the space and go */
1658 if (new_size
<= BTRFS_MAX_EXTENT_SIZE
) {
1659 spin_lock(&BTRFS_I(inode
)->lock
);
1660 BTRFS_I(inode
)->outstanding_extents
--;
1661 spin_unlock(&BTRFS_I(inode
)->lock
);
1666 * We have to add up either side to figure out how many extents were
1667 * accounted for before we merged into one big extent. If the number of
1668 * extents we accounted for is <= the amount we need for the new range
1669 * then we can return, otherwise drop. Think of it like this
1673 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1674 * need 2 outstanding extents, on one side we have 1 and the other side
1675 * we have 1 so they are == and we can return. But in this case
1677 * [MAX_SIZE+4k][MAX_SIZE+4k]
1679 * Each range on their own accounts for 2 extents, but merged together
1680 * they are only 3 extents worth of accounting, so we need to drop in
1683 old_size
= other
->end
- other
->start
+ 1;
1684 num_extents
= div64_u64(old_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1685 BTRFS_MAX_EXTENT_SIZE
);
1686 old_size
= new->end
- new->start
+ 1;
1687 num_extents
+= div64_u64(old_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1688 BTRFS_MAX_EXTENT_SIZE
);
1690 if (div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1691 BTRFS_MAX_EXTENT_SIZE
) >= num_extents
)
1694 spin_lock(&BTRFS_I(inode
)->lock
);
1695 BTRFS_I(inode
)->outstanding_extents
--;
1696 spin_unlock(&BTRFS_I(inode
)->lock
);
1699 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1700 struct inode
*inode
)
1702 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1704 spin_lock(&root
->delalloc_lock
);
1705 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1706 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1707 &root
->delalloc_inodes
);
1708 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1709 &BTRFS_I(inode
)->runtime_flags
);
1710 root
->nr_delalloc_inodes
++;
1711 if (root
->nr_delalloc_inodes
== 1) {
1712 spin_lock(&fs_info
->delalloc_root_lock
);
1713 BUG_ON(!list_empty(&root
->delalloc_root
));
1714 list_add_tail(&root
->delalloc_root
,
1715 &fs_info
->delalloc_roots
);
1716 spin_unlock(&fs_info
->delalloc_root_lock
);
1719 spin_unlock(&root
->delalloc_lock
);
1722 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1723 struct inode
*inode
)
1725 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1727 spin_lock(&root
->delalloc_lock
);
1728 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1729 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1730 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1731 &BTRFS_I(inode
)->runtime_flags
);
1732 root
->nr_delalloc_inodes
--;
1733 if (!root
->nr_delalloc_inodes
) {
1734 spin_lock(&fs_info
->delalloc_root_lock
);
1735 BUG_ON(list_empty(&root
->delalloc_root
));
1736 list_del_init(&root
->delalloc_root
);
1737 spin_unlock(&fs_info
->delalloc_root_lock
);
1740 spin_unlock(&root
->delalloc_lock
);
1744 * extent_io.c set_bit_hook, used to track delayed allocation
1745 * bytes in this file, and to maintain the list of inodes that
1746 * have pending delalloc work to be done.
1748 static void btrfs_set_bit_hook(struct inode
*inode
,
1749 struct extent_state
*state
, unsigned *bits
)
1752 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1754 if ((*bits
& EXTENT_DEFRAG
) && !(*bits
& EXTENT_DELALLOC
))
1757 * set_bit and clear bit hooks normally require _irqsave/restore
1758 * but in this case, we are only testing for the DELALLOC
1759 * bit, which is only set or cleared with irqs on
1761 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1762 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1763 u64 len
= state
->end
+ 1 - state
->start
;
1764 bool do_list
= !btrfs_is_free_space_inode(inode
);
1766 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1767 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1769 spin_lock(&BTRFS_I(inode
)->lock
);
1770 BTRFS_I(inode
)->outstanding_extents
++;
1771 spin_unlock(&BTRFS_I(inode
)->lock
);
1774 /* For sanity tests */
1775 if (btrfs_is_testing(fs_info
))
1778 __percpu_counter_add(&fs_info
->delalloc_bytes
, len
,
1779 fs_info
->delalloc_batch
);
1780 spin_lock(&BTRFS_I(inode
)->lock
);
1781 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1782 if (*bits
& EXTENT_DEFRAG
)
1783 BTRFS_I(inode
)->defrag_bytes
+= len
;
1784 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1785 &BTRFS_I(inode
)->runtime_flags
))
1786 btrfs_add_delalloc_inodes(root
, inode
);
1787 spin_unlock(&BTRFS_I(inode
)->lock
);
1792 * extent_io.c clear_bit_hook, see set_bit_hook for why
1794 static void btrfs_clear_bit_hook(struct inode
*inode
,
1795 struct extent_state
*state
,
1798 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1799 u64 len
= state
->end
+ 1 - state
->start
;
1800 u64 num_extents
= div64_u64(len
+ BTRFS_MAX_EXTENT_SIZE
-1,
1801 BTRFS_MAX_EXTENT_SIZE
);
1803 spin_lock(&BTRFS_I(inode
)->lock
);
1804 if ((state
->state
& EXTENT_DEFRAG
) && (*bits
& EXTENT_DEFRAG
))
1805 BTRFS_I(inode
)->defrag_bytes
-= len
;
1806 spin_unlock(&BTRFS_I(inode
)->lock
);
1809 * set_bit and clear bit hooks normally require _irqsave/restore
1810 * but in this case, we are only testing for the DELALLOC
1811 * bit, which is only set or cleared with irqs on
1813 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1814 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1815 bool do_list
= !btrfs_is_free_space_inode(inode
);
1817 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1818 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1819 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1820 spin_lock(&BTRFS_I(inode
)->lock
);
1821 BTRFS_I(inode
)->outstanding_extents
-= num_extents
;
1822 spin_unlock(&BTRFS_I(inode
)->lock
);
1826 * We don't reserve metadata space for space cache inodes so we
1827 * don't need to call dellalloc_release_metadata if there is an
1830 if (*bits
& EXTENT_DO_ACCOUNTING
&&
1831 root
!= fs_info
->tree_root
)
1832 btrfs_delalloc_release_metadata(inode
, len
);
1834 /* For sanity tests. */
1835 if (btrfs_is_testing(fs_info
))
1838 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1839 && do_list
&& !(state
->state
& EXTENT_NORESERVE
)
1840 && (*bits
& (EXTENT_DO_ACCOUNTING
|
1841 EXTENT_CLEAR_DATA_RESV
)))
1842 btrfs_free_reserved_data_space_noquota(inode
,
1845 __percpu_counter_add(&fs_info
->delalloc_bytes
, -len
,
1846 fs_info
->delalloc_batch
);
1847 spin_lock(&BTRFS_I(inode
)->lock
);
1848 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1849 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1850 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1851 &BTRFS_I(inode
)->runtime_flags
))
1852 btrfs_del_delalloc_inode(root
, inode
);
1853 spin_unlock(&BTRFS_I(inode
)->lock
);
1858 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1859 * we don't create bios that span stripes or chunks
1861 * return 1 if page cannot be merged to bio
1862 * return 0 if page can be merged to bio
1863 * return error otherwise
1865 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1866 size_t size
, struct bio
*bio
,
1867 unsigned long bio_flags
)
1869 struct inode
*inode
= page
->mapping
->host
;
1870 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1871 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
1876 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1879 length
= bio
->bi_iter
.bi_size
;
1880 map_length
= length
;
1881 ret
= btrfs_map_block(fs_info
, btrfs_op(bio
), logical
, &map_length
,
1885 if (map_length
< length
+ size
)
1891 * in order to insert checksums into the metadata in large chunks,
1892 * we wait until bio submission time. All the pages in the bio are
1893 * checksummed and sums are attached onto the ordered extent record.
1895 * At IO completion time the cums attached on the ordered extent record
1896 * are inserted into the btree
1898 static int __btrfs_submit_bio_start(struct inode
*inode
, struct bio
*bio
,
1899 int mirror_num
, unsigned long bio_flags
,
1904 ret
= btrfs_csum_one_bio(inode
, bio
, 0, 0);
1905 BUG_ON(ret
); /* -ENOMEM */
1910 * in order to insert checksums into the metadata in large chunks,
1911 * we wait until bio submission time. All the pages in the bio are
1912 * checksummed and sums are attached onto the ordered extent record.
1914 * At IO completion time the cums attached on the ordered extent record
1915 * are inserted into the btree
1917 static int __btrfs_submit_bio_done(struct inode
*inode
, struct bio
*bio
,
1918 int mirror_num
, unsigned long bio_flags
,
1921 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1924 ret
= btrfs_map_bio(fs_info
, bio
, mirror_num
, 1);
1926 bio
->bi_error
= ret
;
1933 * extent_io.c submission hook. This does the right thing for csum calculation
1934 * on write, or reading the csums from the tree before a read
1936 static int btrfs_submit_bio_hook(struct inode
*inode
, struct bio
*bio
,
1937 int mirror_num
, unsigned long bio_flags
,
1940 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
1941 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1942 enum btrfs_wq_endio_type metadata
= BTRFS_WQ_ENDIO_DATA
;
1945 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1947 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1949 if (btrfs_is_free_space_inode(inode
))
1950 metadata
= BTRFS_WQ_ENDIO_FREE_SPACE
;
1952 if (bio_op(bio
) != REQ_OP_WRITE
) {
1953 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, metadata
);
1957 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1958 ret
= btrfs_submit_compressed_read(inode
, bio
,
1962 } else if (!skip_sum
) {
1963 ret
= btrfs_lookup_bio_sums(inode
, bio
, NULL
);
1968 } else if (async
&& !skip_sum
) {
1969 /* csum items have already been cloned */
1970 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1972 /* we're doing a write, do the async checksumming */
1973 ret
= btrfs_wq_submit_bio(fs_info
, inode
, bio
, mirror_num
,
1974 bio_flags
, bio_offset
,
1975 __btrfs_submit_bio_start
,
1976 __btrfs_submit_bio_done
);
1978 } else if (!skip_sum
) {
1979 ret
= btrfs_csum_one_bio(inode
, bio
, 0, 0);
1985 ret
= btrfs_map_bio(fs_info
, bio
, mirror_num
, 0);
1989 bio
->bi_error
= ret
;
1996 * given a list of ordered sums record them in the inode. This happens
1997 * at IO completion time based on sums calculated at bio submission time.
1999 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
2000 struct inode
*inode
, u64 file_offset
,
2001 struct list_head
*list
)
2003 struct btrfs_ordered_sum
*sum
;
2005 list_for_each_entry(sum
, list
, list
) {
2006 trans
->adding_csums
= 1;
2007 btrfs_csum_file_blocks(trans
,
2008 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
2009 trans
->adding_csums
= 0;
2014 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
2015 struct extent_state
**cached_state
, int dedupe
)
2017 WARN_ON((end
& (PAGE_SIZE
- 1)) == 0);
2018 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
2022 /* see btrfs_writepage_start_hook for details on why this is required */
2023 struct btrfs_writepage_fixup
{
2025 struct btrfs_work work
;
2028 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
2030 struct btrfs_writepage_fixup
*fixup
;
2031 struct btrfs_ordered_extent
*ordered
;
2032 struct extent_state
*cached_state
= NULL
;
2034 struct inode
*inode
;
2039 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
2043 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
2044 ClearPageChecked(page
);
2048 inode
= page
->mapping
->host
;
2049 page_start
= page_offset(page
);
2050 page_end
= page_offset(page
) + PAGE_SIZE
- 1;
2052 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
2055 /* already ordered? We're done */
2056 if (PagePrivate2(page
))
2059 ordered
= btrfs_lookup_ordered_range(inode
, page_start
,
2062 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
2063 page_end
, &cached_state
, GFP_NOFS
);
2065 btrfs_start_ordered_extent(inode
, ordered
, 1);
2066 btrfs_put_ordered_extent(ordered
);
2070 ret
= btrfs_delalloc_reserve_space(inode
, page_start
,
2073 mapping_set_error(page
->mapping
, ret
);
2074 end_extent_writepage(page
, ret
, page_start
, page_end
);
2075 ClearPageChecked(page
);
2079 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
,
2081 ClearPageChecked(page
);
2082 set_page_dirty(page
);
2084 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
2085 &cached_state
, GFP_NOFS
);
2093 * There are a few paths in the higher layers of the kernel that directly
2094 * set the page dirty bit without asking the filesystem if it is a
2095 * good idea. This causes problems because we want to make sure COW
2096 * properly happens and the data=ordered rules are followed.
2098 * In our case any range that doesn't have the ORDERED bit set
2099 * hasn't been properly setup for IO. We kick off an async process
2100 * to fix it up. The async helper will wait for ordered extents, set
2101 * the delalloc bit and make it safe to write the page.
2103 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
2105 struct inode
*inode
= page
->mapping
->host
;
2106 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2107 struct btrfs_writepage_fixup
*fixup
;
2109 /* this page is properly in the ordered list */
2110 if (TestClearPagePrivate2(page
))
2113 if (PageChecked(page
))
2116 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
2120 SetPageChecked(page
);
2122 btrfs_init_work(&fixup
->work
, btrfs_fixup_helper
,
2123 btrfs_writepage_fixup_worker
, NULL
, NULL
);
2125 btrfs_queue_work(fs_info
->fixup_workers
, &fixup
->work
);
2129 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
2130 struct inode
*inode
, u64 file_pos
,
2131 u64 disk_bytenr
, u64 disk_num_bytes
,
2132 u64 num_bytes
, u64 ram_bytes
,
2133 u8 compression
, u8 encryption
,
2134 u16 other_encoding
, int extent_type
)
2136 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2137 struct btrfs_file_extent_item
*fi
;
2138 struct btrfs_path
*path
;
2139 struct extent_buffer
*leaf
;
2140 struct btrfs_key ins
;
2141 int extent_inserted
= 0;
2144 path
= btrfs_alloc_path();
2149 * we may be replacing one extent in the tree with another.
2150 * The new extent is pinned in the extent map, and we don't want
2151 * to drop it from the cache until it is completely in the btree.
2153 * So, tell btrfs_drop_extents to leave this extent in the cache.
2154 * the caller is expected to unpin it and allow it to be merged
2157 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
, file_pos
,
2158 file_pos
+ num_bytes
, NULL
, 0,
2159 1, sizeof(*fi
), &extent_inserted
);
2163 if (!extent_inserted
) {
2164 ins
.objectid
= btrfs_ino(inode
);
2165 ins
.offset
= file_pos
;
2166 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
2168 path
->leave_spinning
= 1;
2169 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
2174 leaf
= path
->nodes
[0];
2175 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2176 struct btrfs_file_extent_item
);
2177 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
2178 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
2179 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
2180 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
2181 btrfs_set_file_extent_offset(leaf
, fi
, 0);
2182 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
2183 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
2184 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
2185 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
2186 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
2188 btrfs_mark_buffer_dirty(leaf
);
2189 btrfs_release_path(path
);
2191 inode_add_bytes(inode
, num_bytes
);
2193 ins
.objectid
= disk_bytenr
;
2194 ins
.offset
= disk_num_bytes
;
2195 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2196 ret
= btrfs_alloc_reserved_file_extent(trans
, root
->root_key
.objectid
,
2197 btrfs_ino(inode
), file_pos
,
2200 * Release the reserved range from inode dirty range map, as it is
2201 * already moved into delayed_ref_head
2203 btrfs_qgroup_release_data(inode
, file_pos
, ram_bytes
);
2205 btrfs_free_path(path
);
2210 /* snapshot-aware defrag */
2211 struct sa_defrag_extent_backref
{
2212 struct rb_node node
;
2213 struct old_sa_defrag_extent
*old
;
2222 struct old_sa_defrag_extent
{
2223 struct list_head list
;
2224 struct new_sa_defrag_extent
*new;
2233 struct new_sa_defrag_extent
{
2234 struct rb_root root
;
2235 struct list_head head
;
2236 struct btrfs_path
*path
;
2237 struct inode
*inode
;
2245 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
2246 struct sa_defrag_extent_backref
*b2
)
2248 if (b1
->root_id
< b2
->root_id
)
2250 else if (b1
->root_id
> b2
->root_id
)
2253 if (b1
->inum
< b2
->inum
)
2255 else if (b1
->inum
> b2
->inum
)
2258 if (b1
->file_pos
< b2
->file_pos
)
2260 else if (b1
->file_pos
> b2
->file_pos
)
2264 * [------------------------------] ===> (a range of space)
2265 * |<--->| |<---->| =============> (fs/file tree A)
2266 * |<---------------------------->| ===> (fs/file tree B)
2268 * A range of space can refer to two file extents in one tree while
2269 * refer to only one file extent in another tree.
2271 * So we may process a disk offset more than one time(two extents in A)
2272 * and locate at the same extent(one extent in B), then insert two same
2273 * backrefs(both refer to the extent in B).
2278 static void backref_insert(struct rb_root
*root
,
2279 struct sa_defrag_extent_backref
*backref
)
2281 struct rb_node
**p
= &root
->rb_node
;
2282 struct rb_node
*parent
= NULL
;
2283 struct sa_defrag_extent_backref
*entry
;
2288 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2290 ret
= backref_comp(backref
, entry
);
2294 p
= &(*p
)->rb_right
;
2297 rb_link_node(&backref
->node
, parent
, p
);
2298 rb_insert_color(&backref
->node
, root
);
2302 * Note the backref might has changed, and in this case we just return 0.
2304 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2307 struct btrfs_file_extent_item
*extent
;
2308 struct old_sa_defrag_extent
*old
= ctx
;
2309 struct new_sa_defrag_extent
*new = old
->new;
2310 struct btrfs_path
*path
= new->path
;
2311 struct btrfs_key key
;
2312 struct btrfs_root
*root
;
2313 struct sa_defrag_extent_backref
*backref
;
2314 struct extent_buffer
*leaf
;
2315 struct inode
*inode
= new->inode
;
2316 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2322 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2323 inum
== btrfs_ino(inode
))
2326 key
.objectid
= root_id
;
2327 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2328 key
.offset
= (u64
)-1;
2330 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2332 if (PTR_ERR(root
) == -ENOENT
)
2335 btrfs_debug(fs_info
, "inum=%llu, offset=%llu, root_id=%llu",
2336 inum
, offset
, root_id
);
2337 return PTR_ERR(root
);
2340 key
.objectid
= inum
;
2341 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2342 if (offset
> (u64
)-1 << 32)
2345 key
.offset
= offset
;
2347 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2348 if (WARN_ON(ret
< 0))
2355 leaf
= path
->nodes
[0];
2356 slot
= path
->slots
[0];
2358 if (slot
>= btrfs_header_nritems(leaf
)) {
2359 ret
= btrfs_next_leaf(root
, path
);
2362 } else if (ret
> 0) {
2371 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2373 if (key
.objectid
> inum
)
2376 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2379 extent
= btrfs_item_ptr(leaf
, slot
,
2380 struct btrfs_file_extent_item
);
2382 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2386 * 'offset' refers to the exact key.offset,
2387 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2388 * (key.offset - extent_offset).
2390 if (key
.offset
!= offset
)
2393 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2394 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2396 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2397 old
->len
|| extent_offset
+ num_bytes
<=
2398 old
->extent_offset
+ old
->offset
)
2403 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2409 backref
->root_id
= root_id
;
2410 backref
->inum
= inum
;
2411 backref
->file_pos
= offset
;
2412 backref
->num_bytes
= num_bytes
;
2413 backref
->extent_offset
= extent_offset
;
2414 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2416 backref_insert(&new->root
, backref
);
2419 btrfs_release_path(path
);
2424 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2425 struct new_sa_defrag_extent
*new)
2427 struct btrfs_fs_info
*fs_info
= btrfs_sb(new->inode
->i_sb
);
2428 struct old_sa_defrag_extent
*old
, *tmp
;
2433 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2434 ret
= iterate_inodes_from_logical(old
->bytenr
+
2435 old
->extent_offset
, fs_info
,
2436 path
, record_one_backref
,
2438 if (ret
< 0 && ret
!= -ENOENT
)
2441 /* no backref to be processed for this extent */
2443 list_del(&old
->list
);
2448 if (list_empty(&new->head
))
2454 static int relink_is_mergable(struct extent_buffer
*leaf
,
2455 struct btrfs_file_extent_item
*fi
,
2456 struct new_sa_defrag_extent
*new)
2458 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != new->bytenr
)
2461 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2464 if (btrfs_file_extent_compression(leaf
, fi
) != new->compress_type
)
2467 if (btrfs_file_extent_encryption(leaf
, fi
) ||
2468 btrfs_file_extent_other_encoding(leaf
, fi
))
2475 * Note the backref might has changed, and in this case we just return 0.
2477 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2478 struct sa_defrag_extent_backref
*prev
,
2479 struct sa_defrag_extent_backref
*backref
)
2481 struct btrfs_file_extent_item
*extent
;
2482 struct btrfs_file_extent_item
*item
;
2483 struct btrfs_ordered_extent
*ordered
;
2484 struct btrfs_trans_handle
*trans
;
2485 struct btrfs_root
*root
;
2486 struct btrfs_key key
;
2487 struct extent_buffer
*leaf
;
2488 struct old_sa_defrag_extent
*old
= backref
->old
;
2489 struct new_sa_defrag_extent
*new = old
->new;
2490 struct btrfs_fs_info
*fs_info
= btrfs_sb(new->inode
->i_sb
);
2491 struct inode
*inode
;
2492 struct extent_state
*cached
= NULL
;
2501 if (prev
&& prev
->root_id
== backref
->root_id
&&
2502 prev
->inum
== backref
->inum
&&
2503 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2506 /* step 1: get root */
2507 key
.objectid
= backref
->root_id
;
2508 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2509 key
.offset
= (u64
)-1;
2511 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2513 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2515 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2516 if (PTR_ERR(root
) == -ENOENT
)
2518 return PTR_ERR(root
);
2521 if (btrfs_root_readonly(root
)) {
2522 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2526 /* step 2: get inode */
2527 key
.objectid
= backref
->inum
;
2528 key
.type
= BTRFS_INODE_ITEM_KEY
;
2531 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2532 if (IS_ERR(inode
)) {
2533 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2537 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2539 /* step 3: relink backref */
2540 lock_start
= backref
->file_pos
;
2541 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2542 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2545 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2547 btrfs_put_ordered_extent(ordered
);
2551 trans
= btrfs_join_transaction(root
);
2552 if (IS_ERR(trans
)) {
2553 ret
= PTR_ERR(trans
);
2557 key
.objectid
= backref
->inum
;
2558 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2559 key
.offset
= backref
->file_pos
;
2561 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2564 } else if (ret
> 0) {
2569 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2570 struct btrfs_file_extent_item
);
2572 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2573 backref
->generation
)
2576 btrfs_release_path(path
);
2578 start
= backref
->file_pos
;
2579 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2580 start
+= old
->extent_offset
+ old
->offset
-
2581 backref
->extent_offset
;
2583 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2584 old
->extent_offset
+ old
->offset
+ old
->len
);
2585 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2587 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2592 key
.objectid
= btrfs_ino(inode
);
2593 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2596 path
->leave_spinning
= 1;
2598 struct btrfs_file_extent_item
*fi
;
2600 struct btrfs_key found_key
;
2602 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2607 leaf
= path
->nodes
[0];
2608 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2610 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2611 struct btrfs_file_extent_item
);
2612 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2614 if (extent_len
+ found_key
.offset
== start
&&
2615 relink_is_mergable(leaf
, fi
, new)) {
2616 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2618 btrfs_mark_buffer_dirty(leaf
);
2619 inode_add_bytes(inode
, len
);
2625 btrfs_release_path(path
);
2630 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2633 btrfs_abort_transaction(trans
, ret
);
2637 leaf
= path
->nodes
[0];
2638 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2639 struct btrfs_file_extent_item
);
2640 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2641 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2642 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2643 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2644 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2645 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2646 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2647 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2648 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2649 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2651 btrfs_mark_buffer_dirty(leaf
);
2652 inode_add_bytes(inode
, len
);
2653 btrfs_release_path(path
);
2655 ret
= btrfs_inc_extent_ref(trans
, fs_info
, new->bytenr
,
2657 backref
->root_id
, backref
->inum
,
2658 new->file_pos
); /* start - extent_offset */
2660 btrfs_abort_transaction(trans
, ret
);
2666 btrfs_release_path(path
);
2667 path
->leave_spinning
= 0;
2668 btrfs_end_transaction(trans
);
2670 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2676 static void free_sa_defrag_extent(struct new_sa_defrag_extent
*new)
2678 struct old_sa_defrag_extent
*old
, *tmp
;
2683 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2689 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2691 struct btrfs_fs_info
*fs_info
= btrfs_sb(new->inode
->i_sb
);
2692 struct btrfs_path
*path
;
2693 struct sa_defrag_extent_backref
*backref
;
2694 struct sa_defrag_extent_backref
*prev
= NULL
;
2695 struct inode
*inode
;
2696 struct btrfs_root
*root
;
2697 struct rb_node
*node
;
2701 root
= BTRFS_I(inode
)->root
;
2703 path
= btrfs_alloc_path();
2707 if (!record_extent_backrefs(path
, new)) {
2708 btrfs_free_path(path
);
2711 btrfs_release_path(path
);
2714 node
= rb_first(&new->root
);
2717 rb_erase(node
, &new->root
);
2719 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2721 ret
= relink_extent_backref(path
, prev
, backref
);
2734 btrfs_free_path(path
);
2736 free_sa_defrag_extent(new);
2738 atomic_dec(&fs_info
->defrag_running
);
2739 wake_up(&fs_info
->transaction_wait
);
2742 static struct new_sa_defrag_extent
*
2743 record_old_file_extents(struct inode
*inode
,
2744 struct btrfs_ordered_extent
*ordered
)
2746 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2747 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2748 struct btrfs_path
*path
;
2749 struct btrfs_key key
;
2750 struct old_sa_defrag_extent
*old
;
2751 struct new_sa_defrag_extent
*new;
2754 new = kmalloc(sizeof(*new), GFP_NOFS
);
2759 new->file_pos
= ordered
->file_offset
;
2760 new->len
= ordered
->len
;
2761 new->bytenr
= ordered
->start
;
2762 new->disk_len
= ordered
->disk_len
;
2763 new->compress_type
= ordered
->compress_type
;
2764 new->root
= RB_ROOT
;
2765 INIT_LIST_HEAD(&new->head
);
2767 path
= btrfs_alloc_path();
2771 key
.objectid
= btrfs_ino(inode
);
2772 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2773 key
.offset
= new->file_pos
;
2775 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2778 if (ret
> 0 && path
->slots
[0] > 0)
2781 /* find out all the old extents for the file range */
2783 struct btrfs_file_extent_item
*extent
;
2784 struct extent_buffer
*l
;
2793 slot
= path
->slots
[0];
2795 if (slot
>= btrfs_header_nritems(l
)) {
2796 ret
= btrfs_next_leaf(root
, path
);
2804 btrfs_item_key_to_cpu(l
, &key
, slot
);
2806 if (key
.objectid
!= btrfs_ino(inode
))
2808 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2810 if (key
.offset
>= new->file_pos
+ new->len
)
2813 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2815 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2816 if (key
.offset
+ num_bytes
< new->file_pos
)
2819 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2823 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2825 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2829 offset
= max(new->file_pos
, key
.offset
);
2830 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2832 old
->bytenr
= disk_bytenr
;
2833 old
->extent_offset
= extent_offset
;
2834 old
->offset
= offset
- key
.offset
;
2835 old
->len
= end
- offset
;
2838 list_add_tail(&old
->list
, &new->head
);
2844 btrfs_free_path(path
);
2845 atomic_inc(&fs_info
->defrag_running
);
2850 btrfs_free_path(path
);
2852 free_sa_defrag_extent(new);
2856 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info
*fs_info
,
2859 struct btrfs_block_group_cache
*cache
;
2861 cache
= btrfs_lookup_block_group(fs_info
, start
);
2864 spin_lock(&cache
->lock
);
2865 cache
->delalloc_bytes
-= len
;
2866 spin_unlock(&cache
->lock
);
2868 btrfs_put_block_group(cache
);
2871 /* as ordered data IO finishes, this gets called so we can finish
2872 * an ordered extent if the range of bytes in the file it covers are
2875 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2877 struct inode
*inode
= ordered_extent
->inode
;
2878 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
2879 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2880 struct btrfs_trans_handle
*trans
= NULL
;
2881 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2882 struct extent_state
*cached_state
= NULL
;
2883 struct new_sa_defrag_extent
*new = NULL
;
2884 int compress_type
= 0;
2886 u64 logical_len
= ordered_extent
->len
;
2888 bool truncated
= false;
2890 nolock
= btrfs_is_free_space_inode(inode
);
2892 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2897 btrfs_free_io_failure_record(inode
, ordered_extent
->file_offset
,
2898 ordered_extent
->file_offset
+
2899 ordered_extent
->len
- 1);
2901 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2903 logical_len
= ordered_extent
->truncated_len
;
2904 /* Truncated the entire extent, don't bother adding */
2909 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2910 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2913 * For mwrite(mmap + memset to write) case, we still reserve
2914 * space for NOCOW range.
2915 * As NOCOW won't cause a new delayed ref, just free the space
2917 btrfs_qgroup_free_data(inode
, ordered_extent
->file_offset
,
2918 ordered_extent
->len
);
2919 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2921 trans
= btrfs_join_transaction_nolock(root
);
2923 trans
= btrfs_join_transaction(root
);
2924 if (IS_ERR(trans
)) {
2925 ret
= PTR_ERR(trans
);
2929 trans
->block_rsv
= &fs_info
->delalloc_block_rsv
;
2930 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2931 if (ret
) /* -ENOMEM or corruption */
2932 btrfs_abort_transaction(trans
, ret
);
2936 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2937 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2940 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2941 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2942 EXTENT_DEFRAG
, 1, cached_state
);
2944 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2945 if (0 && last_snapshot
>= BTRFS_I(inode
)->generation
)
2946 /* the inode is shared */
2947 new = record_old_file_extents(inode
, ordered_extent
);
2949 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2950 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2951 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2955 trans
= btrfs_join_transaction_nolock(root
);
2957 trans
= btrfs_join_transaction(root
);
2958 if (IS_ERR(trans
)) {
2959 ret
= PTR_ERR(trans
);
2964 trans
->block_rsv
= &fs_info
->delalloc_block_rsv
;
2966 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2967 compress_type
= ordered_extent
->compress_type
;
2968 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2969 BUG_ON(compress_type
);
2970 ret
= btrfs_mark_extent_written(trans
, inode
,
2971 ordered_extent
->file_offset
,
2972 ordered_extent
->file_offset
+
2975 BUG_ON(root
== fs_info
->tree_root
);
2976 ret
= insert_reserved_file_extent(trans
, inode
,
2977 ordered_extent
->file_offset
,
2978 ordered_extent
->start
,
2979 ordered_extent
->disk_len
,
2980 logical_len
, logical_len
,
2981 compress_type
, 0, 0,
2982 BTRFS_FILE_EXTENT_REG
);
2984 btrfs_release_delalloc_bytes(fs_info
,
2985 ordered_extent
->start
,
2986 ordered_extent
->disk_len
);
2988 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2989 ordered_extent
->file_offset
, ordered_extent
->len
,
2992 btrfs_abort_transaction(trans
, ret
);
2996 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2997 &ordered_extent
->list
);
2999 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
3000 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
3001 if (ret
) { /* -ENOMEM or corruption */
3002 btrfs_abort_transaction(trans
, ret
);
3007 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
3008 ordered_extent
->file_offset
+
3009 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
3011 if (root
!= fs_info
->tree_root
)
3012 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
3014 btrfs_end_transaction(trans
);
3016 if (ret
|| truncated
) {
3020 start
= ordered_extent
->file_offset
+ logical_len
;
3022 start
= ordered_extent
->file_offset
;
3023 end
= ordered_extent
->file_offset
+ ordered_extent
->len
- 1;
3024 clear_extent_uptodate(io_tree
, start
, end
, NULL
, GFP_NOFS
);
3026 /* Drop the cache for the part of the extent we didn't write. */
3027 btrfs_drop_extent_cache(inode
, start
, end
, 0);
3030 * If the ordered extent had an IOERR or something else went
3031 * wrong we need to return the space for this ordered extent
3032 * back to the allocator. We only free the extent in the
3033 * truncated case if we didn't write out the extent at all.
3035 if ((ret
|| !logical_len
) &&
3036 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
3037 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
3038 btrfs_free_reserved_extent(fs_info
,
3039 ordered_extent
->start
,
3040 ordered_extent
->disk_len
, 1);
3045 * This needs to be done to make sure anybody waiting knows we are done
3046 * updating everything for this ordered extent.
3048 btrfs_remove_ordered_extent(inode
, ordered_extent
);
3050 /* for snapshot-aware defrag */
3053 free_sa_defrag_extent(new);
3054 atomic_dec(&fs_info
->defrag_running
);
3056 relink_file_extents(new);
3061 btrfs_put_ordered_extent(ordered_extent
);
3062 /* once for the tree */
3063 btrfs_put_ordered_extent(ordered_extent
);
3068 static void finish_ordered_fn(struct btrfs_work
*work
)
3070 struct btrfs_ordered_extent
*ordered_extent
;
3071 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
3072 btrfs_finish_ordered_io(ordered_extent
);
3075 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
3076 struct extent_state
*state
, int uptodate
)
3078 struct inode
*inode
= page
->mapping
->host
;
3079 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3080 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
3081 struct btrfs_workqueue
*wq
;
3082 btrfs_work_func_t func
;
3084 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
3086 ClearPagePrivate2(page
);
3087 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
3088 end
- start
+ 1, uptodate
))
3091 if (btrfs_is_free_space_inode(inode
)) {
3092 wq
= fs_info
->endio_freespace_worker
;
3093 func
= btrfs_freespace_write_helper
;
3095 wq
= fs_info
->endio_write_workers
;
3096 func
= btrfs_endio_write_helper
;
3099 btrfs_init_work(&ordered_extent
->work
, func
, finish_ordered_fn
, NULL
,
3101 btrfs_queue_work(wq
, &ordered_extent
->work
);
3106 static int __readpage_endio_check(struct inode
*inode
,
3107 struct btrfs_io_bio
*io_bio
,
3108 int icsum
, struct page
*page
,
3109 int pgoff
, u64 start
, size_t len
)
3115 csum_expected
= *(((u32
*)io_bio
->csum
) + icsum
);
3117 kaddr
= kmap_atomic(page
);
3118 csum
= btrfs_csum_data(kaddr
+ pgoff
, csum
, len
);
3119 btrfs_csum_final(csum
, (u8
*)&csum
);
3120 if (csum
!= csum_expected
)
3123 kunmap_atomic(kaddr
);
3126 btrfs_warn_rl(BTRFS_I(inode
)->root
->fs_info
,
3127 "csum failed ino %llu off %llu csum %u expected csum %u",
3128 btrfs_ino(inode
), start
, csum
, csum_expected
);
3129 memset(kaddr
+ pgoff
, 1, len
);
3130 flush_dcache_page(page
);
3131 kunmap_atomic(kaddr
);
3132 if (csum_expected
== 0)
3138 * when reads are done, we need to check csums to verify the data is correct
3139 * if there's a match, we allow the bio to finish. If not, the code in
3140 * extent_io.c will try to find good copies for us.
3142 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
3143 u64 phy_offset
, struct page
*page
,
3144 u64 start
, u64 end
, int mirror
)
3146 size_t offset
= start
- page_offset(page
);
3147 struct inode
*inode
= page
->mapping
->host
;
3148 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3149 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3151 if (PageChecked(page
)) {
3152 ClearPageChecked(page
);
3156 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
3159 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
3160 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
3161 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
);
3165 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
3166 return __readpage_endio_check(inode
, io_bio
, phy_offset
, page
, offset
,
3167 start
, (size_t)(end
- start
+ 1));
3170 void btrfs_add_delayed_iput(struct inode
*inode
)
3172 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3173 struct btrfs_inode
*binode
= BTRFS_I(inode
);
3175 if (atomic_add_unless(&inode
->i_count
, -1, 1))
3178 spin_lock(&fs_info
->delayed_iput_lock
);
3179 if (binode
->delayed_iput_count
== 0) {
3180 ASSERT(list_empty(&binode
->delayed_iput
));
3181 list_add_tail(&binode
->delayed_iput
, &fs_info
->delayed_iputs
);
3183 binode
->delayed_iput_count
++;
3185 spin_unlock(&fs_info
->delayed_iput_lock
);
3188 void btrfs_run_delayed_iputs(struct btrfs_fs_info
*fs_info
)
3191 spin_lock(&fs_info
->delayed_iput_lock
);
3192 while (!list_empty(&fs_info
->delayed_iputs
)) {
3193 struct btrfs_inode
*inode
;
3195 inode
= list_first_entry(&fs_info
->delayed_iputs
,
3196 struct btrfs_inode
, delayed_iput
);
3197 if (inode
->delayed_iput_count
) {
3198 inode
->delayed_iput_count
--;
3199 list_move_tail(&inode
->delayed_iput
,
3200 &fs_info
->delayed_iputs
);
3202 list_del_init(&inode
->delayed_iput
);
3204 spin_unlock(&fs_info
->delayed_iput_lock
);
3205 iput(&inode
->vfs_inode
);
3206 spin_lock(&fs_info
->delayed_iput_lock
);
3208 spin_unlock(&fs_info
->delayed_iput_lock
);
3212 * This is called in transaction commit time. If there are no orphan
3213 * files in the subvolume, it removes orphan item and frees block_rsv
3216 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
3217 struct btrfs_root
*root
)
3219 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3220 struct btrfs_block_rsv
*block_rsv
;
3223 if (atomic_read(&root
->orphan_inodes
) ||
3224 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
3227 spin_lock(&root
->orphan_lock
);
3228 if (atomic_read(&root
->orphan_inodes
)) {
3229 spin_unlock(&root
->orphan_lock
);
3233 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
3234 spin_unlock(&root
->orphan_lock
);
3238 block_rsv
= root
->orphan_block_rsv
;
3239 root
->orphan_block_rsv
= NULL
;
3240 spin_unlock(&root
->orphan_lock
);
3242 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
) &&
3243 btrfs_root_refs(&root
->root_item
) > 0) {
3244 ret
= btrfs_del_orphan_item(trans
, fs_info
->tree_root
,
3245 root
->root_key
.objectid
);
3247 btrfs_abort_transaction(trans
, ret
);
3249 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
,
3254 WARN_ON(block_rsv
->size
> 0);
3255 btrfs_free_block_rsv(fs_info
, block_rsv
);
3260 * This creates an orphan entry for the given inode in case something goes
3261 * wrong in the middle of an unlink/truncate.
3263 * NOTE: caller of this function should reserve 5 units of metadata for
3266 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
3268 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3269 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3270 struct btrfs_block_rsv
*block_rsv
= NULL
;
3275 if (!root
->orphan_block_rsv
) {
3276 block_rsv
= btrfs_alloc_block_rsv(fs_info
,
3277 BTRFS_BLOCK_RSV_TEMP
);
3282 spin_lock(&root
->orphan_lock
);
3283 if (!root
->orphan_block_rsv
) {
3284 root
->orphan_block_rsv
= block_rsv
;
3285 } else if (block_rsv
) {
3286 btrfs_free_block_rsv(fs_info
, block_rsv
);
3290 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3291 &BTRFS_I(inode
)->runtime_flags
)) {
3294 * For proper ENOSPC handling, we should do orphan
3295 * cleanup when mounting. But this introduces backward
3296 * compatibility issue.
3298 if (!xchg(&root
->orphan_item_inserted
, 1))
3304 atomic_inc(&root
->orphan_inodes
);
3307 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3308 &BTRFS_I(inode
)->runtime_flags
))
3310 spin_unlock(&root
->orphan_lock
);
3312 /* grab metadata reservation from transaction handle */
3314 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
3317 atomic_dec(&root
->orphan_inodes
);
3318 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3319 &BTRFS_I(inode
)->runtime_flags
);
3321 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3322 &BTRFS_I(inode
)->runtime_flags
);
3327 /* insert an orphan item to track this unlinked/truncated file */
3329 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
3331 atomic_dec(&root
->orphan_inodes
);
3333 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3334 &BTRFS_I(inode
)->runtime_flags
);
3335 btrfs_orphan_release_metadata(inode
);
3337 if (ret
!= -EEXIST
) {
3338 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3339 &BTRFS_I(inode
)->runtime_flags
);
3340 btrfs_abort_transaction(trans
, ret
);
3347 /* insert an orphan item to track subvolume contains orphan files */
3349 ret
= btrfs_insert_orphan_item(trans
, fs_info
->tree_root
,
3350 root
->root_key
.objectid
);
3351 if (ret
&& ret
!= -EEXIST
) {
3352 btrfs_abort_transaction(trans
, ret
);
3360 * We have done the truncate/delete so we can go ahead and remove the orphan
3361 * item for this particular inode.
3363 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3364 struct inode
*inode
)
3366 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3367 int delete_item
= 0;
3368 int release_rsv
= 0;
3371 spin_lock(&root
->orphan_lock
);
3372 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3373 &BTRFS_I(inode
)->runtime_flags
))
3376 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3377 &BTRFS_I(inode
)->runtime_flags
))
3379 spin_unlock(&root
->orphan_lock
);
3382 atomic_dec(&root
->orphan_inodes
);
3384 ret
= btrfs_del_orphan_item(trans
, root
,
3389 btrfs_orphan_release_metadata(inode
);
3395 * this cleans up any orphans that may be left on the list from the last use
3398 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3400 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3401 struct btrfs_path
*path
;
3402 struct extent_buffer
*leaf
;
3403 struct btrfs_key key
, found_key
;
3404 struct btrfs_trans_handle
*trans
;
3405 struct inode
*inode
;
3406 u64 last_objectid
= 0;
3407 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3409 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3412 path
= btrfs_alloc_path();
3417 path
->reada
= READA_BACK
;
3419 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3420 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
3421 key
.offset
= (u64
)-1;
3424 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3429 * if ret == 0 means we found what we were searching for, which
3430 * is weird, but possible, so only screw with path if we didn't
3431 * find the key and see if we have stuff that matches
3435 if (path
->slots
[0] == 0)
3440 /* pull out the item */
3441 leaf
= path
->nodes
[0];
3442 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3444 /* make sure the item matches what we want */
3445 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3447 if (found_key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
3450 /* release the path since we're done with it */
3451 btrfs_release_path(path
);
3454 * this is where we are basically btrfs_lookup, without the
3455 * crossing root thing. we store the inode number in the
3456 * offset of the orphan item.
3459 if (found_key
.offset
== last_objectid
) {
3461 "Error removing orphan entry, stopping orphan cleanup");
3466 last_objectid
= found_key
.offset
;
3468 found_key
.objectid
= found_key
.offset
;
3469 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3470 found_key
.offset
= 0;
3471 inode
= btrfs_iget(fs_info
->sb
, &found_key
, root
, NULL
);
3472 ret
= PTR_ERR_OR_ZERO(inode
);
3473 if (ret
&& ret
!= -ENOENT
)
3476 if (ret
== -ENOENT
&& root
== fs_info
->tree_root
) {
3477 struct btrfs_root
*dead_root
;
3478 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3479 int is_dead_root
= 0;
3482 * this is an orphan in the tree root. Currently these
3483 * could come from 2 sources:
3484 * a) a snapshot deletion in progress
3485 * b) a free space cache inode
3486 * We need to distinguish those two, as the snapshot
3487 * orphan must not get deleted.
3488 * find_dead_roots already ran before us, so if this
3489 * is a snapshot deletion, we should find the root
3490 * in the dead_roots list
3492 spin_lock(&fs_info
->trans_lock
);
3493 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3495 if (dead_root
->root_key
.objectid
==
3496 found_key
.objectid
) {
3501 spin_unlock(&fs_info
->trans_lock
);
3503 /* prevent this orphan from being found again */
3504 key
.offset
= found_key
.objectid
- 1;
3509 * Inode is already gone but the orphan item is still there,
3510 * kill the orphan item.
3512 if (ret
== -ENOENT
) {
3513 trans
= btrfs_start_transaction(root
, 1);
3514 if (IS_ERR(trans
)) {
3515 ret
= PTR_ERR(trans
);
3518 btrfs_debug(fs_info
, "auto deleting %Lu",
3519 found_key
.objectid
);
3520 ret
= btrfs_del_orphan_item(trans
, root
,
3521 found_key
.objectid
);
3522 btrfs_end_transaction(trans
);
3529 * add this inode to the orphan list so btrfs_orphan_del does
3530 * the proper thing when we hit it
3532 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3533 &BTRFS_I(inode
)->runtime_flags
);
3534 atomic_inc(&root
->orphan_inodes
);
3536 /* if we have links, this was a truncate, lets do that */
3537 if (inode
->i_nlink
) {
3538 if (WARN_ON(!S_ISREG(inode
->i_mode
))) {
3544 /* 1 for the orphan item deletion. */
3545 trans
= btrfs_start_transaction(root
, 1);
3546 if (IS_ERR(trans
)) {
3548 ret
= PTR_ERR(trans
);
3551 ret
= btrfs_orphan_add(trans
, inode
);
3552 btrfs_end_transaction(trans
);
3558 ret
= btrfs_truncate(inode
);
3560 btrfs_orphan_del(NULL
, inode
);
3565 /* this will do delete_inode and everything for us */
3570 /* release the path since we're done with it */
3571 btrfs_release_path(path
);
3573 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3575 if (root
->orphan_block_rsv
)
3576 btrfs_block_rsv_release(fs_info
, root
->orphan_block_rsv
,
3579 if (root
->orphan_block_rsv
||
3580 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
)) {
3581 trans
= btrfs_join_transaction(root
);
3583 btrfs_end_transaction(trans
);
3587 btrfs_debug(fs_info
, "unlinked %d orphans", nr_unlink
);
3589 btrfs_debug(fs_info
, "truncated %d orphans", nr_truncate
);
3593 btrfs_err(fs_info
, "could not do orphan cleanup %d", ret
);
3594 btrfs_free_path(path
);
3599 * very simple check to peek ahead in the leaf looking for xattrs. If we
3600 * don't find any xattrs, we know there can't be any acls.
3602 * slot is the slot the inode is in, objectid is the objectid of the inode
3604 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3605 int slot
, u64 objectid
,
3606 int *first_xattr_slot
)
3608 u32 nritems
= btrfs_header_nritems(leaf
);
3609 struct btrfs_key found_key
;
3610 static u64 xattr_access
= 0;
3611 static u64 xattr_default
= 0;
3614 if (!xattr_access
) {
3615 xattr_access
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS
,
3616 strlen(XATTR_NAME_POSIX_ACL_ACCESS
));
3617 xattr_default
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT
,
3618 strlen(XATTR_NAME_POSIX_ACL_DEFAULT
));
3622 *first_xattr_slot
= -1;
3623 while (slot
< nritems
) {
3624 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3626 /* we found a different objectid, there must not be acls */
3627 if (found_key
.objectid
!= objectid
)
3630 /* we found an xattr, assume we've got an acl */
3631 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3632 if (*first_xattr_slot
== -1)
3633 *first_xattr_slot
= slot
;
3634 if (found_key
.offset
== xattr_access
||
3635 found_key
.offset
== xattr_default
)
3640 * we found a key greater than an xattr key, there can't
3641 * be any acls later on
3643 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3650 * it goes inode, inode backrefs, xattrs, extents,
3651 * so if there are a ton of hard links to an inode there can
3652 * be a lot of backrefs. Don't waste time searching too hard,
3653 * this is just an optimization
3658 /* we hit the end of the leaf before we found an xattr or
3659 * something larger than an xattr. We have to assume the inode
3662 if (*first_xattr_slot
== -1)
3663 *first_xattr_slot
= slot
;
3668 * read an inode from the btree into the in-memory inode
3670 static int btrfs_read_locked_inode(struct inode
*inode
)
3672 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
3673 struct btrfs_path
*path
;
3674 struct extent_buffer
*leaf
;
3675 struct btrfs_inode_item
*inode_item
;
3676 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3677 struct btrfs_key location
;
3682 bool filled
= false;
3683 int first_xattr_slot
;
3685 ret
= btrfs_fill_inode(inode
, &rdev
);
3689 path
= btrfs_alloc_path();
3695 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3697 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3704 leaf
= path
->nodes
[0];
3709 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3710 struct btrfs_inode_item
);
3711 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3712 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3713 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3714 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3715 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3717 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->atime
);
3718 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->atime
);
3720 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->mtime
);
3721 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->mtime
);
3723 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->ctime
);
3724 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->ctime
);
3726 BTRFS_I(inode
)->i_otime
.tv_sec
=
3727 btrfs_timespec_sec(leaf
, &inode_item
->otime
);
3728 BTRFS_I(inode
)->i_otime
.tv_nsec
=
3729 btrfs_timespec_nsec(leaf
, &inode_item
->otime
);
3731 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3732 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3733 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3735 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3736 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3738 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3740 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3741 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3745 * If we were modified in the current generation and evicted from memory
3746 * and then re-read we need to do a full sync since we don't have any
3747 * idea about which extents were modified before we were evicted from
3750 * This is required for both inode re-read from disk and delayed inode
3751 * in delayed_nodes_tree.
3753 if (BTRFS_I(inode
)->last_trans
== fs_info
->generation
)
3754 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3755 &BTRFS_I(inode
)->runtime_flags
);
3758 * We don't persist the id of the transaction where an unlink operation
3759 * against the inode was last made. So here we assume the inode might
3760 * have been evicted, and therefore the exact value of last_unlink_trans
3761 * lost, and set it to last_trans to avoid metadata inconsistencies
3762 * between the inode and its parent if the inode is fsync'ed and the log
3763 * replayed. For example, in the scenario:
3766 * ln mydir/foo mydir/bar
3769 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3770 * xfs_io -c fsync mydir/foo
3772 * mount fs, triggers fsync log replay
3774 * We must make sure that when we fsync our inode foo we also log its
3775 * parent inode, otherwise after log replay the parent still has the
3776 * dentry with the "bar" name but our inode foo has a link count of 1
3777 * and doesn't have an inode ref with the name "bar" anymore.
3779 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3780 * but it guarantees correctness at the expense of occasional full
3781 * transaction commits on fsync if our inode is a directory, or if our
3782 * inode is not a directory, logging its parent unnecessarily.
3784 BTRFS_I(inode
)->last_unlink_trans
= BTRFS_I(inode
)->last_trans
;
3787 if (inode
->i_nlink
!= 1 ||
3788 path
->slots
[0] >= btrfs_header_nritems(leaf
))
3791 btrfs_item_key_to_cpu(leaf
, &location
, path
->slots
[0]);
3792 if (location
.objectid
!= btrfs_ino(inode
))
3795 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3796 if (location
.type
== BTRFS_INODE_REF_KEY
) {
3797 struct btrfs_inode_ref
*ref
;
3799 ref
= (struct btrfs_inode_ref
*)ptr
;
3800 BTRFS_I(inode
)->dir_index
= btrfs_inode_ref_index(leaf
, ref
);
3801 } else if (location
.type
== BTRFS_INODE_EXTREF_KEY
) {
3802 struct btrfs_inode_extref
*extref
;
3804 extref
= (struct btrfs_inode_extref
*)ptr
;
3805 BTRFS_I(inode
)->dir_index
= btrfs_inode_extref_index(leaf
,
3810 * try to precache a NULL acl entry for files that don't have
3811 * any xattrs or acls
3813 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3814 btrfs_ino(inode
), &first_xattr_slot
);
3815 if (first_xattr_slot
!= -1) {
3816 path
->slots
[0] = first_xattr_slot
;
3817 ret
= btrfs_load_inode_props(inode
, path
);
3820 "error loading props for ino %llu (root %llu): %d",
3822 root
->root_key
.objectid
, ret
);
3824 btrfs_free_path(path
);
3827 cache_no_acl(inode
);
3829 switch (inode
->i_mode
& S_IFMT
) {
3831 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3832 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3833 inode
->i_fop
= &btrfs_file_operations
;
3834 inode
->i_op
= &btrfs_file_inode_operations
;
3837 inode
->i_fop
= &btrfs_dir_file_operations
;
3838 inode
->i_op
= &btrfs_dir_inode_operations
;
3841 inode
->i_op
= &btrfs_symlink_inode_operations
;
3842 inode_nohighmem(inode
);
3843 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3846 inode
->i_op
= &btrfs_special_inode_operations
;
3847 init_special_inode(inode
, inode
->i_mode
, rdev
);
3851 btrfs_update_iflags(inode
);
3855 btrfs_free_path(path
);
3856 make_bad_inode(inode
);
3861 * given a leaf and an inode, copy the inode fields into the leaf
3863 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3864 struct extent_buffer
*leaf
,
3865 struct btrfs_inode_item
*item
,
3866 struct inode
*inode
)
3868 struct btrfs_map_token token
;
3870 btrfs_init_map_token(&token
);
3872 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3873 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3874 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3876 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3877 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3879 btrfs_set_token_timespec_sec(leaf
, &item
->atime
,
3880 inode
->i_atime
.tv_sec
, &token
);
3881 btrfs_set_token_timespec_nsec(leaf
, &item
->atime
,
3882 inode
->i_atime
.tv_nsec
, &token
);
3884 btrfs_set_token_timespec_sec(leaf
, &item
->mtime
,
3885 inode
->i_mtime
.tv_sec
, &token
);
3886 btrfs_set_token_timespec_nsec(leaf
, &item
->mtime
,
3887 inode
->i_mtime
.tv_nsec
, &token
);
3889 btrfs_set_token_timespec_sec(leaf
, &item
->ctime
,
3890 inode
->i_ctime
.tv_sec
, &token
);
3891 btrfs_set_token_timespec_nsec(leaf
, &item
->ctime
,
3892 inode
->i_ctime
.tv_nsec
, &token
);
3894 btrfs_set_token_timespec_sec(leaf
, &item
->otime
,
3895 BTRFS_I(inode
)->i_otime
.tv_sec
, &token
);
3896 btrfs_set_token_timespec_nsec(leaf
, &item
->otime
,
3897 BTRFS_I(inode
)->i_otime
.tv_nsec
, &token
);
3899 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3901 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3903 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3904 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3905 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3906 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3907 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3911 * copy everything in the in-memory inode into the btree.
3913 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3914 struct btrfs_root
*root
, struct inode
*inode
)
3916 struct btrfs_inode_item
*inode_item
;
3917 struct btrfs_path
*path
;
3918 struct extent_buffer
*leaf
;
3921 path
= btrfs_alloc_path();
3925 path
->leave_spinning
= 1;
3926 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3934 leaf
= path
->nodes
[0];
3935 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3936 struct btrfs_inode_item
);
3938 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3939 btrfs_mark_buffer_dirty(leaf
);
3940 btrfs_set_inode_last_trans(trans
, inode
);
3943 btrfs_free_path(path
);
3948 * copy everything in the in-memory inode into the btree.
3950 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3951 struct btrfs_root
*root
, struct inode
*inode
)
3953 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3957 * If the inode is a free space inode, we can deadlock during commit
3958 * if we put it into the delayed code.
3960 * The data relocation inode should also be directly updated
3963 if (!btrfs_is_free_space_inode(inode
)
3964 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
3965 && !test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
)) {
3966 btrfs_update_root_times(trans
, root
);
3968 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3970 btrfs_set_inode_last_trans(trans
, inode
);
3974 return btrfs_update_inode_item(trans
, root
, inode
);
3977 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3978 struct btrfs_root
*root
,
3979 struct inode
*inode
)
3983 ret
= btrfs_update_inode(trans
, root
, inode
);
3985 return btrfs_update_inode_item(trans
, root
, inode
);
3990 * unlink helper that gets used here in inode.c and in the tree logging
3991 * recovery code. It remove a link in a directory with a given name, and
3992 * also drops the back refs in the inode to the directory
3994 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3995 struct btrfs_root
*root
,
3996 struct inode
*dir
, struct inode
*inode
,
3997 const char *name
, int name_len
)
3999 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4000 struct btrfs_path
*path
;
4002 struct extent_buffer
*leaf
;
4003 struct btrfs_dir_item
*di
;
4004 struct btrfs_key key
;
4006 u64 ino
= btrfs_ino(inode
);
4007 u64 dir_ino
= btrfs_ino(dir
);
4009 path
= btrfs_alloc_path();
4015 path
->leave_spinning
= 1;
4016 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
4017 name
, name_len
, -1);
4026 leaf
= path
->nodes
[0];
4027 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
4028 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
4031 btrfs_release_path(path
);
4034 * If we don't have dir index, we have to get it by looking up
4035 * the inode ref, since we get the inode ref, remove it directly,
4036 * it is unnecessary to do delayed deletion.
4038 * But if we have dir index, needn't search inode ref to get it.
4039 * Since the inode ref is close to the inode item, it is better
4040 * that we delay to delete it, and just do this deletion when
4041 * we update the inode item.
4043 if (BTRFS_I(inode
)->dir_index
) {
4044 ret
= btrfs_delayed_delete_inode_ref(inode
);
4046 index
= BTRFS_I(inode
)->dir_index
;
4051 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
4055 "failed to delete reference to %.*s, inode %llu parent %llu",
4056 name_len
, name
, ino
, dir_ino
);
4057 btrfs_abort_transaction(trans
, ret
);
4061 ret
= btrfs_delete_delayed_dir_index(trans
, fs_info
, dir
, index
);
4063 btrfs_abort_transaction(trans
, ret
);
4067 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
4069 if (ret
!= 0 && ret
!= -ENOENT
) {
4070 btrfs_abort_transaction(trans
, ret
);
4074 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
4079 btrfs_abort_transaction(trans
, ret
);
4081 btrfs_free_path(path
);
4085 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
4086 inode_inc_iversion(inode
);
4087 inode_inc_iversion(dir
);
4088 inode
->i_ctime
= dir
->i_mtime
=
4089 dir
->i_ctime
= current_time(inode
);
4090 ret
= btrfs_update_inode(trans
, root
, dir
);
4095 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
4096 struct btrfs_root
*root
,
4097 struct inode
*dir
, struct inode
*inode
,
4098 const char *name
, int name_len
)
4101 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
4104 ret
= btrfs_update_inode(trans
, root
, inode
);
4110 * helper to start transaction for unlink and rmdir.
4112 * unlink and rmdir are special in btrfs, they do not always free space, so
4113 * if we cannot make our reservations the normal way try and see if there is
4114 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4115 * allow the unlink to occur.
4117 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
4119 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4122 * 1 for the possible orphan item
4123 * 1 for the dir item
4124 * 1 for the dir index
4125 * 1 for the inode ref
4128 return btrfs_start_transaction_fallback_global_rsv(root
, 5, 5);
4131 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
4133 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4134 struct btrfs_trans_handle
*trans
;
4135 struct inode
*inode
= d_inode(dentry
);
4138 trans
= __unlink_start_trans(dir
);
4140 return PTR_ERR(trans
);
4142 btrfs_record_unlink_dir(trans
, dir
, d_inode(dentry
), 0);
4144 ret
= btrfs_unlink_inode(trans
, root
, dir
, d_inode(dentry
),
4145 dentry
->d_name
.name
, dentry
->d_name
.len
);
4149 if (inode
->i_nlink
== 0) {
4150 ret
= btrfs_orphan_add(trans
, inode
);
4156 btrfs_end_transaction(trans
);
4157 btrfs_btree_balance_dirty(root
->fs_info
);
4161 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
4162 struct btrfs_root
*root
,
4163 struct inode
*dir
, u64 objectid
,
4164 const char *name
, int name_len
)
4166 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4167 struct btrfs_path
*path
;
4168 struct extent_buffer
*leaf
;
4169 struct btrfs_dir_item
*di
;
4170 struct btrfs_key key
;
4173 u64 dir_ino
= btrfs_ino(dir
);
4175 path
= btrfs_alloc_path();
4179 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
4180 name
, name_len
, -1);
4181 if (IS_ERR_OR_NULL(di
)) {
4189 leaf
= path
->nodes
[0];
4190 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
4191 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
4192 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
4194 btrfs_abort_transaction(trans
, ret
);
4197 btrfs_release_path(path
);
4199 ret
= btrfs_del_root_ref(trans
, fs_info
, objectid
,
4200 root
->root_key
.objectid
, dir_ino
,
4201 &index
, name
, name_len
);
4203 if (ret
!= -ENOENT
) {
4204 btrfs_abort_transaction(trans
, ret
);
4207 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
4209 if (IS_ERR_OR_NULL(di
)) {
4214 btrfs_abort_transaction(trans
, ret
);
4218 leaf
= path
->nodes
[0];
4219 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4220 btrfs_release_path(path
);
4223 btrfs_release_path(path
);
4225 ret
= btrfs_delete_delayed_dir_index(trans
, fs_info
, dir
, index
);
4227 btrfs_abort_transaction(trans
, ret
);
4231 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
4232 inode_inc_iversion(dir
);
4233 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
4234 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
4236 btrfs_abort_transaction(trans
, ret
);
4238 btrfs_free_path(path
);
4242 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
4244 struct inode
*inode
= d_inode(dentry
);
4246 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4247 struct btrfs_trans_handle
*trans
;
4248 u64 last_unlink_trans
;
4250 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
4252 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
4255 trans
= __unlink_start_trans(dir
);
4257 return PTR_ERR(trans
);
4259 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
4260 err
= btrfs_unlink_subvol(trans
, root
, dir
,
4261 BTRFS_I(inode
)->location
.objectid
,
4262 dentry
->d_name
.name
,
4263 dentry
->d_name
.len
);
4267 err
= btrfs_orphan_add(trans
, inode
);
4271 last_unlink_trans
= BTRFS_I(inode
)->last_unlink_trans
;
4273 /* now the directory is empty */
4274 err
= btrfs_unlink_inode(trans
, root
, dir
, d_inode(dentry
),
4275 dentry
->d_name
.name
, dentry
->d_name
.len
);
4277 btrfs_i_size_write(inode
, 0);
4279 * Propagate the last_unlink_trans value of the deleted dir to
4280 * its parent directory. This is to prevent an unrecoverable
4281 * log tree in the case we do something like this:
4283 * 2) create snapshot under dir foo
4284 * 3) delete the snapshot
4287 * 6) fsync foo or some file inside foo
4289 if (last_unlink_trans
>= trans
->transid
)
4290 BTRFS_I(dir
)->last_unlink_trans
= last_unlink_trans
;
4293 btrfs_end_transaction(trans
);
4294 btrfs_btree_balance_dirty(root
->fs_info
);
4299 static int truncate_space_check(struct btrfs_trans_handle
*trans
,
4300 struct btrfs_root
*root
,
4303 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4307 * This is only used to apply pressure to the enospc system, we don't
4308 * intend to use this reservation at all.
4310 bytes_deleted
= btrfs_csum_bytes_to_leaves(fs_info
, bytes_deleted
);
4311 bytes_deleted
*= fs_info
->nodesize
;
4312 ret
= btrfs_block_rsv_add(root
, &fs_info
->trans_block_rsv
,
4313 bytes_deleted
, BTRFS_RESERVE_NO_FLUSH
);
4315 trace_btrfs_space_reservation(fs_info
, "transaction",
4318 trans
->bytes_reserved
+= bytes_deleted
;
4324 static int truncate_inline_extent(struct inode
*inode
,
4325 struct btrfs_path
*path
,
4326 struct btrfs_key
*found_key
,
4330 struct extent_buffer
*leaf
= path
->nodes
[0];
4331 int slot
= path
->slots
[0];
4332 struct btrfs_file_extent_item
*fi
;
4333 u32 size
= (u32
)(new_size
- found_key
->offset
);
4334 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4336 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
4338 if (btrfs_file_extent_compression(leaf
, fi
) != BTRFS_COMPRESS_NONE
) {
4339 loff_t offset
= new_size
;
4340 loff_t page_end
= ALIGN(offset
, PAGE_SIZE
);
4343 * Zero out the remaining of the last page of our inline extent,
4344 * instead of directly truncating our inline extent here - that
4345 * would be much more complex (decompressing all the data, then
4346 * compressing the truncated data, which might be bigger than
4347 * the size of the inline extent, resize the extent, etc).
4348 * We release the path because to get the page we might need to
4349 * read the extent item from disk (data not in the page cache).
4351 btrfs_release_path(path
);
4352 return btrfs_truncate_block(inode
, offset
, page_end
- offset
,
4356 btrfs_set_file_extent_ram_bytes(leaf
, fi
, size
);
4357 size
= btrfs_file_extent_calc_inline_size(size
);
4358 btrfs_truncate_item(root
->fs_info
, path
, size
, 1);
4360 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4361 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4367 * this can truncate away extent items, csum items and directory items.
4368 * It starts at a high offset and removes keys until it can't find
4369 * any higher than new_size
4371 * csum items that cross the new i_size are truncated to the new size
4374 * min_type is the minimum key type to truncate down to. If set to 0, this
4375 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4377 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4378 struct btrfs_root
*root
,
4379 struct inode
*inode
,
4380 u64 new_size
, u32 min_type
)
4382 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
4383 struct btrfs_path
*path
;
4384 struct extent_buffer
*leaf
;
4385 struct btrfs_file_extent_item
*fi
;
4386 struct btrfs_key key
;
4387 struct btrfs_key found_key
;
4388 u64 extent_start
= 0;
4389 u64 extent_num_bytes
= 0;
4390 u64 extent_offset
= 0;
4392 u64 last_size
= new_size
;
4393 u32 found_type
= (u8
)-1;
4396 int pending_del_nr
= 0;
4397 int pending_del_slot
= 0;
4398 int extent_type
= -1;
4401 u64 ino
= btrfs_ino(inode
);
4402 u64 bytes_deleted
= 0;
4404 bool should_throttle
= 0;
4405 bool should_end
= 0;
4407 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4410 * for non-free space inodes and ref cows, we want to back off from
4413 if (!btrfs_is_free_space_inode(inode
) &&
4414 test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4417 path
= btrfs_alloc_path();
4420 path
->reada
= READA_BACK
;
4423 * We want to drop from the next block forward in case this new size is
4424 * not block aligned since we will be keeping the last block of the
4425 * extent just the way it is.
4427 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4428 root
== fs_info
->tree_root
)
4429 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
4430 fs_info
->sectorsize
),
4434 * This function is also used to drop the items in the log tree before
4435 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4436 * it is used to drop the loged items. So we shouldn't kill the delayed
4439 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4440 btrfs_kill_delayed_inode_items(inode
);
4443 key
.offset
= (u64
)-1;
4448 * with a 16K leaf size and 128MB extents, you can actually queue
4449 * up a huge file in a single leaf. Most of the time that
4450 * bytes_deleted is > 0, it will be huge by the time we get here
4452 if (be_nice
&& bytes_deleted
> SZ_32M
) {
4453 if (btrfs_should_end_transaction(trans
)) {
4460 path
->leave_spinning
= 1;
4461 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4468 /* there are no items in the tree for us to truncate, we're
4471 if (path
->slots
[0] == 0)
4478 leaf
= path
->nodes
[0];
4479 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4480 found_type
= found_key
.type
;
4482 if (found_key
.objectid
!= ino
)
4485 if (found_type
< min_type
)
4488 item_end
= found_key
.offset
;
4489 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4490 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4491 struct btrfs_file_extent_item
);
4492 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4493 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4495 btrfs_file_extent_num_bytes(leaf
, fi
);
4496 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4497 item_end
+= btrfs_file_extent_inline_len(leaf
,
4498 path
->slots
[0], fi
);
4502 if (found_type
> min_type
) {
4505 if (item_end
< new_size
) {
4507 * With NO_HOLES mode, for the following mapping
4509 * [0-4k][hole][8k-12k]
4511 * if truncating isize down to 6k, it ends up
4514 if (btrfs_fs_incompat(root
->fs_info
, NO_HOLES
))
4515 last_size
= new_size
;
4518 if (found_key
.offset
>= new_size
)
4524 /* FIXME, shrink the extent if the ref count is only 1 */
4525 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4529 last_size
= found_key
.offset
;
4531 last_size
= new_size
;
4533 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4535 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4537 u64 orig_num_bytes
=
4538 btrfs_file_extent_num_bytes(leaf
, fi
);
4539 extent_num_bytes
= ALIGN(new_size
-
4541 fs_info
->sectorsize
);
4542 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4544 num_dec
= (orig_num_bytes
-
4546 if (test_bit(BTRFS_ROOT_REF_COWS
,
4549 inode_sub_bytes(inode
, num_dec
);
4550 btrfs_mark_buffer_dirty(leaf
);
4553 btrfs_file_extent_disk_num_bytes(leaf
,
4555 extent_offset
= found_key
.offset
-
4556 btrfs_file_extent_offset(leaf
, fi
);
4558 /* FIXME blocksize != 4096 */
4559 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4560 if (extent_start
!= 0) {
4562 if (test_bit(BTRFS_ROOT_REF_COWS
,
4564 inode_sub_bytes(inode
, num_dec
);
4567 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4569 * we can't truncate inline items that have had
4573 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4574 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4577 * Need to release path in order to truncate a
4578 * compressed extent. So delete any accumulated
4579 * extent items so far.
4581 if (btrfs_file_extent_compression(leaf
, fi
) !=
4582 BTRFS_COMPRESS_NONE
&& pending_del_nr
) {
4583 err
= btrfs_del_items(trans
, root
, path
,
4587 btrfs_abort_transaction(trans
,
4594 err
= truncate_inline_extent(inode
, path
,
4599 btrfs_abort_transaction(trans
, err
);
4602 } else if (test_bit(BTRFS_ROOT_REF_COWS
,
4604 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4609 if (!pending_del_nr
) {
4610 /* no pending yet, add ourselves */
4611 pending_del_slot
= path
->slots
[0];
4613 } else if (pending_del_nr
&&
4614 path
->slots
[0] + 1 == pending_del_slot
) {
4615 /* hop on the pending chunk */
4617 pending_del_slot
= path
->slots
[0];
4624 should_throttle
= 0;
4627 (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4628 root
== fs_info
->tree_root
)) {
4629 btrfs_set_path_blocking(path
);
4630 bytes_deleted
+= extent_num_bytes
;
4631 ret
= btrfs_free_extent(trans
, fs_info
, extent_start
,
4632 extent_num_bytes
, 0,
4633 btrfs_header_owner(leaf
),
4634 ino
, extent_offset
);
4636 if (btrfs_should_throttle_delayed_refs(trans
, fs_info
))
4637 btrfs_async_run_delayed_refs(fs_info
,
4638 trans
->delayed_ref_updates
* 2,
4641 if (truncate_space_check(trans
, root
,
4642 extent_num_bytes
)) {
4645 if (btrfs_should_throttle_delayed_refs(trans
,
4647 should_throttle
= 1;
4651 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4654 if (path
->slots
[0] == 0 ||
4655 path
->slots
[0] != pending_del_slot
||
4656 should_throttle
|| should_end
) {
4657 if (pending_del_nr
) {
4658 ret
= btrfs_del_items(trans
, root
, path
,
4662 btrfs_abort_transaction(trans
, ret
);
4667 btrfs_release_path(path
);
4668 if (should_throttle
) {
4669 unsigned long updates
= trans
->delayed_ref_updates
;
4671 trans
->delayed_ref_updates
= 0;
4672 ret
= btrfs_run_delayed_refs(trans
,
4680 * if we failed to refill our space rsv, bail out
4681 * and let the transaction restart
4693 if (pending_del_nr
) {
4694 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4697 btrfs_abort_transaction(trans
, ret
);
4700 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
4701 btrfs_ordered_update_i_size(inode
, last_size
, NULL
);
4703 btrfs_free_path(path
);
4705 if (be_nice
&& bytes_deleted
> SZ_32M
) {
4706 unsigned long updates
= trans
->delayed_ref_updates
;
4708 trans
->delayed_ref_updates
= 0;
4709 ret
= btrfs_run_delayed_refs(trans
, fs_info
,
4719 * btrfs_truncate_block - read, zero a chunk and write a block
4720 * @inode - inode that we're zeroing
4721 * @from - the offset to start zeroing
4722 * @len - the length to zero, 0 to zero the entire range respective to the
4724 * @front - zero up to the offset instead of from the offset on
4726 * This will find the block for the "from" offset and cow the block and zero the
4727 * part we want to zero. This is used with truncate and hole punching.
4729 int btrfs_truncate_block(struct inode
*inode
, loff_t from
, loff_t len
,
4732 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4733 struct address_space
*mapping
= inode
->i_mapping
;
4734 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4735 struct btrfs_ordered_extent
*ordered
;
4736 struct extent_state
*cached_state
= NULL
;
4738 u32 blocksize
= fs_info
->sectorsize
;
4739 pgoff_t index
= from
>> PAGE_SHIFT
;
4740 unsigned offset
= from
& (blocksize
- 1);
4742 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4747 if ((offset
& (blocksize
- 1)) == 0 &&
4748 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4751 ret
= btrfs_delalloc_reserve_space(inode
,
4752 round_down(from
, blocksize
), blocksize
);
4757 page
= find_or_create_page(mapping
, index
, mask
);
4759 btrfs_delalloc_release_space(inode
,
4760 round_down(from
, blocksize
),
4766 block_start
= round_down(from
, blocksize
);
4767 block_end
= block_start
+ blocksize
- 1;
4769 if (!PageUptodate(page
)) {
4770 ret
= btrfs_readpage(NULL
, page
);
4772 if (page
->mapping
!= mapping
) {
4777 if (!PageUptodate(page
)) {
4782 wait_on_page_writeback(page
);
4784 lock_extent_bits(io_tree
, block_start
, block_end
, &cached_state
);
4785 set_page_extent_mapped(page
);
4787 ordered
= btrfs_lookup_ordered_extent(inode
, block_start
);
4789 unlock_extent_cached(io_tree
, block_start
, block_end
,
4790 &cached_state
, GFP_NOFS
);
4793 btrfs_start_ordered_extent(inode
, ordered
, 1);
4794 btrfs_put_ordered_extent(ordered
);
4798 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, block_start
, block_end
,
4799 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4800 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4801 0, 0, &cached_state
, GFP_NOFS
);
4803 ret
= btrfs_set_extent_delalloc(inode
, block_start
, block_end
,
4806 unlock_extent_cached(io_tree
, block_start
, block_end
,
4807 &cached_state
, GFP_NOFS
);
4811 if (offset
!= blocksize
) {
4813 len
= blocksize
- offset
;
4816 memset(kaddr
+ (block_start
- page_offset(page
)),
4819 memset(kaddr
+ (block_start
- page_offset(page
)) + offset
,
4821 flush_dcache_page(page
);
4824 ClearPageChecked(page
);
4825 set_page_dirty(page
);
4826 unlock_extent_cached(io_tree
, block_start
, block_end
, &cached_state
,
4831 btrfs_delalloc_release_space(inode
, block_start
,
4839 static int maybe_insert_hole(struct btrfs_root
*root
, struct inode
*inode
,
4840 u64 offset
, u64 len
)
4842 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4843 struct btrfs_trans_handle
*trans
;
4847 * Still need to make sure the inode looks like it's been updated so
4848 * that any holes get logged if we fsync.
4850 if (btrfs_fs_incompat(fs_info
, NO_HOLES
)) {
4851 BTRFS_I(inode
)->last_trans
= fs_info
->generation
;
4852 BTRFS_I(inode
)->last_sub_trans
= root
->log_transid
;
4853 BTRFS_I(inode
)->last_log_commit
= root
->last_log_commit
;
4858 * 1 - for the one we're dropping
4859 * 1 - for the one we're adding
4860 * 1 - for updating the inode.
4862 trans
= btrfs_start_transaction(root
, 3);
4864 return PTR_ERR(trans
);
4866 ret
= btrfs_drop_extents(trans
, root
, inode
, offset
, offset
+ len
, 1);
4868 btrfs_abort_transaction(trans
, ret
);
4869 btrfs_end_transaction(trans
);
4873 ret
= btrfs_insert_file_extent(trans
, root
, btrfs_ino(inode
), offset
,
4874 0, 0, len
, 0, len
, 0, 0, 0);
4876 btrfs_abort_transaction(trans
, ret
);
4878 btrfs_update_inode(trans
, root
, inode
);
4879 btrfs_end_transaction(trans
);
4884 * This function puts in dummy file extents for the area we're creating a hole
4885 * for. So if we are truncating this file to a larger size we need to insert
4886 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4887 * the range between oldsize and size
4889 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4891 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
4892 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4893 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4894 struct extent_map
*em
= NULL
;
4895 struct extent_state
*cached_state
= NULL
;
4896 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4897 u64 hole_start
= ALIGN(oldsize
, fs_info
->sectorsize
);
4898 u64 block_end
= ALIGN(size
, fs_info
->sectorsize
);
4905 * If our size started in the middle of a block we need to zero out the
4906 * rest of the block before we expand the i_size, otherwise we could
4907 * expose stale data.
4909 err
= btrfs_truncate_block(inode
, oldsize
, 0, 0);
4913 if (size
<= hole_start
)
4917 struct btrfs_ordered_extent
*ordered
;
4919 lock_extent_bits(io_tree
, hole_start
, block_end
- 1,
4921 ordered
= btrfs_lookup_ordered_range(inode
, hole_start
,
4922 block_end
- hole_start
);
4925 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4926 &cached_state
, GFP_NOFS
);
4927 btrfs_start_ordered_extent(inode
, ordered
, 1);
4928 btrfs_put_ordered_extent(ordered
);
4931 cur_offset
= hole_start
;
4933 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4934 block_end
- cur_offset
, 0);
4940 last_byte
= min(extent_map_end(em
), block_end
);
4941 last_byte
= ALIGN(last_byte
, fs_info
->sectorsize
);
4942 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4943 struct extent_map
*hole_em
;
4944 hole_size
= last_byte
- cur_offset
;
4946 err
= maybe_insert_hole(root
, inode
, cur_offset
,
4950 btrfs_drop_extent_cache(inode
, cur_offset
,
4951 cur_offset
+ hole_size
- 1, 0);
4952 hole_em
= alloc_extent_map();
4954 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4955 &BTRFS_I(inode
)->runtime_flags
);
4958 hole_em
->start
= cur_offset
;
4959 hole_em
->len
= hole_size
;
4960 hole_em
->orig_start
= cur_offset
;
4962 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4963 hole_em
->block_len
= 0;
4964 hole_em
->orig_block_len
= 0;
4965 hole_em
->ram_bytes
= hole_size
;
4966 hole_em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
4967 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4968 hole_em
->generation
= fs_info
->generation
;
4971 write_lock(&em_tree
->lock
);
4972 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4973 write_unlock(&em_tree
->lock
);
4976 btrfs_drop_extent_cache(inode
, cur_offset
,
4980 free_extent_map(hole_em
);
4983 free_extent_map(em
);
4985 cur_offset
= last_byte
;
4986 if (cur_offset
>= block_end
)
4989 free_extent_map(em
);
4990 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4995 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4997 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4998 struct btrfs_trans_handle
*trans
;
4999 loff_t oldsize
= i_size_read(inode
);
5000 loff_t newsize
= attr
->ia_size
;
5001 int mask
= attr
->ia_valid
;
5005 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
5006 * special case where we need to update the times despite not having
5007 * these flags set. For all other operations the VFS set these flags
5008 * explicitly if it wants a timestamp update.
5010 if (newsize
!= oldsize
) {
5011 inode_inc_iversion(inode
);
5012 if (!(mask
& (ATTR_CTIME
| ATTR_MTIME
)))
5013 inode
->i_ctime
= inode
->i_mtime
=
5014 current_time(inode
);
5017 if (newsize
> oldsize
) {
5019 * Don't do an expanding truncate while snapshoting is ongoing.
5020 * This is to ensure the snapshot captures a fully consistent
5021 * state of this file - if the snapshot captures this expanding
5022 * truncation, it must capture all writes that happened before
5025 btrfs_wait_for_snapshot_creation(root
);
5026 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
5028 btrfs_end_write_no_snapshoting(root
);
5032 trans
= btrfs_start_transaction(root
, 1);
5033 if (IS_ERR(trans
)) {
5034 btrfs_end_write_no_snapshoting(root
);
5035 return PTR_ERR(trans
);
5038 i_size_write(inode
, newsize
);
5039 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
5040 pagecache_isize_extended(inode
, oldsize
, newsize
);
5041 ret
= btrfs_update_inode(trans
, root
, inode
);
5042 btrfs_end_write_no_snapshoting(root
);
5043 btrfs_end_transaction(trans
);
5047 * We're truncating a file that used to have good data down to
5048 * zero. Make sure it gets into the ordered flush list so that
5049 * any new writes get down to disk quickly.
5052 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
5053 &BTRFS_I(inode
)->runtime_flags
);
5056 * 1 for the orphan item we're going to add
5057 * 1 for the orphan item deletion.
5059 trans
= btrfs_start_transaction(root
, 2);
5061 return PTR_ERR(trans
);
5064 * We need to do this in case we fail at _any_ point during the
5065 * actual truncate. Once we do the truncate_setsize we could
5066 * invalidate pages which forces any outstanding ordered io to
5067 * be instantly completed which will give us extents that need
5068 * to be truncated. If we fail to get an orphan inode down we
5069 * could have left over extents that were never meant to live,
5070 * so we need to guarantee from this point on that everything
5071 * will be consistent.
5073 ret
= btrfs_orphan_add(trans
, inode
);
5074 btrfs_end_transaction(trans
);
5078 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5079 truncate_setsize(inode
, newsize
);
5081 /* Disable nonlocked read DIO to avoid the end less truncate */
5082 btrfs_inode_block_unlocked_dio(inode
);
5083 inode_dio_wait(inode
);
5084 btrfs_inode_resume_unlocked_dio(inode
);
5086 ret
= btrfs_truncate(inode
);
5087 if (ret
&& inode
->i_nlink
) {
5091 * failed to truncate, disk_i_size is only adjusted down
5092 * as we remove extents, so it should represent the true
5093 * size of the inode, so reset the in memory size and
5094 * delete our orphan entry.
5096 trans
= btrfs_join_transaction(root
);
5097 if (IS_ERR(trans
)) {
5098 btrfs_orphan_del(NULL
, inode
);
5101 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
5102 err
= btrfs_orphan_del(trans
, inode
);
5104 btrfs_abort_transaction(trans
, err
);
5105 btrfs_end_transaction(trans
);
5112 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
5114 struct inode
*inode
= d_inode(dentry
);
5115 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5118 if (btrfs_root_readonly(root
))
5121 err
= setattr_prepare(dentry
, attr
);
5125 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
5126 err
= btrfs_setsize(inode
, attr
);
5131 if (attr
->ia_valid
) {
5132 setattr_copy(inode
, attr
);
5133 inode_inc_iversion(inode
);
5134 err
= btrfs_dirty_inode(inode
);
5136 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
5137 err
= posix_acl_chmod(inode
, inode
->i_mode
);
5144 * While truncating the inode pages during eviction, we get the VFS calling
5145 * btrfs_invalidatepage() against each page of the inode. This is slow because
5146 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5147 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5148 * extent_state structures over and over, wasting lots of time.
5150 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5151 * those expensive operations on a per page basis and do only the ordered io
5152 * finishing, while we release here the extent_map and extent_state structures,
5153 * without the excessive merging and splitting.
5155 static void evict_inode_truncate_pages(struct inode
*inode
)
5157 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5158 struct extent_map_tree
*map_tree
= &BTRFS_I(inode
)->extent_tree
;
5159 struct rb_node
*node
;
5161 ASSERT(inode
->i_state
& I_FREEING
);
5162 truncate_inode_pages_final(&inode
->i_data
);
5164 write_lock(&map_tree
->lock
);
5165 while (!RB_EMPTY_ROOT(&map_tree
->map
)) {
5166 struct extent_map
*em
;
5168 node
= rb_first(&map_tree
->map
);
5169 em
= rb_entry(node
, struct extent_map
, rb_node
);
5170 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5171 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
5172 remove_extent_mapping(map_tree
, em
);
5173 free_extent_map(em
);
5174 if (need_resched()) {
5175 write_unlock(&map_tree
->lock
);
5177 write_lock(&map_tree
->lock
);
5180 write_unlock(&map_tree
->lock
);
5183 * Keep looping until we have no more ranges in the io tree.
5184 * We can have ongoing bios started by readpages (called from readahead)
5185 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5186 * still in progress (unlocked the pages in the bio but did not yet
5187 * unlocked the ranges in the io tree). Therefore this means some
5188 * ranges can still be locked and eviction started because before
5189 * submitting those bios, which are executed by a separate task (work
5190 * queue kthread), inode references (inode->i_count) were not taken
5191 * (which would be dropped in the end io callback of each bio).
5192 * Therefore here we effectively end up waiting for those bios and
5193 * anyone else holding locked ranges without having bumped the inode's
5194 * reference count - if we don't do it, when they access the inode's
5195 * io_tree to unlock a range it may be too late, leading to an
5196 * use-after-free issue.
5198 spin_lock(&io_tree
->lock
);
5199 while (!RB_EMPTY_ROOT(&io_tree
->state
)) {
5200 struct extent_state
*state
;
5201 struct extent_state
*cached_state
= NULL
;
5205 node
= rb_first(&io_tree
->state
);
5206 state
= rb_entry(node
, struct extent_state
, rb_node
);
5207 start
= state
->start
;
5209 spin_unlock(&io_tree
->lock
);
5211 lock_extent_bits(io_tree
, start
, end
, &cached_state
);
5214 * If still has DELALLOC flag, the extent didn't reach disk,
5215 * and its reserved space won't be freed by delayed_ref.
5216 * So we need to free its reserved space here.
5217 * (Refer to comment in btrfs_invalidatepage, case 2)
5219 * Note, end is the bytenr of last byte, so we need + 1 here.
5221 if (state
->state
& EXTENT_DELALLOC
)
5222 btrfs_qgroup_free_data(inode
, start
, end
- start
+ 1);
5224 clear_extent_bit(io_tree
, start
, end
,
5225 EXTENT_LOCKED
| EXTENT_DIRTY
|
5226 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
5227 EXTENT_DEFRAG
, 1, 1,
5228 &cached_state
, GFP_NOFS
);
5231 spin_lock(&io_tree
->lock
);
5233 spin_unlock(&io_tree
->lock
);
5236 void btrfs_evict_inode(struct inode
*inode
)
5238 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5239 struct btrfs_trans_handle
*trans
;
5240 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5241 struct btrfs_block_rsv
*rsv
, *global_rsv
;
5242 int steal_from_global
= 0;
5246 trace_btrfs_inode_evict(inode
);
5249 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
5253 min_size
= btrfs_calc_trunc_metadata_size(fs_info
, 1);
5255 evict_inode_truncate_pages(inode
);
5257 if (inode
->i_nlink
&&
5258 ((btrfs_root_refs(&root
->root_item
) != 0 &&
5259 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
5260 btrfs_is_free_space_inode(inode
)))
5263 if (is_bad_inode(inode
)) {
5264 btrfs_orphan_del(NULL
, inode
);
5267 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
5268 if (!special_file(inode
->i_mode
))
5269 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
5271 btrfs_free_io_failure_record(inode
, 0, (u64
)-1);
5273 if (test_bit(BTRFS_FS_LOG_RECOVERING
, &fs_info
->flags
)) {
5274 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
5275 &BTRFS_I(inode
)->runtime_flags
));
5279 if (inode
->i_nlink
> 0) {
5280 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
5281 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
5285 ret
= btrfs_commit_inode_delayed_inode(inode
);
5287 btrfs_orphan_del(NULL
, inode
);
5291 rsv
= btrfs_alloc_block_rsv(fs_info
, BTRFS_BLOCK_RSV_TEMP
);
5293 btrfs_orphan_del(NULL
, inode
);
5296 rsv
->size
= min_size
;
5298 global_rsv
= &fs_info
->global_block_rsv
;
5300 btrfs_i_size_write(inode
, 0);
5303 * This is a bit simpler than btrfs_truncate since we've already
5304 * reserved our space for our orphan item in the unlink, so we just
5305 * need to reserve some slack space in case we add bytes and update
5306 * inode item when doing the truncate.
5309 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
5310 BTRFS_RESERVE_FLUSH_LIMIT
);
5313 * Try and steal from the global reserve since we will
5314 * likely not use this space anyway, we want to try as
5315 * hard as possible to get this to work.
5318 steal_from_global
++;
5320 steal_from_global
= 0;
5324 * steal_from_global == 0: we reserved stuff, hooray!
5325 * steal_from_global == 1: we didn't reserve stuff, boo!
5326 * steal_from_global == 2: we've committed, still not a lot of
5327 * room but maybe we'll have room in the global reserve this
5329 * steal_from_global == 3: abandon all hope!
5331 if (steal_from_global
> 2) {
5333 "Could not get space for a delete, will truncate on mount %d",
5335 btrfs_orphan_del(NULL
, inode
);
5336 btrfs_free_block_rsv(fs_info
, rsv
);
5340 trans
= btrfs_join_transaction(root
);
5341 if (IS_ERR(trans
)) {
5342 btrfs_orphan_del(NULL
, inode
);
5343 btrfs_free_block_rsv(fs_info
, rsv
);
5348 * We can't just steal from the global reserve, we need to make
5349 * sure there is room to do it, if not we need to commit and try
5352 if (steal_from_global
) {
5353 if (!btrfs_check_space_for_delayed_refs(trans
, fs_info
))
5354 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
,
5361 * Couldn't steal from the global reserve, we have too much
5362 * pending stuff built up, commit the transaction and try it
5366 ret
= btrfs_commit_transaction(trans
);
5368 btrfs_orphan_del(NULL
, inode
);
5369 btrfs_free_block_rsv(fs_info
, rsv
);
5374 steal_from_global
= 0;
5377 trans
->block_rsv
= rsv
;
5379 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
5380 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
)
5383 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
5384 btrfs_end_transaction(trans
);
5386 btrfs_btree_balance_dirty(fs_info
);
5389 btrfs_free_block_rsv(fs_info
, rsv
);
5392 * Errors here aren't a big deal, it just means we leave orphan items
5393 * in the tree. They will be cleaned up on the next mount.
5396 trans
->block_rsv
= root
->orphan_block_rsv
;
5397 btrfs_orphan_del(trans
, inode
);
5399 btrfs_orphan_del(NULL
, inode
);
5402 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
5403 if (!(root
== fs_info
->tree_root
||
5404 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
5405 btrfs_return_ino(root
, btrfs_ino(inode
));
5407 btrfs_end_transaction(trans
);
5408 btrfs_btree_balance_dirty(fs_info
);
5410 btrfs_remove_delayed_node(inode
);
5415 * this returns the key found in the dir entry in the location pointer.
5416 * If no dir entries were found, location->objectid is 0.
5418 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
5419 struct btrfs_key
*location
)
5421 const char *name
= dentry
->d_name
.name
;
5422 int namelen
= dentry
->d_name
.len
;
5423 struct btrfs_dir_item
*di
;
5424 struct btrfs_path
*path
;
5425 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5428 path
= btrfs_alloc_path();
5432 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
5437 if (IS_ERR_OR_NULL(di
))
5440 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
5442 btrfs_free_path(path
);
5445 location
->objectid
= 0;
5450 * when we hit a tree root in a directory, the btrfs part of the inode
5451 * needs to be changed to reflect the root directory of the tree root. This
5452 * is kind of like crossing a mount point.
5454 static int fixup_tree_root_location(struct btrfs_fs_info
*fs_info
,
5456 struct dentry
*dentry
,
5457 struct btrfs_key
*location
,
5458 struct btrfs_root
**sub_root
)
5460 struct btrfs_path
*path
;
5461 struct btrfs_root
*new_root
;
5462 struct btrfs_root_ref
*ref
;
5463 struct extent_buffer
*leaf
;
5464 struct btrfs_key key
;
5468 path
= btrfs_alloc_path();
5475 key
.objectid
= BTRFS_I(dir
)->root
->root_key
.objectid
;
5476 key
.type
= BTRFS_ROOT_REF_KEY
;
5477 key
.offset
= location
->objectid
;
5479 ret
= btrfs_search_slot(NULL
, fs_info
->tree_root
, &key
, path
, 0, 0);
5486 leaf
= path
->nodes
[0];
5487 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
5488 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
5489 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
5492 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
5493 (unsigned long)(ref
+ 1),
5494 dentry
->d_name
.len
);
5498 btrfs_release_path(path
);
5500 new_root
= btrfs_read_fs_root_no_name(fs_info
, location
);
5501 if (IS_ERR(new_root
)) {
5502 err
= PTR_ERR(new_root
);
5506 *sub_root
= new_root
;
5507 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
5508 location
->type
= BTRFS_INODE_ITEM_KEY
;
5509 location
->offset
= 0;
5512 btrfs_free_path(path
);
5516 static void inode_tree_add(struct inode
*inode
)
5518 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5519 struct btrfs_inode
*entry
;
5521 struct rb_node
*parent
;
5522 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
5523 u64 ino
= btrfs_ino(inode
);
5525 if (inode_unhashed(inode
))
5528 spin_lock(&root
->inode_lock
);
5529 p
= &root
->inode_tree
.rb_node
;
5532 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
5534 if (ino
< btrfs_ino(&entry
->vfs_inode
))
5535 p
= &parent
->rb_left
;
5536 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
5537 p
= &parent
->rb_right
;
5539 WARN_ON(!(entry
->vfs_inode
.i_state
&
5540 (I_WILL_FREE
| I_FREEING
)));
5541 rb_replace_node(parent
, new, &root
->inode_tree
);
5542 RB_CLEAR_NODE(parent
);
5543 spin_unlock(&root
->inode_lock
);
5547 rb_link_node(new, parent
, p
);
5548 rb_insert_color(new, &root
->inode_tree
);
5549 spin_unlock(&root
->inode_lock
);
5552 static void inode_tree_del(struct inode
*inode
)
5554 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5555 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5558 spin_lock(&root
->inode_lock
);
5559 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
5560 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
5561 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
5562 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5564 spin_unlock(&root
->inode_lock
);
5566 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
5567 synchronize_srcu(&fs_info
->subvol_srcu
);
5568 spin_lock(&root
->inode_lock
);
5569 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5570 spin_unlock(&root
->inode_lock
);
5572 btrfs_add_dead_root(root
);
5576 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
5578 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
5579 struct rb_node
*node
;
5580 struct rb_node
*prev
;
5581 struct btrfs_inode
*entry
;
5582 struct inode
*inode
;
5585 if (!test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
5586 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
5588 spin_lock(&root
->inode_lock
);
5590 node
= root
->inode_tree
.rb_node
;
5594 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5596 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
5597 node
= node
->rb_left
;
5598 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
5599 node
= node
->rb_right
;
5605 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
5606 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
5610 prev
= rb_next(prev
);
5614 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5615 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
5616 inode
= igrab(&entry
->vfs_inode
);
5618 spin_unlock(&root
->inode_lock
);
5619 if (atomic_read(&inode
->i_count
) > 1)
5620 d_prune_aliases(inode
);
5622 * btrfs_drop_inode will have it removed from
5623 * the inode cache when its usage count
5628 spin_lock(&root
->inode_lock
);
5632 if (cond_resched_lock(&root
->inode_lock
))
5635 node
= rb_next(node
);
5637 spin_unlock(&root
->inode_lock
);
5640 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
5642 struct btrfs_iget_args
*args
= p
;
5643 inode
->i_ino
= args
->location
->objectid
;
5644 memcpy(&BTRFS_I(inode
)->location
, args
->location
,
5645 sizeof(*args
->location
));
5646 BTRFS_I(inode
)->root
= args
->root
;
5650 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
5652 struct btrfs_iget_args
*args
= opaque
;
5653 return args
->location
->objectid
== BTRFS_I(inode
)->location
.objectid
&&
5654 args
->root
== BTRFS_I(inode
)->root
;
5657 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
5658 struct btrfs_key
*location
,
5659 struct btrfs_root
*root
)
5661 struct inode
*inode
;
5662 struct btrfs_iget_args args
;
5663 unsigned long hashval
= btrfs_inode_hash(location
->objectid
, root
);
5665 args
.location
= location
;
5668 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
5669 btrfs_init_locked_inode
,
5674 /* Get an inode object given its location and corresponding root.
5675 * Returns in *is_new if the inode was read from disk
5677 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
5678 struct btrfs_root
*root
, int *new)
5680 struct inode
*inode
;
5682 inode
= btrfs_iget_locked(s
, location
, root
);
5684 return ERR_PTR(-ENOMEM
);
5686 if (inode
->i_state
& I_NEW
) {
5689 ret
= btrfs_read_locked_inode(inode
);
5690 if (!is_bad_inode(inode
)) {
5691 inode_tree_add(inode
);
5692 unlock_new_inode(inode
);
5696 unlock_new_inode(inode
);
5699 inode
= ERR_PTR(ret
< 0 ? ret
: -ESTALE
);
5706 static struct inode
*new_simple_dir(struct super_block
*s
,
5707 struct btrfs_key
*key
,
5708 struct btrfs_root
*root
)
5710 struct inode
*inode
= new_inode(s
);
5713 return ERR_PTR(-ENOMEM
);
5715 BTRFS_I(inode
)->root
= root
;
5716 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5717 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5719 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5720 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5721 inode
->i_opflags
&= ~IOP_XATTR
;
5722 inode
->i_fop
= &simple_dir_operations
;
5723 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5724 inode
->i_mtime
= current_time(inode
);
5725 inode
->i_atime
= inode
->i_mtime
;
5726 inode
->i_ctime
= inode
->i_mtime
;
5727 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
5732 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5734 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
5735 struct inode
*inode
;
5736 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5737 struct btrfs_root
*sub_root
= root
;
5738 struct btrfs_key location
;
5742 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5743 return ERR_PTR(-ENAMETOOLONG
);
5745 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5747 return ERR_PTR(ret
);
5749 if (location
.objectid
== 0)
5750 return ERR_PTR(-ENOENT
);
5752 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5753 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5757 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5759 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
5760 ret
= fixup_tree_root_location(fs_info
, dir
, dentry
,
5761 &location
, &sub_root
);
5764 inode
= ERR_PTR(ret
);
5766 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5768 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5770 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
5772 if (!IS_ERR(inode
) && root
!= sub_root
) {
5773 down_read(&fs_info
->cleanup_work_sem
);
5774 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5775 ret
= btrfs_orphan_cleanup(sub_root
);
5776 up_read(&fs_info
->cleanup_work_sem
);
5779 inode
= ERR_PTR(ret
);
5786 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5788 struct btrfs_root
*root
;
5789 struct inode
*inode
= d_inode(dentry
);
5791 if (!inode
&& !IS_ROOT(dentry
))
5792 inode
= d_inode(dentry
->d_parent
);
5795 root
= BTRFS_I(inode
)->root
;
5796 if (btrfs_root_refs(&root
->root_item
) == 0)
5799 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5805 static void btrfs_dentry_release(struct dentry
*dentry
)
5807 kfree(dentry
->d_fsdata
);
5810 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5813 struct inode
*inode
;
5815 inode
= btrfs_lookup_dentry(dir
, dentry
);
5816 if (IS_ERR(inode
)) {
5817 if (PTR_ERR(inode
) == -ENOENT
)
5820 return ERR_CAST(inode
);
5823 return d_splice_alias(inode
, dentry
);
5826 unsigned char btrfs_filetype_table
[] = {
5827 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5830 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
5832 struct inode
*inode
= file_inode(file
);
5833 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
5834 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5835 struct btrfs_item
*item
;
5836 struct btrfs_dir_item
*di
;
5837 struct btrfs_key key
;
5838 struct btrfs_key found_key
;
5839 struct btrfs_path
*path
;
5840 struct list_head ins_list
;
5841 struct list_head del_list
;
5843 struct extent_buffer
*leaf
;
5845 unsigned char d_type
;
5851 struct btrfs_key location
;
5853 if (!dir_emit_dots(file
, ctx
))
5856 path
= btrfs_alloc_path();
5860 path
->reada
= READA_FORWARD
;
5862 INIT_LIST_HEAD(&ins_list
);
5863 INIT_LIST_HEAD(&del_list
);
5864 put
= btrfs_readdir_get_delayed_items(inode
, &ins_list
, &del_list
);
5866 key
.type
= BTRFS_DIR_INDEX_KEY
;
5867 key
.offset
= ctx
->pos
;
5868 key
.objectid
= btrfs_ino(inode
);
5870 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5875 leaf
= path
->nodes
[0];
5876 slot
= path
->slots
[0];
5877 if (slot
>= btrfs_header_nritems(leaf
)) {
5878 ret
= btrfs_next_leaf(root
, path
);
5886 item
= btrfs_item_nr(slot
);
5887 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5889 if (found_key
.objectid
!= key
.objectid
)
5891 if (found_key
.type
!= BTRFS_DIR_INDEX_KEY
)
5893 if (found_key
.offset
< ctx
->pos
)
5895 if (btrfs_should_delete_dir_index(&del_list
, found_key
.offset
))
5898 ctx
->pos
= found_key
.offset
;
5900 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5901 if (verify_dir_item(fs_info
, leaf
, di
))
5904 name_len
= btrfs_dir_name_len(leaf
, di
);
5905 if (name_len
<= sizeof(tmp_name
)) {
5906 name_ptr
= tmp_name
;
5908 name_ptr
= kmalloc(name_len
, GFP_KERNEL
);
5914 read_extent_buffer(leaf
, name_ptr
, (unsigned long)(di
+ 1),
5917 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5918 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5920 over
= !dir_emit(ctx
, name_ptr
, name_len
, location
.objectid
,
5923 if (name_ptr
!= tmp_name
)
5933 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
);
5938 * Stop new entries from being returned after we return the last
5941 * New directory entries are assigned a strictly increasing
5942 * offset. This means that new entries created during readdir
5943 * are *guaranteed* to be seen in the future by that readdir.
5944 * This has broken buggy programs which operate on names as
5945 * they're returned by readdir. Until we re-use freed offsets
5946 * we have this hack to stop new entries from being returned
5947 * under the assumption that they'll never reach this huge
5950 * This is being careful not to overflow 32bit loff_t unless the
5951 * last entry requires it because doing so has broken 32bit apps
5954 if (ctx
->pos
>= INT_MAX
)
5955 ctx
->pos
= LLONG_MAX
;
5962 btrfs_readdir_put_delayed_items(inode
, &ins_list
, &del_list
);
5963 btrfs_free_path(path
);
5967 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5969 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5970 struct btrfs_trans_handle
*trans
;
5972 bool nolock
= false;
5974 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5977 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5980 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5982 trans
= btrfs_join_transaction_nolock(root
);
5984 trans
= btrfs_join_transaction(root
);
5986 return PTR_ERR(trans
);
5987 ret
= btrfs_commit_transaction(trans
);
5993 * This is somewhat expensive, updating the tree every time the
5994 * inode changes. But, it is most likely to find the inode in cache.
5995 * FIXME, needs more benchmarking...there are no reasons other than performance
5996 * to keep or drop this code.
5998 static int btrfs_dirty_inode(struct inode
*inode
)
6000 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
6001 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6002 struct btrfs_trans_handle
*trans
;
6005 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
6008 trans
= btrfs_join_transaction(root
);
6010 return PTR_ERR(trans
);
6012 ret
= btrfs_update_inode(trans
, root
, inode
);
6013 if (ret
&& ret
== -ENOSPC
) {
6014 /* whoops, lets try again with the full transaction */
6015 btrfs_end_transaction(trans
);
6016 trans
= btrfs_start_transaction(root
, 1);
6018 return PTR_ERR(trans
);
6020 ret
= btrfs_update_inode(trans
, root
, inode
);
6022 btrfs_end_transaction(trans
);
6023 if (BTRFS_I(inode
)->delayed_node
)
6024 btrfs_balance_delayed_items(fs_info
);
6030 * This is a copy of file_update_time. We need this so we can return error on
6031 * ENOSPC for updating the inode in the case of file write and mmap writes.
6033 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
6036 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6038 if (btrfs_root_readonly(root
))
6041 if (flags
& S_VERSION
)
6042 inode_inc_iversion(inode
);
6043 if (flags
& S_CTIME
)
6044 inode
->i_ctime
= *now
;
6045 if (flags
& S_MTIME
)
6046 inode
->i_mtime
= *now
;
6047 if (flags
& S_ATIME
)
6048 inode
->i_atime
= *now
;
6049 return btrfs_dirty_inode(inode
);
6053 * find the highest existing sequence number in a directory
6054 * and then set the in-memory index_cnt variable to reflect
6055 * free sequence numbers
6057 static int btrfs_set_inode_index_count(struct inode
*inode
)
6059 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6060 struct btrfs_key key
, found_key
;
6061 struct btrfs_path
*path
;
6062 struct extent_buffer
*leaf
;
6065 key
.objectid
= btrfs_ino(inode
);
6066 key
.type
= BTRFS_DIR_INDEX_KEY
;
6067 key
.offset
= (u64
)-1;
6069 path
= btrfs_alloc_path();
6073 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6076 /* FIXME: we should be able to handle this */
6082 * MAGIC NUMBER EXPLANATION:
6083 * since we search a directory based on f_pos we have to start at 2
6084 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6085 * else has to start at 2
6087 if (path
->slots
[0] == 0) {
6088 BTRFS_I(inode
)->index_cnt
= 2;
6094 leaf
= path
->nodes
[0];
6095 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6097 if (found_key
.objectid
!= btrfs_ino(inode
) ||
6098 found_key
.type
!= BTRFS_DIR_INDEX_KEY
) {
6099 BTRFS_I(inode
)->index_cnt
= 2;
6103 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
6105 btrfs_free_path(path
);
6110 * helper to find a free sequence number in a given directory. This current
6111 * code is very simple, later versions will do smarter things in the btree
6113 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
6117 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
6118 ret
= btrfs_inode_delayed_dir_index_count(dir
);
6120 ret
= btrfs_set_inode_index_count(dir
);
6126 *index
= BTRFS_I(dir
)->index_cnt
;
6127 BTRFS_I(dir
)->index_cnt
++;
6132 static int btrfs_insert_inode_locked(struct inode
*inode
)
6134 struct btrfs_iget_args args
;
6135 args
.location
= &BTRFS_I(inode
)->location
;
6136 args
.root
= BTRFS_I(inode
)->root
;
6138 return insert_inode_locked4(inode
,
6139 btrfs_inode_hash(inode
->i_ino
, BTRFS_I(inode
)->root
),
6140 btrfs_find_actor
, &args
);
6143 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
6144 struct btrfs_root
*root
,
6146 const char *name
, int name_len
,
6147 u64 ref_objectid
, u64 objectid
,
6148 umode_t mode
, u64
*index
)
6150 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
6151 struct inode
*inode
;
6152 struct btrfs_inode_item
*inode_item
;
6153 struct btrfs_key
*location
;
6154 struct btrfs_path
*path
;
6155 struct btrfs_inode_ref
*ref
;
6156 struct btrfs_key key
[2];
6158 int nitems
= name
? 2 : 1;
6162 path
= btrfs_alloc_path();
6164 return ERR_PTR(-ENOMEM
);
6166 inode
= new_inode(fs_info
->sb
);
6168 btrfs_free_path(path
);
6169 return ERR_PTR(-ENOMEM
);
6173 * O_TMPFILE, set link count to 0, so that after this point,
6174 * we fill in an inode item with the correct link count.
6177 set_nlink(inode
, 0);
6180 * we have to initialize this early, so we can reclaim the inode
6181 * number if we fail afterwards in this function.
6183 inode
->i_ino
= objectid
;
6186 trace_btrfs_inode_request(dir
);
6188 ret
= btrfs_set_inode_index(dir
, index
);
6190 btrfs_free_path(path
);
6192 return ERR_PTR(ret
);
6198 * index_cnt is ignored for everything but a dir,
6199 * btrfs_get_inode_index_count has an explanation for the magic
6202 BTRFS_I(inode
)->index_cnt
= 2;
6203 BTRFS_I(inode
)->dir_index
= *index
;
6204 BTRFS_I(inode
)->root
= root
;
6205 BTRFS_I(inode
)->generation
= trans
->transid
;
6206 inode
->i_generation
= BTRFS_I(inode
)->generation
;
6209 * We could have gotten an inode number from somebody who was fsynced
6210 * and then removed in this same transaction, so let's just set full
6211 * sync since it will be a full sync anyway and this will blow away the
6212 * old info in the log.
6214 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
6216 key
[0].objectid
= objectid
;
6217 key
[0].type
= BTRFS_INODE_ITEM_KEY
;
6220 sizes
[0] = sizeof(struct btrfs_inode_item
);
6224 * Start new inodes with an inode_ref. This is slightly more
6225 * efficient for small numbers of hard links since they will
6226 * be packed into one item. Extended refs will kick in if we
6227 * add more hard links than can fit in the ref item.
6229 key
[1].objectid
= objectid
;
6230 key
[1].type
= BTRFS_INODE_REF_KEY
;
6231 key
[1].offset
= ref_objectid
;
6233 sizes
[1] = name_len
+ sizeof(*ref
);
6236 location
= &BTRFS_I(inode
)->location
;
6237 location
->objectid
= objectid
;
6238 location
->offset
= 0;
6239 location
->type
= BTRFS_INODE_ITEM_KEY
;
6241 ret
= btrfs_insert_inode_locked(inode
);
6245 path
->leave_spinning
= 1;
6246 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, nitems
);
6250 inode_init_owner(inode
, dir
, mode
);
6251 inode_set_bytes(inode
, 0);
6253 inode
->i_mtime
= current_time(inode
);
6254 inode
->i_atime
= inode
->i_mtime
;
6255 inode
->i_ctime
= inode
->i_mtime
;
6256 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
6258 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
6259 struct btrfs_inode_item
);
6260 memzero_extent_buffer(path
->nodes
[0], (unsigned long)inode_item
,
6261 sizeof(*inode_item
));
6262 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
6265 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
6266 struct btrfs_inode_ref
);
6267 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
6268 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
6269 ptr
= (unsigned long)(ref
+ 1);
6270 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
6273 btrfs_mark_buffer_dirty(path
->nodes
[0]);
6274 btrfs_free_path(path
);
6276 btrfs_inherit_iflags(inode
, dir
);
6278 if (S_ISREG(mode
)) {
6279 if (btrfs_test_opt(fs_info
, NODATASUM
))
6280 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
6281 if (btrfs_test_opt(fs_info
, NODATACOW
))
6282 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
6283 BTRFS_INODE_NODATASUM
;
6286 inode_tree_add(inode
);
6288 trace_btrfs_inode_new(inode
);
6289 btrfs_set_inode_last_trans(trans
, inode
);
6291 btrfs_update_root_times(trans
, root
);
6293 ret
= btrfs_inode_inherit_props(trans
, inode
, dir
);
6296 "error inheriting props for ino %llu (root %llu): %d",
6297 btrfs_ino(inode
), root
->root_key
.objectid
, ret
);
6302 unlock_new_inode(inode
);
6305 BTRFS_I(dir
)->index_cnt
--;
6306 btrfs_free_path(path
);
6308 return ERR_PTR(ret
);
6311 static inline u8
btrfs_inode_type(struct inode
*inode
)
6313 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
6317 * utility function to add 'inode' into 'parent_inode' with
6318 * a give name and a given sequence number.
6319 * if 'add_backref' is true, also insert a backref from the
6320 * inode to the parent directory.
6322 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
6323 struct inode
*parent_inode
, struct inode
*inode
,
6324 const char *name
, int name_len
, int add_backref
, u64 index
)
6326 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
6328 struct btrfs_key key
;
6329 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
6330 u64 ino
= btrfs_ino(inode
);
6331 u64 parent_ino
= btrfs_ino(parent_inode
);
6333 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6334 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
6337 key
.type
= BTRFS_INODE_ITEM_KEY
;
6341 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6342 ret
= btrfs_add_root_ref(trans
, fs_info
, key
.objectid
,
6343 root
->root_key
.objectid
, parent_ino
,
6344 index
, name
, name_len
);
6345 } else if (add_backref
) {
6346 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
6350 /* Nothing to clean up yet */
6354 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
6356 btrfs_inode_type(inode
), index
);
6357 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
6360 btrfs_abort_transaction(trans
, ret
);
6364 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
6366 inode_inc_iversion(parent_inode
);
6367 parent_inode
->i_mtime
= parent_inode
->i_ctime
=
6368 current_time(parent_inode
);
6369 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
6371 btrfs_abort_transaction(trans
, ret
);
6375 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6378 err
= btrfs_del_root_ref(trans
, fs_info
, key
.objectid
,
6379 root
->root_key
.objectid
, parent_ino
,
6380 &local_index
, name
, name_len
);
6382 } else if (add_backref
) {
6386 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
6387 ino
, parent_ino
, &local_index
);
6392 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
6393 struct inode
*dir
, struct dentry
*dentry
,
6394 struct inode
*inode
, int backref
, u64 index
)
6396 int err
= btrfs_add_link(trans
, dir
, inode
,
6397 dentry
->d_name
.name
, dentry
->d_name
.len
,
6404 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
6405 umode_t mode
, dev_t rdev
)
6407 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6408 struct btrfs_trans_handle
*trans
;
6409 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6410 struct inode
*inode
= NULL
;
6417 * 2 for inode item and ref
6419 * 1 for xattr if selinux is on
6421 trans
= btrfs_start_transaction(root
, 5);
6423 return PTR_ERR(trans
);
6425 err
= btrfs_find_free_ino(root
, &objectid
);
6429 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6430 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6432 if (IS_ERR(inode
)) {
6433 err
= PTR_ERR(inode
);
6438 * If the active LSM wants to access the inode during
6439 * d_instantiate it needs these. Smack checks to see
6440 * if the filesystem supports xattrs by looking at the
6443 inode
->i_op
= &btrfs_special_inode_operations
;
6444 init_special_inode(inode
, inode
->i_mode
, rdev
);
6446 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6448 goto out_unlock_inode
;
6450 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6452 goto out_unlock_inode
;
6454 btrfs_update_inode(trans
, root
, inode
);
6455 unlock_new_inode(inode
);
6456 d_instantiate(dentry
, inode
);
6460 btrfs_end_transaction(trans
);
6461 btrfs_balance_delayed_items(fs_info
);
6462 btrfs_btree_balance_dirty(fs_info
);
6464 inode_dec_link_count(inode
);
6471 unlock_new_inode(inode
);
6476 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
6477 umode_t mode
, bool excl
)
6479 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6480 struct btrfs_trans_handle
*trans
;
6481 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6482 struct inode
*inode
= NULL
;
6483 int drop_inode_on_err
= 0;
6489 * 2 for inode item and ref
6491 * 1 for xattr if selinux is on
6493 trans
= btrfs_start_transaction(root
, 5);
6495 return PTR_ERR(trans
);
6497 err
= btrfs_find_free_ino(root
, &objectid
);
6501 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6502 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6504 if (IS_ERR(inode
)) {
6505 err
= PTR_ERR(inode
);
6508 drop_inode_on_err
= 1;
6510 * If the active LSM wants to access the inode during
6511 * d_instantiate it needs these. Smack checks to see
6512 * if the filesystem supports xattrs by looking at the
6515 inode
->i_fop
= &btrfs_file_operations
;
6516 inode
->i_op
= &btrfs_file_inode_operations
;
6517 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6519 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6521 goto out_unlock_inode
;
6523 err
= btrfs_update_inode(trans
, root
, inode
);
6525 goto out_unlock_inode
;
6527 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6529 goto out_unlock_inode
;
6531 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6532 unlock_new_inode(inode
);
6533 d_instantiate(dentry
, inode
);
6536 btrfs_end_transaction(trans
);
6537 if (err
&& drop_inode_on_err
) {
6538 inode_dec_link_count(inode
);
6541 btrfs_balance_delayed_items(fs_info
);
6542 btrfs_btree_balance_dirty(fs_info
);
6546 unlock_new_inode(inode
);
6551 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
6552 struct dentry
*dentry
)
6554 struct btrfs_trans_handle
*trans
= NULL
;
6555 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6556 struct inode
*inode
= d_inode(old_dentry
);
6557 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
6562 /* do not allow sys_link's with other subvols of the same device */
6563 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
6566 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
6569 err
= btrfs_set_inode_index(dir
, &index
);
6574 * 2 items for inode and inode ref
6575 * 2 items for dir items
6576 * 1 item for parent inode
6578 trans
= btrfs_start_transaction(root
, 5);
6579 if (IS_ERR(trans
)) {
6580 err
= PTR_ERR(trans
);
6585 /* There are several dir indexes for this inode, clear the cache. */
6586 BTRFS_I(inode
)->dir_index
= 0ULL;
6588 inode_inc_iversion(inode
);
6589 inode
->i_ctime
= current_time(inode
);
6591 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
6593 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
6598 struct dentry
*parent
= dentry
->d_parent
;
6599 err
= btrfs_update_inode(trans
, root
, inode
);
6602 if (inode
->i_nlink
== 1) {
6604 * If new hard link count is 1, it's a file created
6605 * with open(2) O_TMPFILE flag.
6607 err
= btrfs_orphan_del(trans
, inode
);
6611 d_instantiate(dentry
, inode
);
6612 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
6615 btrfs_balance_delayed_items(fs_info
);
6618 btrfs_end_transaction(trans
);
6620 inode_dec_link_count(inode
);
6623 btrfs_btree_balance_dirty(fs_info
);
6627 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
6629 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
6630 struct inode
*inode
= NULL
;
6631 struct btrfs_trans_handle
*trans
;
6632 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6634 int drop_on_err
= 0;
6639 * 2 items for inode and ref
6640 * 2 items for dir items
6641 * 1 for xattr if selinux is on
6643 trans
= btrfs_start_transaction(root
, 5);
6645 return PTR_ERR(trans
);
6647 err
= btrfs_find_free_ino(root
, &objectid
);
6651 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6652 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6653 S_IFDIR
| mode
, &index
);
6654 if (IS_ERR(inode
)) {
6655 err
= PTR_ERR(inode
);
6660 /* these must be set before we unlock the inode */
6661 inode
->i_op
= &btrfs_dir_inode_operations
;
6662 inode
->i_fop
= &btrfs_dir_file_operations
;
6664 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6666 goto out_fail_inode
;
6668 btrfs_i_size_write(inode
, 0);
6669 err
= btrfs_update_inode(trans
, root
, inode
);
6671 goto out_fail_inode
;
6673 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
6674 dentry
->d_name
.len
, 0, index
);
6676 goto out_fail_inode
;
6678 d_instantiate(dentry
, inode
);
6680 * mkdir is special. We're unlocking after we call d_instantiate
6681 * to avoid a race with nfsd calling d_instantiate.
6683 unlock_new_inode(inode
);
6687 btrfs_end_transaction(trans
);
6689 inode_dec_link_count(inode
);
6692 btrfs_balance_delayed_items(fs_info
);
6693 btrfs_btree_balance_dirty(fs_info
);
6697 unlock_new_inode(inode
);
6701 /* Find next extent map of a given extent map, caller needs to ensure locks */
6702 static struct extent_map
*next_extent_map(struct extent_map
*em
)
6704 struct rb_node
*next
;
6706 next
= rb_next(&em
->rb_node
);
6709 return container_of(next
, struct extent_map
, rb_node
);
6712 static struct extent_map
*prev_extent_map(struct extent_map
*em
)
6714 struct rb_node
*prev
;
6716 prev
= rb_prev(&em
->rb_node
);
6719 return container_of(prev
, struct extent_map
, rb_node
);
6722 /* helper for btfs_get_extent. Given an existing extent in the tree,
6723 * the existing extent is the nearest extent to map_start,
6724 * and an extent that you want to insert, deal with overlap and insert
6725 * the best fitted new extent into the tree.
6727 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
6728 struct extent_map
*existing
,
6729 struct extent_map
*em
,
6732 struct extent_map
*prev
;
6733 struct extent_map
*next
;
6738 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
6740 if (existing
->start
> map_start
) {
6742 prev
= prev_extent_map(next
);
6745 next
= next_extent_map(prev
);
6748 start
= prev
? extent_map_end(prev
) : em
->start
;
6749 start
= max_t(u64
, start
, em
->start
);
6750 end
= next
? next
->start
: extent_map_end(em
);
6751 end
= min_t(u64
, end
, extent_map_end(em
));
6752 start_diff
= start
- em
->start
;
6754 em
->len
= end
- start
;
6755 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
6756 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
6757 em
->block_start
+= start_diff
;
6758 em
->block_len
-= start_diff
;
6760 return add_extent_mapping(em_tree
, em
, 0);
6763 static noinline
int uncompress_inline(struct btrfs_path
*path
,
6765 size_t pg_offset
, u64 extent_offset
,
6766 struct btrfs_file_extent_item
*item
)
6769 struct extent_buffer
*leaf
= path
->nodes
[0];
6772 unsigned long inline_size
;
6776 WARN_ON(pg_offset
!= 0);
6777 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6778 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6779 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6780 btrfs_item_nr(path
->slots
[0]));
6781 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6784 ptr
= btrfs_file_extent_inline_start(item
);
6786 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6788 max_size
= min_t(unsigned long, PAGE_SIZE
, max_size
);
6789 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6790 extent_offset
, inline_size
, max_size
);
6796 * a bit scary, this does extent mapping from logical file offset to the disk.
6797 * the ugly parts come from merging extents from the disk with the in-ram
6798 * representation. This gets more complex because of the data=ordered code,
6799 * where the in-ram extents might be locked pending data=ordered completion.
6801 * This also copies inline extents directly into the page.
6804 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
6805 size_t pg_offset
, u64 start
, u64 len
,
6808 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
6811 u64 extent_start
= 0;
6813 u64 objectid
= btrfs_ino(inode
);
6815 struct btrfs_path
*path
= NULL
;
6816 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6817 struct btrfs_file_extent_item
*item
;
6818 struct extent_buffer
*leaf
;
6819 struct btrfs_key found_key
;
6820 struct extent_map
*em
= NULL
;
6821 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
6822 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6823 struct btrfs_trans_handle
*trans
= NULL
;
6824 const bool new_inline
= !page
|| create
;
6827 read_lock(&em_tree
->lock
);
6828 em
= lookup_extent_mapping(em_tree
, start
, len
);
6830 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
6831 read_unlock(&em_tree
->lock
);
6834 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6835 free_extent_map(em
);
6836 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6837 free_extent_map(em
);
6841 em
= alloc_extent_map();
6846 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
6847 em
->start
= EXTENT_MAP_HOLE
;
6848 em
->orig_start
= EXTENT_MAP_HOLE
;
6850 em
->block_len
= (u64
)-1;
6853 path
= btrfs_alloc_path();
6859 * Chances are we'll be called again, so go ahead and do
6862 path
->reada
= READA_FORWARD
;
6865 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6866 objectid
, start
, trans
!= NULL
);
6873 if (path
->slots
[0] == 0)
6878 leaf
= path
->nodes
[0];
6879 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6880 struct btrfs_file_extent_item
);
6881 /* are we inside the extent that was found? */
6882 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6883 found_type
= found_key
.type
;
6884 if (found_key
.objectid
!= objectid
||
6885 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6887 * If we backup past the first extent we want to move forward
6888 * and see if there is an extent in front of us, otherwise we'll
6889 * say there is a hole for our whole search range which can
6896 found_type
= btrfs_file_extent_type(leaf
, item
);
6897 extent_start
= found_key
.offset
;
6898 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6899 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6900 extent_end
= extent_start
+
6901 btrfs_file_extent_num_bytes(leaf
, item
);
6902 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6904 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6905 extent_end
= ALIGN(extent_start
+ size
,
6906 fs_info
->sectorsize
);
6909 if (start
>= extent_end
) {
6911 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6912 ret
= btrfs_next_leaf(root
, path
);
6919 leaf
= path
->nodes
[0];
6921 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6922 if (found_key
.objectid
!= objectid
||
6923 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6925 if (start
+ len
<= found_key
.offset
)
6927 if (start
> found_key
.offset
)
6930 em
->orig_start
= start
;
6931 em
->len
= found_key
.offset
- start
;
6935 btrfs_extent_item_to_extent_map(inode
, path
, item
, new_inline
, em
);
6937 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6938 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6940 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6944 size_t extent_offset
;
6950 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6951 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6952 copy_size
= min_t(u64
, PAGE_SIZE
- pg_offset
,
6953 size
- extent_offset
);
6954 em
->start
= extent_start
+ extent_offset
;
6955 em
->len
= ALIGN(copy_size
, fs_info
->sectorsize
);
6956 em
->orig_block_len
= em
->len
;
6957 em
->orig_start
= em
->start
;
6958 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6959 if (create
== 0 && !PageUptodate(page
)) {
6960 if (btrfs_file_extent_compression(leaf
, item
) !=
6961 BTRFS_COMPRESS_NONE
) {
6962 ret
= uncompress_inline(path
, page
, pg_offset
,
6963 extent_offset
, item
);
6970 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6972 if (pg_offset
+ copy_size
< PAGE_SIZE
) {
6973 memset(map
+ pg_offset
+ copy_size
, 0,
6974 PAGE_SIZE
- pg_offset
-
6979 flush_dcache_page(page
);
6980 } else if (create
&& PageUptodate(page
)) {
6984 free_extent_map(em
);
6987 btrfs_release_path(path
);
6988 trans
= btrfs_join_transaction(root
);
6991 return ERR_CAST(trans
);
6995 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6998 btrfs_mark_buffer_dirty(leaf
);
7000 set_extent_uptodate(io_tree
, em
->start
,
7001 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
7006 em
->orig_start
= start
;
7009 em
->block_start
= EXTENT_MAP_HOLE
;
7010 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
7012 btrfs_release_path(path
);
7013 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
7015 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7016 em
->start
, em
->len
, start
, len
);
7022 write_lock(&em_tree
->lock
);
7023 ret
= add_extent_mapping(em_tree
, em
, 0);
7024 /* it is possible that someone inserted the extent into the tree
7025 * while we had the lock dropped. It is also possible that
7026 * an overlapping map exists in the tree
7028 if (ret
== -EEXIST
) {
7029 struct extent_map
*existing
;
7033 existing
= search_extent_mapping(em_tree
, start
, len
);
7035 * existing will always be non-NULL, since there must be
7036 * extent causing the -EEXIST.
7038 if (existing
->start
== em
->start
&&
7039 extent_map_end(existing
) >= extent_map_end(em
) &&
7040 em
->block_start
== existing
->block_start
) {
7042 * The existing extent map already encompasses the
7043 * entire extent map we tried to add.
7045 free_extent_map(em
);
7049 } else if (start
>= extent_map_end(existing
) ||
7050 start
<= existing
->start
) {
7052 * The existing extent map is the one nearest to
7053 * the [start, start + len) range which overlaps
7055 err
= merge_extent_mapping(em_tree
, existing
,
7057 free_extent_map(existing
);
7059 free_extent_map(em
);
7063 free_extent_map(em
);
7068 write_unlock(&em_tree
->lock
);
7071 trace_btrfs_get_extent(root
, inode
, em
);
7073 btrfs_free_path(path
);
7075 ret
= btrfs_end_transaction(trans
);
7080 free_extent_map(em
);
7081 return ERR_PTR(err
);
7083 BUG_ON(!em
); /* Error is always set */
7087 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
7088 size_t pg_offset
, u64 start
, u64 len
,
7091 struct extent_map
*em
;
7092 struct extent_map
*hole_em
= NULL
;
7093 u64 range_start
= start
;
7099 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
7106 * - a pre-alloc extent,
7107 * there might actually be delalloc bytes behind it.
7109 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
7110 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7116 /* check to see if we've wrapped (len == -1 or similar) */
7125 /* ok, we didn't find anything, lets look for delalloc */
7126 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
7127 end
, len
, EXTENT_DELALLOC
, 1);
7128 found_end
= range_start
+ found
;
7129 if (found_end
< range_start
)
7130 found_end
= (u64
)-1;
7133 * we didn't find anything useful, return
7134 * the original results from get_extent()
7136 if (range_start
> end
|| found_end
<= start
) {
7142 /* adjust the range_start to make sure it doesn't
7143 * go backwards from the start they passed in
7145 range_start
= max(start
, range_start
);
7146 found
= found_end
- range_start
;
7149 u64 hole_start
= start
;
7152 em
= alloc_extent_map();
7158 * when btrfs_get_extent can't find anything it
7159 * returns one huge hole
7161 * make sure what it found really fits our range, and
7162 * adjust to make sure it is based on the start from
7166 u64 calc_end
= extent_map_end(hole_em
);
7168 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
7169 free_extent_map(hole_em
);
7172 hole_start
= max(hole_em
->start
, start
);
7173 hole_len
= calc_end
- hole_start
;
7177 if (hole_em
&& range_start
> hole_start
) {
7178 /* our hole starts before our delalloc, so we
7179 * have to return just the parts of the hole
7180 * that go until the delalloc starts
7182 em
->len
= min(hole_len
,
7183 range_start
- hole_start
);
7184 em
->start
= hole_start
;
7185 em
->orig_start
= hole_start
;
7187 * don't adjust block start at all,
7188 * it is fixed at EXTENT_MAP_HOLE
7190 em
->block_start
= hole_em
->block_start
;
7191 em
->block_len
= hole_len
;
7192 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
7193 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
7195 em
->start
= range_start
;
7197 em
->orig_start
= range_start
;
7198 em
->block_start
= EXTENT_MAP_DELALLOC
;
7199 em
->block_len
= found
;
7201 } else if (hole_em
) {
7206 free_extent_map(hole_em
);
7208 free_extent_map(em
);
7209 return ERR_PTR(err
);
7214 static struct extent_map
*btrfs_create_dio_extent(struct inode
*inode
,
7217 const u64 orig_start
,
7218 const u64 block_start
,
7219 const u64 block_len
,
7220 const u64 orig_block_len
,
7221 const u64 ram_bytes
,
7224 struct extent_map
*em
= NULL
;
7227 if (type
!= BTRFS_ORDERED_NOCOW
) {
7228 em
= create_pinned_em(inode
, start
, len
, orig_start
,
7229 block_start
, block_len
, orig_block_len
,
7234 ret
= btrfs_add_ordered_extent_dio(inode
, start
, block_start
,
7235 len
, block_len
, type
);
7238 free_extent_map(em
);
7239 btrfs_drop_extent_cache(inode
, start
,
7240 start
+ len
- 1, 0);
7249 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
7252 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7253 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7254 struct extent_map
*em
;
7255 struct btrfs_key ins
;
7259 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
7260 ret
= btrfs_reserve_extent(root
, len
, len
, fs_info
->sectorsize
,
7261 0, alloc_hint
, &ins
, 1, 1);
7263 return ERR_PTR(ret
);
7265 em
= btrfs_create_dio_extent(inode
, start
, ins
.offset
, start
,
7266 ins
.objectid
, ins
.offset
, ins
.offset
,
7268 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
7270 btrfs_free_reserved_extent(fs_info
, ins
.objectid
,
7277 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7278 * block must be cow'd
7280 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
7281 u64
*orig_start
, u64
*orig_block_len
,
7284 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7285 struct btrfs_trans_handle
*trans
;
7286 struct btrfs_path
*path
;
7288 struct extent_buffer
*leaf
;
7289 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7290 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7291 struct btrfs_file_extent_item
*fi
;
7292 struct btrfs_key key
;
7299 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
7301 path
= btrfs_alloc_path();
7305 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, btrfs_ino(inode
),
7310 slot
= path
->slots
[0];
7313 /* can't find the item, must cow */
7320 leaf
= path
->nodes
[0];
7321 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
7322 if (key
.objectid
!= btrfs_ino(inode
) ||
7323 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
7324 /* not our file or wrong item type, must cow */
7328 if (key
.offset
> offset
) {
7329 /* Wrong offset, must cow */
7333 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
7334 found_type
= btrfs_file_extent_type(leaf
, fi
);
7335 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
7336 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
7337 /* not a regular extent, must cow */
7341 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
7344 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
7345 if (extent_end
<= offset
)
7348 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
7349 if (disk_bytenr
== 0)
7352 if (btrfs_file_extent_compression(leaf
, fi
) ||
7353 btrfs_file_extent_encryption(leaf
, fi
) ||
7354 btrfs_file_extent_other_encoding(leaf
, fi
))
7357 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
7360 *orig_start
= key
.offset
- backref_offset
;
7361 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
7362 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
7365 if (btrfs_extent_readonly(fs_info
, disk_bytenr
))
7368 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
7369 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
7372 range_end
= round_up(offset
+ num_bytes
,
7373 root
->fs_info
->sectorsize
) - 1;
7374 ret
= test_range_bit(io_tree
, offset
, range_end
,
7375 EXTENT_DELALLOC
, 0, NULL
);
7382 btrfs_release_path(path
);
7385 * look for other files referencing this extent, if we
7386 * find any we must cow
7388 trans
= btrfs_join_transaction(root
);
7389 if (IS_ERR(trans
)) {
7394 ret
= btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
7395 key
.offset
- backref_offset
, disk_bytenr
);
7396 btrfs_end_transaction(trans
);
7403 * adjust disk_bytenr and num_bytes to cover just the bytes
7404 * in this extent we are about to write. If there
7405 * are any csums in that range we have to cow in order
7406 * to keep the csums correct
7408 disk_bytenr
+= backref_offset
;
7409 disk_bytenr
+= offset
- key
.offset
;
7410 if (csum_exist_in_range(fs_info
, disk_bytenr
, num_bytes
))
7413 * all of the above have passed, it is safe to overwrite this extent
7419 btrfs_free_path(path
);
7423 bool btrfs_page_exists_in_range(struct inode
*inode
, loff_t start
, loff_t end
)
7425 struct radix_tree_root
*root
= &inode
->i_mapping
->page_tree
;
7427 void **pagep
= NULL
;
7428 struct page
*page
= NULL
;
7432 start_idx
= start
>> PAGE_SHIFT
;
7435 * end is the last byte in the last page. end == start is legal
7437 end_idx
= end
>> PAGE_SHIFT
;
7441 /* Most of the code in this while loop is lifted from
7442 * find_get_page. It's been modified to begin searching from a
7443 * page and return just the first page found in that range. If the
7444 * found idx is less than or equal to the end idx then we know that
7445 * a page exists. If no pages are found or if those pages are
7446 * outside of the range then we're fine (yay!) */
7447 while (page
== NULL
&&
7448 radix_tree_gang_lookup_slot(root
, &pagep
, NULL
, start_idx
, 1)) {
7449 page
= radix_tree_deref_slot(pagep
);
7450 if (unlikely(!page
))
7453 if (radix_tree_exception(page
)) {
7454 if (radix_tree_deref_retry(page
)) {
7459 * Otherwise, shmem/tmpfs must be storing a swap entry
7460 * here as an exceptional entry: so return it without
7461 * attempting to raise page count.
7464 break; /* TODO: Is this relevant for this use case? */
7467 if (!page_cache_get_speculative(page
)) {
7473 * Has the page moved?
7474 * This is part of the lockless pagecache protocol. See
7475 * include/linux/pagemap.h for details.
7477 if (unlikely(page
!= *pagep
)) {
7484 if (page
->index
<= end_idx
)
7493 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
7494 struct extent_state
**cached_state
, int writing
)
7496 struct btrfs_ordered_extent
*ordered
;
7500 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7503 * We're concerned with the entire range that we're going to be
7504 * doing DIO to, so we need to make sure there's no ordered
7505 * extents in this range.
7507 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
7508 lockend
- lockstart
+ 1);
7511 * We need to make sure there are no buffered pages in this
7512 * range either, we could have raced between the invalidate in
7513 * generic_file_direct_write and locking the extent. The
7514 * invalidate needs to happen so that reads after a write do not
7519 !btrfs_page_exists_in_range(inode
, lockstart
, lockend
)))
7522 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7523 cached_state
, GFP_NOFS
);
7527 * If we are doing a DIO read and the ordered extent we
7528 * found is for a buffered write, we can not wait for it
7529 * to complete and retry, because if we do so we can
7530 * deadlock with concurrent buffered writes on page
7531 * locks. This happens only if our DIO read covers more
7532 * than one extent map, if at this point has already
7533 * created an ordered extent for a previous extent map
7534 * and locked its range in the inode's io tree, and a
7535 * concurrent write against that previous extent map's
7536 * range and this range started (we unlock the ranges
7537 * in the io tree only when the bios complete and
7538 * buffered writes always lock pages before attempting
7539 * to lock range in the io tree).
7542 test_bit(BTRFS_ORDERED_DIRECT
, &ordered
->flags
))
7543 btrfs_start_ordered_extent(inode
, ordered
, 1);
7546 btrfs_put_ordered_extent(ordered
);
7549 * We could trigger writeback for this range (and wait
7550 * for it to complete) and then invalidate the pages for
7551 * this range (through invalidate_inode_pages2_range()),
7552 * but that can lead us to a deadlock with a concurrent
7553 * call to readpages() (a buffered read or a defrag call
7554 * triggered a readahead) on a page lock due to an
7555 * ordered dio extent we created before but did not have
7556 * yet a corresponding bio submitted (whence it can not
7557 * complete), which makes readpages() wait for that
7558 * ordered extent to complete while holding a lock on
7573 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
7574 u64 len
, u64 orig_start
,
7575 u64 block_start
, u64 block_len
,
7576 u64 orig_block_len
, u64 ram_bytes
,
7579 struct extent_map_tree
*em_tree
;
7580 struct extent_map
*em
;
7581 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7584 em_tree
= &BTRFS_I(inode
)->extent_tree
;
7585 em
= alloc_extent_map();
7587 return ERR_PTR(-ENOMEM
);
7590 em
->orig_start
= orig_start
;
7591 em
->mod_start
= start
;
7594 em
->block_len
= block_len
;
7595 em
->block_start
= block_start
;
7596 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
7597 em
->orig_block_len
= orig_block_len
;
7598 em
->ram_bytes
= ram_bytes
;
7599 em
->generation
= -1;
7600 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
7601 if (type
== BTRFS_ORDERED_PREALLOC
)
7602 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
7605 btrfs_drop_extent_cache(inode
, em
->start
,
7606 em
->start
+ em
->len
- 1, 0);
7607 write_lock(&em_tree
->lock
);
7608 ret
= add_extent_mapping(em_tree
, em
, 1);
7609 write_unlock(&em_tree
->lock
);
7610 } while (ret
== -EEXIST
);
7613 free_extent_map(em
);
7614 return ERR_PTR(ret
);
7620 static void adjust_dio_outstanding_extents(struct inode
*inode
,
7621 struct btrfs_dio_data
*dio_data
,
7624 unsigned num_extents
;
7626 num_extents
= (unsigned) div64_u64(len
+ BTRFS_MAX_EXTENT_SIZE
- 1,
7627 BTRFS_MAX_EXTENT_SIZE
);
7629 * If we have an outstanding_extents count still set then we're
7630 * within our reservation, otherwise we need to adjust our inode
7631 * counter appropriately.
7633 if (dio_data
->outstanding_extents
>= num_extents
) {
7634 dio_data
->outstanding_extents
-= num_extents
;
7637 * If dio write length has been split due to no large enough
7638 * contiguous space, we need to compensate our inode counter
7641 u64 num_needed
= num_extents
- dio_data
->outstanding_extents
;
7643 spin_lock(&BTRFS_I(inode
)->lock
);
7644 BTRFS_I(inode
)->outstanding_extents
+= num_needed
;
7645 spin_unlock(&BTRFS_I(inode
)->lock
);
7649 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
7650 struct buffer_head
*bh_result
, int create
)
7652 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7653 struct extent_map
*em
;
7654 struct extent_state
*cached_state
= NULL
;
7655 struct btrfs_dio_data
*dio_data
= NULL
;
7656 u64 start
= iblock
<< inode
->i_blkbits
;
7657 u64 lockstart
, lockend
;
7658 u64 len
= bh_result
->b_size
;
7659 int unlock_bits
= EXTENT_LOCKED
;
7663 unlock_bits
|= EXTENT_DIRTY
;
7665 len
= min_t(u64
, len
, fs_info
->sectorsize
);
7668 lockend
= start
+ len
- 1;
7670 if (current
->journal_info
) {
7672 * Need to pull our outstanding extents and set journal_info to NULL so
7673 * that anything that needs to check if there's a transaction doesn't get
7676 dio_data
= current
->journal_info
;
7677 current
->journal_info
= NULL
;
7681 * If this errors out it's because we couldn't invalidate pagecache for
7682 * this range and we need to fallback to buffered.
7684 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
,
7690 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
7697 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7698 * io. INLINE is special, and we could probably kludge it in here, but
7699 * it's still buffered so for safety lets just fall back to the generic
7702 * For COMPRESSED we _have_ to read the entire extent in so we can
7703 * decompress it, so there will be buffering required no matter what we
7704 * do, so go ahead and fallback to buffered.
7706 * We return -ENOTBLK because that's what makes DIO go ahead and go back
7707 * to buffered IO. Don't blame me, this is the price we pay for using
7710 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
7711 em
->block_start
== EXTENT_MAP_INLINE
) {
7712 free_extent_map(em
);
7717 /* Just a good old fashioned hole, return */
7718 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
7719 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
7720 free_extent_map(em
);
7725 * We don't allocate a new extent in the following cases
7727 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7729 * 2) The extent is marked as PREALLOC. We're good to go here and can
7730 * just use the extent.
7734 len
= min(len
, em
->len
- (start
- em
->start
));
7735 lockstart
= start
+ len
;
7739 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
7740 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
7741 em
->block_start
!= EXTENT_MAP_HOLE
)) {
7743 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
7745 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7746 type
= BTRFS_ORDERED_PREALLOC
;
7748 type
= BTRFS_ORDERED_NOCOW
;
7749 len
= min(len
, em
->len
- (start
- em
->start
));
7750 block_start
= em
->block_start
+ (start
- em
->start
);
7752 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
7753 &orig_block_len
, &ram_bytes
) == 1 &&
7754 btrfs_inc_nocow_writers(fs_info
, block_start
)) {
7755 struct extent_map
*em2
;
7757 em2
= btrfs_create_dio_extent(inode
, start
, len
,
7758 orig_start
, block_start
,
7759 len
, orig_block_len
,
7761 btrfs_dec_nocow_writers(fs_info
, block_start
);
7762 if (type
== BTRFS_ORDERED_PREALLOC
) {
7763 free_extent_map(em
);
7766 if (em2
&& IS_ERR(em2
)) {
7771 * For inode marked NODATACOW or extent marked PREALLOC,
7772 * use the existing or preallocated extent, so does not
7773 * need to adjust btrfs_space_info's bytes_may_use.
7775 btrfs_free_reserved_data_space_noquota(inode
,
7782 * this will cow the extent, reset the len in case we changed
7785 len
= bh_result
->b_size
;
7786 free_extent_map(em
);
7787 em
= btrfs_new_extent_direct(inode
, start
, len
);
7792 len
= min(len
, em
->len
- (start
- em
->start
));
7794 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
7796 bh_result
->b_size
= len
;
7797 bh_result
->b_bdev
= em
->bdev
;
7798 set_buffer_mapped(bh_result
);
7800 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7801 set_buffer_new(bh_result
);
7804 * Need to update the i_size under the extent lock so buffered
7805 * readers will get the updated i_size when we unlock.
7807 if (start
+ len
> i_size_read(inode
))
7808 i_size_write(inode
, start
+ len
);
7810 adjust_dio_outstanding_extents(inode
, dio_data
, len
);
7811 WARN_ON(dio_data
->reserve
< len
);
7812 dio_data
->reserve
-= len
;
7813 dio_data
->unsubmitted_oe_range_end
= start
+ len
;
7814 current
->journal_info
= dio_data
;
7818 * In the case of write we need to clear and unlock the entire range,
7819 * in the case of read we need to unlock only the end area that we
7820 * aren't using if there is any left over space.
7822 if (lockstart
< lockend
) {
7823 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
7824 lockend
, unlock_bits
, 1, 0,
7825 &cached_state
, GFP_NOFS
);
7827 free_extent_state(cached_state
);
7830 free_extent_map(em
);
7835 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7836 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
7839 current
->journal_info
= dio_data
;
7841 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7842 * write less data then expected, so that we don't underflow our inode's
7843 * outstanding extents counter.
7845 if (create
&& dio_data
)
7846 adjust_dio_outstanding_extents(inode
, dio_data
, len
);
7851 static inline int submit_dio_repair_bio(struct inode
*inode
, struct bio
*bio
,
7854 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7857 BUG_ON(bio_op(bio
) == REQ_OP_WRITE
);
7861 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, BTRFS_WQ_ENDIO_DIO_REPAIR
);
7865 ret
= btrfs_map_bio(fs_info
, bio
, mirror_num
, 0);
7871 static int btrfs_check_dio_repairable(struct inode
*inode
,
7872 struct bio
*failed_bio
,
7873 struct io_failure_record
*failrec
,
7876 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7879 num_copies
= btrfs_num_copies(fs_info
, failrec
->logical
, failrec
->len
);
7880 if (num_copies
== 1) {
7882 * we only have a single copy of the data, so don't bother with
7883 * all the retry and error correction code that follows. no
7884 * matter what the error is, it is very likely to persist.
7886 btrfs_debug(fs_info
,
7887 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7888 num_copies
, failrec
->this_mirror
, failed_mirror
);
7892 failrec
->failed_mirror
= failed_mirror
;
7893 failrec
->this_mirror
++;
7894 if (failrec
->this_mirror
== failed_mirror
)
7895 failrec
->this_mirror
++;
7897 if (failrec
->this_mirror
> num_copies
) {
7898 btrfs_debug(fs_info
,
7899 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7900 num_copies
, failrec
->this_mirror
, failed_mirror
);
7907 static int dio_read_error(struct inode
*inode
, struct bio
*failed_bio
,
7908 struct page
*page
, unsigned int pgoff
,
7909 u64 start
, u64 end
, int failed_mirror
,
7910 bio_end_io_t
*repair_endio
, void *repair_arg
)
7912 struct io_failure_record
*failrec
;
7918 BUG_ON(bio_op(failed_bio
) == REQ_OP_WRITE
);
7920 ret
= btrfs_get_io_failure_record(inode
, start
, end
, &failrec
);
7924 ret
= btrfs_check_dio_repairable(inode
, failed_bio
, failrec
,
7927 free_io_failure(inode
, failrec
);
7931 if ((failed_bio
->bi_vcnt
> 1)
7932 || (failed_bio
->bi_io_vec
->bv_len
7933 > btrfs_inode_sectorsize(inode
)))
7934 read_mode
|= REQ_FAILFAST_DEV
;
7936 isector
= start
- btrfs_io_bio(failed_bio
)->logical
;
7937 isector
>>= inode
->i_sb
->s_blocksize_bits
;
7938 bio
= btrfs_create_repair_bio(inode
, failed_bio
, failrec
, page
,
7939 pgoff
, isector
, repair_endio
, repair_arg
);
7941 free_io_failure(inode
, failrec
);
7944 bio_set_op_attrs(bio
, REQ_OP_READ
, read_mode
);
7946 btrfs_debug(BTRFS_I(inode
)->root
->fs_info
,
7947 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7948 read_mode
, failrec
->this_mirror
, failrec
->in_validation
);
7950 ret
= submit_dio_repair_bio(inode
, bio
, failrec
->this_mirror
);
7952 free_io_failure(inode
, failrec
);
7959 struct btrfs_retry_complete
{
7960 struct completion done
;
7961 struct inode
*inode
;
7966 static void btrfs_retry_endio_nocsum(struct bio
*bio
)
7968 struct btrfs_retry_complete
*done
= bio
->bi_private
;
7969 struct inode
*inode
;
7970 struct bio_vec
*bvec
;
7976 ASSERT(bio
->bi_vcnt
== 1);
7977 inode
= bio
->bi_io_vec
->bv_page
->mapping
->host
;
7978 ASSERT(bio
->bi_io_vec
->bv_len
== btrfs_inode_sectorsize(inode
));
7981 bio_for_each_segment_all(bvec
, bio
, i
)
7982 clean_io_failure(done
->inode
, done
->start
, bvec
->bv_page
, 0);
7984 complete(&done
->done
);
7988 static int __btrfs_correct_data_nocsum(struct inode
*inode
,
7989 struct btrfs_io_bio
*io_bio
)
7991 struct btrfs_fs_info
*fs_info
;
7992 struct bio_vec
*bvec
;
7993 struct btrfs_retry_complete done
;
8001 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
8002 sectorsize
= fs_info
->sectorsize
;
8004 start
= io_bio
->logical
;
8007 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
8008 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
->bv_len
);
8009 pgoff
= bvec
->bv_offset
;
8011 next_block_or_try_again
:
8014 init_completion(&done
.done
);
8016 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
,
8017 pgoff
, start
, start
+ sectorsize
- 1,
8019 btrfs_retry_endio_nocsum
, &done
);
8023 wait_for_completion(&done
.done
);
8025 if (!done
.uptodate
) {
8026 /* We might have another mirror, so try again */
8027 goto next_block_or_try_again
;
8030 start
+= sectorsize
;
8033 pgoff
+= sectorsize
;
8034 goto next_block_or_try_again
;
8041 static void btrfs_retry_endio(struct bio
*bio
)
8043 struct btrfs_retry_complete
*done
= bio
->bi_private
;
8044 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8045 struct inode
*inode
;
8046 struct bio_vec
*bvec
;
8057 start
= done
->start
;
8059 ASSERT(bio
->bi_vcnt
== 1);
8060 inode
= bio
->bi_io_vec
->bv_page
->mapping
->host
;
8061 ASSERT(bio
->bi_io_vec
->bv_len
== btrfs_inode_sectorsize(inode
));
8063 bio_for_each_segment_all(bvec
, bio
, i
) {
8064 ret
= __readpage_endio_check(done
->inode
, io_bio
, i
,
8065 bvec
->bv_page
, bvec
->bv_offset
,
8066 done
->start
, bvec
->bv_len
);
8068 clean_io_failure(done
->inode
, done
->start
,
8069 bvec
->bv_page
, bvec
->bv_offset
);
8074 done
->uptodate
= uptodate
;
8076 complete(&done
->done
);
8080 static int __btrfs_subio_endio_read(struct inode
*inode
,
8081 struct btrfs_io_bio
*io_bio
, int err
)
8083 struct btrfs_fs_info
*fs_info
;
8084 struct bio_vec
*bvec
;
8085 struct btrfs_retry_complete done
;
8095 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
8096 sectorsize
= fs_info
->sectorsize
;
8099 start
= io_bio
->logical
;
8102 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
8103 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
->bv_len
);
8105 pgoff
= bvec
->bv_offset
;
8107 csum_pos
= BTRFS_BYTES_TO_BLKS(fs_info
, offset
);
8108 ret
= __readpage_endio_check(inode
, io_bio
, csum_pos
,
8109 bvec
->bv_page
, pgoff
, start
,
8116 init_completion(&done
.done
);
8118 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
,
8119 pgoff
, start
, start
+ sectorsize
- 1,
8121 btrfs_retry_endio
, &done
);
8127 wait_for_completion(&done
.done
);
8129 if (!done
.uptodate
) {
8130 /* We might have another mirror, so try again */
8134 offset
+= sectorsize
;
8135 start
+= sectorsize
;
8140 pgoff
+= sectorsize
;
8148 static int btrfs_subio_endio_read(struct inode
*inode
,
8149 struct btrfs_io_bio
*io_bio
, int err
)
8151 bool skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
8155 return __btrfs_correct_data_nocsum(inode
, io_bio
);
8159 return __btrfs_subio_endio_read(inode
, io_bio
, err
);
8163 static void btrfs_endio_direct_read(struct bio
*bio
)
8165 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8166 struct inode
*inode
= dip
->inode
;
8167 struct bio
*dio_bio
;
8168 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8169 int err
= bio
->bi_error
;
8171 if (dip
->flags
& BTRFS_DIO_ORIG_BIO_SUBMITTED
)
8172 err
= btrfs_subio_endio_read(inode
, io_bio
, err
);
8174 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
8175 dip
->logical_offset
+ dip
->bytes
- 1);
8176 dio_bio
= dip
->dio_bio
;
8180 dio_bio
->bi_error
= bio
->bi_error
;
8181 dio_end_io(dio_bio
, bio
->bi_error
);
8184 io_bio
->end_io(io_bio
, err
);
8188 static void btrfs_endio_direct_write_update_ordered(struct inode
*inode
,
8193 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8194 struct btrfs_ordered_extent
*ordered
= NULL
;
8195 u64 ordered_offset
= offset
;
8196 u64 ordered_bytes
= bytes
;
8200 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
8207 btrfs_init_work(&ordered
->work
, btrfs_endio_write_helper
,
8208 finish_ordered_fn
, NULL
, NULL
);
8209 btrfs_queue_work(fs_info
->endio_write_workers
, &ordered
->work
);
8212 * our bio might span multiple ordered extents. If we haven't
8213 * completed the accounting for the whole dio, go back and try again
8215 if (ordered_offset
< offset
+ bytes
) {
8216 ordered_bytes
= offset
+ bytes
- ordered_offset
;
8222 static void btrfs_endio_direct_write(struct bio
*bio
)
8224 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8225 struct bio
*dio_bio
= dip
->dio_bio
;
8227 btrfs_endio_direct_write_update_ordered(dip
->inode
,
8228 dip
->logical_offset
,
8234 dio_bio
->bi_error
= bio
->bi_error
;
8235 dio_end_io(dio_bio
, bio
->bi_error
);
8239 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
,
8240 struct bio
*bio
, int mirror_num
,
8241 unsigned long bio_flags
, u64 offset
)
8244 ret
= btrfs_csum_one_bio(inode
, bio
, offset
, 1);
8245 BUG_ON(ret
); /* -ENOMEM */
8249 static void btrfs_end_dio_bio(struct bio
*bio
)
8251 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8252 int err
= bio
->bi_error
;
8255 btrfs_warn(BTRFS_I(dip
->inode
)->root
->fs_info
,
8256 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
8257 btrfs_ino(dip
->inode
), bio_op(bio
), bio
->bi_opf
,
8258 (unsigned long long)bio
->bi_iter
.bi_sector
,
8259 bio
->bi_iter
.bi_size
, err
);
8261 if (dip
->subio_endio
)
8262 err
= dip
->subio_endio(dip
->inode
, btrfs_io_bio(bio
), err
);
8268 * before atomic variable goto zero, we must make sure
8269 * dip->errors is perceived to be set.
8271 smp_mb__before_atomic();
8274 /* if there are more bios still pending for this dio, just exit */
8275 if (!atomic_dec_and_test(&dip
->pending_bios
))
8279 bio_io_error(dip
->orig_bio
);
8281 dip
->dio_bio
->bi_error
= 0;
8282 bio_endio(dip
->orig_bio
);
8288 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
8289 u64 first_sector
, gfp_t gfp_flags
)
8292 bio
= btrfs_bio_alloc(bdev
, first_sector
, BIO_MAX_PAGES
, gfp_flags
);
8294 bio_associate_current(bio
);
8298 static inline int btrfs_lookup_and_bind_dio_csum(struct inode
*inode
,
8299 struct btrfs_dio_private
*dip
,
8303 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8304 struct btrfs_io_bio
*orig_io_bio
= btrfs_io_bio(dip
->orig_bio
);
8308 * We load all the csum data we need when we submit
8309 * the first bio to reduce the csum tree search and
8312 if (dip
->logical_offset
== file_offset
) {
8313 ret
= btrfs_lookup_bio_sums_dio(inode
, dip
->orig_bio
,
8319 if (bio
== dip
->orig_bio
)
8322 file_offset
-= dip
->logical_offset
;
8323 file_offset
>>= inode
->i_sb
->s_blocksize_bits
;
8324 io_bio
->csum
= (u8
*)(((u32
*)orig_io_bio
->csum
) + file_offset
);
8329 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
8330 u64 file_offset
, int skip_sum
,
8333 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8334 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8335 bool write
= bio_op(bio
) == REQ_OP_WRITE
;
8339 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
8344 ret
= btrfs_bio_wq_end_io(fs_info
, bio
, BTRFS_WQ_ENDIO_DATA
);
8352 if (write
&& async_submit
) {
8353 ret
= btrfs_wq_submit_bio(fs_info
, inode
, bio
, 0, 0,
8355 __btrfs_submit_bio_start_direct_io
,
8356 __btrfs_submit_bio_done
);
8360 * If we aren't doing async submit, calculate the csum of the
8363 ret
= btrfs_csum_one_bio(inode
, bio
, file_offset
, 1);
8367 ret
= btrfs_lookup_and_bind_dio_csum(inode
, dip
, bio
,
8373 ret
= btrfs_map_bio(fs_info
, bio
, 0, async_submit
);
8379 static int btrfs_submit_direct_hook(struct btrfs_dio_private
*dip
,
8382 struct inode
*inode
= dip
->inode
;
8383 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8384 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8386 struct bio
*orig_bio
= dip
->orig_bio
;
8387 struct bio_vec
*bvec
;
8388 u64 start_sector
= orig_bio
->bi_iter
.bi_sector
;
8389 u64 file_offset
= dip
->logical_offset
;
8392 u32 blocksize
= fs_info
->sectorsize
;
8393 int async_submit
= 0;
8398 map_length
= orig_bio
->bi_iter
.bi_size
;
8399 ret
= btrfs_map_block(fs_info
, btrfs_op(orig_bio
), start_sector
<< 9,
8400 &map_length
, NULL
, 0);
8404 if (map_length
>= orig_bio
->bi_iter
.bi_size
) {
8406 dip
->flags
|= BTRFS_DIO_ORIG_BIO_SUBMITTED
;
8410 /* async crcs make it difficult to collect full stripe writes. */
8411 if (btrfs_get_alloc_profile(root
, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK
)
8416 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
8420 bio
->bi_opf
= orig_bio
->bi_opf
;
8421 bio
->bi_private
= dip
;
8422 bio
->bi_end_io
= btrfs_end_dio_bio
;
8423 btrfs_io_bio(bio
)->logical
= file_offset
;
8424 atomic_inc(&dip
->pending_bios
);
8426 bio_for_each_segment_all(bvec
, orig_bio
, j
) {
8427 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
->bv_len
);
8430 if (unlikely(map_length
< submit_len
+ blocksize
||
8431 bio_add_page(bio
, bvec
->bv_page
, blocksize
,
8432 bvec
->bv_offset
+ (i
* blocksize
)) < blocksize
)) {
8434 * inc the count before we submit the bio so
8435 * we know the end IO handler won't happen before
8436 * we inc the count. Otherwise, the dip might get freed
8437 * before we're done setting it up
8439 atomic_inc(&dip
->pending_bios
);
8440 ret
= __btrfs_submit_dio_bio(bio
, inode
,
8441 file_offset
, skip_sum
,
8445 atomic_dec(&dip
->pending_bios
);
8449 start_sector
+= submit_len
>> 9;
8450 file_offset
+= submit_len
;
8454 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
8455 start_sector
, GFP_NOFS
);
8458 bio
->bi_opf
= orig_bio
->bi_opf
;
8459 bio
->bi_private
= dip
;
8460 bio
->bi_end_io
= btrfs_end_dio_bio
;
8461 btrfs_io_bio(bio
)->logical
= file_offset
;
8463 map_length
= orig_bio
->bi_iter
.bi_size
;
8464 ret
= btrfs_map_block(fs_info
, btrfs_op(orig_bio
),
8466 &map_length
, NULL
, 0);
8474 submit_len
+= blocksize
;
8483 ret
= __btrfs_submit_dio_bio(bio
, inode
, file_offset
, skip_sum
,
8492 * before atomic variable goto zero, we must
8493 * make sure dip->errors is perceived to be set.
8495 smp_mb__before_atomic();
8496 if (atomic_dec_and_test(&dip
->pending_bios
))
8497 bio_io_error(dip
->orig_bio
);
8499 /* bio_end_io() will handle error, so we needn't return it */
8503 static void btrfs_submit_direct(struct bio
*dio_bio
, struct inode
*inode
,
8506 struct btrfs_dio_private
*dip
= NULL
;
8507 struct bio
*io_bio
= NULL
;
8508 struct btrfs_io_bio
*btrfs_bio
;
8510 bool write
= (bio_op(dio_bio
) == REQ_OP_WRITE
);
8513 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
8515 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
8521 dip
= kzalloc(sizeof(*dip
), GFP_NOFS
);
8527 dip
->private = dio_bio
->bi_private
;
8529 dip
->logical_offset
= file_offset
;
8530 dip
->bytes
= dio_bio
->bi_iter
.bi_size
;
8531 dip
->disk_bytenr
= (u64
)dio_bio
->bi_iter
.bi_sector
<< 9;
8532 io_bio
->bi_private
= dip
;
8533 dip
->orig_bio
= io_bio
;
8534 dip
->dio_bio
= dio_bio
;
8535 atomic_set(&dip
->pending_bios
, 0);
8536 btrfs_bio
= btrfs_io_bio(io_bio
);
8537 btrfs_bio
->logical
= file_offset
;
8540 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
8542 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
8543 dip
->subio_endio
= btrfs_subio_endio_read
;
8547 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8548 * even if we fail to submit a bio, because in such case we do the
8549 * corresponding error handling below and it must not be done a second
8550 * time by btrfs_direct_IO().
8553 struct btrfs_dio_data
*dio_data
= current
->journal_info
;
8555 dio_data
->unsubmitted_oe_range_end
= dip
->logical_offset
+
8557 dio_data
->unsubmitted_oe_range_start
=
8558 dio_data
->unsubmitted_oe_range_end
;
8561 ret
= btrfs_submit_direct_hook(dip
, skip_sum
);
8565 if (btrfs_bio
->end_io
)
8566 btrfs_bio
->end_io(btrfs_bio
, ret
);
8570 * If we arrived here it means either we failed to submit the dip
8571 * or we either failed to clone the dio_bio or failed to allocate the
8572 * dip. If we cloned the dio_bio and allocated the dip, we can just
8573 * call bio_endio against our io_bio so that we get proper resource
8574 * cleanup if we fail to submit the dip, otherwise, we must do the
8575 * same as btrfs_endio_direct_[write|read] because we can't call these
8576 * callbacks - they require an allocated dip and a clone of dio_bio.
8578 if (io_bio
&& dip
) {
8579 io_bio
->bi_error
= -EIO
;
8582 * The end io callbacks free our dip, do the final put on io_bio
8583 * and all the cleanup and final put for dio_bio (through
8590 btrfs_endio_direct_write_update_ordered(inode
,
8592 dio_bio
->bi_iter
.bi_size
,
8595 unlock_extent(&BTRFS_I(inode
)->io_tree
, file_offset
,
8596 file_offset
+ dio_bio
->bi_iter
.bi_size
- 1);
8598 dio_bio
->bi_error
= -EIO
;
8600 * Releases and cleans up our dio_bio, no need to bio_put()
8601 * nor bio_endio()/bio_io_error() against dio_bio.
8603 dio_end_io(dio_bio
, ret
);
8610 static ssize_t
check_direct_IO(struct btrfs_fs_info
*fs_info
,
8612 const struct iov_iter
*iter
, loff_t offset
)
8616 unsigned int blocksize_mask
= fs_info
->sectorsize
- 1;
8617 ssize_t retval
= -EINVAL
;
8619 if (offset
& blocksize_mask
)
8622 if (iov_iter_alignment(iter
) & blocksize_mask
)
8625 /* If this is a write we don't need to check anymore */
8626 if (iov_iter_rw(iter
) != READ
|| !iter_is_iovec(iter
))
8629 * Check to make sure we don't have duplicate iov_base's in this
8630 * iovec, if so return EINVAL, otherwise we'll get csum errors
8631 * when reading back.
8633 for (seg
= 0; seg
< iter
->nr_segs
; seg
++) {
8634 for (i
= seg
+ 1; i
< iter
->nr_segs
; i
++) {
8635 if (iter
->iov
[seg
].iov_base
== iter
->iov
[i
].iov_base
)
8644 static ssize_t
btrfs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
8646 struct file
*file
= iocb
->ki_filp
;
8647 struct inode
*inode
= file
->f_mapping
->host
;
8648 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8649 struct btrfs_dio_data dio_data
= { 0 };
8650 loff_t offset
= iocb
->ki_pos
;
8654 bool relock
= false;
8657 if (check_direct_IO(fs_info
, iocb
, iter
, offset
))
8660 inode_dio_begin(inode
);
8661 smp_mb__after_atomic();
8664 * The generic stuff only does filemap_write_and_wait_range, which
8665 * isn't enough if we've written compressed pages to this area, so
8666 * we need to flush the dirty pages again to make absolutely sure
8667 * that any outstanding dirty pages are on disk.
8669 count
= iov_iter_count(iter
);
8670 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
8671 &BTRFS_I(inode
)->runtime_flags
))
8672 filemap_fdatawrite_range(inode
->i_mapping
, offset
,
8673 offset
+ count
- 1);
8675 if (iov_iter_rw(iter
) == WRITE
) {
8677 * If the write DIO is beyond the EOF, we need update
8678 * the isize, but it is protected by i_mutex. So we can
8679 * not unlock the i_mutex at this case.
8681 if (offset
+ count
<= inode
->i_size
) {
8682 inode_unlock(inode
);
8685 ret
= btrfs_delalloc_reserve_space(inode
, offset
, count
);
8688 dio_data
.outstanding_extents
= div64_u64(count
+
8689 BTRFS_MAX_EXTENT_SIZE
- 1,
8690 BTRFS_MAX_EXTENT_SIZE
);
8693 * We need to know how many extents we reserved so that we can
8694 * do the accounting properly if we go over the number we
8695 * originally calculated. Abuse current->journal_info for this.
8697 dio_data
.reserve
= round_up(count
,
8698 fs_info
->sectorsize
);
8699 dio_data
.unsubmitted_oe_range_start
= (u64
)offset
;
8700 dio_data
.unsubmitted_oe_range_end
= (u64
)offset
;
8701 current
->journal_info
= &dio_data
;
8702 down_read(&BTRFS_I(inode
)->dio_sem
);
8703 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
8704 &BTRFS_I(inode
)->runtime_flags
)) {
8705 inode_dio_end(inode
);
8706 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
8710 ret
= __blockdev_direct_IO(iocb
, inode
,
8711 fs_info
->fs_devices
->latest_bdev
,
8712 iter
, btrfs_get_blocks_direct
, NULL
,
8713 btrfs_submit_direct
, flags
);
8714 if (iov_iter_rw(iter
) == WRITE
) {
8715 up_read(&BTRFS_I(inode
)->dio_sem
);
8716 current
->journal_info
= NULL
;
8717 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
8718 if (dio_data
.reserve
)
8719 btrfs_delalloc_release_space(inode
, offset
,
8722 * On error we might have left some ordered extents
8723 * without submitting corresponding bios for them, so
8724 * cleanup them up to avoid other tasks getting them
8725 * and waiting for them to complete forever.
8727 if (dio_data
.unsubmitted_oe_range_start
<
8728 dio_data
.unsubmitted_oe_range_end
)
8729 btrfs_endio_direct_write_update_ordered(inode
,
8730 dio_data
.unsubmitted_oe_range_start
,
8731 dio_data
.unsubmitted_oe_range_end
-
8732 dio_data
.unsubmitted_oe_range_start
,
8734 } else if (ret
>= 0 && (size_t)ret
< count
)
8735 btrfs_delalloc_release_space(inode
, offset
,
8736 count
- (size_t)ret
);
8740 inode_dio_end(inode
);
8747 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8749 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
8750 __u64 start
, __u64 len
)
8754 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
8758 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
8761 int btrfs_readpage(struct file
*file
, struct page
*page
)
8763 struct extent_io_tree
*tree
;
8764 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8765 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
8768 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
8770 struct extent_io_tree
*tree
;
8771 struct inode
*inode
= page
->mapping
->host
;
8774 if (current
->flags
& PF_MEMALLOC
) {
8775 redirty_page_for_writepage(wbc
, page
);
8781 * If we are under memory pressure we will call this directly from the
8782 * VM, we need to make sure we have the inode referenced for the ordered
8783 * extent. If not just return like we didn't do anything.
8785 if (!igrab(inode
)) {
8786 redirty_page_for_writepage(wbc
, page
);
8787 return AOP_WRITEPAGE_ACTIVATE
;
8789 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8790 ret
= extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
8791 btrfs_add_delayed_iput(inode
);
8795 static int btrfs_writepages(struct address_space
*mapping
,
8796 struct writeback_control
*wbc
)
8798 struct extent_io_tree
*tree
;
8800 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8801 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
8805 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
8806 struct list_head
*pages
, unsigned nr_pages
)
8808 struct extent_io_tree
*tree
;
8809 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8810 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
8813 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8815 struct extent_io_tree
*tree
;
8816 struct extent_map_tree
*map
;
8819 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8820 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
8821 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
8823 ClearPagePrivate(page
);
8824 set_page_private(page
, 0);
8830 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8832 if (PageWriteback(page
) || PageDirty(page
))
8834 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
8837 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
8838 unsigned int length
)
8840 struct inode
*inode
= page
->mapping
->host
;
8841 struct extent_io_tree
*tree
;
8842 struct btrfs_ordered_extent
*ordered
;
8843 struct extent_state
*cached_state
= NULL
;
8844 u64 page_start
= page_offset(page
);
8845 u64 page_end
= page_start
+ PAGE_SIZE
- 1;
8848 int inode_evicting
= inode
->i_state
& I_FREEING
;
8851 * we have the page locked, so new writeback can't start,
8852 * and the dirty bit won't be cleared while we are here.
8854 * Wait for IO on this page so that we can safely clear
8855 * the PagePrivate2 bit and do ordered accounting
8857 wait_on_page_writeback(page
);
8859 tree
= &BTRFS_I(inode
)->io_tree
;
8861 btrfs_releasepage(page
, GFP_NOFS
);
8865 if (!inode_evicting
)
8866 lock_extent_bits(tree
, page_start
, page_end
, &cached_state
);
8869 ordered
= btrfs_lookup_ordered_range(inode
, start
,
8870 page_end
- start
+ 1);
8872 end
= min(page_end
, ordered
->file_offset
+ ordered
->len
- 1);
8874 * IO on this page will never be started, so we need
8875 * to account for any ordered extents now
8877 if (!inode_evicting
)
8878 clear_extent_bit(tree
, start
, end
,
8879 EXTENT_DIRTY
| EXTENT_DELALLOC
|
8880 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
8881 EXTENT_DEFRAG
, 1, 0, &cached_state
,
8884 * whoever cleared the private bit is responsible
8885 * for the finish_ordered_io
8887 if (TestClearPagePrivate2(page
)) {
8888 struct btrfs_ordered_inode_tree
*tree
;
8891 tree
= &BTRFS_I(inode
)->ordered_tree
;
8893 spin_lock_irq(&tree
->lock
);
8894 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
8895 new_len
= start
- ordered
->file_offset
;
8896 if (new_len
< ordered
->truncated_len
)
8897 ordered
->truncated_len
= new_len
;
8898 spin_unlock_irq(&tree
->lock
);
8900 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
8902 end
- start
+ 1, 1))
8903 btrfs_finish_ordered_io(ordered
);
8905 btrfs_put_ordered_extent(ordered
);
8906 if (!inode_evicting
) {
8907 cached_state
= NULL
;
8908 lock_extent_bits(tree
, start
, end
,
8913 if (start
< page_end
)
8918 * Qgroup reserved space handler
8919 * Page here will be either
8920 * 1) Already written to disk
8921 * In this case, its reserved space is released from data rsv map
8922 * and will be freed by delayed_ref handler finally.
8923 * So even we call qgroup_free_data(), it won't decrease reserved
8925 * 2) Not written to disk
8926 * This means the reserved space should be freed here. However,
8927 * if a truncate invalidates the page (by clearing PageDirty)
8928 * and the page is accounted for while allocating extent
8929 * in btrfs_check_data_free_space() we let delayed_ref to
8930 * free the entire extent.
8932 if (PageDirty(page
))
8933 btrfs_qgroup_free_data(inode
, page_start
, PAGE_SIZE
);
8934 if (!inode_evicting
) {
8935 clear_extent_bit(tree
, page_start
, page_end
,
8936 EXTENT_LOCKED
| EXTENT_DIRTY
|
8937 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
8938 EXTENT_DEFRAG
, 1, 1,
8939 &cached_state
, GFP_NOFS
);
8941 __btrfs_releasepage(page
, GFP_NOFS
);
8944 ClearPageChecked(page
);
8945 if (PagePrivate(page
)) {
8946 ClearPagePrivate(page
);
8947 set_page_private(page
, 0);
8953 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8954 * called from a page fault handler when a page is first dirtied. Hence we must
8955 * be careful to check for EOF conditions here. We set the page up correctly
8956 * for a written page which means we get ENOSPC checking when writing into
8957 * holes and correct delalloc and unwritten extent mapping on filesystems that
8958 * support these features.
8960 * We are not allowed to take the i_mutex here so we have to play games to
8961 * protect against truncate races as the page could now be beyond EOF. Because
8962 * vmtruncate() writes the inode size before removing pages, once we have the
8963 * page lock we can determine safely if the page is beyond EOF. If it is not
8964 * beyond EOF, then the page is guaranteed safe against truncation until we
8967 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
8969 struct page
*page
= vmf
->page
;
8970 struct inode
*inode
= file_inode(vma
->vm_file
);
8971 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
8972 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
8973 struct btrfs_ordered_extent
*ordered
;
8974 struct extent_state
*cached_state
= NULL
;
8976 unsigned long zero_start
;
8985 reserved_space
= PAGE_SIZE
;
8987 sb_start_pagefault(inode
->i_sb
);
8988 page_start
= page_offset(page
);
8989 page_end
= page_start
+ PAGE_SIZE
- 1;
8993 * Reserving delalloc space after obtaining the page lock can lead to
8994 * deadlock. For example, if a dirty page is locked by this function
8995 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8996 * dirty page write out, then the btrfs_writepage() function could
8997 * end up waiting indefinitely to get a lock on the page currently
8998 * being processed by btrfs_page_mkwrite() function.
9000 ret
= btrfs_delalloc_reserve_space(inode
, page_start
,
9003 ret
= file_update_time(vma
->vm_file
);
9009 else /* -ENOSPC, -EIO, etc */
9010 ret
= VM_FAULT_SIGBUS
;
9016 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
9019 size
= i_size_read(inode
);
9021 if ((page
->mapping
!= inode
->i_mapping
) ||
9022 (page_start
>= size
)) {
9023 /* page got truncated out from underneath us */
9026 wait_on_page_writeback(page
);
9028 lock_extent_bits(io_tree
, page_start
, page_end
, &cached_state
);
9029 set_page_extent_mapped(page
);
9032 * we can't set the delalloc bits if there are pending ordered
9033 * extents. Drop our locks and wait for them to finish
9035 ordered
= btrfs_lookup_ordered_range(inode
, page_start
, page_end
);
9037 unlock_extent_cached(io_tree
, page_start
, page_end
,
9038 &cached_state
, GFP_NOFS
);
9040 btrfs_start_ordered_extent(inode
, ordered
, 1);
9041 btrfs_put_ordered_extent(ordered
);
9045 if (page
->index
== ((size
- 1) >> PAGE_SHIFT
)) {
9046 reserved_space
= round_up(size
- page_start
,
9047 fs_info
->sectorsize
);
9048 if (reserved_space
< PAGE_SIZE
) {
9049 end
= page_start
+ reserved_space
- 1;
9050 spin_lock(&BTRFS_I(inode
)->lock
);
9051 BTRFS_I(inode
)->outstanding_extents
++;
9052 spin_unlock(&BTRFS_I(inode
)->lock
);
9053 btrfs_delalloc_release_space(inode
, page_start
,
9054 PAGE_SIZE
- reserved_space
);
9059 * XXX - page_mkwrite gets called every time the page is dirtied, even
9060 * if it was already dirty, so for space accounting reasons we need to
9061 * clear any delalloc bits for the range we are fixing to save. There
9062 * is probably a better way to do this, but for now keep consistent with
9063 * prepare_pages in the normal write path.
9065 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, end
,
9066 EXTENT_DIRTY
| EXTENT_DELALLOC
|
9067 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
9068 0, 0, &cached_state
, GFP_NOFS
);
9070 ret
= btrfs_set_extent_delalloc(inode
, page_start
, end
,
9073 unlock_extent_cached(io_tree
, page_start
, page_end
,
9074 &cached_state
, GFP_NOFS
);
9075 ret
= VM_FAULT_SIGBUS
;
9080 /* page is wholly or partially inside EOF */
9081 if (page_start
+ PAGE_SIZE
> size
)
9082 zero_start
= size
& ~PAGE_MASK
;
9084 zero_start
= PAGE_SIZE
;
9086 if (zero_start
!= PAGE_SIZE
) {
9088 memset(kaddr
+ zero_start
, 0, PAGE_SIZE
- zero_start
);
9089 flush_dcache_page(page
);
9092 ClearPageChecked(page
);
9093 set_page_dirty(page
);
9094 SetPageUptodate(page
);
9096 BTRFS_I(inode
)->last_trans
= fs_info
->generation
;
9097 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
9098 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
9100 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
9104 sb_end_pagefault(inode
->i_sb
);
9105 return VM_FAULT_LOCKED
;
9109 btrfs_delalloc_release_space(inode
, page_start
, reserved_space
);
9111 sb_end_pagefault(inode
->i_sb
);
9115 static int btrfs_truncate(struct inode
*inode
)
9117 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
9118 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9119 struct btrfs_block_rsv
*rsv
;
9122 struct btrfs_trans_handle
*trans
;
9123 u64 mask
= fs_info
->sectorsize
- 1;
9124 u64 min_size
= btrfs_calc_trunc_metadata_size(fs_info
, 1);
9126 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
9132 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
9133 * 3 things going on here
9135 * 1) We need to reserve space for our orphan item and the space to
9136 * delete our orphan item. Lord knows we don't want to have a dangling
9137 * orphan item because we didn't reserve space to remove it.
9139 * 2) We need to reserve space to update our inode.
9141 * 3) We need to have something to cache all the space that is going to
9142 * be free'd up by the truncate operation, but also have some slack
9143 * space reserved in case it uses space during the truncate (thank you
9144 * very much snapshotting).
9146 * And we need these to all be separate. The fact is we can use a lot of
9147 * space doing the truncate, and we have no earthly idea how much space
9148 * we will use, so we need the truncate reservation to be separate so it
9149 * doesn't end up using space reserved for updating the inode or
9150 * removing the orphan item. We also need to be able to stop the
9151 * transaction and start a new one, which means we need to be able to
9152 * update the inode several times, and we have no idea of knowing how
9153 * many times that will be, so we can't just reserve 1 item for the
9154 * entirety of the operation, so that has to be done separately as well.
9155 * Then there is the orphan item, which does indeed need to be held on
9156 * to for the whole operation, and we need nobody to touch this reserved
9157 * space except the orphan code.
9159 * So that leaves us with
9161 * 1) root->orphan_block_rsv - for the orphan deletion.
9162 * 2) rsv - for the truncate reservation, which we will steal from the
9163 * transaction reservation.
9164 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9165 * updating the inode.
9167 rsv
= btrfs_alloc_block_rsv(fs_info
, BTRFS_BLOCK_RSV_TEMP
);
9170 rsv
->size
= min_size
;
9174 * 1 for the truncate slack space
9175 * 1 for updating the inode.
9177 trans
= btrfs_start_transaction(root
, 2);
9178 if (IS_ERR(trans
)) {
9179 err
= PTR_ERR(trans
);
9183 /* Migrate the slack space for the truncate to our reserve */
9184 ret
= btrfs_block_rsv_migrate(&fs_info
->trans_block_rsv
, rsv
,
9189 * So if we truncate and then write and fsync we normally would just
9190 * write the extents that changed, which is a problem if we need to
9191 * first truncate that entire inode. So set this flag so we write out
9192 * all of the extents in the inode to the sync log so we're completely
9195 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
9196 trans
->block_rsv
= rsv
;
9199 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
9201 BTRFS_EXTENT_DATA_KEY
);
9202 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
) {
9207 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
9208 ret
= btrfs_update_inode(trans
, root
, inode
);
9214 btrfs_end_transaction(trans
);
9215 btrfs_btree_balance_dirty(fs_info
);
9217 trans
= btrfs_start_transaction(root
, 2);
9218 if (IS_ERR(trans
)) {
9219 ret
= err
= PTR_ERR(trans
);
9224 btrfs_block_rsv_release(fs_info
, rsv
, -1);
9225 ret
= btrfs_block_rsv_migrate(&fs_info
->trans_block_rsv
,
9227 BUG_ON(ret
); /* shouldn't happen */
9228 trans
->block_rsv
= rsv
;
9231 if (ret
== 0 && inode
->i_nlink
> 0) {
9232 trans
->block_rsv
= root
->orphan_block_rsv
;
9233 ret
= btrfs_orphan_del(trans
, inode
);
9239 trans
->block_rsv
= &fs_info
->trans_block_rsv
;
9240 ret
= btrfs_update_inode(trans
, root
, inode
);
9244 ret
= btrfs_end_transaction(trans
);
9245 btrfs_btree_balance_dirty(fs_info
);
9248 btrfs_free_block_rsv(fs_info
, rsv
);
9257 * create a new subvolume directory/inode (helper for the ioctl).
9259 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
9260 struct btrfs_root
*new_root
,
9261 struct btrfs_root
*parent_root
,
9264 struct inode
*inode
;
9268 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
9269 new_dirid
, new_dirid
,
9270 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
9273 return PTR_ERR(inode
);
9274 inode
->i_op
= &btrfs_dir_inode_operations
;
9275 inode
->i_fop
= &btrfs_dir_file_operations
;
9277 set_nlink(inode
, 1);
9278 btrfs_i_size_write(inode
, 0);
9279 unlock_new_inode(inode
);
9281 err
= btrfs_subvol_inherit_props(trans
, new_root
, parent_root
);
9283 btrfs_err(new_root
->fs_info
,
9284 "error inheriting subvolume %llu properties: %d",
9285 new_root
->root_key
.objectid
, err
);
9287 err
= btrfs_update_inode(trans
, new_root
, inode
);
9293 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
9295 struct btrfs_inode
*ei
;
9296 struct inode
*inode
;
9298 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
9305 ei
->last_sub_trans
= 0;
9306 ei
->logged_trans
= 0;
9307 ei
->delalloc_bytes
= 0;
9308 ei
->defrag_bytes
= 0;
9309 ei
->disk_i_size
= 0;
9312 ei
->index_cnt
= (u64
)-1;
9314 ei
->last_unlink_trans
= 0;
9315 ei
->last_log_commit
= 0;
9316 ei
->delayed_iput_count
= 0;
9318 spin_lock_init(&ei
->lock
);
9319 ei
->outstanding_extents
= 0;
9320 ei
->reserved_extents
= 0;
9322 ei
->runtime_flags
= 0;
9323 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
9325 ei
->delayed_node
= NULL
;
9327 ei
->i_otime
.tv_sec
= 0;
9328 ei
->i_otime
.tv_nsec
= 0;
9330 inode
= &ei
->vfs_inode
;
9331 extent_map_tree_init(&ei
->extent_tree
);
9332 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
9333 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
9334 ei
->io_tree
.track_uptodate
= 1;
9335 ei
->io_failure_tree
.track_uptodate
= 1;
9336 atomic_set(&ei
->sync_writers
, 0);
9337 mutex_init(&ei
->log_mutex
);
9338 mutex_init(&ei
->delalloc_mutex
);
9339 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
9340 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
9341 INIT_LIST_HEAD(&ei
->delayed_iput
);
9342 RB_CLEAR_NODE(&ei
->rb_node
);
9343 init_rwsem(&ei
->dio_sem
);
9348 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9349 void btrfs_test_destroy_inode(struct inode
*inode
)
9351 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
9352 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9356 static void btrfs_i_callback(struct rcu_head
*head
)
9358 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
9359 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9362 void btrfs_destroy_inode(struct inode
*inode
)
9364 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
9365 struct btrfs_ordered_extent
*ordered
;
9366 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9368 WARN_ON(!hlist_empty(&inode
->i_dentry
));
9369 WARN_ON(inode
->i_data
.nrpages
);
9370 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
9371 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
9372 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
9373 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
9374 WARN_ON(BTRFS_I(inode
)->defrag_bytes
);
9377 * This can happen where we create an inode, but somebody else also
9378 * created the same inode and we need to destroy the one we already
9384 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
9385 &BTRFS_I(inode
)->runtime_flags
)) {
9386 btrfs_info(fs_info
, "inode %llu still on the orphan list",
9388 atomic_dec(&root
->orphan_inodes
);
9392 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
9397 "found ordered extent %llu %llu on inode cleanup",
9398 ordered
->file_offset
, ordered
->len
);
9399 btrfs_remove_ordered_extent(inode
, ordered
);
9400 btrfs_put_ordered_extent(ordered
);
9401 btrfs_put_ordered_extent(ordered
);
9404 btrfs_qgroup_check_reserved_leak(inode
);
9405 inode_tree_del(inode
);
9406 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
9408 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
9411 int btrfs_drop_inode(struct inode
*inode
)
9413 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9418 /* the snap/subvol tree is on deleting */
9419 if (btrfs_root_refs(&root
->root_item
) == 0)
9422 return generic_drop_inode(inode
);
9425 static void init_once(void *foo
)
9427 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
9429 inode_init_once(&ei
->vfs_inode
);
9432 void btrfs_destroy_cachep(void)
9435 * Make sure all delayed rcu free inodes are flushed before we
9439 kmem_cache_destroy(btrfs_inode_cachep
);
9440 kmem_cache_destroy(btrfs_trans_handle_cachep
);
9441 kmem_cache_destroy(btrfs_transaction_cachep
);
9442 kmem_cache_destroy(btrfs_path_cachep
);
9443 kmem_cache_destroy(btrfs_free_space_cachep
);
9446 int btrfs_init_cachep(void)
9448 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
9449 sizeof(struct btrfs_inode
), 0,
9450 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
| SLAB_ACCOUNT
,
9452 if (!btrfs_inode_cachep
)
9455 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
9456 sizeof(struct btrfs_trans_handle
), 0,
9457 SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
9458 if (!btrfs_trans_handle_cachep
)
9461 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
9462 sizeof(struct btrfs_transaction
), 0,
9463 SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
9464 if (!btrfs_transaction_cachep
)
9467 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
9468 sizeof(struct btrfs_path
), 0,
9469 SLAB_MEM_SPREAD
, NULL
);
9470 if (!btrfs_path_cachep
)
9473 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
9474 sizeof(struct btrfs_free_space
), 0,
9475 SLAB_MEM_SPREAD
, NULL
);
9476 if (!btrfs_free_space_cachep
)
9481 btrfs_destroy_cachep();
9485 static int btrfs_getattr(struct vfsmount
*mnt
,
9486 struct dentry
*dentry
, struct kstat
*stat
)
9489 struct inode
*inode
= d_inode(dentry
);
9490 u32 blocksize
= inode
->i_sb
->s_blocksize
;
9492 generic_fillattr(inode
, stat
);
9493 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
9495 spin_lock(&BTRFS_I(inode
)->lock
);
9496 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
9497 spin_unlock(&BTRFS_I(inode
)->lock
);
9498 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
9499 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
9503 static int btrfs_rename_exchange(struct inode
*old_dir
,
9504 struct dentry
*old_dentry
,
9505 struct inode
*new_dir
,
9506 struct dentry
*new_dentry
)
9508 struct btrfs_fs_info
*fs_info
= btrfs_sb(old_dir
->i_sb
);
9509 struct btrfs_trans_handle
*trans
;
9510 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
9511 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
9512 struct inode
*new_inode
= new_dentry
->d_inode
;
9513 struct inode
*old_inode
= old_dentry
->d_inode
;
9514 struct timespec ctime
= current_time(old_inode
);
9515 struct dentry
*parent
;
9516 u64 old_ino
= btrfs_ino(old_inode
);
9517 u64 new_ino
= btrfs_ino(new_inode
);
9522 bool root_log_pinned
= false;
9523 bool dest_log_pinned
= false;
9525 /* we only allow rename subvolume link between subvolumes */
9526 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
9529 /* close the race window with snapshot create/destroy ioctl */
9530 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9531 down_read(&fs_info
->subvol_sem
);
9532 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9533 down_read(&fs_info
->subvol_sem
);
9536 * We want to reserve the absolute worst case amount of items. So if
9537 * both inodes are subvols and we need to unlink them then that would
9538 * require 4 item modifications, but if they are both normal inodes it
9539 * would require 5 item modifications, so we'll assume their normal
9540 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9541 * should cover the worst case number of items we'll modify.
9543 trans
= btrfs_start_transaction(root
, 12);
9544 if (IS_ERR(trans
)) {
9545 ret
= PTR_ERR(trans
);
9550 * We need to find a free sequence number both in the source and
9551 * in the destination directory for the exchange.
9553 ret
= btrfs_set_inode_index(new_dir
, &old_idx
);
9556 ret
= btrfs_set_inode_index(old_dir
, &new_idx
);
9560 BTRFS_I(old_inode
)->dir_index
= 0ULL;
9561 BTRFS_I(new_inode
)->dir_index
= 0ULL;
9563 /* Reference for the source. */
9564 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9565 /* force full log commit if subvolume involved. */
9566 btrfs_set_log_full_commit(fs_info
, trans
);
9568 btrfs_pin_log_trans(root
);
9569 root_log_pinned
= true;
9570 ret
= btrfs_insert_inode_ref(trans
, dest
,
9571 new_dentry
->d_name
.name
,
9572 new_dentry
->d_name
.len
,
9574 btrfs_ino(new_dir
), old_idx
);
9579 /* And now for the dest. */
9580 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9581 /* force full log commit if subvolume involved. */
9582 btrfs_set_log_full_commit(fs_info
, trans
);
9584 btrfs_pin_log_trans(dest
);
9585 dest_log_pinned
= true;
9586 ret
= btrfs_insert_inode_ref(trans
, root
,
9587 old_dentry
->d_name
.name
,
9588 old_dentry
->d_name
.len
,
9590 btrfs_ino(old_dir
), new_idx
);
9595 /* Update inode version and ctime/mtime. */
9596 inode_inc_iversion(old_dir
);
9597 inode_inc_iversion(new_dir
);
9598 inode_inc_iversion(old_inode
);
9599 inode_inc_iversion(new_inode
);
9600 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
9601 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
9602 old_inode
->i_ctime
= ctime
;
9603 new_inode
->i_ctime
= ctime
;
9605 if (old_dentry
->d_parent
!= new_dentry
->d_parent
) {
9606 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
9607 btrfs_record_unlink_dir(trans
, new_dir
, new_inode
, 1);
9610 /* src is a subvolume */
9611 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9612 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
9613 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
,
9615 old_dentry
->d_name
.name
,
9616 old_dentry
->d_name
.len
);
9617 } else { /* src is an inode */
9618 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
9619 old_dentry
->d_inode
,
9620 old_dentry
->d_name
.name
,
9621 old_dentry
->d_name
.len
);
9623 ret
= btrfs_update_inode(trans
, root
, old_inode
);
9626 btrfs_abort_transaction(trans
, ret
);
9630 /* dest is a subvolume */
9631 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9632 root_objectid
= BTRFS_I(new_inode
)->root
->root_key
.objectid
;
9633 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
9635 new_dentry
->d_name
.name
,
9636 new_dentry
->d_name
.len
);
9637 } else { /* dest is an inode */
9638 ret
= __btrfs_unlink_inode(trans
, dest
, new_dir
,
9639 new_dentry
->d_inode
,
9640 new_dentry
->d_name
.name
,
9641 new_dentry
->d_name
.len
);
9643 ret
= btrfs_update_inode(trans
, dest
, new_inode
);
9646 btrfs_abort_transaction(trans
, ret
);
9650 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
9651 new_dentry
->d_name
.name
,
9652 new_dentry
->d_name
.len
, 0, old_idx
);
9654 btrfs_abort_transaction(trans
, ret
);
9658 ret
= btrfs_add_link(trans
, old_dir
, new_inode
,
9659 old_dentry
->d_name
.name
,
9660 old_dentry
->d_name
.len
, 0, new_idx
);
9662 btrfs_abort_transaction(trans
, ret
);
9666 if (old_inode
->i_nlink
== 1)
9667 BTRFS_I(old_inode
)->dir_index
= old_idx
;
9668 if (new_inode
->i_nlink
== 1)
9669 BTRFS_I(new_inode
)->dir_index
= new_idx
;
9671 if (root_log_pinned
) {
9672 parent
= new_dentry
->d_parent
;
9673 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
9674 btrfs_end_log_trans(root
);
9675 root_log_pinned
= false;
9677 if (dest_log_pinned
) {
9678 parent
= old_dentry
->d_parent
;
9679 btrfs_log_new_name(trans
, new_inode
, new_dir
, parent
);
9680 btrfs_end_log_trans(dest
);
9681 dest_log_pinned
= false;
9685 * If we have pinned a log and an error happened, we unpin tasks
9686 * trying to sync the log and force them to fallback to a transaction
9687 * commit if the log currently contains any of the inodes involved in
9688 * this rename operation (to ensure we do not persist a log with an
9689 * inconsistent state for any of these inodes or leading to any
9690 * inconsistencies when replayed). If the transaction was aborted, the
9691 * abortion reason is propagated to userspace when attempting to commit
9692 * the transaction. If the log does not contain any of these inodes, we
9693 * allow the tasks to sync it.
9695 if (ret
&& (root_log_pinned
|| dest_log_pinned
)) {
9696 if (btrfs_inode_in_log(old_dir
, fs_info
->generation
) ||
9697 btrfs_inode_in_log(new_dir
, fs_info
->generation
) ||
9698 btrfs_inode_in_log(old_inode
, fs_info
->generation
) ||
9700 btrfs_inode_in_log(new_inode
, fs_info
->generation
)))
9701 btrfs_set_log_full_commit(fs_info
, trans
);
9703 if (root_log_pinned
) {
9704 btrfs_end_log_trans(root
);
9705 root_log_pinned
= false;
9707 if (dest_log_pinned
) {
9708 btrfs_end_log_trans(dest
);
9709 dest_log_pinned
= false;
9712 ret
= btrfs_end_transaction(trans
);
9714 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9715 up_read(&fs_info
->subvol_sem
);
9716 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9717 up_read(&fs_info
->subvol_sem
);
9722 static int btrfs_whiteout_for_rename(struct btrfs_trans_handle
*trans
,
9723 struct btrfs_root
*root
,
9725 struct dentry
*dentry
)
9728 struct inode
*inode
;
9732 ret
= btrfs_find_free_ino(root
, &objectid
);
9736 inode
= btrfs_new_inode(trans
, root
, dir
,
9737 dentry
->d_name
.name
,
9741 S_IFCHR
| WHITEOUT_MODE
,
9744 if (IS_ERR(inode
)) {
9745 ret
= PTR_ERR(inode
);
9749 inode
->i_op
= &btrfs_special_inode_operations
;
9750 init_special_inode(inode
, inode
->i_mode
,
9753 ret
= btrfs_init_inode_security(trans
, inode
, dir
,
9758 ret
= btrfs_add_nondir(trans
, dir
, dentry
,
9763 ret
= btrfs_update_inode(trans
, root
, inode
);
9765 unlock_new_inode(inode
);
9767 inode_dec_link_count(inode
);
9773 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
9774 struct inode
*new_dir
, struct dentry
*new_dentry
,
9777 struct btrfs_fs_info
*fs_info
= btrfs_sb(old_dir
->i_sb
);
9778 struct btrfs_trans_handle
*trans
;
9779 unsigned int trans_num_items
;
9780 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
9781 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
9782 struct inode
*new_inode
= d_inode(new_dentry
);
9783 struct inode
*old_inode
= d_inode(old_dentry
);
9787 u64 old_ino
= btrfs_ino(old_inode
);
9788 bool log_pinned
= false;
9790 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
9793 /* we only allow rename subvolume link between subvolumes */
9794 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
9797 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
9798 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
9801 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
9802 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
9806 /* check for collisions, even if the name isn't there */
9807 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
9808 new_dentry
->d_name
.name
,
9809 new_dentry
->d_name
.len
);
9812 if (ret
== -EEXIST
) {
9814 * eexist without a new_inode */
9815 if (WARN_ON(!new_inode
)) {
9819 /* maybe -EOVERFLOW */
9826 * we're using rename to replace one file with another. Start IO on it
9827 * now so we don't add too much work to the end of the transaction
9829 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
)
9830 filemap_flush(old_inode
->i_mapping
);
9832 /* close the racy window with snapshot create/destroy ioctl */
9833 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9834 down_read(&fs_info
->subvol_sem
);
9836 * We want to reserve the absolute worst case amount of items. So if
9837 * both inodes are subvols and we need to unlink them then that would
9838 * require 4 item modifications, but if they are both normal inodes it
9839 * would require 5 item modifications, so we'll assume they are normal
9840 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9841 * should cover the worst case number of items we'll modify.
9842 * If our rename has the whiteout flag, we need more 5 units for the
9843 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9844 * when selinux is enabled).
9846 trans_num_items
= 11;
9847 if (flags
& RENAME_WHITEOUT
)
9848 trans_num_items
+= 5;
9849 trans
= btrfs_start_transaction(root
, trans_num_items
);
9850 if (IS_ERR(trans
)) {
9851 ret
= PTR_ERR(trans
);
9856 btrfs_record_root_in_trans(trans
, dest
);
9858 ret
= btrfs_set_inode_index(new_dir
, &index
);
9862 BTRFS_I(old_inode
)->dir_index
= 0ULL;
9863 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9864 /* force full log commit if subvolume involved. */
9865 btrfs_set_log_full_commit(fs_info
, trans
);
9867 btrfs_pin_log_trans(root
);
9869 ret
= btrfs_insert_inode_ref(trans
, dest
,
9870 new_dentry
->d_name
.name
,
9871 new_dentry
->d_name
.len
,
9873 btrfs_ino(new_dir
), index
);
9878 inode_inc_iversion(old_dir
);
9879 inode_inc_iversion(new_dir
);
9880 inode_inc_iversion(old_inode
);
9881 old_dir
->i_ctime
= old_dir
->i_mtime
=
9882 new_dir
->i_ctime
= new_dir
->i_mtime
=
9883 old_inode
->i_ctime
= current_time(old_dir
);
9885 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
9886 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
9888 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9889 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
9890 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
9891 old_dentry
->d_name
.name
,
9892 old_dentry
->d_name
.len
);
9894 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
9895 d_inode(old_dentry
),
9896 old_dentry
->d_name
.name
,
9897 old_dentry
->d_name
.len
);
9899 ret
= btrfs_update_inode(trans
, root
, old_inode
);
9902 btrfs_abort_transaction(trans
, ret
);
9907 inode_inc_iversion(new_inode
);
9908 new_inode
->i_ctime
= current_time(new_inode
);
9909 if (unlikely(btrfs_ino(new_inode
) ==
9910 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
9911 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
9912 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
9914 new_dentry
->d_name
.name
,
9915 new_dentry
->d_name
.len
);
9916 BUG_ON(new_inode
->i_nlink
== 0);
9918 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
9919 d_inode(new_dentry
),
9920 new_dentry
->d_name
.name
,
9921 new_dentry
->d_name
.len
);
9923 if (!ret
&& new_inode
->i_nlink
== 0)
9924 ret
= btrfs_orphan_add(trans
, d_inode(new_dentry
));
9926 btrfs_abort_transaction(trans
, ret
);
9931 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
9932 new_dentry
->d_name
.name
,
9933 new_dentry
->d_name
.len
, 0, index
);
9935 btrfs_abort_transaction(trans
, ret
);
9939 if (old_inode
->i_nlink
== 1)
9940 BTRFS_I(old_inode
)->dir_index
= index
;
9943 struct dentry
*parent
= new_dentry
->d_parent
;
9945 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
9946 btrfs_end_log_trans(root
);
9950 if (flags
& RENAME_WHITEOUT
) {
9951 ret
= btrfs_whiteout_for_rename(trans
, root
, old_dir
,
9955 btrfs_abort_transaction(trans
, ret
);
9961 * If we have pinned the log and an error happened, we unpin tasks
9962 * trying to sync the log and force them to fallback to a transaction
9963 * commit if the log currently contains any of the inodes involved in
9964 * this rename operation (to ensure we do not persist a log with an
9965 * inconsistent state for any of these inodes or leading to any
9966 * inconsistencies when replayed). If the transaction was aborted, the
9967 * abortion reason is propagated to userspace when attempting to commit
9968 * the transaction. If the log does not contain any of these inodes, we
9969 * allow the tasks to sync it.
9971 if (ret
&& log_pinned
) {
9972 if (btrfs_inode_in_log(old_dir
, fs_info
->generation
) ||
9973 btrfs_inode_in_log(new_dir
, fs_info
->generation
) ||
9974 btrfs_inode_in_log(old_inode
, fs_info
->generation
) ||
9976 btrfs_inode_in_log(new_inode
, fs_info
->generation
)))
9977 btrfs_set_log_full_commit(fs_info
, trans
);
9979 btrfs_end_log_trans(root
);
9982 btrfs_end_transaction(trans
);
9984 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9985 up_read(&fs_info
->subvol_sem
);
9990 static int btrfs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
9991 struct inode
*new_dir
, struct dentry
*new_dentry
,
9994 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
9997 if (flags
& RENAME_EXCHANGE
)
9998 return btrfs_rename_exchange(old_dir
, old_dentry
, new_dir
,
10001 return btrfs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
10004 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
10006 struct btrfs_delalloc_work
*delalloc_work
;
10007 struct inode
*inode
;
10009 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
10011 inode
= delalloc_work
->inode
;
10012 filemap_flush(inode
->i_mapping
);
10013 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
10014 &BTRFS_I(inode
)->runtime_flags
))
10015 filemap_flush(inode
->i_mapping
);
10017 if (delalloc_work
->delay_iput
)
10018 btrfs_add_delayed_iput(inode
);
10021 complete(&delalloc_work
->completion
);
10024 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
10027 struct btrfs_delalloc_work
*work
;
10029 work
= kmalloc(sizeof(*work
), GFP_NOFS
);
10033 init_completion(&work
->completion
);
10034 INIT_LIST_HEAD(&work
->list
);
10035 work
->inode
= inode
;
10036 work
->delay_iput
= delay_iput
;
10037 WARN_ON_ONCE(!inode
);
10038 btrfs_init_work(&work
->work
, btrfs_flush_delalloc_helper
,
10039 btrfs_run_delalloc_work
, NULL
, NULL
);
10044 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
10046 wait_for_completion(&work
->completion
);
10051 * some fairly slow code that needs optimization. This walks the list
10052 * of all the inodes with pending delalloc and forces them to disk.
10054 static int __start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
,
10057 struct btrfs_inode
*binode
;
10058 struct inode
*inode
;
10059 struct btrfs_delalloc_work
*work
, *next
;
10060 struct list_head works
;
10061 struct list_head splice
;
10064 INIT_LIST_HEAD(&works
);
10065 INIT_LIST_HEAD(&splice
);
10067 mutex_lock(&root
->delalloc_mutex
);
10068 spin_lock(&root
->delalloc_lock
);
10069 list_splice_init(&root
->delalloc_inodes
, &splice
);
10070 while (!list_empty(&splice
)) {
10071 binode
= list_entry(splice
.next
, struct btrfs_inode
,
10074 list_move_tail(&binode
->delalloc_inodes
,
10075 &root
->delalloc_inodes
);
10076 inode
= igrab(&binode
->vfs_inode
);
10078 cond_resched_lock(&root
->delalloc_lock
);
10081 spin_unlock(&root
->delalloc_lock
);
10083 work
= btrfs_alloc_delalloc_work(inode
, delay_iput
);
10086 btrfs_add_delayed_iput(inode
);
10092 list_add_tail(&work
->list
, &works
);
10093 btrfs_queue_work(root
->fs_info
->flush_workers
,
10096 if (nr
!= -1 && ret
>= nr
)
10099 spin_lock(&root
->delalloc_lock
);
10101 spin_unlock(&root
->delalloc_lock
);
10104 list_for_each_entry_safe(work
, next
, &works
, list
) {
10105 list_del_init(&work
->list
);
10106 btrfs_wait_and_free_delalloc_work(work
);
10109 if (!list_empty_careful(&splice
)) {
10110 spin_lock(&root
->delalloc_lock
);
10111 list_splice_tail(&splice
, &root
->delalloc_inodes
);
10112 spin_unlock(&root
->delalloc_lock
);
10114 mutex_unlock(&root
->delalloc_mutex
);
10118 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
10120 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
10123 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
10126 ret
= __start_delalloc_inodes(root
, delay_iput
, -1);
10130 * the filemap_flush will queue IO into the worker threads, but
10131 * we have to make sure the IO is actually started and that
10132 * ordered extents get created before we return
10134 atomic_inc(&fs_info
->async_submit_draining
);
10135 while (atomic_read(&fs_info
->nr_async_submits
) ||
10136 atomic_read(&fs_info
->async_delalloc_pages
)) {
10137 wait_event(fs_info
->async_submit_wait
,
10138 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
10139 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
10141 atomic_dec(&fs_info
->async_submit_draining
);
10145 int btrfs_start_delalloc_roots(struct btrfs_fs_info
*fs_info
, int delay_iput
,
10148 struct btrfs_root
*root
;
10149 struct list_head splice
;
10152 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
10155 INIT_LIST_HEAD(&splice
);
10157 mutex_lock(&fs_info
->delalloc_root_mutex
);
10158 spin_lock(&fs_info
->delalloc_root_lock
);
10159 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
10160 while (!list_empty(&splice
) && nr
) {
10161 root
= list_first_entry(&splice
, struct btrfs_root
,
10163 root
= btrfs_grab_fs_root(root
);
10165 list_move_tail(&root
->delalloc_root
,
10166 &fs_info
->delalloc_roots
);
10167 spin_unlock(&fs_info
->delalloc_root_lock
);
10169 ret
= __start_delalloc_inodes(root
, delay_iput
, nr
);
10170 btrfs_put_fs_root(root
);
10178 spin_lock(&fs_info
->delalloc_root_lock
);
10180 spin_unlock(&fs_info
->delalloc_root_lock
);
10183 atomic_inc(&fs_info
->async_submit_draining
);
10184 while (atomic_read(&fs_info
->nr_async_submits
) ||
10185 atomic_read(&fs_info
->async_delalloc_pages
)) {
10186 wait_event(fs_info
->async_submit_wait
,
10187 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
10188 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
10190 atomic_dec(&fs_info
->async_submit_draining
);
10192 if (!list_empty_careful(&splice
)) {
10193 spin_lock(&fs_info
->delalloc_root_lock
);
10194 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
10195 spin_unlock(&fs_info
->delalloc_root_lock
);
10197 mutex_unlock(&fs_info
->delalloc_root_mutex
);
10201 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
10202 const char *symname
)
10204 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
10205 struct btrfs_trans_handle
*trans
;
10206 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
10207 struct btrfs_path
*path
;
10208 struct btrfs_key key
;
10209 struct inode
*inode
= NULL
;
10211 int drop_inode
= 0;
10217 struct btrfs_file_extent_item
*ei
;
10218 struct extent_buffer
*leaf
;
10220 name_len
= strlen(symname
);
10221 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(fs_info
))
10222 return -ENAMETOOLONG
;
10225 * 2 items for inode item and ref
10226 * 2 items for dir items
10227 * 1 item for updating parent inode item
10228 * 1 item for the inline extent item
10229 * 1 item for xattr if selinux is on
10231 trans
= btrfs_start_transaction(root
, 7);
10233 return PTR_ERR(trans
);
10235 err
= btrfs_find_free_ino(root
, &objectid
);
10239 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
10240 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
10241 S_IFLNK
|S_IRWXUGO
, &index
);
10242 if (IS_ERR(inode
)) {
10243 err
= PTR_ERR(inode
);
10248 * If the active LSM wants to access the inode during
10249 * d_instantiate it needs these. Smack checks to see
10250 * if the filesystem supports xattrs by looking at the
10253 inode
->i_fop
= &btrfs_file_operations
;
10254 inode
->i_op
= &btrfs_file_inode_operations
;
10255 inode
->i_mapping
->a_ops
= &btrfs_aops
;
10256 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
10258 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
10260 goto out_unlock_inode
;
10262 path
= btrfs_alloc_path();
10265 goto out_unlock_inode
;
10267 key
.objectid
= btrfs_ino(inode
);
10269 key
.type
= BTRFS_EXTENT_DATA_KEY
;
10270 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
10271 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
10274 btrfs_free_path(path
);
10275 goto out_unlock_inode
;
10277 leaf
= path
->nodes
[0];
10278 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
10279 struct btrfs_file_extent_item
);
10280 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
10281 btrfs_set_file_extent_type(leaf
, ei
,
10282 BTRFS_FILE_EXTENT_INLINE
);
10283 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
10284 btrfs_set_file_extent_compression(leaf
, ei
, 0);
10285 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
10286 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
10288 ptr
= btrfs_file_extent_inline_start(ei
);
10289 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
10290 btrfs_mark_buffer_dirty(leaf
);
10291 btrfs_free_path(path
);
10293 inode
->i_op
= &btrfs_symlink_inode_operations
;
10294 inode_nohighmem(inode
);
10295 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
10296 inode_set_bytes(inode
, name_len
);
10297 btrfs_i_size_write(inode
, name_len
);
10298 err
= btrfs_update_inode(trans
, root
, inode
);
10300 * Last step, add directory indexes for our symlink inode. This is the
10301 * last step to avoid extra cleanup of these indexes if an error happens
10305 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
10308 goto out_unlock_inode
;
10311 unlock_new_inode(inode
);
10312 d_instantiate(dentry
, inode
);
10315 btrfs_end_transaction(trans
);
10317 inode_dec_link_count(inode
);
10320 btrfs_btree_balance_dirty(fs_info
);
10325 unlock_new_inode(inode
);
10329 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
10330 u64 start
, u64 num_bytes
, u64 min_size
,
10331 loff_t actual_len
, u64
*alloc_hint
,
10332 struct btrfs_trans_handle
*trans
)
10334 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
10335 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
10336 struct extent_map
*em
;
10337 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
10338 struct btrfs_key ins
;
10339 u64 cur_offset
= start
;
10342 u64 last_alloc
= (u64
)-1;
10344 bool own_trans
= true;
10345 u64 end
= start
+ num_bytes
- 1;
10349 while (num_bytes
> 0) {
10351 trans
= btrfs_start_transaction(root
, 3);
10352 if (IS_ERR(trans
)) {
10353 ret
= PTR_ERR(trans
);
10358 cur_bytes
= min_t(u64
, num_bytes
, SZ_256M
);
10359 cur_bytes
= max(cur_bytes
, min_size
);
10361 * If we are severely fragmented we could end up with really
10362 * small allocations, so if the allocator is returning small
10363 * chunks lets make its job easier by only searching for those
10366 cur_bytes
= min(cur_bytes
, last_alloc
);
10367 ret
= btrfs_reserve_extent(root
, cur_bytes
, cur_bytes
,
10368 min_size
, 0, *alloc_hint
, &ins
, 1, 0);
10371 btrfs_end_transaction(trans
);
10374 btrfs_dec_block_group_reservations(fs_info
, ins
.objectid
);
10376 last_alloc
= ins
.offset
;
10377 ret
= insert_reserved_file_extent(trans
, inode
,
10378 cur_offset
, ins
.objectid
,
10379 ins
.offset
, ins
.offset
,
10380 ins
.offset
, 0, 0, 0,
10381 BTRFS_FILE_EXTENT_PREALLOC
);
10383 btrfs_free_reserved_extent(fs_info
, ins
.objectid
,
10385 btrfs_abort_transaction(trans
, ret
);
10387 btrfs_end_transaction(trans
);
10391 btrfs_drop_extent_cache(inode
, cur_offset
,
10392 cur_offset
+ ins
.offset
-1, 0);
10394 em
= alloc_extent_map();
10396 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
10397 &BTRFS_I(inode
)->runtime_flags
);
10401 em
->start
= cur_offset
;
10402 em
->orig_start
= cur_offset
;
10403 em
->len
= ins
.offset
;
10404 em
->block_start
= ins
.objectid
;
10405 em
->block_len
= ins
.offset
;
10406 em
->orig_block_len
= ins
.offset
;
10407 em
->ram_bytes
= ins
.offset
;
10408 em
->bdev
= fs_info
->fs_devices
->latest_bdev
;
10409 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
10410 em
->generation
= trans
->transid
;
10413 write_lock(&em_tree
->lock
);
10414 ret
= add_extent_mapping(em_tree
, em
, 1);
10415 write_unlock(&em_tree
->lock
);
10416 if (ret
!= -EEXIST
)
10418 btrfs_drop_extent_cache(inode
, cur_offset
,
10419 cur_offset
+ ins
.offset
- 1,
10422 free_extent_map(em
);
10424 num_bytes
-= ins
.offset
;
10425 cur_offset
+= ins
.offset
;
10426 *alloc_hint
= ins
.objectid
+ ins
.offset
;
10428 inode_inc_iversion(inode
);
10429 inode
->i_ctime
= current_time(inode
);
10430 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
10431 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
10432 (actual_len
> inode
->i_size
) &&
10433 (cur_offset
> inode
->i_size
)) {
10434 if (cur_offset
> actual_len
)
10435 i_size
= actual_len
;
10437 i_size
= cur_offset
;
10438 i_size_write(inode
, i_size
);
10439 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
10442 ret
= btrfs_update_inode(trans
, root
, inode
);
10445 btrfs_abort_transaction(trans
, ret
);
10447 btrfs_end_transaction(trans
);
10452 btrfs_end_transaction(trans
);
10454 if (cur_offset
< end
)
10455 btrfs_free_reserved_data_space(inode
, cur_offset
,
10456 end
- cur_offset
+ 1);
10460 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
10461 u64 start
, u64 num_bytes
, u64 min_size
,
10462 loff_t actual_len
, u64
*alloc_hint
)
10464 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
10465 min_size
, actual_len
, alloc_hint
,
10469 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
10470 struct btrfs_trans_handle
*trans
, int mode
,
10471 u64 start
, u64 num_bytes
, u64 min_size
,
10472 loff_t actual_len
, u64
*alloc_hint
)
10474 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
10475 min_size
, actual_len
, alloc_hint
, trans
);
10478 static int btrfs_set_page_dirty(struct page
*page
)
10480 return __set_page_dirty_nobuffers(page
);
10483 static int btrfs_permission(struct inode
*inode
, int mask
)
10485 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
10486 umode_t mode
= inode
->i_mode
;
10488 if (mask
& MAY_WRITE
&&
10489 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
10490 if (btrfs_root_readonly(root
))
10492 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
10495 return generic_permission(inode
, mask
);
10498 static int btrfs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
10500 struct btrfs_fs_info
*fs_info
= btrfs_sb(dir
->i_sb
);
10501 struct btrfs_trans_handle
*trans
;
10502 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
10503 struct inode
*inode
= NULL
;
10509 * 5 units required for adding orphan entry
10511 trans
= btrfs_start_transaction(root
, 5);
10513 return PTR_ERR(trans
);
10515 ret
= btrfs_find_free_ino(root
, &objectid
);
10519 inode
= btrfs_new_inode(trans
, root
, dir
, NULL
, 0,
10520 btrfs_ino(dir
), objectid
, mode
, &index
);
10521 if (IS_ERR(inode
)) {
10522 ret
= PTR_ERR(inode
);
10527 inode
->i_fop
= &btrfs_file_operations
;
10528 inode
->i_op
= &btrfs_file_inode_operations
;
10530 inode
->i_mapping
->a_ops
= &btrfs_aops
;
10531 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
10533 ret
= btrfs_init_inode_security(trans
, inode
, dir
, NULL
);
10537 ret
= btrfs_update_inode(trans
, root
, inode
);
10540 ret
= btrfs_orphan_add(trans
, inode
);
10545 * We set number of links to 0 in btrfs_new_inode(), and here we set
10546 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10549 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10551 set_nlink(inode
, 1);
10552 unlock_new_inode(inode
);
10553 d_tmpfile(dentry
, inode
);
10554 mark_inode_dirty(inode
);
10557 btrfs_end_transaction(trans
);
10560 btrfs_balance_delayed_items(fs_info
);
10561 btrfs_btree_balance_dirty(fs_info
);
10565 unlock_new_inode(inode
);
10570 static const struct inode_operations btrfs_dir_inode_operations
= {
10571 .getattr
= btrfs_getattr
,
10572 .lookup
= btrfs_lookup
,
10573 .create
= btrfs_create
,
10574 .unlink
= btrfs_unlink
,
10575 .link
= btrfs_link
,
10576 .mkdir
= btrfs_mkdir
,
10577 .rmdir
= btrfs_rmdir
,
10578 .rename
= btrfs_rename2
,
10579 .symlink
= btrfs_symlink
,
10580 .setattr
= btrfs_setattr
,
10581 .mknod
= btrfs_mknod
,
10582 .listxattr
= btrfs_listxattr
,
10583 .permission
= btrfs_permission
,
10584 .get_acl
= btrfs_get_acl
,
10585 .set_acl
= btrfs_set_acl
,
10586 .update_time
= btrfs_update_time
,
10587 .tmpfile
= btrfs_tmpfile
,
10589 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
10590 .lookup
= btrfs_lookup
,
10591 .permission
= btrfs_permission
,
10592 .update_time
= btrfs_update_time
,
10595 static const struct file_operations btrfs_dir_file_operations
= {
10596 .llseek
= generic_file_llseek
,
10597 .read
= generic_read_dir
,
10598 .iterate_shared
= btrfs_real_readdir
,
10599 .unlocked_ioctl
= btrfs_ioctl
,
10600 #ifdef CONFIG_COMPAT
10601 .compat_ioctl
= btrfs_compat_ioctl
,
10603 .release
= btrfs_release_file
,
10604 .fsync
= btrfs_sync_file
,
10607 static const struct extent_io_ops btrfs_extent_io_ops
= {
10608 .fill_delalloc
= run_delalloc_range
,
10609 .submit_bio_hook
= btrfs_submit_bio_hook
,
10610 .merge_bio_hook
= btrfs_merge_bio_hook
,
10611 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
10612 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
10613 .writepage_start_hook
= btrfs_writepage_start_hook
,
10614 .set_bit_hook
= btrfs_set_bit_hook
,
10615 .clear_bit_hook
= btrfs_clear_bit_hook
,
10616 .merge_extent_hook
= btrfs_merge_extent_hook
,
10617 .split_extent_hook
= btrfs_split_extent_hook
,
10621 * btrfs doesn't support the bmap operation because swapfiles
10622 * use bmap to make a mapping of extents in the file. They assume
10623 * these extents won't change over the life of the file and they
10624 * use the bmap result to do IO directly to the drive.
10626 * the btrfs bmap call would return logical addresses that aren't
10627 * suitable for IO and they also will change frequently as COW
10628 * operations happen. So, swapfile + btrfs == corruption.
10630 * For now we're avoiding this by dropping bmap.
10632 static const struct address_space_operations btrfs_aops
= {
10633 .readpage
= btrfs_readpage
,
10634 .writepage
= btrfs_writepage
,
10635 .writepages
= btrfs_writepages
,
10636 .readpages
= btrfs_readpages
,
10637 .direct_IO
= btrfs_direct_IO
,
10638 .invalidatepage
= btrfs_invalidatepage
,
10639 .releasepage
= btrfs_releasepage
,
10640 .set_page_dirty
= btrfs_set_page_dirty
,
10641 .error_remove_page
= generic_error_remove_page
,
10644 static const struct address_space_operations btrfs_symlink_aops
= {
10645 .readpage
= btrfs_readpage
,
10646 .writepage
= btrfs_writepage
,
10647 .invalidatepage
= btrfs_invalidatepage
,
10648 .releasepage
= btrfs_releasepage
,
10651 static const struct inode_operations btrfs_file_inode_operations
= {
10652 .getattr
= btrfs_getattr
,
10653 .setattr
= btrfs_setattr
,
10654 .listxattr
= btrfs_listxattr
,
10655 .permission
= btrfs_permission
,
10656 .fiemap
= btrfs_fiemap
,
10657 .get_acl
= btrfs_get_acl
,
10658 .set_acl
= btrfs_set_acl
,
10659 .update_time
= btrfs_update_time
,
10661 static const struct inode_operations btrfs_special_inode_operations
= {
10662 .getattr
= btrfs_getattr
,
10663 .setattr
= btrfs_setattr
,
10664 .permission
= btrfs_permission
,
10665 .listxattr
= btrfs_listxattr
,
10666 .get_acl
= btrfs_get_acl
,
10667 .set_acl
= btrfs_set_acl
,
10668 .update_time
= btrfs_update_time
,
10670 static const struct inode_operations btrfs_symlink_inode_operations
= {
10671 .get_link
= page_get_link
,
10672 .getattr
= btrfs_getattr
,
10673 .setattr
= btrfs_setattr
,
10674 .permission
= btrfs_permission
,
10675 .listxattr
= btrfs_listxattr
,
10676 .update_time
= btrfs_update_time
,
10679 const struct dentry_operations btrfs_dentry_operations
= {
10680 .d_delete
= btrfs_dentry_delete
,
10681 .d_release
= btrfs_dentry_release
,