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"
62 struct btrfs_iget_args
{
64 struct btrfs_root
*root
;
67 static const struct inode_operations btrfs_dir_inode_operations
;
68 static const struct inode_operations btrfs_symlink_inode_operations
;
69 static const struct inode_operations btrfs_dir_ro_inode_operations
;
70 static const struct inode_operations btrfs_special_inode_operations
;
71 static const struct inode_operations btrfs_file_inode_operations
;
72 static const struct address_space_operations btrfs_aops
;
73 static const struct address_space_operations btrfs_symlink_aops
;
74 static const struct file_operations btrfs_dir_file_operations
;
75 static struct extent_io_ops btrfs_extent_io_ops
;
77 static struct kmem_cache
*btrfs_inode_cachep
;
78 static struct kmem_cache
*btrfs_delalloc_work_cachep
;
79 struct kmem_cache
*btrfs_trans_handle_cachep
;
80 struct kmem_cache
*btrfs_transaction_cachep
;
81 struct kmem_cache
*btrfs_path_cachep
;
82 struct kmem_cache
*btrfs_free_space_cachep
;
85 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
86 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
87 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
88 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
89 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
90 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
91 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
92 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
95 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
96 static int btrfs_truncate(struct inode
*inode
);
97 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
98 static noinline
int cow_file_range(struct inode
*inode
,
99 struct page
*locked_page
,
100 u64 start
, u64 end
, int *page_started
,
101 unsigned long *nr_written
, int unlock
);
102 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
103 u64 len
, u64 orig_start
,
104 u64 block_start
, u64 block_len
,
105 u64 orig_block_len
, u64 ram_bytes
,
108 static int btrfs_dirty_inode(struct inode
*inode
);
110 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
111 struct inode
*inode
, struct inode
*dir
,
112 const struct qstr
*qstr
)
116 err
= btrfs_init_acl(trans
, inode
, dir
);
118 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
123 * this does all the hard work for inserting an inline extent into
124 * the btree. The caller should have done a btrfs_drop_extents so that
125 * no overlapping inline items exist in the btree
127 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
128 struct btrfs_root
*root
, struct inode
*inode
,
129 u64 start
, size_t size
, size_t compressed_size
,
131 struct page
**compressed_pages
)
133 struct btrfs_key key
;
134 struct btrfs_path
*path
;
135 struct extent_buffer
*leaf
;
136 struct page
*page
= NULL
;
139 struct btrfs_file_extent_item
*ei
;
142 size_t cur_size
= size
;
144 unsigned long offset
;
146 if (compressed_size
&& compressed_pages
)
147 cur_size
= compressed_size
;
149 path
= btrfs_alloc_path();
153 path
->leave_spinning
= 1;
155 key
.objectid
= btrfs_ino(inode
);
157 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
158 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
160 inode_add_bytes(inode
, size
);
161 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
167 leaf
= path
->nodes
[0];
168 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
169 struct btrfs_file_extent_item
);
170 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
171 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
172 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
173 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
174 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
175 ptr
= btrfs_file_extent_inline_start(ei
);
177 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
180 while (compressed_size
> 0) {
181 cpage
= compressed_pages
[i
];
182 cur_size
= min_t(unsigned long, compressed_size
,
185 kaddr
= kmap_atomic(cpage
);
186 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
187 kunmap_atomic(kaddr
);
191 compressed_size
-= cur_size
;
193 btrfs_set_file_extent_compression(leaf
, ei
,
196 page
= find_get_page(inode
->i_mapping
,
197 start
>> PAGE_CACHE_SHIFT
);
198 btrfs_set_file_extent_compression(leaf
, ei
, 0);
199 kaddr
= kmap_atomic(page
);
200 offset
= start
& (PAGE_CACHE_SIZE
- 1);
201 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
202 kunmap_atomic(kaddr
);
203 page_cache_release(page
);
205 btrfs_mark_buffer_dirty(leaf
);
206 btrfs_free_path(path
);
209 * we're an inline extent, so nobody can
210 * extend the file past i_size without locking
211 * a page we already have locked.
213 * We must do any isize and inode updates
214 * before we unlock the pages. Otherwise we
215 * could end up racing with unlink.
217 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
218 ret
= btrfs_update_inode(trans
, root
, inode
);
222 btrfs_free_path(path
);
228 * conditionally insert an inline extent into the file. This
229 * does the checks required to make sure the data is small enough
230 * to fit as an inline extent.
232 static noinline
int cow_file_range_inline(struct btrfs_root
*root
,
233 struct inode
*inode
, u64 start
,
234 u64 end
, size_t compressed_size
,
236 struct page
**compressed_pages
)
238 struct btrfs_trans_handle
*trans
;
239 u64 isize
= i_size_read(inode
);
240 u64 actual_end
= min(end
+ 1, isize
);
241 u64 inline_len
= actual_end
- start
;
242 u64 aligned_end
= ALIGN(end
, root
->sectorsize
);
243 u64 data_len
= inline_len
;
247 data_len
= compressed_size
;
250 actual_end
>= PAGE_CACHE_SIZE
||
251 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
253 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
255 data_len
> root
->fs_info
->max_inline
) {
259 trans
= btrfs_join_transaction(root
);
261 return PTR_ERR(trans
);
262 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
264 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, aligned_end
, 1);
266 btrfs_abort_transaction(trans
, root
, ret
);
270 if (isize
> actual_end
)
271 inline_len
= min_t(u64
, isize
, actual_end
);
272 ret
= insert_inline_extent(trans
, root
, inode
, start
,
273 inline_len
, compressed_size
,
274 compress_type
, compressed_pages
);
275 if (ret
&& ret
!= -ENOSPC
) {
276 btrfs_abort_transaction(trans
, root
, ret
);
278 } else if (ret
== -ENOSPC
) {
283 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
284 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
285 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
287 btrfs_end_transaction(trans
, root
);
291 struct async_extent
{
296 unsigned long nr_pages
;
298 struct list_head list
;
303 struct btrfs_root
*root
;
304 struct page
*locked_page
;
307 struct list_head extents
;
308 struct btrfs_work work
;
311 static noinline
int add_async_extent(struct async_cow
*cow
,
312 u64 start
, u64 ram_size
,
315 unsigned long nr_pages
,
318 struct async_extent
*async_extent
;
320 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
321 BUG_ON(!async_extent
); /* -ENOMEM */
322 async_extent
->start
= start
;
323 async_extent
->ram_size
= ram_size
;
324 async_extent
->compressed_size
= compressed_size
;
325 async_extent
->pages
= pages
;
326 async_extent
->nr_pages
= nr_pages
;
327 async_extent
->compress_type
= compress_type
;
328 list_add_tail(&async_extent
->list
, &cow
->extents
);
333 * we create compressed extents in two phases. The first
334 * phase compresses a range of pages that have already been
335 * locked (both pages and state bits are locked).
337 * This is done inside an ordered work queue, and the compression
338 * is spread across many cpus. The actual IO submission is step
339 * two, and the ordered work queue takes care of making sure that
340 * happens in the same order things were put onto the queue by
341 * writepages and friends.
343 * If this code finds it can't get good compression, it puts an
344 * entry onto the work queue to write the uncompressed bytes. This
345 * makes sure that both compressed inodes and uncompressed inodes
346 * are written in the same order that the flusher thread sent them
349 static noinline
int compress_file_range(struct inode
*inode
,
350 struct page
*locked_page
,
352 struct async_cow
*async_cow
,
355 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
357 u64 blocksize
= root
->sectorsize
;
359 u64 isize
= i_size_read(inode
);
361 struct page
**pages
= NULL
;
362 unsigned long nr_pages
;
363 unsigned long nr_pages_ret
= 0;
364 unsigned long total_compressed
= 0;
365 unsigned long total_in
= 0;
366 unsigned long max_compressed
= 128 * 1024;
367 unsigned long max_uncompressed
= 128 * 1024;
370 int compress_type
= root
->fs_info
->compress_type
;
373 /* if this is a small write inside eof, kick off a defrag */
374 if ((end
- start
+ 1) < 16 * 1024 &&
375 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
376 btrfs_add_inode_defrag(NULL
, inode
);
378 actual_end
= min_t(u64
, isize
, end
+ 1);
381 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
382 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
385 * we don't want to send crud past the end of i_size through
386 * compression, that's just a waste of CPU time. So, if the
387 * end of the file is before the start of our current
388 * requested range of bytes, we bail out to the uncompressed
389 * cleanup code that can deal with all of this.
391 * It isn't really the fastest way to fix things, but this is a
392 * very uncommon corner.
394 if (actual_end
<= start
)
395 goto cleanup_and_bail_uncompressed
;
397 total_compressed
= actual_end
- start
;
399 /* we want to make sure that amount of ram required to uncompress
400 * an extent is reasonable, so we limit the total size in ram
401 * of a compressed extent to 128k. This is a crucial number
402 * because it also controls how easily we can spread reads across
403 * cpus for decompression.
405 * We also want to make sure the amount of IO required to do
406 * a random read is reasonably small, so we limit the size of
407 * a compressed extent to 128k.
409 total_compressed
= min(total_compressed
, max_uncompressed
);
410 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
411 num_bytes
= max(blocksize
, num_bytes
);
416 * we do compression for mount -o compress and when the
417 * inode has not been flagged as nocompress. This flag can
418 * change at any time if we discover bad compression ratios.
420 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
421 (btrfs_test_opt(root
, COMPRESS
) ||
422 (BTRFS_I(inode
)->force_compress
) ||
423 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
425 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
427 /* just bail out to the uncompressed code */
431 if (BTRFS_I(inode
)->force_compress
)
432 compress_type
= BTRFS_I(inode
)->force_compress
;
435 * we need to call clear_page_dirty_for_io on each
436 * page in the range. Otherwise applications with the file
437 * mmap'd can wander in and change the page contents while
438 * we are compressing them.
440 * If the compression fails for any reason, we set the pages
441 * dirty again later on.
443 extent_range_clear_dirty_for_io(inode
, start
, end
);
445 ret
= btrfs_compress_pages(compress_type
,
446 inode
->i_mapping
, start
,
447 total_compressed
, pages
,
448 nr_pages
, &nr_pages_ret
,
454 unsigned long offset
= total_compressed
&
455 (PAGE_CACHE_SIZE
- 1);
456 struct page
*page
= pages
[nr_pages_ret
- 1];
459 /* zero the tail end of the last page, we might be
460 * sending it down to disk
463 kaddr
= kmap_atomic(page
);
464 memset(kaddr
+ offset
, 0,
465 PAGE_CACHE_SIZE
- offset
);
466 kunmap_atomic(kaddr
);
473 /* lets try to make an inline extent */
474 if (ret
|| total_in
< (actual_end
- start
)) {
475 /* we didn't compress the entire range, try
476 * to make an uncompressed inline extent.
478 ret
= cow_file_range_inline(root
, inode
, start
, end
,
481 /* try making a compressed inline extent */
482 ret
= cow_file_range_inline(root
, inode
, start
, end
,
484 compress_type
, pages
);
487 unsigned long clear_flags
= EXTENT_DELALLOC
|
489 clear_flags
|= (ret
< 0) ? EXTENT_DO_ACCOUNTING
: 0;
492 * inline extent creation worked or returned error,
493 * we don't need to create any more async work items.
494 * Unlock and free up our temp pages.
496 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
,
497 clear_flags
, PAGE_UNLOCK
|
507 * we aren't doing an inline extent round the compressed size
508 * up to a block size boundary so the allocator does sane
511 total_compressed
= ALIGN(total_compressed
, blocksize
);
514 * one last check to make sure the compression is really a
515 * win, compare the page count read with the blocks on disk
517 total_in
= ALIGN(total_in
, PAGE_CACHE_SIZE
);
518 if (total_compressed
>= total_in
) {
521 num_bytes
= total_in
;
524 if (!will_compress
&& pages
) {
526 * the compression code ran but failed to make things smaller,
527 * free any pages it allocated and our page pointer array
529 for (i
= 0; i
< nr_pages_ret
; i
++) {
530 WARN_ON(pages
[i
]->mapping
);
531 page_cache_release(pages
[i
]);
535 total_compressed
= 0;
538 /* flag the file so we don't compress in the future */
539 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
540 !(BTRFS_I(inode
)->force_compress
)) {
541 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
547 /* the async work queues will take care of doing actual
548 * allocation on disk for these compressed pages,
549 * and will submit them to the elevator.
551 add_async_extent(async_cow
, start
, num_bytes
,
552 total_compressed
, pages
, nr_pages_ret
,
555 if (start
+ num_bytes
< end
) {
562 cleanup_and_bail_uncompressed
:
564 * No compression, but we still need to write the pages in
565 * the file we've been given so far. redirty the locked
566 * page if it corresponds to our extent and set things up
567 * for the async work queue to run cow_file_range to do
568 * the normal delalloc dance
570 if (page_offset(locked_page
) >= start
&&
571 page_offset(locked_page
) <= end
) {
572 __set_page_dirty_nobuffers(locked_page
);
573 /* unlocked later on in the async handlers */
576 extent_range_redirty_for_io(inode
, start
, end
);
577 add_async_extent(async_cow
, start
, end
- start
+ 1,
578 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
586 for (i
= 0; i
< nr_pages_ret
; i
++) {
587 WARN_ON(pages
[i
]->mapping
);
588 page_cache_release(pages
[i
]);
596 * phase two of compressed writeback. This is the ordered portion
597 * of the code, which only gets called in the order the work was
598 * queued. We walk all the async extents created by compress_file_range
599 * and send them down to the disk.
601 static noinline
int submit_compressed_extents(struct inode
*inode
,
602 struct async_cow
*async_cow
)
604 struct async_extent
*async_extent
;
606 struct btrfs_key ins
;
607 struct extent_map
*em
;
608 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
609 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
610 struct extent_io_tree
*io_tree
;
613 if (list_empty(&async_cow
->extents
))
617 while (!list_empty(&async_cow
->extents
)) {
618 async_extent
= list_entry(async_cow
->extents
.next
,
619 struct async_extent
, list
);
620 list_del(&async_extent
->list
);
622 io_tree
= &BTRFS_I(inode
)->io_tree
;
625 /* did the compression code fall back to uncompressed IO? */
626 if (!async_extent
->pages
) {
627 int page_started
= 0;
628 unsigned long nr_written
= 0;
630 lock_extent(io_tree
, async_extent
->start
,
631 async_extent
->start
+
632 async_extent
->ram_size
- 1);
634 /* allocate blocks */
635 ret
= cow_file_range(inode
, async_cow
->locked_page
,
637 async_extent
->start
+
638 async_extent
->ram_size
- 1,
639 &page_started
, &nr_written
, 0);
644 * if page_started, cow_file_range inserted an
645 * inline extent and took care of all the unlocking
646 * and IO for us. Otherwise, we need to submit
647 * all those pages down to the drive.
649 if (!page_started
&& !ret
)
650 extent_write_locked_range(io_tree
,
651 inode
, async_extent
->start
,
652 async_extent
->start
+
653 async_extent
->ram_size
- 1,
657 unlock_page(async_cow
->locked_page
);
663 lock_extent(io_tree
, async_extent
->start
,
664 async_extent
->start
+ async_extent
->ram_size
- 1);
666 ret
= btrfs_reserve_extent(root
,
667 async_extent
->compressed_size
,
668 async_extent
->compressed_size
,
669 0, alloc_hint
, &ins
, 1);
673 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
674 WARN_ON(async_extent
->pages
[i
]->mapping
);
675 page_cache_release(async_extent
->pages
[i
]);
677 kfree(async_extent
->pages
);
678 async_extent
->nr_pages
= 0;
679 async_extent
->pages
= NULL
;
681 if (ret
== -ENOSPC
) {
682 unlock_extent(io_tree
, async_extent
->start
,
683 async_extent
->start
+
684 async_extent
->ram_size
- 1);
691 * here we're doing allocation and writeback of the
694 btrfs_drop_extent_cache(inode
, async_extent
->start
,
695 async_extent
->start
+
696 async_extent
->ram_size
- 1, 0);
698 em
= alloc_extent_map();
701 goto out_free_reserve
;
703 em
->start
= async_extent
->start
;
704 em
->len
= async_extent
->ram_size
;
705 em
->orig_start
= em
->start
;
706 em
->mod_start
= em
->start
;
707 em
->mod_len
= em
->len
;
709 em
->block_start
= ins
.objectid
;
710 em
->block_len
= ins
.offset
;
711 em
->orig_block_len
= ins
.offset
;
712 em
->ram_bytes
= async_extent
->ram_size
;
713 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
714 em
->compress_type
= async_extent
->compress_type
;
715 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
716 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
720 write_lock(&em_tree
->lock
);
721 ret
= add_extent_mapping(em_tree
, em
, 1);
722 write_unlock(&em_tree
->lock
);
723 if (ret
!= -EEXIST
) {
727 btrfs_drop_extent_cache(inode
, async_extent
->start
,
728 async_extent
->start
+
729 async_extent
->ram_size
- 1, 0);
733 goto out_free_reserve
;
735 ret
= btrfs_add_ordered_extent_compress(inode
,
738 async_extent
->ram_size
,
740 BTRFS_ORDERED_COMPRESSED
,
741 async_extent
->compress_type
);
743 goto out_free_reserve
;
746 * clear dirty, set writeback and unlock the pages.
748 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
749 async_extent
->start
+
750 async_extent
->ram_size
- 1,
751 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
,
752 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
754 ret
= btrfs_submit_compressed_write(inode
,
756 async_extent
->ram_size
,
758 ins
.offset
, async_extent
->pages
,
759 async_extent
->nr_pages
);
760 alloc_hint
= ins
.objectid
+ ins
.offset
;
770 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
772 extent_clear_unlock_delalloc(inode
, async_extent
->start
,
773 async_extent
->start
+
774 async_extent
->ram_size
- 1,
775 NULL
, EXTENT_LOCKED
| EXTENT_DELALLOC
|
776 EXTENT_DEFRAG
| EXTENT_DO_ACCOUNTING
,
777 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
778 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
);
783 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
786 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
787 struct extent_map
*em
;
790 read_lock(&em_tree
->lock
);
791 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
794 * if block start isn't an actual block number then find the
795 * first block in this inode and use that as a hint. If that
796 * block is also bogus then just don't worry about it.
798 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
800 em
= search_extent_mapping(em_tree
, 0, 0);
801 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
802 alloc_hint
= em
->block_start
;
806 alloc_hint
= em
->block_start
;
810 read_unlock(&em_tree
->lock
);
816 * when extent_io.c finds a delayed allocation range in the file,
817 * the call backs end up in this code. The basic idea is to
818 * allocate extents on disk for the range, and create ordered data structs
819 * in ram to track those extents.
821 * locked_page is the page that writepage had locked already. We use
822 * it to make sure we don't do extra locks or unlocks.
824 * *page_started is set to one if we unlock locked_page and do everything
825 * required to start IO on it. It may be clean and already done with
828 static noinline
int cow_file_range(struct inode
*inode
,
829 struct page
*locked_page
,
830 u64 start
, u64 end
, int *page_started
,
831 unsigned long *nr_written
,
834 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
837 unsigned long ram_size
;
840 u64 blocksize
= root
->sectorsize
;
841 struct btrfs_key ins
;
842 struct extent_map
*em
;
843 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
846 if (btrfs_is_free_space_inode(inode
)) {
851 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
852 num_bytes
= max(blocksize
, num_bytes
);
853 disk_num_bytes
= num_bytes
;
855 /* if this is a small write inside eof, kick off defrag */
856 if (num_bytes
< 64 * 1024 &&
857 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
858 btrfs_add_inode_defrag(NULL
, inode
);
861 /* lets try to make an inline extent */
862 ret
= cow_file_range_inline(root
, inode
, start
, end
, 0, 0,
865 extent_clear_unlock_delalloc(inode
, start
, end
, NULL
,
866 EXTENT_LOCKED
| EXTENT_DELALLOC
|
867 EXTENT_DEFRAG
, PAGE_UNLOCK
|
868 PAGE_CLEAR_DIRTY
| PAGE_SET_WRITEBACK
|
871 *nr_written
= *nr_written
+
872 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
875 } else if (ret
< 0) {
880 BUG_ON(disk_num_bytes
>
881 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
883 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
884 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
886 while (disk_num_bytes
> 0) {
889 cur_alloc_size
= disk_num_bytes
;
890 ret
= btrfs_reserve_extent(root
, cur_alloc_size
,
891 root
->sectorsize
, 0, alloc_hint
,
896 em
= alloc_extent_map();
902 em
->orig_start
= em
->start
;
903 ram_size
= ins
.offset
;
904 em
->len
= ins
.offset
;
905 em
->mod_start
= em
->start
;
906 em
->mod_len
= em
->len
;
908 em
->block_start
= ins
.objectid
;
909 em
->block_len
= ins
.offset
;
910 em
->orig_block_len
= ins
.offset
;
911 em
->ram_bytes
= ram_size
;
912 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
913 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
917 write_lock(&em_tree
->lock
);
918 ret
= add_extent_mapping(em_tree
, em
, 1);
919 write_unlock(&em_tree
->lock
);
920 if (ret
!= -EEXIST
) {
924 btrfs_drop_extent_cache(inode
, start
,
925 start
+ ram_size
- 1, 0);
930 cur_alloc_size
= ins
.offset
;
931 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
932 ram_size
, cur_alloc_size
, 0);
936 if (root
->root_key
.objectid
==
937 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
938 ret
= btrfs_reloc_clone_csums(inode
, start
,
944 if (disk_num_bytes
< cur_alloc_size
)
947 /* we're not doing compressed IO, don't unlock the first
948 * page (which the caller expects to stay locked), don't
949 * clear any dirty bits and don't set any writeback bits
951 * Do set the Private2 bit so we know this page was properly
952 * setup for writepage
954 op
= unlock
? PAGE_UNLOCK
: 0;
955 op
|= PAGE_SET_PRIVATE2
;
957 extent_clear_unlock_delalloc(inode
, start
,
958 start
+ ram_size
- 1, locked_page
,
959 EXTENT_LOCKED
| EXTENT_DELALLOC
,
961 disk_num_bytes
-= cur_alloc_size
;
962 num_bytes
-= cur_alloc_size
;
963 alloc_hint
= ins
.objectid
+ ins
.offset
;
964 start
+= cur_alloc_size
;
970 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
972 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
973 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
974 EXTENT_DELALLOC
| EXTENT_DEFRAG
,
975 PAGE_UNLOCK
| PAGE_CLEAR_DIRTY
|
976 PAGE_SET_WRITEBACK
| PAGE_END_WRITEBACK
);
981 * work queue call back to started compression on a file and pages
983 static noinline
void async_cow_start(struct btrfs_work
*work
)
985 struct async_cow
*async_cow
;
987 async_cow
= container_of(work
, struct async_cow
, work
);
989 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
990 async_cow
->start
, async_cow
->end
, async_cow
,
992 if (num_added
== 0) {
993 btrfs_add_delayed_iput(async_cow
->inode
);
994 async_cow
->inode
= NULL
;
999 * work queue call back to submit previously compressed pages
1001 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1003 struct async_cow
*async_cow
;
1004 struct btrfs_root
*root
;
1005 unsigned long nr_pages
;
1007 async_cow
= container_of(work
, struct async_cow
, work
);
1009 root
= async_cow
->root
;
1010 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1013 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1015 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1016 wake_up(&root
->fs_info
->async_submit_wait
);
1018 if (async_cow
->inode
)
1019 submit_compressed_extents(async_cow
->inode
, async_cow
);
1022 static noinline
void async_cow_free(struct btrfs_work
*work
)
1024 struct async_cow
*async_cow
;
1025 async_cow
= container_of(work
, struct async_cow
, work
);
1026 if (async_cow
->inode
)
1027 btrfs_add_delayed_iput(async_cow
->inode
);
1031 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1032 u64 start
, u64 end
, int *page_started
,
1033 unsigned long *nr_written
)
1035 struct async_cow
*async_cow
;
1036 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1037 unsigned long nr_pages
;
1039 int limit
= 10 * 1024 * 1024;
1041 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1042 1, 0, NULL
, GFP_NOFS
);
1043 while (start
< end
) {
1044 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1045 BUG_ON(!async_cow
); /* -ENOMEM */
1046 async_cow
->inode
= igrab(inode
);
1047 async_cow
->root
= root
;
1048 async_cow
->locked_page
= locked_page
;
1049 async_cow
->start
= start
;
1051 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1054 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1056 async_cow
->end
= cur_end
;
1057 INIT_LIST_HEAD(&async_cow
->extents
);
1059 async_cow
->work
.func
= async_cow_start
;
1060 async_cow
->work
.ordered_func
= async_cow_submit
;
1061 async_cow
->work
.ordered_free
= async_cow_free
;
1062 async_cow
->work
.flags
= 0;
1064 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1066 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1068 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
1071 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1072 wait_event(root
->fs_info
->async_submit_wait
,
1073 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1077 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1078 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1079 wait_event(root
->fs_info
->async_submit_wait
,
1080 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1084 *nr_written
+= nr_pages
;
1085 start
= cur_end
+ 1;
1091 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1092 u64 bytenr
, u64 num_bytes
)
1095 struct btrfs_ordered_sum
*sums
;
1098 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1099 bytenr
+ num_bytes
- 1, &list
, 0);
1100 if (ret
== 0 && list_empty(&list
))
1103 while (!list_empty(&list
)) {
1104 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1105 list_del(&sums
->list
);
1112 * when nowcow writeback call back. This checks for snapshots or COW copies
1113 * of the extents that exist in the file, and COWs the file as required.
1115 * If no cow copies or snapshots exist, we write directly to the existing
1118 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1119 struct page
*locked_page
,
1120 u64 start
, u64 end
, int *page_started
, int force
,
1121 unsigned long *nr_written
)
1123 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1124 struct btrfs_trans_handle
*trans
;
1125 struct extent_buffer
*leaf
;
1126 struct btrfs_path
*path
;
1127 struct btrfs_file_extent_item
*fi
;
1128 struct btrfs_key found_key
;
1143 u64 ino
= btrfs_ino(inode
);
1145 path
= btrfs_alloc_path();
1147 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1148 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1149 EXTENT_DO_ACCOUNTING
|
1150 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1152 PAGE_SET_WRITEBACK
|
1153 PAGE_END_WRITEBACK
);
1157 nolock
= btrfs_is_free_space_inode(inode
);
1160 trans
= btrfs_join_transaction_nolock(root
);
1162 trans
= btrfs_join_transaction(root
);
1164 if (IS_ERR(trans
)) {
1165 extent_clear_unlock_delalloc(inode
, start
, end
, locked_page
,
1166 EXTENT_LOCKED
| EXTENT_DELALLOC
|
1167 EXTENT_DO_ACCOUNTING
|
1168 EXTENT_DEFRAG
, PAGE_UNLOCK
|
1170 PAGE_SET_WRITEBACK
|
1171 PAGE_END_WRITEBACK
);
1172 btrfs_free_path(path
);
1173 return PTR_ERR(trans
);
1176 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1178 cow_start
= (u64
)-1;
1181 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1185 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1186 leaf
= path
->nodes
[0];
1187 btrfs_item_key_to_cpu(leaf
, &found_key
,
1188 path
->slots
[0] - 1);
1189 if (found_key
.objectid
== ino
&&
1190 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1195 leaf
= path
->nodes
[0];
1196 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1197 ret
= btrfs_next_leaf(root
, path
);
1202 leaf
= path
->nodes
[0];
1208 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1210 if (found_key
.objectid
> ino
||
1211 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1212 found_key
.offset
> end
)
1215 if (found_key
.offset
> cur_offset
) {
1216 extent_end
= found_key
.offset
;
1221 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1222 struct btrfs_file_extent_item
);
1223 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1225 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1226 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1227 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1228 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1229 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1230 extent_end
= found_key
.offset
+
1231 btrfs_file_extent_num_bytes(leaf
, fi
);
1233 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1234 if (extent_end
<= start
) {
1238 if (disk_bytenr
== 0)
1240 if (btrfs_file_extent_compression(leaf
, fi
) ||
1241 btrfs_file_extent_encryption(leaf
, fi
) ||
1242 btrfs_file_extent_other_encoding(leaf
, fi
))
1244 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1246 if (btrfs_extent_readonly(root
, disk_bytenr
))
1248 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1250 extent_offset
, disk_bytenr
))
1252 disk_bytenr
+= extent_offset
;
1253 disk_bytenr
+= cur_offset
- found_key
.offset
;
1254 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1256 * force cow if csum exists in the range.
1257 * this ensure that csum for a given extent are
1258 * either valid or do not exist.
1260 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1263 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1264 extent_end
= found_key
.offset
+
1265 btrfs_file_extent_inline_len(leaf
, fi
);
1266 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1271 if (extent_end
<= start
) {
1276 if (cow_start
== (u64
)-1)
1277 cow_start
= cur_offset
;
1278 cur_offset
= extent_end
;
1279 if (cur_offset
> end
)
1285 btrfs_release_path(path
);
1286 if (cow_start
!= (u64
)-1) {
1287 ret
= cow_file_range(inode
, locked_page
,
1288 cow_start
, found_key
.offset
- 1,
1289 page_started
, nr_written
, 1);
1292 cow_start
= (u64
)-1;
1295 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1296 struct extent_map
*em
;
1297 struct extent_map_tree
*em_tree
;
1298 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1299 em
= alloc_extent_map();
1300 BUG_ON(!em
); /* -ENOMEM */
1301 em
->start
= cur_offset
;
1302 em
->orig_start
= found_key
.offset
- extent_offset
;
1303 em
->len
= num_bytes
;
1304 em
->block_len
= num_bytes
;
1305 em
->block_start
= disk_bytenr
;
1306 em
->orig_block_len
= disk_num_bytes
;
1307 em
->ram_bytes
= ram_bytes
;
1308 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1309 em
->mod_start
= em
->start
;
1310 em
->mod_len
= em
->len
;
1311 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1312 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1313 em
->generation
= -1;
1315 write_lock(&em_tree
->lock
);
1316 ret
= add_extent_mapping(em_tree
, em
, 1);
1317 write_unlock(&em_tree
->lock
);
1318 if (ret
!= -EEXIST
) {
1319 free_extent_map(em
);
1322 btrfs_drop_extent_cache(inode
, em
->start
,
1323 em
->start
+ em
->len
- 1, 0);
1325 type
= BTRFS_ORDERED_PREALLOC
;
1327 type
= BTRFS_ORDERED_NOCOW
;
1330 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1331 num_bytes
, num_bytes
, type
);
1332 BUG_ON(ret
); /* -ENOMEM */
1334 if (root
->root_key
.objectid
==
1335 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1336 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1342 extent_clear_unlock_delalloc(inode
, cur_offset
,
1343 cur_offset
+ num_bytes
- 1,
1344 locked_page
, EXTENT_LOCKED
|
1345 EXTENT_DELALLOC
, PAGE_UNLOCK
|
1347 cur_offset
= extent_end
;
1348 if (cur_offset
> end
)
1351 btrfs_release_path(path
);
1353 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1354 cow_start
= cur_offset
;
1358 if (cow_start
!= (u64
)-1) {
1359 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1360 page_started
, nr_written
, 1);
1366 err
= btrfs_end_transaction(trans
, root
);
1370 if (ret
&& cur_offset
< end
)
1371 extent_clear_unlock_delalloc(inode
, cur_offset
, end
,
1372 locked_page
, EXTENT_LOCKED
|
1373 EXTENT_DELALLOC
| EXTENT_DEFRAG
|
1374 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1376 PAGE_SET_WRITEBACK
|
1377 PAGE_END_WRITEBACK
);
1378 btrfs_free_path(path
);
1383 * extent_io.c call back to do delayed allocation processing
1385 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1386 u64 start
, u64 end
, int *page_started
,
1387 unsigned long *nr_written
)
1390 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1392 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1393 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1394 page_started
, 1, nr_written
);
1395 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1396 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1397 page_started
, 0, nr_written
);
1398 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1399 !(BTRFS_I(inode
)->force_compress
) &&
1400 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1401 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1402 page_started
, nr_written
, 1);
1404 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1405 &BTRFS_I(inode
)->runtime_flags
);
1406 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1407 page_started
, nr_written
);
1412 static void btrfs_split_extent_hook(struct inode
*inode
,
1413 struct extent_state
*orig
, u64 split
)
1415 /* not delalloc, ignore it */
1416 if (!(orig
->state
& EXTENT_DELALLOC
))
1419 spin_lock(&BTRFS_I(inode
)->lock
);
1420 BTRFS_I(inode
)->outstanding_extents
++;
1421 spin_unlock(&BTRFS_I(inode
)->lock
);
1425 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1426 * extents so we can keep track of new extents that are just merged onto old
1427 * extents, such as when we are doing sequential writes, so we can properly
1428 * account for the metadata space we'll need.
1430 static void btrfs_merge_extent_hook(struct inode
*inode
,
1431 struct extent_state
*new,
1432 struct extent_state
*other
)
1434 /* not delalloc, ignore it */
1435 if (!(other
->state
& EXTENT_DELALLOC
))
1438 spin_lock(&BTRFS_I(inode
)->lock
);
1439 BTRFS_I(inode
)->outstanding_extents
--;
1440 spin_unlock(&BTRFS_I(inode
)->lock
);
1443 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1444 struct inode
*inode
)
1446 spin_lock(&root
->delalloc_lock
);
1447 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1448 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1449 &root
->delalloc_inodes
);
1450 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1451 &BTRFS_I(inode
)->runtime_flags
);
1452 root
->nr_delalloc_inodes
++;
1453 if (root
->nr_delalloc_inodes
== 1) {
1454 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1455 BUG_ON(!list_empty(&root
->delalloc_root
));
1456 list_add_tail(&root
->delalloc_root
,
1457 &root
->fs_info
->delalloc_roots
);
1458 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1461 spin_unlock(&root
->delalloc_lock
);
1464 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1465 struct inode
*inode
)
1467 spin_lock(&root
->delalloc_lock
);
1468 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1469 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1470 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1471 &BTRFS_I(inode
)->runtime_flags
);
1472 root
->nr_delalloc_inodes
--;
1473 if (!root
->nr_delalloc_inodes
) {
1474 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1475 BUG_ON(list_empty(&root
->delalloc_root
));
1476 list_del_init(&root
->delalloc_root
);
1477 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1480 spin_unlock(&root
->delalloc_lock
);
1484 * extent_io.c set_bit_hook, used to track delayed allocation
1485 * bytes in this file, and to maintain the list of inodes that
1486 * have pending delalloc work to be done.
1488 static void btrfs_set_bit_hook(struct inode
*inode
,
1489 struct extent_state
*state
, unsigned long *bits
)
1493 * set_bit and clear bit hooks normally require _irqsave/restore
1494 * but in this case, we are only testing for the DELALLOC
1495 * bit, which is only set or cleared with irqs on
1497 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1498 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1499 u64 len
= state
->end
+ 1 - state
->start
;
1500 bool do_list
= !btrfs_is_free_space_inode(inode
);
1502 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1503 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1505 spin_lock(&BTRFS_I(inode
)->lock
);
1506 BTRFS_I(inode
)->outstanding_extents
++;
1507 spin_unlock(&BTRFS_I(inode
)->lock
);
1510 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1511 root
->fs_info
->delalloc_batch
);
1512 spin_lock(&BTRFS_I(inode
)->lock
);
1513 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1514 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1515 &BTRFS_I(inode
)->runtime_flags
))
1516 btrfs_add_delalloc_inodes(root
, inode
);
1517 spin_unlock(&BTRFS_I(inode
)->lock
);
1522 * extent_io.c clear_bit_hook, see set_bit_hook for why
1524 static void btrfs_clear_bit_hook(struct inode
*inode
,
1525 struct extent_state
*state
,
1526 unsigned long *bits
)
1529 * set_bit and clear bit hooks normally require _irqsave/restore
1530 * but in this case, we are only testing for the DELALLOC
1531 * bit, which is only set or cleared with irqs on
1533 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1534 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1535 u64 len
= state
->end
+ 1 - state
->start
;
1536 bool do_list
= !btrfs_is_free_space_inode(inode
);
1538 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1539 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1540 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1541 spin_lock(&BTRFS_I(inode
)->lock
);
1542 BTRFS_I(inode
)->outstanding_extents
--;
1543 spin_unlock(&BTRFS_I(inode
)->lock
);
1547 * We don't reserve metadata space for space cache inodes so we
1548 * don't need to call dellalloc_release_metadata if there is an
1551 if (*bits
& EXTENT_DO_ACCOUNTING
&&
1552 root
!= root
->fs_info
->tree_root
)
1553 btrfs_delalloc_release_metadata(inode
, len
);
1555 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1556 && do_list
&& !(state
->state
& EXTENT_NORESERVE
))
1557 btrfs_free_reserved_data_space(inode
, len
);
1559 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1560 root
->fs_info
->delalloc_batch
);
1561 spin_lock(&BTRFS_I(inode
)->lock
);
1562 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1563 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1564 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1565 &BTRFS_I(inode
)->runtime_flags
))
1566 btrfs_del_delalloc_inode(root
, inode
);
1567 spin_unlock(&BTRFS_I(inode
)->lock
);
1572 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1573 * we don't create bios that span stripes or chunks
1575 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1576 size_t size
, struct bio
*bio
,
1577 unsigned long bio_flags
)
1579 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1580 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1585 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1588 length
= bio
->bi_size
;
1589 map_length
= length
;
1590 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1591 &map_length
, NULL
, 0);
1592 /* Will always return 0 with map_multi == NULL */
1594 if (map_length
< length
+ size
)
1600 * in order to insert checksums into the metadata in large chunks,
1601 * we wait until bio submission time. All the pages in the bio are
1602 * checksummed and sums are attached onto the ordered extent record.
1604 * At IO completion time the cums attached on the ordered extent record
1605 * are inserted into the btree
1607 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1608 struct bio
*bio
, int mirror_num
,
1609 unsigned long bio_flags
,
1612 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1615 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1616 BUG_ON(ret
); /* -ENOMEM */
1621 * in order to insert checksums into the metadata in large chunks,
1622 * we wait until bio submission time. All the pages in the bio are
1623 * checksummed and sums are attached onto the ordered extent record.
1625 * At IO completion time the cums attached on the ordered extent record
1626 * are inserted into the btree
1628 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1629 int mirror_num
, unsigned long bio_flags
,
1632 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1635 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1637 bio_endio(bio
, ret
);
1642 * extent_io.c submission hook. This does the right thing for csum calculation
1643 * on write, or reading the csums from the tree before a read
1645 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1646 int mirror_num
, unsigned long bio_flags
,
1649 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1653 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1655 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1657 if (btrfs_is_free_space_inode(inode
))
1660 if (!(rw
& REQ_WRITE
)) {
1661 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1665 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1666 ret
= btrfs_submit_compressed_read(inode
, bio
,
1670 } else if (!skip_sum
) {
1671 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1676 } else if (async
&& !skip_sum
) {
1677 /* csum items have already been cloned */
1678 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1680 /* we're doing a write, do the async checksumming */
1681 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1682 inode
, rw
, bio
, mirror_num
,
1683 bio_flags
, bio_offset
,
1684 __btrfs_submit_bio_start
,
1685 __btrfs_submit_bio_done
);
1687 } else if (!skip_sum
) {
1688 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1694 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1698 bio_endio(bio
, ret
);
1703 * given a list of ordered sums record them in the inode. This happens
1704 * at IO completion time based on sums calculated at bio submission time.
1706 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1707 struct inode
*inode
, u64 file_offset
,
1708 struct list_head
*list
)
1710 struct btrfs_ordered_sum
*sum
;
1712 list_for_each_entry(sum
, list
, list
) {
1713 trans
->adding_csums
= 1;
1714 btrfs_csum_file_blocks(trans
,
1715 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1716 trans
->adding_csums
= 0;
1721 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1722 struct extent_state
**cached_state
)
1724 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1725 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1726 cached_state
, GFP_NOFS
);
1729 /* see btrfs_writepage_start_hook for details on why this is required */
1730 struct btrfs_writepage_fixup
{
1732 struct btrfs_work work
;
1735 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1737 struct btrfs_writepage_fixup
*fixup
;
1738 struct btrfs_ordered_extent
*ordered
;
1739 struct extent_state
*cached_state
= NULL
;
1741 struct inode
*inode
;
1746 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1750 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1751 ClearPageChecked(page
);
1755 inode
= page
->mapping
->host
;
1756 page_start
= page_offset(page
);
1757 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1759 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1762 /* already ordered? We're done */
1763 if (PagePrivate2(page
))
1766 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1768 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1769 page_end
, &cached_state
, GFP_NOFS
);
1771 btrfs_start_ordered_extent(inode
, ordered
, 1);
1772 btrfs_put_ordered_extent(ordered
);
1776 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1778 mapping_set_error(page
->mapping
, ret
);
1779 end_extent_writepage(page
, ret
, page_start
, page_end
);
1780 ClearPageChecked(page
);
1784 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1785 ClearPageChecked(page
);
1786 set_page_dirty(page
);
1788 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1789 &cached_state
, GFP_NOFS
);
1792 page_cache_release(page
);
1797 * There are a few paths in the higher layers of the kernel that directly
1798 * set the page dirty bit without asking the filesystem if it is a
1799 * good idea. This causes problems because we want to make sure COW
1800 * properly happens and the data=ordered rules are followed.
1802 * In our case any range that doesn't have the ORDERED bit set
1803 * hasn't been properly setup for IO. We kick off an async process
1804 * to fix it up. The async helper will wait for ordered extents, set
1805 * the delalloc bit and make it safe to write the page.
1807 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1809 struct inode
*inode
= page
->mapping
->host
;
1810 struct btrfs_writepage_fixup
*fixup
;
1811 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1813 /* this page is properly in the ordered list */
1814 if (TestClearPagePrivate2(page
))
1817 if (PageChecked(page
))
1820 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1824 SetPageChecked(page
);
1825 page_cache_get(page
);
1826 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1828 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1832 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1833 struct inode
*inode
, u64 file_pos
,
1834 u64 disk_bytenr
, u64 disk_num_bytes
,
1835 u64 num_bytes
, u64 ram_bytes
,
1836 u8 compression
, u8 encryption
,
1837 u16 other_encoding
, int extent_type
)
1839 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1840 struct btrfs_file_extent_item
*fi
;
1841 struct btrfs_path
*path
;
1842 struct extent_buffer
*leaf
;
1843 struct btrfs_key ins
;
1846 path
= btrfs_alloc_path();
1850 path
->leave_spinning
= 1;
1853 * we may be replacing one extent in the tree with another.
1854 * The new extent is pinned in the extent map, and we don't want
1855 * to drop it from the cache until it is completely in the btree.
1857 * So, tell btrfs_drop_extents to leave this extent in the cache.
1858 * the caller is expected to unpin it and allow it to be merged
1861 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1862 file_pos
+ num_bytes
, 0);
1866 ins
.objectid
= btrfs_ino(inode
);
1867 ins
.offset
= file_pos
;
1868 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1869 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1872 leaf
= path
->nodes
[0];
1873 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1874 struct btrfs_file_extent_item
);
1875 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1876 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1877 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1878 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1879 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1880 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1881 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1882 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1883 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1884 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1886 btrfs_mark_buffer_dirty(leaf
);
1887 btrfs_release_path(path
);
1889 inode_add_bytes(inode
, num_bytes
);
1891 ins
.objectid
= disk_bytenr
;
1892 ins
.offset
= disk_num_bytes
;
1893 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1894 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1895 root
->root_key
.objectid
,
1896 btrfs_ino(inode
), file_pos
, &ins
);
1898 btrfs_free_path(path
);
1903 /* snapshot-aware defrag */
1904 struct sa_defrag_extent_backref
{
1905 struct rb_node node
;
1906 struct old_sa_defrag_extent
*old
;
1915 struct old_sa_defrag_extent
{
1916 struct list_head list
;
1917 struct new_sa_defrag_extent
*new;
1926 struct new_sa_defrag_extent
{
1927 struct rb_root root
;
1928 struct list_head head
;
1929 struct btrfs_path
*path
;
1930 struct inode
*inode
;
1938 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
1939 struct sa_defrag_extent_backref
*b2
)
1941 if (b1
->root_id
< b2
->root_id
)
1943 else if (b1
->root_id
> b2
->root_id
)
1946 if (b1
->inum
< b2
->inum
)
1948 else if (b1
->inum
> b2
->inum
)
1951 if (b1
->file_pos
< b2
->file_pos
)
1953 else if (b1
->file_pos
> b2
->file_pos
)
1957 * [------------------------------] ===> (a range of space)
1958 * |<--->| |<---->| =============> (fs/file tree A)
1959 * |<---------------------------->| ===> (fs/file tree B)
1961 * A range of space can refer to two file extents in one tree while
1962 * refer to only one file extent in another tree.
1964 * So we may process a disk offset more than one time(two extents in A)
1965 * and locate at the same extent(one extent in B), then insert two same
1966 * backrefs(both refer to the extent in B).
1971 static void backref_insert(struct rb_root
*root
,
1972 struct sa_defrag_extent_backref
*backref
)
1974 struct rb_node
**p
= &root
->rb_node
;
1975 struct rb_node
*parent
= NULL
;
1976 struct sa_defrag_extent_backref
*entry
;
1981 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
1983 ret
= backref_comp(backref
, entry
);
1987 p
= &(*p
)->rb_right
;
1990 rb_link_node(&backref
->node
, parent
, p
);
1991 rb_insert_color(&backref
->node
, root
);
1995 * Note the backref might has changed, and in this case we just return 0.
1997 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2000 struct btrfs_file_extent_item
*extent
;
2001 struct btrfs_fs_info
*fs_info
;
2002 struct old_sa_defrag_extent
*old
= ctx
;
2003 struct new_sa_defrag_extent
*new = old
->new;
2004 struct btrfs_path
*path
= new->path
;
2005 struct btrfs_key key
;
2006 struct btrfs_root
*root
;
2007 struct sa_defrag_extent_backref
*backref
;
2008 struct extent_buffer
*leaf
;
2009 struct inode
*inode
= new->inode
;
2015 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2016 inum
== btrfs_ino(inode
))
2019 key
.objectid
= root_id
;
2020 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2021 key
.offset
= (u64
)-1;
2023 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2024 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2026 if (PTR_ERR(root
) == -ENOENT
)
2029 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2030 inum
, offset
, root_id
);
2031 return PTR_ERR(root
);
2034 key
.objectid
= inum
;
2035 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2036 if (offset
> (u64
)-1 << 32)
2039 key
.offset
= offset
;
2041 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2042 if (WARN_ON(ret
< 0))
2049 leaf
= path
->nodes
[0];
2050 slot
= path
->slots
[0];
2052 if (slot
>= btrfs_header_nritems(leaf
)) {
2053 ret
= btrfs_next_leaf(root
, path
);
2056 } else if (ret
> 0) {
2065 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2067 if (key
.objectid
> inum
)
2070 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2073 extent
= btrfs_item_ptr(leaf
, slot
,
2074 struct btrfs_file_extent_item
);
2076 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2080 * 'offset' refers to the exact key.offset,
2081 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2082 * (key.offset - extent_offset).
2084 if (key
.offset
!= offset
)
2087 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2088 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2090 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2091 old
->len
|| extent_offset
+ num_bytes
<=
2092 old
->extent_offset
+ old
->offset
)
2097 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2103 backref
->root_id
= root_id
;
2104 backref
->inum
= inum
;
2105 backref
->file_pos
= offset
;
2106 backref
->num_bytes
= num_bytes
;
2107 backref
->extent_offset
= extent_offset
;
2108 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2110 backref_insert(&new->root
, backref
);
2113 btrfs_release_path(path
);
2118 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2119 struct new_sa_defrag_extent
*new)
2121 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2122 struct old_sa_defrag_extent
*old
, *tmp
;
2127 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2128 ret
= iterate_inodes_from_logical(old
->bytenr
+
2129 old
->extent_offset
, fs_info
,
2130 path
, record_one_backref
,
2132 if (ret
< 0 && ret
!= -ENOENT
)
2135 /* no backref to be processed for this extent */
2137 list_del(&old
->list
);
2142 if (list_empty(&new->head
))
2148 static int relink_is_mergable(struct extent_buffer
*leaf
,
2149 struct btrfs_file_extent_item
*fi
,
2150 struct new_sa_defrag_extent
*new)
2152 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != new->bytenr
)
2155 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2158 if (btrfs_file_extent_compression(leaf
, fi
) != new->compress_type
)
2161 if (btrfs_file_extent_encryption(leaf
, fi
) ||
2162 btrfs_file_extent_other_encoding(leaf
, fi
))
2169 * Note the backref might has changed, and in this case we just return 0.
2171 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2172 struct sa_defrag_extent_backref
*prev
,
2173 struct sa_defrag_extent_backref
*backref
)
2175 struct btrfs_file_extent_item
*extent
;
2176 struct btrfs_file_extent_item
*item
;
2177 struct btrfs_ordered_extent
*ordered
;
2178 struct btrfs_trans_handle
*trans
;
2179 struct btrfs_fs_info
*fs_info
;
2180 struct btrfs_root
*root
;
2181 struct btrfs_key key
;
2182 struct extent_buffer
*leaf
;
2183 struct old_sa_defrag_extent
*old
= backref
->old
;
2184 struct new_sa_defrag_extent
*new = old
->new;
2185 struct inode
*src_inode
= new->inode
;
2186 struct inode
*inode
;
2187 struct extent_state
*cached
= NULL
;
2196 if (prev
&& prev
->root_id
== backref
->root_id
&&
2197 prev
->inum
== backref
->inum
&&
2198 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2201 /* step 1: get root */
2202 key
.objectid
= backref
->root_id
;
2203 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2204 key
.offset
= (u64
)-1;
2206 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2207 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2209 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2211 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2212 if (PTR_ERR(root
) == -ENOENT
)
2214 return PTR_ERR(root
);
2217 /* step 2: get inode */
2218 key
.objectid
= backref
->inum
;
2219 key
.type
= BTRFS_INODE_ITEM_KEY
;
2222 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2223 if (IS_ERR(inode
)) {
2224 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2228 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2230 /* step 3: relink backref */
2231 lock_start
= backref
->file_pos
;
2232 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2233 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2236 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2238 btrfs_put_ordered_extent(ordered
);
2242 trans
= btrfs_join_transaction(root
);
2243 if (IS_ERR(trans
)) {
2244 ret
= PTR_ERR(trans
);
2248 key
.objectid
= backref
->inum
;
2249 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2250 key
.offset
= backref
->file_pos
;
2252 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2255 } else if (ret
> 0) {
2260 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2261 struct btrfs_file_extent_item
);
2263 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2264 backref
->generation
)
2267 btrfs_release_path(path
);
2269 start
= backref
->file_pos
;
2270 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2271 start
+= old
->extent_offset
+ old
->offset
-
2272 backref
->extent_offset
;
2274 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2275 old
->extent_offset
+ old
->offset
+ old
->len
);
2276 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2278 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2283 key
.objectid
= btrfs_ino(inode
);
2284 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2287 path
->leave_spinning
= 1;
2289 struct btrfs_file_extent_item
*fi
;
2291 struct btrfs_key found_key
;
2293 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
2298 leaf
= path
->nodes
[0];
2299 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2301 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2302 struct btrfs_file_extent_item
);
2303 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2305 if (extent_len
+ found_key
.offset
== start
&&
2306 relink_is_mergable(leaf
, fi
, new)) {
2307 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2309 btrfs_mark_buffer_dirty(leaf
);
2310 inode_add_bytes(inode
, len
);
2316 btrfs_release_path(path
);
2321 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2324 btrfs_abort_transaction(trans
, root
, ret
);
2328 leaf
= path
->nodes
[0];
2329 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2330 struct btrfs_file_extent_item
);
2331 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2332 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2333 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2334 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2335 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2336 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2337 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2338 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2339 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2340 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2342 btrfs_mark_buffer_dirty(leaf
);
2343 inode_add_bytes(inode
, len
);
2344 btrfs_release_path(path
);
2346 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2348 backref
->root_id
, backref
->inum
,
2349 new->file_pos
, 0); /* start - extent_offset */
2351 btrfs_abort_transaction(trans
, root
, ret
);
2357 btrfs_release_path(path
);
2358 path
->leave_spinning
= 0;
2359 btrfs_end_transaction(trans
, root
);
2361 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2367 static void free_sa_defrag_extent(struct new_sa_defrag_extent
*new)
2369 struct old_sa_defrag_extent
*old
, *tmp
;
2374 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2375 list_del(&old
->list
);
2381 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2383 struct btrfs_path
*path
;
2384 struct sa_defrag_extent_backref
*backref
;
2385 struct sa_defrag_extent_backref
*prev
= NULL
;
2386 struct inode
*inode
;
2387 struct btrfs_root
*root
;
2388 struct rb_node
*node
;
2392 root
= BTRFS_I(inode
)->root
;
2394 path
= btrfs_alloc_path();
2398 if (!record_extent_backrefs(path
, new)) {
2399 btrfs_free_path(path
);
2402 btrfs_release_path(path
);
2405 node
= rb_first(&new->root
);
2408 rb_erase(node
, &new->root
);
2410 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2412 ret
= relink_extent_backref(path
, prev
, backref
);
2425 btrfs_free_path(path
);
2427 free_sa_defrag_extent(new);
2429 atomic_dec(&root
->fs_info
->defrag_running
);
2430 wake_up(&root
->fs_info
->transaction_wait
);
2433 static struct new_sa_defrag_extent
*
2434 record_old_file_extents(struct inode
*inode
,
2435 struct btrfs_ordered_extent
*ordered
)
2437 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2438 struct btrfs_path
*path
;
2439 struct btrfs_key key
;
2440 struct old_sa_defrag_extent
*old
;
2441 struct new_sa_defrag_extent
*new;
2444 new = kmalloc(sizeof(*new), GFP_NOFS
);
2449 new->file_pos
= ordered
->file_offset
;
2450 new->len
= ordered
->len
;
2451 new->bytenr
= ordered
->start
;
2452 new->disk_len
= ordered
->disk_len
;
2453 new->compress_type
= ordered
->compress_type
;
2454 new->root
= RB_ROOT
;
2455 INIT_LIST_HEAD(&new->head
);
2457 path
= btrfs_alloc_path();
2461 key
.objectid
= btrfs_ino(inode
);
2462 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2463 key
.offset
= new->file_pos
;
2465 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2468 if (ret
> 0 && path
->slots
[0] > 0)
2471 /* find out all the old extents for the file range */
2473 struct btrfs_file_extent_item
*extent
;
2474 struct extent_buffer
*l
;
2483 slot
= path
->slots
[0];
2485 if (slot
>= btrfs_header_nritems(l
)) {
2486 ret
= btrfs_next_leaf(root
, path
);
2494 btrfs_item_key_to_cpu(l
, &key
, slot
);
2496 if (key
.objectid
!= btrfs_ino(inode
))
2498 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2500 if (key
.offset
>= new->file_pos
+ new->len
)
2503 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2505 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2506 if (key
.offset
+ num_bytes
< new->file_pos
)
2509 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2513 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2515 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2519 offset
= max(new->file_pos
, key
.offset
);
2520 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2522 old
->bytenr
= disk_bytenr
;
2523 old
->extent_offset
= extent_offset
;
2524 old
->offset
= offset
- key
.offset
;
2525 old
->len
= end
- offset
;
2528 list_add_tail(&old
->list
, &new->head
);
2534 btrfs_free_path(path
);
2535 atomic_inc(&root
->fs_info
->defrag_running
);
2540 btrfs_free_path(path
);
2542 free_sa_defrag_extent(new);
2546 /* as ordered data IO finishes, this gets called so we can finish
2547 * an ordered extent if the range of bytes in the file it covers are
2550 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2552 struct inode
*inode
= ordered_extent
->inode
;
2553 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2554 struct btrfs_trans_handle
*trans
= NULL
;
2555 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2556 struct extent_state
*cached_state
= NULL
;
2557 struct new_sa_defrag_extent
*new = NULL
;
2558 int compress_type
= 0;
2560 u64 logical_len
= ordered_extent
->len
;
2562 bool truncated
= false;
2564 nolock
= btrfs_is_free_space_inode(inode
);
2566 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2571 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2573 logical_len
= ordered_extent
->truncated_len
;
2574 /* Truncated the entire extent, don't bother adding */
2579 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2580 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2581 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2583 trans
= btrfs_join_transaction_nolock(root
);
2585 trans
= btrfs_join_transaction(root
);
2586 if (IS_ERR(trans
)) {
2587 ret
= PTR_ERR(trans
);
2591 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2592 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2593 if (ret
) /* -ENOMEM or corruption */
2594 btrfs_abort_transaction(trans
, root
, ret
);
2598 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2599 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2602 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2603 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2604 EXTENT_DEFRAG
, 1, cached_state
);
2606 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2607 if (last_snapshot
>= BTRFS_I(inode
)->generation
)
2608 /* the inode is shared */
2609 new = record_old_file_extents(inode
, ordered_extent
);
2611 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2612 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2613 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2617 trans
= btrfs_join_transaction_nolock(root
);
2619 trans
= btrfs_join_transaction(root
);
2620 if (IS_ERR(trans
)) {
2621 ret
= PTR_ERR(trans
);
2625 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2627 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2628 compress_type
= ordered_extent
->compress_type
;
2629 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2630 BUG_ON(compress_type
);
2631 ret
= btrfs_mark_extent_written(trans
, inode
,
2632 ordered_extent
->file_offset
,
2633 ordered_extent
->file_offset
+
2636 BUG_ON(root
== root
->fs_info
->tree_root
);
2637 ret
= insert_reserved_file_extent(trans
, inode
,
2638 ordered_extent
->file_offset
,
2639 ordered_extent
->start
,
2640 ordered_extent
->disk_len
,
2641 logical_len
, logical_len
,
2642 compress_type
, 0, 0,
2643 BTRFS_FILE_EXTENT_REG
);
2645 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2646 ordered_extent
->file_offset
, ordered_extent
->len
,
2649 btrfs_abort_transaction(trans
, root
, ret
);
2653 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2654 &ordered_extent
->list
);
2656 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2657 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2658 if (ret
) { /* -ENOMEM or corruption */
2659 btrfs_abort_transaction(trans
, root
, ret
);
2664 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2665 ordered_extent
->file_offset
+
2666 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2668 if (root
!= root
->fs_info
->tree_root
)
2669 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2671 btrfs_end_transaction(trans
, root
);
2673 if (ret
|| truncated
) {
2677 start
= ordered_extent
->file_offset
+ logical_len
;
2679 start
= ordered_extent
->file_offset
;
2680 end
= ordered_extent
->file_offset
+ ordered_extent
->len
- 1;
2681 clear_extent_uptodate(io_tree
, start
, end
, NULL
, GFP_NOFS
);
2683 /* Drop the cache for the part of the extent we didn't write. */
2684 btrfs_drop_extent_cache(inode
, start
, end
, 0);
2687 * If the ordered extent had an IOERR or something else went
2688 * wrong we need to return the space for this ordered extent
2689 * back to the allocator. We only free the extent in the
2690 * truncated case if we didn't write out the extent at all.
2692 if ((ret
|| !logical_len
) &&
2693 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2694 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2695 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2696 ordered_extent
->disk_len
);
2701 * This needs to be done to make sure anybody waiting knows we are done
2702 * updating everything for this ordered extent.
2704 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2706 /* for snapshot-aware defrag */
2709 free_sa_defrag_extent(new);
2710 atomic_dec(&root
->fs_info
->defrag_running
);
2712 relink_file_extents(new);
2717 btrfs_put_ordered_extent(ordered_extent
);
2718 /* once for the tree */
2719 btrfs_put_ordered_extent(ordered_extent
);
2724 static void finish_ordered_fn(struct btrfs_work
*work
)
2726 struct btrfs_ordered_extent
*ordered_extent
;
2727 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2728 btrfs_finish_ordered_io(ordered_extent
);
2731 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2732 struct extent_state
*state
, int uptodate
)
2734 struct inode
*inode
= page
->mapping
->host
;
2735 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2736 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2737 struct btrfs_workers
*workers
;
2739 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2741 ClearPagePrivate2(page
);
2742 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2743 end
- start
+ 1, uptodate
))
2746 ordered_extent
->work
.func
= finish_ordered_fn
;
2747 ordered_extent
->work
.flags
= 0;
2749 if (btrfs_is_free_space_inode(inode
))
2750 workers
= &root
->fs_info
->endio_freespace_worker
;
2752 workers
= &root
->fs_info
->endio_write_workers
;
2753 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2759 * when reads are done, we need to check csums to verify the data is correct
2760 * if there's a match, we allow the bio to finish. If not, the code in
2761 * extent_io.c will try to find good copies for us.
2763 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
2764 u64 phy_offset
, struct page
*page
,
2765 u64 start
, u64 end
, int mirror
)
2767 size_t offset
= start
- page_offset(page
);
2768 struct inode
*inode
= page
->mapping
->host
;
2769 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2771 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2774 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
2775 DEFAULT_RATELIMIT_BURST
);
2777 if (PageChecked(page
)) {
2778 ClearPageChecked(page
);
2782 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2785 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2786 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2787 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2792 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
2793 csum_expected
= *(((u32
*)io_bio
->csum
) + phy_offset
);
2795 kaddr
= kmap_atomic(page
);
2796 csum
= btrfs_csum_data(kaddr
+ offset
, csum
, end
- start
+ 1);
2797 btrfs_csum_final(csum
, (char *)&csum
);
2798 if (csum
!= csum_expected
)
2801 kunmap_atomic(kaddr
);
2806 if (__ratelimit(&_rs
))
2807 btrfs_info(root
->fs_info
, "csum failed ino %llu off %llu csum %u expected csum %u",
2808 btrfs_ino(page
->mapping
->host
), start
, csum
, csum_expected
);
2809 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2810 flush_dcache_page(page
);
2811 kunmap_atomic(kaddr
);
2812 if (csum_expected
== 0)
2817 struct delayed_iput
{
2818 struct list_head list
;
2819 struct inode
*inode
;
2822 /* JDM: If this is fs-wide, why can't we add a pointer to
2823 * btrfs_inode instead and avoid the allocation? */
2824 void btrfs_add_delayed_iput(struct inode
*inode
)
2826 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2827 struct delayed_iput
*delayed
;
2829 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2832 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2833 delayed
->inode
= inode
;
2835 spin_lock(&fs_info
->delayed_iput_lock
);
2836 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2837 spin_unlock(&fs_info
->delayed_iput_lock
);
2840 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2843 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2844 struct delayed_iput
*delayed
;
2847 spin_lock(&fs_info
->delayed_iput_lock
);
2848 empty
= list_empty(&fs_info
->delayed_iputs
);
2849 spin_unlock(&fs_info
->delayed_iput_lock
);
2853 spin_lock(&fs_info
->delayed_iput_lock
);
2854 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2855 spin_unlock(&fs_info
->delayed_iput_lock
);
2857 while (!list_empty(&list
)) {
2858 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2859 list_del(&delayed
->list
);
2860 iput(delayed
->inode
);
2866 * This is called in transaction commit time. If there are no orphan
2867 * files in the subvolume, it removes orphan item and frees block_rsv
2870 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2871 struct btrfs_root
*root
)
2873 struct btrfs_block_rsv
*block_rsv
;
2876 if (atomic_read(&root
->orphan_inodes
) ||
2877 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2880 spin_lock(&root
->orphan_lock
);
2881 if (atomic_read(&root
->orphan_inodes
)) {
2882 spin_unlock(&root
->orphan_lock
);
2886 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2887 spin_unlock(&root
->orphan_lock
);
2891 block_rsv
= root
->orphan_block_rsv
;
2892 root
->orphan_block_rsv
= NULL
;
2893 spin_unlock(&root
->orphan_lock
);
2895 if (root
->orphan_item_inserted
&&
2896 btrfs_root_refs(&root
->root_item
) > 0) {
2897 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2898 root
->root_key
.objectid
);
2900 btrfs_abort_transaction(trans
, root
, ret
);
2902 root
->orphan_item_inserted
= 0;
2906 WARN_ON(block_rsv
->size
> 0);
2907 btrfs_free_block_rsv(root
, block_rsv
);
2912 * This creates an orphan entry for the given inode in case something goes
2913 * wrong in the middle of an unlink/truncate.
2915 * NOTE: caller of this function should reserve 5 units of metadata for
2918 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2920 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2921 struct btrfs_block_rsv
*block_rsv
= NULL
;
2926 if (!root
->orphan_block_rsv
) {
2927 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2932 spin_lock(&root
->orphan_lock
);
2933 if (!root
->orphan_block_rsv
) {
2934 root
->orphan_block_rsv
= block_rsv
;
2935 } else if (block_rsv
) {
2936 btrfs_free_block_rsv(root
, block_rsv
);
2940 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2941 &BTRFS_I(inode
)->runtime_flags
)) {
2944 * For proper ENOSPC handling, we should do orphan
2945 * cleanup when mounting. But this introduces backward
2946 * compatibility issue.
2948 if (!xchg(&root
->orphan_item_inserted
, 1))
2954 atomic_inc(&root
->orphan_inodes
);
2957 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2958 &BTRFS_I(inode
)->runtime_flags
))
2960 spin_unlock(&root
->orphan_lock
);
2962 /* grab metadata reservation from transaction handle */
2964 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2965 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
2968 /* insert an orphan item to track this unlinked/truncated file */
2970 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2972 atomic_dec(&root
->orphan_inodes
);
2974 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2975 &BTRFS_I(inode
)->runtime_flags
);
2976 btrfs_orphan_release_metadata(inode
);
2978 if (ret
!= -EEXIST
) {
2979 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2980 &BTRFS_I(inode
)->runtime_flags
);
2981 btrfs_abort_transaction(trans
, root
, ret
);
2988 /* insert an orphan item to track subvolume contains orphan files */
2990 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2991 root
->root_key
.objectid
);
2992 if (ret
&& ret
!= -EEXIST
) {
2993 btrfs_abort_transaction(trans
, root
, ret
);
3001 * We have done the truncate/delete so we can go ahead and remove the orphan
3002 * item for this particular inode.
3004 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3005 struct inode
*inode
)
3007 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3008 int delete_item
= 0;
3009 int release_rsv
= 0;
3012 spin_lock(&root
->orphan_lock
);
3013 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3014 &BTRFS_I(inode
)->runtime_flags
))
3017 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3018 &BTRFS_I(inode
)->runtime_flags
))
3020 spin_unlock(&root
->orphan_lock
);
3023 atomic_dec(&root
->orphan_inodes
);
3025 ret
= btrfs_del_orphan_item(trans
, root
,
3030 btrfs_orphan_release_metadata(inode
);
3036 * this cleans up any orphans that may be left on the list from the last use
3039 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3041 struct btrfs_path
*path
;
3042 struct extent_buffer
*leaf
;
3043 struct btrfs_key key
, found_key
;
3044 struct btrfs_trans_handle
*trans
;
3045 struct inode
*inode
;
3046 u64 last_objectid
= 0;
3047 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3049 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3052 path
= btrfs_alloc_path();
3059 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3060 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
3061 key
.offset
= (u64
)-1;
3064 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3069 * if ret == 0 means we found what we were searching for, which
3070 * is weird, but possible, so only screw with path if we didn't
3071 * find the key and see if we have stuff that matches
3075 if (path
->slots
[0] == 0)
3080 /* pull out the item */
3081 leaf
= path
->nodes
[0];
3082 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3084 /* make sure the item matches what we want */
3085 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3087 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
3090 /* release the path since we're done with it */
3091 btrfs_release_path(path
);
3094 * this is where we are basically btrfs_lookup, without the
3095 * crossing root thing. we store the inode number in the
3096 * offset of the orphan item.
3099 if (found_key
.offset
== last_objectid
) {
3100 btrfs_err(root
->fs_info
,
3101 "Error removing orphan entry, stopping orphan cleanup");
3106 last_objectid
= found_key
.offset
;
3108 found_key
.objectid
= found_key
.offset
;
3109 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3110 found_key
.offset
= 0;
3111 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3112 ret
= PTR_ERR_OR_ZERO(inode
);
3113 if (ret
&& ret
!= -ESTALE
)
3116 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3117 struct btrfs_root
*dead_root
;
3118 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3119 int is_dead_root
= 0;
3122 * this is an orphan in the tree root. Currently these
3123 * could come from 2 sources:
3124 * a) a snapshot deletion in progress
3125 * b) a free space cache inode
3126 * We need to distinguish those two, as the snapshot
3127 * orphan must not get deleted.
3128 * find_dead_roots already ran before us, so if this
3129 * is a snapshot deletion, we should find the root
3130 * in the dead_roots list
3132 spin_lock(&fs_info
->trans_lock
);
3133 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3135 if (dead_root
->root_key
.objectid
==
3136 found_key
.objectid
) {
3141 spin_unlock(&fs_info
->trans_lock
);
3143 /* prevent this orphan from being found again */
3144 key
.offset
= found_key
.objectid
- 1;
3149 * Inode is already gone but the orphan item is still there,
3150 * kill the orphan item.
3152 if (ret
== -ESTALE
) {
3153 trans
= btrfs_start_transaction(root
, 1);
3154 if (IS_ERR(trans
)) {
3155 ret
= PTR_ERR(trans
);
3158 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3159 found_key
.objectid
);
3160 ret
= btrfs_del_orphan_item(trans
, root
,
3161 found_key
.objectid
);
3162 btrfs_end_transaction(trans
, root
);
3169 * add this inode to the orphan list so btrfs_orphan_del does
3170 * the proper thing when we hit it
3172 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3173 &BTRFS_I(inode
)->runtime_flags
);
3174 atomic_inc(&root
->orphan_inodes
);
3176 /* if we have links, this was a truncate, lets do that */
3177 if (inode
->i_nlink
) {
3178 if (WARN_ON(!S_ISREG(inode
->i_mode
))) {
3184 /* 1 for the orphan item deletion. */
3185 trans
= btrfs_start_transaction(root
, 1);
3186 if (IS_ERR(trans
)) {
3188 ret
= PTR_ERR(trans
);
3191 ret
= btrfs_orphan_add(trans
, inode
);
3192 btrfs_end_transaction(trans
, root
);
3198 ret
= btrfs_truncate(inode
);
3200 btrfs_orphan_del(NULL
, inode
);
3205 /* this will do delete_inode and everything for us */
3210 /* release the path since we're done with it */
3211 btrfs_release_path(path
);
3213 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3215 if (root
->orphan_block_rsv
)
3216 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3219 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
3220 trans
= btrfs_join_transaction(root
);
3222 btrfs_end_transaction(trans
, root
);
3226 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3228 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3232 btrfs_crit(root
->fs_info
,
3233 "could not do orphan cleanup %d", ret
);
3234 btrfs_free_path(path
);
3239 * very simple check to peek ahead in the leaf looking for xattrs. If we
3240 * don't find any xattrs, we know there can't be any acls.
3242 * slot is the slot the inode is in, objectid is the objectid of the inode
3244 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3245 int slot
, u64 objectid
)
3247 u32 nritems
= btrfs_header_nritems(leaf
);
3248 struct btrfs_key found_key
;
3249 static u64 xattr_access
= 0;
3250 static u64 xattr_default
= 0;
3253 if (!xattr_access
) {
3254 xattr_access
= btrfs_name_hash(POSIX_ACL_XATTR_ACCESS
,
3255 strlen(POSIX_ACL_XATTR_ACCESS
));
3256 xattr_default
= btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT
,
3257 strlen(POSIX_ACL_XATTR_DEFAULT
));
3261 while (slot
< nritems
) {
3262 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3264 /* we found a different objectid, there must not be acls */
3265 if (found_key
.objectid
!= objectid
)
3268 /* we found an xattr, assume we've got an acl */
3269 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3270 if (found_key
.offset
== xattr_access
||
3271 found_key
.offset
== xattr_default
)
3276 * we found a key greater than an xattr key, there can't
3277 * be any acls later on
3279 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3286 * it goes inode, inode backrefs, xattrs, extents,
3287 * so if there are a ton of hard links to an inode there can
3288 * be a lot of backrefs. Don't waste time searching too hard,
3289 * this is just an optimization
3294 /* we hit the end of the leaf before we found an xattr or
3295 * something larger than an xattr. We have to assume the inode
3302 * read an inode from the btree into the in-memory inode
3304 static void btrfs_read_locked_inode(struct inode
*inode
)
3306 struct btrfs_path
*path
;
3307 struct extent_buffer
*leaf
;
3308 struct btrfs_inode_item
*inode_item
;
3309 struct btrfs_timespec
*tspec
;
3310 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3311 struct btrfs_key location
;
3315 bool filled
= false;
3317 ret
= btrfs_fill_inode(inode
, &rdev
);
3321 path
= btrfs_alloc_path();
3325 path
->leave_spinning
= 1;
3326 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3328 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3332 leaf
= path
->nodes
[0];
3337 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3338 struct btrfs_inode_item
);
3339 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3340 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3341 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3342 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3343 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3345 tspec
= btrfs_inode_atime(inode_item
);
3346 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3347 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3349 tspec
= btrfs_inode_mtime(inode_item
);
3350 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3351 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3353 tspec
= btrfs_inode_ctime(inode_item
);
3354 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3355 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3357 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3358 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3359 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3362 * If we were modified in the current generation and evicted from memory
3363 * and then re-read we need to do a full sync since we don't have any
3364 * idea about which extents were modified before we were evicted from
3367 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3368 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3369 &BTRFS_I(inode
)->runtime_flags
);
3371 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3372 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3374 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3376 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3377 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3380 * try to precache a NULL acl entry for files that don't have
3381 * any xattrs or acls
3383 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3386 cache_no_acl(inode
);
3388 btrfs_free_path(path
);
3390 switch (inode
->i_mode
& S_IFMT
) {
3392 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3393 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3394 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3395 inode
->i_fop
= &btrfs_file_operations
;
3396 inode
->i_op
= &btrfs_file_inode_operations
;
3399 inode
->i_fop
= &btrfs_dir_file_operations
;
3400 if (root
== root
->fs_info
->tree_root
)
3401 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3403 inode
->i_op
= &btrfs_dir_inode_operations
;
3406 inode
->i_op
= &btrfs_symlink_inode_operations
;
3407 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3408 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3411 inode
->i_op
= &btrfs_special_inode_operations
;
3412 init_special_inode(inode
, inode
->i_mode
, rdev
);
3416 btrfs_update_iflags(inode
);
3420 btrfs_free_path(path
);
3421 make_bad_inode(inode
);
3425 * given a leaf and an inode, copy the inode fields into the leaf
3427 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3428 struct extent_buffer
*leaf
,
3429 struct btrfs_inode_item
*item
,
3430 struct inode
*inode
)
3432 struct btrfs_map_token token
;
3434 btrfs_init_map_token(&token
);
3436 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3437 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3438 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3440 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3441 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3443 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
3444 inode
->i_atime
.tv_sec
, &token
);
3445 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
3446 inode
->i_atime
.tv_nsec
, &token
);
3448 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
3449 inode
->i_mtime
.tv_sec
, &token
);
3450 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3451 inode
->i_mtime
.tv_nsec
, &token
);
3453 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3454 inode
->i_ctime
.tv_sec
, &token
);
3455 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3456 inode
->i_ctime
.tv_nsec
, &token
);
3458 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3460 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3462 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3463 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3464 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3465 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3466 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3470 * copy everything in the in-memory inode into the btree.
3472 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3473 struct btrfs_root
*root
, struct inode
*inode
)
3475 struct btrfs_inode_item
*inode_item
;
3476 struct btrfs_path
*path
;
3477 struct extent_buffer
*leaf
;
3480 path
= btrfs_alloc_path();
3484 path
->leave_spinning
= 1;
3485 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3493 btrfs_unlock_up_safe(path
, 1);
3494 leaf
= path
->nodes
[0];
3495 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3496 struct btrfs_inode_item
);
3498 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3499 btrfs_mark_buffer_dirty(leaf
);
3500 btrfs_set_inode_last_trans(trans
, inode
);
3503 btrfs_free_path(path
);
3508 * copy everything in the in-memory inode into the btree.
3510 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3511 struct btrfs_root
*root
, struct inode
*inode
)
3516 * If the inode is a free space inode, we can deadlock during commit
3517 * if we put it into the delayed code.
3519 * The data relocation inode should also be directly updated
3522 if (!btrfs_is_free_space_inode(inode
)
3523 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
3524 btrfs_update_root_times(trans
, root
);
3526 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3528 btrfs_set_inode_last_trans(trans
, inode
);
3532 return btrfs_update_inode_item(trans
, root
, inode
);
3535 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3536 struct btrfs_root
*root
,
3537 struct inode
*inode
)
3541 ret
= btrfs_update_inode(trans
, root
, inode
);
3543 return btrfs_update_inode_item(trans
, root
, inode
);
3548 * unlink helper that gets used here in inode.c and in the tree logging
3549 * recovery code. It remove a link in a directory with a given name, and
3550 * also drops the back refs in the inode to the directory
3552 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3553 struct btrfs_root
*root
,
3554 struct inode
*dir
, struct inode
*inode
,
3555 const char *name
, int name_len
)
3557 struct btrfs_path
*path
;
3559 struct extent_buffer
*leaf
;
3560 struct btrfs_dir_item
*di
;
3561 struct btrfs_key key
;
3563 u64 ino
= btrfs_ino(inode
);
3564 u64 dir_ino
= btrfs_ino(dir
);
3566 path
= btrfs_alloc_path();
3572 path
->leave_spinning
= 1;
3573 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3574 name
, name_len
, -1);
3583 leaf
= path
->nodes
[0];
3584 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3585 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3588 btrfs_release_path(path
);
3590 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3593 btrfs_info(root
->fs_info
,
3594 "failed to delete reference to %.*s, inode %llu parent %llu",
3595 name_len
, name
, ino
, dir_ino
);
3596 btrfs_abort_transaction(trans
, root
, ret
);
3600 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3602 btrfs_abort_transaction(trans
, root
, ret
);
3606 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
3608 if (ret
!= 0 && ret
!= -ENOENT
) {
3609 btrfs_abort_transaction(trans
, root
, ret
);
3613 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
3618 btrfs_abort_transaction(trans
, root
, ret
);
3620 btrfs_free_path(path
);
3624 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3625 inode_inc_iversion(inode
);
3626 inode_inc_iversion(dir
);
3627 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3628 ret
= btrfs_update_inode(trans
, root
, dir
);
3633 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3634 struct btrfs_root
*root
,
3635 struct inode
*dir
, struct inode
*inode
,
3636 const char *name
, int name_len
)
3639 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3642 ret
= btrfs_update_inode(trans
, root
, inode
);
3648 * helper to start transaction for unlink and rmdir.
3650 * unlink and rmdir are special in btrfs, they do not always free space, so
3651 * if we cannot make our reservations the normal way try and see if there is
3652 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3653 * allow the unlink to occur.
3655 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
3657 struct btrfs_trans_handle
*trans
;
3658 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3662 * 1 for the possible orphan item
3663 * 1 for the dir item
3664 * 1 for the dir index
3665 * 1 for the inode ref
3668 trans
= btrfs_start_transaction(root
, 5);
3669 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3672 if (PTR_ERR(trans
) == -ENOSPC
) {
3673 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 5);
3675 trans
= btrfs_start_transaction(root
, 0);
3678 ret
= btrfs_cond_migrate_bytes(root
->fs_info
,
3679 &root
->fs_info
->trans_block_rsv
,
3682 btrfs_end_transaction(trans
, root
);
3683 return ERR_PTR(ret
);
3685 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3686 trans
->bytes_reserved
= num_bytes
;
3691 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3693 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3694 struct btrfs_trans_handle
*trans
;
3695 struct inode
*inode
= dentry
->d_inode
;
3698 trans
= __unlink_start_trans(dir
);
3700 return PTR_ERR(trans
);
3702 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3704 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3705 dentry
->d_name
.name
, dentry
->d_name
.len
);
3709 if (inode
->i_nlink
== 0) {
3710 ret
= btrfs_orphan_add(trans
, inode
);
3716 btrfs_end_transaction(trans
, root
);
3717 btrfs_btree_balance_dirty(root
);
3721 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3722 struct btrfs_root
*root
,
3723 struct inode
*dir
, u64 objectid
,
3724 const char *name
, int name_len
)
3726 struct btrfs_path
*path
;
3727 struct extent_buffer
*leaf
;
3728 struct btrfs_dir_item
*di
;
3729 struct btrfs_key key
;
3732 u64 dir_ino
= btrfs_ino(dir
);
3734 path
= btrfs_alloc_path();
3738 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3739 name
, name_len
, -1);
3740 if (IS_ERR_OR_NULL(di
)) {
3748 leaf
= path
->nodes
[0];
3749 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3750 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3751 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3753 btrfs_abort_transaction(trans
, root
, ret
);
3756 btrfs_release_path(path
);
3758 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3759 objectid
, root
->root_key
.objectid
,
3760 dir_ino
, &index
, name
, name_len
);
3762 if (ret
!= -ENOENT
) {
3763 btrfs_abort_transaction(trans
, root
, ret
);
3766 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3768 if (IS_ERR_OR_NULL(di
)) {
3773 btrfs_abort_transaction(trans
, root
, ret
);
3777 leaf
= path
->nodes
[0];
3778 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3779 btrfs_release_path(path
);
3782 btrfs_release_path(path
);
3784 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3786 btrfs_abort_transaction(trans
, root
, ret
);
3790 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3791 inode_inc_iversion(dir
);
3792 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3793 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3795 btrfs_abort_transaction(trans
, root
, ret
);
3797 btrfs_free_path(path
);
3801 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3803 struct inode
*inode
= dentry
->d_inode
;
3805 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3806 struct btrfs_trans_handle
*trans
;
3808 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3810 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3813 trans
= __unlink_start_trans(dir
);
3815 return PTR_ERR(trans
);
3817 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3818 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3819 BTRFS_I(inode
)->location
.objectid
,
3820 dentry
->d_name
.name
,
3821 dentry
->d_name
.len
);
3825 err
= btrfs_orphan_add(trans
, inode
);
3829 /* now the directory is empty */
3830 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3831 dentry
->d_name
.name
, dentry
->d_name
.len
);
3833 btrfs_i_size_write(inode
, 0);
3835 btrfs_end_transaction(trans
, root
);
3836 btrfs_btree_balance_dirty(root
);
3842 * this can truncate away extent items, csum items and directory items.
3843 * It starts at a high offset and removes keys until it can't find
3844 * any higher than new_size
3846 * csum items that cross the new i_size are truncated to the new size
3849 * min_type is the minimum key type to truncate down to. If set to 0, this
3850 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3852 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3853 struct btrfs_root
*root
,
3854 struct inode
*inode
,
3855 u64 new_size
, u32 min_type
)
3857 struct btrfs_path
*path
;
3858 struct extent_buffer
*leaf
;
3859 struct btrfs_file_extent_item
*fi
;
3860 struct btrfs_key key
;
3861 struct btrfs_key found_key
;
3862 u64 extent_start
= 0;
3863 u64 extent_num_bytes
= 0;
3864 u64 extent_offset
= 0;
3866 u64 last_size
= (u64
)-1;
3867 u32 found_type
= (u8
)-1;
3870 int pending_del_nr
= 0;
3871 int pending_del_slot
= 0;
3872 int extent_type
= -1;
3875 u64 ino
= btrfs_ino(inode
);
3877 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3879 path
= btrfs_alloc_path();
3885 * We want to drop from the next block forward in case this new size is
3886 * not block aligned since we will be keeping the last block of the
3887 * extent just the way it is.
3889 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3890 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
3891 root
->sectorsize
), (u64
)-1, 0);
3894 * This function is also used to drop the items in the log tree before
3895 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3896 * it is used to drop the loged items. So we shouldn't kill the delayed
3899 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
3900 btrfs_kill_delayed_inode_items(inode
);
3903 key
.offset
= (u64
)-1;
3907 path
->leave_spinning
= 1;
3908 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3915 /* there are no items in the tree for us to truncate, we're
3918 if (path
->slots
[0] == 0)
3925 leaf
= path
->nodes
[0];
3926 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3927 found_type
= btrfs_key_type(&found_key
);
3929 if (found_key
.objectid
!= ino
)
3932 if (found_type
< min_type
)
3935 item_end
= found_key
.offset
;
3936 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3937 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3938 struct btrfs_file_extent_item
);
3939 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3940 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3942 btrfs_file_extent_num_bytes(leaf
, fi
);
3943 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3944 item_end
+= btrfs_file_extent_inline_len(leaf
,
3949 if (found_type
> min_type
) {
3952 if (item_end
< new_size
)
3954 if (found_key
.offset
>= new_size
)
3960 /* FIXME, shrink the extent if the ref count is only 1 */
3961 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3965 last_size
= found_key
.offset
;
3967 last_size
= new_size
;
3969 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3971 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3973 u64 orig_num_bytes
=
3974 btrfs_file_extent_num_bytes(leaf
, fi
);
3975 extent_num_bytes
= ALIGN(new_size
-
3978 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3980 num_dec
= (orig_num_bytes
-
3982 if (root
->ref_cows
&& extent_start
!= 0)
3983 inode_sub_bytes(inode
, num_dec
);
3984 btrfs_mark_buffer_dirty(leaf
);
3987 btrfs_file_extent_disk_num_bytes(leaf
,
3989 extent_offset
= found_key
.offset
-
3990 btrfs_file_extent_offset(leaf
, fi
);
3992 /* FIXME blocksize != 4096 */
3993 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
3994 if (extent_start
!= 0) {
3997 inode_sub_bytes(inode
, num_dec
);
4000 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4002 * we can't truncate inline items that have had
4006 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4007 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4008 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4009 u32 size
= new_size
- found_key
.offset
;
4011 if (root
->ref_cows
) {
4012 inode_sub_bytes(inode
, item_end
+ 1 -
4016 btrfs_file_extent_calc_inline_size(size
);
4017 btrfs_truncate_item(root
, path
, size
, 1);
4018 } else if (root
->ref_cows
) {
4019 inode_sub_bytes(inode
, item_end
+ 1 -
4025 if (!pending_del_nr
) {
4026 /* no pending yet, add ourselves */
4027 pending_del_slot
= path
->slots
[0];
4029 } else if (pending_del_nr
&&
4030 path
->slots
[0] + 1 == pending_del_slot
) {
4031 /* hop on the pending chunk */
4033 pending_del_slot
= path
->slots
[0];
4040 if (found_extent
&& (root
->ref_cows
||
4041 root
== root
->fs_info
->tree_root
)) {
4042 btrfs_set_path_blocking(path
);
4043 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4044 extent_num_bytes
, 0,
4045 btrfs_header_owner(leaf
),
4046 ino
, extent_offset
, 0);
4050 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4053 if (path
->slots
[0] == 0 ||
4054 path
->slots
[0] != pending_del_slot
) {
4055 if (pending_del_nr
) {
4056 ret
= btrfs_del_items(trans
, root
, path
,
4060 btrfs_abort_transaction(trans
,
4066 btrfs_release_path(path
);
4073 if (pending_del_nr
) {
4074 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4077 btrfs_abort_transaction(trans
, root
, ret
);
4080 if (last_size
!= (u64
)-1)
4081 btrfs_ordered_update_i_size(inode
, last_size
, NULL
);
4082 btrfs_free_path(path
);
4087 * btrfs_truncate_page - read, zero a chunk and write a page
4088 * @inode - inode that we're zeroing
4089 * @from - the offset to start zeroing
4090 * @len - the length to zero, 0 to zero the entire range respective to the
4092 * @front - zero up to the offset instead of from the offset on
4094 * This will find the page for the "from" offset and cow the page and zero the
4095 * part we want to zero. This is used with truncate and hole punching.
4097 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4100 struct address_space
*mapping
= inode
->i_mapping
;
4101 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4102 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4103 struct btrfs_ordered_extent
*ordered
;
4104 struct extent_state
*cached_state
= NULL
;
4106 u32 blocksize
= root
->sectorsize
;
4107 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4108 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4110 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4115 if ((offset
& (blocksize
- 1)) == 0 &&
4116 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4118 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4123 page
= find_or_create_page(mapping
, index
, mask
);
4125 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4130 page_start
= page_offset(page
);
4131 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4133 if (!PageUptodate(page
)) {
4134 ret
= btrfs_readpage(NULL
, page
);
4136 if (page
->mapping
!= mapping
) {
4138 page_cache_release(page
);
4141 if (!PageUptodate(page
)) {
4146 wait_on_page_writeback(page
);
4148 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4149 set_page_extent_mapped(page
);
4151 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4153 unlock_extent_cached(io_tree
, page_start
, page_end
,
4154 &cached_state
, GFP_NOFS
);
4156 page_cache_release(page
);
4157 btrfs_start_ordered_extent(inode
, ordered
, 1);
4158 btrfs_put_ordered_extent(ordered
);
4162 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4163 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4164 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4165 0, 0, &cached_state
, GFP_NOFS
);
4167 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4170 unlock_extent_cached(io_tree
, page_start
, page_end
,
4171 &cached_state
, GFP_NOFS
);
4175 if (offset
!= PAGE_CACHE_SIZE
) {
4177 len
= PAGE_CACHE_SIZE
- offset
;
4180 memset(kaddr
, 0, offset
);
4182 memset(kaddr
+ offset
, 0, len
);
4183 flush_dcache_page(page
);
4186 ClearPageChecked(page
);
4187 set_page_dirty(page
);
4188 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4193 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4195 page_cache_release(page
);
4200 static int maybe_insert_hole(struct btrfs_root
*root
, struct inode
*inode
,
4201 u64 offset
, u64 len
)
4203 struct btrfs_trans_handle
*trans
;
4207 * Still need to make sure the inode looks like it's been updated so
4208 * that any holes get logged if we fsync.
4210 if (btrfs_fs_incompat(root
->fs_info
, NO_HOLES
)) {
4211 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
4212 BTRFS_I(inode
)->last_sub_trans
= root
->log_transid
;
4213 BTRFS_I(inode
)->last_log_commit
= root
->last_log_commit
;
4218 * 1 - for the one we're dropping
4219 * 1 - for the one we're adding
4220 * 1 - for updating the inode.
4222 trans
= btrfs_start_transaction(root
, 3);
4224 return PTR_ERR(trans
);
4226 ret
= btrfs_drop_extents(trans
, root
, inode
, offset
, offset
+ len
, 1);
4228 btrfs_abort_transaction(trans
, root
, ret
);
4229 btrfs_end_transaction(trans
, root
);
4233 ret
= btrfs_insert_file_extent(trans
, root
, btrfs_ino(inode
), offset
,
4234 0, 0, len
, 0, len
, 0, 0, 0);
4236 btrfs_abort_transaction(trans
, root
, ret
);
4238 btrfs_update_inode(trans
, root
, inode
);
4239 btrfs_end_transaction(trans
, root
);
4244 * This function puts in dummy file extents for the area we're creating a hole
4245 * for. So if we are truncating this file to a larger size we need to insert
4246 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4247 * the range between oldsize and size
4249 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4251 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4252 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4253 struct extent_map
*em
= NULL
;
4254 struct extent_state
*cached_state
= NULL
;
4255 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4256 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4257 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4264 * If our size started in the middle of a page we need to zero out the
4265 * rest of the page before we expand the i_size, otherwise we could
4266 * expose stale data.
4268 err
= btrfs_truncate_page(inode
, oldsize
, 0, 0);
4272 if (size
<= hole_start
)
4276 struct btrfs_ordered_extent
*ordered
;
4278 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4280 ordered
= btrfs_lookup_ordered_range(inode
, hole_start
,
4281 block_end
- hole_start
);
4284 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4285 &cached_state
, GFP_NOFS
);
4286 btrfs_start_ordered_extent(inode
, ordered
, 1);
4287 btrfs_put_ordered_extent(ordered
);
4290 cur_offset
= hole_start
;
4292 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4293 block_end
- cur_offset
, 0);
4299 last_byte
= min(extent_map_end(em
), block_end
);
4300 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4301 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4302 struct extent_map
*hole_em
;
4303 hole_size
= last_byte
- cur_offset
;
4305 err
= maybe_insert_hole(root
, inode
, cur_offset
,
4309 btrfs_drop_extent_cache(inode
, cur_offset
,
4310 cur_offset
+ hole_size
- 1, 0);
4311 hole_em
= alloc_extent_map();
4313 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4314 &BTRFS_I(inode
)->runtime_flags
);
4317 hole_em
->start
= cur_offset
;
4318 hole_em
->len
= hole_size
;
4319 hole_em
->orig_start
= cur_offset
;
4321 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4322 hole_em
->block_len
= 0;
4323 hole_em
->orig_block_len
= 0;
4324 hole_em
->ram_bytes
= hole_size
;
4325 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4326 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4327 hole_em
->generation
= root
->fs_info
->generation
;
4330 write_lock(&em_tree
->lock
);
4331 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4332 write_unlock(&em_tree
->lock
);
4335 btrfs_drop_extent_cache(inode
, cur_offset
,
4339 free_extent_map(hole_em
);
4342 free_extent_map(em
);
4344 cur_offset
= last_byte
;
4345 if (cur_offset
>= block_end
)
4348 free_extent_map(em
);
4349 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4354 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4356 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4357 struct btrfs_trans_handle
*trans
;
4358 loff_t oldsize
= i_size_read(inode
);
4359 loff_t newsize
= attr
->ia_size
;
4360 int mask
= attr
->ia_valid
;
4364 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4365 * special case where we need to update the times despite not having
4366 * these flags set. For all other operations the VFS set these flags
4367 * explicitly if it wants a timestamp update.
4369 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
4370 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
4372 if (newsize
> oldsize
) {
4373 truncate_pagecache(inode
, newsize
);
4374 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4378 trans
= btrfs_start_transaction(root
, 1);
4380 return PTR_ERR(trans
);
4382 i_size_write(inode
, newsize
);
4383 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4384 ret
= btrfs_update_inode(trans
, root
, inode
);
4385 btrfs_end_transaction(trans
, root
);
4389 * We're truncating a file that used to have good data down to
4390 * zero. Make sure it gets into the ordered flush list so that
4391 * any new writes get down to disk quickly.
4394 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4395 &BTRFS_I(inode
)->runtime_flags
);
4398 * 1 for the orphan item we're going to add
4399 * 1 for the orphan item deletion.
4401 trans
= btrfs_start_transaction(root
, 2);
4403 return PTR_ERR(trans
);
4406 * We need to do this in case we fail at _any_ point during the
4407 * actual truncate. Once we do the truncate_setsize we could
4408 * invalidate pages which forces any outstanding ordered io to
4409 * be instantly completed which will give us extents that need
4410 * to be truncated. If we fail to get an orphan inode down we
4411 * could have left over extents that were never meant to live,
4412 * so we need to garuntee from this point on that everything
4413 * will be consistent.
4415 ret
= btrfs_orphan_add(trans
, inode
);
4416 btrfs_end_transaction(trans
, root
);
4420 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4421 truncate_setsize(inode
, newsize
);
4423 /* Disable nonlocked read DIO to avoid the end less truncate */
4424 btrfs_inode_block_unlocked_dio(inode
);
4425 inode_dio_wait(inode
);
4426 btrfs_inode_resume_unlocked_dio(inode
);
4428 ret
= btrfs_truncate(inode
);
4429 if (ret
&& inode
->i_nlink
) {
4433 * failed to truncate, disk_i_size is only adjusted down
4434 * as we remove extents, so it should represent the true
4435 * size of the inode, so reset the in memory size and
4436 * delete our orphan entry.
4438 trans
= btrfs_join_transaction(root
);
4439 if (IS_ERR(trans
)) {
4440 btrfs_orphan_del(NULL
, inode
);
4443 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
4444 err
= btrfs_orphan_del(trans
, inode
);
4446 btrfs_abort_transaction(trans
, root
, err
);
4447 btrfs_end_transaction(trans
, root
);
4454 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4456 struct inode
*inode
= dentry
->d_inode
;
4457 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4460 if (btrfs_root_readonly(root
))
4463 err
= inode_change_ok(inode
, attr
);
4467 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4468 err
= btrfs_setsize(inode
, attr
);
4473 if (attr
->ia_valid
) {
4474 setattr_copy(inode
, attr
);
4475 inode_inc_iversion(inode
);
4476 err
= btrfs_dirty_inode(inode
);
4478 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4479 err
= btrfs_acl_chmod(inode
);
4486 * While truncating the inode pages during eviction, we get the VFS calling
4487 * btrfs_invalidatepage() against each page of the inode. This is slow because
4488 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4489 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4490 * extent_state structures over and over, wasting lots of time.
4492 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4493 * those expensive operations on a per page basis and do only the ordered io
4494 * finishing, while we release here the extent_map and extent_state structures,
4495 * without the excessive merging and splitting.
4497 static void evict_inode_truncate_pages(struct inode
*inode
)
4499 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4500 struct extent_map_tree
*map_tree
= &BTRFS_I(inode
)->extent_tree
;
4501 struct rb_node
*node
;
4503 ASSERT(inode
->i_state
& I_FREEING
);
4504 truncate_inode_pages(&inode
->i_data
, 0);
4506 write_lock(&map_tree
->lock
);
4507 while (!RB_EMPTY_ROOT(&map_tree
->map
)) {
4508 struct extent_map
*em
;
4510 node
= rb_first(&map_tree
->map
);
4511 em
= rb_entry(node
, struct extent_map
, rb_node
);
4512 clear_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
4513 clear_bit(EXTENT_FLAG_LOGGING
, &em
->flags
);
4514 remove_extent_mapping(map_tree
, em
);
4515 free_extent_map(em
);
4517 write_unlock(&map_tree
->lock
);
4519 spin_lock(&io_tree
->lock
);
4520 while (!RB_EMPTY_ROOT(&io_tree
->state
)) {
4521 struct extent_state
*state
;
4522 struct extent_state
*cached_state
= NULL
;
4524 node
= rb_first(&io_tree
->state
);
4525 state
= rb_entry(node
, struct extent_state
, rb_node
);
4526 atomic_inc(&state
->refs
);
4527 spin_unlock(&io_tree
->lock
);
4529 lock_extent_bits(io_tree
, state
->start
, state
->end
,
4531 clear_extent_bit(io_tree
, state
->start
, state
->end
,
4532 EXTENT_LOCKED
| EXTENT_DIRTY
|
4533 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
4534 EXTENT_DEFRAG
, 1, 1,
4535 &cached_state
, GFP_NOFS
);
4536 free_extent_state(state
);
4538 spin_lock(&io_tree
->lock
);
4540 spin_unlock(&io_tree
->lock
);
4543 void btrfs_evict_inode(struct inode
*inode
)
4545 struct btrfs_trans_handle
*trans
;
4546 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4547 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4548 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4551 trace_btrfs_inode_evict(inode
);
4553 evict_inode_truncate_pages(inode
);
4555 if (inode
->i_nlink
&&
4556 ((btrfs_root_refs(&root
->root_item
) != 0 &&
4557 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
4558 btrfs_is_free_space_inode(inode
)))
4561 if (is_bad_inode(inode
)) {
4562 btrfs_orphan_del(NULL
, inode
);
4565 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4566 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4568 if (root
->fs_info
->log_root_recovering
) {
4569 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4570 &BTRFS_I(inode
)->runtime_flags
));
4574 if (inode
->i_nlink
> 0) {
4575 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
4576 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
4580 ret
= btrfs_commit_inode_delayed_inode(inode
);
4582 btrfs_orphan_del(NULL
, inode
);
4586 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4588 btrfs_orphan_del(NULL
, inode
);
4591 rsv
->size
= min_size
;
4593 global_rsv
= &root
->fs_info
->global_block_rsv
;
4595 btrfs_i_size_write(inode
, 0);
4598 * This is a bit simpler than btrfs_truncate since we've already
4599 * reserved our space for our orphan item in the unlink, so we just
4600 * need to reserve some slack space in case we add bytes and update
4601 * inode item when doing the truncate.
4604 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4605 BTRFS_RESERVE_FLUSH_LIMIT
);
4608 * Try and steal from the global reserve since we will
4609 * likely not use this space anyway, we want to try as
4610 * hard as possible to get this to work.
4613 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4616 btrfs_warn(root
->fs_info
,
4617 "Could not get space for a delete, will truncate on mount %d",
4619 btrfs_orphan_del(NULL
, inode
);
4620 btrfs_free_block_rsv(root
, rsv
);
4624 trans
= btrfs_join_transaction(root
);
4625 if (IS_ERR(trans
)) {
4626 btrfs_orphan_del(NULL
, inode
);
4627 btrfs_free_block_rsv(root
, rsv
);
4631 trans
->block_rsv
= rsv
;
4633 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4637 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4638 btrfs_end_transaction(trans
, root
);
4640 btrfs_btree_balance_dirty(root
);
4643 btrfs_free_block_rsv(root
, rsv
);
4646 * Errors here aren't a big deal, it just means we leave orphan items
4647 * in the tree. They will be cleaned up on the next mount.
4650 trans
->block_rsv
= root
->orphan_block_rsv
;
4651 btrfs_orphan_del(trans
, inode
);
4653 btrfs_orphan_del(NULL
, inode
);
4656 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4657 if (!(root
== root
->fs_info
->tree_root
||
4658 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4659 btrfs_return_ino(root
, btrfs_ino(inode
));
4661 btrfs_end_transaction(trans
, root
);
4662 btrfs_btree_balance_dirty(root
);
4664 btrfs_remove_delayed_node(inode
);
4670 * this returns the key found in the dir entry in the location pointer.
4671 * If no dir entries were found, location->objectid is 0.
4673 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4674 struct btrfs_key
*location
)
4676 const char *name
= dentry
->d_name
.name
;
4677 int namelen
= dentry
->d_name
.len
;
4678 struct btrfs_dir_item
*di
;
4679 struct btrfs_path
*path
;
4680 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4683 path
= btrfs_alloc_path();
4687 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4692 if (IS_ERR_OR_NULL(di
))
4695 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4697 btrfs_free_path(path
);
4700 location
->objectid
= 0;
4705 * when we hit a tree root in a directory, the btrfs part of the inode
4706 * needs to be changed to reflect the root directory of the tree root. This
4707 * is kind of like crossing a mount point.
4709 static int fixup_tree_root_location(struct btrfs_root
*root
,
4711 struct dentry
*dentry
,
4712 struct btrfs_key
*location
,
4713 struct btrfs_root
**sub_root
)
4715 struct btrfs_path
*path
;
4716 struct btrfs_root
*new_root
;
4717 struct btrfs_root_ref
*ref
;
4718 struct extent_buffer
*leaf
;
4722 path
= btrfs_alloc_path();
4729 ret
= btrfs_find_item(root
->fs_info
->tree_root
, path
,
4730 BTRFS_I(dir
)->root
->root_key
.objectid
,
4731 location
->objectid
, BTRFS_ROOT_REF_KEY
, NULL
);
4738 leaf
= path
->nodes
[0];
4739 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4740 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4741 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4744 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4745 (unsigned long)(ref
+ 1),
4746 dentry
->d_name
.len
);
4750 btrfs_release_path(path
);
4752 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4753 if (IS_ERR(new_root
)) {
4754 err
= PTR_ERR(new_root
);
4758 *sub_root
= new_root
;
4759 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4760 location
->type
= BTRFS_INODE_ITEM_KEY
;
4761 location
->offset
= 0;
4764 btrfs_free_path(path
);
4768 static void inode_tree_add(struct inode
*inode
)
4770 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4771 struct btrfs_inode
*entry
;
4773 struct rb_node
*parent
;
4774 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
4775 u64 ino
= btrfs_ino(inode
);
4777 if (inode_unhashed(inode
))
4780 spin_lock(&root
->inode_lock
);
4781 p
= &root
->inode_tree
.rb_node
;
4784 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4786 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4787 p
= &parent
->rb_left
;
4788 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4789 p
= &parent
->rb_right
;
4791 WARN_ON(!(entry
->vfs_inode
.i_state
&
4792 (I_WILL_FREE
| I_FREEING
)));
4793 rb_replace_node(parent
, new, &root
->inode_tree
);
4794 RB_CLEAR_NODE(parent
);
4795 spin_unlock(&root
->inode_lock
);
4799 rb_link_node(new, parent
, p
);
4800 rb_insert_color(new, &root
->inode_tree
);
4801 spin_unlock(&root
->inode_lock
);
4804 static void inode_tree_del(struct inode
*inode
)
4806 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4809 spin_lock(&root
->inode_lock
);
4810 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4811 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4812 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4813 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4815 spin_unlock(&root
->inode_lock
);
4817 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
4818 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4819 spin_lock(&root
->inode_lock
);
4820 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4821 spin_unlock(&root
->inode_lock
);
4823 btrfs_add_dead_root(root
);
4827 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4829 struct rb_node
*node
;
4830 struct rb_node
*prev
;
4831 struct btrfs_inode
*entry
;
4832 struct inode
*inode
;
4835 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4837 spin_lock(&root
->inode_lock
);
4839 node
= root
->inode_tree
.rb_node
;
4843 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4845 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4846 node
= node
->rb_left
;
4847 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4848 node
= node
->rb_right
;
4854 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4855 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4859 prev
= rb_next(prev
);
4863 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4864 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4865 inode
= igrab(&entry
->vfs_inode
);
4867 spin_unlock(&root
->inode_lock
);
4868 if (atomic_read(&inode
->i_count
) > 1)
4869 d_prune_aliases(inode
);
4871 * btrfs_drop_inode will have it removed from
4872 * the inode cache when its usage count
4877 spin_lock(&root
->inode_lock
);
4881 if (cond_resched_lock(&root
->inode_lock
))
4884 node
= rb_next(node
);
4886 spin_unlock(&root
->inode_lock
);
4889 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4891 struct btrfs_iget_args
*args
= p
;
4892 inode
->i_ino
= args
->ino
;
4893 BTRFS_I(inode
)->root
= args
->root
;
4897 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4899 struct btrfs_iget_args
*args
= opaque
;
4900 return args
->ino
== btrfs_ino(inode
) &&
4901 args
->root
== BTRFS_I(inode
)->root
;
4904 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4906 struct btrfs_root
*root
)
4908 struct inode
*inode
;
4909 struct btrfs_iget_args args
;
4910 unsigned long hashval
= btrfs_inode_hash(objectid
, root
);
4912 args
.ino
= objectid
;
4915 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
4916 btrfs_init_locked_inode
,
4921 /* Get an inode object given its location and corresponding root.
4922 * Returns in *is_new if the inode was read from disk
4924 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4925 struct btrfs_root
*root
, int *new)
4927 struct inode
*inode
;
4929 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4931 return ERR_PTR(-ENOMEM
);
4933 if (inode
->i_state
& I_NEW
) {
4934 BTRFS_I(inode
)->root
= root
;
4935 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4936 btrfs_read_locked_inode(inode
);
4937 if (!is_bad_inode(inode
)) {
4938 inode_tree_add(inode
);
4939 unlock_new_inode(inode
);
4943 unlock_new_inode(inode
);
4945 inode
= ERR_PTR(-ESTALE
);
4952 static struct inode
*new_simple_dir(struct super_block
*s
,
4953 struct btrfs_key
*key
,
4954 struct btrfs_root
*root
)
4956 struct inode
*inode
= new_inode(s
);
4959 return ERR_PTR(-ENOMEM
);
4961 BTRFS_I(inode
)->root
= root
;
4962 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4963 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
4965 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4966 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
4967 inode
->i_fop
= &simple_dir_operations
;
4968 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4969 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4974 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4976 struct inode
*inode
;
4977 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4978 struct btrfs_root
*sub_root
= root
;
4979 struct btrfs_key location
;
4983 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4984 return ERR_PTR(-ENAMETOOLONG
);
4986 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4988 return ERR_PTR(ret
);
4990 if (location
.objectid
== 0)
4991 return ERR_PTR(-ENOENT
);
4993 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4994 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4998 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5000 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
5001 ret
= fixup_tree_root_location(root
, dir
, dentry
,
5002 &location
, &sub_root
);
5005 inode
= ERR_PTR(ret
);
5007 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5009 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5011 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
5013 if (!IS_ERR(inode
) && root
!= sub_root
) {
5014 down_read(&root
->fs_info
->cleanup_work_sem
);
5015 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5016 ret
= btrfs_orphan_cleanup(sub_root
);
5017 up_read(&root
->fs_info
->cleanup_work_sem
);
5020 inode
= ERR_PTR(ret
);
5027 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5029 struct btrfs_root
*root
;
5030 struct inode
*inode
= dentry
->d_inode
;
5032 if (!inode
&& !IS_ROOT(dentry
))
5033 inode
= dentry
->d_parent
->d_inode
;
5036 root
= BTRFS_I(inode
)->root
;
5037 if (btrfs_root_refs(&root
->root_item
) == 0)
5040 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5046 static void btrfs_dentry_release(struct dentry
*dentry
)
5048 if (dentry
->d_fsdata
)
5049 kfree(dentry
->d_fsdata
);
5052 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5055 struct inode
*inode
;
5057 inode
= btrfs_lookup_dentry(dir
, dentry
);
5058 if (IS_ERR(inode
)) {
5059 if (PTR_ERR(inode
) == -ENOENT
)
5062 return ERR_CAST(inode
);
5065 return d_splice_alias(inode
, dentry
);
5068 unsigned char btrfs_filetype_table
[] = {
5069 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5072 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
5074 struct inode
*inode
= file_inode(file
);
5075 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5076 struct btrfs_item
*item
;
5077 struct btrfs_dir_item
*di
;
5078 struct btrfs_key key
;
5079 struct btrfs_key found_key
;
5080 struct btrfs_path
*path
;
5081 struct list_head ins_list
;
5082 struct list_head del_list
;
5084 struct extent_buffer
*leaf
;
5086 unsigned char d_type
;
5091 int key_type
= BTRFS_DIR_INDEX_KEY
;
5095 int is_curr
= 0; /* ctx->pos points to the current index? */
5097 /* FIXME, use a real flag for deciding about the key type */
5098 if (root
->fs_info
->tree_root
== root
)
5099 key_type
= BTRFS_DIR_ITEM_KEY
;
5101 if (!dir_emit_dots(file
, ctx
))
5104 path
= btrfs_alloc_path();
5110 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5111 INIT_LIST_HEAD(&ins_list
);
5112 INIT_LIST_HEAD(&del_list
);
5113 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5116 btrfs_set_key_type(&key
, key_type
);
5117 key
.offset
= ctx
->pos
;
5118 key
.objectid
= btrfs_ino(inode
);
5120 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5125 leaf
= path
->nodes
[0];
5126 slot
= path
->slots
[0];
5127 if (slot
>= btrfs_header_nritems(leaf
)) {
5128 ret
= btrfs_next_leaf(root
, path
);
5136 item
= btrfs_item_nr(slot
);
5137 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5139 if (found_key
.objectid
!= key
.objectid
)
5141 if (btrfs_key_type(&found_key
) != key_type
)
5143 if (found_key
.offset
< ctx
->pos
)
5145 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5146 btrfs_should_delete_dir_index(&del_list
,
5150 ctx
->pos
= found_key
.offset
;
5153 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5155 di_total
= btrfs_item_size(leaf
, item
);
5157 while (di_cur
< di_total
) {
5158 struct btrfs_key location
;
5160 if (verify_dir_item(root
, leaf
, di
))
5163 name_len
= btrfs_dir_name_len(leaf
, di
);
5164 if (name_len
<= sizeof(tmp_name
)) {
5165 name_ptr
= tmp_name
;
5167 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5173 read_extent_buffer(leaf
, name_ptr
,
5174 (unsigned long)(di
+ 1), name_len
);
5176 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5177 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5180 /* is this a reference to our own snapshot? If so
5183 * In contrast to old kernels, we insert the snapshot's
5184 * dir item and dir index after it has been created, so
5185 * we won't find a reference to our own snapshot. We
5186 * still keep the following code for backward
5189 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5190 location
.objectid
== root
->root_key
.objectid
) {
5194 over
= !dir_emit(ctx
, name_ptr
, name_len
,
5195 location
.objectid
, d_type
);
5198 if (name_ptr
!= tmp_name
)
5203 di_len
= btrfs_dir_name_len(leaf
, di
) +
5204 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5206 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5212 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5215 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
);
5220 /* Reached end of directory/root. Bump pos past the last item. */
5224 * Stop new entries from being returned after we return the last
5227 * New directory entries are assigned a strictly increasing
5228 * offset. This means that new entries created during readdir
5229 * are *guaranteed* to be seen in the future by that readdir.
5230 * This has broken buggy programs which operate on names as
5231 * they're returned by readdir. Until we re-use freed offsets
5232 * we have this hack to stop new entries from being returned
5233 * under the assumption that they'll never reach this huge
5236 * This is being careful not to overflow 32bit loff_t unless the
5237 * last entry requires it because doing so has broken 32bit apps
5240 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5241 if (ctx
->pos
>= INT_MAX
)
5242 ctx
->pos
= LLONG_MAX
;
5249 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5250 btrfs_put_delayed_items(&ins_list
, &del_list
);
5251 btrfs_free_path(path
);
5255 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5257 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5258 struct btrfs_trans_handle
*trans
;
5260 bool nolock
= false;
5262 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5265 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5268 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5270 trans
= btrfs_join_transaction_nolock(root
);
5272 trans
= btrfs_join_transaction(root
);
5274 return PTR_ERR(trans
);
5275 ret
= btrfs_commit_transaction(trans
, root
);
5281 * This is somewhat expensive, updating the tree every time the
5282 * inode changes. But, it is most likely to find the inode in cache.
5283 * FIXME, needs more benchmarking...there are no reasons other than performance
5284 * to keep or drop this code.
5286 static int btrfs_dirty_inode(struct inode
*inode
)
5288 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5289 struct btrfs_trans_handle
*trans
;
5292 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5295 trans
= btrfs_join_transaction(root
);
5297 return PTR_ERR(trans
);
5299 ret
= btrfs_update_inode(trans
, root
, inode
);
5300 if (ret
&& ret
== -ENOSPC
) {
5301 /* whoops, lets try again with the full transaction */
5302 btrfs_end_transaction(trans
, root
);
5303 trans
= btrfs_start_transaction(root
, 1);
5305 return PTR_ERR(trans
);
5307 ret
= btrfs_update_inode(trans
, root
, inode
);
5309 btrfs_end_transaction(trans
, root
);
5310 if (BTRFS_I(inode
)->delayed_node
)
5311 btrfs_balance_delayed_items(root
);
5317 * This is a copy of file_update_time. We need this so we can return error on
5318 * ENOSPC for updating the inode in the case of file write and mmap writes.
5320 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5323 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5325 if (btrfs_root_readonly(root
))
5328 if (flags
& S_VERSION
)
5329 inode_inc_iversion(inode
);
5330 if (flags
& S_CTIME
)
5331 inode
->i_ctime
= *now
;
5332 if (flags
& S_MTIME
)
5333 inode
->i_mtime
= *now
;
5334 if (flags
& S_ATIME
)
5335 inode
->i_atime
= *now
;
5336 return btrfs_dirty_inode(inode
);
5340 * find the highest existing sequence number in a directory
5341 * and then set the in-memory index_cnt variable to reflect
5342 * free sequence numbers
5344 static int btrfs_set_inode_index_count(struct inode
*inode
)
5346 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5347 struct btrfs_key key
, found_key
;
5348 struct btrfs_path
*path
;
5349 struct extent_buffer
*leaf
;
5352 key
.objectid
= btrfs_ino(inode
);
5353 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
5354 key
.offset
= (u64
)-1;
5356 path
= btrfs_alloc_path();
5360 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5363 /* FIXME: we should be able to handle this */
5369 * MAGIC NUMBER EXPLANATION:
5370 * since we search a directory based on f_pos we have to start at 2
5371 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5372 * else has to start at 2
5374 if (path
->slots
[0] == 0) {
5375 BTRFS_I(inode
)->index_cnt
= 2;
5381 leaf
= path
->nodes
[0];
5382 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5384 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5385 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
5386 BTRFS_I(inode
)->index_cnt
= 2;
5390 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5392 btrfs_free_path(path
);
5397 * helper to find a free sequence number in a given directory. This current
5398 * code is very simple, later versions will do smarter things in the btree
5400 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5404 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5405 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5407 ret
= btrfs_set_inode_index_count(dir
);
5413 *index
= BTRFS_I(dir
)->index_cnt
;
5414 BTRFS_I(dir
)->index_cnt
++;
5419 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5420 struct btrfs_root
*root
,
5422 const char *name
, int name_len
,
5423 u64 ref_objectid
, u64 objectid
,
5424 umode_t mode
, u64
*index
)
5426 struct inode
*inode
;
5427 struct btrfs_inode_item
*inode_item
;
5428 struct btrfs_key
*location
;
5429 struct btrfs_path
*path
;
5430 struct btrfs_inode_ref
*ref
;
5431 struct btrfs_key key
[2];
5436 path
= btrfs_alloc_path();
5438 return ERR_PTR(-ENOMEM
);
5440 inode
= new_inode(root
->fs_info
->sb
);
5442 btrfs_free_path(path
);
5443 return ERR_PTR(-ENOMEM
);
5447 * we have to initialize this early, so we can reclaim the inode
5448 * number if we fail afterwards in this function.
5450 inode
->i_ino
= objectid
;
5453 trace_btrfs_inode_request(dir
);
5455 ret
= btrfs_set_inode_index(dir
, index
);
5457 btrfs_free_path(path
);
5459 return ERR_PTR(ret
);
5463 * index_cnt is ignored for everything but a dir,
5464 * btrfs_get_inode_index_count has an explanation for the magic
5467 BTRFS_I(inode
)->index_cnt
= 2;
5468 BTRFS_I(inode
)->root
= root
;
5469 BTRFS_I(inode
)->generation
= trans
->transid
;
5470 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5473 * We could have gotten an inode number from somebody who was fsynced
5474 * and then removed in this same transaction, so let's just set full
5475 * sync since it will be a full sync anyway and this will blow away the
5476 * old info in the log.
5478 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5480 key
[0].objectid
= objectid
;
5481 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
5485 * Start new inodes with an inode_ref. This is slightly more
5486 * efficient for small numbers of hard links since they will
5487 * be packed into one item. Extended refs will kick in if we
5488 * add more hard links than can fit in the ref item.
5490 key
[1].objectid
= objectid
;
5491 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
5492 key
[1].offset
= ref_objectid
;
5494 sizes
[0] = sizeof(struct btrfs_inode_item
);
5495 sizes
[1] = name_len
+ sizeof(*ref
);
5497 path
->leave_spinning
= 1;
5498 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
5502 inode_init_owner(inode
, dir
, mode
);
5503 inode_set_bytes(inode
, 0);
5504 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5505 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5506 struct btrfs_inode_item
);
5507 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5508 sizeof(*inode_item
));
5509 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5511 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5512 struct btrfs_inode_ref
);
5513 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5514 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5515 ptr
= (unsigned long)(ref
+ 1);
5516 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5518 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5519 btrfs_free_path(path
);
5521 location
= &BTRFS_I(inode
)->location
;
5522 location
->objectid
= objectid
;
5523 location
->offset
= 0;
5524 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
5526 btrfs_inherit_iflags(inode
, dir
);
5528 if (S_ISREG(mode
)) {
5529 if (btrfs_test_opt(root
, NODATASUM
))
5530 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5531 if (btrfs_test_opt(root
, NODATACOW
))
5532 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5533 BTRFS_INODE_NODATASUM
;
5536 btrfs_insert_inode_hash(inode
);
5537 inode_tree_add(inode
);
5539 trace_btrfs_inode_new(inode
);
5540 btrfs_set_inode_last_trans(trans
, inode
);
5542 btrfs_update_root_times(trans
, root
);
5547 BTRFS_I(dir
)->index_cnt
--;
5548 btrfs_free_path(path
);
5550 return ERR_PTR(ret
);
5553 static inline u8
btrfs_inode_type(struct inode
*inode
)
5555 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5559 * utility function to add 'inode' into 'parent_inode' with
5560 * a give name and a given sequence number.
5561 * if 'add_backref' is true, also insert a backref from the
5562 * inode to the parent directory.
5564 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5565 struct inode
*parent_inode
, struct inode
*inode
,
5566 const char *name
, int name_len
, int add_backref
, u64 index
)
5569 struct btrfs_key key
;
5570 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
5571 u64 ino
= btrfs_ino(inode
);
5572 u64 parent_ino
= btrfs_ino(parent_inode
);
5574 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5575 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
5578 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
5582 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5583 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
5584 key
.objectid
, root
->root_key
.objectid
,
5585 parent_ino
, index
, name
, name_len
);
5586 } else if (add_backref
) {
5587 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
5591 /* Nothing to clean up yet */
5595 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
5597 btrfs_inode_type(inode
), index
);
5598 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
5601 btrfs_abort_transaction(trans
, root
, ret
);
5605 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5607 inode_inc_iversion(parent_inode
);
5608 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5609 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5611 btrfs_abort_transaction(trans
, root
, ret
);
5615 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5618 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5619 key
.objectid
, root
->root_key
.objectid
,
5620 parent_ino
, &local_index
, name
, name_len
);
5622 } else if (add_backref
) {
5626 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5627 ino
, parent_ino
, &local_index
);
5632 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5633 struct inode
*dir
, struct dentry
*dentry
,
5634 struct inode
*inode
, int backref
, u64 index
)
5636 int err
= btrfs_add_link(trans
, dir
, inode
,
5637 dentry
->d_name
.name
, dentry
->d_name
.len
,
5644 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5645 umode_t mode
, dev_t rdev
)
5647 struct btrfs_trans_handle
*trans
;
5648 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5649 struct inode
*inode
= NULL
;
5655 if (!new_valid_dev(rdev
))
5659 * 2 for inode item and ref
5661 * 1 for xattr if selinux is on
5663 trans
= btrfs_start_transaction(root
, 5);
5665 return PTR_ERR(trans
);
5667 err
= btrfs_find_free_ino(root
, &objectid
);
5671 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5672 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5674 if (IS_ERR(inode
)) {
5675 err
= PTR_ERR(inode
);
5679 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5686 * If the active LSM wants to access the inode during
5687 * d_instantiate it needs these. Smack checks to see
5688 * if the filesystem supports xattrs by looking at the
5692 inode
->i_op
= &btrfs_special_inode_operations
;
5693 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5697 init_special_inode(inode
, inode
->i_mode
, rdev
);
5698 btrfs_update_inode(trans
, root
, inode
);
5699 d_instantiate(dentry
, inode
);
5702 btrfs_end_transaction(trans
, root
);
5703 btrfs_btree_balance_dirty(root
);
5705 inode_dec_link_count(inode
);
5711 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5712 umode_t mode
, bool excl
)
5714 struct btrfs_trans_handle
*trans
;
5715 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5716 struct inode
*inode
= NULL
;
5717 int drop_inode_on_err
= 0;
5723 * 2 for inode item and ref
5725 * 1 for xattr if selinux is on
5727 trans
= btrfs_start_transaction(root
, 5);
5729 return PTR_ERR(trans
);
5731 err
= btrfs_find_free_ino(root
, &objectid
);
5735 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5736 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5738 if (IS_ERR(inode
)) {
5739 err
= PTR_ERR(inode
);
5742 drop_inode_on_err
= 1;
5744 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5748 err
= btrfs_update_inode(trans
, root
, inode
);
5753 * If the active LSM wants to access the inode during
5754 * d_instantiate it needs these. Smack checks to see
5755 * if the filesystem supports xattrs by looking at the
5758 inode
->i_fop
= &btrfs_file_operations
;
5759 inode
->i_op
= &btrfs_file_inode_operations
;
5761 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5765 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5766 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5767 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5768 d_instantiate(dentry
, inode
);
5771 btrfs_end_transaction(trans
, root
);
5772 if (err
&& drop_inode_on_err
) {
5773 inode_dec_link_count(inode
);
5776 btrfs_btree_balance_dirty(root
);
5780 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5781 struct dentry
*dentry
)
5783 struct btrfs_trans_handle
*trans
;
5784 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5785 struct inode
*inode
= old_dentry
->d_inode
;
5790 /* do not allow sys_link's with other subvols of the same device */
5791 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5794 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5797 err
= btrfs_set_inode_index(dir
, &index
);
5802 * 2 items for inode and inode ref
5803 * 2 items for dir items
5804 * 1 item for parent inode
5806 trans
= btrfs_start_transaction(root
, 5);
5807 if (IS_ERR(trans
)) {
5808 err
= PTR_ERR(trans
);
5813 inode_inc_iversion(inode
);
5814 inode
->i_ctime
= CURRENT_TIME
;
5816 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5818 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5823 struct dentry
*parent
= dentry
->d_parent
;
5824 err
= btrfs_update_inode(trans
, root
, inode
);
5827 d_instantiate(dentry
, inode
);
5828 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5831 btrfs_end_transaction(trans
, root
);
5834 inode_dec_link_count(inode
);
5837 btrfs_btree_balance_dirty(root
);
5841 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5843 struct inode
*inode
= NULL
;
5844 struct btrfs_trans_handle
*trans
;
5845 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5847 int drop_on_err
= 0;
5852 * 2 items for inode and ref
5853 * 2 items for dir items
5854 * 1 for xattr if selinux is on
5856 trans
= btrfs_start_transaction(root
, 5);
5858 return PTR_ERR(trans
);
5860 err
= btrfs_find_free_ino(root
, &objectid
);
5864 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5865 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5866 S_IFDIR
| mode
, &index
);
5867 if (IS_ERR(inode
)) {
5868 err
= PTR_ERR(inode
);
5874 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5878 inode
->i_op
= &btrfs_dir_inode_operations
;
5879 inode
->i_fop
= &btrfs_dir_file_operations
;
5881 btrfs_i_size_write(inode
, 0);
5882 err
= btrfs_update_inode(trans
, root
, inode
);
5886 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5887 dentry
->d_name
.len
, 0, index
);
5891 d_instantiate(dentry
, inode
);
5895 btrfs_end_transaction(trans
, root
);
5898 btrfs_btree_balance_dirty(root
);
5902 /* helper for btfs_get_extent. Given an existing extent in the tree,
5903 * and an extent that you want to insert, deal with overlap and insert
5904 * the new extent into the tree.
5906 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5907 struct extent_map
*existing
,
5908 struct extent_map
*em
,
5909 u64 map_start
, u64 map_len
)
5913 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5914 start_diff
= map_start
- em
->start
;
5915 em
->start
= map_start
;
5917 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5918 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5919 em
->block_start
+= start_diff
;
5920 em
->block_len
-= start_diff
;
5922 return add_extent_mapping(em_tree
, em
, 0);
5925 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5926 struct inode
*inode
, struct page
*page
,
5927 size_t pg_offset
, u64 extent_offset
,
5928 struct btrfs_file_extent_item
*item
)
5931 struct extent_buffer
*leaf
= path
->nodes
[0];
5934 unsigned long inline_size
;
5938 WARN_ON(pg_offset
!= 0);
5939 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5940 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
5941 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
5942 btrfs_item_nr(path
->slots
[0]));
5943 tmp
= kmalloc(inline_size
, GFP_NOFS
);
5946 ptr
= btrfs_file_extent_inline_start(item
);
5948 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
5950 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
5951 ret
= btrfs_decompress(compress_type
, tmp
, page
,
5952 extent_offset
, inline_size
, max_size
);
5954 char *kaddr
= kmap_atomic(page
);
5955 unsigned long copy_size
= min_t(u64
,
5956 PAGE_CACHE_SIZE
- pg_offset
,
5957 max_size
- extent_offset
);
5958 memset(kaddr
+ pg_offset
, 0, copy_size
);
5959 kunmap_atomic(kaddr
);
5966 * a bit scary, this does extent mapping from logical file offset to the disk.
5967 * the ugly parts come from merging extents from the disk with the in-ram
5968 * representation. This gets more complex because of the data=ordered code,
5969 * where the in-ram extents might be locked pending data=ordered completion.
5971 * This also copies inline extents directly into the page.
5974 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
5975 size_t pg_offset
, u64 start
, u64 len
,
5981 u64 extent_start
= 0;
5983 u64 objectid
= btrfs_ino(inode
);
5985 struct btrfs_path
*path
= NULL
;
5986 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5987 struct btrfs_file_extent_item
*item
;
5988 struct extent_buffer
*leaf
;
5989 struct btrfs_key found_key
;
5990 struct extent_map
*em
= NULL
;
5991 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5992 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5993 struct btrfs_trans_handle
*trans
= NULL
;
5997 read_lock(&em_tree
->lock
);
5998 em
= lookup_extent_mapping(em_tree
, start
, len
);
6000 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6001 read_unlock(&em_tree
->lock
);
6004 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6005 free_extent_map(em
);
6006 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6007 free_extent_map(em
);
6011 em
= alloc_extent_map();
6016 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6017 em
->start
= EXTENT_MAP_HOLE
;
6018 em
->orig_start
= EXTENT_MAP_HOLE
;
6020 em
->block_len
= (u64
)-1;
6023 path
= btrfs_alloc_path();
6029 * Chances are we'll be called again, so go ahead and do
6035 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6036 objectid
, start
, trans
!= NULL
);
6043 if (path
->slots
[0] == 0)
6048 leaf
= path
->nodes
[0];
6049 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6050 struct btrfs_file_extent_item
);
6051 /* are we inside the extent that was found? */
6052 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6053 found_type
= btrfs_key_type(&found_key
);
6054 if (found_key
.objectid
!= objectid
||
6055 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6057 * If we backup past the first extent we want to move forward
6058 * and see if there is an extent in front of us, otherwise we'll
6059 * say there is a hole for our whole search range which can
6066 found_type
= btrfs_file_extent_type(leaf
, item
);
6067 extent_start
= found_key
.offset
;
6068 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6069 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6070 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6071 extent_end
= extent_start
+
6072 btrfs_file_extent_num_bytes(leaf
, item
);
6073 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6075 size
= btrfs_file_extent_inline_len(leaf
, item
);
6076 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6079 if (start
>= extent_end
) {
6081 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6082 ret
= btrfs_next_leaf(root
, path
);
6089 leaf
= path
->nodes
[0];
6091 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6092 if (found_key
.objectid
!= objectid
||
6093 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6095 if (start
+ len
<= found_key
.offset
)
6098 em
->orig_start
= start
;
6099 em
->len
= found_key
.offset
- start
;
6103 em
->ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, item
);
6104 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6105 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6106 em
->start
= extent_start
;
6107 em
->len
= extent_end
- extent_start
;
6108 em
->orig_start
= extent_start
-
6109 btrfs_file_extent_offset(leaf
, item
);
6110 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
6112 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
6114 em
->block_start
= EXTENT_MAP_HOLE
;
6117 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
6118 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6119 em
->compress_type
= compress_type
;
6120 em
->block_start
= bytenr
;
6121 em
->block_len
= em
->orig_block_len
;
6123 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
6124 em
->block_start
= bytenr
;
6125 em
->block_len
= em
->len
;
6126 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
6127 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6130 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6134 size_t extent_offset
;
6137 em
->block_start
= EXTENT_MAP_INLINE
;
6138 if (!page
|| create
) {
6139 em
->start
= extent_start
;
6140 em
->len
= extent_end
- extent_start
;
6144 size
= btrfs_file_extent_inline_len(leaf
, item
);
6145 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6146 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6147 size
- extent_offset
);
6148 em
->start
= extent_start
+ extent_offset
;
6149 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6150 em
->orig_block_len
= em
->len
;
6151 em
->orig_start
= em
->start
;
6152 if (compress_type
) {
6153 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6154 em
->compress_type
= compress_type
;
6156 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6157 if (create
== 0 && !PageUptodate(page
)) {
6158 if (btrfs_file_extent_compression(leaf
, item
) !=
6159 BTRFS_COMPRESS_NONE
) {
6160 ret
= uncompress_inline(path
, inode
, page
,
6162 extent_offset
, item
);
6163 BUG_ON(ret
); /* -ENOMEM */
6166 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6168 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6169 memset(map
+ pg_offset
+ copy_size
, 0,
6170 PAGE_CACHE_SIZE
- pg_offset
-
6175 flush_dcache_page(page
);
6176 } else if (create
&& PageUptodate(page
)) {
6180 free_extent_map(em
);
6183 btrfs_release_path(path
);
6184 trans
= btrfs_join_transaction(root
);
6187 return ERR_CAST(trans
);
6191 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6194 btrfs_mark_buffer_dirty(leaf
);
6196 set_extent_uptodate(io_tree
, em
->start
,
6197 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6200 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
6204 em
->orig_start
= start
;
6207 em
->block_start
= EXTENT_MAP_HOLE
;
6208 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6210 btrfs_release_path(path
);
6211 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6212 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6213 em
->start
, em
->len
, start
, len
);
6219 write_lock(&em_tree
->lock
);
6220 ret
= add_extent_mapping(em_tree
, em
, 0);
6221 /* it is possible that someone inserted the extent into the tree
6222 * while we had the lock dropped. It is also possible that
6223 * an overlapping map exists in the tree
6225 if (ret
== -EEXIST
) {
6226 struct extent_map
*existing
;
6230 existing
= lookup_extent_mapping(em_tree
, start
, len
);
6231 if (existing
&& (existing
->start
> start
||
6232 existing
->start
+ existing
->len
<= start
)) {
6233 free_extent_map(existing
);
6237 existing
= lookup_extent_mapping(em_tree
, em
->start
,
6240 err
= merge_extent_mapping(em_tree
, existing
,
6243 free_extent_map(existing
);
6245 free_extent_map(em
);
6250 free_extent_map(em
);
6254 free_extent_map(em
);
6259 write_unlock(&em_tree
->lock
);
6262 trace_btrfs_get_extent(root
, em
);
6265 btrfs_free_path(path
);
6267 ret
= btrfs_end_transaction(trans
, root
);
6272 free_extent_map(em
);
6273 return ERR_PTR(err
);
6275 BUG_ON(!em
); /* Error is always set */
6279 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6280 size_t pg_offset
, u64 start
, u64 len
,
6283 struct extent_map
*em
;
6284 struct extent_map
*hole_em
= NULL
;
6285 u64 range_start
= start
;
6291 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6298 * - a pre-alloc extent,
6299 * there might actually be delalloc bytes behind it.
6301 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6302 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6308 /* check to see if we've wrapped (len == -1 or similar) */
6317 /* ok, we didn't find anything, lets look for delalloc */
6318 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6319 end
, len
, EXTENT_DELALLOC
, 1);
6320 found_end
= range_start
+ found
;
6321 if (found_end
< range_start
)
6322 found_end
= (u64
)-1;
6325 * we didn't find anything useful, return
6326 * the original results from get_extent()
6328 if (range_start
> end
|| found_end
<= start
) {
6334 /* adjust the range_start to make sure it doesn't
6335 * go backwards from the start they passed in
6337 range_start
= max(start
, range_start
);
6338 found
= found_end
- range_start
;
6341 u64 hole_start
= start
;
6344 em
= alloc_extent_map();
6350 * when btrfs_get_extent can't find anything it
6351 * returns one huge hole
6353 * make sure what it found really fits our range, and
6354 * adjust to make sure it is based on the start from
6358 u64 calc_end
= extent_map_end(hole_em
);
6360 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6361 free_extent_map(hole_em
);
6364 hole_start
= max(hole_em
->start
, start
);
6365 hole_len
= calc_end
- hole_start
;
6369 if (hole_em
&& range_start
> hole_start
) {
6370 /* our hole starts before our delalloc, so we
6371 * have to return just the parts of the hole
6372 * that go until the delalloc starts
6374 em
->len
= min(hole_len
,
6375 range_start
- hole_start
);
6376 em
->start
= hole_start
;
6377 em
->orig_start
= hole_start
;
6379 * don't adjust block start at all,
6380 * it is fixed at EXTENT_MAP_HOLE
6382 em
->block_start
= hole_em
->block_start
;
6383 em
->block_len
= hole_len
;
6384 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6385 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6387 em
->start
= range_start
;
6389 em
->orig_start
= range_start
;
6390 em
->block_start
= EXTENT_MAP_DELALLOC
;
6391 em
->block_len
= found
;
6393 } else if (hole_em
) {
6398 free_extent_map(hole_em
);
6400 free_extent_map(em
);
6401 return ERR_PTR(err
);
6406 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6409 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6410 struct extent_map
*em
;
6411 struct btrfs_key ins
;
6415 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6416 ret
= btrfs_reserve_extent(root
, len
, root
->sectorsize
, 0,
6417 alloc_hint
, &ins
, 1);
6419 return ERR_PTR(ret
);
6421 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6422 ins
.offset
, ins
.offset
, ins
.offset
, 0);
6424 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6428 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6429 ins
.offset
, ins
.offset
, 0);
6431 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6432 free_extent_map(em
);
6433 return ERR_PTR(ret
);
6440 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6441 * block must be cow'd
6443 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
6444 u64
*orig_start
, u64
*orig_block_len
,
6447 struct btrfs_trans_handle
*trans
;
6448 struct btrfs_path
*path
;
6450 struct extent_buffer
*leaf
;
6451 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6452 struct btrfs_file_extent_item
*fi
;
6453 struct btrfs_key key
;
6460 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
6461 path
= btrfs_alloc_path();
6465 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, btrfs_ino(inode
),
6470 slot
= path
->slots
[0];
6473 /* can't find the item, must cow */
6480 leaf
= path
->nodes
[0];
6481 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6482 if (key
.objectid
!= btrfs_ino(inode
) ||
6483 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6484 /* not our file or wrong item type, must cow */
6488 if (key
.offset
> offset
) {
6489 /* Wrong offset, must cow */
6493 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6494 found_type
= btrfs_file_extent_type(leaf
, fi
);
6495 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6496 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6497 /* not a regular extent, must cow */
6501 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
6504 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6505 if (disk_bytenr
== 0)
6508 if (btrfs_file_extent_compression(leaf
, fi
) ||
6509 btrfs_file_extent_encryption(leaf
, fi
) ||
6510 btrfs_file_extent_other_encoding(leaf
, fi
))
6513 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6516 *orig_start
= key
.offset
- backref_offset
;
6517 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
6518 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
6521 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6523 if (btrfs_extent_readonly(root
, disk_bytenr
))
6525 btrfs_release_path(path
);
6528 * look for other files referencing this extent, if we
6529 * find any we must cow
6531 trans
= btrfs_join_transaction(root
);
6532 if (IS_ERR(trans
)) {
6537 ret
= btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
6538 key
.offset
- backref_offset
, disk_bytenr
);
6539 btrfs_end_transaction(trans
, root
);
6546 * adjust disk_bytenr and num_bytes to cover just the bytes
6547 * in this extent we are about to write. If there
6548 * are any csums in that range we have to cow in order
6549 * to keep the csums correct
6551 disk_bytenr
+= backref_offset
;
6552 disk_bytenr
+= offset
- key
.offset
;
6553 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
6554 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
6557 * all of the above have passed, it is safe to overwrite this extent
6563 btrfs_free_path(path
);
6567 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
6568 struct extent_state
**cached_state
, int writing
)
6570 struct btrfs_ordered_extent
*ordered
;
6574 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6577 * We're concerned with the entire range that we're going to be
6578 * doing DIO to, so we need to make sure theres no ordered
6579 * extents in this range.
6581 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6582 lockend
- lockstart
+ 1);
6585 * We need to make sure there are no buffered pages in this
6586 * range either, we could have raced between the invalidate in
6587 * generic_file_direct_write and locking the extent. The
6588 * invalidate needs to happen so that reads after a write do not
6591 if (!ordered
&& (!writing
||
6592 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
6593 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
6597 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6598 cached_state
, GFP_NOFS
);
6601 btrfs_start_ordered_extent(inode
, ordered
, 1);
6602 btrfs_put_ordered_extent(ordered
);
6604 /* Screw you mmap */
6605 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
6612 * If we found a page that couldn't be invalidated just
6613 * fall back to buffered.
6615 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6616 lockstart
>> PAGE_CACHE_SHIFT
,
6617 lockend
>> PAGE_CACHE_SHIFT
);
6628 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
6629 u64 len
, u64 orig_start
,
6630 u64 block_start
, u64 block_len
,
6631 u64 orig_block_len
, u64 ram_bytes
,
6634 struct extent_map_tree
*em_tree
;
6635 struct extent_map
*em
;
6636 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6639 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6640 em
= alloc_extent_map();
6642 return ERR_PTR(-ENOMEM
);
6645 em
->orig_start
= orig_start
;
6646 em
->mod_start
= start
;
6649 em
->block_len
= block_len
;
6650 em
->block_start
= block_start
;
6651 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6652 em
->orig_block_len
= orig_block_len
;
6653 em
->ram_bytes
= ram_bytes
;
6654 em
->generation
= -1;
6655 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6656 if (type
== BTRFS_ORDERED_PREALLOC
)
6657 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6660 btrfs_drop_extent_cache(inode
, em
->start
,
6661 em
->start
+ em
->len
- 1, 0);
6662 write_lock(&em_tree
->lock
);
6663 ret
= add_extent_mapping(em_tree
, em
, 1);
6664 write_unlock(&em_tree
->lock
);
6665 } while (ret
== -EEXIST
);
6668 free_extent_map(em
);
6669 return ERR_PTR(ret
);
6676 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6677 struct buffer_head
*bh_result
, int create
)
6679 struct extent_map
*em
;
6680 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6681 struct extent_state
*cached_state
= NULL
;
6682 u64 start
= iblock
<< inode
->i_blkbits
;
6683 u64 lockstart
, lockend
;
6684 u64 len
= bh_result
->b_size
;
6685 int unlock_bits
= EXTENT_LOCKED
;
6689 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6691 len
= min_t(u64
, len
, root
->sectorsize
);
6694 lockend
= start
+ len
- 1;
6697 * If this errors out it's because we couldn't invalidate pagecache for
6698 * this range and we need to fallback to buffered.
6700 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6703 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6710 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6711 * io. INLINE is special, and we could probably kludge it in here, but
6712 * it's still buffered so for safety lets just fall back to the generic
6715 * For COMPRESSED we _have_ to read the entire extent in so we can
6716 * decompress it, so there will be buffering required no matter what we
6717 * do, so go ahead and fallback to buffered.
6719 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6720 * to buffered IO. Don't blame me, this is the price we pay for using
6723 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6724 em
->block_start
== EXTENT_MAP_INLINE
) {
6725 free_extent_map(em
);
6730 /* Just a good old fashioned hole, return */
6731 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6732 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6733 free_extent_map(em
);
6738 * We don't allocate a new extent in the following cases
6740 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6742 * 2) The extent is marked as PREALLOC. We're good to go here and can
6743 * just use the extent.
6747 len
= min(len
, em
->len
- (start
- em
->start
));
6748 lockstart
= start
+ len
;
6752 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6753 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6754 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6757 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
6759 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6760 type
= BTRFS_ORDERED_PREALLOC
;
6762 type
= BTRFS_ORDERED_NOCOW
;
6763 len
= min(len
, em
->len
- (start
- em
->start
));
6764 block_start
= em
->block_start
+ (start
- em
->start
);
6766 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
6767 &orig_block_len
, &ram_bytes
) == 1) {
6768 if (type
== BTRFS_ORDERED_PREALLOC
) {
6769 free_extent_map(em
);
6770 em
= create_pinned_em(inode
, start
, len
,
6779 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6780 block_start
, len
, len
, type
);
6782 free_extent_map(em
);
6790 * this will cow the extent, reset the len in case we changed
6793 len
= bh_result
->b_size
;
6794 free_extent_map(em
);
6795 em
= btrfs_new_extent_direct(inode
, start
, len
);
6800 len
= min(len
, em
->len
- (start
- em
->start
));
6802 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6804 bh_result
->b_size
= len
;
6805 bh_result
->b_bdev
= em
->bdev
;
6806 set_buffer_mapped(bh_result
);
6808 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6809 set_buffer_new(bh_result
);
6812 * Need to update the i_size under the extent lock so buffered
6813 * readers will get the updated i_size when we unlock.
6815 if (start
+ len
> i_size_read(inode
))
6816 i_size_write(inode
, start
+ len
);
6818 spin_lock(&BTRFS_I(inode
)->lock
);
6819 BTRFS_I(inode
)->outstanding_extents
++;
6820 spin_unlock(&BTRFS_I(inode
)->lock
);
6822 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6823 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
6824 &cached_state
, GFP_NOFS
);
6829 * In the case of write we need to clear and unlock the entire range,
6830 * in the case of read we need to unlock only the end area that we
6831 * aren't using if there is any left over space.
6833 if (lockstart
< lockend
) {
6834 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6835 lockend
, unlock_bits
, 1, 0,
6836 &cached_state
, GFP_NOFS
);
6838 free_extent_state(cached_state
);
6841 free_extent_map(em
);
6846 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6847 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6851 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6853 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6854 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6855 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6856 struct inode
*inode
= dip
->inode
;
6857 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6858 struct bio
*dio_bio
;
6859 u32
*csums
= (u32
*)dip
->csum
;
6863 start
= dip
->logical_offset
;
6865 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6866 struct page
*page
= bvec
->bv_page
;
6869 unsigned long flags
;
6871 local_irq_save(flags
);
6872 kaddr
= kmap_atomic(page
);
6873 csum
= btrfs_csum_data(kaddr
+ bvec
->bv_offset
,
6874 csum
, bvec
->bv_len
);
6875 btrfs_csum_final(csum
, (char *)&csum
);
6876 kunmap_atomic(kaddr
);
6877 local_irq_restore(flags
);
6879 flush_dcache_page(bvec
->bv_page
);
6880 if (csum
!= csums
[index
]) {
6881 btrfs_err(root
->fs_info
, "csum failed ino %llu off %llu csum %u expected csum %u",
6882 btrfs_ino(inode
), start
, csum
,
6888 start
+= bvec
->bv_len
;
6891 } while (bvec
<= bvec_end
);
6893 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6894 dip
->logical_offset
+ dip
->bytes
- 1);
6895 dio_bio
= dip
->dio_bio
;
6899 /* If we had a csum failure make sure to clear the uptodate flag */
6901 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
6902 dio_end_io(dio_bio
, err
);
6906 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6908 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6909 struct inode
*inode
= dip
->inode
;
6910 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6911 struct btrfs_ordered_extent
*ordered
= NULL
;
6912 u64 ordered_offset
= dip
->logical_offset
;
6913 u64 ordered_bytes
= dip
->bytes
;
6914 struct bio
*dio_bio
;
6920 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
6922 ordered_bytes
, !err
);
6926 ordered
->work
.func
= finish_ordered_fn
;
6927 ordered
->work
.flags
= 0;
6928 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
6932 * our bio might span multiple ordered extents. If we haven't
6933 * completed the accounting for the whole dio, go back and try again
6935 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
6936 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
6942 dio_bio
= dip
->dio_bio
;
6946 /* If we had an error make sure to clear the uptodate flag */
6948 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
6949 dio_end_io(dio_bio
, err
);
6953 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
6954 struct bio
*bio
, int mirror_num
,
6955 unsigned long bio_flags
, u64 offset
)
6958 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6959 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
6960 BUG_ON(ret
); /* -ENOMEM */
6964 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
6966 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6969 btrfs_err(BTRFS_I(dip
->inode
)->root
->fs_info
,
6970 "direct IO failed ino %llu rw %lu sector %#Lx len %u err no %d",
6971 btrfs_ino(dip
->inode
), bio
->bi_rw
,
6972 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
6976 * before atomic variable goto zero, we must make sure
6977 * dip->errors is perceived to be set.
6979 smp_mb__before_atomic_dec();
6982 /* if there are more bios still pending for this dio, just exit */
6983 if (!atomic_dec_and_test(&dip
->pending_bios
))
6987 bio_io_error(dip
->orig_bio
);
6989 set_bit(BIO_UPTODATE
, &dip
->dio_bio
->bi_flags
);
6990 bio_endio(dip
->orig_bio
, 0);
6996 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
6997 u64 first_sector
, gfp_t gfp_flags
)
6999 int nr_vecs
= bio_get_nr_vecs(bdev
);
7000 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
7003 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
7004 int rw
, u64 file_offset
, int skip_sum
,
7007 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7008 int write
= rw
& REQ_WRITE
;
7009 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7013 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
7018 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
7026 if (write
&& async_submit
) {
7027 ret
= btrfs_wq_submit_bio(root
->fs_info
,
7028 inode
, rw
, bio
, 0, 0,
7030 __btrfs_submit_bio_start_direct_io
,
7031 __btrfs_submit_bio_done
);
7035 * If we aren't doing async submit, calculate the csum of the
7038 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
7041 } else if (!skip_sum
) {
7042 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, dip
, bio
,
7049 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
7055 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
7058 struct inode
*inode
= dip
->inode
;
7059 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7061 struct bio
*orig_bio
= dip
->orig_bio
;
7062 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
7063 u64 start_sector
= orig_bio
->bi_sector
;
7064 u64 file_offset
= dip
->logical_offset
;
7069 int async_submit
= 0;
7071 map_length
= orig_bio
->bi_size
;
7072 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
7073 &map_length
, NULL
, 0);
7079 if (map_length
>= orig_bio
->bi_size
) {
7084 /* async crcs make it difficult to collect full stripe writes. */
7085 if (btrfs_get_alloc_profile(root
, 1) &
7086 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
7091 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7094 bio
->bi_private
= dip
;
7095 bio
->bi_end_io
= btrfs_end_dio_bio
;
7096 atomic_inc(&dip
->pending_bios
);
7098 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7099 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
7100 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7101 bvec
->bv_offset
) < bvec
->bv_len
)) {
7103 * inc the count before we submit the bio so
7104 * we know the end IO handler won't happen before
7105 * we inc the count. Otherwise, the dip might get freed
7106 * before we're done setting it up
7108 atomic_inc(&dip
->pending_bios
);
7109 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7110 file_offset
, skip_sum
,
7114 atomic_dec(&dip
->pending_bios
);
7118 start_sector
+= submit_len
>> 9;
7119 file_offset
+= submit_len
;
7124 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7125 start_sector
, GFP_NOFS
);
7128 bio
->bi_private
= dip
;
7129 bio
->bi_end_io
= btrfs_end_dio_bio
;
7131 map_length
= orig_bio
->bi_size
;
7132 ret
= btrfs_map_block(root
->fs_info
, rw
,
7134 &map_length
, NULL
, 0);
7140 submit_len
+= bvec
->bv_len
;
7147 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7156 * before atomic variable goto zero, we must
7157 * make sure dip->errors is perceived to be set.
7159 smp_mb__before_atomic_dec();
7160 if (atomic_dec_and_test(&dip
->pending_bios
))
7161 bio_io_error(dip
->orig_bio
);
7163 /* bio_end_io() will handle error, so we needn't return it */
7167 static void btrfs_submit_direct(int rw
, struct bio
*dio_bio
,
7168 struct inode
*inode
, loff_t file_offset
)
7170 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7171 struct btrfs_dio_private
*dip
;
7175 int write
= rw
& REQ_WRITE
;
7179 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7181 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
7187 if (!skip_sum
&& !write
) {
7188 csum_size
= btrfs_super_csum_size(root
->fs_info
->super_copy
);
7189 sum_len
= dio_bio
->bi_size
>> inode
->i_sb
->s_blocksize_bits
;
7190 sum_len
*= csum_size
;
7195 dip
= kmalloc(sizeof(*dip
) + sum_len
, GFP_NOFS
);
7201 dip
->private = dio_bio
->bi_private
;
7203 dip
->logical_offset
= file_offset
;
7204 dip
->bytes
= dio_bio
->bi_size
;
7205 dip
->disk_bytenr
= (u64
)dio_bio
->bi_sector
<< 9;
7206 io_bio
->bi_private
= dip
;
7208 dip
->orig_bio
= io_bio
;
7209 dip
->dio_bio
= dio_bio
;
7210 atomic_set(&dip
->pending_bios
, 0);
7213 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
7215 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
7217 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
7226 * If this is a write, we need to clean up the reserved space and kill
7227 * the ordered extent.
7230 struct btrfs_ordered_extent
*ordered
;
7231 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
7232 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
7233 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
7234 btrfs_free_reserved_extent(root
, ordered
->start
,
7236 btrfs_put_ordered_extent(ordered
);
7237 btrfs_put_ordered_extent(ordered
);
7239 bio_endio(dio_bio
, ret
);
7242 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
7243 const struct iovec
*iov
, loff_t offset
,
7244 unsigned long nr_segs
)
7250 unsigned blocksize_mask
= root
->sectorsize
- 1;
7251 ssize_t retval
= -EINVAL
;
7252 loff_t end
= offset
;
7254 if (offset
& blocksize_mask
)
7257 /* Check the memory alignment. Blocks cannot straddle pages */
7258 for (seg
= 0; seg
< nr_segs
; seg
++) {
7259 addr
= (unsigned long)iov
[seg
].iov_base
;
7260 size
= iov
[seg
].iov_len
;
7262 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
7265 /* If this is a write we don't need to check anymore */
7270 * Check to make sure we don't have duplicate iov_base's in this
7271 * iovec, if so return EINVAL, otherwise we'll get csum errors
7272 * when reading back.
7274 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
7275 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
7284 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
7285 const struct iovec
*iov
, loff_t offset
,
7286 unsigned long nr_segs
)
7288 struct file
*file
= iocb
->ki_filp
;
7289 struct inode
*inode
= file
->f_mapping
->host
;
7293 bool relock
= false;
7296 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
7300 atomic_inc(&inode
->i_dio_count
);
7301 smp_mb__after_atomic_inc();
7304 * The generic stuff only does filemap_write_and_wait_range, which isn't
7305 * enough if we've written compressed pages to this area, so we need to
7306 * call btrfs_wait_ordered_range to make absolutely sure that any
7307 * outstanding dirty pages are on disk.
7309 count
= iov_length(iov
, nr_segs
);
7310 ret
= btrfs_wait_ordered_range(inode
, offset
, count
);
7316 * If the write DIO is beyond the EOF, we need update
7317 * the isize, but it is protected by i_mutex. So we can
7318 * not unlock the i_mutex at this case.
7320 if (offset
+ count
<= inode
->i_size
) {
7321 mutex_unlock(&inode
->i_mutex
);
7324 ret
= btrfs_delalloc_reserve_space(inode
, count
);
7327 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7328 &BTRFS_I(inode
)->runtime_flags
))) {
7329 inode_dio_done(inode
);
7330 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7334 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
7335 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
7336 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
7337 btrfs_submit_direct
, flags
);
7339 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
7340 btrfs_delalloc_release_space(inode
, count
);
7341 else if (ret
>= 0 && (size_t)ret
< count
)
7342 btrfs_delalloc_release_space(inode
,
7343 count
- (size_t)ret
);
7345 btrfs_delalloc_release_metadata(inode
, 0);
7349 inode_dio_done(inode
);
7351 mutex_lock(&inode
->i_mutex
);
7356 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7358 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
7359 __u64 start
, __u64 len
)
7363 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
7367 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
7370 int btrfs_readpage(struct file
*file
, struct page
*page
)
7372 struct extent_io_tree
*tree
;
7373 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7374 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
7377 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
7379 struct extent_io_tree
*tree
;
7382 if (current
->flags
& PF_MEMALLOC
) {
7383 redirty_page_for_writepage(wbc
, page
);
7387 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7388 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
7391 static int btrfs_writepages(struct address_space
*mapping
,
7392 struct writeback_control
*wbc
)
7394 struct extent_io_tree
*tree
;
7396 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7397 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
7401 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
7402 struct list_head
*pages
, unsigned nr_pages
)
7404 struct extent_io_tree
*tree
;
7405 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7406 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
7409 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7411 struct extent_io_tree
*tree
;
7412 struct extent_map_tree
*map
;
7415 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7416 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
7417 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
7419 ClearPagePrivate(page
);
7420 set_page_private(page
, 0);
7421 page_cache_release(page
);
7426 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7428 if (PageWriteback(page
) || PageDirty(page
))
7430 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
7433 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
7434 unsigned int length
)
7436 struct inode
*inode
= page
->mapping
->host
;
7437 struct extent_io_tree
*tree
;
7438 struct btrfs_ordered_extent
*ordered
;
7439 struct extent_state
*cached_state
= NULL
;
7440 u64 page_start
= page_offset(page
);
7441 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7442 int inode_evicting
= inode
->i_state
& I_FREEING
;
7445 * we have the page locked, so new writeback can't start,
7446 * and the dirty bit won't be cleared while we are here.
7448 * Wait for IO on this page so that we can safely clear
7449 * the PagePrivate2 bit and do ordered accounting
7451 wait_on_page_writeback(page
);
7453 tree
= &BTRFS_I(inode
)->io_tree
;
7455 btrfs_releasepage(page
, GFP_NOFS
);
7459 if (!inode_evicting
)
7460 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7461 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7464 * IO on this page will never be started, so we need
7465 * to account for any ordered extents now
7467 if (!inode_evicting
)
7468 clear_extent_bit(tree
, page_start
, page_end
,
7469 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7470 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
7471 EXTENT_DEFRAG
, 1, 0, &cached_state
,
7474 * whoever cleared the private bit is responsible
7475 * for the finish_ordered_io
7477 if (TestClearPagePrivate2(page
)) {
7478 struct btrfs_ordered_inode_tree
*tree
;
7481 tree
= &BTRFS_I(inode
)->ordered_tree
;
7483 spin_lock_irq(&tree
->lock
);
7484 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
7485 new_len
= page_start
- ordered
->file_offset
;
7486 if (new_len
< ordered
->truncated_len
)
7487 ordered
->truncated_len
= new_len
;
7488 spin_unlock_irq(&tree
->lock
);
7490 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
7492 PAGE_CACHE_SIZE
, 1))
7493 btrfs_finish_ordered_io(ordered
);
7495 btrfs_put_ordered_extent(ordered
);
7496 if (!inode_evicting
) {
7497 cached_state
= NULL
;
7498 lock_extent_bits(tree
, page_start
, page_end
, 0,
7503 if (!inode_evicting
) {
7504 clear_extent_bit(tree
, page_start
, page_end
,
7505 EXTENT_LOCKED
| EXTENT_DIRTY
|
7506 EXTENT_DELALLOC
| EXTENT_DO_ACCOUNTING
|
7507 EXTENT_DEFRAG
, 1, 1,
7508 &cached_state
, GFP_NOFS
);
7510 __btrfs_releasepage(page
, GFP_NOFS
);
7513 ClearPageChecked(page
);
7514 if (PagePrivate(page
)) {
7515 ClearPagePrivate(page
);
7516 set_page_private(page
, 0);
7517 page_cache_release(page
);
7522 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7523 * called from a page fault handler when a page is first dirtied. Hence we must
7524 * be careful to check for EOF conditions here. We set the page up correctly
7525 * for a written page which means we get ENOSPC checking when writing into
7526 * holes and correct delalloc and unwritten extent mapping on filesystems that
7527 * support these features.
7529 * We are not allowed to take the i_mutex here so we have to play games to
7530 * protect against truncate races as the page could now be beyond EOF. Because
7531 * vmtruncate() writes the inode size before removing pages, once we have the
7532 * page lock we can determine safely if the page is beyond EOF. If it is not
7533 * beyond EOF, then the page is guaranteed safe against truncation until we
7536 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
7538 struct page
*page
= vmf
->page
;
7539 struct inode
*inode
= file_inode(vma
->vm_file
);
7540 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7541 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7542 struct btrfs_ordered_extent
*ordered
;
7543 struct extent_state
*cached_state
= NULL
;
7545 unsigned long zero_start
;
7552 sb_start_pagefault(inode
->i_sb
);
7553 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
7555 ret
= file_update_time(vma
->vm_file
);
7561 else /* -ENOSPC, -EIO, etc */
7562 ret
= VM_FAULT_SIGBUS
;
7568 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
7571 size
= i_size_read(inode
);
7572 page_start
= page_offset(page
);
7573 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7575 if ((page
->mapping
!= inode
->i_mapping
) ||
7576 (page_start
>= size
)) {
7577 /* page got truncated out from underneath us */
7580 wait_on_page_writeback(page
);
7582 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
7583 set_page_extent_mapped(page
);
7586 * we can't set the delalloc bits if there are pending ordered
7587 * extents. Drop our locks and wait for them to finish
7589 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7591 unlock_extent_cached(io_tree
, page_start
, page_end
,
7592 &cached_state
, GFP_NOFS
);
7594 btrfs_start_ordered_extent(inode
, ordered
, 1);
7595 btrfs_put_ordered_extent(ordered
);
7600 * XXX - page_mkwrite gets called every time the page is dirtied, even
7601 * if it was already dirty, so for space accounting reasons we need to
7602 * clear any delalloc bits for the range we are fixing to save. There
7603 * is probably a better way to do this, but for now keep consistent with
7604 * prepare_pages in the normal write path.
7606 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
7607 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7608 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
7609 0, 0, &cached_state
, GFP_NOFS
);
7611 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
7614 unlock_extent_cached(io_tree
, page_start
, page_end
,
7615 &cached_state
, GFP_NOFS
);
7616 ret
= VM_FAULT_SIGBUS
;
7621 /* page is wholly or partially inside EOF */
7622 if (page_start
+ PAGE_CACHE_SIZE
> size
)
7623 zero_start
= size
& ~PAGE_CACHE_MASK
;
7625 zero_start
= PAGE_CACHE_SIZE
;
7627 if (zero_start
!= PAGE_CACHE_SIZE
) {
7629 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
7630 flush_dcache_page(page
);
7633 ClearPageChecked(page
);
7634 set_page_dirty(page
);
7635 SetPageUptodate(page
);
7637 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
7638 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
7639 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
7641 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
7645 sb_end_pagefault(inode
->i_sb
);
7646 return VM_FAULT_LOCKED
;
7650 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
7652 sb_end_pagefault(inode
->i_sb
);
7656 static int btrfs_truncate(struct inode
*inode
)
7658 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7659 struct btrfs_block_rsv
*rsv
;
7662 struct btrfs_trans_handle
*trans
;
7663 u64 mask
= root
->sectorsize
- 1;
7664 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7666 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
7672 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7673 * 3 things going on here
7675 * 1) We need to reserve space for our orphan item and the space to
7676 * delete our orphan item. Lord knows we don't want to have a dangling
7677 * orphan item because we didn't reserve space to remove it.
7679 * 2) We need to reserve space to update our inode.
7681 * 3) We need to have something to cache all the space that is going to
7682 * be free'd up by the truncate operation, but also have some slack
7683 * space reserved in case it uses space during the truncate (thank you
7684 * very much snapshotting).
7686 * And we need these to all be seperate. The fact is we can use alot of
7687 * space doing the truncate, and we have no earthly idea how much space
7688 * we will use, so we need the truncate reservation to be seperate so it
7689 * doesn't end up using space reserved for updating the inode or
7690 * removing the orphan item. We also need to be able to stop the
7691 * transaction and start a new one, which means we need to be able to
7692 * update the inode several times, and we have no idea of knowing how
7693 * many times that will be, so we can't just reserve 1 item for the
7694 * entirety of the opration, so that has to be done seperately as well.
7695 * Then there is the orphan item, which does indeed need to be held on
7696 * to for the whole operation, and we need nobody to touch this reserved
7697 * space except the orphan code.
7699 * So that leaves us with
7701 * 1) root->orphan_block_rsv - for the orphan deletion.
7702 * 2) rsv - for the truncate reservation, which we will steal from the
7703 * transaction reservation.
7704 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7705 * updating the inode.
7707 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7710 rsv
->size
= min_size
;
7714 * 1 for the truncate slack space
7715 * 1 for updating the inode.
7717 trans
= btrfs_start_transaction(root
, 2);
7718 if (IS_ERR(trans
)) {
7719 err
= PTR_ERR(trans
);
7723 /* Migrate the slack space for the truncate to our reserve */
7724 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7729 * setattr is responsible for setting the ordered_data_close flag,
7730 * but that is only tested during the last file release. That
7731 * could happen well after the next commit, leaving a great big
7732 * window where new writes may get lost if someone chooses to write
7733 * to this file after truncating to zero
7735 * The inode doesn't have any dirty data here, and so if we commit
7736 * this is a noop. If someone immediately starts writing to the inode
7737 * it is very likely we'll catch some of their writes in this
7738 * transaction, and the commit will find this file on the ordered
7739 * data list with good things to send down.
7741 * This is a best effort solution, there is still a window where
7742 * using truncate to replace the contents of the file will
7743 * end up with a zero length file after a crash.
7745 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7746 &BTRFS_I(inode
)->runtime_flags
))
7747 btrfs_add_ordered_operation(trans
, root
, inode
);
7750 * So if we truncate and then write and fsync we normally would just
7751 * write the extents that changed, which is a problem if we need to
7752 * first truncate that entire inode. So set this flag so we write out
7753 * all of the extents in the inode to the sync log so we're completely
7756 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7757 trans
->block_rsv
= rsv
;
7760 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7762 BTRFS_EXTENT_DATA_KEY
);
7763 if (ret
!= -ENOSPC
) {
7768 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7769 ret
= btrfs_update_inode(trans
, root
, inode
);
7775 btrfs_end_transaction(trans
, root
);
7776 btrfs_btree_balance_dirty(root
);
7778 trans
= btrfs_start_transaction(root
, 2);
7779 if (IS_ERR(trans
)) {
7780 ret
= err
= PTR_ERR(trans
);
7785 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7787 BUG_ON(ret
); /* shouldn't happen */
7788 trans
->block_rsv
= rsv
;
7791 if (ret
== 0 && inode
->i_nlink
> 0) {
7792 trans
->block_rsv
= root
->orphan_block_rsv
;
7793 ret
= btrfs_orphan_del(trans
, inode
);
7799 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7800 ret
= btrfs_update_inode(trans
, root
, inode
);
7804 ret
= btrfs_end_transaction(trans
, root
);
7805 btrfs_btree_balance_dirty(root
);
7809 btrfs_free_block_rsv(root
, rsv
);
7818 * create a new subvolume directory/inode (helper for the ioctl).
7820 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7821 struct btrfs_root
*new_root
, u64 new_dirid
)
7823 struct inode
*inode
;
7827 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7828 new_dirid
, new_dirid
,
7829 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7832 return PTR_ERR(inode
);
7833 inode
->i_op
= &btrfs_dir_inode_operations
;
7834 inode
->i_fop
= &btrfs_dir_file_operations
;
7836 set_nlink(inode
, 1);
7837 btrfs_i_size_write(inode
, 0);
7839 err
= btrfs_update_inode(trans
, new_root
, inode
);
7845 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7847 struct btrfs_inode
*ei
;
7848 struct inode
*inode
;
7850 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7857 ei
->last_sub_trans
= 0;
7858 ei
->logged_trans
= 0;
7859 ei
->delalloc_bytes
= 0;
7860 ei
->disk_i_size
= 0;
7863 ei
->index_cnt
= (u64
)-1;
7864 ei
->last_unlink_trans
= 0;
7865 ei
->last_log_commit
= 0;
7867 spin_lock_init(&ei
->lock
);
7868 ei
->outstanding_extents
= 0;
7869 ei
->reserved_extents
= 0;
7871 ei
->runtime_flags
= 0;
7872 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7874 ei
->delayed_node
= NULL
;
7876 inode
= &ei
->vfs_inode
;
7877 extent_map_tree_init(&ei
->extent_tree
);
7878 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7879 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7880 ei
->io_tree
.track_uptodate
= 1;
7881 ei
->io_failure_tree
.track_uptodate
= 1;
7882 atomic_set(&ei
->sync_writers
, 0);
7883 mutex_init(&ei
->log_mutex
);
7884 mutex_init(&ei
->delalloc_mutex
);
7885 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7886 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7887 INIT_LIST_HEAD(&ei
->ordered_operations
);
7888 RB_CLEAR_NODE(&ei
->rb_node
);
7893 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
7894 void btrfs_test_destroy_inode(struct inode
*inode
)
7896 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7897 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7901 static void btrfs_i_callback(struct rcu_head
*head
)
7903 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7904 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7907 void btrfs_destroy_inode(struct inode
*inode
)
7909 struct btrfs_ordered_extent
*ordered
;
7910 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7912 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7913 WARN_ON(inode
->i_data
.nrpages
);
7914 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7915 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7916 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7917 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7920 * This can happen where we create an inode, but somebody else also
7921 * created the same inode and we need to destroy the one we already
7928 * Make sure we're properly removed from the ordered operation
7932 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7933 spin_lock(&root
->fs_info
->ordered_root_lock
);
7934 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7935 spin_unlock(&root
->fs_info
->ordered_root_lock
);
7938 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7939 &BTRFS_I(inode
)->runtime_flags
)) {
7940 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
7942 atomic_dec(&root
->orphan_inodes
);
7946 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7950 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
7951 ordered
->file_offset
, ordered
->len
);
7952 btrfs_remove_ordered_extent(inode
, ordered
);
7953 btrfs_put_ordered_extent(ordered
);
7954 btrfs_put_ordered_extent(ordered
);
7957 inode_tree_del(inode
);
7958 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7960 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7963 int btrfs_drop_inode(struct inode
*inode
)
7965 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7970 /* the snap/subvol tree is on deleting */
7971 if (btrfs_root_refs(&root
->root_item
) == 0)
7974 return generic_drop_inode(inode
);
7977 static void init_once(void *foo
)
7979 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7981 inode_init_once(&ei
->vfs_inode
);
7984 void btrfs_destroy_cachep(void)
7987 * Make sure all delayed rcu free inodes are flushed before we
7991 if (btrfs_inode_cachep
)
7992 kmem_cache_destroy(btrfs_inode_cachep
);
7993 if (btrfs_trans_handle_cachep
)
7994 kmem_cache_destroy(btrfs_trans_handle_cachep
);
7995 if (btrfs_transaction_cachep
)
7996 kmem_cache_destroy(btrfs_transaction_cachep
);
7997 if (btrfs_path_cachep
)
7998 kmem_cache_destroy(btrfs_path_cachep
);
7999 if (btrfs_free_space_cachep
)
8000 kmem_cache_destroy(btrfs_free_space_cachep
);
8001 if (btrfs_delalloc_work_cachep
)
8002 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
8005 int btrfs_init_cachep(void)
8007 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
8008 sizeof(struct btrfs_inode
), 0,
8009 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
8010 if (!btrfs_inode_cachep
)
8013 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
8014 sizeof(struct btrfs_trans_handle
), 0,
8015 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8016 if (!btrfs_trans_handle_cachep
)
8019 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
8020 sizeof(struct btrfs_transaction
), 0,
8021 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8022 if (!btrfs_transaction_cachep
)
8025 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
8026 sizeof(struct btrfs_path
), 0,
8027 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8028 if (!btrfs_path_cachep
)
8031 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
8032 sizeof(struct btrfs_free_space
), 0,
8033 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8034 if (!btrfs_free_space_cachep
)
8037 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
8038 sizeof(struct btrfs_delalloc_work
), 0,
8039 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
8041 if (!btrfs_delalloc_work_cachep
)
8046 btrfs_destroy_cachep();
8050 static int btrfs_getattr(struct vfsmount
*mnt
,
8051 struct dentry
*dentry
, struct kstat
*stat
)
8054 struct inode
*inode
= dentry
->d_inode
;
8055 u32 blocksize
= inode
->i_sb
->s_blocksize
;
8057 generic_fillattr(inode
, stat
);
8058 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
8059 stat
->blksize
= PAGE_CACHE_SIZE
;
8061 spin_lock(&BTRFS_I(inode
)->lock
);
8062 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
8063 spin_unlock(&BTRFS_I(inode
)->lock
);
8064 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
8065 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
8069 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
8070 struct inode
*new_dir
, struct dentry
*new_dentry
)
8072 struct btrfs_trans_handle
*trans
;
8073 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
8074 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
8075 struct inode
*new_inode
= new_dentry
->d_inode
;
8076 struct inode
*old_inode
= old_dentry
->d_inode
;
8077 struct timespec ctime
= CURRENT_TIME
;
8081 u64 old_ino
= btrfs_ino(old_inode
);
8083 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
8086 /* we only allow rename subvolume link between subvolumes */
8087 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8090 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
8091 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
8094 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
8095 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
8099 /* check for collisions, even if the name isn't there */
8100 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
8101 new_dentry
->d_name
.name
,
8102 new_dentry
->d_name
.len
);
8105 if (ret
== -EEXIST
) {
8107 * eexist without a new_inode */
8108 if (WARN_ON(!new_inode
)) {
8112 /* maybe -EOVERFLOW */
8119 * we're using rename to replace one file with another.
8120 * and the replacement file is large. Start IO on it now so
8121 * we don't add too much work to the end of the transaction
8123 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
8124 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
8125 filemap_flush(old_inode
->i_mapping
);
8127 /* close the racy window with snapshot create/destroy ioctl */
8128 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8129 down_read(&root
->fs_info
->subvol_sem
);
8131 * We want to reserve the absolute worst case amount of items. So if
8132 * both inodes are subvols and we need to unlink them then that would
8133 * require 4 item modifications, but if they are both normal inodes it
8134 * would require 5 item modifications, so we'll assume their normal
8135 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8136 * should cover the worst case number of items we'll modify.
8138 trans
= btrfs_start_transaction(root
, 11);
8139 if (IS_ERR(trans
)) {
8140 ret
= PTR_ERR(trans
);
8145 btrfs_record_root_in_trans(trans
, dest
);
8147 ret
= btrfs_set_inode_index(new_dir
, &index
);
8151 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8152 /* force full log commit if subvolume involved. */
8153 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8155 ret
= btrfs_insert_inode_ref(trans
, dest
,
8156 new_dentry
->d_name
.name
,
8157 new_dentry
->d_name
.len
,
8159 btrfs_ino(new_dir
), index
);
8163 * this is an ugly little race, but the rename is required
8164 * to make sure that if we crash, the inode is either at the
8165 * old name or the new one. pinning the log transaction lets
8166 * us make sure we don't allow a log commit to come in after
8167 * we unlink the name but before we add the new name back in.
8169 btrfs_pin_log_trans(root
);
8172 * make sure the inode gets flushed if it is replacing
8175 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
8176 btrfs_add_ordered_operation(trans
, root
, old_inode
);
8178 inode_inc_iversion(old_dir
);
8179 inode_inc_iversion(new_dir
);
8180 inode_inc_iversion(old_inode
);
8181 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8182 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8183 old_inode
->i_ctime
= ctime
;
8185 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8186 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8188 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8189 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8190 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8191 old_dentry
->d_name
.name
,
8192 old_dentry
->d_name
.len
);
8194 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8195 old_dentry
->d_inode
,
8196 old_dentry
->d_name
.name
,
8197 old_dentry
->d_name
.len
);
8199 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8202 btrfs_abort_transaction(trans
, root
, ret
);
8207 inode_inc_iversion(new_inode
);
8208 new_inode
->i_ctime
= CURRENT_TIME
;
8209 if (unlikely(btrfs_ino(new_inode
) ==
8210 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8211 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8212 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
8214 new_dentry
->d_name
.name
,
8215 new_dentry
->d_name
.len
);
8216 BUG_ON(new_inode
->i_nlink
== 0);
8218 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
8219 new_dentry
->d_inode
,
8220 new_dentry
->d_name
.name
,
8221 new_dentry
->d_name
.len
);
8223 if (!ret
&& new_inode
->i_nlink
== 0)
8224 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
8226 btrfs_abort_transaction(trans
, root
, ret
);
8231 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
8232 new_dentry
->d_name
.name
,
8233 new_dentry
->d_name
.len
, 0, index
);
8235 btrfs_abort_transaction(trans
, root
, ret
);
8239 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
8240 struct dentry
*parent
= new_dentry
->d_parent
;
8241 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
8242 btrfs_end_log_trans(root
);
8245 btrfs_end_transaction(trans
, root
);
8247 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8248 up_read(&root
->fs_info
->subvol_sem
);
8253 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
8255 struct btrfs_delalloc_work
*delalloc_work
;
8256 struct inode
*inode
;
8258 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
8260 inode
= delalloc_work
->inode
;
8261 if (delalloc_work
->wait
) {
8262 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
8264 filemap_flush(inode
->i_mapping
);
8265 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
8266 &BTRFS_I(inode
)->runtime_flags
))
8267 filemap_flush(inode
->i_mapping
);
8270 if (delalloc_work
->delay_iput
)
8271 btrfs_add_delayed_iput(inode
);
8274 complete(&delalloc_work
->completion
);
8277 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
8278 int wait
, int delay_iput
)
8280 struct btrfs_delalloc_work
*work
;
8282 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
8286 init_completion(&work
->completion
);
8287 INIT_LIST_HEAD(&work
->list
);
8288 work
->inode
= inode
;
8290 work
->delay_iput
= delay_iput
;
8291 work
->work
.func
= btrfs_run_delalloc_work
;
8296 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
8298 wait_for_completion(&work
->completion
);
8299 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
8303 * some fairly slow code that needs optimization. This walks the list
8304 * of all the inodes with pending delalloc and forces them to disk.
8306 static int __start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8308 struct btrfs_inode
*binode
;
8309 struct inode
*inode
;
8310 struct btrfs_delalloc_work
*work
, *next
;
8311 struct list_head works
;
8312 struct list_head splice
;
8315 INIT_LIST_HEAD(&works
);
8316 INIT_LIST_HEAD(&splice
);
8318 spin_lock(&root
->delalloc_lock
);
8319 list_splice_init(&root
->delalloc_inodes
, &splice
);
8320 while (!list_empty(&splice
)) {
8321 binode
= list_entry(splice
.next
, struct btrfs_inode
,
8324 list_move_tail(&binode
->delalloc_inodes
,
8325 &root
->delalloc_inodes
);
8326 inode
= igrab(&binode
->vfs_inode
);
8328 cond_resched_lock(&root
->delalloc_lock
);
8331 spin_unlock(&root
->delalloc_lock
);
8333 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
8334 if (unlikely(!work
)) {
8336 btrfs_add_delayed_iput(inode
);
8342 list_add_tail(&work
->list
, &works
);
8343 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
8347 spin_lock(&root
->delalloc_lock
);
8349 spin_unlock(&root
->delalloc_lock
);
8351 list_for_each_entry_safe(work
, next
, &works
, list
) {
8352 list_del_init(&work
->list
);
8353 btrfs_wait_and_free_delalloc_work(work
);
8357 list_for_each_entry_safe(work
, next
, &works
, list
) {
8358 list_del_init(&work
->list
);
8359 btrfs_wait_and_free_delalloc_work(work
);
8362 if (!list_empty_careful(&splice
)) {
8363 spin_lock(&root
->delalloc_lock
);
8364 list_splice_tail(&splice
, &root
->delalloc_inodes
);
8365 spin_unlock(&root
->delalloc_lock
);
8370 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8374 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
8377 ret
= __start_delalloc_inodes(root
, delay_iput
);
8379 * the filemap_flush will queue IO into the worker threads, but
8380 * we have to make sure the IO is actually started and that
8381 * ordered extents get created before we return
8383 atomic_inc(&root
->fs_info
->async_submit_draining
);
8384 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
8385 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
8386 wait_event(root
->fs_info
->async_submit_wait
,
8387 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
8388 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
8390 atomic_dec(&root
->fs_info
->async_submit_draining
);
8394 int btrfs_start_delalloc_roots(struct btrfs_fs_info
*fs_info
, int delay_iput
)
8396 struct btrfs_root
*root
;
8397 struct list_head splice
;
8400 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
8403 INIT_LIST_HEAD(&splice
);
8405 spin_lock(&fs_info
->delalloc_root_lock
);
8406 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
8407 while (!list_empty(&splice
)) {
8408 root
= list_first_entry(&splice
, struct btrfs_root
,
8410 root
= btrfs_grab_fs_root(root
);
8412 list_move_tail(&root
->delalloc_root
,
8413 &fs_info
->delalloc_roots
);
8414 spin_unlock(&fs_info
->delalloc_root_lock
);
8416 ret
= __start_delalloc_inodes(root
, delay_iput
);
8417 btrfs_put_fs_root(root
);
8421 spin_lock(&fs_info
->delalloc_root_lock
);
8423 spin_unlock(&fs_info
->delalloc_root_lock
);
8425 atomic_inc(&fs_info
->async_submit_draining
);
8426 while (atomic_read(&fs_info
->nr_async_submits
) ||
8427 atomic_read(&fs_info
->async_delalloc_pages
)) {
8428 wait_event(fs_info
->async_submit_wait
,
8429 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
8430 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
8432 atomic_dec(&fs_info
->async_submit_draining
);
8435 if (!list_empty_careful(&splice
)) {
8436 spin_lock(&fs_info
->delalloc_root_lock
);
8437 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
8438 spin_unlock(&fs_info
->delalloc_root_lock
);
8443 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
8444 const char *symname
)
8446 struct btrfs_trans_handle
*trans
;
8447 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8448 struct btrfs_path
*path
;
8449 struct btrfs_key key
;
8450 struct inode
*inode
= NULL
;
8458 struct btrfs_file_extent_item
*ei
;
8459 struct extent_buffer
*leaf
;
8461 name_len
= strlen(symname
);
8462 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
8463 return -ENAMETOOLONG
;
8466 * 2 items for inode item and ref
8467 * 2 items for dir items
8468 * 1 item for xattr if selinux is on
8470 trans
= btrfs_start_transaction(root
, 5);
8472 return PTR_ERR(trans
);
8474 err
= btrfs_find_free_ino(root
, &objectid
);
8478 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
8479 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
8480 S_IFLNK
|S_IRWXUGO
, &index
);
8481 if (IS_ERR(inode
)) {
8482 err
= PTR_ERR(inode
);
8486 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
8493 * If the active LSM wants to access the inode during
8494 * d_instantiate it needs these. Smack checks to see
8495 * if the filesystem supports xattrs by looking at the
8498 inode
->i_fop
= &btrfs_file_operations
;
8499 inode
->i_op
= &btrfs_file_inode_operations
;
8501 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
8505 inode
->i_mapping
->a_ops
= &btrfs_aops
;
8506 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8507 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
8512 path
= btrfs_alloc_path();
8518 key
.objectid
= btrfs_ino(inode
);
8520 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
8521 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
8522 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
8526 btrfs_free_path(path
);
8529 leaf
= path
->nodes
[0];
8530 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
8531 struct btrfs_file_extent_item
);
8532 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
8533 btrfs_set_file_extent_type(leaf
, ei
,
8534 BTRFS_FILE_EXTENT_INLINE
);
8535 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
8536 btrfs_set_file_extent_compression(leaf
, ei
, 0);
8537 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
8538 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
8540 ptr
= btrfs_file_extent_inline_start(ei
);
8541 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
8542 btrfs_mark_buffer_dirty(leaf
);
8543 btrfs_free_path(path
);
8545 inode
->i_op
= &btrfs_symlink_inode_operations
;
8546 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
8547 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8548 inode_set_bytes(inode
, name_len
);
8549 btrfs_i_size_write(inode
, name_len
);
8550 err
= btrfs_update_inode(trans
, root
, inode
);
8556 d_instantiate(dentry
, inode
);
8557 btrfs_end_transaction(trans
, root
);
8559 inode_dec_link_count(inode
);
8562 btrfs_btree_balance_dirty(root
);
8566 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8567 u64 start
, u64 num_bytes
, u64 min_size
,
8568 loff_t actual_len
, u64
*alloc_hint
,
8569 struct btrfs_trans_handle
*trans
)
8571 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
8572 struct extent_map
*em
;
8573 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8574 struct btrfs_key ins
;
8575 u64 cur_offset
= start
;
8579 bool own_trans
= true;
8583 while (num_bytes
> 0) {
8585 trans
= btrfs_start_transaction(root
, 3);
8586 if (IS_ERR(trans
)) {
8587 ret
= PTR_ERR(trans
);
8592 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
8593 cur_bytes
= max(cur_bytes
, min_size
);
8594 ret
= btrfs_reserve_extent(root
, cur_bytes
, min_size
, 0,
8595 *alloc_hint
, &ins
, 1);
8598 btrfs_end_transaction(trans
, root
);
8602 ret
= insert_reserved_file_extent(trans
, inode
,
8603 cur_offset
, ins
.objectid
,
8604 ins
.offset
, ins
.offset
,
8605 ins
.offset
, 0, 0, 0,
8606 BTRFS_FILE_EXTENT_PREALLOC
);
8608 btrfs_free_reserved_extent(root
, ins
.objectid
,
8610 btrfs_abort_transaction(trans
, root
, ret
);
8612 btrfs_end_transaction(trans
, root
);
8615 btrfs_drop_extent_cache(inode
, cur_offset
,
8616 cur_offset
+ ins
.offset
-1, 0);
8618 em
= alloc_extent_map();
8620 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
8621 &BTRFS_I(inode
)->runtime_flags
);
8625 em
->start
= cur_offset
;
8626 em
->orig_start
= cur_offset
;
8627 em
->len
= ins
.offset
;
8628 em
->block_start
= ins
.objectid
;
8629 em
->block_len
= ins
.offset
;
8630 em
->orig_block_len
= ins
.offset
;
8631 em
->ram_bytes
= ins
.offset
;
8632 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
8633 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
8634 em
->generation
= trans
->transid
;
8637 write_lock(&em_tree
->lock
);
8638 ret
= add_extent_mapping(em_tree
, em
, 1);
8639 write_unlock(&em_tree
->lock
);
8642 btrfs_drop_extent_cache(inode
, cur_offset
,
8643 cur_offset
+ ins
.offset
- 1,
8646 free_extent_map(em
);
8648 num_bytes
-= ins
.offset
;
8649 cur_offset
+= ins
.offset
;
8650 *alloc_hint
= ins
.objectid
+ ins
.offset
;
8652 inode_inc_iversion(inode
);
8653 inode
->i_ctime
= CURRENT_TIME
;
8654 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
8655 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
8656 (actual_len
> inode
->i_size
) &&
8657 (cur_offset
> inode
->i_size
)) {
8658 if (cur_offset
> actual_len
)
8659 i_size
= actual_len
;
8661 i_size
= cur_offset
;
8662 i_size_write(inode
, i_size
);
8663 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
8666 ret
= btrfs_update_inode(trans
, root
, inode
);
8669 btrfs_abort_transaction(trans
, root
, ret
);
8671 btrfs_end_transaction(trans
, root
);
8676 btrfs_end_transaction(trans
, root
);
8681 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8682 u64 start
, u64 num_bytes
, u64 min_size
,
8683 loff_t actual_len
, u64
*alloc_hint
)
8685 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8686 min_size
, actual_len
, alloc_hint
,
8690 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
8691 struct btrfs_trans_handle
*trans
, int mode
,
8692 u64 start
, u64 num_bytes
, u64 min_size
,
8693 loff_t actual_len
, u64
*alloc_hint
)
8695 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8696 min_size
, actual_len
, alloc_hint
, trans
);
8699 static int btrfs_set_page_dirty(struct page
*page
)
8701 return __set_page_dirty_nobuffers(page
);
8704 static int btrfs_permission(struct inode
*inode
, int mask
)
8706 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8707 umode_t mode
= inode
->i_mode
;
8709 if (mask
& MAY_WRITE
&&
8710 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
8711 if (btrfs_root_readonly(root
))
8713 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
8716 return generic_permission(inode
, mask
);
8719 static const struct inode_operations btrfs_dir_inode_operations
= {
8720 .getattr
= btrfs_getattr
,
8721 .lookup
= btrfs_lookup
,
8722 .create
= btrfs_create
,
8723 .unlink
= btrfs_unlink
,
8725 .mkdir
= btrfs_mkdir
,
8726 .rmdir
= btrfs_rmdir
,
8727 .rename
= btrfs_rename
,
8728 .symlink
= btrfs_symlink
,
8729 .setattr
= btrfs_setattr
,
8730 .mknod
= btrfs_mknod
,
8731 .setxattr
= btrfs_setxattr
,
8732 .getxattr
= btrfs_getxattr
,
8733 .listxattr
= btrfs_listxattr
,
8734 .removexattr
= btrfs_removexattr
,
8735 .permission
= btrfs_permission
,
8736 .get_acl
= btrfs_get_acl
,
8737 .update_time
= btrfs_update_time
,
8739 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
8740 .lookup
= btrfs_lookup
,
8741 .permission
= btrfs_permission
,
8742 .get_acl
= btrfs_get_acl
,
8743 .update_time
= btrfs_update_time
,
8746 static const struct file_operations btrfs_dir_file_operations
= {
8747 .llseek
= generic_file_llseek
,
8748 .read
= generic_read_dir
,
8749 .iterate
= btrfs_real_readdir
,
8750 .unlocked_ioctl
= btrfs_ioctl
,
8751 #ifdef CONFIG_COMPAT
8752 .compat_ioctl
= btrfs_ioctl
,
8754 .release
= btrfs_release_file
,
8755 .fsync
= btrfs_sync_file
,
8758 static struct extent_io_ops btrfs_extent_io_ops
= {
8759 .fill_delalloc
= run_delalloc_range
,
8760 .submit_bio_hook
= btrfs_submit_bio_hook
,
8761 .merge_bio_hook
= btrfs_merge_bio_hook
,
8762 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
8763 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
8764 .writepage_start_hook
= btrfs_writepage_start_hook
,
8765 .set_bit_hook
= btrfs_set_bit_hook
,
8766 .clear_bit_hook
= btrfs_clear_bit_hook
,
8767 .merge_extent_hook
= btrfs_merge_extent_hook
,
8768 .split_extent_hook
= btrfs_split_extent_hook
,
8772 * btrfs doesn't support the bmap operation because swapfiles
8773 * use bmap to make a mapping of extents in the file. They assume
8774 * these extents won't change over the life of the file and they
8775 * use the bmap result to do IO directly to the drive.
8777 * the btrfs bmap call would return logical addresses that aren't
8778 * suitable for IO and they also will change frequently as COW
8779 * operations happen. So, swapfile + btrfs == corruption.
8781 * For now we're avoiding this by dropping bmap.
8783 static const struct address_space_operations btrfs_aops
= {
8784 .readpage
= btrfs_readpage
,
8785 .writepage
= btrfs_writepage
,
8786 .writepages
= btrfs_writepages
,
8787 .readpages
= btrfs_readpages
,
8788 .direct_IO
= btrfs_direct_IO
,
8789 .invalidatepage
= btrfs_invalidatepage
,
8790 .releasepage
= btrfs_releasepage
,
8791 .set_page_dirty
= btrfs_set_page_dirty
,
8792 .error_remove_page
= generic_error_remove_page
,
8795 static const struct address_space_operations btrfs_symlink_aops
= {
8796 .readpage
= btrfs_readpage
,
8797 .writepage
= btrfs_writepage
,
8798 .invalidatepage
= btrfs_invalidatepage
,
8799 .releasepage
= btrfs_releasepage
,
8802 static const struct inode_operations btrfs_file_inode_operations
= {
8803 .getattr
= btrfs_getattr
,
8804 .setattr
= btrfs_setattr
,
8805 .setxattr
= btrfs_setxattr
,
8806 .getxattr
= btrfs_getxattr
,
8807 .listxattr
= btrfs_listxattr
,
8808 .removexattr
= btrfs_removexattr
,
8809 .permission
= btrfs_permission
,
8810 .fiemap
= btrfs_fiemap
,
8811 .get_acl
= btrfs_get_acl
,
8812 .update_time
= btrfs_update_time
,
8814 static const struct inode_operations btrfs_special_inode_operations
= {
8815 .getattr
= btrfs_getattr
,
8816 .setattr
= btrfs_setattr
,
8817 .permission
= btrfs_permission
,
8818 .setxattr
= btrfs_setxattr
,
8819 .getxattr
= btrfs_getxattr
,
8820 .listxattr
= btrfs_listxattr
,
8821 .removexattr
= btrfs_removexattr
,
8822 .get_acl
= btrfs_get_acl
,
8823 .update_time
= btrfs_update_time
,
8825 static const struct inode_operations btrfs_symlink_inode_operations
= {
8826 .readlink
= generic_readlink
,
8827 .follow_link
= page_follow_link_light
,
8828 .put_link
= page_put_link
,
8829 .getattr
= btrfs_getattr
,
8830 .setattr
= btrfs_setattr
,
8831 .permission
= btrfs_permission
,
8832 .setxattr
= btrfs_setxattr
,
8833 .getxattr
= btrfs_getxattr
,
8834 .listxattr
= btrfs_listxattr
,
8835 .removexattr
= btrfs_removexattr
,
8836 .get_acl
= btrfs_get_acl
,
8837 .update_time
= btrfs_update_time
,
8840 const struct dentry_operations btrfs_dentry_operations
= {
8841 .d_delete
= btrfs_dentry_delete
,
8842 .d_release
= btrfs_dentry_release
,