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
, NULL
,
564 clear_flags
, PAGE_UNLOCK
|
569 btrfs_free_reserved_data_space_noquota(inode
, start
,
577 * we aren't doing an inline extent round the compressed size
578 * up to a block size boundary so the allocator does sane
581 total_compressed
= ALIGN(total_compressed
, blocksize
);
584 * one last check to make sure the compression is really a
585 * win, compare the page count read with the blocks on disk
587 total_in
= ALIGN(total_in
, PAGE_SIZE
);
588 if (total_compressed
>= total_in
) {
591 num_bytes
= total_in
;
595 * The async work queues will take care of doing actual
596 * allocation on disk for these compressed pages, and
597 * will submit them to the elevator.
599 add_async_extent(async_cow
, start
, num_bytes
,
600 total_compressed
, pages
, nr_pages_ret
,
603 if (start
+ num_bytes
< end
) {
614 * the compression code ran but failed to make things smaller,
615 * free any pages it allocated and our page pointer array
617 for (i
= 0; i
< nr_pages_ret
; i
++) {
618 WARN_ON(pages
[i
]->mapping
);
623 total_compressed
= 0;
626 /* flag the file so we don't compress in the future */
627 if (!btrfs_test_opt(root
->fs_info
, FORCE_COMPRESS
) &&
628 !(BTRFS_I(inode
)->force_compress
)) {
629 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
632 cleanup_and_bail_uncompressed
:
634 * No compression, but we still need to write the pages in the file
635 * we've been given so far. redirty the locked page if it corresponds
636 * to our extent and set things up for the async work queue to run
637 * cow_file_range to do the normal delalloc dance.
639 if (page_offset(locked_page
) >= start
&&
640 page_offset(locked_page
) <= end
)
641 __set_page_dirty_nobuffers(locked_page
);
642 /* unlocked later on in the async handlers */
645 extent_range_redirty_for_io(inode
, start
, end
);
646 add_async_extent(async_cow
, start
, end
- start
+ 1, 0, NULL
, 0,
647 BTRFS_COMPRESS_NONE
);
653 for (i
= 0; i
< nr_pages_ret
; i
++) {
654 WARN_ON(pages
[i
]->mapping
);
660 static void free_async_extent_pages(struct async_extent
*async_extent
)
664 if (!async_extent
->pages
)
667 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
668 WARN_ON(async_extent
->pages
[i
]->mapping
);
669 put_page(async_extent
->pages
[i
]);
671 kfree(async_extent
->pages
);
672 async_extent
->nr_pages
= 0;
673 async_extent
->pages
= NULL
;
677 * phase two of compressed writeback. This is the ordered portion
678 * of the code, which only gets called in the order the work was
679 * queued. We walk all the async extents created by compress_file_range
680 * and send them down to the disk.
682 static noinline
void submit_compressed_extents(struct inode
*inode
,
683 struct async_cow
*async_cow
)
685 struct async_extent
*async_extent
;
687 struct btrfs_key ins
;
688 struct extent_map
*em
;
689 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
690 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
691 struct extent_io_tree
*io_tree
;
695 while (!list_empty(&async_cow
->extents
)) {
696 async_extent
= list_entry(async_cow
->extents
.next
,
697 struct async_extent
, list
);
698 list_del(&async_extent
->list
);
700 io_tree
= &BTRFS_I(inode
)->io_tree
;
703 /* did the compression code fall back to uncompressed IO? */
704 if (!async_extent
->pages
) {
705 int page_started
= 0;
706 unsigned long nr_written
= 0;
708 lock_extent(io_tree
, async_extent
->start
,
709 async_extent
->start
+
710 async_extent
->ram_size
- 1);
712 /* allocate blocks */
713 ret
= cow_file_range(inode
, async_cow
->locked_page
,
715 async_extent
->start
+
716 async_extent
->ram_size
- 1,
717 async_extent
->start
+
718 async_extent
->ram_size
- 1,
719 &page_started
, &nr_written
, 0,
725 * if page_started, cow_file_range inserted an
726 * inline extent and took care of all the unlocking
727 * and IO for us. Otherwise, we need to submit
728 * all those pages down to the drive.
730 if (!page_started
&& !ret
)
731 extent_write_locked_range(io_tree
,
732 inode
, async_extent
->start
,
733 async_extent
->start
+
734 async_extent
->ram_size
- 1,
738 unlock_page(async_cow
->locked_page
);
744 lock_extent(io_tree
, async_extent
->start
,
745 async_extent
->start
+ async_extent
->ram_size
- 1);
747 ret
= btrfs_reserve_extent(root
, async_extent
->ram_size
,
748 async_extent
->compressed_size
,
749 async_extent
->compressed_size
,
750 0, alloc_hint
, &ins
, 1, 1);
752 free_async_extent_pages(async_extent
);
754 if (ret
== -ENOSPC
) {
755 unlock_extent(io_tree
, async_extent
->start
,
756 async_extent
->start
+
757 async_extent
->ram_size
- 1);
760 * we need to redirty the pages if we decide to
761 * fallback to uncompressed IO, otherwise we
762 * will not submit these pages down to lower
765 extent_range_redirty_for_io(inode
,
767 async_extent
->start
+
768 async_extent
->ram_size
- 1);
775 * here we're doing allocation and writeback of the
778 btrfs_drop_extent_cache(inode
, async_extent
->start
,
779 async_extent
->start
+
780 async_extent
->ram_size
- 1, 0);
782 em
= alloc_extent_map();
785 goto out_free_reserve
;
787 em
->start
= async_extent
->start
;
788 em
->len
= async_extent
->ram_size
;
789 em
->orig_start
= em
->start
;
790 em
->mod_start
= em
->start
;
791 em
->mod_len
= em
->len
;
793 em
->block_start
= ins
.objectid
;
794 em
->block_len
= ins
.offset
;
795 em
->orig_block_len
= ins
.offset
;
796 em
->ram_bytes
= async_extent
->ram_size
;
797 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
798 em
->compress_type
= async_extent
->compress_type
;
799 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
800 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
804 write_lock(&em_tree
->lock
);
805 ret
= add_extent_mapping(em_tree
, em
, 1);
806 write_unlock(&em_tree
->lock
);
807 if (ret
!= -EEXIST
) {
811 btrfs_drop_extent_cache(inode
, async_extent
->start
,
812 async_extent
->start
+
813 async_extent
->ram_size
- 1, 0);
817 goto out_free_reserve
;
819 ret
= btrfs_add_ordered_extent_compress(inode
,
822 async_extent
->ram_size
,
824 BTRFS_ORDERED_COMPRESSED
,
825 async_extent
->compress_type
);
827 btrfs_drop_extent_cache(inode
, async_extent
->start
,
828 async_extent
->start
+
829 async_extent
->ram_size
- 1, 0);
830 goto out_free_reserve
;
832 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
835 * clear dirty, set writeback and unlock the pages.
837 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
838 async_extent
->start
+
839 async_extent
->ram_size
- 1,
840 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
,
841 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
843 ret
= btrfs_submit_compressed_write(inode
,
845 async_extent
->ram_size
,
847 ins
.offset
, async_extent
->pages
,
848 async_extent
->nr_pages
);
850 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
851 struct page
*p
= async_extent
->pages
[0];
852 const u64 start
= async_extent
->start
;
853 const u64 end
= start
+ async_extent
->ram_size
- 1;
855 p
->mapping
= inode
->i_mapping
;
856 tree
->ops
->writepage_end_io_hook(p
, start
, end
,
859 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
, 0,
862 free_async_extent_pages(async_extent
);
864 alloc_hint
= ins
.objectid
+ ins
.offset
;
870 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
871 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
873 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
874 async_extent
->start
+
875 async_extent
->ram_size
- 1,
876 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
|
877 EXTENT_DEFRAG
| EXTENT_DO_ACCOUNTING
,
878 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
879 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
|
881 free_async_extent_pages(async_extent
);
886 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
889 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
890 struct extent_map
*em
;
893 read_lock(&em_tree
->lock
);
894 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
897 * if block start isn't an actual block number then find the
898 * first block in this inode and use that as a hint. If that
899 * block is also bogus then just don't worry about it.
901 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
903 em
= search_extent_mapping(em_tree
, 0, 0);
904 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
905 alloc_hint
= em
->block_start
;
909 alloc_hint
= em
->block_start
;
913 read_unlock(&em_tree
->lock
);
919 * when extent_io.c finds a delayed allocation range in the file,
920 * the call backs end up in this code. The basic idea is to
921 * allocate extents on disk for the range, and create ordered data structs
922 * in ram to track those extents.
924 * locked_page is the page that writepage had locked already. We use
925 * it to make sure we don't do extra locks or unlocks.
927 * *page_started is set to one if we unlock locked_page and do everything
928 * required to start IO on it. It may be clean and already done with
931 static noinline
int cow_file_range(struct inode
*inode
,
932 struct page
*locked_page
,
933 u64 start
, u64 end
, u64 delalloc_end
,
934 int *page_started
, unsigned long *nr_written
,
935 int unlock
, struct btrfs_dedupe_hash
*hash
)
937 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
940 unsigned long ram_size
;
943 u64 blocksize
= root
->sectorsize
;
944 struct btrfs_key ins
;
945 struct extent_map
*em
;
946 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
949 if (btrfs_is_free_space_inode(inode
)) {
955 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
956 num_bytes
= max(blocksize
, num_bytes
);
957 disk_num_bytes
= num_bytes
;
959 /* if this is a small write inside eof, kick off defrag */
960 if (num_bytes
< SZ_64K
&&
961 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
962 btrfs_add_inode_defrag(NULL
, inode
);
965 /* lets try to make an inline extent */
966 ret
= cow_file_range_inline(root
, inode
, start
, end
, 0, 0,
969 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
,
970 EXTENT_LOCKED
| EXTENT_DELALLOC
|
971 EXTENT_DEFRAG
, PAGE_UNLOCK
|
972 PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
974 btrfs_free_reserved_data_space_noquota(inode
, start
,
976 *nr_written
= *nr_written
+
977 (end
- start
+ PAGE_SIZE
) / PAGE_SIZE
;
980 } else if (ret
< 0) {
985 BUG_ON(disk_num_bytes
>
986 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
988 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
989 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
991 while (disk_num_bytes
> 0) {
994 cur_alloc_size
= disk_num_bytes
;
995 ret
= btrfs_reserve_extent(root
, cur_alloc_size
, cur_alloc_size
,
996 root
->sectorsize
, 0, alloc_hint
,
1001 em
= alloc_extent_map();
1007 em
->orig_start
= em
->start
;
1008 ram_size
= ins
.offset
;
1009 em
->len
= ins
.offset
;
1010 em
->mod_start
= em
->start
;
1011 em
->mod_len
= em
->len
;
1013 em
->block_start
= ins
.objectid
;
1014 em
->block_len
= ins
.offset
;
1015 em
->orig_block_len
= ins
.offset
;
1016 em
->ram_bytes
= ram_size
;
1017 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1018 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1019 em
->generation
= -1;
1022 write_lock(&em_tree
->lock
);
1023 ret
= add_extent_mapping(em_tree
, em
, 1);
1024 write_unlock(&em_tree
->lock
);
1025 if (ret
!= -EEXIST
) {
1026 free_extent_map(em
);
1029 btrfs_drop_extent_cache(inode
, start
,
1030 start
+ ram_size
- 1, 0);
1035 cur_alloc_size
= ins
.offset
;
1036 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
1037 ram_size
, cur_alloc_size
, 0);
1039 goto out_drop_extent_cache
;
1041 if (root
->root_key
.objectid
==
1042 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1043 ret
= btrfs_reloc_clone_csums(inode
, start
,
1046 goto out_drop_extent_cache
;
1049 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
1051 if (disk_num_bytes
< cur_alloc_size
)
1054 /* we're not doing compressed IO, don't unlock the first
1055 * page (which the caller expects to stay locked), don't
1056 * clear any dirty bits and don't set any writeback bits
1058 * Do set the Private2 bit so we know this page was properly
1059 * setup for writepage
1061 op
= unlock
? PAGE_UNLOCK
: 0;
1062 op
|= PAGE_SET_PRIVATE2
;
1064 extent_clear_unlock_delalloc(inode
, start
,
1065 start
+ ram_size
- 1, locked_page
,
1066 EXTENT_LOCKED
| EXTENT_DELALLOC
,
1068 disk_num_bytes
-= cur_alloc_size
;
1069 num_bytes
-= cur_alloc_size
;
1070 alloc_hint
= ins
.objectid
+ ins
.offset
;
1071 start
+= cur_alloc_size
;
1076 out_drop_extent_cache
:
1077 btrfs_drop_extent_cache(inode
, start
, start
+ ram_size
- 1, 0);
1079 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
1080 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
1082 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1083 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
1084 EXTENT_DELALLOC
| EXTENT_DEFRAG
,
1085 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
1086 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
);
1091 * work queue call back to started compression on a file and pages
1093 static noinline
void async_cow_start(struct btrfs_work
*work
)
1095 struct async_cow
*async_cow
;
1097 async_cow
= container_of(work
, struct async_cow
, work
);
1099 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1100 async_cow
->start
, async_cow
->end
, async_cow
,
1102 if (num_added
== 0) {
1103 btrfs_add_delayed_iput(async_cow
->inode
);
1104 async_cow
->inode
= NULL
;
1109 * work queue call back to submit previously compressed pages
1111 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1113 struct async_cow
*async_cow
;
1114 struct btrfs_root
*root
;
1115 unsigned long nr_pages
;
1117 async_cow
= container_of(work
, struct async_cow
, work
);
1119 root
= async_cow
->root
;
1120 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_SIZE
) >>
1124 * atomic_sub_return implies a barrier for waitqueue_active
1126 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1128 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1129 wake_up(&root
->fs_info
->async_submit_wait
);
1131 if (async_cow
->inode
)
1132 submit_compressed_extents(async_cow
->inode
, async_cow
);
1135 static noinline
void async_cow_free(struct btrfs_work
*work
)
1137 struct async_cow
*async_cow
;
1138 async_cow
= container_of(work
, struct async_cow
, work
);
1139 if (async_cow
->inode
)
1140 btrfs_add_delayed_iput(async_cow
->inode
);
1144 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1145 u64 start
, u64 end
, int *page_started
,
1146 unsigned long *nr_written
)
1148 struct async_cow
*async_cow
;
1149 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1150 unsigned long nr_pages
;
1152 int limit
= 10 * SZ_1M
;
1154 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1155 1, 0, NULL
, GFP_NOFS
);
1156 while (start
< end
) {
1157 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1158 BUG_ON(!async_cow
); /* -ENOMEM */
1159 async_cow
->inode
= igrab(inode
);
1160 async_cow
->root
= root
;
1161 async_cow
->locked_page
= locked_page
;
1162 async_cow
->start
= start
;
1164 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
&&
1165 !btrfs_test_opt(root
->fs_info
, FORCE_COMPRESS
))
1168 cur_end
= min(end
, start
+ SZ_512K
- 1);
1170 async_cow
->end
= cur_end
;
1171 INIT_LIST_HEAD(&async_cow
->extents
);
1173 btrfs_init_work(&async_cow
->work
,
1174 btrfs_delalloc_helper
,
1175 async_cow_start
, async_cow_submit
,
1178 nr_pages
= (cur_end
- start
+ PAGE_SIZE
) >>
1180 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1182 btrfs_queue_work(root
->fs_info
->delalloc_workers
,
1185 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1186 wait_event(root
->fs_info
->async_submit_wait
,
1187 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1191 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1192 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1193 wait_event(root
->fs_info
->async_submit_wait
,
1194 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1198 *nr_written
+= nr_pages
;
1199 start
= cur_end
+ 1;
1205 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1206 u64 bytenr
, u64 num_bytes
)
1209 struct btrfs_ordered_sum
*sums
;
1212 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1213 bytenr
+ num_bytes
- 1, &list
, 0);
1214 if (ret
== 0 && list_empty(&list
))
1217 while (!list_empty(&list
)) {
1218 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1219 list_del(&sums
->list
);
1226 * when nowcow writeback call back. This checks for snapshots or COW copies
1227 * of the extents that exist in the file, and COWs the file as required.
1229 * If no cow copies or snapshots exist, we write directly to the existing
1232 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1233 struct page
*locked_page
,
1234 u64 start
, u64 end
, int *page_started
, int force
,
1235 unsigned long *nr_written
)
1237 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1238 struct btrfs_trans_handle
*trans
;
1239 struct extent_buffer
*leaf
;
1240 struct btrfs_path
*path
;
1241 struct btrfs_file_extent_item
*fi
;
1242 struct btrfs_key found_key
;
1257 u64 ino
= btrfs_ino(inode
);
1259 path
= btrfs_alloc_path();
1261 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1262 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1263 EXTENT_DO_ACCOUNTING
|
1264 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1266 PAGE_SET_WRITEBACK
|
1267 PAGE_END_WRITEBACK
);
1271 nolock
= btrfs_is_free_space_inode(inode
);
1274 trans
= btrfs_join_transaction_nolock(root
);
1276 trans
= btrfs_join_transaction(root
);
1278 if (IS_ERR(trans
)) {
1279 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1280 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1281 EXTENT_DO_ACCOUNTING
|
1282 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1284 PAGE_SET_WRITEBACK
|
1285 PAGE_END_WRITEBACK
);
1286 btrfs_free_path(path
);
1287 return PTR_ERR(trans
);
1290 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1292 cow_start
= (u64
)-1;
1295 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1299 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1300 leaf
= path
->nodes
[0];
1301 btrfs_item_key_to_cpu(leaf
, &found_key
,
1302 path
->slots
[0] - 1);
1303 if (found_key
.objectid
== ino
&&
1304 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1309 leaf
= path
->nodes
[0];
1310 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1311 ret
= btrfs_next_leaf(root
, path
);
1316 leaf
= path
->nodes
[0];
1322 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1324 if (found_key
.objectid
> ino
)
1326 if (WARN_ON_ONCE(found_key
.objectid
< ino
) ||
1327 found_key
.type
< BTRFS_EXTENT_DATA_KEY
) {
1331 if (found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1332 found_key
.offset
> end
)
1335 if (found_key
.offset
> cur_offset
) {
1336 extent_end
= found_key
.offset
;
1341 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1342 struct btrfs_file_extent_item
);
1343 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1345 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1346 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1347 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1348 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1349 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1350 extent_end
= found_key
.offset
+
1351 btrfs_file_extent_num_bytes(leaf
, fi
);
1353 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1354 if (extent_end
<= start
) {
1358 if (disk_bytenr
== 0)
1360 if (btrfs_file_extent_compression(leaf
, fi
) ||
1361 btrfs_file_extent_encryption(leaf
, fi
) ||
1362 btrfs_file_extent_other_encoding(leaf
, fi
))
1364 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1366 if (btrfs_extent_readonly(root
, disk_bytenr
))
1368 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1370 extent_offset
, disk_bytenr
))
1372 disk_bytenr
+= extent_offset
;
1373 disk_bytenr
+= cur_offset
- found_key
.offset
;
1374 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1376 * if there are pending snapshots for this root,
1377 * we fall into common COW way.
1380 err
= btrfs_start_write_no_snapshoting(root
);
1385 * force cow if csum exists in the range.
1386 * this ensure that csum for a given extent are
1387 * either valid or do not exist.
1389 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1391 if (!btrfs_inc_nocow_writers(root
->fs_info
,
1395 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1396 extent_end
= found_key
.offset
+
1397 btrfs_file_extent_inline_len(leaf
,
1398 path
->slots
[0], fi
);
1399 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1404 if (extent_end
<= start
) {
1406 if (!nolock
&& nocow
)
1407 btrfs_end_write_no_snapshoting(root
);
1409 btrfs_dec_nocow_writers(root
->fs_info
,
1414 if (cow_start
== (u64
)-1)
1415 cow_start
= cur_offset
;
1416 cur_offset
= extent_end
;
1417 if (cur_offset
> end
)
1423 btrfs_release_path(path
);
1424 if (cow_start
!= (u64
)-1) {
1425 ret
= cow_file_range(inode
, locked_page
,
1426 cow_start
, found_key
.offset
- 1,
1427 end
, page_started
, nr_written
, 1,
1430 if (!nolock
&& nocow
)
1431 btrfs_end_write_no_snapshoting(root
);
1433 btrfs_dec_nocow_writers(root
->fs_info
,
1437 cow_start
= (u64
)-1;
1440 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1441 struct extent_map
*em
;
1442 struct extent_map_tree
*em_tree
;
1443 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1444 em
= alloc_extent_map();
1445 BUG_ON(!em
); /* -ENOMEM */
1446 em
->start
= cur_offset
;
1447 em
->orig_start
= found_key
.offset
- extent_offset
;
1448 em
->len
= num_bytes
;
1449 em
->block_len
= num_bytes
;
1450 em
->block_start
= disk_bytenr
;
1451 em
->orig_block_len
= disk_num_bytes
;
1452 em
->ram_bytes
= ram_bytes
;
1453 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1454 em
->mod_start
= em
->start
;
1455 em
->mod_len
= em
->len
;
1456 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1457 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1458 em
->generation
= -1;
1460 write_lock(&em_tree
->lock
);
1461 ret
= add_extent_mapping(em_tree
, em
, 1);
1462 write_unlock(&em_tree
->lock
);
1463 if (ret
!= -EEXIST
) {
1464 free_extent_map(em
);
1467 btrfs_drop_extent_cache(inode
, em
->start
,
1468 em
->start
+ em
->len
- 1, 0);
1470 type
= BTRFS_ORDERED_PREALLOC
;
1472 type
= BTRFS_ORDERED_NOCOW
;
1475 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1476 num_bytes
, num_bytes
, type
);
1478 btrfs_dec_nocow_writers(root
->fs_info
, disk_bytenr
);
1479 BUG_ON(ret
); /* -ENOMEM */
1481 if (root
->root_key
.objectid
==
1482 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1483 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1486 if (!nolock
&& nocow
)
1487 btrfs_end_write_no_snapshoting(root
);
1492 extent_clear_unlock_delalloc(inode
, cur_offset
,
1493 cur_offset
+ num_bytes
- 1,
1494 locked_page
, EXTENT_LOCKED
|
1496 EXTENT_CLEAR_DATA_RESV
,
1497 PAGE_UNLOCK
| PAGE_SET_PRIVATE2
);
1499 if (!nolock
&& nocow
)
1500 btrfs_end_write_no_snapshoting(root
);
1501 cur_offset
= extent_end
;
1502 if (cur_offset
> end
)
1505 btrfs_release_path(path
);
1507 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1508 cow_start
= cur_offset
;
1512 if (cow_start
!= (u64
)-1) {
1513 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
, end
,
1514 page_started
, nr_written
, 1, NULL
);
1520 err
= btrfs_end_transaction(trans
, root
);
1524 if (ret
&& cur_offset
< end
)
1525 extent_clear_unlock_delalloc(inode
, cur_offset
, end
,
1526 locked_page
, EXTENT_LOCKED
|
1527 EXTENT_DELALLOC
| EXTENT_DEFRAG
|
1528 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1530 PAGE_SET_WRITEBACK
|
1531 PAGE_END_WRITEBACK
);
1532 btrfs_free_path(path
);
1536 static inline int need_force_cow(struct inode
*inode
, u64 start
, u64 end
)
1539 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
1540 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
))
1544 * @defrag_bytes is a hint value, no spinlock held here,
1545 * if is not zero, it means the file is defragging.
1546 * Force cow if given extent needs to be defragged.
1548 if (BTRFS_I(inode
)->defrag_bytes
&&
1549 test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
1550 EXTENT_DEFRAG
, 0, NULL
))
1557 * extent_io.c call back to do delayed allocation processing
1559 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1560 u64 start
, u64 end
, int *page_started
,
1561 unsigned long *nr_written
)
1564 int force_cow
= need_force_cow(inode
, start
, end
);
1566 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
&& !force_cow
) {
1567 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1568 page_started
, 1, nr_written
);
1569 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
&& !force_cow
) {
1570 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1571 page_started
, 0, nr_written
);
1572 } else if (!inode_need_compress(inode
)) {
1573 ret
= cow_file_range(inode
, locked_page
, start
, end
, end
,
1574 page_started
, nr_written
, 1, NULL
);
1576 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1577 &BTRFS_I(inode
)->runtime_flags
);
1578 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1579 page_started
, nr_written
);
1584 static void btrfs_split_extent_hook(struct inode
*inode
,
1585 struct extent_state
*orig
, u64 split
)
1589 /* not delalloc, ignore it */
1590 if (!(orig
->state
& EXTENT_DELALLOC
))
1593 size
= orig
->end
- orig
->start
+ 1;
1594 if (size
> BTRFS_MAX_EXTENT_SIZE
) {
1599 * See the explanation in btrfs_merge_extent_hook, the same
1600 * applies here, just in reverse.
1602 new_size
= orig
->end
- split
+ 1;
1603 num_extents
= div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1604 BTRFS_MAX_EXTENT_SIZE
);
1605 new_size
= split
- orig
->start
;
1606 num_extents
+= div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1607 BTRFS_MAX_EXTENT_SIZE
);
1608 if (div64_u64(size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1609 BTRFS_MAX_EXTENT_SIZE
) >= num_extents
)
1613 spin_lock(&BTRFS_I(inode
)->lock
);
1614 BTRFS_I(inode
)->outstanding_extents
++;
1615 spin_unlock(&BTRFS_I(inode
)->lock
);
1619 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1620 * extents so we can keep track of new extents that are just merged onto old
1621 * extents, such as when we are doing sequential writes, so we can properly
1622 * account for the metadata space we'll need.
1624 static void btrfs_merge_extent_hook(struct inode
*inode
,
1625 struct extent_state
*new,
1626 struct extent_state
*other
)
1628 u64 new_size
, old_size
;
1631 /* not delalloc, ignore it */
1632 if (!(other
->state
& EXTENT_DELALLOC
))
1635 if (new->start
> other
->start
)
1636 new_size
= new->end
- other
->start
+ 1;
1638 new_size
= other
->end
- new->start
+ 1;
1640 /* we're not bigger than the max, unreserve the space and go */
1641 if (new_size
<= BTRFS_MAX_EXTENT_SIZE
) {
1642 spin_lock(&BTRFS_I(inode
)->lock
);
1643 BTRFS_I(inode
)->outstanding_extents
--;
1644 spin_unlock(&BTRFS_I(inode
)->lock
);
1649 * We have to add up either side to figure out how many extents were
1650 * accounted for before we merged into one big extent. If the number of
1651 * extents we accounted for is <= the amount we need for the new range
1652 * then we can return, otherwise drop. Think of it like this
1656 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1657 * need 2 outstanding extents, on one side we have 1 and the other side
1658 * we have 1 so they are == and we can return. But in this case
1660 * [MAX_SIZE+4k][MAX_SIZE+4k]
1662 * Each range on their own accounts for 2 extents, but merged together
1663 * they are only 3 extents worth of accounting, so we need to drop in
1666 old_size
= other
->end
- other
->start
+ 1;
1667 num_extents
= div64_u64(old_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1668 BTRFS_MAX_EXTENT_SIZE
);
1669 old_size
= new->end
- new->start
+ 1;
1670 num_extents
+= div64_u64(old_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1671 BTRFS_MAX_EXTENT_SIZE
);
1673 if (div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1674 BTRFS_MAX_EXTENT_SIZE
) >= num_extents
)
1677 spin_lock(&BTRFS_I(inode
)->lock
);
1678 BTRFS_I(inode
)->outstanding_extents
--;
1679 spin_unlock(&BTRFS_I(inode
)->lock
);
1682 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1683 struct inode
*inode
)
1685 spin_lock(&root
->delalloc_lock
);
1686 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1687 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1688 &root
->delalloc_inodes
);
1689 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1690 &BTRFS_I(inode
)->runtime_flags
);
1691 root
->nr_delalloc_inodes
++;
1692 if (root
->nr_delalloc_inodes
== 1) {
1693 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1694 BUG_ON(!list_empty(&root
->delalloc_root
));
1695 list_add_tail(&root
->delalloc_root
,
1696 &root
->fs_info
->delalloc_roots
);
1697 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1700 spin_unlock(&root
->delalloc_lock
);
1703 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1704 struct inode
*inode
)
1706 spin_lock(&root
->delalloc_lock
);
1707 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1708 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1709 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1710 &BTRFS_I(inode
)->runtime_flags
);
1711 root
->nr_delalloc_inodes
--;
1712 if (!root
->nr_delalloc_inodes
) {
1713 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1714 BUG_ON(list_empty(&root
->delalloc_root
));
1715 list_del_init(&root
->delalloc_root
);
1716 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1719 spin_unlock(&root
->delalloc_lock
);
1723 * extent_io.c set_bit_hook, used to track delayed allocation
1724 * bytes in this file, and to maintain the list of inodes that
1725 * have pending delalloc work to be done.
1727 static void btrfs_set_bit_hook(struct inode
*inode
,
1728 struct extent_state
*state
, unsigned *bits
)
1731 if ((*bits
& EXTENT_DEFRAG
) && !(*bits
& EXTENT_DELALLOC
))
1734 * set_bit and clear bit hooks normally require _irqsave/restore
1735 * but in this case, we are only testing for the DELALLOC
1736 * bit, which is only set or cleared with irqs on
1738 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1739 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1740 u64 len
= state
->end
+ 1 - state
->start
;
1741 bool do_list
= !btrfs_is_free_space_inode(inode
);
1743 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1744 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1746 spin_lock(&BTRFS_I(inode
)->lock
);
1747 BTRFS_I(inode
)->outstanding_extents
++;
1748 spin_unlock(&BTRFS_I(inode
)->lock
);
1751 /* For sanity tests */
1752 if (btrfs_is_testing(root
->fs_info
))
1755 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1756 root
->fs_info
->delalloc_batch
);
1757 spin_lock(&BTRFS_I(inode
)->lock
);
1758 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1759 if (*bits
& EXTENT_DEFRAG
)
1760 BTRFS_I(inode
)->defrag_bytes
+= len
;
1761 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1762 &BTRFS_I(inode
)->runtime_flags
))
1763 btrfs_add_delalloc_inodes(root
, inode
);
1764 spin_unlock(&BTRFS_I(inode
)->lock
);
1769 * extent_io.c clear_bit_hook, see set_bit_hook for why
1771 static void btrfs_clear_bit_hook(struct inode
*inode
,
1772 struct extent_state
*state
,
1775 u64 len
= state
->end
+ 1 - state
->start
;
1776 u64 num_extents
= div64_u64(len
+ BTRFS_MAX_EXTENT_SIZE
-1,
1777 BTRFS_MAX_EXTENT_SIZE
);
1779 spin_lock(&BTRFS_I(inode
)->lock
);
1780 if ((state
->state
& EXTENT_DEFRAG
) && (*bits
& EXTENT_DEFRAG
))
1781 BTRFS_I(inode
)->defrag_bytes
-= len
;
1782 spin_unlock(&BTRFS_I(inode
)->lock
);
1785 * set_bit and clear bit hooks normally require _irqsave/restore
1786 * but in this case, we are only testing for the DELALLOC
1787 * bit, which is only set or cleared with irqs on
1789 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1790 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1791 bool do_list
= !btrfs_is_free_space_inode(inode
);
1793 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1794 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1795 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1796 spin_lock(&BTRFS_I(inode
)->lock
);
1797 BTRFS_I(inode
)->outstanding_extents
-= num_extents
;
1798 spin_unlock(&BTRFS_I(inode
)->lock
);
1802 * We don't reserve metadata space for space cache inodes so we
1803 * don't need to call dellalloc_release_metadata if there is an
1806 if (*bits
& EXTENT_DO_ACCOUNTING
&&
1807 root
!= root
->fs_info
->tree_root
)
1808 btrfs_delalloc_release_metadata(inode
, len
);
1810 /* For sanity tests. */
1811 if (btrfs_is_testing(root
->fs_info
))
1814 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1815 && do_list
&& !(state
->state
& EXTENT_NORESERVE
)
1816 && (*bits
& (EXTENT_DO_ACCOUNTING
|
1817 EXTENT_CLEAR_DATA_RESV
)))
1818 btrfs_free_reserved_data_space_noquota(inode
,
1821 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1822 root
->fs_info
->delalloc_batch
);
1823 spin_lock(&BTRFS_I(inode
)->lock
);
1824 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1825 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1826 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1827 &BTRFS_I(inode
)->runtime_flags
))
1828 btrfs_del_delalloc_inode(root
, inode
);
1829 spin_unlock(&BTRFS_I(inode
)->lock
);
1834 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1835 * we don't create bios that span stripes or chunks
1837 * return 1 if page cannot be merged to bio
1838 * return 0 if page can be merged to bio
1839 * return error otherwise
1841 int btrfs_merge_bio_hook(struct page
*page
, unsigned long offset
,
1842 size_t size
, struct bio
*bio
,
1843 unsigned long bio_flags
)
1845 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1846 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
1851 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1854 length
= bio
->bi_iter
.bi_size
;
1855 map_length
= length
;
1856 ret
= btrfs_map_block(root
->fs_info
, bio_op(bio
), logical
,
1857 &map_length
, NULL
, 0);
1860 if (map_length
< length
+ size
)
1866 * in order to insert checksums into the metadata in large chunks,
1867 * we wait until bio submission time. All the pages in the bio are
1868 * checksummed and sums are attached onto the ordered extent record.
1870 * At IO completion time the cums attached on the ordered extent record
1871 * are inserted into the btree
1873 static int __btrfs_submit_bio_start(struct inode
*inode
, struct bio
*bio
,
1874 int mirror_num
, unsigned long bio_flags
,
1877 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1880 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1881 BUG_ON(ret
); /* -ENOMEM */
1886 * in order to insert checksums into the metadata in large chunks,
1887 * we wait until bio submission time. All the pages in the bio are
1888 * checksummed and sums are attached onto the ordered extent record.
1890 * At IO completion time the cums attached on the ordered extent record
1891 * are inserted into the btree
1893 static int __btrfs_submit_bio_done(struct inode
*inode
, struct bio
*bio
,
1894 int mirror_num
, unsigned long bio_flags
,
1897 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1900 ret
= btrfs_map_bio(root
, bio
, mirror_num
, 1);
1902 bio
->bi_error
= ret
;
1909 * extent_io.c submission hook. This does the right thing for csum calculation
1910 * on write, or reading the csums from the tree before a read
1912 static int btrfs_submit_bio_hook(struct inode
*inode
, struct bio
*bio
,
1913 int mirror_num
, unsigned long bio_flags
,
1916 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1917 enum btrfs_wq_endio_type metadata
= BTRFS_WQ_ENDIO_DATA
;
1920 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1922 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1924 if (btrfs_is_free_space_inode(inode
))
1925 metadata
= BTRFS_WQ_ENDIO_FREE_SPACE
;
1927 if (bio_op(bio
) != REQ_OP_WRITE
) {
1928 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1932 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1933 ret
= btrfs_submit_compressed_read(inode
, bio
,
1937 } else if (!skip_sum
) {
1938 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1943 } else if (async
&& !skip_sum
) {
1944 /* csum items have already been cloned */
1945 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1947 /* we're doing a write, do the async checksumming */
1948 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1949 inode
, bio
, mirror_num
,
1950 bio_flags
, bio_offset
,
1951 __btrfs_submit_bio_start
,
1952 __btrfs_submit_bio_done
);
1954 } else if (!skip_sum
) {
1955 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1961 ret
= btrfs_map_bio(root
, bio
, mirror_num
, 0);
1965 bio
->bi_error
= ret
;
1972 * given a list of ordered sums record them in the inode. This happens
1973 * at IO completion time based on sums calculated at bio submission time.
1975 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1976 struct inode
*inode
, u64 file_offset
,
1977 struct list_head
*list
)
1979 struct btrfs_ordered_sum
*sum
;
1981 list_for_each_entry(sum
, list
, list
) {
1982 trans
->adding_csums
= 1;
1983 btrfs_csum_file_blocks(trans
,
1984 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1985 trans
->adding_csums
= 0;
1990 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1991 struct extent_state
**cached_state
)
1993 WARN_ON((end
& (PAGE_SIZE
- 1)) == 0);
1994 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1998 /* see btrfs_writepage_start_hook for details on why this is required */
1999 struct btrfs_writepage_fixup
{
2001 struct btrfs_work work
;
2004 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
2006 struct btrfs_writepage_fixup
*fixup
;
2007 struct btrfs_ordered_extent
*ordered
;
2008 struct extent_state
*cached_state
= NULL
;
2010 struct inode
*inode
;
2015 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
2019 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
2020 ClearPageChecked(page
);
2024 inode
= page
->mapping
->host
;
2025 page_start
= page_offset(page
);
2026 page_end
= page_offset(page
) + PAGE_SIZE
- 1;
2028 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
2031 /* already ordered? We're done */
2032 if (PagePrivate2(page
))
2035 ordered
= btrfs_lookup_ordered_range(inode
, page_start
,
2038 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
2039 page_end
, &cached_state
, GFP_NOFS
);
2041 btrfs_start_ordered_extent(inode
, ordered
, 1);
2042 btrfs_put_ordered_extent(ordered
);
2046 ret
= btrfs_delalloc_reserve_space(inode
, page_start
,
2049 mapping_set_error(page
->mapping
, ret
);
2050 end_extent_writepage(page
, ret
, page_start
, page_end
);
2051 ClearPageChecked(page
);
2055 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
2056 ClearPageChecked(page
);
2057 set_page_dirty(page
);
2059 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
2060 &cached_state
, GFP_NOFS
);
2068 * There are a few paths in the higher layers of the kernel that directly
2069 * set the page dirty bit without asking the filesystem if it is a
2070 * good idea. This causes problems because we want to make sure COW
2071 * properly happens and the data=ordered rules are followed.
2073 * In our case any range that doesn't have the ORDERED bit set
2074 * hasn't been properly setup for IO. We kick off an async process
2075 * to fix it up. The async helper will wait for ordered extents, set
2076 * the delalloc bit and make it safe to write the page.
2078 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
2080 struct inode
*inode
= page
->mapping
->host
;
2081 struct btrfs_writepage_fixup
*fixup
;
2082 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2084 /* this page is properly in the ordered list */
2085 if (TestClearPagePrivate2(page
))
2088 if (PageChecked(page
))
2091 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
2095 SetPageChecked(page
);
2097 btrfs_init_work(&fixup
->work
, btrfs_fixup_helper
,
2098 btrfs_writepage_fixup_worker
, NULL
, NULL
);
2100 btrfs_queue_work(root
->fs_info
->fixup_workers
, &fixup
->work
);
2104 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
2105 struct inode
*inode
, u64 file_pos
,
2106 u64 disk_bytenr
, u64 disk_num_bytes
,
2107 u64 num_bytes
, u64 ram_bytes
,
2108 u8 compression
, u8 encryption
,
2109 u16 other_encoding
, int extent_type
)
2111 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2112 struct btrfs_file_extent_item
*fi
;
2113 struct btrfs_path
*path
;
2114 struct extent_buffer
*leaf
;
2115 struct btrfs_key ins
;
2116 int extent_inserted
= 0;
2119 path
= btrfs_alloc_path();
2124 * we may be replacing one extent in the tree with another.
2125 * The new extent is pinned in the extent map, and we don't want
2126 * to drop it from the cache until it is completely in the btree.
2128 * So, tell btrfs_drop_extents to leave this extent in the cache.
2129 * the caller is expected to unpin it and allow it to be merged
2132 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
, file_pos
,
2133 file_pos
+ num_bytes
, NULL
, 0,
2134 1, sizeof(*fi
), &extent_inserted
);
2138 if (!extent_inserted
) {
2139 ins
.objectid
= btrfs_ino(inode
);
2140 ins
.offset
= file_pos
;
2141 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
2143 path
->leave_spinning
= 1;
2144 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
2149 leaf
= path
->nodes
[0];
2150 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2151 struct btrfs_file_extent_item
);
2152 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
2153 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
2154 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
2155 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
2156 btrfs_set_file_extent_offset(leaf
, fi
, 0);
2157 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
2158 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
2159 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
2160 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
2161 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
2163 btrfs_mark_buffer_dirty(leaf
);
2164 btrfs_release_path(path
);
2166 inode_add_bytes(inode
, num_bytes
);
2168 ins
.objectid
= disk_bytenr
;
2169 ins
.offset
= disk_num_bytes
;
2170 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2171 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
2172 root
->root_key
.objectid
,
2173 btrfs_ino(inode
), file_pos
,
2176 * Release the reserved range from inode dirty range map, as it is
2177 * already moved into delayed_ref_head
2179 btrfs_qgroup_release_data(inode
, file_pos
, ram_bytes
);
2181 btrfs_free_path(path
);
2186 /* snapshot-aware defrag */
2187 struct sa_defrag_extent_backref
{
2188 struct rb_node node
;
2189 struct old_sa_defrag_extent
*old
;
2198 struct old_sa_defrag_extent
{
2199 struct list_head list
;
2200 struct new_sa_defrag_extent
*new;
2209 struct new_sa_defrag_extent
{
2210 struct rb_root root
;
2211 struct list_head head
;
2212 struct btrfs_path
*path
;
2213 struct inode
*inode
;
2221 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
2222 struct sa_defrag_extent_backref
*b2
)
2224 if (b1
->root_id
< b2
->root_id
)
2226 else if (b1
->root_id
> b2
->root_id
)
2229 if (b1
->inum
< b2
->inum
)
2231 else if (b1
->inum
> b2
->inum
)
2234 if (b1
->file_pos
< b2
->file_pos
)
2236 else if (b1
->file_pos
> b2
->file_pos
)
2240 * [------------------------------] ===> (a range of space)
2241 * |<--->| |<---->| =============> (fs/file tree A)
2242 * |<---------------------------->| ===> (fs/file tree B)
2244 * A range of space can refer to two file extents in one tree while
2245 * refer to only one file extent in another tree.
2247 * So we may process a disk offset more than one time(two extents in A)
2248 * and locate at the same extent(one extent in B), then insert two same
2249 * backrefs(both refer to the extent in B).
2254 static void backref_insert(struct rb_root
*root
,
2255 struct sa_defrag_extent_backref
*backref
)
2257 struct rb_node
**p
= &root
->rb_node
;
2258 struct rb_node
*parent
= NULL
;
2259 struct sa_defrag_extent_backref
*entry
;
2264 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2266 ret
= backref_comp(backref
, entry
);
2270 p
= &(*p
)->rb_right
;
2273 rb_link_node(&backref
->node
, parent
, p
);
2274 rb_insert_color(&backref
->node
, root
);
2278 * Note the backref might has changed, and in this case we just return 0.
2280 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2283 struct btrfs_file_extent_item
*extent
;
2284 struct btrfs_fs_info
*fs_info
;
2285 struct old_sa_defrag_extent
*old
= ctx
;
2286 struct new_sa_defrag_extent
*new = old
->new;
2287 struct btrfs_path
*path
= new->path
;
2288 struct btrfs_key key
;
2289 struct btrfs_root
*root
;
2290 struct sa_defrag_extent_backref
*backref
;
2291 struct extent_buffer
*leaf
;
2292 struct inode
*inode
= new->inode
;
2298 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2299 inum
== btrfs_ino(inode
))
2302 key
.objectid
= root_id
;
2303 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2304 key
.offset
= (u64
)-1;
2306 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2307 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2309 if (PTR_ERR(root
) == -ENOENT
)
2312 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2313 inum
, offset
, root_id
);
2314 return PTR_ERR(root
);
2317 key
.objectid
= inum
;
2318 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2319 if (offset
> (u64
)-1 << 32)
2322 key
.offset
= offset
;
2324 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2325 if (WARN_ON(ret
< 0))
2332 leaf
= path
->nodes
[0];
2333 slot
= path
->slots
[0];
2335 if (slot
>= btrfs_header_nritems(leaf
)) {
2336 ret
= btrfs_next_leaf(root
, path
);
2339 } else if (ret
> 0) {
2348 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2350 if (key
.objectid
> inum
)
2353 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2356 extent
= btrfs_item_ptr(leaf
, slot
,
2357 struct btrfs_file_extent_item
);
2359 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2363 * 'offset' refers to the exact key.offset,
2364 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2365 * (key.offset - extent_offset).
2367 if (key
.offset
!= offset
)
2370 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2371 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2373 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2374 old
->len
|| extent_offset
+ num_bytes
<=
2375 old
->extent_offset
+ old
->offset
)
2380 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2386 backref
->root_id
= root_id
;
2387 backref
->inum
= inum
;
2388 backref
->file_pos
= offset
;
2389 backref
->num_bytes
= num_bytes
;
2390 backref
->extent_offset
= extent_offset
;
2391 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2393 backref_insert(&new->root
, backref
);
2396 btrfs_release_path(path
);
2401 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2402 struct new_sa_defrag_extent
*new)
2404 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2405 struct old_sa_defrag_extent
*old
, *tmp
;
2410 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2411 ret
= iterate_inodes_from_logical(old
->bytenr
+
2412 old
->extent_offset
, fs_info
,
2413 path
, record_one_backref
,
2415 if (ret
< 0 && ret
!= -ENOENT
)
2418 /* no backref to be processed for this extent */
2420 list_del(&old
->list
);
2425 if (list_empty(&new->head
))
2431 static int relink_is_mergable(struct extent_buffer
*leaf
,
2432 struct btrfs_file_extent_item
*fi
,
2433 struct new_sa_defrag_extent
*new)
2435 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != new->bytenr
)
2438 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2441 if (btrfs_file_extent_compression(leaf
, fi
) != new->compress_type
)
2444 if (btrfs_file_extent_encryption(leaf
, fi
) ||
2445 btrfs_file_extent_other_encoding(leaf
, fi
))
2452 * Note the backref might has changed, and in this case we just return 0.
2454 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2455 struct sa_defrag_extent_backref
*prev
,
2456 struct sa_defrag_extent_backref
*backref
)
2458 struct btrfs_file_extent_item
*extent
;
2459 struct btrfs_file_extent_item
*item
;
2460 struct btrfs_ordered_extent
*ordered
;
2461 struct btrfs_trans_handle
*trans
;
2462 struct btrfs_fs_info
*fs_info
;
2463 struct btrfs_root
*root
;
2464 struct btrfs_key key
;
2465 struct extent_buffer
*leaf
;
2466 struct old_sa_defrag_extent
*old
= backref
->old
;
2467 struct new_sa_defrag_extent
*new = old
->new;
2468 struct inode
*src_inode
= new->inode
;
2469 struct inode
*inode
;
2470 struct extent_state
*cached
= NULL
;
2479 if (prev
&& prev
->root_id
== backref
->root_id
&&
2480 prev
->inum
== backref
->inum
&&
2481 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2484 /* step 1: get root */
2485 key
.objectid
= backref
->root_id
;
2486 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2487 key
.offset
= (u64
)-1;
2489 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2490 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2492 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2494 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2495 if (PTR_ERR(root
) == -ENOENT
)
2497 return PTR_ERR(root
);
2500 if (btrfs_root_readonly(root
)) {
2501 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2505 /* step 2: get inode */
2506 key
.objectid
= backref
->inum
;
2507 key
.type
= BTRFS_INODE_ITEM_KEY
;
2510 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2511 if (IS_ERR(inode
)) {
2512 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2516 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2518 /* step 3: relink backref */
2519 lock_start
= backref
->file_pos
;
2520 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2521 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2524 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2526 btrfs_put_ordered_extent(ordered
);
2530 trans
= btrfs_join_transaction(root
);
2531 if (IS_ERR(trans
)) {
2532 ret
= PTR_ERR(trans
);
2536 key
.objectid
= backref
->inum
;
2537 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2538 key
.offset
= backref
->file_pos
;
2540 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2543 } else if (ret
> 0) {
2548 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2549 struct btrfs_file_extent_item
);
2551 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2552 backref
->generation
)
2555 btrfs_release_path(path
);
2557 start
= backref
->file_pos
;
2558 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2559 start
+= old
->extent_offset
+ old
->offset
-
2560 backref
->extent_offset
;
2562 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2563 old
->extent_offset
+ old
->offset
+ old
->len
);
2564 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2566 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2571 key
.objectid
= btrfs_ino(inode
);
2572 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2575 path
->leave_spinning
= 1;
2577 struct btrfs_file_extent_item
*fi
;
2579 struct btrfs_key found_key
;
2581 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2586 leaf
= path
->nodes
[0];
2587 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2589 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2590 struct btrfs_file_extent_item
);
2591 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2593 if (extent_len
+ found_key
.offset
== start
&&
2594 relink_is_mergable(leaf
, fi
, new)) {
2595 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2597 btrfs_mark_buffer_dirty(leaf
);
2598 inode_add_bytes(inode
, len
);
2604 btrfs_release_path(path
);
2609 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2612 btrfs_abort_transaction(trans
, ret
);
2616 leaf
= path
->nodes
[0];
2617 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2618 struct btrfs_file_extent_item
);
2619 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2620 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2621 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2622 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2623 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2624 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2625 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2626 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2627 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2628 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2630 btrfs_mark_buffer_dirty(leaf
);
2631 inode_add_bytes(inode
, len
);
2632 btrfs_release_path(path
);
2634 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2636 backref
->root_id
, backref
->inum
,
2637 new->file_pos
); /* start - extent_offset */
2639 btrfs_abort_transaction(trans
, ret
);
2645 btrfs_release_path(path
);
2646 path
->leave_spinning
= 0;
2647 btrfs_end_transaction(trans
, root
);
2649 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2655 static void free_sa_defrag_extent(struct new_sa_defrag_extent
*new)
2657 struct old_sa_defrag_extent
*old
, *tmp
;
2662 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2668 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2670 struct btrfs_path
*path
;
2671 struct sa_defrag_extent_backref
*backref
;
2672 struct sa_defrag_extent_backref
*prev
= NULL
;
2673 struct inode
*inode
;
2674 struct btrfs_root
*root
;
2675 struct rb_node
*node
;
2679 root
= BTRFS_I(inode
)->root
;
2681 path
= btrfs_alloc_path();
2685 if (!record_extent_backrefs(path
, new)) {
2686 btrfs_free_path(path
);
2689 btrfs_release_path(path
);
2692 node
= rb_first(&new->root
);
2695 rb_erase(node
, &new->root
);
2697 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2699 ret
= relink_extent_backref(path
, prev
, backref
);
2712 btrfs_free_path(path
);
2714 free_sa_defrag_extent(new);
2716 atomic_dec(&root
->fs_info
->defrag_running
);
2717 wake_up(&root
->fs_info
->transaction_wait
);
2720 static struct new_sa_defrag_extent
*
2721 record_old_file_extents(struct inode
*inode
,
2722 struct btrfs_ordered_extent
*ordered
)
2724 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2725 struct btrfs_path
*path
;
2726 struct btrfs_key key
;
2727 struct old_sa_defrag_extent
*old
;
2728 struct new_sa_defrag_extent
*new;
2731 new = kmalloc(sizeof(*new), GFP_NOFS
);
2736 new->file_pos
= ordered
->file_offset
;
2737 new->len
= ordered
->len
;
2738 new->bytenr
= ordered
->start
;
2739 new->disk_len
= ordered
->disk_len
;
2740 new->compress_type
= ordered
->compress_type
;
2741 new->root
= RB_ROOT
;
2742 INIT_LIST_HEAD(&new->head
);
2744 path
= btrfs_alloc_path();
2748 key
.objectid
= btrfs_ino(inode
);
2749 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2750 key
.offset
= new->file_pos
;
2752 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2755 if (ret
> 0 && path
->slots
[0] > 0)
2758 /* find out all the old extents for the file range */
2760 struct btrfs_file_extent_item
*extent
;
2761 struct extent_buffer
*l
;
2770 slot
= path
->slots
[0];
2772 if (slot
>= btrfs_header_nritems(l
)) {
2773 ret
= btrfs_next_leaf(root
, path
);
2781 btrfs_item_key_to_cpu(l
, &key
, slot
);
2783 if (key
.objectid
!= btrfs_ino(inode
))
2785 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2787 if (key
.offset
>= new->file_pos
+ new->len
)
2790 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2792 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2793 if (key
.offset
+ num_bytes
< new->file_pos
)
2796 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2800 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2802 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2806 offset
= max(new->file_pos
, key
.offset
);
2807 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2809 old
->bytenr
= disk_bytenr
;
2810 old
->extent_offset
= extent_offset
;
2811 old
->offset
= offset
- key
.offset
;
2812 old
->len
= end
- offset
;
2815 list_add_tail(&old
->list
, &new->head
);
2821 btrfs_free_path(path
);
2822 atomic_inc(&root
->fs_info
->defrag_running
);
2827 btrfs_free_path(path
);
2829 free_sa_defrag_extent(new);
2833 static void btrfs_release_delalloc_bytes(struct btrfs_root
*root
,
2836 struct btrfs_block_group_cache
*cache
;
2838 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
2841 spin_lock(&cache
->lock
);
2842 cache
->delalloc_bytes
-= len
;
2843 spin_unlock(&cache
->lock
);
2845 btrfs_put_block_group(cache
);
2848 /* as ordered data IO finishes, this gets called so we can finish
2849 * an ordered extent if the range of bytes in the file it covers are
2852 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2854 struct inode
*inode
= ordered_extent
->inode
;
2855 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2856 struct btrfs_trans_handle
*trans
= NULL
;
2857 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2858 struct extent_state
*cached_state
= NULL
;
2859 struct new_sa_defrag_extent
*new = NULL
;
2860 int compress_type
= 0;
2862 u64 logical_len
= ordered_extent
->len
;
2864 bool truncated
= false;
2866 nolock
= btrfs_is_free_space_inode(inode
);
2868 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2873 btrfs_free_io_failure_record(inode
, ordered_extent
->file_offset
,
2874 ordered_extent
->file_offset
+
2875 ordered_extent
->len
- 1);
2877 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2879 logical_len
= ordered_extent
->truncated_len
;
2880 /* Truncated the entire extent, don't bother adding */
2885 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2886 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2889 * For mwrite(mmap + memset to write) case, we still reserve
2890 * space for NOCOW range.
2891 * As NOCOW won't cause a new delayed ref, just free the space
2893 btrfs_qgroup_free_data(inode
, ordered_extent
->file_offset
,
2894 ordered_extent
->len
);
2895 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2897 trans
= btrfs_join_transaction_nolock(root
);
2899 trans
= btrfs_join_transaction(root
);
2900 if (IS_ERR(trans
)) {
2901 ret
= PTR_ERR(trans
);
2905 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2906 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2907 if (ret
) /* -ENOMEM or corruption */
2908 btrfs_abort_transaction(trans
, ret
);
2912 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2913 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2916 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2917 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2918 EXTENT_DEFRAG
, 1, cached_state
);
2920 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2921 if (0 && last_snapshot
>= BTRFS_I(inode
)->generation
)
2922 /* the inode is shared */
2923 new = record_old_file_extents(inode
, ordered_extent
);
2925 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2926 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2927 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2931 trans
= btrfs_join_transaction_nolock(root
);
2933 trans
= btrfs_join_transaction(root
);
2934 if (IS_ERR(trans
)) {
2935 ret
= PTR_ERR(trans
);
2940 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2942 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2943 compress_type
= ordered_extent
->compress_type
;
2944 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2945 BUG_ON(compress_type
);
2946 ret
= btrfs_mark_extent_written(trans
, inode
,
2947 ordered_extent
->file_offset
,
2948 ordered_extent
->file_offset
+
2951 BUG_ON(root
== root
->fs_info
->tree_root
);
2952 ret
= insert_reserved_file_extent(trans
, inode
,
2953 ordered_extent
->file_offset
,
2954 ordered_extent
->start
,
2955 ordered_extent
->disk_len
,
2956 logical_len
, logical_len
,
2957 compress_type
, 0, 0,
2958 BTRFS_FILE_EXTENT_REG
);
2960 btrfs_release_delalloc_bytes(root
,
2961 ordered_extent
->start
,
2962 ordered_extent
->disk_len
);
2964 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2965 ordered_extent
->file_offset
, ordered_extent
->len
,
2968 btrfs_abort_transaction(trans
, ret
);
2972 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2973 &ordered_extent
->list
);
2975 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2976 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2977 if (ret
) { /* -ENOMEM or corruption */
2978 btrfs_abort_transaction(trans
, ret
);
2983 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2984 ordered_extent
->file_offset
+
2985 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2987 if (root
!= root
->fs_info
->tree_root
)
2988 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2990 btrfs_end_transaction(trans
, root
);
2992 if (ret
|| truncated
) {
2996 start
= ordered_extent
->file_offset
+ logical_len
;
2998 start
= ordered_extent
->file_offset
;
2999 end
= ordered_extent
->file_offset
+ ordered_extent
->len
- 1;
3000 clear_extent_uptodate(io_tree
, start
, end
, NULL
, GFP_NOFS
);
3002 /* Drop the cache for the part of the extent we didn't write. */
3003 btrfs_drop_extent_cache(inode
, start
, end
, 0);
3006 * If the ordered extent had an IOERR or something else went
3007 * wrong we need to return the space for this ordered extent
3008 * back to the allocator. We only free the extent in the
3009 * truncated case if we didn't write out the extent at all.
3011 if ((ret
|| !logical_len
) &&
3012 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
3013 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
3014 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
3015 ordered_extent
->disk_len
, 1);
3020 * This needs to be done to make sure anybody waiting knows we are done
3021 * updating everything for this ordered extent.
3023 btrfs_remove_ordered_extent(inode
, ordered_extent
);
3025 /* for snapshot-aware defrag */
3028 free_sa_defrag_extent(new);
3029 atomic_dec(&root
->fs_info
->defrag_running
);
3031 relink_file_extents(new);
3036 btrfs_put_ordered_extent(ordered_extent
);
3037 /* once for the tree */
3038 btrfs_put_ordered_extent(ordered_extent
);
3043 static void finish_ordered_fn(struct btrfs_work
*work
)
3045 struct btrfs_ordered_extent
*ordered_extent
;
3046 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
3047 btrfs_finish_ordered_io(ordered_extent
);
3050 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
3051 struct extent_state
*state
, int uptodate
)
3053 struct inode
*inode
= page
->mapping
->host
;
3054 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3055 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
3056 struct btrfs_workqueue
*wq
;
3057 btrfs_work_func_t func
;
3059 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
3061 ClearPagePrivate2(page
);
3062 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
3063 end
- start
+ 1, uptodate
))
3066 if (btrfs_is_free_space_inode(inode
)) {
3067 wq
= root
->fs_info
->endio_freespace_worker
;
3068 func
= btrfs_freespace_write_helper
;
3070 wq
= root
->fs_info
->endio_write_workers
;
3071 func
= btrfs_endio_write_helper
;
3074 btrfs_init_work(&ordered_extent
->work
, func
, finish_ordered_fn
, NULL
,
3076 btrfs_queue_work(wq
, &ordered_extent
->work
);
3081 static int __readpage_endio_check(struct inode
*inode
,
3082 struct btrfs_io_bio
*io_bio
,
3083 int icsum
, struct page
*page
,
3084 int pgoff
, u64 start
, size_t len
)
3090 csum_expected
= *(((u32
*)io_bio
->csum
) + icsum
);
3092 kaddr
= kmap_atomic(page
);
3093 csum
= btrfs_csum_data(kaddr
+ pgoff
, csum
, len
);
3094 btrfs_csum_final(csum
, (char *)&csum
);
3095 if (csum
!= csum_expected
)
3098 kunmap_atomic(kaddr
);
3101 btrfs_warn_rl(BTRFS_I(inode
)->root
->fs_info
,
3102 "csum failed ino %llu off %llu csum %u expected csum %u",
3103 btrfs_ino(inode
), start
, csum
, csum_expected
);
3104 memset(kaddr
+ pgoff
, 1, len
);
3105 flush_dcache_page(page
);
3106 kunmap_atomic(kaddr
);
3107 if (csum_expected
== 0)
3113 * when reads are done, we need to check csums to verify the data is correct
3114 * if there's a match, we allow the bio to finish. If not, the code in
3115 * extent_io.c will try to find good copies for us.
3117 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
3118 u64 phy_offset
, struct page
*page
,
3119 u64 start
, u64 end
, int mirror
)
3121 size_t offset
= start
- page_offset(page
);
3122 struct inode
*inode
= page
->mapping
->host
;
3123 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3124 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3126 if (PageChecked(page
)) {
3127 ClearPageChecked(page
);
3131 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
3134 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
3135 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
3136 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
);
3140 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
3141 return __readpage_endio_check(inode
, io_bio
, phy_offset
, page
, offset
,
3142 start
, (size_t)(end
- start
+ 1));
3145 void btrfs_add_delayed_iput(struct inode
*inode
)
3147 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
3148 struct btrfs_inode
*binode
= BTRFS_I(inode
);
3150 if (atomic_add_unless(&inode
->i_count
, -1, 1))
3153 spin_lock(&fs_info
->delayed_iput_lock
);
3154 if (binode
->delayed_iput_count
== 0) {
3155 ASSERT(list_empty(&binode
->delayed_iput
));
3156 list_add_tail(&binode
->delayed_iput
, &fs_info
->delayed_iputs
);
3158 binode
->delayed_iput_count
++;
3160 spin_unlock(&fs_info
->delayed_iput_lock
);
3163 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
3165 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3167 spin_lock(&fs_info
->delayed_iput_lock
);
3168 while (!list_empty(&fs_info
->delayed_iputs
)) {
3169 struct btrfs_inode
*inode
;
3171 inode
= list_first_entry(&fs_info
->delayed_iputs
,
3172 struct btrfs_inode
, delayed_iput
);
3173 if (inode
->delayed_iput_count
) {
3174 inode
->delayed_iput_count
--;
3175 list_move_tail(&inode
->delayed_iput
,
3176 &fs_info
->delayed_iputs
);
3178 list_del_init(&inode
->delayed_iput
);
3180 spin_unlock(&fs_info
->delayed_iput_lock
);
3181 iput(&inode
->vfs_inode
);
3182 spin_lock(&fs_info
->delayed_iput_lock
);
3184 spin_unlock(&fs_info
->delayed_iput_lock
);
3188 * This is called in transaction commit time. If there are no orphan
3189 * files in the subvolume, it removes orphan item and frees block_rsv
3192 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
3193 struct btrfs_root
*root
)
3195 struct btrfs_block_rsv
*block_rsv
;
3198 if (atomic_read(&root
->orphan_inodes
) ||
3199 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
3202 spin_lock(&root
->orphan_lock
);
3203 if (atomic_read(&root
->orphan_inodes
)) {
3204 spin_unlock(&root
->orphan_lock
);
3208 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
3209 spin_unlock(&root
->orphan_lock
);
3213 block_rsv
= root
->orphan_block_rsv
;
3214 root
->orphan_block_rsv
= NULL
;
3215 spin_unlock(&root
->orphan_lock
);
3217 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
) &&
3218 btrfs_root_refs(&root
->root_item
) > 0) {
3219 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
3220 root
->root_key
.objectid
);
3222 btrfs_abort_transaction(trans
, ret
);
3224 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
,
3229 WARN_ON(block_rsv
->size
> 0);
3230 btrfs_free_block_rsv(root
, block_rsv
);
3235 * This creates an orphan entry for the given inode in case something goes
3236 * wrong in the middle of an unlink/truncate.
3238 * NOTE: caller of this function should reserve 5 units of metadata for
3241 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
3243 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3244 struct btrfs_block_rsv
*block_rsv
= NULL
;
3249 if (!root
->orphan_block_rsv
) {
3250 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
3255 spin_lock(&root
->orphan_lock
);
3256 if (!root
->orphan_block_rsv
) {
3257 root
->orphan_block_rsv
= block_rsv
;
3258 } else if (block_rsv
) {
3259 btrfs_free_block_rsv(root
, block_rsv
);
3263 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3264 &BTRFS_I(inode
)->runtime_flags
)) {
3267 * For proper ENOSPC handling, we should do orphan
3268 * cleanup when mounting. But this introduces backward
3269 * compatibility issue.
3271 if (!xchg(&root
->orphan_item_inserted
, 1))
3277 atomic_inc(&root
->orphan_inodes
);
3280 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3281 &BTRFS_I(inode
)->runtime_flags
))
3283 spin_unlock(&root
->orphan_lock
);
3285 /* grab metadata reservation from transaction handle */
3287 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
3290 atomic_dec(&root
->orphan_inodes
);
3291 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3292 &BTRFS_I(inode
)->runtime_flags
);
3294 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3295 &BTRFS_I(inode
)->runtime_flags
);
3300 /* insert an orphan item to track this unlinked/truncated file */
3302 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
3304 atomic_dec(&root
->orphan_inodes
);
3306 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3307 &BTRFS_I(inode
)->runtime_flags
);
3308 btrfs_orphan_release_metadata(inode
);
3310 if (ret
!= -EEXIST
) {
3311 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3312 &BTRFS_I(inode
)->runtime_flags
);
3313 btrfs_abort_transaction(trans
, ret
);
3320 /* insert an orphan item to track subvolume contains orphan files */
3322 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
3323 root
->root_key
.objectid
);
3324 if (ret
&& ret
!= -EEXIST
) {
3325 btrfs_abort_transaction(trans
, ret
);
3333 * We have done the truncate/delete so we can go ahead and remove the orphan
3334 * item for this particular inode.
3336 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3337 struct inode
*inode
)
3339 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3340 int delete_item
= 0;
3341 int release_rsv
= 0;
3344 spin_lock(&root
->orphan_lock
);
3345 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3346 &BTRFS_I(inode
)->runtime_flags
))
3349 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3350 &BTRFS_I(inode
)->runtime_flags
))
3352 spin_unlock(&root
->orphan_lock
);
3355 atomic_dec(&root
->orphan_inodes
);
3357 ret
= btrfs_del_orphan_item(trans
, root
,
3362 btrfs_orphan_release_metadata(inode
);
3368 * this cleans up any orphans that may be left on the list from the last use
3371 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3373 struct btrfs_path
*path
;
3374 struct extent_buffer
*leaf
;
3375 struct btrfs_key key
, found_key
;
3376 struct btrfs_trans_handle
*trans
;
3377 struct inode
*inode
;
3378 u64 last_objectid
= 0;
3379 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3381 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3384 path
= btrfs_alloc_path();
3389 path
->reada
= READA_BACK
;
3391 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3392 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
3393 key
.offset
= (u64
)-1;
3396 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3401 * if ret == 0 means we found what we were searching for, which
3402 * is weird, but possible, so only screw with path if we didn't
3403 * find the key and see if we have stuff that matches
3407 if (path
->slots
[0] == 0)
3412 /* pull out the item */
3413 leaf
= path
->nodes
[0];
3414 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3416 /* make sure the item matches what we want */
3417 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3419 if (found_key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
3422 /* release the path since we're done with it */
3423 btrfs_release_path(path
);
3426 * this is where we are basically btrfs_lookup, without the
3427 * crossing root thing. we store the inode number in the
3428 * offset of the orphan item.
3431 if (found_key
.offset
== last_objectid
) {
3432 btrfs_err(root
->fs_info
,
3433 "Error removing orphan entry, stopping orphan cleanup");
3438 last_objectid
= found_key
.offset
;
3440 found_key
.objectid
= found_key
.offset
;
3441 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3442 found_key
.offset
= 0;
3443 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3444 ret
= PTR_ERR_OR_ZERO(inode
);
3445 if (ret
&& ret
!= -ENOENT
)
3448 if (ret
== -ENOENT
&& root
== root
->fs_info
->tree_root
) {
3449 struct btrfs_root
*dead_root
;
3450 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3451 int is_dead_root
= 0;
3454 * this is an orphan in the tree root. Currently these
3455 * could come from 2 sources:
3456 * a) a snapshot deletion in progress
3457 * b) a free space cache inode
3458 * We need to distinguish those two, as the snapshot
3459 * orphan must not get deleted.
3460 * find_dead_roots already ran before us, so if this
3461 * is a snapshot deletion, we should find the root
3462 * in the dead_roots list
3464 spin_lock(&fs_info
->trans_lock
);
3465 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3467 if (dead_root
->root_key
.objectid
==
3468 found_key
.objectid
) {
3473 spin_unlock(&fs_info
->trans_lock
);
3475 /* prevent this orphan from being found again */
3476 key
.offset
= found_key
.objectid
- 1;
3481 * Inode is already gone but the orphan item is still there,
3482 * kill the orphan item.
3484 if (ret
== -ENOENT
) {
3485 trans
= btrfs_start_transaction(root
, 1);
3486 if (IS_ERR(trans
)) {
3487 ret
= PTR_ERR(trans
);
3490 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3491 found_key
.objectid
);
3492 ret
= btrfs_del_orphan_item(trans
, root
,
3493 found_key
.objectid
);
3494 btrfs_end_transaction(trans
, root
);
3501 * add this inode to the orphan list so btrfs_orphan_del does
3502 * the proper thing when we hit it
3504 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3505 &BTRFS_I(inode
)->runtime_flags
);
3506 atomic_inc(&root
->orphan_inodes
);
3508 /* if we have links, this was a truncate, lets do that */
3509 if (inode
->i_nlink
) {
3510 if (WARN_ON(!S_ISREG(inode
->i_mode
))) {
3516 /* 1 for the orphan item deletion. */
3517 trans
= btrfs_start_transaction(root
, 1);
3518 if (IS_ERR(trans
)) {
3520 ret
= PTR_ERR(trans
);
3523 ret
= btrfs_orphan_add(trans
, inode
);
3524 btrfs_end_transaction(trans
, root
);
3530 ret
= btrfs_truncate(inode
);
3532 btrfs_orphan_del(NULL
, inode
);
3537 /* this will do delete_inode and everything for us */
3542 /* release the path since we're done with it */
3543 btrfs_release_path(path
);
3545 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3547 if (root
->orphan_block_rsv
)
3548 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3551 if (root
->orphan_block_rsv
||
3552 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
)) {
3553 trans
= btrfs_join_transaction(root
);
3555 btrfs_end_transaction(trans
, root
);
3559 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3561 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3565 btrfs_err(root
->fs_info
,
3566 "could not do orphan cleanup %d", ret
);
3567 btrfs_free_path(path
);
3572 * very simple check to peek ahead in the leaf looking for xattrs. If we
3573 * don't find any xattrs, we know there can't be any acls.
3575 * slot is the slot the inode is in, objectid is the objectid of the inode
3577 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3578 int slot
, u64 objectid
,
3579 int *first_xattr_slot
)
3581 u32 nritems
= btrfs_header_nritems(leaf
);
3582 struct btrfs_key found_key
;
3583 static u64 xattr_access
= 0;
3584 static u64 xattr_default
= 0;
3587 if (!xattr_access
) {
3588 xattr_access
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS
,
3589 strlen(XATTR_NAME_POSIX_ACL_ACCESS
));
3590 xattr_default
= btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT
,
3591 strlen(XATTR_NAME_POSIX_ACL_DEFAULT
));
3595 *first_xattr_slot
= -1;
3596 while (slot
< nritems
) {
3597 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3599 /* we found a different objectid, there must not be acls */
3600 if (found_key
.objectid
!= objectid
)
3603 /* we found an xattr, assume we've got an acl */
3604 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3605 if (*first_xattr_slot
== -1)
3606 *first_xattr_slot
= slot
;
3607 if (found_key
.offset
== xattr_access
||
3608 found_key
.offset
== xattr_default
)
3613 * we found a key greater than an xattr key, there can't
3614 * be any acls later on
3616 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3623 * it goes inode, inode backrefs, xattrs, extents,
3624 * so if there are a ton of hard links to an inode there can
3625 * be a lot of backrefs. Don't waste time searching too hard,
3626 * this is just an optimization
3631 /* we hit the end of the leaf before we found an xattr or
3632 * something larger than an xattr. We have to assume the inode
3635 if (*first_xattr_slot
== -1)
3636 *first_xattr_slot
= slot
;
3641 * read an inode from the btree into the in-memory inode
3643 static int btrfs_read_locked_inode(struct inode
*inode
)
3645 struct btrfs_path
*path
;
3646 struct extent_buffer
*leaf
;
3647 struct btrfs_inode_item
*inode_item
;
3648 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3649 struct btrfs_key location
;
3654 bool filled
= false;
3655 int first_xattr_slot
;
3657 ret
= btrfs_fill_inode(inode
, &rdev
);
3661 path
= btrfs_alloc_path();
3667 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3669 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3676 leaf
= path
->nodes
[0];
3681 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3682 struct btrfs_inode_item
);
3683 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3684 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3685 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3686 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3687 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3689 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->atime
);
3690 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->atime
);
3692 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->mtime
);
3693 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->mtime
);
3695 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->ctime
);
3696 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->ctime
);
3698 BTRFS_I(inode
)->i_otime
.tv_sec
=
3699 btrfs_timespec_sec(leaf
, &inode_item
->otime
);
3700 BTRFS_I(inode
)->i_otime
.tv_nsec
=
3701 btrfs_timespec_nsec(leaf
, &inode_item
->otime
);
3703 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3704 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3705 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3707 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3708 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3710 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3712 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3713 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3717 * If we were modified in the current generation and evicted from memory
3718 * and then re-read we need to do a full sync since we don't have any
3719 * idea about which extents were modified before we were evicted from
3722 * This is required for both inode re-read from disk and delayed inode
3723 * in delayed_nodes_tree.
3725 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3726 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3727 &BTRFS_I(inode
)->runtime_flags
);
3730 * We don't persist the id of the transaction where an unlink operation
3731 * against the inode was last made. So here we assume the inode might
3732 * have been evicted, and therefore the exact value of last_unlink_trans
3733 * lost, and set it to last_trans to avoid metadata inconsistencies
3734 * between the inode and its parent if the inode is fsync'ed and the log
3735 * replayed. For example, in the scenario:
3738 * ln mydir/foo mydir/bar
3741 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3742 * xfs_io -c fsync mydir/foo
3744 * mount fs, triggers fsync log replay
3746 * We must make sure that when we fsync our inode foo we also log its
3747 * parent inode, otherwise after log replay the parent still has the
3748 * dentry with the "bar" name but our inode foo has a link count of 1
3749 * and doesn't have an inode ref with the name "bar" anymore.
3751 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3752 * but it guarantees correctness at the expense of occasional full
3753 * transaction commits on fsync if our inode is a directory, or if our
3754 * inode is not a directory, logging its parent unnecessarily.
3756 BTRFS_I(inode
)->last_unlink_trans
= BTRFS_I(inode
)->last_trans
;
3759 if (inode
->i_nlink
!= 1 ||
3760 path
->slots
[0] >= btrfs_header_nritems(leaf
))
3763 btrfs_item_key_to_cpu(leaf
, &location
, path
->slots
[0]);
3764 if (location
.objectid
!= btrfs_ino(inode
))
3767 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3768 if (location
.type
== BTRFS_INODE_REF_KEY
) {
3769 struct btrfs_inode_ref
*ref
;
3771 ref
= (struct btrfs_inode_ref
*)ptr
;
3772 BTRFS_I(inode
)->dir_index
= btrfs_inode_ref_index(leaf
, ref
);
3773 } else if (location
.type
== BTRFS_INODE_EXTREF_KEY
) {
3774 struct btrfs_inode_extref
*extref
;
3776 extref
= (struct btrfs_inode_extref
*)ptr
;
3777 BTRFS_I(inode
)->dir_index
= btrfs_inode_extref_index(leaf
,
3782 * try to precache a NULL acl entry for files that don't have
3783 * any xattrs or acls
3785 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3786 btrfs_ino(inode
), &first_xattr_slot
);
3787 if (first_xattr_slot
!= -1) {
3788 path
->slots
[0] = first_xattr_slot
;
3789 ret
= btrfs_load_inode_props(inode
, path
);
3791 btrfs_err(root
->fs_info
,
3792 "error loading props for ino %llu (root %llu): %d",
3794 root
->root_key
.objectid
, ret
);
3796 btrfs_free_path(path
);
3799 cache_no_acl(inode
);
3801 switch (inode
->i_mode
& S_IFMT
) {
3803 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3804 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3805 inode
->i_fop
= &btrfs_file_operations
;
3806 inode
->i_op
= &btrfs_file_inode_operations
;
3809 inode
->i_fop
= &btrfs_dir_file_operations
;
3810 if (root
== root
->fs_info
->tree_root
)
3811 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3813 inode
->i_op
= &btrfs_dir_inode_operations
;
3816 inode
->i_op
= &btrfs_symlink_inode_operations
;
3817 inode_nohighmem(inode
);
3818 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3821 inode
->i_op
= &btrfs_special_inode_operations
;
3822 init_special_inode(inode
, inode
->i_mode
, rdev
);
3826 btrfs_update_iflags(inode
);
3830 btrfs_free_path(path
);
3831 make_bad_inode(inode
);
3836 * given a leaf and an inode, copy the inode fields into the leaf
3838 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3839 struct extent_buffer
*leaf
,
3840 struct btrfs_inode_item
*item
,
3841 struct inode
*inode
)
3843 struct btrfs_map_token token
;
3845 btrfs_init_map_token(&token
);
3847 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3848 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3849 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3851 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3852 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3854 btrfs_set_token_timespec_sec(leaf
, &item
->atime
,
3855 inode
->i_atime
.tv_sec
, &token
);
3856 btrfs_set_token_timespec_nsec(leaf
, &item
->atime
,
3857 inode
->i_atime
.tv_nsec
, &token
);
3859 btrfs_set_token_timespec_sec(leaf
, &item
->mtime
,
3860 inode
->i_mtime
.tv_sec
, &token
);
3861 btrfs_set_token_timespec_nsec(leaf
, &item
->mtime
,
3862 inode
->i_mtime
.tv_nsec
, &token
);
3864 btrfs_set_token_timespec_sec(leaf
, &item
->ctime
,
3865 inode
->i_ctime
.tv_sec
, &token
);
3866 btrfs_set_token_timespec_nsec(leaf
, &item
->ctime
,
3867 inode
->i_ctime
.tv_nsec
, &token
);
3869 btrfs_set_token_timespec_sec(leaf
, &item
->otime
,
3870 BTRFS_I(inode
)->i_otime
.tv_sec
, &token
);
3871 btrfs_set_token_timespec_nsec(leaf
, &item
->otime
,
3872 BTRFS_I(inode
)->i_otime
.tv_nsec
, &token
);
3874 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3876 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3878 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3879 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3880 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3881 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3882 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3886 * copy everything in the in-memory inode into the btree.
3888 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3889 struct btrfs_root
*root
, struct inode
*inode
)
3891 struct btrfs_inode_item
*inode_item
;
3892 struct btrfs_path
*path
;
3893 struct extent_buffer
*leaf
;
3896 path
= btrfs_alloc_path();
3900 path
->leave_spinning
= 1;
3901 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3909 leaf
= path
->nodes
[0];
3910 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3911 struct btrfs_inode_item
);
3913 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3914 btrfs_mark_buffer_dirty(leaf
);
3915 btrfs_set_inode_last_trans(trans
, inode
);
3918 btrfs_free_path(path
);
3923 * copy everything in the in-memory inode into the btree.
3925 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3926 struct btrfs_root
*root
, struct inode
*inode
)
3931 * If the inode is a free space inode, we can deadlock during commit
3932 * if we put it into the delayed code.
3934 * The data relocation inode should also be directly updated
3937 if (!btrfs_is_free_space_inode(inode
)
3938 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
3939 && !root
->fs_info
->log_root_recovering
) {
3940 btrfs_update_root_times(trans
, root
);
3942 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3944 btrfs_set_inode_last_trans(trans
, inode
);
3948 return btrfs_update_inode_item(trans
, root
, inode
);
3951 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3952 struct btrfs_root
*root
,
3953 struct inode
*inode
)
3957 ret
= btrfs_update_inode(trans
, root
, inode
);
3959 return btrfs_update_inode_item(trans
, root
, inode
);
3964 * unlink helper that gets used here in inode.c and in the tree logging
3965 * recovery code. It remove a link in a directory with a given name, and
3966 * also drops the back refs in the inode to the directory
3968 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3969 struct btrfs_root
*root
,
3970 struct inode
*dir
, struct inode
*inode
,
3971 const char *name
, int name_len
)
3973 struct btrfs_path
*path
;
3975 struct extent_buffer
*leaf
;
3976 struct btrfs_dir_item
*di
;
3977 struct btrfs_key key
;
3979 u64 ino
= btrfs_ino(inode
);
3980 u64 dir_ino
= btrfs_ino(dir
);
3982 path
= btrfs_alloc_path();
3988 path
->leave_spinning
= 1;
3989 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3990 name
, name_len
, -1);
3999 leaf
= path
->nodes
[0];
4000 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
4001 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
4004 btrfs_release_path(path
);
4007 * If we don't have dir index, we have to get it by looking up
4008 * the inode ref, since we get the inode ref, remove it directly,
4009 * it is unnecessary to do delayed deletion.
4011 * But if we have dir index, needn't search inode ref to get it.
4012 * Since the inode ref is close to the inode item, it is better
4013 * that we delay to delete it, and just do this deletion when
4014 * we update the inode item.
4016 if (BTRFS_I(inode
)->dir_index
) {
4017 ret
= btrfs_delayed_delete_inode_ref(inode
);
4019 index
= BTRFS_I(inode
)->dir_index
;
4024 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
4027 btrfs_info(root
->fs_info
,
4028 "failed to delete reference to %.*s, inode %llu parent %llu",
4029 name_len
, name
, ino
, dir_ino
);
4030 btrfs_abort_transaction(trans
, ret
);
4034 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
4036 btrfs_abort_transaction(trans
, ret
);
4040 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
4042 if (ret
!= 0 && ret
!= -ENOENT
) {
4043 btrfs_abort_transaction(trans
, ret
);
4047 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
4052 btrfs_abort_transaction(trans
, ret
);
4054 btrfs_free_path(path
);
4058 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
4059 inode_inc_iversion(inode
);
4060 inode_inc_iversion(dir
);
4061 inode
->i_ctime
= dir
->i_mtime
=
4062 dir
->i_ctime
= current_fs_time(inode
->i_sb
);
4063 ret
= btrfs_update_inode(trans
, root
, dir
);
4068 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
4069 struct btrfs_root
*root
,
4070 struct inode
*dir
, struct inode
*inode
,
4071 const char *name
, int name_len
)
4074 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
4077 ret
= btrfs_update_inode(trans
, root
, inode
);
4083 * helper to start transaction for unlink and rmdir.
4085 * unlink and rmdir are special in btrfs, they do not always free space, so
4086 * if we cannot make our reservations the normal way try and see if there is
4087 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4088 * allow the unlink to occur.
4090 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
4092 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4095 * 1 for the possible orphan item
4096 * 1 for the dir item
4097 * 1 for the dir index
4098 * 1 for the inode ref
4101 return btrfs_start_transaction_fallback_global_rsv(root
, 5, 5);
4104 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
4106 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4107 struct btrfs_trans_handle
*trans
;
4108 struct inode
*inode
= d_inode(dentry
);
4111 trans
= __unlink_start_trans(dir
);
4113 return PTR_ERR(trans
);
4115 btrfs_record_unlink_dir(trans
, dir
, d_inode(dentry
), 0);
4117 ret
= btrfs_unlink_inode(trans
, root
, dir
, d_inode(dentry
),
4118 dentry
->d_name
.name
, dentry
->d_name
.len
);
4122 if (inode
->i_nlink
== 0) {
4123 ret
= btrfs_orphan_add(trans
, inode
);
4129 btrfs_end_transaction(trans
, root
);
4130 btrfs_btree_balance_dirty(root
);
4134 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
4135 struct btrfs_root
*root
,
4136 struct inode
*dir
, u64 objectid
,
4137 const char *name
, int name_len
)
4139 struct btrfs_path
*path
;
4140 struct extent_buffer
*leaf
;
4141 struct btrfs_dir_item
*di
;
4142 struct btrfs_key key
;
4145 u64 dir_ino
= btrfs_ino(dir
);
4147 path
= btrfs_alloc_path();
4151 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
4152 name
, name_len
, -1);
4153 if (IS_ERR_OR_NULL(di
)) {
4161 leaf
= path
->nodes
[0];
4162 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
4163 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
4164 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
4166 btrfs_abort_transaction(trans
, ret
);
4169 btrfs_release_path(path
);
4171 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
4172 objectid
, root
->root_key
.objectid
,
4173 dir_ino
, &index
, name
, name_len
);
4175 if (ret
!= -ENOENT
) {
4176 btrfs_abort_transaction(trans
, ret
);
4179 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
4181 if (IS_ERR_OR_NULL(di
)) {
4186 btrfs_abort_transaction(trans
, ret
);
4190 leaf
= path
->nodes
[0];
4191 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4192 btrfs_release_path(path
);
4195 btrfs_release_path(path
);
4197 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
4199 btrfs_abort_transaction(trans
, ret
);
4203 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
4204 inode_inc_iversion(dir
);
4205 dir
->i_mtime
= dir
->i_ctime
= current_fs_time(dir
->i_sb
);
4206 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
4208 btrfs_abort_transaction(trans
, ret
);
4210 btrfs_free_path(path
);
4214 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
4216 struct inode
*inode
= d_inode(dentry
);
4218 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4219 struct btrfs_trans_handle
*trans
;
4220 u64 last_unlink_trans
;
4222 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
4224 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
4227 trans
= __unlink_start_trans(dir
);
4229 return PTR_ERR(trans
);
4231 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
4232 err
= btrfs_unlink_subvol(trans
, root
, dir
,
4233 BTRFS_I(inode
)->location
.objectid
,
4234 dentry
->d_name
.name
,
4235 dentry
->d_name
.len
);
4239 err
= btrfs_orphan_add(trans
, inode
);
4243 last_unlink_trans
= BTRFS_I(inode
)->last_unlink_trans
;
4245 /* now the directory is empty */
4246 err
= btrfs_unlink_inode(trans
, root
, dir
, d_inode(dentry
),
4247 dentry
->d_name
.name
, dentry
->d_name
.len
);
4249 btrfs_i_size_write(inode
, 0);
4251 * Propagate the last_unlink_trans value of the deleted dir to
4252 * its parent directory. This is to prevent an unrecoverable
4253 * log tree in the case we do something like this:
4255 * 2) create snapshot under dir foo
4256 * 3) delete the snapshot
4259 * 6) fsync foo or some file inside foo
4261 if (last_unlink_trans
>= trans
->transid
)
4262 BTRFS_I(dir
)->last_unlink_trans
= last_unlink_trans
;
4265 btrfs_end_transaction(trans
, root
);
4266 btrfs_btree_balance_dirty(root
);
4271 static int truncate_space_check(struct btrfs_trans_handle
*trans
,
4272 struct btrfs_root
*root
,
4278 * This is only used to apply pressure to the enospc system, we don't
4279 * intend to use this reservation at all.
4281 bytes_deleted
= btrfs_csum_bytes_to_leaves(root
, bytes_deleted
);
4282 bytes_deleted
*= root
->nodesize
;
4283 ret
= btrfs_block_rsv_add(root
, &root
->fs_info
->trans_block_rsv
,
4284 bytes_deleted
, BTRFS_RESERVE_NO_FLUSH
);
4286 trace_btrfs_space_reservation(root
->fs_info
, "transaction",
4289 trans
->bytes_reserved
+= bytes_deleted
;
4295 static int truncate_inline_extent(struct inode
*inode
,
4296 struct btrfs_path
*path
,
4297 struct btrfs_key
*found_key
,
4301 struct extent_buffer
*leaf
= path
->nodes
[0];
4302 int slot
= path
->slots
[0];
4303 struct btrfs_file_extent_item
*fi
;
4304 u32 size
= (u32
)(new_size
- found_key
->offset
);
4305 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4307 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
4309 if (btrfs_file_extent_compression(leaf
, fi
) != BTRFS_COMPRESS_NONE
) {
4310 loff_t offset
= new_size
;
4311 loff_t page_end
= ALIGN(offset
, PAGE_SIZE
);
4314 * Zero out the remaining of the last page of our inline extent,
4315 * instead of directly truncating our inline extent here - that
4316 * would be much more complex (decompressing all the data, then
4317 * compressing the truncated data, which might be bigger than
4318 * the size of the inline extent, resize the extent, etc).
4319 * We release the path because to get the page we might need to
4320 * read the extent item from disk (data not in the page cache).
4322 btrfs_release_path(path
);
4323 return btrfs_truncate_block(inode
, offset
, page_end
- offset
,
4327 btrfs_set_file_extent_ram_bytes(leaf
, fi
, size
);
4328 size
= btrfs_file_extent_calc_inline_size(size
);
4329 btrfs_truncate_item(root
, path
, size
, 1);
4331 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4332 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4338 * this can truncate away extent items, csum items and directory items.
4339 * It starts at a high offset and removes keys until it can't find
4340 * any higher than new_size
4342 * csum items that cross the new i_size are truncated to the new size
4345 * min_type is the minimum key type to truncate down to. If set to 0, this
4346 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4348 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4349 struct btrfs_root
*root
,
4350 struct inode
*inode
,
4351 u64 new_size
, u32 min_type
)
4353 struct btrfs_path
*path
;
4354 struct extent_buffer
*leaf
;
4355 struct btrfs_file_extent_item
*fi
;
4356 struct btrfs_key key
;
4357 struct btrfs_key found_key
;
4358 u64 extent_start
= 0;
4359 u64 extent_num_bytes
= 0;
4360 u64 extent_offset
= 0;
4362 u64 last_size
= new_size
;
4363 u32 found_type
= (u8
)-1;
4366 int pending_del_nr
= 0;
4367 int pending_del_slot
= 0;
4368 int extent_type
= -1;
4371 u64 ino
= btrfs_ino(inode
);
4372 u64 bytes_deleted
= 0;
4374 bool should_throttle
= 0;
4375 bool should_end
= 0;
4377 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4380 * for non-free space inodes and ref cows, we want to back off from
4383 if (!btrfs_is_free_space_inode(inode
) &&
4384 test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4387 path
= btrfs_alloc_path();
4390 path
->reada
= READA_BACK
;
4393 * We want to drop from the next block forward in case this new size is
4394 * not block aligned since we will be keeping the last block of the
4395 * extent just the way it is.
4397 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4398 root
== root
->fs_info
->tree_root
)
4399 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
4400 root
->sectorsize
), (u64
)-1, 0);
4403 * This function is also used to drop the items in the log tree before
4404 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4405 * it is used to drop the loged items. So we shouldn't kill the delayed
4408 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4409 btrfs_kill_delayed_inode_items(inode
);
4412 key
.offset
= (u64
)-1;
4417 * with a 16K leaf size and 128MB extents, you can actually queue
4418 * up a huge file in a single leaf. Most of the time that
4419 * bytes_deleted is > 0, it will be huge by the time we get here
4421 if (be_nice
&& bytes_deleted
> SZ_32M
) {
4422 if (btrfs_should_end_transaction(trans
, root
)) {
4429 path
->leave_spinning
= 1;
4430 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4437 /* there are no items in the tree for us to truncate, we're
4440 if (path
->slots
[0] == 0)
4447 leaf
= path
->nodes
[0];
4448 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4449 found_type
= found_key
.type
;
4451 if (found_key
.objectid
!= ino
)
4454 if (found_type
< min_type
)
4457 item_end
= found_key
.offset
;
4458 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4459 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4460 struct btrfs_file_extent_item
);
4461 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4462 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4464 btrfs_file_extent_num_bytes(leaf
, fi
);
4465 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4466 item_end
+= btrfs_file_extent_inline_len(leaf
,
4467 path
->slots
[0], fi
);
4471 if (found_type
> min_type
) {
4474 if (item_end
< new_size
)
4476 if (found_key
.offset
>= new_size
)
4482 /* FIXME, shrink the extent if the ref count is only 1 */
4483 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4487 last_size
= found_key
.offset
;
4489 last_size
= new_size
;
4491 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4493 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4495 u64 orig_num_bytes
=
4496 btrfs_file_extent_num_bytes(leaf
, fi
);
4497 extent_num_bytes
= ALIGN(new_size
-
4500 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4502 num_dec
= (orig_num_bytes
-
4504 if (test_bit(BTRFS_ROOT_REF_COWS
,
4507 inode_sub_bytes(inode
, num_dec
);
4508 btrfs_mark_buffer_dirty(leaf
);
4511 btrfs_file_extent_disk_num_bytes(leaf
,
4513 extent_offset
= found_key
.offset
-
4514 btrfs_file_extent_offset(leaf
, fi
);
4516 /* FIXME blocksize != 4096 */
4517 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4518 if (extent_start
!= 0) {
4520 if (test_bit(BTRFS_ROOT_REF_COWS
,
4522 inode_sub_bytes(inode
, num_dec
);
4525 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4527 * we can't truncate inline items that have had
4531 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4532 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4535 * Need to release path in order to truncate a
4536 * compressed extent. So delete any accumulated
4537 * extent items so far.
4539 if (btrfs_file_extent_compression(leaf
, fi
) !=
4540 BTRFS_COMPRESS_NONE
&& pending_del_nr
) {
4541 err
= btrfs_del_items(trans
, root
, path
,
4545 btrfs_abort_transaction(trans
,
4552 err
= truncate_inline_extent(inode
, path
,
4557 btrfs_abort_transaction(trans
, err
);
4560 } else if (test_bit(BTRFS_ROOT_REF_COWS
,
4562 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4567 if (!pending_del_nr
) {
4568 /* no pending yet, add ourselves */
4569 pending_del_slot
= path
->slots
[0];
4571 } else if (pending_del_nr
&&
4572 path
->slots
[0] + 1 == pending_del_slot
) {
4573 /* hop on the pending chunk */
4575 pending_del_slot
= path
->slots
[0];
4582 should_throttle
= 0;
4585 (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4586 root
== root
->fs_info
->tree_root
)) {
4587 btrfs_set_path_blocking(path
);
4588 bytes_deleted
+= extent_num_bytes
;
4589 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4590 extent_num_bytes
, 0,
4591 btrfs_header_owner(leaf
),
4592 ino
, extent_offset
);
4594 if (btrfs_should_throttle_delayed_refs(trans
, root
))
4595 btrfs_async_run_delayed_refs(root
,
4597 trans
->delayed_ref_updates
* 2, 0);
4599 if (truncate_space_check(trans
, root
,
4600 extent_num_bytes
)) {
4603 if (btrfs_should_throttle_delayed_refs(trans
,
4605 should_throttle
= 1;
4610 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4613 if (path
->slots
[0] == 0 ||
4614 path
->slots
[0] != pending_del_slot
||
4615 should_throttle
|| should_end
) {
4616 if (pending_del_nr
) {
4617 ret
= btrfs_del_items(trans
, root
, path
,
4621 btrfs_abort_transaction(trans
, ret
);
4626 btrfs_release_path(path
);
4627 if (should_throttle
) {
4628 unsigned long updates
= trans
->delayed_ref_updates
;
4630 trans
->delayed_ref_updates
= 0;
4631 ret
= btrfs_run_delayed_refs(trans
, root
, updates
* 2);
4637 * if we failed to refill our space rsv, bail out
4638 * and let the transaction restart
4650 if (pending_del_nr
) {
4651 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4654 btrfs_abort_transaction(trans
, ret
);
4657 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
4658 btrfs_ordered_update_i_size(inode
, last_size
, NULL
);
4660 btrfs_free_path(path
);
4662 if (be_nice
&& bytes_deleted
> SZ_32M
) {
4663 unsigned long updates
= trans
->delayed_ref_updates
;
4665 trans
->delayed_ref_updates
= 0;
4666 ret
= btrfs_run_delayed_refs(trans
, root
, updates
* 2);
4675 * btrfs_truncate_block - read, zero a chunk and write a block
4676 * @inode - inode that we're zeroing
4677 * @from - the offset to start zeroing
4678 * @len - the length to zero, 0 to zero the entire range respective to the
4680 * @front - zero up to the offset instead of from the offset on
4682 * This will find the block for the "from" offset and cow the block and zero the
4683 * part we want to zero. This is used with truncate and hole punching.
4685 int btrfs_truncate_block(struct inode
*inode
, loff_t from
, loff_t len
,
4688 struct address_space
*mapping
= inode
->i_mapping
;
4689 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4690 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4691 struct btrfs_ordered_extent
*ordered
;
4692 struct extent_state
*cached_state
= NULL
;
4694 u32 blocksize
= root
->sectorsize
;
4695 pgoff_t index
= from
>> PAGE_SHIFT
;
4696 unsigned offset
= from
& (blocksize
- 1);
4698 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4703 if ((offset
& (blocksize
- 1)) == 0 &&
4704 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4707 ret
= btrfs_delalloc_reserve_space(inode
,
4708 round_down(from
, blocksize
), blocksize
);
4713 page
= find_or_create_page(mapping
, index
, mask
);
4715 btrfs_delalloc_release_space(inode
,
4716 round_down(from
, blocksize
),
4722 block_start
= round_down(from
, blocksize
);
4723 block_end
= block_start
+ blocksize
- 1;
4725 if (!PageUptodate(page
)) {
4726 ret
= btrfs_readpage(NULL
, page
);
4728 if (page
->mapping
!= mapping
) {
4733 if (!PageUptodate(page
)) {
4738 wait_on_page_writeback(page
);
4740 lock_extent_bits(io_tree
, block_start
, block_end
, &cached_state
);
4741 set_page_extent_mapped(page
);
4743 ordered
= btrfs_lookup_ordered_extent(inode
, block_start
);
4745 unlock_extent_cached(io_tree
, block_start
, block_end
,
4746 &cached_state
, GFP_NOFS
);
4749 btrfs_start_ordered_extent(inode
, ordered
, 1);
4750 btrfs_put_ordered_extent(ordered
);
4754 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, block_start
, block_end
,
4755 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4756 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4757 0, 0, &cached_state
, GFP_NOFS
);
4759 ret
= btrfs_set_extent_delalloc(inode
, block_start
, block_end
,
4762 unlock_extent_cached(io_tree
, block_start
, block_end
,
4763 &cached_state
, GFP_NOFS
);
4767 if (offset
!= blocksize
) {
4769 len
= blocksize
- offset
;
4772 memset(kaddr
+ (block_start
- page_offset(page
)),
4775 memset(kaddr
+ (block_start
- page_offset(page
)) + offset
,
4777 flush_dcache_page(page
);
4780 ClearPageChecked(page
);
4781 set_page_dirty(page
);
4782 unlock_extent_cached(io_tree
, block_start
, block_end
, &cached_state
,
4787 btrfs_delalloc_release_space(inode
, block_start
,
4795 static int maybe_insert_hole(struct btrfs_root
*root
, struct inode
*inode
,
4796 u64 offset
, u64 len
)
4798 struct btrfs_trans_handle
*trans
;
4802 * Still need to make sure the inode looks like it's been updated so
4803 * that any holes get logged if we fsync.
4805 if (btrfs_fs_incompat(root
->fs_info
, NO_HOLES
)) {
4806 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
4807 BTRFS_I(inode
)->last_sub_trans
= root
->log_transid
;
4808 BTRFS_I(inode
)->last_log_commit
= root
->last_log_commit
;
4813 * 1 - for the one we're dropping
4814 * 1 - for the one we're adding
4815 * 1 - for updating the inode.
4817 trans
= btrfs_start_transaction(root
, 3);
4819 return PTR_ERR(trans
);
4821 ret
= btrfs_drop_extents(trans
, root
, inode
, offset
, offset
+ len
, 1);
4823 btrfs_abort_transaction(trans
, ret
);
4824 btrfs_end_transaction(trans
, root
);
4828 ret
= btrfs_insert_file_extent(trans
, root
, btrfs_ino(inode
), offset
,
4829 0, 0, len
, 0, len
, 0, 0, 0);
4831 btrfs_abort_transaction(trans
, ret
);
4833 btrfs_update_inode(trans
, root
, inode
);
4834 btrfs_end_transaction(trans
, root
);
4839 * This function puts in dummy file extents for the area we're creating a hole
4840 * for. So if we are truncating this file to a larger size we need to insert
4841 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4842 * the range between oldsize and size
4844 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4846 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4847 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4848 struct extent_map
*em
= NULL
;
4849 struct extent_state
*cached_state
= NULL
;
4850 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4851 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4852 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4859 * If our size started in the middle of a block we need to zero out the
4860 * rest of the block before we expand the i_size, otherwise we could
4861 * expose stale data.
4863 err
= btrfs_truncate_block(inode
, oldsize
, 0, 0);
4867 if (size
<= hole_start
)
4871 struct btrfs_ordered_extent
*ordered
;
4873 lock_extent_bits(io_tree
, hole_start
, block_end
- 1,
4875 ordered
= btrfs_lookup_ordered_range(inode
, hole_start
,
4876 block_end
- hole_start
);
4879 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4880 &cached_state
, GFP_NOFS
);
4881 btrfs_start_ordered_extent(inode
, ordered
, 1);
4882 btrfs_put_ordered_extent(ordered
);
4885 cur_offset
= hole_start
;
4887 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4888 block_end
- cur_offset
, 0);
4894 last_byte
= min(extent_map_end(em
), block_end
);
4895 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4896 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4897 struct extent_map
*hole_em
;
4898 hole_size
= last_byte
- cur_offset
;
4900 err
= maybe_insert_hole(root
, inode
, cur_offset
,
4904 btrfs_drop_extent_cache(inode
, cur_offset
,
4905 cur_offset
+ hole_size
- 1, 0);
4906 hole_em
= alloc_extent_map();
4908 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4909 &BTRFS_I(inode
)->runtime_flags
);
4912 hole_em
->start
= cur_offset
;
4913 hole_em
->len
= hole_size
;
4914 hole_em
->orig_start
= cur_offset
;
4916 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4917 hole_em
->block_len
= 0;
4918 hole_em
->orig_block_len
= 0;
4919 hole_em
->ram_bytes
= hole_size
;
4920 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4921 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4922 hole_em
->generation
= root
->fs_info
->generation
;
4925 write_lock(&em_tree
->lock
);
4926 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4927 write_unlock(&em_tree
->lock
);
4930 btrfs_drop_extent_cache(inode
, cur_offset
,
4934 free_extent_map(hole_em
);
4937 free_extent_map(em
);
4939 cur_offset
= last_byte
;
4940 if (cur_offset
>= block_end
)
4943 free_extent_map(em
);
4944 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4949 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4951 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4952 struct btrfs_trans_handle
*trans
;
4953 loff_t oldsize
= i_size_read(inode
);
4954 loff_t newsize
= attr
->ia_size
;
4955 int mask
= attr
->ia_valid
;
4959 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4960 * special case where we need to update the times despite not having
4961 * these flags set. For all other operations the VFS set these flags
4962 * explicitly if it wants a timestamp update.
4964 if (newsize
!= oldsize
) {
4965 inode_inc_iversion(inode
);
4966 if (!(mask
& (ATTR_CTIME
| ATTR_MTIME
)))
4967 inode
->i_ctime
= inode
->i_mtime
=
4968 current_fs_time(inode
->i_sb
);
4971 if (newsize
> oldsize
) {
4973 * Don't do an expanding truncate while snapshoting is ongoing.
4974 * This is to ensure the snapshot captures a fully consistent
4975 * state of this file - if the snapshot captures this expanding
4976 * truncation, it must capture all writes that happened before
4979 btrfs_wait_for_snapshot_creation(root
);
4980 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4982 btrfs_end_write_no_snapshoting(root
);
4986 trans
= btrfs_start_transaction(root
, 1);
4987 if (IS_ERR(trans
)) {
4988 btrfs_end_write_no_snapshoting(root
);
4989 return PTR_ERR(trans
);
4992 i_size_write(inode
, newsize
);
4993 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4994 pagecache_isize_extended(inode
, oldsize
, newsize
);
4995 ret
= btrfs_update_inode(trans
, root
, inode
);
4996 btrfs_end_write_no_snapshoting(root
);
4997 btrfs_end_transaction(trans
, root
);
5001 * We're truncating a file that used to have good data down to
5002 * zero. Make sure it gets into the ordered flush list so that
5003 * any new writes get down to disk quickly.
5006 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
5007 &BTRFS_I(inode
)->runtime_flags
);
5010 * 1 for the orphan item we're going to add
5011 * 1 for the orphan item deletion.
5013 trans
= btrfs_start_transaction(root
, 2);
5015 return PTR_ERR(trans
);
5018 * We need to do this in case we fail at _any_ point during the
5019 * actual truncate. Once we do the truncate_setsize we could
5020 * invalidate pages which forces any outstanding ordered io to
5021 * be instantly completed which will give us extents that need
5022 * to be truncated. If we fail to get an orphan inode down we
5023 * could have left over extents that were never meant to live,
5024 * so we need to guarantee from this point on that everything
5025 * will be consistent.
5027 ret
= btrfs_orphan_add(trans
, inode
);
5028 btrfs_end_transaction(trans
, root
);
5032 /* we don't support swapfiles, so vmtruncate shouldn't fail */
5033 truncate_setsize(inode
, newsize
);
5035 /* Disable nonlocked read DIO to avoid the end less truncate */
5036 btrfs_inode_block_unlocked_dio(inode
);
5037 inode_dio_wait(inode
);
5038 btrfs_inode_resume_unlocked_dio(inode
);
5040 ret
= btrfs_truncate(inode
);
5041 if (ret
&& inode
->i_nlink
) {
5045 * failed to truncate, disk_i_size is only adjusted down
5046 * as we remove extents, so it should represent the true
5047 * size of the inode, so reset the in memory size and
5048 * delete our orphan entry.
5050 trans
= btrfs_join_transaction(root
);
5051 if (IS_ERR(trans
)) {
5052 btrfs_orphan_del(NULL
, inode
);
5055 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
5056 err
= btrfs_orphan_del(trans
, inode
);
5058 btrfs_abort_transaction(trans
, err
);
5059 btrfs_end_transaction(trans
, root
);
5066 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
5068 struct inode
*inode
= d_inode(dentry
);
5069 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5072 if (btrfs_root_readonly(root
))
5075 err
= inode_change_ok(inode
, attr
);
5079 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
5080 err
= btrfs_setsize(inode
, attr
);
5085 if (attr
->ia_valid
) {
5086 setattr_copy(inode
, attr
);
5087 inode_inc_iversion(inode
);
5088 err
= btrfs_dirty_inode(inode
);
5090 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
5091 err
= posix_acl_chmod(inode
, inode
->i_mode
);
5098 * While truncating the inode pages during eviction, we get the VFS calling
5099 * btrfs_invalidatepage() against each page of the inode. This is slow because
5100 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5101 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5102 * extent_state structures over and over, wasting lots of time.
5104 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5105 * those expensive operations on a per page basis and do only the ordered io
5106 * finishing, while we release here the extent_map and extent_state structures,
5107 * without the excessive merging and splitting.
5109 static void evict_inode_truncate_pages(struct inode
*inode
)
5111 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5112 struct extent_map_tree
*map_tree
= &BTRFS_I(inode
)->extent_tree
;
5113 struct rb_node
*node
;
5115 ASSERT(inode
->i_state
& I_FREEING
);
5116 truncate_inode_pages_final(&inode
->i_data
);
5118 write_lock(&map_tree
->lock
);
5119 while (!RB_EMPTY_ROOT(&map_tree
->map
)) {
5120 struct extent_map
*em
;
5122 node
= rb_first(&map_tree
->map
);
5123 em
= rb_entry(node
, struct extent_map
, rb_node
);
5124 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5125 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
5126 remove_extent_mapping(map_tree
, em
);
5127 free_extent_map(em
);
5128 if (need_resched()) {
5129 write_unlock(&map_tree
->lock
);
5131 write_lock(&map_tree
->lock
);
5134 write_unlock(&map_tree
->lock
);
5137 * Keep looping until we have no more ranges in the io tree.
5138 * We can have ongoing bios started by readpages (called from readahead)
5139 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5140 * still in progress (unlocked the pages in the bio but did not yet
5141 * unlocked the ranges in the io tree). Therefore this means some
5142 * ranges can still be locked and eviction started because before
5143 * submitting those bios, which are executed by a separate task (work
5144 * queue kthread), inode references (inode->i_count) were not taken
5145 * (which would be dropped in the end io callback of each bio).
5146 * Therefore here we effectively end up waiting for those bios and
5147 * anyone else holding locked ranges without having bumped the inode's
5148 * reference count - if we don't do it, when they access the inode's
5149 * io_tree to unlock a range it may be too late, leading to an
5150 * use-after-free issue.
5152 spin_lock(&io_tree
->lock
);
5153 while (!RB_EMPTY_ROOT(&io_tree
->state
)) {
5154 struct extent_state
*state
;
5155 struct extent_state
*cached_state
= NULL
;
5159 node
= rb_first(&io_tree
->state
);
5160 state
= rb_entry(node
, struct extent_state
, rb_node
);
5161 start
= state
->start
;
5163 spin_unlock(&io_tree
->lock
);
5165 lock_extent_bits(io_tree
, start
, end
, &cached_state
);
5168 * If still has DELALLOC flag, the extent didn't reach disk,
5169 * and its reserved space won't be freed by delayed_ref.
5170 * So we need to free its reserved space here.
5171 * (Refer to comment in btrfs_invalidatepage, case 2)
5173 * Note, end is the bytenr of last byte, so we need + 1 here.
5175 if (state
->state
& EXTENT_DELALLOC
)
5176 btrfs_qgroup_free_data(inode
, start
, end
- start
+ 1);
5178 clear_extent_bit(io_tree
, start
, end
,
5179 EXTENT_LOCKED
| EXTENT_DIRTY
|
5180 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
5181 EXTENT_DEFRAG
, 1, 1,
5182 &cached_state
, GFP_NOFS
);
5185 spin_lock(&io_tree
->lock
);
5187 spin_unlock(&io_tree
->lock
);
5190 void btrfs_evict_inode(struct inode
*inode
)
5192 struct btrfs_trans_handle
*trans
;
5193 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5194 struct btrfs_block_rsv
*rsv
, *global_rsv
;
5195 int steal_from_global
= 0;
5199 trace_btrfs_inode_evict(inode
);
5202 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
5206 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
5208 evict_inode_truncate_pages(inode
);
5210 if (inode
->i_nlink
&&
5211 ((btrfs_root_refs(&root
->root_item
) != 0 &&
5212 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
5213 btrfs_is_free_space_inode(inode
)))
5216 if (is_bad_inode(inode
)) {
5217 btrfs_orphan_del(NULL
, inode
);
5220 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
5221 if (!special_file(inode
->i_mode
))
5222 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
5224 btrfs_free_io_failure_record(inode
, 0, (u64
)-1);
5226 if (root
->fs_info
->log_root_recovering
) {
5227 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
5228 &BTRFS_I(inode
)->runtime_flags
));
5232 if (inode
->i_nlink
> 0) {
5233 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
5234 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
5238 ret
= btrfs_commit_inode_delayed_inode(inode
);
5240 btrfs_orphan_del(NULL
, inode
);
5244 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
5246 btrfs_orphan_del(NULL
, inode
);
5249 rsv
->size
= min_size
;
5251 global_rsv
= &root
->fs_info
->global_block_rsv
;
5253 btrfs_i_size_write(inode
, 0);
5256 * This is a bit simpler than btrfs_truncate since we've already
5257 * reserved our space for our orphan item in the unlink, so we just
5258 * need to reserve some slack space in case we add bytes and update
5259 * inode item when doing the truncate.
5262 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
5263 BTRFS_RESERVE_FLUSH_LIMIT
);
5266 * Try and steal from the global reserve since we will
5267 * likely not use this space anyway, we want to try as
5268 * hard as possible to get this to work.
5271 steal_from_global
++;
5273 steal_from_global
= 0;
5277 * steal_from_global == 0: we reserved stuff, hooray!
5278 * steal_from_global == 1: we didn't reserve stuff, boo!
5279 * steal_from_global == 2: we've committed, still not a lot of
5280 * room but maybe we'll have room in the global reserve this
5282 * steal_from_global == 3: abandon all hope!
5284 if (steal_from_global
> 2) {
5285 btrfs_warn(root
->fs_info
,
5286 "Could not get space for a delete, will truncate on mount %d",
5288 btrfs_orphan_del(NULL
, inode
);
5289 btrfs_free_block_rsv(root
, rsv
);
5293 trans
= btrfs_join_transaction(root
);
5294 if (IS_ERR(trans
)) {
5295 btrfs_orphan_del(NULL
, inode
);
5296 btrfs_free_block_rsv(root
, rsv
);
5301 * We can't just steal from the global reserve, we need to make
5302 * sure there is room to do it, if not we need to commit and try
5305 if (steal_from_global
) {
5306 if (!btrfs_check_space_for_delayed_refs(trans
, root
))
5307 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
,
5314 * Couldn't steal from the global reserve, we have too much
5315 * pending stuff built up, commit the transaction and try it
5319 ret
= btrfs_commit_transaction(trans
, root
);
5321 btrfs_orphan_del(NULL
, inode
);
5322 btrfs_free_block_rsv(root
, rsv
);
5327 steal_from_global
= 0;
5330 trans
->block_rsv
= rsv
;
5332 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
5333 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
)
5336 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
5337 btrfs_end_transaction(trans
, root
);
5339 btrfs_btree_balance_dirty(root
);
5342 btrfs_free_block_rsv(root
, rsv
);
5345 * Errors here aren't a big deal, it just means we leave orphan items
5346 * in the tree. They will be cleaned up on the next mount.
5349 trans
->block_rsv
= root
->orphan_block_rsv
;
5350 btrfs_orphan_del(trans
, inode
);
5352 btrfs_orphan_del(NULL
, inode
);
5355 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
5356 if (!(root
== root
->fs_info
->tree_root
||
5357 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
5358 btrfs_return_ino(root
, btrfs_ino(inode
));
5360 btrfs_end_transaction(trans
, root
);
5361 btrfs_btree_balance_dirty(root
);
5363 btrfs_remove_delayed_node(inode
);
5368 * this returns the key found in the dir entry in the location pointer.
5369 * If no dir entries were found, location->objectid is 0.
5371 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
5372 struct btrfs_key
*location
)
5374 const char *name
= dentry
->d_name
.name
;
5375 int namelen
= dentry
->d_name
.len
;
5376 struct btrfs_dir_item
*di
;
5377 struct btrfs_path
*path
;
5378 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5381 path
= btrfs_alloc_path();
5385 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
5390 if (IS_ERR_OR_NULL(di
))
5393 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
5395 btrfs_free_path(path
);
5398 location
->objectid
= 0;
5403 * when we hit a tree root in a directory, the btrfs part of the inode
5404 * needs to be changed to reflect the root directory of the tree root. This
5405 * is kind of like crossing a mount point.
5407 static int fixup_tree_root_location(struct btrfs_root
*root
,
5409 struct dentry
*dentry
,
5410 struct btrfs_key
*location
,
5411 struct btrfs_root
**sub_root
)
5413 struct btrfs_path
*path
;
5414 struct btrfs_root
*new_root
;
5415 struct btrfs_root_ref
*ref
;
5416 struct extent_buffer
*leaf
;
5417 struct btrfs_key key
;
5421 path
= btrfs_alloc_path();
5428 key
.objectid
= BTRFS_I(dir
)->root
->root_key
.objectid
;
5429 key
.type
= BTRFS_ROOT_REF_KEY
;
5430 key
.offset
= location
->objectid
;
5432 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
, &key
, path
,
5440 leaf
= path
->nodes
[0];
5441 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
5442 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
5443 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
5446 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
5447 (unsigned long)(ref
+ 1),
5448 dentry
->d_name
.len
);
5452 btrfs_release_path(path
);
5454 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
5455 if (IS_ERR(new_root
)) {
5456 err
= PTR_ERR(new_root
);
5460 *sub_root
= new_root
;
5461 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
5462 location
->type
= BTRFS_INODE_ITEM_KEY
;
5463 location
->offset
= 0;
5466 btrfs_free_path(path
);
5470 static void inode_tree_add(struct inode
*inode
)
5472 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5473 struct btrfs_inode
*entry
;
5475 struct rb_node
*parent
;
5476 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
5477 u64 ino
= btrfs_ino(inode
);
5479 if (inode_unhashed(inode
))
5482 spin_lock(&root
->inode_lock
);
5483 p
= &root
->inode_tree
.rb_node
;
5486 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
5488 if (ino
< btrfs_ino(&entry
->vfs_inode
))
5489 p
= &parent
->rb_left
;
5490 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
5491 p
= &parent
->rb_right
;
5493 WARN_ON(!(entry
->vfs_inode
.i_state
&
5494 (I_WILL_FREE
| I_FREEING
)));
5495 rb_replace_node(parent
, new, &root
->inode_tree
);
5496 RB_CLEAR_NODE(parent
);
5497 spin_unlock(&root
->inode_lock
);
5501 rb_link_node(new, parent
, p
);
5502 rb_insert_color(new, &root
->inode_tree
);
5503 spin_unlock(&root
->inode_lock
);
5506 static void inode_tree_del(struct inode
*inode
)
5508 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5511 spin_lock(&root
->inode_lock
);
5512 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
5513 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
5514 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
5515 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5517 spin_unlock(&root
->inode_lock
);
5519 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
5520 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
5521 spin_lock(&root
->inode_lock
);
5522 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5523 spin_unlock(&root
->inode_lock
);
5525 btrfs_add_dead_root(root
);
5529 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
5531 struct rb_node
*node
;
5532 struct rb_node
*prev
;
5533 struct btrfs_inode
*entry
;
5534 struct inode
*inode
;
5537 if (!test_bit(BTRFS_FS_STATE_ERROR
, &root
->fs_info
->fs_state
))
5538 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
5540 spin_lock(&root
->inode_lock
);
5542 node
= root
->inode_tree
.rb_node
;
5546 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5548 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
5549 node
= node
->rb_left
;
5550 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
5551 node
= node
->rb_right
;
5557 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
5558 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
5562 prev
= rb_next(prev
);
5566 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5567 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
5568 inode
= igrab(&entry
->vfs_inode
);
5570 spin_unlock(&root
->inode_lock
);
5571 if (atomic_read(&inode
->i_count
) > 1)
5572 d_prune_aliases(inode
);
5574 * btrfs_drop_inode will have it removed from
5575 * the inode cache when its usage count
5580 spin_lock(&root
->inode_lock
);
5584 if (cond_resched_lock(&root
->inode_lock
))
5587 node
= rb_next(node
);
5589 spin_unlock(&root
->inode_lock
);
5592 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
5594 struct btrfs_iget_args
*args
= p
;
5595 inode
->i_ino
= args
->location
->objectid
;
5596 memcpy(&BTRFS_I(inode
)->location
, args
->location
,
5597 sizeof(*args
->location
));
5598 BTRFS_I(inode
)->root
= args
->root
;
5602 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
5604 struct btrfs_iget_args
*args
= opaque
;
5605 return args
->location
->objectid
== BTRFS_I(inode
)->location
.objectid
&&
5606 args
->root
== BTRFS_I(inode
)->root
;
5609 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
5610 struct btrfs_key
*location
,
5611 struct btrfs_root
*root
)
5613 struct inode
*inode
;
5614 struct btrfs_iget_args args
;
5615 unsigned long hashval
= btrfs_inode_hash(location
->objectid
, root
);
5617 args
.location
= location
;
5620 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
5621 btrfs_init_locked_inode
,
5626 /* Get an inode object given its location and corresponding root.
5627 * Returns in *is_new if the inode was read from disk
5629 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
5630 struct btrfs_root
*root
, int *new)
5632 struct inode
*inode
;
5634 inode
= btrfs_iget_locked(s
, location
, root
);
5636 return ERR_PTR(-ENOMEM
);
5638 if (inode
->i_state
& I_NEW
) {
5641 ret
= btrfs_read_locked_inode(inode
);
5642 if (!is_bad_inode(inode
)) {
5643 inode_tree_add(inode
);
5644 unlock_new_inode(inode
);
5648 unlock_new_inode(inode
);
5651 inode
= ERR_PTR(ret
< 0 ? ret
: -ESTALE
);
5658 static struct inode
*new_simple_dir(struct super_block
*s
,
5659 struct btrfs_key
*key
,
5660 struct btrfs_root
*root
)
5662 struct inode
*inode
= new_inode(s
);
5665 return ERR_PTR(-ENOMEM
);
5667 BTRFS_I(inode
)->root
= root
;
5668 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5669 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5671 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5672 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5673 inode
->i_fop
= &simple_dir_operations
;
5674 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5675 inode
->i_mtime
= current_fs_time(inode
->i_sb
);
5676 inode
->i_atime
= inode
->i_mtime
;
5677 inode
->i_ctime
= inode
->i_mtime
;
5678 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
5683 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5685 struct inode
*inode
;
5686 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5687 struct btrfs_root
*sub_root
= root
;
5688 struct btrfs_key location
;
5692 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5693 return ERR_PTR(-ENAMETOOLONG
);
5695 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5697 return ERR_PTR(ret
);
5699 if (location
.objectid
== 0)
5700 return ERR_PTR(-ENOENT
);
5702 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5703 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5707 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5709 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
5710 ret
= fixup_tree_root_location(root
, dir
, dentry
,
5711 &location
, &sub_root
);
5714 inode
= ERR_PTR(ret
);
5716 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5718 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5720 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
5722 if (!IS_ERR(inode
) && root
!= sub_root
) {
5723 down_read(&root
->fs_info
->cleanup_work_sem
);
5724 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5725 ret
= btrfs_orphan_cleanup(sub_root
);
5726 up_read(&root
->fs_info
->cleanup_work_sem
);
5729 inode
= ERR_PTR(ret
);
5736 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5738 struct btrfs_root
*root
;
5739 struct inode
*inode
= d_inode(dentry
);
5741 if (!inode
&& !IS_ROOT(dentry
))
5742 inode
= d_inode(dentry
->d_parent
);
5745 root
= BTRFS_I(inode
)->root
;
5746 if (btrfs_root_refs(&root
->root_item
) == 0)
5749 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5755 static void btrfs_dentry_release(struct dentry
*dentry
)
5757 kfree(dentry
->d_fsdata
);
5760 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5763 struct inode
*inode
;
5765 inode
= btrfs_lookup_dentry(dir
, dentry
);
5766 if (IS_ERR(inode
)) {
5767 if (PTR_ERR(inode
) == -ENOENT
)
5770 return ERR_CAST(inode
);
5773 return d_splice_alias(inode
, dentry
);
5776 unsigned char btrfs_filetype_table
[] = {
5777 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5780 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
5782 struct inode
*inode
= file_inode(file
);
5783 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5784 struct btrfs_item
*item
;
5785 struct btrfs_dir_item
*di
;
5786 struct btrfs_key key
;
5787 struct btrfs_key found_key
;
5788 struct btrfs_path
*path
;
5789 struct list_head ins_list
;
5790 struct list_head del_list
;
5792 struct extent_buffer
*leaf
;
5794 unsigned char d_type
;
5799 int key_type
= BTRFS_DIR_INDEX_KEY
;
5803 int is_curr
= 0; /* ctx->pos points to the current index? */
5807 /* FIXME, use a real flag for deciding about the key type */
5808 if (root
->fs_info
->tree_root
== root
)
5809 key_type
= BTRFS_DIR_ITEM_KEY
;
5811 if (!dir_emit_dots(file
, ctx
))
5814 path
= btrfs_alloc_path();
5818 path
->reada
= READA_FORWARD
;
5820 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5821 INIT_LIST_HEAD(&ins_list
);
5822 INIT_LIST_HEAD(&del_list
);
5823 put
= btrfs_readdir_get_delayed_items(inode
, &ins_list
,
5827 key
.type
= key_type
;
5828 key
.offset
= ctx
->pos
;
5829 key
.objectid
= btrfs_ino(inode
);
5831 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5837 leaf
= path
->nodes
[0];
5838 slot
= path
->slots
[0];
5839 if (slot
>= btrfs_header_nritems(leaf
)) {
5840 ret
= btrfs_next_leaf(root
, path
);
5848 item
= btrfs_item_nr(slot
);
5849 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5851 if (found_key
.objectid
!= key
.objectid
)
5853 if (found_key
.type
!= key_type
)
5855 if (found_key
.offset
< ctx
->pos
)
5857 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5858 btrfs_should_delete_dir_index(&del_list
,
5862 ctx
->pos
= found_key
.offset
;
5865 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5867 di_total
= btrfs_item_size(leaf
, item
);
5869 while (di_cur
< di_total
) {
5870 struct btrfs_key location
;
5872 if (verify_dir_item(root
, leaf
, di
))
5875 name_len
= btrfs_dir_name_len(leaf
, di
);
5876 if (name_len
<= sizeof(tmp_name
)) {
5877 name_ptr
= tmp_name
;
5879 name_ptr
= kmalloc(name_len
, GFP_KERNEL
);
5885 read_extent_buffer(leaf
, name_ptr
,
5886 (unsigned long)(di
+ 1), name_len
);
5888 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5889 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5892 /* is this a reference to our own snapshot? If so
5895 * In contrast to old kernels, we insert the snapshot's
5896 * dir item and dir index after it has been created, so
5897 * we won't find a reference to our own snapshot. We
5898 * still keep the following code for backward
5901 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5902 location
.objectid
== root
->root_key
.objectid
) {
5906 over
= !dir_emit(ctx
, name_ptr
, name_len
,
5907 location
.objectid
, d_type
);
5910 if (name_ptr
!= tmp_name
)
5916 di_len
= btrfs_dir_name_len(leaf
, di
) +
5917 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5919 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5925 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5928 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
, &emitted
);
5934 * If we haven't emitted any dir entry, we must not touch ctx->pos as
5935 * it was was set to the termination value in previous call. We assume
5936 * that "." and ".." were emitted if we reach this point and set the
5937 * termination value as well for an empty directory.
5939 if (ctx
->pos
> 2 && !emitted
)
5942 /* Reached end of directory/root. Bump pos past the last item. */
5946 * Stop new entries from being returned after we return the last
5949 * New directory entries are assigned a strictly increasing
5950 * offset. This means that new entries created during readdir
5951 * are *guaranteed* to be seen in the future by that readdir.
5952 * This has broken buggy programs which operate on names as
5953 * they're returned by readdir. Until we re-use freed offsets
5954 * we have this hack to stop new entries from being returned
5955 * under the assumption that they'll never reach this huge
5958 * This is being careful not to overflow 32bit loff_t unless the
5959 * last entry requires it because doing so has broken 32bit apps
5962 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5963 if (ctx
->pos
>= INT_MAX
)
5964 ctx
->pos
= LLONG_MAX
;
5972 btrfs_readdir_put_delayed_items(inode
, &ins_list
, &del_list
);
5973 btrfs_free_path(path
);
5977 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5979 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5980 struct btrfs_trans_handle
*trans
;
5982 bool nolock
= false;
5984 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5987 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5990 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5992 trans
= btrfs_join_transaction_nolock(root
);
5994 trans
= btrfs_join_transaction(root
);
5996 return PTR_ERR(trans
);
5997 ret
= btrfs_commit_transaction(trans
, root
);
6003 * This is somewhat expensive, updating the tree every time the
6004 * inode changes. But, it is most likely to find the inode in cache.
6005 * FIXME, needs more benchmarking...there are no reasons other than performance
6006 * to keep or drop this code.
6008 static int btrfs_dirty_inode(struct inode
*inode
)
6010 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6011 struct btrfs_trans_handle
*trans
;
6014 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
6017 trans
= btrfs_join_transaction(root
);
6019 return PTR_ERR(trans
);
6021 ret
= btrfs_update_inode(trans
, root
, inode
);
6022 if (ret
&& ret
== -ENOSPC
) {
6023 /* whoops, lets try again with the full transaction */
6024 btrfs_end_transaction(trans
, root
);
6025 trans
= btrfs_start_transaction(root
, 1);
6027 return PTR_ERR(trans
);
6029 ret
= btrfs_update_inode(trans
, root
, inode
);
6031 btrfs_end_transaction(trans
, root
);
6032 if (BTRFS_I(inode
)->delayed_node
)
6033 btrfs_balance_delayed_items(root
);
6039 * This is a copy of file_update_time. We need this so we can return error on
6040 * ENOSPC for updating the inode in the case of file write and mmap writes.
6042 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
6045 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6047 if (btrfs_root_readonly(root
))
6050 if (flags
& S_VERSION
)
6051 inode_inc_iversion(inode
);
6052 if (flags
& S_CTIME
)
6053 inode
->i_ctime
= *now
;
6054 if (flags
& S_MTIME
)
6055 inode
->i_mtime
= *now
;
6056 if (flags
& S_ATIME
)
6057 inode
->i_atime
= *now
;
6058 return btrfs_dirty_inode(inode
);
6062 * find the highest existing sequence number in a directory
6063 * and then set the in-memory index_cnt variable to reflect
6064 * free sequence numbers
6066 static int btrfs_set_inode_index_count(struct inode
*inode
)
6068 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6069 struct btrfs_key key
, found_key
;
6070 struct btrfs_path
*path
;
6071 struct extent_buffer
*leaf
;
6074 key
.objectid
= btrfs_ino(inode
);
6075 key
.type
= BTRFS_DIR_INDEX_KEY
;
6076 key
.offset
= (u64
)-1;
6078 path
= btrfs_alloc_path();
6082 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6085 /* FIXME: we should be able to handle this */
6091 * MAGIC NUMBER EXPLANATION:
6092 * since we search a directory based on f_pos we have to start at 2
6093 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6094 * else has to start at 2
6096 if (path
->slots
[0] == 0) {
6097 BTRFS_I(inode
)->index_cnt
= 2;
6103 leaf
= path
->nodes
[0];
6104 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6106 if (found_key
.objectid
!= btrfs_ino(inode
) ||
6107 found_key
.type
!= BTRFS_DIR_INDEX_KEY
) {
6108 BTRFS_I(inode
)->index_cnt
= 2;
6112 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
6114 btrfs_free_path(path
);
6119 * helper to find a free sequence number in a given directory. This current
6120 * code is very simple, later versions will do smarter things in the btree
6122 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
6126 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
6127 ret
= btrfs_inode_delayed_dir_index_count(dir
);
6129 ret
= btrfs_set_inode_index_count(dir
);
6135 *index
= BTRFS_I(dir
)->index_cnt
;
6136 BTRFS_I(dir
)->index_cnt
++;
6141 static int btrfs_insert_inode_locked(struct inode
*inode
)
6143 struct btrfs_iget_args args
;
6144 args
.location
= &BTRFS_I(inode
)->location
;
6145 args
.root
= BTRFS_I(inode
)->root
;
6147 return insert_inode_locked4(inode
,
6148 btrfs_inode_hash(inode
->i_ino
, BTRFS_I(inode
)->root
),
6149 btrfs_find_actor
, &args
);
6152 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
6153 struct btrfs_root
*root
,
6155 const char *name
, int name_len
,
6156 u64 ref_objectid
, u64 objectid
,
6157 umode_t mode
, u64
*index
)
6159 struct inode
*inode
;
6160 struct btrfs_inode_item
*inode_item
;
6161 struct btrfs_key
*location
;
6162 struct btrfs_path
*path
;
6163 struct btrfs_inode_ref
*ref
;
6164 struct btrfs_key key
[2];
6166 int nitems
= name
? 2 : 1;
6170 path
= btrfs_alloc_path();
6172 return ERR_PTR(-ENOMEM
);
6174 inode
= new_inode(root
->fs_info
->sb
);
6176 btrfs_free_path(path
);
6177 return ERR_PTR(-ENOMEM
);
6181 * O_TMPFILE, set link count to 0, so that after this point,
6182 * we fill in an inode item with the correct link count.
6185 set_nlink(inode
, 0);
6188 * we have to initialize this early, so we can reclaim the inode
6189 * number if we fail afterwards in this function.
6191 inode
->i_ino
= objectid
;
6194 trace_btrfs_inode_request(dir
);
6196 ret
= btrfs_set_inode_index(dir
, index
);
6198 btrfs_free_path(path
);
6200 return ERR_PTR(ret
);
6206 * index_cnt is ignored for everything but a dir,
6207 * btrfs_get_inode_index_count has an explanation for the magic
6210 BTRFS_I(inode
)->index_cnt
= 2;
6211 BTRFS_I(inode
)->dir_index
= *index
;
6212 BTRFS_I(inode
)->root
= root
;
6213 BTRFS_I(inode
)->generation
= trans
->transid
;
6214 inode
->i_generation
= BTRFS_I(inode
)->generation
;
6217 * We could have gotten an inode number from somebody who was fsynced
6218 * and then removed in this same transaction, so let's just set full
6219 * sync since it will be a full sync anyway and this will blow away the
6220 * old info in the log.
6222 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
6224 key
[0].objectid
= objectid
;
6225 key
[0].type
= BTRFS_INODE_ITEM_KEY
;
6228 sizes
[0] = sizeof(struct btrfs_inode_item
);
6232 * Start new inodes with an inode_ref. This is slightly more
6233 * efficient for small numbers of hard links since they will
6234 * be packed into one item. Extended refs will kick in if we
6235 * add more hard links than can fit in the ref item.
6237 key
[1].objectid
= objectid
;
6238 key
[1].type
= BTRFS_INODE_REF_KEY
;
6239 key
[1].offset
= ref_objectid
;
6241 sizes
[1] = name_len
+ sizeof(*ref
);
6244 location
= &BTRFS_I(inode
)->location
;
6245 location
->objectid
= objectid
;
6246 location
->offset
= 0;
6247 location
->type
= BTRFS_INODE_ITEM_KEY
;
6249 ret
= btrfs_insert_inode_locked(inode
);
6253 path
->leave_spinning
= 1;
6254 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, nitems
);
6258 inode_init_owner(inode
, dir
, mode
);
6259 inode_set_bytes(inode
, 0);
6261 inode
->i_mtime
= current_fs_time(inode
->i_sb
);
6262 inode
->i_atime
= inode
->i_mtime
;
6263 inode
->i_ctime
= inode
->i_mtime
;
6264 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
6266 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
6267 struct btrfs_inode_item
);
6268 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
6269 sizeof(*inode_item
));
6270 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
6273 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
6274 struct btrfs_inode_ref
);
6275 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
6276 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
6277 ptr
= (unsigned long)(ref
+ 1);
6278 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
6281 btrfs_mark_buffer_dirty(path
->nodes
[0]);
6282 btrfs_free_path(path
);
6284 btrfs_inherit_iflags(inode
, dir
);
6286 if (S_ISREG(mode
)) {
6287 if (btrfs_test_opt(root
->fs_info
, NODATASUM
))
6288 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
6289 if (btrfs_test_opt(root
->fs_info
, NODATACOW
))
6290 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
6291 BTRFS_INODE_NODATASUM
;
6294 inode_tree_add(inode
);
6296 trace_btrfs_inode_new(inode
);
6297 btrfs_set_inode_last_trans(trans
, inode
);
6299 btrfs_update_root_times(trans
, root
);
6301 ret
= btrfs_inode_inherit_props(trans
, inode
, dir
);
6303 btrfs_err(root
->fs_info
,
6304 "error inheriting props for ino %llu (root %llu): %d",
6305 btrfs_ino(inode
), root
->root_key
.objectid
, ret
);
6310 unlock_new_inode(inode
);
6313 BTRFS_I(dir
)->index_cnt
--;
6314 btrfs_free_path(path
);
6316 return ERR_PTR(ret
);
6319 static inline u8
btrfs_inode_type(struct inode
*inode
)
6321 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
6325 * utility function to add 'inode' into 'parent_inode' with
6326 * a give name and a given sequence number.
6327 * if 'add_backref' is true, also insert a backref from the
6328 * inode to the parent directory.
6330 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
6331 struct inode
*parent_inode
, struct inode
*inode
,
6332 const char *name
, int name_len
, int add_backref
, u64 index
)
6335 struct btrfs_key key
;
6336 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
6337 u64 ino
= btrfs_ino(inode
);
6338 u64 parent_ino
= btrfs_ino(parent_inode
);
6340 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6341 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
6344 key
.type
= BTRFS_INODE_ITEM_KEY
;
6348 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6349 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
6350 key
.objectid
, root
->root_key
.objectid
,
6351 parent_ino
, index
, name
, name_len
);
6352 } else if (add_backref
) {
6353 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
6357 /* Nothing to clean up yet */
6361 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
6363 btrfs_inode_type(inode
), index
);
6364 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
6367 btrfs_abort_transaction(trans
, ret
);
6371 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
6373 inode_inc_iversion(parent_inode
);
6374 parent_inode
->i_mtime
= parent_inode
->i_ctime
=
6375 current_fs_time(parent_inode
->i_sb
);
6376 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
6378 btrfs_abort_transaction(trans
, ret
);
6382 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6385 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
6386 key
.objectid
, root
->root_key
.objectid
,
6387 parent_ino
, &local_index
, name
, name_len
);
6389 } else if (add_backref
) {
6393 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
6394 ino
, parent_ino
, &local_index
);
6399 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
6400 struct inode
*dir
, struct dentry
*dentry
,
6401 struct inode
*inode
, int backref
, u64 index
)
6403 int err
= btrfs_add_link(trans
, dir
, inode
,
6404 dentry
->d_name
.name
, dentry
->d_name
.len
,
6411 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
6412 umode_t mode
, dev_t rdev
)
6414 struct btrfs_trans_handle
*trans
;
6415 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6416 struct inode
*inode
= NULL
;
6423 * 2 for inode item and ref
6425 * 1 for xattr if selinux is on
6427 trans
= btrfs_start_transaction(root
, 5);
6429 return PTR_ERR(trans
);
6431 err
= btrfs_find_free_ino(root
, &objectid
);
6435 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6436 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6438 if (IS_ERR(inode
)) {
6439 err
= PTR_ERR(inode
);
6444 * If the active LSM wants to access the inode during
6445 * d_instantiate it needs these. Smack checks to see
6446 * if the filesystem supports xattrs by looking at the
6449 inode
->i_op
= &btrfs_special_inode_operations
;
6450 init_special_inode(inode
, inode
->i_mode
, rdev
);
6452 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6454 goto out_unlock_inode
;
6456 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6458 goto out_unlock_inode
;
6460 btrfs_update_inode(trans
, root
, inode
);
6461 unlock_new_inode(inode
);
6462 d_instantiate(dentry
, inode
);
6466 btrfs_end_transaction(trans
, root
);
6467 btrfs_balance_delayed_items(root
);
6468 btrfs_btree_balance_dirty(root
);
6470 inode_dec_link_count(inode
);
6477 unlock_new_inode(inode
);
6482 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
6483 umode_t mode
, bool excl
)
6485 struct btrfs_trans_handle
*trans
;
6486 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6487 struct inode
*inode
= NULL
;
6488 int drop_inode_on_err
= 0;
6494 * 2 for inode item and ref
6496 * 1 for xattr if selinux is on
6498 trans
= btrfs_start_transaction(root
, 5);
6500 return PTR_ERR(trans
);
6502 err
= btrfs_find_free_ino(root
, &objectid
);
6506 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6507 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6509 if (IS_ERR(inode
)) {
6510 err
= PTR_ERR(inode
);
6513 drop_inode_on_err
= 1;
6515 * If the active LSM wants to access the inode during
6516 * d_instantiate it needs these. Smack checks to see
6517 * if the filesystem supports xattrs by looking at the
6520 inode
->i_fop
= &btrfs_file_operations
;
6521 inode
->i_op
= &btrfs_file_inode_operations
;
6522 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6524 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6526 goto out_unlock_inode
;
6528 err
= btrfs_update_inode(trans
, root
, inode
);
6530 goto out_unlock_inode
;
6532 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6534 goto out_unlock_inode
;
6536 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6537 unlock_new_inode(inode
);
6538 d_instantiate(dentry
, inode
);
6541 btrfs_end_transaction(trans
, root
);
6542 if (err
&& drop_inode_on_err
) {
6543 inode_dec_link_count(inode
);
6546 btrfs_balance_delayed_items(root
);
6547 btrfs_btree_balance_dirty(root
);
6551 unlock_new_inode(inode
);
6556 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
6557 struct dentry
*dentry
)
6559 struct btrfs_trans_handle
*trans
= NULL
;
6560 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6561 struct inode
*inode
= d_inode(old_dentry
);
6566 /* do not allow sys_link's with other subvols of the same device */
6567 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
6570 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
6573 err
= btrfs_set_inode_index(dir
, &index
);
6578 * 2 items for inode and inode ref
6579 * 2 items for dir items
6580 * 1 item for parent inode
6582 trans
= btrfs_start_transaction(root
, 5);
6583 if (IS_ERR(trans
)) {
6584 err
= PTR_ERR(trans
);
6589 /* There are several dir indexes for this inode, clear the cache. */
6590 BTRFS_I(inode
)->dir_index
= 0ULL;
6592 inode_inc_iversion(inode
);
6593 inode
->i_ctime
= current_fs_time(inode
->i_sb
);
6595 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
6597 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
6602 struct dentry
*parent
= dentry
->d_parent
;
6603 err
= btrfs_update_inode(trans
, root
, inode
);
6606 if (inode
->i_nlink
== 1) {
6608 * If new hard link count is 1, it's a file created
6609 * with open(2) O_TMPFILE flag.
6611 err
= btrfs_orphan_del(trans
, inode
);
6615 d_instantiate(dentry
, inode
);
6616 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
6619 btrfs_balance_delayed_items(root
);
6622 btrfs_end_transaction(trans
, root
);
6624 inode_dec_link_count(inode
);
6627 btrfs_btree_balance_dirty(root
);
6631 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
6633 struct inode
*inode
= NULL
;
6634 struct btrfs_trans_handle
*trans
;
6635 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6637 int drop_on_err
= 0;
6642 * 2 items for inode and ref
6643 * 2 items for dir items
6644 * 1 for xattr if selinux is on
6646 trans
= btrfs_start_transaction(root
, 5);
6648 return PTR_ERR(trans
);
6650 err
= btrfs_find_free_ino(root
, &objectid
);
6654 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6655 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6656 S_IFDIR
| mode
, &index
);
6657 if (IS_ERR(inode
)) {
6658 err
= PTR_ERR(inode
);
6663 /* these must be set before we unlock the inode */
6664 inode
->i_op
= &btrfs_dir_inode_operations
;
6665 inode
->i_fop
= &btrfs_dir_file_operations
;
6667 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6669 goto out_fail_inode
;
6671 btrfs_i_size_write(inode
, 0);
6672 err
= btrfs_update_inode(trans
, root
, inode
);
6674 goto out_fail_inode
;
6676 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
6677 dentry
->d_name
.len
, 0, index
);
6679 goto out_fail_inode
;
6681 d_instantiate(dentry
, inode
);
6683 * mkdir is special. We're unlocking after we call d_instantiate
6684 * to avoid a race with nfsd calling d_instantiate.
6686 unlock_new_inode(inode
);
6690 btrfs_end_transaction(trans
, root
);
6692 inode_dec_link_count(inode
);
6695 btrfs_balance_delayed_items(root
);
6696 btrfs_btree_balance_dirty(root
);
6700 unlock_new_inode(inode
);
6704 /* Find next extent map of a given extent map, caller needs to ensure locks */
6705 static struct extent_map
*next_extent_map(struct extent_map
*em
)
6707 struct rb_node
*next
;
6709 next
= rb_next(&em
->rb_node
);
6712 return container_of(next
, struct extent_map
, rb_node
);
6715 static struct extent_map
*prev_extent_map(struct extent_map
*em
)
6717 struct rb_node
*prev
;
6719 prev
= rb_prev(&em
->rb_node
);
6722 return container_of(prev
, struct extent_map
, rb_node
);
6725 /* helper for btfs_get_extent. Given an existing extent in the tree,
6726 * the existing extent is the nearest extent to map_start,
6727 * and an extent that you want to insert, deal with overlap and insert
6728 * the best fitted new extent into the tree.
6730 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
6731 struct extent_map
*existing
,
6732 struct extent_map
*em
,
6735 struct extent_map
*prev
;
6736 struct extent_map
*next
;
6741 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
6743 if (existing
->start
> map_start
) {
6745 prev
= prev_extent_map(next
);
6748 next
= next_extent_map(prev
);
6751 start
= prev
? extent_map_end(prev
) : em
->start
;
6752 start
= max_t(u64
, start
, em
->start
);
6753 end
= next
? next
->start
: extent_map_end(em
);
6754 end
= min_t(u64
, end
, extent_map_end(em
));
6755 start_diff
= start
- em
->start
;
6757 em
->len
= end
- start
;
6758 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
6759 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
6760 em
->block_start
+= start_diff
;
6761 em
->block_len
-= start_diff
;
6763 return add_extent_mapping(em_tree
, em
, 0);
6766 static noinline
int uncompress_inline(struct btrfs_path
*path
,
6768 size_t pg_offset
, u64 extent_offset
,
6769 struct btrfs_file_extent_item
*item
)
6772 struct extent_buffer
*leaf
= path
->nodes
[0];
6775 unsigned long inline_size
;
6779 WARN_ON(pg_offset
!= 0);
6780 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6781 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6782 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6783 btrfs_item_nr(path
->slots
[0]));
6784 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6787 ptr
= btrfs_file_extent_inline_start(item
);
6789 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6791 max_size
= min_t(unsigned long, PAGE_SIZE
, max_size
);
6792 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6793 extent_offset
, inline_size
, max_size
);
6799 * a bit scary, this does extent mapping from logical file offset to the disk.
6800 * the ugly parts come from merging extents from the disk with the in-ram
6801 * representation. This gets more complex because of the data=ordered code,
6802 * where the in-ram extents might be locked pending data=ordered completion.
6804 * This also copies inline extents directly into the page.
6807 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
6808 size_t pg_offset
, u64 start
, u64 len
,
6813 u64 extent_start
= 0;
6815 u64 objectid
= btrfs_ino(inode
);
6817 struct btrfs_path
*path
= NULL
;
6818 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6819 struct btrfs_file_extent_item
*item
;
6820 struct extent_buffer
*leaf
;
6821 struct btrfs_key found_key
;
6822 struct extent_map
*em
= NULL
;
6823 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
6824 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6825 struct btrfs_trans_handle
*trans
= NULL
;
6826 const bool new_inline
= !page
|| create
;
6829 read_lock(&em_tree
->lock
);
6830 em
= lookup_extent_mapping(em_tree
, start
, len
);
6832 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6833 read_unlock(&em_tree
->lock
);
6836 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6837 free_extent_map(em
);
6838 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6839 free_extent_map(em
);
6843 em
= alloc_extent_map();
6848 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6849 em
->start
= EXTENT_MAP_HOLE
;
6850 em
->orig_start
= EXTENT_MAP_HOLE
;
6852 em
->block_len
= (u64
)-1;
6855 path
= btrfs_alloc_path();
6861 * Chances are we'll be called again, so go ahead and do
6864 path
->reada
= READA_FORWARD
;
6867 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6868 objectid
, start
, trans
!= NULL
);
6875 if (path
->slots
[0] == 0)
6880 leaf
= path
->nodes
[0];
6881 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6882 struct btrfs_file_extent_item
);
6883 /* are we inside the extent that was found? */
6884 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6885 found_type
= found_key
.type
;
6886 if (found_key
.objectid
!= objectid
||
6887 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6889 * If we backup past the first extent we want to move forward
6890 * and see if there is an extent in front of us, otherwise we'll
6891 * say there is a hole for our whole search range which can
6898 found_type
= btrfs_file_extent_type(leaf
, item
);
6899 extent_start
= found_key
.offset
;
6900 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6901 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6902 extent_end
= extent_start
+
6903 btrfs_file_extent_num_bytes(leaf
, item
);
6904 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6906 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6907 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6910 if (start
>= extent_end
) {
6912 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6913 ret
= btrfs_next_leaf(root
, path
);
6920 leaf
= path
->nodes
[0];
6922 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6923 if (found_key
.objectid
!= objectid
||
6924 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6926 if (start
+ len
<= found_key
.offset
)
6928 if (start
> found_key
.offset
)
6931 em
->orig_start
= start
;
6932 em
->len
= found_key
.offset
- start
;
6936 btrfs_extent_item_to_extent_map(inode
, path
, item
, new_inline
, em
);
6938 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6939 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6941 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6945 size_t extent_offset
;
6951 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6952 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6953 copy_size
= min_t(u64
, PAGE_SIZE
- pg_offset
,
6954 size
- extent_offset
);
6955 em
->start
= extent_start
+ extent_offset
;
6956 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6957 em
->orig_block_len
= em
->len
;
6958 em
->orig_start
= em
->start
;
6959 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6960 if (create
== 0 && !PageUptodate(page
)) {
6961 if (btrfs_file_extent_compression(leaf
, item
) !=
6962 BTRFS_COMPRESS_NONE
) {
6963 ret
= uncompress_inline(path
, page
, pg_offset
,
6964 extent_offset
, item
);
6971 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6973 if (pg_offset
+ copy_size
< PAGE_SIZE
) {
6974 memset(map
+ pg_offset
+ copy_size
, 0,
6975 PAGE_SIZE
- pg_offset
-
6980 flush_dcache_page(page
);
6981 } else if (create
&& PageUptodate(page
)) {
6985 free_extent_map(em
);
6988 btrfs_release_path(path
);
6989 trans
= btrfs_join_transaction(root
);
6992 return ERR_CAST(trans
);
6996 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6999 btrfs_mark_buffer_dirty(leaf
);
7001 set_extent_uptodate(io_tree
, em
->start
,
7002 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
7007 em
->orig_start
= start
;
7010 em
->block_start
= EXTENT_MAP_HOLE
;
7011 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
7013 btrfs_release_path(path
);
7014 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
7015 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
7016 em
->start
, em
->len
, start
, len
);
7022 write_lock(&em_tree
->lock
);
7023 ret
= add_extent_mapping(em_tree
, em
, 0);
7024 /* it is possible that someone inserted the extent into the tree
7025 * while we had the lock dropped. It is also possible that
7026 * an overlapping map exists in the tree
7028 if (ret
== -EEXIST
) {
7029 struct extent_map
*existing
;
7033 existing
= search_extent_mapping(em_tree
, start
, len
);
7035 * existing will always be non-NULL, since there must be
7036 * extent causing the -EEXIST.
7038 if (existing
->start
== em
->start
&&
7039 extent_map_end(existing
) == extent_map_end(em
) &&
7040 em
->block_start
== existing
->block_start
) {
7042 * these two extents are the same, it happens
7043 * with inlines especially
7045 free_extent_map(em
);
7049 } else if (start
>= extent_map_end(existing
) ||
7050 start
<= existing
->start
) {
7052 * The existing extent map is the one nearest to
7053 * the [start, start + len) range which overlaps
7055 err
= merge_extent_mapping(em_tree
, existing
,
7057 free_extent_map(existing
);
7059 free_extent_map(em
);
7063 free_extent_map(em
);
7068 write_unlock(&em_tree
->lock
);
7071 trace_btrfs_get_extent(root
, em
);
7073 btrfs_free_path(path
);
7075 ret
= btrfs_end_transaction(trans
, root
);
7080 free_extent_map(em
);
7081 return ERR_PTR(err
);
7083 BUG_ON(!em
); /* Error is always set */
7087 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
7088 size_t pg_offset
, u64 start
, u64 len
,
7091 struct extent_map
*em
;
7092 struct extent_map
*hole_em
= NULL
;
7093 u64 range_start
= start
;
7099 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
7106 * - a pre-alloc extent,
7107 * there might actually be delalloc bytes behind it.
7109 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
7110 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7116 /* check to see if we've wrapped (len == -1 or similar) */
7125 /* ok, we didn't find anything, lets look for delalloc */
7126 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
7127 end
, len
, EXTENT_DELALLOC
, 1);
7128 found_end
= range_start
+ found
;
7129 if (found_end
< range_start
)
7130 found_end
= (u64
)-1;
7133 * we didn't find anything useful, return
7134 * the original results from get_extent()
7136 if (range_start
> end
|| found_end
<= start
) {
7142 /* adjust the range_start to make sure it doesn't
7143 * go backwards from the start they passed in
7145 range_start
= max(start
, range_start
);
7146 found
= found_end
- range_start
;
7149 u64 hole_start
= start
;
7152 em
= alloc_extent_map();
7158 * when btrfs_get_extent can't find anything it
7159 * returns one huge hole
7161 * make sure what it found really fits our range, and
7162 * adjust to make sure it is based on the start from
7166 u64 calc_end
= extent_map_end(hole_em
);
7168 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
7169 free_extent_map(hole_em
);
7172 hole_start
= max(hole_em
->start
, start
);
7173 hole_len
= calc_end
- hole_start
;
7177 if (hole_em
&& range_start
> hole_start
) {
7178 /* our hole starts before our delalloc, so we
7179 * have to return just the parts of the hole
7180 * that go until the delalloc starts
7182 em
->len
= min(hole_len
,
7183 range_start
- hole_start
);
7184 em
->start
= hole_start
;
7185 em
->orig_start
= hole_start
;
7187 * don't adjust block start at all,
7188 * it is fixed at EXTENT_MAP_HOLE
7190 em
->block_start
= hole_em
->block_start
;
7191 em
->block_len
= hole_len
;
7192 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
7193 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
7195 em
->start
= range_start
;
7197 em
->orig_start
= range_start
;
7198 em
->block_start
= EXTENT_MAP_DELALLOC
;
7199 em
->block_len
= found
;
7201 } else if (hole_em
) {
7206 free_extent_map(hole_em
);
7208 free_extent_map(em
);
7209 return ERR_PTR(err
);
7214 static struct extent_map
*btrfs_create_dio_extent(struct inode
*inode
,
7217 const u64 orig_start
,
7218 const u64 block_start
,
7219 const u64 block_len
,
7220 const u64 orig_block_len
,
7221 const u64 ram_bytes
,
7224 struct extent_map
*em
= NULL
;
7227 down_read(&BTRFS_I(inode
)->dio_sem
);
7228 if (type
!= BTRFS_ORDERED_NOCOW
) {
7229 em
= create_pinned_em(inode
, start
, len
, orig_start
,
7230 block_start
, block_len
, orig_block_len
,
7235 ret
= btrfs_add_ordered_extent_dio(inode
, start
, block_start
,
7236 len
, block_len
, type
);
7239 free_extent_map(em
);
7240 btrfs_drop_extent_cache(inode
, start
,
7241 start
+ len
- 1, 0);
7246 up_read(&BTRFS_I(inode
)->dio_sem
);
7251 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
7254 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7255 struct extent_map
*em
;
7256 struct btrfs_key ins
;
7260 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
7261 ret
= btrfs_reserve_extent(root
, len
, len
, root
->sectorsize
, 0,
7262 alloc_hint
, &ins
, 1, 1);
7264 return ERR_PTR(ret
);
7266 em
= btrfs_create_dio_extent(inode
, start
, ins
.offset
, start
,
7267 ins
.objectid
, ins
.offset
, ins
.offset
,
7269 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
7271 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
7277 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7278 * block must be cow'd
7280 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
7281 u64
*orig_start
, u64
*orig_block_len
,
7284 struct btrfs_trans_handle
*trans
;
7285 struct btrfs_path
*path
;
7287 struct extent_buffer
*leaf
;
7288 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7289 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7290 struct btrfs_file_extent_item
*fi
;
7291 struct btrfs_key key
;
7298 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
7300 path
= btrfs_alloc_path();
7304 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, btrfs_ino(inode
),
7309 slot
= path
->slots
[0];
7312 /* can't find the item, must cow */
7319 leaf
= path
->nodes
[0];
7320 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
7321 if (key
.objectid
!= btrfs_ino(inode
) ||
7322 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
7323 /* not our file or wrong item type, must cow */
7327 if (key
.offset
> offset
) {
7328 /* Wrong offset, must cow */
7332 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
7333 found_type
= btrfs_file_extent_type(leaf
, fi
);
7334 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
7335 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
7336 /* not a regular extent, must cow */
7340 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
7343 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
7344 if (extent_end
<= offset
)
7347 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
7348 if (disk_bytenr
== 0)
7351 if (btrfs_file_extent_compression(leaf
, fi
) ||
7352 btrfs_file_extent_encryption(leaf
, fi
) ||
7353 btrfs_file_extent_other_encoding(leaf
, fi
))
7356 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
7359 *orig_start
= key
.offset
- backref_offset
;
7360 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
7361 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
7364 if (btrfs_extent_readonly(root
, disk_bytenr
))
7367 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
7368 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
7371 range_end
= round_up(offset
+ num_bytes
, root
->sectorsize
) - 1;
7372 ret
= test_range_bit(io_tree
, offset
, range_end
,
7373 EXTENT_DELALLOC
, 0, NULL
);
7380 btrfs_release_path(path
);
7383 * look for other files referencing this extent, if we
7384 * find any we must cow
7386 trans
= btrfs_join_transaction(root
);
7387 if (IS_ERR(trans
)) {
7392 ret
= btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
7393 key
.offset
- backref_offset
, disk_bytenr
);
7394 btrfs_end_transaction(trans
, root
);
7401 * adjust disk_bytenr and num_bytes to cover just the bytes
7402 * in this extent we are about to write. If there
7403 * are any csums in that range we have to cow in order
7404 * to keep the csums correct
7406 disk_bytenr
+= backref_offset
;
7407 disk_bytenr
+= offset
- key
.offset
;
7408 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
7411 * all of the above have passed, it is safe to overwrite this extent
7417 btrfs_free_path(path
);
7421 bool btrfs_page_exists_in_range(struct inode
*inode
, loff_t start
, loff_t end
)
7423 struct radix_tree_root
*root
= &inode
->i_mapping
->page_tree
;
7425 void **pagep
= NULL
;
7426 struct page
*page
= NULL
;
7430 start_idx
= start
>> PAGE_SHIFT
;
7433 * end is the last byte in the last page. end == start is legal
7435 end_idx
= end
>> PAGE_SHIFT
;
7439 /* Most of the code in this while loop is lifted from
7440 * find_get_page. It's been modified to begin searching from a
7441 * page and return just the first page found in that range. If the
7442 * found idx is less than or equal to the end idx then we know that
7443 * a page exists. If no pages are found or if those pages are
7444 * outside of the range then we're fine (yay!) */
7445 while (page
== NULL
&&
7446 radix_tree_gang_lookup_slot(root
, &pagep
, NULL
, start_idx
, 1)) {
7447 page
= radix_tree_deref_slot(pagep
);
7448 if (unlikely(!page
))
7451 if (radix_tree_exception(page
)) {
7452 if (radix_tree_deref_retry(page
)) {
7457 * Otherwise, shmem/tmpfs must be storing a swap entry
7458 * here as an exceptional entry: so return it without
7459 * attempting to raise page count.
7462 break; /* TODO: Is this relevant for this use case? */
7465 if (!page_cache_get_speculative(page
)) {
7471 * Has the page moved?
7472 * This is part of the lockless pagecache protocol. See
7473 * include/linux/pagemap.h for details.
7475 if (unlikely(page
!= *pagep
)) {
7482 if (page
->index
<= end_idx
)
7491 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
7492 struct extent_state
**cached_state
, int writing
)
7494 struct btrfs_ordered_extent
*ordered
;
7498 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7501 * We're concerned with the entire range that we're going to be
7502 * doing DIO to, so we need to make sure there's no ordered
7503 * extents in this range.
7505 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
7506 lockend
- lockstart
+ 1);
7509 * We need to make sure there are no buffered pages in this
7510 * range either, we could have raced between the invalidate in
7511 * generic_file_direct_write and locking the extent. The
7512 * invalidate needs to happen so that reads after a write do not
7517 !btrfs_page_exists_in_range(inode
, lockstart
, lockend
)))
7520 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7521 cached_state
, GFP_NOFS
);
7525 * If we are doing a DIO read and the ordered extent we
7526 * found is for a buffered write, we can not wait for it
7527 * to complete and retry, because if we do so we can
7528 * deadlock with concurrent buffered writes on page
7529 * locks. This happens only if our DIO read covers more
7530 * than one extent map, if at this point has already
7531 * created an ordered extent for a previous extent map
7532 * and locked its range in the inode's io tree, and a
7533 * concurrent write against that previous extent map's
7534 * range and this range started (we unlock the ranges
7535 * in the io tree only when the bios complete and
7536 * buffered writes always lock pages before attempting
7537 * to lock range in the io tree).
7540 test_bit(BTRFS_ORDERED_DIRECT
, &ordered
->flags
))
7541 btrfs_start_ordered_extent(inode
, ordered
, 1);
7544 btrfs_put_ordered_extent(ordered
);
7547 * We could trigger writeback for this range (and wait
7548 * for it to complete) and then invalidate the pages for
7549 * this range (through invalidate_inode_pages2_range()),
7550 * but that can lead us to a deadlock with a concurrent
7551 * call to readpages() (a buffered read or a defrag call
7552 * triggered a readahead) on a page lock due to an
7553 * ordered dio extent we created before but did not have
7554 * yet a corresponding bio submitted (whence it can not
7555 * complete), which makes readpages() wait for that
7556 * ordered extent to complete while holding a lock on
7571 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
7572 u64 len
, u64 orig_start
,
7573 u64 block_start
, u64 block_len
,
7574 u64 orig_block_len
, u64 ram_bytes
,
7577 struct extent_map_tree
*em_tree
;
7578 struct extent_map
*em
;
7579 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7582 em_tree
= &BTRFS_I(inode
)->extent_tree
;
7583 em
= alloc_extent_map();
7585 return ERR_PTR(-ENOMEM
);
7588 em
->orig_start
= orig_start
;
7589 em
->mod_start
= start
;
7592 em
->block_len
= block_len
;
7593 em
->block_start
= block_start
;
7594 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
7595 em
->orig_block_len
= orig_block_len
;
7596 em
->ram_bytes
= ram_bytes
;
7597 em
->generation
= -1;
7598 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
7599 if (type
== BTRFS_ORDERED_PREALLOC
)
7600 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
7603 btrfs_drop_extent_cache(inode
, em
->start
,
7604 em
->start
+ em
->len
- 1, 0);
7605 write_lock(&em_tree
->lock
);
7606 ret
= add_extent_mapping(em_tree
, em
, 1);
7607 write_unlock(&em_tree
->lock
);
7608 } while (ret
== -EEXIST
);
7611 free_extent_map(em
);
7612 return ERR_PTR(ret
);
7618 static void adjust_dio_outstanding_extents(struct inode
*inode
,
7619 struct btrfs_dio_data
*dio_data
,
7622 unsigned num_extents
;
7624 num_extents
= (unsigned) div64_u64(len
+ BTRFS_MAX_EXTENT_SIZE
- 1,
7625 BTRFS_MAX_EXTENT_SIZE
);
7627 * If we have an outstanding_extents count still set then we're
7628 * within our reservation, otherwise we need to adjust our inode
7629 * counter appropriately.
7631 if (dio_data
->outstanding_extents
) {
7632 dio_data
->outstanding_extents
-= num_extents
;
7634 spin_lock(&BTRFS_I(inode
)->lock
);
7635 BTRFS_I(inode
)->outstanding_extents
+= num_extents
;
7636 spin_unlock(&BTRFS_I(inode
)->lock
);
7640 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
7641 struct buffer_head
*bh_result
, int create
)
7643 struct extent_map
*em
;
7644 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7645 struct extent_state
*cached_state
= NULL
;
7646 struct btrfs_dio_data
*dio_data
= NULL
;
7647 u64 start
= iblock
<< inode
->i_blkbits
;
7648 u64 lockstart
, lockend
;
7649 u64 len
= bh_result
->b_size
;
7650 int unlock_bits
= EXTENT_LOCKED
;
7654 unlock_bits
|= EXTENT_DIRTY
;
7656 len
= min_t(u64
, len
, root
->sectorsize
);
7659 lockend
= start
+ len
- 1;
7661 if (current
->journal_info
) {
7663 * Need to pull our outstanding extents and set journal_info to NULL so
7664 * that anything that needs to check if there's a transaction doesn't get
7667 dio_data
= current
->journal_info
;
7668 current
->journal_info
= NULL
;
7672 * If this errors out it's because we couldn't invalidate pagecache for
7673 * this range and we need to fallback to buffered.
7675 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
,
7681 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
7688 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7689 * io. INLINE is special, and we could probably kludge it in here, but
7690 * it's still buffered so for safety lets just fall back to the generic
7693 * For COMPRESSED we _have_ to read the entire extent in so we can
7694 * decompress it, so there will be buffering required no matter what we
7695 * do, so go ahead and fallback to buffered.
7697 * We return -ENOTBLK because that's what makes DIO go ahead and go back
7698 * to buffered IO. Don't blame me, this is the price we pay for using
7701 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
7702 em
->block_start
== EXTENT_MAP_INLINE
) {
7703 free_extent_map(em
);
7708 /* Just a good old fashioned hole, return */
7709 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
7710 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
7711 free_extent_map(em
);
7716 * We don't allocate a new extent in the following cases
7718 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7720 * 2) The extent is marked as PREALLOC. We're good to go here and can
7721 * just use the extent.
7725 len
= min(len
, em
->len
- (start
- em
->start
));
7726 lockstart
= start
+ len
;
7730 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
7731 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
7732 em
->block_start
!= EXTENT_MAP_HOLE
)) {
7734 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
7736 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7737 type
= BTRFS_ORDERED_PREALLOC
;
7739 type
= BTRFS_ORDERED_NOCOW
;
7740 len
= min(len
, em
->len
- (start
- em
->start
));
7741 block_start
= em
->block_start
+ (start
- em
->start
);
7743 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
7744 &orig_block_len
, &ram_bytes
) == 1 &&
7745 btrfs_inc_nocow_writers(root
->fs_info
, block_start
)) {
7746 struct extent_map
*em2
;
7748 em2
= btrfs_create_dio_extent(inode
, start
, len
,
7749 orig_start
, block_start
,
7750 len
, orig_block_len
,
7752 btrfs_dec_nocow_writers(root
->fs_info
, block_start
);
7753 if (type
== BTRFS_ORDERED_PREALLOC
) {
7754 free_extent_map(em
);
7757 if (em2
&& IS_ERR(em2
)) {
7762 * For inode marked NODATACOW or extent marked PREALLOC,
7763 * use the existing or preallocated extent, so does not
7764 * need to adjust btrfs_space_info's bytes_may_use.
7766 btrfs_free_reserved_data_space_noquota(inode
,
7773 * this will cow the extent, reset the len in case we changed
7776 len
= bh_result
->b_size
;
7777 free_extent_map(em
);
7778 em
= btrfs_new_extent_direct(inode
, start
, len
);
7783 len
= min(len
, em
->len
- (start
- em
->start
));
7785 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
7787 bh_result
->b_size
= len
;
7788 bh_result
->b_bdev
= em
->bdev
;
7789 set_buffer_mapped(bh_result
);
7791 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7792 set_buffer_new(bh_result
);
7795 * Need to update the i_size under the extent lock so buffered
7796 * readers will get the updated i_size when we unlock.
7798 if (start
+ len
> i_size_read(inode
))
7799 i_size_write(inode
, start
+ len
);
7801 adjust_dio_outstanding_extents(inode
, dio_data
, len
);
7802 WARN_ON(dio_data
->reserve
< len
);
7803 dio_data
->reserve
-= len
;
7804 dio_data
->unsubmitted_oe_range_end
= start
+ len
;
7805 current
->journal_info
= dio_data
;
7809 * In the case of write we need to clear and unlock the entire range,
7810 * in the case of read we need to unlock only the end area that we
7811 * aren't using if there is any left over space.
7813 if (lockstart
< lockend
) {
7814 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
7815 lockend
, unlock_bits
, 1, 0,
7816 &cached_state
, GFP_NOFS
);
7818 free_extent_state(cached_state
);
7821 free_extent_map(em
);
7826 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7827 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
7830 current
->journal_info
= dio_data
;
7832 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7833 * write less data then expected, so that we don't underflow our inode's
7834 * outstanding extents counter.
7836 if (create
&& dio_data
)
7837 adjust_dio_outstanding_extents(inode
, dio_data
, len
);
7842 static inline int submit_dio_repair_bio(struct inode
*inode
, struct bio
*bio
,
7845 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7848 BUG_ON(bio_op(bio
) == REQ_OP_WRITE
);
7852 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
,
7853 BTRFS_WQ_ENDIO_DIO_REPAIR
);
7857 ret
= btrfs_map_bio(root
, bio
, mirror_num
, 0);
7863 static int btrfs_check_dio_repairable(struct inode
*inode
,
7864 struct bio
*failed_bio
,
7865 struct io_failure_record
*failrec
,
7870 num_copies
= btrfs_num_copies(BTRFS_I(inode
)->root
->fs_info
,
7871 failrec
->logical
, failrec
->len
);
7872 if (num_copies
== 1) {
7874 * we only have a single copy of the data, so don't bother with
7875 * all the retry and error correction code that follows. no
7876 * matter what the error is, it is very likely to persist.
7878 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7879 num_copies
, failrec
->this_mirror
, failed_mirror
);
7883 failrec
->failed_mirror
= failed_mirror
;
7884 failrec
->this_mirror
++;
7885 if (failrec
->this_mirror
== failed_mirror
)
7886 failrec
->this_mirror
++;
7888 if (failrec
->this_mirror
> num_copies
) {
7889 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7890 num_copies
, failrec
->this_mirror
, failed_mirror
);
7897 static int dio_read_error(struct inode
*inode
, struct bio
*failed_bio
,
7898 struct page
*page
, unsigned int pgoff
,
7899 u64 start
, u64 end
, int failed_mirror
,
7900 bio_end_io_t
*repair_endio
, void *repair_arg
)
7902 struct io_failure_record
*failrec
;
7908 BUG_ON(bio_op(failed_bio
) == REQ_OP_WRITE
);
7910 ret
= btrfs_get_io_failure_record(inode
, start
, end
, &failrec
);
7914 ret
= btrfs_check_dio_repairable(inode
, failed_bio
, failrec
,
7917 free_io_failure(inode
, failrec
);
7921 if ((failed_bio
->bi_vcnt
> 1)
7922 || (failed_bio
->bi_io_vec
->bv_len
7923 > BTRFS_I(inode
)->root
->sectorsize
))
7924 read_mode
= READ_SYNC
| REQ_FAILFAST_DEV
;
7926 read_mode
= READ_SYNC
;
7928 isector
= start
- btrfs_io_bio(failed_bio
)->logical
;
7929 isector
>>= inode
->i_sb
->s_blocksize_bits
;
7930 bio
= btrfs_create_repair_bio(inode
, failed_bio
, failrec
, page
,
7931 pgoff
, isector
, repair_endio
, repair_arg
);
7933 free_io_failure(inode
, failrec
);
7936 bio_set_op_attrs(bio
, REQ_OP_READ
, read_mode
);
7938 btrfs_debug(BTRFS_I(inode
)->root
->fs_info
,
7939 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7940 read_mode
, failrec
->this_mirror
, failrec
->in_validation
);
7942 ret
= submit_dio_repair_bio(inode
, bio
, failrec
->this_mirror
);
7944 free_io_failure(inode
, failrec
);
7951 struct btrfs_retry_complete
{
7952 struct completion done
;
7953 struct inode
*inode
;
7958 static void btrfs_retry_endio_nocsum(struct bio
*bio
)
7960 struct btrfs_retry_complete
*done
= bio
->bi_private
;
7961 struct inode
*inode
;
7962 struct bio_vec
*bvec
;
7968 ASSERT(bio
->bi_vcnt
== 1);
7969 inode
= bio
->bi_io_vec
->bv_page
->mapping
->host
;
7970 ASSERT(bio
->bi_io_vec
->bv_len
== BTRFS_I(inode
)->root
->sectorsize
);
7973 bio_for_each_segment_all(bvec
, bio
, i
)
7974 clean_io_failure(done
->inode
, done
->start
, bvec
->bv_page
, 0);
7976 complete(&done
->done
);
7980 static int __btrfs_correct_data_nocsum(struct inode
*inode
,
7981 struct btrfs_io_bio
*io_bio
)
7983 struct btrfs_fs_info
*fs_info
;
7984 struct bio_vec
*bvec
;
7985 struct btrfs_retry_complete done
;
7993 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
7994 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
7996 start
= io_bio
->logical
;
7999 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
8000 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
->bv_len
);
8001 pgoff
= bvec
->bv_offset
;
8003 next_block_or_try_again
:
8006 init_completion(&done
.done
);
8008 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
,
8009 pgoff
, start
, start
+ sectorsize
- 1,
8011 btrfs_retry_endio_nocsum
, &done
);
8015 wait_for_completion(&done
.done
);
8017 if (!done
.uptodate
) {
8018 /* We might have another mirror, so try again */
8019 goto next_block_or_try_again
;
8022 start
+= sectorsize
;
8025 pgoff
+= sectorsize
;
8026 goto next_block_or_try_again
;
8033 static void btrfs_retry_endio(struct bio
*bio
)
8035 struct btrfs_retry_complete
*done
= bio
->bi_private
;
8036 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8037 struct inode
*inode
;
8038 struct bio_vec
*bvec
;
8049 start
= done
->start
;
8051 ASSERT(bio
->bi_vcnt
== 1);
8052 inode
= bio
->bi_io_vec
->bv_page
->mapping
->host
;
8053 ASSERT(bio
->bi_io_vec
->bv_len
== BTRFS_I(inode
)->root
->sectorsize
);
8055 bio_for_each_segment_all(bvec
, bio
, i
) {
8056 ret
= __readpage_endio_check(done
->inode
, io_bio
, i
,
8057 bvec
->bv_page
, bvec
->bv_offset
,
8058 done
->start
, bvec
->bv_len
);
8060 clean_io_failure(done
->inode
, done
->start
,
8061 bvec
->bv_page
, bvec
->bv_offset
);
8066 done
->uptodate
= uptodate
;
8068 complete(&done
->done
);
8072 static int __btrfs_subio_endio_read(struct inode
*inode
,
8073 struct btrfs_io_bio
*io_bio
, int err
)
8075 struct btrfs_fs_info
*fs_info
;
8076 struct bio_vec
*bvec
;
8077 struct btrfs_retry_complete done
;
8087 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
8088 sectorsize
= BTRFS_I(inode
)->root
->sectorsize
;
8091 start
= io_bio
->logical
;
8094 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
8095 nr_sectors
= BTRFS_BYTES_TO_BLKS(fs_info
, bvec
->bv_len
);
8097 pgoff
= bvec
->bv_offset
;
8099 csum_pos
= BTRFS_BYTES_TO_BLKS(fs_info
, offset
);
8100 ret
= __readpage_endio_check(inode
, io_bio
, csum_pos
,
8101 bvec
->bv_page
, pgoff
, start
,
8108 init_completion(&done
.done
);
8110 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
,
8111 pgoff
, start
, start
+ sectorsize
- 1,
8113 btrfs_retry_endio
, &done
);
8119 wait_for_completion(&done
.done
);
8121 if (!done
.uptodate
) {
8122 /* We might have another mirror, so try again */
8126 offset
+= sectorsize
;
8127 start
+= sectorsize
;
8132 pgoff
+= sectorsize
;
8140 static int btrfs_subio_endio_read(struct inode
*inode
,
8141 struct btrfs_io_bio
*io_bio
, int err
)
8143 bool skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
8147 return __btrfs_correct_data_nocsum(inode
, io_bio
);
8151 return __btrfs_subio_endio_read(inode
, io_bio
, err
);
8155 static void btrfs_endio_direct_read(struct bio
*bio
)
8157 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8158 struct inode
*inode
= dip
->inode
;
8159 struct bio
*dio_bio
;
8160 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8161 int err
= bio
->bi_error
;
8163 if (dip
->flags
& BTRFS_DIO_ORIG_BIO_SUBMITTED
)
8164 err
= btrfs_subio_endio_read(inode
, io_bio
, err
);
8166 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
8167 dip
->logical_offset
+ dip
->bytes
- 1);
8168 dio_bio
= dip
->dio_bio
;
8172 dio_bio
->bi_error
= bio
->bi_error
;
8173 dio_end_io(dio_bio
, bio
->bi_error
);
8176 io_bio
->end_io(io_bio
, err
);
8180 static void btrfs_endio_direct_write_update_ordered(struct inode
*inode
,
8185 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8186 struct btrfs_ordered_extent
*ordered
= NULL
;
8187 u64 ordered_offset
= offset
;
8188 u64 ordered_bytes
= bytes
;
8192 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
8199 btrfs_init_work(&ordered
->work
, btrfs_endio_write_helper
,
8200 finish_ordered_fn
, NULL
, NULL
);
8201 btrfs_queue_work(root
->fs_info
->endio_write_workers
,
8205 * our bio might span multiple ordered extents. If we haven't
8206 * completed the accounting for the whole dio, go back and try again
8208 if (ordered_offset
< offset
+ bytes
) {
8209 ordered_bytes
= offset
+ bytes
- ordered_offset
;
8215 static void btrfs_endio_direct_write(struct bio
*bio
)
8217 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8218 struct bio
*dio_bio
= dip
->dio_bio
;
8220 btrfs_endio_direct_write_update_ordered(dip
->inode
,
8221 dip
->logical_offset
,
8227 dio_bio
->bi_error
= bio
->bi_error
;
8228 dio_end_io(dio_bio
, bio
->bi_error
);
8232 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
,
8233 struct bio
*bio
, int mirror_num
,
8234 unsigned long bio_flags
, u64 offset
)
8237 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8238 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
8239 BUG_ON(ret
); /* -ENOMEM */
8243 static void btrfs_end_dio_bio(struct bio
*bio
)
8245 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8246 int err
= bio
->bi_error
;
8249 btrfs_warn(BTRFS_I(dip
->inode
)->root
->fs_info
,
8250 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
8251 btrfs_ino(dip
->inode
), bio_op(bio
), bio
->bi_opf
,
8252 (unsigned long long)bio
->bi_iter
.bi_sector
,
8253 bio
->bi_iter
.bi_size
, err
);
8255 if (dip
->subio_endio
)
8256 err
= dip
->subio_endio(dip
->inode
, btrfs_io_bio(bio
), err
);
8262 * before atomic variable goto zero, we must make sure
8263 * dip->errors is perceived to be set.
8265 smp_mb__before_atomic();
8268 /* if there are more bios still pending for this dio, just exit */
8269 if (!atomic_dec_and_test(&dip
->pending_bios
))
8273 bio_io_error(dip
->orig_bio
);
8275 dip
->dio_bio
->bi_error
= 0;
8276 bio_endio(dip
->orig_bio
);
8282 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
8283 u64 first_sector
, gfp_t gfp_flags
)
8286 bio
= btrfs_bio_alloc(bdev
, first_sector
, BIO_MAX_PAGES
, gfp_flags
);
8288 bio_associate_current(bio
);
8292 static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root
*root
,
8293 struct inode
*inode
,
8294 struct btrfs_dio_private
*dip
,
8298 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8299 struct btrfs_io_bio
*orig_io_bio
= btrfs_io_bio(dip
->orig_bio
);
8303 * We load all the csum data we need when we submit
8304 * the first bio to reduce the csum tree search and
8307 if (dip
->logical_offset
== file_offset
) {
8308 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, dip
->orig_bio
,
8314 if (bio
== dip
->orig_bio
)
8317 file_offset
-= dip
->logical_offset
;
8318 file_offset
>>= inode
->i_sb
->s_blocksize_bits
;
8319 io_bio
->csum
= (u8
*)(((u32
*)orig_io_bio
->csum
) + file_offset
);
8324 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
8325 u64 file_offset
, int skip_sum
,
8328 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8329 bool write
= bio_op(bio
) == REQ_OP_WRITE
;
8330 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8334 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
8339 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
,
8340 BTRFS_WQ_ENDIO_DATA
);
8348 if (write
&& async_submit
) {
8349 ret
= btrfs_wq_submit_bio(root
->fs_info
,
8350 inode
, bio
, 0, 0, file_offset
,
8351 __btrfs_submit_bio_start_direct_io
,
8352 __btrfs_submit_bio_done
);
8356 * If we aren't doing async submit, calculate the csum of the
8359 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
8363 ret
= btrfs_lookup_and_bind_dio_csum(root
, inode
, dip
, bio
,
8369 ret
= btrfs_map_bio(root
, bio
, 0, async_submit
);
8375 static int btrfs_submit_direct_hook(struct btrfs_dio_private
*dip
,
8378 struct inode
*inode
= dip
->inode
;
8379 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8381 struct bio
*orig_bio
= dip
->orig_bio
;
8382 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
8383 u64 start_sector
= orig_bio
->bi_iter
.bi_sector
;
8384 u64 file_offset
= dip
->logical_offset
;
8387 u32 blocksize
= root
->sectorsize
;
8388 int async_submit
= 0;
8393 map_length
= orig_bio
->bi_iter
.bi_size
;
8394 ret
= btrfs_map_block(root
->fs_info
, bio_op(orig_bio
),
8395 start_sector
<< 9, &map_length
, NULL
, 0);
8399 if (map_length
>= orig_bio
->bi_iter
.bi_size
) {
8401 dip
->flags
|= BTRFS_DIO_ORIG_BIO_SUBMITTED
;
8405 /* async crcs make it difficult to collect full stripe writes. */
8406 if (btrfs_get_alloc_profile(root
, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK
)
8411 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
8415 bio_set_op_attrs(bio
, bio_op(orig_bio
), bio_flags(orig_bio
));
8416 bio
->bi_private
= dip
;
8417 bio
->bi_end_io
= btrfs_end_dio_bio
;
8418 btrfs_io_bio(bio
)->logical
= file_offset
;
8419 atomic_inc(&dip
->pending_bios
);
8421 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
8422 nr_sectors
= BTRFS_BYTES_TO_BLKS(root
->fs_info
, bvec
->bv_len
);
8425 if (unlikely(map_length
< submit_len
+ blocksize
||
8426 bio_add_page(bio
, bvec
->bv_page
, blocksize
,
8427 bvec
->bv_offset
+ (i
* blocksize
)) < blocksize
)) {
8429 * inc the count before we submit the bio so
8430 * we know the end IO handler won't happen before
8431 * we inc the count. Otherwise, the dip might get freed
8432 * before we're done setting it up
8434 atomic_inc(&dip
->pending_bios
);
8435 ret
= __btrfs_submit_dio_bio(bio
, inode
,
8436 file_offset
, skip_sum
,
8440 atomic_dec(&dip
->pending_bios
);
8444 start_sector
+= submit_len
>> 9;
8445 file_offset
+= submit_len
;
8449 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
8450 start_sector
, GFP_NOFS
);
8453 bio_set_op_attrs(bio
, bio_op(orig_bio
),
8454 bio_flags(orig_bio
));
8455 bio
->bi_private
= dip
;
8456 bio
->bi_end_io
= btrfs_end_dio_bio
;
8457 btrfs_io_bio(bio
)->logical
= file_offset
;
8459 map_length
= orig_bio
->bi_iter
.bi_size
;
8460 ret
= btrfs_map_block(root
->fs_info
, bio_op(orig_bio
),
8462 &map_length
, NULL
, 0);
8470 submit_len
+= blocksize
;
8480 ret
= __btrfs_submit_dio_bio(bio
, inode
, file_offset
, skip_sum
,
8489 * before atomic variable goto zero, we must
8490 * make sure dip->errors is perceived to be set.
8492 smp_mb__before_atomic();
8493 if (atomic_dec_and_test(&dip
->pending_bios
))
8494 bio_io_error(dip
->orig_bio
);
8496 /* bio_end_io() will handle error, so we needn't return it */
8500 static void btrfs_submit_direct(struct bio
*dio_bio
, struct inode
*inode
,
8503 struct btrfs_dio_private
*dip
= NULL
;
8504 struct bio
*io_bio
= NULL
;
8505 struct btrfs_io_bio
*btrfs_bio
;
8507 bool write
= (bio_op(dio_bio
) == REQ_OP_WRITE
);
8510 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
8512 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
8518 dip
= kzalloc(sizeof(*dip
), GFP_NOFS
);
8524 dip
->private = dio_bio
->bi_private
;
8526 dip
->logical_offset
= file_offset
;
8527 dip
->bytes
= dio_bio
->bi_iter
.bi_size
;
8528 dip
->disk_bytenr
= (u64
)dio_bio
->bi_iter
.bi_sector
<< 9;
8529 io_bio
->bi_private
= dip
;
8530 dip
->orig_bio
= io_bio
;
8531 dip
->dio_bio
= dio_bio
;
8532 atomic_set(&dip
->pending_bios
, 0);
8533 btrfs_bio
= btrfs_io_bio(io_bio
);
8534 btrfs_bio
->logical
= file_offset
;
8537 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
8539 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
8540 dip
->subio_endio
= btrfs_subio_endio_read
;
8544 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8545 * even if we fail to submit a bio, because in such case we do the
8546 * corresponding error handling below and it must not be done a second
8547 * time by btrfs_direct_IO().
8550 struct btrfs_dio_data
*dio_data
= current
->journal_info
;
8552 dio_data
->unsubmitted_oe_range_end
= dip
->logical_offset
+
8554 dio_data
->unsubmitted_oe_range_start
=
8555 dio_data
->unsubmitted_oe_range_end
;
8558 ret
= btrfs_submit_direct_hook(dip
, skip_sum
);
8562 if (btrfs_bio
->end_io
)
8563 btrfs_bio
->end_io(btrfs_bio
, ret
);
8567 * If we arrived here it means either we failed to submit the dip
8568 * or we either failed to clone the dio_bio or failed to allocate the
8569 * dip. If we cloned the dio_bio and allocated the dip, we can just
8570 * call bio_endio against our io_bio so that we get proper resource
8571 * cleanup if we fail to submit the dip, otherwise, we must do the
8572 * same as btrfs_endio_direct_[write|read] because we can't call these
8573 * callbacks - they require an allocated dip and a clone of dio_bio.
8575 if (io_bio
&& dip
) {
8576 io_bio
->bi_error
= -EIO
;
8579 * The end io callbacks free our dip, do the final put on io_bio
8580 * and all the cleanup and final put for dio_bio (through
8587 btrfs_endio_direct_write_update_ordered(inode
,
8589 dio_bio
->bi_iter
.bi_size
,
8592 unlock_extent(&BTRFS_I(inode
)->io_tree
, file_offset
,
8593 file_offset
+ dio_bio
->bi_iter
.bi_size
- 1);
8595 dio_bio
->bi_error
= -EIO
;
8597 * Releases and cleans up our dio_bio, no need to bio_put()
8598 * nor bio_endio()/bio_io_error() against dio_bio.
8600 dio_end_io(dio_bio
, ret
);
8607 static ssize_t
check_direct_IO(struct btrfs_root
*root
, struct kiocb
*iocb
,
8608 const struct iov_iter
*iter
, loff_t offset
)
8612 unsigned blocksize_mask
= root
->sectorsize
- 1;
8613 ssize_t retval
= -EINVAL
;
8615 if (offset
& blocksize_mask
)
8618 if (iov_iter_alignment(iter
) & blocksize_mask
)
8621 /* If this is a write we don't need to check anymore */
8622 if (iov_iter_rw(iter
) == WRITE
)
8625 * Check to make sure we don't have duplicate iov_base's in this
8626 * iovec, if so return EINVAL, otherwise we'll get csum errors
8627 * when reading back.
8629 for (seg
= 0; seg
< iter
->nr_segs
; seg
++) {
8630 for (i
= seg
+ 1; i
< iter
->nr_segs
; i
++) {
8631 if (iter
->iov
[seg
].iov_base
== iter
->iov
[i
].iov_base
)
8640 static ssize_t
btrfs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
8642 struct file
*file
= iocb
->ki_filp
;
8643 struct inode
*inode
= file
->f_mapping
->host
;
8644 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8645 struct btrfs_dio_data dio_data
= { 0 };
8646 loff_t offset
= iocb
->ki_pos
;
8650 bool relock
= false;
8653 if (check_direct_IO(BTRFS_I(inode
)->root
, iocb
, iter
, offset
))
8656 inode_dio_begin(inode
);
8657 smp_mb__after_atomic();
8660 * The generic stuff only does filemap_write_and_wait_range, which
8661 * isn't enough if we've written compressed pages to this area, so
8662 * we need to flush the dirty pages again to make absolutely sure
8663 * that any outstanding dirty pages are on disk.
8665 count
= iov_iter_count(iter
);
8666 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
8667 &BTRFS_I(inode
)->runtime_flags
))
8668 filemap_fdatawrite_range(inode
->i_mapping
, offset
,
8669 offset
+ count
- 1);
8671 if (iov_iter_rw(iter
) == WRITE
) {
8673 * If the write DIO is beyond the EOF, we need update
8674 * the isize, but it is protected by i_mutex. So we can
8675 * not unlock the i_mutex at this case.
8677 if (offset
+ count
<= inode
->i_size
) {
8678 inode_unlock(inode
);
8681 ret
= btrfs_delalloc_reserve_space(inode
, offset
, count
);
8684 dio_data
.outstanding_extents
= div64_u64(count
+
8685 BTRFS_MAX_EXTENT_SIZE
- 1,
8686 BTRFS_MAX_EXTENT_SIZE
);
8689 * We need to know how many extents we reserved so that we can
8690 * do the accounting properly if we go over the number we
8691 * originally calculated. Abuse current->journal_info for this.
8693 dio_data
.reserve
= round_up(count
, root
->sectorsize
);
8694 dio_data
.unsubmitted_oe_range_start
= (u64
)offset
;
8695 dio_data
.unsubmitted_oe_range_end
= (u64
)offset
;
8696 current
->journal_info
= &dio_data
;
8697 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
8698 &BTRFS_I(inode
)->runtime_flags
)) {
8699 inode_dio_end(inode
);
8700 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
8704 ret
= __blockdev_direct_IO(iocb
, inode
,
8705 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
8706 iter
, btrfs_get_blocks_direct
, NULL
,
8707 btrfs_submit_direct
, flags
);
8708 if (iov_iter_rw(iter
) == WRITE
) {
8709 current
->journal_info
= NULL
;
8710 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
8711 if (dio_data
.reserve
)
8712 btrfs_delalloc_release_space(inode
, offset
,
8715 * On error we might have left some ordered extents
8716 * without submitting corresponding bios for them, so
8717 * cleanup them up to avoid other tasks getting them
8718 * and waiting for them to complete forever.
8720 if (dio_data
.unsubmitted_oe_range_start
<
8721 dio_data
.unsubmitted_oe_range_end
)
8722 btrfs_endio_direct_write_update_ordered(inode
,
8723 dio_data
.unsubmitted_oe_range_start
,
8724 dio_data
.unsubmitted_oe_range_end
-
8725 dio_data
.unsubmitted_oe_range_start
,
8727 } else if (ret
>= 0 && (size_t)ret
< count
)
8728 btrfs_delalloc_release_space(inode
, offset
,
8729 count
- (size_t)ret
);
8733 inode_dio_end(inode
);
8740 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8742 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
8743 __u64 start
, __u64 len
)
8747 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
8751 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
8754 int btrfs_readpage(struct file
*file
, struct page
*page
)
8756 struct extent_io_tree
*tree
;
8757 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8758 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
8761 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
8763 struct extent_io_tree
*tree
;
8764 struct inode
*inode
= page
->mapping
->host
;
8767 if (current
->flags
& PF_MEMALLOC
) {
8768 redirty_page_for_writepage(wbc
, page
);
8774 * If we are under memory pressure we will call this directly from the
8775 * VM, we need to make sure we have the inode referenced for the ordered
8776 * extent. If not just return like we didn't do anything.
8778 if (!igrab(inode
)) {
8779 redirty_page_for_writepage(wbc
, page
);
8780 return AOP_WRITEPAGE_ACTIVATE
;
8782 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8783 ret
= extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
8784 btrfs_add_delayed_iput(inode
);
8788 static int btrfs_writepages(struct address_space
*mapping
,
8789 struct writeback_control
*wbc
)
8791 struct extent_io_tree
*tree
;
8793 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8794 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
8798 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
8799 struct list_head
*pages
, unsigned nr_pages
)
8801 struct extent_io_tree
*tree
;
8802 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8803 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
8806 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8808 struct extent_io_tree
*tree
;
8809 struct extent_map_tree
*map
;
8812 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8813 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
8814 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
8816 ClearPagePrivate(page
);
8817 set_page_private(page
, 0);
8823 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8825 if (PageWriteback(page
) || PageDirty(page
))
8827 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
8830 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
8831 unsigned int length
)
8833 struct inode
*inode
= page
->mapping
->host
;
8834 struct extent_io_tree
*tree
;
8835 struct btrfs_ordered_extent
*ordered
;
8836 struct extent_state
*cached_state
= NULL
;
8837 u64 page_start
= page_offset(page
);
8838 u64 page_end
= page_start
+ PAGE_SIZE
- 1;
8841 int inode_evicting
= inode
->i_state
& I_FREEING
;
8844 * we have the page locked, so new writeback can't start,
8845 * and the dirty bit won't be cleared while we are here.
8847 * Wait for IO on this page so that we can safely clear
8848 * the PagePrivate2 bit and do ordered accounting
8850 wait_on_page_writeback(page
);
8852 tree
= &BTRFS_I(inode
)->io_tree
;
8854 btrfs_releasepage(page
, GFP_NOFS
);
8858 if (!inode_evicting
)
8859 lock_extent_bits(tree
, page_start
, page_end
, &cached_state
);
8862 ordered
= btrfs_lookup_ordered_range(inode
, start
,
8863 page_end
- start
+ 1);
8865 end
= min(page_end
, ordered
->file_offset
+ ordered
->len
- 1);
8867 * IO on this page will never be started, so we need
8868 * to account for any ordered extents now
8870 if (!inode_evicting
)
8871 clear_extent_bit(tree
, start
, end
,
8872 EXTENT_DIRTY
| EXTENT_DELALLOC
|
8873 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
8874 EXTENT_DEFRAG
, 1, 0, &cached_state
,
8877 * whoever cleared the private bit is responsible
8878 * for the finish_ordered_io
8880 if (TestClearPagePrivate2(page
)) {
8881 struct btrfs_ordered_inode_tree
*tree
;
8884 tree
= &BTRFS_I(inode
)->ordered_tree
;
8886 spin_lock_irq(&tree
->lock
);
8887 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
8888 new_len
= start
- ordered
->file_offset
;
8889 if (new_len
< ordered
->truncated_len
)
8890 ordered
->truncated_len
= new_len
;
8891 spin_unlock_irq(&tree
->lock
);
8893 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
8895 end
- start
+ 1, 1))
8896 btrfs_finish_ordered_io(ordered
);
8898 btrfs_put_ordered_extent(ordered
);
8899 if (!inode_evicting
) {
8900 cached_state
= NULL
;
8901 lock_extent_bits(tree
, start
, end
,
8906 if (start
< page_end
)
8911 * Qgroup reserved space handler
8912 * Page here will be either
8913 * 1) Already written to disk
8914 * In this case, its reserved space is released from data rsv map
8915 * and will be freed by delayed_ref handler finally.
8916 * So even we call qgroup_free_data(), it won't decrease reserved
8918 * 2) Not written to disk
8919 * This means the reserved space should be freed here.
8921 btrfs_qgroup_free_data(inode
, page_start
, PAGE_SIZE
);
8922 if (!inode_evicting
) {
8923 clear_extent_bit(tree
, page_start
, page_end
,
8924 EXTENT_LOCKED
| EXTENT_DIRTY
|
8925 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
8926 EXTENT_DEFRAG
, 1, 1,
8927 &cached_state
, GFP_NOFS
);
8929 __btrfs_releasepage(page
, GFP_NOFS
);
8932 ClearPageChecked(page
);
8933 if (PagePrivate(page
)) {
8934 ClearPagePrivate(page
);
8935 set_page_private(page
, 0);
8941 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8942 * called from a page fault handler when a page is first dirtied. Hence we must
8943 * be careful to check for EOF conditions here. We set the page up correctly
8944 * for a written page which means we get ENOSPC checking when writing into
8945 * holes and correct delalloc and unwritten extent mapping on filesystems that
8946 * support these features.
8948 * We are not allowed to take the i_mutex here so we have to play games to
8949 * protect against truncate races as the page could now be beyond EOF. Because
8950 * vmtruncate() writes the inode size before removing pages, once we have the
8951 * page lock we can determine safely if the page is beyond EOF. If it is not
8952 * beyond EOF, then the page is guaranteed safe against truncation until we
8955 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
8957 struct page
*page
= vmf
->page
;
8958 struct inode
*inode
= file_inode(vma
->vm_file
);
8959 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8960 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
8961 struct btrfs_ordered_extent
*ordered
;
8962 struct extent_state
*cached_state
= NULL
;
8964 unsigned long zero_start
;
8973 reserved_space
= PAGE_SIZE
;
8975 sb_start_pagefault(inode
->i_sb
);
8976 page_start
= page_offset(page
);
8977 page_end
= page_start
+ PAGE_SIZE
- 1;
8981 * Reserving delalloc space after obtaining the page lock can lead to
8982 * deadlock. For example, if a dirty page is locked by this function
8983 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8984 * dirty page write out, then the btrfs_writepage() function could
8985 * end up waiting indefinitely to get a lock on the page currently
8986 * being processed by btrfs_page_mkwrite() function.
8988 ret
= btrfs_delalloc_reserve_space(inode
, page_start
,
8991 ret
= file_update_time(vma
->vm_file
);
8997 else /* -ENOSPC, -EIO, etc */
8998 ret
= VM_FAULT_SIGBUS
;
9004 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
9007 size
= i_size_read(inode
);
9009 if ((page
->mapping
!= inode
->i_mapping
) ||
9010 (page_start
>= size
)) {
9011 /* page got truncated out from underneath us */
9014 wait_on_page_writeback(page
);
9016 lock_extent_bits(io_tree
, page_start
, page_end
, &cached_state
);
9017 set_page_extent_mapped(page
);
9020 * we can't set the delalloc bits if there are pending ordered
9021 * extents. Drop our locks and wait for them to finish
9023 ordered
= btrfs_lookup_ordered_range(inode
, page_start
, page_end
);
9025 unlock_extent_cached(io_tree
, page_start
, page_end
,
9026 &cached_state
, GFP_NOFS
);
9028 btrfs_start_ordered_extent(inode
, ordered
, 1);
9029 btrfs_put_ordered_extent(ordered
);
9033 if (page
->index
== ((size
- 1) >> PAGE_SHIFT
)) {
9034 reserved_space
= round_up(size
- page_start
, root
->sectorsize
);
9035 if (reserved_space
< PAGE_SIZE
) {
9036 end
= page_start
+ reserved_space
- 1;
9037 spin_lock(&BTRFS_I(inode
)->lock
);
9038 BTRFS_I(inode
)->outstanding_extents
++;
9039 spin_unlock(&BTRFS_I(inode
)->lock
);
9040 btrfs_delalloc_release_space(inode
, page_start
,
9041 PAGE_SIZE
- reserved_space
);
9046 * XXX - page_mkwrite gets called every time the page is dirtied, even
9047 * if it was already dirty, so for space accounting reasons we need to
9048 * clear any delalloc bits for the range we are fixing to save. There
9049 * is probably a better way to do this, but for now keep consistent with
9050 * prepare_pages in the normal write path.
9052 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, end
,
9053 EXTENT_DIRTY
| EXTENT_DELALLOC
|
9054 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
9055 0, 0, &cached_state
, GFP_NOFS
);
9057 ret
= btrfs_set_extent_delalloc(inode
, page_start
, end
,
9060 unlock_extent_cached(io_tree
, page_start
, page_end
,
9061 &cached_state
, GFP_NOFS
);
9062 ret
= VM_FAULT_SIGBUS
;
9067 /* page is wholly or partially inside EOF */
9068 if (page_start
+ PAGE_SIZE
> size
)
9069 zero_start
= size
& ~PAGE_MASK
;
9071 zero_start
= PAGE_SIZE
;
9073 if (zero_start
!= PAGE_SIZE
) {
9075 memset(kaddr
+ zero_start
, 0, PAGE_SIZE
- zero_start
);
9076 flush_dcache_page(page
);
9079 ClearPageChecked(page
);
9080 set_page_dirty(page
);
9081 SetPageUptodate(page
);
9083 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
9084 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
9085 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
9087 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
9091 sb_end_pagefault(inode
->i_sb
);
9092 return VM_FAULT_LOCKED
;
9096 btrfs_delalloc_release_space(inode
, page_start
, reserved_space
);
9098 sb_end_pagefault(inode
->i_sb
);
9102 static int btrfs_truncate(struct inode
*inode
)
9104 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9105 struct btrfs_block_rsv
*rsv
;
9108 struct btrfs_trans_handle
*trans
;
9109 u64 mask
= root
->sectorsize
- 1;
9110 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
9112 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
9118 * Yes ladies and gentlemen, this is indeed ugly. The fact is we have
9119 * 3 things going on here
9121 * 1) We need to reserve space for our orphan item and the space to
9122 * delete our orphan item. Lord knows we don't want to have a dangling
9123 * orphan item because we didn't reserve space to remove it.
9125 * 2) We need to reserve space to update our inode.
9127 * 3) We need to have something to cache all the space that is going to
9128 * be free'd up by the truncate operation, but also have some slack
9129 * space reserved in case it uses space during the truncate (thank you
9130 * very much snapshotting).
9132 * And we need these to all be separate. The fact is we can use a lot of
9133 * space doing the truncate, and we have no earthly idea how much space
9134 * we will use, so we need the truncate reservation to be separate so it
9135 * doesn't end up using space reserved for updating the inode or
9136 * removing the orphan item. We also need to be able to stop the
9137 * transaction and start a new one, which means we need to be able to
9138 * update the inode several times, and we have no idea of knowing how
9139 * many times that will be, so we can't just reserve 1 item for the
9140 * entirety of the operation, so that has to be done separately as well.
9141 * Then there is the orphan item, which does indeed need to be held on
9142 * to for the whole operation, and we need nobody to touch this reserved
9143 * space except the orphan code.
9145 * So that leaves us with
9147 * 1) root->orphan_block_rsv - for the orphan deletion.
9148 * 2) rsv - for the truncate reservation, which we will steal from the
9149 * transaction reservation.
9150 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9151 * updating the inode.
9153 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
9156 rsv
->size
= min_size
;
9160 * 1 for the truncate slack space
9161 * 1 for updating the inode.
9163 trans
= btrfs_start_transaction(root
, 2);
9164 if (IS_ERR(trans
)) {
9165 err
= PTR_ERR(trans
);
9169 /* Migrate the slack space for the truncate to our reserve */
9170 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
9175 * So if we truncate and then write and fsync we normally would just
9176 * write the extents that changed, which is a problem if we need to
9177 * first truncate that entire inode. So set this flag so we write out
9178 * all of the extents in the inode to the sync log so we're completely
9181 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
9182 trans
->block_rsv
= rsv
;
9185 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
9187 BTRFS_EXTENT_DATA_KEY
);
9188 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
) {
9193 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
9194 ret
= btrfs_update_inode(trans
, root
, inode
);
9200 btrfs_end_transaction(trans
, root
);
9201 btrfs_btree_balance_dirty(root
);
9203 trans
= btrfs_start_transaction(root
, 2);
9204 if (IS_ERR(trans
)) {
9205 ret
= err
= PTR_ERR(trans
);
9210 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
9212 BUG_ON(ret
); /* shouldn't happen */
9213 trans
->block_rsv
= rsv
;
9216 if (ret
== 0 && inode
->i_nlink
> 0) {
9217 trans
->block_rsv
= root
->orphan_block_rsv
;
9218 ret
= btrfs_orphan_del(trans
, inode
);
9224 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
9225 ret
= btrfs_update_inode(trans
, root
, inode
);
9229 ret
= btrfs_end_transaction(trans
, root
);
9230 btrfs_btree_balance_dirty(root
);
9233 btrfs_free_block_rsv(root
, rsv
);
9242 * create a new subvolume directory/inode (helper for the ioctl).
9244 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
9245 struct btrfs_root
*new_root
,
9246 struct btrfs_root
*parent_root
,
9249 struct inode
*inode
;
9253 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
9254 new_dirid
, new_dirid
,
9255 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
9258 return PTR_ERR(inode
);
9259 inode
->i_op
= &btrfs_dir_inode_operations
;
9260 inode
->i_fop
= &btrfs_dir_file_operations
;
9262 set_nlink(inode
, 1);
9263 btrfs_i_size_write(inode
, 0);
9264 unlock_new_inode(inode
);
9266 err
= btrfs_subvol_inherit_props(trans
, new_root
, parent_root
);
9268 btrfs_err(new_root
->fs_info
,
9269 "error inheriting subvolume %llu properties: %d",
9270 new_root
->root_key
.objectid
, err
);
9272 err
= btrfs_update_inode(trans
, new_root
, inode
);
9278 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
9280 struct btrfs_inode
*ei
;
9281 struct inode
*inode
;
9283 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
9290 ei
->last_sub_trans
= 0;
9291 ei
->logged_trans
= 0;
9292 ei
->delalloc_bytes
= 0;
9293 ei
->defrag_bytes
= 0;
9294 ei
->disk_i_size
= 0;
9297 ei
->index_cnt
= (u64
)-1;
9299 ei
->last_unlink_trans
= 0;
9300 ei
->last_log_commit
= 0;
9301 ei
->delayed_iput_count
= 0;
9303 spin_lock_init(&ei
->lock
);
9304 ei
->outstanding_extents
= 0;
9305 ei
->reserved_extents
= 0;
9307 ei
->runtime_flags
= 0;
9308 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
9310 ei
->delayed_node
= NULL
;
9312 ei
->i_otime
.tv_sec
= 0;
9313 ei
->i_otime
.tv_nsec
= 0;
9315 inode
= &ei
->vfs_inode
;
9316 extent_map_tree_init(&ei
->extent_tree
);
9317 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
9318 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
9319 ei
->io_tree
.track_uptodate
= 1;
9320 ei
->io_failure_tree
.track_uptodate
= 1;
9321 atomic_set(&ei
->sync_writers
, 0);
9322 mutex_init(&ei
->log_mutex
);
9323 mutex_init(&ei
->delalloc_mutex
);
9324 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
9325 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
9326 INIT_LIST_HEAD(&ei
->delayed_iput
);
9327 RB_CLEAR_NODE(&ei
->rb_node
);
9328 init_rwsem(&ei
->dio_sem
);
9333 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9334 void btrfs_test_destroy_inode(struct inode
*inode
)
9336 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
9337 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9341 static void btrfs_i_callback(struct rcu_head
*head
)
9343 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
9344 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9347 void btrfs_destroy_inode(struct inode
*inode
)
9349 struct btrfs_ordered_extent
*ordered
;
9350 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9352 WARN_ON(!hlist_empty(&inode
->i_dentry
));
9353 WARN_ON(inode
->i_data
.nrpages
);
9354 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
9355 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
9356 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
9357 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
9358 WARN_ON(BTRFS_I(inode
)->defrag_bytes
);
9361 * This can happen where we create an inode, but somebody else also
9362 * created the same inode and we need to destroy the one we already
9368 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
9369 &BTRFS_I(inode
)->runtime_flags
)) {
9370 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
9372 atomic_dec(&root
->orphan_inodes
);
9376 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
9380 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
9381 ordered
->file_offset
, ordered
->len
);
9382 btrfs_remove_ordered_extent(inode
, ordered
);
9383 btrfs_put_ordered_extent(ordered
);
9384 btrfs_put_ordered_extent(ordered
);
9387 btrfs_qgroup_check_reserved_leak(inode
);
9388 inode_tree_del(inode
);
9389 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
9391 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
9394 int btrfs_drop_inode(struct inode
*inode
)
9396 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9401 /* the snap/subvol tree is on deleting */
9402 if (btrfs_root_refs(&root
->root_item
) == 0)
9405 return generic_drop_inode(inode
);
9408 static void init_once(void *foo
)
9410 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
9412 inode_init_once(&ei
->vfs_inode
);
9415 void btrfs_destroy_cachep(void)
9418 * Make sure all delayed rcu free inodes are flushed before we
9422 kmem_cache_destroy(btrfs_inode_cachep
);
9423 kmem_cache_destroy(btrfs_trans_handle_cachep
);
9424 kmem_cache_destroy(btrfs_transaction_cachep
);
9425 kmem_cache_destroy(btrfs_path_cachep
);
9426 kmem_cache_destroy(btrfs_free_space_cachep
);
9429 int btrfs_init_cachep(void)
9431 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
9432 sizeof(struct btrfs_inode
), 0,
9433 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
| SLAB_ACCOUNT
,
9435 if (!btrfs_inode_cachep
)
9438 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
9439 sizeof(struct btrfs_trans_handle
), 0,
9440 SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
9441 if (!btrfs_trans_handle_cachep
)
9444 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
9445 sizeof(struct btrfs_transaction
), 0,
9446 SLAB_TEMPORARY
| SLAB_MEM_SPREAD
, NULL
);
9447 if (!btrfs_transaction_cachep
)
9450 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
9451 sizeof(struct btrfs_path
), 0,
9452 SLAB_MEM_SPREAD
, NULL
);
9453 if (!btrfs_path_cachep
)
9456 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
9457 sizeof(struct btrfs_free_space
), 0,
9458 SLAB_MEM_SPREAD
, NULL
);
9459 if (!btrfs_free_space_cachep
)
9464 btrfs_destroy_cachep();
9468 static int btrfs_getattr(struct vfsmount
*mnt
,
9469 struct dentry
*dentry
, struct kstat
*stat
)
9472 struct inode
*inode
= d_inode(dentry
);
9473 u32 blocksize
= inode
->i_sb
->s_blocksize
;
9475 generic_fillattr(inode
, stat
);
9476 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
9478 spin_lock(&BTRFS_I(inode
)->lock
);
9479 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
9480 spin_unlock(&BTRFS_I(inode
)->lock
);
9481 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
9482 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
9486 static int btrfs_rename_exchange(struct inode
*old_dir
,
9487 struct dentry
*old_dentry
,
9488 struct inode
*new_dir
,
9489 struct dentry
*new_dentry
)
9491 struct btrfs_trans_handle
*trans
;
9492 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
9493 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
9494 struct inode
*new_inode
= new_dentry
->d_inode
;
9495 struct inode
*old_inode
= old_dentry
->d_inode
;
9496 struct timespec ctime
= CURRENT_TIME
;
9497 struct dentry
*parent
;
9498 u64 old_ino
= btrfs_ino(old_inode
);
9499 u64 new_ino
= btrfs_ino(new_inode
);
9504 bool root_log_pinned
= false;
9505 bool dest_log_pinned
= false;
9507 /* we only allow rename subvolume link between subvolumes */
9508 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
9511 /* close the race window with snapshot create/destroy ioctl */
9512 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9513 down_read(&root
->fs_info
->subvol_sem
);
9514 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9515 down_read(&dest
->fs_info
->subvol_sem
);
9518 * We want to reserve the absolute worst case amount of items. So if
9519 * both inodes are subvols and we need to unlink them then that would
9520 * require 4 item modifications, but if they are both normal inodes it
9521 * would require 5 item modifications, so we'll assume their normal
9522 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9523 * should cover the worst case number of items we'll modify.
9525 trans
= btrfs_start_transaction(root
, 12);
9526 if (IS_ERR(trans
)) {
9527 ret
= PTR_ERR(trans
);
9532 * We need to find a free sequence number both in the source and
9533 * in the destination directory for the exchange.
9535 ret
= btrfs_set_inode_index(new_dir
, &old_idx
);
9538 ret
= btrfs_set_inode_index(old_dir
, &new_idx
);
9542 BTRFS_I(old_inode
)->dir_index
= 0ULL;
9543 BTRFS_I(new_inode
)->dir_index
= 0ULL;
9545 /* Reference for the source. */
9546 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9547 /* force full log commit if subvolume involved. */
9548 btrfs_set_log_full_commit(root
->fs_info
, trans
);
9550 btrfs_pin_log_trans(root
);
9551 root_log_pinned
= true;
9552 ret
= btrfs_insert_inode_ref(trans
, dest
,
9553 new_dentry
->d_name
.name
,
9554 new_dentry
->d_name
.len
,
9556 btrfs_ino(new_dir
), old_idx
);
9561 /* And now for the dest. */
9562 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9563 /* force full log commit if subvolume involved. */
9564 btrfs_set_log_full_commit(dest
->fs_info
, trans
);
9566 btrfs_pin_log_trans(dest
);
9567 dest_log_pinned
= true;
9568 ret
= btrfs_insert_inode_ref(trans
, root
,
9569 old_dentry
->d_name
.name
,
9570 old_dentry
->d_name
.len
,
9572 btrfs_ino(old_dir
), new_idx
);
9577 /* Update inode version and ctime/mtime. */
9578 inode_inc_iversion(old_dir
);
9579 inode_inc_iversion(new_dir
);
9580 inode_inc_iversion(old_inode
);
9581 inode_inc_iversion(new_inode
);
9582 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
9583 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
9584 old_inode
->i_ctime
= ctime
;
9585 new_inode
->i_ctime
= ctime
;
9587 if (old_dentry
->d_parent
!= new_dentry
->d_parent
) {
9588 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
9589 btrfs_record_unlink_dir(trans
, new_dir
, new_inode
, 1);
9592 /* src is a subvolume */
9593 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9594 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
9595 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
,
9597 old_dentry
->d_name
.name
,
9598 old_dentry
->d_name
.len
);
9599 } else { /* src is an inode */
9600 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
9601 old_dentry
->d_inode
,
9602 old_dentry
->d_name
.name
,
9603 old_dentry
->d_name
.len
);
9605 ret
= btrfs_update_inode(trans
, root
, old_inode
);
9608 btrfs_abort_transaction(trans
, ret
);
9612 /* dest is a subvolume */
9613 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
) {
9614 root_objectid
= BTRFS_I(new_inode
)->root
->root_key
.objectid
;
9615 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
9617 new_dentry
->d_name
.name
,
9618 new_dentry
->d_name
.len
);
9619 } else { /* dest is an inode */
9620 ret
= __btrfs_unlink_inode(trans
, dest
, new_dir
,
9621 new_dentry
->d_inode
,
9622 new_dentry
->d_name
.name
,
9623 new_dentry
->d_name
.len
);
9625 ret
= btrfs_update_inode(trans
, dest
, new_inode
);
9628 btrfs_abort_transaction(trans
, ret
);
9632 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
9633 new_dentry
->d_name
.name
,
9634 new_dentry
->d_name
.len
, 0, old_idx
);
9636 btrfs_abort_transaction(trans
, ret
);
9640 ret
= btrfs_add_link(trans
, old_dir
, new_inode
,
9641 old_dentry
->d_name
.name
,
9642 old_dentry
->d_name
.len
, 0, new_idx
);
9644 btrfs_abort_transaction(trans
, ret
);
9648 if (old_inode
->i_nlink
== 1)
9649 BTRFS_I(old_inode
)->dir_index
= old_idx
;
9650 if (new_inode
->i_nlink
== 1)
9651 BTRFS_I(new_inode
)->dir_index
= new_idx
;
9653 if (root_log_pinned
) {
9654 parent
= new_dentry
->d_parent
;
9655 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
9656 btrfs_end_log_trans(root
);
9657 root_log_pinned
= false;
9659 if (dest_log_pinned
) {
9660 parent
= old_dentry
->d_parent
;
9661 btrfs_log_new_name(trans
, new_inode
, new_dir
, parent
);
9662 btrfs_end_log_trans(dest
);
9663 dest_log_pinned
= false;
9667 * If we have pinned a log and an error happened, we unpin tasks
9668 * trying to sync the log and force them to fallback to a transaction
9669 * commit if the log currently contains any of the inodes involved in
9670 * this rename operation (to ensure we do not persist a log with an
9671 * inconsistent state for any of these inodes or leading to any
9672 * inconsistencies when replayed). If the transaction was aborted, the
9673 * abortion reason is propagated to userspace when attempting to commit
9674 * the transaction. If the log does not contain any of these inodes, we
9675 * allow the tasks to sync it.
9677 if (ret
&& (root_log_pinned
|| dest_log_pinned
)) {
9678 if (btrfs_inode_in_log(old_dir
, root
->fs_info
->generation
) ||
9679 btrfs_inode_in_log(new_dir
, root
->fs_info
->generation
) ||
9680 btrfs_inode_in_log(old_inode
, root
->fs_info
->generation
) ||
9682 btrfs_inode_in_log(new_inode
, root
->fs_info
->generation
)))
9683 btrfs_set_log_full_commit(root
->fs_info
, trans
);
9685 if (root_log_pinned
) {
9686 btrfs_end_log_trans(root
);
9687 root_log_pinned
= false;
9689 if (dest_log_pinned
) {
9690 btrfs_end_log_trans(dest
);
9691 dest_log_pinned
= false;
9694 ret
= btrfs_end_transaction(trans
, root
);
9696 if (new_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9697 up_read(&dest
->fs_info
->subvol_sem
);
9698 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9699 up_read(&root
->fs_info
->subvol_sem
);
9704 static int btrfs_whiteout_for_rename(struct btrfs_trans_handle
*trans
,
9705 struct btrfs_root
*root
,
9707 struct dentry
*dentry
)
9710 struct inode
*inode
;
9714 ret
= btrfs_find_free_ino(root
, &objectid
);
9718 inode
= btrfs_new_inode(trans
, root
, dir
,
9719 dentry
->d_name
.name
,
9723 S_IFCHR
| WHITEOUT_MODE
,
9726 if (IS_ERR(inode
)) {
9727 ret
= PTR_ERR(inode
);
9731 inode
->i_op
= &btrfs_special_inode_operations
;
9732 init_special_inode(inode
, inode
->i_mode
,
9735 ret
= btrfs_init_inode_security(trans
, inode
, dir
,
9740 ret
= btrfs_add_nondir(trans
, dir
, dentry
,
9745 ret
= btrfs_update_inode(trans
, root
, inode
);
9747 unlock_new_inode(inode
);
9749 inode_dec_link_count(inode
);
9755 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
9756 struct inode
*new_dir
, struct dentry
*new_dentry
,
9759 struct btrfs_trans_handle
*trans
;
9760 unsigned int trans_num_items
;
9761 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
9762 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
9763 struct inode
*new_inode
= d_inode(new_dentry
);
9764 struct inode
*old_inode
= d_inode(old_dentry
);
9768 u64 old_ino
= btrfs_ino(old_inode
);
9769 bool log_pinned
= false;
9771 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
9774 /* we only allow rename subvolume link between subvolumes */
9775 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
9778 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
9779 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
9782 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
9783 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
9787 /* check for collisions, even if the name isn't there */
9788 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
9789 new_dentry
->d_name
.name
,
9790 new_dentry
->d_name
.len
);
9793 if (ret
== -EEXIST
) {
9795 * eexist without a new_inode */
9796 if (WARN_ON(!new_inode
)) {
9800 /* maybe -EOVERFLOW */
9807 * we're using rename to replace one file with another. Start IO on it
9808 * now so we don't add too much work to the end of the transaction
9810 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
)
9811 filemap_flush(old_inode
->i_mapping
);
9813 /* close the racy window with snapshot create/destroy ioctl */
9814 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9815 down_read(&root
->fs_info
->subvol_sem
);
9817 * We want to reserve the absolute worst case amount of items. So if
9818 * both inodes are subvols and we need to unlink them then that would
9819 * require 4 item modifications, but if they are both normal inodes it
9820 * would require 5 item modifications, so we'll assume they are normal
9821 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9822 * should cover the worst case number of items we'll modify.
9823 * If our rename has the whiteout flag, we need more 5 units for the
9824 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9825 * when selinux is enabled).
9827 trans_num_items
= 11;
9828 if (flags
& RENAME_WHITEOUT
)
9829 trans_num_items
+= 5;
9830 trans
= btrfs_start_transaction(root
, trans_num_items
);
9831 if (IS_ERR(trans
)) {
9832 ret
= PTR_ERR(trans
);
9837 btrfs_record_root_in_trans(trans
, dest
);
9839 ret
= btrfs_set_inode_index(new_dir
, &index
);
9843 BTRFS_I(old_inode
)->dir_index
= 0ULL;
9844 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9845 /* force full log commit if subvolume involved. */
9846 btrfs_set_log_full_commit(root
->fs_info
, trans
);
9848 btrfs_pin_log_trans(root
);
9850 ret
= btrfs_insert_inode_ref(trans
, dest
,
9851 new_dentry
->d_name
.name
,
9852 new_dentry
->d_name
.len
,
9854 btrfs_ino(new_dir
), index
);
9859 inode_inc_iversion(old_dir
);
9860 inode_inc_iversion(new_dir
);
9861 inode_inc_iversion(old_inode
);
9862 old_dir
->i_ctime
= old_dir
->i_mtime
=
9863 new_dir
->i_ctime
= new_dir
->i_mtime
=
9864 old_inode
->i_ctime
= current_fs_time(old_dir
->i_sb
);
9866 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
9867 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
9869 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9870 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
9871 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
9872 old_dentry
->d_name
.name
,
9873 old_dentry
->d_name
.len
);
9875 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
9876 d_inode(old_dentry
),
9877 old_dentry
->d_name
.name
,
9878 old_dentry
->d_name
.len
);
9880 ret
= btrfs_update_inode(trans
, root
, old_inode
);
9883 btrfs_abort_transaction(trans
, ret
);
9888 inode_inc_iversion(new_inode
);
9889 new_inode
->i_ctime
= current_fs_time(new_inode
->i_sb
);
9890 if (unlikely(btrfs_ino(new_inode
) ==
9891 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
9892 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
9893 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
9895 new_dentry
->d_name
.name
,
9896 new_dentry
->d_name
.len
);
9897 BUG_ON(new_inode
->i_nlink
== 0);
9899 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
9900 d_inode(new_dentry
),
9901 new_dentry
->d_name
.name
,
9902 new_dentry
->d_name
.len
);
9904 if (!ret
&& new_inode
->i_nlink
== 0)
9905 ret
= btrfs_orphan_add(trans
, d_inode(new_dentry
));
9907 btrfs_abort_transaction(trans
, ret
);
9912 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
9913 new_dentry
->d_name
.name
,
9914 new_dentry
->d_name
.len
, 0, index
);
9916 btrfs_abort_transaction(trans
, ret
);
9920 if (old_inode
->i_nlink
== 1)
9921 BTRFS_I(old_inode
)->dir_index
= index
;
9924 struct dentry
*parent
= new_dentry
->d_parent
;
9926 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
9927 btrfs_end_log_trans(root
);
9931 if (flags
& RENAME_WHITEOUT
) {
9932 ret
= btrfs_whiteout_for_rename(trans
, root
, old_dir
,
9936 btrfs_abort_transaction(trans
, ret
);
9942 * If we have pinned the log and an error happened, we unpin tasks
9943 * trying to sync the log and force them to fallback to a transaction
9944 * commit if the log currently contains any of the inodes involved in
9945 * this rename operation (to ensure we do not persist a log with an
9946 * inconsistent state for any of these inodes or leading to any
9947 * inconsistencies when replayed). If the transaction was aborted, the
9948 * abortion reason is propagated to userspace when attempting to commit
9949 * the transaction. If the log does not contain any of these inodes, we
9950 * allow the tasks to sync it.
9952 if (ret
&& log_pinned
) {
9953 if (btrfs_inode_in_log(old_dir
, root
->fs_info
->generation
) ||
9954 btrfs_inode_in_log(new_dir
, root
->fs_info
->generation
) ||
9955 btrfs_inode_in_log(old_inode
, root
->fs_info
->generation
) ||
9957 btrfs_inode_in_log(new_inode
, root
->fs_info
->generation
)))
9958 btrfs_set_log_full_commit(root
->fs_info
, trans
);
9960 btrfs_end_log_trans(root
);
9963 btrfs_end_transaction(trans
, root
);
9965 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9966 up_read(&root
->fs_info
->subvol_sem
);
9971 static int btrfs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
9972 struct inode
*new_dir
, struct dentry
*new_dentry
,
9975 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
9978 if (flags
& RENAME_EXCHANGE
)
9979 return btrfs_rename_exchange(old_dir
, old_dentry
, new_dir
,
9982 return btrfs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
, flags
);
9985 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
9987 struct btrfs_delalloc_work
*delalloc_work
;
9988 struct inode
*inode
;
9990 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
9992 inode
= delalloc_work
->inode
;
9993 filemap_flush(inode
->i_mapping
);
9994 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
9995 &BTRFS_I(inode
)->runtime_flags
))
9996 filemap_flush(inode
->i_mapping
);
9998 if (delalloc_work
->delay_iput
)
9999 btrfs_add_delayed_iput(inode
);
10002 complete(&delalloc_work
->completion
);
10005 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
10008 struct btrfs_delalloc_work
*work
;
10010 work
= kmalloc(sizeof(*work
), GFP_NOFS
);
10014 init_completion(&work
->completion
);
10015 INIT_LIST_HEAD(&work
->list
);
10016 work
->inode
= inode
;
10017 work
->delay_iput
= delay_iput
;
10018 WARN_ON_ONCE(!inode
);
10019 btrfs_init_work(&work
->work
, btrfs_flush_delalloc_helper
,
10020 btrfs_run_delalloc_work
, NULL
, NULL
);
10025 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
10027 wait_for_completion(&work
->completion
);
10032 * some fairly slow code that needs optimization. This walks the list
10033 * of all the inodes with pending delalloc and forces them to disk.
10035 static int __start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
,
10038 struct btrfs_inode
*binode
;
10039 struct inode
*inode
;
10040 struct btrfs_delalloc_work
*work
, *next
;
10041 struct list_head works
;
10042 struct list_head splice
;
10045 INIT_LIST_HEAD(&works
);
10046 INIT_LIST_HEAD(&splice
);
10048 mutex_lock(&root
->delalloc_mutex
);
10049 spin_lock(&root
->delalloc_lock
);
10050 list_splice_init(&root
->delalloc_inodes
, &splice
);
10051 while (!list_empty(&splice
)) {
10052 binode
= list_entry(splice
.next
, struct btrfs_inode
,
10055 list_move_tail(&binode
->delalloc_inodes
,
10056 &root
->delalloc_inodes
);
10057 inode
= igrab(&binode
->vfs_inode
);
10059 cond_resched_lock(&root
->delalloc_lock
);
10062 spin_unlock(&root
->delalloc_lock
);
10064 work
= btrfs_alloc_delalloc_work(inode
, delay_iput
);
10067 btrfs_add_delayed_iput(inode
);
10073 list_add_tail(&work
->list
, &works
);
10074 btrfs_queue_work(root
->fs_info
->flush_workers
,
10077 if (nr
!= -1 && ret
>= nr
)
10080 spin_lock(&root
->delalloc_lock
);
10082 spin_unlock(&root
->delalloc_lock
);
10085 list_for_each_entry_safe(work
, next
, &works
, list
) {
10086 list_del_init(&work
->list
);
10087 btrfs_wait_and_free_delalloc_work(work
);
10090 if (!list_empty_careful(&splice
)) {
10091 spin_lock(&root
->delalloc_lock
);
10092 list_splice_tail(&splice
, &root
->delalloc_inodes
);
10093 spin_unlock(&root
->delalloc_lock
);
10095 mutex_unlock(&root
->delalloc_mutex
);
10099 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
10103 if (test_bit(BTRFS_FS_STATE_ERROR
, &root
->fs_info
->fs_state
))
10106 ret
= __start_delalloc_inodes(root
, delay_iput
, -1);
10110 * the filemap_flush will queue IO into the worker threads, but
10111 * we have to make sure the IO is actually started and that
10112 * ordered extents get created before we return
10114 atomic_inc(&root
->fs_info
->async_submit_draining
);
10115 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
10116 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
10117 wait_event(root
->fs_info
->async_submit_wait
,
10118 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
10119 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
10121 atomic_dec(&root
->fs_info
->async_submit_draining
);
10125 int btrfs_start_delalloc_roots(struct btrfs_fs_info
*fs_info
, int delay_iput
,
10128 struct btrfs_root
*root
;
10129 struct list_head splice
;
10132 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
10135 INIT_LIST_HEAD(&splice
);
10137 mutex_lock(&fs_info
->delalloc_root_mutex
);
10138 spin_lock(&fs_info
->delalloc_root_lock
);
10139 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
10140 while (!list_empty(&splice
) && nr
) {
10141 root
= list_first_entry(&splice
, struct btrfs_root
,
10143 root
= btrfs_grab_fs_root(root
);
10145 list_move_tail(&root
->delalloc_root
,
10146 &fs_info
->delalloc_roots
);
10147 spin_unlock(&fs_info
->delalloc_root_lock
);
10149 ret
= __start_delalloc_inodes(root
, delay_iput
, nr
);
10150 btrfs_put_fs_root(root
);
10158 spin_lock(&fs_info
->delalloc_root_lock
);
10160 spin_unlock(&fs_info
->delalloc_root_lock
);
10163 atomic_inc(&fs_info
->async_submit_draining
);
10164 while (atomic_read(&fs_info
->nr_async_submits
) ||
10165 atomic_read(&fs_info
->async_delalloc_pages
)) {
10166 wait_event(fs_info
->async_submit_wait
,
10167 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
10168 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
10170 atomic_dec(&fs_info
->async_submit_draining
);
10172 if (!list_empty_careful(&splice
)) {
10173 spin_lock(&fs_info
->delalloc_root_lock
);
10174 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
10175 spin_unlock(&fs_info
->delalloc_root_lock
);
10177 mutex_unlock(&fs_info
->delalloc_root_mutex
);
10181 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
10182 const char *symname
)
10184 struct btrfs_trans_handle
*trans
;
10185 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
10186 struct btrfs_path
*path
;
10187 struct btrfs_key key
;
10188 struct inode
*inode
= NULL
;
10190 int drop_inode
= 0;
10196 struct btrfs_file_extent_item
*ei
;
10197 struct extent_buffer
*leaf
;
10199 name_len
= strlen(symname
);
10200 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
10201 return -ENAMETOOLONG
;
10204 * 2 items for inode item and ref
10205 * 2 items for dir items
10206 * 1 item for updating parent inode item
10207 * 1 item for the inline extent item
10208 * 1 item for xattr if selinux is on
10210 trans
= btrfs_start_transaction(root
, 7);
10212 return PTR_ERR(trans
);
10214 err
= btrfs_find_free_ino(root
, &objectid
);
10218 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
10219 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
10220 S_IFLNK
|S_IRWXUGO
, &index
);
10221 if (IS_ERR(inode
)) {
10222 err
= PTR_ERR(inode
);
10227 * If the active LSM wants to access the inode during
10228 * d_instantiate it needs these. Smack checks to see
10229 * if the filesystem supports xattrs by looking at the
10232 inode
->i_fop
= &btrfs_file_operations
;
10233 inode
->i_op
= &btrfs_file_inode_operations
;
10234 inode
->i_mapping
->a_ops
= &btrfs_aops
;
10235 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
10237 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
10239 goto out_unlock_inode
;
10241 path
= btrfs_alloc_path();
10244 goto out_unlock_inode
;
10246 key
.objectid
= btrfs_ino(inode
);
10248 key
.type
= BTRFS_EXTENT_DATA_KEY
;
10249 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
10250 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
10253 btrfs_free_path(path
);
10254 goto out_unlock_inode
;
10256 leaf
= path
->nodes
[0];
10257 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
10258 struct btrfs_file_extent_item
);
10259 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
10260 btrfs_set_file_extent_type(leaf
, ei
,
10261 BTRFS_FILE_EXTENT_INLINE
);
10262 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
10263 btrfs_set_file_extent_compression(leaf
, ei
, 0);
10264 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
10265 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
10267 ptr
= btrfs_file_extent_inline_start(ei
);
10268 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
10269 btrfs_mark_buffer_dirty(leaf
);
10270 btrfs_free_path(path
);
10272 inode
->i_op
= &btrfs_symlink_inode_operations
;
10273 inode_nohighmem(inode
);
10274 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
10275 inode_set_bytes(inode
, name_len
);
10276 btrfs_i_size_write(inode
, name_len
);
10277 err
= btrfs_update_inode(trans
, root
, inode
);
10279 * Last step, add directory indexes for our symlink inode. This is the
10280 * last step to avoid extra cleanup of these indexes if an error happens
10284 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
10287 goto out_unlock_inode
;
10290 unlock_new_inode(inode
);
10291 d_instantiate(dentry
, inode
);
10294 btrfs_end_transaction(trans
, root
);
10296 inode_dec_link_count(inode
);
10299 btrfs_btree_balance_dirty(root
);
10304 unlock_new_inode(inode
);
10308 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
10309 u64 start
, u64 num_bytes
, u64 min_size
,
10310 loff_t actual_len
, u64
*alloc_hint
,
10311 struct btrfs_trans_handle
*trans
)
10313 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
10314 struct extent_map
*em
;
10315 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
10316 struct btrfs_key ins
;
10317 u64 cur_offset
= start
;
10320 u64 last_alloc
= (u64
)-1;
10322 bool own_trans
= true;
10323 u64 end
= start
+ num_bytes
- 1;
10327 while (num_bytes
> 0) {
10329 trans
= btrfs_start_transaction(root
, 3);
10330 if (IS_ERR(trans
)) {
10331 ret
= PTR_ERR(trans
);
10336 cur_bytes
= min_t(u64
, num_bytes
, SZ_256M
);
10337 cur_bytes
= max(cur_bytes
, min_size
);
10339 * If we are severely fragmented we could end up with really
10340 * small allocations, so if the allocator is returning small
10341 * chunks lets make its job easier by only searching for those
10344 cur_bytes
= min(cur_bytes
, last_alloc
);
10345 ret
= btrfs_reserve_extent(root
, cur_bytes
, cur_bytes
,
10346 min_size
, 0, *alloc_hint
, &ins
, 1, 0);
10349 btrfs_end_transaction(trans
, root
);
10352 btrfs_dec_block_group_reservations(root
->fs_info
, ins
.objectid
);
10354 last_alloc
= ins
.offset
;
10355 ret
= insert_reserved_file_extent(trans
, inode
,
10356 cur_offset
, ins
.objectid
,
10357 ins
.offset
, ins
.offset
,
10358 ins
.offset
, 0, 0, 0,
10359 BTRFS_FILE_EXTENT_PREALLOC
);
10361 btrfs_free_reserved_extent(root
, ins
.objectid
,
10363 btrfs_abort_transaction(trans
, ret
);
10365 btrfs_end_transaction(trans
, root
);
10369 btrfs_drop_extent_cache(inode
, cur_offset
,
10370 cur_offset
+ ins
.offset
-1, 0);
10372 em
= alloc_extent_map();
10374 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
10375 &BTRFS_I(inode
)->runtime_flags
);
10379 em
->start
= cur_offset
;
10380 em
->orig_start
= cur_offset
;
10381 em
->len
= ins
.offset
;
10382 em
->block_start
= ins
.objectid
;
10383 em
->block_len
= ins
.offset
;
10384 em
->orig_block_len
= ins
.offset
;
10385 em
->ram_bytes
= ins
.offset
;
10386 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
10387 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
10388 em
->generation
= trans
->transid
;
10391 write_lock(&em_tree
->lock
);
10392 ret
= add_extent_mapping(em_tree
, em
, 1);
10393 write_unlock(&em_tree
->lock
);
10394 if (ret
!= -EEXIST
)
10396 btrfs_drop_extent_cache(inode
, cur_offset
,
10397 cur_offset
+ ins
.offset
- 1,
10400 free_extent_map(em
);
10402 num_bytes
-= ins
.offset
;
10403 cur_offset
+= ins
.offset
;
10404 *alloc_hint
= ins
.objectid
+ ins
.offset
;
10406 inode_inc_iversion(inode
);
10407 inode
->i_ctime
= current_fs_time(inode
->i_sb
);
10408 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
10409 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
10410 (actual_len
> inode
->i_size
) &&
10411 (cur_offset
> inode
->i_size
)) {
10412 if (cur_offset
> actual_len
)
10413 i_size
= actual_len
;
10415 i_size
= cur_offset
;
10416 i_size_write(inode
, i_size
);
10417 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
10420 ret
= btrfs_update_inode(trans
, root
, inode
);
10423 btrfs_abort_transaction(trans
, ret
);
10425 btrfs_end_transaction(trans
, root
);
10430 btrfs_end_transaction(trans
, root
);
10432 if (cur_offset
< end
)
10433 btrfs_free_reserved_data_space(inode
, cur_offset
,
10434 end
- cur_offset
+ 1);
10438 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
10439 u64 start
, u64 num_bytes
, u64 min_size
,
10440 loff_t actual_len
, u64
*alloc_hint
)
10442 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
10443 min_size
, actual_len
, alloc_hint
,
10447 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
10448 struct btrfs_trans_handle
*trans
, int mode
,
10449 u64 start
, u64 num_bytes
, u64 min_size
,
10450 loff_t actual_len
, u64
*alloc_hint
)
10452 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
10453 min_size
, actual_len
, alloc_hint
, trans
);
10456 static int btrfs_set_page_dirty(struct page
*page
)
10458 return __set_page_dirty_nobuffers(page
);
10461 static int btrfs_permission(struct inode
*inode
, int mask
)
10463 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
10464 umode_t mode
= inode
->i_mode
;
10466 if (mask
& MAY_WRITE
&&
10467 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
10468 if (btrfs_root_readonly(root
))
10470 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
10473 return generic_permission(inode
, mask
);
10476 static int btrfs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
10478 struct btrfs_trans_handle
*trans
;
10479 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
10480 struct inode
*inode
= NULL
;
10486 * 5 units required for adding orphan entry
10488 trans
= btrfs_start_transaction(root
, 5);
10490 return PTR_ERR(trans
);
10492 ret
= btrfs_find_free_ino(root
, &objectid
);
10496 inode
= btrfs_new_inode(trans
, root
, dir
, NULL
, 0,
10497 btrfs_ino(dir
), objectid
, mode
, &index
);
10498 if (IS_ERR(inode
)) {
10499 ret
= PTR_ERR(inode
);
10504 inode
->i_fop
= &btrfs_file_operations
;
10505 inode
->i_op
= &btrfs_file_inode_operations
;
10507 inode
->i_mapping
->a_ops
= &btrfs_aops
;
10508 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
10510 ret
= btrfs_init_inode_security(trans
, inode
, dir
, NULL
);
10514 ret
= btrfs_update_inode(trans
, root
, inode
);
10517 ret
= btrfs_orphan_add(trans
, inode
);
10522 * We set number of links to 0 in btrfs_new_inode(), and here we set
10523 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10526 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10528 set_nlink(inode
, 1);
10529 unlock_new_inode(inode
);
10530 d_tmpfile(dentry
, inode
);
10531 mark_inode_dirty(inode
);
10534 btrfs_end_transaction(trans
, root
);
10537 btrfs_balance_delayed_items(root
);
10538 btrfs_btree_balance_dirty(root
);
10542 unlock_new_inode(inode
);
10547 /* Inspired by filemap_check_errors() */
10548 int btrfs_inode_check_errors(struct inode
*inode
)
10552 if (test_bit(AS_ENOSPC
, &inode
->i_mapping
->flags
) &&
10553 test_and_clear_bit(AS_ENOSPC
, &inode
->i_mapping
->flags
))
10555 if (test_bit(AS_EIO
, &inode
->i_mapping
->flags
) &&
10556 test_and_clear_bit(AS_EIO
, &inode
->i_mapping
->flags
))
10562 static const struct inode_operations btrfs_dir_inode_operations
= {
10563 .getattr
= btrfs_getattr
,
10564 .lookup
= btrfs_lookup
,
10565 .create
= btrfs_create
,
10566 .unlink
= btrfs_unlink
,
10567 .link
= btrfs_link
,
10568 .mkdir
= btrfs_mkdir
,
10569 .rmdir
= btrfs_rmdir
,
10570 .rename2
= btrfs_rename2
,
10571 .symlink
= btrfs_symlink
,
10572 .setattr
= btrfs_setattr
,
10573 .mknod
= btrfs_mknod
,
10574 .setxattr
= generic_setxattr
,
10575 .getxattr
= generic_getxattr
,
10576 .listxattr
= btrfs_listxattr
,
10577 .removexattr
= generic_removexattr
,
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 .setxattr
= generic_setxattr
,
10652 .getxattr
= generic_getxattr
,
10653 .listxattr
= btrfs_listxattr
,
10654 .removexattr
= generic_removexattr
,
10655 .permission
= btrfs_permission
,
10656 .fiemap
= btrfs_fiemap
,
10657 .get_acl
= btrfs_get_acl
,
10658 .set_acl
= btrfs_set_acl
,
10659 .update_time
= btrfs_update_time
,
10661 static const struct inode_operations btrfs_special_inode_operations
= {
10662 .getattr
= btrfs_getattr
,
10663 .setattr
= btrfs_setattr
,
10664 .permission
= btrfs_permission
,
10665 .setxattr
= generic_setxattr
,
10666 .getxattr
= generic_getxattr
,
10667 .listxattr
= btrfs_listxattr
,
10668 .removexattr
= generic_removexattr
,
10669 .get_acl
= btrfs_get_acl
,
10670 .set_acl
= btrfs_set_acl
,
10671 .update_time
= btrfs_update_time
,
10673 static const struct inode_operations btrfs_symlink_inode_operations
= {
10674 .readlink
= generic_readlink
,
10675 .get_link
= page_get_link
,
10676 .getattr
= btrfs_getattr
,
10677 .setattr
= btrfs_setattr
,
10678 .permission
= btrfs_permission
,
10679 .setxattr
= generic_setxattr
,
10680 .getxattr
= generic_getxattr
,
10681 .listxattr
= btrfs_listxattr
,
10682 .removexattr
= generic_removexattr
,
10683 .update_time
= btrfs_update_time
,
10686 const struct dentry_operations btrfs_dentry_operations
= {
10687 .d_delete
= btrfs_dentry_delete
,
10688 .d_release
= btrfs_dentry_release
,