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/aio.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/xattr.h>
38 #include <linux/posix_acl.h>
39 #include <linux/falloc.h>
40 #include <linux/slab.h>
41 #include <linux/ratelimit.h>
42 #include <linux/mount.h>
43 #include <linux/btrfs.h>
44 #include <linux/blkdev.h>
45 #include <linux/posix_acl_xattr.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"
63 struct btrfs_iget_args
{
64 struct btrfs_key
*location
;
65 struct btrfs_root
*root
;
68 static const struct inode_operations btrfs_dir_inode_operations
;
69 static const struct inode_operations btrfs_symlink_inode_operations
;
70 static const struct inode_operations btrfs_dir_ro_inode_operations
;
71 static const struct inode_operations btrfs_special_inode_operations
;
72 static const struct inode_operations btrfs_file_inode_operations
;
73 static const struct address_space_operations btrfs_aops
;
74 static const struct address_space_operations btrfs_symlink_aops
;
75 static const struct file_operations btrfs_dir_file_operations
;
76 static struct extent_io_ops btrfs_extent_io_ops
;
78 static struct kmem_cache
*btrfs_inode_cachep
;
79 static struct kmem_cache
*btrfs_delalloc_work_cachep
;
80 struct kmem_cache
*btrfs_trans_handle_cachep
;
81 struct kmem_cache
*btrfs_transaction_cachep
;
82 struct kmem_cache
*btrfs_path_cachep
;
83 struct kmem_cache
*btrfs_free_space_cachep
;
86 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
87 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
88 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
89 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
90 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
91 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
92 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
93 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
96 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
97 static int btrfs_truncate(struct inode
*inode
);
98 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
99 static noinline
int cow_file_range(struct inode
*inode
,
100 struct page
*locked_page
,
101 u64 start
, u64 end
, int *page_started
,
102 unsigned long *nr_written
, int unlock
);
103 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
104 u64 len
, u64 orig_start
,
105 u64 block_start
, u64 block_len
,
106 u64 orig_block_len
, u64 ram_bytes
,
109 static int btrfs_dirty_inode(struct inode
*inode
);
111 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
112 void btrfs_test_inode_set_ops(struct inode
*inode
)
114 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
118 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
119 struct inode
*inode
, struct inode
*dir
,
120 const struct qstr
*qstr
)
124 err
= btrfs_init_acl(trans
, inode
, dir
);
126 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
131 * this does all the hard work for inserting an inline extent into
132 * the btree. The caller should have done a btrfs_drop_extents so that
133 * no overlapping inline items exist in the btree
135 static int insert_inline_extent(struct btrfs_trans_handle
*trans
,
136 struct btrfs_path
*path
, int extent_inserted
,
137 struct btrfs_root
*root
, struct inode
*inode
,
138 u64 start
, size_t size
, size_t compressed_size
,
140 struct page
**compressed_pages
)
142 struct extent_buffer
*leaf
;
143 struct page
*page
= NULL
;
146 struct btrfs_file_extent_item
*ei
;
149 size_t cur_size
= size
;
150 unsigned long offset
;
152 if (compressed_size
&& compressed_pages
)
153 cur_size
= compressed_size
;
155 inode_add_bytes(inode
, size
);
157 if (!extent_inserted
) {
158 struct btrfs_key key
;
161 key
.objectid
= btrfs_ino(inode
);
163 key
.type
= BTRFS_EXTENT_DATA_KEY
;
165 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
166 path
->leave_spinning
= 1;
167 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
174 leaf
= path
->nodes
[0];
175 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
176 struct btrfs_file_extent_item
);
177 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
178 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
179 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
180 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
181 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
182 ptr
= btrfs_file_extent_inline_start(ei
);
184 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
187 while (compressed_size
> 0) {
188 cpage
= compressed_pages
[i
];
189 cur_size
= min_t(unsigned long, compressed_size
,
192 kaddr
= kmap_atomic(cpage
);
193 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
194 kunmap_atomic(kaddr
);
198 compressed_size
-= cur_size
;
200 btrfs_set_file_extent_compression(leaf
, ei
,
203 page
= find_get_page(inode
->i_mapping
,
204 start
>> PAGE_CACHE_SHIFT
);
205 btrfs_set_file_extent_compression(leaf
, ei
, 0);
206 kaddr
= kmap_atomic(page
);
207 offset
= start
& (PAGE_CACHE_SIZE
- 1);
208 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
209 kunmap_atomic(kaddr
);
210 page_cache_release(page
);
212 btrfs_mark_buffer_dirty(leaf
);
213 btrfs_release_path(path
);
216 * we're an inline extent, so nobody can
217 * extend the file past i_size without locking
218 * a page we already have locked.
220 * We must do any isize and inode updates
221 * before we unlock the pages. Otherwise we
222 * could end up racing with unlink.
224 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
225 ret
= btrfs_update_inode(trans
, root
, inode
);
234 * conditionally insert an inline extent into the file. This
235 * does the checks required to make sure the data is small enough
236 * to fit as an inline extent.
238 static noinline
int cow_file_range_inline(struct btrfs_root
*root
,
239 struct inode
*inode
, u64 start
,
240 u64 end
, size_t compressed_size
,
242 struct page
**compressed_pages
)
244 struct btrfs_trans_handle
*trans
;
245 u64 isize
= i_size_read(inode
);
246 u64 actual_end
= min(end
+ 1, isize
);
247 u64 inline_len
= actual_end
- start
;
248 u64 aligned_end
= ALIGN(end
, root
->sectorsize
);
249 u64 data_len
= inline_len
;
251 struct btrfs_path
*path
;
252 int extent_inserted
= 0;
253 u32 extent_item_size
;
256 data_len
= compressed_size
;
259 actual_end
> PAGE_CACHE_SIZE
||
260 data_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
262 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
264 data_len
> root
->fs_info
->max_inline
) {
268 path
= btrfs_alloc_path();
272 trans
= btrfs_join_transaction(root
);
274 btrfs_free_path(path
);
275 return PTR_ERR(trans
);
277 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
279 if (compressed_size
&& compressed_pages
)
280 extent_item_size
= btrfs_file_extent_calc_inline_size(
283 extent_item_size
= btrfs_file_extent_calc_inline_size(
286 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
,
287 start
, aligned_end
, NULL
,
288 1, 1, extent_item_size
, &extent_inserted
);
290 btrfs_abort_transaction(trans
, root
, ret
);
294 if (isize
> actual_end
)
295 inline_len
= min_t(u64
, isize
, actual_end
);
296 ret
= insert_inline_extent(trans
, path
, extent_inserted
,
298 inline_len
, compressed_size
,
299 compress_type
, compressed_pages
);
300 if (ret
&& ret
!= -ENOSPC
) {
301 btrfs_abort_transaction(trans
, root
, ret
);
303 } else if (ret
== -ENOSPC
) {
308 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
309 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
310 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
312 btrfs_free_path(path
);
313 btrfs_end_transaction(trans
, root
);
317 struct async_extent
{
322 unsigned long nr_pages
;
324 struct list_head list
;
329 struct btrfs_root
*root
;
330 struct page
*locked_page
;
333 struct list_head extents
;
334 struct btrfs_work work
;
337 static noinline
int add_async_extent(struct async_cow
*cow
,
338 u64 start
, u64 ram_size
,
341 unsigned long nr_pages
,
344 struct async_extent
*async_extent
;
346 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
347 BUG_ON(!async_extent
); /* -ENOMEM */
348 async_extent
->start
= start
;
349 async_extent
->ram_size
= ram_size
;
350 async_extent
->compressed_size
= compressed_size
;
351 async_extent
->pages
= pages
;
352 async_extent
->nr_pages
= nr_pages
;
353 async_extent
->compress_type
= compress_type
;
354 list_add_tail(&async_extent
->list
, &cow
->extents
);
358 static inline int inode_need_compress(struct inode
*inode
)
360 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
363 if (btrfs_test_opt(root
, FORCE_COMPRESS
))
365 /* bad compression ratios */
366 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
368 if (btrfs_test_opt(root
, COMPRESS
) ||
369 BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
||
370 BTRFS_I(inode
)->force_compress
)
376 * we create compressed extents in two phases. The first
377 * phase compresses a range of pages that have already been
378 * locked (both pages and state bits are locked).
380 * This is done inside an ordered work queue, and the compression
381 * is spread across many cpus. The actual IO submission is step
382 * two, and the ordered work queue takes care of making sure that
383 * happens in the same order things were put onto the queue by
384 * writepages and friends.
386 * If this code finds it can't get good compression, it puts an
387 * entry onto the work queue to write the uncompressed bytes. This
388 * makes sure that both compressed inodes and uncompressed inodes
389 * are written in the same order that the flusher thread sent them
392 static noinline
void compress_file_range(struct inode
*inode
,
393 struct page
*locked_page
,
395 struct async_cow
*async_cow
,
398 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
400 u64 blocksize
= root
->sectorsize
;
402 u64 isize
= i_size_read(inode
);
404 struct page
**pages
= NULL
;
405 unsigned long nr_pages
;
406 unsigned long nr_pages_ret
= 0;
407 unsigned long total_compressed
= 0;
408 unsigned long total_in
= 0;
409 unsigned long max_compressed
= 128 * 1024;
410 unsigned long max_uncompressed
= 128 * 1024;
413 int compress_type
= root
->fs_info
->compress_type
;
416 /* if this is a small write inside eof, kick off a defrag */
417 if ((end
- start
+ 1) < 16 * 1024 &&
418 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
419 btrfs_add_inode_defrag(NULL
, inode
);
421 actual_end
= min_t(u64
, isize
, end
+ 1);
424 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
425 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
428 * we don't want to send crud past the end of i_size through
429 * compression, that's just a waste of CPU time. So, if the
430 * end of the file is before the start of our current
431 * requested range of bytes, we bail out to the uncompressed
432 * cleanup code that can deal with all of this.
434 * It isn't really the fastest way to fix things, but this is a
435 * very uncommon corner.
437 if (actual_end
<= start
)
438 goto cleanup_and_bail_uncompressed
;
440 total_compressed
= actual_end
- start
;
443 * skip compression for a small file range(<=blocksize) that
444 * isn't an inline extent, since it dosen't save disk space at all.
446 if (total_compressed
<= blocksize
&&
447 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
448 goto cleanup_and_bail_uncompressed
;
450 /* we want to make sure that amount of ram required to uncompress
451 * an extent is reasonable, so we limit the total size in ram
452 * of a compressed extent to 128k. This is a crucial number
453 * because it also controls how easily we can spread reads across
454 * cpus for decompression.
456 * We also want to make sure the amount of IO required to do
457 * a random read is reasonably small, so we limit the size of
458 * a compressed extent to 128k.
460 total_compressed
= min(total_compressed
, max_uncompressed
);
461 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
462 num_bytes
= max(blocksize
, num_bytes
);
467 * we do compression for mount -o compress and when the
468 * inode has not been flagged as nocompress. This flag can
469 * change at any time if we discover bad compression ratios.
471 if (inode_need_compress(inode
)) {
473 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
475 /* just bail out to the uncompressed code */
479 if (BTRFS_I(inode
)->force_compress
)
480 compress_type
= BTRFS_I(inode
)->force_compress
;
483 * we need to call clear_page_dirty_for_io on each
484 * page in the range. Otherwise applications with the file
485 * mmap'd can wander in and change the page contents while
486 * we are compressing them.
488 * If the compression fails for any reason, we set the pages
489 * dirty again later on.
491 extent_range_clear_dirty_for_io(inode
, start
, end
);
493 ret
= btrfs_compress_pages(compress_type
,
494 inode
->i_mapping
, start
,
495 total_compressed
, pages
,
496 nr_pages
, &nr_pages_ret
,
502 unsigned long offset
= total_compressed
&
503 (PAGE_CACHE_SIZE
- 1);
504 struct page
*page
= pages
[nr_pages_ret
- 1];
507 /* zero the tail end of the last page, we might be
508 * sending it down to disk
511 kaddr
= kmap_atomic(page
);
512 memset(kaddr
+ offset
, 0,
513 PAGE_CACHE_SIZE
- offset
);
514 kunmap_atomic(kaddr
);
521 /* lets try to make an inline extent */
522 if (ret
|| total_in
< (actual_end
- start
)) {
523 /* we didn't compress the entire range, try
524 * to make an uncompressed inline extent.
526 ret
= cow_file_range_inline(root
, inode
, start
, end
,
529 /* try making a compressed inline extent */
530 ret
= cow_file_range_inline(root
, inode
, start
, end
,
532 compress_type
, pages
);
535 unsigned long clear_flags
= EXTENT_DELALLOC
|
537 unsigned long page_error_op
;
539 clear_flags
|= (ret
< 0) ? EXTENT_DO_ACCOUNTING
: 0;
540 page_error_op
= ret
< 0 ? PAGE_SET_ERROR
: 0;
543 * inline extent creation worked or returned error,
544 * we don't need to create any more async work items.
545 * Unlock and free up our temp pages.
547 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
,
548 clear_flags
, PAGE_UNLOCK
|
559 * we aren't doing an inline extent round the compressed size
560 * up to a block size boundary so the allocator does sane
563 total_compressed
= ALIGN(total_compressed
, blocksize
);
566 * one last check to make sure the compression is really a
567 * win, compare the page count read with the blocks on disk
569 total_in
= ALIGN(total_in
, PAGE_CACHE_SIZE
);
570 if (total_compressed
>= total_in
) {
573 num_bytes
= total_in
;
576 if (!will_compress
&& pages
) {
578 * the compression code ran but failed to make things smaller,
579 * free any pages it allocated and our page pointer array
581 for (i
= 0; i
< nr_pages_ret
; i
++) {
582 WARN_ON(pages
[i
]->mapping
);
583 page_cache_release(pages
[i
]);
587 total_compressed
= 0;
590 /* flag the file so we don't compress in the future */
591 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
592 !(BTRFS_I(inode
)->force_compress
)) {
593 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
599 /* the async work queues will take care of doing actual
600 * allocation on disk for these compressed pages,
601 * and will submit them to the elevator.
603 add_async_extent(async_cow
, start
, num_bytes
,
604 total_compressed
, pages
, nr_pages_ret
,
607 if (start
+ num_bytes
< end
) {
614 cleanup_and_bail_uncompressed
:
616 * No compression, but we still need to write the pages in
617 * the file we've been given so far. redirty the locked
618 * page if it corresponds to our extent and set things up
619 * for the async work queue to run cow_file_range to do
620 * the normal delalloc dance
622 if (page_offset(locked_page
) >= start
&&
623 page_offset(locked_page
) <= end
) {
624 __set_page_dirty_nobuffers(locked_page
);
625 /* unlocked later on in the async handlers */
628 extent_range_redirty_for_io(inode
, start
, end
);
629 add_async_extent(async_cow
, start
, end
- start
+ 1,
630 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
637 for (i
= 0; i
< nr_pages_ret
; i
++) {
638 WARN_ON(pages
[i
]->mapping
);
639 page_cache_release(pages
[i
]);
644 static void free_async_extent_pages(struct async_extent
*async_extent
)
648 if (!async_extent
->pages
)
651 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
652 WARN_ON(async_extent
->pages
[i
]->mapping
);
653 page_cache_release(async_extent
->pages
[i
]);
655 kfree(async_extent
->pages
);
656 async_extent
->nr_pages
= 0;
657 async_extent
->pages
= NULL
;
661 * phase two of compressed writeback. This is the ordered portion
662 * of the code, which only gets called in the order the work was
663 * queued. We walk all the async extents created by compress_file_range
664 * and send them down to the disk.
666 static noinline
void submit_compressed_extents(struct inode
*inode
,
667 struct async_cow
*async_cow
)
669 struct async_extent
*async_extent
;
671 struct btrfs_key ins
;
672 struct extent_map
*em
;
673 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
674 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
675 struct extent_io_tree
*io_tree
;
679 while (!list_empty(&async_cow
->extents
)) {
680 async_extent
= list_entry(async_cow
->extents
.next
,
681 struct async_extent
, list
);
682 list_del(&async_extent
->list
);
684 io_tree
= &BTRFS_I(inode
)->io_tree
;
687 /* did the compression code fall back to uncompressed IO? */
688 if (!async_extent
->pages
) {
689 int page_started
= 0;
690 unsigned long nr_written
= 0;
692 lock_extent(io_tree
, async_extent
->start
,
693 async_extent
->start
+
694 async_extent
->ram_size
- 1);
696 /* allocate blocks */
697 ret
= cow_file_range(inode
, async_cow
->locked_page
,
699 async_extent
->start
+
700 async_extent
->ram_size
- 1,
701 &page_started
, &nr_written
, 0);
706 * if page_started, cow_file_range inserted an
707 * inline extent and took care of all the unlocking
708 * and IO for us. Otherwise, we need to submit
709 * all those pages down to the drive.
711 if (!page_started
&& !ret
)
712 extent_write_locked_range(io_tree
,
713 inode
, async_extent
->start
,
714 async_extent
->start
+
715 async_extent
->ram_size
- 1,
719 unlock_page(async_cow
->locked_page
);
725 lock_extent(io_tree
, async_extent
->start
,
726 async_extent
->start
+ async_extent
->ram_size
- 1);
728 ret
= btrfs_reserve_extent(root
,
729 async_extent
->compressed_size
,
730 async_extent
->compressed_size
,
731 0, alloc_hint
, &ins
, 1, 1);
733 free_async_extent_pages(async_extent
);
735 if (ret
== -ENOSPC
) {
736 unlock_extent(io_tree
, async_extent
->start
,
737 async_extent
->start
+
738 async_extent
->ram_size
- 1);
741 * we need to redirty the pages if we decide to
742 * fallback to uncompressed IO, otherwise we
743 * will not submit these pages down to lower
746 extent_range_redirty_for_io(inode
,
748 async_extent
->start
+
749 async_extent
->ram_size
- 1);
757 * here we're doing allocation and writeback of the
760 btrfs_drop_extent_cache(inode
, async_extent
->start
,
761 async_extent
->start
+
762 async_extent
->ram_size
- 1, 0);
764 em
= alloc_extent_map();
767 goto out_free_reserve
;
769 em
->start
= async_extent
->start
;
770 em
->len
= async_extent
->ram_size
;
771 em
->orig_start
= em
->start
;
772 em
->mod_start
= em
->start
;
773 em
->mod_len
= em
->len
;
775 em
->block_start
= ins
.objectid
;
776 em
->block_len
= ins
.offset
;
777 em
->orig_block_len
= ins
.offset
;
778 em
->ram_bytes
= async_extent
->ram_size
;
779 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
780 em
->compress_type
= async_extent
->compress_type
;
781 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
782 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
786 write_lock(&em_tree
->lock
);
787 ret
= add_extent_mapping(em_tree
, em
, 1);
788 write_unlock(&em_tree
->lock
);
789 if (ret
!= -EEXIST
) {
793 btrfs_drop_extent_cache(inode
, async_extent
->start
,
794 async_extent
->start
+
795 async_extent
->ram_size
- 1, 0);
799 goto out_free_reserve
;
801 ret
= btrfs_add_ordered_extent_compress(inode
,
804 async_extent
->ram_size
,
806 BTRFS_ORDERED_COMPRESSED
,
807 async_extent
->compress_type
);
809 btrfs_drop_extent_cache(inode
, async_extent
->start
,
810 async_extent
->start
+
811 async_extent
->ram_size
- 1, 0);
812 goto out_free_reserve
;
816 * clear dirty, set writeback and unlock the pages.
818 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
819 async_extent
->start
+
820 async_extent
->ram_size
- 1,
821 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
,
822 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
824 ret
= btrfs_submit_compressed_write(inode
,
826 async_extent
->ram_size
,
828 ins
.offset
, async_extent
->pages
,
829 async_extent
->nr_pages
);
831 struct extent_io_tree
*tree
= &BTRFS_I(inode
)->io_tree
;
832 struct page
*p
= async_extent
->pages
[0];
833 const u64 start
= async_extent
->start
;
834 const u64 end
= start
+ async_extent
->ram_size
- 1;
836 p
->mapping
= inode
->i_mapping
;
837 tree
->ops
->writepage_end_io_hook(p
, start
, end
,
840 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
, 0,
843 free_async_extent_pages(async_extent
);
845 alloc_hint
= ins
.objectid
+ ins
.offset
;
851 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
853 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
854 async_extent
->start
+
855 async_extent
->ram_size
- 1,
856 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
|
857 EXTENT_DEFRAG
| EXTENT_DO_ACCOUNTING
,
858 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
859 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
|
861 free_async_extent_pages(async_extent
);
866 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
869 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
870 struct extent_map
*em
;
873 read_lock(&em_tree
->lock
);
874 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
877 * if block start isn't an actual block number then find the
878 * first block in this inode and use that as a hint. If that
879 * block is also bogus then just don't worry about it.
881 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
883 em
= search_extent_mapping(em_tree
, 0, 0);
884 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
885 alloc_hint
= em
->block_start
;
889 alloc_hint
= em
->block_start
;
893 read_unlock(&em_tree
->lock
);
899 * when extent_io.c finds a delayed allocation range in the file,
900 * the call backs end up in this code. The basic idea is to
901 * allocate extents on disk for the range, and create ordered data structs
902 * in ram to track those extents.
904 * locked_page is the page that writepage had locked already. We use
905 * it to make sure we don't do extra locks or unlocks.
907 * *page_started is set to one if we unlock locked_page and do everything
908 * required to start IO on it. It may be clean and already done with
911 static noinline
int cow_file_range(struct inode
*inode
,
912 struct page
*locked_page
,
913 u64 start
, u64 end
, int *page_started
,
914 unsigned long *nr_written
,
917 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
920 unsigned long ram_size
;
923 u64 blocksize
= root
->sectorsize
;
924 struct btrfs_key ins
;
925 struct extent_map
*em
;
926 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
929 if (btrfs_is_free_space_inode(inode
)) {
935 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
936 num_bytes
= max(blocksize
, num_bytes
);
937 disk_num_bytes
= num_bytes
;
939 /* if this is a small write inside eof, kick off defrag */
940 if (num_bytes
< 64 * 1024 &&
941 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
942 btrfs_add_inode_defrag(NULL
, inode
);
945 /* lets try to make an inline extent */
946 ret
= cow_file_range_inline(root
, inode
, start
, end
, 0, 0,
949 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
,
950 EXTENT_LOCKED
| EXTENT_DELALLOC
|
951 EXTENT_DEFRAG
, PAGE_UNLOCK
|
952 PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
955 *nr_written
= *nr_written
+
956 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
959 } else if (ret
< 0) {
964 BUG_ON(disk_num_bytes
>
965 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
967 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
968 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
970 while (disk_num_bytes
> 0) {
973 cur_alloc_size
= disk_num_bytes
;
974 ret
= btrfs_reserve_extent(root
, cur_alloc_size
,
975 root
->sectorsize
, 0, alloc_hint
,
980 em
= alloc_extent_map();
986 em
->orig_start
= em
->start
;
987 ram_size
= ins
.offset
;
988 em
->len
= ins
.offset
;
989 em
->mod_start
= em
->start
;
990 em
->mod_len
= em
->len
;
992 em
->block_start
= ins
.objectid
;
993 em
->block_len
= ins
.offset
;
994 em
->orig_block_len
= ins
.offset
;
995 em
->ram_bytes
= ram_size
;
996 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
997 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1001 write_lock(&em_tree
->lock
);
1002 ret
= add_extent_mapping(em_tree
, em
, 1);
1003 write_unlock(&em_tree
->lock
);
1004 if (ret
!= -EEXIST
) {
1005 free_extent_map(em
);
1008 btrfs_drop_extent_cache(inode
, start
,
1009 start
+ ram_size
- 1, 0);
1014 cur_alloc_size
= ins
.offset
;
1015 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
1016 ram_size
, cur_alloc_size
, 0);
1018 goto out_drop_extent_cache
;
1020 if (root
->root_key
.objectid
==
1021 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1022 ret
= btrfs_reloc_clone_csums(inode
, start
,
1025 goto out_drop_extent_cache
;
1028 if (disk_num_bytes
< cur_alloc_size
)
1031 /* we're not doing compressed IO, don't unlock the first
1032 * page (which the caller expects to stay locked), don't
1033 * clear any dirty bits and don't set any writeback bits
1035 * Do set the Private2 bit so we know this page was properly
1036 * setup for writepage
1038 op
= unlock
? PAGE_UNLOCK
: 0;
1039 op
|= PAGE_SET_PRIVATE2
;
1041 extent_clear_unlock_delalloc(inode
, start
,
1042 start
+ ram_size
- 1, locked_page
,
1043 EXTENT_LOCKED
| EXTENT_DELALLOC
,
1045 disk_num_bytes
-= cur_alloc_size
;
1046 num_bytes
-= cur_alloc_size
;
1047 alloc_hint
= ins
.objectid
+ ins
.offset
;
1048 start
+= cur_alloc_size
;
1053 out_drop_extent_cache
:
1054 btrfs_drop_extent_cache(inode
, start
, start
+ ram_size
- 1, 0);
1056 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
1058 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1059 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
1060 EXTENT_DELALLOC
| EXTENT_DEFRAG
,
1061 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
1062 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
);
1067 * work queue call back to started compression on a file and pages
1069 static noinline
void async_cow_start(struct btrfs_work
*work
)
1071 struct async_cow
*async_cow
;
1073 async_cow
= container_of(work
, struct async_cow
, work
);
1075 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1076 async_cow
->start
, async_cow
->end
, async_cow
,
1078 if (num_added
== 0) {
1079 btrfs_add_delayed_iput(async_cow
->inode
);
1080 async_cow
->inode
= NULL
;
1085 * work queue call back to submit previously compressed pages
1087 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1089 struct async_cow
*async_cow
;
1090 struct btrfs_root
*root
;
1091 unsigned long nr_pages
;
1093 async_cow
= container_of(work
, struct async_cow
, work
);
1095 root
= async_cow
->root
;
1096 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1099 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1101 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1102 wake_up(&root
->fs_info
->async_submit_wait
);
1104 if (async_cow
->inode
)
1105 submit_compressed_extents(async_cow
->inode
, async_cow
);
1108 static noinline
void async_cow_free(struct btrfs_work
*work
)
1110 struct async_cow
*async_cow
;
1111 async_cow
= container_of(work
, struct async_cow
, work
);
1112 if (async_cow
->inode
)
1113 btrfs_add_delayed_iput(async_cow
->inode
);
1117 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1118 u64 start
, u64 end
, int *page_started
,
1119 unsigned long *nr_written
)
1121 struct async_cow
*async_cow
;
1122 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1123 unsigned long nr_pages
;
1125 int limit
= 10 * 1024 * 1024;
1127 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1128 1, 0, NULL
, GFP_NOFS
);
1129 while (start
< end
) {
1130 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1131 BUG_ON(!async_cow
); /* -ENOMEM */
1132 async_cow
->inode
= igrab(inode
);
1133 async_cow
->root
= root
;
1134 async_cow
->locked_page
= locked_page
;
1135 async_cow
->start
= start
;
1137 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
&&
1138 !btrfs_test_opt(root
, FORCE_COMPRESS
))
1141 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1143 async_cow
->end
= cur_end
;
1144 INIT_LIST_HEAD(&async_cow
->extents
);
1146 btrfs_init_work(&async_cow
->work
,
1147 btrfs_delalloc_helper
,
1148 async_cow_start
, async_cow_submit
,
1151 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1153 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1155 btrfs_queue_work(root
->fs_info
->delalloc_workers
,
1158 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1159 wait_event(root
->fs_info
->async_submit_wait
,
1160 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1164 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1165 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1166 wait_event(root
->fs_info
->async_submit_wait
,
1167 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1171 *nr_written
+= nr_pages
;
1172 start
= cur_end
+ 1;
1178 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1179 u64 bytenr
, u64 num_bytes
)
1182 struct btrfs_ordered_sum
*sums
;
1185 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1186 bytenr
+ num_bytes
- 1, &list
, 0);
1187 if (ret
== 0 && list_empty(&list
))
1190 while (!list_empty(&list
)) {
1191 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1192 list_del(&sums
->list
);
1199 * when nowcow writeback call back. This checks for snapshots or COW copies
1200 * of the extents that exist in the file, and COWs the file as required.
1202 * If no cow copies or snapshots exist, we write directly to the existing
1205 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1206 struct page
*locked_page
,
1207 u64 start
, u64 end
, int *page_started
, int force
,
1208 unsigned long *nr_written
)
1210 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1211 struct btrfs_trans_handle
*trans
;
1212 struct extent_buffer
*leaf
;
1213 struct btrfs_path
*path
;
1214 struct btrfs_file_extent_item
*fi
;
1215 struct btrfs_key found_key
;
1230 u64 ino
= btrfs_ino(inode
);
1232 path
= btrfs_alloc_path();
1234 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1235 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1236 EXTENT_DO_ACCOUNTING
|
1237 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1239 PAGE_SET_WRITEBACK
|
1240 PAGE_END_WRITEBACK
);
1244 nolock
= btrfs_is_free_space_inode(inode
);
1247 trans
= btrfs_join_transaction_nolock(root
);
1249 trans
= btrfs_join_transaction(root
);
1251 if (IS_ERR(trans
)) {
1252 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1253 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1254 EXTENT_DO_ACCOUNTING
|
1255 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1257 PAGE_SET_WRITEBACK
|
1258 PAGE_END_WRITEBACK
);
1259 btrfs_free_path(path
);
1260 return PTR_ERR(trans
);
1263 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1265 cow_start
= (u64
)-1;
1268 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1272 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1273 leaf
= path
->nodes
[0];
1274 btrfs_item_key_to_cpu(leaf
, &found_key
,
1275 path
->slots
[0] - 1);
1276 if (found_key
.objectid
== ino
&&
1277 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1282 leaf
= path
->nodes
[0];
1283 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1284 ret
= btrfs_next_leaf(root
, path
);
1289 leaf
= path
->nodes
[0];
1295 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1297 if (found_key
.objectid
> ino
||
1298 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1299 found_key
.offset
> end
)
1302 if (found_key
.offset
> cur_offset
) {
1303 extent_end
= found_key
.offset
;
1308 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1309 struct btrfs_file_extent_item
);
1310 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1312 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1313 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1314 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1315 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1316 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1317 extent_end
= found_key
.offset
+
1318 btrfs_file_extent_num_bytes(leaf
, fi
);
1320 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1321 if (extent_end
<= start
) {
1325 if (disk_bytenr
== 0)
1327 if (btrfs_file_extent_compression(leaf
, fi
) ||
1328 btrfs_file_extent_encryption(leaf
, fi
) ||
1329 btrfs_file_extent_other_encoding(leaf
, fi
))
1331 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1333 if (btrfs_extent_readonly(root
, disk_bytenr
))
1335 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1337 extent_offset
, disk_bytenr
))
1339 disk_bytenr
+= extent_offset
;
1340 disk_bytenr
+= cur_offset
- found_key
.offset
;
1341 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1343 * if there are pending snapshots for this root,
1344 * we fall into common COW way.
1347 err
= btrfs_start_write_no_snapshoting(root
);
1352 * force cow if csum exists in the range.
1353 * this ensure that csum for a given extent are
1354 * either valid or do not exist.
1356 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1359 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1360 extent_end
= found_key
.offset
+
1361 btrfs_file_extent_inline_len(leaf
,
1362 path
->slots
[0], fi
);
1363 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1368 if (extent_end
<= start
) {
1370 if (!nolock
&& nocow
)
1371 btrfs_end_write_no_snapshoting(root
);
1375 if (cow_start
== (u64
)-1)
1376 cow_start
= cur_offset
;
1377 cur_offset
= extent_end
;
1378 if (cur_offset
> end
)
1384 btrfs_release_path(path
);
1385 if (cow_start
!= (u64
)-1) {
1386 ret
= cow_file_range(inode
, locked_page
,
1387 cow_start
, found_key
.offset
- 1,
1388 page_started
, nr_written
, 1);
1390 if (!nolock
&& nocow
)
1391 btrfs_end_write_no_snapshoting(root
);
1394 cow_start
= (u64
)-1;
1397 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1398 struct extent_map
*em
;
1399 struct extent_map_tree
*em_tree
;
1400 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1401 em
= alloc_extent_map();
1402 BUG_ON(!em
); /* -ENOMEM */
1403 em
->start
= cur_offset
;
1404 em
->orig_start
= found_key
.offset
- extent_offset
;
1405 em
->len
= num_bytes
;
1406 em
->block_len
= num_bytes
;
1407 em
->block_start
= disk_bytenr
;
1408 em
->orig_block_len
= disk_num_bytes
;
1409 em
->ram_bytes
= ram_bytes
;
1410 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1411 em
->mod_start
= em
->start
;
1412 em
->mod_len
= em
->len
;
1413 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1414 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1415 em
->generation
= -1;
1417 write_lock(&em_tree
->lock
);
1418 ret
= add_extent_mapping(em_tree
, em
, 1);
1419 write_unlock(&em_tree
->lock
);
1420 if (ret
!= -EEXIST
) {
1421 free_extent_map(em
);
1424 btrfs_drop_extent_cache(inode
, em
->start
,
1425 em
->start
+ em
->len
- 1, 0);
1427 type
= BTRFS_ORDERED_PREALLOC
;
1429 type
= BTRFS_ORDERED_NOCOW
;
1432 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1433 num_bytes
, num_bytes
, type
);
1434 BUG_ON(ret
); /* -ENOMEM */
1436 if (root
->root_key
.objectid
==
1437 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1438 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1441 if (!nolock
&& nocow
)
1442 btrfs_end_write_no_snapshoting(root
);
1447 extent_clear_unlock_delalloc(inode
, cur_offset
,
1448 cur_offset
+ num_bytes
- 1,
1449 locked_page
, EXTENT_LOCKED
|
1450 EXTENT_DELALLOC
, PAGE_UNLOCK
|
1452 if (!nolock
&& nocow
)
1453 btrfs_end_write_no_snapshoting(root
);
1454 cur_offset
= extent_end
;
1455 if (cur_offset
> end
)
1458 btrfs_release_path(path
);
1460 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1461 cow_start
= cur_offset
;
1465 if (cow_start
!= (u64
)-1) {
1466 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1467 page_started
, nr_written
, 1);
1473 err
= btrfs_end_transaction(trans
, root
);
1477 if (ret
&& cur_offset
< end
)
1478 extent_clear_unlock_delalloc(inode
, cur_offset
, end
,
1479 locked_page
, EXTENT_LOCKED
|
1480 EXTENT_DELALLOC
| EXTENT_DEFRAG
|
1481 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1483 PAGE_SET_WRITEBACK
|
1484 PAGE_END_WRITEBACK
);
1485 btrfs_free_path(path
);
1489 static inline int need_force_cow(struct inode
*inode
, u64 start
, u64 end
)
1492 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
1493 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
))
1497 * @defrag_bytes is a hint value, no spinlock held here,
1498 * if is not zero, it means the file is defragging.
1499 * Force cow if given extent needs to be defragged.
1501 if (BTRFS_I(inode
)->defrag_bytes
&&
1502 test_range_bit(&BTRFS_I(inode
)->io_tree
, start
, end
,
1503 EXTENT_DEFRAG
, 0, NULL
))
1510 * extent_io.c call back to do delayed allocation processing
1512 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1513 u64 start
, u64 end
, int *page_started
,
1514 unsigned long *nr_written
)
1517 int force_cow
= need_force_cow(inode
, start
, end
);
1519 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
&& !force_cow
) {
1520 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1521 page_started
, 1, nr_written
);
1522 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
&& !force_cow
) {
1523 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1524 page_started
, 0, nr_written
);
1525 } else if (!inode_need_compress(inode
)) {
1526 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1527 page_started
, nr_written
, 1);
1529 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1530 &BTRFS_I(inode
)->runtime_flags
);
1531 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1532 page_started
, nr_written
);
1537 static void btrfs_split_extent_hook(struct inode
*inode
,
1538 struct extent_state
*orig
, u64 split
)
1542 /* not delalloc, ignore it */
1543 if (!(orig
->state
& EXTENT_DELALLOC
))
1546 size
= orig
->end
- orig
->start
+ 1;
1547 if (size
> BTRFS_MAX_EXTENT_SIZE
) {
1552 * See the explanation in btrfs_merge_extent_hook, the same
1553 * applies here, just in reverse.
1555 new_size
= orig
->end
- split
+ 1;
1556 num_extents
= div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1557 BTRFS_MAX_EXTENT_SIZE
);
1558 new_size
= split
- orig
->start
;
1559 num_extents
+= div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1560 BTRFS_MAX_EXTENT_SIZE
);
1561 if (div64_u64(size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1562 BTRFS_MAX_EXTENT_SIZE
) >= num_extents
)
1566 spin_lock(&BTRFS_I(inode
)->lock
);
1567 BTRFS_I(inode
)->outstanding_extents
++;
1568 spin_unlock(&BTRFS_I(inode
)->lock
);
1572 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1573 * extents so we can keep track of new extents that are just merged onto old
1574 * extents, such as when we are doing sequential writes, so we can properly
1575 * account for the metadata space we'll need.
1577 static void btrfs_merge_extent_hook(struct inode
*inode
,
1578 struct extent_state
*new,
1579 struct extent_state
*other
)
1581 u64 new_size
, old_size
;
1584 /* not delalloc, ignore it */
1585 if (!(other
->state
& EXTENT_DELALLOC
))
1588 if (new->start
> other
->start
)
1589 new_size
= new->end
- other
->start
+ 1;
1591 new_size
= other
->end
- new->start
+ 1;
1593 /* we're not bigger than the max, unreserve the space and go */
1594 if (new_size
<= BTRFS_MAX_EXTENT_SIZE
) {
1595 spin_lock(&BTRFS_I(inode
)->lock
);
1596 BTRFS_I(inode
)->outstanding_extents
--;
1597 spin_unlock(&BTRFS_I(inode
)->lock
);
1602 * We have to add up either side to figure out how many extents were
1603 * accounted for before we merged into one big extent. If the number of
1604 * extents we accounted for is <= the amount we need for the new range
1605 * then we can return, otherwise drop. Think of it like this
1609 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1610 * need 2 outstanding extents, on one side we have 1 and the other side
1611 * we have 1 so they are == and we can return. But in this case
1613 * [MAX_SIZE+4k][MAX_SIZE+4k]
1615 * Each range on their own accounts for 2 extents, but merged together
1616 * they are only 3 extents worth of accounting, so we need to drop in
1619 old_size
= other
->end
- other
->start
+ 1;
1620 num_extents
= div64_u64(old_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1621 BTRFS_MAX_EXTENT_SIZE
);
1622 old_size
= new->end
- new->start
+ 1;
1623 num_extents
+= div64_u64(old_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1624 BTRFS_MAX_EXTENT_SIZE
);
1626 if (div64_u64(new_size
+ BTRFS_MAX_EXTENT_SIZE
- 1,
1627 BTRFS_MAX_EXTENT_SIZE
) >= num_extents
)
1630 spin_lock(&BTRFS_I(inode
)->lock
);
1631 BTRFS_I(inode
)->outstanding_extents
--;
1632 spin_unlock(&BTRFS_I(inode
)->lock
);
1635 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1636 struct inode
*inode
)
1638 spin_lock(&root
->delalloc_lock
);
1639 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1640 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1641 &root
->delalloc_inodes
);
1642 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1643 &BTRFS_I(inode
)->runtime_flags
);
1644 root
->nr_delalloc_inodes
++;
1645 if (root
->nr_delalloc_inodes
== 1) {
1646 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1647 BUG_ON(!list_empty(&root
->delalloc_root
));
1648 list_add_tail(&root
->delalloc_root
,
1649 &root
->fs_info
->delalloc_roots
);
1650 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1653 spin_unlock(&root
->delalloc_lock
);
1656 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1657 struct inode
*inode
)
1659 spin_lock(&root
->delalloc_lock
);
1660 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1661 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1662 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1663 &BTRFS_I(inode
)->runtime_flags
);
1664 root
->nr_delalloc_inodes
--;
1665 if (!root
->nr_delalloc_inodes
) {
1666 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1667 BUG_ON(list_empty(&root
->delalloc_root
));
1668 list_del_init(&root
->delalloc_root
);
1669 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1672 spin_unlock(&root
->delalloc_lock
);
1676 * extent_io.c set_bit_hook, used to track delayed allocation
1677 * bytes in this file, and to maintain the list of inodes that
1678 * have pending delalloc work to be done.
1680 static void btrfs_set_bit_hook(struct inode
*inode
,
1681 struct extent_state
*state
, unsigned *bits
)
1684 if ((*bits
& EXTENT_DEFRAG
) && !(*bits
& EXTENT_DELALLOC
))
1687 * set_bit and clear bit hooks normally require _irqsave/restore
1688 * but in this case, we are only testing for the DELALLOC
1689 * bit, which is only set or cleared with irqs on
1691 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1692 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1693 u64 len
= state
->end
+ 1 - state
->start
;
1694 bool do_list
= !btrfs_is_free_space_inode(inode
);
1696 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1697 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1699 spin_lock(&BTRFS_I(inode
)->lock
);
1700 BTRFS_I(inode
)->outstanding_extents
++;
1701 spin_unlock(&BTRFS_I(inode
)->lock
);
1704 /* For sanity tests */
1705 if (btrfs_test_is_dummy_root(root
))
1708 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1709 root
->fs_info
->delalloc_batch
);
1710 spin_lock(&BTRFS_I(inode
)->lock
);
1711 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1712 if (*bits
& EXTENT_DEFRAG
)
1713 BTRFS_I(inode
)->defrag_bytes
+= len
;
1714 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1715 &BTRFS_I(inode
)->runtime_flags
))
1716 btrfs_add_delalloc_inodes(root
, inode
);
1717 spin_unlock(&BTRFS_I(inode
)->lock
);
1722 * extent_io.c clear_bit_hook, see set_bit_hook for why
1724 static void btrfs_clear_bit_hook(struct inode
*inode
,
1725 struct extent_state
*state
,
1728 u64 len
= state
->end
+ 1 - state
->start
;
1729 u64 num_extents
= div64_u64(len
+ BTRFS_MAX_EXTENT_SIZE
-1,
1730 BTRFS_MAX_EXTENT_SIZE
);
1732 spin_lock(&BTRFS_I(inode
)->lock
);
1733 if ((state
->state
& EXTENT_DEFRAG
) && (*bits
& EXTENT_DEFRAG
))
1734 BTRFS_I(inode
)->defrag_bytes
-= len
;
1735 spin_unlock(&BTRFS_I(inode
)->lock
);
1738 * set_bit and clear bit hooks normally require _irqsave/restore
1739 * but in this case, we are only testing for the DELALLOC
1740 * bit, which is only set or cleared with irqs on
1742 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1743 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1744 bool do_list
= !btrfs_is_free_space_inode(inode
);
1746 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1747 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1748 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1749 spin_lock(&BTRFS_I(inode
)->lock
);
1750 BTRFS_I(inode
)->outstanding_extents
-= num_extents
;
1751 spin_unlock(&BTRFS_I(inode
)->lock
);
1755 * We don't reserve metadata space for space cache inodes so we
1756 * don't need to call dellalloc_release_metadata if there is an
1759 if (*bits
& EXTENT_DO_ACCOUNTING
&&
1760 root
!= root
->fs_info
->tree_root
)
1761 btrfs_delalloc_release_metadata(inode
, len
);
1763 /* For sanity tests. */
1764 if (btrfs_test_is_dummy_root(root
))
1767 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1768 && do_list
&& !(state
->state
& EXTENT_NORESERVE
))
1769 btrfs_free_reserved_data_space(inode
, len
);
1771 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1772 root
->fs_info
->delalloc_batch
);
1773 spin_lock(&BTRFS_I(inode
)->lock
);
1774 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1775 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1776 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1777 &BTRFS_I(inode
)->runtime_flags
))
1778 btrfs_del_delalloc_inode(root
, inode
);
1779 spin_unlock(&BTRFS_I(inode
)->lock
);
1784 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1785 * we don't create bios that span stripes or chunks
1787 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1788 size_t size
, struct bio
*bio
,
1789 unsigned long bio_flags
)
1791 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1792 u64 logical
= (u64
)bio
->bi_iter
.bi_sector
<< 9;
1797 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1800 length
= bio
->bi_iter
.bi_size
;
1801 map_length
= length
;
1802 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1803 &map_length
, NULL
, 0);
1804 /* Will always return 0 with map_multi == NULL */
1806 if (map_length
< length
+ size
)
1812 * in order to insert checksums into the metadata in large chunks,
1813 * we wait until bio submission time. All the pages in the bio are
1814 * checksummed and sums are attached onto the ordered extent record.
1816 * At IO completion time the cums attached on the ordered extent record
1817 * are inserted into the btree
1819 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1820 struct bio
*bio
, int mirror_num
,
1821 unsigned long bio_flags
,
1824 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1827 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1828 BUG_ON(ret
); /* -ENOMEM */
1833 * in order to insert checksums into the metadata in large chunks,
1834 * we wait until bio submission time. All the pages in the bio are
1835 * checksummed and sums are attached onto the ordered extent record.
1837 * At IO completion time the cums attached on the ordered extent record
1838 * are inserted into the btree
1840 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1841 int mirror_num
, unsigned long bio_flags
,
1844 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1847 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1849 bio_endio(bio
, ret
);
1854 * extent_io.c submission hook. This does the right thing for csum calculation
1855 * on write, or reading the csums from the tree before a read
1857 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1858 int mirror_num
, unsigned long bio_flags
,
1861 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1865 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1867 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1869 if (btrfs_is_free_space_inode(inode
))
1872 if (!(rw
& REQ_WRITE
)) {
1873 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1877 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1878 ret
= btrfs_submit_compressed_read(inode
, bio
,
1882 } else if (!skip_sum
) {
1883 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1888 } else if (async
&& !skip_sum
) {
1889 /* csum items have already been cloned */
1890 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1892 /* we're doing a write, do the async checksumming */
1893 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1894 inode
, rw
, bio
, mirror_num
,
1895 bio_flags
, bio_offset
,
1896 __btrfs_submit_bio_start
,
1897 __btrfs_submit_bio_done
);
1899 } else if (!skip_sum
) {
1900 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1906 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1910 bio_endio(bio
, ret
);
1915 * given a list of ordered sums record them in the inode. This happens
1916 * at IO completion time based on sums calculated at bio submission time.
1918 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1919 struct inode
*inode
, u64 file_offset
,
1920 struct list_head
*list
)
1922 struct btrfs_ordered_sum
*sum
;
1924 list_for_each_entry(sum
, list
, list
) {
1925 trans
->adding_csums
= 1;
1926 btrfs_csum_file_blocks(trans
,
1927 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1928 trans
->adding_csums
= 0;
1933 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1934 struct extent_state
**cached_state
)
1936 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1937 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1938 cached_state
, GFP_NOFS
);
1941 /* see btrfs_writepage_start_hook for details on why this is required */
1942 struct btrfs_writepage_fixup
{
1944 struct btrfs_work work
;
1947 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1949 struct btrfs_writepage_fixup
*fixup
;
1950 struct btrfs_ordered_extent
*ordered
;
1951 struct extent_state
*cached_state
= NULL
;
1953 struct inode
*inode
;
1958 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1962 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1963 ClearPageChecked(page
);
1967 inode
= page
->mapping
->host
;
1968 page_start
= page_offset(page
);
1969 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1971 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1974 /* already ordered? We're done */
1975 if (PagePrivate2(page
))
1978 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1980 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1981 page_end
, &cached_state
, GFP_NOFS
);
1983 btrfs_start_ordered_extent(inode
, ordered
, 1);
1984 btrfs_put_ordered_extent(ordered
);
1988 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1990 mapping_set_error(page
->mapping
, ret
);
1991 end_extent_writepage(page
, ret
, page_start
, page_end
);
1992 ClearPageChecked(page
);
1996 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1997 ClearPageChecked(page
);
1998 set_page_dirty(page
);
2000 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
2001 &cached_state
, GFP_NOFS
);
2004 page_cache_release(page
);
2009 * There are a few paths in the higher layers of the kernel that directly
2010 * set the page dirty bit without asking the filesystem if it is a
2011 * good idea. This causes problems because we want to make sure COW
2012 * properly happens and the data=ordered rules are followed.
2014 * In our case any range that doesn't have the ORDERED bit set
2015 * hasn't been properly setup for IO. We kick off an async process
2016 * to fix it up. The async helper will wait for ordered extents, set
2017 * the delalloc bit and make it safe to write the page.
2019 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
2021 struct inode
*inode
= page
->mapping
->host
;
2022 struct btrfs_writepage_fixup
*fixup
;
2023 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2025 /* this page is properly in the ordered list */
2026 if (TestClearPagePrivate2(page
))
2029 if (PageChecked(page
))
2032 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
2036 SetPageChecked(page
);
2037 page_cache_get(page
);
2038 btrfs_init_work(&fixup
->work
, btrfs_fixup_helper
,
2039 btrfs_writepage_fixup_worker
, NULL
, NULL
);
2041 btrfs_queue_work(root
->fs_info
->fixup_workers
, &fixup
->work
);
2045 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
2046 struct inode
*inode
, u64 file_pos
,
2047 u64 disk_bytenr
, u64 disk_num_bytes
,
2048 u64 num_bytes
, u64 ram_bytes
,
2049 u8 compression
, u8 encryption
,
2050 u16 other_encoding
, int extent_type
)
2052 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2053 struct btrfs_file_extent_item
*fi
;
2054 struct btrfs_path
*path
;
2055 struct extent_buffer
*leaf
;
2056 struct btrfs_key ins
;
2057 int extent_inserted
= 0;
2060 path
= btrfs_alloc_path();
2065 * we may be replacing one extent in the tree with another.
2066 * The new extent is pinned in the extent map, and we don't want
2067 * to drop it from the cache until it is completely in the btree.
2069 * So, tell btrfs_drop_extents to leave this extent in the cache.
2070 * the caller is expected to unpin it and allow it to be merged
2073 ret
= __btrfs_drop_extents(trans
, root
, inode
, path
, file_pos
,
2074 file_pos
+ num_bytes
, NULL
, 0,
2075 1, sizeof(*fi
), &extent_inserted
);
2079 if (!extent_inserted
) {
2080 ins
.objectid
= btrfs_ino(inode
);
2081 ins
.offset
= file_pos
;
2082 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
2084 path
->leave_spinning
= 1;
2085 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
,
2090 leaf
= path
->nodes
[0];
2091 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2092 struct btrfs_file_extent_item
);
2093 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
2094 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
2095 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
2096 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
2097 btrfs_set_file_extent_offset(leaf
, fi
, 0);
2098 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
2099 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
2100 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
2101 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
2102 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
2104 btrfs_mark_buffer_dirty(leaf
);
2105 btrfs_release_path(path
);
2107 inode_add_bytes(inode
, num_bytes
);
2109 ins
.objectid
= disk_bytenr
;
2110 ins
.offset
= disk_num_bytes
;
2111 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
2112 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
2113 root
->root_key
.objectid
,
2114 btrfs_ino(inode
), file_pos
, &ins
);
2116 btrfs_free_path(path
);
2121 /* snapshot-aware defrag */
2122 struct sa_defrag_extent_backref
{
2123 struct rb_node node
;
2124 struct old_sa_defrag_extent
*old
;
2133 struct old_sa_defrag_extent
{
2134 struct list_head list
;
2135 struct new_sa_defrag_extent
*new;
2144 struct new_sa_defrag_extent
{
2145 struct rb_root root
;
2146 struct list_head head
;
2147 struct btrfs_path
*path
;
2148 struct inode
*inode
;
2156 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
2157 struct sa_defrag_extent_backref
*b2
)
2159 if (b1
->root_id
< b2
->root_id
)
2161 else if (b1
->root_id
> b2
->root_id
)
2164 if (b1
->inum
< b2
->inum
)
2166 else if (b1
->inum
> b2
->inum
)
2169 if (b1
->file_pos
< b2
->file_pos
)
2171 else if (b1
->file_pos
> b2
->file_pos
)
2175 * [------------------------------] ===> (a range of space)
2176 * |<--->| |<---->| =============> (fs/file tree A)
2177 * |<---------------------------->| ===> (fs/file tree B)
2179 * A range of space can refer to two file extents in one tree while
2180 * refer to only one file extent in another tree.
2182 * So we may process a disk offset more than one time(two extents in A)
2183 * and locate at the same extent(one extent in B), then insert two same
2184 * backrefs(both refer to the extent in B).
2189 static void backref_insert(struct rb_root
*root
,
2190 struct sa_defrag_extent_backref
*backref
)
2192 struct rb_node
**p
= &root
->rb_node
;
2193 struct rb_node
*parent
= NULL
;
2194 struct sa_defrag_extent_backref
*entry
;
2199 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2201 ret
= backref_comp(backref
, entry
);
2205 p
= &(*p
)->rb_right
;
2208 rb_link_node(&backref
->node
, parent
, p
);
2209 rb_insert_color(&backref
->node
, root
);
2213 * Note the backref might has changed, and in this case we just return 0.
2215 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2218 struct btrfs_file_extent_item
*extent
;
2219 struct btrfs_fs_info
*fs_info
;
2220 struct old_sa_defrag_extent
*old
= ctx
;
2221 struct new_sa_defrag_extent
*new = old
->new;
2222 struct btrfs_path
*path
= new->path
;
2223 struct btrfs_key key
;
2224 struct btrfs_root
*root
;
2225 struct sa_defrag_extent_backref
*backref
;
2226 struct extent_buffer
*leaf
;
2227 struct inode
*inode
= new->inode
;
2233 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2234 inum
== btrfs_ino(inode
))
2237 key
.objectid
= root_id
;
2238 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2239 key
.offset
= (u64
)-1;
2241 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2242 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2244 if (PTR_ERR(root
) == -ENOENT
)
2247 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2248 inum
, offset
, root_id
);
2249 return PTR_ERR(root
);
2252 key
.objectid
= inum
;
2253 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2254 if (offset
> (u64
)-1 << 32)
2257 key
.offset
= offset
;
2259 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2260 if (WARN_ON(ret
< 0))
2267 leaf
= path
->nodes
[0];
2268 slot
= path
->slots
[0];
2270 if (slot
>= btrfs_header_nritems(leaf
)) {
2271 ret
= btrfs_next_leaf(root
, path
);
2274 } else if (ret
> 0) {
2283 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2285 if (key
.objectid
> inum
)
2288 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2291 extent
= btrfs_item_ptr(leaf
, slot
,
2292 struct btrfs_file_extent_item
);
2294 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2298 * 'offset' refers to the exact key.offset,
2299 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2300 * (key.offset - extent_offset).
2302 if (key
.offset
!= offset
)
2305 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2306 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2308 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2309 old
->len
|| extent_offset
+ num_bytes
<=
2310 old
->extent_offset
+ old
->offset
)
2315 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2321 backref
->root_id
= root_id
;
2322 backref
->inum
= inum
;
2323 backref
->file_pos
= offset
;
2324 backref
->num_bytes
= num_bytes
;
2325 backref
->extent_offset
= extent_offset
;
2326 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2328 backref_insert(&new->root
, backref
);
2331 btrfs_release_path(path
);
2336 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2337 struct new_sa_defrag_extent
*new)
2339 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2340 struct old_sa_defrag_extent
*old
, *tmp
;
2345 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2346 ret
= iterate_inodes_from_logical(old
->bytenr
+
2347 old
->extent_offset
, fs_info
,
2348 path
, record_one_backref
,
2350 if (ret
< 0 && ret
!= -ENOENT
)
2353 /* no backref to be processed for this extent */
2355 list_del(&old
->list
);
2360 if (list_empty(&new->head
))
2366 static int relink_is_mergable(struct extent_buffer
*leaf
,
2367 struct btrfs_file_extent_item
*fi
,
2368 struct new_sa_defrag_extent
*new)
2370 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != new->bytenr
)
2373 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2376 if (btrfs_file_extent_compression(leaf
, fi
) != new->compress_type
)
2379 if (btrfs_file_extent_encryption(leaf
, fi
) ||
2380 btrfs_file_extent_other_encoding(leaf
, fi
))
2387 * Note the backref might has changed, and in this case we just return 0.
2389 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2390 struct sa_defrag_extent_backref
*prev
,
2391 struct sa_defrag_extent_backref
*backref
)
2393 struct btrfs_file_extent_item
*extent
;
2394 struct btrfs_file_extent_item
*item
;
2395 struct btrfs_ordered_extent
*ordered
;
2396 struct btrfs_trans_handle
*trans
;
2397 struct btrfs_fs_info
*fs_info
;
2398 struct btrfs_root
*root
;
2399 struct btrfs_key key
;
2400 struct extent_buffer
*leaf
;
2401 struct old_sa_defrag_extent
*old
= backref
->old
;
2402 struct new_sa_defrag_extent
*new = old
->new;
2403 struct inode
*src_inode
= new->inode
;
2404 struct inode
*inode
;
2405 struct extent_state
*cached
= NULL
;
2414 if (prev
&& prev
->root_id
== backref
->root_id
&&
2415 prev
->inum
== backref
->inum
&&
2416 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2419 /* step 1: get root */
2420 key
.objectid
= backref
->root_id
;
2421 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2422 key
.offset
= (u64
)-1;
2424 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2425 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2427 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2429 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2430 if (PTR_ERR(root
) == -ENOENT
)
2432 return PTR_ERR(root
);
2435 if (btrfs_root_readonly(root
)) {
2436 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2440 /* step 2: get inode */
2441 key
.objectid
= backref
->inum
;
2442 key
.type
= BTRFS_INODE_ITEM_KEY
;
2445 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2446 if (IS_ERR(inode
)) {
2447 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2451 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2453 /* step 3: relink backref */
2454 lock_start
= backref
->file_pos
;
2455 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2456 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2459 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2461 btrfs_put_ordered_extent(ordered
);
2465 trans
= btrfs_join_transaction(root
);
2466 if (IS_ERR(trans
)) {
2467 ret
= PTR_ERR(trans
);
2471 key
.objectid
= backref
->inum
;
2472 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2473 key
.offset
= backref
->file_pos
;
2475 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2478 } else if (ret
> 0) {
2483 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2484 struct btrfs_file_extent_item
);
2486 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2487 backref
->generation
)
2490 btrfs_release_path(path
);
2492 start
= backref
->file_pos
;
2493 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2494 start
+= old
->extent_offset
+ old
->offset
-
2495 backref
->extent_offset
;
2497 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2498 old
->extent_offset
+ old
->offset
+ old
->len
);
2499 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2501 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2506 key
.objectid
= btrfs_ino(inode
);
2507 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2510 path
->leave_spinning
= 1;
2512 struct btrfs_file_extent_item
*fi
;
2514 struct btrfs_key found_key
;
2516 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 0, 1);
2521 leaf
= path
->nodes
[0];
2522 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2524 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2525 struct btrfs_file_extent_item
);
2526 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2528 if (extent_len
+ found_key
.offset
== start
&&
2529 relink_is_mergable(leaf
, fi
, new)) {
2530 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2532 btrfs_mark_buffer_dirty(leaf
);
2533 inode_add_bytes(inode
, len
);
2539 btrfs_release_path(path
);
2544 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2547 btrfs_abort_transaction(trans
, root
, ret
);
2551 leaf
= path
->nodes
[0];
2552 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2553 struct btrfs_file_extent_item
);
2554 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2555 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2556 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2557 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2558 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2559 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2560 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2561 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2562 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2563 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2565 btrfs_mark_buffer_dirty(leaf
);
2566 inode_add_bytes(inode
, len
);
2567 btrfs_release_path(path
);
2569 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2571 backref
->root_id
, backref
->inum
,
2572 new->file_pos
, 0); /* start - extent_offset */
2574 btrfs_abort_transaction(trans
, root
, ret
);
2580 btrfs_release_path(path
);
2581 path
->leave_spinning
= 0;
2582 btrfs_end_transaction(trans
, root
);
2584 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2590 static void free_sa_defrag_extent(struct new_sa_defrag_extent
*new)
2592 struct old_sa_defrag_extent
*old
, *tmp
;
2597 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2598 list_del(&old
->list
);
2604 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2606 struct btrfs_path
*path
;
2607 struct sa_defrag_extent_backref
*backref
;
2608 struct sa_defrag_extent_backref
*prev
= NULL
;
2609 struct inode
*inode
;
2610 struct btrfs_root
*root
;
2611 struct rb_node
*node
;
2615 root
= BTRFS_I(inode
)->root
;
2617 path
= btrfs_alloc_path();
2621 if (!record_extent_backrefs(path
, new)) {
2622 btrfs_free_path(path
);
2625 btrfs_release_path(path
);
2628 node
= rb_first(&new->root
);
2631 rb_erase(node
, &new->root
);
2633 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2635 ret
= relink_extent_backref(path
, prev
, backref
);
2648 btrfs_free_path(path
);
2650 free_sa_defrag_extent(new);
2652 atomic_dec(&root
->fs_info
->defrag_running
);
2653 wake_up(&root
->fs_info
->transaction_wait
);
2656 static struct new_sa_defrag_extent
*
2657 record_old_file_extents(struct inode
*inode
,
2658 struct btrfs_ordered_extent
*ordered
)
2660 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2661 struct btrfs_path
*path
;
2662 struct btrfs_key key
;
2663 struct old_sa_defrag_extent
*old
;
2664 struct new_sa_defrag_extent
*new;
2667 new = kmalloc(sizeof(*new), GFP_NOFS
);
2672 new->file_pos
= ordered
->file_offset
;
2673 new->len
= ordered
->len
;
2674 new->bytenr
= ordered
->start
;
2675 new->disk_len
= ordered
->disk_len
;
2676 new->compress_type
= ordered
->compress_type
;
2677 new->root
= RB_ROOT
;
2678 INIT_LIST_HEAD(&new->head
);
2680 path
= btrfs_alloc_path();
2684 key
.objectid
= btrfs_ino(inode
);
2685 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2686 key
.offset
= new->file_pos
;
2688 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2691 if (ret
> 0 && path
->slots
[0] > 0)
2694 /* find out all the old extents for the file range */
2696 struct btrfs_file_extent_item
*extent
;
2697 struct extent_buffer
*l
;
2706 slot
= path
->slots
[0];
2708 if (slot
>= btrfs_header_nritems(l
)) {
2709 ret
= btrfs_next_leaf(root
, path
);
2717 btrfs_item_key_to_cpu(l
, &key
, slot
);
2719 if (key
.objectid
!= btrfs_ino(inode
))
2721 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2723 if (key
.offset
>= new->file_pos
+ new->len
)
2726 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2728 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2729 if (key
.offset
+ num_bytes
< new->file_pos
)
2732 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2736 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2738 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2742 offset
= max(new->file_pos
, key
.offset
);
2743 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2745 old
->bytenr
= disk_bytenr
;
2746 old
->extent_offset
= extent_offset
;
2747 old
->offset
= offset
- key
.offset
;
2748 old
->len
= end
- offset
;
2751 list_add_tail(&old
->list
, &new->head
);
2757 btrfs_free_path(path
);
2758 atomic_inc(&root
->fs_info
->defrag_running
);
2763 btrfs_free_path(path
);
2765 free_sa_defrag_extent(new);
2769 static void btrfs_release_delalloc_bytes(struct btrfs_root
*root
,
2772 struct btrfs_block_group_cache
*cache
;
2774 cache
= btrfs_lookup_block_group(root
->fs_info
, start
);
2777 spin_lock(&cache
->lock
);
2778 cache
->delalloc_bytes
-= len
;
2779 spin_unlock(&cache
->lock
);
2781 btrfs_put_block_group(cache
);
2784 /* as ordered data IO finishes, this gets called so we can finish
2785 * an ordered extent if the range of bytes in the file it covers are
2788 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2790 struct inode
*inode
= ordered_extent
->inode
;
2791 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2792 struct btrfs_trans_handle
*trans
= NULL
;
2793 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2794 struct extent_state
*cached_state
= NULL
;
2795 struct new_sa_defrag_extent
*new = NULL
;
2796 int compress_type
= 0;
2798 u64 logical_len
= ordered_extent
->len
;
2800 bool truncated
= false;
2802 nolock
= btrfs_is_free_space_inode(inode
);
2804 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2809 btrfs_free_io_failure_record(inode
, ordered_extent
->file_offset
,
2810 ordered_extent
->file_offset
+
2811 ordered_extent
->len
- 1);
2813 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2815 logical_len
= ordered_extent
->truncated_len
;
2816 /* Truncated the entire extent, don't bother adding */
2821 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2822 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2823 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2825 trans
= btrfs_join_transaction_nolock(root
);
2827 trans
= btrfs_join_transaction(root
);
2828 if (IS_ERR(trans
)) {
2829 ret
= PTR_ERR(trans
);
2833 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2834 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2835 if (ret
) /* -ENOMEM or corruption */
2836 btrfs_abort_transaction(trans
, root
, ret
);
2840 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2841 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2844 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2845 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2846 EXTENT_DEFRAG
, 1, cached_state
);
2848 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2849 if (0 && last_snapshot
>= BTRFS_I(inode
)->generation
)
2850 /* the inode is shared */
2851 new = record_old_file_extents(inode
, ordered_extent
);
2853 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2854 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2855 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2859 trans
= btrfs_join_transaction_nolock(root
);
2861 trans
= btrfs_join_transaction(root
);
2862 if (IS_ERR(trans
)) {
2863 ret
= PTR_ERR(trans
);
2868 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2870 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2871 compress_type
= ordered_extent
->compress_type
;
2872 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2873 BUG_ON(compress_type
);
2874 ret
= btrfs_mark_extent_written(trans
, inode
,
2875 ordered_extent
->file_offset
,
2876 ordered_extent
->file_offset
+
2879 BUG_ON(root
== root
->fs_info
->tree_root
);
2880 ret
= insert_reserved_file_extent(trans
, inode
,
2881 ordered_extent
->file_offset
,
2882 ordered_extent
->start
,
2883 ordered_extent
->disk_len
,
2884 logical_len
, logical_len
,
2885 compress_type
, 0, 0,
2886 BTRFS_FILE_EXTENT_REG
);
2888 btrfs_release_delalloc_bytes(root
,
2889 ordered_extent
->start
,
2890 ordered_extent
->disk_len
);
2892 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2893 ordered_extent
->file_offset
, ordered_extent
->len
,
2896 btrfs_abort_transaction(trans
, root
, ret
);
2900 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2901 &ordered_extent
->list
);
2903 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2904 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2905 if (ret
) { /* -ENOMEM or corruption */
2906 btrfs_abort_transaction(trans
, root
, ret
);
2911 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2912 ordered_extent
->file_offset
+
2913 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2915 if (root
!= root
->fs_info
->tree_root
)
2916 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2918 btrfs_end_transaction(trans
, root
);
2920 if (ret
|| truncated
) {
2924 start
= ordered_extent
->file_offset
+ logical_len
;
2926 start
= ordered_extent
->file_offset
;
2927 end
= ordered_extent
->file_offset
+ ordered_extent
->len
- 1;
2928 clear_extent_uptodate(io_tree
, start
, end
, NULL
, GFP_NOFS
);
2930 /* Drop the cache for the part of the extent we didn't write. */
2931 btrfs_drop_extent_cache(inode
, start
, end
, 0);
2934 * If the ordered extent had an IOERR or something else went
2935 * wrong we need to return the space for this ordered extent
2936 * back to the allocator. We only free the extent in the
2937 * truncated case if we didn't write out the extent at all.
2939 if ((ret
|| !logical_len
) &&
2940 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2941 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2942 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2943 ordered_extent
->disk_len
, 1);
2948 * This needs to be done to make sure anybody waiting knows we are done
2949 * updating everything for this ordered extent.
2951 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2953 /* for snapshot-aware defrag */
2956 free_sa_defrag_extent(new);
2957 atomic_dec(&root
->fs_info
->defrag_running
);
2959 relink_file_extents(new);
2964 btrfs_put_ordered_extent(ordered_extent
);
2965 /* once for the tree */
2966 btrfs_put_ordered_extent(ordered_extent
);
2971 static void finish_ordered_fn(struct btrfs_work
*work
)
2973 struct btrfs_ordered_extent
*ordered_extent
;
2974 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2975 btrfs_finish_ordered_io(ordered_extent
);
2978 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2979 struct extent_state
*state
, int uptodate
)
2981 struct inode
*inode
= page
->mapping
->host
;
2982 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2983 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2984 struct btrfs_workqueue
*wq
;
2985 btrfs_work_func_t func
;
2987 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2989 ClearPagePrivate2(page
);
2990 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2991 end
- start
+ 1, uptodate
))
2994 if (btrfs_is_free_space_inode(inode
)) {
2995 wq
= root
->fs_info
->endio_freespace_worker
;
2996 func
= btrfs_freespace_write_helper
;
2998 wq
= root
->fs_info
->endio_write_workers
;
2999 func
= btrfs_endio_write_helper
;
3002 btrfs_init_work(&ordered_extent
->work
, func
, finish_ordered_fn
, NULL
,
3004 btrfs_queue_work(wq
, &ordered_extent
->work
);
3009 static int __readpage_endio_check(struct inode
*inode
,
3010 struct btrfs_io_bio
*io_bio
,
3011 int icsum
, struct page
*page
,
3012 int pgoff
, u64 start
, size_t len
)
3017 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
3018 DEFAULT_RATELIMIT_BURST
);
3020 csum_expected
= *(((u32
*)io_bio
->csum
) + icsum
);
3022 kaddr
= kmap_atomic(page
);
3023 csum
= btrfs_csum_data(kaddr
+ pgoff
, csum
, len
);
3024 btrfs_csum_final(csum
, (char *)&csum
);
3025 if (csum
!= csum_expected
)
3028 kunmap_atomic(kaddr
);
3031 if (__ratelimit(&_rs
))
3032 btrfs_warn(BTRFS_I(inode
)->root
->fs_info
,
3033 "csum failed ino %llu off %llu csum %u expected csum %u",
3034 btrfs_ino(inode
), start
, csum
, csum_expected
);
3035 memset(kaddr
+ pgoff
, 1, len
);
3036 flush_dcache_page(page
);
3037 kunmap_atomic(kaddr
);
3038 if (csum_expected
== 0)
3044 * when reads are done, we need to check csums to verify the data is correct
3045 * if there's a match, we allow the bio to finish. If not, the code in
3046 * extent_io.c will try to find good copies for us.
3048 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
3049 u64 phy_offset
, struct page
*page
,
3050 u64 start
, u64 end
, int mirror
)
3052 size_t offset
= start
- page_offset(page
);
3053 struct inode
*inode
= page
->mapping
->host
;
3054 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
3055 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3057 if (PageChecked(page
)) {
3058 ClearPageChecked(page
);
3062 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
3065 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
3066 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
3067 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
3072 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
3073 return __readpage_endio_check(inode
, io_bio
, phy_offset
, page
, offset
,
3074 start
, (size_t)(end
- start
+ 1));
3077 struct delayed_iput
{
3078 struct list_head list
;
3079 struct inode
*inode
;
3082 /* JDM: If this is fs-wide, why can't we add a pointer to
3083 * btrfs_inode instead and avoid the allocation? */
3084 void btrfs_add_delayed_iput(struct inode
*inode
)
3086 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
3087 struct delayed_iput
*delayed
;
3089 if (atomic_add_unless(&inode
->i_count
, -1, 1))
3092 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
3093 delayed
->inode
= inode
;
3095 spin_lock(&fs_info
->delayed_iput_lock
);
3096 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
3097 spin_unlock(&fs_info
->delayed_iput_lock
);
3100 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
3103 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3104 struct delayed_iput
*delayed
;
3107 spin_lock(&fs_info
->delayed_iput_lock
);
3108 empty
= list_empty(&fs_info
->delayed_iputs
);
3109 spin_unlock(&fs_info
->delayed_iput_lock
);
3113 spin_lock(&fs_info
->delayed_iput_lock
);
3114 list_splice_init(&fs_info
->delayed_iputs
, &list
);
3115 spin_unlock(&fs_info
->delayed_iput_lock
);
3117 while (!list_empty(&list
)) {
3118 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
3119 list_del(&delayed
->list
);
3120 iput(delayed
->inode
);
3126 * This is called in transaction commit time. If there are no orphan
3127 * files in the subvolume, it removes orphan item and frees block_rsv
3130 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
3131 struct btrfs_root
*root
)
3133 struct btrfs_block_rsv
*block_rsv
;
3136 if (atomic_read(&root
->orphan_inodes
) ||
3137 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
3140 spin_lock(&root
->orphan_lock
);
3141 if (atomic_read(&root
->orphan_inodes
)) {
3142 spin_unlock(&root
->orphan_lock
);
3146 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
3147 spin_unlock(&root
->orphan_lock
);
3151 block_rsv
= root
->orphan_block_rsv
;
3152 root
->orphan_block_rsv
= NULL
;
3153 spin_unlock(&root
->orphan_lock
);
3155 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
) &&
3156 btrfs_root_refs(&root
->root_item
) > 0) {
3157 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
3158 root
->root_key
.objectid
);
3160 btrfs_abort_transaction(trans
, root
, ret
);
3162 clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
,
3167 WARN_ON(block_rsv
->size
> 0);
3168 btrfs_free_block_rsv(root
, block_rsv
);
3173 * This creates an orphan entry for the given inode in case something goes
3174 * wrong in the middle of an unlink/truncate.
3176 * NOTE: caller of this function should reserve 5 units of metadata for
3179 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
3181 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3182 struct btrfs_block_rsv
*block_rsv
= NULL
;
3187 if (!root
->orphan_block_rsv
) {
3188 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
3193 spin_lock(&root
->orphan_lock
);
3194 if (!root
->orphan_block_rsv
) {
3195 root
->orphan_block_rsv
= block_rsv
;
3196 } else if (block_rsv
) {
3197 btrfs_free_block_rsv(root
, block_rsv
);
3201 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3202 &BTRFS_I(inode
)->runtime_flags
)) {
3205 * For proper ENOSPC handling, we should do orphan
3206 * cleanup when mounting. But this introduces backward
3207 * compatibility issue.
3209 if (!xchg(&root
->orphan_item_inserted
, 1))
3215 atomic_inc(&root
->orphan_inodes
);
3218 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3219 &BTRFS_I(inode
)->runtime_flags
))
3221 spin_unlock(&root
->orphan_lock
);
3223 /* grab metadata reservation from transaction handle */
3225 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
3226 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
3229 /* insert an orphan item to track this unlinked/truncated file */
3231 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
3233 atomic_dec(&root
->orphan_inodes
);
3235 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3236 &BTRFS_I(inode
)->runtime_flags
);
3237 btrfs_orphan_release_metadata(inode
);
3239 if (ret
!= -EEXIST
) {
3240 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3241 &BTRFS_I(inode
)->runtime_flags
);
3242 btrfs_abort_transaction(trans
, root
, ret
);
3249 /* insert an orphan item to track subvolume contains orphan files */
3251 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
3252 root
->root_key
.objectid
);
3253 if (ret
&& ret
!= -EEXIST
) {
3254 btrfs_abort_transaction(trans
, root
, ret
);
3262 * We have done the truncate/delete so we can go ahead and remove the orphan
3263 * item for this particular inode.
3265 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3266 struct inode
*inode
)
3268 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3269 int delete_item
= 0;
3270 int release_rsv
= 0;
3273 spin_lock(&root
->orphan_lock
);
3274 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3275 &BTRFS_I(inode
)->runtime_flags
))
3278 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3279 &BTRFS_I(inode
)->runtime_flags
))
3281 spin_unlock(&root
->orphan_lock
);
3284 atomic_dec(&root
->orphan_inodes
);
3286 ret
= btrfs_del_orphan_item(trans
, root
,
3291 btrfs_orphan_release_metadata(inode
);
3297 * this cleans up any orphans that may be left on the list from the last use
3300 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3302 struct btrfs_path
*path
;
3303 struct extent_buffer
*leaf
;
3304 struct btrfs_key key
, found_key
;
3305 struct btrfs_trans_handle
*trans
;
3306 struct inode
*inode
;
3307 u64 last_objectid
= 0;
3308 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3310 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3313 path
= btrfs_alloc_path();
3320 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3321 key
.type
= BTRFS_ORPHAN_ITEM_KEY
;
3322 key
.offset
= (u64
)-1;
3325 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3330 * if ret == 0 means we found what we were searching for, which
3331 * is weird, but possible, so only screw with path if we didn't
3332 * find the key and see if we have stuff that matches
3336 if (path
->slots
[0] == 0)
3341 /* pull out the item */
3342 leaf
= path
->nodes
[0];
3343 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3345 /* make sure the item matches what we want */
3346 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3348 if (found_key
.type
!= BTRFS_ORPHAN_ITEM_KEY
)
3351 /* release the path since we're done with it */
3352 btrfs_release_path(path
);
3355 * this is where we are basically btrfs_lookup, without the
3356 * crossing root thing. we store the inode number in the
3357 * offset of the orphan item.
3360 if (found_key
.offset
== last_objectid
) {
3361 btrfs_err(root
->fs_info
,
3362 "Error removing orphan entry, stopping orphan cleanup");
3367 last_objectid
= found_key
.offset
;
3369 found_key
.objectid
= found_key
.offset
;
3370 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3371 found_key
.offset
= 0;
3372 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3373 ret
= PTR_ERR_OR_ZERO(inode
);
3374 if (ret
&& ret
!= -ESTALE
)
3377 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3378 struct btrfs_root
*dead_root
;
3379 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3380 int is_dead_root
= 0;
3383 * this is an orphan in the tree root. Currently these
3384 * could come from 2 sources:
3385 * a) a snapshot deletion in progress
3386 * b) a free space cache inode
3387 * We need to distinguish those two, as the snapshot
3388 * orphan must not get deleted.
3389 * find_dead_roots already ran before us, so if this
3390 * is a snapshot deletion, we should find the root
3391 * in the dead_roots list
3393 spin_lock(&fs_info
->trans_lock
);
3394 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3396 if (dead_root
->root_key
.objectid
==
3397 found_key
.objectid
) {
3402 spin_unlock(&fs_info
->trans_lock
);
3404 /* prevent this orphan from being found again */
3405 key
.offset
= found_key
.objectid
- 1;
3410 * Inode is already gone but the orphan item is still there,
3411 * kill the orphan item.
3413 if (ret
== -ESTALE
) {
3414 trans
= btrfs_start_transaction(root
, 1);
3415 if (IS_ERR(trans
)) {
3416 ret
= PTR_ERR(trans
);
3419 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3420 found_key
.objectid
);
3421 ret
= btrfs_del_orphan_item(trans
, root
,
3422 found_key
.objectid
);
3423 btrfs_end_transaction(trans
, root
);
3430 * add this inode to the orphan list so btrfs_orphan_del does
3431 * the proper thing when we hit it
3433 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3434 &BTRFS_I(inode
)->runtime_flags
);
3435 atomic_inc(&root
->orphan_inodes
);
3437 /* if we have links, this was a truncate, lets do that */
3438 if (inode
->i_nlink
) {
3439 if (WARN_ON(!S_ISREG(inode
->i_mode
))) {
3445 /* 1 for the orphan item deletion. */
3446 trans
= btrfs_start_transaction(root
, 1);
3447 if (IS_ERR(trans
)) {
3449 ret
= PTR_ERR(trans
);
3452 ret
= btrfs_orphan_add(trans
, inode
);
3453 btrfs_end_transaction(trans
, root
);
3459 ret
= btrfs_truncate(inode
);
3461 btrfs_orphan_del(NULL
, inode
);
3466 /* this will do delete_inode and everything for us */
3471 /* release the path since we're done with it */
3472 btrfs_release_path(path
);
3474 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3476 if (root
->orphan_block_rsv
)
3477 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3480 if (root
->orphan_block_rsv
||
3481 test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED
, &root
->state
)) {
3482 trans
= btrfs_join_transaction(root
);
3484 btrfs_end_transaction(trans
, root
);
3488 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3490 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3494 btrfs_err(root
->fs_info
,
3495 "could not do orphan cleanup %d", ret
);
3496 btrfs_free_path(path
);
3501 * very simple check to peek ahead in the leaf looking for xattrs. If we
3502 * don't find any xattrs, we know there can't be any acls.
3504 * slot is the slot the inode is in, objectid is the objectid of the inode
3506 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3507 int slot
, u64 objectid
,
3508 int *first_xattr_slot
)
3510 u32 nritems
= btrfs_header_nritems(leaf
);
3511 struct btrfs_key found_key
;
3512 static u64 xattr_access
= 0;
3513 static u64 xattr_default
= 0;
3516 if (!xattr_access
) {
3517 xattr_access
= btrfs_name_hash(POSIX_ACL_XATTR_ACCESS
,
3518 strlen(POSIX_ACL_XATTR_ACCESS
));
3519 xattr_default
= btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT
,
3520 strlen(POSIX_ACL_XATTR_DEFAULT
));
3524 *first_xattr_slot
= -1;
3525 while (slot
< nritems
) {
3526 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3528 /* we found a different objectid, there must not be acls */
3529 if (found_key
.objectid
!= objectid
)
3532 /* we found an xattr, assume we've got an acl */
3533 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3534 if (*first_xattr_slot
== -1)
3535 *first_xattr_slot
= slot
;
3536 if (found_key
.offset
== xattr_access
||
3537 found_key
.offset
== xattr_default
)
3542 * we found a key greater than an xattr key, there can't
3543 * be any acls later on
3545 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3552 * it goes inode, inode backrefs, xattrs, extents,
3553 * so if there are a ton of hard links to an inode there can
3554 * be a lot of backrefs. Don't waste time searching too hard,
3555 * this is just an optimization
3560 /* we hit the end of the leaf before we found an xattr or
3561 * something larger than an xattr. We have to assume the inode
3564 if (*first_xattr_slot
== -1)
3565 *first_xattr_slot
= slot
;
3570 * read an inode from the btree into the in-memory inode
3572 static void btrfs_read_locked_inode(struct inode
*inode
)
3574 struct btrfs_path
*path
;
3575 struct extent_buffer
*leaf
;
3576 struct btrfs_inode_item
*inode_item
;
3577 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3578 struct btrfs_key location
;
3583 bool filled
= false;
3584 int first_xattr_slot
;
3586 ret
= btrfs_fill_inode(inode
, &rdev
);
3590 path
= btrfs_alloc_path();
3594 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3596 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3600 leaf
= path
->nodes
[0];
3605 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3606 struct btrfs_inode_item
);
3607 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3608 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3609 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3610 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3611 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3613 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->atime
);
3614 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->atime
);
3616 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->mtime
);
3617 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->mtime
);
3619 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, &inode_item
->ctime
);
3620 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, &inode_item
->ctime
);
3622 BTRFS_I(inode
)->i_otime
.tv_sec
=
3623 btrfs_timespec_sec(leaf
, &inode_item
->otime
);
3624 BTRFS_I(inode
)->i_otime
.tv_nsec
=
3625 btrfs_timespec_nsec(leaf
, &inode_item
->otime
);
3627 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3628 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3629 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3632 * If we were modified in the current generation and evicted from memory
3633 * and then re-read we need to do a full sync since we don't have any
3634 * idea about which extents were modified before we were evicted from
3637 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3638 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3639 &BTRFS_I(inode
)->runtime_flags
);
3641 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3642 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3644 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3646 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3647 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3651 if (inode
->i_nlink
!= 1 ||
3652 path
->slots
[0] >= btrfs_header_nritems(leaf
))
3655 btrfs_item_key_to_cpu(leaf
, &location
, path
->slots
[0]);
3656 if (location
.objectid
!= btrfs_ino(inode
))
3659 ptr
= btrfs_item_ptr_offset(leaf
, path
->slots
[0]);
3660 if (location
.type
== BTRFS_INODE_REF_KEY
) {
3661 struct btrfs_inode_ref
*ref
;
3663 ref
= (struct btrfs_inode_ref
*)ptr
;
3664 BTRFS_I(inode
)->dir_index
= btrfs_inode_ref_index(leaf
, ref
);
3665 } else if (location
.type
== BTRFS_INODE_EXTREF_KEY
) {
3666 struct btrfs_inode_extref
*extref
;
3668 extref
= (struct btrfs_inode_extref
*)ptr
;
3669 BTRFS_I(inode
)->dir_index
= btrfs_inode_extref_index(leaf
,
3674 * try to precache a NULL acl entry for files that don't have
3675 * any xattrs or acls
3677 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3678 btrfs_ino(inode
), &first_xattr_slot
);
3679 if (first_xattr_slot
!= -1) {
3680 path
->slots
[0] = first_xattr_slot
;
3681 ret
= btrfs_load_inode_props(inode
, path
);
3683 btrfs_err(root
->fs_info
,
3684 "error loading props for ino %llu (root %llu): %d",
3686 root
->root_key
.objectid
, ret
);
3688 btrfs_free_path(path
);
3691 cache_no_acl(inode
);
3693 switch (inode
->i_mode
& S_IFMT
) {
3695 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3696 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3697 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3698 inode
->i_fop
= &btrfs_file_operations
;
3699 inode
->i_op
= &btrfs_file_inode_operations
;
3702 inode
->i_fop
= &btrfs_dir_file_operations
;
3703 if (root
== root
->fs_info
->tree_root
)
3704 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3706 inode
->i_op
= &btrfs_dir_inode_operations
;
3709 inode
->i_op
= &btrfs_symlink_inode_operations
;
3710 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3711 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3714 inode
->i_op
= &btrfs_special_inode_operations
;
3715 init_special_inode(inode
, inode
->i_mode
, rdev
);
3719 btrfs_update_iflags(inode
);
3723 btrfs_free_path(path
);
3724 make_bad_inode(inode
);
3728 * given a leaf and an inode, copy the inode fields into the leaf
3730 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3731 struct extent_buffer
*leaf
,
3732 struct btrfs_inode_item
*item
,
3733 struct inode
*inode
)
3735 struct btrfs_map_token token
;
3737 btrfs_init_map_token(&token
);
3739 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3740 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3741 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3743 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3744 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3746 btrfs_set_token_timespec_sec(leaf
, &item
->atime
,
3747 inode
->i_atime
.tv_sec
, &token
);
3748 btrfs_set_token_timespec_nsec(leaf
, &item
->atime
,
3749 inode
->i_atime
.tv_nsec
, &token
);
3751 btrfs_set_token_timespec_sec(leaf
, &item
->mtime
,
3752 inode
->i_mtime
.tv_sec
, &token
);
3753 btrfs_set_token_timespec_nsec(leaf
, &item
->mtime
,
3754 inode
->i_mtime
.tv_nsec
, &token
);
3756 btrfs_set_token_timespec_sec(leaf
, &item
->ctime
,
3757 inode
->i_ctime
.tv_sec
, &token
);
3758 btrfs_set_token_timespec_nsec(leaf
, &item
->ctime
,
3759 inode
->i_ctime
.tv_nsec
, &token
);
3761 btrfs_set_token_timespec_sec(leaf
, &item
->otime
,
3762 BTRFS_I(inode
)->i_otime
.tv_sec
, &token
);
3763 btrfs_set_token_timespec_nsec(leaf
, &item
->otime
,
3764 BTRFS_I(inode
)->i_otime
.tv_nsec
, &token
);
3766 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3768 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3770 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3771 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3772 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3773 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3774 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3778 * copy everything in the in-memory inode into the btree.
3780 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3781 struct btrfs_root
*root
, struct inode
*inode
)
3783 struct btrfs_inode_item
*inode_item
;
3784 struct btrfs_path
*path
;
3785 struct extent_buffer
*leaf
;
3788 path
= btrfs_alloc_path();
3792 path
->leave_spinning
= 1;
3793 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3801 leaf
= path
->nodes
[0];
3802 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3803 struct btrfs_inode_item
);
3805 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3806 btrfs_mark_buffer_dirty(leaf
);
3807 btrfs_set_inode_last_trans(trans
, inode
);
3810 btrfs_free_path(path
);
3815 * copy everything in the in-memory inode into the btree.
3817 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3818 struct btrfs_root
*root
, struct inode
*inode
)
3823 * If the inode is a free space inode, we can deadlock during commit
3824 * if we put it into the delayed code.
3826 * The data relocation inode should also be directly updated
3829 if (!btrfs_is_free_space_inode(inode
)
3830 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
3831 && !root
->fs_info
->log_root_recovering
) {
3832 btrfs_update_root_times(trans
, root
);
3834 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3836 btrfs_set_inode_last_trans(trans
, inode
);
3840 return btrfs_update_inode_item(trans
, root
, inode
);
3843 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3844 struct btrfs_root
*root
,
3845 struct inode
*inode
)
3849 ret
= btrfs_update_inode(trans
, root
, inode
);
3851 return btrfs_update_inode_item(trans
, root
, inode
);
3856 * unlink helper that gets used here in inode.c and in the tree logging
3857 * recovery code. It remove a link in a directory with a given name, and
3858 * also drops the back refs in the inode to the directory
3860 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3861 struct btrfs_root
*root
,
3862 struct inode
*dir
, struct inode
*inode
,
3863 const char *name
, int name_len
)
3865 struct btrfs_path
*path
;
3867 struct extent_buffer
*leaf
;
3868 struct btrfs_dir_item
*di
;
3869 struct btrfs_key key
;
3871 u64 ino
= btrfs_ino(inode
);
3872 u64 dir_ino
= btrfs_ino(dir
);
3874 path
= btrfs_alloc_path();
3880 path
->leave_spinning
= 1;
3881 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3882 name
, name_len
, -1);
3891 leaf
= path
->nodes
[0];
3892 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3893 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3896 btrfs_release_path(path
);
3899 * If we don't have dir index, we have to get it by looking up
3900 * the inode ref, since we get the inode ref, remove it directly,
3901 * it is unnecessary to do delayed deletion.
3903 * But if we have dir index, needn't search inode ref to get it.
3904 * Since the inode ref is close to the inode item, it is better
3905 * that we delay to delete it, and just do this deletion when
3906 * we update the inode item.
3908 if (BTRFS_I(inode
)->dir_index
) {
3909 ret
= btrfs_delayed_delete_inode_ref(inode
);
3911 index
= BTRFS_I(inode
)->dir_index
;
3916 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3919 btrfs_info(root
->fs_info
,
3920 "failed to delete reference to %.*s, inode %llu parent %llu",
3921 name_len
, name
, ino
, dir_ino
);
3922 btrfs_abort_transaction(trans
, root
, ret
);
3926 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3928 btrfs_abort_transaction(trans
, root
, ret
);
3932 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
3934 if (ret
!= 0 && ret
!= -ENOENT
) {
3935 btrfs_abort_transaction(trans
, root
, ret
);
3939 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
3944 btrfs_abort_transaction(trans
, root
, ret
);
3946 btrfs_free_path(path
);
3950 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3951 inode_inc_iversion(inode
);
3952 inode_inc_iversion(dir
);
3953 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3954 ret
= btrfs_update_inode(trans
, root
, dir
);
3959 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3960 struct btrfs_root
*root
,
3961 struct inode
*dir
, struct inode
*inode
,
3962 const char *name
, int name_len
)
3965 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3968 ret
= btrfs_update_inode(trans
, root
, inode
);
3974 * helper to start transaction for unlink and rmdir.
3976 * unlink and rmdir are special in btrfs, they do not always free space, so
3977 * if we cannot make our reservations the normal way try and see if there is
3978 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3979 * allow the unlink to occur.
3981 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
3983 struct btrfs_trans_handle
*trans
;
3984 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3988 * 1 for the possible orphan item
3989 * 1 for the dir item
3990 * 1 for the dir index
3991 * 1 for the inode ref
3994 trans
= btrfs_start_transaction(root
, 5);
3995 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3998 if (PTR_ERR(trans
) == -ENOSPC
) {
3999 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 5);
4001 trans
= btrfs_start_transaction(root
, 0);
4004 ret
= btrfs_cond_migrate_bytes(root
->fs_info
,
4005 &root
->fs_info
->trans_block_rsv
,
4008 btrfs_end_transaction(trans
, root
);
4009 return ERR_PTR(ret
);
4011 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4012 trans
->bytes_reserved
= num_bytes
;
4017 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
4019 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4020 struct btrfs_trans_handle
*trans
;
4021 struct inode
*inode
= dentry
->d_inode
;
4024 trans
= __unlink_start_trans(dir
);
4026 return PTR_ERR(trans
);
4028 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
4030 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
4031 dentry
->d_name
.name
, dentry
->d_name
.len
);
4035 if (inode
->i_nlink
== 0) {
4036 ret
= btrfs_orphan_add(trans
, inode
);
4042 btrfs_end_transaction(trans
, root
);
4043 btrfs_btree_balance_dirty(root
);
4047 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
4048 struct btrfs_root
*root
,
4049 struct inode
*dir
, u64 objectid
,
4050 const char *name
, int name_len
)
4052 struct btrfs_path
*path
;
4053 struct extent_buffer
*leaf
;
4054 struct btrfs_dir_item
*di
;
4055 struct btrfs_key key
;
4058 u64 dir_ino
= btrfs_ino(dir
);
4060 path
= btrfs_alloc_path();
4064 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
4065 name
, name_len
, -1);
4066 if (IS_ERR_OR_NULL(di
)) {
4074 leaf
= path
->nodes
[0];
4075 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
4076 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
4077 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
4079 btrfs_abort_transaction(trans
, root
, ret
);
4082 btrfs_release_path(path
);
4084 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
4085 objectid
, root
->root_key
.objectid
,
4086 dir_ino
, &index
, name
, name_len
);
4088 if (ret
!= -ENOENT
) {
4089 btrfs_abort_transaction(trans
, root
, ret
);
4092 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
4094 if (IS_ERR_OR_NULL(di
)) {
4099 btrfs_abort_transaction(trans
, root
, ret
);
4103 leaf
= path
->nodes
[0];
4104 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
4105 btrfs_release_path(path
);
4108 btrfs_release_path(path
);
4110 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
4112 btrfs_abort_transaction(trans
, root
, ret
);
4116 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
4117 inode_inc_iversion(dir
);
4118 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
4119 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
4121 btrfs_abort_transaction(trans
, root
, ret
);
4123 btrfs_free_path(path
);
4127 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
4129 struct inode
*inode
= dentry
->d_inode
;
4131 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4132 struct btrfs_trans_handle
*trans
;
4134 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
4136 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
4139 trans
= __unlink_start_trans(dir
);
4141 return PTR_ERR(trans
);
4143 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
4144 err
= btrfs_unlink_subvol(trans
, root
, dir
,
4145 BTRFS_I(inode
)->location
.objectid
,
4146 dentry
->d_name
.name
,
4147 dentry
->d_name
.len
);
4151 err
= btrfs_orphan_add(trans
, inode
);
4155 /* now the directory is empty */
4156 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
4157 dentry
->d_name
.name
, dentry
->d_name
.len
);
4159 btrfs_i_size_write(inode
, 0);
4161 btrfs_end_transaction(trans
, root
);
4162 btrfs_btree_balance_dirty(root
);
4168 * this can truncate away extent items, csum items and directory items.
4169 * It starts at a high offset and removes keys until it can't find
4170 * any higher than new_size
4172 * csum items that cross the new i_size are truncated to the new size
4175 * min_type is the minimum key type to truncate down to. If set to 0, this
4176 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4178 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4179 struct btrfs_root
*root
,
4180 struct inode
*inode
,
4181 u64 new_size
, u32 min_type
)
4183 struct btrfs_path
*path
;
4184 struct extent_buffer
*leaf
;
4185 struct btrfs_file_extent_item
*fi
;
4186 struct btrfs_key key
;
4187 struct btrfs_key found_key
;
4188 u64 extent_start
= 0;
4189 u64 extent_num_bytes
= 0;
4190 u64 extent_offset
= 0;
4192 u64 last_size
= (u64
)-1;
4193 u32 found_type
= (u8
)-1;
4196 int pending_del_nr
= 0;
4197 int pending_del_slot
= 0;
4198 int extent_type
= -1;
4201 u64 ino
= btrfs_ino(inode
);
4203 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4205 path
= btrfs_alloc_path();
4211 * We want to drop from the next block forward in case this new size is
4212 * not block aligned since we will be keeping the last block of the
4213 * extent just the way it is.
4215 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4216 root
== root
->fs_info
->tree_root
)
4217 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
4218 root
->sectorsize
), (u64
)-1, 0);
4221 * This function is also used to drop the items in the log tree before
4222 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4223 * it is used to drop the loged items. So we shouldn't kill the delayed
4226 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4227 btrfs_kill_delayed_inode_items(inode
);
4230 key
.offset
= (u64
)-1;
4234 path
->leave_spinning
= 1;
4235 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4242 /* there are no items in the tree for us to truncate, we're
4245 if (path
->slots
[0] == 0)
4252 leaf
= path
->nodes
[0];
4253 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4254 found_type
= found_key
.type
;
4256 if (found_key
.objectid
!= ino
)
4259 if (found_type
< min_type
)
4262 item_end
= found_key
.offset
;
4263 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4264 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4265 struct btrfs_file_extent_item
);
4266 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4267 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4269 btrfs_file_extent_num_bytes(leaf
, fi
);
4270 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4271 item_end
+= btrfs_file_extent_inline_len(leaf
,
4272 path
->slots
[0], fi
);
4276 if (found_type
> min_type
) {
4279 if (item_end
< new_size
)
4281 if (found_key
.offset
>= new_size
)
4287 /* FIXME, shrink the extent if the ref count is only 1 */
4288 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4292 last_size
= found_key
.offset
;
4294 last_size
= new_size
;
4296 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4298 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4300 u64 orig_num_bytes
=
4301 btrfs_file_extent_num_bytes(leaf
, fi
);
4302 extent_num_bytes
= ALIGN(new_size
-
4305 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4307 num_dec
= (orig_num_bytes
-
4309 if (test_bit(BTRFS_ROOT_REF_COWS
,
4312 inode_sub_bytes(inode
, num_dec
);
4313 btrfs_mark_buffer_dirty(leaf
);
4316 btrfs_file_extent_disk_num_bytes(leaf
,
4318 extent_offset
= found_key
.offset
-
4319 btrfs_file_extent_offset(leaf
, fi
);
4321 /* FIXME blocksize != 4096 */
4322 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4323 if (extent_start
!= 0) {
4325 if (test_bit(BTRFS_ROOT_REF_COWS
,
4327 inode_sub_bytes(inode
, num_dec
);
4330 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4332 * we can't truncate inline items that have had
4336 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4337 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4338 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4339 u32 size
= new_size
- found_key
.offset
;
4341 if (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
))
4342 inode_sub_bytes(inode
, item_end
+ 1 -
4346 * update the ram bytes to properly reflect
4347 * the new size of our item
4349 btrfs_set_file_extent_ram_bytes(leaf
, fi
, size
);
4351 btrfs_file_extent_calc_inline_size(size
);
4352 btrfs_truncate_item(root
, path
, size
, 1);
4353 } else if (test_bit(BTRFS_ROOT_REF_COWS
,
4355 inode_sub_bytes(inode
, item_end
+ 1 -
4361 if (!pending_del_nr
) {
4362 /* no pending yet, add ourselves */
4363 pending_del_slot
= path
->slots
[0];
4365 } else if (pending_del_nr
&&
4366 path
->slots
[0] + 1 == pending_del_slot
) {
4367 /* hop on the pending chunk */
4369 pending_del_slot
= path
->slots
[0];
4377 (test_bit(BTRFS_ROOT_REF_COWS
, &root
->state
) ||
4378 root
== root
->fs_info
->tree_root
)) {
4379 btrfs_set_path_blocking(path
);
4380 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4381 extent_num_bytes
, 0,
4382 btrfs_header_owner(leaf
),
4383 ino
, extent_offset
, 0);
4387 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4390 if (path
->slots
[0] == 0 ||
4391 path
->slots
[0] != pending_del_slot
) {
4392 if (pending_del_nr
) {
4393 ret
= btrfs_del_items(trans
, root
, path
,
4397 btrfs_abort_transaction(trans
,
4403 btrfs_release_path(path
);
4410 if (pending_del_nr
) {
4411 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4414 btrfs_abort_transaction(trans
, root
, ret
);
4417 if (last_size
!= (u64
)-1 &&
4418 root
->root_key
.objectid
!= BTRFS_TREE_LOG_OBJECTID
)
4419 btrfs_ordered_update_i_size(inode
, last_size
, NULL
);
4420 btrfs_free_path(path
);
4425 * btrfs_truncate_page - read, zero a chunk and write a page
4426 * @inode - inode that we're zeroing
4427 * @from - the offset to start zeroing
4428 * @len - the length to zero, 0 to zero the entire range respective to the
4430 * @front - zero up to the offset instead of from the offset on
4432 * This will find the page for the "from" offset and cow the page and zero the
4433 * part we want to zero. This is used with truncate and hole punching.
4435 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4438 struct address_space
*mapping
= inode
->i_mapping
;
4439 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4440 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4441 struct btrfs_ordered_extent
*ordered
;
4442 struct extent_state
*cached_state
= NULL
;
4444 u32 blocksize
= root
->sectorsize
;
4445 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4446 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4448 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4453 if ((offset
& (blocksize
- 1)) == 0 &&
4454 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4456 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4461 page
= find_or_create_page(mapping
, index
, mask
);
4463 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4468 page_start
= page_offset(page
);
4469 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4471 if (!PageUptodate(page
)) {
4472 ret
= btrfs_readpage(NULL
, page
);
4474 if (page
->mapping
!= mapping
) {
4476 page_cache_release(page
);
4479 if (!PageUptodate(page
)) {
4484 wait_on_page_writeback(page
);
4486 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4487 set_page_extent_mapped(page
);
4489 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4491 unlock_extent_cached(io_tree
, page_start
, page_end
,
4492 &cached_state
, GFP_NOFS
);
4494 page_cache_release(page
);
4495 btrfs_start_ordered_extent(inode
, ordered
, 1);
4496 btrfs_put_ordered_extent(ordered
);
4500 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4501 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4502 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4503 0, 0, &cached_state
, GFP_NOFS
);
4505 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4508 unlock_extent_cached(io_tree
, page_start
, page_end
,
4509 &cached_state
, GFP_NOFS
);
4513 if (offset
!= PAGE_CACHE_SIZE
) {
4515 len
= PAGE_CACHE_SIZE
- offset
;
4518 memset(kaddr
, 0, offset
);
4520 memset(kaddr
+ offset
, 0, len
);
4521 flush_dcache_page(page
);
4524 ClearPageChecked(page
);
4525 set_page_dirty(page
);
4526 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4531 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4533 page_cache_release(page
);
4538 static int maybe_insert_hole(struct btrfs_root
*root
, struct inode
*inode
,
4539 u64 offset
, u64 len
)
4541 struct btrfs_trans_handle
*trans
;
4545 * Still need to make sure the inode looks like it's been updated so
4546 * that any holes get logged if we fsync.
4548 if (btrfs_fs_incompat(root
->fs_info
, NO_HOLES
)) {
4549 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
4550 BTRFS_I(inode
)->last_sub_trans
= root
->log_transid
;
4551 BTRFS_I(inode
)->last_log_commit
= root
->last_log_commit
;
4556 * 1 - for the one we're dropping
4557 * 1 - for the one we're adding
4558 * 1 - for updating the inode.
4560 trans
= btrfs_start_transaction(root
, 3);
4562 return PTR_ERR(trans
);
4564 ret
= btrfs_drop_extents(trans
, root
, inode
, offset
, offset
+ len
, 1);
4566 btrfs_abort_transaction(trans
, root
, ret
);
4567 btrfs_end_transaction(trans
, root
);
4571 ret
= btrfs_insert_file_extent(trans
, root
, btrfs_ino(inode
), offset
,
4572 0, 0, len
, 0, len
, 0, 0, 0);
4574 btrfs_abort_transaction(trans
, root
, ret
);
4576 btrfs_update_inode(trans
, root
, inode
);
4577 btrfs_end_transaction(trans
, root
);
4582 * This function puts in dummy file extents for the area we're creating a hole
4583 * for. So if we are truncating this file to a larger size we need to insert
4584 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4585 * the range between oldsize and size
4587 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4589 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4590 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4591 struct extent_map
*em
= NULL
;
4592 struct extent_state
*cached_state
= NULL
;
4593 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4594 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4595 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4602 * If our size started in the middle of a page we need to zero out the
4603 * rest of the page before we expand the i_size, otherwise we could
4604 * expose stale data.
4606 err
= btrfs_truncate_page(inode
, oldsize
, 0, 0);
4610 if (size
<= hole_start
)
4614 struct btrfs_ordered_extent
*ordered
;
4616 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4618 ordered
= btrfs_lookup_ordered_range(inode
, hole_start
,
4619 block_end
- hole_start
);
4622 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4623 &cached_state
, GFP_NOFS
);
4624 btrfs_start_ordered_extent(inode
, ordered
, 1);
4625 btrfs_put_ordered_extent(ordered
);
4628 cur_offset
= hole_start
;
4630 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4631 block_end
- cur_offset
, 0);
4637 last_byte
= min(extent_map_end(em
), block_end
);
4638 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4639 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4640 struct extent_map
*hole_em
;
4641 hole_size
= last_byte
- cur_offset
;
4643 err
= maybe_insert_hole(root
, inode
, cur_offset
,
4647 btrfs_drop_extent_cache(inode
, cur_offset
,
4648 cur_offset
+ hole_size
- 1, 0);
4649 hole_em
= alloc_extent_map();
4651 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4652 &BTRFS_I(inode
)->runtime_flags
);
4655 hole_em
->start
= cur_offset
;
4656 hole_em
->len
= hole_size
;
4657 hole_em
->orig_start
= cur_offset
;
4659 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4660 hole_em
->block_len
= 0;
4661 hole_em
->orig_block_len
= 0;
4662 hole_em
->ram_bytes
= hole_size
;
4663 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4664 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4665 hole_em
->generation
= root
->fs_info
->generation
;
4668 write_lock(&em_tree
->lock
);
4669 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4670 write_unlock(&em_tree
->lock
);
4673 btrfs_drop_extent_cache(inode
, cur_offset
,
4677 free_extent_map(hole_em
);
4680 free_extent_map(em
);
4682 cur_offset
= last_byte
;
4683 if (cur_offset
>= block_end
)
4686 free_extent_map(em
);
4687 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4692 static int wait_snapshoting_atomic_t(atomic_t
*a
)
4698 static void wait_for_snapshot_creation(struct btrfs_root
*root
)
4703 ret
= btrfs_start_write_no_snapshoting(root
);
4706 wait_on_atomic_t(&root
->will_be_snapshoted
,
4707 wait_snapshoting_atomic_t
,
4708 TASK_UNINTERRUPTIBLE
);
4712 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4714 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4715 struct btrfs_trans_handle
*trans
;
4716 loff_t oldsize
= i_size_read(inode
);
4717 loff_t newsize
= attr
->ia_size
;
4718 int mask
= attr
->ia_valid
;
4722 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4723 * special case where we need to update the times despite not having
4724 * these flags set. For all other operations the VFS set these flags
4725 * explicitly if it wants a timestamp update.
4727 if (newsize
!= oldsize
) {
4728 inode_inc_iversion(inode
);
4729 if (!(mask
& (ATTR_CTIME
| ATTR_MTIME
)))
4730 inode
->i_ctime
= inode
->i_mtime
=
4731 current_fs_time(inode
->i_sb
);
4734 if (newsize
> oldsize
) {
4735 truncate_pagecache(inode
, newsize
);
4737 * Don't do an expanding truncate while snapshoting is ongoing.
4738 * This is to ensure the snapshot captures a fully consistent
4739 * state of this file - if the snapshot captures this expanding
4740 * truncation, it must capture all writes that happened before
4743 wait_for_snapshot_creation(root
);
4744 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4746 btrfs_end_write_no_snapshoting(root
);
4750 trans
= btrfs_start_transaction(root
, 1);
4751 if (IS_ERR(trans
)) {
4752 btrfs_end_write_no_snapshoting(root
);
4753 return PTR_ERR(trans
);
4756 i_size_write(inode
, newsize
);
4757 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4758 ret
= btrfs_update_inode(trans
, root
, inode
);
4759 btrfs_end_write_no_snapshoting(root
);
4760 btrfs_end_transaction(trans
, root
);
4764 * We're truncating a file that used to have good data down to
4765 * zero. Make sure it gets into the ordered flush list so that
4766 * any new writes get down to disk quickly.
4769 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4770 &BTRFS_I(inode
)->runtime_flags
);
4773 * 1 for the orphan item we're going to add
4774 * 1 for the orphan item deletion.
4776 trans
= btrfs_start_transaction(root
, 2);
4778 return PTR_ERR(trans
);
4781 * We need to do this in case we fail at _any_ point during the
4782 * actual truncate. Once we do the truncate_setsize we could
4783 * invalidate pages which forces any outstanding ordered io to
4784 * be instantly completed which will give us extents that need
4785 * to be truncated. If we fail to get an orphan inode down we
4786 * could have left over extents that were never meant to live,
4787 * so we need to garuntee from this point on that everything
4788 * will be consistent.
4790 ret
= btrfs_orphan_add(trans
, inode
);
4791 btrfs_end_transaction(trans
, root
);
4795 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4796 truncate_setsize(inode
, newsize
);
4798 /* Disable nonlocked read DIO to avoid the end less truncate */
4799 btrfs_inode_block_unlocked_dio(inode
);
4800 inode_dio_wait(inode
);
4801 btrfs_inode_resume_unlocked_dio(inode
);
4803 ret
= btrfs_truncate(inode
);
4804 if (ret
&& inode
->i_nlink
) {
4808 * failed to truncate, disk_i_size is only adjusted down
4809 * as we remove extents, so it should represent the true
4810 * size of the inode, so reset the in memory size and
4811 * delete our orphan entry.
4813 trans
= btrfs_join_transaction(root
);
4814 if (IS_ERR(trans
)) {
4815 btrfs_orphan_del(NULL
, inode
);
4818 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
4819 err
= btrfs_orphan_del(trans
, inode
);
4821 btrfs_abort_transaction(trans
, root
, err
);
4822 btrfs_end_transaction(trans
, root
);
4829 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4831 struct inode
*inode
= dentry
->d_inode
;
4832 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4835 if (btrfs_root_readonly(root
))
4838 err
= inode_change_ok(inode
, attr
);
4842 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4843 err
= btrfs_setsize(inode
, attr
);
4848 if (attr
->ia_valid
) {
4849 setattr_copy(inode
, attr
);
4850 inode_inc_iversion(inode
);
4851 err
= btrfs_dirty_inode(inode
);
4853 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4854 err
= posix_acl_chmod(inode
, inode
->i_mode
);
4861 * While truncating the inode pages during eviction, we get the VFS calling
4862 * btrfs_invalidatepage() against each page of the inode. This is slow because
4863 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4864 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4865 * extent_state structures over and over, wasting lots of time.
4867 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4868 * those expensive operations on a per page basis and do only the ordered io
4869 * finishing, while we release here the extent_map and extent_state structures,
4870 * without the excessive merging and splitting.
4872 static void evict_inode_truncate_pages(struct inode
*inode
)
4874 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4875 struct extent_map_tree
*map_tree
= &BTRFS_I(inode
)->extent_tree
;
4876 struct rb_node
*node
;
4878 ASSERT(inode
->i_state
& I_FREEING
);
4879 truncate_inode_pages_final(&inode
->i_data
);
4881 write_lock(&map_tree
->lock
);
4882 while (!RB_EMPTY_ROOT(&map_tree
->map
)) {
4883 struct extent_map
*em
;
4885 node
= rb_first(&map_tree
->map
);
4886 em
= rb_entry(node
, struct extent_map
, rb_node
);
4887 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
4888 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
4889 remove_extent_mapping(map_tree
, em
);
4890 free_extent_map(em
);
4891 if (need_resched()) {
4892 write_unlock(&map_tree
->lock
);
4894 write_lock(&map_tree
->lock
);
4897 write_unlock(&map_tree
->lock
);
4899 spin_lock(&io_tree
->lock
);
4900 while (!RB_EMPTY_ROOT(&io_tree
->state
)) {
4901 struct extent_state
*state
;
4902 struct extent_state
*cached_state
= NULL
;
4904 node
= rb_first(&io_tree
->state
);
4905 state
= rb_entry(node
, struct extent_state
, rb_node
);
4906 atomic_inc(&state
->refs
);
4907 spin_unlock(&io_tree
->lock
);
4909 lock_extent_bits(io_tree
, state
->start
, state
->end
,
4911 clear_extent_bit(io_tree
, state
->start
, state
->end
,
4912 EXTENT_LOCKED
| EXTENT_DIRTY
|
4913 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
4914 EXTENT_DEFRAG
, 1, 1,
4915 &cached_state
, GFP_NOFS
);
4916 free_extent_state(state
);
4919 spin_lock(&io_tree
->lock
);
4921 spin_unlock(&io_tree
->lock
);
4924 void btrfs_evict_inode(struct inode
*inode
)
4926 struct btrfs_trans_handle
*trans
;
4927 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4928 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4929 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4932 trace_btrfs_inode_evict(inode
);
4934 evict_inode_truncate_pages(inode
);
4936 if (inode
->i_nlink
&&
4937 ((btrfs_root_refs(&root
->root_item
) != 0 &&
4938 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
4939 btrfs_is_free_space_inode(inode
)))
4942 if (is_bad_inode(inode
)) {
4943 btrfs_orphan_del(NULL
, inode
);
4946 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4947 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4949 btrfs_free_io_failure_record(inode
, 0, (u64
)-1);
4951 if (root
->fs_info
->log_root_recovering
) {
4952 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4953 &BTRFS_I(inode
)->runtime_flags
));
4957 if (inode
->i_nlink
> 0) {
4958 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
4959 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
4963 ret
= btrfs_commit_inode_delayed_inode(inode
);
4965 btrfs_orphan_del(NULL
, inode
);
4969 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4971 btrfs_orphan_del(NULL
, inode
);
4974 rsv
->size
= min_size
;
4976 global_rsv
= &root
->fs_info
->global_block_rsv
;
4978 btrfs_i_size_write(inode
, 0);
4981 * This is a bit simpler than btrfs_truncate since we've already
4982 * reserved our space for our orphan item in the unlink, so we just
4983 * need to reserve some slack space in case we add bytes and update
4984 * inode item when doing the truncate.
4987 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4988 BTRFS_RESERVE_FLUSH_LIMIT
);
4991 * Try and steal from the global reserve since we will
4992 * likely not use this space anyway, we want to try as
4993 * hard as possible to get this to work.
4996 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4999 btrfs_warn(root
->fs_info
,
5000 "Could not get space for a delete, will truncate on mount %d",
5002 btrfs_orphan_del(NULL
, inode
);
5003 btrfs_free_block_rsv(root
, rsv
);
5007 trans
= btrfs_join_transaction(root
);
5008 if (IS_ERR(trans
)) {
5009 btrfs_orphan_del(NULL
, inode
);
5010 btrfs_free_block_rsv(root
, rsv
);
5014 trans
->block_rsv
= rsv
;
5016 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
5020 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
5021 btrfs_end_transaction(trans
, root
);
5023 btrfs_btree_balance_dirty(root
);
5026 btrfs_free_block_rsv(root
, rsv
);
5029 * Errors here aren't a big deal, it just means we leave orphan items
5030 * in the tree. They will be cleaned up on the next mount.
5033 trans
->block_rsv
= root
->orphan_block_rsv
;
5034 btrfs_orphan_del(trans
, inode
);
5036 btrfs_orphan_del(NULL
, inode
);
5039 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
5040 if (!(root
== root
->fs_info
->tree_root
||
5041 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
5042 btrfs_return_ino(root
, btrfs_ino(inode
));
5044 btrfs_end_transaction(trans
, root
);
5045 btrfs_btree_balance_dirty(root
);
5047 btrfs_remove_delayed_node(inode
);
5053 * this returns the key found in the dir entry in the location pointer.
5054 * If no dir entries were found, location->objectid is 0.
5056 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
5057 struct btrfs_key
*location
)
5059 const char *name
= dentry
->d_name
.name
;
5060 int namelen
= dentry
->d_name
.len
;
5061 struct btrfs_dir_item
*di
;
5062 struct btrfs_path
*path
;
5063 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5066 path
= btrfs_alloc_path();
5070 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
5075 if (IS_ERR_OR_NULL(di
))
5078 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
5080 btrfs_free_path(path
);
5083 location
->objectid
= 0;
5088 * when we hit a tree root in a directory, the btrfs part of the inode
5089 * needs to be changed to reflect the root directory of the tree root. This
5090 * is kind of like crossing a mount point.
5092 static int fixup_tree_root_location(struct btrfs_root
*root
,
5094 struct dentry
*dentry
,
5095 struct btrfs_key
*location
,
5096 struct btrfs_root
**sub_root
)
5098 struct btrfs_path
*path
;
5099 struct btrfs_root
*new_root
;
5100 struct btrfs_root_ref
*ref
;
5101 struct extent_buffer
*leaf
;
5102 struct btrfs_key key
;
5106 path
= btrfs_alloc_path();
5113 key
.objectid
= BTRFS_I(dir
)->root
->root_key
.objectid
;
5114 key
.type
= BTRFS_ROOT_REF_KEY
;
5115 key
.offset
= location
->objectid
;
5117 ret
= btrfs_search_slot(NULL
, root
->fs_info
->tree_root
, &key
, path
,
5125 leaf
= path
->nodes
[0];
5126 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
5127 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
5128 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
5131 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
5132 (unsigned long)(ref
+ 1),
5133 dentry
->d_name
.len
);
5137 btrfs_release_path(path
);
5139 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
5140 if (IS_ERR(new_root
)) {
5141 err
= PTR_ERR(new_root
);
5145 *sub_root
= new_root
;
5146 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
5147 location
->type
= BTRFS_INODE_ITEM_KEY
;
5148 location
->offset
= 0;
5151 btrfs_free_path(path
);
5155 static void inode_tree_add(struct inode
*inode
)
5157 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5158 struct btrfs_inode
*entry
;
5160 struct rb_node
*parent
;
5161 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
5162 u64 ino
= btrfs_ino(inode
);
5164 if (inode_unhashed(inode
))
5167 spin_lock(&root
->inode_lock
);
5168 p
= &root
->inode_tree
.rb_node
;
5171 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
5173 if (ino
< btrfs_ino(&entry
->vfs_inode
))
5174 p
= &parent
->rb_left
;
5175 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
5176 p
= &parent
->rb_right
;
5178 WARN_ON(!(entry
->vfs_inode
.i_state
&
5179 (I_WILL_FREE
| I_FREEING
)));
5180 rb_replace_node(parent
, new, &root
->inode_tree
);
5181 RB_CLEAR_NODE(parent
);
5182 spin_unlock(&root
->inode_lock
);
5186 rb_link_node(new, parent
, p
);
5187 rb_insert_color(new, &root
->inode_tree
);
5188 spin_unlock(&root
->inode_lock
);
5191 static void inode_tree_del(struct inode
*inode
)
5193 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5196 spin_lock(&root
->inode_lock
);
5197 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
5198 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
5199 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
5200 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5202 spin_unlock(&root
->inode_lock
);
5204 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
5205 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
5206 spin_lock(&root
->inode_lock
);
5207 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
5208 spin_unlock(&root
->inode_lock
);
5210 btrfs_add_dead_root(root
);
5214 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
5216 struct rb_node
*node
;
5217 struct rb_node
*prev
;
5218 struct btrfs_inode
*entry
;
5219 struct inode
*inode
;
5222 if (!test_bit(BTRFS_FS_STATE_ERROR
, &root
->fs_info
->fs_state
))
5223 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
5225 spin_lock(&root
->inode_lock
);
5227 node
= root
->inode_tree
.rb_node
;
5231 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5233 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
5234 node
= node
->rb_left
;
5235 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
5236 node
= node
->rb_right
;
5242 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
5243 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
5247 prev
= rb_next(prev
);
5251 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
5252 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
5253 inode
= igrab(&entry
->vfs_inode
);
5255 spin_unlock(&root
->inode_lock
);
5256 if (atomic_read(&inode
->i_count
) > 1)
5257 d_prune_aliases(inode
);
5259 * btrfs_drop_inode will have it removed from
5260 * the inode cache when its usage count
5265 spin_lock(&root
->inode_lock
);
5269 if (cond_resched_lock(&root
->inode_lock
))
5272 node
= rb_next(node
);
5274 spin_unlock(&root
->inode_lock
);
5277 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
5279 struct btrfs_iget_args
*args
= p
;
5280 inode
->i_ino
= args
->location
->objectid
;
5281 memcpy(&BTRFS_I(inode
)->location
, args
->location
,
5282 sizeof(*args
->location
));
5283 BTRFS_I(inode
)->root
= args
->root
;
5287 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
5289 struct btrfs_iget_args
*args
= opaque
;
5290 return args
->location
->objectid
== BTRFS_I(inode
)->location
.objectid
&&
5291 args
->root
== BTRFS_I(inode
)->root
;
5294 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
5295 struct btrfs_key
*location
,
5296 struct btrfs_root
*root
)
5298 struct inode
*inode
;
5299 struct btrfs_iget_args args
;
5300 unsigned long hashval
= btrfs_inode_hash(location
->objectid
, root
);
5302 args
.location
= location
;
5305 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
5306 btrfs_init_locked_inode
,
5311 /* Get an inode object given its location and corresponding root.
5312 * Returns in *is_new if the inode was read from disk
5314 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
5315 struct btrfs_root
*root
, int *new)
5317 struct inode
*inode
;
5319 inode
= btrfs_iget_locked(s
, location
, root
);
5321 return ERR_PTR(-ENOMEM
);
5323 if (inode
->i_state
& I_NEW
) {
5324 btrfs_read_locked_inode(inode
);
5325 if (!is_bad_inode(inode
)) {
5326 inode_tree_add(inode
);
5327 unlock_new_inode(inode
);
5331 unlock_new_inode(inode
);
5333 inode
= ERR_PTR(-ESTALE
);
5340 static struct inode
*new_simple_dir(struct super_block
*s
,
5341 struct btrfs_key
*key
,
5342 struct btrfs_root
*root
)
5344 struct inode
*inode
= new_inode(s
);
5347 return ERR_PTR(-ENOMEM
);
5349 BTRFS_I(inode
)->root
= root
;
5350 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5351 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5353 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5354 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5355 inode
->i_fop
= &simple_dir_operations
;
5356 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5357 inode
->i_mtime
= CURRENT_TIME
;
5358 inode
->i_atime
= inode
->i_mtime
;
5359 inode
->i_ctime
= inode
->i_mtime
;
5360 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
5365 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5367 struct inode
*inode
;
5368 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5369 struct btrfs_root
*sub_root
= root
;
5370 struct btrfs_key location
;
5374 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5375 return ERR_PTR(-ENAMETOOLONG
);
5377 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5379 return ERR_PTR(ret
);
5381 if (location
.objectid
== 0)
5382 return ERR_PTR(-ENOENT
);
5384 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5385 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5389 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5391 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
5392 ret
= fixup_tree_root_location(root
, dir
, dentry
,
5393 &location
, &sub_root
);
5396 inode
= ERR_PTR(ret
);
5398 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5400 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5402 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
5404 if (!IS_ERR(inode
) && root
!= sub_root
) {
5405 down_read(&root
->fs_info
->cleanup_work_sem
);
5406 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5407 ret
= btrfs_orphan_cleanup(sub_root
);
5408 up_read(&root
->fs_info
->cleanup_work_sem
);
5411 inode
= ERR_PTR(ret
);
5418 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5420 struct btrfs_root
*root
;
5421 struct inode
*inode
= dentry
->d_inode
;
5423 if (!inode
&& !IS_ROOT(dentry
))
5424 inode
= dentry
->d_parent
->d_inode
;
5427 root
= BTRFS_I(inode
)->root
;
5428 if (btrfs_root_refs(&root
->root_item
) == 0)
5431 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5437 static void btrfs_dentry_release(struct dentry
*dentry
)
5439 kfree(dentry
->d_fsdata
);
5442 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5445 struct inode
*inode
;
5447 inode
= btrfs_lookup_dentry(dir
, dentry
);
5448 if (IS_ERR(inode
)) {
5449 if (PTR_ERR(inode
) == -ENOENT
)
5452 return ERR_CAST(inode
);
5455 return d_splice_alias(inode
, dentry
);
5458 unsigned char btrfs_filetype_table
[] = {
5459 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5462 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
5464 struct inode
*inode
= file_inode(file
);
5465 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5466 struct btrfs_item
*item
;
5467 struct btrfs_dir_item
*di
;
5468 struct btrfs_key key
;
5469 struct btrfs_key found_key
;
5470 struct btrfs_path
*path
;
5471 struct list_head ins_list
;
5472 struct list_head del_list
;
5474 struct extent_buffer
*leaf
;
5476 unsigned char d_type
;
5481 int key_type
= BTRFS_DIR_INDEX_KEY
;
5485 int is_curr
= 0; /* ctx->pos points to the current index? */
5487 /* FIXME, use a real flag for deciding about the key type */
5488 if (root
->fs_info
->tree_root
== root
)
5489 key_type
= BTRFS_DIR_ITEM_KEY
;
5491 if (!dir_emit_dots(file
, ctx
))
5494 path
= btrfs_alloc_path();
5500 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5501 INIT_LIST_HEAD(&ins_list
);
5502 INIT_LIST_HEAD(&del_list
);
5503 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5506 key
.type
= key_type
;
5507 key
.offset
= ctx
->pos
;
5508 key
.objectid
= btrfs_ino(inode
);
5510 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5515 leaf
= path
->nodes
[0];
5516 slot
= path
->slots
[0];
5517 if (slot
>= btrfs_header_nritems(leaf
)) {
5518 ret
= btrfs_next_leaf(root
, path
);
5526 item
= btrfs_item_nr(slot
);
5527 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5529 if (found_key
.objectid
!= key
.objectid
)
5531 if (found_key
.type
!= key_type
)
5533 if (found_key
.offset
< ctx
->pos
)
5535 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5536 btrfs_should_delete_dir_index(&del_list
,
5540 ctx
->pos
= found_key
.offset
;
5543 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5545 di_total
= btrfs_item_size(leaf
, item
);
5547 while (di_cur
< di_total
) {
5548 struct btrfs_key location
;
5550 if (verify_dir_item(root
, leaf
, di
))
5553 name_len
= btrfs_dir_name_len(leaf
, di
);
5554 if (name_len
<= sizeof(tmp_name
)) {
5555 name_ptr
= tmp_name
;
5557 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5563 read_extent_buffer(leaf
, name_ptr
,
5564 (unsigned long)(di
+ 1), name_len
);
5566 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5567 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5570 /* is this a reference to our own snapshot? If so
5573 * In contrast to old kernels, we insert the snapshot's
5574 * dir item and dir index after it has been created, so
5575 * we won't find a reference to our own snapshot. We
5576 * still keep the following code for backward
5579 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5580 location
.objectid
== root
->root_key
.objectid
) {
5584 over
= !dir_emit(ctx
, name_ptr
, name_len
,
5585 location
.objectid
, d_type
);
5588 if (name_ptr
!= tmp_name
)
5593 di_len
= btrfs_dir_name_len(leaf
, di
) +
5594 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5596 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5602 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5605 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
);
5610 /* Reached end of directory/root. Bump pos past the last item. */
5614 * Stop new entries from being returned after we return the last
5617 * New directory entries are assigned a strictly increasing
5618 * offset. This means that new entries created during readdir
5619 * are *guaranteed* to be seen in the future by that readdir.
5620 * This has broken buggy programs which operate on names as
5621 * they're returned by readdir. Until we re-use freed offsets
5622 * we have this hack to stop new entries from being returned
5623 * under the assumption that they'll never reach this huge
5626 * This is being careful not to overflow 32bit loff_t unless the
5627 * last entry requires it because doing so has broken 32bit apps
5630 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5631 if (ctx
->pos
>= INT_MAX
)
5632 ctx
->pos
= LLONG_MAX
;
5639 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5640 btrfs_put_delayed_items(&ins_list
, &del_list
);
5641 btrfs_free_path(path
);
5645 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5647 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5648 struct btrfs_trans_handle
*trans
;
5650 bool nolock
= false;
5652 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5655 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5658 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5660 trans
= btrfs_join_transaction_nolock(root
);
5662 trans
= btrfs_join_transaction(root
);
5664 return PTR_ERR(trans
);
5665 ret
= btrfs_commit_transaction(trans
, root
);
5671 * This is somewhat expensive, updating the tree every time the
5672 * inode changes. But, it is most likely to find the inode in cache.
5673 * FIXME, needs more benchmarking...there are no reasons other than performance
5674 * to keep or drop this code.
5676 static int btrfs_dirty_inode(struct inode
*inode
)
5678 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5679 struct btrfs_trans_handle
*trans
;
5682 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5685 trans
= btrfs_join_transaction(root
);
5687 return PTR_ERR(trans
);
5689 ret
= btrfs_update_inode(trans
, root
, inode
);
5690 if (ret
&& ret
== -ENOSPC
) {
5691 /* whoops, lets try again with the full transaction */
5692 btrfs_end_transaction(trans
, root
);
5693 trans
= btrfs_start_transaction(root
, 1);
5695 return PTR_ERR(trans
);
5697 ret
= btrfs_update_inode(trans
, root
, inode
);
5699 btrfs_end_transaction(trans
, root
);
5700 if (BTRFS_I(inode
)->delayed_node
)
5701 btrfs_balance_delayed_items(root
);
5707 * This is a copy of file_update_time. We need this so we can return error on
5708 * ENOSPC for updating the inode in the case of file write and mmap writes.
5710 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5713 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5715 if (btrfs_root_readonly(root
))
5718 if (flags
& S_VERSION
)
5719 inode_inc_iversion(inode
);
5720 if (flags
& S_CTIME
)
5721 inode
->i_ctime
= *now
;
5722 if (flags
& S_MTIME
)
5723 inode
->i_mtime
= *now
;
5724 if (flags
& S_ATIME
)
5725 inode
->i_atime
= *now
;
5726 return btrfs_dirty_inode(inode
);
5730 * find the highest existing sequence number in a directory
5731 * and then set the in-memory index_cnt variable to reflect
5732 * free sequence numbers
5734 static int btrfs_set_inode_index_count(struct inode
*inode
)
5736 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5737 struct btrfs_key key
, found_key
;
5738 struct btrfs_path
*path
;
5739 struct extent_buffer
*leaf
;
5742 key
.objectid
= btrfs_ino(inode
);
5743 key
.type
= BTRFS_DIR_INDEX_KEY
;
5744 key
.offset
= (u64
)-1;
5746 path
= btrfs_alloc_path();
5750 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5753 /* FIXME: we should be able to handle this */
5759 * MAGIC NUMBER EXPLANATION:
5760 * since we search a directory based on f_pos we have to start at 2
5761 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5762 * else has to start at 2
5764 if (path
->slots
[0] == 0) {
5765 BTRFS_I(inode
)->index_cnt
= 2;
5771 leaf
= path
->nodes
[0];
5772 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5774 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5775 found_key
.type
!= BTRFS_DIR_INDEX_KEY
) {
5776 BTRFS_I(inode
)->index_cnt
= 2;
5780 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5782 btrfs_free_path(path
);
5787 * helper to find a free sequence number in a given directory. This current
5788 * code is very simple, later versions will do smarter things in the btree
5790 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5794 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5795 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5797 ret
= btrfs_set_inode_index_count(dir
);
5803 *index
= BTRFS_I(dir
)->index_cnt
;
5804 BTRFS_I(dir
)->index_cnt
++;
5809 static int btrfs_insert_inode_locked(struct inode
*inode
)
5811 struct btrfs_iget_args args
;
5812 args
.location
= &BTRFS_I(inode
)->location
;
5813 args
.root
= BTRFS_I(inode
)->root
;
5815 return insert_inode_locked4(inode
,
5816 btrfs_inode_hash(inode
->i_ino
, BTRFS_I(inode
)->root
),
5817 btrfs_find_actor
, &args
);
5820 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5821 struct btrfs_root
*root
,
5823 const char *name
, int name_len
,
5824 u64 ref_objectid
, u64 objectid
,
5825 umode_t mode
, u64
*index
)
5827 struct inode
*inode
;
5828 struct btrfs_inode_item
*inode_item
;
5829 struct btrfs_key
*location
;
5830 struct btrfs_path
*path
;
5831 struct btrfs_inode_ref
*ref
;
5832 struct btrfs_key key
[2];
5834 int nitems
= name
? 2 : 1;
5838 path
= btrfs_alloc_path();
5840 return ERR_PTR(-ENOMEM
);
5842 inode
= new_inode(root
->fs_info
->sb
);
5844 btrfs_free_path(path
);
5845 return ERR_PTR(-ENOMEM
);
5849 * O_TMPFILE, set link count to 0, so that after this point,
5850 * we fill in an inode item with the correct link count.
5853 set_nlink(inode
, 0);
5856 * we have to initialize this early, so we can reclaim the inode
5857 * number if we fail afterwards in this function.
5859 inode
->i_ino
= objectid
;
5862 trace_btrfs_inode_request(dir
);
5864 ret
= btrfs_set_inode_index(dir
, index
);
5866 btrfs_free_path(path
);
5868 return ERR_PTR(ret
);
5874 * index_cnt is ignored for everything but a dir,
5875 * btrfs_get_inode_index_count has an explanation for the magic
5878 BTRFS_I(inode
)->index_cnt
= 2;
5879 BTRFS_I(inode
)->dir_index
= *index
;
5880 BTRFS_I(inode
)->root
= root
;
5881 BTRFS_I(inode
)->generation
= trans
->transid
;
5882 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5885 * We could have gotten an inode number from somebody who was fsynced
5886 * and then removed in this same transaction, so let's just set full
5887 * sync since it will be a full sync anyway and this will blow away the
5888 * old info in the log.
5890 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5892 key
[0].objectid
= objectid
;
5893 key
[0].type
= BTRFS_INODE_ITEM_KEY
;
5896 sizes
[0] = sizeof(struct btrfs_inode_item
);
5900 * Start new inodes with an inode_ref. This is slightly more
5901 * efficient for small numbers of hard links since they will
5902 * be packed into one item. Extended refs will kick in if we
5903 * add more hard links than can fit in the ref item.
5905 key
[1].objectid
= objectid
;
5906 key
[1].type
= BTRFS_INODE_REF_KEY
;
5907 key
[1].offset
= ref_objectid
;
5909 sizes
[1] = name_len
+ sizeof(*ref
);
5912 location
= &BTRFS_I(inode
)->location
;
5913 location
->objectid
= objectid
;
5914 location
->offset
= 0;
5915 location
->type
= BTRFS_INODE_ITEM_KEY
;
5917 ret
= btrfs_insert_inode_locked(inode
);
5921 path
->leave_spinning
= 1;
5922 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, nitems
);
5926 inode_init_owner(inode
, dir
, mode
);
5927 inode_set_bytes(inode
, 0);
5929 inode
->i_mtime
= CURRENT_TIME
;
5930 inode
->i_atime
= inode
->i_mtime
;
5931 inode
->i_ctime
= inode
->i_mtime
;
5932 BTRFS_I(inode
)->i_otime
= inode
->i_mtime
;
5934 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5935 struct btrfs_inode_item
);
5936 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5937 sizeof(*inode_item
));
5938 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5941 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5942 struct btrfs_inode_ref
);
5943 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5944 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5945 ptr
= (unsigned long)(ref
+ 1);
5946 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5949 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5950 btrfs_free_path(path
);
5952 btrfs_inherit_iflags(inode
, dir
);
5954 if (S_ISREG(mode
)) {
5955 if (btrfs_test_opt(root
, NODATASUM
))
5956 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5957 if (btrfs_test_opt(root
, NODATACOW
))
5958 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5959 BTRFS_INODE_NODATASUM
;
5962 inode_tree_add(inode
);
5964 trace_btrfs_inode_new(inode
);
5965 btrfs_set_inode_last_trans(trans
, inode
);
5967 btrfs_update_root_times(trans
, root
);
5969 ret
= btrfs_inode_inherit_props(trans
, inode
, dir
);
5971 btrfs_err(root
->fs_info
,
5972 "error inheriting props for ino %llu (root %llu): %d",
5973 btrfs_ino(inode
), root
->root_key
.objectid
, ret
);
5978 unlock_new_inode(inode
);
5981 BTRFS_I(dir
)->index_cnt
--;
5982 btrfs_free_path(path
);
5984 return ERR_PTR(ret
);
5987 static inline u8
btrfs_inode_type(struct inode
*inode
)
5989 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5993 * utility function to add 'inode' into 'parent_inode' with
5994 * a give name and a given sequence number.
5995 * if 'add_backref' is true, also insert a backref from the
5996 * inode to the parent directory.
5998 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5999 struct inode
*parent_inode
, struct inode
*inode
,
6000 const char *name
, int name_len
, int add_backref
, u64 index
)
6003 struct btrfs_key key
;
6004 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
6005 u64 ino
= btrfs_ino(inode
);
6006 u64 parent_ino
= btrfs_ino(parent_inode
);
6008 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6009 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
6012 key
.type
= BTRFS_INODE_ITEM_KEY
;
6016 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6017 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
6018 key
.objectid
, root
->root_key
.objectid
,
6019 parent_ino
, index
, name
, name_len
);
6020 } else if (add_backref
) {
6021 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
6025 /* Nothing to clean up yet */
6029 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
6031 btrfs_inode_type(inode
), index
);
6032 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
6035 btrfs_abort_transaction(trans
, root
, ret
);
6039 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
6041 inode_inc_iversion(parent_inode
);
6042 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
6043 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
6045 btrfs_abort_transaction(trans
, root
, ret
);
6049 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
6052 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
6053 key
.objectid
, root
->root_key
.objectid
,
6054 parent_ino
, &local_index
, name
, name_len
);
6056 } else if (add_backref
) {
6060 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
6061 ino
, parent_ino
, &local_index
);
6066 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
6067 struct inode
*dir
, struct dentry
*dentry
,
6068 struct inode
*inode
, int backref
, u64 index
)
6070 int err
= btrfs_add_link(trans
, dir
, inode
,
6071 dentry
->d_name
.name
, dentry
->d_name
.len
,
6078 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
6079 umode_t mode
, dev_t rdev
)
6081 struct btrfs_trans_handle
*trans
;
6082 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6083 struct inode
*inode
= NULL
;
6089 if (!new_valid_dev(rdev
))
6093 * 2 for inode item and ref
6095 * 1 for xattr if selinux is on
6097 trans
= btrfs_start_transaction(root
, 5);
6099 return PTR_ERR(trans
);
6101 err
= btrfs_find_free_ino(root
, &objectid
);
6105 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6106 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6108 if (IS_ERR(inode
)) {
6109 err
= PTR_ERR(inode
);
6114 * If the active LSM wants to access the inode during
6115 * d_instantiate it needs these. Smack checks to see
6116 * if the filesystem supports xattrs by looking at the
6119 inode
->i_op
= &btrfs_special_inode_operations
;
6120 init_special_inode(inode
, inode
->i_mode
, rdev
);
6122 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6124 goto out_unlock_inode
;
6126 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6128 goto out_unlock_inode
;
6130 btrfs_update_inode(trans
, root
, inode
);
6131 unlock_new_inode(inode
);
6132 d_instantiate(dentry
, inode
);
6136 btrfs_end_transaction(trans
, root
);
6137 btrfs_balance_delayed_items(root
);
6138 btrfs_btree_balance_dirty(root
);
6140 inode_dec_link_count(inode
);
6147 unlock_new_inode(inode
);
6152 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
6153 umode_t mode
, bool excl
)
6155 struct btrfs_trans_handle
*trans
;
6156 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6157 struct inode
*inode
= NULL
;
6158 int drop_inode_on_err
= 0;
6164 * 2 for inode item and ref
6166 * 1 for xattr if selinux is on
6168 trans
= btrfs_start_transaction(root
, 5);
6170 return PTR_ERR(trans
);
6172 err
= btrfs_find_free_ino(root
, &objectid
);
6176 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6177 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6179 if (IS_ERR(inode
)) {
6180 err
= PTR_ERR(inode
);
6183 drop_inode_on_err
= 1;
6185 * If the active LSM wants to access the inode during
6186 * d_instantiate it needs these. Smack checks to see
6187 * if the filesystem supports xattrs by looking at the
6190 inode
->i_fop
= &btrfs_file_operations
;
6191 inode
->i_op
= &btrfs_file_inode_operations
;
6192 inode
->i_mapping
->a_ops
= &btrfs_aops
;
6193 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
6195 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6197 goto out_unlock_inode
;
6199 err
= btrfs_update_inode(trans
, root
, inode
);
6201 goto out_unlock_inode
;
6203 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
6205 goto out_unlock_inode
;
6207 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
6208 unlock_new_inode(inode
);
6209 d_instantiate(dentry
, inode
);
6212 btrfs_end_transaction(trans
, root
);
6213 if (err
&& drop_inode_on_err
) {
6214 inode_dec_link_count(inode
);
6217 btrfs_balance_delayed_items(root
);
6218 btrfs_btree_balance_dirty(root
);
6222 unlock_new_inode(inode
);
6227 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
6228 struct dentry
*dentry
)
6230 struct btrfs_trans_handle
*trans
;
6231 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6232 struct inode
*inode
= old_dentry
->d_inode
;
6237 /* do not allow sys_link's with other subvols of the same device */
6238 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
6241 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
6244 err
= btrfs_set_inode_index(dir
, &index
);
6249 * 2 items for inode and inode ref
6250 * 2 items for dir items
6251 * 1 item for parent inode
6253 trans
= btrfs_start_transaction(root
, 5);
6254 if (IS_ERR(trans
)) {
6255 err
= PTR_ERR(trans
);
6259 /* There are several dir indexes for this inode, clear the cache. */
6260 BTRFS_I(inode
)->dir_index
= 0ULL;
6262 inode_inc_iversion(inode
);
6263 inode
->i_ctime
= CURRENT_TIME
;
6265 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
6267 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
6272 struct dentry
*parent
= dentry
->d_parent
;
6273 err
= btrfs_update_inode(trans
, root
, inode
);
6276 if (inode
->i_nlink
== 1) {
6278 * If new hard link count is 1, it's a file created
6279 * with open(2) O_TMPFILE flag.
6281 err
= btrfs_orphan_del(trans
, inode
);
6285 d_instantiate(dentry
, inode
);
6286 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
6289 btrfs_end_transaction(trans
, root
);
6290 btrfs_balance_delayed_items(root
);
6293 inode_dec_link_count(inode
);
6296 btrfs_btree_balance_dirty(root
);
6300 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
6302 struct inode
*inode
= NULL
;
6303 struct btrfs_trans_handle
*trans
;
6304 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
6306 int drop_on_err
= 0;
6311 * 2 items for inode and ref
6312 * 2 items for dir items
6313 * 1 for xattr if selinux is on
6315 trans
= btrfs_start_transaction(root
, 5);
6317 return PTR_ERR(trans
);
6319 err
= btrfs_find_free_ino(root
, &objectid
);
6323 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
6324 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
6325 S_IFDIR
| mode
, &index
);
6326 if (IS_ERR(inode
)) {
6327 err
= PTR_ERR(inode
);
6332 /* these must be set before we unlock the inode */
6333 inode
->i_op
= &btrfs_dir_inode_operations
;
6334 inode
->i_fop
= &btrfs_dir_file_operations
;
6336 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
6338 goto out_fail_inode
;
6340 btrfs_i_size_write(inode
, 0);
6341 err
= btrfs_update_inode(trans
, root
, inode
);
6343 goto out_fail_inode
;
6345 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
6346 dentry
->d_name
.len
, 0, index
);
6348 goto out_fail_inode
;
6350 d_instantiate(dentry
, inode
);
6352 * mkdir is special. We're unlocking after we call d_instantiate
6353 * to avoid a race with nfsd calling d_instantiate.
6355 unlock_new_inode(inode
);
6359 btrfs_end_transaction(trans
, root
);
6361 inode_dec_link_count(inode
);
6364 btrfs_balance_delayed_items(root
);
6365 btrfs_btree_balance_dirty(root
);
6369 unlock_new_inode(inode
);
6373 /* Find next extent map of a given extent map, caller needs to ensure locks */
6374 static struct extent_map
*next_extent_map(struct extent_map
*em
)
6376 struct rb_node
*next
;
6378 next
= rb_next(&em
->rb_node
);
6381 return container_of(next
, struct extent_map
, rb_node
);
6384 static struct extent_map
*prev_extent_map(struct extent_map
*em
)
6386 struct rb_node
*prev
;
6388 prev
= rb_prev(&em
->rb_node
);
6391 return container_of(prev
, struct extent_map
, rb_node
);
6394 /* helper for btfs_get_extent. Given an existing extent in the tree,
6395 * the existing extent is the nearest extent to map_start,
6396 * and an extent that you want to insert, deal with overlap and insert
6397 * the best fitted new extent into the tree.
6399 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
6400 struct extent_map
*existing
,
6401 struct extent_map
*em
,
6404 struct extent_map
*prev
;
6405 struct extent_map
*next
;
6410 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
6412 if (existing
->start
> map_start
) {
6414 prev
= prev_extent_map(next
);
6417 next
= next_extent_map(prev
);
6420 start
= prev
? extent_map_end(prev
) : em
->start
;
6421 start
= max_t(u64
, start
, em
->start
);
6422 end
= next
? next
->start
: extent_map_end(em
);
6423 end
= min_t(u64
, end
, extent_map_end(em
));
6424 start_diff
= start
- em
->start
;
6426 em
->len
= end
- start
;
6427 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
6428 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
6429 em
->block_start
+= start_diff
;
6430 em
->block_len
-= start_diff
;
6432 return add_extent_mapping(em_tree
, em
, 0);
6435 static noinline
int uncompress_inline(struct btrfs_path
*path
,
6436 struct inode
*inode
, struct page
*page
,
6437 size_t pg_offset
, u64 extent_offset
,
6438 struct btrfs_file_extent_item
*item
)
6441 struct extent_buffer
*leaf
= path
->nodes
[0];
6444 unsigned long inline_size
;
6448 WARN_ON(pg_offset
!= 0);
6449 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6450 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6451 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6452 btrfs_item_nr(path
->slots
[0]));
6453 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6456 ptr
= btrfs_file_extent_inline_start(item
);
6458 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6460 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
6461 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6462 extent_offset
, inline_size
, max_size
);
6468 * a bit scary, this does extent mapping from logical file offset to the disk.
6469 * the ugly parts come from merging extents from the disk with the in-ram
6470 * representation. This gets more complex because of the data=ordered code,
6471 * where the in-ram extents might be locked pending data=ordered completion.
6473 * This also copies inline extents directly into the page.
6476 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
6477 size_t pg_offset
, u64 start
, u64 len
,
6482 u64 extent_start
= 0;
6484 u64 objectid
= btrfs_ino(inode
);
6486 struct btrfs_path
*path
= NULL
;
6487 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6488 struct btrfs_file_extent_item
*item
;
6489 struct extent_buffer
*leaf
;
6490 struct btrfs_key found_key
;
6491 struct extent_map
*em
= NULL
;
6492 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
6493 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6494 struct btrfs_trans_handle
*trans
= NULL
;
6495 const bool new_inline
= !page
|| create
;
6498 read_lock(&em_tree
->lock
);
6499 em
= lookup_extent_mapping(em_tree
, start
, len
);
6501 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6502 read_unlock(&em_tree
->lock
);
6505 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6506 free_extent_map(em
);
6507 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6508 free_extent_map(em
);
6512 em
= alloc_extent_map();
6517 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6518 em
->start
= EXTENT_MAP_HOLE
;
6519 em
->orig_start
= EXTENT_MAP_HOLE
;
6521 em
->block_len
= (u64
)-1;
6524 path
= btrfs_alloc_path();
6530 * Chances are we'll be called again, so go ahead and do
6536 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6537 objectid
, start
, trans
!= NULL
);
6544 if (path
->slots
[0] == 0)
6549 leaf
= path
->nodes
[0];
6550 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6551 struct btrfs_file_extent_item
);
6552 /* are we inside the extent that was found? */
6553 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6554 found_type
= found_key
.type
;
6555 if (found_key
.objectid
!= objectid
||
6556 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6558 * If we backup past the first extent we want to move forward
6559 * and see if there is an extent in front of us, otherwise we'll
6560 * say there is a hole for our whole search range which can
6567 found_type
= btrfs_file_extent_type(leaf
, item
);
6568 extent_start
= found_key
.offset
;
6569 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6570 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6571 extent_end
= extent_start
+
6572 btrfs_file_extent_num_bytes(leaf
, item
);
6573 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6575 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6576 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6579 if (start
>= extent_end
) {
6581 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6582 ret
= btrfs_next_leaf(root
, path
);
6589 leaf
= path
->nodes
[0];
6591 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6592 if (found_key
.objectid
!= objectid
||
6593 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6595 if (start
+ len
<= found_key
.offset
)
6597 if (start
> found_key
.offset
)
6600 em
->orig_start
= start
;
6601 em
->len
= found_key
.offset
- start
;
6605 btrfs_extent_item_to_extent_map(inode
, path
, item
, new_inline
, em
);
6607 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6608 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6610 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6614 size_t extent_offset
;
6620 size
= btrfs_file_extent_inline_len(leaf
, path
->slots
[0], item
);
6621 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6622 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6623 size
- extent_offset
);
6624 em
->start
= extent_start
+ extent_offset
;
6625 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6626 em
->orig_block_len
= em
->len
;
6627 em
->orig_start
= em
->start
;
6628 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6629 if (create
== 0 && !PageUptodate(page
)) {
6630 if (btrfs_file_extent_compression(leaf
, item
) !=
6631 BTRFS_COMPRESS_NONE
) {
6632 ret
= uncompress_inline(path
, inode
, page
,
6634 extent_offset
, item
);
6641 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6643 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6644 memset(map
+ pg_offset
+ copy_size
, 0,
6645 PAGE_CACHE_SIZE
- pg_offset
-
6650 flush_dcache_page(page
);
6651 } else if (create
&& PageUptodate(page
)) {
6655 free_extent_map(em
);
6658 btrfs_release_path(path
);
6659 trans
= btrfs_join_transaction(root
);
6662 return ERR_CAST(trans
);
6666 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6669 btrfs_mark_buffer_dirty(leaf
);
6671 set_extent_uptodate(io_tree
, em
->start
,
6672 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6677 em
->orig_start
= start
;
6680 em
->block_start
= EXTENT_MAP_HOLE
;
6681 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6683 btrfs_release_path(path
);
6684 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6685 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6686 em
->start
, em
->len
, start
, len
);
6692 write_lock(&em_tree
->lock
);
6693 ret
= add_extent_mapping(em_tree
, em
, 0);
6694 /* it is possible that someone inserted the extent into the tree
6695 * while we had the lock dropped. It is also possible that
6696 * an overlapping map exists in the tree
6698 if (ret
== -EEXIST
) {
6699 struct extent_map
*existing
;
6703 existing
= search_extent_mapping(em_tree
, start
, len
);
6705 * existing will always be non-NULL, since there must be
6706 * extent causing the -EEXIST.
6708 if (start
>= extent_map_end(existing
) ||
6709 start
<= existing
->start
) {
6711 * The existing extent map is the one nearest to
6712 * the [start, start + len) range which overlaps
6714 err
= merge_extent_mapping(em_tree
, existing
,
6716 free_extent_map(existing
);
6718 free_extent_map(em
);
6722 free_extent_map(em
);
6727 write_unlock(&em_tree
->lock
);
6730 trace_btrfs_get_extent(root
, em
);
6733 btrfs_free_path(path
);
6735 ret
= btrfs_end_transaction(trans
, root
);
6740 free_extent_map(em
);
6741 return ERR_PTR(err
);
6743 BUG_ON(!em
); /* Error is always set */
6747 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6748 size_t pg_offset
, u64 start
, u64 len
,
6751 struct extent_map
*em
;
6752 struct extent_map
*hole_em
= NULL
;
6753 u64 range_start
= start
;
6759 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6766 * - a pre-alloc extent,
6767 * there might actually be delalloc bytes behind it.
6769 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6770 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6776 /* check to see if we've wrapped (len == -1 or similar) */
6785 /* ok, we didn't find anything, lets look for delalloc */
6786 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6787 end
, len
, EXTENT_DELALLOC
, 1);
6788 found_end
= range_start
+ found
;
6789 if (found_end
< range_start
)
6790 found_end
= (u64
)-1;
6793 * we didn't find anything useful, return
6794 * the original results from get_extent()
6796 if (range_start
> end
|| found_end
<= start
) {
6802 /* adjust the range_start to make sure it doesn't
6803 * go backwards from the start they passed in
6805 range_start
= max(start
, range_start
);
6806 found
= found_end
- range_start
;
6809 u64 hole_start
= start
;
6812 em
= alloc_extent_map();
6818 * when btrfs_get_extent can't find anything it
6819 * returns one huge hole
6821 * make sure what it found really fits our range, and
6822 * adjust to make sure it is based on the start from
6826 u64 calc_end
= extent_map_end(hole_em
);
6828 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6829 free_extent_map(hole_em
);
6832 hole_start
= max(hole_em
->start
, start
);
6833 hole_len
= calc_end
- hole_start
;
6837 if (hole_em
&& range_start
> hole_start
) {
6838 /* our hole starts before our delalloc, so we
6839 * have to return just the parts of the hole
6840 * that go until the delalloc starts
6842 em
->len
= min(hole_len
,
6843 range_start
- hole_start
);
6844 em
->start
= hole_start
;
6845 em
->orig_start
= hole_start
;
6847 * don't adjust block start at all,
6848 * it is fixed at EXTENT_MAP_HOLE
6850 em
->block_start
= hole_em
->block_start
;
6851 em
->block_len
= hole_len
;
6852 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6853 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6855 em
->start
= range_start
;
6857 em
->orig_start
= range_start
;
6858 em
->block_start
= EXTENT_MAP_DELALLOC
;
6859 em
->block_len
= found
;
6861 } else if (hole_em
) {
6866 free_extent_map(hole_em
);
6868 free_extent_map(em
);
6869 return ERR_PTR(err
);
6874 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6877 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6878 struct extent_map
*em
;
6879 struct btrfs_key ins
;
6883 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6884 ret
= btrfs_reserve_extent(root
, len
, root
->sectorsize
, 0,
6885 alloc_hint
, &ins
, 1, 1);
6887 return ERR_PTR(ret
);
6889 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6890 ins
.offset
, ins
.offset
, ins
.offset
, 0);
6892 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
6896 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6897 ins
.offset
, ins
.offset
, 0);
6899 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
, 1);
6900 free_extent_map(em
);
6901 return ERR_PTR(ret
);
6908 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6909 * block must be cow'd
6911 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
6912 u64
*orig_start
, u64
*orig_block_len
,
6915 struct btrfs_trans_handle
*trans
;
6916 struct btrfs_path
*path
;
6918 struct extent_buffer
*leaf
;
6919 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6920 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6921 struct btrfs_file_extent_item
*fi
;
6922 struct btrfs_key key
;
6929 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
6931 path
= btrfs_alloc_path();
6935 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, btrfs_ino(inode
),
6940 slot
= path
->slots
[0];
6943 /* can't find the item, must cow */
6950 leaf
= path
->nodes
[0];
6951 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6952 if (key
.objectid
!= btrfs_ino(inode
) ||
6953 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6954 /* not our file or wrong item type, must cow */
6958 if (key
.offset
> offset
) {
6959 /* Wrong offset, must cow */
6963 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6964 found_type
= btrfs_file_extent_type(leaf
, fi
);
6965 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6966 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6967 /* not a regular extent, must cow */
6971 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
6974 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6975 if (extent_end
<= offset
)
6978 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6979 if (disk_bytenr
== 0)
6982 if (btrfs_file_extent_compression(leaf
, fi
) ||
6983 btrfs_file_extent_encryption(leaf
, fi
) ||
6984 btrfs_file_extent_other_encoding(leaf
, fi
))
6987 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6990 *orig_start
= key
.offset
- backref_offset
;
6991 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
6992 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
6995 if (btrfs_extent_readonly(root
, disk_bytenr
))
6998 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
6999 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
7002 range_end
= round_up(offset
+ num_bytes
, root
->sectorsize
) - 1;
7003 ret
= test_range_bit(io_tree
, offset
, range_end
,
7004 EXTENT_DELALLOC
, 0, NULL
);
7011 btrfs_release_path(path
);
7014 * look for other files referencing this extent, if we
7015 * find any we must cow
7017 trans
= btrfs_join_transaction(root
);
7018 if (IS_ERR(trans
)) {
7023 ret
= btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
7024 key
.offset
- backref_offset
, disk_bytenr
);
7025 btrfs_end_transaction(trans
, root
);
7032 * adjust disk_bytenr and num_bytes to cover just the bytes
7033 * in this extent we are about to write. If there
7034 * are any csums in that range we have to cow in order
7035 * to keep the csums correct
7037 disk_bytenr
+= backref_offset
;
7038 disk_bytenr
+= offset
- key
.offset
;
7039 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
7042 * all of the above have passed, it is safe to overwrite this extent
7048 btrfs_free_path(path
);
7052 bool btrfs_page_exists_in_range(struct inode
*inode
, loff_t start
, loff_t end
)
7054 struct radix_tree_root
*root
= &inode
->i_mapping
->page_tree
;
7056 void **pagep
= NULL
;
7057 struct page
*page
= NULL
;
7061 start_idx
= start
>> PAGE_CACHE_SHIFT
;
7064 * end is the last byte in the last page. end == start is legal
7066 end_idx
= end
>> PAGE_CACHE_SHIFT
;
7070 /* Most of the code in this while loop is lifted from
7071 * find_get_page. It's been modified to begin searching from a
7072 * page and return just the first page found in that range. If the
7073 * found idx is less than or equal to the end idx then we know that
7074 * a page exists. If no pages are found or if those pages are
7075 * outside of the range then we're fine (yay!) */
7076 while (page
== NULL
&&
7077 radix_tree_gang_lookup_slot(root
, &pagep
, NULL
, start_idx
, 1)) {
7078 page
= radix_tree_deref_slot(pagep
);
7079 if (unlikely(!page
))
7082 if (radix_tree_exception(page
)) {
7083 if (radix_tree_deref_retry(page
)) {
7088 * Otherwise, shmem/tmpfs must be storing a swap entry
7089 * here as an exceptional entry: so return it without
7090 * attempting to raise page count.
7093 break; /* TODO: Is this relevant for this use case? */
7096 if (!page_cache_get_speculative(page
)) {
7102 * Has the page moved?
7103 * This is part of the lockless pagecache protocol. See
7104 * include/linux/pagemap.h for details.
7106 if (unlikely(page
!= *pagep
)) {
7107 page_cache_release(page
);
7113 if (page
->index
<= end_idx
)
7115 page_cache_release(page
);
7122 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
7123 struct extent_state
**cached_state
, int writing
)
7125 struct btrfs_ordered_extent
*ordered
;
7129 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7132 * We're concerned with the entire range that we're going to be
7133 * doing DIO to, so we need to make sure theres no ordered
7134 * extents in this range.
7136 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
7137 lockend
- lockstart
+ 1);
7140 * We need to make sure there are no buffered pages in this
7141 * range either, we could have raced between the invalidate in
7142 * generic_file_direct_write and locking the extent. The
7143 * invalidate needs to happen so that reads after a write do not
7148 !btrfs_page_exists_in_range(inode
, lockstart
, lockend
)))
7151 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7152 cached_state
, GFP_NOFS
);
7155 btrfs_start_ordered_extent(inode
, ordered
, 1);
7156 btrfs_put_ordered_extent(ordered
);
7158 /* Screw you mmap */
7159 ret
= btrfs_fdatawrite_range(inode
, lockstart
, lockend
);
7162 ret
= filemap_fdatawait_range(inode
->i_mapping
,
7169 * If we found a page that couldn't be invalidated just
7170 * fall back to buffered.
7172 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
7173 lockstart
>> PAGE_CACHE_SHIFT
,
7174 lockend
>> PAGE_CACHE_SHIFT
);
7185 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
7186 u64 len
, u64 orig_start
,
7187 u64 block_start
, u64 block_len
,
7188 u64 orig_block_len
, u64 ram_bytes
,
7191 struct extent_map_tree
*em_tree
;
7192 struct extent_map
*em
;
7193 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7196 em_tree
= &BTRFS_I(inode
)->extent_tree
;
7197 em
= alloc_extent_map();
7199 return ERR_PTR(-ENOMEM
);
7202 em
->orig_start
= orig_start
;
7203 em
->mod_start
= start
;
7206 em
->block_len
= block_len
;
7207 em
->block_start
= block_start
;
7208 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
7209 em
->orig_block_len
= orig_block_len
;
7210 em
->ram_bytes
= ram_bytes
;
7211 em
->generation
= -1;
7212 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
7213 if (type
== BTRFS_ORDERED_PREALLOC
)
7214 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
7217 btrfs_drop_extent_cache(inode
, em
->start
,
7218 em
->start
+ em
->len
- 1, 0);
7219 write_lock(&em_tree
->lock
);
7220 ret
= add_extent_mapping(em_tree
, em
, 1);
7221 write_unlock(&em_tree
->lock
);
7222 } while (ret
== -EEXIST
);
7225 free_extent_map(em
);
7226 return ERR_PTR(ret
);
7233 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
7234 struct buffer_head
*bh_result
, int create
)
7236 struct extent_map
*em
;
7237 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7238 struct extent_state
*cached_state
= NULL
;
7239 u64 start
= iblock
<< inode
->i_blkbits
;
7240 u64 lockstart
, lockend
;
7241 u64 len
= bh_result
->b_size
;
7243 int unlock_bits
= EXTENT_LOCKED
;
7247 unlock_bits
|= EXTENT_DIRTY
;
7249 len
= min_t(u64
, len
, root
->sectorsize
);
7252 lockend
= start
+ len
- 1;
7255 * If this errors out it's because we couldn't invalidate pagecache for
7256 * this range and we need to fallback to buffered.
7258 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
7261 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
7268 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7269 * io. INLINE is special, and we could probably kludge it in here, but
7270 * it's still buffered so for safety lets just fall back to the generic
7273 * For COMPRESSED we _have_ to read the entire extent in so we can
7274 * decompress it, so there will be buffering required no matter what we
7275 * do, so go ahead and fallback to buffered.
7277 * We return -ENOTBLK because thats what makes DIO go ahead and go back
7278 * to buffered IO. Don't blame me, this is the price we pay for using
7281 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
7282 em
->block_start
== EXTENT_MAP_INLINE
) {
7283 free_extent_map(em
);
7288 /* Just a good old fashioned hole, return */
7289 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
7290 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
7291 free_extent_map(em
);
7296 * We don't allocate a new extent in the following cases
7298 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7300 * 2) The extent is marked as PREALLOC. We're good to go here and can
7301 * just use the extent.
7305 len
= min(len
, em
->len
- (start
- em
->start
));
7306 lockstart
= start
+ len
;
7310 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
7311 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
7312 em
->block_start
!= EXTENT_MAP_HOLE
)) {
7314 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
7316 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7317 type
= BTRFS_ORDERED_PREALLOC
;
7319 type
= BTRFS_ORDERED_NOCOW
;
7320 len
= min(len
, em
->len
- (start
- em
->start
));
7321 block_start
= em
->block_start
+ (start
- em
->start
);
7323 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
7324 &orig_block_len
, &ram_bytes
) == 1) {
7325 if (type
== BTRFS_ORDERED_PREALLOC
) {
7326 free_extent_map(em
);
7327 em
= create_pinned_em(inode
, start
, len
,
7338 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
7339 block_start
, len
, len
, type
);
7341 free_extent_map(em
);
7349 * this will cow the extent, reset the len in case we changed
7352 len
= bh_result
->b_size
;
7353 free_extent_map(em
);
7354 em
= btrfs_new_extent_direct(inode
, start
, len
);
7359 len
= min(len
, em
->len
- (start
- em
->start
));
7361 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
7363 bh_result
->b_size
= len
;
7364 bh_result
->b_bdev
= em
->bdev
;
7365 set_buffer_mapped(bh_result
);
7367 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
7368 set_buffer_new(bh_result
);
7371 * Need to update the i_size under the extent lock so buffered
7372 * readers will get the updated i_size when we unlock.
7374 if (start
+ len
> i_size_read(inode
))
7375 i_size_write(inode
, start
+ len
);
7377 if (len
< orig_len
) {
7378 spin_lock(&BTRFS_I(inode
)->lock
);
7379 BTRFS_I(inode
)->outstanding_extents
++;
7380 spin_unlock(&BTRFS_I(inode
)->lock
);
7382 btrfs_free_reserved_data_space(inode
, len
);
7386 * In the case of write we need to clear and unlock the entire range,
7387 * in the case of read we need to unlock only the end area that we
7388 * aren't using if there is any left over space.
7390 if (lockstart
< lockend
) {
7391 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
7392 lockend
, unlock_bits
, 1, 0,
7393 &cached_state
, GFP_NOFS
);
7395 free_extent_state(cached_state
);
7398 free_extent_map(em
);
7403 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
7404 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
7408 static inline int submit_dio_repair_bio(struct inode
*inode
, struct bio
*bio
,
7409 int rw
, int mirror_num
)
7411 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7414 BUG_ON(rw
& REQ_WRITE
);
7418 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
,
7419 BTRFS_WQ_ENDIO_DIO_REPAIR
);
7423 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
7429 static int btrfs_check_dio_repairable(struct inode
*inode
,
7430 struct bio
*failed_bio
,
7431 struct io_failure_record
*failrec
,
7436 num_copies
= btrfs_num_copies(BTRFS_I(inode
)->root
->fs_info
,
7437 failrec
->logical
, failrec
->len
);
7438 if (num_copies
== 1) {
7440 * we only have a single copy of the data, so don't bother with
7441 * all the retry and error correction code that follows. no
7442 * matter what the error is, it is very likely to persist.
7444 pr_debug("Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
7445 num_copies
, failrec
->this_mirror
, failed_mirror
);
7449 failrec
->failed_mirror
= failed_mirror
;
7450 failrec
->this_mirror
++;
7451 if (failrec
->this_mirror
== failed_mirror
)
7452 failrec
->this_mirror
++;
7454 if (failrec
->this_mirror
> num_copies
) {
7455 pr_debug("Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
7456 num_copies
, failrec
->this_mirror
, failed_mirror
);
7463 static int dio_read_error(struct inode
*inode
, struct bio
*failed_bio
,
7464 struct page
*page
, u64 start
, u64 end
,
7465 int failed_mirror
, bio_end_io_t
*repair_endio
,
7468 struct io_failure_record
*failrec
;
7474 BUG_ON(failed_bio
->bi_rw
& REQ_WRITE
);
7476 ret
= btrfs_get_io_failure_record(inode
, start
, end
, &failrec
);
7480 ret
= btrfs_check_dio_repairable(inode
, failed_bio
, failrec
,
7483 free_io_failure(inode
, failrec
);
7487 if (failed_bio
->bi_vcnt
> 1)
7488 read_mode
= READ_SYNC
| REQ_FAILFAST_DEV
;
7490 read_mode
= READ_SYNC
;
7492 isector
= start
- btrfs_io_bio(failed_bio
)->logical
;
7493 isector
>>= inode
->i_sb
->s_blocksize_bits
;
7494 bio
= btrfs_create_repair_bio(inode
, failed_bio
, failrec
, page
,
7495 0, isector
, repair_endio
, repair_arg
);
7497 free_io_failure(inode
, failrec
);
7501 btrfs_debug(BTRFS_I(inode
)->root
->fs_info
,
7502 "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7503 read_mode
, failrec
->this_mirror
, failrec
->in_validation
);
7505 ret
= submit_dio_repair_bio(inode
, bio
, read_mode
,
7506 failrec
->this_mirror
);
7508 free_io_failure(inode
, failrec
);
7515 struct btrfs_retry_complete
{
7516 struct completion done
;
7517 struct inode
*inode
;
7522 static void btrfs_retry_endio_nocsum(struct bio
*bio
, int err
)
7524 struct btrfs_retry_complete
*done
= bio
->bi_private
;
7525 struct bio_vec
*bvec
;
7532 bio_for_each_segment_all(bvec
, bio
, i
)
7533 clean_io_failure(done
->inode
, done
->start
, bvec
->bv_page
, 0);
7535 complete(&done
->done
);
7539 static int __btrfs_correct_data_nocsum(struct inode
*inode
,
7540 struct btrfs_io_bio
*io_bio
)
7542 struct bio_vec
*bvec
;
7543 struct btrfs_retry_complete done
;
7548 start
= io_bio
->logical
;
7551 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
7555 init_completion(&done
.done
);
7557 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
, start
,
7558 start
+ bvec
->bv_len
- 1,
7560 btrfs_retry_endio_nocsum
, &done
);
7564 wait_for_completion(&done
.done
);
7566 if (!done
.uptodate
) {
7567 /* We might have another mirror, so try again */
7571 start
+= bvec
->bv_len
;
7577 static void btrfs_retry_endio(struct bio
*bio
, int err
)
7579 struct btrfs_retry_complete
*done
= bio
->bi_private
;
7580 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
7581 struct bio_vec
*bvec
;
7590 bio_for_each_segment_all(bvec
, bio
, i
) {
7591 ret
= __readpage_endio_check(done
->inode
, io_bio
, i
,
7593 done
->start
, bvec
->bv_len
);
7595 clean_io_failure(done
->inode
, done
->start
,
7601 done
->uptodate
= uptodate
;
7603 complete(&done
->done
);
7607 static int __btrfs_subio_endio_read(struct inode
*inode
,
7608 struct btrfs_io_bio
*io_bio
, int err
)
7610 struct bio_vec
*bvec
;
7611 struct btrfs_retry_complete done
;
7618 start
= io_bio
->logical
;
7621 bio_for_each_segment_all(bvec
, &io_bio
->bio
, i
) {
7622 ret
= __readpage_endio_check(inode
, io_bio
, i
, bvec
->bv_page
,
7623 0, start
, bvec
->bv_len
);
7629 init_completion(&done
.done
);
7631 ret
= dio_read_error(inode
, &io_bio
->bio
, bvec
->bv_page
, start
,
7632 start
+ bvec
->bv_len
- 1,
7634 btrfs_retry_endio
, &done
);
7640 wait_for_completion(&done
.done
);
7642 if (!done
.uptodate
) {
7643 /* We might have another mirror, so try again */
7647 offset
+= bvec
->bv_len
;
7648 start
+= bvec
->bv_len
;
7654 static int btrfs_subio_endio_read(struct inode
*inode
,
7655 struct btrfs_io_bio
*io_bio
, int err
)
7657 bool skip_csum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7661 return __btrfs_correct_data_nocsum(inode
, io_bio
);
7665 return __btrfs_subio_endio_read(inode
, io_bio
, err
);
7669 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
7671 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7672 struct inode
*inode
= dip
->inode
;
7673 struct bio
*dio_bio
;
7674 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
7676 if (dip
->flags
& BTRFS_DIO_ORIG_BIO_SUBMITTED
)
7677 err
= btrfs_subio_endio_read(inode
, io_bio
, err
);
7679 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
7680 dip
->logical_offset
+ dip
->bytes
- 1);
7681 dio_bio
= dip
->dio_bio
;
7685 /* If we had a csum failure make sure to clear the uptodate flag */
7687 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
7688 dio_end_io(dio_bio
, err
);
7691 io_bio
->end_io(io_bio
, err
);
7695 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
7697 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7698 struct inode
*inode
= dip
->inode
;
7699 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7700 struct btrfs_ordered_extent
*ordered
= NULL
;
7701 u64 ordered_offset
= dip
->logical_offset
;
7702 u64 ordered_bytes
= dip
->bytes
;
7703 struct bio
*dio_bio
;
7709 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
7711 ordered_bytes
, !err
);
7715 btrfs_init_work(&ordered
->work
, btrfs_endio_write_helper
,
7716 finish_ordered_fn
, NULL
, NULL
);
7717 btrfs_queue_work(root
->fs_info
->endio_write_workers
,
7721 * our bio might span multiple ordered extents. If we haven't
7722 * completed the accounting for the whole dio, go back and try again
7724 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
7725 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
7731 dio_bio
= dip
->dio_bio
;
7735 /* If we had an error make sure to clear the uptodate flag */
7737 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
7738 dio_end_io(dio_bio
, err
);
7742 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
7743 struct bio
*bio
, int mirror_num
,
7744 unsigned long bio_flags
, u64 offset
)
7747 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7748 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
7749 BUG_ON(ret
); /* -ENOMEM */
7753 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
7755 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7758 btrfs_warn(BTRFS_I(dip
->inode
)->root
->fs_info
,
7759 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
7760 btrfs_ino(dip
->inode
), bio
->bi_rw
,
7761 (unsigned long long)bio
->bi_iter
.bi_sector
,
7762 bio
->bi_iter
.bi_size
, err
);
7764 if (dip
->subio_endio
)
7765 err
= dip
->subio_endio(dip
->inode
, btrfs_io_bio(bio
), err
);
7771 * before atomic variable goto zero, we must make sure
7772 * dip->errors is perceived to be set.
7774 smp_mb__before_atomic();
7777 /* if there are more bios still pending for this dio, just exit */
7778 if (!atomic_dec_and_test(&dip
->pending_bios
))
7782 bio_io_error(dip
->orig_bio
);
7784 set_bit(BIO_UPTODATE
, &dip
->dio_bio
->bi_flags
);
7785 bio_endio(dip
->orig_bio
, 0);
7791 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
7792 u64 first_sector
, gfp_t gfp_flags
)
7794 int nr_vecs
= bio_get_nr_vecs(bdev
);
7795 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
7798 static inline int btrfs_lookup_and_bind_dio_csum(struct btrfs_root
*root
,
7799 struct inode
*inode
,
7800 struct btrfs_dio_private
*dip
,
7804 struct btrfs_io_bio
*io_bio
= btrfs_io_bio(bio
);
7805 struct btrfs_io_bio
*orig_io_bio
= btrfs_io_bio(dip
->orig_bio
);
7809 * We load all the csum data we need when we submit
7810 * the first bio to reduce the csum tree search and
7813 if (dip
->logical_offset
== file_offset
) {
7814 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, dip
->orig_bio
,
7820 if (bio
== dip
->orig_bio
)
7823 file_offset
-= dip
->logical_offset
;
7824 file_offset
>>= inode
->i_sb
->s_blocksize_bits
;
7825 io_bio
->csum
= (u8
*)(((u32
*)orig_io_bio
->csum
) + file_offset
);
7830 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
7831 int rw
, u64 file_offset
, int skip_sum
,
7834 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7835 int write
= rw
& REQ_WRITE
;
7836 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7840 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
7845 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
,
7846 BTRFS_WQ_ENDIO_DATA
);
7854 if (write
&& async_submit
) {
7855 ret
= btrfs_wq_submit_bio(root
->fs_info
,
7856 inode
, rw
, bio
, 0, 0,
7858 __btrfs_submit_bio_start_direct_io
,
7859 __btrfs_submit_bio_done
);
7863 * If we aren't doing async submit, calculate the csum of the
7866 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
7870 ret
= btrfs_lookup_and_bind_dio_csum(root
, inode
, dip
, bio
,
7876 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
7882 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
7885 struct inode
*inode
= dip
->inode
;
7886 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7888 struct bio
*orig_bio
= dip
->orig_bio
;
7889 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
7890 u64 start_sector
= orig_bio
->bi_iter
.bi_sector
;
7891 u64 file_offset
= dip
->logical_offset
;
7896 int async_submit
= 0;
7898 map_length
= orig_bio
->bi_iter
.bi_size
;
7899 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
7900 &map_length
, NULL
, 0);
7904 if (map_length
>= orig_bio
->bi_iter
.bi_size
) {
7906 dip
->flags
|= BTRFS_DIO_ORIG_BIO_SUBMITTED
;
7910 /* async crcs make it difficult to collect full stripe writes. */
7911 if (btrfs_get_alloc_profile(root
, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK
)
7916 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7920 bio
->bi_private
= dip
;
7921 bio
->bi_end_io
= btrfs_end_dio_bio
;
7922 btrfs_io_bio(bio
)->logical
= file_offset
;
7923 atomic_inc(&dip
->pending_bios
);
7925 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7926 if (map_length
< submit_len
+ bvec
->bv_len
||
7927 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7928 bvec
->bv_offset
) < bvec
->bv_len
) {
7930 * inc the count before we submit the bio so
7931 * we know the end IO handler won't happen before
7932 * we inc the count. Otherwise, the dip might get freed
7933 * before we're done setting it up
7935 atomic_inc(&dip
->pending_bios
);
7936 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7937 file_offset
, skip_sum
,
7941 atomic_dec(&dip
->pending_bios
);
7945 start_sector
+= submit_len
>> 9;
7946 file_offset
+= submit_len
;
7951 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7952 start_sector
, GFP_NOFS
);
7955 bio
->bi_private
= dip
;
7956 bio
->bi_end_io
= btrfs_end_dio_bio
;
7957 btrfs_io_bio(bio
)->logical
= file_offset
;
7959 map_length
= orig_bio
->bi_iter
.bi_size
;
7960 ret
= btrfs_map_block(root
->fs_info
, rw
,
7962 &map_length
, NULL
, 0);
7968 submit_len
+= bvec
->bv_len
;
7975 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7984 * before atomic variable goto zero, we must
7985 * make sure dip->errors is perceived to be set.
7987 smp_mb__before_atomic();
7988 if (atomic_dec_and_test(&dip
->pending_bios
))
7989 bio_io_error(dip
->orig_bio
);
7991 /* bio_end_io() will handle error, so we needn't return it */
7995 static void btrfs_submit_direct(int rw
, struct bio
*dio_bio
,
7996 struct inode
*inode
, loff_t file_offset
)
7998 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7999 struct btrfs_dio_private
*dip
;
8001 struct btrfs_io_bio
*btrfs_bio
;
8003 int write
= rw
& REQ_WRITE
;
8006 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
8008 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
8014 dip
= kzalloc(sizeof(*dip
), GFP_NOFS
);
8020 dip
->private = dio_bio
->bi_private
;
8022 dip
->logical_offset
= file_offset
;
8023 dip
->bytes
= dio_bio
->bi_iter
.bi_size
;
8024 dip
->disk_bytenr
= (u64
)dio_bio
->bi_iter
.bi_sector
<< 9;
8025 io_bio
->bi_private
= dip
;
8026 dip
->orig_bio
= io_bio
;
8027 dip
->dio_bio
= dio_bio
;
8028 atomic_set(&dip
->pending_bios
, 0);
8029 btrfs_bio
= btrfs_io_bio(io_bio
);
8030 btrfs_bio
->logical
= file_offset
;
8033 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
8035 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
8036 dip
->subio_endio
= btrfs_subio_endio_read
;
8039 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
8043 if (btrfs_bio
->end_io
)
8044 btrfs_bio
->end_io(btrfs_bio
, ret
);
8050 * If this is a write, we need to clean up the reserved space and kill
8051 * the ordered extent.
8054 struct btrfs_ordered_extent
*ordered
;
8055 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
8056 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
8057 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
8058 btrfs_free_reserved_extent(root
, ordered
->start
,
8059 ordered
->disk_len
, 1);
8060 btrfs_put_ordered_extent(ordered
);
8061 btrfs_put_ordered_extent(ordered
);
8063 bio_endio(dio_bio
, ret
);
8066 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
8067 const struct iov_iter
*iter
, loff_t offset
)
8071 unsigned blocksize_mask
= root
->sectorsize
- 1;
8072 ssize_t retval
= -EINVAL
;
8074 if (offset
& blocksize_mask
)
8077 if (iov_iter_alignment(iter
) & blocksize_mask
)
8080 /* If this is a write we don't need to check anymore */
8084 * Check to make sure we don't have duplicate iov_base's in this
8085 * iovec, if so return EINVAL, otherwise we'll get csum errors
8086 * when reading back.
8088 for (seg
= 0; seg
< iter
->nr_segs
; seg
++) {
8089 for (i
= seg
+ 1; i
< iter
->nr_segs
; i
++) {
8090 if (iter
->iov
[seg
].iov_base
== iter
->iov
[i
].iov_base
)
8099 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
8100 struct iov_iter
*iter
, loff_t offset
)
8102 struct file
*file
= iocb
->ki_filp
;
8103 struct inode
*inode
= file
->f_mapping
->host
;
8107 bool relock
= false;
8110 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iter
, offset
))
8113 atomic_inc(&inode
->i_dio_count
);
8114 smp_mb__after_atomic();
8117 * The generic stuff only does filemap_write_and_wait_range, which
8118 * isn't enough if we've written compressed pages to this area, so
8119 * we need to flush the dirty pages again to make absolutely sure
8120 * that any outstanding dirty pages are on disk.
8122 count
= iov_iter_count(iter
);
8123 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
8124 &BTRFS_I(inode
)->runtime_flags
))
8125 filemap_fdatawrite_range(inode
->i_mapping
, offset
,
8126 offset
+ count
- 1);
8130 * If the write DIO is beyond the EOF, we need update
8131 * the isize, but it is protected by i_mutex. So we can
8132 * not unlock the i_mutex at this case.
8134 if (offset
+ count
<= inode
->i_size
) {
8135 mutex_unlock(&inode
->i_mutex
);
8138 ret
= btrfs_delalloc_reserve_space(inode
, count
);
8141 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
8142 &BTRFS_I(inode
)->runtime_flags
)) {
8143 inode_dio_done(inode
);
8144 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
8148 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
8149 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
8150 iter
, offset
, btrfs_get_blocks_direct
, NULL
,
8151 btrfs_submit_direct
, flags
);
8153 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
8154 btrfs_delalloc_release_space(inode
, count
);
8155 else if (ret
>= 0 && (size_t)ret
< count
)
8156 btrfs_delalloc_release_space(inode
,
8157 count
- (size_t)ret
);
8161 inode_dio_done(inode
);
8163 mutex_lock(&inode
->i_mutex
);
8168 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8170 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
8171 __u64 start
, __u64 len
)
8175 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
8179 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
8182 int btrfs_readpage(struct file
*file
, struct page
*page
)
8184 struct extent_io_tree
*tree
;
8185 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8186 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
8189 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
8191 struct extent_io_tree
*tree
;
8194 if (current
->flags
& PF_MEMALLOC
) {
8195 redirty_page_for_writepage(wbc
, page
);
8199 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8200 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
8203 static int btrfs_writepages(struct address_space
*mapping
,
8204 struct writeback_control
*wbc
)
8206 struct extent_io_tree
*tree
;
8208 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8209 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
8213 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
8214 struct list_head
*pages
, unsigned nr_pages
)
8216 struct extent_io_tree
*tree
;
8217 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
8218 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
8221 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8223 struct extent_io_tree
*tree
;
8224 struct extent_map_tree
*map
;
8227 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
8228 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
8229 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
8231 ClearPagePrivate(page
);
8232 set_page_private(page
, 0);
8233 page_cache_release(page
);
8238 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
8240 if (PageWriteback(page
) || PageDirty(page
))
8242 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
8245 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
8246 unsigned int length
)
8248 struct inode
*inode
= page
->mapping
->host
;
8249 struct extent_io_tree
*tree
;
8250 struct btrfs_ordered_extent
*ordered
;
8251 struct extent_state
*cached_state
= NULL
;
8252 u64 page_start
= page_offset(page
);
8253 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
8254 int inode_evicting
= inode
->i_state
& I_FREEING
;
8257 * we have the page locked, so new writeback can't start,
8258 * and the dirty bit won't be cleared while we are here.
8260 * Wait for IO on this page so that we can safely clear
8261 * the PagePrivate2 bit and do ordered accounting
8263 wait_on_page_writeback(page
);
8265 tree
= &BTRFS_I(inode
)->io_tree
;
8267 btrfs_releasepage(page
, GFP_NOFS
);
8271 if (!inode_evicting
)
8272 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
8273 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
8276 * IO on this page will never be started, so we need
8277 * to account for any ordered extents now
8279 if (!inode_evicting
)
8280 clear_extent_bit(tree
, page_start
, page_end
,
8281 EXTENT_DIRTY
| EXTENT_DELALLOC
|
8282 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
8283 EXTENT_DEFRAG
, 1, 0, &cached_state
,
8286 * whoever cleared the private bit is responsible
8287 * for the finish_ordered_io
8289 if (TestClearPagePrivate2(page
)) {
8290 struct btrfs_ordered_inode_tree
*tree
;
8293 tree
= &BTRFS_I(inode
)->ordered_tree
;
8295 spin_lock_irq(&tree
->lock
);
8296 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
8297 new_len
= page_start
- ordered
->file_offset
;
8298 if (new_len
< ordered
->truncated_len
)
8299 ordered
->truncated_len
= new_len
;
8300 spin_unlock_irq(&tree
->lock
);
8302 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
8304 PAGE_CACHE_SIZE
, 1))
8305 btrfs_finish_ordered_io(ordered
);
8307 btrfs_put_ordered_extent(ordered
);
8308 if (!inode_evicting
) {
8309 cached_state
= NULL
;
8310 lock_extent_bits(tree
, page_start
, page_end
, 0,
8315 if (!inode_evicting
) {
8316 clear_extent_bit(tree
, page_start
, page_end
,
8317 EXTENT_LOCKED
| EXTENT_DIRTY
|
8318 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
8319 EXTENT_DEFRAG
, 1, 1,
8320 &cached_state
, GFP_NOFS
);
8322 __btrfs_releasepage(page
, GFP_NOFS
);
8325 ClearPageChecked(page
);
8326 if (PagePrivate(page
)) {
8327 ClearPagePrivate(page
);
8328 set_page_private(page
, 0);
8329 page_cache_release(page
);
8334 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8335 * called from a page fault handler when a page is first dirtied. Hence we must
8336 * be careful to check for EOF conditions here. We set the page up correctly
8337 * for a written page which means we get ENOSPC checking when writing into
8338 * holes and correct delalloc and unwritten extent mapping on filesystems that
8339 * support these features.
8341 * We are not allowed to take the i_mutex here so we have to play games to
8342 * protect against truncate races as the page could now be beyond EOF. Because
8343 * vmtruncate() writes the inode size before removing pages, once we have the
8344 * page lock we can determine safely if the page is beyond EOF. If it is not
8345 * beyond EOF, then the page is guaranteed safe against truncation until we
8348 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
8350 struct page
*page
= vmf
->page
;
8351 struct inode
*inode
= file_inode(vma
->vm_file
);
8352 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8353 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
8354 struct btrfs_ordered_extent
*ordered
;
8355 struct extent_state
*cached_state
= NULL
;
8357 unsigned long zero_start
;
8364 sb_start_pagefault(inode
->i_sb
);
8365 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
8367 ret
= file_update_time(vma
->vm_file
);
8373 else /* -ENOSPC, -EIO, etc */
8374 ret
= VM_FAULT_SIGBUS
;
8380 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
8383 size
= i_size_read(inode
);
8384 page_start
= page_offset(page
);
8385 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
8387 if ((page
->mapping
!= inode
->i_mapping
) ||
8388 (page_start
>= size
)) {
8389 /* page got truncated out from underneath us */
8392 wait_on_page_writeback(page
);
8394 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
8395 set_page_extent_mapped(page
);
8398 * we can't set the delalloc bits if there are pending ordered
8399 * extents. Drop our locks and wait for them to finish
8401 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
8403 unlock_extent_cached(io_tree
, page_start
, page_end
,
8404 &cached_state
, GFP_NOFS
);
8406 btrfs_start_ordered_extent(inode
, ordered
, 1);
8407 btrfs_put_ordered_extent(ordered
);
8412 * XXX - page_mkwrite gets called every time the page is dirtied, even
8413 * if it was already dirty, so for space accounting reasons we need to
8414 * clear any delalloc bits for the range we are fixing to save. There
8415 * is probably a better way to do this, but for now keep consistent with
8416 * prepare_pages in the normal write path.
8418 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
8419 EXTENT_DIRTY
| EXTENT_DELALLOC
|
8420 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
8421 0, 0, &cached_state
, GFP_NOFS
);
8423 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
8426 unlock_extent_cached(io_tree
, page_start
, page_end
,
8427 &cached_state
, GFP_NOFS
);
8428 ret
= VM_FAULT_SIGBUS
;
8433 /* page is wholly or partially inside EOF */
8434 if (page_start
+ PAGE_CACHE_SIZE
> size
)
8435 zero_start
= size
& ~PAGE_CACHE_MASK
;
8437 zero_start
= PAGE_CACHE_SIZE
;
8439 if (zero_start
!= PAGE_CACHE_SIZE
) {
8441 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
8442 flush_dcache_page(page
);
8445 ClearPageChecked(page
);
8446 set_page_dirty(page
);
8447 SetPageUptodate(page
);
8449 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
8450 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
8451 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
8453 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
8457 sb_end_pagefault(inode
->i_sb
);
8458 return VM_FAULT_LOCKED
;
8462 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
8464 sb_end_pagefault(inode
->i_sb
);
8468 static int btrfs_truncate(struct inode
*inode
)
8470 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8471 struct btrfs_block_rsv
*rsv
;
8474 struct btrfs_trans_handle
*trans
;
8475 u64 mask
= root
->sectorsize
- 1;
8476 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
8478 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
8484 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
8485 * 3 things going on here
8487 * 1) We need to reserve space for our orphan item and the space to
8488 * delete our orphan item. Lord knows we don't want to have a dangling
8489 * orphan item because we didn't reserve space to remove it.
8491 * 2) We need to reserve space to update our inode.
8493 * 3) We need to have something to cache all the space that is going to
8494 * be free'd up by the truncate operation, but also have some slack
8495 * space reserved in case it uses space during the truncate (thank you
8496 * very much snapshotting).
8498 * And we need these to all be seperate. The fact is we can use alot of
8499 * space doing the truncate, and we have no earthly idea how much space
8500 * we will use, so we need the truncate reservation to be seperate so it
8501 * doesn't end up using space reserved for updating the inode or
8502 * removing the orphan item. We also need to be able to stop the
8503 * transaction and start a new one, which means we need to be able to
8504 * update the inode several times, and we have no idea of knowing how
8505 * many times that will be, so we can't just reserve 1 item for the
8506 * entirety of the opration, so that has to be done seperately as well.
8507 * Then there is the orphan item, which does indeed need to be held on
8508 * to for the whole operation, and we need nobody to touch this reserved
8509 * space except the orphan code.
8511 * So that leaves us with
8513 * 1) root->orphan_block_rsv - for the orphan deletion.
8514 * 2) rsv - for the truncate reservation, which we will steal from the
8515 * transaction reservation.
8516 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
8517 * updating the inode.
8519 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
8522 rsv
->size
= min_size
;
8526 * 1 for the truncate slack space
8527 * 1 for updating the inode.
8529 trans
= btrfs_start_transaction(root
, 2);
8530 if (IS_ERR(trans
)) {
8531 err
= PTR_ERR(trans
);
8535 /* Migrate the slack space for the truncate to our reserve */
8536 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
8541 * So if we truncate and then write and fsync we normally would just
8542 * write the extents that changed, which is a problem if we need to
8543 * first truncate that entire inode. So set this flag so we write out
8544 * all of the extents in the inode to the sync log so we're completely
8547 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
8548 trans
->block_rsv
= rsv
;
8551 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
8553 BTRFS_EXTENT_DATA_KEY
);
8554 if (ret
!= -ENOSPC
) {
8559 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
8560 ret
= btrfs_update_inode(trans
, root
, inode
);
8566 btrfs_end_transaction(trans
, root
);
8567 btrfs_btree_balance_dirty(root
);
8569 trans
= btrfs_start_transaction(root
, 2);
8570 if (IS_ERR(trans
)) {
8571 ret
= err
= PTR_ERR(trans
);
8576 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
8578 BUG_ON(ret
); /* shouldn't happen */
8579 trans
->block_rsv
= rsv
;
8582 if (ret
== 0 && inode
->i_nlink
> 0) {
8583 trans
->block_rsv
= root
->orphan_block_rsv
;
8584 ret
= btrfs_orphan_del(trans
, inode
);
8590 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
8591 ret
= btrfs_update_inode(trans
, root
, inode
);
8595 ret
= btrfs_end_transaction(trans
, root
);
8596 btrfs_btree_balance_dirty(root
);
8600 btrfs_free_block_rsv(root
, rsv
);
8609 * create a new subvolume directory/inode (helper for the ioctl).
8611 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
8612 struct btrfs_root
*new_root
,
8613 struct btrfs_root
*parent_root
,
8616 struct inode
*inode
;
8620 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
8621 new_dirid
, new_dirid
,
8622 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
8625 return PTR_ERR(inode
);
8626 inode
->i_op
= &btrfs_dir_inode_operations
;
8627 inode
->i_fop
= &btrfs_dir_file_operations
;
8629 set_nlink(inode
, 1);
8630 btrfs_i_size_write(inode
, 0);
8631 unlock_new_inode(inode
);
8633 err
= btrfs_subvol_inherit_props(trans
, new_root
, parent_root
);
8635 btrfs_err(new_root
->fs_info
,
8636 "error inheriting subvolume %llu properties: %d",
8637 new_root
->root_key
.objectid
, err
);
8639 err
= btrfs_update_inode(trans
, new_root
, inode
);
8645 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
8647 struct btrfs_inode
*ei
;
8648 struct inode
*inode
;
8650 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
8657 ei
->last_sub_trans
= 0;
8658 ei
->logged_trans
= 0;
8659 ei
->delalloc_bytes
= 0;
8660 ei
->defrag_bytes
= 0;
8661 ei
->disk_i_size
= 0;
8664 ei
->index_cnt
= (u64
)-1;
8666 ei
->last_unlink_trans
= 0;
8667 ei
->last_log_commit
= 0;
8669 spin_lock_init(&ei
->lock
);
8670 ei
->outstanding_extents
= 0;
8671 ei
->reserved_extents
= 0;
8673 ei
->runtime_flags
= 0;
8674 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
8676 ei
->delayed_node
= NULL
;
8678 ei
->i_otime
.tv_sec
= 0;
8679 ei
->i_otime
.tv_nsec
= 0;
8681 inode
= &ei
->vfs_inode
;
8682 extent_map_tree_init(&ei
->extent_tree
);
8683 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
8684 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
8685 ei
->io_tree
.track_uptodate
= 1;
8686 ei
->io_failure_tree
.track_uptodate
= 1;
8687 atomic_set(&ei
->sync_writers
, 0);
8688 mutex_init(&ei
->log_mutex
);
8689 mutex_init(&ei
->delalloc_mutex
);
8690 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
8691 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
8692 RB_CLEAR_NODE(&ei
->rb_node
);
8697 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8698 void btrfs_test_destroy_inode(struct inode
*inode
)
8700 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
8701 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
8705 static void btrfs_i_callback(struct rcu_head
*head
)
8707 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
8708 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
8711 void btrfs_destroy_inode(struct inode
*inode
)
8713 struct btrfs_ordered_extent
*ordered
;
8714 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8716 WARN_ON(!hlist_empty(&inode
->i_dentry
));
8717 WARN_ON(inode
->i_data
.nrpages
);
8718 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
8719 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
8720 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
8721 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
8722 WARN_ON(BTRFS_I(inode
)->defrag_bytes
);
8725 * This can happen where we create an inode, but somebody else also
8726 * created the same inode and we need to destroy the one we already
8732 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
8733 &BTRFS_I(inode
)->runtime_flags
)) {
8734 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
8736 atomic_dec(&root
->orphan_inodes
);
8740 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
8744 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
8745 ordered
->file_offset
, ordered
->len
);
8746 btrfs_remove_ordered_extent(inode
, ordered
);
8747 btrfs_put_ordered_extent(ordered
);
8748 btrfs_put_ordered_extent(ordered
);
8751 inode_tree_del(inode
);
8752 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
8754 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
8757 int btrfs_drop_inode(struct inode
*inode
)
8759 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8764 /* the snap/subvol tree is on deleting */
8765 if (btrfs_root_refs(&root
->root_item
) == 0)
8768 return generic_drop_inode(inode
);
8771 static void init_once(void *foo
)
8773 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
8775 inode_init_once(&ei
->vfs_inode
);
8778 void btrfs_destroy_cachep(void)
8781 * Make sure all delayed rcu free inodes are flushed before we
8785 if (btrfs_inode_cachep
)
8786 kmem_cache_destroy(btrfs_inode_cachep
);
8787 if (btrfs_trans_handle_cachep
)
8788 kmem_cache_destroy(btrfs_trans_handle_cachep
);
8789 if (btrfs_transaction_cachep
)
8790 kmem_cache_destroy(btrfs_transaction_cachep
);
8791 if (btrfs_path_cachep
)
8792 kmem_cache_destroy(btrfs_path_cachep
);
8793 if (btrfs_free_space_cachep
)
8794 kmem_cache_destroy(btrfs_free_space_cachep
);
8795 if (btrfs_delalloc_work_cachep
)
8796 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
8799 int btrfs_init_cachep(void)
8801 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
8802 sizeof(struct btrfs_inode
), 0,
8803 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
8804 if (!btrfs_inode_cachep
)
8807 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
8808 sizeof(struct btrfs_trans_handle
), 0,
8809 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8810 if (!btrfs_trans_handle_cachep
)
8813 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
8814 sizeof(struct btrfs_transaction
), 0,
8815 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8816 if (!btrfs_transaction_cachep
)
8819 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
8820 sizeof(struct btrfs_path
), 0,
8821 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8822 if (!btrfs_path_cachep
)
8825 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
8826 sizeof(struct btrfs_free_space
), 0,
8827 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8828 if (!btrfs_free_space_cachep
)
8831 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
8832 sizeof(struct btrfs_delalloc_work
), 0,
8833 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
8835 if (!btrfs_delalloc_work_cachep
)
8840 btrfs_destroy_cachep();
8844 static int btrfs_getattr(struct vfsmount
*mnt
,
8845 struct dentry
*dentry
, struct kstat
*stat
)
8848 struct inode
*inode
= dentry
->d_inode
;
8849 u32 blocksize
= inode
->i_sb
->s_blocksize
;
8851 generic_fillattr(inode
, stat
);
8852 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
8853 stat
->blksize
= PAGE_CACHE_SIZE
;
8855 spin_lock(&BTRFS_I(inode
)->lock
);
8856 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
8857 spin_unlock(&BTRFS_I(inode
)->lock
);
8858 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
8859 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
8863 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
8864 struct inode
*new_dir
, struct dentry
*new_dentry
)
8866 struct btrfs_trans_handle
*trans
;
8867 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
8868 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
8869 struct inode
*new_inode
= new_dentry
->d_inode
;
8870 struct inode
*old_inode
= old_dentry
->d_inode
;
8871 struct timespec ctime
= CURRENT_TIME
;
8875 u64 old_ino
= btrfs_ino(old_inode
);
8877 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
8880 /* we only allow rename subvolume link between subvolumes */
8881 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8884 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
8885 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
8888 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
8889 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
8893 /* check for collisions, even if the name isn't there */
8894 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
8895 new_dentry
->d_name
.name
,
8896 new_dentry
->d_name
.len
);
8899 if (ret
== -EEXIST
) {
8901 * eexist without a new_inode */
8902 if (WARN_ON(!new_inode
)) {
8906 /* maybe -EOVERFLOW */
8913 * we're using rename to replace one file with another. Start IO on it
8914 * now so we don't add too much work to the end of the transaction
8916 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
)
8917 filemap_flush(old_inode
->i_mapping
);
8919 /* close the racy window with snapshot create/destroy ioctl */
8920 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8921 down_read(&root
->fs_info
->subvol_sem
);
8923 * We want to reserve the absolute worst case amount of items. So if
8924 * both inodes are subvols and we need to unlink them then that would
8925 * require 4 item modifications, but if they are both normal inodes it
8926 * would require 5 item modifications, so we'll assume their normal
8927 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8928 * should cover the worst case number of items we'll modify.
8930 trans
= btrfs_start_transaction(root
, 11);
8931 if (IS_ERR(trans
)) {
8932 ret
= PTR_ERR(trans
);
8937 btrfs_record_root_in_trans(trans
, dest
);
8939 ret
= btrfs_set_inode_index(new_dir
, &index
);
8943 BTRFS_I(old_inode
)->dir_index
= 0ULL;
8944 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8945 /* force full log commit if subvolume involved. */
8946 btrfs_set_log_full_commit(root
->fs_info
, trans
);
8948 ret
= btrfs_insert_inode_ref(trans
, dest
,
8949 new_dentry
->d_name
.name
,
8950 new_dentry
->d_name
.len
,
8952 btrfs_ino(new_dir
), index
);
8956 * this is an ugly little race, but the rename is required
8957 * to make sure that if we crash, the inode is either at the
8958 * old name or the new one. pinning the log transaction lets
8959 * us make sure we don't allow a log commit to come in after
8960 * we unlink the name but before we add the new name back in.
8962 btrfs_pin_log_trans(root
);
8965 inode_inc_iversion(old_dir
);
8966 inode_inc_iversion(new_dir
);
8967 inode_inc_iversion(old_inode
);
8968 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8969 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8970 old_inode
->i_ctime
= ctime
;
8972 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8973 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8975 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8976 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8977 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8978 old_dentry
->d_name
.name
,
8979 old_dentry
->d_name
.len
);
8981 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8982 old_dentry
->d_inode
,
8983 old_dentry
->d_name
.name
,
8984 old_dentry
->d_name
.len
);
8986 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8989 btrfs_abort_transaction(trans
, root
, ret
);
8994 inode_inc_iversion(new_inode
);
8995 new_inode
->i_ctime
= CURRENT_TIME
;
8996 if (unlikely(btrfs_ino(new_inode
) ==
8997 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8998 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8999 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
9001 new_dentry
->d_name
.name
,
9002 new_dentry
->d_name
.len
);
9003 BUG_ON(new_inode
->i_nlink
== 0);
9005 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
9006 new_dentry
->d_inode
,
9007 new_dentry
->d_name
.name
,
9008 new_dentry
->d_name
.len
);
9010 if (!ret
&& new_inode
->i_nlink
== 0)
9011 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
9013 btrfs_abort_transaction(trans
, root
, ret
);
9018 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
9019 new_dentry
->d_name
.name
,
9020 new_dentry
->d_name
.len
, 0, index
);
9022 btrfs_abort_transaction(trans
, root
, ret
);
9026 if (old_inode
->i_nlink
== 1)
9027 BTRFS_I(old_inode
)->dir_index
= index
;
9029 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
9030 struct dentry
*parent
= new_dentry
->d_parent
;
9031 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
9032 btrfs_end_log_trans(root
);
9035 btrfs_end_transaction(trans
, root
);
9037 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
9038 up_read(&root
->fs_info
->subvol_sem
);
9043 static int btrfs_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
,
9044 struct inode
*new_dir
, struct dentry
*new_dentry
,
9047 if (flags
& ~RENAME_NOREPLACE
)
9050 return btrfs_rename(old_dir
, old_dentry
, new_dir
, new_dentry
);
9053 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
9055 struct btrfs_delalloc_work
*delalloc_work
;
9056 struct inode
*inode
;
9058 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
9060 inode
= delalloc_work
->inode
;
9061 if (delalloc_work
->wait
) {
9062 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
9064 filemap_flush(inode
->i_mapping
);
9065 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
9066 &BTRFS_I(inode
)->runtime_flags
))
9067 filemap_flush(inode
->i_mapping
);
9070 if (delalloc_work
->delay_iput
)
9071 btrfs_add_delayed_iput(inode
);
9074 complete(&delalloc_work
->completion
);
9077 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
9078 int wait
, int delay_iput
)
9080 struct btrfs_delalloc_work
*work
;
9082 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
9086 init_completion(&work
->completion
);
9087 INIT_LIST_HEAD(&work
->list
);
9088 work
->inode
= inode
;
9090 work
->delay_iput
= delay_iput
;
9091 WARN_ON_ONCE(!inode
);
9092 btrfs_init_work(&work
->work
, btrfs_flush_delalloc_helper
,
9093 btrfs_run_delalloc_work
, NULL
, NULL
);
9098 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
9100 wait_for_completion(&work
->completion
);
9101 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
9105 * some fairly slow code that needs optimization. This walks the list
9106 * of all the inodes with pending delalloc and forces them to disk.
9108 static int __start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
,
9111 struct btrfs_inode
*binode
;
9112 struct inode
*inode
;
9113 struct btrfs_delalloc_work
*work
, *next
;
9114 struct list_head works
;
9115 struct list_head splice
;
9118 INIT_LIST_HEAD(&works
);
9119 INIT_LIST_HEAD(&splice
);
9121 mutex_lock(&root
->delalloc_mutex
);
9122 spin_lock(&root
->delalloc_lock
);
9123 list_splice_init(&root
->delalloc_inodes
, &splice
);
9124 while (!list_empty(&splice
)) {
9125 binode
= list_entry(splice
.next
, struct btrfs_inode
,
9128 list_move_tail(&binode
->delalloc_inodes
,
9129 &root
->delalloc_inodes
);
9130 inode
= igrab(&binode
->vfs_inode
);
9132 cond_resched_lock(&root
->delalloc_lock
);
9135 spin_unlock(&root
->delalloc_lock
);
9137 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
9140 btrfs_add_delayed_iput(inode
);
9146 list_add_tail(&work
->list
, &works
);
9147 btrfs_queue_work(root
->fs_info
->flush_workers
,
9150 if (nr
!= -1 && ret
>= nr
)
9153 spin_lock(&root
->delalloc_lock
);
9155 spin_unlock(&root
->delalloc_lock
);
9158 list_for_each_entry_safe(work
, next
, &works
, list
) {
9159 list_del_init(&work
->list
);
9160 btrfs_wait_and_free_delalloc_work(work
);
9163 if (!list_empty_careful(&splice
)) {
9164 spin_lock(&root
->delalloc_lock
);
9165 list_splice_tail(&splice
, &root
->delalloc_inodes
);
9166 spin_unlock(&root
->delalloc_lock
);
9168 mutex_unlock(&root
->delalloc_mutex
);
9172 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
9176 if (test_bit(BTRFS_FS_STATE_ERROR
, &root
->fs_info
->fs_state
))
9179 ret
= __start_delalloc_inodes(root
, delay_iput
, -1);
9183 * the filemap_flush will queue IO into the worker threads, but
9184 * we have to make sure the IO is actually started and that
9185 * ordered extents get created before we return
9187 atomic_inc(&root
->fs_info
->async_submit_draining
);
9188 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
9189 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
9190 wait_event(root
->fs_info
->async_submit_wait
,
9191 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
9192 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
9194 atomic_dec(&root
->fs_info
->async_submit_draining
);
9198 int btrfs_start_delalloc_roots(struct btrfs_fs_info
*fs_info
, int delay_iput
,
9201 struct btrfs_root
*root
;
9202 struct list_head splice
;
9205 if (test_bit(BTRFS_FS_STATE_ERROR
, &fs_info
->fs_state
))
9208 INIT_LIST_HEAD(&splice
);
9210 mutex_lock(&fs_info
->delalloc_root_mutex
);
9211 spin_lock(&fs_info
->delalloc_root_lock
);
9212 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
9213 while (!list_empty(&splice
) && nr
) {
9214 root
= list_first_entry(&splice
, struct btrfs_root
,
9216 root
= btrfs_grab_fs_root(root
);
9218 list_move_tail(&root
->delalloc_root
,
9219 &fs_info
->delalloc_roots
);
9220 spin_unlock(&fs_info
->delalloc_root_lock
);
9222 ret
= __start_delalloc_inodes(root
, delay_iput
, nr
);
9223 btrfs_put_fs_root(root
);
9231 spin_lock(&fs_info
->delalloc_root_lock
);
9233 spin_unlock(&fs_info
->delalloc_root_lock
);
9236 atomic_inc(&fs_info
->async_submit_draining
);
9237 while (atomic_read(&fs_info
->nr_async_submits
) ||
9238 atomic_read(&fs_info
->async_delalloc_pages
)) {
9239 wait_event(fs_info
->async_submit_wait
,
9240 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
9241 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
9243 atomic_dec(&fs_info
->async_submit_draining
);
9245 if (!list_empty_careful(&splice
)) {
9246 spin_lock(&fs_info
->delalloc_root_lock
);
9247 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
9248 spin_unlock(&fs_info
->delalloc_root_lock
);
9250 mutex_unlock(&fs_info
->delalloc_root_mutex
);
9254 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
9255 const char *symname
)
9257 struct btrfs_trans_handle
*trans
;
9258 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
9259 struct btrfs_path
*path
;
9260 struct btrfs_key key
;
9261 struct inode
*inode
= NULL
;
9269 struct btrfs_file_extent_item
*ei
;
9270 struct extent_buffer
*leaf
;
9272 name_len
= strlen(symname
);
9273 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
9274 return -ENAMETOOLONG
;
9277 * 2 items for inode item and ref
9278 * 2 items for dir items
9279 * 1 item for xattr if selinux is on
9281 trans
= btrfs_start_transaction(root
, 5);
9283 return PTR_ERR(trans
);
9285 err
= btrfs_find_free_ino(root
, &objectid
);
9289 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
9290 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
9291 S_IFLNK
|S_IRWXUGO
, &index
);
9292 if (IS_ERR(inode
)) {
9293 err
= PTR_ERR(inode
);
9298 * If the active LSM wants to access the inode during
9299 * d_instantiate it needs these. Smack checks to see
9300 * if the filesystem supports xattrs by looking at the
9303 inode
->i_fop
= &btrfs_file_operations
;
9304 inode
->i_op
= &btrfs_file_inode_operations
;
9305 inode
->i_mapping
->a_ops
= &btrfs_aops
;
9306 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
9307 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
9309 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
9311 goto out_unlock_inode
;
9313 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
9315 goto out_unlock_inode
;
9317 path
= btrfs_alloc_path();
9320 goto out_unlock_inode
;
9322 key
.objectid
= btrfs_ino(inode
);
9324 key
.type
= BTRFS_EXTENT_DATA_KEY
;
9325 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
9326 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
9329 btrfs_free_path(path
);
9330 goto out_unlock_inode
;
9332 leaf
= path
->nodes
[0];
9333 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
9334 struct btrfs_file_extent_item
);
9335 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
9336 btrfs_set_file_extent_type(leaf
, ei
,
9337 BTRFS_FILE_EXTENT_INLINE
);
9338 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
9339 btrfs_set_file_extent_compression(leaf
, ei
, 0);
9340 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
9341 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
9343 ptr
= btrfs_file_extent_inline_start(ei
);
9344 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
9345 btrfs_mark_buffer_dirty(leaf
);
9346 btrfs_free_path(path
);
9348 inode
->i_op
= &btrfs_symlink_inode_operations
;
9349 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
9350 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
9351 inode_set_bytes(inode
, name_len
);
9352 btrfs_i_size_write(inode
, name_len
);
9353 err
= btrfs_update_inode(trans
, root
, inode
);
9356 goto out_unlock_inode
;
9359 unlock_new_inode(inode
);
9360 d_instantiate(dentry
, inode
);
9363 btrfs_end_transaction(trans
, root
);
9365 inode_dec_link_count(inode
);
9368 btrfs_btree_balance_dirty(root
);
9373 unlock_new_inode(inode
);
9377 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
9378 u64 start
, u64 num_bytes
, u64 min_size
,
9379 loff_t actual_len
, u64
*alloc_hint
,
9380 struct btrfs_trans_handle
*trans
)
9382 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
9383 struct extent_map
*em
;
9384 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9385 struct btrfs_key ins
;
9386 u64 cur_offset
= start
;
9390 bool own_trans
= true;
9394 while (num_bytes
> 0) {
9396 trans
= btrfs_start_transaction(root
, 3);
9397 if (IS_ERR(trans
)) {
9398 ret
= PTR_ERR(trans
);
9403 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
9404 cur_bytes
= max(cur_bytes
, min_size
);
9405 ret
= btrfs_reserve_extent(root
, cur_bytes
, min_size
, 0,
9406 *alloc_hint
, &ins
, 1, 0);
9409 btrfs_end_transaction(trans
, root
);
9413 ret
= insert_reserved_file_extent(trans
, inode
,
9414 cur_offset
, ins
.objectid
,
9415 ins
.offset
, ins
.offset
,
9416 ins
.offset
, 0, 0, 0,
9417 BTRFS_FILE_EXTENT_PREALLOC
);
9419 btrfs_free_reserved_extent(root
, ins
.objectid
,
9421 btrfs_abort_transaction(trans
, root
, ret
);
9423 btrfs_end_transaction(trans
, root
);
9426 btrfs_drop_extent_cache(inode
, cur_offset
,
9427 cur_offset
+ ins
.offset
-1, 0);
9429 em
= alloc_extent_map();
9431 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
9432 &BTRFS_I(inode
)->runtime_flags
);
9436 em
->start
= cur_offset
;
9437 em
->orig_start
= cur_offset
;
9438 em
->len
= ins
.offset
;
9439 em
->block_start
= ins
.objectid
;
9440 em
->block_len
= ins
.offset
;
9441 em
->orig_block_len
= ins
.offset
;
9442 em
->ram_bytes
= ins
.offset
;
9443 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
9444 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
9445 em
->generation
= trans
->transid
;
9448 write_lock(&em_tree
->lock
);
9449 ret
= add_extent_mapping(em_tree
, em
, 1);
9450 write_unlock(&em_tree
->lock
);
9453 btrfs_drop_extent_cache(inode
, cur_offset
,
9454 cur_offset
+ ins
.offset
- 1,
9457 free_extent_map(em
);
9459 num_bytes
-= ins
.offset
;
9460 cur_offset
+= ins
.offset
;
9461 *alloc_hint
= ins
.objectid
+ ins
.offset
;
9463 inode_inc_iversion(inode
);
9464 inode
->i_ctime
= CURRENT_TIME
;
9465 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
9466 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
9467 (actual_len
> inode
->i_size
) &&
9468 (cur_offset
> inode
->i_size
)) {
9469 if (cur_offset
> actual_len
)
9470 i_size
= actual_len
;
9472 i_size
= cur_offset
;
9473 i_size_write(inode
, i_size
);
9474 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
9477 ret
= btrfs_update_inode(trans
, root
, inode
);
9480 btrfs_abort_transaction(trans
, root
, ret
);
9482 btrfs_end_transaction(trans
, root
);
9487 btrfs_end_transaction(trans
, root
);
9492 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
9493 u64 start
, u64 num_bytes
, u64 min_size
,
9494 loff_t actual_len
, u64
*alloc_hint
)
9496 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
9497 min_size
, actual_len
, alloc_hint
,
9501 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
9502 struct btrfs_trans_handle
*trans
, int mode
,
9503 u64 start
, u64 num_bytes
, u64 min_size
,
9504 loff_t actual_len
, u64
*alloc_hint
)
9506 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
9507 min_size
, actual_len
, alloc_hint
, trans
);
9510 static int btrfs_set_page_dirty(struct page
*page
)
9512 return __set_page_dirty_nobuffers(page
);
9515 static int btrfs_permission(struct inode
*inode
, int mask
)
9517 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
9518 umode_t mode
= inode
->i_mode
;
9520 if (mask
& MAY_WRITE
&&
9521 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
9522 if (btrfs_root_readonly(root
))
9524 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
9527 return generic_permission(inode
, mask
);
9530 static int btrfs_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
9532 struct btrfs_trans_handle
*trans
;
9533 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
9534 struct inode
*inode
= NULL
;
9540 * 5 units required for adding orphan entry
9542 trans
= btrfs_start_transaction(root
, 5);
9544 return PTR_ERR(trans
);
9546 ret
= btrfs_find_free_ino(root
, &objectid
);
9550 inode
= btrfs_new_inode(trans
, root
, dir
, NULL
, 0,
9551 btrfs_ino(dir
), objectid
, mode
, &index
);
9552 if (IS_ERR(inode
)) {
9553 ret
= PTR_ERR(inode
);
9558 inode
->i_fop
= &btrfs_file_operations
;
9559 inode
->i_op
= &btrfs_file_inode_operations
;
9561 inode
->i_mapping
->a_ops
= &btrfs_aops
;
9562 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
9563 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
9565 ret
= btrfs_init_inode_security(trans
, inode
, dir
, NULL
);
9569 ret
= btrfs_update_inode(trans
, root
, inode
);
9572 ret
= btrfs_orphan_add(trans
, inode
);
9577 * We set number of links to 0 in btrfs_new_inode(), and here we set
9578 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9581 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9583 set_nlink(inode
, 1);
9584 unlock_new_inode(inode
);
9585 d_tmpfile(dentry
, inode
);
9586 mark_inode_dirty(inode
);
9589 btrfs_end_transaction(trans
, root
);
9592 btrfs_balance_delayed_items(root
);
9593 btrfs_btree_balance_dirty(root
);
9597 unlock_new_inode(inode
);
9602 /* Inspired by filemap_check_errors() */
9603 int btrfs_inode_check_errors(struct inode
*inode
)
9607 if (test_bit(AS_ENOSPC
, &inode
->i_mapping
->flags
) &&
9608 test_and_clear_bit(AS_ENOSPC
, &inode
->i_mapping
->flags
))
9610 if (test_bit(AS_EIO
, &inode
->i_mapping
->flags
) &&
9611 test_and_clear_bit(AS_EIO
, &inode
->i_mapping
->flags
))
9617 static const struct inode_operations btrfs_dir_inode_operations
= {
9618 .getattr
= btrfs_getattr
,
9619 .lookup
= btrfs_lookup
,
9620 .create
= btrfs_create
,
9621 .unlink
= btrfs_unlink
,
9623 .mkdir
= btrfs_mkdir
,
9624 .rmdir
= btrfs_rmdir
,
9625 .rename2
= btrfs_rename2
,
9626 .symlink
= btrfs_symlink
,
9627 .setattr
= btrfs_setattr
,
9628 .mknod
= btrfs_mknod
,
9629 .setxattr
= btrfs_setxattr
,
9630 .getxattr
= btrfs_getxattr
,
9631 .listxattr
= btrfs_listxattr
,
9632 .removexattr
= btrfs_removexattr
,
9633 .permission
= btrfs_permission
,
9634 .get_acl
= btrfs_get_acl
,
9635 .set_acl
= btrfs_set_acl
,
9636 .update_time
= btrfs_update_time
,
9637 .tmpfile
= btrfs_tmpfile
,
9639 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
9640 .lookup
= btrfs_lookup
,
9641 .permission
= btrfs_permission
,
9642 .get_acl
= btrfs_get_acl
,
9643 .set_acl
= btrfs_set_acl
,
9644 .update_time
= btrfs_update_time
,
9647 static const struct file_operations btrfs_dir_file_operations
= {
9648 .llseek
= generic_file_llseek
,
9649 .read
= generic_read_dir
,
9650 .iterate
= btrfs_real_readdir
,
9651 .unlocked_ioctl
= btrfs_ioctl
,
9652 #ifdef CONFIG_COMPAT
9653 .compat_ioctl
= btrfs_ioctl
,
9655 .release
= btrfs_release_file
,
9656 .fsync
= btrfs_sync_file
,
9659 static struct extent_io_ops btrfs_extent_io_ops
= {
9660 .fill_delalloc
= run_delalloc_range
,
9661 .submit_bio_hook
= btrfs_submit_bio_hook
,
9662 .merge_bio_hook
= btrfs_merge_bio_hook
,
9663 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
9664 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
9665 .writepage_start_hook
= btrfs_writepage_start_hook
,
9666 .set_bit_hook
= btrfs_set_bit_hook
,
9667 .clear_bit_hook
= btrfs_clear_bit_hook
,
9668 .merge_extent_hook
= btrfs_merge_extent_hook
,
9669 .split_extent_hook
= btrfs_split_extent_hook
,
9673 * btrfs doesn't support the bmap operation because swapfiles
9674 * use bmap to make a mapping of extents in the file. They assume
9675 * these extents won't change over the life of the file and they
9676 * use the bmap result to do IO directly to the drive.
9678 * the btrfs bmap call would return logical addresses that aren't
9679 * suitable for IO and they also will change frequently as COW
9680 * operations happen. So, swapfile + btrfs == corruption.
9682 * For now we're avoiding this by dropping bmap.
9684 static const struct address_space_operations btrfs_aops
= {
9685 .readpage
= btrfs_readpage
,
9686 .writepage
= btrfs_writepage
,
9687 .writepages
= btrfs_writepages
,
9688 .readpages
= btrfs_readpages
,
9689 .direct_IO
= btrfs_direct_IO
,
9690 .invalidatepage
= btrfs_invalidatepage
,
9691 .releasepage
= btrfs_releasepage
,
9692 .set_page_dirty
= btrfs_set_page_dirty
,
9693 .error_remove_page
= generic_error_remove_page
,
9696 static const struct address_space_operations btrfs_symlink_aops
= {
9697 .readpage
= btrfs_readpage
,
9698 .writepage
= btrfs_writepage
,
9699 .invalidatepage
= btrfs_invalidatepage
,
9700 .releasepage
= btrfs_releasepage
,
9703 static const struct inode_operations btrfs_file_inode_operations
= {
9704 .getattr
= btrfs_getattr
,
9705 .setattr
= btrfs_setattr
,
9706 .setxattr
= btrfs_setxattr
,
9707 .getxattr
= btrfs_getxattr
,
9708 .listxattr
= btrfs_listxattr
,
9709 .removexattr
= btrfs_removexattr
,
9710 .permission
= btrfs_permission
,
9711 .fiemap
= btrfs_fiemap
,
9712 .get_acl
= btrfs_get_acl
,
9713 .set_acl
= btrfs_set_acl
,
9714 .update_time
= btrfs_update_time
,
9716 static const struct inode_operations btrfs_special_inode_operations
= {
9717 .getattr
= btrfs_getattr
,
9718 .setattr
= btrfs_setattr
,
9719 .permission
= btrfs_permission
,
9720 .setxattr
= btrfs_setxattr
,
9721 .getxattr
= btrfs_getxattr
,
9722 .listxattr
= btrfs_listxattr
,
9723 .removexattr
= btrfs_removexattr
,
9724 .get_acl
= btrfs_get_acl
,
9725 .set_acl
= btrfs_set_acl
,
9726 .update_time
= btrfs_update_time
,
9728 static const struct inode_operations btrfs_symlink_inode_operations
= {
9729 .readlink
= generic_readlink
,
9730 .follow_link
= page_follow_link_light
,
9731 .put_link
= page_put_link
,
9732 .getattr
= btrfs_getattr
,
9733 .setattr
= btrfs_setattr
,
9734 .permission
= btrfs_permission
,
9735 .setxattr
= btrfs_setxattr
,
9736 .getxattr
= btrfs_getxattr
,
9737 .listxattr
= btrfs_listxattr
,
9738 .removexattr
= btrfs_removexattr
,
9739 .update_time
= btrfs_update_time
,
9742 const struct dentry_operations btrfs_dentry_operations
= {
9743 .d_delete
= btrfs_dentry_delete
,
9744 .d_release
= btrfs_dentry_release
,