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/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
40 #include <linux/ratelimit.h>
41 #include <linux/mount.h>
42 #include <linux/btrfs.h>
43 #include <linux/blkdev.h>
44 #include <linux/posix_acl_xattr.h>
45 #include <linux/uio.h>
48 #include "transaction.h"
49 #include "btrfs_inode.h"
50 #include "print-tree.h"
51 #include "ordered-data.h"
55 #include "compression.h"
57 #include "free-space-cache.h"
58 #include "inode-map.h"
65 struct btrfs_iget_args
{
66 struct btrfs_key
*location
;
67 struct btrfs_root
*root
;
70 struct btrfs_dio_data
{
71 u64 outstanding_extents
;
73 u64 unsubmitted_oe_range_start
;
74 u64 unsubmitted_oe_range_end
;
77 static const struct inode_operations btrfs_dir_inode_operations
;
78 static const struct inode_operations btrfs_symlink_inode_operations
;
79 static const struct inode_operations btrfs_dir_ro_inode_operations
;
80 static const struct inode_operations btrfs_special_inode_operations
;
81 static const struct inode_operations btrfs_file_inode_operations
;
82 static const struct address_space_operations btrfs_aops
;
83 static const struct address_space_operations btrfs_symlink_aops
;
84 static const struct file_operations btrfs_dir_file_operations
;
85 static const struct extent_io_ops btrfs_extent_io_ops
;
87 static struct kmem_cache
*btrfs_inode_cachep
;
88 struct kmem_cache
*btrfs_trans_handle_cachep
;
89 struct kmem_cache
*btrfs_transaction_cachep
;
90 struct kmem_cache
*btrfs_path_cachep
;
91 struct kmem_cache
*btrfs_free_space_cachep
;
94 static const unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
95 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
96 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
97 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
98 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
99 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
100 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
101 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
104 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
105 static int btrfs_truncate(struct inode
*inode
);
106 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
107 static noinline
int cow_file_range(struct inode
*inode
,
108 struct page
*locked_page
,
109 u64 start
, u64 end
, u64 delalloc_end
,
110 int *page_started
, unsigned long *nr_written
,
111 int unlock
, struct btrfs_dedupe_hash
*hash
);
112 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
113 u64 len
, u64 orig_start
,
114 u64 block_start
, u64 block_len
,
115 u64 orig_block_len
, u64 ram_bytes
,
118 static int btrfs_dirty_inode(struct inode
*inode
);
120 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121 void btrfs_test_inode_set_ops(struct inode
*inode
)
123 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
127 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
128 struct inode
*inode
, struct inode
*dir
,
129 const struct qstr
*qstr
)
133 err
= btrfs_init_acl(trans
, inode
, dir
);
135 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
140 * this does all the hard work for inserting an inline extent into
141 * the btree. The caller should have done a btrfs_drop_extents so that
142 * no overlapping inline items exist in the btree
144 static int insert_inline_extent(struct btrfs_trans_handle
*trans
,
145 struct btrfs_path
*path
, int extent_inserted
,
146 struct btrfs_root
*root
, struct inode
*inode
,
147 u64 start
, size_t size
, size_t compressed_size
,
149 struct page
**compressed_pages
)
151 struct extent_buffer
*leaf
;
152 struct page
*page
= NULL
;
155 struct btrfs_file_extent_item
*ei
;
158 size_t cur_size
= size
;
159 unsigned long offset
;
161 if (compressed_size
&& compressed_pages
)
162 cur_size
= compressed_size
;
164 inode_add_bytes(inode
, size
);
166 if (!extent_inserted
) {
167 struct btrfs_key key
;
170 key
.objectid
= btrfs_ino(inode
);
172 key
.type
= BTRFS_EXTENT_DATA_KEY
;
174 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
175 path
->leave_spinning
= 1;
176 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
183 leaf
= path
->nodes
[0];
184 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
185 struct btrfs_file_extent_item
);
186 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
187 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
188 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
189 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
190 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
191 ptr
= btrfs_file_extent_inline_start(ei
);
193 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
196 while (compressed_size
> 0) {
197 cpage
= compressed_pages
[i
];
198 cur_size
= min_t(unsigned long, compressed_size
,
201 kaddr
= kmap_atomic(cpage
);
202 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
203 kunmap_atomic(kaddr
);
207 compressed_size
-= cur_size
;
209 btrfs_set_file_extent_compression(leaf
, ei
,
212 page
= find_get_page(inode
->i_mapping
,
213 start
>> PAGE_SHIFT
);
214 btrfs_set_file_extent_compression(leaf
, ei
, 0);
215 kaddr
= kmap_atomic(page
);
216 offset
= start
& (PAGE_SIZE
- 1);
217 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
218 kunmap_atomic(kaddr
);
221 btrfs_mark_buffer_dirty(leaf
);
222 btrfs_release_path(path
);
225 * we're an inline extent, so nobody can
226 * extend the file past i_size without locking
227 * a page we already have locked.
229 * We must do any isize and inode updates
230 * before we unlock the pages. Otherwise we
231 * could end up racing with unlink.
233 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
234 ret
= btrfs_update_inode(trans
, root
, inode
);
243 * conditionally insert an inline extent into the file. This
244 * does the checks required to make sure the data is small enough
245 * to fit as an inline extent.
247 static noinline
int cow_file_range_inline(struct btrfs_root
*root
,
248 struct inode
*inode
, u64 start
,
249 u64 end
, size_t compressed_size
,
251 struct page
**compressed_pages
)
253 struct btrfs_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
, root
->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
> root
->sectorsize
||
269 data_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
271 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
273 data_len
> root
->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
= &root
->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
, root
);
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_root
*root
= BTRFS_I(inode
)->root
;
379 if (btrfs_test_opt(root
->fs_info
, FORCE_COMPRESS
))
381 /* bad compression ratios */
382 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
384 if (btrfs_test_opt(root
->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_root
*root
= BTRFS_I(inode
)->root
;
416 u64 blocksize
= root
->sectorsize
;
418 u64 isize
= i_size_read(inode
);
420 struct page
**pages
= NULL
;
421 unsigned long nr_pages
;
422 unsigned long nr_pages_ret
= 0;
423 unsigned long total_compressed
= 0;
424 unsigned long total_in
= 0;
425 unsigned long max_compressed
= SZ_128K
;
426 unsigned long max_uncompressed
= SZ_128K
;
429 int compress_type
= root
->fs_info
->compress_type
;
432 /* if this is a small write inside eof, kick off a defrag */
433 if ((end
- start
+ 1) < SZ_16K
&&
434 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
435 btrfs_add_inode_defrag(NULL
, inode
);
437 actual_end
= min_t(u64
, isize
, end
+ 1);
440 nr_pages
= (end
>> PAGE_SHIFT
) - (start
>> PAGE_SHIFT
) + 1;
441 nr_pages
= min_t(unsigned long, nr_pages
, SZ_128K
/ PAGE_SIZE
);
444 * we don't want to send crud past the end of i_size through
445 * compression, that's just a waste of CPU time. So, if the
446 * end of the file is before the start of our current
447 * requested range of bytes, we bail out to the uncompressed
448 * cleanup code that can deal with all of this.
450 * It isn't really the fastest way to fix things, but this is a
451 * very uncommon corner.
453 if (actual_end
<= start
)
454 goto cleanup_and_bail_uncompressed
;
456 total_compressed
= actual_end
- start
;
459 * skip compression for a small file range(<=blocksize) that
460 * isn't an inline extent, since it doesn't save disk space at all.
462 if (total_compressed
<= blocksize
&&
463 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
464 goto cleanup_and_bail_uncompressed
;
466 /* we want to make sure that amount of ram required to uncompress
467 * an extent is reasonable, so we limit the total size in ram
468 * of a compressed extent to 128k. This is a crucial number
469 * because it also controls how easily we can spread reads across
470 * cpus for decompression.
472 * We also want to make sure the amount of IO required to do
473 * a random read is reasonably small, so we limit the size of
474 * a compressed extent to 128k.
476 total_compressed
= min(total_compressed
, max_uncompressed
);
477 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
478 num_bytes
= max(blocksize
, num_bytes
);
483 * we do compression for mount -o compress and when the
484 * inode has not been flagged as nocompress. This flag can
485 * change at any time if we discover bad compression ratios.
487 if (inode_need_compress(inode
)) {
489 pages
= kcalloc(nr_pages
, sizeof(struct page
*), GFP_NOFS
);
491 /* just bail out to the uncompressed code */
495 if (BTRFS_I(inode
)->force_compress
)
496 compress_type
= BTRFS_I(inode
)->force_compress
;
499 * we need to call clear_page_dirty_for_io on each
500 * page in the range. Otherwise applications with the file
501 * mmap'd can wander in and change the page contents while
502 * we are compressing them.
504 * If the compression fails for any reason, we set the pages
505 * dirty again later on.
507 extent_range_clear_dirty_for_io(inode
, start
, end
);
509 ret
= btrfs_compress_pages(compress_type
,
510 inode
->i_mapping
, start
,
511 total_compressed
, pages
,
512 nr_pages
, &nr_pages_ret
,
518 unsigned long offset
= total_compressed
&
520 struct page
*page
= pages
[nr_pages_ret
- 1];
523 /* zero the tail end of the last page, we might be
524 * sending it down to disk
527 kaddr
= kmap_atomic(page
);
528 memset(kaddr
+ offset
, 0,
530 kunmap_atomic(kaddr
);
537 /* lets try to make an inline extent */
538 if (ret
|| total_in
< (actual_end
- start
)) {
539 /* we didn't compress the entire range, try
540 * to make an uncompressed inline extent.
542 ret
= cow_file_range_inline(root
, inode
, start
, end
,
545 /* try making a compressed inline extent */
546 ret
= cow_file_range_inline(root
, inode
, start
, end
,
548 compress_type
, pages
);
551 unsigned long clear_flags
= EXTENT_DELALLOC
|
553 unsigned long page_error_op
;
555 clear_flags
|= (ret
< 0) ? EXTENT_DO_ACCOUNTING
: 0;
556 page_error_op
= ret
< 0 ? PAGE_SET_ERROR
: 0;
559 * inline extent creation worked or returned error,
560 * we don't need to create any more async work items.
561 * Unlock and free up our temp pages.
563 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
570 btrfs_free_reserved_data_space_noquota(inode
, start
,
578 * we aren't doing an inline extent round the compressed size
579 * up to a block size boundary so the allocator does sane
582 total_compressed
= ALIGN(total_compressed
, blocksize
);
585 * one last check to make sure the compression is really a
586 * win, compare the page count read with the blocks on disk
588 total_in
= ALIGN(total_in
, PAGE_SIZE
);
589 if (total_compressed
>= total_in
) {
592 num_bytes
= total_in
;
596 * The async work queues will take care of doing actual
597 * allocation on disk for these compressed pages, and
598 * will submit them to the elevator.
600 add_async_extent(async_cow
, start
, num_bytes
,
601 total_compressed
, pages
, nr_pages_ret
,
604 if (start
+ num_bytes
< end
) {
615 * the compression code ran but failed to make things smaller,
616 * free any pages it allocated and our page pointer array
618 for (i
= 0; i
< nr_pages_ret
; i
++) {
619 WARN_ON(pages
[i
]->mapping
);
624 total_compressed
= 0;
627 /* flag the file so we don't compress in the future */
628 if (!btrfs_test_opt(root
->fs_info
, FORCE_COMPRESS
) &&
629 !(BTRFS_I(inode
)->force_compress
)) {
630 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
633 cleanup_and_bail_uncompressed
:
635 * No compression, but we still need to write the pages in the file
636 * we've been given so far. redirty the locked page if it corresponds
637 * to our extent and set things up for the async work queue to run
638 * cow_file_range to do the normal delalloc dance.
640 if (page_offset(locked_page
) >= start
&&
641 page_offset(locked_page
) <= end
)
642 __set_page_dirty_nobuffers(locked_page
);
643 /* unlocked later on in the async handlers */
646 extent_range_redirty_for_io(inode
, start
, end
);
647 add_async_extent(async_cow
, start
, end
- start
+ 1, 0, NULL
, 0,
648 BTRFS_COMPRESS_NONE
);
654 for (i
= 0; i
< nr_pages_ret
; i
++) {
655 WARN_ON(pages
[i
]->mapping
);
661 static void free_async_extent_pages(struct async_extent
*async_extent
)
665 if (!async_extent
->pages
)
668 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
669 WARN_ON(async_extent
->pages
[i
]->mapping
);
670 put_page(async_extent
->pages
[i
]);
672 kfree(async_extent
->pages
);
673 async_extent
->nr_pages
= 0;
674 async_extent
->pages
= NULL
;
678 * phase two of compressed writeback. This is the ordered portion
679 * of the code, which only gets called in the order the work was
680 * queued. We walk all the async extents created by compress_file_range
681 * and send them down to the disk.
683 static noinline
void submit_compressed_extents(struct inode
*inode
,
684 struct async_cow
*async_cow
)
686 struct async_extent
*async_extent
;
688 struct btrfs_key ins
;
689 struct extent_map
*em
;
690 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
691 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
692 struct extent_io_tree
*io_tree
;
696 while (!list_empty(&async_cow
->extents
)) {
697 async_extent
= list_entry(async_cow
->extents
.next
,
698 struct async_extent
, list
);
699 list_del(&async_extent
->list
);
701 io_tree
= &BTRFS_I(inode
)->io_tree
;
704 /* did the compression code fall back to uncompressed IO? */
705 if (!async_extent
->pages
) {
706 int page_started
= 0;
707 unsigned long nr_written
= 0;
709 lock_extent(io_tree
, async_extent
->start
,
710 async_extent
->start
+
711 async_extent
->ram_size
- 1);
713 /* allocate blocks */
714 ret
= cow_file_range(inode
, async_cow
->locked_page
,
716 async_extent
->start
+
717 async_extent
->ram_size
- 1,
718 async_extent
->start
+
719 async_extent
->ram_size
- 1,
720 &page_started
, &nr_written
, 0,
726 * if page_started, cow_file_range inserted an
727 * inline extent and took care of all the unlocking
728 * and IO for us. Otherwise, we need to submit
729 * all those pages down to the drive.
731 if (!page_started
&& !ret
)
732 extent_write_locked_range(io_tree
,
733 inode
, async_extent
->start
,
734 async_extent
->start
+
735 async_extent
->ram_size
- 1,
739 unlock_page(async_cow
->locked_page
);
745 lock_extent(io_tree
, async_extent
->start
,
746 async_extent
->start
+ async_extent
->ram_size
- 1);
748 ret
= btrfs_reserve_extent(root
, async_extent
->ram_size
,
749 async_extent
->compressed_size
,
750 async_extent
->compressed_size
,
751 0, alloc_hint
, &ins
, 1, 1);
753 free_async_extent_pages(async_extent
);
755 if (ret
== -ENOSPC
) {
756 unlock_extent(io_tree
, async_extent
->start
,
757 async_extent
->start
+
758 async_extent
->ram_size
- 1);
761 * we need to redirty the pages if we decide to
762 * fallback to uncompressed IO, otherwise we
763 * will not submit these pages down to lower
766 extent_range_redirty_for_io(inode
,
768 async_extent
->start
+
769 async_extent
->ram_size
- 1);
776 * here we're doing allocation and writeback of the
779 btrfs_drop_extent_cache(inode
, async_extent
->start
,
780 async_extent
->start
+
781 async_extent
->ram_size
- 1, 0);
783 em
= alloc_extent_map();
786 goto out_free_reserve
;
788 em
->start
= async_extent
->start
;
789 em
->len
= async_extent
->ram_size
;
790 em
->orig_start
= em
->start
;
791 em
->mod_start
= em
->start
;
792 em
->mod_len
= em
->len
;
794 em
->block_start
= ins
.objectid
;
795 em
->block_len
= ins
.offset
;
796 em
->orig_block_len
= ins
.offset
;
797 em
->ram_bytes
= async_extent
->ram_size
;
798 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
799 em
->compress_type
= async_extent
->compress_type
;
800 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
801 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
805 write_lock(&em_tree
->lock
);
806 ret
= add_extent_mapping(em_tree
, em
, 1);
807 write_unlock(&em_tree
->lock
);
808 if (ret
!= -EEXIST
) {
812 btrfs_drop_extent_cache(inode
, async_extent
->start
,
813 async_extent
->start
+
814 async_extent
->ram_size
- 1, 0);
818 goto out_free_reserve
;
820 ret
= btrfs_add_ordered_extent_compress(inode
,
823 async_extent
->ram_size
,
825 BTRFS_ORDERED_COMPRESSED
,
826 async_extent
->compress_type
);
828 btrfs_drop_extent_cache(inode
, async_extent
->start
,
829 async_extent
->start
+
830 async_extent
->ram_size
- 1, 0);
831 goto out_free_reserve
;
833 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
836 * clear dirty, set writeback and unlock the pages.
838 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
839 async_extent
->start
+
840 async_extent
->ram_size
- 1,
841 async_extent
->start
+
842 async_extent
->ram_size
- 1,
843 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
,
844 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
846 ret
= btrfs_submit_compressed_write(inode
,
848 async_extent
->ram_size
,
850 ins
.offset
, async_extent
->pages
,
851 async_extent
->nr_pages
);
853 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
854 struct page
*p
= async_extent
->pages
[0];
855 const u64 start
= async_extent
->start
;
856 const u64 end
= start
+ async_extent
->ram_size
- 1;
858 p
->mapping
= inode
->i_mapping
;
859 tree
->ops
->writepage_end_io_hook(p
, start
, end
,
862 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
866 free_async_extent_pages(async_extent
);
868 alloc_hint
= ins
.objectid
+ ins
.offset
;
874 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
875 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
877 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
878 async_extent
->start
+
879 async_extent
->ram_size
- 1,
880 async_extent
->start
+
881 async_extent
->ram_size
- 1,
882 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
|
883 EXTENT_DEFRAG
| EXTENT_DO_ACCOUNTING
,
884 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
885 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
|
887 free_async_extent_pages(async_extent
);
892 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
895 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
896 struct extent_map
*em
;
899 read_lock(&em_tree
->lock
);
900 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
903 * if block start isn't an actual block number then find the
904 * first block in this inode and use that as a hint. If that
905 * block is also bogus then just don't worry about it.
907 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
909 em
= search_extent_mapping(em_tree
, 0, 0);
910 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
911 alloc_hint
= em
->block_start
;
915 alloc_hint
= em
->block_start
;
919 read_unlock(&em_tree
->lock
);
925 * when extent_io.c finds a delayed allocation range in the file,
926 * the call backs end up in this code. The basic idea is to
927 * allocate extents on disk for the range, and create ordered data structs
928 * in ram to track those extents.
930 * locked_page is the page that writepage had locked already. We use
931 * it to make sure we don't do extra locks or unlocks.
933 * *page_started is set to one if we unlock locked_page and do everything
934 * required to start IO on it. It may be clean and already done with
937 static noinline
int cow_file_range(struct inode
*inode
,
938 struct page
*locked_page
,
939 u64 start
, u64 end
, u64 delalloc_end
,
940 int *page_started
, unsigned long *nr_written
,
941 int unlock
, struct btrfs_dedupe_hash
*hash
)
943 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
946 unsigned long ram_size
;
949 u64 blocksize
= root
->sectorsize
;
950 struct btrfs_key ins
;
951 struct extent_map
*em
;
952 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
955 if (btrfs_is_free_space_inode(inode
)) {
961 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
962 num_bytes
= max(blocksize
, num_bytes
);
963 disk_num_bytes
= num_bytes
;
965 /* if this is a small write inside eof, kick off defrag */
966 if (num_bytes
< SZ_64K
&&
967 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
968 btrfs_add_inode_defrag(NULL
, inode
);
971 /* lets try to make an inline extent */
972 ret
= cow_file_range_inline(root
, inode
, start
, end
, 0, 0,
975 extent_clear_unlock_delalloc(inode
, start
, end
,
977 EXTENT_LOCKED
| EXTENT_DELALLOC
|
978 EXTENT_DEFRAG
, PAGE_UNLOCK
|
979 PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
981 btrfs_free_reserved_data_space_noquota(inode
, start
,
983 *nr_written
= *nr_written
+
984 (end
- start
+ PAGE_SIZE
) / PAGE_SIZE
;
987 } else if (ret
< 0) {
992 BUG_ON(disk_num_bytes
>
993 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
995 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
996 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
998 while (disk_num_bytes
> 0) {
1001 cur_alloc_size
= disk_num_bytes
;
1002 ret
= btrfs_reserve_extent(root
, cur_alloc_size
, cur_alloc_size
,
1003 root
->sectorsize
, 0, alloc_hint
,
1008 em
= alloc_extent_map();
1014 em
->orig_start
= em
->start
;
1015 ram_size
= ins
.offset
;
1016 em
->len
= ins
.offset
;
1017 em
->mod_start
= em
->start
;
1018 em
->mod_len
= em
->len
;
1020 em
->block_start
= ins
.objectid
;
1021 em
->block_len
= ins
.offset
;
1022 em
->orig_block_len
= ins
.offset
;
1023 em
->ram_bytes
= ram_size
;
1024 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1025 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1026 em
->generation
= -1;
1029 write_lock(&em_tree
->lock
);
1030 ret
= add_extent_mapping(em_tree
, em
, 1);
1031 write_unlock(&em_tree
->lock
);
1032 if (ret
!= -EEXIST
) {
1033 free_extent_map(em
);
1036 btrfs_drop_extent_cache(inode
, start
,
1037 start
+ ram_size
- 1, 0);
1042 cur_alloc_size
= ins
.offset
;
1043 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
1044 ram_size
, cur_alloc_size
, 0);
1046 goto out_drop_extent_cache
;
1048 if (root
->root_key
.objectid
==
1049 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1050 ret
= btrfs_reloc_clone_csums(inode
, start
,
1053 goto out_drop_extent_cache
;
1056 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
1058 if (disk_num_bytes
< cur_alloc_size
)
1061 /* we're not doing compressed IO, don't unlock the first
1062 * page (which the caller expects to stay locked), don't
1063 * clear any dirty bits and don't set any writeback bits
1065 * Do set the Private2 bit so we know this page was properly
1066 * setup for writepage
1068 op
= unlock
? PAGE_UNLOCK
: 0;
1069 op
|= PAGE_SET_PRIVATE2
;
1071 extent_clear_unlock_delalloc(inode
, start
,
1072 start
+ ram_size
- 1,
1073 delalloc_end
, locked_page
,
1074 EXTENT_LOCKED
| EXTENT_DELALLOC
,
1076 disk_num_bytes
-= cur_alloc_size
;
1077 num_bytes
-= cur_alloc_size
;
1078 alloc_hint
= ins
.objectid
+ ins
.offset
;
1079 start
+= cur_alloc_size
;
1084 out_drop_extent_cache
:
1085 btrfs_drop_extent_cache(inode
, start
, start
+ ram_size
- 1, 0);
1087 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
1088 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
1090 extent_clear_unlock_delalloc(inode
, start
, end
, delalloc_end
,
1092 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
1093 EXTENT_DELALLOC
| EXTENT_DEFRAG
,
1094 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
1095 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
);
1100 * work queue call back to started compression on a file and pages
1102 static noinline
void async_cow_start(struct btrfs_work
*work
)
1104 struct async_cow
*async_cow
;
1106 async_cow
= container_of(work
, struct async_cow
, work
);
1108 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1109 async_cow
->start
, async_cow
->end
, async_cow
,
1111 if (num_added
== 0) {
1112 btrfs_add_delayed_iput(async_cow
->inode
);
1113 async_cow
->inode
= NULL
;
1118 * work queue call back to submit previously compressed pages
1120 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1122 struct async_cow
*async_cow
;
1123 struct btrfs_root
*root
;
1124 unsigned long nr_pages
;
1126 async_cow
= container_of(work
, struct async_cow
, work
);
1128 root
= async_cow
->root
;
1129 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_SIZE
) >>
1133 * atomic_sub_return implies a barrier for waitqueue_active
1135 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1137 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1138 wake_up(&root
->fs_info
->async_submit_wait
);
1140 if (async_cow
->inode
)
1141 submit_compressed_extents(async_cow
->inode
, async_cow
);
1144 static noinline
void async_cow_free(struct btrfs_work
*work
)
1146 struct async_cow
*async_cow
;
1147 async_cow
= container_of(work
, struct async_cow
, work
);
1148 if (async_cow
->inode
)
1149 btrfs_add_delayed_iput(async_cow
->inode
);
1153 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1154 u64 start
, u64 end
, int *page_started
,
1155 unsigned long *nr_written
)
1157 struct async_cow
*async_cow
;
1158 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1159 unsigned long nr_pages
;
1161 int limit
= 10 * SZ_1M
;
1163 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1164 1, 0, NULL
, GFP_NOFS
);
1165 while (start
< end
) {
1166 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1167 BUG_ON(!async_cow
); /* -ENOMEM */
1168 async_cow
->inode
= igrab(inode
);
1169 async_cow
->root
= root
;
1170 async_cow
->locked_page
= locked_page
;
1171 async_cow
->start
= start
;
1173 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
&&
1174 !btrfs_test_opt(root
->fs_info
, FORCE_COMPRESS
))
1177 cur_end
= min(end
, start
+ SZ_512K
- 1);
1179 async_cow
->end
= cur_end
;
1180 INIT_LIST_HEAD(&async_cow
->extents
);
1182 btrfs_init_work(&async_cow
->work
,
1183 btrfs_delalloc_helper
,
1184 async_cow_start
, async_cow_submit
,
1187 nr_pages
= (cur_end
- start
+ PAGE_SIZE
) >>
1189 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1191 btrfs_queue_work(root
->fs_info
->delalloc_workers
,
1194 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1195 wait_event(root
->fs_info
->async_submit_wait
,
1196 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1200 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1201 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1202 wait_event(root
->fs_info
->async_submit_wait
,
1203 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1207 *nr_written
+= nr_pages
;
1208 start
= cur_end
+ 1;
1214 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1215 u64 bytenr
, u64 num_bytes
)
1218 struct btrfs_ordered_sum
*sums
;
1221 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1222 bytenr
+ num_bytes
- 1, &list
, 0);
1223 if (ret
== 0 && list_empty(&list
))
1226 while (!list_empty(&list
)) {
1227 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1228 list_del(&sums
->list
);
1235 * when nowcow writeback call back. This checks for snapshots or COW copies
1236 * of the extents that exist in the file, and COWs the file as required.
1238 * If no cow copies or snapshots exist, we write directly to the existing
1241 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1242 struct page
*locked_page
,
1243 u64 start
, u64 end
, int *page_started
, int force
,
1244 unsigned long *nr_written
)
1246 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1247 struct btrfs_trans_handle
*trans
;
1248 struct extent_buffer
*leaf
;
1249 struct btrfs_path
*path
;
1250 struct btrfs_file_extent_item
*fi
;
1251 struct btrfs_key found_key
;
1266 u64 ino
= btrfs_ino(inode
);
1268 path
= btrfs_alloc_path();
1270 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
1272 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1273 EXTENT_DO_ACCOUNTING
|
1274 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1276 PAGE_SET_WRITEBACK
|
1277 PAGE_END_WRITEBACK
);
1281 nolock
= btrfs_is_free_space_inode(inode
);
1284 trans
= btrfs_join_transaction_nolock(root
);
1286 trans
= btrfs_join_transaction(root
);
1288 if (IS_ERR(trans
)) {
1289 extent_clear_unlock_delalloc(inode
, start
, end
, end
,
1291 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1292 EXTENT_DO_ACCOUNTING
|
1293 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1295 PAGE_SET_WRITEBACK
|
1296 PAGE_END_WRITEBACK
);
1297 btrfs_free_path(path
);
1298 return PTR_ERR(trans
);
1301 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1303 cow_start
= (u64
)-1;
1306 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1310 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1311 leaf
= path
->nodes
[0];
1312 btrfs_item_key_to_cpu(leaf
, &found_key
,
1313 path
->slots
[0] - 1);
1314 if (found_key
.objectid
== ino
&&
1315 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1320 leaf
= path
->nodes
[0];
1321 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1322 ret
= btrfs_next_leaf(root
, path
);
1327 leaf
= path
->nodes
[0];
1333 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1335 if (found_key
.objectid
> ino
)
1337 if (WARN_ON_ONCE(found_key
.objectid
< ino
) ||
1338 found_key
.type
< BTRFS_EXTENT_DATA_KEY
) {
1342 if (found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1343 found_key
.offset
> end
)
1346 if (found_key
.offset
> cur_offset
) {
1347 extent_end
= found_key
.offset
;
1352 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1353 struct btrfs_file_extent_item
);
1354 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1356 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1357 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1358 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1359 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1360 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1361 extent_end
= found_key
.offset
+
1362 btrfs_file_extent_num_bytes(leaf
, fi
);
1364 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1365 if (extent_end
<= start
) {
1369 if (disk_bytenr
== 0)
1371 if (btrfs_file_extent_compression(leaf
, fi
) ||
1372 btrfs_file_extent_encryption(leaf
, fi
) ||
1373 btrfs_file_extent_other_encoding(leaf
, fi
))
1375 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1377 if (btrfs_extent_readonly(root
, disk_bytenr
))
1379 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1381 extent_offset
, disk_bytenr
))
1383 disk_bytenr
+= extent_offset
;
1384 disk_bytenr
+= cur_offset
- found_key
.offset
;
1385 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1387 * if there are pending snapshots for this root,
1388 * we fall into common COW way.
1391 err
= btrfs_start_write_no_snapshoting(root
);
1396 * force cow if csum exists in the range.
1397 * this ensure that csum for a given extent are
1398 * either valid or do not exist.
1400 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1402 if (!btrfs_inc_nocow_writers(root
->fs_info
,
1406 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1407 extent_end
= found_key
.offset
+
1408 btrfs_file_extent_inline_len(leaf
,
1409 path
->slots
[0], fi
);
1410 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1415 if (extent_end
<= start
) {
1417 if (!nolock
&& nocow
)
1418 btrfs_end_write_no_snapshoting(root
);
1420 btrfs_dec_nocow_writers(root
->fs_info
,
1425 if (cow_start
== (u64
)-1)
1426 cow_start
= cur_offset
;
1427 cur_offset
= extent_end
;
1428 if (cur_offset
> end
)
1434 btrfs_release_path(path
);
1435 if (cow_start
!= (u64
)-1) {
1436 ret
= cow_file_range(inode
, locked_page
,
1437 cow_start
, found_key
.offset
- 1,
1438 end
, page_started
, nr_written
, 1,
1441 if (!nolock
&& nocow
)
1442 btrfs_end_write_no_snapshoting(root
);
1444 btrfs_dec_nocow_writers(root
->fs_info
,
1448 cow_start
= (u64
)-1;
1451 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1452 struct extent_map
*em
;
1453 struct extent_map_tree
*em_tree
;
1454 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1455 em
= alloc_extent_map();
1456 BUG_ON(!em
); /* -ENOMEM */
1457 em
->start
= cur_offset
;
1458 em
->orig_start
= found_key
.offset
- extent_offset
;
1459 em
->len
= num_bytes
;
1460 em
->block_len
= num_bytes
;
1461 em
->block_start
= disk_bytenr
;
1462 em
->orig_block_len
= disk_num_bytes
;
1463 em
->ram_bytes
= ram_bytes
;
1464 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1465 em
->mod_start
= em
->start
;
1466 em
->mod_len
= em
->len
;
1467 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1468 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1469 em
->generation
= -1;
1471 write_lock(&em_tree
->lock
);
1472 ret
= add_extent_mapping(em_tree
, em
, 1);
1473 write_unlock(&em_tree
->lock
);
1474 if (ret
!= -EEXIST
) {
1475 free_extent_map(em
);
1478 btrfs_drop_extent_cache(inode
, em
->start
,
1479 em
->start
+ em
->len
- 1, 0);
1481 type
= BTRFS_ORDERED_PREALLOC
;
1483 type
= BTRFS_ORDERED_NOCOW
;
1486 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1487 num_bytes
, num_bytes
, type
);
1489 btrfs_dec_nocow_writers(root
->fs_info
, disk_bytenr
);
1490 BUG_ON(ret
); /* -ENOMEM */
1492 if (root
->root_key
.objectid
==
1493 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1494 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1497 if (!nolock
&& nocow
)
1498 btrfs_end_write_no_snapshoting(root
);
1503 extent_clear_unlock_delalloc(inode
, cur_offset
,
1504 cur_offset
+ num_bytes
- 1, end
,
1505 locked_page
, EXTENT_LOCKED
|
1507 EXTENT_CLEAR_DATA_RESV
,
1508 PAGE_UNLOCK
| PAGE_SET_PRIVATE2
);
1510 if (!nolock
&& nocow
)
1511 btrfs_end_write_no_snapshoting(root
);
1512 cur_offset
= extent_end
;
1513 if (cur_offset
> end
)
1516 btrfs_release_path(path
);
1518 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1519 cow_start
= cur_offset
;
1523 if (cow_start
!= (u64
)-1) {
1524 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
, end
,
1525 page_started
, nr_written
, 1, NULL
);
1531 err
= btrfs_end_transaction(trans
, root
);
1535 if (ret
&& cur_offset
< end
)
1536 extent_clear_unlock_delalloc(inode
, cur_offset
, end
, end
,
1537 locked_page
, EXTENT_LOCKED
|
1538 EXTENT_DELALLOC
| EXTENT_DEFRAG
|
1539 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1541 PAGE_SET_WRITEBACK
|
1542 PAGE_END_WRITEBACK
);
1543 btrfs_free_path(path
);
1547 static inline int need_force_cow(struct inode
*inode
, u64 start
, u64 end
)
1550 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
1551 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
))
1555 * @defrag_bytes is a hint value, no spinlock held here,
1556 * if is not zero, it means the file is defragging.
1557 * Force cow if given extent needs to be defragged.
1559 if (BTRFS_I(inode
)->defrag_bytes
&&
1560 test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
1561 EXTENT_DEFRAG
, 0, NULL
))
1568 * extent_io.c call back to do delayed allocation processing
1570 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1571 u64 start
, u64 end
, int *page_started
,
1572 unsigned long *nr_written
)
1575 int force_cow
= need_force_cow(inode
, start
, end
);
1577 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
&& !force_cow
) {
1578 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1579 page_started
, 1, nr_written
);
1580 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
&& !force_cow
) {
1581 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1582 page_started
, 0, nr_written
);
1583 } else if (!inode_need_compress(inode
)) {
1584 ret
= cow_file_range(inode
, locked_page
, start
, end
, end
,
1585 page_started
, nr_written
, 1, NULL
);
1587 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1588 &BTRFS_I(inode
)->runtime_flags
);
1589 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1590 page_started
, nr_written
);
1595 static void btrfs_split_extent_hook(struct inode
*inode
,
1596 struct extent_state
*orig
, u64 split
)
1600 /* not delalloc, ignore it */
1601 if (!(orig
->state
& EXTENT_DELALLOC
))
1604 size
= orig
->end
- orig
->start
+ 1;
1605 if (size
> BTRFS_MAX_EXTENT_SIZE
) {
1610 * See the explanation in btrfs_merge_extent_hook, the same
1611 * applies here, just in reverse.
1613 new_size
= orig
->end
- split
+ 1;
1614 num_extents
= div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1615 BTRFS_MAX_EXTENT_SIZE
);
1616 new_size
= split
- orig
->start
;
1617 num_extents
+= div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1618 BTRFS_MAX_EXTENT_SIZE
);
1619 if (div64_u64(size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1620 BTRFS_MAX_EXTENT_SIZE
) >= num_extents
)
1624 spin_lock(&BTRFS_I(inode
)->lock
);
1625 BTRFS_I(inode
)->outstanding_extents
++;
1626 spin_unlock(&BTRFS_I(inode
)->lock
);
1630 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1631 * extents so we can keep track of new extents that are just merged onto old
1632 * extents, such as when we are doing sequential writes, so we can properly
1633 * account for the metadata space we'll need.
1635 static void btrfs_merge_extent_hook(struct inode
*inode
,
1636 struct extent_state
*new,
1637 struct extent_state
*other
)
1639 u64 new_size
, old_size
;
1642 /* not delalloc, ignore it */
1643 if (!(other
->state
& EXTENT_DELALLOC
))
1646 if (new->start
> other
->start
)
1647 new_size
= new->end
- other
->start
+ 1;
1649 new_size
= other
->end
- new->start
+ 1;
1651 /* we're not bigger than the max, unreserve the space and go */
1652 if (new_size
<= BTRFS_MAX_EXTENT_SIZE
) {
1653 spin_lock(&BTRFS_I(inode
)->lock
);
1654 BTRFS_I(inode
)->outstanding_extents
--;
1655 spin_unlock(&BTRFS_I(inode
)->lock
);
1660 * We have to add up either side to figure out how many extents were
1661 * accounted for before we merged into one big extent. If the number of
1662 * extents we accounted for is <= the amount we need for the new range
1663 * then we can return, otherwise drop. Think of it like this
1667 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1668 * need 2 outstanding extents, on one side we have 1 and the other side
1669 * we have 1 so they are == and we can return. But in this case
1671 * [MAX_SIZE+4k][MAX_SIZE+4k]
1673 * Each range on their own accounts for 2 extents, but merged together
1674 * they are only 3 extents worth of accounting, so we need to drop in
1677 old_size
= other
->end
- other
->start
+ 1;
1678 num_extents
= div64_u64(old_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1679 BTRFS_MAX_EXTENT_SIZE
);
1680 old_size
= new->end
- new->start
+ 1;
1681 num_extents
+= div64_u64(old_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1682 BTRFS_MAX_EXTENT_SIZE
);
1684 if (div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1685 BTRFS_MAX_EXTENT_SIZE
) >= num_extents
)
1688 spin_lock(&BTRFS_I(inode
)->lock
);
1689 BTRFS_I(inode
)->outstanding_extents
--;
1690 spin_unlock(&BTRFS_I(inode
)->lock
);
1693 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1694 struct inode
*inode
)
1696 spin_lock(&root
->delalloc_lock
);
1697 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1698 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1699 &root
->delalloc_inodes
);
1700 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1701 &BTRFS_I(inode
)->runtime_flags
);
1702 root
->nr_delalloc_inodes
++;
1703 if (root
->nr_delalloc_inodes
== 1) {
1704 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1705 BUG_ON(!list_empty(&root
->delalloc_root
));
1706 list_add_tail(&root
->delalloc_root
,
1707 &root
->fs_info
->delalloc_roots
);
1708 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1711 spin_unlock(&root
->delalloc_lock
);
1714 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1715 struct inode
*inode
)
1717 spin_lock(&root
->delalloc_lock
);
1718 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1719 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1720 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1721 &BTRFS_I(inode
)->runtime_flags
);
1722 root
->nr_delalloc_inodes
--;
1723 if (!root
->nr_delalloc_inodes
) {
1724 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1725 BUG_ON(list_empty(&root
->delalloc_root
));
1726 list_del_init(&root
->delalloc_root
);
1727 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1730 spin_unlock(&root
->delalloc_lock
);
1734 * extent_io.c set_bit_hook, used to track delayed allocation
1735 * bytes in this file, and to maintain the list of inodes that
1736 * have pending delalloc work to be done.
1738 static void btrfs_set_bit_hook(struct inode
*inode
,
1739 struct extent_state
*state
, unsigned *bits
)
1742 if ((*bits
& EXTENT_DEFRAG
) && !(*bits
& EXTENT_DELALLOC
))
1745 * set_bit and clear bit hooks normally require _irqsave/restore
1746 * but in this case, we are only testing for the DELALLOC
1747 * bit, which is only set or cleared with irqs on
1749 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1750 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1751 u64 len
= state
->end
+ 1 - state
->start
;
1752 bool do_list
= !btrfs_is_free_space_inode(inode
);
1754 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1755 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1757 spin_lock(&BTRFS_I(inode
)->lock
);
1758 BTRFS_I(inode
)->outstanding_extents
++;
1759 spin_unlock(&BTRFS_I(inode
)->lock
);
1762 /* For sanity tests */
1763 if (btrfs_is_testing(root
->fs_info
))
1766 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1767 root
->fs_info
->delalloc_batch
);
1768 spin_lock(&BTRFS_I(inode
)->lock
);
1769 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1770 if (*bits
& EXTENT_DEFRAG
)
1771 BTRFS_I(inode
)->defrag_bytes
+= len
;
1772 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1773 &BTRFS_I(inode
)->runtime_flags
))
1774 btrfs_add_delalloc_inodes(root
, inode
);
1775 spin_unlock(&BTRFS_I(inode
)->lock
);
1780 * extent_io.c clear_bit_hook, see set_bit_hook for why
1782 static void btrfs_clear_bit_hook(struct inode
*inode
,
1783 struct extent_state
*state
,
1786 u64 len
= state
->end
+ 1 - state
->start
;
1787 u64 num_extents
= div64_u64(len
+ BTRFS_MAX_EXTENT_SIZE
-1,
1788 BTRFS_MAX_EXTENT_SIZE
);
1790 spin_lock(&BTRFS_I(inode
)->lock
);
1791 if ((state
->state
& EXTENT_DEFRAG
) && (*bits
& EXTENT_DEFRAG
))
1792 BTRFS_I(inode
)->defrag_bytes
-= len
;
1793 spin_unlock(&BTRFS_I(inode
)->lock
);
1796 * set_bit and clear bit hooks normally require _irqsave/restore
1797 * but in this case, we are only testing for the DELALLOC
1798 * bit, which is only set or cleared with irqs on
1800 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1801 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1802 bool do_list
= !btrfs_is_free_space_inode(inode
);
1804 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1805 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1806 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1807 spin_lock(&BTRFS_I(inode
)->lock
);
1808 BTRFS_I(inode
)->outstanding_extents
-= num_extents
;
1809 spin_unlock(&BTRFS_I(inode
)->lock
);
1813 * We don't reserve metadata space for space cache inodes so we
1814 * don't need to call dellalloc_release_metadata if there is an
1817 if (*bits
& EXTENT_DO_ACCOUNTING
&&
1818 root
!= root
->fs_info
->tree_root
)
1819 btrfs_delalloc_release_metadata(inode
, len
);
1821 /* For sanity tests. */
1822 if (btrfs_is_testing(root
->fs_info
))
1825 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1826 && do_list
&& !(state
->state
& EXTENT_NORESERVE
)
1827 && (*bits
& (EXTENT_DO_ACCOUNTING
|
1828 EXTENT_CLEAR_DATA_RESV
)))
1829 btrfs_free_reserved_data_space_noquota(inode
,
1832 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1833 root
->fs_info
->delalloc_batch
);
1834 spin_lock(&BTRFS_I(inode
)->lock
);
1835 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1836 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1837 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1838 &BTRFS_I(inode
)->runtime_flags
))
1839 btrfs_del_delalloc_inode(root
, inode
);
1840 spin_unlock(&BTRFS_I(inode
)->lock
);
1845 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1846 * we don't create bios that span stripes or chunks
1848 * return 1 if page cannot be merged to bio
1849 * return 0 if page can be merged to bio
1850 * return error otherwise
1852 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1853 size_t size
, struct bio
*bio
,
1854 unsigned long bio_flags
)
1856 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1857 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
1862 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1865 length
= bio
->bi_iter
.bi_size
;
1866 map_length
= length
;
1867 ret
= btrfs_map_block(root
->fs_info
, bio_op(bio
), logical
,
1868 &map_length
, NULL
, 0);
1871 if (map_length
< length
+ size
)
1877 * in order to insert checksums into the metadata in large chunks,
1878 * we wait until bio submission time. All the pages in the bio are
1879 * checksummed and sums are attached onto the ordered extent record.
1881 * At IO completion time the cums attached on the ordered extent record
1882 * are inserted into the btree
1884 static int __btrfs_submit_bio_start(struct inode
*inode
, struct bio
*bio
,
1885 int mirror_num
, unsigned long bio_flags
,
1888 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1891 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1892 BUG_ON(ret
); /* -ENOMEM */
1897 * in order to insert checksums into the metadata in large chunks,
1898 * we wait until bio submission time. All the pages in the bio are
1899 * checksummed and sums are attached onto the ordered extent record.
1901 * At IO completion time the cums attached on the ordered extent record
1902 * are inserted into the btree
1904 static int __btrfs_submit_bio_done(struct inode
*inode
, struct bio
*bio
,
1905 int mirror_num
, unsigned long bio_flags
,
1908 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1911 ret
= btrfs_map_bio(root
, bio
, mirror_num
, 1);
1913 bio
->bi_error
= ret
;
1920 * extent_io.c submission hook. This does the right thing for csum calculation
1921 * on write, or reading the csums from the tree before a read
1923 static int btrfs_submit_bio_hook(struct inode
*inode
, struct bio
*bio
,
1924 int mirror_num
, unsigned long bio_flags
,
1927 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1928 enum btrfs_wq_endio_type metadata
= BTRFS_WQ_ENDIO_DATA
;
1931 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1933 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1935 if (btrfs_is_free_space_inode(inode
))
1936 metadata
= BTRFS_WQ_ENDIO_FREE_SPACE
;
1938 if (bio_op(bio
) != REQ_OP_WRITE
) {
1939 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1943 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1944 ret
= btrfs_submit_compressed_read(inode
, bio
,
1948 } else if (!skip_sum
) {
1949 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1954 } else if (async
&& !skip_sum
) {
1955 /* csum items have already been cloned */
1956 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1958 /* we're doing a write, do the async checksumming */
1959 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1960 inode
, bio
, mirror_num
,
1961 bio_flags
, bio_offset
,
1962 __btrfs_submit_bio_start
,
1963 __btrfs_submit_bio_done
);
1965 } else if (!skip_sum
) {
1966 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1972 ret
= btrfs_map_bio(root
, bio
, mirror_num
, 0);
1976 bio
->bi_error
= ret
;
1983 * given a list of ordered sums record them in the inode. This happens
1984 * at IO completion time based on sums calculated at bio submission time.
1986 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1987 struct inode
*inode
, u64 file_offset
,
1988 struct list_head
*list
)
1990 struct btrfs_ordered_sum
*sum
;
1992 list_for_each_entry(sum
, list
, list
) {
1993 trans
->adding_csums
= 1;
1994 btrfs_csum_file_blocks(trans
,
1995 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1996 trans
->adding_csums
= 0;
2001 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
2002 struct extent_state
**cached_state
, int dedupe
)
2004 WARN_ON((end
& (PAGE_SIZE
- 1)) == 0);
2005 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
2009 /* see btrfs_writepage_start_hook for details on why this is required */
2010 struct btrfs_writepage_fixup
{
2012 struct btrfs_work work
;
2015 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
2017 struct btrfs_writepage_fixup
*fixup
;
2018 struct btrfs_ordered_extent
*ordered
;
2019 struct extent_state
*cached_state
= NULL
;
2021 struct inode
*inode
;
2026 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
2030 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
2031 ClearPageChecked(page
);
2035 inode
= page
->mapping
->host
;
2036 page_start
= page_offset(page
);
2037 page_end
= page_offset(page
) + PAGE_SIZE
- 1;
2039 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
2042 /* already ordered? We're done */
2043 if (PagePrivate2(page
))
2046 ordered
= btrfs_lookup_ordered_range(inode
, page_start
,
2049 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
2050 page_end
, &cached_state
, GFP_NOFS
);
2052 btrfs_start_ordered_extent(inode
, ordered
, 1);
2053 btrfs_put_ordered_extent(ordered
);
2057 ret
= btrfs_delalloc_reserve_space(inode
, page_start
,
2060 mapping_set_error(page
->mapping
, ret
);
2061 end_extent_writepage(page
, ret
, page_start
, page_end
);
2062 ClearPageChecked(page
);
2066 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
,
2068 ClearPageChecked(page
);
2069 set_page_dirty(page
);
2071 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
2072 &cached_state
, GFP_NOFS
);
2080 * There are a few paths in the higher layers of the kernel that directly
2081 * set the page dirty bit without asking the filesystem if it is a
2082 * good idea. This causes problems because we want to make sure COW
2083 * properly happens and the data=ordered rules are followed.
2085 * In our case any range that doesn't have the ORDERED bit set
2086 * hasn't been properly setup for IO. We kick off an async process
2087 * to fix it up. The async helper will wait for ordered extents, set
2088 * the delalloc bit and make it safe to write the page.
2090 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
2092 struct inode
*inode
= page
->mapping
->host
;
2093 struct btrfs_writepage_fixup
*fixup
;
2094 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2096 /* this page is properly in the ordered list */
2097 if (TestClearPagePrivate2(page
))
2100 if (PageChecked(page
))
2103 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
2107 SetPageChecked(page
);
2109 btrfs_init_work(&fixup
->work
, btrfs_fixup_helper
,
2110 btrfs_writepage_fixup_worker
, NULL
, NULL
);
2112 btrfs_queue_work(root
->fs_info
->fixup_workers
, &fixup
->work
);
2116 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
2117 struct inode
*inode
, u64 file_pos
,
2118 u64 disk_bytenr
, u64 disk_num_bytes
,
2119 u64 num_bytes
, u64 ram_bytes
,
2120 u8 compression
, u8 encryption
,
2121 u16 other_encoding
, int extent_type
)
2123 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2124 struct btrfs_file_extent_item
*fi
;
2125 struct btrfs_path
*path
;
2126 struct extent_buffer
*leaf
;
2127 struct btrfs_key ins
;
2128 int extent_inserted
= 0;
2131 path
= btrfs_alloc_path();
2136 * we may be replacing one extent in the tree with another.
2137 * The new extent is pinned in the extent map, and we don't want
2138 * to drop it from the cache until it is completely in the btree.
2140 * So, tell btrfs_drop_extents to leave this extent in the cache.
2141 * the caller is expected to unpin it and allow it to be merged
2144 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
, file_pos
,
2145 file_pos
+ num_bytes
, NULL
, 0,
2146 1, sizeof(*fi
), &extent_inserted
);
2150 if (!extent_inserted
) {
2151 ins
.objectid
= btrfs_ino(inode
);
2152 ins
.offset
= file_pos
;
2153 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
2155 path
->leave_spinning
= 1;
2156 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
2161 leaf
= path
->nodes
[0];
2162 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2163 struct btrfs_file_extent_item
);
2164 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
2165 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
2166 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
2167 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
2168 btrfs_set_file_extent_offset(leaf
, fi
, 0);
2169 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
2170 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
2171 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
2172 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
2173 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
2175 btrfs_mark_buffer_dirty(leaf
);
2176 btrfs_release_path(path
);
2178 inode_add_bytes(inode
, num_bytes
);
2180 ins
.objectid
= disk_bytenr
;
2181 ins
.offset
= disk_num_bytes
;
2182 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2183 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
2184 root
->root_key
.objectid
,
2185 btrfs_ino(inode
), file_pos
,
2188 * Release the reserved range from inode dirty range map, as it is
2189 * already moved into delayed_ref_head
2191 btrfs_qgroup_release_data(inode
, file_pos
, ram_bytes
);
2193 btrfs_free_path(path
);
2198 /* snapshot-aware defrag */
2199 struct sa_defrag_extent_backref
{
2200 struct rb_node node
;
2201 struct old_sa_defrag_extent
*old
;
2210 struct old_sa_defrag_extent
{
2211 struct list_head list
;
2212 struct new_sa_defrag_extent
*new;
2221 struct new_sa_defrag_extent
{
2222 struct rb_root root
;
2223 struct list_head head
;
2224 struct btrfs_path
*path
;
2225 struct inode
*inode
;
2233 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
2234 struct sa_defrag_extent_backref
*b2
)
2236 if (b1
->root_id
< b2
->root_id
)
2238 else if (b1
->root_id
> b2
->root_id
)
2241 if (b1
->inum
< b2
->inum
)
2243 else if (b1
->inum
> b2
->inum
)
2246 if (b1
->file_pos
< b2
->file_pos
)
2248 else if (b1
->file_pos
> b2
->file_pos
)
2252 * [------------------------------] ===> (a range of space)
2253 * |<--->| |<---->| =============> (fs/file tree A)
2254 * |<---------------------------->| ===> (fs/file tree B)
2256 * A range of space can refer to two file extents in one tree while
2257 * refer to only one file extent in another tree.
2259 * So we may process a disk offset more than one time(two extents in A)
2260 * and locate at the same extent(one extent in B), then insert two same
2261 * backrefs(both refer to the extent in B).
2266 static void backref_insert(struct rb_root
*root
,
2267 struct sa_defrag_extent_backref
*backref
)
2269 struct rb_node
**p
= &root
->rb_node
;
2270 struct rb_node
*parent
= NULL
;
2271 struct sa_defrag_extent_backref
*entry
;
2276 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2278 ret
= backref_comp(backref
, entry
);
2282 p
= &(*p
)->rb_right
;
2285 rb_link_node(&backref
->node
, parent
, p
);
2286 rb_insert_color(&backref
->node
, root
);
2290 * Note the backref might has changed, and in this case we just return 0.
2292 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2295 struct btrfs_file_extent_item
*extent
;
2296 struct btrfs_fs_info
*fs_info
;
2297 struct old_sa_defrag_extent
*old
= ctx
;
2298 struct new_sa_defrag_extent
*new = old
->new;
2299 struct btrfs_path
*path
= new->path
;
2300 struct btrfs_key key
;
2301 struct btrfs_root
*root
;
2302 struct sa_defrag_extent_backref
*backref
;
2303 struct extent_buffer
*leaf
;
2304 struct inode
*inode
= new->inode
;
2310 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2311 inum
== btrfs_ino(inode
))
2314 key
.objectid
= root_id
;
2315 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2316 key
.offset
= (u64
)-1;
2318 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2319 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2321 if (PTR_ERR(root
) == -ENOENT
)
2324 btrfs_debug(fs_info
, "inum=%llu, offset=%llu, root_id=%llu",
2325 inum
, offset
, root_id
);
2326 return PTR_ERR(root
);
2329 key
.objectid
= inum
;
2330 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2331 if (offset
> (u64
)-1 << 32)
2334 key
.offset
= offset
;
2336 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2337 if (WARN_ON(ret
< 0))
2344 leaf
= path
->nodes
[0];
2345 slot
= path
->slots
[0];
2347 if (slot
>= btrfs_header_nritems(leaf
)) {
2348 ret
= btrfs_next_leaf(root
, path
);
2351 } else if (ret
> 0) {
2360 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2362 if (key
.objectid
> inum
)
2365 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2368 extent
= btrfs_item_ptr(leaf
, slot
,
2369 struct btrfs_file_extent_item
);
2371 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2375 * 'offset' refers to the exact key.offset,
2376 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2377 * (key.offset - extent_offset).
2379 if (key
.offset
!= offset
)
2382 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2383 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2385 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2386 old
->len
|| extent_offset
+ num_bytes
<=
2387 old
->extent_offset
+ old
->offset
)
2392 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2398 backref
->root_id
= root_id
;
2399 backref
->inum
= inum
;
2400 backref
->file_pos
= offset
;
2401 backref
->num_bytes
= num_bytes
;
2402 backref
->extent_offset
= extent_offset
;
2403 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2405 backref_insert(&new->root
, backref
);
2408 btrfs_release_path(path
);
2413 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2414 struct new_sa_defrag_extent
*new)
2416 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2417 struct old_sa_defrag_extent
*old
, *tmp
;
2422 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2423 ret
= iterate_inodes_from_logical(old
->bytenr
+
2424 old
->extent_offset
, fs_info
,
2425 path
, record_one_backref
,
2427 if (ret
< 0 && ret
!= -ENOENT
)
2430 /* no backref to be processed for this extent */
2432 list_del(&old
->list
);
2437 if (list_empty(&new->head
))
2443 static int relink_is_mergable(struct extent_buffer
*leaf
,
2444 struct btrfs_file_extent_item
*fi
,
2445 struct new_sa_defrag_extent
*new)
2447 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != new->bytenr
)
2450 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2453 if (btrfs_file_extent_compression(leaf
, fi
) != new->compress_type
)
2456 if (btrfs_file_extent_encryption(leaf
, fi
) ||
2457 btrfs_file_extent_other_encoding(leaf
, fi
))
2464 * Note the backref might has changed, and in this case we just return 0.
2466 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2467 struct sa_defrag_extent_backref
*prev
,
2468 struct sa_defrag_extent_backref
*backref
)
2470 struct btrfs_file_extent_item
*extent
;
2471 struct btrfs_file_extent_item
*item
;
2472 struct btrfs_ordered_extent
*ordered
;
2473 struct btrfs_trans_handle
*trans
;
2474 struct btrfs_fs_info
*fs_info
;
2475 struct btrfs_root
*root
;
2476 struct btrfs_key key
;
2477 struct extent_buffer
*leaf
;
2478 struct old_sa_defrag_extent
*old
= backref
->old
;
2479 struct new_sa_defrag_extent
*new = old
->new;
2480 struct inode
*src_inode
= new->inode
;
2481 struct inode
*inode
;
2482 struct extent_state
*cached
= NULL
;
2491 if (prev
&& prev
->root_id
== backref
->root_id
&&
2492 prev
->inum
== backref
->inum
&&
2493 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2496 /* step 1: get root */
2497 key
.objectid
= backref
->root_id
;
2498 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2499 key
.offset
= (u64
)-1;
2501 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2502 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2504 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2506 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2507 if (PTR_ERR(root
) == -ENOENT
)
2509 return PTR_ERR(root
);
2512 if (btrfs_root_readonly(root
)) {
2513 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2517 /* step 2: get inode */
2518 key
.objectid
= backref
->inum
;
2519 key
.type
= BTRFS_INODE_ITEM_KEY
;
2522 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2523 if (IS_ERR(inode
)) {
2524 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2528 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2530 /* step 3: relink backref */
2531 lock_start
= backref
->file_pos
;
2532 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2533 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2536 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2538 btrfs_put_ordered_extent(ordered
);
2542 trans
= btrfs_join_transaction(root
);
2543 if (IS_ERR(trans
)) {
2544 ret
= PTR_ERR(trans
);
2548 key
.objectid
= backref
->inum
;
2549 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2550 key
.offset
= backref
->file_pos
;
2552 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2555 } else if (ret
> 0) {
2560 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2561 struct btrfs_file_extent_item
);
2563 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2564 backref
->generation
)
2567 btrfs_release_path(path
);
2569 start
= backref
->file_pos
;
2570 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2571 start
+= old
->extent_offset
+ old
->offset
-
2572 backref
->extent_offset
;
2574 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2575 old
->extent_offset
+ old
->offset
+ old
->len
);
2576 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2578 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2583 key
.objectid
= btrfs_ino(inode
);
2584 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2587 path
->leave_spinning
= 1;
2589 struct btrfs_file_extent_item
*fi
;
2591 struct btrfs_key found_key
;
2593 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2598 leaf
= path
->nodes
[0];
2599 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2601 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2602 struct btrfs_file_extent_item
);
2603 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2605 if (extent_len
+ found_key
.offset
== start
&&
2606 relink_is_mergable(leaf
, fi
, new)) {
2607 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2609 btrfs_mark_buffer_dirty(leaf
);
2610 inode_add_bytes(inode
, len
);
2616 btrfs_release_path(path
);
2621 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2624 btrfs_abort_transaction(trans
, ret
);
2628 leaf
= path
->nodes
[0];
2629 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2630 struct btrfs_file_extent_item
);
2631 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2632 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2633 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2634 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2635 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2636 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2637 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2638 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2639 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2640 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2642 btrfs_mark_buffer_dirty(leaf
);
2643 inode_add_bytes(inode
, len
);
2644 btrfs_release_path(path
);
2646 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2648 backref
->root_id
, backref
->inum
,
2649 new->file_pos
); /* start - extent_offset */
2651 btrfs_abort_transaction(trans
, ret
);
2657 btrfs_release_path(path
);
2658 path
->leave_spinning
= 0;
2659 btrfs_end_transaction(trans
, root
);
2661 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2667 static void free_sa_defrag_extent(struct new_sa_defrag_extent
*new)
2669 struct old_sa_defrag_extent
*old
, *tmp
;
2674 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2680 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2682 struct btrfs_path
*path
;
2683 struct sa_defrag_extent_backref
*backref
;
2684 struct sa_defrag_extent_backref
*prev
= NULL
;
2685 struct inode
*inode
;
2686 struct btrfs_root
*root
;
2687 struct rb_node
*node
;
2691 root
= BTRFS_I(inode
)->root
;
2693 path
= btrfs_alloc_path();
2697 if (!record_extent_backrefs(path
, new)) {
2698 btrfs_free_path(path
);
2701 btrfs_release_path(path
);
2704 node
= rb_first(&new->root
);
2707 rb_erase(node
, &new->root
);
2709 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2711 ret
= relink_extent_backref(path
, prev
, backref
);
2724 btrfs_free_path(path
);
2726 free_sa_defrag_extent(new);
2728 atomic_dec(&root
->fs_info
->defrag_running
);
2729 wake_up(&root
->fs_info
->transaction_wait
);
2732 static struct new_sa_defrag_extent
*
2733 record_old_file_extents(struct inode
*inode
,
2734 struct btrfs_ordered_extent
*ordered
)
2736 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2737 struct btrfs_path
*path
;
2738 struct btrfs_key key
;
2739 struct old_sa_defrag_extent
*old
;
2740 struct new_sa_defrag_extent
*new;
2743 new = kmalloc(sizeof(*new), GFP_NOFS
);
2748 new->file_pos
= ordered
->file_offset
;
2749 new->len
= ordered
->len
;
2750 new->bytenr
= ordered
->start
;
2751 new->disk_len
= ordered
->disk_len
;
2752 new->compress_type
= ordered
->compress_type
;
2753 new->root
= RB_ROOT
;
2754 INIT_LIST_HEAD(&new->head
);
2756 path
= btrfs_alloc_path();
2760 key
.objectid
= btrfs_ino(inode
);
2761 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2762 key
.offset
= new->file_pos
;
2764 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2767 if (ret
> 0 && path
->slots
[0] > 0)
2770 /* find out all the old extents for the file range */
2772 struct btrfs_file_extent_item
*extent
;
2773 struct extent_buffer
*l
;
2782 slot
= path
->slots
[0];
2784 if (slot
>= btrfs_header_nritems(l
)) {
2785 ret
= btrfs_next_leaf(root
, path
);
2793 btrfs_item_key_to_cpu(l
, &key
, slot
);
2795 if (key
.objectid
!= btrfs_ino(inode
))
2797 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2799 if (key
.offset
>= new->file_pos
+ new->len
)
2802 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2804 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2805 if (key
.offset
+ num_bytes
< new->file_pos
)
2808 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2812 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2814 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2818 offset
= max(new->file_pos
, key
.offset
);
2819 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2821 old
->bytenr
= disk_bytenr
;
2822 old
->extent_offset
= extent_offset
;
2823 old
->offset
= offset
- key
.offset
;
2824 old
->len
= end
- offset
;
2827 list_add_tail(&old
->list
, &new->head
);
2833 btrfs_free_path(path
);
2834 atomic_inc(&root
->fs_info
->defrag_running
);
2839 btrfs_free_path(path
);
2841 free_sa_defrag_extent(new);
2845 static void btrfs_release_delalloc_bytes(struct btrfs_root
*root
,
2848 struct btrfs_block_group_cache
*cache
;
2850 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
2853 spin_lock(&cache
->lock
);
2854 cache
->delalloc_bytes
-= len
;
2855 spin_unlock(&cache
->lock
);
2857 btrfs_put_block_group(cache
);
2860 /* as ordered data IO finishes, this gets called so we can finish
2861 * an ordered extent if the range of bytes in the file it covers are
2864 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2866 struct inode
*inode
= ordered_extent
->inode
;
2867 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2868 struct btrfs_trans_handle
*trans
= NULL
;
2869 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2870 struct extent_state
*cached_state
= NULL
;
2871 struct new_sa_defrag_extent
*new = NULL
;
2872 int compress_type
= 0;
2874 u64 logical_len
= ordered_extent
->len
;
2876 bool truncated
= false;
2878 nolock
= btrfs_is_free_space_inode(inode
);
2880 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2885 btrfs_free_io_failure_record(inode
, ordered_extent
->file_offset
,
2886 ordered_extent
->file_offset
+
2887 ordered_extent
->len
- 1);
2889 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2891 logical_len
= ordered_extent
->truncated_len
;
2892 /* Truncated the entire extent, don't bother adding */
2897 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2898 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2901 * For mwrite(mmap + memset to write) case, we still reserve
2902 * space for NOCOW range.
2903 * As NOCOW won't cause a new delayed ref, just free the space
2905 btrfs_qgroup_free_data(inode
, ordered_extent
->file_offset
,
2906 ordered_extent
->len
);
2907 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2909 trans
= btrfs_join_transaction_nolock(root
);
2911 trans
= btrfs_join_transaction(root
);
2912 if (IS_ERR(trans
)) {
2913 ret
= PTR_ERR(trans
);
2917 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2918 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2919 if (ret
) /* -ENOMEM or corruption */
2920 btrfs_abort_transaction(trans
, ret
);
2924 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2925 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2928 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2929 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2930 EXTENT_DEFRAG
, 1, cached_state
);
2932 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2933 if (0 && last_snapshot
>= BTRFS_I(inode
)->generation
)
2934 /* the inode is shared */
2935 new = record_old_file_extents(inode
, ordered_extent
);
2937 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2938 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2939 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2943 trans
= btrfs_join_transaction_nolock(root
);
2945 trans
= btrfs_join_transaction(root
);
2946 if (IS_ERR(trans
)) {
2947 ret
= PTR_ERR(trans
);
2952 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2954 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2955 compress_type
= ordered_extent
->compress_type
;
2956 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2957 BUG_ON(compress_type
);
2958 ret
= btrfs_mark_extent_written(trans
, inode
,
2959 ordered_extent
->file_offset
,
2960 ordered_extent
->file_offset
+
2963 BUG_ON(root
== root
->fs_info
->tree_root
);
2964 ret
= insert_reserved_file_extent(trans
, inode
,
2965 ordered_extent
->file_offset
,
2966 ordered_extent
->start
,
2967 ordered_extent
->disk_len
,
2968 logical_len
, logical_len
,
2969 compress_type
, 0, 0,
2970 BTRFS_FILE_EXTENT_REG
);
2972 btrfs_release_delalloc_bytes(root
,
2973 ordered_extent
->start
,
2974 ordered_extent
->disk_len
);
2976 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2977 ordered_extent
->file_offset
, ordered_extent
->len
,
2980 btrfs_abort_transaction(trans
, ret
);
2984 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2985 &ordered_extent
->list
);
2987 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2988 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2989 if (ret
) { /* -ENOMEM or corruption */
2990 btrfs_abort_transaction(trans
, ret
);
2995 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2996 ordered_extent
->file_offset
+
2997 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2999 if (root
!= root
->fs_info
->tree_root
)
3000 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
3002 btrfs_end_transaction(trans
, root
);
3004 if (ret
|| truncated
) {
3008 start
= ordered_extent
->file_offset
+ logical_len
;
3010 start
= ordered_extent
->file_offset
;
3011 end
= ordered_extent
->file_offset
+ ordered_extent
->len
- 1;
3012 clear_extent_uptodate(io_tree
, start
, end
, NULL
, GFP_NOFS
);
3014 /* Drop the cache for the part of the extent we didn't write. */
3015 btrfs_drop_extent_cache(inode
, start
, end
, 0);
3018 * If the ordered extent had an IOERR or something else went
3019 * wrong we need to return the space for this ordered extent
3020 * back to the allocator. We only free the extent in the
3021 * truncated case if we didn't write out the extent at all.
3023 if ((ret
|| !logical_len
) &&
3024 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
3025 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
3026 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
3027 ordered_extent
->disk_len
, 1);
3032 * This needs to be done to make sure anybody waiting knows we are done
3033 * updating everything for this ordered extent.
3035 btrfs_remove_ordered_extent(inode
, ordered_extent
);
3037 /* for snapshot-aware defrag */
3040 free_sa_defrag_extent(new);
3041 atomic_dec(&root
->fs_info
->defrag_running
);
3043 relink_file_extents(new);
3048 btrfs_put_ordered_extent(ordered_extent
);
3049 /* once for the tree */
3050 btrfs_put_ordered_extent(ordered_extent
);
3055 static void finish_ordered_fn(struct btrfs_work
*work
)
3057 struct btrfs_ordered_extent
*ordered_extent
;
3058 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
3059 btrfs_finish_ordered_io(ordered_extent
);
3062 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
3063 struct extent_state
*state
, int uptodate
)
3065 struct inode
*inode
= page
->mapping
->host
;
3066 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3067 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
3068 struct btrfs_workqueue
*wq
;
3069 btrfs_work_func_t func
;
3071 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
3073 ClearPagePrivate2(page
);
3074 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
3075 end
- start
+ 1, uptodate
))
3078 if (btrfs_is_free_space_inode(inode
)) {
3079 wq
= root
->fs_info
->endio_freespace_worker
;
3080 func
= btrfs_freespace_write_helper
;
3082 wq
= root
->fs_info
->endio_write_workers
;
3083 func
= btrfs_endio_write_helper
;
3086 btrfs_init_work(&ordered_extent
->work
, func
, finish_ordered_fn
, NULL
,
3088 btrfs_queue_work(wq
, &ordered_extent
->work
);
3093 static int __readpage_endio_check(struct inode
*inode
,
3094 struct btrfs_io_bio
*io_bio
,
3095 int icsum
, struct page
*page
,
3096 int pgoff
, u64 start
, size_t len
)
3102 csum_expected
= *(((u32
*)io_bio
->csum
) + icsum
);
3104 kaddr
= kmap_atomic(page
);
3105 csum
= btrfs_csum_data(kaddr
+ pgoff
, csum
, len
);
3106 btrfs_csum_final(csum
, (char *)&csum
);
3107 if (csum
!= csum_expected
)
3110 kunmap_atomic(kaddr
);
3113 btrfs_warn_rl(BTRFS_I(inode
)->root
->fs_info
,
3114 "csum failed ino %llu off %llu csum %u expected csum %u",
3115 btrfs_ino(inode
), start
, csum
, csum_expected
);
3116 memset(kaddr
+ pgoff
, 1, len
);
3117 flush_dcache_page(page
);
3118 kunmap_atomic(kaddr
);
3119 if (csum_expected
== 0)
3125 * when reads are done, we need to check csums to verify the data is correct
3126 * if there's a match, we allow the bio to finish. If not, the code in
3127 * extent_io.c will try to find good copies for us.
3129 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
3130 u64 phy_offset
, struct page
*page
,
3131 u64 start
, u64 end
, int mirror
)
3133 size_t offset
= start
- page_offset(page
);
3134 struct inode
*inode
= page
->mapping
->host
;
3135 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3136 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3138 if (PageChecked(page
)) {
3139 ClearPageChecked(page
);
3143 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
3146 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
3147 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
3148 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
);
3152 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
3153 return __readpage_endio_check(inode
, io_bio
, phy_offset
, page
, offset
,
3154 start
, (size_t)(end
- start
+ 1));
3157 void btrfs_add_delayed_iput(struct inode
*inode
)
3159 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
3160 struct btrfs_inode
*binode
= BTRFS_I(inode
);
3162 if (atomic_add_unless(&inode
->i_count
, -1, 1))
3165 spin_lock(&fs_info
->delayed_iput_lock
);
3166 if (binode
->delayed_iput_count
== 0) {
3167 ASSERT(list_empty(&binode
->delayed_iput
));
3168 list_add_tail(&binode
->delayed_iput
, &fs_info
->delayed_iputs
);
3170 binode
->delayed_iput_count
++;
3172 spin_unlock(&fs_info
->delayed_iput_lock
);
3175 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
3177 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3179 spin_lock(&fs_info
->delayed_iput_lock
);
3180 while (!list_empty(&fs_info
->delayed_iputs
)) {
3181 struct btrfs_inode
*inode
;
3183 inode
= list_first_entry(&fs_info
->delayed_iputs
,
3184 struct btrfs_inode
, delayed_iput
);
3185 if (inode
->delayed_iput_count
) {
3186 inode
->delayed_iput_count
--;
3187 list_move_tail(&inode
->delayed_iput
,
3188 &fs_info
->delayed_iputs
);
3190 list_del_init(&inode
->delayed_iput
);
3192 spin_unlock(&fs_info
->delayed_iput_lock
);
3193 iput(&inode
->vfs_inode
);
3194 spin_lock(&fs_info
->delayed_iput_lock
);
3196 spin_unlock(&fs_info
->delayed_iput_lock
);
3200 * This is called in transaction commit time. If there are no orphan
3201 * files in the subvolume, it removes orphan item and frees block_rsv
3204 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
3205 struct btrfs_root
*root
)
3207 struct btrfs_block_rsv
*block_rsv
;
3210 if (atomic_read(&root
->orphan_inodes
) ||
3211 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
3214 spin_lock(&root
->orphan_lock
);
3215 if (atomic_read(&root
->orphan_inodes
)) {
3216 spin_unlock(&root
->orphan_lock
);
3220 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
3221 spin_unlock(&root
->orphan_lock
);
3225 block_rsv
= root
->orphan_block_rsv
;
3226 root
->orphan_block_rsv
= NULL
;
3227 spin_unlock(&root
->orphan_lock
);
3229 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
) &&
3230 btrfs_root_refs(&root
->root_item
) > 0) {
3231 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
3232 root
->root_key
.objectid
);
3234 btrfs_abort_transaction(trans
, ret
);
3236 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
,
3241 WARN_ON(block_rsv
->size
> 0);
3242 btrfs_free_block_rsv(root
, block_rsv
);
3247 * This creates an orphan entry for the given inode in case something goes
3248 * wrong in the middle of an unlink/truncate.
3250 * NOTE: caller of this function should reserve 5 units of metadata for
3253 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
3255 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3256 struct btrfs_block_rsv
*block_rsv
= NULL
;
3261 if (!root
->orphan_block_rsv
) {
3262 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
3267 spin_lock(&root
->orphan_lock
);
3268 if (!root
->orphan_block_rsv
) {
3269 root
->orphan_block_rsv
= block_rsv
;
3270 } else if (block_rsv
) {
3271 btrfs_free_block_rsv(root
, block_rsv
);
3275 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3276 &BTRFS_I(inode
)->runtime_flags
)) {
3279 * For proper ENOSPC handling, we should do orphan
3280 * cleanup when mounting. But this introduces backward
3281 * compatibility issue.
3283 if (!xchg(&root
->orphan_item_inserted
, 1))
3289 atomic_inc(&root
->orphan_inodes
);
3292 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3293 &BTRFS_I(inode
)->runtime_flags
))
3295 spin_unlock(&root
->orphan_lock
);
3297 /* grab metadata reservation from transaction handle */
3299 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
3302 atomic_dec(&root
->orphan_inodes
);
3303 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3304 &BTRFS_I(inode
)->runtime_flags
);
3306 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3307 &BTRFS_I(inode
)->runtime_flags
);
3312 /* insert an orphan item to track this unlinked/truncated file */
3314 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
3316 atomic_dec(&root
->orphan_inodes
);
3318 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3319 &BTRFS_I(inode
)->runtime_flags
);
3320 btrfs_orphan_release_metadata(inode
);
3322 if (ret
!= -EEXIST
) {
3323 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3324 &BTRFS_I(inode
)->runtime_flags
);
3325 btrfs_abort_transaction(trans
, ret
);
3332 /* insert an orphan item to track subvolume contains orphan files */
3334 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
3335 root
->root_key
.objectid
);
3336 if (ret
&& ret
!= -EEXIST
) {
3337 btrfs_abort_transaction(trans
, ret
);
3345 * We have done the truncate/delete so we can go ahead and remove the orphan
3346 * item for this particular inode.
3348 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3349 struct inode
*inode
)
3351 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3352 int delete_item
= 0;
3353 int release_rsv
= 0;
3356 spin_lock(&root
->orphan_lock
);
3357 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3358 &BTRFS_I(inode
)->runtime_flags
))
3361 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3362 &BTRFS_I(inode
)->runtime_flags
))
3364 spin_unlock(&root
->orphan_lock
);
3367 atomic_dec(&root
->orphan_inodes
);
3369 ret
= btrfs_del_orphan_item(trans
, root
,
3374 btrfs_orphan_release_metadata(inode
);
3380 * this cleans up any orphans that may be left on the list from the last use
3383 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3385 struct btrfs_path
*path
;
3386 struct extent_buffer
*leaf
;
3387 struct btrfs_key key
, found_key
;
3388 struct btrfs_trans_handle
*trans
;
3389 struct inode
*inode
;
3390 u64 last_objectid
= 0;
3391 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3393 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3396 path
= btrfs_alloc_path();
3401 path
->reada
= READA_BACK
;
3403 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3404 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
3405 key
.offset
= (u64
)-1;
3408 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3413 * if ret == 0 means we found what we were searching for, which
3414 * is weird, but possible, so only screw with path if we didn't
3415 * find the key and see if we have stuff that matches
3419 if (path
->slots
[0] == 0)
3424 /* pull out the item */
3425 leaf
= path
->nodes
[0];
3426 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3428 /* make sure the item matches what we want */
3429 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3431 if (found_key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
3434 /* release the path since we're done with it */
3435 btrfs_release_path(path
);
3438 * this is where we are basically btrfs_lookup, without the
3439 * crossing root thing. we store the inode number in the
3440 * offset of the orphan item.
3443 if (found_key
.offset
== last_objectid
) {
3444 btrfs_err(root
->fs_info
,
3445 "Error removing orphan entry, stopping orphan cleanup");
3450 last_objectid
= found_key
.offset
;
3452 found_key
.objectid
= found_key
.offset
;
3453 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3454 found_key
.offset
= 0;
3455 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3456 ret
= PTR_ERR_OR_ZERO(inode
);
3457 if (ret
&& ret
!= -ENOENT
)
3460 if (ret
== -ENOENT
&& root
== root
->fs_info
->tree_root
) {
3461 struct btrfs_root
*dead_root
;
3462 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3463 int is_dead_root
= 0;
3466 * this is an orphan in the tree root. Currently these
3467 * could come from 2 sources:
3468 * a) a snapshot deletion in progress
3469 * b) a free space cache inode
3470 * We need to distinguish those two, as the snapshot
3471 * orphan must not get deleted.
3472 * find_dead_roots already ran before us, so if this
3473 * is a snapshot deletion, we should find the root
3474 * in the dead_roots list
3476 spin_lock(&fs_info
->trans_lock
);
3477 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3479 if (dead_root
->root_key
.objectid
==
3480 found_key
.objectid
) {
3485 spin_unlock(&fs_info
->trans_lock
);
3487 /* prevent this orphan from being found again */
3488 key
.offset
= found_key
.objectid
- 1;
3493 * Inode is already gone but the orphan item is still there,
3494 * kill the orphan item.
3496 if (ret
== -ENOENT
) {
3497 trans
= btrfs_start_transaction(root
, 1);
3498 if (IS_ERR(trans
)) {
3499 ret
= PTR_ERR(trans
);
3502 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3503 found_key
.objectid
);
3504 ret
= btrfs_del_orphan_item(trans
, root
,
3505 found_key
.objectid
);
3506 btrfs_end_transaction(trans
, root
);
3513 * add this inode to the orphan list so btrfs_orphan_del does
3514 * the proper thing when we hit it
3516 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3517 &BTRFS_I(inode
)->runtime_flags
);
3518 atomic_inc(&root
->orphan_inodes
);
3520 /* if we have links, this was a truncate, lets do that */
3521 if (inode
->i_nlink
) {
3522 if (WARN_ON(!S_ISREG(inode
->i_mode
))) {
3528 /* 1 for the orphan item deletion. */
3529 trans
= btrfs_start_transaction(root
, 1);
3530 if (IS_ERR(trans
)) {
3532 ret
= PTR_ERR(trans
);
3535 ret
= btrfs_orphan_add(trans
, inode
);
3536 btrfs_end_transaction(trans
, root
);
3542 ret
= btrfs_truncate(inode
);
3544 btrfs_orphan_del(NULL
, inode
);
3549 /* this will do delete_inode and everything for us */
3554 /* release the path since we're done with it */
3555 btrfs_release_path(path
);
3557 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3559 if (root
->orphan_block_rsv
)
3560 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3563 if (root
->orphan_block_rsv
||
3564 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
)) {
3565 trans
= btrfs_join_transaction(root
);
3567 btrfs_end_transaction(trans
, root
);
3571 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3573 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3577 btrfs_err(root
->fs_info
,
3578 "could not do orphan cleanup %d", ret
);
3579 btrfs_free_path(path
);
3584 * very simple check to peek ahead in the leaf looking for xattrs. If we
3585 * don't find any xattrs, we know there can't be any acls.
3587 * slot is the slot the inode is in, objectid is the objectid of the inode
3589 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3590 int slot
, u64 objectid
,
3591 int *first_xattr_slot
)
3593 u32 nritems
= btrfs_header_nritems(leaf
);
3594 struct btrfs_key found_key
;
3595 static u64 xattr_access
= 0;
3596 static u64 xattr_default
= 0;
3599 if (!xattr_access
) {
3600 xattr_access
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS
,
3601 strlen(XATTR_NAME_POSIX_ACL_ACCESS
));
3602 xattr_default
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT
,
3603 strlen(XATTR_NAME_POSIX_ACL_DEFAULT
));
3607 *first_xattr_slot
= -1;
3608 while (slot
< nritems
) {
3609 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3611 /* we found a different objectid, there must not be acls */
3612 if (found_key
.objectid
!= objectid
)
3615 /* we found an xattr, assume we've got an acl */
3616 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3617 if (*first_xattr_slot
== -1)
3618 *first_xattr_slot
= slot
;
3619 if (found_key
.offset
== xattr_access
||
3620 found_key
.offset
== xattr_default
)
3625 * we found a key greater than an xattr key, there can't
3626 * be any acls later on
3628 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3635 * it goes inode, inode backrefs, xattrs, extents,
3636 * so if there are a ton of hard links to an inode there can
3637 * be a lot of backrefs. Don't waste time searching too hard,
3638 * this is just an optimization
3643 /* we hit the end of the leaf before we found an xattr or
3644 * something larger than an xattr. We have to assume the inode
3647 if (*first_xattr_slot
== -1)
3648 *first_xattr_slot
= slot
;
3653 * read an inode from the btree into the in-memory inode
3655 static int btrfs_read_locked_inode(struct inode
*inode
)
3657 struct btrfs_path
*path
;
3658 struct extent_buffer
*leaf
;
3659 struct btrfs_inode_item
*inode_item
;
3660 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3661 struct btrfs_key location
;
3666 bool filled
= false;
3667 int first_xattr_slot
;
3669 ret
= btrfs_fill_inode(inode
, &rdev
);
3673 path
= btrfs_alloc_path();
3679 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3681 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3688 leaf
= path
->nodes
[0];
3693 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3694 struct btrfs_inode_item
);
3695 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3696 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3697 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3698 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3699 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3701 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->atime
);
3702 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->atime
);
3704 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->mtime
);
3705 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->mtime
);
3707 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->ctime
);
3708 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->ctime
);
3710 BTRFS_I(inode
)->i_otime
.tv_sec
=
3711 btrfs_timespec_sec(leaf
, &inode_item
->otime
);
3712 BTRFS_I(inode
)->i_otime
.tv_nsec
=
3713 btrfs_timespec_nsec(leaf
, &inode_item
->otime
);
3715 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3716 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3717 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3719 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3720 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3722 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3724 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3725 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3729 * If we were modified in the current generation and evicted from memory
3730 * and then re-read we need to do a full sync since we don't have any
3731 * idea about which extents were modified before we were evicted from
3734 * This is required for both inode re-read from disk and delayed inode
3735 * in delayed_nodes_tree.
3737 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3738 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3739 &BTRFS_I(inode
)->runtime_flags
);
3742 * We don't persist the id of the transaction where an unlink operation
3743 * against the inode was last made. So here we assume the inode might
3744 * have been evicted, and therefore the exact value of last_unlink_trans
3745 * lost, and set it to last_trans to avoid metadata inconsistencies
3746 * between the inode and its parent if the inode is fsync'ed and the log
3747 * replayed. For example, in the scenario:
3750 * ln mydir/foo mydir/bar
3753 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3754 * xfs_io -c fsync mydir/foo
3756 * mount fs, triggers fsync log replay
3758 * We must make sure that when we fsync our inode foo we also log its
3759 * parent inode, otherwise after log replay the parent still has the
3760 * dentry with the "bar" name but our inode foo has a link count of 1
3761 * and doesn't have an inode ref with the name "bar" anymore.
3763 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3764 * but it guarantees correctness at the expense of occasional full
3765 * transaction commits on fsync if our inode is a directory, or if our
3766 * inode is not a directory, logging its parent unnecessarily.
3768 BTRFS_I(inode
)->last_unlink_trans
= BTRFS_I(inode
)->last_trans
;
3771 if (inode
->i_nlink
!= 1 ||
3772 path
->slots
[0] >= btrfs_header_nritems(leaf
))
3775 btrfs_item_key_to_cpu(leaf
, &location
, path
->slots
[0]);
3776 if (location
.objectid
!= btrfs_ino(inode
))
3779 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3780 if (location
.type
== BTRFS_INODE_REF_KEY
) {
3781 struct btrfs_inode_ref
*ref
;
3783 ref
= (struct btrfs_inode_ref
*)ptr
;
3784 BTRFS_I(inode
)->dir_index
= btrfs_inode_ref_index(leaf
, ref
);
3785 } else if (location
.type
== BTRFS_INODE_EXTREF_KEY
) {
3786 struct btrfs_inode_extref
*extref
;
3788 extref
= (struct btrfs_inode_extref
*)ptr
;
3789 BTRFS_I(inode
)->dir_index
= btrfs_inode_extref_index(leaf
,
3794 * try to precache a NULL acl entry for files that don't have
3795 * any xattrs or acls
3797 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3798 btrfs_ino(inode
), &first_xattr_slot
);
3799 if (first_xattr_slot
!= -1) {
3800 path
->slots
[0] = first_xattr_slot
;
3801 ret
= btrfs_load_inode_props(inode
, path
);
3803 btrfs_err(root
->fs_info
,
3804 "error loading props for ino %llu (root %llu): %d",
3806 root
->root_key
.objectid
, ret
);
3808 btrfs_free_path(path
);
3811 cache_no_acl(inode
);
3813 switch (inode
->i_mode
& S_IFMT
) {
3815 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3816 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3817 inode
->i_fop
= &btrfs_file_operations
;
3818 inode
->i_op
= &btrfs_file_inode_operations
;
3821 inode
->i_fop
= &btrfs_dir_file_operations
;
3822 if (root
== root
->fs_info
->tree_root
)
3823 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3825 inode
->i_op
= &btrfs_dir_inode_operations
;
3828 inode
->i_op
= &btrfs_symlink_inode_operations
;
3829 inode_nohighmem(inode
);
3830 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3833 inode
->i_op
= &btrfs_special_inode_operations
;
3834 init_special_inode(inode
, inode
->i_mode
, rdev
);
3838 btrfs_update_iflags(inode
);
3842 btrfs_free_path(path
);
3843 make_bad_inode(inode
);
3848 * given a leaf and an inode, copy the inode fields into the leaf
3850 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3851 struct extent_buffer
*leaf
,
3852 struct btrfs_inode_item
*item
,
3853 struct inode
*inode
)
3855 struct btrfs_map_token token
;
3857 btrfs_init_map_token(&token
);
3859 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3860 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3861 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3863 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3864 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3866 btrfs_set_token_timespec_sec(leaf
, &item
->atime
,
3867 inode
->i_atime
.tv_sec
, &token
);
3868 btrfs_set_token_timespec_nsec(leaf
, &item
->atime
,
3869 inode
->i_atime
.tv_nsec
, &token
);
3871 btrfs_set_token_timespec_sec(leaf
, &item
->mtime
,
3872 inode
->i_mtime
.tv_sec
, &token
);
3873 btrfs_set_token_timespec_nsec(leaf
, &item
->mtime
,
3874 inode
->i_mtime
.tv_nsec
, &token
);
3876 btrfs_set_token_timespec_sec(leaf
, &item
->ctime
,
3877 inode
->i_ctime
.tv_sec
, &token
);
3878 btrfs_set_token_timespec_nsec(leaf
, &item
->ctime
,
3879 inode
->i_ctime
.tv_nsec
, &token
);
3881 btrfs_set_token_timespec_sec(leaf
, &item
->otime
,
3882 BTRFS_I(inode
)->i_otime
.tv_sec
, &token
);
3883 btrfs_set_token_timespec_nsec(leaf
, &item
->otime
,
3884 BTRFS_I(inode
)->i_otime
.tv_nsec
, &token
);
3886 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3888 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3890 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3891 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3892 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3893 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3894 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3898 * copy everything in the in-memory inode into the btree.
3900 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3901 struct btrfs_root
*root
, struct inode
*inode
)
3903 struct btrfs_inode_item
*inode_item
;
3904 struct btrfs_path
*path
;
3905 struct extent_buffer
*leaf
;
3908 path
= btrfs_alloc_path();
3912 path
->leave_spinning
= 1;
3913 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3921 leaf
= path
->nodes
[0];
3922 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3923 struct btrfs_inode_item
);
3925 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3926 btrfs_mark_buffer_dirty(leaf
);
3927 btrfs_set_inode_last_trans(trans
, inode
);
3930 btrfs_free_path(path
);
3935 * copy everything in the in-memory inode into the btree.
3937 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3938 struct btrfs_root
*root
, struct inode
*inode
)
3943 * If the inode is a free space inode, we can deadlock during commit
3944 * if we put it into the delayed code.
3946 * The data relocation inode should also be directly updated
3949 if (!btrfs_is_free_space_inode(inode
)
3950 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
3951 && !test_bit(BTRFS_FS_LOG_RECOVERING
, &root
->fs_info
->flags
)) {
3952 btrfs_update_root_times(trans
, root
);
3954 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3956 btrfs_set_inode_last_trans(trans
, inode
);
3960 return btrfs_update_inode_item(trans
, root
, inode
);
3963 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3964 struct btrfs_root
*root
,
3965 struct inode
*inode
)
3969 ret
= btrfs_update_inode(trans
, root
, inode
);
3971 return btrfs_update_inode_item(trans
, root
, inode
);
3976 * unlink helper that gets used here in inode.c and in the tree logging
3977 * recovery code. It remove a link in a directory with a given name, and
3978 * also drops the back refs in the inode to the directory
3980 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3981 struct btrfs_root
*root
,
3982 struct inode
*dir
, struct inode
*inode
,
3983 const char *name
, int name_len
)
3985 struct btrfs_path
*path
;
3987 struct extent_buffer
*leaf
;
3988 struct btrfs_dir_item
*di
;
3989 struct btrfs_key key
;
3991 u64 ino
= btrfs_ino(inode
);
3992 u64 dir_ino
= btrfs_ino(dir
);
3994 path
= btrfs_alloc_path();
4000 path
->leave_spinning
= 1;
4001 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
4002 name
, name_len
, -1);
4011 leaf
= path
->nodes
[0];
4012 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
4013 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
4016 btrfs_release_path(path
);
4019 * If we don't have dir index, we have to get it by looking up
4020 * the inode ref, since we get the inode ref, remove it directly,
4021 * it is unnecessary to do delayed deletion.
4023 * But if we have dir index, needn't search inode ref to get it.
4024 * Since the inode ref is close to the inode item, it is better
4025 * that we delay to delete it, and just do this deletion when
4026 * we update the inode item.
4028 if (BTRFS_I(inode
)->dir_index
) {
4029 ret
= btrfs_delayed_delete_inode_ref(inode
);
4031 index
= BTRFS_I(inode
)->dir_index
;
4036 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
4039 btrfs_info(root
->fs_info
,
4040 "failed to delete reference to %.*s, inode %llu parent %llu",
4041 name_len
, name
, ino
, dir_ino
);
4042 btrfs_abort_transaction(trans
, ret
);
4046 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
4048 btrfs_abort_transaction(trans
, ret
);
4052 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
4054 if (ret
!= 0 && ret
!= -ENOENT
) {
4055 btrfs_abort_transaction(trans
, ret
);
4059 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
4064 btrfs_abort_transaction(trans
, ret
);
4066 btrfs_free_path(path
);
4070 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
4071 inode_inc_iversion(inode
);
4072 inode_inc_iversion(dir
);
4073 inode
->i_ctime
= dir
->i_mtime
=
4074 dir
->i_ctime
= current_time(inode
);
4075 ret
= btrfs_update_inode(trans
, root
, dir
);
4080 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
4081 struct btrfs_root
*root
,
4082 struct inode
*dir
, struct inode
*inode
,
4083 const char *name
, int name_len
)
4086 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
4089 ret
= btrfs_update_inode(trans
, root
, inode
);
4095 * helper to start transaction for unlink and rmdir.
4097 * unlink and rmdir are special in btrfs, they do not always free space, so
4098 * if we cannot make our reservations the normal way try and see if there is
4099 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4100 * allow the unlink to occur.
4102 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
4104 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4107 * 1 for the possible orphan item
4108 * 1 for the dir item
4109 * 1 for the dir index
4110 * 1 for the inode ref
4113 return btrfs_start_transaction_fallback_global_rsv(root
, 5, 5);
4116 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
4118 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4119 struct btrfs_trans_handle
*trans
;
4120 struct inode
*inode
= d_inode(dentry
);
4123 trans
= __unlink_start_trans(dir
);
4125 return PTR_ERR(trans
);
4127 btrfs_record_unlink_dir(trans
, dir
, d_inode(dentry
), 0);
4129 ret
= btrfs_unlink_inode(trans
, root
, dir
, d_inode(dentry
),
4130 dentry
->d_name
.name
, dentry
->d_name
.len
);
4134 if (inode
->i_nlink
== 0) {
4135 ret
= btrfs_orphan_add(trans
, inode
);
4141 btrfs_end_transaction(trans
, root
);
4142 btrfs_btree_balance_dirty(root
);
4146 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
4147 struct btrfs_root
*root
,
4148 struct inode
*dir
, u64 objectid
,
4149 const char *name
, int name_len
)
4151 struct btrfs_path
*path
;
4152 struct extent_buffer
*leaf
;
4153 struct btrfs_dir_item
*di
;
4154 struct btrfs_key key
;
4157 u64 dir_ino
= btrfs_ino(dir
);
4159 path
= btrfs_alloc_path();
4163 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
4164 name
, name_len
, -1);
4165 if (IS_ERR_OR_NULL(di
)) {
4173 leaf
= path
->nodes
[0];
4174 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
4175 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
4176 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
4178 btrfs_abort_transaction(trans
, ret
);
4181 btrfs_release_path(path
);
4183 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
4184 objectid
, root
->root_key
.objectid
,
4185 dir_ino
, &index
, name
, name_len
);
4187 if (ret
!= -ENOENT
) {
4188 btrfs_abort_transaction(trans
, ret
);
4191 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
4193 if (IS_ERR_OR_NULL(di
)) {
4198 btrfs_abort_transaction(trans
, ret
);
4202 leaf
= path
->nodes
[0];
4203 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4204 btrfs_release_path(path
);
4207 btrfs_release_path(path
);
4209 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
4211 btrfs_abort_transaction(trans
, ret
);
4215 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
4216 inode_inc_iversion(dir
);
4217 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
4218 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
4220 btrfs_abort_transaction(trans
, ret
);
4222 btrfs_free_path(path
);
4226 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
4228 struct inode
*inode
= d_inode(dentry
);
4230 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4231 struct btrfs_trans_handle
*trans
;
4232 u64 last_unlink_trans
;
4234 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
4236 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
4239 trans
= __unlink_start_trans(dir
);
4241 return PTR_ERR(trans
);
4243 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
4244 err
= btrfs_unlink_subvol(trans
, root
, dir
,
4245 BTRFS_I(inode
)->location
.objectid
,
4246 dentry
->d_name
.name
,
4247 dentry
->d_name
.len
);
4251 err
= btrfs_orphan_add(trans
, inode
);
4255 last_unlink_trans
= BTRFS_I(inode
)->last_unlink_trans
;
4257 /* now the directory is empty */
4258 err
= btrfs_unlink_inode(trans
, root
, dir
, d_inode(dentry
),
4259 dentry
->d_name
.name
, dentry
->d_name
.len
);
4261 btrfs_i_size_write(inode
, 0);
4263 * Propagate the last_unlink_trans value of the deleted dir to
4264 * its parent directory. This is to prevent an unrecoverable
4265 * log tree in the case we do something like this:
4267 * 2) create snapshot under dir foo
4268 * 3) delete the snapshot
4271 * 6) fsync foo or some file inside foo
4273 if (last_unlink_trans
>= trans
->transid
)
4274 BTRFS_I(dir
)->last_unlink_trans
= last_unlink_trans
;
4277 btrfs_end_transaction(trans
, root
);
4278 btrfs_btree_balance_dirty(root
);
4283 static int truncate_space_check(struct btrfs_trans_handle
*trans
,
4284 struct btrfs_root
*root
,
4290 * This is only used to apply pressure to the enospc system, we don't
4291 * intend to use this reservation at all.
4293 bytes_deleted
= btrfs_csum_bytes_to_leaves(root
, bytes_deleted
);
4294 bytes_deleted
*= root
->nodesize
;
4295 ret
= btrfs_block_rsv_add(root
, &root
->fs_info
->trans_block_rsv
,
4296 bytes_deleted
, BTRFS_RESERVE_NO_FLUSH
);
4298 trace_btrfs_space_reservation(root
->fs_info
, "transaction",
4301 trans
->bytes_reserved
+= bytes_deleted
;
4307 static int truncate_inline_extent(struct inode
*inode
,
4308 struct btrfs_path
*path
,
4309 struct btrfs_key
*found_key
,
4313 struct extent_buffer
*leaf
= path
->nodes
[0];
4314 int slot
= path
->slots
[0];
4315 struct btrfs_file_extent_item
*fi
;
4316 u32 size
= (u32
)(new_size
- found_key
->offset
);
4317 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4319 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
4321 if (btrfs_file_extent_compression(leaf
, fi
) != BTRFS_COMPRESS_NONE
) {
4322 loff_t offset
= new_size
;
4323 loff_t page_end
= ALIGN(offset
, PAGE_SIZE
);
4326 * Zero out the remaining of the last page of our inline extent,
4327 * instead of directly truncating our inline extent here - that
4328 * would be much more complex (decompressing all the data, then
4329 * compressing the truncated data, which might be bigger than
4330 * the size of the inline extent, resize the extent, etc).
4331 * We release the path because to get the page we might need to
4332 * read the extent item from disk (data not in the page cache).
4334 btrfs_release_path(path
);
4335 return btrfs_truncate_block(inode
, offset
, page_end
- offset
,
4339 btrfs_set_file_extent_ram_bytes(leaf
, fi
, size
);
4340 size
= btrfs_file_extent_calc_inline_size(size
);
4341 btrfs_truncate_item(root
, path
, size
, 1);
4343 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4344 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4350 * this can truncate away extent items, csum items and directory items.
4351 * It starts at a high offset and removes keys until it can't find
4352 * any higher than new_size
4354 * csum items that cross the new i_size are truncated to the new size
4357 * min_type is the minimum key type to truncate down to. If set to 0, this
4358 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4360 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4361 struct btrfs_root
*root
,
4362 struct inode
*inode
,
4363 u64 new_size
, u32 min_type
)
4365 struct btrfs_path
*path
;
4366 struct extent_buffer
*leaf
;
4367 struct btrfs_file_extent_item
*fi
;
4368 struct btrfs_key key
;
4369 struct btrfs_key found_key
;
4370 u64 extent_start
= 0;
4371 u64 extent_num_bytes
= 0;
4372 u64 extent_offset
= 0;
4374 u64 last_size
= new_size
;
4375 u32 found_type
= (u8
)-1;
4378 int pending_del_nr
= 0;
4379 int pending_del_slot
= 0;
4380 int extent_type
= -1;
4383 u64 ino
= btrfs_ino(inode
);
4384 u64 bytes_deleted
= 0;
4386 bool should_throttle
= 0;
4387 bool should_end
= 0;
4389 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4392 * for non-free space inodes and ref cows, we want to back off from
4395 if (!btrfs_is_free_space_inode(inode
) &&
4396 test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4399 path
= btrfs_alloc_path();
4402 path
->reada
= READA_BACK
;
4405 * We want to drop from the next block forward in case this new size is
4406 * not block aligned since we will be keeping the last block of the
4407 * extent just the way it is.
4409 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4410 root
== root
->fs_info
->tree_root
)
4411 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
4412 root
->sectorsize
), (u64
)-1, 0);
4415 * This function is also used to drop the items in the log tree before
4416 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4417 * it is used to drop the loged items. So we shouldn't kill the delayed
4420 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4421 btrfs_kill_delayed_inode_items(inode
);
4424 key
.offset
= (u64
)-1;
4429 * with a 16K leaf size and 128MB extents, you can actually queue
4430 * up a huge file in a single leaf. Most of the time that
4431 * bytes_deleted is > 0, it will be huge by the time we get here
4433 if (be_nice
&& bytes_deleted
> SZ_32M
) {
4434 if (btrfs_should_end_transaction(trans
, root
)) {
4441 path
->leave_spinning
= 1;
4442 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4449 /* there are no items in the tree for us to truncate, we're
4452 if (path
->slots
[0] == 0)
4459 leaf
= path
->nodes
[0];
4460 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4461 found_type
= found_key
.type
;
4463 if (found_key
.objectid
!= ino
)
4466 if (found_type
< min_type
)
4469 item_end
= found_key
.offset
;
4470 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4471 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4472 struct btrfs_file_extent_item
);
4473 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4474 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4476 btrfs_file_extent_num_bytes(leaf
, fi
);
4477 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4478 item_end
+= btrfs_file_extent_inline_len(leaf
,
4479 path
->slots
[0], fi
);
4483 if (found_type
> min_type
) {
4486 if (item_end
< new_size
)
4488 if (found_key
.offset
>= new_size
)
4494 /* FIXME, shrink the extent if the ref count is only 1 */
4495 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4499 last_size
= found_key
.offset
;
4501 last_size
= new_size
;
4503 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4505 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4507 u64 orig_num_bytes
=
4508 btrfs_file_extent_num_bytes(leaf
, fi
);
4509 extent_num_bytes
= ALIGN(new_size
-
4512 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4514 num_dec
= (orig_num_bytes
-
4516 if (test_bit(BTRFS_ROOT_REF_COWS
,
4519 inode_sub_bytes(inode
, num_dec
);
4520 btrfs_mark_buffer_dirty(leaf
);
4523 btrfs_file_extent_disk_num_bytes(leaf
,
4525 extent_offset
= found_key
.offset
-
4526 btrfs_file_extent_offset(leaf
, fi
);
4528 /* FIXME blocksize != 4096 */
4529 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4530 if (extent_start
!= 0) {
4532 if (test_bit(BTRFS_ROOT_REF_COWS
,
4534 inode_sub_bytes(inode
, num_dec
);
4537 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4539 * we can't truncate inline items that have had
4543 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4544 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4547 * Need to release path in order to truncate a
4548 * compressed extent. So delete any accumulated
4549 * extent items so far.
4551 if (btrfs_file_extent_compression(leaf
, fi
) !=
4552 BTRFS_COMPRESS_NONE
&& pending_del_nr
) {
4553 err
= btrfs_del_items(trans
, root
, path
,
4557 btrfs_abort_transaction(trans
,
4564 err
= truncate_inline_extent(inode
, path
,
4569 btrfs_abort_transaction(trans
, err
);
4572 } else if (test_bit(BTRFS_ROOT_REF_COWS
,
4574 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4579 if (!pending_del_nr
) {
4580 /* no pending yet, add ourselves */
4581 pending_del_slot
= path
->slots
[0];
4583 } else if (pending_del_nr
&&
4584 path
->slots
[0] + 1 == pending_del_slot
) {
4585 /* hop on the pending chunk */
4587 pending_del_slot
= path
->slots
[0];
4594 should_throttle
= 0;
4597 (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4598 root
== root
->fs_info
->tree_root
)) {
4599 btrfs_set_path_blocking(path
);
4600 bytes_deleted
+= extent_num_bytes
;
4601 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4602 extent_num_bytes
, 0,
4603 btrfs_header_owner(leaf
),
4604 ino
, extent_offset
);
4606 if (btrfs_should_throttle_delayed_refs(trans
, root
))
4607 btrfs_async_run_delayed_refs(root
,
4608 trans
->delayed_ref_updates
* 2,
4611 if (truncate_space_check(trans
, root
,
4612 extent_num_bytes
)) {
4615 if (btrfs_should_throttle_delayed_refs(trans
,
4617 should_throttle
= 1;
4622 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4625 if (path
->slots
[0] == 0 ||
4626 path
->slots
[0] != pending_del_slot
||
4627 should_throttle
|| should_end
) {
4628 if (pending_del_nr
) {
4629 ret
= btrfs_del_items(trans
, root
, path
,
4633 btrfs_abort_transaction(trans
, ret
);
4638 btrfs_release_path(path
);
4639 if (should_throttle
) {
4640 unsigned long updates
= trans
->delayed_ref_updates
;
4642 trans
->delayed_ref_updates
= 0;
4643 ret
= btrfs_run_delayed_refs(trans
, root
, updates
* 2);
4649 * if we failed to refill our space rsv, bail out
4650 * and let the transaction restart
4662 if (pending_del_nr
) {
4663 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4666 btrfs_abort_transaction(trans
, ret
);
4669 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
4670 btrfs_ordered_update_i_size(inode
, last_size
, NULL
);
4672 btrfs_free_path(path
);
4674 if (be_nice
&& bytes_deleted
> SZ_32M
) {
4675 unsigned long updates
= trans
->delayed_ref_updates
;
4677 trans
->delayed_ref_updates
= 0;
4678 ret
= btrfs_run_delayed_refs(trans
, root
, updates
* 2);
4687 * btrfs_truncate_block - read, zero a chunk and write a block
4688 * @inode - inode that we're zeroing
4689 * @from - the offset to start zeroing
4690 * @len - the length to zero, 0 to zero the entire range respective to the
4692 * @front - zero up to the offset instead of from the offset on
4694 * This will find the block for the "from" offset and cow the block and zero the
4695 * part we want to zero. This is used with truncate and hole punching.
4697 int btrfs_truncate_block(struct inode
*inode
, loff_t from
, loff_t len
,
4700 struct address_space
*mapping
= inode
->i_mapping
;
4701 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4702 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4703 struct btrfs_ordered_extent
*ordered
;
4704 struct extent_state
*cached_state
= NULL
;
4706 u32 blocksize
= root
->sectorsize
;
4707 pgoff_t index
= from
>> PAGE_SHIFT
;
4708 unsigned offset
= from
& (blocksize
- 1);
4710 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4715 if ((offset
& (blocksize
- 1)) == 0 &&
4716 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4719 ret
= btrfs_delalloc_reserve_space(inode
,
4720 round_down(from
, blocksize
), blocksize
);
4725 page
= find_or_create_page(mapping
, index
, mask
);
4727 btrfs_delalloc_release_space(inode
,
4728 round_down(from
, blocksize
),
4734 block_start
= round_down(from
, blocksize
);
4735 block_end
= block_start
+ blocksize
- 1;
4737 if (!PageUptodate(page
)) {
4738 ret
= btrfs_readpage(NULL
, page
);
4740 if (page
->mapping
!= mapping
) {
4745 if (!PageUptodate(page
)) {
4750 wait_on_page_writeback(page
);
4752 lock_extent_bits(io_tree
, block_start
, block_end
, &cached_state
);
4753 set_page_extent_mapped(page
);
4755 ordered
= btrfs_lookup_ordered_extent(inode
, block_start
);
4757 unlock_extent_cached(io_tree
, block_start
, block_end
,
4758 &cached_state
, GFP_NOFS
);
4761 btrfs_start_ordered_extent(inode
, ordered
, 1);
4762 btrfs_put_ordered_extent(ordered
);
4766 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, block_start
, block_end
,
4767 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4768 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4769 0, 0, &cached_state
, GFP_NOFS
);
4771 ret
= btrfs_set_extent_delalloc(inode
, block_start
, block_end
,
4774 unlock_extent_cached(io_tree
, block_start
, block_end
,
4775 &cached_state
, GFP_NOFS
);
4779 if (offset
!= blocksize
) {
4781 len
= blocksize
- offset
;
4784 memset(kaddr
+ (block_start
- page_offset(page
)),
4787 memset(kaddr
+ (block_start
- page_offset(page
)) + offset
,
4789 flush_dcache_page(page
);
4792 ClearPageChecked(page
);
4793 set_page_dirty(page
);
4794 unlock_extent_cached(io_tree
, block_start
, block_end
, &cached_state
,
4799 btrfs_delalloc_release_space(inode
, block_start
,
4807 static int maybe_insert_hole(struct btrfs_root
*root
, struct inode
*inode
,
4808 u64 offset
, u64 len
)
4810 struct btrfs_trans_handle
*trans
;
4814 * Still need to make sure the inode looks like it's been updated so
4815 * that any holes get logged if we fsync.
4817 if (btrfs_fs_incompat(root
->fs_info
, NO_HOLES
)) {
4818 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
4819 BTRFS_I(inode
)->last_sub_trans
= root
->log_transid
;
4820 BTRFS_I(inode
)->last_log_commit
= root
->last_log_commit
;
4825 * 1 - for the one we're dropping
4826 * 1 - for the one we're adding
4827 * 1 - for updating the inode.
4829 trans
= btrfs_start_transaction(root
, 3);
4831 return PTR_ERR(trans
);
4833 ret
= btrfs_drop_extents(trans
, root
, inode
, offset
, offset
+ len
, 1);
4835 btrfs_abort_transaction(trans
, ret
);
4836 btrfs_end_transaction(trans
, root
);
4840 ret
= btrfs_insert_file_extent(trans
, root
, btrfs_ino(inode
), offset
,
4841 0, 0, len
, 0, len
, 0, 0, 0);
4843 btrfs_abort_transaction(trans
, ret
);
4845 btrfs_update_inode(trans
, root
, inode
);
4846 btrfs_end_transaction(trans
, root
);
4851 * This function puts in dummy file extents for the area we're creating a hole
4852 * for. So if we are truncating this file to a larger size we need to insert
4853 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4854 * the range between oldsize and size
4856 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4858 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4859 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4860 struct extent_map
*em
= NULL
;
4861 struct extent_state
*cached_state
= NULL
;
4862 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4863 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4864 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4871 * If our size started in the middle of a block we need to zero out the
4872 * rest of the block before we expand the i_size, otherwise we could
4873 * expose stale data.
4875 err
= btrfs_truncate_block(inode
, oldsize
, 0, 0);
4879 if (size
<= hole_start
)
4883 struct btrfs_ordered_extent
*ordered
;
4885 lock_extent_bits(io_tree
, hole_start
, block_end
- 1,
4887 ordered
= btrfs_lookup_ordered_range(inode
, hole_start
,
4888 block_end
- hole_start
);
4891 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4892 &cached_state
, GFP_NOFS
);
4893 btrfs_start_ordered_extent(inode
, ordered
, 1);
4894 btrfs_put_ordered_extent(ordered
);
4897 cur_offset
= hole_start
;
4899 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4900 block_end
- cur_offset
, 0);
4906 last_byte
= min(extent_map_end(em
), block_end
);
4907 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4908 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4909 struct extent_map
*hole_em
;
4910 hole_size
= last_byte
- cur_offset
;
4912 err
= maybe_insert_hole(root
, inode
, cur_offset
,
4916 btrfs_drop_extent_cache(inode
, cur_offset
,
4917 cur_offset
+ hole_size
- 1, 0);
4918 hole_em
= alloc_extent_map();
4920 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4921 &BTRFS_I(inode
)->runtime_flags
);
4924 hole_em
->start
= cur_offset
;
4925 hole_em
->len
= hole_size
;
4926 hole_em
->orig_start
= cur_offset
;
4928 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4929 hole_em
->block_len
= 0;
4930 hole_em
->orig_block_len
= 0;
4931 hole_em
->ram_bytes
= hole_size
;
4932 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4933 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4934 hole_em
->generation
= root
->fs_info
->generation
;
4937 write_lock(&em_tree
->lock
);
4938 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4939 write_unlock(&em_tree
->lock
);
4942 btrfs_drop_extent_cache(inode
, cur_offset
,
4946 free_extent_map(hole_em
);
4949 free_extent_map(em
);
4951 cur_offset
= last_byte
;
4952 if (cur_offset
>= block_end
)
4955 free_extent_map(em
);
4956 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4961 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4963 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4964 struct btrfs_trans_handle
*trans
;
4965 loff_t oldsize
= i_size_read(inode
);
4966 loff_t newsize
= attr
->ia_size
;
4967 int mask
= attr
->ia_valid
;
4971 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4972 * special case where we need to update the times despite not having
4973 * these flags set. For all other operations the VFS set these flags
4974 * explicitly if it wants a timestamp update.
4976 if (newsize
!= oldsize
) {
4977 inode_inc_iversion(inode
);
4978 if (!(mask
& (ATTR_CTIME
| ATTR_MTIME
)))
4979 inode
->i_ctime
= inode
->i_mtime
=
4980 current_time(inode
);
4983 if (newsize
> oldsize
) {
4985 * Don't do an expanding truncate while snapshoting is ongoing.
4986 * This is to ensure the snapshot captures a fully consistent
4987 * state of this file - if the snapshot captures this expanding
4988 * truncation, it must capture all writes that happened before
4991 btrfs_wait_for_snapshot_creation(root
);
4992 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4994 btrfs_end_write_no_snapshoting(root
);
4998 trans
= btrfs_start_transaction(root
, 1);
4999 if (IS_ERR(trans
)) {
5000 btrfs_end_write_no_snapshoting(root
);
5001 return PTR_ERR(trans
);
5004 i_size_write(inode
, newsize
);
5005 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
5006 pagecache_isize_extended(inode
, oldsize
, newsize
);
5007 ret
= btrfs_update_inode(trans
, root
, inode
);
5008 btrfs_end_write_no_snapshoting(root
);
5009 btrfs_end_transaction(trans
, root
);
5013 * We're truncating a file that used to have good data down to
5014 * zero. Make sure it gets into the ordered flush list so that
5015 * any new writes get down to disk quickly.
5018 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
5019 &BTRFS_I(inode
)->runtime_flags
);
5022 * 1 for the orphan item we're going to add
5023 * 1 for the orphan item deletion.
5025 trans
= btrfs_start_transaction(root
, 2);
5027 return PTR_ERR(trans
);
5030 * We need to do this in case we fail at _any_ point during the
5031 * actual truncate. Once we do the truncate_setsize we could
5032 * invalidate pages which forces any outstanding ordered io to
5033 * be instantly completed which will give us extents that need
5034 * to be truncated. If we fail to get an orphan inode down we
5035 * could have left over extents that were never meant to live,
5036 * so we need to guarantee from this point on that everything
5037 * will be consistent.
5039 ret
= btrfs_orphan_add(trans
, inode
);
5040 btrfs_end_transaction(trans
, root
);
5044 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5045 truncate_setsize(inode
, newsize
);
5047 /* Disable nonlocked read DIO to avoid the end less truncate */
5048 btrfs_inode_block_unlocked_dio(inode
);
5049 inode_dio_wait(inode
);
5050 btrfs_inode_resume_unlocked_dio(inode
);
5052 ret
= btrfs_truncate(inode
);
5053 if (ret
&& inode
->i_nlink
) {
5057 * failed to truncate, disk_i_size is only adjusted down
5058 * as we remove extents, so it should represent the true
5059 * size of the inode, so reset the in memory size and
5060 * delete our orphan entry.
5062 trans
= btrfs_join_transaction(root
);
5063 if (IS_ERR(trans
)) {
5064 btrfs_orphan_del(NULL
, inode
);
5067 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
5068 err
= btrfs_orphan_del(trans
, inode
);
5070 btrfs_abort_transaction(trans
, err
);
5071 btrfs_end_transaction(trans
, root
);
5078 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
5080 struct inode
*inode
= d_inode(dentry
);
5081 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5084 if (btrfs_root_readonly(root
))
5087 err
= setattr_prepare(dentry
, attr
);
5091 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
5092 err
= btrfs_setsize(inode
, attr
);
5097 if (attr
->ia_valid
) {
5098 setattr_copy(inode
, attr
);
5099 inode_inc_iversion(inode
);
5100 err
= btrfs_dirty_inode(inode
);
5102 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
5103 err
= posix_acl_chmod(inode
, inode
->i_mode
);
5110 * While truncating the inode pages during eviction, we get the VFS calling
5111 * btrfs_invalidatepage() against each page of the inode. This is slow because
5112 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5113 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5114 * extent_state structures over and over, wasting lots of time.
5116 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5117 * those expensive operations on a per page basis and do only the ordered io
5118 * finishing, while we release here the extent_map and extent_state structures,
5119 * without the excessive merging and splitting.
5121 static void evict_inode_truncate_pages(struct inode
*inode
)
5123 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5124 struct extent_map_tree
*map_tree
= &BTRFS_I(inode
)->extent_tree
;
5125 struct rb_node
*node
;
5127 ASSERT(inode
->i_state
& I_FREEING
);
5128 truncate_inode_pages_final(&inode
->i_data
);
5130 write_lock(&map_tree
->lock
);
5131 while (!RB_EMPTY_ROOT(&map_tree
->map
)) {
5132 struct extent_map
*em
;
5134 node
= rb_first(&map_tree
->map
);
5135 em
= rb_entry(node
, struct extent_map
, rb_node
);
5136 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5137 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
5138 remove_extent_mapping(map_tree
, em
);
5139 free_extent_map(em
);
5140 if (need_resched()) {
5141 write_unlock(&map_tree
->lock
);
5143 write_lock(&map_tree
->lock
);
5146 write_unlock(&map_tree
->lock
);
5149 * Keep looping until we have no more ranges in the io tree.
5150 * We can have ongoing bios started by readpages (called from readahead)
5151 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5152 * still in progress (unlocked the pages in the bio but did not yet
5153 * unlocked the ranges in the io tree). Therefore this means some
5154 * ranges can still be locked and eviction started because before
5155 * submitting those bios, which are executed by a separate task (work
5156 * queue kthread), inode references (inode->i_count) were not taken
5157 * (which would be dropped in the end io callback of each bio).
5158 * Therefore here we effectively end up waiting for those bios and
5159 * anyone else holding locked ranges without having bumped the inode's
5160 * reference count - if we don't do it, when they access the inode's
5161 * io_tree to unlock a range it may be too late, leading to an
5162 * use-after-free issue.
5164 spin_lock(&io_tree
->lock
);
5165 while (!RB_EMPTY_ROOT(&io_tree
->state
)) {
5166 struct extent_state
*state
;
5167 struct extent_state
*cached_state
= NULL
;
5171 node
= rb_first(&io_tree
->state
);
5172 state
= rb_entry(node
, struct extent_state
, rb_node
);
5173 start
= state
->start
;
5175 spin_unlock(&io_tree
->lock
);
5177 lock_extent_bits(io_tree
, start
, end
, &cached_state
);
5180 * If still has DELALLOC flag, the extent didn't reach disk,
5181 * and its reserved space won't be freed by delayed_ref.
5182 * So we need to free its reserved space here.
5183 * (Refer to comment in btrfs_invalidatepage, case 2)
5185 * Note, end is the bytenr of last byte, so we need + 1 here.
5187 if (state
->state
& EXTENT_DELALLOC
)
5188 btrfs_qgroup_free_data(inode
, start
, end
- start
+ 1);
5190 clear_extent_bit(io_tree
, start
, end
,
5191 EXTENT_LOCKED
| EXTENT_DIRTY
|
5192 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
5193 EXTENT_DEFRAG
, 1, 1,
5194 &cached_state
, GFP_NOFS
);
5197 spin_lock(&io_tree
->lock
);
5199 spin_unlock(&io_tree
->lock
);
5202 void btrfs_evict_inode(struct inode
*inode
)
5204 struct btrfs_trans_handle
*trans
;
5205 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5206 struct btrfs_block_rsv
*rsv
, *global_rsv
;
5207 int steal_from_global
= 0;
5211 trace_btrfs_inode_evict(inode
);
5214 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
5218 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
5220 evict_inode_truncate_pages(inode
);
5222 if (inode
->i_nlink
&&
5223 ((btrfs_root_refs(&root
->root_item
) != 0 &&
5224 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
5225 btrfs_is_free_space_inode(inode
)))
5228 if (is_bad_inode(inode
)) {
5229 btrfs_orphan_del(NULL
, inode
);
5232 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
5233 if (!special_file(inode
->i_mode
))
5234 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
5236 btrfs_free_io_failure_record(inode
, 0, (u64
)-1);
5238 if (test_bit(BTRFS_FS_LOG_RECOVERING
, &root
->fs_info
->flags
)) {
5239 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
5240 &BTRFS_I(inode
)->runtime_flags
));
5244 if (inode
->i_nlink
> 0) {
5245 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
5246 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
5250 ret
= btrfs_commit_inode_delayed_inode(inode
);
5252 btrfs_orphan_del(NULL
, inode
);
5256 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
5258 btrfs_orphan_del(NULL
, inode
);
5261 rsv
->size
= min_size
;
5263 global_rsv
= &root
->fs_info
->global_block_rsv
;
5265 btrfs_i_size_write(inode
, 0);
5268 * This is a bit simpler than btrfs_truncate since we've already
5269 * reserved our space for our orphan item in the unlink, so we just
5270 * need to reserve some slack space in case we add bytes and update
5271 * inode item when doing the truncate.
5274 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
5275 BTRFS_RESERVE_FLUSH_LIMIT
);
5278 * Try and steal from the global reserve since we will
5279 * likely not use this space anyway, we want to try as
5280 * hard as possible to get this to work.
5283 steal_from_global
++;
5285 steal_from_global
= 0;
5289 * steal_from_global == 0: we reserved stuff, hooray!
5290 * steal_from_global == 1: we didn't reserve stuff, boo!
5291 * steal_from_global == 2: we've committed, still not a lot of
5292 * room but maybe we'll have room in the global reserve this
5294 * steal_from_global == 3: abandon all hope!
5296 if (steal_from_global
> 2) {
5297 btrfs_warn(root
->fs_info
,
5298 "Could not get space for a delete, will truncate on mount %d",
5300 btrfs_orphan_del(NULL
, inode
);
5301 btrfs_free_block_rsv(root
, rsv
);
5305 trans
= btrfs_join_transaction(root
);
5306 if (IS_ERR(trans
)) {
5307 btrfs_orphan_del(NULL
, inode
);
5308 btrfs_free_block_rsv(root
, rsv
);
5313 * We can't just steal from the global reserve, we need to make
5314 * sure there is room to do it, if not we need to commit and try
5317 if (steal_from_global
) {
5318 if (!btrfs_check_space_for_delayed_refs(trans
, root
))
5319 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
,
5326 * Couldn't steal from the global reserve, we have too much
5327 * pending stuff built up, commit the transaction and try it
5331 ret
= btrfs_commit_transaction(trans
, root
);
5333 btrfs_orphan_del(NULL
, inode
);
5334 btrfs_free_block_rsv(root
, rsv
);
5339 steal_from_global
= 0;
5342 trans
->block_rsv
= rsv
;
5344 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
5345 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
)
5348 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
5349 btrfs_end_transaction(trans
, root
);
5351 btrfs_btree_balance_dirty(root
);
5354 btrfs_free_block_rsv(root
, rsv
);
5357 * Errors here aren't a big deal, it just means we leave orphan items
5358 * in the tree. They will be cleaned up on the next mount.
5361 trans
->block_rsv
= root
->orphan_block_rsv
;
5362 btrfs_orphan_del(trans
, inode
);
5364 btrfs_orphan_del(NULL
, inode
);
5367 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
5368 if (!(root
== root
->fs_info
->tree_root
||
5369 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
5370 btrfs_return_ino(root
, btrfs_ino(inode
));
5372 btrfs_end_transaction(trans
, root
);
5373 btrfs_btree_balance_dirty(root
);
5375 btrfs_remove_delayed_node(inode
);
5380 * this returns the key found in the dir entry in the location pointer.
5381 * If no dir entries were found, location->objectid is 0.
5383 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
5384 struct btrfs_key
*location
)
5386 const char *name
= dentry
->d_name
.name
;
5387 int namelen
= dentry
->d_name
.len
;
5388 struct btrfs_dir_item
*di
;
5389 struct btrfs_path
*path
;
5390 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5393 path
= btrfs_alloc_path();
5397 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
5402 if (IS_ERR_OR_NULL(di
))
5405 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
5407 btrfs_free_path(path
);
5410 location
->objectid
= 0;
5415 * when we hit a tree root in a directory, the btrfs part of the inode
5416 * needs to be changed to reflect the root directory of the tree root. This
5417 * is kind of like crossing a mount point.
5419 static int fixup_tree_root_location(struct btrfs_root
*root
,
5421 struct dentry
*dentry
,
5422 struct btrfs_key
*location
,
5423 struct btrfs_root
**sub_root
)
5425 struct btrfs_path
*path
;
5426 struct btrfs_root
*new_root
;
5427 struct btrfs_root_ref
*ref
;
5428 struct extent_buffer
*leaf
;
5429 struct btrfs_key key
;
5433 path
= btrfs_alloc_path();
5440 key
.objectid
= BTRFS_I(dir
)->root
->root_key
.objectid
;
5441 key
.type
= BTRFS_ROOT_REF_KEY
;
5442 key
.offset
= location
->objectid
;
5444 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
, &key
, path
,
5452 leaf
= path
->nodes
[0];
5453 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
5454 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
5455 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
5458 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
5459 (unsigned long)(ref
+ 1),
5460 dentry
->d_name
.len
);
5464 btrfs_release_path(path
);
5466 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
5467 if (IS_ERR(new_root
)) {
5468 err
= PTR_ERR(new_root
);
5472 *sub_root
= new_root
;
5473 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
5474 location
->type
= BTRFS_INODE_ITEM_KEY
;
5475 location
->offset
= 0;
5478 btrfs_free_path(path
);
5482 static void inode_tree_add(struct inode
*inode
)
5484 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5485 struct btrfs_inode
*entry
;
5487 struct rb_node
*parent
;
5488 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
5489 u64 ino
= btrfs_ino(inode
);
5491 if (inode_unhashed(inode
))
5494 spin_lock(&root
->inode_lock
);
5495 p
= &root
->inode_tree
.rb_node
;
5498 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
5500 if (ino
< btrfs_ino(&entry
->vfs_inode
))
5501 p
= &parent
->rb_left
;
5502 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
5503 p
= &parent
->rb_right
;
5505 WARN_ON(!(entry
->vfs_inode
.i_state
&
5506 (I_WILL_FREE
| I_FREEING
)));
5507 rb_replace_node(parent
, new, &root
->inode_tree
);
5508 RB_CLEAR_NODE(parent
);
5509 spin_unlock(&root
->inode_lock
);
5513 rb_link_node(new, parent
, p
);
5514 rb_insert_color(new, &root
->inode_tree
);
5515 spin_unlock(&root
->inode_lock
);
5518 static void inode_tree_del(struct inode
*inode
)
5520 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5523 spin_lock(&root
->inode_lock
);
5524 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
5525 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
5526 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
5527 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5529 spin_unlock(&root
->inode_lock
);
5531 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
5532 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
5533 spin_lock(&root
->inode_lock
);
5534 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5535 spin_unlock(&root
->inode_lock
);
5537 btrfs_add_dead_root(root
);
5541 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
5543 struct rb_node
*node
;
5544 struct rb_node
*prev
;
5545 struct btrfs_inode
*entry
;
5546 struct inode
*inode
;
5549 if (!test_bit(BTRFS_FS_STATE_ERROR
, &root
->fs_info
->fs_state
))
5550 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
5552 spin_lock(&root
->inode_lock
);
5554 node
= root
->inode_tree
.rb_node
;
5558 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5560 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
5561 node
= node
->rb_left
;
5562 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
5563 node
= node
->rb_right
;
5569 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
5570 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
5574 prev
= rb_next(prev
);
5578 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5579 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
5580 inode
= igrab(&entry
->vfs_inode
);
5582 spin_unlock(&root
->inode_lock
);
5583 if (atomic_read(&inode
->i_count
) > 1)
5584 d_prune_aliases(inode
);
5586 * btrfs_drop_inode will have it removed from
5587 * the inode cache when its usage count
5592 spin_lock(&root
->inode_lock
);
5596 if (cond_resched_lock(&root
->inode_lock
))
5599 node
= rb_next(node
);
5601 spin_unlock(&root
->inode_lock
);
5604 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
5606 struct btrfs_iget_args
*args
= p
;
5607 inode
->i_ino
= args
->location
->objectid
;
5608 memcpy(&BTRFS_I(inode
)->location
, args
->location
,
5609 sizeof(*args
->location
));
5610 BTRFS_I(inode
)->root
= args
->root
;
5614 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
5616 struct btrfs_iget_args
*args
= opaque
;
5617 return args
->location
->objectid
== BTRFS_I(inode
)->location
.objectid
&&
5618 args
->root
== BTRFS_I(inode
)->root
;
5621 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
5622 struct btrfs_key
*location
,
5623 struct btrfs_root
*root
)
5625 struct inode
*inode
;
5626 struct btrfs_iget_args args
;
5627 unsigned long hashval
= btrfs_inode_hash(location
->objectid
, root
);
5629 args
.location
= location
;
5632 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
5633 btrfs_init_locked_inode
,
5638 /* Get an inode object given its location and corresponding root.
5639 * Returns in *is_new if the inode was read from disk
5641 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
5642 struct btrfs_root
*root
, int *new)
5644 struct inode
*inode
;
5646 inode
= btrfs_iget_locked(s
, location
, root
);
5648 return ERR_PTR(-ENOMEM
);
5650 if (inode
->i_state
& I_NEW
) {
5653 ret
= btrfs_read_locked_inode(inode
);
5654 if (!is_bad_inode(inode
)) {
5655 inode_tree_add(inode
);
5656 unlock_new_inode(inode
);
5660 unlock_new_inode(inode
);
5663 inode
= ERR_PTR(ret
< 0 ? ret
: -ESTALE
);
5670 static struct inode
*new_simple_dir(struct super_block
*s
,
5671 struct btrfs_key
*key
,
5672 struct btrfs_root
*root
)
5674 struct inode
*inode
= new_inode(s
);
5677 return ERR_PTR(-ENOMEM
);
5679 BTRFS_I(inode
)->root
= root
;
5680 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5681 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5683 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5684 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5685 inode
->i_fop
= &simple_dir_operations
;
5686 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5687 inode
->i_mtime
= current_time(inode
);
5688 inode
->i_atime
= inode
->i_mtime
;
5689 inode
->i_ctime
= inode
->i_mtime
;
5690 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
5695 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5697 struct inode
*inode
;
5698 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5699 struct btrfs_root
*sub_root
= root
;
5700 struct btrfs_key location
;
5704 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5705 return ERR_PTR(-ENAMETOOLONG
);
5707 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5709 return ERR_PTR(ret
);
5711 if (location
.objectid
== 0)
5712 return ERR_PTR(-ENOENT
);
5714 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5715 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5719 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5721 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
5722 ret
= fixup_tree_root_location(root
, dir
, dentry
,
5723 &location
, &sub_root
);
5726 inode
= ERR_PTR(ret
);
5728 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5730 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5732 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
5734 if (!IS_ERR(inode
) && root
!= sub_root
) {
5735 down_read(&root
->fs_info
->cleanup_work_sem
);
5736 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5737 ret
= btrfs_orphan_cleanup(sub_root
);
5738 up_read(&root
->fs_info
->cleanup_work_sem
);
5741 inode
= ERR_PTR(ret
);
5748 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5750 struct btrfs_root
*root
;
5751 struct inode
*inode
= d_inode(dentry
);
5753 if (!inode
&& !IS_ROOT(dentry
))
5754 inode
= d_inode(dentry
->d_parent
);
5757 root
= BTRFS_I(inode
)->root
;
5758 if (btrfs_root_refs(&root
->root_item
) == 0)
5761 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5767 static void btrfs_dentry_release(struct dentry
*dentry
)
5769 kfree(dentry
->d_fsdata
);
5772 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5775 struct inode
*inode
;
5777 inode
= btrfs_lookup_dentry(dir
, dentry
);
5778 if (IS_ERR(inode
)) {
5779 if (PTR_ERR(inode
) == -ENOENT
)
5782 return ERR_CAST(inode
);
5785 return d_splice_alias(inode
, dentry
);
5788 unsigned char btrfs_filetype_table
[] = {
5789 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5792 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
5794 struct inode
*inode
= file_inode(file
);
5795 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5796 struct btrfs_item
*item
;
5797 struct btrfs_dir_item
*di
;
5798 struct btrfs_key key
;
5799 struct btrfs_key found_key
;
5800 struct btrfs_path
*path
;
5801 struct list_head ins_list
;
5802 struct list_head del_list
;
5804 struct extent_buffer
*leaf
;
5806 unsigned char d_type
;
5811 int key_type
= BTRFS_DIR_INDEX_KEY
;
5815 int is_curr
= 0; /* ctx->pos points to the current index? */
5819 /* FIXME, use a real flag for deciding about the key type */
5820 if (root
->fs_info
->tree_root
== root
)
5821 key_type
= BTRFS_DIR_ITEM_KEY
;
5823 if (!dir_emit_dots(file
, ctx
))
5826 path
= btrfs_alloc_path();
5830 path
->reada
= READA_FORWARD
;
5832 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5833 INIT_LIST_HEAD(&ins_list
);
5834 INIT_LIST_HEAD(&del_list
);
5835 put
= btrfs_readdir_get_delayed_items(inode
, &ins_list
,
5839 key
.type
= key_type
;
5840 key
.offset
= ctx
->pos
;
5841 key
.objectid
= btrfs_ino(inode
);
5843 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5849 leaf
= path
->nodes
[0];
5850 slot
= path
->slots
[0];
5851 if (slot
>= btrfs_header_nritems(leaf
)) {
5852 ret
= btrfs_next_leaf(root
, path
);
5860 item
= btrfs_item_nr(slot
);
5861 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5863 if (found_key
.objectid
!= key
.objectid
)
5865 if (found_key
.type
!= key_type
)
5867 if (found_key
.offset
< ctx
->pos
)
5869 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5870 btrfs_should_delete_dir_index(&del_list
,
5874 ctx
->pos
= found_key
.offset
;
5877 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5879 di_total
= btrfs_item_size(leaf
, item
);
5881 while (di_cur
< di_total
) {
5882 struct btrfs_key location
;
5884 if (verify_dir_item(root
, leaf
, di
))
5887 name_len
= btrfs_dir_name_len(leaf
, di
);
5888 if (name_len
<= sizeof(tmp_name
)) {
5889 name_ptr
= tmp_name
;
5891 name_ptr
= kmalloc(name_len
, GFP_KERNEL
);
5897 read_extent_buffer(leaf
, name_ptr
,
5898 (unsigned long)(di
+ 1), name_len
);
5900 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5901 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5904 /* is this a reference to our own snapshot? If so
5907 * In contrast to old kernels, we insert the snapshot's
5908 * dir item and dir index after it has been created, so
5909 * we won't find a reference to our own snapshot. We
5910 * still keep the following code for backward
5913 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5914 location
.objectid
== root
->root_key
.objectid
) {
5918 over
= !dir_emit(ctx
, name_ptr
, name_len
,
5919 location
.objectid
, d_type
);
5922 if (name_ptr
!= tmp_name
)
5928 di_len
= btrfs_dir_name_len(leaf
, di
) +
5929 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5931 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5937 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5940 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
, &emitted
);
5946 * If we haven't emitted any dir entry, we must not touch ctx->pos as
5947 * it was was set to the termination value in previous call. We assume
5948 * that "." and ".." were emitted if we reach this point and set the
5949 * termination value as well for an empty directory.
5951 if (ctx
->pos
> 2 && !emitted
)
5954 /* Reached end of directory/root. Bump pos past the last item. */
5958 * Stop new entries from being returned after we return the last
5961 * New directory entries are assigned a strictly increasing
5962 * offset. This means that new entries created during readdir
5963 * are *guaranteed* to be seen in the future by that readdir.
5964 * This has broken buggy programs which operate on names as
5965 * they're returned by readdir. Until we re-use freed offsets
5966 * we have this hack to stop new entries from being returned
5967 * under the assumption that they'll never reach this huge
5970 * This is being careful not to overflow 32bit loff_t unless the
5971 * last entry requires it because doing so has broken 32bit apps
5974 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5975 if (ctx
->pos
>= INT_MAX
)
5976 ctx
->pos
= LLONG_MAX
;
5984 btrfs_readdir_put_delayed_items(inode
, &ins_list
, &del_list
);
5985 btrfs_free_path(path
);
5989 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5991 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5992 struct btrfs_trans_handle
*trans
;
5994 bool nolock
= false;
5996 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5999 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
6002 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
6004 trans
= btrfs_join_transaction_nolock(root
);
6006 trans
= btrfs_join_transaction(root
);
6008 return PTR_ERR(trans
);
6009 ret
= btrfs_commit_transaction(trans
, root
);
6015 * This is somewhat expensive, updating the tree every time the
6016 * inode changes. But, it is most likely to find the inode in cache.
6017 * FIXME, needs more benchmarking...there are no reasons other than performance
6018 * to keep or drop this code.
6020 static int btrfs_dirty_inode(struct inode
*inode
)
6022 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6023 struct btrfs_trans_handle
*trans
;
6026 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
6029 trans
= btrfs_join_transaction(root
);
6031 return PTR_ERR(trans
);
6033 ret
= btrfs_update_inode(trans
, root
, inode
);
6034 if (ret
&& ret
== -ENOSPC
) {
6035 /* whoops, lets try again with the full transaction */
6036 btrfs_end_transaction(trans
, root
);
6037 trans
= btrfs_start_transaction(root
, 1);
6039 return PTR_ERR(trans
);
6041 ret
= btrfs_update_inode(trans
, root
, inode
);
6043 btrfs_end_transaction(trans
, root
);
6044 if (BTRFS_I(inode
)->delayed_node
)
6045 btrfs_balance_delayed_items(root
);
6051 * This is a copy of file_update_time. We need this so we can return error on
6052 * ENOSPC for updating the inode in the case of file write and mmap writes.
6054 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
6057 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6059 if (btrfs_root_readonly(root
))
6062 if (flags
& S_VERSION
)
6063 inode_inc_iversion(inode
);
6064 if (flags
& S_CTIME
)
6065 inode
->i_ctime
= *now
;
6066 if (flags
& S_MTIME
)
6067 inode
->i_mtime
= *now
;
6068 if (flags
& S_ATIME
)
6069 inode
->i_atime
= *now
;
6070 return btrfs_dirty_inode(inode
);
6074 * find the highest existing sequence number in a directory
6075 * and then set the in-memory index_cnt variable to reflect
6076 * free sequence numbers
6078 static int btrfs_set_inode_index_count(struct inode
*inode
)
6080 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6081 struct btrfs_key key
, found_key
;
6082 struct btrfs_path
*path
;
6083 struct extent_buffer
*leaf
;
6086 key
.objectid
= btrfs_ino(inode
);
6087 key
.type
= BTRFS_DIR_INDEX_KEY
;
6088 key
.offset
= (u64
)-1;
6090 path
= btrfs_alloc_path();
6094 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6097 /* FIXME: we should be able to handle this */
6103 * MAGIC NUMBER EXPLANATION:
6104 * since we search a directory based on f_pos we have to start at 2
6105 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6106 * else has to start at 2
6108 if (path
->slots
[0] == 0) {
6109 BTRFS_I(inode
)->index_cnt
= 2;
6115 leaf
= path
->nodes
[0];
6116 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6118 if (found_key
.objectid
!= btrfs_ino(inode
) ||
6119 found_key
.type
!= BTRFS_DIR_INDEX_KEY
) {
6120 BTRFS_I(inode
)->index_cnt
= 2;
6124 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
6126 btrfs_free_path(path
);
6131 * helper to find a free sequence number in a given directory. This current
6132 * code is very simple, later versions will do smarter things in the btree
6134 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
6138 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
6139 ret
= btrfs_inode_delayed_dir_index_count(dir
);
6141 ret
= btrfs_set_inode_index_count(dir
);
6147 *index
= BTRFS_I(dir
)->index_cnt
;
6148 BTRFS_I(dir
)->index_cnt
++;
6153 static int btrfs_insert_inode_locked(struct inode
*inode
)
6155 struct btrfs_iget_args args
;
6156 args
.location
= &BTRFS_I(inode
)->location
;
6157 args
.root
= BTRFS_I(inode
)->root
;
6159 return insert_inode_locked4(inode
,
6160 btrfs_inode_hash(inode
->i_ino
, BTRFS_I(inode
)->root
),
6161 btrfs_find_actor
, &args
);
6164 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
6165 struct btrfs_root
*root
,
6167 const char *name
, int name_len
,
6168 u64 ref_objectid
, u64 objectid
,
6169 umode_t mode
, u64
*index
)
6171 struct inode
*inode
;
6172 struct btrfs_inode_item
*inode_item
;
6173 struct btrfs_key
*location
;
6174 struct btrfs_path
*path
;
6175 struct btrfs_inode_ref
*ref
;
6176 struct btrfs_key key
[2];
6178 int nitems
= name
? 2 : 1;
6182 path
= btrfs_alloc_path();
6184 return ERR_PTR(-ENOMEM
);
6186 inode
= new_inode(root
->fs_info
->sb
);
6188 btrfs_free_path(path
);
6189 return ERR_PTR(-ENOMEM
);
6193 * O_TMPFILE, set link count to 0, so that after this point,
6194 * we fill in an inode item with the correct link count.
6197 set_nlink(inode
, 0);
6200 * we have to initialize this early, so we can reclaim the inode
6201 * number if we fail afterwards in this function.
6203 inode
->i_ino
= objectid
;
6206 trace_btrfs_inode_request(dir
);
6208 ret
= btrfs_set_inode_index(dir
, index
);
6210 btrfs_free_path(path
);
6212 return ERR_PTR(ret
);
6218 * index_cnt is ignored for everything but a dir,
6219 * btrfs_get_inode_index_count has an explanation for the magic
6222 BTRFS_I(inode
)->index_cnt
= 2;
6223 BTRFS_I(inode
)->dir_index
= *index
;
6224 BTRFS_I(inode
)->root
= root
;
6225 BTRFS_I(inode
)->generation
= trans
->transid
;
6226 inode
->i_generation
= BTRFS_I(inode
)->generation
;
6229 * We could have gotten an inode number from somebody who was fsynced
6230 * and then removed in this same transaction, so let's just set full
6231 * sync since it will be a full sync anyway and this will blow away the
6232 * old info in the log.
6234 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
6236 key
[0].objectid
= objectid
;
6237 key
[0].type
= BTRFS_INODE_ITEM_KEY
;
6240 sizes
[0] = sizeof(struct btrfs_inode_item
);
6244 * Start new inodes with an inode_ref. This is slightly more
6245 * efficient for small numbers of hard links since they will
6246 * be packed into one item. Extended refs will kick in if we
6247 * add more hard links than can fit in the ref item.
6249 key
[1].objectid
= objectid
;
6250 key
[1].type
= BTRFS_INODE_REF_KEY
;
6251 key
[1].offset
= ref_objectid
;
6253 sizes
[1] = name_len
+ sizeof(*ref
);
6256 location
= &BTRFS_I(inode
)->location
;
6257 location
->objectid
= objectid
;
6258 location
->offset
= 0;
6259 location
->type
= BTRFS_INODE_ITEM_KEY
;
6261 ret
= btrfs_insert_inode_locked(inode
);
6265 path
->leave_spinning
= 1;
6266 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, nitems
);
6270 inode_init_owner(inode
, dir
, mode
);
6271 inode_set_bytes(inode
, 0);
6273 inode
->i_mtime
= current_time(inode
);
6274 inode
->i_atime
= inode
->i_mtime
;
6275 inode
->i_ctime
= inode
->i_mtime
;
6276 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
6278 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
6279 struct btrfs_inode_item
);
6280 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
6281 sizeof(*inode_item
));
6282 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
6285 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
6286 struct btrfs_inode_ref
);
6287 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
6288 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
6289 ptr
= (unsigned long)(ref
+ 1);
6290 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
6293 btrfs_mark_buffer_dirty(path
->nodes
[0]);
6294 btrfs_free_path(path
);
6296 btrfs_inherit_iflags(inode
, dir
);
6298 if (S_ISREG(mode
)) {
6299 if (btrfs_test_opt(root
->fs_info
, NODATASUM
))
6300 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
6301 if (btrfs_test_opt(root
->fs_info
, NODATACOW
))
6302 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
6303 BTRFS_INODE_NODATASUM
;
6306 inode_tree_add(inode
);
6308 trace_btrfs_inode_new(inode
);
6309 btrfs_set_inode_last_trans(trans
, inode
);
6311 btrfs_update_root_times(trans
, root
);
6313 ret
= btrfs_inode_inherit_props(trans
, inode
, dir
);
6315 btrfs_err(root
->fs_info
,
6316 "error inheriting props for ino %llu (root %llu): %d",
6317 btrfs_ino(inode
), root
->root_key
.objectid
, ret
);
6322 unlock_new_inode(inode
);
6325 BTRFS_I(dir
)->index_cnt
--;
6326 btrfs_free_path(path
);
6328 return ERR_PTR(ret
);
6331 static inline u8
btrfs_inode_type(struct inode
*inode
)
6333 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
6337 * utility function to add 'inode' into 'parent_inode' with
6338 * a give name and a given sequence number.
6339 * if 'add_backref' is true, also insert a backref from the
6340 * inode to the parent directory.
6342 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
6343 struct inode
*parent_inode
, struct inode
*inode
,
6344 const char *name
, int name_len
, int add_backref
, u64 index
)
6347 struct btrfs_key key
;
6348 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
6349 u64 ino
= btrfs_ino(inode
);
6350 u64 parent_ino
= btrfs_ino(parent_inode
);
6352 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6353 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
6356 key
.type
= BTRFS_INODE_ITEM_KEY
;
6360 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6361 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
6362 key
.objectid
, root
->root_key
.objectid
,
6363 parent_ino
, index
, name
, name_len
);
6364 } else if (add_backref
) {
6365 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
6369 /* Nothing to clean up yet */
6373 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
6375 btrfs_inode_type(inode
), index
);
6376 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
6379 btrfs_abort_transaction(trans
, ret
);
6383 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
6385 inode_inc_iversion(parent_inode
);
6386 parent_inode
->i_mtime
= parent_inode
->i_ctime
=
6387 current_time(parent_inode
);
6388 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
6390 btrfs_abort_transaction(trans
, ret
);
6394 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6397 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
6398 key
.objectid
, root
->root_key
.objectid
,
6399 parent_ino
, &local_index
, name
, name_len
);
6401 } else if (add_backref
) {
6405 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
6406 ino
, parent_ino
, &local_index
);
6411 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
6412 struct inode
*dir
, struct dentry
*dentry
,
6413 struct inode
*inode
, int backref
, u64 index
)
6415 int err
= btrfs_add_link(trans
, dir
, inode
,
6416 dentry
->d_name
.name
, dentry
->d_name
.len
,
6423 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
6424 umode_t mode
, dev_t rdev
)
6426 struct btrfs_trans_handle
*trans
;
6427 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6428 struct inode
*inode
= NULL
;
6435 * 2 for inode item and ref
6437 * 1 for xattr if selinux is on
6439 trans
= btrfs_start_transaction(root
, 5);
6441 return PTR_ERR(trans
);
6443 err
= btrfs_find_free_ino(root
, &objectid
);
6447 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6448 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6450 if (IS_ERR(inode
)) {
6451 err
= PTR_ERR(inode
);
6456 * If the active LSM wants to access the inode during
6457 * d_instantiate it needs these. Smack checks to see
6458 * if the filesystem supports xattrs by looking at the
6461 inode
->i_op
= &btrfs_special_inode_operations
;
6462 init_special_inode(inode
, inode
->i_mode
, rdev
);
6464 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6466 goto out_unlock_inode
;
6468 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6470 goto out_unlock_inode
;
6472 btrfs_update_inode(trans
, root
, inode
);
6473 unlock_new_inode(inode
);
6474 d_instantiate(dentry
, inode
);
6478 btrfs_end_transaction(trans
, root
);
6479 btrfs_balance_delayed_items(root
);
6480 btrfs_btree_balance_dirty(root
);
6482 inode_dec_link_count(inode
);
6489 unlock_new_inode(inode
);
6494 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
6495 umode_t mode
, bool excl
)
6497 struct btrfs_trans_handle
*trans
;
6498 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6499 struct inode
*inode
= NULL
;
6500 int drop_inode_on_err
= 0;
6506 * 2 for inode item and ref
6508 * 1 for xattr if selinux is on
6510 trans
= btrfs_start_transaction(root
, 5);
6512 return PTR_ERR(trans
);
6514 err
= btrfs_find_free_ino(root
, &objectid
);
6518 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6519 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6521 if (IS_ERR(inode
)) {
6522 err
= PTR_ERR(inode
);
6525 drop_inode_on_err
= 1;
6527 * If the active LSM wants to access the inode during
6528 * d_instantiate it needs these. Smack checks to see
6529 * if the filesystem supports xattrs by looking at the
6532 inode
->i_fop
= &btrfs_file_operations
;
6533 inode
->i_op
= &btrfs_file_inode_operations
;
6534 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6536 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6538 goto out_unlock_inode
;
6540 err
= btrfs_update_inode(trans
, root
, inode
);
6542 goto out_unlock_inode
;
6544 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6546 goto out_unlock_inode
;
6548 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6549 unlock_new_inode(inode
);
6550 d_instantiate(dentry
, inode
);
6553 btrfs_end_transaction(trans
, root
);
6554 if (err
&& drop_inode_on_err
) {
6555 inode_dec_link_count(inode
);
6558 btrfs_balance_delayed_items(root
);
6559 btrfs_btree_balance_dirty(root
);
6563 unlock_new_inode(inode
);
6568 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
6569 struct dentry
*dentry
)
6571 struct btrfs_trans_handle
*trans
= NULL
;
6572 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6573 struct inode
*inode
= d_inode(old_dentry
);
6578 /* do not allow sys_link's with other subvols of the same device */
6579 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
6582 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
6585 err
= btrfs_set_inode_index(dir
, &index
);
6590 * 2 items for inode and inode ref
6591 * 2 items for dir items
6592 * 1 item for parent inode
6594 trans
= btrfs_start_transaction(root
, 5);
6595 if (IS_ERR(trans
)) {
6596 err
= PTR_ERR(trans
);
6601 /* There are several dir indexes for this inode, clear the cache. */
6602 BTRFS_I(inode
)->dir_index
= 0ULL;
6604 inode_inc_iversion(inode
);
6605 inode
->i_ctime
= current_time(inode
);
6607 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
6609 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
6614 struct dentry
*parent
= dentry
->d_parent
;
6615 err
= btrfs_update_inode(trans
, root
, inode
);
6618 if (inode
->i_nlink
== 1) {
6620 * If new hard link count is 1, it's a file created
6621 * with open(2) O_TMPFILE flag.
6623 err
= btrfs_orphan_del(trans
, inode
);
6627 d_instantiate(dentry
, inode
);
6628 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
6631 btrfs_balance_delayed_items(root
);
6634 btrfs_end_transaction(trans
, root
);
6636 inode_dec_link_count(inode
);
6639 btrfs_btree_balance_dirty(root
);
6643 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
6645 struct inode
*inode
= NULL
;
6646 struct btrfs_trans_handle
*trans
;
6647 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6649 int drop_on_err
= 0;
6654 * 2 items for inode and ref
6655 * 2 items for dir items
6656 * 1 for xattr if selinux is on
6658 trans
= btrfs_start_transaction(root
, 5);
6660 return PTR_ERR(trans
);
6662 err
= btrfs_find_free_ino(root
, &objectid
);
6666 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6667 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6668 S_IFDIR
| mode
, &index
);
6669 if (IS_ERR(inode
)) {
6670 err
= PTR_ERR(inode
);
6675 /* these must be set before we unlock the inode */
6676 inode
->i_op
= &btrfs_dir_inode_operations
;
6677 inode
->i_fop
= &btrfs_dir_file_operations
;
6679 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6681 goto out_fail_inode
;
6683 btrfs_i_size_write(inode
, 0);
6684 err
= btrfs_update_inode(trans
, root
, inode
);
6686 goto out_fail_inode
;
6688 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
6689 dentry
->d_name
.len
, 0, index
);
6691 goto out_fail_inode
;
6693 d_instantiate(dentry
, inode
);
6695 * mkdir is special. We're unlocking after we call d_instantiate
6696 * to avoid a race with nfsd calling d_instantiate.
6698 unlock_new_inode(inode
);
6702 btrfs_end_transaction(trans
, root
);
6704 inode_dec_link_count(inode
);
6707 btrfs_balance_delayed_items(root
);
6708 btrfs_btree_balance_dirty(root
);
6712 unlock_new_inode(inode
);
6716 /* Find next extent map of a given extent map, caller needs to ensure locks */
6717 static struct extent_map
*next_extent_map(struct extent_map
*em
)
6719 struct rb_node
*next
;
6721 next
= rb_next(&em
->rb_node
);
6724 return container_of(next
, struct extent_map
, rb_node
);
6727 static struct extent_map
*prev_extent_map(struct extent_map
*em
)
6729 struct rb_node
*prev
;
6731 prev
= rb_prev(&em
->rb_node
);
6734 return container_of(prev
, struct extent_map
, rb_node
);
6737 /* helper for btfs_get_extent. Given an existing extent in the tree,
6738 * the existing extent is the nearest extent to map_start,
6739 * and an extent that you want to insert, deal with overlap and insert
6740 * the best fitted new extent into the tree.
6742 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
6743 struct extent_map
*existing
,
6744 struct extent_map
*em
,
6747 struct extent_map
*prev
;
6748 struct extent_map
*next
;
6753 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
6755 if (existing
->start
> map_start
) {
6757 prev
= prev_extent_map(next
);
6760 next
= next_extent_map(prev
);
6763 start
= prev
? extent_map_end(prev
) : em
->start
;
6764 start
= max_t(u64
, start
, em
->start
);
6765 end
= next
? next
->start
: extent_map_end(em
);
6766 end
= min_t(u64
, end
, extent_map_end(em
));
6767 start_diff
= start
- em
->start
;
6769 em
->len
= end
- start
;
6770 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
6771 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
6772 em
->block_start
+= start_diff
;
6773 em
->block_len
-= start_diff
;
6775 return add_extent_mapping(em_tree
, em
, 0);
6778 static noinline
int uncompress_inline(struct btrfs_path
*path
,
6780 size_t pg_offset
, u64 extent_offset
,
6781 struct btrfs_file_extent_item
*item
)
6784 struct extent_buffer
*leaf
= path
->nodes
[0];
6787 unsigned long inline_size
;
6791 WARN_ON(pg_offset
!= 0);
6792 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6793 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6794 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6795 btrfs_item_nr(path
->slots
[0]));
6796 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6799 ptr
= btrfs_file_extent_inline_start(item
);
6801 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6803 max_size
= min_t(unsigned long, PAGE_SIZE
, max_size
);
6804 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6805 extent_offset
, inline_size
, max_size
);
6811 * a bit scary, this does extent mapping from logical file offset to the disk.
6812 * the ugly parts come from merging extents from the disk with the in-ram
6813 * representation. This gets more complex because of the data=ordered code,
6814 * where the in-ram extents might be locked pending data=ordered completion.
6816 * This also copies inline extents directly into the page.
6819 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
6820 size_t pg_offset
, u64 start
, u64 len
,
6825 u64 extent_start
= 0;
6827 u64 objectid
= btrfs_ino(inode
);
6829 struct btrfs_path
*path
= NULL
;
6830 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6831 struct btrfs_file_extent_item
*item
;
6832 struct extent_buffer
*leaf
;
6833 struct btrfs_key found_key
;
6834 struct extent_map
*em
= NULL
;
6835 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
6836 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6837 struct btrfs_trans_handle
*trans
= NULL
;
6838 const bool new_inline
= !page
|| create
;
6841 read_lock(&em_tree
->lock
);
6842 em
= lookup_extent_mapping(em_tree
, start
, len
);
6844 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6845 read_unlock(&em_tree
->lock
);
6848 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6849 free_extent_map(em
);
6850 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6851 free_extent_map(em
);
6855 em
= alloc_extent_map();
6860 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6861 em
->start
= EXTENT_MAP_HOLE
;
6862 em
->orig_start
= EXTENT_MAP_HOLE
;
6864 em
->block_len
= (u64
)-1;
6867 path
= btrfs_alloc_path();
6873 * Chances are we'll be called again, so go ahead and do
6876 path
->reada
= READA_FORWARD
;
6879 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6880 objectid
, start
, trans
!= NULL
);
6887 if (path
->slots
[0] == 0)
6892 leaf
= path
->nodes
[0];
6893 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6894 struct btrfs_file_extent_item
);
6895 /* are we inside the extent that was found? */
6896 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6897 found_type
= found_key
.type
;
6898 if (found_key
.objectid
!= objectid
||
6899 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6901 * If we backup past the first extent we want to move forward
6902 * and see if there is an extent in front of us, otherwise we'll
6903 * say there is a hole for our whole search range which can
6910 found_type
= btrfs_file_extent_type(leaf
, item
);
6911 extent_start
= found_key
.offset
;
6912 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6913 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6914 extent_end
= extent_start
+
6915 btrfs_file_extent_num_bytes(leaf
, item
);
6916 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6918 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6919 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6922 if (start
>= extent_end
) {
6924 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6925 ret
= btrfs_next_leaf(root
, path
);
6932 leaf
= path
->nodes
[0];
6934 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6935 if (found_key
.objectid
!= objectid
||
6936 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6938 if (start
+ len
<= found_key
.offset
)
6940 if (start
> found_key
.offset
)
6943 em
->orig_start
= start
;
6944 em
->len
= found_key
.offset
- start
;
6948 btrfs_extent_item_to_extent_map(inode
, path
, item
, new_inline
, em
);
6950 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6951 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6953 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6957 size_t extent_offset
;
6963 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6964 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6965 copy_size
= min_t(u64
, PAGE_SIZE
- pg_offset
,
6966 size
- extent_offset
);
6967 em
->start
= extent_start
+ extent_offset
;
6968 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6969 em
->orig_block_len
= em
->len
;
6970 em
->orig_start
= em
->start
;
6971 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6972 if (create
== 0 && !PageUptodate(page
)) {
6973 if (btrfs_file_extent_compression(leaf
, item
) !=
6974 BTRFS_COMPRESS_NONE
) {
6975 ret
= uncompress_inline(path
, page
, pg_offset
,
6976 extent_offset
, item
);
6983 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6985 if (pg_offset
+ copy_size
< PAGE_SIZE
) {
6986 memset(map
+ pg_offset
+ copy_size
, 0,
6987 PAGE_SIZE
- pg_offset
-
6992 flush_dcache_page(page
);
6993 } else if (create
&& PageUptodate(page
)) {
6997 free_extent_map(em
);
7000 btrfs_release_path(path
);
7001 trans
= btrfs_join_transaction(root
);
7004 return ERR_CAST(trans
);
7008 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
7011 btrfs_mark_buffer_dirty(leaf
);
7013 set_extent_uptodate(io_tree
, em
->start
,
7014 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
7019 em
->orig_start
= start
;
7022 em
->block_start
= EXTENT_MAP_HOLE
;
7023 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
7025 btrfs_release_path(path
);
7026 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
7027 btrfs_err(root
->fs_info
,
7028 "bad extent! em: [%llu %llu] passed [%llu %llu]",
7029 em
->start
, em
->len
, start
, len
);
7035 write_lock(&em_tree
->lock
);
7036 ret
= add_extent_mapping(em_tree
, em
, 0);
7037 /* it is possible that someone inserted the extent into the tree
7038 * while we had the lock dropped. It is also possible that
7039 * an overlapping map exists in the tree
7041 if (ret
== -EEXIST
) {
7042 struct extent_map
*existing
;
7046 existing
= search_extent_mapping(em_tree
, start
, len
);
7048 * existing will always be non-NULL, since there must be
7049 * extent causing the -EEXIST.
7051 if (existing
->start
== em
->start
&&
7052 extent_map_end(existing
) == extent_map_end(em
) &&
7053 em
->block_start
== existing
->block_start
) {
7055 * these two extents are the same, it happens
7056 * with inlines especially
7058 free_extent_map(em
);
7062 } else if (start
>= extent_map_end(existing
) ||
7063 start
<= existing
->start
) {
7065 * The existing extent map is the one nearest to
7066 * the [start, start + len) range which overlaps
7068 err
= merge_extent_mapping(em_tree
, existing
,
7070 free_extent_map(existing
);
7072 free_extent_map(em
);
7076 free_extent_map(em
);
7081 write_unlock(&em_tree
->lock
);
7084 trace_btrfs_get_extent(root
, em
);
7086 btrfs_free_path(path
);
7088 ret
= btrfs_end_transaction(trans
, root
);
7093 free_extent_map(em
);
7094 return ERR_PTR(err
);
7096 BUG_ON(!em
); /* Error is always set */
7100 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
7101 size_t pg_offset
, u64 start
, u64 len
,
7104 struct extent_map
*em
;
7105 struct extent_map
*hole_em
= NULL
;
7106 u64 range_start
= start
;
7112 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
7119 * - a pre-alloc extent,
7120 * there might actually be delalloc bytes behind it.
7122 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
7123 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7129 /* check to see if we've wrapped (len == -1 or similar) */
7138 /* ok, we didn't find anything, lets look for delalloc */
7139 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
7140 end
, len
, EXTENT_DELALLOC
, 1);
7141 found_end
= range_start
+ found
;
7142 if (found_end
< range_start
)
7143 found_end
= (u64
)-1;
7146 * we didn't find anything useful, return
7147 * the original results from get_extent()
7149 if (range_start
> end
|| found_end
<= start
) {
7155 /* adjust the range_start to make sure it doesn't
7156 * go backwards from the start they passed in
7158 range_start
= max(start
, range_start
);
7159 found
= found_end
- range_start
;
7162 u64 hole_start
= start
;
7165 em
= alloc_extent_map();
7171 * when btrfs_get_extent can't find anything it
7172 * returns one huge hole
7174 * make sure what it found really fits our range, and
7175 * adjust to make sure it is based on the start from
7179 u64 calc_end
= extent_map_end(hole_em
);
7181 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
7182 free_extent_map(hole_em
);
7185 hole_start
= max(hole_em
->start
, start
);
7186 hole_len
= calc_end
- hole_start
;
7190 if (hole_em
&& range_start
> hole_start
) {
7191 /* our hole starts before our delalloc, so we
7192 * have to return just the parts of the hole
7193 * that go until the delalloc starts
7195 em
->len
= min(hole_len
,
7196 range_start
- hole_start
);
7197 em
->start
= hole_start
;
7198 em
->orig_start
= hole_start
;
7200 * don't adjust block start at all,
7201 * it is fixed at EXTENT_MAP_HOLE
7203 em
->block_start
= hole_em
->block_start
;
7204 em
->block_len
= hole_len
;
7205 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
7206 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
7208 em
->start
= range_start
;
7210 em
->orig_start
= range_start
;
7211 em
->block_start
= EXTENT_MAP_DELALLOC
;
7212 em
->block_len
= found
;
7214 } else if (hole_em
) {
7219 free_extent_map(hole_em
);
7221 free_extent_map(em
);
7222 return ERR_PTR(err
);
7227 static struct extent_map
*btrfs_create_dio_extent(struct inode
*inode
,
7230 const u64 orig_start
,
7231 const u64 block_start
,
7232 const u64 block_len
,
7233 const u64 orig_block_len
,
7234 const u64 ram_bytes
,
7237 struct extent_map
*em
= NULL
;
7240 down_read(&BTRFS_I(inode
)->dio_sem
);
7241 if (type
!= BTRFS_ORDERED_NOCOW
) {
7242 em
= create_pinned_em(inode
, start
, len
, orig_start
,
7243 block_start
, block_len
, orig_block_len
,
7248 ret
= btrfs_add_ordered_extent_dio(inode
, start
, block_start
,
7249 len
, block_len
, type
);
7252 free_extent_map(em
);
7253 btrfs_drop_extent_cache(inode
, start
,
7254 start
+ len
- 1, 0);
7259 up_read(&BTRFS_I(inode
)->dio_sem
);
7264 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
7267 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7268 struct extent_map
*em
;
7269 struct btrfs_key ins
;
7273 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
7274 ret
= btrfs_reserve_extent(root
, len
, len
, root
->sectorsize
, 0,
7275 alloc_hint
, &ins
, 1, 1);
7277 return ERR_PTR(ret
);
7279 em
= btrfs_create_dio_extent(inode
, start
, ins
.offset
, start
,
7280 ins
.objectid
, ins
.offset
, ins
.offset
,
7282 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
7284 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
7290 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7291 * block must be cow'd
7293 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
7294 u64
*orig_start
, u64
*orig_block_len
,
7297 struct btrfs_trans_handle
*trans
;
7298 struct btrfs_path
*path
;
7300 struct extent_buffer
*leaf
;
7301 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7302 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7303 struct btrfs_file_extent_item
*fi
;
7304 struct btrfs_key key
;
7311 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
7313 path
= btrfs_alloc_path();
7317 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, btrfs_ino(inode
),
7322 slot
= path
->slots
[0];
7325 /* can't find the item, must cow */
7332 leaf
= path
->nodes
[0];
7333 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
7334 if (key
.objectid
!= btrfs_ino(inode
) ||
7335 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
7336 /* not our file or wrong item type, must cow */
7340 if (key
.offset
> offset
) {
7341 /* Wrong offset, must cow */
7345 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
7346 found_type
= btrfs_file_extent_type(leaf
, fi
);
7347 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
7348 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
7349 /* not a regular extent, must cow */
7353 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
7356 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
7357 if (extent_end
<= offset
)
7360 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
7361 if (disk_bytenr
== 0)
7364 if (btrfs_file_extent_compression(leaf
, fi
) ||
7365 btrfs_file_extent_encryption(leaf
, fi
) ||
7366 btrfs_file_extent_other_encoding(leaf
, fi
))
7369 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
7372 *orig_start
= key
.offset
- backref_offset
;
7373 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
7374 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
7377 if (btrfs_extent_readonly(root
, disk_bytenr
))
7380 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
7381 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
7384 range_end
= round_up(offset
+ num_bytes
, root
->sectorsize
) - 1;
7385 ret
= test_range_bit(io_tree
, offset
, range_end
,
7386 EXTENT_DELALLOC
, 0, NULL
);
7393 btrfs_release_path(path
);
7396 * look for other files referencing this extent, if we
7397 * find any we must cow
7399 trans
= btrfs_join_transaction(root
);
7400 if (IS_ERR(trans
)) {
7405 ret
= btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
7406 key
.offset
- backref_offset
, disk_bytenr
);
7407 btrfs_end_transaction(trans
, root
);
7414 * adjust disk_bytenr and num_bytes to cover just the bytes
7415 * in this extent we are about to write. If there
7416 * are any csums in that range we have to cow in order
7417 * to keep the csums correct
7419 disk_bytenr
+= backref_offset
;
7420 disk_bytenr
+= offset
- key
.offset
;
7421 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
7424 * all of the above have passed, it is safe to overwrite this extent
7430 btrfs_free_path(path
);
7434 bool btrfs_page_exists_in_range(struct inode
*inode
, loff_t start
, loff_t end
)
7436 struct radix_tree_root
*root
= &inode
->i_mapping
->page_tree
;
7438 void **pagep
= NULL
;
7439 struct page
*page
= NULL
;
7443 start_idx
= start
>> PAGE_SHIFT
;
7446 * end is the last byte in the last page. end == start is legal
7448 end_idx
= end
>> PAGE_SHIFT
;
7452 /* Most of the code in this while loop is lifted from
7453 * find_get_page. It's been modified to begin searching from a
7454 * page and return just the first page found in that range. If the
7455 * found idx is less than or equal to the end idx then we know that
7456 * a page exists. If no pages are found or if those pages are
7457 * outside of the range then we're fine (yay!) */
7458 while (page
== NULL
&&
7459 radix_tree_gang_lookup_slot(root
, &pagep
, NULL
, start_idx
, 1)) {
7460 page
= radix_tree_deref_slot(pagep
);
7461 if (unlikely(!page
))
7464 if (radix_tree_exception(page
)) {
7465 if (radix_tree_deref_retry(page
)) {
7470 * Otherwise, shmem/tmpfs must be storing a swap entry
7471 * here as an exceptional entry: so return it without
7472 * attempting to raise page count.
7475 break; /* TODO: Is this relevant for this use case? */
7478 if (!page_cache_get_speculative(page
)) {
7484 * Has the page moved?
7485 * This is part of the lockless pagecache protocol. See
7486 * include/linux/pagemap.h for details.
7488 if (unlikely(page
!= *pagep
)) {
7495 if (page
->index
<= end_idx
)
7504 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
7505 struct extent_state
**cached_state
, int writing
)
7507 struct btrfs_ordered_extent
*ordered
;
7511 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7514 * We're concerned with the entire range that we're going to be
7515 * doing DIO to, so we need to make sure there's no ordered
7516 * extents in this range.
7518 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
7519 lockend
- lockstart
+ 1);
7522 * We need to make sure there are no buffered pages in this
7523 * range either, we could have raced between the invalidate in
7524 * generic_file_direct_write and locking the extent. The
7525 * invalidate needs to happen so that reads after a write do not
7530 !btrfs_page_exists_in_range(inode
, lockstart
, lockend
)))
7533 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7534 cached_state
, GFP_NOFS
);
7538 * If we are doing a DIO read and the ordered extent we
7539 * found is for a buffered write, we can not wait for it
7540 * to complete and retry, because if we do so we can
7541 * deadlock with concurrent buffered writes on page
7542 * locks. This happens only if our DIO read covers more
7543 * than one extent map, if at this point has already
7544 * created an ordered extent for a previous extent map
7545 * and locked its range in the inode's io tree, and a
7546 * concurrent write against that previous extent map's
7547 * range and this range started (we unlock the ranges
7548 * in the io tree only when the bios complete and
7549 * buffered writes always lock pages before attempting
7550 * to lock range in the io tree).
7553 test_bit(BTRFS_ORDERED_DIRECT
, &ordered
->flags
))
7554 btrfs_start_ordered_extent(inode
, ordered
, 1);
7557 btrfs_put_ordered_extent(ordered
);
7560 * We could trigger writeback for this range (and wait
7561 * for it to complete) and then invalidate the pages for
7562 * this range (through invalidate_inode_pages2_range()),
7563 * but that can lead us to a deadlock with a concurrent
7564 * call to readpages() (a buffered read or a defrag call
7565 * triggered a readahead) on a page lock due to an
7566 * ordered dio extent we created before but did not have
7567 * yet a corresponding bio submitted (whence it can not
7568 * complete), which makes readpages() wait for that
7569 * ordered extent to complete while holding a lock on
7584 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
7585 u64 len
, u64 orig_start
,
7586 u64 block_start
, u64 block_len
,
7587 u64 orig_block_len
, u64 ram_bytes
,
7590 struct extent_map_tree
*em_tree
;
7591 struct extent_map
*em
;
7592 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7595 em_tree
= &BTRFS_I(inode
)->extent_tree
;
7596 em
= alloc_extent_map();
7598 return ERR_PTR(-ENOMEM
);
7601 em
->orig_start
= orig_start
;
7602 em
->mod_start
= start
;
7605 em
->block_len
= block_len
;
7606 em
->block_start
= block_start
;
7607 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
7608 em
->orig_block_len
= orig_block_len
;
7609 em
->ram_bytes
= ram_bytes
;
7610 em
->generation
= -1;
7611 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
7612 if (type
== BTRFS_ORDERED_PREALLOC
)
7613 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
7616 btrfs_drop_extent_cache(inode
, em
->start
,
7617 em
->start
+ em
->len
- 1, 0);
7618 write_lock(&em_tree
->lock
);
7619 ret
= add_extent_mapping(em_tree
, em
, 1);
7620 write_unlock(&em_tree
->lock
);
7621 } while (ret
== -EEXIST
);
7624 free_extent_map(em
);
7625 return ERR_PTR(ret
);
7631 static void adjust_dio_outstanding_extents(struct inode
*inode
,
7632 struct btrfs_dio_data
*dio_data
,
7635 unsigned num_extents
;
7637 num_extents
= (unsigned) div64_u64(len
+ BTRFS_MAX_EXTENT_SIZE
- 1,
7638 BTRFS_MAX_EXTENT_SIZE
);
7640 * If we have an outstanding_extents count still set then we're
7641 * within our reservation, otherwise we need to adjust our inode
7642 * counter appropriately.
7644 if (dio_data
->outstanding_extents
) {
7645 dio_data
->outstanding_extents
-= num_extents
;
7647 spin_lock(&BTRFS_I(inode
)->lock
);
7648 BTRFS_I(inode
)->outstanding_extents
+= num_extents
;
7649 spin_unlock(&BTRFS_I(inode
)->lock
);
7653 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
7654 struct buffer_head
*bh_result
, int create
)
7656 struct extent_map
*em
;
7657 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7658 struct extent_state
*cached_state
= NULL
;
7659 struct btrfs_dio_data
*dio_data
= NULL
;
7660 u64 start
= iblock
<< inode
->i_blkbits
;
7661 u64 lockstart
, lockend
;
7662 u64 len
= bh_result
->b_size
;
7663 int unlock_bits
= EXTENT_LOCKED
;
7667 unlock_bits
|= EXTENT_DIRTY
;
7669 len
= min_t(u64
, len
, root
->sectorsize
);
7672 lockend
= start
+ len
- 1;
7674 if (current
->journal_info
) {
7676 * Need to pull our outstanding extents and set journal_info to NULL so
7677 * that anything that needs to check if there's a transaction doesn't get
7680 dio_data
= current
->journal_info
;
7681 current
->journal_info
= NULL
;
7685 * If this errors out it's because we couldn't invalidate pagecache for
7686 * this range and we need to fallback to buffered.
7688 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
,
7694 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
7701 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7702 * io. INLINE is special, and we could probably kludge it in here, but
7703 * it's still buffered so for safety lets just fall back to the generic
7706 * For COMPRESSED we _have_ to read the entire extent in so we can
7707 * decompress it, so there will be buffering required no matter what we
7708 * do, so go ahead and fallback to buffered.
7710 * We return -ENOTBLK because that's what makes DIO go ahead and go back
7711 * to buffered IO. Don't blame me, this is the price we pay for using
7714 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
7715 em
->block_start
== EXTENT_MAP_INLINE
) {
7716 free_extent_map(em
);
7721 /* Just a good old fashioned hole, return */
7722 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
7723 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
7724 free_extent_map(em
);
7729 * We don't allocate a new extent in the following cases
7731 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7733 * 2) The extent is marked as PREALLOC. We're good to go here and can
7734 * just use the extent.
7738 len
= min(len
, em
->len
- (start
- em
->start
));
7739 lockstart
= start
+ len
;
7743 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
7744 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
7745 em
->block_start
!= EXTENT_MAP_HOLE
)) {
7747 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
7749 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7750 type
= BTRFS_ORDERED_PREALLOC
;
7752 type
= BTRFS_ORDERED_NOCOW
;
7753 len
= min(len
, em
->len
- (start
- em
->start
));
7754 block_start
= em
->block_start
+ (start
- em
->start
);
7756 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
7757 &orig_block_len
, &ram_bytes
) == 1 &&
7758 btrfs_inc_nocow_writers(root
->fs_info
, block_start
)) {
7759 struct extent_map
*em2
;
7761 em2
= btrfs_create_dio_extent(inode
, start
, len
,
7762 orig_start
, block_start
,
7763 len
, orig_block_len
,
7765 btrfs_dec_nocow_writers(root
->fs_info
, block_start
);
7766 if (type
== BTRFS_ORDERED_PREALLOC
) {
7767 free_extent_map(em
);
7770 if (em2
&& IS_ERR(em2
)) {
7775 * For inode marked NODATACOW or extent marked PREALLOC,
7776 * use the existing or preallocated extent, so does not
7777 * need to adjust btrfs_space_info's bytes_may_use.
7779 btrfs_free_reserved_data_space_noquota(inode
,
7786 * this will cow the extent, reset the len in case we changed
7789 len
= bh_result
->b_size
;
7790 free_extent_map(em
);
7791 em
= btrfs_new_extent_direct(inode
, start
, len
);
7796 len
= min(len
, em
->len
- (start
- em
->start
));
7798 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
7800 bh_result
->b_size
= len
;
7801 bh_result
->b_bdev
= em
->bdev
;
7802 set_buffer_mapped(bh_result
);
7804 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7805 set_buffer_new(bh_result
);
7808 * Need to update the i_size under the extent lock so buffered
7809 * readers will get the updated i_size when we unlock.
7811 if (start
+ len
> i_size_read(inode
))
7812 i_size_write(inode
, start
+ len
);
7814 adjust_dio_outstanding_extents(inode
, dio_data
, len
);
7815 WARN_ON(dio_data
->reserve
< len
);
7816 dio_data
->reserve
-= len
;
7817 dio_data
->unsubmitted_oe_range_end
= start
+ len
;
7818 current
->journal_info
= dio_data
;
7822 * In the case of write we need to clear and unlock the entire range,
7823 * in the case of read we need to unlock only the end area that we
7824 * aren't using if there is any left over space.
7826 if (lockstart
< lockend
) {
7827 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
7828 lockend
, unlock_bits
, 1, 0,
7829 &cached_state
, GFP_NOFS
);
7831 free_extent_state(cached_state
);
7834 free_extent_map(em
);
7839 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7840 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
7843 current
->journal_info
= dio_data
;
7845 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7846 * write less data then expected, so that we don't underflow our inode's
7847 * outstanding extents counter.
7849 if (create
&& dio_data
)
7850 adjust_dio_outstanding_extents(inode
, dio_data
, len
);
7855 static inline int submit_dio_repair_bio(struct inode
*inode
, struct bio
*bio
,
7858 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7861 BUG_ON(bio_op(bio
) == REQ_OP_WRITE
);
7865 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
,
7866 BTRFS_WQ_ENDIO_DIO_REPAIR
);
7870 ret
= btrfs_map_bio(root
, bio
, mirror_num
, 0);
7876 static int btrfs_check_dio_repairable(struct inode
*inode
,
7877 struct bio
*failed_bio
,
7878 struct io_failure_record
*failrec
,
7881 struct btrfs_fs_info
*fs_info
= btrfs_sb(inode
->i_sb
);
7884 num_copies
= btrfs_num_copies(fs_info
, failrec
->logical
, failrec
->len
);
7885 if (num_copies
== 1) {
7887 * we only have a single copy of the data, so don't bother with
7888 * all the retry and error correction code that follows. no
7889 * matter what the error is, it is very likely to persist.
7891 btrfs_debug(fs_info
,
7892 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7893 num_copies
, failrec
->this_mirror
, failed_mirror
);
7897 failrec
->failed_mirror
= failed_mirror
;
7898 failrec
->this_mirror
++;
7899 if (failrec
->this_mirror
== failed_mirror
)
7900 failrec
->this_mirror
++;
7902 if (failrec
->this_mirror
> num_copies
) {
7903 btrfs_debug(fs_info
,
7904 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7905 num_copies
, failrec
->this_mirror
, failed_mirror
);
7912 static int dio_read_error(struct inode
*inode
, struct bio
*failed_bio
,
7913 struct page
*page
, unsigned int pgoff
,
7914 u64 start
, u64 end
, int failed_mirror
,
7915 bio_end_io_t
*repair_endio
, void *repair_arg
)
7917 struct io_failure_record
*failrec
;
7923 BUG_ON(bio_op(failed_bio
) == REQ_OP_WRITE
);
7925 ret
= btrfs_get_io_failure_record(inode
, start
, end
, &failrec
);
7929 ret
= btrfs_check_dio_repairable(inode
, failed_bio
, failrec
,
7932 free_io_failure(inode
, failrec
);
7936 if ((failed_bio
->bi_vcnt
> 1)
7937 || (failed_bio
->bi_io_vec
->bv_len
7938 > BTRFS_I(inode
)->root
->sectorsize
))
7939 read_mode
|= REQ_FAILFAST_DEV
;
7941 isector
= start
- btrfs_io_bio(failed_bio
)->logical
;
7942 isector
>>= inode
->i_sb
->s_blocksize_bits
;
7943 bio
= btrfs_create_repair_bio(inode
, failed_bio
, failrec
, page
,
7944 pgoff
, isector
, repair_endio
, repair_arg
);
7946 free_io_failure(inode
, failrec
);
7949 bio_set_op_attrs(bio
, REQ_OP_READ
, read_mode
);
7951 btrfs_debug(BTRFS_I(inode
)->root
->fs_info
,
7952 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7953 read_mode
, failrec
->this_mirror
, failrec
->in_validation
);
7955 ret
= submit_dio_repair_bio(inode
, bio
, failrec
->this_mirror
);
7957 free_io_failure(inode
, failrec
);
7964 struct btrfs_retry_complete
{
7965 struct completion done
;
7966 struct inode
*inode
;
7971 static void btrfs_retry_endio_nocsum(struct bio
*bio
)
7973 struct btrfs_retry_complete
*done
= bio
->bi_private
;
7974 struct inode
*inode
;
7975 struct bio_vec
*bvec
;
7981 ASSERT(bio
->bi_vcnt
== 1);
7982 inode
= bio
->bi_io_vec
->bv_page
->mapping
->host
;
7983 ASSERT(bio
->bi_io_vec
->bv_len
== BTRFS_I(inode
)->root
->sectorsize
);
7986 bio_for_each_segment_all(bvec
, bio
, i
)
7987 clean_io_failure(done
->inode
, done
->start
, bvec
->bv_page
, 0);
7989 complete(&done
->done
);
7993 static int __btrfs_correct_data_nocsum(struct inode
*inode
,
7994 struct btrfs_io_bio
*io_bio
)
7996 struct btrfs_fs_info
*fs_info
;
7997 struct bio_vec
*bvec
;
7998 struct btrfs_retry_complete done
;
8006 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
8007 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
8009 start
= io_bio
->logical
;
8012 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
8013 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
->bv_len
);
8014 pgoff
= bvec
->bv_offset
;
8016 next_block_or_try_again
:
8019 init_completion(&done
.done
);
8021 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
,
8022 pgoff
, start
, start
+ sectorsize
- 1,
8024 btrfs_retry_endio_nocsum
, &done
);
8028 wait_for_completion(&done
.done
);
8030 if (!done
.uptodate
) {
8031 /* We might have another mirror, so try again */
8032 goto next_block_or_try_again
;
8035 start
+= sectorsize
;
8038 pgoff
+= sectorsize
;
8039 goto next_block_or_try_again
;
8046 static void btrfs_retry_endio(struct bio
*bio
)
8048 struct btrfs_retry_complete
*done
= bio
->bi_private
;
8049 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8050 struct inode
*inode
;
8051 struct bio_vec
*bvec
;
8062 start
= done
->start
;
8064 ASSERT(bio
->bi_vcnt
== 1);
8065 inode
= bio
->bi_io_vec
->bv_page
->mapping
->host
;
8066 ASSERT(bio
->bi_io_vec
->bv_len
== BTRFS_I(inode
)->root
->sectorsize
);
8068 bio_for_each_segment_all(bvec
, bio
, i
) {
8069 ret
= __readpage_endio_check(done
->inode
, io_bio
, i
,
8070 bvec
->bv_page
, bvec
->bv_offset
,
8071 done
->start
, bvec
->bv_len
);
8073 clean_io_failure(done
->inode
, done
->start
,
8074 bvec
->bv_page
, bvec
->bv_offset
);
8079 done
->uptodate
= uptodate
;
8081 complete(&done
->done
);
8085 static int __btrfs_subio_endio_read(struct inode
*inode
,
8086 struct btrfs_io_bio
*io_bio
, int err
)
8088 struct btrfs_fs_info
*fs_info
;
8089 struct bio_vec
*bvec
;
8090 struct btrfs_retry_complete done
;
8100 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
8101 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
8104 start
= io_bio
->logical
;
8107 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
8108 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
->bv_len
);
8110 pgoff
= bvec
->bv_offset
;
8112 csum_pos
= BTRFS_BYTES_TO_BLKS(fs_info
, offset
);
8113 ret
= __readpage_endio_check(inode
, io_bio
, csum_pos
,
8114 bvec
->bv_page
, pgoff
, start
,
8121 init_completion(&done
.done
);
8123 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
,
8124 pgoff
, start
, start
+ sectorsize
- 1,
8126 btrfs_retry_endio
, &done
);
8132 wait_for_completion(&done
.done
);
8134 if (!done
.uptodate
) {
8135 /* We might have another mirror, so try again */
8139 offset
+= sectorsize
;
8140 start
+= sectorsize
;
8145 pgoff
+= sectorsize
;
8153 static int btrfs_subio_endio_read(struct inode
*inode
,
8154 struct btrfs_io_bio
*io_bio
, int err
)
8156 bool skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
8160 return __btrfs_correct_data_nocsum(inode
, io_bio
);
8164 return __btrfs_subio_endio_read(inode
, io_bio
, err
);
8168 static void btrfs_endio_direct_read(struct bio
*bio
)
8170 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8171 struct inode
*inode
= dip
->inode
;
8172 struct bio
*dio_bio
;
8173 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8174 int err
= bio
->bi_error
;
8176 if (dip
->flags
& BTRFS_DIO_ORIG_BIO_SUBMITTED
)
8177 err
= btrfs_subio_endio_read(inode
, io_bio
, err
);
8179 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
8180 dip
->logical_offset
+ dip
->bytes
- 1);
8181 dio_bio
= dip
->dio_bio
;
8185 dio_bio
->bi_error
= bio
->bi_error
;
8186 dio_end_io(dio_bio
, bio
->bi_error
);
8189 io_bio
->end_io(io_bio
, err
);
8193 static void btrfs_endio_direct_write_update_ordered(struct inode
*inode
,
8198 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8199 struct btrfs_ordered_extent
*ordered
= NULL
;
8200 u64 ordered_offset
= offset
;
8201 u64 ordered_bytes
= bytes
;
8205 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
8212 btrfs_init_work(&ordered
->work
, btrfs_endio_write_helper
,
8213 finish_ordered_fn
, NULL
, NULL
);
8214 btrfs_queue_work(root
->fs_info
->endio_write_workers
,
8218 * our bio might span multiple ordered extents. If we haven't
8219 * completed the accounting for the whole dio, go back and try again
8221 if (ordered_offset
< offset
+ bytes
) {
8222 ordered_bytes
= offset
+ bytes
- ordered_offset
;
8228 static void btrfs_endio_direct_write(struct bio
*bio
)
8230 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8231 struct bio
*dio_bio
= dip
->dio_bio
;
8233 btrfs_endio_direct_write_update_ordered(dip
->inode
,
8234 dip
->logical_offset
,
8240 dio_bio
->bi_error
= bio
->bi_error
;
8241 dio_end_io(dio_bio
, bio
->bi_error
);
8245 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
,
8246 struct bio
*bio
, int mirror_num
,
8247 unsigned long bio_flags
, u64 offset
)
8250 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8251 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
8252 BUG_ON(ret
); /* -ENOMEM */
8256 static void btrfs_end_dio_bio(struct bio
*bio
)
8258 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8259 int err
= bio
->bi_error
;
8262 btrfs_warn(BTRFS_I(dip
->inode
)->root
->fs_info
,
8263 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
8264 btrfs_ino(dip
->inode
), bio_op(bio
), bio
->bi_opf
,
8265 (unsigned long long)bio
->bi_iter
.bi_sector
,
8266 bio
->bi_iter
.bi_size
, err
);
8268 if (dip
->subio_endio
)
8269 err
= dip
->subio_endio(dip
->inode
, btrfs_io_bio(bio
), err
);
8275 * before atomic variable goto zero, we must make sure
8276 * dip->errors is perceived to be set.
8278 smp_mb__before_atomic();
8281 /* if there are more bios still pending for this dio, just exit */
8282 if (!atomic_dec_and_test(&dip
->pending_bios
))
8286 bio_io_error(dip
->orig_bio
);
8288 dip
->dio_bio
->bi_error
= 0;
8289 bio_endio(dip
->orig_bio
);
8295 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
8296 u64 first_sector
, gfp_t gfp_flags
)
8299 bio
= btrfs_bio_alloc(bdev
, first_sector
, BIO_MAX_PAGES
, gfp_flags
);
8301 bio_associate_current(bio
);
8305 static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root
*root
,
8306 struct inode
*inode
,
8307 struct btrfs_dio_private
*dip
,
8311 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8312 struct btrfs_io_bio
*orig_io_bio
= btrfs_io_bio(dip
->orig_bio
);
8316 * We load all the csum data we need when we submit
8317 * the first bio to reduce the csum tree search and
8320 if (dip
->logical_offset
== file_offset
) {
8321 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, dip
->orig_bio
,
8327 if (bio
== dip
->orig_bio
)
8330 file_offset
-= dip
->logical_offset
;
8331 file_offset
>>= inode
->i_sb
->s_blocksize_bits
;
8332 io_bio
->csum
= (u8
*)(((u32
*)orig_io_bio
->csum
) + file_offset
);
8337 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
8338 u64 file_offset
, int skip_sum
,
8341 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8342 bool write
= bio_op(bio
) == REQ_OP_WRITE
;
8343 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8347 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
8352 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
,
8353 BTRFS_WQ_ENDIO_DATA
);
8361 if (write
&& async_submit
) {
8362 ret
= btrfs_wq_submit_bio(root
->fs_info
,
8363 inode
, bio
, 0, 0, file_offset
,
8364 __btrfs_submit_bio_start_direct_io
,
8365 __btrfs_submit_bio_done
);
8369 * If we aren't doing async submit, calculate the csum of the
8372 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
8376 ret
= btrfs_lookup_and_bind_dio_csum(root
, inode
, dip
, bio
,
8382 ret
= btrfs_map_bio(root
, bio
, 0, async_submit
);
8388 static int btrfs_submit_direct_hook(struct btrfs_dio_private
*dip
,
8391 struct inode
*inode
= dip
->inode
;
8392 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8394 struct bio
*orig_bio
= dip
->orig_bio
;
8395 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
8396 u64 start_sector
= orig_bio
->bi_iter
.bi_sector
;
8397 u64 file_offset
= dip
->logical_offset
;
8400 u32 blocksize
= root
->sectorsize
;
8401 int async_submit
= 0;
8406 map_length
= orig_bio
->bi_iter
.bi_size
;
8407 ret
= btrfs_map_block(root
->fs_info
, bio_op(orig_bio
),
8408 start_sector
<< 9, &map_length
, NULL
, 0);
8412 if (map_length
>= orig_bio
->bi_iter
.bi_size
) {
8414 dip
->flags
|= BTRFS_DIO_ORIG_BIO_SUBMITTED
;
8418 /* async crcs make it difficult to collect full stripe writes. */
8419 if (btrfs_get_alloc_profile(root
, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK
)
8424 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
8428 bio
->bi_opf
= orig_bio
->bi_opf
;
8429 bio
->bi_private
= dip
;
8430 bio
->bi_end_io
= btrfs_end_dio_bio
;
8431 btrfs_io_bio(bio
)->logical
= file_offset
;
8432 atomic_inc(&dip
->pending_bios
);
8434 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
8435 nr_sectors
= BTRFS_BYTES_TO_BLKS(root
->fs_info
, bvec
->bv_len
);
8438 if (unlikely(map_length
< submit_len
+ blocksize
||
8439 bio_add_page(bio
, bvec
->bv_page
, blocksize
,
8440 bvec
->bv_offset
+ (i
* blocksize
)) < blocksize
)) {
8442 * inc the count before we submit the bio so
8443 * we know the end IO handler won't happen before
8444 * we inc the count. Otherwise, the dip might get freed
8445 * before we're done setting it up
8447 atomic_inc(&dip
->pending_bios
);
8448 ret
= __btrfs_submit_dio_bio(bio
, inode
,
8449 file_offset
, skip_sum
,
8453 atomic_dec(&dip
->pending_bios
);
8457 start_sector
+= submit_len
>> 9;
8458 file_offset
+= submit_len
;
8462 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
8463 start_sector
, GFP_NOFS
);
8466 bio
->bi_opf
= orig_bio
->bi_opf
;
8467 bio
->bi_private
= dip
;
8468 bio
->bi_end_io
= btrfs_end_dio_bio
;
8469 btrfs_io_bio(bio
)->logical
= file_offset
;
8471 map_length
= orig_bio
->bi_iter
.bi_size
;
8472 ret
= btrfs_map_block(root
->fs_info
, bio_op(orig_bio
),
8474 &map_length
, NULL
, 0);
8482 submit_len
+= blocksize
;
8492 ret
= __btrfs_submit_dio_bio(bio
, inode
, file_offset
, skip_sum
,
8501 * before atomic variable goto zero, we must
8502 * make sure dip->errors is perceived to be set.
8504 smp_mb__before_atomic();
8505 if (atomic_dec_and_test(&dip
->pending_bios
))
8506 bio_io_error(dip
->orig_bio
);
8508 /* bio_end_io() will handle error, so we needn't return it */
8512 static void btrfs_submit_direct(struct bio
*dio_bio
, struct inode
*inode
,
8515 struct btrfs_dio_private
*dip
= NULL
;
8516 struct bio
*io_bio
= NULL
;
8517 struct btrfs_io_bio
*btrfs_bio
;
8519 bool write
= (bio_op(dio_bio
) == REQ_OP_WRITE
);
8522 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
8524 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
8530 dip
= kzalloc(sizeof(*dip
), GFP_NOFS
);
8536 dip
->private = dio_bio
->bi_private
;
8538 dip
->logical_offset
= file_offset
;
8539 dip
->bytes
= dio_bio
->bi_iter
.bi_size
;
8540 dip
->disk_bytenr
= (u64
)dio_bio
->bi_iter
.bi_sector
<< 9;
8541 io_bio
->bi_private
= dip
;
8542 dip
->orig_bio
= io_bio
;
8543 dip
->dio_bio
= dio_bio
;
8544 atomic_set(&dip
->pending_bios
, 0);
8545 btrfs_bio
= btrfs_io_bio(io_bio
);
8546 btrfs_bio
->logical
= file_offset
;
8549 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
8551 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
8552 dip
->subio_endio
= btrfs_subio_endio_read
;
8556 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8557 * even if we fail to submit a bio, because in such case we do the
8558 * corresponding error handling below and it must not be done a second
8559 * time by btrfs_direct_IO().
8562 struct btrfs_dio_data
*dio_data
= current
->journal_info
;
8564 dio_data
->unsubmitted_oe_range_end
= dip
->logical_offset
+
8566 dio_data
->unsubmitted_oe_range_start
=
8567 dio_data
->unsubmitted_oe_range_end
;
8570 ret
= btrfs_submit_direct_hook(dip
, skip_sum
);
8574 if (btrfs_bio
->end_io
)
8575 btrfs_bio
->end_io(btrfs_bio
, ret
);
8579 * If we arrived here it means either we failed to submit the dip
8580 * or we either failed to clone the dio_bio or failed to allocate the
8581 * dip. If we cloned the dio_bio and allocated the dip, we can just
8582 * call bio_endio against our io_bio so that we get proper resource
8583 * cleanup if we fail to submit the dip, otherwise, we must do the
8584 * same as btrfs_endio_direct_[write|read] because we can't call these
8585 * callbacks - they require an allocated dip and a clone of dio_bio.
8587 if (io_bio
&& dip
) {
8588 io_bio
->bi_error
= -EIO
;
8591 * The end io callbacks free our dip, do the final put on io_bio
8592 * and all the cleanup and final put for dio_bio (through
8599 btrfs_endio_direct_write_update_ordered(inode
,
8601 dio_bio
->bi_iter
.bi_size
,
8604 unlock_extent(&BTRFS_I(inode
)->io_tree
, file_offset
,
8605 file_offset
+ dio_bio
->bi_iter
.bi_size
- 1);
8607 dio_bio
->bi_error
= -EIO
;
8609 * Releases and cleans up our dio_bio, no need to bio_put()
8610 * nor bio_endio()/bio_io_error() against dio_bio.
8612 dio_end_io(dio_bio
, ret
);
8619 static ssize_t
check_direct_IO(struct btrfs_root
*root
, struct kiocb
*iocb
,
8620 const struct iov_iter
*iter
, loff_t offset
)
8624 unsigned blocksize_mask
= root
->sectorsize
- 1;
8625 ssize_t retval
= -EINVAL
;
8627 if (offset
& blocksize_mask
)
8630 if (iov_iter_alignment(iter
) & blocksize_mask
)
8633 /* If this is a write we don't need to check anymore */
8634 if (iov_iter_rw(iter
) != READ
|| !iter_is_iovec(iter
))
8637 * Check to make sure we don't have duplicate iov_base's in this
8638 * iovec, if so return EINVAL, otherwise we'll get csum errors
8639 * when reading back.
8641 for (seg
= 0; seg
< iter
->nr_segs
; seg
++) {
8642 for (i
= seg
+ 1; i
< iter
->nr_segs
; i
++) {
8643 if (iter
->iov
[seg
].iov_base
== iter
->iov
[i
].iov_base
)
8652 static ssize_t
btrfs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
8654 struct file
*file
= iocb
->ki_filp
;
8655 struct inode
*inode
= file
->f_mapping
->host
;
8656 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8657 struct btrfs_dio_data dio_data
= { 0 };
8658 loff_t offset
= iocb
->ki_pos
;
8662 bool relock
= false;
8665 if (check_direct_IO(BTRFS_I(inode
)->root
, iocb
, iter
, offset
))
8668 inode_dio_begin(inode
);
8669 smp_mb__after_atomic();
8672 * The generic stuff only does filemap_write_and_wait_range, which
8673 * isn't enough if we've written compressed pages to this area, so
8674 * we need to flush the dirty pages again to make absolutely sure
8675 * that any outstanding dirty pages are on disk.
8677 count
= iov_iter_count(iter
);
8678 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
8679 &BTRFS_I(inode
)->runtime_flags
))
8680 filemap_fdatawrite_range(inode
->i_mapping
, offset
,
8681 offset
+ count
- 1);
8683 if (iov_iter_rw(iter
) == WRITE
) {
8685 * If the write DIO is beyond the EOF, we need update
8686 * the isize, but it is protected by i_mutex. So we can
8687 * not unlock the i_mutex at this case.
8689 if (offset
+ count
<= inode
->i_size
) {
8690 inode_unlock(inode
);
8693 ret
= btrfs_delalloc_reserve_space(inode
, offset
, count
);
8696 dio_data
.outstanding_extents
= div64_u64(count
+
8697 BTRFS_MAX_EXTENT_SIZE
- 1,
8698 BTRFS_MAX_EXTENT_SIZE
);
8701 * We need to know how many extents we reserved so that we can
8702 * do the accounting properly if we go over the number we
8703 * originally calculated. Abuse current->journal_info for this.
8705 dio_data
.reserve
= round_up(count
, root
->sectorsize
);
8706 dio_data
.unsubmitted_oe_range_start
= (u64
)offset
;
8707 dio_data
.unsubmitted_oe_range_end
= (u64
)offset
;
8708 current
->journal_info
= &dio_data
;
8709 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
8710 &BTRFS_I(inode
)->runtime_flags
)) {
8711 inode_dio_end(inode
);
8712 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
8716 ret
= __blockdev_direct_IO(iocb
, inode
,
8717 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
8718 iter
, btrfs_get_blocks_direct
, NULL
,
8719 btrfs_submit_direct
, flags
);
8720 if (iov_iter_rw(iter
) == WRITE
) {
8721 current
->journal_info
= NULL
;
8722 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
8723 if (dio_data
.reserve
)
8724 btrfs_delalloc_release_space(inode
, offset
,
8727 * On error we might have left some ordered extents
8728 * without submitting corresponding bios for them, so
8729 * cleanup them up to avoid other tasks getting them
8730 * and waiting for them to complete forever.
8732 if (dio_data
.unsubmitted_oe_range_start
<
8733 dio_data
.unsubmitted_oe_range_end
)
8734 btrfs_endio_direct_write_update_ordered(inode
,
8735 dio_data
.unsubmitted_oe_range_start
,
8736 dio_data
.unsubmitted_oe_range_end
-
8737 dio_data
.unsubmitted_oe_range_start
,
8739 } else if (ret
>= 0 && (size_t)ret
< count
)
8740 btrfs_delalloc_release_space(inode
, offset
,
8741 count
- (size_t)ret
);
8745 inode_dio_end(inode
);
8752 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8754 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
8755 __u64 start
, __u64 len
)
8759 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
8763 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
8766 int btrfs_readpage(struct file
*file
, struct page
*page
)
8768 struct extent_io_tree
*tree
;
8769 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8770 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
8773 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
8775 struct extent_io_tree
*tree
;
8776 struct inode
*inode
= page
->mapping
->host
;
8779 if (current
->flags
& PF_MEMALLOC
) {
8780 redirty_page_for_writepage(wbc
, page
);
8786 * If we are under memory pressure we will call this directly from the
8787 * VM, we need to make sure we have the inode referenced for the ordered
8788 * extent. If not just return like we didn't do anything.
8790 if (!igrab(inode
)) {
8791 redirty_page_for_writepage(wbc
, page
);
8792 return AOP_WRITEPAGE_ACTIVATE
;
8794 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8795 ret
= extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
8796 btrfs_add_delayed_iput(inode
);
8800 static int btrfs_writepages(struct address_space
*mapping
,
8801 struct writeback_control
*wbc
)
8803 struct extent_io_tree
*tree
;
8805 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8806 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
8810 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
8811 struct list_head
*pages
, unsigned nr_pages
)
8813 struct extent_io_tree
*tree
;
8814 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8815 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
8818 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8820 struct extent_io_tree
*tree
;
8821 struct extent_map_tree
*map
;
8824 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8825 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
8826 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
8828 ClearPagePrivate(page
);
8829 set_page_private(page
, 0);
8835 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8837 if (PageWriteback(page
) || PageDirty(page
))
8839 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
8842 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
8843 unsigned int length
)
8845 struct inode
*inode
= page
->mapping
->host
;
8846 struct extent_io_tree
*tree
;
8847 struct btrfs_ordered_extent
*ordered
;
8848 struct extent_state
*cached_state
= NULL
;
8849 u64 page_start
= page_offset(page
);
8850 u64 page_end
= page_start
+ PAGE_SIZE
- 1;
8853 int inode_evicting
= inode
->i_state
& I_FREEING
;
8856 * we have the page locked, so new writeback can't start,
8857 * and the dirty bit won't be cleared while we are here.
8859 * Wait for IO on this page so that we can safely clear
8860 * the PagePrivate2 bit and do ordered accounting
8862 wait_on_page_writeback(page
);
8864 tree
= &BTRFS_I(inode
)->io_tree
;
8866 btrfs_releasepage(page
, GFP_NOFS
);
8870 if (!inode_evicting
)
8871 lock_extent_bits(tree
, page_start
, page_end
, &cached_state
);
8874 ordered
= btrfs_lookup_ordered_range(inode
, start
,
8875 page_end
- start
+ 1);
8877 end
= min(page_end
, ordered
->file_offset
+ ordered
->len
- 1);
8879 * IO on this page will never be started, so we need
8880 * to account for any ordered extents now
8882 if (!inode_evicting
)
8883 clear_extent_bit(tree
, start
, end
,
8884 EXTENT_DIRTY
| EXTENT_DELALLOC
|
8885 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
8886 EXTENT_DEFRAG
, 1, 0, &cached_state
,
8889 * whoever cleared the private bit is responsible
8890 * for the finish_ordered_io
8892 if (TestClearPagePrivate2(page
)) {
8893 struct btrfs_ordered_inode_tree
*tree
;
8896 tree
= &BTRFS_I(inode
)->ordered_tree
;
8898 spin_lock_irq(&tree
->lock
);
8899 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
8900 new_len
= start
- ordered
->file_offset
;
8901 if (new_len
< ordered
->truncated_len
)
8902 ordered
->truncated_len
= new_len
;
8903 spin_unlock_irq(&tree
->lock
);
8905 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
8907 end
- start
+ 1, 1))
8908 btrfs_finish_ordered_io(ordered
);
8910 btrfs_put_ordered_extent(ordered
);
8911 if (!inode_evicting
) {
8912 cached_state
= NULL
;
8913 lock_extent_bits(tree
, start
, end
,
8918 if (start
< page_end
)
8923 * Qgroup reserved space handler
8924 * Page here will be either
8925 * 1) Already written to disk
8926 * In this case, its reserved space is released from data rsv map
8927 * and will be freed by delayed_ref handler finally.
8928 * So even we call qgroup_free_data(), it won't decrease reserved
8930 * 2) Not written to disk
8931 * This means the reserved space should be freed here. However,
8932 * if a truncate invalidates the page (by clearing PageDirty)
8933 * and the page is accounted for while allocating extent
8934 * in btrfs_check_data_free_space() we let delayed_ref to
8935 * free the entire extent.
8937 if (PageDirty(page
))
8938 btrfs_qgroup_free_data(inode
, page_start
, PAGE_SIZE
);
8939 if (!inode_evicting
) {
8940 clear_extent_bit(tree
, page_start
, page_end
,
8941 EXTENT_LOCKED
| EXTENT_DIRTY
|
8942 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
8943 EXTENT_DEFRAG
, 1, 1,
8944 &cached_state
, GFP_NOFS
);
8946 __btrfs_releasepage(page
, GFP_NOFS
);
8949 ClearPageChecked(page
);
8950 if (PagePrivate(page
)) {
8951 ClearPagePrivate(page
);
8952 set_page_private(page
, 0);
8958 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8959 * called from a page fault handler when a page is first dirtied. Hence we must
8960 * be careful to check for EOF conditions here. We set the page up correctly
8961 * for a written page which means we get ENOSPC checking when writing into
8962 * holes and correct delalloc and unwritten extent mapping on filesystems that
8963 * support these features.
8965 * We are not allowed to take the i_mutex here so we have to play games to
8966 * protect against truncate races as the page could now be beyond EOF. Because
8967 * vmtruncate() writes the inode size before removing pages, once we have the
8968 * page lock we can determine safely if the page is beyond EOF. If it is not
8969 * beyond EOF, then the page is guaranteed safe against truncation until we
8972 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
8974 struct page
*page
= vmf
->page
;
8975 struct inode
*inode
= file_inode(vma
->vm_file
);
8976 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8977 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
8978 struct btrfs_ordered_extent
*ordered
;
8979 struct extent_state
*cached_state
= NULL
;
8981 unsigned long zero_start
;
8990 reserved_space
= PAGE_SIZE
;
8992 sb_start_pagefault(inode
->i_sb
);
8993 page_start
= page_offset(page
);
8994 page_end
= page_start
+ PAGE_SIZE
- 1;
8998 * Reserving delalloc space after obtaining the page lock can lead to
8999 * deadlock. For example, if a dirty page is locked by this function
9000 * and the call to btrfs_delalloc_reserve_space() ends up triggering
9001 * dirty page write out, then the btrfs_writepage() function could
9002 * end up waiting indefinitely to get a lock on the page currently
9003 * being processed by btrfs_page_mkwrite() function.
9005 ret
= btrfs_delalloc_reserve_space(inode
, page_start
,
9008 ret
= file_update_time(vma
->vm_file
);
9014 else /* -ENOSPC, -EIO, etc */
9015 ret
= VM_FAULT_SIGBUS
;
9021 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
9024 size
= i_size_read(inode
);
9026 if ((page
->mapping
!= inode
->i_mapping
) ||
9027 (page_start
>= size
)) {
9028 /* page got truncated out from underneath us */
9031 wait_on_page_writeback(page
);
9033 lock_extent_bits(io_tree
, page_start
, page_end
, &cached_state
);
9034 set_page_extent_mapped(page
);
9037 * we can't set the delalloc bits if there are pending ordered
9038 * extents. Drop our locks and wait for them to finish
9040 ordered
= btrfs_lookup_ordered_range(inode
, page_start
, page_end
);
9042 unlock_extent_cached(io_tree
, page_start
, page_end
,
9043 &cached_state
, GFP_NOFS
);
9045 btrfs_start_ordered_extent(inode
, ordered
, 1);
9046 btrfs_put_ordered_extent(ordered
);
9050 if (page
->index
== ((size
- 1) >> PAGE_SHIFT
)) {
9051 reserved_space
= round_up(size
- page_start
, root
->sectorsize
);
9052 if (reserved_space
< PAGE_SIZE
) {
9053 end
= page_start
+ reserved_space
- 1;
9054 spin_lock(&BTRFS_I(inode
)->lock
);
9055 BTRFS_I(inode
)->outstanding_extents
++;
9056 spin_unlock(&BTRFS_I(inode
)->lock
);
9057 btrfs_delalloc_release_space(inode
, page_start
,
9058 PAGE_SIZE
- reserved_space
);
9063 * XXX - page_mkwrite gets called every time the page is dirtied, even
9064 * if it was already dirty, so for space accounting reasons we need to
9065 * clear any delalloc bits for the range we are fixing to save. There
9066 * is probably a better way to do this, but for now keep consistent with
9067 * prepare_pages in the normal write path.
9069 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, end
,
9070 EXTENT_DIRTY
| EXTENT_DELALLOC
|
9071 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
9072 0, 0, &cached_state
, GFP_NOFS
);
9074 ret
= btrfs_set_extent_delalloc(inode
, page_start
, end
,
9077 unlock_extent_cached(io_tree
, page_start
, page_end
,
9078 &cached_state
, GFP_NOFS
);
9079 ret
= VM_FAULT_SIGBUS
;
9084 /* page is wholly or partially inside EOF */
9085 if (page_start
+ PAGE_SIZE
> size
)
9086 zero_start
= size
& ~PAGE_MASK
;
9088 zero_start
= PAGE_SIZE
;
9090 if (zero_start
!= PAGE_SIZE
) {
9092 memset(kaddr
+ zero_start
, 0, PAGE_SIZE
- zero_start
);
9093 flush_dcache_page(page
);
9096 ClearPageChecked(page
);
9097 set_page_dirty(page
);
9098 SetPageUptodate(page
);
9100 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
9101 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
9102 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
9104 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
9108 sb_end_pagefault(inode
->i_sb
);
9109 return VM_FAULT_LOCKED
;
9113 btrfs_delalloc_release_space(inode
, page_start
, reserved_space
);
9115 sb_end_pagefault(inode
->i_sb
);
9119 static int btrfs_truncate(struct inode
*inode
)
9121 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9122 struct btrfs_block_rsv
*rsv
;
9125 struct btrfs_trans_handle
*trans
;
9126 u64 mask
= root
->sectorsize
- 1;
9127 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
9129 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
9135 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
9136 * 3 things going on here
9138 * 1) We need to reserve space for our orphan item and the space to
9139 * delete our orphan item. Lord knows we don't want to have a dangling
9140 * orphan item because we didn't reserve space to remove it.
9142 * 2) We need to reserve space to update our inode.
9144 * 3) We need to have something to cache all the space that is going to
9145 * be free'd up by the truncate operation, but also have some slack
9146 * space reserved in case it uses space during the truncate (thank you
9147 * very much snapshotting).
9149 * And we need these to all be separate. The fact is we can use a lot of
9150 * space doing the truncate, and we have no earthly idea how much space
9151 * we will use, so we need the truncate reservation to be separate so it
9152 * doesn't end up using space reserved for updating the inode or
9153 * removing the orphan item. We also need to be able to stop the
9154 * transaction and start a new one, which means we need to be able to
9155 * update the inode several times, and we have no idea of knowing how
9156 * many times that will be, so we can't just reserve 1 item for the
9157 * entirety of the operation, so that has to be done separately as well.
9158 * Then there is the orphan item, which does indeed need to be held on
9159 * to for the whole operation, and we need nobody to touch this reserved
9160 * space except the orphan code.
9162 * So that leaves us with
9164 * 1) root->orphan_block_rsv - for the orphan deletion.
9165 * 2) rsv - for the truncate reservation, which we will steal from the
9166 * transaction reservation.
9167 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9168 * updating the inode.
9170 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
9173 rsv
->size
= min_size
;
9177 * 1 for the truncate slack space
9178 * 1 for updating the inode.
9180 trans
= btrfs_start_transaction(root
, 2);
9181 if (IS_ERR(trans
)) {
9182 err
= PTR_ERR(trans
);
9186 /* Migrate the slack space for the truncate to our reserve */
9187 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
9192 * So if we truncate and then write and fsync we normally would just
9193 * write the extents that changed, which is a problem if we need to
9194 * first truncate that entire inode. So set this flag so we write out
9195 * all of the extents in the inode to the sync log so we're completely
9198 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
9199 trans
->block_rsv
= rsv
;
9202 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
9204 BTRFS_EXTENT_DATA_KEY
);
9205 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
) {
9210 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
9211 ret
= btrfs_update_inode(trans
, root
, inode
);
9217 btrfs_end_transaction(trans
, root
);
9218 btrfs_btree_balance_dirty(root
);
9220 trans
= btrfs_start_transaction(root
, 2);
9221 if (IS_ERR(trans
)) {
9222 ret
= err
= PTR_ERR(trans
);
9227 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
9229 BUG_ON(ret
); /* shouldn't happen */
9230 trans
->block_rsv
= rsv
;
9233 if (ret
== 0 && inode
->i_nlink
> 0) {
9234 trans
->block_rsv
= root
->orphan_block_rsv
;
9235 ret
= btrfs_orphan_del(trans
, inode
);
9241 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
9242 ret
= btrfs_update_inode(trans
, root
, inode
);
9246 ret
= btrfs_end_transaction(trans
, root
);
9247 btrfs_btree_balance_dirty(root
);
9250 btrfs_free_block_rsv(root
, rsv
);
9259 * create a new subvolume directory/inode (helper for the ioctl).
9261 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
9262 struct btrfs_root
*new_root
,
9263 struct btrfs_root
*parent_root
,
9266 struct inode
*inode
;
9270 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
9271 new_dirid
, new_dirid
,
9272 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
9275 return PTR_ERR(inode
);
9276 inode
->i_op
= &btrfs_dir_inode_operations
;
9277 inode
->i_fop
= &btrfs_dir_file_operations
;
9279 set_nlink(inode
, 1);
9280 btrfs_i_size_write(inode
, 0);
9281 unlock_new_inode(inode
);
9283 err
= btrfs_subvol_inherit_props(trans
, new_root
, parent_root
);
9285 btrfs_err(new_root
->fs_info
,
9286 "error inheriting subvolume %llu properties: %d",
9287 new_root
->root_key
.objectid
, err
);
9289 err
= btrfs_update_inode(trans
, new_root
, inode
);
9295 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
9297 struct btrfs_inode
*ei
;
9298 struct inode
*inode
;
9300 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
9307 ei
->last_sub_trans
= 0;
9308 ei
->logged_trans
= 0;
9309 ei
->delalloc_bytes
= 0;
9310 ei
->defrag_bytes
= 0;
9311 ei
->disk_i_size
= 0;
9314 ei
->index_cnt
= (u64
)-1;
9316 ei
->last_unlink_trans
= 0;
9317 ei
->last_log_commit
= 0;
9318 ei
->delayed_iput_count
= 0;
9320 spin_lock_init(&ei
->lock
);
9321 ei
->outstanding_extents
= 0;
9322 ei
->reserved_extents
= 0;
9324 ei
->runtime_flags
= 0;
9325 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
9327 ei
->delayed_node
= NULL
;
9329 ei
->i_otime
.tv_sec
= 0;
9330 ei
->i_otime
.tv_nsec
= 0;
9332 inode
= &ei
->vfs_inode
;
9333 extent_map_tree_init(&ei
->extent_tree
);
9334 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
9335 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
9336 ei
->io_tree
.track_uptodate
= 1;
9337 ei
->io_failure_tree
.track_uptodate
= 1;
9338 atomic_set(&ei
->sync_writers
, 0);
9339 mutex_init(&ei
->log_mutex
);
9340 mutex_init(&ei
->delalloc_mutex
);
9341 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
9342 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
9343 INIT_LIST_HEAD(&ei
->delayed_iput
);
9344 RB_CLEAR_NODE(&ei
->rb_node
);
9345 init_rwsem(&ei
->dio_sem
);
9350 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9351 void btrfs_test_destroy_inode(struct inode
*inode
)
9353 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
9354 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9358 static void btrfs_i_callback(struct rcu_head
*head
)
9360 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
9361 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9364 void btrfs_destroy_inode(struct inode
*inode
)
9366 struct btrfs_ordered_extent
*ordered
;
9367 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9369 WARN_ON(!hlist_empty(&inode
->i_dentry
));
9370 WARN_ON(inode
->i_data
.nrpages
);
9371 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
9372 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
9373 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
9374 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
9375 WARN_ON(BTRFS_I(inode
)->defrag_bytes
);
9378 * This can happen where we create an inode, but somebody else also
9379 * created the same inode and we need to destroy the one we already
9385 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
9386 &BTRFS_I(inode
)->runtime_flags
)) {
9387 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
9389 atomic_dec(&root
->orphan_inodes
);
9393 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
9397 btrfs_err(root
->fs_info
,
9398 "found ordered extent %llu %llu on inode cleanup",
9399 ordered
->file_offset
, ordered
->len
);
9400 btrfs_remove_ordered_extent(inode
, ordered
);
9401 btrfs_put_ordered_extent(ordered
);
9402 btrfs_put_ordered_extent(ordered
);
9405 btrfs_qgroup_check_reserved_leak(inode
);
9406 inode_tree_del(inode
);
9407 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
9409 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
9412 int btrfs_drop_inode(struct inode
*inode
)
9414 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9419 /* the snap/subvol tree is on deleting */
9420 if (btrfs_root_refs(&root
->root_item
) == 0)
9423 return generic_drop_inode(inode
);
9426 static void init_once(void *foo
)
9428 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
9430 inode_init_once(&ei
->vfs_inode
);
9433 void btrfs_destroy_cachep(void)
9436 * Make sure all delayed rcu free inodes are flushed before we
9440 kmem_cache_destroy(btrfs_inode_cachep
);
9441 kmem_cache_destroy(btrfs_trans_handle_cachep
);
9442 kmem_cache_destroy(btrfs_transaction_cachep
);
9443 kmem_cache_destroy(btrfs_path_cachep
);
9444 kmem_cache_destroy(btrfs_free_space_cachep
);
9447 int btrfs_init_cachep(void)
9449 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
9450 sizeof(struct btrfs_inode
), 0,
9451 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
| SLAB_ACCOUNT
,
9453 if (!btrfs_inode_cachep
)
9456 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
9457 sizeof(struct btrfs_trans_handle
), 0,
9458 SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
9459 if (!btrfs_trans_handle_cachep
)
9462 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
9463 sizeof(struct btrfs_transaction
), 0,
9464 SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
9465 if (!btrfs_transaction_cachep
)
9468 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
9469 sizeof(struct btrfs_path
), 0,
9470 SLAB_MEM_SPREAD
, NULL
);
9471 if (!btrfs_path_cachep
)
9474 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
9475 sizeof(struct btrfs_free_space
), 0,
9476 SLAB_MEM_SPREAD
, NULL
);
9477 if (!btrfs_free_space_cachep
)
9482 btrfs_destroy_cachep();
9486 static int btrfs_getattr(struct vfsmount
*mnt
,
9487 struct dentry
*dentry
, struct kstat
*stat
)
9490 struct inode
*inode
= d_inode(dentry
);
9491 u32 blocksize
= inode
->i_sb
->s_blocksize
;
9493 generic_fillattr(inode
, stat
);
9494 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
9496 spin_lock(&BTRFS_I(inode
)->lock
);
9497 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
9498 spin_unlock(&BTRFS_I(inode
)->lock
);
9499 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
9500 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
9504 static int btrfs_rename_exchange(struct inode
*old_dir
,
9505 struct dentry
*old_dentry
,
9506 struct inode
*new_dir
,
9507 struct dentry
*new_dentry
)
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(&root
->fs_info
->subvol_sem
);
9532 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9533 down_read(&dest
->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(root
->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(dest
->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
, root
->fs_info
->generation
) ||
9697 btrfs_inode_in_log(new_dir
, root
->fs_info
->generation
) ||
9698 btrfs_inode_in_log(old_inode
, root
->fs_info
->generation
) ||
9700 btrfs_inode_in_log(new_inode
, root
->fs_info
->generation
)))
9701 btrfs_set_log_full_commit(root
->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
, root
);
9714 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9715 up_read(&dest
->fs_info
->subvol_sem
);
9716 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9717 up_read(&root
->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_trans_handle
*trans
;
9778 unsigned int trans_num_items
;
9779 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
9780 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
9781 struct inode
*new_inode
= d_inode(new_dentry
);
9782 struct inode
*old_inode
= d_inode(old_dentry
);
9786 u64 old_ino
= btrfs_ino(old_inode
);
9787 bool log_pinned
= false;
9789 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
9792 /* we only allow rename subvolume link between subvolumes */
9793 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
9796 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
9797 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
9800 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
9801 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
9805 /* check for collisions, even if the name isn't there */
9806 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
9807 new_dentry
->d_name
.name
,
9808 new_dentry
->d_name
.len
);
9811 if (ret
== -EEXIST
) {
9813 * eexist without a new_inode */
9814 if (WARN_ON(!new_inode
)) {
9818 /* maybe -EOVERFLOW */
9825 * we're using rename to replace one file with another. Start IO on it
9826 * now so we don't add too much work to the end of the transaction
9828 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
)
9829 filemap_flush(old_inode
->i_mapping
);
9831 /* close the racy window with snapshot create/destroy ioctl */
9832 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9833 down_read(&root
->fs_info
->subvol_sem
);
9835 * We want to reserve the absolute worst case amount of items. So if
9836 * both inodes are subvols and we need to unlink them then that would
9837 * require 4 item modifications, but if they are both normal inodes it
9838 * would require 5 item modifications, so we'll assume they are normal
9839 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9840 * should cover the worst case number of items we'll modify.
9841 * If our rename has the whiteout flag, we need more 5 units for the
9842 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9843 * when selinux is enabled).
9845 trans_num_items
= 11;
9846 if (flags
& RENAME_WHITEOUT
)
9847 trans_num_items
+= 5;
9848 trans
= btrfs_start_transaction(root
, trans_num_items
);
9849 if (IS_ERR(trans
)) {
9850 ret
= PTR_ERR(trans
);
9855 btrfs_record_root_in_trans(trans
, dest
);
9857 ret
= btrfs_set_inode_index(new_dir
, &index
);
9861 BTRFS_I(old_inode
)->dir_index
= 0ULL;
9862 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9863 /* force full log commit if subvolume involved. */
9864 btrfs_set_log_full_commit(root
->fs_info
, trans
);
9866 btrfs_pin_log_trans(root
);
9868 ret
= btrfs_insert_inode_ref(trans
, dest
,
9869 new_dentry
->d_name
.name
,
9870 new_dentry
->d_name
.len
,
9872 btrfs_ino(new_dir
), index
);
9877 inode_inc_iversion(old_dir
);
9878 inode_inc_iversion(new_dir
);
9879 inode_inc_iversion(old_inode
);
9880 old_dir
->i_ctime
= old_dir
->i_mtime
=
9881 new_dir
->i_ctime
= new_dir
->i_mtime
=
9882 old_inode
->i_ctime
= current_time(old_dir
);
9884 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
9885 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
9887 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9888 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
9889 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
9890 old_dentry
->d_name
.name
,
9891 old_dentry
->d_name
.len
);
9893 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
9894 d_inode(old_dentry
),
9895 old_dentry
->d_name
.name
,
9896 old_dentry
->d_name
.len
);
9898 ret
= btrfs_update_inode(trans
, root
, old_inode
);
9901 btrfs_abort_transaction(trans
, ret
);
9906 inode_inc_iversion(new_inode
);
9907 new_inode
->i_ctime
= current_time(new_inode
);
9908 if (unlikely(btrfs_ino(new_inode
) ==
9909 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
9910 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
9911 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
9913 new_dentry
->d_name
.name
,
9914 new_dentry
->d_name
.len
);
9915 BUG_ON(new_inode
->i_nlink
== 0);
9917 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
9918 d_inode(new_dentry
),
9919 new_dentry
->d_name
.name
,
9920 new_dentry
->d_name
.len
);
9922 if (!ret
&& new_inode
->i_nlink
== 0)
9923 ret
= btrfs_orphan_add(trans
, d_inode(new_dentry
));
9925 btrfs_abort_transaction(trans
, ret
);
9930 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
9931 new_dentry
->d_name
.name
,
9932 new_dentry
->d_name
.len
, 0, index
);
9934 btrfs_abort_transaction(trans
, ret
);
9938 if (old_inode
->i_nlink
== 1)
9939 BTRFS_I(old_inode
)->dir_index
= index
;
9942 struct dentry
*parent
= new_dentry
->d_parent
;
9944 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
9945 btrfs_end_log_trans(root
);
9949 if (flags
& RENAME_WHITEOUT
) {
9950 ret
= btrfs_whiteout_for_rename(trans
, root
, old_dir
,
9954 btrfs_abort_transaction(trans
, ret
);
9960 * If we have pinned the log and an error happened, we unpin tasks
9961 * trying to sync the log and force them to fallback to a transaction
9962 * commit if the log currently contains any of the inodes involved in
9963 * this rename operation (to ensure we do not persist a log with an
9964 * inconsistent state for any of these inodes or leading to any
9965 * inconsistencies when replayed). If the transaction was aborted, the
9966 * abortion reason is propagated to userspace when attempting to commit
9967 * the transaction. If the log does not contain any of these inodes, we
9968 * allow the tasks to sync it.
9970 if (ret
&& log_pinned
) {
9971 if (btrfs_inode_in_log(old_dir
, root
->fs_info
->generation
) ||
9972 btrfs_inode_in_log(new_dir
, root
->fs_info
->generation
) ||
9973 btrfs_inode_in_log(old_inode
, root
->fs_info
->generation
) ||
9975 btrfs_inode_in_log(new_inode
, root
->fs_info
->generation
)))
9976 btrfs_set_log_full_commit(root
->fs_info
, trans
);
9978 btrfs_end_log_trans(root
);
9981 btrfs_end_transaction(trans
, root
);
9983 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9984 up_read(&root
->fs_info
->subvol_sem
);
9989 static int btrfs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
9990 struct inode
*new_dir
, struct dentry
*new_dentry
,
9993 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
9996 if (flags
& RENAME_EXCHANGE
)
9997 return btrfs_rename_exchange(old_dir
, old_dentry
, new_dir
,
10000 return btrfs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
10003 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
10005 struct btrfs_delalloc_work
*delalloc_work
;
10006 struct inode
*inode
;
10008 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
10010 inode
= delalloc_work
->inode
;
10011 filemap_flush(inode
->i_mapping
);
10012 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
10013 &BTRFS_I(inode
)->runtime_flags
))
10014 filemap_flush(inode
->i_mapping
);
10016 if (delalloc_work
->delay_iput
)
10017 btrfs_add_delayed_iput(inode
);
10020 complete(&delalloc_work
->completion
);
10023 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
10026 struct btrfs_delalloc_work
*work
;
10028 work
= kmalloc(sizeof(*work
), GFP_NOFS
);
10032 init_completion(&work
->completion
);
10033 INIT_LIST_HEAD(&work
->list
);
10034 work
->inode
= inode
;
10035 work
->delay_iput
= delay_iput
;
10036 WARN_ON_ONCE(!inode
);
10037 btrfs_init_work(&work
->work
, btrfs_flush_delalloc_helper
,
10038 btrfs_run_delalloc_work
, NULL
, NULL
);
10043 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
10045 wait_for_completion(&work
->completion
);
10050 * some fairly slow code that needs optimization. This walks the list
10051 * of all the inodes with pending delalloc and forces them to disk.
10053 static int __start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
,
10056 struct btrfs_inode
*binode
;
10057 struct inode
*inode
;
10058 struct btrfs_delalloc_work
*work
, *next
;
10059 struct list_head works
;
10060 struct list_head splice
;
10063 INIT_LIST_HEAD(&works
);
10064 INIT_LIST_HEAD(&splice
);
10066 mutex_lock(&root
->delalloc_mutex
);
10067 spin_lock(&root
->delalloc_lock
);
10068 list_splice_init(&root
->delalloc_inodes
, &splice
);
10069 while (!list_empty(&splice
)) {
10070 binode
= list_entry(splice
.next
, struct btrfs_inode
,
10073 list_move_tail(&binode
->delalloc_inodes
,
10074 &root
->delalloc_inodes
);
10075 inode
= igrab(&binode
->vfs_inode
);
10077 cond_resched_lock(&root
->delalloc_lock
);
10080 spin_unlock(&root
->delalloc_lock
);
10082 work
= btrfs_alloc_delalloc_work(inode
, delay_iput
);
10085 btrfs_add_delayed_iput(inode
);
10091 list_add_tail(&work
->list
, &works
);
10092 btrfs_queue_work(root
->fs_info
->flush_workers
,
10095 if (nr
!= -1 && ret
>= nr
)
10098 spin_lock(&root
->delalloc_lock
);
10100 spin_unlock(&root
->delalloc_lock
);
10103 list_for_each_entry_safe(work
, next
, &works
, list
) {
10104 list_del_init(&work
->list
);
10105 btrfs_wait_and_free_delalloc_work(work
);
10108 if (!list_empty_careful(&splice
)) {
10109 spin_lock(&root
->delalloc_lock
);
10110 list_splice_tail(&splice
, &root
->delalloc_inodes
);
10111 spin_unlock(&root
->delalloc_lock
);
10113 mutex_unlock(&root
->delalloc_mutex
);
10117 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
10121 if (test_bit(BTRFS_FS_STATE_ERROR
, &root
->fs_info
->fs_state
))
10124 ret
= __start_delalloc_inodes(root
, delay_iput
, -1);
10128 * the filemap_flush will queue IO into the worker threads, but
10129 * we have to make sure the IO is actually started and that
10130 * ordered extents get created before we return
10132 atomic_inc(&root
->fs_info
->async_submit_draining
);
10133 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
10134 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
10135 wait_event(root
->fs_info
->async_submit_wait
,
10136 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
10137 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
10139 atomic_dec(&root
->fs_info
->async_submit_draining
);
10143 int btrfs_start_delalloc_roots(struct btrfs_fs_info
*fs_info
, int delay_iput
,
10146 struct btrfs_root
*root
;
10147 struct list_head splice
;
10150 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
10153 INIT_LIST_HEAD(&splice
);
10155 mutex_lock(&fs_info
->delalloc_root_mutex
);
10156 spin_lock(&fs_info
->delalloc_root_lock
);
10157 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
10158 while (!list_empty(&splice
) && nr
) {
10159 root
= list_first_entry(&splice
, struct btrfs_root
,
10161 root
= btrfs_grab_fs_root(root
);
10163 list_move_tail(&root
->delalloc_root
,
10164 &fs_info
->delalloc_roots
);
10165 spin_unlock(&fs_info
->delalloc_root_lock
);
10167 ret
= __start_delalloc_inodes(root
, delay_iput
, nr
);
10168 btrfs_put_fs_root(root
);
10176 spin_lock(&fs_info
->delalloc_root_lock
);
10178 spin_unlock(&fs_info
->delalloc_root_lock
);
10181 atomic_inc(&fs_info
->async_submit_draining
);
10182 while (atomic_read(&fs_info
->nr_async_submits
) ||
10183 atomic_read(&fs_info
->async_delalloc_pages
)) {
10184 wait_event(fs_info
->async_submit_wait
,
10185 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
10186 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
10188 atomic_dec(&fs_info
->async_submit_draining
);
10190 if (!list_empty_careful(&splice
)) {
10191 spin_lock(&fs_info
->delalloc_root_lock
);
10192 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
10193 spin_unlock(&fs_info
->delalloc_root_lock
);
10195 mutex_unlock(&fs_info
->delalloc_root_mutex
);
10199 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
10200 const char *symname
)
10202 struct btrfs_trans_handle
*trans
;
10203 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
10204 struct btrfs_path
*path
;
10205 struct btrfs_key key
;
10206 struct inode
*inode
= NULL
;
10208 int drop_inode
= 0;
10214 struct btrfs_file_extent_item
*ei
;
10215 struct extent_buffer
*leaf
;
10217 name_len
= strlen(symname
);
10218 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
10219 return -ENAMETOOLONG
;
10222 * 2 items for inode item and ref
10223 * 2 items for dir items
10224 * 1 item for updating parent inode item
10225 * 1 item for the inline extent item
10226 * 1 item for xattr if selinux is on
10228 trans
= btrfs_start_transaction(root
, 7);
10230 return PTR_ERR(trans
);
10232 err
= btrfs_find_free_ino(root
, &objectid
);
10236 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
10237 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
10238 S_IFLNK
|S_IRWXUGO
, &index
);
10239 if (IS_ERR(inode
)) {
10240 err
= PTR_ERR(inode
);
10245 * If the active LSM wants to access the inode during
10246 * d_instantiate it needs these. Smack checks to see
10247 * if the filesystem supports xattrs by looking at the
10250 inode
->i_fop
= &btrfs_file_operations
;
10251 inode
->i_op
= &btrfs_file_inode_operations
;
10252 inode
->i_mapping
->a_ops
= &btrfs_aops
;
10253 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
10255 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
10257 goto out_unlock_inode
;
10259 path
= btrfs_alloc_path();
10262 goto out_unlock_inode
;
10264 key
.objectid
= btrfs_ino(inode
);
10266 key
.type
= BTRFS_EXTENT_DATA_KEY
;
10267 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
10268 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
10271 btrfs_free_path(path
);
10272 goto out_unlock_inode
;
10274 leaf
= path
->nodes
[0];
10275 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
10276 struct btrfs_file_extent_item
);
10277 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
10278 btrfs_set_file_extent_type(leaf
, ei
,
10279 BTRFS_FILE_EXTENT_INLINE
);
10280 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
10281 btrfs_set_file_extent_compression(leaf
, ei
, 0);
10282 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
10283 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
10285 ptr
= btrfs_file_extent_inline_start(ei
);
10286 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
10287 btrfs_mark_buffer_dirty(leaf
);
10288 btrfs_free_path(path
);
10290 inode
->i_op
= &btrfs_symlink_inode_operations
;
10291 inode_nohighmem(inode
);
10292 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
10293 inode_set_bytes(inode
, name_len
);
10294 btrfs_i_size_write(inode
, name_len
);
10295 err
= btrfs_update_inode(trans
, root
, inode
);
10297 * Last step, add directory indexes for our symlink inode. This is the
10298 * last step to avoid extra cleanup of these indexes if an error happens
10302 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
10305 goto out_unlock_inode
;
10308 unlock_new_inode(inode
);
10309 d_instantiate(dentry
, inode
);
10312 btrfs_end_transaction(trans
, root
);
10314 inode_dec_link_count(inode
);
10317 btrfs_btree_balance_dirty(root
);
10322 unlock_new_inode(inode
);
10326 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
10327 u64 start
, u64 num_bytes
, u64 min_size
,
10328 loff_t actual_len
, u64
*alloc_hint
,
10329 struct btrfs_trans_handle
*trans
)
10331 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
10332 struct extent_map
*em
;
10333 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
10334 struct btrfs_key ins
;
10335 u64 cur_offset
= start
;
10338 u64 last_alloc
= (u64
)-1;
10340 bool own_trans
= true;
10341 u64 end
= start
+ num_bytes
- 1;
10345 while (num_bytes
> 0) {
10347 trans
= btrfs_start_transaction(root
, 3);
10348 if (IS_ERR(trans
)) {
10349 ret
= PTR_ERR(trans
);
10354 cur_bytes
= min_t(u64
, num_bytes
, SZ_256M
);
10355 cur_bytes
= max(cur_bytes
, min_size
);
10357 * If we are severely fragmented we could end up with really
10358 * small allocations, so if the allocator is returning small
10359 * chunks lets make its job easier by only searching for those
10362 cur_bytes
= min(cur_bytes
, last_alloc
);
10363 ret
= btrfs_reserve_extent(root
, cur_bytes
, cur_bytes
,
10364 min_size
, 0, *alloc_hint
, &ins
, 1, 0);
10367 btrfs_end_transaction(trans
, root
);
10370 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
10372 last_alloc
= ins
.offset
;
10373 ret
= insert_reserved_file_extent(trans
, inode
,
10374 cur_offset
, ins
.objectid
,
10375 ins
.offset
, ins
.offset
,
10376 ins
.offset
, 0, 0, 0,
10377 BTRFS_FILE_EXTENT_PREALLOC
);
10379 btrfs_free_reserved_extent(root
, ins
.objectid
,
10381 btrfs_abort_transaction(trans
, ret
);
10383 btrfs_end_transaction(trans
, root
);
10387 btrfs_drop_extent_cache(inode
, cur_offset
,
10388 cur_offset
+ ins
.offset
-1, 0);
10390 em
= alloc_extent_map();
10392 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
10393 &BTRFS_I(inode
)->runtime_flags
);
10397 em
->start
= cur_offset
;
10398 em
->orig_start
= cur_offset
;
10399 em
->len
= ins
.offset
;
10400 em
->block_start
= ins
.objectid
;
10401 em
->block_len
= ins
.offset
;
10402 em
->orig_block_len
= ins
.offset
;
10403 em
->ram_bytes
= ins
.offset
;
10404 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
10405 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
10406 em
->generation
= trans
->transid
;
10409 write_lock(&em_tree
->lock
);
10410 ret
= add_extent_mapping(em_tree
, em
, 1);
10411 write_unlock(&em_tree
->lock
);
10412 if (ret
!= -EEXIST
)
10414 btrfs_drop_extent_cache(inode
, cur_offset
,
10415 cur_offset
+ ins
.offset
- 1,
10418 free_extent_map(em
);
10420 num_bytes
-= ins
.offset
;
10421 cur_offset
+= ins
.offset
;
10422 *alloc_hint
= ins
.objectid
+ ins
.offset
;
10424 inode_inc_iversion(inode
);
10425 inode
->i_ctime
= current_time(inode
);
10426 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
10427 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
10428 (actual_len
> inode
->i_size
) &&
10429 (cur_offset
> inode
->i_size
)) {
10430 if (cur_offset
> actual_len
)
10431 i_size
= actual_len
;
10433 i_size
= cur_offset
;
10434 i_size_write(inode
, i_size
);
10435 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
10438 ret
= btrfs_update_inode(trans
, root
, inode
);
10441 btrfs_abort_transaction(trans
, ret
);
10443 btrfs_end_transaction(trans
, root
);
10448 btrfs_end_transaction(trans
, root
);
10450 if (cur_offset
< end
)
10451 btrfs_free_reserved_data_space(inode
, cur_offset
,
10452 end
- cur_offset
+ 1);
10456 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
10457 u64 start
, u64 num_bytes
, u64 min_size
,
10458 loff_t actual_len
, u64
*alloc_hint
)
10460 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
10461 min_size
, actual_len
, alloc_hint
,
10465 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
10466 struct btrfs_trans_handle
*trans
, int mode
,
10467 u64 start
, u64 num_bytes
, u64 min_size
,
10468 loff_t actual_len
, u64
*alloc_hint
)
10470 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
10471 min_size
, actual_len
, alloc_hint
, trans
);
10474 static int btrfs_set_page_dirty(struct page
*page
)
10476 return __set_page_dirty_nobuffers(page
);
10479 static int btrfs_permission(struct inode
*inode
, int mask
)
10481 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
10482 umode_t mode
= inode
->i_mode
;
10484 if (mask
& MAY_WRITE
&&
10485 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
10486 if (btrfs_root_readonly(root
))
10488 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
10491 return generic_permission(inode
, mask
);
10494 static int btrfs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
10496 struct btrfs_trans_handle
*trans
;
10497 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
10498 struct inode
*inode
= NULL
;
10504 * 5 units required for adding orphan entry
10506 trans
= btrfs_start_transaction(root
, 5);
10508 return PTR_ERR(trans
);
10510 ret
= btrfs_find_free_ino(root
, &objectid
);
10514 inode
= btrfs_new_inode(trans
, root
, dir
, NULL
, 0,
10515 btrfs_ino(dir
), objectid
, mode
, &index
);
10516 if (IS_ERR(inode
)) {
10517 ret
= PTR_ERR(inode
);
10522 inode
->i_fop
= &btrfs_file_operations
;
10523 inode
->i_op
= &btrfs_file_inode_operations
;
10525 inode
->i_mapping
->a_ops
= &btrfs_aops
;
10526 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
10528 ret
= btrfs_init_inode_security(trans
, inode
, dir
, NULL
);
10532 ret
= btrfs_update_inode(trans
, root
, inode
);
10535 ret
= btrfs_orphan_add(trans
, inode
);
10540 * We set number of links to 0 in btrfs_new_inode(), and here we set
10541 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10544 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10546 set_nlink(inode
, 1);
10547 unlock_new_inode(inode
);
10548 d_tmpfile(dentry
, inode
);
10549 mark_inode_dirty(inode
);
10552 btrfs_end_transaction(trans
, root
);
10555 btrfs_balance_delayed_items(root
);
10556 btrfs_btree_balance_dirty(root
);
10560 unlock_new_inode(inode
);
10565 static const struct inode_operations btrfs_dir_inode_operations
= {
10566 .getattr
= btrfs_getattr
,
10567 .lookup
= btrfs_lookup
,
10568 .create
= btrfs_create
,
10569 .unlink
= btrfs_unlink
,
10570 .link
= btrfs_link
,
10571 .mkdir
= btrfs_mkdir
,
10572 .rmdir
= btrfs_rmdir
,
10573 .rename
= btrfs_rename2
,
10574 .symlink
= btrfs_symlink
,
10575 .setattr
= btrfs_setattr
,
10576 .mknod
= btrfs_mknod
,
10577 .listxattr
= btrfs_listxattr
,
10578 .permission
= btrfs_permission
,
10579 .get_acl
= btrfs_get_acl
,
10580 .set_acl
= btrfs_set_acl
,
10581 .update_time
= btrfs_update_time
,
10582 .tmpfile
= btrfs_tmpfile
,
10584 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
10585 .lookup
= btrfs_lookup
,
10586 .permission
= btrfs_permission
,
10587 .get_acl
= btrfs_get_acl
,
10588 .set_acl
= btrfs_set_acl
,
10589 .update_time
= btrfs_update_time
,
10592 static const struct file_operations btrfs_dir_file_operations
= {
10593 .llseek
= generic_file_llseek
,
10594 .read
= generic_read_dir
,
10595 .iterate_shared
= btrfs_real_readdir
,
10596 .unlocked_ioctl
= btrfs_ioctl
,
10597 #ifdef CONFIG_COMPAT
10598 .compat_ioctl
= btrfs_compat_ioctl
,
10600 .release
= btrfs_release_file
,
10601 .fsync
= btrfs_sync_file
,
10604 static const struct extent_io_ops btrfs_extent_io_ops
= {
10605 .fill_delalloc
= run_delalloc_range
,
10606 .submit_bio_hook
= btrfs_submit_bio_hook
,
10607 .merge_bio_hook
= btrfs_merge_bio_hook
,
10608 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
10609 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
10610 .writepage_start_hook
= btrfs_writepage_start_hook
,
10611 .set_bit_hook
= btrfs_set_bit_hook
,
10612 .clear_bit_hook
= btrfs_clear_bit_hook
,
10613 .merge_extent_hook
= btrfs_merge_extent_hook
,
10614 .split_extent_hook
= btrfs_split_extent_hook
,
10618 * btrfs doesn't support the bmap operation because swapfiles
10619 * use bmap to make a mapping of extents in the file. They assume
10620 * these extents won't change over the life of the file and they
10621 * use the bmap result to do IO directly to the drive.
10623 * the btrfs bmap call would return logical addresses that aren't
10624 * suitable for IO and they also will change frequently as COW
10625 * operations happen. So, swapfile + btrfs == corruption.
10627 * For now we're avoiding this by dropping bmap.
10629 static const struct address_space_operations btrfs_aops
= {
10630 .readpage
= btrfs_readpage
,
10631 .writepage
= btrfs_writepage
,
10632 .writepages
= btrfs_writepages
,
10633 .readpages
= btrfs_readpages
,
10634 .direct_IO
= btrfs_direct_IO
,
10635 .invalidatepage
= btrfs_invalidatepage
,
10636 .releasepage
= btrfs_releasepage
,
10637 .set_page_dirty
= btrfs_set_page_dirty
,
10638 .error_remove_page
= generic_error_remove_page
,
10641 static const struct address_space_operations btrfs_symlink_aops
= {
10642 .readpage
= btrfs_readpage
,
10643 .writepage
= btrfs_writepage
,
10644 .invalidatepage
= btrfs_invalidatepage
,
10645 .releasepage
= btrfs_releasepage
,
10648 static const struct inode_operations btrfs_file_inode_operations
= {
10649 .getattr
= btrfs_getattr
,
10650 .setattr
= btrfs_setattr
,
10651 .listxattr
= btrfs_listxattr
,
10652 .permission
= btrfs_permission
,
10653 .fiemap
= btrfs_fiemap
,
10654 .get_acl
= btrfs_get_acl
,
10655 .set_acl
= btrfs_set_acl
,
10656 .update_time
= btrfs_update_time
,
10658 static const struct inode_operations btrfs_special_inode_operations
= {
10659 .getattr
= btrfs_getattr
,
10660 .setattr
= btrfs_setattr
,
10661 .permission
= btrfs_permission
,
10662 .listxattr
= btrfs_listxattr
,
10663 .get_acl
= btrfs_get_acl
,
10664 .set_acl
= btrfs_set_acl
,
10665 .update_time
= btrfs_update_time
,
10667 static const struct inode_operations btrfs_symlink_inode_operations
= {
10668 .readlink
= generic_readlink
,
10669 .get_link
= page_get_link
,
10670 .getattr
= btrfs_getattr
,
10671 .setattr
= btrfs_setattr
,
10672 .permission
= btrfs_permission
,
10673 .listxattr
= btrfs_listxattr
,
10674 .update_time
= btrfs_update_time
,
10677 const struct dentry_operations btrfs_dentry_operations
= {
10678 .d_delete
= btrfs_dentry_delete
,
10679 .d_release
= btrfs_dentry_release
,