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"
64 struct btrfs_iget_args
{
65 struct btrfs_key
*location
;
66 struct btrfs_root
*root
;
69 struct btrfs_dio_data
{
70 u64 outstanding_extents
;
72 u64 unsubmitted_oe_range_start
;
73 u64 unsubmitted_oe_range_end
;
76 static const struct inode_operations btrfs_dir_inode_operations
;
77 static const struct inode_operations btrfs_symlink_inode_operations
;
78 static const struct inode_operations btrfs_dir_ro_inode_operations
;
79 static const struct inode_operations btrfs_special_inode_operations
;
80 static const struct inode_operations btrfs_file_inode_operations
;
81 static const struct address_space_operations btrfs_aops
;
82 static const struct address_space_operations btrfs_symlink_aops
;
83 static const struct file_operations btrfs_dir_file_operations
;
84 static struct extent_io_ops btrfs_extent_io_ops
;
86 static struct kmem_cache
*btrfs_inode_cachep
;
87 static struct kmem_cache
*btrfs_delalloc_work_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 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
, int *page_started
,
110 unsigned long *nr_written
, int unlock
);
111 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
112 u64 len
, u64 orig_start
,
113 u64 block_start
, u64 block_len
,
114 u64 orig_block_len
, u64 ram_bytes
,
117 static int btrfs_dirty_inode(struct inode
*inode
);
119 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
120 void btrfs_test_inode_set_ops(struct inode
*inode
)
122 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
126 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
127 struct inode
*inode
, struct inode
*dir
,
128 const struct qstr
*qstr
)
132 err
= btrfs_init_acl(trans
, inode
, dir
);
134 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
139 * this does all the hard work for inserting an inline extent into
140 * the btree. The caller should have done a btrfs_drop_extents so that
141 * no overlapping inline items exist in the btree
143 static int insert_inline_extent(struct btrfs_trans_handle
*trans
,
144 struct btrfs_path
*path
, int extent_inserted
,
145 struct btrfs_root
*root
, struct inode
*inode
,
146 u64 start
, size_t size
, size_t compressed_size
,
148 struct page
**compressed_pages
)
150 struct extent_buffer
*leaf
;
151 struct page
*page
= NULL
;
154 struct btrfs_file_extent_item
*ei
;
157 size_t cur_size
= size
;
158 unsigned long offset
;
160 if (compressed_size
&& compressed_pages
)
161 cur_size
= compressed_size
;
163 inode_add_bytes(inode
, size
);
165 if (!extent_inserted
) {
166 struct btrfs_key key
;
169 key
.objectid
= btrfs_ino(inode
);
171 key
.type
= BTRFS_EXTENT_DATA_KEY
;
173 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
174 path
->leave_spinning
= 1;
175 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
182 leaf
= path
->nodes
[0];
183 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
184 struct btrfs_file_extent_item
);
185 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
186 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
187 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
188 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
189 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
190 ptr
= btrfs_file_extent_inline_start(ei
);
192 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
195 while (compressed_size
> 0) {
196 cpage
= compressed_pages
[i
];
197 cur_size
= min_t(unsigned long, compressed_size
,
200 kaddr
= kmap_atomic(cpage
);
201 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
202 kunmap_atomic(kaddr
);
206 compressed_size
-= cur_size
;
208 btrfs_set_file_extent_compression(leaf
, ei
,
211 page
= find_get_page(inode
->i_mapping
,
212 start
>> PAGE_CACHE_SHIFT
);
213 btrfs_set_file_extent_compression(leaf
, ei
, 0);
214 kaddr
= kmap_atomic(page
);
215 offset
= start
& (PAGE_CACHE_SIZE
- 1);
216 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
217 kunmap_atomic(kaddr
);
218 page_cache_release(page
);
220 btrfs_mark_buffer_dirty(leaf
);
221 btrfs_release_path(path
);
224 * we're an inline extent, so nobody can
225 * extend the file past i_size without locking
226 * a page we already have locked.
228 * We must do any isize and inode updates
229 * before we unlock the pages. Otherwise we
230 * could end up racing with unlink.
232 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
233 ret
= btrfs_update_inode(trans
, root
, inode
);
242 * conditionally insert an inline extent into the file. This
243 * does the checks required to make sure the data is small enough
244 * to fit as an inline extent.
246 static noinline
int cow_file_range_inline(struct btrfs_root
*root
,
247 struct inode
*inode
, u64 start
,
248 u64 end
, size_t compressed_size
,
250 struct page
**compressed_pages
)
252 struct btrfs_trans_handle
*trans
;
253 u64 isize
= i_size_read(inode
);
254 u64 actual_end
= min(end
+ 1, isize
);
255 u64 inline_len
= actual_end
- start
;
256 u64 aligned_end
= ALIGN(end
, root
->sectorsize
);
257 u64 data_len
= inline_len
;
259 struct btrfs_path
*path
;
260 int extent_inserted
= 0;
261 u32 extent_item_size
;
264 data_len
= compressed_size
;
267 actual_end
> PAGE_CACHE_SIZE
||
268 data_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
270 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
272 data_len
> root
->fs_info
->max_inline
) {
276 path
= btrfs_alloc_path();
280 trans
= btrfs_join_transaction(root
);
282 btrfs_free_path(path
);
283 return PTR_ERR(trans
);
285 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
287 if (compressed_size
&& compressed_pages
)
288 extent_item_size
= btrfs_file_extent_calc_inline_size(
291 extent_item_size
= btrfs_file_extent_calc_inline_size(
294 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
,
295 start
, aligned_end
, NULL
,
296 1, 1, extent_item_size
, &extent_inserted
);
298 btrfs_abort_transaction(trans
, root
, ret
);
302 if (isize
> actual_end
)
303 inline_len
= min_t(u64
, isize
, actual_end
);
304 ret
= insert_inline_extent(trans
, path
, extent_inserted
,
306 inline_len
, compressed_size
,
307 compress_type
, compressed_pages
);
308 if (ret
&& ret
!= -ENOSPC
) {
309 btrfs_abort_transaction(trans
, root
, ret
);
311 } else if (ret
== -ENOSPC
) {
316 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
317 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
318 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
321 * Don't forget to free the reserved space, as for inlined extent
322 * it won't count as data extent, free them directly here.
323 * And at reserve time, it's always aligned to page size, so
324 * just free one page here.
326 btrfs_qgroup_free_data(inode
, 0, PAGE_CACHE_SIZE
);
327 btrfs_free_path(path
);
328 btrfs_end_transaction(trans
, root
);
332 struct async_extent
{
337 unsigned long nr_pages
;
339 struct list_head list
;
344 struct btrfs_root
*root
;
345 struct page
*locked_page
;
348 struct list_head extents
;
349 struct btrfs_work work
;
352 static noinline
int add_async_extent(struct async_cow
*cow
,
353 u64 start
, u64 ram_size
,
356 unsigned long nr_pages
,
359 struct async_extent
*async_extent
;
361 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
362 BUG_ON(!async_extent
); /* -ENOMEM */
363 async_extent
->start
= start
;
364 async_extent
->ram_size
= ram_size
;
365 async_extent
->compressed_size
= compressed_size
;
366 async_extent
->pages
= pages
;
367 async_extent
->nr_pages
= nr_pages
;
368 async_extent
->compress_type
= compress_type
;
369 list_add_tail(&async_extent
->list
, &cow
->extents
);
373 static inline int inode_need_compress(struct inode
*inode
)
375 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
378 if (btrfs_test_opt(root
, FORCE_COMPRESS
))
380 /* bad compression ratios */
381 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
383 if (btrfs_test_opt(root
, COMPRESS
) ||
384 BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
||
385 BTRFS_I(inode
)->force_compress
)
391 * we create compressed extents in two phases. The first
392 * phase compresses a range of pages that have already been
393 * locked (both pages and state bits are locked).
395 * This is done inside an ordered work queue, and the compression
396 * is spread across many cpus. The actual IO submission is step
397 * two, and the ordered work queue takes care of making sure that
398 * happens in the same order things were put onto the queue by
399 * writepages and friends.
401 * If this code finds it can't get good compression, it puts an
402 * entry onto the work queue to write the uncompressed bytes. This
403 * makes sure that both compressed inodes and uncompressed inodes
404 * are written in the same order that the flusher thread sent them
407 static noinline
void compress_file_range(struct inode
*inode
,
408 struct page
*locked_page
,
410 struct async_cow
*async_cow
,
413 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
415 u64 blocksize
= root
->sectorsize
;
417 u64 isize
= i_size_read(inode
);
419 struct page
**pages
= NULL
;
420 unsigned long nr_pages
;
421 unsigned long nr_pages_ret
= 0;
422 unsigned long total_compressed
= 0;
423 unsigned long total_in
= 0;
424 unsigned long max_compressed
= 128 * 1024;
425 unsigned long max_uncompressed
= 128 * 1024;
428 int compress_type
= root
->fs_info
->compress_type
;
431 /* if this is a small write inside eof, kick off a defrag */
432 if ((end
- start
+ 1) < 16 * 1024 &&
433 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
434 btrfs_add_inode_defrag(NULL
, inode
);
436 actual_end
= min_t(u64
, isize
, end
+ 1);
439 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
440 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
443 * we don't want to send crud past the end of i_size through
444 * compression, that's just a waste of CPU time. So, if the
445 * end of the file is before the start of our current
446 * requested range of bytes, we bail out to the uncompressed
447 * cleanup code that can deal with all of this.
449 * It isn't really the fastest way to fix things, but this is a
450 * very uncommon corner.
452 if (actual_end
<= start
)
453 goto cleanup_and_bail_uncompressed
;
455 total_compressed
= actual_end
- start
;
458 * skip compression for a small file range(<=blocksize) that
459 * isn't an inline extent, since it dosen't save disk space at all.
461 if (total_compressed
<= blocksize
&&
462 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
463 goto cleanup_and_bail_uncompressed
;
465 /* we want to make sure that amount of ram required to uncompress
466 * an extent is reasonable, so we limit the total size in ram
467 * of a compressed extent to 128k. This is a crucial number
468 * because it also controls how easily we can spread reads across
469 * cpus for decompression.
471 * We also want to make sure the amount of IO required to do
472 * a random read is reasonably small, so we limit the size of
473 * a compressed extent to 128k.
475 total_compressed
= min(total_compressed
, max_uncompressed
);
476 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
477 num_bytes
= max(blocksize
, num_bytes
);
482 * we do compression for mount -o compress and when the
483 * inode has not been flagged as nocompress. This flag can
484 * change at any time if we discover bad compression ratios.
486 if (inode_need_compress(inode
)) {
488 pages
= kcalloc(nr_pages
, sizeof(struct page
*), GFP_NOFS
);
490 /* just bail out to the uncompressed code */
494 if (BTRFS_I(inode
)->force_compress
)
495 compress_type
= BTRFS_I(inode
)->force_compress
;
498 * we need to call clear_page_dirty_for_io on each
499 * page in the range. Otherwise applications with the file
500 * mmap'd can wander in and change the page contents while
501 * we are compressing them.
503 * If the compression fails for any reason, we set the pages
504 * dirty again later on.
506 extent_range_clear_dirty_for_io(inode
, start
, end
);
508 ret
= btrfs_compress_pages(compress_type
,
509 inode
->i_mapping
, start
,
510 total_compressed
, pages
,
511 nr_pages
, &nr_pages_ret
,
517 unsigned long offset
= total_compressed
&
518 (PAGE_CACHE_SIZE
- 1);
519 struct page
*page
= pages
[nr_pages_ret
- 1];
522 /* zero the tail end of the last page, we might be
523 * sending it down to disk
526 kaddr
= kmap_atomic(page
);
527 memset(kaddr
+ offset
, 0,
528 PAGE_CACHE_SIZE
- offset
);
529 kunmap_atomic(kaddr
);
536 /* lets try to make an inline extent */
537 if (ret
|| total_in
< (actual_end
- start
)) {
538 /* we didn't compress the entire range, try
539 * to make an uncompressed inline extent.
541 ret
= cow_file_range_inline(root
, inode
, start
, end
,
544 /* try making a compressed inline extent */
545 ret
= cow_file_range_inline(root
, inode
, start
, end
,
547 compress_type
, pages
);
550 unsigned long clear_flags
= EXTENT_DELALLOC
|
552 unsigned long page_error_op
;
554 clear_flags
|= (ret
< 0) ? EXTENT_DO_ACCOUNTING
: 0;
555 page_error_op
= ret
< 0 ? PAGE_SET_ERROR
: 0;
558 * inline extent creation worked or returned error,
559 * we don't need to create any more async work items.
560 * Unlock and free up our temp pages.
562 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
,
563 clear_flags
, PAGE_UNLOCK
|
574 * we aren't doing an inline extent round the compressed size
575 * up to a block size boundary so the allocator does sane
578 total_compressed
= ALIGN(total_compressed
, blocksize
);
581 * one last check to make sure the compression is really a
582 * win, compare the page count read with the blocks on disk
584 total_in
= ALIGN(total_in
, PAGE_CACHE_SIZE
);
585 if (total_compressed
>= total_in
) {
588 num_bytes
= total_in
;
591 if (!will_compress
&& pages
) {
593 * the compression code ran but failed to make things smaller,
594 * free any pages it allocated and our page pointer array
596 for (i
= 0; i
< nr_pages_ret
; i
++) {
597 WARN_ON(pages
[i
]->mapping
);
598 page_cache_release(pages
[i
]);
602 total_compressed
= 0;
605 /* flag the file so we don't compress in the future */
606 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
607 !(BTRFS_I(inode
)->force_compress
)) {
608 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
614 /* the async work queues will take care of doing actual
615 * allocation on disk for these compressed pages,
616 * and will submit them to the elevator.
618 add_async_extent(async_cow
, start
, num_bytes
,
619 total_compressed
, pages
, nr_pages_ret
,
622 if (start
+ num_bytes
< end
) {
629 cleanup_and_bail_uncompressed
:
631 * No compression, but we still need to write the pages in
632 * the file we've been given so far. redirty the locked
633 * page if it corresponds to our extent and set things up
634 * for the async work queue to run cow_file_range to do
635 * the normal delalloc dance
637 if (page_offset(locked_page
) >= start
&&
638 page_offset(locked_page
) <= end
) {
639 __set_page_dirty_nobuffers(locked_page
);
640 /* unlocked later on in the async handlers */
643 extent_range_redirty_for_io(inode
, start
, end
);
644 add_async_extent(async_cow
, start
, end
- start
+ 1,
645 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
652 for (i
= 0; i
< nr_pages_ret
; i
++) {
653 WARN_ON(pages
[i
]->mapping
);
654 page_cache_release(pages
[i
]);
659 static void free_async_extent_pages(struct async_extent
*async_extent
)
663 if (!async_extent
->pages
)
666 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
667 WARN_ON(async_extent
->pages
[i
]->mapping
);
668 page_cache_release(async_extent
->pages
[i
]);
670 kfree(async_extent
->pages
);
671 async_extent
->nr_pages
= 0;
672 async_extent
->pages
= NULL
;
676 * phase two of compressed writeback. This is the ordered portion
677 * of the code, which only gets called in the order the work was
678 * queued. We walk all the async extents created by compress_file_range
679 * and send them down to the disk.
681 static noinline
void submit_compressed_extents(struct inode
*inode
,
682 struct async_cow
*async_cow
)
684 struct async_extent
*async_extent
;
686 struct btrfs_key ins
;
687 struct extent_map
*em
;
688 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
689 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
690 struct extent_io_tree
*io_tree
;
694 while (!list_empty(&async_cow
->extents
)) {
695 async_extent
= list_entry(async_cow
->extents
.next
,
696 struct async_extent
, list
);
697 list_del(&async_extent
->list
);
699 io_tree
= &BTRFS_I(inode
)->io_tree
;
702 /* did the compression code fall back to uncompressed IO? */
703 if (!async_extent
->pages
) {
704 int page_started
= 0;
705 unsigned long nr_written
= 0;
707 lock_extent(io_tree
, async_extent
->start
,
708 async_extent
->start
+
709 async_extent
->ram_size
- 1);
711 /* allocate blocks */
712 ret
= cow_file_range(inode
, async_cow
->locked_page
,
714 async_extent
->start
+
715 async_extent
->ram_size
- 1,
716 &page_started
, &nr_written
, 0);
721 * if page_started, cow_file_range inserted an
722 * inline extent and took care of all the unlocking
723 * and IO for us. Otherwise, we need to submit
724 * all those pages down to the drive.
726 if (!page_started
&& !ret
)
727 extent_write_locked_range(io_tree
,
728 inode
, async_extent
->start
,
729 async_extent
->start
+
730 async_extent
->ram_size
- 1,
734 unlock_page(async_cow
->locked_page
);
740 lock_extent(io_tree
, async_extent
->start
,
741 async_extent
->start
+ async_extent
->ram_size
- 1);
743 ret
= btrfs_reserve_extent(root
,
744 async_extent
->compressed_size
,
745 async_extent
->compressed_size
,
746 0, alloc_hint
, &ins
, 1, 1);
748 free_async_extent_pages(async_extent
);
750 if (ret
== -ENOSPC
) {
751 unlock_extent(io_tree
, async_extent
->start
,
752 async_extent
->start
+
753 async_extent
->ram_size
- 1);
756 * we need to redirty the pages if we decide to
757 * fallback to uncompressed IO, otherwise we
758 * will not submit these pages down to lower
761 extent_range_redirty_for_io(inode
,
763 async_extent
->start
+
764 async_extent
->ram_size
- 1);
771 * here we're doing allocation and writeback of the
774 btrfs_drop_extent_cache(inode
, async_extent
->start
,
775 async_extent
->start
+
776 async_extent
->ram_size
- 1, 0);
778 em
= alloc_extent_map();
781 goto out_free_reserve
;
783 em
->start
= async_extent
->start
;
784 em
->len
= async_extent
->ram_size
;
785 em
->orig_start
= em
->start
;
786 em
->mod_start
= em
->start
;
787 em
->mod_len
= em
->len
;
789 em
->block_start
= ins
.objectid
;
790 em
->block_len
= ins
.offset
;
791 em
->orig_block_len
= ins
.offset
;
792 em
->ram_bytes
= async_extent
->ram_size
;
793 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
794 em
->compress_type
= async_extent
->compress_type
;
795 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
796 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
800 write_lock(&em_tree
->lock
);
801 ret
= add_extent_mapping(em_tree
, em
, 1);
802 write_unlock(&em_tree
->lock
);
803 if (ret
!= -EEXIST
) {
807 btrfs_drop_extent_cache(inode
, async_extent
->start
,
808 async_extent
->start
+
809 async_extent
->ram_size
- 1, 0);
813 goto out_free_reserve
;
815 ret
= btrfs_add_ordered_extent_compress(inode
,
818 async_extent
->ram_size
,
820 BTRFS_ORDERED_COMPRESSED
,
821 async_extent
->compress_type
);
823 btrfs_drop_extent_cache(inode
, async_extent
->start
,
824 async_extent
->start
+
825 async_extent
->ram_size
- 1, 0);
826 goto out_free_reserve
;
830 * clear dirty, set writeback and unlock the pages.
832 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
833 async_extent
->start
+
834 async_extent
->ram_size
- 1,
835 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
,
836 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
838 ret
= btrfs_submit_compressed_write(inode
,
840 async_extent
->ram_size
,
842 ins
.offset
, async_extent
->pages
,
843 async_extent
->nr_pages
);
845 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
846 struct page
*p
= async_extent
->pages
[0];
847 const u64 start
= async_extent
->start
;
848 const u64 end
= start
+ async_extent
->ram_size
- 1;
850 p
->mapping
= inode
->i_mapping
;
851 tree
->ops
->writepage_end_io_hook(p
, start
, end
,
854 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
, 0,
857 free_async_extent_pages(async_extent
);
859 alloc_hint
= ins
.objectid
+ ins
.offset
;
865 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
867 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
868 async_extent
->start
+
869 async_extent
->ram_size
- 1,
870 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
|
871 EXTENT_DEFRAG
| EXTENT_DO_ACCOUNTING
,
872 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
873 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
|
875 free_async_extent_pages(async_extent
);
880 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
883 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
884 struct extent_map
*em
;
887 read_lock(&em_tree
->lock
);
888 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
891 * if block start isn't an actual block number then find the
892 * first block in this inode and use that as a hint. If that
893 * block is also bogus then just don't worry about it.
895 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
897 em
= search_extent_mapping(em_tree
, 0, 0);
898 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
899 alloc_hint
= em
->block_start
;
903 alloc_hint
= em
->block_start
;
907 read_unlock(&em_tree
->lock
);
913 * when extent_io.c finds a delayed allocation range in the file,
914 * the call backs end up in this code. The basic idea is to
915 * allocate extents on disk for the range, and create ordered data structs
916 * in ram to track those extents.
918 * locked_page is the page that writepage had locked already. We use
919 * it to make sure we don't do extra locks or unlocks.
921 * *page_started is set to one if we unlock locked_page and do everything
922 * required to start IO on it. It may be clean and already done with
925 static noinline
int cow_file_range(struct inode
*inode
,
926 struct page
*locked_page
,
927 u64 start
, u64 end
, int *page_started
,
928 unsigned long *nr_written
,
931 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
934 unsigned long ram_size
;
937 u64 blocksize
= root
->sectorsize
;
938 struct btrfs_key ins
;
939 struct extent_map
*em
;
940 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
943 if (btrfs_is_free_space_inode(inode
)) {
949 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
950 num_bytes
= max(blocksize
, num_bytes
);
951 disk_num_bytes
= num_bytes
;
953 /* if this is a small write inside eof, kick off defrag */
954 if (num_bytes
< 64 * 1024 &&
955 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
956 btrfs_add_inode_defrag(NULL
, inode
);
959 /* lets try to make an inline extent */
960 ret
= cow_file_range_inline(root
, inode
, start
, end
, 0, 0,
963 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
,
964 EXTENT_LOCKED
| EXTENT_DELALLOC
|
965 EXTENT_DEFRAG
, PAGE_UNLOCK
|
966 PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
969 *nr_written
= *nr_written
+
970 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
973 } else if (ret
< 0) {
978 BUG_ON(disk_num_bytes
>
979 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
981 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
982 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
984 while (disk_num_bytes
> 0) {
987 cur_alloc_size
= disk_num_bytes
;
988 ret
= btrfs_reserve_extent(root
, cur_alloc_size
,
989 root
->sectorsize
, 0, alloc_hint
,
994 em
= alloc_extent_map();
1000 em
->orig_start
= em
->start
;
1001 ram_size
= ins
.offset
;
1002 em
->len
= ins
.offset
;
1003 em
->mod_start
= em
->start
;
1004 em
->mod_len
= em
->len
;
1006 em
->block_start
= ins
.objectid
;
1007 em
->block_len
= ins
.offset
;
1008 em
->orig_block_len
= ins
.offset
;
1009 em
->ram_bytes
= ram_size
;
1010 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1011 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1012 em
->generation
= -1;
1015 write_lock(&em_tree
->lock
);
1016 ret
= add_extent_mapping(em_tree
, em
, 1);
1017 write_unlock(&em_tree
->lock
);
1018 if (ret
!= -EEXIST
) {
1019 free_extent_map(em
);
1022 btrfs_drop_extent_cache(inode
, start
,
1023 start
+ ram_size
- 1, 0);
1028 cur_alloc_size
= ins
.offset
;
1029 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
1030 ram_size
, cur_alloc_size
, 0);
1032 goto out_drop_extent_cache
;
1034 if (root
->root_key
.objectid
==
1035 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1036 ret
= btrfs_reloc_clone_csums(inode
, start
,
1039 goto out_drop_extent_cache
;
1042 if (disk_num_bytes
< cur_alloc_size
)
1045 /* we're not doing compressed IO, don't unlock the first
1046 * page (which the caller expects to stay locked), don't
1047 * clear any dirty bits and don't set any writeback bits
1049 * Do set the Private2 bit so we know this page was properly
1050 * setup for writepage
1052 op
= unlock
? PAGE_UNLOCK
: 0;
1053 op
|= PAGE_SET_PRIVATE2
;
1055 extent_clear_unlock_delalloc(inode
, start
,
1056 start
+ ram_size
- 1, locked_page
,
1057 EXTENT_LOCKED
| EXTENT_DELALLOC
,
1059 disk_num_bytes
-= cur_alloc_size
;
1060 num_bytes
-= cur_alloc_size
;
1061 alloc_hint
= ins
.objectid
+ ins
.offset
;
1062 start
+= cur_alloc_size
;
1067 out_drop_extent_cache
:
1068 btrfs_drop_extent_cache(inode
, start
, start
+ ram_size
- 1, 0);
1070 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
1072 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1073 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
1074 EXTENT_DELALLOC
| EXTENT_DEFRAG
,
1075 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
1076 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
);
1081 * work queue call back to started compression on a file and pages
1083 static noinline
void async_cow_start(struct btrfs_work
*work
)
1085 struct async_cow
*async_cow
;
1087 async_cow
= container_of(work
, struct async_cow
, work
);
1089 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1090 async_cow
->start
, async_cow
->end
, async_cow
,
1092 if (num_added
== 0) {
1093 btrfs_add_delayed_iput(async_cow
->inode
);
1094 async_cow
->inode
= NULL
;
1099 * work queue call back to submit previously compressed pages
1101 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1103 struct async_cow
*async_cow
;
1104 struct btrfs_root
*root
;
1105 unsigned long nr_pages
;
1107 async_cow
= container_of(work
, struct async_cow
, work
);
1109 root
= async_cow
->root
;
1110 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1114 * atomic_sub_return implies a barrier for waitqueue_active
1116 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1118 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1119 wake_up(&root
->fs_info
->async_submit_wait
);
1121 if (async_cow
->inode
)
1122 submit_compressed_extents(async_cow
->inode
, async_cow
);
1125 static noinline
void async_cow_free(struct btrfs_work
*work
)
1127 struct async_cow
*async_cow
;
1128 async_cow
= container_of(work
, struct async_cow
, work
);
1129 if (async_cow
->inode
)
1130 btrfs_add_delayed_iput(async_cow
->inode
);
1134 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1135 u64 start
, u64 end
, int *page_started
,
1136 unsigned long *nr_written
)
1138 struct async_cow
*async_cow
;
1139 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1140 unsigned long nr_pages
;
1142 int limit
= 10 * 1024 * 1024;
1144 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1145 1, 0, NULL
, GFP_NOFS
);
1146 while (start
< end
) {
1147 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1148 BUG_ON(!async_cow
); /* -ENOMEM */
1149 async_cow
->inode
= igrab(inode
);
1150 async_cow
->root
= root
;
1151 async_cow
->locked_page
= locked_page
;
1152 async_cow
->start
= start
;
1154 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
&&
1155 !btrfs_test_opt(root
, FORCE_COMPRESS
))
1158 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1160 async_cow
->end
= cur_end
;
1161 INIT_LIST_HEAD(&async_cow
->extents
);
1163 btrfs_init_work(&async_cow
->work
,
1164 btrfs_delalloc_helper
,
1165 async_cow_start
, async_cow_submit
,
1168 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1170 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1172 btrfs_queue_work(root
->fs_info
->delalloc_workers
,
1175 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1176 wait_event(root
->fs_info
->async_submit_wait
,
1177 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1181 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1182 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1183 wait_event(root
->fs_info
->async_submit_wait
,
1184 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1188 *nr_written
+= nr_pages
;
1189 start
= cur_end
+ 1;
1195 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1196 u64 bytenr
, u64 num_bytes
)
1199 struct btrfs_ordered_sum
*sums
;
1202 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1203 bytenr
+ num_bytes
- 1, &list
, 0);
1204 if (ret
== 0 && list_empty(&list
))
1207 while (!list_empty(&list
)) {
1208 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1209 list_del(&sums
->list
);
1216 * when nowcow writeback call back. This checks for snapshots or COW copies
1217 * of the extents that exist in the file, and COWs the file as required.
1219 * If no cow copies or snapshots exist, we write directly to the existing
1222 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1223 struct page
*locked_page
,
1224 u64 start
, u64 end
, int *page_started
, int force
,
1225 unsigned long *nr_written
)
1227 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1228 struct btrfs_trans_handle
*trans
;
1229 struct extent_buffer
*leaf
;
1230 struct btrfs_path
*path
;
1231 struct btrfs_file_extent_item
*fi
;
1232 struct btrfs_key found_key
;
1247 u64 ino
= btrfs_ino(inode
);
1249 path
= btrfs_alloc_path();
1251 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1252 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1253 EXTENT_DO_ACCOUNTING
|
1254 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1256 PAGE_SET_WRITEBACK
|
1257 PAGE_END_WRITEBACK
);
1261 nolock
= btrfs_is_free_space_inode(inode
);
1264 trans
= btrfs_join_transaction_nolock(root
);
1266 trans
= btrfs_join_transaction(root
);
1268 if (IS_ERR(trans
)) {
1269 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1270 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1271 EXTENT_DO_ACCOUNTING
|
1272 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1274 PAGE_SET_WRITEBACK
|
1275 PAGE_END_WRITEBACK
);
1276 btrfs_free_path(path
);
1277 return PTR_ERR(trans
);
1280 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1282 cow_start
= (u64
)-1;
1285 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1289 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1290 leaf
= path
->nodes
[0];
1291 btrfs_item_key_to_cpu(leaf
, &found_key
,
1292 path
->slots
[0] - 1);
1293 if (found_key
.objectid
== ino
&&
1294 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1299 leaf
= path
->nodes
[0];
1300 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1301 ret
= btrfs_next_leaf(root
, path
);
1306 leaf
= path
->nodes
[0];
1312 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1314 if (found_key
.objectid
> ino
)
1316 if (WARN_ON_ONCE(found_key
.objectid
< ino
) ||
1317 found_key
.type
< BTRFS_EXTENT_DATA_KEY
) {
1321 if (found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1322 found_key
.offset
> end
)
1325 if (found_key
.offset
> cur_offset
) {
1326 extent_end
= found_key
.offset
;
1331 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1332 struct btrfs_file_extent_item
);
1333 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1335 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1336 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1337 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1338 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1339 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1340 extent_end
= found_key
.offset
+
1341 btrfs_file_extent_num_bytes(leaf
, fi
);
1343 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1344 if (extent_end
<= start
) {
1348 if (disk_bytenr
== 0)
1350 if (btrfs_file_extent_compression(leaf
, fi
) ||
1351 btrfs_file_extent_encryption(leaf
, fi
) ||
1352 btrfs_file_extent_other_encoding(leaf
, fi
))
1354 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1356 if (btrfs_extent_readonly(root
, disk_bytenr
))
1358 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1360 extent_offset
, disk_bytenr
))
1362 disk_bytenr
+= extent_offset
;
1363 disk_bytenr
+= cur_offset
- found_key
.offset
;
1364 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1366 * if there are pending snapshots for this root,
1367 * we fall into common COW way.
1370 err
= btrfs_start_write_no_snapshoting(root
);
1375 * force cow if csum exists in the range.
1376 * this ensure that csum for a given extent are
1377 * either valid or do not exist.
1379 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1382 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1383 extent_end
= found_key
.offset
+
1384 btrfs_file_extent_inline_len(leaf
,
1385 path
->slots
[0], fi
);
1386 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1391 if (extent_end
<= start
) {
1393 if (!nolock
&& nocow
)
1394 btrfs_end_write_no_snapshoting(root
);
1398 if (cow_start
== (u64
)-1)
1399 cow_start
= cur_offset
;
1400 cur_offset
= extent_end
;
1401 if (cur_offset
> end
)
1407 btrfs_release_path(path
);
1408 if (cow_start
!= (u64
)-1) {
1409 ret
= cow_file_range(inode
, locked_page
,
1410 cow_start
, found_key
.offset
- 1,
1411 page_started
, nr_written
, 1);
1413 if (!nolock
&& nocow
)
1414 btrfs_end_write_no_snapshoting(root
);
1417 cow_start
= (u64
)-1;
1420 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1421 struct extent_map
*em
;
1422 struct extent_map_tree
*em_tree
;
1423 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1424 em
= alloc_extent_map();
1425 BUG_ON(!em
); /* -ENOMEM */
1426 em
->start
= cur_offset
;
1427 em
->orig_start
= found_key
.offset
- extent_offset
;
1428 em
->len
= num_bytes
;
1429 em
->block_len
= num_bytes
;
1430 em
->block_start
= disk_bytenr
;
1431 em
->orig_block_len
= disk_num_bytes
;
1432 em
->ram_bytes
= ram_bytes
;
1433 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1434 em
->mod_start
= em
->start
;
1435 em
->mod_len
= em
->len
;
1436 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1437 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1438 em
->generation
= -1;
1440 write_lock(&em_tree
->lock
);
1441 ret
= add_extent_mapping(em_tree
, em
, 1);
1442 write_unlock(&em_tree
->lock
);
1443 if (ret
!= -EEXIST
) {
1444 free_extent_map(em
);
1447 btrfs_drop_extent_cache(inode
, em
->start
,
1448 em
->start
+ em
->len
- 1, 0);
1450 type
= BTRFS_ORDERED_PREALLOC
;
1452 type
= BTRFS_ORDERED_NOCOW
;
1455 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1456 num_bytes
, num_bytes
, type
);
1457 BUG_ON(ret
); /* -ENOMEM */
1459 if (root
->root_key
.objectid
==
1460 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1461 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1464 if (!nolock
&& nocow
)
1465 btrfs_end_write_no_snapshoting(root
);
1470 extent_clear_unlock_delalloc(inode
, cur_offset
,
1471 cur_offset
+ num_bytes
- 1,
1472 locked_page
, EXTENT_LOCKED
|
1473 EXTENT_DELALLOC
, PAGE_UNLOCK
|
1475 if (!nolock
&& nocow
)
1476 btrfs_end_write_no_snapshoting(root
);
1477 cur_offset
= extent_end
;
1478 if (cur_offset
> end
)
1481 btrfs_release_path(path
);
1483 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1484 cow_start
= cur_offset
;
1488 if (cow_start
!= (u64
)-1) {
1489 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1490 page_started
, nr_written
, 1);
1496 err
= btrfs_end_transaction(trans
, root
);
1500 if (ret
&& cur_offset
< end
)
1501 extent_clear_unlock_delalloc(inode
, cur_offset
, end
,
1502 locked_page
, EXTENT_LOCKED
|
1503 EXTENT_DELALLOC
| EXTENT_DEFRAG
|
1504 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1506 PAGE_SET_WRITEBACK
|
1507 PAGE_END_WRITEBACK
);
1508 btrfs_free_path(path
);
1512 static inline int need_force_cow(struct inode
*inode
, u64 start
, u64 end
)
1515 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
1516 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
))
1520 * @defrag_bytes is a hint value, no spinlock held here,
1521 * if is not zero, it means the file is defragging.
1522 * Force cow if given extent needs to be defragged.
1524 if (BTRFS_I(inode
)->defrag_bytes
&&
1525 test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
1526 EXTENT_DEFRAG
, 0, NULL
))
1533 * extent_io.c call back to do delayed allocation processing
1535 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1536 u64 start
, u64 end
, int *page_started
,
1537 unsigned long *nr_written
)
1540 int force_cow
= need_force_cow(inode
, start
, end
);
1542 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
&& !force_cow
) {
1543 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1544 page_started
, 1, nr_written
);
1545 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
&& !force_cow
) {
1546 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1547 page_started
, 0, nr_written
);
1548 } else if (!inode_need_compress(inode
)) {
1549 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1550 page_started
, nr_written
, 1);
1552 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1553 &BTRFS_I(inode
)->runtime_flags
);
1554 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1555 page_started
, nr_written
);
1560 static void btrfs_split_extent_hook(struct inode
*inode
,
1561 struct extent_state
*orig
, u64 split
)
1565 /* not delalloc, ignore it */
1566 if (!(orig
->state
& EXTENT_DELALLOC
))
1569 size
= orig
->end
- orig
->start
+ 1;
1570 if (size
> BTRFS_MAX_EXTENT_SIZE
) {
1575 * See the explanation in btrfs_merge_extent_hook, the same
1576 * applies here, just in reverse.
1578 new_size
= orig
->end
- split
+ 1;
1579 num_extents
= div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1580 BTRFS_MAX_EXTENT_SIZE
);
1581 new_size
= split
- orig
->start
;
1582 num_extents
+= div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1583 BTRFS_MAX_EXTENT_SIZE
);
1584 if (div64_u64(size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1585 BTRFS_MAX_EXTENT_SIZE
) >= num_extents
)
1589 spin_lock(&BTRFS_I(inode
)->lock
);
1590 BTRFS_I(inode
)->outstanding_extents
++;
1591 spin_unlock(&BTRFS_I(inode
)->lock
);
1595 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1596 * extents so we can keep track of new extents that are just merged onto old
1597 * extents, such as when we are doing sequential writes, so we can properly
1598 * account for the metadata space we'll need.
1600 static void btrfs_merge_extent_hook(struct inode
*inode
,
1601 struct extent_state
*new,
1602 struct extent_state
*other
)
1604 u64 new_size
, old_size
;
1607 /* not delalloc, ignore it */
1608 if (!(other
->state
& EXTENT_DELALLOC
))
1611 if (new->start
> other
->start
)
1612 new_size
= new->end
- other
->start
+ 1;
1614 new_size
= other
->end
- new->start
+ 1;
1616 /* we're not bigger than the max, unreserve the space and go */
1617 if (new_size
<= BTRFS_MAX_EXTENT_SIZE
) {
1618 spin_lock(&BTRFS_I(inode
)->lock
);
1619 BTRFS_I(inode
)->outstanding_extents
--;
1620 spin_unlock(&BTRFS_I(inode
)->lock
);
1625 * We have to add up either side to figure out how many extents were
1626 * accounted for before we merged into one big extent. If the number of
1627 * extents we accounted for is <= the amount we need for the new range
1628 * then we can return, otherwise drop. Think of it like this
1632 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1633 * need 2 outstanding extents, on one side we have 1 and the other side
1634 * we have 1 so they are == and we can return. But in this case
1636 * [MAX_SIZE+4k][MAX_SIZE+4k]
1638 * Each range on their own accounts for 2 extents, but merged together
1639 * they are only 3 extents worth of accounting, so we need to drop in
1642 old_size
= other
->end
- other
->start
+ 1;
1643 num_extents
= div64_u64(old_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1644 BTRFS_MAX_EXTENT_SIZE
);
1645 old_size
= new->end
- new->start
+ 1;
1646 num_extents
+= div64_u64(old_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1647 BTRFS_MAX_EXTENT_SIZE
);
1649 if (div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1650 BTRFS_MAX_EXTENT_SIZE
) >= num_extents
)
1653 spin_lock(&BTRFS_I(inode
)->lock
);
1654 BTRFS_I(inode
)->outstanding_extents
--;
1655 spin_unlock(&BTRFS_I(inode
)->lock
);
1658 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1659 struct inode
*inode
)
1661 spin_lock(&root
->delalloc_lock
);
1662 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1663 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1664 &root
->delalloc_inodes
);
1665 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1666 &BTRFS_I(inode
)->runtime_flags
);
1667 root
->nr_delalloc_inodes
++;
1668 if (root
->nr_delalloc_inodes
== 1) {
1669 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1670 BUG_ON(!list_empty(&root
->delalloc_root
));
1671 list_add_tail(&root
->delalloc_root
,
1672 &root
->fs_info
->delalloc_roots
);
1673 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1676 spin_unlock(&root
->delalloc_lock
);
1679 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1680 struct inode
*inode
)
1682 spin_lock(&root
->delalloc_lock
);
1683 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1684 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1685 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1686 &BTRFS_I(inode
)->runtime_flags
);
1687 root
->nr_delalloc_inodes
--;
1688 if (!root
->nr_delalloc_inodes
) {
1689 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1690 BUG_ON(list_empty(&root
->delalloc_root
));
1691 list_del_init(&root
->delalloc_root
);
1692 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1695 spin_unlock(&root
->delalloc_lock
);
1699 * extent_io.c set_bit_hook, used to track delayed allocation
1700 * bytes in this file, and to maintain the list of inodes that
1701 * have pending delalloc work to be done.
1703 static void btrfs_set_bit_hook(struct inode
*inode
,
1704 struct extent_state
*state
, unsigned *bits
)
1707 if ((*bits
& EXTENT_DEFRAG
) && !(*bits
& EXTENT_DELALLOC
))
1710 * set_bit and clear bit hooks normally require _irqsave/restore
1711 * but in this case, we are only testing for the DELALLOC
1712 * bit, which is only set or cleared with irqs on
1714 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1715 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1716 u64 len
= state
->end
+ 1 - state
->start
;
1717 bool do_list
= !btrfs_is_free_space_inode(inode
);
1719 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1720 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1722 spin_lock(&BTRFS_I(inode
)->lock
);
1723 BTRFS_I(inode
)->outstanding_extents
++;
1724 spin_unlock(&BTRFS_I(inode
)->lock
);
1727 /* For sanity tests */
1728 if (btrfs_test_is_dummy_root(root
))
1731 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1732 root
->fs_info
->delalloc_batch
);
1733 spin_lock(&BTRFS_I(inode
)->lock
);
1734 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1735 if (*bits
& EXTENT_DEFRAG
)
1736 BTRFS_I(inode
)->defrag_bytes
+= len
;
1737 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1738 &BTRFS_I(inode
)->runtime_flags
))
1739 btrfs_add_delalloc_inodes(root
, inode
);
1740 spin_unlock(&BTRFS_I(inode
)->lock
);
1745 * extent_io.c clear_bit_hook, see set_bit_hook for why
1747 static void btrfs_clear_bit_hook(struct inode
*inode
,
1748 struct extent_state
*state
,
1751 u64 len
= state
->end
+ 1 - state
->start
;
1752 u64 num_extents
= div64_u64(len
+ BTRFS_MAX_EXTENT_SIZE
-1,
1753 BTRFS_MAX_EXTENT_SIZE
);
1755 spin_lock(&BTRFS_I(inode
)->lock
);
1756 if ((state
->state
& EXTENT_DEFRAG
) && (*bits
& EXTENT_DEFRAG
))
1757 BTRFS_I(inode
)->defrag_bytes
-= len
;
1758 spin_unlock(&BTRFS_I(inode
)->lock
);
1761 * set_bit and clear bit hooks normally require _irqsave/restore
1762 * but in this case, we are only testing for the DELALLOC
1763 * bit, which is only set or cleared with irqs on
1765 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1766 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1767 bool do_list
= !btrfs_is_free_space_inode(inode
);
1769 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1770 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1771 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1772 spin_lock(&BTRFS_I(inode
)->lock
);
1773 BTRFS_I(inode
)->outstanding_extents
-= num_extents
;
1774 spin_unlock(&BTRFS_I(inode
)->lock
);
1778 * We don't reserve metadata space for space cache inodes so we
1779 * don't need to call dellalloc_release_metadata if there is an
1782 if (*bits
& EXTENT_DO_ACCOUNTING
&&
1783 root
!= root
->fs_info
->tree_root
)
1784 btrfs_delalloc_release_metadata(inode
, len
);
1786 /* For sanity tests. */
1787 if (btrfs_test_is_dummy_root(root
))
1790 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1791 && do_list
&& !(state
->state
& EXTENT_NORESERVE
))
1792 btrfs_free_reserved_data_space_noquota(inode
,
1795 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1796 root
->fs_info
->delalloc_batch
);
1797 spin_lock(&BTRFS_I(inode
)->lock
);
1798 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1799 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1800 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1801 &BTRFS_I(inode
)->runtime_flags
))
1802 btrfs_del_delalloc_inode(root
, inode
);
1803 spin_unlock(&BTRFS_I(inode
)->lock
);
1808 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1809 * we don't create bios that span stripes or chunks
1811 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1812 size_t size
, struct bio
*bio
,
1813 unsigned long bio_flags
)
1815 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1816 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
1821 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1824 length
= bio
->bi_iter
.bi_size
;
1825 map_length
= length
;
1826 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1827 &map_length
, NULL
, 0);
1828 /* Will always return 0 with map_multi == NULL */
1830 if (map_length
< length
+ size
)
1836 * in order to insert checksums into the metadata in large chunks,
1837 * we wait until bio submission time. All the pages in the bio are
1838 * checksummed and sums are attached onto the ordered extent record.
1840 * At IO completion time the cums attached on the ordered extent record
1841 * are inserted into the btree
1843 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1844 struct bio
*bio
, int mirror_num
,
1845 unsigned long bio_flags
,
1848 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1851 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1852 BUG_ON(ret
); /* -ENOMEM */
1857 * in order to insert checksums into the metadata in large chunks,
1858 * we wait until bio submission time. All the pages in the bio are
1859 * checksummed and sums are attached onto the ordered extent record.
1861 * At IO completion time the cums attached on the ordered extent record
1862 * are inserted into the btree
1864 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1865 int mirror_num
, unsigned long bio_flags
,
1868 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1871 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1873 bio
->bi_error
= ret
;
1880 * extent_io.c submission hook. This does the right thing for csum calculation
1881 * on write, or reading the csums from the tree before a read
1883 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1884 int mirror_num
, unsigned long bio_flags
,
1887 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1888 enum btrfs_wq_endio_type metadata
= BTRFS_WQ_ENDIO_DATA
;
1891 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1893 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1895 if (btrfs_is_free_space_inode(inode
))
1896 metadata
= BTRFS_WQ_ENDIO_FREE_SPACE
;
1898 if (!(rw
& REQ_WRITE
)) {
1899 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1903 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1904 ret
= btrfs_submit_compressed_read(inode
, bio
,
1908 } else if (!skip_sum
) {
1909 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1914 } else if (async
&& !skip_sum
) {
1915 /* csum items have already been cloned */
1916 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1918 /* we're doing a write, do the async checksumming */
1919 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1920 inode
, rw
, bio
, mirror_num
,
1921 bio_flags
, bio_offset
,
1922 __btrfs_submit_bio_start
,
1923 __btrfs_submit_bio_done
);
1925 } else if (!skip_sum
) {
1926 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1932 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1936 bio
->bi_error
= ret
;
1943 * given a list of ordered sums record them in the inode. This happens
1944 * at IO completion time based on sums calculated at bio submission time.
1946 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1947 struct inode
*inode
, u64 file_offset
,
1948 struct list_head
*list
)
1950 struct btrfs_ordered_sum
*sum
;
1952 list_for_each_entry(sum
, list
, list
) {
1953 trans
->adding_csums
= 1;
1954 btrfs_csum_file_blocks(trans
,
1955 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1956 trans
->adding_csums
= 0;
1961 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1962 struct extent_state
**cached_state
)
1964 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1965 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1966 cached_state
, GFP_NOFS
);
1969 /* see btrfs_writepage_start_hook for details on why this is required */
1970 struct btrfs_writepage_fixup
{
1972 struct btrfs_work work
;
1975 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1977 struct btrfs_writepage_fixup
*fixup
;
1978 struct btrfs_ordered_extent
*ordered
;
1979 struct extent_state
*cached_state
= NULL
;
1981 struct inode
*inode
;
1986 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1990 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1991 ClearPageChecked(page
);
1995 inode
= page
->mapping
->host
;
1996 page_start
= page_offset(page
);
1997 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1999 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
2002 /* already ordered? We're done */
2003 if (PagePrivate2(page
))
2006 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
2008 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
2009 page_end
, &cached_state
, GFP_NOFS
);
2011 btrfs_start_ordered_extent(inode
, ordered
, 1);
2012 btrfs_put_ordered_extent(ordered
);
2016 ret
= btrfs_delalloc_reserve_space(inode
, page_start
,
2019 mapping_set_error(page
->mapping
, ret
);
2020 end_extent_writepage(page
, ret
, page_start
, page_end
);
2021 ClearPageChecked(page
);
2025 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
2026 ClearPageChecked(page
);
2027 set_page_dirty(page
);
2029 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
2030 &cached_state
, GFP_NOFS
);
2033 page_cache_release(page
);
2038 * There are a few paths in the higher layers of the kernel that directly
2039 * set the page dirty bit without asking the filesystem if it is a
2040 * good idea. This causes problems because we want to make sure COW
2041 * properly happens and the data=ordered rules are followed.
2043 * In our case any range that doesn't have the ORDERED bit set
2044 * hasn't been properly setup for IO. We kick off an async process
2045 * to fix it up. The async helper will wait for ordered extents, set
2046 * the delalloc bit and make it safe to write the page.
2048 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
2050 struct inode
*inode
= page
->mapping
->host
;
2051 struct btrfs_writepage_fixup
*fixup
;
2052 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2054 /* this page is properly in the ordered list */
2055 if (TestClearPagePrivate2(page
))
2058 if (PageChecked(page
))
2061 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
2065 SetPageChecked(page
);
2066 page_cache_get(page
);
2067 btrfs_init_work(&fixup
->work
, btrfs_fixup_helper
,
2068 btrfs_writepage_fixup_worker
, NULL
, NULL
);
2070 btrfs_queue_work(root
->fs_info
->fixup_workers
, &fixup
->work
);
2074 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
2075 struct inode
*inode
, u64 file_pos
,
2076 u64 disk_bytenr
, u64 disk_num_bytes
,
2077 u64 num_bytes
, u64 ram_bytes
,
2078 u8 compression
, u8 encryption
,
2079 u16 other_encoding
, int extent_type
)
2081 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2082 struct btrfs_file_extent_item
*fi
;
2083 struct btrfs_path
*path
;
2084 struct extent_buffer
*leaf
;
2085 struct btrfs_key ins
;
2086 int extent_inserted
= 0;
2089 path
= btrfs_alloc_path();
2094 * we may be replacing one extent in the tree with another.
2095 * The new extent is pinned in the extent map, and we don't want
2096 * to drop it from the cache until it is completely in the btree.
2098 * So, tell btrfs_drop_extents to leave this extent in the cache.
2099 * the caller is expected to unpin it and allow it to be merged
2102 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
, file_pos
,
2103 file_pos
+ num_bytes
, NULL
, 0,
2104 1, sizeof(*fi
), &extent_inserted
);
2108 if (!extent_inserted
) {
2109 ins
.objectid
= btrfs_ino(inode
);
2110 ins
.offset
= file_pos
;
2111 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
2113 path
->leave_spinning
= 1;
2114 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
2119 leaf
= path
->nodes
[0];
2120 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2121 struct btrfs_file_extent_item
);
2122 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
2123 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
2124 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
2125 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
2126 btrfs_set_file_extent_offset(leaf
, fi
, 0);
2127 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
2128 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
2129 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
2130 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
2131 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
2133 btrfs_mark_buffer_dirty(leaf
);
2134 btrfs_release_path(path
);
2136 inode_add_bytes(inode
, num_bytes
);
2138 ins
.objectid
= disk_bytenr
;
2139 ins
.offset
= disk_num_bytes
;
2140 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2141 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
2142 root
->root_key
.objectid
,
2143 btrfs_ino(inode
), file_pos
,
2146 * Release the reserved range from inode dirty range map, as it is
2147 * already moved into delayed_ref_head
2149 btrfs_qgroup_release_data(inode
, file_pos
, ram_bytes
);
2151 btrfs_free_path(path
);
2156 /* snapshot-aware defrag */
2157 struct sa_defrag_extent_backref
{
2158 struct rb_node node
;
2159 struct old_sa_defrag_extent
*old
;
2168 struct old_sa_defrag_extent
{
2169 struct list_head list
;
2170 struct new_sa_defrag_extent
*new;
2179 struct new_sa_defrag_extent
{
2180 struct rb_root root
;
2181 struct list_head head
;
2182 struct btrfs_path
*path
;
2183 struct inode
*inode
;
2191 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
2192 struct sa_defrag_extent_backref
*b2
)
2194 if (b1
->root_id
< b2
->root_id
)
2196 else if (b1
->root_id
> b2
->root_id
)
2199 if (b1
->inum
< b2
->inum
)
2201 else if (b1
->inum
> b2
->inum
)
2204 if (b1
->file_pos
< b2
->file_pos
)
2206 else if (b1
->file_pos
> b2
->file_pos
)
2210 * [------------------------------] ===> (a range of space)
2211 * |<--->| |<---->| =============> (fs/file tree A)
2212 * |<---------------------------->| ===> (fs/file tree B)
2214 * A range of space can refer to two file extents in one tree while
2215 * refer to only one file extent in another tree.
2217 * So we may process a disk offset more than one time(two extents in A)
2218 * and locate at the same extent(one extent in B), then insert two same
2219 * backrefs(both refer to the extent in B).
2224 static void backref_insert(struct rb_root
*root
,
2225 struct sa_defrag_extent_backref
*backref
)
2227 struct rb_node
**p
= &root
->rb_node
;
2228 struct rb_node
*parent
= NULL
;
2229 struct sa_defrag_extent_backref
*entry
;
2234 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2236 ret
= backref_comp(backref
, entry
);
2240 p
= &(*p
)->rb_right
;
2243 rb_link_node(&backref
->node
, parent
, p
);
2244 rb_insert_color(&backref
->node
, root
);
2248 * Note the backref might has changed, and in this case we just return 0.
2250 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2253 struct btrfs_file_extent_item
*extent
;
2254 struct btrfs_fs_info
*fs_info
;
2255 struct old_sa_defrag_extent
*old
= ctx
;
2256 struct new_sa_defrag_extent
*new = old
->new;
2257 struct btrfs_path
*path
= new->path
;
2258 struct btrfs_key key
;
2259 struct btrfs_root
*root
;
2260 struct sa_defrag_extent_backref
*backref
;
2261 struct extent_buffer
*leaf
;
2262 struct inode
*inode
= new->inode
;
2268 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2269 inum
== btrfs_ino(inode
))
2272 key
.objectid
= root_id
;
2273 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2274 key
.offset
= (u64
)-1;
2276 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2277 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2279 if (PTR_ERR(root
) == -ENOENT
)
2282 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2283 inum
, offset
, root_id
);
2284 return PTR_ERR(root
);
2287 key
.objectid
= inum
;
2288 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2289 if (offset
> (u64
)-1 << 32)
2292 key
.offset
= offset
;
2294 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2295 if (WARN_ON(ret
< 0))
2302 leaf
= path
->nodes
[0];
2303 slot
= path
->slots
[0];
2305 if (slot
>= btrfs_header_nritems(leaf
)) {
2306 ret
= btrfs_next_leaf(root
, path
);
2309 } else if (ret
> 0) {
2318 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2320 if (key
.objectid
> inum
)
2323 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2326 extent
= btrfs_item_ptr(leaf
, slot
,
2327 struct btrfs_file_extent_item
);
2329 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2333 * 'offset' refers to the exact key.offset,
2334 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2335 * (key.offset - extent_offset).
2337 if (key
.offset
!= offset
)
2340 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2341 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2343 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2344 old
->len
|| extent_offset
+ num_bytes
<=
2345 old
->extent_offset
+ old
->offset
)
2350 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2356 backref
->root_id
= root_id
;
2357 backref
->inum
= inum
;
2358 backref
->file_pos
= offset
;
2359 backref
->num_bytes
= num_bytes
;
2360 backref
->extent_offset
= extent_offset
;
2361 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2363 backref_insert(&new->root
, backref
);
2366 btrfs_release_path(path
);
2371 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2372 struct new_sa_defrag_extent
*new)
2374 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2375 struct old_sa_defrag_extent
*old
, *tmp
;
2380 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2381 ret
= iterate_inodes_from_logical(old
->bytenr
+
2382 old
->extent_offset
, fs_info
,
2383 path
, record_one_backref
,
2385 if (ret
< 0 && ret
!= -ENOENT
)
2388 /* no backref to be processed for this extent */
2390 list_del(&old
->list
);
2395 if (list_empty(&new->head
))
2401 static int relink_is_mergable(struct extent_buffer
*leaf
,
2402 struct btrfs_file_extent_item
*fi
,
2403 struct new_sa_defrag_extent
*new)
2405 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != new->bytenr
)
2408 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2411 if (btrfs_file_extent_compression(leaf
, fi
) != new->compress_type
)
2414 if (btrfs_file_extent_encryption(leaf
, fi
) ||
2415 btrfs_file_extent_other_encoding(leaf
, fi
))
2422 * Note the backref might has changed, and in this case we just return 0.
2424 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2425 struct sa_defrag_extent_backref
*prev
,
2426 struct sa_defrag_extent_backref
*backref
)
2428 struct btrfs_file_extent_item
*extent
;
2429 struct btrfs_file_extent_item
*item
;
2430 struct btrfs_ordered_extent
*ordered
;
2431 struct btrfs_trans_handle
*trans
;
2432 struct btrfs_fs_info
*fs_info
;
2433 struct btrfs_root
*root
;
2434 struct btrfs_key key
;
2435 struct extent_buffer
*leaf
;
2436 struct old_sa_defrag_extent
*old
= backref
->old
;
2437 struct new_sa_defrag_extent
*new = old
->new;
2438 struct inode
*src_inode
= new->inode
;
2439 struct inode
*inode
;
2440 struct extent_state
*cached
= NULL
;
2449 if (prev
&& prev
->root_id
== backref
->root_id
&&
2450 prev
->inum
== backref
->inum
&&
2451 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2454 /* step 1: get root */
2455 key
.objectid
= backref
->root_id
;
2456 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2457 key
.offset
= (u64
)-1;
2459 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2460 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2462 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2464 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2465 if (PTR_ERR(root
) == -ENOENT
)
2467 return PTR_ERR(root
);
2470 if (btrfs_root_readonly(root
)) {
2471 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2475 /* step 2: get inode */
2476 key
.objectid
= backref
->inum
;
2477 key
.type
= BTRFS_INODE_ITEM_KEY
;
2480 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2481 if (IS_ERR(inode
)) {
2482 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2486 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2488 /* step 3: relink backref */
2489 lock_start
= backref
->file_pos
;
2490 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2491 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2494 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2496 btrfs_put_ordered_extent(ordered
);
2500 trans
= btrfs_join_transaction(root
);
2501 if (IS_ERR(trans
)) {
2502 ret
= PTR_ERR(trans
);
2506 key
.objectid
= backref
->inum
;
2507 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2508 key
.offset
= backref
->file_pos
;
2510 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2513 } else if (ret
> 0) {
2518 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2519 struct btrfs_file_extent_item
);
2521 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2522 backref
->generation
)
2525 btrfs_release_path(path
);
2527 start
= backref
->file_pos
;
2528 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2529 start
+= old
->extent_offset
+ old
->offset
-
2530 backref
->extent_offset
;
2532 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2533 old
->extent_offset
+ old
->offset
+ old
->len
);
2534 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2536 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2541 key
.objectid
= btrfs_ino(inode
);
2542 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2545 path
->leave_spinning
= 1;
2547 struct btrfs_file_extent_item
*fi
;
2549 struct btrfs_key found_key
;
2551 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2556 leaf
= path
->nodes
[0];
2557 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2559 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2560 struct btrfs_file_extent_item
);
2561 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2563 if (extent_len
+ found_key
.offset
== start
&&
2564 relink_is_mergable(leaf
, fi
, new)) {
2565 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2567 btrfs_mark_buffer_dirty(leaf
);
2568 inode_add_bytes(inode
, len
);
2574 btrfs_release_path(path
);
2579 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2582 btrfs_abort_transaction(trans
, root
, ret
);
2586 leaf
= path
->nodes
[0];
2587 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2588 struct btrfs_file_extent_item
);
2589 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2590 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2591 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2592 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2593 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2594 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2595 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2596 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2597 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2598 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2600 btrfs_mark_buffer_dirty(leaf
);
2601 inode_add_bytes(inode
, len
);
2602 btrfs_release_path(path
);
2604 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2606 backref
->root_id
, backref
->inum
,
2607 new->file_pos
); /* start - extent_offset */
2609 btrfs_abort_transaction(trans
, root
, ret
);
2615 btrfs_release_path(path
);
2616 path
->leave_spinning
= 0;
2617 btrfs_end_transaction(trans
, root
);
2619 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2625 static void free_sa_defrag_extent(struct new_sa_defrag_extent
*new)
2627 struct old_sa_defrag_extent
*old
, *tmp
;
2632 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2638 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2640 struct btrfs_path
*path
;
2641 struct sa_defrag_extent_backref
*backref
;
2642 struct sa_defrag_extent_backref
*prev
= NULL
;
2643 struct inode
*inode
;
2644 struct btrfs_root
*root
;
2645 struct rb_node
*node
;
2649 root
= BTRFS_I(inode
)->root
;
2651 path
= btrfs_alloc_path();
2655 if (!record_extent_backrefs(path
, new)) {
2656 btrfs_free_path(path
);
2659 btrfs_release_path(path
);
2662 node
= rb_first(&new->root
);
2665 rb_erase(node
, &new->root
);
2667 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2669 ret
= relink_extent_backref(path
, prev
, backref
);
2682 btrfs_free_path(path
);
2684 free_sa_defrag_extent(new);
2686 atomic_dec(&root
->fs_info
->defrag_running
);
2687 wake_up(&root
->fs_info
->transaction_wait
);
2690 static struct new_sa_defrag_extent
*
2691 record_old_file_extents(struct inode
*inode
,
2692 struct btrfs_ordered_extent
*ordered
)
2694 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2695 struct btrfs_path
*path
;
2696 struct btrfs_key key
;
2697 struct old_sa_defrag_extent
*old
;
2698 struct new_sa_defrag_extent
*new;
2701 new = kmalloc(sizeof(*new), GFP_NOFS
);
2706 new->file_pos
= ordered
->file_offset
;
2707 new->len
= ordered
->len
;
2708 new->bytenr
= ordered
->start
;
2709 new->disk_len
= ordered
->disk_len
;
2710 new->compress_type
= ordered
->compress_type
;
2711 new->root
= RB_ROOT
;
2712 INIT_LIST_HEAD(&new->head
);
2714 path
= btrfs_alloc_path();
2718 key
.objectid
= btrfs_ino(inode
);
2719 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2720 key
.offset
= new->file_pos
;
2722 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2725 if (ret
> 0 && path
->slots
[0] > 0)
2728 /* find out all the old extents for the file range */
2730 struct btrfs_file_extent_item
*extent
;
2731 struct extent_buffer
*l
;
2740 slot
= path
->slots
[0];
2742 if (slot
>= btrfs_header_nritems(l
)) {
2743 ret
= btrfs_next_leaf(root
, path
);
2751 btrfs_item_key_to_cpu(l
, &key
, slot
);
2753 if (key
.objectid
!= btrfs_ino(inode
))
2755 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2757 if (key
.offset
>= new->file_pos
+ new->len
)
2760 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2762 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2763 if (key
.offset
+ num_bytes
< new->file_pos
)
2766 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2770 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2772 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2776 offset
= max(new->file_pos
, key
.offset
);
2777 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2779 old
->bytenr
= disk_bytenr
;
2780 old
->extent_offset
= extent_offset
;
2781 old
->offset
= offset
- key
.offset
;
2782 old
->len
= end
- offset
;
2785 list_add_tail(&old
->list
, &new->head
);
2791 btrfs_free_path(path
);
2792 atomic_inc(&root
->fs_info
->defrag_running
);
2797 btrfs_free_path(path
);
2799 free_sa_defrag_extent(new);
2803 static void btrfs_release_delalloc_bytes(struct btrfs_root
*root
,
2806 struct btrfs_block_group_cache
*cache
;
2808 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
2811 spin_lock(&cache
->lock
);
2812 cache
->delalloc_bytes
-= len
;
2813 spin_unlock(&cache
->lock
);
2815 btrfs_put_block_group(cache
);
2818 /* as ordered data IO finishes, this gets called so we can finish
2819 * an ordered extent if the range of bytes in the file it covers are
2822 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2824 struct inode
*inode
= ordered_extent
->inode
;
2825 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2826 struct btrfs_trans_handle
*trans
= NULL
;
2827 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2828 struct extent_state
*cached_state
= NULL
;
2829 struct new_sa_defrag_extent
*new = NULL
;
2830 int compress_type
= 0;
2832 u64 logical_len
= ordered_extent
->len
;
2834 bool truncated
= false;
2836 nolock
= btrfs_is_free_space_inode(inode
);
2838 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2843 btrfs_free_io_failure_record(inode
, ordered_extent
->file_offset
,
2844 ordered_extent
->file_offset
+
2845 ordered_extent
->len
- 1);
2847 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2849 logical_len
= ordered_extent
->truncated_len
;
2850 /* Truncated the entire extent, don't bother adding */
2855 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2856 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2859 * For mwrite(mmap + memset to write) case, we still reserve
2860 * space for NOCOW range.
2861 * As NOCOW won't cause a new delayed ref, just free the space
2863 btrfs_qgroup_free_data(inode
, ordered_extent
->file_offset
,
2864 ordered_extent
->len
);
2865 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2867 trans
= btrfs_join_transaction_nolock(root
);
2869 trans
= btrfs_join_transaction(root
);
2870 if (IS_ERR(trans
)) {
2871 ret
= PTR_ERR(trans
);
2875 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2876 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2877 if (ret
) /* -ENOMEM or corruption */
2878 btrfs_abort_transaction(trans
, root
, ret
);
2882 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2883 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2886 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2887 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2888 EXTENT_DEFRAG
, 1, cached_state
);
2890 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2891 if (0 && last_snapshot
>= BTRFS_I(inode
)->generation
)
2892 /* the inode is shared */
2893 new = record_old_file_extents(inode
, ordered_extent
);
2895 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2896 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2897 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2901 trans
= btrfs_join_transaction_nolock(root
);
2903 trans
= btrfs_join_transaction(root
);
2904 if (IS_ERR(trans
)) {
2905 ret
= PTR_ERR(trans
);
2910 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2912 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2913 compress_type
= ordered_extent
->compress_type
;
2914 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2915 BUG_ON(compress_type
);
2916 ret
= btrfs_mark_extent_written(trans
, inode
,
2917 ordered_extent
->file_offset
,
2918 ordered_extent
->file_offset
+
2921 BUG_ON(root
== root
->fs_info
->tree_root
);
2922 ret
= insert_reserved_file_extent(trans
, inode
,
2923 ordered_extent
->file_offset
,
2924 ordered_extent
->start
,
2925 ordered_extent
->disk_len
,
2926 logical_len
, logical_len
,
2927 compress_type
, 0, 0,
2928 BTRFS_FILE_EXTENT_REG
);
2930 btrfs_release_delalloc_bytes(root
,
2931 ordered_extent
->start
,
2932 ordered_extent
->disk_len
);
2934 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2935 ordered_extent
->file_offset
, ordered_extent
->len
,
2938 btrfs_abort_transaction(trans
, root
, ret
);
2942 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2943 &ordered_extent
->list
);
2945 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2946 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2947 if (ret
) { /* -ENOMEM or corruption */
2948 btrfs_abort_transaction(trans
, root
, ret
);
2953 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2954 ordered_extent
->file_offset
+
2955 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2957 if (root
!= root
->fs_info
->tree_root
)
2958 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2960 btrfs_end_transaction(trans
, root
);
2962 if (ret
|| truncated
) {
2966 start
= ordered_extent
->file_offset
+ logical_len
;
2968 start
= ordered_extent
->file_offset
;
2969 end
= ordered_extent
->file_offset
+ ordered_extent
->len
- 1;
2970 clear_extent_uptodate(io_tree
, start
, end
, NULL
, GFP_NOFS
);
2972 /* Drop the cache for the part of the extent we didn't write. */
2973 btrfs_drop_extent_cache(inode
, start
, end
, 0);
2976 * If the ordered extent had an IOERR or something else went
2977 * wrong we need to return the space for this ordered extent
2978 * back to the allocator. We only free the extent in the
2979 * truncated case if we didn't write out the extent at all.
2981 if ((ret
|| !logical_len
) &&
2982 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2983 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2984 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2985 ordered_extent
->disk_len
, 1);
2990 * This needs to be done to make sure anybody waiting knows we are done
2991 * updating everything for this ordered extent.
2993 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2995 /* for snapshot-aware defrag */
2998 free_sa_defrag_extent(new);
2999 atomic_dec(&root
->fs_info
->defrag_running
);
3001 relink_file_extents(new);
3006 btrfs_put_ordered_extent(ordered_extent
);
3007 /* once for the tree */
3008 btrfs_put_ordered_extent(ordered_extent
);
3013 static void finish_ordered_fn(struct btrfs_work
*work
)
3015 struct btrfs_ordered_extent
*ordered_extent
;
3016 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
3017 btrfs_finish_ordered_io(ordered_extent
);
3020 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
3021 struct extent_state
*state
, int uptodate
)
3023 struct inode
*inode
= page
->mapping
->host
;
3024 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3025 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
3026 struct btrfs_workqueue
*wq
;
3027 btrfs_work_func_t func
;
3029 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
3031 ClearPagePrivate2(page
);
3032 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
3033 end
- start
+ 1, uptodate
))
3036 if (btrfs_is_free_space_inode(inode
)) {
3037 wq
= root
->fs_info
->endio_freespace_worker
;
3038 func
= btrfs_freespace_write_helper
;
3040 wq
= root
->fs_info
->endio_write_workers
;
3041 func
= btrfs_endio_write_helper
;
3044 btrfs_init_work(&ordered_extent
->work
, func
, finish_ordered_fn
, NULL
,
3046 btrfs_queue_work(wq
, &ordered_extent
->work
);
3051 static int __readpage_endio_check(struct inode
*inode
,
3052 struct btrfs_io_bio
*io_bio
,
3053 int icsum
, struct page
*page
,
3054 int pgoff
, u64 start
, size_t len
)
3060 csum_expected
= *(((u32
*)io_bio
->csum
) + icsum
);
3062 kaddr
= kmap_atomic(page
);
3063 csum
= btrfs_csum_data(kaddr
+ pgoff
, csum
, len
);
3064 btrfs_csum_final(csum
, (char *)&csum
);
3065 if (csum
!= csum_expected
)
3068 kunmap_atomic(kaddr
);
3071 btrfs_warn_rl(BTRFS_I(inode
)->root
->fs_info
,
3072 "csum failed ino %llu off %llu csum %u expected csum %u",
3073 btrfs_ino(inode
), start
, csum
, csum_expected
);
3074 memset(kaddr
+ pgoff
, 1, len
);
3075 flush_dcache_page(page
);
3076 kunmap_atomic(kaddr
);
3077 if (csum_expected
== 0)
3083 * when reads are done, we need to check csums to verify the data is correct
3084 * if there's a match, we allow the bio to finish. If not, the code in
3085 * extent_io.c will try to find good copies for us.
3087 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
3088 u64 phy_offset
, struct page
*page
,
3089 u64 start
, u64 end
, int mirror
)
3091 size_t offset
= start
- page_offset(page
);
3092 struct inode
*inode
= page
->mapping
->host
;
3093 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3094 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3096 if (PageChecked(page
)) {
3097 ClearPageChecked(page
);
3101 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
3104 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
3105 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
3106 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
3111 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
3112 return __readpage_endio_check(inode
, io_bio
, phy_offset
, page
, offset
,
3113 start
, (size_t)(end
- start
+ 1));
3116 struct delayed_iput
{
3117 struct list_head list
;
3118 struct inode
*inode
;
3121 /* JDM: If this is fs-wide, why can't we add a pointer to
3122 * btrfs_inode instead and avoid the allocation? */
3123 void btrfs_add_delayed_iput(struct inode
*inode
)
3125 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
3126 struct delayed_iput
*delayed
;
3128 if (atomic_add_unless(&inode
->i_count
, -1, 1))
3131 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
3132 delayed
->inode
= inode
;
3134 spin_lock(&fs_info
->delayed_iput_lock
);
3135 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
3136 spin_unlock(&fs_info
->delayed_iput_lock
);
3139 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
3142 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3143 struct delayed_iput
*delayed
;
3146 spin_lock(&fs_info
->delayed_iput_lock
);
3147 empty
= list_empty(&fs_info
->delayed_iputs
);
3148 spin_unlock(&fs_info
->delayed_iput_lock
);
3152 down_read(&fs_info
->delayed_iput_sem
);
3154 spin_lock(&fs_info
->delayed_iput_lock
);
3155 list_splice_init(&fs_info
->delayed_iputs
, &list
);
3156 spin_unlock(&fs_info
->delayed_iput_lock
);
3158 while (!list_empty(&list
)) {
3159 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
3160 list_del(&delayed
->list
);
3161 iput(delayed
->inode
);
3165 up_read(&root
->fs_info
->delayed_iput_sem
);
3169 * This is called in transaction commit time. If there are no orphan
3170 * files in the subvolume, it removes orphan item and frees block_rsv
3173 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
3174 struct btrfs_root
*root
)
3176 struct btrfs_block_rsv
*block_rsv
;
3179 if (atomic_read(&root
->orphan_inodes
) ||
3180 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
3183 spin_lock(&root
->orphan_lock
);
3184 if (atomic_read(&root
->orphan_inodes
)) {
3185 spin_unlock(&root
->orphan_lock
);
3189 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
3190 spin_unlock(&root
->orphan_lock
);
3194 block_rsv
= root
->orphan_block_rsv
;
3195 root
->orphan_block_rsv
= NULL
;
3196 spin_unlock(&root
->orphan_lock
);
3198 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
) &&
3199 btrfs_root_refs(&root
->root_item
) > 0) {
3200 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
3201 root
->root_key
.objectid
);
3203 btrfs_abort_transaction(trans
, root
, ret
);
3205 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
,
3210 WARN_ON(block_rsv
->size
> 0);
3211 btrfs_free_block_rsv(root
, block_rsv
);
3216 * This creates an orphan entry for the given inode in case something goes
3217 * wrong in the middle of an unlink/truncate.
3219 * NOTE: caller of this function should reserve 5 units of metadata for
3222 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
3224 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3225 struct btrfs_block_rsv
*block_rsv
= NULL
;
3230 if (!root
->orphan_block_rsv
) {
3231 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
3236 spin_lock(&root
->orphan_lock
);
3237 if (!root
->orphan_block_rsv
) {
3238 root
->orphan_block_rsv
= block_rsv
;
3239 } else if (block_rsv
) {
3240 btrfs_free_block_rsv(root
, block_rsv
);
3244 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3245 &BTRFS_I(inode
)->runtime_flags
)) {
3248 * For proper ENOSPC handling, we should do orphan
3249 * cleanup when mounting. But this introduces backward
3250 * compatibility issue.
3252 if (!xchg(&root
->orphan_item_inserted
, 1))
3258 atomic_inc(&root
->orphan_inodes
);
3261 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3262 &BTRFS_I(inode
)->runtime_flags
))
3264 spin_unlock(&root
->orphan_lock
);
3266 /* grab metadata reservation from transaction handle */
3268 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
3269 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
3272 /* insert an orphan item to track this unlinked/truncated file */
3274 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
3276 atomic_dec(&root
->orphan_inodes
);
3278 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3279 &BTRFS_I(inode
)->runtime_flags
);
3280 btrfs_orphan_release_metadata(inode
);
3282 if (ret
!= -EEXIST
) {
3283 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3284 &BTRFS_I(inode
)->runtime_flags
);
3285 btrfs_abort_transaction(trans
, root
, ret
);
3292 /* insert an orphan item to track subvolume contains orphan files */
3294 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
3295 root
->root_key
.objectid
);
3296 if (ret
&& ret
!= -EEXIST
) {
3297 btrfs_abort_transaction(trans
, root
, ret
);
3305 * We have done the truncate/delete so we can go ahead and remove the orphan
3306 * item for this particular inode.
3308 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3309 struct inode
*inode
)
3311 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3312 int delete_item
= 0;
3313 int release_rsv
= 0;
3316 spin_lock(&root
->orphan_lock
);
3317 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3318 &BTRFS_I(inode
)->runtime_flags
))
3321 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3322 &BTRFS_I(inode
)->runtime_flags
))
3324 spin_unlock(&root
->orphan_lock
);
3327 atomic_dec(&root
->orphan_inodes
);
3329 ret
= btrfs_del_orphan_item(trans
, root
,
3334 btrfs_orphan_release_metadata(inode
);
3340 * this cleans up any orphans that may be left on the list from the last use
3343 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3345 struct btrfs_path
*path
;
3346 struct extent_buffer
*leaf
;
3347 struct btrfs_key key
, found_key
;
3348 struct btrfs_trans_handle
*trans
;
3349 struct inode
*inode
;
3350 u64 last_objectid
= 0;
3351 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3353 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3356 path
= btrfs_alloc_path();
3363 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3364 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
3365 key
.offset
= (u64
)-1;
3368 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3373 * if ret == 0 means we found what we were searching for, which
3374 * is weird, but possible, so only screw with path if we didn't
3375 * find the key and see if we have stuff that matches
3379 if (path
->slots
[0] == 0)
3384 /* pull out the item */
3385 leaf
= path
->nodes
[0];
3386 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3388 /* make sure the item matches what we want */
3389 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3391 if (found_key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
3394 /* release the path since we're done with it */
3395 btrfs_release_path(path
);
3398 * this is where we are basically btrfs_lookup, without the
3399 * crossing root thing. we store the inode number in the
3400 * offset of the orphan item.
3403 if (found_key
.offset
== last_objectid
) {
3404 btrfs_err(root
->fs_info
,
3405 "Error removing orphan entry, stopping orphan cleanup");
3410 last_objectid
= found_key
.offset
;
3412 found_key
.objectid
= found_key
.offset
;
3413 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3414 found_key
.offset
= 0;
3415 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3416 ret
= PTR_ERR_OR_ZERO(inode
);
3417 if (ret
&& ret
!= -ESTALE
)
3420 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3421 struct btrfs_root
*dead_root
;
3422 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3423 int is_dead_root
= 0;
3426 * this is an orphan in the tree root. Currently these
3427 * could come from 2 sources:
3428 * a) a snapshot deletion in progress
3429 * b) a free space cache inode
3430 * We need to distinguish those two, as the snapshot
3431 * orphan must not get deleted.
3432 * find_dead_roots already ran before us, so if this
3433 * is a snapshot deletion, we should find the root
3434 * in the dead_roots list
3436 spin_lock(&fs_info
->trans_lock
);
3437 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3439 if (dead_root
->root_key
.objectid
==
3440 found_key
.objectid
) {
3445 spin_unlock(&fs_info
->trans_lock
);
3447 /* prevent this orphan from being found again */
3448 key
.offset
= found_key
.objectid
- 1;
3453 * Inode is already gone but the orphan item is still there,
3454 * kill the orphan item.
3456 if (ret
== -ESTALE
) {
3457 trans
= btrfs_start_transaction(root
, 1);
3458 if (IS_ERR(trans
)) {
3459 ret
= PTR_ERR(trans
);
3462 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3463 found_key
.objectid
);
3464 ret
= btrfs_del_orphan_item(trans
, root
,
3465 found_key
.objectid
);
3466 btrfs_end_transaction(trans
, root
);
3473 * add this inode to the orphan list so btrfs_orphan_del does
3474 * the proper thing when we hit it
3476 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3477 &BTRFS_I(inode
)->runtime_flags
);
3478 atomic_inc(&root
->orphan_inodes
);
3480 /* if we have links, this was a truncate, lets do that */
3481 if (inode
->i_nlink
) {
3482 if (WARN_ON(!S_ISREG(inode
->i_mode
))) {
3488 /* 1 for the orphan item deletion. */
3489 trans
= btrfs_start_transaction(root
, 1);
3490 if (IS_ERR(trans
)) {
3492 ret
= PTR_ERR(trans
);
3495 ret
= btrfs_orphan_add(trans
, inode
);
3496 btrfs_end_transaction(trans
, root
);
3502 ret
= btrfs_truncate(inode
);
3504 btrfs_orphan_del(NULL
, inode
);
3509 /* this will do delete_inode and everything for us */
3514 /* release the path since we're done with it */
3515 btrfs_release_path(path
);
3517 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3519 if (root
->orphan_block_rsv
)
3520 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3523 if (root
->orphan_block_rsv
||
3524 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
)) {
3525 trans
= btrfs_join_transaction(root
);
3527 btrfs_end_transaction(trans
, root
);
3531 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3533 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3537 btrfs_err(root
->fs_info
,
3538 "could not do orphan cleanup %d", ret
);
3539 btrfs_free_path(path
);
3544 * very simple check to peek ahead in the leaf looking for xattrs. If we
3545 * don't find any xattrs, we know there can't be any acls.
3547 * slot is the slot the inode is in, objectid is the objectid of the inode
3549 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3550 int slot
, u64 objectid
,
3551 int *first_xattr_slot
)
3553 u32 nritems
= btrfs_header_nritems(leaf
);
3554 struct btrfs_key found_key
;
3555 static u64 xattr_access
= 0;
3556 static u64 xattr_default
= 0;
3559 if (!xattr_access
) {
3560 xattr_access
= btrfs_name_hash(POSIX_ACL_XATTR_ACCESS
,
3561 strlen(POSIX_ACL_XATTR_ACCESS
));
3562 xattr_default
= btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT
,
3563 strlen(POSIX_ACL_XATTR_DEFAULT
));
3567 *first_xattr_slot
= -1;
3568 while (slot
< nritems
) {
3569 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3571 /* we found a different objectid, there must not be acls */
3572 if (found_key
.objectid
!= objectid
)
3575 /* we found an xattr, assume we've got an acl */
3576 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3577 if (*first_xattr_slot
== -1)
3578 *first_xattr_slot
= slot
;
3579 if (found_key
.offset
== xattr_access
||
3580 found_key
.offset
== xattr_default
)
3585 * we found a key greater than an xattr key, there can't
3586 * be any acls later on
3588 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3595 * it goes inode, inode backrefs, xattrs, extents,
3596 * so if there are a ton of hard links to an inode there can
3597 * be a lot of backrefs. Don't waste time searching too hard,
3598 * this is just an optimization
3603 /* we hit the end of the leaf before we found an xattr or
3604 * something larger than an xattr. We have to assume the inode
3607 if (*first_xattr_slot
== -1)
3608 *first_xattr_slot
= slot
;
3613 * read an inode from the btree into the in-memory inode
3615 static void btrfs_read_locked_inode(struct inode
*inode
)
3617 struct btrfs_path
*path
;
3618 struct extent_buffer
*leaf
;
3619 struct btrfs_inode_item
*inode_item
;
3620 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3621 struct btrfs_key location
;
3626 bool filled
= false;
3627 int first_xattr_slot
;
3629 ret
= btrfs_fill_inode(inode
, &rdev
);
3633 path
= btrfs_alloc_path();
3637 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3639 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3643 leaf
= path
->nodes
[0];
3648 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3649 struct btrfs_inode_item
);
3650 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3651 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3652 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3653 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3654 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3656 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->atime
);
3657 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->atime
);
3659 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->mtime
);
3660 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->mtime
);
3662 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->ctime
);
3663 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->ctime
);
3665 BTRFS_I(inode
)->i_otime
.tv_sec
=
3666 btrfs_timespec_sec(leaf
, &inode_item
->otime
);
3667 BTRFS_I(inode
)->i_otime
.tv_nsec
=
3668 btrfs_timespec_nsec(leaf
, &inode_item
->otime
);
3670 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3671 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3672 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3674 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3675 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3677 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3679 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3680 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3684 * If we were modified in the current generation and evicted from memory
3685 * and then re-read we need to do a full sync since we don't have any
3686 * idea about which extents were modified before we were evicted from
3689 * This is required for both inode re-read from disk and delayed inode
3690 * in delayed_nodes_tree.
3692 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3693 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3694 &BTRFS_I(inode
)->runtime_flags
);
3697 * We don't persist the id of the transaction where an unlink operation
3698 * against the inode was last made. So here we assume the inode might
3699 * have been evicted, and therefore the exact value of last_unlink_trans
3700 * lost, and set it to last_trans to avoid metadata inconsistencies
3701 * between the inode and its parent if the inode is fsync'ed and the log
3702 * replayed. For example, in the scenario:
3705 * ln mydir/foo mydir/bar
3708 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3709 * xfs_io -c fsync mydir/foo
3711 * mount fs, triggers fsync log replay
3713 * We must make sure that when we fsync our inode foo we also log its
3714 * parent inode, otherwise after log replay the parent still has the
3715 * dentry with the "bar" name but our inode foo has a link count of 1
3716 * and doesn't have an inode ref with the name "bar" anymore.
3718 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3719 * but it guarantees correctness at the expense of ocassional full
3720 * transaction commits on fsync if our inode is a directory, or if our
3721 * inode is not a directory, logging its parent unnecessarily.
3723 BTRFS_I(inode
)->last_unlink_trans
= BTRFS_I(inode
)->last_trans
;
3726 if (inode
->i_nlink
!= 1 ||
3727 path
->slots
[0] >= btrfs_header_nritems(leaf
))
3730 btrfs_item_key_to_cpu(leaf
, &location
, path
->slots
[0]);
3731 if (location
.objectid
!= btrfs_ino(inode
))
3734 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3735 if (location
.type
== BTRFS_INODE_REF_KEY
) {
3736 struct btrfs_inode_ref
*ref
;
3738 ref
= (struct btrfs_inode_ref
*)ptr
;
3739 BTRFS_I(inode
)->dir_index
= btrfs_inode_ref_index(leaf
, ref
);
3740 } else if (location
.type
== BTRFS_INODE_EXTREF_KEY
) {
3741 struct btrfs_inode_extref
*extref
;
3743 extref
= (struct btrfs_inode_extref
*)ptr
;
3744 BTRFS_I(inode
)->dir_index
= btrfs_inode_extref_index(leaf
,
3749 * try to precache a NULL acl entry for files that don't have
3750 * any xattrs or acls
3752 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3753 btrfs_ino(inode
), &first_xattr_slot
);
3754 if (first_xattr_slot
!= -1) {
3755 path
->slots
[0] = first_xattr_slot
;
3756 ret
= btrfs_load_inode_props(inode
, path
);
3758 btrfs_err(root
->fs_info
,
3759 "error loading props for ino %llu (root %llu): %d",
3761 root
->root_key
.objectid
, ret
);
3763 btrfs_free_path(path
);
3766 cache_no_acl(inode
);
3768 switch (inode
->i_mode
& S_IFMT
) {
3770 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3771 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3772 inode
->i_fop
= &btrfs_file_operations
;
3773 inode
->i_op
= &btrfs_file_inode_operations
;
3776 inode
->i_fop
= &btrfs_dir_file_operations
;
3777 if (root
== root
->fs_info
->tree_root
)
3778 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3780 inode
->i_op
= &btrfs_dir_inode_operations
;
3783 inode
->i_op
= &btrfs_symlink_inode_operations
;
3784 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3787 inode
->i_op
= &btrfs_special_inode_operations
;
3788 init_special_inode(inode
, inode
->i_mode
, rdev
);
3792 btrfs_update_iflags(inode
);
3796 btrfs_free_path(path
);
3797 make_bad_inode(inode
);
3801 * given a leaf and an inode, copy the inode fields into the leaf
3803 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3804 struct extent_buffer
*leaf
,
3805 struct btrfs_inode_item
*item
,
3806 struct inode
*inode
)
3808 struct btrfs_map_token token
;
3810 btrfs_init_map_token(&token
);
3812 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3813 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3814 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3816 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3817 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3819 btrfs_set_token_timespec_sec(leaf
, &item
->atime
,
3820 inode
->i_atime
.tv_sec
, &token
);
3821 btrfs_set_token_timespec_nsec(leaf
, &item
->atime
,
3822 inode
->i_atime
.tv_nsec
, &token
);
3824 btrfs_set_token_timespec_sec(leaf
, &item
->mtime
,
3825 inode
->i_mtime
.tv_sec
, &token
);
3826 btrfs_set_token_timespec_nsec(leaf
, &item
->mtime
,
3827 inode
->i_mtime
.tv_nsec
, &token
);
3829 btrfs_set_token_timespec_sec(leaf
, &item
->ctime
,
3830 inode
->i_ctime
.tv_sec
, &token
);
3831 btrfs_set_token_timespec_nsec(leaf
, &item
->ctime
,
3832 inode
->i_ctime
.tv_nsec
, &token
);
3834 btrfs_set_token_timespec_sec(leaf
, &item
->otime
,
3835 BTRFS_I(inode
)->i_otime
.tv_sec
, &token
);
3836 btrfs_set_token_timespec_nsec(leaf
, &item
->otime
,
3837 BTRFS_I(inode
)->i_otime
.tv_nsec
, &token
);
3839 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3841 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3843 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3844 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3845 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3846 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3847 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3851 * copy everything in the in-memory inode into the btree.
3853 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3854 struct btrfs_root
*root
, struct inode
*inode
)
3856 struct btrfs_inode_item
*inode_item
;
3857 struct btrfs_path
*path
;
3858 struct extent_buffer
*leaf
;
3861 path
= btrfs_alloc_path();
3865 path
->leave_spinning
= 1;
3866 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3874 leaf
= path
->nodes
[0];
3875 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3876 struct btrfs_inode_item
);
3878 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3879 btrfs_mark_buffer_dirty(leaf
);
3880 btrfs_set_inode_last_trans(trans
, inode
);
3883 btrfs_free_path(path
);
3888 * copy everything in the in-memory inode into the btree.
3890 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3891 struct btrfs_root
*root
, struct inode
*inode
)
3896 * If the inode is a free space inode, we can deadlock during commit
3897 * if we put it into the delayed code.
3899 * The data relocation inode should also be directly updated
3902 if (!btrfs_is_free_space_inode(inode
)
3903 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
3904 && !root
->fs_info
->log_root_recovering
) {
3905 btrfs_update_root_times(trans
, root
);
3907 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3909 btrfs_set_inode_last_trans(trans
, inode
);
3913 return btrfs_update_inode_item(trans
, root
, inode
);
3916 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3917 struct btrfs_root
*root
,
3918 struct inode
*inode
)
3922 ret
= btrfs_update_inode(trans
, root
, inode
);
3924 return btrfs_update_inode_item(trans
, root
, inode
);
3929 * unlink helper that gets used here in inode.c and in the tree logging
3930 * recovery code. It remove a link in a directory with a given name, and
3931 * also drops the back refs in the inode to the directory
3933 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3934 struct btrfs_root
*root
,
3935 struct inode
*dir
, struct inode
*inode
,
3936 const char *name
, int name_len
)
3938 struct btrfs_path
*path
;
3940 struct extent_buffer
*leaf
;
3941 struct btrfs_dir_item
*di
;
3942 struct btrfs_key key
;
3944 u64 ino
= btrfs_ino(inode
);
3945 u64 dir_ino
= btrfs_ino(dir
);
3947 path
= btrfs_alloc_path();
3953 path
->leave_spinning
= 1;
3954 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3955 name
, name_len
, -1);
3964 leaf
= path
->nodes
[0];
3965 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3966 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3969 btrfs_release_path(path
);
3972 * If we don't have dir index, we have to get it by looking up
3973 * the inode ref, since we get the inode ref, remove it directly,
3974 * it is unnecessary to do delayed deletion.
3976 * But if we have dir index, needn't search inode ref to get it.
3977 * Since the inode ref is close to the inode item, it is better
3978 * that we delay to delete it, and just do this deletion when
3979 * we update the inode item.
3981 if (BTRFS_I(inode
)->dir_index
) {
3982 ret
= btrfs_delayed_delete_inode_ref(inode
);
3984 index
= BTRFS_I(inode
)->dir_index
;
3989 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3992 btrfs_info(root
->fs_info
,
3993 "failed to delete reference to %.*s, inode %llu parent %llu",
3994 name_len
, name
, ino
, dir_ino
);
3995 btrfs_abort_transaction(trans
, root
, ret
);
3999 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
4001 btrfs_abort_transaction(trans
, root
, ret
);
4005 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
4007 if (ret
!= 0 && ret
!= -ENOENT
) {
4008 btrfs_abort_transaction(trans
, root
, ret
);
4012 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
4017 btrfs_abort_transaction(trans
, root
, ret
);
4019 btrfs_free_path(path
);
4023 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
4024 inode_inc_iversion(inode
);
4025 inode_inc_iversion(dir
);
4026 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
4027 ret
= btrfs_update_inode(trans
, root
, dir
);
4032 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
4033 struct btrfs_root
*root
,
4034 struct inode
*dir
, struct inode
*inode
,
4035 const char *name
, int name_len
)
4038 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
4041 ret
= btrfs_update_inode(trans
, root
, inode
);
4047 * helper to start transaction for unlink and rmdir.
4049 * unlink and rmdir are special in btrfs, they do not always free space, so
4050 * if we cannot make our reservations the normal way try and see if there is
4051 * plenty of slack room in the global reserve to migrate, otherwise we cannot
4052 * allow the unlink to occur.
4054 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
4056 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4059 * 1 for the possible orphan item
4060 * 1 for the dir item
4061 * 1 for the dir index
4062 * 1 for the inode ref
4065 return btrfs_start_transaction_fallback_global_rsv(root
, 5, 5);
4068 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
4070 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4071 struct btrfs_trans_handle
*trans
;
4072 struct inode
*inode
= d_inode(dentry
);
4075 trans
= __unlink_start_trans(dir
);
4077 return PTR_ERR(trans
);
4079 btrfs_record_unlink_dir(trans
, dir
, d_inode(dentry
), 0);
4081 ret
= btrfs_unlink_inode(trans
, root
, dir
, d_inode(dentry
),
4082 dentry
->d_name
.name
, dentry
->d_name
.len
);
4086 if (inode
->i_nlink
== 0) {
4087 ret
= btrfs_orphan_add(trans
, inode
);
4093 btrfs_end_transaction(trans
, root
);
4094 btrfs_btree_balance_dirty(root
);
4098 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
4099 struct btrfs_root
*root
,
4100 struct inode
*dir
, u64 objectid
,
4101 const char *name
, int name_len
)
4103 struct btrfs_path
*path
;
4104 struct extent_buffer
*leaf
;
4105 struct btrfs_dir_item
*di
;
4106 struct btrfs_key key
;
4109 u64 dir_ino
= btrfs_ino(dir
);
4111 path
= btrfs_alloc_path();
4115 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
4116 name
, name_len
, -1);
4117 if (IS_ERR_OR_NULL(di
)) {
4125 leaf
= path
->nodes
[0];
4126 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
4127 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
4128 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
4130 btrfs_abort_transaction(trans
, root
, ret
);
4133 btrfs_release_path(path
);
4135 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
4136 objectid
, root
->root_key
.objectid
,
4137 dir_ino
, &index
, name
, name_len
);
4139 if (ret
!= -ENOENT
) {
4140 btrfs_abort_transaction(trans
, root
, ret
);
4143 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
4145 if (IS_ERR_OR_NULL(di
)) {
4150 btrfs_abort_transaction(trans
, root
, ret
);
4154 leaf
= path
->nodes
[0];
4155 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4156 btrfs_release_path(path
);
4159 btrfs_release_path(path
);
4161 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
4163 btrfs_abort_transaction(trans
, root
, ret
);
4167 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
4168 inode_inc_iversion(dir
);
4169 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
4170 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
4172 btrfs_abort_transaction(trans
, root
, ret
);
4174 btrfs_free_path(path
);
4178 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
4180 struct inode
*inode
= d_inode(dentry
);
4182 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4183 struct btrfs_trans_handle
*trans
;
4185 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
4187 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
4190 trans
= __unlink_start_trans(dir
);
4192 return PTR_ERR(trans
);
4194 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
4195 err
= btrfs_unlink_subvol(trans
, root
, dir
,
4196 BTRFS_I(inode
)->location
.objectid
,
4197 dentry
->d_name
.name
,
4198 dentry
->d_name
.len
);
4202 err
= btrfs_orphan_add(trans
, inode
);
4206 /* now the directory is empty */
4207 err
= btrfs_unlink_inode(trans
, root
, dir
, d_inode(dentry
),
4208 dentry
->d_name
.name
, dentry
->d_name
.len
);
4210 btrfs_i_size_write(inode
, 0);
4212 btrfs_end_transaction(trans
, root
);
4213 btrfs_btree_balance_dirty(root
);
4218 static int truncate_space_check(struct btrfs_trans_handle
*trans
,
4219 struct btrfs_root
*root
,
4224 bytes_deleted
= btrfs_csum_bytes_to_leaves(root
, bytes_deleted
);
4225 ret
= btrfs_block_rsv_add(root
, &root
->fs_info
->trans_block_rsv
,
4226 bytes_deleted
, BTRFS_RESERVE_NO_FLUSH
);
4228 trans
->bytes_reserved
+= bytes_deleted
;
4233 static int truncate_inline_extent(struct inode
*inode
,
4234 struct btrfs_path
*path
,
4235 struct btrfs_key
*found_key
,
4239 struct extent_buffer
*leaf
= path
->nodes
[0];
4240 int slot
= path
->slots
[0];
4241 struct btrfs_file_extent_item
*fi
;
4242 u32 size
= (u32
)(new_size
- found_key
->offset
);
4243 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4245 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
4247 if (btrfs_file_extent_compression(leaf
, fi
) != BTRFS_COMPRESS_NONE
) {
4248 loff_t offset
= new_size
;
4249 loff_t page_end
= ALIGN(offset
, PAGE_CACHE_SIZE
);
4252 * Zero out the remaining of the last page of our inline extent,
4253 * instead of directly truncating our inline extent here - that
4254 * would be much more complex (decompressing all the data, then
4255 * compressing the truncated data, which might be bigger than
4256 * the size of the inline extent, resize the extent, etc).
4257 * We release the path because to get the page we might need to
4258 * read the extent item from disk (data not in the page cache).
4260 btrfs_release_path(path
);
4261 return btrfs_truncate_page(inode
, offset
, page_end
- offset
, 0);
4264 btrfs_set_file_extent_ram_bytes(leaf
, fi
, size
);
4265 size
= btrfs_file_extent_calc_inline_size(size
);
4266 btrfs_truncate_item(root
, path
, size
, 1);
4268 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4269 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4275 * this can truncate away extent items, csum items and directory items.
4276 * It starts at a high offset and removes keys until it can't find
4277 * any higher than new_size
4279 * csum items that cross the new i_size are truncated to the new size
4282 * min_type is the minimum key type to truncate down to. If set to 0, this
4283 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4285 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4286 struct btrfs_root
*root
,
4287 struct inode
*inode
,
4288 u64 new_size
, u32 min_type
)
4290 struct btrfs_path
*path
;
4291 struct extent_buffer
*leaf
;
4292 struct btrfs_file_extent_item
*fi
;
4293 struct btrfs_key key
;
4294 struct btrfs_key found_key
;
4295 u64 extent_start
= 0;
4296 u64 extent_num_bytes
= 0;
4297 u64 extent_offset
= 0;
4299 u64 last_size
= new_size
;
4300 u32 found_type
= (u8
)-1;
4303 int pending_del_nr
= 0;
4304 int pending_del_slot
= 0;
4305 int extent_type
= -1;
4308 u64 ino
= btrfs_ino(inode
);
4309 u64 bytes_deleted
= 0;
4311 bool should_throttle
= 0;
4312 bool should_end
= 0;
4314 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4317 * for non-free space inodes and ref cows, we want to back off from
4320 if (!btrfs_is_free_space_inode(inode
) &&
4321 test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4324 path
= btrfs_alloc_path();
4330 * We want to drop from the next block forward in case this new size is
4331 * not block aligned since we will be keeping the last block of the
4332 * extent just the way it is.
4334 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4335 root
== root
->fs_info
->tree_root
)
4336 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
4337 root
->sectorsize
), (u64
)-1, 0);
4340 * This function is also used to drop the items in the log tree before
4341 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4342 * it is used to drop the loged items. So we shouldn't kill the delayed
4345 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4346 btrfs_kill_delayed_inode_items(inode
);
4349 key
.offset
= (u64
)-1;
4354 * with a 16K leaf size and 128MB extents, you can actually queue
4355 * up a huge file in a single leaf. Most of the time that
4356 * bytes_deleted is > 0, it will be huge by the time we get here
4358 if (be_nice
&& bytes_deleted
> 32 * 1024 * 1024) {
4359 if (btrfs_should_end_transaction(trans
, root
)) {
4366 path
->leave_spinning
= 1;
4367 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4374 /* there are no items in the tree for us to truncate, we're
4377 if (path
->slots
[0] == 0)
4384 leaf
= path
->nodes
[0];
4385 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4386 found_type
= found_key
.type
;
4388 if (found_key
.objectid
!= ino
)
4391 if (found_type
< min_type
)
4394 item_end
= found_key
.offset
;
4395 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4396 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4397 struct btrfs_file_extent_item
);
4398 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4399 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4401 btrfs_file_extent_num_bytes(leaf
, fi
);
4402 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4403 item_end
+= btrfs_file_extent_inline_len(leaf
,
4404 path
->slots
[0], fi
);
4408 if (found_type
> min_type
) {
4411 if (item_end
< new_size
)
4413 if (found_key
.offset
>= new_size
)
4419 /* FIXME, shrink the extent if the ref count is only 1 */
4420 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4424 last_size
= found_key
.offset
;
4426 last_size
= new_size
;
4428 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4430 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4432 u64 orig_num_bytes
=
4433 btrfs_file_extent_num_bytes(leaf
, fi
);
4434 extent_num_bytes
= ALIGN(new_size
-
4437 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4439 num_dec
= (orig_num_bytes
-
4441 if (test_bit(BTRFS_ROOT_REF_COWS
,
4444 inode_sub_bytes(inode
, num_dec
);
4445 btrfs_mark_buffer_dirty(leaf
);
4448 btrfs_file_extent_disk_num_bytes(leaf
,
4450 extent_offset
= found_key
.offset
-
4451 btrfs_file_extent_offset(leaf
, fi
);
4453 /* FIXME blocksize != 4096 */
4454 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4455 if (extent_start
!= 0) {
4457 if (test_bit(BTRFS_ROOT_REF_COWS
,
4459 inode_sub_bytes(inode
, num_dec
);
4462 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4464 * we can't truncate inline items that have had
4468 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4469 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4472 * Need to release path in order to truncate a
4473 * compressed extent. So delete any accumulated
4474 * extent items so far.
4476 if (btrfs_file_extent_compression(leaf
, fi
) !=
4477 BTRFS_COMPRESS_NONE
&& pending_del_nr
) {
4478 err
= btrfs_del_items(trans
, root
, path
,
4482 btrfs_abort_transaction(trans
,
4490 err
= truncate_inline_extent(inode
, path
,
4495 btrfs_abort_transaction(trans
,
4499 } else if (test_bit(BTRFS_ROOT_REF_COWS
,
4501 inode_sub_bytes(inode
, item_end
+ 1 - new_size
);
4506 if (!pending_del_nr
) {
4507 /* no pending yet, add ourselves */
4508 pending_del_slot
= path
->slots
[0];
4510 } else if (pending_del_nr
&&
4511 path
->slots
[0] + 1 == pending_del_slot
) {
4512 /* hop on the pending chunk */
4514 pending_del_slot
= path
->slots
[0];
4521 should_throttle
= 0;
4524 (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4525 root
== root
->fs_info
->tree_root
)) {
4526 btrfs_set_path_blocking(path
);
4527 bytes_deleted
+= extent_num_bytes
;
4528 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4529 extent_num_bytes
, 0,
4530 btrfs_header_owner(leaf
),
4531 ino
, extent_offset
);
4533 if (btrfs_should_throttle_delayed_refs(trans
, root
))
4534 btrfs_async_run_delayed_refs(root
,
4535 trans
->delayed_ref_updates
* 2, 0);
4537 if (truncate_space_check(trans
, root
,
4538 extent_num_bytes
)) {
4541 if (btrfs_should_throttle_delayed_refs(trans
,
4543 should_throttle
= 1;
4548 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4551 if (path
->slots
[0] == 0 ||
4552 path
->slots
[0] != pending_del_slot
||
4553 should_throttle
|| should_end
) {
4554 if (pending_del_nr
) {
4555 ret
= btrfs_del_items(trans
, root
, path
,
4559 btrfs_abort_transaction(trans
,
4565 btrfs_release_path(path
);
4566 if (should_throttle
) {
4567 unsigned long updates
= trans
->delayed_ref_updates
;
4569 trans
->delayed_ref_updates
= 0;
4570 ret
= btrfs_run_delayed_refs(trans
, root
, updates
* 2);
4576 * if we failed to refill our space rsv, bail out
4577 * and let the transaction restart
4589 if (pending_del_nr
) {
4590 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4593 btrfs_abort_transaction(trans
, root
, ret
);
4596 if (root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
4597 btrfs_ordered_update_i_size(inode
, last_size
, NULL
);
4599 btrfs_free_path(path
);
4601 if (be_nice
&& bytes_deleted
> 32 * 1024 * 1024) {
4602 unsigned long updates
= trans
->delayed_ref_updates
;
4604 trans
->delayed_ref_updates
= 0;
4605 ret
= btrfs_run_delayed_refs(trans
, root
, updates
* 2);
4614 * btrfs_truncate_page - read, zero a chunk and write a page
4615 * @inode - inode that we're zeroing
4616 * @from - the offset to start zeroing
4617 * @len - the length to zero, 0 to zero the entire range respective to the
4619 * @front - zero up to the offset instead of from the offset on
4621 * This will find the page for the "from" offset and cow the page and zero the
4622 * part we want to zero. This is used with truncate and hole punching.
4624 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4627 struct address_space
*mapping
= inode
->i_mapping
;
4628 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4629 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4630 struct btrfs_ordered_extent
*ordered
;
4631 struct extent_state
*cached_state
= NULL
;
4633 u32 blocksize
= root
->sectorsize
;
4634 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4635 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4637 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4642 if ((offset
& (blocksize
- 1)) == 0 &&
4643 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4645 ret
= btrfs_delalloc_reserve_space(inode
,
4646 round_down(from
, PAGE_CACHE_SIZE
), PAGE_CACHE_SIZE
);
4651 page
= find_or_create_page(mapping
, index
, mask
);
4653 btrfs_delalloc_release_space(inode
,
4654 round_down(from
, PAGE_CACHE_SIZE
),
4660 page_start
= page_offset(page
);
4661 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4663 if (!PageUptodate(page
)) {
4664 ret
= btrfs_readpage(NULL
, page
);
4666 if (page
->mapping
!= mapping
) {
4668 page_cache_release(page
);
4671 if (!PageUptodate(page
)) {
4676 wait_on_page_writeback(page
);
4678 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4679 set_page_extent_mapped(page
);
4681 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4683 unlock_extent_cached(io_tree
, page_start
, page_end
,
4684 &cached_state
, GFP_NOFS
);
4686 page_cache_release(page
);
4687 btrfs_start_ordered_extent(inode
, ordered
, 1);
4688 btrfs_put_ordered_extent(ordered
);
4692 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4693 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4694 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4695 0, 0, &cached_state
, GFP_NOFS
);
4697 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4700 unlock_extent_cached(io_tree
, page_start
, page_end
,
4701 &cached_state
, GFP_NOFS
);
4705 if (offset
!= PAGE_CACHE_SIZE
) {
4707 len
= PAGE_CACHE_SIZE
- offset
;
4710 memset(kaddr
, 0, offset
);
4712 memset(kaddr
+ offset
, 0, len
);
4713 flush_dcache_page(page
);
4716 ClearPageChecked(page
);
4717 set_page_dirty(page
);
4718 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4723 btrfs_delalloc_release_space(inode
, page_start
,
4726 page_cache_release(page
);
4731 static int maybe_insert_hole(struct btrfs_root
*root
, struct inode
*inode
,
4732 u64 offset
, u64 len
)
4734 struct btrfs_trans_handle
*trans
;
4738 * Still need to make sure the inode looks like it's been updated so
4739 * that any holes get logged if we fsync.
4741 if (btrfs_fs_incompat(root
->fs_info
, NO_HOLES
)) {
4742 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
4743 BTRFS_I(inode
)->last_sub_trans
= root
->log_transid
;
4744 BTRFS_I(inode
)->last_log_commit
= root
->last_log_commit
;
4749 * 1 - for the one we're dropping
4750 * 1 - for the one we're adding
4751 * 1 - for updating the inode.
4753 trans
= btrfs_start_transaction(root
, 3);
4755 return PTR_ERR(trans
);
4757 ret
= btrfs_drop_extents(trans
, root
, inode
, offset
, offset
+ len
, 1);
4759 btrfs_abort_transaction(trans
, root
, ret
);
4760 btrfs_end_transaction(trans
, root
);
4764 ret
= btrfs_insert_file_extent(trans
, root
, btrfs_ino(inode
), offset
,
4765 0, 0, len
, 0, len
, 0, 0, 0);
4767 btrfs_abort_transaction(trans
, root
, ret
);
4769 btrfs_update_inode(trans
, root
, inode
);
4770 btrfs_end_transaction(trans
, root
);
4775 * This function puts in dummy file extents for the area we're creating a hole
4776 * for. So if we are truncating this file to a larger size we need to insert
4777 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4778 * the range between oldsize and size
4780 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4782 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4783 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4784 struct extent_map
*em
= NULL
;
4785 struct extent_state
*cached_state
= NULL
;
4786 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4787 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4788 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4795 * If our size started in the middle of a page we need to zero out the
4796 * rest of the page before we expand the i_size, otherwise we could
4797 * expose stale data.
4799 err
= btrfs_truncate_page(inode
, oldsize
, 0, 0);
4803 if (size
<= hole_start
)
4807 struct btrfs_ordered_extent
*ordered
;
4809 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4811 ordered
= btrfs_lookup_ordered_range(inode
, hole_start
,
4812 block_end
- hole_start
);
4815 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4816 &cached_state
, GFP_NOFS
);
4817 btrfs_start_ordered_extent(inode
, ordered
, 1);
4818 btrfs_put_ordered_extent(ordered
);
4821 cur_offset
= hole_start
;
4823 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4824 block_end
- cur_offset
, 0);
4830 last_byte
= min(extent_map_end(em
), block_end
);
4831 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4832 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4833 struct extent_map
*hole_em
;
4834 hole_size
= last_byte
- cur_offset
;
4836 err
= maybe_insert_hole(root
, inode
, cur_offset
,
4840 btrfs_drop_extent_cache(inode
, cur_offset
,
4841 cur_offset
+ hole_size
- 1, 0);
4842 hole_em
= alloc_extent_map();
4844 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4845 &BTRFS_I(inode
)->runtime_flags
);
4848 hole_em
->start
= cur_offset
;
4849 hole_em
->len
= hole_size
;
4850 hole_em
->orig_start
= cur_offset
;
4852 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4853 hole_em
->block_len
= 0;
4854 hole_em
->orig_block_len
= 0;
4855 hole_em
->ram_bytes
= hole_size
;
4856 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4857 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4858 hole_em
->generation
= root
->fs_info
->generation
;
4861 write_lock(&em_tree
->lock
);
4862 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4863 write_unlock(&em_tree
->lock
);
4866 btrfs_drop_extent_cache(inode
, cur_offset
,
4870 free_extent_map(hole_em
);
4873 free_extent_map(em
);
4875 cur_offset
= last_byte
;
4876 if (cur_offset
>= block_end
)
4879 free_extent_map(em
);
4880 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4885 static int wait_snapshoting_atomic_t(atomic_t
*a
)
4891 static void wait_for_snapshot_creation(struct btrfs_root
*root
)
4896 ret
= btrfs_start_write_no_snapshoting(root
);
4899 wait_on_atomic_t(&root
->will_be_snapshoted
,
4900 wait_snapshoting_atomic_t
,
4901 TASK_UNINTERRUPTIBLE
);
4905 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4907 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4908 struct btrfs_trans_handle
*trans
;
4909 loff_t oldsize
= i_size_read(inode
);
4910 loff_t newsize
= attr
->ia_size
;
4911 int mask
= attr
->ia_valid
;
4915 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4916 * special case where we need to update the times despite not having
4917 * these flags set. For all other operations the VFS set these flags
4918 * explicitly if it wants a timestamp update.
4920 if (newsize
!= oldsize
) {
4921 inode_inc_iversion(inode
);
4922 if (!(mask
& (ATTR_CTIME
| ATTR_MTIME
)))
4923 inode
->i_ctime
= inode
->i_mtime
=
4924 current_fs_time(inode
->i_sb
);
4927 if (newsize
> oldsize
) {
4928 truncate_pagecache(inode
, newsize
);
4930 * Don't do an expanding truncate while snapshoting is ongoing.
4931 * This is to ensure the snapshot captures a fully consistent
4932 * state of this file - if the snapshot captures this expanding
4933 * truncation, it must capture all writes that happened before
4936 wait_for_snapshot_creation(root
);
4937 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4939 btrfs_end_write_no_snapshoting(root
);
4943 trans
= btrfs_start_transaction(root
, 1);
4944 if (IS_ERR(trans
)) {
4945 btrfs_end_write_no_snapshoting(root
);
4946 return PTR_ERR(trans
);
4949 i_size_write(inode
, newsize
);
4950 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4951 ret
= btrfs_update_inode(trans
, root
, inode
);
4952 btrfs_end_write_no_snapshoting(root
);
4953 btrfs_end_transaction(trans
, root
);
4957 * We're truncating a file that used to have good data down to
4958 * zero. Make sure it gets into the ordered flush list so that
4959 * any new writes get down to disk quickly.
4962 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4963 &BTRFS_I(inode
)->runtime_flags
);
4966 * 1 for the orphan item we're going to add
4967 * 1 for the orphan item deletion.
4969 trans
= btrfs_start_transaction(root
, 2);
4971 return PTR_ERR(trans
);
4974 * We need to do this in case we fail at _any_ point during the
4975 * actual truncate. Once we do the truncate_setsize we could
4976 * invalidate pages which forces any outstanding ordered io to
4977 * be instantly completed which will give us extents that need
4978 * to be truncated. If we fail to get an orphan inode down we
4979 * could have left over extents that were never meant to live,
4980 * so we need to garuntee from this point on that everything
4981 * will be consistent.
4983 ret
= btrfs_orphan_add(trans
, inode
);
4984 btrfs_end_transaction(trans
, root
);
4988 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4989 truncate_setsize(inode
, newsize
);
4991 /* Disable nonlocked read DIO to avoid the end less truncate */
4992 btrfs_inode_block_unlocked_dio(inode
);
4993 inode_dio_wait(inode
);
4994 btrfs_inode_resume_unlocked_dio(inode
);
4996 ret
= btrfs_truncate(inode
);
4997 if (ret
&& inode
->i_nlink
) {
5001 * failed to truncate, disk_i_size is only adjusted down
5002 * as we remove extents, so it should represent the true
5003 * size of the inode, so reset the in memory size and
5004 * delete our orphan entry.
5006 trans
= btrfs_join_transaction(root
);
5007 if (IS_ERR(trans
)) {
5008 btrfs_orphan_del(NULL
, inode
);
5011 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
5012 err
= btrfs_orphan_del(trans
, inode
);
5014 btrfs_abort_transaction(trans
, root
, err
);
5015 btrfs_end_transaction(trans
, root
);
5022 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
5024 struct inode
*inode
= d_inode(dentry
);
5025 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5028 if (btrfs_root_readonly(root
))
5031 err
= inode_change_ok(inode
, attr
);
5035 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
5036 err
= btrfs_setsize(inode
, attr
);
5041 if (attr
->ia_valid
) {
5042 setattr_copy(inode
, attr
);
5043 inode_inc_iversion(inode
);
5044 err
= btrfs_dirty_inode(inode
);
5046 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
5047 err
= posix_acl_chmod(inode
, inode
->i_mode
);
5054 * While truncating the inode pages during eviction, we get the VFS calling
5055 * btrfs_invalidatepage() against each page of the inode. This is slow because
5056 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5057 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5058 * extent_state structures over and over, wasting lots of time.
5060 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5061 * those expensive operations on a per page basis and do only the ordered io
5062 * finishing, while we release here the extent_map and extent_state structures,
5063 * without the excessive merging and splitting.
5065 static void evict_inode_truncate_pages(struct inode
*inode
)
5067 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5068 struct extent_map_tree
*map_tree
= &BTRFS_I(inode
)->extent_tree
;
5069 struct rb_node
*node
;
5071 ASSERT(inode
->i_state
& I_FREEING
);
5072 truncate_inode_pages_final(&inode
->i_data
);
5074 write_lock(&map_tree
->lock
);
5075 while (!RB_EMPTY_ROOT(&map_tree
->map
)) {
5076 struct extent_map
*em
;
5078 node
= rb_first(&map_tree
->map
);
5079 em
= rb_entry(node
, struct extent_map
, rb_node
);
5080 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
5081 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
5082 remove_extent_mapping(map_tree
, em
);
5083 free_extent_map(em
);
5084 if (need_resched()) {
5085 write_unlock(&map_tree
->lock
);
5087 write_lock(&map_tree
->lock
);
5090 write_unlock(&map_tree
->lock
);
5093 * Keep looping until we have no more ranges in the io tree.
5094 * We can have ongoing bios started by readpages (called from readahead)
5095 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5096 * still in progress (unlocked the pages in the bio but did not yet
5097 * unlocked the ranges in the io tree). Therefore this means some
5098 * ranges can still be locked and eviction started because before
5099 * submitting those bios, which are executed by a separate task (work
5100 * queue kthread), inode references (inode->i_count) were not taken
5101 * (which would be dropped in the end io callback of each bio).
5102 * Therefore here we effectively end up waiting for those bios and
5103 * anyone else holding locked ranges without having bumped the inode's
5104 * reference count - if we don't do it, when they access the inode's
5105 * io_tree to unlock a range it may be too late, leading to an
5106 * use-after-free issue.
5108 spin_lock(&io_tree
->lock
);
5109 while (!RB_EMPTY_ROOT(&io_tree
->state
)) {
5110 struct extent_state
*state
;
5111 struct extent_state
*cached_state
= NULL
;
5115 node
= rb_first(&io_tree
->state
);
5116 state
= rb_entry(node
, struct extent_state
, rb_node
);
5117 start
= state
->start
;
5119 spin_unlock(&io_tree
->lock
);
5121 lock_extent_bits(io_tree
, start
, end
, 0, &cached_state
);
5124 * If still has DELALLOC flag, the extent didn't reach disk,
5125 * and its reserved space won't be freed by delayed_ref.
5126 * So we need to free its reserved space here.
5127 * (Refer to comment in btrfs_invalidatepage, case 2)
5129 * Note, end is the bytenr of last byte, so we need + 1 here.
5131 if (state
->state
& EXTENT_DELALLOC
)
5132 btrfs_qgroup_free_data(inode
, start
, end
- start
+ 1);
5134 clear_extent_bit(io_tree
, start
, end
,
5135 EXTENT_LOCKED
| EXTENT_DIRTY
|
5136 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
5137 EXTENT_DEFRAG
, 1, 1,
5138 &cached_state
, GFP_NOFS
);
5141 spin_lock(&io_tree
->lock
);
5143 spin_unlock(&io_tree
->lock
);
5146 void btrfs_evict_inode(struct inode
*inode
)
5148 struct btrfs_trans_handle
*trans
;
5149 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5150 struct btrfs_block_rsv
*rsv
, *global_rsv
;
5151 int steal_from_global
= 0;
5152 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
5155 trace_btrfs_inode_evict(inode
);
5157 evict_inode_truncate_pages(inode
);
5159 if (inode
->i_nlink
&&
5160 ((btrfs_root_refs(&root
->root_item
) != 0 &&
5161 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
5162 btrfs_is_free_space_inode(inode
)))
5165 if (is_bad_inode(inode
)) {
5166 btrfs_orphan_del(NULL
, inode
);
5169 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
5170 if (!special_file(inode
->i_mode
))
5171 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
5173 btrfs_free_io_failure_record(inode
, 0, (u64
)-1);
5175 if (root
->fs_info
->log_root_recovering
) {
5176 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
5177 &BTRFS_I(inode
)->runtime_flags
));
5181 if (inode
->i_nlink
> 0) {
5182 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
5183 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
5187 ret
= btrfs_commit_inode_delayed_inode(inode
);
5189 btrfs_orphan_del(NULL
, inode
);
5193 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
5195 btrfs_orphan_del(NULL
, inode
);
5198 rsv
->size
= min_size
;
5200 global_rsv
= &root
->fs_info
->global_block_rsv
;
5202 btrfs_i_size_write(inode
, 0);
5205 * This is a bit simpler than btrfs_truncate since we've already
5206 * reserved our space for our orphan item in the unlink, so we just
5207 * need to reserve some slack space in case we add bytes and update
5208 * inode item when doing the truncate.
5211 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
5212 BTRFS_RESERVE_FLUSH_LIMIT
);
5215 * Try and steal from the global reserve since we will
5216 * likely not use this space anyway, we want to try as
5217 * hard as possible to get this to work.
5220 steal_from_global
++;
5222 steal_from_global
= 0;
5226 * steal_from_global == 0: we reserved stuff, hooray!
5227 * steal_from_global == 1: we didn't reserve stuff, boo!
5228 * steal_from_global == 2: we've committed, still not a lot of
5229 * room but maybe we'll have room in the global reserve this
5231 * steal_from_global == 3: abandon all hope!
5233 if (steal_from_global
> 2) {
5234 btrfs_warn(root
->fs_info
,
5235 "Could not get space for a delete, will truncate on mount %d",
5237 btrfs_orphan_del(NULL
, inode
);
5238 btrfs_free_block_rsv(root
, rsv
);
5242 trans
= btrfs_join_transaction(root
);
5243 if (IS_ERR(trans
)) {
5244 btrfs_orphan_del(NULL
, inode
);
5245 btrfs_free_block_rsv(root
, rsv
);
5250 * We can't just steal from the global reserve, we need tomake
5251 * sure there is room to do it, if not we need to commit and try
5254 if (steal_from_global
) {
5255 if (!btrfs_check_space_for_delayed_refs(trans
, root
))
5256 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
,
5263 * Couldn't steal from the global reserve, we have too much
5264 * pending stuff built up, commit the transaction and try it
5268 ret
= btrfs_commit_transaction(trans
, root
);
5270 btrfs_orphan_del(NULL
, inode
);
5271 btrfs_free_block_rsv(root
, rsv
);
5276 steal_from_global
= 0;
5279 trans
->block_rsv
= rsv
;
5281 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
5282 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
)
5285 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
5286 btrfs_end_transaction(trans
, root
);
5288 btrfs_btree_balance_dirty(root
);
5291 btrfs_free_block_rsv(root
, rsv
);
5294 * Errors here aren't a big deal, it just means we leave orphan items
5295 * in the tree. They will be cleaned up on the next mount.
5298 trans
->block_rsv
= root
->orphan_block_rsv
;
5299 btrfs_orphan_del(trans
, inode
);
5301 btrfs_orphan_del(NULL
, inode
);
5304 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
5305 if (!(root
== root
->fs_info
->tree_root
||
5306 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
5307 btrfs_return_ino(root
, btrfs_ino(inode
));
5309 btrfs_end_transaction(trans
, root
);
5310 btrfs_btree_balance_dirty(root
);
5312 btrfs_remove_delayed_node(inode
);
5318 * this returns the key found in the dir entry in the location pointer.
5319 * If no dir entries were found, location->objectid is 0.
5321 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
5322 struct btrfs_key
*location
)
5324 const char *name
= dentry
->d_name
.name
;
5325 int namelen
= dentry
->d_name
.len
;
5326 struct btrfs_dir_item
*di
;
5327 struct btrfs_path
*path
;
5328 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5331 path
= btrfs_alloc_path();
5335 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
5340 if (IS_ERR_OR_NULL(di
))
5343 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
5345 btrfs_free_path(path
);
5348 location
->objectid
= 0;
5353 * when we hit a tree root in a directory, the btrfs part of the inode
5354 * needs to be changed to reflect the root directory of the tree root. This
5355 * is kind of like crossing a mount point.
5357 static int fixup_tree_root_location(struct btrfs_root
*root
,
5359 struct dentry
*dentry
,
5360 struct btrfs_key
*location
,
5361 struct btrfs_root
**sub_root
)
5363 struct btrfs_path
*path
;
5364 struct btrfs_root
*new_root
;
5365 struct btrfs_root_ref
*ref
;
5366 struct extent_buffer
*leaf
;
5367 struct btrfs_key key
;
5371 path
= btrfs_alloc_path();
5378 key
.objectid
= BTRFS_I(dir
)->root
->root_key
.objectid
;
5379 key
.type
= BTRFS_ROOT_REF_KEY
;
5380 key
.offset
= location
->objectid
;
5382 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
, &key
, path
,
5390 leaf
= path
->nodes
[0];
5391 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
5392 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
5393 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
5396 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
5397 (unsigned long)(ref
+ 1),
5398 dentry
->d_name
.len
);
5402 btrfs_release_path(path
);
5404 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
5405 if (IS_ERR(new_root
)) {
5406 err
= PTR_ERR(new_root
);
5410 *sub_root
= new_root
;
5411 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
5412 location
->type
= BTRFS_INODE_ITEM_KEY
;
5413 location
->offset
= 0;
5416 btrfs_free_path(path
);
5420 static void inode_tree_add(struct inode
*inode
)
5422 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5423 struct btrfs_inode
*entry
;
5425 struct rb_node
*parent
;
5426 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
5427 u64 ino
= btrfs_ino(inode
);
5429 if (inode_unhashed(inode
))
5432 spin_lock(&root
->inode_lock
);
5433 p
= &root
->inode_tree
.rb_node
;
5436 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
5438 if (ino
< btrfs_ino(&entry
->vfs_inode
))
5439 p
= &parent
->rb_left
;
5440 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
5441 p
= &parent
->rb_right
;
5443 WARN_ON(!(entry
->vfs_inode
.i_state
&
5444 (I_WILL_FREE
| I_FREEING
)));
5445 rb_replace_node(parent
, new, &root
->inode_tree
);
5446 RB_CLEAR_NODE(parent
);
5447 spin_unlock(&root
->inode_lock
);
5451 rb_link_node(new, parent
, p
);
5452 rb_insert_color(new, &root
->inode_tree
);
5453 spin_unlock(&root
->inode_lock
);
5456 static void inode_tree_del(struct inode
*inode
)
5458 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5461 spin_lock(&root
->inode_lock
);
5462 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
5463 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
5464 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
5465 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5467 spin_unlock(&root
->inode_lock
);
5469 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
5470 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
5471 spin_lock(&root
->inode_lock
);
5472 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5473 spin_unlock(&root
->inode_lock
);
5475 btrfs_add_dead_root(root
);
5479 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
5481 struct rb_node
*node
;
5482 struct rb_node
*prev
;
5483 struct btrfs_inode
*entry
;
5484 struct inode
*inode
;
5487 if (!test_bit(BTRFS_FS_STATE_ERROR
, &root
->fs_info
->fs_state
))
5488 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
5490 spin_lock(&root
->inode_lock
);
5492 node
= root
->inode_tree
.rb_node
;
5496 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5498 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
5499 node
= node
->rb_left
;
5500 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
5501 node
= node
->rb_right
;
5507 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
5508 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
5512 prev
= rb_next(prev
);
5516 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5517 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
5518 inode
= igrab(&entry
->vfs_inode
);
5520 spin_unlock(&root
->inode_lock
);
5521 if (atomic_read(&inode
->i_count
) > 1)
5522 d_prune_aliases(inode
);
5524 * btrfs_drop_inode will have it removed from
5525 * the inode cache when its usage count
5530 spin_lock(&root
->inode_lock
);
5534 if (cond_resched_lock(&root
->inode_lock
))
5537 node
= rb_next(node
);
5539 spin_unlock(&root
->inode_lock
);
5542 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
5544 struct btrfs_iget_args
*args
= p
;
5545 inode
->i_ino
= args
->location
->objectid
;
5546 memcpy(&BTRFS_I(inode
)->location
, args
->location
,
5547 sizeof(*args
->location
));
5548 BTRFS_I(inode
)->root
= args
->root
;
5552 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
5554 struct btrfs_iget_args
*args
= opaque
;
5555 return args
->location
->objectid
== BTRFS_I(inode
)->location
.objectid
&&
5556 args
->root
== BTRFS_I(inode
)->root
;
5559 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
5560 struct btrfs_key
*location
,
5561 struct btrfs_root
*root
)
5563 struct inode
*inode
;
5564 struct btrfs_iget_args args
;
5565 unsigned long hashval
= btrfs_inode_hash(location
->objectid
, root
);
5567 args
.location
= location
;
5570 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
5571 btrfs_init_locked_inode
,
5576 /* Get an inode object given its location and corresponding root.
5577 * Returns in *is_new if the inode was read from disk
5579 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
5580 struct btrfs_root
*root
, int *new)
5582 struct inode
*inode
;
5584 inode
= btrfs_iget_locked(s
, location
, root
);
5586 return ERR_PTR(-ENOMEM
);
5588 if (inode
->i_state
& I_NEW
) {
5589 btrfs_read_locked_inode(inode
);
5590 if (!is_bad_inode(inode
)) {
5591 inode_tree_add(inode
);
5592 unlock_new_inode(inode
);
5596 unlock_new_inode(inode
);
5598 inode
= ERR_PTR(-ESTALE
);
5605 static struct inode
*new_simple_dir(struct super_block
*s
,
5606 struct btrfs_key
*key
,
5607 struct btrfs_root
*root
)
5609 struct inode
*inode
= new_inode(s
);
5612 return ERR_PTR(-ENOMEM
);
5614 BTRFS_I(inode
)->root
= root
;
5615 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5616 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5618 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5619 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5620 inode
->i_fop
= &simple_dir_operations
;
5621 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5622 inode
->i_mtime
= CURRENT_TIME
;
5623 inode
->i_atime
= inode
->i_mtime
;
5624 inode
->i_ctime
= inode
->i_mtime
;
5625 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
5630 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5632 struct inode
*inode
;
5633 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5634 struct btrfs_root
*sub_root
= root
;
5635 struct btrfs_key location
;
5639 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5640 return ERR_PTR(-ENAMETOOLONG
);
5642 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5644 return ERR_PTR(ret
);
5646 if (location
.objectid
== 0)
5647 return ERR_PTR(-ENOENT
);
5649 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5650 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5654 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5656 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
5657 ret
= fixup_tree_root_location(root
, dir
, dentry
,
5658 &location
, &sub_root
);
5661 inode
= ERR_PTR(ret
);
5663 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5665 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5667 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
5669 if (!IS_ERR(inode
) && root
!= sub_root
) {
5670 down_read(&root
->fs_info
->cleanup_work_sem
);
5671 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5672 ret
= btrfs_orphan_cleanup(sub_root
);
5673 up_read(&root
->fs_info
->cleanup_work_sem
);
5676 inode
= ERR_PTR(ret
);
5683 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5685 struct btrfs_root
*root
;
5686 struct inode
*inode
= d_inode(dentry
);
5688 if (!inode
&& !IS_ROOT(dentry
))
5689 inode
= d_inode(dentry
->d_parent
);
5692 root
= BTRFS_I(inode
)->root
;
5693 if (btrfs_root_refs(&root
->root_item
) == 0)
5696 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5702 static void btrfs_dentry_release(struct dentry
*dentry
)
5704 kfree(dentry
->d_fsdata
);
5707 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5710 struct inode
*inode
;
5712 inode
= btrfs_lookup_dentry(dir
, dentry
);
5713 if (IS_ERR(inode
)) {
5714 if (PTR_ERR(inode
) == -ENOENT
)
5717 return ERR_CAST(inode
);
5720 return d_splice_alias(inode
, dentry
);
5723 unsigned char btrfs_filetype_table
[] = {
5724 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5727 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
5729 struct inode
*inode
= file_inode(file
);
5730 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5731 struct btrfs_item
*item
;
5732 struct btrfs_dir_item
*di
;
5733 struct btrfs_key key
;
5734 struct btrfs_key found_key
;
5735 struct btrfs_path
*path
;
5736 struct list_head ins_list
;
5737 struct list_head del_list
;
5739 struct extent_buffer
*leaf
;
5741 unsigned char d_type
;
5746 int key_type
= BTRFS_DIR_INDEX_KEY
;
5750 int is_curr
= 0; /* ctx->pos points to the current index? */
5752 /* FIXME, use a real flag for deciding about the key type */
5753 if (root
->fs_info
->tree_root
== root
)
5754 key_type
= BTRFS_DIR_ITEM_KEY
;
5756 if (!dir_emit_dots(file
, ctx
))
5759 path
= btrfs_alloc_path();
5765 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5766 INIT_LIST_HEAD(&ins_list
);
5767 INIT_LIST_HEAD(&del_list
);
5768 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5771 key
.type
= key_type
;
5772 key
.offset
= ctx
->pos
;
5773 key
.objectid
= btrfs_ino(inode
);
5775 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5780 leaf
= path
->nodes
[0];
5781 slot
= path
->slots
[0];
5782 if (slot
>= btrfs_header_nritems(leaf
)) {
5783 ret
= btrfs_next_leaf(root
, path
);
5791 item
= btrfs_item_nr(slot
);
5792 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5794 if (found_key
.objectid
!= key
.objectid
)
5796 if (found_key
.type
!= key_type
)
5798 if (found_key
.offset
< ctx
->pos
)
5800 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5801 btrfs_should_delete_dir_index(&del_list
,
5805 ctx
->pos
= found_key
.offset
;
5808 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5810 di_total
= btrfs_item_size(leaf
, item
);
5812 while (di_cur
< di_total
) {
5813 struct btrfs_key location
;
5815 if (verify_dir_item(root
, leaf
, di
))
5818 name_len
= btrfs_dir_name_len(leaf
, di
);
5819 if (name_len
<= sizeof(tmp_name
)) {
5820 name_ptr
= tmp_name
;
5822 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5828 read_extent_buffer(leaf
, name_ptr
,
5829 (unsigned long)(di
+ 1), name_len
);
5831 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5832 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5835 /* is this a reference to our own snapshot? If so
5838 * In contrast to old kernels, we insert the snapshot's
5839 * dir item and dir index after it has been created, so
5840 * we won't find a reference to our own snapshot. We
5841 * still keep the following code for backward
5844 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5845 location
.objectid
== root
->root_key
.objectid
) {
5849 over
= !dir_emit(ctx
, name_ptr
, name_len
,
5850 location
.objectid
, d_type
);
5853 if (name_ptr
!= tmp_name
)
5858 di_len
= btrfs_dir_name_len(leaf
, di
) +
5859 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5861 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5867 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5870 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
);
5875 /* Reached end of directory/root. Bump pos past the last item. */
5879 * Stop new entries from being returned after we return the last
5882 * New directory entries are assigned a strictly increasing
5883 * offset. This means that new entries created during readdir
5884 * are *guaranteed* to be seen in the future by that readdir.
5885 * This has broken buggy programs which operate on names as
5886 * they're returned by readdir. Until we re-use freed offsets
5887 * we have this hack to stop new entries from being returned
5888 * under the assumption that they'll never reach this huge
5891 * This is being careful not to overflow 32bit loff_t unless the
5892 * last entry requires it because doing so has broken 32bit apps
5895 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5896 if (ctx
->pos
>= INT_MAX
)
5897 ctx
->pos
= LLONG_MAX
;
5904 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5905 btrfs_put_delayed_items(&ins_list
, &del_list
);
5906 btrfs_free_path(path
);
5910 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5912 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5913 struct btrfs_trans_handle
*trans
;
5915 bool nolock
= false;
5917 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5920 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5923 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5925 trans
= btrfs_join_transaction_nolock(root
);
5927 trans
= btrfs_join_transaction(root
);
5929 return PTR_ERR(trans
);
5930 ret
= btrfs_commit_transaction(trans
, root
);
5936 * This is somewhat expensive, updating the tree every time the
5937 * inode changes. But, it is most likely to find the inode in cache.
5938 * FIXME, needs more benchmarking...there are no reasons other than performance
5939 * to keep or drop this code.
5941 static int btrfs_dirty_inode(struct inode
*inode
)
5943 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5944 struct btrfs_trans_handle
*trans
;
5947 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5950 trans
= btrfs_join_transaction(root
);
5952 return PTR_ERR(trans
);
5954 ret
= btrfs_update_inode(trans
, root
, inode
);
5955 if (ret
&& ret
== -ENOSPC
) {
5956 /* whoops, lets try again with the full transaction */
5957 btrfs_end_transaction(trans
, root
);
5958 trans
= btrfs_start_transaction(root
, 1);
5960 return PTR_ERR(trans
);
5962 ret
= btrfs_update_inode(trans
, root
, inode
);
5964 btrfs_end_transaction(trans
, root
);
5965 if (BTRFS_I(inode
)->delayed_node
)
5966 btrfs_balance_delayed_items(root
);
5972 * This is a copy of file_update_time. We need this so we can return error on
5973 * ENOSPC for updating the inode in the case of file write and mmap writes.
5975 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5978 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5980 if (btrfs_root_readonly(root
))
5983 if (flags
& S_VERSION
)
5984 inode_inc_iversion(inode
);
5985 if (flags
& S_CTIME
)
5986 inode
->i_ctime
= *now
;
5987 if (flags
& S_MTIME
)
5988 inode
->i_mtime
= *now
;
5989 if (flags
& S_ATIME
)
5990 inode
->i_atime
= *now
;
5991 return btrfs_dirty_inode(inode
);
5995 * find the highest existing sequence number in a directory
5996 * and then set the in-memory index_cnt variable to reflect
5997 * free sequence numbers
5999 static int btrfs_set_inode_index_count(struct inode
*inode
)
6001 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6002 struct btrfs_key key
, found_key
;
6003 struct btrfs_path
*path
;
6004 struct extent_buffer
*leaf
;
6007 key
.objectid
= btrfs_ino(inode
);
6008 key
.type
= BTRFS_DIR_INDEX_KEY
;
6009 key
.offset
= (u64
)-1;
6011 path
= btrfs_alloc_path();
6015 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
6018 /* FIXME: we should be able to handle this */
6024 * MAGIC NUMBER EXPLANATION:
6025 * since we search a directory based on f_pos we have to start at 2
6026 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6027 * else has to start at 2
6029 if (path
->slots
[0] == 0) {
6030 BTRFS_I(inode
)->index_cnt
= 2;
6036 leaf
= path
->nodes
[0];
6037 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6039 if (found_key
.objectid
!= btrfs_ino(inode
) ||
6040 found_key
.type
!= BTRFS_DIR_INDEX_KEY
) {
6041 BTRFS_I(inode
)->index_cnt
= 2;
6045 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
6047 btrfs_free_path(path
);
6052 * helper to find a free sequence number in a given directory. This current
6053 * code is very simple, later versions will do smarter things in the btree
6055 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
6059 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
6060 ret
= btrfs_inode_delayed_dir_index_count(dir
);
6062 ret
= btrfs_set_inode_index_count(dir
);
6068 *index
= BTRFS_I(dir
)->index_cnt
;
6069 BTRFS_I(dir
)->index_cnt
++;
6074 static int btrfs_insert_inode_locked(struct inode
*inode
)
6076 struct btrfs_iget_args args
;
6077 args
.location
= &BTRFS_I(inode
)->location
;
6078 args
.root
= BTRFS_I(inode
)->root
;
6080 return insert_inode_locked4(inode
,
6081 btrfs_inode_hash(inode
->i_ino
, BTRFS_I(inode
)->root
),
6082 btrfs_find_actor
, &args
);
6085 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
6086 struct btrfs_root
*root
,
6088 const char *name
, int name_len
,
6089 u64 ref_objectid
, u64 objectid
,
6090 umode_t mode
, u64
*index
)
6092 struct inode
*inode
;
6093 struct btrfs_inode_item
*inode_item
;
6094 struct btrfs_key
*location
;
6095 struct btrfs_path
*path
;
6096 struct btrfs_inode_ref
*ref
;
6097 struct btrfs_key key
[2];
6099 int nitems
= name
? 2 : 1;
6103 path
= btrfs_alloc_path();
6105 return ERR_PTR(-ENOMEM
);
6107 inode
= new_inode(root
->fs_info
->sb
);
6109 btrfs_free_path(path
);
6110 return ERR_PTR(-ENOMEM
);
6114 * O_TMPFILE, set link count to 0, so that after this point,
6115 * we fill in an inode item with the correct link count.
6118 set_nlink(inode
, 0);
6121 * we have to initialize this early, so we can reclaim the inode
6122 * number if we fail afterwards in this function.
6124 inode
->i_ino
= objectid
;
6127 trace_btrfs_inode_request(dir
);
6129 ret
= btrfs_set_inode_index(dir
, index
);
6131 btrfs_free_path(path
);
6133 return ERR_PTR(ret
);
6139 * index_cnt is ignored for everything but a dir,
6140 * btrfs_get_inode_index_count has an explanation for the magic
6143 BTRFS_I(inode
)->index_cnt
= 2;
6144 BTRFS_I(inode
)->dir_index
= *index
;
6145 BTRFS_I(inode
)->root
= root
;
6146 BTRFS_I(inode
)->generation
= trans
->transid
;
6147 inode
->i_generation
= BTRFS_I(inode
)->generation
;
6150 * We could have gotten an inode number from somebody who was fsynced
6151 * and then removed in this same transaction, so let's just set full
6152 * sync since it will be a full sync anyway and this will blow away the
6153 * old info in the log.
6155 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
6157 key
[0].objectid
= objectid
;
6158 key
[0].type
= BTRFS_INODE_ITEM_KEY
;
6161 sizes
[0] = sizeof(struct btrfs_inode_item
);
6165 * Start new inodes with an inode_ref. This is slightly more
6166 * efficient for small numbers of hard links since they will
6167 * be packed into one item. Extended refs will kick in if we
6168 * add more hard links than can fit in the ref item.
6170 key
[1].objectid
= objectid
;
6171 key
[1].type
= BTRFS_INODE_REF_KEY
;
6172 key
[1].offset
= ref_objectid
;
6174 sizes
[1] = name_len
+ sizeof(*ref
);
6177 location
= &BTRFS_I(inode
)->location
;
6178 location
->objectid
= objectid
;
6179 location
->offset
= 0;
6180 location
->type
= BTRFS_INODE_ITEM_KEY
;
6182 ret
= btrfs_insert_inode_locked(inode
);
6186 path
->leave_spinning
= 1;
6187 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, nitems
);
6191 inode_init_owner(inode
, dir
, mode
);
6192 inode_set_bytes(inode
, 0);
6194 inode
->i_mtime
= CURRENT_TIME
;
6195 inode
->i_atime
= inode
->i_mtime
;
6196 inode
->i_ctime
= inode
->i_mtime
;
6197 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
6199 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
6200 struct btrfs_inode_item
);
6201 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
6202 sizeof(*inode_item
));
6203 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
6206 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
6207 struct btrfs_inode_ref
);
6208 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
6209 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
6210 ptr
= (unsigned long)(ref
+ 1);
6211 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
6214 btrfs_mark_buffer_dirty(path
->nodes
[0]);
6215 btrfs_free_path(path
);
6217 btrfs_inherit_iflags(inode
, dir
);
6219 if (S_ISREG(mode
)) {
6220 if (btrfs_test_opt(root
, NODATASUM
))
6221 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
6222 if (btrfs_test_opt(root
, NODATACOW
))
6223 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
6224 BTRFS_INODE_NODATASUM
;
6227 inode_tree_add(inode
);
6229 trace_btrfs_inode_new(inode
);
6230 btrfs_set_inode_last_trans(trans
, inode
);
6232 btrfs_update_root_times(trans
, root
);
6234 ret
= btrfs_inode_inherit_props(trans
, inode
, dir
);
6236 btrfs_err(root
->fs_info
,
6237 "error inheriting props for ino %llu (root %llu): %d",
6238 btrfs_ino(inode
), root
->root_key
.objectid
, ret
);
6243 unlock_new_inode(inode
);
6246 BTRFS_I(dir
)->index_cnt
--;
6247 btrfs_free_path(path
);
6249 return ERR_PTR(ret
);
6252 static inline u8
btrfs_inode_type(struct inode
*inode
)
6254 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
6258 * utility function to add 'inode' into 'parent_inode' with
6259 * a give name and a given sequence number.
6260 * if 'add_backref' is true, also insert a backref from the
6261 * inode to the parent directory.
6263 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
6264 struct inode
*parent_inode
, struct inode
*inode
,
6265 const char *name
, int name_len
, int add_backref
, u64 index
)
6268 struct btrfs_key key
;
6269 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
6270 u64 ino
= btrfs_ino(inode
);
6271 u64 parent_ino
= btrfs_ino(parent_inode
);
6273 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6274 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
6277 key
.type
= BTRFS_INODE_ITEM_KEY
;
6281 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6282 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
6283 key
.objectid
, root
->root_key
.objectid
,
6284 parent_ino
, index
, name
, name_len
);
6285 } else if (add_backref
) {
6286 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
6290 /* Nothing to clean up yet */
6294 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
6296 btrfs_inode_type(inode
), index
);
6297 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
6300 btrfs_abort_transaction(trans
, root
, ret
);
6304 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
6306 inode_inc_iversion(parent_inode
);
6307 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
6308 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
6310 btrfs_abort_transaction(trans
, root
, ret
);
6314 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6317 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
6318 key
.objectid
, root
->root_key
.objectid
,
6319 parent_ino
, &local_index
, name
, name_len
);
6321 } else if (add_backref
) {
6325 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
6326 ino
, parent_ino
, &local_index
);
6331 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
6332 struct inode
*dir
, struct dentry
*dentry
,
6333 struct inode
*inode
, int backref
, u64 index
)
6335 int err
= btrfs_add_link(trans
, dir
, inode
,
6336 dentry
->d_name
.name
, dentry
->d_name
.len
,
6343 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
6344 umode_t mode
, dev_t rdev
)
6346 struct btrfs_trans_handle
*trans
;
6347 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6348 struct inode
*inode
= NULL
;
6354 if (!new_valid_dev(rdev
))
6358 * 2 for inode item and ref
6360 * 1 for xattr if selinux is on
6362 trans
= btrfs_start_transaction(root
, 5);
6364 return PTR_ERR(trans
);
6366 err
= btrfs_find_free_ino(root
, &objectid
);
6370 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6371 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6373 if (IS_ERR(inode
)) {
6374 err
= PTR_ERR(inode
);
6379 * If the active LSM wants to access the inode during
6380 * d_instantiate it needs these. Smack checks to see
6381 * if the filesystem supports xattrs by looking at the
6384 inode
->i_op
= &btrfs_special_inode_operations
;
6385 init_special_inode(inode
, inode
->i_mode
, rdev
);
6387 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6389 goto out_unlock_inode
;
6391 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6393 goto out_unlock_inode
;
6395 btrfs_update_inode(trans
, root
, inode
);
6396 unlock_new_inode(inode
);
6397 d_instantiate(dentry
, inode
);
6401 btrfs_end_transaction(trans
, root
);
6402 btrfs_balance_delayed_items(root
);
6403 btrfs_btree_balance_dirty(root
);
6405 inode_dec_link_count(inode
);
6412 unlock_new_inode(inode
);
6417 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
6418 umode_t mode
, bool excl
)
6420 struct btrfs_trans_handle
*trans
;
6421 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6422 struct inode
*inode
= NULL
;
6423 int drop_inode_on_err
= 0;
6429 * 2 for inode item and ref
6431 * 1 for xattr if selinux is on
6433 trans
= btrfs_start_transaction(root
, 5);
6435 return PTR_ERR(trans
);
6437 err
= btrfs_find_free_ino(root
, &objectid
);
6441 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6442 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6444 if (IS_ERR(inode
)) {
6445 err
= PTR_ERR(inode
);
6448 drop_inode_on_err
= 1;
6450 * If the active LSM wants to access the inode during
6451 * d_instantiate it needs these. Smack checks to see
6452 * if the filesystem supports xattrs by looking at the
6455 inode
->i_fop
= &btrfs_file_operations
;
6456 inode
->i_op
= &btrfs_file_inode_operations
;
6457 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6459 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6461 goto out_unlock_inode
;
6463 err
= btrfs_update_inode(trans
, root
, inode
);
6465 goto out_unlock_inode
;
6467 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6469 goto out_unlock_inode
;
6471 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6472 unlock_new_inode(inode
);
6473 d_instantiate(dentry
, inode
);
6476 btrfs_end_transaction(trans
, root
);
6477 if (err
&& drop_inode_on_err
) {
6478 inode_dec_link_count(inode
);
6481 btrfs_balance_delayed_items(root
);
6482 btrfs_btree_balance_dirty(root
);
6486 unlock_new_inode(inode
);
6491 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
6492 struct dentry
*dentry
)
6494 struct btrfs_trans_handle
*trans
;
6495 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6496 struct inode
*inode
= d_inode(old_dentry
);
6501 /* do not allow sys_link's with other subvols of the same device */
6502 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
6505 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
6508 err
= btrfs_set_inode_index(dir
, &index
);
6513 * 2 items for inode and inode ref
6514 * 2 items for dir items
6515 * 1 item for parent inode
6517 trans
= btrfs_start_transaction(root
, 5);
6518 if (IS_ERR(trans
)) {
6519 err
= PTR_ERR(trans
);
6523 /* There are several dir indexes for this inode, clear the cache. */
6524 BTRFS_I(inode
)->dir_index
= 0ULL;
6526 inode_inc_iversion(inode
);
6527 inode
->i_ctime
= CURRENT_TIME
;
6529 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
6531 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
6536 struct dentry
*parent
= dentry
->d_parent
;
6537 err
= btrfs_update_inode(trans
, root
, inode
);
6540 if (inode
->i_nlink
== 1) {
6542 * If new hard link count is 1, it's a file created
6543 * with open(2) O_TMPFILE flag.
6545 err
= btrfs_orphan_del(trans
, inode
);
6549 d_instantiate(dentry
, inode
);
6550 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
6553 btrfs_end_transaction(trans
, root
);
6554 btrfs_balance_delayed_items(root
);
6557 inode_dec_link_count(inode
);
6560 btrfs_btree_balance_dirty(root
);
6564 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
6566 struct inode
*inode
= NULL
;
6567 struct btrfs_trans_handle
*trans
;
6568 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6570 int drop_on_err
= 0;
6575 * 2 items for inode and ref
6576 * 2 items for dir items
6577 * 1 for xattr if selinux is on
6579 trans
= btrfs_start_transaction(root
, 5);
6581 return PTR_ERR(trans
);
6583 err
= btrfs_find_free_ino(root
, &objectid
);
6587 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6588 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6589 S_IFDIR
| mode
, &index
);
6590 if (IS_ERR(inode
)) {
6591 err
= PTR_ERR(inode
);
6596 /* these must be set before we unlock the inode */
6597 inode
->i_op
= &btrfs_dir_inode_operations
;
6598 inode
->i_fop
= &btrfs_dir_file_operations
;
6600 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6602 goto out_fail_inode
;
6604 btrfs_i_size_write(inode
, 0);
6605 err
= btrfs_update_inode(trans
, root
, inode
);
6607 goto out_fail_inode
;
6609 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
6610 dentry
->d_name
.len
, 0, index
);
6612 goto out_fail_inode
;
6614 d_instantiate(dentry
, inode
);
6616 * mkdir is special. We're unlocking after we call d_instantiate
6617 * to avoid a race with nfsd calling d_instantiate.
6619 unlock_new_inode(inode
);
6623 btrfs_end_transaction(trans
, root
);
6625 inode_dec_link_count(inode
);
6628 btrfs_balance_delayed_items(root
);
6629 btrfs_btree_balance_dirty(root
);
6633 unlock_new_inode(inode
);
6637 /* Find next extent map of a given extent map, caller needs to ensure locks */
6638 static struct extent_map
*next_extent_map(struct extent_map
*em
)
6640 struct rb_node
*next
;
6642 next
= rb_next(&em
->rb_node
);
6645 return container_of(next
, struct extent_map
, rb_node
);
6648 static struct extent_map
*prev_extent_map(struct extent_map
*em
)
6650 struct rb_node
*prev
;
6652 prev
= rb_prev(&em
->rb_node
);
6655 return container_of(prev
, struct extent_map
, rb_node
);
6658 /* helper for btfs_get_extent. Given an existing extent in the tree,
6659 * the existing extent is the nearest extent to map_start,
6660 * and an extent that you want to insert, deal with overlap and insert
6661 * the best fitted new extent into the tree.
6663 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
6664 struct extent_map
*existing
,
6665 struct extent_map
*em
,
6668 struct extent_map
*prev
;
6669 struct extent_map
*next
;
6674 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
6676 if (existing
->start
> map_start
) {
6678 prev
= prev_extent_map(next
);
6681 next
= next_extent_map(prev
);
6684 start
= prev
? extent_map_end(prev
) : em
->start
;
6685 start
= max_t(u64
, start
, em
->start
);
6686 end
= next
? next
->start
: extent_map_end(em
);
6687 end
= min_t(u64
, end
, extent_map_end(em
));
6688 start_diff
= start
- em
->start
;
6690 em
->len
= end
- start
;
6691 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
6692 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
6693 em
->block_start
+= start_diff
;
6694 em
->block_len
-= start_diff
;
6696 return add_extent_mapping(em_tree
, em
, 0);
6699 static noinline
int uncompress_inline(struct btrfs_path
*path
,
6700 struct inode
*inode
, struct page
*page
,
6701 size_t pg_offset
, u64 extent_offset
,
6702 struct btrfs_file_extent_item
*item
)
6705 struct extent_buffer
*leaf
= path
->nodes
[0];
6708 unsigned long inline_size
;
6712 WARN_ON(pg_offset
!= 0);
6713 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6714 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6715 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6716 btrfs_item_nr(path
->slots
[0]));
6717 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6720 ptr
= btrfs_file_extent_inline_start(item
);
6722 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6724 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
6725 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6726 extent_offset
, inline_size
, max_size
);
6732 * a bit scary, this does extent mapping from logical file offset to the disk.
6733 * the ugly parts come from merging extents from the disk with the in-ram
6734 * representation. This gets more complex because of the data=ordered code,
6735 * where the in-ram extents might be locked pending data=ordered completion.
6737 * This also copies inline extents directly into the page.
6740 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
6741 size_t pg_offset
, u64 start
, u64 len
,
6746 u64 extent_start
= 0;
6748 u64 objectid
= btrfs_ino(inode
);
6750 struct btrfs_path
*path
= NULL
;
6751 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6752 struct btrfs_file_extent_item
*item
;
6753 struct extent_buffer
*leaf
;
6754 struct btrfs_key found_key
;
6755 struct extent_map
*em
= NULL
;
6756 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
6757 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6758 struct btrfs_trans_handle
*trans
= NULL
;
6759 const bool new_inline
= !page
|| create
;
6762 read_lock(&em_tree
->lock
);
6763 em
= lookup_extent_mapping(em_tree
, start
, len
);
6765 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6766 read_unlock(&em_tree
->lock
);
6769 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6770 free_extent_map(em
);
6771 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6772 free_extent_map(em
);
6776 em
= alloc_extent_map();
6781 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6782 em
->start
= EXTENT_MAP_HOLE
;
6783 em
->orig_start
= EXTENT_MAP_HOLE
;
6785 em
->block_len
= (u64
)-1;
6788 path
= btrfs_alloc_path();
6794 * Chances are we'll be called again, so go ahead and do
6800 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6801 objectid
, start
, trans
!= NULL
);
6808 if (path
->slots
[0] == 0)
6813 leaf
= path
->nodes
[0];
6814 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6815 struct btrfs_file_extent_item
);
6816 /* are we inside the extent that was found? */
6817 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6818 found_type
= found_key
.type
;
6819 if (found_key
.objectid
!= objectid
||
6820 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6822 * If we backup past the first extent we want to move forward
6823 * and see if there is an extent in front of us, otherwise we'll
6824 * say there is a hole for our whole search range which can
6831 found_type
= btrfs_file_extent_type(leaf
, item
);
6832 extent_start
= found_key
.offset
;
6833 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6834 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6835 extent_end
= extent_start
+
6836 btrfs_file_extent_num_bytes(leaf
, item
);
6837 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6839 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6840 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6843 if (start
>= extent_end
) {
6845 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6846 ret
= btrfs_next_leaf(root
, path
);
6853 leaf
= path
->nodes
[0];
6855 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6856 if (found_key
.objectid
!= objectid
||
6857 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6859 if (start
+ len
<= found_key
.offset
)
6861 if (start
> found_key
.offset
)
6864 em
->orig_start
= start
;
6865 em
->len
= found_key
.offset
- start
;
6869 btrfs_extent_item_to_extent_map(inode
, path
, item
, new_inline
, em
);
6871 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6872 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6874 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6878 size_t extent_offset
;
6884 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6885 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6886 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6887 size
- extent_offset
);
6888 em
->start
= extent_start
+ extent_offset
;
6889 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6890 em
->orig_block_len
= em
->len
;
6891 em
->orig_start
= em
->start
;
6892 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6893 if (create
== 0 && !PageUptodate(page
)) {
6894 if (btrfs_file_extent_compression(leaf
, item
) !=
6895 BTRFS_COMPRESS_NONE
) {
6896 ret
= uncompress_inline(path
, inode
, page
,
6898 extent_offset
, item
);
6905 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6907 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6908 memset(map
+ pg_offset
+ copy_size
, 0,
6909 PAGE_CACHE_SIZE
- pg_offset
-
6914 flush_dcache_page(page
);
6915 } else if (create
&& PageUptodate(page
)) {
6919 free_extent_map(em
);
6922 btrfs_release_path(path
);
6923 trans
= btrfs_join_transaction(root
);
6926 return ERR_CAST(trans
);
6930 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6933 btrfs_mark_buffer_dirty(leaf
);
6935 set_extent_uptodate(io_tree
, em
->start
,
6936 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6941 em
->orig_start
= start
;
6944 em
->block_start
= EXTENT_MAP_HOLE
;
6945 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6947 btrfs_release_path(path
);
6948 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6949 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6950 em
->start
, em
->len
, start
, len
);
6956 write_lock(&em_tree
->lock
);
6957 ret
= add_extent_mapping(em_tree
, em
, 0);
6958 /* it is possible that someone inserted the extent into the tree
6959 * while we had the lock dropped. It is also possible that
6960 * an overlapping map exists in the tree
6962 if (ret
== -EEXIST
) {
6963 struct extent_map
*existing
;
6967 existing
= search_extent_mapping(em_tree
, start
, len
);
6969 * existing will always be non-NULL, since there must be
6970 * extent causing the -EEXIST.
6972 if (start
>= extent_map_end(existing
) ||
6973 start
<= existing
->start
) {
6975 * The existing extent map is the one nearest to
6976 * the [start, start + len) range which overlaps
6978 err
= merge_extent_mapping(em_tree
, existing
,
6980 free_extent_map(existing
);
6982 free_extent_map(em
);
6986 free_extent_map(em
);
6991 write_unlock(&em_tree
->lock
);
6994 trace_btrfs_get_extent(root
, em
);
6996 btrfs_free_path(path
);
6998 ret
= btrfs_end_transaction(trans
, root
);
7003 free_extent_map(em
);
7004 return ERR_PTR(err
);
7006 BUG_ON(!em
); /* Error is always set */
7010 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
7011 size_t pg_offset
, u64 start
, u64 len
,
7014 struct extent_map
*em
;
7015 struct extent_map
*hole_em
= NULL
;
7016 u64 range_start
= start
;
7022 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
7029 * - a pre-alloc extent,
7030 * there might actually be delalloc bytes behind it.
7032 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
7033 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7039 /* check to see if we've wrapped (len == -1 or similar) */
7048 /* ok, we didn't find anything, lets look for delalloc */
7049 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
7050 end
, len
, EXTENT_DELALLOC
, 1);
7051 found_end
= range_start
+ found
;
7052 if (found_end
< range_start
)
7053 found_end
= (u64
)-1;
7056 * we didn't find anything useful, return
7057 * the original results from get_extent()
7059 if (range_start
> end
|| found_end
<= start
) {
7065 /* adjust the range_start to make sure it doesn't
7066 * go backwards from the start they passed in
7068 range_start
= max(start
, range_start
);
7069 found
= found_end
- range_start
;
7072 u64 hole_start
= start
;
7075 em
= alloc_extent_map();
7081 * when btrfs_get_extent can't find anything it
7082 * returns one huge hole
7084 * make sure what it found really fits our range, and
7085 * adjust to make sure it is based on the start from
7089 u64 calc_end
= extent_map_end(hole_em
);
7091 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
7092 free_extent_map(hole_em
);
7095 hole_start
= max(hole_em
->start
, start
);
7096 hole_len
= calc_end
- hole_start
;
7100 if (hole_em
&& range_start
> hole_start
) {
7101 /* our hole starts before our delalloc, so we
7102 * have to return just the parts of the hole
7103 * that go until the delalloc starts
7105 em
->len
= min(hole_len
,
7106 range_start
- hole_start
);
7107 em
->start
= hole_start
;
7108 em
->orig_start
= hole_start
;
7110 * don't adjust block start at all,
7111 * it is fixed at EXTENT_MAP_HOLE
7113 em
->block_start
= hole_em
->block_start
;
7114 em
->block_len
= hole_len
;
7115 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
7116 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
7118 em
->start
= range_start
;
7120 em
->orig_start
= range_start
;
7121 em
->block_start
= EXTENT_MAP_DELALLOC
;
7122 em
->block_len
= found
;
7124 } else if (hole_em
) {
7129 free_extent_map(hole_em
);
7131 free_extent_map(em
);
7132 return ERR_PTR(err
);
7137 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
7140 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7141 struct extent_map
*em
;
7142 struct btrfs_key ins
;
7146 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
7147 ret
= btrfs_reserve_extent(root
, len
, root
->sectorsize
, 0,
7148 alloc_hint
, &ins
, 1, 1);
7150 return ERR_PTR(ret
);
7152 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
7153 ins
.offset
, ins
.offset
, ins
.offset
, 0);
7155 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
7159 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
7160 ins
.offset
, ins
.offset
, 0);
7162 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
7163 free_extent_map(em
);
7164 return ERR_PTR(ret
);
7171 * returns 1 when the nocow is safe, < 1 on error, 0 if the
7172 * block must be cow'd
7174 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
7175 u64
*orig_start
, u64
*orig_block_len
,
7178 struct btrfs_trans_handle
*trans
;
7179 struct btrfs_path
*path
;
7181 struct extent_buffer
*leaf
;
7182 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7183 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7184 struct btrfs_file_extent_item
*fi
;
7185 struct btrfs_key key
;
7192 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
7194 path
= btrfs_alloc_path();
7198 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, btrfs_ino(inode
),
7203 slot
= path
->slots
[0];
7206 /* can't find the item, must cow */
7213 leaf
= path
->nodes
[0];
7214 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
7215 if (key
.objectid
!= btrfs_ino(inode
) ||
7216 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
7217 /* not our file or wrong item type, must cow */
7221 if (key
.offset
> offset
) {
7222 /* Wrong offset, must cow */
7226 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
7227 found_type
= btrfs_file_extent_type(leaf
, fi
);
7228 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
7229 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
7230 /* not a regular extent, must cow */
7234 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
7237 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
7238 if (extent_end
<= offset
)
7241 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
7242 if (disk_bytenr
== 0)
7245 if (btrfs_file_extent_compression(leaf
, fi
) ||
7246 btrfs_file_extent_encryption(leaf
, fi
) ||
7247 btrfs_file_extent_other_encoding(leaf
, fi
))
7250 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
7253 *orig_start
= key
.offset
- backref_offset
;
7254 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
7255 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
7258 if (btrfs_extent_readonly(root
, disk_bytenr
))
7261 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
7262 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
7265 range_end
= round_up(offset
+ num_bytes
, root
->sectorsize
) - 1;
7266 ret
= test_range_bit(io_tree
, offset
, range_end
,
7267 EXTENT_DELALLOC
, 0, NULL
);
7274 btrfs_release_path(path
);
7277 * look for other files referencing this extent, if we
7278 * find any we must cow
7280 trans
= btrfs_join_transaction(root
);
7281 if (IS_ERR(trans
)) {
7286 ret
= btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
7287 key
.offset
- backref_offset
, disk_bytenr
);
7288 btrfs_end_transaction(trans
, root
);
7295 * adjust disk_bytenr and num_bytes to cover just the bytes
7296 * in this extent we are about to write. If there
7297 * are any csums in that range we have to cow in order
7298 * to keep the csums correct
7300 disk_bytenr
+= backref_offset
;
7301 disk_bytenr
+= offset
- key
.offset
;
7302 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
7305 * all of the above have passed, it is safe to overwrite this extent
7311 btrfs_free_path(path
);
7315 bool btrfs_page_exists_in_range(struct inode
*inode
, loff_t start
, loff_t end
)
7317 struct radix_tree_root
*root
= &inode
->i_mapping
->page_tree
;
7319 void **pagep
= NULL
;
7320 struct page
*page
= NULL
;
7324 start_idx
= start
>> PAGE_CACHE_SHIFT
;
7327 * end is the last byte in the last page. end == start is legal
7329 end_idx
= end
>> PAGE_CACHE_SHIFT
;
7333 /* Most of the code in this while loop is lifted from
7334 * find_get_page. It's been modified to begin searching from a
7335 * page and return just the first page found in that range. If the
7336 * found idx is less than or equal to the end idx then we know that
7337 * a page exists. If no pages are found or if those pages are
7338 * outside of the range then we're fine (yay!) */
7339 while (page
== NULL
&&
7340 radix_tree_gang_lookup_slot(root
, &pagep
, NULL
, start_idx
, 1)) {
7341 page
= radix_tree_deref_slot(pagep
);
7342 if (unlikely(!page
))
7345 if (radix_tree_exception(page
)) {
7346 if (radix_tree_deref_retry(page
)) {
7351 * Otherwise, shmem/tmpfs must be storing a swap entry
7352 * here as an exceptional entry: so return it without
7353 * attempting to raise page count.
7356 break; /* TODO: Is this relevant for this use case? */
7359 if (!page_cache_get_speculative(page
)) {
7365 * Has the page moved?
7366 * This is part of the lockless pagecache protocol. See
7367 * include/linux/pagemap.h for details.
7369 if (unlikely(page
!= *pagep
)) {
7370 page_cache_release(page
);
7376 if (page
->index
<= end_idx
)
7378 page_cache_release(page
);
7385 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
7386 struct extent_state
**cached_state
, int writing
)
7388 struct btrfs_ordered_extent
*ordered
;
7392 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7395 * We're concerned with the entire range that we're going to be
7396 * doing DIO to, so we need to make sure theres no ordered
7397 * extents in this range.
7399 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
7400 lockend
- lockstart
+ 1);
7403 * We need to make sure there are no buffered pages in this
7404 * range either, we could have raced between the invalidate in
7405 * generic_file_direct_write and locking the extent. The
7406 * invalidate needs to happen so that reads after a write do not
7411 !btrfs_page_exists_in_range(inode
, lockstart
, lockend
)))
7414 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7415 cached_state
, GFP_NOFS
);
7418 btrfs_start_ordered_extent(inode
, ordered
, 1);
7419 btrfs_put_ordered_extent(ordered
);
7422 * We could trigger writeback for this range (and wait
7423 * for it to complete) and then invalidate the pages for
7424 * this range (through invalidate_inode_pages2_range()),
7425 * but that can lead us to a deadlock with a concurrent
7426 * call to readpages() (a buffered read or a defrag call
7427 * triggered a readahead) on a page lock due to an
7428 * ordered dio extent we created before but did not have
7429 * yet a corresponding bio submitted (whence it can not
7430 * complete), which makes readpages() wait for that
7431 * ordered extent to complete while holding a lock on
7444 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
7445 u64 len
, u64 orig_start
,
7446 u64 block_start
, u64 block_len
,
7447 u64 orig_block_len
, u64 ram_bytes
,
7450 struct extent_map_tree
*em_tree
;
7451 struct extent_map
*em
;
7452 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7455 em_tree
= &BTRFS_I(inode
)->extent_tree
;
7456 em
= alloc_extent_map();
7458 return ERR_PTR(-ENOMEM
);
7461 em
->orig_start
= orig_start
;
7462 em
->mod_start
= start
;
7465 em
->block_len
= block_len
;
7466 em
->block_start
= block_start
;
7467 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
7468 em
->orig_block_len
= orig_block_len
;
7469 em
->ram_bytes
= ram_bytes
;
7470 em
->generation
= -1;
7471 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
7472 if (type
== BTRFS_ORDERED_PREALLOC
)
7473 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
7476 btrfs_drop_extent_cache(inode
, em
->start
,
7477 em
->start
+ em
->len
- 1, 0);
7478 write_lock(&em_tree
->lock
);
7479 ret
= add_extent_mapping(em_tree
, em
, 1);
7480 write_unlock(&em_tree
->lock
);
7481 } while (ret
== -EEXIST
);
7484 free_extent_map(em
);
7485 return ERR_PTR(ret
);
7491 static void adjust_dio_outstanding_extents(struct inode
*inode
,
7492 struct btrfs_dio_data
*dio_data
,
7495 unsigned num_extents
;
7497 num_extents
= (unsigned) div64_u64(len
+ BTRFS_MAX_EXTENT_SIZE
- 1,
7498 BTRFS_MAX_EXTENT_SIZE
);
7500 * If we have an outstanding_extents count still set then we're
7501 * within our reservation, otherwise we need to adjust our inode
7502 * counter appropriately.
7504 if (dio_data
->outstanding_extents
) {
7505 dio_data
->outstanding_extents
-= num_extents
;
7507 spin_lock(&BTRFS_I(inode
)->lock
);
7508 BTRFS_I(inode
)->outstanding_extents
+= num_extents
;
7509 spin_unlock(&BTRFS_I(inode
)->lock
);
7513 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
7514 struct buffer_head
*bh_result
, int create
)
7516 struct extent_map
*em
;
7517 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7518 struct extent_state
*cached_state
= NULL
;
7519 struct btrfs_dio_data
*dio_data
= NULL
;
7520 u64 start
= iblock
<< inode
->i_blkbits
;
7521 u64 lockstart
, lockend
;
7522 u64 len
= bh_result
->b_size
;
7523 int unlock_bits
= EXTENT_LOCKED
;
7527 unlock_bits
|= EXTENT_DIRTY
;
7529 len
= min_t(u64
, len
, root
->sectorsize
);
7532 lockend
= start
+ len
- 1;
7534 if (current
->journal_info
) {
7536 * Need to pull our outstanding extents and set journal_info to NULL so
7537 * that anything that needs to check if there's a transction doesn't get
7540 dio_data
= current
->journal_info
;
7541 current
->journal_info
= NULL
;
7545 * If this errors out it's because we couldn't invalidate pagecache for
7546 * this range and we need to fallback to buffered.
7548 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
,
7554 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
7561 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7562 * io. INLINE is special, and we could probably kludge it in here, but
7563 * it's still buffered so for safety lets just fall back to the generic
7566 * For COMPRESSED we _have_ to read the entire extent in so we can
7567 * decompress it, so there will be buffering required no matter what we
7568 * do, so go ahead and fallback to buffered.
7570 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7571 * to buffered IO. Don't blame me, this is the price we pay for using
7574 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
7575 em
->block_start
== EXTENT_MAP_INLINE
) {
7576 free_extent_map(em
);
7581 /* Just a good old fashioned hole, return */
7582 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
7583 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
7584 free_extent_map(em
);
7589 * We don't allocate a new extent in the following cases
7591 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7593 * 2) The extent is marked as PREALLOC. We're good to go here and can
7594 * just use the extent.
7598 len
= min(len
, em
->len
- (start
- em
->start
));
7599 lockstart
= start
+ len
;
7603 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
7604 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
7605 em
->block_start
!= EXTENT_MAP_HOLE
)) {
7607 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
7609 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7610 type
= BTRFS_ORDERED_PREALLOC
;
7612 type
= BTRFS_ORDERED_NOCOW
;
7613 len
= min(len
, em
->len
- (start
- em
->start
));
7614 block_start
= em
->block_start
+ (start
- em
->start
);
7616 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
7617 &orig_block_len
, &ram_bytes
) == 1) {
7618 if (type
== BTRFS_ORDERED_PREALLOC
) {
7619 free_extent_map(em
);
7620 em
= create_pinned_em(inode
, start
, len
,
7631 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
7632 block_start
, len
, len
, type
);
7634 free_extent_map(em
);
7642 * this will cow the extent, reset the len in case we changed
7645 len
= bh_result
->b_size
;
7646 free_extent_map(em
);
7647 em
= btrfs_new_extent_direct(inode
, start
, len
);
7652 len
= min(len
, em
->len
- (start
- em
->start
));
7654 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
7656 bh_result
->b_size
= len
;
7657 bh_result
->b_bdev
= em
->bdev
;
7658 set_buffer_mapped(bh_result
);
7660 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7661 set_buffer_new(bh_result
);
7664 * Need to update the i_size under the extent lock so buffered
7665 * readers will get the updated i_size when we unlock.
7667 if (start
+ len
> i_size_read(inode
))
7668 i_size_write(inode
, start
+ len
);
7670 adjust_dio_outstanding_extents(inode
, dio_data
, len
);
7671 btrfs_free_reserved_data_space(inode
, start
, len
);
7672 WARN_ON(dio_data
->reserve
< len
);
7673 dio_data
->reserve
-= len
;
7674 dio_data
->unsubmitted_oe_range_end
= start
+ len
;
7675 current
->journal_info
= dio_data
;
7679 * In the case of write we need to clear and unlock the entire range,
7680 * in the case of read we need to unlock only the end area that we
7681 * aren't using if there is any left over space.
7683 if (lockstart
< lockend
) {
7684 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
7685 lockend
, unlock_bits
, 1, 0,
7686 &cached_state
, GFP_NOFS
);
7688 free_extent_state(cached_state
);
7691 free_extent_map(em
);
7696 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7697 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
7700 current
->journal_info
= dio_data
;
7702 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7703 * write less data then expected, so that we don't underflow our inode's
7704 * outstanding extents counter.
7706 if (create
&& dio_data
)
7707 adjust_dio_outstanding_extents(inode
, dio_data
, len
);
7712 static inline int submit_dio_repair_bio(struct inode
*inode
, struct bio
*bio
,
7713 int rw
, int mirror_num
)
7715 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7718 BUG_ON(rw
& REQ_WRITE
);
7722 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
,
7723 BTRFS_WQ_ENDIO_DIO_REPAIR
);
7727 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
7733 static int btrfs_check_dio_repairable(struct inode
*inode
,
7734 struct bio
*failed_bio
,
7735 struct io_failure_record
*failrec
,
7740 num_copies
= btrfs_num_copies(BTRFS_I(inode
)->root
->fs_info
,
7741 failrec
->logical
, failrec
->len
);
7742 if (num_copies
== 1) {
7744 * we only have a single copy of the data, so don't bother with
7745 * all the retry and error correction code that follows. no
7746 * matter what the error is, it is very likely to persist.
7748 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7749 num_copies
, failrec
->this_mirror
, failed_mirror
);
7753 failrec
->failed_mirror
= failed_mirror
;
7754 failrec
->this_mirror
++;
7755 if (failrec
->this_mirror
== failed_mirror
)
7756 failrec
->this_mirror
++;
7758 if (failrec
->this_mirror
> num_copies
) {
7759 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7760 num_copies
, failrec
->this_mirror
, failed_mirror
);
7767 static int dio_read_error(struct inode
*inode
, struct bio
*failed_bio
,
7768 struct page
*page
, u64 start
, u64 end
,
7769 int failed_mirror
, bio_end_io_t
*repair_endio
,
7772 struct io_failure_record
*failrec
;
7778 BUG_ON(failed_bio
->bi_rw
& REQ_WRITE
);
7780 ret
= btrfs_get_io_failure_record(inode
, start
, end
, &failrec
);
7784 ret
= btrfs_check_dio_repairable(inode
, failed_bio
, failrec
,
7787 free_io_failure(inode
, failrec
);
7791 if (failed_bio
->bi_vcnt
> 1)
7792 read_mode
= READ_SYNC
| REQ_FAILFAST_DEV
;
7794 read_mode
= READ_SYNC
;
7796 isector
= start
- btrfs_io_bio(failed_bio
)->logical
;
7797 isector
>>= inode
->i_sb
->s_blocksize_bits
;
7798 bio
= btrfs_create_repair_bio(inode
, failed_bio
, failrec
, page
,
7799 0, isector
, repair_endio
, repair_arg
);
7801 free_io_failure(inode
, failrec
);
7805 btrfs_debug(BTRFS_I(inode
)->root
->fs_info
,
7806 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7807 read_mode
, failrec
->this_mirror
, failrec
->in_validation
);
7809 ret
= submit_dio_repair_bio(inode
, bio
, read_mode
,
7810 failrec
->this_mirror
);
7812 free_io_failure(inode
, failrec
);
7819 struct btrfs_retry_complete
{
7820 struct completion done
;
7821 struct inode
*inode
;
7826 static void btrfs_retry_endio_nocsum(struct bio
*bio
)
7828 struct btrfs_retry_complete
*done
= bio
->bi_private
;
7829 struct bio_vec
*bvec
;
7836 bio_for_each_segment_all(bvec
, bio
, i
)
7837 clean_io_failure(done
->inode
, done
->start
, bvec
->bv_page
, 0);
7839 complete(&done
->done
);
7843 static int __btrfs_correct_data_nocsum(struct inode
*inode
,
7844 struct btrfs_io_bio
*io_bio
)
7846 struct bio_vec
*bvec
;
7847 struct btrfs_retry_complete done
;
7852 start
= io_bio
->logical
;
7855 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
7859 init_completion(&done
.done
);
7861 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
, start
,
7862 start
+ bvec
->bv_len
- 1,
7864 btrfs_retry_endio_nocsum
, &done
);
7868 wait_for_completion(&done
.done
);
7870 if (!done
.uptodate
) {
7871 /* We might have another mirror, so try again */
7875 start
+= bvec
->bv_len
;
7881 static void btrfs_retry_endio(struct bio
*bio
)
7883 struct btrfs_retry_complete
*done
= bio
->bi_private
;
7884 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
7885 struct bio_vec
*bvec
;
7894 bio_for_each_segment_all(bvec
, bio
, i
) {
7895 ret
= __readpage_endio_check(done
->inode
, io_bio
, i
,
7897 done
->start
, bvec
->bv_len
);
7899 clean_io_failure(done
->inode
, done
->start
,
7905 done
->uptodate
= uptodate
;
7907 complete(&done
->done
);
7911 static int __btrfs_subio_endio_read(struct inode
*inode
,
7912 struct btrfs_io_bio
*io_bio
, int err
)
7914 struct bio_vec
*bvec
;
7915 struct btrfs_retry_complete done
;
7922 start
= io_bio
->logical
;
7925 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
7926 ret
= __readpage_endio_check(inode
, io_bio
, i
, bvec
->bv_page
,
7927 0, start
, bvec
->bv_len
);
7933 init_completion(&done
.done
);
7935 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
, start
,
7936 start
+ bvec
->bv_len
- 1,
7938 btrfs_retry_endio
, &done
);
7944 wait_for_completion(&done
.done
);
7946 if (!done
.uptodate
) {
7947 /* We might have another mirror, so try again */
7951 offset
+= bvec
->bv_len
;
7952 start
+= bvec
->bv_len
;
7958 static int btrfs_subio_endio_read(struct inode
*inode
,
7959 struct btrfs_io_bio
*io_bio
, int err
)
7961 bool skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7965 return __btrfs_correct_data_nocsum(inode
, io_bio
);
7969 return __btrfs_subio_endio_read(inode
, io_bio
, err
);
7973 static void btrfs_endio_direct_read(struct bio
*bio
)
7975 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7976 struct inode
*inode
= dip
->inode
;
7977 struct bio
*dio_bio
;
7978 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
7979 int err
= bio
->bi_error
;
7981 if (dip
->flags
& BTRFS_DIO_ORIG_BIO_SUBMITTED
)
7982 err
= btrfs_subio_endio_read(inode
, io_bio
, err
);
7984 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
7985 dip
->logical_offset
+ dip
->bytes
- 1);
7986 dio_bio
= dip
->dio_bio
;
7990 dio_end_io(dio_bio
, bio
->bi_error
);
7993 io_bio
->end_io(io_bio
, err
);
7997 static void btrfs_endio_direct_write_update_ordered(struct inode
*inode
,
8002 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8003 struct btrfs_ordered_extent
*ordered
= NULL
;
8004 u64 ordered_offset
= offset
;
8005 u64 ordered_bytes
= bytes
;
8009 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
8016 btrfs_init_work(&ordered
->work
, btrfs_endio_write_helper
,
8017 finish_ordered_fn
, NULL
, NULL
);
8018 btrfs_queue_work(root
->fs_info
->endio_write_workers
,
8022 * our bio might span multiple ordered extents. If we haven't
8023 * completed the accounting for the whole dio, go back and try again
8025 if (ordered_offset
< offset
+ bytes
) {
8026 ordered_bytes
= offset
+ bytes
- ordered_offset
;
8032 static void btrfs_endio_direct_write(struct bio
*bio
)
8034 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8035 struct bio
*dio_bio
= dip
->dio_bio
;
8037 btrfs_endio_direct_write_update_ordered(dip
->inode
,
8038 dip
->logical_offset
,
8044 dio_end_io(dio_bio
, bio
->bi_error
);
8048 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
8049 struct bio
*bio
, int mirror_num
,
8050 unsigned long bio_flags
, u64 offset
)
8053 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8054 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
8055 BUG_ON(ret
); /* -ENOMEM */
8059 static void btrfs_end_dio_bio(struct bio
*bio
)
8061 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8062 int err
= bio
->bi_error
;
8065 btrfs_warn(BTRFS_I(dip
->inode
)->root
->fs_info
,
8066 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
8067 btrfs_ino(dip
->inode
), bio
->bi_rw
,
8068 (unsigned long long)bio
->bi_iter
.bi_sector
,
8069 bio
->bi_iter
.bi_size
, err
);
8071 if (dip
->subio_endio
)
8072 err
= dip
->subio_endio(dip
->inode
, btrfs_io_bio(bio
), err
);
8078 * before atomic variable goto zero, we must make sure
8079 * dip->errors is perceived to be set.
8081 smp_mb__before_atomic();
8084 /* if there are more bios still pending for this dio, just exit */
8085 if (!atomic_dec_and_test(&dip
->pending_bios
))
8089 bio_io_error(dip
->orig_bio
);
8091 dip
->dio_bio
->bi_error
= 0;
8092 bio_endio(dip
->orig_bio
);
8098 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
8099 u64 first_sector
, gfp_t gfp_flags
)
8102 bio
= btrfs_bio_alloc(bdev
, first_sector
, BIO_MAX_PAGES
, gfp_flags
);
8104 bio_associate_current(bio
);
8108 static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root
*root
,
8109 struct inode
*inode
,
8110 struct btrfs_dio_private
*dip
,
8114 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
8115 struct btrfs_io_bio
*orig_io_bio
= btrfs_io_bio(dip
->orig_bio
);
8119 * We load all the csum data we need when we submit
8120 * the first bio to reduce the csum tree search and
8123 if (dip
->logical_offset
== file_offset
) {
8124 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, dip
->orig_bio
,
8130 if (bio
== dip
->orig_bio
)
8133 file_offset
-= dip
->logical_offset
;
8134 file_offset
>>= inode
->i_sb
->s_blocksize_bits
;
8135 io_bio
->csum
= (u8
*)(((u32
*)orig_io_bio
->csum
) + file_offset
);
8140 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
8141 int rw
, u64 file_offset
, int skip_sum
,
8144 struct btrfs_dio_private
*dip
= bio
->bi_private
;
8145 int write
= rw
& REQ_WRITE
;
8146 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8150 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
8155 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
,
8156 BTRFS_WQ_ENDIO_DATA
);
8164 if (write
&& async_submit
) {
8165 ret
= btrfs_wq_submit_bio(root
->fs_info
,
8166 inode
, rw
, bio
, 0, 0,
8168 __btrfs_submit_bio_start_direct_io
,
8169 __btrfs_submit_bio_done
);
8173 * If we aren't doing async submit, calculate the csum of the
8176 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
8180 ret
= btrfs_lookup_and_bind_dio_csum(root
, inode
, dip
, bio
,
8186 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
8192 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
8195 struct inode
*inode
= dip
->inode
;
8196 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8198 struct bio
*orig_bio
= dip
->orig_bio
;
8199 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
8200 u64 start_sector
= orig_bio
->bi_iter
.bi_sector
;
8201 u64 file_offset
= dip
->logical_offset
;
8206 int async_submit
= 0;
8208 map_length
= orig_bio
->bi_iter
.bi_size
;
8209 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
8210 &map_length
, NULL
, 0);
8214 if (map_length
>= orig_bio
->bi_iter
.bi_size
) {
8216 dip
->flags
|= BTRFS_DIO_ORIG_BIO_SUBMITTED
;
8220 /* async crcs make it difficult to collect full stripe writes. */
8221 if (btrfs_get_alloc_profile(root
, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK
)
8226 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
8230 bio
->bi_private
= dip
;
8231 bio
->bi_end_io
= btrfs_end_dio_bio
;
8232 btrfs_io_bio(bio
)->logical
= file_offset
;
8233 atomic_inc(&dip
->pending_bios
);
8235 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
8236 if (map_length
< submit_len
+ bvec
->bv_len
||
8237 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
8238 bvec
->bv_offset
) < bvec
->bv_len
) {
8240 * inc the count before we submit the bio so
8241 * we know the end IO handler won't happen before
8242 * we inc the count. Otherwise, the dip might get freed
8243 * before we're done setting it up
8245 atomic_inc(&dip
->pending_bios
);
8246 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
8247 file_offset
, skip_sum
,
8251 atomic_dec(&dip
->pending_bios
);
8255 start_sector
+= submit_len
>> 9;
8256 file_offset
+= submit_len
;
8261 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
8262 start_sector
, GFP_NOFS
);
8265 bio
->bi_private
= dip
;
8266 bio
->bi_end_io
= btrfs_end_dio_bio
;
8267 btrfs_io_bio(bio
)->logical
= file_offset
;
8269 map_length
= orig_bio
->bi_iter
.bi_size
;
8270 ret
= btrfs_map_block(root
->fs_info
, rw
,
8272 &map_length
, NULL
, 0);
8278 submit_len
+= bvec
->bv_len
;
8285 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
8294 * before atomic variable goto zero, we must
8295 * make sure dip->errors is perceived to be set.
8297 smp_mb__before_atomic();
8298 if (atomic_dec_and_test(&dip
->pending_bios
))
8299 bio_io_error(dip
->orig_bio
);
8301 /* bio_end_io() will handle error, so we needn't return it */
8305 static void btrfs_submit_direct(int rw
, struct bio
*dio_bio
,
8306 struct inode
*inode
, loff_t file_offset
)
8308 struct btrfs_dio_private
*dip
= NULL
;
8309 struct bio
*io_bio
= NULL
;
8310 struct btrfs_io_bio
*btrfs_bio
;
8312 int write
= rw
& REQ_WRITE
;
8315 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
8317 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
8323 dip
= kzalloc(sizeof(*dip
), GFP_NOFS
);
8329 dip
->private = dio_bio
->bi_private
;
8331 dip
->logical_offset
= file_offset
;
8332 dip
->bytes
= dio_bio
->bi_iter
.bi_size
;
8333 dip
->disk_bytenr
= (u64
)dio_bio
->bi_iter
.bi_sector
<< 9;
8334 io_bio
->bi_private
= dip
;
8335 dip
->orig_bio
= io_bio
;
8336 dip
->dio_bio
= dio_bio
;
8337 atomic_set(&dip
->pending_bios
, 0);
8338 btrfs_bio
= btrfs_io_bio(io_bio
);
8339 btrfs_bio
->logical
= file_offset
;
8342 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
8344 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
8345 dip
->subio_endio
= btrfs_subio_endio_read
;
8349 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8350 * even if we fail to submit a bio, because in such case we do the
8351 * corresponding error handling below and it must not be done a second
8352 * time by btrfs_direct_IO().
8355 struct btrfs_dio_data
*dio_data
= current
->journal_info
;
8357 dio_data
->unsubmitted_oe_range_end
= dip
->logical_offset
+
8359 dio_data
->unsubmitted_oe_range_start
=
8360 dio_data
->unsubmitted_oe_range_end
;
8363 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
8367 if (btrfs_bio
->end_io
)
8368 btrfs_bio
->end_io(btrfs_bio
, ret
);
8372 * If we arrived here it means either we failed to submit the dip
8373 * or we either failed to clone the dio_bio or failed to allocate the
8374 * dip. If we cloned the dio_bio and allocated the dip, we can just
8375 * call bio_endio against our io_bio so that we get proper resource
8376 * cleanup if we fail to submit the dip, otherwise, we must do the
8377 * same as btrfs_endio_direct_[write|read] because we can't call these
8378 * callbacks - they require an allocated dip and a clone of dio_bio.
8380 if (io_bio
&& dip
) {
8381 io_bio
->bi_error
= -EIO
;
8384 * The end io callbacks free our dip, do the final put on io_bio
8385 * and all the cleanup and final put for dio_bio (through
8392 btrfs_endio_direct_write_update_ordered(inode
,
8394 dio_bio
->bi_iter
.bi_size
,
8397 unlock_extent(&BTRFS_I(inode
)->io_tree
, file_offset
,
8398 file_offset
+ dio_bio
->bi_iter
.bi_size
- 1);
8400 dio_bio
->bi_error
= -EIO
;
8402 * Releases and cleans up our dio_bio, no need to bio_put()
8403 * nor bio_endio()/bio_io_error() against dio_bio.
8405 dio_end_io(dio_bio
, ret
);
8412 static ssize_t
check_direct_IO(struct btrfs_root
*root
, struct kiocb
*iocb
,
8413 const struct iov_iter
*iter
, loff_t offset
)
8417 unsigned blocksize_mask
= root
->sectorsize
- 1;
8418 ssize_t retval
= -EINVAL
;
8420 if (offset
& blocksize_mask
)
8423 if (iov_iter_alignment(iter
) & blocksize_mask
)
8426 /* If this is a write we don't need to check anymore */
8427 if (iov_iter_rw(iter
) == WRITE
)
8430 * Check to make sure we don't have duplicate iov_base's in this
8431 * iovec, if so return EINVAL, otherwise we'll get csum errors
8432 * when reading back.
8434 for (seg
= 0; seg
< iter
->nr_segs
; seg
++) {
8435 for (i
= seg
+ 1; i
< iter
->nr_segs
; i
++) {
8436 if (iter
->iov
[seg
].iov_base
== iter
->iov
[i
].iov_base
)
8445 static ssize_t
btrfs_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
,
8448 struct file
*file
= iocb
->ki_filp
;
8449 struct inode
*inode
= file
->f_mapping
->host
;
8450 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8451 struct btrfs_dio_data dio_data
= { 0 };
8455 bool relock
= false;
8458 if (check_direct_IO(BTRFS_I(inode
)->root
, iocb
, iter
, offset
))
8461 inode_dio_begin(inode
);
8462 smp_mb__after_atomic();
8465 * The generic stuff only does filemap_write_and_wait_range, which
8466 * isn't enough if we've written compressed pages to this area, so
8467 * we need to flush the dirty pages again to make absolutely sure
8468 * that any outstanding dirty pages are on disk.
8470 count
= iov_iter_count(iter
);
8471 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
8472 &BTRFS_I(inode
)->runtime_flags
))
8473 filemap_fdatawrite_range(inode
->i_mapping
, offset
,
8474 offset
+ count
- 1);
8476 if (iov_iter_rw(iter
) == WRITE
) {
8478 * If the write DIO is beyond the EOF, we need update
8479 * the isize, but it is protected by i_mutex. So we can
8480 * not unlock the i_mutex at this case.
8482 if (offset
+ count
<= inode
->i_size
) {
8483 mutex_unlock(&inode
->i_mutex
);
8486 ret
= btrfs_delalloc_reserve_space(inode
, offset
, count
);
8489 dio_data
.outstanding_extents
= div64_u64(count
+
8490 BTRFS_MAX_EXTENT_SIZE
- 1,
8491 BTRFS_MAX_EXTENT_SIZE
);
8494 * We need to know how many extents we reserved so that we can
8495 * do the accounting properly if we go over the number we
8496 * originally calculated. Abuse current->journal_info for this.
8498 dio_data
.reserve
= round_up(count
, root
->sectorsize
);
8499 dio_data
.unsubmitted_oe_range_start
= (u64
)offset
;
8500 dio_data
.unsubmitted_oe_range_end
= (u64
)offset
;
8501 current
->journal_info
= &dio_data
;
8502 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
8503 &BTRFS_I(inode
)->runtime_flags
)) {
8504 inode_dio_end(inode
);
8505 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
8509 ret
= __blockdev_direct_IO(iocb
, inode
,
8510 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
8511 iter
, offset
, btrfs_get_blocks_direct
, NULL
,
8512 btrfs_submit_direct
, flags
);
8513 if (iov_iter_rw(iter
) == WRITE
) {
8514 current
->journal_info
= NULL
;
8515 if (ret
< 0 && ret
!= -EIOCBQUEUED
) {
8516 if (dio_data
.reserve
)
8517 btrfs_delalloc_release_space(inode
, offset
,
8520 * On error we might have left some ordered extents
8521 * without submitting corresponding bios for them, so
8522 * cleanup them up to avoid other tasks getting them
8523 * and waiting for them to complete forever.
8525 if (dio_data
.unsubmitted_oe_range_start
<
8526 dio_data
.unsubmitted_oe_range_end
)
8527 btrfs_endio_direct_write_update_ordered(inode
,
8528 dio_data
.unsubmitted_oe_range_start
,
8529 dio_data
.unsubmitted_oe_range_end
-
8530 dio_data
.unsubmitted_oe_range_start
,
8532 } else if (ret
>= 0 && (size_t)ret
< count
)
8533 btrfs_delalloc_release_space(inode
, offset
,
8534 count
- (size_t)ret
);
8538 inode_dio_end(inode
);
8540 mutex_lock(&inode
->i_mutex
);
8545 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8547 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
8548 __u64 start
, __u64 len
)
8552 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
8556 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
8559 int btrfs_readpage(struct file
*file
, struct page
*page
)
8561 struct extent_io_tree
*tree
;
8562 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8563 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
8566 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
8568 struct extent_io_tree
*tree
;
8571 if (current
->flags
& PF_MEMALLOC
) {
8572 redirty_page_for_writepage(wbc
, page
);
8576 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8577 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
8580 static int btrfs_writepages(struct address_space
*mapping
,
8581 struct writeback_control
*wbc
)
8583 struct extent_io_tree
*tree
;
8585 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8586 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
8590 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
8591 struct list_head
*pages
, unsigned nr_pages
)
8593 struct extent_io_tree
*tree
;
8594 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8595 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
8598 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8600 struct extent_io_tree
*tree
;
8601 struct extent_map_tree
*map
;
8604 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8605 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
8606 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
8608 ClearPagePrivate(page
);
8609 set_page_private(page
, 0);
8610 page_cache_release(page
);
8615 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8617 if (PageWriteback(page
) || PageDirty(page
))
8619 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
8622 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
8623 unsigned int length
)
8625 struct inode
*inode
= page
->mapping
->host
;
8626 struct extent_io_tree
*tree
;
8627 struct btrfs_ordered_extent
*ordered
;
8628 struct extent_state
*cached_state
= NULL
;
8629 u64 page_start
= page_offset(page
);
8630 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
8631 int inode_evicting
= inode
->i_state
& I_FREEING
;
8634 * we have the page locked, so new writeback can't start,
8635 * and the dirty bit won't be cleared while we are here.
8637 * Wait for IO on this page so that we can safely clear
8638 * the PagePrivate2 bit and do ordered accounting
8640 wait_on_page_writeback(page
);
8642 tree
= &BTRFS_I(inode
)->io_tree
;
8644 btrfs_releasepage(page
, GFP_NOFS
);
8648 if (!inode_evicting
)
8649 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
8650 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
8653 * IO on this page will never be started, so we need
8654 * to account for any ordered extents now
8656 if (!inode_evicting
)
8657 clear_extent_bit(tree
, page_start
, page_end
,
8658 EXTENT_DIRTY
| EXTENT_DELALLOC
|
8659 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
8660 EXTENT_DEFRAG
, 1, 0, &cached_state
,
8663 * whoever cleared the private bit is responsible
8664 * for the finish_ordered_io
8666 if (TestClearPagePrivate2(page
)) {
8667 struct btrfs_ordered_inode_tree
*tree
;
8670 tree
= &BTRFS_I(inode
)->ordered_tree
;
8672 spin_lock_irq(&tree
->lock
);
8673 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
8674 new_len
= page_start
- ordered
->file_offset
;
8675 if (new_len
< ordered
->truncated_len
)
8676 ordered
->truncated_len
= new_len
;
8677 spin_unlock_irq(&tree
->lock
);
8679 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
8681 PAGE_CACHE_SIZE
, 1))
8682 btrfs_finish_ordered_io(ordered
);
8684 btrfs_put_ordered_extent(ordered
);
8685 if (!inode_evicting
) {
8686 cached_state
= NULL
;
8687 lock_extent_bits(tree
, page_start
, page_end
, 0,
8693 * Qgroup reserved space handler
8694 * Page here will be either
8695 * 1) Already written to disk
8696 * In this case, its reserved space is released from data rsv map
8697 * and will be freed by delayed_ref handler finally.
8698 * So even we call qgroup_free_data(), it won't decrease reserved
8700 * 2) Not written to disk
8701 * This means the reserved space should be freed here.
8703 btrfs_qgroup_free_data(inode
, page_start
, PAGE_CACHE_SIZE
);
8704 if (!inode_evicting
) {
8705 clear_extent_bit(tree
, page_start
, page_end
,
8706 EXTENT_LOCKED
| EXTENT_DIRTY
|
8707 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
8708 EXTENT_DEFRAG
, 1, 1,
8709 &cached_state
, GFP_NOFS
);
8711 __btrfs_releasepage(page
, GFP_NOFS
);
8714 ClearPageChecked(page
);
8715 if (PagePrivate(page
)) {
8716 ClearPagePrivate(page
);
8717 set_page_private(page
, 0);
8718 page_cache_release(page
);
8723 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8724 * called from a page fault handler when a page is first dirtied. Hence we must
8725 * be careful to check for EOF conditions here. We set the page up correctly
8726 * for a written page which means we get ENOSPC checking when writing into
8727 * holes and correct delalloc and unwritten extent mapping on filesystems that
8728 * support these features.
8730 * We are not allowed to take the i_mutex here so we have to play games to
8731 * protect against truncate races as the page could now be beyond EOF. Because
8732 * vmtruncate() writes the inode size before removing pages, once we have the
8733 * page lock we can determine safely if the page is beyond EOF. If it is not
8734 * beyond EOF, then the page is guaranteed safe against truncation until we
8737 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
8739 struct page
*page
= vmf
->page
;
8740 struct inode
*inode
= file_inode(vma
->vm_file
);
8741 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8742 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
8743 struct btrfs_ordered_extent
*ordered
;
8744 struct extent_state
*cached_state
= NULL
;
8746 unsigned long zero_start
;
8753 sb_start_pagefault(inode
->i_sb
);
8754 page_start
= page_offset(page
);
8755 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
8757 ret
= btrfs_delalloc_reserve_space(inode
, page_start
,
8760 ret
= file_update_time(vma
->vm_file
);
8766 else /* -ENOSPC, -EIO, etc */
8767 ret
= VM_FAULT_SIGBUS
;
8773 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
8776 size
= i_size_read(inode
);
8778 if ((page
->mapping
!= inode
->i_mapping
) ||
8779 (page_start
>= size
)) {
8780 /* page got truncated out from underneath us */
8783 wait_on_page_writeback(page
);
8785 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
8786 set_page_extent_mapped(page
);
8789 * we can't set the delalloc bits if there are pending ordered
8790 * extents. Drop our locks and wait for them to finish
8792 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
8794 unlock_extent_cached(io_tree
, page_start
, page_end
,
8795 &cached_state
, GFP_NOFS
);
8797 btrfs_start_ordered_extent(inode
, ordered
, 1);
8798 btrfs_put_ordered_extent(ordered
);
8803 * XXX - page_mkwrite gets called every time the page is dirtied, even
8804 * if it was already dirty, so for space accounting reasons we need to
8805 * clear any delalloc bits for the range we are fixing to save. There
8806 * is probably a better way to do this, but for now keep consistent with
8807 * prepare_pages in the normal write path.
8809 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
8810 EXTENT_DIRTY
| EXTENT_DELALLOC
|
8811 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
8812 0, 0, &cached_state
, GFP_NOFS
);
8814 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
8817 unlock_extent_cached(io_tree
, page_start
, page_end
,
8818 &cached_state
, GFP_NOFS
);
8819 ret
= VM_FAULT_SIGBUS
;
8824 /* page is wholly or partially inside EOF */
8825 if (page_start
+ PAGE_CACHE_SIZE
> size
)
8826 zero_start
= size
& ~PAGE_CACHE_MASK
;
8828 zero_start
= PAGE_CACHE_SIZE
;
8830 if (zero_start
!= PAGE_CACHE_SIZE
) {
8832 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
8833 flush_dcache_page(page
);
8836 ClearPageChecked(page
);
8837 set_page_dirty(page
);
8838 SetPageUptodate(page
);
8840 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
8841 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
8842 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
8844 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
8848 sb_end_pagefault(inode
->i_sb
);
8849 return VM_FAULT_LOCKED
;
8853 btrfs_delalloc_release_space(inode
, page_start
, PAGE_CACHE_SIZE
);
8855 sb_end_pagefault(inode
->i_sb
);
8859 static int btrfs_truncate(struct inode
*inode
)
8861 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8862 struct btrfs_block_rsv
*rsv
;
8865 struct btrfs_trans_handle
*trans
;
8866 u64 mask
= root
->sectorsize
- 1;
8867 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
8869 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
8875 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8876 * 3 things going on here
8878 * 1) We need to reserve space for our orphan item and the space to
8879 * delete our orphan item. Lord knows we don't want to have a dangling
8880 * orphan item because we didn't reserve space to remove it.
8882 * 2) We need to reserve space to update our inode.
8884 * 3) We need to have something to cache all the space that is going to
8885 * be free'd up by the truncate operation, but also have some slack
8886 * space reserved in case it uses space during the truncate (thank you
8887 * very much snapshotting).
8889 * And we need these to all be seperate. The fact is we can use alot of
8890 * space doing the truncate, and we have no earthly idea how much space
8891 * we will use, so we need the truncate reservation to be seperate so it
8892 * doesn't end up using space reserved for updating the inode or
8893 * removing the orphan item. We also need to be able to stop the
8894 * transaction and start a new one, which means we need to be able to
8895 * update the inode several times, and we have no idea of knowing how
8896 * many times that will be, so we can't just reserve 1 item for the
8897 * entirety of the opration, so that has to be done seperately as well.
8898 * Then there is the orphan item, which does indeed need to be held on
8899 * to for the whole operation, and we need nobody to touch this reserved
8900 * space except the orphan code.
8902 * So that leaves us with
8904 * 1) root->orphan_block_rsv - for the orphan deletion.
8905 * 2) rsv - for the truncate reservation, which we will steal from the
8906 * transaction reservation.
8907 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8908 * updating the inode.
8910 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
8913 rsv
->size
= min_size
;
8917 * 1 for the truncate slack space
8918 * 1 for updating the inode.
8920 trans
= btrfs_start_transaction(root
, 2);
8921 if (IS_ERR(trans
)) {
8922 err
= PTR_ERR(trans
);
8926 /* Migrate the slack space for the truncate to our reserve */
8927 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
8932 * So if we truncate and then write and fsync we normally would just
8933 * write the extents that changed, which is a problem if we need to
8934 * first truncate that entire inode. So set this flag so we write out
8935 * all of the extents in the inode to the sync log so we're completely
8938 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
8939 trans
->block_rsv
= rsv
;
8942 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
8944 BTRFS_EXTENT_DATA_KEY
);
8945 if (ret
!= -ENOSPC
&& ret
!= -EAGAIN
) {
8950 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
8951 ret
= btrfs_update_inode(trans
, root
, inode
);
8957 btrfs_end_transaction(trans
, root
);
8958 btrfs_btree_balance_dirty(root
);
8960 trans
= btrfs_start_transaction(root
, 2);
8961 if (IS_ERR(trans
)) {
8962 ret
= err
= PTR_ERR(trans
);
8967 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
8969 BUG_ON(ret
); /* shouldn't happen */
8970 trans
->block_rsv
= rsv
;
8973 if (ret
== 0 && inode
->i_nlink
> 0) {
8974 trans
->block_rsv
= root
->orphan_block_rsv
;
8975 ret
= btrfs_orphan_del(trans
, inode
);
8981 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
8982 ret
= btrfs_update_inode(trans
, root
, inode
);
8986 ret
= btrfs_end_transaction(trans
, root
);
8987 btrfs_btree_balance_dirty(root
);
8991 btrfs_free_block_rsv(root
, rsv
);
9000 * create a new subvolume directory/inode (helper for the ioctl).
9002 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
9003 struct btrfs_root
*new_root
,
9004 struct btrfs_root
*parent_root
,
9007 struct inode
*inode
;
9011 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
9012 new_dirid
, new_dirid
,
9013 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
9016 return PTR_ERR(inode
);
9017 inode
->i_op
= &btrfs_dir_inode_operations
;
9018 inode
->i_fop
= &btrfs_dir_file_operations
;
9020 set_nlink(inode
, 1);
9021 btrfs_i_size_write(inode
, 0);
9022 unlock_new_inode(inode
);
9024 err
= btrfs_subvol_inherit_props(trans
, new_root
, parent_root
);
9026 btrfs_err(new_root
->fs_info
,
9027 "error inheriting subvolume %llu properties: %d",
9028 new_root
->root_key
.objectid
, err
);
9030 err
= btrfs_update_inode(trans
, new_root
, inode
);
9036 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
9038 struct btrfs_inode
*ei
;
9039 struct inode
*inode
;
9041 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
9048 ei
->last_sub_trans
= 0;
9049 ei
->logged_trans
= 0;
9050 ei
->delalloc_bytes
= 0;
9051 ei
->defrag_bytes
= 0;
9052 ei
->disk_i_size
= 0;
9055 ei
->index_cnt
= (u64
)-1;
9057 ei
->last_unlink_trans
= 0;
9058 ei
->last_log_commit
= 0;
9060 spin_lock_init(&ei
->lock
);
9061 ei
->outstanding_extents
= 0;
9062 ei
->reserved_extents
= 0;
9064 ei
->runtime_flags
= 0;
9065 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
9067 ei
->delayed_node
= NULL
;
9069 ei
->i_otime
.tv_sec
= 0;
9070 ei
->i_otime
.tv_nsec
= 0;
9072 inode
= &ei
->vfs_inode
;
9073 extent_map_tree_init(&ei
->extent_tree
);
9074 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
9075 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
9076 ei
->io_tree
.track_uptodate
= 1;
9077 ei
->io_failure_tree
.track_uptodate
= 1;
9078 atomic_set(&ei
->sync_writers
, 0);
9079 mutex_init(&ei
->log_mutex
);
9080 mutex_init(&ei
->delalloc_mutex
);
9081 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
9082 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
9083 RB_CLEAR_NODE(&ei
->rb_node
);
9088 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9089 void btrfs_test_destroy_inode(struct inode
*inode
)
9091 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
9092 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9096 static void btrfs_i_callback(struct rcu_head
*head
)
9098 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
9099 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
9102 void btrfs_destroy_inode(struct inode
*inode
)
9104 struct btrfs_ordered_extent
*ordered
;
9105 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9107 WARN_ON(!hlist_empty(&inode
->i_dentry
));
9108 WARN_ON(inode
->i_data
.nrpages
);
9109 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
9110 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
9111 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
9112 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
9113 WARN_ON(BTRFS_I(inode
)->defrag_bytes
);
9116 * This can happen where we create an inode, but somebody else also
9117 * created the same inode and we need to destroy the one we already
9123 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
9124 &BTRFS_I(inode
)->runtime_flags
)) {
9125 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
9127 atomic_dec(&root
->orphan_inodes
);
9131 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
9135 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
9136 ordered
->file_offset
, ordered
->len
);
9137 btrfs_remove_ordered_extent(inode
, ordered
);
9138 btrfs_put_ordered_extent(ordered
);
9139 btrfs_put_ordered_extent(ordered
);
9142 btrfs_qgroup_check_reserved_leak(inode
);
9143 inode_tree_del(inode
);
9144 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
9146 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
9149 int btrfs_drop_inode(struct inode
*inode
)
9151 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9156 /* the snap/subvol tree is on deleting */
9157 if (btrfs_root_refs(&root
->root_item
) == 0)
9160 return generic_drop_inode(inode
);
9163 static void init_once(void *foo
)
9165 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
9167 inode_init_once(&ei
->vfs_inode
);
9170 void btrfs_destroy_cachep(void)
9173 * Make sure all delayed rcu free inodes are flushed before we
9177 if (btrfs_inode_cachep
)
9178 kmem_cache_destroy(btrfs_inode_cachep
);
9179 if (btrfs_trans_handle_cachep
)
9180 kmem_cache_destroy(btrfs_trans_handle_cachep
);
9181 if (btrfs_transaction_cachep
)
9182 kmem_cache_destroy(btrfs_transaction_cachep
);
9183 if (btrfs_path_cachep
)
9184 kmem_cache_destroy(btrfs_path_cachep
);
9185 if (btrfs_free_space_cachep
)
9186 kmem_cache_destroy(btrfs_free_space_cachep
);
9187 if (btrfs_delalloc_work_cachep
)
9188 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
9191 int btrfs_init_cachep(void)
9193 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
9194 sizeof(struct btrfs_inode
), 0,
9195 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
9196 if (!btrfs_inode_cachep
)
9199 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
9200 sizeof(struct btrfs_trans_handle
), 0,
9201 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
9202 if (!btrfs_trans_handle_cachep
)
9205 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
9206 sizeof(struct btrfs_transaction
), 0,
9207 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
9208 if (!btrfs_transaction_cachep
)
9211 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
9212 sizeof(struct btrfs_path
), 0,
9213 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
9214 if (!btrfs_path_cachep
)
9217 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
9218 sizeof(struct btrfs_free_space
), 0,
9219 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
9220 if (!btrfs_free_space_cachep
)
9223 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
9224 sizeof(struct btrfs_delalloc_work
), 0,
9225 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
9227 if (!btrfs_delalloc_work_cachep
)
9232 btrfs_destroy_cachep();
9236 static int btrfs_getattr(struct vfsmount
*mnt
,
9237 struct dentry
*dentry
, struct kstat
*stat
)
9240 struct inode
*inode
= d_inode(dentry
);
9241 u32 blocksize
= inode
->i_sb
->s_blocksize
;
9243 generic_fillattr(inode
, stat
);
9244 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
9245 stat
->blksize
= PAGE_CACHE_SIZE
;
9247 spin_lock(&BTRFS_I(inode
)->lock
);
9248 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
9249 spin_unlock(&BTRFS_I(inode
)->lock
);
9250 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
9251 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
9255 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
9256 struct inode
*new_dir
, struct dentry
*new_dentry
)
9258 struct btrfs_trans_handle
*trans
;
9259 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
9260 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
9261 struct inode
*new_inode
= d_inode(new_dentry
);
9262 struct inode
*old_inode
= d_inode(old_dentry
);
9263 struct timespec ctime
= CURRENT_TIME
;
9267 u64 old_ino
= btrfs_ino(old_inode
);
9269 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
9272 /* we only allow rename subvolume link between subvolumes */
9273 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
9276 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
9277 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
9280 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
9281 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
9285 /* check for collisions, even if the name isn't there */
9286 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
9287 new_dentry
->d_name
.name
,
9288 new_dentry
->d_name
.len
);
9291 if (ret
== -EEXIST
) {
9293 * eexist without a new_inode */
9294 if (WARN_ON(!new_inode
)) {
9298 /* maybe -EOVERFLOW */
9305 * we're using rename to replace one file with another. Start IO on it
9306 * now so we don't add too much work to the end of the transaction
9308 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
)
9309 filemap_flush(old_inode
->i_mapping
);
9311 /* close the racy window with snapshot create/destroy ioctl */
9312 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9313 down_read(&root
->fs_info
->subvol_sem
);
9315 * We want to reserve the absolute worst case amount of items. So if
9316 * both inodes are subvols and we need to unlink them then that would
9317 * require 4 item modifications, but if they are both normal inodes it
9318 * would require 5 item modifications, so we'll assume their normal
9319 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9320 * should cover the worst case number of items we'll modify.
9322 trans
= btrfs_start_transaction(root
, 11);
9323 if (IS_ERR(trans
)) {
9324 ret
= PTR_ERR(trans
);
9329 btrfs_record_root_in_trans(trans
, dest
);
9331 ret
= btrfs_set_inode_index(new_dir
, &index
);
9335 BTRFS_I(old_inode
)->dir_index
= 0ULL;
9336 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9337 /* force full log commit if subvolume involved. */
9338 btrfs_set_log_full_commit(root
->fs_info
, trans
);
9340 ret
= btrfs_insert_inode_ref(trans
, dest
,
9341 new_dentry
->d_name
.name
,
9342 new_dentry
->d_name
.len
,
9344 btrfs_ino(new_dir
), index
);
9348 * this is an ugly little race, but the rename is required
9349 * to make sure that if we crash, the inode is either at the
9350 * old name or the new one. pinning the log transaction lets
9351 * us make sure we don't allow a log commit to come in after
9352 * we unlink the name but before we add the new name back in.
9354 btrfs_pin_log_trans(root
);
9357 inode_inc_iversion(old_dir
);
9358 inode_inc_iversion(new_dir
);
9359 inode_inc_iversion(old_inode
);
9360 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
9361 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
9362 old_inode
->i_ctime
= ctime
;
9364 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
9365 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
9367 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
9368 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
9369 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
9370 old_dentry
->d_name
.name
,
9371 old_dentry
->d_name
.len
);
9373 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
9374 d_inode(old_dentry
),
9375 old_dentry
->d_name
.name
,
9376 old_dentry
->d_name
.len
);
9378 ret
= btrfs_update_inode(trans
, root
, old_inode
);
9381 btrfs_abort_transaction(trans
, root
, ret
);
9386 inode_inc_iversion(new_inode
);
9387 new_inode
->i_ctime
= CURRENT_TIME
;
9388 if (unlikely(btrfs_ino(new_inode
) ==
9389 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
9390 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
9391 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
9393 new_dentry
->d_name
.name
,
9394 new_dentry
->d_name
.len
);
9395 BUG_ON(new_inode
->i_nlink
== 0);
9397 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
9398 d_inode(new_dentry
),
9399 new_dentry
->d_name
.name
,
9400 new_dentry
->d_name
.len
);
9402 if (!ret
&& new_inode
->i_nlink
== 0)
9403 ret
= btrfs_orphan_add(trans
, d_inode(new_dentry
));
9405 btrfs_abort_transaction(trans
, root
, ret
);
9410 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
9411 new_dentry
->d_name
.name
,
9412 new_dentry
->d_name
.len
, 0, index
);
9414 btrfs_abort_transaction(trans
, root
, ret
);
9418 if (old_inode
->i_nlink
== 1)
9419 BTRFS_I(old_inode
)->dir_index
= index
;
9421 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
9422 struct dentry
*parent
= new_dentry
->d_parent
;
9423 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
9424 btrfs_end_log_trans(root
);
9427 btrfs_end_transaction(trans
, root
);
9429 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9430 up_read(&root
->fs_info
->subvol_sem
);
9435 static int btrfs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
9436 struct inode
*new_dir
, struct dentry
*new_dentry
,
9439 if (flags
& ~RENAME_NOREPLACE
)
9442 return btrfs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
9445 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
9447 struct btrfs_delalloc_work
*delalloc_work
;
9448 struct inode
*inode
;
9450 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
9452 inode
= delalloc_work
->inode
;
9453 if (delalloc_work
->wait
) {
9454 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
9456 filemap_flush(inode
->i_mapping
);
9457 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
9458 &BTRFS_I(inode
)->runtime_flags
))
9459 filemap_flush(inode
->i_mapping
);
9462 if (delalloc_work
->delay_iput
)
9463 btrfs_add_delayed_iput(inode
);
9466 complete(&delalloc_work
->completion
);
9469 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
9470 int wait
, int delay_iput
)
9472 struct btrfs_delalloc_work
*work
;
9474 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
9478 init_completion(&work
->completion
);
9479 INIT_LIST_HEAD(&work
->list
);
9480 work
->inode
= inode
;
9482 work
->delay_iput
= delay_iput
;
9483 WARN_ON_ONCE(!inode
);
9484 btrfs_init_work(&work
->work
, btrfs_flush_delalloc_helper
,
9485 btrfs_run_delalloc_work
, NULL
, NULL
);
9490 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
9492 wait_for_completion(&work
->completion
);
9493 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
9497 * some fairly slow code that needs optimization. This walks the list
9498 * of all the inodes with pending delalloc and forces them to disk.
9500 static int __start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
,
9503 struct btrfs_inode
*binode
;
9504 struct inode
*inode
;
9505 struct btrfs_delalloc_work
*work
, *next
;
9506 struct list_head works
;
9507 struct list_head splice
;
9510 INIT_LIST_HEAD(&works
);
9511 INIT_LIST_HEAD(&splice
);
9513 mutex_lock(&root
->delalloc_mutex
);
9514 spin_lock(&root
->delalloc_lock
);
9515 list_splice_init(&root
->delalloc_inodes
, &splice
);
9516 while (!list_empty(&splice
)) {
9517 binode
= list_entry(splice
.next
, struct btrfs_inode
,
9520 list_move_tail(&binode
->delalloc_inodes
,
9521 &root
->delalloc_inodes
);
9522 inode
= igrab(&binode
->vfs_inode
);
9524 cond_resched_lock(&root
->delalloc_lock
);
9527 spin_unlock(&root
->delalloc_lock
);
9529 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
9532 btrfs_add_delayed_iput(inode
);
9538 list_add_tail(&work
->list
, &works
);
9539 btrfs_queue_work(root
->fs_info
->flush_workers
,
9542 if (nr
!= -1 && ret
>= nr
)
9545 spin_lock(&root
->delalloc_lock
);
9547 spin_unlock(&root
->delalloc_lock
);
9550 list_for_each_entry_safe(work
, next
, &works
, list
) {
9551 list_del_init(&work
->list
);
9552 btrfs_wait_and_free_delalloc_work(work
);
9555 if (!list_empty_careful(&splice
)) {
9556 spin_lock(&root
->delalloc_lock
);
9557 list_splice_tail(&splice
, &root
->delalloc_inodes
);
9558 spin_unlock(&root
->delalloc_lock
);
9560 mutex_unlock(&root
->delalloc_mutex
);
9564 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
9568 if (test_bit(BTRFS_FS_STATE_ERROR
, &root
->fs_info
->fs_state
))
9571 ret
= __start_delalloc_inodes(root
, delay_iput
, -1);
9575 * the filemap_flush will queue IO into the worker threads, but
9576 * we have to make sure the IO is actually started and that
9577 * ordered extents get created before we return
9579 atomic_inc(&root
->fs_info
->async_submit_draining
);
9580 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
9581 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
9582 wait_event(root
->fs_info
->async_submit_wait
,
9583 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
9584 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
9586 atomic_dec(&root
->fs_info
->async_submit_draining
);
9590 int btrfs_start_delalloc_roots(struct btrfs_fs_info
*fs_info
, int delay_iput
,
9593 struct btrfs_root
*root
;
9594 struct list_head splice
;
9597 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
9600 INIT_LIST_HEAD(&splice
);
9602 mutex_lock(&fs_info
->delalloc_root_mutex
);
9603 spin_lock(&fs_info
->delalloc_root_lock
);
9604 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
9605 while (!list_empty(&splice
) && nr
) {
9606 root
= list_first_entry(&splice
, struct btrfs_root
,
9608 root
= btrfs_grab_fs_root(root
);
9610 list_move_tail(&root
->delalloc_root
,
9611 &fs_info
->delalloc_roots
);
9612 spin_unlock(&fs_info
->delalloc_root_lock
);
9614 ret
= __start_delalloc_inodes(root
, delay_iput
, nr
);
9615 btrfs_put_fs_root(root
);
9623 spin_lock(&fs_info
->delalloc_root_lock
);
9625 spin_unlock(&fs_info
->delalloc_root_lock
);
9628 atomic_inc(&fs_info
->async_submit_draining
);
9629 while (atomic_read(&fs_info
->nr_async_submits
) ||
9630 atomic_read(&fs_info
->async_delalloc_pages
)) {
9631 wait_event(fs_info
->async_submit_wait
,
9632 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
9633 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
9635 atomic_dec(&fs_info
->async_submit_draining
);
9637 if (!list_empty_careful(&splice
)) {
9638 spin_lock(&fs_info
->delalloc_root_lock
);
9639 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
9640 spin_unlock(&fs_info
->delalloc_root_lock
);
9642 mutex_unlock(&fs_info
->delalloc_root_mutex
);
9646 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
9647 const char *symname
)
9649 struct btrfs_trans_handle
*trans
;
9650 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
9651 struct btrfs_path
*path
;
9652 struct btrfs_key key
;
9653 struct inode
*inode
= NULL
;
9661 struct btrfs_file_extent_item
*ei
;
9662 struct extent_buffer
*leaf
;
9664 name_len
= strlen(symname
);
9665 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
9666 return -ENAMETOOLONG
;
9669 * 2 items for inode item and ref
9670 * 2 items for dir items
9671 * 1 item for xattr if selinux is on
9673 trans
= btrfs_start_transaction(root
, 5);
9675 return PTR_ERR(trans
);
9677 err
= btrfs_find_free_ino(root
, &objectid
);
9681 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
9682 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
9683 S_IFLNK
|S_IRWXUGO
, &index
);
9684 if (IS_ERR(inode
)) {
9685 err
= PTR_ERR(inode
);
9690 * If the active LSM wants to access the inode during
9691 * d_instantiate it needs these. Smack checks to see
9692 * if the filesystem supports xattrs by looking at the
9695 inode
->i_fop
= &btrfs_file_operations
;
9696 inode
->i_op
= &btrfs_file_inode_operations
;
9697 inode
->i_mapping
->a_ops
= &btrfs_aops
;
9698 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
9700 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
9702 goto out_unlock_inode
;
9704 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
9706 goto out_unlock_inode
;
9708 path
= btrfs_alloc_path();
9711 goto out_unlock_inode
;
9713 key
.objectid
= btrfs_ino(inode
);
9715 key
.type
= BTRFS_EXTENT_DATA_KEY
;
9716 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
9717 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
9720 btrfs_free_path(path
);
9721 goto out_unlock_inode
;
9723 leaf
= path
->nodes
[0];
9724 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
9725 struct btrfs_file_extent_item
);
9726 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
9727 btrfs_set_file_extent_type(leaf
, ei
,
9728 BTRFS_FILE_EXTENT_INLINE
);
9729 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
9730 btrfs_set_file_extent_compression(leaf
, ei
, 0);
9731 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
9732 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
9734 ptr
= btrfs_file_extent_inline_start(ei
);
9735 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
9736 btrfs_mark_buffer_dirty(leaf
);
9737 btrfs_free_path(path
);
9739 inode
->i_op
= &btrfs_symlink_inode_operations
;
9740 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
9741 inode_set_bytes(inode
, name_len
);
9742 btrfs_i_size_write(inode
, name_len
);
9743 err
= btrfs_update_inode(trans
, root
, inode
);
9746 goto out_unlock_inode
;
9749 unlock_new_inode(inode
);
9750 d_instantiate(dentry
, inode
);
9753 btrfs_end_transaction(trans
, root
);
9755 inode_dec_link_count(inode
);
9758 btrfs_btree_balance_dirty(root
);
9763 unlock_new_inode(inode
);
9767 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
9768 u64 start
, u64 num_bytes
, u64 min_size
,
9769 loff_t actual_len
, u64
*alloc_hint
,
9770 struct btrfs_trans_handle
*trans
)
9772 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
9773 struct extent_map
*em
;
9774 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9775 struct btrfs_key ins
;
9776 u64 cur_offset
= start
;
9779 u64 last_alloc
= (u64
)-1;
9781 bool own_trans
= true;
9785 while (num_bytes
> 0) {
9787 trans
= btrfs_start_transaction(root
, 3);
9788 if (IS_ERR(trans
)) {
9789 ret
= PTR_ERR(trans
);
9794 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
9795 cur_bytes
= max(cur_bytes
, min_size
);
9797 * If we are severely fragmented we could end up with really
9798 * small allocations, so if the allocator is returning small
9799 * chunks lets make its job easier by only searching for those
9802 cur_bytes
= min(cur_bytes
, last_alloc
);
9803 ret
= btrfs_reserve_extent(root
, cur_bytes
, min_size
, 0,
9804 *alloc_hint
, &ins
, 1, 0);
9807 btrfs_end_transaction(trans
, root
);
9811 last_alloc
= ins
.offset
;
9812 ret
= insert_reserved_file_extent(trans
, inode
,
9813 cur_offset
, ins
.objectid
,
9814 ins
.offset
, ins
.offset
,
9815 ins
.offset
, 0, 0, 0,
9816 BTRFS_FILE_EXTENT_PREALLOC
);
9818 btrfs_free_reserved_extent(root
, ins
.objectid
,
9820 btrfs_abort_transaction(trans
, root
, ret
);
9822 btrfs_end_transaction(trans
, root
);
9826 btrfs_drop_extent_cache(inode
, cur_offset
,
9827 cur_offset
+ ins
.offset
-1, 0);
9829 em
= alloc_extent_map();
9831 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
9832 &BTRFS_I(inode
)->runtime_flags
);
9836 em
->start
= cur_offset
;
9837 em
->orig_start
= cur_offset
;
9838 em
->len
= ins
.offset
;
9839 em
->block_start
= ins
.objectid
;
9840 em
->block_len
= ins
.offset
;
9841 em
->orig_block_len
= ins
.offset
;
9842 em
->ram_bytes
= ins
.offset
;
9843 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
9844 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
9845 em
->generation
= trans
->transid
;
9848 write_lock(&em_tree
->lock
);
9849 ret
= add_extent_mapping(em_tree
, em
, 1);
9850 write_unlock(&em_tree
->lock
);
9853 btrfs_drop_extent_cache(inode
, cur_offset
,
9854 cur_offset
+ ins
.offset
- 1,
9857 free_extent_map(em
);
9859 num_bytes
-= ins
.offset
;
9860 cur_offset
+= ins
.offset
;
9861 *alloc_hint
= ins
.objectid
+ ins
.offset
;
9863 inode_inc_iversion(inode
);
9864 inode
->i_ctime
= CURRENT_TIME
;
9865 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
9866 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
9867 (actual_len
> inode
->i_size
) &&
9868 (cur_offset
> inode
->i_size
)) {
9869 if (cur_offset
> actual_len
)
9870 i_size
= actual_len
;
9872 i_size
= cur_offset
;
9873 i_size_write(inode
, i_size
);
9874 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
9877 ret
= btrfs_update_inode(trans
, root
, inode
);
9880 btrfs_abort_transaction(trans
, root
, ret
);
9882 btrfs_end_transaction(trans
, root
);
9887 btrfs_end_transaction(trans
, root
);
9892 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
9893 u64 start
, u64 num_bytes
, u64 min_size
,
9894 loff_t actual_len
, u64
*alloc_hint
)
9896 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
9897 min_size
, actual_len
, alloc_hint
,
9901 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
9902 struct btrfs_trans_handle
*trans
, int mode
,
9903 u64 start
, u64 num_bytes
, u64 min_size
,
9904 loff_t actual_len
, u64
*alloc_hint
)
9906 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
9907 min_size
, actual_len
, alloc_hint
, trans
);
9910 static int btrfs_set_page_dirty(struct page
*page
)
9912 return __set_page_dirty_nobuffers(page
);
9915 static int btrfs_permission(struct inode
*inode
, int mask
)
9917 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9918 umode_t mode
= inode
->i_mode
;
9920 if (mask
& MAY_WRITE
&&
9921 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
9922 if (btrfs_root_readonly(root
))
9924 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
9927 return generic_permission(inode
, mask
);
9930 static int btrfs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
9932 struct btrfs_trans_handle
*trans
;
9933 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
9934 struct inode
*inode
= NULL
;
9940 * 5 units required for adding orphan entry
9942 trans
= btrfs_start_transaction(root
, 5);
9944 return PTR_ERR(trans
);
9946 ret
= btrfs_find_free_ino(root
, &objectid
);
9950 inode
= btrfs_new_inode(trans
, root
, dir
, NULL
, 0,
9951 btrfs_ino(dir
), objectid
, mode
, &index
);
9952 if (IS_ERR(inode
)) {
9953 ret
= PTR_ERR(inode
);
9958 inode
->i_fop
= &btrfs_file_operations
;
9959 inode
->i_op
= &btrfs_file_inode_operations
;
9961 inode
->i_mapping
->a_ops
= &btrfs_aops
;
9962 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
9964 ret
= btrfs_init_inode_security(trans
, inode
, dir
, NULL
);
9968 ret
= btrfs_update_inode(trans
, root
, inode
);
9971 ret
= btrfs_orphan_add(trans
, inode
);
9976 * We set number of links to 0 in btrfs_new_inode(), and here we set
9977 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9980 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9982 set_nlink(inode
, 1);
9983 unlock_new_inode(inode
);
9984 d_tmpfile(dentry
, inode
);
9985 mark_inode_dirty(inode
);
9988 btrfs_end_transaction(trans
, root
);
9991 btrfs_balance_delayed_items(root
);
9992 btrfs_btree_balance_dirty(root
);
9996 unlock_new_inode(inode
);
10001 /* Inspired by filemap_check_errors() */
10002 int btrfs_inode_check_errors(struct inode
*inode
)
10006 if (test_bit(AS_ENOSPC
, &inode
->i_mapping
->flags
) &&
10007 test_and_clear_bit(AS_ENOSPC
, &inode
->i_mapping
->flags
))
10009 if (test_bit(AS_EIO
, &inode
->i_mapping
->flags
) &&
10010 test_and_clear_bit(AS_EIO
, &inode
->i_mapping
->flags
))
10016 static const struct inode_operations btrfs_dir_inode_operations
= {
10017 .getattr
= btrfs_getattr
,
10018 .lookup
= btrfs_lookup
,
10019 .create
= btrfs_create
,
10020 .unlink
= btrfs_unlink
,
10021 .link
= btrfs_link
,
10022 .mkdir
= btrfs_mkdir
,
10023 .rmdir
= btrfs_rmdir
,
10024 .rename2
= btrfs_rename2
,
10025 .symlink
= btrfs_symlink
,
10026 .setattr
= btrfs_setattr
,
10027 .mknod
= btrfs_mknod
,
10028 .setxattr
= btrfs_setxattr
,
10029 .getxattr
= btrfs_getxattr
,
10030 .listxattr
= btrfs_listxattr
,
10031 .removexattr
= btrfs_removexattr
,
10032 .permission
= btrfs_permission
,
10033 .get_acl
= btrfs_get_acl
,
10034 .set_acl
= btrfs_set_acl
,
10035 .update_time
= btrfs_update_time
,
10036 .tmpfile
= btrfs_tmpfile
,
10038 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
10039 .lookup
= btrfs_lookup
,
10040 .permission
= btrfs_permission
,
10041 .get_acl
= btrfs_get_acl
,
10042 .set_acl
= btrfs_set_acl
,
10043 .update_time
= btrfs_update_time
,
10046 static const struct file_operations btrfs_dir_file_operations
= {
10047 .llseek
= generic_file_llseek
,
10048 .read
= generic_read_dir
,
10049 .iterate
= btrfs_real_readdir
,
10050 .unlocked_ioctl
= btrfs_ioctl
,
10051 #ifdef CONFIG_COMPAT
10052 .compat_ioctl
= btrfs_ioctl
,
10054 .release
= btrfs_release_file
,
10055 .fsync
= btrfs_sync_file
,
10058 static struct extent_io_ops btrfs_extent_io_ops
= {
10059 .fill_delalloc
= run_delalloc_range
,
10060 .submit_bio_hook
= btrfs_submit_bio_hook
,
10061 .merge_bio_hook
= btrfs_merge_bio_hook
,
10062 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
10063 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
10064 .writepage_start_hook
= btrfs_writepage_start_hook
,
10065 .set_bit_hook
= btrfs_set_bit_hook
,
10066 .clear_bit_hook
= btrfs_clear_bit_hook
,
10067 .merge_extent_hook
= btrfs_merge_extent_hook
,
10068 .split_extent_hook
= btrfs_split_extent_hook
,
10072 * btrfs doesn't support the bmap operation because swapfiles
10073 * use bmap to make a mapping of extents in the file. They assume
10074 * these extents won't change over the life of the file and they
10075 * use the bmap result to do IO directly to the drive.
10077 * the btrfs bmap call would return logical addresses that aren't
10078 * suitable for IO and they also will change frequently as COW
10079 * operations happen. So, swapfile + btrfs == corruption.
10081 * For now we're avoiding this by dropping bmap.
10083 static const struct address_space_operations btrfs_aops
= {
10084 .readpage
= btrfs_readpage
,
10085 .writepage
= btrfs_writepage
,
10086 .writepages
= btrfs_writepages
,
10087 .readpages
= btrfs_readpages
,
10088 .direct_IO
= btrfs_direct_IO
,
10089 .invalidatepage
= btrfs_invalidatepage
,
10090 .releasepage
= btrfs_releasepage
,
10091 .set_page_dirty
= btrfs_set_page_dirty
,
10092 .error_remove_page
= generic_error_remove_page
,
10095 static const struct address_space_operations btrfs_symlink_aops
= {
10096 .readpage
= btrfs_readpage
,
10097 .writepage
= btrfs_writepage
,
10098 .invalidatepage
= btrfs_invalidatepage
,
10099 .releasepage
= btrfs_releasepage
,
10102 static const struct inode_operations btrfs_file_inode_operations
= {
10103 .getattr
= btrfs_getattr
,
10104 .setattr
= btrfs_setattr
,
10105 .setxattr
= btrfs_setxattr
,
10106 .getxattr
= btrfs_getxattr
,
10107 .listxattr
= btrfs_listxattr
,
10108 .removexattr
= btrfs_removexattr
,
10109 .permission
= btrfs_permission
,
10110 .fiemap
= btrfs_fiemap
,
10111 .get_acl
= btrfs_get_acl
,
10112 .set_acl
= btrfs_set_acl
,
10113 .update_time
= btrfs_update_time
,
10115 static const struct inode_operations btrfs_special_inode_operations
= {
10116 .getattr
= btrfs_getattr
,
10117 .setattr
= btrfs_setattr
,
10118 .permission
= btrfs_permission
,
10119 .setxattr
= btrfs_setxattr
,
10120 .getxattr
= btrfs_getxattr
,
10121 .listxattr
= btrfs_listxattr
,
10122 .removexattr
= btrfs_removexattr
,
10123 .get_acl
= btrfs_get_acl
,
10124 .set_acl
= btrfs_set_acl
,
10125 .update_time
= btrfs_update_time
,
10127 static const struct inode_operations btrfs_symlink_inode_operations
= {
10128 .readlink
= generic_readlink
,
10129 .follow_link
= page_follow_link_light
,
10130 .put_link
= page_put_link
,
10131 .getattr
= btrfs_getattr
,
10132 .setattr
= btrfs_setattr
,
10133 .permission
= btrfs_permission
,
10134 .setxattr
= btrfs_setxattr
,
10135 .getxattr
= btrfs_getxattr
,
10136 .listxattr
= btrfs_listxattr
,
10137 .removexattr
= btrfs_removexattr
,
10138 .update_time
= btrfs_update_time
,
10141 const struct dentry_operations btrfs_dentry_operations
= {
10142 .d_delete
= btrfs_dentry_delete
,
10143 .d_release
= btrfs_dentry_release
,