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>
49 #include "transaction.h"
50 #include "btrfs_inode.h"
51 #include "print-tree.h"
52 #include "ordered-data.h"
56 #include "compression.h"
58 #include "free-space-cache.h"
59 #include "inode-map.h"
63 struct btrfs_iget_args
{
65 struct btrfs_root
*root
;
68 static const struct inode_operations btrfs_dir_inode_operations
;
69 static const struct inode_operations btrfs_symlink_inode_operations
;
70 static const struct inode_operations btrfs_dir_ro_inode_operations
;
71 static const struct inode_operations btrfs_special_inode_operations
;
72 static const struct inode_operations btrfs_file_inode_operations
;
73 static const struct address_space_operations btrfs_aops
;
74 static const struct address_space_operations btrfs_symlink_aops
;
75 static const struct file_operations btrfs_dir_file_operations
;
76 static struct extent_io_ops btrfs_extent_io_ops
;
78 static struct kmem_cache
*btrfs_inode_cachep
;
79 static struct kmem_cache
*btrfs_delalloc_work_cachep
;
80 struct kmem_cache
*btrfs_trans_handle_cachep
;
81 struct kmem_cache
*btrfs_transaction_cachep
;
82 struct kmem_cache
*btrfs_path_cachep
;
83 struct kmem_cache
*btrfs_free_space_cachep
;
86 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
87 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
88 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
89 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
90 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
91 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
92 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
93 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
96 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
97 static int btrfs_truncate(struct inode
*inode
);
98 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
99 static noinline
int cow_file_range(struct inode
*inode
,
100 struct page
*locked_page
,
101 u64 start
, u64 end
, int *page_started
,
102 unsigned long *nr_written
, int unlock
);
103 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
104 u64 len
, u64 orig_start
,
105 u64 block_start
, u64 block_len
,
106 u64 orig_block_len
, u64 ram_bytes
,
109 static int btrfs_dirty_inode(struct inode
*inode
);
111 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
112 struct inode
*inode
, struct inode
*dir
,
113 const struct qstr
*qstr
)
117 err
= btrfs_init_acl(trans
, inode
, dir
);
119 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
124 * this does all the hard work for inserting an inline extent into
125 * the btree. The caller should have done a btrfs_drop_extents so that
126 * no overlapping inline items exist in the btree
128 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
129 struct btrfs_root
*root
, struct inode
*inode
,
130 u64 start
, size_t size
, size_t compressed_size
,
132 struct page
**compressed_pages
)
134 struct btrfs_key key
;
135 struct btrfs_path
*path
;
136 struct extent_buffer
*leaf
;
137 struct page
*page
= NULL
;
140 struct btrfs_file_extent_item
*ei
;
143 size_t cur_size
= size
;
145 unsigned long offset
;
147 if (compressed_size
&& compressed_pages
)
148 cur_size
= compressed_size
;
150 path
= btrfs_alloc_path();
154 path
->leave_spinning
= 1;
156 key
.objectid
= btrfs_ino(inode
);
158 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
159 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
161 inode_add_bytes(inode
, size
);
162 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
168 leaf
= path
->nodes
[0];
169 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
170 struct btrfs_file_extent_item
);
171 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
172 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
173 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
174 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
175 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
176 ptr
= btrfs_file_extent_inline_start(ei
);
178 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
181 while (compressed_size
> 0) {
182 cpage
= compressed_pages
[i
];
183 cur_size
= min_t(unsigned long, compressed_size
,
186 kaddr
= kmap_atomic(cpage
);
187 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
188 kunmap_atomic(kaddr
);
192 compressed_size
-= cur_size
;
194 btrfs_set_file_extent_compression(leaf
, ei
,
197 page
= find_get_page(inode
->i_mapping
,
198 start
>> PAGE_CACHE_SHIFT
);
199 btrfs_set_file_extent_compression(leaf
, ei
, 0);
200 kaddr
= kmap_atomic(page
);
201 offset
= start
& (PAGE_CACHE_SIZE
- 1);
202 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
203 kunmap_atomic(kaddr
);
204 page_cache_release(page
);
206 btrfs_mark_buffer_dirty(leaf
);
207 btrfs_free_path(path
);
210 * we're an inline extent, so nobody can
211 * extend the file past i_size without locking
212 * a page we already have locked.
214 * We must do any isize and inode updates
215 * before we unlock the pages. Otherwise we
216 * could end up racing with unlink.
218 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
219 ret
= btrfs_update_inode(trans
, root
, inode
);
223 btrfs_free_path(path
);
229 * conditionally insert an inline extent into the file. This
230 * does the checks required to make sure the data is small enough
231 * to fit as an inline extent.
233 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
234 struct btrfs_root
*root
,
235 struct inode
*inode
, u64 start
, u64 end
,
236 size_t compressed_size
, int compress_type
,
237 struct page
**compressed_pages
)
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 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, aligned_end
, 1);
263 if (isize
> actual_end
)
264 inline_len
= min_t(u64
, isize
, actual_end
);
265 ret
= insert_inline_extent(trans
, root
, inode
, start
,
266 inline_len
, compressed_size
,
267 compress_type
, compressed_pages
);
268 if (ret
&& ret
!= -ENOSPC
) {
269 btrfs_abort_transaction(trans
, root
, ret
);
271 } else if (ret
== -ENOSPC
) {
275 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
276 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
277 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
281 struct async_extent
{
286 unsigned long nr_pages
;
288 struct list_head list
;
293 struct btrfs_root
*root
;
294 struct page
*locked_page
;
297 struct list_head extents
;
298 struct btrfs_work work
;
301 static noinline
int add_async_extent(struct async_cow
*cow
,
302 u64 start
, u64 ram_size
,
305 unsigned long nr_pages
,
308 struct async_extent
*async_extent
;
310 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
311 BUG_ON(!async_extent
); /* -ENOMEM */
312 async_extent
->start
= start
;
313 async_extent
->ram_size
= ram_size
;
314 async_extent
->compressed_size
= compressed_size
;
315 async_extent
->pages
= pages
;
316 async_extent
->nr_pages
= nr_pages
;
317 async_extent
->compress_type
= compress_type
;
318 list_add_tail(&async_extent
->list
, &cow
->extents
);
323 * we create compressed extents in two phases. The first
324 * phase compresses a range of pages that have already been
325 * locked (both pages and state bits are locked).
327 * This is done inside an ordered work queue, and the compression
328 * is spread across many cpus. The actual IO submission is step
329 * two, and the ordered work queue takes care of making sure that
330 * happens in the same order things were put onto the queue by
331 * writepages and friends.
333 * If this code finds it can't get good compression, it puts an
334 * entry onto the work queue to write the uncompressed bytes. This
335 * makes sure that both compressed inodes and uncompressed inodes
336 * are written in the same order that the flusher thread sent them
339 static noinline
int compress_file_range(struct inode
*inode
,
340 struct page
*locked_page
,
342 struct async_cow
*async_cow
,
345 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
346 struct btrfs_trans_handle
*trans
;
348 u64 blocksize
= root
->sectorsize
;
350 u64 isize
= i_size_read(inode
);
352 struct page
**pages
= NULL
;
353 unsigned long nr_pages
;
354 unsigned long nr_pages_ret
= 0;
355 unsigned long total_compressed
= 0;
356 unsigned long total_in
= 0;
357 unsigned long max_compressed
= 128 * 1024;
358 unsigned long max_uncompressed
= 128 * 1024;
361 int compress_type
= root
->fs_info
->compress_type
;
364 /* if this is a small write inside eof, kick off a defrag */
365 if ((end
- start
+ 1) < 16 * 1024 &&
366 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
367 btrfs_add_inode_defrag(NULL
, inode
);
369 actual_end
= min_t(u64
, isize
, end
+ 1);
372 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
373 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
376 * we don't want to send crud past the end of i_size through
377 * compression, that's just a waste of CPU time. So, if the
378 * end of the file is before the start of our current
379 * requested range of bytes, we bail out to the uncompressed
380 * cleanup code that can deal with all of this.
382 * It isn't really the fastest way to fix things, but this is a
383 * very uncommon corner.
385 if (actual_end
<= start
)
386 goto cleanup_and_bail_uncompressed
;
388 total_compressed
= actual_end
- start
;
390 /* we want to make sure that amount of ram required to uncompress
391 * an extent is reasonable, so we limit the total size in ram
392 * of a compressed extent to 128k. This is a crucial number
393 * because it also controls how easily we can spread reads across
394 * cpus for decompression.
396 * We also want to make sure the amount of IO required to do
397 * a random read is reasonably small, so we limit the size of
398 * a compressed extent to 128k.
400 total_compressed
= min(total_compressed
, max_uncompressed
);
401 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
402 num_bytes
= max(blocksize
, num_bytes
);
407 * we do compression for mount -o compress and when the
408 * inode has not been flagged as nocompress. This flag can
409 * change at any time if we discover bad compression ratios.
411 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
412 (btrfs_test_opt(root
, COMPRESS
) ||
413 (BTRFS_I(inode
)->force_compress
) ||
414 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
416 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
418 /* just bail out to the uncompressed code */
422 if (BTRFS_I(inode
)->force_compress
)
423 compress_type
= BTRFS_I(inode
)->force_compress
;
426 * we need to call clear_page_dirty_for_io on each
427 * page in the range. Otherwise applications with the file
428 * mmap'd can wander in and change the page contents while
429 * we are compressing them.
431 * If the compression fails for any reason, we set the pages
432 * dirty again later on.
434 extent_range_clear_dirty_for_io(inode
, start
, end
);
436 ret
= btrfs_compress_pages(compress_type
,
437 inode
->i_mapping
, start
,
438 total_compressed
, pages
,
439 nr_pages
, &nr_pages_ret
,
445 unsigned long offset
= total_compressed
&
446 (PAGE_CACHE_SIZE
- 1);
447 struct page
*page
= pages
[nr_pages_ret
- 1];
450 /* zero the tail end of the last page, we might be
451 * sending it down to disk
454 kaddr
= kmap_atomic(page
);
455 memset(kaddr
+ offset
, 0,
456 PAGE_CACHE_SIZE
- offset
);
457 kunmap_atomic(kaddr
);
464 trans
= btrfs_join_transaction(root
);
466 ret
= PTR_ERR(trans
);
468 goto cleanup_and_out
;
470 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
472 /* lets try to make an inline extent */
473 if (ret
|| total_in
< (actual_end
- start
)) {
474 /* we didn't compress the entire range, try
475 * to make an uncompressed inline extent.
477 ret
= cow_file_range_inline(trans
, root
, inode
,
478 start
, end
, 0, 0, NULL
);
480 /* try making a compressed inline extent */
481 ret
= cow_file_range_inline(trans
, root
, inode
,
484 compress_type
, pages
);
488 * inline extent creation worked or returned error,
489 * we don't need to create any more async work items.
490 * Unlock and free up our temp pages.
492 extent_clear_unlock_delalloc(inode
,
493 &BTRFS_I(inode
)->io_tree
,
495 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
496 EXTENT_CLEAR_DELALLOC
|
497 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
499 btrfs_end_transaction(trans
, root
);
502 btrfs_end_transaction(trans
, root
);
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
]);
595 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
597 EXTENT_CLEAR_UNLOCK_PAGE
|
599 EXTENT_CLEAR_DELALLOC
|
600 EXTENT_SET_WRITEBACK
|
601 EXTENT_END_WRITEBACK
);
602 if (!trans
|| IS_ERR(trans
))
603 btrfs_error(root
->fs_info
, ret
, "Failed to join transaction");
605 btrfs_abort_transaction(trans
, root
, ret
);
610 * phase two of compressed writeback. This is the ordered portion
611 * of the code, which only gets called in the order the work was
612 * queued. We walk all the async extents created by compress_file_range
613 * and send them down to the disk.
615 static noinline
int submit_compressed_extents(struct inode
*inode
,
616 struct async_cow
*async_cow
)
618 struct async_extent
*async_extent
;
620 struct btrfs_trans_handle
*trans
;
621 struct btrfs_key ins
;
622 struct extent_map
*em
;
623 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
624 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
625 struct extent_io_tree
*io_tree
;
628 if (list_empty(&async_cow
->extents
))
632 while (!list_empty(&async_cow
->extents
)) {
633 async_extent
= list_entry(async_cow
->extents
.next
,
634 struct async_extent
, list
);
635 list_del(&async_extent
->list
);
637 io_tree
= &BTRFS_I(inode
)->io_tree
;
640 /* did the compression code fall back to uncompressed IO? */
641 if (!async_extent
->pages
) {
642 int page_started
= 0;
643 unsigned long nr_written
= 0;
645 lock_extent(io_tree
, async_extent
->start
,
646 async_extent
->start
+
647 async_extent
->ram_size
- 1);
649 /* allocate blocks */
650 ret
= cow_file_range(inode
, async_cow
->locked_page
,
652 async_extent
->start
+
653 async_extent
->ram_size
- 1,
654 &page_started
, &nr_written
, 0);
659 * if page_started, cow_file_range inserted an
660 * inline extent and took care of all the unlocking
661 * and IO for us. Otherwise, we need to submit
662 * all those pages down to the drive.
664 if (!page_started
&& !ret
)
665 extent_write_locked_range(io_tree
,
666 inode
, async_extent
->start
,
667 async_extent
->start
+
668 async_extent
->ram_size
- 1,
672 unlock_page(async_cow
->locked_page
);
678 lock_extent(io_tree
, async_extent
->start
,
679 async_extent
->start
+ async_extent
->ram_size
- 1);
681 trans
= btrfs_join_transaction(root
);
683 ret
= PTR_ERR(trans
);
685 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
686 ret
= btrfs_reserve_extent(trans
, root
,
687 async_extent
->compressed_size
,
688 async_extent
->compressed_size
,
689 0, alloc_hint
, &ins
, 1);
690 if (ret
&& ret
!= -ENOSPC
)
691 btrfs_abort_transaction(trans
, root
, ret
);
692 btrfs_end_transaction(trans
, root
);
698 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
699 WARN_ON(async_extent
->pages
[i
]->mapping
);
700 page_cache_release(async_extent
->pages
[i
]);
702 kfree(async_extent
->pages
);
703 async_extent
->nr_pages
= 0;
704 async_extent
->pages
= NULL
;
706 if (ret
== -ENOSPC
) {
707 unlock_extent(io_tree
, async_extent
->start
,
708 async_extent
->start
+
709 async_extent
->ram_size
- 1);
716 * here we're doing allocation and writeback of the
719 btrfs_drop_extent_cache(inode
, async_extent
->start
,
720 async_extent
->start
+
721 async_extent
->ram_size
- 1, 0);
723 em
= alloc_extent_map();
726 goto out_free_reserve
;
728 em
->start
= async_extent
->start
;
729 em
->len
= async_extent
->ram_size
;
730 em
->orig_start
= em
->start
;
731 em
->mod_start
= em
->start
;
732 em
->mod_len
= em
->len
;
734 em
->block_start
= ins
.objectid
;
735 em
->block_len
= ins
.offset
;
736 em
->orig_block_len
= ins
.offset
;
737 em
->ram_bytes
= async_extent
->ram_size
;
738 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
739 em
->compress_type
= async_extent
->compress_type
;
740 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
741 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
745 write_lock(&em_tree
->lock
);
746 ret
= add_extent_mapping(em_tree
, em
, 1);
747 write_unlock(&em_tree
->lock
);
748 if (ret
!= -EEXIST
) {
752 btrfs_drop_extent_cache(inode
, async_extent
->start
,
753 async_extent
->start
+
754 async_extent
->ram_size
- 1, 0);
758 goto out_free_reserve
;
760 ret
= btrfs_add_ordered_extent_compress(inode
,
763 async_extent
->ram_size
,
765 BTRFS_ORDERED_COMPRESSED
,
766 async_extent
->compress_type
);
768 goto out_free_reserve
;
771 * clear dirty, set writeback and unlock the pages.
773 extent_clear_unlock_delalloc(inode
,
774 &BTRFS_I(inode
)->io_tree
,
776 async_extent
->start
+
777 async_extent
->ram_size
- 1,
778 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
779 EXTENT_CLEAR_UNLOCK
|
780 EXTENT_CLEAR_DELALLOC
|
781 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
783 ret
= btrfs_submit_compressed_write(inode
,
785 async_extent
->ram_size
,
787 ins
.offset
, async_extent
->pages
,
788 async_extent
->nr_pages
);
789 alloc_hint
= ins
.objectid
+ ins
.offset
;
799 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
801 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
803 async_extent
->start
+
804 async_extent
->ram_size
- 1,
805 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
806 EXTENT_CLEAR_UNLOCK
|
807 EXTENT_CLEAR_DELALLOC
|
809 EXTENT_SET_WRITEBACK
|
810 EXTENT_END_WRITEBACK
);
815 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
818 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
819 struct extent_map
*em
;
822 read_lock(&em_tree
->lock
);
823 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
826 * if block start isn't an actual block number then find the
827 * first block in this inode and use that as a hint. If that
828 * block is also bogus then just don't worry about it.
830 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
832 em
= search_extent_mapping(em_tree
, 0, 0);
833 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
834 alloc_hint
= em
->block_start
;
838 alloc_hint
= em
->block_start
;
842 read_unlock(&em_tree
->lock
);
848 * when extent_io.c finds a delayed allocation range in the file,
849 * the call backs end up in this code. The basic idea is to
850 * allocate extents on disk for the range, and create ordered data structs
851 * in ram to track those extents.
853 * locked_page is the page that writepage had locked already. We use
854 * it to make sure we don't do extra locks or unlocks.
856 * *page_started is set to one if we unlock locked_page and do everything
857 * required to start IO on it. It may be clean and already done with
860 static noinline
int __cow_file_range(struct btrfs_trans_handle
*trans
,
862 struct btrfs_root
*root
,
863 struct page
*locked_page
,
864 u64 start
, u64 end
, int *page_started
,
865 unsigned long *nr_written
,
870 unsigned long ram_size
;
873 u64 blocksize
= root
->sectorsize
;
874 struct btrfs_key ins
;
875 struct extent_map
*em
;
876 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
879 BUG_ON(btrfs_is_free_space_inode(inode
));
881 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
882 num_bytes
= max(blocksize
, num_bytes
);
883 disk_num_bytes
= num_bytes
;
885 /* if this is a small write inside eof, kick off defrag */
886 if (num_bytes
< 64 * 1024 &&
887 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
888 btrfs_add_inode_defrag(trans
, inode
);
891 /* lets try to make an inline extent */
892 ret
= cow_file_range_inline(trans
, root
, inode
,
893 start
, end
, 0, 0, NULL
);
895 extent_clear_unlock_delalloc(inode
,
896 &BTRFS_I(inode
)->io_tree
,
898 EXTENT_CLEAR_UNLOCK_PAGE
|
899 EXTENT_CLEAR_UNLOCK
|
900 EXTENT_CLEAR_DELALLOC
|
902 EXTENT_SET_WRITEBACK
|
903 EXTENT_END_WRITEBACK
);
905 *nr_written
= *nr_written
+
906 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
909 } else if (ret
< 0) {
910 btrfs_abort_transaction(trans
, root
, ret
);
915 BUG_ON(disk_num_bytes
>
916 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
918 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
919 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
921 while (disk_num_bytes
> 0) {
924 cur_alloc_size
= disk_num_bytes
;
925 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
926 root
->sectorsize
, 0, alloc_hint
,
929 btrfs_abort_transaction(trans
, root
, ret
);
933 em
= alloc_extent_map();
939 em
->orig_start
= em
->start
;
940 ram_size
= ins
.offset
;
941 em
->len
= ins
.offset
;
942 em
->mod_start
= em
->start
;
943 em
->mod_len
= em
->len
;
945 em
->block_start
= ins
.objectid
;
946 em
->block_len
= ins
.offset
;
947 em
->orig_block_len
= ins
.offset
;
948 em
->ram_bytes
= ram_size
;
949 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
950 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
954 write_lock(&em_tree
->lock
);
955 ret
= add_extent_mapping(em_tree
, em
, 1);
956 write_unlock(&em_tree
->lock
);
957 if (ret
!= -EEXIST
) {
961 btrfs_drop_extent_cache(inode
, start
,
962 start
+ ram_size
- 1, 0);
967 cur_alloc_size
= ins
.offset
;
968 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
969 ram_size
, cur_alloc_size
, 0);
973 if (root
->root_key
.objectid
==
974 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
975 ret
= btrfs_reloc_clone_csums(inode
, start
,
978 btrfs_abort_transaction(trans
, root
, ret
);
983 if (disk_num_bytes
< cur_alloc_size
)
986 /* we're not doing compressed IO, don't unlock the first
987 * page (which the caller expects to stay locked), don't
988 * clear any dirty bits and don't set any writeback bits
990 * Do set the Private2 bit so we know this page was properly
991 * setup for writepage
993 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
994 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
997 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
998 start
, start
+ ram_size
- 1,
1000 disk_num_bytes
-= cur_alloc_size
;
1001 num_bytes
-= cur_alloc_size
;
1002 alloc_hint
= ins
.objectid
+ ins
.offset
;
1003 start
+= cur_alloc_size
;
1009 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
1011 extent_clear_unlock_delalloc(inode
,
1012 &BTRFS_I(inode
)->io_tree
,
1013 start
, end
, locked_page
,
1014 EXTENT_CLEAR_UNLOCK_PAGE
|
1015 EXTENT_CLEAR_UNLOCK
|
1016 EXTENT_CLEAR_DELALLOC
|
1017 EXTENT_CLEAR_DIRTY
|
1018 EXTENT_SET_WRITEBACK
|
1019 EXTENT_END_WRITEBACK
);
1024 static noinline
int cow_file_range(struct inode
*inode
,
1025 struct page
*locked_page
,
1026 u64 start
, u64 end
, int *page_started
,
1027 unsigned long *nr_written
,
1030 struct btrfs_trans_handle
*trans
;
1031 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1034 trans
= btrfs_join_transaction(root
);
1035 if (IS_ERR(trans
)) {
1036 extent_clear_unlock_delalloc(inode
,
1037 &BTRFS_I(inode
)->io_tree
,
1038 start
, end
, locked_page
,
1039 EXTENT_CLEAR_UNLOCK_PAGE
|
1040 EXTENT_CLEAR_UNLOCK
|
1041 EXTENT_CLEAR_DELALLOC
|
1042 EXTENT_CLEAR_DIRTY
|
1043 EXTENT_SET_WRITEBACK
|
1044 EXTENT_END_WRITEBACK
);
1045 return PTR_ERR(trans
);
1047 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1049 ret
= __cow_file_range(trans
, inode
, root
, locked_page
, start
, end
,
1050 page_started
, nr_written
, unlock
);
1052 btrfs_end_transaction(trans
, root
);
1058 * work queue call back to started compression on a file and pages
1060 static noinline
void async_cow_start(struct btrfs_work
*work
)
1062 struct async_cow
*async_cow
;
1064 async_cow
= container_of(work
, struct async_cow
, work
);
1066 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1067 async_cow
->start
, async_cow
->end
, async_cow
,
1069 if (num_added
== 0) {
1070 btrfs_add_delayed_iput(async_cow
->inode
);
1071 async_cow
->inode
= NULL
;
1076 * work queue call back to submit previously compressed pages
1078 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1080 struct async_cow
*async_cow
;
1081 struct btrfs_root
*root
;
1082 unsigned long nr_pages
;
1084 async_cow
= container_of(work
, struct async_cow
, work
);
1086 root
= async_cow
->root
;
1087 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1090 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1092 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1093 wake_up(&root
->fs_info
->async_submit_wait
);
1095 if (async_cow
->inode
)
1096 submit_compressed_extents(async_cow
->inode
, async_cow
);
1099 static noinline
void async_cow_free(struct btrfs_work
*work
)
1101 struct async_cow
*async_cow
;
1102 async_cow
= container_of(work
, struct async_cow
, work
);
1103 if (async_cow
->inode
)
1104 btrfs_add_delayed_iput(async_cow
->inode
);
1108 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1109 u64 start
, u64 end
, int *page_started
,
1110 unsigned long *nr_written
)
1112 struct async_cow
*async_cow
;
1113 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1114 unsigned long nr_pages
;
1116 int limit
= 10 * 1024 * 1024;
1118 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1119 1, 0, NULL
, GFP_NOFS
);
1120 while (start
< end
) {
1121 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1122 BUG_ON(!async_cow
); /* -ENOMEM */
1123 async_cow
->inode
= igrab(inode
);
1124 async_cow
->root
= root
;
1125 async_cow
->locked_page
= locked_page
;
1126 async_cow
->start
= start
;
1128 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1131 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1133 async_cow
->end
= cur_end
;
1134 INIT_LIST_HEAD(&async_cow
->extents
);
1136 async_cow
->work
.func
= async_cow_start
;
1137 async_cow
->work
.ordered_func
= async_cow_submit
;
1138 async_cow
->work
.ordered_free
= async_cow_free
;
1139 async_cow
->work
.flags
= 0;
1141 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1143 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1145 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
1148 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1149 wait_event(root
->fs_info
->async_submit_wait
,
1150 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1154 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1155 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1156 wait_event(root
->fs_info
->async_submit_wait
,
1157 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1161 *nr_written
+= nr_pages
;
1162 start
= cur_end
+ 1;
1168 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1169 u64 bytenr
, u64 num_bytes
)
1172 struct btrfs_ordered_sum
*sums
;
1175 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1176 bytenr
+ num_bytes
- 1, &list
, 0);
1177 if (ret
== 0 && list_empty(&list
))
1180 while (!list_empty(&list
)) {
1181 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1182 list_del(&sums
->list
);
1189 * when nowcow writeback call back. This checks for snapshots or COW copies
1190 * of the extents that exist in the file, and COWs the file as required.
1192 * If no cow copies or snapshots exist, we write directly to the existing
1195 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1196 struct page
*locked_page
,
1197 u64 start
, u64 end
, int *page_started
, int force
,
1198 unsigned long *nr_written
)
1200 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1201 struct btrfs_trans_handle
*trans
;
1202 struct extent_buffer
*leaf
;
1203 struct btrfs_path
*path
;
1204 struct btrfs_file_extent_item
*fi
;
1205 struct btrfs_key found_key
;
1220 u64 ino
= btrfs_ino(inode
);
1222 path
= btrfs_alloc_path();
1224 extent_clear_unlock_delalloc(inode
,
1225 &BTRFS_I(inode
)->io_tree
,
1226 start
, end
, locked_page
,
1227 EXTENT_CLEAR_UNLOCK_PAGE
|
1228 EXTENT_CLEAR_UNLOCK
|
1229 EXTENT_CLEAR_DELALLOC
|
1230 EXTENT_CLEAR_DIRTY
|
1231 EXTENT_SET_WRITEBACK
|
1232 EXTENT_END_WRITEBACK
);
1236 nolock
= btrfs_is_free_space_inode(inode
);
1239 trans
= btrfs_join_transaction_nolock(root
);
1241 trans
= btrfs_join_transaction(root
);
1243 if (IS_ERR(trans
)) {
1244 extent_clear_unlock_delalloc(inode
,
1245 &BTRFS_I(inode
)->io_tree
,
1246 start
, end
, locked_page
,
1247 EXTENT_CLEAR_UNLOCK_PAGE
|
1248 EXTENT_CLEAR_UNLOCK
|
1249 EXTENT_CLEAR_DELALLOC
|
1250 EXTENT_CLEAR_DIRTY
|
1251 EXTENT_SET_WRITEBACK
|
1252 EXTENT_END_WRITEBACK
);
1253 btrfs_free_path(path
);
1254 return PTR_ERR(trans
);
1257 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1259 cow_start
= (u64
)-1;
1262 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1265 btrfs_abort_transaction(trans
, root
, ret
);
1268 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1269 leaf
= path
->nodes
[0];
1270 btrfs_item_key_to_cpu(leaf
, &found_key
,
1271 path
->slots
[0] - 1);
1272 if (found_key
.objectid
== ino
&&
1273 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1278 leaf
= path
->nodes
[0];
1279 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1280 ret
= btrfs_next_leaf(root
, path
);
1282 btrfs_abort_transaction(trans
, root
, ret
);
1287 leaf
= path
->nodes
[0];
1293 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1295 if (found_key
.objectid
> ino
||
1296 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1297 found_key
.offset
> end
)
1300 if (found_key
.offset
> cur_offset
) {
1301 extent_end
= found_key
.offset
;
1306 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1307 struct btrfs_file_extent_item
);
1308 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1310 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1311 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1312 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1313 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1314 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1315 extent_end
= found_key
.offset
+
1316 btrfs_file_extent_num_bytes(leaf
, fi
);
1318 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1319 if (extent_end
<= start
) {
1323 if (disk_bytenr
== 0)
1325 if (btrfs_file_extent_compression(leaf
, fi
) ||
1326 btrfs_file_extent_encryption(leaf
, fi
) ||
1327 btrfs_file_extent_other_encoding(leaf
, fi
))
1329 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1331 if (btrfs_extent_readonly(root
, disk_bytenr
))
1333 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1335 extent_offset
, disk_bytenr
))
1337 disk_bytenr
+= extent_offset
;
1338 disk_bytenr
+= cur_offset
- found_key
.offset
;
1339 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1341 * force cow if csum exists in the range.
1342 * this ensure that csum for a given extent are
1343 * either valid or do not exist.
1345 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1348 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1349 extent_end
= found_key
.offset
+
1350 btrfs_file_extent_inline_len(leaf
, fi
);
1351 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1356 if (extent_end
<= start
) {
1361 if (cow_start
== (u64
)-1)
1362 cow_start
= cur_offset
;
1363 cur_offset
= extent_end
;
1364 if (cur_offset
> end
)
1370 btrfs_release_path(path
);
1371 if (cow_start
!= (u64
)-1) {
1372 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1373 cow_start
, found_key
.offset
- 1,
1374 page_started
, nr_written
, 1);
1376 btrfs_abort_transaction(trans
, root
, ret
);
1379 cow_start
= (u64
)-1;
1382 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1383 struct extent_map
*em
;
1384 struct extent_map_tree
*em_tree
;
1385 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1386 em
= alloc_extent_map();
1387 BUG_ON(!em
); /* -ENOMEM */
1388 em
->start
= cur_offset
;
1389 em
->orig_start
= found_key
.offset
- extent_offset
;
1390 em
->len
= num_bytes
;
1391 em
->block_len
= num_bytes
;
1392 em
->block_start
= disk_bytenr
;
1393 em
->orig_block_len
= disk_num_bytes
;
1394 em
->ram_bytes
= ram_bytes
;
1395 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1396 em
->mod_start
= em
->start
;
1397 em
->mod_len
= em
->len
;
1398 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1399 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1400 em
->generation
= -1;
1402 write_lock(&em_tree
->lock
);
1403 ret
= add_extent_mapping(em_tree
, em
, 1);
1404 write_unlock(&em_tree
->lock
);
1405 if (ret
!= -EEXIST
) {
1406 free_extent_map(em
);
1409 btrfs_drop_extent_cache(inode
, em
->start
,
1410 em
->start
+ em
->len
- 1, 0);
1412 type
= BTRFS_ORDERED_PREALLOC
;
1414 type
= BTRFS_ORDERED_NOCOW
;
1417 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1418 num_bytes
, num_bytes
, type
);
1419 BUG_ON(ret
); /* -ENOMEM */
1421 if (root
->root_key
.objectid
==
1422 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1423 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1426 btrfs_abort_transaction(trans
, root
, ret
);
1431 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1432 cur_offset
, cur_offset
+ num_bytes
- 1,
1433 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1434 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1435 EXTENT_SET_PRIVATE2
);
1436 cur_offset
= extent_end
;
1437 if (cur_offset
> end
)
1440 btrfs_release_path(path
);
1442 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1443 cow_start
= cur_offset
;
1447 if (cow_start
!= (u64
)-1) {
1448 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1450 page_started
, nr_written
, 1);
1452 btrfs_abort_transaction(trans
, root
, ret
);
1458 err
= btrfs_end_transaction(trans
, root
);
1462 if (ret
&& cur_offset
< end
)
1463 extent_clear_unlock_delalloc(inode
,
1464 &BTRFS_I(inode
)->io_tree
,
1465 cur_offset
, end
, locked_page
,
1466 EXTENT_CLEAR_UNLOCK_PAGE
|
1467 EXTENT_CLEAR_UNLOCK
|
1468 EXTENT_CLEAR_DELALLOC
|
1469 EXTENT_CLEAR_DIRTY
|
1470 EXTENT_SET_WRITEBACK
|
1471 EXTENT_END_WRITEBACK
);
1473 btrfs_free_path(path
);
1478 * extent_io.c call back to do delayed allocation processing
1480 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1481 u64 start
, u64 end
, int *page_started
,
1482 unsigned long *nr_written
)
1485 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1487 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1488 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1489 page_started
, 1, nr_written
);
1490 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1491 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1492 page_started
, 0, nr_written
);
1493 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1494 !(BTRFS_I(inode
)->force_compress
) &&
1495 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1496 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1497 page_started
, nr_written
, 1);
1499 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1500 &BTRFS_I(inode
)->runtime_flags
);
1501 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1502 page_started
, nr_written
);
1507 static void btrfs_split_extent_hook(struct inode
*inode
,
1508 struct extent_state
*orig
, u64 split
)
1510 /* not delalloc, ignore it */
1511 if (!(orig
->state
& EXTENT_DELALLOC
))
1514 spin_lock(&BTRFS_I(inode
)->lock
);
1515 BTRFS_I(inode
)->outstanding_extents
++;
1516 spin_unlock(&BTRFS_I(inode
)->lock
);
1520 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1521 * extents so we can keep track of new extents that are just merged onto old
1522 * extents, such as when we are doing sequential writes, so we can properly
1523 * account for the metadata space we'll need.
1525 static void btrfs_merge_extent_hook(struct inode
*inode
,
1526 struct extent_state
*new,
1527 struct extent_state
*other
)
1529 /* not delalloc, ignore it */
1530 if (!(other
->state
& EXTENT_DELALLOC
))
1533 spin_lock(&BTRFS_I(inode
)->lock
);
1534 BTRFS_I(inode
)->outstanding_extents
--;
1535 spin_unlock(&BTRFS_I(inode
)->lock
);
1538 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1539 struct inode
*inode
)
1541 spin_lock(&root
->delalloc_lock
);
1542 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1543 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1544 &root
->delalloc_inodes
);
1545 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1546 &BTRFS_I(inode
)->runtime_flags
);
1547 root
->nr_delalloc_inodes
++;
1548 if (root
->nr_delalloc_inodes
== 1) {
1549 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1550 BUG_ON(!list_empty(&root
->delalloc_root
));
1551 list_add_tail(&root
->delalloc_root
,
1552 &root
->fs_info
->delalloc_roots
);
1553 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1556 spin_unlock(&root
->delalloc_lock
);
1559 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1560 struct inode
*inode
)
1562 spin_lock(&root
->delalloc_lock
);
1563 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1564 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1565 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1566 &BTRFS_I(inode
)->runtime_flags
);
1567 root
->nr_delalloc_inodes
--;
1568 if (!root
->nr_delalloc_inodes
) {
1569 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1570 BUG_ON(list_empty(&root
->delalloc_root
));
1571 list_del_init(&root
->delalloc_root
);
1572 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1575 spin_unlock(&root
->delalloc_lock
);
1579 * extent_io.c set_bit_hook, used to track delayed allocation
1580 * bytes in this file, and to maintain the list of inodes that
1581 * have pending delalloc work to be done.
1583 static void btrfs_set_bit_hook(struct inode
*inode
,
1584 struct extent_state
*state
, unsigned long *bits
)
1588 * set_bit and clear bit hooks normally require _irqsave/restore
1589 * but in this case, we are only testing for the DELALLOC
1590 * bit, which is only set or cleared with irqs on
1592 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1593 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1594 u64 len
= state
->end
+ 1 - state
->start
;
1595 bool do_list
= !btrfs_is_free_space_inode(inode
);
1597 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1598 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1600 spin_lock(&BTRFS_I(inode
)->lock
);
1601 BTRFS_I(inode
)->outstanding_extents
++;
1602 spin_unlock(&BTRFS_I(inode
)->lock
);
1605 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1606 root
->fs_info
->delalloc_batch
);
1607 spin_lock(&BTRFS_I(inode
)->lock
);
1608 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1609 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1610 &BTRFS_I(inode
)->runtime_flags
))
1611 btrfs_add_delalloc_inodes(root
, inode
);
1612 spin_unlock(&BTRFS_I(inode
)->lock
);
1617 * extent_io.c clear_bit_hook, see set_bit_hook for why
1619 static void btrfs_clear_bit_hook(struct inode
*inode
,
1620 struct extent_state
*state
,
1621 unsigned long *bits
)
1624 * set_bit and clear bit hooks normally require _irqsave/restore
1625 * but in this case, we are only testing for the DELALLOC
1626 * bit, which is only set or cleared with irqs on
1628 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1629 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1630 u64 len
= state
->end
+ 1 - state
->start
;
1631 bool do_list
= !btrfs_is_free_space_inode(inode
);
1633 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1634 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1635 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1636 spin_lock(&BTRFS_I(inode
)->lock
);
1637 BTRFS_I(inode
)->outstanding_extents
--;
1638 spin_unlock(&BTRFS_I(inode
)->lock
);
1641 if (*bits
& EXTENT_DO_ACCOUNTING
)
1642 btrfs_delalloc_release_metadata(inode
, len
);
1644 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1645 && do_list
&& !(state
->state
& EXTENT_NORESERVE
))
1646 btrfs_free_reserved_data_space(inode
, len
);
1648 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1649 root
->fs_info
->delalloc_batch
);
1650 spin_lock(&BTRFS_I(inode
)->lock
);
1651 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1652 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1653 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1654 &BTRFS_I(inode
)->runtime_flags
))
1655 btrfs_del_delalloc_inode(root
, inode
);
1656 spin_unlock(&BTRFS_I(inode
)->lock
);
1661 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1662 * we don't create bios that span stripes or chunks
1664 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1665 size_t size
, struct bio
*bio
,
1666 unsigned long bio_flags
)
1668 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1669 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1674 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1677 length
= bio
->bi_size
;
1678 map_length
= length
;
1679 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1680 &map_length
, NULL
, 0);
1681 /* Will always return 0 with map_multi == NULL */
1683 if (map_length
< length
+ size
)
1689 * in order to insert checksums into the metadata in large chunks,
1690 * we wait until bio submission time. All the pages in the bio are
1691 * checksummed and sums are attached onto the ordered extent record.
1693 * At IO completion time the cums attached on the ordered extent record
1694 * are inserted into the btree
1696 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1697 struct bio
*bio
, int mirror_num
,
1698 unsigned long bio_flags
,
1701 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1704 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1705 BUG_ON(ret
); /* -ENOMEM */
1710 * in order to insert checksums into the metadata in large chunks,
1711 * we wait until bio submission time. All the pages in the bio are
1712 * checksummed and sums are attached onto the ordered extent record.
1714 * At IO completion time the cums attached on the ordered extent record
1715 * are inserted into the btree
1717 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1718 int mirror_num
, unsigned long bio_flags
,
1721 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1724 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1726 bio_endio(bio
, ret
);
1731 * extent_io.c submission hook. This does the right thing for csum calculation
1732 * on write, or reading the csums from the tree before a read
1734 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1735 int mirror_num
, unsigned long bio_flags
,
1738 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1742 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1744 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1746 if (btrfs_is_free_space_inode(inode
))
1749 if (!(rw
& REQ_WRITE
)) {
1750 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1754 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1755 ret
= btrfs_submit_compressed_read(inode
, bio
,
1759 } else if (!skip_sum
) {
1760 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1765 } else if (async
&& !skip_sum
) {
1766 /* csum items have already been cloned */
1767 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1769 /* we're doing a write, do the async checksumming */
1770 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1771 inode
, rw
, bio
, mirror_num
,
1772 bio_flags
, bio_offset
,
1773 __btrfs_submit_bio_start
,
1774 __btrfs_submit_bio_done
);
1776 } else if (!skip_sum
) {
1777 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1783 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1787 bio_endio(bio
, ret
);
1792 * given a list of ordered sums record them in the inode. This happens
1793 * at IO completion time based on sums calculated at bio submission time.
1795 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1796 struct inode
*inode
, u64 file_offset
,
1797 struct list_head
*list
)
1799 struct btrfs_ordered_sum
*sum
;
1801 list_for_each_entry(sum
, list
, list
) {
1802 trans
->adding_csums
= 1;
1803 btrfs_csum_file_blocks(trans
,
1804 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1805 trans
->adding_csums
= 0;
1810 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1811 struct extent_state
**cached_state
)
1813 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1814 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1815 cached_state
, GFP_NOFS
);
1818 /* see btrfs_writepage_start_hook for details on why this is required */
1819 struct btrfs_writepage_fixup
{
1821 struct btrfs_work work
;
1824 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1826 struct btrfs_writepage_fixup
*fixup
;
1827 struct btrfs_ordered_extent
*ordered
;
1828 struct extent_state
*cached_state
= NULL
;
1830 struct inode
*inode
;
1835 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1839 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1840 ClearPageChecked(page
);
1844 inode
= page
->mapping
->host
;
1845 page_start
= page_offset(page
);
1846 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1848 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1851 /* already ordered? We're done */
1852 if (PagePrivate2(page
))
1855 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1857 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1858 page_end
, &cached_state
, GFP_NOFS
);
1860 btrfs_start_ordered_extent(inode
, ordered
, 1);
1861 btrfs_put_ordered_extent(ordered
);
1865 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1867 mapping_set_error(page
->mapping
, ret
);
1868 end_extent_writepage(page
, ret
, page_start
, page_end
);
1869 ClearPageChecked(page
);
1873 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1874 ClearPageChecked(page
);
1875 set_page_dirty(page
);
1877 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1878 &cached_state
, GFP_NOFS
);
1881 page_cache_release(page
);
1886 * There are a few paths in the higher layers of the kernel that directly
1887 * set the page dirty bit without asking the filesystem if it is a
1888 * good idea. This causes problems because we want to make sure COW
1889 * properly happens and the data=ordered rules are followed.
1891 * In our case any range that doesn't have the ORDERED bit set
1892 * hasn't been properly setup for IO. We kick off an async process
1893 * to fix it up. The async helper will wait for ordered extents, set
1894 * the delalloc bit and make it safe to write the page.
1896 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1898 struct inode
*inode
= page
->mapping
->host
;
1899 struct btrfs_writepage_fixup
*fixup
;
1900 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1902 /* this page is properly in the ordered list */
1903 if (TestClearPagePrivate2(page
))
1906 if (PageChecked(page
))
1909 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1913 SetPageChecked(page
);
1914 page_cache_get(page
);
1915 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1917 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1921 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1922 struct inode
*inode
, u64 file_pos
,
1923 u64 disk_bytenr
, u64 disk_num_bytes
,
1924 u64 num_bytes
, u64 ram_bytes
,
1925 u8 compression
, u8 encryption
,
1926 u16 other_encoding
, int extent_type
)
1928 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1929 struct btrfs_file_extent_item
*fi
;
1930 struct btrfs_path
*path
;
1931 struct extent_buffer
*leaf
;
1932 struct btrfs_key ins
;
1935 path
= btrfs_alloc_path();
1939 path
->leave_spinning
= 1;
1942 * we may be replacing one extent in the tree with another.
1943 * The new extent is pinned in the extent map, and we don't want
1944 * to drop it from the cache until it is completely in the btree.
1946 * So, tell btrfs_drop_extents to leave this extent in the cache.
1947 * the caller is expected to unpin it and allow it to be merged
1950 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1951 file_pos
+ num_bytes
, 0);
1955 ins
.objectid
= btrfs_ino(inode
);
1956 ins
.offset
= file_pos
;
1957 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1958 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1961 leaf
= path
->nodes
[0];
1962 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1963 struct btrfs_file_extent_item
);
1964 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1965 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1966 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1967 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1968 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1969 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1970 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1971 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1972 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1973 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1975 btrfs_mark_buffer_dirty(leaf
);
1976 btrfs_release_path(path
);
1978 inode_add_bytes(inode
, num_bytes
);
1980 ins
.objectid
= disk_bytenr
;
1981 ins
.offset
= disk_num_bytes
;
1982 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1983 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1984 root
->root_key
.objectid
,
1985 btrfs_ino(inode
), file_pos
, &ins
);
1987 btrfs_free_path(path
);
1992 /* snapshot-aware defrag */
1993 struct sa_defrag_extent_backref
{
1994 struct rb_node node
;
1995 struct old_sa_defrag_extent
*old
;
2004 struct old_sa_defrag_extent
{
2005 struct list_head list
;
2006 struct new_sa_defrag_extent
*new;
2015 struct new_sa_defrag_extent
{
2016 struct rb_root root
;
2017 struct list_head head
;
2018 struct btrfs_path
*path
;
2019 struct inode
*inode
;
2027 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
2028 struct sa_defrag_extent_backref
*b2
)
2030 if (b1
->root_id
< b2
->root_id
)
2032 else if (b1
->root_id
> b2
->root_id
)
2035 if (b1
->inum
< b2
->inum
)
2037 else if (b1
->inum
> b2
->inum
)
2040 if (b1
->file_pos
< b2
->file_pos
)
2042 else if (b1
->file_pos
> b2
->file_pos
)
2046 * [------------------------------] ===> (a range of space)
2047 * |<--->| |<---->| =============> (fs/file tree A)
2048 * |<---------------------------->| ===> (fs/file tree B)
2050 * A range of space can refer to two file extents in one tree while
2051 * refer to only one file extent in another tree.
2053 * So we may process a disk offset more than one time(two extents in A)
2054 * and locate at the same extent(one extent in B), then insert two same
2055 * backrefs(both refer to the extent in B).
2060 static void backref_insert(struct rb_root
*root
,
2061 struct sa_defrag_extent_backref
*backref
)
2063 struct rb_node
**p
= &root
->rb_node
;
2064 struct rb_node
*parent
= NULL
;
2065 struct sa_defrag_extent_backref
*entry
;
2070 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2072 ret
= backref_comp(backref
, entry
);
2076 p
= &(*p
)->rb_right
;
2079 rb_link_node(&backref
->node
, parent
, p
);
2080 rb_insert_color(&backref
->node
, root
);
2084 * Note the backref might has changed, and in this case we just return 0.
2086 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2089 struct btrfs_file_extent_item
*extent
;
2090 struct btrfs_fs_info
*fs_info
;
2091 struct old_sa_defrag_extent
*old
= ctx
;
2092 struct new_sa_defrag_extent
*new = old
->new;
2093 struct btrfs_path
*path
= new->path
;
2094 struct btrfs_key key
;
2095 struct btrfs_root
*root
;
2096 struct sa_defrag_extent_backref
*backref
;
2097 struct extent_buffer
*leaf
;
2098 struct inode
*inode
= new->inode
;
2104 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2105 inum
== btrfs_ino(inode
))
2108 key
.objectid
= root_id
;
2109 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2110 key
.offset
= (u64
)-1;
2112 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2113 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2115 if (PTR_ERR(root
) == -ENOENT
)
2118 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2119 inum
, offset
, root_id
);
2120 return PTR_ERR(root
);
2123 key
.objectid
= inum
;
2124 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2125 if (offset
> (u64
)-1 << 32)
2128 key
.offset
= offset
;
2130 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2139 leaf
= path
->nodes
[0];
2140 slot
= path
->slots
[0];
2142 if (slot
>= btrfs_header_nritems(leaf
)) {
2143 ret
= btrfs_next_leaf(root
, path
);
2146 } else if (ret
> 0) {
2155 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2157 if (key
.objectid
> inum
)
2160 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2163 extent
= btrfs_item_ptr(leaf
, slot
,
2164 struct btrfs_file_extent_item
);
2166 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2170 * 'offset' refers to the exact key.offset,
2171 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2172 * (key.offset - extent_offset).
2174 if (key
.offset
!= offset
)
2177 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2178 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2180 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2181 old
->len
|| extent_offset
+ num_bytes
<=
2182 old
->extent_offset
+ old
->offset
)
2189 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2195 backref
->root_id
= root_id
;
2196 backref
->inum
= inum
;
2197 backref
->file_pos
= offset
;
2198 backref
->num_bytes
= num_bytes
;
2199 backref
->extent_offset
= extent_offset
;
2200 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2202 backref_insert(&new->root
, backref
);
2205 btrfs_release_path(path
);
2210 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2211 struct new_sa_defrag_extent
*new)
2213 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2214 struct old_sa_defrag_extent
*old
, *tmp
;
2219 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2220 ret
= iterate_inodes_from_logical(old
->bytenr
+
2221 old
->extent_offset
, fs_info
,
2222 path
, record_one_backref
,
2224 BUG_ON(ret
< 0 && ret
!= -ENOENT
);
2226 /* no backref to be processed for this extent */
2228 list_del(&old
->list
);
2233 if (list_empty(&new->head
))
2239 static int relink_is_mergable(struct extent_buffer
*leaf
,
2240 struct btrfs_file_extent_item
*fi
,
2243 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != disk_bytenr
)
2246 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2249 if (btrfs_file_extent_compression(leaf
, fi
) ||
2250 btrfs_file_extent_encryption(leaf
, fi
) ||
2251 btrfs_file_extent_other_encoding(leaf
, fi
))
2258 * Note the backref might has changed, and in this case we just return 0.
2260 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2261 struct sa_defrag_extent_backref
*prev
,
2262 struct sa_defrag_extent_backref
*backref
)
2264 struct btrfs_file_extent_item
*extent
;
2265 struct btrfs_file_extent_item
*item
;
2266 struct btrfs_ordered_extent
*ordered
;
2267 struct btrfs_trans_handle
*trans
;
2268 struct btrfs_fs_info
*fs_info
;
2269 struct btrfs_root
*root
;
2270 struct btrfs_key key
;
2271 struct extent_buffer
*leaf
;
2272 struct old_sa_defrag_extent
*old
= backref
->old
;
2273 struct new_sa_defrag_extent
*new = old
->new;
2274 struct inode
*src_inode
= new->inode
;
2275 struct inode
*inode
;
2276 struct extent_state
*cached
= NULL
;
2285 if (prev
&& prev
->root_id
== backref
->root_id
&&
2286 prev
->inum
== backref
->inum
&&
2287 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2290 /* step 1: get root */
2291 key
.objectid
= backref
->root_id
;
2292 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2293 key
.offset
= (u64
)-1;
2295 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2296 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2298 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2300 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2301 if (PTR_ERR(root
) == -ENOENT
)
2303 return PTR_ERR(root
);
2306 /* step 2: get inode */
2307 key
.objectid
= backref
->inum
;
2308 key
.type
= BTRFS_INODE_ITEM_KEY
;
2311 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2312 if (IS_ERR(inode
)) {
2313 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2317 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2319 /* step 3: relink backref */
2320 lock_start
= backref
->file_pos
;
2321 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2322 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2325 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2327 btrfs_put_ordered_extent(ordered
);
2331 trans
= btrfs_join_transaction(root
);
2332 if (IS_ERR(trans
)) {
2333 ret
= PTR_ERR(trans
);
2337 key
.objectid
= backref
->inum
;
2338 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2339 key
.offset
= backref
->file_pos
;
2341 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2344 } else if (ret
> 0) {
2349 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2350 struct btrfs_file_extent_item
);
2352 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2353 backref
->generation
)
2356 btrfs_release_path(path
);
2358 start
= backref
->file_pos
;
2359 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2360 start
+= old
->extent_offset
+ old
->offset
-
2361 backref
->extent_offset
;
2363 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2364 old
->extent_offset
+ old
->offset
+ old
->len
);
2365 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2367 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2372 key
.objectid
= btrfs_ino(inode
);
2373 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2376 path
->leave_spinning
= 1;
2378 struct btrfs_file_extent_item
*fi
;
2380 struct btrfs_key found_key
;
2382 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
2387 leaf
= path
->nodes
[0];
2388 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2390 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2391 struct btrfs_file_extent_item
);
2392 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2394 if (relink_is_mergable(leaf
, fi
, new->bytenr
) &&
2395 extent_len
+ found_key
.offset
== start
) {
2396 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2398 btrfs_mark_buffer_dirty(leaf
);
2399 inode_add_bytes(inode
, len
);
2405 btrfs_release_path(path
);
2410 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2413 btrfs_abort_transaction(trans
, root
, ret
);
2417 leaf
= path
->nodes
[0];
2418 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2419 struct btrfs_file_extent_item
);
2420 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2421 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2422 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2423 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2424 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2425 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2426 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2427 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2428 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2429 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2431 btrfs_mark_buffer_dirty(leaf
);
2432 inode_add_bytes(inode
, len
);
2433 btrfs_release_path(path
);
2435 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2437 backref
->root_id
, backref
->inum
,
2438 new->file_pos
, 0); /* start - extent_offset */
2440 btrfs_abort_transaction(trans
, root
, ret
);
2446 btrfs_release_path(path
);
2447 path
->leave_spinning
= 0;
2448 btrfs_end_transaction(trans
, root
);
2450 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2456 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2458 struct btrfs_path
*path
;
2459 struct old_sa_defrag_extent
*old
, *tmp
;
2460 struct sa_defrag_extent_backref
*backref
;
2461 struct sa_defrag_extent_backref
*prev
= NULL
;
2462 struct inode
*inode
;
2463 struct btrfs_root
*root
;
2464 struct rb_node
*node
;
2468 root
= BTRFS_I(inode
)->root
;
2470 path
= btrfs_alloc_path();
2474 if (!record_extent_backrefs(path
, new)) {
2475 btrfs_free_path(path
);
2478 btrfs_release_path(path
);
2481 node
= rb_first(&new->root
);
2484 rb_erase(node
, &new->root
);
2486 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2488 ret
= relink_extent_backref(path
, prev
, backref
);
2501 btrfs_free_path(path
);
2503 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2504 list_del(&old
->list
);
2508 atomic_dec(&root
->fs_info
->defrag_running
);
2509 wake_up(&root
->fs_info
->transaction_wait
);
2514 static struct new_sa_defrag_extent
*
2515 record_old_file_extents(struct inode
*inode
,
2516 struct btrfs_ordered_extent
*ordered
)
2518 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2519 struct btrfs_path
*path
;
2520 struct btrfs_key key
;
2521 struct old_sa_defrag_extent
*old
, *tmp
;
2522 struct new_sa_defrag_extent
*new;
2525 new = kmalloc(sizeof(*new), GFP_NOFS
);
2530 new->file_pos
= ordered
->file_offset
;
2531 new->len
= ordered
->len
;
2532 new->bytenr
= ordered
->start
;
2533 new->disk_len
= ordered
->disk_len
;
2534 new->compress_type
= ordered
->compress_type
;
2535 new->root
= RB_ROOT
;
2536 INIT_LIST_HEAD(&new->head
);
2538 path
= btrfs_alloc_path();
2542 key
.objectid
= btrfs_ino(inode
);
2543 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2544 key
.offset
= new->file_pos
;
2546 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2549 if (ret
> 0 && path
->slots
[0] > 0)
2552 /* find out all the old extents for the file range */
2554 struct btrfs_file_extent_item
*extent
;
2555 struct extent_buffer
*l
;
2564 slot
= path
->slots
[0];
2566 if (slot
>= btrfs_header_nritems(l
)) {
2567 ret
= btrfs_next_leaf(root
, path
);
2575 btrfs_item_key_to_cpu(l
, &key
, slot
);
2577 if (key
.objectid
!= btrfs_ino(inode
))
2579 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2581 if (key
.offset
>= new->file_pos
+ new->len
)
2584 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2586 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2587 if (key
.offset
+ num_bytes
< new->file_pos
)
2590 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2594 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2596 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2600 offset
= max(new->file_pos
, key
.offset
);
2601 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2603 old
->bytenr
= disk_bytenr
;
2604 old
->extent_offset
= extent_offset
;
2605 old
->offset
= offset
- key
.offset
;
2606 old
->len
= end
- offset
;
2609 list_add_tail(&old
->list
, &new->head
);
2615 btrfs_free_path(path
);
2616 atomic_inc(&root
->fs_info
->defrag_running
);
2621 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2622 list_del(&old
->list
);
2626 btrfs_free_path(path
);
2633 * helper function for btrfs_finish_ordered_io, this
2634 * just reads in some of the csum leaves to prime them into ram
2635 * before we start the transaction. It limits the amount of btree
2636 * reads required while inside the transaction.
2638 /* as ordered data IO finishes, this gets called so we can finish
2639 * an ordered extent if the range of bytes in the file it covers are
2642 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2644 struct inode
*inode
= ordered_extent
->inode
;
2645 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2646 struct btrfs_trans_handle
*trans
= NULL
;
2647 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2648 struct extent_state
*cached_state
= NULL
;
2649 struct new_sa_defrag_extent
*new = NULL
;
2650 int compress_type
= 0;
2654 nolock
= btrfs_is_free_space_inode(inode
);
2656 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2661 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2662 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2663 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2665 trans
= btrfs_join_transaction_nolock(root
);
2667 trans
= btrfs_join_transaction(root
);
2668 if (IS_ERR(trans
)) {
2669 ret
= PTR_ERR(trans
);
2673 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2674 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2675 if (ret
) /* -ENOMEM or corruption */
2676 btrfs_abort_transaction(trans
, root
, ret
);
2680 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2681 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2684 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2685 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2686 EXTENT_DEFRAG
, 1, cached_state
);
2688 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2689 if (last_snapshot
>= BTRFS_I(inode
)->generation
)
2690 /* the inode is shared */
2691 new = record_old_file_extents(inode
, ordered_extent
);
2693 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2694 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2695 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2699 trans
= btrfs_join_transaction_nolock(root
);
2701 trans
= btrfs_join_transaction(root
);
2702 if (IS_ERR(trans
)) {
2703 ret
= PTR_ERR(trans
);
2707 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2709 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2710 compress_type
= ordered_extent
->compress_type
;
2711 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2712 BUG_ON(compress_type
);
2713 ret
= btrfs_mark_extent_written(trans
, inode
,
2714 ordered_extent
->file_offset
,
2715 ordered_extent
->file_offset
+
2716 ordered_extent
->len
);
2718 BUG_ON(root
== root
->fs_info
->tree_root
);
2719 ret
= insert_reserved_file_extent(trans
, inode
,
2720 ordered_extent
->file_offset
,
2721 ordered_extent
->start
,
2722 ordered_extent
->disk_len
,
2723 ordered_extent
->len
,
2724 ordered_extent
->len
,
2725 compress_type
, 0, 0,
2726 BTRFS_FILE_EXTENT_REG
);
2728 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2729 ordered_extent
->file_offset
, ordered_extent
->len
,
2732 btrfs_abort_transaction(trans
, root
, ret
);
2736 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2737 &ordered_extent
->list
);
2739 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2740 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2741 if (ret
) { /* -ENOMEM or corruption */
2742 btrfs_abort_transaction(trans
, root
, ret
);
2747 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2748 ordered_extent
->file_offset
+
2749 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2751 if (root
!= root
->fs_info
->tree_root
)
2752 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2754 btrfs_end_transaction(trans
, root
);
2757 clear_extent_uptodate(io_tree
, ordered_extent
->file_offset
,
2758 ordered_extent
->file_offset
+
2759 ordered_extent
->len
- 1, NULL
, GFP_NOFS
);
2762 * If the ordered extent had an IOERR or something else went
2763 * wrong we need to return the space for this ordered extent
2764 * back to the allocator.
2766 if (!test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2767 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2768 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2769 ordered_extent
->disk_len
);
2774 * This needs to be done to make sure anybody waiting knows we are done
2775 * updating everything for this ordered extent.
2777 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2779 /* for snapshot-aware defrag */
2781 relink_file_extents(new);
2784 btrfs_put_ordered_extent(ordered_extent
);
2785 /* once for the tree */
2786 btrfs_put_ordered_extent(ordered_extent
);
2791 static void finish_ordered_fn(struct btrfs_work
*work
)
2793 struct btrfs_ordered_extent
*ordered_extent
;
2794 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2795 btrfs_finish_ordered_io(ordered_extent
);
2798 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2799 struct extent_state
*state
, int uptodate
)
2801 struct inode
*inode
= page
->mapping
->host
;
2802 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2803 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2804 struct btrfs_workers
*workers
;
2806 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2808 ClearPagePrivate2(page
);
2809 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2810 end
- start
+ 1, uptodate
))
2813 ordered_extent
->work
.func
= finish_ordered_fn
;
2814 ordered_extent
->work
.flags
= 0;
2816 if (btrfs_is_free_space_inode(inode
))
2817 workers
= &root
->fs_info
->endio_freespace_worker
;
2819 workers
= &root
->fs_info
->endio_write_workers
;
2820 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2826 * when reads are done, we need to check csums to verify the data is correct
2827 * if there's a match, we allow the bio to finish. If not, the code in
2828 * extent_io.c will try to find good copies for us.
2830 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2831 struct extent_state
*state
, int mirror
)
2833 size_t offset
= start
- page_offset(page
);
2834 struct inode
*inode
= page
->mapping
->host
;
2835 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2837 u64
private = ~(u32
)0;
2839 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2841 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
2842 DEFAULT_RATELIMIT_BURST
);
2844 if (PageChecked(page
)) {
2845 ClearPageChecked(page
);
2849 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2852 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2853 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2854 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2859 if (state
&& state
->start
== start
) {
2860 private = state
->private;
2863 ret
= get_state_private(io_tree
, start
, &private);
2865 kaddr
= kmap_atomic(page
);
2869 csum
= btrfs_csum_data(kaddr
+ offset
, csum
, end
- start
+ 1);
2870 btrfs_csum_final(csum
, (char *)&csum
);
2871 if (csum
!= private)
2874 kunmap_atomic(kaddr
);
2879 if (__ratelimit(&_rs
))
2880 btrfs_info(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %llu",
2881 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2882 (unsigned long long)start
, csum
,
2883 (unsigned long long)private);
2884 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2885 flush_dcache_page(page
);
2886 kunmap_atomic(kaddr
);
2892 struct delayed_iput
{
2893 struct list_head list
;
2894 struct inode
*inode
;
2897 /* JDM: If this is fs-wide, why can't we add a pointer to
2898 * btrfs_inode instead and avoid the allocation? */
2899 void btrfs_add_delayed_iput(struct inode
*inode
)
2901 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2902 struct delayed_iput
*delayed
;
2904 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2907 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2908 delayed
->inode
= inode
;
2910 spin_lock(&fs_info
->delayed_iput_lock
);
2911 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2912 spin_unlock(&fs_info
->delayed_iput_lock
);
2915 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2918 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2919 struct delayed_iput
*delayed
;
2922 spin_lock(&fs_info
->delayed_iput_lock
);
2923 empty
= list_empty(&fs_info
->delayed_iputs
);
2924 spin_unlock(&fs_info
->delayed_iput_lock
);
2928 spin_lock(&fs_info
->delayed_iput_lock
);
2929 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2930 spin_unlock(&fs_info
->delayed_iput_lock
);
2932 while (!list_empty(&list
)) {
2933 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2934 list_del(&delayed
->list
);
2935 iput(delayed
->inode
);
2941 * This is called in transaction commit time. If there are no orphan
2942 * files in the subvolume, it removes orphan item and frees block_rsv
2945 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2946 struct btrfs_root
*root
)
2948 struct btrfs_block_rsv
*block_rsv
;
2951 if (atomic_read(&root
->orphan_inodes
) ||
2952 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2955 spin_lock(&root
->orphan_lock
);
2956 if (atomic_read(&root
->orphan_inodes
)) {
2957 spin_unlock(&root
->orphan_lock
);
2961 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2962 spin_unlock(&root
->orphan_lock
);
2966 block_rsv
= root
->orphan_block_rsv
;
2967 root
->orphan_block_rsv
= NULL
;
2968 spin_unlock(&root
->orphan_lock
);
2970 if (root
->orphan_item_inserted
&&
2971 btrfs_root_refs(&root
->root_item
) > 0) {
2972 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2973 root
->root_key
.objectid
);
2975 root
->orphan_item_inserted
= 0;
2979 WARN_ON(block_rsv
->size
> 0);
2980 btrfs_free_block_rsv(root
, block_rsv
);
2985 * This creates an orphan entry for the given inode in case something goes
2986 * wrong in the middle of an unlink/truncate.
2988 * NOTE: caller of this function should reserve 5 units of metadata for
2991 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2993 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2994 struct btrfs_block_rsv
*block_rsv
= NULL
;
2999 if (!root
->orphan_block_rsv
) {
3000 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
3005 spin_lock(&root
->orphan_lock
);
3006 if (!root
->orphan_block_rsv
) {
3007 root
->orphan_block_rsv
= block_rsv
;
3008 } else if (block_rsv
) {
3009 btrfs_free_block_rsv(root
, block_rsv
);
3013 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3014 &BTRFS_I(inode
)->runtime_flags
)) {
3017 * For proper ENOSPC handling, we should do orphan
3018 * cleanup when mounting. But this introduces backward
3019 * compatibility issue.
3021 if (!xchg(&root
->orphan_item_inserted
, 1))
3027 atomic_inc(&root
->orphan_inodes
);
3030 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3031 &BTRFS_I(inode
)->runtime_flags
))
3033 spin_unlock(&root
->orphan_lock
);
3035 /* grab metadata reservation from transaction handle */
3037 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
3038 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
3041 /* insert an orphan item to track this unlinked/truncated file */
3043 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
3044 if (ret
&& ret
!= -EEXIST
) {
3045 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3046 &BTRFS_I(inode
)->runtime_flags
);
3047 btrfs_abort_transaction(trans
, root
, ret
);
3053 /* insert an orphan item to track subvolume contains orphan files */
3055 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
3056 root
->root_key
.objectid
);
3057 if (ret
&& ret
!= -EEXIST
) {
3058 btrfs_abort_transaction(trans
, root
, ret
);
3066 * We have done the truncate/delete so we can go ahead and remove the orphan
3067 * item for this particular inode.
3069 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3070 struct inode
*inode
)
3072 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3073 int delete_item
= 0;
3074 int release_rsv
= 0;
3077 spin_lock(&root
->orphan_lock
);
3078 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3079 &BTRFS_I(inode
)->runtime_flags
))
3082 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3083 &BTRFS_I(inode
)->runtime_flags
))
3085 spin_unlock(&root
->orphan_lock
);
3087 if (trans
&& delete_item
) {
3088 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
3089 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3093 btrfs_orphan_release_metadata(inode
);
3094 atomic_dec(&root
->orphan_inodes
);
3101 * this cleans up any orphans that may be left on the list from the last use
3104 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3106 struct btrfs_path
*path
;
3107 struct extent_buffer
*leaf
;
3108 struct btrfs_key key
, found_key
;
3109 struct btrfs_trans_handle
*trans
;
3110 struct inode
*inode
;
3111 u64 last_objectid
= 0;
3112 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3114 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3117 path
= btrfs_alloc_path();
3124 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3125 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
3126 key
.offset
= (u64
)-1;
3129 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3134 * if ret == 0 means we found what we were searching for, which
3135 * is weird, but possible, so only screw with path if we didn't
3136 * find the key and see if we have stuff that matches
3140 if (path
->slots
[0] == 0)
3145 /* pull out the item */
3146 leaf
= path
->nodes
[0];
3147 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3149 /* make sure the item matches what we want */
3150 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3152 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
3155 /* release the path since we're done with it */
3156 btrfs_release_path(path
);
3159 * this is where we are basically btrfs_lookup, without the
3160 * crossing root thing. we store the inode number in the
3161 * offset of the orphan item.
3164 if (found_key
.offset
== last_objectid
) {
3165 btrfs_err(root
->fs_info
,
3166 "Error removing orphan entry, stopping orphan cleanup");
3171 last_objectid
= found_key
.offset
;
3173 found_key
.objectid
= found_key
.offset
;
3174 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3175 found_key
.offset
= 0;
3176 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3177 ret
= PTR_ERR_OR_ZERO(inode
);
3178 if (ret
&& ret
!= -ESTALE
)
3181 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3182 struct btrfs_root
*dead_root
;
3183 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3184 int is_dead_root
= 0;
3187 * this is an orphan in the tree root. Currently these
3188 * could come from 2 sources:
3189 * a) a snapshot deletion in progress
3190 * b) a free space cache inode
3191 * We need to distinguish those two, as the snapshot
3192 * orphan must not get deleted.
3193 * find_dead_roots already ran before us, so if this
3194 * is a snapshot deletion, we should find the root
3195 * in the dead_roots list
3197 spin_lock(&fs_info
->trans_lock
);
3198 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3200 if (dead_root
->root_key
.objectid
==
3201 found_key
.objectid
) {
3206 spin_unlock(&fs_info
->trans_lock
);
3208 /* prevent this orphan from being found again */
3209 key
.offset
= found_key
.objectid
- 1;
3214 * Inode is already gone but the orphan item is still there,
3215 * kill the orphan item.
3217 if (ret
== -ESTALE
) {
3218 trans
= btrfs_start_transaction(root
, 1);
3219 if (IS_ERR(trans
)) {
3220 ret
= PTR_ERR(trans
);
3223 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3224 found_key
.objectid
);
3225 ret
= btrfs_del_orphan_item(trans
, root
,
3226 found_key
.objectid
);
3227 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3228 btrfs_end_transaction(trans
, root
);
3233 * add this inode to the orphan list so btrfs_orphan_del does
3234 * the proper thing when we hit it
3236 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3237 &BTRFS_I(inode
)->runtime_flags
);
3238 atomic_inc(&root
->orphan_inodes
);
3240 /* if we have links, this was a truncate, lets do that */
3241 if (inode
->i_nlink
) {
3242 if (!S_ISREG(inode
->i_mode
)) {
3249 /* 1 for the orphan item deletion. */
3250 trans
= btrfs_start_transaction(root
, 1);
3251 if (IS_ERR(trans
)) {
3253 ret
= PTR_ERR(trans
);
3256 ret
= btrfs_orphan_add(trans
, inode
);
3257 btrfs_end_transaction(trans
, root
);
3263 ret
= btrfs_truncate(inode
);
3265 btrfs_orphan_del(NULL
, inode
);
3270 /* this will do delete_inode and everything for us */
3275 /* release the path since we're done with it */
3276 btrfs_release_path(path
);
3278 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3280 if (root
->orphan_block_rsv
)
3281 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3284 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
3285 trans
= btrfs_join_transaction(root
);
3287 btrfs_end_transaction(trans
, root
);
3291 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3293 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3297 btrfs_crit(root
->fs_info
,
3298 "could not do orphan cleanup %d", ret
);
3299 btrfs_free_path(path
);
3304 * very simple check to peek ahead in the leaf looking for xattrs. If we
3305 * don't find any xattrs, we know there can't be any acls.
3307 * slot is the slot the inode is in, objectid is the objectid of the inode
3309 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3310 int slot
, u64 objectid
)
3312 u32 nritems
= btrfs_header_nritems(leaf
);
3313 struct btrfs_key found_key
;
3314 static u64 xattr_access
= 0;
3315 static u64 xattr_default
= 0;
3318 if (!xattr_access
) {
3319 xattr_access
= btrfs_name_hash(POSIX_ACL_XATTR_ACCESS
,
3320 strlen(POSIX_ACL_XATTR_ACCESS
));
3321 xattr_default
= btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT
,
3322 strlen(POSIX_ACL_XATTR_DEFAULT
));
3326 while (slot
< nritems
) {
3327 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3329 /* we found a different objectid, there must not be acls */
3330 if (found_key
.objectid
!= objectid
)
3333 /* we found an xattr, assume we've got an acl */
3334 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
) {
3335 if (found_key
.offset
== xattr_access
||
3336 found_key
.offset
== xattr_default
)
3341 * we found a key greater than an xattr key, there can't
3342 * be any acls later on
3344 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3351 * it goes inode, inode backrefs, xattrs, extents,
3352 * so if there are a ton of hard links to an inode there can
3353 * be a lot of backrefs. Don't waste time searching too hard,
3354 * this is just an optimization
3359 /* we hit the end of the leaf before we found an xattr or
3360 * something larger than an xattr. We have to assume the inode
3367 * read an inode from the btree into the in-memory inode
3369 static void btrfs_read_locked_inode(struct inode
*inode
)
3371 struct btrfs_path
*path
;
3372 struct extent_buffer
*leaf
;
3373 struct btrfs_inode_item
*inode_item
;
3374 struct btrfs_timespec
*tspec
;
3375 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3376 struct btrfs_key location
;
3380 bool filled
= false;
3382 ret
= btrfs_fill_inode(inode
, &rdev
);
3386 path
= btrfs_alloc_path();
3390 path
->leave_spinning
= 1;
3391 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3393 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3397 leaf
= path
->nodes
[0];
3402 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3403 struct btrfs_inode_item
);
3404 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3405 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3406 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3407 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3408 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3410 tspec
= btrfs_inode_atime(inode_item
);
3411 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3412 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3414 tspec
= btrfs_inode_mtime(inode_item
);
3415 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3416 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3418 tspec
= btrfs_inode_ctime(inode_item
);
3419 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3420 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3422 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3423 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3424 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3427 * If we were modified in the current generation and evicted from memory
3428 * and then re-read we need to do a full sync since we don't have any
3429 * idea about which extents were modified before we were evicted from
3432 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3433 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3434 &BTRFS_I(inode
)->runtime_flags
);
3436 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3437 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3439 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3441 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3442 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3445 * try to precache a NULL acl entry for files that don't have
3446 * any xattrs or acls
3448 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3451 cache_no_acl(inode
);
3453 btrfs_free_path(path
);
3455 switch (inode
->i_mode
& S_IFMT
) {
3457 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3458 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3459 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3460 inode
->i_fop
= &btrfs_file_operations
;
3461 inode
->i_op
= &btrfs_file_inode_operations
;
3464 inode
->i_fop
= &btrfs_dir_file_operations
;
3465 if (root
== root
->fs_info
->tree_root
)
3466 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3468 inode
->i_op
= &btrfs_dir_inode_operations
;
3471 inode
->i_op
= &btrfs_symlink_inode_operations
;
3472 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3473 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3476 inode
->i_op
= &btrfs_special_inode_operations
;
3477 init_special_inode(inode
, inode
->i_mode
, rdev
);
3481 btrfs_update_iflags(inode
);
3485 btrfs_free_path(path
);
3486 make_bad_inode(inode
);
3490 * given a leaf and an inode, copy the inode fields into the leaf
3492 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3493 struct extent_buffer
*leaf
,
3494 struct btrfs_inode_item
*item
,
3495 struct inode
*inode
)
3497 struct btrfs_map_token token
;
3499 btrfs_init_map_token(&token
);
3501 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3502 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3503 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3505 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3506 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3508 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
3509 inode
->i_atime
.tv_sec
, &token
);
3510 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
3511 inode
->i_atime
.tv_nsec
, &token
);
3513 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
3514 inode
->i_mtime
.tv_sec
, &token
);
3515 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3516 inode
->i_mtime
.tv_nsec
, &token
);
3518 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3519 inode
->i_ctime
.tv_sec
, &token
);
3520 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3521 inode
->i_ctime
.tv_nsec
, &token
);
3523 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3525 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3527 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3528 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3529 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3530 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3531 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3535 * copy everything in the in-memory inode into the btree.
3537 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3538 struct btrfs_root
*root
, struct inode
*inode
)
3540 struct btrfs_inode_item
*inode_item
;
3541 struct btrfs_path
*path
;
3542 struct extent_buffer
*leaf
;
3545 path
= btrfs_alloc_path();
3549 path
->leave_spinning
= 1;
3550 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3558 btrfs_unlock_up_safe(path
, 1);
3559 leaf
= path
->nodes
[0];
3560 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3561 struct btrfs_inode_item
);
3563 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3564 btrfs_mark_buffer_dirty(leaf
);
3565 btrfs_set_inode_last_trans(trans
, inode
);
3568 btrfs_free_path(path
);
3573 * copy everything in the in-memory inode into the btree.
3575 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3576 struct btrfs_root
*root
, struct inode
*inode
)
3581 * If the inode is a free space inode, we can deadlock during commit
3582 * if we put it into the delayed code.
3584 * The data relocation inode should also be directly updated
3587 if (!btrfs_is_free_space_inode(inode
)
3588 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
3589 btrfs_update_root_times(trans
, root
);
3591 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3593 btrfs_set_inode_last_trans(trans
, inode
);
3597 return btrfs_update_inode_item(trans
, root
, inode
);
3600 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3601 struct btrfs_root
*root
,
3602 struct inode
*inode
)
3606 ret
= btrfs_update_inode(trans
, root
, inode
);
3608 return btrfs_update_inode_item(trans
, root
, inode
);
3613 * unlink helper that gets used here in inode.c and in the tree logging
3614 * recovery code. It remove a link in a directory with a given name, and
3615 * also drops the back refs in the inode to the directory
3617 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3618 struct btrfs_root
*root
,
3619 struct inode
*dir
, struct inode
*inode
,
3620 const char *name
, int name_len
)
3622 struct btrfs_path
*path
;
3624 struct extent_buffer
*leaf
;
3625 struct btrfs_dir_item
*di
;
3626 struct btrfs_key key
;
3628 u64 ino
= btrfs_ino(inode
);
3629 u64 dir_ino
= btrfs_ino(dir
);
3631 path
= btrfs_alloc_path();
3637 path
->leave_spinning
= 1;
3638 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3639 name
, name_len
, -1);
3648 leaf
= path
->nodes
[0];
3649 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3650 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3653 btrfs_release_path(path
);
3655 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3658 btrfs_info(root
->fs_info
,
3659 "failed to delete reference to %.*s, inode %llu parent %llu",
3661 (unsigned long long)ino
, (unsigned long long)dir_ino
);
3662 btrfs_abort_transaction(trans
, root
, ret
);
3666 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3668 btrfs_abort_transaction(trans
, root
, ret
);
3672 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
3674 if (ret
!= 0 && ret
!= -ENOENT
) {
3675 btrfs_abort_transaction(trans
, root
, ret
);
3679 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
3684 btrfs_abort_transaction(trans
, root
, ret
);
3686 btrfs_free_path(path
);
3690 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3691 inode_inc_iversion(inode
);
3692 inode_inc_iversion(dir
);
3693 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3694 ret
= btrfs_update_inode(trans
, root
, dir
);
3699 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3700 struct btrfs_root
*root
,
3701 struct inode
*dir
, struct inode
*inode
,
3702 const char *name
, int name_len
)
3705 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3707 btrfs_drop_nlink(inode
);
3708 ret
= btrfs_update_inode(trans
, root
, inode
);
3714 * helper to start transaction for unlink and rmdir.
3716 * unlink and rmdir are special in btrfs, they do not always free space, so
3717 * if we cannot make our reservations the normal way try and see if there is
3718 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3719 * allow the unlink to occur.
3721 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
3723 struct btrfs_trans_handle
*trans
;
3724 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3728 * 1 for the possible orphan item
3729 * 1 for the dir item
3730 * 1 for the dir index
3731 * 1 for the inode ref
3734 trans
= btrfs_start_transaction(root
, 5);
3735 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3738 if (PTR_ERR(trans
) == -ENOSPC
) {
3739 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 5);
3741 trans
= btrfs_start_transaction(root
, 0);
3744 ret
= btrfs_cond_migrate_bytes(root
->fs_info
,
3745 &root
->fs_info
->trans_block_rsv
,
3748 btrfs_end_transaction(trans
, root
);
3749 return ERR_PTR(ret
);
3751 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3752 trans
->bytes_reserved
= num_bytes
;
3757 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3759 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3760 struct btrfs_trans_handle
*trans
;
3761 struct inode
*inode
= dentry
->d_inode
;
3764 trans
= __unlink_start_trans(dir
);
3766 return PTR_ERR(trans
);
3768 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3770 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3771 dentry
->d_name
.name
, dentry
->d_name
.len
);
3775 if (inode
->i_nlink
== 0) {
3776 ret
= btrfs_orphan_add(trans
, inode
);
3782 btrfs_end_transaction(trans
, root
);
3783 btrfs_btree_balance_dirty(root
);
3787 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3788 struct btrfs_root
*root
,
3789 struct inode
*dir
, u64 objectid
,
3790 const char *name
, int name_len
)
3792 struct btrfs_path
*path
;
3793 struct extent_buffer
*leaf
;
3794 struct btrfs_dir_item
*di
;
3795 struct btrfs_key key
;
3798 u64 dir_ino
= btrfs_ino(dir
);
3800 path
= btrfs_alloc_path();
3804 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3805 name
, name_len
, -1);
3806 if (IS_ERR_OR_NULL(di
)) {
3814 leaf
= path
->nodes
[0];
3815 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3816 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3817 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3819 btrfs_abort_transaction(trans
, root
, ret
);
3822 btrfs_release_path(path
);
3824 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3825 objectid
, root
->root_key
.objectid
,
3826 dir_ino
, &index
, name
, name_len
);
3828 if (ret
!= -ENOENT
) {
3829 btrfs_abort_transaction(trans
, root
, ret
);
3832 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3834 if (IS_ERR_OR_NULL(di
)) {
3839 btrfs_abort_transaction(trans
, root
, ret
);
3843 leaf
= path
->nodes
[0];
3844 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3845 btrfs_release_path(path
);
3848 btrfs_release_path(path
);
3850 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3852 btrfs_abort_transaction(trans
, root
, ret
);
3856 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3857 inode_inc_iversion(dir
);
3858 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3859 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3861 btrfs_abort_transaction(trans
, root
, ret
);
3863 btrfs_free_path(path
);
3867 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3869 struct inode
*inode
= dentry
->d_inode
;
3871 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3872 struct btrfs_trans_handle
*trans
;
3874 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3876 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3879 trans
= __unlink_start_trans(dir
);
3881 return PTR_ERR(trans
);
3883 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3884 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3885 BTRFS_I(inode
)->location
.objectid
,
3886 dentry
->d_name
.name
,
3887 dentry
->d_name
.len
);
3891 err
= btrfs_orphan_add(trans
, inode
);
3895 /* now the directory is empty */
3896 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3897 dentry
->d_name
.name
, dentry
->d_name
.len
);
3899 btrfs_i_size_write(inode
, 0);
3901 btrfs_end_transaction(trans
, root
);
3902 btrfs_btree_balance_dirty(root
);
3908 * this can truncate away extent items, csum items and directory items.
3909 * It starts at a high offset and removes keys until it can't find
3910 * any higher than new_size
3912 * csum items that cross the new i_size are truncated to the new size
3915 * min_type is the minimum key type to truncate down to. If set to 0, this
3916 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3918 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3919 struct btrfs_root
*root
,
3920 struct inode
*inode
,
3921 u64 new_size
, u32 min_type
)
3923 struct btrfs_path
*path
;
3924 struct extent_buffer
*leaf
;
3925 struct btrfs_file_extent_item
*fi
;
3926 struct btrfs_key key
;
3927 struct btrfs_key found_key
;
3928 u64 extent_start
= 0;
3929 u64 extent_num_bytes
= 0;
3930 u64 extent_offset
= 0;
3932 u32 found_type
= (u8
)-1;
3935 int pending_del_nr
= 0;
3936 int pending_del_slot
= 0;
3937 int extent_type
= -1;
3940 u64 ino
= btrfs_ino(inode
);
3942 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3944 path
= btrfs_alloc_path();
3950 * We want to drop from the next block forward in case this new size is
3951 * not block aligned since we will be keeping the last block of the
3952 * extent just the way it is.
3954 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3955 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
3956 root
->sectorsize
), (u64
)-1, 0);
3959 * This function is also used to drop the items in the log tree before
3960 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3961 * it is used to drop the loged items. So we shouldn't kill the delayed
3964 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
3965 btrfs_kill_delayed_inode_items(inode
);
3968 key
.offset
= (u64
)-1;
3972 path
->leave_spinning
= 1;
3973 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3980 /* there are no items in the tree for us to truncate, we're
3983 if (path
->slots
[0] == 0)
3990 leaf
= path
->nodes
[0];
3991 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3992 found_type
= btrfs_key_type(&found_key
);
3994 if (found_key
.objectid
!= ino
)
3997 if (found_type
< min_type
)
4000 item_end
= found_key
.offset
;
4001 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4002 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4003 struct btrfs_file_extent_item
);
4004 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4005 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4007 btrfs_file_extent_num_bytes(leaf
, fi
);
4008 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4009 item_end
+= btrfs_file_extent_inline_len(leaf
,
4014 if (found_type
> min_type
) {
4017 if (item_end
< new_size
)
4019 if (found_key
.offset
>= new_size
)
4025 /* FIXME, shrink the extent if the ref count is only 1 */
4026 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4029 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4031 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4033 u64 orig_num_bytes
=
4034 btrfs_file_extent_num_bytes(leaf
, fi
);
4035 extent_num_bytes
= ALIGN(new_size
-
4038 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4040 num_dec
= (orig_num_bytes
-
4042 if (root
->ref_cows
&& extent_start
!= 0)
4043 inode_sub_bytes(inode
, num_dec
);
4044 btrfs_mark_buffer_dirty(leaf
);
4047 btrfs_file_extent_disk_num_bytes(leaf
,
4049 extent_offset
= found_key
.offset
-
4050 btrfs_file_extent_offset(leaf
, fi
);
4052 /* FIXME blocksize != 4096 */
4053 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4054 if (extent_start
!= 0) {
4057 inode_sub_bytes(inode
, num_dec
);
4060 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4062 * we can't truncate inline items that have had
4066 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4067 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4068 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4069 u32 size
= new_size
- found_key
.offset
;
4071 if (root
->ref_cows
) {
4072 inode_sub_bytes(inode
, item_end
+ 1 -
4076 btrfs_file_extent_calc_inline_size(size
);
4077 btrfs_truncate_item(root
, path
, size
, 1);
4078 } else if (root
->ref_cows
) {
4079 inode_sub_bytes(inode
, item_end
+ 1 -
4085 if (!pending_del_nr
) {
4086 /* no pending yet, add ourselves */
4087 pending_del_slot
= path
->slots
[0];
4089 } else if (pending_del_nr
&&
4090 path
->slots
[0] + 1 == pending_del_slot
) {
4091 /* hop on the pending chunk */
4093 pending_del_slot
= path
->slots
[0];
4100 if (found_extent
&& (root
->ref_cows
||
4101 root
== root
->fs_info
->tree_root
)) {
4102 btrfs_set_path_blocking(path
);
4103 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4104 extent_num_bytes
, 0,
4105 btrfs_header_owner(leaf
),
4106 ino
, extent_offset
, 0);
4110 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4113 if (path
->slots
[0] == 0 ||
4114 path
->slots
[0] != pending_del_slot
) {
4115 if (pending_del_nr
) {
4116 ret
= btrfs_del_items(trans
, root
, path
,
4120 btrfs_abort_transaction(trans
,
4126 btrfs_release_path(path
);
4133 if (pending_del_nr
) {
4134 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4137 btrfs_abort_transaction(trans
, root
, ret
);
4140 btrfs_free_path(path
);
4145 * btrfs_truncate_page - read, zero a chunk and write a page
4146 * @inode - inode that we're zeroing
4147 * @from - the offset to start zeroing
4148 * @len - the length to zero, 0 to zero the entire range respective to the
4150 * @front - zero up to the offset instead of from the offset on
4152 * This will find the page for the "from" offset and cow the page and zero the
4153 * part we want to zero. This is used with truncate and hole punching.
4155 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4158 struct address_space
*mapping
= inode
->i_mapping
;
4159 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4160 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4161 struct btrfs_ordered_extent
*ordered
;
4162 struct extent_state
*cached_state
= NULL
;
4164 u32 blocksize
= root
->sectorsize
;
4165 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4166 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4168 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4173 if ((offset
& (blocksize
- 1)) == 0 &&
4174 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4176 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4181 page
= find_or_create_page(mapping
, index
, mask
);
4183 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4188 page_start
= page_offset(page
);
4189 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4191 if (!PageUptodate(page
)) {
4192 ret
= btrfs_readpage(NULL
, page
);
4194 if (page
->mapping
!= mapping
) {
4196 page_cache_release(page
);
4199 if (!PageUptodate(page
)) {
4204 wait_on_page_writeback(page
);
4206 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4207 set_page_extent_mapped(page
);
4209 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4211 unlock_extent_cached(io_tree
, page_start
, page_end
,
4212 &cached_state
, GFP_NOFS
);
4214 page_cache_release(page
);
4215 btrfs_start_ordered_extent(inode
, ordered
, 1);
4216 btrfs_put_ordered_extent(ordered
);
4220 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4221 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4222 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4223 0, 0, &cached_state
, GFP_NOFS
);
4225 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4228 unlock_extent_cached(io_tree
, page_start
, page_end
,
4229 &cached_state
, GFP_NOFS
);
4233 if (offset
!= PAGE_CACHE_SIZE
) {
4235 len
= PAGE_CACHE_SIZE
- offset
;
4238 memset(kaddr
, 0, offset
);
4240 memset(kaddr
+ offset
, 0, len
);
4241 flush_dcache_page(page
);
4244 ClearPageChecked(page
);
4245 set_page_dirty(page
);
4246 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4251 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4253 page_cache_release(page
);
4259 * This function puts in dummy file extents for the area we're creating a hole
4260 * for. So if we are truncating this file to a larger size we need to insert
4261 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4262 * the range between oldsize and size
4264 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4266 struct btrfs_trans_handle
*trans
;
4267 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4268 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4269 struct extent_map
*em
= NULL
;
4270 struct extent_state
*cached_state
= NULL
;
4271 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4272 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4273 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4280 * If our size started in the middle of a page we need to zero out the
4281 * rest of the page before we expand the i_size, otherwise we could
4282 * expose stale data.
4284 err
= btrfs_truncate_page(inode
, oldsize
, 0, 0);
4288 if (size
<= hole_start
)
4292 struct btrfs_ordered_extent
*ordered
;
4293 btrfs_wait_ordered_range(inode
, hole_start
,
4294 block_end
- hole_start
);
4295 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4297 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
4300 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4301 &cached_state
, GFP_NOFS
);
4302 btrfs_put_ordered_extent(ordered
);
4305 cur_offset
= hole_start
;
4307 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4308 block_end
- cur_offset
, 0);
4314 last_byte
= min(extent_map_end(em
), block_end
);
4315 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4316 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4317 struct extent_map
*hole_em
;
4318 hole_size
= last_byte
- cur_offset
;
4320 trans
= btrfs_start_transaction(root
, 3);
4321 if (IS_ERR(trans
)) {
4322 err
= PTR_ERR(trans
);
4326 err
= btrfs_drop_extents(trans
, root
, inode
,
4328 cur_offset
+ hole_size
, 1);
4330 btrfs_abort_transaction(trans
, root
, err
);
4331 btrfs_end_transaction(trans
, root
);
4335 err
= btrfs_insert_file_extent(trans
, root
,
4336 btrfs_ino(inode
), cur_offset
, 0,
4337 0, hole_size
, 0, hole_size
,
4340 btrfs_abort_transaction(trans
, root
, err
);
4341 btrfs_end_transaction(trans
, root
);
4345 btrfs_drop_extent_cache(inode
, cur_offset
,
4346 cur_offset
+ hole_size
- 1, 0);
4347 hole_em
= alloc_extent_map();
4349 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4350 &BTRFS_I(inode
)->runtime_flags
);
4353 hole_em
->start
= cur_offset
;
4354 hole_em
->len
= hole_size
;
4355 hole_em
->orig_start
= cur_offset
;
4357 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4358 hole_em
->block_len
= 0;
4359 hole_em
->orig_block_len
= 0;
4360 hole_em
->ram_bytes
= hole_size
;
4361 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4362 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4363 hole_em
->generation
= trans
->transid
;
4366 write_lock(&em_tree
->lock
);
4367 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4368 write_unlock(&em_tree
->lock
);
4371 btrfs_drop_extent_cache(inode
, cur_offset
,
4375 free_extent_map(hole_em
);
4377 btrfs_update_inode(trans
, root
, inode
);
4378 btrfs_end_transaction(trans
, root
);
4380 free_extent_map(em
);
4382 cur_offset
= last_byte
;
4383 if (cur_offset
>= block_end
)
4387 free_extent_map(em
);
4388 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4393 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4395 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4396 struct btrfs_trans_handle
*trans
;
4397 loff_t oldsize
= i_size_read(inode
);
4398 loff_t newsize
= attr
->ia_size
;
4399 int mask
= attr
->ia_valid
;
4403 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4404 * special case where we need to update the times despite not having
4405 * these flags set. For all other operations the VFS set these flags
4406 * explicitly if it wants a timestamp update.
4408 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
4409 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
4411 if (newsize
> oldsize
) {
4412 truncate_pagecache(inode
, oldsize
, newsize
);
4413 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4417 trans
= btrfs_start_transaction(root
, 1);
4419 return PTR_ERR(trans
);
4421 i_size_write(inode
, newsize
);
4422 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4423 ret
= btrfs_update_inode(trans
, root
, inode
);
4424 btrfs_end_transaction(trans
, root
);
4428 * We're truncating a file that used to have good data down to
4429 * zero. Make sure it gets into the ordered flush list so that
4430 * any new writes get down to disk quickly.
4433 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4434 &BTRFS_I(inode
)->runtime_flags
);
4437 * 1 for the orphan item we're going to add
4438 * 1 for the orphan item deletion.
4440 trans
= btrfs_start_transaction(root
, 2);
4442 return PTR_ERR(trans
);
4445 * We need to do this in case we fail at _any_ point during the
4446 * actual truncate. Once we do the truncate_setsize we could
4447 * invalidate pages which forces any outstanding ordered io to
4448 * be instantly completed which will give us extents that need
4449 * to be truncated. If we fail to get an orphan inode down we
4450 * could have left over extents that were never meant to live,
4451 * so we need to garuntee from this point on that everything
4452 * will be consistent.
4454 ret
= btrfs_orphan_add(trans
, inode
);
4455 btrfs_end_transaction(trans
, root
);
4459 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4460 truncate_setsize(inode
, newsize
);
4462 /* Disable nonlocked read DIO to avoid the end less truncate */
4463 btrfs_inode_block_unlocked_dio(inode
);
4464 inode_dio_wait(inode
);
4465 btrfs_inode_resume_unlocked_dio(inode
);
4467 ret
= btrfs_truncate(inode
);
4468 if (ret
&& inode
->i_nlink
)
4469 btrfs_orphan_del(NULL
, inode
);
4475 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4477 struct inode
*inode
= dentry
->d_inode
;
4478 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4481 if (btrfs_root_readonly(root
))
4484 err
= inode_change_ok(inode
, attr
);
4488 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4489 err
= btrfs_setsize(inode
, attr
);
4494 if (attr
->ia_valid
) {
4495 setattr_copy(inode
, attr
);
4496 inode_inc_iversion(inode
);
4497 err
= btrfs_dirty_inode(inode
);
4499 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4500 err
= btrfs_acl_chmod(inode
);
4506 void btrfs_evict_inode(struct inode
*inode
)
4508 struct btrfs_trans_handle
*trans
;
4509 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4510 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4511 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4514 trace_btrfs_inode_evict(inode
);
4516 truncate_inode_pages(&inode
->i_data
, 0);
4517 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
4518 btrfs_is_free_space_inode(inode
)))
4521 if (is_bad_inode(inode
)) {
4522 btrfs_orphan_del(NULL
, inode
);
4525 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4526 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4528 if (root
->fs_info
->log_root_recovering
) {
4529 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4530 &BTRFS_I(inode
)->runtime_flags
));
4534 if (inode
->i_nlink
> 0) {
4535 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
4539 ret
= btrfs_commit_inode_delayed_inode(inode
);
4541 btrfs_orphan_del(NULL
, inode
);
4545 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4547 btrfs_orphan_del(NULL
, inode
);
4550 rsv
->size
= min_size
;
4552 global_rsv
= &root
->fs_info
->global_block_rsv
;
4554 btrfs_i_size_write(inode
, 0);
4557 * This is a bit simpler than btrfs_truncate since we've already
4558 * reserved our space for our orphan item in the unlink, so we just
4559 * need to reserve some slack space in case we add bytes and update
4560 * inode item when doing the truncate.
4563 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4564 BTRFS_RESERVE_FLUSH_LIMIT
);
4567 * Try and steal from the global reserve since we will
4568 * likely not use this space anyway, we want to try as
4569 * hard as possible to get this to work.
4572 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4575 btrfs_warn(root
->fs_info
,
4576 "Could not get space for a delete, will truncate on mount %d",
4578 btrfs_orphan_del(NULL
, inode
);
4579 btrfs_free_block_rsv(root
, rsv
);
4583 trans
= btrfs_join_transaction(root
);
4584 if (IS_ERR(trans
)) {
4585 btrfs_orphan_del(NULL
, inode
);
4586 btrfs_free_block_rsv(root
, rsv
);
4590 trans
->block_rsv
= rsv
;
4592 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4596 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4597 btrfs_end_transaction(trans
, root
);
4599 btrfs_btree_balance_dirty(root
);
4602 btrfs_free_block_rsv(root
, rsv
);
4605 trans
->block_rsv
= root
->orphan_block_rsv
;
4606 ret
= btrfs_orphan_del(trans
, inode
);
4610 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4611 if (!(root
== root
->fs_info
->tree_root
||
4612 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4613 btrfs_return_ino(root
, btrfs_ino(inode
));
4615 btrfs_end_transaction(trans
, root
);
4616 btrfs_btree_balance_dirty(root
);
4618 btrfs_remove_delayed_node(inode
);
4624 * this returns the key found in the dir entry in the location pointer.
4625 * If no dir entries were found, location->objectid is 0.
4627 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4628 struct btrfs_key
*location
)
4630 const char *name
= dentry
->d_name
.name
;
4631 int namelen
= dentry
->d_name
.len
;
4632 struct btrfs_dir_item
*di
;
4633 struct btrfs_path
*path
;
4634 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4637 path
= btrfs_alloc_path();
4641 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4646 if (IS_ERR_OR_NULL(di
))
4649 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4651 btrfs_free_path(path
);
4654 location
->objectid
= 0;
4659 * when we hit a tree root in a directory, the btrfs part of the inode
4660 * needs to be changed to reflect the root directory of the tree root. This
4661 * is kind of like crossing a mount point.
4663 static int fixup_tree_root_location(struct btrfs_root
*root
,
4665 struct dentry
*dentry
,
4666 struct btrfs_key
*location
,
4667 struct btrfs_root
**sub_root
)
4669 struct btrfs_path
*path
;
4670 struct btrfs_root
*new_root
;
4671 struct btrfs_root_ref
*ref
;
4672 struct extent_buffer
*leaf
;
4676 path
= btrfs_alloc_path();
4683 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4684 BTRFS_I(dir
)->root
->root_key
.objectid
,
4685 location
->objectid
);
4692 leaf
= path
->nodes
[0];
4693 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4694 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4695 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4698 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4699 (unsigned long)(ref
+ 1),
4700 dentry
->d_name
.len
);
4704 btrfs_release_path(path
);
4706 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4707 if (IS_ERR(new_root
)) {
4708 err
= PTR_ERR(new_root
);
4712 *sub_root
= new_root
;
4713 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4714 location
->type
= BTRFS_INODE_ITEM_KEY
;
4715 location
->offset
= 0;
4718 btrfs_free_path(path
);
4722 static void inode_tree_add(struct inode
*inode
)
4724 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4725 struct btrfs_inode
*entry
;
4727 struct rb_node
*parent
;
4728 u64 ino
= btrfs_ino(inode
);
4730 if (inode_unhashed(inode
))
4734 spin_lock(&root
->inode_lock
);
4735 p
= &root
->inode_tree
.rb_node
;
4738 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4740 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4741 p
= &parent
->rb_left
;
4742 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4743 p
= &parent
->rb_right
;
4745 WARN_ON(!(entry
->vfs_inode
.i_state
&
4746 (I_WILL_FREE
| I_FREEING
)));
4747 rb_erase(parent
, &root
->inode_tree
);
4748 RB_CLEAR_NODE(parent
);
4749 spin_unlock(&root
->inode_lock
);
4753 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
4754 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4755 spin_unlock(&root
->inode_lock
);
4758 static void inode_tree_del(struct inode
*inode
)
4760 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4763 spin_lock(&root
->inode_lock
);
4764 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4765 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4766 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4767 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4769 spin_unlock(&root
->inode_lock
);
4772 * Free space cache has inodes in the tree root, but the tree root has a
4773 * root_refs of 0, so this could end up dropping the tree root as a
4774 * snapshot, so we need the extra !root->fs_info->tree_root check to
4775 * make sure we don't drop it.
4777 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
4778 root
!= root
->fs_info
->tree_root
) {
4779 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4780 spin_lock(&root
->inode_lock
);
4781 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4782 spin_unlock(&root
->inode_lock
);
4784 btrfs_add_dead_root(root
);
4788 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4790 struct rb_node
*node
;
4791 struct rb_node
*prev
;
4792 struct btrfs_inode
*entry
;
4793 struct inode
*inode
;
4796 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4798 spin_lock(&root
->inode_lock
);
4800 node
= root
->inode_tree
.rb_node
;
4804 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4806 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4807 node
= node
->rb_left
;
4808 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4809 node
= node
->rb_right
;
4815 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4816 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4820 prev
= rb_next(prev
);
4824 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4825 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4826 inode
= igrab(&entry
->vfs_inode
);
4828 spin_unlock(&root
->inode_lock
);
4829 if (atomic_read(&inode
->i_count
) > 1)
4830 d_prune_aliases(inode
);
4832 * btrfs_drop_inode will have it removed from
4833 * the inode cache when its usage count
4838 spin_lock(&root
->inode_lock
);
4842 if (cond_resched_lock(&root
->inode_lock
))
4845 node
= rb_next(node
);
4847 spin_unlock(&root
->inode_lock
);
4850 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4852 struct btrfs_iget_args
*args
= p
;
4853 inode
->i_ino
= args
->ino
;
4854 BTRFS_I(inode
)->root
= args
->root
;
4858 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4860 struct btrfs_iget_args
*args
= opaque
;
4861 return args
->ino
== btrfs_ino(inode
) &&
4862 args
->root
== BTRFS_I(inode
)->root
;
4865 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4867 struct btrfs_root
*root
)
4869 struct inode
*inode
;
4870 struct btrfs_iget_args args
;
4871 args
.ino
= objectid
;
4874 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4875 btrfs_init_locked_inode
,
4880 /* Get an inode object given its location and corresponding root.
4881 * Returns in *is_new if the inode was read from disk
4883 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4884 struct btrfs_root
*root
, int *new)
4886 struct inode
*inode
;
4888 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4890 return ERR_PTR(-ENOMEM
);
4892 if (inode
->i_state
& I_NEW
) {
4893 BTRFS_I(inode
)->root
= root
;
4894 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4895 btrfs_read_locked_inode(inode
);
4896 if (!is_bad_inode(inode
)) {
4897 inode_tree_add(inode
);
4898 unlock_new_inode(inode
);
4902 unlock_new_inode(inode
);
4904 inode
= ERR_PTR(-ESTALE
);
4911 static struct inode
*new_simple_dir(struct super_block
*s
,
4912 struct btrfs_key
*key
,
4913 struct btrfs_root
*root
)
4915 struct inode
*inode
= new_inode(s
);
4918 return ERR_PTR(-ENOMEM
);
4920 BTRFS_I(inode
)->root
= root
;
4921 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4922 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
4924 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4925 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
4926 inode
->i_fop
= &simple_dir_operations
;
4927 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4928 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4933 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4935 struct inode
*inode
;
4936 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4937 struct btrfs_root
*sub_root
= root
;
4938 struct btrfs_key location
;
4942 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4943 return ERR_PTR(-ENAMETOOLONG
);
4945 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4947 return ERR_PTR(ret
);
4949 if (location
.objectid
== 0)
4952 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4953 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4957 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4959 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4960 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4961 &location
, &sub_root
);
4964 inode
= ERR_PTR(ret
);
4966 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4968 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4970 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4972 if (!IS_ERR(inode
) && root
!= sub_root
) {
4973 down_read(&root
->fs_info
->cleanup_work_sem
);
4974 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4975 ret
= btrfs_orphan_cleanup(sub_root
);
4976 up_read(&root
->fs_info
->cleanup_work_sem
);
4979 inode
= ERR_PTR(ret
);
4986 static int btrfs_dentry_delete(const struct dentry
*dentry
)
4988 struct btrfs_root
*root
;
4989 struct inode
*inode
= dentry
->d_inode
;
4991 if (!inode
&& !IS_ROOT(dentry
))
4992 inode
= dentry
->d_parent
->d_inode
;
4995 root
= BTRFS_I(inode
)->root
;
4996 if (btrfs_root_refs(&root
->root_item
) == 0)
4999 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5005 static void btrfs_dentry_release(struct dentry
*dentry
)
5007 if (dentry
->d_fsdata
)
5008 kfree(dentry
->d_fsdata
);
5011 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5016 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
5020 unsigned char btrfs_filetype_table
[] = {
5021 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5024 static int btrfs_real_readdir(struct file
*file
, struct dir_context
*ctx
)
5026 struct inode
*inode
= file_inode(file
);
5027 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5028 struct btrfs_item
*item
;
5029 struct btrfs_dir_item
*di
;
5030 struct btrfs_key key
;
5031 struct btrfs_key found_key
;
5032 struct btrfs_path
*path
;
5033 struct list_head ins_list
;
5034 struct list_head del_list
;
5036 struct extent_buffer
*leaf
;
5038 unsigned char d_type
;
5043 int key_type
= BTRFS_DIR_INDEX_KEY
;
5047 int is_curr
= 0; /* ctx->pos points to the current index? */
5049 /* FIXME, use a real flag for deciding about the key type */
5050 if (root
->fs_info
->tree_root
== root
)
5051 key_type
= BTRFS_DIR_ITEM_KEY
;
5053 if (!dir_emit_dots(file
, ctx
))
5056 path
= btrfs_alloc_path();
5062 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5063 INIT_LIST_HEAD(&ins_list
);
5064 INIT_LIST_HEAD(&del_list
);
5065 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5068 btrfs_set_key_type(&key
, key_type
);
5069 key
.offset
= ctx
->pos
;
5070 key
.objectid
= btrfs_ino(inode
);
5072 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5077 leaf
= path
->nodes
[0];
5078 slot
= path
->slots
[0];
5079 if (slot
>= btrfs_header_nritems(leaf
)) {
5080 ret
= btrfs_next_leaf(root
, path
);
5088 item
= btrfs_item_nr(leaf
, slot
);
5089 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5091 if (found_key
.objectid
!= key
.objectid
)
5093 if (btrfs_key_type(&found_key
) != key_type
)
5095 if (found_key
.offset
< ctx
->pos
)
5097 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5098 btrfs_should_delete_dir_index(&del_list
,
5102 ctx
->pos
= found_key
.offset
;
5105 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5107 di_total
= btrfs_item_size(leaf
, item
);
5109 while (di_cur
< di_total
) {
5110 struct btrfs_key location
;
5112 if (verify_dir_item(root
, leaf
, di
))
5115 name_len
= btrfs_dir_name_len(leaf
, di
);
5116 if (name_len
<= sizeof(tmp_name
)) {
5117 name_ptr
= tmp_name
;
5119 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5125 read_extent_buffer(leaf
, name_ptr
,
5126 (unsigned long)(di
+ 1), name_len
);
5128 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5129 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5132 /* is this a reference to our own snapshot? If so
5135 * In contrast to old kernels, we insert the snapshot's
5136 * dir item and dir index after it has been created, so
5137 * we won't find a reference to our own snapshot. We
5138 * still keep the following code for backward
5141 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5142 location
.objectid
== root
->root_key
.objectid
) {
5146 over
= !dir_emit(ctx
, name_ptr
, name_len
,
5147 location
.objectid
, d_type
);
5150 if (name_ptr
!= tmp_name
)
5155 di_len
= btrfs_dir_name_len(leaf
, di
) +
5156 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5158 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5164 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5167 ret
= btrfs_readdir_delayed_dir_index(ctx
, &ins_list
);
5172 /* Reached end of directory/root. Bump pos past the last item. */
5176 * Stop new entries from being returned after we return the last
5179 * New directory entries are assigned a strictly increasing
5180 * offset. This means that new entries created during readdir
5181 * are *guaranteed* to be seen in the future by that readdir.
5182 * This has broken buggy programs which operate on names as
5183 * they're returned by readdir. Until we re-use freed offsets
5184 * we have this hack to stop new entries from being returned
5185 * under the assumption that they'll never reach this huge
5188 * This is being careful not to overflow 32bit loff_t unless the
5189 * last entry requires it because doing so has broken 32bit apps
5192 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5193 if (ctx
->pos
>= INT_MAX
)
5194 ctx
->pos
= LLONG_MAX
;
5201 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5202 btrfs_put_delayed_items(&ins_list
, &del_list
);
5203 btrfs_free_path(path
);
5207 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5209 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5210 struct btrfs_trans_handle
*trans
;
5212 bool nolock
= false;
5214 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5217 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5220 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5222 trans
= btrfs_join_transaction_nolock(root
);
5224 trans
= btrfs_join_transaction(root
);
5226 return PTR_ERR(trans
);
5227 ret
= btrfs_commit_transaction(trans
, root
);
5233 * This is somewhat expensive, updating the tree every time the
5234 * inode changes. But, it is most likely to find the inode in cache.
5235 * FIXME, needs more benchmarking...there are no reasons other than performance
5236 * to keep or drop this code.
5238 static int btrfs_dirty_inode(struct inode
*inode
)
5240 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5241 struct btrfs_trans_handle
*trans
;
5244 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5247 trans
= btrfs_join_transaction(root
);
5249 return PTR_ERR(trans
);
5251 ret
= btrfs_update_inode(trans
, root
, inode
);
5252 if (ret
&& ret
== -ENOSPC
) {
5253 /* whoops, lets try again with the full transaction */
5254 btrfs_end_transaction(trans
, root
);
5255 trans
= btrfs_start_transaction(root
, 1);
5257 return PTR_ERR(trans
);
5259 ret
= btrfs_update_inode(trans
, root
, inode
);
5261 btrfs_end_transaction(trans
, root
);
5262 if (BTRFS_I(inode
)->delayed_node
)
5263 btrfs_balance_delayed_items(root
);
5269 * This is a copy of file_update_time. We need this so we can return error on
5270 * ENOSPC for updating the inode in the case of file write and mmap writes.
5272 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5275 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5277 if (btrfs_root_readonly(root
))
5280 if (flags
& S_VERSION
)
5281 inode_inc_iversion(inode
);
5282 if (flags
& S_CTIME
)
5283 inode
->i_ctime
= *now
;
5284 if (flags
& S_MTIME
)
5285 inode
->i_mtime
= *now
;
5286 if (flags
& S_ATIME
)
5287 inode
->i_atime
= *now
;
5288 return btrfs_dirty_inode(inode
);
5292 * find the highest existing sequence number in a directory
5293 * and then set the in-memory index_cnt variable to reflect
5294 * free sequence numbers
5296 static int btrfs_set_inode_index_count(struct inode
*inode
)
5298 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5299 struct btrfs_key key
, found_key
;
5300 struct btrfs_path
*path
;
5301 struct extent_buffer
*leaf
;
5304 key
.objectid
= btrfs_ino(inode
);
5305 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
5306 key
.offset
= (u64
)-1;
5308 path
= btrfs_alloc_path();
5312 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5315 /* FIXME: we should be able to handle this */
5321 * MAGIC NUMBER EXPLANATION:
5322 * since we search a directory based on f_pos we have to start at 2
5323 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5324 * else has to start at 2
5326 if (path
->slots
[0] == 0) {
5327 BTRFS_I(inode
)->index_cnt
= 2;
5333 leaf
= path
->nodes
[0];
5334 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5336 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5337 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
5338 BTRFS_I(inode
)->index_cnt
= 2;
5342 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5344 btrfs_free_path(path
);
5349 * helper to find a free sequence number in a given directory. This current
5350 * code is very simple, later versions will do smarter things in the btree
5352 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5356 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5357 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5359 ret
= btrfs_set_inode_index_count(dir
);
5365 *index
= BTRFS_I(dir
)->index_cnt
;
5366 BTRFS_I(dir
)->index_cnt
++;
5371 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5372 struct btrfs_root
*root
,
5374 const char *name
, int name_len
,
5375 u64 ref_objectid
, u64 objectid
,
5376 umode_t mode
, u64
*index
)
5378 struct inode
*inode
;
5379 struct btrfs_inode_item
*inode_item
;
5380 struct btrfs_key
*location
;
5381 struct btrfs_path
*path
;
5382 struct btrfs_inode_ref
*ref
;
5383 struct btrfs_key key
[2];
5389 path
= btrfs_alloc_path();
5391 return ERR_PTR(-ENOMEM
);
5393 inode
= new_inode(root
->fs_info
->sb
);
5395 btrfs_free_path(path
);
5396 return ERR_PTR(-ENOMEM
);
5400 * we have to initialize this early, so we can reclaim the inode
5401 * number if we fail afterwards in this function.
5403 inode
->i_ino
= objectid
;
5406 trace_btrfs_inode_request(dir
);
5408 ret
= btrfs_set_inode_index(dir
, index
);
5410 btrfs_free_path(path
);
5412 return ERR_PTR(ret
);
5416 * index_cnt is ignored for everything but a dir,
5417 * btrfs_get_inode_index_count has an explanation for the magic
5420 BTRFS_I(inode
)->index_cnt
= 2;
5421 BTRFS_I(inode
)->root
= root
;
5422 BTRFS_I(inode
)->generation
= trans
->transid
;
5423 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5426 * We could have gotten an inode number from somebody who was fsynced
5427 * and then removed in this same transaction, so let's just set full
5428 * sync since it will be a full sync anyway and this will blow away the
5429 * old info in the log.
5431 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5438 key
[0].objectid
= objectid
;
5439 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
5443 * Start new inodes with an inode_ref. This is slightly more
5444 * efficient for small numbers of hard links since they will
5445 * be packed into one item. Extended refs will kick in if we
5446 * add more hard links than can fit in the ref item.
5448 key
[1].objectid
= objectid
;
5449 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
5450 key
[1].offset
= ref_objectid
;
5452 sizes
[0] = sizeof(struct btrfs_inode_item
);
5453 sizes
[1] = name_len
+ sizeof(*ref
);
5455 path
->leave_spinning
= 1;
5456 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
5460 inode_init_owner(inode
, dir
, mode
);
5461 inode_set_bytes(inode
, 0);
5462 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5463 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5464 struct btrfs_inode_item
);
5465 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5466 sizeof(*inode_item
));
5467 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5469 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5470 struct btrfs_inode_ref
);
5471 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5472 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5473 ptr
= (unsigned long)(ref
+ 1);
5474 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5476 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5477 btrfs_free_path(path
);
5479 location
= &BTRFS_I(inode
)->location
;
5480 location
->objectid
= objectid
;
5481 location
->offset
= 0;
5482 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
5484 btrfs_inherit_iflags(inode
, dir
);
5486 if (S_ISREG(mode
)) {
5487 if (btrfs_test_opt(root
, NODATASUM
))
5488 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5489 if (btrfs_test_opt(root
, NODATACOW
))
5490 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5491 BTRFS_INODE_NODATASUM
;
5494 insert_inode_hash(inode
);
5495 inode_tree_add(inode
);
5497 trace_btrfs_inode_new(inode
);
5498 btrfs_set_inode_last_trans(trans
, inode
);
5500 btrfs_update_root_times(trans
, root
);
5505 BTRFS_I(dir
)->index_cnt
--;
5506 btrfs_free_path(path
);
5508 return ERR_PTR(ret
);
5511 static inline u8
btrfs_inode_type(struct inode
*inode
)
5513 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5517 * utility function to add 'inode' into 'parent_inode' with
5518 * a give name and a given sequence number.
5519 * if 'add_backref' is true, also insert a backref from the
5520 * inode to the parent directory.
5522 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5523 struct inode
*parent_inode
, struct inode
*inode
,
5524 const char *name
, int name_len
, int add_backref
, u64 index
)
5527 struct btrfs_key key
;
5528 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
5529 u64 ino
= btrfs_ino(inode
);
5530 u64 parent_ino
= btrfs_ino(parent_inode
);
5532 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5533 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
5536 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
5540 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5541 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
5542 key
.objectid
, root
->root_key
.objectid
,
5543 parent_ino
, index
, name
, name_len
);
5544 } else if (add_backref
) {
5545 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
5549 /* Nothing to clean up yet */
5553 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
5555 btrfs_inode_type(inode
), index
);
5556 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
5559 btrfs_abort_transaction(trans
, root
, ret
);
5563 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5565 inode_inc_iversion(parent_inode
);
5566 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5567 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5569 btrfs_abort_transaction(trans
, root
, ret
);
5573 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5576 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5577 key
.objectid
, root
->root_key
.objectid
,
5578 parent_ino
, &local_index
, name
, name_len
);
5580 } else if (add_backref
) {
5584 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5585 ino
, parent_ino
, &local_index
);
5590 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5591 struct inode
*dir
, struct dentry
*dentry
,
5592 struct inode
*inode
, int backref
, u64 index
)
5594 int err
= btrfs_add_link(trans
, dir
, inode
,
5595 dentry
->d_name
.name
, dentry
->d_name
.len
,
5602 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5603 umode_t mode
, dev_t rdev
)
5605 struct btrfs_trans_handle
*trans
;
5606 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5607 struct inode
*inode
= NULL
;
5613 if (!new_valid_dev(rdev
))
5617 * 2 for inode item and ref
5619 * 1 for xattr if selinux is on
5621 trans
= btrfs_start_transaction(root
, 5);
5623 return PTR_ERR(trans
);
5625 err
= btrfs_find_free_ino(root
, &objectid
);
5629 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5630 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5632 if (IS_ERR(inode
)) {
5633 err
= PTR_ERR(inode
);
5637 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5644 * If the active LSM wants to access the inode during
5645 * d_instantiate it needs these. Smack checks to see
5646 * if the filesystem supports xattrs by looking at the
5650 inode
->i_op
= &btrfs_special_inode_operations
;
5651 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5655 init_special_inode(inode
, inode
->i_mode
, rdev
);
5656 btrfs_update_inode(trans
, root
, inode
);
5657 d_instantiate(dentry
, inode
);
5660 btrfs_end_transaction(trans
, root
);
5661 btrfs_btree_balance_dirty(root
);
5663 inode_dec_link_count(inode
);
5669 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5670 umode_t mode
, bool excl
)
5672 struct btrfs_trans_handle
*trans
;
5673 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5674 struct inode
*inode
= NULL
;
5675 int drop_inode_on_err
= 0;
5681 * 2 for inode item and ref
5683 * 1 for xattr if selinux is on
5685 trans
= btrfs_start_transaction(root
, 5);
5687 return PTR_ERR(trans
);
5689 err
= btrfs_find_free_ino(root
, &objectid
);
5693 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5694 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5696 if (IS_ERR(inode
)) {
5697 err
= PTR_ERR(inode
);
5700 drop_inode_on_err
= 1;
5702 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5706 err
= btrfs_update_inode(trans
, root
, inode
);
5711 * If the active LSM wants to access the inode during
5712 * d_instantiate it needs these. Smack checks to see
5713 * if the filesystem supports xattrs by looking at the
5716 inode
->i_fop
= &btrfs_file_operations
;
5717 inode
->i_op
= &btrfs_file_inode_operations
;
5719 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5723 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5724 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5725 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5726 d_instantiate(dentry
, inode
);
5729 btrfs_end_transaction(trans
, root
);
5730 if (err
&& drop_inode_on_err
) {
5731 inode_dec_link_count(inode
);
5734 btrfs_btree_balance_dirty(root
);
5738 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5739 struct dentry
*dentry
)
5741 struct btrfs_trans_handle
*trans
;
5742 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5743 struct inode
*inode
= old_dentry
->d_inode
;
5748 /* do not allow sys_link's with other subvols of the same device */
5749 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5752 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5755 err
= btrfs_set_inode_index(dir
, &index
);
5760 * 2 items for inode and inode ref
5761 * 2 items for dir items
5762 * 1 item for parent inode
5764 trans
= btrfs_start_transaction(root
, 5);
5765 if (IS_ERR(trans
)) {
5766 err
= PTR_ERR(trans
);
5770 btrfs_inc_nlink(inode
);
5771 inode_inc_iversion(inode
);
5772 inode
->i_ctime
= CURRENT_TIME
;
5774 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5776 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5781 struct dentry
*parent
= dentry
->d_parent
;
5782 err
= btrfs_update_inode(trans
, root
, inode
);
5785 d_instantiate(dentry
, inode
);
5786 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5789 btrfs_end_transaction(trans
, root
);
5792 inode_dec_link_count(inode
);
5795 btrfs_btree_balance_dirty(root
);
5799 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5801 struct inode
*inode
= NULL
;
5802 struct btrfs_trans_handle
*trans
;
5803 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5805 int drop_on_err
= 0;
5810 * 2 items for inode and ref
5811 * 2 items for dir items
5812 * 1 for xattr if selinux is on
5814 trans
= btrfs_start_transaction(root
, 5);
5816 return PTR_ERR(trans
);
5818 err
= btrfs_find_free_ino(root
, &objectid
);
5822 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5823 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5824 S_IFDIR
| mode
, &index
);
5825 if (IS_ERR(inode
)) {
5826 err
= PTR_ERR(inode
);
5832 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5836 inode
->i_op
= &btrfs_dir_inode_operations
;
5837 inode
->i_fop
= &btrfs_dir_file_operations
;
5839 btrfs_i_size_write(inode
, 0);
5840 err
= btrfs_update_inode(trans
, root
, inode
);
5844 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5845 dentry
->d_name
.len
, 0, index
);
5849 d_instantiate(dentry
, inode
);
5853 btrfs_end_transaction(trans
, root
);
5856 btrfs_btree_balance_dirty(root
);
5860 /* helper for btfs_get_extent. Given an existing extent in the tree,
5861 * and an extent that you want to insert, deal with overlap and insert
5862 * the new extent into the tree.
5864 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5865 struct extent_map
*existing
,
5866 struct extent_map
*em
,
5867 u64 map_start
, u64 map_len
)
5871 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5872 start_diff
= map_start
- em
->start
;
5873 em
->start
= map_start
;
5875 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5876 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5877 em
->block_start
+= start_diff
;
5878 em
->block_len
-= start_diff
;
5880 return add_extent_mapping(em_tree
, em
, 0);
5883 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5884 struct inode
*inode
, struct page
*page
,
5885 size_t pg_offset
, u64 extent_offset
,
5886 struct btrfs_file_extent_item
*item
)
5889 struct extent_buffer
*leaf
= path
->nodes
[0];
5892 unsigned long inline_size
;
5896 WARN_ON(pg_offset
!= 0);
5897 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5898 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
5899 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
5900 btrfs_item_nr(leaf
, path
->slots
[0]));
5901 tmp
= kmalloc(inline_size
, GFP_NOFS
);
5904 ptr
= btrfs_file_extent_inline_start(item
);
5906 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
5908 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
5909 ret
= btrfs_decompress(compress_type
, tmp
, page
,
5910 extent_offset
, inline_size
, max_size
);
5912 char *kaddr
= kmap_atomic(page
);
5913 unsigned long copy_size
= min_t(u64
,
5914 PAGE_CACHE_SIZE
- pg_offset
,
5915 max_size
- extent_offset
);
5916 memset(kaddr
+ pg_offset
, 0, copy_size
);
5917 kunmap_atomic(kaddr
);
5924 * a bit scary, this does extent mapping from logical file offset to the disk.
5925 * the ugly parts come from merging extents from the disk with the in-ram
5926 * representation. This gets more complex because of the data=ordered code,
5927 * where the in-ram extents might be locked pending data=ordered completion.
5929 * This also copies inline extents directly into the page.
5932 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
5933 size_t pg_offset
, u64 start
, u64 len
,
5939 u64 extent_start
= 0;
5941 u64 objectid
= btrfs_ino(inode
);
5943 struct btrfs_path
*path
= NULL
;
5944 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5945 struct btrfs_file_extent_item
*item
;
5946 struct extent_buffer
*leaf
;
5947 struct btrfs_key found_key
;
5948 struct extent_map
*em
= NULL
;
5949 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5950 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5951 struct btrfs_trans_handle
*trans
= NULL
;
5955 read_lock(&em_tree
->lock
);
5956 em
= lookup_extent_mapping(em_tree
, start
, len
);
5958 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5959 read_unlock(&em_tree
->lock
);
5962 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
5963 free_extent_map(em
);
5964 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5965 free_extent_map(em
);
5969 em
= alloc_extent_map();
5974 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5975 em
->start
= EXTENT_MAP_HOLE
;
5976 em
->orig_start
= EXTENT_MAP_HOLE
;
5978 em
->block_len
= (u64
)-1;
5981 path
= btrfs_alloc_path();
5987 * Chances are we'll be called again, so go ahead and do
5993 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5994 objectid
, start
, trans
!= NULL
);
6001 if (path
->slots
[0] == 0)
6006 leaf
= path
->nodes
[0];
6007 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6008 struct btrfs_file_extent_item
);
6009 /* are we inside the extent that was found? */
6010 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6011 found_type
= btrfs_key_type(&found_key
);
6012 if (found_key
.objectid
!= objectid
||
6013 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6017 found_type
= btrfs_file_extent_type(leaf
, item
);
6018 extent_start
= found_key
.offset
;
6019 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6020 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6021 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6022 extent_end
= extent_start
+
6023 btrfs_file_extent_num_bytes(leaf
, item
);
6024 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6026 size
= btrfs_file_extent_inline_len(leaf
, item
);
6027 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6030 if (start
>= extent_end
) {
6032 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6033 ret
= btrfs_next_leaf(root
, path
);
6040 leaf
= path
->nodes
[0];
6042 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6043 if (found_key
.objectid
!= objectid
||
6044 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6046 if (start
+ len
<= found_key
.offset
)
6049 em
->orig_start
= start
;
6050 em
->len
= found_key
.offset
- start
;
6054 em
->ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, item
);
6055 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6056 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6057 em
->start
= extent_start
;
6058 em
->len
= extent_end
- extent_start
;
6059 em
->orig_start
= extent_start
-
6060 btrfs_file_extent_offset(leaf
, item
);
6061 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
6063 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
6065 em
->block_start
= EXTENT_MAP_HOLE
;
6068 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
6069 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6070 em
->compress_type
= compress_type
;
6071 em
->block_start
= bytenr
;
6072 em
->block_len
= em
->orig_block_len
;
6074 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
6075 em
->block_start
= bytenr
;
6076 em
->block_len
= em
->len
;
6077 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
6078 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6081 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6085 size_t extent_offset
;
6088 em
->block_start
= EXTENT_MAP_INLINE
;
6089 if (!page
|| create
) {
6090 em
->start
= extent_start
;
6091 em
->len
= extent_end
- extent_start
;
6095 size
= btrfs_file_extent_inline_len(leaf
, item
);
6096 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6097 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6098 size
- extent_offset
);
6099 em
->start
= extent_start
+ extent_offset
;
6100 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6101 em
->orig_block_len
= em
->len
;
6102 em
->orig_start
= em
->start
;
6103 if (compress_type
) {
6104 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6105 em
->compress_type
= compress_type
;
6107 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6108 if (create
== 0 && !PageUptodate(page
)) {
6109 if (btrfs_file_extent_compression(leaf
, item
) !=
6110 BTRFS_COMPRESS_NONE
) {
6111 ret
= uncompress_inline(path
, inode
, page
,
6113 extent_offset
, item
);
6114 BUG_ON(ret
); /* -ENOMEM */
6117 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6119 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6120 memset(map
+ pg_offset
+ copy_size
, 0,
6121 PAGE_CACHE_SIZE
- pg_offset
-
6126 flush_dcache_page(page
);
6127 } else if (create
&& PageUptodate(page
)) {
6131 free_extent_map(em
);
6134 btrfs_release_path(path
);
6135 trans
= btrfs_join_transaction(root
);
6138 return ERR_CAST(trans
);
6142 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6145 btrfs_mark_buffer_dirty(leaf
);
6147 set_extent_uptodate(io_tree
, em
->start
,
6148 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6151 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
6155 em
->orig_start
= start
;
6158 em
->block_start
= EXTENT_MAP_HOLE
;
6159 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6161 btrfs_release_path(path
);
6162 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6163 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6164 (unsigned long long)em
->start
,
6165 (unsigned long long)em
->len
,
6166 (unsigned long long)start
,
6167 (unsigned long long)len
);
6173 write_lock(&em_tree
->lock
);
6174 ret
= add_extent_mapping(em_tree
, em
, 0);
6175 /* it is possible that someone inserted the extent into the tree
6176 * while we had the lock dropped. It is also possible that
6177 * an overlapping map exists in the tree
6179 if (ret
== -EEXIST
) {
6180 struct extent_map
*existing
;
6184 existing
= lookup_extent_mapping(em_tree
, start
, len
);
6185 if (existing
&& (existing
->start
> start
||
6186 existing
->start
+ existing
->len
<= start
)) {
6187 free_extent_map(existing
);
6191 existing
= lookup_extent_mapping(em_tree
, em
->start
,
6194 err
= merge_extent_mapping(em_tree
, existing
,
6197 free_extent_map(existing
);
6199 free_extent_map(em
);
6204 free_extent_map(em
);
6208 free_extent_map(em
);
6213 write_unlock(&em_tree
->lock
);
6217 trace_btrfs_get_extent(root
, em
);
6220 btrfs_free_path(path
);
6222 ret
= btrfs_end_transaction(trans
, root
);
6227 free_extent_map(em
);
6228 return ERR_PTR(err
);
6230 BUG_ON(!em
); /* Error is always set */
6234 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6235 size_t pg_offset
, u64 start
, u64 len
,
6238 struct extent_map
*em
;
6239 struct extent_map
*hole_em
= NULL
;
6240 u64 range_start
= start
;
6246 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6253 * - a pre-alloc extent,
6254 * there might actually be delalloc bytes behind it.
6256 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6257 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6263 /* check to see if we've wrapped (len == -1 or similar) */
6272 /* ok, we didn't find anything, lets look for delalloc */
6273 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6274 end
, len
, EXTENT_DELALLOC
, 1);
6275 found_end
= range_start
+ found
;
6276 if (found_end
< range_start
)
6277 found_end
= (u64
)-1;
6280 * we didn't find anything useful, return
6281 * the original results from get_extent()
6283 if (range_start
> end
|| found_end
<= start
) {
6289 /* adjust the range_start to make sure it doesn't
6290 * go backwards from the start they passed in
6292 range_start
= max(start
,range_start
);
6293 found
= found_end
- range_start
;
6296 u64 hole_start
= start
;
6299 em
= alloc_extent_map();
6305 * when btrfs_get_extent can't find anything it
6306 * returns one huge hole
6308 * make sure what it found really fits our range, and
6309 * adjust to make sure it is based on the start from
6313 u64 calc_end
= extent_map_end(hole_em
);
6315 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6316 free_extent_map(hole_em
);
6319 hole_start
= max(hole_em
->start
, start
);
6320 hole_len
= calc_end
- hole_start
;
6324 if (hole_em
&& range_start
> hole_start
) {
6325 /* our hole starts before our delalloc, so we
6326 * have to return just the parts of the hole
6327 * that go until the delalloc starts
6329 em
->len
= min(hole_len
,
6330 range_start
- hole_start
);
6331 em
->start
= hole_start
;
6332 em
->orig_start
= hole_start
;
6334 * don't adjust block start at all,
6335 * it is fixed at EXTENT_MAP_HOLE
6337 em
->block_start
= hole_em
->block_start
;
6338 em
->block_len
= hole_len
;
6339 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6340 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6342 em
->start
= range_start
;
6344 em
->orig_start
= range_start
;
6345 em
->block_start
= EXTENT_MAP_DELALLOC
;
6346 em
->block_len
= found
;
6348 } else if (hole_em
) {
6353 free_extent_map(hole_em
);
6355 free_extent_map(em
);
6356 return ERR_PTR(err
);
6361 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6364 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6365 struct btrfs_trans_handle
*trans
;
6366 struct extent_map
*em
;
6367 struct btrfs_key ins
;
6371 trans
= btrfs_join_transaction(root
);
6373 return ERR_CAST(trans
);
6375 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
6377 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6378 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
6379 alloc_hint
, &ins
, 1);
6385 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6386 ins
.offset
, ins
.offset
, ins
.offset
, 0);
6390 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6391 ins
.offset
, ins
.offset
, 0);
6393 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6397 btrfs_end_transaction(trans
, root
);
6402 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6403 * block must be cow'd
6405 noinline
int can_nocow_extent(struct btrfs_trans_handle
*trans
,
6406 struct inode
*inode
, u64 offset
, u64
*len
,
6407 u64
*orig_start
, u64
*orig_block_len
,
6410 struct btrfs_path
*path
;
6412 struct extent_buffer
*leaf
;
6413 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6414 struct btrfs_file_extent_item
*fi
;
6415 struct btrfs_key key
;
6422 bool nocow
= (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
);
6423 path
= btrfs_alloc_path();
6427 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
6432 slot
= path
->slots
[0];
6435 /* can't find the item, must cow */
6442 leaf
= path
->nodes
[0];
6443 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6444 if (key
.objectid
!= btrfs_ino(inode
) ||
6445 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6446 /* not our file or wrong item type, must cow */
6450 if (key
.offset
> offset
) {
6451 /* Wrong offset, must cow */
6455 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6456 found_type
= btrfs_file_extent_type(leaf
, fi
);
6457 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6458 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6459 /* not a regular extent, must cow */
6463 if (!nocow
&& found_type
== BTRFS_FILE_EXTENT_REG
)
6466 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6467 if (disk_bytenr
== 0)
6470 if (btrfs_file_extent_compression(leaf
, fi
) ||
6471 btrfs_file_extent_encryption(leaf
, fi
) ||
6472 btrfs_file_extent_other_encoding(leaf
, fi
))
6475 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6478 *orig_start
= key
.offset
- backref_offset
;
6479 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
6480 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
6483 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6485 if (btrfs_extent_readonly(root
, disk_bytenr
))
6489 * look for other files referencing this extent, if we
6490 * find any we must cow
6492 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
6493 key
.offset
- backref_offset
, disk_bytenr
))
6497 * adjust disk_bytenr and num_bytes to cover just the bytes
6498 * in this extent we are about to write. If there
6499 * are any csums in that range we have to cow in order
6500 * to keep the csums correct
6502 disk_bytenr
+= backref_offset
;
6503 disk_bytenr
+= offset
- key
.offset
;
6504 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
6505 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
6508 * all of the above have passed, it is safe to overwrite this extent
6514 btrfs_free_path(path
);
6518 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
6519 struct extent_state
**cached_state
, int writing
)
6521 struct btrfs_ordered_extent
*ordered
;
6525 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6528 * We're concerned with the entire range that we're going to be
6529 * doing DIO to, so we need to make sure theres no ordered
6530 * extents in this range.
6532 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6533 lockend
- lockstart
+ 1);
6536 * We need to make sure there are no buffered pages in this
6537 * range either, we could have raced between the invalidate in
6538 * generic_file_direct_write and locking the extent. The
6539 * invalidate needs to happen so that reads after a write do not
6542 if (!ordered
&& (!writing
||
6543 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
6544 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
6548 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6549 cached_state
, GFP_NOFS
);
6552 btrfs_start_ordered_extent(inode
, ordered
, 1);
6553 btrfs_put_ordered_extent(ordered
);
6555 /* Screw you mmap */
6556 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
6563 * If we found a page that couldn't be invalidated just
6564 * fall back to buffered.
6566 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6567 lockstart
>> PAGE_CACHE_SHIFT
,
6568 lockend
>> PAGE_CACHE_SHIFT
);
6579 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
6580 u64 len
, u64 orig_start
,
6581 u64 block_start
, u64 block_len
,
6582 u64 orig_block_len
, u64 ram_bytes
,
6585 struct extent_map_tree
*em_tree
;
6586 struct extent_map
*em
;
6587 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6590 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6591 em
= alloc_extent_map();
6593 return ERR_PTR(-ENOMEM
);
6596 em
->orig_start
= orig_start
;
6597 em
->mod_start
= start
;
6600 em
->block_len
= block_len
;
6601 em
->block_start
= block_start
;
6602 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6603 em
->orig_block_len
= orig_block_len
;
6604 em
->ram_bytes
= ram_bytes
;
6605 em
->generation
= -1;
6606 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6607 if (type
== BTRFS_ORDERED_PREALLOC
)
6608 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6611 btrfs_drop_extent_cache(inode
, em
->start
,
6612 em
->start
+ em
->len
- 1, 0);
6613 write_lock(&em_tree
->lock
);
6614 ret
= add_extent_mapping(em_tree
, em
, 1);
6615 write_unlock(&em_tree
->lock
);
6616 } while (ret
== -EEXIST
);
6619 free_extent_map(em
);
6620 return ERR_PTR(ret
);
6627 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6628 struct buffer_head
*bh_result
, int create
)
6630 struct extent_map
*em
;
6631 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6632 struct extent_state
*cached_state
= NULL
;
6633 u64 start
= iblock
<< inode
->i_blkbits
;
6634 u64 lockstart
, lockend
;
6635 u64 len
= bh_result
->b_size
;
6636 struct btrfs_trans_handle
*trans
;
6637 int unlock_bits
= EXTENT_LOCKED
;
6641 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6643 len
= min_t(u64
, len
, root
->sectorsize
);
6646 lockend
= start
+ len
- 1;
6649 * If this errors out it's because we couldn't invalidate pagecache for
6650 * this range and we need to fallback to buffered.
6652 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6655 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6662 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6663 * io. INLINE is special, and we could probably kludge it in here, but
6664 * it's still buffered so for safety lets just fall back to the generic
6667 * For COMPRESSED we _have_ to read the entire extent in so we can
6668 * decompress it, so there will be buffering required no matter what we
6669 * do, so go ahead and fallback to buffered.
6671 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6672 * to buffered IO. Don't blame me, this is the price we pay for using
6675 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6676 em
->block_start
== EXTENT_MAP_INLINE
) {
6677 free_extent_map(em
);
6682 /* Just a good old fashioned hole, return */
6683 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6684 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6685 free_extent_map(em
);
6690 * We don't allocate a new extent in the following cases
6692 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6694 * 2) The extent is marked as PREALLOC. We're good to go here and can
6695 * just use the extent.
6699 len
= min(len
, em
->len
- (start
- em
->start
));
6700 lockstart
= start
+ len
;
6704 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6705 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6706 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6709 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
6711 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6712 type
= BTRFS_ORDERED_PREALLOC
;
6714 type
= BTRFS_ORDERED_NOCOW
;
6715 len
= min(len
, em
->len
- (start
- em
->start
));
6716 block_start
= em
->block_start
+ (start
- em
->start
);
6719 * we're not going to log anything, but we do need
6720 * to make sure the current transaction stays open
6721 * while we look for nocow cross refs
6723 trans
= btrfs_join_transaction(root
);
6727 if (can_nocow_extent(trans
, inode
, start
, &len
, &orig_start
,
6728 &orig_block_len
, &ram_bytes
) == 1) {
6729 if (type
== BTRFS_ORDERED_PREALLOC
) {
6730 free_extent_map(em
);
6731 em
= create_pinned_em(inode
, start
, len
,
6737 btrfs_end_transaction(trans
, root
);
6742 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6743 block_start
, len
, len
, type
);
6744 btrfs_end_transaction(trans
, root
);
6746 free_extent_map(em
);
6751 btrfs_end_transaction(trans
, root
);
6755 * this will cow the extent, reset the len in case we changed
6758 len
= bh_result
->b_size
;
6759 free_extent_map(em
);
6760 em
= btrfs_new_extent_direct(inode
, start
, len
);
6765 len
= min(len
, em
->len
- (start
- em
->start
));
6767 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6769 bh_result
->b_size
= len
;
6770 bh_result
->b_bdev
= em
->bdev
;
6771 set_buffer_mapped(bh_result
);
6773 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6774 set_buffer_new(bh_result
);
6777 * Need to update the i_size under the extent lock so buffered
6778 * readers will get the updated i_size when we unlock.
6780 if (start
+ len
> i_size_read(inode
))
6781 i_size_write(inode
, start
+ len
);
6783 spin_lock(&BTRFS_I(inode
)->lock
);
6784 BTRFS_I(inode
)->outstanding_extents
++;
6785 spin_unlock(&BTRFS_I(inode
)->lock
);
6787 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6788 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
6789 &cached_state
, GFP_NOFS
);
6794 * In the case of write we need to clear and unlock the entire range,
6795 * in the case of read we need to unlock only the end area that we
6796 * aren't using if there is any left over space.
6798 if (lockstart
< lockend
) {
6799 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6800 lockend
, unlock_bits
, 1, 0,
6801 &cached_state
, GFP_NOFS
);
6803 free_extent_state(cached_state
);
6806 free_extent_map(em
);
6811 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6812 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6816 struct btrfs_dio_private
{
6817 struct inode
*inode
;
6823 /* number of bios pending for this dio */
6824 atomic_t pending_bios
;
6829 /* orig_bio is our btrfs_io_bio */
6830 struct bio
*orig_bio
;
6832 /* dio_bio came from fs/direct-io.c */
6833 struct bio
*dio_bio
;
6836 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6838 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6839 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6840 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6841 struct inode
*inode
= dip
->inode
;
6842 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6843 struct bio
*dio_bio
;
6846 start
= dip
->logical_offset
;
6848 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6849 struct page
*page
= bvec
->bv_page
;
6852 u64
private = ~(u32
)0;
6853 unsigned long flags
;
6855 if (get_state_private(&BTRFS_I(inode
)->io_tree
,
6858 local_irq_save(flags
);
6859 kaddr
= kmap_atomic(page
);
6860 csum
= btrfs_csum_data(kaddr
+ bvec
->bv_offset
,
6861 csum
, bvec
->bv_len
);
6862 btrfs_csum_final(csum
, (char *)&csum
);
6863 kunmap_atomic(kaddr
);
6864 local_irq_restore(flags
);
6866 flush_dcache_page(bvec
->bv_page
);
6867 if (csum
!= private) {
6869 btrfs_err(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %u",
6870 (unsigned long long)btrfs_ino(inode
),
6871 (unsigned long long)start
,
6872 csum
, (unsigned)private);
6877 start
+= bvec
->bv_len
;
6879 } while (bvec
<= bvec_end
);
6881 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6882 dip
->logical_offset
+ dip
->bytes
- 1);
6883 dio_bio
= dip
->dio_bio
;
6887 /* If we had a csum failure make sure to clear the uptodate flag */
6889 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
6890 dio_end_io(dio_bio
, err
);
6894 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6896 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6897 struct inode
*inode
= dip
->inode
;
6898 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6899 struct btrfs_ordered_extent
*ordered
= NULL
;
6900 u64 ordered_offset
= dip
->logical_offset
;
6901 u64 ordered_bytes
= dip
->bytes
;
6902 struct bio
*dio_bio
;
6908 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
6910 ordered_bytes
, !err
);
6914 ordered
->work
.func
= finish_ordered_fn
;
6915 ordered
->work
.flags
= 0;
6916 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
6920 * our bio might span multiple ordered extents. If we haven't
6921 * completed the accounting for the whole dio, go back and try again
6923 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
6924 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
6930 dio_bio
= dip
->dio_bio
;
6934 /* If we had an error make sure to clear the uptodate flag */
6936 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
6937 dio_end_io(dio_bio
, err
);
6941 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
6942 struct bio
*bio
, int mirror_num
,
6943 unsigned long bio_flags
, u64 offset
)
6946 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6947 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
6948 BUG_ON(ret
); /* -ENOMEM */
6952 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
6954 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6957 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
6958 "sector %#Lx len %u err no %d\n",
6959 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
6960 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
6964 * before atomic variable goto zero, we must make sure
6965 * dip->errors is perceived to be set.
6967 smp_mb__before_atomic_dec();
6970 /* if there are more bios still pending for this dio, just exit */
6971 if (!atomic_dec_and_test(&dip
->pending_bios
))
6975 bio_io_error(dip
->orig_bio
);
6977 set_bit(BIO_UPTODATE
, &dip
->dio_bio
->bi_flags
);
6978 bio_endio(dip
->orig_bio
, 0);
6984 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
6985 u64 first_sector
, gfp_t gfp_flags
)
6987 int nr_vecs
= bio_get_nr_vecs(bdev
);
6988 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
6991 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
6992 int rw
, u64 file_offset
, int skip_sum
,
6995 int write
= rw
& REQ_WRITE
;
6996 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7000 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
7005 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
7013 if (write
&& async_submit
) {
7014 ret
= btrfs_wq_submit_bio(root
->fs_info
,
7015 inode
, rw
, bio
, 0, 0,
7017 __btrfs_submit_bio_start_direct_io
,
7018 __btrfs_submit_bio_done
);
7022 * If we aren't doing async submit, calculate the csum of the
7025 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
7028 } else if (!skip_sum
) {
7029 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
, file_offset
);
7035 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
7041 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
7044 struct inode
*inode
= dip
->inode
;
7045 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7047 struct bio
*orig_bio
= dip
->orig_bio
;
7048 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
7049 u64 start_sector
= orig_bio
->bi_sector
;
7050 u64 file_offset
= dip
->logical_offset
;
7055 int async_submit
= 0;
7057 map_length
= orig_bio
->bi_size
;
7058 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
7059 &map_length
, NULL
, 0);
7064 if (map_length
>= orig_bio
->bi_size
) {
7069 /* async crcs make it difficult to collect full stripe writes. */
7070 if (btrfs_get_alloc_profile(root
, 1) &
7071 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
7076 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7079 bio
->bi_private
= dip
;
7080 bio
->bi_end_io
= btrfs_end_dio_bio
;
7081 atomic_inc(&dip
->pending_bios
);
7083 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7084 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
7085 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7086 bvec
->bv_offset
) < bvec
->bv_len
)) {
7088 * inc the count before we submit the bio so
7089 * we know the end IO handler won't happen before
7090 * we inc the count. Otherwise, the dip might get freed
7091 * before we're done setting it up
7093 atomic_inc(&dip
->pending_bios
);
7094 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7095 file_offset
, skip_sum
,
7099 atomic_dec(&dip
->pending_bios
);
7103 start_sector
+= submit_len
>> 9;
7104 file_offset
+= submit_len
;
7109 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7110 start_sector
, GFP_NOFS
);
7113 bio
->bi_private
= dip
;
7114 bio
->bi_end_io
= btrfs_end_dio_bio
;
7116 map_length
= orig_bio
->bi_size
;
7117 ret
= btrfs_map_block(root
->fs_info
, rw
,
7119 &map_length
, NULL
, 0);
7125 submit_len
+= bvec
->bv_len
;
7132 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7141 * before atomic variable goto zero, we must
7142 * make sure dip->errors is perceived to be set.
7144 smp_mb__before_atomic_dec();
7145 if (atomic_dec_and_test(&dip
->pending_bios
))
7146 bio_io_error(dip
->orig_bio
);
7148 /* bio_end_io() will handle error, so we needn't return it */
7152 static void btrfs_submit_direct(int rw
, struct bio
*dio_bio
,
7153 struct inode
*inode
, loff_t file_offset
)
7155 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7156 struct btrfs_dio_private
*dip
;
7159 int write
= rw
& REQ_WRITE
;
7162 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7164 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
7171 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
7177 dip
->private = dio_bio
->bi_private
;
7179 dip
->logical_offset
= file_offset
;
7180 dip
->bytes
= dio_bio
->bi_size
;
7181 dip
->disk_bytenr
= (u64
)dio_bio
->bi_sector
<< 9;
7182 io_bio
->bi_private
= dip
;
7184 dip
->orig_bio
= io_bio
;
7185 dip
->dio_bio
= dio_bio
;
7186 atomic_set(&dip
->pending_bios
, 0);
7189 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
7191 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
7193 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
7202 * If this is a write, we need to clean up the reserved space and kill
7203 * the ordered extent.
7206 struct btrfs_ordered_extent
*ordered
;
7207 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
7208 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
7209 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
7210 btrfs_free_reserved_extent(root
, ordered
->start
,
7212 btrfs_put_ordered_extent(ordered
);
7213 btrfs_put_ordered_extent(ordered
);
7215 bio_endio(dio_bio
, ret
);
7218 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
7219 const struct iovec
*iov
, loff_t offset
,
7220 unsigned long nr_segs
)
7226 unsigned blocksize_mask
= root
->sectorsize
- 1;
7227 ssize_t retval
= -EINVAL
;
7228 loff_t end
= offset
;
7230 if (offset
& blocksize_mask
)
7233 /* Check the memory alignment. Blocks cannot straddle pages */
7234 for (seg
= 0; seg
< nr_segs
; seg
++) {
7235 addr
= (unsigned long)iov
[seg
].iov_base
;
7236 size
= iov
[seg
].iov_len
;
7238 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
7241 /* If this is a write we don't need to check anymore */
7246 * Check to make sure we don't have duplicate iov_base's in this
7247 * iovec, if so return EINVAL, otherwise we'll get csum errors
7248 * when reading back.
7250 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
7251 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
7260 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
7261 const struct iovec
*iov
, loff_t offset
,
7262 unsigned long nr_segs
)
7264 struct file
*file
= iocb
->ki_filp
;
7265 struct inode
*inode
= file
->f_mapping
->host
;
7269 bool relock
= false;
7272 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
7276 atomic_inc(&inode
->i_dio_count
);
7277 smp_mb__after_atomic_inc();
7280 * The generic stuff only does filemap_write_and_wait_range, which isn't
7281 * enough if we've written compressed pages to this area, so we need to
7282 * call btrfs_wait_ordered_range to make absolutely sure that any
7283 * outstanding dirty pages are on disk.
7285 count
= iov_length(iov
, nr_segs
);
7286 btrfs_wait_ordered_range(inode
, offset
, count
);
7290 * If the write DIO is beyond the EOF, we need update
7291 * the isize, but it is protected by i_mutex. So we can
7292 * not unlock the i_mutex at this case.
7294 if (offset
+ count
<= inode
->i_size
) {
7295 mutex_unlock(&inode
->i_mutex
);
7298 ret
= btrfs_delalloc_reserve_space(inode
, count
);
7301 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7302 &BTRFS_I(inode
)->runtime_flags
))) {
7303 inode_dio_done(inode
);
7304 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7308 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
7309 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
7310 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
7311 btrfs_submit_direct
, flags
);
7313 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
7314 btrfs_delalloc_release_space(inode
, count
);
7315 else if (ret
>= 0 && (size_t)ret
< count
)
7316 btrfs_delalloc_release_space(inode
,
7317 count
- (size_t)ret
);
7319 btrfs_delalloc_release_metadata(inode
, 0);
7323 inode_dio_done(inode
);
7325 mutex_lock(&inode
->i_mutex
);
7330 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7332 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
7333 __u64 start
, __u64 len
)
7337 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
7341 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
7344 int btrfs_readpage(struct file
*file
, struct page
*page
)
7346 struct extent_io_tree
*tree
;
7347 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7348 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
7351 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
7353 struct extent_io_tree
*tree
;
7356 if (current
->flags
& PF_MEMALLOC
) {
7357 redirty_page_for_writepage(wbc
, page
);
7361 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7362 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
7365 static int btrfs_writepages(struct address_space
*mapping
,
7366 struct writeback_control
*wbc
)
7368 struct extent_io_tree
*tree
;
7370 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7371 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
7375 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
7376 struct list_head
*pages
, unsigned nr_pages
)
7378 struct extent_io_tree
*tree
;
7379 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7380 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
7383 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7385 struct extent_io_tree
*tree
;
7386 struct extent_map_tree
*map
;
7389 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7390 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
7391 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
7393 ClearPagePrivate(page
);
7394 set_page_private(page
, 0);
7395 page_cache_release(page
);
7400 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7402 if (PageWriteback(page
) || PageDirty(page
))
7404 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
7407 static void btrfs_invalidatepage(struct page
*page
, unsigned int offset
,
7408 unsigned int length
)
7410 struct inode
*inode
= page
->mapping
->host
;
7411 struct extent_io_tree
*tree
;
7412 struct btrfs_ordered_extent
*ordered
;
7413 struct extent_state
*cached_state
= NULL
;
7414 u64 page_start
= page_offset(page
);
7415 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7418 * we have the page locked, so new writeback can't start,
7419 * and the dirty bit won't be cleared while we are here.
7421 * Wait for IO on this page so that we can safely clear
7422 * the PagePrivate2 bit and do ordered accounting
7424 wait_on_page_writeback(page
);
7426 tree
= &BTRFS_I(inode
)->io_tree
;
7428 btrfs_releasepage(page
, GFP_NOFS
);
7431 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7432 ordered
= btrfs_lookup_ordered_extent(inode
, page_offset(page
));
7435 * IO on this page will never be started, so we need
7436 * to account for any ordered extents now
7438 clear_extent_bit(tree
, page_start
, page_end
,
7439 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7440 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
7441 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
7443 * whoever cleared the private bit is responsible
7444 * for the finish_ordered_io
7446 if (TestClearPagePrivate2(page
) &&
7447 btrfs_dec_test_ordered_pending(inode
, &ordered
, page_start
,
7448 PAGE_CACHE_SIZE
, 1)) {
7449 btrfs_finish_ordered_io(ordered
);
7451 btrfs_put_ordered_extent(ordered
);
7452 cached_state
= NULL
;
7453 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7455 clear_extent_bit(tree
, page_start
, page_end
,
7456 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
7457 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
7458 &cached_state
, GFP_NOFS
);
7459 __btrfs_releasepage(page
, GFP_NOFS
);
7461 ClearPageChecked(page
);
7462 if (PagePrivate(page
)) {
7463 ClearPagePrivate(page
);
7464 set_page_private(page
, 0);
7465 page_cache_release(page
);
7470 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7471 * called from a page fault handler when a page is first dirtied. Hence we must
7472 * be careful to check for EOF conditions here. We set the page up correctly
7473 * for a written page which means we get ENOSPC checking when writing into
7474 * holes and correct delalloc and unwritten extent mapping on filesystems that
7475 * support these features.
7477 * We are not allowed to take the i_mutex here so we have to play games to
7478 * protect against truncate races as the page could now be beyond EOF. Because
7479 * vmtruncate() writes the inode size before removing pages, once we have the
7480 * page lock we can determine safely if the page is beyond EOF. If it is not
7481 * beyond EOF, then the page is guaranteed safe against truncation until we
7484 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
7486 struct page
*page
= vmf
->page
;
7487 struct inode
*inode
= file_inode(vma
->vm_file
);
7488 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7489 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7490 struct btrfs_ordered_extent
*ordered
;
7491 struct extent_state
*cached_state
= NULL
;
7493 unsigned long zero_start
;
7500 sb_start_pagefault(inode
->i_sb
);
7501 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
7503 ret
= file_update_time(vma
->vm_file
);
7509 else /* -ENOSPC, -EIO, etc */
7510 ret
= VM_FAULT_SIGBUS
;
7516 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
7519 size
= i_size_read(inode
);
7520 page_start
= page_offset(page
);
7521 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7523 if ((page
->mapping
!= inode
->i_mapping
) ||
7524 (page_start
>= size
)) {
7525 /* page got truncated out from underneath us */
7528 wait_on_page_writeback(page
);
7530 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
7531 set_page_extent_mapped(page
);
7534 * we can't set the delalloc bits if there are pending ordered
7535 * extents. Drop our locks and wait for them to finish
7537 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7539 unlock_extent_cached(io_tree
, page_start
, page_end
,
7540 &cached_state
, GFP_NOFS
);
7542 btrfs_start_ordered_extent(inode
, ordered
, 1);
7543 btrfs_put_ordered_extent(ordered
);
7548 * XXX - page_mkwrite gets called every time the page is dirtied, even
7549 * if it was already dirty, so for space accounting reasons we need to
7550 * clear any delalloc bits for the range we are fixing to save. There
7551 * is probably a better way to do this, but for now keep consistent with
7552 * prepare_pages in the normal write path.
7554 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
7555 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7556 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
7557 0, 0, &cached_state
, GFP_NOFS
);
7559 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
7562 unlock_extent_cached(io_tree
, page_start
, page_end
,
7563 &cached_state
, GFP_NOFS
);
7564 ret
= VM_FAULT_SIGBUS
;
7569 /* page is wholly or partially inside EOF */
7570 if (page_start
+ PAGE_CACHE_SIZE
> size
)
7571 zero_start
= size
& ~PAGE_CACHE_MASK
;
7573 zero_start
= PAGE_CACHE_SIZE
;
7575 if (zero_start
!= PAGE_CACHE_SIZE
) {
7577 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
7578 flush_dcache_page(page
);
7581 ClearPageChecked(page
);
7582 set_page_dirty(page
);
7583 SetPageUptodate(page
);
7585 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
7586 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
7587 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
7589 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
7593 sb_end_pagefault(inode
->i_sb
);
7594 return VM_FAULT_LOCKED
;
7598 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
7600 sb_end_pagefault(inode
->i_sb
);
7604 static int btrfs_truncate(struct inode
*inode
)
7606 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7607 struct btrfs_block_rsv
*rsv
;
7610 struct btrfs_trans_handle
*trans
;
7611 u64 mask
= root
->sectorsize
- 1;
7612 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7614 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
7615 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
7618 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7619 * 3 things going on here
7621 * 1) We need to reserve space for our orphan item and the space to
7622 * delete our orphan item. Lord knows we don't want to have a dangling
7623 * orphan item because we didn't reserve space to remove it.
7625 * 2) We need to reserve space to update our inode.
7627 * 3) We need to have something to cache all the space that is going to
7628 * be free'd up by the truncate operation, but also have some slack
7629 * space reserved in case it uses space during the truncate (thank you
7630 * very much snapshotting).
7632 * And we need these to all be seperate. The fact is we can use alot of
7633 * space doing the truncate, and we have no earthly idea how much space
7634 * we will use, so we need the truncate reservation to be seperate so it
7635 * doesn't end up using space reserved for updating the inode or
7636 * removing the orphan item. We also need to be able to stop the
7637 * transaction and start a new one, which means we need to be able to
7638 * update the inode several times, and we have no idea of knowing how
7639 * many times that will be, so we can't just reserve 1 item for the
7640 * entirety of the opration, so that has to be done seperately as well.
7641 * Then there is the orphan item, which does indeed need to be held on
7642 * to for the whole operation, and we need nobody to touch this reserved
7643 * space except the orphan code.
7645 * So that leaves us with
7647 * 1) root->orphan_block_rsv - for the orphan deletion.
7648 * 2) rsv - for the truncate reservation, which we will steal from the
7649 * transaction reservation.
7650 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7651 * updating the inode.
7653 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7656 rsv
->size
= min_size
;
7660 * 1 for the truncate slack space
7661 * 1 for updating the inode.
7663 trans
= btrfs_start_transaction(root
, 2);
7664 if (IS_ERR(trans
)) {
7665 err
= PTR_ERR(trans
);
7669 /* Migrate the slack space for the truncate to our reserve */
7670 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7675 * setattr is responsible for setting the ordered_data_close flag,
7676 * but that is only tested during the last file release. That
7677 * could happen well after the next commit, leaving a great big
7678 * window where new writes may get lost if someone chooses to write
7679 * to this file after truncating to zero
7681 * The inode doesn't have any dirty data here, and so if we commit
7682 * this is a noop. If someone immediately starts writing to the inode
7683 * it is very likely we'll catch some of their writes in this
7684 * transaction, and the commit will find this file on the ordered
7685 * data list with good things to send down.
7687 * This is a best effort solution, there is still a window where
7688 * using truncate to replace the contents of the file will
7689 * end up with a zero length file after a crash.
7691 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7692 &BTRFS_I(inode
)->runtime_flags
))
7693 btrfs_add_ordered_operation(trans
, root
, inode
);
7696 * So if we truncate and then write and fsync we normally would just
7697 * write the extents that changed, which is a problem if we need to
7698 * first truncate that entire inode. So set this flag so we write out
7699 * all of the extents in the inode to the sync log so we're completely
7702 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7703 trans
->block_rsv
= rsv
;
7706 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7708 BTRFS_EXTENT_DATA_KEY
);
7709 if (ret
!= -ENOSPC
) {
7714 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7715 ret
= btrfs_update_inode(trans
, root
, inode
);
7721 btrfs_end_transaction(trans
, root
);
7722 btrfs_btree_balance_dirty(root
);
7724 trans
= btrfs_start_transaction(root
, 2);
7725 if (IS_ERR(trans
)) {
7726 ret
= err
= PTR_ERR(trans
);
7731 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7733 BUG_ON(ret
); /* shouldn't happen */
7734 trans
->block_rsv
= rsv
;
7737 if (ret
== 0 && inode
->i_nlink
> 0) {
7738 trans
->block_rsv
= root
->orphan_block_rsv
;
7739 ret
= btrfs_orphan_del(trans
, inode
);
7745 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7746 ret
= btrfs_update_inode(trans
, root
, inode
);
7750 ret
= btrfs_end_transaction(trans
, root
);
7751 btrfs_btree_balance_dirty(root
);
7755 btrfs_free_block_rsv(root
, rsv
);
7764 * create a new subvolume directory/inode (helper for the ioctl).
7766 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7767 struct btrfs_root
*new_root
, u64 new_dirid
)
7769 struct inode
*inode
;
7773 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7774 new_dirid
, new_dirid
,
7775 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7778 return PTR_ERR(inode
);
7779 inode
->i_op
= &btrfs_dir_inode_operations
;
7780 inode
->i_fop
= &btrfs_dir_file_operations
;
7782 set_nlink(inode
, 1);
7783 btrfs_i_size_write(inode
, 0);
7785 err
= btrfs_update_inode(trans
, new_root
, inode
);
7791 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7793 struct btrfs_inode
*ei
;
7794 struct inode
*inode
;
7796 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7803 ei
->last_sub_trans
= 0;
7804 ei
->logged_trans
= 0;
7805 ei
->delalloc_bytes
= 0;
7806 ei
->disk_i_size
= 0;
7809 ei
->index_cnt
= (u64
)-1;
7810 ei
->last_unlink_trans
= 0;
7811 ei
->last_log_commit
= 0;
7813 spin_lock_init(&ei
->lock
);
7814 ei
->outstanding_extents
= 0;
7815 ei
->reserved_extents
= 0;
7817 ei
->runtime_flags
= 0;
7818 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7820 ei
->delayed_node
= NULL
;
7822 inode
= &ei
->vfs_inode
;
7823 extent_map_tree_init(&ei
->extent_tree
);
7824 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7825 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7826 ei
->io_tree
.track_uptodate
= 1;
7827 ei
->io_failure_tree
.track_uptodate
= 1;
7828 atomic_set(&ei
->sync_writers
, 0);
7829 mutex_init(&ei
->log_mutex
);
7830 mutex_init(&ei
->delalloc_mutex
);
7831 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7832 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7833 INIT_LIST_HEAD(&ei
->ordered_operations
);
7834 RB_CLEAR_NODE(&ei
->rb_node
);
7839 static void btrfs_i_callback(struct rcu_head
*head
)
7841 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7842 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7845 void btrfs_destroy_inode(struct inode
*inode
)
7847 struct btrfs_ordered_extent
*ordered
;
7848 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7850 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7851 WARN_ON(inode
->i_data
.nrpages
);
7852 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7853 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7854 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7855 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7858 * This can happen where we create an inode, but somebody else also
7859 * created the same inode and we need to destroy the one we already
7866 * Make sure we're properly removed from the ordered operation
7870 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7871 spin_lock(&root
->fs_info
->ordered_root_lock
);
7872 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7873 spin_unlock(&root
->fs_info
->ordered_root_lock
);
7876 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7877 &BTRFS_I(inode
)->runtime_flags
)) {
7878 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
7879 (unsigned long long)btrfs_ino(inode
));
7880 atomic_dec(&root
->orphan_inodes
);
7884 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7888 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
7889 (unsigned long long)ordered
->file_offset
,
7890 (unsigned long long)ordered
->len
);
7891 btrfs_remove_ordered_extent(inode
, ordered
);
7892 btrfs_put_ordered_extent(ordered
);
7893 btrfs_put_ordered_extent(ordered
);
7896 inode_tree_del(inode
);
7897 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7899 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7902 int btrfs_drop_inode(struct inode
*inode
)
7904 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7909 /* the snap/subvol tree is on deleting */
7910 if (btrfs_root_refs(&root
->root_item
) == 0 &&
7911 root
!= root
->fs_info
->tree_root
)
7914 return generic_drop_inode(inode
);
7917 static void init_once(void *foo
)
7919 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7921 inode_init_once(&ei
->vfs_inode
);
7924 void btrfs_destroy_cachep(void)
7927 * Make sure all delayed rcu free inodes are flushed before we
7931 if (btrfs_inode_cachep
)
7932 kmem_cache_destroy(btrfs_inode_cachep
);
7933 if (btrfs_trans_handle_cachep
)
7934 kmem_cache_destroy(btrfs_trans_handle_cachep
);
7935 if (btrfs_transaction_cachep
)
7936 kmem_cache_destroy(btrfs_transaction_cachep
);
7937 if (btrfs_path_cachep
)
7938 kmem_cache_destroy(btrfs_path_cachep
);
7939 if (btrfs_free_space_cachep
)
7940 kmem_cache_destroy(btrfs_free_space_cachep
);
7941 if (btrfs_delalloc_work_cachep
)
7942 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
7945 int btrfs_init_cachep(void)
7947 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
7948 sizeof(struct btrfs_inode
), 0,
7949 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
7950 if (!btrfs_inode_cachep
)
7953 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
7954 sizeof(struct btrfs_trans_handle
), 0,
7955 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7956 if (!btrfs_trans_handle_cachep
)
7959 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
7960 sizeof(struct btrfs_transaction
), 0,
7961 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7962 if (!btrfs_transaction_cachep
)
7965 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
7966 sizeof(struct btrfs_path
), 0,
7967 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7968 if (!btrfs_path_cachep
)
7971 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
7972 sizeof(struct btrfs_free_space
), 0,
7973 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7974 if (!btrfs_free_space_cachep
)
7977 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
7978 sizeof(struct btrfs_delalloc_work
), 0,
7979 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
7981 if (!btrfs_delalloc_work_cachep
)
7986 btrfs_destroy_cachep();
7990 static int btrfs_getattr(struct vfsmount
*mnt
,
7991 struct dentry
*dentry
, struct kstat
*stat
)
7994 struct inode
*inode
= dentry
->d_inode
;
7995 u32 blocksize
= inode
->i_sb
->s_blocksize
;
7997 generic_fillattr(inode
, stat
);
7998 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
7999 stat
->blksize
= PAGE_CACHE_SIZE
;
8001 spin_lock(&BTRFS_I(inode
)->lock
);
8002 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
8003 spin_unlock(&BTRFS_I(inode
)->lock
);
8004 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
8005 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
8009 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
8010 struct inode
*new_dir
, struct dentry
*new_dentry
)
8012 struct btrfs_trans_handle
*trans
;
8013 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
8014 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
8015 struct inode
*new_inode
= new_dentry
->d_inode
;
8016 struct inode
*old_inode
= old_dentry
->d_inode
;
8017 struct timespec ctime
= CURRENT_TIME
;
8021 u64 old_ino
= btrfs_ino(old_inode
);
8023 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
8026 /* we only allow rename subvolume link between subvolumes */
8027 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8030 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
8031 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
8034 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
8035 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
8039 /* check for collisions, even if the name isn't there */
8040 ret
= btrfs_check_dir_item_collision(root
, new_dir
->i_ino
,
8041 new_dentry
->d_name
.name
,
8042 new_dentry
->d_name
.len
);
8045 if (ret
== -EEXIST
) {
8047 * eexist without a new_inode */
8053 /* maybe -EOVERFLOW */
8060 * we're using rename to replace one file with another.
8061 * and the replacement file is large. Start IO on it now so
8062 * we don't add too much work to the end of the transaction
8064 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
8065 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
8066 filemap_flush(old_inode
->i_mapping
);
8068 /* close the racy window with snapshot create/destroy ioctl */
8069 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8070 down_read(&root
->fs_info
->subvol_sem
);
8072 * We want to reserve the absolute worst case amount of items. So if
8073 * both inodes are subvols and we need to unlink them then that would
8074 * require 4 item modifications, but if they are both normal inodes it
8075 * would require 5 item modifications, so we'll assume their normal
8076 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8077 * should cover the worst case number of items we'll modify.
8079 trans
= btrfs_start_transaction(root
, 11);
8080 if (IS_ERR(trans
)) {
8081 ret
= PTR_ERR(trans
);
8086 btrfs_record_root_in_trans(trans
, dest
);
8088 ret
= btrfs_set_inode_index(new_dir
, &index
);
8092 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8093 /* force full log commit if subvolume involved. */
8094 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8096 ret
= btrfs_insert_inode_ref(trans
, dest
,
8097 new_dentry
->d_name
.name
,
8098 new_dentry
->d_name
.len
,
8100 btrfs_ino(new_dir
), index
);
8104 * this is an ugly little race, but the rename is required
8105 * to make sure that if we crash, the inode is either at the
8106 * old name or the new one. pinning the log transaction lets
8107 * us make sure we don't allow a log commit to come in after
8108 * we unlink the name but before we add the new name back in.
8110 btrfs_pin_log_trans(root
);
8113 * make sure the inode gets flushed if it is replacing
8116 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
8117 btrfs_add_ordered_operation(trans
, root
, old_inode
);
8119 inode_inc_iversion(old_dir
);
8120 inode_inc_iversion(new_dir
);
8121 inode_inc_iversion(old_inode
);
8122 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8123 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8124 old_inode
->i_ctime
= ctime
;
8126 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8127 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8129 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8130 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8131 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8132 old_dentry
->d_name
.name
,
8133 old_dentry
->d_name
.len
);
8135 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8136 old_dentry
->d_inode
,
8137 old_dentry
->d_name
.name
,
8138 old_dentry
->d_name
.len
);
8140 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8143 btrfs_abort_transaction(trans
, root
, ret
);
8148 inode_inc_iversion(new_inode
);
8149 new_inode
->i_ctime
= CURRENT_TIME
;
8150 if (unlikely(btrfs_ino(new_inode
) ==
8151 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8152 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8153 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
8155 new_dentry
->d_name
.name
,
8156 new_dentry
->d_name
.len
);
8157 BUG_ON(new_inode
->i_nlink
== 0);
8159 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
8160 new_dentry
->d_inode
,
8161 new_dentry
->d_name
.name
,
8162 new_dentry
->d_name
.len
);
8164 if (!ret
&& new_inode
->i_nlink
== 0) {
8165 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
8169 btrfs_abort_transaction(trans
, root
, ret
);
8174 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
8175 new_dentry
->d_name
.name
,
8176 new_dentry
->d_name
.len
, 0, index
);
8178 btrfs_abort_transaction(trans
, root
, ret
);
8182 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
8183 struct dentry
*parent
= new_dentry
->d_parent
;
8184 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
8185 btrfs_end_log_trans(root
);
8188 btrfs_end_transaction(trans
, root
);
8190 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8191 up_read(&root
->fs_info
->subvol_sem
);
8196 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
8198 struct btrfs_delalloc_work
*delalloc_work
;
8200 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
8202 if (delalloc_work
->wait
)
8203 btrfs_wait_ordered_range(delalloc_work
->inode
, 0, (u64
)-1);
8205 filemap_flush(delalloc_work
->inode
->i_mapping
);
8207 if (delalloc_work
->delay_iput
)
8208 btrfs_add_delayed_iput(delalloc_work
->inode
);
8210 iput(delalloc_work
->inode
);
8211 complete(&delalloc_work
->completion
);
8214 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
8215 int wait
, int delay_iput
)
8217 struct btrfs_delalloc_work
*work
;
8219 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
8223 init_completion(&work
->completion
);
8224 INIT_LIST_HEAD(&work
->list
);
8225 work
->inode
= inode
;
8227 work
->delay_iput
= delay_iput
;
8228 work
->work
.func
= btrfs_run_delalloc_work
;
8233 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
8235 wait_for_completion(&work
->completion
);
8236 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
8240 * some fairly slow code that needs optimization. This walks the list
8241 * of all the inodes with pending delalloc and forces them to disk.
8243 static int __start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8245 struct btrfs_inode
*binode
;
8246 struct inode
*inode
;
8247 struct btrfs_delalloc_work
*work
, *next
;
8248 struct list_head works
;
8249 struct list_head splice
;
8252 INIT_LIST_HEAD(&works
);
8253 INIT_LIST_HEAD(&splice
);
8255 spin_lock(&root
->delalloc_lock
);
8256 list_splice_init(&root
->delalloc_inodes
, &splice
);
8257 while (!list_empty(&splice
)) {
8258 binode
= list_entry(splice
.next
, struct btrfs_inode
,
8261 list_move_tail(&binode
->delalloc_inodes
,
8262 &root
->delalloc_inodes
);
8263 inode
= igrab(&binode
->vfs_inode
);
8265 cond_resched_lock(&root
->delalloc_lock
);
8268 spin_unlock(&root
->delalloc_lock
);
8270 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
8271 if (unlikely(!work
)) {
8275 list_add_tail(&work
->list
, &works
);
8276 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
8280 spin_lock(&root
->delalloc_lock
);
8282 spin_unlock(&root
->delalloc_lock
);
8284 list_for_each_entry_safe(work
, next
, &works
, list
) {
8285 list_del_init(&work
->list
);
8286 btrfs_wait_and_free_delalloc_work(work
);
8290 list_for_each_entry_safe(work
, next
, &works
, list
) {
8291 list_del_init(&work
->list
);
8292 btrfs_wait_and_free_delalloc_work(work
);
8295 if (!list_empty_careful(&splice
)) {
8296 spin_lock(&root
->delalloc_lock
);
8297 list_splice_tail(&splice
, &root
->delalloc_inodes
);
8298 spin_unlock(&root
->delalloc_lock
);
8303 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8307 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
8310 ret
= __start_delalloc_inodes(root
, delay_iput
);
8312 * the filemap_flush will queue IO into the worker threads, but
8313 * we have to make sure the IO is actually started and that
8314 * ordered extents get created before we return
8316 atomic_inc(&root
->fs_info
->async_submit_draining
);
8317 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
8318 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
8319 wait_event(root
->fs_info
->async_submit_wait
,
8320 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
8321 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
8323 atomic_dec(&root
->fs_info
->async_submit_draining
);
8327 int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info
*fs_info
,
8330 struct btrfs_root
*root
;
8331 struct list_head splice
;
8334 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
8337 INIT_LIST_HEAD(&splice
);
8339 spin_lock(&fs_info
->delalloc_root_lock
);
8340 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
8341 while (!list_empty(&splice
)) {
8342 root
= list_first_entry(&splice
, struct btrfs_root
,
8344 root
= btrfs_grab_fs_root(root
);
8346 list_move_tail(&root
->delalloc_root
,
8347 &fs_info
->delalloc_roots
);
8348 spin_unlock(&fs_info
->delalloc_root_lock
);
8350 ret
= __start_delalloc_inodes(root
, delay_iput
);
8351 btrfs_put_fs_root(root
);
8355 spin_lock(&fs_info
->delalloc_root_lock
);
8357 spin_unlock(&fs_info
->delalloc_root_lock
);
8359 atomic_inc(&fs_info
->async_submit_draining
);
8360 while (atomic_read(&fs_info
->nr_async_submits
) ||
8361 atomic_read(&fs_info
->async_delalloc_pages
)) {
8362 wait_event(fs_info
->async_submit_wait
,
8363 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
8364 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
8366 atomic_dec(&fs_info
->async_submit_draining
);
8369 if (!list_empty_careful(&splice
)) {
8370 spin_lock(&fs_info
->delalloc_root_lock
);
8371 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
8372 spin_unlock(&fs_info
->delalloc_root_lock
);
8377 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
8378 const char *symname
)
8380 struct btrfs_trans_handle
*trans
;
8381 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8382 struct btrfs_path
*path
;
8383 struct btrfs_key key
;
8384 struct inode
*inode
= NULL
;
8392 struct btrfs_file_extent_item
*ei
;
8393 struct extent_buffer
*leaf
;
8395 name_len
= strlen(symname
) + 1;
8396 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
8397 return -ENAMETOOLONG
;
8400 * 2 items for inode item and ref
8401 * 2 items for dir items
8402 * 1 item for xattr if selinux is on
8404 trans
= btrfs_start_transaction(root
, 5);
8406 return PTR_ERR(trans
);
8408 err
= btrfs_find_free_ino(root
, &objectid
);
8412 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
8413 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
8414 S_IFLNK
|S_IRWXUGO
, &index
);
8415 if (IS_ERR(inode
)) {
8416 err
= PTR_ERR(inode
);
8420 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
8427 * If the active LSM wants to access the inode during
8428 * d_instantiate it needs these. Smack checks to see
8429 * if the filesystem supports xattrs by looking at the
8432 inode
->i_fop
= &btrfs_file_operations
;
8433 inode
->i_op
= &btrfs_file_inode_operations
;
8435 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
8439 inode
->i_mapping
->a_ops
= &btrfs_aops
;
8440 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8441 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
8446 path
= btrfs_alloc_path();
8452 key
.objectid
= btrfs_ino(inode
);
8454 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
8455 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
8456 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
8460 btrfs_free_path(path
);
8463 leaf
= path
->nodes
[0];
8464 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
8465 struct btrfs_file_extent_item
);
8466 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
8467 btrfs_set_file_extent_type(leaf
, ei
,
8468 BTRFS_FILE_EXTENT_INLINE
);
8469 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
8470 btrfs_set_file_extent_compression(leaf
, ei
, 0);
8471 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
8472 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
8474 ptr
= btrfs_file_extent_inline_start(ei
);
8475 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
8476 btrfs_mark_buffer_dirty(leaf
);
8477 btrfs_free_path(path
);
8479 inode
->i_op
= &btrfs_symlink_inode_operations
;
8480 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
8481 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8482 inode_set_bytes(inode
, name_len
);
8483 btrfs_i_size_write(inode
, name_len
- 1);
8484 err
= btrfs_update_inode(trans
, root
, inode
);
8490 d_instantiate(dentry
, inode
);
8491 btrfs_end_transaction(trans
, root
);
8493 inode_dec_link_count(inode
);
8496 btrfs_btree_balance_dirty(root
);
8500 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8501 u64 start
, u64 num_bytes
, u64 min_size
,
8502 loff_t actual_len
, u64
*alloc_hint
,
8503 struct btrfs_trans_handle
*trans
)
8505 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
8506 struct extent_map
*em
;
8507 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8508 struct btrfs_key ins
;
8509 u64 cur_offset
= start
;
8513 bool own_trans
= true;
8517 while (num_bytes
> 0) {
8519 trans
= btrfs_start_transaction(root
, 3);
8520 if (IS_ERR(trans
)) {
8521 ret
= PTR_ERR(trans
);
8526 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
8527 cur_bytes
= max(cur_bytes
, min_size
);
8528 ret
= btrfs_reserve_extent(trans
, root
, cur_bytes
,
8529 min_size
, 0, *alloc_hint
, &ins
, 1);
8532 btrfs_end_transaction(trans
, root
);
8536 ret
= insert_reserved_file_extent(trans
, inode
,
8537 cur_offset
, ins
.objectid
,
8538 ins
.offset
, ins
.offset
,
8539 ins
.offset
, 0, 0, 0,
8540 BTRFS_FILE_EXTENT_PREALLOC
);
8542 btrfs_abort_transaction(trans
, root
, ret
);
8544 btrfs_end_transaction(trans
, root
);
8547 btrfs_drop_extent_cache(inode
, cur_offset
,
8548 cur_offset
+ ins
.offset
-1, 0);
8550 em
= alloc_extent_map();
8552 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
8553 &BTRFS_I(inode
)->runtime_flags
);
8557 em
->start
= cur_offset
;
8558 em
->orig_start
= cur_offset
;
8559 em
->len
= ins
.offset
;
8560 em
->block_start
= ins
.objectid
;
8561 em
->block_len
= ins
.offset
;
8562 em
->orig_block_len
= ins
.offset
;
8563 em
->ram_bytes
= ins
.offset
;
8564 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
8565 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
8566 em
->generation
= trans
->transid
;
8569 write_lock(&em_tree
->lock
);
8570 ret
= add_extent_mapping(em_tree
, em
, 1);
8571 write_unlock(&em_tree
->lock
);
8574 btrfs_drop_extent_cache(inode
, cur_offset
,
8575 cur_offset
+ ins
.offset
- 1,
8578 free_extent_map(em
);
8580 num_bytes
-= ins
.offset
;
8581 cur_offset
+= ins
.offset
;
8582 *alloc_hint
= ins
.objectid
+ ins
.offset
;
8584 inode_inc_iversion(inode
);
8585 inode
->i_ctime
= CURRENT_TIME
;
8586 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
8587 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
8588 (actual_len
> inode
->i_size
) &&
8589 (cur_offset
> inode
->i_size
)) {
8590 if (cur_offset
> actual_len
)
8591 i_size
= actual_len
;
8593 i_size
= cur_offset
;
8594 i_size_write(inode
, i_size
);
8595 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
8598 ret
= btrfs_update_inode(trans
, root
, inode
);
8601 btrfs_abort_transaction(trans
, root
, ret
);
8603 btrfs_end_transaction(trans
, root
);
8608 btrfs_end_transaction(trans
, root
);
8613 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8614 u64 start
, u64 num_bytes
, u64 min_size
,
8615 loff_t actual_len
, u64
*alloc_hint
)
8617 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8618 min_size
, actual_len
, alloc_hint
,
8622 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
8623 struct btrfs_trans_handle
*trans
, int mode
,
8624 u64 start
, u64 num_bytes
, u64 min_size
,
8625 loff_t actual_len
, u64
*alloc_hint
)
8627 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8628 min_size
, actual_len
, alloc_hint
, trans
);
8631 static int btrfs_set_page_dirty(struct page
*page
)
8633 return __set_page_dirty_nobuffers(page
);
8636 static int btrfs_permission(struct inode
*inode
, int mask
)
8638 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8639 umode_t mode
= inode
->i_mode
;
8641 if (mask
& MAY_WRITE
&&
8642 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
8643 if (btrfs_root_readonly(root
))
8645 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
8648 return generic_permission(inode
, mask
);
8651 static const struct inode_operations btrfs_dir_inode_operations
= {
8652 .getattr
= btrfs_getattr
,
8653 .lookup
= btrfs_lookup
,
8654 .create
= btrfs_create
,
8655 .unlink
= btrfs_unlink
,
8657 .mkdir
= btrfs_mkdir
,
8658 .rmdir
= btrfs_rmdir
,
8659 .rename
= btrfs_rename
,
8660 .symlink
= btrfs_symlink
,
8661 .setattr
= btrfs_setattr
,
8662 .mknod
= btrfs_mknod
,
8663 .setxattr
= btrfs_setxattr
,
8664 .getxattr
= btrfs_getxattr
,
8665 .listxattr
= btrfs_listxattr
,
8666 .removexattr
= btrfs_removexattr
,
8667 .permission
= btrfs_permission
,
8668 .get_acl
= btrfs_get_acl
,
8670 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
8671 .lookup
= btrfs_lookup
,
8672 .permission
= btrfs_permission
,
8673 .get_acl
= btrfs_get_acl
,
8676 static const struct file_operations btrfs_dir_file_operations
= {
8677 .llseek
= generic_file_llseek
,
8678 .read
= generic_read_dir
,
8679 .iterate
= btrfs_real_readdir
,
8680 .unlocked_ioctl
= btrfs_ioctl
,
8681 #ifdef CONFIG_COMPAT
8682 .compat_ioctl
= btrfs_ioctl
,
8684 .release
= btrfs_release_file
,
8685 .fsync
= btrfs_sync_file
,
8688 static struct extent_io_ops btrfs_extent_io_ops
= {
8689 .fill_delalloc
= run_delalloc_range
,
8690 .submit_bio_hook
= btrfs_submit_bio_hook
,
8691 .merge_bio_hook
= btrfs_merge_bio_hook
,
8692 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
8693 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
8694 .writepage_start_hook
= btrfs_writepage_start_hook
,
8695 .set_bit_hook
= btrfs_set_bit_hook
,
8696 .clear_bit_hook
= btrfs_clear_bit_hook
,
8697 .merge_extent_hook
= btrfs_merge_extent_hook
,
8698 .split_extent_hook
= btrfs_split_extent_hook
,
8702 * btrfs doesn't support the bmap operation because swapfiles
8703 * use bmap to make a mapping of extents in the file. They assume
8704 * these extents won't change over the life of the file and they
8705 * use the bmap result to do IO directly to the drive.
8707 * the btrfs bmap call would return logical addresses that aren't
8708 * suitable for IO and they also will change frequently as COW
8709 * operations happen. So, swapfile + btrfs == corruption.
8711 * For now we're avoiding this by dropping bmap.
8713 static const struct address_space_operations btrfs_aops
= {
8714 .readpage
= btrfs_readpage
,
8715 .writepage
= btrfs_writepage
,
8716 .writepages
= btrfs_writepages
,
8717 .readpages
= btrfs_readpages
,
8718 .direct_IO
= btrfs_direct_IO
,
8719 .invalidatepage
= btrfs_invalidatepage
,
8720 .releasepage
= btrfs_releasepage
,
8721 .set_page_dirty
= btrfs_set_page_dirty
,
8722 .error_remove_page
= generic_error_remove_page
,
8725 static const struct address_space_operations btrfs_symlink_aops
= {
8726 .readpage
= btrfs_readpage
,
8727 .writepage
= btrfs_writepage
,
8728 .invalidatepage
= btrfs_invalidatepage
,
8729 .releasepage
= btrfs_releasepage
,
8732 static const struct inode_operations btrfs_file_inode_operations
= {
8733 .getattr
= btrfs_getattr
,
8734 .setattr
= btrfs_setattr
,
8735 .setxattr
= btrfs_setxattr
,
8736 .getxattr
= btrfs_getxattr
,
8737 .listxattr
= btrfs_listxattr
,
8738 .removexattr
= btrfs_removexattr
,
8739 .permission
= btrfs_permission
,
8740 .fiemap
= btrfs_fiemap
,
8741 .get_acl
= btrfs_get_acl
,
8742 .update_time
= btrfs_update_time
,
8744 static const struct inode_operations btrfs_special_inode_operations
= {
8745 .getattr
= btrfs_getattr
,
8746 .setattr
= btrfs_setattr
,
8747 .permission
= btrfs_permission
,
8748 .setxattr
= btrfs_setxattr
,
8749 .getxattr
= btrfs_getxattr
,
8750 .listxattr
= btrfs_listxattr
,
8751 .removexattr
= btrfs_removexattr
,
8752 .get_acl
= btrfs_get_acl
,
8753 .update_time
= btrfs_update_time
,
8755 static const struct inode_operations btrfs_symlink_inode_operations
= {
8756 .readlink
= generic_readlink
,
8757 .follow_link
= page_follow_link_light
,
8758 .put_link
= page_put_link
,
8759 .getattr
= btrfs_getattr
,
8760 .setattr
= btrfs_setattr
,
8761 .permission
= btrfs_permission
,
8762 .setxattr
= btrfs_setxattr
,
8763 .getxattr
= btrfs_getxattr
,
8764 .listxattr
= btrfs_listxattr
,
8765 .removexattr
= btrfs_removexattr
,
8766 .get_acl
= btrfs_get_acl
,
8767 .update_time
= btrfs_update_time
,
8770 const struct dentry_operations btrfs_dentry_operations
= {
8771 .d_delete
= btrfs_dentry_delete
,
8772 .d_release
= btrfs_dentry_release
,