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/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
40 #include <linux/ratelimit.h>
41 #include <linux/mount.h>
42 #include <linux/btrfs.h>
43 #include <linux/blkdev.h>
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "print-tree.h"
50 #include "ordered-data.h"
54 #include "compression.h"
56 #include "free-space-cache.h"
57 #include "inode-map.h"
60 struct btrfs_iget_args
{
62 struct btrfs_root
*root
;
65 static const struct inode_operations btrfs_dir_inode_operations
;
66 static const struct inode_operations btrfs_symlink_inode_operations
;
67 static const struct inode_operations btrfs_dir_ro_inode_operations
;
68 static const struct inode_operations btrfs_special_inode_operations
;
69 static const struct inode_operations btrfs_file_inode_operations
;
70 static const struct address_space_operations btrfs_aops
;
71 static const struct address_space_operations btrfs_symlink_aops
;
72 static const struct file_operations btrfs_dir_file_operations
;
73 static struct extent_io_ops btrfs_extent_io_ops
;
75 static struct kmem_cache
*btrfs_inode_cachep
;
76 static struct kmem_cache
*btrfs_delalloc_work_cachep
;
77 struct kmem_cache
*btrfs_trans_handle_cachep
;
78 struct kmem_cache
*btrfs_transaction_cachep
;
79 struct kmem_cache
*btrfs_path_cachep
;
80 struct kmem_cache
*btrfs_free_space_cachep
;
83 static unsigned char btrfs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
84 [S_IFREG
>> S_SHIFT
] = BTRFS_FT_REG_FILE
,
85 [S_IFDIR
>> S_SHIFT
] = BTRFS_FT_DIR
,
86 [S_IFCHR
>> S_SHIFT
] = BTRFS_FT_CHRDEV
,
87 [S_IFBLK
>> S_SHIFT
] = BTRFS_FT_BLKDEV
,
88 [S_IFIFO
>> S_SHIFT
] = BTRFS_FT_FIFO
,
89 [S_IFSOCK
>> S_SHIFT
] = BTRFS_FT_SOCK
,
90 [S_IFLNK
>> S_SHIFT
] = BTRFS_FT_SYMLINK
,
93 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
);
94 static int btrfs_truncate(struct inode
*inode
);
95 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
);
96 static noinline
int cow_file_range(struct inode
*inode
,
97 struct page
*locked_page
,
98 u64 start
, u64 end
, int *page_started
,
99 unsigned long *nr_written
, int unlock
);
100 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
101 u64 len
, u64 orig_start
,
102 u64 block_start
, u64 block_len
,
103 u64 orig_block_len
, u64 ram_bytes
,
106 static int btrfs_dirty_inode(struct inode
*inode
);
108 static int btrfs_init_inode_security(struct btrfs_trans_handle
*trans
,
109 struct inode
*inode
, struct inode
*dir
,
110 const struct qstr
*qstr
)
114 err
= btrfs_init_acl(trans
, inode
, dir
);
116 err
= btrfs_xattr_security_init(trans
, inode
, dir
, qstr
);
121 * this does all the hard work for inserting an inline extent into
122 * the btree. The caller should have done a btrfs_drop_extents so that
123 * no overlapping inline items exist in the btree
125 static noinline
int insert_inline_extent(struct btrfs_trans_handle
*trans
,
126 struct btrfs_root
*root
, struct inode
*inode
,
127 u64 start
, size_t size
, size_t compressed_size
,
129 struct page
**compressed_pages
)
131 struct btrfs_key key
;
132 struct btrfs_path
*path
;
133 struct extent_buffer
*leaf
;
134 struct page
*page
= NULL
;
137 struct btrfs_file_extent_item
*ei
;
140 size_t cur_size
= size
;
142 unsigned long offset
;
144 if (compressed_size
&& compressed_pages
)
145 cur_size
= compressed_size
;
147 path
= btrfs_alloc_path();
151 path
->leave_spinning
= 1;
153 key
.objectid
= btrfs_ino(inode
);
155 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
156 datasize
= btrfs_file_extent_calc_inline_size(cur_size
);
158 inode_add_bytes(inode
, size
);
159 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
165 leaf
= path
->nodes
[0];
166 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
167 struct btrfs_file_extent_item
);
168 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
169 btrfs_set_file_extent_type(leaf
, ei
, BTRFS_FILE_EXTENT_INLINE
);
170 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
171 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
172 btrfs_set_file_extent_ram_bytes(leaf
, ei
, size
);
173 ptr
= btrfs_file_extent_inline_start(ei
);
175 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
178 while (compressed_size
> 0) {
179 cpage
= compressed_pages
[i
];
180 cur_size
= min_t(unsigned long, compressed_size
,
183 kaddr
= kmap_atomic(cpage
);
184 write_extent_buffer(leaf
, kaddr
, ptr
, cur_size
);
185 kunmap_atomic(kaddr
);
189 compressed_size
-= cur_size
;
191 btrfs_set_file_extent_compression(leaf
, ei
,
194 page
= find_get_page(inode
->i_mapping
,
195 start
>> PAGE_CACHE_SHIFT
);
196 btrfs_set_file_extent_compression(leaf
, ei
, 0);
197 kaddr
= kmap_atomic(page
);
198 offset
= start
& (PAGE_CACHE_SIZE
- 1);
199 write_extent_buffer(leaf
, kaddr
+ offset
, ptr
, size
);
200 kunmap_atomic(kaddr
);
201 page_cache_release(page
);
203 btrfs_mark_buffer_dirty(leaf
);
204 btrfs_free_path(path
);
207 * we're an inline extent, so nobody can
208 * extend the file past i_size without locking
209 * a page we already have locked.
211 * We must do any isize and inode updates
212 * before we unlock the pages. Otherwise we
213 * could end up racing with unlink.
215 BTRFS_I(inode
)->disk_i_size
= inode
->i_size
;
216 ret
= btrfs_update_inode(trans
, root
, inode
);
220 btrfs_free_path(path
);
226 * conditionally insert an inline extent into the file. This
227 * does the checks required to make sure the data is small enough
228 * to fit as an inline extent.
230 static noinline
int cow_file_range_inline(struct btrfs_trans_handle
*trans
,
231 struct btrfs_root
*root
,
232 struct inode
*inode
, u64 start
, u64 end
,
233 size_t compressed_size
, int compress_type
,
234 struct page
**compressed_pages
)
236 u64 isize
= i_size_read(inode
);
237 u64 actual_end
= min(end
+ 1, isize
);
238 u64 inline_len
= actual_end
- start
;
239 u64 aligned_end
= ALIGN(end
, root
->sectorsize
);
240 u64 data_len
= inline_len
;
244 data_len
= compressed_size
;
247 actual_end
>= PAGE_CACHE_SIZE
||
248 data_len
>= BTRFS_MAX_INLINE_DATA_SIZE(root
) ||
250 (actual_end
& (root
->sectorsize
- 1)) == 0) ||
252 data_len
> root
->fs_info
->max_inline
) {
256 ret
= btrfs_drop_extents(trans
, root
, inode
, start
, aligned_end
, 1);
260 if (isize
> actual_end
)
261 inline_len
= min_t(u64
, isize
, actual_end
);
262 ret
= insert_inline_extent(trans
, root
, inode
, start
,
263 inline_len
, compressed_size
,
264 compress_type
, compressed_pages
);
265 if (ret
&& ret
!= -ENOSPC
) {
266 btrfs_abort_transaction(trans
, root
, ret
);
268 } else if (ret
== -ENOSPC
) {
272 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
273 btrfs_delalloc_release_metadata(inode
, end
+ 1 - start
);
274 btrfs_drop_extent_cache(inode
, start
, aligned_end
- 1, 0);
278 struct async_extent
{
283 unsigned long nr_pages
;
285 struct list_head list
;
290 struct btrfs_root
*root
;
291 struct page
*locked_page
;
294 struct list_head extents
;
295 struct btrfs_work work
;
298 static noinline
int add_async_extent(struct async_cow
*cow
,
299 u64 start
, u64 ram_size
,
302 unsigned long nr_pages
,
305 struct async_extent
*async_extent
;
307 async_extent
= kmalloc(sizeof(*async_extent
), GFP_NOFS
);
308 BUG_ON(!async_extent
); /* -ENOMEM */
309 async_extent
->start
= start
;
310 async_extent
->ram_size
= ram_size
;
311 async_extent
->compressed_size
= compressed_size
;
312 async_extent
->pages
= pages
;
313 async_extent
->nr_pages
= nr_pages
;
314 async_extent
->compress_type
= compress_type
;
315 list_add_tail(&async_extent
->list
, &cow
->extents
);
320 * we create compressed extents in two phases. The first
321 * phase compresses a range of pages that have already been
322 * locked (both pages and state bits are locked).
324 * This is done inside an ordered work queue, and the compression
325 * is spread across many cpus. The actual IO submission is step
326 * two, and the ordered work queue takes care of making sure that
327 * happens in the same order things were put onto the queue by
328 * writepages and friends.
330 * If this code finds it can't get good compression, it puts an
331 * entry onto the work queue to write the uncompressed bytes. This
332 * makes sure that both compressed inodes and uncompressed inodes
333 * are written in the same order that the flusher thread sent them
336 static noinline
int compress_file_range(struct inode
*inode
,
337 struct page
*locked_page
,
339 struct async_cow
*async_cow
,
342 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
343 struct btrfs_trans_handle
*trans
;
345 u64 blocksize
= root
->sectorsize
;
347 u64 isize
= i_size_read(inode
);
349 struct page
**pages
= NULL
;
350 unsigned long nr_pages
;
351 unsigned long nr_pages_ret
= 0;
352 unsigned long total_compressed
= 0;
353 unsigned long total_in
= 0;
354 unsigned long max_compressed
= 128 * 1024;
355 unsigned long max_uncompressed
= 128 * 1024;
358 int compress_type
= root
->fs_info
->compress_type
;
361 /* if this is a small write inside eof, kick off a defrag */
362 if ((end
- start
+ 1) < 16 * 1024 &&
363 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
364 btrfs_add_inode_defrag(NULL
, inode
);
366 actual_end
= min_t(u64
, isize
, end
+ 1);
369 nr_pages
= (end
>> PAGE_CACHE_SHIFT
) - (start
>> PAGE_CACHE_SHIFT
) + 1;
370 nr_pages
= min(nr_pages
, (128 * 1024UL) / PAGE_CACHE_SIZE
);
373 * we don't want to send crud past the end of i_size through
374 * compression, that's just a waste of CPU time. So, if the
375 * end of the file is before the start of our current
376 * requested range of bytes, we bail out to the uncompressed
377 * cleanup code that can deal with all of this.
379 * It isn't really the fastest way to fix things, but this is a
380 * very uncommon corner.
382 if (actual_end
<= start
)
383 goto cleanup_and_bail_uncompressed
;
385 total_compressed
= actual_end
- start
;
387 /* we want to make sure that amount of ram required to uncompress
388 * an extent is reasonable, so we limit the total size in ram
389 * of a compressed extent to 128k. This is a crucial number
390 * because it also controls how easily we can spread reads across
391 * cpus for decompression.
393 * We also want to make sure the amount of IO required to do
394 * a random read is reasonably small, so we limit the size of
395 * a compressed extent to 128k.
397 total_compressed
= min(total_compressed
, max_uncompressed
);
398 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
399 num_bytes
= max(blocksize
, num_bytes
);
404 * we do compression for mount -o compress and when the
405 * inode has not been flagged as nocompress. This flag can
406 * change at any time if we discover bad compression ratios.
408 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
) &&
409 (btrfs_test_opt(root
, COMPRESS
) ||
410 (BTRFS_I(inode
)->force_compress
) ||
411 (BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
))) {
413 pages
= kzalloc(sizeof(struct page
*) * nr_pages
, GFP_NOFS
);
415 /* just bail out to the uncompressed code */
419 if (BTRFS_I(inode
)->force_compress
)
420 compress_type
= BTRFS_I(inode
)->force_compress
;
423 * we need to call clear_page_dirty_for_io on each
424 * page in the range. Otherwise applications with the file
425 * mmap'd can wander in and change the page contents while
426 * we are compressing them.
428 * If the compression fails for any reason, we set the pages
429 * dirty again later on.
431 extent_range_clear_dirty_for_io(inode
, start
, end
);
433 ret
= btrfs_compress_pages(compress_type
,
434 inode
->i_mapping
, start
,
435 total_compressed
, pages
,
436 nr_pages
, &nr_pages_ret
,
442 unsigned long offset
= total_compressed
&
443 (PAGE_CACHE_SIZE
- 1);
444 struct page
*page
= pages
[nr_pages_ret
- 1];
447 /* zero the tail end of the last page, we might be
448 * sending it down to disk
451 kaddr
= kmap_atomic(page
);
452 memset(kaddr
+ offset
, 0,
453 PAGE_CACHE_SIZE
- offset
);
454 kunmap_atomic(kaddr
);
461 trans
= btrfs_join_transaction(root
);
463 ret
= PTR_ERR(trans
);
465 goto cleanup_and_out
;
467 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
469 /* lets try to make an inline extent */
470 if (ret
|| total_in
< (actual_end
- start
)) {
471 /* we didn't compress the entire range, try
472 * to make an uncompressed inline extent.
474 ret
= cow_file_range_inline(trans
, root
, inode
,
475 start
, end
, 0, 0, NULL
);
477 /* try making a compressed inline extent */
478 ret
= cow_file_range_inline(trans
, root
, inode
,
481 compress_type
, pages
);
485 * inline extent creation worked or returned error,
486 * we don't need to create any more async work items.
487 * Unlock and free up our temp pages.
489 extent_clear_unlock_delalloc(inode
,
490 &BTRFS_I(inode
)->io_tree
,
492 EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
493 EXTENT_CLEAR_DELALLOC
|
494 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
);
496 btrfs_end_transaction(trans
, root
);
499 btrfs_end_transaction(trans
, root
);
504 * we aren't doing an inline extent round the compressed size
505 * up to a block size boundary so the allocator does sane
508 total_compressed
= ALIGN(total_compressed
, blocksize
);
511 * one last check to make sure the compression is really a
512 * win, compare the page count read with the blocks on disk
514 total_in
= ALIGN(total_in
, PAGE_CACHE_SIZE
);
515 if (total_compressed
>= total_in
) {
518 num_bytes
= total_in
;
521 if (!will_compress
&& pages
) {
523 * the compression code ran but failed to make things smaller,
524 * free any pages it allocated and our page pointer array
526 for (i
= 0; i
< nr_pages_ret
; i
++) {
527 WARN_ON(pages
[i
]->mapping
);
528 page_cache_release(pages
[i
]);
532 total_compressed
= 0;
535 /* flag the file so we don't compress in the future */
536 if (!btrfs_test_opt(root
, FORCE_COMPRESS
) &&
537 !(BTRFS_I(inode
)->force_compress
)) {
538 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NOCOMPRESS
;
544 /* the async work queues will take care of doing actual
545 * allocation on disk for these compressed pages,
546 * and will submit them to the elevator.
548 add_async_extent(async_cow
, start
, num_bytes
,
549 total_compressed
, pages
, nr_pages_ret
,
552 if (start
+ num_bytes
< end
) {
559 cleanup_and_bail_uncompressed
:
561 * No compression, but we still need to write the pages in
562 * the file we've been given so far. redirty the locked
563 * page if it corresponds to our extent and set things up
564 * for the async work queue to run cow_file_range to do
565 * the normal delalloc dance
567 if (page_offset(locked_page
) >= start
&&
568 page_offset(locked_page
) <= end
) {
569 __set_page_dirty_nobuffers(locked_page
);
570 /* unlocked later on in the async handlers */
573 extent_range_redirty_for_io(inode
, start
, end
);
574 add_async_extent(async_cow
, start
, end
- start
+ 1,
575 0, NULL
, 0, BTRFS_COMPRESS_NONE
);
583 for (i
= 0; i
< nr_pages_ret
; i
++) {
584 WARN_ON(pages
[i
]->mapping
);
585 page_cache_release(pages
[i
]);
592 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
594 EXTENT_CLEAR_UNLOCK_PAGE
|
596 EXTENT_CLEAR_DELALLOC
|
597 EXTENT_SET_WRITEBACK
|
598 EXTENT_END_WRITEBACK
);
599 if (!trans
|| IS_ERR(trans
))
600 btrfs_error(root
->fs_info
, ret
, "Failed to join transaction");
602 btrfs_abort_transaction(trans
, root
, ret
);
607 * phase two of compressed writeback. This is the ordered portion
608 * of the code, which only gets called in the order the work was
609 * queued. We walk all the async extents created by compress_file_range
610 * and send them down to the disk.
612 static noinline
int submit_compressed_extents(struct inode
*inode
,
613 struct async_cow
*async_cow
)
615 struct async_extent
*async_extent
;
617 struct btrfs_trans_handle
*trans
;
618 struct btrfs_key ins
;
619 struct extent_map
*em
;
620 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
621 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
622 struct extent_io_tree
*io_tree
;
625 if (list_empty(&async_cow
->extents
))
629 while (!list_empty(&async_cow
->extents
)) {
630 async_extent
= list_entry(async_cow
->extents
.next
,
631 struct async_extent
, list
);
632 list_del(&async_extent
->list
);
634 io_tree
= &BTRFS_I(inode
)->io_tree
;
637 /* did the compression code fall back to uncompressed IO? */
638 if (!async_extent
->pages
) {
639 int page_started
= 0;
640 unsigned long nr_written
= 0;
642 lock_extent(io_tree
, async_extent
->start
,
643 async_extent
->start
+
644 async_extent
->ram_size
- 1);
646 /* allocate blocks */
647 ret
= cow_file_range(inode
, async_cow
->locked_page
,
649 async_extent
->start
+
650 async_extent
->ram_size
- 1,
651 &page_started
, &nr_written
, 0);
656 * if page_started, cow_file_range inserted an
657 * inline extent and took care of all the unlocking
658 * and IO for us. Otherwise, we need to submit
659 * all those pages down to the drive.
661 if (!page_started
&& !ret
)
662 extent_write_locked_range(io_tree
,
663 inode
, async_extent
->start
,
664 async_extent
->start
+
665 async_extent
->ram_size
- 1,
669 unlock_page(async_cow
->locked_page
);
675 lock_extent(io_tree
, async_extent
->start
,
676 async_extent
->start
+ async_extent
->ram_size
- 1);
678 trans
= btrfs_join_transaction(root
);
680 ret
= PTR_ERR(trans
);
682 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
683 ret
= btrfs_reserve_extent(trans
, root
,
684 async_extent
->compressed_size
,
685 async_extent
->compressed_size
,
686 0, alloc_hint
, &ins
, 1);
687 if (ret
&& ret
!= -ENOSPC
)
688 btrfs_abort_transaction(trans
, root
, ret
);
689 btrfs_end_transaction(trans
, root
);
695 for (i
= 0; i
< async_extent
->nr_pages
; i
++) {
696 WARN_ON(async_extent
->pages
[i
]->mapping
);
697 page_cache_release(async_extent
->pages
[i
]);
699 kfree(async_extent
->pages
);
700 async_extent
->nr_pages
= 0;
701 async_extent
->pages
= NULL
;
709 * here we're doing allocation and writeback of the
712 btrfs_drop_extent_cache(inode
, async_extent
->start
,
713 async_extent
->start
+
714 async_extent
->ram_size
- 1, 0);
716 em
= alloc_extent_map();
719 goto out_free_reserve
;
721 em
->start
= async_extent
->start
;
722 em
->len
= async_extent
->ram_size
;
723 em
->orig_start
= em
->start
;
724 em
->mod_start
= em
->start
;
725 em
->mod_len
= em
->len
;
727 em
->block_start
= ins
.objectid
;
728 em
->block_len
= ins
.offset
;
729 em
->orig_block_len
= ins
.offset
;
730 em
->ram_bytes
= async_extent
->ram_size
;
731 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
732 em
->compress_type
= async_extent
->compress_type
;
733 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
734 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
738 write_lock(&em_tree
->lock
);
739 ret
= add_extent_mapping(em_tree
, em
, 1);
740 write_unlock(&em_tree
->lock
);
741 if (ret
!= -EEXIST
) {
745 btrfs_drop_extent_cache(inode
, async_extent
->start
,
746 async_extent
->start
+
747 async_extent
->ram_size
- 1, 0);
751 goto out_free_reserve
;
753 ret
= btrfs_add_ordered_extent_compress(inode
,
756 async_extent
->ram_size
,
758 BTRFS_ORDERED_COMPRESSED
,
759 async_extent
->compress_type
);
761 goto out_free_reserve
;
764 * clear dirty, set writeback and unlock the pages.
766 extent_clear_unlock_delalloc(inode
,
767 &BTRFS_I(inode
)->io_tree
,
769 async_extent
->start
+
770 async_extent
->ram_size
- 1,
771 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
772 EXTENT_CLEAR_UNLOCK
|
773 EXTENT_CLEAR_DELALLOC
|
774 EXTENT_CLEAR_DIRTY
| EXTENT_SET_WRITEBACK
);
776 ret
= btrfs_submit_compressed_write(inode
,
778 async_extent
->ram_size
,
780 ins
.offset
, async_extent
->pages
,
781 async_extent
->nr_pages
);
782 alloc_hint
= ins
.objectid
+ ins
.offset
;
792 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
794 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
796 async_extent
->start
+
797 async_extent
->ram_size
- 1,
798 NULL
, EXTENT_CLEAR_UNLOCK_PAGE
|
799 EXTENT_CLEAR_UNLOCK
|
800 EXTENT_CLEAR_DELALLOC
|
802 EXTENT_SET_WRITEBACK
|
803 EXTENT_END_WRITEBACK
);
808 static u64
get_extent_allocation_hint(struct inode
*inode
, u64 start
,
811 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
812 struct extent_map
*em
;
815 read_lock(&em_tree
->lock
);
816 em
= search_extent_mapping(em_tree
, start
, num_bytes
);
819 * if block start isn't an actual block number then find the
820 * first block in this inode and use that as a hint. If that
821 * block is also bogus then just don't worry about it.
823 if (em
->block_start
>= EXTENT_MAP_LAST_BYTE
) {
825 em
= search_extent_mapping(em_tree
, 0, 0);
826 if (em
&& em
->block_start
< EXTENT_MAP_LAST_BYTE
)
827 alloc_hint
= em
->block_start
;
831 alloc_hint
= em
->block_start
;
835 read_unlock(&em_tree
->lock
);
841 * when extent_io.c finds a delayed allocation range in the file,
842 * the call backs end up in this code. The basic idea is to
843 * allocate extents on disk for the range, and create ordered data structs
844 * in ram to track those extents.
846 * locked_page is the page that writepage had locked already. We use
847 * it to make sure we don't do extra locks or unlocks.
849 * *page_started is set to one if we unlock locked_page and do everything
850 * required to start IO on it. It may be clean and already done with
853 static noinline
int __cow_file_range(struct btrfs_trans_handle
*trans
,
855 struct btrfs_root
*root
,
856 struct page
*locked_page
,
857 u64 start
, u64 end
, int *page_started
,
858 unsigned long *nr_written
,
863 unsigned long ram_size
;
866 u64 blocksize
= root
->sectorsize
;
867 struct btrfs_key ins
;
868 struct extent_map
*em
;
869 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
872 BUG_ON(btrfs_is_free_space_inode(inode
));
874 num_bytes
= ALIGN(end
- start
+ 1, blocksize
);
875 num_bytes
= max(blocksize
, num_bytes
);
876 disk_num_bytes
= num_bytes
;
878 /* if this is a small write inside eof, kick off defrag */
879 if (num_bytes
< 64 * 1024 &&
880 (start
> 0 || end
+ 1 < BTRFS_I(inode
)->disk_i_size
))
881 btrfs_add_inode_defrag(trans
, inode
);
884 /* lets try to make an inline extent */
885 ret
= cow_file_range_inline(trans
, root
, inode
,
886 start
, end
, 0, 0, NULL
);
888 extent_clear_unlock_delalloc(inode
,
889 &BTRFS_I(inode
)->io_tree
,
891 EXTENT_CLEAR_UNLOCK_PAGE
|
892 EXTENT_CLEAR_UNLOCK
|
893 EXTENT_CLEAR_DELALLOC
|
895 EXTENT_SET_WRITEBACK
|
896 EXTENT_END_WRITEBACK
);
898 *nr_written
= *nr_written
+
899 (end
- start
+ PAGE_CACHE_SIZE
) / PAGE_CACHE_SIZE
;
902 } else if (ret
< 0) {
903 btrfs_abort_transaction(trans
, root
, ret
);
908 BUG_ON(disk_num_bytes
>
909 btrfs_super_total_bytes(root
->fs_info
->super_copy
));
911 alloc_hint
= get_extent_allocation_hint(inode
, start
, num_bytes
);
912 btrfs_drop_extent_cache(inode
, start
, start
+ num_bytes
- 1, 0);
914 while (disk_num_bytes
> 0) {
917 cur_alloc_size
= disk_num_bytes
;
918 ret
= btrfs_reserve_extent(trans
, root
, cur_alloc_size
,
919 root
->sectorsize
, 0, alloc_hint
,
922 btrfs_abort_transaction(trans
, root
, ret
);
926 em
= alloc_extent_map();
932 em
->orig_start
= em
->start
;
933 ram_size
= ins
.offset
;
934 em
->len
= ins
.offset
;
935 em
->mod_start
= em
->start
;
936 em
->mod_len
= em
->len
;
938 em
->block_start
= ins
.objectid
;
939 em
->block_len
= ins
.offset
;
940 em
->orig_block_len
= ins
.offset
;
941 em
->ram_bytes
= ram_size
;
942 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
943 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
947 write_lock(&em_tree
->lock
);
948 ret
= add_extent_mapping(em_tree
, em
, 1);
949 write_unlock(&em_tree
->lock
);
950 if (ret
!= -EEXIST
) {
954 btrfs_drop_extent_cache(inode
, start
,
955 start
+ ram_size
- 1, 0);
960 cur_alloc_size
= ins
.offset
;
961 ret
= btrfs_add_ordered_extent(inode
, start
, ins
.objectid
,
962 ram_size
, cur_alloc_size
, 0);
966 if (root
->root_key
.objectid
==
967 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
968 ret
= btrfs_reloc_clone_csums(inode
, start
,
971 btrfs_abort_transaction(trans
, root
, ret
);
976 if (disk_num_bytes
< cur_alloc_size
)
979 /* we're not doing compressed IO, don't unlock the first
980 * page (which the caller expects to stay locked), don't
981 * clear any dirty bits and don't set any writeback bits
983 * Do set the Private2 bit so we know this page was properly
984 * setup for writepage
986 op
= unlock
? EXTENT_CLEAR_UNLOCK_PAGE
: 0;
987 op
|= EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
990 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
991 start
, start
+ ram_size
- 1,
993 disk_num_bytes
-= cur_alloc_size
;
994 num_bytes
-= cur_alloc_size
;
995 alloc_hint
= ins
.objectid
+ ins
.offset
;
996 start
+= cur_alloc_size
;
1002 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
1004 extent_clear_unlock_delalloc(inode
,
1005 &BTRFS_I(inode
)->io_tree
,
1006 start
, end
, locked_page
,
1007 EXTENT_CLEAR_UNLOCK_PAGE
|
1008 EXTENT_CLEAR_UNLOCK
|
1009 EXTENT_CLEAR_DELALLOC
|
1010 EXTENT_CLEAR_DIRTY
|
1011 EXTENT_SET_WRITEBACK
|
1012 EXTENT_END_WRITEBACK
);
1017 static noinline
int cow_file_range(struct inode
*inode
,
1018 struct page
*locked_page
,
1019 u64 start
, u64 end
, int *page_started
,
1020 unsigned long *nr_written
,
1023 struct btrfs_trans_handle
*trans
;
1024 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1027 trans
= btrfs_join_transaction(root
);
1028 if (IS_ERR(trans
)) {
1029 extent_clear_unlock_delalloc(inode
,
1030 &BTRFS_I(inode
)->io_tree
,
1031 start
, end
, locked_page
,
1032 EXTENT_CLEAR_UNLOCK_PAGE
|
1033 EXTENT_CLEAR_UNLOCK
|
1034 EXTENT_CLEAR_DELALLOC
|
1035 EXTENT_CLEAR_DIRTY
|
1036 EXTENT_SET_WRITEBACK
|
1037 EXTENT_END_WRITEBACK
);
1038 return PTR_ERR(trans
);
1040 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1042 ret
= __cow_file_range(trans
, inode
, root
, locked_page
, start
, end
,
1043 page_started
, nr_written
, unlock
);
1045 btrfs_end_transaction(trans
, root
);
1051 * work queue call back to started compression on a file and pages
1053 static noinline
void async_cow_start(struct btrfs_work
*work
)
1055 struct async_cow
*async_cow
;
1057 async_cow
= container_of(work
, struct async_cow
, work
);
1059 compress_file_range(async_cow
->inode
, async_cow
->locked_page
,
1060 async_cow
->start
, async_cow
->end
, async_cow
,
1062 if (num_added
== 0) {
1063 btrfs_add_delayed_iput(async_cow
->inode
);
1064 async_cow
->inode
= NULL
;
1069 * work queue call back to submit previously compressed pages
1071 static noinline
void async_cow_submit(struct btrfs_work
*work
)
1073 struct async_cow
*async_cow
;
1074 struct btrfs_root
*root
;
1075 unsigned long nr_pages
;
1077 async_cow
= container_of(work
, struct async_cow
, work
);
1079 root
= async_cow
->root
;
1080 nr_pages
= (async_cow
->end
- async_cow
->start
+ PAGE_CACHE_SIZE
) >>
1083 if (atomic_sub_return(nr_pages
, &root
->fs_info
->async_delalloc_pages
) <
1085 waitqueue_active(&root
->fs_info
->async_submit_wait
))
1086 wake_up(&root
->fs_info
->async_submit_wait
);
1088 if (async_cow
->inode
)
1089 submit_compressed_extents(async_cow
->inode
, async_cow
);
1092 static noinline
void async_cow_free(struct btrfs_work
*work
)
1094 struct async_cow
*async_cow
;
1095 async_cow
= container_of(work
, struct async_cow
, work
);
1096 if (async_cow
->inode
)
1097 btrfs_add_delayed_iput(async_cow
->inode
);
1101 static int cow_file_range_async(struct inode
*inode
, struct page
*locked_page
,
1102 u64 start
, u64 end
, int *page_started
,
1103 unsigned long *nr_written
)
1105 struct async_cow
*async_cow
;
1106 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1107 unsigned long nr_pages
;
1109 int limit
= 10 * 1024 * 1024;
1111 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, start
, end
, EXTENT_LOCKED
,
1112 1, 0, NULL
, GFP_NOFS
);
1113 while (start
< end
) {
1114 async_cow
= kmalloc(sizeof(*async_cow
), GFP_NOFS
);
1115 BUG_ON(!async_cow
); /* -ENOMEM */
1116 async_cow
->inode
= igrab(inode
);
1117 async_cow
->root
= root
;
1118 async_cow
->locked_page
= locked_page
;
1119 async_cow
->start
= start
;
1121 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NOCOMPRESS
)
1124 cur_end
= min(end
, start
+ 512 * 1024 - 1);
1126 async_cow
->end
= cur_end
;
1127 INIT_LIST_HEAD(&async_cow
->extents
);
1129 async_cow
->work
.func
= async_cow_start
;
1130 async_cow
->work
.ordered_func
= async_cow_submit
;
1131 async_cow
->work
.ordered_free
= async_cow_free
;
1132 async_cow
->work
.flags
= 0;
1134 nr_pages
= (cur_end
- start
+ PAGE_CACHE_SIZE
) >>
1136 atomic_add(nr_pages
, &root
->fs_info
->async_delalloc_pages
);
1138 btrfs_queue_worker(&root
->fs_info
->delalloc_workers
,
1141 if (atomic_read(&root
->fs_info
->async_delalloc_pages
) > limit
) {
1142 wait_event(root
->fs_info
->async_submit_wait
,
1143 (atomic_read(&root
->fs_info
->async_delalloc_pages
) <
1147 while (atomic_read(&root
->fs_info
->async_submit_draining
) &&
1148 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
1149 wait_event(root
->fs_info
->async_submit_wait
,
1150 (atomic_read(&root
->fs_info
->async_delalloc_pages
) ==
1154 *nr_written
+= nr_pages
;
1155 start
= cur_end
+ 1;
1161 static noinline
int csum_exist_in_range(struct btrfs_root
*root
,
1162 u64 bytenr
, u64 num_bytes
)
1165 struct btrfs_ordered_sum
*sums
;
1168 ret
= btrfs_lookup_csums_range(root
->fs_info
->csum_root
, bytenr
,
1169 bytenr
+ num_bytes
- 1, &list
, 0);
1170 if (ret
== 0 && list_empty(&list
))
1173 while (!list_empty(&list
)) {
1174 sums
= list_entry(list
.next
, struct btrfs_ordered_sum
, list
);
1175 list_del(&sums
->list
);
1182 * when nowcow writeback call back. This checks for snapshots or COW copies
1183 * of the extents that exist in the file, and COWs the file as required.
1185 * If no cow copies or snapshots exist, we write directly to the existing
1188 static noinline
int run_delalloc_nocow(struct inode
*inode
,
1189 struct page
*locked_page
,
1190 u64 start
, u64 end
, int *page_started
, int force
,
1191 unsigned long *nr_written
)
1193 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1194 struct btrfs_trans_handle
*trans
;
1195 struct extent_buffer
*leaf
;
1196 struct btrfs_path
*path
;
1197 struct btrfs_file_extent_item
*fi
;
1198 struct btrfs_key found_key
;
1213 u64 ino
= btrfs_ino(inode
);
1215 path
= btrfs_alloc_path();
1217 extent_clear_unlock_delalloc(inode
,
1218 &BTRFS_I(inode
)->io_tree
,
1219 start
, end
, locked_page
,
1220 EXTENT_CLEAR_UNLOCK_PAGE
|
1221 EXTENT_CLEAR_UNLOCK
|
1222 EXTENT_CLEAR_DELALLOC
|
1223 EXTENT_CLEAR_DIRTY
|
1224 EXTENT_SET_WRITEBACK
|
1225 EXTENT_END_WRITEBACK
);
1229 nolock
= btrfs_is_free_space_inode(inode
);
1232 trans
= btrfs_join_transaction_nolock(root
);
1234 trans
= btrfs_join_transaction(root
);
1236 if (IS_ERR(trans
)) {
1237 extent_clear_unlock_delalloc(inode
,
1238 &BTRFS_I(inode
)->io_tree
,
1239 start
, end
, locked_page
,
1240 EXTENT_CLEAR_UNLOCK_PAGE
|
1241 EXTENT_CLEAR_UNLOCK
|
1242 EXTENT_CLEAR_DELALLOC
|
1243 EXTENT_CLEAR_DIRTY
|
1244 EXTENT_SET_WRITEBACK
|
1245 EXTENT_END_WRITEBACK
);
1246 btrfs_free_path(path
);
1247 return PTR_ERR(trans
);
1250 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
1252 cow_start
= (u64
)-1;
1255 ret
= btrfs_lookup_file_extent(trans
, root
, path
, ino
,
1258 btrfs_abort_transaction(trans
, root
, ret
);
1261 if (ret
> 0 && path
->slots
[0] > 0 && check_prev
) {
1262 leaf
= path
->nodes
[0];
1263 btrfs_item_key_to_cpu(leaf
, &found_key
,
1264 path
->slots
[0] - 1);
1265 if (found_key
.objectid
== ino
&&
1266 found_key
.type
== BTRFS_EXTENT_DATA_KEY
)
1271 leaf
= path
->nodes
[0];
1272 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
1273 ret
= btrfs_next_leaf(root
, path
);
1275 btrfs_abort_transaction(trans
, root
, ret
);
1280 leaf
= path
->nodes
[0];
1286 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
1288 if (found_key
.objectid
> ino
||
1289 found_key
.type
> BTRFS_EXTENT_DATA_KEY
||
1290 found_key
.offset
> end
)
1293 if (found_key
.offset
> cur_offset
) {
1294 extent_end
= found_key
.offset
;
1299 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1300 struct btrfs_file_extent_item
);
1301 extent_type
= btrfs_file_extent_type(leaf
, fi
);
1303 ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
1304 if (extent_type
== BTRFS_FILE_EXTENT_REG
||
1305 extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1306 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
1307 extent_offset
= btrfs_file_extent_offset(leaf
, fi
);
1308 extent_end
= found_key
.offset
+
1309 btrfs_file_extent_num_bytes(leaf
, fi
);
1311 btrfs_file_extent_disk_num_bytes(leaf
, fi
);
1312 if (extent_end
<= start
) {
1316 if (disk_bytenr
== 0)
1318 if (btrfs_file_extent_compression(leaf
, fi
) ||
1319 btrfs_file_extent_encryption(leaf
, fi
) ||
1320 btrfs_file_extent_other_encoding(leaf
, fi
))
1322 if (extent_type
== BTRFS_FILE_EXTENT_REG
&& !force
)
1324 if (btrfs_extent_readonly(root
, disk_bytenr
))
1326 if (btrfs_cross_ref_exist(trans
, root
, ino
,
1328 extent_offset
, disk_bytenr
))
1330 disk_bytenr
+= extent_offset
;
1331 disk_bytenr
+= cur_offset
- found_key
.offset
;
1332 num_bytes
= min(end
+ 1, extent_end
) - cur_offset
;
1334 * force cow if csum exists in the range.
1335 * this ensure that csum for a given extent are
1336 * either valid or do not exist.
1338 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
1341 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
1342 extent_end
= found_key
.offset
+
1343 btrfs_file_extent_inline_len(leaf
, fi
);
1344 extent_end
= ALIGN(extent_end
, root
->sectorsize
);
1349 if (extent_end
<= start
) {
1354 if (cow_start
== (u64
)-1)
1355 cow_start
= cur_offset
;
1356 cur_offset
= extent_end
;
1357 if (cur_offset
> end
)
1363 btrfs_release_path(path
);
1364 if (cow_start
!= (u64
)-1) {
1365 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1366 cow_start
, found_key
.offset
- 1,
1367 page_started
, nr_written
, 1);
1369 btrfs_abort_transaction(trans
, root
, ret
);
1372 cow_start
= (u64
)-1;
1375 if (extent_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
1376 struct extent_map
*em
;
1377 struct extent_map_tree
*em_tree
;
1378 em_tree
= &BTRFS_I(inode
)->extent_tree
;
1379 em
= alloc_extent_map();
1380 BUG_ON(!em
); /* -ENOMEM */
1381 em
->start
= cur_offset
;
1382 em
->orig_start
= found_key
.offset
- extent_offset
;
1383 em
->len
= num_bytes
;
1384 em
->block_len
= num_bytes
;
1385 em
->block_start
= disk_bytenr
;
1386 em
->orig_block_len
= disk_num_bytes
;
1387 em
->ram_bytes
= ram_bytes
;
1388 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
1389 em
->mod_start
= em
->start
;
1390 em
->mod_len
= em
->len
;
1391 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
1392 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
1393 em
->generation
= -1;
1395 write_lock(&em_tree
->lock
);
1396 ret
= add_extent_mapping(em_tree
, em
, 1);
1397 write_unlock(&em_tree
->lock
);
1398 if (ret
!= -EEXIST
) {
1399 free_extent_map(em
);
1402 btrfs_drop_extent_cache(inode
, em
->start
,
1403 em
->start
+ em
->len
- 1, 0);
1405 type
= BTRFS_ORDERED_PREALLOC
;
1407 type
= BTRFS_ORDERED_NOCOW
;
1410 ret
= btrfs_add_ordered_extent(inode
, cur_offset
, disk_bytenr
,
1411 num_bytes
, num_bytes
, type
);
1412 BUG_ON(ret
); /* -ENOMEM */
1414 if (root
->root_key
.objectid
==
1415 BTRFS_DATA_RELOC_TREE_OBJECTID
) {
1416 ret
= btrfs_reloc_clone_csums(inode
, cur_offset
,
1419 btrfs_abort_transaction(trans
, root
, ret
);
1424 extent_clear_unlock_delalloc(inode
, &BTRFS_I(inode
)->io_tree
,
1425 cur_offset
, cur_offset
+ num_bytes
- 1,
1426 locked_page
, EXTENT_CLEAR_UNLOCK_PAGE
|
1427 EXTENT_CLEAR_UNLOCK
| EXTENT_CLEAR_DELALLOC
|
1428 EXTENT_SET_PRIVATE2
);
1429 cur_offset
= extent_end
;
1430 if (cur_offset
> end
)
1433 btrfs_release_path(path
);
1435 if (cur_offset
<= end
&& cow_start
== (u64
)-1) {
1436 cow_start
= cur_offset
;
1440 if (cow_start
!= (u64
)-1) {
1441 ret
= __cow_file_range(trans
, inode
, root
, locked_page
,
1443 page_started
, nr_written
, 1);
1445 btrfs_abort_transaction(trans
, root
, ret
);
1451 err
= btrfs_end_transaction(trans
, root
);
1455 if (ret
&& cur_offset
< end
)
1456 extent_clear_unlock_delalloc(inode
,
1457 &BTRFS_I(inode
)->io_tree
,
1458 cur_offset
, end
, locked_page
,
1459 EXTENT_CLEAR_UNLOCK_PAGE
|
1460 EXTENT_CLEAR_UNLOCK
|
1461 EXTENT_CLEAR_DELALLOC
|
1462 EXTENT_CLEAR_DIRTY
|
1463 EXTENT_SET_WRITEBACK
|
1464 EXTENT_END_WRITEBACK
);
1466 btrfs_free_path(path
);
1471 * extent_io.c call back to do delayed allocation processing
1473 static int run_delalloc_range(struct inode
*inode
, struct page
*locked_page
,
1474 u64 start
, u64 end
, int *page_started
,
1475 unsigned long *nr_written
)
1478 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1480 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) {
1481 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1482 page_started
, 1, nr_written
);
1483 } else if (BTRFS_I(inode
)->flags
& BTRFS_INODE_PREALLOC
) {
1484 ret
= run_delalloc_nocow(inode
, locked_page
, start
, end
,
1485 page_started
, 0, nr_written
);
1486 } else if (!btrfs_test_opt(root
, COMPRESS
) &&
1487 !(BTRFS_I(inode
)->force_compress
) &&
1488 !(BTRFS_I(inode
)->flags
& BTRFS_INODE_COMPRESS
)) {
1489 ret
= cow_file_range(inode
, locked_page
, start
, end
,
1490 page_started
, nr_written
, 1);
1492 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT
,
1493 &BTRFS_I(inode
)->runtime_flags
);
1494 ret
= cow_file_range_async(inode
, locked_page
, start
, end
,
1495 page_started
, nr_written
);
1500 static void btrfs_split_extent_hook(struct inode
*inode
,
1501 struct extent_state
*orig
, u64 split
)
1503 /* not delalloc, ignore it */
1504 if (!(orig
->state
& EXTENT_DELALLOC
))
1507 spin_lock(&BTRFS_I(inode
)->lock
);
1508 BTRFS_I(inode
)->outstanding_extents
++;
1509 spin_unlock(&BTRFS_I(inode
)->lock
);
1513 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1514 * extents so we can keep track of new extents that are just merged onto old
1515 * extents, such as when we are doing sequential writes, so we can properly
1516 * account for the metadata space we'll need.
1518 static void btrfs_merge_extent_hook(struct inode
*inode
,
1519 struct extent_state
*new,
1520 struct extent_state
*other
)
1522 /* not delalloc, ignore it */
1523 if (!(other
->state
& EXTENT_DELALLOC
))
1526 spin_lock(&BTRFS_I(inode
)->lock
);
1527 BTRFS_I(inode
)->outstanding_extents
--;
1528 spin_unlock(&BTRFS_I(inode
)->lock
);
1531 static void btrfs_add_delalloc_inodes(struct btrfs_root
*root
,
1532 struct inode
*inode
)
1534 spin_lock(&root
->delalloc_lock
);
1535 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1536 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1537 &root
->delalloc_inodes
);
1538 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1539 &BTRFS_I(inode
)->runtime_flags
);
1540 root
->nr_delalloc_inodes
++;
1541 if (root
->nr_delalloc_inodes
== 1) {
1542 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1543 BUG_ON(!list_empty(&root
->delalloc_root
));
1544 list_add_tail(&root
->delalloc_root
,
1545 &root
->fs_info
->delalloc_roots
);
1546 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1549 spin_unlock(&root
->delalloc_lock
);
1552 static void btrfs_del_delalloc_inode(struct btrfs_root
*root
,
1553 struct inode
*inode
)
1555 spin_lock(&root
->delalloc_lock
);
1556 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1557 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1558 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1559 &BTRFS_I(inode
)->runtime_flags
);
1560 root
->nr_delalloc_inodes
--;
1561 if (!root
->nr_delalloc_inodes
) {
1562 spin_lock(&root
->fs_info
->delalloc_root_lock
);
1563 BUG_ON(list_empty(&root
->delalloc_root
));
1564 list_del_init(&root
->delalloc_root
);
1565 spin_unlock(&root
->fs_info
->delalloc_root_lock
);
1568 spin_unlock(&root
->delalloc_lock
);
1572 * extent_io.c set_bit_hook, used to track delayed allocation
1573 * bytes in this file, and to maintain the list of inodes that
1574 * have pending delalloc work to be done.
1576 static void btrfs_set_bit_hook(struct inode
*inode
,
1577 struct extent_state
*state
, unsigned long *bits
)
1581 * set_bit and clear bit hooks normally require _irqsave/restore
1582 * but in this case, we are only testing for the DELALLOC
1583 * bit, which is only set or cleared with irqs on
1585 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1586 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1587 u64 len
= state
->end
+ 1 - state
->start
;
1588 bool do_list
= !btrfs_is_free_space_inode(inode
);
1590 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1591 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1593 spin_lock(&BTRFS_I(inode
)->lock
);
1594 BTRFS_I(inode
)->outstanding_extents
++;
1595 spin_unlock(&BTRFS_I(inode
)->lock
);
1598 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1599 root
->fs_info
->delalloc_batch
);
1600 spin_lock(&BTRFS_I(inode
)->lock
);
1601 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1602 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1603 &BTRFS_I(inode
)->runtime_flags
))
1604 btrfs_add_delalloc_inodes(root
, inode
);
1605 spin_unlock(&BTRFS_I(inode
)->lock
);
1610 * extent_io.c clear_bit_hook, see set_bit_hook for why
1612 static void btrfs_clear_bit_hook(struct inode
*inode
,
1613 struct extent_state
*state
,
1614 unsigned long *bits
)
1617 * set_bit and clear bit hooks normally require _irqsave/restore
1618 * but in this case, we are only testing for the DELALLOC
1619 * bit, which is only set or cleared with irqs on
1621 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1622 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1623 u64 len
= state
->end
+ 1 - state
->start
;
1624 bool do_list
= !btrfs_is_free_space_inode(inode
);
1626 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1627 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1628 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1629 spin_lock(&BTRFS_I(inode
)->lock
);
1630 BTRFS_I(inode
)->outstanding_extents
--;
1631 spin_unlock(&BTRFS_I(inode
)->lock
);
1634 if (*bits
& EXTENT_DO_ACCOUNTING
)
1635 btrfs_delalloc_release_metadata(inode
, len
);
1637 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1639 btrfs_free_reserved_data_space(inode
, len
);
1641 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1642 root
->fs_info
->delalloc_batch
);
1643 spin_lock(&BTRFS_I(inode
)->lock
);
1644 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1645 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1646 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1647 &BTRFS_I(inode
)->runtime_flags
))
1648 btrfs_del_delalloc_inode(root
, inode
);
1649 spin_unlock(&BTRFS_I(inode
)->lock
);
1654 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1655 * we don't create bios that span stripes or chunks
1657 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1658 size_t size
, struct bio
*bio
,
1659 unsigned long bio_flags
)
1661 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1662 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1667 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1670 length
= bio
->bi_size
;
1671 map_length
= length
;
1672 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1673 &map_length
, NULL
, 0);
1674 /* Will always return 0 with map_multi == NULL */
1676 if (map_length
< length
+ size
)
1682 * in order to insert checksums into the metadata in large chunks,
1683 * we wait until bio submission time. All the pages in the bio are
1684 * checksummed and sums are attached onto the ordered extent record.
1686 * At IO completion time the cums attached on the ordered extent record
1687 * are inserted into the btree
1689 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1690 struct bio
*bio
, int mirror_num
,
1691 unsigned long bio_flags
,
1694 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1697 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1698 BUG_ON(ret
); /* -ENOMEM */
1703 * in order to insert checksums into the metadata in large chunks,
1704 * we wait until bio submission time. All the pages in the bio are
1705 * checksummed and sums are attached onto the ordered extent record.
1707 * At IO completion time the cums attached on the ordered extent record
1708 * are inserted into the btree
1710 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1711 int mirror_num
, unsigned long bio_flags
,
1714 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1717 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1719 bio_endio(bio
, ret
);
1724 * extent_io.c submission hook. This does the right thing for csum calculation
1725 * on write, or reading the csums from the tree before a read
1727 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1728 int mirror_num
, unsigned long bio_flags
,
1731 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1735 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1737 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1739 if (btrfs_is_free_space_inode(inode
))
1742 if (!(rw
& REQ_WRITE
)) {
1743 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1747 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1748 ret
= btrfs_submit_compressed_read(inode
, bio
,
1752 } else if (!skip_sum
) {
1753 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1758 } else if (async
&& !skip_sum
) {
1759 /* csum items have already been cloned */
1760 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1762 /* we're doing a write, do the async checksumming */
1763 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1764 inode
, rw
, bio
, mirror_num
,
1765 bio_flags
, bio_offset
,
1766 __btrfs_submit_bio_start
,
1767 __btrfs_submit_bio_done
);
1769 } else if (!skip_sum
) {
1770 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1776 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1780 bio_endio(bio
, ret
);
1785 * given a list of ordered sums record them in the inode. This happens
1786 * at IO completion time based on sums calculated at bio submission time.
1788 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1789 struct inode
*inode
, u64 file_offset
,
1790 struct list_head
*list
)
1792 struct btrfs_ordered_sum
*sum
;
1794 list_for_each_entry(sum
, list
, list
) {
1795 trans
->adding_csums
= 1;
1796 btrfs_csum_file_blocks(trans
,
1797 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1798 trans
->adding_csums
= 0;
1803 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1804 struct extent_state
**cached_state
)
1806 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1807 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1808 cached_state
, GFP_NOFS
);
1811 /* see btrfs_writepage_start_hook for details on why this is required */
1812 struct btrfs_writepage_fixup
{
1814 struct btrfs_work work
;
1817 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1819 struct btrfs_writepage_fixup
*fixup
;
1820 struct btrfs_ordered_extent
*ordered
;
1821 struct extent_state
*cached_state
= NULL
;
1823 struct inode
*inode
;
1828 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1832 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1833 ClearPageChecked(page
);
1837 inode
= page
->mapping
->host
;
1838 page_start
= page_offset(page
);
1839 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1841 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1844 /* already ordered? We're done */
1845 if (PagePrivate2(page
))
1848 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1850 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1851 page_end
, &cached_state
, GFP_NOFS
);
1853 btrfs_start_ordered_extent(inode
, ordered
, 1);
1854 btrfs_put_ordered_extent(ordered
);
1858 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1860 mapping_set_error(page
->mapping
, ret
);
1861 end_extent_writepage(page
, ret
, page_start
, page_end
);
1862 ClearPageChecked(page
);
1866 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1867 ClearPageChecked(page
);
1868 set_page_dirty(page
);
1870 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1871 &cached_state
, GFP_NOFS
);
1874 page_cache_release(page
);
1879 * There are a few paths in the higher layers of the kernel that directly
1880 * set the page dirty bit without asking the filesystem if it is a
1881 * good idea. This causes problems because we want to make sure COW
1882 * properly happens and the data=ordered rules are followed.
1884 * In our case any range that doesn't have the ORDERED bit set
1885 * hasn't been properly setup for IO. We kick off an async process
1886 * to fix it up. The async helper will wait for ordered extents, set
1887 * the delalloc bit and make it safe to write the page.
1889 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1891 struct inode
*inode
= page
->mapping
->host
;
1892 struct btrfs_writepage_fixup
*fixup
;
1893 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1895 /* this page is properly in the ordered list */
1896 if (TestClearPagePrivate2(page
))
1899 if (PageChecked(page
))
1902 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1906 SetPageChecked(page
);
1907 page_cache_get(page
);
1908 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1910 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1914 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1915 struct inode
*inode
, u64 file_pos
,
1916 u64 disk_bytenr
, u64 disk_num_bytes
,
1917 u64 num_bytes
, u64 ram_bytes
,
1918 u8 compression
, u8 encryption
,
1919 u16 other_encoding
, int extent_type
)
1921 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1922 struct btrfs_file_extent_item
*fi
;
1923 struct btrfs_path
*path
;
1924 struct extent_buffer
*leaf
;
1925 struct btrfs_key ins
;
1928 path
= btrfs_alloc_path();
1932 path
->leave_spinning
= 1;
1935 * we may be replacing one extent in the tree with another.
1936 * The new extent is pinned in the extent map, and we don't want
1937 * to drop it from the cache until it is completely in the btree.
1939 * So, tell btrfs_drop_extents to leave this extent in the cache.
1940 * the caller is expected to unpin it and allow it to be merged
1943 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1944 file_pos
+ num_bytes
, 0);
1948 ins
.objectid
= btrfs_ino(inode
);
1949 ins
.offset
= file_pos
;
1950 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1951 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1954 leaf
= path
->nodes
[0];
1955 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1956 struct btrfs_file_extent_item
);
1957 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1958 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1959 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1960 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1961 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1962 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1963 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1964 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1965 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1966 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1968 btrfs_mark_buffer_dirty(leaf
);
1969 btrfs_release_path(path
);
1971 inode_add_bytes(inode
, num_bytes
);
1973 ins
.objectid
= disk_bytenr
;
1974 ins
.offset
= disk_num_bytes
;
1975 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1976 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1977 root
->root_key
.objectid
,
1978 btrfs_ino(inode
), file_pos
, &ins
);
1980 btrfs_free_path(path
);
1985 /* snapshot-aware defrag */
1986 struct sa_defrag_extent_backref
{
1987 struct rb_node node
;
1988 struct old_sa_defrag_extent
*old
;
1997 struct old_sa_defrag_extent
{
1998 struct list_head list
;
1999 struct new_sa_defrag_extent
*new;
2008 struct new_sa_defrag_extent
{
2009 struct rb_root root
;
2010 struct list_head head
;
2011 struct btrfs_path
*path
;
2012 struct inode
*inode
;
2020 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
2021 struct sa_defrag_extent_backref
*b2
)
2023 if (b1
->root_id
< b2
->root_id
)
2025 else if (b1
->root_id
> b2
->root_id
)
2028 if (b1
->inum
< b2
->inum
)
2030 else if (b1
->inum
> b2
->inum
)
2033 if (b1
->file_pos
< b2
->file_pos
)
2035 else if (b1
->file_pos
> b2
->file_pos
)
2039 * [------------------------------] ===> (a range of space)
2040 * |<--->| |<---->| =============> (fs/file tree A)
2041 * |<---------------------------->| ===> (fs/file tree B)
2043 * A range of space can refer to two file extents in one tree while
2044 * refer to only one file extent in another tree.
2046 * So we may process a disk offset more than one time(two extents in A)
2047 * and locate at the same extent(one extent in B), then insert two same
2048 * backrefs(both refer to the extent in B).
2053 static void backref_insert(struct rb_root
*root
,
2054 struct sa_defrag_extent_backref
*backref
)
2056 struct rb_node
**p
= &root
->rb_node
;
2057 struct rb_node
*parent
= NULL
;
2058 struct sa_defrag_extent_backref
*entry
;
2063 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2065 ret
= backref_comp(backref
, entry
);
2069 p
= &(*p
)->rb_right
;
2072 rb_link_node(&backref
->node
, parent
, p
);
2073 rb_insert_color(&backref
->node
, root
);
2077 * Note the backref might has changed, and in this case we just return 0.
2079 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2082 struct btrfs_file_extent_item
*extent
;
2083 struct btrfs_fs_info
*fs_info
;
2084 struct old_sa_defrag_extent
*old
= ctx
;
2085 struct new_sa_defrag_extent
*new = old
->new;
2086 struct btrfs_path
*path
= new->path
;
2087 struct btrfs_key key
;
2088 struct btrfs_root
*root
;
2089 struct sa_defrag_extent_backref
*backref
;
2090 struct extent_buffer
*leaf
;
2091 struct inode
*inode
= new->inode
;
2097 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2098 inum
== btrfs_ino(inode
))
2101 key
.objectid
= root_id
;
2102 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2103 key
.offset
= (u64
)-1;
2105 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2106 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2108 if (PTR_ERR(root
) == -ENOENT
)
2111 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2112 inum
, offset
, root_id
);
2113 return PTR_ERR(root
);
2116 key
.objectid
= inum
;
2117 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2118 if (offset
> (u64
)-1 << 32)
2121 key
.offset
= offset
;
2123 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2132 leaf
= path
->nodes
[0];
2133 slot
= path
->slots
[0];
2135 if (slot
>= btrfs_header_nritems(leaf
)) {
2136 ret
= btrfs_next_leaf(root
, path
);
2139 } else if (ret
> 0) {
2148 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2150 if (key
.objectid
> inum
)
2153 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2156 extent
= btrfs_item_ptr(leaf
, slot
,
2157 struct btrfs_file_extent_item
);
2159 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2162 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2163 if (key
.offset
- extent_offset
!= offset
)
2166 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2167 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2168 old
->len
|| extent_offset
+ num_bytes
<=
2169 old
->extent_offset
+ old
->offset
)
2175 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2181 backref
->root_id
= root_id
;
2182 backref
->inum
= inum
;
2183 backref
->file_pos
= offset
+ extent_offset
;
2184 backref
->num_bytes
= num_bytes
;
2185 backref
->extent_offset
= extent_offset
;
2186 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2188 backref_insert(&new->root
, backref
);
2191 btrfs_release_path(path
);
2196 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2197 struct new_sa_defrag_extent
*new)
2199 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2200 struct old_sa_defrag_extent
*old
, *tmp
;
2205 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2206 ret
= iterate_inodes_from_logical(old
->bytenr
, fs_info
,
2207 path
, record_one_backref
,
2209 BUG_ON(ret
< 0 && ret
!= -ENOENT
);
2211 /* no backref to be processed for this extent */
2213 list_del(&old
->list
);
2218 if (list_empty(&new->head
))
2224 static int relink_is_mergable(struct extent_buffer
*leaf
,
2225 struct btrfs_file_extent_item
*fi
,
2228 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != disk_bytenr
)
2231 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2234 if (btrfs_file_extent_compression(leaf
, fi
) ||
2235 btrfs_file_extent_encryption(leaf
, fi
) ||
2236 btrfs_file_extent_other_encoding(leaf
, fi
))
2243 * Note the backref might has changed, and in this case we just return 0.
2245 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2246 struct sa_defrag_extent_backref
*prev
,
2247 struct sa_defrag_extent_backref
*backref
)
2249 struct btrfs_file_extent_item
*extent
;
2250 struct btrfs_file_extent_item
*item
;
2251 struct btrfs_ordered_extent
*ordered
;
2252 struct btrfs_trans_handle
*trans
;
2253 struct btrfs_fs_info
*fs_info
;
2254 struct btrfs_root
*root
;
2255 struct btrfs_key key
;
2256 struct extent_buffer
*leaf
;
2257 struct old_sa_defrag_extent
*old
= backref
->old
;
2258 struct new_sa_defrag_extent
*new = old
->new;
2259 struct inode
*src_inode
= new->inode
;
2260 struct inode
*inode
;
2261 struct extent_state
*cached
= NULL
;
2270 if (prev
&& prev
->root_id
== backref
->root_id
&&
2271 prev
->inum
== backref
->inum
&&
2272 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2275 /* step 1: get root */
2276 key
.objectid
= backref
->root_id
;
2277 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2278 key
.offset
= (u64
)-1;
2280 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2281 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2283 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2285 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2286 if (PTR_ERR(root
) == -ENOENT
)
2288 return PTR_ERR(root
);
2291 /* step 2: get inode */
2292 key
.objectid
= backref
->inum
;
2293 key
.type
= BTRFS_INODE_ITEM_KEY
;
2296 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2297 if (IS_ERR(inode
)) {
2298 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2302 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2304 /* step 3: relink backref */
2305 lock_start
= backref
->file_pos
;
2306 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2307 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2310 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2312 btrfs_put_ordered_extent(ordered
);
2316 trans
= btrfs_join_transaction(root
);
2317 if (IS_ERR(trans
)) {
2318 ret
= PTR_ERR(trans
);
2322 key
.objectid
= backref
->inum
;
2323 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2324 key
.offset
= backref
->file_pos
;
2326 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2329 } else if (ret
> 0) {
2334 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2335 struct btrfs_file_extent_item
);
2337 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2338 backref
->generation
)
2341 btrfs_release_path(path
);
2343 start
= backref
->file_pos
;
2344 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2345 start
+= old
->extent_offset
+ old
->offset
-
2346 backref
->extent_offset
;
2348 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2349 old
->extent_offset
+ old
->offset
+ old
->len
);
2350 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2352 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2357 key
.objectid
= btrfs_ino(inode
);
2358 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2361 path
->leave_spinning
= 1;
2363 struct btrfs_file_extent_item
*fi
;
2365 struct btrfs_key found_key
;
2367 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
2372 leaf
= path
->nodes
[0];
2373 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2375 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2376 struct btrfs_file_extent_item
);
2377 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2379 if (relink_is_mergable(leaf
, fi
, new->bytenr
) &&
2380 extent_len
+ found_key
.offset
== start
) {
2381 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2383 btrfs_mark_buffer_dirty(leaf
);
2384 inode_add_bytes(inode
, len
);
2390 btrfs_release_path(path
);
2395 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2398 btrfs_abort_transaction(trans
, root
, ret
);
2402 leaf
= path
->nodes
[0];
2403 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2404 struct btrfs_file_extent_item
);
2405 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2406 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2407 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2408 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2409 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2410 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2411 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2412 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2413 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2414 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2416 btrfs_mark_buffer_dirty(leaf
);
2417 inode_add_bytes(inode
, len
);
2418 btrfs_release_path(path
);
2420 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2422 backref
->root_id
, backref
->inum
,
2423 new->file_pos
, 0); /* start - extent_offset */
2425 btrfs_abort_transaction(trans
, root
, ret
);
2431 btrfs_release_path(path
);
2432 path
->leave_spinning
= 0;
2433 btrfs_end_transaction(trans
, root
);
2435 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2441 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2443 struct btrfs_path
*path
;
2444 struct old_sa_defrag_extent
*old
, *tmp
;
2445 struct sa_defrag_extent_backref
*backref
;
2446 struct sa_defrag_extent_backref
*prev
= NULL
;
2447 struct inode
*inode
;
2448 struct btrfs_root
*root
;
2449 struct rb_node
*node
;
2453 root
= BTRFS_I(inode
)->root
;
2455 path
= btrfs_alloc_path();
2459 if (!record_extent_backrefs(path
, new)) {
2460 btrfs_free_path(path
);
2463 btrfs_release_path(path
);
2466 node
= rb_first(&new->root
);
2469 rb_erase(node
, &new->root
);
2471 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2473 ret
= relink_extent_backref(path
, prev
, backref
);
2486 btrfs_free_path(path
);
2488 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2489 list_del(&old
->list
);
2493 atomic_dec(&root
->fs_info
->defrag_running
);
2494 wake_up(&root
->fs_info
->transaction_wait
);
2499 static struct new_sa_defrag_extent
*
2500 record_old_file_extents(struct inode
*inode
,
2501 struct btrfs_ordered_extent
*ordered
)
2503 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2504 struct btrfs_path
*path
;
2505 struct btrfs_key key
;
2506 struct old_sa_defrag_extent
*old
, *tmp
;
2507 struct new_sa_defrag_extent
*new;
2510 new = kmalloc(sizeof(*new), GFP_NOFS
);
2515 new->file_pos
= ordered
->file_offset
;
2516 new->len
= ordered
->len
;
2517 new->bytenr
= ordered
->start
;
2518 new->disk_len
= ordered
->disk_len
;
2519 new->compress_type
= ordered
->compress_type
;
2520 new->root
= RB_ROOT
;
2521 INIT_LIST_HEAD(&new->head
);
2523 path
= btrfs_alloc_path();
2527 key
.objectid
= btrfs_ino(inode
);
2528 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2529 key
.offset
= new->file_pos
;
2531 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2534 if (ret
> 0 && path
->slots
[0] > 0)
2537 /* find out all the old extents for the file range */
2539 struct btrfs_file_extent_item
*extent
;
2540 struct extent_buffer
*l
;
2549 slot
= path
->slots
[0];
2551 if (slot
>= btrfs_header_nritems(l
)) {
2552 ret
= btrfs_next_leaf(root
, path
);
2560 btrfs_item_key_to_cpu(l
, &key
, slot
);
2562 if (key
.objectid
!= btrfs_ino(inode
))
2564 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2566 if (key
.offset
>= new->file_pos
+ new->len
)
2569 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2571 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2572 if (key
.offset
+ num_bytes
< new->file_pos
)
2575 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2579 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2581 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2585 offset
= max(new->file_pos
, key
.offset
);
2586 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2588 old
->bytenr
= disk_bytenr
;
2589 old
->extent_offset
= extent_offset
;
2590 old
->offset
= offset
- key
.offset
;
2591 old
->len
= end
- offset
;
2594 list_add_tail(&old
->list
, &new->head
);
2600 btrfs_free_path(path
);
2601 atomic_inc(&root
->fs_info
->defrag_running
);
2606 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2607 list_del(&old
->list
);
2611 btrfs_free_path(path
);
2618 * helper function for btrfs_finish_ordered_io, this
2619 * just reads in some of the csum leaves to prime them into ram
2620 * before we start the transaction. It limits the amount of btree
2621 * reads required while inside the transaction.
2623 /* as ordered data IO finishes, this gets called so we can finish
2624 * an ordered extent if the range of bytes in the file it covers are
2627 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2629 struct inode
*inode
= ordered_extent
->inode
;
2630 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2631 struct btrfs_trans_handle
*trans
= NULL
;
2632 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2633 struct extent_state
*cached_state
= NULL
;
2634 struct new_sa_defrag_extent
*new = NULL
;
2635 int compress_type
= 0;
2639 nolock
= btrfs_is_free_space_inode(inode
);
2641 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2646 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2647 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2648 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2650 trans
= btrfs_join_transaction_nolock(root
);
2652 trans
= btrfs_join_transaction(root
);
2653 if (IS_ERR(trans
)) {
2654 ret
= PTR_ERR(trans
);
2658 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2659 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2660 if (ret
) /* -ENOMEM or corruption */
2661 btrfs_abort_transaction(trans
, root
, ret
);
2665 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2666 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2669 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2670 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2671 EXTENT_DEFRAG
, 1, cached_state
);
2673 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2674 if (last_snapshot
>= BTRFS_I(inode
)->generation
)
2675 /* the inode is shared */
2676 new = record_old_file_extents(inode
, ordered_extent
);
2678 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2679 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2680 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2684 trans
= btrfs_join_transaction_nolock(root
);
2686 trans
= btrfs_join_transaction(root
);
2687 if (IS_ERR(trans
)) {
2688 ret
= PTR_ERR(trans
);
2692 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2694 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2695 compress_type
= ordered_extent
->compress_type
;
2696 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2697 BUG_ON(compress_type
);
2698 ret
= btrfs_mark_extent_written(trans
, inode
,
2699 ordered_extent
->file_offset
,
2700 ordered_extent
->file_offset
+
2701 ordered_extent
->len
);
2703 BUG_ON(root
== root
->fs_info
->tree_root
);
2704 ret
= insert_reserved_file_extent(trans
, inode
,
2705 ordered_extent
->file_offset
,
2706 ordered_extent
->start
,
2707 ordered_extent
->disk_len
,
2708 ordered_extent
->len
,
2709 ordered_extent
->len
,
2710 compress_type
, 0, 0,
2711 BTRFS_FILE_EXTENT_REG
);
2713 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2714 ordered_extent
->file_offset
, ordered_extent
->len
,
2717 btrfs_abort_transaction(trans
, root
, ret
);
2721 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2722 &ordered_extent
->list
);
2724 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2725 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2726 if (ret
) { /* -ENOMEM or corruption */
2727 btrfs_abort_transaction(trans
, root
, ret
);
2732 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2733 ordered_extent
->file_offset
+
2734 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2736 if (root
!= root
->fs_info
->tree_root
)
2737 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2739 btrfs_end_transaction(trans
, root
);
2742 clear_extent_uptodate(io_tree
, ordered_extent
->file_offset
,
2743 ordered_extent
->file_offset
+
2744 ordered_extent
->len
- 1, NULL
, GFP_NOFS
);
2747 * If the ordered extent had an IOERR or something else went
2748 * wrong we need to return the space for this ordered extent
2749 * back to the allocator.
2751 if (!test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2752 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2753 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2754 ordered_extent
->disk_len
);
2759 * This needs to be done to make sure anybody waiting knows we are done
2760 * updating everything for this ordered extent.
2762 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2764 /* for snapshot-aware defrag */
2766 relink_file_extents(new);
2769 btrfs_put_ordered_extent(ordered_extent
);
2770 /* once for the tree */
2771 btrfs_put_ordered_extent(ordered_extent
);
2776 static void finish_ordered_fn(struct btrfs_work
*work
)
2778 struct btrfs_ordered_extent
*ordered_extent
;
2779 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2780 btrfs_finish_ordered_io(ordered_extent
);
2783 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2784 struct extent_state
*state
, int uptodate
)
2786 struct inode
*inode
= page
->mapping
->host
;
2787 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2788 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2789 struct btrfs_workers
*workers
;
2791 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2793 ClearPagePrivate2(page
);
2794 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2795 end
- start
+ 1, uptodate
))
2798 ordered_extent
->work
.func
= finish_ordered_fn
;
2799 ordered_extent
->work
.flags
= 0;
2801 if (btrfs_is_free_space_inode(inode
))
2802 workers
= &root
->fs_info
->endio_freespace_worker
;
2804 workers
= &root
->fs_info
->endio_write_workers
;
2805 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2811 * when reads are done, we need to check csums to verify the data is correct
2812 * if there's a match, we allow the bio to finish. If not, the code in
2813 * extent_io.c will try to find good copies for us.
2815 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2816 struct extent_state
*state
, int mirror
)
2818 size_t offset
= start
- page_offset(page
);
2819 struct inode
*inode
= page
->mapping
->host
;
2820 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2822 u64
private = ~(u32
)0;
2824 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2826 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
2827 DEFAULT_RATELIMIT_BURST
);
2829 if (PageChecked(page
)) {
2830 ClearPageChecked(page
);
2834 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2837 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2838 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2839 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2844 if (state
&& state
->start
== start
) {
2845 private = state
->private;
2848 ret
= get_state_private(io_tree
, start
, &private);
2850 kaddr
= kmap_atomic(page
);
2854 csum
= btrfs_csum_data(kaddr
+ offset
, csum
, end
- start
+ 1);
2855 btrfs_csum_final(csum
, (char *)&csum
);
2856 if (csum
!= private)
2859 kunmap_atomic(kaddr
);
2864 if (__ratelimit(&_rs
))
2865 btrfs_info(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %llu",
2866 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2867 (unsigned long long)start
, csum
,
2868 (unsigned long long)private);
2869 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2870 flush_dcache_page(page
);
2871 kunmap_atomic(kaddr
);
2877 struct delayed_iput
{
2878 struct list_head list
;
2879 struct inode
*inode
;
2882 /* JDM: If this is fs-wide, why can't we add a pointer to
2883 * btrfs_inode instead and avoid the allocation? */
2884 void btrfs_add_delayed_iput(struct inode
*inode
)
2886 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2887 struct delayed_iput
*delayed
;
2889 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2892 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2893 delayed
->inode
= inode
;
2895 spin_lock(&fs_info
->delayed_iput_lock
);
2896 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2897 spin_unlock(&fs_info
->delayed_iput_lock
);
2900 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2903 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2904 struct delayed_iput
*delayed
;
2907 spin_lock(&fs_info
->delayed_iput_lock
);
2908 empty
= list_empty(&fs_info
->delayed_iputs
);
2909 spin_unlock(&fs_info
->delayed_iput_lock
);
2913 spin_lock(&fs_info
->delayed_iput_lock
);
2914 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2915 spin_unlock(&fs_info
->delayed_iput_lock
);
2917 while (!list_empty(&list
)) {
2918 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2919 list_del(&delayed
->list
);
2920 iput(delayed
->inode
);
2926 * This is called in transaction commit time. If there are no orphan
2927 * files in the subvolume, it removes orphan item and frees block_rsv
2930 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2931 struct btrfs_root
*root
)
2933 struct btrfs_block_rsv
*block_rsv
;
2936 if (atomic_read(&root
->orphan_inodes
) ||
2937 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2940 spin_lock(&root
->orphan_lock
);
2941 if (atomic_read(&root
->orphan_inodes
)) {
2942 spin_unlock(&root
->orphan_lock
);
2946 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2947 spin_unlock(&root
->orphan_lock
);
2951 block_rsv
= root
->orphan_block_rsv
;
2952 root
->orphan_block_rsv
= NULL
;
2953 spin_unlock(&root
->orphan_lock
);
2955 if (root
->orphan_item_inserted
&&
2956 btrfs_root_refs(&root
->root_item
) > 0) {
2957 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2958 root
->root_key
.objectid
);
2960 root
->orphan_item_inserted
= 0;
2964 WARN_ON(block_rsv
->size
> 0);
2965 btrfs_free_block_rsv(root
, block_rsv
);
2970 * This creates an orphan entry for the given inode in case something goes
2971 * wrong in the middle of an unlink/truncate.
2973 * NOTE: caller of this function should reserve 5 units of metadata for
2976 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2978 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2979 struct btrfs_block_rsv
*block_rsv
= NULL
;
2984 if (!root
->orphan_block_rsv
) {
2985 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2990 spin_lock(&root
->orphan_lock
);
2991 if (!root
->orphan_block_rsv
) {
2992 root
->orphan_block_rsv
= block_rsv
;
2993 } else if (block_rsv
) {
2994 btrfs_free_block_rsv(root
, block_rsv
);
2998 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2999 &BTRFS_I(inode
)->runtime_flags
)) {
3002 * For proper ENOSPC handling, we should do orphan
3003 * cleanup when mounting. But this introduces backward
3004 * compatibility issue.
3006 if (!xchg(&root
->orphan_item_inserted
, 1))
3012 atomic_inc(&root
->orphan_inodes
);
3015 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3016 &BTRFS_I(inode
)->runtime_flags
))
3018 spin_unlock(&root
->orphan_lock
);
3020 /* grab metadata reservation from transaction handle */
3022 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
3023 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
3026 /* insert an orphan item to track this unlinked/truncated file */
3028 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
3029 if (ret
&& ret
!= -EEXIST
) {
3030 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3031 &BTRFS_I(inode
)->runtime_flags
);
3032 btrfs_abort_transaction(trans
, root
, ret
);
3038 /* insert an orphan item to track subvolume contains orphan files */
3040 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
3041 root
->root_key
.objectid
);
3042 if (ret
&& ret
!= -EEXIST
) {
3043 btrfs_abort_transaction(trans
, root
, ret
);
3051 * We have done the truncate/delete so we can go ahead and remove the orphan
3052 * item for this particular inode.
3054 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3055 struct inode
*inode
)
3057 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3058 int delete_item
= 0;
3059 int release_rsv
= 0;
3062 spin_lock(&root
->orphan_lock
);
3063 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3064 &BTRFS_I(inode
)->runtime_flags
))
3067 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3068 &BTRFS_I(inode
)->runtime_flags
))
3070 spin_unlock(&root
->orphan_lock
);
3072 if (trans
&& delete_item
) {
3073 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
3074 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3078 btrfs_orphan_release_metadata(inode
);
3079 atomic_dec(&root
->orphan_inodes
);
3086 * this cleans up any orphans that may be left on the list from the last use
3089 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3091 struct btrfs_path
*path
;
3092 struct extent_buffer
*leaf
;
3093 struct btrfs_key key
, found_key
;
3094 struct btrfs_trans_handle
*trans
;
3095 struct inode
*inode
;
3096 u64 last_objectid
= 0;
3097 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3099 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3102 path
= btrfs_alloc_path();
3109 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3110 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
3111 key
.offset
= (u64
)-1;
3114 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3119 * if ret == 0 means we found what we were searching for, which
3120 * is weird, but possible, so only screw with path if we didn't
3121 * find the key and see if we have stuff that matches
3125 if (path
->slots
[0] == 0)
3130 /* pull out the item */
3131 leaf
= path
->nodes
[0];
3132 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3134 /* make sure the item matches what we want */
3135 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3137 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
3140 /* release the path since we're done with it */
3141 btrfs_release_path(path
);
3144 * this is where we are basically btrfs_lookup, without the
3145 * crossing root thing. we store the inode number in the
3146 * offset of the orphan item.
3149 if (found_key
.offset
== last_objectid
) {
3150 btrfs_err(root
->fs_info
,
3151 "Error removing orphan entry, stopping orphan cleanup");
3156 last_objectid
= found_key
.offset
;
3158 found_key
.objectid
= found_key
.offset
;
3159 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3160 found_key
.offset
= 0;
3161 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3162 ret
= PTR_RET(inode
);
3163 if (ret
&& ret
!= -ESTALE
)
3166 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3167 struct btrfs_root
*dead_root
;
3168 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3169 int is_dead_root
= 0;
3172 * this is an orphan in the tree root. Currently these
3173 * could come from 2 sources:
3174 * a) a snapshot deletion in progress
3175 * b) a free space cache inode
3176 * We need to distinguish those two, as the snapshot
3177 * orphan must not get deleted.
3178 * find_dead_roots already ran before us, so if this
3179 * is a snapshot deletion, we should find the root
3180 * in the dead_roots list
3182 spin_lock(&fs_info
->trans_lock
);
3183 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3185 if (dead_root
->root_key
.objectid
==
3186 found_key
.objectid
) {
3191 spin_unlock(&fs_info
->trans_lock
);
3193 /* prevent this orphan from being found again */
3194 key
.offset
= found_key
.objectid
- 1;
3199 * Inode is already gone but the orphan item is still there,
3200 * kill the orphan item.
3202 if (ret
== -ESTALE
) {
3203 trans
= btrfs_start_transaction(root
, 1);
3204 if (IS_ERR(trans
)) {
3205 ret
= PTR_ERR(trans
);
3208 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3209 found_key
.objectid
);
3210 ret
= btrfs_del_orphan_item(trans
, root
,
3211 found_key
.objectid
);
3212 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3213 btrfs_end_transaction(trans
, root
);
3218 * add this inode to the orphan list so btrfs_orphan_del does
3219 * the proper thing when we hit it
3221 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3222 &BTRFS_I(inode
)->runtime_flags
);
3223 atomic_inc(&root
->orphan_inodes
);
3225 /* if we have links, this was a truncate, lets do that */
3226 if (inode
->i_nlink
) {
3227 if (!S_ISREG(inode
->i_mode
)) {
3234 /* 1 for the orphan item deletion. */
3235 trans
= btrfs_start_transaction(root
, 1);
3236 if (IS_ERR(trans
)) {
3237 ret
= PTR_ERR(trans
);
3240 ret
= btrfs_orphan_add(trans
, inode
);
3241 btrfs_end_transaction(trans
, root
);
3245 ret
= btrfs_truncate(inode
);
3247 btrfs_orphan_del(NULL
, inode
);
3252 /* this will do delete_inode and everything for us */
3257 /* release the path since we're done with it */
3258 btrfs_release_path(path
);
3260 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3262 if (root
->orphan_block_rsv
)
3263 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3266 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
3267 trans
= btrfs_join_transaction(root
);
3269 btrfs_end_transaction(trans
, root
);
3273 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3275 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3279 btrfs_crit(root
->fs_info
,
3280 "could not do orphan cleanup %d", ret
);
3281 btrfs_free_path(path
);
3286 * very simple check to peek ahead in the leaf looking for xattrs. If we
3287 * don't find any xattrs, we know there can't be any acls.
3289 * slot is the slot the inode is in, objectid is the objectid of the inode
3291 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3292 int slot
, u64 objectid
)
3294 u32 nritems
= btrfs_header_nritems(leaf
);
3295 struct btrfs_key found_key
;
3299 while (slot
< nritems
) {
3300 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3302 /* we found a different objectid, there must not be acls */
3303 if (found_key
.objectid
!= objectid
)
3306 /* we found an xattr, assume we've got an acl */
3307 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
3311 * we found a key greater than an xattr key, there can't
3312 * be any acls later on
3314 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3321 * it goes inode, inode backrefs, xattrs, extents,
3322 * so if there are a ton of hard links to an inode there can
3323 * be a lot of backrefs. Don't waste time searching too hard,
3324 * this is just an optimization
3329 /* we hit the end of the leaf before we found an xattr or
3330 * something larger than an xattr. We have to assume the inode
3337 * read an inode from the btree into the in-memory inode
3339 static void btrfs_read_locked_inode(struct inode
*inode
)
3341 struct btrfs_path
*path
;
3342 struct extent_buffer
*leaf
;
3343 struct btrfs_inode_item
*inode_item
;
3344 struct btrfs_timespec
*tspec
;
3345 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3346 struct btrfs_key location
;
3350 bool filled
= false;
3352 ret
= btrfs_fill_inode(inode
, &rdev
);
3356 path
= btrfs_alloc_path();
3360 path
->leave_spinning
= 1;
3361 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3363 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3367 leaf
= path
->nodes
[0];
3372 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3373 struct btrfs_inode_item
);
3374 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3375 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3376 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3377 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3378 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3380 tspec
= btrfs_inode_atime(inode_item
);
3381 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3382 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3384 tspec
= btrfs_inode_mtime(inode_item
);
3385 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3386 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3388 tspec
= btrfs_inode_ctime(inode_item
);
3389 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3390 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3392 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3393 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3394 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3397 * If we were modified in the current generation and evicted from memory
3398 * and then re-read we need to do a full sync since we don't have any
3399 * idea about which extents were modified before we were evicted from
3402 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3403 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3404 &BTRFS_I(inode
)->runtime_flags
);
3406 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3407 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3409 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3411 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3412 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3415 * try to precache a NULL acl entry for files that don't have
3416 * any xattrs or acls
3418 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3421 cache_no_acl(inode
);
3423 btrfs_free_path(path
);
3425 switch (inode
->i_mode
& S_IFMT
) {
3427 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3428 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3429 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3430 inode
->i_fop
= &btrfs_file_operations
;
3431 inode
->i_op
= &btrfs_file_inode_operations
;
3434 inode
->i_fop
= &btrfs_dir_file_operations
;
3435 if (root
== root
->fs_info
->tree_root
)
3436 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3438 inode
->i_op
= &btrfs_dir_inode_operations
;
3441 inode
->i_op
= &btrfs_symlink_inode_operations
;
3442 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3443 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3446 inode
->i_op
= &btrfs_special_inode_operations
;
3447 init_special_inode(inode
, inode
->i_mode
, rdev
);
3451 btrfs_update_iflags(inode
);
3455 btrfs_free_path(path
);
3456 make_bad_inode(inode
);
3460 * given a leaf and an inode, copy the inode fields into the leaf
3462 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3463 struct extent_buffer
*leaf
,
3464 struct btrfs_inode_item
*item
,
3465 struct inode
*inode
)
3467 struct btrfs_map_token token
;
3469 btrfs_init_map_token(&token
);
3471 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3472 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3473 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3475 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3476 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3478 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
3479 inode
->i_atime
.tv_sec
, &token
);
3480 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
3481 inode
->i_atime
.tv_nsec
, &token
);
3483 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
3484 inode
->i_mtime
.tv_sec
, &token
);
3485 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3486 inode
->i_mtime
.tv_nsec
, &token
);
3488 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3489 inode
->i_ctime
.tv_sec
, &token
);
3490 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3491 inode
->i_ctime
.tv_nsec
, &token
);
3493 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3495 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3497 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3498 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3499 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3500 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3501 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3505 * copy everything in the in-memory inode into the btree.
3507 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3508 struct btrfs_root
*root
, struct inode
*inode
)
3510 struct btrfs_inode_item
*inode_item
;
3511 struct btrfs_path
*path
;
3512 struct extent_buffer
*leaf
;
3515 path
= btrfs_alloc_path();
3519 path
->leave_spinning
= 1;
3520 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3528 btrfs_unlock_up_safe(path
, 1);
3529 leaf
= path
->nodes
[0];
3530 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3531 struct btrfs_inode_item
);
3533 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3534 btrfs_mark_buffer_dirty(leaf
);
3535 btrfs_set_inode_last_trans(trans
, inode
);
3538 btrfs_free_path(path
);
3543 * copy everything in the in-memory inode into the btree.
3545 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3546 struct btrfs_root
*root
, struct inode
*inode
)
3551 * If the inode is a free space inode, we can deadlock during commit
3552 * if we put it into the delayed code.
3554 * The data relocation inode should also be directly updated
3557 if (!btrfs_is_free_space_inode(inode
)
3558 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
3559 btrfs_update_root_times(trans
, root
);
3561 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3563 btrfs_set_inode_last_trans(trans
, inode
);
3567 return btrfs_update_inode_item(trans
, root
, inode
);
3570 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3571 struct btrfs_root
*root
,
3572 struct inode
*inode
)
3576 ret
= btrfs_update_inode(trans
, root
, inode
);
3578 return btrfs_update_inode_item(trans
, root
, inode
);
3583 * unlink helper that gets used here in inode.c and in the tree logging
3584 * recovery code. It remove a link in a directory with a given name, and
3585 * also drops the back refs in the inode to the directory
3587 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3588 struct btrfs_root
*root
,
3589 struct inode
*dir
, struct inode
*inode
,
3590 const char *name
, int name_len
)
3592 struct btrfs_path
*path
;
3594 struct extent_buffer
*leaf
;
3595 struct btrfs_dir_item
*di
;
3596 struct btrfs_key key
;
3598 u64 ino
= btrfs_ino(inode
);
3599 u64 dir_ino
= btrfs_ino(dir
);
3601 path
= btrfs_alloc_path();
3607 path
->leave_spinning
= 1;
3608 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3609 name
, name_len
, -1);
3618 leaf
= path
->nodes
[0];
3619 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3620 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3623 btrfs_release_path(path
);
3625 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3628 btrfs_info(root
->fs_info
,
3629 "failed to delete reference to %.*s, inode %llu parent %llu",
3631 (unsigned long long)ino
, (unsigned long long)dir_ino
);
3632 btrfs_abort_transaction(trans
, root
, ret
);
3636 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3638 btrfs_abort_transaction(trans
, root
, ret
);
3642 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
3644 if (ret
!= 0 && ret
!= -ENOENT
) {
3645 btrfs_abort_transaction(trans
, root
, ret
);
3649 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
3654 btrfs_abort_transaction(trans
, root
, ret
);
3656 btrfs_free_path(path
);
3660 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3661 inode_inc_iversion(inode
);
3662 inode_inc_iversion(dir
);
3663 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3664 ret
= btrfs_update_inode(trans
, root
, dir
);
3669 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3670 struct btrfs_root
*root
,
3671 struct inode
*dir
, struct inode
*inode
,
3672 const char *name
, int name_len
)
3675 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3677 btrfs_drop_nlink(inode
);
3678 ret
= btrfs_update_inode(trans
, root
, inode
);
3684 * helper to start transaction for unlink and rmdir.
3686 * unlink and rmdir are special in btrfs, they do not always free space, so
3687 * if we cannot make our reservations the normal way try and see if there is
3688 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3689 * allow the unlink to occur.
3691 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
)
3693 struct btrfs_trans_handle
*trans
;
3694 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3698 * 1 for the possible orphan item
3699 * 1 for the dir item
3700 * 1 for the dir index
3701 * 1 for the inode ref
3704 trans
= btrfs_start_transaction(root
, 5);
3705 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3708 if (PTR_ERR(trans
) == -ENOSPC
) {
3709 u64 num_bytes
= btrfs_calc_trans_metadata_size(root
, 5);
3711 trans
= btrfs_start_transaction(root
, 0);
3714 ret
= btrfs_cond_migrate_bytes(root
->fs_info
,
3715 &root
->fs_info
->trans_block_rsv
,
3718 btrfs_end_transaction(trans
, root
);
3719 return ERR_PTR(ret
);
3721 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3722 trans
->bytes_reserved
= num_bytes
;
3727 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3729 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3730 struct btrfs_trans_handle
*trans
;
3731 struct inode
*inode
= dentry
->d_inode
;
3734 trans
= __unlink_start_trans(dir
);
3736 return PTR_ERR(trans
);
3738 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3740 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3741 dentry
->d_name
.name
, dentry
->d_name
.len
);
3745 if (inode
->i_nlink
== 0) {
3746 ret
= btrfs_orphan_add(trans
, inode
);
3752 btrfs_end_transaction(trans
, root
);
3753 btrfs_btree_balance_dirty(root
);
3757 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3758 struct btrfs_root
*root
,
3759 struct inode
*dir
, u64 objectid
,
3760 const char *name
, int name_len
)
3762 struct btrfs_path
*path
;
3763 struct extent_buffer
*leaf
;
3764 struct btrfs_dir_item
*di
;
3765 struct btrfs_key key
;
3768 u64 dir_ino
= btrfs_ino(dir
);
3770 path
= btrfs_alloc_path();
3774 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3775 name
, name_len
, -1);
3776 if (IS_ERR_OR_NULL(di
)) {
3784 leaf
= path
->nodes
[0];
3785 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3786 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3787 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3789 btrfs_abort_transaction(trans
, root
, ret
);
3792 btrfs_release_path(path
);
3794 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3795 objectid
, root
->root_key
.objectid
,
3796 dir_ino
, &index
, name
, name_len
);
3798 if (ret
!= -ENOENT
) {
3799 btrfs_abort_transaction(trans
, root
, ret
);
3802 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3804 if (IS_ERR_OR_NULL(di
)) {
3809 btrfs_abort_transaction(trans
, root
, ret
);
3813 leaf
= path
->nodes
[0];
3814 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3815 btrfs_release_path(path
);
3818 btrfs_release_path(path
);
3820 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3822 btrfs_abort_transaction(trans
, root
, ret
);
3826 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3827 inode_inc_iversion(dir
);
3828 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3829 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3831 btrfs_abort_transaction(trans
, root
, ret
);
3833 btrfs_free_path(path
);
3837 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3839 struct inode
*inode
= dentry
->d_inode
;
3841 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3842 struct btrfs_trans_handle
*trans
;
3844 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3846 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3849 trans
= __unlink_start_trans(dir
);
3851 return PTR_ERR(trans
);
3853 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3854 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3855 BTRFS_I(inode
)->location
.objectid
,
3856 dentry
->d_name
.name
,
3857 dentry
->d_name
.len
);
3861 err
= btrfs_orphan_add(trans
, inode
);
3865 /* now the directory is empty */
3866 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3867 dentry
->d_name
.name
, dentry
->d_name
.len
);
3869 btrfs_i_size_write(inode
, 0);
3871 btrfs_end_transaction(trans
, root
);
3872 btrfs_btree_balance_dirty(root
);
3878 * this can truncate away extent items, csum items and directory items.
3879 * It starts at a high offset and removes keys until it can't find
3880 * any higher than new_size
3882 * csum items that cross the new i_size are truncated to the new size
3885 * min_type is the minimum key type to truncate down to. If set to 0, this
3886 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3888 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
3889 struct btrfs_root
*root
,
3890 struct inode
*inode
,
3891 u64 new_size
, u32 min_type
)
3893 struct btrfs_path
*path
;
3894 struct extent_buffer
*leaf
;
3895 struct btrfs_file_extent_item
*fi
;
3896 struct btrfs_key key
;
3897 struct btrfs_key found_key
;
3898 u64 extent_start
= 0;
3899 u64 extent_num_bytes
= 0;
3900 u64 extent_offset
= 0;
3902 u32 found_type
= (u8
)-1;
3905 int pending_del_nr
= 0;
3906 int pending_del_slot
= 0;
3907 int extent_type
= -1;
3910 u64 ino
= btrfs_ino(inode
);
3912 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
3914 path
= btrfs_alloc_path();
3920 * We want to drop from the next block forward in case this new size is
3921 * not block aligned since we will be keeping the last block of the
3922 * extent just the way it is.
3924 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
3925 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
3926 root
->sectorsize
), (u64
)-1, 0);
3929 * This function is also used to drop the items in the log tree before
3930 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3931 * it is used to drop the loged items. So we shouldn't kill the delayed
3934 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
3935 btrfs_kill_delayed_inode_items(inode
);
3938 key
.offset
= (u64
)-1;
3942 path
->leave_spinning
= 1;
3943 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
3950 /* there are no items in the tree for us to truncate, we're
3953 if (path
->slots
[0] == 0)
3960 leaf
= path
->nodes
[0];
3961 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3962 found_type
= btrfs_key_type(&found_key
);
3964 if (found_key
.objectid
!= ino
)
3967 if (found_type
< min_type
)
3970 item_end
= found_key
.offset
;
3971 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
3972 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
3973 struct btrfs_file_extent_item
);
3974 extent_type
= btrfs_file_extent_type(leaf
, fi
);
3975 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
3977 btrfs_file_extent_num_bytes(leaf
, fi
);
3978 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
3979 item_end
+= btrfs_file_extent_inline_len(leaf
,
3984 if (found_type
> min_type
) {
3987 if (item_end
< new_size
)
3989 if (found_key
.offset
>= new_size
)
3995 /* FIXME, shrink the extent if the ref count is only 1 */
3996 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
3999 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4001 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4003 u64 orig_num_bytes
=
4004 btrfs_file_extent_num_bytes(leaf
, fi
);
4005 extent_num_bytes
= ALIGN(new_size
-
4008 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4010 num_dec
= (orig_num_bytes
-
4012 if (root
->ref_cows
&& extent_start
!= 0)
4013 inode_sub_bytes(inode
, num_dec
);
4014 btrfs_mark_buffer_dirty(leaf
);
4017 btrfs_file_extent_disk_num_bytes(leaf
,
4019 extent_offset
= found_key
.offset
-
4020 btrfs_file_extent_offset(leaf
, fi
);
4022 /* FIXME blocksize != 4096 */
4023 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4024 if (extent_start
!= 0) {
4027 inode_sub_bytes(inode
, num_dec
);
4030 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4032 * we can't truncate inline items that have had
4036 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4037 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4038 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4039 u32 size
= new_size
- found_key
.offset
;
4041 if (root
->ref_cows
) {
4042 inode_sub_bytes(inode
, item_end
+ 1 -
4046 btrfs_file_extent_calc_inline_size(size
);
4047 btrfs_truncate_item(root
, path
, size
, 1);
4048 } else if (root
->ref_cows
) {
4049 inode_sub_bytes(inode
, item_end
+ 1 -
4055 if (!pending_del_nr
) {
4056 /* no pending yet, add ourselves */
4057 pending_del_slot
= path
->slots
[0];
4059 } else if (pending_del_nr
&&
4060 path
->slots
[0] + 1 == pending_del_slot
) {
4061 /* hop on the pending chunk */
4063 pending_del_slot
= path
->slots
[0];
4070 if (found_extent
&& (root
->ref_cows
||
4071 root
== root
->fs_info
->tree_root
)) {
4072 btrfs_set_path_blocking(path
);
4073 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4074 extent_num_bytes
, 0,
4075 btrfs_header_owner(leaf
),
4076 ino
, extent_offset
, 0);
4080 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4083 if (path
->slots
[0] == 0 ||
4084 path
->slots
[0] != pending_del_slot
) {
4085 if (pending_del_nr
) {
4086 ret
= btrfs_del_items(trans
, root
, path
,
4090 btrfs_abort_transaction(trans
,
4096 btrfs_release_path(path
);
4103 if (pending_del_nr
) {
4104 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4107 btrfs_abort_transaction(trans
, root
, ret
);
4110 btrfs_free_path(path
);
4115 * btrfs_truncate_page - read, zero a chunk and write a page
4116 * @inode - inode that we're zeroing
4117 * @from - the offset to start zeroing
4118 * @len - the length to zero, 0 to zero the entire range respective to the
4120 * @front - zero up to the offset instead of from the offset on
4122 * This will find the page for the "from" offset and cow the page and zero the
4123 * part we want to zero. This is used with truncate and hole punching.
4125 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4128 struct address_space
*mapping
= inode
->i_mapping
;
4129 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4130 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4131 struct btrfs_ordered_extent
*ordered
;
4132 struct extent_state
*cached_state
= NULL
;
4134 u32 blocksize
= root
->sectorsize
;
4135 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4136 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4138 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4143 if ((offset
& (blocksize
- 1)) == 0 &&
4144 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4146 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4151 page
= find_or_create_page(mapping
, index
, mask
);
4153 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4158 page_start
= page_offset(page
);
4159 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4161 if (!PageUptodate(page
)) {
4162 ret
= btrfs_readpage(NULL
, page
);
4164 if (page
->mapping
!= mapping
) {
4166 page_cache_release(page
);
4169 if (!PageUptodate(page
)) {
4174 wait_on_page_writeback(page
);
4176 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4177 set_page_extent_mapped(page
);
4179 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4181 unlock_extent_cached(io_tree
, page_start
, page_end
,
4182 &cached_state
, GFP_NOFS
);
4184 page_cache_release(page
);
4185 btrfs_start_ordered_extent(inode
, ordered
, 1);
4186 btrfs_put_ordered_extent(ordered
);
4190 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4191 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4192 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4193 0, 0, &cached_state
, GFP_NOFS
);
4195 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4198 unlock_extent_cached(io_tree
, page_start
, page_end
,
4199 &cached_state
, GFP_NOFS
);
4203 if (offset
!= PAGE_CACHE_SIZE
) {
4205 len
= PAGE_CACHE_SIZE
- offset
;
4208 memset(kaddr
, 0, offset
);
4210 memset(kaddr
+ offset
, 0, len
);
4211 flush_dcache_page(page
);
4214 ClearPageChecked(page
);
4215 set_page_dirty(page
);
4216 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4221 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4223 page_cache_release(page
);
4229 * This function puts in dummy file extents for the area we're creating a hole
4230 * for. So if we are truncating this file to a larger size we need to insert
4231 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4232 * the range between oldsize and size
4234 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4236 struct btrfs_trans_handle
*trans
;
4237 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4238 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4239 struct extent_map
*em
= NULL
;
4240 struct extent_state
*cached_state
= NULL
;
4241 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4242 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4243 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4249 if (size
<= hole_start
)
4253 struct btrfs_ordered_extent
*ordered
;
4254 btrfs_wait_ordered_range(inode
, hole_start
,
4255 block_end
- hole_start
);
4256 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4258 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
4261 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4262 &cached_state
, GFP_NOFS
);
4263 btrfs_put_ordered_extent(ordered
);
4266 cur_offset
= hole_start
;
4268 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4269 block_end
- cur_offset
, 0);
4275 last_byte
= min(extent_map_end(em
), block_end
);
4276 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4277 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4278 struct extent_map
*hole_em
;
4279 hole_size
= last_byte
- cur_offset
;
4281 trans
= btrfs_start_transaction(root
, 3);
4282 if (IS_ERR(trans
)) {
4283 err
= PTR_ERR(trans
);
4287 err
= btrfs_drop_extents(trans
, root
, inode
,
4289 cur_offset
+ hole_size
, 1);
4291 btrfs_abort_transaction(trans
, root
, err
);
4292 btrfs_end_transaction(trans
, root
);
4296 err
= btrfs_insert_file_extent(trans
, root
,
4297 btrfs_ino(inode
), cur_offset
, 0,
4298 0, hole_size
, 0, hole_size
,
4301 btrfs_abort_transaction(trans
, root
, err
);
4302 btrfs_end_transaction(trans
, root
);
4306 btrfs_drop_extent_cache(inode
, cur_offset
,
4307 cur_offset
+ hole_size
- 1, 0);
4308 hole_em
= alloc_extent_map();
4310 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4311 &BTRFS_I(inode
)->runtime_flags
);
4314 hole_em
->start
= cur_offset
;
4315 hole_em
->len
= hole_size
;
4316 hole_em
->orig_start
= cur_offset
;
4318 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4319 hole_em
->block_len
= 0;
4320 hole_em
->orig_block_len
= 0;
4321 hole_em
->ram_bytes
= hole_size
;
4322 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4323 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4324 hole_em
->generation
= trans
->transid
;
4327 write_lock(&em_tree
->lock
);
4328 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4329 write_unlock(&em_tree
->lock
);
4332 btrfs_drop_extent_cache(inode
, cur_offset
,
4336 free_extent_map(hole_em
);
4338 btrfs_update_inode(trans
, root
, inode
);
4339 btrfs_end_transaction(trans
, root
);
4341 free_extent_map(em
);
4343 cur_offset
= last_byte
;
4344 if (cur_offset
>= block_end
)
4348 free_extent_map(em
);
4349 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4354 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4356 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4357 struct btrfs_trans_handle
*trans
;
4358 loff_t oldsize
= i_size_read(inode
);
4359 loff_t newsize
= attr
->ia_size
;
4360 int mask
= attr
->ia_valid
;
4363 if (newsize
== oldsize
)
4367 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4368 * special case where we need to update the times despite not having
4369 * these flags set. For all other operations the VFS set these flags
4370 * explicitly if it wants a timestamp update.
4372 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
4373 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
4375 if (newsize
> oldsize
) {
4376 truncate_pagecache(inode
, oldsize
, newsize
);
4377 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4381 trans
= btrfs_start_transaction(root
, 1);
4383 return PTR_ERR(trans
);
4385 i_size_write(inode
, newsize
);
4386 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4387 ret
= btrfs_update_inode(trans
, root
, inode
);
4388 btrfs_end_transaction(trans
, root
);
4392 * We're truncating a file that used to have good data down to
4393 * zero. Make sure it gets into the ordered flush list so that
4394 * any new writes get down to disk quickly.
4397 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4398 &BTRFS_I(inode
)->runtime_flags
);
4401 * 1 for the orphan item we're going to add
4402 * 1 for the orphan item deletion.
4404 trans
= btrfs_start_transaction(root
, 2);
4406 return PTR_ERR(trans
);
4409 * We need to do this in case we fail at _any_ point during the
4410 * actual truncate. Once we do the truncate_setsize we could
4411 * invalidate pages which forces any outstanding ordered io to
4412 * be instantly completed which will give us extents that need
4413 * to be truncated. If we fail to get an orphan inode down we
4414 * could have left over extents that were never meant to live,
4415 * so we need to garuntee from this point on that everything
4416 * will be consistent.
4418 ret
= btrfs_orphan_add(trans
, inode
);
4419 btrfs_end_transaction(trans
, root
);
4423 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4424 truncate_setsize(inode
, newsize
);
4426 /* Disable nonlocked read DIO to avoid the end less truncate */
4427 btrfs_inode_block_unlocked_dio(inode
);
4428 inode_dio_wait(inode
);
4429 btrfs_inode_resume_unlocked_dio(inode
);
4431 ret
= btrfs_truncate(inode
);
4432 if (ret
&& inode
->i_nlink
)
4433 btrfs_orphan_del(NULL
, inode
);
4439 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4441 struct inode
*inode
= dentry
->d_inode
;
4442 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4445 if (btrfs_root_readonly(root
))
4448 err
= inode_change_ok(inode
, attr
);
4452 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4453 err
= btrfs_setsize(inode
, attr
);
4458 if (attr
->ia_valid
) {
4459 setattr_copy(inode
, attr
);
4460 inode_inc_iversion(inode
);
4461 err
= btrfs_dirty_inode(inode
);
4463 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4464 err
= btrfs_acl_chmod(inode
);
4470 void btrfs_evict_inode(struct inode
*inode
)
4472 struct btrfs_trans_handle
*trans
;
4473 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4474 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4475 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4478 trace_btrfs_inode_evict(inode
);
4480 truncate_inode_pages(&inode
->i_data
, 0);
4481 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
4482 btrfs_is_free_space_inode(inode
)))
4485 if (is_bad_inode(inode
)) {
4486 btrfs_orphan_del(NULL
, inode
);
4489 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4490 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4492 if (root
->fs_info
->log_root_recovering
) {
4493 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4494 &BTRFS_I(inode
)->runtime_flags
));
4498 if (inode
->i_nlink
> 0) {
4499 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
4503 ret
= btrfs_commit_inode_delayed_inode(inode
);
4505 btrfs_orphan_del(NULL
, inode
);
4509 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4511 btrfs_orphan_del(NULL
, inode
);
4514 rsv
->size
= min_size
;
4516 global_rsv
= &root
->fs_info
->global_block_rsv
;
4518 btrfs_i_size_write(inode
, 0);
4521 * This is a bit simpler than btrfs_truncate since we've already
4522 * reserved our space for our orphan item in the unlink, so we just
4523 * need to reserve some slack space in case we add bytes and update
4524 * inode item when doing the truncate.
4527 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4528 BTRFS_RESERVE_FLUSH_LIMIT
);
4531 * Try and steal from the global reserve since we will
4532 * likely not use this space anyway, we want to try as
4533 * hard as possible to get this to work.
4536 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4539 btrfs_warn(root
->fs_info
,
4540 "Could not get space for a delete, will truncate on mount %d",
4542 btrfs_orphan_del(NULL
, inode
);
4543 btrfs_free_block_rsv(root
, rsv
);
4547 trans
= btrfs_join_transaction(root
);
4548 if (IS_ERR(trans
)) {
4549 btrfs_orphan_del(NULL
, inode
);
4550 btrfs_free_block_rsv(root
, rsv
);
4554 trans
->block_rsv
= rsv
;
4556 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4560 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4561 btrfs_end_transaction(trans
, root
);
4563 btrfs_btree_balance_dirty(root
);
4566 btrfs_free_block_rsv(root
, rsv
);
4569 trans
->block_rsv
= root
->orphan_block_rsv
;
4570 ret
= btrfs_orphan_del(trans
, inode
);
4574 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4575 if (!(root
== root
->fs_info
->tree_root
||
4576 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4577 btrfs_return_ino(root
, btrfs_ino(inode
));
4579 btrfs_end_transaction(trans
, root
);
4580 btrfs_btree_balance_dirty(root
);
4582 btrfs_remove_delayed_node(inode
);
4588 * this returns the key found in the dir entry in the location pointer.
4589 * If no dir entries were found, location->objectid is 0.
4591 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4592 struct btrfs_key
*location
)
4594 const char *name
= dentry
->d_name
.name
;
4595 int namelen
= dentry
->d_name
.len
;
4596 struct btrfs_dir_item
*di
;
4597 struct btrfs_path
*path
;
4598 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4601 path
= btrfs_alloc_path();
4605 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4610 if (IS_ERR_OR_NULL(di
))
4613 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4615 btrfs_free_path(path
);
4618 location
->objectid
= 0;
4623 * when we hit a tree root in a directory, the btrfs part of the inode
4624 * needs to be changed to reflect the root directory of the tree root. This
4625 * is kind of like crossing a mount point.
4627 static int fixup_tree_root_location(struct btrfs_root
*root
,
4629 struct dentry
*dentry
,
4630 struct btrfs_key
*location
,
4631 struct btrfs_root
**sub_root
)
4633 struct btrfs_path
*path
;
4634 struct btrfs_root
*new_root
;
4635 struct btrfs_root_ref
*ref
;
4636 struct extent_buffer
*leaf
;
4640 path
= btrfs_alloc_path();
4647 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4648 BTRFS_I(dir
)->root
->root_key
.objectid
,
4649 location
->objectid
);
4656 leaf
= path
->nodes
[0];
4657 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4658 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4659 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4662 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4663 (unsigned long)(ref
+ 1),
4664 dentry
->d_name
.len
);
4668 btrfs_release_path(path
);
4670 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4671 if (IS_ERR(new_root
)) {
4672 err
= PTR_ERR(new_root
);
4676 *sub_root
= new_root
;
4677 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4678 location
->type
= BTRFS_INODE_ITEM_KEY
;
4679 location
->offset
= 0;
4682 btrfs_free_path(path
);
4686 static void inode_tree_add(struct inode
*inode
)
4688 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4689 struct btrfs_inode
*entry
;
4691 struct rb_node
*parent
;
4692 u64 ino
= btrfs_ino(inode
);
4694 if (inode_unhashed(inode
))
4698 spin_lock(&root
->inode_lock
);
4699 p
= &root
->inode_tree
.rb_node
;
4702 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4704 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4705 p
= &parent
->rb_left
;
4706 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4707 p
= &parent
->rb_right
;
4709 WARN_ON(!(entry
->vfs_inode
.i_state
&
4710 (I_WILL_FREE
| I_FREEING
)));
4711 rb_erase(parent
, &root
->inode_tree
);
4712 RB_CLEAR_NODE(parent
);
4713 spin_unlock(&root
->inode_lock
);
4717 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
4718 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4719 spin_unlock(&root
->inode_lock
);
4722 static void inode_tree_del(struct inode
*inode
)
4724 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4727 spin_lock(&root
->inode_lock
);
4728 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4729 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4730 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4731 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4733 spin_unlock(&root
->inode_lock
);
4736 * Free space cache has inodes in the tree root, but the tree root has a
4737 * root_refs of 0, so this could end up dropping the tree root as a
4738 * snapshot, so we need the extra !root->fs_info->tree_root check to
4739 * make sure we don't drop it.
4741 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
4742 root
!= root
->fs_info
->tree_root
) {
4743 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4744 spin_lock(&root
->inode_lock
);
4745 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4746 spin_unlock(&root
->inode_lock
);
4748 btrfs_add_dead_root(root
);
4752 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4754 struct rb_node
*node
;
4755 struct rb_node
*prev
;
4756 struct btrfs_inode
*entry
;
4757 struct inode
*inode
;
4760 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4762 spin_lock(&root
->inode_lock
);
4764 node
= root
->inode_tree
.rb_node
;
4768 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4770 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4771 node
= node
->rb_left
;
4772 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4773 node
= node
->rb_right
;
4779 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4780 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4784 prev
= rb_next(prev
);
4788 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4789 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4790 inode
= igrab(&entry
->vfs_inode
);
4792 spin_unlock(&root
->inode_lock
);
4793 if (atomic_read(&inode
->i_count
) > 1)
4794 d_prune_aliases(inode
);
4796 * btrfs_drop_inode will have it removed from
4797 * the inode cache when its usage count
4802 spin_lock(&root
->inode_lock
);
4806 if (cond_resched_lock(&root
->inode_lock
))
4809 node
= rb_next(node
);
4811 spin_unlock(&root
->inode_lock
);
4814 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4816 struct btrfs_iget_args
*args
= p
;
4817 inode
->i_ino
= args
->ino
;
4818 BTRFS_I(inode
)->root
= args
->root
;
4822 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4824 struct btrfs_iget_args
*args
= opaque
;
4825 return args
->ino
== btrfs_ino(inode
) &&
4826 args
->root
== BTRFS_I(inode
)->root
;
4829 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4831 struct btrfs_root
*root
)
4833 struct inode
*inode
;
4834 struct btrfs_iget_args args
;
4835 args
.ino
= objectid
;
4838 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4839 btrfs_init_locked_inode
,
4844 /* Get an inode object given its location and corresponding root.
4845 * Returns in *is_new if the inode was read from disk
4847 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4848 struct btrfs_root
*root
, int *new)
4850 struct inode
*inode
;
4852 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4854 return ERR_PTR(-ENOMEM
);
4856 if (inode
->i_state
& I_NEW
) {
4857 BTRFS_I(inode
)->root
= root
;
4858 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
4859 btrfs_read_locked_inode(inode
);
4860 if (!is_bad_inode(inode
)) {
4861 inode_tree_add(inode
);
4862 unlock_new_inode(inode
);
4866 unlock_new_inode(inode
);
4868 inode
= ERR_PTR(-ESTALE
);
4875 static struct inode
*new_simple_dir(struct super_block
*s
,
4876 struct btrfs_key
*key
,
4877 struct btrfs_root
*root
)
4879 struct inode
*inode
= new_inode(s
);
4882 return ERR_PTR(-ENOMEM
);
4884 BTRFS_I(inode
)->root
= root
;
4885 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
4886 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
4888 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
4889 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
4890 inode
->i_fop
= &simple_dir_operations
;
4891 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
4892 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
4897 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
4899 struct inode
*inode
;
4900 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4901 struct btrfs_root
*sub_root
= root
;
4902 struct btrfs_key location
;
4906 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
4907 return ERR_PTR(-ENAMETOOLONG
);
4909 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
4911 return ERR_PTR(ret
);
4913 if (location
.objectid
== 0)
4916 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
4917 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
4921 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
4923 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
4924 ret
= fixup_tree_root_location(root
, dir
, dentry
,
4925 &location
, &sub_root
);
4928 inode
= ERR_PTR(ret
);
4930 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
4932 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
4934 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
4936 if (!IS_ERR(inode
) && root
!= sub_root
) {
4937 down_read(&root
->fs_info
->cleanup_work_sem
);
4938 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
4939 ret
= btrfs_orphan_cleanup(sub_root
);
4940 up_read(&root
->fs_info
->cleanup_work_sem
);
4942 inode
= ERR_PTR(ret
);
4948 static int btrfs_dentry_delete(const struct dentry
*dentry
)
4950 struct btrfs_root
*root
;
4951 struct inode
*inode
= dentry
->d_inode
;
4953 if (!inode
&& !IS_ROOT(dentry
))
4954 inode
= dentry
->d_parent
->d_inode
;
4957 root
= BTRFS_I(inode
)->root
;
4958 if (btrfs_root_refs(&root
->root_item
) == 0)
4961 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
4967 static void btrfs_dentry_release(struct dentry
*dentry
)
4969 if (dentry
->d_fsdata
)
4970 kfree(dentry
->d_fsdata
);
4973 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
4978 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
4982 unsigned char btrfs_filetype_table
[] = {
4983 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
4986 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
4989 struct inode
*inode
= file_inode(filp
);
4990 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4991 struct btrfs_item
*item
;
4992 struct btrfs_dir_item
*di
;
4993 struct btrfs_key key
;
4994 struct btrfs_key found_key
;
4995 struct btrfs_path
*path
;
4996 struct list_head ins_list
;
4997 struct list_head del_list
;
4999 struct extent_buffer
*leaf
;
5001 unsigned char d_type
;
5006 int key_type
= BTRFS_DIR_INDEX_KEY
;
5010 int is_curr
= 0; /* filp->f_pos points to the current index? */
5012 /* FIXME, use a real flag for deciding about the key type */
5013 if (root
->fs_info
->tree_root
== root
)
5014 key_type
= BTRFS_DIR_ITEM_KEY
;
5016 /* special case for "." */
5017 if (filp
->f_pos
== 0) {
5018 over
= filldir(dirent
, ".", 1,
5019 filp
->f_pos
, btrfs_ino(inode
), DT_DIR
);
5024 /* special case for .., just use the back ref */
5025 if (filp
->f_pos
== 1) {
5026 u64 pino
= parent_ino(filp
->f_path
.dentry
);
5027 over
= filldir(dirent
, "..", 2,
5028 filp
->f_pos
, pino
, DT_DIR
);
5033 path
= btrfs_alloc_path();
5039 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5040 INIT_LIST_HEAD(&ins_list
);
5041 INIT_LIST_HEAD(&del_list
);
5042 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5045 btrfs_set_key_type(&key
, key_type
);
5046 key
.offset
= filp
->f_pos
;
5047 key
.objectid
= btrfs_ino(inode
);
5049 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5054 leaf
= path
->nodes
[0];
5055 slot
= path
->slots
[0];
5056 if (slot
>= btrfs_header_nritems(leaf
)) {
5057 ret
= btrfs_next_leaf(root
, path
);
5065 item
= btrfs_item_nr(leaf
, slot
);
5066 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5068 if (found_key
.objectid
!= key
.objectid
)
5070 if (btrfs_key_type(&found_key
) != key_type
)
5072 if (found_key
.offset
< filp
->f_pos
)
5074 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5075 btrfs_should_delete_dir_index(&del_list
,
5079 filp
->f_pos
= found_key
.offset
;
5082 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5084 di_total
= btrfs_item_size(leaf
, item
);
5086 while (di_cur
< di_total
) {
5087 struct btrfs_key location
;
5089 if (verify_dir_item(root
, leaf
, di
))
5092 name_len
= btrfs_dir_name_len(leaf
, di
);
5093 if (name_len
<= sizeof(tmp_name
)) {
5094 name_ptr
= tmp_name
;
5096 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5102 read_extent_buffer(leaf
, name_ptr
,
5103 (unsigned long)(di
+ 1), name_len
);
5105 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5106 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5109 /* is this a reference to our own snapshot? If so
5112 * In contrast to old kernels, we insert the snapshot's
5113 * dir item and dir index after it has been created, so
5114 * we won't find a reference to our own snapshot. We
5115 * still keep the following code for backward
5118 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5119 location
.objectid
== root
->root_key
.objectid
) {
5123 over
= filldir(dirent
, name_ptr
, name_len
,
5124 found_key
.offset
, location
.objectid
,
5128 if (name_ptr
!= tmp_name
)
5133 di_len
= btrfs_dir_name_len(leaf
, di
) +
5134 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5136 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5142 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5145 ret
= btrfs_readdir_delayed_dir_index(filp
, dirent
, filldir
,
5151 /* Reached end of directory/root. Bump pos past the last item. */
5152 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5154 * 32-bit glibc will use getdents64, but then strtol -
5155 * so the last number we can serve is this.
5157 filp
->f_pos
= 0x7fffffff;
5163 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5164 btrfs_put_delayed_items(&ins_list
, &del_list
);
5165 btrfs_free_path(path
);
5169 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5171 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5172 struct btrfs_trans_handle
*trans
;
5174 bool nolock
= false;
5176 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5179 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5182 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5184 trans
= btrfs_join_transaction_nolock(root
);
5186 trans
= btrfs_join_transaction(root
);
5188 return PTR_ERR(trans
);
5189 ret
= btrfs_commit_transaction(trans
, root
);
5195 * This is somewhat expensive, updating the tree every time the
5196 * inode changes. But, it is most likely to find the inode in cache.
5197 * FIXME, needs more benchmarking...there are no reasons other than performance
5198 * to keep or drop this code.
5200 static int btrfs_dirty_inode(struct inode
*inode
)
5202 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5203 struct btrfs_trans_handle
*trans
;
5206 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5209 trans
= btrfs_join_transaction(root
);
5211 return PTR_ERR(trans
);
5213 ret
= btrfs_update_inode(trans
, root
, inode
);
5214 if (ret
&& ret
== -ENOSPC
) {
5215 /* whoops, lets try again with the full transaction */
5216 btrfs_end_transaction(trans
, root
);
5217 trans
= btrfs_start_transaction(root
, 1);
5219 return PTR_ERR(trans
);
5221 ret
= btrfs_update_inode(trans
, root
, inode
);
5223 btrfs_end_transaction(trans
, root
);
5224 if (BTRFS_I(inode
)->delayed_node
)
5225 btrfs_balance_delayed_items(root
);
5231 * This is a copy of file_update_time. We need this so we can return error on
5232 * ENOSPC for updating the inode in the case of file write and mmap writes.
5234 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5237 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5239 if (btrfs_root_readonly(root
))
5242 if (flags
& S_VERSION
)
5243 inode_inc_iversion(inode
);
5244 if (flags
& S_CTIME
)
5245 inode
->i_ctime
= *now
;
5246 if (flags
& S_MTIME
)
5247 inode
->i_mtime
= *now
;
5248 if (flags
& S_ATIME
)
5249 inode
->i_atime
= *now
;
5250 return btrfs_dirty_inode(inode
);
5254 * find the highest existing sequence number in a directory
5255 * and then set the in-memory index_cnt variable to reflect
5256 * free sequence numbers
5258 static int btrfs_set_inode_index_count(struct inode
*inode
)
5260 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5261 struct btrfs_key key
, found_key
;
5262 struct btrfs_path
*path
;
5263 struct extent_buffer
*leaf
;
5266 key
.objectid
= btrfs_ino(inode
);
5267 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
5268 key
.offset
= (u64
)-1;
5270 path
= btrfs_alloc_path();
5274 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5277 /* FIXME: we should be able to handle this */
5283 * MAGIC NUMBER EXPLANATION:
5284 * since we search a directory based on f_pos we have to start at 2
5285 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5286 * else has to start at 2
5288 if (path
->slots
[0] == 0) {
5289 BTRFS_I(inode
)->index_cnt
= 2;
5295 leaf
= path
->nodes
[0];
5296 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5298 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5299 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
5300 BTRFS_I(inode
)->index_cnt
= 2;
5304 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5306 btrfs_free_path(path
);
5311 * helper to find a free sequence number in a given directory. This current
5312 * code is very simple, later versions will do smarter things in the btree
5314 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5318 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5319 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5321 ret
= btrfs_set_inode_index_count(dir
);
5327 *index
= BTRFS_I(dir
)->index_cnt
;
5328 BTRFS_I(dir
)->index_cnt
++;
5333 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5334 struct btrfs_root
*root
,
5336 const char *name
, int name_len
,
5337 u64 ref_objectid
, u64 objectid
,
5338 umode_t mode
, u64
*index
)
5340 struct inode
*inode
;
5341 struct btrfs_inode_item
*inode_item
;
5342 struct btrfs_key
*location
;
5343 struct btrfs_path
*path
;
5344 struct btrfs_inode_ref
*ref
;
5345 struct btrfs_key key
[2];
5351 path
= btrfs_alloc_path();
5353 return ERR_PTR(-ENOMEM
);
5355 inode
= new_inode(root
->fs_info
->sb
);
5357 btrfs_free_path(path
);
5358 return ERR_PTR(-ENOMEM
);
5362 * we have to initialize this early, so we can reclaim the inode
5363 * number if we fail afterwards in this function.
5365 inode
->i_ino
= objectid
;
5368 trace_btrfs_inode_request(dir
);
5370 ret
= btrfs_set_inode_index(dir
, index
);
5372 btrfs_free_path(path
);
5374 return ERR_PTR(ret
);
5378 * index_cnt is ignored for everything but a dir,
5379 * btrfs_get_inode_index_count has an explanation for the magic
5382 BTRFS_I(inode
)->index_cnt
= 2;
5383 BTRFS_I(inode
)->root
= root
;
5384 BTRFS_I(inode
)->generation
= trans
->transid
;
5385 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5388 * We could have gotten an inode number from somebody who was fsynced
5389 * and then removed in this same transaction, so let's just set full
5390 * sync since it will be a full sync anyway and this will blow away the
5391 * old info in the log.
5393 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5400 key
[0].objectid
= objectid
;
5401 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
5405 * Start new inodes with an inode_ref. This is slightly more
5406 * efficient for small numbers of hard links since they will
5407 * be packed into one item. Extended refs will kick in if we
5408 * add more hard links than can fit in the ref item.
5410 key
[1].objectid
= objectid
;
5411 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
5412 key
[1].offset
= ref_objectid
;
5414 sizes
[0] = sizeof(struct btrfs_inode_item
);
5415 sizes
[1] = name_len
+ sizeof(*ref
);
5417 path
->leave_spinning
= 1;
5418 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
5422 inode_init_owner(inode
, dir
, mode
);
5423 inode_set_bytes(inode
, 0);
5424 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5425 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5426 struct btrfs_inode_item
);
5427 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5428 sizeof(*inode_item
));
5429 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5431 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5432 struct btrfs_inode_ref
);
5433 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5434 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5435 ptr
= (unsigned long)(ref
+ 1);
5436 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5438 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5439 btrfs_free_path(path
);
5441 location
= &BTRFS_I(inode
)->location
;
5442 location
->objectid
= objectid
;
5443 location
->offset
= 0;
5444 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
5446 btrfs_inherit_iflags(inode
, dir
);
5448 if (S_ISREG(mode
)) {
5449 if (btrfs_test_opt(root
, NODATASUM
))
5450 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5451 if (btrfs_test_opt(root
, NODATACOW
))
5452 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5453 BTRFS_INODE_NODATASUM
;
5456 insert_inode_hash(inode
);
5457 inode_tree_add(inode
);
5459 trace_btrfs_inode_new(inode
);
5460 btrfs_set_inode_last_trans(trans
, inode
);
5462 btrfs_update_root_times(trans
, root
);
5467 BTRFS_I(dir
)->index_cnt
--;
5468 btrfs_free_path(path
);
5470 return ERR_PTR(ret
);
5473 static inline u8
btrfs_inode_type(struct inode
*inode
)
5475 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5479 * utility function to add 'inode' into 'parent_inode' with
5480 * a give name and a given sequence number.
5481 * if 'add_backref' is true, also insert a backref from the
5482 * inode to the parent directory.
5484 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5485 struct inode
*parent_inode
, struct inode
*inode
,
5486 const char *name
, int name_len
, int add_backref
, u64 index
)
5489 struct btrfs_key key
;
5490 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
5491 u64 ino
= btrfs_ino(inode
);
5492 u64 parent_ino
= btrfs_ino(parent_inode
);
5494 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5495 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
5498 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
5502 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5503 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
5504 key
.objectid
, root
->root_key
.objectid
,
5505 parent_ino
, index
, name
, name_len
);
5506 } else if (add_backref
) {
5507 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
5511 /* Nothing to clean up yet */
5515 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
5517 btrfs_inode_type(inode
), index
);
5518 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
5521 btrfs_abort_transaction(trans
, root
, ret
);
5525 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5527 inode_inc_iversion(parent_inode
);
5528 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5529 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5531 btrfs_abort_transaction(trans
, root
, ret
);
5535 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5538 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5539 key
.objectid
, root
->root_key
.objectid
,
5540 parent_ino
, &local_index
, name
, name_len
);
5542 } else if (add_backref
) {
5546 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5547 ino
, parent_ino
, &local_index
);
5552 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5553 struct inode
*dir
, struct dentry
*dentry
,
5554 struct inode
*inode
, int backref
, u64 index
)
5556 int err
= btrfs_add_link(trans
, dir
, inode
,
5557 dentry
->d_name
.name
, dentry
->d_name
.len
,
5564 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5565 umode_t mode
, dev_t rdev
)
5567 struct btrfs_trans_handle
*trans
;
5568 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5569 struct inode
*inode
= NULL
;
5575 if (!new_valid_dev(rdev
))
5579 * 2 for inode item and ref
5581 * 1 for xattr if selinux is on
5583 trans
= btrfs_start_transaction(root
, 5);
5585 return PTR_ERR(trans
);
5587 err
= btrfs_find_free_ino(root
, &objectid
);
5591 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5592 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5594 if (IS_ERR(inode
)) {
5595 err
= PTR_ERR(inode
);
5599 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5606 * If the active LSM wants to access the inode during
5607 * d_instantiate it needs these. Smack checks to see
5608 * if the filesystem supports xattrs by looking at the
5612 inode
->i_op
= &btrfs_special_inode_operations
;
5613 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5617 init_special_inode(inode
, inode
->i_mode
, rdev
);
5618 btrfs_update_inode(trans
, root
, inode
);
5619 d_instantiate(dentry
, inode
);
5622 btrfs_end_transaction(trans
, root
);
5623 btrfs_btree_balance_dirty(root
);
5625 inode_dec_link_count(inode
);
5631 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5632 umode_t mode
, bool excl
)
5634 struct btrfs_trans_handle
*trans
;
5635 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5636 struct inode
*inode
= NULL
;
5637 int drop_inode_on_err
= 0;
5643 * 2 for inode item and ref
5645 * 1 for xattr if selinux is on
5647 trans
= btrfs_start_transaction(root
, 5);
5649 return PTR_ERR(trans
);
5651 err
= btrfs_find_free_ino(root
, &objectid
);
5655 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5656 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5658 if (IS_ERR(inode
)) {
5659 err
= PTR_ERR(inode
);
5662 drop_inode_on_err
= 1;
5664 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5668 err
= btrfs_update_inode(trans
, root
, inode
);
5673 * If the active LSM wants to access the inode during
5674 * d_instantiate it needs these. Smack checks to see
5675 * if the filesystem supports xattrs by looking at the
5678 inode
->i_fop
= &btrfs_file_operations
;
5679 inode
->i_op
= &btrfs_file_inode_operations
;
5681 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5685 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5686 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5687 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5688 d_instantiate(dentry
, inode
);
5691 btrfs_end_transaction(trans
, root
);
5692 if (err
&& drop_inode_on_err
) {
5693 inode_dec_link_count(inode
);
5696 btrfs_btree_balance_dirty(root
);
5700 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5701 struct dentry
*dentry
)
5703 struct btrfs_trans_handle
*trans
;
5704 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5705 struct inode
*inode
= old_dentry
->d_inode
;
5710 /* do not allow sys_link's with other subvols of the same device */
5711 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5714 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5717 err
= btrfs_set_inode_index(dir
, &index
);
5722 * 2 items for inode and inode ref
5723 * 2 items for dir items
5724 * 1 item for parent inode
5726 trans
= btrfs_start_transaction(root
, 5);
5727 if (IS_ERR(trans
)) {
5728 err
= PTR_ERR(trans
);
5732 btrfs_inc_nlink(inode
);
5733 inode_inc_iversion(inode
);
5734 inode
->i_ctime
= CURRENT_TIME
;
5736 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5738 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5743 struct dentry
*parent
= dentry
->d_parent
;
5744 err
= btrfs_update_inode(trans
, root
, inode
);
5747 d_instantiate(dentry
, inode
);
5748 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5751 btrfs_end_transaction(trans
, root
);
5754 inode_dec_link_count(inode
);
5757 btrfs_btree_balance_dirty(root
);
5761 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5763 struct inode
*inode
= NULL
;
5764 struct btrfs_trans_handle
*trans
;
5765 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5767 int drop_on_err
= 0;
5772 * 2 items for inode and ref
5773 * 2 items for dir items
5774 * 1 for xattr if selinux is on
5776 trans
= btrfs_start_transaction(root
, 5);
5778 return PTR_ERR(trans
);
5780 err
= btrfs_find_free_ino(root
, &objectid
);
5784 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5785 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5786 S_IFDIR
| mode
, &index
);
5787 if (IS_ERR(inode
)) {
5788 err
= PTR_ERR(inode
);
5794 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5798 inode
->i_op
= &btrfs_dir_inode_operations
;
5799 inode
->i_fop
= &btrfs_dir_file_operations
;
5801 btrfs_i_size_write(inode
, 0);
5802 err
= btrfs_update_inode(trans
, root
, inode
);
5806 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5807 dentry
->d_name
.len
, 0, index
);
5811 d_instantiate(dentry
, inode
);
5815 btrfs_end_transaction(trans
, root
);
5818 btrfs_btree_balance_dirty(root
);
5822 /* helper for btfs_get_extent. Given an existing extent in the tree,
5823 * and an extent that you want to insert, deal with overlap and insert
5824 * the new extent into the tree.
5826 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5827 struct extent_map
*existing
,
5828 struct extent_map
*em
,
5829 u64 map_start
, u64 map_len
)
5833 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5834 start_diff
= map_start
- em
->start
;
5835 em
->start
= map_start
;
5837 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5838 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5839 em
->block_start
+= start_diff
;
5840 em
->block_len
-= start_diff
;
5842 return add_extent_mapping(em_tree
, em
, 0);
5845 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5846 struct inode
*inode
, struct page
*page
,
5847 size_t pg_offset
, u64 extent_offset
,
5848 struct btrfs_file_extent_item
*item
)
5851 struct extent_buffer
*leaf
= path
->nodes
[0];
5854 unsigned long inline_size
;
5858 WARN_ON(pg_offset
!= 0);
5859 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5860 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
5861 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
5862 btrfs_item_nr(leaf
, path
->slots
[0]));
5863 tmp
= kmalloc(inline_size
, GFP_NOFS
);
5866 ptr
= btrfs_file_extent_inline_start(item
);
5868 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
5870 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
5871 ret
= btrfs_decompress(compress_type
, tmp
, page
,
5872 extent_offset
, inline_size
, max_size
);
5874 char *kaddr
= kmap_atomic(page
);
5875 unsigned long copy_size
= min_t(u64
,
5876 PAGE_CACHE_SIZE
- pg_offset
,
5877 max_size
- extent_offset
);
5878 memset(kaddr
+ pg_offset
, 0, copy_size
);
5879 kunmap_atomic(kaddr
);
5886 * a bit scary, this does extent mapping from logical file offset to the disk.
5887 * the ugly parts come from merging extents from the disk with the in-ram
5888 * representation. This gets more complex because of the data=ordered code,
5889 * where the in-ram extents might be locked pending data=ordered completion.
5891 * This also copies inline extents directly into the page.
5894 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
5895 size_t pg_offset
, u64 start
, u64 len
,
5901 u64 extent_start
= 0;
5903 u64 objectid
= btrfs_ino(inode
);
5905 struct btrfs_path
*path
= NULL
;
5906 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5907 struct btrfs_file_extent_item
*item
;
5908 struct extent_buffer
*leaf
;
5909 struct btrfs_key found_key
;
5910 struct extent_map
*em
= NULL
;
5911 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
5912 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
5913 struct btrfs_trans_handle
*trans
= NULL
;
5917 read_lock(&em_tree
->lock
);
5918 em
= lookup_extent_mapping(em_tree
, start
, len
);
5920 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5921 read_unlock(&em_tree
->lock
);
5924 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
5925 free_extent_map(em
);
5926 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
5927 free_extent_map(em
);
5931 em
= alloc_extent_map();
5936 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
5937 em
->start
= EXTENT_MAP_HOLE
;
5938 em
->orig_start
= EXTENT_MAP_HOLE
;
5940 em
->block_len
= (u64
)-1;
5943 path
= btrfs_alloc_path();
5949 * Chances are we'll be called again, so go ahead and do
5955 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
5956 objectid
, start
, trans
!= NULL
);
5963 if (path
->slots
[0] == 0)
5968 leaf
= path
->nodes
[0];
5969 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
5970 struct btrfs_file_extent_item
);
5971 /* are we inside the extent that was found? */
5972 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5973 found_type
= btrfs_key_type(&found_key
);
5974 if (found_key
.objectid
!= objectid
||
5975 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
5979 found_type
= btrfs_file_extent_type(leaf
, item
);
5980 extent_start
= found_key
.offset
;
5981 compress_type
= btrfs_file_extent_compression(leaf
, item
);
5982 if (found_type
== BTRFS_FILE_EXTENT_REG
||
5983 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
5984 extent_end
= extent_start
+
5985 btrfs_file_extent_num_bytes(leaf
, item
);
5986 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
5988 size
= btrfs_file_extent_inline_len(leaf
, item
);
5989 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
5992 if (start
>= extent_end
) {
5994 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
5995 ret
= btrfs_next_leaf(root
, path
);
6002 leaf
= path
->nodes
[0];
6004 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6005 if (found_key
.objectid
!= objectid
||
6006 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6008 if (start
+ len
<= found_key
.offset
)
6011 em
->orig_start
= start
;
6012 em
->len
= found_key
.offset
- start
;
6016 em
->ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, item
);
6017 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6018 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6019 em
->start
= extent_start
;
6020 em
->len
= extent_end
- extent_start
;
6021 em
->orig_start
= extent_start
-
6022 btrfs_file_extent_offset(leaf
, item
);
6023 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
6025 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
6027 em
->block_start
= EXTENT_MAP_HOLE
;
6030 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
6031 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6032 em
->compress_type
= compress_type
;
6033 em
->block_start
= bytenr
;
6034 em
->block_len
= em
->orig_block_len
;
6036 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
6037 em
->block_start
= bytenr
;
6038 em
->block_len
= em
->len
;
6039 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
6040 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6043 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6047 size_t extent_offset
;
6050 em
->block_start
= EXTENT_MAP_INLINE
;
6051 if (!page
|| create
) {
6052 em
->start
= extent_start
;
6053 em
->len
= extent_end
- extent_start
;
6057 size
= btrfs_file_extent_inline_len(leaf
, item
);
6058 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6059 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6060 size
- extent_offset
);
6061 em
->start
= extent_start
+ extent_offset
;
6062 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6063 em
->orig_block_len
= em
->len
;
6064 em
->orig_start
= em
->start
;
6065 if (compress_type
) {
6066 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6067 em
->compress_type
= compress_type
;
6069 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6070 if (create
== 0 && !PageUptodate(page
)) {
6071 if (btrfs_file_extent_compression(leaf
, item
) !=
6072 BTRFS_COMPRESS_NONE
) {
6073 ret
= uncompress_inline(path
, inode
, page
,
6075 extent_offset
, item
);
6076 BUG_ON(ret
); /* -ENOMEM */
6079 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6081 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6082 memset(map
+ pg_offset
+ copy_size
, 0,
6083 PAGE_CACHE_SIZE
- pg_offset
-
6088 flush_dcache_page(page
);
6089 } else if (create
&& PageUptodate(page
)) {
6093 free_extent_map(em
);
6096 btrfs_release_path(path
);
6097 trans
= btrfs_join_transaction(root
);
6100 return ERR_CAST(trans
);
6104 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6107 btrfs_mark_buffer_dirty(leaf
);
6109 set_extent_uptodate(io_tree
, em
->start
,
6110 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6113 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
6117 em
->orig_start
= start
;
6120 em
->block_start
= EXTENT_MAP_HOLE
;
6121 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6123 btrfs_release_path(path
);
6124 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6125 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6126 (unsigned long long)em
->start
,
6127 (unsigned long long)em
->len
,
6128 (unsigned long long)start
,
6129 (unsigned long long)len
);
6135 write_lock(&em_tree
->lock
);
6136 ret
= add_extent_mapping(em_tree
, em
, 0);
6137 /* it is possible that someone inserted the extent into the tree
6138 * while we had the lock dropped. It is also possible that
6139 * an overlapping map exists in the tree
6141 if (ret
== -EEXIST
) {
6142 struct extent_map
*existing
;
6146 existing
= lookup_extent_mapping(em_tree
, start
, len
);
6147 if (existing
&& (existing
->start
> start
||
6148 existing
->start
+ existing
->len
<= start
)) {
6149 free_extent_map(existing
);
6153 existing
= lookup_extent_mapping(em_tree
, em
->start
,
6156 err
= merge_extent_mapping(em_tree
, existing
,
6159 free_extent_map(existing
);
6161 free_extent_map(em
);
6166 free_extent_map(em
);
6170 free_extent_map(em
);
6175 write_unlock(&em_tree
->lock
);
6179 trace_btrfs_get_extent(root
, em
);
6182 btrfs_free_path(path
);
6184 ret
= btrfs_end_transaction(trans
, root
);
6189 free_extent_map(em
);
6190 return ERR_PTR(err
);
6192 BUG_ON(!em
); /* Error is always set */
6196 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6197 size_t pg_offset
, u64 start
, u64 len
,
6200 struct extent_map
*em
;
6201 struct extent_map
*hole_em
= NULL
;
6202 u64 range_start
= start
;
6208 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6215 * - a pre-alloc extent,
6216 * there might actually be delalloc bytes behind it.
6218 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6219 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6225 /* check to see if we've wrapped (len == -1 or similar) */
6234 /* ok, we didn't find anything, lets look for delalloc */
6235 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6236 end
, len
, EXTENT_DELALLOC
, 1);
6237 found_end
= range_start
+ found
;
6238 if (found_end
< range_start
)
6239 found_end
= (u64
)-1;
6242 * we didn't find anything useful, return
6243 * the original results from get_extent()
6245 if (range_start
> end
|| found_end
<= start
) {
6251 /* adjust the range_start to make sure it doesn't
6252 * go backwards from the start they passed in
6254 range_start
= max(start
,range_start
);
6255 found
= found_end
- range_start
;
6258 u64 hole_start
= start
;
6261 em
= alloc_extent_map();
6267 * when btrfs_get_extent can't find anything it
6268 * returns one huge hole
6270 * make sure what it found really fits our range, and
6271 * adjust to make sure it is based on the start from
6275 u64 calc_end
= extent_map_end(hole_em
);
6277 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6278 free_extent_map(hole_em
);
6281 hole_start
= max(hole_em
->start
, start
);
6282 hole_len
= calc_end
- hole_start
;
6286 if (hole_em
&& range_start
> hole_start
) {
6287 /* our hole starts before our delalloc, so we
6288 * have to return just the parts of the hole
6289 * that go until the delalloc starts
6291 em
->len
= min(hole_len
,
6292 range_start
- hole_start
);
6293 em
->start
= hole_start
;
6294 em
->orig_start
= hole_start
;
6296 * don't adjust block start at all,
6297 * it is fixed at EXTENT_MAP_HOLE
6299 em
->block_start
= hole_em
->block_start
;
6300 em
->block_len
= hole_len
;
6301 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6302 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6304 em
->start
= range_start
;
6306 em
->orig_start
= range_start
;
6307 em
->block_start
= EXTENT_MAP_DELALLOC
;
6308 em
->block_len
= found
;
6310 } else if (hole_em
) {
6315 free_extent_map(hole_em
);
6317 free_extent_map(em
);
6318 return ERR_PTR(err
);
6323 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6326 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6327 struct btrfs_trans_handle
*trans
;
6328 struct extent_map
*em
;
6329 struct btrfs_key ins
;
6333 trans
= btrfs_join_transaction(root
);
6335 return ERR_CAST(trans
);
6337 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
6339 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6340 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
6341 alloc_hint
, &ins
, 1);
6347 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6348 ins
.offset
, ins
.offset
, ins
.offset
, 0);
6352 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6353 ins
.offset
, ins
.offset
, 0);
6355 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6359 btrfs_end_transaction(trans
, root
);
6364 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6365 * block must be cow'd
6367 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
6368 struct inode
*inode
, u64 offset
, u64
*len
,
6369 u64
*orig_start
, u64
*orig_block_len
,
6372 struct btrfs_path
*path
;
6374 struct extent_buffer
*leaf
;
6375 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6376 struct btrfs_file_extent_item
*fi
;
6377 struct btrfs_key key
;
6385 path
= btrfs_alloc_path();
6389 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
6394 slot
= path
->slots
[0];
6397 /* can't find the item, must cow */
6404 leaf
= path
->nodes
[0];
6405 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6406 if (key
.objectid
!= btrfs_ino(inode
) ||
6407 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6408 /* not our file or wrong item type, must cow */
6412 if (key
.offset
> offset
) {
6413 /* Wrong offset, must cow */
6417 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6418 found_type
= btrfs_file_extent_type(leaf
, fi
);
6419 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6420 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6421 /* not a regular extent, must cow */
6424 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6425 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6427 *orig_start
= key
.offset
- backref_offset
;
6428 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
6429 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
6431 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6432 if (extent_end
< offset
+ *len
) {
6433 /* extent doesn't include our full range, must cow */
6437 if (btrfs_extent_readonly(root
, disk_bytenr
))
6441 * look for other files referencing this extent, if we
6442 * find any we must cow
6444 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
6445 key
.offset
- backref_offset
, disk_bytenr
))
6449 * adjust disk_bytenr and num_bytes to cover just the bytes
6450 * in this extent we are about to write. If there
6451 * are any csums in that range we have to cow in order
6452 * to keep the csums correct
6454 disk_bytenr
+= backref_offset
;
6455 disk_bytenr
+= offset
- key
.offset
;
6456 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
6457 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
6460 * all of the above have passed, it is safe to overwrite this extent
6466 btrfs_free_path(path
);
6470 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
6471 struct extent_state
**cached_state
, int writing
)
6473 struct btrfs_ordered_extent
*ordered
;
6477 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6480 * We're concerned with the entire range that we're going to be
6481 * doing DIO to, so we need to make sure theres no ordered
6482 * extents in this range.
6484 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6485 lockend
- lockstart
+ 1);
6488 * We need to make sure there are no buffered pages in this
6489 * range either, we could have raced between the invalidate in
6490 * generic_file_direct_write and locking the extent. The
6491 * invalidate needs to happen so that reads after a write do not
6494 if (!ordered
&& (!writing
||
6495 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
6496 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
6500 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6501 cached_state
, GFP_NOFS
);
6504 btrfs_start_ordered_extent(inode
, ordered
, 1);
6505 btrfs_put_ordered_extent(ordered
);
6507 /* Screw you mmap */
6508 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
6515 * If we found a page that couldn't be invalidated just
6516 * fall back to buffered.
6518 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6519 lockstart
>> PAGE_CACHE_SHIFT
,
6520 lockend
>> PAGE_CACHE_SHIFT
);
6531 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
6532 u64 len
, u64 orig_start
,
6533 u64 block_start
, u64 block_len
,
6534 u64 orig_block_len
, u64 ram_bytes
,
6537 struct extent_map_tree
*em_tree
;
6538 struct extent_map
*em
;
6539 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6542 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6543 em
= alloc_extent_map();
6545 return ERR_PTR(-ENOMEM
);
6548 em
->orig_start
= orig_start
;
6549 em
->mod_start
= start
;
6552 em
->block_len
= block_len
;
6553 em
->block_start
= block_start
;
6554 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6555 em
->orig_block_len
= orig_block_len
;
6556 em
->ram_bytes
= ram_bytes
;
6557 em
->generation
= -1;
6558 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6559 if (type
== BTRFS_ORDERED_PREALLOC
)
6560 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6563 btrfs_drop_extent_cache(inode
, em
->start
,
6564 em
->start
+ em
->len
- 1, 0);
6565 write_lock(&em_tree
->lock
);
6566 ret
= add_extent_mapping(em_tree
, em
, 1);
6567 write_unlock(&em_tree
->lock
);
6568 } while (ret
== -EEXIST
);
6571 free_extent_map(em
);
6572 return ERR_PTR(ret
);
6579 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6580 struct buffer_head
*bh_result
, int create
)
6582 struct extent_map
*em
;
6583 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6584 struct extent_state
*cached_state
= NULL
;
6585 u64 start
= iblock
<< inode
->i_blkbits
;
6586 u64 lockstart
, lockend
;
6587 u64 len
= bh_result
->b_size
;
6588 struct btrfs_trans_handle
*trans
;
6589 int unlock_bits
= EXTENT_LOCKED
;
6593 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6595 len
= min_t(u64
, len
, root
->sectorsize
);
6598 lockend
= start
+ len
- 1;
6601 * If this errors out it's because we couldn't invalidate pagecache for
6602 * this range and we need to fallback to buffered.
6604 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6607 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6614 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6615 * io. INLINE is special, and we could probably kludge it in here, but
6616 * it's still buffered so for safety lets just fall back to the generic
6619 * For COMPRESSED we _have_ to read the entire extent in so we can
6620 * decompress it, so there will be buffering required no matter what we
6621 * do, so go ahead and fallback to buffered.
6623 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6624 * to buffered IO. Don't blame me, this is the price we pay for using
6627 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6628 em
->block_start
== EXTENT_MAP_INLINE
) {
6629 free_extent_map(em
);
6634 /* Just a good old fashioned hole, return */
6635 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6636 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6637 free_extent_map(em
);
6642 * We don't allocate a new extent in the following cases
6644 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6646 * 2) The extent is marked as PREALLOC. We're good to go here and can
6647 * just use the extent.
6651 len
= min(len
, em
->len
- (start
- em
->start
));
6652 lockstart
= start
+ len
;
6656 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6657 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6658 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6661 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
6663 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6664 type
= BTRFS_ORDERED_PREALLOC
;
6666 type
= BTRFS_ORDERED_NOCOW
;
6667 len
= min(len
, em
->len
- (start
- em
->start
));
6668 block_start
= em
->block_start
+ (start
- em
->start
);
6671 * we're not going to log anything, but we do need
6672 * to make sure the current transaction stays open
6673 * while we look for nocow cross refs
6675 trans
= btrfs_join_transaction(root
);
6679 if (can_nocow_odirect(trans
, inode
, start
, &len
, &orig_start
,
6680 &orig_block_len
, &ram_bytes
) == 1) {
6681 if (type
== BTRFS_ORDERED_PREALLOC
) {
6682 free_extent_map(em
);
6683 em
= create_pinned_em(inode
, start
, len
,
6689 btrfs_end_transaction(trans
, root
);
6694 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6695 block_start
, len
, len
, type
);
6696 btrfs_end_transaction(trans
, root
);
6698 free_extent_map(em
);
6703 btrfs_end_transaction(trans
, root
);
6707 * this will cow the extent, reset the len in case we changed
6710 len
= bh_result
->b_size
;
6711 free_extent_map(em
);
6712 em
= btrfs_new_extent_direct(inode
, start
, len
);
6717 len
= min(len
, em
->len
- (start
- em
->start
));
6719 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6721 bh_result
->b_size
= len
;
6722 bh_result
->b_bdev
= em
->bdev
;
6723 set_buffer_mapped(bh_result
);
6725 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6726 set_buffer_new(bh_result
);
6729 * Need to update the i_size under the extent lock so buffered
6730 * readers will get the updated i_size when we unlock.
6732 if (start
+ len
> i_size_read(inode
))
6733 i_size_write(inode
, start
+ len
);
6735 spin_lock(&BTRFS_I(inode
)->lock
);
6736 BTRFS_I(inode
)->outstanding_extents
++;
6737 spin_unlock(&BTRFS_I(inode
)->lock
);
6739 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6740 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
6741 &cached_state
, GFP_NOFS
);
6746 * In the case of write we need to clear and unlock the entire range,
6747 * in the case of read we need to unlock only the end area that we
6748 * aren't using if there is any left over space.
6750 if (lockstart
< lockend
) {
6751 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6752 lockend
, unlock_bits
, 1, 0,
6753 &cached_state
, GFP_NOFS
);
6755 free_extent_state(cached_state
);
6758 free_extent_map(em
);
6763 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6764 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6768 struct btrfs_dio_private
{
6769 struct inode
*inode
;
6775 /* number of bios pending for this dio */
6776 atomic_t pending_bios
;
6781 /* orig_bio is our btrfs_io_bio */
6782 struct bio
*orig_bio
;
6784 /* dio_bio came from fs/direct-io.c */
6785 struct bio
*dio_bio
;
6788 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6790 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6791 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6792 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6793 struct inode
*inode
= dip
->inode
;
6794 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6795 struct bio
*dio_bio
;
6798 start
= dip
->logical_offset
;
6800 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6801 struct page
*page
= bvec
->bv_page
;
6804 u64
private = ~(u32
)0;
6805 unsigned long flags
;
6807 if (get_state_private(&BTRFS_I(inode
)->io_tree
,
6810 local_irq_save(flags
);
6811 kaddr
= kmap_atomic(page
);
6812 csum
= btrfs_csum_data(kaddr
+ bvec
->bv_offset
,
6813 csum
, bvec
->bv_len
);
6814 btrfs_csum_final(csum
, (char *)&csum
);
6815 kunmap_atomic(kaddr
);
6816 local_irq_restore(flags
);
6818 flush_dcache_page(bvec
->bv_page
);
6819 if (csum
!= private) {
6821 btrfs_err(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %u",
6822 (unsigned long long)btrfs_ino(inode
),
6823 (unsigned long long)start
,
6824 csum
, (unsigned)private);
6829 start
+= bvec
->bv_len
;
6831 } while (bvec
<= bvec_end
);
6833 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6834 dip
->logical_offset
+ dip
->bytes
- 1);
6835 dio_bio
= dip
->dio_bio
;
6839 /* If we had a csum failure make sure to clear the uptodate flag */
6841 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
6842 dio_end_io(dio_bio
, err
);
6846 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6848 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6849 struct inode
*inode
= dip
->inode
;
6850 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6851 struct btrfs_ordered_extent
*ordered
= NULL
;
6852 u64 ordered_offset
= dip
->logical_offset
;
6853 u64 ordered_bytes
= dip
->bytes
;
6854 struct bio
*dio_bio
;
6860 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
6862 ordered_bytes
, !err
);
6866 ordered
->work
.func
= finish_ordered_fn
;
6867 ordered
->work
.flags
= 0;
6868 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
6872 * our bio might span multiple ordered extents. If we haven't
6873 * completed the accounting for the whole dio, go back and try again
6875 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
6876 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
6882 dio_bio
= dip
->dio_bio
;
6886 /* If we had an error make sure to clear the uptodate flag */
6888 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
6889 dio_end_io(dio_bio
, err
);
6893 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
6894 struct bio
*bio
, int mirror_num
,
6895 unsigned long bio_flags
, u64 offset
)
6898 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6899 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
6900 BUG_ON(ret
); /* -ENOMEM */
6904 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
6906 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6909 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
6910 "sector %#Lx len %u err no %d\n",
6911 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
6912 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
6916 * before atomic variable goto zero, we must make sure
6917 * dip->errors is perceived to be set.
6919 smp_mb__before_atomic_dec();
6922 /* if there are more bios still pending for this dio, just exit */
6923 if (!atomic_dec_and_test(&dip
->pending_bios
))
6927 bio_io_error(dip
->orig_bio
);
6929 set_bit(BIO_UPTODATE
, &dip
->dio_bio
->bi_flags
);
6930 bio_endio(dip
->orig_bio
, 0);
6936 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
6937 u64 first_sector
, gfp_t gfp_flags
)
6939 int nr_vecs
= bio_get_nr_vecs(bdev
);
6940 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
6943 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
6944 int rw
, u64 file_offset
, int skip_sum
,
6947 int write
= rw
& REQ_WRITE
;
6948 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6952 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
6957 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
6965 if (write
&& async_submit
) {
6966 ret
= btrfs_wq_submit_bio(root
->fs_info
,
6967 inode
, rw
, bio
, 0, 0,
6969 __btrfs_submit_bio_start_direct_io
,
6970 __btrfs_submit_bio_done
);
6974 * If we aren't doing async submit, calculate the csum of the
6977 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
6980 } else if (!skip_sum
) {
6981 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
, file_offset
);
6987 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
6993 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
6996 struct inode
*inode
= dip
->inode
;
6997 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6999 struct bio
*orig_bio
= dip
->orig_bio
;
7000 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
7001 u64 start_sector
= orig_bio
->bi_sector
;
7002 u64 file_offset
= dip
->logical_offset
;
7007 int async_submit
= 0;
7009 map_length
= orig_bio
->bi_size
;
7010 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
7011 &map_length
, NULL
, 0);
7016 if (map_length
>= orig_bio
->bi_size
) {
7021 /* async crcs make it difficult to collect full stripe writes. */
7022 if (btrfs_get_alloc_profile(root
, 1) &
7023 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
7028 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7031 bio
->bi_private
= dip
;
7032 bio
->bi_end_io
= btrfs_end_dio_bio
;
7033 atomic_inc(&dip
->pending_bios
);
7035 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7036 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
7037 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7038 bvec
->bv_offset
) < bvec
->bv_len
)) {
7040 * inc the count before we submit the bio so
7041 * we know the end IO handler won't happen before
7042 * we inc the count. Otherwise, the dip might get freed
7043 * before we're done setting it up
7045 atomic_inc(&dip
->pending_bios
);
7046 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7047 file_offset
, skip_sum
,
7051 atomic_dec(&dip
->pending_bios
);
7055 start_sector
+= submit_len
>> 9;
7056 file_offset
+= submit_len
;
7061 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7062 start_sector
, GFP_NOFS
);
7065 bio
->bi_private
= dip
;
7066 bio
->bi_end_io
= btrfs_end_dio_bio
;
7068 map_length
= orig_bio
->bi_size
;
7069 ret
= btrfs_map_block(root
->fs_info
, rw
,
7071 &map_length
, NULL
, 0);
7077 submit_len
+= bvec
->bv_len
;
7084 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7093 * before atomic variable goto zero, we must
7094 * make sure dip->errors is perceived to be set.
7096 smp_mb__before_atomic_dec();
7097 if (atomic_dec_and_test(&dip
->pending_bios
))
7098 bio_io_error(dip
->orig_bio
);
7100 /* bio_end_io() will handle error, so we needn't return it */
7104 static void btrfs_submit_direct(int rw
, struct bio
*dio_bio
,
7105 struct inode
*inode
, loff_t file_offset
)
7107 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7108 struct btrfs_dio_private
*dip
;
7109 struct bio_vec
*bvec
= dio_bio
->bi_io_vec
;
7112 int write
= rw
& REQ_WRITE
;
7115 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7117 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
7124 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
7130 dip
->private = dio_bio
->bi_private
;
7131 io_bio
->bi_private
= dio_bio
->bi_private
;
7133 dip
->logical_offset
= file_offset
;
7137 dip
->bytes
+= bvec
->bv_len
;
7139 } while (bvec
<= (dio_bio
->bi_io_vec
+ dio_bio
->bi_vcnt
- 1));
7141 dip
->disk_bytenr
= (u64
)dio_bio
->bi_sector
<< 9;
7142 io_bio
->bi_private
= dip
;
7144 dip
->orig_bio
= io_bio
;
7145 dip
->dio_bio
= dio_bio
;
7146 atomic_set(&dip
->pending_bios
, 0);
7149 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
7151 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
7153 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
7162 * If this is a write, we need to clean up the reserved space and kill
7163 * the ordered extent.
7166 struct btrfs_ordered_extent
*ordered
;
7167 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
7168 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
7169 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
7170 btrfs_free_reserved_extent(root
, ordered
->start
,
7172 btrfs_put_ordered_extent(ordered
);
7173 btrfs_put_ordered_extent(ordered
);
7175 bio_endio(dio_bio
, ret
);
7178 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
7179 const struct iovec
*iov
, loff_t offset
,
7180 unsigned long nr_segs
)
7186 unsigned blocksize_mask
= root
->sectorsize
- 1;
7187 ssize_t retval
= -EINVAL
;
7188 loff_t end
= offset
;
7190 if (offset
& blocksize_mask
)
7193 /* Check the memory alignment. Blocks cannot straddle pages */
7194 for (seg
= 0; seg
< nr_segs
; seg
++) {
7195 addr
= (unsigned long)iov
[seg
].iov_base
;
7196 size
= iov
[seg
].iov_len
;
7198 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
7201 /* If this is a write we don't need to check anymore */
7206 * Check to make sure we don't have duplicate iov_base's in this
7207 * iovec, if so return EINVAL, otherwise we'll get csum errors
7208 * when reading back.
7210 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
7211 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
7220 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
7221 const struct iovec
*iov
, loff_t offset
,
7222 unsigned long nr_segs
)
7224 struct file
*file
= iocb
->ki_filp
;
7225 struct inode
*inode
= file
->f_mapping
->host
;
7229 bool relock
= false;
7232 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
7236 atomic_inc(&inode
->i_dio_count
);
7237 smp_mb__after_atomic_inc();
7240 count
= iov_length(iov
, nr_segs
);
7242 * If the write DIO is beyond the EOF, we need update
7243 * the isize, but it is protected by i_mutex. So we can
7244 * not unlock the i_mutex at this case.
7246 if (offset
+ count
<= inode
->i_size
) {
7247 mutex_unlock(&inode
->i_mutex
);
7250 ret
= btrfs_delalloc_reserve_space(inode
, count
);
7253 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7254 &BTRFS_I(inode
)->runtime_flags
))) {
7255 inode_dio_done(inode
);
7256 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7260 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
7261 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
7262 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
7263 btrfs_submit_direct
, flags
);
7265 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
7266 btrfs_delalloc_release_space(inode
, count
);
7267 else if (ret
>= 0 && (size_t)ret
< count
)
7268 btrfs_delalloc_release_space(inode
,
7269 count
- (size_t)ret
);
7271 btrfs_delalloc_release_metadata(inode
, 0);
7275 inode_dio_done(inode
);
7277 mutex_lock(&inode
->i_mutex
);
7282 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7284 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
7285 __u64 start
, __u64 len
)
7289 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
7293 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
7296 int btrfs_readpage(struct file
*file
, struct page
*page
)
7298 struct extent_io_tree
*tree
;
7299 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7300 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
7303 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
7305 struct extent_io_tree
*tree
;
7308 if (current
->flags
& PF_MEMALLOC
) {
7309 redirty_page_for_writepage(wbc
, page
);
7313 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7314 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
7317 static int btrfs_writepages(struct address_space
*mapping
,
7318 struct writeback_control
*wbc
)
7320 struct extent_io_tree
*tree
;
7322 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7323 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
7327 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
7328 struct list_head
*pages
, unsigned nr_pages
)
7330 struct extent_io_tree
*tree
;
7331 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7332 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
7335 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7337 struct extent_io_tree
*tree
;
7338 struct extent_map_tree
*map
;
7341 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7342 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
7343 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
7345 ClearPagePrivate(page
);
7346 set_page_private(page
, 0);
7347 page_cache_release(page
);
7352 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7354 if (PageWriteback(page
) || PageDirty(page
))
7356 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
7359 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
7361 struct inode
*inode
= page
->mapping
->host
;
7362 struct extent_io_tree
*tree
;
7363 struct btrfs_ordered_extent
*ordered
;
7364 struct extent_state
*cached_state
= NULL
;
7365 u64 page_start
= page_offset(page
);
7366 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7369 * we have the page locked, so new writeback can't start,
7370 * and the dirty bit won't be cleared while we are here.
7372 * Wait for IO on this page so that we can safely clear
7373 * the PagePrivate2 bit and do ordered accounting
7375 wait_on_page_writeback(page
);
7377 tree
= &BTRFS_I(inode
)->io_tree
;
7379 btrfs_releasepage(page
, GFP_NOFS
);
7382 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7383 ordered
= btrfs_lookup_ordered_extent(inode
, page_offset(page
));
7386 * IO on this page will never be started, so we need
7387 * to account for any ordered extents now
7389 clear_extent_bit(tree
, page_start
, page_end
,
7390 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7391 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
7392 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
7394 * whoever cleared the private bit is responsible
7395 * for the finish_ordered_io
7397 if (TestClearPagePrivate2(page
) &&
7398 btrfs_dec_test_ordered_pending(inode
, &ordered
, page_start
,
7399 PAGE_CACHE_SIZE
, 1)) {
7400 btrfs_finish_ordered_io(ordered
);
7402 btrfs_put_ordered_extent(ordered
);
7403 cached_state
= NULL
;
7404 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7406 clear_extent_bit(tree
, page_start
, page_end
,
7407 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
7408 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
7409 &cached_state
, GFP_NOFS
);
7410 __btrfs_releasepage(page
, GFP_NOFS
);
7412 ClearPageChecked(page
);
7413 if (PagePrivate(page
)) {
7414 ClearPagePrivate(page
);
7415 set_page_private(page
, 0);
7416 page_cache_release(page
);
7421 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7422 * called from a page fault handler when a page is first dirtied. Hence we must
7423 * be careful to check for EOF conditions here. We set the page up correctly
7424 * for a written page which means we get ENOSPC checking when writing into
7425 * holes and correct delalloc and unwritten extent mapping on filesystems that
7426 * support these features.
7428 * We are not allowed to take the i_mutex here so we have to play games to
7429 * protect against truncate races as the page could now be beyond EOF. Because
7430 * vmtruncate() writes the inode size before removing pages, once we have the
7431 * page lock we can determine safely if the page is beyond EOF. If it is not
7432 * beyond EOF, then the page is guaranteed safe against truncation until we
7435 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
7437 struct page
*page
= vmf
->page
;
7438 struct inode
*inode
= file_inode(vma
->vm_file
);
7439 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7440 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7441 struct btrfs_ordered_extent
*ordered
;
7442 struct extent_state
*cached_state
= NULL
;
7444 unsigned long zero_start
;
7451 sb_start_pagefault(inode
->i_sb
);
7452 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
7454 ret
= file_update_time(vma
->vm_file
);
7460 else /* -ENOSPC, -EIO, etc */
7461 ret
= VM_FAULT_SIGBUS
;
7467 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
7470 size
= i_size_read(inode
);
7471 page_start
= page_offset(page
);
7472 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7474 if ((page
->mapping
!= inode
->i_mapping
) ||
7475 (page_start
>= size
)) {
7476 /* page got truncated out from underneath us */
7479 wait_on_page_writeback(page
);
7481 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
7482 set_page_extent_mapped(page
);
7485 * we can't set the delalloc bits if there are pending ordered
7486 * extents. Drop our locks and wait for them to finish
7488 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7490 unlock_extent_cached(io_tree
, page_start
, page_end
,
7491 &cached_state
, GFP_NOFS
);
7493 btrfs_start_ordered_extent(inode
, ordered
, 1);
7494 btrfs_put_ordered_extent(ordered
);
7499 * XXX - page_mkwrite gets called every time the page is dirtied, even
7500 * if it was already dirty, so for space accounting reasons we need to
7501 * clear any delalloc bits for the range we are fixing to save. There
7502 * is probably a better way to do this, but for now keep consistent with
7503 * prepare_pages in the normal write path.
7505 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
7506 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7507 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
7508 0, 0, &cached_state
, GFP_NOFS
);
7510 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
7513 unlock_extent_cached(io_tree
, page_start
, page_end
,
7514 &cached_state
, GFP_NOFS
);
7515 ret
= VM_FAULT_SIGBUS
;
7520 /* page is wholly or partially inside EOF */
7521 if (page_start
+ PAGE_CACHE_SIZE
> size
)
7522 zero_start
= size
& ~PAGE_CACHE_MASK
;
7524 zero_start
= PAGE_CACHE_SIZE
;
7526 if (zero_start
!= PAGE_CACHE_SIZE
) {
7528 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
7529 flush_dcache_page(page
);
7532 ClearPageChecked(page
);
7533 set_page_dirty(page
);
7534 SetPageUptodate(page
);
7536 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
7537 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
7538 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
7540 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
7544 sb_end_pagefault(inode
->i_sb
);
7545 return VM_FAULT_LOCKED
;
7549 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
7551 sb_end_pagefault(inode
->i_sb
);
7555 static int btrfs_truncate(struct inode
*inode
)
7557 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7558 struct btrfs_block_rsv
*rsv
;
7561 struct btrfs_trans_handle
*trans
;
7562 u64 mask
= root
->sectorsize
- 1;
7563 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7565 ret
= btrfs_truncate_page(inode
, inode
->i_size
, 0, 0);
7569 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
7570 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
7573 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7574 * 3 things going on here
7576 * 1) We need to reserve space for our orphan item and the space to
7577 * delete our orphan item. Lord knows we don't want to have a dangling
7578 * orphan item because we didn't reserve space to remove it.
7580 * 2) We need to reserve space to update our inode.
7582 * 3) We need to have something to cache all the space that is going to
7583 * be free'd up by the truncate operation, but also have some slack
7584 * space reserved in case it uses space during the truncate (thank you
7585 * very much snapshotting).
7587 * And we need these to all be seperate. The fact is we can use alot of
7588 * space doing the truncate, and we have no earthly idea how much space
7589 * we will use, so we need the truncate reservation to be seperate so it
7590 * doesn't end up using space reserved for updating the inode or
7591 * removing the orphan item. We also need to be able to stop the
7592 * transaction and start a new one, which means we need to be able to
7593 * update the inode several times, and we have no idea of knowing how
7594 * many times that will be, so we can't just reserve 1 item for the
7595 * entirety of the opration, so that has to be done seperately as well.
7596 * Then there is the orphan item, which does indeed need to be held on
7597 * to for the whole operation, and we need nobody to touch this reserved
7598 * space except the orphan code.
7600 * So that leaves us with
7602 * 1) root->orphan_block_rsv - for the orphan deletion.
7603 * 2) rsv - for the truncate reservation, which we will steal from the
7604 * transaction reservation.
7605 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7606 * updating the inode.
7608 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7611 rsv
->size
= min_size
;
7615 * 1 for the truncate slack space
7616 * 1 for updating the inode.
7618 trans
= btrfs_start_transaction(root
, 2);
7619 if (IS_ERR(trans
)) {
7620 err
= PTR_ERR(trans
);
7624 /* Migrate the slack space for the truncate to our reserve */
7625 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7630 * setattr is responsible for setting the ordered_data_close flag,
7631 * but that is only tested during the last file release. That
7632 * could happen well after the next commit, leaving a great big
7633 * window where new writes may get lost if someone chooses to write
7634 * to this file after truncating to zero
7636 * The inode doesn't have any dirty data here, and so if we commit
7637 * this is a noop. If someone immediately starts writing to the inode
7638 * it is very likely we'll catch some of their writes in this
7639 * transaction, and the commit will find this file on the ordered
7640 * data list with good things to send down.
7642 * This is a best effort solution, there is still a window where
7643 * using truncate to replace the contents of the file will
7644 * end up with a zero length file after a crash.
7646 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7647 &BTRFS_I(inode
)->runtime_flags
))
7648 btrfs_add_ordered_operation(trans
, root
, inode
);
7651 * So if we truncate and then write and fsync we normally would just
7652 * write the extents that changed, which is a problem if we need to
7653 * first truncate that entire inode. So set this flag so we write out
7654 * all of the extents in the inode to the sync log so we're completely
7657 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7658 trans
->block_rsv
= rsv
;
7661 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7663 BTRFS_EXTENT_DATA_KEY
);
7664 if (ret
!= -ENOSPC
) {
7669 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7670 ret
= btrfs_update_inode(trans
, root
, inode
);
7676 btrfs_end_transaction(trans
, root
);
7677 btrfs_btree_balance_dirty(root
);
7679 trans
= btrfs_start_transaction(root
, 2);
7680 if (IS_ERR(trans
)) {
7681 ret
= err
= PTR_ERR(trans
);
7686 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7688 BUG_ON(ret
); /* shouldn't happen */
7689 trans
->block_rsv
= rsv
;
7692 if (ret
== 0 && inode
->i_nlink
> 0) {
7693 trans
->block_rsv
= root
->orphan_block_rsv
;
7694 ret
= btrfs_orphan_del(trans
, inode
);
7700 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7701 ret
= btrfs_update_inode(trans
, root
, inode
);
7705 ret
= btrfs_end_transaction(trans
, root
);
7706 btrfs_btree_balance_dirty(root
);
7710 btrfs_free_block_rsv(root
, rsv
);
7719 * create a new subvolume directory/inode (helper for the ioctl).
7721 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7722 struct btrfs_root
*new_root
, u64 new_dirid
)
7724 struct inode
*inode
;
7728 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7729 new_dirid
, new_dirid
,
7730 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7733 return PTR_ERR(inode
);
7734 inode
->i_op
= &btrfs_dir_inode_operations
;
7735 inode
->i_fop
= &btrfs_dir_file_operations
;
7737 set_nlink(inode
, 1);
7738 btrfs_i_size_write(inode
, 0);
7740 err
= btrfs_update_inode(trans
, new_root
, inode
);
7746 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7748 struct btrfs_inode
*ei
;
7749 struct inode
*inode
;
7751 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7758 ei
->last_sub_trans
= 0;
7759 ei
->logged_trans
= 0;
7760 ei
->delalloc_bytes
= 0;
7761 ei
->disk_i_size
= 0;
7764 ei
->index_cnt
= (u64
)-1;
7765 ei
->last_unlink_trans
= 0;
7766 ei
->last_log_commit
= 0;
7768 spin_lock_init(&ei
->lock
);
7769 ei
->outstanding_extents
= 0;
7770 ei
->reserved_extents
= 0;
7772 ei
->runtime_flags
= 0;
7773 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7775 ei
->delayed_node
= NULL
;
7777 inode
= &ei
->vfs_inode
;
7778 extent_map_tree_init(&ei
->extent_tree
);
7779 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7780 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7781 ei
->io_tree
.track_uptodate
= 1;
7782 ei
->io_failure_tree
.track_uptodate
= 1;
7783 atomic_set(&ei
->sync_writers
, 0);
7784 mutex_init(&ei
->log_mutex
);
7785 mutex_init(&ei
->delalloc_mutex
);
7786 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7787 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7788 INIT_LIST_HEAD(&ei
->ordered_operations
);
7789 RB_CLEAR_NODE(&ei
->rb_node
);
7794 static void btrfs_i_callback(struct rcu_head
*head
)
7796 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7797 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7800 void btrfs_destroy_inode(struct inode
*inode
)
7802 struct btrfs_ordered_extent
*ordered
;
7803 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7805 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7806 WARN_ON(inode
->i_data
.nrpages
);
7807 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7808 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7809 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7810 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7813 * This can happen where we create an inode, but somebody else also
7814 * created the same inode and we need to destroy the one we already
7821 * Make sure we're properly removed from the ordered operation
7825 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7826 spin_lock(&root
->fs_info
->ordered_root_lock
);
7827 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7828 spin_unlock(&root
->fs_info
->ordered_root_lock
);
7831 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7832 &BTRFS_I(inode
)->runtime_flags
)) {
7833 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
7834 (unsigned long long)btrfs_ino(inode
));
7835 atomic_dec(&root
->orphan_inodes
);
7839 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7843 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
7844 (unsigned long long)ordered
->file_offset
,
7845 (unsigned long long)ordered
->len
);
7846 btrfs_remove_ordered_extent(inode
, ordered
);
7847 btrfs_put_ordered_extent(ordered
);
7848 btrfs_put_ordered_extent(ordered
);
7851 inode_tree_del(inode
);
7852 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7854 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
7857 int btrfs_drop_inode(struct inode
*inode
)
7859 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7864 /* the snap/subvol tree is on deleting */
7865 if (btrfs_root_refs(&root
->root_item
) == 0 &&
7866 root
!= root
->fs_info
->tree_root
)
7869 return generic_drop_inode(inode
);
7872 static void init_once(void *foo
)
7874 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
7876 inode_init_once(&ei
->vfs_inode
);
7879 void btrfs_destroy_cachep(void)
7882 * Make sure all delayed rcu free inodes are flushed before we
7886 if (btrfs_inode_cachep
)
7887 kmem_cache_destroy(btrfs_inode_cachep
);
7888 if (btrfs_trans_handle_cachep
)
7889 kmem_cache_destroy(btrfs_trans_handle_cachep
);
7890 if (btrfs_transaction_cachep
)
7891 kmem_cache_destroy(btrfs_transaction_cachep
);
7892 if (btrfs_path_cachep
)
7893 kmem_cache_destroy(btrfs_path_cachep
);
7894 if (btrfs_free_space_cachep
)
7895 kmem_cache_destroy(btrfs_free_space_cachep
);
7896 if (btrfs_delalloc_work_cachep
)
7897 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
7900 int btrfs_init_cachep(void)
7902 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
7903 sizeof(struct btrfs_inode
), 0,
7904 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
7905 if (!btrfs_inode_cachep
)
7908 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
7909 sizeof(struct btrfs_trans_handle
), 0,
7910 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7911 if (!btrfs_trans_handle_cachep
)
7914 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
7915 sizeof(struct btrfs_transaction
), 0,
7916 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7917 if (!btrfs_transaction_cachep
)
7920 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
7921 sizeof(struct btrfs_path
), 0,
7922 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7923 if (!btrfs_path_cachep
)
7926 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
7927 sizeof(struct btrfs_free_space
), 0,
7928 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
7929 if (!btrfs_free_space_cachep
)
7932 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
7933 sizeof(struct btrfs_delalloc_work
), 0,
7934 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
7936 if (!btrfs_delalloc_work_cachep
)
7941 btrfs_destroy_cachep();
7945 static int btrfs_getattr(struct vfsmount
*mnt
,
7946 struct dentry
*dentry
, struct kstat
*stat
)
7949 struct inode
*inode
= dentry
->d_inode
;
7950 u32 blocksize
= inode
->i_sb
->s_blocksize
;
7952 generic_fillattr(inode
, stat
);
7953 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
7954 stat
->blksize
= PAGE_CACHE_SIZE
;
7956 spin_lock(&BTRFS_I(inode
)->lock
);
7957 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
7958 spin_unlock(&BTRFS_I(inode
)->lock
);
7959 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
7960 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
7964 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
7965 struct inode
*new_dir
, struct dentry
*new_dentry
)
7967 struct btrfs_trans_handle
*trans
;
7968 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
7969 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
7970 struct inode
*new_inode
= new_dentry
->d_inode
;
7971 struct inode
*old_inode
= old_dentry
->d_inode
;
7972 struct timespec ctime
= CURRENT_TIME
;
7976 u64 old_ino
= btrfs_ino(old_inode
);
7978 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
7981 /* we only allow rename subvolume link between subvolumes */
7982 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
7985 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
7986 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
7989 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
7990 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
7994 /* check for collisions, even if the name isn't there */
7995 ret
= btrfs_check_dir_item_collision(root
, new_dir
->i_ino
,
7996 new_dentry
->d_name
.name
,
7997 new_dentry
->d_name
.len
);
8000 if (ret
== -EEXIST
) {
8002 * eexist without a new_inode */
8008 /* maybe -EOVERFLOW */
8015 * we're using rename to replace one file with another.
8016 * and the replacement file is large. Start IO on it now so
8017 * we don't add too much work to the end of the transaction
8019 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
8020 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
8021 filemap_flush(old_inode
->i_mapping
);
8023 /* close the racy window with snapshot create/destroy ioctl */
8024 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8025 down_read(&root
->fs_info
->subvol_sem
);
8027 * We want to reserve the absolute worst case amount of items. So if
8028 * both inodes are subvols and we need to unlink them then that would
8029 * require 4 item modifications, but if they are both normal inodes it
8030 * would require 5 item modifications, so we'll assume their normal
8031 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8032 * should cover the worst case number of items we'll modify.
8034 trans
= btrfs_start_transaction(root
, 11);
8035 if (IS_ERR(trans
)) {
8036 ret
= PTR_ERR(trans
);
8041 btrfs_record_root_in_trans(trans
, dest
);
8043 ret
= btrfs_set_inode_index(new_dir
, &index
);
8047 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8048 /* force full log commit if subvolume involved. */
8049 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8051 ret
= btrfs_insert_inode_ref(trans
, dest
,
8052 new_dentry
->d_name
.name
,
8053 new_dentry
->d_name
.len
,
8055 btrfs_ino(new_dir
), index
);
8059 * this is an ugly little race, but the rename is required
8060 * to make sure that if we crash, the inode is either at the
8061 * old name or the new one. pinning the log transaction lets
8062 * us make sure we don't allow a log commit to come in after
8063 * we unlink the name but before we add the new name back in.
8065 btrfs_pin_log_trans(root
);
8068 * make sure the inode gets flushed if it is replacing
8071 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
8072 btrfs_add_ordered_operation(trans
, root
, old_inode
);
8074 inode_inc_iversion(old_dir
);
8075 inode_inc_iversion(new_dir
);
8076 inode_inc_iversion(old_inode
);
8077 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8078 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8079 old_inode
->i_ctime
= ctime
;
8081 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8082 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8084 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8085 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8086 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8087 old_dentry
->d_name
.name
,
8088 old_dentry
->d_name
.len
);
8090 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8091 old_dentry
->d_inode
,
8092 old_dentry
->d_name
.name
,
8093 old_dentry
->d_name
.len
);
8095 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8098 btrfs_abort_transaction(trans
, root
, ret
);
8103 inode_inc_iversion(new_inode
);
8104 new_inode
->i_ctime
= CURRENT_TIME
;
8105 if (unlikely(btrfs_ino(new_inode
) ==
8106 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8107 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8108 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
8110 new_dentry
->d_name
.name
,
8111 new_dentry
->d_name
.len
);
8112 BUG_ON(new_inode
->i_nlink
== 0);
8114 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
8115 new_dentry
->d_inode
,
8116 new_dentry
->d_name
.name
,
8117 new_dentry
->d_name
.len
);
8119 if (!ret
&& new_inode
->i_nlink
== 0) {
8120 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
8124 btrfs_abort_transaction(trans
, root
, ret
);
8129 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
8130 new_dentry
->d_name
.name
,
8131 new_dentry
->d_name
.len
, 0, index
);
8133 btrfs_abort_transaction(trans
, root
, ret
);
8137 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
8138 struct dentry
*parent
= new_dentry
->d_parent
;
8139 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
8140 btrfs_end_log_trans(root
);
8143 btrfs_end_transaction(trans
, root
);
8145 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8146 up_read(&root
->fs_info
->subvol_sem
);
8151 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
8153 struct btrfs_delalloc_work
*delalloc_work
;
8155 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
8157 if (delalloc_work
->wait
)
8158 btrfs_wait_ordered_range(delalloc_work
->inode
, 0, (u64
)-1);
8160 filemap_flush(delalloc_work
->inode
->i_mapping
);
8162 if (delalloc_work
->delay_iput
)
8163 btrfs_add_delayed_iput(delalloc_work
->inode
);
8165 iput(delalloc_work
->inode
);
8166 complete(&delalloc_work
->completion
);
8169 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
8170 int wait
, int delay_iput
)
8172 struct btrfs_delalloc_work
*work
;
8174 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
8178 init_completion(&work
->completion
);
8179 INIT_LIST_HEAD(&work
->list
);
8180 work
->inode
= inode
;
8182 work
->delay_iput
= delay_iput
;
8183 work
->work
.func
= btrfs_run_delalloc_work
;
8188 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
8190 wait_for_completion(&work
->completion
);
8191 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
8195 * some fairly slow code that needs optimization. This walks the list
8196 * of all the inodes with pending delalloc and forces them to disk.
8198 static int __start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8200 struct btrfs_inode
*binode
;
8201 struct inode
*inode
;
8202 struct btrfs_delalloc_work
*work
, *next
;
8203 struct list_head works
;
8204 struct list_head splice
;
8207 INIT_LIST_HEAD(&works
);
8208 INIT_LIST_HEAD(&splice
);
8210 spin_lock(&root
->delalloc_lock
);
8211 list_splice_init(&root
->delalloc_inodes
, &splice
);
8212 while (!list_empty(&splice
)) {
8213 binode
= list_entry(splice
.next
, struct btrfs_inode
,
8216 list_move_tail(&binode
->delalloc_inodes
,
8217 &root
->delalloc_inodes
);
8218 inode
= igrab(&binode
->vfs_inode
);
8220 cond_resched_lock(&root
->delalloc_lock
);
8223 spin_unlock(&root
->delalloc_lock
);
8225 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
8226 if (unlikely(!work
)) {
8230 list_add_tail(&work
->list
, &works
);
8231 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
8235 spin_lock(&root
->delalloc_lock
);
8237 spin_unlock(&root
->delalloc_lock
);
8239 list_for_each_entry_safe(work
, next
, &works
, list
) {
8240 list_del_init(&work
->list
);
8241 btrfs_wait_and_free_delalloc_work(work
);
8245 list_for_each_entry_safe(work
, next
, &works
, list
) {
8246 list_del_init(&work
->list
);
8247 btrfs_wait_and_free_delalloc_work(work
);
8250 if (!list_empty_careful(&splice
)) {
8251 spin_lock(&root
->delalloc_lock
);
8252 list_splice_tail(&splice
, &root
->delalloc_inodes
);
8253 spin_unlock(&root
->delalloc_lock
);
8258 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8262 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
8265 ret
= __start_delalloc_inodes(root
, delay_iput
);
8267 * the filemap_flush will queue IO into the worker threads, but
8268 * we have to make sure the IO is actually started and that
8269 * ordered extents get created before we return
8271 atomic_inc(&root
->fs_info
->async_submit_draining
);
8272 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
8273 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
8274 wait_event(root
->fs_info
->async_submit_wait
,
8275 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
8276 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
8278 atomic_dec(&root
->fs_info
->async_submit_draining
);
8282 int btrfs_start_all_delalloc_inodes(struct btrfs_fs_info
*fs_info
,
8285 struct btrfs_root
*root
;
8286 struct list_head splice
;
8289 if (fs_info
->sb
->s_flags
& MS_RDONLY
)
8292 INIT_LIST_HEAD(&splice
);
8294 spin_lock(&fs_info
->delalloc_root_lock
);
8295 list_splice_init(&fs_info
->delalloc_roots
, &splice
);
8296 while (!list_empty(&splice
)) {
8297 root
= list_first_entry(&splice
, struct btrfs_root
,
8299 root
= btrfs_grab_fs_root(root
);
8301 list_move_tail(&root
->delalloc_root
,
8302 &fs_info
->delalloc_roots
);
8303 spin_unlock(&fs_info
->delalloc_root_lock
);
8305 ret
= __start_delalloc_inodes(root
, delay_iput
);
8306 btrfs_put_fs_root(root
);
8310 spin_lock(&fs_info
->delalloc_root_lock
);
8312 spin_unlock(&fs_info
->delalloc_root_lock
);
8314 atomic_inc(&fs_info
->async_submit_draining
);
8315 while (atomic_read(&fs_info
->nr_async_submits
) ||
8316 atomic_read(&fs_info
->async_delalloc_pages
)) {
8317 wait_event(fs_info
->async_submit_wait
,
8318 (atomic_read(&fs_info
->nr_async_submits
) == 0 &&
8319 atomic_read(&fs_info
->async_delalloc_pages
) == 0));
8321 atomic_dec(&fs_info
->async_submit_draining
);
8324 if (!list_empty_careful(&splice
)) {
8325 spin_lock(&fs_info
->delalloc_root_lock
);
8326 list_splice_tail(&splice
, &fs_info
->delalloc_roots
);
8327 spin_unlock(&fs_info
->delalloc_root_lock
);
8332 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
8333 const char *symname
)
8335 struct btrfs_trans_handle
*trans
;
8336 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8337 struct btrfs_path
*path
;
8338 struct btrfs_key key
;
8339 struct inode
*inode
= NULL
;
8347 struct btrfs_file_extent_item
*ei
;
8348 struct extent_buffer
*leaf
;
8350 name_len
= strlen(symname
) + 1;
8351 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
8352 return -ENAMETOOLONG
;
8355 * 2 items for inode item and ref
8356 * 2 items for dir items
8357 * 1 item for xattr if selinux is on
8359 trans
= btrfs_start_transaction(root
, 5);
8361 return PTR_ERR(trans
);
8363 err
= btrfs_find_free_ino(root
, &objectid
);
8367 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
8368 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
8369 S_IFLNK
|S_IRWXUGO
, &index
);
8370 if (IS_ERR(inode
)) {
8371 err
= PTR_ERR(inode
);
8375 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
8382 * If the active LSM wants to access the inode during
8383 * d_instantiate it needs these. Smack checks to see
8384 * if the filesystem supports xattrs by looking at the
8387 inode
->i_fop
= &btrfs_file_operations
;
8388 inode
->i_op
= &btrfs_file_inode_operations
;
8390 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
8394 inode
->i_mapping
->a_ops
= &btrfs_aops
;
8395 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8396 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
8401 path
= btrfs_alloc_path();
8407 key
.objectid
= btrfs_ino(inode
);
8409 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
8410 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
8411 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
8415 btrfs_free_path(path
);
8418 leaf
= path
->nodes
[0];
8419 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
8420 struct btrfs_file_extent_item
);
8421 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
8422 btrfs_set_file_extent_type(leaf
, ei
,
8423 BTRFS_FILE_EXTENT_INLINE
);
8424 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
8425 btrfs_set_file_extent_compression(leaf
, ei
, 0);
8426 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
8427 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
8429 ptr
= btrfs_file_extent_inline_start(ei
);
8430 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
8431 btrfs_mark_buffer_dirty(leaf
);
8432 btrfs_free_path(path
);
8434 inode
->i_op
= &btrfs_symlink_inode_operations
;
8435 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
8436 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8437 inode_set_bytes(inode
, name_len
);
8438 btrfs_i_size_write(inode
, name_len
- 1);
8439 err
= btrfs_update_inode(trans
, root
, inode
);
8445 d_instantiate(dentry
, inode
);
8446 btrfs_end_transaction(trans
, root
);
8448 inode_dec_link_count(inode
);
8451 btrfs_btree_balance_dirty(root
);
8455 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8456 u64 start
, u64 num_bytes
, u64 min_size
,
8457 loff_t actual_len
, u64
*alloc_hint
,
8458 struct btrfs_trans_handle
*trans
)
8460 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
8461 struct extent_map
*em
;
8462 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8463 struct btrfs_key ins
;
8464 u64 cur_offset
= start
;
8468 bool own_trans
= true;
8472 while (num_bytes
> 0) {
8474 trans
= btrfs_start_transaction(root
, 3);
8475 if (IS_ERR(trans
)) {
8476 ret
= PTR_ERR(trans
);
8481 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
8482 cur_bytes
= max(cur_bytes
, min_size
);
8483 ret
= btrfs_reserve_extent(trans
, root
, cur_bytes
,
8484 min_size
, 0, *alloc_hint
, &ins
, 1);
8487 btrfs_end_transaction(trans
, root
);
8491 ret
= insert_reserved_file_extent(trans
, inode
,
8492 cur_offset
, ins
.objectid
,
8493 ins
.offset
, ins
.offset
,
8494 ins
.offset
, 0, 0, 0,
8495 BTRFS_FILE_EXTENT_PREALLOC
);
8497 btrfs_abort_transaction(trans
, root
, ret
);
8499 btrfs_end_transaction(trans
, root
);
8502 btrfs_drop_extent_cache(inode
, cur_offset
,
8503 cur_offset
+ ins
.offset
-1, 0);
8505 em
= alloc_extent_map();
8507 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
8508 &BTRFS_I(inode
)->runtime_flags
);
8512 em
->start
= cur_offset
;
8513 em
->orig_start
= cur_offset
;
8514 em
->len
= ins
.offset
;
8515 em
->block_start
= ins
.objectid
;
8516 em
->block_len
= ins
.offset
;
8517 em
->orig_block_len
= ins
.offset
;
8518 em
->ram_bytes
= ins
.offset
;
8519 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
8520 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
8521 em
->generation
= trans
->transid
;
8524 write_lock(&em_tree
->lock
);
8525 ret
= add_extent_mapping(em_tree
, em
, 1);
8526 write_unlock(&em_tree
->lock
);
8529 btrfs_drop_extent_cache(inode
, cur_offset
,
8530 cur_offset
+ ins
.offset
- 1,
8533 free_extent_map(em
);
8535 num_bytes
-= ins
.offset
;
8536 cur_offset
+= ins
.offset
;
8537 *alloc_hint
= ins
.objectid
+ ins
.offset
;
8539 inode_inc_iversion(inode
);
8540 inode
->i_ctime
= CURRENT_TIME
;
8541 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
8542 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
8543 (actual_len
> inode
->i_size
) &&
8544 (cur_offset
> inode
->i_size
)) {
8545 if (cur_offset
> actual_len
)
8546 i_size
= actual_len
;
8548 i_size
= cur_offset
;
8549 i_size_write(inode
, i_size
);
8550 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
8553 ret
= btrfs_update_inode(trans
, root
, inode
);
8556 btrfs_abort_transaction(trans
, root
, ret
);
8558 btrfs_end_transaction(trans
, root
);
8563 btrfs_end_transaction(trans
, root
);
8568 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8569 u64 start
, u64 num_bytes
, u64 min_size
,
8570 loff_t actual_len
, u64
*alloc_hint
)
8572 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8573 min_size
, actual_len
, alloc_hint
,
8577 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
8578 struct btrfs_trans_handle
*trans
, int mode
,
8579 u64 start
, u64 num_bytes
, u64 min_size
,
8580 loff_t actual_len
, u64
*alloc_hint
)
8582 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8583 min_size
, actual_len
, alloc_hint
, trans
);
8586 static int btrfs_set_page_dirty(struct page
*page
)
8588 return __set_page_dirty_nobuffers(page
);
8591 static int btrfs_permission(struct inode
*inode
, int mask
)
8593 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8594 umode_t mode
= inode
->i_mode
;
8596 if (mask
& MAY_WRITE
&&
8597 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
8598 if (btrfs_root_readonly(root
))
8600 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
8603 return generic_permission(inode
, mask
);
8606 static const struct inode_operations btrfs_dir_inode_operations
= {
8607 .getattr
= btrfs_getattr
,
8608 .lookup
= btrfs_lookup
,
8609 .create
= btrfs_create
,
8610 .unlink
= btrfs_unlink
,
8612 .mkdir
= btrfs_mkdir
,
8613 .rmdir
= btrfs_rmdir
,
8614 .rename
= btrfs_rename
,
8615 .symlink
= btrfs_symlink
,
8616 .setattr
= btrfs_setattr
,
8617 .mknod
= btrfs_mknod
,
8618 .setxattr
= btrfs_setxattr
,
8619 .getxattr
= btrfs_getxattr
,
8620 .listxattr
= btrfs_listxattr
,
8621 .removexattr
= btrfs_removexattr
,
8622 .permission
= btrfs_permission
,
8623 .get_acl
= btrfs_get_acl
,
8625 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
8626 .lookup
= btrfs_lookup
,
8627 .permission
= btrfs_permission
,
8628 .get_acl
= btrfs_get_acl
,
8631 static const struct file_operations btrfs_dir_file_operations
= {
8632 .llseek
= generic_file_llseek
,
8633 .read
= generic_read_dir
,
8634 .readdir
= btrfs_real_readdir
,
8635 .unlocked_ioctl
= btrfs_ioctl
,
8636 #ifdef CONFIG_COMPAT
8637 .compat_ioctl
= btrfs_ioctl
,
8639 .release
= btrfs_release_file
,
8640 .fsync
= btrfs_sync_file
,
8643 static struct extent_io_ops btrfs_extent_io_ops
= {
8644 .fill_delalloc
= run_delalloc_range
,
8645 .submit_bio_hook
= btrfs_submit_bio_hook
,
8646 .merge_bio_hook
= btrfs_merge_bio_hook
,
8647 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
8648 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
8649 .writepage_start_hook
= btrfs_writepage_start_hook
,
8650 .set_bit_hook
= btrfs_set_bit_hook
,
8651 .clear_bit_hook
= btrfs_clear_bit_hook
,
8652 .merge_extent_hook
= btrfs_merge_extent_hook
,
8653 .split_extent_hook
= btrfs_split_extent_hook
,
8657 * btrfs doesn't support the bmap operation because swapfiles
8658 * use bmap to make a mapping of extents in the file. They assume
8659 * these extents won't change over the life of the file and they
8660 * use the bmap result to do IO directly to the drive.
8662 * the btrfs bmap call would return logical addresses that aren't
8663 * suitable for IO and they also will change frequently as COW
8664 * operations happen. So, swapfile + btrfs == corruption.
8666 * For now we're avoiding this by dropping bmap.
8668 static const struct address_space_operations btrfs_aops
= {
8669 .readpage
= btrfs_readpage
,
8670 .writepage
= btrfs_writepage
,
8671 .writepages
= btrfs_writepages
,
8672 .readpages
= btrfs_readpages
,
8673 .direct_IO
= btrfs_direct_IO
,
8674 .invalidatepage
= btrfs_invalidatepage
,
8675 .releasepage
= btrfs_releasepage
,
8676 .set_page_dirty
= btrfs_set_page_dirty
,
8677 .error_remove_page
= generic_error_remove_page
,
8680 static const struct address_space_operations btrfs_symlink_aops
= {
8681 .readpage
= btrfs_readpage
,
8682 .writepage
= btrfs_writepage
,
8683 .invalidatepage
= btrfs_invalidatepage
,
8684 .releasepage
= btrfs_releasepage
,
8687 static const struct inode_operations btrfs_file_inode_operations
= {
8688 .getattr
= btrfs_getattr
,
8689 .setattr
= btrfs_setattr
,
8690 .setxattr
= btrfs_setxattr
,
8691 .getxattr
= btrfs_getxattr
,
8692 .listxattr
= btrfs_listxattr
,
8693 .removexattr
= btrfs_removexattr
,
8694 .permission
= btrfs_permission
,
8695 .fiemap
= btrfs_fiemap
,
8696 .get_acl
= btrfs_get_acl
,
8697 .update_time
= btrfs_update_time
,
8699 static const struct inode_operations btrfs_special_inode_operations
= {
8700 .getattr
= btrfs_getattr
,
8701 .setattr
= btrfs_setattr
,
8702 .permission
= btrfs_permission
,
8703 .setxattr
= btrfs_setxattr
,
8704 .getxattr
= btrfs_getxattr
,
8705 .listxattr
= btrfs_listxattr
,
8706 .removexattr
= btrfs_removexattr
,
8707 .get_acl
= btrfs_get_acl
,
8708 .update_time
= btrfs_update_time
,
8710 static const struct inode_operations btrfs_symlink_inode_operations
= {
8711 .readlink
= generic_readlink
,
8712 .follow_link
= page_follow_link_light
,
8713 .put_link
= page_put_link
,
8714 .getattr
= btrfs_getattr
,
8715 .setattr
= btrfs_setattr
,
8716 .permission
= btrfs_permission
,
8717 .setxattr
= btrfs_setxattr
,
8718 .getxattr
= btrfs_getxattr
,
8719 .listxattr
= btrfs_listxattr
,
8720 .removexattr
= btrfs_removexattr
,
8721 .get_acl
= btrfs_get_acl
,
8722 .update_time
= btrfs_update_time
,
8725 const struct dentry_operations btrfs_dentry_operations
= {
8726 .d_delete
= btrfs_dentry_delete
,
8727 .d_release
= btrfs_dentry_release
,