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
);
1532 * extent_io.c set_bit_hook, used to track delayed allocation
1533 * bytes in this file, and to maintain the list of inodes that
1534 * have pending delalloc work to be done.
1536 static void btrfs_set_bit_hook(struct inode
*inode
,
1537 struct extent_state
*state
, unsigned long *bits
)
1541 * set_bit and clear bit hooks normally require _irqsave/restore
1542 * but in this case, we are only testing for the DELALLOC
1543 * bit, which is only set or cleared with irqs on
1545 if (!(state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1546 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1547 u64 len
= state
->end
+ 1 - state
->start
;
1548 bool do_list
= !btrfs_is_free_space_inode(inode
);
1550 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1551 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1553 spin_lock(&BTRFS_I(inode
)->lock
);
1554 BTRFS_I(inode
)->outstanding_extents
++;
1555 spin_unlock(&BTRFS_I(inode
)->lock
);
1558 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, len
,
1559 root
->fs_info
->delalloc_batch
);
1560 spin_lock(&BTRFS_I(inode
)->lock
);
1561 BTRFS_I(inode
)->delalloc_bytes
+= len
;
1562 if (do_list
&& !test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1563 &BTRFS_I(inode
)->runtime_flags
)) {
1564 spin_lock(&root
->fs_info
->delalloc_lock
);
1565 if (list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1566 list_add_tail(&BTRFS_I(inode
)->delalloc_inodes
,
1567 &root
->fs_info
->delalloc_inodes
);
1568 set_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1569 &BTRFS_I(inode
)->runtime_flags
);
1571 spin_unlock(&root
->fs_info
->delalloc_lock
);
1573 spin_unlock(&BTRFS_I(inode
)->lock
);
1578 * extent_io.c clear_bit_hook, see set_bit_hook for why
1580 static void btrfs_clear_bit_hook(struct inode
*inode
,
1581 struct extent_state
*state
,
1582 unsigned long *bits
)
1585 * set_bit and clear bit hooks normally require _irqsave/restore
1586 * but in this case, we are only testing for the DELALLOC
1587 * bit, which is only set or cleared with irqs on
1589 if ((state
->state
& EXTENT_DELALLOC
) && (*bits
& EXTENT_DELALLOC
)) {
1590 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1591 u64 len
= state
->end
+ 1 - state
->start
;
1592 bool do_list
= !btrfs_is_free_space_inode(inode
);
1594 if (*bits
& EXTENT_FIRST_DELALLOC
) {
1595 *bits
&= ~EXTENT_FIRST_DELALLOC
;
1596 } else if (!(*bits
& EXTENT_DO_ACCOUNTING
)) {
1597 spin_lock(&BTRFS_I(inode
)->lock
);
1598 BTRFS_I(inode
)->outstanding_extents
--;
1599 spin_unlock(&BTRFS_I(inode
)->lock
);
1602 if (*bits
& EXTENT_DO_ACCOUNTING
)
1603 btrfs_delalloc_release_metadata(inode
, len
);
1605 if (root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
1607 btrfs_free_reserved_data_space(inode
, len
);
1609 __percpu_counter_add(&root
->fs_info
->delalloc_bytes
, -len
,
1610 root
->fs_info
->delalloc_batch
);
1611 spin_lock(&BTRFS_I(inode
)->lock
);
1612 BTRFS_I(inode
)->delalloc_bytes
-= len
;
1613 if (do_list
&& BTRFS_I(inode
)->delalloc_bytes
== 0 &&
1614 test_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1615 &BTRFS_I(inode
)->runtime_flags
)) {
1616 spin_lock(&root
->fs_info
->delalloc_lock
);
1617 if (!list_empty(&BTRFS_I(inode
)->delalloc_inodes
)) {
1618 list_del_init(&BTRFS_I(inode
)->delalloc_inodes
);
1619 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
1620 &BTRFS_I(inode
)->runtime_flags
);
1622 spin_unlock(&root
->fs_info
->delalloc_lock
);
1624 spin_unlock(&BTRFS_I(inode
)->lock
);
1629 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1630 * we don't create bios that span stripes or chunks
1632 int btrfs_merge_bio_hook(int rw
, struct page
*page
, unsigned long offset
,
1633 size_t size
, struct bio
*bio
,
1634 unsigned long bio_flags
)
1636 struct btrfs_root
*root
= BTRFS_I(page
->mapping
->host
)->root
;
1637 u64 logical
= (u64
)bio
->bi_sector
<< 9;
1642 if (bio_flags
& EXTENT_BIO_COMPRESSED
)
1645 length
= bio
->bi_size
;
1646 map_length
= length
;
1647 ret
= btrfs_map_block(root
->fs_info
, rw
, logical
,
1648 &map_length
, NULL
, 0);
1649 /* Will always return 0 with map_multi == NULL */
1651 if (map_length
< length
+ size
)
1657 * in order to insert checksums into the metadata in large chunks,
1658 * we wait until bio submission time. All the pages in the bio are
1659 * checksummed and sums are attached onto the ordered extent record.
1661 * At IO completion time the cums attached on the ordered extent record
1662 * are inserted into the btree
1664 static int __btrfs_submit_bio_start(struct inode
*inode
, int rw
,
1665 struct bio
*bio
, int mirror_num
,
1666 unsigned long bio_flags
,
1669 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1672 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1673 BUG_ON(ret
); /* -ENOMEM */
1678 * in order to insert checksums into the metadata in large chunks,
1679 * we wait until bio submission time. All the pages in the bio are
1680 * checksummed and sums are attached onto the ordered extent record.
1682 * At IO completion time the cums attached on the ordered extent record
1683 * are inserted into the btree
1685 static int __btrfs_submit_bio_done(struct inode
*inode
, int rw
, struct bio
*bio
,
1686 int mirror_num
, unsigned long bio_flags
,
1689 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1692 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 1);
1694 bio_endio(bio
, ret
);
1699 * extent_io.c submission hook. This does the right thing for csum calculation
1700 * on write, or reading the csums from the tree before a read
1702 static int btrfs_submit_bio_hook(struct inode
*inode
, int rw
, struct bio
*bio
,
1703 int mirror_num
, unsigned long bio_flags
,
1706 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1710 int async
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
1712 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
1714 if (btrfs_is_free_space_inode(inode
))
1717 if (!(rw
& REQ_WRITE
)) {
1718 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, metadata
);
1722 if (bio_flags
& EXTENT_BIO_COMPRESSED
) {
1723 ret
= btrfs_submit_compressed_read(inode
, bio
,
1727 } else if (!skip_sum
) {
1728 ret
= btrfs_lookup_bio_sums(root
, inode
, bio
, NULL
);
1733 } else if (async
&& !skip_sum
) {
1734 /* csum items have already been cloned */
1735 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
)
1737 /* we're doing a write, do the async checksumming */
1738 ret
= btrfs_wq_submit_bio(BTRFS_I(inode
)->root
->fs_info
,
1739 inode
, rw
, bio
, mirror_num
,
1740 bio_flags
, bio_offset
,
1741 __btrfs_submit_bio_start
,
1742 __btrfs_submit_bio_done
);
1744 } else if (!skip_sum
) {
1745 ret
= btrfs_csum_one_bio(root
, inode
, bio
, 0, 0);
1751 ret
= btrfs_map_bio(root
, rw
, bio
, mirror_num
, 0);
1755 bio_endio(bio
, ret
);
1760 * given a list of ordered sums record them in the inode. This happens
1761 * at IO completion time based on sums calculated at bio submission time.
1763 static noinline
int add_pending_csums(struct btrfs_trans_handle
*trans
,
1764 struct inode
*inode
, u64 file_offset
,
1765 struct list_head
*list
)
1767 struct btrfs_ordered_sum
*sum
;
1769 list_for_each_entry(sum
, list
, list
) {
1770 trans
->adding_csums
= 1;
1771 btrfs_csum_file_blocks(trans
,
1772 BTRFS_I(inode
)->root
->fs_info
->csum_root
, sum
);
1773 trans
->adding_csums
= 0;
1778 int btrfs_set_extent_delalloc(struct inode
*inode
, u64 start
, u64 end
,
1779 struct extent_state
**cached_state
)
1781 WARN_ON((end
& (PAGE_CACHE_SIZE
- 1)) == 0);
1782 return set_extent_delalloc(&BTRFS_I(inode
)->io_tree
, start
, end
,
1783 cached_state
, GFP_NOFS
);
1786 /* see btrfs_writepage_start_hook for details on why this is required */
1787 struct btrfs_writepage_fixup
{
1789 struct btrfs_work work
;
1792 static void btrfs_writepage_fixup_worker(struct btrfs_work
*work
)
1794 struct btrfs_writepage_fixup
*fixup
;
1795 struct btrfs_ordered_extent
*ordered
;
1796 struct extent_state
*cached_state
= NULL
;
1798 struct inode
*inode
;
1803 fixup
= container_of(work
, struct btrfs_writepage_fixup
, work
);
1807 if (!page
->mapping
|| !PageDirty(page
) || !PageChecked(page
)) {
1808 ClearPageChecked(page
);
1812 inode
= page
->mapping
->host
;
1813 page_start
= page_offset(page
);
1814 page_end
= page_offset(page
) + PAGE_CACHE_SIZE
- 1;
1816 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
, 0,
1819 /* already ordered? We're done */
1820 if (PagePrivate2(page
))
1823 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
1825 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
,
1826 page_end
, &cached_state
, GFP_NOFS
);
1828 btrfs_start_ordered_extent(inode
, ordered
, 1);
1829 btrfs_put_ordered_extent(ordered
);
1833 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
1835 mapping_set_error(page
->mapping
, ret
);
1836 end_extent_writepage(page
, ret
, page_start
, page_end
);
1837 ClearPageChecked(page
);
1841 btrfs_set_extent_delalloc(inode
, page_start
, page_end
, &cached_state
);
1842 ClearPageChecked(page
);
1843 set_page_dirty(page
);
1845 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
1846 &cached_state
, GFP_NOFS
);
1849 page_cache_release(page
);
1854 * There are a few paths in the higher layers of the kernel that directly
1855 * set the page dirty bit without asking the filesystem if it is a
1856 * good idea. This causes problems because we want to make sure COW
1857 * properly happens and the data=ordered rules are followed.
1859 * In our case any range that doesn't have the ORDERED bit set
1860 * hasn't been properly setup for IO. We kick off an async process
1861 * to fix it up. The async helper will wait for ordered extents, set
1862 * the delalloc bit and make it safe to write the page.
1864 static int btrfs_writepage_start_hook(struct page
*page
, u64 start
, u64 end
)
1866 struct inode
*inode
= page
->mapping
->host
;
1867 struct btrfs_writepage_fixup
*fixup
;
1868 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1870 /* this page is properly in the ordered list */
1871 if (TestClearPagePrivate2(page
))
1874 if (PageChecked(page
))
1877 fixup
= kzalloc(sizeof(*fixup
), GFP_NOFS
);
1881 SetPageChecked(page
);
1882 page_cache_get(page
);
1883 fixup
->work
.func
= btrfs_writepage_fixup_worker
;
1885 btrfs_queue_worker(&root
->fs_info
->fixup_workers
, &fixup
->work
);
1889 static int insert_reserved_file_extent(struct btrfs_trans_handle
*trans
,
1890 struct inode
*inode
, u64 file_pos
,
1891 u64 disk_bytenr
, u64 disk_num_bytes
,
1892 u64 num_bytes
, u64 ram_bytes
,
1893 u8 compression
, u8 encryption
,
1894 u16 other_encoding
, int extent_type
)
1896 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
1897 struct btrfs_file_extent_item
*fi
;
1898 struct btrfs_path
*path
;
1899 struct extent_buffer
*leaf
;
1900 struct btrfs_key ins
;
1903 path
= btrfs_alloc_path();
1907 path
->leave_spinning
= 1;
1910 * we may be replacing one extent in the tree with another.
1911 * The new extent is pinned in the extent map, and we don't want
1912 * to drop it from the cache until it is completely in the btree.
1914 * So, tell btrfs_drop_extents to leave this extent in the cache.
1915 * the caller is expected to unpin it and allow it to be merged
1918 ret
= btrfs_drop_extents(trans
, root
, inode
, file_pos
,
1919 file_pos
+ num_bytes
, 0);
1923 ins
.objectid
= btrfs_ino(inode
);
1924 ins
.offset
= file_pos
;
1925 ins
.type
= BTRFS_EXTENT_DATA_KEY
;
1926 ret
= btrfs_insert_empty_item(trans
, root
, path
, &ins
, sizeof(*fi
));
1929 leaf
= path
->nodes
[0];
1930 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
1931 struct btrfs_file_extent_item
);
1932 btrfs_set_file_extent_generation(leaf
, fi
, trans
->transid
);
1933 btrfs_set_file_extent_type(leaf
, fi
, extent_type
);
1934 btrfs_set_file_extent_disk_bytenr(leaf
, fi
, disk_bytenr
);
1935 btrfs_set_file_extent_disk_num_bytes(leaf
, fi
, disk_num_bytes
);
1936 btrfs_set_file_extent_offset(leaf
, fi
, 0);
1937 btrfs_set_file_extent_num_bytes(leaf
, fi
, num_bytes
);
1938 btrfs_set_file_extent_ram_bytes(leaf
, fi
, ram_bytes
);
1939 btrfs_set_file_extent_compression(leaf
, fi
, compression
);
1940 btrfs_set_file_extent_encryption(leaf
, fi
, encryption
);
1941 btrfs_set_file_extent_other_encoding(leaf
, fi
, other_encoding
);
1943 btrfs_mark_buffer_dirty(leaf
);
1944 btrfs_release_path(path
);
1946 inode_add_bytes(inode
, num_bytes
);
1948 ins
.objectid
= disk_bytenr
;
1949 ins
.offset
= disk_num_bytes
;
1950 ins
.type
= BTRFS_EXTENT_ITEM_KEY
;
1951 ret
= btrfs_alloc_reserved_file_extent(trans
, root
,
1952 root
->root_key
.objectid
,
1953 btrfs_ino(inode
), file_pos
, &ins
);
1955 btrfs_free_path(path
);
1960 /* snapshot-aware defrag */
1961 struct sa_defrag_extent_backref
{
1962 struct rb_node node
;
1963 struct old_sa_defrag_extent
*old
;
1972 struct old_sa_defrag_extent
{
1973 struct list_head list
;
1974 struct new_sa_defrag_extent
*new;
1983 struct new_sa_defrag_extent
{
1984 struct rb_root root
;
1985 struct list_head head
;
1986 struct btrfs_path
*path
;
1987 struct inode
*inode
;
1995 static int backref_comp(struct sa_defrag_extent_backref
*b1
,
1996 struct sa_defrag_extent_backref
*b2
)
1998 if (b1
->root_id
< b2
->root_id
)
2000 else if (b1
->root_id
> b2
->root_id
)
2003 if (b1
->inum
< b2
->inum
)
2005 else if (b1
->inum
> b2
->inum
)
2008 if (b1
->file_pos
< b2
->file_pos
)
2010 else if (b1
->file_pos
> b2
->file_pos
)
2014 * [------------------------------] ===> (a range of space)
2015 * |<--->| |<---->| =============> (fs/file tree A)
2016 * |<---------------------------->| ===> (fs/file tree B)
2018 * A range of space can refer to two file extents in one tree while
2019 * refer to only one file extent in another tree.
2021 * So we may process a disk offset more than one time(two extents in A)
2022 * and locate at the same extent(one extent in B), then insert two same
2023 * backrefs(both refer to the extent in B).
2028 static void backref_insert(struct rb_root
*root
,
2029 struct sa_defrag_extent_backref
*backref
)
2031 struct rb_node
**p
= &root
->rb_node
;
2032 struct rb_node
*parent
= NULL
;
2033 struct sa_defrag_extent_backref
*entry
;
2038 entry
= rb_entry(parent
, struct sa_defrag_extent_backref
, node
);
2040 ret
= backref_comp(backref
, entry
);
2044 p
= &(*p
)->rb_right
;
2047 rb_link_node(&backref
->node
, parent
, p
);
2048 rb_insert_color(&backref
->node
, root
);
2052 * Note the backref might has changed, and in this case we just return 0.
2054 static noinline
int record_one_backref(u64 inum
, u64 offset
, u64 root_id
,
2057 struct btrfs_file_extent_item
*extent
;
2058 struct btrfs_fs_info
*fs_info
;
2059 struct old_sa_defrag_extent
*old
= ctx
;
2060 struct new_sa_defrag_extent
*new = old
->new;
2061 struct btrfs_path
*path
= new->path
;
2062 struct btrfs_key key
;
2063 struct btrfs_root
*root
;
2064 struct sa_defrag_extent_backref
*backref
;
2065 struct extent_buffer
*leaf
;
2066 struct inode
*inode
= new->inode
;
2072 if (BTRFS_I(inode
)->root
->root_key
.objectid
== root_id
&&
2073 inum
== btrfs_ino(inode
))
2076 key
.objectid
= root_id
;
2077 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2078 key
.offset
= (u64
)-1;
2080 fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2081 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2083 if (PTR_ERR(root
) == -ENOENT
)
2086 pr_debug("inum=%llu, offset=%llu, root_id=%llu\n",
2087 inum
, offset
, root_id
);
2088 return PTR_ERR(root
);
2091 key
.objectid
= inum
;
2092 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2093 if (offset
> (u64
)-1 << 32)
2096 key
.offset
= offset
;
2098 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2107 leaf
= path
->nodes
[0];
2108 slot
= path
->slots
[0];
2110 if (slot
>= btrfs_header_nritems(leaf
)) {
2111 ret
= btrfs_next_leaf(root
, path
);
2114 } else if (ret
> 0) {
2123 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
2125 if (key
.objectid
> inum
)
2128 if (key
.objectid
< inum
|| key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2131 extent
= btrfs_item_ptr(leaf
, slot
,
2132 struct btrfs_file_extent_item
);
2134 if (btrfs_file_extent_disk_bytenr(leaf
, extent
) != old
->bytenr
)
2137 extent_offset
= btrfs_file_extent_offset(leaf
, extent
);
2138 if (key
.offset
- extent_offset
!= offset
)
2141 num_bytes
= btrfs_file_extent_num_bytes(leaf
, extent
);
2142 if (extent_offset
>= old
->extent_offset
+ old
->offset
+
2143 old
->len
|| extent_offset
+ num_bytes
<=
2144 old
->extent_offset
+ old
->offset
)
2150 backref
= kmalloc(sizeof(*backref
), GFP_NOFS
);
2156 backref
->root_id
= root_id
;
2157 backref
->inum
= inum
;
2158 backref
->file_pos
= offset
+ extent_offset
;
2159 backref
->num_bytes
= num_bytes
;
2160 backref
->extent_offset
= extent_offset
;
2161 backref
->generation
= btrfs_file_extent_generation(leaf
, extent
);
2163 backref_insert(&new->root
, backref
);
2166 btrfs_release_path(path
);
2171 static noinline
bool record_extent_backrefs(struct btrfs_path
*path
,
2172 struct new_sa_defrag_extent
*new)
2174 struct btrfs_fs_info
*fs_info
= BTRFS_I(new->inode
)->root
->fs_info
;
2175 struct old_sa_defrag_extent
*old
, *tmp
;
2180 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2181 ret
= iterate_inodes_from_logical(old
->bytenr
, fs_info
,
2182 path
, record_one_backref
,
2184 BUG_ON(ret
< 0 && ret
!= -ENOENT
);
2186 /* no backref to be processed for this extent */
2188 list_del(&old
->list
);
2193 if (list_empty(&new->head
))
2199 static int relink_is_mergable(struct extent_buffer
*leaf
,
2200 struct btrfs_file_extent_item
*fi
,
2203 if (btrfs_file_extent_disk_bytenr(leaf
, fi
) != disk_bytenr
)
2206 if (btrfs_file_extent_type(leaf
, fi
) != BTRFS_FILE_EXTENT_REG
)
2209 if (btrfs_file_extent_compression(leaf
, fi
) ||
2210 btrfs_file_extent_encryption(leaf
, fi
) ||
2211 btrfs_file_extent_other_encoding(leaf
, fi
))
2218 * Note the backref might has changed, and in this case we just return 0.
2220 static noinline
int relink_extent_backref(struct btrfs_path
*path
,
2221 struct sa_defrag_extent_backref
*prev
,
2222 struct sa_defrag_extent_backref
*backref
)
2224 struct btrfs_file_extent_item
*extent
;
2225 struct btrfs_file_extent_item
*item
;
2226 struct btrfs_ordered_extent
*ordered
;
2227 struct btrfs_trans_handle
*trans
;
2228 struct btrfs_fs_info
*fs_info
;
2229 struct btrfs_root
*root
;
2230 struct btrfs_key key
;
2231 struct extent_buffer
*leaf
;
2232 struct old_sa_defrag_extent
*old
= backref
->old
;
2233 struct new_sa_defrag_extent
*new = old
->new;
2234 struct inode
*src_inode
= new->inode
;
2235 struct inode
*inode
;
2236 struct extent_state
*cached
= NULL
;
2245 if (prev
&& prev
->root_id
== backref
->root_id
&&
2246 prev
->inum
== backref
->inum
&&
2247 prev
->file_pos
+ prev
->num_bytes
== backref
->file_pos
)
2250 /* step 1: get root */
2251 key
.objectid
= backref
->root_id
;
2252 key
.type
= BTRFS_ROOT_ITEM_KEY
;
2253 key
.offset
= (u64
)-1;
2255 fs_info
= BTRFS_I(src_inode
)->root
->fs_info
;
2256 index
= srcu_read_lock(&fs_info
->subvol_srcu
);
2258 root
= btrfs_read_fs_root_no_name(fs_info
, &key
);
2260 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2261 if (PTR_ERR(root
) == -ENOENT
)
2263 return PTR_ERR(root
);
2266 /* step 2: get inode */
2267 key
.objectid
= backref
->inum
;
2268 key
.type
= BTRFS_INODE_ITEM_KEY
;
2271 inode
= btrfs_iget(fs_info
->sb
, &key
, root
, NULL
);
2272 if (IS_ERR(inode
)) {
2273 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2277 srcu_read_unlock(&fs_info
->subvol_srcu
, index
);
2279 /* step 3: relink backref */
2280 lock_start
= backref
->file_pos
;
2281 lock_end
= backref
->file_pos
+ backref
->num_bytes
- 1;
2282 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2285 ordered
= btrfs_lookup_first_ordered_extent(inode
, lock_end
);
2287 btrfs_put_ordered_extent(ordered
);
2291 trans
= btrfs_join_transaction(root
);
2292 if (IS_ERR(trans
)) {
2293 ret
= PTR_ERR(trans
);
2297 key
.objectid
= backref
->inum
;
2298 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2299 key
.offset
= backref
->file_pos
;
2301 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2304 } else if (ret
> 0) {
2309 extent
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
2310 struct btrfs_file_extent_item
);
2312 if (btrfs_file_extent_generation(path
->nodes
[0], extent
) !=
2313 backref
->generation
)
2316 btrfs_release_path(path
);
2318 start
= backref
->file_pos
;
2319 if (backref
->extent_offset
< old
->extent_offset
+ old
->offset
)
2320 start
+= old
->extent_offset
+ old
->offset
-
2321 backref
->extent_offset
;
2323 len
= min(backref
->extent_offset
+ backref
->num_bytes
,
2324 old
->extent_offset
+ old
->offset
+ old
->len
);
2325 len
-= max(backref
->extent_offset
, old
->extent_offset
+ old
->offset
);
2327 ret
= btrfs_drop_extents(trans
, root
, inode
, start
,
2332 key
.objectid
= btrfs_ino(inode
);
2333 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2336 path
->leave_spinning
= 1;
2338 struct btrfs_file_extent_item
*fi
;
2340 struct btrfs_key found_key
;
2342 ret
= btrfs_search_slot(trans
, root
, &key
, path
, 1, 1);
2347 leaf
= path
->nodes
[0];
2348 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
2350 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
2351 struct btrfs_file_extent_item
);
2352 extent_len
= btrfs_file_extent_num_bytes(leaf
, fi
);
2354 if (relink_is_mergable(leaf
, fi
, new->bytenr
) &&
2355 extent_len
+ found_key
.offset
== start
) {
2356 btrfs_set_file_extent_num_bytes(leaf
, fi
,
2358 btrfs_mark_buffer_dirty(leaf
);
2359 inode_add_bytes(inode
, len
);
2365 btrfs_release_path(path
);
2370 ret
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
2373 btrfs_abort_transaction(trans
, root
, ret
);
2377 leaf
= path
->nodes
[0];
2378 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
2379 struct btrfs_file_extent_item
);
2380 btrfs_set_file_extent_disk_bytenr(leaf
, item
, new->bytenr
);
2381 btrfs_set_file_extent_disk_num_bytes(leaf
, item
, new->disk_len
);
2382 btrfs_set_file_extent_offset(leaf
, item
, start
- new->file_pos
);
2383 btrfs_set_file_extent_num_bytes(leaf
, item
, len
);
2384 btrfs_set_file_extent_ram_bytes(leaf
, item
, new->len
);
2385 btrfs_set_file_extent_generation(leaf
, item
, trans
->transid
);
2386 btrfs_set_file_extent_type(leaf
, item
, BTRFS_FILE_EXTENT_REG
);
2387 btrfs_set_file_extent_compression(leaf
, item
, new->compress_type
);
2388 btrfs_set_file_extent_encryption(leaf
, item
, 0);
2389 btrfs_set_file_extent_other_encoding(leaf
, item
, 0);
2391 btrfs_mark_buffer_dirty(leaf
);
2392 inode_add_bytes(inode
, len
);
2393 btrfs_release_path(path
);
2395 ret
= btrfs_inc_extent_ref(trans
, root
, new->bytenr
,
2397 backref
->root_id
, backref
->inum
,
2398 new->file_pos
, 0); /* start - extent_offset */
2400 btrfs_abort_transaction(trans
, root
, ret
);
2406 btrfs_release_path(path
);
2407 path
->leave_spinning
= 0;
2408 btrfs_end_transaction(trans
, root
);
2410 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lock_start
, lock_end
,
2416 static void relink_file_extents(struct new_sa_defrag_extent
*new)
2418 struct btrfs_path
*path
;
2419 struct old_sa_defrag_extent
*old
, *tmp
;
2420 struct sa_defrag_extent_backref
*backref
;
2421 struct sa_defrag_extent_backref
*prev
= NULL
;
2422 struct inode
*inode
;
2423 struct btrfs_root
*root
;
2424 struct rb_node
*node
;
2428 root
= BTRFS_I(inode
)->root
;
2430 path
= btrfs_alloc_path();
2434 if (!record_extent_backrefs(path
, new)) {
2435 btrfs_free_path(path
);
2438 btrfs_release_path(path
);
2441 node
= rb_first(&new->root
);
2444 rb_erase(node
, &new->root
);
2446 backref
= rb_entry(node
, struct sa_defrag_extent_backref
, node
);
2448 ret
= relink_extent_backref(path
, prev
, backref
);
2461 btrfs_free_path(path
);
2463 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2464 list_del(&old
->list
);
2468 atomic_dec(&root
->fs_info
->defrag_running
);
2469 wake_up(&root
->fs_info
->transaction_wait
);
2474 static struct new_sa_defrag_extent
*
2475 record_old_file_extents(struct inode
*inode
,
2476 struct btrfs_ordered_extent
*ordered
)
2478 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2479 struct btrfs_path
*path
;
2480 struct btrfs_key key
;
2481 struct old_sa_defrag_extent
*old
, *tmp
;
2482 struct new_sa_defrag_extent
*new;
2485 new = kmalloc(sizeof(*new), GFP_NOFS
);
2490 new->file_pos
= ordered
->file_offset
;
2491 new->len
= ordered
->len
;
2492 new->bytenr
= ordered
->start
;
2493 new->disk_len
= ordered
->disk_len
;
2494 new->compress_type
= ordered
->compress_type
;
2495 new->root
= RB_ROOT
;
2496 INIT_LIST_HEAD(&new->head
);
2498 path
= btrfs_alloc_path();
2502 key
.objectid
= btrfs_ino(inode
);
2503 key
.type
= BTRFS_EXTENT_DATA_KEY
;
2504 key
.offset
= new->file_pos
;
2506 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
2509 if (ret
> 0 && path
->slots
[0] > 0)
2512 /* find out all the old extents for the file range */
2514 struct btrfs_file_extent_item
*extent
;
2515 struct extent_buffer
*l
;
2524 slot
= path
->slots
[0];
2526 if (slot
>= btrfs_header_nritems(l
)) {
2527 ret
= btrfs_next_leaf(root
, path
);
2535 btrfs_item_key_to_cpu(l
, &key
, slot
);
2537 if (key
.objectid
!= btrfs_ino(inode
))
2539 if (key
.type
!= BTRFS_EXTENT_DATA_KEY
)
2541 if (key
.offset
>= new->file_pos
+ new->len
)
2544 extent
= btrfs_item_ptr(l
, slot
, struct btrfs_file_extent_item
);
2546 num_bytes
= btrfs_file_extent_num_bytes(l
, extent
);
2547 if (key
.offset
+ num_bytes
< new->file_pos
)
2550 disk_bytenr
= btrfs_file_extent_disk_bytenr(l
, extent
);
2554 extent_offset
= btrfs_file_extent_offset(l
, extent
);
2556 old
= kmalloc(sizeof(*old
), GFP_NOFS
);
2560 offset
= max(new->file_pos
, key
.offset
);
2561 end
= min(new->file_pos
+ new->len
, key
.offset
+ num_bytes
);
2563 old
->bytenr
= disk_bytenr
;
2564 old
->extent_offset
= extent_offset
;
2565 old
->offset
= offset
- key
.offset
;
2566 old
->len
= end
- offset
;
2569 list_add_tail(&old
->list
, &new->head
);
2575 btrfs_free_path(path
);
2576 atomic_inc(&root
->fs_info
->defrag_running
);
2581 list_for_each_entry_safe(old
, tmp
, &new->head
, list
) {
2582 list_del(&old
->list
);
2586 btrfs_free_path(path
);
2593 * helper function for btrfs_finish_ordered_io, this
2594 * just reads in some of the csum leaves to prime them into ram
2595 * before we start the transaction. It limits the amount of btree
2596 * reads required while inside the transaction.
2598 /* as ordered data IO finishes, this gets called so we can finish
2599 * an ordered extent if the range of bytes in the file it covers are
2602 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent
*ordered_extent
)
2604 struct inode
*inode
= ordered_extent
->inode
;
2605 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2606 struct btrfs_trans_handle
*trans
= NULL
;
2607 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2608 struct extent_state
*cached_state
= NULL
;
2609 struct new_sa_defrag_extent
*new = NULL
;
2610 int compress_type
= 0;
2614 nolock
= btrfs_is_free_space_inode(inode
);
2616 if (test_bit(BTRFS_ORDERED_IOERR
, &ordered_extent
->flags
)) {
2621 if (test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
)) {
2622 BUG_ON(!list_empty(&ordered_extent
->list
)); /* Logic error */
2623 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2625 trans
= btrfs_join_transaction_nolock(root
);
2627 trans
= btrfs_join_transaction(root
);
2628 if (IS_ERR(trans
)) {
2629 ret
= PTR_ERR(trans
);
2633 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2634 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2635 if (ret
) /* -ENOMEM or corruption */
2636 btrfs_abort_transaction(trans
, root
, ret
);
2640 lock_extent_bits(io_tree
, ordered_extent
->file_offset
,
2641 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2644 ret
= test_range_bit(io_tree
, ordered_extent
->file_offset
,
2645 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2646 EXTENT_DEFRAG
, 1, cached_state
);
2648 u64 last_snapshot
= btrfs_root_last_snapshot(&root
->root_item
);
2649 if (last_snapshot
>= BTRFS_I(inode
)->generation
)
2650 /* the inode is shared */
2651 new = record_old_file_extents(inode
, ordered_extent
);
2653 clear_extent_bit(io_tree
, ordered_extent
->file_offset
,
2654 ordered_extent
->file_offset
+ ordered_extent
->len
- 1,
2655 EXTENT_DEFRAG
, 0, 0, &cached_state
, GFP_NOFS
);
2659 trans
= btrfs_join_transaction_nolock(root
);
2661 trans
= btrfs_join_transaction(root
);
2662 if (IS_ERR(trans
)) {
2663 ret
= PTR_ERR(trans
);
2667 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
2669 if (test_bit(BTRFS_ORDERED_COMPRESSED
, &ordered_extent
->flags
))
2670 compress_type
= ordered_extent
->compress_type
;
2671 if (test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
)) {
2672 BUG_ON(compress_type
);
2673 ret
= btrfs_mark_extent_written(trans
, inode
,
2674 ordered_extent
->file_offset
,
2675 ordered_extent
->file_offset
+
2676 ordered_extent
->len
);
2678 BUG_ON(root
== root
->fs_info
->tree_root
);
2679 ret
= insert_reserved_file_extent(trans
, inode
,
2680 ordered_extent
->file_offset
,
2681 ordered_extent
->start
,
2682 ordered_extent
->disk_len
,
2683 ordered_extent
->len
,
2684 ordered_extent
->len
,
2685 compress_type
, 0, 0,
2686 BTRFS_FILE_EXTENT_REG
);
2688 unpin_extent_cache(&BTRFS_I(inode
)->extent_tree
,
2689 ordered_extent
->file_offset
, ordered_extent
->len
,
2692 btrfs_abort_transaction(trans
, root
, ret
);
2696 add_pending_csums(trans
, inode
, ordered_extent
->file_offset
,
2697 &ordered_extent
->list
);
2699 btrfs_ordered_update_i_size(inode
, 0, ordered_extent
);
2700 ret
= btrfs_update_inode_fallback(trans
, root
, inode
);
2701 if (ret
) { /* -ENOMEM or corruption */
2702 btrfs_abort_transaction(trans
, root
, ret
);
2707 unlock_extent_cached(io_tree
, ordered_extent
->file_offset
,
2708 ordered_extent
->file_offset
+
2709 ordered_extent
->len
- 1, &cached_state
, GFP_NOFS
);
2711 if (root
!= root
->fs_info
->tree_root
)
2712 btrfs_delalloc_release_metadata(inode
, ordered_extent
->len
);
2714 btrfs_end_transaction(trans
, root
);
2717 clear_extent_uptodate(io_tree
, ordered_extent
->file_offset
,
2718 ordered_extent
->file_offset
+
2719 ordered_extent
->len
- 1, NULL
, GFP_NOFS
);
2722 * If the ordered extent had an IOERR or something else went
2723 * wrong we need to return the space for this ordered extent
2724 * back to the allocator.
2726 if (!test_bit(BTRFS_ORDERED_NOCOW
, &ordered_extent
->flags
) &&
2727 !test_bit(BTRFS_ORDERED_PREALLOC
, &ordered_extent
->flags
))
2728 btrfs_free_reserved_extent(root
, ordered_extent
->start
,
2729 ordered_extent
->disk_len
);
2734 * This needs to be done to make sure anybody waiting knows we are done
2735 * updating everything for this ordered extent.
2737 btrfs_remove_ordered_extent(inode
, ordered_extent
);
2739 /* for snapshot-aware defrag */
2741 relink_file_extents(new);
2744 btrfs_put_ordered_extent(ordered_extent
);
2745 /* once for the tree */
2746 btrfs_put_ordered_extent(ordered_extent
);
2751 static void finish_ordered_fn(struct btrfs_work
*work
)
2753 struct btrfs_ordered_extent
*ordered_extent
;
2754 ordered_extent
= container_of(work
, struct btrfs_ordered_extent
, work
);
2755 btrfs_finish_ordered_io(ordered_extent
);
2758 static int btrfs_writepage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2759 struct extent_state
*state
, int uptodate
)
2761 struct inode
*inode
= page
->mapping
->host
;
2762 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2763 struct btrfs_ordered_extent
*ordered_extent
= NULL
;
2764 struct btrfs_workers
*workers
;
2766 trace_btrfs_writepage_end_io_hook(page
, start
, end
, uptodate
);
2768 ClearPagePrivate2(page
);
2769 if (!btrfs_dec_test_ordered_pending(inode
, &ordered_extent
, start
,
2770 end
- start
+ 1, uptodate
))
2773 ordered_extent
->work
.func
= finish_ordered_fn
;
2774 ordered_extent
->work
.flags
= 0;
2776 if (btrfs_is_free_space_inode(inode
))
2777 workers
= &root
->fs_info
->endio_freespace_worker
;
2779 workers
= &root
->fs_info
->endio_write_workers
;
2780 btrfs_queue_worker(workers
, &ordered_extent
->work
);
2786 * when reads are done, we need to check csums to verify the data is correct
2787 * if there's a match, we allow the bio to finish. If not, the code in
2788 * extent_io.c will try to find good copies for us.
2790 static int btrfs_readpage_end_io_hook(struct page
*page
, u64 start
, u64 end
,
2791 struct extent_state
*state
, int mirror
)
2793 size_t offset
= start
- page_offset(page
);
2794 struct inode
*inode
= page
->mapping
->host
;
2795 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
2797 u64
private = ~(u32
)0;
2799 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2801 static DEFINE_RATELIMIT_STATE(_rs
, DEFAULT_RATELIMIT_INTERVAL
,
2802 DEFAULT_RATELIMIT_BURST
);
2804 if (PageChecked(page
)) {
2805 ClearPageChecked(page
);
2809 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)
2812 if (root
->root_key
.objectid
== BTRFS_DATA_RELOC_TREE_OBJECTID
&&
2813 test_range_bit(io_tree
, start
, end
, EXTENT_NODATASUM
, 1, NULL
)) {
2814 clear_extent_bits(io_tree
, start
, end
, EXTENT_NODATASUM
,
2819 if (state
&& state
->start
== start
) {
2820 private = state
->private;
2823 ret
= get_state_private(io_tree
, start
, &private);
2825 kaddr
= kmap_atomic(page
);
2829 csum
= btrfs_csum_data(kaddr
+ offset
, csum
, end
- start
+ 1);
2830 btrfs_csum_final(csum
, (char *)&csum
);
2831 if (csum
!= private)
2834 kunmap_atomic(kaddr
);
2839 if (__ratelimit(&_rs
))
2840 btrfs_info(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %llu",
2841 (unsigned long long)btrfs_ino(page
->mapping
->host
),
2842 (unsigned long long)start
, csum
,
2843 (unsigned long long)private);
2844 memset(kaddr
+ offset
, 1, end
- start
+ 1);
2845 flush_dcache_page(page
);
2846 kunmap_atomic(kaddr
);
2852 struct delayed_iput
{
2853 struct list_head list
;
2854 struct inode
*inode
;
2857 /* JDM: If this is fs-wide, why can't we add a pointer to
2858 * btrfs_inode instead and avoid the allocation? */
2859 void btrfs_add_delayed_iput(struct inode
*inode
)
2861 struct btrfs_fs_info
*fs_info
= BTRFS_I(inode
)->root
->fs_info
;
2862 struct delayed_iput
*delayed
;
2864 if (atomic_add_unless(&inode
->i_count
, -1, 1))
2867 delayed
= kmalloc(sizeof(*delayed
), GFP_NOFS
| __GFP_NOFAIL
);
2868 delayed
->inode
= inode
;
2870 spin_lock(&fs_info
->delayed_iput_lock
);
2871 list_add_tail(&delayed
->list
, &fs_info
->delayed_iputs
);
2872 spin_unlock(&fs_info
->delayed_iput_lock
);
2875 void btrfs_run_delayed_iputs(struct btrfs_root
*root
)
2878 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
2879 struct delayed_iput
*delayed
;
2882 spin_lock(&fs_info
->delayed_iput_lock
);
2883 empty
= list_empty(&fs_info
->delayed_iputs
);
2884 spin_unlock(&fs_info
->delayed_iput_lock
);
2888 spin_lock(&fs_info
->delayed_iput_lock
);
2889 list_splice_init(&fs_info
->delayed_iputs
, &list
);
2890 spin_unlock(&fs_info
->delayed_iput_lock
);
2892 while (!list_empty(&list
)) {
2893 delayed
= list_entry(list
.next
, struct delayed_iput
, list
);
2894 list_del(&delayed
->list
);
2895 iput(delayed
->inode
);
2901 * This is called in transaction commit time. If there are no orphan
2902 * files in the subvolume, it removes orphan item and frees block_rsv
2905 void btrfs_orphan_commit_root(struct btrfs_trans_handle
*trans
,
2906 struct btrfs_root
*root
)
2908 struct btrfs_block_rsv
*block_rsv
;
2911 if (atomic_read(&root
->orphan_inodes
) ||
2912 root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
)
2915 spin_lock(&root
->orphan_lock
);
2916 if (atomic_read(&root
->orphan_inodes
)) {
2917 spin_unlock(&root
->orphan_lock
);
2921 if (root
->orphan_cleanup_state
!= ORPHAN_CLEANUP_DONE
) {
2922 spin_unlock(&root
->orphan_lock
);
2926 block_rsv
= root
->orphan_block_rsv
;
2927 root
->orphan_block_rsv
= NULL
;
2928 spin_unlock(&root
->orphan_lock
);
2930 if (root
->orphan_item_inserted
&&
2931 btrfs_root_refs(&root
->root_item
) > 0) {
2932 ret
= btrfs_del_orphan_item(trans
, root
->fs_info
->tree_root
,
2933 root
->root_key
.objectid
);
2935 root
->orphan_item_inserted
= 0;
2939 WARN_ON(block_rsv
->size
> 0);
2940 btrfs_free_block_rsv(root
, block_rsv
);
2945 * This creates an orphan entry for the given inode in case something goes
2946 * wrong in the middle of an unlink/truncate.
2948 * NOTE: caller of this function should reserve 5 units of metadata for
2951 int btrfs_orphan_add(struct btrfs_trans_handle
*trans
, struct inode
*inode
)
2953 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
2954 struct btrfs_block_rsv
*block_rsv
= NULL
;
2959 if (!root
->orphan_block_rsv
) {
2960 block_rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
2965 spin_lock(&root
->orphan_lock
);
2966 if (!root
->orphan_block_rsv
) {
2967 root
->orphan_block_rsv
= block_rsv
;
2968 } else if (block_rsv
) {
2969 btrfs_free_block_rsv(root
, block_rsv
);
2973 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
2974 &BTRFS_I(inode
)->runtime_flags
)) {
2977 * For proper ENOSPC handling, we should do orphan
2978 * cleanup when mounting. But this introduces backward
2979 * compatibility issue.
2981 if (!xchg(&root
->orphan_item_inserted
, 1))
2987 atomic_inc(&root
->orphan_inodes
);
2990 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
2991 &BTRFS_I(inode
)->runtime_flags
))
2993 spin_unlock(&root
->orphan_lock
);
2995 /* grab metadata reservation from transaction handle */
2997 ret
= btrfs_orphan_reserve_metadata(trans
, inode
);
2998 BUG_ON(ret
); /* -ENOSPC in reservation; Logic error? JDM */
3001 /* insert an orphan item to track this unlinked/truncated file */
3003 ret
= btrfs_insert_orphan_item(trans
, root
, btrfs_ino(inode
));
3004 if (ret
&& ret
!= -EEXIST
) {
3005 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3006 &BTRFS_I(inode
)->runtime_flags
);
3007 btrfs_abort_transaction(trans
, root
, ret
);
3013 /* insert an orphan item to track subvolume contains orphan files */
3015 ret
= btrfs_insert_orphan_item(trans
, root
->fs_info
->tree_root
,
3016 root
->root_key
.objectid
);
3017 if (ret
&& ret
!= -EEXIST
) {
3018 btrfs_abort_transaction(trans
, root
, ret
);
3026 * We have done the truncate/delete so we can go ahead and remove the orphan
3027 * item for this particular inode.
3029 static int btrfs_orphan_del(struct btrfs_trans_handle
*trans
,
3030 struct inode
*inode
)
3032 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3033 int delete_item
= 0;
3034 int release_rsv
= 0;
3037 spin_lock(&root
->orphan_lock
);
3038 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3039 &BTRFS_I(inode
)->runtime_flags
))
3042 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED
,
3043 &BTRFS_I(inode
)->runtime_flags
))
3045 spin_unlock(&root
->orphan_lock
);
3047 if (trans
&& delete_item
) {
3048 ret
= btrfs_del_orphan_item(trans
, root
, btrfs_ino(inode
));
3049 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3053 btrfs_orphan_release_metadata(inode
);
3054 atomic_dec(&root
->orphan_inodes
);
3061 * this cleans up any orphans that may be left on the list from the last use
3064 int btrfs_orphan_cleanup(struct btrfs_root
*root
)
3066 struct btrfs_path
*path
;
3067 struct extent_buffer
*leaf
;
3068 struct btrfs_key key
, found_key
;
3069 struct btrfs_trans_handle
*trans
;
3070 struct inode
*inode
;
3071 u64 last_objectid
= 0;
3072 int ret
= 0, nr_unlink
= 0, nr_truncate
= 0;
3074 if (cmpxchg(&root
->orphan_cleanup_state
, 0, ORPHAN_CLEANUP_STARTED
))
3077 path
= btrfs_alloc_path();
3084 key
.objectid
= BTRFS_ORPHAN_OBJECTID
;
3085 btrfs_set_key_type(&key
, BTRFS_ORPHAN_ITEM_KEY
);
3086 key
.offset
= (u64
)-1;
3089 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
3094 * if ret == 0 means we found what we were searching for, which
3095 * is weird, but possible, so only screw with path if we didn't
3096 * find the key and see if we have stuff that matches
3100 if (path
->slots
[0] == 0)
3105 /* pull out the item */
3106 leaf
= path
->nodes
[0];
3107 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
3109 /* make sure the item matches what we want */
3110 if (found_key
.objectid
!= BTRFS_ORPHAN_OBJECTID
)
3112 if (btrfs_key_type(&found_key
) != BTRFS_ORPHAN_ITEM_KEY
)
3115 /* release the path since we're done with it */
3116 btrfs_release_path(path
);
3119 * this is where we are basically btrfs_lookup, without the
3120 * crossing root thing. we store the inode number in the
3121 * offset of the orphan item.
3124 if (found_key
.offset
== last_objectid
) {
3125 btrfs_err(root
->fs_info
,
3126 "Error removing orphan entry, stopping orphan cleanup");
3131 last_objectid
= found_key
.offset
;
3133 found_key
.objectid
= found_key
.offset
;
3134 found_key
.type
= BTRFS_INODE_ITEM_KEY
;
3135 found_key
.offset
= 0;
3136 inode
= btrfs_iget(root
->fs_info
->sb
, &found_key
, root
, NULL
);
3137 ret
= PTR_RET(inode
);
3138 if (ret
&& ret
!= -ESTALE
)
3141 if (ret
== -ESTALE
&& root
== root
->fs_info
->tree_root
) {
3142 struct btrfs_root
*dead_root
;
3143 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
3144 int is_dead_root
= 0;
3147 * this is an orphan in the tree root. Currently these
3148 * could come from 2 sources:
3149 * a) a snapshot deletion in progress
3150 * b) a free space cache inode
3151 * We need to distinguish those two, as the snapshot
3152 * orphan must not get deleted.
3153 * find_dead_roots already ran before us, so if this
3154 * is a snapshot deletion, we should find the root
3155 * in the dead_roots list
3157 spin_lock(&fs_info
->trans_lock
);
3158 list_for_each_entry(dead_root
, &fs_info
->dead_roots
,
3160 if (dead_root
->root_key
.objectid
==
3161 found_key
.objectid
) {
3166 spin_unlock(&fs_info
->trans_lock
);
3168 /* prevent this orphan from being found again */
3169 key
.offset
= found_key
.objectid
- 1;
3174 * Inode is already gone but the orphan item is still there,
3175 * kill the orphan item.
3177 if (ret
== -ESTALE
) {
3178 trans
= btrfs_start_transaction(root
, 1);
3179 if (IS_ERR(trans
)) {
3180 ret
= PTR_ERR(trans
);
3183 btrfs_debug(root
->fs_info
, "auto deleting %Lu",
3184 found_key
.objectid
);
3185 ret
= btrfs_del_orphan_item(trans
, root
,
3186 found_key
.objectid
);
3187 BUG_ON(ret
); /* -ENOMEM or corruption (JDM: Recheck) */
3188 btrfs_end_transaction(trans
, root
);
3193 * add this inode to the orphan list so btrfs_orphan_del does
3194 * the proper thing when we hit it
3196 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
3197 &BTRFS_I(inode
)->runtime_flags
);
3198 atomic_inc(&root
->orphan_inodes
);
3200 /* if we have links, this was a truncate, lets do that */
3201 if (inode
->i_nlink
) {
3202 if (!S_ISREG(inode
->i_mode
)) {
3209 /* 1 for the orphan item deletion. */
3210 trans
= btrfs_start_transaction(root
, 1);
3211 if (IS_ERR(trans
)) {
3212 ret
= PTR_ERR(trans
);
3215 ret
= btrfs_orphan_add(trans
, inode
);
3216 btrfs_end_transaction(trans
, root
);
3220 ret
= btrfs_truncate(inode
);
3222 btrfs_orphan_del(NULL
, inode
);
3227 /* this will do delete_inode and everything for us */
3232 /* release the path since we're done with it */
3233 btrfs_release_path(path
);
3235 root
->orphan_cleanup_state
= ORPHAN_CLEANUP_DONE
;
3237 if (root
->orphan_block_rsv
)
3238 btrfs_block_rsv_release(root
, root
->orphan_block_rsv
,
3241 if (root
->orphan_block_rsv
|| root
->orphan_item_inserted
) {
3242 trans
= btrfs_join_transaction(root
);
3244 btrfs_end_transaction(trans
, root
);
3248 btrfs_debug(root
->fs_info
, "unlinked %d orphans", nr_unlink
);
3250 btrfs_debug(root
->fs_info
, "truncated %d orphans", nr_truncate
);
3254 btrfs_crit(root
->fs_info
,
3255 "could not do orphan cleanup %d", ret
);
3256 btrfs_free_path(path
);
3261 * very simple check to peek ahead in the leaf looking for xattrs. If we
3262 * don't find any xattrs, we know there can't be any acls.
3264 * slot is the slot the inode is in, objectid is the objectid of the inode
3266 static noinline
int acls_after_inode_item(struct extent_buffer
*leaf
,
3267 int slot
, u64 objectid
)
3269 u32 nritems
= btrfs_header_nritems(leaf
);
3270 struct btrfs_key found_key
;
3274 while (slot
< nritems
) {
3275 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
3277 /* we found a different objectid, there must not be acls */
3278 if (found_key
.objectid
!= objectid
)
3281 /* we found an xattr, assume we've got an acl */
3282 if (found_key
.type
== BTRFS_XATTR_ITEM_KEY
)
3286 * we found a key greater than an xattr key, there can't
3287 * be any acls later on
3289 if (found_key
.type
> BTRFS_XATTR_ITEM_KEY
)
3296 * it goes inode, inode backrefs, xattrs, extents,
3297 * so if there are a ton of hard links to an inode there can
3298 * be a lot of backrefs. Don't waste time searching too hard,
3299 * this is just an optimization
3304 /* we hit the end of the leaf before we found an xattr or
3305 * something larger than an xattr. We have to assume the inode
3312 * read an inode from the btree into the in-memory inode
3314 static void btrfs_read_locked_inode(struct inode
*inode
)
3316 struct btrfs_path
*path
;
3317 struct extent_buffer
*leaf
;
3318 struct btrfs_inode_item
*inode_item
;
3319 struct btrfs_timespec
*tspec
;
3320 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
3321 struct btrfs_key location
;
3325 bool filled
= false;
3327 ret
= btrfs_fill_inode(inode
, &rdev
);
3331 path
= btrfs_alloc_path();
3335 path
->leave_spinning
= 1;
3336 memcpy(&location
, &BTRFS_I(inode
)->location
, sizeof(location
));
3338 ret
= btrfs_lookup_inode(NULL
, root
, path
, &location
, 0);
3342 leaf
= path
->nodes
[0];
3347 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3348 struct btrfs_inode_item
);
3349 inode
->i_mode
= btrfs_inode_mode(leaf
, inode_item
);
3350 set_nlink(inode
, btrfs_inode_nlink(leaf
, inode_item
));
3351 i_uid_write(inode
, btrfs_inode_uid(leaf
, inode_item
));
3352 i_gid_write(inode
, btrfs_inode_gid(leaf
, inode_item
));
3353 btrfs_i_size_write(inode
, btrfs_inode_size(leaf
, inode_item
));
3355 tspec
= btrfs_inode_atime(inode_item
);
3356 inode
->i_atime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3357 inode
->i_atime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3359 tspec
= btrfs_inode_mtime(inode_item
);
3360 inode
->i_mtime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3361 inode
->i_mtime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3363 tspec
= btrfs_inode_ctime(inode_item
);
3364 inode
->i_ctime
.tv_sec
= btrfs_timespec_sec(leaf
, tspec
);
3365 inode
->i_ctime
.tv_nsec
= btrfs_timespec_nsec(leaf
, tspec
);
3367 inode_set_bytes(inode
, btrfs_inode_nbytes(leaf
, inode_item
));
3368 BTRFS_I(inode
)->generation
= btrfs_inode_generation(leaf
, inode_item
);
3369 BTRFS_I(inode
)->last_trans
= btrfs_inode_transid(leaf
, inode_item
);
3372 * If we were modified in the current generation and evicted from memory
3373 * and then re-read we need to do a full sync since we don't have any
3374 * idea about which extents were modified before we were evicted from
3377 if (BTRFS_I(inode
)->last_trans
== root
->fs_info
->generation
)
3378 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
3379 &BTRFS_I(inode
)->runtime_flags
);
3381 inode
->i_version
= btrfs_inode_sequence(leaf
, inode_item
);
3382 inode
->i_generation
= BTRFS_I(inode
)->generation
;
3384 rdev
= btrfs_inode_rdev(leaf
, inode_item
);
3386 BTRFS_I(inode
)->index_cnt
= (u64
)-1;
3387 BTRFS_I(inode
)->flags
= btrfs_inode_flags(leaf
, inode_item
);
3390 * try to precache a NULL acl entry for files that don't have
3391 * any xattrs or acls
3393 maybe_acls
= acls_after_inode_item(leaf
, path
->slots
[0],
3396 cache_no_acl(inode
);
3398 btrfs_free_path(path
);
3400 switch (inode
->i_mode
& S_IFMT
) {
3402 inode
->i_mapping
->a_ops
= &btrfs_aops
;
3403 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3404 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
3405 inode
->i_fop
= &btrfs_file_operations
;
3406 inode
->i_op
= &btrfs_file_inode_operations
;
3409 inode
->i_fop
= &btrfs_dir_file_operations
;
3410 if (root
== root
->fs_info
->tree_root
)
3411 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
3413 inode
->i_op
= &btrfs_dir_inode_operations
;
3416 inode
->i_op
= &btrfs_symlink_inode_operations
;
3417 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
3418 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
3421 inode
->i_op
= &btrfs_special_inode_operations
;
3422 init_special_inode(inode
, inode
->i_mode
, rdev
);
3426 btrfs_update_iflags(inode
);
3430 btrfs_free_path(path
);
3431 make_bad_inode(inode
);
3435 * given a leaf and an inode, copy the inode fields into the leaf
3437 static void fill_inode_item(struct btrfs_trans_handle
*trans
,
3438 struct extent_buffer
*leaf
,
3439 struct btrfs_inode_item
*item
,
3440 struct inode
*inode
)
3442 struct btrfs_map_token token
;
3444 btrfs_init_map_token(&token
);
3446 btrfs_set_token_inode_uid(leaf
, item
, i_uid_read(inode
), &token
);
3447 btrfs_set_token_inode_gid(leaf
, item
, i_gid_read(inode
), &token
);
3448 btrfs_set_token_inode_size(leaf
, item
, BTRFS_I(inode
)->disk_i_size
,
3450 btrfs_set_token_inode_mode(leaf
, item
, inode
->i_mode
, &token
);
3451 btrfs_set_token_inode_nlink(leaf
, item
, inode
->i_nlink
, &token
);
3453 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_atime(item
),
3454 inode
->i_atime
.tv_sec
, &token
);
3455 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_atime(item
),
3456 inode
->i_atime
.tv_nsec
, &token
);
3458 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_mtime(item
),
3459 inode
->i_mtime
.tv_sec
, &token
);
3460 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_mtime(item
),
3461 inode
->i_mtime
.tv_nsec
, &token
);
3463 btrfs_set_token_timespec_sec(leaf
, btrfs_inode_ctime(item
),
3464 inode
->i_ctime
.tv_sec
, &token
);
3465 btrfs_set_token_timespec_nsec(leaf
, btrfs_inode_ctime(item
),
3466 inode
->i_ctime
.tv_nsec
, &token
);
3468 btrfs_set_token_inode_nbytes(leaf
, item
, inode_get_bytes(inode
),
3470 btrfs_set_token_inode_generation(leaf
, item
, BTRFS_I(inode
)->generation
,
3472 btrfs_set_token_inode_sequence(leaf
, item
, inode
->i_version
, &token
);
3473 btrfs_set_token_inode_transid(leaf
, item
, trans
->transid
, &token
);
3474 btrfs_set_token_inode_rdev(leaf
, item
, inode
->i_rdev
, &token
);
3475 btrfs_set_token_inode_flags(leaf
, item
, BTRFS_I(inode
)->flags
, &token
);
3476 btrfs_set_token_inode_block_group(leaf
, item
, 0, &token
);
3480 * copy everything in the in-memory inode into the btree.
3482 static noinline
int btrfs_update_inode_item(struct btrfs_trans_handle
*trans
,
3483 struct btrfs_root
*root
, struct inode
*inode
)
3485 struct btrfs_inode_item
*inode_item
;
3486 struct btrfs_path
*path
;
3487 struct extent_buffer
*leaf
;
3490 path
= btrfs_alloc_path();
3494 path
->leave_spinning
= 1;
3495 ret
= btrfs_lookup_inode(trans
, root
, path
, &BTRFS_I(inode
)->location
,
3503 btrfs_unlock_up_safe(path
, 1);
3504 leaf
= path
->nodes
[0];
3505 inode_item
= btrfs_item_ptr(leaf
, path
->slots
[0],
3506 struct btrfs_inode_item
);
3508 fill_inode_item(trans
, leaf
, inode_item
, inode
);
3509 btrfs_mark_buffer_dirty(leaf
);
3510 btrfs_set_inode_last_trans(trans
, inode
);
3513 btrfs_free_path(path
);
3518 * copy everything in the in-memory inode into the btree.
3520 noinline
int btrfs_update_inode(struct btrfs_trans_handle
*trans
,
3521 struct btrfs_root
*root
, struct inode
*inode
)
3526 * If the inode is a free space inode, we can deadlock during commit
3527 * if we put it into the delayed code.
3529 * The data relocation inode should also be directly updated
3532 if (!btrfs_is_free_space_inode(inode
)
3533 && root
->root_key
.objectid
!= BTRFS_DATA_RELOC_TREE_OBJECTID
) {
3534 btrfs_update_root_times(trans
, root
);
3536 ret
= btrfs_delayed_update_inode(trans
, root
, inode
);
3538 btrfs_set_inode_last_trans(trans
, inode
);
3542 return btrfs_update_inode_item(trans
, root
, inode
);
3545 noinline
int btrfs_update_inode_fallback(struct btrfs_trans_handle
*trans
,
3546 struct btrfs_root
*root
,
3547 struct inode
*inode
)
3551 ret
= btrfs_update_inode(trans
, root
, inode
);
3553 return btrfs_update_inode_item(trans
, root
, inode
);
3558 * unlink helper that gets used here in inode.c and in the tree logging
3559 * recovery code. It remove a link in a directory with a given name, and
3560 * also drops the back refs in the inode to the directory
3562 static int __btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3563 struct btrfs_root
*root
,
3564 struct inode
*dir
, struct inode
*inode
,
3565 const char *name
, int name_len
)
3567 struct btrfs_path
*path
;
3569 struct extent_buffer
*leaf
;
3570 struct btrfs_dir_item
*di
;
3571 struct btrfs_key key
;
3573 u64 ino
= btrfs_ino(inode
);
3574 u64 dir_ino
= btrfs_ino(dir
);
3576 path
= btrfs_alloc_path();
3582 path
->leave_spinning
= 1;
3583 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3584 name
, name_len
, -1);
3593 leaf
= path
->nodes
[0];
3594 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3595 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3598 btrfs_release_path(path
);
3600 ret
= btrfs_del_inode_ref(trans
, root
, name
, name_len
, ino
,
3603 btrfs_info(root
->fs_info
,
3604 "failed to delete reference to %.*s, inode %llu parent %llu",
3606 (unsigned long long)ino
, (unsigned long long)dir_ino
);
3607 btrfs_abort_transaction(trans
, root
, ret
);
3611 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3613 btrfs_abort_transaction(trans
, root
, ret
);
3617 ret
= btrfs_del_inode_ref_in_log(trans
, root
, name
, name_len
,
3619 if (ret
!= 0 && ret
!= -ENOENT
) {
3620 btrfs_abort_transaction(trans
, root
, ret
);
3624 ret
= btrfs_del_dir_entries_in_log(trans
, root
, name
, name_len
,
3629 btrfs_abort_transaction(trans
, root
, ret
);
3631 btrfs_free_path(path
);
3635 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3636 inode_inc_iversion(inode
);
3637 inode_inc_iversion(dir
);
3638 inode
->i_ctime
= dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3639 ret
= btrfs_update_inode(trans
, root
, dir
);
3644 int btrfs_unlink_inode(struct btrfs_trans_handle
*trans
,
3645 struct btrfs_root
*root
,
3646 struct inode
*dir
, struct inode
*inode
,
3647 const char *name
, int name_len
)
3650 ret
= __btrfs_unlink_inode(trans
, root
, dir
, inode
, name
, name_len
);
3652 btrfs_drop_nlink(inode
);
3653 ret
= btrfs_update_inode(trans
, root
, inode
);
3659 /* helper to check if there is any shared block in the path */
3660 static int check_path_shared(struct btrfs_root
*root
,
3661 struct btrfs_path
*path
)
3663 struct extent_buffer
*eb
;
3667 for (level
= 0; level
< BTRFS_MAX_LEVEL
; level
++) {
3670 if (!path
->nodes
[level
])
3672 eb
= path
->nodes
[level
];
3673 if (!btrfs_block_can_be_shared(root
, eb
))
3675 ret
= btrfs_lookup_extent_info(NULL
, root
, eb
->start
, level
, 1,
3684 * helper to start transaction for unlink and rmdir.
3686 * unlink and rmdir are special in btrfs, they do not always free space.
3687 * so in enospc case, we should make sure they will free space before
3688 * allowing them to use the global metadata reservation.
3690 static struct btrfs_trans_handle
*__unlink_start_trans(struct inode
*dir
,
3691 struct dentry
*dentry
)
3693 struct btrfs_trans_handle
*trans
;
3694 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3695 struct btrfs_path
*path
;
3696 struct btrfs_dir_item
*di
;
3697 struct inode
*inode
= dentry
->d_inode
;
3702 u64 ino
= btrfs_ino(inode
);
3703 u64 dir_ino
= btrfs_ino(dir
);
3706 * 1 for the possible orphan item
3707 * 1 for the dir item
3708 * 1 for the dir index
3709 * 1 for the inode ref
3712 trans
= btrfs_start_transaction(root
, 5);
3713 if (!IS_ERR(trans
) || PTR_ERR(trans
) != -ENOSPC
)
3716 if (ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
3717 return ERR_PTR(-ENOSPC
);
3719 /* check if there is someone else holds reference */
3720 if (S_ISDIR(inode
->i_mode
) && atomic_read(&inode
->i_count
) > 1)
3721 return ERR_PTR(-ENOSPC
);
3723 if (atomic_read(&inode
->i_count
) > 2)
3724 return ERR_PTR(-ENOSPC
);
3726 if (xchg(&root
->fs_info
->enospc_unlink
, 1))
3727 return ERR_PTR(-ENOSPC
);
3729 path
= btrfs_alloc_path();
3731 root
->fs_info
->enospc_unlink
= 0;
3732 return ERR_PTR(-ENOMEM
);
3735 /* 1 for the orphan item */
3736 trans
= btrfs_start_transaction(root
, 1);
3737 if (IS_ERR(trans
)) {
3738 btrfs_free_path(path
);
3739 root
->fs_info
->enospc_unlink
= 0;
3743 path
->skip_locking
= 1;
3744 path
->search_commit_root
= 1;
3746 ret
= btrfs_lookup_inode(trans
, root
, path
,
3747 &BTRFS_I(dir
)->location
, 0);
3753 if (check_path_shared(root
, path
))
3758 btrfs_release_path(path
);
3760 ret
= btrfs_lookup_inode(trans
, root
, path
,
3761 &BTRFS_I(inode
)->location
, 0);
3767 if (check_path_shared(root
, path
))
3772 btrfs_release_path(path
);
3774 if (ret
== 0 && S_ISREG(inode
->i_mode
)) {
3775 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
3781 BUG_ON(ret
== 0); /* Corruption */
3782 if (check_path_shared(root
, path
))
3784 btrfs_release_path(path
);
3792 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3793 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3799 if (check_path_shared(root
, path
))
3805 btrfs_release_path(path
);
3807 ret
= btrfs_get_inode_ref_index(trans
, root
, path
, dentry
->d_name
.name
,
3808 dentry
->d_name
.len
, ino
, dir_ino
, 0,
3815 if (check_path_shared(root
, path
))
3818 btrfs_release_path(path
);
3821 * This is a commit root search, if we can lookup inode item and other
3822 * relative items in the commit root, it means the transaction of
3823 * dir/file creation has been committed, and the dir index item that we
3824 * delay to insert has also been inserted into the commit root. So
3825 * we needn't worry about the delayed insertion of the dir index item
3828 di
= btrfs_lookup_dir_index_item(trans
, root
, path
, dir_ino
, index
,
3829 dentry
->d_name
.name
, dentry
->d_name
.len
, 0);
3834 BUG_ON(ret
== -ENOENT
);
3835 if (check_path_shared(root
, path
))
3840 btrfs_free_path(path
);
3841 /* Migrate the orphan reservation over */
3843 err
= btrfs_block_rsv_migrate(trans
->block_rsv
,
3844 &root
->fs_info
->global_block_rsv
,
3845 trans
->bytes_reserved
);
3848 btrfs_end_transaction(trans
, root
);
3849 root
->fs_info
->enospc_unlink
= 0;
3850 return ERR_PTR(err
);
3853 trans
->block_rsv
= &root
->fs_info
->global_block_rsv
;
3857 static void __unlink_end_trans(struct btrfs_trans_handle
*trans
,
3858 struct btrfs_root
*root
)
3860 if (trans
->block_rsv
->type
== BTRFS_BLOCK_RSV_GLOBAL
) {
3861 btrfs_block_rsv_release(root
, trans
->block_rsv
,
3862 trans
->bytes_reserved
);
3863 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
3864 BUG_ON(!root
->fs_info
->enospc_unlink
);
3865 root
->fs_info
->enospc_unlink
= 0;
3867 btrfs_end_transaction(trans
, root
);
3870 static int btrfs_unlink(struct inode
*dir
, struct dentry
*dentry
)
3872 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3873 struct btrfs_trans_handle
*trans
;
3874 struct inode
*inode
= dentry
->d_inode
;
3877 trans
= __unlink_start_trans(dir
, dentry
);
3879 return PTR_ERR(trans
);
3881 btrfs_record_unlink_dir(trans
, dir
, dentry
->d_inode
, 0);
3883 ret
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
3884 dentry
->d_name
.name
, dentry
->d_name
.len
);
3888 if (inode
->i_nlink
== 0) {
3889 ret
= btrfs_orphan_add(trans
, inode
);
3895 __unlink_end_trans(trans
, root
);
3896 btrfs_btree_balance_dirty(root
);
3900 int btrfs_unlink_subvol(struct btrfs_trans_handle
*trans
,
3901 struct btrfs_root
*root
,
3902 struct inode
*dir
, u64 objectid
,
3903 const char *name
, int name_len
)
3905 struct btrfs_path
*path
;
3906 struct extent_buffer
*leaf
;
3907 struct btrfs_dir_item
*di
;
3908 struct btrfs_key key
;
3911 u64 dir_ino
= btrfs_ino(dir
);
3913 path
= btrfs_alloc_path();
3917 di
= btrfs_lookup_dir_item(trans
, root
, path
, dir_ino
,
3918 name
, name_len
, -1);
3919 if (IS_ERR_OR_NULL(di
)) {
3927 leaf
= path
->nodes
[0];
3928 btrfs_dir_item_key_to_cpu(leaf
, di
, &key
);
3929 WARN_ON(key
.type
!= BTRFS_ROOT_ITEM_KEY
|| key
.objectid
!= objectid
);
3930 ret
= btrfs_delete_one_dir_name(trans
, root
, path
, di
);
3932 btrfs_abort_transaction(trans
, root
, ret
);
3935 btrfs_release_path(path
);
3937 ret
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
3938 objectid
, root
->root_key
.objectid
,
3939 dir_ino
, &index
, name
, name_len
);
3941 if (ret
!= -ENOENT
) {
3942 btrfs_abort_transaction(trans
, root
, ret
);
3945 di
= btrfs_search_dir_index_item(root
, path
, dir_ino
,
3947 if (IS_ERR_OR_NULL(di
)) {
3952 btrfs_abort_transaction(trans
, root
, ret
);
3956 leaf
= path
->nodes
[0];
3957 btrfs_item_key_to_cpu(leaf
, &key
, path
->slots
[0]);
3958 btrfs_release_path(path
);
3961 btrfs_release_path(path
);
3963 ret
= btrfs_delete_delayed_dir_index(trans
, root
, dir
, index
);
3965 btrfs_abort_transaction(trans
, root
, ret
);
3969 btrfs_i_size_write(dir
, dir
->i_size
- name_len
* 2);
3970 inode_inc_iversion(dir
);
3971 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
3972 ret
= btrfs_update_inode_fallback(trans
, root
, dir
);
3974 btrfs_abort_transaction(trans
, root
, ret
);
3976 btrfs_free_path(path
);
3980 static int btrfs_rmdir(struct inode
*dir
, struct dentry
*dentry
)
3982 struct inode
*inode
= dentry
->d_inode
;
3984 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
3985 struct btrfs_trans_handle
*trans
;
3987 if (inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
3989 if (btrfs_ino(inode
) == BTRFS_FIRST_FREE_OBJECTID
)
3992 trans
= __unlink_start_trans(dir
, dentry
);
3994 return PTR_ERR(trans
);
3996 if (unlikely(btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
3997 err
= btrfs_unlink_subvol(trans
, root
, dir
,
3998 BTRFS_I(inode
)->location
.objectid
,
3999 dentry
->d_name
.name
,
4000 dentry
->d_name
.len
);
4004 err
= btrfs_orphan_add(trans
, inode
);
4008 /* now the directory is empty */
4009 err
= btrfs_unlink_inode(trans
, root
, dir
, dentry
->d_inode
,
4010 dentry
->d_name
.name
, dentry
->d_name
.len
);
4012 btrfs_i_size_write(inode
, 0);
4014 __unlink_end_trans(trans
, root
);
4015 btrfs_btree_balance_dirty(root
);
4021 * this can truncate away extent items, csum items and directory items.
4022 * It starts at a high offset and removes keys until it can't find
4023 * any higher than new_size
4025 * csum items that cross the new i_size are truncated to the new size
4028 * min_type is the minimum key type to truncate down to. If set to 0, this
4029 * will kill all the items on this inode, including the INODE_ITEM_KEY.
4031 int btrfs_truncate_inode_items(struct btrfs_trans_handle
*trans
,
4032 struct btrfs_root
*root
,
4033 struct inode
*inode
,
4034 u64 new_size
, u32 min_type
)
4036 struct btrfs_path
*path
;
4037 struct extent_buffer
*leaf
;
4038 struct btrfs_file_extent_item
*fi
;
4039 struct btrfs_key key
;
4040 struct btrfs_key found_key
;
4041 u64 extent_start
= 0;
4042 u64 extent_num_bytes
= 0;
4043 u64 extent_offset
= 0;
4045 u32 found_type
= (u8
)-1;
4048 int pending_del_nr
= 0;
4049 int pending_del_slot
= 0;
4050 int extent_type
= -1;
4053 u64 ino
= btrfs_ino(inode
);
4055 BUG_ON(new_size
> 0 && min_type
!= BTRFS_EXTENT_DATA_KEY
);
4057 path
= btrfs_alloc_path();
4063 * We want to drop from the next block forward in case this new size is
4064 * not block aligned since we will be keeping the last block of the
4065 * extent just the way it is.
4067 if (root
->ref_cows
|| root
== root
->fs_info
->tree_root
)
4068 btrfs_drop_extent_cache(inode
, ALIGN(new_size
,
4069 root
->sectorsize
), (u64
)-1, 0);
4072 * This function is also used to drop the items in the log tree before
4073 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4074 * it is used to drop the loged items. So we shouldn't kill the delayed
4077 if (min_type
== 0 && root
== BTRFS_I(inode
)->root
)
4078 btrfs_kill_delayed_inode_items(inode
);
4081 key
.offset
= (u64
)-1;
4085 path
->leave_spinning
= 1;
4086 ret
= btrfs_search_slot(trans
, root
, &key
, path
, -1, 1);
4093 /* there are no items in the tree for us to truncate, we're
4096 if (path
->slots
[0] == 0)
4103 leaf
= path
->nodes
[0];
4104 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
4105 found_type
= btrfs_key_type(&found_key
);
4107 if (found_key
.objectid
!= ino
)
4110 if (found_type
< min_type
)
4113 item_end
= found_key
.offset
;
4114 if (found_type
== BTRFS_EXTENT_DATA_KEY
) {
4115 fi
= btrfs_item_ptr(leaf
, path
->slots
[0],
4116 struct btrfs_file_extent_item
);
4117 extent_type
= btrfs_file_extent_type(leaf
, fi
);
4118 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4120 btrfs_file_extent_num_bytes(leaf
, fi
);
4121 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4122 item_end
+= btrfs_file_extent_inline_len(leaf
,
4127 if (found_type
> min_type
) {
4130 if (item_end
< new_size
)
4132 if (found_key
.offset
>= new_size
)
4138 /* FIXME, shrink the extent if the ref count is only 1 */
4139 if (found_type
!= BTRFS_EXTENT_DATA_KEY
)
4142 if (extent_type
!= BTRFS_FILE_EXTENT_INLINE
) {
4144 extent_start
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
4146 u64 orig_num_bytes
=
4147 btrfs_file_extent_num_bytes(leaf
, fi
);
4148 extent_num_bytes
= ALIGN(new_size
-
4151 btrfs_set_file_extent_num_bytes(leaf
, fi
,
4153 num_dec
= (orig_num_bytes
-
4155 if (root
->ref_cows
&& extent_start
!= 0)
4156 inode_sub_bytes(inode
, num_dec
);
4157 btrfs_mark_buffer_dirty(leaf
);
4160 btrfs_file_extent_disk_num_bytes(leaf
,
4162 extent_offset
= found_key
.offset
-
4163 btrfs_file_extent_offset(leaf
, fi
);
4165 /* FIXME blocksize != 4096 */
4166 num_dec
= btrfs_file_extent_num_bytes(leaf
, fi
);
4167 if (extent_start
!= 0) {
4170 inode_sub_bytes(inode
, num_dec
);
4173 } else if (extent_type
== BTRFS_FILE_EXTENT_INLINE
) {
4175 * we can't truncate inline items that have had
4179 btrfs_file_extent_compression(leaf
, fi
) == 0 &&
4180 btrfs_file_extent_encryption(leaf
, fi
) == 0 &&
4181 btrfs_file_extent_other_encoding(leaf
, fi
) == 0) {
4182 u32 size
= new_size
- found_key
.offset
;
4184 if (root
->ref_cows
) {
4185 inode_sub_bytes(inode
, item_end
+ 1 -
4189 btrfs_file_extent_calc_inline_size(size
);
4190 btrfs_truncate_item(root
, path
, size
, 1);
4191 } else if (root
->ref_cows
) {
4192 inode_sub_bytes(inode
, item_end
+ 1 -
4198 if (!pending_del_nr
) {
4199 /* no pending yet, add ourselves */
4200 pending_del_slot
= path
->slots
[0];
4202 } else if (pending_del_nr
&&
4203 path
->slots
[0] + 1 == pending_del_slot
) {
4204 /* hop on the pending chunk */
4206 pending_del_slot
= path
->slots
[0];
4213 if (found_extent
&& (root
->ref_cows
||
4214 root
== root
->fs_info
->tree_root
)) {
4215 btrfs_set_path_blocking(path
);
4216 ret
= btrfs_free_extent(trans
, root
, extent_start
,
4217 extent_num_bytes
, 0,
4218 btrfs_header_owner(leaf
),
4219 ino
, extent_offset
, 0);
4223 if (found_type
== BTRFS_INODE_ITEM_KEY
)
4226 if (path
->slots
[0] == 0 ||
4227 path
->slots
[0] != pending_del_slot
) {
4228 if (pending_del_nr
) {
4229 ret
= btrfs_del_items(trans
, root
, path
,
4233 btrfs_abort_transaction(trans
,
4239 btrfs_release_path(path
);
4246 if (pending_del_nr
) {
4247 ret
= btrfs_del_items(trans
, root
, path
, pending_del_slot
,
4250 btrfs_abort_transaction(trans
, root
, ret
);
4253 btrfs_free_path(path
);
4258 * btrfs_truncate_page - read, zero a chunk and write a page
4259 * @inode - inode that we're zeroing
4260 * @from - the offset to start zeroing
4261 * @len - the length to zero, 0 to zero the entire range respective to the
4263 * @front - zero up to the offset instead of from the offset on
4265 * This will find the page for the "from" offset and cow the page and zero the
4266 * part we want to zero. This is used with truncate and hole punching.
4268 int btrfs_truncate_page(struct inode
*inode
, loff_t from
, loff_t len
,
4271 struct address_space
*mapping
= inode
->i_mapping
;
4272 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4273 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4274 struct btrfs_ordered_extent
*ordered
;
4275 struct extent_state
*cached_state
= NULL
;
4277 u32 blocksize
= root
->sectorsize
;
4278 pgoff_t index
= from
>> PAGE_CACHE_SHIFT
;
4279 unsigned offset
= from
& (PAGE_CACHE_SIZE
-1);
4281 gfp_t mask
= btrfs_alloc_write_mask(mapping
);
4286 if ((offset
& (blocksize
- 1)) == 0 &&
4287 (!len
|| ((len
& (blocksize
- 1)) == 0)))
4289 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
4294 page
= find_or_create_page(mapping
, index
, mask
);
4296 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4301 page_start
= page_offset(page
);
4302 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
4304 if (!PageUptodate(page
)) {
4305 ret
= btrfs_readpage(NULL
, page
);
4307 if (page
->mapping
!= mapping
) {
4309 page_cache_release(page
);
4312 if (!PageUptodate(page
)) {
4317 wait_on_page_writeback(page
);
4319 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
4320 set_page_extent_mapped(page
);
4322 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
4324 unlock_extent_cached(io_tree
, page_start
, page_end
,
4325 &cached_state
, GFP_NOFS
);
4327 page_cache_release(page
);
4328 btrfs_start_ordered_extent(inode
, ordered
, 1);
4329 btrfs_put_ordered_extent(ordered
);
4333 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
4334 EXTENT_DIRTY
| EXTENT_DELALLOC
|
4335 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
4336 0, 0, &cached_state
, GFP_NOFS
);
4338 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
4341 unlock_extent_cached(io_tree
, page_start
, page_end
,
4342 &cached_state
, GFP_NOFS
);
4346 if (offset
!= PAGE_CACHE_SIZE
) {
4348 len
= PAGE_CACHE_SIZE
- offset
;
4351 memset(kaddr
, 0, offset
);
4353 memset(kaddr
+ offset
, 0, len
);
4354 flush_dcache_page(page
);
4357 ClearPageChecked(page
);
4358 set_page_dirty(page
);
4359 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
,
4364 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
4366 page_cache_release(page
);
4372 * This function puts in dummy file extents for the area we're creating a hole
4373 * for. So if we are truncating this file to a larger size we need to insert
4374 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4375 * the range between oldsize and size
4377 int btrfs_cont_expand(struct inode
*inode
, loff_t oldsize
, loff_t size
)
4379 struct btrfs_trans_handle
*trans
;
4380 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4381 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
4382 struct extent_map
*em
= NULL
;
4383 struct extent_state
*cached_state
= NULL
;
4384 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
4385 u64 hole_start
= ALIGN(oldsize
, root
->sectorsize
);
4386 u64 block_end
= ALIGN(size
, root
->sectorsize
);
4392 if (size
<= hole_start
)
4396 struct btrfs_ordered_extent
*ordered
;
4397 btrfs_wait_ordered_range(inode
, hole_start
,
4398 block_end
- hole_start
);
4399 lock_extent_bits(io_tree
, hole_start
, block_end
- 1, 0,
4401 ordered
= btrfs_lookup_ordered_extent(inode
, hole_start
);
4404 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1,
4405 &cached_state
, GFP_NOFS
);
4406 btrfs_put_ordered_extent(ordered
);
4409 cur_offset
= hole_start
;
4411 em
= btrfs_get_extent(inode
, NULL
, 0, cur_offset
,
4412 block_end
- cur_offset
, 0);
4418 last_byte
= min(extent_map_end(em
), block_end
);
4419 last_byte
= ALIGN(last_byte
, root
->sectorsize
);
4420 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
)) {
4421 struct extent_map
*hole_em
;
4422 hole_size
= last_byte
- cur_offset
;
4424 trans
= btrfs_start_transaction(root
, 3);
4425 if (IS_ERR(trans
)) {
4426 err
= PTR_ERR(trans
);
4430 err
= btrfs_drop_extents(trans
, root
, inode
,
4432 cur_offset
+ hole_size
, 1);
4434 btrfs_abort_transaction(trans
, root
, err
);
4435 btrfs_end_transaction(trans
, root
);
4439 err
= btrfs_insert_file_extent(trans
, root
,
4440 btrfs_ino(inode
), cur_offset
, 0,
4441 0, hole_size
, 0, hole_size
,
4444 btrfs_abort_transaction(trans
, root
, err
);
4445 btrfs_end_transaction(trans
, root
);
4449 btrfs_drop_extent_cache(inode
, cur_offset
,
4450 cur_offset
+ hole_size
- 1, 0);
4451 hole_em
= alloc_extent_map();
4453 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
4454 &BTRFS_I(inode
)->runtime_flags
);
4457 hole_em
->start
= cur_offset
;
4458 hole_em
->len
= hole_size
;
4459 hole_em
->orig_start
= cur_offset
;
4461 hole_em
->block_start
= EXTENT_MAP_HOLE
;
4462 hole_em
->block_len
= 0;
4463 hole_em
->orig_block_len
= 0;
4464 hole_em
->ram_bytes
= hole_size
;
4465 hole_em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
4466 hole_em
->compress_type
= BTRFS_COMPRESS_NONE
;
4467 hole_em
->generation
= trans
->transid
;
4470 write_lock(&em_tree
->lock
);
4471 err
= add_extent_mapping(em_tree
, hole_em
, 1);
4472 write_unlock(&em_tree
->lock
);
4475 btrfs_drop_extent_cache(inode
, cur_offset
,
4479 free_extent_map(hole_em
);
4481 btrfs_update_inode(trans
, root
, inode
);
4482 btrfs_end_transaction(trans
, root
);
4484 free_extent_map(em
);
4486 cur_offset
= last_byte
;
4487 if (cur_offset
>= block_end
)
4491 free_extent_map(em
);
4492 unlock_extent_cached(io_tree
, hole_start
, block_end
- 1, &cached_state
,
4497 static int btrfs_setsize(struct inode
*inode
, struct iattr
*attr
)
4499 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4500 struct btrfs_trans_handle
*trans
;
4501 loff_t oldsize
= i_size_read(inode
);
4502 loff_t newsize
= attr
->ia_size
;
4503 int mask
= attr
->ia_valid
;
4506 if (newsize
== oldsize
)
4510 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4511 * special case where we need to update the times despite not having
4512 * these flags set. For all other operations the VFS set these flags
4513 * explicitly if it wants a timestamp update.
4515 if (newsize
!= oldsize
&& (!(mask
& (ATTR_CTIME
| ATTR_MTIME
))))
4516 inode
->i_ctime
= inode
->i_mtime
= current_fs_time(inode
->i_sb
);
4518 if (newsize
> oldsize
) {
4519 truncate_pagecache(inode
, oldsize
, newsize
);
4520 ret
= btrfs_cont_expand(inode
, oldsize
, newsize
);
4524 trans
= btrfs_start_transaction(root
, 1);
4526 return PTR_ERR(trans
);
4528 i_size_write(inode
, newsize
);
4529 btrfs_ordered_update_i_size(inode
, i_size_read(inode
), NULL
);
4530 ret
= btrfs_update_inode(trans
, root
, inode
);
4531 btrfs_end_transaction(trans
, root
);
4535 * We're truncating a file that used to have good data down to
4536 * zero. Make sure it gets into the ordered flush list so that
4537 * any new writes get down to disk quickly.
4540 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
4541 &BTRFS_I(inode
)->runtime_flags
);
4544 * 1 for the orphan item we're going to add
4545 * 1 for the orphan item deletion.
4547 trans
= btrfs_start_transaction(root
, 2);
4549 return PTR_ERR(trans
);
4552 * We need to do this in case we fail at _any_ point during the
4553 * actual truncate. Once we do the truncate_setsize we could
4554 * invalidate pages which forces any outstanding ordered io to
4555 * be instantly completed which will give us extents that need
4556 * to be truncated. If we fail to get an orphan inode down we
4557 * could have left over extents that were never meant to live,
4558 * so we need to garuntee from this point on that everything
4559 * will be consistent.
4561 ret
= btrfs_orphan_add(trans
, inode
);
4562 btrfs_end_transaction(trans
, root
);
4566 /* we don't support swapfiles, so vmtruncate shouldn't fail */
4567 truncate_setsize(inode
, newsize
);
4569 /* Disable nonlocked read DIO to avoid the end less truncate */
4570 btrfs_inode_block_unlocked_dio(inode
);
4571 inode_dio_wait(inode
);
4572 btrfs_inode_resume_unlocked_dio(inode
);
4574 ret
= btrfs_truncate(inode
);
4575 if (ret
&& inode
->i_nlink
)
4576 btrfs_orphan_del(NULL
, inode
);
4582 static int btrfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
4584 struct inode
*inode
= dentry
->d_inode
;
4585 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4588 if (btrfs_root_readonly(root
))
4591 err
= inode_change_ok(inode
, attr
);
4595 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
4596 err
= btrfs_setsize(inode
, attr
);
4601 if (attr
->ia_valid
) {
4602 setattr_copy(inode
, attr
);
4603 inode_inc_iversion(inode
);
4604 err
= btrfs_dirty_inode(inode
);
4606 if (!err
&& attr
->ia_valid
& ATTR_MODE
)
4607 err
= btrfs_acl_chmod(inode
);
4613 void btrfs_evict_inode(struct inode
*inode
)
4615 struct btrfs_trans_handle
*trans
;
4616 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4617 struct btrfs_block_rsv
*rsv
, *global_rsv
;
4618 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
4621 trace_btrfs_inode_evict(inode
);
4623 truncate_inode_pages(&inode
->i_data
, 0);
4624 if (inode
->i_nlink
&& (btrfs_root_refs(&root
->root_item
) != 0 ||
4625 btrfs_is_free_space_inode(inode
)))
4628 if (is_bad_inode(inode
)) {
4629 btrfs_orphan_del(NULL
, inode
);
4632 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
4633 btrfs_wait_ordered_range(inode
, 0, (u64
)-1);
4635 if (root
->fs_info
->log_root_recovering
) {
4636 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
4637 &BTRFS_I(inode
)->runtime_flags
));
4641 if (inode
->i_nlink
> 0) {
4642 BUG_ON(btrfs_root_refs(&root
->root_item
) != 0);
4646 ret
= btrfs_commit_inode_delayed_inode(inode
);
4648 btrfs_orphan_del(NULL
, inode
);
4652 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
4654 btrfs_orphan_del(NULL
, inode
);
4657 rsv
->size
= min_size
;
4659 global_rsv
= &root
->fs_info
->global_block_rsv
;
4661 btrfs_i_size_write(inode
, 0);
4664 * This is a bit simpler than btrfs_truncate since we've already
4665 * reserved our space for our orphan item in the unlink, so we just
4666 * need to reserve some slack space in case we add bytes and update
4667 * inode item when doing the truncate.
4670 ret
= btrfs_block_rsv_refill(root
, rsv
, min_size
,
4671 BTRFS_RESERVE_FLUSH_LIMIT
);
4674 * Try and steal from the global reserve since we will
4675 * likely not use this space anyway, we want to try as
4676 * hard as possible to get this to work.
4679 ret
= btrfs_block_rsv_migrate(global_rsv
, rsv
, min_size
);
4682 btrfs_warn(root
->fs_info
,
4683 "Could not get space for a delete, will truncate on mount %d",
4685 btrfs_orphan_del(NULL
, inode
);
4686 btrfs_free_block_rsv(root
, rsv
);
4690 trans
= btrfs_join_transaction(root
);
4691 if (IS_ERR(trans
)) {
4692 btrfs_orphan_del(NULL
, inode
);
4693 btrfs_free_block_rsv(root
, rsv
);
4697 trans
->block_rsv
= rsv
;
4699 ret
= btrfs_truncate_inode_items(trans
, root
, inode
, 0, 0);
4703 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4704 btrfs_end_transaction(trans
, root
);
4706 btrfs_btree_balance_dirty(root
);
4709 btrfs_free_block_rsv(root
, rsv
);
4712 trans
->block_rsv
= root
->orphan_block_rsv
;
4713 ret
= btrfs_orphan_del(trans
, inode
);
4717 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
4718 if (!(root
== root
->fs_info
->tree_root
||
4719 root
->root_key
.objectid
== BTRFS_TREE_RELOC_OBJECTID
))
4720 btrfs_return_ino(root
, btrfs_ino(inode
));
4722 btrfs_end_transaction(trans
, root
);
4723 btrfs_btree_balance_dirty(root
);
4725 btrfs_remove_delayed_node(inode
);
4731 * this returns the key found in the dir entry in the location pointer.
4732 * If no dir entries were found, location->objectid is 0.
4734 static int btrfs_inode_by_name(struct inode
*dir
, struct dentry
*dentry
,
4735 struct btrfs_key
*location
)
4737 const char *name
= dentry
->d_name
.name
;
4738 int namelen
= dentry
->d_name
.len
;
4739 struct btrfs_dir_item
*di
;
4740 struct btrfs_path
*path
;
4741 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
4744 path
= btrfs_alloc_path();
4748 di
= btrfs_lookup_dir_item(NULL
, root
, path
, btrfs_ino(dir
), name
,
4753 if (IS_ERR_OR_NULL(di
))
4756 btrfs_dir_item_key_to_cpu(path
->nodes
[0], di
, location
);
4758 btrfs_free_path(path
);
4761 location
->objectid
= 0;
4766 * when we hit a tree root in a directory, the btrfs part of the inode
4767 * needs to be changed to reflect the root directory of the tree root. This
4768 * is kind of like crossing a mount point.
4770 static int fixup_tree_root_location(struct btrfs_root
*root
,
4772 struct dentry
*dentry
,
4773 struct btrfs_key
*location
,
4774 struct btrfs_root
**sub_root
)
4776 struct btrfs_path
*path
;
4777 struct btrfs_root
*new_root
;
4778 struct btrfs_root_ref
*ref
;
4779 struct extent_buffer
*leaf
;
4783 path
= btrfs_alloc_path();
4790 ret
= btrfs_find_root_ref(root
->fs_info
->tree_root
, path
,
4791 BTRFS_I(dir
)->root
->root_key
.objectid
,
4792 location
->objectid
);
4799 leaf
= path
->nodes
[0];
4800 ref
= btrfs_item_ptr(leaf
, path
->slots
[0], struct btrfs_root_ref
);
4801 if (btrfs_root_ref_dirid(leaf
, ref
) != btrfs_ino(dir
) ||
4802 btrfs_root_ref_name_len(leaf
, ref
) != dentry
->d_name
.len
)
4805 ret
= memcmp_extent_buffer(leaf
, dentry
->d_name
.name
,
4806 (unsigned long)(ref
+ 1),
4807 dentry
->d_name
.len
);
4811 btrfs_release_path(path
);
4813 new_root
= btrfs_read_fs_root_no_name(root
->fs_info
, location
);
4814 if (IS_ERR(new_root
)) {
4815 err
= PTR_ERR(new_root
);
4819 *sub_root
= new_root
;
4820 location
->objectid
= btrfs_root_dirid(&new_root
->root_item
);
4821 location
->type
= BTRFS_INODE_ITEM_KEY
;
4822 location
->offset
= 0;
4825 btrfs_free_path(path
);
4829 static void inode_tree_add(struct inode
*inode
)
4831 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4832 struct btrfs_inode
*entry
;
4834 struct rb_node
*parent
;
4835 u64 ino
= btrfs_ino(inode
);
4837 if (inode_unhashed(inode
))
4841 spin_lock(&root
->inode_lock
);
4842 p
= &root
->inode_tree
.rb_node
;
4845 entry
= rb_entry(parent
, struct btrfs_inode
, rb_node
);
4847 if (ino
< btrfs_ino(&entry
->vfs_inode
))
4848 p
= &parent
->rb_left
;
4849 else if (ino
> btrfs_ino(&entry
->vfs_inode
))
4850 p
= &parent
->rb_right
;
4852 WARN_ON(!(entry
->vfs_inode
.i_state
&
4853 (I_WILL_FREE
| I_FREEING
)));
4854 rb_erase(parent
, &root
->inode_tree
);
4855 RB_CLEAR_NODE(parent
);
4856 spin_unlock(&root
->inode_lock
);
4860 rb_link_node(&BTRFS_I(inode
)->rb_node
, parent
, p
);
4861 rb_insert_color(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4862 spin_unlock(&root
->inode_lock
);
4865 static void inode_tree_del(struct inode
*inode
)
4867 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
4870 spin_lock(&root
->inode_lock
);
4871 if (!RB_EMPTY_NODE(&BTRFS_I(inode
)->rb_node
)) {
4872 rb_erase(&BTRFS_I(inode
)->rb_node
, &root
->inode_tree
);
4873 RB_CLEAR_NODE(&BTRFS_I(inode
)->rb_node
);
4874 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4876 spin_unlock(&root
->inode_lock
);
4879 * Free space cache has inodes in the tree root, but the tree root has a
4880 * root_refs of 0, so this could end up dropping the tree root as a
4881 * snapshot, so we need the extra !root->fs_info->tree_root check to
4882 * make sure we don't drop it.
4884 if (empty
&& btrfs_root_refs(&root
->root_item
) == 0 &&
4885 root
!= root
->fs_info
->tree_root
) {
4886 synchronize_srcu(&root
->fs_info
->subvol_srcu
);
4887 spin_lock(&root
->inode_lock
);
4888 empty
= RB_EMPTY_ROOT(&root
->inode_tree
);
4889 spin_unlock(&root
->inode_lock
);
4891 btrfs_add_dead_root(root
);
4895 void btrfs_invalidate_inodes(struct btrfs_root
*root
)
4897 struct rb_node
*node
;
4898 struct rb_node
*prev
;
4899 struct btrfs_inode
*entry
;
4900 struct inode
*inode
;
4903 WARN_ON(btrfs_root_refs(&root
->root_item
) != 0);
4905 spin_lock(&root
->inode_lock
);
4907 node
= root
->inode_tree
.rb_node
;
4911 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4913 if (objectid
< btrfs_ino(&entry
->vfs_inode
))
4914 node
= node
->rb_left
;
4915 else if (objectid
> btrfs_ino(&entry
->vfs_inode
))
4916 node
= node
->rb_right
;
4922 entry
= rb_entry(prev
, struct btrfs_inode
, rb_node
);
4923 if (objectid
<= btrfs_ino(&entry
->vfs_inode
)) {
4927 prev
= rb_next(prev
);
4931 entry
= rb_entry(node
, struct btrfs_inode
, rb_node
);
4932 objectid
= btrfs_ino(&entry
->vfs_inode
) + 1;
4933 inode
= igrab(&entry
->vfs_inode
);
4935 spin_unlock(&root
->inode_lock
);
4936 if (atomic_read(&inode
->i_count
) > 1)
4937 d_prune_aliases(inode
);
4939 * btrfs_drop_inode will have it removed from
4940 * the inode cache when its usage count
4945 spin_lock(&root
->inode_lock
);
4949 if (cond_resched_lock(&root
->inode_lock
))
4952 node
= rb_next(node
);
4954 spin_unlock(&root
->inode_lock
);
4957 static int btrfs_init_locked_inode(struct inode
*inode
, void *p
)
4959 struct btrfs_iget_args
*args
= p
;
4960 inode
->i_ino
= args
->ino
;
4961 BTRFS_I(inode
)->root
= args
->root
;
4965 static int btrfs_find_actor(struct inode
*inode
, void *opaque
)
4967 struct btrfs_iget_args
*args
= opaque
;
4968 return args
->ino
== btrfs_ino(inode
) &&
4969 args
->root
== BTRFS_I(inode
)->root
;
4972 static struct inode
*btrfs_iget_locked(struct super_block
*s
,
4974 struct btrfs_root
*root
)
4976 struct inode
*inode
;
4977 struct btrfs_iget_args args
;
4978 args
.ino
= objectid
;
4981 inode
= iget5_locked(s
, objectid
, btrfs_find_actor
,
4982 btrfs_init_locked_inode
,
4987 /* Get an inode object given its location and corresponding root.
4988 * Returns in *is_new if the inode was read from disk
4990 struct inode
*btrfs_iget(struct super_block
*s
, struct btrfs_key
*location
,
4991 struct btrfs_root
*root
, int *new)
4993 struct inode
*inode
;
4995 inode
= btrfs_iget_locked(s
, location
->objectid
, root
);
4997 return ERR_PTR(-ENOMEM
);
4999 if (inode
->i_state
& I_NEW
) {
5000 BTRFS_I(inode
)->root
= root
;
5001 memcpy(&BTRFS_I(inode
)->location
, location
, sizeof(*location
));
5002 btrfs_read_locked_inode(inode
);
5003 if (!is_bad_inode(inode
)) {
5004 inode_tree_add(inode
);
5005 unlock_new_inode(inode
);
5009 unlock_new_inode(inode
);
5011 inode
= ERR_PTR(-ESTALE
);
5018 static struct inode
*new_simple_dir(struct super_block
*s
,
5019 struct btrfs_key
*key
,
5020 struct btrfs_root
*root
)
5022 struct inode
*inode
= new_inode(s
);
5025 return ERR_PTR(-ENOMEM
);
5027 BTRFS_I(inode
)->root
= root
;
5028 memcpy(&BTRFS_I(inode
)->location
, key
, sizeof(*key
));
5029 set_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
);
5031 inode
->i_ino
= BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
;
5032 inode
->i_op
= &btrfs_dir_ro_inode_operations
;
5033 inode
->i_fop
= &simple_dir_operations
;
5034 inode
->i_mode
= S_IFDIR
| S_IRUGO
| S_IWUSR
| S_IXUGO
;
5035 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5040 struct inode
*btrfs_lookup_dentry(struct inode
*dir
, struct dentry
*dentry
)
5042 struct inode
*inode
;
5043 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5044 struct btrfs_root
*sub_root
= root
;
5045 struct btrfs_key location
;
5049 if (dentry
->d_name
.len
> BTRFS_NAME_LEN
)
5050 return ERR_PTR(-ENAMETOOLONG
);
5052 ret
= btrfs_inode_by_name(dir
, dentry
, &location
);
5054 return ERR_PTR(ret
);
5056 if (location
.objectid
== 0)
5059 if (location
.type
== BTRFS_INODE_ITEM_KEY
) {
5060 inode
= btrfs_iget(dir
->i_sb
, &location
, root
, NULL
);
5064 BUG_ON(location
.type
!= BTRFS_ROOT_ITEM_KEY
);
5066 index
= srcu_read_lock(&root
->fs_info
->subvol_srcu
);
5067 ret
= fixup_tree_root_location(root
, dir
, dentry
,
5068 &location
, &sub_root
);
5071 inode
= ERR_PTR(ret
);
5073 inode
= new_simple_dir(dir
->i_sb
, &location
, sub_root
);
5075 inode
= btrfs_iget(dir
->i_sb
, &location
, sub_root
, NULL
);
5077 srcu_read_unlock(&root
->fs_info
->subvol_srcu
, index
);
5079 if (!IS_ERR(inode
) && root
!= sub_root
) {
5080 down_read(&root
->fs_info
->cleanup_work_sem
);
5081 if (!(inode
->i_sb
->s_flags
& MS_RDONLY
))
5082 ret
= btrfs_orphan_cleanup(sub_root
);
5083 up_read(&root
->fs_info
->cleanup_work_sem
);
5085 inode
= ERR_PTR(ret
);
5091 static int btrfs_dentry_delete(const struct dentry
*dentry
)
5093 struct btrfs_root
*root
;
5094 struct inode
*inode
= dentry
->d_inode
;
5096 if (!inode
&& !IS_ROOT(dentry
))
5097 inode
= dentry
->d_parent
->d_inode
;
5100 root
= BTRFS_I(inode
)->root
;
5101 if (btrfs_root_refs(&root
->root_item
) == 0)
5104 if (btrfs_ino(inode
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
5110 static void btrfs_dentry_release(struct dentry
*dentry
)
5112 if (dentry
->d_fsdata
)
5113 kfree(dentry
->d_fsdata
);
5116 static struct dentry
*btrfs_lookup(struct inode
*dir
, struct dentry
*dentry
,
5121 ret
= d_splice_alias(btrfs_lookup_dentry(dir
, dentry
), dentry
);
5125 unsigned char btrfs_filetype_table
[] = {
5126 DT_UNKNOWN
, DT_REG
, DT_DIR
, DT_CHR
, DT_BLK
, DT_FIFO
, DT_SOCK
, DT_LNK
5129 static int btrfs_real_readdir(struct file
*filp
, void *dirent
,
5132 struct inode
*inode
= file_inode(filp
);
5133 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5134 struct btrfs_item
*item
;
5135 struct btrfs_dir_item
*di
;
5136 struct btrfs_key key
;
5137 struct btrfs_key found_key
;
5138 struct btrfs_path
*path
;
5139 struct list_head ins_list
;
5140 struct list_head del_list
;
5142 struct extent_buffer
*leaf
;
5144 unsigned char d_type
;
5149 int key_type
= BTRFS_DIR_INDEX_KEY
;
5153 int is_curr
= 0; /* filp->f_pos points to the current index? */
5155 /* FIXME, use a real flag for deciding about the key type */
5156 if (root
->fs_info
->tree_root
== root
)
5157 key_type
= BTRFS_DIR_ITEM_KEY
;
5159 /* special case for "." */
5160 if (filp
->f_pos
== 0) {
5161 over
= filldir(dirent
, ".", 1,
5162 filp
->f_pos
, btrfs_ino(inode
), DT_DIR
);
5167 /* special case for .., just use the back ref */
5168 if (filp
->f_pos
== 1) {
5169 u64 pino
= parent_ino(filp
->f_path
.dentry
);
5170 over
= filldir(dirent
, "..", 2,
5171 filp
->f_pos
, pino
, DT_DIR
);
5176 path
= btrfs_alloc_path();
5182 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5183 INIT_LIST_HEAD(&ins_list
);
5184 INIT_LIST_HEAD(&del_list
);
5185 btrfs_get_delayed_items(inode
, &ins_list
, &del_list
);
5188 btrfs_set_key_type(&key
, key_type
);
5189 key
.offset
= filp
->f_pos
;
5190 key
.objectid
= btrfs_ino(inode
);
5192 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5197 leaf
= path
->nodes
[0];
5198 slot
= path
->slots
[0];
5199 if (slot
>= btrfs_header_nritems(leaf
)) {
5200 ret
= btrfs_next_leaf(root
, path
);
5208 item
= btrfs_item_nr(leaf
, slot
);
5209 btrfs_item_key_to_cpu(leaf
, &found_key
, slot
);
5211 if (found_key
.objectid
!= key
.objectid
)
5213 if (btrfs_key_type(&found_key
) != key_type
)
5215 if (found_key
.offset
< filp
->f_pos
)
5217 if (key_type
== BTRFS_DIR_INDEX_KEY
&&
5218 btrfs_should_delete_dir_index(&del_list
,
5222 filp
->f_pos
= found_key
.offset
;
5225 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
5227 di_total
= btrfs_item_size(leaf
, item
);
5229 while (di_cur
< di_total
) {
5230 struct btrfs_key location
;
5232 if (verify_dir_item(root
, leaf
, di
))
5235 name_len
= btrfs_dir_name_len(leaf
, di
);
5236 if (name_len
<= sizeof(tmp_name
)) {
5237 name_ptr
= tmp_name
;
5239 name_ptr
= kmalloc(name_len
, GFP_NOFS
);
5245 read_extent_buffer(leaf
, name_ptr
,
5246 (unsigned long)(di
+ 1), name_len
);
5248 d_type
= btrfs_filetype_table
[btrfs_dir_type(leaf
, di
)];
5249 btrfs_dir_item_key_to_cpu(leaf
, di
, &location
);
5252 /* is this a reference to our own snapshot? If so
5255 * In contrast to old kernels, we insert the snapshot's
5256 * dir item and dir index after it has been created, so
5257 * we won't find a reference to our own snapshot. We
5258 * still keep the following code for backward
5261 if (location
.type
== BTRFS_ROOT_ITEM_KEY
&&
5262 location
.objectid
== root
->root_key
.objectid
) {
5266 over
= filldir(dirent
, name_ptr
, name_len
,
5267 found_key
.offset
, location
.objectid
,
5271 if (name_ptr
!= tmp_name
)
5276 di_len
= btrfs_dir_name_len(leaf
, di
) +
5277 btrfs_dir_data_len(leaf
, di
) + sizeof(*di
);
5279 di
= (struct btrfs_dir_item
*)((char *)di
+ di_len
);
5285 if (key_type
== BTRFS_DIR_INDEX_KEY
) {
5288 ret
= btrfs_readdir_delayed_dir_index(filp
, dirent
, filldir
,
5294 /* Reached end of directory/root. Bump pos past the last item. */
5295 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5297 * 32-bit glibc will use getdents64, but then strtol -
5298 * so the last number we can serve is this.
5300 filp
->f_pos
= 0x7fffffff;
5306 if (key_type
== BTRFS_DIR_INDEX_KEY
)
5307 btrfs_put_delayed_items(&ins_list
, &del_list
);
5308 btrfs_free_path(path
);
5312 int btrfs_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
5314 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5315 struct btrfs_trans_handle
*trans
;
5317 bool nolock
= false;
5319 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5322 if (btrfs_fs_closing(root
->fs_info
) && btrfs_is_free_space_inode(inode
))
5325 if (wbc
->sync_mode
== WB_SYNC_ALL
) {
5327 trans
= btrfs_join_transaction_nolock(root
);
5329 trans
= btrfs_join_transaction(root
);
5331 return PTR_ERR(trans
);
5332 ret
= btrfs_commit_transaction(trans
, root
);
5338 * This is somewhat expensive, updating the tree every time the
5339 * inode changes. But, it is most likely to find the inode in cache.
5340 * FIXME, needs more benchmarking...there are no reasons other than performance
5341 * to keep or drop this code.
5343 static int btrfs_dirty_inode(struct inode
*inode
)
5345 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5346 struct btrfs_trans_handle
*trans
;
5349 if (test_bit(BTRFS_INODE_DUMMY
, &BTRFS_I(inode
)->runtime_flags
))
5352 trans
= btrfs_join_transaction(root
);
5354 return PTR_ERR(trans
);
5356 ret
= btrfs_update_inode(trans
, root
, inode
);
5357 if (ret
&& ret
== -ENOSPC
) {
5358 /* whoops, lets try again with the full transaction */
5359 btrfs_end_transaction(trans
, root
);
5360 trans
= btrfs_start_transaction(root
, 1);
5362 return PTR_ERR(trans
);
5364 ret
= btrfs_update_inode(trans
, root
, inode
);
5366 btrfs_end_transaction(trans
, root
);
5367 if (BTRFS_I(inode
)->delayed_node
)
5368 btrfs_balance_delayed_items(root
);
5374 * This is a copy of file_update_time. We need this so we can return error on
5375 * ENOSPC for updating the inode in the case of file write and mmap writes.
5377 static int btrfs_update_time(struct inode
*inode
, struct timespec
*now
,
5380 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5382 if (btrfs_root_readonly(root
))
5385 if (flags
& S_VERSION
)
5386 inode_inc_iversion(inode
);
5387 if (flags
& S_CTIME
)
5388 inode
->i_ctime
= *now
;
5389 if (flags
& S_MTIME
)
5390 inode
->i_mtime
= *now
;
5391 if (flags
& S_ATIME
)
5392 inode
->i_atime
= *now
;
5393 return btrfs_dirty_inode(inode
);
5397 * find the highest existing sequence number in a directory
5398 * and then set the in-memory index_cnt variable to reflect
5399 * free sequence numbers
5401 static int btrfs_set_inode_index_count(struct inode
*inode
)
5403 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
5404 struct btrfs_key key
, found_key
;
5405 struct btrfs_path
*path
;
5406 struct extent_buffer
*leaf
;
5409 key
.objectid
= btrfs_ino(inode
);
5410 btrfs_set_key_type(&key
, BTRFS_DIR_INDEX_KEY
);
5411 key
.offset
= (u64
)-1;
5413 path
= btrfs_alloc_path();
5417 ret
= btrfs_search_slot(NULL
, root
, &key
, path
, 0, 0);
5420 /* FIXME: we should be able to handle this */
5426 * MAGIC NUMBER EXPLANATION:
5427 * since we search a directory based on f_pos we have to start at 2
5428 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5429 * else has to start at 2
5431 if (path
->slots
[0] == 0) {
5432 BTRFS_I(inode
)->index_cnt
= 2;
5438 leaf
= path
->nodes
[0];
5439 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
5441 if (found_key
.objectid
!= btrfs_ino(inode
) ||
5442 btrfs_key_type(&found_key
) != BTRFS_DIR_INDEX_KEY
) {
5443 BTRFS_I(inode
)->index_cnt
= 2;
5447 BTRFS_I(inode
)->index_cnt
= found_key
.offset
+ 1;
5449 btrfs_free_path(path
);
5454 * helper to find a free sequence number in a given directory. This current
5455 * code is very simple, later versions will do smarter things in the btree
5457 int btrfs_set_inode_index(struct inode
*dir
, u64
*index
)
5461 if (BTRFS_I(dir
)->index_cnt
== (u64
)-1) {
5462 ret
= btrfs_inode_delayed_dir_index_count(dir
);
5464 ret
= btrfs_set_inode_index_count(dir
);
5470 *index
= BTRFS_I(dir
)->index_cnt
;
5471 BTRFS_I(dir
)->index_cnt
++;
5476 static struct inode
*btrfs_new_inode(struct btrfs_trans_handle
*trans
,
5477 struct btrfs_root
*root
,
5479 const char *name
, int name_len
,
5480 u64 ref_objectid
, u64 objectid
,
5481 umode_t mode
, u64
*index
)
5483 struct inode
*inode
;
5484 struct btrfs_inode_item
*inode_item
;
5485 struct btrfs_key
*location
;
5486 struct btrfs_path
*path
;
5487 struct btrfs_inode_ref
*ref
;
5488 struct btrfs_key key
[2];
5494 path
= btrfs_alloc_path();
5496 return ERR_PTR(-ENOMEM
);
5498 inode
= new_inode(root
->fs_info
->sb
);
5500 btrfs_free_path(path
);
5501 return ERR_PTR(-ENOMEM
);
5505 * we have to initialize this early, so we can reclaim the inode
5506 * number if we fail afterwards in this function.
5508 inode
->i_ino
= objectid
;
5511 trace_btrfs_inode_request(dir
);
5513 ret
= btrfs_set_inode_index(dir
, index
);
5515 btrfs_free_path(path
);
5517 return ERR_PTR(ret
);
5521 * index_cnt is ignored for everything but a dir,
5522 * btrfs_get_inode_index_count has an explanation for the magic
5525 BTRFS_I(inode
)->index_cnt
= 2;
5526 BTRFS_I(inode
)->root
= root
;
5527 BTRFS_I(inode
)->generation
= trans
->transid
;
5528 inode
->i_generation
= BTRFS_I(inode
)->generation
;
5531 * We could have gotten an inode number from somebody who was fsynced
5532 * and then removed in this same transaction, so let's just set full
5533 * sync since it will be a full sync anyway and this will blow away the
5534 * old info in the log.
5536 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
5543 key
[0].objectid
= objectid
;
5544 btrfs_set_key_type(&key
[0], BTRFS_INODE_ITEM_KEY
);
5548 * Start new inodes with an inode_ref. This is slightly more
5549 * efficient for small numbers of hard links since they will
5550 * be packed into one item. Extended refs will kick in if we
5551 * add more hard links than can fit in the ref item.
5553 key
[1].objectid
= objectid
;
5554 btrfs_set_key_type(&key
[1], BTRFS_INODE_REF_KEY
);
5555 key
[1].offset
= ref_objectid
;
5557 sizes
[0] = sizeof(struct btrfs_inode_item
);
5558 sizes
[1] = name_len
+ sizeof(*ref
);
5560 path
->leave_spinning
= 1;
5561 ret
= btrfs_insert_empty_items(trans
, root
, path
, key
, sizes
, 2);
5565 inode_init_owner(inode
, dir
, mode
);
5566 inode_set_bytes(inode
, 0);
5567 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
5568 inode_item
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0],
5569 struct btrfs_inode_item
);
5570 memset_extent_buffer(path
->nodes
[0], 0, (unsigned long)inode_item
,
5571 sizeof(*inode_item
));
5572 fill_inode_item(trans
, path
->nodes
[0], inode_item
, inode
);
5574 ref
= btrfs_item_ptr(path
->nodes
[0], path
->slots
[0] + 1,
5575 struct btrfs_inode_ref
);
5576 btrfs_set_inode_ref_name_len(path
->nodes
[0], ref
, name_len
);
5577 btrfs_set_inode_ref_index(path
->nodes
[0], ref
, *index
);
5578 ptr
= (unsigned long)(ref
+ 1);
5579 write_extent_buffer(path
->nodes
[0], name
, ptr
, name_len
);
5581 btrfs_mark_buffer_dirty(path
->nodes
[0]);
5582 btrfs_free_path(path
);
5584 location
= &BTRFS_I(inode
)->location
;
5585 location
->objectid
= objectid
;
5586 location
->offset
= 0;
5587 btrfs_set_key_type(location
, BTRFS_INODE_ITEM_KEY
);
5589 btrfs_inherit_iflags(inode
, dir
);
5591 if (S_ISREG(mode
)) {
5592 if (btrfs_test_opt(root
, NODATASUM
))
5593 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATASUM
;
5594 if (btrfs_test_opt(root
, NODATACOW
))
5595 BTRFS_I(inode
)->flags
|= BTRFS_INODE_NODATACOW
|
5596 BTRFS_INODE_NODATASUM
;
5599 insert_inode_hash(inode
);
5600 inode_tree_add(inode
);
5602 trace_btrfs_inode_new(inode
);
5603 btrfs_set_inode_last_trans(trans
, inode
);
5605 btrfs_update_root_times(trans
, root
);
5610 BTRFS_I(dir
)->index_cnt
--;
5611 btrfs_free_path(path
);
5613 return ERR_PTR(ret
);
5616 static inline u8
btrfs_inode_type(struct inode
*inode
)
5618 return btrfs_type_by_mode
[(inode
->i_mode
& S_IFMT
) >> S_SHIFT
];
5622 * utility function to add 'inode' into 'parent_inode' with
5623 * a give name and a given sequence number.
5624 * if 'add_backref' is true, also insert a backref from the
5625 * inode to the parent directory.
5627 int btrfs_add_link(struct btrfs_trans_handle
*trans
,
5628 struct inode
*parent_inode
, struct inode
*inode
,
5629 const char *name
, int name_len
, int add_backref
, u64 index
)
5632 struct btrfs_key key
;
5633 struct btrfs_root
*root
= BTRFS_I(parent_inode
)->root
;
5634 u64 ino
= btrfs_ino(inode
);
5635 u64 parent_ino
= btrfs_ino(parent_inode
);
5637 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5638 memcpy(&key
, &BTRFS_I(inode
)->root
->root_key
, sizeof(key
));
5641 btrfs_set_key_type(&key
, BTRFS_INODE_ITEM_KEY
);
5645 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5646 ret
= btrfs_add_root_ref(trans
, root
->fs_info
->tree_root
,
5647 key
.objectid
, root
->root_key
.objectid
,
5648 parent_ino
, index
, name
, name_len
);
5649 } else if (add_backref
) {
5650 ret
= btrfs_insert_inode_ref(trans
, root
, name
, name_len
, ino
,
5654 /* Nothing to clean up yet */
5658 ret
= btrfs_insert_dir_item(trans
, root
, name
, name_len
,
5660 btrfs_inode_type(inode
), index
);
5661 if (ret
== -EEXIST
|| ret
== -EOVERFLOW
)
5664 btrfs_abort_transaction(trans
, root
, ret
);
5668 btrfs_i_size_write(parent_inode
, parent_inode
->i_size
+
5670 inode_inc_iversion(parent_inode
);
5671 parent_inode
->i_mtime
= parent_inode
->i_ctime
= CURRENT_TIME
;
5672 ret
= btrfs_update_inode(trans
, root
, parent_inode
);
5674 btrfs_abort_transaction(trans
, root
, ret
);
5678 if (unlikely(ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
5681 err
= btrfs_del_root_ref(trans
, root
->fs_info
->tree_root
,
5682 key
.objectid
, root
->root_key
.objectid
,
5683 parent_ino
, &local_index
, name
, name_len
);
5685 } else if (add_backref
) {
5689 err
= btrfs_del_inode_ref(trans
, root
, name
, name_len
,
5690 ino
, parent_ino
, &local_index
);
5695 static int btrfs_add_nondir(struct btrfs_trans_handle
*trans
,
5696 struct inode
*dir
, struct dentry
*dentry
,
5697 struct inode
*inode
, int backref
, u64 index
)
5699 int err
= btrfs_add_link(trans
, dir
, inode
,
5700 dentry
->d_name
.name
, dentry
->d_name
.len
,
5707 static int btrfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
5708 umode_t mode
, dev_t rdev
)
5710 struct btrfs_trans_handle
*trans
;
5711 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5712 struct inode
*inode
= NULL
;
5718 if (!new_valid_dev(rdev
))
5722 * 2 for inode item and ref
5724 * 1 for xattr if selinux is on
5726 trans
= btrfs_start_transaction(root
, 5);
5728 return PTR_ERR(trans
);
5730 err
= btrfs_find_free_ino(root
, &objectid
);
5734 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5735 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5737 if (IS_ERR(inode
)) {
5738 err
= PTR_ERR(inode
);
5742 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5749 * If the active LSM wants to access the inode during
5750 * d_instantiate it needs these. Smack checks to see
5751 * if the filesystem supports xattrs by looking at the
5755 inode
->i_op
= &btrfs_special_inode_operations
;
5756 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5760 init_special_inode(inode
, inode
->i_mode
, rdev
);
5761 btrfs_update_inode(trans
, root
, inode
);
5762 d_instantiate(dentry
, inode
);
5765 btrfs_end_transaction(trans
, root
);
5766 btrfs_btree_balance_dirty(root
);
5768 inode_dec_link_count(inode
);
5774 static int btrfs_create(struct inode
*dir
, struct dentry
*dentry
,
5775 umode_t mode
, bool excl
)
5777 struct btrfs_trans_handle
*trans
;
5778 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5779 struct inode
*inode
= NULL
;
5780 int drop_inode_on_err
= 0;
5786 * 2 for inode item and ref
5788 * 1 for xattr if selinux is on
5790 trans
= btrfs_start_transaction(root
, 5);
5792 return PTR_ERR(trans
);
5794 err
= btrfs_find_free_ino(root
, &objectid
);
5798 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5799 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5801 if (IS_ERR(inode
)) {
5802 err
= PTR_ERR(inode
);
5805 drop_inode_on_err
= 1;
5807 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5811 err
= btrfs_update_inode(trans
, root
, inode
);
5816 * If the active LSM wants to access the inode during
5817 * d_instantiate it needs these. Smack checks to see
5818 * if the filesystem supports xattrs by looking at the
5821 inode
->i_fop
= &btrfs_file_operations
;
5822 inode
->i_op
= &btrfs_file_inode_operations
;
5824 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
5828 inode
->i_mapping
->a_ops
= &btrfs_aops
;
5829 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
5830 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
5831 d_instantiate(dentry
, inode
);
5834 btrfs_end_transaction(trans
, root
);
5835 if (err
&& drop_inode_on_err
) {
5836 inode_dec_link_count(inode
);
5839 btrfs_btree_balance_dirty(root
);
5843 static int btrfs_link(struct dentry
*old_dentry
, struct inode
*dir
,
5844 struct dentry
*dentry
)
5846 struct btrfs_trans_handle
*trans
;
5847 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5848 struct inode
*inode
= old_dentry
->d_inode
;
5853 /* do not allow sys_link's with other subvols of the same device */
5854 if (root
->objectid
!= BTRFS_I(inode
)->root
->objectid
)
5857 if (inode
->i_nlink
>= BTRFS_LINK_MAX
)
5860 err
= btrfs_set_inode_index(dir
, &index
);
5865 * 2 items for inode and inode ref
5866 * 2 items for dir items
5867 * 1 item for parent inode
5869 trans
= btrfs_start_transaction(root
, 5);
5870 if (IS_ERR(trans
)) {
5871 err
= PTR_ERR(trans
);
5875 btrfs_inc_nlink(inode
);
5876 inode_inc_iversion(inode
);
5877 inode
->i_ctime
= CURRENT_TIME
;
5879 set_bit(BTRFS_INODE_COPY_EVERYTHING
, &BTRFS_I(inode
)->runtime_flags
);
5881 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 1, index
);
5886 struct dentry
*parent
= dentry
->d_parent
;
5887 err
= btrfs_update_inode(trans
, root
, inode
);
5890 d_instantiate(dentry
, inode
);
5891 btrfs_log_new_name(trans
, inode
, NULL
, parent
);
5894 btrfs_end_transaction(trans
, root
);
5897 inode_dec_link_count(inode
);
5900 btrfs_btree_balance_dirty(root
);
5904 static int btrfs_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
5906 struct inode
*inode
= NULL
;
5907 struct btrfs_trans_handle
*trans
;
5908 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
5910 int drop_on_err
= 0;
5915 * 2 items for inode and ref
5916 * 2 items for dir items
5917 * 1 for xattr if selinux is on
5919 trans
= btrfs_start_transaction(root
, 5);
5921 return PTR_ERR(trans
);
5923 err
= btrfs_find_free_ino(root
, &objectid
);
5927 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
5928 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
5929 S_IFDIR
| mode
, &index
);
5930 if (IS_ERR(inode
)) {
5931 err
= PTR_ERR(inode
);
5937 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
5941 inode
->i_op
= &btrfs_dir_inode_operations
;
5942 inode
->i_fop
= &btrfs_dir_file_operations
;
5944 btrfs_i_size_write(inode
, 0);
5945 err
= btrfs_update_inode(trans
, root
, inode
);
5949 err
= btrfs_add_link(trans
, dir
, inode
, dentry
->d_name
.name
,
5950 dentry
->d_name
.len
, 0, index
);
5954 d_instantiate(dentry
, inode
);
5958 btrfs_end_transaction(trans
, root
);
5961 btrfs_btree_balance_dirty(root
);
5965 /* helper for btfs_get_extent. Given an existing extent in the tree,
5966 * and an extent that you want to insert, deal with overlap and insert
5967 * the new extent into the tree.
5969 static int merge_extent_mapping(struct extent_map_tree
*em_tree
,
5970 struct extent_map
*existing
,
5971 struct extent_map
*em
,
5972 u64 map_start
, u64 map_len
)
5976 BUG_ON(map_start
< em
->start
|| map_start
>= extent_map_end(em
));
5977 start_diff
= map_start
- em
->start
;
5978 em
->start
= map_start
;
5980 if (em
->block_start
< EXTENT_MAP_LAST_BYTE
&&
5981 !test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
)) {
5982 em
->block_start
+= start_diff
;
5983 em
->block_len
-= start_diff
;
5985 return add_extent_mapping(em_tree
, em
, 0);
5988 static noinline
int uncompress_inline(struct btrfs_path
*path
,
5989 struct inode
*inode
, struct page
*page
,
5990 size_t pg_offset
, u64 extent_offset
,
5991 struct btrfs_file_extent_item
*item
)
5994 struct extent_buffer
*leaf
= path
->nodes
[0];
5997 unsigned long inline_size
;
6001 WARN_ON(pg_offset
!= 0);
6002 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6003 max_size
= btrfs_file_extent_ram_bytes(leaf
, item
);
6004 inline_size
= btrfs_file_extent_inline_item_len(leaf
,
6005 btrfs_item_nr(leaf
, path
->slots
[0]));
6006 tmp
= kmalloc(inline_size
, GFP_NOFS
);
6009 ptr
= btrfs_file_extent_inline_start(item
);
6011 read_extent_buffer(leaf
, tmp
, ptr
, inline_size
);
6013 max_size
= min_t(unsigned long, PAGE_CACHE_SIZE
, max_size
);
6014 ret
= btrfs_decompress(compress_type
, tmp
, page
,
6015 extent_offset
, inline_size
, max_size
);
6017 char *kaddr
= kmap_atomic(page
);
6018 unsigned long copy_size
= min_t(u64
,
6019 PAGE_CACHE_SIZE
- pg_offset
,
6020 max_size
- extent_offset
);
6021 memset(kaddr
+ pg_offset
, 0, copy_size
);
6022 kunmap_atomic(kaddr
);
6029 * a bit scary, this does extent mapping from logical file offset to the disk.
6030 * the ugly parts come from merging extents from the disk with the in-ram
6031 * representation. This gets more complex because of the data=ordered code,
6032 * where the in-ram extents might be locked pending data=ordered completion.
6034 * This also copies inline extents directly into the page.
6037 struct extent_map
*btrfs_get_extent(struct inode
*inode
, struct page
*page
,
6038 size_t pg_offset
, u64 start
, u64 len
,
6044 u64 extent_start
= 0;
6046 u64 objectid
= btrfs_ino(inode
);
6048 struct btrfs_path
*path
= NULL
;
6049 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6050 struct btrfs_file_extent_item
*item
;
6051 struct extent_buffer
*leaf
;
6052 struct btrfs_key found_key
;
6053 struct extent_map
*em
= NULL
;
6054 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
6055 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
6056 struct btrfs_trans_handle
*trans
= NULL
;
6060 read_lock(&em_tree
->lock
);
6061 em
= lookup_extent_mapping(em_tree
, start
, len
);
6063 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6064 read_unlock(&em_tree
->lock
);
6067 if (em
->start
> start
|| em
->start
+ em
->len
<= start
)
6068 free_extent_map(em
);
6069 else if (em
->block_start
== EXTENT_MAP_INLINE
&& page
)
6070 free_extent_map(em
);
6074 em
= alloc_extent_map();
6079 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6080 em
->start
= EXTENT_MAP_HOLE
;
6081 em
->orig_start
= EXTENT_MAP_HOLE
;
6083 em
->block_len
= (u64
)-1;
6086 path
= btrfs_alloc_path();
6092 * Chances are we'll be called again, so go ahead and do
6098 ret
= btrfs_lookup_file_extent(trans
, root
, path
,
6099 objectid
, start
, trans
!= NULL
);
6106 if (path
->slots
[0] == 0)
6111 leaf
= path
->nodes
[0];
6112 item
= btrfs_item_ptr(leaf
, path
->slots
[0],
6113 struct btrfs_file_extent_item
);
6114 /* are we inside the extent that was found? */
6115 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6116 found_type
= btrfs_key_type(&found_key
);
6117 if (found_key
.objectid
!= objectid
||
6118 found_type
!= BTRFS_EXTENT_DATA_KEY
) {
6122 found_type
= btrfs_file_extent_type(leaf
, item
);
6123 extent_start
= found_key
.offset
;
6124 compress_type
= btrfs_file_extent_compression(leaf
, item
);
6125 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6126 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6127 extent_end
= extent_start
+
6128 btrfs_file_extent_num_bytes(leaf
, item
);
6129 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6131 size
= btrfs_file_extent_inline_len(leaf
, item
);
6132 extent_end
= ALIGN(extent_start
+ size
, root
->sectorsize
);
6135 if (start
>= extent_end
) {
6137 if (path
->slots
[0] >= btrfs_header_nritems(leaf
)) {
6138 ret
= btrfs_next_leaf(root
, path
);
6145 leaf
= path
->nodes
[0];
6147 btrfs_item_key_to_cpu(leaf
, &found_key
, path
->slots
[0]);
6148 if (found_key
.objectid
!= objectid
||
6149 found_key
.type
!= BTRFS_EXTENT_DATA_KEY
)
6151 if (start
+ len
<= found_key
.offset
)
6154 em
->orig_start
= start
;
6155 em
->len
= found_key
.offset
- start
;
6159 em
->ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, item
);
6160 if (found_type
== BTRFS_FILE_EXTENT_REG
||
6161 found_type
== BTRFS_FILE_EXTENT_PREALLOC
) {
6162 em
->start
= extent_start
;
6163 em
->len
= extent_end
- extent_start
;
6164 em
->orig_start
= extent_start
-
6165 btrfs_file_extent_offset(leaf
, item
);
6166 em
->orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
,
6168 bytenr
= btrfs_file_extent_disk_bytenr(leaf
, item
);
6170 em
->block_start
= EXTENT_MAP_HOLE
;
6173 if (compress_type
!= BTRFS_COMPRESS_NONE
) {
6174 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6175 em
->compress_type
= compress_type
;
6176 em
->block_start
= bytenr
;
6177 em
->block_len
= em
->orig_block_len
;
6179 bytenr
+= btrfs_file_extent_offset(leaf
, item
);
6180 em
->block_start
= bytenr
;
6181 em
->block_len
= em
->len
;
6182 if (found_type
== BTRFS_FILE_EXTENT_PREALLOC
)
6183 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6186 } else if (found_type
== BTRFS_FILE_EXTENT_INLINE
) {
6190 size_t extent_offset
;
6193 em
->block_start
= EXTENT_MAP_INLINE
;
6194 if (!page
|| create
) {
6195 em
->start
= extent_start
;
6196 em
->len
= extent_end
- extent_start
;
6200 size
= btrfs_file_extent_inline_len(leaf
, item
);
6201 extent_offset
= page_offset(page
) + pg_offset
- extent_start
;
6202 copy_size
= min_t(u64
, PAGE_CACHE_SIZE
- pg_offset
,
6203 size
- extent_offset
);
6204 em
->start
= extent_start
+ extent_offset
;
6205 em
->len
= ALIGN(copy_size
, root
->sectorsize
);
6206 em
->orig_block_len
= em
->len
;
6207 em
->orig_start
= em
->start
;
6208 if (compress_type
) {
6209 set_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
6210 em
->compress_type
= compress_type
;
6212 ptr
= btrfs_file_extent_inline_start(item
) + extent_offset
;
6213 if (create
== 0 && !PageUptodate(page
)) {
6214 if (btrfs_file_extent_compression(leaf
, item
) !=
6215 BTRFS_COMPRESS_NONE
) {
6216 ret
= uncompress_inline(path
, inode
, page
,
6218 extent_offset
, item
);
6219 BUG_ON(ret
); /* -ENOMEM */
6222 read_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6224 if (pg_offset
+ copy_size
< PAGE_CACHE_SIZE
) {
6225 memset(map
+ pg_offset
+ copy_size
, 0,
6226 PAGE_CACHE_SIZE
- pg_offset
-
6231 flush_dcache_page(page
);
6232 } else if (create
&& PageUptodate(page
)) {
6236 free_extent_map(em
);
6239 btrfs_release_path(path
);
6240 trans
= btrfs_join_transaction(root
);
6243 return ERR_CAST(trans
);
6247 write_extent_buffer(leaf
, map
+ pg_offset
, ptr
,
6250 btrfs_mark_buffer_dirty(leaf
);
6252 set_extent_uptodate(io_tree
, em
->start
,
6253 extent_map_end(em
) - 1, NULL
, GFP_NOFS
);
6256 WARN(1, KERN_ERR
"btrfs unknown found_type %d\n", found_type
);
6260 em
->orig_start
= start
;
6263 em
->block_start
= EXTENT_MAP_HOLE
;
6264 set_bit(EXTENT_FLAG_VACANCY
, &em
->flags
);
6266 btrfs_release_path(path
);
6267 if (em
->start
> start
|| extent_map_end(em
) <= start
) {
6268 btrfs_err(root
->fs_info
, "bad extent! em: [%llu %llu] passed [%llu %llu]",
6269 (unsigned long long)em
->start
,
6270 (unsigned long long)em
->len
,
6271 (unsigned long long)start
,
6272 (unsigned long long)len
);
6278 write_lock(&em_tree
->lock
);
6279 ret
= add_extent_mapping(em_tree
, em
, 0);
6280 /* it is possible that someone inserted the extent into the tree
6281 * while we had the lock dropped. It is also possible that
6282 * an overlapping map exists in the tree
6284 if (ret
== -EEXIST
) {
6285 struct extent_map
*existing
;
6289 existing
= lookup_extent_mapping(em_tree
, start
, len
);
6290 if (existing
&& (existing
->start
> start
||
6291 existing
->start
+ existing
->len
<= start
)) {
6292 free_extent_map(existing
);
6296 existing
= lookup_extent_mapping(em_tree
, em
->start
,
6299 err
= merge_extent_mapping(em_tree
, existing
,
6302 free_extent_map(existing
);
6304 free_extent_map(em
);
6309 free_extent_map(em
);
6313 free_extent_map(em
);
6318 write_unlock(&em_tree
->lock
);
6322 trace_btrfs_get_extent(root
, em
);
6325 btrfs_free_path(path
);
6327 ret
= btrfs_end_transaction(trans
, root
);
6332 free_extent_map(em
);
6333 return ERR_PTR(err
);
6335 BUG_ON(!em
); /* Error is always set */
6339 struct extent_map
*btrfs_get_extent_fiemap(struct inode
*inode
, struct page
*page
,
6340 size_t pg_offset
, u64 start
, u64 len
,
6343 struct extent_map
*em
;
6344 struct extent_map
*hole_em
= NULL
;
6345 u64 range_start
= start
;
6351 em
= btrfs_get_extent(inode
, page
, pg_offset
, start
, len
, create
);
6358 * - a pre-alloc extent,
6359 * there might actually be delalloc bytes behind it.
6361 if (em
->block_start
!= EXTENT_MAP_HOLE
&&
6362 !test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6368 /* check to see if we've wrapped (len == -1 or similar) */
6377 /* ok, we didn't find anything, lets look for delalloc */
6378 found
= count_range_bits(&BTRFS_I(inode
)->io_tree
, &range_start
,
6379 end
, len
, EXTENT_DELALLOC
, 1);
6380 found_end
= range_start
+ found
;
6381 if (found_end
< range_start
)
6382 found_end
= (u64
)-1;
6385 * we didn't find anything useful, return
6386 * the original results from get_extent()
6388 if (range_start
> end
|| found_end
<= start
) {
6394 /* adjust the range_start to make sure it doesn't
6395 * go backwards from the start they passed in
6397 range_start
= max(start
,range_start
);
6398 found
= found_end
- range_start
;
6401 u64 hole_start
= start
;
6404 em
= alloc_extent_map();
6410 * when btrfs_get_extent can't find anything it
6411 * returns one huge hole
6413 * make sure what it found really fits our range, and
6414 * adjust to make sure it is based on the start from
6418 u64 calc_end
= extent_map_end(hole_em
);
6420 if (calc_end
<= start
|| (hole_em
->start
> end
)) {
6421 free_extent_map(hole_em
);
6424 hole_start
= max(hole_em
->start
, start
);
6425 hole_len
= calc_end
- hole_start
;
6429 if (hole_em
&& range_start
> hole_start
) {
6430 /* our hole starts before our delalloc, so we
6431 * have to return just the parts of the hole
6432 * that go until the delalloc starts
6434 em
->len
= min(hole_len
,
6435 range_start
- hole_start
);
6436 em
->start
= hole_start
;
6437 em
->orig_start
= hole_start
;
6439 * don't adjust block start at all,
6440 * it is fixed at EXTENT_MAP_HOLE
6442 em
->block_start
= hole_em
->block_start
;
6443 em
->block_len
= hole_len
;
6444 if (test_bit(EXTENT_FLAG_PREALLOC
, &hole_em
->flags
))
6445 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
6447 em
->start
= range_start
;
6449 em
->orig_start
= range_start
;
6450 em
->block_start
= EXTENT_MAP_DELALLOC
;
6451 em
->block_len
= found
;
6453 } else if (hole_em
) {
6458 free_extent_map(hole_em
);
6460 free_extent_map(em
);
6461 return ERR_PTR(err
);
6466 static struct extent_map
*btrfs_new_extent_direct(struct inode
*inode
,
6469 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6470 struct btrfs_trans_handle
*trans
;
6471 struct extent_map
*em
;
6472 struct btrfs_key ins
;
6476 trans
= btrfs_join_transaction(root
);
6478 return ERR_CAST(trans
);
6480 trans
->block_rsv
= &root
->fs_info
->delalloc_block_rsv
;
6482 alloc_hint
= get_extent_allocation_hint(inode
, start
, len
);
6483 ret
= btrfs_reserve_extent(trans
, root
, len
, root
->sectorsize
, 0,
6484 alloc_hint
, &ins
, 1);
6490 em
= create_pinned_em(inode
, start
, ins
.offset
, start
, ins
.objectid
,
6491 ins
.offset
, ins
.offset
, ins
.offset
, 0);
6495 ret
= btrfs_add_ordered_extent_dio(inode
, start
, ins
.objectid
,
6496 ins
.offset
, ins
.offset
, 0);
6498 btrfs_free_reserved_extent(root
, ins
.objectid
, ins
.offset
);
6502 btrfs_end_transaction(trans
, root
);
6507 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6508 * block must be cow'd
6510 static noinline
int can_nocow_odirect(struct btrfs_trans_handle
*trans
,
6511 struct inode
*inode
, u64 offset
, u64
*len
,
6512 u64
*orig_start
, u64
*orig_block_len
,
6515 struct btrfs_path
*path
;
6517 struct extent_buffer
*leaf
;
6518 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6519 struct btrfs_file_extent_item
*fi
;
6520 struct btrfs_key key
;
6528 path
= btrfs_alloc_path();
6532 ret
= btrfs_lookup_file_extent(trans
, root
, path
, btrfs_ino(inode
),
6537 slot
= path
->slots
[0];
6540 /* can't find the item, must cow */
6547 leaf
= path
->nodes
[0];
6548 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
6549 if (key
.objectid
!= btrfs_ino(inode
) ||
6550 key
.type
!= BTRFS_EXTENT_DATA_KEY
) {
6551 /* not our file or wrong item type, must cow */
6555 if (key
.offset
> offset
) {
6556 /* Wrong offset, must cow */
6560 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
6561 found_type
= btrfs_file_extent_type(leaf
, fi
);
6562 if (found_type
!= BTRFS_FILE_EXTENT_REG
&&
6563 found_type
!= BTRFS_FILE_EXTENT_PREALLOC
) {
6564 /* not a regular extent, must cow */
6567 disk_bytenr
= btrfs_file_extent_disk_bytenr(leaf
, fi
);
6568 backref_offset
= btrfs_file_extent_offset(leaf
, fi
);
6570 *orig_start
= key
.offset
- backref_offset
;
6571 *orig_block_len
= btrfs_file_extent_disk_num_bytes(leaf
, fi
);
6572 *ram_bytes
= btrfs_file_extent_ram_bytes(leaf
, fi
);
6574 extent_end
= key
.offset
+ btrfs_file_extent_num_bytes(leaf
, fi
);
6575 if (extent_end
< offset
+ *len
) {
6576 /* extent doesn't include our full range, must cow */
6580 if (btrfs_extent_readonly(root
, disk_bytenr
))
6584 * look for other files referencing this extent, if we
6585 * find any we must cow
6587 if (btrfs_cross_ref_exist(trans
, root
, btrfs_ino(inode
),
6588 key
.offset
- backref_offset
, disk_bytenr
))
6592 * adjust disk_bytenr and num_bytes to cover just the bytes
6593 * in this extent we are about to write. If there
6594 * are any csums in that range we have to cow in order
6595 * to keep the csums correct
6597 disk_bytenr
+= backref_offset
;
6598 disk_bytenr
+= offset
- key
.offset
;
6599 num_bytes
= min(offset
+ *len
, extent_end
) - offset
;
6600 if (csum_exist_in_range(root
, disk_bytenr
, num_bytes
))
6603 * all of the above have passed, it is safe to overwrite this extent
6609 btrfs_free_path(path
);
6613 static int lock_extent_direct(struct inode
*inode
, u64 lockstart
, u64 lockend
,
6614 struct extent_state
**cached_state
, int writing
)
6616 struct btrfs_ordered_extent
*ordered
;
6620 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6623 * We're concerned with the entire range that we're going to be
6624 * doing DIO to, so we need to make sure theres no ordered
6625 * extents in this range.
6627 ordered
= btrfs_lookup_ordered_range(inode
, lockstart
,
6628 lockend
- lockstart
+ 1);
6631 * We need to make sure there are no buffered pages in this
6632 * range either, we could have raced between the invalidate in
6633 * generic_file_direct_write and locking the extent. The
6634 * invalidate needs to happen so that reads after a write do not
6637 if (!ordered
&& (!writing
||
6638 !test_range_bit(&BTRFS_I(inode
)->io_tree
,
6639 lockstart
, lockend
, EXTENT_UPTODATE
, 0,
6643 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6644 cached_state
, GFP_NOFS
);
6647 btrfs_start_ordered_extent(inode
, ordered
, 1);
6648 btrfs_put_ordered_extent(ordered
);
6650 /* Screw you mmap */
6651 ret
= filemap_write_and_wait_range(inode
->i_mapping
,
6658 * If we found a page that couldn't be invalidated just
6659 * fall back to buffered.
6661 ret
= invalidate_inode_pages2_range(inode
->i_mapping
,
6662 lockstart
>> PAGE_CACHE_SHIFT
,
6663 lockend
>> PAGE_CACHE_SHIFT
);
6674 static struct extent_map
*create_pinned_em(struct inode
*inode
, u64 start
,
6675 u64 len
, u64 orig_start
,
6676 u64 block_start
, u64 block_len
,
6677 u64 orig_block_len
, u64 ram_bytes
,
6680 struct extent_map_tree
*em_tree
;
6681 struct extent_map
*em
;
6682 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6685 em_tree
= &BTRFS_I(inode
)->extent_tree
;
6686 em
= alloc_extent_map();
6688 return ERR_PTR(-ENOMEM
);
6691 em
->orig_start
= orig_start
;
6692 em
->mod_start
= start
;
6695 em
->block_len
= block_len
;
6696 em
->block_start
= block_start
;
6697 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
6698 em
->orig_block_len
= orig_block_len
;
6699 em
->ram_bytes
= ram_bytes
;
6700 em
->generation
= -1;
6701 set_bit(EXTENT_FLAG_PINNED
, &em
->flags
);
6702 if (type
== BTRFS_ORDERED_PREALLOC
)
6703 set_bit(EXTENT_FLAG_FILLING
, &em
->flags
);
6706 btrfs_drop_extent_cache(inode
, em
->start
,
6707 em
->start
+ em
->len
- 1, 0);
6708 write_lock(&em_tree
->lock
);
6709 ret
= add_extent_mapping(em_tree
, em
, 1);
6710 write_unlock(&em_tree
->lock
);
6711 } while (ret
== -EEXIST
);
6714 free_extent_map(em
);
6715 return ERR_PTR(ret
);
6722 static int btrfs_get_blocks_direct(struct inode
*inode
, sector_t iblock
,
6723 struct buffer_head
*bh_result
, int create
)
6725 struct extent_map
*em
;
6726 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6727 struct extent_state
*cached_state
= NULL
;
6728 u64 start
= iblock
<< inode
->i_blkbits
;
6729 u64 lockstart
, lockend
;
6730 u64 len
= bh_result
->b_size
;
6731 struct btrfs_trans_handle
*trans
;
6732 int unlock_bits
= EXTENT_LOCKED
;
6736 unlock_bits
|= EXTENT_DELALLOC
| EXTENT_DIRTY
;
6738 len
= min_t(u64
, len
, root
->sectorsize
);
6741 lockend
= start
+ len
- 1;
6744 * If this errors out it's because we couldn't invalidate pagecache for
6745 * this range and we need to fallback to buffered.
6747 if (lock_extent_direct(inode
, lockstart
, lockend
, &cached_state
, create
))
6750 em
= btrfs_get_extent(inode
, NULL
, 0, start
, len
, 0);
6757 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
6758 * io. INLINE is special, and we could probably kludge it in here, but
6759 * it's still buffered so for safety lets just fall back to the generic
6762 * For COMPRESSED we _have_ to read the entire extent in so we can
6763 * decompress it, so there will be buffering required no matter what we
6764 * do, so go ahead and fallback to buffered.
6766 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6767 * to buffered IO. Don't blame me, this is the price we pay for using
6770 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
) ||
6771 em
->block_start
== EXTENT_MAP_INLINE
) {
6772 free_extent_map(em
);
6777 /* Just a good old fashioned hole, return */
6778 if (!create
&& (em
->block_start
== EXTENT_MAP_HOLE
||
6779 test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))) {
6780 free_extent_map(em
);
6785 * We don't allocate a new extent in the following cases
6787 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6789 * 2) The extent is marked as PREALLOC. We're good to go here and can
6790 * just use the extent.
6794 len
= min(len
, em
->len
- (start
- em
->start
));
6795 lockstart
= start
+ len
;
6799 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
) ||
6800 ((BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATACOW
) &&
6801 em
->block_start
!= EXTENT_MAP_HOLE
)) {
6804 u64 block_start
, orig_start
, orig_block_len
, ram_bytes
;
6806 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6807 type
= BTRFS_ORDERED_PREALLOC
;
6809 type
= BTRFS_ORDERED_NOCOW
;
6810 len
= min(len
, em
->len
- (start
- em
->start
));
6811 block_start
= em
->block_start
+ (start
- em
->start
);
6814 * we're not going to log anything, but we do need
6815 * to make sure the current transaction stays open
6816 * while we look for nocow cross refs
6818 trans
= btrfs_join_transaction(root
);
6822 if (can_nocow_odirect(trans
, inode
, start
, &len
, &orig_start
,
6823 &orig_block_len
, &ram_bytes
) == 1) {
6824 if (type
== BTRFS_ORDERED_PREALLOC
) {
6825 free_extent_map(em
);
6826 em
= create_pinned_em(inode
, start
, len
,
6832 btrfs_end_transaction(trans
, root
);
6837 ret
= btrfs_add_ordered_extent_dio(inode
, start
,
6838 block_start
, len
, len
, type
);
6839 btrfs_end_transaction(trans
, root
);
6841 free_extent_map(em
);
6846 btrfs_end_transaction(trans
, root
);
6850 * this will cow the extent, reset the len in case we changed
6853 len
= bh_result
->b_size
;
6854 free_extent_map(em
);
6855 em
= btrfs_new_extent_direct(inode
, start
, len
);
6860 len
= min(len
, em
->len
- (start
- em
->start
));
6862 bh_result
->b_blocknr
= (em
->block_start
+ (start
- em
->start
)) >>
6864 bh_result
->b_size
= len
;
6865 bh_result
->b_bdev
= em
->bdev
;
6866 set_buffer_mapped(bh_result
);
6868 if (!test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
6869 set_buffer_new(bh_result
);
6872 * Need to update the i_size under the extent lock so buffered
6873 * readers will get the updated i_size when we unlock.
6875 if (start
+ len
> i_size_read(inode
))
6876 i_size_write(inode
, start
+ len
);
6878 spin_lock(&BTRFS_I(inode
)->lock
);
6879 BTRFS_I(inode
)->outstanding_extents
++;
6880 spin_unlock(&BTRFS_I(inode
)->lock
);
6882 ret
= set_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6883 lockstart
+ len
- 1, EXTENT_DELALLOC
, NULL
,
6884 &cached_state
, GFP_NOFS
);
6889 * In the case of write we need to clear and unlock the entire range,
6890 * in the case of read we need to unlock only the end area that we
6891 * aren't using if there is any left over space.
6893 if (lockstart
< lockend
) {
6894 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
,
6895 lockend
, unlock_bits
, 1, 0,
6896 &cached_state
, GFP_NOFS
);
6898 free_extent_state(cached_state
);
6901 free_extent_map(em
);
6906 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, lockstart
, lockend
,
6907 unlock_bits
, 1, 0, &cached_state
, GFP_NOFS
);
6911 struct btrfs_dio_private
{
6912 struct inode
*inode
;
6918 /* number of bios pending for this dio */
6919 atomic_t pending_bios
;
6924 /* orig_bio is our btrfs_io_bio */
6925 struct bio
*orig_bio
;
6927 /* dio_bio came from fs/direct-io.c */
6928 struct bio
*dio_bio
;
6931 static void btrfs_endio_direct_read(struct bio
*bio
, int err
)
6933 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6934 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
6935 struct bio_vec
*bvec
= bio
->bi_io_vec
;
6936 struct inode
*inode
= dip
->inode
;
6937 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6938 struct bio
*dio_bio
;
6941 start
= dip
->logical_offset
;
6943 if (!(BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
)) {
6944 struct page
*page
= bvec
->bv_page
;
6947 u64
private = ~(u32
)0;
6948 unsigned long flags
;
6950 if (get_state_private(&BTRFS_I(inode
)->io_tree
,
6953 local_irq_save(flags
);
6954 kaddr
= kmap_atomic(page
);
6955 csum
= btrfs_csum_data(kaddr
+ bvec
->bv_offset
,
6956 csum
, bvec
->bv_len
);
6957 btrfs_csum_final(csum
, (char *)&csum
);
6958 kunmap_atomic(kaddr
);
6959 local_irq_restore(flags
);
6961 flush_dcache_page(bvec
->bv_page
);
6962 if (csum
!= private) {
6964 btrfs_err(root
->fs_info
, "csum failed ino %llu off %llu csum %u private %u",
6965 (unsigned long long)btrfs_ino(inode
),
6966 (unsigned long long)start
,
6967 csum
, (unsigned)private);
6972 start
+= bvec
->bv_len
;
6974 } while (bvec
<= bvec_end
);
6976 unlock_extent(&BTRFS_I(inode
)->io_tree
, dip
->logical_offset
,
6977 dip
->logical_offset
+ dip
->bytes
- 1);
6978 dio_bio
= dip
->dio_bio
;
6982 /* If we had a csum failure make sure to clear the uptodate flag */
6984 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
6985 dio_end_io(dio_bio
, err
);
6989 static void btrfs_endio_direct_write(struct bio
*bio
, int err
)
6991 struct btrfs_dio_private
*dip
= bio
->bi_private
;
6992 struct inode
*inode
= dip
->inode
;
6993 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
6994 struct btrfs_ordered_extent
*ordered
= NULL
;
6995 u64 ordered_offset
= dip
->logical_offset
;
6996 u64 ordered_bytes
= dip
->bytes
;
6997 struct bio
*dio_bio
;
7003 ret
= btrfs_dec_test_first_ordered_pending(inode
, &ordered
,
7005 ordered_bytes
, !err
);
7009 ordered
->work
.func
= finish_ordered_fn
;
7010 ordered
->work
.flags
= 0;
7011 btrfs_queue_worker(&root
->fs_info
->endio_write_workers
,
7015 * our bio might span multiple ordered extents. If we haven't
7016 * completed the accounting for the whole dio, go back and try again
7018 if (ordered_offset
< dip
->logical_offset
+ dip
->bytes
) {
7019 ordered_bytes
= dip
->logical_offset
+ dip
->bytes
-
7025 dio_bio
= dip
->dio_bio
;
7029 /* If we had an error make sure to clear the uptodate flag */
7031 clear_bit(BIO_UPTODATE
, &dio_bio
->bi_flags
);
7032 dio_end_io(dio_bio
, err
);
7036 static int __btrfs_submit_bio_start_direct_io(struct inode
*inode
, int rw
,
7037 struct bio
*bio
, int mirror_num
,
7038 unsigned long bio_flags
, u64 offset
)
7041 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7042 ret
= btrfs_csum_one_bio(root
, inode
, bio
, offset
, 1);
7043 BUG_ON(ret
); /* -ENOMEM */
7047 static void btrfs_end_dio_bio(struct bio
*bio
, int err
)
7049 struct btrfs_dio_private
*dip
= bio
->bi_private
;
7052 printk(KERN_ERR
"btrfs direct IO failed ino %llu rw %lu "
7053 "sector %#Lx len %u err no %d\n",
7054 (unsigned long long)btrfs_ino(dip
->inode
), bio
->bi_rw
,
7055 (unsigned long long)bio
->bi_sector
, bio
->bi_size
, err
);
7059 * before atomic variable goto zero, we must make sure
7060 * dip->errors is perceived to be set.
7062 smp_mb__before_atomic_dec();
7065 /* if there are more bios still pending for this dio, just exit */
7066 if (!atomic_dec_and_test(&dip
->pending_bios
))
7070 bio_io_error(dip
->orig_bio
);
7072 set_bit(BIO_UPTODATE
, &dip
->dio_bio
->bi_flags
);
7073 bio_endio(dip
->orig_bio
, 0);
7079 static struct bio
*btrfs_dio_bio_alloc(struct block_device
*bdev
,
7080 u64 first_sector
, gfp_t gfp_flags
)
7082 int nr_vecs
= bio_get_nr_vecs(bdev
);
7083 return btrfs_bio_alloc(bdev
, first_sector
, nr_vecs
, gfp_flags
);
7086 static inline int __btrfs_submit_dio_bio(struct bio
*bio
, struct inode
*inode
,
7087 int rw
, u64 file_offset
, int skip_sum
,
7090 int write
= rw
& REQ_WRITE
;
7091 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7095 async_submit
= !atomic_read(&BTRFS_I(inode
)->sync_writers
);
7100 ret
= btrfs_bio_wq_end_io(root
->fs_info
, bio
, 0);
7108 if (write
&& async_submit
) {
7109 ret
= btrfs_wq_submit_bio(root
->fs_info
,
7110 inode
, rw
, bio
, 0, 0,
7112 __btrfs_submit_bio_start_direct_io
,
7113 __btrfs_submit_bio_done
);
7117 * If we aren't doing async submit, calculate the csum of the
7120 ret
= btrfs_csum_one_bio(root
, inode
, bio
, file_offset
, 1);
7123 } else if (!skip_sum
) {
7124 ret
= btrfs_lookup_bio_sums_dio(root
, inode
, bio
, file_offset
);
7130 ret
= btrfs_map_bio(root
, rw
, bio
, 0, async_submit
);
7136 static int btrfs_submit_direct_hook(int rw
, struct btrfs_dio_private
*dip
,
7139 struct inode
*inode
= dip
->inode
;
7140 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7142 struct bio
*orig_bio
= dip
->orig_bio
;
7143 struct bio_vec
*bvec
= orig_bio
->bi_io_vec
;
7144 u64 start_sector
= orig_bio
->bi_sector
;
7145 u64 file_offset
= dip
->logical_offset
;
7150 int async_submit
= 0;
7152 map_length
= orig_bio
->bi_size
;
7153 ret
= btrfs_map_block(root
->fs_info
, rw
, start_sector
<< 9,
7154 &map_length
, NULL
, 0);
7159 if (map_length
>= orig_bio
->bi_size
) {
7164 /* async crcs make it difficult to collect full stripe writes. */
7165 if (btrfs_get_alloc_profile(root
, 1) &
7166 (BTRFS_BLOCK_GROUP_RAID5
| BTRFS_BLOCK_GROUP_RAID6
))
7171 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
, start_sector
, GFP_NOFS
);
7174 bio
->bi_private
= dip
;
7175 bio
->bi_end_io
= btrfs_end_dio_bio
;
7176 atomic_inc(&dip
->pending_bios
);
7178 while (bvec
<= (orig_bio
->bi_io_vec
+ orig_bio
->bi_vcnt
- 1)) {
7179 if (unlikely(map_length
< submit_len
+ bvec
->bv_len
||
7180 bio_add_page(bio
, bvec
->bv_page
, bvec
->bv_len
,
7181 bvec
->bv_offset
) < bvec
->bv_len
)) {
7183 * inc the count before we submit the bio so
7184 * we know the end IO handler won't happen before
7185 * we inc the count. Otherwise, the dip might get freed
7186 * before we're done setting it up
7188 atomic_inc(&dip
->pending_bios
);
7189 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
,
7190 file_offset
, skip_sum
,
7194 atomic_dec(&dip
->pending_bios
);
7198 start_sector
+= submit_len
>> 9;
7199 file_offset
+= submit_len
;
7204 bio
= btrfs_dio_bio_alloc(orig_bio
->bi_bdev
,
7205 start_sector
, GFP_NOFS
);
7208 bio
->bi_private
= dip
;
7209 bio
->bi_end_io
= btrfs_end_dio_bio
;
7211 map_length
= orig_bio
->bi_size
;
7212 ret
= btrfs_map_block(root
->fs_info
, rw
,
7214 &map_length
, NULL
, 0);
7220 submit_len
+= bvec
->bv_len
;
7227 ret
= __btrfs_submit_dio_bio(bio
, inode
, rw
, file_offset
, skip_sum
,
7236 * before atomic variable goto zero, we must
7237 * make sure dip->errors is perceived to be set.
7239 smp_mb__before_atomic_dec();
7240 if (atomic_dec_and_test(&dip
->pending_bios
))
7241 bio_io_error(dip
->orig_bio
);
7243 /* bio_end_io() will handle error, so we needn't return it */
7247 static void btrfs_submit_direct(int rw
, struct bio
*dio_bio
,
7248 struct inode
*inode
, loff_t file_offset
)
7250 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7251 struct btrfs_dio_private
*dip
;
7252 struct bio_vec
*bvec
= dio_bio
->bi_io_vec
;
7255 int write
= rw
& REQ_WRITE
;
7258 skip_sum
= BTRFS_I(inode
)->flags
& BTRFS_INODE_NODATASUM
;
7260 io_bio
= btrfs_bio_clone(dio_bio
, GFP_NOFS
);
7267 dip
= kmalloc(sizeof(*dip
), GFP_NOFS
);
7273 dip
->private = dio_bio
->bi_private
;
7274 io_bio
->bi_private
= dio_bio
->bi_private
;
7276 dip
->logical_offset
= file_offset
;
7280 dip
->bytes
+= bvec
->bv_len
;
7282 } while (bvec
<= (dio_bio
->bi_io_vec
+ dio_bio
->bi_vcnt
- 1));
7284 dip
->disk_bytenr
= (u64
)dio_bio
->bi_sector
<< 9;
7285 io_bio
->bi_private
= dip
;
7287 dip
->orig_bio
= io_bio
;
7288 dip
->dio_bio
= dio_bio
;
7289 atomic_set(&dip
->pending_bios
, 0);
7292 io_bio
->bi_end_io
= btrfs_endio_direct_write
;
7294 io_bio
->bi_end_io
= btrfs_endio_direct_read
;
7296 ret
= btrfs_submit_direct_hook(rw
, dip
, skip_sum
);
7305 * If this is a write, we need to clean up the reserved space and kill
7306 * the ordered extent.
7309 struct btrfs_ordered_extent
*ordered
;
7310 ordered
= btrfs_lookup_ordered_extent(inode
, file_offset
);
7311 if (!test_bit(BTRFS_ORDERED_PREALLOC
, &ordered
->flags
) &&
7312 !test_bit(BTRFS_ORDERED_NOCOW
, &ordered
->flags
))
7313 btrfs_free_reserved_extent(root
, ordered
->start
,
7315 btrfs_put_ordered_extent(ordered
);
7316 btrfs_put_ordered_extent(ordered
);
7318 bio_endio(dio_bio
, ret
);
7321 static ssize_t
check_direct_IO(struct btrfs_root
*root
, int rw
, struct kiocb
*iocb
,
7322 const struct iovec
*iov
, loff_t offset
,
7323 unsigned long nr_segs
)
7329 unsigned blocksize_mask
= root
->sectorsize
- 1;
7330 ssize_t retval
= -EINVAL
;
7331 loff_t end
= offset
;
7333 if (offset
& blocksize_mask
)
7336 /* Check the memory alignment. Blocks cannot straddle pages */
7337 for (seg
= 0; seg
< nr_segs
; seg
++) {
7338 addr
= (unsigned long)iov
[seg
].iov_base
;
7339 size
= iov
[seg
].iov_len
;
7341 if ((addr
& blocksize_mask
) || (size
& blocksize_mask
))
7344 /* If this is a write we don't need to check anymore */
7349 * Check to make sure we don't have duplicate iov_base's in this
7350 * iovec, if so return EINVAL, otherwise we'll get csum errors
7351 * when reading back.
7353 for (i
= seg
+ 1; i
< nr_segs
; i
++) {
7354 if (iov
[seg
].iov_base
== iov
[i
].iov_base
)
7363 static ssize_t
btrfs_direct_IO(int rw
, struct kiocb
*iocb
,
7364 const struct iovec
*iov
, loff_t offset
,
7365 unsigned long nr_segs
)
7367 struct file
*file
= iocb
->ki_filp
;
7368 struct inode
*inode
= file
->f_mapping
->host
;
7372 bool relock
= false;
7375 if (check_direct_IO(BTRFS_I(inode
)->root
, rw
, iocb
, iov
,
7379 atomic_inc(&inode
->i_dio_count
);
7380 smp_mb__after_atomic_inc();
7383 count
= iov_length(iov
, nr_segs
);
7385 * If the write DIO is beyond the EOF, we need update
7386 * the isize, but it is protected by i_mutex. So we can
7387 * not unlock the i_mutex at this case.
7389 if (offset
+ count
<= inode
->i_size
) {
7390 mutex_unlock(&inode
->i_mutex
);
7393 ret
= btrfs_delalloc_reserve_space(inode
, count
);
7396 } else if (unlikely(test_bit(BTRFS_INODE_READDIO_NEED_LOCK
,
7397 &BTRFS_I(inode
)->runtime_flags
))) {
7398 inode_dio_done(inode
);
7399 flags
= DIO_LOCKING
| DIO_SKIP_HOLES
;
7403 ret
= __blockdev_direct_IO(rw
, iocb
, inode
,
7404 BTRFS_I(inode
)->root
->fs_info
->fs_devices
->latest_bdev
,
7405 iov
, offset
, nr_segs
, btrfs_get_blocks_direct
, NULL
,
7406 btrfs_submit_direct
, flags
);
7408 if (ret
< 0 && ret
!= -EIOCBQUEUED
)
7409 btrfs_delalloc_release_space(inode
, count
);
7410 else if (ret
>= 0 && (size_t)ret
< count
)
7411 btrfs_delalloc_release_space(inode
,
7412 count
- (size_t)ret
);
7414 btrfs_delalloc_release_metadata(inode
, 0);
7418 inode_dio_done(inode
);
7420 mutex_lock(&inode
->i_mutex
);
7425 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7427 static int btrfs_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
7428 __u64 start
, __u64 len
)
7432 ret
= fiemap_check_flags(fieinfo
, BTRFS_FIEMAP_FLAGS
);
7436 return extent_fiemap(inode
, fieinfo
, start
, len
, btrfs_get_extent_fiemap
);
7439 int btrfs_readpage(struct file
*file
, struct page
*page
)
7441 struct extent_io_tree
*tree
;
7442 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7443 return extent_read_full_page(tree
, page
, btrfs_get_extent
, 0);
7446 static int btrfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
7448 struct extent_io_tree
*tree
;
7451 if (current
->flags
& PF_MEMALLOC
) {
7452 redirty_page_for_writepage(wbc
, page
);
7456 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7457 return extent_write_full_page(tree
, page
, btrfs_get_extent
, wbc
);
7460 static int btrfs_writepages(struct address_space
*mapping
,
7461 struct writeback_control
*wbc
)
7463 struct extent_io_tree
*tree
;
7465 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7466 return extent_writepages(tree
, mapping
, btrfs_get_extent
, wbc
);
7470 btrfs_readpages(struct file
*file
, struct address_space
*mapping
,
7471 struct list_head
*pages
, unsigned nr_pages
)
7473 struct extent_io_tree
*tree
;
7474 tree
= &BTRFS_I(mapping
->host
)->io_tree
;
7475 return extent_readpages(tree
, mapping
, pages
, nr_pages
,
7478 static int __btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7480 struct extent_io_tree
*tree
;
7481 struct extent_map_tree
*map
;
7484 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
7485 map
= &BTRFS_I(page
->mapping
->host
)->extent_tree
;
7486 ret
= try_release_extent_mapping(map
, tree
, page
, gfp_flags
);
7488 ClearPagePrivate(page
);
7489 set_page_private(page
, 0);
7490 page_cache_release(page
);
7495 static int btrfs_releasepage(struct page
*page
, gfp_t gfp_flags
)
7497 if (PageWriteback(page
) || PageDirty(page
))
7499 return __btrfs_releasepage(page
, gfp_flags
& GFP_NOFS
);
7502 static void btrfs_invalidatepage(struct page
*page
, unsigned long offset
)
7504 struct inode
*inode
= page
->mapping
->host
;
7505 struct extent_io_tree
*tree
;
7506 struct btrfs_ordered_extent
*ordered
;
7507 struct extent_state
*cached_state
= NULL
;
7508 u64 page_start
= page_offset(page
);
7509 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7512 * we have the page locked, so new writeback can't start,
7513 * and the dirty bit won't be cleared while we are here.
7515 * Wait for IO on this page so that we can safely clear
7516 * the PagePrivate2 bit and do ordered accounting
7518 wait_on_page_writeback(page
);
7520 tree
= &BTRFS_I(inode
)->io_tree
;
7522 btrfs_releasepage(page
, GFP_NOFS
);
7525 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7526 ordered
= btrfs_lookup_ordered_extent(inode
, page_offset(page
));
7529 * IO on this page will never be started, so we need
7530 * to account for any ordered extents now
7532 clear_extent_bit(tree
, page_start
, page_end
,
7533 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7534 EXTENT_LOCKED
| EXTENT_DO_ACCOUNTING
|
7535 EXTENT_DEFRAG
, 1, 0, &cached_state
, GFP_NOFS
);
7537 * whoever cleared the private bit is responsible
7538 * for the finish_ordered_io
7540 if (TestClearPagePrivate2(page
) &&
7541 btrfs_dec_test_ordered_pending(inode
, &ordered
, page_start
,
7542 PAGE_CACHE_SIZE
, 1)) {
7543 btrfs_finish_ordered_io(ordered
);
7545 btrfs_put_ordered_extent(ordered
);
7546 cached_state
= NULL
;
7547 lock_extent_bits(tree
, page_start
, page_end
, 0, &cached_state
);
7549 clear_extent_bit(tree
, page_start
, page_end
,
7550 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
7551 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
, 1, 1,
7552 &cached_state
, GFP_NOFS
);
7553 __btrfs_releasepage(page
, GFP_NOFS
);
7555 ClearPageChecked(page
);
7556 if (PagePrivate(page
)) {
7557 ClearPagePrivate(page
);
7558 set_page_private(page
, 0);
7559 page_cache_release(page
);
7564 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
7565 * called from a page fault handler when a page is first dirtied. Hence we must
7566 * be careful to check for EOF conditions here. We set the page up correctly
7567 * for a written page which means we get ENOSPC checking when writing into
7568 * holes and correct delalloc and unwritten extent mapping on filesystems that
7569 * support these features.
7571 * We are not allowed to take the i_mutex here so we have to play games to
7572 * protect against truncate races as the page could now be beyond EOF. Because
7573 * vmtruncate() writes the inode size before removing pages, once we have the
7574 * page lock we can determine safely if the page is beyond EOF. If it is not
7575 * beyond EOF, then the page is guaranteed safe against truncation until we
7578 int btrfs_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
7580 struct page
*page
= vmf
->page
;
7581 struct inode
*inode
= file_inode(vma
->vm_file
);
7582 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7583 struct extent_io_tree
*io_tree
= &BTRFS_I(inode
)->io_tree
;
7584 struct btrfs_ordered_extent
*ordered
;
7585 struct extent_state
*cached_state
= NULL
;
7587 unsigned long zero_start
;
7594 sb_start_pagefault(inode
->i_sb
);
7595 ret
= btrfs_delalloc_reserve_space(inode
, PAGE_CACHE_SIZE
);
7597 ret
= file_update_time(vma
->vm_file
);
7603 else /* -ENOSPC, -EIO, etc */
7604 ret
= VM_FAULT_SIGBUS
;
7610 ret
= VM_FAULT_NOPAGE
; /* make the VM retry the fault */
7613 size
= i_size_read(inode
);
7614 page_start
= page_offset(page
);
7615 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
7617 if ((page
->mapping
!= inode
->i_mapping
) ||
7618 (page_start
>= size
)) {
7619 /* page got truncated out from underneath us */
7622 wait_on_page_writeback(page
);
7624 lock_extent_bits(io_tree
, page_start
, page_end
, 0, &cached_state
);
7625 set_page_extent_mapped(page
);
7628 * we can't set the delalloc bits if there are pending ordered
7629 * extents. Drop our locks and wait for them to finish
7631 ordered
= btrfs_lookup_ordered_extent(inode
, page_start
);
7633 unlock_extent_cached(io_tree
, page_start
, page_end
,
7634 &cached_state
, GFP_NOFS
);
7636 btrfs_start_ordered_extent(inode
, ordered
, 1);
7637 btrfs_put_ordered_extent(ordered
);
7642 * XXX - page_mkwrite gets called every time the page is dirtied, even
7643 * if it was already dirty, so for space accounting reasons we need to
7644 * clear any delalloc bits for the range we are fixing to save. There
7645 * is probably a better way to do this, but for now keep consistent with
7646 * prepare_pages in the normal write path.
7648 clear_extent_bit(&BTRFS_I(inode
)->io_tree
, page_start
, page_end
,
7649 EXTENT_DIRTY
| EXTENT_DELALLOC
|
7650 EXTENT_DO_ACCOUNTING
| EXTENT_DEFRAG
,
7651 0, 0, &cached_state
, GFP_NOFS
);
7653 ret
= btrfs_set_extent_delalloc(inode
, page_start
, page_end
,
7656 unlock_extent_cached(io_tree
, page_start
, page_end
,
7657 &cached_state
, GFP_NOFS
);
7658 ret
= VM_FAULT_SIGBUS
;
7663 /* page is wholly or partially inside EOF */
7664 if (page_start
+ PAGE_CACHE_SIZE
> size
)
7665 zero_start
= size
& ~PAGE_CACHE_MASK
;
7667 zero_start
= PAGE_CACHE_SIZE
;
7669 if (zero_start
!= PAGE_CACHE_SIZE
) {
7671 memset(kaddr
+ zero_start
, 0, PAGE_CACHE_SIZE
- zero_start
);
7672 flush_dcache_page(page
);
7675 ClearPageChecked(page
);
7676 set_page_dirty(page
);
7677 SetPageUptodate(page
);
7679 BTRFS_I(inode
)->last_trans
= root
->fs_info
->generation
;
7680 BTRFS_I(inode
)->last_sub_trans
= BTRFS_I(inode
)->root
->log_transid
;
7681 BTRFS_I(inode
)->last_log_commit
= BTRFS_I(inode
)->root
->last_log_commit
;
7683 unlock_extent_cached(io_tree
, page_start
, page_end
, &cached_state
, GFP_NOFS
);
7687 sb_end_pagefault(inode
->i_sb
);
7688 return VM_FAULT_LOCKED
;
7692 btrfs_delalloc_release_space(inode
, PAGE_CACHE_SIZE
);
7694 sb_end_pagefault(inode
->i_sb
);
7698 static int btrfs_truncate(struct inode
*inode
)
7700 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7701 struct btrfs_block_rsv
*rsv
;
7704 struct btrfs_trans_handle
*trans
;
7705 u64 mask
= root
->sectorsize
- 1;
7706 u64 min_size
= btrfs_calc_trunc_metadata_size(root
, 1);
7708 ret
= btrfs_truncate_page(inode
, inode
->i_size
, 0, 0);
7712 btrfs_wait_ordered_range(inode
, inode
->i_size
& (~mask
), (u64
)-1);
7713 btrfs_ordered_update_i_size(inode
, inode
->i_size
, NULL
);
7716 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
7717 * 3 things going on here
7719 * 1) We need to reserve space for our orphan item and the space to
7720 * delete our orphan item. Lord knows we don't want to have a dangling
7721 * orphan item because we didn't reserve space to remove it.
7723 * 2) We need to reserve space to update our inode.
7725 * 3) We need to have something to cache all the space that is going to
7726 * be free'd up by the truncate operation, but also have some slack
7727 * space reserved in case it uses space during the truncate (thank you
7728 * very much snapshotting).
7730 * And we need these to all be seperate. The fact is we can use alot of
7731 * space doing the truncate, and we have no earthly idea how much space
7732 * we will use, so we need the truncate reservation to be seperate so it
7733 * doesn't end up using space reserved for updating the inode or
7734 * removing the orphan item. We also need to be able to stop the
7735 * transaction and start a new one, which means we need to be able to
7736 * update the inode several times, and we have no idea of knowing how
7737 * many times that will be, so we can't just reserve 1 item for the
7738 * entirety of the opration, so that has to be done seperately as well.
7739 * Then there is the orphan item, which does indeed need to be held on
7740 * to for the whole operation, and we need nobody to touch this reserved
7741 * space except the orphan code.
7743 * So that leaves us with
7745 * 1) root->orphan_block_rsv - for the orphan deletion.
7746 * 2) rsv - for the truncate reservation, which we will steal from the
7747 * transaction reservation.
7748 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
7749 * updating the inode.
7751 rsv
= btrfs_alloc_block_rsv(root
, BTRFS_BLOCK_RSV_TEMP
);
7754 rsv
->size
= min_size
;
7758 * 1 for the truncate slack space
7759 * 1 for updating the inode.
7761 trans
= btrfs_start_transaction(root
, 2);
7762 if (IS_ERR(trans
)) {
7763 err
= PTR_ERR(trans
);
7767 /* Migrate the slack space for the truncate to our reserve */
7768 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
, rsv
,
7773 * setattr is responsible for setting the ordered_data_close flag,
7774 * but that is only tested during the last file release. That
7775 * could happen well after the next commit, leaving a great big
7776 * window where new writes may get lost if someone chooses to write
7777 * to this file after truncating to zero
7779 * The inode doesn't have any dirty data here, and so if we commit
7780 * this is a noop. If someone immediately starts writing to the inode
7781 * it is very likely we'll catch some of their writes in this
7782 * transaction, and the commit will find this file on the ordered
7783 * data list with good things to send down.
7785 * This is a best effort solution, there is still a window where
7786 * using truncate to replace the contents of the file will
7787 * end up with a zero length file after a crash.
7789 if (inode
->i_size
== 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE
,
7790 &BTRFS_I(inode
)->runtime_flags
))
7791 btrfs_add_ordered_operation(trans
, root
, inode
);
7794 * So if we truncate and then write and fsync we normally would just
7795 * write the extents that changed, which is a problem if we need to
7796 * first truncate that entire inode. So set this flag so we write out
7797 * all of the extents in the inode to the sync log so we're completely
7800 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
, &BTRFS_I(inode
)->runtime_flags
);
7801 trans
->block_rsv
= rsv
;
7804 ret
= btrfs_truncate_inode_items(trans
, root
, inode
,
7806 BTRFS_EXTENT_DATA_KEY
);
7807 if (ret
!= -ENOSPC
) {
7812 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7813 ret
= btrfs_update_inode(trans
, root
, inode
);
7819 btrfs_end_transaction(trans
, root
);
7820 btrfs_btree_balance_dirty(root
);
7822 trans
= btrfs_start_transaction(root
, 2);
7823 if (IS_ERR(trans
)) {
7824 ret
= err
= PTR_ERR(trans
);
7829 ret
= btrfs_block_rsv_migrate(&root
->fs_info
->trans_block_rsv
,
7831 BUG_ON(ret
); /* shouldn't happen */
7832 trans
->block_rsv
= rsv
;
7835 if (ret
== 0 && inode
->i_nlink
> 0) {
7836 trans
->block_rsv
= root
->orphan_block_rsv
;
7837 ret
= btrfs_orphan_del(trans
, inode
);
7843 trans
->block_rsv
= &root
->fs_info
->trans_block_rsv
;
7844 ret
= btrfs_update_inode(trans
, root
, inode
);
7848 ret
= btrfs_end_transaction(trans
, root
);
7849 btrfs_btree_balance_dirty(root
);
7853 btrfs_free_block_rsv(root
, rsv
);
7862 * create a new subvolume directory/inode (helper for the ioctl).
7864 int btrfs_create_subvol_root(struct btrfs_trans_handle
*trans
,
7865 struct btrfs_root
*new_root
, u64 new_dirid
)
7867 struct inode
*inode
;
7871 inode
= btrfs_new_inode(trans
, new_root
, NULL
, "..", 2,
7872 new_dirid
, new_dirid
,
7873 S_IFDIR
| (~current_umask() & S_IRWXUGO
),
7876 return PTR_ERR(inode
);
7877 inode
->i_op
= &btrfs_dir_inode_operations
;
7878 inode
->i_fop
= &btrfs_dir_file_operations
;
7880 set_nlink(inode
, 1);
7881 btrfs_i_size_write(inode
, 0);
7883 err
= btrfs_update_inode(trans
, new_root
, inode
);
7889 struct inode
*btrfs_alloc_inode(struct super_block
*sb
)
7891 struct btrfs_inode
*ei
;
7892 struct inode
*inode
;
7894 ei
= kmem_cache_alloc(btrfs_inode_cachep
, GFP_NOFS
);
7901 ei
->last_sub_trans
= 0;
7902 ei
->logged_trans
= 0;
7903 ei
->delalloc_bytes
= 0;
7904 ei
->disk_i_size
= 0;
7907 ei
->index_cnt
= (u64
)-1;
7908 ei
->last_unlink_trans
= 0;
7909 ei
->last_log_commit
= 0;
7911 spin_lock_init(&ei
->lock
);
7912 ei
->outstanding_extents
= 0;
7913 ei
->reserved_extents
= 0;
7915 ei
->runtime_flags
= 0;
7916 ei
->force_compress
= BTRFS_COMPRESS_NONE
;
7918 ei
->delayed_node
= NULL
;
7920 inode
= &ei
->vfs_inode
;
7921 extent_map_tree_init(&ei
->extent_tree
);
7922 extent_io_tree_init(&ei
->io_tree
, &inode
->i_data
);
7923 extent_io_tree_init(&ei
->io_failure_tree
, &inode
->i_data
);
7924 ei
->io_tree
.track_uptodate
= 1;
7925 ei
->io_failure_tree
.track_uptodate
= 1;
7926 atomic_set(&ei
->sync_writers
, 0);
7927 mutex_init(&ei
->log_mutex
);
7928 mutex_init(&ei
->delalloc_mutex
);
7929 btrfs_ordered_inode_tree_init(&ei
->ordered_tree
);
7930 INIT_LIST_HEAD(&ei
->delalloc_inodes
);
7931 INIT_LIST_HEAD(&ei
->ordered_operations
);
7932 RB_CLEAR_NODE(&ei
->rb_node
);
7937 static void btrfs_i_callback(struct rcu_head
*head
)
7939 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
7940 kmem_cache_free(btrfs_inode_cachep
, BTRFS_I(inode
));
7943 void btrfs_destroy_inode(struct inode
*inode
)
7945 struct btrfs_ordered_extent
*ordered
;
7946 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
7948 WARN_ON(!hlist_empty(&inode
->i_dentry
));
7949 WARN_ON(inode
->i_data
.nrpages
);
7950 WARN_ON(BTRFS_I(inode
)->outstanding_extents
);
7951 WARN_ON(BTRFS_I(inode
)->reserved_extents
);
7952 WARN_ON(BTRFS_I(inode
)->delalloc_bytes
);
7953 WARN_ON(BTRFS_I(inode
)->csum_bytes
);
7956 * This can happen where we create an inode, but somebody else also
7957 * created the same inode and we need to destroy the one we already
7964 * Make sure we're properly removed from the ordered operation
7968 if (!list_empty(&BTRFS_I(inode
)->ordered_operations
)) {
7969 spin_lock(&root
->fs_info
->ordered_extent_lock
);
7970 list_del_init(&BTRFS_I(inode
)->ordered_operations
);
7971 spin_unlock(&root
->fs_info
->ordered_extent_lock
);
7974 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM
,
7975 &BTRFS_I(inode
)->runtime_flags
)) {
7976 btrfs_info(root
->fs_info
, "inode %llu still on the orphan list",
7977 (unsigned long long)btrfs_ino(inode
));
7978 atomic_dec(&root
->orphan_inodes
);
7982 ordered
= btrfs_lookup_first_ordered_extent(inode
, (u64
)-1);
7986 btrfs_err(root
->fs_info
, "found ordered extent %llu %llu on inode cleanup",
7987 (unsigned long long)ordered
->file_offset
,
7988 (unsigned long long)ordered
->len
);
7989 btrfs_remove_ordered_extent(inode
, ordered
);
7990 btrfs_put_ordered_extent(ordered
);
7991 btrfs_put_ordered_extent(ordered
);
7994 inode_tree_del(inode
);
7995 btrfs_drop_extent_cache(inode
, 0, (u64
)-1, 0);
7997 call_rcu(&inode
->i_rcu
, btrfs_i_callback
);
8000 int btrfs_drop_inode(struct inode
*inode
)
8002 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8007 /* the snap/subvol tree is on deleting */
8008 if (btrfs_root_refs(&root
->root_item
) == 0 &&
8009 root
!= root
->fs_info
->tree_root
)
8012 return generic_drop_inode(inode
);
8015 static void init_once(void *foo
)
8017 struct btrfs_inode
*ei
= (struct btrfs_inode
*) foo
;
8019 inode_init_once(&ei
->vfs_inode
);
8022 void btrfs_destroy_cachep(void)
8025 * Make sure all delayed rcu free inodes are flushed before we
8029 if (btrfs_inode_cachep
)
8030 kmem_cache_destroy(btrfs_inode_cachep
);
8031 if (btrfs_trans_handle_cachep
)
8032 kmem_cache_destroy(btrfs_trans_handle_cachep
);
8033 if (btrfs_transaction_cachep
)
8034 kmem_cache_destroy(btrfs_transaction_cachep
);
8035 if (btrfs_path_cachep
)
8036 kmem_cache_destroy(btrfs_path_cachep
);
8037 if (btrfs_free_space_cachep
)
8038 kmem_cache_destroy(btrfs_free_space_cachep
);
8039 if (btrfs_delalloc_work_cachep
)
8040 kmem_cache_destroy(btrfs_delalloc_work_cachep
);
8043 int btrfs_init_cachep(void)
8045 btrfs_inode_cachep
= kmem_cache_create("btrfs_inode",
8046 sizeof(struct btrfs_inode
), 0,
8047 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, init_once
);
8048 if (!btrfs_inode_cachep
)
8051 btrfs_trans_handle_cachep
= kmem_cache_create("btrfs_trans_handle",
8052 sizeof(struct btrfs_trans_handle
), 0,
8053 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8054 if (!btrfs_trans_handle_cachep
)
8057 btrfs_transaction_cachep
= kmem_cache_create("btrfs_transaction",
8058 sizeof(struct btrfs_transaction
), 0,
8059 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8060 if (!btrfs_transaction_cachep
)
8063 btrfs_path_cachep
= kmem_cache_create("btrfs_path",
8064 sizeof(struct btrfs_path
), 0,
8065 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8066 if (!btrfs_path_cachep
)
8069 btrfs_free_space_cachep
= kmem_cache_create("btrfs_free_space",
8070 sizeof(struct btrfs_free_space
), 0,
8071 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
8072 if (!btrfs_free_space_cachep
)
8075 btrfs_delalloc_work_cachep
= kmem_cache_create("btrfs_delalloc_work",
8076 sizeof(struct btrfs_delalloc_work
), 0,
8077 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
,
8079 if (!btrfs_delalloc_work_cachep
)
8084 btrfs_destroy_cachep();
8088 static int btrfs_getattr(struct vfsmount
*mnt
,
8089 struct dentry
*dentry
, struct kstat
*stat
)
8092 struct inode
*inode
= dentry
->d_inode
;
8093 u32 blocksize
= inode
->i_sb
->s_blocksize
;
8095 generic_fillattr(inode
, stat
);
8096 stat
->dev
= BTRFS_I(inode
)->root
->anon_dev
;
8097 stat
->blksize
= PAGE_CACHE_SIZE
;
8099 spin_lock(&BTRFS_I(inode
)->lock
);
8100 delalloc_bytes
= BTRFS_I(inode
)->delalloc_bytes
;
8101 spin_unlock(&BTRFS_I(inode
)->lock
);
8102 stat
->blocks
= (ALIGN(inode_get_bytes(inode
), blocksize
) +
8103 ALIGN(delalloc_bytes
, blocksize
)) >> 9;
8107 static int btrfs_rename(struct inode
*old_dir
, struct dentry
*old_dentry
,
8108 struct inode
*new_dir
, struct dentry
*new_dentry
)
8110 struct btrfs_trans_handle
*trans
;
8111 struct btrfs_root
*root
= BTRFS_I(old_dir
)->root
;
8112 struct btrfs_root
*dest
= BTRFS_I(new_dir
)->root
;
8113 struct inode
*new_inode
= new_dentry
->d_inode
;
8114 struct inode
*old_inode
= old_dentry
->d_inode
;
8115 struct timespec ctime
= CURRENT_TIME
;
8119 u64 old_ino
= btrfs_ino(old_inode
);
8121 if (btrfs_ino(new_dir
) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)
8124 /* we only allow rename subvolume link between subvolumes */
8125 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
&& root
!= dest
)
8128 if (old_ino
== BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
||
8129 (new_inode
&& btrfs_ino(new_inode
) == BTRFS_FIRST_FREE_OBJECTID
))
8132 if (S_ISDIR(old_inode
->i_mode
) && new_inode
&&
8133 new_inode
->i_size
> BTRFS_EMPTY_DIR_SIZE
)
8137 /* check for collisions, even if the name isn't there */
8138 ret
= btrfs_check_dir_item_collision(root
, new_dir
->i_ino
,
8139 new_dentry
->d_name
.name
,
8140 new_dentry
->d_name
.len
);
8143 if (ret
== -EEXIST
) {
8145 * eexist without a new_inode */
8151 /* maybe -EOVERFLOW */
8158 * we're using rename to replace one file with another.
8159 * and the replacement file is large. Start IO on it now so
8160 * we don't add too much work to the end of the transaction
8162 if (new_inode
&& S_ISREG(old_inode
->i_mode
) && new_inode
->i_size
&&
8163 old_inode
->i_size
> BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT
)
8164 filemap_flush(old_inode
->i_mapping
);
8166 /* close the racy window with snapshot create/destroy ioctl */
8167 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8168 down_read(&root
->fs_info
->subvol_sem
);
8170 * We want to reserve the absolute worst case amount of items. So if
8171 * both inodes are subvols and we need to unlink them then that would
8172 * require 4 item modifications, but if they are both normal inodes it
8173 * would require 5 item modifications, so we'll assume their normal
8174 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
8175 * should cover the worst case number of items we'll modify.
8177 trans
= btrfs_start_transaction(root
, 11);
8178 if (IS_ERR(trans
)) {
8179 ret
= PTR_ERR(trans
);
8184 btrfs_record_root_in_trans(trans
, dest
);
8186 ret
= btrfs_set_inode_index(new_dir
, &index
);
8190 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8191 /* force full log commit if subvolume involved. */
8192 root
->fs_info
->last_trans_log_full_commit
= trans
->transid
;
8194 ret
= btrfs_insert_inode_ref(trans
, dest
,
8195 new_dentry
->d_name
.name
,
8196 new_dentry
->d_name
.len
,
8198 btrfs_ino(new_dir
), index
);
8202 * this is an ugly little race, but the rename is required
8203 * to make sure that if we crash, the inode is either at the
8204 * old name or the new one. pinning the log transaction lets
8205 * us make sure we don't allow a log commit to come in after
8206 * we unlink the name but before we add the new name back in.
8208 btrfs_pin_log_trans(root
);
8211 * make sure the inode gets flushed if it is replacing
8214 if (new_inode
&& new_inode
->i_size
&& S_ISREG(old_inode
->i_mode
))
8215 btrfs_add_ordered_operation(trans
, root
, old_inode
);
8217 inode_inc_iversion(old_dir
);
8218 inode_inc_iversion(new_dir
);
8219 inode_inc_iversion(old_inode
);
8220 old_dir
->i_ctime
= old_dir
->i_mtime
= ctime
;
8221 new_dir
->i_ctime
= new_dir
->i_mtime
= ctime
;
8222 old_inode
->i_ctime
= ctime
;
8224 if (old_dentry
->d_parent
!= new_dentry
->d_parent
)
8225 btrfs_record_unlink_dir(trans
, old_dir
, old_inode
, 1);
8227 if (unlikely(old_ino
== BTRFS_FIRST_FREE_OBJECTID
)) {
8228 root_objectid
= BTRFS_I(old_inode
)->root
->root_key
.objectid
;
8229 ret
= btrfs_unlink_subvol(trans
, root
, old_dir
, root_objectid
,
8230 old_dentry
->d_name
.name
,
8231 old_dentry
->d_name
.len
);
8233 ret
= __btrfs_unlink_inode(trans
, root
, old_dir
,
8234 old_dentry
->d_inode
,
8235 old_dentry
->d_name
.name
,
8236 old_dentry
->d_name
.len
);
8238 ret
= btrfs_update_inode(trans
, root
, old_inode
);
8241 btrfs_abort_transaction(trans
, root
, ret
);
8246 inode_inc_iversion(new_inode
);
8247 new_inode
->i_ctime
= CURRENT_TIME
;
8248 if (unlikely(btrfs_ino(new_inode
) ==
8249 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID
)) {
8250 root_objectid
= BTRFS_I(new_inode
)->location
.objectid
;
8251 ret
= btrfs_unlink_subvol(trans
, dest
, new_dir
,
8253 new_dentry
->d_name
.name
,
8254 new_dentry
->d_name
.len
);
8255 BUG_ON(new_inode
->i_nlink
== 0);
8257 ret
= btrfs_unlink_inode(trans
, dest
, new_dir
,
8258 new_dentry
->d_inode
,
8259 new_dentry
->d_name
.name
,
8260 new_dentry
->d_name
.len
);
8262 if (!ret
&& new_inode
->i_nlink
== 0) {
8263 ret
= btrfs_orphan_add(trans
, new_dentry
->d_inode
);
8267 btrfs_abort_transaction(trans
, root
, ret
);
8272 ret
= btrfs_add_link(trans
, new_dir
, old_inode
,
8273 new_dentry
->d_name
.name
,
8274 new_dentry
->d_name
.len
, 0, index
);
8276 btrfs_abort_transaction(trans
, root
, ret
);
8280 if (old_ino
!= BTRFS_FIRST_FREE_OBJECTID
) {
8281 struct dentry
*parent
= new_dentry
->d_parent
;
8282 btrfs_log_new_name(trans
, old_inode
, old_dir
, parent
);
8283 btrfs_end_log_trans(root
);
8286 btrfs_end_transaction(trans
, root
);
8288 if (old_ino
== BTRFS_FIRST_FREE_OBJECTID
)
8289 up_read(&root
->fs_info
->subvol_sem
);
8294 static void btrfs_run_delalloc_work(struct btrfs_work
*work
)
8296 struct btrfs_delalloc_work
*delalloc_work
;
8298 delalloc_work
= container_of(work
, struct btrfs_delalloc_work
,
8300 if (delalloc_work
->wait
)
8301 btrfs_wait_ordered_range(delalloc_work
->inode
, 0, (u64
)-1);
8303 filemap_flush(delalloc_work
->inode
->i_mapping
);
8305 if (delalloc_work
->delay_iput
)
8306 btrfs_add_delayed_iput(delalloc_work
->inode
);
8308 iput(delalloc_work
->inode
);
8309 complete(&delalloc_work
->completion
);
8312 struct btrfs_delalloc_work
*btrfs_alloc_delalloc_work(struct inode
*inode
,
8313 int wait
, int delay_iput
)
8315 struct btrfs_delalloc_work
*work
;
8317 work
= kmem_cache_zalloc(btrfs_delalloc_work_cachep
, GFP_NOFS
);
8321 init_completion(&work
->completion
);
8322 INIT_LIST_HEAD(&work
->list
);
8323 work
->inode
= inode
;
8325 work
->delay_iput
= delay_iput
;
8326 work
->work
.func
= btrfs_run_delalloc_work
;
8331 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work
*work
)
8333 wait_for_completion(&work
->completion
);
8334 kmem_cache_free(btrfs_delalloc_work_cachep
, work
);
8338 * some fairly slow code that needs optimization. This walks the list
8339 * of all the inodes with pending delalloc and forces them to disk.
8341 int btrfs_start_delalloc_inodes(struct btrfs_root
*root
, int delay_iput
)
8343 struct btrfs_inode
*binode
;
8344 struct inode
*inode
;
8345 struct btrfs_delalloc_work
*work
, *next
;
8346 struct list_head works
;
8347 struct list_head splice
;
8350 if (root
->fs_info
->sb
->s_flags
& MS_RDONLY
)
8353 INIT_LIST_HEAD(&works
);
8354 INIT_LIST_HEAD(&splice
);
8356 spin_lock(&root
->fs_info
->delalloc_lock
);
8357 list_splice_init(&root
->fs_info
->delalloc_inodes
, &splice
);
8358 while (!list_empty(&splice
)) {
8359 binode
= list_entry(splice
.next
, struct btrfs_inode
,
8362 list_del_init(&binode
->delalloc_inodes
);
8364 inode
= igrab(&binode
->vfs_inode
);
8366 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST
,
8367 &binode
->runtime_flags
);
8371 list_add_tail(&binode
->delalloc_inodes
,
8372 &root
->fs_info
->delalloc_inodes
);
8373 spin_unlock(&root
->fs_info
->delalloc_lock
);
8375 work
= btrfs_alloc_delalloc_work(inode
, 0, delay_iput
);
8376 if (unlikely(!work
)) {
8380 list_add_tail(&work
->list
, &works
);
8381 btrfs_queue_worker(&root
->fs_info
->flush_workers
,
8385 spin_lock(&root
->fs_info
->delalloc_lock
);
8387 spin_unlock(&root
->fs_info
->delalloc_lock
);
8389 list_for_each_entry_safe(work
, next
, &works
, list
) {
8390 list_del_init(&work
->list
);
8391 btrfs_wait_and_free_delalloc_work(work
);
8394 /* the filemap_flush will queue IO into the worker threads, but
8395 * we have to make sure the IO is actually started and that
8396 * ordered extents get created before we return
8398 atomic_inc(&root
->fs_info
->async_submit_draining
);
8399 while (atomic_read(&root
->fs_info
->nr_async_submits
) ||
8400 atomic_read(&root
->fs_info
->async_delalloc_pages
)) {
8401 wait_event(root
->fs_info
->async_submit_wait
,
8402 (atomic_read(&root
->fs_info
->nr_async_submits
) == 0 &&
8403 atomic_read(&root
->fs_info
->async_delalloc_pages
) == 0));
8405 atomic_dec(&root
->fs_info
->async_submit_draining
);
8408 list_for_each_entry_safe(work
, next
, &works
, list
) {
8409 list_del_init(&work
->list
);
8410 btrfs_wait_and_free_delalloc_work(work
);
8413 if (!list_empty_careful(&splice
)) {
8414 spin_lock(&root
->fs_info
->delalloc_lock
);
8415 list_splice_tail(&splice
, &root
->fs_info
->delalloc_inodes
);
8416 spin_unlock(&root
->fs_info
->delalloc_lock
);
8421 static int btrfs_symlink(struct inode
*dir
, struct dentry
*dentry
,
8422 const char *symname
)
8424 struct btrfs_trans_handle
*trans
;
8425 struct btrfs_root
*root
= BTRFS_I(dir
)->root
;
8426 struct btrfs_path
*path
;
8427 struct btrfs_key key
;
8428 struct inode
*inode
= NULL
;
8436 struct btrfs_file_extent_item
*ei
;
8437 struct extent_buffer
*leaf
;
8439 name_len
= strlen(symname
) + 1;
8440 if (name_len
> BTRFS_MAX_INLINE_DATA_SIZE(root
))
8441 return -ENAMETOOLONG
;
8444 * 2 items for inode item and ref
8445 * 2 items for dir items
8446 * 1 item for xattr if selinux is on
8448 trans
= btrfs_start_transaction(root
, 5);
8450 return PTR_ERR(trans
);
8452 err
= btrfs_find_free_ino(root
, &objectid
);
8456 inode
= btrfs_new_inode(trans
, root
, dir
, dentry
->d_name
.name
,
8457 dentry
->d_name
.len
, btrfs_ino(dir
), objectid
,
8458 S_IFLNK
|S_IRWXUGO
, &index
);
8459 if (IS_ERR(inode
)) {
8460 err
= PTR_ERR(inode
);
8464 err
= btrfs_init_inode_security(trans
, inode
, dir
, &dentry
->d_name
);
8471 * If the active LSM wants to access the inode during
8472 * d_instantiate it needs these. Smack checks to see
8473 * if the filesystem supports xattrs by looking at the
8476 inode
->i_fop
= &btrfs_file_operations
;
8477 inode
->i_op
= &btrfs_file_inode_operations
;
8479 err
= btrfs_add_nondir(trans
, dir
, dentry
, inode
, 0, index
);
8483 inode
->i_mapping
->a_ops
= &btrfs_aops
;
8484 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8485 BTRFS_I(inode
)->io_tree
.ops
= &btrfs_extent_io_ops
;
8490 path
= btrfs_alloc_path();
8496 key
.objectid
= btrfs_ino(inode
);
8498 btrfs_set_key_type(&key
, BTRFS_EXTENT_DATA_KEY
);
8499 datasize
= btrfs_file_extent_calc_inline_size(name_len
);
8500 err
= btrfs_insert_empty_item(trans
, root
, path
, &key
,
8504 btrfs_free_path(path
);
8507 leaf
= path
->nodes
[0];
8508 ei
= btrfs_item_ptr(leaf
, path
->slots
[0],
8509 struct btrfs_file_extent_item
);
8510 btrfs_set_file_extent_generation(leaf
, ei
, trans
->transid
);
8511 btrfs_set_file_extent_type(leaf
, ei
,
8512 BTRFS_FILE_EXTENT_INLINE
);
8513 btrfs_set_file_extent_encryption(leaf
, ei
, 0);
8514 btrfs_set_file_extent_compression(leaf
, ei
, 0);
8515 btrfs_set_file_extent_other_encoding(leaf
, ei
, 0);
8516 btrfs_set_file_extent_ram_bytes(leaf
, ei
, name_len
);
8518 ptr
= btrfs_file_extent_inline_start(ei
);
8519 write_extent_buffer(leaf
, symname
, ptr
, name_len
);
8520 btrfs_mark_buffer_dirty(leaf
);
8521 btrfs_free_path(path
);
8523 inode
->i_op
= &btrfs_symlink_inode_operations
;
8524 inode
->i_mapping
->a_ops
= &btrfs_symlink_aops
;
8525 inode
->i_mapping
->backing_dev_info
= &root
->fs_info
->bdi
;
8526 inode_set_bytes(inode
, name_len
);
8527 btrfs_i_size_write(inode
, name_len
- 1);
8528 err
= btrfs_update_inode(trans
, root
, inode
);
8534 d_instantiate(dentry
, inode
);
8535 btrfs_end_transaction(trans
, root
);
8537 inode_dec_link_count(inode
);
8540 btrfs_btree_balance_dirty(root
);
8544 static int __btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8545 u64 start
, u64 num_bytes
, u64 min_size
,
8546 loff_t actual_len
, u64
*alloc_hint
,
8547 struct btrfs_trans_handle
*trans
)
8549 struct extent_map_tree
*em_tree
= &BTRFS_I(inode
)->extent_tree
;
8550 struct extent_map
*em
;
8551 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8552 struct btrfs_key ins
;
8553 u64 cur_offset
= start
;
8557 bool own_trans
= true;
8561 while (num_bytes
> 0) {
8563 trans
= btrfs_start_transaction(root
, 3);
8564 if (IS_ERR(trans
)) {
8565 ret
= PTR_ERR(trans
);
8570 cur_bytes
= min(num_bytes
, 256ULL * 1024 * 1024);
8571 cur_bytes
= max(cur_bytes
, min_size
);
8572 ret
= btrfs_reserve_extent(trans
, root
, cur_bytes
,
8573 min_size
, 0, *alloc_hint
, &ins
, 1);
8576 btrfs_end_transaction(trans
, root
);
8580 ret
= insert_reserved_file_extent(trans
, inode
,
8581 cur_offset
, ins
.objectid
,
8582 ins
.offset
, ins
.offset
,
8583 ins
.offset
, 0, 0, 0,
8584 BTRFS_FILE_EXTENT_PREALLOC
);
8586 btrfs_abort_transaction(trans
, root
, ret
);
8588 btrfs_end_transaction(trans
, root
);
8591 btrfs_drop_extent_cache(inode
, cur_offset
,
8592 cur_offset
+ ins
.offset
-1, 0);
8594 em
= alloc_extent_map();
8596 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC
,
8597 &BTRFS_I(inode
)->runtime_flags
);
8601 em
->start
= cur_offset
;
8602 em
->orig_start
= cur_offset
;
8603 em
->len
= ins
.offset
;
8604 em
->block_start
= ins
.objectid
;
8605 em
->block_len
= ins
.offset
;
8606 em
->orig_block_len
= ins
.offset
;
8607 em
->ram_bytes
= ins
.offset
;
8608 em
->bdev
= root
->fs_info
->fs_devices
->latest_bdev
;
8609 set_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
);
8610 em
->generation
= trans
->transid
;
8613 write_lock(&em_tree
->lock
);
8614 ret
= add_extent_mapping(em_tree
, em
, 1);
8615 write_unlock(&em_tree
->lock
);
8618 btrfs_drop_extent_cache(inode
, cur_offset
,
8619 cur_offset
+ ins
.offset
- 1,
8622 free_extent_map(em
);
8624 num_bytes
-= ins
.offset
;
8625 cur_offset
+= ins
.offset
;
8626 *alloc_hint
= ins
.objectid
+ ins
.offset
;
8628 inode_inc_iversion(inode
);
8629 inode
->i_ctime
= CURRENT_TIME
;
8630 BTRFS_I(inode
)->flags
|= BTRFS_INODE_PREALLOC
;
8631 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
8632 (actual_len
> inode
->i_size
) &&
8633 (cur_offset
> inode
->i_size
)) {
8634 if (cur_offset
> actual_len
)
8635 i_size
= actual_len
;
8637 i_size
= cur_offset
;
8638 i_size_write(inode
, i_size
);
8639 btrfs_ordered_update_i_size(inode
, i_size
, NULL
);
8642 ret
= btrfs_update_inode(trans
, root
, inode
);
8645 btrfs_abort_transaction(trans
, root
, ret
);
8647 btrfs_end_transaction(trans
, root
);
8652 btrfs_end_transaction(trans
, root
);
8657 int btrfs_prealloc_file_range(struct inode
*inode
, int mode
,
8658 u64 start
, u64 num_bytes
, u64 min_size
,
8659 loff_t actual_len
, u64
*alloc_hint
)
8661 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8662 min_size
, actual_len
, alloc_hint
,
8666 int btrfs_prealloc_file_range_trans(struct inode
*inode
,
8667 struct btrfs_trans_handle
*trans
, int mode
,
8668 u64 start
, u64 num_bytes
, u64 min_size
,
8669 loff_t actual_len
, u64
*alloc_hint
)
8671 return __btrfs_prealloc_file_range(inode
, mode
, start
, num_bytes
,
8672 min_size
, actual_len
, alloc_hint
, trans
);
8675 static int btrfs_set_page_dirty(struct page
*page
)
8677 return __set_page_dirty_nobuffers(page
);
8680 static int btrfs_permission(struct inode
*inode
, int mask
)
8682 struct btrfs_root
*root
= BTRFS_I(inode
)->root
;
8683 umode_t mode
= inode
->i_mode
;
8685 if (mask
& MAY_WRITE
&&
8686 (S_ISREG(mode
) || S_ISDIR(mode
) || S_ISLNK(mode
))) {
8687 if (btrfs_root_readonly(root
))
8689 if (BTRFS_I(inode
)->flags
& BTRFS_INODE_READONLY
)
8692 return generic_permission(inode
, mask
);
8695 static const struct inode_operations btrfs_dir_inode_operations
= {
8696 .getattr
= btrfs_getattr
,
8697 .lookup
= btrfs_lookup
,
8698 .create
= btrfs_create
,
8699 .unlink
= btrfs_unlink
,
8701 .mkdir
= btrfs_mkdir
,
8702 .rmdir
= btrfs_rmdir
,
8703 .rename
= btrfs_rename
,
8704 .symlink
= btrfs_symlink
,
8705 .setattr
= btrfs_setattr
,
8706 .mknod
= btrfs_mknod
,
8707 .setxattr
= btrfs_setxattr
,
8708 .getxattr
= btrfs_getxattr
,
8709 .listxattr
= btrfs_listxattr
,
8710 .removexattr
= btrfs_removexattr
,
8711 .permission
= btrfs_permission
,
8712 .get_acl
= btrfs_get_acl
,
8714 static const struct inode_operations btrfs_dir_ro_inode_operations
= {
8715 .lookup
= btrfs_lookup
,
8716 .permission
= btrfs_permission
,
8717 .get_acl
= btrfs_get_acl
,
8720 static const struct file_operations btrfs_dir_file_operations
= {
8721 .llseek
= generic_file_llseek
,
8722 .read
= generic_read_dir
,
8723 .readdir
= btrfs_real_readdir
,
8724 .unlocked_ioctl
= btrfs_ioctl
,
8725 #ifdef CONFIG_COMPAT
8726 .compat_ioctl
= btrfs_ioctl
,
8728 .release
= btrfs_release_file
,
8729 .fsync
= btrfs_sync_file
,
8732 static struct extent_io_ops btrfs_extent_io_ops
= {
8733 .fill_delalloc
= run_delalloc_range
,
8734 .submit_bio_hook
= btrfs_submit_bio_hook
,
8735 .merge_bio_hook
= btrfs_merge_bio_hook
,
8736 .readpage_end_io_hook
= btrfs_readpage_end_io_hook
,
8737 .writepage_end_io_hook
= btrfs_writepage_end_io_hook
,
8738 .writepage_start_hook
= btrfs_writepage_start_hook
,
8739 .set_bit_hook
= btrfs_set_bit_hook
,
8740 .clear_bit_hook
= btrfs_clear_bit_hook
,
8741 .merge_extent_hook
= btrfs_merge_extent_hook
,
8742 .split_extent_hook
= btrfs_split_extent_hook
,
8746 * btrfs doesn't support the bmap operation because swapfiles
8747 * use bmap to make a mapping of extents in the file. They assume
8748 * these extents won't change over the life of the file and they
8749 * use the bmap result to do IO directly to the drive.
8751 * the btrfs bmap call would return logical addresses that aren't
8752 * suitable for IO and they also will change frequently as COW
8753 * operations happen. So, swapfile + btrfs == corruption.
8755 * For now we're avoiding this by dropping bmap.
8757 static const struct address_space_operations btrfs_aops
= {
8758 .readpage
= btrfs_readpage
,
8759 .writepage
= btrfs_writepage
,
8760 .writepages
= btrfs_writepages
,
8761 .readpages
= btrfs_readpages
,
8762 .direct_IO
= btrfs_direct_IO
,
8763 .invalidatepage
= btrfs_invalidatepage
,
8764 .releasepage
= btrfs_releasepage
,
8765 .set_page_dirty
= btrfs_set_page_dirty
,
8766 .error_remove_page
= generic_error_remove_page
,
8769 static const struct address_space_operations btrfs_symlink_aops
= {
8770 .readpage
= btrfs_readpage
,
8771 .writepage
= btrfs_writepage
,
8772 .invalidatepage
= btrfs_invalidatepage
,
8773 .releasepage
= btrfs_releasepage
,
8776 static const struct inode_operations btrfs_file_inode_operations
= {
8777 .getattr
= btrfs_getattr
,
8778 .setattr
= btrfs_setattr
,
8779 .setxattr
= btrfs_setxattr
,
8780 .getxattr
= btrfs_getxattr
,
8781 .listxattr
= btrfs_listxattr
,
8782 .removexattr
= btrfs_removexattr
,
8783 .permission
= btrfs_permission
,
8784 .fiemap
= btrfs_fiemap
,
8785 .get_acl
= btrfs_get_acl
,
8786 .update_time
= btrfs_update_time
,
8788 static const struct inode_operations btrfs_special_inode_operations
= {
8789 .getattr
= btrfs_getattr
,
8790 .setattr
= btrfs_setattr
,
8791 .permission
= btrfs_permission
,
8792 .setxattr
= btrfs_setxattr
,
8793 .getxattr
= btrfs_getxattr
,
8794 .listxattr
= btrfs_listxattr
,
8795 .removexattr
= btrfs_removexattr
,
8796 .get_acl
= btrfs_get_acl
,
8797 .update_time
= btrfs_update_time
,
8799 static const struct inode_operations btrfs_symlink_inode_operations
= {
8800 .readlink
= generic_readlink
,
8801 .follow_link
= page_follow_link_light
,
8802 .put_link
= page_put_link
,
8803 .getattr
= btrfs_getattr
,
8804 .setattr
= btrfs_setattr
,
8805 .permission
= btrfs_permission
,
8806 .setxattr
= btrfs_setxattr
,
8807 .getxattr
= btrfs_getxattr
,
8808 .listxattr
= btrfs_listxattr
,
8809 .removexattr
= btrfs_removexattr
,
8810 .get_acl
= btrfs_get_acl
,
8811 .update_time
= btrfs_update_time
,
8814 const struct dentry_operations btrfs_dentry_operations
= {
8815 .d_delete
= btrfs_dentry_delete
,
8816 .d_release
= btrfs_dentry_release
,