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_iter
.bi_sector
<< 9;
1585 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1588 length
= bio
->bi_iter
.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);
2547 * helper function for btrfs_finish_ordered_io, this
2548 * just reads in some of the csum leaves to prime them into ram
2549 * before we start the transaction. It limits the amount of btree
2550 * reads required while inside the transaction.
2552 /* as ordered data IO finishes, this gets called so we can finish
2553 * an ordered extent if the range of bytes in the file it covers are
2556 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2558 struct inode
*inode
= ordered_extent
->inode
;
2559 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2560 struct btrfs_trans_handle
*trans
= NULL
;
2561 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2562 struct extent_state
*cached_state
= NULL
;
2563 struct new_sa_defrag_extent
*new = NULL
;
2564 int compress_type
= 0;
2566 u64 logical_len
= ordered_extent
->len
;
2568 bool truncated
= false;
2570 nolock
= btrfs_is_free_space_inode(inode
);
2572 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2577 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2579 logical_len
= ordered_extent
->truncated_len
;
2580 /* Truncated the entire extent, don't bother adding */
2585 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2586 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2587 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2589 trans
= btrfs_join_transaction_nolock(root
);
2591 trans
= btrfs_join_transaction(root
);
2592 if (IS_ERR(trans
)) {
2593 ret
= PTR_ERR(trans
);
2597 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2598 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2599 if (ret
) /* -ENOMEM or corruption */
2600 btrfs_abort_transaction(trans
, root
, ret
);
2604 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2605 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2608 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2609 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2610 EXTENT_DEFRAG
, 1, cached_state
);
2612 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2613 if (last_snapshot
>= BTRFS_I(inode
)->generation
)
2614 /* the inode is shared */
2615 new = record_old_file_extents(inode
, ordered_extent
);
2617 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2618 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2619 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2623 trans
= btrfs_join_transaction_nolock(root
);
2625 trans
= btrfs_join_transaction(root
);
2626 if (IS_ERR(trans
)) {
2627 ret
= PTR_ERR(trans
);
2631 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2633 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2634 compress_type
= ordered_extent
->compress_type
;
2635 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2636 BUG_ON(compress_type
);
2637 ret
= btrfs_mark_extent_written(trans
, inode
,
2638 ordered_extent
->file_offset
,
2639 ordered_extent
->file_offset
+
2642 BUG_ON(root
== root
->fs_info
->tree_root
);
2643 ret
= insert_reserved_file_extent(trans
, inode
,
2644 ordered_extent
->file_offset
,
2645 ordered_extent
->start
,
2646 ordered_extent
->disk_len
,
2647 logical_len
, logical_len
,
2648 compress_type
, 0, 0,
2649 BTRFS_FILE_EXTENT_REG
);
2651 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2652 ordered_extent
->file_offset
, ordered_extent
->len
,
2655 btrfs_abort_transaction(trans
, root
, ret
);
2659 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2660 &ordered_extent
->list
);
2662 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2663 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2664 if (ret
) { /* -ENOMEM or corruption */
2665 btrfs_abort_transaction(trans
, root
, ret
);
2670 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2671 ordered_extent
->file_offset
+
2672 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2674 if (root
!= root
->fs_info
->tree_root
)
2675 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2677 btrfs_end_transaction(trans
, root
);
2679 if (ret
|| truncated
) {
2683 start
= ordered_extent
->file_offset
+ logical_len
;
2685 start
= ordered_extent
->file_offset
;
2686 end
= ordered_extent
->file_offset
+ ordered_extent
->len
- 1;
2687 clear_extent_uptodate(io_tree
, start
, end
, NULL
, GFP_NOFS
);
2689 /* Drop the cache for the part of the extent we didn't write. */
2690 btrfs_drop_extent_cache(inode
, start
, end
, 0);
2693 * If the ordered extent had an IOERR or something else went
2694 * wrong we need to return the space for this ordered extent
2695 * back to the allocator. We only free the extent in the
2696 * truncated case if we didn't write out the extent at all.
2698 if ((ret
|| !logical_len
) &&
2699 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2700 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2701 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2702 ordered_extent
->disk_len
);
2707 * This needs to be done to make sure anybody waiting knows we are done
2708 * updating everything for this ordered extent.
2710 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2712 /* for snapshot-aware defrag */
2715 free_sa_defrag_extent(new);
2716 atomic_dec(&root
->fs_info
->defrag_running
);
2718 relink_file_extents(new);
2723 btrfs_put_ordered_extent(ordered_extent
);
2724 /* once for the tree */
2725 btrfs_put_ordered_extent(ordered_extent
);
2730 static void finish_ordered_fn(struct btrfs_work
*work
)
2732 struct btrfs_ordered_extent
*ordered_extent
;
2733 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2734 btrfs_finish_ordered_io(ordered_extent
);
2737 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2738 struct extent_state
*state
, int uptodate
)
2740 struct inode
*inode
= page
->mapping
->host
;
2741 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2742 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2743 struct btrfs_workers
*workers
;
2745 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2747 ClearPagePrivate2(page
);
2748 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2749 end
- start
+ 1, uptodate
))
2752 ordered_extent
->work
.func
= finish_ordered_fn
;
2753 ordered_extent
->work
.flags
= 0;
2755 if (btrfs_is_free_space_inode(inode
))
2756 workers
= &root
->fs_info
->endio_freespace_worker
;
2758 workers
= &root
->fs_info
->endio_write_workers
;
2759 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2765 * when reads are done, we need to check csums to verify the data is correct
2766 * if there's a match, we allow the bio to finish. If not, the code in
2767 * extent_io.c will try to find good copies for us.
2769 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
2770 u64 phy_offset
, struct page
*page
,
2771 u64 start
, u64 end
, int mirror
)
2773 size_t offset
= start
- page_offset(page
);
2774 struct inode
*inode
= page
->mapping
->host
;
2775 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2777 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2780 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
2781 DEFAULT_RATELIMIT_BURST
);
2783 if (PageChecked(page
)) {
2784 ClearPageChecked(page
);
2788 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2791 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2792 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2793 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2798 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
2799 csum_expected
= *(((u32
*)io_bio
->csum
) + phy_offset
);
2801 kaddr
= kmap_atomic(page
);
2802 csum
= btrfs_csum_data(kaddr
+ offset
, csum
, end
- start
+ 1);
2803 btrfs_csum_final(csum
, (char *)&csum
);
2804 if (csum
!= csum_expected
)
2807 kunmap_atomic(kaddr
);
2812 if (__ratelimit(&_rs
))
2813 btrfs_info(root
->fs_info
, "csum failed ino %llu off %llu csum %u expected csum %u",
2814 btrfs_ino(page
->mapping
->host
), start
, csum
, csum_expected
);
2815 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2816 flush_dcache_page(page
);
2817 kunmap_atomic(kaddr
);
2818 if (csum_expected
== 0)
2823 struct delayed_iput
{
2824 struct list_head list
;
2825 struct inode
*inode
;
2828 /* JDM: If this is fs-wide, why can't we add a pointer to
2829 * btrfs_inode instead and avoid the allocation? */
2830 void btrfs_add_delayed_iput(struct inode
*inode
)
2832 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2833 struct delayed_iput
*delayed
;
2835 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2838 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2839 delayed
->inode
= inode
;
2841 spin_lock(&fs_info
->delayed_iput_lock
);
2842 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2843 spin_unlock(&fs_info
->delayed_iput_lock
);
2846 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2849 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2850 struct delayed_iput
*delayed
;
2853 spin_lock(&fs_info
->delayed_iput_lock
);
2854 empty
= list_empty(&fs_info
->delayed_iputs
);
2855 spin_unlock(&fs_info
->delayed_iput_lock
);
2859 spin_lock(&fs_info
->delayed_iput_lock
);
2860 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2861 spin_unlock(&fs_info
->delayed_iput_lock
);
2863 while (!list_empty(&list
)) {
2864 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2865 list_del(&delayed
->list
);
2866 iput(delayed
->inode
);
2872 * This is called in transaction commit time. If there are no orphan
2873 * files in the subvolume, it removes orphan item and frees block_rsv
2876 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2877 struct btrfs_root
*root
)
2879 struct btrfs_block_rsv
*block_rsv
;
2882 if (atomic_read(&root
->orphan_inodes
) ||
2883 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2886 spin_lock(&root
->orphan_lock
);
2887 if (atomic_read(&root
->orphan_inodes
)) {
2888 spin_unlock(&root
->orphan_lock
);
2892 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2893 spin_unlock(&root
->orphan_lock
);
2897 block_rsv
= root
->orphan_block_rsv
;
2898 root
->orphan_block_rsv
= NULL
;
2899 spin_unlock(&root
->orphan_lock
);
2901 if (root
->orphan_item_inserted
&&
2902 btrfs_root_refs(&root
->root_item
) > 0) {
2903 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2904 root
->root_key
.objectid
);
2906 btrfs_abort_transaction(trans
, root
, ret
);
2908 root
->orphan_item_inserted
= 0;
2912 WARN_ON(block_rsv
->size
> 0);
2913 btrfs_free_block_rsv(root
, block_rsv
);
2918 * This creates an orphan entry for the given inode in case something goes
2919 * wrong in the middle of an unlink/truncate.
2921 * NOTE: caller of this function should reserve 5 units of metadata for
2924 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2926 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2927 struct btrfs_block_rsv
*block_rsv
= NULL
;
2932 if (!root
->orphan_block_rsv
) {
2933 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2938 spin_lock(&root
->orphan_lock
);
2939 if (!root
->orphan_block_rsv
) {
2940 root
->orphan_block_rsv
= block_rsv
;
2941 } else if (block_rsv
) {
2942 btrfs_free_block_rsv(root
, block_rsv
);
2946 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2947 &BTRFS_I(inode
)->runtime_flags
)) {
2950 * For proper ENOSPC handling, we should do orphan
2951 * cleanup when mounting. But this introduces backward
2952 * compatibility issue.
2954 if (!xchg(&root
->orphan_item_inserted
, 1))
2960 atomic_inc(&root
->orphan_inodes
);
2963 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2964 &BTRFS_I(inode
)->runtime_flags
))
2966 spin_unlock(&root
->orphan_lock
);
2968 /* grab metadata reservation from transaction handle */
2970 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2971 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
2974 /* insert an orphan item to track this unlinked/truncated file */
2976 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2978 atomic_dec(&root
->orphan_inodes
);
2980 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2981 &BTRFS_I(inode
)->runtime_flags
);
2982 btrfs_orphan_release_metadata(inode
);
2984 if (ret
!= -EEXIST
) {
2985 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2986 &BTRFS_I(inode
)->runtime_flags
);
2987 btrfs_abort_transaction(trans
, root
, ret
);
2994 /* insert an orphan item to track subvolume contains orphan files */
2996 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2997 root
->root_key
.objectid
);
2998 if (ret
&& ret
!= -EEXIST
) {
2999 btrfs_abort_transaction(trans
, root
, ret
);
3007 * We have done the truncate/delete so we can go ahead and remove the orphan
3008 * item for this particular inode.
3010 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3011 struct inode
*inode
)
3013 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3014 int delete_item
= 0;
3015 int release_rsv
= 0;
3018 spin_lock(&root
->orphan_lock
);
3019 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3020 &BTRFS_I(inode
)->runtime_flags
))
3023 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3024 &BTRFS_I(inode
)->runtime_flags
))
3026 spin_unlock(&root
->orphan_lock
);
3029 atomic_dec(&root
->orphan_inodes
);
3031 ret
= btrfs_del_orphan_item(trans
, root
,
3036 btrfs_orphan_release_metadata(inode
);
3042 * this cleans up any orphans that may be left on the list from the last use
3045 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3047 struct btrfs_path
*path
;
3048 struct extent_buffer
*leaf
;
3049 struct btrfs_key key
, found_key
;
3050 struct btrfs_trans_handle
*trans
;
3051 struct inode
*inode
;
3052 u64 last_objectid
= 0;
3053 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3055 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3058 path
= btrfs_alloc_path();
3065 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3066 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
3067 key
.offset
= (u64
)-1;
3070 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3075 * if ret == 0 means we found what we were searching for, which
3076 * is weird, but possible, so only screw with path if we didn't
3077 * find the key and see if we have stuff that matches
3081 if (path
->slots
[0] == 0)
3086 /* pull out the item */
3087 leaf
= path
->nodes
[0];
3088 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3090 /* make sure the item matches what we want */
3091 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3093 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
3096 /* release the path since we're done with it */
3097 btrfs_release_path(path
);
3100 * this is where we are basically btrfs_lookup, without the
3101 * crossing root thing. we store the inode number in the
3102 * offset of the orphan item.
3105 if (found_key
.offset
== last_objectid
) {
3106 btrfs_err(root
->fs_info
,
3107 "Error removing orphan entry, stopping orphan cleanup");
3112 last_objectid
= found_key
.offset
;
3114 found_key
.objectid
= found_key
.offset
;
3115 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3116 found_key
.offset
= 0;
3117 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3118 ret
= PTR_ERR_OR_ZERO(inode
);
3119 if (ret
&& ret
!= -ESTALE
)
3122 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3123 struct btrfs_root
*dead_root
;
3124 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3125 int is_dead_root
= 0;
3128 * this is an orphan in the tree root. Currently these
3129 * could come from 2 sources:
3130 * a) a snapshot deletion in progress
3131 * b) a free space cache inode
3132 * We need to distinguish those two, as the snapshot
3133 * orphan must not get deleted.
3134 * find_dead_roots already ran before us, so if this
3135 * is a snapshot deletion, we should find the root
3136 * in the dead_roots list
3138 spin_lock(&fs_info
->trans_lock
);
3139 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3141 if (dead_root
->root_key
.objectid
==
3142 found_key
.objectid
) {
3147 spin_unlock(&fs_info
->trans_lock
);
3149 /* prevent this orphan from being found again */
3150 key
.offset
= found_key
.objectid
- 1;
3155 * Inode is already gone but the orphan item is still there,
3156 * kill the orphan item.
3158 if (ret
== -ESTALE
) {
3159 trans
= btrfs_start_transaction(root
, 1);
3160 if (IS_ERR(trans
)) {
3161 ret
= PTR_ERR(trans
);
3164 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3165 found_key
.objectid
);
3166 ret
= btrfs_del_orphan_item(trans
, root
,
3167 found_key
.objectid
);
3168 btrfs_end_transaction(trans
, root
);
3175 * add this inode to the orphan list so btrfs_orphan_del does
3176 * the proper thing when we hit it
3178 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3179 &BTRFS_I(inode
)->runtime_flags
);
3180 atomic_inc(&root
->orphan_inodes
);
3182 /* if we have links, this was a truncate, lets do that */
3183 if (inode
->i_nlink
) {
3184 if (WARN_ON(!S_ISREG(inode
->i_mode
))) {
3190 /* 1 for the orphan item deletion. */
3191 trans
= btrfs_start_transaction(root
, 1);
3192 if (IS_ERR(trans
)) {
3194 ret
= PTR_ERR(trans
);
3197 ret
= btrfs_orphan_add(trans
, inode
);
3198 btrfs_end_transaction(trans
, root
);
3204 ret
= btrfs_truncate(inode
);
3206 btrfs_orphan_del(NULL
, inode
);
3211 /* this will do delete_inode and everything for us */
3216 /* release the path since we're done with it */
3217 btrfs_release_path(path
);
3219 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3221 if (root
->orphan_block_rsv
)
3222 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3225 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
3226 trans
= btrfs_join_transaction(root
);
3228 btrfs_end_transaction(trans
, root
);
3232 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3234 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3238 btrfs_crit(root
->fs_info
,
3239 "could not do orphan cleanup %d", ret
);
3240 btrfs_free_path(path
);
3245 * very simple check to peek ahead in the leaf looking for xattrs. If we
3246 * don't find any xattrs, we know there can't be any acls.
3248 * slot is the slot the inode is in, objectid is the objectid of the inode
3250 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3251 int slot
, u64 objectid
)
3253 u32 nritems
= btrfs_header_nritems(leaf
);
3254 struct btrfs_key found_key
;
3255 static u64 xattr_access
= 0;
3256 static u64 xattr_default
= 0;
3259 if (!xattr_access
) {
3260 xattr_access
= btrfs_name_hash(POSIX_ACL_XATTR_ACCESS
,
3261 strlen(POSIX_ACL_XATTR_ACCESS
));
3262 xattr_default
= btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT
,
3263 strlen(POSIX_ACL_XATTR_DEFAULT
));
3267 while (slot
< nritems
) {
3268 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3270 /* we found a different objectid, there must not be acls */
3271 if (found_key
.objectid
!= objectid
)
3274 /* we found an xattr, assume we've got an acl */
3275 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3276 if (found_key
.offset
== xattr_access
||
3277 found_key
.offset
== xattr_default
)
3282 * we found a key greater than an xattr key, there can't
3283 * be any acls later on
3285 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3292 * it goes inode, inode backrefs, xattrs, extents,
3293 * so if there are a ton of hard links to an inode there can
3294 * be a lot of backrefs. Don't waste time searching too hard,
3295 * this is just an optimization
3300 /* we hit the end of the leaf before we found an xattr or
3301 * something larger than an xattr. We have to assume the inode
3308 * read an inode from the btree into the in-memory inode
3310 static void btrfs_read_locked_inode(struct inode
*inode
)
3312 struct btrfs_path
*path
;
3313 struct extent_buffer
*leaf
;
3314 struct btrfs_inode_item
*inode_item
;
3315 struct btrfs_timespec
*tspec
;
3316 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3317 struct btrfs_key location
;
3321 bool filled
= false;
3323 ret
= btrfs_fill_inode(inode
, &rdev
);
3327 path
= btrfs_alloc_path();
3331 path
->leave_spinning
= 1;
3332 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3334 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3338 leaf
= path
->nodes
[0];
3343 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3344 struct btrfs_inode_item
);
3345 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3346 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3347 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3348 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3349 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3351 tspec
= btrfs_inode_atime(inode_item
);
3352 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3353 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3355 tspec
= btrfs_inode_mtime(inode_item
);
3356 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3357 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3359 tspec
= btrfs_inode_ctime(inode_item
);
3360 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3361 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3363 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3364 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3365 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3368 * If we were modified in the current generation and evicted from memory
3369 * and then re-read we need to do a full sync since we don't have any
3370 * idea about which extents were modified before we were evicted from
3373 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3374 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3375 &BTRFS_I(inode
)->runtime_flags
);
3377 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3378 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3380 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3382 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3383 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3386 * try to precache a NULL acl entry for files that don't have
3387 * any xattrs or acls
3389 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3392 cache_no_acl(inode
);
3394 btrfs_free_path(path
);
3396 switch (inode
->i_mode
& S_IFMT
) {
3398 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3399 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3400 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3401 inode
->i_fop
= &btrfs_file_operations
;
3402 inode
->i_op
= &btrfs_file_inode_operations
;
3405 inode
->i_fop
= &btrfs_dir_file_operations
;
3406 if (root
== root
->fs_info
->tree_root
)
3407 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3409 inode
->i_op
= &btrfs_dir_inode_operations
;
3412 inode
->i_op
= &btrfs_symlink_inode_operations
;
3413 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3414 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3417 inode
->i_op
= &btrfs_special_inode_operations
;
3418 init_special_inode(inode
, inode
->i_mode
, rdev
);
3422 btrfs_update_iflags(inode
);
3426 btrfs_free_path(path
);
3427 make_bad_inode(inode
);
3431 * given a leaf and an inode, copy the inode fields into the leaf
3433 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3434 struct extent_buffer
*leaf
,
3435 struct btrfs_inode_item
*item
,
3436 struct inode
*inode
)
3438 struct btrfs_map_token token
;
3440 btrfs_init_map_token(&token
);
3442 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3443 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3444 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3446 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3447 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3449 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
3450 inode
->i_atime
.tv_sec
, &token
);
3451 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
3452 inode
->i_atime
.tv_nsec
, &token
);
3454 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
3455 inode
->i_mtime
.tv_sec
, &token
);
3456 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3457 inode
->i_mtime
.tv_nsec
, &token
);
3459 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3460 inode
->i_ctime
.tv_sec
, &token
);
3461 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3462 inode
->i_ctime
.tv_nsec
, &token
);
3464 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3466 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3468 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3469 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3470 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3471 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3472 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3476 * copy everything in the in-memory inode into the btree.
3478 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3479 struct btrfs_root
*root
, struct inode
*inode
)
3481 struct btrfs_inode_item
*inode_item
;
3482 struct btrfs_path
*path
;
3483 struct extent_buffer
*leaf
;
3486 path
= btrfs_alloc_path();
3490 path
->leave_spinning
= 1;
3491 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3499 btrfs_unlock_up_safe(path
, 1);
3500 leaf
= path
->nodes
[0];
3501 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3502 struct btrfs_inode_item
);
3504 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3505 btrfs_mark_buffer_dirty(leaf
);
3506 btrfs_set_inode_last_trans(trans
, inode
);
3509 btrfs_free_path(path
);
3514 * copy everything in the in-memory inode into the btree.
3516 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3517 struct btrfs_root
*root
, struct inode
*inode
)
3522 * If the inode is a free space inode, we can deadlock during commit
3523 * if we put it into the delayed code.
3525 * The data relocation inode should also be directly updated
3528 if (!btrfs_is_free_space_inode(inode
)
3529 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
3530 btrfs_update_root_times(trans
, root
);
3532 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3534 btrfs_set_inode_last_trans(trans
, inode
);
3538 return btrfs_update_inode_item(trans
, root
, inode
);
3541 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3542 struct btrfs_root
*root
,
3543 struct inode
*inode
)
3547 ret
= btrfs_update_inode(trans
, root
, inode
);
3549 return btrfs_update_inode_item(trans
, root
, inode
);
3554 * unlink helper that gets used here in inode.c and in the tree logging
3555 * recovery code. It remove a link in a directory with a given name, and
3556 * also drops the back refs in the inode to the directory
3558 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3559 struct btrfs_root
*root
,
3560 struct inode
*dir
, struct inode
*inode
,
3561 const char *name
, int name_len
)
3563 struct btrfs_path
*path
;
3565 struct extent_buffer
*leaf
;
3566 struct btrfs_dir_item
*di
;
3567 struct btrfs_key key
;
3569 u64 ino
= btrfs_ino(inode
);
3570 u64 dir_ino
= btrfs_ino(dir
);
3572 path
= btrfs_alloc_path();
3578 path
->leave_spinning
= 1;
3579 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3580 name
, name_len
, -1);
3589 leaf
= path
->nodes
[0];
3590 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3591 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3594 btrfs_release_path(path
);
3596 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3599 btrfs_info(root
->fs_info
,
3600 "failed to delete reference to %.*s, inode %llu parent %llu",
3601 name_len
, name
, ino
, dir_ino
);
3602 btrfs_abort_transaction(trans
, root
, ret
);
3606 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3608 btrfs_abort_transaction(trans
, root
, ret
);
3612 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
3614 if (ret
!= 0 && ret
!= -ENOENT
) {
3615 btrfs_abort_transaction(trans
, root
, ret
);
3619 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
3624 btrfs_abort_transaction(trans
, root
, ret
);
3626 btrfs_free_path(path
);
3630 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3631 inode_inc_iversion(inode
);
3632 inode_inc_iversion(dir
);
3633 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3634 ret
= btrfs_update_inode(trans
, root
, dir
);
3639 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3640 struct btrfs_root
*root
,
3641 struct inode
*dir
, struct inode
*inode
,
3642 const char *name
, int name_len
)
3645 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3648 ret
= btrfs_update_inode(trans
, root
, inode
);
3654 * helper to start transaction for unlink and rmdir.
3656 * unlink and rmdir are special in btrfs, they do not always free space, so
3657 * if we cannot make our reservations the normal way try and see if there is
3658 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3659 * allow the unlink to occur.
3661 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
3663 struct btrfs_trans_handle
*trans
;
3664 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3668 * 1 for the possible orphan item
3669 * 1 for the dir item
3670 * 1 for the dir index
3671 * 1 for the inode ref
3674 trans
= btrfs_start_transaction(root
, 5);
3675 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3678 if (PTR_ERR(trans
) == -ENOSPC
) {
3679 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 5);
3681 trans
= btrfs_start_transaction(root
, 0);
3684 ret
= btrfs_cond_migrate_bytes(root
->fs_info
,
3685 &root
->fs_info
->trans_block_rsv
,
3688 btrfs_end_transaction(trans
, root
);
3689 return ERR_PTR(ret
);
3691 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3692 trans
->bytes_reserved
= num_bytes
;
3697 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3699 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3700 struct btrfs_trans_handle
*trans
;
3701 struct inode
*inode
= dentry
->d_inode
;
3704 trans
= __unlink_start_trans(dir
);
3706 return PTR_ERR(trans
);
3708 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3710 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3711 dentry
->d_name
.name
, dentry
->d_name
.len
);
3715 if (inode
->i_nlink
== 0) {
3716 ret
= btrfs_orphan_add(trans
, inode
);
3722 btrfs_end_transaction(trans
, root
);
3723 btrfs_btree_balance_dirty(root
);
3727 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3728 struct btrfs_root
*root
,
3729 struct inode
*dir
, u64 objectid
,
3730 const char *name
, int name_len
)
3732 struct btrfs_path
*path
;
3733 struct extent_buffer
*leaf
;
3734 struct btrfs_dir_item
*di
;
3735 struct btrfs_key key
;
3738 u64 dir_ino
= btrfs_ino(dir
);
3740 path
= btrfs_alloc_path();
3744 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3745 name
, name_len
, -1);
3746 if (IS_ERR_OR_NULL(di
)) {
3754 leaf
= path
->nodes
[0];
3755 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3756 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3757 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3759 btrfs_abort_transaction(trans
, root
, ret
);
3762 btrfs_release_path(path
);
3764 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3765 objectid
, root
->root_key
.objectid
,
3766 dir_ino
, &index
, name
, name_len
);
3768 if (ret
!= -ENOENT
) {
3769 btrfs_abort_transaction(trans
, root
, ret
);
3772 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3774 if (IS_ERR_OR_NULL(di
)) {
3779 btrfs_abort_transaction(trans
, root
, ret
);
3783 leaf
= path
->nodes
[0];
3784 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3785 btrfs_release_path(path
);
3788 btrfs_release_path(path
);
3790 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3792 btrfs_abort_transaction(trans
, root
, ret
);
3796 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3797 inode_inc_iversion(dir
);
3798 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3799 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3801 btrfs_abort_transaction(trans
, root
, ret
);
3803 btrfs_free_path(path
);
3807 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3809 struct inode
*inode
= dentry
->d_inode
;
3811 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3812 struct btrfs_trans_handle
*trans
;
3814 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3816 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3819 trans
= __unlink_start_trans(dir
);
3821 return PTR_ERR(trans
);
3823 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3824 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3825 BTRFS_I(inode
)->location
.objectid
,
3826 dentry
->d_name
.name
,
3827 dentry
->d_name
.len
);
3831 err
= btrfs_orphan_add(trans
, inode
);
3835 /* now the directory is empty */
3836 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3837 dentry
->d_name
.name
, dentry
->d_name
.len
);
3839 btrfs_i_size_write(inode
, 0);
3841 btrfs_end_transaction(trans
, root
);
3842 btrfs_btree_balance_dirty(root
);
3848 * this can truncate away extent items, csum items and directory items.
3849 * It starts at a high offset and removes keys until it can't find
3850 * any higher than new_size
3852 * csum items that cross the new i_size are truncated to the new size
3855 * min_type is the minimum key type to truncate down to. If set to 0, this
3856 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3858 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3859 struct btrfs_root
*root
,
3860 struct inode
*inode
,
3861 u64 new_size
, u32 min_type
)
3863 struct btrfs_path
*path
;
3864 struct extent_buffer
*leaf
;
3865 struct btrfs_file_extent_item
*fi
;
3866 struct btrfs_key key
;
3867 struct btrfs_key found_key
;
3868 u64 extent_start
= 0;
3869 u64 extent_num_bytes
= 0;
3870 u64 extent_offset
= 0;
3872 u64 last_size
= (u64
)-1;
3873 u32 found_type
= (u8
)-1;
3876 int pending_del_nr
= 0;
3877 int pending_del_slot
= 0;
3878 int extent_type
= -1;
3881 u64 ino
= btrfs_ino(inode
);
3883 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3885 path
= btrfs_alloc_path();
3891 * We want to drop from the next block forward in case this new size is
3892 * not block aligned since we will be keeping the last block of the
3893 * extent just the way it is.
3895 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3896 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
3897 root
->sectorsize
), (u64
)-1, 0);
3900 * This function is also used to drop the items in the log tree before
3901 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3902 * it is used to drop the loged items. So we shouldn't kill the delayed
3905 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
3906 btrfs_kill_delayed_inode_items(inode
);
3909 key
.offset
= (u64
)-1;
3913 path
->leave_spinning
= 1;
3914 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3921 /* there are no items in the tree for us to truncate, we're
3924 if (path
->slots
[0] == 0)
3931 leaf
= path
->nodes
[0];
3932 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3933 found_type
= btrfs_key_type(&found_key
);
3935 if (found_key
.objectid
!= ino
)
3938 if (found_type
< min_type
)
3941 item_end
= found_key
.offset
;
3942 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3943 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3944 struct btrfs_file_extent_item
);
3945 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3946 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3948 btrfs_file_extent_num_bytes(leaf
, fi
);
3949 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3950 item_end
+= btrfs_file_extent_inline_len(leaf
,
3955 if (found_type
> min_type
) {
3958 if (item_end
< new_size
)
3960 if (found_key
.offset
>= new_size
)
3966 /* FIXME, shrink the extent if the ref count is only 1 */
3967 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3971 last_size
= found_key
.offset
;
3973 last_size
= new_size
;
3975 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3977 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3979 u64 orig_num_bytes
=
3980 btrfs_file_extent_num_bytes(leaf
, fi
);
3981 extent_num_bytes
= ALIGN(new_size
-
3984 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3986 num_dec
= (orig_num_bytes
-
3988 if (root
->ref_cows
&& extent_start
!= 0)
3989 inode_sub_bytes(inode
, num_dec
);
3990 btrfs_mark_buffer_dirty(leaf
);
3993 btrfs_file_extent_disk_num_bytes(leaf
,
3995 extent_offset
= found_key
.offset
-
3996 btrfs_file_extent_offset(leaf
, fi
);
3998 /* FIXME blocksize != 4096 */
3999 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4000 if (extent_start
!= 0) {
4003 inode_sub_bytes(inode
, num_dec
);
4006 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4008 * we can't truncate inline items that have had
4012 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4013 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4014 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4015 u32 size
= new_size
- found_key
.offset
;
4017 if (root
->ref_cows
) {
4018 inode_sub_bytes(inode
, item_end
+ 1 -
4022 btrfs_file_extent_calc_inline_size(size
);
4023 btrfs_truncate_item(root
, path
, size
, 1);
4024 } else if (root
->ref_cows
) {
4025 inode_sub_bytes(inode
, item_end
+ 1 -
4031 if (!pending_del_nr
) {
4032 /* no pending yet, add ourselves */
4033 pending_del_slot
= path
->slots
[0];
4035 } else if (pending_del_nr
&&
4036 path
->slots
[0] + 1 == pending_del_slot
) {
4037 /* hop on the pending chunk */
4039 pending_del_slot
= path
->slots
[0];
4046 if (found_extent
&& (root
->ref_cows
||
4047 root
== root
->fs_info
->tree_root
)) {
4048 btrfs_set_path_blocking(path
);
4049 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4050 extent_num_bytes
, 0,
4051 btrfs_header_owner(leaf
),
4052 ino
, extent_offset
, 0);
4056 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4059 if (path
->slots
[0] == 0 ||
4060 path
->slots
[0] != pending_del_slot
) {
4061 if (pending_del_nr
) {
4062 ret
= btrfs_del_items(trans
, root
, path
,
4066 btrfs_abort_transaction(trans
,
4072 btrfs_release_path(path
);
4079 if (pending_del_nr
) {
4080 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4083 btrfs_abort_transaction(trans
, root
, ret
);
4086 if (last_size
!= (u64
)-1)
4087 btrfs_ordered_update_i_size(inode
, last_size
, NULL
);
4088 btrfs_free_path(path
);
4093 * btrfs_truncate_page - read, zero a chunk and write a page
4094 * @inode - inode that we're zeroing
4095 * @from - the offset to start zeroing
4096 * @len - the length to zero, 0 to zero the entire range respective to the
4098 * @front - zero up to the offset instead of from the offset on
4100 * This will find the page for the "from" offset and cow the page and zero the
4101 * part we want to zero. This is used with truncate and hole punching.
4103 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4106 struct address_space
*mapping
= inode
->i_mapping
;
4107 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4108 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4109 struct btrfs_ordered_extent
*ordered
;
4110 struct extent_state
*cached_state
= NULL
;
4112 u32 blocksize
= root
->sectorsize
;
4113 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4114 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4116 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4121 if ((offset
& (blocksize
- 1)) == 0 &&
4122 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4124 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4129 page
= find_or_create_page(mapping
, index
, mask
);
4131 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4136 page_start
= page_offset(page
);
4137 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4139 if (!PageUptodate(page
)) {
4140 ret
= btrfs_readpage(NULL
, page
);
4142 if (page
->mapping
!= mapping
) {
4144 page_cache_release(page
);
4147 if (!PageUptodate(page
)) {
4152 wait_on_page_writeback(page
);
4154 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4155 set_page_extent_mapped(page
);
4157 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4159 unlock_extent_cached(io_tree
, page_start
, page_end
,
4160 &cached_state
, GFP_NOFS
);
4162 page_cache_release(page
);
4163 btrfs_start_ordered_extent(inode
, ordered
, 1);
4164 btrfs_put_ordered_extent(ordered
);
4168 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4169 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4170 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4171 0, 0, &cached_state
, GFP_NOFS
);
4173 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4176 unlock_extent_cached(io_tree
, page_start
, page_end
,
4177 &cached_state
, GFP_NOFS
);
4181 if (offset
!= PAGE_CACHE_SIZE
) {
4183 len
= PAGE_CACHE_SIZE
- offset
;
4186 memset(kaddr
, 0, offset
);
4188 memset(kaddr
+ offset
, 0, len
);
4189 flush_dcache_page(page
);
4192 ClearPageChecked(page
);
4193 set_page_dirty(page
);
4194 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4199 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4201 page_cache_release(page
);
4207 * This function puts in dummy file extents for the area we're creating a hole
4208 * for. So if we are truncating this file to a larger size we need to insert
4209 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4210 * the range between oldsize and size
4212 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4214 struct btrfs_trans_handle
*trans
;
4215 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4216 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4217 struct extent_map
*em
= NULL
;
4218 struct extent_state
*cached_state
= NULL
;
4219 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4220 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4221 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4228 * If our size started in the middle of a page we need to zero out the
4229 * rest of the page before we expand the i_size, otherwise we could
4230 * expose stale data.
4232 err
= btrfs_truncate_page(inode
, oldsize
, 0, 0);
4236 if (size
<= hole_start
)
4240 struct btrfs_ordered_extent
*ordered
;
4242 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4244 ordered
= btrfs_lookup_ordered_range(inode
, hole_start
,
4245 block_end
- hole_start
);
4248 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4249 &cached_state
, GFP_NOFS
);
4250 btrfs_start_ordered_extent(inode
, ordered
, 1);
4251 btrfs_put_ordered_extent(ordered
);
4254 cur_offset
= hole_start
;
4256 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4257 block_end
- cur_offset
, 0);
4263 last_byte
= min(extent_map_end(em
), block_end
);
4264 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4265 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4266 struct extent_map
*hole_em
;
4267 hole_size
= last_byte
- cur_offset
;
4269 trans
= btrfs_start_transaction(root
, 3);
4270 if (IS_ERR(trans
)) {
4271 err
= PTR_ERR(trans
);
4275 err
= btrfs_drop_extents(trans
, root
, inode
,
4277 cur_offset
+ hole_size
, 1);
4279 btrfs_abort_transaction(trans
, root
, err
);
4280 btrfs_end_transaction(trans
, root
);
4284 err
= btrfs_insert_file_extent(trans
, root
,
4285 btrfs_ino(inode
), cur_offset
, 0,
4286 0, hole_size
, 0, hole_size
,
4289 btrfs_abort_transaction(trans
, root
, err
);
4290 btrfs_end_transaction(trans
, root
);
4294 btrfs_drop_extent_cache(inode
, cur_offset
,
4295 cur_offset
+ hole_size
- 1, 0);
4296 hole_em
= alloc_extent_map();
4298 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4299 &BTRFS_I(inode
)->runtime_flags
);
4302 hole_em
->start
= cur_offset
;
4303 hole_em
->len
= hole_size
;
4304 hole_em
->orig_start
= cur_offset
;
4306 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4307 hole_em
->block_len
= 0;
4308 hole_em
->orig_block_len
= 0;
4309 hole_em
->ram_bytes
= hole_size
;
4310 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4311 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4312 hole_em
->generation
= trans
->transid
;
4315 write_lock(&em_tree
->lock
);
4316 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4317 write_unlock(&em_tree
->lock
);
4320 btrfs_drop_extent_cache(inode
, cur_offset
,
4324 free_extent_map(hole_em
);
4326 btrfs_update_inode(trans
, root
, inode
);
4327 btrfs_end_transaction(trans
, root
);
4329 free_extent_map(em
);
4331 cur_offset
= last_byte
;
4332 if (cur_offset
>= block_end
)
4336 free_extent_map(em
);
4337 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4342 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4344 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4345 struct btrfs_trans_handle
*trans
;
4346 loff_t oldsize
= i_size_read(inode
);
4347 loff_t newsize
= attr
->ia_size
;
4348 int mask
= attr
->ia_valid
;
4352 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4353 * special case where we need to update the times despite not having
4354 * these flags set. For all other operations the VFS set these flags
4355 * explicitly if it wants a timestamp update.
4357 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
4358 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
4360 if (newsize
> oldsize
) {
4361 truncate_pagecache(inode
, newsize
);
4362 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4366 trans
= btrfs_start_transaction(root
, 1);
4368 return PTR_ERR(trans
);
4370 i_size_write(inode
, newsize
);
4371 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4372 ret
= btrfs_update_inode(trans
, root
, inode
);
4373 btrfs_end_transaction(trans
, root
);
4377 * We're truncating a file that used to have good data down to
4378 * zero. Make sure it gets into the ordered flush list so that
4379 * any new writes get down to disk quickly.
4382 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4383 &BTRFS_I(inode
)->runtime_flags
);
4386 * 1 for the orphan item we're going to add
4387 * 1 for the orphan item deletion.
4389 trans
= btrfs_start_transaction(root
, 2);
4391 return PTR_ERR(trans
);
4394 * We need to do this in case we fail at _any_ point during the
4395 * actual truncate. Once we do the truncate_setsize we could
4396 * invalidate pages which forces any outstanding ordered io to
4397 * be instantly completed which will give us extents that need
4398 * to be truncated. If we fail to get an orphan inode down we
4399 * could have left over extents that were never meant to live,
4400 * so we need to garuntee from this point on that everything
4401 * will be consistent.
4403 ret
= btrfs_orphan_add(trans
, inode
);
4404 btrfs_end_transaction(trans
, root
);
4408 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4409 truncate_setsize(inode
, newsize
);
4411 /* Disable nonlocked read DIO to avoid the end less truncate */
4412 btrfs_inode_block_unlocked_dio(inode
);
4413 inode_dio_wait(inode
);
4414 btrfs_inode_resume_unlocked_dio(inode
);
4416 ret
= btrfs_truncate(inode
);
4417 if (ret
&& inode
->i_nlink
) {
4421 * failed to truncate, disk_i_size is only adjusted down
4422 * as we remove extents, so it should represent the true
4423 * size of the inode, so reset the in memory size and
4424 * delete our orphan entry.
4426 trans
= btrfs_join_transaction(root
);
4427 if (IS_ERR(trans
)) {
4428 btrfs_orphan_del(NULL
, inode
);
4431 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
4432 err
= btrfs_orphan_del(trans
, inode
);
4434 btrfs_abort_transaction(trans
, root
, err
);
4435 btrfs_end_transaction(trans
, root
);
4442 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4444 struct inode
*inode
= dentry
->d_inode
;
4445 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4448 if (btrfs_root_readonly(root
))
4451 err
= inode_change_ok(inode
, attr
);
4455 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4456 err
= btrfs_setsize(inode
, attr
);
4461 if (attr
->ia_valid
) {
4462 setattr_copy(inode
, attr
);
4463 inode_inc_iversion(inode
);
4464 err
= btrfs_dirty_inode(inode
);
4466 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4467 err
= btrfs_acl_chmod(inode
);
4473 void btrfs_evict_inode(struct inode
*inode
)
4475 struct btrfs_trans_handle
*trans
;
4476 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4477 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4478 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4481 trace_btrfs_inode_evict(inode
);
4483 truncate_inode_pages(&inode
->i_data
, 0);
4484 if (inode
->i_nlink
&&
4485 ((btrfs_root_refs(&root
->root_item
) != 0 &&
4486 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
4487 btrfs_is_free_space_inode(inode
)))
4490 if (is_bad_inode(inode
)) {
4491 btrfs_orphan_del(NULL
, inode
);
4494 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4495 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4497 if (root
->fs_info
->log_root_recovering
) {
4498 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4499 &BTRFS_I(inode
)->runtime_flags
));
4503 if (inode
->i_nlink
> 0) {
4504 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
4505 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
4509 ret
= btrfs_commit_inode_delayed_inode(inode
);
4511 btrfs_orphan_del(NULL
, inode
);
4515 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4517 btrfs_orphan_del(NULL
, inode
);
4520 rsv
->size
= min_size
;
4522 global_rsv
= &root
->fs_info
->global_block_rsv
;
4524 btrfs_i_size_write(inode
, 0);
4527 * This is a bit simpler than btrfs_truncate since we've already
4528 * reserved our space for our orphan item in the unlink, so we just
4529 * need to reserve some slack space in case we add bytes and update
4530 * inode item when doing the truncate.
4533 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4534 BTRFS_RESERVE_FLUSH_LIMIT
);
4537 * Try and steal from the global reserve since we will
4538 * likely not use this space anyway, we want to try as
4539 * hard as possible to get this to work.
4542 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4545 btrfs_warn(root
->fs_info
,
4546 "Could not get space for a delete, will truncate on mount %d",
4548 btrfs_orphan_del(NULL
, inode
);
4549 btrfs_free_block_rsv(root
, rsv
);
4553 trans
= btrfs_join_transaction(root
);
4554 if (IS_ERR(trans
)) {
4555 btrfs_orphan_del(NULL
, inode
);
4556 btrfs_free_block_rsv(root
, rsv
);
4560 trans
->block_rsv
= rsv
;
4562 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4566 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4567 btrfs_end_transaction(trans
, root
);
4569 btrfs_btree_balance_dirty(root
);
4572 btrfs_free_block_rsv(root
, rsv
);
4575 * Errors here aren't a big deal, it just means we leave orphan items
4576 * in the tree. They will be cleaned up on the next mount.
4579 trans
->block_rsv
= root
->orphan_block_rsv
;
4580 btrfs_orphan_del(trans
, inode
);
4582 btrfs_orphan_del(NULL
, inode
);
4585 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4586 if (!(root
== root
->fs_info
->tree_root
||
4587 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4588 btrfs_return_ino(root
, btrfs_ino(inode
));
4590 btrfs_end_transaction(trans
, root
);
4591 btrfs_btree_balance_dirty(root
);
4593 btrfs_remove_delayed_node(inode
);
4599 * this returns the key found in the dir entry in the location pointer.
4600 * If no dir entries were found, location->objectid is 0.
4602 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4603 struct btrfs_key
*location
)
4605 const char *name
= dentry
->d_name
.name
;
4606 int namelen
= dentry
->d_name
.len
;
4607 struct btrfs_dir_item
*di
;
4608 struct btrfs_path
*path
;
4609 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4612 path
= btrfs_alloc_path();
4616 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4621 if (IS_ERR_OR_NULL(di
))
4624 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4626 btrfs_free_path(path
);
4629 location
->objectid
= 0;
4634 * when we hit a tree root in a directory, the btrfs part of the inode
4635 * needs to be changed to reflect the root directory of the tree root. This
4636 * is kind of like crossing a mount point.
4638 static int fixup_tree_root_location(struct btrfs_root
*root
,
4640 struct dentry
*dentry
,
4641 struct btrfs_key
*location
,
4642 struct btrfs_root
**sub_root
)
4644 struct btrfs_path
*path
;
4645 struct btrfs_root
*new_root
;
4646 struct btrfs_root_ref
*ref
;
4647 struct extent_buffer
*leaf
;
4651 path
= btrfs_alloc_path();
4658 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4659 BTRFS_I(dir
)->root
->root_key
.objectid
,
4660 location
->objectid
);
4667 leaf
= path
->nodes
[0];
4668 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4669 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4670 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4673 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4674 (unsigned long)(ref
+ 1),
4675 dentry
->d_name
.len
);
4679 btrfs_release_path(path
);
4681 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4682 if (IS_ERR(new_root
)) {
4683 err
= PTR_ERR(new_root
);
4687 *sub_root
= new_root
;
4688 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4689 location
->type
= BTRFS_INODE_ITEM_KEY
;
4690 location
->offset
= 0;
4693 btrfs_free_path(path
);
4697 static void inode_tree_add(struct inode
*inode
)
4699 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4700 struct btrfs_inode
*entry
;
4702 struct rb_node
*parent
;
4703 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
4704 u64 ino
= btrfs_ino(inode
);
4706 if (inode_unhashed(inode
))
4709 spin_lock(&root
->inode_lock
);
4710 p
= &root
->inode_tree
.rb_node
;
4713 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4715 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4716 p
= &parent
->rb_left
;
4717 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4718 p
= &parent
->rb_right
;
4720 WARN_ON(!(entry
->vfs_inode
.i_state
&
4721 (I_WILL_FREE
| I_FREEING
)));
4722 rb_replace_node(parent
, new, &root
->inode_tree
);
4723 RB_CLEAR_NODE(parent
);
4724 spin_unlock(&root
->inode_lock
);
4728 rb_link_node(new, parent
, p
);
4729 rb_insert_color(new, &root
->inode_tree
);
4730 spin_unlock(&root
->inode_lock
);
4733 static void inode_tree_del(struct inode
*inode
)
4735 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4738 spin_lock(&root
->inode_lock
);
4739 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4740 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4741 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4742 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4744 spin_unlock(&root
->inode_lock
);
4746 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
4747 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4748 spin_lock(&root
->inode_lock
);
4749 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4750 spin_unlock(&root
->inode_lock
);
4752 btrfs_add_dead_root(root
);
4756 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4758 struct rb_node
*node
;
4759 struct rb_node
*prev
;
4760 struct btrfs_inode
*entry
;
4761 struct inode
*inode
;
4764 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4766 spin_lock(&root
->inode_lock
);
4768 node
= root
->inode_tree
.rb_node
;
4772 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4774 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4775 node
= node
->rb_left
;
4776 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4777 node
= node
->rb_right
;
4783 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4784 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4788 prev
= rb_next(prev
);
4792 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4793 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4794 inode
= igrab(&entry
->vfs_inode
);
4796 spin_unlock(&root
->inode_lock
);
4797 if (atomic_read(&inode
->i_count
) > 1)
4798 d_prune_aliases(inode
);
4800 * btrfs_drop_inode will have it removed from
4801 * the inode cache when its usage count
4806 spin_lock(&root
->inode_lock
);
4810 if (cond_resched_lock(&root
->inode_lock
))
4813 node
= rb_next(node
);
4815 spin_unlock(&root
->inode_lock
);
4818 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4820 struct btrfs_iget_args
*args
= p
;
4821 inode
->i_ino
= args
->ino
;
4822 BTRFS_I(inode
)->root
= args
->root
;
4826 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4828 struct btrfs_iget_args
*args
= opaque
;
4829 return args
->ino
== btrfs_ino(inode
) &&
4830 args
->root
== BTRFS_I(inode
)->root
;
4833 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4835 struct btrfs_root
*root
)
4837 struct inode
*inode
;
4838 struct btrfs_iget_args args
;
4839 unsigned long hashval
= btrfs_inode_hash(objectid
, root
);
4841 args
.ino
= objectid
;
4844 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
4845 btrfs_init_locked_inode
,
4850 /* Get an inode object given its location and corresponding root.
4851 * Returns in *is_new if the inode was read from disk
4853 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4854 struct btrfs_root
*root
, int *new)
4856 struct inode
*inode
;
4858 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4860 return ERR_PTR(-ENOMEM
);
4862 if (inode
->i_state
& I_NEW
) {
4863 BTRFS_I(inode
)->root
= root
;
4864 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4865 btrfs_read_locked_inode(inode
);
4866 if (!is_bad_inode(inode
)) {
4867 inode_tree_add(inode
);
4868 unlock_new_inode(inode
);
4872 unlock_new_inode(inode
);
4874 inode
= ERR_PTR(-ESTALE
);
4881 static struct inode
*new_simple_dir(struct super_block
*s
,
4882 struct btrfs_key
*key
,
4883 struct btrfs_root
*root
)
4885 struct inode
*inode
= new_inode(s
);
4888 return ERR_PTR(-ENOMEM
);
4890 BTRFS_I(inode
)->root
= root
;
4891 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4892 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
4894 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4895 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
4896 inode
->i_fop
= &simple_dir_operations
;
4897 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4898 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4903 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4905 struct inode
*inode
;
4906 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4907 struct btrfs_root
*sub_root
= root
;
4908 struct btrfs_key location
;
4912 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4913 return ERR_PTR(-ENAMETOOLONG
);
4915 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4917 return ERR_PTR(ret
);
4919 if (location
.objectid
== 0)
4922 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4923 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4927 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4929 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4930 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4931 &location
, &sub_root
);
4934 inode
= ERR_PTR(ret
);
4936 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4938 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4940 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4942 if (!IS_ERR(inode
) && root
!= sub_root
) {
4943 down_read(&root
->fs_info
->cleanup_work_sem
);
4944 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4945 ret
= btrfs_orphan_cleanup(sub_root
);
4946 up_read(&root
->fs_info
->cleanup_work_sem
);
4949 inode
= ERR_PTR(ret
);
4956 static int btrfs_dentry_delete(const struct dentry
*dentry
)
4958 struct btrfs_root
*root
;
4959 struct inode
*inode
= dentry
->d_inode
;
4961 if (!inode
&& !IS_ROOT(dentry
))
4962 inode
= dentry
->d_parent
->d_inode
;
4965 root
= BTRFS_I(inode
)->root
;
4966 if (btrfs_root_refs(&root
->root_item
) == 0)
4969 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
4975 static void btrfs_dentry_release(struct dentry
*dentry
)
4977 if (dentry
->d_fsdata
)
4978 kfree(dentry
->d_fsdata
);
4981 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4986 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
4990 unsigned char btrfs_filetype_table
[] = {
4991 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4994 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
4996 struct inode
*inode
= file_inode(file
);
4997 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4998 struct btrfs_item
*item
;
4999 struct btrfs_dir_item
*di
;
5000 struct btrfs_key key
;
5001 struct btrfs_key found_key
;
5002 struct btrfs_path
*path
;
5003 struct list_head ins_list
;
5004 struct list_head del_list
;
5006 struct extent_buffer
*leaf
;
5008 unsigned char d_type
;
5013 int key_type
= BTRFS_DIR_INDEX_KEY
;
5017 int is_curr
= 0; /* ctx->pos points to the current index? */
5019 /* FIXME, use a real flag for deciding about the key type */
5020 if (root
->fs_info
->tree_root
== root
)
5021 key_type
= BTRFS_DIR_ITEM_KEY
;
5023 if (!dir_emit_dots(file
, ctx
))
5026 path
= btrfs_alloc_path();
5032 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5033 INIT_LIST_HEAD(&ins_list
);
5034 INIT_LIST_HEAD(&del_list
);
5035 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5038 btrfs_set_key_type(&key
, key_type
);
5039 key
.offset
= ctx
->pos
;
5040 key
.objectid
= btrfs_ino(inode
);
5042 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5047 leaf
= path
->nodes
[0];
5048 slot
= path
->slots
[0];
5049 if (slot
>= btrfs_header_nritems(leaf
)) {
5050 ret
= btrfs_next_leaf(root
, path
);
5058 item
= btrfs_item_nr(slot
);
5059 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5061 if (found_key
.objectid
!= key
.objectid
)
5063 if (btrfs_key_type(&found_key
) != key_type
)
5065 if (found_key
.offset
< ctx
->pos
)
5067 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5068 btrfs_should_delete_dir_index(&del_list
,
5072 ctx
->pos
= found_key
.offset
;
5075 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5077 di_total
= btrfs_item_size(leaf
, item
);
5079 while (di_cur
< di_total
) {
5080 struct btrfs_key location
;
5082 if (verify_dir_item(root
, leaf
, di
))
5085 name_len
= btrfs_dir_name_len(leaf
, di
);
5086 if (name_len
<= sizeof(tmp_name
)) {
5087 name_ptr
= tmp_name
;
5089 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5095 read_extent_buffer(leaf
, name_ptr
,
5096 (unsigned long)(di
+ 1), name_len
);
5098 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5099 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5102 /* is this a reference to our own snapshot? If so
5105 * In contrast to old kernels, we insert the snapshot's
5106 * dir item and dir index after it has been created, so
5107 * we won't find a reference to our own snapshot. We
5108 * still keep the following code for backward
5111 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5112 location
.objectid
== root
->root_key
.objectid
) {
5116 over
= !dir_emit(ctx
, name_ptr
, name_len
,
5117 location
.objectid
, d_type
);
5120 if (name_ptr
!= tmp_name
)
5125 di_len
= btrfs_dir_name_len(leaf
, di
) +
5126 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5128 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5134 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5137 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
);
5142 /* Reached end of directory/root. Bump pos past the last item. */
5146 * Stop new entries from being returned after we return the last
5149 * New directory entries are assigned a strictly increasing
5150 * offset. This means that new entries created during readdir
5151 * are *guaranteed* to be seen in the future by that readdir.
5152 * This has broken buggy programs which operate on names as
5153 * they're returned by readdir. Until we re-use freed offsets
5154 * we have this hack to stop new entries from being returned
5155 * under the assumption that they'll never reach this huge
5158 * This is being careful not to overflow 32bit loff_t unless the
5159 * last entry requires it because doing so has broken 32bit apps
5162 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5163 if (ctx
->pos
>= INT_MAX
)
5164 ctx
->pos
= LLONG_MAX
;
5171 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5172 btrfs_put_delayed_items(&ins_list
, &del_list
);
5173 btrfs_free_path(path
);
5177 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5179 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5180 struct btrfs_trans_handle
*trans
;
5182 bool nolock
= false;
5184 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5187 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5190 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5192 trans
= btrfs_join_transaction_nolock(root
);
5194 trans
= btrfs_join_transaction(root
);
5196 return PTR_ERR(trans
);
5197 ret
= btrfs_commit_transaction(trans
, root
);
5203 * This is somewhat expensive, updating the tree every time the
5204 * inode changes. But, it is most likely to find the inode in cache.
5205 * FIXME, needs more benchmarking...there are no reasons other than performance
5206 * to keep or drop this code.
5208 static int btrfs_dirty_inode(struct inode
*inode
)
5210 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5211 struct btrfs_trans_handle
*trans
;
5214 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5217 trans
= btrfs_join_transaction(root
);
5219 return PTR_ERR(trans
);
5221 ret
= btrfs_update_inode(trans
, root
, inode
);
5222 if (ret
&& ret
== -ENOSPC
) {
5223 /* whoops, lets try again with the full transaction */
5224 btrfs_end_transaction(trans
, root
);
5225 trans
= btrfs_start_transaction(root
, 1);
5227 return PTR_ERR(trans
);
5229 ret
= btrfs_update_inode(trans
, root
, inode
);
5231 btrfs_end_transaction(trans
, root
);
5232 if (BTRFS_I(inode
)->delayed_node
)
5233 btrfs_balance_delayed_items(root
);
5239 * This is a copy of file_update_time. We need this so we can return error on
5240 * ENOSPC for updating the inode in the case of file write and mmap writes.
5242 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5245 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5247 if (btrfs_root_readonly(root
))
5250 if (flags
& S_VERSION
)
5251 inode_inc_iversion(inode
);
5252 if (flags
& S_CTIME
)
5253 inode
->i_ctime
= *now
;
5254 if (flags
& S_MTIME
)
5255 inode
->i_mtime
= *now
;
5256 if (flags
& S_ATIME
)
5257 inode
->i_atime
= *now
;
5258 return btrfs_dirty_inode(inode
);
5262 * find the highest existing sequence number in a directory
5263 * and then set the in-memory index_cnt variable to reflect
5264 * free sequence numbers
5266 static int btrfs_set_inode_index_count(struct inode
*inode
)
5268 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5269 struct btrfs_key key
, found_key
;
5270 struct btrfs_path
*path
;
5271 struct extent_buffer
*leaf
;
5274 key
.objectid
= btrfs_ino(inode
);
5275 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
5276 key
.offset
= (u64
)-1;
5278 path
= btrfs_alloc_path();
5282 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5285 /* FIXME: we should be able to handle this */
5291 * MAGIC NUMBER EXPLANATION:
5292 * since we search a directory based on f_pos we have to start at 2
5293 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5294 * else has to start at 2
5296 if (path
->slots
[0] == 0) {
5297 BTRFS_I(inode
)->index_cnt
= 2;
5303 leaf
= path
->nodes
[0];
5304 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5306 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5307 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
5308 BTRFS_I(inode
)->index_cnt
= 2;
5312 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5314 btrfs_free_path(path
);
5319 * helper to find a free sequence number in a given directory. This current
5320 * code is very simple, later versions will do smarter things in the btree
5322 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5326 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5327 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5329 ret
= btrfs_set_inode_index_count(dir
);
5335 *index
= BTRFS_I(dir
)->index_cnt
;
5336 BTRFS_I(dir
)->index_cnt
++;
5341 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5342 struct btrfs_root
*root
,
5344 const char *name
, int name_len
,
5345 u64 ref_objectid
, u64 objectid
,
5346 umode_t mode
, u64
*index
)
5348 struct inode
*inode
;
5349 struct btrfs_inode_item
*inode_item
;
5350 struct btrfs_key
*location
;
5351 struct btrfs_path
*path
;
5352 struct btrfs_inode_ref
*ref
;
5353 struct btrfs_key key
[2];
5359 path
= btrfs_alloc_path();
5361 return ERR_PTR(-ENOMEM
);
5363 inode
= new_inode(root
->fs_info
->sb
);
5365 btrfs_free_path(path
);
5366 return ERR_PTR(-ENOMEM
);
5370 * we have to initialize this early, so we can reclaim the inode
5371 * number if we fail afterwards in this function.
5373 inode
->i_ino
= objectid
;
5376 trace_btrfs_inode_request(dir
);
5378 ret
= btrfs_set_inode_index(dir
, index
);
5380 btrfs_free_path(path
);
5382 return ERR_PTR(ret
);
5386 * index_cnt is ignored for everything but a dir,
5387 * btrfs_get_inode_index_count has an explanation for the magic
5390 BTRFS_I(inode
)->index_cnt
= 2;
5391 BTRFS_I(inode
)->root
= root
;
5392 BTRFS_I(inode
)->generation
= trans
->transid
;
5393 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5396 * We could have gotten an inode number from somebody who was fsynced
5397 * and then removed in this same transaction, so let's just set full
5398 * sync since it will be a full sync anyway and this will blow away the
5399 * old info in the log.
5401 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5408 key
[0].objectid
= objectid
;
5409 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
5413 * Start new inodes with an inode_ref. This is slightly more
5414 * efficient for small numbers of hard links since they will
5415 * be packed into one item. Extended refs will kick in if we
5416 * add more hard links than can fit in the ref item.
5418 key
[1].objectid
= objectid
;
5419 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
5420 key
[1].offset
= ref_objectid
;
5422 sizes
[0] = sizeof(struct btrfs_inode_item
);
5423 sizes
[1] = name_len
+ sizeof(*ref
);
5425 path
->leave_spinning
= 1;
5426 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
5430 inode_init_owner(inode
, dir
, mode
);
5431 inode_set_bytes(inode
, 0);
5432 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5433 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5434 struct btrfs_inode_item
);
5435 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5436 sizeof(*inode_item
));
5437 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5439 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5440 struct btrfs_inode_ref
);
5441 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5442 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5443 ptr
= (unsigned long)(ref
+ 1);
5444 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5446 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5447 btrfs_free_path(path
);
5449 location
= &BTRFS_I(inode
)->location
;
5450 location
->objectid
= objectid
;
5451 location
->offset
= 0;
5452 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
5454 btrfs_inherit_iflags(inode
, dir
);
5456 if (S_ISREG(mode
)) {
5457 if (btrfs_test_opt(root
, NODATASUM
))
5458 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5459 if (btrfs_test_opt(root
, NODATACOW
))
5460 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5461 BTRFS_INODE_NODATASUM
;
5464 btrfs_insert_inode_hash(inode
);
5465 inode_tree_add(inode
);
5467 trace_btrfs_inode_new(inode
);
5468 btrfs_set_inode_last_trans(trans
, inode
);
5470 btrfs_update_root_times(trans
, root
);
5475 BTRFS_I(dir
)->index_cnt
--;
5476 btrfs_free_path(path
);
5478 return ERR_PTR(ret
);
5481 static inline u8
btrfs_inode_type(struct inode
*inode
)
5483 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5487 * utility function to add 'inode' into 'parent_inode' with
5488 * a give name and a given sequence number.
5489 * if 'add_backref' is true, also insert a backref from the
5490 * inode to the parent directory.
5492 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5493 struct inode
*parent_inode
, struct inode
*inode
,
5494 const char *name
, int name_len
, int add_backref
, u64 index
)
5497 struct btrfs_key key
;
5498 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
5499 u64 ino
= btrfs_ino(inode
);
5500 u64 parent_ino
= btrfs_ino(parent_inode
);
5502 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5503 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
5506 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
5510 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5511 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
5512 key
.objectid
, root
->root_key
.objectid
,
5513 parent_ino
, index
, name
, name_len
);
5514 } else if (add_backref
) {
5515 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
5519 /* Nothing to clean up yet */
5523 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
5525 btrfs_inode_type(inode
), index
);
5526 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
5529 btrfs_abort_transaction(trans
, root
, ret
);
5533 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5535 inode_inc_iversion(parent_inode
);
5536 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5537 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5539 btrfs_abort_transaction(trans
, root
, ret
);
5543 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5546 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5547 key
.objectid
, root
->root_key
.objectid
,
5548 parent_ino
, &local_index
, name
, name_len
);
5550 } else if (add_backref
) {
5554 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5555 ino
, parent_ino
, &local_index
);
5560 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5561 struct inode
*dir
, struct dentry
*dentry
,
5562 struct inode
*inode
, int backref
, u64 index
)
5564 int err
= btrfs_add_link(trans
, dir
, inode
,
5565 dentry
->d_name
.name
, dentry
->d_name
.len
,
5572 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5573 umode_t mode
, dev_t rdev
)
5575 struct btrfs_trans_handle
*trans
;
5576 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5577 struct inode
*inode
= NULL
;
5583 if (!new_valid_dev(rdev
))
5587 * 2 for inode item and ref
5589 * 1 for xattr if selinux is on
5591 trans
= btrfs_start_transaction(root
, 5);
5593 return PTR_ERR(trans
);
5595 err
= btrfs_find_free_ino(root
, &objectid
);
5599 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5600 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5602 if (IS_ERR(inode
)) {
5603 err
= PTR_ERR(inode
);
5607 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5614 * If the active LSM wants to access the inode during
5615 * d_instantiate it needs these. Smack checks to see
5616 * if the filesystem supports xattrs by looking at the
5620 inode
->i_op
= &btrfs_special_inode_operations
;
5621 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5625 init_special_inode(inode
, inode
->i_mode
, rdev
);
5626 btrfs_update_inode(trans
, root
, inode
);
5627 d_instantiate(dentry
, inode
);
5630 btrfs_end_transaction(trans
, root
);
5631 btrfs_btree_balance_dirty(root
);
5633 inode_dec_link_count(inode
);
5639 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5640 umode_t mode
, bool excl
)
5642 struct btrfs_trans_handle
*trans
;
5643 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5644 struct inode
*inode
= NULL
;
5645 int drop_inode_on_err
= 0;
5651 * 2 for inode item and ref
5653 * 1 for xattr if selinux is on
5655 trans
= btrfs_start_transaction(root
, 5);
5657 return PTR_ERR(trans
);
5659 err
= btrfs_find_free_ino(root
, &objectid
);
5663 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5664 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5666 if (IS_ERR(inode
)) {
5667 err
= PTR_ERR(inode
);
5670 drop_inode_on_err
= 1;
5672 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5676 err
= btrfs_update_inode(trans
, root
, inode
);
5681 * If the active LSM wants to access the inode during
5682 * d_instantiate it needs these. Smack checks to see
5683 * if the filesystem supports xattrs by looking at the
5686 inode
->i_fop
= &btrfs_file_operations
;
5687 inode
->i_op
= &btrfs_file_inode_operations
;
5689 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5693 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5694 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5695 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5696 d_instantiate(dentry
, inode
);
5699 btrfs_end_transaction(trans
, root
);
5700 if (err
&& drop_inode_on_err
) {
5701 inode_dec_link_count(inode
);
5704 btrfs_btree_balance_dirty(root
);
5708 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5709 struct dentry
*dentry
)
5711 struct btrfs_trans_handle
*trans
;
5712 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5713 struct inode
*inode
= old_dentry
->d_inode
;
5718 /* do not allow sys_link's with other subvols of the same device */
5719 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5722 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5725 err
= btrfs_set_inode_index(dir
, &index
);
5730 * 2 items for inode and inode ref
5731 * 2 items for dir items
5732 * 1 item for parent inode
5734 trans
= btrfs_start_transaction(root
, 5);
5735 if (IS_ERR(trans
)) {
5736 err
= PTR_ERR(trans
);
5741 inode_inc_iversion(inode
);
5742 inode
->i_ctime
= CURRENT_TIME
;
5744 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5746 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5751 struct dentry
*parent
= dentry
->d_parent
;
5752 err
= btrfs_update_inode(trans
, root
, inode
);
5755 d_instantiate(dentry
, inode
);
5756 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5759 btrfs_end_transaction(trans
, root
);
5762 inode_dec_link_count(inode
);
5765 btrfs_btree_balance_dirty(root
);
5769 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5771 struct inode
*inode
= NULL
;
5772 struct btrfs_trans_handle
*trans
;
5773 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5775 int drop_on_err
= 0;
5780 * 2 items for inode and ref
5781 * 2 items for dir items
5782 * 1 for xattr if selinux is on
5784 trans
= btrfs_start_transaction(root
, 5);
5786 return PTR_ERR(trans
);
5788 err
= btrfs_find_free_ino(root
, &objectid
);
5792 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5793 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5794 S_IFDIR
| mode
, &index
);
5795 if (IS_ERR(inode
)) {
5796 err
= PTR_ERR(inode
);
5802 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5806 inode
->i_op
= &btrfs_dir_inode_operations
;
5807 inode
->i_fop
= &btrfs_dir_file_operations
;
5809 btrfs_i_size_write(inode
, 0);
5810 err
= btrfs_update_inode(trans
, root
, inode
);
5814 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5815 dentry
->d_name
.len
, 0, index
);
5819 d_instantiate(dentry
, inode
);
5823 btrfs_end_transaction(trans
, root
);
5826 btrfs_btree_balance_dirty(root
);
5830 /* helper for btfs_get_extent. Given an existing extent in the tree,
5831 * and an extent that you want to insert, deal with overlap and insert
5832 * the new extent into the tree.
5834 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5835 struct extent_map
*existing
,
5836 struct extent_map
*em
,
5837 u64 map_start
, u64 map_len
)
5841 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5842 start_diff
= map_start
- em
->start
;
5843 em
->start
= map_start
;
5845 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5846 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5847 em
->block_start
+= start_diff
;
5848 em
->block_len
-= start_diff
;
5850 return add_extent_mapping(em_tree
, em
, 0);
5853 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5854 struct inode
*inode
, struct page
*page
,
5855 size_t pg_offset
, u64 extent_offset
,
5856 struct btrfs_file_extent_item
*item
)
5859 struct extent_buffer
*leaf
= path
->nodes
[0];
5862 unsigned long inline_size
;
5866 WARN_ON(pg_offset
!= 0);
5867 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5868 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
5869 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
5870 btrfs_item_nr(path
->slots
[0]));
5871 tmp
= kmalloc(inline_size
, GFP_NOFS
);
5874 ptr
= btrfs_file_extent_inline_start(item
);
5876 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
5878 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
5879 ret
= btrfs_decompress(compress_type
, tmp
, page
,
5880 extent_offset
, inline_size
, max_size
);
5882 char *kaddr
= kmap_atomic(page
);
5883 unsigned long copy_size
= min_t(u64
,
5884 PAGE_CACHE_SIZE
- pg_offset
,
5885 max_size
- extent_offset
);
5886 memset(kaddr
+ pg_offset
, 0, copy_size
);
5887 kunmap_atomic(kaddr
);
5894 * a bit scary, this does extent mapping from logical file offset to the disk.
5895 * the ugly parts come from merging extents from the disk with the in-ram
5896 * representation. This gets more complex because of the data=ordered code,
5897 * where the in-ram extents might be locked pending data=ordered completion.
5899 * This also copies inline extents directly into the page.
5902 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
5903 size_t pg_offset
, u64 start
, u64 len
,
5909 u64 extent_start
= 0;
5911 u64 objectid
= btrfs_ino(inode
);
5913 struct btrfs_path
*path
= NULL
;
5914 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5915 struct btrfs_file_extent_item
*item
;
5916 struct extent_buffer
*leaf
;
5917 struct btrfs_key found_key
;
5918 struct extent_map
*em
= NULL
;
5919 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5920 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5921 struct btrfs_trans_handle
*trans
= NULL
;
5925 read_lock(&em_tree
->lock
);
5926 em
= lookup_extent_mapping(em_tree
, start
, len
);
5928 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5929 read_unlock(&em_tree
->lock
);
5932 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
5933 free_extent_map(em
);
5934 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5935 free_extent_map(em
);
5939 em
= alloc_extent_map();
5944 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5945 em
->start
= EXTENT_MAP_HOLE
;
5946 em
->orig_start
= EXTENT_MAP_HOLE
;
5948 em
->block_len
= (u64
)-1;
5951 path
= btrfs_alloc_path();
5957 * Chances are we'll be called again, so go ahead and do
5963 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5964 objectid
, start
, trans
!= NULL
);
5971 if (path
->slots
[0] == 0)
5976 leaf
= path
->nodes
[0];
5977 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5978 struct btrfs_file_extent_item
);
5979 /* are we inside the extent that was found? */
5980 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5981 found_type
= btrfs_key_type(&found_key
);
5982 if (found_key
.objectid
!= objectid
||
5983 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5985 * If we backup past the first extent we want to move forward
5986 * and see if there is an extent in front of us, otherwise we'll
5987 * say there is a hole for our whole search range which can
5994 found_type
= btrfs_file_extent_type(leaf
, item
);
5995 extent_start
= found_key
.offset
;
5996 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5997 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5998 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5999 extent_end
= extent_start
+
6000 btrfs_file_extent_num_bytes(leaf
, item
);
6001 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6003 size
= btrfs_file_extent_inline_len(leaf
, item
);
6004 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6007 if (start
>= extent_end
) {
6009 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6010 ret
= btrfs_next_leaf(root
, path
);
6017 leaf
= path
->nodes
[0];
6019 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6020 if (found_key
.objectid
!= objectid
||
6021 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6023 if (start
+ len
<= found_key
.offset
)
6026 em
->orig_start
= start
;
6027 em
->len
= found_key
.offset
- start
;
6031 em
->ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, item
);
6032 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6033 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6034 em
->start
= extent_start
;
6035 em
->len
= extent_end
- extent_start
;
6036 em
->orig_start
= extent_start
-
6037 btrfs_file_extent_offset(leaf
, item
);
6038 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
6040 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
6042 em
->block_start
= EXTENT_MAP_HOLE
;
6045 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
6046 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6047 em
->compress_type
= compress_type
;
6048 em
->block_start
= bytenr
;
6049 em
->block_len
= em
->orig_block_len
;
6051 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
6052 em
->block_start
= bytenr
;
6053 em
->block_len
= em
->len
;
6054 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
6055 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6058 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6062 size_t extent_offset
;
6065 em
->block_start
= EXTENT_MAP_INLINE
;
6066 if (!page
|| create
) {
6067 em
->start
= extent_start
;
6068 em
->len
= extent_end
- extent_start
;
6072 size
= btrfs_file_extent_inline_len(leaf
, item
);
6073 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6074 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6075 size
- extent_offset
);
6076 em
->start
= extent_start
+ extent_offset
;
6077 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6078 em
->orig_block_len
= em
->len
;
6079 em
->orig_start
= em
->start
;
6080 if (compress_type
) {
6081 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6082 em
->compress_type
= compress_type
;
6084 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6085 if (create
== 0 && !PageUptodate(page
)) {
6086 if (btrfs_file_extent_compression(leaf
, item
) !=
6087 BTRFS_COMPRESS_NONE
) {
6088 ret
= uncompress_inline(path
, inode
, page
,
6090 extent_offset
, item
);
6091 BUG_ON(ret
); /* -ENOMEM */
6094 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6096 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6097 memset(map
+ pg_offset
+ copy_size
, 0,
6098 PAGE_CACHE_SIZE
- pg_offset
-
6103 flush_dcache_page(page
);
6104 } else if (create
&& PageUptodate(page
)) {
6108 free_extent_map(em
);
6111 btrfs_release_path(path
);
6112 trans
= btrfs_join_transaction(root
);
6115 return ERR_CAST(trans
);
6119 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6122 btrfs_mark_buffer_dirty(leaf
);
6124 set_extent_uptodate(io_tree
, em
->start
,
6125 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6128 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
6132 em
->orig_start
= start
;
6135 em
->block_start
= EXTENT_MAP_HOLE
;
6136 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6138 btrfs_release_path(path
);
6139 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6140 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6141 em
->start
, em
->len
, start
, len
);
6147 write_lock(&em_tree
->lock
);
6148 ret
= add_extent_mapping(em_tree
, em
, 0);
6149 /* it is possible that someone inserted the extent into the tree
6150 * while we had the lock dropped. It is also possible that
6151 * an overlapping map exists in the tree
6153 if (ret
== -EEXIST
) {
6154 struct extent_map
*existing
;
6158 existing
= lookup_extent_mapping(em_tree
, start
, len
);
6159 if (existing
&& (existing
->start
> start
||
6160 existing
->start
+ existing
->len
<= start
)) {
6161 free_extent_map(existing
);
6165 existing
= lookup_extent_mapping(em_tree
, em
->start
,
6168 err
= merge_extent_mapping(em_tree
, existing
,
6171 free_extent_map(existing
);
6173 free_extent_map(em
);
6178 free_extent_map(em
);
6182 free_extent_map(em
);
6187 write_unlock(&em_tree
->lock
);
6190 trace_btrfs_get_extent(root
, em
);
6193 btrfs_free_path(path
);
6195 ret
= btrfs_end_transaction(trans
, root
);
6200 free_extent_map(em
);
6201 return ERR_PTR(err
);
6203 BUG_ON(!em
); /* Error is always set */
6207 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6208 size_t pg_offset
, u64 start
, u64 len
,
6211 struct extent_map
*em
;
6212 struct extent_map
*hole_em
= NULL
;
6213 u64 range_start
= start
;
6219 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6226 * - a pre-alloc extent,
6227 * there might actually be delalloc bytes behind it.
6229 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6230 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6236 /* check to see if we've wrapped (len == -1 or similar) */
6245 /* ok, we didn't find anything, lets look for delalloc */
6246 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6247 end
, len
, EXTENT_DELALLOC
, 1);
6248 found_end
= range_start
+ found
;
6249 if (found_end
< range_start
)
6250 found_end
= (u64
)-1;
6253 * we didn't find anything useful, return
6254 * the original results from get_extent()
6256 if (range_start
> end
|| found_end
<= start
) {
6262 /* adjust the range_start to make sure it doesn't
6263 * go backwards from the start they passed in
6265 range_start
= max(start
, range_start
);
6266 found
= found_end
- range_start
;
6269 u64 hole_start
= start
;
6272 em
= alloc_extent_map();
6278 * when btrfs_get_extent can't find anything it
6279 * returns one huge hole
6281 * make sure what it found really fits our range, and
6282 * adjust to make sure it is based on the start from
6286 u64 calc_end
= extent_map_end(hole_em
);
6288 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6289 free_extent_map(hole_em
);
6292 hole_start
= max(hole_em
->start
, start
);
6293 hole_len
= calc_end
- hole_start
;
6297 if (hole_em
&& range_start
> hole_start
) {
6298 /* our hole starts before our delalloc, so we
6299 * have to return just the parts of the hole
6300 * that go until the delalloc starts
6302 em
->len
= min(hole_len
,
6303 range_start
- hole_start
);
6304 em
->start
= hole_start
;
6305 em
->orig_start
= hole_start
;
6307 * don't adjust block start at all,
6308 * it is fixed at EXTENT_MAP_HOLE
6310 em
->block_start
= hole_em
->block_start
;
6311 em
->block_len
= hole_len
;
6312 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6313 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6315 em
->start
= range_start
;
6317 em
->orig_start
= range_start
;
6318 em
->block_start
= EXTENT_MAP_DELALLOC
;
6319 em
->block_len
= found
;
6321 } else if (hole_em
) {
6326 free_extent_map(hole_em
);
6328 free_extent_map(em
);
6329 return ERR_PTR(err
);
6334 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6337 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6338 struct extent_map
*em
;
6339 struct btrfs_key ins
;
6343 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6344 ret
= btrfs_reserve_extent(root
, len
, root
->sectorsize
, 0,
6345 alloc_hint
, &ins
, 1);
6347 return ERR_PTR(ret
);
6349 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6350 ins
.offset
, ins
.offset
, ins
.offset
, 0);
6352 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6356 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6357 ins
.offset
, ins
.offset
, 0);
6359 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6360 free_extent_map(em
);
6361 return ERR_PTR(ret
);
6368 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6369 * block must be cow'd
6371 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
6372 u64
*orig_start
, u64
*orig_block_len
,
6375 struct btrfs_trans_handle
*trans
;
6376 struct btrfs_path
*path
;
6378 struct extent_buffer
*leaf
;
6379 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6380 struct btrfs_file_extent_item
*fi
;
6381 struct btrfs_key key
;
6388 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
6389 path
= btrfs_alloc_path();
6393 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, btrfs_ino(inode
),
6398 slot
= path
->slots
[0];
6401 /* can't find the item, must cow */
6408 leaf
= path
->nodes
[0];
6409 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6410 if (key
.objectid
!= btrfs_ino(inode
) ||
6411 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6412 /* not our file or wrong item type, must cow */
6416 if (key
.offset
> offset
) {
6417 /* Wrong offset, must cow */
6421 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6422 found_type
= btrfs_file_extent_type(leaf
, fi
);
6423 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6424 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6425 /* not a regular extent, must cow */
6429 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
6432 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6433 if (disk_bytenr
== 0)
6436 if (btrfs_file_extent_compression(leaf
, fi
) ||
6437 btrfs_file_extent_encryption(leaf
, fi
) ||
6438 btrfs_file_extent_other_encoding(leaf
, fi
))
6441 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6444 *orig_start
= key
.offset
- backref_offset
;
6445 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
6446 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
6449 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6451 if (btrfs_extent_readonly(root
, disk_bytenr
))
6453 btrfs_release_path(path
);
6456 * look for other files referencing this extent, if we
6457 * find any we must cow
6459 trans
= btrfs_join_transaction(root
);
6460 if (IS_ERR(trans
)) {
6465 ret
= btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
6466 key
.offset
- backref_offset
, disk_bytenr
);
6467 btrfs_end_transaction(trans
, root
);
6474 * adjust disk_bytenr and num_bytes to cover just the bytes
6475 * in this extent we are about to write. If there
6476 * are any csums in that range we have to cow in order
6477 * to keep the csums correct
6479 disk_bytenr
+= backref_offset
;
6480 disk_bytenr
+= offset
- key
.offset
;
6481 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
6482 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
6485 * all of the above have passed, it is safe to overwrite this extent
6491 btrfs_free_path(path
);
6495 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
6496 struct extent_state
**cached_state
, int writing
)
6498 struct btrfs_ordered_extent
*ordered
;
6502 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6505 * We're concerned with the entire range that we're going to be
6506 * doing DIO to, so we need to make sure theres no ordered
6507 * extents in this range.
6509 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6510 lockend
- lockstart
+ 1);
6513 * We need to make sure there are no buffered pages in this
6514 * range either, we could have raced between the invalidate in
6515 * generic_file_direct_write and locking the extent. The
6516 * invalidate needs to happen so that reads after a write do not
6519 if (!ordered
&& (!writing
||
6520 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
6521 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
6525 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6526 cached_state
, GFP_NOFS
);
6529 btrfs_start_ordered_extent(inode
, ordered
, 1);
6530 btrfs_put_ordered_extent(ordered
);
6532 /* Screw you mmap */
6533 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
6540 * If we found a page that couldn't be invalidated just
6541 * fall back to buffered.
6543 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6544 lockstart
>> PAGE_CACHE_SHIFT
,
6545 lockend
>> PAGE_CACHE_SHIFT
);
6556 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
6557 u64 len
, u64 orig_start
,
6558 u64 block_start
, u64 block_len
,
6559 u64 orig_block_len
, u64 ram_bytes
,
6562 struct extent_map_tree
*em_tree
;
6563 struct extent_map
*em
;
6564 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6567 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6568 em
= alloc_extent_map();
6570 return ERR_PTR(-ENOMEM
);
6573 em
->orig_start
= orig_start
;
6574 em
->mod_start
= start
;
6577 em
->block_len
= block_len
;
6578 em
->block_start
= block_start
;
6579 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6580 em
->orig_block_len
= orig_block_len
;
6581 em
->ram_bytes
= ram_bytes
;
6582 em
->generation
= -1;
6583 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6584 if (type
== BTRFS_ORDERED_PREALLOC
)
6585 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6588 btrfs_drop_extent_cache(inode
, em
->start
,
6589 em
->start
+ em
->len
- 1, 0);
6590 write_lock(&em_tree
->lock
);
6591 ret
= add_extent_mapping(em_tree
, em
, 1);
6592 write_unlock(&em_tree
->lock
);
6593 } while (ret
== -EEXIST
);
6596 free_extent_map(em
);
6597 return ERR_PTR(ret
);
6604 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6605 struct buffer_head
*bh_result
, int create
)
6607 struct extent_map
*em
;
6608 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6609 struct extent_state
*cached_state
= NULL
;
6610 u64 start
= iblock
<< inode
->i_blkbits
;
6611 u64 lockstart
, lockend
;
6612 u64 len
= bh_result
->b_size
;
6613 int unlock_bits
= EXTENT_LOCKED
;
6617 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6619 len
= min_t(u64
, len
, root
->sectorsize
);
6622 lockend
= start
+ len
- 1;
6625 * If this errors out it's because we couldn't invalidate pagecache for
6626 * this range and we need to fallback to buffered.
6628 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6631 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6638 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6639 * io. INLINE is special, and we could probably kludge it in here, but
6640 * it's still buffered so for safety lets just fall back to the generic
6643 * For COMPRESSED we _have_ to read the entire extent in so we can
6644 * decompress it, so there will be buffering required no matter what we
6645 * do, so go ahead and fallback to buffered.
6647 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6648 * to buffered IO. Don't blame me, this is the price we pay for using
6651 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6652 em
->block_start
== EXTENT_MAP_INLINE
) {
6653 free_extent_map(em
);
6658 /* Just a good old fashioned hole, return */
6659 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6660 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6661 free_extent_map(em
);
6666 * We don't allocate a new extent in the following cases
6668 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6670 * 2) The extent is marked as PREALLOC. We're good to go here and can
6671 * just use the extent.
6675 len
= min(len
, em
->len
- (start
- em
->start
));
6676 lockstart
= start
+ len
;
6680 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6681 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6682 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6685 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
6687 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6688 type
= BTRFS_ORDERED_PREALLOC
;
6690 type
= BTRFS_ORDERED_NOCOW
;
6691 len
= min(len
, em
->len
- (start
- em
->start
));
6692 block_start
= em
->block_start
+ (start
- em
->start
);
6694 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
6695 &orig_block_len
, &ram_bytes
) == 1) {
6696 if (type
== BTRFS_ORDERED_PREALLOC
) {
6697 free_extent_map(em
);
6698 em
= create_pinned_em(inode
, start
, len
,
6707 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6708 block_start
, len
, len
, type
);
6710 free_extent_map(em
);
6718 * this will cow the extent, reset the len in case we changed
6721 len
= bh_result
->b_size
;
6722 free_extent_map(em
);
6723 em
= btrfs_new_extent_direct(inode
, start
, len
);
6728 len
= min(len
, em
->len
- (start
- em
->start
));
6730 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6732 bh_result
->b_size
= len
;
6733 bh_result
->b_bdev
= em
->bdev
;
6734 set_buffer_mapped(bh_result
);
6736 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6737 set_buffer_new(bh_result
);
6740 * Need to update the i_size under the extent lock so buffered
6741 * readers will get the updated i_size when we unlock.
6743 if (start
+ len
> i_size_read(inode
))
6744 i_size_write(inode
, start
+ len
);
6746 spin_lock(&BTRFS_I(inode
)->lock
);
6747 BTRFS_I(inode
)->outstanding_extents
++;
6748 spin_unlock(&BTRFS_I(inode
)->lock
);
6750 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6751 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
6752 &cached_state
, GFP_NOFS
);
6757 * In the case of write we need to clear and unlock the entire range,
6758 * in the case of read we need to unlock only the end area that we
6759 * aren't using if there is any left over space.
6761 if (lockstart
< lockend
) {
6762 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6763 lockend
, unlock_bits
, 1, 0,
6764 &cached_state
, GFP_NOFS
);
6766 free_extent_state(cached_state
);
6769 free_extent_map(em
);
6774 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6775 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6779 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6781 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6782 struct bio_vec
*bvec
;
6783 struct inode
*inode
= dip
->inode
;
6784 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6785 struct bio
*dio_bio
;
6786 u32
*csums
= (u32
*)dip
->csum
;
6790 start
= dip
->logical_offset
;
6791 bio_for_each_segment_all(bvec
, bio
, i
) {
6792 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6793 struct page
*page
= bvec
->bv_page
;
6796 unsigned long flags
;
6798 local_irq_save(flags
);
6799 kaddr
= kmap_atomic(page
);
6800 csum
= btrfs_csum_data(kaddr
+ bvec
->bv_offset
,
6801 csum
, bvec
->bv_len
);
6802 btrfs_csum_final(csum
, (char *)&csum
);
6803 kunmap_atomic(kaddr
);
6804 local_irq_restore(flags
);
6806 flush_dcache_page(bvec
->bv_page
);
6807 if (csum
!= csums
[i
]) {
6808 btrfs_err(root
->fs_info
, "csum failed ino %llu off %llu csum %u expected csum %u",
6809 btrfs_ino(inode
), start
, csum
,
6815 start
+= bvec
->bv_len
;
6818 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6819 dip
->logical_offset
+ dip
->bytes
- 1);
6820 dio_bio
= dip
->dio_bio
;
6824 /* If we had a csum failure make sure to clear the uptodate flag */
6826 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
6827 dio_end_io(dio_bio
, err
);
6831 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6833 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6834 struct inode
*inode
= dip
->inode
;
6835 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6836 struct btrfs_ordered_extent
*ordered
= NULL
;
6837 u64 ordered_offset
= dip
->logical_offset
;
6838 u64 ordered_bytes
= dip
->bytes
;
6839 struct bio
*dio_bio
;
6845 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
6847 ordered_bytes
, !err
);
6851 ordered
->work
.func
= finish_ordered_fn
;
6852 ordered
->work
.flags
= 0;
6853 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
6857 * our bio might span multiple ordered extents. If we haven't
6858 * completed the accounting for the whole dio, go back and try again
6860 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
6861 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
6867 dio_bio
= dip
->dio_bio
;
6871 /* If we had an error make sure to clear the uptodate flag */
6873 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
6874 dio_end_io(dio_bio
, err
);
6878 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
6879 struct bio
*bio
, int mirror_num
,
6880 unsigned long bio_flags
, u64 offset
)
6883 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6884 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
6885 BUG_ON(ret
); /* -ENOMEM */
6889 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
6891 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6894 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
6895 "sector %#Lx len %u err no %d\n",
6896 btrfs_ino(dip
->inode
), bio
->bi_rw
,
6897 (unsigned long long)bio
->bi_iter
.bi_sector
,
6898 bio
->bi_iter
.bi_size
, err
);
6902 * before atomic variable goto zero, we must make sure
6903 * dip->errors is perceived to be set.
6905 smp_mb__before_atomic_dec();
6908 /* if there are more bios still pending for this dio, just exit */
6909 if (!atomic_dec_and_test(&dip
->pending_bios
))
6913 bio_io_error(dip
->orig_bio
);
6915 set_bit(BIO_UPTODATE
, &dip
->dio_bio
->bi_flags
);
6916 bio_endio(dip
->orig_bio
, 0);
6922 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
6923 u64 first_sector
, gfp_t gfp_flags
)
6925 int nr_vecs
= bio_get_nr_vecs(bdev
);
6926 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
6929 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
6930 int rw
, u64 file_offset
, int skip_sum
,
6933 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6934 int write
= rw
& REQ_WRITE
;
6935 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6939 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
6944 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
6952 if (write
&& async_submit
) {
6953 ret
= btrfs_wq_submit_bio(root
->fs_info
,
6954 inode
, rw
, bio
, 0, 0,
6956 __btrfs_submit_bio_start_direct_io
,
6957 __btrfs_submit_bio_done
);
6961 * If we aren't doing async submit, calculate the csum of the
6964 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
6967 } else if (!skip_sum
) {
6968 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, dip
, bio
,
6975 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
6981 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
6984 struct inode
*inode
= dip
->inode
;
6985 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6987 struct bio
*orig_bio
= dip
->orig_bio
;
6988 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
6989 u64 start_sector
= orig_bio
->bi_iter
.bi_sector
;
6990 u64 file_offset
= dip
->logical_offset
;
6995 int async_submit
= 0;
6997 map_length
= orig_bio
->bi_iter
.bi_size
;
6998 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
6999 &map_length
, NULL
, 0);
7005 if (map_length
>= orig_bio
->bi_iter
.bi_size
) {
7010 /* async crcs make it difficult to collect full stripe writes. */
7011 if (btrfs_get_alloc_profile(root
, 1) &
7012 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
7017 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7020 bio
->bi_private
= dip
;
7021 bio
->bi_end_io
= btrfs_end_dio_bio
;
7022 atomic_inc(&dip
->pending_bios
);
7024 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7025 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
7026 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7027 bvec
->bv_offset
) < bvec
->bv_len
)) {
7029 * inc the count before we submit the bio so
7030 * we know the end IO handler won't happen before
7031 * we inc the count. Otherwise, the dip might get freed
7032 * before we're done setting it up
7034 atomic_inc(&dip
->pending_bios
);
7035 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7036 file_offset
, skip_sum
,
7040 atomic_dec(&dip
->pending_bios
);
7044 start_sector
+= submit_len
>> 9;
7045 file_offset
+= submit_len
;
7050 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7051 start_sector
, GFP_NOFS
);
7054 bio
->bi_private
= dip
;
7055 bio
->bi_end_io
= btrfs_end_dio_bio
;
7057 map_length
= orig_bio
->bi_iter
.bi_size
;
7058 ret
= btrfs_map_block(root
->fs_info
, rw
,
7060 &map_length
, NULL
, 0);
7066 submit_len
+= bvec
->bv_len
;
7073 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7082 * before atomic variable goto zero, we must
7083 * make sure dip->errors is perceived to be set.
7085 smp_mb__before_atomic_dec();
7086 if (atomic_dec_and_test(&dip
->pending_bios
))
7087 bio_io_error(dip
->orig_bio
);
7089 /* bio_end_io() will handle error, so we needn't return it */
7093 static void btrfs_submit_direct(int rw
, struct bio
*dio_bio
,
7094 struct inode
*inode
, loff_t file_offset
)
7096 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7097 struct btrfs_dio_private
*dip
;
7101 int write
= rw
& REQ_WRITE
;
7105 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7107 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
7113 if (!skip_sum
&& !write
) {
7114 csum_size
= btrfs_super_csum_size(root
->fs_info
->super_copy
);
7115 sum_len
= dio_bio
->bi_iter
.bi_size
>>
7116 inode
->i_sb
->s_blocksize_bits
;
7117 sum_len
*= csum_size
;
7122 dip
= kmalloc(sizeof(*dip
) + sum_len
, GFP_NOFS
);
7128 dip
->private = dio_bio
->bi_private
;
7130 dip
->logical_offset
= file_offset
;
7131 dip
->bytes
= dio_bio
->bi_iter
.bi_size
;
7132 dip
->disk_bytenr
= (u64
)dio_bio
->bi_iter
.bi_sector
<< 9;
7133 io_bio
->bi_private
= dip
;
7135 dip
->orig_bio
= io_bio
;
7136 dip
->dio_bio
= dio_bio
;
7137 atomic_set(&dip
->pending_bios
, 0);
7140 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
7142 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
7144 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
7153 * If this is a write, we need to clean up the reserved space and kill
7154 * the ordered extent.
7157 struct btrfs_ordered_extent
*ordered
;
7158 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
7159 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
7160 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
7161 btrfs_free_reserved_extent(root
, ordered
->start
,
7163 btrfs_put_ordered_extent(ordered
);
7164 btrfs_put_ordered_extent(ordered
);
7166 bio_endio(dio_bio
, ret
);
7169 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
7170 const struct iovec
*iov
, loff_t offset
,
7171 unsigned long nr_segs
)
7177 unsigned blocksize_mask
= root
->sectorsize
- 1;
7178 ssize_t retval
= -EINVAL
;
7179 loff_t end
= offset
;
7181 if (offset
& blocksize_mask
)
7184 /* Check the memory alignment. Blocks cannot straddle pages */
7185 for (seg
= 0; seg
< nr_segs
; seg
++) {
7186 addr
= (unsigned long)iov
[seg
].iov_base
;
7187 size
= iov
[seg
].iov_len
;
7189 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
7192 /* If this is a write we don't need to check anymore */
7197 * Check to make sure we don't have duplicate iov_base's in this
7198 * iovec, if so return EINVAL, otherwise we'll get csum errors
7199 * when reading back.
7201 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
7202 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
7211 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
7212 const struct iovec
*iov
, loff_t offset
,
7213 unsigned long nr_segs
)
7215 struct file
*file
= iocb
->ki_filp
;
7216 struct inode
*inode
= file
->f_mapping
->host
;
7220 bool relock
= false;
7223 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
7227 atomic_inc(&inode
->i_dio_count
);
7228 smp_mb__after_atomic_inc();
7231 * The generic stuff only does filemap_write_and_wait_range, which isn't
7232 * enough if we've written compressed pages to this area, so we need to
7233 * call btrfs_wait_ordered_range to make absolutely sure that any
7234 * outstanding dirty pages are on disk.
7236 count
= iov_length(iov
, nr_segs
);
7237 ret
= btrfs_wait_ordered_range(inode
, offset
, count
);
7243 * If the write DIO is beyond the EOF, we need update
7244 * the isize, but it is protected by i_mutex. So we can
7245 * not unlock the i_mutex at this case.
7247 if (offset
+ count
<= inode
->i_size
) {
7248 mutex_unlock(&inode
->i_mutex
);
7251 ret
= btrfs_delalloc_reserve_space(inode
, count
);
7254 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7255 &BTRFS_I(inode
)->runtime_flags
))) {
7256 inode_dio_done(inode
);
7257 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7261 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
7262 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
7263 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
7264 btrfs_submit_direct
, flags
);
7266 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
7267 btrfs_delalloc_release_space(inode
, count
);
7268 else if (ret
>= 0 && (size_t)ret
< count
)
7269 btrfs_delalloc_release_space(inode
,
7270 count
- (size_t)ret
);
7272 btrfs_delalloc_release_metadata(inode
, 0);
7276 inode_dio_done(inode
);
7278 mutex_lock(&inode
->i_mutex
);
7283 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7285 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
7286 __u64 start
, __u64 len
)
7290 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
7294 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
7297 int btrfs_readpage(struct file
*file
, struct page
*page
)
7299 struct extent_io_tree
*tree
;
7300 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7301 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
7304 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
7306 struct extent_io_tree
*tree
;
7309 if (current
->flags
& PF_MEMALLOC
) {
7310 redirty_page_for_writepage(wbc
, page
);
7314 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7315 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
7318 static int btrfs_writepages(struct address_space
*mapping
,
7319 struct writeback_control
*wbc
)
7321 struct extent_io_tree
*tree
;
7323 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7324 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
7328 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
7329 struct list_head
*pages
, unsigned nr_pages
)
7331 struct extent_io_tree
*tree
;
7332 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7333 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
7336 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7338 struct extent_io_tree
*tree
;
7339 struct extent_map_tree
*map
;
7342 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7343 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
7344 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
7346 ClearPagePrivate(page
);
7347 set_page_private(page
, 0);
7348 page_cache_release(page
);
7353 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7355 if (PageWriteback(page
) || PageDirty(page
))
7357 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
7360 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
7361 unsigned int length
)
7363 struct inode
*inode
= page
->mapping
->host
;
7364 struct extent_io_tree
*tree
;
7365 struct btrfs_ordered_extent
*ordered
;
7366 struct extent_state
*cached_state
= NULL
;
7367 u64 page_start
= page_offset(page
);
7368 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7371 * we have the page locked, so new writeback can't start,
7372 * and the dirty bit won't be cleared while we are here.
7374 * Wait for IO on this page so that we can safely clear
7375 * the PagePrivate2 bit and do ordered accounting
7377 wait_on_page_writeback(page
);
7379 tree
= &BTRFS_I(inode
)->io_tree
;
7381 btrfs_releasepage(page
, GFP_NOFS
);
7384 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7385 ordered
= btrfs_lookup_ordered_extent(inode
, page_offset(page
));
7388 * IO on this page will never be started, so we need
7389 * to account for any ordered extents now
7391 clear_extent_bit(tree
, page_start
, page_end
,
7392 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7393 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
7394 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
7396 * whoever cleared the private bit is responsible
7397 * for the finish_ordered_io
7399 if (TestClearPagePrivate2(page
)) {
7400 struct btrfs_ordered_inode_tree
*tree
;
7403 tree
= &BTRFS_I(inode
)->ordered_tree
;
7405 spin_lock_irq(&tree
->lock
);
7406 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
7407 new_len
= page_start
- ordered
->file_offset
;
7408 if (new_len
< ordered
->truncated_len
)
7409 ordered
->truncated_len
= new_len
;
7410 spin_unlock_irq(&tree
->lock
);
7412 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
7414 PAGE_CACHE_SIZE
, 1))
7415 btrfs_finish_ordered_io(ordered
);
7417 btrfs_put_ordered_extent(ordered
);
7418 cached_state
= NULL
;
7419 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7421 clear_extent_bit(tree
, page_start
, page_end
,
7422 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
7423 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
7424 &cached_state
, GFP_NOFS
);
7425 __btrfs_releasepage(page
, GFP_NOFS
);
7427 ClearPageChecked(page
);
7428 if (PagePrivate(page
)) {
7429 ClearPagePrivate(page
);
7430 set_page_private(page
, 0);
7431 page_cache_release(page
);
7436 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7437 * called from a page fault handler when a page is first dirtied. Hence we must
7438 * be careful to check for EOF conditions here. We set the page up correctly
7439 * for a written page which means we get ENOSPC checking when writing into
7440 * holes and correct delalloc and unwritten extent mapping on filesystems that
7441 * support these features.
7443 * We are not allowed to take the i_mutex here so we have to play games to
7444 * protect against truncate races as the page could now be beyond EOF. Because
7445 * vmtruncate() writes the inode size before removing pages, once we have the
7446 * page lock we can determine safely if the page is beyond EOF. If it is not
7447 * beyond EOF, then the page is guaranteed safe against truncation until we
7450 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
7452 struct page
*page
= vmf
->page
;
7453 struct inode
*inode
= file_inode(vma
->vm_file
);
7454 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7455 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7456 struct btrfs_ordered_extent
*ordered
;
7457 struct extent_state
*cached_state
= NULL
;
7459 unsigned long zero_start
;
7466 sb_start_pagefault(inode
->i_sb
);
7467 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
7469 ret
= file_update_time(vma
->vm_file
);
7475 else /* -ENOSPC, -EIO, etc */
7476 ret
= VM_FAULT_SIGBUS
;
7482 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
7485 size
= i_size_read(inode
);
7486 page_start
= page_offset(page
);
7487 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7489 if ((page
->mapping
!= inode
->i_mapping
) ||
7490 (page_start
>= size
)) {
7491 /* page got truncated out from underneath us */
7494 wait_on_page_writeback(page
);
7496 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
7497 set_page_extent_mapped(page
);
7500 * we can't set the delalloc bits if there are pending ordered
7501 * extents. Drop our locks and wait for them to finish
7503 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7505 unlock_extent_cached(io_tree
, page_start
, page_end
,
7506 &cached_state
, GFP_NOFS
);
7508 btrfs_start_ordered_extent(inode
, ordered
, 1);
7509 btrfs_put_ordered_extent(ordered
);
7514 * XXX - page_mkwrite gets called every time the page is dirtied, even
7515 * if it was already dirty, so for space accounting reasons we need to
7516 * clear any delalloc bits for the range we are fixing to save. There
7517 * is probably a better way to do this, but for now keep consistent with
7518 * prepare_pages in the normal write path.
7520 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
7521 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7522 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
7523 0, 0, &cached_state
, GFP_NOFS
);
7525 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
7528 unlock_extent_cached(io_tree
, page_start
, page_end
,
7529 &cached_state
, GFP_NOFS
);
7530 ret
= VM_FAULT_SIGBUS
;
7535 /* page is wholly or partially inside EOF */
7536 if (page_start
+ PAGE_CACHE_SIZE
> size
)
7537 zero_start
= size
& ~PAGE_CACHE_MASK
;
7539 zero_start
= PAGE_CACHE_SIZE
;
7541 if (zero_start
!= PAGE_CACHE_SIZE
) {
7543 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
7544 flush_dcache_page(page
);
7547 ClearPageChecked(page
);
7548 set_page_dirty(page
);
7549 SetPageUptodate(page
);
7551 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
7552 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
7553 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
7555 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
7559 sb_end_pagefault(inode
->i_sb
);
7560 return VM_FAULT_LOCKED
;
7564 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
7566 sb_end_pagefault(inode
->i_sb
);
7570 static int btrfs_truncate(struct inode
*inode
)
7572 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7573 struct btrfs_block_rsv
*rsv
;
7576 struct btrfs_trans_handle
*trans
;
7577 u64 mask
= root
->sectorsize
- 1;
7578 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7580 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
7586 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7587 * 3 things going on here
7589 * 1) We need to reserve space for our orphan item and the space to
7590 * delete our orphan item. Lord knows we don't want to have a dangling
7591 * orphan item because we didn't reserve space to remove it.
7593 * 2) We need to reserve space to update our inode.
7595 * 3) We need to have something to cache all the space that is going to
7596 * be free'd up by the truncate operation, but also have some slack
7597 * space reserved in case it uses space during the truncate (thank you
7598 * very much snapshotting).
7600 * And we need these to all be seperate. The fact is we can use alot of
7601 * space doing the truncate, and we have no earthly idea how much space
7602 * we will use, so we need the truncate reservation to be seperate so it
7603 * doesn't end up using space reserved for updating the inode or
7604 * removing the orphan item. We also need to be able to stop the
7605 * transaction and start a new one, which means we need to be able to
7606 * update the inode several times, and we have no idea of knowing how
7607 * many times that will be, so we can't just reserve 1 item for the
7608 * entirety of the opration, so that has to be done seperately as well.
7609 * Then there is the orphan item, which does indeed need to be held on
7610 * to for the whole operation, and we need nobody to touch this reserved
7611 * space except the orphan code.
7613 * So that leaves us with
7615 * 1) root->orphan_block_rsv - for the orphan deletion.
7616 * 2) rsv - for the truncate reservation, which we will steal from the
7617 * transaction reservation.
7618 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7619 * updating the inode.
7621 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7624 rsv
->size
= min_size
;
7628 * 1 for the truncate slack space
7629 * 1 for updating the inode.
7631 trans
= btrfs_start_transaction(root
, 2);
7632 if (IS_ERR(trans
)) {
7633 err
= PTR_ERR(trans
);
7637 /* Migrate the slack space for the truncate to our reserve */
7638 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7643 * setattr is responsible for setting the ordered_data_close flag,
7644 * but that is only tested during the last file release. That
7645 * could happen well after the next commit, leaving a great big
7646 * window where new writes may get lost if someone chooses to write
7647 * to this file after truncating to zero
7649 * The inode doesn't have any dirty data here, and so if we commit
7650 * this is a noop. If someone immediately starts writing to the inode
7651 * it is very likely we'll catch some of their writes in this
7652 * transaction, and the commit will find this file on the ordered
7653 * data list with good things to send down.
7655 * This is a best effort solution, there is still a window where
7656 * using truncate to replace the contents of the file will
7657 * end up with a zero length file after a crash.
7659 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7660 &BTRFS_I(inode
)->runtime_flags
))
7661 btrfs_add_ordered_operation(trans
, root
, inode
);
7664 * So if we truncate and then write and fsync we normally would just
7665 * write the extents that changed, which is a problem if we need to
7666 * first truncate that entire inode. So set this flag so we write out
7667 * all of the extents in the inode to the sync log so we're completely
7670 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7671 trans
->block_rsv
= rsv
;
7674 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7676 BTRFS_EXTENT_DATA_KEY
);
7677 if (ret
!= -ENOSPC
) {
7682 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7683 ret
= btrfs_update_inode(trans
, root
, inode
);
7689 btrfs_end_transaction(trans
, root
);
7690 btrfs_btree_balance_dirty(root
);
7692 trans
= btrfs_start_transaction(root
, 2);
7693 if (IS_ERR(trans
)) {
7694 ret
= err
= PTR_ERR(trans
);
7699 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7701 BUG_ON(ret
); /* shouldn't happen */
7702 trans
->block_rsv
= rsv
;
7705 if (ret
== 0 && inode
->i_nlink
> 0) {
7706 trans
->block_rsv
= root
->orphan_block_rsv
;
7707 ret
= btrfs_orphan_del(trans
, inode
);
7713 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7714 ret
= btrfs_update_inode(trans
, root
, inode
);
7718 ret
= btrfs_end_transaction(trans
, root
);
7719 btrfs_btree_balance_dirty(root
);
7723 btrfs_free_block_rsv(root
, rsv
);
7732 * create a new subvolume directory/inode (helper for the ioctl).
7734 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7735 struct btrfs_root
*new_root
, u64 new_dirid
)
7737 struct inode
*inode
;
7741 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7742 new_dirid
, new_dirid
,
7743 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7746 return PTR_ERR(inode
);
7747 inode
->i_op
= &btrfs_dir_inode_operations
;
7748 inode
->i_fop
= &btrfs_dir_file_operations
;
7750 set_nlink(inode
, 1);
7751 btrfs_i_size_write(inode
, 0);
7753 err
= btrfs_update_inode(trans
, new_root
, inode
);
7759 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7761 struct btrfs_inode
*ei
;
7762 struct inode
*inode
;
7764 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7771 ei
->last_sub_trans
= 0;
7772 ei
->logged_trans
= 0;
7773 ei
->delalloc_bytes
= 0;
7774 ei
->disk_i_size
= 0;
7777 ei
->index_cnt
= (u64
)-1;
7778 ei
->last_unlink_trans
= 0;
7779 ei
->last_log_commit
= 0;
7781 spin_lock_init(&ei
->lock
);
7782 ei
->outstanding_extents
= 0;
7783 ei
->reserved_extents
= 0;
7785 ei
->runtime_flags
= 0;
7786 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7788 ei
->delayed_node
= NULL
;
7790 inode
= &ei
->vfs_inode
;
7791 extent_map_tree_init(&ei
->extent_tree
);
7792 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7793 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7794 ei
->io_tree
.track_uptodate
= 1;
7795 ei
->io_failure_tree
.track_uptodate
= 1;
7796 atomic_set(&ei
->sync_writers
, 0);
7797 mutex_init(&ei
->log_mutex
);
7798 mutex_init(&ei
->delalloc_mutex
);
7799 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7800 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7801 INIT_LIST_HEAD(&ei
->ordered_operations
);
7802 RB_CLEAR_NODE(&ei
->rb_node
);
7807 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
7808 void btrfs_test_destroy_inode(struct inode
*inode
)
7810 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7811 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7815 static void btrfs_i_callback(struct rcu_head
*head
)
7817 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7818 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7821 void btrfs_destroy_inode(struct inode
*inode
)
7823 struct btrfs_ordered_extent
*ordered
;
7824 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7826 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7827 WARN_ON(inode
->i_data
.nrpages
);
7828 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7829 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7830 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7831 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7834 * This can happen where we create an inode, but somebody else also
7835 * created the same inode and we need to destroy the one we already
7842 * Make sure we're properly removed from the ordered operation
7846 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7847 spin_lock(&root
->fs_info
->ordered_root_lock
);
7848 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7849 spin_unlock(&root
->fs_info
->ordered_root_lock
);
7852 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7853 &BTRFS_I(inode
)->runtime_flags
)) {
7854 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
7856 atomic_dec(&root
->orphan_inodes
);
7860 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7864 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
7865 ordered
->file_offset
, ordered
->len
);
7866 btrfs_remove_ordered_extent(inode
, ordered
);
7867 btrfs_put_ordered_extent(ordered
);
7868 btrfs_put_ordered_extent(ordered
);
7871 inode_tree_del(inode
);
7872 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7874 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7877 int btrfs_drop_inode(struct inode
*inode
)
7879 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7884 /* the snap/subvol tree is on deleting */
7885 if (btrfs_root_refs(&root
->root_item
) == 0)
7888 return generic_drop_inode(inode
);
7891 static void init_once(void *foo
)
7893 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7895 inode_init_once(&ei
->vfs_inode
);
7898 void btrfs_destroy_cachep(void)
7901 * Make sure all delayed rcu free inodes are flushed before we
7905 if (btrfs_inode_cachep
)
7906 kmem_cache_destroy(btrfs_inode_cachep
);
7907 if (btrfs_trans_handle_cachep
)
7908 kmem_cache_destroy(btrfs_trans_handle_cachep
);
7909 if (btrfs_transaction_cachep
)
7910 kmem_cache_destroy(btrfs_transaction_cachep
);
7911 if (btrfs_path_cachep
)
7912 kmem_cache_destroy(btrfs_path_cachep
);
7913 if (btrfs_free_space_cachep
)
7914 kmem_cache_destroy(btrfs_free_space_cachep
);
7915 if (btrfs_delalloc_work_cachep
)
7916 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
7919 int btrfs_init_cachep(void)
7921 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
7922 sizeof(struct btrfs_inode
), 0,
7923 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
7924 if (!btrfs_inode_cachep
)
7927 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
7928 sizeof(struct btrfs_trans_handle
), 0,
7929 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7930 if (!btrfs_trans_handle_cachep
)
7933 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
7934 sizeof(struct btrfs_transaction
), 0,
7935 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7936 if (!btrfs_transaction_cachep
)
7939 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
7940 sizeof(struct btrfs_path
), 0,
7941 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7942 if (!btrfs_path_cachep
)
7945 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
7946 sizeof(struct btrfs_free_space
), 0,
7947 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7948 if (!btrfs_free_space_cachep
)
7951 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
7952 sizeof(struct btrfs_delalloc_work
), 0,
7953 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
7955 if (!btrfs_delalloc_work_cachep
)
7960 btrfs_destroy_cachep();
7964 static int btrfs_getattr(struct vfsmount
*mnt
,
7965 struct dentry
*dentry
, struct kstat
*stat
)
7968 struct inode
*inode
= dentry
->d_inode
;
7969 u32 blocksize
= inode
->i_sb
->s_blocksize
;
7971 generic_fillattr(inode
, stat
);
7972 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
7973 stat
->blksize
= PAGE_CACHE_SIZE
;
7975 spin_lock(&BTRFS_I(inode
)->lock
);
7976 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
7977 spin_unlock(&BTRFS_I(inode
)->lock
);
7978 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
7979 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
7983 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
7984 struct inode
*new_dir
, struct dentry
*new_dentry
)
7986 struct btrfs_trans_handle
*trans
;
7987 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
7988 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
7989 struct inode
*new_inode
= new_dentry
->d_inode
;
7990 struct inode
*old_inode
= old_dentry
->d_inode
;
7991 struct timespec ctime
= CURRENT_TIME
;
7995 u64 old_ino
= btrfs_ino(old_inode
);
7997 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
8000 /* we only allow rename subvolume link between subvolumes */
8001 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8004 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
8005 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
8008 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
8009 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
8013 /* check for collisions, even if the name isn't there */
8014 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
8015 new_dentry
->d_name
.name
,
8016 new_dentry
->d_name
.len
);
8019 if (ret
== -EEXIST
) {
8021 * eexist without a new_inode */
8022 if (WARN_ON(!new_inode
)) {
8026 /* maybe -EOVERFLOW */
8033 * we're using rename to replace one file with another.
8034 * and the replacement file is large. Start IO on it now so
8035 * we don't add too much work to the end of the transaction
8037 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
8038 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
8039 filemap_flush(old_inode
->i_mapping
);
8041 /* close the racy window with snapshot create/destroy ioctl */
8042 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8043 down_read(&root
->fs_info
->subvol_sem
);
8045 * We want to reserve the absolute worst case amount of items. So if
8046 * both inodes are subvols and we need to unlink them then that would
8047 * require 4 item modifications, but if they are both normal inodes it
8048 * would require 5 item modifications, so we'll assume their normal
8049 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8050 * should cover the worst case number of items we'll modify.
8052 trans
= btrfs_start_transaction(root
, 11);
8053 if (IS_ERR(trans
)) {
8054 ret
= PTR_ERR(trans
);
8059 btrfs_record_root_in_trans(trans
, dest
);
8061 ret
= btrfs_set_inode_index(new_dir
, &index
);
8065 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8066 /* force full log commit if subvolume involved. */
8067 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8069 ret
= btrfs_insert_inode_ref(trans
, dest
,
8070 new_dentry
->d_name
.name
,
8071 new_dentry
->d_name
.len
,
8073 btrfs_ino(new_dir
), index
);
8077 * this is an ugly little race, but the rename is required
8078 * to make sure that if we crash, the inode is either at the
8079 * old name or the new one. pinning the log transaction lets
8080 * us make sure we don't allow a log commit to come in after
8081 * we unlink the name but before we add the new name back in.
8083 btrfs_pin_log_trans(root
);
8086 * make sure the inode gets flushed if it is replacing
8089 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
8090 btrfs_add_ordered_operation(trans
, root
, old_inode
);
8092 inode_inc_iversion(old_dir
);
8093 inode_inc_iversion(new_dir
);
8094 inode_inc_iversion(old_inode
);
8095 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8096 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8097 old_inode
->i_ctime
= ctime
;
8099 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8100 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8102 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8103 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8104 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8105 old_dentry
->d_name
.name
,
8106 old_dentry
->d_name
.len
);
8108 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8109 old_dentry
->d_inode
,
8110 old_dentry
->d_name
.name
,
8111 old_dentry
->d_name
.len
);
8113 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8116 btrfs_abort_transaction(trans
, root
, ret
);
8121 inode_inc_iversion(new_inode
);
8122 new_inode
->i_ctime
= CURRENT_TIME
;
8123 if (unlikely(btrfs_ino(new_inode
) ==
8124 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8125 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8126 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
8128 new_dentry
->d_name
.name
,
8129 new_dentry
->d_name
.len
);
8130 BUG_ON(new_inode
->i_nlink
== 0);
8132 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
8133 new_dentry
->d_inode
,
8134 new_dentry
->d_name
.name
,
8135 new_dentry
->d_name
.len
);
8137 if (!ret
&& new_inode
->i_nlink
== 0)
8138 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
8140 btrfs_abort_transaction(trans
, root
, ret
);
8145 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
8146 new_dentry
->d_name
.name
,
8147 new_dentry
->d_name
.len
, 0, index
);
8149 btrfs_abort_transaction(trans
, root
, ret
);
8153 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
8154 struct dentry
*parent
= new_dentry
->d_parent
;
8155 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
8156 btrfs_end_log_trans(root
);
8159 btrfs_end_transaction(trans
, root
);
8161 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8162 up_read(&root
->fs_info
->subvol_sem
);
8167 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
8169 struct btrfs_delalloc_work
*delalloc_work
;
8170 struct inode
*inode
;
8172 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
8174 inode
= delalloc_work
->inode
;
8175 if (delalloc_work
->wait
) {
8176 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
8178 filemap_flush(inode
->i_mapping
);
8179 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
8180 &BTRFS_I(inode
)->runtime_flags
))
8181 filemap_flush(inode
->i_mapping
);
8184 if (delalloc_work
->delay_iput
)
8185 btrfs_add_delayed_iput(inode
);
8188 complete(&delalloc_work
->completion
);
8191 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
8192 int wait
, int delay_iput
)
8194 struct btrfs_delalloc_work
*work
;
8196 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
8200 init_completion(&work
->completion
);
8201 INIT_LIST_HEAD(&work
->list
);
8202 work
->inode
= inode
;
8204 work
->delay_iput
= delay_iput
;
8205 work
->work
.func
= btrfs_run_delalloc_work
;
8210 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
8212 wait_for_completion(&work
->completion
);
8213 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
8217 * some fairly slow code that needs optimization. This walks the list
8218 * of all the inodes with pending delalloc and forces them to disk.
8220 static int __start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8222 struct btrfs_inode
*binode
;
8223 struct inode
*inode
;
8224 struct btrfs_delalloc_work
*work
, *next
;
8225 struct list_head works
;
8226 struct list_head splice
;
8229 INIT_LIST_HEAD(&works
);
8230 INIT_LIST_HEAD(&splice
);
8232 spin_lock(&root
->delalloc_lock
);
8233 list_splice_init(&root
->delalloc_inodes
, &splice
);
8234 while (!list_empty(&splice
)) {
8235 binode
= list_entry(splice
.next
, struct btrfs_inode
,
8238 list_move_tail(&binode
->delalloc_inodes
,
8239 &root
->delalloc_inodes
);
8240 inode
= igrab(&binode
->vfs_inode
);
8242 cond_resched_lock(&root
->delalloc_lock
);
8245 spin_unlock(&root
->delalloc_lock
);
8247 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
8248 if (unlikely(!work
)) {
8250 btrfs_add_delayed_iput(inode
);
8256 list_add_tail(&work
->list
, &works
);
8257 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
8261 spin_lock(&root
->delalloc_lock
);
8263 spin_unlock(&root
->delalloc_lock
);
8265 list_for_each_entry_safe(work
, next
, &works
, list
) {
8266 list_del_init(&work
->list
);
8267 btrfs_wait_and_free_delalloc_work(work
);
8271 list_for_each_entry_safe(work
, next
, &works
, list
) {
8272 list_del_init(&work
->list
);
8273 btrfs_wait_and_free_delalloc_work(work
);
8276 if (!list_empty_careful(&splice
)) {
8277 spin_lock(&root
->delalloc_lock
);
8278 list_splice_tail(&splice
, &root
->delalloc_inodes
);
8279 spin_unlock(&root
->delalloc_lock
);
8284 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8288 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
8291 ret
= __start_delalloc_inodes(root
, delay_iput
);
8293 * the filemap_flush will queue IO into the worker threads, but
8294 * we have to make sure the IO is actually started and that
8295 * ordered extents get created before we return
8297 atomic_inc(&root
->fs_info
->async_submit_draining
);
8298 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
8299 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
8300 wait_event(root
->fs_info
->async_submit_wait
,
8301 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
8302 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
8304 atomic_dec(&root
->fs_info
->async_submit_draining
);
8308 int btrfs_start_delalloc_roots(struct btrfs_fs_info
*fs_info
, int delay_iput
)
8310 struct btrfs_root
*root
;
8311 struct list_head splice
;
8314 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
8317 INIT_LIST_HEAD(&splice
);
8319 spin_lock(&fs_info
->delalloc_root_lock
);
8320 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
8321 while (!list_empty(&splice
)) {
8322 root
= list_first_entry(&splice
, struct btrfs_root
,
8324 root
= btrfs_grab_fs_root(root
);
8326 list_move_tail(&root
->delalloc_root
,
8327 &fs_info
->delalloc_roots
);
8328 spin_unlock(&fs_info
->delalloc_root_lock
);
8330 ret
= __start_delalloc_inodes(root
, delay_iput
);
8331 btrfs_put_fs_root(root
);
8335 spin_lock(&fs_info
->delalloc_root_lock
);
8337 spin_unlock(&fs_info
->delalloc_root_lock
);
8339 atomic_inc(&fs_info
->async_submit_draining
);
8340 while (atomic_read(&fs_info
->nr_async_submits
) ||
8341 atomic_read(&fs_info
->async_delalloc_pages
)) {
8342 wait_event(fs_info
->async_submit_wait
,
8343 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
8344 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
8346 atomic_dec(&fs_info
->async_submit_draining
);
8349 if (!list_empty_careful(&splice
)) {
8350 spin_lock(&fs_info
->delalloc_root_lock
);
8351 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
8352 spin_unlock(&fs_info
->delalloc_root_lock
);
8357 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
8358 const char *symname
)
8360 struct btrfs_trans_handle
*trans
;
8361 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8362 struct btrfs_path
*path
;
8363 struct btrfs_key key
;
8364 struct inode
*inode
= NULL
;
8372 struct btrfs_file_extent_item
*ei
;
8373 struct extent_buffer
*leaf
;
8375 name_len
= strlen(symname
);
8376 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
8377 return -ENAMETOOLONG
;
8380 * 2 items for inode item and ref
8381 * 2 items for dir items
8382 * 1 item for xattr if selinux is on
8384 trans
= btrfs_start_transaction(root
, 5);
8386 return PTR_ERR(trans
);
8388 err
= btrfs_find_free_ino(root
, &objectid
);
8392 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
8393 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
8394 S_IFLNK
|S_IRWXUGO
, &index
);
8395 if (IS_ERR(inode
)) {
8396 err
= PTR_ERR(inode
);
8400 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
8407 * If the active LSM wants to access the inode during
8408 * d_instantiate it needs these. Smack checks to see
8409 * if the filesystem supports xattrs by looking at the
8412 inode
->i_fop
= &btrfs_file_operations
;
8413 inode
->i_op
= &btrfs_file_inode_operations
;
8415 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
8419 inode
->i_mapping
->a_ops
= &btrfs_aops
;
8420 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8421 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
8426 path
= btrfs_alloc_path();
8432 key
.objectid
= btrfs_ino(inode
);
8434 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
8435 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
8436 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
8440 btrfs_free_path(path
);
8443 leaf
= path
->nodes
[0];
8444 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
8445 struct btrfs_file_extent_item
);
8446 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
8447 btrfs_set_file_extent_type(leaf
, ei
,
8448 BTRFS_FILE_EXTENT_INLINE
);
8449 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
8450 btrfs_set_file_extent_compression(leaf
, ei
, 0);
8451 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
8452 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
8454 ptr
= btrfs_file_extent_inline_start(ei
);
8455 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
8456 btrfs_mark_buffer_dirty(leaf
);
8457 btrfs_free_path(path
);
8459 inode
->i_op
= &btrfs_symlink_inode_operations
;
8460 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
8461 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8462 inode_set_bytes(inode
, name_len
);
8463 btrfs_i_size_write(inode
, name_len
);
8464 err
= btrfs_update_inode(trans
, root
, inode
);
8470 d_instantiate(dentry
, inode
);
8471 btrfs_end_transaction(trans
, root
);
8473 inode_dec_link_count(inode
);
8476 btrfs_btree_balance_dirty(root
);
8480 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8481 u64 start
, u64 num_bytes
, u64 min_size
,
8482 loff_t actual_len
, u64
*alloc_hint
,
8483 struct btrfs_trans_handle
*trans
)
8485 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
8486 struct extent_map
*em
;
8487 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8488 struct btrfs_key ins
;
8489 u64 cur_offset
= start
;
8493 bool own_trans
= true;
8497 while (num_bytes
> 0) {
8499 trans
= btrfs_start_transaction(root
, 3);
8500 if (IS_ERR(trans
)) {
8501 ret
= PTR_ERR(trans
);
8506 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
8507 cur_bytes
= max(cur_bytes
, min_size
);
8508 ret
= btrfs_reserve_extent(root
, cur_bytes
, min_size
, 0,
8509 *alloc_hint
, &ins
, 1);
8512 btrfs_end_transaction(trans
, root
);
8516 ret
= insert_reserved_file_extent(trans
, inode
,
8517 cur_offset
, ins
.objectid
,
8518 ins
.offset
, ins
.offset
,
8519 ins
.offset
, 0, 0, 0,
8520 BTRFS_FILE_EXTENT_PREALLOC
);
8522 btrfs_free_reserved_extent(root
, ins
.objectid
,
8524 btrfs_abort_transaction(trans
, root
, ret
);
8526 btrfs_end_transaction(trans
, root
);
8529 btrfs_drop_extent_cache(inode
, cur_offset
,
8530 cur_offset
+ ins
.offset
-1, 0);
8532 em
= alloc_extent_map();
8534 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
8535 &BTRFS_I(inode
)->runtime_flags
);
8539 em
->start
= cur_offset
;
8540 em
->orig_start
= cur_offset
;
8541 em
->len
= ins
.offset
;
8542 em
->block_start
= ins
.objectid
;
8543 em
->block_len
= ins
.offset
;
8544 em
->orig_block_len
= ins
.offset
;
8545 em
->ram_bytes
= ins
.offset
;
8546 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
8547 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
8548 em
->generation
= trans
->transid
;
8551 write_lock(&em_tree
->lock
);
8552 ret
= add_extent_mapping(em_tree
, em
, 1);
8553 write_unlock(&em_tree
->lock
);
8556 btrfs_drop_extent_cache(inode
, cur_offset
,
8557 cur_offset
+ ins
.offset
- 1,
8560 free_extent_map(em
);
8562 num_bytes
-= ins
.offset
;
8563 cur_offset
+= ins
.offset
;
8564 *alloc_hint
= ins
.objectid
+ ins
.offset
;
8566 inode_inc_iversion(inode
);
8567 inode
->i_ctime
= CURRENT_TIME
;
8568 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
8569 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
8570 (actual_len
> inode
->i_size
) &&
8571 (cur_offset
> inode
->i_size
)) {
8572 if (cur_offset
> actual_len
)
8573 i_size
= actual_len
;
8575 i_size
= cur_offset
;
8576 i_size_write(inode
, i_size
);
8577 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
8580 ret
= btrfs_update_inode(trans
, root
, inode
);
8583 btrfs_abort_transaction(trans
, root
, ret
);
8585 btrfs_end_transaction(trans
, root
);
8590 btrfs_end_transaction(trans
, root
);
8595 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8596 u64 start
, u64 num_bytes
, u64 min_size
,
8597 loff_t actual_len
, u64
*alloc_hint
)
8599 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8600 min_size
, actual_len
, alloc_hint
,
8604 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
8605 struct btrfs_trans_handle
*trans
, int mode
,
8606 u64 start
, u64 num_bytes
, u64 min_size
,
8607 loff_t actual_len
, u64
*alloc_hint
)
8609 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8610 min_size
, actual_len
, alloc_hint
, trans
);
8613 static int btrfs_set_page_dirty(struct page
*page
)
8615 return __set_page_dirty_nobuffers(page
);
8618 static int btrfs_permission(struct inode
*inode
, int mask
)
8620 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8621 umode_t mode
= inode
->i_mode
;
8623 if (mask
& MAY_WRITE
&&
8624 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
8625 if (btrfs_root_readonly(root
))
8627 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
8630 return generic_permission(inode
, mask
);
8633 static const struct inode_operations btrfs_dir_inode_operations
= {
8634 .getattr
= btrfs_getattr
,
8635 .lookup
= btrfs_lookup
,
8636 .create
= btrfs_create
,
8637 .unlink
= btrfs_unlink
,
8639 .mkdir
= btrfs_mkdir
,
8640 .rmdir
= btrfs_rmdir
,
8641 .rename
= btrfs_rename
,
8642 .symlink
= btrfs_symlink
,
8643 .setattr
= btrfs_setattr
,
8644 .mknod
= btrfs_mknod
,
8645 .setxattr
= btrfs_setxattr
,
8646 .getxattr
= btrfs_getxattr
,
8647 .listxattr
= btrfs_listxattr
,
8648 .removexattr
= btrfs_removexattr
,
8649 .permission
= btrfs_permission
,
8650 .get_acl
= btrfs_get_acl
,
8651 .update_time
= btrfs_update_time
,
8653 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
8654 .lookup
= btrfs_lookup
,
8655 .permission
= btrfs_permission
,
8656 .get_acl
= btrfs_get_acl
,
8657 .update_time
= btrfs_update_time
,
8660 static const struct file_operations btrfs_dir_file_operations
= {
8661 .llseek
= generic_file_llseek
,
8662 .read
= generic_read_dir
,
8663 .iterate
= btrfs_real_readdir
,
8664 .unlocked_ioctl
= btrfs_ioctl
,
8665 #ifdef CONFIG_COMPAT
8666 .compat_ioctl
= btrfs_ioctl
,
8668 .release
= btrfs_release_file
,
8669 .fsync
= btrfs_sync_file
,
8672 static struct extent_io_ops btrfs_extent_io_ops
= {
8673 .fill_delalloc
= run_delalloc_range
,
8674 .submit_bio_hook
= btrfs_submit_bio_hook
,
8675 .merge_bio_hook
= btrfs_merge_bio_hook
,
8676 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
8677 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
8678 .writepage_start_hook
= btrfs_writepage_start_hook
,
8679 .set_bit_hook
= btrfs_set_bit_hook
,
8680 .clear_bit_hook
= btrfs_clear_bit_hook
,
8681 .merge_extent_hook
= btrfs_merge_extent_hook
,
8682 .split_extent_hook
= btrfs_split_extent_hook
,
8686 * btrfs doesn't support the bmap operation because swapfiles
8687 * use bmap to make a mapping of extents in the file. They assume
8688 * these extents won't change over the life of the file and they
8689 * use the bmap result to do IO directly to the drive.
8691 * the btrfs bmap call would return logical addresses that aren't
8692 * suitable for IO and they also will change frequently as COW
8693 * operations happen. So, swapfile + btrfs == corruption.
8695 * For now we're avoiding this by dropping bmap.
8697 static const struct address_space_operations btrfs_aops
= {
8698 .readpage
= btrfs_readpage
,
8699 .writepage
= btrfs_writepage
,
8700 .writepages
= btrfs_writepages
,
8701 .readpages
= btrfs_readpages
,
8702 .direct_IO
= btrfs_direct_IO
,
8703 .invalidatepage
= btrfs_invalidatepage
,
8704 .releasepage
= btrfs_releasepage
,
8705 .set_page_dirty
= btrfs_set_page_dirty
,
8706 .error_remove_page
= generic_error_remove_page
,
8709 static const struct address_space_operations btrfs_symlink_aops
= {
8710 .readpage
= btrfs_readpage
,
8711 .writepage
= btrfs_writepage
,
8712 .invalidatepage
= btrfs_invalidatepage
,
8713 .releasepage
= btrfs_releasepage
,
8716 static const struct inode_operations btrfs_file_inode_operations
= {
8717 .getattr
= btrfs_getattr
,
8718 .setattr
= btrfs_setattr
,
8719 .setxattr
= btrfs_setxattr
,
8720 .getxattr
= btrfs_getxattr
,
8721 .listxattr
= btrfs_listxattr
,
8722 .removexattr
= btrfs_removexattr
,
8723 .permission
= btrfs_permission
,
8724 .fiemap
= btrfs_fiemap
,
8725 .get_acl
= btrfs_get_acl
,
8726 .update_time
= btrfs_update_time
,
8728 static const struct inode_operations btrfs_special_inode_operations
= {
8729 .getattr
= btrfs_getattr
,
8730 .setattr
= btrfs_setattr
,
8731 .permission
= btrfs_permission
,
8732 .setxattr
= btrfs_setxattr
,
8733 .getxattr
= btrfs_getxattr
,
8734 .listxattr
= btrfs_listxattr
,
8735 .removexattr
= btrfs_removexattr
,
8736 .get_acl
= btrfs_get_acl
,
8737 .update_time
= btrfs_update_time
,
8739 static const struct inode_operations btrfs_symlink_inode_operations
= {
8740 .readlink
= generic_readlink
,
8741 .follow_link
= page_follow_link_light
,
8742 .put_link
= page_put_link
,
8743 .getattr
= btrfs_getattr
,
8744 .setattr
= btrfs_setattr
,
8745 .permission
= btrfs_permission
,
8746 .setxattr
= btrfs_setxattr
,
8747 .getxattr
= btrfs_getxattr
,
8748 .listxattr
= btrfs_listxattr
,
8749 .removexattr
= btrfs_removexattr
,
8750 .get_acl
= btrfs_get_acl
,
8751 .update_time
= btrfs_update_time
,
8754 const struct dentry_operations btrfs_dentry_operations
= {
8755 .d_delete
= btrfs_dentry_delete
,
8756 .d_release
= btrfs_dentry_release
,