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
,
1184 btrfs_abort_transaction(trans
, root
, ret
);
1187 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1188 leaf
= path
->nodes
[0];
1189 btrfs_item_key_to_cpu(leaf
, &found_key
,
1190 path
->slots
[0] - 1);
1191 if (found_key
.objectid
== ino
&&
1192 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1197 leaf
= path
->nodes
[0];
1198 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1199 ret
= btrfs_next_leaf(root
, path
);
1201 btrfs_abort_transaction(trans
, root
, ret
);
1206 leaf
= path
->nodes
[0];
1212 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1214 if (found_key
.objectid
> ino
||
1215 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1216 found_key
.offset
> end
)
1219 if (found_key
.offset
> cur_offset
) {
1220 extent_end
= found_key
.offset
;
1225 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1226 struct btrfs_file_extent_item
);
1227 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1229 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1230 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1231 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1232 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1233 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1234 extent_end
= found_key
.offset
+
1235 btrfs_file_extent_num_bytes(leaf
, fi
);
1237 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1238 if (extent_end
<= start
) {
1242 if (disk_bytenr
== 0)
1244 if (btrfs_file_extent_compression(leaf
, fi
) ||
1245 btrfs_file_extent_encryption(leaf
, fi
) ||
1246 btrfs_file_extent_other_encoding(leaf
, fi
))
1248 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1250 if (btrfs_extent_readonly(root
, disk_bytenr
))
1252 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1254 extent_offset
, disk_bytenr
))
1256 disk_bytenr
+= extent_offset
;
1257 disk_bytenr
+= cur_offset
- found_key
.offset
;
1258 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1260 * force cow if csum exists in the range.
1261 * this ensure that csum for a given extent are
1262 * either valid or do not exist.
1264 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1267 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1268 extent_end
= found_key
.offset
+
1269 btrfs_file_extent_inline_len(leaf
, fi
);
1270 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1275 if (extent_end
<= start
) {
1280 if (cow_start
== (u64
)-1)
1281 cow_start
= cur_offset
;
1282 cur_offset
= extent_end
;
1283 if (cur_offset
> end
)
1289 btrfs_release_path(path
);
1290 if (cow_start
!= (u64
)-1) {
1291 ret
= cow_file_range(inode
, locked_page
,
1292 cow_start
, found_key
.offset
- 1,
1293 page_started
, nr_written
, 1);
1295 btrfs_abort_transaction(trans
, root
, ret
);
1298 cow_start
= (u64
)-1;
1301 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1302 struct extent_map
*em
;
1303 struct extent_map_tree
*em_tree
;
1304 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1305 em
= alloc_extent_map();
1306 BUG_ON(!em
); /* -ENOMEM */
1307 em
->start
= cur_offset
;
1308 em
->orig_start
= found_key
.offset
- extent_offset
;
1309 em
->len
= num_bytes
;
1310 em
->block_len
= num_bytes
;
1311 em
->block_start
= disk_bytenr
;
1312 em
->orig_block_len
= disk_num_bytes
;
1313 em
->ram_bytes
= ram_bytes
;
1314 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1315 em
->mod_start
= em
->start
;
1316 em
->mod_len
= em
->len
;
1317 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1318 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1319 em
->generation
= -1;
1321 write_lock(&em_tree
->lock
);
1322 ret
= add_extent_mapping(em_tree
, em
, 1);
1323 write_unlock(&em_tree
->lock
);
1324 if (ret
!= -EEXIST
) {
1325 free_extent_map(em
);
1328 btrfs_drop_extent_cache(inode
, em
->start
,
1329 em
->start
+ em
->len
- 1, 0);
1331 type
= BTRFS_ORDERED_PREALLOC
;
1333 type
= BTRFS_ORDERED_NOCOW
;
1336 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1337 num_bytes
, num_bytes
, type
);
1338 BUG_ON(ret
); /* -ENOMEM */
1340 if (root
->root_key
.objectid
==
1341 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1342 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1345 btrfs_abort_transaction(trans
, root
, ret
);
1350 extent_clear_unlock_delalloc(inode
, cur_offset
,
1351 cur_offset
+ num_bytes
- 1,
1352 locked_page
, EXTENT_LOCKED
|
1353 EXTENT_DELALLOC
, PAGE_UNLOCK
|
1355 cur_offset
= extent_end
;
1356 if (cur_offset
> end
)
1359 btrfs_release_path(path
);
1361 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1362 cow_start
= cur_offset
;
1366 if (cow_start
!= (u64
)-1) {
1367 ret
= cow_file_range(inode
, locked_page
, cow_start
, end
,
1368 page_started
, nr_written
, 1);
1370 btrfs_abort_transaction(trans
, root
, ret
);
1376 err
= btrfs_end_transaction(trans
, root
);
1380 if (ret
&& cur_offset
< end
)
1381 extent_clear_unlock_delalloc(inode
, cur_offset
, end
,
1382 locked_page
, EXTENT_LOCKED
|
1383 EXTENT_DELALLOC
| EXTENT_DEFRAG
|
1384 EXTENT_DO_ACCOUNTING
, PAGE_UNLOCK
|
1386 PAGE_SET_WRITEBACK
|
1387 PAGE_END_WRITEBACK
);
1388 btrfs_free_path(path
);
1393 * extent_io.c call back to do delayed allocation processing
1395 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1396 u64 start
, u64 end
, int *page_started
,
1397 unsigned long *nr_written
)
1400 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1402 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1403 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1404 page_started
, 1, nr_written
);
1405 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1406 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1407 page_started
, 0, nr_written
);
1408 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1409 !(BTRFS_I(inode
)->force_compress
) &&
1410 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1411 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1412 page_started
, nr_written
, 1);
1414 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1415 &BTRFS_I(inode
)->runtime_flags
);
1416 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1417 page_started
, nr_written
);
1422 static void btrfs_split_extent_hook(struct inode
*inode
,
1423 struct extent_state
*orig
, u64 split
)
1425 /* not delalloc, ignore it */
1426 if (!(orig
->state
& EXTENT_DELALLOC
))
1429 spin_lock(&BTRFS_I(inode
)->lock
);
1430 BTRFS_I(inode
)->outstanding_extents
++;
1431 spin_unlock(&BTRFS_I(inode
)->lock
);
1435 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1436 * extents so we can keep track of new extents that are just merged onto old
1437 * extents, such as when we are doing sequential writes, so we can properly
1438 * account for the metadata space we'll need.
1440 static void btrfs_merge_extent_hook(struct inode
*inode
,
1441 struct extent_state
*new,
1442 struct extent_state
*other
)
1444 /* not delalloc, ignore it */
1445 if (!(other
->state
& EXTENT_DELALLOC
))
1448 spin_lock(&BTRFS_I(inode
)->lock
);
1449 BTRFS_I(inode
)->outstanding_extents
--;
1450 spin_unlock(&BTRFS_I(inode
)->lock
);
1453 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1454 struct inode
*inode
)
1456 spin_lock(&root
->delalloc_lock
);
1457 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1458 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1459 &root
->delalloc_inodes
);
1460 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1461 &BTRFS_I(inode
)->runtime_flags
);
1462 root
->nr_delalloc_inodes
++;
1463 if (root
->nr_delalloc_inodes
== 1) {
1464 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1465 BUG_ON(!list_empty(&root
->delalloc_root
));
1466 list_add_tail(&root
->delalloc_root
,
1467 &root
->fs_info
->delalloc_roots
);
1468 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1471 spin_unlock(&root
->delalloc_lock
);
1474 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1475 struct inode
*inode
)
1477 spin_lock(&root
->delalloc_lock
);
1478 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1479 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1480 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1481 &BTRFS_I(inode
)->runtime_flags
);
1482 root
->nr_delalloc_inodes
--;
1483 if (!root
->nr_delalloc_inodes
) {
1484 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1485 BUG_ON(list_empty(&root
->delalloc_root
));
1486 list_del_init(&root
->delalloc_root
);
1487 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1490 spin_unlock(&root
->delalloc_lock
);
1494 * extent_io.c set_bit_hook, used to track delayed allocation
1495 * bytes in this file, and to maintain the list of inodes that
1496 * have pending delalloc work to be done.
1498 static void btrfs_set_bit_hook(struct inode
*inode
,
1499 struct extent_state
*state
, unsigned long *bits
)
1503 * set_bit and clear bit hooks normally require _irqsave/restore
1504 * but in this case, we are only testing for the DELALLOC
1505 * bit, which is only set or cleared with irqs on
1507 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1508 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1509 u64 len
= state
->end
+ 1 - state
->start
;
1510 bool do_list
= !btrfs_is_free_space_inode(inode
);
1512 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1513 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1515 spin_lock(&BTRFS_I(inode
)->lock
);
1516 BTRFS_I(inode
)->outstanding_extents
++;
1517 spin_unlock(&BTRFS_I(inode
)->lock
);
1520 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1521 root
->fs_info
->delalloc_batch
);
1522 spin_lock(&BTRFS_I(inode
)->lock
);
1523 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1524 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1525 &BTRFS_I(inode
)->runtime_flags
))
1526 btrfs_add_delalloc_inodes(root
, inode
);
1527 spin_unlock(&BTRFS_I(inode
)->lock
);
1532 * extent_io.c clear_bit_hook, see set_bit_hook for why
1534 static void btrfs_clear_bit_hook(struct inode
*inode
,
1535 struct extent_state
*state
,
1536 unsigned long *bits
)
1539 * set_bit and clear bit hooks normally require _irqsave/restore
1540 * but in this case, we are only testing for the DELALLOC
1541 * bit, which is only set or cleared with irqs on
1543 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1544 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1545 u64 len
= state
->end
+ 1 - state
->start
;
1546 bool do_list
= !btrfs_is_free_space_inode(inode
);
1548 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1549 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1550 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1551 spin_lock(&BTRFS_I(inode
)->lock
);
1552 BTRFS_I(inode
)->outstanding_extents
--;
1553 spin_unlock(&BTRFS_I(inode
)->lock
);
1557 * We don't reserve metadata space for space cache inodes so we
1558 * don't need to call dellalloc_release_metadata if there is an
1561 if (*bits
& EXTENT_DO_ACCOUNTING
&&
1562 root
!= root
->fs_info
->tree_root
)
1563 btrfs_delalloc_release_metadata(inode
, len
);
1565 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1566 && do_list
&& !(state
->state
& EXTENT_NORESERVE
))
1567 btrfs_free_reserved_data_space(inode
, len
);
1569 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1570 root
->fs_info
->delalloc_batch
);
1571 spin_lock(&BTRFS_I(inode
)->lock
);
1572 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1573 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1574 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1575 &BTRFS_I(inode
)->runtime_flags
))
1576 btrfs_del_delalloc_inode(root
, inode
);
1577 spin_unlock(&BTRFS_I(inode
)->lock
);
1582 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1583 * we don't create bios that span stripes or chunks
1585 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1586 size_t size
, struct bio
*bio
,
1587 unsigned long bio_flags
)
1589 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1590 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1595 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1598 length
= bio
->bi_size
;
1599 map_length
= length
;
1600 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1601 &map_length
, NULL
, 0);
1602 /* Will always return 0 with map_multi == NULL */
1604 if (map_length
< length
+ size
)
1610 * in order to insert checksums into the metadata in large chunks,
1611 * we wait until bio submission time. All the pages in the bio are
1612 * checksummed and sums are attached onto the ordered extent record.
1614 * At IO completion time the cums attached on the ordered extent record
1615 * are inserted into the btree
1617 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1618 struct bio
*bio
, int mirror_num
,
1619 unsigned long bio_flags
,
1622 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1625 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1626 BUG_ON(ret
); /* -ENOMEM */
1631 * in order to insert checksums into the metadata in large chunks,
1632 * we wait until bio submission time. All the pages in the bio are
1633 * checksummed and sums are attached onto the ordered extent record.
1635 * At IO completion time the cums attached on the ordered extent record
1636 * are inserted into the btree
1638 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1639 int mirror_num
, unsigned long bio_flags
,
1642 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1645 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1647 bio_endio(bio
, ret
);
1652 * extent_io.c submission hook. This does the right thing for csum calculation
1653 * on write, or reading the csums from the tree before a read
1655 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1656 int mirror_num
, unsigned long bio_flags
,
1659 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1663 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1665 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1667 if (btrfs_is_free_space_inode(inode
))
1670 if (!(rw
& REQ_WRITE
)) {
1671 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1675 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1676 ret
= btrfs_submit_compressed_read(inode
, bio
,
1680 } else if (!skip_sum
) {
1681 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1686 } else if (async
&& !skip_sum
) {
1687 /* csum items have already been cloned */
1688 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1690 /* we're doing a write, do the async checksumming */
1691 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1692 inode
, rw
, bio
, mirror_num
,
1693 bio_flags
, bio_offset
,
1694 __btrfs_submit_bio_start
,
1695 __btrfs_submit_bio_done
);
1697 } else if (!skip_sum
) {
1698 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1704 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1708 bio_endio(bio
, ret
);
1713 * given a list of ordered sums record them in the inode. This happens
1714 * at IO completion time based on sums calculated at bio submission time.
1716 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1717 struct inode
*inode
, u64 file_offset
,
1718 struct list_head
*list
)
1720 struct btrfs_ordered_sum
*sum
;
1722 list_for_each_entry(sum
, list
, list
) {
1723 trans
->adding_csums
= 1;
1724 btrfs_csum_file_blocks(trans
,
1725 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1726 trans
->adding_csums
= 0;
1731 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1732 struct extent_state
**cached_state
)
1734 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1735 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1736 cached_state
, GFP_NOFS
);
1739 /* see btrfs_writepage_start_hook for details on why this is required */
1740 struct btrfs_writepage_fixup
{
1742 struct btrfs_work work
;
1745 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1747 struct btrfs_writepage_fixup
*fixup
;
1748 struct btrfs_ordered_extent
*ordered
;
1749 struct extent_state
*cached_state
= NULL
;
1751 struct inode
*inode
;
1756 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1760 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1761 ClearPageChecked(page
);
1765 inode
= page
->mapping
->host
;
1766 page_start
= page_offset(page
);
1767 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1769 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1772 /* already ordered? We're done */
1773 if (PagePrivate2(page
))
1776 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1778 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1779 page_end
, &cached_state
, GFP_NOFS
);
1781 btrfs_start_ordered_extent(inode
, ordered
, 1);
1782 btrfs_put_ordered_extent(ordered
);
1786 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1788 mapping_set_error(page
->mapping
, ret
);
1789 end_extent_writepage(page
, ret
, page_start
, page_end
);
1790 ClearPageChecked(page
);
1794 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1795 ClearPageChecked(page
);
1796 set_page_dirty(page
);
1798 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1799 &cached_state
, GFP_NOFS
);
1802 page_cache_release(page
);
1807 * There are a few paths in the higher layers of the kernel that directly
1808 * set the page dirty bit without asking the filesystem if it is a
1809 * good idea. This causes problems because we want to make sure COW
1810 * properly happens and the data=ordered rules are followed.
1812 * In our case any range that doesn't have the ORDERED bit set
1813 * hasn't been properly setup for IO. We kick off an async process
1814 * to fix it up. The async helper will wait for ordered extents, set
1815 * the delalloc bit and make it safe to write the page.
1817 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1819 struct inode
*inode
= page
->mapping
->host
;
1820 struct btrfs_writepage_fixup
*fixup
;
1821 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1823 /* this page is properly in the ordered list */
1824 if (TestClearPagePrivate2(page
))
1827 if (PageChecked(page
))
1830 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1834 SetPageChecked(page
);
1835 page_cache_get(page
);
1836 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1838 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1842 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1843 struct inode
*inode
, u64 file_pos
,
1844 u64 disk_bytenr
, u64 disk_num_bytes
,
1845 u64 num_bytes
, u64 ram_bytes
,
1846 u8 compression
, u8 encryption
,
1847 u16 other_encoding
, int extent_type
)
1849 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1850 struct btrfs_file_extent_item
*fi
;
1851 struct btrfs_path
*path
;
1852 struct extent_buffer
*leaf
;
1853 struct btrfs_key ins
;
1856 path
= btrfs_alloc_path();
1860 path
->leave_spinning
= 1;
1863 * we may be replacing one extent in the tree with another.
1864 * The new extent is pinned in the extent map, and we don't want
1865 * to drop it from the cache until it is completely in the btree.
1867 * So, tell btrfs_drop_extents to leave this extent in the cache.
1868 * the caller is expected to unpin it and allow it to be merged
1871 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1872 file_pos
+ num_bytes
, 0);
1876 ins
.objectid
= btrfs_ino(inode
);
1877 ins
.offset
= file_pos
;
1878 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1879 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1882 leaf
= path
->nodes
[0];
1883 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1884 struct btrfs_file_extent_item
);
1885 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1886 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1887 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1888 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1889 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1890 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1891 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1892 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1893 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1894 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1896 btrfs_mark_buffer_dirty(leaf
);
1897 btrfs_release_path(path
);
1899 inode_add_bytes(inode
, num_bytes
);
1901 ins
.objectid
= disk_bytenr
;
1902 ins
.offset
= disk_num_bytes
;
1903 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1904 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1905 root
->root_key
.objectid
,
1906 btrfs_ino(inode
), file_pos
, &ins
);
1908 btrfs_free_path(path
);
1913 /* snapshot-aware defrag */
1914 struct sa_defrag_extent_backref
{
1915 struct rb_node node
;
1916 struct old_sa_defrag_extent
*old
;
1925 struct old_sa_defrag_extent
{
1926 struct list_head list
;
1927 struct new_sa_defrag_extent
*new;
1936 struct new_sa_defrag_extent
{
1937 struct rb_root root
;
1938 struct list_head head
;
1939 struct btrfs_path
*path
;
1940 struct inode
*inode
;
1948 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
1949 struct sa_defrag_extent_backref
*b2
)
1951 if (b1
->root_id
< b2
->root_id
)
1953 else if (b1
->root_id
> b2
->root_id
)
1956 if (b1
->inum
< b2
->inum
)
1958 else if (b1
->inum
> b2
->inum
)
1961 if (b1
->file_pos
< b2
->file_pos
)
1963 else if (b1
->file_pos
> b2
->file_pos
)
1967 * [------------------------------] ===> (a range of space)
1968 * |<--->| |<---->| =============> (fs/file tree A)
1969 * |<---------------------------->| ===> (fs/file tree B)
1971 * A range of space can refer to two file extents in one tree while
1972 * refer to only one file extent in another tree.
1974 * So we may process a disk offset more than one time(two extents in A)
1975 * and locate at the same extent(one extent in B), then insert two same
1976 * backrefs(both refer to the extent in B).
1981 static void backref_insert(struct rb_root
*root
,
1982 struct sa_defrag_extent_backref
*backref
)
1984 struct rb_node
**p
= &root
->rb_node
;
1985 struct rb_node
*parent
= NULL
;
1986 struct sa_defrag_extent_backref
*entry
;
1991 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
1993 ret
= backref_comp(backref
, entry
);
1997 p
= &(*p
)->rb_right
;
2000 rb_link_node(&backref
->node
, parent
, p
);
2001 rb_insert_color(&backref
->node
, root
);
2005 * Note the backref might has changed, and in this case we just return 0.
2007 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2010 struct btrfs_file_extent_item
*extent
;
2011 struct btrfs_fs_info
*fs_info
;
2012 struct old_sa_defrag_extent
*old
= ctx
;
2013 struct new_sa_defrag_extent
*new = old
->new;
2014 struct btrfs_path
*path
= new->path
;
2015 struct btrfs_key key
;
2016 struct btrfs_root
*root
;
2017 struct sa_defrag_extent_backref
*backref
;
2018 struct extent_buffer
*leaf
;
2019 struct inode
*inode
= new->inode
;
2025 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2026 inum
== btrfs_ino(inode
))
2029 key
.objectid
= root_id
;
2030 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2031 key
.offset
= (u64
)-1;
2033 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2034 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2036 if (PTR_ERR(root
) == -ENOENT
)
2039 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2040 inum
, offset
, root_id
);
2041 return PTR_ERR(root
);
2044 key
.objectid
= inum
;
2045 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2046 if (offset
> (u64
)-1 << 32)
2049 key
.offset
= offset
;
2051 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2061 leaf
= path
->nodes
[0];
2062 slot
= path
->slots
[0];
2064 if (slot
>= btrfs_header_nritems(leaf
)) {
2065 ret
= btrfs_next_leaf(root
, path
);
2068 } else if (ret
> 0) {
2077 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2079 if (key
.objectid
> inum
)
2082 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2085 extent
= btrfs_item_ptr(leaf
, slot
,
2086 struct btrfs_file_extent_item
);
2088 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2092 * 'offset' refers to the exact key.offset,
2093 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2094 * (key.offset - extent_offset).
2096 if (key
.offset
!= offset
)
2099 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2100 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2102 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2103 old
->len
|| extent_offset
+ num_bytes
<=
2104 old
->extent_offset
+ old
->offset
)
2109 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2115 backref
->root_id
= root_id
;
2116 backref
->inum
= inum
;
2117 backref
->file_pos
= offset
;
2118 backref
->num_bytes
= num_bytes
;
2119 backref
->extent_offset
= extent_offset
;
2120 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2122 backref_insert(&new->root
, backref
);
2125 btrfs_release_path(path
);
2130 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2131 struct new_sa_defrag_extent
*new)
2133 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2134 struct old_sa_defrag_extent
*old
, *tmp
;
2139 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2140 ret
= iterate_inodes_from_logical(old
->bytenr
+
2141 old
->extent_offset
, fs_info
,
2142 path
, record_one_backref
,
2144 BUG_ON(ret
< 0 && ret
!= -ENOENT
);
2146 /* no backref to be processed for this extent */
2148 list_del(&old
->list
);
2153 if (list_empty(&new->head
))
2159 static int relink_is_mergable(struct extent_buffer
*leaf
,
2160 struct btrfs_file_extent_item
*fi
,
2161 struct new_sa_defrag_extent
*new)
2163 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != new->bytenr
)
2166 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2169 if (btrfs_file_extent_compression(leaf
, fi
) != new->compress_type
)
2172 if (btrfs_file_extent_encryption(leaf
, fi
) ||
2173 btrfs_file_extent_other_encoding(leaf
, fi
))
2180 * Note the backref might has changed, and in this case we just return 0.
2182 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2183 struct sa_defrag_extent_backref
*prev
,
2184 struct sa_defrag_extent_backref
*backref
)
2186 struct btrfs_file_extent_item
*extent
;
2187 struct btrfs_file_extent_item
*item
;
2188 struct btrfs_ordered_extent
*ordered
;
2189 struct btrfs_trans_handle
*trans
;
2190 struct btrfs_fs_info
*fs_info
;
2191 struct btrfs_root
*root
;
2192 struct btrfs_key key
;
2193 struct extent_buffer
*leaf
;
2194 struct old_sa_defrag_extent
*old
= backref
->old
;
2195 struct new_sa_defrag_extent
*new = old
->new;
2196 struct inode
*src_inode
= new->inode
;
2197 struct inode
*inode
;
2198 struct extent_state
*cached
= NULL
;
2207 if (prev
&& prev
->root_id
== backref
->root_id
&&
2208 prev
->inum
== backref
->inum
&&
2209 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2212 /* step 1: get root */
2213 key
.objectid
= backref
->root_id
;
2214 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2215 key
.offset
= (u64
)-1;
2217 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2218 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2220 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2222 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2223 if (PTR_ERR(root
) == -ENOENT
)
2225 return PTR_ERR(root
);
2228 /* step 2: get inode */
2229 key
.objectid
= backref
->inum
;
2230 key
.type
= BTRFS_INODE_ITEM_KEY
;
2233 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2234 if (IS_ERR(inode
)) {
2235 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2239 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2241 /* step 3: relink backref */
2242 lock_start
= backref
->file_pos
;
2243 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2244 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2247 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2249 btrfs_put_ordered_extent(ordered
);
2253 trans
= btrfs_join_transaction(root
);
2254 if (IS_ERR(trans
)) {
2255 ret
= PTR_ERR(trans
);
2259 key
.objectid
= backref
->inum
;
2260 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2261 key
.offset
= backref
->file_pos
;
2263 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2266 } else if (ret
> 0) {
2271 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2272 struct btrfs_file_extent_item
);
2274 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2275 backref
->generation
)
2278 btrfs_release_path(path
);
2280 start
= backref
->file_pos
;
2281 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2282 start
+= old
->extent_offset
+ old
->offset
-
2283 backref
->extent_offset
;
2285 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2286 old
->extent_offset
+ old
->offset
+ old
->len
);
2287 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2289 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2294 key
.objectid
= btrfs_ino(inode
);
2295 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2298 path
->leave_spinning
= 1;
2300 struct btrfs_file_extent_item
*fi
;
2302 struct btrfs_key found_key
;
2304 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
2309 leaf
= path
->nodes
[0];
2310 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2312 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2313 struct btrfs_file_extent_item
);
2314 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2316 if (extent_len
+ found_key
.offset
== start
&&
2317 relink_is_mergable(leaf
, fi
, new)) {
2318 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2320 btrfs_mark_buffer_dirty(leaf
);
2321 inode_add_bytes(inode
, len
);
2327 btrfs_release_path(path
);
2332 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2335 btrfs_abort_transaction(trans
, root
, ret
);
2339 leaf
= path
->nodes
[0];
2340 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2341 struct btrfs_file_extent_item
);
2342 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2343 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2344 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2345 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2346 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2347 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2348 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2349 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2350 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2351 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2353 btrfs_mark_buffer_dirty(leaf
);
2354 inode_add_bytes(inode
, len
);
2355 btrfs_release_path(path
);
2357 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2359 backref
->root_id
, backref
->inum
,
2360 new->file_pos
, 0); /* start - extent_offset */
2362 btrfs_abort_transaction(trans
, root
, ret
);
2368 btrfs_release_path(path
);
2369 path
->leave_spinning
= 0;
2370 btrfs_end_transaction(trans
, root
);
2372 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2378 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2380 struct btrfs_path
*path
;
2381 struct old_sa_defrag_extent
*old
, *tmp
;
2382 struct sa_defrag_extent_backref
*backref
;
2383 struct sa_defrag_extent_backref
*prev
= NULL
;
2384 struct inode
*inode
;
2385 struct btrfs_root
*root
;
2386 struct rb_node
*node
;
2390 root
= BTRFS_I(inode
)->root
;
2392 path
= btrfs_alloc_path();
2396 if (!record_extent_backrefs(path
, new)) {
2397 btrfs_free_path(path
);
2400 btrfs_release_path(path
);
2403 node
= rb_first(&new->root
);
2406 rb_erase(node
, &new->root
);
2408 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2410 ret
= relink_extent_backref(path
, prev
, backref
);
2423 btrfs_free_path(path
);
2425 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2426 list_del(&old
->list
);
2430 atomic_dec(&root
->fs_info
->defrag_running
);
2431 wake_up(&root
->fs_info
->transaction_wait
);
2436 static struct new_sa_defrag_extent
*
2437 record_old_file_extents(struct inode
*inode
,
2438 struct btrfs_ordered_extent
*ordered
)
2440 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2441 struct btrfs_path
*path
;
2442 struct btrfs_key key
;
2443 struct old_sa_defrag_extent
*old
, *tmp
;
2444 struct new_sa_defrag_extent
*new;
2447 new = kmalloc(sizeof(*new), GFP_NOFS
);
2452 new->file_pos
= ordered
->file_offset
;
2453 new->len
= ordered
->len
;
2454 new->bytenr
= ordered
->start
;
2455 new->disk_len
= ordered
->disk_len
;
2456 new->compress_type
= ordered
->compress_type
;
2457 new->root
= RB_ROOT
;
2458 INIT_LIST_HEAD(&new->head
);
2460 path
= btrfs_alloc_path();
2464 key
.objectid
= btrfs_ino(inode
);
2465 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2466 key
.offset
= new->file_pos
;
2468 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2471 if (ret
> 0 && path
->slots
[0] > 0)
2474 /* find out all the old extents for the file range */
2476 struct btrfs_file_extent_item
*extent
;
2477 struct extent_buffer
*l
;
2486 slot
= path
->slots
[0];
2488 if (slot
>= btrfs_header_nritems(l
)) {
2489 ret
= btrfs_next_leaf(root
, path
);
2497 btrfs_item_key_to_cpu(l
, &key
, slot
);
2499 if (key
.objectid
!= btrfs_ino(inode
))
2501 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2503 if (key
.offset
>= new->file_pos
+ new->len
)
2506 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2508 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2509 if (key
.offset
+ num_bytes
< new->file_pos
)
2512 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2516 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2518 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2522 offset
= max(new->file_pos
, key
.offset
);
2523 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2525 old
->bytenr
= disk_bytenr
;
2526 old
->extent_offset
= extent_offset
;
2527 old
->offset
= offset
- key
.offset
;
2528 old
->len
= end
- offset
;
2531 list_add_tail(&old
->list
, &new->head
);
2537 btrfs_free_path(path
);
2538 atomic_inc(&root
->fs_info
->defrag_running
);
2543 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2544 list_del(&old
->list
);
2548 btrfs_free_path(path
);
2555 * helper function for btrfs_finish_ordered_io, this
2556 * just reads in some of the csum leaves to prime them into ram
2557 * before we start the transaction. It limits the amount of btree
2558 * reads required while inside the transaction.
2560 /* as ordered data IO finishes, this gets called so we can finish
2561 * an ordered extent if the range of bytes in the file it covers are
2564 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2566 struct inode
*inode
= ordered_extent
->inode
;
2567 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2568 struct btrfs_trans_handle
*trans
= NULL
;
2569 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2570 struct extent_state
*cached_state
= NULL
;
2571 struct new_sa_defrag_extent
*new = NULL
;
2572 int compress_type
= 0;
2574 u64 logical_len
= ordered_extent
->len
;
2576 bool truncated
= false;
2578 nolock
= btrfs_is_free_space_inode(inode
);
2580 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2585 if (test_bit(BTRFS_ORDERED_TRUNCATED
, &ordered_extent
->flags
)) {
2587 logical_len
= ordered_extent
->truncated_len
;
2588 /* Truncated the entire extent, don't bother adding */
2593 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2594 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2595 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2597 trans
= btrfs_join_transaction_nolock(root
);
2599 trans
= btrfs_join_transaction(root
);
2600 if (IS_ERR(trans
)) {
2601 ret
= PTR_ERR(trans
);
2605 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2606 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2607 if (ret
) /* -ENOMEM or corruption */
2608 btrfs_abort_transaction(trans
, root
, ret
);
2612 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2613 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2616 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2617 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2618 EXTENT_DEFRAG
, 1, cached_state
);
2620 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2621 if (last_snapshot
>= BTRFS_I(inode
)->generation
)
2622 /* the inode is shared */
2623 new = record_old_file_extents(inode
, ordered_extent
);
2625 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2626 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2627 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2631 trans
= btrfs_join_transaction_nolock(root
);
2633 trans
= btrfs_join_transaction(root
);
2634 if (IS_ERR(trans
)) {
2635 ret
= PTR_ERR(trans
);
2639 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2641 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2642 compress_type
= ordered_extent
->compress_type
;
2643 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2644 BUG_ON(compress_type
);
2645 ret
= btrfs_mark_extent_written(trans
, inode
,
2646 ordered_extent
->file_offset
,
2647 ordered_extent
->file_offset
+
2650 BUG_ON(root
== root
->fs_info
->tree_root
);
2651 ret
= insert_reserved_file_extent(trans
, inode
,
2652 ordered_extent
->file_offset
,
2653 ordered_extent
->start
,
2654 ordered_extent
->disk_len
,
2655 logical_len
, logical_len
,
2656 compress_type
, 0, 0,
2657 BTRFS_FILE_EXTENT_REG
);
2659 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2660 ordered_extent
->file_offset
, ordered_extent
->len
,
2663 btrfs_abort_transaction(trans
, root
, ret
);
2667 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2668 &ordered_extent
->list
);
2670 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2671 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2672 if (ret
) { /* -ENOMEM or corruption */
2673 btrfs_abort_transaction(trans
, root
, ret
);
2678 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2679 ordered_extent
->file_offset
+
2680 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2682 if (root
!= root
->fs_info
->tree_root
)
2683 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2685 btrfs_end_transaction(trans
, root
);
2687 if (ret
|| truncated
) {
2691 start
= ordered_extent
->file_offset
+ logical_len
;
2693 start
= ordered_extent
->file_offset
;
2694 end
= ordered_extent
->file_offset
+ ordered_extent
->len
- 1;
2695 clear_extent_uptodate(io_tree
, start
, end
, NULL
, GFP_NOFS
);
2697 /* Drop the cache for the part of the extent we didn't write. */
2698 btrfs_drop_extent_cache(inode
, start
, end
, 0);
2701 * If the ordered extent had an IOERR or something else went
2702 * wrong we need to return the space for this ordered extent
2703 * back to the allocator. We only free the extent in the
2704 * truncated case if we didn't write out the extent at all.
2706 if ((ret
|| !logical_len
) &&
2707 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2708 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2709 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2710 ordered_extent
->disk_len
);
2715 * This needs to be done to make sure anybody waiting knows we are done
2716 * updating everything for this ordered extent.
2718 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2720 /* for snapshot-aware defrag */
2722 relink_file_extents(new);
2725 btrfs_put_ordered_extent(ordered_extent
);
2726 /* once for the tree */
2727 btrfs_put_ordered_extent(ordered_extent
);
2732 static void finish_ordered_fn(struct btrfs_work
*work
)
2734 struct btrfs_ordered_extent
*ordered_extent
;
2735 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2736 btrfs_finish_ordered_io(ordered_extent
);
2739 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2740 struct extent_state
*state
, int uptodate
)
2742 struct inode
*inode
= page
->mapping
->host
;
2743 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2744 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2745 struct btrfs_workers
*workers
;
2747 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2749 ClearPagePrivate2(page
);
2750 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2751 end
- start
+ 1, uptodate
))
2754 ordered_extent
->work
.func
= finish_ordered_fn
;
2755 ordered_extent
->work
.flags
= 0;
2757 if (btrfs_is_free_space_inode(inode
))
2758 workers
= &root
->fs_info
->endio_freespace_worker
;
2760 workers
= &root
->fs_info
->endio_write_workers
;
2761 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2767 * when reads are done, we need to check csums to verify the data is correct
2768 * if there's a match, we allow the bio to finish. If not, the code in
2769 * extent_io.c will try to find good copies for us.
2771 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio
*io_bio
,
2772 u64 phy_offset
, struct page
*page
,
2773 u64 start
, u64 end
, int mirror
)
2775 size_t offset
= start
- page_offset(page
);
2776 struct inode
*inode
= page
->mapping
->host
;
2777 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2779 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2782 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
2783 DEFAULT_RATELIMIT_BURST
);
2785 if (PageChecked(page
)) {
2786 ClearPageChecked(page
);
2790 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2793 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2794 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2795 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2800 phy_offset
>>= inode
->i_sb
->s_blocksize_bits
;
2801 csum_expected
= *(((u32
*)io_bio
->csum
) + phy_offset
);
2803 kaddr
= kmap_atomic(page
);
2804 csum
= btrfs_csum_data(kaddr
+ offset
, csum
, end
- start
+ 1);
2805 btrfs_csum_final(csum
, (char *)&csum
);
2806 if (csum
!= csum_expected
)
2809 kunmap_atomic(kaddr
);
2814 if (__ratelimit(&_rs
))
2815 btrfs_info(root
->fs_info
, "csum failed ino %llu off %llu csum %u expected csum %u",
2816 btrfs_ino(page
->mapping
->host
), start
, csum
, csum_expected
);
2817 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2818 flush_dcache_page(page
);
2819 kunmap_atomic(kaddr
);
2820 if (csum_expected
== 0)
2825 struct delayed_iput
{
2826 struct list_head list
;
2827 struct inode
*inode
;
2830 /* JDM: If this is fs-wide, why can't we add a pointer to
2831 * btrfs_inode instead and avoid the allocation? */
2832 void btrfs_add_delayed_iput(struct inode
*inode
)
2834 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2835 struct delayed_iput
*delayed
;
2837 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2840 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2841 delayed
->inode
= inode
;
2843 spin_lock(&fs_info
->delayed_iput_lock
);
2844 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2845 spin_unlock(&fs_info
->delayed_iput_lock
);
2848 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2851 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2852 struct delayed_iput
*delayed
;
2855 spin_lock(&fs_info
->delayed_iput_lock
);
2856 empty
= list_empty(&fs_info
->delayed_iputs
);
2857 spin_unlock(&fs_info
->delayed_iput_lock
);
2861 spin_lock(&fs_info
->delayed_iput_lock
);
2862 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2863 spin_unlock(&fs_info
->delayed_iput_lock
);
2865 while (!list_empty(&list
)) {
2866 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2867 list_del(&delayed
->list
);
2868 iput(delayed
->inode
);
2874 * This is called in transaction commit time. If there are no orphan
2875 * files in the subvolume, it removes orphan item and frees block_rsv
2878 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2879 struct btrfs_root
*root
)
2881 struct btrfs_block_rsv
*block_rsv
;
2884 if (atomic_read(&root
->orphan_inodes
) ||
2885 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2888 spin_lock(&root
->orphan_lock
);
2889 if (atomic_read(&root
->orphan_inodes
)) {
2890 spin_unlock(&root
->orphan_lock
);
2894 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2895 spin_unlock(&root
->orphan_lock
);
2899 block_rsv
= root
->orphan_block_rsv
;
2900 root
->orphan_block_rsv
= NULL
;
2901 spin_unlock(&root
->orphan_lock
);
2903 if (root
->orphan_item_inserted
&&
2904 btrfs_root_refs(&root
->root_item
) > 0) {
2905 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2906 root
->root_key
.objectid
);
2908 btrfs_abort_transaction(trans
, root
, ret
);
2910 root
->orphan_item_inserted
= 0;
2914 WARN_ON(block_rsv
->size
> 0);
2915 btrfs_free_block_rsv(root
, block_rsv
);
2920 * This creates an orphan entry for the given inode in case something goes
2921 * wrong in the middle of an unlink/truncate.
2923 * NOTE: caller of this function should reserve 5 units of metadata for
2926 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2928 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2929 struct btrfs_block_rsv
*block_rsv
= NULL
;
2934 if (!root
->orphan_block_rsv
) {
2935 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2940 spin_lock(&root
->orphan_lock
);
2941 if (!root
->orphan_block_rsv
) {
2942 root
->orphan_block_rsv
= block_rsv
;
2943 } else if (block_rsv
) {
2944 btrfs_free_block_rsv(root
, block_rsv
);
2948 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2949 &BTRFS_I(inode
)->runtime_flags
)) {
2952 * For proper ENOSPC handling, we should do orphan
2953 * cleanup when mounting. But this introduces backward
2954 * compatibility issue.
2956 if (!xchg(&root
->orphan_item_inserted
, 1))
2962 atomic_inc(&root
->orphan_inodes
);
2965 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2966 &BTRFS_I(inode
)->runtime_flags
))
2968 spin_unlock(&root
->orphan_lock
);
2970 /* grab metadata reservation from transaction handle */
2972 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2973 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
2976 /* insert an orphan item to track this unlinked/truncated file */
2978 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
2980 atomic_dec(&root
->orphan_inodes
);
2982 clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2983 &BTRFS_I(inode
)->runtime_flags
);
2984 btrfs_orphan_release_metadata(inode
);
2986 if (ret
!= -EEXIST
) {
2987 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2988 &BTRFS_I(inode
)->runtime_flags
);
2989 btrfs_abort_transaction(trans
, root
, ret
);
2996 /* insert an orphan item to track subvolume contains orphan files */
2998 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
2999 root
->root_key
.objectid
);
3000 if (ret
&& ret
!= -EEXIST
) {
3001 btrfs_abort_transaction(trans
, root
, ret
);
3009 * We have done the truncate/delete so we can go ahead and remove the orphan
3010 * item for this particular inode.
3012 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3013 struct inode
*inode
)
3015 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3016 int delete_item
= 0;
3017 int release_rsv
= 0;
3020 spin_lock(&root
->orphan_lock
);
3021 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3022 &BTRFS_I(inode
)->runtime_flags
))
3025 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3026 &BTRFS_I(inode
)->runtime_flags
))
3028 spin_unlock(&root
->orphan_lock
);
3031 atomic_dec(&root
->orphan_inodes
);
3033 ret
= btrfs_del_orphan_item(trans
, root
,
3038 btrfs_orphan_release_metadata(inode
);
3044 * this cleans up any orphans that may be left on the list from the last use
3047 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3049 struct btrfs_path
*path
;
3050 struct extent_buffer
*leaf
;
3051 struct btrfs_key key
, found_key
;
3052 struct btrfs_trans_handle
*trans
;
3053 struct inode
*inode
;
3054 u64 last_objectid
= 0;
3055 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3057 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3060 path
= btrfs_alloc_path();
3067 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3068 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
3069 key
.offset
= (u64
)-1;
3072 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3077 * if ret == 0 means we found what we were searching for, which
3078 * is weird, but possible, so only screw with path if we didn't
3079 * find the key and see if we have stuff that matches
3083 if (path
->slots
[0] == 0)
3088 /* pull out the item */
3089 leaf
= path
->nodes
[0];
3090 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3092 /* make sure the item matches what we want */
3093 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3095 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
3098 /* release the path since we're done with it */
3099 btrfs_release_path(path
);
3102 * this is where we are basically btrfs_lookup, without the
3103 * crossing root thing. we store the inode number in the
3104 * offset of the orphan item.
3107 if (found_key
.offset
== last_objectid
) {
3108 btrfs_err(root
->fs_info
,
3109 "Error removing orphan entry, stopping orphan cleanup");
3114 last_objectid
= found_key
.offset
;
3116 found_key
.objectid
= found_key
.offset
;
3117 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3118 found_key
.offset
= 0;
3119 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3120 ret
= PTR_ERR_OR_ZERO(inode
);
3121 if (ret
&& ret
!= -ESTALE
)
3124 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3125 struct btrfs_root
*dead_root
;
3126 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3127 int is_dead_root
= 0;
3130 * this is an orphan in the tree root. Currently these
3131 * could come from 2 sources:
3132 * a) a snapshot deletion in progress
3133 * b) a free space cache inode
3134 * We need to distinguish those two, as the snapshot
3135 * orphan must not get deleted.
3136 * find_dead_roots already ran before us, so if this
3137 * is a snapshot deletion, we should find the root
3138 * in the dead_roots list
3140 spin_lock(&fs_info
->trans_lock
);
3141 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3143 if (dead_root
->root_key
.objectid
==
3144 found_key
.objectid
) {
3149 spin_unlock(&fs_info
->trans_lock
);
3151 /* prevent this orphan from being found again */
3152 key
.offset
= found_key
.objectid
- 1;
3157 * Inode is already gone but the orphan item is still there,
3158 * kill the orphan item.
3160 if (ret
== -ESTALE
) {
3161 trans
= btrfs_start_transaction(root
, 1);
3162 if (IS_ERR(trans
)) {
3163 ret
= PTR_ERR(trans
);
3166 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3167 found_key
.objectid
);
3168 ret
= btrfs_del_orphan_item(trans
, root
,
3169 found_key
.objectid
);
3170 btrfs_end_transaction(trans
, root
);
3177 * add this inode to the orphan list so btrfs_orphan_del does
3178 * the proper thing when we hit it
3180 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3181 &BTRFS_I(inode
)->runtime_flags
);
3182 atomic_inc(&root
->orphan_inodes
);
3184 /* if we have links, this was a truncate, lets do that */
3185 if (inode
->i_nlink
) {
3186 if (!S_ISREG(inode
->i_mode
)) {
3193 /* 1 for the orphan item deletion. */
3194 trans
= btrfs_start_transaction(root
, 1);
3195 if (IS_ERR(trans
)) {
3197 ret
= PTR_ERR(trans
);
3200 ret
= btrfs_orphan_add(trans
, inode
);
3201 btrfs_end_transaction(trans
, root
);
3207 ret
= btrfs_truncate(inode
);
3209 btrfs_orphan_del(NULL
, inode
);
3214 /* this will do delete_inode and everything for us */
3219 /* release the path since we're done with it */
3220 btrfs_release_path(path
);
3222 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3224 if (root
->orphan_block_rsv
)
3225 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3228 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
3229 trans
= btrfs_join_transaction(root
);
3231 btrfs_end_transaction(trans
, root
);
3235 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3237 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3241 btrfs_crit(root
->fs_info
,
3242 "could not do orphan cleanup %d", ret
);
3243 btrfs_free_path(path
);
3248 * very simple check to peek ahead in the leaf looking for xattrs. If we
3249 * don't find any xattrs, we know there can't be any acls.
3251 * slot is the slot the inode is in, objectid is the objectid of the inode
3253 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3254 int slot
, u64 objectid
)
3256 u32 nritems
= btrfs_header_nritems(leaf
);
3257 struct btrfs_key found_key
;
3258 static u64 xattr_access
= 0;
3259 static u64 xattr_default
= 0;
3262 if (!xattr_access
) {
3263 xattr_access
= btrfs_name_hash(POSIX_ACL_XATTR_ACCESS
,
3264 strlen(POSIX_ACL_XATTR_ACCESS
));
3265 xattr_default
= btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT
,
3266 strlen(POSIX_ACL_XATTR_DEFAULT
));
3270 while (slot
< nritems
) {
3271 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3273 /* we found a different objectid, there must not be acls */
3274 if (found_key
.objectid
!= objectid
)
3277 /* we found an xattr, assume we've got an acl */
3278 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3279 if (found_key
.offset
== xattr_access
||
3280 found_key
.offset
== xattr_default
)
3285 * we found a key greater than an xattr key, there can't
3286 * be any acls later on
3288 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3295 * it goes inode, inode backrefs, xattrs, extents,
3296 * so if there are a ton of hard links to an inode there can
3297 * be a lot of backrefs. Don't waste time searching too hard,
3298 * this is just an optimization
3303 /* we hit the end of the leaf before we found an xattr or
3304 * something larger than an xattr. We have to assume the inode
3311 * read an inode from the btree into the in-memory inode
3313 static void btrfs_read_locked_inode(struct inode
*inode
)
3315 struct btrfs_path
*path
;
3316 struct extent_buffer
*leaf
;
3317 struct btrfs_inode_item
*inode_item
;
3318 struct btrfs_timespec
*tspec
;
3319 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3320 struct btrfs_key location
;
3324 bool filled
= false;
3326 ret
= btrfs_fill_inode(inode
, &rdev
);
3330 path
= btrfs_alloc_path();
3334 path
->leave_spinning
= 1;
3335 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3337 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3341 leaf
= path
->nodes
[0];
3346 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3347 struct btrfs_inode_item
);
3348 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3349 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3350 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3351 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3352 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3354 tspec
= btrfs_inode_atime(inode_item
);
3355 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3356 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3358 tspec
= btrfs_inode_mtime(inode_item
);
3359 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3360 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3362 tspec
= btrfs_inode_ctime(inode_item
);
3363 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3364 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3366 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3367 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3368 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3371 * If we were modified in the current generation and evicted from memory
3372 * and then re-read we need to do a full sync since we don't have any
3373 * idea about which extents were modified before we were evicted from
3376 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3377 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3378 &BTRFS_I(inode
)->runtime_flags
);
3380 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3381 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3383 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3385 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3386 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3389 * try to precache a NULL acl entry for files that don't have
3390 * any xattrs or acls
3392 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3395 cache_no_acl(inode
);
3397 btrfs_free_path(path
);
3399 switch (inode
->i_mode
& S_IFMT
) {
3401 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3402 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3403 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3404 inode
->i_fop
= &btrfs_file_operations
;
3405 inode
->i_op
= &btrfs_file_inode_operations
;
3408 inode
->i_fop
= &btrfs_dir_file_operations
;
3409 if (root
== root
->fs_info
->tree_root
)
3410 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3412 inode
->i_op
= &btrfs_dir_inode_operations
;
3415 inode
->i_op
= &btrfs_symlink_inode_operations
;
3416 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3417 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3420 inode
->i_op
= &btrfs_special_inode_operations
;
3421 init_special_inode(inode
, inode
->i_mode
, rdev
);
3425 btrfs_update_iflags(inode
);
3429 btrfs_free_path(path
);
3430 make_bad_inode(inode
);
3434 * given a leaf and an inode, copy the inode fields into the leaf
3436 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3437 struct extent_buffer
*leaf
,
3438 struct btrfs_inode_item
*item
,
3439 struct inode
*inode
)
3441 struct btrfs_map_token token
;
3443 btrfs_init_map_token(&token
);
3445 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3446 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3447 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3449 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3450 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3452 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
3453 inode
->i_atime
.tv_sec
, &token
);
3454 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
3455 inode
->i_atime
.tv_nsec
, &token
);
3457 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
3458 inode
->i_mtime
.tv_sec
, &token
);
3459 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3460 inode
->i_mtime
.tv_nsec
, &token
);
3462 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3463 inode
->i_ctime
.tv_sec
, &token
);
3464 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3465 inode
->i_ctime
.tv_nsec
, &token
);
3467 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3469 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3471 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3472 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3473 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3474 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3475 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3479 * copy everything in the in-memory inode into the btree.
3481 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3482 struct btrfs_root
*root
, struct inode
*inode
)
3484 struct btrfs_inode_item
*inode_item
;
3485 struct btrfs_path
*path
;
3486 struct extent_buffer
*leaf
;
3489 path
= btrfs_alloc_path();
3493 path
->leave_spinning
= 1;
3494 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3502 btrfs_unlock_up_safe(path
, 1);
3503 leaf
= path
->nodes
[0];
3504 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3505 struct btrfs_inode_item
);
3507 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3508 btrfs_mark_buffer_dirty(leaf
);
3509 btrfs_set_inode_last_trans(trans
, inode
);
3512 btrfs_free_path(path
);
3517 * copy everything in the in-memory inode into the btree.
3519 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3520 struct btrfs_root
*root
, struct inode
*inode
)
3525 * If the inode is a free space inode, we can deadlock during commit
3526 * if we put it into the delayed code.
3528 * The data relocation inode should also be directly updated
3531 if (!btrfs_is_free_space_inode(inode
)
3532 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
3533 btrfs_update_root_times(trans
, root
);
3535 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3537 btrfs_set_inode_last_trans(trans
, inode
);
3541 return btrfs_update_inode_item(trans
, root
, inode
);
3544 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3545 struct btrfs_root
*root
,
3546 struct inode
*inode
)
3550 ret
= btrfs_update_inode(trans
, root
, inode
);
3552 return btrfs_update_inode_item(trans
, root
, inode
);
3557 * unlink helper that gets used here in inode.c and in the tree logging
3558 * recovery code. It remove a link in a directory with a given name, and
3559 * also drops the back refs in the inode to the directory
3561 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3562 struct btrfs_root
*root
,
3563 struct inode
*dir
, struct inode
*inode
,
3564 const char *name
, int name_len
)
3566 struct btrfs_path
*path
;
3568 struct extent_buffer
*leaf
;
3569 struct btrfs_dir_item
*di
;
3570 struct btrfs_key key
;
3572 u64 ino
= btrfs_ino(inode
);
3573 u64 dir_ino
= btrfs_ino(dir
);
3575 path
= btrfs_alloc_path();
3581 path
->leave_spinning
= 1;
3582 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3583 name
, name_len
, -1);
3592 leaf
= path
->nodes
[0];
3593 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3594 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3597 btrfs_release_path(path
);
3599 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3602 btrfs_info(root
->fs_info
,
3603 "failed to delete reference to %.*s, inode %llu parent %llu",
3604 name_len
, name
, ino
, dir_ino
);
3605 btrfs_abort_transaction(trans
, root
, ret
);
3609 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3611 btrfs_abort_transaction(trans
, root
, ret
);
3615 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
3617 if (ret
!= 0 && ret
!= -ENOENT
) {
3618 btrfs_abort_transaction(trans
, root
, ret
);
3622 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
3627 btrfs_abort_transaction(trans
, root
, ret
);
3629 btrfs_free_path(path
);
3633 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3634 inode_inc_iversion(inode
);
3635 inode_inc_iversion(dir
);
3636 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3637 ret
= btrfs_update_inode(trans
, root
, dir
);
3642 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3643 struct btrfs_root
*root
,
3644 struct inode
*dir
, struct inode
*inode
,
3645 const char *name
, int name_len
)
3648 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3651 ret
= btrfs_update_inode(trans
, root
, inode
);
3657 * helper to start transaction for unlink and rmdir.
3659 * unlink and rmdir are special in btrfs, they do not always free space, so
3660 * if we cannot make our reservations the normal way try and see if there is
3661 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3662 * allow the unlink to occur.
3664 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
3666 struct btrfs_trans_handle
*trans
;
3667 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3671 * 1 for the possible orphan item
3672 * 1 for the dir item
3673 * 1 for the dir index
3674 * 1 for the inode ref
3677 trans
= btrfs_start_transaction(root
, 5);
3678 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3681 if (PTR_ERR(trans
) == -ENOSPC
) {
3682 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 5);
3684 trans
= btrfs_start_transaction(root
, 0);
3687 ret
= btrfs_cond_migrate_bytes(root
->fs_info
,
3688 &root
->fs_info
->trans_block_rsv
,
3691 btrfs_end_transaction(trans
, root
);
3692 return ERR_PTR(ret
);
3694 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3695 trans
->bytes_reserved
= num_bytes
;
3700 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3702 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3703 struct btrfs_trans_handle
*trans
;
3704 struct inode
*inode
= dentry
->d_inode
;
3707 trans
= __unlink_start_trans(dir
);
3709 return PTR_ERR(trans
);
3711 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3713 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3714 dentry
->d_name
.name
, dentry
->d_name
.len
);
3718 if (inode
->i_nlink
== 0) {
3719 ret
= btrfs_orphan_add(trans
, inode
);
3725 btrfs_end_transaction(trans
, root
);
3726 btrfs_btree_balance_dirty(root
);
3730 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3731 struct btrfs_root
*root
,
3732 struct inode
*dir
, u64 objectid
,
3733 const char *name
, int name_len
)
3735 struct btrfs_path
*path
;
3736 struct extent_buffer
*leaf
;
3737 struct btrfs_dir_item
*di
;
3738 struct btrfs_key key
;
3741 u64 dir_ino
= btrfs_ino(dir
);
3743 path
= btrfs_alloc_path();
3747 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3748 name
, name_len
, -1);
3749 if (IS_ERR_OR_NULL(di
)) {
3757 leaf
= path
->nodes
[0];
3758 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3759 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3760 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3762 btrfs_abort_transaction(trans
, root
, ret
);
3765 btrfs_release_path(path
);
3767 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3768 objectid
, root
->root_key
.objectid
,
3769 dir_ino
, &index
, name
, name_len
);
3771 if (ret
!= -ENOENT
) {
3772 btrfs_abort_transaction(trans
, root
, ret
);
3775 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3777 if (IS_ERR_OR_NULL(di
)) {
3782 btrfs_abort_transaction(trans
, root
, ret
);
3786 leaf
= path
->nodes
[0];
3787 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3788 btrfs_release_path(path
);
3791 btrfs_release_path(path
);
3793 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3795 btrfs_abort_transaction(trans
, root
, ret
);
3799 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3800 inode_inc_iversion(dir
);
3801 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3802 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3804 btrfs_abort_transaction(trans
, root
, ret
);
3806 btrfs_free_path(path
);
3810 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3812 struct inode
*inode
= dentry
->d_inode
;
3814 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3815 struct btrfs_trans_handle
*trans
;
3817 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3819 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3822 trans
= __unlink_start_trans(dir
);
3824 return PTR_ERR(trans
);
3826 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3827 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3828 BTRFS_I(inode
)->location
.objectid
,
3829 dentry
->d_name
.name
,
3830 dentry
->d_name
.len
);
3834 err
= btrfs_orphan_add(trans
, inode
);
3838 /* now the directory is empty */
3839 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3840 dentry
->d_name
.name
, dentry
->d_name
.len
);
3842 btrfs_i_size_write(inode
, 0);
3844 btrfs_end_transaction(trans
, root
);
3845 btrfs_btree_balance_dirty(root
);
3851 * this can truncate away extent items, csum items and directory items.
3852 * It starts at a high offset and removes keys until it can't find
3853 * any higher than new_size
3855 * csum items that cross the new i_size are truncated to the new size
3858 * min_type is the minimum key type to truncate down to. If set to 0, this
3859 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3861 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3862 struct btrfs_root
*root
,
3863 struct inode
*inode
,
3864 u64 new_size
, u32 min_type
)
3866 struct btrfs_path
*path
;
3867 struct extent_buffer
*leaf
;
3868 struct btrfs_file_extent_item
*fi
;
3869 struct btrfs_key key
;
3870 struct btrfs_key found_key
;
3871 u64 extent_start
= 0;
3872 u64 extent_num_bytes
= 0;
3873 u64 extent_offset
= 0;
3875 u64 last_size
= (u64
)-1;
3876 u32 found_type
= (u8
)-1;
3879 int pending_del_nr
= 0;
3880 int pending_del_slot
= 0;
3881 int extent_type
= -1;
3884 u64 ino
= btrfs_ino(inode
);
3886 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3888 path
= btrfs_alloc_path();
3894 * We want to drop from the next block forward in case this new size is
3895 * not block aligned since we will be keeping the last block of the
3896 * extent just the way it is.
3898 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3899 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
3900 root
->sectorsize
), (u64
)-1, 0);
3903 * This function is also used to drop the items in the log tree before
3904 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3905 * it is used to drop the loged items. So we shouldn't kill the delayed
3908 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
3909 btrfs_kill_delayed_inode_items(inode
);
3912 key
.offset
= (u64
)-1;
3916 path
->leave_spinning
= 1;
3917 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3924 /* there are no items in the tree for us to truncate, we're
3927 if (path
->slots
[0] == 0)
3934 leaf
= path
->nodes
[0];
3935 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3936 found_type
= btrfs_key_type(&found_key
);
3938 if (found_key
.objectid
!= ino
)
3941 if (found_type
< min_type
)
3944 item_end
= found_key
.offset
;
3945 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3946 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3947 struct btrfs_file_extent_item
);
3948 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3949 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3951 btrfs_file_extent_num_bytes(leaf
, fi
);
3952 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3953 item_end
+= btrfs_file_extent_inline_len(leaf
,
3958 if (found_type
> min_type
) {
3961 if (item_end
< new_size
)
3963 if (found_key
.offset
>= new_size
)
3969 /* FIXME, shrink the extent if the ref count is only 1 */
3970 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3974 last_size
= found_key
.offset
;
3976 last_size
= new_size
;
3978 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3980 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
3982 u64 orig_num_bytes
=
3983 btrfs_file_extent_num_bytes(leaf
, fi
);
3984 extent_num_bytes
= ALIGN(new_size
-
3987 btrfs_set_file_extent_num_bytes(leaf
, fi
,
3989 num_dec
= (orig_num_bytes
-
3991 if (root
->ref_cows
&& extent_start
!= 0)
3992 inode_sub_bytes(inode
, num_dec
);
3993 btrfs_mark_buffer_dirty(leaf
);
3996 btrfs_file_extent_disk_num_bytes(leaf
,
3998 extent_offset
= found_key
.offset
-
3999 btrfs_file_extent_offset(leaf
, fi
);
4001 /* FIXME blocksize != 4096 */
4002 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4003 if (extent_start
!= 0) {
4006 inode_sub_bytes(inode
, num_dec
);
4009 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4011 * we can't truncate inline items that have had
4015 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4016 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4017 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4018 u32 size
= new_size
- found_key
.offset
;
4020 if (root
->ref_cows
) {
4021 inode_sub_bytes(inode
, item_end
+ 1 -
4025 btrfs_file_extent_calc_inline_size(size
);
4026 btrfs_truncate_item(root
, path
, size
, 1);
4027 } else if (root
->ref_cows
) {
4028 inode_sub_bytes(inode
, item_end
+ 1 -
4034 if (!pending_del_nr
) {
4035 /* no pending yet, add ourselves */
4036 pending_del_slot
= path
->slots
[0];
4038 } else if (pending_del_nr
&&
4039 path
->slots
[0] + 1 == pending_del_slot
) {
4040 /* hop on the pending chunk */
4042 pending_del_slot
= path
->slots
[0];
4049 if (found_extent
&& (root
->ref_cows
||
4050 root
== root
->fs_info
->tree_root
)) {
4051 btrfs_set_path_blocking(path
);
4052 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4053 extent_num_bytes
, 0,
4054 btrfs_header_owner(leaf
),
4055 ino
, extent_offset
, 0);
4059 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4062 if (path
->slots
[0] == 0 ||
4063 path
->slots
[0] != pending_del_slot
) {
4064 if (pending_del_nr
) {
4065 ret
= btrfs_del_items(trans
, root
, path
,
4069 btrfs_abort_transaction(trans
,
4075 btrfs_release_path(path
);
4082 if (pending_del_nr
) {
4083 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4086 btrfs_abort_transaction(trans
, root
, ret
);
4089 if (last_size
!= (u64
)-1)
4090 btrfs_ordered_update_i_size(inode
, last_size
, NULL
);
4091 btrfs_free_path(path
);
4096 * btrfs_truncate_page - read, zero a chunk and write a page
4097 * @inode - inode that we're zeroing
4098 * @from - the offset to start zeroing
4099 * @len - the length to zero, 0 to zero the entire range respective to the
4101 * @front - zero up to the offset instead of from the offset on
4103 * This will find the page for the "from" offset and cow the page and zero the
4104 * part we want to zero. This is used with truncate and hole punching.
4106 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4109 struct address_space
*mapping
= inode
->i_mapping
;
4110 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4111 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4112 struct btrfs_ordered_extent
*ordered
;
4113 struct extent_state
*cached_state
= NULL
;
4115 u32 blocksize
= root
->sectorsize
;
4116 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4117 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4119 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4124 if ((offset
& (blocksize
- 1)) == 0 &&
4125 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4127 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4132 page
= find_or_create_page(mapping
, index
, mask
);
4134 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4139 page_start
= page_offset(page
);
4140 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4142 if (!PageUptodate(page
)) {
4143 ret
= btrfs_readpage(NULL
, page
);
4145 if (page
->mapping
!= mapping
) {
4147 page_cache_release(page
);
4150 if (!PageUptodate(page
)) {
4155 wait_on_page_writeback(page
);
4157 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4158 set_page_extent_mapped(page
);
4160 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4162 unlock_extent_cached(io_tree
, page_start
, page_end
,
4163 &cached_state
, GFP_NOFS
);
4165 page_cache_release(page
);
4166 btrfs_start_ordered_extent(inode
, ordered
, 1);
4167 btrfs_put_ordered_extent(ordered
);
4171 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4172 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4173 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4174 0, 0, &cached_state
, GFP_NOFS
);
4176 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4179 unlock_extent_cached(io_tree
, page_start
, page_end
,
4180 &cached_state
, GFP_NOFS
);
4184 if (offset
!= PAGE_CACHE_SIZE
) {
4186 len
= PAGE_CACHE_SIZE
- offset
;
4189 memset(kaddr
, 0, offset
);
4191 memset(kaddr
+ offset
, 0, len
);
4192 flush_dcache_page(page
);
4195 ClearPageChecked(page
);
4196 set_page_dirty(page
);
4197 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4202 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4204 page_cache_release(page
);
4210 * This function puts in dummy file extents for the area we're creating a hole
4211 * for. So if we are truncating this file to a larger size we need to insert
4212 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4213 * the range between oldsize and size
4215 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4217 struct btrfs_trans_handle
*trans
;
4218 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4219 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4220 struct extent_map
*em
= NULL
;
4221 struct extent_state
*cached_state
= NULL
;
4222 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4223 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4224 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4231 * If our size started in the middle of a page we need to zero out the
4232 * rest of the page before we expand the i_size, otherwise we could
4233 * expose stale data.
4235 err
= btrfs_truncate_page(inode
, oldsize
, 0, 0);
4239 if (size
<= hole_start
)
4243 struct btrfs_ordered_extent
*ordered
;
4245 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4247 ordered
= btrfs_lookup_ordered_range(inode
, hole_start
,
4248 block_end
- hole_start
);
4251 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4252 &cached_state
, GFP_NOFS
);
4253 btrfs_start_ordered_extent(inode
, ordered
, 1);
4254 btrfs_put_ordered_extent(ordered
);
4257 cur_offset
= hole_start
;
4259 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4260 block_end
- cur_offset
, 0);
4266 last_byte
= min(extent_map_end(em
), block_end
);
4267 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4268 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4269 struct extent_map
*hole_em
;
4270 hole_size
= last_byte
- cur_offset
;
4272 trans
= btrfs_start_transaction(root
, 3);
4273 if (IS_ERR(trans
)) {
4274 err
= PTR_ERR(trans
);
4278 err
= btrfs_drop_extents(trans
, root
, inode
,
4280 cur_offset
+ hole_size
, 1);
4282 btrfs_abort_transaction(trans
, root
, err
);
4283 btrfs_end_transaction(trans
, root
);
4287 err
= btrfs_insert_file_extent(trans
, root
,
4288 btrfs_ino(inode
), cur_offset
, 0,
4289 0, hole_size
, 0, hole_size
,
4292 btrfs_abort_transaction(trans
, root
, err
);
4293 btrfs_end_transaction(trans
, root
);
4297 btrfs_drop_extent_cache(inode
, cur_offset
,
4298 cur_offset
+ hole_size
- 1, 0);
4299 hole_em
= alloc_extent_map();
4301 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4302 &BTRFS_I(inode
)->runtime_flags
);
4305 hole_em
->start
= cur_offset
;
4306 hole_em
->len
= hole_size
;
4307 hole_em
->orig_start
= cur_offset
;
4309 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4310 hole_em
->block_len
= 0;
4311 hole_em
->orig_block_len
= 0;
4312 hole_em
->ram_bytes
= hole_size
;
4313 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4314 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4315 hole_em
->generation
= trans
->transid
;
4318 write_lock(&em_tree
->lock
);
4319 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4320 write_unlock(&em_tree
->lock
);
4323 btrfs_drop_extent_cache(inode
, cur_offset
,
4327 free_extent_map(hole_em
);
4329 btrfs_update_inode(trans
, root
, inode
);
4330 btrfs_end_transaction(trans
, root
);
4332 free_extent_map(em
);
4334 cur_offset
= last_byte
;
4335 if (cur_offset
>= block_end
)
4339 free_extent_map(em
);
4340 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4345 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4347 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4348 struct btrfs_trans_handle
*trans
;
4349 loff_t oldsize
= i_size_read(inode
);
4350 loff_t newsize
= attr
->ia_size
;
4351 int mask
= attr
->ia_valid
;
4355 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4356 * special case where we need to update the times despite not having
4357 * these flags set. For all other operations the VFS set these flags
4358 * explicitly if it wants a timestamp update.
4360 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
4361 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
4363 if (newsize
> oldsize
) {
4364 truncate_pagecache(inode
, newsize
);
4365 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4369 trans
= btrfs_start_transaction(root
, 1);
4371 return PTR_ERR(trans
);
4373 i_size_write(inode
, newsize
);
4374 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4375 ret
= btrfs_update_inode(trans
, root
, inode
);
4376 btrfs_end_transaction(trans
, root
);
4380 * We're truncating a file that used to have good data down to
4381 * zero. Make sure it gets into the ordered flush list so that
4382 * any new writes get down to disk quickly.
4385 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4386 &BTRFS_I(inode
)->runtime_flags
);
4389 * 1 for the orphan item we're going to add
4390 * 1 for the orphan item deletion.
4392 trans
= btrfs_start_transaction(root
, 2);
4394 return PTR_ERR(trans
);
4397 * We need to do this in case we fail at _any_ point during the
4398 * actual truncate. Once we do the truncate_setsize we could
4399 * invalidate pages which forces any outstanding ordered io to
4400 * be instantly completed which will give us extents that need
4401 * to be truncated. If we fail to get an orphan inode down we
4402 * could have left over extents that were never meant to live,
4403 * so we need to garuntee from this point on that everything
4404 * will be consistent.
4406 ret
= btrfs_orphan_add(trans
, inode
);
4407 btrfs_end_transaction(trans
, root
);
4411 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4412 truncate_setsize(inode
, newsize
);
4414 /* Disable nonlocked read DIO to avoid the end less truncate */
4415 btrfs_inode_block_unlocked_dio(inode
);
4416 inode_dio_wait(inode
);
4417 btrfs_inode_resume_unlocked_dio(inode
);
4419 ret
= btrfs_truncate(inode
);
4420 if (ret
&& inode
->i_nlink
) {
4424 * failed to truncate, disk_i_size is only adjusted down
4425 * as we remove extents, so it should represent the true
4426 * size of the inode, so reset the in memory size and
4427 * delete our orphan entry.
4429 trans
= btrfs_join_transaction(root
);
4430 if (IS_ERR(trans
)) {
4431 btrfs_orphan_del(NULL
, inode
);
4434 i_size_write(inode
, BTRFS_I(inode
)->disk_i_size
);
4435 err
= btrfs_orphan_del(trans
, inode
);
4437 btrfs_abort_transaction(trans
, root
, err
);
4438 btrfs_end_transaction(trans
, root
);
4445 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4447 struct inode
*inode
= dentry
->d_inode
;
4448 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4451 if (btrfs_root_readonly(root
))
4454 err
= inode_change_ok(inode
, attr
);
4458 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4459 err
= btrfs_setsize(inode
, attr
);
4464 if (attr
->ia_valid
) {
4465 setattr_copy(inode
, attr
);
4466 inode_inc_iversion(inode
);
4467 err
= btrfs_dirty_inode(inode
);
4469 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4470 err
= btrfs_acl_chmod(inode
);
4476 void btrfs_evict_inode(struct inode
*inode
)
4478 struct btrfs_trans_handle
*trans
;
4479 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4480 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4481 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4484 trace_btrfs_inode_evict(inode
);
4486 truncate_inode_pages(&inode
->i_data
, 0);
4487 if (inode
->i_nlink
&&
4488 ((btrfs_root_refs(&root
->root_item
) != 0 &&
4489 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
) ||
4490 btrfs_is_free_space_inode(inode
)))
4493 if (is_bad_inode(inode
)) {
4494 btrfs_orphan_del(NULL
, inode
);
4497 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4498 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4500 if (root
->fs_info
->log_root_recovering
) {
4501 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4502 &BTRFS_I(inode
)->runtime_flags
));
4506 if (inode
->i_nlink
> 0) {
4507 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0 &&
4508 root
->root_key
.objectid
!= BTRFS_ROOT_TREE_OBJECTID
);
4512 ret
= btrfs_commit_inode_delayed_inode(inode
);
4514 btrfs_orphan_del(NULL
, inode
);
4518 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4520 btrfs_orphan_del(NULL
, inode
);
4523 rsv
->size
= min_size
;
4525 global_rsv
= &root
->fs_info
->global_block_rsv
;
4527 btrfs_i_size_write(inode
, 0);
4530 * This is a bit simpler than btrfs_truncate since we've already
4531 * reserved our space for our orphan item in the unlink, so we just
4532 * need to reserve some slack space in case we add bytes and update
4533 * inode item when doing the truncate.
4536 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4537 BTRFS_RESERVE_FLUSH_LIMIT
);
4540 * Try and steal from the global reserve since we will
4541 * likely not use this space anyway, we want to try as
4542 * hard as possible to get this to work.
4545 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4548 btrfs_warn(root
->fs_info
,
4549 "Could not get space for a delete, will truncate on mount %d",
4551 btrfs_orphan_del(NULL
, inode
);
4552 btrfs_free_block_rsv(root
, rsv
);
4556 trans
= btrfs_join_transaction(root
);
4557 if (IS_ERR(trans
)) {
4558 btrfs_orphan_del(NULL
, inode
);
4559 btrfs_free_block_rsv(root
, rsv
);
4563 trans
->block_rsv
= rsv
;
4565 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4569 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4570 btrfs_end_transaction(trans
, root
);
4572 btrfs_btree_balance_dirty(root
);
4575 btrfs_free_block_rsv(root
, rsv
);
4578 * Errors here aren't a big deal, it just means we leave orphan items
4579 * in the tree. They will be cleaned up on the next mount.
4582 trans
->block_rsv
= root
->orphan_block_rsv
;
4583 btrfs_orphan_del(trans
, inode
);
4585 btrfs_orphan_del(NULL
, inode
);
4588 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4589 if (!(root
== root
->fs_info
->tree_root
||
4590 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4591 btrfs_return_ino(root
, btrfs_ino(inode
));
4593 btrfs_end_transaction(trans
, root
);
4594 btrfs_btree_balance_dirty(root
);
4596 btrfs_remove_delayed_node(inode
);
4602 * this returns the key found in the dir entry in the location pointer.
4603 * If no dir entries were found, location->objectid is 0.
4605 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4606 struct btrfs_key
*location
)
4608 const char *name
= dentry
->d_name
.name
;
4609 int namelen
= dentry
->d_name
.len
;
4610 struct btrfs_dir_item
*di
;
4611 struct btrfs_path
*path
;
4612 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4615 path
= btrfs_alloc_path();
4619 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4624 if (IS_ERR_OR_NULL(di
))
4627 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4629 btrfs_free_path(path
);
4632 location
->objectid
= 0;
4637 * when we hit a tree root in a directory, the btrfs part of the inode
4638 * needs to be changed to reflect the root directory of the tree root. This
4639 * is kind of like crossing a mount point.
4641 static int fixup_tree_root_location(struct btrfs_root
*root
,
4643 struct dentry
*dentry
,
4644 struct btrfs_key
*location
,
4645 struct btrfs_root
**sub_root
)
4647 struct btrfs_path
*path
;
4648 struct btrfs_root
*new_root
;
4649 struct btrfs_root_ref
*ref
;
4650 struct extent_buffer
*leaf
;
4654 path
= btrfs_alloc_path();
4661 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4662 BTRFS_I(dir
)->root
->root_key
.objectid
,
4663 location
->objectid
);
4670 leaf
= path
->nodes
[0];
4671 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4672 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4673 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4676 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4677 (unsigned long)(ref
+ 1),
4678 dentry
->d_name
.len
);
4682 btrfs_release_path(path
);
4684 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4685 if (IS_ERR(new_root
)) {
4686 err
= PTR_ERR(new_root
);
4690 *sub_root
= new_root
;
4691 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4692 location
->type
= BTRFS_INODE_ITEM_KEY
;
4693 location
->offset
= 0;
4696 btrfs_free_path(path
);
4700 static void inode_tree_add(struct inode
*inode
)
4702 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4703 struct btrfs_inode
*entry
;
4705 struct rb_node
*parent
;
4706 struct rb_node
*new = &BTRFS_I(inode
)->rb_node
;
4707 u64 ino
= btrfs_ino(inode
);
4709 if (inode_unhashed(inode
))
4712 spin_lock(&root
->inode_lock
);
4713 p
= &root
->inode_tree
.rb_node
;
4716 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4718 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4719 p
= &parent
->rb_left
;
4720 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4721 p
= &parent
->rb_right
;
4723 WARN_ON(!(entry
->vfs_inode
.i_state
&
4724 (I_WILL_FREE
| I_FREEING
)));
4725 rb_replace_node(parent
, new, &root
->inode_tree
);
4726 RB_CLEAR_NODE(parent
);
4727 spin_unlock(&root
->inode_lock
);
4731 rb_link_node(new, parent
, p
);
4732 rb_insert_color(new, &root
->inode_tree
);
4733 spin_unlock(&root
->inode_lock
);
4736 static void inode_tree_del(struct inode
*inode
)
4738 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4741 spin_lock(&root
->inode_lock
);
4742 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4743 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4744 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4745 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4747 spin_unlock(&root
->inode_lock
);
4749 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0) {
4750 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4751 spin_lock(&root
->inode_lock
);
4752 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4753 spin_unlock(&root
->inode_lock
);
4755 btrfs_add_dead_root(root
);
4759 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4761 struct rb_node
*node
;
4762 struct rb_node
*prev
;
4763 struct btrfs_inode
*entry
;
4764 struct inode
*inode
;
4767 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4769 spin_lock(&root
->inode_lock
);
4771 node
= root
->inode_tree
.rb_node
;
4775 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4777 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4778 node
= node
->rb_left
;
4779 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4780 node
= node
->rb_right
;
4786 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4787 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4791 prev
= rb_next(prev
);
4795 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4796 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4797 inode
= igrab(&entry
->vfs_inode
);
4799 spin_unlock(&root
->inode_lock
);
4800 if (atomic_read(&inode
->i_count
) > 1)
4801 d_prune_aliases(inode
);
4803 * btrfs_drop_inode will have it removed from
4804 * the inode cache when its usage count
4809 spin_lock(&root
->inode_lock
);
4813 if (cond_resched_lock(&root
->inode_lock
))
4816 node
= rb_next(node
);
4818 spin_unlock(&root
->inode_lock
);
4821 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4823 struct btrfs_iget_args
*args
= p
;
4824 inode
->i_ino
= args
->ino
;
4825 BTRFS_I(inode
)->root
= args
->root
;
4829 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4831 struct btrfs_iget_args
*args
= opaque
;
4832 return args
->ino
== btrfs_ino(inode
) &&
4833 args
->root
== BTRFS_I(inode
)->root
;
4836 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4838 struct btrfs_root
*root
)
4840 struct inode
*inode
;
4841 struct btrfs_iget_args args
;
4842 unsigned long hashval
= btrfs_inode_hash(objectid
, root
);
4844 args
.ino
= objectid
;
4847 inode
= iget5_locked(s
, hashval
, btrfs_find_actor
,
4848 btrfs_init_locked_inode
,
4853 /* Get an inode object given its location and corresponding root.
4854 * Returns in *is_new if the inode was read from disk
4856 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4857 struct btrfs_root
*root
, int *new)
4859 struct inode
*inode
;
4861 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4863 return ERR_PTR(-ENOMEM
);
4865 if (inode
->i_state
& I_NEW
) {
4866 BTRFS_I(inode
)->root
= root
;
4867 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4868 btrfs_read_locked_inode(inode
);
4869 if (!is_bad_inode(inode
)) {
4870 inode_tree_add(inode
);
4871 unlock_new_inode(inode
);
4875 unlock_new_inode(inode
);
4877 inode
= ERR_PTR(-ESTALE
);
4884 static struct inode
*new_simple_dir(struct super_block
*s
,
4885 struct btrfs_key
*key
,
4886 struct btrfs_root
*root
)
4888 struct inode
*inode
= new_inode(s
);
4891 return ERR_PTR(-ENOMEM
);
4893 BTRFS_I(inode
)->root
= root
;
4894 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4895 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
4897 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4898 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
4899 inode
->i_fop
= &simple_dir_operations
;
4900 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4901 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4906 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4908 struct inode
*inode
;
4909 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4910 struct btrfs_root
*sub_root
= root
;
4911 struct btrfs_key location
;
4915 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4916 return ERR_PTR(-ENAMETOOLONG
);
4918 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4920 return ERR_PTR(ret
);
4922 if (location
.objectid
== 0)
4925 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4926 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4930 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4932 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4933 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4934 &location
, &sub_root
);
4937 inode
= ERR_PTR(ret
);
4939 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4941 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4943 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4945 if (!IS_ERR(inode
) && root
!= sub_root
) {
4946 down_read(&root
->fs_info
->cleanup_work_sem
);
4947 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4948 ret
= btrfs_orphan_cleanup(sub_root
);
4949 up_read(&root
->fs_info
->cleanup_work_sem
);
4952 inode
= ERR_PTR(ret
);
4959 static int btrfs_dentry_delete(const struct dentry
*dentry
)
4961 struct btrfs_root
*root
;
4962 struct inode
*inode
= dentry
->d_inode
;
4964 if (!inode
&& !IS_ROOT(dentry
))
4965 inode
= dentry
->d_parent
->d_inode
;
4968 root
= BTRFS_I(inode
)->root
;
4969 if (btrfs_root_refs(&root
->root_item
) == 0)
4972 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
4978 static void btrfs_dentry_release(struct dentry
*dentry
)
4980 if (dentry
->d_fsdata
)
4981 kfree(dentry
->d_fsdata
);
4984 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4989 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
4993 unsigned char btrfs_filetype_table
[] = {
4994 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4997 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
4999 struct inode
*inode
= file_inode(file
);
5000 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5001 struct btrfs_item
*item
;
5002 struct btrfs_dir_item
*di
;
5003 struct btrfs_key key
;
5004 struct btrfs_key found_key
;
5005 struct btrfs_path
*path
;
5006 struct list_head ins_list
;
5007 struct list_head del_list
;
5009 struct extent_buffer
*leaf
;
5011 unsigned char d_type
;
5016 int key_type
= BTRFS_DIR_INDEX_KEY
;
5020 int is_curr
= 0; /* ctx->pos points to the current index? */
5022 /* FIXME, use a real flag for deciding about the key type */
5023 if (root
->fs_info
->tree_root
== root
)
5024 key_type
= BTRFS_DIR_ITEM_KEY
;
5026 if (!dir_emit_dots(file
, ctx
))
5029 path
= btrfs_alloc_path();
5035 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5036 INIT_LIST_HEAD(&ins_list
);
5037 INIT_LIST_HEAD(&del_list
);
5038 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5041 btrfs_set_key_type(&key
, key_type
);
5042 key
.offset
= ctx
->pos
;
5043 key
.objectid
= btrfs_ino(inode
);
5045 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5050 leaf
= path
->nodes
[0];
5051 slot
= path
->slots
[0];
5052 if (slot
>= btrfs_header_nritems(leaf
)) {
5053 ret
= btrfs_next_leaf(root
, path
);
5061 item
= btrfs_item_nr(slot
);
5062 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5064 if (found_key
.objectid
!= key
.objectid
)
5066 if (btrfs_key_type(&found_key
) != key_type
)
5068 if (found_key
.offset
< ctx
->pos
)
5070 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5071 btrfs_should_delete_dir_index(&del_list
,
5075 ctx
->pos
= found_key
.offset
;
5078 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5080 di_total
= btrfs_item_size(leaf
, item
);
5082 while (di_cur
< di_total
) {
5083 struct btrfs_key location
;
5085 if (verify_dir_item(root
, leaf
, di
))
5088 name_len
= btrfs_dir_name_len(leaf
, di
);
5089 if (name_len
<= sizeof(tmp_name
)) {
5090 name_ptr
= tmp_name
;
5092 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5098 read_extent_buffer(leaf
, name_ptr
,
5099 (unsigned long)(di
+ 1), name_len
);
5101 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5102 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5105 /* is this a reference to our own snapshot? If so
5108 * In contrast to old kernels, we insert the snapshot's
5109 * dir item and dir index after it has been created, so
5110 * we won't find a reference to our own snapshot. We
5111 * still keep the following code for backward
5114 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5115 location
.objectid
== root
->root_key
.objectid
) {
5119 over
= !dir_emit(ctx
, name_ptr
, name_len
,
5120 location
.objectid
, d_type
);
5123 if (name_ptr
!= tmp_name
)
5128 di_len
= btrfs_dir_name_len(leaf
, di
) +
5129 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5131 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5137 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5140 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
);
5145 /* Reached end of directory/root. Bump pos past the last item. */
5149 * Stop new entries from being returned after we return the last
5152 * New directory entries are assigned a strictly increasing
5153 * offset. This means that new entries created during readdir
5154 * are *guaranteed* to be seen in the future by that readdir.
5155 * This has broken buggy programs which operate on names as
5156 * they're returned by readdir. Until we re-use freed offsets
5157 * we have this hack to stop new entries from being returned
5158 * under the assumption that they'll never reach this huge
5161 * This is being careful not to overflow 32bit loff_t unless the
5162 * last entry requires it because doing so has broken 32bit apps
5165 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5166 if (ctx
->pos
>= INT_MAX
)
5167 ctx
->pos
= LLONG_MAX
;
5174 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5175 btrfs_put_delayed_items(&ins_list
, &del_list
);
5176 btrfs_free_path(path
);
5180 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5182 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5183 struct btrfs_trans_handle
*trans
;
5185 bool nolock
= false;
5187 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5190 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5193 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5195 trans
= btrfs_join_transaction_nolock(root
);
5197 trans
= btrfs_join_transaction(root
);
5199 return PTR_ERR(trans
);
5200 ret
= btrfs_commit_transaction(trans
, root
);
5206 * This is somewhat expensive, updating the tree every time the
5207 * inode changes. But, it is most likely to find the inode in cache.
5208 * FIXME, needs more benchmarking...there are no reasons other than performance
5209 * to keep or drop this code.
5211 static int btrfs_dirty_inode(struct inode
*inode
)
5213 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5214 struct btrfs_trans_handle
*trans
;
5217 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5220 trans
= btrfs_join_transaction(root
);
5222 return PTR_ERR(trans
);
5224 ret
= btrfs_update_inode(trans
, root
, inode
);
5225 if (ret
&& ret
== -ENOSPC
) {
5226 /* whoops, lets try again with the full transaction */
5227 btrfs_end_transaction(trans
, root
);
5228 trans
= btrfs_start_transaction(root
, 1);
5230 return PTR_ERR(trans
);
5232 ret
= btrfs_update_inode(trans
, root
, inode
);
5234 btrfs_end_transaction(trans
, root
);
5235 if (BTRFS_I(inode
)->delayed_node
)
5236 btrfs_balance_delayed_items(root
);
5242 * This is a copy of file_update_time. We need this so we can return error on
5243 * ENOSPC for updating the inode in the case of file write and mmap writes.
5245 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5248 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5250 if (btrfs_root_readonly(root
))
5253 if (flags
& S_VERSION
)
5254 inode_inc_iversion(inode
);
5255 if (flags
& S_CTIME
)
5256 inode
->i_ctime
= *now
;
5257 if (flags
& S_MTIME
)
5258 inode
->i_mtime
= *now
;
5259 if (flags
& S_ATIME
)
5260 inode
->i_atime
= *now
;
5261 return btrfs_dirty_inode(inode
);
5265 * find the highest existing sequence number in a directory
5266 * and then set the in-memory index_cnt variable to reflect
5267 * free sequence numbers
5269 static int btrfs_set_inode_index_count(struct inode
*inode
)
5271 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5272 struct btrfs_key key
, found_key
;
5273 struct btrfs_path
*path
;
5274 struct extent_buffer
*leaf
;
5277 key
.objectid
= btrfs_ino(inode
);
5278 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
5279 key
.offset
= (u64
)-1;
5281 path
= btrfs_alloc_path();
5285 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5288 /* FIXME: we should be able to handle this */
5294 * MAGIC NUMBER EXPLANATION:
5295 * since we search a directory based on f_pos we have to start at 2
5296 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5297 * else has to start at 2
5299 if (path
->slots
[0] == 0) {
5300 BTRFS_I(inode
)->index_cnt
= 2;
5306 leaf
= path
->nodes
[0];
5307 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5309 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5310 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
5311 BTRFS_I(inode
)->index_cnt
= 2;
5315 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5317 btrfs_free_path(path
);
5322 * helper to find a free sequence number in a given directory. This current
5323 * code is very simple, later versions will do smarter things in the btree
5325 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5329 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5330 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5332 ret
= btrfs_set_inode_index_count(dir
);
5338 *index
= BTRFS_I(dir
)->index_cnt
;
5339 BTRFS_I(dir
)->index_cnt
++;
5344 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5345 struct btrfs_root
*root
,
5347 const char *name
, int name_len
,
5348 u64 ref_objectid
, u64 objectid
,
5349 umode_t mode
, u64
*index
)
5351 struct inode
*inode
;
5352 struct btrfs_inode_item
*inode_item
;
5353 struct btrfs_key
*location
;
5354 struct btrfs_path
*path
;
5355 struct btrfs_inode_ref
*ref
;
5356 struct btrfs_key key
[2];
5362 path
= btrfs_alloc_path();
5364 return ERR_PTR(-ENOMEM
);
5366 inode
= new_inode(root
->fs_info
->sb
);
5368 btrfs_free_path(path
);
5369 return ERR_PTR(-ENOMEM
);
5373 * we have to initialize this early, so we can reclaim the inode
5374 * number if we fail afterwards in this function.
5376 inode
->i_ino
= objectid
;
5379 trace_btrfs_inode_request(dir
);
5381 ret
= btrfs_set_inode_index(dir
, index
);
5383 btrfs_free_path(path
);
5385 return ERR_PTR(ret
);
5389 * index_cnt is ignored for everything but a dir,
5390 * btrfs_get_inode_index_count has an explanation for the magic
5393 BTRFS_I(inode
)->index_cnt
= 2;
5394 BTRFS_I(inode
)->root
= root
;
5395 BTRFS_I(inode
)->generation
= trans
->transid
;
5396 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5399 * We could have gotten an inode number from somebody who was fsynced
5400 * and then removed in this same transaction, so let's just set full
5401 * sync since it will be a full sync anyway and this will blow away the
5402 * old info in the log.
5404 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5411 key
[0].objectid
= objectid
;
5412 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
5416 * Start new inodes with an inode_ref. This is slightly more
5417 * efficient for small numbers of hard links since they will
5418 * be packed into one item. Extended refs will kick in if we
5419 * add more hard links than can fit in the ref item.
5421 key
[1].objectid
= objectid
;
5422 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
5423 key
[1].offset
= ref_objectid
;
5425 sizes
[0] = sizeof(struct btrfs_inode_item
);
5426 sizes
[1] = name_len
+ sizeof(*ref
);
5428 path
->leave_spinning
= 1;
5429 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
5433 inode_init_owner(inode
, dir
, mode
);
5434 inode_set_bytes(inode
, 0);
5435 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5436 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5437 struct btrfs_inode_item
);
5438 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5439 sizeof(*inode_item
));
5440 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5442 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5443 struct btrfs_inode_ref
);
5444 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5445 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5446 ptr
= (unsigned long)(ref
+ 1);
5447 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5449 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5450 btrfs_free_path(path
);
5452 location
= &BTRFS_I(inode
)->location
;
5453 location
->objectid
= objectid
;
5454 location
->offset
= 0;
5455 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
5457 btrfs_inherit_iflags(inode
, dir
);
5459 if (S_ISREG(mode
)) {
5460 if (btrfs_test_opt(root
, NODATASUM
))
5461 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5462 if (btrfs_test_opt(root
, NODATACOW
))
5463 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5464 BTRFS_INODE_NODATASUM
;
5467 btrfs_insert_inode_hash(inode
);
5468 inode_tree_add(inode
);
5470 trace_btrfs_inode_new(inode
);
5471 btrfs_set_inode_last_trans(trans
, inode
);
5473 btrfs_update_root_times(trans
, root
);
5478 BTRFS_I(dir
)->index_cnt
--;
5479 btrfs_free_path(path
);
5481 return ERR_PTR(ret
);
5484 static inline u8
btrfs_inode_type(struct inode
*inode
)
5486 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5490 * utility function to add 'inode' into 'parent_inode' with
5491 * a give name and a given sequence number.
5492 * if 'add_backref' is true, also insert a backref from the
5493 * inode to the parent directory.
5495 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5496 struct inode
*parent_inode
, struct inode
*inode
,
5497 const char *name
, int name_len
, int add_backref
, u64 index
)
5500 struct btrfs_key key
;
5501 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
5502 u64 ino
= btrfs_ino(inode
);
5503 u64 parent_ino
= btrfs_ino(parent_inode
);
5505 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5506 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
5509 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
5513 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5514 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
5515 key
.objectid
, root
->root_key
.objectid
,
5516 parent_ino
, index
, name
, name_len
);
5517 } else if (add_backref
) {
5518 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
5522 /* Nothing to clean up yet */
5526 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
5528 btrfs_inode_type(inode
), index
);
5529 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
5532 btrfs_abort_transaction(trans
, root
, ret
);
5536 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5538 inode_inc_iversion(parent_inode
);
5539 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5540 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5542 btrfs_abort_transaction(trans
, root
, ret
);
5546 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5549 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5550 key
.objectid
, root
->root_key
.objectid
,
5551 parent_ino
, &local_index
, name
, name_len
);
5553 } else if (add_backref
) {
5557 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5558 ino
, parent_ino
, &local_index
);
5563 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5564 struct inode
*dir
, struct dentry
*dentry
,
5565 struct inode
*inode
, int backref
, u64 index
)
5567 int err
= btrfs_add_link(trans
, dir
, inode
,
5568 dentry
->d_name
.name
, dentry
->d_name
.len
,
5575 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5576 umode_t mode
, dev_t rdev
)
5578 struct btrfs_trans_handle
*trans
;
5579 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5580 struct inode
*inode
= NULL
;
5586 if (!new_valid_dev(rdev
))
5590 * 2 for inode item and ref
5592 * 1 for xattr if selinux is on
5594 trans
= btrfs_start_transaction(root
, 5);
5596 return PTR_ERR(trans
);
5598 err
= btrfs_find_free_ino(root
, &objectid
);
5602 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5603 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5605 if (IS_ERR(inode
)) {
5606 err
= PTR_ERR(inode
);
5610 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5617 * If the active LSM wants to access the inode during
5618 * d_instantiate it needs these. Smack checks to see
5619 * if the filesystem supports xattrs by looking at the
5623 inode
->i_op
= &btrfs_special_inode_operations
;
5624 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5628 init_special_inode(inode
, inode
->i_mode
, rdev
);
5629 btrfs_update_inode(trans
, root
, inode
);
5630 d_instantiate(dentry
, inode
);
5633 btrfs_end_transaction(trans
, root
);
5634 btrfs_btree_balance_dirty(root
);
5636 inode_dec_link_count(inode
);
5642 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5643 umode_t mode
, bool excl
)
5645 struct btrfs_trans_handle
*trans
;
5646 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5647 struct inode
*inode
= NULL
;
5648 int drop_inode_on_err
= 0;
5654 * 2 for inode item and ref
5656 * 1 for xattr if selinux is on
5658 trans
= btrfs_start_transaction(root
, 5);
5660 return PTR_ERR(trans
);
5662 err
= btrfs_find_free_ino(root
, &objectid
);
5666 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5667 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5669 if (IS_ERR(inode
)) {
5670 err
= PTR_ERR(inode
);
5673 drop_inode_on_err
= 1;
5675 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5679 err
= btrfs_update_inode(trans
, root
, inode
);
5684 * If the active LSM wants to access the inode during
5685 * d_instantiate it needs these. Smack checks to see
5686 * if the filesystem supports xattrs by looking at the
5689 inode
->i_fop
= &btrfs_file_operations
;
5690 inode
->i_op
= &btrfs_file_inode_operations
;
5692 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5696 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5697 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5698 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5699 d_instantiate(dentry
, inode
);
5702 btrfs_end_transaction(trans
, root
);
5703 if (err
&& drop_inode_on_err
) {
5704 inode_dec_link_count(inode
);
5707 btrfs_btree_balance_dirty(root
);
5711 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5712 struct dentry
*dentry
)
5714 struct btrfs_trans_handle
*trans
;
5715 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5716 struct inode
*inode
= old_dentry
->d_inode
;
5721 /* do not allow sys_link's with other subvols of the same device */
5722 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5725 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5728 err
= btrfs_set_inode_index(dir
, &index
);
5733 * 2 items for inode and inode ref
5734 * 2 items for dir items
5735 * 1 item for parent inode
5737 trans
= btrfs_start_transaction(root
, 5);
5738 if (IS_ERR(trans
)) {
5739 err
= PTR_ERR(trans
);
5744 inode_inc_iversion(inode
);
5745 inode
->i_ctime
= CURRENT_TIME
;
5747 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5749 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5754 struct dentry
*parent
= dentry
->d_parent
;
5755 err
= btrfs_update_inode(trans
, root
, inode
);
5758 d_instantiate(dentry
, inode
);
5759 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5762 btrfs_end_transaction(trans
, root
);
5765 inode_dec_link_count(inode
);
5768 btrfs_btree_balance_dirty(root
);
5772 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5774 struct inode
*inode
= NULL
;
5775 struct btrfs_trans_handle
*trans
;
5776 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5778 int drop_on_err
= 0;
5783 * 2 items for inode and ref
5784 * 2 items for dir items
5785 * 1 for xattr if selinux is on
5787 trans
= btrfs_start_transaction(root
, 5);
5789 return PTR_ERR(trans
);
5791 err
= btrfs_find_free_ino(root
, &objectid
);
5795 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5796 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5797 S_IFDIR
| mode
, &index
);
5798 if (IS_ERR(inode
)) {
5799 err
= PTR_ERR(inode
);
5805 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5809 inode
->i_op
= &btrfs_dir_inode_operations
;
5810 inode
->i_fop
= &btrfs_dir_file_operations
;
5812 btrfs_i_size_write(inode
, 0);
5813 err
= btrfs_update_inode(trans
, root
, inode
);
5817 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5818 dentry
->d_name
.len
, 0, index
);
5822 d_instantiate(dentry
, inode
);
5826 btrfs_end_transaction(trans
, root
);
5829 btrfs_btree_balance_dirty(root
);
5833 /* helper for btfs_get_extent. Given an existing extent in the tree,
5834 * and an extent that you want to insert, deal with overlap and insert
5835 * the new extent into the tree.
5837 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5838 struct extent_map
*existing
,
5839 struct extent_map
*em
,
5840 u64 map_start
, u64 map_len
)
5844 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5845 start_diff
= map_start
- em
->start
;
5846 em
->start
= map_start
;
5848 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5849 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5850 em
->block_start
+= start_diff
;
5851 em
->block_len
-= start_diff
;
5853 return add_extent_mapping(em_tree
, em
, 0);
5856 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5857 struct inode
*inode
, struct page
*page
,
5858 size_t pg_offset
, u64 extent_offset
,
5859 struct btrfs_file_extent_item
*item
)
5862 struct extent_buffer
*leaf
= path
->nodes
[0];
5865 unsigned long inline_size
;
5869 WARN_ON(pg_offset
!= 0);
5870 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5871 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
5872 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
5873 btrfs_item_nr(path
->slots
[0]));
5874 tmp
= kmalloc(inline_size
, GFP_NOFS
);
5877 ptr
= btrfs_file_extent_inline_start(item
);
5879 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
5881 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
5882 ret
= btrfs_decompress(compress_type
, tmp
, page
,
5883 extent_offset
, inline_size
, max_size
);
5885 char *kaddr
= kmap_atomic(page
);
5886 unsigned long copy_size
= min_t(u64
,
5887 PAGE_CACHE_SIZE
- pg_offset
,
5888 max_size
- extent_offset
);
5889 memset(kaddr
+ pg_offset
, 0, copy_size
);
5890 kunmap_atomic(kaddr
);
5897 * a bit scary, this does extent mapping from logical file offset to the disk.
5898 * the ugly parts come from merging extents from the disk with the in-ram
5899 * representation. This gets more complex because of the data=ordered code,
5900 * where the in-ram extents might be locked pending data=ordered completion.
5902 * This also copies inline extents directly into the page.
5905 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
5906 size_t pg_offset
, u64 start
, u64 len
,
5912 u64 extent_start
= 0;
5914 u64 objectid
= btrfs_ino(inode
);
5916 struct btrfs_path
*path
= NULL
;
5917 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5918 struct btrfs_file_extent_item
*item
;
5919 struct extent_buffer
*leaf
;
5920 struct btrfs_key found_key
;
5921 struct extent_map
*em
= NULL
;
5922 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5923 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5924 struct btrfs_trans_handle
*trans
= NULL
;
5928 read_lock(&em_tree
->lock
);
5929 em
= lookup_extent_mapping(em_tree
, start
, len
);
5931 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5932 read_unlock(&em_tree
->lock
);
5935 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
5936 free_extent_map(em
);
5937 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5938 free_extent_map(em
);
5942 em
= alloc_extent_map();
5947 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5948 em
->start
= EXTENT_MAP_HOLE
;
5949 em
->orig_start
= EXTENT_MAP_HOLE
;
5951 em
->block_len
= (u64
)-1;
5954 path
= btrfs_alloc_path();
5960 * Chances are we'll be called again, so go ahead and do
5966 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5967 objectid
, start
, trans
!= NULL
);
5974 if (path
->slots
[0] == 0)
5979 leaf
= path
->nodes
[0];
5980 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5981 struct btrfs_file_extent_item
);
5982 /* are we inside the extent that was found? */
5983 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5984 found_type
= btrfs_key_type(&found_key
);
5985 if (found_key
.objectid
!= objectid
||
5986 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5988 * If we backup past the first extent we want to move forward
5989 * and see if there is an extent in front of us, otherwise we'll
5990 * say there is a hole for our whole search range which can
5997 found_type
= btrfs_file_extent_type(leaf
, item
);
5998 extent_start
= found_key
.offset
;
5999 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6000 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6001 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6002 extent_end
= extent_start
+
6003 btrfs_file_extent_num_bytes(leaf
, item
);
6004 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6006 size
= btrfs_file_extent_inline_len(leaf
, item
);
6007 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6010 if (start
>= extent_end
) {
6012 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6013 ret
= btrfs_next_leaf(root
, path
);
6020 leaf
= path
->nodes
[0];
6022 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6023 if (found_key
.objectid
!= objectid
||
6024 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6026 if (start
+ len
<= found_key
.offset
)
6029 em
->orig_start
= start
;
6030 em
->len
= found_key
.offset
- start
;
6034 em
->ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, item
);
6035 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6036 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6037 em
->start
= extent_start
;
6038 em
->len
= extent_end
- extent_start
;
6039 em
->orig_start
= extent_start
-
6040 btrfs_file_extent_offset(leaf
, item
);
6041 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
6043 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
6045 em
->block_start
= EXTENT_MAP_HOLE
;
6048 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
6049 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6050 em
->compress_type
= compress_type
;
6051 em
->block_start
= bytenr
;
6052 em
->block_len
= em
->orig_block_len
;
6054 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
6055 em
->block_start
= bytenr
;
6056 em
->block_len
= em
->len
;
6057 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
6058 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6061 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6065 size_t extent_offset
;
6068 em
->block_start
= EXTENT_MAP_INLINE
;
6069 if (!page
|| create
) {
6070 em
->start
= extent_start
;
6071 em
->len
= extent_end
- extent_start
;
6075 size
= btrfs_file_extent_inline_len(leaf
, item
);
6076 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6077 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6078 size
- extent_offset
);
6079 em
->start
= extent_start
+ extent_offset
;
6080 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6081 em
->orig_block_len
= em
->len
;
6082 em
->orig_start
= em
->start
;
6083 if (compress_type
) {
6084 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6085 em
->compress_type
= compress_type
;
6087 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6088 if (create
== 0 && !PageUptodate(page
)) {
6089 if (btrfs_file_extent_compression(leaf
, item
) !=
6090 BTRFS_COMPRESS_NONE
) {
6091 ret
= uncompress_inline(path
, inode
, page
,
6093 extent_offset
, item
);
6094 BUG_ON(ret
); /* -ENOMEM */
6097 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6099 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6100 memset(map
+ pg_offset
+ copy_size
, 0,
6101 PAGE_CACHE_SIZE
- pg_offset
-
6106 flush_dcache_page(page
);
6107 } else if (create
&& PageUptodate(page
)) {
6111 free_extent_map(em
);
6114 btrfs_release_path(path
);
6115 trans
= btrfs_join_transaction(root
);
6118 return ERR_CAST(trans
);
6122 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6125 btrfs_mark_buffer_dirty(leaf
);
6127 set_extent_uptodate(io_tree
, em
->start
,
6128 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6131 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
6135 em
->orig_start
= start
;
6138 em
->block_start
= EXTENT_MAP_HOLE
;
6139 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6141 btrfs_release_path(path
);
6142 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6143 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6144 em
->start
, em
->len
, start
, len
);
6150 write_lock(&em_tree
->lock
);
6151 ret
= add_extent_mapping(em_tree
, em
, 0);
6152 /* it is possible that someone inserted the extent into the tree
6153 * while we had the lock dropped. It is also possible that
6154 * an overlapping map exists in the tree
6156 if (ret
== -EEXIST
) {
6157 struct extent_map
*existing
;
6161 existing
= lookup_extent_mapping(em_tree
, start
, len
);
6162 if (existing
&& (existing
->start
> start
||
6163 existing
->start
+ existing
->len
<= start
)) {
6164 free_extent_map(existing
);
6168 existing
= lookup_extent_mapping(em_tree
, em
->start
,
6171 err
= merge_extent_mapping(em_tree
, existing
,
6174 free_extent_map(existing
);
6176 free_extent_map(em
);
6181 free_extent_map(em
);
6185 free_extent_map(em
);
6190 write_unlock(&em_tree
->lock
);
6194 trace_btrfs_get_extent(root
, em
);
6197 btrfs_free_path(path
);
6199 ret
= btrfs_end_transaction(trans
, root
);
6204 free_extent_map(em
);
6205 return ERR_PTR(err
);
6207 BUG_ON(!em
); /* Error is always set */
6211 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6212 size_t pg_offset
, u64 start
, u64 len
,
6215 struct extent_map
*em
;
6216 struct extent_map
*hole_em
= NULL
;
6217 u64 range_start
= start
;
6223 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6230 * - a pre-alloc extent,
6231 * there might actually be delalloc bytes behind it.
6233 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6234 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6240 /* check to see if we've wrapped (len == -1 or similar) */
6249 /* ok, we didn't find anything, lets look for delalloc */
6250 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6251 end
, len
, EXTENT_DELALLOC
, 1);
6252 found_end
= range_start
+ found
;
6253 if (found_end
< range_start
)
6254 found_end
= (u64
)-1;
6257 * we didn't find anything useful, return
6258 * the original results from get_extent()
6260 if (range_start
> end
|| found_end
<= start
) {
6266 /* adjust the range_start to make sure it doesn't
6267 * go backwards from the start they passed in
6269 range_start
= max(start
,range_start
);
6270 found
= found_end
- range_start
;
6273 u64 hole_start
= start
;
6276 em
= alloc_extent_map();
6282 * when btrfs_get_extent can't find anything it
6283 * returns one huge hole
6285 * make sure what it found really fits our range, and
6286 * adjust to make sure it is based on the start from
6290 u64 calc_end
= extent_map_end(hole_em
);
6292 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6293 free_extent_map(hole_em
);
6296 hole_start
= max(hole_em
->start
, start
);
6297 hole_len
= calc_end
- hole_start
;
6301 if (hole_em
&& range_start
> hole_start
) {
6302 /* our hole starts before our delalloc, so we
6303 * have to return just the parts of the hole
6304 * that go until the delalloc starts
6306 em
->len
= min(hole_len
,
6307 range_start
- hole_start
);
6308 em
->start
= hole_start
;
6309 em
->orig_start
= hole_start
;
6311 * don't adjust block start at all,
6312 * it is fixed at EXTENT_MAP_HOLE
6314 em
->block_start
= hole_em
->block_start
;
6315 em
->block_len
= hole_len
;
6316 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6317 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6319 em
->start
= range_start
;
6321 em
->orig_start
= range_start
;
6322 em
->block_start
= EXTENT_MAP_DELALLOC
;
6323 em
->block_len
= found
;
6325 } else if (hole_em
) {
6330 free_extent_map(hole_em
);
6332 free_extent_map(em
);
6333 return ERR_PTR(err
);
6338 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6341 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6342 struct extent_map
*em
;
6343 struct btrfs_key ins
;
6347 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6348 ret
= btrfs_reserve_extent(root
, len
, root
->sectorsize
, 0,
6349 alloc_hint
, &ins
, 1);
6351 return ERR_PTR(ret
);
6353 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6354 ins
.offset
, ins
.offset
, ins
.offset
, 0);
6356 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6360 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6361 ins
.offset
, ins
.offset
, 0);
6363 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6364 free_extent_map(em
);
6365 return ERR_PTR(ret
);
6372 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6373 * block must be cow'd
6375 noinline
int can_nocow_extent(struct inode
*inode
, u64 offset
, u64
*len
,
6376 u64
*orig_start
, u64
*orig_block_len
,
6379 struct btrfs_trans_handle
*trans
;
6380 struct btrfs_path
*path
;
6382 struct extent_buffer
*leaf
;
6383 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6384 struct btrfs_file_extent_item
*fi
;
6385 struct btrfs_key key
;
6392 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
6393 path
= btrfs_alloc_path();
6397 ret
= btrfs_lookup_file_extent(NULL
, root
, path
, btrfs_ino(inode
),
6402 slot
= path
->slots
[0];
6405 /* can't find the item, must cow */
6412 leaf
= path
->nodes
[0];
6413 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6414 if (key
.objectid
!= btrfs_ino(inode
) ||
6415 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6416 /* not our file or wrong item type, must cow */
6420 if (key
.offset
> offset
) {
6421 /* Wrong offset, must cow */
6425 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6426 found_type
= btrfs_file_extent_type(leaf
, fi
);
6427 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6428 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6429 /* not a regular extent, must cow */
6433 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
6436 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6437 if (disk_bytenr
== 0)
6440 if (btrfs_file_extent_compression(leaf
, fi
) ||
6441 btrfs_file_extent_encryption(leaf
, fi
) ||
6442 btrfs_file_extent_other_encoding(leaf
, fi
))
6445 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6448 *orig_start
= key
.offset
- backref_offset
;
6449 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
6450 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
6453 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6455 if (btrfs_extent_readonly(root
, disk_bytenr
))
6457 btrfs_release_path(path
);
6460 * look for other files referencing this extent, if we
6461 * find any we must cow
6463 trans
= btrfs_join_transaction(root
);
6464 if (IS_ERR(trans
)) {
6469 ret
= btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
6470 key
.offset
- backref_offset
, disk_bytenr
);
6471 btrfs_end_transaction(trans
, root
);
6478 * adjust disk_bytenr and num_bytes to cover just the bytes
6479 * in this extent we are about to write. If there
6480 * are any csums in that range we have to cow in order
6481 * to keep the csums correct
6483 disk_bytenr
+= backref_offset
;
6484 disk_bytenr
+= offset
- key
.offset
;
6485 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
6486 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
6489 * all of the above have passed, it is safe to overwrite this extent
6495 btrfs_free_path(path
);
6499 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
6500 struct extent_state
**cached_state
, int writing
)
6502 struct btrfs_ordered_extent
*ordered
;
6506 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6509 * We're concerned with the entire range that we're going to be
6510 * doing DIO to, so we need to make sure theres no ordered
6511 * extents in this range.
6513 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6514 lockend
- lockstart
+ 1);
6517 * We need to make sure there are no buffered pages in this
6518 * range either, we could have raced between the invalidate in
6519 * generic_file_direct_write and locking the extent. The
6520 * invalidate needs to happen so that reads after a write do not
6523 if (!ordered
&& (!writing
||
6524 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
6525 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
6529 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6530 cached_state
, GFP_NOFS
);
6533 btrfs_start_ordered_extent(inode
, ordered
, 1);
6534 btrfs_put_ordered_extent(ordered
);
6536 /* Screw you mmap */
6537 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
6544 * If we found a page that couldn't be invalidated just
6545 * fall back to buffered.
6547 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6548 lockstart
>> PAGE_CACHE_SHIFT
,
6549 lockend
>> PAGE_CACHE_SHIFT
);
6560 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
6561 u64 len
, u64 orig_start
,
6562 u64 block_start
, u64 block_len
,
6563 u64 orig_block_len
, u64 ram_bytes
,
6566 struct extent_map_tree
*em_tree
;
6567 struct extent_map
*em
;
6568 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6571 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6572 em
= alloc_extent_map();
6574 return ERR_PTR(-ENOMEM
);
6577 em
->orig_start
= orig_start
;
6578 em
->mod_start
= start
;
6581 em
->block_len
= block_len
;
6582 em
->block_start
= block_start
;
6583 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6584 em
->orig_block_len
= orig_block_len
;
6585 em
->ram_bytes
= ram_bytes
;
6586 em
->generation
= -1;
6587 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6588 if (type
== BTRFS_ORDERED_PREALLOC
)
6589 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6592 btrfs_drop_extent_cache(inode
, em
->start
,
6593 em
->start
+ em
->len
- 1, 0);
6594 write_lock(&em_tree
->lock
);
6595 ret
= add_extent_mapping(em_tree
, em
, 1);
6596 write_unlock(&em_tree
->lock
);
6597 } while (ret
== -EEXIST
);
6600 free_extent_map(em
);
6601 return ERR_PTR(ret
);
6608 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6609 struct buffer_head
*bh_result
, int create
)
6611 struct extent_map
*em
;
6612 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6613 struct extent_state
*cached_state
= NULL
;
6614 u64 start
= iblock
<< inode
->i_blkbits
;
6615 u64 lockstart
, lockend
;
6616 u64 len
= bh_result
->b_size
;
6617 int unlock_bits
= EXTENT_LOCKED
;
6621 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6623 len
= min_t(u64
, len
, root
->sectorsize
);
6626 lockend
= start
+ len
- 1;
6629 * If this errors out it's because we couldn't invalidate pagecache for
6630 * this range and we need to fallback to buffered.
6632 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6635 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6642 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6643 * io. INLINE is special, and we could probably kludge it in here, but
6644 * it's still buffered so for safety lets just fall back to the generic
6647 * For COMPRESSED we _have_ to read the entire extent in so we can
6648 * decompress it, so there will be buffering required no matter what we
6649 * do, so go ahead and fallback to buffered.
6651 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6652 * to buffered IO. Don't blame me, this is the price we pay for using
6655 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6656 em
->block_start
== EXTENT_MAP_INLINE
) {
6657 free_extent_map(em
);
6662 /* Just a good old fashioned hole, return */
6663 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6664 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6665 free_extent_map(em
);
6670 * We don't allocate a new extent in the following cases
6672 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6674 * 2) The extent is marked as PREALLOC. We're good to go here and can
6675 * just use the extent.
6679 len
= min(len
, em
->len
- (start
- em
->start
));
6680 lockstart
= start
+ len
;
6684 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6685 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6686 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6689 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
6691 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6692 type
= BTRFS_ORDERED_PREALLOC
;
6694 type
= BTRFS_ORDERED_NOCOW
;
6695 len
= min(len
, em
->len
- (start
- em
->start
));
6696 block_start
= em
->block_start
+ (start
- em
->start
);
6698 if (can_nocow_extent(inode
, start
, &len
, &orig_start
,
6699 &orig_block_len
, &ram_bytes
) == 1) {
6700 if (type
== BTRFS_ORDERED_PREALLOC
) {
6701 free_extent_map(em
);
6702 em
= create_pinned_em(inode
, start
, len
,
6711 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6712 block_start
, len
, len
, type
);
6714 free_extent_map(em
);
6722 * this will cow the extent, reset the len in case we changed
6725 len
= bh_result
->b_size
;
6726 free_extent_map(em
);
6727 em
= btrfs_new_extent_direct(inode
, start
, len
);
6732 len
= min(len
, em
->len
- (start
- em
->start
));
6734 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6736 bh_result
->b_size
= len
;
6737 bh_result
->b_bdev
= em
->bdev
;
6738 set_buffer_mapped(bh_result
);
6740 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6741 set_buffer_new(bh_result
);
6744 * Need to update the i_size under the extent lock so buffered
6745 * readers will get the updated i_size when we unlock.
6747 if (start
+ len
> i_size_read(inode
))
6748 i_size_write(inode
, start
+ len
);
6750 spin_lock(&BTRFS_I(inode
)->lock
);
6751 BTRFS_I(inode
)->outstanding_extents
++;
6752 spin_unlock(&BTRFS_I(inode
)->lock
);
6754 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6755 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
6756 &cached_state
, GFP_NOFS
);
6761 * In the case of write we need to clear and unlock the entire range,
6762 * in the case of read we need to unlock only the end area that we
6763 * aren't using if there is any left over space.
6765 if (lockstart
< lockend
) {
6766 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6767 lockend
, unlock_bits
, 1, 0,
6768 &cached_state
, GFP_NOFS
);
6770 free_extent_state(cached_state
);
6773 free_extent_map(em
);
6778 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6779 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6783 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6785 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6786 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6787 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6788 struct inode
*inode
= dip
->inode
;
6789 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6790 struct bio
*dio_bio
;
6791 u32
*csums
= (u32
*)dip
->csum
;
6795 start
= dip
->logical_offset
;
6797 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6798 struct page
*page
= bvec
->bv_page
;
6801 unsigned long flags
;
6803 local_irq_save(flags
);
6804 kaddr
= kmap_atomic(page
);
6805 csum
= btrfs_csum_data(kaddr
+ bvec
->bv_offset
,
6806 csum
, bvec
->bv_len
);
6807 btrfs_csum_final(csum
, (char *)&csum
);
6808 kunmap_atomic(kaddr
);
6809 local_irq_restore(flags
);
6811 flush_dcache_page(bvec
->bv_page
);
6812 if (csum
!= csums
[index
]) {
6813 btrfs_err(root
->fs_info
, "csum failed ino %llu off %llu csum %u expected csum %u",
6814 btrfs_ino(inode
), start
, csum
,
6820 start
+= bvec
->bv_len
;
6823 } while (bvec
<= bvec_end
);
6825 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6826 dip
->logical_offset
+ dip
->bytes
- 1);
6827 dio_bio
= dip
->dio_bio
;
6831 /* If we had a csum failure make sure to clear the uptodate flag */
6833 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
6834 dio_end_io(dio_bio
, err
);
6838 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6840 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6841 struct inode
*inode
= dip
->inode
;
6842 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6843 struct btrfs_ordered_extent
*ordered
= NULL
;
6844 u64 ordered_offset
= dip
->logical_offset
;
6845 u64 ordered_bytes
= dip
->bytes
;
6846 struct bio
*dio_bio
;
6852 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
6854 ordered_bytes
, !err
);
6858 ordered
->work
.func
= finish_ordered_fn
;
6859 ordered
->work
.flags
= 0;
6860 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
6864 * our bio might span multiple ordered extents. If we haven't
6865 * completed the accounting for the whole dio, go back and try again
6867 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
6868 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
6874 dio_bio
= dip
->dio_bio
;
6878 /* If we had an error make sure to clear the uptodate flag */
6880 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
6881 dio_end_io(dio_bio
, err
);
6885 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
6886 struct bio
*bio
, int mirror_num
,
6887 unsigned long bio_flags
, u64 offset
)
6890 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6891 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
6892 BUG_ON(ret
); /* -ENOMEM */
6896 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
6898 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6901 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
6902 "sector %#Lx len %u err no %d\n",
6903 btrfs_ino(dip
->inode
), bio
->bi_rw
,
6904 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
6908 * before atomic variable goto zero, we must make sure
6909 * dip->errors is perceived to be set.
6911 smp_mb__before_atomic_dec();
6914 /* if there are more bios still pending for this dio, just exit */
6915 if (!atomic_dec_and_test(&dip
->pending_bios
))
6919 bio_io_error(dip
->orig_bio
);
6921 set_bit(BIO_UPTODATE
, &dip
->dio_bio
->bi_flags
);
6922 bio_endio(dip
->orig_bio
, 0);
6928 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
6929 u64 first_sector
, gfp_t gfp_flags
)
6931 int nr_vecs
= bio_get_nr_vecs(bdev
);
6932 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
6935 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
6936 int rw
, u64 file_offset
, int skip_sum
,
6939 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6940 int write
= rw
& REQ_WRITE
;
6941 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6945 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
6950 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
6958 if (write
&& async_submit
) {
6959 ret
= btrfs_wq_submit_bio(root
->fs_info
,
6960 inode
, rw
, bio
, 0, 0,
6962 __btrfs_submit_bio_start_direct_io
,
6963 __btrfs_submit_bio_done
);
6967 * If we aren't doing async submit, calculate the csum of the
6970 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
6973 } else if (!skip_sum
) {
6974 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, dip
, bio
,
6981 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
6987 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
6990 struct inode
*inode
= dip
->inode
;
6991 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6993 struct bio
*orig_bio
= dip
->orig_bio
;
6994 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
6995 u64 start_sector
= orig_bio
->bi_sector
;
6996 u64 file_offset
= dip
->logical_offset
;
7001 int async_submit
= 0;
7003 map_length
= orig_bio
->bi_size
;
7004 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
7005 &map_length
, NULL
, 0);
7011 if (map_length
>= orig_bio
->bi_size
) {
7016 /* async crcs make it difficult to collect full stripe writes. */
7017 if (btrfs_get_alloc_profile(root
, 1) &
7018 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
7023 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7026 bio
->bi_private
= dip
;
7027 bio
->bi_end_io
= btrfs_end_dio_bio
;
7028 atomic_inc(&dip
->pending_bios
);
7030 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7031 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
7032 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7033 bvec
->bv_offset
) < bvec
->bv_len
)) {
7035 * inc the count before we submit the bio so
7036 * we know the end IO handler won't happen before
7037 * we inc the count. Otherwise, the dip might get freed
7038 * before we're done setting it up
7040 atomic_inc(&dip
->pending_bios
);
7041 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7042 file_offset
, skip_sum
,
7046 atomic_dec(&dip
->pending_bios
);
7050 start_sector
+= submit_len
>> 9;
7051 file_offset
+= submit_len
;
7056 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7057 start_sector
, GFP_NOFS
);
7060 bio
->bi_private
= dip
;
7061 bio
->bi_end_io
= btrfs_end_dio_bio
;
7063 map_length
= orig_bio
->bi_size
;
7064 ret
= btrfs_map_block(root
->fs_info
, rw
,
7066 &map_length
, NULL
, 0);
7072 submit_len
+= bvec
->bv_len
;
7079 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7088 * before atomic variable goto zero, we must
7089 * make sure dip->errors is perceived to be set.
7091 smp_mb__before_atomic_dec();
7092 if (atomic_dec_and_test(&dip
->pending_bios
))
7093 bio_io_error(dip
->orig_bio
);
7095 /* bio_end_io() will handle error, so we needn't return it */
7099 static void btrfs_submit_direct(int rw
, struct bio
*dio_bio
,
7100 struct inode
*inode
, loff_t file_offset
)
7102 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7103 struct btrfs_dio_private
*dip
;
7107 int write
= rw
& REQ_WRITE
;
7111 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7113 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
7119 if (!skip_sum
&& !write
) {
7120 csum_size
= btrfs_super_csum_size(root
->fs_info
->super_copy
);
7121 sum_len
= dio_bio
->bi_size
>> inode
->i_sb
->s_blocksize_bits
;
7122 sum_len
*= csum_size
;
7127 dip
= kmalloc(sizeof(*dip
) + sum_len
, GFP_NOFS
);
7133 dip
->private = dio_bio
->bi_private
;
7135 dip
->logical_offset
= file_offset
;
7136 dip
->bytes
= dio_bio
->bi_size
;
7137 dip
->disk_bytenr
= (u64
)dio_bio
->bi_sector
<< 9;
7138 io_bio
->bi_private
= dip
;
7140 dip
->orig_bio
= io_bio
;
7141 dip
->dio_bio
= dio_bio
;
7142 atomic_set(&dip
->pending_bios
, 0);
7145 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
7147 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
7149 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
7158 * If this is a write, we need to clean up the reserved space and kill
7159 * the ordered extent.
7162 struct btrfs_ordered_extent
*ordered
;
7163 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
7164 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
7165 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
7166 btrfs_free_reserved_extent(root
, ordered
->start
,
7168 btrfs_put_ordered_extent(ordered
);
7169 btrfs_put_ordered_extent(ordered
);
7171 bio_endio(dio_bio
, ret
);
7174 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
7175 const struct iovec
*iov
, loff_t offset
,
7176 unsigned long nr_segs
)
7182 unsigned blocksize_mask
= root
->sectorsize
- 1;
7183 ssize_t retval
= -EINVAL
;
7184 loff_t end
= offset
;
7186 if (offset
& blocksize_mask
)
7189 /* Check the memory alignment. Blocks cannot straddle pages */
7190 for (seg
= 0; seg
< nr_segs
; seg
++) {
7191 addr
= (unsigned long)iov
[seg
].iov_base
;
7192 size
= iov
[seg
].iov_len
;
7194 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
7197 /* If this is a write we don't need to check anymore */
7202 * Check to make sure we don't have duplicate iov_base's in this
7203 * iovec, if so return EINVAL, otherwise we'll get csum errors
7204 * when reading back.
7206 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
7207 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
7216 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
7217 const struct iovec
*iov
, loff_t offset
,
7218 unsigned long nr_segs
)
7220 struct file
*file
= iocb
->ki_filp
;
7221 struct inode
*inode
= file
->f_mapping
->host
;
7225 bool relock
= false;
7228 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
7232 atomic_inc(&inode
->i_dio_count
);
7233 smp_mb__after_atomic_inc();
7236 * The generic stuff only does filemap_write_and_wait_range, which isn't
7237 * enough if we've written compressed pages to this area, so we need to
7238 * call btrfs_wait_ordered_range to make absolutely sure that any
7239 * outstanding dirty pages are on disk.
7241 count
= iov_length(iov
, nr_segs
);
7242 ret
= btrfs_wait_ordered_range(inode
, offset
, count
);
7248 * If the write DIO is beyond the EOF, we need update
7249 * the isize, but it is protected by i_mutex. So we can
7250 * not unlock the i_mutex at this case.
7252 if (offset
+ count
<= inode
->i_size
) {
7253 mutex_unlock(&inode
->i_mutex
);
7256 ret
= btrfs_delalloc_reserve_space(inode
, count
);
7259 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7260 &BTRFS_I(inode
)->runtime_flags
))) {
7261 inode_dio_done(inode
);
7262 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7266 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
7267 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
7268 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
7269 btrfs_submit_direct
, flags
);
7271 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
7272 btrfs_delalloc_release_space(inode
, count
);
7273 else if (ret
>= 0 && (size_t)ret
< count
)
7274 btrfs_delalloc_release_space(inode
,
7275 count
- (size_t)ret
);
7277 btrfs_delalloc_release_metadata(inode
, 0);
7281 inode_dio_done(inode
);
7283 mutex_lock(&inode
->i_mutex
);
7288 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7290 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
7291 __u64 start
, __u64 len
)
7295 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
7299 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
7302 int btrfs_readpage(struct file
*file
, struct page
*page
)
7304 struct extent_io_tree
*tree
;
7305 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7306 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
7309 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
7311 struct extent_io_tree
*tree
;
7314 if (current
->flags
& PF_MEMALLOC
) {
7315 redirty_page_for_writepage(wbc
, page
);
7319 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7320 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
7323 static int btrfs_writepages(struct address_space
*mapping
,
7324 struct writeback_control
*wbc
)
7326 struct extent_io_tree
*tree
;
7328 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7329 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
7333 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
7334 struct list_head
*pages
, unsigned nr_pages
)
7336 struct extent_io_tree
*tree
;
7337 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7338 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
7341 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7343 struct extent_io_tree
*tree
;
7344 struct extent_map_tree
*map
;
7347 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7348 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
7349 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
7351 ClearPagePrivate(page
);
7352 set_page_private(page
, 0);
7353 page_cache_release(page
);
7358 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7360 if (PageWriteback(page
) || PageDirty(page
))
7362 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
7365 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
7366 unsigned int length
)
7368 struct inode
*inode
= page
->mapping
->host
;
7369 struct extent_io_tree
*tree
;
7370 struct btrfs_ordered_extent
*ordered
;
7371 struct extent_state
*cached_state
= NULL
;
7372 u64 page_start
= page_offset(page
);
7373 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7376 * we have the page locked, so new writeback can't start,
7377 * and the dirty bit won't be cleared while we are here.
7379 * Wait for IO on this page so that we can safely clear
7380 * the PagePrivate2 bit and do ordered accounting
7382 wait_on_page_writeback(page
);
7384 tree
= &BTRFS_I(inode
)->io_tree
;
7386 btrfs_releasepage(page
, GFP_NOFS
);
7389 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7390 ordered
= btrfs_lookup_ordered_extent(inode
, page_offset(page
));
7393 * IO on this page will never be started, so we need
7394 * to account for any ordered extents now
7396 clear_extent_bit(tree
, page_start
, page_end
,
7397 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7398 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
7399 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
7401 * whoever cleared the private bit is responsible
7402 * for the finish_ordered_io
7404 if (TestClearPagePrivate2(page
)) {
7405 struct btrfs_ordered_inode_tree
*tree
;
7408 tree
= &BTRFS_I(inode
)->ordered_tree
;
7410 spin_lock_irq(&tree
->lock
);
7411 set_bit(BTRFS_ORDERED_TRUNCATED
, &ordered
->flags
);
7412 new_len
= page_start
- ordered
->file_offset
;
7413 if (new_len
< ordered
->truncated_len
)
7414 ordered
->truncated_len
= new_len
;
7415 spin_unlock_irq(&tree
->lock
);
7417 if (btrfs_dec_test_ordered_pending(inode
, &ordered
,
7419 PAGE_CACHE_SIZE
, 1))
7420 btrfs_finish_ordered_io(ordered
);
7422 btrfs_put_ordered_extent(ordered
);
7423 cached_state
= NULL
;
7424 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7426 clear_extent_bit(tree
, page_start
, page_end
,
7427 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
7428 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
7429 &cached_state
, GFP_NOFS
);
7430 __btrfs_releasepage(page
, GFP_NOFS
);
7432 ClearPageChecked(page
);
7433 if (PagePrivate(page
)) {
7434 ClearPagePrivate(page
);
7435 set_page_private(page
, 0);
7436 page_cache_release(page
);
7441 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7442 * called from a page fault handler when a page is first dirtied. Hence we must
7443 * be careful to check for EOF conditions here. We set the page up correctly
7444 * for a written page which means we get ENOSPC checking when writing into
7445 * holes and correct delalloc and unwritten extent mapping on filesystems that
7446 * support these features.
7448 * We are not allowed to take the i_mutex here so we have to play games to
7449 * protect against truncate races as the page could now be beyond EOF. Because
7450 * vmtruncate() writes the inode size before removing pages, once we have the
7451 * page lock we can determine safely if the page is beyond EOF. If it is not
7452 * beyond EOF, then the page is guaranteed safe against truncation until we
7455 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
7457 struct page
*page
= vmf
->page
;
7458 struct inode
*inode
= file_inode(vma
->vm_file
);
7459 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7460 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7461 struct btrfs_ordered_extent
*ordered
;
7462 struct extent_state
*cached_state
= NULL
;
7464 unsigned long zero_start
;
7471 sb_start_pagefault(inode
->i_sb
);
7472 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
7474 ret
= file_update_time(vma
->vm_file
);
7480 else /* -ENOSPC, -EIO, etc */
7481 ret
= VM_FAULT_SIGBUS
;
7487 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
7490 size
= i_size_read(inode
);
7491 page_start
= page_offset(page
);
7492 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7494 if ((page
->mapping
!= inode
->i_mapping
) ||
7495 (page_start
>= size
)) {
7496 /* page got truncated out from underneath us */
7499 wait_on_page_writeback(page
);
7501 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
7502 set_page_extent_mapped(page
);
7505 * we can't set the delalloc bits if there are pending ordered
7506 * extents. Drop our locks and wait for them to finish
7508 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7510 unlock_extent_cached(io_tree
, page_start
, page_end
,
7511 &cached_state
, GFP_NOFS
);
7513 btrfs_start_ordered_extent(inode
, ordered
, 1);
7514 btrfs_put_ordered_extent(ordered
);
7519 * XXX - page_mkwrite gets called every time the page is dirtied, even
7520 * if it was already dirty, so for space accounting reasons we need to
7521 * clear any delalloc bits for the range we are fixing to save. There
7522 * is probably a better way to do this, but for now keep consistent with
7523 * prepare_pages in the normal write path.
7525 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
7526 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7527 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
7528 0, 0, &cached_state
, GFP_NOFS
);
7530 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
7533 unlock_extent_cached(io_tree
, page_start
, page_end
,
7534 &cached_state
, GFP_NOFS
);
7535 ret
= VM_FAULT_SIGBUS
;
7540 /* page is wholly or partially inside EOF */
7541 if (page_start
+ PAGE_CACHE_SIZE
> size
)
7542 zero_start
= size
& ~PAGE_CACHE_MASK
;
7544 zero_start
= PAGE_CACHE_SIZE
;
7546 if (zero_start
!= PAGE_CACHE_SIZE
) {
7548 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
7549 flush_dcache_page(page
);
7552 ClearPageChecked(page
);
7553 set_page_dirty(page
);
7554 SetPageUptodate(page
);
7556 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
7557 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
7558 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
7560 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
7564 sb_end_pagefault(inode
->i_sb
);
7565 return VM_FAULT_LOCKED
;
7569 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
7571 sb_end_pagefault(inode
->i_sb
);
7575 static int btrfs_truncate(struct inode
*inode
)
7577 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7578 struct btrfs_block_rsv
*rsv
;
7581 struct btrfs_trans_handle
*trans
;
7582 u64 mask
= root
->sectorsize
- 1;
7583 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7585 ret
= btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
),
7591 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7592 * 3 things going on here
7594 * 1) We need to reserve space for our orphan item and the space to
7595 * delete our orphan item. Lord knows we don't want to have a dangling
7596 * orphan item because we didn't reserve space to remove it.
7598 * 2) We need to reserve space to update our inode.
7600 * 3) We need to have something to cache all the space that is going to
7601 * be free'd up by the truncate operation, but also have some slack
7602 * space reserved in case it uses space during the truncate (thank you
7603 * very much snapshotting).
7605 * And we need these to all be seperate. The fact is we can use alot of
7606 * space doing the truncate, and we have no earthly idea how much space
7607 * we will use, so we need the truncate reservation to be seperate so it
7608 * doesn't end up using space reserved for updating the inode or
7609 * removing the orphan item. We also need to be able to stop the
7610 * transaction and start a new one, which means we need to be able to
7611 * update the inode several times, and we have no idea of knowing how
7612 * many times that will be, so we can't just reserve 1 item for the
7613 * entirety of the opration, so that has to be done seperately as well.
7614 * Then there is the orphan item, which does indeed need to be held on
7615 * to for the whole operation, and we need nobody to touch this reserved
7616 * space except the orphan code.
7618 * So that leaves us with
7620 * 1) root->orphan_block_rsv - for the orphan deletion.
7621 * 2) rsv - for the truncate reservation, which we will steal from the
7622 * transaction reservation.
7623 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7624 * updating the inode.
7626 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7629 rsv
->size
= min_size
;
7633 * 1 for the truncate slack space
7634 * 1 for updating the inode.
7636 trans
= btrfs_start_transaction(root
, 2);
7637 if (IS_ERR(trans
)) {
7638 err
= PTR_ERR(trans
);
7642 /* Migrate the slack space for the truncate to our reserve */
7643 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7648 * setattr is responsible for setting the ordered_data_close flag,
7649 * but that is only tested during the last file release. That
7650 * could happen well after the next commit, leaving a great big
7651 * window where new writes may get lost if someone chooses to write
7652 * to this file after truncating to zero
7654 * The inode doesn't have any dirty data here, and so if we commit
7655 * this is a noop. If someone immediately starts writing to the inode
7656 * it is very likely we'll catch some of their writes in this
7657 * transaction, and the commit will find this file on the ordered
7658 * data list with good things to send down.
7660 * This is a best effort solution, there is still a window where
7661 * using truncate to replace the contents of the file will
7662 * end up with a zero length file after a crash.
7664 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7665 &BTRFS_I(inode
)->runtime_flags
))
7666 btrfs_add_ordered_operation(trans
, root
, inode
);
7669 * So if we truncate and then write and fsync we normally would just
7670 * write the extents that changed, which is a problem if we need to
7671 * first truncate that entire inode. So set this flag so we write out
7672 * all of the extents in the inode to the sync log so we're completely
7675 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7676 trans
->block_rsv
= rsv
;
7679 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7681 BTRFS_EXTENT_DATA_KEY
);
7682 if (ret
!= -ENOSPC
) {
7687 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7688 ret
= btrfs_update_inode(trans
, root
, inode
);
7694 btrfs_end_transaction(trans
, root
);
7695 btrfs_btree_balance_dirty(root
);
7697 trans
= btrfs_start_transaction(root
, 2);
7698 if (IS_ERR(trans
)) {
7699 ret
= err
= PTR_ERR(trans
);
7704 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7706 BUG_ON(ret
); /* shouldn't happen */
7707 trans
->block_rsv
= rsv
;
7710 if (ret
== 0 && inode
->i_nlink
> 0) {
7711 trans
->block_rsv
= root
->orphan_block_rsv
;
7712 ret
= btrfs_orphan_del(trans
, inode
);
7718 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7719 ret
= btrfs_update_inode(trans
, root
, inode
);
7723 ret
= btrfs_end_transaction(trans
, root
);
7724 btrfs_btree_balance_dirty(root
);
7728 btrfs_free_block_rsv(root
, rsv
);
7737 * create a new subvolume directory/inode (helper for the ioctl).
7739 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7740 struct btrfs_root
*new_root
, u64 new_dirid
)
7742 struct inode
*inode
;
7746 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7747 new_dirid
, new_dirid
,
7748 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7751 return PTR_ERR(inode
);
7752 inode
->i_op
= &btrfs_dir_inode_operations
;
7753 inode
->i_fop
= &btrfs_dir_file_operations
;
7755 set_nlink(inode
, 1);
7756 btrfs_i_size_write(inode
, 0);
7758 err
= btrfs_update_inode(trans
, new_root
, inode
);
7764 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7766 struct btrfs_inode
*ei
;
7767 struct inode
*inode
;
7769 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7776 ei
->last_sub_trans
= 0;
7777 ei
->logged_trans
= 0;
7778 ei
->delalloc_bytes
= 0;
7779 ei
->disk_i_size
= 0;
7782 ei
->index_cnt
= (u64
)-1;
7783 ei
->last_unlink_trans
= 0;
7784 ei
->last_log_commit
= 0;
7786 spin_lock_init(&ei
->lock
);
7787 ei
->outstanding_extents
= 0;
7788 ei
->reserved_extents
= 0;
7790 ei
->runtime_flags
= 0;
7791 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7793 ei
->delayed_node
= NULL
;
7795 inode
= &ei
->vfs_inode
;
7796 extent_map_tree_init(&ei
->extent_tree
);
7797 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7798 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7799 ei
->io_tree
.track_uptodate
= 1;
7800 ei
->io_failure_tree
.track_uptodate
= 1;
7801 atomic_set(&ei
->sync_writers
, 0);
7802 mutex_init(&ei
->log_mutex
);
7803 mutex_init(&ei
->delalloc_mutex
);
7804 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7805 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7806 INIT_LIST_HEAD(&ei
->ordered_operations
);
7807 RB_CLEAR_NODE(&ei
->rb_node
);
7812 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
7813 void btrfs_test_destroy_inode(struct inode
*inode
)
7815 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7816 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7820 static void btrfs_i_callback(struct rcu_head
*head
)
7822 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7823 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7826 void btrfs_destroy_inode(struct inode
*inode
)
7828 struct btrfs_ordered_extent
*ordered
;
7829 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7831 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7832 WARN_ON(inode
->i_data
.nrpages
);
7833 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7834 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7835 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7836 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7839 * This can happen where we create an inode, but somebody else also
7840 * created the same inode and we need to destroy the one we already
7847 * Make sure we're properly removed from the ordered operation
7851 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7852 spin_lock(&root
->fs_info
->ordered_root_lock
);
7853 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7854 spin_unlock(&root
->fs_info
->ordered_root_lock
);
7857 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7858 &BTRFS_I(inode
)->runtime_flags
)) {
7859 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
7861 atomic_dec(&root
->orphan_inodes
);
7865 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7869 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
7870 ordered
->file_offset
, ordered
->len
);
7871 btrfs_remove_ordered_extent(inode
, ordered
);
7872 btrfs_put_ordered_extent(ordered
);
7873 btrfs_put_ordered_extent(ordered
);
7876 inode_tree_del(inode
);
7877 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7879 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7882 int btrfs_drop_inode(struct inode
*inode
)
7884 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7889 /* the snap/subvol tree is on deleting */
7890 if (btrfs_root_refs(&root
->root_item
) == 0)
7893 return generic_drop_inode(inode
);
7896 static void init_once(void *foo
)
7898 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7900 inode_init_once(&ei
->vfs_inode
);
7903 void btrfs_destroy_cachep(void)
7906 * Make sure all delayed rcu free inodes are flushed before we
7910 if (btrfs_inode_cachep
)
7911 kmem_cache_destroy(btrfs_inode_cachep
);
7912 if (btrfs_trans_handle_cachep
)
7913 kmem_cache_destroy(btrfs_trans_handle_cachep
);
7914 if (btrfs_transaction_cachep
)
7915 kmem_cache_destroy(btrfs_transaction_cachep
);
7916 if (btrfs_path_cachep
)
7917 kmem_cache_destroy(btrfs_path_cachep
);
7918 if (btrfs_free_space_cachep
)
7919 kmem_cache_destroy(btrfs_free_space_cachep
);
7920 if (btrfs_delalloc_work_cachep
)
7921 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
7924 int btrfs_init_cachep(void)
7926 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
7927 sizeof(struct btrfs_inode
), 0,
7928 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
7929 if (!btrfs_inode_cachep
)
7932 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
7933 sizeof(struct btrfs_trans_handle
), 0,
7934 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7935 if (!btrfs_trans_handle_cachep
)
7938 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
7939 sizeof(struct btrfs_transaction
), 0,
7940 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7941 if (!btrfs_transaction_cachep
)
7944 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
7945 sizeof(struct btrfs_path
), 0,
7946 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7947 if (!btrfs_path_cachep
)
7950 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
7951 sizeof(struct btrfs_free_space
), 0,
7952 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7953 if (!btrfs_free_space_cachep
)
7956 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
7957 sizeof(struct btrfs_delalloc_work
), 0,
7958 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
7960 if (!btrfs_delalloc_work_cachep
)
7965 btrfs_destroy_cachep();
7969 static int btrfs_getattr(struct vfsmount
*mnt
,
7970 struct dentry
*dentry
, struct kstat
*stat
)
7973 struct inode
*inode
= dentry
->d_inode
;
7974 u32 blocksize
= inode
->i_sb
->s_blocksize
;
7976 generic_fillattr(inode
, stat
);
7977 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
7978 stat
->blksize
= PAGE_CACHE_SIZE
;
7980 spin_lock(&BTRFS_I(inode
)->lock
);
7981 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
7982 spin_unlock(&BTRFS_I(inode
)->lock
);
7983 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
7984 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
7988 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
7989 struct inode
*new_dir
, struct dentry
*new_dentry
)
7991 struct btrfs_trans_handle
*trans
;
7992 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
7993 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
7994 struct inode
*new_inode
= new_dentry
->d_inode
;
7995 struct inode
*old_inode
= old_dentry
->d_inode
;
7996 struct timespec ctime
= CURRENT_TIME
;
8000 u64 old_ino
= btrfs_ino(old_inode
);
8002 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
8005 /* we only allow rename subvolume link between subvolumes */
8006 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8009 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
8010 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
8013 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
8014 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
8018 /* check for collisions, even if the name isn't there */
8019 ret
= btrfs_check_dir_item_collision(dest
, new_dir
->i_ino
,
8020 new_dentry
->d_name
.name
,
8021 new_dentry
->d_name
.len
);
8024 if (ret
== -EEXIST
) {
8026 * eexist without a new_inode */
8032 /* maybe -EOVERFLOW */
8039 * we're using rename to replace one file with another.
8040 * and the replacement file is large. Start IO on it now so
8041 * we don't add too much work to the end of the transaction
8043 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
8044 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
8045 filemap_flush(old_inode
->i_mapping
);
8047 /* close the racy window with snapshot create/destroy ioctl */
8048 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8049 down_read(&root
->fs_info
->subvol_sem
);
8051 * We want to reserve the absolute worst case amount of items. So if
8052 * both inodes are subvols and we need to unlink them then that would
8053 * require 4 item modifications, but if they are both normal inodes it
8054 * would require 5 item modifications, so we'll assume their normal
8055 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8056 * should cover the worst case number of items we'll modify.
8058 trans
= btrfs_start_transaction(root
, 11);
8059 if (IS_ERR(trans
)) {
8060 ret
= PTR_ERR(trans
);
8065 btrfs_record_root_in_trans(trans
, dest
);
8067 ret
= btrfs_set_inode_index(new_dir
, &index
);
8071 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8072 /* force full log commit if subvolume involved. */
8073 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8075 ret
= btrfs_insert_inode_ref(trans
, dest
,
8076 new_dentry
->d_name
.name
,
8077 new_dentry
->d_name
.len
,
8079 btrfs_ino(new_dir
), index
);
8083 * this is an ugly little race, but the rename is required
8084 * to make sure that if we crash, the inode is either at the
8085 * old name or the new one. pinning the log transaction lets
8086 * us make sure we don't allow a log commit to come in after
8087 * we unlink the name but before we add the new name back in.
8089 btrfs_pin_log_trans(root
);
8092 * make sure the inode gets flushed if it is replacing
8095 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
8096 btrfs_add_ordered_operation(trans
, root
, old_inode
);
8098 inode_inc_iversion(old_dir
);
8099 inode_inc_iversion(new_dir
);
8100 inode_inc_iversion(old_inode
);
8101 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8102 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8103 old_inode
->i_ctime
= ctime
;
8105 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8106 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8108 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8109 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8110 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8111 old_dentry
->d_name
.name
,
8112 old_dentry
->d_name
.len
);
8114 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8115 old_dentry
->d_inode
,
8116 old_dentry
->d_name
.name
,
8117 old_dentry
->d_name
.len
);
8119 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8122 btrfs_abort_transaction(trans
, root
, ret
);
8127 inode_inc_iversion(new_inode
);
8128 new_inode
->i_ctime
= CURRENT_TIME
;
8129 if (unlikely(btrfs_ino(new_inode
) ==
8130 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8131 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8132 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
8134 new_dentry
->d_name
.name
,
8135 new_dentry
->d_name
.len
);
8136 BUG_ON(new_inode
->i_nlink
== 0);
8138 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
8139 new_dentry
->d_inode
,
8140 new_dentry
->d_name
.name
,
8141 new_dentry
->d_name
.len
);
8143 if (!ret
&& new_inode
->i_nlink
== 0)
8144 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
8146 btrfs_abort_transaction(trans
, root
, ret
);
8151 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
8152 new_dentry
->d_name
.name
,
8153 new_dentry
->d_name
.len
, 0, index
);
8155 btrfs_abort_transaction(trans
, root
, ret
);
8159 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
8160 struct dentry
*parent
= new_dentry
->d_parent
;
8161 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
8162 btrfs_end_log_trans(root
);
8165 btrfs_end_transaction(trans
, root
);
8167 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8168 up_read(&root
->fs_info
->subvol_sem
);
8173 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
8175 struct btrfs_delalloc_work
*delalloc_work
;
8177 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
8179 if (delalloc_work
->wait
)
8180 btrfs_wait_ordered_range(delalloc_work
->inode
, 0, (u64
)-1);
8182 filemap_flush(delalloc_work
->inode
->i_mapping
);
8184 if (delalloc_work
->delay_iput
)
8185 btrfs_add_delayed_iput(delalloc_work
->inode
);
8187 iput(delalloc_work
->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_all_delalloc_inodes(struct btrfs_fs_info
*fs_info
,
8311 struct btrfs_root
*root
;
8312 struct list_head splice
;
8315 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
8318 INIT_LIST_HEAD(&splice
);
8320 spin_lock(&fs_info
->delalloc_root_lock
);
8321 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
8322 while (!list_empty(&splice
)) {
8323 root
= list_first_entry(&splice
, struct btrfs_root
,
8325 root
= btrfs_grab_fs_root(root
);
8327 list_move_tail(&root
->delalloc_root
,
8328 &fs_info
->delalloc_roots
);
8329 spin_unlock(&fs_info
->delalloc_root_lock
);
8331 ret
= __start_delalloc_inodes(root
, delay_iput
);
8332 btrfs_put_fs_root(root
);
8336 spin_lock(&fs_info
->delalloc_root_lock
);
8338 spin_unlock(&fs_info
->delalloc_root_lock
);
8340 atomic_inc(&fs_info
->async_submit_draining
);
8341 while (atomic_read(&fs_info
->nr_async_submits
) ||
8342 atomic_read(&fs_info
->async_delalloc_pages
)) {
8343 wait_event(fs_info
->async_submit_wait
,
8344 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
8345 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
8347 atomic_dec(&fs_info
->async_submit_draining
);
8350 if (!list_empty_careful(&splice
)) {
8351 spin_lock(&fs_info
->delalloc_root_lock
);
8352 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
8353 spin_unlock(&fs_info
->delalloc_root_lock
);
8358 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
8359 const char *symname
)
8361 struct btrfs_trans_handle
*trans
;
8362 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8363 struct btrfs_path
*path
;
8364 struct btrfs_key key
;
8365 struct inode
*inode
= NULL
;
8373 struct btrfs_file_extent_item
*ei
;
8374 struct extent_buffer
*leaf
;
8376 name_len
= strlen(symname
);
8377 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
8378 return -ENAMETOOLONG
;
8381 * 2 items for inode item and ref
8382 * 2 items for dir items
8383 * 1 item for xattr if selinux is on
8385 trans
= btrfs_start_transaction(root
, 5);
8387 return PTR_ERR(trans
);
8389 err
= btrfs_find_free_ino(root
, &objectid
);
8393 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
8394 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
8395 S_IFLNK
|S_IRWXUGO
, &index
);
8396 if (IS_ERR(inode
)) {
8397 err
= PTR_ERR(inode
);
8401 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
8408 * If the active LSM wants to access the inode during
8409 * d_instantiate it needs these. Smack checks to see
8410 * if the filesystem supports xattrs by looking at the
8413 inode
->i_fop
= &btrfs_file_operations
;
8414 inode
->i_op
= &btrfs_file_inode_operations
;
8416 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
8420 inode
->i_mapping
->a_ops
= &btrfs_aops
;
8421 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8422 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
8427 path
= btrfs_alloc_path();
8433 key
.objectid
= btrfs_ino(inode
);
8435 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
8436 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
8437 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
8441 btrfs_free_path(path
);
8444 leaf
= path
->nodes
[0];
8445 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
8446 struct btrfs_file_extent_item
);
8447 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
8448 btrfs_set_file_extent_type(leaf
, ei
,
8449 BTRFS_FILE_EXTENT_INLINE
);
8450 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
8451 btrfs_set_file_extent_compression(leaf
, ei
, 0);
8452 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
8453 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
8455 ptr
= btrfs_file_extent_inline_start(ei
);
8456 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
8457 btrfs_mark_buffer_dirty(leaf
);
8458 btrfs_free_path(path
);
8460 inode
->i_op
= &btrfs_symlink_inode_operations
;
8461 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
8462 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8463 inode_set_bytes(inode
, name_len
);
8464 btrfs_i_size_write(inode
, name_len
);
8465 err
= btrfs_update_inode(trans
, root
, inode
);
8471 d_instantiate(dentry
, inode
);
8472 btrfs_end_transaction(trans
, root
);
8474 inode_dec_link_count(inode
);
8477 btrfs_btree_balance_dirty(root
);
8481 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8482 u64 start
, u64 num_bytes
, u64 min_size
,
8483 loff_t actual_len
, u64
*alloc_hint
,
8484 struct btrfs_trans_handle
*trans
)
8486 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
8487 struct extent_map
*em
;
8488 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8489 struct btrfs_key ins
;
8490 u64 cur_offset
= start
;
8494 bool own_trans
= true;
8498 while (num_bytes
> 0) {
8500 trans
= btrfs_start_transaction(root
, 3);
8501 if (IS_ERR(trans
)) {
8502 ret
= PTR_ERR(trans
);
8507 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
8508 cur_bytes
= max(cur_bytes
, min_size
);
8509 ret
= btrfs_reserve_extent(root
, cur_bytes
, min_size
, 0,
8510 *alloc_hint
, &ins
, 1);
8513 btrfs_end_transaction(trans
, root
);
8517 ret
= insert_reserved_file_extent(trans
, inode
,
8518 cur_offset
, ins
.objectid
,
8519 ins
.offset
, ins
.offset
,
8520 ins
.offset
, 0, 0, 0,
8521 BTRFS_FILE_EXTENT_PREALLOC
);
8523 btrfs_free_reserved_extent(root
, ins
.objectid
,
8525 btrfs_abort_transaction(trans
, root
, ret
);
8527 btrfs_end_transaction(trans
, root
);
8530 btrfs_drop_extent_cache(inode
, cur_offset
,
8531 cur_offset
+ ins
.offset
-1, 0);
8533 em
= alloc_extent_map();
8535 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
8536 &BTRFS_I(inode
)->runtime_flags
);
8540 em
->start
= cur_offset
;
8541 em
->orig_start
= cur_offset
;
8542 em
->len
= ins
.offset
;
8543 em
->block_start
= ins
.objectid
;
8544 em
->block_len
= ins
.offset
;
8545 em
->orig_block_len
= ins
.offset
;
8546 em
->ram_bytes
= ins
.offset
;
8547 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
8548 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
8549 em
->generation
= trans
->transid
;
8552 write_lock(&em_tree
->lock
);
8553 ret
= add_extent_mapping(em_tree
, em
, 1);
8554 write_unlock(&em_tree
->lock
);
8557 btrfs_drop_extent_cache(inode
, cur_offset
,
8558 cur_offset
+ ins
.offset
- 1,
8561 free_extent_map(em
);
8563 num_bytes
-= ins
.offset
;
8564 cur_offset
+= ins
.offset
;
8565 *alloc_hint
= ins
.objectid
+ ins
.offset
;
8567 inode_inc_iversion(inode
);
8568 inode
->i_ctime
= CURRENT_TIME
;
8569 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
8570 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
8571 (actual_len
> inode
->i_size
) &&
8572 (cur_offset
> inode
->i_size
)) {
8573 if (cur_offset
> actual_len
)
8574 i_size
= actual_len
;
8576 i_size
= cur_offset
;
8577 i_size_write(inode
, i_size
);
8578 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
8581 ret
= btrfs_update_inode(trans
, root
, inode
);
8584 btrfs_abort_transaction(trans
, root
, ret
);
8586 btrfs_end_transaction(trans
, root
);
8591 btrfs_end_transaction(trans
, root
);
8596 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8597 u64 start
, u64 num_bytes
, u64 min_size
,
8598 loff_t actual_len
, u64
*alloc_hint
)
8600 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8601 min_size
, actual_len
, alloc_hint
,
8605 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
8606 struct btrfs_trans_handle
*trans
, int mode
,
8607 u64 start
, u64 num_bytes
, u64 min_size
,
8608 loff_t actual_len
, u64
*alloc_hint
)
8610 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8611 min_size
, actual_len
, alloc_hint
, trans
);
8614 static int btrfs_set_page_dirty(struct page
*page
)
8616 return __set_page_dirty_nobuffers(page
);
8619 static int btrfs_permission(struct inode
*inode
, int mask
)
8621 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8622 umode_t mode
= inode
->i_mode
;
8624 if (mask
& MAY_WRITE
&&
8625 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
8626 if (btrfs_root_readonly(root
))
8628 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
8631 return generic_permission(inode
, mask
);
8634 static const struct inode_operations btrfs_dir_inode_operations
= {
8635 .getattr
= btrfs_getattr
,
8636 .lookup
= btrfs_lookup
,
8637 .create
= btrfs_create
,
8638 .unlink
= btrfs_unlink
,
8640 .mkdir
= btrfs_mkdir
,
8641 .rmdir
= btrfs_rmdir
,
8642 .rename
= btrfs_rename
,
8643 .symlink
= btrfs_symlink
,
8644 .setattr
= btrfs_setattr
,
8645 .mknod
= btrfs_mknod
,
8646 .setxattr
= btrfs_setxattr
,
8647 .getxattr
= btrfs_getxattr
,
8648 .listxattr
= btrfs_listxattr
,
8649 .removexattr
= btrfs_removexattr
,
8650 .permission
= btrfs_permission
,
8651 .get_acl
= btrfs_get_acl
,
8652 .update_time
= btrfs_update_time
,
8654 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
8655 .lookup
= btrfs_lookup
,
8656 .permission
= btrfs_permission
,
8657 .get_acl
= btrfs_get_acl
,
8658 .update_time
= btrfs_update_time
,
8661 static const struct file_operations btrfs_dir_file_operations
= {
8662 .llseek
= generic_file_llseek
,
8663 .read
= generic_read_dir
,
8664 .iterate
= btrfs_real_readdir
,
8665 .unlocked_ioctl
= btrfs_ioctl
,
8666 #ifdef CONFIG_COMPAT
8667 .compat_ioctl
= btrfs_ioctl
,
8669 .release
= btrfs_release_file
,
8670 .fsync
= btrfs_sync_file
,
8673 static struct extent_io_ops btrfs_extent_io_ops
= {
8674 .fill_delalloc
= run_delalloc_range
,
8675 .submit_bio_hook
= btrfs_submit_bio_hook
,
8676 .merge_bio_hook
= btrfs_merge_bio_hook
,
8677 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
8678 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
8679 .writepage_start_hook
= btrfs_writepage_start_hook
,
8680 .set_bit_hook
= btrfs_set_bit_hook
,
8681 .clear_bit_hook
= btrfs_clear_bit_hook
,
8682 .merge_extent_hook
= btrfs_merge_extent_hook
,
8683 .split_extent_hook
= btrfs_split_extent_hook
,
8687 * btrfs doesn't support the bmap operation because swapfiles
8688 * use bmap to make a mapping of extents in the file. They assume
8689 * these extents won't change over the life of the file and they
8690 * use the bmap result to do IO directly to the drive.
8692 * the btrfs bmap call would return logical addresses that aren't
8693 * suitable for IO and they also will change frequently as COW
8694 * operations happen. So, swapfile + btrfs == corruption.
8696 * For now we're avoiding this by dropping bmap.
8698 static const struct address_space_operations btrfs_aops
= {
8699 .readpage
= btrfs_readpage
,
8700 .writepage
= btrfs_writepage
,
8701 .writepages
= btrfs_writepages
,
8702 .readpages
= btrfs_readpages
,
8703 .direct_IO
= btrfs_direct_IO
,
8704 .invalidatepage
= btrfs_invalidatepage
,
8705 .releasepage
= btrfs_releasepage
,
8706 .set_page_dirty
= btrfs_set_page_dirty
,
8707 .error_remove_page
= generic_error_remove_page
,
8710 static const struct address_space_operations btrfs_symlink_aops
= {
8711 .readpage
= btrfs_readpage
,
8712 .writepage
= btrfs_writepage
,
8713 .invalidatepage
= btrfs_invalidatepage
,
8714 .releasepage
= btrfs_releasepage
,
8717 static const struct inode_operations btrfs_file_inode_operations
= {
8718 .getattr
= btrfs_getattr
,
8719 .setattr
= btrfs_setattr
,
8720 .setxattr
= btrfs_setxattr
,
8721 .getxattr
= btrfs_getxattr
,
8722 .listxattr
= btrfs_listxattr
,
8723 .removexattr
= btrfs_removexattr
,
8724 .permission
= btrfs_permission
,
8725 .fiemap
= btrfs_fiemap
,
8726 .get_acl
= btrfs_get_acl
,
8727 .update_time
= btrfs_update_time
,
8729 static const struct inode_operations btrfs_special_inode_operations
= {
8730 .getattr
= btrfs_getattr
,
8731 .setattr
= btrfs_setattr
,
8732 .permission
= btrfs_permission
,
8733 .setxattr
= btrfs_setxattr
,
8734 .getxattr
= btrfs_getxattr
,
8735 .listxattr
= btrfs_listxattr
,
8736 .removexattr
= btrfs_removexattr
,
8737 .get_acl
= btrfs_get_acl
,
8738 .update_time
= btrfs_update_time
,
8740 static const struct inode_operations btrfs_symlink_inode_operations
= {
8741 .readlink
= generic_readlink
,
8742 .follow_link
= page_follow_link_light
,
8743 .put_link
= page_put_link
,
8744 .getattr
= btrfs_getattr
,
8745 .setattr
= btrfs_setattr
,
8746 .permission
= btrfs_permission
,
8747 .setxattr
= btrfs_setxattr
,
8748 .getxattr
= btrfs_getxattr
,
8749 .listxattr
= btrfs_listxattr
,
8750 .removexattr
= btrfs_removexattr
,
8751 .get_acl
= btrfs_get_acl
,
8752 .update_time
= btrfs_update_time
,
8755 const struct dentry_operations btrfs_dentry_operations
= {
8756 .d_delete
= btrfs_dentry_delete
,
8757 .d_release
= btrfs_dentry_release
,